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 * Audit Helper Functions
154 * netlbl_unlabel_audit_addr4 - Audit an IPv4 address
155 * @audit_buf: audit buffer
156 * @dev: network interface
158 * @mask: IP address mask
161 * Write the IPv4 address and address mask, if necessary, to @audit_buf.
164 static void netlbl_unlabel_audit_addr4(struct audit_buffer *audit_buf,
166 __be32 addr, __be32 mask)
168 u32 mask_val = ntohl(mask);
171 audit_log_format(audit_buf, " netif=%s", dev);
172 audit_log_format(audit_buf, " src=" NIPQUAD_FMT, NIPQUAD(addr));
173 if (mask_val != 0xffffffff) {
175 while (mask_val > 0) {
179 audit_log_format(audit_buf, " src_prefixlen=%d", mask_len);
184 * netlbl_unlabel_audit_addr6 - Audit an IPv6 address
185 * @audit_buf: audit buffer
186 * @dev: network interface
188 * @mask: IP address mask
191 * Write the IPv6 address and address mask, if necessary, to @audit_buf.
194 static void netlbl_unlabel_audit_addr6(struct audit_buffer *audit_buf,
196 const struct in6_addr *addr,
197 const struct in6_addr *mask)
200 audit_log_format(audit_buf, " netif=%s", dev);
201 audit_log_format(audit_buf, " src=" NIP6_FMT, NIP6(*addr));
202 if (ntohl(mask->s6_addr32[3]) != 0xffffffff) {
206 while (ntohl(mask->s6_addr32[++iter]) == 0xffffffff)
208 mask_val = ntohl(mask->s6_addr32[iter]);
209 while (mask_val > 0) {
213 audit_log_format(audit_buf, " src_prefixlen=%d", mask_len);
218 * Unlabeled Connection Hash Table Functions
222 * netlbl_unlhsh_free_addr4 - Frees an IPv4 address entry from the hash table
223 * @entry: the entry's RCU field
226 * This function is designed to be used as a callback to the call_rcu()
227 * function so that memory allocated to a hash table address entry can be
231 static void netlbl_unlhsh_free_addr4(struct rcu_head *entry)
233 struct netlbl_unlhsh_addr4 *ptr;
235 ptr = container_of(entry, struct netlbl_unlhsh_addr4, rcu);
239 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
241 * netlbl_unlhsh_free_addr6 - Frees an IPv6 address entry from the hash table
242 * @entry: the entry's RCU field
245 * This function is designed to be used as a callback to the call_rcu()
246 * function so that memory allocated to a hash table address entry can be
250 static void netlbl_unlhsh_free_addr6(struct rcu_head *entry)
252 struct netlbl_unlhsh_addr6 *ptr;
254 ptr = container_of(entry, struct netlbl_unlhsh_addr6, rcu);
260 * netlbl_unlhsh_free_iface - Frees an interface entry from the hash table
261 * @entry: the entry's RCU field
264 * This function is designed to be used as a callback to the call_rcu()
265 * function so that memory allocated to a hash table interface entry can be
266 * released safely. It is important to note that this function does not free
267 * the IPv4 and IPv6 address lists contained as part of an interface entry. It
268 * is up to the rest of the code to make sure an interface entry is only freed
269 * once it's address lists are empty.
272 static void netlbl_unlhsh_free_iface(struct rcu_head *entry)
274 struct netlbl_unlhsh_iface *iface;
275 struct netlbl_unlhsh_addr4 *iter4;
276 struct netlbl_unlhsh_addr4 *tmp4;
277 struct netlbl_unlhsh_addr6 *iter6;
278 struct netlbl_unlhsh_addr6 *tmp6;
280 iface = container_of(entry, struct netlbl_unlhsh_iface, rcu);
282 /* no need for locks here since we are the only one with access to this
285 list_for_each_entry_safe(iter4, tmp4, &iface->addr4_list, list)
287 list_del_rcu(&iter4->list);
290 list_for_each_entry_safe(iter6, tmp6, &iface->addr6_list, list)
292 list_del_rcu(&iter6->list);
299 * netlbl_unlhsh_hash - Hashing function for the hash table
300 * @ifindex: the network interface/device to hash
303 * This is the hashing function for the unlabeled hash table, it returns the
304 * bucket number for the given device/interface. The caller is responsible for
305 * calling the rcu_read_[un]lock() functions.
308 static u32 netlbl_unlhsh_hash(int ifindex)
310 /* this is taken _almost_ directly from
311 * security/selinux/netif.c:sel_netif_hasfn() as they do pretty much
313 return ifindex & (rcu_dereference(netlbl_unlhsh)->size - 1);
317 * netlbl_unlhsh_search_addr4 - Search for a matching IPv4 address entry
318 * @addr: IPv4 address
319 * @iface: the network interface entry
322 * Searches the IPv4 address list of the network interface specified by @iface.
323 * If a matching address entry is found it is returned, otherwise NULL is
324 * returned. The caller is responsible for calling the rcu_read_[un]lock()
328 static struct netlbl_unlhsh_addr4 *netlbl_unlhsh_search_addr4(
330 const struct netlbl_unlhsh_iface *iface)
332 struct netlbl_unlhsh_addr4 *iter;
334 list_for_each_entry_rcu(iter, &iface->addr4_list, list)
335 if (iter->valid && (addr & iter->mask) == iter->addr)
341 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
343 * netlbl_unlhsh_search_addr6 - Search for a matching IPv6 address entry
344 * @addr: IPv6 address
345 * @iface: the network interface entry
348 * Searches the IPv6 address list of the network interface specified by @iface.
349 * If a matching address entry is found it is returned, otherwise NULL is
350 * returned. The caller is responsible for calling the rcu_read_[un]lock()
354 static struct netlbl_unlhsh_addr6 *netlbl_unlhsh_search_addr6(
355 const struct in6_addr *addr,
356 const struct netlbl_unlhsh_iface *iface)
358 struct netlbl_unlhsh_addr6 *iter;
360 list_for_each_entry_rcu(iter, &iface->addr6_list, list)
362 ipv6_masked_addr_cmp(&iter->addr, &iter->mask, addr) == 0)
370 * netlbl_unlhsh_search_iface - Search for a matching interface entry
371 * @ifindex: the network interface
374 * Searches the unlabeled connection hash table and returns a pointer to the
375 * interface entry which matches @ifindex, otherwise NULL is returned. The
376 * caller is responsible for calling the rcu_read_[un]lock() functions.
379 static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface(int ifindex)
382 struct netlbl_unlhsh_iface *iter;
384 bkt = netlbl_unlhsh_hash(ifindex);
385 list_for_each_entry_rcu(iter,
386 &rcu_dereference(netlbl_unlhsh)->tbl[bkt],
388 if (iter->valid && iter->ifindex == ifindex)
395 * netlbl_unlhsh_search_iface_def - Search for a matching interface entry
396 * @ifindex: the network interface
399 * Searches the unlabeled connection hash table and returns a pointer to the
400 * interface entry which matches @ifindex. If an exact match can not be found
401 * and there is a valid default entry, the default entry is returned, otherwise
402 * NULL is returned. The caller is responsible for calling the
403 * rcu_read_[un]lock() functions.
406 static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface_def(int ifindex)
408 struct netlbl_unlhsh_iface *entry;
410 entry = netlbl_unlhsh_search_iface(ifindex);
414 entry = rcu_dereference(netlbl_unlhsh_def);
415 if (entry != NULL && entry->valid)
422 * netlbl_unlhsh_add_addr4 - Add a new IPv4 address entry to the hash table
423 * @iface: the associated interface entry
424 * @addr: IPv4 address in network byte order
425 * @mask: IPv4 address mask in network byte order
426 * @secid: LSM secid value for entry
429 * Add a new address entry into the unlabeled connection hash table using the
430 * interface entry specified by @iface. On success zero is returned, otherwise
431 * a negative value is returned. The caller is responsible for calling the
432 * rcu_read_[un]lock() functions.
435 static int netlbl_unlhsh_add_addr4(struct netlbl_unlhsh_iface *iface,
436 const struct in_addr *addr,
437 const struct in_addr *mask,
440 struct netlbl_unlhsh_addr4 *entry;
441 struct netlbl_unlhsh_addr4 *iter;
443 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
447 entry->addr = addr->s_addr & mask->s_addr;
448 entry->mask = mask->s_addr;
449 entry->secid = secid;
451 INIT_RCU_HEAD(&entry->rcu);
453 spin_lock(&netlbl_unlhsh_lock);
454 iter = netlbl_unlhsh_search_addr4(entry->addr, iface);
456 iter->addr == addr->s_addr && iter->mask == mask->s_addr) {
457 spin_unlock(&netlbl_unlhsh_lock);
461 /* in order to speed up address searches through the list (the common
462 * case) we need to keep the list in order based on the size of the
463 * address mask such that the entry with the widest mask (smallest
464 * numerical value) appears first in the list */
465 list_for_each_entry_rcu(iter, &iface->addr4_list, list)
467 ntohl(entry->mask) > ntohl(iter->mask)) {
468 __list_add_rcu(&entry->list,
471 spin_unlock(&netlbl_unlhsh_lock);
474 list_add_tail_rcu(&entry->list, &iface->addr4_list);
475 spin_unlock(&netlbl_unlhsh_lock);
479 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
481 * netlbl_unlhsh_add_addr6 - Add a new IPv6 address entry to the hash table
482 * @iface: the associated interface entry
483 * @addr: IPv6 address in network byte order
484 * @mask: IPv6 address mask in network byte order
485 * @secid: LSM secid value for entry
488 * Add a new address entry into the unlabeled connection hash table using the
489 * interface entry specified by @iface. On success zero is returned, otherwise
490 * a negative value is returned. The caller is responsible for calling the
491 * rcu_read_[un]lock() functions.
494 static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface,
495 const struct in6_addr *addr,
496 const struct in6_addr *mask,
499 struct netlbl_unlhsh_addr6 *entry;
500 struct netlbl_unlhsh_addr6 *iter;
502 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
506 ipv6_addr_copy(&entry->addr, addr);
507 entry->addr.s6_addr32[0] &= mask->s6_addr32[0];
508 entry->addr.s6_addr32[1] &= mask->s6_addr32[1];
509 entry->addr.s6_addr32[2] &= mask->s6_addr32[2];
510 entry->addr.s6_addr32[3] &= mask->s6_addr32[3];
511 ipv6_addr_copy(&entry->mask, mask);
512 entry->secid = secid;
514 INIT_RCU_HEAD(&entry->rcu);
516 spin_lock(&netlbl_unlhsh_lock);
517 iter = netlbl_unlhsh_search_addr6(&entry->addr, iface);
519 (ipv6_addr_equal(&iter->addr, addr) &&
520 ipv6_addr_equal(&iter->mask, mask))) {
521 spin_unlock(&netlbl_unlhsh_lock);
525 /* in order to speed up address searches through the list (the common
526 * case) we need to keep the list in order based on the size of the
527 * address mask such that the entry with the widest mask (smallest
528 * numerical value) appears first in the list */
529 list_for_each_entry_rcu(iter, &iface->addr6_list, list)
531 ipv6_addr_cmp(&entry->mask, &iter->mask) > 0) {
532 __list_add_rcu(&entry->list,
535 spin_unlock(&netlbl_unlhsh_lock);
538 list_add_tail_rcu(&entry->list, &iface->addr6_list);
539 spin_unlock(&netlbl_unlhsh_lock);
545 * netlbl_unlhsh_add_iface - Adds a new interface entry to the hash table
546 * @ifindex: network interface
549 * Add a new, empty, interface entry into the unlabeled connection hash table.
550 * On success a pointer to the new interface entry is returned, on failure NULL
551 * is returned. The caller is responsible for calling the rcu_read_[un]lock()
555 static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex)
558 struct netlbl_unlhsh_iface *iface;
560 iface = kzalloc(sizeof(*iface), GFP_ATOMIC);
564 iface->ifindex = ifindex;
565 INIT_LIST_HEAD(&iface->addr4_list);
566 INIT_LIST_HEAD(&iface->addr6_list);
568 INIT_RCU_HEAD(&iface->rcu);
570 spin_lock(&netlbl_unlhsh_lock);
572 bkt = netlbl_unlhsh_hash(ifindex);
573 if (netlbl_unlhsh_search_iface(ifindex) != NULL)
574 goto add_iface_failure;
575 list_add_tail_rcu(&iface->list,
576 &rcu_dereference(netlbl_unlhsh)->tbl[bkt]);
578 INIT_LIST_HEAD(&iface->list);
579 if (rcu_dereference(netlbl_unlhsh_def) != NULL)
580 goto add_iface_failure;
581 rcu_assign_pointer(netlbl_unlhsh_def, iface);
583 spin_unlock(&netlbl_unlhsh_lock);
588 spin_unlock(&netlbl_unlhsh_lock);
594 * netlbl_unlhsh_add - Adds a new entry to the unlabeled connection hash table
595 * @net: network namespace
596 * @dev_name: interface name
597 * @addr: IP address in network byte order
598 * @mask: address mask in network byte order
599 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
600 * @secid: LSM secid value for the entry
601 * @audit_info: NetLabel audit information
604 * Adds a new entry to the unlabeled connection hash table. Returns zero on
605 * success, negative values on failure.
608 static int netlbl_unlhsh_add(struct net *net,
609 const char *dev_name,
614 struct netlbl_audit *audit_info)
618 struct net_device *dev;
619 struct netlbl_unlhsh_iface *iface;
620 struct audit_buffer *audit_buf = NULL;
624 if (addr_len != sizeof(struct in_addr) &&
625 addr_len != sizeof(struct in6_addr))
629 if (dev_name != NULL) {
630 dev = dev_get_by_name(net, dev_name);
633 goto unlhsh_add_return;
635 ifindex = dev->ifindex;
637 iface = netlbl_unlhsh_search_iface(ifindex);
640 iface = rcu_dereference(netlbl_unlhsh_def);
643 iface = netlbl_unlhsh_add_iface(ifindex);
646 goto unlhsh_add_return;
649 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCADD,
652 case sizeof(struct in_addr): {
653 struct in_addr *addr4, *mask4;
655 addr4 = (struct in_addr *)addr;
656 mask4 = (struct in_addr *)mask;
657 ret_val = netlbl_unlhsh_add_addr4(iface, addr4, mask4, secid);
658 if (audit_buf != NULL)
659 netlbl_unlabel_audit_addr4(audit_buf,
665 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
666 case sizeof(struct in6_addr): {
667 struct in6_addr *addr6, *mask6;
669 addr6 = (struct in6_addr *)addr;
670 mask6 = (struct in6_addr *)mask;
671 ret_val = netlbl_unlhsh_add_addr6(iface, addr6, mask6, secid);
672 if (audit_buf != NULL)
673 netlbl_unlabel_audit_addr6(audit_buf,
683 atomic_inc(&netlabel_mgmt_protocount);
687 if (audit_buf != NULL) {
688 if (security_secid_to_secctx(secid,
691 audit_log_format(audit_buf, " sec_obj=%s", secctx);
692 security_release_secctx(secctx, secctx_len);
694 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
695 audit_log_end(audit_buf);
701 * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry
702 * @net: network namespace
703 * @iface: interface entry
705 * @mask: IP address mask
706 * @audit_info: NetLabel audit information
709 * Remove an IP address entry from the unlabeled connection hash table.
710 * Returns zero on success, negative values on failure. The caller is
711 * responsible for calling the rcu_read_[un]lock() functions.
714 static int netlbl_unlhsh_remove_addr4(struct net *net,
715 struct netlbl_unlhsh_iface *iface,
716 const struct in_addr *addr,
717 const struct in_addr *mask,
718 struct netlbl_audit *audit_info)
720 int ret_val = -ENOENT;
721 struct netlbl_unlhsh_addr4 *entry;
722 struct audit_buffer *audit_buf = NULL;
723 struct net_device *dev;
727 spin_lock(&netlbl_unlhsh_lock);
728 entry = netlbl_unlhsh_search_addr4(addr->s_addr, iface);
730 entry->addr == addr->s_addr && entry->mask == mask->s_addr) {
732 list_del_rcu(&entry->list);
735 spin_unlock(&netlbl_unlhsh_lock);
737 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
739 if (audit_buf != NULL) {
740 dev = dev_get_by_index(net, iface->ifindex);
741 netlbl_unlabel_audit_addr4(audit_buf,
742 (dev != NULL ? dev->name : NULL),
743 entry->addr, entry->mask);
746 if (security_secid_to_secctx(entry->secid,
749 audit_log_format(audit_buf, " sec_obj=%s", secctx);
750 security_release_secctx(secctx, secctx_len);
752 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
753 audit_log_end(audit_buf);
757 call_rcu(&entry->rcu, netlbl_unlhsh_free_addr4);
761 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
763 * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry
764 * @net: network namespace
765 * @iface: interface entry
767 * @mask: IP address mask
768 * @audit_info: NetLabel audit information
771 * Remove an IP address entry from the unlabeled connection hash table.
772 * Returns zero on success, negative values on failure. The caller is
773 * responsible for calling the rcu_read_[un]lock() functions.
776 static int netlbl_unlhsh_remove_addr6(struct net *net,
777 struct netlbl_unlhsh_iface *iface,
778 const struct in6_addr *addr,
779 const struct in6_addr *mask,
780 struct netlbl_audit *audit_info)
782 int ret_val = -ENOENT;
783 struct netlbl_unlhsh_addr6 *entry;
784 struct audit_buffer *audit_buf = NULL;
785 struct net_device *dev;
789 spin_lock(&netlbl_unlhsh_lock);
790 entry = netlbl_unlhsh_search_addr6(addr, iface);
792 (ipv6_addr_equal(&entry->addr, addr) &&
793 ipv6_addr_equal(&entry->mask, mask))) {
795 list_del_rcu(&entry->list);
798 spin_unlock(&netlbl_unlhsh_lock);
800 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
802 if (audit_buf != NULL) {
803 dev = dev_get_by_index(net, iface->ifindex);
804 netlbl_unlabel_audit_addr6(audit_buf,
805 (dev != NULL ? dev->name : NULL),
809 if (security_secid_to_secctx(entry->secid,
812 audit_log_format(audit_buf, " sec_obj=%s", secctx);
813 security_release_secctx(secctx, secctx_len);
815 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
816 audit_log_end(audit_buf);
820 call_rcu(&entry->rcu, netlbl_unlhsh_free_addr6);
826 * netlbl_unlhsh_condremove_iface - Remove an interface entry
827 * @iface: the interface entry
830 * Remove an interface entry from the unlabeled connection hash table if it is
831 * empty. An interface entry is considered to be empty if there are no
832 * address entries assigned to it.
835 static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface)
837 struct netlbl_unlhsh_addr4 *iter4;
838 struct netlbl_unlhsh_addr6 *iter6;
840 spin_lock(&netlbl_unlhsh_lock);
841 list_for_each_entry_rcu(iter4, &iface->addr4_list, list)
843 goto unlhsh_condremove_failure;
844 list_for_each_entry_rcu(iter6, &iface->addr6_list, list)
846 goto unlhsh_condremove_failure;
848 if (iface->ifindex > 0)
849 list_del_rcu(&iface->list);
851 rcu_assign_pointer(netlbl_unlhsh_def, NULL);
852 spin_unlock(&netlbl_unlhsh_lock);
854 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
857 unlhsh_condremove_failure:
858 spin_unlock(&netlbl_unlhsh_lock);
863 * netlbl_unlhsh_remove - Remove an entry from the unlabeled hash table
864 * @net: network namespace
865 * @dev_name: interface name
866 * @addr: IP address in network byte order
867 * @mask: address mask in network byte order
868 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
869 * @audit_info: NetLabel audit information
872 * Removes and existing entry from the unlabeled connection hash table.
873 * Returns zero on success, negative values on failure.
876 static int netlbl_unlhsh_remove(struct net *net,
877 const char *dev_name,
881 struct netlbl_audit *audit_info)
884 struct net_device *dev;
885 struct netlbl_unlhsh_iface *iface;
887 if (addr_len != sizeof(struct in_addr) &&
888 addr_len != sizeof(struct in6_addr))
892 if (dev_name != NULL) {
893 dev = dev_get_by_name(net, dev_name);
896 goto unlhsh_remove_return;
898 iface = netlbl_unlhsh_search_iface(dev->ifindex);
901 iface = rcu_dereference(netlbl_unlhsh_def);
904 goto unlhsh_remove_return;
907 case sizeof(struct in_addr):
908 ret_val = netlbl_unlhsh_remove_addr4(net,
912 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
913 case sizeof(struct in6_addr):
914 ret_val = netlbl_unlhsh_remove_addr6(net,
923 netlbl_unlhsh_condremove_iface(iface);
924 atomic_dec(&netlabel_mgmt_protocount);
927 unlhsh_remove_return:
933 * General Helper Functions
937 * netlbl_unlhsh_netdev_handler - Network device notification handler
938 * @this: notifier block
940 * @ptr: the network device (cast to void)
943 * Handle network device events, although at present all we care about is a
944 * network device going away. In the case of a device going away we clear any
945 * related entries from the unlabeled connection hash table.
948 static int netlbl_unlhsh_netdev_handler(struct notifier_block *this,
952 struct net_device *dev = ptr;
953 struct netlbl_unlhsh_iface *iface = NULL;
955 if (dev->nd_net != &init_net)
958 /* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */
959 if (event == NETDEV_DOWN) {
960 spin_lock(&netlbl_unlhsh_lock);
961 iface = netlbl_unlhsh_search_iface(dev->ifindex);
962 if (iface != NULL && iface->valid) {
964 list_del_rcu(&iface->list);
967 spin_unlock(&netlbl_unlhsh_lock);
971 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
977 * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
978 * @value: desired value
979 * @audit_info: NetLabel audit information
982 * Set the value of the unlabeled accept flag to @value.
985 static void netlbl_unlabel_acceptflg_set(u8 value,
986 struct netlbl_audit *audit_info)
988 struct audit_buffer *audit_buf;
991 old_val = netlabel_unlabel_acceptflg;
992 netlabel_unlabel_acceptflg = value;
993 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
995 if (audit_buf != NULL) {
996 audit_log_format(audit_buf,
997 " unlbl_accept=%u old=%u", value, old_val);
998 audit_log_end(audit_buf);
1003 * netlbl_unlabel_addrinfo_get - Get the IPv4/6 address information
1004 * @info: the Generic NETLINK info block
1005 * @addr: the IP address
1006 * @mask: the IP address mask
1007 * @len: the address length
1010 * Examine the Generic NETLINK message and extract the IP address information.
1011 * Returns zero on success, negative values on failure.
1014 static int netlbl_unlabel_addrinfo_get(struct genl_info *info,
1021 if (info->attrs[NLBL_UNLABEL_A_IPV4ADDR]) {
1022 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
1023 if (addr_len != sizeof(struct in_addr) &&
1024 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK]))
1027 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
1028 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]);
1030 } else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) {
1031 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
1032 if (addr_len != sizeof(struct in6_addr) &&
1033 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK]))
1036 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
1037 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]);
1045 * NetLabel Command Handlers
1049 * netlbl_unlabel_accept - Handle an ACCEPT message
1050 * @skb: the NETLINK buffer
1051 * @info: the Generic NETLINK info block
1054 * Process a user generated ACCEPT message and set the accept flag accordingly.
1055 * Returns zero on success, negative values on failure.
1058 static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
1061 struct netlbl_audit audit_info;
1063 if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
1064 value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
1065 if (value == 1 || value == 0) {
1066 netlbl_netlink_auditinfo(skb, &audit_info);
1067 netlbl_unlabel_acceptflg_set(value, &audit_info);
1076 * netlbl_unlabel_list - Handle a LIST message
1077 * @skb: the NETLINK buffer
1078 * @info: the Generic NETLINK info block
1081 * Process a user generated LIST message and respond with the current status.
1082 * Returns zero on success, negative values on failure.
1085 static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
1087 int ret_val = -EINVAL;
1088 struct sk_buff *ans_skb;
1091 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1092 if (ans_skb == NULL)
1094 data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
1095 0, NLBL_UNLABEL_C_LIST);
1101 ret_val = nla_put_u8(ans_skb,
1102 NLBL_UNLABEL_A_ACPTFLG,
1103 netlabel_unlabel_acceptflg);
1107 genlmsg_end(ans_skb, data);
1109 ret_val = genlmsg_reply(ans_skb, info);
1120 * netlbl_unlabel_staticadd - Handle a STATICADD message
1121 * @skb: the NETLINK buffer
1122 * @info: the Generic NETLINK info block
1125 * Process a user generated STATICADD message and add a new unlabeled
1126 * connection entry to the hash table. Returns zero on success, negative
1127 * values on failure.
1130 static int netlbl_unlabel_staticadd(struct sk_buff *skb,
1131 struct genl_info *info)
1139 struct netlbl_audit audit_info;
1141 /* Don't allow users to add both IPv4 and IPv6 addresses for a
1142 * single entry. However, allow users to create two entries, one each
1143 * for IPv4 and IPv4, with the same LSM security context which should
1144 * achieve the same result. */
1145 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
1146 !info->attrs[NLBL_UNLABEL_A_IFACE] ||
1147 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1148 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1149 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1150 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1153 netlbl_netlink_auditinfo(skb, &audit_info);
1155 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1158 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
1159 ret_val = security_secctx_to_secid(
1160 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1161 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1166 return netlbl_unlhsh_add(&init_net,
1167 dev_name, addr, mask, addr_len, secid,
1172 * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message
1173 * @skb: the NETLINK buffer
1174 * @info: the Generic NETLINK info block
1177 * Process a user generated STATICADDDEF message and add a new default
1178 * unlabeled connection entry. Returns zero on success, negative values on
1182 static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
1183 struct genl_info *info)
1190 struct netlbl_audit audit_info;
1192 /* Don't allow users to add both IPv4 and IPv6 addresses for a
1193 * single entry. However, allow users to create two entries, one each
1194 * for IPv4 and IPv6, with the same LSM security context which should
1195 * achieve the same result. */
1196 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
1197 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1198 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1199 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1200 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1203 netlbl_netlink_auditinfo(skb, &audit_info);
1205 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1208 ret_val = security_secctx_to_secid(
1209 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1210 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1215 return netlbl_unlhsh_add(&init_net,
1216 NULL, addr, mask, addr_len, secid,
1221 * netlbl_unlabel_staticremove - Handle a STATICREMOVE message
1222 * @skb: the NETLINK buffer
1223 * @info: the Generic NETLINK info block
1226 * Process a user generated STATICREMOVE message and remove the specified
1227 * unlabeled connection entry. Returns zero on success, negative values on
1231 static int netlbl_unlabel_staticremove(struct sk_buff *skb,
1232 struct genl_info *info)
1239 struct netlbl_audit audit_info;
1241 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1242 * IPv4 and IPv6 in the same entry. */
1243 if (!info->attrs[NLBL_UNLABEL_A_IFACE] ||
1244 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1245 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1246 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1247 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1250 netlbl_netlink_auditinfo(skb, &audit_info);
1252 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1255 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
1257 return netlbl_unlhsh_remove(&init_net,
1258 dev_name, addr, mask, addr_len,
1263 * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message
1264 * @skb: the NETLINK buffer
1265 * @info: the Generic NETLINK info block
1268 * Process a user generated STATICREMOVEDEF message and remove the default
1269 * unlabeled connection entry. Returns zero on success, negative values on
1273 static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
1274 struct genl_info *info)
1280 struct netlbl_audit audit_info;
1282 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1283 * IPv4 and IPv6 in the same entry. */
1284 if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1285 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1286 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1287 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1290 netlbl_netlink_auditinfo(skb, &audit_info);
1292 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1296 return netlbl_unlhsh_remove(&init_net,
1297 NULL, addr, mask, addr_len,
1303 * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF]
1304 * @cmd: command/message
1305 * @iface: the interface entry
1306 * @addr4: the IPv4 address entry
1307 * @addr6: the IPv6 address entry
1308 * @arg: the netlbl_unlhsh_walk_arg structure
1311 * This function is designed to be used to generate a response for a
1312 * STATICLIST or STATICLISTDEF message. When called either @addr4 or @addr6
1313 * can be specified, not both, the other unspecified entry should be set to
1314 * NULL by the caller. Returns the size of the message on success, negative
1315 * values on failure.
1318 static int netlbl_unlabel_staticlist_gen(u32 cmd,
1319 const struct netlbl_unlhsh_iface *iface,
1320 const struct netlbl_unlhsh_addr4 *addr4,
1321 const struct netlbl_unlhsh_addr6 *addr6,
1324 int ret_val = -ENOMEM;
1325 struct netlbl_unlhsh_walk_arg *cb_arg = arg;
1326 struct net_device *dev;
1332 data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
1333 cb_arg->seq, &netlbl_unlabel_gnl_family,
1336 goto list_cb_failure;
1338 if (iface->ifindex > 0) {
1339 dev = dev_get_by_index(&init_net, iface->ifindex);
1340 ret_val = nla_put_string(cb_arg->skb,
1341 NLBL_UNLABEL_A_IFACE, dev->name);
1344 goto list_cb_failure;
1348 struct in_addr addr_struct;
1350 addr_struct.s_addr = addr4->addr;
1351 ret_val = nla_put(cb_arg->skb,
1352 NLBL_UNLABEL_A_IPV4ADDR,
1353 sizeof(struct in_addr),
1356 goto list_cb_failure;
1358 addr_struct.s_addr = addr4->mask;
1359 ret_val = nla_put(cb_arg->skb,
1360 NLBL_UNLABEL_A_IPV4MASK,
1361 sizeof(struct in_addr),
1364 goto list_cb_failure;
1366 secid = addr4->secid;
1368 ret_val = nla_put(cb_arg->skb,
1369 NLBL_UNLABEL_A_IPV6ADDR,
1370 sizeof(struct in6_addr),
1373 goto list_cb_failure;
1375 ret_val = nla_put(cb_arg->skb,
1376 NLBL_UNLABEL_A_IPV6MASK,
1377 sizeof(struct in6_addr),
1380 goto list_cb_failure;
1382 secid = addr6->secid;
1385 ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len);
1387 goto list_cb_failure;
1388 ret_val = nla_put(cb_arg->skb,
1389 NLBL_UNLABEL_A_SECCTX,
1392 security_release_secctx(secctx, secctx_len);
1394 goto list_cb_failure;
1397 return genlmsg_end(cb_arg->skb, data);
1400 genlmsg_cancel(cb_arg->skb, data);
1405 * netlbl_unlabel_staticlist - Handle a STATICLIST message
1406 * @skb: the NETLINK buffer
1407 * @cb: the NETLINK callback
1410 * Process a user generated STATICLIST message and dump the unlabeled
1411 * connection hash table in a form suitable for use in a kernel generated
1412 * STATICLIST message. Returns the length of @skb.
1415 static int netlbl_unlabel_staticlist(struct sk_buff *skb,
1416 struct netlink_callback *cb)
1418 struct netlbl_unlhsh_walk_arg cb_arg;
1419 u32 skip_bkt = cb->args[0];
1420 u32 skip_chain = cb->args[1];
1421 u32 skip_addr4 = cb->args[2];
1422 u32 skip_addr6 = cb->args[3];
1424 u32 iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
1425 struct netlbl_unlhsh_iface *iface;
1426 struct netlbl_unlhsh_addr4 *addr4;
1427 struct netlbl_unlhsh_addr6 *addr6;
1431 cb_arg.seq = cb->nlh->nlmsg_seq;
1434 for (iter_bkt = skip_bkt;
1435 iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
1436 iter_bkt++, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0) {
1437 list_for_each_entry_rcu(iface,
1438 &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt],
1440 if (!iface->valid ||
1441 iter_chain++ < skip_chain)
1443 list_for_each_entry_rcu(addr4,
1446 if (!addr4->valid || iter_addr4++ < skip_addr4)
1448 if (netlbl_unlabel_staticlist_gen(
1449 NLBL_UNLABEL_C_STATICLIST,
1456 goto unlabel_staticlist_return;
1459 list_for_each_entry_rcu(addr6,
1462 if (!addr6->valid || iter_addr6++ < skip_addr6)
1464 if (netlbl_unlabel_staticlist_gen(
1465 NLBL_UNLABEL_C_STATICLIST,
1472 goto unlabel_staticlist_return;
1478 unlabel_staticlist_return:
1480 cb->args[0] = skip_bkt;
1481 cb->args[1] = skip_chain;
1482 cb->args[2] = skip_addr4;
1483 cb->args[3] = skip_addr6;
1488 * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message
1489 * @skb: the NETLINK buffer
1490 * @cb: the NETLINK callback
1493 * Process a user generated STATICLISTDEF message and dump the default
1494 * unlabeled connection entry in a form suitable for use in a kernel generated
1495 * STATICLISTDEF message. Returns the length of @skb.
1498 static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
1499 struct netlink_callback *cb)
1501 struct netlbl_unlhsh_walk_arg cb_arg;
1502 struct netlbl_unlhsh_iface *iface;
1503 u32 skip_addr4 = cb->args[0];
1504 u32 skip_addr6 = cb->args[1];
1505 u32 iter_addr4 = 0, iter_addr6 = 0;
1506 struct netlbl_unlhsh_addr4 *addr4;
1507 struct netlbl_unlhsh_addr6 *addr6;
1511 cb_arg.seq = cb->nlh->nlmsg_seq;
1514 iface = rcu_dereference(netlbl_unlhsh_def);
1515 if (iface == NULL || !iface->valid)
1516 goto unlabel_staticlistdef_return;
1518 list_for_each_entry_rcu(addr4, &iface->addr4_list, list) {
1519 if (!addr4->valid || iter_addr4++ < skip_addr4)
1521 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
1527 goto unlabel_staticlistdef_return;
1530 list_for_each_entry_rcu(addr6, &iface->addr6_list, list) {
1531 if (addr6->valid || iter_addr6++ < skip_addr6)
1533 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
1539 goto unlabel_staticlistdef_return;
1543 unlabel_staticlistdef_return:
1545 cb->args[0] = skip_addr4;
1546 cb->args[1] = skip_addr6;
1551 * NetLabel Generic NETLINK Command Definitions
1554 static struct genl_ops netlbl_unlabel_genl_c_staticadd = {
1555 .cmd = NLBL_UNLABEL_C_STATICADD,
1556 .flags = GENL_ADMIN_PERM,
1557 .policy = netlbl_unlabel_genl_policy,
1558 .doit = netlbl_unlabel_staticadd,
1562 static struct genl_ops netlbl_unlabel_genl_c_staticremove = {
1563 .cmd = NLBL_UNLABEL_C_STATICREMOVE,
1564 .flags = GENL_ADMIN_PERM,
1565 .policy = netlbl_unlabel_genl_policy,
1566 .doit = netlbl_unlabel_staticremove,
1570 static struct genl_ops netlbl_unlabel_genl_c_staticlist = {
1571 .cmd = NLBL_UNLABEL_C_STATICLIST,
1573 .policy = netlbl_unlabel_genl_policy,
1575 .dumpit = netlbl_unlabel_staticlist,
1578 static struct genl_ops netlbl_unlabel_genl_c_staticadddef = {
1579 .cmd = NLBL_UNLABEL_C_STATICADDDEF,
1580 .flags = GENL_ADMIN_PERM,
1581 .policy = netlbl_unlabel_genl_policy,
1582 .doit = netlbl_unlabel_staticadddef,
1586 static struct genl_ops netlbl_unlabel_genl_c_staticremovedef = {
1587 .cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
1588 .flags = GENL_ADMIN_PERM,
1589 .policy = netlbl_unlabel_genl_policy,
1590 .doit = netlbl_unlabel_staticremovedef,
1594 static struct genl_ops netlbl_unlabel_genl_c_staticlistdef = {
1595 .cmd = NLBL_UNLABEL_C_STATICLISTDEF,
1597 .policy = netlbl_unlabel_genl_policy,
1599 .dumpit = netlbl_unlabel_staticlistdef,
1602 static struct genl_ops netlbl_unlabel_genl_c_accept = {
1603 .cmd = NLBL_UNLABEL_C_ACCEPT,
1604 .flags = GENL_ADMIN_PERM,
1605 .policy = netlbl_unlabel_genl_policy,
1606 .doit = netlbl_unlabel_accept,
1610 static struct genl_ops netlbl_unlabel_genl_c_list = {
1611 .cmd = NLBL_UNLABEL_C_LIST,
1613 .policy = netlbl_unlabel_genl_policy,
1614 .doit = netlbl_unlabel_list,
1619 * NetLabel Generic NETLINK Protocol Functions
1623 * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
1626 * Register the unlabeled packet NetLabel component with the Generic NETLINK
1627 * mechanism. Returns zero on success, negative values on failure.
1630 int netlbl_unlabel_genl_init(void)
1634 ret_val = genl_register_family(&netlbl_unlabel_gnl_family);
1638 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
1639 &netlbl_unlabel_genl_c_staticadd);
1643 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
1644 &netlbl_unlabel_genl_c_staticremove);
1648 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
1649 &netlbl_unlabel_genl_c_staticlist);
1653 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
1654 &netlbl_unlabel_genl_c_staticadddef);
1658 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
1659 &netlbl_unlabel_genl_c_staticremovedef);
1663 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
1664 &netlbl_unlabel_genl_c_staticlistdef);
1668 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
1669 &netlbl_unlabel_genl_c_accept);
1673 ret_val = genl_register_ops(&netlbl_unlabel_gnl_family,
1674 &netlbl_unlabel_genl_c_list);
1682 * NetLabel KAPI Hooks
1685 static struct notifier_block netlbl_unlhsh_netdev_notifier = {
1686 .notifier_call = netlbl_unlhsh_netdev_handler,
1690 * netlbl_unlabel_init - Initialize the unlabeled connection hash table
1691 * @size: the number of bits to use for the hash buckets
1694 * Initializes the unlabeled connection hash table and registers a network
1695 * device notification handler. This function should only be called by the
1696 * NetLabel subsystem itself during initialization. Returns zero on success,
1697 * non-zero values on error.
1700 int netlbl_unlabel_init(u32 size)
1703 struct netlbl_unlhsh_tbl *hsh_tbl;
1708 hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
1709 if (hsh_tbl == NULL)
1711 hsh_tbl->size = 1 << size;
1712 hsh_tbl->tbl = kcalloc(hsh_tbl->size,
1713 sizeof(struct list_head),
1715 if (hsh_tbl->tbl == NULL) {
1719 for (iter = 0; iter < hsh_tbl->size; iter++)
1720 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
1723 spin_lock(&netlbl_unlhsh_lock);
1724 rcu_assign_pointer(netlbl_unlhsh, hsh_tbl);
1725 spin_unlock(&netlbl_unlhsh_lock);
1728 register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier);
1734 * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
1736 * @family: protocol family
1737 * @secattr: the security attributes
1740 * Determine the security attributes, if any, for an unlabled packet and return
1741 * them in @secattr. Returns zero on success and negative values on failure.
1744 int netlbl_unlabel_getattr(const struct sk_buff *skb,
1746 struct netlbl_lsm_secattr *secattr)
1748 struct netlbl_unlhsh_iface *iface;
1751 iface = netlbl_unlhsh_search_iface_def(skb->iif);
1753 goto unlabel_getattr_nolabel;
1757 struct netlbl_unlhsh_addr4 *addr4;
1760 addr4 = netlbl_unlhsh_search_addr4(hdr4->saddr, iface);
1762 goto unlabel_getattr_nolabel;
1763 secattr->attr.secid = addr4->secid;
1766 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1768 struct ipv6hdr *hdr6;
1769 struct netlbl_unlhsh_addr6 *addr6;
1771 hdr6 = ipv6_hdr(skb);
1772 addr6 = netlbl_unlhsh_search_addr6(&hdr6->saddr, iface);
1774 goto unlabel_getattr_nolabel;
1775 secattr->attr.secid = addr6->secid;
1780 goto unlabel_getattr_nolabel;
1784 secattr->flags |= NETLBL_SECATTR_SECID;
1785 secattr->type = NETLBL_NLTYPE_UNLABELED;
1788 unlabel_getattr_nolabel:
1790 if (netlabel_unlabel_acceptflg == 0)
1792 secattr->type = NETLBL_NLTYPE_UNLABELED;
1797 * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
1800 * Set the default NetLabel configuration to allow incoming unlabeled packets
1801 * and to send unlabeled network traffic by default.
1804 int netlbl_unlabel_defconf(void)
1807 struct netlbl_dom_map *entry;
1808 struct netlbl_audit audit_info;
1810 /* Only the kernel is allowed to call this function and the only time
1811 * it is called is at bootup before the audit subsystem is reporting
1812 * messages so don't worry to much about these values. */
1813 security_task_getsecid(current, &audit_info.secid);
1814 audit_info.loginuid = 0;
1816 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1819 entry->type = NETLBL_NLTYPE_UNLABELED;
1820 ret_val = netlbl_domhsh_add_default(entry, &audit_info);
1824 netlbl_unlabel_acceptflg_set(1, &audit_info);