2 * NetLabel Unlabeled Support
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.
8 * Author: Paul Moore <paul.moore@hp.com>
13 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006 - 2007
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.
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.
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
31 #include <linux/types.h>
32 #include <linux/rcupdate.h>
33 #include <linux/list.h>
34 #include <linux/spinlock.h>
35 #include <linux/socket.h>
36 #include <linux/string.h>
37 #include <linux/skbuff.h>
38 #include <linux/audit.h>
40 #include <linux/in6.h>
42 #include <linux/ipv6.h>
43 #include <linux/notifier.h>
44 #include <linux/netdevice.h>
45 #include <linux/security.h>
47 #include <net/netlink.h>
48 #include <net/genetlink.h>
51 #include <net/net_namespace.h>
52 #include <net/netlabel.h>
54 #include <asm/atomic.h>
56 #include "netlabel_user.h"
57 #include "netlabel_domainhash.h"
58 #include "netlabel_unlabeled.h"
59 #include "netlabel_mgmt.h"
61 /* NOTE: at present we always use init's network namespace since we don't
62 * presently support different namespaces even though the majority of
63 * the functions in this file are "namespace safe" */
65 /* The unlabeled connection hash table which we use to map network interfaces
66 * and addresses of unlabeled packets to a user specified secid value for the
67 * LSM. The hash table is used to lookup the network interface entry
68 * (struct netlbl_unlhsh_iface) and then the interface entry is used to
69 * lookup an IP address match from an ordered list. If a network interface
70 * match can not be found in the hash table then the default entry
71 * (netlbl_unlhsh_def) is used. The IP address entry list
72 * (struct netlbl_unlhsh_addr) is ordered such that the entries with a
73 * larger netmask come first.
75 struct netlbl_unlhsh_tbl {
76 struct list_head *tbl;
79 struct netlbl_unlhsh_addr4 {
85 struct list_head list;
88 struct netlbl_unlhsh_addr6 {
94 struct list_head list;
97 struct netlbl_unlhsh_iface {
99 struct list_head addr4_list;
100 struct list_head addr6_list;
103 struct list_head list;
107 /* Argument struct for netlbl_unlhsh_walk() */
108 struct netlbl_unlhsh_walk_arg {
109 struct netlink_callback *nl_cb;
114 /* Unlabeled connection hash table */
115 /* updates should be so rare that having one spinlock for the entire
116 * hash table should be okay */
117 static DEFINE_SPINLOCK(netlbl_unlhsh_lock);
118 static struct netlbl_unlhsh_tbl *netlbl_unlhsh = NULL;
119 static struct netlbl_unlhsh_iface *netlbl_unlhsh_def = NULL;
121 /* Accept unlabeled packets flag */
122 static u8 netlabel_unlabel_acceptflg = 0;
124 /* NetLabel Generic NETLINK unlabeled family */
125 static struct genl_family netlbl_unlabel_gnl_family = {
126 .id = GENL_ID_GENERATE,
128 .name = NETLBL_NLTYPE_UNLABELED_NAME,
129 .version = NETLBL_PROTO_VERSION,
130 .maxattr = NLBL_UNLABEL_A_MAX,
133 /* NetLabel Netlink attribute policy */
134 static const struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = {
135 [NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 },
136 [NLBL_UNLABEL_A_IPV6ADDR] = { .type = NLA_BINARY,
137 .len = sizeof(struct in6_addr) },
138 [NLBL_UNLABEL_A_IPV6MASK] = { .type = NLA_BINARY,
139 .len = sizeof(struct in6_addr) },
140 [NLBL_UNLABEL_A_IPV4ADDR] = { .type = NLA_BINARY,
141 .len = sizeof(struct in_addr) },
142 [NLBL_UNLABEL_A_IPV4MASK] = { .type = NLA_BINARY,
143 .len = sizeof(struct in_addr) },
144 [NLBL_UNLABEL_A_IFACE] = { .type = NLA_NUL_STRING,
145 .len = IFNAMSIZ - 1 },
146 [NLBL_UNLABEL_A_SECCTX] = { .type = NLA_BINARY }
150 * Unlabeled Connection Hash Table Functions
154 * netlbl_unlhsh_free_addr4 - Frees an IPv4 address entry from the hash table
155 * @entry: the entry's RCU field
158 * This function is designed to be used as a callback to the call_rcu()
159 * function so that memory allocated to a hash table address entry can be
163 static void netlbl_unlhsh_free_addr4(struct rcu_head *entry)
165 struct netlbl_unlhsh_addr4 *ptr;
167 ptr = container_of(entry, struct netlbl_unlhsh_addr4, rcu);
171 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
173 * netlbl_unlhsh_free_addr6 - Frees an IPv6 address entry from the hash table
174 * @entry: the entry's RCU field
177 * This function is designed to be used as a callback to the call_rcu()
178 * function so that memory allocated to a hash table address entry can be
182 static void netlbl_unlhsh_free_addr6(struct rcu_head *entry)
184 struct netlbl_unlhsh_addr6 *ptr;
186 ptr = container_of(entry, struct netlbl_unlhsh_addr6, rcu);
192 * netlbl_unlhsh_free_iface - Frees an interface entry from the hash table
193 * @entry: the entry's RCU field
196 * This function is designed to be used as a callback to the call_rcu()
197 * function so that memory allocated to a hash table interface entry can be
198 * released safely. It is important to note that this function does not free
199 * the IPv4 and IPv6 address lists contained as part of an interface entry. It
200 * is up to the rest of the code to make sure an interface entry is only freed
201 * once it's address lists are empty.
204 static void netlbl_unlhsh_free_iface(struct rcu_head *entry)
206 struct netlbl_unlhsh_iface *iface;
207 struct netlbl_unlhsh_addr4 *iter4;
208 struct netlbl_unlhsh_addr4 *tmp4;
209 struct netlbl_unlhsh_addr6 *iter6;
210 struct netlbl_unlhsh_addr6 *tmp6;
212 iface = container_of(entry, struct netlbl_unlhsh_iface, rcu);
214 /* no need for locks here since we are the only one with access to this
217 list_for_each_entry_safe(iter4, tmp4, &iface->addr4_list, list)
219 list_del_rcu(&iter4->list);
222 list_for_each_entry_safe(iter6, tmp6, &iface->addr6_list, list)
224 list_del_rcu(&iter6->list);
231 * netlbl_unlhsh_hash - Hashing function for the hash table
232 * @ifindex: the network interface/device to hash
235 * This is the hashing function for the unlabeled hash table, it returns the
236 * bucket number for the given device/interface. The caller is responsible for
237 * calling the rcu_read_[un]lock() functions.
240 static u32 netlbl_unlhsh_hash(int ifindex)
242 /* this is taken _almost_ directly from
243 * security/selinux/netif.c:sel_netif_hasfn() as they do pretty much
245 return ifindex & (rcu_dereference(netlbl_unlhsh)->size - 1);
249 * netlbl_unlhsh_search_addr4 - Search for a matching IPv4 address entry
250 * @addr: IPv4 address
251 * @iface: the network interface entry
254 * Searches the IPv4 address list of the network interface specified by @iface.
255 * If a matching address entry is found it is returned, otherwise NULL is
256 * returned. The caller is responsible for calling the rcu_read_[un]lock()
260 static struct netlbl_unlhsh_addr4 *netlbl_unlhsh_search_addr4(
262 const struct netlbl_unlhsh_iface *iface)
264 struct netlbl_unlhsh_addr4 *iter;
266 list_for_each_entry_rcu(iter, &iface->addr4_list, list)
267 if (iter->valid && (addr & iter->mask) == iter->addr)
273 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
275 * netlbl_unlhsh_search_addr6 - Search for a matching IPv6 address entry
276 * @addr: IPv6 address
277 * @iface: the network interface entry
280 * Searches the IPv6 address list of the network interface specified by @iface.
281 * If a matching address entry is found it is returned, otherwise NULL is
282 * returned. The caller is responsible for calling the rcu_read_[un]lock()
286 static struct netlbl_unlhsh_addr6 *netlbl_unlhsh_search_addr6(
287 const struct in6_addr *addr,
288 const struct netlbl_unlhsh_iface *iface)
290 struct netlbl_unlhsh_addr6 *iter;
292 list_for_each_entry_rcu(iter, &iface->addr6_list, list)
294 ipv6_masked_addr_cmp(&iter->addr, &iter->mask, addr) == 0)
302 * netlbl_unlhsh_search_iface - Search for a matching interface entry
303 * @ifindex: the network interface
306 * Searches the unlabeled connection hash table and returns a pointer to the
307 * interface entry which matches @ifindex, otherwise NULL is returned. The
308 * caller is responsible for calling the rcu_read_[un]lock() functions.
311 static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface(int ifindex)
314 struct netlbl_unlhsh_iface *iter;
316 bkt = netlbl_unlhsh_hash(ifindex);
317 list_for_each_entry_rcu(iter,
318 &rcu_dereference(netlbl_unlhsh)->tbl[bkt],
320 if (iter->valid && iter->ifindex == ifindex)
327 * netlbl_unlhsh_search_iface_def - Search for a matching interface entry
328 * @ifindex: the network interface
331 * Searches the unlabeled connection hash table and returns a pointer to the
332 * interface entry which matches @ifindex. If an exact match can not be found
333 * and there is a valid default entry, the default entry is returned, otherwise
334 * NULL is returned. The caller is responsible for calling the
335 * rcu_read_[un]lock() functions.
338 static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface_def(int ifindex)
340 struct netlbl_unlhsh_iface *entry;
342 entry = netlbl_unlhsh_search_iface(ifindex);
346 entry = rcu_dereference(netlbl_unlhsh_def);
347 if (entry != NULL && entry->valid)
354 * netlbl_unlhsh_add_addr4 - Add a new IPv4 address entry to the hash table
355 * @iface: the associated interface entry
356 * @addr: IPv4 address in network byte order
357 * @mask: IPv4 address mask in network byte order
358 * @secid: LSM secid value for entry
361 * Add a new address entry into the unlabeled connection hash table using the
362 * interface entry specified by @iface. On success zero is returned, otherwise
363 * a negative value is returned. The caller is responsible for calling the
364 * rcu_read_[un]lock() functions.
367 static int netlbl_unlhsh_add_addr4(struct netlbl_unlhsh_iface *iface,
368 const struct in_addr *addr,
369 const struct in_addr *mask,
372 struct netlbl_unlhsh_addr4 *entry;
373 struct netlbl_unlhsh_addr4 *iter;
375 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
379 entry->addr = addr->s_addr & mask->s_addr;
380 entry->mask = mask->s_addr;
381 entry->secid = secid;
383 INIT_RCU_HEAD(&entry->rcu);
385 spin_lock(&netlbl_unlhsh_lock);
386 iter = netlbl_unlhsh_search_addr4(entry->addr, iface);
388 iter->addr == addr->s_addr && iter->mask == mask->s_addr) {
389 spin_unlock(&netlbl_unlhsh_lock);
393 /* in order to speed up address searches through the list (the common
394 * case) we need to keep the list in order based on the size of the
395 * address mask such that the entry with the widest mask (smallest
396 * numerical value) appears first in the list */
397 list_for_each_entry_rcu(iter, &iface->addr4_list, list)
399 ntohl(entry->mask) > ntohl(iter->mask)) {
400 __list_add_rcu(&entry->list,
403 spin_unlock(&netlbl_unlhsh_lock);
406 list_add_tail_rcu(&entry->list, &iface->addr4_list);
407 spin_unlock(&netlbl_unlhsh_lock);
411 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
413 * netlbl_unlhsh_add_addr6 - Add a new IPv6 address entry to the hash table
414 * @iface: the associated interface entry
415 * @addr: IPv6 address in network byte order
416 * @mask: IPv6 address mask in network byte order
417 * @secid: LSM secid value for entry
420 * Add a new address entry into the unlabeled connection hash table using the
421 * interface entry specified by @iface. On success zero is returned, otherwise
422 * a negative value is returned. The caller is responsible for calling the
423 * rcu_read_[un]lock() functions.
426 static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface,
427 const struct in6_addr *addr,
428 const struct in6_addr *mask,
431 struct netlbl_unlhsh_addr6 *entry;
432 struct netlbl_unlhsh_addr6 *iter;
434 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
438 ipv6_addr_copy(&entry->addr, addr);
439 entry->addr.s6_addr32[0] &= mask->s6_addr32[0];
440 entry->addr.s6_addr32[1] &= mask->s6_addr32[1];
441 entry->addr.s6_addr32[2] &= mask->s6_addr32[2];
442 entry->addr.s6_addr32[3] &= mask->s6_addr32[3];
443 ipv6_addr_copy(&entry->mask, mask);
444 entry->secid = secid;
446 INIT_RCU_HEAD(&entry->rcu);
448 spin_lock(&netlbl_unlhsh_lock);
449 iter = netlbl_unlhsh_search_addr6(&entry->addr, iface);
451 (ipv6_addr_equal(&iter->addr, addr) &&
452 ipv6_addr_equal(&iter->mask, mask))) {
453 spin_unlock(&netlbl_unlhsh_lock);
457 /* in order to speed up address searches through the list (the common
458 * case) we need to keep the list in order based on the size of the
459 * address mask such that the entry with the widest mask (smallest
460 * numerical value) appears first in the list */
461 list_for_each_entry_rcu(iter, &iface->addr6_list, list)
463 ipv6_addr_cmp(&entry->mask, &iter->mask) > 0) {
464 __list_add_rcu(&entry->list,
467 spin_unlock(&netlbl_unlhsh_lock);
470 list_add_tail_rcu(&entry->list, &iface->addr6_list);
471 spin_unlock(&netlbl_unlhsh_lock);
477 * netlbl_unlhsh_add_iface - Adds a new interface entry to the hash table
478 * @ifindex: network interface
481 * Add a new, empty, interface entry into the unlabeled connection hash table.
482 * On success a pointer to the new interface entry is returned, on failure NULL
483 * is returned. The caller is responsible for calling the rcu_read_[un]lock()
487 static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex)
490 struct netlbl_unlhsh_iface *iface;
492 iface = kzalloc(sizeof(*iface), GFP_ATOMIC);
496 iface->ifindex = ifindex;
497 INIT_LIST_HEAD(&iface->addr4_list);
498 INIT_LIST_HEAD(&iface->addr6_list);
500 INIT_RCU_HEAD(&iface->rcu);
502 spin_lock(&netlbl_unlhsh_lock);
504 bkt = netlbl_unlhsh_hash(ifindex);
505 if (netlbl_unlhsh_search_iface(ifindex) != NULL)
506 goto add_iface_failure;
507 list_add_tail_rcu(&iface->list,
508 &rcu_dereference(netlbl_unlhsh)->tbl[bkt]);
510 INIT_LIST_HEAD(&iface->list);
511 if (rcu_dereference(netlbl_unlhsh_def) != NULL)
512 goto add_iface_failure;
513 rcu_assign_pointer(netlbl_unlhsh_def, iface);
515 spin_unlock(&netlbl_unlhsh_lock);
520 spin_unlock(&netlbl_unlhsh_lock);
526 * netlbl_unlhsh_add - Adds a new entry to the unlabeled connection hash table
527 * @net: network namespace
528 * @dev_name: interface name
529 * @addr: IP address in network byte order
530 * @mask: address mask in network byte order
531 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
532 * @secid: LSM secid value for the entry
535 * Adds a new entry to the unlabeled connection hash table. Returns zero on
536 * success, negative values on failure.
539 static int netlbl_unlhsh_add(struct net *net,
540 const char *dev_name,
548 struct net_device *dev;
549 struct netlbl_unlhsh_iface *iface;
551 if (addr_len != sizeof(struct in_addr) &&
552 addr_len != sizeof(struct in6_addr))
556 if (dev_name != NULL) {
557 dev = dev_get_by_name(net, dev_name);
560 goto unlhsh_add_return;
562 ifindex = dev->ifindex;
564 iface = netlbl_unlhsh_search_iface(ifindex);
567 iface = rcu_dereference(netlbl_unlhsh_def);
570 iface = netlbl_unlhsh_add_iface(ifindex);
573 goto unlhsh_add_return;
577 case sizeof(struct in_addr):
578 ret_val = netlbl_unlhsh_add_addr4(iface, addr, mask, secid);
580 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
581 case sizeof(struct in6_addr):
582 ret_val = netlbl_unlhsh_add_addr6(iface, addr, mask, secid);
589 atomic_inc(&netlabel_mgmt_protocount);
597 * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry
598 * @iface: interface entry
600 * @mask: IP address mask
603 * Remove an IP address entry from the unlabeled connection hash table.
604 * Returns zero on success, negative values on failure. The caller is
605 * responsible for calling the rcu_read_[un]lock() functions.
608 static int netlbl_unlhsh_remove_addr4(struct netlbl_unlhsh_iface *iface,
609 const struct in_addr *addr,
610 const struct in_addr *mask)
612 int ret_val = -ENOENT;
613 struct netlbl_unlhsh_addr4 *entry;
615 spin_lock(&netlbl_unlhsh_lock);
616 entry = netlbl_unlhsh_search_addr4(addr->s_addr, iface);
618 entry->addr == addr->s_addr && entry->mask == mask->s_addr) {
620 list_del_rcu(&entry->list);
623 spin_unlock(&netlbl_unlhsh_lock);
626 call_rcu(&entry->rcu, netlbl_unlhsh_free_addr4);
630 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
632 * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry
633 * @iface: interface entry
635 * @mask: IP address mask
638 * Remove an IP address entry from the unlabeled connection hash table.
639 * Returns zero on success, negative values on failure. The caller is
640 * responsible for calling the rcu_read_[un]lock() functions.
643 static int netlbl_unlhsh_remove_addr6(struct netlbl_unlhsh_iface *iface,
644 const struct in6_addr *addr,
645 const struct in6_addr *mask)
647 int ret_val = -ENOENT;
648 struct netlbl_unlhsh_addr6 *entry;
650 spin_lock(&netlbl_unlhsh_lock);
651 entry = netlbl_unlhsh_search_addr6(addr, iface);
653 (ipv6_addr_equal(&entry->addr, addr) &&
654 ipv6_addr_equal(&entry->mask, mask))) {
656 list_del_rcu(&entry->list);
659 spin_unlock(&netlbl_unlhsh_lock);
662 call_rcu(&entry->rcu, netlbl_unlhsh_free_addr6);
668 * netlbl_unlhsh_condremove_iface - Remove an interface entry
669 * @iface: the interface entry
672 * Remove an interface entry from the unlabeled connection hash table if it is
673 * empty. An interface entry is considered to be empty if there are no
674 * address entries assigned to it.
677 static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface)
679 struct netlbl_unlhsh_addr4 *iter4;
680 struct netlbl_unlhsh_addr6 *iter6;
682 spin_lock(&netlbl_unlhsh_lock);
683 list_for_each_entry_rcu(iter4, &iface->addr4_list, list)
685 goto unlhsh_condremove_failure;
686 list_for_each_entry_rcu(iter6, &iface->addr6_list, list)
688 goto unlhsh_condremove_failure;
690 if (iface->ifindex > 0)
691 list_del_rcu(&iface->list);
693 rcu_assign_pointer(netlbl_unlhsh_def, NULL);
694 spin_unlock(&netlbl_unlhsh_lock);
696 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
699 unlhsh_condremove_failure:
700 spin_unlock(&netlbl_unlhsh_lock);
705 * netlbl_unlhsh_remove - Remove an entry from the unlabeled hash table
706 * @net: network namespace
707 * @dev_name: interface name
708 * @addr: IP address in network byte order
709 * @mask: address mask in network byte order
710 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
713 * Removes and existing entry from the unlabeled connection hash table.
714 * Returns zero on success, negative values on failure.
717 static int netlbl_unlhsh_remove(struct net *net,
718 const char *dev_name,
724 struct net_device *dev;
725 struct netlbl_unlhsh_iface *iface;
727 if (addr_len != sizeof(struct in_addr) &&
728 addr_len != sizeof(struct in6_addr))
732 if (dev_name != NULL) {
733 dev = dev_get_by_name(net, dev_name);
736 goto unlhsh_remove_return;
738 iface = netlbl_unlhsh_search_iface(dev->ifindex);
741 iface = rcu_dereference(netlbl_unlhsh_def);
744 goto unlhsh_remove_return;
747 case sizeof(struct in_addr):
748 ret_val = netlbl_unlhsh_remove_addr4(iface, addr, mask);
750 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
751 case sizeof(struct in6_addr):
752 ret_val = netlbl_unlhsh_remove_addr6(iface, addr, mask);
759 netlbl_unlhsh_condremove_iface(iface);
760 atomic_dec(&netlabel_mgmt_protocount);
763 unlhsh_remove_return:
769 * General Helper Functions
773 * netlbl_unlhsh_netdev_handler - Network device notification handler
774 * @this: notifier block
776 * @ptr: the network device (cast to void)
779 * Handle network device events, although at present all we care about is a
780 * network device going away. In the case of a device going away we clear any
781 * related entries from the unlabeled connection hash table.
784 static int netlbl_unlhsh_netdev_handler(struct notifier_block *this,
788 struct net_device *dev = ptr;
789 struct netlbl_unlhsh_iface *iface = NULL;
791 if (dev->nd_net != &init_net)
794 /* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */
795 if (event == NETDEV_DOWN) {
796 spin_lock(&netlbl_unlhsh_lock);
797 iface = netlbl_unlhsh_search_iface(dev->ifindex);
798 if (iface != NULL && iface->valid) {
800 list_del_rcu(&iface->list);
803 spin_unlock(&netlbl_unlhsh_lock);
807 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
813 * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
814 * @value: desired value
815 * @audit_info: NetLabel audit information
818 * Set the value of the unlabeled accept flag to @value.
821 static void netlbl_unlabel_acceptflg_set(u8 value,
822 struct netlbl_audit *audit_info)
824 struct audit_buffer *audit_buf;
827 old_val = netlabel_unlabel_acceptflg;
828 netlabel_unlabel_acceptflg = value;
829 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
831 if (audit_buf != NULL) {
832 audit_log_format(audit_buf,
833 " unlbl_accept=%u old=%u", value, old_val);
834 audit_log_end(audit_buf);
839 * netlbl_unlabel_addrinfo_get - Get the IPv4/6 address information
840 * @info: the Generic NETLINK info block
841 * @addr: the IP address
842 * @mask: the IP address mask
843 * @len: the address length
846 * Examine the Generic NETLINK message and extract the IP address information.
847 * Returns zero on success, negative values on failure.
850 static int netlbl_unlabel_addrinfo_get(struct genl_info *info,
857 if (info->attrs[NLBL_UNLABEL_A_IPV4ADDR]) {
858 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
859 if (addr_len != sizeof(struct in_addr) &&
860 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK]))
863 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
864 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]);
866 } else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) {
867 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
868 if (addr_len != sizeof(struct in6_addr) &&
869 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK]))
872 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
873 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]);
881 * NetLabel Command Handlers
885 * netlbl_unlabel_accept - Handle an ACCEPT message
886 * @skb: the NETLINK buffer
887 * @info: the Generic NETLINK info block
890 * Process a user generated ACCEPT message and set the accept flag accordingly.
891 * Returns zero on success, negative values on failure.
894 static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
897 struct netlbl_audit audit_info;
899 if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
900 value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
901 if (value == 1 || value == 0) {
902 netlbl_netlink_auditinfo(skb, &audit_info);
903 netlbl_unlabel_acceptflg_set(value, &audit_info);
912 * netlbl_unlabel_list - Handle a LIST message
913 * @skb: the NETLINK buffer
914 * @info: the Generic NETLINK info block
917 * Process a user generated LIST message and respond with the current status.
918 * Returns zero on success, negative values on failure.
921 static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
923 int ret_val = -EINVAL;
924 struct sk_buff *ans_skb;
927 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
930 data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
931 0, NLBL_UNLABEL_C_LIST);
937 ret_val = nla_put_u8(ans_skb,
938 NLBL_UNLABEL_A_ACPTFLG,
939 netlabel_unlabel_acceptflg);
943 genlmsg_end(ans_skb, data);
945 ret_val = genlmsg_reply(ans_skb, info);
956 * netlbl_unlabel_staticadd - Handle a STATICADD message
957 * @skb: the NETLINK buffer
958 * @info: the Generic NETLINK info block
961 * Process a user generated STATICADD message and add a new unlabeled
962 * connection entry to the hash table. Returns zero on success, negative
966 static int netlbl_unlabel_staticadd(struct sk_buff *skb,
967 struct genl_info *info)
976 /* Don't allow users to add both IPv4 and IPv6 addresses for a
977 * single entry. However, allow users to create two entries, one each
978 * for IPv4 and IPv4, with the same LSM security context which should
979 * achieve the same result. */
980 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
981 !info->attrs[NLBL_UNLABEL_A_IFACE] ||
982 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
983 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
984 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
985 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
988 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
991 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
992 ret_val = security_secctx_to_secid(
993 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
994 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
999 return netlbl_unlhsh_add(&init_net,
1000 dev_name, addr, mask, addr_len, secid);
1004 * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message
1005 * @skb: the NETLINK buffer
1006 * @info: the Generic NETLINK info block
1009 * Process a user generated STATICADDDEF message and add a new default
1010 * unlabeled connection entry. Returns zero on success, negative values on
1014 static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
1015 struct genl_info *info)
1023 /* Don't allow users to add both IPv4 and IPv6 addresses for a
1024 * single entry. However, allow users to create two entries, one each
1025 * for IPv4 and IPv6, with the same LSM security context which should
1026 * achieve the same result. */
1027 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
1028 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1029 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1030 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1031 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1034 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1037 ret_val = security_secctx_to_secid(
1038 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1039 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1044 return netlbl_unlhsh_add(&init_net, NULL, addr, mask, addr_len, secid);
1048 * netlbl_unlabel_staticremove - Handle a STATICREMOVE message
1049 * @skb: the NETLINK buffer
1050 * @info: the Generic NETLINK info block
1053 * Process a user generated STATICREMOVE message and remove the specified
1054 * unlabeled connection entry. Returns zero on success, negative values on
1058 static int netlbl_unlabel_staticremove(struct sk_buff *skb,
1059 struct genl_info *info)
1067 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1068 * IPv4 and IPv6 in the same entry. */
1069 if (!info->attrs[NLBL_UNLABEL_A_IFACE] ||
1070 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1071 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1072 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1073 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1076 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1079 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
1081 return netlbl_unlhsh_remove(&init_net, dev_name, addr, mask, addr_len);
1085 * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message
1086 * @skb: the NETLINK buffer
1087 * @info: the Generic NETLINK info block
1090 * Process a user generated STATICREMOVEDEF message and remove the default
1091 * unlabeled connection entry. Returns zero on success, negative values on
1095 static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
1096 struct genl_info *info)
1103 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1104 * IPv4 and IPv6 in the same entry. */
1105 if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1106 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1107 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1108 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1111 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1115 return netlbl_unlhsh_remove(&init_net, NULL, addr, mask, addr_len);
1120 * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF]
1121 * @cmd: command/message
1122 * @iface: the interface entry
1123 * @addr4: the IPv4 address entry
1124 * @addr6: the IPv6 address entry
1125 * @arg: the netlbl_unlhsh_walk_arg structure
1128 * This function is designed to be used to generate a response for a
1129 * STATICLIST or STATICLISTDEF message. When called either @addr4 or @addr6
1130 * can be specified, not both, the other unspecified entry should be set to
1131 * NULL by the caller. Returns the size of the message on success, negative
1132 * values on failure.
1135 static int netlbl_unlabel_staticlist_gen(u32 cmd,
1136 const struct netlbl_unlhsh_iface *iface,
1137 const struct netlbl_unlhsh_addr4 *addr4,
1138 const struct netlbl_unlhsh_addr6 *addr6,
1141 int ret_val = -ENOMEM;
1142 struct netlbl_unlhsh_walk_arg *cb_arg = arg;
1143 struct net_device *dev;
1149 data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
1150 cb_arg->seq, &netlbl_unlabel_gnl_family,
1153 goto list_cb_failure;
1155 if (iface->ifindex > 0) {
1156 dev = dev_get_by_index(&init_net, iface->ifindex);
1157 ret_val = nla_put_string(cb_arg->skb,
1158 NLBL_UNLABEL_A_IFACE, dev->name);
1161 goto list_cb_failure;
1165 struct in_addr addr_struct;
1167 addr_struct.s_addr = addr4->addr;
1168 ret_val = nla_put(cb_arg->skb,
1169 NLBL_UNLABEL_A_IPV4ADDR,
1170 sizeof(struct in_addr),
1173 goto list_cb_failure;
1175 addr_struct.s_addr = addr4->mask;
1176 ret_val = nla_put(cb_arg->skb,
1177 NLBL_UNLABEL_A_IPV4MASK,
1178 sizeof(struct in_addr),
1181 goto list_cb_failure;
1183 secid = addr4->secid;
1185 ret_val = nla_put(cb_arg->skb,
1186 NLBL_UNLABEL_A_IPV6ADDR,
1187 sizeof(struct in6_addr),
1190 goto list_cb_failure;
1192 ret_val = nla_put(cb_arg->skb,
1193 NLBL_UNLABEL_A_IPV6MASK,
1194 sizeof(struct in6_addr),
1197 goto list_cb_failure;
1199 secid = addr6->secid;
1202 ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len);
1204 goto list_cb_failure;
1205 ret_val = nla_put(cb_arg->skb,
1206 NLBL_UNLABEL_A_SECCTX,
1209 security_release_secctx(secctx, secctx_len);
1211 goto list_cb_failure;
1214 return genlmsg_end(cb_arg->skb, data);
1217 genlmsg_cancel(cb_arg->skb, data);
1222 * netlbl_unlabel_staticlist - Handle a STATICLIST message
1223 * @skb: the NETLINK buffer
1224 * @cb: the NETLINK callback
1227 * Process a user generated STATICLIST message and dump the unlabeled
1228 * connection hash table in a form suitable for use in a kernel generated
1229 * STATICLIST message. Returns the length of @skb.
1232 static int netlbl_unlabel_staticlist(struct sk_buff *skb,
1233 struct netlink_callback *cb)
1235 struct netlbl_unlhsh_walk_arg cb_arg;
1236 u32 skip_bkt = cb->args[0];
1237 u32 skip_chain = cb->args[1];
1238 u32 skip_addr4 = cb->args[2];
1239 u32 skip_addr6 = cb->args[3];
1241 u32 iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
1242 struct netlbl_unlhsh_iface *iface;
1243 struct netlbl_unlhsh_addr4 *addr4;
1244 struct netlbl_unlhsh_addr6 *addr6;
1248 cb_arg.seq = cb->nlh->nlmsg_seq;
1251 for (iter_bkt = skip_bkt;
1252 iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
1253 iter_bkt++, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0) {
1254 list_for_each_entry_rcu(iface,
1255 &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt],
1257 if (!iface->valid ||
1258 iter_chain++ < skip_chain)
1260 list_for_each_entry_rcu(addr4,
1263 if (!addr4->valid || iter_addr4++ < skip_addr4)
1265 if (netlbl_unlabel_staticlist_gen(
1266 NLBL_UNLABEL_C_STATICLIST,
1273 goto unlabel_staticlist_return;
1276 list_for_each_entry_rcu(addr6,
1279 if (!addr6->valid || iter_addr6++ < skip_addr6)
1281 if (netlbl_unlabel_staticlist_gen(
1282 NLBL_UNLABEL_C_STATICLIST,
1289 goto unlabel_staticlist_return;
1295 unlabel_staticlist_return:
1297 cb->args[0] = skip_bkt;
1298 cb->args[1] = skip_chain;
1299 cb->args[2] = skip_addr4;
1300 cb->args[3] = skip_addr6;
1305 * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message
1306 * @skb: the NETLINK buffer
1307 * @cb: the NETLINK callback
1310 * Process a user generated STATICLISTDEF message and dump the default
1311 * unlabeled connection entry in a form suitable for use in a kernel generated
1312 * STATICLISTDEF message. Returns the length of @skb.
1315 static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
1316 struct netlink_callback *cb)
1318 struct netlbl_unlhsh_walk_arg cb_arg;
1319 struct netlbl_unlhsh_iface *iface;
1320 u32 skip_addr4 = cb->args[0];
1321 u32 skip_addr6 = cb->args[1];
1322 u32 iter_addr4 = 0, iter_addr6 = 0;
1323 struct netlbl_unlhsh_addr4 *addr4;
1324 struct netlbl_unlhsh_addr6 *addr6;
1328 cb_arg.seq = cb->nlh->nlmsg_seq;
1331 iface = rcu_dereference(netlbl_unlhsh_def);
1332 if (iface == NULL || !iface->valid)
1333 goto unlabel_staticlistdef_return;
1335 list_for_each_entry_rcu(addr4, &iface->addr4_list, list) {
1336 if (!addr4->valid || iter_addr4++ < skip_addr4)
1338 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
1344 goto unlabel_staticlistdef_return;
1347 list_for_each_entry_rcu(addr6, &iface->addr6_list, list) {
1348 if (addr6->valid || iter_addr6++ < skip_addr6)
1350 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
1356 goto unlabel_staticlistdef_return;
1360 unlabel_staticlistdef_return:
1362 cb->args[0] = skip_addr4;
1363 cb->args[1] = skip_addr6;
1368 * NetLabel Generic NETLINK Command Definitions
1371 static struct genl_ops netlbl_unlabel_genl_c_staticadd = {
1372 .cmd = NLBL_UNLABEL_C_STATICADD,
1373 .flags = GENL_ADMIN_PERM,
1374 .policy = netlbl_unlabel_genl_policy,
1375 .doit = netlbl_unlabel_staticadd,
1379 static struct genl_ops netlbl_unlabel_genl_c_staticremove = {
1380 .cmd = NLBL_UNLABEL_C_STATICREMOVE,
1381 .flags = GENL_ADMIN_PERM,
1382 .policy = netlbl_unlabel_genl_policy,
1383 .doit = netlbl_unlabel_staticremove,
1387 static struct genl_ops netlbl_unlabel_genl_c_staticlist = {
1388 .cmd = NLBL_UNLABEL_C_STATICLIST,
1390 .policy = netlbl_unlabel_genl_policy,
1392 .dumpit = netlbl_unlabel_staticlist,
1395 static struct genl_ops netlbl_unlabel_genl_c_staticadddef = {
1396 .cmd = NLBL_UNLABEL_C_STATICADDDEF,
1397 .flags = GENL_ADMIN_PERM,
1398 .policy = netlbl_unlabel_genl_policy,
1399 .doit = netlbl_unlabel_staticadddef,
1403 static struct genl_ops netlbl_unlabel_genl_c_staticremovedef = {
1404 .cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
1405 .flags = GENL_ADMIN_PERM,
1406 .policy = netlbl_unlabel_genl_policy,
1407 .doit = netlbl_unlabel_staticremovedef,
1411 static struct genl_ops netlbl_unlabel_genl_c_staticlistdef = {
1412 .cmd = NLBL_UNLABEL_C_STATICLISTDEF,
1414 .policy = netlbl_unlabel_genl_policy,
1416 .dumpit = netlbl_unlabel_staticlistdef,
1419 static struct genl_ops netlbl_unlabel_genl_c_accept = {
1420 .cmd = NLBL_UNLABEL_C_ACCEPT,
1421 .flags = GENL_ADMIN_PERM,
1422 .policy = netlbl_unlabel_genl_policy,
1423 .doit = netlbl_unlabel_accept,
1427 static struct genl_ops netlbl_unlabel_genl_c_list = {
1428 .cmd = NLBL_UNLABEL_C_LIST,
1430 .policy = netlbl_unlabel_genl_policy,
1431 .doit = netlbl_unlabel_list,
1436 * NetLabel Generic NETLINK Protocol Functions
1440 * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
1443 * Register the unlabeled packet NetLabel component with the Generic NETLINK
1444 * mechanism. Returns zero on success, negative values on failure.
1447 int netlbl_unlabel_genl_init(void)
1451 ret_val = genl_register_family(&netlbl_unlabel_gnl_family);
1455 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
1456 &netlbl_unlabel_genl_c_staticadd);
1460 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
1461 &netlbl_unlabel_genl_c_staticremove);
1465 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
1466 &netlbl_unlabel_genl_c_staticlist);
1470 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
1471 &netlbl_unlabel_genl_c_staticadddef);
1475 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
1476 &netlbl_unlabel_genl_c_staticremovedef);
1480 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
1481 &netlbl_unlabel_genl_c_staticlistdef);
1485 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
1486 &netlbl_unlabel_genl_c_accept);
1490 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
1491 &netlbl_unlabel_genl_c_list);
1499 * NetLabel KAPI Hooks
1502 static struct notifier_block netlbl_unlhsh_netdev_notifier = {
1503 .notifier_call = netlbl_unlhsh_netdev_handler,
1507 * netlbl_unlabel_init - Initialize the unlabeled connection hash table
1508 * @size: the number of bits to use for the hash buckets
1511 * Initializes the unlabeled connection hash table and registers a network
1512 * device notification handler. This function should only be called by the
1513 * NetLabel subsystem itself during initialization. Returns zero on success,
1514 * non-zero values on error.
1517 int netlbl_unlabel_init(u32 size)
1520 struct netlbl_unlhsh_tbl *hsh_tbl;
1525 hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
1526 if (hsh_tbl == NULL)
1528 hsh_tbl->size = 1 << size;
1529 hsh_tbl->tbl = kcalloc(hsh_tbl->size,
1530 sizeof(struct list_head),
1532 if (hsh_tbl->tbl == NULL) {
1536 for (iter = 0; iter < hsh_tbl->size; iter++)
1537 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
1540 spin_lock(&netlbl_unlhsh_lock);
1541 rcu_assign_pointer(netlbl_unlhsh, hsh_tbl);
1542 spin_unlock(&netlbl_unlhsh_lock);
1545 register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier);
1551 * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
1553 * @family: protocol family
1554 * @secattr: the security attributes
1557 * Determine the security attributes, if any, for an unlabled packet and return
1558 * them in @secattr. Returns zero on success and negative values on failure.
1561 int netlbl_unlabel_getattr(const struct sk_buff *skb,
1563 struct netlbl_lsm_secattr *secattr)
1566 struct ipv6hdr *hdr6;
1567 struct netlbl_unlhsh_addr4 *addr4;
1568 struct netlbl_unlhsh_addr6 *addr6;
1569 struct netlbl_unlhsh_iface *iface;
1572 iface = netlbl_unlhsh_search_iface_def(skb->iif);
1574 goto unlabel_getattr_nolabel;
1578 addr4 = netlbl_unlhsh_search_addr4(hdr4->saddr, iface);
1580 goto unlabel_getattr_nolabel;
1581 secattr->attr.secid = addr4->secid;
1583 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1585 hdr6 = ipv6_hdr(skb);
1586 addr6 = netlbl_unlhsh_search_addr6(&hdr6->saddr, iface);
1588 goto unlabel_getattr_nolabel;
1589 secattr->attr.secid = addr6->secid;
1593 goto unlabel_getattr_nolabel;
1597 secattr->flags |= NETLBL_SECATTR_SECID;
1598 secattr->type = NETLBL_NLTYPE_UNLABELED;
1601 unlabel_getattr_nolabel:
1603 if (netlabel_unlabel_acceptflg == 0)
1605 secattr->type = NETLBL_NLTYPE_UNLABELED;
1610 * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
1613 * Set the default NetLabel configuration to allow incoming unlabeled packets
1614 * and to send unlabeled network traffic by default.
1617 int netlbl_unlabel_defconf(void)
1620 struct netlbl_dom_map *entry;
1621 struct netlbl_audit audit_info;
1623 /* Only the kernel is allowed to call this function and the only time
1624 * it is called is at bootup before the audit subsystem is reporting
1625 * messages so don't worry to much about these values. */
1626 security_task_getsecid(current, &audit_info.secid);
1627 audit_info.loginuid = 0;
1629 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1632 entry->type = NETLBL_NLTYPE_UNLABELED;
1633 ret_val = netlbl_domhsh_add_default(entry, &audit_info);
1637 netlbl_unlabel_acceptflg_set(1, &audit_info);