blob: 9c38658fba8b9afc61fdf5a158331a8f8a69c167 [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
Paul Moore8cc44572008-01-29 08:44:21 -0500156/**
157 * netlbl_unlhsh_free_iface - Frees an interface 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 interface entry can be
163 * released safely. It is important to note that this function does not free
164 * the IPv4 and IPv6 address lists contained as part of an interface entry. It
165 * is up to the rest of the code to make sure an interface entry is only freed
166 * once it's address lists are empty.
167 *
168 */
169static void netlbl_unlhsh_free_iface(struct rcu_head *entry)
170{
171 struct netlbl_unlhsh_iface *iface;
Paul Moore61e10682008-10-10 10:16:32 -0400172 struct netlbl_af4list *iter4;
173 struct netlbl_af4list *tmp4;
174#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
175 struct netlbl_af6list *iter6;
176 struct netlbl_af6list *tmp6;
177#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500178
179 iface = container_of(entry, struct netlbl_unlhsh_iface, rcu);
180
181 /* no need for locks here since we are the only one with access to this
182 * structure */
183
Paul Moore61e10682008-10-10 10:16:32 -0400184 netlbl_af4list_foreach_safe(iter4, tmp4, &iface->addr4_list) {
185 netlbl_af4list_remove_entry(iter4);
186 kfree(netlbl_unlhsh_addr4_entry(iter4));
187 }
188#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
189 netlbl_af6list_foreach_safe(iter6, tmp6, &iface->addr6_list) {
190 netlbl_af6list_remove_entry(iter6);
191 kfree(netlbl_unlhsh_addr6_entry(iter6));
192 }
193#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500194 kfree(iface);
195}
196
197/**
198 * netlbl_unlhsh_hash - Hashing function for the hash table
199 * @ifindex: the network interface/device to hash
200 *
201 * Description:
202 * This is the hashing function for the unlabeled hash table, it returns the
203 * bucket number for the given device/interface. The caller is responsible for
Paul Mooreb914f3a2010-04-01 10:43:57 +0000204 * ensuring that the hash table is protected with either a RCU read lock or
205 * the hash table lock.
Paul Moore8cc44572008-01-29 08:44:21 -0500206 *
207 */
208static u32 netlbl_unlhsh_hash(int ifindex)
209{
Paul Mooreb914f3a2010-04-01 10:43:57 +0000210 return ifindex & (netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->size - 1);
Paul Moore8cc44572008-01-29 08:44:21 -0500211}
212
213/**
Paul Moore8cc44572008-01-29 08:44:21 -0500214 * netlbl_unlhsh_search_iface - Search for a matching interface entry
215 * @ifindex: the network interface
216 *
217 * Description:
218 * Searches the unlabeled connection hash table and returns a pointer to the
219 * interface entry which matches @ifindex, otherwise NULL is returned. The
Paul Mooreb914f3a2010-04-01 10:43:57 +0000220 * caller is responsible for ensuring that the hash table is protected with
221 * either a RCU read lock or the hash table lock.
Paul Moore8cc44572008-01-29 08:44:21 -0500222 *
223 */
224static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface(int ifindex)
225{
226 u32 bkt;
Paul Moore56196702008-10-10 10:16:29 -0400227 struct list_head *bkt_list;
Paul Moore8cc44572008-01-29 08:44:21 -0500228 struct netlbl_unlhsh_iface *iter;
229
230 bkt = netlbl_unlhsh_hash(ifindex);
Paul Mooreb914f3a2010-04-01 10:43:57 +0000231 bkt_list = &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt];
Paul Moore56196702008-10-10 10:16:29 -0400232 list_for_each_entry_rcu(iter, bkt_list, list)
Paul Moore8cc44572008-01-29 08:44:21 -0500233 if (iter->valid && iter->ifindex == ifindex)
234 return iter;
235
236 return NULL;
237}
238
239/**
Paul Moore8cc44572008-01-29 08:44:21 -0500240 * netlbl_unlhsh_add_addr4 - Add a new IPv4 address entry to the hash table
241 * @iface: the associated interface entry
242 * @addr: IPv4 address in network byte order
243 * @mask: IPv4 address mask in network byte order
244 * @secid: LSM secid value for entry
245 *
246 * Description:
247 * Add a new address entry into the unlabeled connection hash table using the
248 * interface entry specified by @iface. On success zero is returned, otherwise
Paul Mooreb914f3a2010-04-01 10:43:57 +0000249 * a negative value is returned.
Paul Moore8cc44572008-01-29 08:44:21 -0500250 *
251 */
252static int netlbl_unlhsh_add_addr4(struct netlbl_unlhsh_iface *iface,
253 const struct in_addr *addr,
254 const struct in_addr *mask,
255 u32 secid)
256{
Paul Moore61e10682008-10-10 10:16:32 -0400257 int ret_val;
Paul Moore8cc44572008-01-29 08:44:21 -0500258 struct netlbl_unlhsh_addr4 *entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500259
260 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
261 if (entry == NULL)
262 return -ENOMEM;
263
Paul Moore61e10682008-10-10 10:16:32 -0400264 entry->list.addr = addr->s_addr & mask->s_addr;
265 entry->list.mask = mask->s_addr;
266 entry->list.valid = 1;
Paul Moore61e10682008-10-10 10:16:32 -0400267 entry->secid = secid;
Paul Moore8cc44572008-01-29 08:44:21 -0500268
269 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400270 ret_val = netlbl_af4list_add(&entry->list, &iface->addr4_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500271 spin_unlock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400272
273 if (ret_val != 0)
274 kfree(entry);
275 return ret_val;
Paul Moore8cc44572008-01-29 08:44:21 -0500276}
277
278#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
279/**
280 * netlbl_unlhsh_add_addr6 - Add a new IPv6 address entry to the hash table
281 * @iface: the associated interface entry
282 * @addr: IPv6 address in network byte order
283 * @mask: IPv6 address mask in network byte order
284 * @secid: LSM secid value for entry
285 *
286 * Description:
287 * Add a new address entry into the unlabeled connection hash table using the
288 * interface entry specified by @iface. On success zero is returned, otherwise
Paul Mooreb914f3a2010-04-01 10:43:57 +0000289 * a negative value is returned.
Paul Moore8cc44572008-01-29 08:44:21 -0500290 *
291 */
292static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface,
293 const struct in6_addr *addr,
294 const struct in6_addr *mask,
295 u32 secid)
296{
Paul Moore61e10682008-10-10 10:16:32 -0400297 int ret_val;
Paul Moore8cc44572008-01-29 08:44:21 -0500298 struct netlbl_unlhsh_addr6 *entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500299
300 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
301 if (entry == NULL)
302 return -ENOMEM;
303
Paul Moore61e10682008-10-10 10:16:32 -0400304 ipv6_addr_copy(&entry->list.addr, addr);
305 entry->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
306 entry->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
307 entry->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
308 entry->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
309 ipv6_addr_copy(&entry->list.mask, mask);
310 entry->list.valid = 1;
Paul Moore61e10682008-10-10 10:16:32 -0400311 entry->secid = secid;
Paul Moore8cc44572008-01-29 08:44:21 -0500312
313 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400314 ret_val = netlbl_af6list_add(&entry->list, &iface->addr6_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500315 spin_unlock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400316
317 if (ret_val != 0)
318 kfree(entry);
Paul Moore8cc44572008-01-29 08:44:21 -0500319 return 0;
320}
321#endif /* IPv6 */
322
323/**
324 * netlbl_unlhsh_add_iface - Adds a new interface entry to the hash table
325 * @ifindex: network interface
326 *
327 * Description:
328 * Add a new, empty, interface entry into the unlabeled connection hash table.
329 * On success a pointer to the new interface entry is returned, on failure NULL
Paul Mooreb914f3a2010-04-01 10:43:57 +0000330 * is returned.
Paul Moore8cc44572008-01-29 08:44:21 -0500331 *
332 */
333static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex)
334{
335 u32 bkt;
336 struct netlbl_unlhsh_iface *iface;
337
338 iface = kzalloc(sizeof(*iface), GFP_ATOMIC);
339 if (iface == NULL)
340 return NULL;
341
342 iface->ifindex = ifindex;
343 INIT_LIST_HEAD(&iface->addr4_list);
344 INIT_LIST_HEAD(&iface->addr6_list);
345 iface->valid = 1;
Paul Moore8cc44572008-01-29 08:44:21 -0500346
347 spin_lock(&netlbl_unlhsh_lock);
348 if (ifindex > 0) {
349 bkt = netlbl_unlhsh_hash(ifindex);
350 if (netlbl_unlhsh_search_iface(ifindex) != NULL)
351 goto add_iface_failure;
352 list_add_tail_rcu(&iface->list,
Paul Mooreb914f3a2010-04-01 10:43:57 +0000353 &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt]);
Paul Moore8cc44572008-01-29 08:44:21 -0500354 } else {
355 INIT_LIST_HEAD(&iface->list);
Paul Mooreb914f3a2010-04-01 10:43:57 +0000356 if (netlbl_unlhsh_rcu_deref(netlbl_unlhsh_def) != NULL)
Paul Moore8cc44572008-01-29 08:44:21 -0500357 goto add_iface_failure;
358 rcu_assign_pointer(netlbl_unlhsh_def, iface);
359 }
360 spin_unlock(&netlbl_unlhsh_lock);
361
362 return iface;
363
364add_iface_failure:
365 spin_unlock(&netlbl_unlhsh_lock);
366 kfree(iface);
367 return NULL;
368}
369
370/**
371 * netlbl_unlhsh_add - Adds a new entry to the unlabeled connection hash table
372 * @net: network namespace
373 * @dev_name: interface name
374 * @addr: IP address in network byte order
375 * @mask: address mask in network byte order
376 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
377 * @secid: LSM secid value for the entry
Paul Moore13541b32008-01-29 08:44:23 -0500378 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500379 *
380 * Description:
381 * Adds a new entry to the unlabeled connection hash table. Returns zero on
382 * success, negative values on failure.
383 *
384 */
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500385int netlbl_unlhsh_add(struct net *net,
386 const char *dev_name,
387 const void *addr,
388 const void *mask,
389 u32 addr_len,
390 u32 secid,
391 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500392{
393 int ret_val;
394 int ifindex;
395 struct net_device *dev;
396 struct netlbl_unlhsh_iface *iface;
Paul Moore13541b32008-01-29 08:44:23 -0500397 struct audit_buffer *audit_buf = NULL;
398 char *secctx = NULL;
399 u32 secctx_len;
Paul Moore8cc44572008-01-29 08:44:21 -0500400
401 if (addr_len != sizeof(struct in_addr) &&
402 addr_len != sizeof(struct in6_addr))
403 return -EINVAL;
404
405 rcu_read_lock();
406 if (dev_name != NULL) {
Eric Dumazet122ec6f2009-11-05 20:53:47 -0800407 dev = dev_get_by_name_rcu(net, dev_name);
Paul Moore8cc44572008-01-29 08:44:21 -0500408 if (dev == NULL) {
409 ret_val = -ENODEV;
410 goto unlhsh_add_return;
411 }
412 ifindex = dev->ifindex;
Paul Moore8cc44572008-01-29 08:44:21 -0500413 iface = netlbl_unlhsh_search_iface(ifindex);
414 } else {
415 ifindex = 0;
416 iface = rcu_dereference(netlbl_unlhsh_def);
417 }
418 if (iface == NULL) {
419 iface = netlbl_unlhsh_add_iface(ifindex);
420 if (iface == NULL) {
421 ret_val = -ENOMEM;
422 goto unlhsh_add_return;
423 }
424 }
Paul Moore13541b32008-01-29 08:44:23 -0500425 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCADD,
426 audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500427 switch (addr_len) {
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800428 case sizeof(struct in_addr): {
429 struct in_addr *addr4, *mask4;
430
Paul Moore13541b32008-01-29 08:44:23 -0500431 addr4 = (struct in_addr *)addr;
432 mask4 = (struct in_addr *)mask;
433 ret_val = netlbl_unlhsh_add_addr4(iface, addr4, mask4, secid);
434 if (audit_buf != NULL)
Paul Moore63c41682008-10-10 10:16:32 -0400435 netlbl_af4list_audit_addr(audit_buf, 1,
436 dev_name,
437 addr4->s_addr,
438 mask4->s_addr);
Paul Moore8cc44572008-01-29 08:44:21 -0500439 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800440 }
Paul Moore8cc44572008-01-29 08:44:21 -0500441#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800442 case sizeof(struct in6_addr): {
443 struct in6_addr *addr6, *mask6;
444
Paul Moore13541b32008-01-29 08:44:23 -0500445 addr6 = (struct in6_addr *)addr;
446 mask6 = (struct in6_addr *)mask;
447 ret_val = netlbl_unlhsh_add_addr6(iface, addr6, mask6, secid);
448 if (audit_buf != NULL)
Paul Moore63c41682008-10-10 10:16:32 -0400449 netlbl_af6list_audit_addr(audit_buf, 1,
450 dev_name,
451 addr6, mask6);
Paul Moore8cc44572008-01-29 08:44:21 -0500452 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800453 }
Paul Moore8cc44572008-01-29 08:44:21 -0500454#endif /* IPv6 */
455 default:
456 ret_val = -EINVAL;
457 }
458 if (ret_val == 0)
459 atomic_inc(&netlabel_mgmt_protocount);
460
461unlhsh_add_return:
462 rcu_read_unlock();
Paul Moore13541b32008-01-29 08:44:23 -0500463 if (audit_buf != NULL) {
464 if (security_secid_to_secctx(secid,
465 &secctx,
466 &secctx_len) == 0) {
467 audit_log_format(audit_buf, " sec_obj=%s", secctx);
468 security_release_secctx(secctx, secctx_len);
469 }
470 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
471 audit_log_end(audit_buf);
472 }
Paul Moore8cc44572008-01-29 08:44:21 -0500473 return ret_val;
474}
475
476/**
477 * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry
Paul Moore13541b32008-01-29 08:44:23 -0500478 * @net: network namespace
Paul Moore8cc44572008-01-29 08:44:21 -0500479 * @iface: interface entry
480 * @addr: IP address
481 * @mask: IP address mask
Paul Moore13541b32008-01-29 08:44:23 -0500482 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500483 *
484 * Description:
485 * Remove an IP address entry from the unlabeled connection hash table.
Paul Mooreb914f3a2010-04-01 10:43:57 +0000486 * Returns zero on success, negative values on failure.
Paul Moore8cc44572008-01-29 08:44:21 -0500487 *
488 */
Paul Moore13541b32008-01-29 08:44:23 -0500489static int netlbl_unlhsh_remove_addr4(struct net *net,
490 struct netlbl_unlhsh_iface *iface,
Paul Moore8cc44572008-01-29 08:44:21 -0500491 const struct in_addr *addr,
Paul Moore13541b32008-01-29 08:44:23 -0500492 const struct in_addr *mask,
493 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500494{
Paul Moore61e10682008-10-10 10:16:32 -0400495 struct netlbl_af4list *list_entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500496 struct netlbl_unlhsh_addr4 *entry;
Paul Moore61e10682008-10-10 10:16:32 -0400497 struct audit_buffer *audit_buf;
Paul Moore13541b32008-01-29 08:44:23 -0500498 struct net_device *dev;
Paul Moore61e10682008-10-10 10:16:32 -0400499 char *secctx;
Paul Moore13541b32008-01-29 08:44:23 -0500500 u32 secctx_len;
Paul Moore8cc44572008-01-29 08:44:21 -0500501
502 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400503 list_entry = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
504 &iface->addr4_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500505 spin_unlock(&netlbl_unlhsh_lock);
Paul Moored25830e2008-12-03 00:37:04 -0800506 if (list_entry != NULL)
507 entry = netlbl_unlhsh_addr4_entry(list_entry);
508 else
Paul Mooreec8f2372008-12-11 21:31:50 -0800509 entry = NULL;
Paul Moore8cc44572008-01-29 08:44:21 -0500510
Paul Moore13541b32008-01-29 08:44:23 -0500511 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
512 audit_info);
513 if (audit_buf != NULL) {
514 dev = dev_get_by_index(net, iface->ifindex);
Paul Moore63c41682008-10-10 10:16:32 -0400515 netlbl_af4list_audit_addr(audit_buf, 1,
516 (dev != NULL ? dev->name : NULL),
517 addr->s_addr, mask->s_addr);
Paul Moore13541b32008-01-29 08:44:23 -0500518 if (dev != NULL)
519 dev_put(dev);
Paul Mooreec8f2372008-12-11 21:31:50 -0800520 if (entry != NULL &&
521 security_secid_to_secctx(entry->secid,
522 &secctx, &secctx_len) == 0) {
Paul Moore13541b32008-01-29 08:44:23 -0500523 audit_log_format(audit_buf, " sec_obj=%s", secctx);
524 security_release_secctx(secctx, secctx_len);
525 }
Paul Mooreec8f2372008-12-11 21:31:50 -0800526 audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
Paul Moore13541b32008-01-29 08:44:23 -0500527 audit_log_end(audit_buf);
528 }
529
Paul Mooreec8f2372008-12-11 21:31:50 -0800530 if (entry == NULL)
531 return -ENOENT;
532
Lai Jiangshanc3b49422011-03-18 12:03:56 +0800533 kfree_rcu(entry, rcu);
Paul Mooreec8f2372008-12-11 21:31:50 -0800534 return 0;
Paul Moore8cc44572008-01-29 08:44:21 -0500535}
536
537#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
538/**
539 * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry
Paul Moore13541b32008-01-29 08:44:23 -0500540 * @net: network namespace
Paul Moore8cc44572008-01-29 08:44:21 -0500541 * @iface: interface entry
542 * @addr: IP address
543 * @mask: IP address mask
Paul Moore13541b32008-01-29 08:44:23 -0500544 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500545 *
546 * Description:
547 * Remove an IP address entry from the unlabeled connection hash table.
Paul Mooreb914f3a2010-04-01 10:43:57 +0000548 * Returns zero on success, negative values on failure.
Paul Moore8cc44572008-01-29 08:44:21 -0500549 *
550 */
Paul Moore13541b32008-01-29 08:44:23 -0500551static int netlbl_unlhsh_remove_addr6(struct net *net,
552 struct netlbl_unlhsh_iface *iface,
Paul Moore8cc44572008-01-29 08:44:21 -0500553 const struct in6_addr *addr,
Paul Moore13541b32008-01-29 08:44:23 -0500554 const struct in6_addr *mask,
555 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500556{
Paul Moore61e10682008-10-10 10:16:32 -0400557 struct netlbl_af6list *list_entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500558 struct netlbl_unlhsh_addr6 *entry;
Paul Moore61e10682008-10-10 10:16:32 -0400559 struct audit_buffer *audit_buf;
Paul Moore13541b32008-01-29 08:44:23 -0500560 struct net_device *dev;
Paul Moore61e10682008-10-10 10:16:32 -0400561 char *secctx;
Paul Moore13541b32008-01-29 08:44:23 -0500562 u32 secctx_len;
Paul Moore8cc44572008-01-29 08:44:21 -0500563
564 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400565 list_entry = netlbl_af6list_remove(addr, mask, &iface->addr6_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500566 spin_unlock(&netlbl_unlhsh_lock);
Paul Moored25830e2008-12-03 00:37:04 -0800567 if (list_entry != NULL)
568 entry = netlbl_unlhsh_addr6_entry(list_entry);
569 else
Paul Mooreec8f2372008-12-11 21:31:50 -0800570 entry = NULL;
Paul Moore8cc44572008-01-29 08:44:21 -0500571
Paul Moore13541b32008-01-29 08:44:23 -0500572 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
573 audit_info);
574 if (audit_buf != NULL) {
575 dev = dev_get_by_index(net, iface->ifindex);
Paul Moore63c41682008-10-10 10:16:32 -0400576 netlbl_af6list_audit_addr(audit_buf, 1,
577 (dev != NULL ? dev->name : NULL),
578 addr, mask);
Paul Moore13541b32008-01-29 08:44:23 -0500579 if (dev != NULL)
580 dev_put(dev);
Paul Mooreec8f2372008-12-11 21:31:50 -0800581 if (entry != NULL &&
582 security_secid_to_secctx(entry->secid,
583 &secctx, &secctx_len) == 0) {
Paul Moore13541b32008-01-29 08:44:23 -0500584 audit_log_format(audit_buf, " sec_obj=%s", secctx);
585 security_release_secctx(secctx, secctx_len);
586 }
Paul Mooreec8f2372008-12-11 21:31:50 -0800587 audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
Paul Moore13541b32008-01-29 08:44:23 -0500588 audit_log_end(audit_buf);
589 }
590
Paul Mooreec8f2372008-12-11 21:31:50 -0800591 if (entry == NULL)
592 return -ENOENT;
593
Lai Jiangshan6b262232011-03-18 12:04:50 +0800594 kfree_rcu(entry, rcu);
Paul Mooreec8f2372008-12-11 21:31:50 -0800595 return 0;
Paul Moore8cc44572008-01-29 08:44:21 -0500596}
597#endif /* IPv6 */
598
599/**
600 * netlbl_unlhsh_condremove_iface - Remove an interface entry
601 * @iface: the interface entry
602 *
603 * Description:
604 * Remove an interface entry from the unlabeled connection hash table if it is
605 * empty. An interface entry is considered to be empty if there are no
606 * address entries assigned to it.
607 *
608 */
609static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface)
610{
Paul Moore61e10682008-10-10 10:16:32 -0400611 struct netlbl_af4list *iter4;
612#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
613 struct netlbl_af6list *iter6;
614#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500615
616 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400617 netlbl_af4list_foreach_rcu(iter4, &iface->addr4_list)
618 goto unlhsh_condremove_failure;
619#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
620 netlbl_af6list_foreach_rcu(iter6, &iface->addr6_list)
621 goto unlhsh_condremove_failure;
622#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500623 iface->valid = 0;
624 if (iface->ifindex > 0)
625 list_del_rcu(&iface->list);
626 else
627 rcu_assign_pointer(netlbl_unlhsh_def, NULL);
628 spin_unlock(&netlbl_unlhsh_lock);
629
630 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
631 return;
632
633unlhsh_condremove_failure:
634 spin_unlock(&netlbl_unlhsh_lock);
Paul Moore8cc44572008-01-29 08:44:21 -0500635}
636
637/**
638 * netlbl_unlhsh_remove - Remove an entry from the unlabeled hash table
639 * @net: network namespace
640 * @dev_name: interface name
641 * @addr: IP address in network byte order
642 * @mask: address mask in network byte order
643 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
Paul Moore13541b32008-01-29 08:44:23 -0500644 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500645 *
646 * Description:
647 * Removes and existing entry from the unlabeled connection hash table.
648 * Returns zero on success, negative values on failure.
649 *
650 */
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500651int netlbl_unlhsh_remove(struct net *net,
652 const char *dev_name,
653 const void *addr,
654 const void *mask,
655 u32 addr_len,
656 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500657{
658 int ret_val;
659 struct net_device *dev;
660 struct netlbl_unlhsh_iface *iface;
661
662 if (addr_len != sizeof(struct in_addr) &&
663 addr_len != sizeof(struct in6_addr))
664 return -EINVAL;
665
666 rcu_read_lock();
667 if (dev_name != NULL) {
Eric Dumazet122ec6f2009-11-05 20:53:47 -0800668 dev = dev_get_by_name_rcu(net, dev_name);
Paul Moore8cc44572008-01-29 08:44:21 -0500669 if (dev == NULL) {
670 ret_val = -ENODEV;
671 goto unlhsh_remove_return;
672 }
673 iface = netlbl_unlhsh_search_iface(dev->ifindex);
Paul Moore8cc44572008-01-29 08:44:21 -0500674 } else
675 iface = rcu_dereference(netlbl_unlhsh_def);
676 if (iface == NULL) {
677 ret_val = -ENOENT;
678 goto unlhsh_remove_return;
679 }
680 switch (addr_len) {
681 case sizeof(struct in_addr):
Paul Moore13541b32008-01-29 08:44:23 -0500682 ret_val = netlbl_unlhsh_remove_addr4(net,
683 iface, addr, mask,
684 audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500685 break;
686#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
687 case sizeof(struct in6_addr):
Paul Moore13541b32008-01-29 08:44:23 -0500688 ret_val = netlbl_unlhsh_remove_addr6(net,
689 iface, addr, mask,
690 audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500691 break;
692#endif /* IPv6 */
693 default:
694 ret_val = -EINVAL;
695 }
696 if (ret_val == 0) {
697 netlbl_unlhsh_condremove_iface(iface);
698 atomic_dec(&netlabel_mgmt_protocount);
699 }
700
701unlhsh_remove_return:
702 rcu_read_unlock();
703 return ret_val;
704}
705
706/*
707 * General Helper Functions
708 */
709
710/**
711 * netlbl_unlhsh_netdev_handler - Network device notification handler
712 * @this: notifier block
713 * @event: the event
714 * @ptr: the network device (cast to void)
715 *
716 * Description:
717 * Handle network device events, although at present all we care about is a
718 * network device going away. In the case of a device going away we clear any
719 * related entries from the unlabeled connection hash table.
720 *
721 */
722static int netlbl_unlhsh_netdev_handler(struct notifier_block *this,
723 unsigned long event,
724 void *ptr)
725{
726 struct net_device *dev = ptr;
727 struct netlbl_unlhsh_iface *iface = NULL;
728
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700729 if (!net_eq(dev_net(dev), &init_net))
Paul Moore8cc44572008-01-29 08:44:21 -0500730 return NOTIFY_DONE;
731
732 /* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */
733 if (event == NETDEV_DOWN) {
734 spin_lock(&netlbl_unlhsh_lock);
735 iface = netlbl_unlhsh_search_iface(dev->ifindex);
736 if (iface != NULL && iface->valid) {
737 iface->valid = 0;
738 list_del_rcu(&iface->list);
739 } else
740 iface = NULL;
741 spin_unlock(&netlbl_unlhsh_lock);
742 }
743
744 if (iface != NULL)
745 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
746
747 return NOTIFY_DONE;
748}
749
750/**
Paul Moore32f50cd2006-09-28 14:51:47 -0700751 * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
752 * @value: desired value
Paul Moore95d4e6b2006-09-29 17:05:05 -0700753 * @audit_info: NetLabel audit information
Paul Moore32f50cd2006-09-28 14:51:47 -0700754 *
755 * Description:
756 * Set the value of the unlabeled accept flag to @value.
757 *
758 */
Paul Moore95d4e6b2006-09-29 17:05:05 -0700759static void netlbl_unlabel_acceptflg_set(u8 value,
760 struct netlbl_audit *audit_info)
Paul Moore32f50cd2006-09-28 14:51:47 -0700761{
Paul Moore95d4e6b2006-09-29 17:05:05 -0700762 struct audit_buffer *audit_buf;
763 u8 old_val;
764
Paul Moore4be27002007-10-26 04:29:08 -0700765 old_val = netlabel_unlabel_acceptflg;
Paul Moorecd287862006-11-17 17:38:44 -0500766 netlabel_unlabel_acceptflg = value;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700767 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
768 audit_info);
Paul Moorede646882006-11-17 17:38:55 -0500769 if (audit_buf != NULL) {
770 audit_log_format(audit_buf,
771 " unlbl_accept=%u old=%u", value, old_val);
772 audit_log_end(audit_buf);
773 }
Paul Moore32f50cd2006-09-28 14:51:47 -0700774}
775
Paul Moore8cc44572008-01-29 08:44:21 -0500776/**
777 * netlbl_unlabel_addrinfo_get - Get the IPv4/6 address information
778 * @info: the Generic NETLINK info block
779 * @addr: the IP address
780 * @mask: the IP address mask
781 * @len: the address length
782 *
783 * Description:
784 * Examine the Generic NETLINK message and extract the IP address information.
785 * Returns zero on success, negative values on failure.
786 *
787 */
788static int netlbl_unlabel_addrinfo_get(struct genl_info *info,
789 void **addr,
790 void **mask,
791 u32 *len)
792{
793 u32 addr_len;
794
795 if (info->attrs[NLBL_UNLABEL_A_IPV4ADDR]) {
796 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
797 if (addr_len != sizeof(struct in_addr) &&
798 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK]))
799 return -EINVAL;
800 *len = addr_len;
801 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
802 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]);
803 return 0;
804 } else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) {
805 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
806 if (addr_len != sizeof(struct in6_addr) &&
807 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK]))
808 return -EINVAL;
809 *len = addr_len;
810 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
811 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]);
812 return 0;
813 }
814
815 return -EINVAL;
816}
817
Paul Moore32f50cd2006-09-28 14:51:47 -0700818/*
Paul Moore96cb8e32006-08-03 16:48:59 -0700819 * NetLabel Command Handlers
820 */
821
822/**
823 * netlbl_unlabel_accept - Handle an ACCEPT message
824 * @skb: the NETLINK buffer
825 * @info: the Generic NETLINK info block
826 *
827 * Description:
828 * Process a user generated ACCEPT message and set the accept flag accordingly.
829 * Returns zero on success, negative values on failure.
830 *
831 */
832static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
833{
Paul Moorefd385852006-09-25 15:56:37 -0700834 u8 value;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700835 struct netlbl_audit audit_info;
Paul Moore96cb8e32006-08-03 16:48:59 -0700836
Paul Moorefd385852006-09-25 15:56:37 -0700837 if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
838 value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
Paul Moore96cb8e32006-08-03 16:48:59 -0700839 if (value == 1 || value == 0) {
Paul Moore95d4e6b2006-09-29 17:05:05 -0700840 netlbl_netlink_auditinfo(skb, &audit_info);
841 netlbl_unlabel_acceptflg_set(value, &audit_info);
Paul Moore32f50cd2006-09-28 14:51:47 -0700842 return 0;
Paul Moore96cb8e32006-08-03 16:48:59 -0700843 }
844 }
845
Paul Moore32f50cd2006-09-28 14:51:47 -0700846 return -EINVAL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700847}
848
849/**
850 * netlbl_unlabel_list - Handle a LIST message
851 * @skb: the NETLINK buffer
852 * @info: the Generic NETLINK info block
853 *
854 * Description:
855 * Process a user generated LIST message and respond with the current status.
856 * Returns zero on success, negative values on failure.
857 *
858 */
859static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
860{
Paul Moorefd385852006-09-25 15:56:37 -0700861 int ret_val = -EINVAL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700862 struct sk_buff *ans_skb;
Paul Moorefd385852006-09-25 15:56:37 -0700863 void *data;
Paul Moore96cb8e32006-08-03 16:48:59 -0700864
Thomas Graf339bf982006-11-10 14:10:15 -0800865 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Paul Moore96cb8e32006-08-03 16:48:59 -0700866 if (ans_skb == NULL)
867 goto list_failure;
Thomas Graf17c157c2006-11-14 19:46:02 -0800868 data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
869 0, NLBL_UNLABEL_C_LIST);
Paul Moorefd385852006-09-25 15:56:37 -0700870 if (data == NULL) {
871 ret_val = -ENOMEM;
Paul Moore96cb8e32006-08-03 16:48:59 -0700872 goto list_failure;
Paul Moorefd385852006-09-25 15:56:37 -0700873 }
Paul Moore96cb8e32006-08-03 16:48:59 -0700874
Paul Moorefd385852006-09-25 15:56:37 -0700875 ret_val = nla_put_u8(ans_skb,
876 NLBL_UNLABEL_A_ACPTFLG,
Paul Moorecd287862006-11-17 17:38:44 -0500877 netlabel_unlabel_acceptflg);
Paul Moore96cb8e32006-08-03 16:48:59 -0700878 if (ret_val != 0)
879 goto list_failure;
880
Paul Moorefd385852006-09-25 15:56:37 -0700881 genlmsg_end(ans_skb, data);
Denis V. Lunevfe785be2008-07-10 16:53:39 -0700882 return genlmsg_reply(ans_skb, info);
Paul Moore96cb8e32006-08-03 16:48:59 -0700883
884list_failure:
Patrick McHardyb08d5842007-02-27 09:57:37 -0800885 kfree_skb(ans_skb);
Paul Moore96cb8e32006-08-03 16:48:59 -0700886 return ret_val;
887}
888
Paul Moore8cc44572008-01-29 08:44:21 -0500889/**
890 * netlbl_unlabel_staticadd - Handle a STATICADD message
891 * @skb: the NETLINK buffer
892 * @info: the Generic NETLINK info block
893 *
894 * Description:
895 * Process a user generated STATICADD message and add a new unlabeled
896 * connection entry to the hash table. Returns zero on success, negative
897 * values on failure.
898 *
899 */
900static int netlbl_unlabel_staticadd(struct sk_buff *skb,
901 struct genl_info *info)
902{
903 int ret_val;
904 char *dev_name;
905 void *addr;
906 void *mask;
907 u32 addr_len;
908 u32 secid;
Paul Moore13541b32008-01-29 08:44:23 -0500909 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -0500910
911 /* Don't allow users to add both IPv4 and IPv6 addresses for a
912 * single entry. However, allow users to create two entries, one each
913 * for IPv4 and IPv4, with the same LSM security context which should
914 * achieve the same result. */
915 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
916 !info->attrs[NLBL_UNLABEL_A_IFACE] ||
917 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
918 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
919 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
920 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
921 return -EINVAL;
922
Paul Moore13541b32008-01-29 08:44:23 -0500923 netlbl_netlink_auditinfo(skb, &audit_info);
924
Paul Moore8cc44572008-01-29 08:44:21 -0500925 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
926 if (ret_val != 0)
927 return ret_val;
928 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
929 ret_val = security_secctx_to_secid(
930 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
931 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
932 &secid);
933 if (ret_val != 0)
934 return ret_val;
935
936 return netlbl_unlhsh_add(&init_net,
Paul Moore13541b32008-01-29 08:44:23 -0500937 dev_name, addr, mask, addr_len, secid,
938 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500939}
940
941/**
942 * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message
943 * @skb: the NETLINK buffer
944 * @info: the Generic NETLINK info block
945 *
946 * Description:
947 * Process a user generated STATICADDDEF message and add a new default
948 * unlabeled connection entry. Returns zero on success, negative values on
949 * failure.
950 *
951 */
952static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
953 struct genl_info *info)
954{
955 int ret_val;
956 void *addr;
957 void *mask;
958 u32 addr_len;
959 u32 secid;
Paul Moore13541b32008-01-29 08:44:23 -0500960 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -0500961
962 /* Don't allow users to add both IPv4 and IPv6 addresses for a
963 * single entry. However, allow users to create two entries, one each
964 * for IPv4 and IPv6, with the same LSM security context which should
965 * achieve the same result. */
966 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
967 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
968 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
969 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
970 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
971 return -EINVAL;
972
Paul Moore13541b32008-01-29 08:44:23 -0500973 netlbl_netlink_auditinfo(skb, &audit_info);
974
Paul Moore8cc44572008-01-29 08:44:21 -0500975 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
976 if (ret_val != 0)
977 return ret_val;
978 ret_val = security_secctx_to_secid(
979 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
980 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
981 &secid);
982 if (ret_val != 0)
983 return ret_val;
984
Paul Moore13541b32008-01-29 08:44:23 -0500985 return netlbl_unlhsh_add(&init_net,
986 NULL, addr, mask, addr_len, secid,
987 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500988}
989
990/**
991 * netlbl_unlabel_staticremove - Handle a STATICREMOVE message
992 * @skb: the NETLINK buffer
993 * @info: the Generic NETLINK info block
994 *
995 * Description:
996 * Process a user generated STATICREMOVE message and remove the specified
997 * unlabeled connection entry. Returns zero on success, negative values on
998 * failure.
999 *
1000 */
1001static int netlbl_unlabel_staticremove(struct sk_buff *skb,
1002 struct genl_info *info)
1003{
1004 int ret_val;
1005 char *dev_name;
1006 void *addr;
1007 void *mask;
1008 u32 addr_len;
Paul Moore13541b32008-01-29 08:44:23 -05001009 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -05001010
1011 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1012 * IPv4 and IPv6 in the same entry. */
1013 if (!info->attrs[NLBL_UNLABEL_A_IFACE] ||
1014 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1015 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1016 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1017 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1018 return -EINVAL;
1019
Paul Moore13541b32008-01-29 08:44:23 -05001020 netlbl_netlink_auditinfo(skb, &audit_info);
1021
Paul Moore8cc44572008-01-29 08:44:21 -05001022 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1023 if (ret_val != 0)
1024 return ret_val;
1025 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
1026
Paul Moore13541b32008-01-29 08:44:23 -05001027 return netlbl_unlhsh_remove(&init_net,
1028 dev_name, addr, mask, addr_len,
1029 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -05001030}
1031
1032/**
1033 * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message
1034 * @skb: the NETLINK buffer
1035 * @info: the Generic NETLINK info block
1036 *
1037 * Description:
1038 * Process a user generated STATICREMOVEDEF message and remove the default
1039 * unlabeled connection entry. Returns zero on success, negative values on
1040 * failure.
1041 *
1042 */
1043static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
1044 struct genl_info *info)
1045{
1046 int ret_val;
1047 void *addr;
1048 void *mask;
1049 u32 addr_len;
Paul Moore13541b32008-01-29 08:44:23 -05001050 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -05001051
1052 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1053 * IPv4 and IPv6 in the same entry. */
1054 if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1055 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1056 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1057 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1058 return -EINVAL;
1059
Paul Moore13541b32008-01-29 08:44:23 -05001060 netlbl_netlink_auditinfo(skb, &audit_info);
1061
Paul Moore8cc44572008-01-29 08:44:21 -05001062 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1063 if (ret_val != 0)
1064 return ret_val;
1065
Paul Moore13541b32008-01-29 08:44:23 -05001066 return netlbl_unlhsh_remove(&init_net,
1067 NULL, addr, mask, addr_len,
1068 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -05001069}
1070
1071
1072/**
1073 * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF]
1074 * @cmd: command/message
1075 * @iface: the interface entry
1076 * @addr4: the IPv4 address entry
1077 * @addr6: the IPv6 address entry
1078 * @arg: the netlbl_unlhsh_walk_arg structure
1079 *
1080 * Description:
1081 * This function is designed to be used to generate a response for a
1082 * STATICLIST or STATICLISTDEF message. When called either @addr4 or @addr6
1083 * can be specified, not both, the other unspecified entry should be set to
1084 * NULL by the caller. Returns the size of the message on success, negative
1085 * values on failure.
1086 *
1087 */
1088static int netlbl_unlabel_staticlist_gen(u32 cmd,
1089 const struct netlbl_unlhsh_iface *iface,
1090 const struct netlbl_unlhsh_addr4 *addr4,
1091 const struct netlbl_unlhsh_addr6 *addr6,
1092 void *arg)
1093{
1094 int ret_val = -ENOMEM;
1095 struct netlbl_unlhsh_walk_arg *cb_arg = arg;
1096 struct net_device *dev;
1097 void *data;
1098 u32 secid;
1099 char *secctx;
1100 u32 secctx_len;
1101
1102 data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
1103 cb_arg->seq, &netlbl_unlabel_gnl_family,
1104 NLM_F_MULTI, cmd);
1105 if (data == NULL)
1106 goto list_cb_failure;
1107
1108 if (iface->ifindex > 0) {
1109 dev = dev_get_by_index(&init_net, iface->ifindex);
Jesper Juhl794eb6b2008-04-17 23:22:54 -07001110 if (!dev) {
1111 ret_val = -ENODEV;
1112 goto list_cb_failure;
1113 }
Paul Moore8cc44572008-01-29 08:44:21 -05001114 ret_val = nla_put_string(cb_arg->skb,
1115 NLBL_UNLABEL_A_IFACE, dev->name);
1116 dev_put(dev);
1117 if (ret_val != 0)
1118 goto list_cb_failure;
1119 }
1120
1121 if (addr4) {
1122 struct in_addr addr_struct;
1123
Paul Moore61e10682008-10-10 10:16:32 -04001124 addr_struct.s_addr = addr4->list.addr;
Paul Moore8cc44572008-01-29 08:44:21 -05001125 ret_val = nla_put(cb_arg->skb,
1126 NLBL_UNLABEL_A_IPV4ADDR,
1127 sizeof(struct in_addr),
1128 &addr_struct);
1129 if (ret_val != 0)
1130 goto list_cb_failure;
1131
Paul Moore61e10682008-10-10 10:16:32 -04001132 addr_struct.s_addr = addr4->list.mask;
Paul Moore8cc44572008-01-29 08:44:21 -05001133 ret_val = nla_put(cb_arg->skb,
1134 NLBL_UNLABEL_A_IPV4MASK,
1135 sizeof(struct in_addr),
1136 &addr_struct);
1137 if (ret_val != 0)
1138 goto list_cb_failure;
1139
1140 secid = addr4->secid;
1141 } else {
1142 ret_val = nla_put(cb_arg->skb,
1143 NLBL_UNLABEL_A_IPV6ADDR,
1144 sizeof(struct in6_addr),
Paul Moore61e10682008-10-10 10:16:32 -04001145 &addr6->list.addr);
Paul Moore8cc44572008-01-29 08:44:21 -05001146 if (ret_val != 0)
1147 goto list_cb_failure;
1148
1149 ret_val = nla_put(cb_arg->skb,
1150 NLBL_UNLABEL_A_IPV6MASK,
1151 sizeof(struct in6_addr),
Paul Moore61e10682008-10-10 10:16:32 -04001152 &addr6->list.mask);
Paul Moore8cc44572008-01-29 08:44:21 -05001153 if (ret_val != 0)
1154 goto list_cb_failure;
1155
1156 secid = addr6->secid;
1157 }
1158
1159 ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len);
1160 if (ret_val != 0)
1161 goto list_cb_failure;
1162 ret_val = nla_put(cb_arg->skb,
1163 NLBL_UNLABEL_A_SECCTX,
1164 secctx_len,
1165 secctx);
1166 security_release_secctx(secctx, secctx_len);
1167 if (ret_val != 0)
1168 goto list_cb_failure;
1169
1170 cb_arg->seq++;
1171 return genlmsg_end(cb_arg->skb, data);
1172
1173list_cb_failure:
1174 genlmsg_cancel(cb_arg->skb, data);
1175 return ret_val;
1176}
1177
1178/**
1179 * netlbl_unlabel_staticlist - Handle a STATICLIST message
1180 * @skb: the NETLINK buffer
1181 * @cb: the NETLINK callback
1182 *
1183 * Description:
1184 * Process a user generated STATICLIST message and dump the unlabeled
1185 * connection hash table in a form suitable for use in a kernel generated
1186 * STATICLIST message. Returns the length of @skb.
1187 *
1188 */
1189static int netlbl_unlabel_staticlist(struct sk_buff *skb,
1190 struct netlink_callback *cb)
1191{
1192 struct netlbl_unlhsh_walk_arg cb_arg;
1193 u32 skip_bkt = cb->args[0];
1194 u32 skip_chain = cb->args[1];
1195 u32 skip_addr4 = cb->args[2];
1196 u32 skip_addr6 = cb->args[3];
1197 u32 iter_bkt;
1198 u32 iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
1199 struct netlbl_unlhsh_iface *iface;
Paul Moore56196702008-10-10 10:16:29 -04001200 struct list_head *iter_list;
Paul Moore61e10682008-10-10 10:16:32 -04001201 struct netlbl_af4list *addr4;
1202#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1203 struct netlbl_af6list *addr6;
1204#endif
Paul Moore8cc44572008-01-29 08:44:21 -05001205
1206 cb_arg.nl_cb = cb;
1207 cb_arg.skb = skb;
1208 cb_arg.seq = cb->nlh->nlmsg_seq;
1209
1210 rcu_read_lock();
1211 for (iter_bkt = skip_bkt;
1212 iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
1213 iter_bkt++, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0) {
Paul Moore56196702008-10-10 10:16:29 -04001214 iter_list = &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt];
1215 list_for_each_entry_rcu(iface, iter_list, list) {
Paul Moore8cc44572008-01-29 08:44:21 -05001216 if (!iface->valid ||
1217 iter_chain++ < skip_chain)
1218 continue;
Paul Moore61e10682008-10-10 10:16:32 -04001219 netlbl_af4list_foreach_rcu(addr4,
1220 &iface->addr4_list) {
1221 if (iter_addr4++ < skip_addr4)
Paul Moore8cc44572008-01-29 08:44:21 -05001222 continue;
1223 if (netlbl_unlabel_staticlist_gen(
Paul Moore61e10682008-10-10 10:16:32 -04001224 NLBL_UNLABEL_C_STATICLIST,
1225 iface,
1226 netlbl_unlhsh_addr4_entry(addr4),
1227 NULL,
1228 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001229 iter_addr4--;
1230 iter_chain--;
1231 goto unlabel_staticlist_return;
1232 }
1233 }
Paul Moore61e10682008-10-10 10:16:32 -04001234#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1235 netlbl_af6list_foreach_rcu(addr6,
1236 &iface->addr6_list) {
1237 if (iter_addr6++ < skip_addr6)
Paul Moore8cc44572008-01-29 08:44:21 -05001238 continue;
1239 if (netlbl_unlabel_staticlist_gen(
Paul Moore61e10682008-10-10 10:16:32 -04001240 NLBL_UNLABEL_C_STATICLIST,
1241 iface,
1242 NULL,
1243 netlbl_unlhsh_addr6_entry(addr6),
1244 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001245 iter_addr6--;
1246 iter_chain--;
1247 goto unlabel_staticlist_return;
1248 }
1249 }
Paul Moore61e10682008-10-10 10:16:32 -04001250#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -05001251 }
1252 }
1253
1254unlabel_staticlist_return:
1255 rcu_read_unlock();
1256 cb->args[0] = skip_bkt;
1257 cb->args[1] = skip_chain;
1258 cb->args[2] = skip_addr4;
1259 cb->args[3] = skip_addr6;
1260 return skb->len;
1261}
1262
1263/**
1264 * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message
1265 * @skb: the NETLINK buffer
1266 * @cb: the NETLINK callback
1267 *
1268 * Description:
1269 * Process a user generated STATICLISTDEF message and dump the default
1270 * unlabeled connection entry in a form suitable for use in a kernel generated
1271 * STATICLISTDEF message. Returns the length of @skb.
1272 *
1273 */
1274static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
1275 struct netlink_callback *cb)
1276{
1277 struct netlbl_unlhsh_walk_arg cb_arg;
1278 struct netlbl_unlhsh_iface *iface;
1279 u32 skip_addr4 = cb->args[0];
1280 u32 skip_addr6 = cb->args[1];
Paul Moore61e10682008-10-10 10:16:32 -04001281 u32 iter_addr4 = 0;
1282 struct netlbl_af4list *addr4;
1283#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1284 u32 iter_addr6 = 0;
1285 struct netlbl_af6list *addr6;
1286#endif
Paul Moore8cc44572008-01-29 08:44:21 -05001287
1288 cb_arg.nl_cb = cb;
1289 cb_arg.skb = skb;
1290 cb_arg.seq = cb->nlh->nlmsg_seq;
1291
1292 rcu_read_lock();
1293 iface = rcu_dereference(netlbl_unlhsh_def);
1294 if (iface == NULL || !iface->valid)
1295 goto unlabel_staticlistdef_return;
1296
Paul Moore61e10682008-10-10 10:16:32 -04001297 netlbl_af4list_foreach_rcu(addr4, &iface->addr4_list) {
1298 if (iter_addr4++ < skip_addr4)
Paul Moore8cc44572008-01-29 08:44:21 -05001299 continue;
1300 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
Paul Moore61e10682008-10-10 10:16:32 -04001301 iface,
1302 netlbl_unlhsh_addr4_entry(addr4),
1303 NULL,
1304 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001305 iter_addr4--;
1306 goto unlabel_staticlistdef_return;
1307 }
1308 }
Paul Moore61e10682008-10-10 10:16:32 -04001309#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1310 netlbl_af6list_foreach_rcu(addr6, &iface->addr6_list) {
1311 if (iter_addr6++ < skip_addr6)
Paul Moore8cc44572008-01-29 08:44:21 -05001312 continue;
1313 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
Paul Moore61e10682008-10-10 10:16:32 -04001314 iface,
1315 NULL,
1316 netlbl_unlhsh_addr6_entry(addr6),
1317 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001318 iter_addr6--;
1319 goto unlabel_staticlistdef_return;
1320 }
1321 }
Paul Moore61e10682008-10-10 10:16:32 -04001322#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -05001323
1324unlabel_staticlistdef_return:
1325 rcu_read_unlock();
1326 cb->args[0] = skip_addr4;
1327 cb->args[1] = skip_addr6;
1328 return skb->len;
1329}
Paul Moore96cb8e32006-08-03 16:48:59 -07001330
1331/*
1332 * NetLabel Generic NETLINK Command Definitions
1333 */
1334
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001335static struct genl_ops netlbl_unlabel_genl_ops[] = {
1336 {
Paul Moore8cc44572008-01-29 08:44:21 -05001337 .cmd = NLBL_UNLABEL_C_STATICADD,
1338 .flags = GENL_ADMIN_PERM,
1339 .policy = netlbl_unlabel_genl_policy,
1340 .doit = netlbl_unlabel_staticadd,
1341 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001342 },
1343 {
Paul Moore8cc44572008-01-29 08:44:21 -05001344 .cmd = NLBL_UNLABEL_C_STATICREMOVE,
1345 .flags = GENL_ADMIN_PERM,
1346 .policy = netlbl_unlabel_genl_policy,
1347 .doit = netlbl_unlabel_staticremove,
1348 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001349 },
1350 {
Paul Moore8cc44572008-01-29 08:44:21 -05001351 .cmd = NLBL_UNLABEL_C_STATICLIST,
1352 .flags = 0,
1353 .policy = netlbl_unlabel_genl_policy,
1354 .doit = NULL,
1355 .dumpit = netlbl_unlabel_staticlist,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001356 },
1357 {
Paul Moore8cc44572008-01-29 08:44:21 -05001358 .cmd = NLBL_UNLABEL_C_STATICADDDEF,
1359 .flags = GENL_ADMIN_PERM,
1360 .policy = netlbl_unlabel_genl_policy,
1361 .doit = netlbl_unlabel_staticadddef,
1362 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001363 },
1364 {
Paul Moore8cc44572008-01-29 08:44:21 -05001365 .cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
1366 .flags = GENL_ADMIN_PERM,
1367 .policy = netlbl_unlabel_genl_policy,
1368 .doit = netlbl_unlabel_staticremovedef,
1369 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001370 },
1371 {
Paul Moore8cc44572008-01-29 08:44:21 -05001372 .cmd = NLBL_UNLABEL_C_STATICLISTDEF,
1373 .flags = 0,
1374 .policy = netlbl_unlabel_genl_policy,
1375 .doit = NULL,
1376 .dumpit = netlbl_unlabel_staticlistdef,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001377 },
1378 {
Paul Moore96cb8e32006-08-03 16:48:59 -07001379 .cmd = NLBL_UNLABEL_C_ACCEPT,
Paul Moorefd385852006-09-25 15:56:37 -07001380 .flags = GENL_ADMIN_PERM,
1381 .policy = netlbl_unlabel_genl_policy,
Paul Moore96cb8e32006-08-03 16:48:59 -07001382 .doit = netlbl_unlabel_accept,
1383 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001384 },
1385 {
Paul Moore96cb8e32006-08-03 16:48:59 -07001386 .cmd = NLBL_UNLABEL_C_LIST,
1387 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -07001388 .policy = netlbl_unlabel_genl_policy,
Paul Moore96cb8e32006-08-03 16:48:59 -07001389 .doit = netlbl_unlabel_list,
1390 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001391 },
Paul Moore96cb8e32006-08-03 16:48:59 -07001392};
1393
Paul Moore96cb8e32006-08-03 16:48:59 -07001394/*
1395 * NetLabel Generic NETLINK Protocol Functions
1396 */
1397
1398/**
1399 * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
1400 *
1401 * Description:
1402 * Register the unlabeled packet NetLabel component with the Generic NETLINK
1403 * mechanism. Returns zero on success, negative values on failure.
1404 *
1405 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -08001406int __init netlbl_unlabel_genl_init(void)
Paul Moore96cb8e32006-08-03 16:48:59 -07001407{
Michał Mirosław7ae740d2009-05-21 10:34:05 +00001408 return genl_register_family_with_ops(&netlbl_unlabel_gnl_family,
1409 netlbl_unlabel_genl_ops, ARRAY_SIZE(netlbl_unlabel_genl_ops));
Paul Moore96cb8e32006-08-03 16:48:59 -07001410}
1411
1412/*
1413 * NetLabel KAPI Hooks
1414 */
1415
Paul Moore8cc44572008-01-29 08:44:21 -05001416static struct notifier_block netlbl_unlhsh_netdev_notifier = {
1417 .notifier_call = netlbl_unlhsh_netdev_handler,
1418};
1419
1420/**
1421 * netlbl_unlabel_init - Initialize the unlabeled connection hash table
1422 * @size: the number of bits to use for the hash buckets
1423 *
1424 * Description:
1425 * Initializes the unlabeled connection hash table and registers a network
1426 * device notification handler. This function should only be called by the
1427 * NetLabel subsystem itself during initialization. Returns zero on success,
1428 * non-zero values on error.
1429 *
1430 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -08001431int __init netlbl_unlabel_init(u32 size)
Paul Moore8cc44572008-01-29 08:44:21 -05001432{
1433 u32 iter;
1434 struct netlbl_unlhsh_tbl *hsh_tbl;
1435
1436 if (size == 0)
1437 return -EINVAL;
1438
1439 hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
1440 if (hsh_tbl == NULL)
1441 return -ENOMEM;
1442 hsh_tbl->size = 1 << size;
1443 hsh_tbl->tbl = kcalloc(hsh_tbl->size,
1444 sizeof(struct list_head),
1445 GFP_KERNEL);
1446 if (hsh_tbl->tbl == NULL) {
1447 kfree(hsh_tbl);
1448 return -ENOMEM;
1449 }
1450 for (iter = 0; iter < hsh_tbl->size; iter++)
1451 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
1452
1453 rcu_read_lock();
1454 spin_lock(&netlbl_unlhsh_lock);
1455 rcu_assign_pointer(netlbl_unlhsh, hsh_tbl);
1456 spin_unlock(&netlbl_unlhsh_lock);
1457 rcu_read_unlock();
1458
1459 register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier);
1460
1461 return 0;
1462}
1463
Paul Moore96cb8e32006-08-03 16:48:59 -07001464/**
1465 * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
Paul Moore8cc44572008-01-29 08:44:21 -05001466 * @skb: the packet
1467 * @family: protocol family
Paul Moore96cb8e32006-08-03 16:48:59 -07001468 * @secattr: the security attributes
1469 *
1470 * Description:
1471 * Determine the security attributes, if any, for an unlabled packet and return
1472 * them in @secattr. Returns zero on success and negative values on failure.
1473 *
1474 */
Paul Moore8cc44572008-01-29 08:44:21 -05001475int netlbl_unlabel_getattr(const struct sk_buff *skb,
1476 u16 family,
1477 struct netlbl_lsm_secattr *secattr)
Paul Moore96cb8e32006-08-03 16:48:59 -07001478{
Paul Moore8cc44572008-01-29 08:44:21 -05001479 struct netlbl_unlhsh_iface *iface;
1480
1481 rcu_read_lock();
Paul Mooreb914f3a2010-04-01 10:43:57 +00001482 iface = netlbl_unlhsh_search_iface(skb->skb_iif);
Paul Moore8cc44572008-01-29 08:44:21 -05001483 if (iface == NULL)
Paul Mooreb914f3a2010-04-01 10:43:57 +00001484 iface = rcu_dereference(netlbl_unlhsh_def);
1485 if (iface == NULL || !iface->valid)
Paul Moore8cc44572008-01-29 08:44:21 -05001486 goto unlabel_getattr_nolabel;
1487 switch (family) {
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001488 case PF_INET: {
1489 struct iphdr *hdr4;
Paul Moore61e10682008-10-10 10:16:32 -04001490 struct netlbl_af4list *addr4;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001491
Paul Moore8cc44572008-01-29 08:44:21 -05001492 hdr4 = ip_hdr(skb);
Paul Moore61e10682008-10-10 10:16:32 -04001493 addr4 = netlbl_af4list_search(hdr4->saddr,
1494 &iface->addr4_list);
Paul Moore8cc44572008-01-29 08:44:21 -05001495 if (addr4 == NULL)
1496 goto unlabel_getattr_nolabel;
Paul Moore61e10682008-10-10 10:16:32 -04001497 secattr->attr.secid = netlbl_unlhsh_addr4_entry(addr4)->secid;
Paul Moore8cc44572008-01-29 08:44:21 -05001498 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001499 }
Paul Moore8cc44572008-01-29 08:44:21 -05001500#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001501 case PF_INET6: {
1502 struct ipv6hdr *hdr6;
Paul Moore61e10682008-10-10 10:16:32 -04001503 struct netlbl_af6list *addr6;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001504
Paul Moore8cc44572008-01-29 08:44:21 -05001505 hdr6 = ipv6_hdr(skb);
Paul Moore61e10682008-10-10 10:16:32 -04001506 addr6 = netlbl_af6list_search(&hdr6->saddr,
1507 &iface->addr6_list);
Paul Moore8cc44572008-01-29 08:44:21 -05001508 if (addr6 == NULL)
1509 goto unlabel_getattr_nolabel;
Paul Moore61e10682008-10-10 10:16:32 -04001510 secattr->attr.secid = netlbl_unlhsh_addr6_entry(addr6)->secid;
Paul Moore8cc44572008-01-29 08:44:21 -05001511 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001512 }
Paul Moore8cc44572008-01-29 08:44:21 -05001513#endif /* IPv6 */
1514 default:
1515 goto unlabel_getattr_nolabel;
1516 }
1517 rcu_read_unlock();
1518
1519 secattr->flags |= NETLBL_SECATTR_SECID;
1520 secattr->type = NETLBL_NLTYPE_UNLABELED;
1521 return 0;
1522
1523unlabel_getattr_nolabel:
1524 rcu_read_unlock();
Paul Moorec783f1c2008-01-29 08:37:52 -05001525 if (netlabel_unlabel_acceptflg == 0)
1526 return -ENOMSG;
Paul Moore16efd452008-01-29 08:37:59 -05001527 secattr->type = NETLBL_NLTYPE_UNLABELED;
Paul Moorec783f1c2008-01-29 08:37:52 -05001528 return 0;
Paul Moore96cb8e32006-08-03 16:48:59 -07001529}
1530
1531/**
1532 * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
1533 *
1534 * Description:
1535 * Set the default NetLabel configuration to allow incoming unlabeled packets
1536 * and to send unlabeled network traffic by default.
1537 *
1538 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -08001539int __init netlbl_unlabel_defconf(void)
Paul Moore96cb8e32006-08-03 16:48:59 -07001540{
1541 int ret_val;
1542 struct netlbl_dom_map *entry;
Paul Moore95d4e6b2006-09-29 17:05:05 -07001543 struct netlbl_audit audit_info;
Paul Moore32f50cd2006-09-28 14:51:47 -07001544
Paul Moore95d4e6b2006-09-29 17:05:05 -07001545 /* Only the kernel is allowed to call this function and the only time
1546 * it is called is at bootup before the audit subsystem is reporting
1547 * messages so don't worry to much about these values. */
1548 security_task_getsecid(current, &audit_info.secid);
1549 audit_info.loginuid = 0;
Eric Paris25323862008-04-18 10:09:25 -04001550 audit_info.sessionid = 0;
Paul Moore96cb8e32006-08-03 16:48:59 -07001551
1552 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1553 if (entry == NULL)
1554 return -ENOMEM;
1555 entry->type = NETLBL_NLTYPE_UNLABELED;
Paul Moore95d4e6b2006-09-29 17:05:05 -07001556 ret_val = netlbl_domhsh_add_default(entry, &audit_info);
Paul Moore96cb8e32006-08-03 16:48:59 -07001557 if (ret_val != 0)
1558 return ret_val;
1559
Paul Moore95d4e6b2006-09-29 17:05:05 -07001560 netlbl_unlabel_acceptflg_set(1, &audit_info);
Paul Moore96cb8e32006-08-03 16:48:59 -07001561
1562 return 0;
1563}