Blame view

net/netlabel/netlabel_unlabeled.c 41.7 KB
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
1
2
3
4
5
6
7
  /*
   * NetLabel Unlabeled Support
   *
   * This file defines functions for dealing with unlabeled packets for the
   * NetLabel system.  The NetLabel system manages static and dynamic label
   * mappings for network protocols such as CIPSO and RIPSO.
   *
82c21bfab   Paul Moore   doc: Update the e...
8
   * Author: Paul Moore <paul@paul-moore.com>
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
9
10
11
12
   *
   */
  
  /*
61e106821   Paul Moore   netlabel: Add a g...
13
   * (c) Copyright Hewlett-Packard Development Company, L.P., 2006 - 2008
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
   *
   * This program is free software;  you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY;  without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
   * the GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program;  if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   *
   */
  
  #include <linux/types.h>
8cc44579d   Paul Moore   NetLabel: Introdu...
32
  #include <linux/rcupdate.h>
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
33
34
35
36
37
  #include <linux/list.h>
  #include <linux/spinlock.h>
  #include <linux/socket.h>
  #include <linux/string.h>
  #include <linux/skbuff.h>
de64688ff   Paul Moore   NetLabel: honor t...
38
  #include <linux/audit.h>
8cc44579d   Paul Moore   NetLabel: Introdu...
39
40
41
42
43
44
45
  #include <linux/in.h>
  #include <linux/in6.h>
  #include <linux/ip.h>
  #include <linux/ipv6.h>
  #include <linux/notifier.h>
  #include <linux/netdevice.h>
  #include <linux/security.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
46
  #include <linux/slab.h>
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
47
48
49
  #include <net/sock.h>
  #include <net/netlink.h>
  #include <net/genetlink.h>
8cc44579d   Paul Moore   NetLabel: Introdu...
50
51
52
  #include <net/ip.h>
  #include <net/ipv6.h>
  #include <net/net_namespace.h>
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
53
54
  #include <net/netlabel.h>
  #include <asm/bug.h>
60063497a   Arun Sharma   atomic: use <linu...
55
  #include <linux/atomic.h>
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
56
57
  
  #include "netlabel_user.h"
61e106821   Paul Moore   netlabel: Add a g...
58
  #include "netlabel_addrlist.h"
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
59
60
  #include "netlabel_domainhash.h"
  #include "netlabel_unlabeled.h"
8cc44579d   Paul Moore   NetLabel: Introdu...
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  #include "netlabel_mgmt.h"
  
  /* NOTE: at present we always use init's network namespace since we don't
   *       presently support different namespaces even though the majority of
   *       the functions in this file are "namespace safe" */
  
  /* The unlabeled connection hash table which we use to map network interfaces
   * and addresses of unlabeled packets to a user specified secid value for the
   * LSM.  The hash table is used to lookup the network interface entry
   * (struct netlbl_unlhsh_iface) and then the interface entry is used to
   * lookup an IP address match from an ordered list.  If a network interface
   * match can not be found in the hash table then the default entry
   * (netlbl_unlhsh_def) is used.  The IP address entry list
   * (struct netlbl_unlhsh_addr) is ordered such that the entries with a
   * larger netmask come first.
   */
  struct netlbl_unlhsh_tbl {
  	struct list_head *tbl;
  	u32 size;
  };
61e106821   Paul Moore   netlabel: Add a g...
81
82
  #define netlbl_unlhsh_addr4_entry(iter) \
  	container_of(iter, struct netlbl_unlhsh_addr4, list)
8cc44579d   Paul Moore   NetLabel: Introdu...
83
  struct netlbl_unlhsh_addr4 {
8cc44579d   Paul Moore   NetLabel: Introdu...
84
  	u32 secid;
61e106821   Paul Moore   netlabel: Add a g...
85
  	struct netlbl_af4list list;
8cc44579d   Paul Moore   NetLabel: Introdu...
86
87
  	struct rcu_head rcu;
  };
61e106821   Paul Moore   netlabel: Add a g...
88
89
  #define netlbl_unlhsh_addr6_entry(iter) \
  	container_of(iter, struct netlbl_unlhsh_addr6, list)
8cc44579d   Paul Moore   NetLabel: Introdu...
90
  struct netlbl_unlhsh_addr6 {
8cc44579d   Paul Moore   NetLabel: Introdu...
91
  	u32 secid;
61e106821   Paul Moore   netlabel: Add a g...
92
  	struct netlbl_af6list list;
8cc44579d   Paul Moore   NetLabel: Introdu...
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  	struct rcu_head rcu;
  };
  struct netlbl_unlhsh_iface {
  	int ifindex;
  	struct list_head addr4_list;
  	struct list_head addr6_list;
  
  	u32 valid;
  	struct list_head list;
  	struct rcu_head rcu;
  };
  
  /* Argument struct for netlbl_unlhsh_walk() */
  struct netlbl_unlhsh_walk_arg {
  	struct netlink_callback *nl_cb;
  	struct sk_buff *skb;
  	u32 seq;
  };
  
  /* Unlabeled connection hash table */
  /* updates should be so rare that having one spinlock for the entire
   * hash table should be okay */
  static DEFINE_SPINLOCK(netlbl_unlhsh_lock);
b914f3a2a   Paul Moore   netlabel: Fix sev...
116
  #define netlbl_unlhsh_rcu_deref(p) \
d8bf4ca9c   Michal Hocko   rcu: treewide: Do...
117
  	rcu_dereference_check(p, lockdep_is_held(&netlbl_unlhsh_lock))
8cc44579d   Paul Moore   NetLabel: Introdu...
118
119
  static struct netlbl_unlhsh_tbl *netlbl_unlhsh = NULL;
  static struct netlbl_unlhsh_iface *netlbl_unlhsh_def = NULL;
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
120
121
  
  /* Accept unlabeled packets flag */
cd28786d6   Paul Moore   NetLabel: convert...
122
  static u8 netlabel_unlabel_acceptflg = 0;
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
123

8cc44579d   Paul Moore   NetLabel: Introdu...
124
  /* NetLabel Generic NETLINK unlabeled family */
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
125
126
127
128
129
  static struct genl_family netlbl_unlabel_gnl_family = {
  	.id = GENL_ID_GENERATE,
  	.hdrsize = 0,
  	.name = NETLBL_NLTYPE_UNLABELED_NAME,
  	.version = NETLBL_PROTO_VERSION,
fd3858554   Paul Moore   [NetLabel]: rewor...
130
  	.maxattr = NLBL_UNLABEL_A_MAX,
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
131
  };
fd3858554   Paul Moore   [NetLabel]: rewor...
132
  /* NetLabel Netlink attribute policy */
ef7c79ed6   Patrick McHardy   [NETLINK]: Mark n...
133
  static const struct nla_policy netlbl_unlabel_genl_policy[NLBL_UNLABEL_A_MAX + 1] = {
fd3858554   Paul Moore   [NetLabel]: rewor...
134
  	[NLBL_UNLABEL_A_ACPTFLG] = { .type = NLA_U8 },
8cc44579d   Paul Moore   NetLabel: Introdu...
135
136
137
138
139
140
141
142
143
144
145
  	[NLBL_UNLABEL_A_IPV6ADDR] = { .type = NLA_BINARY,
  				      .len = sizeof(struct in6_addr) },
  	[NLBL_UNLABEL_A_IPV6MASK] = { .type = NLA_BINARY,
  				      .len = sizeof(struct in6_addr) },
  	[NLBL_UNLABEL_A_IPV4ADDR] = { .type = NLA_BINARY,
  				      .len = sizeof(struct in_addr) },
  	[NLBL_UNLABEL_A_IPV4MASK] = { .type = NLA_BINARY,
  				      .len = sizeof(struct in_addr) },
  	[NLBL_UNLABEL_A_IFACE] = { .type = NLA_NUL_STRING,
  				   .len = IFNAMSIZ - 1 },
  	[NLBL_UNLABEL_A_SECCTX] = { .type = NLA_BINARY }
fd3858554   Paul Moore   [NetLabel]: rewor...
146
  };
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
147
148
  
  /*
8cc44579d   Paul Moore   NetLabel: Introdu...
149
150
   * Unlabeled Connection Hash Table Functions
   */
8cc44579d   Paul Moore   NetLabel: Introdu...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  /**
   * netlbl_unlhsh_free_iface - Frees an interface entry from the hash table
   * @entry: the entry's RCU field
   *
   * Description:
   * This function is designed to be used as a callback to the call_rcu()
   * function so that memory allocated to a hash table interface entry can be
   * released safely.  It is important to note that this function does not free
   * the IPv4 and IPv6 address lists contained as part of an interface entry.  It
   * is up to the rest of the code to make sure an interface entry is only freed
   * once it's address lists are empty.
   *
   */
  static void netlbl_unlhsh_free_iface(struct rcu_head *entry)
  {
  	struct netlbl_unlhsh_iface *iface;
61e106821   Paul Moore   netlabel: Add a g...
167
168
  	struct netlbl_af4list *iter4;
  	struct netlbl_af4list *tmp4;
dfd56b8b3   Eric Dumazet   net: use IS_ENABL...
169
  #if IS_ENABLED(CONFIG_IPV6)
61e106821   Paul Moore   netlabel: Add a g...
170
171
172
  	struct netlbl_af6list *iter6;
  	struct netlbl_af6list *tmp6;
  #endif /* IPv6 */
8cc44579d   Paul Moore   NetLabel: Introdu...
173
174
175
176
177
  
  	iface = container_of(entry, struct netlbl_unlhsh_iface, rcu);
  
  	/* no need for locks here since we are the only one with access to this
  	 * structure */
61e106821   Paul Moore   netlabel: Add a g...
178
179
180
181
  	netlbl_af4list_foreach_safe(iter4, tmp4, &iface->addr4_list) {
  		netlbl_af4list_remove_entry(iter4);
  		kfree(netlbl_unlhsh_addr4_entry(iter4));
  	}
dfd56b8b3   Eric Dumazet   net: use IS_ENABL...
182
  #if IS_ENABLED(CONFIG_IPV6)
61e106821   Paul Moore   netlabel: Add a g...
183
184
185
186
187
  	netlbl_af6list_foreach_safe(iter6, tmp6, &iface->addr6_list) {
  		netlbl_af6list_remove_entry(iter6);
  		kfree(netlbl_unlhsh_addr6_entry(iter6));
  	}
  #endif /* IPv6 */
8cc44579d   Paul Moore   NetLabel: Introdu...
188
189
190
191
192
193
194
195
196
197
  	kfree(iface);
  }
  
  /**
   * netlbl_unlhsh_hash - Hashing function for the hash table
   * @ifindex: the network interface/device to hash
   *
   * Description:
   * This is the hashing function for the unlabeled hash table, it returns the
   * bucket number for the given device/interface.  The caller is responsible for
b914f3a2a   Paul Moore   netlabel: Fix sev...
198
199
   * ensuring that the hash table is protected with either a RCU read lock or
   * the hash table lock.
8cc44579d   Paul Moore   NetLabel: Introdu...
200
201
202
203
   *
   */
  static u32 netlbl_unlhsh_hash(int ifindex)
  {
b914f3a2a   Paul Moore   netlabel: Fix sev...
204
  	return ifindex & (netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->size - 1);
8cc44579d   Paul Moore   NetLabel: Introdu...
205
206
207
  }
  
  /**
8cc44579d   Paul Moore   NetLabel: Introdu...
208
209
210
211
212
213
   * netlbl_unlhsh_search_iface - Search for a matching interface entry
   * @ifindex: the network interface
   *
   * Description:
   * Searches the unlabeled connection hash table and returns a pointer to the
   * interface entry which matches @ifindex, otherwise NULL is returned.  The
b914f3a2a   Paul Moore   netlabel: Fix sev...
214
215
   * caller is responsible for ensuring that the hash table is protected with
   * either a RCU read lock or the hash table lock.
8cc44579d   Paul Moore   NetLabel: Introdu...
216
217
218
219
220
   *
   */
  static struct netlbl_unlhsh_iface *netlbl_unlhsh_search_iface(int ifindex)
  {
  	u32 bkt;
561967010   Paul Moore   netlabel: Fix som...
221
  	struct list_head *bkt_list;
8cc44579d   Paul Moore   NetLabel: Introdu...
222
223
224
  	struct netlbl_unlhsh_iface *iter;
  
  	bkt = netlbl_unlhsh_hash(ifindex);
b914f3a2a   Paul Moore   netlabel: Fix sev...
225
  	bkt_list = &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt];
561967010   Paul Moore   netlabel: Fix som...
226
  	list_for_each_entry_rcu(iter, bkt_list, list)
8cc44579d   Paul Moore   NetLabel: Introdu...
227
228
229
230
231
232
233
  		if (iter->valid && iter->ifindex == ifindex)
  			return iter;
  
  	return NULL;
  }
  
  /**
8cc44579d   Paul Moore   NetLabel: Introdu...
234
235
236
237
238
239
240
241
242
   * netlbl_unlhsh_add_addr4 - Add a new IPv4 address entry to the hash table
   * @iface: the associated interface entry
   * @addr: IPv4 address in network byte order
   * @mask: IPv4 address mask in network byte order
   * @secid: LSM secid value for entry
   *
   * Description:
   * Add a new address entry into the unlabeled connection hash table using the
   * interface entry specified by @iface.  On success zero is returned, otherwise
b914f3a2a   Paul Moore   netlabel: Fix sev...
243
   * a negative value is returned.
8cc44579d   Paul Moore   NetLabel: Introdu...
244
   *
32f50cdee   Paul Moore   [NetLabel]: add a...
245
   */
8cc44579d   Paul Moore   NetLabel: Introdu...
246
247
248
249
250
  static int netlbl_unlhsh_add_addr4(struct netlbl_unlhsh_iface *iface,
  				   const struct in_addr *addr,
  				   const struct in_addr *mask,
  				   u32 secid)
  {
61e106821   Paul Moore   netlabel: Add a g...
251
  	int ret_val;
8cc44579d   Paul Moore   NetLabel: Introdu...
252
  	struct netlbl_unlhsh_addr4 *entry;
8cc44579d   Paul Moore   NetLabel: Introdu...
253
254
255
256
  
  	entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  	if (entry == NULL)
  		return -ENOMEM;
61e106821   Paul Moore   netlabel: Add a g...
257
258
259
  	entry->list.addr = addr->s_addr & mask->s_addr;
  	entry->list.mask = mask->s_addr;
  	entry->list.valid = 1;
61e106821   Paul Moore   netlabel: Add a g...
260
  	entry->secid = secid;
8cc44579d   Paul Moore   NetLabel: Introdu...
261
262
  
  	spin_lock(&netlbl_unlhsh_lock);
61e106821   Paul Moore   netlabel: Add a g...
263
  	ret_val = netlbl_af4list_add(&entry->list, &iface->addr4_list);
8cc44579d   Paul Moore   NetLabel: Introdu...
264
  	spin_unlock(&netlbl_unlhsh_lock);
61e106821   Paul Moore   netlabel: Add a g...
265
266
267
268
  
  	if (ret_val != 0)
  		kfree(entry);
  	return ret_val;
8cc44579d   Paul Moore   NetLabel: Introdu...
269
  }
dfd56b8b3   Eric Dumazet   net: use IS_ENABL...
270
  #if IS_ENABLED(CONFIG_IPV6)
8cc44579d   Paul Moore   NetLabel: Introdu...
271
272
273
274
275
276
277
278
279
280
  /**
   * netlbl_unlhsh_add_addr6 - Add a new IPv6 address entry to the hash table
   * @iface: the associated interface entry
   * @addr: IPv6 address in network byte order
   * @mask: IPv6 address mask in network byte order
   * @secid: LSM secid value for entry
   *
   * Description:
   * Add a new address entry into the unlabeled connection hash table using the
   * interface entry specified by @iface.  On success zero is returned, otherwise
b914f3a2a   Paul Moore   netlabel: Fix sev...
281
   * a negative value is returned.
8cc44579d   Paul Moore   NetLabel: Introdu...
282
283
284
285
286
287
288
   *
   */
  static int netlbl_unlhsh_add_addr6(struct netlbl_unlhsh_iface *iface,
  				   const struct in6_addr *addr,
  				   const struct in6_addr *mask,
  				   u32 secid)
  {
61e106821   Paul Moore   netlabel: Add a g...
289
  	int ret_val;
8cc44579d   Paul Moore   NetLabel: Introdu...
290
  	struct netlbl_unlhsh_addr6 *entry;
8cc44579d   Paul Moore   NetLabel: Introdu...
291
292
293
294
  
  	entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  	if (entry == NULL)
  		return -ENOMEM;
4e3fd7a06   Alexey Dobriyan   net: remove ipv6_...
295
  	entry->list.addr = *addr;
61e106821   Paul Moore   netlabel: Add a g...
296
297
298
299
  	entry->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
  	entry->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
  	entry->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
  	entry->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
4e3fd7a06   Alexey Dobriyan   net: remove ipv6_...
300
  	entry->list.mask = *mask;
61e106821   Paul Moore   netlabel: Add a g...
301
  	entry->list.valid = 1;
61e106821   Paul Moore   netlabel: Add a g...
302
  	entry->secid = secid;
8cc44579d   Paul Moore   NetLabel: Introdu...
303
304
  
  	spin_lock(&netlbl_unlhsh_lock);
61e106821   Paul Moore   netlabel: Add a g...
305
  	ret_val = netlbl_af6list_add(&entry->list, &iface->addr6_list);
8cc44579d   Paul Moore   NetLabel: Introdu...
306
  	spin_unlock(&netlbl_unlhsh_lock);
61e106821   Paul Moore   netlabel: Add a g...
307
308
309
  
  	if (ret_val != 0)
  		kfree(entry);
8cc44579d   Paul Moore   NetLabel: Introdu...
310
311
312
313
314
315
316
317
318
319
320
  	return 0;
  }
  #endif /* IPv6 */
  
  /**
   * netlbl_unlhsh_add_iface - Adds a new interface entry to the hash table
   * @ifindex: network interface
   *
   * Description:
   * Add a new, empty, interface entry into the unlabeled connection hash table.
   * On success a pointer to the new interface entry is returned, on failure NULL
b914f3a2a   Paul Moore   netlabel: Fix sev...
321
   * is returned.
8cc44579d   Paul Moore   NetLabel: Introdu...
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
   *
   */
  static struct netlbl_unlhsh_iface *netlbl_unlhsh_add_iface(int ifindex)
  {
  	u32 bkt;
  	struct netlbl_unlhsh_iface *iface;
  
  	iface = kzalloc(sizeof(*iface), GFP_ATOMIC);
  	if (iface == NULL)
  		return NULL;
  
  	iface->ifindex = ifindex;
  	INIT_LIST_HEAD(&iface->addr4_list);
  	INIT_LIST_HEAD(&iface->addr6_list);
  	iface->valid = 1;
8cc44579d   Paul Moore   NetLabel: Introdu...
337
338
339
340
341
342
343
  
  	spin_lock(&netlbl_unlhsh_lock);
  	if (ifindex > 0) {
  		bkt = netlbl_unlhsh_hash(ifindex);
  		if (netlbl_unlhsh_search_iface(ifindex) != NULL)
  			goto add_iface_failure;
  		list_add_tail_rcu(&iface->list,
b914f3a2a   Paul Moore   netlabel: Fix sev...
344
  			     &netlbl_unlhsh_rcu_deref(netlbl_unlhsh)->tbl[bkt]);
8cc44579d   Paul Moore   NetLabel: Introdu...
345
346
  	} else {
  		INIT_LIST_HEAD(&iface->list);
b914f3a2a   Paul Moore   netlabel: Fix sev...
347
  		if (netlbl_unlhsh_rcu_deref(netlbl_unlhsh_def) != NULL)
8cc44579d   Paul Moore   NetLabel: Introdu...
348
  			goto add_iface_failure;
cf778b00e   Eric Dumazet   net: reintroduce ...
349
  		rcu_assign_pointer(netlbl_unlhsh_def, iface);
8cc44579d   Paul Moore   NetLabel: Introdu...
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
  	}
  	spin_unlock(&netlbl_unlhsh_lock);
  
  	return iface;
  
  add_iface_failure:
  	spin_unlock(&netlbl_unlhsh_lock);
  	kfree(iface);
  	return NULL;
  }
  
  /**
   * netlbl_unlhsh_add - Adds a new entry to the unlabeled connection hash table
   * @net: network namespace
   * @dev_name: interface name
   * @addr: IP address in network byte order
   * @mask: address mask in network byte order
   * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
   * @secid: LSM secid value for the entry
13541b3ad   Paul Moore   NetLabel: Add aud...
369
   * @audit_info: NetLabel audit information
8cc44579d   Paul Moore   NetLabel: Introdu...
370
371
372
373
374
375
   *
   * Description:
   * Adds a new entry to the unlabeled connection hash table.  Returns zero on
   * success, negative values on failure.
   *
   */
6c2e8ac09   Paul Moore   netlabel: Update ...
376
377
378
379
380
381
382
  int netlbl_unlhsh_add(struct net *net,
  		      const char *dev_name,
  		      const void *addr,
  		      const void *mask,
  		      u32 addr_len,
  		      u32 secid,
  		      struct netlbl_audit *audit_info)
8cc44579d   Paul Moore   NetLabel: Introdu...
383
384
385
386
387
  {
  	int ret_val;
  	int ifindex;
  	struct net_device *dev;
  	struct netlbl_unlhsh_iface *iface;
13541b3ad   Paul Moore   NetLabel: Add aud...
388
389
390
  	struct audit_buffer *audit_buf = NULL;
  	char *secctx = NULL;
  	u32 secctx_len;
8cc44579d   Paul Moore   NetLabel: Introdu...
391
392
393
394
395
396
397
  
  	if (addr_len != sizeof(struct in_addr) &&
  	    addr_len != sizeof(struct in6_addr))
  		return -EINVAL;
  
  	rcu_read_lock();
  	if (dev_name != NULL) {
122ec6ffc   Eric Dumazet   netlabel: remove ...
398
  		dev = dev_get_by_name_rcu(net, dev_name);
8cc44579d   Paul Moore   NetLabel: Introdu...
399
400
401
402
403
  		if (dev == NULL) {
  			ret_val = -ENODEV;
  			goto unlhsh_add_return;
  		}
  		ifindex = dev->ifindex;
8cc44579d   Paul Moore   NetLabel: Introdu...
404
405
406
407
408
409
410
411
412
413
414
415
  		iface = netlbl_unlhsh_search_iface(ifindex);
  	} else {
  		ifindex = 0;
  		iface = rcu_dereference(netlbl_unlhsh_def);
  	}
  	if (iface == NULL) {
  		iface = netlbl_unlhsh_add_iface(ifindex);
  		if (iface == NULL) {
  			ret_val = -ENOMEM;
  			goto unlhsh_add_return;
  		}
  	}
13541b3ad   Paul Moore   NetLabel: Add aud...
416
417
  	audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCADD,
  					      audit_info);
8cc44579d   Paul Moore   NetLabel: Introdu...
418
  	switch (addr_len) {
56628b1d8   Pavel Emelyanov   [NETLABEL]: Don't...
419
  	case sizeof(struct in_addr): {
ea1107338   Joe Perches   net: Remove casts...
420
421
  		const struct in_addr *addr4 = addr;
  		const struct in_addr *mask4 = mask;
56628b1d8   Pavel Emelyanov   [NETLABEL]: Don't...
422

13541b3ad   Paul Moore   NetLabel: Add aud...
423
424
  		ret_val = netlbl_unlhsh_add_addr4(iface, addr4, mask4, secid);
  		if (audit_buf != NULL)
63c416887   Paul Moore   netlabel: Add net...
425
426
427
428
  			netlbl_af4list_audit_addr(audit_buf, 1,
  						  dev_name,
  						  addr4->s_addr,
  						  mask4->s_addr);
8cc44579d   Paul Moore   NetLabel: Introdu...
429
  		break;
56628b1d8   Pavel Emelyanov   [NETLABEL]: Don't...
430
  	}
dfd56b8b3   Eric Dumazet   net: use IS_ENABL...
431
  #if IS_ENABLED(CONFIG_IPV6)
56628b1d8   Pavel Emelyanov   [NETLABEL]: Don't...
432
  	case sizeof(struct in6_addr): {
ea1107338   Joe Perches   net: Remove casts...
433
434
  		const struct in6_addr *addr6 = addr;
  		const struct in6_addr *mask6 = mask;
56628b1d8   Pavel Emelyanov   [NETLABEL]: Don't...
435

13541b3ad   Paul Moore   NetLabel: Add aud...
436
437
  		ret_val = netlbl_unlhsh_add_addr6(iface, addr6, mask6, secid);
  		if (audit_buf != NULL)
63c416887   Paul Moore   netlabel: Add net...
438
439
440
  			netlbl_af6list_audit_addr(audit_buf, 1,
  						  dev_name,
  						  addr6, mask6);
8cc44579d   Paul Moore   NetLabel: Introdu...
441
  		break;
56628b1d8   Pavel Emelyanov   [NETLABEL]: Don't...
442
  	}
8cc44579d   Paul Moore   NetLabel: Introdu...
443
444
445
446
447
448
449
450
451
  #endif /* IPv6 */
  	default:
  		ret_val = -EINVAL;
  	}
  	if (ret_val == 0)
  		atomic_inc(&netlabel_mgmt_protocount);
  
  unlhsh_add_return:
  	rcu_read_unlock();
13541b3ad   Paul Moore   NetLabel: Add aud...
452
453
454
455
456
457
458
459
460
461
  	if (audit_buf != NULL) {
  		if (security_secid_to_secctx(secid,
  					     &secctx,
  					     &secctx_len) == 0) {
  			audit_log_format(audit_buf, " sec_obj=%s", secctx);
  			security_release_secctx(secctx, secctx_len);
  		}
  		audit_log_format(audit_buf, " res=%u", ret_val == 0 ? 1 : 0);
  		audit_log_end(audit_buf);
  	}
8cc44579d   Paul Moore   NetLabel: Introdu...
462
463
464
465
466
  	return ret_val;
  }
  
  /**
   * netlbl_unlhsh_remove_addr4 - Remove an IPv4 address entry
13541b3ad   Paul Moore   NetLabel: Add aud...
467
   * @net: network namespace
8cc44579d   Paul Moore   NetLabel: Introdu...
468
469
470
   * @iface: interface entry
   * @addr: IP address
   * @mask: IP address mask
13541b3ad   Paul Moore   NetLabel: Add aud...
471
   * @audit_info: NetLabel audit information
8cc44579d   Paul Moore   NetLabel: Introdu...
472
473
474
   *
   * Description:
   * Remove an IP address entry from the unlabeled connection hash table.
b914f3a2a   Paul Moore   netlabel: Fix sev...
475
   * Returns zero on success, negative values on failure.
8cc44579d   Paul Moore   NetLabel: Introdu...
476
477
   *
   */
13541b3ad   Paul Moore   NetLabel: Add aud...
478
479
  static int netlbl_unlhsh_remove_addr4(struct net *net,
  				      struct netlbl_unlhsh_iface *iface,
8cc44579d   Paul Moore   NetLabel: Introdu...
480
  				      const struct in_addr *addr,
13541b3ad   Paul Moore   NetLabel: Add aud...
481
482
  				      const struct in_addr *mask,
  				      struct netlbl_audit *audit_info)
8cc44579d   Paul Moore   NetLabel: Introdu...
483
  {
61e106821   Paul Moore   netlabel: Add a g...
484
  	struct netlbl_af4list *list_entry;
8cc44579d   Paul Moore   NetLabel: Introdu...
485
  	struct netlbl_unlhsh_addr4 *entry;
61e106821   Paul Moore   netlabel: Add a g...
486
  	struct audit_buffer *audit_buf;
13541b3ad   Paul Moore   NetLabel: Add aud...
487
  	struct net_device *dev;
61e106821   Paul Moore   netlabel: Add a g...
488
  	char *secctx;
13541b3ad   Paul Moore   NetLabel: Add aud...
489
  	u32 secctx_len;
8cc44579d   Paul Moore   NetLabel: Introdu...
490
491
  
  	spin_lock(&netlbl_unlhsh_lock);
61e106821   Paul Moore   netlabel: Add a g...
492
493
  	list_entry = netlbl_af4list_remove(addr->s_addr, mask->s_addr,
  					   &iface->addr4_list);
8cc44579d   Paul Moore   NetLabel: Introdu...
494
  	spin_unlock(&netlbl_unlhsh_lock);
d25830e55   Paul Moore   netlabel: Fix a p...
495
496
497
  	if (list_entry != NULL)
  		entry = netlbl_unlhsh_addr4_entry(list_entry);
  	else
ec8f2375d   Paul Moore   netlabel: Compile...
498
  		entry = NULL;
8cc44579d   Paul Moore   NetLabel: Introdu...
499

13541b3ad   Paul Moore   NetLabel: Add aud...
500
501
502
503
  	audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
  					      audit_info);
  	if (audit_buf != NULL) {
  		dev = dev_get_by_index(net, iface->ifindex);
63c416887   Paul Moore   netlabel: Add net...
504
505
506
  		netlbl_af4list_audit_addr(audit_buf, 1,
  					  (dev != NULL ? dev->name : NULL),
  					  addr->s_addr, mask->s_addr);
13541b3ad   Paul Moore   NetLabel: Add aud...
507
508
  		if (dev != NULL)
  			dev_put(dev);
ec8f2375d   Paul Moore   netlabel: Compile...
509
510
511
  		if (entry != NULL &&
  		    security_secid_to_secctx(entry->secid,
  					     &secctx, &secctx_len) == 0) {
13541b3ad   Paul Moore   NetLabel: Add aud...
512
513
514
  			audit_log_format(audit_buf, " sec_obj=%s", secctx);
  			security_release_secctx(secctx, secctx_len);
  		}
ec8f2375d   Paul Moore   netlabel: Compile...
515
  		audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
13541b3ad   Paul Moore   NetLabel: Add aud...
516
517
  		audit_log_end(audit_buf);
  	}
ec8f2375d   Paul Moore   netlabel: Compile...
518
519
  	if (entry == NULL)
  		return -ENOENT;
c3b494209   Lai Jiangshan   net,rcu: convert ...
520
  	kfree_rcu(entry, rcu);
ec8f2375d   Paul Moore   netlabel: Compile...
521
  	return 0;
8cc44579d   Paul Moore   NetLabel: Introdu...
522
  }
dfd56b8b3   Eric Dumazet   net: use IS_ENABL...
523
  #if IS_ENABLED(CONFIG_IPV6)
8cc44579d   Paul Moore   NetLabel: Introdu...
524
525
  /**
   * netlbl_unlhsh_remove_addr6 - Remove an IPv6 address entry
13541b3ad   Paul Moore   NetLabel: Add aud...
526
   * @net: network namespace
8cc44579d   Paul Moore   NetLabel: Introdu...
527
528
529
   * @iface: interface entry
   * @addr: IP address
   * @mask: IP address mask
13541b3ad   Paul Moore   NetLabel: Add aud...
530
   * @audit_info: NetLabel audit information
8cc44579d   Paul Moore   NetLabel: Introdu...
531
532
533
   *
   * Description:
   * Remove an IP address entry from the unlabeled connection hash table.
b914f3a2a   Paul Moore   netlabel: Fix sev...
534
   * Returns zero on success, negative values on failure.
8cc44579d   Paul Moore   NetLabel: Introdu...
535
536
   *
   */
13541b3ad   Paul Moore   NetLabel: Add aud...
537
538
  static int netlbl_unlhsh_remove_addr6(struct net *net,
  				      struct netlbl_unlhsh_iface *iface,
8cc44579d   Paul Moore   NetLabel: Introdu...
539
  				      const struct in6_addr *addr,
13541b3ad   Paul Moore   NetLabel: Add aud...
540
541
  				      const struct in6_addr *mask,
  				      struct netlbl_audit *audit_info)
8cc44579d   Paul Moore   NetLabel: Introdu...
542
  {
61e106821   Paul Moore   netlabel: Add a g...
543
  	struct netlbl_af6list *list_entry;
8cc44579d   Paul Moore   NetLabel: Introdu...
544
  	struct netlbl_unlhsh_addr6 *entry;
61e106821   Paul Moore   netlabel: Add a g...
545
  	struct audit_buffer *audit_buf;
13541b3ad   Paul Moore   NetLabel: Add aud...
546
  	struct net_device *dev;
61e106821   Paul Moore   netlabel: Add a g...
547
  	char *secctx;
13541b3ad   Paul Moore   NetLabel: Add aud...
548
  	u32 secctx_len;
8cc44579d   Paul Moore   NetLabel: Introdu...
549
550
  
  	spin_lock(&netlbl_unlhsh_lock);
61e106821   Paul Moore   netlabel: Add a g...
551
  	list_entry = netlbl_af6list_remove(addr, mask, &iface->addr6_list);
8cc44579d   Paul Moore   NetLabel: Introdu...
552
  	spin_unlock(&netlbl_unlhsh_lock);
d25830e55   Paul Moore   netlabel: Fix a p...
553
554
555
  	if (list_entry != NULL)
  		entry = netlbl_unlhsh_addr6_entry(list_entry);
  	else
ec8f2375d   Paul Moore   netlabel: Compile...
556
  		entry = NULL;
8cc44579d   Paul Moore   NetLabel: Introdu...
557

13541b3ad   Paul Moore   NetLabel: Add aud...
558
559
560
561
  	audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_STCDEL,
  					      audit_info);
  	if (audit_buf != NULL) {
  		dev = dev_get_by_index(net, iface->ifindex);
63c416887   Paul Moore   netlabel: Add net...
562
563
564
  		netlbl_af6list_audit_addr(audit_buf, 1,
  					  (dev != NULL ? dev->name : NULL),
  					  addr, mask);
13541b3ad   Paul Moore   NetLabel: Add aud...
565
566
  		if (dev != NULL)
  			dev_put(dev);
ec8f2375d   Paul Moore   netlabel: Compile...
567
568
569
  		if (entry != NULL &&
  		    security_secid_to_secctx(entry->secid,
  					     &secctx, &secctx_len) == 0) {
13541b3ad   Paul Moore   NetLabel: Add aud...
570
571
572
  			audit_log_format(audit_buf, " sec_obj=%s", secctx);
  			security_release_secctx(secctx, secctx_len);
  		}
ec8f2375d   Paul Moore   netlabel: Compile...
573
  		audit_log_format(audit_buf, " res=%u", entry != NULL ? 1 : 0);
13541b3ad   Paul Moore   NetLabel: Add aud...
574
575
  		audit_log_end(audit_buf);
  	}
ec8f2375d   Paul Moore   netlabel: Compile...
576
577
  	if (entry == NULL)
  		return -ENOENT;
6b2622328   Lai Jiangshan   net,rcu: convert ...
578
  	kfree_rcu(entry, rcu);
ec8f2375d   Paul Moore   netlabel: Compile...
579
  	return 0;
8cc44579d   Paul Moore   NetLabel: Introdu...
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
  }
  #endif /* IPv6 */
  
  /**
   * netlbl_unlhsh_condremove_iface - Remove an interface entry
   * @iface: the interface entry
   *
   * Description:
   * Remove an interface entry from the unlabeled connection hash table if it is
   * empty.  An interface entry is considered to be empty if there are no
   * address entries assigned to it.
   *
   */
  static void netlbl_unlhsh_condremove_iface(struct netlbl_unlhsh_iface *iface)
  {
61e106821   Paul Moore   netlabel: Add a g...
595
  	struct netlbl_af4list *iter4;
dfd56b8b3   Eric Dumazet   net: use IS_ENABL...
596
  #if IS_ENABLED(CONFIG_IPV6)
61e106821   Paul Moore   netlabel: Add a g...
597
598
  	struct netlbl_af6list *iter6;
  #endif /* IPv6 */
8cc44579d   Paul Moore   NetLabel: Introdu...
599
600
  
  	spin_lock(&netlbl_unlhsh_lock);
61e106821   Paul Moore   netlabel: Add a g...
601
602
  	netlbl_af4list_foreach_rcu(iter4, &iface->addr4_list)
  		goto unlhsh_condremove_failure;
dfd56b8b3   Eric Dumazet   net: use IS_ENABL...
603
  #if IS_ENABLED(CONFIG_IPV6)
61e106821   Paul Moore   netlabel: Add a g...
604
605
606
  	netlbl_af6list_foreach_rcu(iter6, &iface->addr6_list)
  		goto unlhsh_condremove_failure;
  #endif /* IPv6 */
8cc44579d   Paul Moore   NetLabel: Introdu...
607
608
609
610
  	iface->valid = 0;
  	if (iface->ifindex > 0)
  		list_del_rcu(&iface->list);
  	else
a9b3cd7f3   Stephen Hemminger   rcu: convert uses...
611
  		RCU_INIT_POINTER(netlbl_unlhsh_def, NULL);
8cc44579d   Paul Moore   NetLabel: Introdu...
612
613
614
615
616
617
618
  	spin_unlock(&netlbl_unlhsh_lock);
  
  	call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
  	return;
  
  unlhsh_condremove_failure:
  	spin_unlock(&netlbl_unlhsh_lock);
8cc44579d   Paul Moore   NetLabel: Introdu...
619
620
621
622
623
624
625
626
627
  }
  
  /**
   * netlbl_unlhsh_remove - Remove an entry from the unlabeled hash table
   * @net: network namespace
   * @dev_name: interface name
   * @addr: IP address in network byte order
   * @mask: address mask in network byte order
   * @addr_len: length of address/mask (4 for IPv4, 16 for IPv6)
13541b3ad   Paul Moore   NetLabel: Add aud...
628
   * @audit_info: NetLabel audit information
8cc44579d   Paul Moore   NetLabel: Introdu...
629
630
631
632
633
634
   *
   * Description:
   * Removes and existing entry from the unlabeled connection hash table.
   * Returns zero on success, negative values on failure.
   *
   */
6c2e8ac09   Paul Moore   netlabel: Update ...
635
636
637
638
639
640
  int netlbl_unlhsh_remove(struct net *net,
  			 const char *dev_name,
  			 const void *addr,
  			 const void *mask,
  			 u32 addr_len,
  			 struct netlbl_audit *audit_info)
8cc44579d   Paul Moore   NetLabel: Introdu...
641
642
643
644
645
646
647
648
649
650
651
  {
  	int ret_val;
  	struct net_device *dev;
  	struct netlbl_unlhsh_iface *iface;
  
  	if (addr_len != sizeof(struct in_addr) &&
  	    addr_len != sizeof(struct in6_addr))
  		return -EINVAL;
  
  	rcu_read_lock();
  	if (dev_name != NULL) {
122ec6ffc   Eric Dumazet   netlabel: remove ...
652
  		dev = dev_get_by_name_rcu(net, dev_name);
8cc44579d   Paul Moore   NetLabel: Introdu...
653
654
655
656
657
  		if (dev == NULL) {
  			ret_val = -ENODEV;
  			goto unlhsh_remove_return;
  		}
  		iface = netlbl_unlhsh_search_iface(dev->ifindex);
8cc44579d   Paul Moore   NetLabel: Introdu...
658
659
660
661
662
663
664
665
  	} else
  		iface = rcu_dereference(netlbl_unlhsh_def);
  	if (iface == NULL) {
  		ret_val = -ENOENT;
  		goto unlhsh_remove_return;
  	}
  	switch (addr_len) {
  	case sizeof(struct in_addr):
13541b3ad   Paul Moore   NetLabel: Add aud...
666
667
668
  		ret_val = netlbl_unlhsh_remove_addr4(net,
  						     iface, addr, mask,
  						     audit_info);
8cc44579d   Paul Moore   NetLabel: Introdu...
669
  		break;
dfd56b8b3   Eric Dumazet   net: use IS_ENABL...
670
  #if IS_ENABLED(CONFIG_IPV6)
8cc44579d   Paul Moore   NetLabel: Introdu...
671
  	case sizeof(struct in6_addr):
13541b3ad   Paul Moore   NetLabel: Add aud...
672
673
674
  		ret_val = netlbl_unlhsh_remove_addr6(net,
  						     iface, addr, mask,
  						     audit_info);
8cc44579d   Paul Moore   NetLabel: Introdu...
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
  		break;
  #endif /* IPv6 */
  	default:
  		ret_val = -EINVAL;
  	}
  	if (ret_val == 0) {
  		netlbl_unlhsh_condremove_iface(iface);
  		atomic_dec(&netlabel_mgmt_protocount);
  	}
  
  unlhsh_remove_return:
  	rcu_read_unlock();
  	return ret_val;
  }
  
  /*
   * General Helper Functions
   */
  
  /**
   * netlbl_unlhsh_netdev_handler - Network device notification handler
   * @this: notifier block
   * @event: the event
   * @ptr: the network device (cast to void)
   *
   * Description:
   * Handle network device events, although at present all we care about is a
   * network device going away.  In the case of a device going away we clear any
   * related entries from the unlabeled connection hash table.
   *
   */
  static int netlbl_unlhsh_netdev_handler(struct notifier_block *this,
  					unsigned long event,
  					void *ptr)
  {
  	struct net_device *dev = ptr;
  	struct netlbl_unlhsh_iface *iface = NULL;
721499e89   YOSHIFUJI Hideaki   netns: Use net_eq...
712
  	if (!net_eq(dev_net(dev), &init_net))
8cc44579d   Paul Moore   NetLabel: Introdu...
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
  		return NOTIFY_DONE;
  
  	/* XXX - should this be a check for NETDEV_DOWN or _UNREGISTER? */
  	if (event == NETDEV_DOWN) {
  		spin_lock(&netlbl_unlhsh_lock);
  		iface = netlbl_unlhsh_search_iface(dev->ifindex);
  		if (iface != NULL && iface->valid) {
  			iface->valid = 0;
  			list_del_rcu(&iface->list);
  		} else
  			iface = NULL;
  		spin_unlock(&netlbl_unlhsh_lock);
  	}
  
  	if (iface != NULL)
  		call_rcu(&iface->rcu, netlbl_unlhsh_free_iface);
  
  	return NOTIFY_DONE;
  }
32f50cdee   Paul Moore   [NetLabel]: add a...
732
733
734
735
  
  /**
   * netlbl_unlabel_acceptflg_set - Set the unlabeled accept flag
   * @value: desired value
95d4e6be2   Paul Moore   [NetLabel]: audit...
736
   * @audit_info: NetLabel audit information
32f50cdee   Paul Moore   [NetLabel]: add a...
737
738
739
740
741
   *
   * Description:
   * Set the value of the unlabeled accept flag to @value.
   *
   */
95d4e6be2   Paul Moore   [NetLabel]: audit...
742
743
  static void netlbl_unlabel_acceptflg_set(u8 value,
  					 struct netlbl_audit *audit_info)
32f50cdee   Paul Moore   [NetLabel]: add a...
744
  {
95d4e6be2   Paul Moore   [NetLabel]: audit...
745
746
  	struct audit_buffer *audit_buf;
  	u8 old_val;
4be2700fb   Paul Moore   [NetLabel]: corre...
747
  	old_val = netlabel_unlabel_acceptflg;
cd28786d6   Paul Moore   NetLabel: convert...
748
  	netlabel_unlabel_acceptflg = value;
95d4e6be2   Paul Moore   [NetLabel]: audit...
749
750
  	audit_buf = netlbl_audit_start_common(AUDIT_MAC_UNLBL_ALLOW,
  					      audit_info);
de64688ff   Paul Moore   NetLabel: honor t...
751
752
753
754
755
  	if (audit_buf != NULL) {
  		audit_log_format(audit_buf,
  				 " unlbl_accept=%u old=%u", value, old_val);
  		audit_log_end(audit_buf);
  	}
32f50cdee   Paul Moore   [NetLabel]: add a...
756
  }
8cc44579d   Paul Moore   NetLabel: Introdu...
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
  /**
   * netlbl_unlabel_addrinfo_get - Get the IPv4/6 address information
   * @info: the Generic NETLINK info block
   * @addr: the IP address
   * @mask: the IP address mask
   * @len: the address length
   *
   * Description:
   * Examine the Generic NETLINK message and extract the IP address information.
   * Returns zero on success, negative values on failure.
   *
   */
  static int netlbl_unlabel_addrinfo_get(struct genl_info *info,
  				       void **addr,
  				       void **mask,
  				       u32 *len)
  {
  	u32 addr_len;
  
  	if (info->attrs[NLBL_UNLABEL_A_IPV4ADDR]) {
  		addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
  		if (addr_len != sizeof(struct in_addr) &&
  		    addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV4MASK]))
  			return -EINVAL;
  		*len = addr_len;
  		*addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4ADDR]);
  		*mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV4MASK]);
  		return 0;
  	} else if (info->attrs[NLBL_UNLABEL_A_IPV6ADDR]) {
  		addr_len = nla_len(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
  		if (addr_len != sizeof(struct in6_addr) &&
  		    addr_len != nla_len(info->attrs[NLBL_UNLABEL_A_IPV6MASK]))
  			return -EINVAL;
  		*len = addr_len;
  		*addr = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6ADDR]);
  		*mask = nla_data(info->attrs[NLBL_UNLABEL_A_IPV6MASK]);
  		return 0;
  	}
  
  	return -EINVAL;
  }
32f50cdee   Paul Moore   [NetLabel]: add a...
798
  /*
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
   * NetLabel Command Handlers
   */
  
  /**
   * netlbl_unlabel_accept - Handle an ACCEPT message
   * @skb: the NETLINK buffer
   * @info: the Generic NETLINK info block
   *
   * Description:
   * Process a user generated ACCEPT message and set the accept flag accordingly.
   * Returns zero on success, negative values on failure.
   *
   */
  static int netlbl_unlabel_accept(struct sk_buff *skb, struct genl_info *info)
  {
fd3858554   Paul Moore   [NetLabel]: rewor...
814
  	u8 value;
95d4e6be2   Paul Moore   [NetLabel]: audit...
815
  	struct netlbl_audit audit_info;
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
816

fd3858554   Paul Moore   [NetLabel]: rewor...
817
818
  	if (info->attrs[NLBL_UNLABEL_A_ACPTFLG]) {
  		value = nla_get_u8(info->attrs[NLBL_UNLABEL_A_ACPTFLG]);
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
819
  		if (value == 1 || value == 0) {
95d4e6be2   Paul Moore   [NetLabel]: audit...
820
821
  			netlbl_netlink_auditinfo(skb, &audit_info);
  			netlbl_unlabel_acceptflg_set(value, &audit_info);
32f50cdee   Paul Moore   [NetLabel]: add a...
822
  			return 0;
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
823
824
  		}
  	}
32f50cdee   Paul Moore   [NetLabel]: add a...
825
  	return -EINVAL;
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
826
827
828
829
830
831
832
833
834
835
836
837
838
839
  }
  
  /**
   * netlbl_unlabel_list - Handle a LIST message
   * @skb: the NETLINK buffer
   * @info: the Generic NETLINK info block
   *
   * Description:
   * Process a user generated LIST message and respond with the current status.
   * Returns zero on success, negative values on failure.
   *
   */
  static int netlbl_unlabel_list(struct sk_buff *skb, struct genl_info *info)
  {
fd3858554   Paul Moore   [NetLabel]: rewor...
840
  	int ret_val = -EINVAL;
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
841
  	struct sk_buff *ans_skb;
fd3858554   Paul Moore   [NetLabel]: rewor...
842
  	void *data;
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
843

339bf98ff   Thomas Graf   [NETLINK]: Do pre...
844
  	ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
845
846
  	if (ans_skb == NULL)
  		goto list_failure;
17c157c88   Thomas Graf   [GENL]: Add genlm...
847
848
  	data = genlmsg_put_reply(ans_skb, info, &netlbl_unlabel_gnl_family,
  				 0, NLBL_UNLABEL_C_LIST);
fd3858554   Paul Moore   [NetLabel]: rewor...
849
850
  	if (data == NULL) {
  		ret_val = -ENOMEM;
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
851
  		goto list_failure;
fd3858554   Paul Moore   [NetLabel]: rewor...
852
  	}
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
853

fd3858554   Paul Moore   [NetLabel]: rewor...
854
855
  	ret_val = nla_put_u8(ans_skb,
  			     NLBL_UNLABEL_A_ACPTFLG,
cd28786d6   Paul Moore   NetLabel: convert...
856
  			     netlabel_unlabel_acceptflg);
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
857
858
  	if (ret_val != 0)
  		goto list_failure;
fd3858554   Paul Moore   [NetLabel]: rewor...
859
  	genlmsg_end(ans_skb, data);
fe785bee0   Denis V. Lunev   netlabel: netlink...
860
  	return genlmsg_reply(ans_skb, info);
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
861
862
  
  list_failure:
b08d5840d   Patrick McHardy   [NET]: Fix kfree(...
863
  	kfree_skb(ans_skb);
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
864
865
  	return ret_val;
  }
8cc44579d   Paul Moore   NetLabel: Introdu...
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
  /**
   * netlbl_unlabel_staticadd - Handle a STATICADD message
   * @skb: the NETLINK buffer
   * @info: the Generic NETLINK info block
   *
   * Description:
   * Process a user generated STATICADD message and add a new unlabeled
   * connection entry to the hash table.  Returns zero on success, negative
   * values on failure.
   *
   */
  static int netlbl_unlabel_staticadd(struct sk_buff *skb,
  				    struct genl_info *info)
  {
  	int ret_val;
  	char *dev_name;
  	void *addr;
  	void *mask;
  	u32 addr_len;
  	u32 secid;
13541b3ad   Paul Moore   NetLabel: Add aud...
886
  	struct netlbl_audit audit_info;
8cc44579d   Paul Moore   NetLabel: Introdu...
887
888
889
890
891
892
893
894
895
896
897
898
  
  	/* Don't allow users to add both IPv4 and IPv6 addresses for a
  	 * single entry.  However, allow users to create two entries, one each
  	 * for IPv4 and IPv4, with the same LSM security context which should
  	 * achieve the same result. */
  	if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
  	    !info->attrs[NLBL_UNLABEL_A_IFACE] ||
  	    !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  	       !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  	      (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  	       !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  		return -EINVAL;
13541b3ad   Paul Moore   NetLabel: Add aud...
899
  	netlbl_netlink_auditinfo(skb, &audit_info);
8cc44579d   Paul Moore   NetLabel: Introdu...
900
901
902
903
904
905
906
907
908
909
910
911
  	ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  	if (ret_val != 0)
  		return ret_val;
  	dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
  	ret_val = security_secctx_to_secid(
  		                  nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  				  nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  				  &secid);
  	if (ret_val != 0)
  		return ret_val;
  
  	return netlbl_unlhsh_add(&init_net,
13541b3ad   Paul Moore   NetLabel: Add aud...
912
913
  				 dev_name, addr, mask, addr_len, secid,
  				 &audit_info);
8cc44579d   Paul Moore   NetLabel: Introdu...
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
  }
  
  /**
   * netlbl_unlabel_staticadddef - Handle a STATICADDDEF message
   * @skb: the NETLINK buffer
   * @info: the Generic NETLINK info block
   *
   * Description:
   * Process a user generated STATICADDDEF message and add a new default
   * unlabeled connection entry.  Returns zero on success, negative values on
   * failure.
   *
   */
  static int netlbl_unlabel_staticadddef(struct sk_buff *skb,
  				       struct genl_info *info)
  {
  	int ret_val;
  	void *addr;
  	void *mask;
  	u32 addr_len;
  	u32 secid;
13541b3ad   Paul Moore   NetLabel: Add aud...
935
  	struct netlbl_audit audit_info;
8cc44579d   Paul Moore   NetLabel: Introdu...
936
937
938
939
940
941
942
943
944
945
946
  
  	/* Don't allow users to add both IPv4 and IPv6 addresses for a
  	 * single entry.  However, allow users to create two entries, one each
  	 * for IPv4 and IPv6, with the same LSM security context which should
  	 * achieve the same result. */
  	if (!info->attrs[NLBL_UNLABEL_A_SECCTX] ||
  	    !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  	       !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  	      (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  	       !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  		return -EINVAL;
13541b3ad   Paul Moore   NetLabel: Add aud...
947
  	netlbl_netlink_auditinfo(skb, &audit_info);
8cc44579d   Paul Moore   NetLabel: Introdu...
948
949
950
951
952
953
954
955
956
  	ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  	if (ret_val != 0)
  		return ret_val;
  	ret_val = security_secctx_to_secid(
  		                  nla_data(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  				  nla_len(info->attrs[NLBL_UNLABEL_A_SECCTX]),
  				  &secid);
  	if (ret_val != 0)
  		return ret_val;
13541b3ad   Paul Moore   NetLabel: Add aud...
957
958
959
  	return netlbl_unlhsh_add(&init_net,
  				 NULL, addr, mask, addr_len, secid,
  				 &audit_info);
8cc44579d   Paul Moore   NetLabel: Introdu...
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
  }
  
  /**
   * netlbl_unlabel_staticremove - Handle a STATICREMOVE message
   * @skb: the NETLINK buffer
   * @info: the Generic NETLINK info block
   *
   * Description:
   * Process a user generated STATICREMOVE message and remove the specified
   * unlabeled connection entry.  Returns zero on success, negative values on
   * failure.
   *
   */
  static int netlbl_unlabel_staticremove(struct sk_buff *skb,
  				       struct genl_info *info)
  {
  	int ret_val;
  	char *dev_name;
  	void *addr;
  	void *mask;
  	u32 addr_len;
13541b3ad   Paul Moore   NetLabel: Add aud...
981
  	struct netlbl_audit audit_info;
8cc44579d   Paul Moore   NetLabel: Introdu...
982
983
984
985
986
987
988
989
990
  
  	/* See the note in netlbl_unlabel_staticadd() about not allowing both
  	 * IPv4 and IPv6 in the same entry. */
  	if (!info->attrs[NLBL_UNLABEL_A_IFACE] ||
  	    !((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  	       !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  	      (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  	       !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  		return -EINVAL;
13541b3ad   Paul Moore   NetLabel: Add aud...
991
  	netlbl_netlink_auditinfo(skb, &audit_info);
8cc44579d   Paul Moore   NetLabel: Introdu...
992
993
994
995
  	ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  	if (ret_val != 0)
  		return ret_val;
  	dev_name = nla_data(info->attrs[NLBL_UNLABEL_A_IFACE]);
13541b3ad   Paul Moore   NetLabel: Add aud...
996
997
998
  	return netlbl_unlhsh_remove(&init_net,
  				    dev_name, addr, mask, addr_len,
  				    &audit_info);
8cc44579d   Paul Moore   NetLabel: Introdu...
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
  }
  
  /**
   * netlbl_unlabel_staticremovedef - Handle a STATICREMOVEDEF message
   * @skb: the NETLINK buffer
   * @info: the Generic NETLINK info block
   *
   * Description:
   * Process a user generated STATICREMOVEDEF message and remove the default
   * unlabeled connection entry.  Returns zero on success, negative values on
   * failure.
   *
   */
  static int netlbl_unlabel_staticremovedef(struct sk_buff *skb,
  					  struct genl_info *info)
  {
  	int ret_val;
  	void *addr;
  	void *mask;
  	u32 addr_len;
13541b3ad   Paul Moore   NetLabel: Add aud...
1019
  	struct netlbl_audit audit_info;
8cc44579d   Paul Moore   NetLabel: Introdu...
1020
1021
1022
1023
1024
1025
1026
1027
  
  	/* See the note in netlbl_unlabel_staticadd() about not allowing both
  	 * IPv4 and IPv6 in the same entry. */
  	if (!((!info->attrs[NLBL_UNLABEL_A_IPV4ADDR] ||
  	       !info->attrs[NLBL_UNLABEL_A_IPV4MASK]) ^
  	      (!info->attrs[NLBL_UNLABEL_A_IPV6ADDR] ||
  	       !info->attrs[NLBL_UNLABEL_A_IPV6MASK])))
  		return -EINVAL;
13541b3ad   Paul Moore   NetLabel: Add aud...
1028
  	netlbl_netlink_auditinfo(skb, &audit_info);
8cc44579d   Paul Moore   NetLabel: Introdu...
1029
1030
1031
  	ret_val = netlbl_unlabel_addrinfo_get(info, &addr, &mask, &addr_len);
  	if (ret_val != 0)
  		return ret_val;
13541b3ad   Paul Moore   NetLabel: Add aud...
1032
1033
1034
  	return netlbl_unlhsh_remove(&init_net,
  				    NULL, addr, mask, addr_len,
  				    &audit_info);
8cc44579d   Paul Moore   NetLabel: Introdu...
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
  }
  
  
  /**
   * netlbl_unlabel_staticlist_gen - Generate messages for STATICLIST[DEF]
   * @cmd: command/message
   * @iface: the interface entry
   * @addr4: the IPv4 address entry
   * @addr6: the IPv6 address entry
   * @arg: the netlbl_unlhsh_walk_arg structure
   *
   * Description:
   * This function is designed to be used to generate a response for a
   * STATICLIST or STATICLISTDEF message.  When called either @addr4 or @addr6
   * can be specified, not both, the other unspecified entry should be set to
   * NULL by the caller.  Returns the size of the message on success, negative
   * values on failure.
   *
   */
  static int netlbl_unlabel_staticlist_gen(u32 cmd,
  				       const struct netlbl_unlhsh_iface *iface,
  				       const struct netlbl_unlhsh_addr4 *addr4,
  				       const struct netlbl_unlhsh_addr6 *addr6,
  				       void *arg)
  {
  	int ret_val = -ENOMEM;
  	struct netlbl_unlhsh_walk_arg *cb_arg = arg;
  	struct net_device *dev;
  	void *data;
  	u32 secid;
  	char *secctx;
  	u32 secctx_len;
  
  	data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).pid,
  			   cb_arg->seq, &netlbl_unlabel_gnl_family,
  			   NLM_F_MULTI, cmd);
  	if (data == NULL)
  		goto list_cb_failure;
  
  	if (iface->ifindex > 0) {
  		dev = dev_get_by_index(&init_net, iface->ifindex);
794eb6bf2   Jesper Juhl   [NETLABEL]: Fix N...
1076
1077
1078
1079
  		if (!dev) {
  			ret_val = -ENODEV;
  			goto list_cb_failure;
  		}
8cc44579d   Paul Moore   NetLabel: Introdu...
1080
1081
1082
1083
1084
1085
1086
1087
1088
  		ret_val = nla_put_string(cb_arg->skb,
  					 NLBL_UNLABEL_A_IFACE, dev->name);
  		dev_put(dev);
  		if (ret_val != 0)
  			goto list_cb_failure;
  	}
  
  	if (addr4) {
  		struct in_addr addr_struct;
61e106821   Paul Moore   netlabel: Add a g...
1089
  		addr_struct.s_addr = addr4->list.addr;
8cc44579d   Paul Moore   NetLabel: Introdu...
1090
1091
1092
1093
1094
1095
  		ret_val = nla_put(cb_arg->skb,
  				  NLBL_UNLABEL_A_IPV4ADDR,
  				  sizeof(struct in_addr),
  				  &addr_struct);
  		if (ret_val != 0)
  			goto list_cb_failure;
61e106821   Paul Moore   netlabel: Add a g...
1096
  		addr_struct.s_addr = addr4->list.mask;
8cc44579d   Paul Moore   NetLabel: Introdu...
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
  		ret_val = nla_put(cb_arg->skb,
  				  NLBL_UNLABEL_A_IPV4MASK,
  				  sizeof(struct in_addr),
  				  &addr_struct);
  		if (ret_val != 0)
  			goto list_cb_failure;
  
  		secid = addr4->secid;
  	} else {
  		ret_val = nla_put(cb_arg->skb,
  				  NLBL_UNLABEL_A_IPV6ADDR,
  				  sizeof(struct in6_addr),
61e106821   Paul Moore   netlabel: Add a g...
1109
  				  &addr6->list.addr);
8cc44579d   Paul Moore   NetLabel: Introdu...
1110
1111
1112
1113
1114
1115
  		if (ret_val != 0)
  			goto list_cb_failure;
  
  		ret_val = nla_put(cb_arg->skb,
  				  NLBL_UNLABEL_A_IPV6MASK,
  				  sizeof(struct in6_addr),
61e106821   Paul Moore   netlabel: Add a g...
1116
  				  &addr6->list.mask);
8cc44579d   Paul Moore   NetLabel: Introdu...
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
  		if (ret_val != 0)
  			goto list_cb_failure;
  
  		secid = addr6->secid;
  	}
  
  	ret_val = security_secid_to_secctx(secid, &secctx, &secctx_len);
  	if (ret_val != 0)
  		goto list_cb_failure;
  	ret_val = nla_put(cb_arg->skb,
  			  NLBL_UNLABEL_A_SECCTX,
  			  secctx_len,
  			  secctx);
  	security_release_secctx(secctx, secctx_len);
  	if (ret_val != 0)
  		goto list_cb_failure;
  
  	cb_arg->seq++;
  	return genlmsg_end(cb_arg->skb, data);
  
  list_cb_failure:
  	genlmsg_cancel(cb_arg->skb, data);
  	return ret_val;
  }
  
  /**
   * netlbl_unlabel_staticlist - Handle a STATICLIST message
   * @skb: the NETLINK buffer
   * @cb: the NETLINK callback
   *
   * Description:
   * Process a user generated STATICLIST message and dump the unlabeled
   * connection hash table in a form suitable for use in a kernel generated
   * STATICLIST message.  Returns the length of @skb.
   *
   */
  static int netlbl_unlabel_staticlist(struct sk_buff *skb,
  				     struct netlink_callback *cb)
  {
  	struct netlbl_unlhsh_walk_arg cb_arg;
  	u32 skip_bkt = cb->args[0];
  	u32 skip_chain = cb->args[1];
  	u32 skip_addr4 = cb->args[2];
  	u32 skip_addr6 = cb->args[3];
  	u32 iter_bkt;
  	u32 iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0;
  	struct netlbl_unlhsh_iface *iface;
561967010   Paul Moore   netlabel: Fix som...
1164
  	struct list_head *iter_list;
61e106821   Paul Moore   netlabel: Add a g...
1165
  	struct netlbl_af4list *addr4;
dfd56b8b3   Eric Dumazet   net: use IS_ENABL...
1166
  #if IS_ENABLED(CONFIG_IPV6)
61e106821   Paul Moore   netlabel: Add a g...
1167
1168
  	struct netlbl_af6list *addr6;
  #endif
8cc44579d   Paul Moore   NetLabel: Introdu...
1169
1170
1171
1172
1173
1174
1175
1176
1177
  
  	cb_arg.nl_cb = cb;
  	cb_arg.skb = skb;
  	cb_arg.seq = cb->nlh->nlmsg_seq;
  
  	rcu_read_lock();
  	for (iter_bkt = skip_bkt;
  	     iter_bkt < rcu_dereference(netlbl_unlhsh)->size;
  	     iter_bkt++, iter_chain = 0, iter_addr4 = 0, iter_addr6 = 0) {
561967010   Paul Moore   netlabel: Fix som...
1178
1179
  		iter_list = &rcu_dereference(netlbl_unlhsh)->tbl[iter_bkt];
  		list_for_each_entry_rcu(iface, iter_list, list) {
8cc44579d   Paul Moore   NetLabel: Introdu...
1180
1181
1182
  			if (!iface->valid ||
  			    iter_chain++ < skip_chain)
  				continue;
61e106821   Paul Moore   netlabel: Add a g...
1183
1184
1185
  			netlbl_af4list_foreach_rcu(addr4,
  						   &iface->addr4_list) {
  				if (iter_addr4++ < skip_addr4)
8cc44579d   Paul Moore   NetLabel: Introdu...
1186
1187
  					continue;
  				if (netlbl_unlabel_staticlist_gen(
61e106821   Paul Moore   netlabel: Add a g...
1188
1189
1190
1191
1192
  					      NLBL_UNLABEL_C_STATICLIST,
  					      iface,
  					      netlbl_unlhsh_addr4_entry(addr4),
  					      NULL,
  					      &cb_arg) < 0) {
8cc44579d   Paul Moore   NetLabel: Introdu...
1193
1194
1195
1196
1197
  					iter_addr4--;
  					iter_chain--;
  					goto unlabel_staticlist_return;
  				}
  			}
dfd56b8b3   Eric Dumazet   net: use IS_ENABL...
1198
  #if IS_ENABLED(CONFIG_IPV6)
61e106821   Paul Moore   netlabel: Add a g...
1199
1200
1201
  			netlbl_af6list_foreach_rcu(addr6,
  						   &iface->addr6_list) {
  				if (iter_addr6++ < skip_addr6)
8cc44579d   Paul Moore   NetLabel: Introdu...
1202
1203
  					continue;
  				if (netlbl_unlabel_staticlist_gen(
61e106821   Paul Moore   netlabel: Add a g...
1204
1205
1206
1207
1208
  					      NLBL_UNLABEL_C_STATICLIST,
  					      iface,
  					      NULL,
  					      netlbl_unlhsh_addr6_entry(addr6),
  					      &cb_arg) < 0) {
8cc44579d   Paul Moore   NetLabel: Introdu...
1209
1210
1211
1212
1213
  					iter_addr6--;
  					iter_chain--;
  					goto unlabel_staticlist_return;
  				}
  			}
61e106821   Paul Moore   netlabel: Add a g...
1214
  #endif /* IPv6 */
8cc44579d   Paul Moore   NetLabel: Introdu...
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
  		}
  	}
  
  unlabel_staticlist_return:
  	rcu_read_unlock();
  	cb->args[0] = skip_bkt;
  	cb->args[1] = skip_chain;
  	cb->args[2] = skip_addr4;
  	cb->args[3] = skip_addr6;
  	return skb->len;
  }
  
  /**
   * netlbl_unlabel_staticlistdef - Handle a STATICLISTDEF message
   * @skb: the NETLINK buffer
   * @cb: the NETLINK callback
   *
   * Description:
   * Process a user generated STATICLISTDEF message and dump the default
   * unlabeled connection entry in a form suitable for use in a kernel generated
   * STATICLISTDEF message.  Returns the length of @skb.
   *
   */
  static int netlbl_unlabel_staticlistdef(struct sk_buff *skb,
  					struct netlink_callback *cb)
  {
  	struct netlbl_unlhsh_walk_arg cb_arg;
  	struct netlbl_unlhsh_iface *iface;
  	u32 skip_addr4 = cb->args[0];
  	u32 skip_addr6 = cb->args[1];
61e106821   Paul Moore   netlabel: Add a g...
1245
1246
  	u32 iter_addr4 = 0;
  	struct netlbl_af4list *addr4;
dfd56b8b3   Eric Dumazet   net: use IS_ENABL...
1247
  #if IS_ENABLED(CONFIG_IPV6)
61e106821   Paul Moore   netlabel: Add a g...
1248
1249
1250
  	u32 iter_addr6 = 0;
  	struct netlbl_af6list *addr6;
  #endif
8cc44579d   Paul Moore   NetLabel: Introdu...
1251
1252
1253
1254
1255
1256
1257
1258
1259
  
  	cb_arg.nl_cb = cb;
  	cb_arg.skb = skb;
  	cb_arg.seq = cb->nlh->nlmsg_seq;
  
  	rcu_read_lock();
  	iface = rcu_dereference(netlbl_unlhsh_def);
  	if (iface == NULL || !iface->valid)
  		goto unlabel_staticlistdef_return;
61e106821   Paul Moore   netlabel: Add a g...
1260
1261
  	netlbl_af4list_foreach_rcu(addr4, &iface->addr4_list) {
  		if (iter_addr4++ < skip_addr4)
8cc44579d   Paul Moore   NetLabel: Introdu...
1262
1263
  			continue;
  		if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
61e106821   Paul Moore   netlabel: Add a g...
1264
1265
1266
1267
  					      iface,
  					      netlbl_unlhsh_addr4_entry(addr4),
  					      NULL,
  					      &cb_arg) < 0) {
8cc44579d   Paul Moore   NetLabel: Introdu...
1268
1269
1270
1271
  			iter_addr4--;
  			goto unlabel_staticlistdef_return;
  		}
  	}
dfd56b8b3   Eric Dumazet   net: use IS_ENABL...
1272
  #if IS_ENABLED(CONFIG_IPV6)
61e106821   Paul Moore   netlabel: Add a g...
1273
1274
  	netlbl_af6list_foreach_rcu(addr6, &iface->addr6_list) {
  		if (iter_addr6++ < skip_addr6)
8cc44579d   Paul Moore   NetLabel: Introdu...
1275
1276
  			continue;
  		if (netlbl_unlabel_staticlist_gen(NLBL_UNLABEL_C_STATICLISTDEF,
61e106821   Paul Moore   netlabel: Add a g...
1277
1278
1279
1280
  					      iface,
  					      NULL,
  					      netlbl_unlhsh_addr6_entry(addr6),
  					      &cb_arg) < 0) {
8cc44579d   Paul Moore   NetLabel: Introdu...
1281
1282
1283
1284
  			iter_addr6--;
  			goto unlabel_staticlistdef_return;
  		}
  	}
61e106821   Paul Moore   netlabel: Add a g...
1285
  #endif /* IPv6 */
8cc44579d   Paul Moore   NetLabel: Introdu...
1286
1287
1288
1289
1290
1291
1292
  
  unlabel_staticlistdef_return:
  	rcu_read_unlock();
  	cb->args[0] = skip_addr4;
  	cb->args[1] = skip_addr6;
  	return skb->len;
  }
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
1293
1294
1295
1296
  
  /*
   * NetLabel Generic NETLINK Command Definitions
   */
227c43c3b   Pavel Emelyanov   [NETLABEL]: Shrin...
1297
1298
  static struct genl_ops netlbl_unlabel_genl_ops[] = {
  	{
8cc44579d   Paul Moore   NetLabel: Introdu...
1299
1300
1301
1302
1303
  	.cmd = NLBL_UNLABEL_C_STATICADD,
  	.flags = GENL_ADMIN_PERM,
  	.policy = netlbl_unlabel_genl_policy,
  	.doit = netlbl_unlabel_staticadd,
  	.dumpit = NULL,
227c43c3b   Pavel Emelyanov   [NETLABEL]: Shrin...
1304
1305
  	},
  	{
8cc44579d   Paul Moore   NetLabel: Introdu...
1306
1307
1308
1309
1310
  	.cmd = NLBL_UNLABEL_C_STATICREMOVE,
  	.flags = GENL_ADMIN_PERM,
  	.policy = netlbl_unlabel_genl_policy,
  	.doit = netlbl_unlabel_staticremove,
  	.dumpit = NULL,
227c43c3b   Pavel Emelyanov   [NETLABEL]: Shrin...
1311
1312
  	},
  	{
8cc44579d   Paul Moore   NetLabel: Introdu...
1313
1314
1315
1316
1317
  	.cmd = NLBL_UNLABEL_C_STATICLIST,
  	.flags = 0,
  	.policy = netlbl_unlabel_genl_policy,
  	.doit = NULL,
  	.dumpit = netlbl_unlabel_staticlist,
227c43c3b   Pavel Emelyanov   [NETLABEL]: Shrin...
1318
1319
  	},
  	{
8cc44579d   Paul Moore   NetLabel: Introdu...
1320
1321
1322
1323
1324
  	.cmd = NLBL_UNLABEL_C_STATICADDDEF,
  	.flags = GENL_ADMIN_PERM,
  	.policy = netlbl_unlabel_genl_policy,
  	.doit = netlbl_unlabel_staticadddef,
  	.dumpit = NULL,
227c43c3b   Pavel Emelyanov   [NETLABEL]: Shrin...
1325
1326
  	},
  	{
8cc44579d   Paul Moore   NetLabel: Introdu...
1327
1328
1329
1330
1331
  	.cmd = NLBL_UNLABEL_C_STATICREMOVEDEF,
  	.flags = GENL_ADMIN_PERM,
  	.policy = netlbl_unlabel_genl_policy,
  	.doit = netlbl_unlabel_staticremovedef,
  	.dumpit = NULL,
227c43c3b   Pavel Emelyanov   [NETLABEL]: Shrin...
1332
1333
  	},
  	{
8cc44579d   Paul Moore   NetLabel: Introdu...
1334
1335
1336
1337
1338
  	.cmd = NLBL_UNLABEL_C_STATICLISTDEF,
  	.flags = 0,
  	.policy = netlbl_unlabel_genl_policy,
  	.doit = NULL,
  	.dumpit = netlbl_unlabel_staticlistdef,
227c43c3b   Pavel Emelyanov   [NETLABEL]: Shrin...
1339
1340
  	},
  	{
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
1341
  	.cmd = NLBL_UNLABEL_C_ACCEPT,
fd3858554   Paul Moore   [NetLabel]: rewor...
1342
1343
  	.flags = GENL_ADMIN_PERM,
  	.policy = netlbl_unlabel_genl_policy,
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
1344
1345
  	.doit = netlbl_unlabel_accept,
  	.dumpit = NULL,
227c43c3b   Pavel Emelyanov   [NETLABEL]: Shrin...
1346
1347
  	},
  	{
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
1348
1349
  	.cmd = NLBL_UNLABEL_C_LIST,
  	.flags = 0,
fd3858554   Paul Moore   [NetLabel]: rewor...
1350
  	.policy = netlbl_unlabel_genl_policy,
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
1351
1352
  	.doit = netlbl_unlabel_list,
  	.dumpit = NULL,
227c43c3b   Pavel Emelyanov   [NETLABEL]: Shrin...
1353
  	},
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
1354
  };
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
  /*
   * NetLabel Generic NETLINK Protocol Functions
   */
  
  /**
   * netlbl_unlabel_genl_init - Register the Unlabeled NetLabel component
   *
   * Description:
   * Register the unlabeled packet NetLabel component with the Generic NETLINK
   * mechanism.  Returns zero on success, negative values on failure.
   *
   */
05705e4e1   Pavel Emelyanov   [NETLABEL]: Move ...
1367
  int __init netlbl_unlabel_genl_init(void)
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
1368
  {
7ae740df3   Michał Mirosław   netlabel: Use gen...
1369
1370
  	return genl_register_family_with_ops(&netlbl_unlabel_gnl_family,
  		netlbl_unlabel_genl_ops, ARRAY_SIZE(netlbl_unlabel_genl_ops));
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
1371
1372
1373
1374
1375
  }
  
  /*
   * NetLabel KAPI Hooks
   */
8cc44579d   Paul Moore   NetLabel: Introdu...
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
  static struct notifier_block netlbl_unlhsh_netdev_notifier = {
  	.notifier_call = netlbl_unlhsh_netdev_handler,
  };
  
  /**
   * netlbl_unlabel_init - Initialize the unlabeled connection hash table
   * @size: the number of bits to use for the hash buckets
   *
   * Description:
   * Initializes the unlabeled connection hash table and registers a network
   * device notification handler.  This function should only be called by the
   * NetLabel subsystem itself during initialization.  Returns zero on success,
   * non-zero values on error.
   *
   */
05705e4e1   Pavel Emelyanov   [NETLABEL]: Move ...
1391
  int __init netlbl_unlabel_init(u32 size)
8cc44579d   Paul Moore   NetLabel: Introdu...
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
  {
  	u32 iter;
  	struct netlbl_unlhsh_tbl *hsh_tbl;
  
  	if (size == 0)
  		return -EINVAL;
  
  	hsh_tbl = kmalloc(sizeof(*hsh_tbl), GFP_KERNEL);
  	if (hsh_tbl == NULL)
  		return -ENOMEM;
  	hsh_tbl->size = 1 << size;
  	hsh_tbl->tbl = kcalloc(hsh_tbl->size,
  			       sizeof(struct list_head),
  			       GFP_KERNEL);
  	if (hsh_tbl->tbl == NULL) {
  		kfree(hsh_tbl);
  		return -ENOMEM;
  	}
  	for (iter = 0; iter < hsh_tbl->size; iter++)
  		INIT_LIST_HEAD(&hsh_tbl->tbl[iter]);
8cc44579d   Paul Moore   NetLabel: Introdu...
1412
  	spin_lock(&netlbl_unlhsh_lock);
cf778b00e   Eric Dumazet   net: reintroduce ...
1413
  	rcu_assign_pointer(netlbl_unlhsh, hsh_tbl);
8cc44579d   Paul Moore   NetLabel: Introdu...
1414
  	spin_unlock(&netlbl_unlhsh_lock);
8cc44579d   Paul Moore   NetLabel: Introdu...
1415
1416
1417
1418
1419
  
  	register_netdevice_notifier(&netlbl_unlhsh_netdev_notifier);
  
  	return 0;
  }
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
1420
1421
  /**
   * netlbl_unlabel_getattr - Get the security attributes for an unlabled packet
8cc44579d   Paul Moore   NetLabel: Introdu...
1422
1423
   * @skb: the packet
   * @family: protocol family
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
1424
1425
1426
1427
1428
1429
1430
   * @secattr: the security attributes
   *
   * Description:
   * Determine the security attributes, if any, for an unlabled packet and return
   * them in @secattr.  Returns zero on success and negative values on failure.
   *
   */
8cc44579d   Paul Moore   NetLabel: Introdu...
1431
1432
1433
  int netlbl_unlabel_getattr(const struct sk_buff *skb,
  			   u16 family,
  			   struct netlbl_lsm_secattr *secattr)
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
1434
  {
8cc44579d   Paul Moore   NetLabel: Introdu...
1435
1436
1437
  	struct netlbl_unlhsh_iface *iface;
  
  	rcu_read_lock();
b914f3a2a   Paul Moore   netlabel: Fix sev...
1438
  	iface = netlbl_unlhsh_search_iface(skb->skb_iif);
8cc44579d   Paul Moore   NetLabel: Introdu...
1439
  	if (iface == NULL)
b914f3a2a   Paul Moore   netlabel: Fix sev...
1440
1441
  		iface = rcu_dereference(netlbl_unlhsh_def);
  	if (iface == NULL || !iface->valid)
8cc44579d   Paul Moore   NetLabel: Introdu...
1442
1443
  		goto unlabel_getattr_nolabel;
  	switch (family) {
56628b1d8   Pavel Emelyanov   [NETLABEL]: Don't...
1444
1445
  	case PF_INET: {
  		struct iphdr *hdr4;
61e106821   Paul Moore   netlabel: Add a g...
1446
  		struct netlbl_af4list *addr4;
56628b1d8   Pavel Emelyanov   [NETLABEL]: Don't...
1447

8cc44579d   Paul Moore   NetLabel: Introdu...
1448
  		hdr4 = ip_hdr(skb);
61e106821   Paul Moore   netlabel: Add a g...
1449
1450
  		addr4 = netlbl_af4list_search(hdr4->saddr,
  					      &iface->addr4_list);
8cc44579d   Paul Moore   NetLabel: Introdu...
1451
1452
  		if (addr4 == NULL)
  			goto unlabel_getattr_nolabel;
61e106821   Paul Moore   netlabel: Add a g...
1453
  		secattr->attr.secid = netlbl_unlhsh_addr4_entry(addr4)->secid;
8cc44579d   Paul Moore   NetLabel: Introdu...
1454
  		break;
56628b1d8   Pavel Emelyanov   [NETLABEL]: Don't...
1455
  	}
dfd56b8b3   Eric Dumazet   net: use IS_ENABL...
1456
  #if IS_ENABLED(CONFIG_IPV6)
56628b1d8   Pavel Emelyanov   [NETLABEL]: Don't...
1457
1458
  	case PF_INET6: {
  		struct ipv6hdr *hdr6;
61e106821   Paul Moore   netlabel: Add a g...
1459
  		struct netlbl_af6list *addr6;
56628b1d8   Pavel Emelyanov   [NETLABEL]: Don't...
1460

8cc44579d   Paul Moore   NetLabel: Introdu...
1461
  		hdr6 = ipv6_hdr(skb);
61e106821   Paul Moore   netlabel: Add a g...
1462
1463
  		addr6 = netlbl_af6list_search(&hdr6->saddr,
  					      &iface->addr6_list);
8cc44579d   Paul Moore   NetLabel: Introdu...
1464
1465
  		if (addr6 == NULL)
  			goto unlabel_getattr_nolabel;
61e106821   Paul Moore   netlabel: Add a g...
1466
  		secattr->attr.secid = netlbl_unlhsh_addr6_entry(addr6)->secid;
8cc44579d   Paul Moore   NetLabel: Introdu...
1467
  		break;
56628b1d8   Pavel Emelyanov   [NETLABEL]: Don't...
1468
  	}
8cc44579d   Paul Moore   NetLabel: Introdu...
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
  #endif /* IPv6 */
  	default:
  		goto unlabel_getattr_nolabel;
  	}
  	rcu_read_unlock();
  
  	secattr->flags |= NETLBL_SECATTR_SECID;
  	secattr->type = NETLBL_NLTYPE_UNLABELED;
  	return 0;
  
  unlabel_getattr_nolabel:
  	rcu_read_unlock();
c783f1ce5   Paul Moore   NetLabel: Remove ...
1481
1482
  	if (netlabel_unlabel_acceptflg == 0)
  		return -ENOMSG;
16efd4543   Paul Moore   NetLabel: Add sec...
1483
  	secattr->type = NETLBL_NLTYPE_UNLABELED;
c783f1ce5   Paul Moore   NetLabel: Remove ...
1484
  	return 0;
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
  }
  
  /**
   * netlbl_unlabel_defconf - Set the default config to allow unlabeled packets
   *
   * Description:
   * Set the default NetLabel configuration to allow incoming unlabeled packets
   * and to send unlabeled network traffic by default.
   *
   */
05705e4e1   Pavel Emelyanov   [NETLABEL]: Move ...
1495
  int __init netlbl_unlabel_defconf(void)
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
1496
1497
1498
  {
  	int ret_val;
  	struct netlbl_dom_map *entry;
95d4e6be2   Paul Moore   [NetLabel]: audit...
1499
  	struct netlbl_audit audit_info;
32f50cdee   Paul Moore   [NetLabel]: add a...
1500

95d4e6be2   Paul Moore   [NetLabel]: audit...
1501
1502
1503
1504
1505
  	/* Only the kernel is allowed to call this function and the only time
  	 * it is called is at bootup before the audit subsystem is reporting
  	 * messages so don't worry to much about these values. */
  	security_task_getsecid(current, &audit_info.secid);
  	audit_info.loginuid = 0;
2532386f4   Eric Paris   Audit: collect se...
1506
  	audit_info.sessionid = 0;
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
1507
1508
1509
1510
1511
  
  	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
  	if (entry == NULL)
  		return -ENOMEM;
  	entry->type = NETLBL_NLTYPE_UNLABELED;
95d4e6be2   Paul Moore   [NetLabel]: audit...
1512
  	ret_val = netlbl_domhsh_add_default(entry, &audit_info);
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
1513
1514
  	if (ret_val != 0)
  		return ret_val;
95d4e6be2   Paul Moore   [NetLabel]: audit...
1515
  	netlbl_unlabel_acceptflg_set(1, &audit_info);
96cb8e331   Paul Moore   [NetLabel]: CIPSO...
1516
1517
1518
  
  	return 0;
  }