GNU Linux-libre 4.9.309-gnu1
[releases.git] / net / netlabel / netlabel_unlabeled.c
1 /*
2  * NetLabel Unlabeled Support
3  *
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.
7  *
8  * Author: Paul Moore <paul@paul-moore.com>
9  *
10  */
11
12 /*
13  * (c) Copyright Hewlett-Packard Development Company, L.P., 2006 - 2008
14  *
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.
19  *
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.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program;  if not, see <http://www.gnu.org/licenses/>.
27  *
28  */
29
30 #include <linux/types.h>
31 #include <linux/rcupdate.h>
32 #include <linux/list.h>
33 #include <linux/spinlock.h>
34 #include <linux/socket.h>
35 #include <linux/string.h>
36 #include <linux/skbuff.h>
37 #include <linux/audit.h>
38 #include <linux/in.h>
39 #include <linux/in6.h>
40 #include <linux/ip.h>
41 #include <linux/ipv6.h>
42 #include <linux/notifier.h>
43 #include <linux/netdevice.h>
44 #include <linux/security.h>
45 #include <linux/slab.h>
46 #include <net/sock.h>
47 #include <net/netlink.h>
48 #include <net/genetlink.h>
49 #include <net/ip.h>
50 #include <net/ipv6.h>
51 #include <net/net_namespace.h>
52 #include <net/netlabel.h>
53 #include <asm/bug.h>
54 #include <linux/atomic.h>
55
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"
61
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" */
65
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.
75  */
76 struct netlbl_unlhsh_tbl {
77         struct list_head *tbl;
78         u32 size;
79 };
80 #define netlbl_unlhsh_addr4_entry(iter) \
81         container_of(iter, struct netlbl_unlhsh_addr4, list)
82 struct netlbl_unlhsh_addr4 {
83         u32 secid;
84
85         struct netlbl_af4list list;
86         struct rcu_head rcu;
87 };
88 #define netlbl_unlhsh_addr6_entry(iter) \
89         container_of(iter, struct netlbl_unlhsh_addr6, list)
90 struct netlbl_unlhsh_addr6 {
91         u32 secid;
92
93         struct netlbl_af6list list;
94         struct rcu_head rcu;
95 };
96 struct netlbl_unlhsh_iface {
97         int ifindex;
98         struct list_head addr4_list;
99         struct list_head addr6_list;
100
101         u32 valid;
102         struct list_head list;
103         struct rcu_head rcu;
104 };
105
106 /* Argument struct for netlbl_unlhsh_walk() */
107 struct netlbl_unlhsh_walk_arg {
108         struct netlink_callback *nl_cb;
109         struct sk_buff *skb;
110         u32 seq;
111 };
112
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 #define netlbl_unlhsh_rcu_deref(p) \
118         rcu_dereference_check(p, lockdep_is_held(&netlbl_unlhsh_lock))
119 static struct netlbl_unlhsh_tbl __rcu *netlbl_unlhsh;
120 static struct netlbl_unlhsh_iface __rcu *netlbl_unlhsh_def;
121
122 /* Accept unlabeled packets flag */
123 static u8 netlabel_unlabel_acceptflg;
124
125 /* NetLabel Generic NETLINK unlabeled family */
126 static struct genl_family netlbl_unlabel_gnl_family = {
127         .id = GENL_ID_GENERATE,
128         .hdrsize = 0,
129         .name = NETLBL_NLTYPE_UNLABELED_NAME,
130         .version = NETLBL_PROTO_VERSION,
131         .maxattr = NLBL_UNLABEL_A_MAX,
132 };
133
134 /* NetLabel Netlink attribute policy */
135 static const struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = {
136         [NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 },
137         [NLBL_UNLABEL_A_IPV6ADDR] = { .type = NLA_BINARY,
138                                       .len = sizeof(struct in6_addr) },
139         [NLBL_UNLABEL_A_IPV6MASK] = { .type = NLA_BINARY,
140                                       .len = sizeof(struct in6_addr) },
141         [NLBL_UNLABEL_A_IPV4ADDR] = { .type = NLA_BINARY,
142                                       .len = sizeof(struct in_addr) },
143         [NLBL_UNLABEL_A_IPV4MASK] = { .type = NLA_BINARY,
144                                       .len = sizeof(struct in_addr) },
145         [NLBL_UNLABEL_A_IFACE] = { .type = NLA_NUL_STRING,
146                                    .len = IFNAMSIZ - 1 },
147         [NLBL_UNLABEL_A_SECCTX] = { .type = NLA_BINARY }
148 };
149
150 /*
151  * Unlabeled Connection Hash Table Functions
152  */
153
154 /**
155  * netlbl_unlhsh_free_iface - Frees an interface entry from the hash table
156  * @entry: the entry's RCU field
157  *
158  * Description:
159  * This function is designed to be used as a callback to the call_rcu()
160  * function so that memory allocated to a hash table interface entry can be
161  * released safely.  It is important to note that this function does not free
162  * the IPv4 and IPv6 address lists contained as part of an interface entry.  It
163  * is up to the rest of the code to make sure an interface entry is only freed
164  * once it's address lists are empty.
165  *
166  */
167 static void netlbl_unlhsh_free_iface(struct rcu_head *entry)
168 {
169         struct netlbl_unlhsh_iface *iface;
170         struct netlbl_af4list *iter4;
171         struct netlbl_af4list *tmp4;
172 #if IS_ENABLED(CONFIG_IPV6)
173         struct netlbl_af6list *iter6;
174         struct netlbl_af6list *tmp6;
175 #endif /* IPv6 */
176
177         iface = container_of(entry, struct netlbl_unlhsh_iface, rcu);
178
179         /* no need for locks here since we are the only one with access to this
180          * structure */
181
182         netlbl_af4list_foreach_safe(iter4, tmp4, &iface->addr4_list) {
183                 netlbl_af4list_remove_entry(iter4);
184                 kfree(netlbl_unlhsh_addr4_entry(iter4));
185         }
186 #if IS_ENABLED(CONFIG_IPV6)
187         netlbl_af6list_foreach_safe(iter6, tmp6, &iface->addr6_list) {
188                 netlbl_af6list_remove_entry(iter6);
189                 kfree(netlbl_unlhsh_addr6_entry(iter6));
190         }
191 #endif /* IPv6 */
192         kfree(iface);
193 }
194
195 /**
196  * netlbl_unlhsh_hash - Hashing function for the hash table
197  * @ifindex: the network interface/device to hash
198  *
199  * Description:
200  * This is the hashing function for the unlabeled hash table, it returns the
201  * bucket number for the given device/interface.  The caller is responsible for
202  * ensuring that the hash table is protected with either a RCU read lock or
203  * the hash table lock.
204  *
205  */
206 static u32 netlbl_unlhsh_hash(int ifindex)
207 {
208         return ifindex & (netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->size - 1);
209 }
210
211 /**
212  * netlbl_unlhsh_search_iface - Search for a matching interface entry
213  * @ifindex: the network interface
214  *
215  * Description:
216  * Searches the unlabeled connection hash table and returns a pointer to the
217  * interface entry which matches @ifindex, otherwise NULL is returned.  The
218  * caller is responsible for ensuring that the hash table is protected with
219  * either a RCU read lock or the hash table lock.
220  *
221  */
222 static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface(int ifindex)
223 {
224         u32 bkt;
225         struct list_head *bkt_list;
226         struct netlbl_unlhsh_iface *iter;
227
228         bkt = netlbl_unlhsh_hash(ifindex);
229         bkt_list = &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt];
230         list_for_each_entry_rcu(iter, bkt_list, list)
231                 if (iter->valid && iter->ifindex == ifindex)
232                         return iter;
233
234         return NULL;
235 }
236
237 /**
238  * netlbl_unlhsh_add_addr4 - Add a new IPv4 address entry to the hash table
239  * @iface: the associated interface entry
240  * @addr: IPv4 address in network byte order
241  * @mask: IPv4 address mask in network byte order
242  * @secid: LSM secid value for entry
243  *
244  * Description:
245  * Add a new address entry into the unlabeled connection hash table using the
246  * interface entry specified by @iface.  On success zero is returned, otherwise
247  * a negative value is returned.
248  *
249  */
250 static int netlbl_unlhsh_add_addr4(struct netlbl_unlhsh_iface *iface,
251                                    const struct in_addr *addr,
252                                    const struct in_addr *mask,
253                                    u32 secid)
254 {
255         int ret_val;
256         struct netlbl_unlhsh_addr4 *entry;
257
258         entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
259         if (entry == NULL)
260                 return -ENOMEM;
261
262         entry->list.addr = addr->s_addr & mask->s_addr;
263         entry->list.mask = mask->s_addr;
264         entry->list.valid = 1;
265         entry->secid = secid;
266
267         spin_lock(&netlbl_unlhsh_lock);
268         ret_val = netlbl_af4list_add(&entry->list, &iface->addr4_list);
269         spin_unlock(&netlbl_unlhsh_lock);
270
271         if (ret_val != 0)
272                 kfree(entry);
273         return ret_val;
274 }
275
276 #if IS_ENABLED(CONFIG_IPV6)
277 /**
278  * netlbl_unlhsh_add_addr6 - Add a new IPv6 address entry to the hash table
279  * @iface: the associated interface entry
280  * @addr: IPv6 address in network byte order
281  * @mask: IPv6 address mask in network byte order
282  * @secid: LSM secid value for entry
283  *
284  * Description:
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.
288  *
289  */
290 static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface,
291                                    const struct in6_addr *addr,
292                                    const struct in6_addr *mask,
293                                    u32 secid)
294 {
295         int ret_val;
296         struct netlbl_unlhsh_addr6 *entry;
297
298         entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
299         if (entry == NULL)
300                 return -ENOMEM;
301
302         entry->list.addr = *addr;
303         entry->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
304         entry->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
305         entry->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
306         entry->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
307         entry->list.mask = *mask;
308         entry->list.valid = 1;
309         entry->secid = secid;
310
311         spin_lock(&netlbl_unlhsh_lock);
312         ret_val = netlbl_af6list_add(&entry->list, &iface->addr6_list);
313         spin_unlock(&netlbl_unlhsh_lock);
314
315         if (ret_val != 0)
316                 kfree(entry);
317         return 0;
318 }
319 #endif /* IPv6 */
320
321 /**
322  * netlbl_unlhsh_add_iface - Adds a new interface entry to the hash table
323  * @ifindex: network interface
324  *
325  * Description:
326  * Add a new, empty, interface entry into the unlabeled connection hash table.
327  * On success a pointer to the new interface entry is returned, on failure NULL
328  * is returned.
329  *
330  */
331 static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex)
332 {
333         u32 bkt;
334         struct netlbl_unlhsh_iface *iface;
335
336         iface = kzalloc(sizeof(*iface), GFP_ATOMIC);
337         if (iface == NULL)
338                 return NULL;
339
340         iface->ifindex = ifindex;
341         INIT_LIST_HEAD(&iface->addr4_list);
342         INIT_LIST_HEAD(&iface->addr6_list);
343         iface->valid = 1;
344
345         spin_lock(&netlbl_unlhsh_lock);
346         if (ifindex > 0) {
347                 bkt = netlbl_unlhsh_hash(ifindex);
348                 if (netlbl_unlhsh_search_iface(ifindex) != NULL)
349                         goto add_iface_failure;
350                 list_add_tail_rcu(&iface->list,
351                              &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt]);
352         } else {
353                 INIT_LIST_HEAD(&iface->list);
354                 if (netlbl_unlhsh_rcu_deref(netlbl_unlhsh_def) != NULL)
355                         goto add_iface_failure;
356                 rcu_assign_pointer(netlbl_unlhsh_def, iface);
357         }
358         spin_unlock(&netlbl_unlhsh_lock);
359
360         return iface;
361
362 add_iface_failure:
363         spin_unlock(&netlbl_unlhsh_lock);
364         kfree(iface);
365         return NULL;
366 }
367
368 /**
369  * netlbl_unlhsh_add - Adds a new entry to the unlabeled connection hash table
370  * @net: network namespace
371  * @dev_name: interface name
372  * @addr: IP address in network byte order
373  * @mask: address mask in network byte order
374  * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
375  * @secid: LSM secid value for the entry
376  * @audit_info: NetLabel audit information
377  *
378  * Description:
379  * Adds a new entry to the unlabeled connection hash table.  Returns zero on
380  * success, negative values on failure.
381  *
382  */
383 int netlbl_unlhsh_add(struct net *net,
384                       const char *dev_name,
385                       const void *addr,
386                       const void *mask,
387                       u32 addr_len,
388                       u32 secid,
389                       struct netlbl_audit *audit_info)
390 {
391         int ret_val;
392         int ifindex;
393         struct net_device *dev;
394         struct netlbl_unlhsh_iface *iface;
395         struct audit_buffer *audit_buf = NULL;
396         char *secctx = NULL;
397         u32 secctx_len;
398
399         if (addr_len != sizeof(struct in_addr) &&
400             addr_len != sizeof(struct in6_addr))
401                 return -EINVAL;
402
403         rcu_read_lock();
404         if (dev_name != NULL) {
405                 dev = dev_get_by_name_rcu(net, dev_name);
406                 if (dev == NULL) {
407                         ret_val = -ENODEV;
408                         goto unlhsh_add_return;
409                 }
410                 ifindex = dev->ifindex;
411                 iface = netlbl_unlhsh_search_iface(ifindex);
412         } else {
413                 ifindex = 0;
414                 iface = rcu_dereference(netlbl_unlhsh_def);
415         }
416         if (iface == NULL) {
417                 iface = netlbl_unlhsh_add_iface(ifindex);
418                 if (iface == NULL) {
419                         ret_val = -ENOMEM;
420                         goto unlhsh_add_return;
421                 }
422         }
423         audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCADD,
424                                               audit_info);
425         switch (addr_len) {
426         case sizeof(struct in_addr): {
427                 const struct in_addr *addr4 = addr;
428                 const struct in_addr *mask4 = mask;
429
430                 ret_val = netlbl_unlhsh_add_addr4(iface, addr4, mask4, secid);
431                 if (audit_buf != NULL)
432                         netlbl_af4list_audit_addr(audit_buf, 1,
433                                                   dev_name,
434                                                   addr4->s_addr,
435                                                   mask4->s_addr);
436                 break;
437         }
438 #if IS_ENABLED(CONFIG_IPV6)
439         case sizeof(struct in6_addr): {
440                 const struct in6_addr *addr6 = addr;
441                 const struct in6_addr *mask6 = mask;
442
443                 ret_val = netlbl_unlhsh_add_addr6(iface, addr6, mask6, secid);
444                 if (audit_buf != NULL)
445                         netlbl_af6list_audit_addr(audit_buf, 1,
446                                                   dev_name,
447                                                   addr6, mask6);
448                 break;
449         }
450 #endif /* IPv6 */
451         default:
452                 ret_val = -EINVAL;
453         }
454         if (ret_val == 0)
455                 atomic_inc(&netlabel_mgmt_protocount);
456
457 unlhsh_add_return:
458         rcu_read_unlock();
459         if (audit_buf != NULL) {
460                 if (security_secid_to_secctx(secid,
461                                              &secctx,
462                                              &secctx_len) == 0) {
463                         audit_log_format(audit_buf, " sec_obj=%s", secctx);
464                         security_release_secctx(secctx, secctx_len);
465                 }
466                 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
467                 audit_log_end(audit_buf);
468         }
469         return ret_val;
470 }
471
472 /**
473  * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry
474  * @net: network namespace
475  * @iface: interface entry
476  * @addr: IP address
477  * @mask: IP address mask
478  * @audit_info: NetLabel audit information
479  *
480  * Description:
481  * Remove an IP address entry from the unlabeled connection hash table.
482  * Returns zero on success, negative values on failure.
483  *
484  */
485 static int netlbl_unlhsh_remove_addr4(struct net *net,
486                                       struct netlbl_unlhsh_iface *iface,
487                                       const struct in_addr *addr,
488                                       const struct in_addr *mask,
489                                       struct netlbl_audit *audit_info)
490 {
491         struct netlbl_af4list *list_entry;
492         struct netlbl_unlhsh_addr4 *entry;
493         struct audit_buffer *audit_buf;
494         struct net_device *dev;
495         char *secctx;
496         u32 secctx_len;
497
498         spin_lock(&netlbl_unlhsh_lock);
499         list_entry = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
500                                            &iface->addr4_list);
501         spin_unlock(&netlbl_unlhsh_lock);
502         if (list_entry != NULL)
503                 entry = netlbl_unlhsh_addr4_entry(list_entry);
504         else
505                 entry = NULL;
506
507         audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
508                                               audit_info);
509         if (audit_buf != NULL) {
510                 dev = dev_get_by_index(net, iface->ifindex);
511                 netlbl_af4list_audit_addr(audit_buf, 1,
512                                           (dev != NULL ? dev->name : NULL),
513                                           addr->s_addr, mask->s_addr);
514                 if (dev != NULL)
515                         dev_put(dev);
516                 if (entry != NULL &&
517                     security_secid_to_secctx(entry->secid,
518                                              &secctx, &secctx_len) == 0) {
519                         audit_log_format(audit_buf, " sec_obj=%s", secctx);
520                         security_release_secctx(secctx, secctx_len);
521                 }
522                 audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
523                 audit_log_end(audit_buf);
524         }
525
526         if (entry == NULL)
527                 return -ENOENT;
528
529         kfree_rcu(entry, rcu);
530         return 0;
531 }
532
533 #if IS_ENABLED(CONFIG_IPV6)
534 /**
535  * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry
536  * @net: network namespace
537  * @iface: interface entry
538  * @addr: IP address
539  * @mask: IP address mask
540  * @audit_info: NetLabel audit information
541  *
542  * Description:
543  * Remove an IP address entry from the unlabeled connection hash table.
544  * Returns zero on success, negative values on failure.
545  *
546  */
547 static int netlbl_unlhsh_remove_addr6(struct net *net,
548                                       struct netlbl_unlhsh_iface *iface,
549                                       const struct in6_addr *addr,
550                                       const struct in6_addr *mask,
551                                       struct netlbl_audit *audit_info)
552 {
553         struct netlbl_af6list *list_entry;
554         struct netlbl_unlhsh_addr6 *entry;
555         struct audit_buffer *audit_buf;
556         struct net_device *dev;
557         char *secctx;
558         u32 secctx_len;
559
560         spin_lock(&netlbl_unlhsh_lock);
561         list_entry = netlbl_af6list_remove(addr, mask, &iface->addr6_list);
562         spin_unlock(&netlbl_unlhsh_lock);
563         if (list_entry != NULL)
564                 entry = netlbl_unlhsh_addr6_entry(list_entry);
565         else
566                 entry = NULL;
567
568         audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
569                                               audit_info);
570         if (audit_buf != NULL) {
571                 dev = dev_get_by_index(net, iface->ifindex);
572                 netlbl_af6list_audit_addr(audit_buf, 1,
573                                           (dev != NULL ? dev->name : NULL),
574                                           addr, mask);
575                 if (dev != NULL)
576                         dev_put(dev);
577                 if (entry != NULL &&
578                     security_secid_to_secctx(entry->secid,
579                                              &secctx, &secctx_len) == 0) {
580                         audit_log_format(audit_buf, " sec_obj=%s", secctx);
581                         security_release_secctx(secctx, secctx_len);
582                 }
583                 audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
584                 audit_log_end(audit_buf);
585         }
586
587         if (entry == NULL)
588                 return -ENOENT;
589
590         kfree_rcu(entry, rcu);
591         return 0;
592 }
593 #endif /* IPv6 */
594
595 /**
596  * netlbl_unlhsh_condremove_iface - Remove an interface entry
597  * @iface: the interface entry
598  *
599  * Description:
600  * Remove an interface entry from the unlabeled connection hash table if it is
601  * empty.  An interface entry is considered to be empty if there are no
602  * address entries assigned to it.
603  *
604  */
605 static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface)
606 {
607         struct netlbl_af4list *iter4;
608 #if IS_ENABLED(CONFIG_IPV6)
609         struct netlbl_af6list *iter6;
610 #endif /* IPv6 */
611
612         spin_lock(&netlbl_unlhsh_lock);
613         netlbl_af4list_foreach_rcu(iter4, &iface->addr4_list)
614                 goto unlhsh_condremove_failure;
615 #if IS_ENABLED(CONFIG_IPV6)
616         netlbl_af6list_foreach_rcu(iter6, &iface->addr6_list)
617                 goto unlhsh_condremove_failure;
618 #endif /* IPv6 */
619         iface->valid = 0;
620         if (iface->ifindex > 0)
621                 list_del_rcu(&iface->list);
622         else
623                 RCU_INIT_POINTER(netlbl_unlhsh_def, NULL);
624         spin_unlock(&netlbl_unlhsh_lock);
625
626         call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
627         return;
628
629 unlhsh_condremove_failure:
630         spin_unlock(&netlbl_unlhsh_lock);
631 }
632
633 /**
634  * netlbl_unlhsh_remove - Remove an entry from the unlabeled hash table
635  * @net: network namespace
636  * @dev_name: interface name
637  * @addr: IP address in network byte order
638  * @mask: address mask in network byte order
639  * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
640  * @audit_info: NetLabel audit information
641  *
642  * Description:
643  * Removes and existing entry from the unlabeled connection hash table.
644  * Returns zero on success, negative values on failure.
645  *
646  */
647 int netlbl_unlhsh_remove(struct net *net,
648                          const char *dev_name,
649                          const void *addr,
650                          const void *mask,
651                          u32 addr_len,
652                          struct netlbl_audit *audit_info)
653 {
654         int ret_val;
655         struct net_device *dev;
656         struct netlbl_unlhsh_iface *iface;
657
658         if (addr_len != sizeof(struct in_addr) &&
659             addr_len != sizeof(struct in6_addr))
660                 return -EINVAL;
661
662         rcu_read_lock();
663         if (dev_name != NULL) {
664                 dev = dev_get_by_name_rcu(net, dev_name);
665                 if (dev == NULL) {
666                         ret_val = -ENODEV;
667                         goto unlhsh_remove_return;
668                 }
669                 iface = netlbl_unlhsh_search_iface(dev->ifindex);
670         } else
671                 iface = rcu_dereference(netlbl_unlhsh_def);
672         if (iface == NULL) {
673                 ret_val = -ENOENT;
674                 goto unlhsh_remove_return;
675         }
676         switch (addr_len) {
677         case sizeof(struct in_addr):
678                 ret_val = netlbl_unlhsh_remove_addr4(net,
679                                                      iface, addr, mask,
680                                                      audit_info);
681                 break;
682 #if IS_ENABLED(CONFIG_IPV6)
683         case sizeof(struct in6_addr):
684                 ret_val = netlbl_unlhsh_remove_addr6(net,
685                                                      iface, addr, mask,
686                                                      audit_info);
687                 break;
688 #endif /* IPv6 */
689         default:
690                 ret_val = -EINVAL;
691         }
692         if (ret_val == 0) {
693                 netlbl_unlhsh_condremove_iface(iface);
694                 atomic_dec(&netlabel_mgmt_protocount);
695         }
696
697 unlhsh_remove_return:
698         rcu_read_unlock();
699         return ret_val;
700 }
701
702 /*
703  * General Helper Functions
704  */
705
706 /**
707  * netlbl_unlhsh_netdev_handler - Network device notification handler
708  * @this: notifier block
709  * @event: the event
710  * @ptr: the netdevice notifier info (cast to void)
711  *
712  * Description:
713  * Handle network device events, although at present all we care about is a
714  * network device going away.  In the case of a device going away we clear any
715  * related entries from the unlabeled connection hash table.
716  *
717  */
718 static int netlbl_unlhsh_netdev_handler(struct notifier_block *this,
719                                         unsigned long event, void *ptr)
720 {
721         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
722         struct netlbl_unlhsh_iface *iface = NULL;
723
724         if (!net_eq(dev_net(dev), &init_net))
725                 return NOTIFY_DONE;
726
727         /* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */
728         if (event == NETDEV_DOWN) {
729                 spin_lock(&netlbl_unlhsh_lock);
730                 iface = netlbl_unlhsh_search_iface(dev->ifindex);
731                 if (iface != NULL && iface->valid) {
732                         iface->valid = 0;
733                         list_del_rcu(&iface->list);
734                 } else
735                         iface = NULL;
736                 spin_unlock(&netlbl_unlhsh_lock);
737         }
738
739         if (iface != NULL)
740                 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
741
742         return NOTIFY_DONE;
743 }
744
745 /**
746  * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
747  * @value: desired value
748  * @audit_info: NetLabel audit information
749  *
750  * Description:
751  * Set the value of the unlabeled accept flag to @value.
752  *
753  */
754 static void netlbl_unlabel_acceptflg_set(u8 value,
755                                          struct netlbl_audit *audit_info)
756 {
757         struct audit_buffer *audit_buf;
758         u8 old_val;
759
760         old_val = netlabel_unlabel_acceptflg;
761         netlabel_unlabel_acceptflg = value;
762         audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
763                                               audit_info);
764         if (audit_buf != NULL) {
765                 audit_log_format(audit_buf,
766                                  " unlbl_accept=%u old=%u", value, old_val);
767                 audit_log_end(audit_buf);
768         }
769 }
770
771 /**
772  * netlbl_unlabel_addrinfo_get - Get the IPv4/6 address information
773  * @info: the Generic NETLINK info block
774  * @addr: the IP address
775  * @mask: the IP address mask
776  * @len: the address length
777  *
778  * Description:
779  * Examine the Generic NETLINK message and extract the IP address information.
780  * Returns zero on success, negative values on failure.
781  *
782  */
783 static int netlbl_unlabel_addrinfo_get(struct genl_info *info,
784                                        void **addr,
785                                        void **mask,
786                                        u32 *len)
787 {
788         u32 addr_len;
789
790         if (info->attrs[NLBL_UNLABEL_A_IPV4ADDR] &&
791             info->attrs[NLBL_UNLABEL_A_IPV4MASK]) {
792                 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
793                 if (addr_len != sizeof(struct in_addr) &&
794                     addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK]))
795                         return -EINVAL;
796                 *len = addr_len;
797                 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
798                 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]);
799                 return 0;
800         } else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) {
801                 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
802                 if (addr_len != sizeof(struct in6_addr) &&
803                     addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK]))
804                         return -EINVAL;
805                 *len = addr_len;
806                 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
807                 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]);
808                 return 0;
809         }
810
811         return -EINVAL;
812 }
813
814 /*
815  * NetLabel Command Handlers
816  */
817
818 /**
819  * netlbl_unlabel_accept - Handle an ACCEPT message
820  * @skb: the NETLINK buffer
821  * @info: the Generic NETLINK info block
822  *
823  * Description:
824  * Process a user generated ACCEPT message and set the accept flag accordingly.
825  * Returns zero on success, negative values on failure.
826  *
827  */
828 static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
829 {
830         u8 value;
831         struct netlbl_audit audit_info;
832
833         if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
834                 value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
835                 if (value == 1 || value == 0) {
836                         netlbl_netlink_auditinfo(skb, &audit_info);
837                         netlbl_unlabel_acceptflg_set(value, &audit_info);
838                         return 0;
839                 }
840         }
841
842         return -EINVAL;
843 }
844
845 /**
846  * netlbl_unlabel_list - Handle a LIST message
847  * @skb: the NETLINK buffer
848  * @info: the Generic NETLINK info block
849  *
850  * Description:
851  * Process a user generated LIST message and respond with the current status.
852  * Returns zero on success, negative values on failure.
853  *
854  */
855 static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
856 {
857         int ret_val = -EINVAL;
858         struct sk_buff *ans_skb;
859         void *data;
860
861         ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
862         if (ans_skb == NULL)
863                 goto list_failure;
864         data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
865                                  0, NLBL_UNLABEL_C_LIST);
866         if (data == NULL) {
867                 ret_val = -ENOMEM;
868                 goto list_failure;
869         }
870
871         ret_val = nla_put_u8(ans_skb,
872                              NLBL_UNLABEL_A_ACPTFLG,
873                              netlabel_unlabel_acceptflg);
874         if (ret_val != 0)
875                 goto list_failure;
876
877         genlmsg_end(ans_skb, data);
878         return genlmsg_reply(ans_skb, info);
879
880 list_failure:
881         kfree_skb(ans_skb);
882         return ret_val;
883 }
884
885 /**
886  * netlbl_unlabel_staticadd - Handle a STATICADD message
887  * @skb: the NETLINK buffer
888  * @info: the Generic NETLINK info block
889  *
890  * Description:
891  * Process a user generated STATICADD message and add a new unlabeled
892  * connection entry to the hash table.  Returns zero on success, negative
893  * values on failure.
894  *
895  */
896 static int netlbl_unlabel_staticadd(struct sk_buff *skb,
897                                     struct genl_info *info)
898 {
899         int ret_val;
900         char *dev_name;
901         void *addr;
902         void *mask;
903         u32 addr_len;
904         u32 secid;
905         struct netlbl_audit audit_info;
906
907         /* Don't allow users to add both IPv4 and IPv6 addresses for a
908          * single entry.  However, allow users to create two entries, one each
909          * for IPv4 and IPv4, with the same LSM security context which should
910          * achieve the same result. */
911         if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
912             !info->attrs[NLBL_UNLABEL_A_IFACE] ||
913             !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
914                !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
915               (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
916                !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
917                 return -EINVAL;
918
919         netlbl_netlink_auditinfo(skb, &audit_info);
920
921         ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
922         if (ret_val != 0)
923                 return ret_val;
924         dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
925         ret_val = security_secctx_to_secid(
926                                   nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
927                                   nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
928                                   &secid);
929         if (ret_val != 0)
930                 return ret_val;
931
932         return netlbl_unlhsh_add(&init_net,
933                                  dev_name, addr, mask, addr_len, secid,
934                                  &audit_info);
935 }
936
937 /**
938  * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message
939  * @skb: the NETLINK buffer
940  * @info: the Generic NETLINK info block
941  *
942  * Description:
943  * Process a user generated STATICADDDEF message and add a new default
944  * unlabeled connection entry.  Returns zero on success, negative values on
945  * failure.
946  *
947  */
948 static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
949                                        struct genl_info *info)
950 {
951         int ret_val;
952         void *addr;
953         void *mask;
954         u32 addr_len;
955         u32 secid;
956         struct netlbl_audit audit_info;
957
958         /* Don't allow users to add both IPv4 and IPv6 addresses for a
959          * single entry.  However, allow users to create two entries, one each
960          * for IPv4 and IPv6, with the same LSM security context which should
961          * achieve the same result. */
962         if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
963             !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
964                !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
965               (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
966                !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
967                 return -EINVAL;
968
969         netlbl_netlink_auditinfo(skb, &audit_info);
970
971         ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
972         if (ret_val != 0)
973                 return ret_val;
974         ret_val = security_secctx_to_secid(
975                                   nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
976                                   nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
977                                   &secid);
978         if (ret_val != 0)
979                 return ret_val;
980
981         return netlbl_unlhsh_add(&init_net,
982                                  NULL, addr, mask, addr_len, secid,
983                                  &audit_info);
984 }
985
986 /**
987  * netlbl_unlabel_staticremove - Handle a STATICREMOVE message
988  * @skb: the NETLINK buffer
989  * @info: the Generic NETLINK info block
990  *
991  * Description:
992  * Process a user generated STATICREMOVE message and remove the specified
993  * unlabeled connection entry.  Returns zero on success, negative values on
994  * failure.
995  *
996  */
997 static int netlbl_unlabel_staticremove(struct sk_buff *skb,
998                                        struct genl_info *info)
999 {
1000         int ret_val;
1001         char *dev_name;
1002         void *addr;
1003         void *mask;
1004         u32 addr_len;
1005         struct netlbl_audit audit_info;
1006
1007         /* See the note in netlbl_unlabel_staticadd() about not allowing both
1008          * IPv4 and IPv6 in the same entry. */
1009         if (!info->attrs[NLBL_UNLABEL_A_IFACE] ||
1010             !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1011                !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1012               (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1013                !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1014                 return -EINVAL;
1015
1016         netlbl_netlink_auditinfo(skb, &audit_info);
1017
1018         ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1019         if (ret_val != 0)
1020                 return ret_val;
1021         dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
1022
1023         return netlbl_unlhsh_remove(&init_net,
1024                                     dev_name, addr, mask, addr_len,
1025                                     &audit_info);
1026 }
1027
1028 /**
1029  * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message
1030  * @skb: the NETLINK buffer
1031  * @info: the Generic NETLINK info block
1032  *
1033  * Description:
1034  * Process a user generated STATICREMOVEDEF message and remove the default
1035  * unlabeled connection entry.  Returns zero on success, negative values on
1036  * failure.
1037  *
1038  */
1039 static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
1040                                           struct genl_info *info)
1041 {
1042         int ret_val;
1043         void *addr;
1044         void *mask;
1045         u32 addr_len;
1046         struct netlbl_audit audit_info;
1047
1048         /* See the note in netlbl_unlabel_staticadd() about not allowing both
1049          * IPv4 and IPv6 in the same entry. */
1050         if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1051                !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1052               (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1053                !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1054                 return -EINVAL;
1055
1056         netlbl_netlink_auditinfo(skb, &audit_info);
1057
1058         ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1059         if (ret_val != 0)
1060                 return ret_val;
1061
1062         return netlbl_unlhsh_remove(&init_net,
1063                                     NULL, addr, mask, addr_len,
1064                                     &audit_info);
1065 }
1066
1067
1068 /**
1069  * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF]
1070  * @cmd: command/message
1071  * @iface: the interface entry
1072  * @addr4: the IPv4 address entry
1073  * @addr6: the IPv6 address entry
1074  * @arg: the netlbl_unlhsh_walk_arg structure
1075  *
1076  * Description:
1077  * This function is designed to be used to generate a response for a
1078  * STATICLIST or STATICLISTDEF message.  When called either @addr4 or @addr6
1079  * can be specified, not both, the other unspecified entry should be set to
1080  * NULL by the caller.  Returns the size of the message on success, negative
1081  * values on failure.
1082  *
1083  */
1084 static int netlbl_unlabel_staticlist_gen(u32 cmd,
1085                                        const struct netlbl_unlhsh_iface *iface,
1086                                        const struct netlbl_unlhsh_addr4 *addr4,
1087                                        const struct netlbl_unlhsh_addr6 *addr6,
1088                                        void *arg)
1089 {
1090         int ret_val = -ENOMEM;
1091         struct netlbl_unlhsh_walk_arg *cb_arg = arg;
1092         struct net_device *dev;
1093         void *data;
1094         u32 secid;
1095         char *secctx;
1096         u32 secctx_len;
1097
1098         data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).portid,
1099                            cb_arg->seq, &netlbl_unlabel_gnl_family,
1100                            NLM_F_MULTI, cmd);
1101         if (data == NULL)
1102                 goto list_cb_failure;
1103
1104         if (iface->ifindex > 0) {
1105                 dev = dev_get_by_index(&init_net, iface->ifindex);
1106                 if (!dev) {
1107                         ret_val = -ENODEV;
1108                         goto list_cb_failure;
1109                 }
1110                 ret_val = nla_put_string(cb_arg->skb,
1111                                          NLBL_UNLABEL_A_IFACE, dev->name);
1112                 dev_put(dev);
1113                 if (ret_val != 0)
1114                         goto list_cb_failure;
1115         }
1116
1117         if (addr4) {
1118                 struct in_addr addr_struct;
1119
1120                 addr_struct.s_addr = addr4->list.addr;
1121                 ret_val = nla_put_in_addr(cb_arg->skb,
1122                                           NLBL_UNLABEL_A_IPV4ADDR,
1123                                           addr_struct.s_addr);
1124                 if (ret_val != 0)
1125                         goto list_cb_failure;
1126
1127                 addr_struct.s_addr = addr4->list.mask;
1128                 ret_val = nla_put_in_addr(cb_arg->skb,
1129                                           NLBL_UNLABEL_A_IPV4MASK,
1130                                           addr_struct.s_addr);
1131                 if (ret_val != 0)
1132                         goto list_cb_failure;
1133
1134                 secid = addr4->secid;
1135         } else {
1136                 ret_val = nla_put_in6_addr(cb_arg->skb,
1137                                            NLBL_UNLABEL_A_IPV6ADDR,
1138                                            &addr6->list.addr);
1139                 if (ret_val != 0)
1140                         goto list_cb_failure;
1141
1142                 ret_val = nla_put_in6_addr(cb_arg->skb,
1143                                            NLBL_UNLABEL_A_IPV6MASK,
1144                                            &addr6->list.mask);
1145                 if (ret_val != 0)
1146                         goto list_cb_failure;
1147
1148                 secid = addr6->secid;
1149         }
1150
1151         ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len);
1152         if (ret_val != 0)
1153                 goto list_cb_failure;
1154         ret_val = nla_put(cb_arg->skb,
1155                           NLBL_UNLABEL_A_SECCTX,
1156                           secctx_len,
1157                           secctx);
1158         security_release_secctx(secctx, secctx_len);
1159         if (ret_val != 0)
1160                 goto list_cb_failure;
1161
1162         cb_arg->seq++;
1163         genlmsg_end(cb_arg->skb, data);
1164         return 0;
1165
1166 list_cb_failure:
1167         genlmsg_cancel(cb_arg->skb, data);
1168         return ret_val;
1169 }
1170
1171 /**
1172  * netlbl_unlabel_staticlist - Handle a STATICLIST message
1173  * @skb: the NETLINK buffer
1174  * @cb: the NETLINK callback
1175  *
1176  * Description:
1177  * Process a user generated STATICLIST message and dump the unlabeled
1178  * connection hash table in a form suitable for use in a kernel generated
1179  * STATICLIST message.  Returns the length of @skb.
1180  *
1181  */
1182 static int netlbl_unlabel_staticlist(struct sk_buff *skb,
1183                                      struct netlink_callback *cb)
1184 {
1185         struct netlbl_unlhsh_walk_arg cb_arg;
1186         u32 skip_bkt = cb->args[0];
1187         u32 skip_chain = cb->args[1];
1188         u32 skip_addr4 = cb->args[2];
1189         u32 iter_bkt, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
1190         struct netlbl_unlhsh_iface *iface;
1191         struct list_head *iter_list;
1192         struct netlbl_af4list *addr4;
1193 #if IS_ENABLED(CONFIG_IPV6)
1194         u32 skip_addr6 = cb->args[3];
1195         struct netlbl_af6list *addr6;
1196 #endif
1197
1198         cb_arg.nl_cb = cb;
1199         cb_arg.skb = skb;
1200         cb_arg.seq = cb->nlh->nlmsg_seq;
1201
1202         rcu_read_lock();
1203         for (iter_bkt = skip_bkt;
1204              iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
1205              iter_bkt++) {
1206                 iter_list = &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt];
1207                 list_for_each_entry_rcu(iface, iter_list, list) {
1208                         if (!iface->valid ||
1209                             iter_chain++ < skip_chain)
1210                                 continue;
1211                         netlbl_af4list_foreach_rcu(addr4,
1212                                                    &iface->addr4_list) {
1213                                 if (iter_addr4++ < skip_addr4)
1214                                         continue;
1215                                 if (netlbl_unlabel_staticlist_gen(
1216                                               NLBL_UNLABEL_C_STATICLIST,
1217                                               iface,
1218                                               netlbl_unlhsh_addr4_entry(addr4),
1219                                               NULL,
1220                                               &cb_arg) < 0) {
1221                                         iter_addr4--;
1222                                         iter_chain--;
1223                                         goto unlabel_staticlist_return;
1224                                 }
1225                         }
1226                         iter_addr4 = 0;
1227                         skip_addr4 = 0;
1228 #if IS_ENABLED(CONFIG_IPV6)
1229                         netlbl_af6list_foreach_rcu(addr6,
1230                                                    &iface->addr6_list) {
1231                                 if (iter_addr6++ < skip_addr6)
1232                                         continue;
1233                                 if (netlbl_unlabel_staticlist_gen(
1234                                               NLBL_UNLABEL_C_STATICLIST,
1235                                               iface,
1236                                               NULL,
1237                                               netlbl_unlhsh_addr6_entry(addr6),
1238                                               &cb_arg) < 0) {
1239                                         iter_addr6--;
1240                                         iter_chain--;
1241                                         goto unlabel_staticlist_return;
1242                                 }
1243                         }
1244                         iter_addr6 = 0;
1245                         skip_addr6 = 0;
1246 #endif /* IPv6 */
1247                 }
1248                 iter_chain = 0;
1249                 skip_chain = 0;
1250         }
1251
1252 unlabel_staticlist_return:
1253         rcu_read_unlock();
1254         cb->args[0] = iter_bkt;
1255         cb->args[1] = iter_chain;
1256         cb->args[2] = iter_addr4;
1257         cb->args[3] = iter_addr6;
1258         return skb->len;
1259 }
1260
1261 /**
1262  * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message
1263  * @skb: the NETLINK buffer
1264  * @cb: the NETLINK callback
1265  *
1266  * Description:
1267  * Process a user generated STATICLISTDEF message and dump the default
1268  * unlabeled connection entry in a form suitable for use in a kernel generated
1269  * STATICLISTDEF message.  Returns the length of @skb.
1270  *
1271  */
1272 static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
1273                                         struct netlink_callback *cb)
1274 {
1275         struct netlbl_unlhsh_walk_arg cb_arg;
1276         struct netlbl_unlhsh_iface *iface;
1277         u32 iter_addr4 = 0, iter_addr6 = 0;
1278         struct netlbl_af4list *addr4;
1279 #if IS_ENABLED(CONFIG_IPV6)
1280         struct netlbl_af6list *addr6;
1281 #endif
1282
1283         cb_arg.nl_cb = cb;
1284         cb_arg.skb = skb;
1285         cb_arg.seq = cb->nlh->nlmsg_seq;
1286
1287         rcu_read_lock();
1288         iface = rcu_dereference(netlbl_unlhsh_def);
1289         if (iface == NULL || !iface->valid)
1290                 goto unlabel_staticlistdef_return;
1291
1292         netlbl_af4list_foreach_rcu(addr4, &iface->addr4_list) {
1293                 if (iter_addr4++ < cb->args[0])
1294                         continue;
1295                 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
1296                                               iface,
1297                                               netlbl_unlhsh_addr4_entry(addr4),
1298                                               NULL,
1299                                               &cb_arg) < 0) {
1300                         iter_addr4--;
1301                         goto unlabel_staticlistdef_return;
1302                 }
1303         }
1304 #if IS_ENABLED(CONFIG_IPV6)
1305         netlbl_af6list_foreach_rcu(addr6, &iface->addr6_list) {
1306                 if (iter_addr6++ < cb->args[1])
1307                         continue;
1308                 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
1309                                               iface,
1310                                               NULL,
1311                                               netlbl_unlhsh_addr6_entry(addr6),
1312                                               &cb_arg) < 0) {
1313                         iter_addr6--;
1314                         goto unlabel_staticlistdef_return;
1315                 }
1316         }
1317 #endif /* IPv6 */
1318
1319 unlabel_staticlistdef_return:
1320         rcu_read_unlock();
1321         cb->args[0] = iter_addr4;
1322         cb->args[1] = iter_addr6;
1323         return skb->len;
1324 }
1325
1326 /*
1327  * NetLabel Generic NETLINK Command Definitions
1328  */
1329
1330 static const struct genl_ops netlbl_unlabel_genl_ops[] = {
1331         {
1332         .cmd = NLBL_UNLABEL_C_STATICADD,
1333         .flags = GENL_ADMIN_PERM,
1334         .policy = netlbl_unlabel_genl_policy,
1335         .doit = netlbl_unlabel_staticadd,
1336         .dumpit = NULL,
1337         },
1338         {
1339         .cmd = NLBL_UNLABEL_C_STATICREMOVE,
1340         .flags = GENL_ADMIN_PERM,
1341         .policy = netlbl_unlabel_genl_policy,
1342         .doit = netlbl_unlabel_staticremove,
1343         .dumpit = NULL,
1344         },
1345         {
1346         .cmd = NLBL_UNLABEL_C_STATICLIST,
1347         .flags = 0,
1348         .policy = netlbl_unlabel_genl_policy,
1349         .doit = NULL,
1350         .dumpit = netlbl_unlabel_staticlist,
1351         },
1352         {
1353         .cmd = NLBL_UNLABEL_C_STATICADDDEF,
1354         .flags = GENL_ADMIN_PERM,
1355         .policy = netlbl_unlabel_genl_policy,
1356         .doit = netlbl_unlabel_staticadddef,
1357         .dumpit = NULL,
1358         },
1359         {
1360         .cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
1361         .flags = GENL_ADMIN_PERM,
1362         .policy = netlbl_unlabel_genl_policy,
1363         .doit = netlbl_unlabel_staticremovedef,
1364         .dumpit = NULL,
1365         },
1366         {
1367         .cmd = NLBL_UNLABEL_C_STATICLISTDEF,
1368         .flags = 0,
1369         .policy = netlbl_unlabel_genl_policy,
1370         .doit = NULL,
1371         .dumpit = netlbl_unlabel_staticlistdef,
1372         },
1373         {
1374         .cmd = NLBL_UNLABEL_C_ACCEPT,
1375         .flags = GENL_ADMIN_PERM,
1376         .policy = netlbl_unlabel_genl_policy,
1377         .doit = netlbl_unlabel_accept,
1378         .dumpit = NULL,
1379         },
1380         {
1381         .cmd = NLBL_UNLABEL_C_LIST,
1382         .flags = 0,
1383         .policy = netlbl_unlabel_genl_policy,
1384         .doit = netlbl_unlabel_list,
1385         .dumpit = NULL,
1386         },
1387 };
1388
1389 /*
1390  * NetLabel Generic NETLINK Protocol Functions
1391  */
1392
1393 /**
1394  * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
1395  *
1396  * Description:
1397  * Register the unlabeled packet NetLabel component with the Generic NETLINK
1398  * mechanism.  Returns zero on success, negative values on failure.
1399  *
1400  */
1401 int __init netlbl_unlabel_genl_init(void)
1402 {
1403         return genl_register_family_with_ops(&netlbl_unlabel_gnl_family,
1404                                              netlbl_unlabel_genl_ops);
1405 }
1406
1407 /*
1408  * NetLabel KAPI Hooks
1409  */
1410
1411 static struct notifier_block netlbl_unlhsh_netdev_notifier = {
1412         .notifier_call = netlbl_unlhsh_netdev_handler,
1413 };
1414
1415 /**
1416  * netlbl_unlabel_init - Initialize the unlabeled connection hash table
1417  * @size: the number of bits to use for the hash buckets
1418  *
1419  * Description:
1420  * Initializes the unlabeled connection hash table and registers a network
1421  * device notification handler.  This function should only be called by the
1422  * NetLabel subsystem itself during initialization.  Returns zero on success,
1423  * non-zero values on error.
1424  *
1425  */
1426 int __init netlbl_unlabel_init(u32 size)
1427 {
1428         u32 iter;
1429         struct netlbl_unlhsh_tbl *hsh_tbl;
1430
1431         if (size == 0)
1432                 return -EINVAL;
1433
1434         hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
1435         if (hsh_tbl == NULL)
1436                 return -ENOMEM;
1437         hsh_tbl->size = 1 << size;
1438         hsh_tbl->tbl = kcalloc(hsh_tbl->size,
1439                                sizeof(struct list_head),
1440                                GFP_KERNEL);
1441         if (hsh_tbl->tbl == NULL) {
1442                 kfree(hsh_tbl);
1443                 return -ENOMEM;
1444         }
1445         for (iter = 0; iter < hsh_tbl->size; iter++)
1446                 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
1447
1448         spin_lock(&netlbl_unlhsh_lock);
1449         rcu_assign_pointer(netlbl_unlhsh, hsh_tbl);
1450         spin_unlock(&netlbl_unlhsh_lock);
1451
1452         register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier);
1453
1454         return 0;
1455 }
1456
1457 /**
1458  * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
1459  * @skb: the packet
1460  * @family: protocol family
1461  * @secattr: the security attributes
1462  *
1463  * Description:
1464  * Determine the security attributes, if any, for an unlabled packet and return
1465  * them in @secattr.  Returns zero on success and negative values on failure.
1466  *
1467  */
1468 int netlbl_unlabel_getattr(const struct sk_buff *skb,
1469                            u16 family,
1470                            struct netlbl_lsm_secattr *secattr)
1471 {
1472         struct netlbl_unlhsh_iface *iface;
1473
1474         rcu_read_lock();
1475         iface = netlbl_unlhsh_search_iface(skb->skb_iif);
1476         if (iface == NULL)
1477                 iface = rcu_dereference(netlbl_unlhsh_def);
1478         if (iface == NULL || !iface->valid)
1479                 goto unlabel_getattr_nolabel;
1480
1481 #if IS_ENABLED(CONFIG_IPV6)
1482         /* When resolving a fallback label, check the sk_buff version as
1483          * it is possible (e.g. SCTP) to have family = PF_INET6 while
1484          * receiving ip_hdr(skb)->version = 4.
1485          */
1486         if (family == PF_INET6 && ip_hdr(skb)->version == 4)
1487                 family = PF_INET;
1488 #endif /* IPv6 */
1489
1490         switch (family) {
1491         case PF_INET: {
1492                 struct iphdr *hdr4;
1493                 struct netlbl_af4list *addr4;
1494
1495                 hdr4 = ip_hdr(skb);
1496                 addr4 = netlbl_af4list_search(hdr4->saddr,
1497                                               &iface->addr4_list);
1498                 if (addr4 == NULL)
1499                         goto unlabel_getattr_nolabel;
1500                 secattr->attr.secid = netlbl_unlhsh_addr4_entry(addr4)->secid;
1501                 break;
1502         }
1503 #if IS_ENABLED(CONFIG_IPV6)
1504         case PF_INET6: {
1505                 struct ipv6hdr *hdr6;
1506                 struct netlbl_af6list *addr6;
1507
1508                 hdr6 = ipv6_hdr(skb);
1509                 addr6 = netlbl_af6list_search(&hdr6->saddr,
1510                                               &iface->addr6_list);
1511                 if (addr6 == NULL)
1512                         goto unlabel_getattr_nolabel;
1513                 secattr->attr.secid = netlbl_unlhsh_addr6_entry(addr6)->secid;
1514                 break;
1515         }
1516 #endif /* IPv6 */
1517         default:
1518                 goto unlabel_getattr_nolabel;
1519         }
1520         rcu_read_unlock();
1521
1522         secattr->flags |= NETLBL_SECATTR_SECID;
1523         secattr->type = NETLBL_NLTYPE_UNLABELED;
1524         return 0;
1525
1526 unlabel_getattr_nolabel:
1527         rcu_read_unlock();
1528         if (netlabel_unlabel_acceptflg == 0)
1529                 return -ENOMSG;
1530         secattr->type = NETLBL_NLTYPE_UNLABELED;
1531         return 0;
1532 }
1533
1534 /**
1535  * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
1536  *
1537  * Description:
1538  * Set the default NetLabel configuration to allow incoming unlabeled packets
1539  * and to send unlabeled network traffic by default.
1540  *
1541  */
1542 int __init netlbl_unlabel_defconf(void)
1543 {
1544         int ret_val;
1545         struct netlbl_dom_map *entry;
1546         struct netlbl_audit audit_info;
1547
1548         /* Only the kernel is allowed to call this function and the only time
1549          * it is called is at bootup before the audit subsystem is reporting
1550          * messages so don't worry to much about these values. */
1551         security_task_getsecid(current, &audit_info.secid);
1552         audit_info.loginuid = GLOBAL_ROOT_UID;
1553         audit_info.sessionid = 0;
1554
1555         entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1556         if (entry == NULL)
1557                 return -ENOMEM;
1558         entry->family = AF_UNSPEC;
1559         entry->def.type = NETLBL_NLTYPE_UNLABELED;
1560         ret_val = netlbl_domhsh_add_default(entry, &audit_info);
1561         if (ret_val != 0)
1562                 return ret_val;
1563
1564         netlbl_unlabel_acceptflg_set(1, &audit_info);
1565
1566         return 0;
1567 }