blob: 3dfe2bac8623397b0ca081a845925d8190c0e5be [file] [log] [blame]
Paul Moore96cb8e32006-08-03 16:48:59 -07001/*
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.moore@hp.com>
9 *
10 */
11
12/*
Paul Moore61e10682008-10-10 10:16:32 -040013 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006 - 2008
Paul Moore96cb8e32006-08-03 16:48:59 -070014 *
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>
Paul Moore8cc44572008-01-29 08:44:21 -050032#include <linux/rcupdate.h>
Paul Moore96cb8e32006-08-03 16:48:59 -070033#include <linux/list.h>
34#include <linux/spinlock.h>
35#include <linux/socket.h>
36#include <linux/string.h>
37#include <linux/skbuff.h>
Paul Moorede646882006-11-17 17:38:55 -050038#include <linux/audit.h>
Paul Moore8cc44572008-01-29 08:44:21 -050039#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>
Paul Moore96cb8e32006-08-03 16:48:59 -070046#include <net/sock.h>
47#include <net/netlink.h>
48#include <net/genetlink.h>
Paul Moore8cc44572008-01-29 08:44:21 -050049#include <net/ip.h>
50#include <net/ipv6.h>
51#include <net/net_namespace.h>
Paul Moore96cb8e32006-08-03 16:48:59 -070052#include <net/netlabel.h>
53#include <asm/bug.h>
Paul Moore8cc44572008-01-29 08:44:21 -050054#include <asm/atomic.h>
Paul Moore96cb8e32006-08-03 16:48:59 -070055
56#include "netlabel_user.h"
Paul Moore61e10682008-10-10 10:16:32 -040057#include "netlabel_addrlist.h"
Paul Moore96cb8e32006-08-03 16:48:59 -070058#include "netlabel_domainhash.h"
59#include "netlabel_unlabeled.h"
Paul Moore8cc44572008-01-29 08:44:21 -050060#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 */
76struct netlbl_unlhsh_tbl {
77 struct list_head *tbl;
78 u32 size;
79};
Paul Moore61e10682008-10-10 10:16:32 -040080#define netlbl_unlhsh_addr4_entry(iter) \
81 container_of(iter, struct netlbl_unlhsh_addr4, list)
Paul Moore8cc44572008-01-29 08:44:21 -050082struct netlbl_unlhsh_addr4 {
Paul Moore8cc44572008-01-29 08:44:21 -050083 u32 secid;
84
Paul Moore61e10682008-10-10 10:16:32 -040085 struct netlbl_af4list list;
Paul Moore8cc44572008-01-29 08:44:21 -050086 struct rcu_head rcu;
87};
Paul Moore61e10682008-10-10 10:16:32 -040088#define netlbl_unlhsh_addr6_entry(iter) \
89 container_of(iter, struct netlbl_unlhsh_addr6, list)
Paul Moore8cc44572008-01-29 08:44:21 -050090struct netlbl_unlhsh_addr6 {
Paul Moore8cc44572008-01-29 08:44:21 -050091 u32 secid;
92
Paul Moore61e10682008-10-10 10:16:32 -040093 struct netlbl_af6list list;
Paul Moore8cc44572008-01-29 08:44:21 -050094 struct rcu_head rcu;
95};
96struct 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() */
107struct 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 */
116static DEFINE_SPINLOCK(netlbl_unlhsh_lock);
117static struct netlbl_unlhsh_tbl *netlbl_unlhsh = NULL;
118static struct netlbl_unlhsh_iface *netlbl_unlhsh_def = NULL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700119
120/* Accept unlabeled packets flag */
Paul Moorecd287862006-11-17 17:38:44 -0500121static u8 netlabel_unlabel_acceptflg = 0;
Paul Moore96cb8e32006-08-03 16:48:59 -0700122
Paul Moore8cc44572008-01-29 08:44:21 -0500123/* NetLabel Generic NETLINK unlabeled family */
Paul Moore96cb8e32006-08-03 16:48:59 -0700124static struct genl_family netlbl_unlabel_gnl_family = {
125 .id = GENL_ID_GENERATE,
126 .hdrsize = 0,
127 .name = NETLBL_NLTYPE_UNLABELED_NAME,
128 .version = NETLBL_PROTO_VERSION,
Paul Moorefd385852006-09-25 15:56:37 -0700129 .maxattr = NLBL_UNLABEL_A_MAX,
Paul Moore96cb8e32006-08-03 16:48:59 -0700130};
131
Paul Moorefd385852006-09-25 15:56:37 -0700132/* NetLabel Netlink attribute policy */
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700133static const struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = {
Paul Moorefd385852006-09-25 15:56:37 -0700134 [NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 },
Paul Moore8cc44572008-01-29 08:44:21 -0500135 [NLBL_UNLABEL_A_IPV6ADDR] = { .type = NLA_BINARY,
136 .len = sizeof(struct in6_addr) },
137 [NLBL_UNLABEL_A_IPV6MASK] = { .type = NLA_BINARY,
138 .len = sizeof(struct in6_addr) },
139 [NLBL_UNLABEL_A_IPV4ADDR] = { .type = NLA_BINARY,
140 .len = sizeof(struct in_addr) },
141 [NLBL_UNLABEL_A_IPV4MASK] = { .type = NLA_BINARY,
142 .len = sizeof(struct in_addr) },
143 [NLBL_UNLABEL_A_IFACE] = { .type = NLA_NUL_STRING,
144 .len = IFNAMSIZ - 1 },
145 [NLBL_UNLABEL_A_SECCTX] = { .type = NLA_BINARY }
Paul Moorefd385852006-09-25 15:56:37 -0700146};
Paul Moore96cb8e32006-08-03 16:48:59 -0700147
148/*
Paul Moore8cc44572008-01-29 08:44:21 -0500149 * Unlabeled Connection Hash Table Functions
Paul Moore32f50cd2006-09-28 14:51:47 -0700150 */
151
152/**
Paul Moore8cc44572008-01-29 08:44:21 -0500153 * netlbl_unlhsh_free_addr4 - Frees an IPv4 address entry from the hash table
154 * @entry: the entry's RCU field
155 *
156 * Description:
157 * This function is designed to be used as a callback to the call_rcu()
158 * function so that memory allocated to a hash table address entry can be
159 * released safely.
160 *
161 */
162static void netlbl_unlhsh_free_addr4(struct rcu_head *entry)
163{
164 struct netlbl_unlhsh_addr4 *ptr;
165
166 ptr = container_of(entry, struct netlbl_unlhsh_addr4, rcu);
167 kfree(ptr);
168}
169
170#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
171/**
172 * netlbl_unlhsh_free_addr6 - Frees an IPv6 address entry from the hash table
173 * @entry: the entry's RCU field
174 *
175 * Description:
176 * This function is designed to be used as a callback to the call_rcu()
177 * function so that memory allocated to a hash table address entry can be
178 * released safely.
179 *
180 */
181static void netlbl_unlhsh_free_addr6(struct rcu_head *entry)
182{
183 struct netlbl_unlhsh_addr6 *ptr;
184
185 ptr = container_of(entry, struct netlbl_unlhsh_addr6, rcu);
186 kfree(ptr);
187}
188#endif /* IPv6 */
189
190/**
191 * netlbl_unlhsh_free_iface - Frees an interface entry from the hash table
192 * @entry: the entry's RCU field
193 *
194 * Description:
195 * This function is designed to be used as a callback to the call_rcu()
196 * function so that memory allocated to a hash table interface entry can be
197 * released safely. It is important to note that this function does not free
198 * the IPv4 and IPv6 address lists contained as part of an interface entry. It
199 * is up to the rest of the code to make sure an interface entry is only freed
200 * once it's address lists are empty.
201 *
202 */
203static void netlbl_unlhsh_free_iface(struct rcu_head *entry)
204{
205 struct netlbl_unlhsh_iface *iface;
Paul Moore61e10682008-10-10 10:16:32 -0400206 struct netlbl_af4list *iter4;
207 struct netlbl_af4list *tmp4;
208#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
209 struct netlbl_af6list *iter6;
210 struct netlbl_af6list *tmp6;
211#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500212
213 iface = container_of(entry, struct netlbl_unlhsh_iface, rcu);
214
215 /* no need for locks here since we are the only one with access to this
216 * structure */
217
Paul Moore61e10682008-10-10 10:16:32 -0400218 netlbl_af4list_foreach_safe(iter4, tmp4, &iface->addr4_list) {
219 netlbl_af4list_remove_entry(iter4);
220 kfree(netlbl_unlhsh_addr4_entry(iter4));
221 }
222#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
223 netlbl_af6list_foreach_safe(iter6, tmp6, &iface->addr6_list) {
224 netlbl_af6list_remove_entry(iter6);
225 kfree(netlbl_unlhsh_addr6_entry(iter6));
226 }
227#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500228 kfree(iface);
229}
230
231/**
232 * netlbl_unlhsh_hash - Hashing function for the hash table
233 * @ifindex: the network interface/device to hash
234 *
235 * Description:
236 * This is the hashing function for the unlabeled hash table, it returns the
237 * bucket number for the given device/interface. The caller is responsible for
238 * calling the rcu_read_[un]lock() functions.
239 *
240 */
241static u32 netlbl_unlhsh_hash(int ifindex)
242{
243 /* this is taken _almost_ directly from
244 * security/selinux/netif.c:sel_netif_hasfn() as they do pretty much
245 * the same thing */
246 return ifindex & (rcu_dereference(netlbl_unlhsh)->size - 1);
247}
248
249/**
Paul Moore8cc44572008-01-29 08:44:21 -0500250 * netlbl_unlhsh_search_iface - Search for a matching interface entry
251 * @ifindex: the network interface
252 *
253 * Description:
254 * Searches the unlabeled connection hash table and returns a pointer to the
255 * interface entry which matches @ifindex, otherwise NULL is returned. The
256 * caller is responsible for calling the rcu_read_[un]lock() functions.
257 *
258 */
259static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface(int ifindex)
260{
261 u32 bkt;
Paul Moore56196702008-10-10 10:16:29 -0400262 struct list_head *bkt_list;
Paul Moore8cc44572008-01-29 08:44:21 -0500263 struct netlbl_unlhsh_iface *iter;
264
265 bkt = netlbl_unlhsh_hash(ifindex);
Paul Moore56196702008-10-10 10:16:29 -0400266 bkt_list = &rcu_dereference(netlbl_unlhsh)->tbl[bkt];
267 list_for_each_entry_rcu(iter, bkt_list, list)
Paul Moore8cc44572008-01-29 08:44:21 -0500268 if (iter->valid && iter->ifindex == ifindex)
269 return iter;
270
271 return NULL;
272}
273
274/**
275 * netlbl_unlhsh_search_iface_def - Search for a matching interface entry
276 * @ifindex: the network interface
277 *
278 * Description:
279 * Searches the unlabeled connection hash table and returns a pointer to the
280 * interface entry which matches @ifindex. If an exact match can not be found
281 * and there is a valid default entry, the default entry is returned, otherwise
282 * NULL is returned. The caller is responsible for calling the
283 * rcu_read_[un]lock() functions.
284 *
285 */
286static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface_def(int ifindex)
287{
288 struct netlbl_unlhsh_iface *entry;
289
290 entry = netlbl_unlhsh_search_iface(ifindex);
291 if (entry != NULL)
292 return entry;
293
294 entry = rcu_dereference(netlbl_unlhsh_def);
295 if (entry != NULL && entry->valid)
296 return entry;
297
298 return NULL;
299}
300
301/**
302 * netlbl_unlhsh_add_addr4 - Add a new IPv4 address entry to the hash table
303 * @iface: the associated interface entry
304 * @addr: IPv4 address in network byte order
305 * @mask: IPv4 address mask in network byte order
306 * @secid: LSM secid value for entry
307 *
308 * Description:
309 * Add a new address entry into the unlabeled connection hash table using the
310 * interface entry specified by @iface. On success zero is returned, otherwise
311 * a negative value is returned. The caller is responsible for calling the
312 * rcu_read_[un]lock() functions.
313 *
314 */
315static int netlbl_unlhsh_add_addr4(struct netlbl_unlhsh_iface *iface,
316 const struct in_addr *addr,
317 const struct in_addr *mask,
318 u32 secid)
319{
Paul Moore61e10682008-10-10 10:16:32 -0400320 int ret_val;
Paul Moore8cc44572008-01-29 08:44:21 -0500321 struct netlbl_unlhsh_addr4 *entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500322
323 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
324 if (entry == NULL)
325 return -ENOMEM;
326
Paul Moore61e10682008-10-10 10:16:32 -0400327 entry->list.addr = addr->s_addr & mask->s_addr;
328 entry->list.mask = mask->s_addr;
329 entry->list.valid = 1;
Paul Moore8cc44572008-01-29 08:44:21 -0500330 INIT_RCU_HEAD(&entry->rcu);
Paul Moore61e10682008-10-10 10:16:32 -0400331 entry->secid = secid;
Paul Moore8cc44572008-01-29 08:44:21 -0500332
333 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400334 ret_val = netlbl_af4list_add(&entry->list, &iface->addr4_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500335 spin_unlock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400336
337 if (ret_val != 0)
338 kfree(entry);
339 return ret_val;
Paul Moore8cc44572008-01-29 08:44:21 -0500340}
341
342#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
343/**
344 * netlbl_unlhsh_add_addr6 - Add a new IPv6 address entry to the hash table
345 * @iface: the associated interface entry
346 * @addr: IPv6 address in network byte order
347 * @mask: IPv6 address mask in network byte order
348 * @secid: LSM secid value for entry
349 *
350 * Description:
351 * Add a new address entry into the unlabeled connection hash table using the
352 * interface entry specified by @iface. On success zero is returned, otherwise
353 * a negative value is returned. The caller is responsible for calling the
354 * rcu_read_[un]lock() functions.
355 *
356 */
357static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface,
358 const struct in6_addr *addr,
359 const struct in6_addr *mask,
360 u32 secid)
361{
Paul Moore61e10682008-10-10 10:16:32 -0400362 int ret_val;
Paul Moore8cc44572008-01-29 08:44:21 -0500363 struct netlbl_unlhsh_addr6 *entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500364
365 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
366 if (entry == NULL)
367 return -ENOMEM;
368
Paul Moore61e10682008-10-10 10:16:32 -0400369 ipv6_addr_copy(&entry->list.addr, addr);
370 entry->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
371 entry->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
372 entry->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
373 entry->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
374 ipv6_addr_copy(&entry->list.mask, mask);
375 entry->list.valid = 1;
Paul Moore8cc44572008-01-29 08:44:21 -0500376 INIT_RCU_HEAD(&entry->rcu);
Paul Moore61e10682008-10-10 10:16:32 -0400377 entry->secid = secid;
Paul Moore8cc44572008-01-29 08:44:21 -0500378
379 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400380 ret_val = netlbl_af6list_add(&entry->list, &iface->addr6_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500381 spin_unlock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400382
383 if (ret_val != 0)
384 kfree(entry);
Paul Moore8cc44572008-01-29 08:44:21 -0500385 return 0;
386}
387#endif /* IPv6 */
388
389/**
390 * netlbl_unlhsh_add_iface - Adds a new interface entry to the hash table
391 * @ifindex: network interface
392 *
393 * Description:
394 * Add a new, empty, interface entry into the unlabeled connection hash table.
395 * On success a pointer to the new interface entry is returned, on failure NULL
396 * is returned. The caller is responsible for calling the rcu_read_[un]lock()
397 * functions.
398 *
399 */
400static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex)
401{
402 u32 bkt;
403 struct netlbl_unlhsh_iface *iface;
404
405 iface = kzalloc(sizeof(*iface), GFP_ATOMIC);
406 if (iface == NULL)
407 return NULL;
408
409 iface->ifindex = ifindex;
410 INIT_LIST_HEAD(&iface->addr4_list);
411 INIT_LIST_HEAD(&iface->addr6_list);
412 iface->valid = 1;
413 INIT_RCU_HEAD(&iface->rcu);
414
415 spin_lock(&netlbl_unlhsh_lock);
416 if (ifindex > 0) {
417 bkt = netlbl_unlhsh_hash(ifindex);
418 if (netlbl_unlhsh_search_iface(ifindex) != NULL)
419 goto add_iface_failure;
420 list_add_tail_rcu(&iface->list,
421 &rcu_dereference(netlbl_unlhsh)->tbl[bkt]);
422 } else {
423 INIT_LIST_HEAD(&iface->list);
424 if (rcu_dereference(netlbl_unlhsh_def) != NULL)
425 goto add_iface_failure;
426 rcu_assign_pointer(netlbl_unlhsh_def, iface);
427 }
428 spin_unlock(&netlbl_unlhsh_lock);
429
430 return iface;
431
432add_iface_failure:
433 spin_unlock(&netlbl_unlhsh_lock);
434 kfree(iface);
435 return NULL;
436}
437
438/**
439 * netlbl_unlhsh_add - Adds a new entry to the unlabeled connection hash table
440 * @net: network namespace
441 * @dev_name: interface name
442 * @addr: IP address in network byte order
443 * @mask: address mask in network byte order
444 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
445 * @secid: LSM secid value for the entry
Paul Moore13541b32008-01-29 08:44:23 -0500446 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500447 *
448 * Description:
449 * Adds a new entry to the unlabeled connection hash table. Returns zero on
450 * success, negative values on failure.
451 *
452 */
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500453int netlbl_unlhsh_add(struct net *net,
454 const char *dev_name,
455 const void *addr,
456 const void *mask,
457 u32 addr_len,
458 u32 secid,
459 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500460{
461 int ret_val;
462 int ifindex;
463 struct net_device *dev;
464 struct netlbl_unlhsh_iface *iface;
Paul Moore13541b32008-01-29 08:44:23 -0500465 struct audit_buffer *audit_buf = NULL;
466 char *secctx = NULL;
467 u32 secctx_len;
Paul Moore8cc44572008-01-29 08:44:21 -0500468
469 if (addr_len != sizeof(struct in_addr) &&
470 addr_len != sizeof(struct in6_addr))
471 return -EINVAL;
472
473 rcu_read_lock();
474 if (dev_name != NULL) {
Eric Dumazet122ec6f2009-11-05 20:53:47 -0800475 dev = dev_get_by_name_rcu(net, dev_name);
Paul Moore8cc44572008-01-29 08:44:21 -0500476 if (dev == NULL) {
477 ret_val = -ENODEV;
478 goto unlhsh_add_return;
479 }
480 ifindex = dev->ifindex;
Paul Moore8cc44572008-01-29 08:44:21 -0500481 iface = netlbl_unlhsh_search_iface(ifindex);
482 } else {
483 ifindex = 0;
484 iface = rcu_dereference(netlbl_unlhsh_def);
485 }
486 if (iface == NULL) {
487 iface = netlbl_unlhsh_add_iface(ifindex);
488 if (iface == NULL) {
489 ret_val = -ENOMEM;
490 goto unlhsh_add_return;
491 }
492 }
Paul Moore13541b32008-01-29 08:44:23 -0500493 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCADD,
494 audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500495 switch (addr_len) {
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800496 case sizeof(struct in_addr): {
497 struct in_addr *addr4, *mask4;
498
Paul Moore13541b32008-01-29 08:44:23 -0500499 addr4 = (struct in_addr *)addr;
500 mask4 = (struct in_addr *)mask;
501 ret_val = netlbl_unlhsh_add_addr4(iface, addr4, mask4, secid);
502 if (audit_buf != NULL)
Paul Moore63c41682008-10-10 10:16:32 -0400503 netlbl_af4list_audit_addr(audit_buf, 1,
504 dev_name,
505 addr4->s_addr,
506 mask4->s_addr);
Paul Moore8cc44572008-01-29 08:44:21 -0500507 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800508 }
Paul Moore8cc44572008-01-29 08:44:21 -0500509#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800510 case sizeof(struct in6_addr): {
511 struct in6_addr *addr6, *mask6;
512
Paul Moore13541b32008-01-29 08:44:23 -0500513 addr6 = (struct in6_addr *)addr;
514 mask6 = (struct in6_addr *)mask;
515 ret_val = netlbl_unlhsh_add_addr6(iface, addr6, mask6, secid);
516 if (audit_buf != NULL)
Paul Moore63c41682008-10-10 10:16:32 -0400517 netlbl_af6list_audit_addr(audit_buf, 1,
518 dev_name,
519 addr6, mask6);
Paul Moore8cc44572008-01-29 08:44:21 -0500520 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800521 }
Paul Moore8cc44572008-01-29 08:44:21 -0500522#endif /* IPv6 */
523 default:
524 ret_val = -EINVAL;
525 }
526 if (ret_val == 0)
527 atomic_inc(&netlabel_mgmt_protocount);
528
529unlhsh_add_return:
530 rcu_read_unlock();
Paul Moore13541b32008-01-29 08:44:23 -0500531 if (audit_buf != NULL) {
532 if (security_secid_to_secctx(secid,
533 &secctx,
534 &secctx_len) == 0) {
535 audit_log_format(audit_buf, " sec_obj=%s", secctx);
536 security_release_secctx(secctx, secctx_len);
537 }
538 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
539 audit_log_end(audit_buf);
540 }
Paul Moore8cc44572008-01-29 08:44:21 -0500541 return ret_val;
542}
543
544/**
545 * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry
Paul Moore13541b32008-01-29 08:44:23 -0500546 * @net: network namespace
Paul Moore8cc44572008-01-29 08:44:21 -0500547 * @iface: interface entry
548 * @addr: IP address
549 * @mask: IP address mask
Paul Moore13541b32008-01-29 08:44:23 -0500550 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500551 *
552 * Description:
553 * Remove an IP address entry from the unlabeled connection hash table.
554 * Returns zero on success, negative values on failure. The caller is
555 * responsible for calling the rcu_read_[un]lock() functions.
556 *
557 */
Paul Moore13541b32008-01-29 08:44:23 -0500558static int netlbl_unlhsh_remove_addr4(struct net *net,
559 struct netlbl_unlhsh_iface *iface,
Paul Moore8cc44572008-01-29 08:44:21 -0500560 const struct in_addr *addr,
Paul Moore13541b32008-01-29 08:44:23 -0500561 const struct in_addr *mask,
562 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500563{
Paul Moore61e10682008-10-10 10:16:32 -0400564 struct netlbl_af4list *list_entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500565 struct netlbl_unlhsh_addr4 *entry;
Paul Moore61e10682008-10-10 10:16:32 -0400566 struct audit_buffer *audit_buf;
Paul Moore13541b32008-01-29 08:44:23 -0500567 struct net_device *dev;
Paul Moore61e10682008-10-10 10:16:32 -0400568 char *secctx;
Paul Moore13541b32008-01-29 08:44:23 -0500569 u32 secctx_len;
Paul Moore8cc44572008-01-29 08:44:21 -0500570
571 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400572 list_entry = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
573 &iface->addr4_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500574 spin_unlock(&netlbl_unlhsh_lock);
Paul Moored25830e2008-12-03 00:37:04 -0800575 if (list_entry != NULL)
576 entry = netlbl_unlhsh_addr4_entry(list_entry);
577 else
Paul Mooreec8f2372008-12-11 21:31:50 -0800578 entry = NULL;
Paul Moore8cc44572008-01-29 08:44:21 -0500579
Paul Moore13541b32008-01-29 08:44:23 -0500580 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
581 audit_info);
582 if (audit_buf != NULL) {
583 dev = dev_get_by_index(net, iface->ifindex);
Paul Moore63c41682008-10-10 10:16:32 -0400584 netlbl_af4list_audit_addr(audit_buf, 1,
585 (dev != NULL ? dev->name : NULL),
586 addr->s_addr, mask->s_addr);
Paul Moore13541b32008-01-29 08:44:23 -0500587 if (dev != NULL)
588 dev_put(dev);
Paul Mooreec8f2372008-12-11 21:31:50 -0800589 if (entry != NULL &&
590 security_secid_to_secctx(entry->secid,
591 &secctx, &secctx_len) == 0) {
Paul Moore13541b32008-01-29 08:44:23 -0500592 audit_log_format(audit_buf, " sec_obj=%s", secctx);
593 security_release_secctx(secctx, secctx_len);
594 }
Paul Mooreec8f2372008-12-11 21:31:50 -0800595 audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
Paul Moore13541b32008-01-29 08:44:23 -0500596 audit_log_end(audit_buf);
597 }
598
Paul Mooreec8f2372008-12-11 21:31:50 -0800599 if (entry == NULL)
600 return -ENOENT;
601
602 call_rcu(&entry->rcu, netlbl_unlhsh_free_addr4);
603 return 0;
Paul Moore8cc44572008-01-29 08:44:21 -0500604}
605
606#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
607/**
608 * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry
Paul Moore13541b32008-01-29 08:44:23 -0500609 * @net: network namespace
Paul Moore8cc44572008-01-29 08:44:21 -0500610 * @iface: interface entry
611 * @addr: IP address
612 * @mask: IP address mask
Paul Moore13541b32008-01-29 08:44:23 -0500613 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500614 *
615 * Description:
616 * Remove an IP address entry from the unlabeled connection hash table.
617 * Returns zero on success, negative values on failure. The caller is
618 * responsible for calling the rcu_read_[un]lock() functions.
619 *
620 */
Paul Moore13541b32008-01-29 08:44:23 -0500621static int netlbl_unlhsh_remove_addr6(struct net *net,
622 struct netlbl_unlhsh_iface *iface,
Paul Moore8cc44572008-01-29 08:44:21 -0500623 const struct in6_addr *addr,
Paul Moore13541b32008-01-29 08:44:23 -0500624 const struct in6_addr *mask,
625 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500626{
Paul Moore61e10682008-10-10 10:16:32 -0400627 struct netlbl_af6list *list_entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500628 struct netlbl_unlhsh_addr6 *entry;
Paul Moore61e10682008-10-10 10:16:32 -0400629 struct audit_buffer *audit_buf;
Paul Moore13541b32008-01-29 08:44:23 -0500630 struct net_device *dev;
Paul Moore61e10682008-10-10 10:16:32 -0400631 char *secctx;
Paul Moore13541b32008-01-29 08:44:23 -0500632 u32 secctx_len;
Paul Moore8cc44572008-01-29 08:44:21 -0500633
634 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400635 list_entry = netlbl_af6list_remove(addr, mask, &iface->addr6_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500636 spin_unlock(&netlbl_unlhsh_lock);
Paul Moored25830e2008-12-03 00:37:04 -0800637 if (list_entry != NULL)
638 entry = netlbl_unlhsh_addr6_entry(list_entry);
639 else
Paul Mooreec8f2372008-12-11 21:31:50 -0800640 entry = NULL;
Paul Moore8cc44572008-01-29 08:44:21 -0500641
Paul Moore13541b32008-01-29 08:44:23 -0500642 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
643 audit_info);
644 if (audit_buf != NULL) {
645 dev = dev_get_by_index(net, iface->ifindex);
Paul Moore63c41682008-10-10 10:16:32 -0400646 netlbl_af6list_audit_addr(audit_buf, 1,
647 (dev != NULL ? dev->name : NULL),
648 addr, mask);
Paul Moore13541b32008-01-29 08:44:23 -0500649 if (dev != NULL)
650 dev_put(dev);
Paul Mooreec8f2372008-12-11 21:31:50 -0800651 if (entry != NULL &&
652 security_secid_to_secctx(entry->secid,
653 &secctx, &secctx_len) == 0) {
Paul Moore13541b32008-01-29 08:44:23 -0500654 audit_log_format(audit_buf, " sec_obj=%s", secctx);
655 security_release_secctx(secctx, secctx_len);
656 }
Paul Mooreec8f2372008-12-11 21:31:50 -0800657 audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
Paul Moore13541b32008-01-29 08:44:23 -0500658 audit_log_end(audit_buf);
659 }
660
Paul Mooreec8f2372008-12-11 21:31:50 -0800661 if (entry == NULL)
662 return -ENOENT;
663
664 call_rcu(&entry->rcu, netlbl_unlhsh_free_addr6);
665 return 0;
Paul Moore8cc44572008-01-29 08:44:21 -0500666}
667#endif /* IPv6 */
668
669/**
670 * netlbl_unlhsh_condremove_iface - Remove an interface entry
671 * @iface: the interface entry
672 *
673 * Description:
674 * Remove an interface entry from the unlabeled connection hash table if it is
675 * empty. An interface entry is considered to be empty if there are no
676 * address entries assigned to it.
677 *
678 */
679static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface)
680{
Paul Moore61e10682008-10-10 10:16:32 -0400681 struct netlbl_af4list *iter4;
682#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
683 struct netlbl_af6list *iter6;
684#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500685
686 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400687 netlbl_af4list_foreach_rcu(iter4, &iface->addr4_list)
688 goto unlhsh_condremove_failure;
689#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
690 netlbl_af6list_foreach_rcu(iter6, &iface->addr6_list)
691 goto unlhsh_condremove_failure;
692#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500693 iface->valid = 0;
694 if (iface->ifindex > 0)
695 list_del_rcu(&iface->list);
696 else
697 rcu_assign_pointer(netlbl_unlhsh_def, NULL);
698 spin_unlock(&netlbl_unlhsh_lock);
699
700 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
701 return;
702
703unlhsh_condremove_failure:
704 spin_unlock(&netlbl_unlhsh_lock);
705 return;
706}
707
708/**
709 * netlbl_unlhsh_remove - Remove an entry from the unlabeled hash table
710 * @net: network namespace
711 * @dev_name: interface name
712 * @addr: IP address in network byte order
713 * @mask: address mask in network byte order
714 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
Paul Moore13541b32008-01-29 08:44:23 -0500715 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500716 *
717 * Description:
718 * Removes and existing entry from the unlabeled connection hash table.
719 * Returns zero on success, negative values on failure.
720 *
721 */
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500722int netlbl_unlhsh_remove(struct net *net,
723 const char *dev_name,
724 const void *addr,
725 const void *mask,
726 u32 addr_len,
727 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500728{
729 int ret_val;
730 struct net_device *dev;
731 struct netlbl_unlhsh_iface *iface;
732
733 if (addr_len != sizeof(struct in_addr) &&
734 addr_len != sizeof(struct in6_addr))
735 return -EINVAL;
736
737 rcu_read_lock();
738 if (dev_name != NULL) {
Eric Dumazet122ec6f2009-11-05 20:53:47 -0800739 dev = dev_get_by_name_rcu(net, dev_name);
Paul Moore8cc44572008-01-29 08:44:21 -0500740 if (dev == NULL) {
741 ret_val = -ENODEV;
742 goto unlhsh_remove_return;
743 }
744 iface = netlbl_unlhsh_search_iface(dev->ifindex);
Paul Moore8cc44572008-01-29 08:44:21 -0500745 } else
746 iface = rcu_dereference(netlbl_unlhsh_def);
747 if (iface == NULL) {
748 ret_val = -ENOENT;
749 goto unlhsh_remove_return;
750 }
751 switch (addr_len) {
752 case sizeof(struct in_addr):
Paul Moore13541b32008-01-29 08:44:23 -0500753 ret_val = netlbl_unlhsh_remove_addr4(net,
754 iface, addr, mask,
755 audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500756 break;
757#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
758 case sizeof(struct in6_addr):
Paul Moore13541b32008-01-29 08:44:23 -0500759 ret_val = netlbl_unlhsh_remove_addr6(net,
760 iface, addr, mask,
761 audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500762 break;
763#endif /* IPv6 */
764 default:
765 ret_val = -EINVAL;
766 }
767 if (ret_val == 0) {
768 netlbl_unlhsh_condremove_iface(iface);
769 atomic_dec(&netlabel_mgmt_protocount);
770 }
771
772unlhsh_remove_return:
773 rcu_read_unlock();
774 return ret_val;
775}
776
777/*
778 * General Helper Functions
779 */
780
781/**
782 * netlbl_unlhsh_netdev_handler - Network device notification handler
783 * @this: notifier block
784 * @event: the event
785 * @ptr: the network device (cast to void)
786 *
787 * Description:
788 * Handle network device events, although at present all we care about is a
789 * network device going away. In the case of a device going away we clear any
790 * related entries from the unlabeled connection hash table.
791 *
792 */
793static int netlbl_unlhsh_netdev_handler(struct notifier_block *this,
794 unsigned long event,
795 void *ptr)
796{
797 struct net_device *dev = ptr;
798 struct netlbl_unlhsh_iface *iface = NULL;
799
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700800 if (!net_eq(dev_net(dev), &init_net))
Paul Moore8cc44572008-01-29 08:44:21 -0500801 return NOTIFY_DONE;
802
803 /* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */
804 if (event == NETDEV_DOWN) {
805 spin_lock(&netlbl_unlhsh_lock);
806 iface = netlbl_unlhsh_search_iface(dev->ifindex);
807 if (iface != NULL && iface->valid) {
808 iface->valid = 0;
809 list_del_rcu(&iface->list);
810 } else
811 iface = NULL;
812 spin_unlock(&netlbl_unlhsh_lock);
813 }
814
815 if (iface != NULL)
816 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
817
818 return NOTIFY_DONE;
819}
820
821/**
Paul Moore32f50cd2006-09-28 14:51:47 -0700822 * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
823 * @value: desired value
Paul Moore95d4e6b2006-09-29 17:05:05 -0700824 * @audit_info: NetLabel audit information
Paul Moore32f50cd2006-09-28 14:51:47 -0700825 *
826 * Description:
827 * Set the value of the unlabeled accept flag to @value.
828 *
829 */
Paul Moore95d4e6b2006-09-29 17:05:05 -0700830static void netlbl_unlabel_acceptflg_set(u8 value,
831 struct netlbl_audit *audit_info)
Paul Moore32f50cd2006-09-28 14:51:47 -0700832{
Paul Moore95d4e6b2006-09-29 17:05:05 -0700833 struct audit_buffer *audit_buf;
834 u8 old_val;
835
Paul Moore4be27002007-10-26 04:29:08 -0700836 old_val = netlabel_unlabel_acceptflg;
Paul Moorecd287862006-11-17 17:38:44 -0500837 netlabel_unlabel_acceptflg = value;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700838 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
839 audit_info);
Paul Moorede646882006-11-17 17:38:55 -0500840 if (audit_buf != NULL) {
841 audit_log_format(audit_buf,
842 " unlbl_accept=%u old=%u", value, old_val);
843 audit_log_end(audit_buf);
844 }
Paul Moore32f50cd2006-09-28 14:51:47 -0700845}
846
Paul Moore8cc44572008-01-29 08:44:21 -0500847/**
848 * netlbl_unlabel_addrinfo_get - Get the IPv4/6 address information
849 * @info: the Generic NETLINK info block
850 * @addr: the IP address
851 * @mask: the IP address mask
852 * @len: the address length
853 *
854 * Description:
855 * Examine the Generic NETLINK message and extract the IP address information.
856 * Returns zero on success, negative values on failure.
857 *
858 */
859static int netlbl_unlabel_addrinfo_get(struct genl_info *info,
860 void **addr,
861 void **mask,
862 u32 *len)
863{
864 u32 addr_len;
865
866 if (info->attrs[NLBL_UNLABEL_A_IPV4ADDR]) {
867 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
868 if (addr_len != sizeof(struct in_addr) &&
869 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK]))
870 return -EINVAL;
871 *len = addr_len;
872 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
873 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]);
874 return 0;
875 } else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) {
876 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
877 if (addr_len != sizeof(struct in6_addr) &&
878 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK]))
879 return -EINVAL;
880 *len = addr_len;
881 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
882 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]);
883 return 0;
884 }
885
886 return -EINVAL;
887}
888
Paul Moore32f50cd2006-09-28 14:51:47 -0700889/*
Paul Moore96cb8e32006-08-03 16:48:59 -0700890 * NetLabel Command Handlers
891 */
892
893/**
894 * netlbl_unlabel_accept - Handle an ACCEPT message
895 * @skb: the NETLINK buffer
896 * @info: the Generic NETLINK info block
897 *
898 * Description:
899 * Process a user generated ACCEPT message and set the accept flag accordingly.
900 * Returns zero on success, negative values on failure.
901 *
902 */
903static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
904{
Paul Moorefd385852006-09-25 15:56:37 -0700905 u8 value;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700906 struct netlbl_audit audit_info;
Paul Moore96cb8e32006-08-03 16:48:59 -0700907
Paul Moorefd385852006-09-25 15:56:37 -0700908 if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
909 value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
Paul Moore96cb8e32006-08-03 16:48:59 -0700910 if (value == 1 || value == 0) {
Paul Moore95d4e6b2006-09-29 17:05:05 -0700911 netlbl_netlink_auditinfo(skb, &audit_info);
912 netlbl_unlabel_acceptflg_set(value, &audit_info);
Paul Moore32f50cd2006-09-28 14:51:47 -0700913 return 0;
Paul Moore96cb8e32006-08-03 16:48:59 -0700914 }
915 }
916
Paul Moore32f50cd2006-09-28 14:51:47 -0700917 return -EINVAL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700918}
919
920/**
921 * netlbl_unlabel_list - Handle a LIST message
922 * @skb: the NETLINK buffer
923 * @info: the Generic NETLINK info block
924 *
925 * Description:
926 * Process a user generated LIST message and respond with the current status.
927 * Returns zero on success, negative values on failure.
928 *
929 */
930static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
931{
Paul Moorefd385852006-09-25 15:56:37 -0700932 int ret_val = -EINVAL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700933 struct sk_buff *ans_skb;
Paul Moorefd385852006-09-25 15:56:37 -0700934 void *data;
Paul Moore96cb8e32006-08-03 16:48:59 -0700935
Thomas Graf339bf982006-11-10 14:10:15 -0800936 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Paul Moore96cb8e32006-08-03 16:48:59 -0700937 if (ans_skb == NULL)
938 goto list_failure;
Thomas Graf17c157c2006-11-14 19:46:02 -0800939 data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
940 0, NLBL_UNLABEL_C_LIST);
Paul Moorefd385852006-09-25 15:56:37 -0700941 if (data == NULL) {
942 ret_val = -ENOMEM;
Paul Moore96cb8e32006-08-03 16:48:59 -0700943 goto list_failure;
Paul Moorefd385852006-09-25 15:56:37 -0700944 }
Paul Moore96cb8e32006-08-03 16:48:59 -0700945
Paul Moorefd385852006-09-25 15:56:37 -0700946 ret_val = nla_put_u8(ans_skb,
947 NLBL_UNLABEL_A_ACPTFLG,
Paul Moorecd287862006-11-17 17:38:44 -0500948 netlabel_unlabel_acceptflg);
Paul Moore96cb8e32006-08-03 16:48:59 -0700949 if (ret_val != 0)
950 goto list_failure;
951
Paul Moorefd385852006-09-25 15:56:37 -0700952 genlmsg_end(ans_skb, data);
Denis V. Lunevfe785be2008-07-10 16:53:39 -0700953 return genlmsg_reply(ans_skb, info);
Paul Moore96cb8e32006-08-03 16:48:59 -0700954
955list_failure:
Patrick McHardyb08d5842007-02-27 09:57:37 -0800956 kfree_skb(ans_skb);
Paul Moore96cb8e32006-08-03 16:48:59 -0700957 return ret_val;
958}
959
Paul Moore8cc44572008-01-29 08:44:21 -0500960/**
961 * netlbl_unlabel_staticadd - Handle a STATICADD message
962 * @skb: the NETLINK buffer
963 * @info: the Generic NETLINK info block
964 *
965 * Description:
966 * Process a user generated STATICADD message and add a new unlabeled
967 * connection entry to the hash table. Returns zero on success, negative
968 * values on failure.
969 *
970 */
971static int netlbl_unlabel_staticadd(struct sk_buff *skb,
972 struct genl_info *info)
973{
974 int ret_val;
975 char *dev_name;
976 void *addr;
977 void *mask;
978 u32 addr_len;
979 u32 secid;
Paul Moore13541b32008-01-29 08:44:23 -0500980 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -0500981
982 /* Don't allow users to add both IPv4 and IPv6 addresses for a
983 * single entry. However, allow users to create two entries, one each
984 * for IPv4 and IPv4, with the same LSM security context which should
985 * achieve the same result. */
986 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
987 !info->attrs[NLBL_UNLABEL_A_IFACE] ||
988 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
989 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
990 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
991 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
992 return -EINVAL;
993
Paul Moore13541b32008-01-29 08:44:23 -0500994 netlbl_netlink_auditinfo(skb, &audit_info);
995
Paul Moore8cc44572008-01-29 08:44:21 -0500996 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
997 if (ret_val != 0)
998 return ret_val;
999 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
1000 ret_val = security_secctx_to_secid(
1001 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1002 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1003 &secid);
1004 if (ret_val != 0)
1005 return ret_val;
1006
1007 return netlbl_unlhsh_add(&init_net,
Paul Moore13541b32008-01-29 08:44:23 -05001008 dev_name, addr, mask, addr_len, secid,
1009 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -05001010}
1011
1012/**
1013 * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message
1014 * @skb: the NETLINK buffer
1015 * @info: the Generic NETLINK info block
1016 *
1017 * Description:
1018 * Process a user generated STATICADDDEF message and add a new default
1019 * unlabeled connection entry. Returns zero on success, negative values on
1020 * failure.
1021 *
1022 */
1023static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
1024 struct genl_info *info)
1025{
1026 int ret_val;
1027 void *addr;
1028 void *mask;
1029 u32 addr_len;
1030 u32 secid;
Paul Moore13541b32008-01-29 08:44:23 -05001031 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -05001032
1033 /* Don't allow users to add both IPv4 and IPv6 addresses for a
1034 * single entry. However, allow users to create two entries, one each
1035 * for IPv4 and IPv6, with the same LSM security context which should
1036 * achieve the same result. */
1037 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
1038 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1039 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1040 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1041 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1042 return -EINVAL;
1043
Paul Moore13541b32008-01-29 08:44:23 -05001044 netlbl_netlink_auditinfo(skb, &audit_info);
1045
Paul Moore8cc44572008-01-29 08:44:21 -05001046 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1047 if (ret_val != 0)
1048 return ret_val;
1049 ret_val = security_secctx_to_secid(
1050 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1051 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1052 &secid);
1053 if (ret_val != 0)
1054 return ret_val;
1055
Paul Moore13541b32008-01-29 08:44:23 -05001056 return netlbl_unlhsh_add(&init_net,
1057 NULL, addr, mask, addr_len, secid,
1058 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -05001059}
1060
1061/**
1062 * netlbl_unlabel_staticremove - Handle a STATICREMOVE message
1063 * @skb: the NETLINK buffer
1064 * @info: the Generic NETLINK info block
1065 *
1066 * Description:
1067 * Process a user generated STATICREMOVE message and remove the specified
1068 * unlabeled connection entry. Returns zero on success, negative values on
1069 * failure.
1070 *
1071 */
1072static int netlbl_unlabel_staticremove(struct sk_buff *skb,
1073 struct genl_info *info)
1074{
1075 int ret_val;
1076 char *dev_name;
1077 void *addr;
1078 void *mask;
1079 u32 addr_len;
Paul Moore13541b32008-01-29 08:44:23 -05001080 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -05001081
1082 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1083 * IPv4 and IPv6 in the same entry. */
1084 if (!info->attrs[NLBL_UNLABEL_A_IFACE] ||
1085 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1086 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1087 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1088 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1089 return -EINVAL;
1090
Paul Moore13541b32008-01-29 08:44:23 -05001091 netlbl_netlink_auditinfo(skb, &audit_info);
1092
Paul Moore8cc44572008-01-29 08:44:21 -05001093 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1094 if (ret_val != 0)
1095 return ret_val;
1096 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
1097
Paul Moore13541b32008-01-29 08:44:23 -05001098 return netlbl_unlhsh_remove(&init_net,
1099 dev_name, addr, mask, addr_len,
1100 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -05001101}
1102
1103/**
1104 * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message
1105 * @skb: the NETLINK buffer
1106 * @info: the Generic NETLINK info block
1107 *
1108 * Description:
1109 * Process a user generated STATICREMOVEDEF message and remove the default
1110 * unlabeled connection entry. Returns zero on success, negative values on
1111 * failure.
1112 *
1113 */
1114static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
1115 struct genl_info *info)
1116{
1117 int ret_val;
1118 void *addr;
1119 void *mask;
1120 u32 addr_len;
Paul Moore13541b32008-01-29 08:44:23 -05001121 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -05001122
1123 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1124 * IPv4 and IPv6 in the same entry. */
1125 if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1126 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1127 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1128 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1129 return -EINVAL;
1130
Paul Moore13541b32008-01-29 08:44:23 -05001131 netlbl_netlink_auditinfo(skb, &audit_info);
1132
Paul Moore8cc44572008-01-29 08:44:21 -05001133 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1134 if (ret_val != 0)
1135 return ret_val;
1136
Paul Moore13541b32008-01-29 08:44:23 -05001137 return netlbl_unlhsh_remove(&init_net,
1138 NULL, addr, mask, addr_len,
1139 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -05001140}
1141
1142
1143/**
1144 * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF]
1145 * @cmd: command/message
1146 * @iface: the interface entry
1147 * @addr4: the IPv4 address entry
1148 * @addr6: the IPv6 address entry
1149 * @arg: the netlbl_unlhsh_walk_arg structure
1150 *
1151 * Description:
1152 * This function is designed to be used to generate a response for a
1153 * STATICLIST or STATICLISTDEF message. When called either @addr4 or @addr6
1154 * can be specified, not both, the other unspecified entry should be set to
1155 * NULL by the caller. Returns the size of the message on success, negative
1156 * values on failure.
1157 *
1158 */
1159static int netlbl_unlabel_staticlist_gen(u32 cmd,
1160 const struct netlbl_unlhsh_iface *iface,
1161 const struct netlbl_unlhsh_addr4 *addr4,
1162 const struct netlbl_unlhsh_addr6 *addr6,
1163 void *arg)
1164{
1165 int ret_val = -ENOMEM;
1166 struct netlbl_unlhsh_walk_arg *cb_arg = arg;
1167 struct net_device *dev;
1168 void *data;
1169 u32 secid;
1170 char *secctx;
1171 u32 secctx_len;
1172
1173 data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
1174 cb_arg->seq, &netlbl_unlabel_gnl_family,
1175 NLM_F_MULTI, cmd);
1176 if (data == NULL)
1177 goto list_cb_failure;
1178
1179 if (iface->ifindex > 0) {
1180 dev = dev_get_by_index(&init_net, iface->ifindex);
Jesper Juhl794eb6b2008-04-17 23:22:54 -07001181 if (!dev) {
1182 ret_val = -ENODEV;
1183 goto list_cb_failure;
1184 }
Paul Moore8cc44572008-01-29 08:44:21 -05001185 ret_val = nla_put_string(cb_arg->skb,
1186 NLBL_UNLABEL_A_IFACE, dev->name);
1187 dev_put(dev);
1188 if (ret_val != 0)
1189 goto list_cb_failure;
1190 }
1191
1192 if (addr4) {
1193 struct in_addr addr_struct;
1194
Paul Moore61e10682008-10-10 10:16:32 -04001195 addr_struct.s_addr = addr4->list.addr;
Paul Moore8cc44572008-01-29 08:44:21 -05001196 ret_val = nla_put(cb_arg->skb,
1197 NLBL_UNLABEL_A_IPV4ADDR,
1198 sizeof(struct in_addr),
1199 &addr_struct);
1200 if (ret_val != 0)
1201 goto list_cb_failure;
1202
Paul Moore61e10682008-10-10 10:16:32 -04001203 addr_struct.s_addr = addr4->list.mask;
Paul Moore8cc44572008-01-29 08:44:21 -05001204 ret_val = nla_put(cb_arg->skb,
1205 NLBL_UNLABEL_A_IPV4MASK,
1206 sizeof(struct in_addr),
1207 &addr_struct);
1208 if (ret_val != 0)
1209 goto list_cb_failure;
1210
1211 secid = addr4->secid;
1212 } else {
1213 ret_val = nla_put(cb_arg->skb,
1214 NLBL_UNLABEL_A_IPV6ADDR,
1215 sizeof(struct in6_addr),
Paul Moore61e10682008-10-10 10:16:32 -04001216 &addr6->list.addr);
Paul Moore8cc44572008-01-29 08:44:21 -05001217 if (ret_val != 0)
1218 goto list_cb_failure;
1219
1220 ret_val = nla_put(cb_arg->skb,
1221 NLBL_UNLABEL_A_IPV6MASK,
1222 sizeof(struct in6_addr),
Paul Moore61e10682008-10-10 10:16:32 -04001223 &addr6->list.mask);
Paul Moore8cc44572008-01-29 08:44:21 -05001224 if (ret_val != 0)
1225 goto list_cb_failure;
1226
1227 secid = addr6->secid;
1228 }
1229
1230 ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len);
1231 if (ret_val != 0)
1232 goto list_cb_failure;
1233 ret_val = nla_put(cb_arg->skb,
1234 NLBL_UNLABEL_A_SECCTX,
1235 secctx_len,
1236 secctx);
1237 security_release_secctx(secctx, secctx_len);
1238 if (ret_val != 0)
1239 goto list_cb_failure;
1240
1241 cb_arg->seq++;
1242 return genlmsg_end(cb_arg->skb, data);
1243
1244list_cb_failure:
1245 genlmsg_cancel(cb_arg->skb, data);
1246 return ret_val;
1247}
1248
1249/**
1250 * netlbl_unlabel_staticlist - Handle a STATICLIST message
1251 * @skb: the NETLINK buffer
1252 * @cb: the NETLINK callback
1253 *
1254 * Description:
1255 * Process a user generated STATICLIST message and dump the unlabeled
1256 * connection hash table in a form suitable for use in a kernel generated
1257 * STATICLIST message. Returns the length of @skb.
1258 *
1259 */
1260static int netlbl_unlabel_staticlist(struct sk_buff *skb,
1261 struct netlink_callback *cb)
1262{
1263 struct netlbl_unlhsh_walk_arg cb_arg;
1264 u32 skip_bkt = cb->args[0];
1265 u32 skip_chain = cb->args[1];
1266 u32 skip_addr4 = cb->args[2];
1267 u32 skip_addr6 = cb->args[3];
1268 u32 iter_bkt;
1269 u32 iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
1270 struct netlbl_unlhsh_iface *iface;
Paul Moore56196702008-10-10 10:16:29 -04001271 struct list_head *iter_list;
Paul Moore61e10682008-10-10 10:16:32 -04001272 struct netlbl_af4list *addr4;
1273#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1274 struct netlbl_af6list *addr6;
1275#endif
Paul Moore8cc44572008-01-29 08:44:21 -05001276
1277 cb_arg.nl_cb = cb;
1278 cb_arg.skb = skb;
1279 cb_arg.seq = cb->nlh->nlmsg_seq;
1280
1281 rcu_read_lock();
1282 for (iter_bkt = skip_bkt;
1283 iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
1284 iter_bkt++, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0) {
Paul Moore56196702008-10-10 10:16:29 -04001285 iter_list = &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt];
1286 list_for_each_entry_rcu(iface, iter_list, list) {
Paul Moore8cc44572008-01-29 08:44:21 -05001287 if (!iface->valid ||
1288 iter_chain++ < skip_chain)
1289 continue;
Paul Moore61e10682008-10-10 10:16:32 -04001290 netlbl_af4list_foreach_rcu(addr4,
1291 &iface->addr4_list) {
1292 if (iter_addr4++ < skip_addr4)
Paul Moore8cc44572008-01-29 08:44:21 -05001293 continue;
1294 if (netlbl_unlabel_staticlist_gen(
Paul Moore61e10682008-10-10 10:16:32 -04001295 NLBL_UNLABEL_C_STATICLIST,
1296 iface,
1297 netlbl_unlhsh_addr4_entry(addr4),
1298 NULL,
1299 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001300 iter_addr4--;
1301 iter_chain--;
1302 goto unlabel_staticlist_return;
1303 }
1304 }
Paul Moore61e10682008-10-10 10:16:32 -04001305#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1306 netlbl_af6list_foreach_rcu(addr6,
1307 &iface->addr6_list) {
1308 if (iter_addr6++ < skip_addr6)
Paul Moore8cc44572008-01-29 08:44:21 -05001309 continue;
1310 if (netlbl_unlabel_staticlist_gen(
Paul Moore61e10682008-10-10 10:16:32 -04001311 NLBL_UNLABEL_C_STATICLIST,
1312 iface,
1313 NULL,
1314 netlbl_unlhsh_addr6_entry(addr6),
1315 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001316 iter_addr6--;
1317 iter_chain--;
1318 goto unlabel_staticlist_return;
1319 }
1320 }
Paul Moore61e10682008-10-10 10:16:32 -04001321#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -05001322 }
1323 }
1324
1325unlabel_staticlist_return:
1326 rcu_read_unlock();
1327 cb->args[0] = skip_bkt;
1328 cb->args[1] = skip_chain;
1329 cb->args[2] = skip_addr4;
1330 cb->args[3] = skip_addr6;
1331 return skb->len;
1332}
1333
1334/**
1335 * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message
1336 * @skb: the NETLINK buffer
1337 * @cb: the NETLINK callback
1338 *
1339 * Description:
1340 * Process a user generated STATICLISTDEF message and dump the default
1341 * unlabeled connection entry in a form suitable for use in a kernel generated
1342 * STATICLISTDEF message. Returns the length of @skb.
1343 *
1344 */
1345static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
1346 struct netlink_callback *cb)
1347{
1348 struct netlbl_unlhsh_walk_arg cb_arg;
1349 struct netlbl_unlhsh_iface *iface;
1350 u32 skip_addr4 = cb->args[0];
1351 u32 skip_addr6 = cb->args[1];
Paul Moore61e10682008-10-10 10:16:32 -04001352 u32 iter_addr4 = 0;
1353 struct netlbl_af4list *addr4;
1354#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1355 u32 iter_addr6 = 0;
1356 struct netlbl_af6list *addr6;
1357#endif
Paul Moore8cc44572008-01-29 08:44:21 -05001358
1359 cb_arg.nl_cb = cb;
1360 cb_arg.skb = skb;
1361 cb_arg.seq = cb->nlh->nlmsg_seq;
1362
1363 rcu_read_lock();
1364 iface = rcu_dereference(netlbl_unlhsh_def);
1365 if (iface == NULL || !iface->valid)
1366 goto unlabel_staticlistdef_return;
1367
Paul Moore61e10682008-10-10 10:16:32 -04001368 netlbl_af4list_foreach_rcu(addr4, &iface->addr4_list) {
1369 if (iter_addr4++ < skip_addr4)
Paul Moore8cc44572008-01-29 08:44:21 -05001370 continue;
1371 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
Paul Moore61e10682008-10-10 10:16:32 -04001372 iface,
1373 netlbl_unlhsh_addr4_entry(addr4),
1374 NULL,
1375 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001376 iter_addr4--;
1377 goto unlabel_staticlistdef_return;
1378 }
1379 }
Paul Moore61e10682008-10-10 10:16:32 -04001380#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1381 netlbl_af6list_foreach_rcu(addr6, &iface->addr6_list) {
1382 if (iter_addr6++ < skip_addr6)
Paul Moore8cc44572008-01-29 08:44:21 -05001383 continue;
1384 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
Paul Moore61e10682008-10-10 10:16:32 -04001385 iface,
1386 NULL,
1387 netlbl_unlhsh_addr6_entry(addr6),
1388 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001389 iter_addr6--;
1390 goto unlabel_staticlistdef_return;
1391 }
1392 }
Paul Moore61e10682008-10-10 10:16:32 -04001393#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -05001394
1395unlabel_staticlistdef_return:
1396 rcu_read_unlock();
1397 cb->args[0] = skip_addr4;
1398 cb->args[1] = skip_addr6;
1399 return skb->len;
1400}
Paul Moore96cb8e32006-08-03 16:48:59 -07001401
1402/*
1403 * NetLabel Generic NETLINK Command Definitions
1404 */
1405
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001406static struct genl_ops netlbl_unlabel_genl_ops[] = {
1407 {
Paul Moore8cc44572008-01-29 08:44:21 -05001408 .cmd = NLBL_UNLABEL_C_STATICADD,
1409 .flags = GENL_ADMIN_PERM,
1410 .policy = netlbl_unlabel_genl_policy,
1411 .doit = netlbl_unlabel_staticadd,
1412 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001413 },
1414 {
Paul Moore8cc44572008-01-29 08:44:21 -05001415 .cmd = NLBL_UNLABEL_C_STATICREMOVE,
1416 .flags = GENL_ADMIN_PERM,
1417 .policy = netlbl_unlabel_genl_policy,
1418 .doit = netlbl_unlabel_staticremove,
1419 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001420 },
1421 {
Paul Moore8cc44572008-01-29 08:44:21 -05001422 .cmd = NLBL_UNLABEL_C_STATICLIST,
1423 .flags = 0,
1424 .policy = netlbl_unlabel_genl_policy,
1425 .doit = NULL,
1426 .dumpit = netlbl_unlabel_staticlist,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001427 },
1428 {
Paul Moore8cc44572008-01-29 08:44:21 -05001429 .cmd = NLBL_UNLABEL_C_STATICADDDEF,
1430 .flags = GENL_ADMIN_PERM,
1431 .policy = netlbl_unlabel_genl_policy,
1432 .doit = netlbl_unlabel_staticadddef,
1433 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001434 },
1435 {
Paul Moore8cc44572008-01-29 08:44:21 -05001436 .cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
1437 .flags = GENL_ADMIN_PERM,
1438 .policy = netlbl_unlabel_genl_policy,
1439 .doit = netlbl_unlabel_staticremovedef,
1440 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001441 },
1442 {
Paul Moore8cc44572008-01-29 08:44:21 -05001443 .cmd = NLBL_UNLABEL_C_STATICLISTDEF,
1444 .flags = 0,
1445 .policy = netlbl_unlabel_genl_policy,
1446 .doit = NULL,
1447 .dumpit = netlbl_unlabel_staticlistdef,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001448 },
1449 {
Paul Moore96cb8e32006-08-03 16:48:59 -07001450 .cmd = NLBL_UNLABEL_C_ACCEPT,
Paul Moorefd385852006-09-25 15:56:37 -07001451 .flags = GENL_ADMIN_PERM,
1452 .policy = netlbl_unlabel_genl_policy,
Paul Moore96cb8e32006-08-03 16:48:59 -07001453 .doit = netlbl_unlabel_accept,
1454 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001455 },
1456 {
Paul Moore96cb8e32006-08-03 16:48:59 -07001457 .cmd = NLBL_UNLABEL_C_LIST,
1458 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -07001459 .policy = netlbl_unlabel_genl_policy,
Paul Moore96cb8e32006-08-03 16:48:59 -07001460 .doit = netlbl_unlabel_list,
1461 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001462 },
Paul Moore96cb8e32006-08-03 16:48:59 -07001463};
1464
Paul Moore96cb8e32006-08-03 16:48:59 -07001465/*
1466 * NetLabel Generic NETLINK Protocol Functions
1467 */
1468
1469/**
1470 * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
1471 *
1472 * Description:
1473 * Register the unlabeled packet NetLabel component with the Generic NETLINK
1474 * mechanism. Returns zero on success, negative values on failure.
1475 *
1476 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -08001477int __init netlbl_unlabel_genl_init(void)
Paul Moore96cb8e32006-08-03 16:48:59 -07001478{
Michał Mirosław7ae740d2009-05-21 10:34:05 +00001479 return genl_register_family_with_ops(&netlbl_unlabel_gnl_family,
1480 netlbl_unlabel_genl_ops, ARRAY_SIZE(netlbl_unlabel_genl_ops));
Paul Moore96cb8e32006-08-03 16:48:59 -07001481}
1482
1483/*
1484 * NetLabel KAPI Hooks
1485 */
1486
Paul Moore8cc44572008-01-29 08:44:21 -05001487static struct notifier_block netlbl_unlhsh_netdev_notifier = {
1488 .notifier_call = netlbl_unlhsh_netdev_handler,
1489};
1490
1491/**
1492 * netlbl_unlabel_init - Initialize the unlabeled connection hash table
1493 * @size: the number of bits to use for the hash buckets
1494 *
1495 * Description:
1496 * Initializes the unlabeled connection hash table and registers a network
1497 * device notification handler. This function should only be called by the
1498 * NetLabel subsystem itself during initialization. Returns zero on success,
1499 * non-zero values on error.
1500 *
1501 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -08001502int __init netlbl_unlabel_init(u32 size)
Paul Moore8cc44572008-01-29 08:44:21 -05001503{
1504 u32 iter;
1505 struct netlbl_unlhsh_tbl *hsh_tbl;
1506
1507 if (size == 0)
1508 return -EINVAL;
1509
1510 hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
1511 if (hsh_tbl == NULL)
1512 return -ENOMEM;
1513 hsh_tbl->size = 1 << size;
1514 hsh_tbl->tbl = kcalloc(hsh_tbl->size,
1515 sizeof(struct list_head),
1516 GFP_KERNEL);
1517 if (hsh_tbl->tbl == NULL) {
1518 kfree(hsh_tbl);
1519 return -ENOMEM;
1520 }
1521 for (iter = 0; iter < hsh_tbl->size; iter++)
1522 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
1523
1524 rcu_read_lock();
1525 spin_lock(&netlbl_unlhsh_lock);
1526 rcu_assign_pointer(netlbl_unlhsh, hsh_tbl);
1527 spin_unlock(&netlbl_unlhsh_lock);
1528 rcu_read_unlock();
1529
1530 register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier);
1531
1532 return 0;
1533}
1534
Paul Moore96cb8e32006-08-03 16:48:59 -07001535/**
1536 * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
Paul Moore8cc44572008-01-29 08:44:21 -05001537 * @skb: the packet
1538 * @family: protocol family
Paul Moore96cb8e32006-08-03 16:48:59 -07001539 * @secattr: the security attributes
1540 *
1541 * Description:
1542 * Determine the security attributes, if any, for an unlabled packet and return
1543 * them in @secattr. Returns zero on success and negative values on failure.
1544 *
1545 */
Paul Moore8cc44572008-01-29 08:44:21 -05001546int netlbl_unlabel_getattr(const struct sk_buff *skb,
1547 u16 family,
1548 struct netlbl_lsm_secattr *secattr)
Paul Moore96cb8e32006-08-03 16:48:59 -07001549{
Paul Moore8cc44572008-01-29 08:44:21 -05001550 struct netlbl_unlhsh_iface *iface;
1551
1552 rcu_read_lock();
1553 iface = netlbl_unlhsh_search_iface_def(skb->iif);
1554 if (iface == NULL)
1555 goto unlabel_getattr_nolabel;
1556 switch (family) {
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001557 case PF_INET: {
1558 struct iphdr *hdr4;
Paul Moore61e10682008-10-10 10:16:32 -04001559 struct netlbl_af4list *addr4;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001560
Paul Moore8cc44572008-01-29 08:44:21 -05001561 hdr4 = ip_hdr(skb);
Paul Moore61e10682008-10-10 10:16:32 -04001562 addr4 = netlbl_af4list_search(hdr4->saddr,
1563 &iface->addr4_list);
Paul Moore8cc44572008-01-29 08:44:21 -05001564 if (addr4 == NULL)
1565 goto unlabel_getattr_nolabel;
Paul Moore61e10682008-10-10 10:16:32 -04001566 secattr->attr.secid = netlbl_unlhsh_addr4_entry(addr4)->secid;
Paul Moore8cc44572008-01-29 08:44:21 -05001567 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001568 }
Paul Moore8cc44572008-01-29 08:44:21 -05001569#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001570 case PF_INET6: {
1571 struct ipv6hdr *hdr6;
Paul Moore61e10682008-10-10 10:16:32 -04001572 struct netlbl_af6list *addr6;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001573
Paul Moore8cc44572008-01-29 08:44:21 -05001574 hdr6 = ipv6_hdr(skb);
Paul Moore61e10682008-10-10 10:16:32 -04001575 addr6 = netlbl_af6list_search(&hdr6->saddr,
1576 &iface->addr6_list);
Paul Moore8cc44572008-01-29 08:44:21 -05001577 if (addr6 == NULL)
1578 goto unlabel_getattr_nolabel;
Paul Moore61e10682008-10-10 10:16:32 -04001579 secattr->attr.secid = netlbl_unlhsh_addr6_entry(addr6)->secid;
Paul Moore8cc44572008-01-29 08:44:21 -05001580 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001581 }
Paul Moore8cc44572008-01-29 08:44:21 -05001582#endif /* IPv6 */
1583 default:
1584 goto unlabel_getattr_nolabel;
1585 }
1586 rcu_read_unlock();
1587
1588 secattr->flags |= NETLBL_SECATTR_SECID;
1589 secattr->type = NETLBL_NLTYPE_UNLABELED;
1590 return 0;
1591
1592unlabel_getattr_nolabel:
1593 rcu_read_unlock();
Paul Moorec783f1c2008-01-29 08:37:52 -05001594 if (netlabel_unlabel_acceptflg == 0)
1595 return -ENOMSG;
Paul Moore16efd452008-01-29 08:37:59 -05001596 secattr->type = NETLBL_NLTYPE_UNLABELED;
Paul Moorec783f1c2008-01-29 08:37:52 -05001597 return 0;
Paul Moore96cb8e32006-08-03 16:48:59 -07001598}
1599
1600/**
1601 * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
1602 *
1603 * Description:
1604 * Set the default NetLabel configuration to allow incoming unlabeled packets
1605 * and to send unlabeled network traffic by default.
1606 *
1607 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -08001608int __init netlbl_unlabel_defconf(void)
Paul Moore96cb8e32006-08-03 16:48:59 -07001609{
1610 int ret_val;
1611 struct netlbl_dom_map *entry;
Paul Moore95d4e6b2006-09-29 17:05:05 -07001612 struct netlbl_audit audit_info;
Paul Moore32f50cd2006-09-28 14:51:47 -07001613
Paul Moore95d4e6b2006-09-29 17:05:05 -07001614 /* Only the kernel is allowed to call this function and the only time
1615 * it is called is at bootup before the audit subsystem is reporting
1616 * messages so don't worry to much about these values. */
1617 security_task_getsecid(current, &audit_info.secid);
1618 audit_info.loginuid = 0;
Eric Paris25323862008-04-18 10:09:25 -04001619 audit_info.sessionid = 0;
Paul Moore96cb8e32006-08-03 16:48:59 -07001620
1621 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1622 if (entry == NULL)
1623 return -ENOMEM;
1624 entry->type = NETLBL_NLTYPE_UNLABELED;
Paul Moore95d4e6b2006-09-29 17:05:05 -07001625 ret_val = netlbl_domhsh_add_default(entry, &audit_info);
Paul Moore96cb8e32006-08-03 16:48:59 -07001626 if (ret_val != 0)
1627 return ret_val;
1628
Paul Moore95d4e6b2006-09-29 17:05:05 -07001629 netlbl_unlabel_acceptflg_set(1, &audit_info);
Paul Moore96cb8e32006-08-03 16:48:59 -07001630
1631 return 0;
1632}