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 - 2008
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_addrlist.h"
58 #include "netlabel_domainhash.h"
59 #include "netlabel_unlabeled.h"
60 #include "netlabel_mgmt.h"
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" */
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.
76 struct netlbl_unlhsh_tbl {
77 struct list_head *tbl;
80 #define netlbl_unlhsh_addr4_entry(iter) \
81 container_of(iter, struct netlbl_unlhsh_addr4, list)
82 struct netlbl_unlhsh_addr4 {
85 struct netlbl_af4list list;
88 #define netlbl_unlhsh_addr6_entry(iter) \
89 container_of(iter, struct netlbl_unlhsh_addr6, list)
90 struct netlbl_unlhsh_addr6 {
93 struct netlbl_af6list list;
96 struct netlbl_unlhsh_iface {
98 struct list_head addr4_list;
99 struct list_head addr6_list;
102 struct list_head list;
106 /* Argument struct for netlbl_unlhsh_walk() */
107 struct netlbl_unlhsh_walk_arg {
108 struct netlink_callback *nl_cb;
113 /* Unlabeled connection hash table */
114 /* updates should be so rare that having one spinlock for the entire
115 * hash table should be okay */
116 static DEFINE_SPINLOCK(netlbl_unlhsh_lock);
117 static struct netlbl_unlhsh_tbl *netlbl_unlhsh = NULL;
118 static struct netlbl_unlhsh_iface *netlbl_unlhsh_def = NULL;
120 /* Accept unlabeled packets flag */
121 static u8 netlabel_unlabel_acceptflg = 0;
123 /* NetLabel Generic NETLINK unlabeled family */
124 static struct genl_family netlbl_unlabel_gnl_family = {
125 .id = GENL_ID_GENERATE,
127 .name = NETLBL_NLTYPE_UNLABELED_NAME,
128 .version = NETLBL_PROTO_VERSION,
129 .maxattr = NLBL_UNLABEL_A_MAX,
132 /* NetLabel Netlink attribute policy */
133 static const struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = {
134 [NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 },
135 [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 }
149 * Unlabeled Connection Hash Table Functions
153 * netlbl_unlhsh_free_addr4 - Frees an IPv4 address entry from the hash table
154 * @entry: the entry's RCU field
157 * This function is designed to be used as a callback to the call_rcu()
158 * function so that memory allocated to a hash table address entry can be
162 static void netlbl_unlhsh_free_addr4(struct rcu_head *entry)
164 struct netlbl_unlhsh_addr4 *ptr;
166 ptr = container_of(entry, struct netlbl_unlhsh_addr4, rcu);
170 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
172 * netlbl_unlhsh_free_addr6 - Frees an IPv6 address entry from the hash table
173 * @entry: the entry's RCU field
176 * This function is designed to be used as a callback to the call_rcu()
177 * function so that memory allocated to a hash table address entry can be
181 static void netlbl_unlhsh_free_addr6(struct rcu_head *entry)
183 struct netlbl_unlhsh_addr6 *ptr;
185 ptr = container_of(entry, struct netlbl_unlhsh_addr6, rcu);
191 * netlbl_unlhsh_free_iface - Frees an interface entry from the hash table
192 * @entry: the entry's RCU field
195 * This function is designed to be used as a callback to the call_rcu()
196 * function so that memory allocated to a hash table interface entry can be
197 * released safely. It is important to note that this function does not free
198 * the IPv4 and IPv6 address lists contained as part of an interface entry. It
199 * is up to the rest of the code to make sure an interface entry is only freed
200 * once it's address lists are empty.
203 static void netlbl_unlhsh_free_iface(struct rcu_head *entry)
205 struct netlbl_unlhsh_iface *iface;
206 struct netlbl_af4list *iter4;
207 struct netlbl_af4list *tmp4;
208 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
209 struct netlbl_af6list *iter6;
210 struct netlbl_af6list *tmp6;
213 iface = container_of(entry, struct netlbl_unlhsh_iface, rcu);
215 /* no need for locks here since we are the only one with access to this
218 netlbl_af4list_foreach_safe(iter4, tmp4, &iface->addr4_list) {
219 netlbl_af4list_remove_entry(iter4);
220 kfree(netlbl_unlhsh_addr4_entry(iter4));
222 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
223 netlbl_af6list_foreach_safe(iter6, tmp6, &iface->addr6_list) {
224 netlbl_af6list_remove_entry(iter6);
225 kfree(netlbl_unlhsh_addr6_entry(iter6));
232 * netlbl_unlhsh_hash - Hashing function for the hash table
233 * @ifindex: the network interface/device to hash
236 * This is the hashing function for the unlabeled hash table, it returns the
237 * bucket number for the given device/interface. The caller is responsible for
238 * calling the rcu_read_[un]lock() functions.
241 static u32 netlbl_unlhsh_hash(int ifindex)
243 /* this is taken _almost_ directly from
244 * security/selinux/netif.c:sel_netif_hasfn() as they do pretty much
246 return ifindex & (rcu_dereference(netlbl_unlhsh)->size - 1);
250 * netlbl_unlhsh_search_iface - Search for a matching interface entry
251 * @ifindex: the network interface
254 * Searches the unlabeled connection hash table and returns a pointer to the
255 * interface entry which matches @ifindex, otherwise NULL is returned. The
256 * caller is responsible for calling the rcu_read_[un]lock() functions.
259 static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface(int ifindex)
262 struct list_head *bkt_list;
263 struct netlbl_unlhsh_iface *iter;
265 bkt = netlbl_unlhsh_hash(ifindex);
266 bkt_list = &rcu_dereference(netlbl_unlhsh)->tbl[bkt];
267 list_for_each_entry_rcu(iter, bkt_list, list)
268 if (iter->valid && iter->ifindex == ifindex)
275 * netlbl_unlhsh_search_iface_def - Search for a matching interface entry
276 * @ifindex: the network interface
279 * Searches the unlabeled connection hash table and returns a pointer to the
280 * interface entry which matches @ifindex. If an exact match can not be found
281 * and there is a valid default entry, the default entry is returned, otherwise
282 * NULL is returned. The caller is responsible for calling the
283 * rcu_read_[un]lock() functions.
286 static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface_def(int ifindex)
288 struct netlbl_unlhsh_iface *entry;
290 entry = netlbl_unlhsh_search_iface(ifindex);
294 entry = rcu_dereference(netlbl_unlhsh_def);
295 if (entry != NULL && entry->valid)
302 * netlbl_unlhsh_add_addr4 - Add a new IPv4 address entry to the hash table
303 * @iface: the associated interface entry
304 * @addr: IPv4 address in network byte order
305 * @mask: IPv4 address mask in network byte order
306 * @secid: LSM secid value for entry
309 * Add a new address entry into the unlabeled connection hash table using the
310 * interface entry specified by @iface. On success zero is returned, otherwise
311 * a negative value is returned. The caller is responsible for calling the
312 * rcu_read_[un]lock() functions.
315 static int netlbl_unlhsh_add_addr4(struct netlbl_unlhsh_iface *iface,
316 const struct in_addr *addr,
317 const struct in_addr *mask,
321 struct netlbl_unlhsh_addr4 *entry;
323 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
327 entry->list.addr = addr->s_addr & mask->s_addr;
328 entry->list.mask = mask->s_addr;
329 entry->list.valid = 1;
330 entry->secid = secid;
332 spin_lock(&netlbl_unlhsh_lock);
333 ret_val = netlbl_af4list_add(&entry->list, &iface->addr4_list);
334 spin_unlock(&netlbl_unlhsh_lock);
341 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
343 * netlbl_unlhsh_add_addr6 - Add a new IPv6 address entry to the hash table
344 * @iface: the associated interface entry
345 * @addr: IPv6 address in network byte order
346 * @mask: IPv6 address mask in network byte order
347 * @secid: LSM secid value for entry
350 * Add a new address entry into the unlabeled connection hash table using the
351 * interface entry specified by @iface. On success zero is returned, otherwise
352 * a negative value is returned. The caller is responsible for calling the
353 * rcu_read_[un]lock() functions.
356 static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface,
357 const struct in6_addr *addr,
358 const struct in6_addr *mask,
362 struct netlbl_unlhsh_addr6 *entry;
364 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
368 ipv6_addr_copy(&entry->list.addr, addr);
369 entry->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
370 entry->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
371 entry->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
372 entry->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
373 ipv6_addr_copy(&entry->list.mask, mask);
374 entry->list.valid = 1;
375 entry->secid = secid;
377 spin_lock(&netlbl_unlhsh_lock);
378 ret_val = netlbl_af6list_add(&entry->list, &iface->addr6_list);
379 spin_unlock(&netlbl_unlhsh_lock);
388 * netlbl_unlhsh_add_iface - Adds a new interface entry to the hash table
389 * @ifindex: network interface
392 * Add a new, empty, interface entry into the unlabeled connection hash table.
393 * On success a pointer to the new interface entry is returned, on failure NULL
394 * is returned. The caller is responsible for calling the rcu_read_[un]lock()
398 static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex)
401 struct netlbl_unlhsh_iface *iface;
403 iface = kzalloc(sizeof(*iface), GFP_ATOMIC);
407 iface->ifindex = ifindex;
408 INIT_LIST_HEAD(&iface->addr4_list);
409 INIT_LIST_HEAD(&iface->addr6_list);
412 spin_lock(&netlbl_unlhsh_lock);
414 bkt = netlbl_unlhsh_hash(ifindex);
415 if (netlbl_unlhsh_search_iface(ifindex) != NULL)
416 goto add_iface_failure;
417 list_add_tail_rcu(&iface->list,
418 &rcu_dereference(netlbl_unlhsh)->tbl[bkt]);
420 INIT_LIST_HEAD(&iface->list);
421 if (rcu_dereference(netlbl_unlhsh_def) != NULL)
422 goto add_iface_failure;
423 rcu_assign_pointer(netlbl_unlhsh_def, iface);
425 spin_unlock(&netlbl_unlhsh_lock);
430 spin_unlock(&netlbl_unlhsh_lock);
436 * netlbl_unlhsh_add - Adds a new entry to the unlabeled connection hash table
437 * @net: network namespace
438 * @dev_name: interface name
439 * @addr: IP address in network byte order
440 * @mask: address mask in network byte order
441 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
442 * @secid: LSM secid value for the entry
443 * @audit_info: NetLabel audit information
446 * Adds a new entry to the unlabeled connection hash table. Returns zero on
447 * success, negative values on failure.
450 int netlbl_unlhsh_add(struct net *net,
451 const char *dev_name,
456 struct netlbl_audit *audit_info)
460 struct net_device *dev;
461 struct netlbl_unlhsh_iface *iface;
462 struct audit_buffer *audit_buf = NULL;
466 if (addr_len != sizeof(struct in_addr) &&
467 addr_len != sizeof(struct in6_addr))
471 if (dev_name != NULL) {
472 dev = dev_get_by_name_rcu(net, dev_name);
475 goto unlhsh_add_return;
477 ifindex = dev->ifindex;
478 iface = netlbl_unlhsh_search_iface(ifindex);
481 iface = rcu_dereference(netlbl_unlhsh_def);
484 iface = netlbl_unlhsh_add_iface(ifindex);
487 goto unlhsh_add_return;
490 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCADD,
493 case sizeof(struct in_addr): {
494 struct in_addr *addr4, *mask4;
496 addr4 = (struct in_addr *)addr;
497 mask4 = (struct in_addr *)mask;
498 ret_val = netlbl_unlhsh_add_addr4(iface, addr4, mask4, secid);
499 if (audit_buf != NULL)
500 netlbl_af4list_audit_addr(audit_buf, 1,
506 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
507 case sizeof(struct in6_addr): {
508 struct in6_addr *addr6, *mask6;
510 addr6 = (struct in6_addr *)addr;
511 mask6 = (struct in6_addr *)mask;
512 ret_val = netlbl_unlhsh_add_addr6(iface, addr6, mask6, secid);
513 if (audit_buf != NULL)
514 netlbl_af6list_audit_addr(audit_buf, 1,
524 atomic_inc(&netlabel_mgmt_protocount);
528 if (audit_buf != NULL) {
529 if (security_secid_to_secctx(secid,
532 audit_log_format(audit_buf, " sec_obj=%s", secctx);
533 security_release_secctx(secctx, secctx_len);
535 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
536 audit_log_end(audit_buf);
542 * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry
543 * @net: network namespace
544 * @iface: interface entry
546 * @mask: IP address mask
547 * @audit_info: NetLabel audit information
550 * Remove an IP address entry from the unlabeled connection hash table.
551 * Returns zero on success, negative values on failure. The caller is
552 * responsible for calling the rcu_read_[un]lock() functions.
555 static int netlbl_unlhsh_remove_addr4(struct net *net,
556 struct netlbl_unlhsh_iface *iface,
557 const struct in_addr *addr,
558 const struct in_addr *mask,
559 struct netlbl_audit *audit_info)
561 struct netlbl_af4list *list_entry;
562 struct netlbl_unlhsh_addr4 *entry;
563 struct audit_buffer *audit_buf;
564 struct net_device *dev;
568 spin_lock(&netlbl_unlhsh_lock);
569 list_entry = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
571 spin_unlock(&netlbl_unlhsh_lock);
572 if (list_entry != NULL)
573 entry = netlbl_unlhsh_addr4_entry(list_entry);
577 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
579 if (audit_buf != NULL) {
580 dev = dev_get_by_index(net, iface->ifindex);
581 netlbl_af4list_audit_addr(audit_buf, 1,
582 (dev != NULL ? dev->name : NULL),
583 addr->s_addr, mask->s_addr);
587 security_secid_to_secctx(entry->secid,
588 &secctx, &secctx_len) == 0) {
589 audit_log_format(audit_buf, " sec_obj=%s", secctx);
590 security_release_secctx(secctx, secctx_len);
592 audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
593 audit_log_end(audit_buf);
599 call_rcu(&entry->rcu, netlbl_unlhsh_free_addr4);
603 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
605 * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry
606 * @net: network namespace
607 * @iface: interface entry
609 * @mask: IP address mask
610 * @audit_info: NetLabel audit information
613 * Remove an IP address entry from the unlabeled connection hash table.
614 * Returns zero on success, negative values on failure. The caller is
615 * responsible for calling the rcu_read_[un]lock() functions.
618 static int netlbl_unlhsh_remove_addr6(struct net *net,
619 struct netlbl_unlhsh_iface *iface,
620 const struct in6_addr *addr,
621 const struct in6_addr *mask,
622 struct netlbl_audit *audit_info)
624 struct netlbl_af6list *list_entry;
625 struct netlbl_unlhsh_addr6 *entry;
626 struct audit_buffer *audit_buf;
627 struct net_device *dev;
631 spin_lock(&netlbl_unlhsh_lock);
632 list_entry = netlbl_af6list_remove(addr, mask, &iface->addr6_list);
633 spin_unlock(&netlbl_unlhsh_lock);
634 if (list_entry != NULL)
635 entry = netlbl_unlhsh_addr6_entry(list_entry);
639 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
641 if (audit_buf != NULL) {
642 dev = dev_get_by_index(net, iface->ifindex);
643 netlbl_af6list_audit_addr(audit_buf, 1,
644 (dev != NULL ? dev->name : NULL),
649 security_secid_to_secctx(entry->secid,
650 &secctx, &secctx_len) == 0) {
651 audit_log_format(audit_buf, " sec_obj=%s", secctx);
652 security_release_secctx(secctx, secctx_len);
654 audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
655 audit_log_end(audit_buf);
661 call_rcu(&entry->rcu, netlbl_unlhsh_free_addr6);
667 * netlbl_unlhsh_condremove_iface - Remove an interface entry
668 * @iface: the interface entry
671 * Remove an interface entry from the unlabeled connection hash table if it is
672 * empty. An interface entry is considered to be empty if there are no
673 * address entries assigned to it.
676 static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface)
678 struct netlbl_af4list *iter4;
679 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
680 struct netlbl_af6list *iter6;
683 spin_lock(&netlbl_unlhsh_lock);
684 netlbl_af4list_foreach_rcu(iter4, &iface->addr4_list)
685 goto unlhsh_condremove_failure;
686 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
687 netlbl_af6list_foreach_rcu(iter6, &iface->addr6_list)
688 goto unlhsh_condremove_failure;
691 if (iface->ifindex > 0)
692 list_del_rcu(&iface->list);
694 rcu_assign_pointer(netlbl_unlhsh_def, NULL);
695 spin_unlock(&netlbl_unlhsh_lock);
697 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
700 unlhsh_condremove_failure:
701 spin_unlock(&netlbl_unlhsh_lock);
706 * netlbl_unlhsh_remove - Remove an entry from the unlabeled hash table
707 * @net: network namespace
708 * @dev_name: interface name
709 * @addr: IP address in network byte order
710 * @mask: address mask in network byte order
711 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
712 * @audit_info: NetLabel audit information
715 * Removes and existing entry from the unlabeled connection hash table.
716 * Returns zero on success, negative values on failure.
719 int netlbl_unlhsh_remove(struct net *net,
720 const char *dev_name,
724 struct netlbl_audit *audit_info)
727 struct net_device *dev;
728 struct netlbl_unlhsh_iface *iface;
730 if (addr_len != sizeof(struct in_addr) &&
731 addr_len != sizeof(struct in6_addr))
735 if (dev_name != NULL) {
736 dev = dev_get_by_name_rcu(net, dev_name);
739 goto unlhsh_remove_return;
741 iface = netlbl_unlhsh_search_iface(dev->ifindex);
743 iface = rcu_dereference(netlbl_unlhsh_def);
746 goto unlhsh_remove_return;
749 case sizeof(struct in_addr):
750 ret_val = netlbl_unlhsh_remove_addr4(net,
754 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
755 case sizeof(struct in6_addr):
756 ret_val = netlbl_unlhsh_remove_addr6(net,
765 netlbl_unlhsh_condremove_iface(iface);
766 atomic_dec(&netlabel_mgmt_protocount);
769 unlhsh_remove_return:
775 * General Helper Functions
779 * netlbl_unlhsh_netdev_handler - Network device notification handler
780 * @this: notifier block
782 * @ptr: the network device (cast to void)
785 * Handle network device events, although at present all we care about is a
786 * network device going away. In the case of a device going away we clear any
787 * related entries from the unlabeled connection hash table.
790 static int netlbl_unlhsh_netdev_handler(struct notifier_block *this,
794 struct net_device *dev = ptr;
795 struct netlbl_unlhsh_iface *iface = NULL;
797 if (!net_eq(dev_net(dev), &init_net))
800 /* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */
801 if (event == NETDEV_DOWN) {
802 spin_lock(&netlbl_unlhsh_lock);
803 iface = netlbl_unlhsh_search_iface(dev->ifindex);
804 if (iface != NULL && iface->valid) {
806 list_del_rcu(&iface->list);
809 spin_unlock(&netlbl_unlhsh_lock);
813 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
819 * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
820 * @value: desired value
821 * @audit_info: NetLabel audit information
824 * Set the value of the unlabeled accept flag to @value.
827 static void netlbl_unlabel_acceptflg_set(u8 value,
828 struct netlbl_audit *audit_info)
830 struct audit_buffer *audit_buf;
833 old_val = netlabel_unlabel_acceptflg;
834 netlabel_unlabel_acceptflg = value;
835 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
837 if (audit_buf != NULL) {
838 audit_log_format(audit_buf,
839 " unlbl_accept=%u old=%u", value, old_val);
840 audit_log_end(audit_buf);
845 * netlbl_unlabel_addrinfo_get - Get the IPv4/6 address information
846 * @info: the Generic NETLINK info block
847 * @addr: the IP address
848 * @mask: the IP address mask
849 * @len: the address length
852 * Examine the Generic NETLINK message and extract the IP address information.
853 * Returns zero on success, negative values on failure.
856 static int netlbl_unlabel_addrinfo_get(struct genl_info *info,
863 if (info->attrs[NLBL_UNLABEL_A_IPV4ADDR]) {
864 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
865 if (addr_len != sizeof(struct in_addr) &&
866 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK]))
869 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
870 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]);
872 } else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) {
873 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
874 if (addr_len != sizeof(struct in6_addr) &&
875 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK]))
878 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
879 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]);
887 * NetLabel Command Handlers
891 * netlbl_unlabel_accept - Handle an ACCEPT message
892 * @skb: the NETLINK buffer
893 * @info: the Generic NETLINK info block
896 * Process a user generated ACCEPT message and set the accept flag accordingly.
897 * Returns zero on success, negative values on failure.
900 static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
903 struct netlbl_audit audit_info;
905 if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
906 value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
907 if (value == 1 || value == 0) {
908 netlbl_netlink_auditinfo(skb, &audit_info);
909 netlbl_unlabel_acceptflg_set(value, &audit_info);
918 * netlbl_unlabel_list - Handle a LIST message
919 * @skb: the NETLINK buffer
920 * @info: the Generic NETLINK info block
923 * Process a user generated LIST message and respond with the current status.
924 * Returns zero on success, negative values on failure.
927 static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
929 int ret_val = -EINVAL;
930 struct sk_buff *ans_skb;
933 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
936 data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
937 0, NLBL_UNLABEL_C_LIST);
943 ret_val = nla_put_u8(ans_skb,
944 NLBL_UNLABEL_A_ACPTFLG,
945 netlabel_unlabel_acceptflg);
949 genlmsg_end(ans_skb, data);
950 return genlmsg_reply(ans_skb, info);
958 * netlbl_unlabel_staticadd - Handle a STATICADD message
959 * @skb: the NETLINK buffer
960 * @info: the Generic NETLINK info block
963 * Process a user generated STATICADD message and add a new unlabeled
964 * connection entry to the hash table. Returns zero on success, negative
968 static int netlbl_unlabel_staticadd(struct sk_buff *skb,
969 struct genl_info *info)
977 struct netlbl_audit audit_info;
979 /* Don't allow users to add both IPv4 and IPv6 addresses for a
980 * single entry. However, allow users to create two entries, one each
981 * for IPv4 and IPv4, with the same LSM security context which should
982 * achieve the same result. */
983 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
984 !info->attrs[NLBL_UNLABEL_A_IFACE] ||
985 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
986 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
987 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
988 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
991 netlbl_netlink_auditinfo(skb, &audit_info);
993 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
996 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
997 ret_val = security_secctx_to_secid(
998 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
999 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1004 return netlbl_unlhsh_add(&init_net,
1005 dev_name, addr, mask, addr_len, secid,
1010 * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message
1011 * @skb: the NETLINK buffer
1012 * @info: the Generic NETLINK info block
1015 * Process a user generated STATICADDDEF message and add a new default
1016 * unlabeled connection entry. Returns zero on success, negative values on
1020 static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
1021 struct genl_info *info)
1028 struct netlbl_audit audit_info;
1030 /* Don't allow users to add both IPv4 and IPv6 addresses for a
1031 * single entry. However, allow users to create two entries, one each
1032 * for IPv4 and IPv6, with the same LSM security context which should
1033 * achieve the same result. */
1034 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
1035 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1036 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1037 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1038 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1041 netlbl_netlink_auditinfo(skb, &audit_info);
1043 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1046 ret_val = security_secctx_to_secid(
1047 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1048 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1053 return netlbl_unlhsh_add(&init_net,
1054 NULL, addr, mask, addr_len, secid,
1059 * netlbl_unlabel_staticremove - Handle a STATICREMOVE message
1060 * @skb: the NETLINK buffer
1061 * @info: the Generic NETLINK info block
1064 * Process a user generated STATICREMOVE message and remove the specified
1065 * unlabeled connection entry. Returns zero on success, negative values on
1069 static int netlbl_unlabel_staticremove(struct sk_buff *skb,
1070 struct genl_info *info)
1077 struct netlbl_audit audit_info;
1079 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1080 * IPv4 and IPv6 in the same entry. */
1081 if (!info->attrs[NLBL_UNLABEL_A_IFACE] ||
1082 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1083 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1084 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1085 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1088 netlbl_netlink_auditinfo(skb, &audit_info);
1090 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1093 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
1095 return netlbl_unlhsh_remove(&init_net,
1096 dev_name, addr, mask, addr_len,
1101 * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message
1102 * @skb: the NETLINK buffer
1103 * @info: the Generic NETLINK info block
1106 * Process a user generated STATICREMOVEDEF message and remove the default
1107 * unlabeled connection entry. Returns zero on success, negative values on
1111 static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
1112 struct genl_info *info)
1118 struct netlbl_audit audit_info;
1120 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1121 * IPv4 and IPv6 in the same entry. */
1122 if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1123 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1124 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1125 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1128 netlbl_netlink_auditinfo(skb, &audit_info);
1130 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1134 return netlbl_unlhsh_remove(&init_net,
1135 NULL, addr, mask, addr_len,
1141 * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF]
1142 * @cmd: command/message
1143 * @iface: the interface entry
1144 * @addr4: the IPv4 address entry
1145 * @addr6: the IPv6 address entry
1146 * @arg: the netlbl_unlhsh_walk_arg structure
1149 * This function is designed to be used to generate a response for a
1150 * STATICLIST or STATICLISTDEF message. When called either @addr4 or @addr6
1151 * can be specified, not both, the other unspecified entry should be set to
1152 * NULL by the caller. Returns the size of the message on success, negative
1153 * values on failure.
1156 static int netlbl_unlabel_staticlist_gen(u32 cmd,
1157 const struct netlbl_unlhsh_iface *iface,
1158 const struct netlbl_unlhsh_addr4 *addr4,
1159 const struct netlbl_unlhsh_addr6 *addr6,
1162 int ret_val = -ENOMEM;
1163 struct netlbl_unlhsh_walk_arg *cb_arg = arg;
1164 struct net_device *dev;
1170 data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
1171 cb_arg->seq, &netlbl_unlabel_gnl_family,
1174 goto list_cb_failure;
1176 if (iface->ifindex > 0) {
1177 dev = dev_get_by_index(&init_net, iface->ifindex);
1180 goto list_cb_failure;
1182 ret_val = nla_put_string(cb_arg->skb,
1183 NLBL_UNLABEL_A_IFACE, dev->name);
1186 goto list_cb_failure;
1190 struct in_addr addr_struct;
1192 addr_struct.s_addr = addr4->list.addr;
1193 ret_val = nla_put(cb_arg->skb,
1194 NLBL_UNLABEL_A_IPV4ADDR,
1195 sizeof(struct in_addr),
1198 goto list_cb_failure;
1200 addr_struct.s_addr = addr4->list.mask;
1201 ret_val = nla_put(cb_arg->skb,
1202 NLBL_UNLABEL_A_IPV4MASK,
1203 sizeof(struct in_addr),
1206 goto list_cb_failure;
1208 secid = addr4->secid;
1210 ret_val = nla_put(cb_arg->skb,
1211 NLBL_UNLABEL_A_IPV6ADDR,
1212 sizeof(struct in6_addr),
1215 goto list_cb_failure;
1217 ret_val = nla_put(cb_arg->skb,
1218 NLBL_UNLABEL_A_IPV6MASK,
1219 sizeof(struct in6_addr),
1222 goto list_cb_failure;
1224 secid = addr6->secid;
1227 ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len);
1229 goto list_cb_failure;
1230 ret_val = nla_put(cb_arg->skb,
1231 NLBL_UNLABEL_A_SECCTX,
1234 security_release_secctx(secctx, secctx_len);
1236 goto list_cb_failure;
1239 return genlmsg_end(cb_arg->skb, data);
1242 genlmsg_cancel(cb_arg->skb, data);
1247 * netlbl_unlabel_staticlist - Handle a STATICLIST message
1248 * @skb: the NETLINK buffer
1249 * @cb: the NETLINK callback
1252 * Process a user generated STATICLIST message and dump the unlabeled
1253 * connection hash table in a form suitable for use in a kernel generated
1254 * STATICLIST message. Returns the length of @skb.
1257 static int netlbl_unlabel_staticlist(struct sk_buff *skb,
1258 struct netlink_callback *cb)
1260 struct netlbl_unlhsh_walk_arg cb_arg;
1261 u32 skip_bkt = cb->args[0];
1262 u32 skip_chain = cb->args[1];
1263 u32 skip_addr4 = cb->args[2];
1264 u32 skip_addr6 = cb->args[3];
1266 u32 iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
1267 struct netlbl_unlhsh_iface *iface;
1268 struct list_head *iter_list;
1269 struct netlbl_af4list *addr4;
1270 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1271 struct netlbl_af6list *addr6;
1276 cb_arg.seq = cb->nlh->nlmsg_seq;
1279 for (iter_bkt = skip_bkt;
1280 iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
1281 iter_bkt++, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0) {
1282 iter_list = &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt];
1283 list_for_each_entry_rcu(iface, iter_list, list) {
1284 if (!iface->valid ||
1285 iter_chain++ < skip_chain)
1287 netlbl_af4list_foreach_rcu(addr4,
1288 &iface->addr4_list) {
1289 if (iter_addr4++ < skip_addr4)
1291 if (netlbl_unlabel_staticlist_gen(
1292 NLBL_UNLABEL_C_STATICLIST,
1294 netlbl_unlhsh_addr4_entry(addr4),
1299 goto unlabel_staticlist_return;
1302 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1303 netlbl_af6list_foreach_rcu(addr6,
1304 &iface->addr6_list) {
1305 if (iter_addr6++ < skip_addr6)
1307 if (netlbl_unlabel_staticlist_gen(
1308 NLBL_UNLABEL_C_STATICLIST,
1311 netlbl_unlhsh_addr6_entry(addr6),
1315 goto unlabel_staticlist_return;
1322 unlabel_staticlist_return:
1324 cb->args[0] = skip_bkt;
1325 cb->args[1] = skip_chain;
1326 cb->args[2] = skip_addr4;
1327 cb->args[3] = skip_addr6;
1332 * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message
1333 * @skb: the NETLINK buffer
1334 * @cb: the NETLINK callback
1337 * Process a user generated STATICLISTDEF message and dump the default
1338 * unlabeled connection entry in a form suitable for use in a kernel generated
1339 * STATICLISTDEF message. Returns the length of @skb.
1342 static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
1343 struct netlink_callback *cb)
1345 struct netlbl_unlhsh_walk_arg cb_arg;
1346 struct netlbl_unlhsh_iface *iface;
1347 u32 skip_addr4 = cb->args[0];
1348 u32 skip_addr6 = cb->args[1];
1350 struct netlbl_af4list *addr4;
1351 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1353 struct netlbl_af6list *addr6;
1358 cb_arg.seq = cb->nlh->nlmsg_seq;
1361 iface = rcu_dereference(netlbl_unlhsh_def);
1362 if (iface == NULL || !iface->valid)
1363 goto unlabel_staticlistdef_return;
1365 netlbl_af4list_foreach_rcu(addr4, &iface->addr4_list) {
1366 if (iter_addr4++ < skip_addr4)
1368 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
1370 netlbl_unlhsh_addr4_entry(addr4),
1374 goto unlabel_staticlistdef_return;
1377 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1378 netlbl_af6list_foreach_rcu(addr6, &iface->addr6_list) {
1379 if (iter_addr6++ < skip_addr6)
1381 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
1384 netlbl_unlhsh_addr6_entry(addr6),
1387 goto unlabel_staticlistdef_return;
1392 unlabel_staticlistdef_return:
1394 cb->args[0] = skip_addr4;
1395 cb->args[1] = skip_addr6;
1400 * NetLabel Generic NETLINK Command Definitions
1403 static struct genl_ops netlbl_unlabel_genl_ops[] = {
1405 .cmd = NLBL_UNLABEL_C_STATICADD,
1406 .flags = GENL_ADMIN_PERM,
1407 .policy = netlbl_unlabel_genl_policy,
1408 .doit = netlbl_unlabel_staticadd,
1412 .cmd = NLBL_UNLABEL_C_STATICREMOVE,
1413 .flags = GENL_ADMIN_PERM,
1414 .policy = netlbl_unlabel_genl_policy,
1415 .doit = netlbl_unlabel_staticremove,
1419 .cmd = NLBL_UNLABEL_C_STATICLIST,
1421 .policy = netlbl_unlabel_genl_policy,
1423 .dumpit = netlbl_unlabel_staticlist,
1426 .cmd = NLBL_UNLABEL_C_STATICADDDEF,
1427 .flags = GENL_ADMIN_PERM,
1428 .policy = netlbl_unlabel_genl_policy,
1429 .doit = netlbl_unlabel_staticadddef,
1433 .cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
1434 .flags = GENL_ADMIN_PERM,
1435 .policy = netlbl_unlabel_genl_policy,
1436 .doit = netlbl_unlabel_staticremovedef,
1440 .cmd = NLBL_UNLABEL_C_STATICLISTDEF,
1442 .policy = netlbl_unlabel_genl_policy,
1444 .dumpit = netlbl_unlabel_staticlistdef,
1447 .cmd = NLBL_UNLABEL_C_ACCEPT,
1448 .flags = GENL_ADMIN_PERM,
1449 .policy = netlbl_unlabel_genl_policy,
1450 .doit = netlbl_unlabel_accept,
1454 .cmd = NLBL_UNLABEL_C_LIST,
1456 .policy = netlbl_unlabel_genl_policy,
1457 .doit = netlbl_unlabel_list,
1463 * NetLabel Generic NETLINK Protocol Functions
1467 * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
1470 * Register the unlabeled packet NetLabel component with the Generic NETLINK
1471 * mechanism. Returns zero on success, negative values on failure.
1474 int __init netlbl_unlabel_genl_init(void)
1476 return genl_register_family_with_ops(&netlbl_unlabel_gnl_family,
1477 netlbl_unlabel_genl_ops, ARRAY_SIZE(netlbl_unlabel_genl_ops));
1481 * NetLabel KAPI Hooks
1484 static struct notifier_block netlbl_unlhsh_netdev_notifier = {
1485 .notifier_call = netlbl_unlhsh_netdev_handler,
1489 * netlbl_unlabel_init - Initialize the unlabeled connection hash table
1490 * @size: the number of bits to use for the hash buckets
1493 * Initializes the unlabeled connection hash table and registers a network
1494 * device notification handler. This function should only be called by the
1495 * NetLabel subsystem itself during initialization. Returns zero on success,
1496 * non-zero values on error.
1499 int __init netlbl_unlabel_init(u32 size)
1502 struct netlbl_unlhsh_tbl *hsh_tbl;
1507 hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
1508 if (hsh_tbl == NULL)
1510 hsh_tbl->size = 1 << size;
1511 hsh_tbl->tbl = kcalloc(hsh_tbl->size,
1512 sizeof(struct list_head),
1514 if (hsh_tbl->tbl == NULL) {
1518 for (iter = 0; iter < hsh_tbl->size; iter++)
1519 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
1522 spin_lock(&netlbl_unlhsh_lock);
1523 rcu_assign_pointer(netlbl_unlhsh, hsh_tbl);
1524 spin_unlock(&netlbl_unlhsh_lock);
1527 register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier);
1533 * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
1535 * @family: protocol family
1536 * @secattr: the security attributes
1539 * Determine the security attributes, if any, for an unlabled packet and return
1540 * them in @secattr. Returns zero on success and negative values on failure.
1543 int netlbl_unlabel_getattr(const struct sk_buff *skb,
1545 struct netlbl_lsm_secattr *secattr)
1547 struct netlbl_unlhsh_iface *iface;
1550 iface = netlbl_unlhsh_search_iface_def(skb->skb_iif);
1552 goto unlabel_getattr_nolabel;
1556 struct netlbl_af4list *addr4;
1559 addr4 = netlbl_af4list_search(hdr4->saddr,
1560 &iface->addr4_list);
1562 goto unlabel_getattr_nolabel;
1563 secattr->attr.secid = netlbl_unlhsh_addr4_entry(addr4)->secid;
1566 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1568 struct ipv6hdr *hdr6;
1569 struct netlbl_af6list *addr6;
1571 hdr6 = ipv6_hdr(skb);
1572 addr6 = netlbl_af6list_search(&hdr6->saddr,
1573 &iface->addr6_list);
1575 goto unlabel_getattr_nolabel;
1576 secattr->attr.secid = netlbl_unlhsh_addr6_entry(addr6)->secid;
1581 goto unlabel_getattr_nolabel;
1585 secattr->flags |= NETLBL_SECATTR_SECID;
1586 secattr->type = NETLBL_NLTYPE_UNLABELED;
1589 unlabel_getattr_nolabel:
1591 if (netlabel_unlabel_acceptflg == 0)
1593 secattr->type = NETLBL_NLTYPE_UNLABELED;
1598 * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
1601 * Set the default NetLabel configuration to allow incoming unlabeled packets
1602 * and to send unlabeled network traffic by default.
1605 int __init netlbl_unlabel_defconf(void)
1608 struct netlbl_dom_map *entry;
1609 struct netlbl_audit audit_info;
1611 /* Only the kernel is allowed to call this function and the only time
1612 * it is called is at bootup before the audit subsystem is reporting
1613 * messages so don't worry to much about these values. */
1614 security_task_getsecid(current, &audit_info.secid);
1615 audit_info.loginuid = 0;
1616 audit_info.sessionid = 0;
1618 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1621 entry->type = NETLBL_NLTYPE_UNLABELED;
1622 ret_val = netlbl_domhsh_add_default(entry, &audit_info);
1626 netlbl_unlabel_acceptflg_set(1, &audit_info);