blob: a3d64aabe2f73d6bbd90b12fbbdb6ae908d34447 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Paul Moore96cb8e32006-08-03 16:48:59 -070047#include <net/sock.h>
48#include <net/netlink.h>
49#include <net/genetlink.h>
Paul Moore8cc44572008-01-29 08:44:21 -050050#include <net/ip.h>
51#include <net/ipv6.h>
52#include <net/net_namespace.h>
Paul Moore96cb8e32006-08-03 16:48:59 -070053#include <net/netlabel.h>
54#include <asm/bug.h>
Paul Moore8cc44572008-01-29 08:44:21 -050055#include <asm/atomic.h>
Paul Moore96cb8e32006-08-03 16:48:59 -070056
57#include "netlabel_user.h"
Paul Moore61e10682008-10-10 10:16:32 -040058#include "netlabel_addrlist.h"
Paul Moore96cb8e32006-08-03 16:48:59 -070059#include "netlabel_domainhash.h"
60#include "netlabel_unlabeled.h"
Paul Moore8cc44572008-01-29 08:44:21 -050061#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};
Paul Moore61e10682008-10-10 10:16:32 -040081#define netlbl_unlhsh_addr4_entry(iter) \
82 container_of(iter, struct netlbl_unlhsh_addr4, list)
Paul Moore8cc44572008-01-29 08:44:21 -050083struct netlbl_unlhsh_addr4 {
Paul Moore8cc44572008-01-29 08:44:21 -050084 u32 secid;
85
Paul Moore61e10682008-10-10 10:16:32 -040086 struct netlbl_af4list list;
Paul Moore8cc44572008-01-29 08:44:21 -050087 struct rcu_head rcu;
88};
Paul Moore61e10682008-10-10 10:16:32 -040089#define netlbl_unlhsh_addr6_entry(iter) \
90 container_of(iter, struct netlbl_unlhsh_addr6, list)
Paul Moore8cc44572008-01-29 08:44:21 -050091struct netlbl_unlhsh_addr6 {
Paul Moore8cc44572008-01-29 08:44:21 -050092 u32 secid;
93
Paul Moore61e10682008-10-10 10:16:32 -040094 struct netlbl_af6list list;
Paul Moore8cc44572008-01-29 08:44:21 -050095 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);
Paul Mooreb914f3a2010-04-01 10:43:57 +0000118#define netlbl_unlhsh_rcu_deref(p) \
119 rcu_dereference_check(p, rcu_read_lock_held() || \
120 lockdep_is_held(&netlbl_unlhsh_lock))
Paul Moore8cc44572008-01-29 08:44:21 -0500121static struct netlbl_unlhsh_tbl *netlbl_unlhsh = NULL;
122static struct netlbl_unlhsh_iface *netlbl_unlhsh_def = NULL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700123
124/* Accept unlabeled packets flag */
Paul Moorecd287862006-11-17 17:38:44 -0500125static u8 netlabel_unlabel_acceptflg = 0;
Paul Moore96cb8e32006-08-03 16:48:59 -0700126
Paul Moore8cc44572008-01-29 08:44:21 -0500127/* NetLabel Generic NETLINK unlabeled family */
Paul Moore96cb8e32006-08-03 16:48:59 -0700128static 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,
Paul Moorefd385852006-09-25 15:56:37 -0700133 .maxattr = NLBL_UNLABEL_A_MAX,
Paul Moore96cb8e32006-08-03 16:48:59 -0700134};
135
Paul Moorefd385852006-09-25 15:56:37 -0700136/* NetLabel Netlink attribute policy */
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700137static const struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = {
Paul Moorefd385852006-09-25 15:56:37 -0700138 [NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 },
Paul Moore8cc44572008-01-29 08:44:21 -0500139 [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 }
Paul Moorefd385852006-09-25 15:56:37 -0700150};
Paul Moore96cb8e32006-08-03 16:48:59 -0700151
152/*
Paul Moore8cc44572008-01-29 08:44:21 -0500153 * Unlabeled Connection Hash Table Functions
Paul Moore32f50cd2006-09-28 14:51:47 -0700154 */
155
156/**
Paul Moore8cc44572008-01-29 08:44:21 -0500157 * netlbl_unlhsh_free_addr4 - Frees an IPv4 address entry from the hash table
158 * @entry: the entry's RCU field
159 *
160 * Description:
161 * This function is designed to be used as a callback to the call_rcu()
162 * function so that memory allocated to a hash table address entry can be
163 * released safely.
164 *
165 */
166static void netlbl_unlhsh_free_addr4(struct rcu_head *entry)
167{
168 struct netlbl_unlhsh_addr4 *ptr;
169
170 ptr = container_of(entry, struct netlbl_unlhsh_addr4, rcu);
171 kfree(ptr);
172}
173
174#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
175/**
176 * netlbl_unlhsh_free_addr6 - Frees an IPv6 address entry from the hash table
177 * @entry: the entry's RCU field
178 *
179 * Description:
180 * This function is designed to be used as a callback to the call_rcu()
181 * function so that memory allocated to a hash table address entry can be
182 * released safely.
183 *
184 */
185static void netlbl_unlhsh_free_addr6(struct rcu_head *entry)
186{
187 struct netlbl_unlhsh_addr6 *ptr;
188
189 ptr = container_of(entry, struct netlbl_unlhsh_addr6, rcu);
190 kfree(ptr);
191}
192#endif /* IPv6 */
193
194/**
195 * netlbl_unlhsh_free_iface - Frees an interface entry from the hash table
196 * @entry: the entry's RCU field
197 *
198 * Description:
199 * This function is designed to be used as a callback to the call_rcu()
200 * function so that memory allocated to a hash table interface entry can be
201 * released safely. It is important to note that this function does not free
202 * the IPv4 and IPv6 address lists contained as part of an interface entry. It
203 * is up to the rest of the code to make sure an interface entry is only freed
204 * once it's address lists are empty.
205 *
206 */
207static void netlbl_unlhsh_free_iface(struct rcu_head *entry)
208{
209 struct netlbl_unlhsh_iface *iface;
Paul Moore61e10682008-10-10 10:16:32 -0400210 struct netlbl_af4list *iter4;
211 struct netlbl_af4list *tmp4;
212#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
213 struct netlbl_af6list *iter6;
214 struct netlbl_af6list *tmp6;
215#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500216
217 iface = container_of(entry, struct netlbl_unlhsh_iface, rcu);
218
219 /* no need for locks here since we are the only one with access to this
220 * structure */
221
Paul Moore61e10682008-10-10 10:16:32 -0400222 netlbl_af4list_foreach_safe(iter4, tmp4, &iface->addr4_list) {
223 netlbl_af4list_remove_entry(iter4);
224 kfree(netlbl_unlhsh_addr4_entry(iter4));
225 }
226#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
227 netlbl_af6list_foreach_safe(iter6, tmp6, &iface->addr6_list) {
228 netlbl_af6list_remove_entry(iter6);
229 kfree(netlbl_unlhsh_addr6_entry(iter6));
230 }
231#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500232 kfree(iface);
233}
234
235/**
236 * netlbl_unlhsh_hash - Hashing function for the hash table
237 * @ifindex: the network interface/device to hash
238 *
239 * Description:
240 * This is the hashing function for the unlabeled hash table, it returns the
241 * bucket number for the given device/interface. The caller is responsible for
Paul Mooreb914f3a2010-04-01 10:43:57 +0000242 * ensuring that the hash table is protected with either a RCU read lock or
243 * the hash table lock.
Paul Moore8cc44572008-01-29 08:44:21 -0500244 *
245 */
246static u32 netlbl_unlhsh_hash(int ifindex)
247{
Paul Mooreb914f3a2010-04-01 10:43:57 +0000248 return ifindex & (netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->size - 1);
Paul Moore8cc44572008-01-29 08:44:21 -0500249}
250
251/**
Paul Moore8cc44572008-01-29 08:44:21 -0500252 * netlbl_unlhsh_search_iface - Search for a matching interface entry
253 * @ifindex: the network interface
254 *
255 * Description:
256 * Searches the unlabeled connection hash table and returns a pointer to the
257 * interface entry which matches @ifindex, otherwise NULL is returned. The
Paul Mooreb914f3a2010-04-01 10:43:57 +0000258 * caller is responsible for ensuring that the hash table is protected with
259 * either a RCU read lock or the hash table lock.
Paul Moore8cc44572008-01-29 08:44:21 -0500260 *
261 */
262static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface(int ifindex)
263{
264 u32 bkt;
Paul Moore56196702008-10-10 10:16:29 -0400265 struct list_head *bkt_list;
Paul Moore8cc44572008-01-29 08:44:21 -0500266 struct netlbl_unlhsh_iface *iter;
267
268 bkt = netlbl_unlhsh_hash(ifindex);
Paul Mooreb914f3a2010-04-01 10:43:57 +0000269 bkt_list = &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt];
Paul Moore56196702008-10-10 10:16:29 -0400270 list_for_each_entry_rcu(iter, bkt_list, list)
Paul Moore8cc44572008-01-29 08:44:21 -0500271 if (iter->valid && iter->ifindex == ifindex)
272 return iter;
273
274 return NULL;
275}
276
277/**
Paul Moore8cc44572008-01-29 08:44:21 -0500278 * netlbl_unlhsh_add_addr4 - Add a new IPv4 address entry to the hash table
279 * @iface: the associated interface entry
280 * @addr: IPv4 address in network byte order
281 * @mask: IPv4 address mask in network byte order
282 * @secid: LSM secid value for entry
283 *
284 * Description:
285 * Add a new address entry into the unlabeled connection hash table using the
286 * interface entry specified by @iface. On success zero is returned, otherwise
Paul Mooreb914f3a2010-04-01 10:43:57 +0000287 * a negative value is returned.
Paul Moore8cc44572008-01-29 08:44:21 -0500288 *
289 */
290static int netlbl_unlhsh_add_addr4(struct netlbl_unlhsh_iface *iface,
291 const struct in_addr *addr,
292 const struct in_addr *mask,
293 u32 secid)
294{
Paul Moore61e10682008-10-10 10:16:32 -0400295 int ret_val;
Paul Moore8cc44572008-01-29 08:44:21 -0500296 struct netlbl_unlhsh_addr4 *entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500297
298 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
299 if (entry == NULL)
300 return -ENOMEM;
301
Paul Moore61e10682008-10-10 10:16:32 -0400302 entry->list.addr = addr->s_addr & mask->s_addr;
303 entry->list.mask = mask->s_addr;
304 entry->list.valid = 1;
Paul Moore61e10682008-10-10 10:16:32 -0400305 entry->secid = secid;
Paul Moore8cc44572008-01-29 08:44:21 -0500306
307 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400308 ret_val = netlbl_af4list_add(&entry->list, &iface->addr4_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500309 spin_unlock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400310
311 if (ret_val != 0)
312 kfree(entry);
313 return ret_val;
Paul Moore8cc44572008-01-29 08:44:21 -0500314}
315
316#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
317/**
318 * netlbl_unlhsh_add_addr6 - Add a new IPv6 address entry to the hash table
319 * @iface: the associated interface entry
320 * @addr: IPv6 address in network byte order
321 * @mask: IPv6 address mask in network byte order
322 * @secid: LSM secid value for entry
323 *
324 * Description:
325 * Add a new address entry into the unlabeled connection hash table using the
326 * interface entry specified by @iface. On success zero is returned, otherwise
Paul Mooreb914f3a2010-04-01 10:43:57 +0000327 * a negative value is returned.
Paul Moore8cc44572008-01-29 08:44:21 -0500328 *
329 */
330static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface,
331 const struct in6_addr *addr,
332 const struct in6_addr *mask,
333 u32 secid)
334{
Paul Moore61e10682008-10-10 10:16:32 -0400335 int ret_val;
Paul Moore8cc44572008-01-29 08:44:21 -0500336 struct netlbl_unlhsh_addr6 *entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500337
338 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
339 if (entry == NULL)
340 return -ENOMEM;
341
Paul Moore61e10682008-10-10 10:16:32 -0400342 ipv6_addr_copy(&entry->list.addr, addr);
343 entry->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
344 entry->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
345 entry->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
346 entry->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
347 ipv6_addr_copy(&entry->list.mask, mask);
348 entry->list.valid = 1;
Paul Moore61e10682008-10-10 10:16:32 -0400349 entry->secid = secid;
Paul Moore8cc44572008-01-29 08:44:21 -0500350
351 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400352 ret_val = netlbl_af6list_add(&entry->list, &iface->addr6_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500353 spin_unlock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400354
355 if (ret_val != 0)
356 kfree(entry);
Paul Moore8cc44572008-01-29 08:44:21 -0500357 return 0;
358}
359#endif /* IPv6 */
360
361/**
362 * netlbl_unlhsh_add_iface - Adds a new interface entry to the hash table
363 * @ifindex: network interface
364 *
365 * Description:
366 * Add a new, empty, interface entry into the unlabeled connection hash table.
367 * On success a pointer to the new interface entry is returned, on failure NULL
Paul Mooreb914f3a2010-04-01 10:43:57 +0000368 * is returned.
Paul Moore8cc44572008-01-29 08:44:21 -0500369 *
370 */
371static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex)
372{
373 u32 bkt;
374 struct netlbl_unlhsh_iface *iface;
375
376 iface = kzalloc(sizeof(*iface), GFP_ATOMIC);
377 if (iface == NULL)
378 return NULL;
379
380 iface->ifindex = ifindex;
381 INIT_LIST_HEAD(&iface->addr4_list);
382 INIT_LIST_HEAD(&iface->addr6_list);
383 iface->valid = 1;
Paul Moore8cc44572008-01-29 08:44:21 -0500384
385 spin_lock(&netlbl_unlhsh_lock);
386 if (ifindex > 0) {
387 bkt = netlbl_unlhsh_hash(ifindex);
388 if (netlbl_unlhsh_search_iface(ifindex) != NULL)
389 goto add_iface_failure;
390 list_add_tail_rcu(&iface->list,
Paul Mooreb914f3a2010-04-01 10:43:57 +0000391 &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt]);
Paul Moore8cc44572008-01-29 08:44:21 -0500392 } else {
393 INIT_LIST_HEAD(&iface->list);
Paul Mooreb914f3a2010-04-01 10:43:57 +0000394 if (netlbl_unlhsh_rcu_deref(netlbl_unlhsh_def) != NULL)
Paul Moore8cc44572008-01-29 08:44:21 -0500395 goto add_iface_failure;
396 rcu_assign_pointer(netlbl_unlhsh_def, iface);
397 }
398 spin_unlock(&netlbl_unlhsh_lock);
399
400 return iface;
401
402add_iface_failure:
403 spin_unlock(&netlbl_unlhsh_lock);
404 kfree(iface);
405 return NULL;
406}
407
408/**
409 * netlbl_unlhsh_add - Adds a new entry to the unlabeled connection hash table
410 * @net: network namespace
411 * @dev_name: interface name
412 * @addr: IP address in network byte order
413 * @mask: address mask in network byte order
414 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
415 * @secid: LSM secid value for the entry
Paul Moore13541b32008-01-29 08:44:23 -0500416 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500417 *
418 * Description:
419 * Adds a new entry to the unlabeled connection hash table. Returns zero on
420 * success, negative values on failure.
421 *
422 */
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500423int netlbl_unlhsh_add(struct net *net,
424 const char *dev_name,
425 const void *addr,
426 const void *mask,
427 u32 addr_len,
428 u32 secid,
429 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500430{
431 int ret_val;
432 int ifindex;
433 struct net_device *dev;
434 struct netlbl_unlhsh_iface *iface;
Paul Moore13541b32008-01-29 08:44:23 -0500435 struct audit_buffer *audit_buf = NULL;
436 char *secctx = NULL;
437 u32 secctx_len;
Paul Moore8cc44572008-01-29 08:44:21 -0500438
439 if (addr_len != sizeof(struct in_addr) &&
440 addr_len != sizeof(struct in6_addr))
441 return -EINVAL;
442
443 rcu_read_lock();
444 if (dev_name != NULL) {
Eric Dumazet122ec6f2009-11-05 20:53:47 -0800445 dev = dev_get_by_name_rcu(net, dev_name);
Paul Moore8cc44572008-01-29 08:44:21 -0500446 if (dev == NULL) {
447 ret_val = -ENODEV;
448 goto unlhsh_add_return;
449 }
450 ifindex = dev->ifindex;
Paul Moore8cc44572008-01-29 08:44:21 -0500451 iface = netlbl_unlhsh_search_iface(ifindex);
452 } else {
453 ifindex = 0;
454 iface = rcu_dereference(netlbl_unlhsh_def);
455 }
456 if (iface == NULL) {
457 iface = netlbl_unlhsh_add_iface(ifindex);
458 if (iface == NULL) {
459 ret_val = -ENOMEM;
460 goto unlhsh_add_return;
461 }
462 }
Paul Moore13541b32008-01-29 08:44:23 -0500463 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCADD,
464 audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500465 switch (addr_len) {
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800466 case sizeof(struct in_addr): {
467 struct in_addr *addr4, *mask4;
468
Paul Moore13541b32008-01-29 08:44:23 -0500469 addr4 = (struct in_addr *)addr;
470 mask4 = (struct in_addr *)mask;
471 ret_val = netlbl_unlhsh_add_addr4(iface, addr4, mask4, secid);
472 if (audit_buf != NULL)
Paul Moore63c41682008-10-10 10:16:32 -0400473 netlbl_af4list_audit_addr(audit_buf, 1,
474 dev_name,
475 addr4->s_addr,
476 mask4->s_addr);
Paul Moore8cc44572008-01-29 08:44:21 -0500477 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800478 }
Paul Moore8cc44572008-01-29 08:44:21 -0500479#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800480 case sizeof(struct in6_addr): {
481 struct in6_addr *addr6, *mask6;
482
Paul Moore13541b32008-01-29 08:44:23 -0500483 addr6 = (struct in6_addr *)addr;
484 mask6 = (struct in6_addr *)mask;
485 ret_val = netlbl_unlhsh_add_addr6(iface, addr6, mask6, secid);
486 if (audit_buf != NULL)
Paul Moore63c41682008-10-10 10:16:32 -0400487 netlbl_af6list_audit_addr(audit_buf, 1,
488 dev_name,
489 addr6, mask6);
Paul Moore8cc44572008-01-29 08:44:21 -0500490 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800491 }
Paul Moore8cc44572008-01-29 08:44:21 -0500492#endif /* IPv6 */
493 default:
494 ret_val = -EINVAL;
495 }
496 if (ret_val == 0)
497 atomic_inc(&netlabel_mgmt_protocount);
498
499unlhsh_add_return:
500 rcu_read_unlock();
Paul Moore13541b32008-01-29 08:44:23 -0500501 if (audit_buf != NULL) {
502 if (security_secid_to_secctx(secid,
503 &secctx,
504 &secctx_len) == 0) {
505 audit_log_format(audit_buf, " sec_obj=%s", secctx);
506 security_release_secctx(secctx, secctx_len);
507 }
508 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
509 audit_log_end(audit_buf);
510 }
Paul Moore8cc44572008-01-29 08:44:21 -0500511 return ret_val;
512}
513
514/**
515 * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry
Paul Moore13541b32008-01-29 08:44:23 -0500516 * @net: network namespace
Paul Moore8cc44572008-01-29 08:44:21 -0500517 * @iface: interface entry
518 * @addr: IP address
519 * @mask: IP address mask
Paul Moore13541b32008-01-29 08:44:23 -0500520 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500521 *
522 * Description:
523 * Remove an IP address entry from the unlabeled connection hash table.
Paul Mooreb914f3a2010-04-01 10:43:57 +0000524 * Returns zero on success, negative values on failure.
Paul Moore8cc44572008-01-29 08:44:21 -0500525 *
526 */
Paul Moore13541b32008-01-29 08:44:23 -0500527static int netlbl_unlhsh_remove_addr4(struct net *net,
528 struct netlbl_unlhsh_iface *iface,
Paul Moore8cc44572008-01-29 08:44:21 -0500529 const struct in_addr *addr,
Paul Moore13541b32008-01-29 08:44:23 -0500530 const struct in_addr *mask,
531 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500532{
Paul Moore61e10682008-10-10 10:16:32 -0400533 struct netlbl_af4list *list_entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500534 struct netlbl_unlhsh_addr4 *entry;
Paul Moore61e10682008-10-10 10:16:32 -0400535 struct audit_buffer *audit_buf;
Paul Moore13541b32008-01-29 08:44:23 -0500536 struct net_device *dev;
Paul Moore61e10682008-10-10 10:16:32 -0400537 char *secctx;
Paul Moore13541b32008-01-29 08:44:23 -0500538 u32 secctx_len;
Paul Moore8cc44572008-01-29 08:44:21 -0500539
540 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400541 list_entry = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
542 &iface->addr4_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500543 spin_unlock(&netlbl_unlhsh_lock);
Paul Moored25830e2008-12-03 00:37:04 -0800544 if (list_entry != NULL)
545 entry = netlbl_unlhsh_addr4_entry(list_entry);
546 else
Paul Mooreec8f2372008-12-11 21:31:50 -0800547 entry = NULL;
Paul Moore8cc44572008-01-29 08:44:21 -0500548
Paul Moore13541b32008-01-29 08:44:23 -0500549 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
550 audit_info);
551 if (audit_buf != NULL) {
552 dev = dev_get_by_index(net, iface->ifindex);
Paul Moore63c41682008-10-10 10:16:32 -0400553 netlbl_af4list_audit_addr(audit_buf, 1,
554 (dev != NULL ? dev->name : NULL),
555 addr->s_addr, mask->s_addr);
Paul Moore13541b32008-01-29 08:44:23 -0500556 if (dev != NULL)
557 dev_put(dev);
Paul Mooreec8f2372008-12-11 21:31:50 -0800558 if (entry != NULL &&
559 security_secid_to_secctx(entry->secid,
560 &secctx, &secctx_len) == 0) {
Paul Moore13541b32008-01-29 08:44:23 -0500561 audit_log_format(audit_buf, " sec_obj=%s", secctx);
562 security_release_secctx(secctx, secctx_len);
563 }
Paul Mooreec8f2372008-12-11 21:31:50 -0800564 audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
Paul Moore13541b32008-01-29 08:44:23 -0500565 audit_log_end(audit_buf);
566 }
567
Paul Mooreec8f2372008-12-11 21:31:50 -0800568 if (entry == NULL)
569 return -ENOENT;
570
571 call_rcu(&entry->rcu, netlbl_unlhsh_free_addr4);
572 return 0;
Paul Moore8cc44572008-01-29 08:44:21 -0500573}
574
575#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
576/**
577 * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry
Paul Moore13541b32008-01-29 08:44:23 -0500578 * @net: network namespace
Paul Moore8cc44572008-01-29 08:44:21 -0500579 * @iface: interface entry
580 * @addr: IP address
581 * @mask: IP address mask
Paul Moore13541b32008-01-29 08:44:23 -0500582 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500583 *
584 * Description:
585 * Remove an IP address entry from the unlabeled connection hash table.
Paul Mooreb914f3a2010-04-01 10:43:57 +0000586 * Returns zero on success, negative values on failure.
Paul Moore8cc44572008-01-29 08:44:21 -0500587 *
588 */
Paul Moore13541b32008-01-29 08:44:23 -0500589static int netlbl_unlhsh_remove_addr6(struct net *net,
590 struct netlbl_unlhsh_iface *iface,
Paul Moore8cc44572008-01-29 08:44:21 -0500591 const struct in6_addr *addr,
Paul Moore13541b32008-01-29 08:44:23 -0500592 const struct in6_addr *mask,
593 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500594{
Paul Moore61e10682008-10-10 10:16:32 -0400595 struct netlbl_af6list *list_entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500596 struct netlbl_unlhsh_addr6 *entry;
Paul Moore61e10682008-10-10 10:16:32 -0400597 struct audit_buffer *audit_buf;
Paul Moore13541b32008-01-29 08:44:23 -0500598 struct net_device *dev;
Paul Moore61e10682008-10-10 10:16:32 -0400599 char *secctx;
Paul Moore13541b32008-01-29 08:44:23 -0500600 u32 secctx_len;
Paul Moore8cc44572008-01-29 08:44:21 -0500601
602 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400603 list_entry = netlbl_af6list_remove(addr, mask, &iface->addr6_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500604 spin_unlock(&netlbl_unlhsh_lock);
Paul Moored25830e2008-12-03 00:37:04 -0800605 if (list_entry != NULL)
606 entry = netlbl_unlhsh_addr6_entry(list_entry);
607 else
Paul Mooreec8f2372008-12-11 21:31:50 -0800608 entry = NULL;
Paul Moore8cc44572008-01-29 08:44:21 -0500609
Paul Moore13541b32008-01-29 08:44:23 -0500610 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
611 audit_info);
612 if (audit_buf != NULL) {
613 dev = dev_get_by_index(net, iface->ifindex);
Paul Moore63c41682008-10-10 10:16:32 -0400614 netlbl_af6list_audit_addr(audit_buf, 1,
615 (dev != NULL ? dev->name : NULL),
616 addr, mask);
Paul Moore13541b32008-01-29 08:44:23 -0500617 if (dev != NULL)
618 dev_put(dev);
Paul Mooreec8f2372008-12-11 21:31:50 -0800619 if (entry != NULL &&
620 security_secid_to_secctx(entry->secid,
621 &secctx, &secctx_len) == 0) {
Paul Moore13541b32008-01-29 08:44:23 -0500622 audit_log_format(audit_buf, " sec_obj=%s", secctx);
623 security_release_secctx(secctx, secctx_len);
624 }
Paul Mooreec8f2372008-12-11 21:31:50 -0800625 audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
Paul Moore13541b32008-01-29 08:44:23 -0500626 audit_log_end(audit_buf);
627 }
628
Paul Mooreec8f2372008-12-11 21:31:50 -0800629 if (entry == NULL)
630 return -ENOENT;
631
632 call_rcu(&entry->rcu, netlbl_unlhsh_free_addr6);
633 return 0;
Paul Moore8cc44572008-01-29 08:44:21 -0500634}
635#endif /* IPv6 */
636
637/**
638 * netlbl_unlhsh_condremove_iface - Remove an interface entry
639 * @iface: the interface entry
640 *
641 * Description:
642 * Remove an interface entry from the unlabeled connection hash table if it is
643 * empty. An interface entry is considered to be empty if there are no
644 * address entries assigned to it.
645 *
646 */
647static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface)
648{
Paul Moore61e10682008-10-10 10:16:32 -0400649 struct netlbl_af4list *iter4;
650#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
651 struct netlbl_af6list *iter6;
652#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500653
654 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400655 netlbl_af4list_foreach_rcu(iter4, &iface->addr4_list)
656 goto unlhsh_condremove_failure;
657#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
658 netlbl_af6list_foreach_rcu(iter6, &iface->addr6_list)
659 goto unlhsh_condremove_failure;
660#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500661 iface->valid = 0;
662 if (iface->ifindex > 0)
663 list_del_rcu(&iface->list);
664 else
665 rcu_assign_pointer(netlbl_unlhsh_def, NULL);
666 spin_unlock(&netlbl_unlhsh_lock);
667
668 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
669 return;
670
671unlhsh_condremove_failure:
672 spin_unlock(&netlbl_unlhsh_lock);
673 return;
674}
675
676/**
677 * netlbl_unlhsh_remove - Remove an entry from the unlabeled hash table
678 * @net: network namespace
679 * @dev_name: interface name
680 * @addr: IP address in network byte order
681 * @mask: address mask in network byte order
682 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
Paul Moore13541b32008-01-29 08:44:23 -0500683 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500684 *
685 * Description:
686 * Removes and existing entry from the unlabeled connection hash table.
687 * Returns zero on success, negative values on failure.
688 *
689 */
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500690int netlbl_unlhsh_remove(struct net *net,
691 const char *dev_name,
692 const void *addr,
693 const void *mask,
694 u32 addr_len,
695 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500696{
697 int ret_val;
698 struct net_device *dev;
699 struct netlbl_unlhsh_iface *iface;
700
701 if (addr_len != sizeof(struct in_addr) &&
702 addr_len != sizeof(struct in6_addr))
703 return -EINVAL;
704
705 rcu_read_lock();
706 if (dev_name != NULL) {
Eric Dumazet122ec6f2009-11-05 20:53:47 -0800707 dev = dev_get_by_name_rcu(net, dev_name);
Paul Moore8cc44572008-01-29 08:44:21 -0500708 if (dev == NULL) {
709 ret_val = -ENODEV;
710 goto unlhsh_remove_return;
711 }
712 iface = netlbl_unlhsh_search_iface(dev->ifindex);
Paul Moore8cc44572008-01-29 08:44:21 -0500713 } else
714 iface = rcu_dereference(netlbl_unlhsh_def);
715 if (iface == NULL) {
716 ret_val = -ENOENT;
717 goto unlhsh_remove_return;
718 }
719 switch (addr_len) {
720 case sizeof(struct in_addr):
Paul Moore13541b32008-01-29 08:44:23 -0500721 ret_val = netlbl_unlhsh_remove_addr4(net,
722 iface, addr, mask,
723 audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500724 break;
725#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
726 case sizeof(struct in6_addr):
Paul Moore13541b32008-01-29 08:44:23 -0500727 ret_val = netlbl_unlhsh_remove_addr6(net,
728 iface, addr, mask,
729 audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500730 break;
731#endif /* IPv6 */
732 default:
733 ret_val = -EINVAL;
734 }
735 if (ret_val == 0) {
736 netlbl_unlhsh_condremove_iface(iface);
737 atomic_dec(&netlabel_mgmt_protocount);
738 }
739
740unlhsh_remove_return:
741 rcu_read_unlock();
742 return ret_val;
743}
744
745/*
746 * General Helper Functions
747 */
748
749/**
750 * netlbl_unlhsh_netdev_handler - Network device notification handler
751 * @this: notifier block
752 * @event: the event
753 * @ptr: the network device (cast to void)
754 *
755 * Description:
756 * Handle network device events, although at present all we care about is a
757 * network device going away. In the case of a device going away we clear any
758 * related entries from the unlabeled connection hash table.
759 *
760 */
761static int netlbl_unlhsh_netdev_handler(struct notifier_block *this,
762 unsigned long event,
763 void *ptr)
764{
765 struct net_device *dev = ptr;
766 struct netlbl_unlhsh_iface *iface = NULL;
767
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700768 if (!net_eq(dev_net(dev), &init_net))
Paul Moore8cc44572008-01-29 08:44:21 -0500769 return NOTIFY_DONE;
770
771 /* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */
772 if (event == NETDEV_DOWN) {
773 spin_lock(&netlbl_unlhsh_lock);
774 iface = netlbl_unlhsh_search_iface(dev->ifindex);
775 if (iface != NULL && iface->valid) {
776 iface->valid = 0;
777 list_del_rcu(&iface->list);
778 } else
779 iface = NULL;
780 spin_unlock(&netlbl_unlhsh_lock);
781 }
782
783 if (iface != NULL)
784 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
785
786 return NOTIFY_DONE;
787}
788
789/**
Paul Moore32f50cd2006-09-28 14:51:47 -0700790 * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
791 * @value: desired value
Paul Moore95d4e6b2006-09-29 17:05:05 -0700792 * @audit_info: NetLabel audit information
Paul Moore32f50cd2006-09-28 14:51:47 -0700793 *
794 * Description:
795 * Set the value of the unlabeled accept flag to @value.
796 *
797 */
Paul Moore95d4e6b2006-09-29 17:05:05 -0700798static void netlbl_unlabel_acceptflg_set(u8 value,
799 struct netlbl_audit *audit_info)
Paul Moore32f50cd2006-09-28 14:51:47 -0700800{
Paul Moore95d4e6b2006-09-29 17:05:05 -0700801 struct audit_buffer *audit_buf;
802 u8 old_val;
803
Paul Moore4be27002007-10-26 04:29:08 -0700804 old_val = netlabel_unlabel_acceptflg;
Paul Moorecd287862006-11-17 17:38:44 -0500805 netlabel_unlabel_acceptflg = value;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700806 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
807 audit_info);
Paul Moorede646882006-11-17 17:38:55 -0500808 if (audit_buf != NULL) {
809 audit_log_format(audit_buf,
810 " unlbl_accept=%u old=%u", value, old_val);
811 audit_log_end(audit_buf);
812 }
Paul Moore32f50cd2006-09-28 14:51:47 -0700813}
814
Paul Moore8cc44572008-01-29 08:44:21 -0500815/**
816 * netlbl_unlabel_addrinfo_get - Get the IPv4/6 address information
817 * @info: the Generic NETLINK info block
818 * @addr: the IP address
819 * @mask: the IP address mask
820 * @len: the address length
821 *
822 * Description:
823 * Examine the Generic NETLINK message and extract the IP address information.
824 * Returns zero on success, negative values on failure.
825 *
826 */
827static int netlbl_unlabel_addrinfo_get(struct genl_info *info,
828 void **addr,
829 void **mask,
830 u32 *len)
831{
832 u32 addr_len;
833
834 if (info->attrs[NLBL_UNLABEL_A_IPV4ADDR]) {
835 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
836 if (addr_len != sizeof(struct in_addr) &&
837 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK]))
838 return -EINVAL;
839 *len = addr_len;
840 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
841 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]);
842 return 0;
843 } else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) {
844 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
845 if (addr_len != sizeof(struct in6_addr) &&
846 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK]))
847 return -EINVAL;
848 *len = addr_len;
849 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
850 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]);
851 return 0;
852 }
853
854 return -EINVAL;
855}
856
Paul Moore32f50cd2006-09-28 14:51:47 -0700857/*
Paul Moore96cb8e32006-08-03 16:48:59 -0700858 * NetLabel Command Handlers
859 */
860
861/**
862 * netlbl_unlabel_accept - Handle an ACCEPT message
863 * @skb: the NETLINK buffer
864 * @info: the Generic NETLINK info block
865 *
866 * Description:
867 * Process a user generated ACCEPT message and set the accept flag accordingly.
868 * Returns zero on success, negative values on failure.
869 *
870 */
871static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
872{
Paul Moorefd385852006-09-25 15:56:37 -0700873 u8 value;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700874 struct netlbl_audit audit_info;
Paul Moore96cb8e32006-08-03 16:48:59 -0700875
Paul Moorefd385852006-09-25 15:56:37 -0700876 if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
877 value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
Paul Moore96cb8e32006-08-03 16:48:59 -0700878 if (value == 1 || value == 0) {
Paul Moore95d4e6b2006-09-29 17:05:05 -0700879 netlbl_netlink_auditinfo(skb, &audit_info);
880 netlbl_unlabel_acceptflg_set(value, &audit_info);
Paul Moore32f50cd2006-09-28 14:51:47 -0700881 return 0;
Paul Moore96cb8e32006-08-03 16:48:59 -0700882 }
883 }
884
Paul Moore32f50cd2006-09-28 14:51:47 -0700885 return -EINVAL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700886}
887
888/**
889 * netlbl_unlabel_list - Handle a LIST message
890 * @skb: the NETLINK buffer
891 * @info: the Generic NETLINK info block
892 *
893 * Description:
894 * Process a user generated LIST message and respond with the current status.
895 * Returns zero on success, negative values on failure.
896 *
897 */
898static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
899{
Paul Moorefd385852006-09-25 15:56:37 -0700900 int ret_val = -EINVAL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700901 struct sk_buff *ans_skb;
Paul Moorefd385852006-09-25 15:56:37 -0700902 void *data;
Paul Moore96cb8e32006-08-03 16:48:59 -0700903
Thomas Graf339bf982006-11-10 14:10:15 -0800904 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Paul Moore96cb8e32006-08-03 16:48:59 -0700905 if (ans_skb == NULL)
906 goto list_failure;
Thomas Graf17c157c2006-11-14 19:46:02 -0800907 data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
908 0, NLBL_UNLABEL_C_LIST);
Paul Moorefd385852006-09-25 15:56:37 -0700909 if (data == NULL) {
910 ret_val = -ENOMEM;
Paul Moore96cb8e32006-08-03 16:48:59 -0700911 goto list_failure;
Paul Moorefd385852006-09-25 15:56:37 -0700912 }
Paul Moore96cb8e32006-08-03 16:48:59 -0700913
Paul Moorefd385852006-09-25 15:56:37 -0700914 ret_val = nla_put_u8(ans_skb,
915 NLBL_UNLABEL_A_ACPTFLG,
Paul Moorecd287862006-11-17 17:38:44 -0500916 netlabel_unlabel_acceptflg);
Paul Moore96cb8e32006-08-03 16:48:59 -0700917 if (ret_val != 0)
918 goto list_failure;
919
Paul Moorefd385852006-09-25 15:56:37 -0700920 genlmsg_end(ans_skb, data);
Denis V. Lunevfe785be2008-07-10 16:53:39 -0700921 return genlmsg_reply(ans_skb, info);
Paul Moore96cb8e32006-08-03 16:48:59 -0700922
923list_failure:
Patrick McHardyb08d5842007-02-27 09:57:37 -0800924 kfree_skb(ans_skb);
Paul Moore96cb8e32006-08-03 16:48:59 -0700925 return ret_val;
926}
927
Paul Moore8cc44572008-01-29 08:44:21 -0500928/**
929 * netlbl_unlabel_staticadd - Handle a STATICADD message
930 * @skb: the NETLINK buffer
931 * @info: the Generic NETLINK info block
932 *
933 * Description:
934 * Process a user generated STATICADD message and add a new unlabeled
935 * connection entry to the hash table. Returns zero on success, negative
936 * values on failure.
937 *
938 */
939static int netlbl_unlabel_staticadd(struct sk_buff *skb,
940 struct genl_info *info)
941{
942 int ret_val;
943 char *dev_name;
944 void *addr;
945 void *mask;
946 u32 addr_len;
947 u32 secid;
Paul Moore13541b32008-01-29 08:44:23 -0500948 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -0500949
950 /* Don't allow users to add both IPv4 and IPv6 addresses for a
951 * single entry. However, allow users to create two entries, one each
952 * for IPv4 and IPv4, with the same LSM security context which should
953 * achieve the same result. */
954 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
955 !info->attrs[NLBL_UNLABEL_A_IFACE] ||
956 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
957 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
958 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
959 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
960 return -EINVAL;
961
Paul Moore13541b32008-01-29 08:44:23 -0500962 netlbl_netlink_auditinfo(skb, &audit_info);
963
Paul Moore8cc44572008-01-29 08:44:21 -0500964 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
965 if (ret_val != 0)
966 return ret_val;
967 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
968 ret_val = security_secctx_to_secid(
969 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
970 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
971 &secid);
972 if (ret_val != 0)
973 return ret_val;
974
975 return netlbl_unlhsh_add(&init_net,
Paul Moore13541b32008-01-29 08:44:23 -0500976 dev_name, addr, mask, addr_len, secid,
977 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500978}
979
980/**
981 * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message
982 * @skb: the NETLINK buffer
983 * @info: the Generic NETLINK info block
984 *
985 * Description:
986 * Process a user generated STATICADDDEF message and add a new default
987 * unlabeled connection entry. Returns zero on success, negative values on
988 * failure.
989 *
990 */
991static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
992 struct genl_info *info)
993{
994 int ret_val;
995 void *addr;
996 void *mask;
997 u32 addr_len;
998 u32 secid;
Paul Moore13541b32008-01-29 08:44:23 -0500999 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -05001000
1001 /* Don't allow users to add both IPv4 and IPv6 addresses for a
1002 * single entry. However, allow users to create two entries, one each
1003 * for IPv4 and IPv6, with the same LSM security context which should
1004 * achieve the same result. */
1005 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
1006 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1007 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1008 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1009 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1010 return -EINVAL;
1011
Paul Moore13541b32008-01-29 08:44:23 -05001012 netlbl_netlink_auditinfo(skb, &audit_info);
1013
Paul Moore8cc44572008-01-29 08:44:21 -05001014 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1015 if (ret_val != 0)
1016 return ret_val;
1017 ret_val = security_secctx_to_secid(
1018 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1019 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1020 &secid);
1021 if (ret_val != 0)
1022 return ret_val;
1023
Paul Moore13541b32008-01-29 08:44:23 -05001024 return netlbl_unlhsh_add(&init_net,
1025 NULL, addr, mask, addr_len, secid,
1026 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -05001027}
1028
1029/**
1030 * netlbl_unlabel_staticremove - Handle a STATICREMOVE message
1031 * @skb: the NETLINK buffer
1032 * @info: the Generic NETLINK info block
1033 *
1034 * Description:
1035 * Process a user generated STATICREMOVE message and remove the specified
1036 * unlabeled connection entry. Returns zero on success, negative values on
1037 * failure.
1038 *
1039 */
1040static int netlbl_unlabel_staticremove(struct sk_buff *skb,
1041 struct genl_info *info)
1042{
1043 int ret_val;
1044 char *dev_name;
1045 void *addr;
1046 void *mask;
1047 u32 addr_len;
Paul Moore13541b32008-01-29 08:44:23 -05001048 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -05001049
1050 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1051 * IPv4 and IPv6 in the same entry. */
1052 if (!info->attrs[NLBL_UNLABEL_A_IFACE] ||
1053 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1054 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1055 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1056 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1057 return -EINVAL;
1058
Paul Moore13541b32008-01-29 08:44:23 -05001059 netlbl_netlink_auditinfo(skb, &audit_info);
1060
Paul Moore8cc44572008-01-29 08:44:21 -05001061 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1062 if (ret_val != 0)
1063 return ret_val;
1064 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
1065
Paul Moore13541b32008-01-29 08:44:23 -05001066 return netlbl_unlhsh_remove(&init_net,
1067 dev_name, addr, mask, addr_len,
1068 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -05001069}
1070
1071/**
1072 * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message
1073 * @skb: the NETLINK buffer
1074 * @info: the Generic NETLINK info block
1075 *
1076 * Description:
1077 * Process a user generated STATICREMOVEDEF message and remove the default
1078 * unlabeled connection entry. Returns zero on success, negative values on
1079 * failure.
1080 *
1081 */
1082static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
1083 struct genl_info *info)
1084{
1085 int ret_val;
1086 void *addr;
1087 void *mask;
1088 u32 addr_len;
Paul Moore13541b32008-01-29 08:44:23 -05001089 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -05001090
1091 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1092 * IPv4 and IPv6 in the same entry. */
1093 if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1094 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1095 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1096 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1097 return -EINVAL;
1098
Paul Moore13541b32008-01-29 08:44:23 -05001099 netlbl_netlink_auditinfo(skb, &audit_info);
1100
Paul Moore8cc44572008-01-29 08:44:21 -05001101 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1102 if (ret_val != 0)
1103 return ret_val;
1104
Paul Moore13541b32008-01-29 08:44:23 -05001105 return netlbl_unlhsh_remove(&init_net,
1106 NULL, addr, mask, addr_len,
1107 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -05001108}
1109
1110
1111/**
1112 * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF]
1113 * @cmd: command/message
1114 * @iface: the interface entry
1115 * @addr4: the IPv4 address entry
1116 * @addr6: the IPv6 address entry
1117 * @arg: the netlbl_unlhsh_walk_arg structure
1118 *
1119 * Description:
1120 * This function is designed to be used to generate a response for a
1121 * STATICLIST or STATICLISTDEF message. When called either @addr4 or @addr6
1122 * can be specified, not both, the other unspecified entry should be set to
1123 * NULL by the caller. Returns the size of the message on success, negative
1124 * values on failure.
1125 *
1126 */
1127static int netlbl_unlabel_staticlist_gen(u32 cmd,
1128 const struct netlbl_unlhsh_iface *iface,
1129 const struct netlbl_unlhsh_addr4 *addr4,
1130 const struct netlbl_unlhsh_addr6 *addr6,
1131 void *arg)
1132{
1133 int ret_val = -ENOMEM;
1134 struct netlbl_unlhsh_walk_arg *cb_arg = arg;
1135 struct net_device *dev;
1136 void *data;
1137 u32 secid;
1138 char *secctx;
1139 u32 secctx_len;
1140
1141 data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
1142 cb_arg->seq, &netlbl_unlabel_gnl_family,
1143 NLM_F_MULTI, cmd);
1144 if (data == NULL)
1145 goto list_cb_failure;
1146
1147 if (iface->ifindex > 0) {
1148 dev = dev_get_by_index(&init_net, iface->ifindex);
Jesper Juhl794eb6b2008-04-17 23:22:54 -07001149 if (!dev) {
1150 ret_val = -ENODEV;
1151 goto list_cb_failure;
1152 }
Paul Moore8cc44572008-01-29 08:44:21 -05001153 ret_val = nla_put_string(cb_arg->skb,
1154 NLBL_UNLABEL_A_IFACE, dev->name);
1155 dev_put(dev);
1156 if (ret_val != 0)
1157 goto list_cb_failure;
1158 }
1159
1160 if (addr4) {
1161 struct in_addr addr_struct;
1162
Paul Moore61e10682008-10-10 10:16:32 -04001163 addr_struct.s_addr = addr4->list.addr;
Paul Moore8cc44572008-01-29 08:44:21 -05001164 ret_val = nla_put(cb_arg->skb,
1165 NLBL_UNLABEL_A_IPV4ADDR,
1166 sizeof(struct in_addr),
1167 &addr_struct);
1168 if (ret_val != 0)
1169 goto list_cb_failure;
1170
Paul Moore61e10682008-10-10 10:16:32 -04001171 addr_struct.s_addr = addr4->list.mask;
Paul Moore8cc44572008-01-29 08:44:21 -05001172 ret_val = nla_put(cb_arg->skb,
1173 NLBL_UNLABEL_A_IPV4MASK,
1174 sizeof(struct in_addr),
1175 &addr_struct);
1176 if (ret_val != 0)
1177 goto list_cb_failure;
1178
1179 secid = addr4->secid;
1180 } else {
1181 ret_val = nla_put(cb_arg->skb,
1182 NLBL_UNLABEL_A_IPV6ADDR,
1183 sizeof(struct in6_addr),
Paul Moore61e10682008-10-10 10:16:32 -04001184 &addr6->list.addr);
Paul Moore8cc44572008-01-29 08:44:21 -05001185 if (ret_val != 0)
1186 goto list_cb_failure;
1187
1188 ret_val = nla_put(cb_arg->skb,
1189 NLBL_UNLABEL_A_IPV6MASK,
1190 sizeof(struct in6_addr),
Paul Moore61e10682008-10-10 10:16:32 -04001191 &addr6->list.mask);
Paul Moore8cc44572008-01-29 08:44:21 -05001192 if (ret_val != 0)
1193 goto list_cb_failure;
1194
1195 secid = addr6->secid;
1196 }
1197
1198 ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len);
1199 if (ret_val != 0)
1200 goto list_cb_failure;
1201 ret_val = nla_put(cb_arg->skb,
1202 NLBL_UNLABEL_A_SECCTX,
1203 secctx_len,
1204 secctx);
1205 security_release_secctx(secctx, secctx_len);
1206 if (ret_val != 0)
1207 goto list_cb_failure;
1208
1209 cb_arg->seq++;
1210 return genlmsg_end(cb_arg->skb, data);
1211
1212list_cb_failure:
1213 genlmsg_cancel(cb_arg->skb, data);
1214 return ret_val;
1215}
1216
1217/**
1218 * netlbl_unlabel_staticlist - Handle a STATICLIST message
1219 * @skb: the NETLINK buffer
1220 * @cb: the NETLINK callback
1221 *
1222 * Description:
1223 * Process a user generated STATICLIST message and dump the unlabeled
1224 * connection hash table in a form suitable for use in a kernel generated
1225 * STATICLIST message. Returns the length of @skb.
1226 *
1227 */
1228static int netlbl_unlabel_staticlist(struct sk_buff *skb,
1229 struct netlink_callback *cb)
1230{
1231 struct netlbl_unlhsh_walk_arg cb_arg;
1232 u32 skip_bkt = cb->args[0];
1233 u32 skip_chain = cb->args[1];
1234 u32 skip_addr4 = cb->args[2];
1235 u32 skip_addr6 = cb->args[3];
1236 u32 iter_bkt;
1237 u32 iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
1238 struct netlbl_unlhsh_iface *iface;
Paul Moore56196702008-10-10 10:16:29 -04001239 struct list_head *iter_list;
Paul Moore61e10682008-10-10 10:16:32 -04001240 struct netlbl_af4list *addr4;
1241#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1242 struct netlbl_af6list *addr6;
1243#endif
Paul Moore8cc44572008-01-29 08:44:21 -05001244
1245 cb_arg.nl_cb = cb;
1246 cb_arg.skb = skb;
1247 cb_arg.seq = cb->nlh->nlmsg_seq;
1248
1249 rcu_read_lock();
1250 for (iter_bkt = skip_bkt;
1251 iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
1252 iter_bkt++, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0) {
Paul Moore56196702008-10-10 10:16:29 -04001253 iter_list = &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt];
1254 list_for_each_entry_rcu(iface, iter_list, list) {
Paul Moore8cc44572008-01-29 08:44:21 -05001255 if (!iface->valid ||
1256 iter_chain++ < skip_chain)
1257 continue;
Paul Moore61e10682008-10-10 10:16:32 -04001258 netlbl_af4list_foreach_rcu(addr4,
1259 &iface->addr4_list) {
1260 if (iter_addr4++ < skip_addr4)
Paul Moore8cc44572008-01-29 08:44:21 -05001261 continue;
1262 if (netlbl_unlabel_staticlist_gen(
Paul Moore61e10682008-10-10 10:16:32 -04001263 NLBL_UNLABEL_C_STATICLIST,
1264 iface,
1265 netlbl_unlhsh_addr4_entry(addr4),
1266 NULL,
1267 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001268 iter_addr4--;
1269 iter_chain--;
1270 goto unlabel_staticlist_return;
1271 }
1272 }
Paul Moore61e10682008-10-10 10:16:32 -04001273#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1274 netlbl_af6list_foreach_rcu(addr6,
1275 &iface->addr6_list) {
1276 if (iter_addr6++ < skip_addr6)
Paul Moore8cc44572008-01-29 08:44:21 -05001277 continue;
1278 if (netlbl_unlabel_staticlist_gen(
Paul Moore61e10682008-10-10 10:16:32 -04001279 NLBL_UNLABEL_C_STATICLIST,
1280 iface,
1281 NULL,
1282 netlbl_unlhsh_addr6_entry(addr6),
1283 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001284 iter_addr6--;
1285 iter_chain--;
1286 goto unlabel_staticlist_return;
1287 }
1288 }
Paul Moore61e10682008-10-10 10:16:32 -04001289#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -05001290 }
1291 }
1292
1293unlabel_staticlist_return:
1294 rcu_read_unlock();
1295 cb->args[0] = skip_bkt;
1296 cb->args[1] = skip_chain;
1297 cb->args[2] = skip_addr4;
1298 cb->args[3] = skip_addr6;
1299 return skb->len;
1300}
1301
1302/**
1303 * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message
1304 * @skb: the NETLINK buffer
1305 * @cb: the NETLINK callback
1306 *
1307 * Description:
1308 * Process a user generated STATICLISTDEF message and dump the default
1309 * unlabeled connection entry in a form suitable for use in a kernel generated
1310 * STATICLISTDEF message. Returns the length of @skb.
1311 *
1312 */
1313static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
1314 struct netlink_callback *cb)
1315{
1316 struct netlbl_unlhsh_walk_arg cb_arg;
1317 struct netlbl_unlhsh_iface *iface;
1318 u32 skip_addr4 = cb->args[0];
1319 u32 skip_addr6 = cb->args[1];
Paul Moore61e10682008-10-10 10:16:32 -04001320 u32 iter_addr4 = 0;
1321 struct netlbl_af4list *addr4;
1322#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1323 u32 iter_addr6 = 0;
1324 struct netlbl_af6list *addr6;
1325#endif
Paul Moore8cc44572008-01-29 08:44:21 -05001326
1327 cb_arg.nl_cb = cb;
1328 cb_arg.skb = skb;
1329 cb_arg.seq = cb->nlh->nlmsg_seq;
1330
1331 rcu_read_lock();
1332 iface = rcu_dereference(netlbl_unlhsh_def);
1333 if (iface == NULL || !iface->valid)
1334 goto unlabel_staticlistdef_return;
1335
Paul Moore61e10682008-10-10 10:16:32 -04001336 netlbl_af4list_foreach_rcu(addr4, &iface->addr4_list) {
1337 if (iter_addr4++ < skip_addr4)
Paul Moore8cc44572008-01-29 08:44:21 -05001338 continue;
1339 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
Paul Moore61e10682008-10-10 10:16:32 -04001340 iface,
1341 netlbl_unlhsh_addr4_entry(addr4),
1342 NULL,
1343 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001344 iter_addr4--;
1345 goto unlabel_staticlistdef_return;
1346 }
1347 }
Paul Moore61e10682008-10-10 10:16:32 -04001348#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1349 netlbl_af6list_foreach_rcu(addr6, &iface->addr6_list) {
1350 if (iter_addr6++ < skip_addr6)
Paul Moore8cc44572008-01-29 08:44:21 -05001351 continue;
1352 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
Paul Moore61e10682008-10-10 10:16:32 -04001353 iface,
1354 NULL,
1355 netlbl_unlhsh_addr6_entry(addr6),
1356 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001357 iter_addr6--;
1358 goto unlabel_staticlistdef_return;
1359 }
1360 }
Paul Moore61e10682008-10-10 10:16:32 -04001361#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -05001362
1363unlabel_staticlistdef_return:
1364 rcu_read_unlock();
1365 cb->args[0] = skip_addr4;
1366 cb->args[1] = skip_addr6;
1367 return skb->len;
1368}
Paul Moore96cb8e32006-08-03 16:48:59 -07001369
1370/*
1371 * NetLabel Generic NETLINK Command Definitions
1372 */
1373
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001374static struct genl_ops netlbl_unlabel_genl_ops[] = {
1375 {
Paul Moore8cc44572008-01-29 08:44:21 -05001376 .cmd = NLBL_UNLABEL_C_STATICADD,
1377 .flags = GENL_ADMIN_PERM,
1378 .policy = netlbl_unlabel_genl_policy,
1379 .doit = netlbl_unlabel_staticadd,
1380 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001381 },
1382 {
Paul Moore8cc44572008-01-29 08:44:21 -05001383 .cmd = NLBL_UNLABEL_C_STATICREMOVE,
1384 .flags = GENL_ADMIN_PERM,
1385 .policy = netlbl_unlabel_genl_policy,
1386 .doit = netlbl_unlabel_staticremove,
1387 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001388 },
1389 {
Paul Moore8cc44572008-01-29 08:44:21 -05001390 .cmd = NLBL_UNLABEL_C_STATICLIST,
1391 .flags = 0,
1392 .policy = netlbl_unlabel_genl_policy,
1393 .doit = NULL,
1394 .dumpit = netlbl_unlabel_staticlist,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001395 },
1396 {
Paul Moore8cc44572008-01-29 08:44:21 -05001397 .cmd = NLBL_UNLABEL_C_STATICADDDEF,
1398 .flags = GENL_ADMIN_PERM,
1399 .policy = netlbl_unlabel_genl_policy,
1400 .doit = netlbl_unlabel_staticadddef,
1401 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001402 },
1403 {
Paul Moore8cc44572008-01-29 08:44:21 -05001404 .cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
1405 .flags = GENL_ADMIN_PERM,
1406 .policy = netlbl_unlabel_genl_policy,
1407 .doit = netlbl_unlabel_staticremovedef,
1408 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001409 },
1410 {
Paul Moore8cc44572008-01-29 08:44:21 -05001411 .cmd = NLBL_UNLABEL_C_STATICLISTDEF,
1412 .flags = 0,
1413 .policy = netlbl_unlabel_genl_policy,
1414 .doit = NULL,
1415 .dumpit = netlbl_unlabel_staticlistdef,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001416 },
1417 {
Paul Moore96cb8e32006-08-03 16:48:59 -07001418 .cmd = NLBL_UNLABEL_C_ACCEPT,
Paul Moorefd385852006-09-25 15:56:37 -07001419 .flags = GENL_ADMIN_PERM,
1420 .policy = netlbl_unlabel_genl_policy,
Paul Moore96cb8e32006-08-03 16:48:59 -07001421 .doit = netlbl_unlabel_accept,
1422 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001423 },
1424 {
Paul Moore96cb8e32006-08-03 16:48:59 -07001425 .cmd = NLBL_UNLABEL_C_LIST,
1426 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -07001427 .policy = netlbl_unlabel_genl_policy,
Paul Moore96cb8e32006-08-03 16:48:59 -07001428 .doit = netlbl_unlabel_list,
1429 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001430 },
Paul Moore96cb8e32006-08-03 16:48:59 -07001431};
1432
Paul Moore96cb8e32006-08-03 16:48:59 -07001433/*
1434 * NetLabel Generic NETLINK Protocol Functions
1435 */
1436
1437/**
1438 * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
1439 *
1440 * Description:
1441 * Register the unlabeled packet NetLabel component with the Generic NETLINK
1442 * mechanism. Returns zero on success, negative values on failure.
1443 *
1444 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -08001445int __init netlbl_unlabel_genl_init(void)
Paul Moore96cb8e32006-08-03 16:48:59 -07001446{
Michał Mirosław7ae740d2009-05-21 10:34:05 +00001447 return genl_register_family_with_ops(&netlbl_unlabel_gnl_family,
1448 netlbl_unlabel_genl_ops, ARRAY_SIZE(netlbl_unlabel_genl_ops));
Paul Moore96cb8e32006-08-03 16:48:59 -07001449}
1450
1451/*
1452 * NetLabel KAPI Hooks
1453 */
1454
Paul Moore8cc44572008-01-29 08:44:21 -05001455static struct notifier_block netlbl_unlhsh_netdev_notifier = {
1456 .notifier_call = netlbl_unlhsh_netdev_handler,
1457};
1458
1459/**
1460 * netlbl_unlabel_init - Initialize the unlabeled connection hash table
1461 * @size: the number of bits to use for the hash buckets
1462 *
1463 * Description:
1464 * Initializes the unlabeled connection hash table and registers a network
1465 * device notification handler. This function should only be called by the
1466 * NetLabel subsystem itself during initialization. Returns zero on success,
1467 * non-zero values on error.
1468 *
1469 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -08001470int __init netlbl_unlabel_init(u32 size)
Paul Moore8cc44572008-01-29 08:44:21 -05001471{
1472 u32 iter;
1473 struct netlbl_unlhsh_tbl *hsh_tbl;
1474
1475 if (size == 0)
1476 return -EINVAL;
1477
1478 hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
1479 if (hsh_tbl == NULL)
1480 return -ENOMEM;
1481 hsh_tbl->size = 1 << size;
1482 hsh_tbl->tbl = kcalloc(hsh_tbl->size,
1483 sizeof(struct list_head),
1484 GFP_KERNEL);
1485 if (hsh_tbl->tbl == NULL) {
1486 kfree(hsh_tbl);
1487 return -ENOMEM;
1488 }
1489 for (iter = 0; iter < hsh_tbl->size; iter++)
1490 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
1491
1492 rcu_read_lock();
1493 spin_lock(&netlbl_unlhsh_lock);
1494 rcu_assign_pointer(netlbl_unlhsh, hsh_tbl);
1495 spin_unlock(&netlbl_unlhsh_lock);
1496 rcu_read_unlock();
1497
1498 register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier);
1499
1500 return 0;
1501}
1502
Paul Moore96cb8e32006-08-03 16:48:59 -07001503/**
1504 * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
Paul Moore8cc44572008-01-29 08:44:21 -05001505 * @skb: the packet
1506 * @family: protocol family
Paul Moore96cb8e32006-08-03 16:48:59 -07001507 * @secattr: the security attributes
1508 *
1509 * Description:
1510 * Determine the security attributes, if any, for an unlabled packet and return
1511 * them in @secattr. Returns zero on success and negative values on failure.
1512 *
1513 */
Paul Moore8cc44572008-01-29 08:44:21 -05001514int netlbl_unlabel_getattr(const struct sk_buff *skb,
1515 u16 family,
1516 struct netlbl_lsm_secattr *secattr)
Paul Moore96cb8e32006-08-03 16:48:59 -07001517{
Paul Moore8cc44572008-01-29 08:44:21 -05001518 struct netlbl_unlhsh_iface *iface;
1519
1520 rcu_read_lock();
Paul Mooreb914f3a2010-04-01 10:43:57 +00001521 iface = netlbl_unlhsh_search_iface(skb->skb_iif);
Paul Moore8cc44572008-01-29 08:44:21 -05001522 if (iface == NULL)
Paul Mooreb914f3a2010-04-01 10:43:57 +00001523 iface = rcu_dereference(netlbl_unlhsh_def);
1524 if (iface == NULL || !iface->valid)
Paul Moore8cc44572008-01-29 08:44:21 -05001525 goto unlabel_getattr_nolabel;
1526 switch (family) {
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001527 case PF_INET: {
1528 struct iphdr *hdr4;
Paul Moore61e10682008-10-10 10:16:32 -04001529 struct netlbl_af4list *addr4;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001530
Paul Moore8cc44572008-01-29 08:44:21 -05001531 hdr4 = ip_hdr(skb);
Paul Moore61e10682008-10-10 10:16:32 -04001532 addr4 = netlbl_af4list_search(hdr4->saddr,
1533 &iface->addr4_list);
Paul Moore8cc44572008-01-29 08:44:21 -05001534 if (addr4 == NULL)
1535 goto unlabel_getattr_nolabel;
Paul Moore61e10682008-10-10 10:16:32 -04001536 secattr->attr.secid = netlbl_unlhsh_addr4_entry(addr4)->secid;
Paul Moore8cc44572008-01-29 08:44:21 -05001537 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001538 }
Paul Moore8cc44572008-01-29 08:44:21 -05001539#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001540 case PF_INET6: {
1541 struct ipv6hdr *hdr6;
Paul Moore61e10682008-10-10 10:16:32 -04001542 struct netlbl_af6list *addr6;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001543
Paul Moore8cc44572008-01-29 08:44:21 -05001544 hdr6 = ipv6_hdr(skb);
Paul Moore61e10682008-10-10 10:16:32 -04001545 addr6 = netlbl_af6list_search(&hdr6->saddr,
1546 &iface->addr6_list);
Paul Moore8cc44572008-01-29 08:44:21 -05001547 if (addr6 == NULL)
1548 goto unlabel_getattr_nolabel;
Paul Moore61e10682008-10-10 10:16:32 -04001549 secattr->attr.secid = netlbl_unlhsh_addr6_entry(addr6)->secid;
Paul Moore8cc44572008-01-29 08:44:21 -05001550 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001551 }
Paul Moore8cc44572008-01-29 08:44:21 -05001552#endif /* IPv6 */
1553 default:
1554 goto unlabel_getattr_nolabel;
1555 }
1556 rcu_read_unlock();
1557
1558 secattr->flags |= NETLBL_SECATTR_SECID;
1559 secattr->type = NETLBL_NLTYPE_UNLABELED;
1560 return 0;
1561
1562unlabel_getattr_nolabel:
1563 rcu_read_unlock();
Paul Moorec783f1c2008-01-29 08:37:52 -05001564 if (netlabel_unlabel_acceptflg == 0)
1565 return -ENOMSG;
Paul Moore16efd452008-01-29 08:37:59 -05001566 secattr->type = NETLBL_NLTYPE_UNLABELED;
Paul Moorec783f1c2008-01-29 08:37:52 -05001567 return 0;
Paul Moore96cb8e32006-08-03 16:48:59 -07001568}
1569
1570/**
1571 * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
1572 *
1573 * Description:
1574 * Set the default NetLabel configuration to allow incoming unlabeled packets
1575 * and to send unlabeled network traffic by default.
1576 *
1577 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -08001578int __init netlbl_unlabel_defconf(void)
Paul Moore96cb8e32006-08-03 16:48:59 -07001579{
1580 int ret_val;
1581 struct netlbl_dom_map *entry;
Paul Moore95d4e6b2006-09-29 17:05:05 -07001582 struct netlbl_audit audit_info;
Paul Moore32f50cd2006-09-28 14:51:47 -07001583
Paul Moore95d4e6b2006-09-29 17:05:05 -07001584 /* Only the kernel is allowed to call this function and the only time
1585 * it is called is at bootup before the audit subsystem is reporting
1586 * messages so don't worry to much about these values. */
1587 security_task_getsecid(current, &audit_info.secid);
1588 audit_info.loginuid = 0;
Eric Paris25323862008-04-18 10:09:25 -04001589 audit_info.sessionid = 0;
Paul Moore96cb8e32006-08-03 16:48:59 -07001590
1591 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1592 if (entry == NULL)
1593 return -ENOMEM;
1594 entry->type = NETLBL_NLTYPE_UNLABELED;
Paul Moore95d4e6b2006-09-29 17:05:05 -07001595 ret_val = netlbl_domhsh_add_default(entry, &audit_info);
Paul Moore96cb8e32006-08-03 16:48:59 -07001596 if (ret_val != 0)
1597 return ret_val;
1598
Paul Moore95d4e6b2006-09-29 17:05:05 -07001599 netlbl_unlabel_acceptflg_set(1, &audit_info);
Paul Moore96cb8e32006-08-03 16:48:59 -07001600
1601 return 0;
1602}