blob: 3b4fde7622a3e7a06caae914b4e4a781d06d833c [file] [log] [blame]
Paul Moore96cb8e32006-08-03 16:48:59 -07001/*
2 * NetLabel Unlabeled Support
3 *
4 * This file defines functions for dealing with unlabeled packets for the
5 * NetLabel system. The NetLabel system manages static and dynamic label
6 * mappings for network protocols such as CIPSO and RIPSO.
7 *
8 * Author: Paul Moore <paul.moore@hp.com>
9 *
10 */
11
12/*
Paul Moore61e10682008-10-10 10:16:32 -040013 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006 - 2008
Paul Moore96cb8e32006-08-03 16:48:59 -070014 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
23 * the GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 *
29 */
30
31#include <linux/types.h>
Paul Moore8cc44572008-01-29 08:44:21 -050032#include <linux/rcupdate.h>
Paul Moore96cb8e32006-08-03 16:48:59 -070033#include <linux/list.h>
34#include <linux/spinlock.h>
35#include <linux/socket.h>
36#include <linux/string.h>
37#include <linux/skbuff.h>
Paul Moorede646882006-11-17 17:38:55 -050038#include <linux/audit.h>
Paul Moore8cc44572008-01-29 08:44:21 -050039#include <linux/in.h>
40#include <linux/in6.h>
41#include <linux/ip.h>
42#include <linux/ipv6.h>
43#include <linux/notifier.h>
44#include <linux/netdevice.h>
45#include <linux/security.h>
Paul Moore96cb8e32006-08-03 16:48:59 -070046#include <net/sock.h>
47#include <net/netlink.h>
48#include <net/genetlink.h>
Paul Moore8cc44572008-01-29 08:44:21 -050049#include <net/ip.h>
50#include <net/ipv6.h>
51#include <net/net_namespace.h>
Paul Moore96cb8e32006-08-03 16:48:59 -070052#include <net/netlabel.h>
53#include <asm/bug.h>
Paul Moore8cc44572008-01-29 08:44:21 -050054#include <asm/atomic.h>
Paul Moore96cb8e32006-08-03 16:48:59 -070055
56#include "netlabel_user.h"
Paul Moore61e10682008-10-10 10:16:32 -040057#include "netlabel_addrlist.h"
Paul Moore96cb8e32006-08-03 16:48:59 -070058#include "netlabel_domainhash.h"
59#include "netlabel_unlabeled.h"
Paul Moore8cc44572008-01-29 08:44:21 -050060#include "netlabel_mgmt.h"
61
62/* NOTE: at present we always use init's network namespace since we don't
63 * presently support different namespaces even though the majority of
64 * the functions in this file are "namespace safe" */
65
66/* The unlabeled connection hash table which we use to map network interfaces
67 * and addresses of unlabeled packets to a user specified secid value for the
68 * LSM. The hash table is used to lookup the network interface entry
69 * (struct netlbl_unlhsh_iface) and then the interface entry is used to
70 * lookup an IP address match from an ordered list. If a network interface
71 * match can not be found in the hash table then the default entry
72 * (netlbl_unlhsh_def) is used. The IP address entry list
73 * (struct netlbl_unlhsh_addr) is ordered such that the entries with a
74 * larger netmask come first.
75 */
76struct netlbl_unlhsh_tbl {
77 struct list_head *tbl;
78 u32 size;
79};
Paul Moore61e10682008-10-10 10:16:32 -040080#define netlbl_unlhsh_addr4_entry(iter) \
81 container_of(iter, struct netlbl_unlhsh_addr4, list)
Paul Moore8cc44572008-01-29 08:44:21 -050082struct netlbl_unlhsh_addr4 {
Paul Moore8cc44572008-01-29 08:44:21 -050083 u32 secid;
84
Paul Moore61e10682008-10-10 10:16:32 -040085 struct netlbl_af4list list;
Paul Moore8cc44572008-01-29 08:44:21 -050086 struct rcu_head rcu;
87};
Paul Moore61e10682008-10-10 10:16:32 -040088#define netlbl_unlhsh_addr6_entry(iter) \
89 container_of(iter, struct netlbl_unlhsh_addr6, list)
Paul Moore8cc44572008-01-29 08:44:21 -050090struct netlbl_unlhsh_addr6 {
Paul Moore8cc44572008-01-29 08:44:21 -050091 u32 secid;
92
Paul Moore61e10682008-10-10 10:16:32 -040093 struct netlbl_af6list list;
Paul Moore8cc44572008-01-29 08:44:21 -050094 struct rcu_head rcu;
95};
96struct netlbl_unlhsh_iface {
97 int ifindex;
98 struct list_head addr4_list;
99 struct list_head addr6_list;
100
101 u32 valid;
102 struct list_head list;
103 struct rcu_head rcu;
104};
105
106/* Argument struct for netlbl_unlhsh_walk() */
107struct netlbl_unlhsh_walk_arg {
108 struct netlink_callback *nl_cb;
109 struct sk_buff *skb;
110 u32 seq;
111};
112
113/* Unlabeled connection hash table */
114/* updates should be so rare that having one spinlock for the entire
115 * hash table should be okay */
116static DEFINE_SPINLOCK(netlbl_unlhsh_lock);
Paul Mooreb914f3a2010-04-01 10:43:57 +0000117#define netlbl_unlhsh_rcu_deref(p) \
118 rcu_dereference_check(p, rcu_read_lock_held() || \
119 lockdep_is_held(&netlbl_unlhsh_lock))
Paul Moore8cc44572008-01-29 08:44:21 -0500120static struct netlbl_unlhsh_tbl *netlbl_unlhsh = NULL;
121static struct netlbl_unlhsh_iface *netlbl_unlhsh_def = NULL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700122
123/* Accept unlabeled packets flag */
Paul Moorecd287862006-11-17 17:38:44 -0500124static u8 netlabel_unlabel_acceptflg = 0;
Paul Moore96cb8e32006-08-03 16:48:59 -0700125
Paul Moore8cc44572008-01-29 08:44:21 -0500126/* NetLabel Generic NETLINK unlabeled family */
Paul Moore96cb8e32006-08-03 16:48:59 -0700127static struct genl_family netlbl_unlabel_gnl_family = {
128 .id = GENL_ID_GENERATE,
129 .hdrsize = 0,
130 .name = NETLBL_NLTYPE_UNLABELED_NAME,
131 .version = NETLBL_PROTO_VERSION,
Paul Moorefd385852006-09-25 15:56:37 -0700132 .maxattr = NLBL_UNLABEL_A_MAX,
Paul Moore96cb8e32006-08-03 16:48:59 -0700133};
134
Paul Moorefd385852006-09-25 15:56:37 -0700135/* NetLabel Netlink attribute policy */
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700136static const struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = {
Paul Moorefd385852006-09-25 15:56:37 -0700137 [NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 },
Paul Moore8cc44572008-01-29 08:44:21 -0500138 [NLBL_UNLABEL_A_IPV6ADDR] = { .type = NLA_BINARY,
139 .len = sizeof(struct in6_addr) },
140 [NLBL_UNLABEL_A_IPV6MASK] = { .type = NLA_BINARY,
141 .len = sizeof(struct in6_addr) },
142 [NLBL_UNLABEL_A_IPV4ADDR] = { .type = NLA_BINARY,
143 .len = sizeof(struct in_addr) },
144 [NLBL_UNLABEL_A_IPV4MASK] = { .type = NLA_BINARY,
145 .len = sizeof(struct in_addr) },
146 [NLBL_UNLABEL_A_IFACE] = { .type = NLA_NUL_STRING,
147 .len = IFNAMSIZ - 1 },
148 [NLBL_UNLABEL_A_SECCTX] = { .type = NLA_BINARY }
Paul Moorefd385852006-09-25 15:56:37 -0700149};
Paul Moore96cb8e32006-08-03 16:48:59 -0700150
151/*
Paul Moore8cc44572008-01-29 08:44:21 -0500152 * Unlabeled Connection Hash Table Functions
Paul Moore32f50cd2006-09-28 14:51:47 -0700153 */
154
155/**
Paul Moore8cc44572008-01-29 08:44:21 -0500156 * netlbl_unlhsh_free_addr4 - Frees an IPv4 address entry from the hash table
157 * @entry: the entry's RCU field
158 *
159 * Description:
160 * This function is designed to be used as a callback to the call_rcu()
161 * function so that memory allocated to a hash table address entry can be
162 * released safely.
163 *
164 */
165static void netlbl_unlhsh_free_addr4(struct rcu_head *entry)
166{
167 struct netlbl_unlhsh_addr4 *ptr;
168
169 ptr = container_of(entry, struct netlbl_unlhsh_addr4, rcu);
170 kfree(ptr);
171}
172
173#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
174/**
175 * netlbl_unlhsh_free_addr6 - Frees an IPv6 address entry from the hash table
176 * @entry: the entry's RCU field
177 *
178 * Description:
179 * This function is designed to be used as a callback to the call_rcu()
180 * function so that memory allocated to a hash table address entry can be
181 * released safely.
182 *
183 */
184static void netlbl_unlhsh_free_addr6(struct rcu_head *entry)
185{
186 struct netlbl_unlhsh_addr6 *ptr;
187
188 ptr = container_of(entry, struct netlbl_unlhsh_addr6, rcu);
189 kfree(ptr);
190}
191#endif /* IPv6 */
192
193/**
194 * netlbl_unlhsh_free_iface - Frees an interface entry from the hash table
195 * @entry: the entry's RCU field
196 *
197 * Description:
198 * This function is designed to be used as a callback to the call_rcu()
199 * function so that memory allocated to a hash table interface entry can be
200 * released safely. It is important to note that this function does not free
201 * the IPv4 and IPv6 address lists contained as part of an interface entry. It
202 * is up to the rest of the code to make sure an interface entry is only freed
203 * once it's address lists are empty.
204 *
205 */
206static void netlbl_unlhsh_free_iface(struct rcu_head *entry)
207{
208 struct netlbl_unlhsh_iface *iface;
Paul Moore61e10682008-10-10 10:16:32 -0400209 struct netlbl_af4list *iter4;
210 struct netlbl_af4list *tmp4;
211#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
212 struct netlbl_af6list *iter6;
213 struct netlbl_af6list *tmp6;
214#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500215
216 iface = container_of(entry, struct netlbl_unlhsh_iface, rcu);
217
218 /* no need for locks here since we are the only one with access to this
219 * structure */
220
Paul Moore61e10682008-10-10 10:16:32 -0400221 netlbl_af4list_foreach_safe(iter4, tmp4, &iface->addr4_list) {
222 netlbl_af4list_remove_entry(iter4);
223 kfree(netlbl_unlhsh_addr4_entry(iter4));
224 }
225#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
226 netlbl_af6list_foreach_safe(iter6, tmp6, &iface->addr6_list) {
227 netlbl_af6list_remove_entry(iter6);
228 kfree(netlbl_unlhsh_addr6_entry(iter6));
229 }
230#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500231 kfree(iface);
232}
233
234/**
235 * netlbl_unlhsh_hash - Hashing function for the hash table
236 * @ifindex: the network interface/device to hash
237 *
238 * Description:
239 * This is the hashing function for the unlabeled hash table, it returns the
240 * bucket number for the given device/interface. The caller is responsible for
Paul Mooreb914f3a2010-04-01 10:43:57 +0000241 * ensuring that the hash table is protected with either a RCU read lock or
242 * the hash table lock.
Paul Moore8cc44572008-01-29 08:44:21 -0500243 *
244 */
245static u32 netlbl_unlhsh_hash(int ifindex)
246{
Paul Mooreb914f3a2010-04-01 10:43:57 +0000247 return ifindex & (netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->size - 1);
Paul Moore8cc44572008-01-29 08:44:21 -0500248}
249
250/**
Paul Moore8cc44572008-01-29 08:44:21 -0500251 * netlbl_unlhsh_search_iface - Search for a matching interface entry
252 * @ifindex: the network interface
253 *
254 * Description:
255 * Searches the unlabeled connection hash table and returns a pointer to the
256 * interface entry which matches @ifindex, otherwise NULL is returned. The
Paul Mooreb914f3a2010-04-01 10:43:57 +0000257 * caller is responsible for ensuring that the hash table is protected with
258 * either a RCU read lock or the hash table lock.
Paul Moore8cc44572008-01-29 08:44:21 -0500259 *
260 */
261static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface(int ifindex)
262{
263 u32 bkt;
Paul Moore56196702008-10-10 10:16:29 -0400264 struct list_head *bkt_list;
Paul Moore8cc44572008-01-29 08:44:21 -0500265 struct netlbl_unlhsh_iface *iter;
266
267 bkt = netlbl_unlhsh_hash(ifindex);
Paul Mooreb914f3a2010-04-01 10:43:57 +0000268 bkt_list = &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt];
Paul Moore56196702008-10-10 10:16:29 -0400269 list_for_each_entry_rcu(iter, bkt_list, list)
Paul Moore8cc44572008-01-29 08:44:21 -0500270 if (iter->valid && iter->ifindex == ifindex)
271 return iter;
272
273 return NULL;
274}
275
276/**
Paul Moore8cc44572008-01-29 08:44:21 -0500277 * netlbl_unlhsh_add_addr4 - Add a new IPv4 address entry to the hash table
278 * @iface: the associated interface entry
279 * @addr: IPv4 address in network byte order
280 * @mask: IPv4 address mask in network byte order
281 * @secid: LSM secid value for entry
282 *
283 * Description:
284 * Add a new address entry into the unlabeled connection hash table using the
285 * interface entry specified by @iface. On success zero is returned, otherwise
Paul Mooreb914f3a2010-04-01 10:43:57 +0000286 * a negative value is returned.
Paul Moore8cc44572008-01-29 08:44:21 -0500287 *
288 */
289static int netlbl_unlhsh_add_addr4(struct netlbl_unlhsh_iface *iface,
290 const struct in_addr *addr,
291 const struct in_addr *mask,
292 u32 secid)
293{
Paul Moore61e10682008-10-10 10:16:32 -0400294 int ret_val;
Paul Moore8cc44572008-01-29 08:44:21 -0500295 struct netlbl_unlhsh_addr4 *entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500296
297 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
298 if (entry == NULL)
299 return -ENOMEM;
300
Paul Moore61e10682008-10-10 10:16:32 -0400301 entry->list.addr = addr->s_addr & mask->s_addr;
302 entry->list.mask = mask->s_addr;
303 entry->list.valid = 1;
Paul Moore61e10682008-10-10 10:16:32 -0400304 entry->secid = secid;
Paul Moore8cc44572008-01-29 08:44:21 -0500305
306 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400307 ret_val = netlbl_af4list_add(&entry->list, &iface->addr4_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500308 spin_unlock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400309
310 if (ret_val != 0)
311 kfree(entry);
312 return ret_val;
Paul Moore8cc44572008-01-29 08:44:21 -0500313}
314
315#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
316/**
317 * netlbl_unlhsh_add_addr6 - Add a new IPv6 address entry to the hash table
318 * @iface: the associated interface entry
319 * @addr: IPv6 address in network byte order
320 * @mask: IPv6 address mask in network byte order
321 * @secid: LSM secid value for entry
322 *
323 * Description:
324 * Add a new address entry into the unlabeled connection hash table using the
325 * interface entry specified by @iface. On success zero is returned, otherwise
Paul Mooreb914f3a2010-04-01 10:43:57 +0000326 * a negative value is returned.
Paul Moore8cc44572008-01-29 08:44:21 -0500327 *
328 */
329static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface,
330 const struct in6_addr *addr,
331 const struct in6_addr *mask,
332 u32 secid)
333{
Paul Moore61e10682008-10-10 10:16:32 -0400334 int ret_val;
Paul Moore8cc44572008-01-29 08:44:21 -0500335 struct netlbl_unlhsh_addr6 *entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500336
337 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
338 if (entry == NULL)
339 return -ENOMEM;
340
Paul Moore61e10682008-10-10 10:16:32 -0400341 ipv6_addr_copy(&entry->list.addr, addr);
342 entry->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
343 entry->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
344 entry->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
345 entry->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
346 ipv6_addr_copy(&entry->list.mask, mask);
347 entry->list.valid = 1;
Paul Moore61e10682008-10-10 10:16:32 -0400348 entry->secid = secid;
Paul Moore8cc44572008-01-29 08:44:21 -0500349
350 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400351 ret_val = netlbl_af6list_add(&entry->list, &iface->addr6_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500352 spin_unlock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400353
354 if (ret_val != 0)
355 kfree(entry);
Paul Moore8cc44572008-01-29 08:44:21 -0500356 return 0;
357}
358#endif /* IPv6 */
359
360/**
361 * netlbl_unlhsh_add_iface - Adds a new interface entry to the hash table
362 * @ifindex: network interface
363 *
364 * Description:
365 * Add a new, empty, interface entry into the unlabeled connection hash table.
366 * On success a pointer to the new interface entry is returned, on failure NULL
Paul Mooreb914f3a2010-04-01 10:43:57 +0000367 * is returned.
Paul Moore8cc44572008-01-29 08:44:21 -0500368 *
369 */
370static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex)
371{
372 u32 bkt;
373 struct netlbl_unlhsh_iface *iface;
374
375 iface = kzalloc(sizeof(*iface), GFP_ATOMIC);
376 if (iface == NULL)
377 return NULL;
378
379 iface->ifindex = ifindex;
380 INIT_LIST_HEAD(&iface->addr4_list);
381 INIT_LIST_HEAD(&iface->addr6_list);
382 iface->valid = 1;
Paul Moore8cc44572008-01-29 08:44:21 -0500383
384 spin_lock(&netlbl_unlhsh_lock);
385 if (ifindex > 0) {
386 bkt = netlbl_unlhsh_hash(ifindex);
387 if (netlbl_unlhsh_search_iface(ifindex) != NULL)
388 goto add_iface_failure;
389 list_add_tail_rcu(&iface->list,
Paul Mooreb914f3a2010-04-01 10:43:57 +0000390 &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt]);
Paul Moore8cc44572008-01-29 08:44:21 -0500391 } else {
392 INIT_LIST_HEAD(&iface->list);
Paul Mooreb914f3a2010-04-01 10:43:57 +0000393 if (netlbl_unlhsh_rcu_deref(netlbl_unlhsh_def) != NULL)
Paul Moore8cc44572008-01-29 08:44:21 -0500394 goto add_iface_failure;
395 rcu_assign_pointer(netlbl_unlhsh_def, iface);
396 }
397 spin_unlock(&netlbl_unlhsh_lock);
398
399 return iface;
400
401add_iface_failure:
402 spin_unlock(&netlbl_unlhsh_lock);
403 kfree(iface);
404 return NULL;
405}
406
407/**
408 * netlbl_unlhsh_add - Adds a new entry to the unlabeled connection hash table
409 * @net: network namespace
410 * @dev_name: interface name
411 * @addr: IP address in network byte order
412 * @mask: address mask in network byte order
413 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
414 * @secid: LSM secid value for the entry
Paul Moore13541b32008-01-29 08:44:23 -0500415 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500416 *
417 * Description:
418 * Adds a new entry to the unlabeled connection hash table. Returns zero on
419 * success, negative values on failure.
420 *
421 */
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500422int netlbl_unlhsh_add(struct net *net,
423 const char *dev_name,
424 const void *addr,
425 const void *mask,
426 u32 addr_len,
427 u32 secid,
428 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500429{
430 int ret_val;
431 int ifindex;
432 struct net_device *dev;
433 struct netlbl_unlhsh_iface *iface;
Paul Moore13541b32008-01-29 08:44:23 -0500434 struct audit_buffer *audit_buf = NULL;
435 char *secctx = NULL;
436 u32 secctx_len;
Paul Moore8cc44572008-01-29 08:44:21 -0500437
438 if (addr_len != sizeof(struct in_addr) &&
439 addr_len != sizeof(struct in6_addr))
440 return -EINVAL;
441
442 rcu_read_lock();
443 if (dev_name != NULL) {
Eric Dumazet122ec6f2009-11-05 20:53:47 -0800444 dev = dev_get_by_name_rcu(net, dev_name);
Paul Moore8cc44572008-01-29 08:44:21 -0500445 if (dev == NULL) {
446 ret_val = -ENODEV;
447 goto unlhsh_add_return;
448 }
449 ifindex = dev->ifindex;
Paul Moore8cc44572008-01-29 08:44:21 -0500450 iface = netlbl_unlhsh_search_iface(ifindex);
451 } else {
452 ifindex = 0;
453 iface = rcu_dereference(netlbl_unlhsh_def);
454 }
455 if (iface == NULL) {
456 iface = netlbl_unlhsh_add_iface(ifindex);
457 if (iface == NULL) {
458 ret_val = -ENOMEM;
459 goto unlhsh_add_return;
460 }
461 }
Paul Moore13541b32008-01-29 08:44:23 -0500462 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCADD,
463 audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500464 switch (addr_len) {
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800465 case sizeof(struct in_addr): {
466 struct in_addr *addr4, *mask4;
467
Paul Moore13541b32008-01-29 08:44:23 -0500468 addr4 = (struct in_addr *)addr;
469 mask4 = (struct in_addr *)mask;
470 ret_val = netlbl_unlhsh_add_addr4(iface, addr4, mask4, secid);
471 if (audit_buf != NULL)
Paul Moore63c41682008-10-10 10:16:32 -0400472 netlbl_af4list_audit_addr(audit_buf, 1,
473 dev_name,
474 addr4->s_addr,
475 mask4->s_addr);
Paul Moore8cc44572008-01-29 08:44:21 -0500476 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800477 }
Paul Moore8cc44572008-01-29 08:44:21 -0500478#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800479 case sizeof(struct in6_addr): {
480 struct in6_addr *addr6, *mask6;
481
Paul Moore13541b32008-01-29 08:44:23 -0500482 addr6 = (struct in6_addr *)addr;
483 mask6 = (struct in6_addr *)mask;
484 ret_val = netlbl_unlhsh_add_addr6(iface, addr6, mask6, secid);
485 if (audit_buf != NULL)
Paul Moore63c41682008-10-10 10:16:32 -0400486 netlbl_af6list_audit_addr(audit_buf, 1,
487 dev_name,
488 addr6, mask6);
Paul Moore8cc44572008-01-29 08:44:21 -0500489 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -0800490 }
Paul Moore8cc44572008-01-29 08:44:21 -0500491#endif /* IPv6 */
492 default:
493 ret_val = -EINVAL;
494 }
495 if (ret_val == 0)
496 atomic_inc(&netlabel_mgmt_protocount);
497
498unlhsh_add_return:
499 rcu_read_unlock();
Paul Moore13541b32008-01-29 08:44:23 -0500500 if (audit_buf != NULL) {
501 if (security_secid_to_secctx(secid,
502 &secctx,
503 &secctx_len) == 0) {
504 audit_log_format(audit_buf, " sec_obj=%s", secctx);
505 security_release_secctx(secctx, secctx_len);
506 }
507 audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
508 audit_log_end(audit_buf);
509 }
Paul Moore8cc44572008-01-29 08:44:21 -0500510 return ret_val;
511}
512
513/**
514 * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry
Paul Moore13541b32008-01-29 08:44:23 -0500515 * @net: network namespace
Paul Moore8cc44572008-01-29 08:44:21 -0500516 * @iface: interface entry
517 * @addr: IP address
518 * @mask: IP address mask
Paul Moore13541b32008-01-29 08:44:23 -0500519 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500520 *
521 * Description:
522 * Remove an IP address entry from the unlabeled connection hash table.
Paul Mooreb914f3a2010-04-01 10:43:57 +0000523 * Returns zero on success, negative values on failure.
Paul Moore8cc44572008-01-29 08:44:21 -0500524 *
525 */
Paul Moore13541b32008-01-29 08:44:23 -0500526static int netlbl_unlhsh_remove_addr4(struct net *net,
527 struct netlbl_unlhsh_iface *iface,
Paul Moore8cc44572008-01-29 08:44:21 -0500528 const struct in_addr *addr,
Paul Moore13541b32008-01-29 08:44:23 -0500529 const struct in_addr *mask,
530 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500531{
Paul Moore61e10682008-10-10 10:16:32 -0400532 struct netlbl_af4list *list_entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500533 struct netlbl_unlhsh_addr4 *entry;
Paul Moore61e10682008-10-10 10:16:32 -0400534 struct audit_buffer *audit_buf;
Paul Moore13541b32008-01-29 08:44:23 -0500535 struct net_device *dev;
Paul Moore61e10682008-10-10 10:16:32 -0400536 char *secctx;
Paul Moore13541b32008-01-29 08:44:23 -0500537 u32 secctx_len;
Paul Moore8cc44572008-01-29 08:44:21 -0500538
539 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400540 list_entry = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
541 &iface->addr4_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500542 spin_unlock(&netlbl_unlhsh_lock);
Paul Moored25830e2008-12-03 00:37:04 -0800543 if (list_entry != NULL)
544 entry = netlbl_unlhsh_addr4_entry(list_entry);
545 else
Paul Mooreec8f2372008-12-11 21:31:50 -0800546 entry = NULL;
Paul Moore8cc44572008-01-29 08:44:21 -0500547
Paul Moore13541b32008-01-29 08:44:23 -0500548 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
549 audit_info);
550 if (audit_buf != NULL) {
551 dev = dev_get_by_index(net, iface->ifindex);
Paul Moore63c41682008-10-10 10:16:32 -0400552 netlbl_af4list_audit_addr(audit_buf, 1,
553 (dev != NULL ? dev->name : NULL),
554 addr->s_addr, mask->s_addr);
Paul Moore13541b32008-01-29 08:44:23 -0500555 if (dev != NULL)
556 dev_put(dev);
Paul Mooreec8f2372008-12-11 21:31:50 -0800557 if (entry != NULL &&
558 security_secid_to_secctx(entry->secid,
559 &secctx, &secctx_len) == 0) {
Paul Moore13541b32008-01-29 08:44:23 -0500560 audit_log_format(audit_buf, " sec_obj=%s", secctx);
561 security_release_secctx(secctx, secctx_len);
562 }
Paul Mooreec8f2372008-12-11 21:31:50 -0800563 audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
Paul Moore13541b32008-01-29 08:44:23 -0500564 audit_log_end(audit_buf);
565 }
566
Paul Mooreec8f2372008-12-11 21:31:50 -0800567 if (entry == NULL)
568 return -ENOENT;
569
570 call_rcu(&entry->rcu, netlbl_unlhsh_free_addr4);
571 return 0;
Paul Moore8cc44572008-01-29 08:44:21 -0500572}
573
574#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
575/**
576 * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry
Paul Moore13541b32008-01-29 08:44:23 -0500577 * @net: network namespace
Paul Moore8cc44572008-01-29 08:44:21 -0500578 * @iface: interface entry
579 * @addr: IP address
580 * @mask: IP address mask
Paul Moore13541b32008-01-29 08:44:23 -0500581 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500582 *
583 * Description:
584 * Remove an IP address entry from the unlabeled connection hash table.
Paul Mooreb914f3a2010-04-01 10:43:57 +0000585 * Returns zero on success, negative values on failure.
Paul Moore8cc44572008-01-29 08:44:21 -0500586 *
587 */
Paul Moore13541b32008-01-29 08:44:23 -0500588static int netlbl_unlhsh_remove_addr6(struct net *net,
589 struct netlbl_unlhsh_iface *iface,
Paul Moore8cc44572008-01-29 08:44:21 -0500590 const struct in6_addr *addr,
Paul Moore13541b32008-01-29 08:44:23 -0500591 const struct in6_addr *mask,
592 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500593{
Paul Moore61e10682008-10-10 10:16:32 -0400594 struct netlbl_af6list *list_entry;
Paul Moore8cc44572008-01-29 08:44:21 -0500595 struct netlbl_unlhsh_addr6 *entry;
Paul Moore61e10682008-10-10 10:16:32 -0400596 struct audit_buffer *audit_buf;
Paul Moore13541b32008-01-29 08:44:23 -0500597 struct net_device *dev;
Paul Moore61e10682008-10-10 10:16:32 -0400598 char *secctx;
Paul Moore13541b32008-01-29 08:44:23 -0500599 u32 secctx_len;
Paul Moore8cc44572008-01-29 08:44:21 -0500600
601 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400602 list_entry = netlbl_af6list_remove(addr, mask, &iface->addr6_list);
Paul Moore8cc44572008-01-29 08:44:21 -0500603 spin_unlock(&netlbl_unlhsh_lock);
Paul Moored25830e2008-12-03 00:37:04 -0800604 if (list_entry != NULL)
605 entry = netlbl_unlhsh_addr6_entry(list_entry);
606 else
Paul Mooreec8f2372008-12-11 21:31:50 -0800607 entry = NULL;
Paul Moore8cc44572008-01-29 08:44:21 -0500608
Paul Moore13541b32008-01-29 08:44:23 -0500609 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
610 audit_info);
611 if (audit_buf != NULL) {
612 dev = dev_get_by_index(net, iface->ifindex);
Paul Moore63c41682008-10-10 10:16:32 -0400613 netlbl_af6list_audit_addr(audit_buf, 1,
614 (dev != NULL ? dev->name : NULL),
615 addr, mask);
Paul Moore13541b32008-01-29 08:44:23 -0500616 if (dev != NULL)
617 dev_put(dev);
Paul Mooreec8f2372008-12-11 21:31:50 -0800618 if (entry != NULL &&
619 security_secid_to_secctx(entry->secid,
620 &secctx, &secctx_len) == 0) {
Paul Moore13541b32008-01-29 08:44:23 -0500621 audit_log_format(audit_buf, " sec_obj=%s", secctx);
622 security_release_secctx(secctx, secctx_len);
623 }
Paul Mooreec8f2372008-12-11 21:31:50 -0800624 audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
Paul Moore13541b32008-01-29 08:44:23 -0500625 audit_log_end(audit_buf);
626 }
627
Paul Mooreec8f2372008-12-11 21:31:50 -0800628 if (entry == NULL)
629 return -ENOENT;
630
631 call_rcu(&entry->rcu, netlbl_unlhsh_free_addr6);
632 return 0;
Paul Moore8cc44572008-01-29 08:44:21 -0500633}
634#endif /* IPv6 */
635
636/**
637 * netlbl_unlhsh_condremove_iface - Remove an interface entry
638 * @iface: the interface entry
639 *
640 * Description:
641 * Remove an interface entry from the unlabeled connection hash table if it is
642 * empty. An interface entry is considered to be empty if there are no
643 * address entries assigned to it.
644 *
645 */
646static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface)
647{
Paul Moore61e10682008-10-10 10:16:32 -0400648 struct netlbl_af4list *iter4;
649#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
650 struct netlbl_af6list *iter6;
651#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500652
653 spin_lock(&netlbl_unlhsh_lock);
Paul Moore61e10682008-10-10 10:16:32 -0400654 netlbl_af4list_foreach_rcu(iter4, &iface->addr4_list)
655 goto unlhsh_condremove_failure;
656#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
657 netlbl_af6list_foreach_rcu(iter6, &iface->addr6_list)
658 goto unlhsh_condremove_failure;
659#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -0500660 iface->valid = 0;
661 if (iface->ifindex > 0)
662 list_del_rcu(&iface->list);
663 else
664 rcu_assign_pointer(netlbl_unlhsh_def, NULL);
665 spin_unlock(&netlbl_unlhsh_lock);
666
667 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
668 return;
669
670unlhsh_condremove_failure:
671 spin_unlock(&netlbl_unlhsh_lock);
672 return;
673}
674
675/**
676 * netlbl_unlhsh_remove - Remove an entry from the unlabeled hash table
677 * @net: network namespace
678 * @dev_name: interface name
679 * @addr: IP address in network byte order
680 * @mask: address mask in network byte order
681 * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
Paul Moore13541b32008-01-29 08:44:23 -0500682 * @audit_info: NetLabel audit information
Paul Moore8cc44572008-01-29 08:44:21 -0500683 *
684 * Description:
685 * Removes and existing entry from the unlabeled connection hash table.
686 * Returns zero on success, negative values on failure.
687 *
688 */
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500689int netlbl_unlhsh_remove(struct net *net,
690 const char *dev_name,
691 const void *addr,
692 const void *mask,
693 u32 addr_len,
694 struct netlbl_audit *audit_info)
Paul Moore8cc44572008-01-29 08:44:21 -0500695{
696 int ret_val;
697 struct net_device *dev;
698 struct netlbl_unlhsh_iface *iface;
699
700 if (addr_len != sizeof(struct in_addr) &&
701 addr_len != sizeof(struct in6_addr))
702 return -EINVAL;
703
704 rcu_read_lock();
705 if (dev_name != NULL) {
Eric Dumazet122ec6f2009-11-05 20:53:47 -0800706 dev = dev_get_by_name_rcu(net, dev_name);
Paul Moore8cc44572008-01-29 08:44:21 -0500707 if (dev == NULL) {
708 ret_val = -ENODEV;
709 goto unlhsh_remove_return;
710 }
711 iface = netlbl_unlhsh_search_iface(dev->ifindex);
Paul Moore8cc44572008-01-29 08:44:21 -0500712 } else
713 iface = rcu_dereference(netlbl_unlhsh_def);
714 if (iface == NULL) {
715 ret_val = -ENOENT;
716 goto unlhsh_remove_return;
717 }
718 switch (addr_len) {
719 case sizeof(struct in_addr):
Paul Moore13541b32008-01-29 08:44:23 -0500720 ret_val = netlbl_unlhsh_remove_addr4(net,
721 iface, addr, mask,
722 audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500723 break;
724#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
725 case sizeof(struct in6_addr):
Paul Moore13541b32008-01-29 08:44:23 -0500726 ret_val = netlbl_unlhsh_remove_addr6(net,
727 iface, addr, mask,
728 audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500729 break;
730#endif /* IPv6 */
731 default:
732 ret_val = -EINVAL;
733 }
734 if (ret_val == 0) {
735 netlbl_unlhsh_condremove_iface(iface);
736 atomic_dec(&netlabel_mgmt_protocount);
737 }
738
739unlhsh_remove_return:
740 rcu_read_unlock();
741 return ret_val;
742}
743
744/*
745 * General Helper Functions
746 */
747
748/**
749 * netlbl_unlhsh_netdev_handler - Network device notification handler
750 * @this: notifier block
751 * @event: the event
752 * @ptr: the network device (cast to void)
753 *
754 * Description:
755 * Handle network device events, although at present all we care about is a
756 * network device going away. In the case of a device going away we clear any
757 * related entries from the unlabeled connection hash table.
758 *
759 */
760static int netlbl_unlhsh_netdev_handler(struct notifier_block *this,
761 unsigned long event,
762 void *ptr)
763{
764 struct net_device *dev = ptr;
765 struct netlbl_unlhsh_iface *iface = NULL;
766
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700767 if (!net_eq(dev_net(dev), &init_net))
Paul Moore8cc44572008-01-29 08:44:21 -0500768 return NOTIFY_DONE;
769
770 /* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */
771 if (event == NETDEV_DOWN) {
772 spin_lock(&netlbl_unlhsh_lock);
773 iface = netlbl_unlhsh_search_iface(dev->ifindex);
774 if (iface != NULL && iface->valid) {
775 iface->valid = 0;
776 list_del_rcu(&iface->list);
777 } else
778 iface = NULL;
779 spin_unlock(&netlbl_unlhsh_lock);
780 }
781
782 if (iface != NULL)
783 call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
784
785 return NOTIFY_DONE;
786}
787
788/**
Paul Moore32f50cd2006-09-28 14:51:47 -0700789 * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
790 * @value: desired value
Paul Moore95d4e6b2006-09-29 17:05:05 -0700791 * @audit_info: NetLabel audit information
Paul Moore32f50cd2006-09-28 14:51:47 -0700792 *
793 * Description:
794 * Set the value of the unlabeled accept flag to @value.
795 *
796 */
Paul Moore95d4e6b2006-09-29 17:05:05 -0700797static void netlbl_unlabel_acceptflg_set(u8 value,
798 struct netlbl_audit *audit_info)
Paul Moore32f50cd2006-09-28 14:51:47 -0700799{
Paul Moore95d4e6b2006-09-29 17:05:05 -0700800 struct audit_buffer *audit_buf;
801 u8 old_val;
802
Paul Moore4be27002007-10-26 04:29:08 -0700803 old_val = netlabel_unlabel_acceptflg;
Paul Moorecd287862006-11-17 17:38:44 -0500804 netlabel_unlabel_acceptflg = value;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700805 audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
806 audit_info);
Paul Moorede646882006-11-17 17:38:55 -0500807 if (audit_buf != NULL) {
808 audit_log_format(audit_buf,
809 " unlbl_accept=%u old=%u", value, old_val);
810 audit_log_end(audit_buf);
811 }
Paul Moore32f50cd2006-09-28 14:51:47 -0700812}
813
Paul Moore8cc44572008-01-29 08:44:21 -0500814/**
815 * netlbl_unlabel_addrinfo_get - Get the IPv4/6 address information
816 * @info: the Generic NETLINK info block
817 * @addr: the IP address
818 * @mask: the IP address mask
819 * @len: the address length
820 *
821 * Description:
822 * Examine the Generic NETLINK message and extract the IP address information.
823 * Returns zero on success, negative values on failure.
824 *
825 */
826static int netlbl_unlabel_addrinfo_get(struct genl_info *info,
827 void **addr,
828 void **mask,
829 u32 *len)
830{
831 u32 addr_len;
832
833 if (info->attrs[NLBL_UNLABEL_A_IPV4ADDR]) {
834 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
835 if (addr_len != sizeof(struct in_addr) &&
836 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK]))
837 return -EINVAL;
838 *len = addr_len;
839 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
840 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]);
841 return 0;
842 } else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) {
843 addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
844 if (addr_len != sizeof(struct in6_addr) &&
845 addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK]))
846 return -EINVAL;
847 *len = addr_len;
848 *addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
849 *mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]);
850 return 0;
851 }
852
853 return -EINVAL;
854}
855
Paul Moore32f50cd2006-09-28 14:51:47 -0700856/*
Paul Moore96cb8e32006-08-03 16:48:59 -0700857 * NetLabel Command Handlers
858 */
859
860/**
861 * netlbl_unlabel_accept - Handle an ACCEPT message
862 * @skb: the NETLINK buffer
863 * @info: the Generic NETLINK info block
864 *
865 * Description:
866 * Process a user generated ACCEPT message and set the accept flag accordingly.
867 * Returns zero on success, negative values on failure.
868 *
869 */
870static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
871{
Paul Moorefd385852006-09-25 15:56:37 -0700872 u8 value;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700873 struct netlbl_audit audit_info;
Paul Moore96cb8e32006-08-03 16:48:59 -0700874
Paul Moorefd385852006-09-25 15:56:37 -0700875 if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
876 value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
Paul Moore96cb8e32006-08-03 16:48:59 -0700877 if (value == 1 || value == 0) {
Paul Moore95d4e6b2006-09-29 17:05:05 -0700878 netlbl_netlink_auditinfo(skb, &audit_info);
879 netlbl_unlabel_acceptflg_set(value, &audit_info);
Paul Moore32f50cd2006-09-28 14:51:47 -0700880 return 0;
Paul Moore96cb8e32006-08-03 16:48:59 -0700881 }
882 }
883
Paul Moore32f50cd2006-09-28 14:51:47 -0700884 return -EINVAL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700885}
886
887/**
888 * netlbl_unlabel_list - Handle a LIST message
889 * @skb: the NETLINK buffer
890 * @info: the Generic NETLINK info block
891 *
892 * Description:
893 * Process a user generated LIST message and respond with the current status.
894 * Returns zero on success, negative values on failure.
895 *
896 */
897static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
898{
Paul Moorefd385852006-09-25 15:56:37 -0700899 int ret_val = -EINVAL;
Paul Moore96cb8e32006-08-03 16:48:59 -0700900 struct sk_buff *ans_skb;
Paul Moorefd385852006-09-25 15:56:37 -0700901 void *data;
Paul Moore96cb8e32006-08-03 16:48:59 -0700902
Thomas Graf339bf982006-11-10 14:10:15 -0800903 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Paul Moore96cb8e32006-08-03 16:48:59 -0700904 if (ans_skb == NULL)
905 goto list_failure;
Thomas Graf17c157c2006-11-14 19:46:02 -0800906 data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
907 0, NLBL_UNLABEL_C_LIST);
Paul Moorefd385852006-09-25 15:56:37 -0700908 if (data == NULL) {
909 ret_val = -ENOMEM;
Paul Moore96cb8e32006-08-03 16:48:59 -0700910 goto list_failure;
Paul Moorefd385852006-09-25 15:56:37 -0700911 }
Paul Moore96cb8e32006-08-03 16:48:59 -0700912
Paul Moorefd385852006-09-25 15:56:37 -0700913 ret_val = nla_put_u8(ans_skb,
914 NLBL_UNLABEL_A_ACPTFLG,
Paul Moorecd287862006-11-17 17:38:44 -0500915 netlabel_unlabel_acceptflg);
Paul Moore96cb8e32006-08-03 16:48:59 -0700916 if (ret_val != 0)
917 goto list_failure;
918
Paul Moorefd385852006-09-25 15:56:37 -0700919 genlmsg_end(ans_skb, data);
Denis V. Lunevfe785be2008-07-10 16:53:39 -0700920 return genlmsg_reply(ans_skb, info);
Paul Moore96cb8e32006-08-03 16:48:59 -0700921
922list_failure:
Patrick McHardyb08d5842007-02-27 09:57:37 -0800923 kfree_skb(ans_skb);
Paul Moore96cb8e32006-08-03 16:48:59 -0700924 return ret_val;
925}
926
Paul Moore8cc44572008-01-29 08:44:21 -0500927/**
928 * netlbl_unlabel_staticadd - Handle a STATICADD message
929 * @skb: the NETLINK buffer
930 * @info: the Generic NETLINK info block
931 *
932 * Description:
933 * Process a user generated STATICADD message and add a new unlabeled
934 * connection entry to the hash table. Returns zero on success, negative
935 * values on failure.
936 *
937 */
938static int netlbl_unlabel_staticadd(struct sk_buff *skb,
939 struct genl_info *info)
940{
941 int ret_val;
942 char *dev_name;
943 void *addr;
944 void *mask;
945 u32 addr_len;
946 u32 secid;
Paul Moore13541b32008-01-29 08:44:23 -0500947 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -0500948
949 /* Don't allow users to add both IPv4 and IPv6 addresses for a
950 * single entry. However, allow users to create two entries, one each
951 * for IPv4 and IPv4, with the same LSM security context which should
952 * achieve the same result. */
953 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
954 !info->attrs[NLBL_UNLABEL_A_IFACE] ||
955 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
956 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
957 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
958 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
959 return -EINVAL;
960
Paul Moore13541b32008-01-29 08:44:23 -0500961 netlbl_netlink_auditinfo(skb, &audit_info);
962
Paul Moore8cc44572008-01-29 08:44:21 -0500963 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
964 if (ret_val != 0)
965 return ret_val;
966 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
967 ret_val = security_secctx_to_secid(
968 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
969 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
970 &secid);
971 if (ret_val != 0)
972 return ret_val;
973
974 return netlbl_unlhsh_add(&init_net,
Paul Moore13541b32008-01-29 08:44:23 -0500975 dev_name, addr, mask, addr_len, secid,
976 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -0500977}
978
979/**
980 * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message
981 * @skb: the NETLINK buffer
982 * @info: the Generic NETLINK info block
983 *
984 * Description:
985 * Process a user generated STATICADDDEF message and add a new default
986 * unlabeled connection entry. Returns zero on success, negative values on
987 * failure.
988 *
989 */
990static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
991 struct genl_info *info)
992{
993 int ret_val;
994 void *addr;
995 void *mask;
996 u32 addr_len;
997 u32 secid;
Paul Moore13541b32008-01-29 08:44:23 -0500998 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -0500999
1000 /* Don't allow users to add both IPv4 and IPv6 addresses for a
1001 * single entry. However, allow users to create two entries, one each
1002 * for IPv4 and IPv6, with the same LSM security context which should
1003 * achieve the same result. */
1004 if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
1005 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1006 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1007 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1008 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1009 return -EINVAL;
1010
Paul Moore13541b32008-01-29 08:44:23 -05001011 netlbl_netlink_auditinfo(skb, &audit_info);
1012
Paul Moore8cc44572008-01-29 08:44:21 -05001013 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1014 if (ret_val != 0)
1015 return ret_val;
1016 ret_val = security_secctx_to_secid(
1017 nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1018 nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
1019 &secid);
1020 if (ret_val != 0)
1021 return ret_val;
1022
Paul Moore13541b32008-01-29 08:44:23 -05001023 return netlbl_unlhsh_add(&init_net,
1024 NULL, addr, mask, addr_len, secid,
1025 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -05001026}
1027
1028/**
1029 * netlbl_unlabel_staticremove - Handle a STATICREMOVE message
1030 * @skb: the NETLINK buffer
1031 * @info: the Generic NETLINK info block
1032 *
1033 * Description:
1034 * Process a user generated STATICREMOVE message and remove the specified
1035 * unlabeled connection entry. Returns zero on success, negative values on
1036 * failure.
1037 *
1038 */
1039static int netlbl_unlabel_staticremove(struct sk_buff *skb,
1040 struct genl_info *info)
1041{
1042 int ret_val;
1043 char *dev_name;
1044 void *addr;
1045 void *mask;
1046 u32 addr_len;
Paul Moore13541b32008-01-29 08:44:23 -05001047 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -05001048
1049 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1050 * IPv4 and IPv6 in the same entry. */
1051 if (!info->attrs[NLBL_UNLABEL_A_IFACE] ||
1052 !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1053 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1054 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1055 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1056 return -EINVAL;
1057
Paul Moore13541b32008-01-29 08:44:23 -05001058 netlbl_netlink_auditinfo(skb, &audit_info);
1059
Paul Moore8cc44572008-01-29 08:44:21 -05001060 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1061 if (ret_val != 0)
1062 return ret_val;
1063 dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
1064
Paul Moore13541b32008-01-29 08:44:23 -05001065 return netlbl_unlhsh_remove(&init_net,
1066 dev_name, addr, mask, addr_len,
1067 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -05001068}
1069
1070/**
1071 * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message
1072 * @skb: the NETLINK buffer
1073 * @info: the Generic NETLINK info block
1074 *
1075 * Description:
1076 * Process a user generated STATICREMOVEDEF message and remove the default
1077 * unlabeled connection entry. Returns zero on success, negative values on
1078 * failure.
1079 *
1080 */
1081static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
1082 struct genl_info *info)
1083{
1084 int ret_val;
1085 void *addr;
1086 void *mask;
1087 u32 addr_len;
Paul Moore13541b32008-01-29 08:44:23 -05001088 struct netlbl_audit audit_info;
Paul Moore8cc44572008-01-29 08:44:21 -05001089
1090 /* See the note in netlbl_unlabel_staticadd() about not allowing both
1091 * IPv4 and IPv6 in the same entry. */
1092 if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
1093 !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
1094 (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
1095 !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
1096 return -EINVAL;
1097
Paul Moore13541b32008-01-29 08:44:23 -05001098 netlbl_netlink_auditinfo(skb, &audit_info);
1099
Paul Moore8cc44572008-01-29 08:44:21 -05001100 ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
1101 if (ret_val != 0)
1102 return ret_val;
1103
Paul Moore13541b32008-01-29 08:44:23 -05001104 return netlbl_unlhsh_remove(&init_net,
1105 NULL, addr, mask, addr_len,
1106 &audit_info);
Paul Moore8cc44572008-01-29 08:44:21 -05001107}
1108
1109
1110/**
1111 * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF]
1112 * @cmd: command/message
1113 * @iface: the interface entry
1114 * @addr4: the IPv4 address entry
1115 * @addr6: the IPv6 address entry
1116 * @arg: the netlbl_unlhsh_walk_arg structure
1117 *
1118 * Description:
1119 * This function is designed to be used to generate a response for a
1120 * STATICLIST or STATICLISTDEF message. When called either @addr4 or @addr6
1121 * can be specified, not both, the other unspecified entry should be set to
1122 * NULL by the caller. Returns the size of the message on success, negative
1123 * values on failure.
1124 *
1125 */
1126static int netlbl_unlabel_staticlist_gen(u32 cmd,
1127 const struct netlbl_unlhsh_iface *iface,
1128 const struct netlbl_unlhsh_addr4 *addr4,
1129 const struct netlbl_unlhsh_addr6 *addr6,
1130 void *arg)
1131{
1132 int ret_val = -ENOMEM;
1133 struct netlbl_unlhsh_walk_arg *cb_arg = arg;
1134 struct net_device *dev;
1135 void *data;
1136 u32 secid;
1137 char *secctx;
1138 u32 secctx_len;
1139
1140 data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
1141 cb_arg->seq, &netlbl_unlabel_gnl_family,
1142 NLM_F_MULTI, cmd);
1143 if (data == NULL)
1144 goto list_cb_failure;
1145
1146 if (iface->ifindex > 0) {
1147 dev = dev_get_by_index(&init_net, iface->ifindex);
Jesper Juhl794eb6b2008-04-17 23:22:54 -07001148 if (!dev) {
1149 ret_val = -ENODEV;
1150 goto list_cb_failure;
1151 }
Paul Moore8cc44572008-01-29 08:44:21 -05001152 ret_val = nla_put_string(cb_arg->skb,
1153 NLBL_UNLABEL_A_IFACE, dev->name);
1154 dev_put(dev);
1155 if (ret_val != 0)
1156 goto list_cb_failure;
1157 }
1158
1159 if (addr4) {
1160 struct in_addr addr_struct;
1161
Paul Moore61e10682008-10-10 10:16:32 -04001162 addr_struct.s_addr = addr4->list.addr;
Paul Moore8cc44572008-01-29 08:44:21 -05001163 ret_val = nla_put(cb_arg->skb,
1164 NLBL_UNLABEL_A_IPV4ADDR,
1165 sizeof(struct in_addr),
1166 &addr_struct);
1167 if (ret_val != 0)
1168 goto list_cb_failure;
1169
Paul Moore61e10682008-10-10 10:16:32 -04001170 addr_struct.s_addr = addr4->list.mask;
Paul Moore8cc44572008-01-29 08:44:21 -05001171 ret_val = nla_put(cb_arg->skb,
1172 NLBL_UNLABEL_A_IPV4MASK,
1173 sizeof(struct in_addr),
1174 &addr_struct);
1175 if (ret_val != 0)
1176 goto list_cb_failure;
1177
1178 secid = addr4->secid;
1179 } else {
1180 ret_val = nla_put(cb_arg->skb,
1181 NLBL_UNLABEL_A_IPV6ADDR,
1182 sizeof(struct in6_addr),
Paul Moore61e10682008-10-10 10:16:32 -04001183 &addr6->list.addr);
Paul Moore8cc44572008-01-29 08:44:21 -05001184 if (ret_val != 0)
1185 goto list_cb_failure;
1186
1187 ret_val = nla_put(cb_arg->skb,
1188 NLBL_UNLABEL_A_IPV6MASK,
1189 sizeof(struct in6_addr),
Paul Moore61e10682008-10-10 10:16:32 -04001190 &addr6->list.mask);
Paul Moore8cc44572008-01-29 08:44:21 -05001191 if (ret_val != 0)
1192 goto list_cb_failure;
1193
1194 secid = addr6->secid;
1195 }
1196
1197 ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len);
1198 if (ret_val != 0)
1199 goto list_cb_failure;
1200 ret_val = nla_put(cb_arg->skb,
1201 NLBL_UNLABEL_A_SECCTX,
1202 secctx_len,
1203 secctx);
1204 security_release_secctx(secctx, secctx_len);
1205 if (ret_val != 0)
1206 goto list_cb_failure;
1207
1208 cb_arg->seq++;
1209 return genlmsg_end(cb_arg->skb, data);
1210
1211list_cb_failure:
1212 genlmsg_cancel(cb_arg->skb, data);
1213 return ret_val;
1214}
1215
1216/**
1217 * netlbl_unlabel_staticlist - Handle a STATICLIST message
1218 * @skb: the NETLINK buffer
1219 * @cb: the NETLINK callback
1220 *
1221 * Description:
1222 * Process a user generated STATICLIST message and dump the unlabeled
1223 * connection hash table in a form suitable for use in a kernel generated
1224 * STATICLIST message. Returns the length of @skb.
1225 *
1226 */
1227static int netlbl_unlabel_staticlist(struct sk_buff *skb,
1228 struct netlink_callback *cb)
1229{
1230 struct netlbl_unlhsh_walk_arg cb_arg;
1231 u32 skip_bkt = cb->args[0];
1232 u32 skip_chain = cb->args[1];
1233 u32 skip_addr4 = cb->args[2];
1234 u32 skip_addr6 = cb->args[3];
1235 u32 iter_bkt;
1236 u32 iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
1237 struct netlbl_unlhsh_iface *iface;
Paul Moore56196702008-10-10 10:16:29 -04001238 struct list_head *iter_list;
Paul Moore61e10682008-10-10 10:16:32 -04001239 struct netlbl_af4list *addr4;
1240#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1241 struct netlbl_af6list *addr6;
1242#endif
Paul Moore8cc44572008-01-29 08:44:21 -05001243
1244 cb_arg.nl_cb = cb;
1245 cb_arg.skb = skb;
1246 cb_arg.seq = cb->nlh->nlmsg_seq;
1247
1248 rcu_read_lock();
1249 for (iter_bkt = skip_bkt;
1250 iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
1251 iter_bkt++, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0) {
Paul Moore56196702008-10-10 10:16:29 -04001252 iter_list = &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt];
1253 list_for_each_entry_rcu(iface, iter_list, list) {
Paul Moore8cc44572008-01-29 08:44:21 -05001254 if (!iface->valid ||
1255 iter_chain++ < skip_chain)
1256 continue;
Paul Moore61e10682008-10-10 10:16:32 -04001257 netlbl_af4list_foreach_rcu(addr4,
1258 &iface->addr4_list) {
1259 if (iter_addr4++ < skip_addr4)
Paul Moore8cc44572008-01-29 08:44:21 -05001260 continue;
1261 if (netlbl_unlabel_staticlist_gen(
Paul Moore61e10682008-10-10 10:16:32 -04001262 NLBL_UNLABEL_C_STATICLIST,
1263 iface,
1264 netlbl_unlhsh_addr4_entry(addr4),
1265 NULL,
1266 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001267 iter_addr4--;
1268 iter_chain--;
1269 goto unlabel_staticlist_return;
1270 }
1271 }
Paul Moore61e10682008-10-10 10:16:32 -04001272#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1273 netlbl_af6list_foreach_rcu(addr6,
1274 &iface->addr6_list) {
1275 if (iter_addr6++ < skip_addr6)
Paul Moore8cc44572008-01-29 08:44:21 -05001276 continue;
1277 if (netlbl_unlabel_staticlist_gen(
Paul Moore61e10682008-10-10 10:16:32 -04001278 NLBL_UNLABEL_C_STATICLIST,
1279 iface,
1280 NULL,
1281 netlbl_unlhsh_addr6_entry(addr6),
1282 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001283 iter_addr6--;
1284 iter_chain--;
1285 goto unlabel_staticlist_return;
1286 }
1287 }
Paul Moore61e10682008-10-10 10:16:32 -04001288#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -05001289 }
1290 }
1291
1292unlabel_staticlist_return:
1293 rcu_read_unlock();
1294 cb->args[0] = skip_bkt;
1295 cb->args[1] = skip_chain;
1296 cb->args[2] = skip_addr4;
1297 cb->args[3] = skip_addr6;
1298 return skb->len;
1299}
1300
1301/**
1302 * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message
1303 * @skb: the NETLINK buffer
1304 * @cb: the NETLINK callback
1305 *
1306 * Description:
1307 * Process a user generated STATICLISTDEF message and dump the default
1308 * unlabeled connection entry in a form suitable for use in a kernel generated
1309 * STATICLISTDEF message. Returns the length of @skb.
1310 *
1311 */
1312static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
1313 struct netlink_callback *cb)
1314{
1315 struct netlbl_unlhsh_walk_arg cb_arg;
1316 struct netlbl_unlhsh_iface *iface;
1317 u32 skip_addr4 = cb->args[0];
1318 u32 skip_addr6 = cb->args[1];
Paul Moore61e10682008-10-10 10:16:32 -04001319 u32 iter_addr4 = 0;
1320 struct netlbl_af4list *addr4;
1321#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1322 u32 iter_addr6 = 0;
1323 struct netlbl_af6list *addr6;
1324#endif
Paul Moore8cc44572008-01-29 08:44:21 -05001325
1326 cb_arg.nl_cb = cb;
1327 cb_arg.skb = skb;
1328 cb_arg.seq = cb->nlh->nlmsg_seq;
1329
1330 rcu_read_lock();
1331 iface = rcu_dereference(netlbl_unlhsh_def);
1332 if (iface == NULL || !iface->valid)
1333 goto unlabel_staticlistdef_return;
1334
Paul Moore61e10682008-10-10 10:16:32 -04001335 netlbl_af4list_foreach_rcu(addr4, &iface->addr4_list) {
1336 if (iter_addr4++ < skip_addr4)
Paul Moore8cc44572008-01-29 08:44:21 -05001337 continue;
1338 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
Paul Moore61e10682008-10-10 10:16:32 -04001339 iface,
1340 netlbl_unlhsh_addr4_entry(addr4),
1341 NULL,
1342 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001343 iter_addr4--;
1344 goto unlabel_staticlistdef_return;
1345 }
1346 }
Paul Moore61e10682008-10-10 10:16:32 -04001347#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1348 netlbl_af6list_foreach_rcu(addr6, &iface->addr6_list) {
1349 if (iter_addr6++ < skip_addr6)
Paul Moore8cc44572008-01-29 08:44:21 -05001350 continue;
1351 if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
Paul Moore61e10682008-10-10 10:16:32 -04001352 iface,
1353 NULL,
1354 netlbl_unlhsh_addr6_entry(addr6),
1355 &cb_arg) < 0) {
Paul Moore8cc44572008-01-29 08:44:21 -05001356 iter_addr6--;
1357 goto unlabel_staticlistdef_return;
1358 }
1359 }
Paul Moore61e10682008-10-10 10:16:32 -04001360#endif /* IPv6 */
Paul Moore8cc44572008-01-29 08:44:21 -05001361
1362unlabel_staticlistdef_return:
1363 rcu_read_unlock();
1364 cb->args[0] = skip_addr4;
1365 cb->args[1] = skip_addr6;
1366 return skb->len;
1367}
Paul Moore96cb8e32006-08-03 16:48:59 -07001368
1369/*
1370 * NetLabel Generic NETLINK Command Definitions
1371 */
1372
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001373static struct genl_ops netlbl_unlabel_genl_ops[] = {
1374 {
Paul Moore8cc44572008-01-29 08:44:21 -05001375 .cmd = NLBL_UNLABEL_C_STATICADD,
1376 .flags = GENL_ADMIN_PERM,
1377 .policy = netlbl_unlabel_genl_policy,
1378 .doit = netlbl_unlabel_staticadd,
1379 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001380 },
1381 {
Paul Moore8cc44572008-01-29 08:44:21 -05001382 .cmd = NLBL_UNLABEL_C_STATICREMOVE,
1383 .flags = GENL_ADMIN_PERM,
1384 .policy = netlbl_unlabel_genl_policy,
1385 .doit = netlbl_unlabel_staticremove,
1386 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001387 },
1388 {
Paul Moore8cc44572008-01-29 08:44:21 -05001389 .cmd = NLBL_UNLABEL_C_STATICLIST,
1390 .flags = 0,
1391 .policy = netlbl_unlabel_genl_policy,
1392 .doit = NULL,
1393 .dumpit = netlbl_unlabel_staticlist,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001394 },
1395 {
Paul Moore8cc44572008-01-29 08:44:21 -05001396 .cmd = NLBL_UNLABEL_C_STATICADDDEF,
1397 .flags = GENL_ADMIN_PERM,
1398 .policy = netlbl_unlabel_genl_policy,
1399 .doit = netlbl_unlabel_staticadddef,
1400 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001401 },
1402 {
Paul Moore8cc44572008-01-29 08:44:21 -05001403 .cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
1404 .flags = GENL_ADMIN_PERM,
1405 .policy = netlbl_unlabel_genl_policy,
1406 .doit = netlbl_unlabel_staticremovedef,
1407 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001408 },
1409 {
Paul Moore8cc44572008-01-29 08:44:21 -05001410 .cmd = NLBL_UNLABEL_C_STATICLISTDEF,
1411 .flags = 0,
1412 .policy = netlbl_unlabel_genl_policy,
1413 .doit = NULL,
1414 .dumpit = netlbl_unlabel_staticlistdef,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001415 },
1416 {
Paul Moore96cb8e32006-08-03 16:48:59 -07001417 .cmd = NLBL_UNLABEL_C_ACCEPT,
Paul Moorefd385852006-09-25 15:56:37 -07001418 .flags = GENL_ADMIN_PERM,
1419 .policy = netlbl_unlabel_genl_policy,
Paul Moore96cb8e32006-08-03 16:48:59 -07001420 .doit = netlbl_unlabel_accept,
1421 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001422 },
1423 {
Paul Moore96cb8e32006-08-03 16:48:59 -07001424 .cmd = NLBL_UNLABEL_C_LIST,
1425 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -07001426 .policy = netlbl_unlabel_genl_policy,
Paul Moore96cb8e32006-08-03 16:48:59 -07001427 .doit = netlbl_unlabel_list,
1428 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -08001429 },
Paul Moore96cb8e32006-08-03 16:48:59 -07001430};
1431
Paul Moore96cb8e32006-08-03 16:48:59 -07001432/*
1433 * NetLabel Generic NETLINK Protocol Functions
1434 */
1435
1436/**
1437 * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
1438 *
1439 * Description:
1440 * Register the unlabeled packet NetLabel component with the Generic NETLINK
1441 * mechanism. Returns zero on success, negative values on failure.
1442 *
1443 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -08001444int __init netlbl_unlabel_genl_init(void)
Paul Moore96cb8e32006-08-03 16:48:59 -07001445{
Michał Mirosław7ae740d2009-05-21 10:34:05 +00001446 return genl_register_family_with_ops(&netlbl_unlabel_gnl_family,
1447 netlbl_unlabel_genl_ops, ARRAY_SIZE(netlbl_unlabel_genl_ops));
Paul Moore96cb8e32006-08-03 16:48:59 -07001448}
1449
1450/*
1451 * NetLabel KAPI Hooks
1452 */
1453
Paul Moore8cc44572008-01-29 08:44:21 -05001454static struct notifier_block netlbl_unlhsh_netdev_notifier = {
1455 .notifier_call = netlbl_unlhsh_netdev_handler,
1456};
1457
1458/**
1459 * netlbl_unlabel_init - Initialize the unlabeled connection hash table
1460 * @size: the number of bits to use for the hash buckets
1461 *
1462 * Description:
1463 * Initializes the unlabeled connection hash table and registers a network
1464 * device notification handler. This function should only be called by the
1465 * NetLabel subsystem itself during initialization. Returns zero on success,
1466 * non-zero values on error.
1467 *
1468 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -08001469int __init netlbl_unlabel_init(u32 size)
Paul Moore8cc44572008-01-29 08:44:21 -05001470{
1471 u32 iter;
1472 struct netlbl_unlhsh_tbl *hsh_tbl;
1473
1474 if (size == 0)
1475 return -EINVAL;
1476
1477 hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
1478 if (hsh_tbl == NULL)
1479 return -ENOMEM;
1480 hsh_tbl->size = 1 << size;
1481 hsh_tbl->tbl = kcalloc(hsh_tbl->size,
1482 sizeof(struct list_head),
1483 GFP_KERNEL);
1484 if (hsh_tbl->tbl == NULL) {
1485 kfree(hsh_tbl);
1486 return -ENOMEM;
1487 }
1488 for (iter = 0; iter < hsh_tbl->size; iter++)
1489 INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
1490
1491 rcu_read_lock();
1492 spin_lock(&netlbl_unlhsh_lock);
1493 rcu_assign_pointer(netlbl_unlhsh, hsh_tbl);
1494 spin_unlock(&netlbl_unlhsh_lock);
1495 rcu_read_unlock();
1496
1497 register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier);
1498
1499 return 0;
1500}
1501
Paul Moore96cb8e32006-08-03 16:48:59 -07001502/**
1503 * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
Paul Moore8cc44572008-01-29 08:44:21 -05001504 * @skb: the packet
1505 * @family: protocol family
Paul Moore96cb8e32006-08-03 16:48:59 -07001506 * @secattr: the security attributes
1507 *
1508 * Description:
1509 * Determine the security attributes, if any, for an unlabled packet and return
1510 * them in @secattr. Returns zero on success and negative values on failure.
1511 *
1512 */
Paul Moore8cc44572008-01-29 08:44:21 -05001513int netlbl_unlabel_getattr(const struct sk_buff *skb,
1514 u16 family,
1515 struct netlbl_lsm_secattr *secattr)
Paul Moore96cb8e32006-08-03 16:48:59 -07001516{
Paul Moore8cc44572008-01-29 08:44:21 -05001517 struct netlbl_unlhsh_iface *iface;
1518
1519 rcu_read_lock();
Paul Mooreb914f3a2010-04-01 10:43:57 +00001520 iface = netlbl_unlhsh_search_iface(skb->skb_iif);
Paul Moore8cc44572008-01-29 08:44:21 -05001521 if (iface == NULL)
Paul Mooreb914f3a2010-04-01 10:43:57 +00001522 iface = rcu_dereference(netlbl_unlhsh_def);
1523 if (iface == NULL || !iface->valid)
Paul Moore8cc44572008-01-29 08:44:21 -05001524 goto unlabel_getattr_nolabel;
1525 switch (family) {
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001526 case PF_INET: {
1527 struct iphdr *hdr4;
Paul Moore61e10682008-10-10 10:16:32 -04001528 struct netlbl_af4list *addr4;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001529
Paul Moore8cc44572008-01-29 08:44:21 -05001530 hdr4 = ip_hdr(skb);
Paul Moore61e10682008-10-10 10:16:32 -04001531 addr4 = netlbl_af4list_search(hdr4->saddr,
1532 &iface->addr4_list);
Paul Moore8cc44572008-01-29 08:44:21 -05001533 if (addr4 == NULL)
1534 goto unlabel_getattr_nolabel;
Paul Moore61e10682008-10-10 10:16:32 -04001535 secattr->attr.secid = netlbl_unlhsh_addr4_entry(addr4)->secid;
Paul Moore8cc44572008-01-29 08:44:21 -05001536 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001537 }
Paul Moore8cc44572008-01-29 08:44:21 -05001538#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001539 case PF_INET6: {
1540 struct ipv6hdr *hdr6;
Paul Moore61e10682008-10-10 10:16:32 -04001541 struct netlbl_af6list *addr6;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001542
Paul Moore8cc44572008-01-29 08:44:21 -05001543 hdr6 = ipv6_hdr(skb);
Paul Moore61e10682008-10-10 10:16:32 -04001544 addr6 = netlbl_af6list_search(&hdr6->saddr,
1545 &iface->addr6_list);
Paul Moore8cc44572008-01-29 08:44:21 -05001546 if (addr6 == NULL)
1547 goto unlabel_getattr_nolabel;
Paul Moore61e10682008-10-10 10:16:32 -04001548 secattr->attr.secid = netlbl_unlhsh_addr6_entry(addr6)->secid;
Paul Moore8cc44572008-01-29 08:44:21 -05001549 break;
Pavel Emelyanov56628b12008-02-12 22:37:19 -08001550 }
Paul Moore8cc44572008-01-29 08:44:21 -05001551#endif /* IPv6 */
1552 default:
1553 goto unlabel_getattr_nolabel;
1554 }
1555 rcu_read_unlock();
1556
1557 secattr->flags |= NETLBL_SECATTR_SECID;
1558 secattr->type = NETLBL_NLTYPE_UNLABELED;
1559 return 0;
1560
1561unlabel_getattr_nolabel:
1562 rcu_read_unlock();
Paul Moorec783f1c2008-01-29 08:37:52 -05001563 if (netlabel_unlabel_acceptflg == 0)
1564 return -ENOMSG;
Paul Moore16efd452008-01-29 08:37:59 -05001565 secattr->type = NETLBL_NLTYPE_UNLABELED;
Paul Moorec783f1c2008-01-29 08:37:52 -05001566 return 0;
Paul Moore96cb8e32006-08-03 16:48:59 -07001567}
1568
1569/**
1570 * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
1571 *
1572 * Description:
1573 * Set the default NetLabel configuration to allow incoming unlabeled packets
1574 * and to send unlabeled network traffic by default.
1575 *
1576 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -08001577int __init netlbl_unlabel_defconf(void)
Paul Moore96cb8e32006-08-03 16:48:59 -07001578{
1579 int ret_val;
1580 struct netlbl_dom_map *entry;
Paul Moore95d4e6b2006-09-29 17:05:05 -07001581 struct netlbl_audit audit_info;
Paul Moore32f50cd2006-09-28 14:51:47 -07001582
Paul Moore95d4e6b2006-09-29 17:05:05 -07001583 /* Only the kernel is allowed to call this function and the only time
1584 * it is called is at bootup before the audit subsystem is reporting
1585 * messages so don't worry to much about these values. */
1586 security_task_getsecid(current, &audit_info.secid);
1587 audit_info.loginuid = 0;
Eric Paris25323862008-04-18 10:09:25 -04001588 audit_info.sessionid = 0;
Paul Moore96cb8e32006-08-03 16:48:59 -07001589
1590 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1591 if (entry == NULL)
1592 return -ENOMEM;
1593 entry->type = NETLBL_NLTYPE_UNLABELED;
Paul Moore95d4e6b2006-09-29 17:05:05 -07001594 ret_val = netlbl_domhsh_add_default(entry, &audit_info);
Paul Moore96cb8e32006-08-03 16:48:59 -07001595 if (ret_val != 0)
1596 return ret_val;
1597
Paul Moore95d4e6b2006-09-29 17:05:05 -07001598 netlbl_unlabel_acceptflg_set(1, &audit_info);
Paul Moore96cb8e32006-08-03 16:48:59 -07001599
1600 return 0;
1601}