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 INIT_RCU_HEAD(&entry->rcu);
331 entry->secid = secid;
333 spin_lock(&netlbl_unlhsh_lock);
334 ret_val = netlbl_af4list_add(&entry->list, &iface->addr4_list);
335 spin_unlock(&netlbl_unlhsh_lock);
342 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
344 * netlbl_unlhsh_add_addr6 - Add a new IPv6 address entry to the hash table
345 * @iface: the associated interface entry
346 * @addr: IPv6 address in network byte order
347 * @mask: IPv6 address mask in network byte order
348 * @secid: LSM secid value for entry
351 * Add a new address entry into the unlabeled connection hash table using the
352 * interface entry specified by @iface. On success zero is returned, otherwise
353 * a negative value is returned. The caller is responsible for calling the
354 * rcu_read_[un]lock() functions.
357 static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface,
358 const struct in6_addr *addr,
359 const struct in6_addr *mask,
363 struct netlbl_unlhsh_addr6 *entry;
365 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
369 ipv6_addr_copy(&entry->list.addr, addr);
370 entry->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
371 entry->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
372 entry->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
373 entry->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
374 ipv6_addr_copy(&entry->list.mask, mask);
375 entry->list.valid = 1;
376 INIT_RCU_HEAD(&entry->rcu);
377 entry->secid = secid;
379 spin_lock(&netlbl_unlhsh_lock);
380 ret_val = netlbl_af6list_add(&entry->list, &iface->addr6_list);
381 spin_unlock(&netlbl_unlhsh_lock);
390 * netlbl_unlhsh_add_iface - Adds a new interface entry to the hash table
391 * @ifindex: network interface
394 * Add a new, empty, interface entry into the unlabeled connection hash table.
395 * On success a pointer to the new interface entry is returned, on failure NULL
396 * is returned. The caller is responsible for calling the rcu_read_[un]lock()
400 static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex)
403 struct netlbl_unlhsh_iface *iface;
405 iface = kzalloc(sizeof(*iface), GFP_ATOMIC);
409 iface->ifindex = ifindex;
410 INIT_LIST_HEAD(&iface->addr4_list);
411 INIT_LIST_HEAD(&iface->addr6_list);
413 INIT_RCU_HEAD(&iface->rcu);
415 spin_lock(&netlbl_unlhsh_lock);
417 bkt = netlbl_unlhsh_hash(ifindex);
418 if (netlbl_unlhsh_search_iface(ifindex) != NULL)
419 goto add_iface_failure;
420 list_add_tail_rcu(&iface->list,
421 &rcu_dereference(netlbl_unlhsh)->tbl[bkt]);
423 INIT_LIST_HEAD(&iface->list);
424 if (rcu_dereference(netlbl_unlhsh_def) != NULL)
425 goto add_iface_failure;
426 rcu_assign_pointer(netlbl_unlhsh_def, iface);
428 spin_unlock(&netlbl_unlhsh_lock);
433 spin_unlock(&netlbl_unlhsh_lock);
439 * netlbl_unlhsh_add - Adds a new entry to the unlabeled connection hash table
440 * @net: network namespace
441 * @dev_name: interface name
442 * @addr: IP address in network byte order
443 * @mask: address mask in network byte order
444 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
445 * @secid: LSM secid value for the entry
446 * @audit_info: NetLabel audit information
449 * Adds a new entry to the unlabeled connection hash table. Returns zero on
450 * success, negative values on failure.
453 static int netlbl_unlhsh_add(struct net *net,
454 const char *dev_name,
459 struct netlbl_audit *audit_info)
463 struct net_device *dev;
464 struct netlbl_unlhsh_iface *iface;
465 struct audit_buffer *audit_buf = NULL;
469 if (addr_len != sizeof(struct in_addr) &&
470 addr_len != sizeof(struct in6_addr))
474 if (dev_name != NULL) {
475 dev = dev_get_by_name(net, dev_name);
478 goto unlhsh_add_return;
480 ifindex = dev->ifindex;
482 iface = netlbl_unlhsh_search_iface(ifindex);
485 iface = rcu_dereference(netlbl_unlhsh_def);
488 iface = netlbl_unlhsh_add_iface(ifindex);
491 goto unlhsh_add_return;
494 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCADD,
497 case sizeof(struct in_addr): {
498 struct in_addr *addr4, *mask4;
500 addr4 = (struct in_addr *)addr;
501 mask4 = (struct in_addr *)mask;
502 ret_val = netlbl_unlhsh_add_addr4(iface, addr4, mask4, secid);
503 if (audit_buf != NULL)
504 netlbl_af4list_audit_addr(audit_buf, 1,
510 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
511 case sizeof(struct in6_addr): {
512 struct in6_addr *addr6, *mask6;
514 addr6 = (struct in6_addr *)addr;
515 mask6 = (struct in6_addr *)mask;
516 ret_val = netlbl_unlhsh_add_addr6(iface, addr6, mask6, secid);
517 if (audit_buf != NULL)
518 netlbl_af6list_audit_addr(audit_buf, 1,
528 atomic_inc(&netlabel_mgmt_protocount);
532 if (audit_buf != NULL) {
533 if (security_secid_to_secctx(secid,
536 audit_log_format(audit_buf, " sec_obj=%s", secctx);
537 security_release_secctx(secctx, secctx_len);
539 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
540 audit_log_end(audit_buf);
546 * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry
547 * @net: network namespace
548 * @iface: interface entry
550 * @mask: IP address mask
551 * @audit_info: NetLabel audit information
554 * Remove an IP address entry from the unlabeled connection hash table.
555 * Returns zero on success, negative values on failure. The caller is
556 * responsible for calling the rcu_read_[un]lock() functions.
559 static int netlbl_unlhsh_remove_addr4(struct net *net,
560 struct netlbl_unlhsh_iface *iface,
561 const struct in_addr *addr,
562 const struct in_addr *mask,
563 struct netlbl_audit *audit_info)
566 struct netlbl_af4list *list_entry;
567 struct netlbl_unlhsh_addr4 *entry;
568 struct audit_buffer *audit_buf;
569 struct net_device *dev;
573 spin_lock(&netlbl_unlhsh_lock);
574 list_entry = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
576 spin_unlock(&netlbl_unlhsh_lock);
577 if (list_entry != NULL)
578 entry = netlbl_unlhsh_addr4_entry(list_entry);
582 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
584 if (audit_buf != NULL) {
585 dev = dev_get_by_index(net, iface->ifindex);
586 netlbl_af4list_audit_addr(audit_buf, 1,
587 (dev != NULL ? dev->name : NULL),
588 addr->s_addr, mask->s_addr);
591 if (entry && security_secid_to_secctx(entry->secid,
594 audit_log_format(audit_buf, " sec_obj=%s", secctx);
595 security_release_secctx(secctx, secctx_len);
597 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
598 audit_log_end(audit_buf);
602 call_rcu(&entry->rcu, netlbl_unlhsh_free_addr4);
606 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
608 * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry
609 * @net: network namespace
610 * @iface: interface entry
612 * @mask: IP address mask
613 * @audit_info: NetLabel audit information
616 * Remove an IP address entry from the unlabeled connection hash table.
617 * Returns zero on success, negative values on failure. The caller is
618 * responsible for calling the rcu_read_[un]lock() functions.
621 static int netlbl_unlhsh_remove_addr6(struct net *net,
622 struct netlbl_unlhsh_iface *iface,
623 const struct in6_addr *addr,
624 const struct in6_addr *mask,
625 struct netlbl_audit *audit_info)
628 struct netlbl_af6list *list_entry;
629 struct netlbl_unlhsh_addr6 *entry;
630 struct audit_buffer *audit_buf;
631 struct net_device *dev;
635 spin_lock(&netlbl_unlhsh_lock);
636 list_entry = netlbl_af6list_remove(addr, mask, &iface->addr6_list);
637 spin_unlock(&netlbl_unlhsh_lock);
638 if (list_entry != NULL)
639 entry = netlbl_unlhsh_addr6_entry(list_entry);
643 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
645 if (audit_buf != NULL) {
646 dev = dev_get_by_index(net, iface->ifindex);
647 netlbl_af6list_audit_addr(audit_buf, 1,
648 (dev != NULL ? dev->name : NULL),
652 if (entry && security_secid_to_secctx(entry->secid,
655 audit_log_format(audit_buf, " sec_obj=%s", secctx);
656 security_release_secctx(secctx, secctx_len);
658 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
659 audit_log_end(audit_buf);
663 call_rcu(&entry->rcu, netlbl_unlhsh_free_addr6);
669 * netlbl_unlhsh_condremove_iface - Remove an interface entry
670 * @iface: the interface entry
673 * Remove an interface entry from the unlabeled connection hash table if it is
674 * empty. An interface entry is considered to be empty if there are no
675 * address entries assigned to it.
678 static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface)
680 struct netlbl_af4list *iter4;
681 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
682 struct netlbl_af6list *iter6;
685 spin_lock(&netlbl_unlhsh_lock);
686 netlbl_af4list_foreach_rcu(iter4, &iface->addr4_list)
687 goto unlhsh_condremove_failure;
688 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
689 netlbl_af6list_foreach_rcu(iter6, &iface->addr6_list)
690 goto unlhsh_condremove_failure;
693 if (iface->ifindex > 0)
694 list_del_rcu(&iface->list);
696 rcu_assign_pointer(netlbl_unlhsh_def, NULL);
697 spin_unlock(&netlbl_unlhsh_lock);
699 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
702 unlhsh_condremove_failure:
703 spin_unlock(&netlbl_unlhsh_lock);
708 * netlbl_unlhsh_remove - Remove an entry from the unlabeled hash table
709 * @net: network namespace
710 * @dev_name: interface name
711 * @addr: IP address in network byte order
712 * @mask: address mask in network byte order
713 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
714 * @audit_info: NetLabel audit information
717 * Removes and existing entry from the unlabeled connection hash table.
718 * Returns zero on success, negative values on failure.
721 static int netlbl_unlhsh_remove(struct net *net,
722 const char *dev_name,
726 struct netlbl_audit *audit_info)
729 struct net_device *dev;
730 struct netlbl_unlhsh_iface *iface;
732 if (addr_len != sizeof(struct in_addr) &&
733 addr_len != sizeof(struct in6_addr))
737 if (dev_name != NULL) {
738 dev = dev_get_by_name(net, dev_name);
741 goto unlhsh_remove_return;
743 iface = netlbl_unlhsh_search_iface(dev->ifindex);
746 iface = rcu_dereference(netlbl_unlhsh_def);
749 goto unlhsh_remove_return;
752 case sizeof(struct in_addr):
753 ret_val = netlbl_unlhsh_remove_addr4(net,
757 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
758 case sizeof(struct in6_addr):
759 ret_val = netlbl_unlhsh_remove_addr6(net,
768 netlbl_unlhsh_condremove_iface(iface);
769 atomic_dec(&netlabel_mgmt_protocount);
772 unlhsh_remove_return:
778 * General Helper Functions
782 * netlbl_unlhsh_netdev_handler - Network device notification handler
783 * @this: notifier block
785 * @ptr: the network device (cast to void)
788 * Handle network device events, although at present all we care about is a
789 * network device going away. In the case of a device going away we clear any
790 * related entries from the unlabeled connection hash table.
793 static int netlbl_unlhsh_netdev_handler(struct notifier_block *this,
797 struct net_device *dev = ptr;
798 struct netlbl_unlhsh_iface *iface = NULL;
800 if (!net_eq(dev_net(dev), &init_net))
803 /* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */
804 if (event == NETDEV_DOWN) {
805 spin_lock(&netlbl_unlhsh_lock);
806 iface = netlbl_unlhsh_search_iface(dev->ifindex);
807 if (iface != NULL && iface->valid) {
809 list_del_rcu(&iface->list);
812 spin_unlock(&netlbl_unlhsh_lock);
816 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
822 * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
823 * @value: desired value
824 * @audit_info: NetLabel audit information
827 * Set the value of the unlabeled accept flag to @value.
830 static void netlbl_unlabel_acceptflg_set(u8 value,
831 struct netlbl_audit *audit_info)
833 struct audit_buffer *audit_buf;
836 old_val = netlabel_unlabel_acceptflg;
837 netlabel_unlabel_acceptflg = value;
838 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
840 if (audit_buf != NULL) {
841 audit_log_format(audit_buf,
842 " unlbl_accept=%u old=%u", value, old_val);
843 audit_log_end(audit_buf);
848 * netlbl_unlabel_addrinfo_get - Get the IPv4/6 address information
849 * @info: the Generic NETLINK info block
850 * @addr: the IP address
851 * @mask: the IP address mask
852 * @len: the address length
855 * Examine the Generic NETLINK message and extract the IP address information.
856 * Returns zero on success, negative values on failure.
859 static int netlbl_unlabel_addrinfo_get(struct genl_info *info,
866 if (info->attrs[NLBL_UNLABEL_A_IPV4ADDR]) {
867 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
868 if (addr_len != sizeof(struct in_addr) &&
869 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK]))
872 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
873 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]);
875 } else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) {
876 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
877 if (addr_len != sizeof(struct in6_addr) &&
878 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK]))
881 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
882 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]);
890 * NetLabel Command Handlers
894 * netlbl_unlabel_accept - Handle an ACCEPT message
895 * @skb: the NETLINK buffer
896 * @info: the Generic NETLINK info block
899 * Process a user generated ACCEPT message and set the accept flag accordingly.
900 * Returns zero on success, negative values on failure.
903 static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
906 struct netlbl_audit audit_info;
908 if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
909 value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
910 if (value == 1 || value == 0) {
911 netlbl_netlink_auditinfo(skb, &audit_info);
912 netlbl_unlabel_acceptflg_set(value, &audit_info);
921 * netlbl_unlabel_list - Handle a LIST message
922 * @skb: the NETLINK buffer
923 * @info: the Generic NETLINK info block
926 * Process a user generated LIST message and respond with the current status.
927 * Returns zero on success, negative values on failure.
930 static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
932 int ret_val = -EINVAL;
933 struct sk_buff *ans_skb;
936 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
939 data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
940 0, NLBL_UNLABEL_C_LIST);
946 ret_val = nla_put_u8(ans_skb,
947 NLBL_UNLABEL_A_ACPTFLG,
948 netlabel_unlabel_acceptflg);
952 genlmsg_end(ans_skb, data);
953 return genlmsg_reply(ans_skb, info);
961 * netlbl_unlabel_staticadd - Handle a STATICADD message
962 * @skb: the NETLINK buffer
963 * @info: the Generic NETLINK info block
966 * Process a user generated STATICADD message and add a new unlabeled
967 * connection entry to the hash table. Returns zero on success, negative
971 static int netlbl_unlabel_staticadd(struct sk_buff *skb,
972 struct genl_info *info)
980 struct netlbl_audit audit_info;
982 /* Don't allow users to add both IPv4 and IPv6 addresses for a
983 * single entry. However, allow users to create two entries, one each
984 * for IPv4 and IPv4, with the same LSM security context which should
985 * achieve the same result. */
986 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
987 !info->attrs[NLBL_UNLABEL_A_IFACE] ||
988 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
989 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
990 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
991 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
994 netlbl_netlink_auditinfo(skb, &audit_info);
996 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
999 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
1000 ret_val = security_secctx_to_secid(
1001 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1002 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1007 return netlbl_unlhsh_add(&init_net,
1008 dev_name, addr, mask, addr_len, secid,
1013 * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message
1014 * @skb: the NETLINK buffer
1015 * @info: the Generic NETLINK info block
1018 * Process a user generated STATICADDDEF message and add a new default
1019 * unlabeled connection entry. Returns zero on success, negative values on
1023 static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
1024 struct genl_info *info)
1031 struct netlbl_audit audit_info;
1033 /* Don't allow users to add both IPv4 and IPv6 addresses for a
1034 * single entry. However, allow users to create two entries, one each
1035 * for IPv4 and IPv6, with the same LSM security context which should
1036 * achieve the same result. */
1037 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
1038 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1039 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1040 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1041 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1044 netlbl_netlink_auditinfo(skb, &audit_info);
1046 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1049 ret_val = security_secctx_to_secid(
1050 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1051 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1056 return netlbl_unlhsh_add(&init_net,
1057 NULL, addr, mask, addr_len, secid,
1062 * netlbl_unlabel_staticremove - Handle a STATICREMOVE message
1063 * @skb: the NETLINK buffer
1064 * @info: the Generic NETLINK info block
1067 * Process a user generated STATICREMOVE message and remove the specified
1068 * unlabeled connection entry. Returns zero on success, negative values on
1072 static int netlbl_unlabel_staticremove(struct sk_buff *skb,
1073 struct genl_info *info)
1080 struct netlbl_audit audit_info;
1082 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1083 * IPv4 and IPv6 in the same entry. */
1084 if (!info->attrs[NLBL_UNLABEL_A_IFACE] ||
1085 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1086 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1087 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1088 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1091 netlbl_netlink_auditinfo(skb, &audit_info);
1093 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1096 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
1098 return netlbl_unlhsh_remove(&init_net,
1099 dev_name, addr, mask, addr_len,
1104 * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message
1105 * @skb: the NETLINK buffer
1106 * @info: the Generic NETLINK info block
1109 * Process a user generated STATICREMOVEDEF message and remove the default
1110 * unlabeled connection entry. Returns zero on success, negative values on
1114 static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
1115 struct genl_info *info)
1121 struct netlbl_audit audit_info;
1123 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1124 * IPv4 and IPv6 in the same entry. */
1125 if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1126 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1127 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1128 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1131 netlbl_netlink_auditinfo(skb, &audit_info);
1133 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1137 return netlbl_unlhsh_remove(&init_net,
1138 NULL, addr, mask, addr_len,
1144 * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF]
1145 * @cmd: command/message
1146 * @iface: the interface entry
1147 * @addr4: the IPv4 address entry
1148 * @addr6: the IPv6 address entry
1149 * @arg: the netlbl_unlhsh_walk_arg structure
1152 * This function is designed to be used to generate a response for a
1153 * STATICLIST or STATICLISTDEF message. When called either @addr4 or @addr6
1154 * can be specified, not both, the other unspecified entry should be set to
1155 * NULL by the caller. Returns the size of the message on success, negative
1156 * values on failure.
1159 static int netlbl_unlabel_staticlist_gen(u32 cmd,
1160 const struct netlbl_unlhsh_iface *iface,
1161 const struct netlbl_unlhsh_addr4 *addr4,
1162 const struct netlbl_unlhsh_addr6 *addr6,
1165 int ret_val = -ENOMEM;
1166 struct netlbl_unlhsh_walk_arg *cb_arg = arg;
1167 struct net_device *dev;
1173 data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
1174 cb_arg->seq, &netlbl_unlabel_gnl_family,
1177 goto list_cb_failure;
1179 if (iface->ifindex > 0) {
1180 dev = dev_get_by_index(&init_net, iface->ifindex);
1183 goto list_cb_failure;
1185 ret_val = nla_put_string(cb_arg->skb,
1186 NLBL_UNLABEL_A_IFACE, dev->name);
1189 goto list_cb_failure;
1193 struct in_addr addr_struct;
1195 addr_struct.s_addr = addr4->list.addr;
1196 ret_val = nla_put(cb_arg->skb,
1197 NLBL_UNLABEL_A_IPV4ADDR,
1198 sizeof(struct in_addr),
1201 goto list_cb_failure;
1203 addr_struct.s_addr = addr4->list.mask;
1204 ret_val = nla_put(cb_arg->skb,
1205 NLBL_UNLABEL_A_IPV4MASK,
1206 sizeof(struct in_addr),
1209 goto list_cb_failure;
1211 secid = addr4->secid;
1213 ret_val = nla_put(cb_arg->skb,
1214 NLBL_UNLABEL_A_IPV6ADDR,
1215 sizeof(struct in6_addr),
1218 goto list_cb_failure;
1220 ret_val = nla_put(cb_arg->skb,
1221 NLBL_UNLABEL_A_IPV6MASK,
1222 sizeof(struct in6_addr),
1225 goto list_cb_failure;
1227 secid = addr6->secid;
1230 ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len);
1232 goto list_cb_failure;
1233 ret_val = nla_put(cb_arg->skb,
1234 NLBL_UNLABEL_A_SECCTX,
1237 security_release_secctx(secctx, secctx_len);
1239 goto list_cb_failure;
1242 return genlmsg_end(cb_arg->skb, data);
1245 genlmsg_cancel(cb_arg->skb, data);
1250 * netlbl_unlabel_staticlist - Handle a STATICLIST message
1251 * @skb: the NETLINK buffer
1252 * @cb: the NETLINK callback
1255 * Process a user generated STATICLIST message and dump the unlabeled
1256 * connection hash table in a form suitable for use in a kernel generated
1257 * STATICLIST message. Returns the length of @skb.
1260 static int netlbl_unlabel_staticlist(struct sk_buff *skb,
1261 struct netlink_callback *cb)
1263 struct netlbl_unlhsh_walk_arg cb_arg;
1264 u32 skip_bkt = cb->args[0];
1265 u32 skip_chain = cb->args[1];
1266 u32 skip_addr4 = cb->args[2];
1267 u32 skip_addr6 = cb->args[3];
1269 u32 iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
1270 struct netlbl_unlhsh_iface *iface;
1271 struct list_head *iter_list;
1272 struct netlbl_af4list *addr4;
1273 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1274 struct netlbl_af6list *addr6;
1279 cb_arg.seq = cb->nlh->nlmsg_seq;
1282 for (iter_bkt = skip_bkt;
1283 iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
1284 iter_bkt++, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0) {
1285 iter_list = &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt];
1286 list_for_each_entry_rcu(iface, iter_list, list) {
1287 if (!iface->valid ||
1288 iter_chain++ < skip_chain)
1290 netlbl_af4list_foreach_rcu(addr4,
1291 &iface->addr4_list) {
1292 if (iter_addr4++ < skip_addr4)
1294 if (netlbl_unlabel_staticlist_gen(
1295 NLBL_UNLABEL_C_STATICLIST,
1297 netlbl_unlhsh_addr4_entry(addr4),
1302 goto unlabel_staticlist_return;
1305 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1306 netlbl_af6list_foreach_rcu(addr6,
1307 &iface->addr6_list) {
1308 if (iter_addr6++ < skip_addr6)
1310 if (netlbl_unlabel_staticlist_gen(
1311 NLBL_UNLABEL_C_STATICLIST,
1314 netlbl_unlhsh_addr6_entry(addr6),
1318 goto unlabel_staticlist_return;
1325 unlabel_staticlist_return:
1327 cb->args[0] = skip_bkt;
1328 cb->args[1] = skip_chain;
1329 cb->args[2] = skip_addr4;
1330 cb->args[3] = skip_addr6;
1335 * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message
1336 * @skb: the NETLINK buffer
1337 * @cb: the NETLINK callback
1340 * Process a user generated STATICLISTDEF message and dump the default
1341 * unlabeled connection entry in a form suitable for use in a kernel generated
1342 * STATICLISTDEF message. Returns the length of @skb.
1345 static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
1346 struct netlink_callback *cb)
1348 struct netlbl_unlhsh_walk_arg cb_arg;
1349 struct netlbl_unlhsh_iface *iface;
1350 u32 skip_addr4 = cb->args[0];
1351 u32 skip_addr6 = cb->args[1];
1353 struct netlbl_af4list *addr4;
1354 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1356 struct netlbl_af6list *addr6;
1361 cb_arg.seq = cb->nlh->nlmsg_seq;
1364 iface = rcu_dereference(netlbl_unlhsh_def);
1365 if (iface == NULL || !iface->valid)
1366 goto unlabel_staticlistdef_return;
1368 netlbl_af4list_foreach_rcu(addr4, &iface->addr4_list) {
1369 if (iter_addr4++ < skip_addr4)
1371 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
1373 netlbl_unlhsh_addr4_entry(addr4),
1377 goto unlabel_staticlistdef_return;
1380 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1381 netlbl_af6list_foreach_rcu(addr6, &iface->addr6_list) {
1382 if (iter_addr6++ < skip_addr6)
1384 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
1387 netlbl_unlhsh_addr6_entry(addr6),
1390 goto unlabel_staticlistdef_return;
1395 unlabel_staticlistdef_return:
1397 cb->args[0] = skip_addr4;
1398 cb->args[1] = skip_addr6;
1403 * NetLabel Generic NETLINK Command Definitions
1406 static struct genl_ops netlbl_unlabel_genl_ops[] = {
1408 .cmd = NLBL_UNLABEL_C_STATICADD,
1409 .flags = GENL_ADMIN_PERM,
1410 .policy = netlbl_unlabel_genl_policy,
1411 .doit = netlbl_unlabel_staticadd,
1415 .cmd = NLBL_UNLABEL_C_STATICREMOVE,
1416 .flags = GENL_ADMIN_PERM,
1417 .policy = netlbl_unlabel_genl_policy,
1418 .doit = netlbl_unlabel_staticremove,
1422 .cmd = NLBL_UNLABEL_C_STATICLIST,
1424 .policy = netlbl_unlabel_genl_policy,
1426 .dumpit = netlbl_unlabel_staticlist,
1429 .cmd = NLBL_UNLABEL_C_STATICADDDEF,
1430 .flags = GENL_ADMIN_PERM,
1431 .policy = netlbl_unlabel_genl_policy,
1432 .doit = netlbl_unlabel_staticadddef,
1436 .cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
1437 .flags = GENL_ADMIN_PERM,
1438 .policy = netlbl_unlabel_genl_policy,
1439 .doit = netlbl_unlabel_staticremovedef,
1443 .cmd = NLBL_UNLABEL_C_STATICLISTDEF,
1445 .policy = netlbl_unlabel_genl_policy,
1447 .dumpit = netlbl_unlabel_staticlistdef,
1450 .cmd = NLBL_UNLABEL_C_ACCEPT,
1451 .flags = GENL_ADMIN_PERM,
1452 .policy = netlbl_unlabel_genl_policy,
1453 .doit = netlbl_unlabel_accept,
1457 .cmd = NLBL_UNLABEL_C_LIST,
1459 .policy = netlbl_unlabel_genl_policy,
1460 .doit = netlbl_unlabel_list,
1466 * NetLabel Generic NETLINK Protocol Functions
1470 * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
1473 * Register the unlabeled packet NetLabel component with the Generic NETLINK
1474 * mechanism. Returns zero on success, negative values on failure.
1477 int __init netlbl_unlabel_genl_init(void)
1481 ret_val = genl_register_family(&netlbl_unlabel_gnl_family);
1485 for (i = 0; i < ARRAY_SIZE(netlbl_unlabel_genl_ops); i++) {
1486 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
1487 &netlbl_unlabel_genl_ops[i]);
1496 * NetLabel KAPI Hooks
1499 static struct notifier_block netlbl_unlhsh_netdev_notifier = {
1500 .notifier_call = netlbl_unlhsh_netdev_handler,
1504 * netlbl_unlabel_init - Initialize the unlabeled connection hash table
1505 * @size: the number of bits to use for the hash buckets
1508 * Initializes the unlabeled connection hash table and registers a network
1509 * device notification handler. This function should only be called by the
1510 * NetLabel subsystem itself during initialization. Returns zero on success,
1511 * non-zero values on error.
1514 int __init netlbl_unlabel_init(u32 size)
1517 struct netlbl_unlhsh_tbl *hsh_tbl;
1522 hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
1523 if (hsh_tbl == NULL)
1525 hsh_tbl->size = 1 << size;
1526 hsh_tbl->tbl = kcalloc(hsh_tbl->size,
1527 sizeof(struct list_head),
1529 if (hsh_tbl->tbl == NULL) {
1533 for (iter = 0; iter < hsh_tbl->size; iter++)
1534 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
1537 spin_lock(&netlbl_unlhsh_lock);
1538 rcu_assign_pointer(netlbl_unlhsh, hsh_tbl);
1539 spin_unlock(&netlbl_unlhsh_lock);
1542 register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier);
1548 * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
1550 * @family: protocol family
1551 * @secattr: the security attributes
1554 * Determine the security attributes, if any, for an unlabled packet and return
1555 * them in @secattr. Returns zero on success and negative values on failure.
1558 int netlbl_unlabel_getattr(const struct sk_buff *skb,
1560 struct netlbl_lsm_secattr *secattr)
1562 struct netlbl_unlhsh_iface *iface;
1565 iface = netlbl_unlhsh_search_iface_def(skb->iif);
1567 goto unlabel_getattr_nolabel;
1571 struct netlbl_af4list *addr4;
1574 addr4 = netlbl_af4list_search(hdr4->saddr,
1575 &iface->addr4_list);
1577 goto unlabel_getattr_nolabel;
1578 secattr->attr.secid = netlbl_unlhsh_addr4_entry(addr4)->secid;
1581 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1583 struct ipv6hdr *hdr6;
1584 struct netlbl_af6list *addr6;
1586 hdr6 = ipv6_hdr(skb);
1587 addr6 = netlbl_af6list_search(&hdr6->saddr,
1588 &iface->addr6_list);
1590 goto unlabel_getattr_nolabel;
1591 secattr->attr.secid = netlbl_unlhsh_addr6_entry(addr6)->secid;
1596 goto unlabel_getattr_nolabel;
1600 secattr->flags |= NETLBL_SECATTR_SECID;
1601 secattr->type = NETLBL_NLTYPE_UNLABELED;
1604 unlabel_getattr_nolabel:
1606 if (netlabel_unlabel_acceptflg == 0)
1608 secattr->type = NETLBL_NLTYPE_UNLABELED;
1613 * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
1616 * Set the default NetLabel configuration to allow incoming unlabeled packets
1617 * and to send unlabeled network traffic by default.
1620 int __init netlbl_unlabel_defconf(void)
1623 struct netlbl_dom_map *entry;
1624 struct netlbl_audit audit_info;
1626 /* Only the kernel is allowed to call this function and the only time
1627 * it is called is at bootup before the audit subsystem is reporting
1628 * messages so don't worry to much about these values. */
1629 security_task_getsecid(current, &audit_info.secid);
1630 audit_info.loginuid = 0;
1631 audit_info.sessionid = 0;
1633 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1636 entry->type = NETLBL_NLTYPE_UNLABELED;
1637 ret_val = netlbl_domhsh_add_default(entry, &audit_info);
1641 netlbl_unlabel_acceptflg_set(1, &audit_info);