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>
46 #include <linux/slab.h>
48 #include <net/netlink.h>
49 #include <net/genetlink.h>
52 #include <net/net_namespace.h>
53 #include <net/netlabel.h>
55 #include <asm/atomic.h>
57 #include "netlabel_user.h"
58 #include "netlabel_addrlist.h"
59 #include "netlabel_domainhash.h"
60 #include "netlabel_unlabeled.h"
61 #include "netlabel_mgmt.h"
63 /* NOTE: at present we always use init's network namespace since we don't
64 * presently support different namespaces even though the majority of
65 * the functions in this file are "namespace safe" */
67 /* The unlabeled connection hash table which we use to map network interfaces
68 * and addresses of unlabeled packets to a user specified secid value for the
69 * LSM. The hash table is used to lookup the network interface entry
70 * (struct netlbl_unlhsh_iface) and then the interface entry is used to
71 * lookup an IP address match from an ordered list. If a network interface
72 * match can not be found in the hash table then the default entry
73 * (netlbl_unlhsh_def) is used. The IP address entry list
74 * (struct netlbl_unlhsh_addr) is ordered such that the entries with a
75 * larger netmask come first.
77 struct netlbl_unlhsh_tbl {
78 struct list_head *tbl;
81 #define netlbl_unlhsh_addr4_entry(iter) \
82 container_of(iter, struct netlbl_unlhsh_addr4, list)
83 struct netlbl_unlhsh_addr4 {
86 struct netlbl_af4list list;
89 #define netlbl_unlhsh_addr6_entry(iter) \
90 container_of(iter, struct netlbl_unlhsh_addr6, list)
91 struct netlbl_unlhsh_addr6 {
94 struct netlbl_af6list 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 #define netlbl_unlhsh_rcu_deref(p) \
119 rcu_dereference_check(p, rcu_read_lock_held() || \
120 lockdep_is_held(&netlbl_unlhsh_lock))
121 static struct netlbl_unlhsh_tbl *netlbl_unlhsh = NULL;
122 static struct netlbl_unlhsh_iface *netlbl_unlhsh_def = NULL;
124 /* Accept unlabeled packets flag */
125 static u8 netlabel_unlabel_acceptflg = 0;
127 /* NetLabel Generic NETLINK unlabeled family */
128 static struct genl_family netlbl_unlabel_gnl_family = {
129 .id = GENL_ID_GENERATE,
131 .name = NETLBL_NLTYPE_UNLABELED_NAME,
132 .version = NETLBL_PROTO_VERSION,
133 .maxattr = NLBL_UNLABEL_A_MAX,
136 /* NetLabel Netlink attribute policy */
137 static const struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = {
138 [NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 },
139 [NLBL_UNLABEL_A_IPV6ADDR] = { .type = NLA_BINARY,
140 .len = sizeof(struct in6_addr) },
141 [NLBL_UNLABEL_A_IPV6MASK] = { .type = NLA_BINARY,
142 .len = sizeof(struct in6_addr) },
143 [NLBL_UNLABEL_A_IPV4ADDR] = { .type = NLA_BINARY,
144 .len = sizeof(struct in_addr) },
145 [NLBL_UNLABEL_A_IPV4MASK] = { .type = NLA_BINARY,
146 .len = sizeof(struct in_addr) },
147 [NLBL_UNLABEL_A_IFACE] = { .type = NLA_NUL_STRING,
148 .len = IFNAMSIZ - 1 },
149 [NLBL_UNLABEL_A_SECCTX] = { .type = NLA_BINARY }
153 * Unlabeled Connection Hash Table Functions
157 * netlbl_unlhsh_free_addr4 - Frees an IPv4 address entry from the hash table
158 * @entry: the entry's RCU field
161 * This function is designed to be used as a callback to the call_rcu()
162 * function so that memory allocated to a hash table address entry can be
166 static void netlbl_unlhsh_free_addr4(struct rcu_head *entry)
168 struct netlbl_unlhsh_addr4 *ptr;
170 ptr = container_of(entry, struct netlbl_unlhsh_addr4, rcu);
174 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
176 * netlbl_unlhsh_free_addr6 - Frees an IPv6 address entry from the hash table
177 * @entry: the entry's RCU field
180 * This function is designed to be used as a callback to the call_rcu()
181 * function so that memory allocated to a hash table address entry can be
185 static void netlbl_unlhsh_free_addr6(struct rcu_head *entry)
187 struct netlbl_unlhsh_addr6 *ptr;
189 ptr = container_of(entry, struct netlbl_unlhsh_addr6, rcu);
195 * netlbl_unlhsh_free_iface - Frees an interface entry from the hash table
196 * @entry: the entry's RCU field
199 * This function is designed to be used as a callback to the call_rcu()
200 * function so that memory allocated to a hash table interface entry can be
201 * released safely. It is important to note that this function does not free
202 * the IPv4 and IPv6 address lists contained as part of an interface entry. It
203 * is up to the rest of the code to make sure an interface entry is only freed
204 * once it's address lists are empty.
207 static void netlbl_unlhsh_free_iface(struct rcu_head *entry)
209 struct netlbl_unlhsh_iface *iface;
210 struct netlbl_af4list *iter4;
211 struct netlbl_af4list *tmp4;
212 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
213 struct netlbl_af6list *iter6;
214 struct netlbl_af6list *tmp6;
217 iface = container_of(entry, struct netlbl_unlhsh_iface, rcu);
219 /* no need for locks here since we are the only one with access to this
222 netlbl_af4list_foreach_safe(iter4, tmp4, &iface->addr4_list) {
223 netlbl_af4list_remove_entry(iter4);
224 kfree(netlbl_unlhsh_addr4_entry(iter4));
226 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
227 netlbl_af6list_foreach_safe(iter6, tmp6, &iface->addr6_list) {
228 netlbl_af6list_remove_entry(iter6);
229 kfree(netlbl_unlhsh_addr6_entry(iter6));
236 * netlbl_unlhsh_hash - Hashing function for the hash table
237 * @ifindex: the network interface/device to hash
240 * This is the hashing function for the unlabeled hash table, it returns the
241 * bucket number for the given device/interface. The caller is responsible for
242 * ensuring that the hash table is protected with either a RCU read lock or
243 * the hash table lock.
246 static u32 netlbl_unlhsh_hash(int ifindex)
248 return ifindex & (netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->size - 1);
252 * netlbl_unlhsh_search_iface - Search for a matching interface entry
253 * @ifindex: the network interface
256 * Searches the unlabeled connection hash table and returns a pointer to the
257 * interface entry which matches @ifindex, otherwise NULL is returned. The
258 * caller is responsible for ensuring that the hash table is protected with
259 * either a RCU read lock or the hash table lock.
262 static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface(int ifindex)
265 struct list_head *bkt_list;
266 struct netlbl_unlhsh_iface *iter;
268 bkt = netlbl_unlhsh_hash(ifindex);
269 bkt_list = &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt];
270 list_for_each_entry_rcu(iter, bkt_list, list)
271 if (iter->valid && iter->ifindex == ifindex)
278 * netlbl_unlhsh_add_addr4 - Add a new IPv4 address entry to the hash table
279 * @iface: the associated interface entry
280 * @addr: IPv4 address in network byte order
281 * @mask: IPv4 address mask in network byte order
282 * @secid: LSM secid value for entry
285 * Add a new address entry into the unlabeled connection hash table using the
286 * interface entry specified by @iface. On success zero is returned, otherwise
287 * a negative value is returned.
290 static int netlbl_unlhsh_add_addr4(struct netlbl_unlhsh_iface *iface,
291 const struct in_addr *addr,
292 const struct in_addr *mask,
296 struct netlbl_unlhsh_addr4 *entry;
298 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
302 entry->list.addr = addr->s_addr & mask->s_addr;
303 entry->list.mask = mask->s_addr;
304 entry->list.valid = 1;
305 entry->secid = secid;
307 spin_lock(&netlbl_unlhsh_lock);
308 ret_val = netlbl_af4list_add(&entry->list, &iface->addr4_list);
309 spin_unlock(&netlbl_unlhsh_lock);
316 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
318 * netlbl_unlhsh_add_addr6 - Add a new IPv6 address entry to the hash table
319 * @iface: the associated interface entry
320 * @addr: IPv6 address in network byte order
321 * @mask: IPv6 address mask in network byte order
322 * @secid: LSM secid value for entry
325 * Add a new address entry into the unlabeled connection hash table using the
326 * interface entry specified by @iface. On success zero is returned, otherwise
327 * a negative value is returned.
330 static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface,
331 const struct in6_addr *addr,
332 const struct in6_addr *mask,
336 struct netlbl_unlhsh_addr6 *entry;
338 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
342 ipv6_addr_copy(&entry->list.addr, addr);
343 entry->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
344 entry->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
345 entry->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
346 entry->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
347 ipv6_addr_copy(&entry->list.mask, mask);
348 entry->list.valid = 1;
349 entry->secid = secid;
351 spin_lock(&netlbl_unlhsh_lock);
352 ret_val = netlbl_af6list_add(&entry->list, &iface->addr6_list);
353 spin_unlock(&netlbl_unlhsh_lock);
362 * netlbl_unlhsh_add_iface - Adds a new interface entry to the hash table
363 * @ifindex: network interface
366 * Add a new, empty, interface entry into the unlabeled connection hash table.
367 * On success a pointer to the new interface entry is returned, on failure NULL
371 static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex)
374 struct netlbl_unlhsh_iface *iface;
376 iface = kzalloc(sizeof(*iface), GFP_ATOMIC);
380 iface->ifindex = ifindex;
381 INIT_LIST_HEAD(&iface->addr4_list);
382 INIT_LIST_HEAD(&iface->addr6_list);
385 spin_lock(&netlbl_unlhsh_lock);
387 bkt = netlbl_unlhsh_hash(ifindex);
388 if (netlbl_unlhsh_search_iface(ifindex) != NULL)
389 goto add_iface_failure;
390 list_add_tail_rcu(&iface->list,
391 &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt]);
393 INIT_LIST_HEAD(&iface->list);
394 if (netlbl_unlhsh_rcu_deref(netlbl_unlhsh_def) != NULL)
395 goto add_iface_failure;
396 rcu_assign_pointer(netlbl_unlhsh_def, iface);
398 spin_unlock(&netlbl_unlhsh_lock);
403 spin_unlock(&netlbl_unlhsh_lock);
409 * netlbl_unlhsh_add - Adds a new entry to the unlabeled connection hash table
410 * @net: network namespace
411 * @dev_name: interface name
412 * @addr: IP address in network byte order
413 * @mask: address mask in network byte order
414 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
415 * @secid: LSM secid value for the entry
416 * @audit_info: NetLabel audit information
419 * Adds a new entry to the unlabeled connection hash table. Returns zero on
420 * success, negative values on failure.
423 int netlbl_unlhsh_add(struct net *net,
424 const char *dev_name,
429 struct netlbl_audit *audit_info)
433 struct net_device *dev;
434 struct netlbl_unlhsh_iface *iface;
435 struct audit_buffer *audit_buf = NULL;
439 if (addr_len != sizeof(struct in_addr) &&
440 addr_len != sizeof(struct in6_addr))
444 if (dev_name != NULL) {
445 dev = dev_get_by_name_rcu(net, dev_name);
448 goto unlhsh_add_return;
450 ifindex = dev->ifindex;
451 iface = netlbl_unlhsh_search_iface(ifindex);
454 iface = rcu_dereference(netlbl_unlhsh_def);
457 iface = netlbl_unlhsh_add_iface(ifindex);
460 goto unlhsh_add_return;
463 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCADD,
466 case sizeof(struct in_addr): {
467 struct in_addr *addr4, *mask4;
469 addr4 = (struct in_addr *)addr;
470 mask4 = (struct in_addr *)mask;
471 ret_val = netlbl_unlhsh_add_addr4(iface, addr4, mask4, secid);
472 if (audit_buf != NULL)
473 netlbl_af4list_audit_addr(audit_buf, 1,
479 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
480 case sizeof(struct in6_addr): {
481 struct in6_addr *addr6, *mask6;
483 addr6 = (struct in6_addr *)addr;
484 mask6 = (struct in6_addr *)mask;
485 ret_val = netlbl_unlhsh_add_addr6(iface, addr6, mask6, secid);
486 if (audit_buf != NULL)
487 netlbl_af6list_audit_addr(audit_buf, 1,
497 atomic_inc(&netlabel_mgmt_protocount);
501 if (audit_buf != NULL) {
502 if (security_secid_to_secctx(secid,
505 audit_log_format(audit_buf, " sec_obj=%s", secctx);
506 security_release_secctx(secctx, secctx_len);
508 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
509 audit_log_end(audit_buf);
515 * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry
516 * @net: network namespace
517 * @iface: interface entry
519 * @mask: IP address mask
520 * @audit_info: NetLabel audit information
523 * Remove an IP address entry from the unlabeled connection hash table.
524 * Returns zero on success, negative values on failure.
527 static int netlbl_unlhsh_remove_addr4(struct net *net,
528 struct netlbl_unlhsh_iface *iface,
529 const struct in_addr *addr,
530 const struct in_addr *mask,
531 struct netlbl_audit *audit_info)
533 struct netlbl_af4list *list_entry;
534 struct netlbl_unlhsh_addr4 *entry;
535 struct audit_buffer *audit_buf;
536 struct net_device *dev;
540 spin_lock(&netlbl_unlhsh_lock);
541 list_entry = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
543 spin_unlock(&netlbl_unlhsh_lock);
544 if (list_entry != NULL)
545 entry = netlbl_unlhsh_addr4_entry(list_entry);
549 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
551 if (audit_buf != NULL) {
552 dev = dev_get_by_index(net, iface->ifindex);
553 netlbl_af4list_audit_addr(audit_buf, 1,
554 (dev != NULL ? dev->name : NULL),
555 addr->s_addr, mask->s_addr);
559 security_secid_to_secctx(entry->secid,
560 &secctx, &secctx_len) == 0) {
561 audit_log_format(audit_buf, " sec_obj=%s", secctx);
562 security_release_secctx(secctx, secctx_len);
564 audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
565 audit_log_end(audit_buf);
571 call_rcu(&entry->rcu, netlbl_unlhsh_free_addr4);
575 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
577 * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry
578 * @net: network namespace
579 * @iface: interface entry
581 * @mask: IP address mask
582 * @audit_info: NetLabel audit information
585 * Remove an IP address entry from the unlabeled connection hash table.
586 * Returns zero on success, negative values on failure.
589 static int netlbl_unlhsh_remove_addr6(struct net *net,
590 struct netlbl_unlhsh_iface *iface,
591 const struct in6_addr *addr,
592 const struct in6_addr *mask,
593 struct netlbl_audit *audit_info)
595 struct netlbl_af6list *list_entry;
596 struct netlbl_unlhsh_addr6 *entry;
597 struct audit_buffer *audit_buf;
598 struct net_device *dev;
602 spin_lock(&netlbl_unlhsh_lock);
603 list_entry = netlbl_af6list_remove(addr, mask, &iface->addr6_list);
604 spin_unlock(&netlbl_unlhsh_lock);
605 if (list_entry != NULL)
606 entry = netlbl_unlhsh_addr6_entry(list_entry);
610 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
612 if (audit_buf != NULL) {
613 dev = dev_get_by_index(net, iface->ifindex);
614 netlbl_af6list_audit_addr(audit_buf, 1,
615 (dev != NULL ? dev->name : NULL),
620 security_secid_to_secctx(entry->secid,
621 &secctx, &secctx_len) == 0) {
622 audit_log_format(audit_buf, " sec_obj=%s", secctx);
623 security_release_secctx(secctx, secctx_len);
625 audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
626 audit_log_end(audit_buf);
632 call_rcu(&entry->rcu, netlbl_unlhsh_free_addr6);
638 * netlbl_unlhsh_condremove_iface - Remove an interface entry
639 * @iface: the interface entry
642 * Remove an interface entry from the unlabeled connection hash table if it is
643 * empty. An interface entry is considered to be empty if there are no
644 * address entries assigned to it.
647 static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface)
649 struct netlbl_af4list *iter4;
650 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
651 struct netlbl_af6list *iter6;
654 spin_lock(&netlbl_unlhsh_lock);
655 netlbl_af4list_foreach_rcu(iter4, &iface->addr4_list)
656 goto unlhsh_condremove_failure;
657 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
658 netlbl_af6list_foreach_rcu(iter6, &iface->addr6_list)
659 goto unlhsh_condremove_failure;
662 if (iface->ifindex > 0)
663 list_del_rcu(&iface->list);
665 rcu_assign_pointer(netlbl_unlhsh_def, NULL);
666 spin_unlock(&netlbl_unlhsh_lock);
668 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
671 unlhsh_condremove_failure:
672 spin_unlock(&netlbl_unlhsh_lock);
676 * netlbl_unlhsh_remove - Remove an entry from the unlabeled hash table
677 * @net: network namespace
678 * @dev_name: interface name
679 * @addr: IP address in network byte order
680 * @mask: address mask in network byte order
681 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
682 * @audit_info: NetLabel audit information
685 * Removes and existing entry from the unlabeled connection hash table.
686 * Returns zero on success, negative values on failure.
689 int netlbl_unlhsh_remove(struct net *net,
690 const char *dev_name,
694 struct netlbl_audit *audit_info)
697 struct net_device *dev;
698 struct netlbl_unlhsh_iface *iface;
700 if (addr_len != sizeof(struct in_addr) &&
701 addr_len != sizeof(struct in6_addr))
705 if (dev_name != NULL) {
706 dev = dev_get_by_name_rcu(net, dev_name);
709 goto unlhsh_remove_return;
711 iface = netlbl_unlhsh_search_iface(dev->ifindex);
713 iface = rcu_dereference(netlbl_unlhsh_def);
716 goto unlhsh_remove_return;
719 case sizeof(struct in_addr):
720 ret_val = netlbl_unlhsh_remove_addr4(net,
724 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
725 case sizeof(struct in6_addr):
726 ret_val = netlbl_unlhsh_remove_addr6(net,
735 netlbl_unlhsh_condremove_iface(iface);
736 atomic_dec(&netlabel_mgmt_protocount);
739 unlhsh_remove_return:
745 * General Helper Functions
749 * netlbl_unlhsh_netdev_handler - Network device notification handler
750 * @this: notifier block
752 * @ptr: the network device (cast to void)
755 * Handle network device events, although at present all we care about is a
756 * network device going away. In the case of a device going away we clear any
757 * related entries from the unlabeled connection hash table.
760 static int netlbl_unlhsh_netdev_handler(struct notifier_block *this,
764 struct net_device *dev = ptr;
765 struct netlbl_unlhsh_iface *iface = NULL;
767 if (!net_eq(dev_net(dev), &init_net))
770 /* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */
771 if (event == NETDEV_DOWN) {
772 spin_lock(&netlbl_unlhsh_lock);
773 iface = netlbl_unlhsh_search_iface(dev->ifindex);
774 if (iface != NULL && iface->valid) {
776 list_del_rcu(&iface->list);
779 spin_unlock(&netlbl_unlhsh_lock);
783 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
789 * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
790 * @value: desired value
791 * @audit_info: NetLabel audit information
794 * Set the value of the unlabeled accept flag to @value.
797 static void netlbl_unlabel_acceptflg_set(u8 value,
798 struct netlbl_audit *audit_info)
800 struct audit_buffer *audit_buf;
803 old_val = netlabel_unlabel_acceptflg;
804 netlabel_unlabel_acceptflg = value;
805 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
807 if (audit_buf != NULL) {
808 audit_log_format(audit_buf,
809 " unlbl_accept=%u old=%u", value, old_val);
810 audit_log_end(audit_buf);
815 * netlbl_unlabel_addrinfo_get - Get the IPv4/6 address information
816 * @info: the Generic NETLINK info block
817 * @addr: the IP address
818 * @mask: the IP address mask
819 * @len: the address length
822 * Examine the Generic NETLINK message and extract the IP address information.
823 * Returns zero on success, negative values on failure.
826 static int netlbl_unlabel_addrinfo_get(struct genl_info *info,
833 if (info->attrs[NLBL_UNLABEL_A_IPV4ADDR]) {
834 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
835 if (addr_len != sizeof(struct in_addr) &&
836 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK]))
839 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
840 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]);
842 } else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) {
843 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
844 if (addr_len != sizeof(struct in6_addr) &&
845 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK]))
848 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
849 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]);
857 * NetLabel Command Handlers
861 * netlbl_unlabel_accept - Handle an ACCEPT message
862 * @skb: the NETLINK buffer
863 * @info: the Generic NETLINK info block
866 * Process a user generated ACCEPT message and set the accept flag accordingly.
867 * Returns zero on success, negative values on failure.
870 static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
873 struct netlbl_audit audit_info;
875 if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
876 value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
877 if (value == 1 || value == 0) {
878 netlbl_netlink_auditinfo(skb, &audit_info);
879 netlbl_unlabel_acceptflg_set(value, &audit_info);
888 * netlbl_unlabel_list - Handle a LIST message
889 * @skb: the NETLINK buffer
890 * @info: the Generic NETLINK info block
893 * Process a user generated LIST message and respond with the current status.
894 * Returns zero on success, negative values on failure.
897 static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
899 int ret_val = -EINVAL;
900 struct sk_buff *ans_skb;
903 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
906 data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
907 0, NLBL_UNLABEL_C_LIST);
913 ret_val = nla_put_u8(ans_skb,
914 NLBL_UNLABEL_A_ACPTFLG,
915 netlabel_unlabel_acceptflg);
919 genlmsg_end(ans_skb, data);
920 return genlmsg_reply(ans_skb, info);
928 * netlbl_unlabel_staticadd - Handle a STATICADD message
929 * @skb: the NETLINK buffer
930 * @info: the Generic NETLINK info block
933 * Process a user generated STATICADD message and add a new unlabeled
934 * connection entry to the hash table. Returns zero on success, negative
938 static int netlbl_unlabel_staticadd(struct sk_buff *skb,
939 struct genl_info *info)
947 struct netlbl_audit audit_info;
949 /* Don't allow users to add both IPv4 and IPv6 addresses for a
950 * single entry. However, allow users to create two entries, one each
951 * for IPv4 and IPv4, with the same LSM security context which should
952 * achieve the same result. */
953 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
954 !info->attrs[NLBL_UNLABEL_A_IFACE] ||
955 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
956 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
957 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
958 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
961 netlbl_netlink_auditinfo(skb, &audit_info);
963 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
966 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
967 ret_val = security_secctx_to_secid(
968 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
969 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
974 return netlbl_unlhsh_add(&init_net,
975 dev_name, addr, mask, addr_len, secid,
980 * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message
981 * @skb: the NETLINK buffer
982 * @info: the Generic NETLINK info block
985 * Process a user generated STATICADDDEF message and add a new default
986 * unlabeled connection entry. Returns zero on success, negative values on
990 static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
991 struct genl_info *info)
998 struct netlbl_audit audit_info;
1000 /* Don't allow users to add both IPv4 and IPv6 addresses for a
1001 * single entry. However, allow users to create two entries, one each
1002 * for IPv4 and IPv6, with the same LSM security context which should
1003 * achieve the same result. */
1004 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
1005 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1006 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1007 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1008 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1011 netlbl_netlink_auditinfo(skb, &audit_info);
1013 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1016 ret_val = security_secctx_to_secid(
1017 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1018 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1023 return netlbl_unlhsh_add(&init_net,
1024 NULL, addr, mask, addr_len, secid,
1029 * netlbl_unlabel_staticremove - Handle a STATICREMOVE message
1030 * @skb: the NETLINK buffer
1031 * @info: the Generic NETLINK info block
1034 * Process a user generated STATICREMOVE message and remove the specified
1035 * unlabeled connection entry. Returns zero on success, negative values on
1039 static int netlbl_unlabel_staticremove(struct sk_buff *skb,
1040 struct genl_info *info)
1047 struct netlbl_audit audit_info;
1049 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1050 * IPv4 and IPv6 in the same entry. */
1051 if (!info->attrs[NLBL_UNLABEL_A_IFACE] ||
1052 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1053 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1054 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1055 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1058 netlbl_netlink_auditinfo(skb, &audit_info);
1060 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1063 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
1065 return netlbl_unlhsh_remove(&init_net,
1066 dev_name, addr, mask, addr_len,
1071 * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message
1072 * @skb: the NETLINK buffer
1073 * @info: the Generic NETLINK info block
1076 * Process a user generated STATICREMOVEDEF message and remove the default
1077 * unlabeled connection entry. Returns zero on success, negative values on
1081 static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
1082 struct genl_info *info)
1088 struct netlbl_audit audit_info;
1090 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1091 * IPv4 and IPv6 in the same entry. */
1092 if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1093 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1094 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1095 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1098 netlbl_netlink_auditinfo(skb, &audit_info);
1100 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1104 return netlbl_unlhsh_remove(&init_net,
1105 NULL, addr, mask, addr_len,
1111 * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF]
1112 * @cmd: command/message
1113 * @iface: the interface entry
1114 * @addr4: the IPv4 address entry
1115 * @addr6: the IPv6 address entry
1116 * @arg: the netlbl_unlhsh_walk_arg structure
1119 * This function is designed to be used to generate a response for a
1120 * STATICLIST or STATICLISTDEF message. When called either @addr4 or @addr6
1121 * can be specified, not both, the other unspecified entry should be set to
1122 * NULL by the caller. Returns the size of the message on success, negative
1123 * values on failure.
1126 static int netlbl_unlabel_staticlist_gen(u32 cmd,
1127 const struct netlbl_unlhsh_iface *iface,
1128 const struct netlbl_unlhsh_addr4 *addr4,
1129 const struct netlbl_unlhsh_addr6 *addr6,
1132 int ret_val = -ENOMEM;
1133 struct netlbl_unlhsh_walk_arg *cb_arg = arg;
1134 struct net_device *dev;
1140 data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
1141 cb_arg->seq, &netlbl_unlabel_gnl_family,
1144 goto list_cb_failure;
1146 if (iface->ifindex > 0) {
1147 dev = dev_get_by_index(&init_net, iface->ifindex);
1150 goto list_cb_failure;
1152 ret_val = nla_put_string(cb_arg->skb,
1153 NLBL_UNLABEL_A_IFACE, dev->name);
1156 goto list_cb_failure;
1160 struct in_addr addr_struct;
1162 addr_struct.s_addr = addr4->list.addr;
1163 ret_val = nla_put(cb_arg->skb,
1164 NLBL_UNLABEL_A_IPV4ADDR,
1165 sizeof(struct in_addr),
1168 goto list_cb_failure;
1170 addr_struct.s_addr = addr4->list.mask;
1171 ret_val = nla_put(cb_arg->skb,
1172 NLBL_UNLABEL_A_IPV4MASK,
1173 sizeof(struct in_addr),
1176 goto list_cb_failure;
1178 secid = addr4->secid;
1180 ret_val = nla_put(cb_arg->skb,
1181 NLBL_UNLABEL_A_IPV6ADDR,
1182 sizeof(struct in6_addr),
1185 goto list_cb_failure;
1187 ret_val = nla_put(cb_arg->skb,
1188 NLBL_UNLABEL_A_IPV6MASK,
1189 sizeof(struct in6_addr),
1192 goto list_cb_failure;
1194 secid = addr6->secid;
1197 ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len);
1199 goto list_cb_failure;
1200 ret_val = nla_put(cb_arg->skb,
1201 NLBL_UNLABEL_A_SECCTX,
1204 security_release_secctx(secctx, secctx_len);
1206 goto list_cb_failure;
1209 return genlmsg_end(cb_arg->skb, data);
1212 genlmsg_cancel(cb_arg->skb, data);
1217 * netlbl_unlabel_staticlist - Handle a STATICLIST message
1218 * @skb: the NETLINK buffer
1219 * @cb: the NETLINK callback
1222 * Process a user generated STATICLIST message and dump the unlabeled
1223 * connection hash table in a form suitable for use in a kernel generated
1224 * STATICLIST message. Returns the length of @skb.
1227 static int netlbl_unlabel_staticlist(struct sk_buff *skb,
1228 struct netlink_callback *cb)
1230 struct netlbl_unlhsh_walk_arg cb_arg;
1231 u32 skip_bkt = cb->args[0];
1232 u32 skip_chain = cb->args[1];
1233 u32 skip_addr4 = cb->args[2];
1234 u32 skip_addr6 = cb->args[3];
1236 u32 iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
1237 struct netlbl_unlhsh_iface *iface;
1238 struct list_head *iter_list;
1239 struct netlbl_af4list *addr4;
1240 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1241 struct netlbl_af6list *addr6;
1246 cb_arg.seq = cb->nlh->nlmsg_seq;
1249 for (iter_bkt = skip_bkt;
1250 iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
1251 iter_bkt++, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0) {
1252 iter_list = &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt];
1253 list_for_each_entry_rcu(iface, iter_list, list) {
1254 if (!iface->valid ||
1255 iter_chain++ < skip_chain)
1257 netlbl_af4list_foreach_rcu(addr4,
1258 &iface->addr4_list) {
1259 if (iter_addr4++ < skip_addr4)
1261 if (netlbl_unlabel_staticlist_gen(
1262 NLBL_UNLABEL_C_STATICLIST,
1264 netlbl_unlhsh_addr4_entry(addr4),
1269 goto unlabel_staticlist_return;
1272 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1273 netlbl_af6list_foreach_rcu(addr6,
1274 &iface->addr6_list) {
1275 if (iter_addr6++ < skip_addr6)
1277 if (netlbl_unlabel_staticlist_gen(
1278 NLBL_UNLABEL_C_STATICLIST,
1281 netlbl_unlhsh_addr6_entry(addr6),
1285 goto unlabel_staticlist_return;
1292 unlabel_staticlist_return:
1294 cb->args[0] = skip_bkt;
1295 cb->args[1] = skip_chain;
1296 cb->args[2] = skip_addr4;
1297 cb->args[3] = skip_addr6;
1302 * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message
1303 * @skb: the NETLINK buffer
1304 * @cb: the NETLINK callback
1307 * Process a user generated STATICLISTDEF message and dump the default
1308 * unlabeled connection entry in a form suitable for use in a kernel generated
1309 * STATICLISTDEF message. Returns the length of @skb.
1312 static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
1313 struct netlink_callback *cb)
1315 struct netlbl_unlhsh_walk_arg cb_arg;
1316 struct netlbl_unlhsh_iface *iface;
1317 u32 skip_addr4 = cb->args[0];
1318 u32 skip_addr6 = cb->args[1];
1320 struct netlbl_af4list *addr4;
1321 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1323 struct netlbl_af6list *addr6;
1328 cb_arg.seq = cb->nlh->nlmsg_seq;
1331 iface = rcu_dereference(netlbl_unlhsh_def);
1332 if (iface == NULL || !iface->valid)
1333 goto unlabel_staticlistdef_return;
1335 netlbl_af4list_foreach_rcu(addr4, &iface->addr4_list) {
1336 if (iter_addr4++ < skip_addr4)
1338 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
1340 netlbl_unlhsh_addr4_entry(addr4),
1344 goto unlabel_staticlistdef_return;
1347 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1348 netlbl_af6list_foreach_rcu(addr6, &iface->addr6_list) {
1349 if (iter_addr6++ < skip_addr6)
1351 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
1354 netlbl_unlhsh_addr6_entry(addr6),
1357 goto unlabel_staticlistdef_return;
1362 unlabel_staticlistdef_return:
1364 cb->args[0] = skip_addr4;
1365 cb->args[1] = skip_addr6;
1370 * NetLabel Generic NETLINK Command Definitions
1373 static struct genl_ops netlbl_unlabel_genl_ops[] = {
1375 .cmd = NLBL_UNLABEL_C_STATICADD,
1376 .flags = GENL_ADMIN_PERM,
1377 .policy = netlbl_unlabel_genl_policy,
1378 .doit = netlbl_unlabel_staticadd,
1382 .cmd = NLBL_UNLABEL_C_STATICREMOVE,
1383 .flags = GENL_ADMIN_PERM,
1384 .policy = netlbl_unlabel_genl_policy,
1385 .doit = netlbl_unlabel_staticremove,
1389 .cmd = NLBL_UNLABEL_C_STATICLIST,
1391 .policy = netlbl_unlabel_genl_policy,
1393 .dumpit = netlbl_unlabel_staticlist,
1396 .cmd = NLBL_UNLABEL_C_STATICADDDEF,
1397 .flags = GENL_ADMIN_PERM,
1398 .policy = netlbl_unlabel_genl_policy,
1399 .doit = netlbl_unlabel_staticadddef,
1403 .cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
1404 .flags = GENL_ADMIN_PERM,
1405 .policy = netlbl_unlabel_genl_policy,
1406 .doit = netlbl_unlabel_staticremovedef,
1410 .cmd = NLBL_UNLABEL_C_STATICLISTDEF,
1412 .policy = netlbl_unlabel_genl_policy,
1414 .dumpit = netlbl_unlabel_staticlistdef,
1417 .cmd = NLBL_UNLABEL_C_ACCEPT,
1418 .flags = GENL_ADMIN_PERM,
1419 .policy = netlbl_unlabel_genl_policy,
1420 .doit = netlbl_unlabel_accept,
1424 .cmd = NLBL_UNLABEL_C_LIST,
1426 .policy = netlbl_unlabel_genl_policy,
1427 .doit = netlbl_unlabel_list,
1433 * NetLabel Generic NETLINK Protocol Functions
1437 * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
1440 * Register the unlabeled packet NetLabel component with the Generic NETLINK
1441 * mechanism. Returns zero on success, negative values on failure.
1444 int __init netlbl_unlabel_genl_init(void)
1446 return genl_register_family_with_ops(&netlbl_unlabel_gnl_family,
1447 netlbl_unlabel_genl_ops, ARRAY_SIZE(netlbl_unlabel_genl_ops));
1451 * NetLabel KAPI Hooks
1454 static struct notifier_block netlbl_unlhsh_netdev_notifier = {
1455 .notifier_call = netlbl_unlhsh_netdev_handler,
1459 * netlbl_unlabel_init - Initialize the unlabeled connection hash table
1460 * @size: the number of bits to use for the hash buckets
1463 * Initializes the unlabeled connection hash table and registers a network
1464 * device notification handler. This function should only be called by the
1465 * NetLabel subsystem itself during initialization. Returns zero on success,
1466 * non-zero values on error.
1469 int __init netlbl_unlabel_init(u32 size)
1472 struct netlbl_unlhsh_tbl *hsh_tbl;
1477 hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
1478 if (hsh_tbl == NULL)
1480 hsh_tbl->size = 1 << size;
1481 hsh_tbl->tbl = kcalloc(hsh_tbl->size,
1482 sizeof(struct list_head),
1484 if (hsh_tbl->tbl == NULL) {
1488 for (iter = 0; iter < hsh_tbl->size; iter++)
1489 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
1492 spin_lock(&netlbl_unlhsh_lock);
1493 rcu_assign_pointer(netlbl_unlhsh, hsh_tbl);
1494 spin_unlock(&netlbl_unlhsh_lock);
1497 register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier);
1503 * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
1505 * @family: protocol family
1506 * @secattr: the security attributes
1509 * Determine the security attributes, if any, for an unlabled packet and return
1510 * them in @secattr. Returns zero on success and negative values on failure.
1513 int netlbl_unlabel_getattr(const struct sk_buff *skb,
1515 struct netlbl_lsm_secattr *secattr)
1517 struct netlbl_unlhsh_iface *iface;
1520 iface = netlbl_unlhsh_search_iface(skb->skb_iif);
1522 iface = rcu_dereference(netlbl_unlhsh_def);
1523 if (iface == NULL || !iface->valid)
1524 goto unlabel_getattr_nolabel;
1528 struct netlbl_af4list *addr4;
1531 addr4 = netlbl_af4list_search(hdr4->saddr,
1532 &iface->addr4_list);
1534 goto unlabel_getattr_nolabel;
1535 secattr->attr.secid = netlbl_unlhsh_addr4_entry(addr4)->secid;
1538 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1540 struct ipv6hdr *hdr6;
1541 struct netlbl_af6list *addr6;
1543 hdr6 = ipv6_hdr(skb);
1544 addr6 = netlbl_af6list_search(&hdr6->saddr,
1545 &iface->addr6_list);
1547 goto unlabel_getattr_nolabel;
1548 secattr->attr.secid = netlbl_unlhsh_addr6_entry(addr6)->secid;
1553 goto unlabel_getattr_nolabel;
1557 secattr->flags |= NETLBL_SECATTR_SECID;
1558 secattr->type = NETLBL_NLTYPE_UNLABELED;
1561 unlabel_getattr_nolabel:
1563 if (netlabel_unlabel_acceptflg == 0)
1565 secattr->type = NETLBL_NLTYPE_UNLABELED;
1570 * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
1573 * Set the default NetLabel configuration to allow incoming unlabeled packets
1574 * and to send unlabeled network traffic by default.
1577 int __init netlbl_unlabel_defconf(void)
1580 struct netlbl_dom_map *entry;
1581 struct netlbl_audit audit_info;
1583 /* Only the kernel is allowed to call this function and the only time
1584 * it is called is at bootup before the audit subsystem is reporting
1585 * messages so don't worry to much about these values. */
1586 security_task_getsecid(current, &audit_info.secid);
1587 audit_info.loginuid = 0;
1588 audit_info.sessionid = 0;
1590 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1593 entry->type = NETLBL_NLTYPE_UNLABELED;
1594 ret_val = netlbl_domhsh_add_default(entry, &audit_info);
1598 netlbl_unlabel_acceptflg_set(1, &audit_info);