blob: ab8131a8e48937e33ef01ec43dcc60e278f934f2 [file] [log] [blame]
Paul Moore96cb8e32006-08-03 16:48:59 -07001/*
2 * NetLabel Unlabeled Support
3 *
4 * This file defines functions for dealing with unlabeled packets for the
5 * NetLabel system. The NetLabel system manages static and dynamic label
6 * mappings for network protocols such as CIPSO and RIPSO.
7 *
8 * Author: Paul Moore <paul.moore@hp.com>
9 *
10 */
11
12/*
Paul Moore61e10682008-10-10 10:16:32 -040013 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006 - 2008
Paul Moore96cb8e32006-08-03 16:48:59 -070014 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
23 * the GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 *
29 */
30
31#include <linux/types.h>
Paul Moore8cc44572008-01-29 08:44:21 -050032#include <linux/rcupdate.h>
Paul Moore96cb8e32006-08-03 16:48:59 -070033#include <linux/list.h>
34#include <linux/spinlock.h>
35#include <linux/socket.h>
36#include <linux/string.h>
37#include <linux/skbuff.h>
Paul Moorede646882006-11-17 17:38:55 -050038#include <linux/audit.h>
Paul Moore8cc44572008-01-29 08:44:21 -050039#include <linux/in.h>
40#include <linux/in6.h>
41#include <linux/ip.h>
42#include <linux/ipv6.h>
43#include <linux/notifier.h>
44#include <linux/netdevice.h>
45#include <linux/security.h>
Paul Moore96cb8e32006-08-03 16:48:59 -070046#include <net/sock.h>
47#include <net/netlink.h>
48#include <net/genetlink.h>
Paul Moore8cc44572008-01-29 08:44:21 -050049#include <net/ip.h>
50#include <net/ipv6.h>
51#include <net/net_namespace.h>
Paul Moore96cb8e32006-08-03 16:48:59 -070052#include <net/netlabel.h>
53#include <asm/bug.h>
Paul Moore8cc44572008-01-29 08:44:21 -050054#include <asm/atomic.h>
Paul Moore96cb8e32006-08-03 16:48:59 -070055
56#include "netlabel_user.h"
Paul Moore61e10682008-10-10 10:16:32 -040057#include "netlabel_addrlist.h"
Paul Moore96cb8e32006-08-03 16:48:59 -070058#include "netlabel_domainhash.h"
59#include "netlabel_unlabeled.h"
Paul Moore8cc44572008-01-29 08:44:21 -050060#include "netlabel_mgmt.h"
61
62/* NOTE: at present we always use init's network namespace since we don't
63 * presently support different namespaces even though the majority of
64 * the functions in this file are "namespace safe" */
65
66/* The unlabeled connection hash table which we use to map network interfaces
67 * and addresses of unlabeled packets to a user specified secid value for the
68 * LSM. The hash table is used to lookup the network interface entry
69 * (struct netlbl_unlhsh_iface) and then the interface entry is used to
70 * lookup an IP address match from an ordered list. If a network interface
71 * match can not be found in the hash table then the default entry
72 * (netlbl_unlhsh_def) is used. The IP address entry list
73 * (struct netlbl_unlhsh_addr) is ordered such that the entries with a
74 * larger netmask come first.
75 */
76struct netlbl_unlhsh_tbl {
77 struct list_head *tbl;
78 u32 size;
79};
Paul Moore61e10682008-10-10 10:16:32 -040080#define netlbl_unlhsh_addr4_entry(iter) \
81 container_of(iter, struct netlbl_unlhsh_addr4, list)
Paul Moore8cc44572008-01-29 08:44:21 -050082struct netlbl_unlhsh_addr4 {
Paul Moore8cc44572008-01-29 08:44:21 -050083 u32 secid;
84
Paul Moore61e10682008-10-10 10:16:32 -040085 struct netlbl_af4list list;
Paul Moore8cc44572008-01-29 08:44:21 -050086 struct rcu_head rcu;
87};
Paul Moore61e10682008-10-10 10:16:32 -040088#define netlbl_unlhsh_addr6_entry(iter) \
89 container_of(iter, struct netlbl_unlhsh_addr6, list)
Paul Moore8cc44572008-01-29 08:44:21 -050090struct netlbl_unlhsh_addr6 {
Paul Moore8cc44572008-01-29 08:44:21 -050091 u32 secid;
92
Paul Moore61e10682008-10-10 10:16:32 -040093 struct netlbl_af6list list;
Paul Moore8cc44572008-01-29 08:44:21 -050094 struct rcu_head rcu;
95};
96struct netlbl_unlhsh_iface {
97 int ifindex;
98 struct list_head addr4_list;
99 struct list_head addr6_list;
100
101 u32 valid;
102 struct list_head list;
103 struct rcu_head rcu;
104};
105
106/* Argument struct for netlbl_unlhsh_walk() */
107struct netlbl_unlhsh_walk_arg {
108 struct netlink_callback *nl_cb;
109 struct sk_buff *skb;
110 u32 seq;
111};
112
113/* Unlabeled connection hash table */
114/* updates should be so rare that having one spinlock for the entire
115 * hash table should be okay */
116static DEFINE_SPINLOCK(netlbl_unlhsh_lock);
117static struct netlbl_unlhsh_tbl *netlbl_unlhsh = NULL;
118static struct netlbl_unlhsh_iface *netlbl_unlhsh_def = NULL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700119
120/* Accept unlabeled packets flag */
Paul Moorecd287862006-11-17 17:38:44 -0500121static u8 netlabel_unlabel_acceptflg = 0;
Paul Moore96cb8e32006-08-03 16:48:59 -0700122
Paul Moore8cc44572008-01-29 08:44:21 -0500123/* NetLabel Generic NETLINK unlabeled family */
Paul Moore96cb8e32006-08-03 16:48:59 -0700124static struct genl_family netlbl_unlabel_gnl_family = {
125 .id = GENL_ID_GENERATE,
126 .hdrsize = 0,
127 .name = NETLBL_NLTYPE_UNLABELED_NAME,
128 .version = NETLBL_PROTO_VERSION,
Paul Moorefd385852006-09-25 15:56:37 -0700129 .maxattr = NLBL_UNLABEL_A_MAX,
Paul Moore96cb8e32006-08-03 16:48:59 -0700130};
131
Paul Moorefd385852006-09-25 15:56:37 -0700132/* NetLabel Netlink attribute policy */
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700133static const struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = {
Paul Moorefd385852006-09-25 15:56:37 -0700134 [NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 },
Paul Moore8cc44572008-01-29 08:44:21 -0500135 [NLBL_UNLABEL_A_IPV6ADDR] = { .type = NLA_BINARY,
136 .len = sizeof(struct in6_addr) },
137 [NLBL_UNLABEL_A_IPV6MASK] = { .type = NLA_BINARY,
138 .len = sizeof(struct in6_addr) },
139 [NLBL_UNLABEL_A_IPV4ADDR] = { .type = NLA_BINARY,
140 .len = sizeof(struct in_addr) },
141 [NLBL_UNLABEL_A_IPV4MASK] = { .type = NLA_BINARY,
142 .len = sizeof(struct in_addr) },
143 [NLBL_UNLABEL_A_IFACE] = { .type = NLA_NUL_STRING,
144 .len = IFNAMSIZ - 1 },
145 [NLBL_UNLABEL_A_SECCTX] = { .type = NLA_BINARY }
Paul Moorefd385852006-09-25 15:56:37 -0700146};
Paul Moore96cb8e32006-08-03 16:48:59 -0700147
148/*
Paul Moore13541b32008-01-29 08:44:23 -0500149 * Audit Helper Functions
150 */
151
152/**
153 * netlbl_unlabel_audit_addr4 - Audit an IPv4 address
154 * @audit_buf: audit buffer
155 * @dev: network interface
156 * @addr: IP address
157 * @mask: IP address mask
158 *
159 * Description:
160 * Write the IPv4 address and address mask, if necessary, to @audit_buf.
161 *
162 */
163static void netlbl_unlabel_audit_addr4(struct audit_buffer *audit_buf,
164 const char *dev,
165 __be32 addr, __be32 mask)
166{
167 u32 mask_val = ntohl(mask);
168
169 if (dev != NULL)
170 audit_log_format(audit_buf, " netif=%s", dev);
171 audit_log_format(audit_buf, " src=" NIPQUAD_FMT, NIPQUAD(addr));
172 if (mask_val != 0xffffffff) {
173 u32 mask_len = 0;
174 while (mask_val > 0) {
175 mask_val <<= 1;
176 mask_len++;
177 }
178 audit_log_format(audit_buf, " src_prefixlen=%d", mask_len);
179 }
180}
181
Pavel Emelyanov370125f2008-02-12 22:38:06 -0800182#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Paul Moore13541b32008-01-29 08:44:23 -0500183/**
184 * netlbl_unlabel_audit_addr6 - Audit an IPv6 address
185 * @audit_buf: audit buffer
186 * @dev: network interface
187 * @addr: IP address
188 * @mask: IP address mask
189 *
190 * Description:
191 * Write the IPv6 address and address mask, if necessary, to @audit_buf.
192 *
193 */
194static void netlbl_unlabel_audit_addr6(struct audit_buffer *audit_buf,
195 const char *dev,
196 const struct in6_addr *addr,
197 const struct in6_addr *mask)
198{
199 if (dev != NULL)
200 audit_log_format(audit_buf, " netif=%s", dev);
201 audit_log_format(audit_buf, " src=" NIP6_FMT, NIP6(*addr));
202 if (ntohl(mask->s6_addr32[3]) != 0xffffffff) {
203 u32 mask_len = 0;
204 u32 mask_val;
205 int iter = -1;
206 while (ntohl(mask->s6_addr32[++iter]) == 0xffffffff)
207 mask_len += 32;
208 mask_val = ntohl(mask->s6_addr32[iter]);
209 while (mask_val > 0) {
210 mask_val <<= 1;
211 mask_len++;
212 }
213 audit_log_format(audit_buf, " src_prefixlen=%d", mask_len);
214 }
215}
Pavel Emelyanov370125f2008-02-12 22:38:06 -0800216#endif /* IPv6 */
Paul Moore13541b32008-01-29 08:44:23 -0500217
218/*
Paul Moore8cc44572008-01-29 08:44:21 -0500219 * Unlabeled Connection Hash Table Functions
Paul Moore32f50cd2006-09-28 14:51:47 -0700220 */
221
222/**
Paul Moore8cc44572008-01-29 08:44:21 -0500223 * netlbl_unlhsh_free_addr4 - Frees an IPv4 address entry from the hash table
224 * @entry: the entry's RCU field
225 *
226 * Description:
227 * This function is designed to be used as a callback to the call_rcu()
228 * function so that memory allocated to a hash table address entry can be
229 * released safely.
230 *
231 */
232static void netlbl_unlhsh_free_addr4(struct rcu_head *entry)
233{
234 struct netlbl_unlhsh_addr4 *ptr;
235
236 ptr = container_of(entry, struct netlbl_unlhsh_addr4, rcu);
237 kfree(ptr);
238}
239
240#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
241/**
242 * netlbl_unlhsh_free_addr6 - Frees an IPv6 address entry from the hash table
243 * @entry: the entry's RCU field
244 *
245 * Description:
246 * This function is designed to be used as a callback to the call_rcu()
247 * function so that memory allocated to a hash table address entry can be
248 * released safely.
249 *
250 */
251static void netlbl_unlhsh_free_addr6(struct rcu_head *entry)
252{
253 struct netlbl_unlhsh_addr6 *ptr;
254
255 ptr = container_of(entry, struct netlbl_unlhsh_addr6, rcu);
256 kfree(ptr);
257}
258#endif /* IPv6 */
259
260/**
261 * netlbl_unlhsh_free_iface - Frees an interface entry from the hash table
262 * @entry: the entry's RCU field
263 *
264 * Description:
265 * This function is designed to be used as a callback to the call_rcu()
266 * function so that memory allocated to a hash table interface entry can be
267 * released safely. It is important to note that this function does not free
268 * the IPv4 and IPv6 address lists contained as part of an interface entry. It
269 * is up to the rest of the code to make sure an interface entry is only freed
270 * once it's address lists are empty.
271 *
272 */
273static void netlbl_unlhsh_free_iface(struct rcu_head *entry)
274{
275 struct netlbl_unlhsh_iface *iface;
Paul Moore61e10682008-10-10 10:16:32 -0400276 struct netlbl_af4list *iter4;
277 struct netlbl_af4list *tmp4;
278#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
279 struct netlbl_af6list *iter6;
280 struct netlbl_af6list *tmp6;
281#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500282
283 iface = container_of(entry, struct netlbl_unlhsh_iface, rcu);
284
285 /* no need for locks here since we are the only one with access to this
286 * structure */
287
Paul Moore61e10682008-10-10 10:16:32 -0400288 netlbl_af4list_foreach_safe(iter4, tmp4, &iface->addr4_list) {
289 netlbl_af4list_remove_entry(iter4);
290 kfree(netlbl_unlhsh_addr4_entry(iter4));
291 }
292#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
293 netlbl_af6list_foreach_safe(iter6, tmp6, &iface->addr6_list) {
294 netlbl_af6list_remove_entry(iter6);
295 kfree(netlbl_unlhsh_addr6_entry(iter6));
296 }
297#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500298 kfree(iface);
299}
300
301/**
302 * netlbl_unlhsh_hash - Hashing function for the hash table
303 * @ifindex: the network interface/device to hash
304 *
305 * Description:
306 * This is the hashing function for the unlabeled hash table, it returns the
307 * bucket number for the given device/interface. The caller is responsible for
308 * calling the rcu_read_[un]lock() functions.
309 *
310 */
311static u32 netlbl_unlhsh_hash(int ifindex)
312{
313 /* this is taken _almost_ directly from
314 * security/selinux/netif.c:sel_netif_hasfn() as they do pretty much
315 * the same thing */
316 return ifindex & (rcu_dereference(netlbl_unlhsh)->size - 1);
317}
318
319/**
Paul Moore8cc44572008-01-29 08:44:21 -0500320 * netlbl_unlhsh_search_iface - Search for a matching interface entry
321 * @ifindex: the network interface
322 *
323 * Description:
324 * Searches the unlabeled connection hash table and returns a pointer to the
325 * interface entry which matches @ifindex, otherwise NULL is returned. The
326 * caller is responsible for calling the rcu_read_[un]lock() functions.
327 *
328 */
329static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface(int ifindex)
330{
331 u32 bkt;
Paul Moore56196702008-10-10 10:16:29 -0400332 struct list_head *bkt_list;
Paul Moore8cc44572008-01-29 08:44:21 -0500333 struct netlbl_unlhsh_iface *iter;
334
335 bkt = netlbl_unlhsh_hash(ifindex);
Paul Moore56196702008-10-10 10:16:29 -0400336 bkt_list = &rcu_dereference(netlbl_unlhsh)->tbl[bkt];
337 list_for_each_entry_rcu(iter, bkt_list, list)
Paul Moore8cc44572008-01-29 08:44:21 -0500338 if (iter->valid && iter->ifindex == ifindex)
339 return iter;
340
341 return NULL;
342}
343
344/**
345 * netlbl_unlhsh_search_iface_def - Search for a matching interface entry
346 * @ifindex: the network interface
347 *
348 * Description:
349 * Searches the unlabeled connection hash table and returns a pointer to the
350 * interface entry which matches @ifindex. If an exact match can not be found
351 * and there is a valid default entry, the default entry is returned, otherwise
352 * NULL is returned. The caller is responsible for calling the
353 * rcu_read_[un]lock() functions.
354 *
355 */
356static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface_def(int ifindex)
357{
358 struct netlbl_unlhsh_iface *entry;
359
360 entry = netlbl_unlhsh_search_iface(ifindex);
361 if (entry != NULL)
362 return entry;
363
364 entry = rcu_dereference(netlbl_unlhsh_def);
365 if (entry != NULL && entry->valid)
366 return entry;
367
368 return NULL;
369}
370
371/**
372 * netlbl_unlhsh_add_addr4 - Add a new IPv4 address entry to the hash table
373 * @iface: the associated interface entry
374 * @addr: IPv4 address in network byte order
375 * @mask: IPv4 address mask in network byte order
376 * @secid: LSM secid value for entry
377 *
378 * Description:
379 * Add a new address entry into the unlabeled connection hash table using the
380 * interface entry specified by @iface. On success zero is returned, otherwise
381 * a negative value is returned. The caller is responsible for calling the
382 * rcu_read_[un]lock() functions.
383 *
384 */
385static int netlbl_unlhsh_add_addr4(struct netlbl_unlhsh_iface *iface,
386 const struct in_addr *addr,
387 const struct in_addr *mask,
388 u32 secid)
389{
Paul Moore61e10682008-10-10 10:16:32 -0400390 int ret_val;
Paul Moore8cc44572008-01-29 08:44:21 -0500391 struct netlbl_unlhsh_addr4 *entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500392
393 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
394 if (entry == NULL)
395 return -ENOMEM;
396
Paul Moore61e10682008-10-10 10:16:32 -0400397 entry->list.addr = addr->s_addr & mask->s_addr;
398 entry->list.mask = mask->s_addr;
399 entry->list.valid = 1;
Paul Moore8cc44572008-01-29 08:44:21 -0500400 INIT_RCU_HEAD(&entry->rcu);
Paul Moore61e10682008-10-10 10:16:32 -0400401 entry->secid = secid;
Paul Moore8cc44572008-01-29 08:44:21 -0500402
403 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400404 ret_val = netlbl_af4list_add(&entry->list, &iface->addr4_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500405 spin_unlock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400406
407 if (ret_val != 0)
408 kfree(entry);
409 return ret_val;
Paul Moore8cc44572008-01-29 08:44:21 -0500410}
411
412#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
413/**
414 * netlbl_unlhsh_add_addr6 - Add a new IPv6 address entry to the hash table
415 * @iface: the associated interface entry
416 * @addr: IPv6 address in network byte order
417 * @mask: IPv6 address mask in network byte order
418 * @secid: LSM secid value for entry
419 *
420 * Description:
421 * Add a new address entry into the unlabeled connection hash table using the
422 * interface entry specified by @iface. On success zero is returned, otherwise
423 * a negative value is returned. The caller is responsible for calling the
424 * rcu_read_[un]lock() functions.
425 *
426 */
427static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface,
428 const struct in6_addr *addr,
429 const struct in6_addr *mask,
430 u32 secid)
431{
Paul Moore61e10682008-10-10 10:16:32 -0400432 int ret_val;
Paul Moore8cc44572008-01-29 08:44:21 -0500433 struct netlbl_unlhsh_addr6 *entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500434
435 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
436 if (entry == NULL)
437 return -ENOMEM;
438
Paul Moore61e10682008-10-10 10:16:32 -0400439 ipv6_addr_copy(&entry->list.addr, addr);
440 entry->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
441 entry->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
442 entry->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
443 entry->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
444 ipv6_addr_copy(&entry->list.mask, mask);
445 entry->list.valid = 1;
Paul Moore8cc44572008-01-29 08:44:21 -0500446 INIT_RCU_HEAD(&entry->rcu);
Paul Moore61e10682008-10-10 10:16:32 -0400447 entry->secid = secid;
Paul Moore8cc44572008-01-29 08:44:21 -0500448
449 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400450 ret_val = netlbl_af6list_add(&entry->list, &iface->addr6_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500451 spin_unlock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400452
453 if (ret_val != 0)
454 kfree(entry);
Paul Moore8cc44572008-01-29 08:44:21 -0500455 return 0;
456}
457#endif /* IPv6 */
458
459/**
460 * netlbl_unlhsh_add_iface - Adds a new interface entry to the hash table
461 * @ifindex: network interface
462 *
463 * Description:
464 * Add a new, empty, interface entry into the unlabeled connection hash table.
465 * On success a pointer to the new interface entry is returned, on failure NULL
466 * is returned. The caller is responsible for calling the rcu_read_[un]lock()
467 * functions.
468 *
469 */
470static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex)
471{
472 u32 bkt;
473 struct netlbl_unlhsh_iface *iface;
474
475 iface = kzalloc(sizeof(*iface), GFP_ATOMIC);
476 if (iface == NULL)
477 return NULL;
478
479 iface->ifindex = ifindex;
480 INIT_LIST_HEAD(&iface->addr4_list);
481 INIT_LIST_HEAD(&iface->addr6_list);
482 iface->valid = 1;
483 INIT_RCU_HEAD(&iface->rcu);
484
485 spin_lock(&netlbl_unlhsh_lock);
486 if (ifindex > 0) {
487 bkt = netlbl_unlhsh_hash(ifindex);
488 if (netlbl_unlhsh_search_iface(ifindex) != NULL)
489 goto add_iface_failure;
490 list_add_tail_rcu(&iface->list,
491 &rcu_dereference(netlbl_unlhsh)->tbl[bkt]);
492 } else {
493 INIT_LIST_HEAD(&iface->list);
494 if (rcu_dereference(netlbl_unlhsh_def) != NULL)
495 goto add_iface_failure;
496 rcu_assign_pointer(netlbl_unlhsh_def, iface);
497 }
498 spin_unlock(&netlbl_unlhsh_lock);
499
500 return iface;
501
502add_iface_failure:
503 spin_unlock(&netlbl_unlhsh_lock);
504 kfree(iface);
505 return NULL;
506}
507
508/**
509 * netlbl_unlhsh_add - Adds a new entry to the unlabeled connection hash table
510 * @net: network namespace
511 * @dev_name: interface name
512 * @addr: IP address in network byte order
513 * @mask: address mask in network byte order
514 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
515 * @secid: LSM secid value for the entry
Paul Moore13541b32008-01-29 08:44:23 -0500516 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500517 *
518 * Description:
519 * Adds a new entry to the unlabeled connection hash table. Returns zero on
520 * success, negative values on failure.
521 *
522 */
523static int netlbl_unlhsh_add(struct net *net,
524 const char *dev_name,
525 const void *addr,
526 const void *mask,
527 u32 addr_len,
Paul Moore13541b32008-01-29 08:44:23 -0500528 u32 secid,
529 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500530{
531 int ret_val;
532 int ifindex;
533 struct net_device *dev;
534 struct netlbl_unlhsh_iface *iface;
Paul Moore13541b32008-01-29 08:44:23 -0500535 struct audit_buffer *audit_buf = NULL;
536 char *secctx = NULL;
537 u32 secctx_len;
Paul Moore8cc44572008-01-29 08:44:21 -0500538
539 if (addr_len != sizeof(struct in_addr) &&
540 addr_len != sizeof(struct in6_addr))
541 return -EINVAL;
542
543 rcu_read_lock();
544 if (dev_name != NULL) {
545 dev = dev_get_by_name(net, dev_name);
546 if (dev == NULL) {
547 ret_val = -ENODEV;
548 goto unlhsh_add_return;
549 }
550 ifindex = dev->ifindex;
551 dev_put(dev);
552 iface = netlbl_unlhsh_search_iface(ifindex);
553 } else {
554 ifindex = 0;
555 iface = rcu_dereference(netlbl_unlhsh_def);
556 }
557 if (iface == NULL) {
558 iface = netlbl_unlhsh_add_iface(ifindex);
559 if (iface == NULL) {
560 ret_val = -ENOMEM;
561 goto unlhsh_add_return;
562 }
563 }
Paul Moore13541b32008-01-29 08:44:23 -0500564 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCADD,
565 audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500566 switch (addr_len) {
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800567 case sizeof(struct in_addr): {
568 struct in_addr *addr4, *mask4;
569
Paul Moore13541b32008-01-29 08:44:23 -0500570 addr4 = (struct in_addr *)addr;
571 mask4 = (struct in_addr *)mask;
572 ret_val = netlbl_unlhsh_add_addr4(iface, addr4, mask4, secid);
573 if (audit_buf != NULL)
574 netlbl_unlabel_audit_addr4(audit_buf,
575 dev_name,
576 addr4->s_addr,
577 mask4->s_addr);
Paul Moore8cc44572008-01-29 08:44:21 -0500578 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800579 }
Paul Moore8cc44572008-01-29 08:44:21 -0500580#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800581 case sizeof(struct in6_addr): {
582 struct in6_addr *addr6, *mask6;
583
Paul Moore13541b32008-01-29 08:44:23 -0500584 addr6 = (struct in6_addr *)addr;
585 mask6 = (struct in6_addr *)mask;
586 ret_val = netlbl_unlhsh_add_addr6(iface, addr6, mask6, secid);
587 if (audit_buf != NULL)
588 netlbl_unlabel_audit_addr6(audit_buf,
589 dev_name,
590 addr6, mask6);
Paul Moore8cc44572008-01-29 08:44:21 -0500591 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800592 }
Paul Moore8cc44572008-01-29 08:44:21 -0500593#endif /* IPv6 */
594 default:
595 ret_val = -EINVAL;
596 }
597 if (ret_val == 0)
598 atomic_inc(&netlabel_mgmt_protocount);
599
600unlhsh_add_return:
601 rcu_read_unlock();
Paul Moore13541b32008-01-29 08:44:23 -0500602 if (audit_buf != NULL) {
603 if (security_secid_to_secctx(secid,
604 &secctx,
605 &secctx_len) == 0) {
606 audit_log_format(audit_buf, " sec_obj=%s", secctx);
607 security_release_secctx(secctx, secctx_len);
608 }
609 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
610 audit_log_end(audit_buf);
611 }
Paul Moore8cc44572008-01-29 08:44:21 -0500612 return ret_val;
613}
614
615/**
616 * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry
Paul Moore13541b32008-01-29 08:44:23 -0500617 * @net: network namespace
Paul Moore8cc44572008-01-29 08:44:21 -0500618 * @iface: interface entry
619 * @addr: IP address
620 * @mask: IP address mask
Paul Moore13541b32008-01-29 08:44:23 -0500621 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500622 *
623 * Description:
624 * Remove an IP address entry from the unlabeled connection hash table.
625 * Returns zero on success, negative values on failure. The caller is
626 * responsible for calling the rcu_read_[un]lock() functions.
627 *
628 */
Paul Moore13541b32008-01-29 08:44:23 -0500629static int netlbl_unlhsh_remove_addr4(struct net *net,
630 struct netlbl_unlhsh_iface *iface,
Paul Moore8cc44572008-01-29 08:44:21 -0500631 const struct in_addr *addr,
Paul Moore13541b32008-01-29 08:44:23 -0500632 const struct in_addr *mask,
633 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500634{
Paul Moore61e10682008-10-10 10:16:32 -0400635 int ret_val = 0;
636 struct netlbl_af4list *list_entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500637 struct netlbl_unlhsh_addr4 *entry;
Paul Moore61e10682008-10-10 10:16:32 -0400638 struct audit_buffer *audit_buf;
Paul Moore13541b32008-01-29 08:44:23 -0500639 struct net_device *dev;
Paul Moore61e10682008-10-10 10:16:32 -0400640 char *secctx;
Paul Moore13541b32008-01-29 08:44:23 -0500641 u32 secctx_len;
Paul Moore8cc44572008-01-29 08:44:21 -0500642
643 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400644 list_entry = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
645 &iface->addr4_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500646 spin_unlock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400647 if (list_entry == NULL)
648 ret_val = -ENOENT;
649 entry = netlbl_unlhsh_addr4_entry(list_entry);
Paul Moore8cc44572008-01-29 08:44:21 -0500650
Paul Moore13541b32008-01-29 08:44:23 -0500651 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
652 audit_info);
653 if (audit_buf != NULL) {
654 dev = dev_get_by_index(net, iface->ifindex);
655 netlbl_unlabel_audit_addr4(audit_buf,
656 (dev != NULL ? dev->name : NULL),
Paul Moore61e10682008-10-10 10:16:32 -0400657 addr->s_addr, mask->s_addr);
Paul Moore13541b32008-01-29 08:44:23 -0500658 if (dev != NULL)
659 dev_put(dev);
Paul Moore61e10682008-10-10 10:16:32 -0400660 if (entry && security_secid_to_secctx(entry->secid,
661 &secctx,
662 &secctx_len) == 0) {
Paul Moore13541b32008-01-29 08:44:23 -0500663 audit_log_format(audit_buf, " sec_obj=%s", secctx);
664 security_release_secctx(secctx, secctx_len);
665 }
666 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
667 audit_log_end(audit_buf);
668 }
669
Paul Moore8cc44572008-01-29 08:44:21 -0500670 if (ret_val == 0)
671 call_rcu(&entry->rcu, netlbl_unlhsh_free_addr4);
672 return ret_val;
673}
674
675#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
676/**
677 * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry
Paul Moore13541b32008-01-29 08:44:23 -0500678 * @net: network namespace
Paul Moore8cc44572008-01-29 08:44:21 -0500679 * @iface: interface entry
680 * @addr: IP address
681 * @mask: IP address mask
Paul Moore13541b32008-01-29 08:44:23 -0500682 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500683 *
684 * Description:
685 * Remove an IP address entry from the unlabeled connection hash table.
686 * Returns zero on success, negative values on failure. The caller is
687 * responsible for calling the rcu_read_[un]lock() functions.
688 *
689 */
Paul Moore13541b32008-01-29 08:44:23 -0500690static int netlbl_unlhsh_remove_addr6(struct net *net,
691 struct netlbl_unlhsh_iface *iface,
Paul Moore8cc44572008-01-29 08:44:21 -0500692 const struct in6_addr *addr,
Paul Moore13541b32008-01-29 08:44:23 -0500693 const struct in6_addr *mask,
694 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500695{
Paul Moore61e10682008-10-10 10:16:32 -0400696 int ret_val = 0;
697 struct netlbl_af6list *list_entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500698 struct netlbl_unlhsh_addr6 *entry;
Paul Moore61e10682008-10-10 10:16:32 -0400699 struct audit_buffer *audit_buf;
Paul Moore13541b32008-01-29 08:44:23 -0500700 struct net_device *dev;
Paul Moore61e10682008-10-10 10:16:32 -0400701 char *secctx;
Paul Moore13541b32008-01-29 08:44:23 -0500702 u32 secctx_len;
Paul Moore8cc44572008-01-29 08:44:21 -0500703
704 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400705 list_entry = netlbl_af6list_remove(addr, mask, &iface->addr6_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500706 spin_unlock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400707 if (list_entry == NULL)
708 ret_val = -ENOENT;
709 entry = netlbl_unlhsh_addr6_entry(list_entry);
Paul Moore8cc44572008-01-29 08:44:21 -0500710
Paul Moore13541b32008-01-29 08:44:23 -0500711 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
712 audit_info);
713 if (audit_buf != NULL) {
714 dev = dev_get_by_index(net, iface->ifindex);
715 netlbl_unlabel_audit_addr6(audit_buf,
716 (dev != NULL ? dev->name : NULL),
717 addr, mask);
718 if (dev != NULL)
719 dev_put(dev);
Paul Moore61e10682008-10-10 10:16:32 -0400720 if (entry && security_secid_to_secctx(entry->secid,
721 &secctx,
722 &secctx_len) == 0) {
Paul Moore13541b32008-01-29 08:44:23 -0500723 audit_log_format(audit_buf, " sec_obj=%s", secctx);
724 security_release_secctx(secctx, secctx_len);
725 }
726 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
727 audit_log_end(audit_buf);
728 }
729
Paul Moore8cc44572008-01-29 08:44:21 -0500730 if (ret_val == 0)
731 call_rcu(&entry->rcu, netlbl_unlhsh_free_addr6);
732 return ret_val;
733}
734#endif /* IPv6 */
735
736/**
737 * netlbl_unlhsh_condremove_iface - Remove an interface entry
738 * @iface: the interface entry
739 *
740 * Description:
741 * Remove an interface entry from the unlabeled connection hash table if it is
742 * empty. An interface entry is considered to be empty if there are no
743 * address entries assigned to it.
744 *
745 */
746static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface)
747{
Paul Moore61e10682008-10-10 10:16:32 -0400748 struct netlbl_af4list *iter4;
749#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
750 struct netlbl_af6list *iter6;
751#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500752
753 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400754 netlbl_af4list_foreach_rcu(iter4, &iface->addr4_list)
755 goto unlhsh_condremove_failure;
756#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
757 netlbl_af6list_foreach_rcu(iter6, &iface->addr6_list)
758 goto unlhsh_condremove_failure;
759#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500760 iface->valid = 0;
761 if (iface->ifindex > 0)
762 list_del_rcu(&iface->list);
763 else
764 rcu_assign_pointer(netlbl_unlhsh_def, NULL);
765 spin_unlock(&netlbl_unlhsh_lock);
766
767 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
768 return;
769
770unlhsh_condremove_failure:
771 spin_unlock(&netlbl_unlhsh_lock);
772 return;
773}
774
775/**
776 * netlbl_unlhsh_remove - Remove an entry from the unlabeled hash table
777 * @net: network namespace
778 * @dev_name: interface name
779 * @addr: IP address in network byte order
780 * @mask: address mask in network byte order
781 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
Paul Moore13541b32008-01-29 08:44:23 -0500782 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500783 *
784 * Description:
785 * Removes and existing entry from the unlabeled connection hash table.
786 * Returns zero on success, negative values on failure.
787 *
788 */
789static int netlbl_unlhsh_remove(struct net *net,
790 const char *dev_name,
791 const void *addr,
792 const void *mask,
Paul Moore13541b32008-01-29 08:44:23 -0500793 u32 addr_len,
794 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500795{
796 int ret_val;
797 struct net_device *dev;
798 struct netlbl_unlhsh_iface *iface;
799
800 if (addr_len != sizeof(struct in_addr) &&
801 addr_len != sizeof(struct in6_addr))
802 return -EINVAL;
803
804 rcu_read_lock();
805 if (dev_name != NULL) {
806 dev = dev_get_by_name(net, dev_name);
807 if (dev == NULL) {
808 ret_val = -ENODEV;
809 goto unlhsh_remove_return;
810 }
811 iface = netlbl_unlhsh_search_iface(dev->ifindex);
812 dev_put(dev);
813 } else
814 iface = rcu_dereference(netlbl_unlhsh_def);
815 if (iface == NULL) {
816 ret_val = -ENOENT;
817 goto unlhsh_remove_return;
818 }
819 switch (addr_len) {
820 case sizeof(struct in_addr):
Paul Moore13541b32008-01-29 08:44:23 -0500821 ret_val = netlbl_unlhsh_remove_addr4(net,
822 iface, addr, mask,
823 audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500824 break;
825#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
826 case sizeof(struct in6_addr):
Paul Moore13541b32008-01-29 08:44:23 -0500827 ret_val = netlbl_unlhsh_remove_addr6(net,
828 iface, addr, mask,
829 audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500830 break;
831#endif /* IPv6 */
832 default:
833 ret_val = -EINVAL;
834 }
835 if (ret_val == 0) {
836 netlbl_unlhsh_condremove_iface(iface);
837 atomic_dec(&netlabel_mgmt_protocount);
838 }
839
840unlhsh_remove_return:
841 rcu_read_unlock();
842 return ret_val;
843}
844
845/*
846 * General Helper Functions
847 */
848
849/**
850 * netlbl_unlhsh_netdev_handler - Network device notification handler
851 * @this: notifier block
852 * @event: the event
853 * @ptr: the network device (cast to void)
854 *
855 * Description:
856 * Handle network device events, although at present all we care about is a
857 * network device going away. In the case of a device going away we clear any
858 * related entries from the unlabeled connection hash table.
859 *
860 */
861static int netlbl_unlhsh_netdev_handler(struct notifier_block *this,
862 unsigned long event,
863 void *ptr)
864{
865 struct net_device *dev = ptr;
866 struct netlbl_unlhsh_iface *iface = NULL;
867
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700868 if (!net_eq(dev_net(dev), &init_net))
Paul Moore8cc44572008-01-29 08:44:21 -0500869 return NOTIFY_DONE;
870
871 /* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */
872 if (event == NETDEV_DOWN) {
873 spin_lock(&netlbl_unlhsh_lock);
874 iface = netlbl_unlhsh_search_iface(dev->ifindex);
875 if (iface != NULL && iface->valid) {
876 iface->valid = 0;
877 list_del_rcu(&iface->list);
878 } else
879 iface = NULL;
880 spin_unlock(&netlbl_unlhsh_lock);
881 }
882
883 if (iface != NULL)
884 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
885
886 return NOTIFY_DONE;
887}
888
889/**
Paul Moore32f50cd2006-09-28 14:51:47 -0700890 * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
891 * @value: desired value
Paul Moore95d4e6b2006-09-29 17:05:05 -0700892 * @audit_info: NetLabel audit information
Paul Moore32f50cd2006-09-28 14:51:47 -0700893 *
894 * Description:
895 * Set the value of the unlabeled accept flag to @value.
896 *
897 */
Paul Moore95d4e6b2006-09-29 17:05:05 -0700898static void netlbl_unlabel_acceptflg_set(u8 value,
899 struct netlbl_audit *audit_info)
Paul Moore32f50cd2006-09-28 14:51:47 -0700900{
Paul Moore95d4e6b2006-09-29 17:05:05 -0700901 struct audit_buffer *audit_buf;
902 u8 old_val;
903
Paul Moore4be27002007-10-26 04:29:08 -0700904 old_val = netlabel_unlabel_acceptflg;
Paul Moorecd287862006-11-17 17:38:44 -0500905 netlabel_unlabel_acceptflg = value;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700906 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
907 audit_info);
Paul Moorede646882006-11-17 17:38:55 -0500908 if (audit_buf != NULL) {
909 audit_log_format(audit_buf,
910 " unlbl_accept=%u old=%u", value, old_val);
911 audit_log_end(audit_buf);
912 }
Paul Moore32f50cd2006-09-28 14:51:47 -0700913}
914
Paul Moore8cc44572008-01-29 08:44:21 -0500915/**
916 * netlbl_unlabel_addrinfo_get - Get the IPv4/6 address information
917 * @info: the Generic NETLINK info block
918 * @addr: the IP address
919 * @mask: the IP address mask
920 * @len: the address length
921 *
922 * Description:
923 * Examine the Generic NETLINK message and extract the IP address information.
924 * Returns zero on success, negative values on failure.
925 *
926 */
927static int netlbl_unlabel_addrinfo_get(struct genl_info *info,
928 void **addr,
929 void **mask,
930 u32 *len)
931{
932 u32 addr_len;
933
934 if (info->attrs[NLBL_UNLABEL_A_IPV4ADDR]) {
935 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
936 if (addr_len != sizeof(struct in_addr) &&
937 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK]))
938 return -EINVAL;
939 *len = addr_len;
940 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
941 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]);
942 return 0;
943 } else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) {
944 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
945 if (addr_len != sizeof(struct in6_addr) &&
946 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK]))
947 return -EINVAL;
948 *len = addr_len;
949 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
950 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]);
951 return 0;
952 }
953
954 return -EINVAL;
955}
956
Paul Moore32f50cd2006-09-28 14:51:47 -0700957/*
Paul Moore96cb8e32006-08-03 16:48:59 -0700958 * NetLabel Command Handlers
959 */
960
961/**
962 * netlbl_unlabel_accept - Handle an ACCEPT message
963 * @skb: the NETLINK buffer
964 * @info: the Generic NETLINK info block
965 *
966 * Description:
967 * Process a user generated ACCEPT message and set the accept flag accordingly.
968 * Returns zero on success, negative values on failure.
969 *
970 */
971static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
972{
Paul Moorefd385852006-09-25 15:56:37 -0700973 u8 value;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700974 struct netlbl_audit audit_info;
Paul Moore96cb8e32006-08-03 16:48:59 -0700975
Paul Moorefd385852006-09-25 15:56:37 -0700976 if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
977 value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
Paul Moore96cb8e32006-08-03 16:48:59 -0700978 if (value == 1 || value == 0) {
Paul Moore95d4e6b2006-09-29 17:05:05 -0700979 netlbl_netlink_auditinfo(skb, &audit_info);
980 netlbl_unlabel_acceptflg_set(value, &audit_info);
Paul Moore32f50cd2006-09-28 14:51:47 -0700981 return 0;
Paul Moore96cb8e32006-08-03 16:48:59 -0700982 }
983 }
984
Paul Moore32f50cd2006-09-28 14:51:47 -0700985 return -EINVAL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700986}
987
988/**
989 * netlbl_unlabel_list - Handle a LIST message
990 * @skb: the NETLINK buffer
991 * @info: the Generic NETLINK info block
992 *
993 * Description:
994 * Process a user generated LIST message and respond with the current status.
995 * Returns zero on success, negative values on failure.
996 *
997 */
998static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
999{
Paul Moorefd385852006-09-25 15:56:37 -07001000 int ret_val = -EINVAL;
Paul Moore96cb8e32006-08-03 16:48:59 -07001001 struct sk_buff *ans_skb;
Paul Moorefd385852006-09-25 15:56:37 -07001002 void *data;
Paul Moore96cb8e32006-08-03 16:48:59 -07001003
Thomas Graf339bf982006-11-10 14:10:15 -08001004 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Paul Moore96cb8e32006-08-03 16:48:59 -07001005 if (ans_skb == NULL)
1006 goto list_failure;
Thomas Graf17c157c2006-11-14 19:46:02 -08001007 data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
1008 0, NLBL_UNLABEL_C_LIST);
Paul Moorefd385852006-09-25 15:56:37 -07001009 if (data == NULL) {
1010 ret_val = -ENOMEM;
Paul Moore96cb8e32006-08-03 16:48:59 -07001011 goto list_failure;
Paul Moorefd385852006-09-25 15:56:37 -07001012 }
Paul Moore96cb8e32006-08-03 16:48:59 -07001013
Paul Moorefd385852006-09-25 15:56:37 -07001014 ret_val = nla_put_u8(ans_skb,
1015 NLBL_UNLABEL_A_ACPTFLG,
Paul Moorecd287862006-11-17 17:38:44 -05001016 netlabel_unlabel_acceptflg);
Paul Moore96cb8e32006-08-03 16:48:59 -07001017 if (ret_val != 0)
1018 goto list_failure;
1019
Paul Moorefd385852006-09-25 15:56:37 -07001020 genlmsg_end(ans_skb, data);
Denis V. Lunevfe785be2008-07-10 16:53:39 -07001021 return genlmsg_reply(ans_skb, info);
Paul Moore96cb8e32006-08-03 16:48:59 -07001022
1023list_failure:
Patrick McHardyb08d5842007-02-27 09:57:37 -08001024 kfree_skb(ans_skb);
Paul Moore96cb8e32006-08-03 16:48:59 -07001025 return ret_val;
1026}
1027
Paul Moore8cc44572008-01-29 08:44:21 -05001028/**
1029 * netlbl_unlabel_staticadd - Handle a STATICADD message
1030 * @skb: the NETLINK buffer
1031 * @info: the Generic NETLINK info block
1032 *
1033 * Description:
1034 * Process a user generated STATICADD message and add a new unlabeled
1035 * connection entry to the hash table. Returns zero on success, negative
1036 * values on failure.
1037 *
1038 */
1039static int netlbl_unlabel_staticadd(struct sk_buff *skb,
1040 struct genl_info *info)
1041{
1042 int ret_val;
1043 char *dev_name;
1044 void *addr;
1045 void *mask;
1046 u32 addr_len;
1047 u32 secid;
Paul Moore13541b32008-01-29 08:44:23 -05001048 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -05001049
1050 /* Don't allow users to add both IPv4 and IPv6 addresses for a
1051 * single entry. However, allow users to create two entries, one each
1052 * for IPv4 and IPv4, with the same LSM security context which should
1053 * achieve the same result. */
1054 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
1055 !info->attrs[NLBL_UNLABEL_A_IFACE] ||
1056 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1057 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1058 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1059 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1060 return -EINVAL;
1061
Paul Moore13541b32008-01-29 08:44:23 -05001062 netlbl_netlink_auditinfo(skb, &audit_info);
1063
Paul Moore8cc44572008-01-29 08:44:21 -05001064 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1065 if (ret_val != 0)
1066 return ret_val;
1067 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
1068 ret_val = security_secctx_to_secid(
1069 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1070 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1071 &secid);
1072 if (ret_val != 0)
1073 return ret_val;
1074
1075 return netlbl_unlhsh_add(&init_net,
Paul Moore13541b32008-01-29 08:44:23 -05001076 dev_name, addr, mask, addr_len, secid,
1077 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -05001078}
1079
1080/**
1081 * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message
1082 * @skb: the NETLINK buffer
1083 * @info: the Generic NETLINK info block
1084 *
1085 * Description:
1086 * Process a user generated STATICADDDEF message and add a new default
1087 * unlabeled connection entry. Returns zero on success, negative values on
1088 * failure.
1089 *
1090 */
1091static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
1092 struct genl_info *info)
1093{
1094 int ret_val;
1095 void *addr;
1096 void *mask;
1097 u32 addr_len;
1098 u32 secid;
Paul Moore13541b32008-01-29 08:44:23 -05001099 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -05001100
1101 /* Don't allow users to add both IPv4 and IPv6 addresses for a
1102 * single entry. However, allow users to create two entries, one each
1103 * for IPv4 and IPv6, with the same LSM security context which should
1104 * achieve the same result. */
1105 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
1106 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1107 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1108 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1109 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1110 return -EINVAL;
1111
Paul Moore13541b32008-01-29 08:44:23 -05001112 netlbl_netlink_auditinfo(skb, &audit_info);
1113
Paul Moore8cc44572008-01-29 08:44:21 -05001114 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1115 if (ret_val != 0)
1116 return ret_val;
1117 ret_val = security_secctx_to_secid(
1118 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1119 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1120 &secid);
1121 if (ret_val != 0)
1122 return ret_val;
1123
Paul Moore13541b32008-01-29 08:44:23 -05001124 return netlbl_unlhsh_add(&init_net,
1125 NULL, addr, mask, addr_len, secid,
1126 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -05001127}
1128
1129/**
1130 * netlbl_unlabel_staticremove - Handle a STATICREMOVE message
1131 * @skb: the NETLINK buffer
1132 * @info: the Generic NETLINK info block
1133 *
1134 * Description:
1135 * Process a user generated STATICREMOVE message and remove the specified
1136 * unlabeled connection entry. Returns zero on success, negative values on
1137 * failure.
1138 *
1139 */
1140static int netlbl_unlabel_staticremove(struct sk_buff *skb,
1141 struct genl_info *info)
1142{
1143 int ret_val;
1144 char *dev_name;
1145 void *addr;
1146 void *mask;
1147 u32 addr_len;
Paul Moore13541b32008-01-29 08:44:23 -05001148 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -05001149
1150 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1151 * IPv4 and IPv6 in the same entry. */
1152 if (!info->attrs[NLBL_UNLABEL_A_IFACE] ||
1153 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1154 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1155 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1156 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1157 return -EINVAL;
1158
Paul Moore13541b32008-01-29 08:44:23 -05001159 netlbl_netlink_auditinfo(skb, &audit_info);
1160
Paul Moore8cc44572008-01-29 08:44:21 -05001161 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1162 if (ret_val != 0)
1163 return ret_val;
1164 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
1165
Paul Moore13541b32008-01-29 08:44:23 -05001166 return netlbl_unlhsh_remove(&init_net,
1167 dev_name, addr, mask, addr_len,
1168 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -05001169}
1170
1171/**
1172 * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message
1173 * @skb: the NETLINK buffer
1174 * @info: the Generic NETLINK info block
1175 *
1176 * Description:
1177 * Process a user generated STATICREMOVEDEF message and remove the default
1178 * unlabeled connection entry. Returns zero on success, negative values on
1179 * failure.
1180 *
1181 */
1182static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
1183 struct genl_info *info)
1184{
1185 int ret_val;
1186 void *addr;
1187 void *mask;
1188 u32 addr_len;
Paul Moore13541b32008-01-29 08:44:23 -05001189 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -05001190
1191 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1192 * IPv4 and IPv6 in the same entry. */
1193 if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1194 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1195 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1196 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1197 return -EINVAL;
1198
Paul Moore13541b32008-01-29 08:44:23 -05001199 netlbl_netlink_auditinfo(skb, &audit_info);
1200
Paul Moore8cc44572008-01-29 08:44:21 -05001201 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1202 if (ret_val != 0)
1203 return ret_val;
1204
Paul Moore13541b32008-01-29 08:44:23 -05001205 return netlbl_unlhsh_remove(&init_net,
1206 NULL, addr, mask, addr_len,
1207 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -05001208}
1209
1210
1211/**
1212 * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF]
1213 * @cmd: command/message
1214 * @iface: the interface entry
1215 * @addr4: the IPv4 address entry
1216 * @addr6: the IPv6 address entry
1217 * @arg: the netlbl_unlhsh_walk_arg structure
1218 *
1219 * Description:
1220 * This function is designed to be used to generate a response for a
1221 * STATICLIST or STATICLISTDEF message. When called either @addr4 or @addr6
1222 * can be specified, not both, the other unspecified entry should be set to
1223 * NULL by the caller. Returns the size of the message on success, negative
1224 * values on failure.
1225 *
1226 */
1227static int netlbl_unlabel_staticlist_gen(u32 cmd,
1228 const struct netlbl_unlhsh_iface *iface,
1229 const struct netlbl_unlhsh_addr4 *addr4,
1230 const struct netlbl_unlhsh_addr6 *addr6,
1231 void *arg)
1232{
1233 int ret_val = -ENOMEM;
1234 struct netlbl_unlhsh_walk_arg *cb_arg = arg;
1235 struct net_device *dev;
1236 void *data;
1237 u32 secid;
1238 char *secctx;
1239 u32 secctx_len;
1240
1241 data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
1242 cb_arg->seq, &netlbl_unlabel_gnl_family,
1243 NLM_F_MULTI, cmd);
1244 if (data == NULL)
1245 goto list_cb_failure;
1246
1247 if (iface->ifindex > 0) {
1248 dev = dev_get_by_index(&init_net, iface->ifindex);
Jesper Juhl794eb6b2008-04-17 23:22:54 -07001249 if (!dev) {
1250 ret_val = -ENODEV;
1251 goto list_cb_failure;
1252 }
Paul Moore8cc44572008-01-29 08:44:21 -05001253 ret_val = nla_put_string(cb_arg->skb,
1254 NLBL_UNLABEL_A_IFACE, dev->name);
1255 dev_put(dev);
1256 if (ret_val != 0)
1257 goto list_cb_failure;
1258 }
1259
1260 if (addr4) {
1261 struct in_addr addr_struct;
1262
Paul Moore61e10682008-10-10 10:16:32 -04001263 addr_struct.s_addr = addr4->list.addr;
Paul Moore8cc44572008-01-29 08:44:21 -05001264 ret_val = nla_put(cb_arg->skb,
1265 NLBL_UNLABEL_A_IPV4ADDR,
1266 sizeof(struct in_addr),
1267 &addr_struct);
1268 if (ret_val != 0)
1269 goto list_cb_failure;
1270
Paul Moore61e10682008-10-10 10:16:32 -04001271 addr_struct.s_addr = addr4->list.mask;
Paul Moore8cc44572008-01-29 08:44:21 -05001272 ret_val = nla_put(cb_arg->skb,
1273 NLBL_UNLABEL_A_IPV4MASK,
1274 sizeof(struct in_addr),
1275 &addr_struct);
1276 if (ret_val != 0)
1277 goto list_cb_failure;
1278
1279 secid = addr4->secid;
1280 } else {
1281 ret_val = nla_put(cb_arg->skb,
1282 NLBL_UNLABEL_A_IPV6ADDR,
1283 sizeof(struct in6_addr),
Paul Moore61e10682008-10-10 10:16:32 -04001284 &addr6->list.addr);
Paul Moore8cc44572008-01-29 08:44:21 -05001285 if (ret_val != 0)
1286 goto list_cb_failure;
1287
1288 ret_val = nla_put(cb_arg->skb,
1289 NLBL_UNLABEL_A_IPV6MASK,
1290 sizeof(struct in6_addr),
Paul Moore61e10682008-10-10 10:16:32 -04001291 &addr6->list.mask);
Paul Moore8cc44572008-01-29 08:44:21 -05001292 if (ret_val != 0)
1293 goto list_cb_failure;
1294
1295 secid = addr6->secid;
1296 }
1297
1298 ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len);
1299 if (ret_val != 0)
1300 goto list_cb_failure;
1301 ret_val = nla_put(cb_arg->skb,
1302 NLBL_UNLABEL_A_SECCTX,
1303 secctx_len,
1304 secctx);
1305 security_release_secctx(secctx, secctx_len);
1306 if (ret_val != 0)
1307 goto list_cb_failure;
1308
1309 cb_arg->seq++;
1310 return genlmsg_end(cb_arg->skb, data);
1311
1312list_cb_failure:
1313 genlmsg_cancel(cb_arg->skb, data);
1314 return ret_val;
1315}
1316
1317/**
1318 * netlbl_unlabel_staticlist - Handle a STATICLIST message
1319 * @skb: the NETLINK buffer
1320 * @cb: the NETLINK callback
1321 *
1322 * Description:
1323 * Process a user generated STATICLIST message and dump the unlabeled
1324 * connection hash table in a form suitable for use in a kernel generated
1325 * STATICLIST message. Returns the length of @skb.
1326 *
1327 */
1328static int netlbl_unlabel_staticlist(struct sk_buff *skb,
1329 struct netlink_callback *cb)
1330{
1331 struct netlbl_unlhsh_walk_arg cb_arg;
1332 u32 skip_bkt = cb->args[0];
1333 u32 skip_chain = cb->args[1];
1334 u32 skip_addr4 = cb->args[2];
1335 u32 skip_addr6 = cb->args[3];
1336 u32 iter_bkt;
1337 u32 iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
1338 struct netlbl_unlhsh_iface *iface;
Paul Moore56196702008-10-10 10:16:29 -04001339 struct list_head *iter_list;
Paul Moore61e10682008-10-10 10:16:32 -04001340 struct netlbl_af4list *addr4;
1341#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1342 struct netlbl_af6list *addr6;
1343#endif
Paul Moore8cc44572008-01-29 08:44:21 -05001344
1345 cb_arg.nl_cb = cb;
1346 cb_arg.skb = skb;
1347 cb_arg.seq = cb->nlh->nlmsg_seq;
1348
1349 rcu_read_lock();
1350 for (iter_bkt = skip_bkt;
1351 iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
1352 iter_bkt++, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0) {
Paul Moore56196702008-10-10 10:16:29 -04001353 iter_list = &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt];
1354 list_for_each_entry_rcu(iface, iter_list, list) {
Paul Moore8cc44572008-01-29 08:44:21 -05001355 if (!iface->valid ||
1356 iter_chain++ < skip_chain)
1357 continue;
Paul Moore61e10682008-10-10 10:16:32 -04001358 netlbl_af4list_foreach_rcu(addr4,
1359 &iface->addr4_list) {
1360 if (iter_addr4++ < skip_addr4)
Paul Moore8cc44572008-01-29 08:44:21 -05001361 continue;
1362 if (netlbl_unlabel_staticlist_gen(
Paul Moore61e10682008-10-10 10:16:32 -04001363 NLBL_UNLABEL_C_STATICLIST,
1364 iface,
1365 netlbl_unlhsh_addr4_entry(addr4),
1366 NULL,
1367 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001368 iter_addr4--;
1369 iter_chain--;
1370 goto unlabel_staticlist_return;
1371 }
1372 }
Paul Moore61e10682008-10-10 10:16:32 -04001373#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1374 netlbl_af6list_foreach_rcu(addr6,
1375 &iface->addr6_list) {
1376 if (iter_addr6++ < skip_addr6)
Paul Moore8cc44572008-01-29 08:44:21 -05001377 continue;
1378 if (netlbl_unlabel_staticlist_gen(
Paul Moore61e10682008-10-10 10:16:32 -04001379 NLBL_UNLABEL_C_STATICLIST,
1380 iface,
1381 NULL,
1382 netlbl_unlhsh_addr6_entry(addr6),
1383 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001384 iter_addr6--;
1385 iter_chain--;
1386 goto unlabel_staticlist_return;
1387 }
1388 }
Paul Moore61e10682008-10-10 10:16:32 -04001389#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -05001390 }
1391 }
1392
1393unlabel_staticlist_return:
1394 rcu_read_unlock();
1395 cb->args[0] = skip_bkt;
1396 cb->args[1] = skip_chain;
1397 cb->args[2] = skip_addr4;
1398 cb->args[3] = skip_addr6;
1399 return skb->len;
1400}
1401
1402/**
1403 * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message
1404 * @skb: the NETLINK buffer
1405 * @cb: the NETLINK callback
1406 *
1407 * Description:
1408 * Process a user generated STATICLISTDEF message and dump the default
1409 * unlabeled connection entry in a form suitable for use in a kernel generated
1410 * STATICLISTDEF message. Returns the length of @skb.
1411 *
1412 */
1413static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
1414 struct netlink_callback *cb)
1415{
1416 struct netlbl_unlhsh_walk_arg cb_arg;
1417 struct netlbl_unlhsh_iface *iface;
1418 u32 skip_addr4 = cb->args[0];
1419 u32 skip_addr6 = cb->args[1];
Paul Moore61e10682008-10-10 10:16:32 -04001420 u32 iter_addr4 = 0;
1421 struct netlbl_af4list *addr4;
1422#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1423 u32 iter_addr6 = 0;
1424 struct netlbl_af6list *addr6;
1425#endif
Paul Moore8cc44572008-01-29 08:44:21 -05001426
1427 cb_arg.nl_cb = cb;
1428 cb_arg.skb = skb;
1429 cb_arg.seq = cb->nlh->nlmsg_seq;
1430
1431 rcu_read_lock();
1432 iface = rcu_dereference(netlbl_unlhsh_def);
1433 if (iface == NULL || !iface->valid)
1434 goto unlabel_staticlistdef_return;
1435
Paul Moore61e10682008-10-10 10:16:32 -04001436 netlbl_af4list_foreach_rcu(addr4, &iface->addr4_list) {
1437 if (iter_addr4++ < skip_addr4)
Paul Moore8cc44572008-01-29 08:44:21 -05001438 continue;
1439 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
Paul Moore61e10682008-10-10 10:16:32 -04001440 iface,
1441 netlbl_unlhsh_addr4_entry(addr4),
1442 NULL,
1443 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001444 iter_addr4--;
1445 goto unlabel_staticlistdef_return;
1446 }
1447 }
Paul Moore61e10682008-10-10 10:16:32 -04001448#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1449 netlbl_af6list_foreach_rcu(addr6, &iface->addr6_list) {
1450 if (iter_addr6++ < skip_addr6)
Paul Moore8cc44572008-01-29 08:44:21 -05001451 continue;
1452 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
Paul Moore61e10682008-10-10 10:16:32 -04001453 iface,
1454 NULL,
1455 netlbl_unlhsh_addr6_entry(addr6),
1456 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001457 iter_addr6--;
1458 goto unlabel_staticlistdef_return;
1459 }
1460 }
Paul Moore61e10682008-10-10 10:16:32 -04001461#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -05001462
1463unlabel_staticlistdef_return:
1464 rcu_read_unlock();
1465 cb->args[0] = skip_addr4;
1466 cb->args[1] = skip_addr6;
1467 return skb->len;
1468}
Paul Moore96cb8e32006-08-03 16:48:59 -07001469
1470/*
1471 * NetLabel Generic NETLINK Command Definitions
1472 */
1473
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001474static struct genl_ops netlbl_unlabel_genl_ops[] = {
1475 {
Paul Moore8cc44572008-01-29 08:44:21 -05001476 .cmd = NLBL_UNLABEL_C_STATICADD,
1477 .flags = GENL_ADMIN_PERM,
1478 .policy = netlbl_unlabel_genl_policy,
1479 .doit = netlbl_unlabel_staticadd,
1480 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001481 },
1482 {
Paul Moore8cc44572008-01-29 08:44:21 -05001483 .cmd = NLBL_UNLABEL_C_STATICREMOVE,
1484 .flags = GENL_ADMIN_PERM,
1485 .policy = netlbl_unlabel_genl_policy,
1486 .doit = netlbl_unlabel_staticremove,
1487 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001488 },
1489 {
Paul Moore8cc44572008-01-29 08:44:21 -05001490 .cmd = NLBL_UNLABEL_C_STATICLIST,
1491 .flags = 0,
1492 .policy = netlbl_unlabel_genl_policy,
1493 .doit = NULL,
1494 .dumpit = netlbl_unlabel_staticlist,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001495 },
1496 {
Paul Moore8cc44572008-01-29 08:44:21 -05001497 .cmd = NLBL_UNLABEL_C_STATICADDDEF,
1498 .flags = GENL_ADMIN_PERM,
1499 .policy = netlbl_unlabel_genl_policy,
1500 .doit = netlbl_unlabel_staticadddef,
1501 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001502 },
1503 {
Paul Moore8cc44572008-01-29 08:44:21 -05001504 .cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
1505 .flags = GENL_ADMIN_PERM,
1506 .policy = netlbl_unlabel_genl_policy,
1507 .doit = netlbl_unlabel_staticremovedef,
1508 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001509 },
1510 {
Paul Moore8cc44572008-01-29 08:44:21 -05001511 .cmd = NLBL_UNLABEL_C_STATICLISTDEF,
1512 .flags = 0,
1513 .policy = netlbl_unlabel_genl_policy,
1514 .doit = NULL,
1515 .dumpit = netlbl_unlabel_staticlistdef,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001516 },
1517 {
Paul Moore96cb8e32006-08-03 16:48:59 -07001518 .cmd = NLBL_UNLABEL_C_ACCEPT,
Paul Moorefd385852006-09-25 15:56:37 -07001519 .flags = GENL_ADMIN_PERM,
1520 .policy = netlbl_unlabel_genl_policy,
Paul Moore96cb8e32006-08-03 16:48:59 -07001521 .doit = netlbl_unlabel_accept,
1522 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001523 },
1524 {
Paul Moore96cb8e32006-08-03 16:48:59 -07001525 .cmd = NLBL_UNLABEL_C_LIST,
1526 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -07001527 .policy = netlbl_unlabel_genl_policy,
Paul Moore96cb8e32006-08-03 16:48:59 -07001528 .doit = netlbl_unlabel_list,
1529 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001530 },
Paul Moore96cb8e32006-08-03 16:48:59 -07001531};
1532
Paul Moore96cb8e32006-08-03 16:48:59 -07001533/*
1534 * NetLabel Generic NETLINK Protocol Functions
1535 */
1536
1537/**
1538 * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
1539 *
1540 * Description:
1541 * Register the unlabeled packet NetLabel component with the Generic NETLINK
1542 * mechanism. Returns zero on success, negative values on failure.
1543 *
1544 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -08001545int __init netlbl_unlabel_genl_init(void)
Paul Moore96cb8e32006-08-03 16:48:59 -07001546{
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001547 int ret_val, i;
Paul Moore96cb8e32006-08-03 16:48:59 -07001548
1549 ret_val = genl_register_family(&netlbl_unlabel_gnl_family);
1550 if (ret_val != 0)
1551 return ret_val;
1552
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001553 for (i = 0; i < ARRAY_SIZE(netlbl_unlabel_genl_ops); i++) {
1554 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
1555 &netlbl_unlabel_genl_ops[i]);
1556 if (ret_val != 0)
1557 return ret_val;
1558 }
Paul Moore96cb8e32006-08-03 16:48:59 -07001559
1560 return 0;
1561}
1562
1563/*
1564 * NetLabel KAPI Hooks
1565 */
1566
Paul Moore8cc44572008-01-29 08:44:21 -05001567static struct notifier_block netlbl_unlhsh_netdev_notifier = {
1568 .notifier_call = netlbl_unlhsh_netdev_handler,
1569};
1570
1571/**
1572 * netlbl_unlabel_init - Initialize the unlabeled connection hash table
1573 * @size: the number of bits to use for the hash buckets
1574 *
1575 * Description:
1576 * Initializes the unlabeled connection hash table and registers a network
1577 * device notification handler. This function should only be called by the
1578 * NetLabel subsystem itself during initialization. Returns zero on success,
1579 * non-zero values on error.
1580 *
1581 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -08001582int __init netlbl_unlabel_init(u32 size)
Paul Moore8cc44572008-01-29 08:44:21 -05001583{
1584 u32 iter;
1585 struct netlbl_unlhsh_tbl *hsh_tbl;
1586
1587 if (size == 0)
1588 return -EINVAL;
1589
1590 hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
1591 if (hsh_tbl == NULL)
1592 return -ENOMEM;
1593 hsh_tbl->size = 1 << size;
1594 hsh_tbl->tbl = kcalloc(hsh_tbl->size,
1595 sizeof(struct list_head),
1596 GFP_KERNEL);
1597 if (hsh_tbl->tbl == NULL) {
1598 kfree(hsh_tbl);
1599 return -ENOMEM;
1600 }
1601 for (iter = 0; iter < hsh_tbl->size; iter++)
1602 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
1603
1604 rcu_read_lock();
1605 spin_lock(&netlbl_unlhsh_lock);
1606 rcu_assign_pointer(netlbl_unlhsh, hsh_tbl);
1607 spin_unlock(&netlbl_unlhsh_lock);
1608 rcu_read_unlock();
1609
1610 register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier);
1611
1612 return 0;
1613}
1614
Paul Moore96cb8e32006-08-03 16:48:59 -07001615/**
1616 * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
Paul Moore8cc44572008-01-29 08:44:21 -05001617 * @skb: the packet
1618 * @family: protocol family
Paul Moore96cb8e32006-08-03 16:48:59 -07001619 * @secattr: the security attributes
1620 *
1621 * Description:
1622 * Determine the security attributes, if any, for an unlabled packet and return
1623 * them in @secattr. Returns zero on success and negative values on failure.
1624 *
1625 */
Paul Moore8cc44572008-01-29 08:44:21 -05001626int netlbl_unlabel_getattr(const struct sk_buff *skb,
1627 u16 family,
1628 struct netlbl_lsm_secattr *secattr)
Paul Moore96cb8e32006-08-03 16:48:59 -07001629{
Paul Moore8cc44572008-01-29 08:44:21 -05001630 struct netlbl_unlhsh_iface *iface;
1631
1632 rcu_read_lock();
1633 iface = netlbl_unlhsh_search_iface_def(skb->iif);
1634 if (iface == NULL)
1635 goto unlabel_getattr_nolabel;
1636 switch (family) {
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001637 case PF_INET: {
1638 struct iphdr *hdr4;
Paul Moore61e10682008-10-10 10:16:32 -04001639 struct netlbl_af4list *addr4;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001640
Paul Moore8cc44572008-01-29 08:44:21 -05001641 hdr4 = ip_hdr(skb);
Paul Moore61e10682008-10-10 10:16:32 -04001642 addr4 = netlbl_af4list_search(hdr4->saddr,
1643 &iface->addr4_list);
Paul Moore8cc44572008-01-29 08:44:21 -05001644 if (addr4 == NULL)
1645 goto unlabel_getattr_nolabel;
Paul Moore61e10682008-10-10 10:16:32 -04001646 secattr->attr.secid = netlbl_unlhsh_addr4_entry(addr4)->secid;
Paul Moore8cc44572008-01-29 08:44:21 -05001647 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001648 }
Paul Moore8cc44572008-01-29 08:44:21 -05001649#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001650 case PF_INET6: {
1651 struct ipv6hdr *hdr6;
Paul Moore61e10682008-10-10 10:16:32 -04001652 struct netlbl_af6list *addr6;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001653
Paul Moore8cc44572008-01-29 08:44:21 -05001654 hdr6 = ipv6_hdr(skb);
Paul Moore61e10682008-10-10 10:16:32 -04001655 addr6 = netlbl_af6list_search(&hdr6->saddr,
1656 &iface->addr6_list);
Paul Moore8cc44572008-01-29 08:44:21 -05001657 if (addr6 == NULL)
1658 goto unlabel_getattr_nolabel;
Paul Moore61e10682008-10-10 10:16:32 -04001659 secattr->attr.secid = netlbl_unlhsh_addr6_entry(addr6)->secid;
Paul Moore8cc44572008-01-29 08:44:21 -05001660 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001661 }
Paul Moore8cc44572008-01-29 08:44:21 -05001662#endif /* IPv6 */
1663 default:
1664 goto unlabel_getattr_nolabel;
1665 }
1666 rcu_read_unlock();
1667
1668 secattr->flags |= NETLBL_SECATTR_SECID;
1669 secattr->type = NETLBL_NLTYPE_UNLABELED;
1670 return 0;
1671
1672unlabel_getattr_nolabel:
1673 rcu_read_unlock();
Paul Moorec783f1c2008-01-29 08:37:52 -05001674 if (netlabel_unlabel_acceptflg == 0)
1675 return -ENOMSG;
Paul Moore16efd452008-01-29 08:37:59 -05001676 secattr->type = NETLBL_NLTYPE_UNLABELED;
Paul Moorec783f1c2008-01-29 08:37:52 -05001677 return 0;
Paul Moore96cb8e32006-08-03 16:48:59 -07001678}
1679
1680/**
1681 * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
1682 *
1683 * Description:
1684 * Set the default NetLabel configuration to allow incoming unlabeled packets
1685 * and to send unlabeled network traffic by default.
1686 *
1687 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -08001688int __init netlbl_unlabel_defconf(void)
Paul Moore96cb8e32006-08-03 16:48:59 -07001689{
1690 int ret_val;
1691 struct netlbl_dom_map *entry;
Paul Moore95d4e6b2006-09-29 17:05:05 -07001692 struct netlbl_audit audit_info;
Paul Moore32f50cd2006-09-28 14:51:47 -07001693
Paul Moore95d4e6b2006-09-29 17:05:05 -07001694 /* Only the kernel is allowed to call this function and the only time
1695 * it is called is at bootup before the audit subsystem is reporting
1696 * messages so don't worry to much about these values. */
1697 security_task_getsecid(current, &audit_info.secid);
1698 audit_info.loginuid = 0;
Eric Paris25323862008-04-18 10:09:25 -04001699 audit_info.sessionid = 0;
Paul Moore96cb8e32006-08-03 16:48:59 -07001700
1701 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1702 if (entry == NULL)
1703 return -ENOMEM;
1704 entry->type = NETLBL_NLTYPE_UNLABELED;
Paul Moore95d4e6b2006-09-29 17:05:05 -07001705 ret_val = netlbl_domhsh_add_default(entry, &audit_info);
Paul Moore96cb8e32006-08-03 16:48:59 -07001706 if (ret_val != 0)
1707 return ret_val;
1708
Paul Moore95d4e6b2006-09-29 17:05:05 -07001709 netlbl_unlabel_acceptflg_set(1, &audit_info);
Paul Moore96cb8e32006-08-03 16:48:59 -07001710
1711 return 0;
1712}