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