blob: 00bab51c291e8116d208eb84ed45155dbaf03430 [file] [log] [blame]
Paul Moored15c3452006-08-03 16:48:37 -07001/*
2 * NetLabel Kernel API
3 *
4 * This file defines the kernel API for the NetLabel system. The NetLabel
5 * system manages static and dynamic label mappings for network protocols such
6 * as CIPSO and RIPSO.
7 *
Paul Moore82c21bf2011-08-01 11:10:33 +00008 * Author: Paul Moore <paul@paul-moore.com>
Paul Moored15c3452006-08-03 16:48:37 -07009 *
10 */
11
12/*
Paul Moore014ab192008-10-10 10:16:33 -040013 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
Paul Moored15c3452006-08-03 16:48:37 -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
Jeff Kirsherd484ff12013-12-06 09:13:41 -080026 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Paul Moored15c3452006-08-03 16:48:37 -070027 *
28 */
29
30#include <linux/init.h>
31#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Paul Mooreeda61d32008-02-04 22:29:47 -080033#include <linux/audit.h>
Paul Moore6c2e8ac2008-12-31 12:54:11 -050034#include <linux/in.h>
35#include <linux/in6.h>
Paul Moored15c3452006-08-03 16:48:37 -070036#include <net/ip.h>
Paul Moore6c2e8ac2008-12-31 12:54:11 -050037#include <net/ipv6.h>
Paul Moored15c3452006-08-03 16:48:37 -070038#include <net/netlabel.h>
39#include <net/cipso_ipv4.h>
Huw Daviesceba1832016-06-27 15:02:51 -040040#include <net/calipso.h>
Paul Moored15c3452006-08-03 16:48:37 -070041#include <asm/bug.h>
Arun Sharma600634972011-07-26 16:09:06 -070042#include <linux/atomic.h>
Paul Moored15c3452006-08-03 16:48:37 -070043
44#include "netlabel_domainhash.h"
45#include "netlabel_unlabeled.h"
Paul Mooreeda61d32008-02-04 22:29:47 -080046#include "netlabel_cipso_v4.h"
Huw Daviesceba1832016-06-27 15:02:51 -040047#include "netlabel_calipso.h"
Paul Moored15c3452006-08-03 16:48:37 -070048#include "netlabel_user.h"
Paul Moore23bcdc12007-07-18 12:28:45 -040049#include "netlabel_mgmt.h"
Paul Moore6c2e8ac2008-12-31 12:54:11 -050050#include "netlabel_addrlist.h"
Paul Moored15c3452006-08-03 16:48:37 -070051
52/*
Paul Mooreeda61d32008-02-04 22:29:47 -080053 * Configuration Functions
54 */
55
56/**
57 * netlbl_cfg_map_del - Remove a NetLabel/LSM domain mapping
58 * @domain: the domain mapping to remove
Paul Moore6c2e8ac2008-12-31 12:54:11 -050059 * @family: address family
60 * @addr: IP address
61 * @mask: IP address mask
Paul Mooreeda61d32008-02-04 22:29:47 -080062 * @audit_info: NetLabel audit information
63 *
64 * Description:
65 * Removes a NetLabel/LSM domain mapping. A @domain value of NULL causes the
66 * default domain mapping to be removed. Returns zero on success, negative
67 * values on failure.
68 *
69 */
Paul Moore6c2e8ac2008-12-31 12:54:11 -050070int netlbl_cfg_map_del(const char *domain,
71 u16 family,
72 const void *addr,
73 const void *mask,
74 struct netlbl_audit *audit_info)
Paul Mooreeda61d32008-02-04 22:29:47 -080075{
Paul Moore6c2e8ac2008-12-31 12:54:11 -050076 if (addr == NULL && mask == NULL) {
Huw Davies8f18e672016-06-27 15:02:46 -040077 return netlbl_domhsh_remove(domain, family, audit_info);
Paul Moore6c2e8ac2008-12-31 12:54:11 -050078 } else if (addr != NULL && mask != NULL) {
79 switch (family) {
80 case AF_INET:
81 return netlbl_domhsh_remove_af4(domain, addr, mask,
82 audit_info);
83 default:
84 return -EPFNOSUPPORT;
85 }
86 } else
87 return -EINVAL;
Paul Mooreeda61d32008-02-04 22:29:47 -080088}
89
90/**
Paul Moore6c2e8ac2008-12-31 12:54:11 -050091 * netlbl_cfg_unlbl_map_add - Add a new unlabeled mapping
Paul Mooreeda61d32008-02-04 22:29:47 -080092 * @domain: the domain mapping to add
Paul Moore6c2e8ac2008-12-31 12:54:11 -050093 * @family: address family
94 * @addr: IP address
95 * @mask: IP address mask
Paul Mooreeda61d32008-02-04 22:29:47 -080096 * @audit_info: NetLabel audit information
97 *
98 * Description:
99 * Adds a new unlabeled NetLabel/LSM domain mapping. A @domain value of NULL
100 * causes a new default domain mapping to be added. Returns zero on success,
101 * negative values on failure.
102 *
103 */
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500104int netlbl_cfg_unlbl_map_add(const char *domain,
105 u16 family,
106 const void *addr,
107 const void *mask,
Paul Mooreeda61d32008-02-04 22:29:47 -0800108 struct netlbl_audit *audit_info)
109{
110 int ret_val = -ENOMEM;
111 struct netlbl_dom_map *entry;
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500112 struct netlbl_domaddr_map *addrmap = NULL;
113 struct netlbl_domaddr4_map *map4 = NULL;
114 struct netlbl_domaddr6_map *map6 = NULL;
Paul Mooreeda61d32008-02-04 22:29:47 -0800115
116 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
117 if (entry == NULL)
Paul Moore948a7242008-10-10 10:16:30 -0400118 return -ENOMEM;
Paul Mooreeda61d32008-02-04 22:29:47 -0800119 if (domain != NULL) {
120 entry->domain = kstrdup(domain, GFP_ATOMIC);
121 if (entry->domain == NULL)
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500122 goto cfg_unlbl_map_add_failure;
Paul Mooreeda61d32008-02-04 22:29:47 -0800123 }
Huw Davies8f18e672016-06-27 15:02:46 -0400124 entry->family = family;
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500125
126 if (addr == NULL && mask == NULL)
Paul Moore6a8b7f02013-08-02 14:45:08 -0400127 entry->def.type = NETLBL_NLTYPE_UNLABELED;
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500128 else if (addr != NULL && mask != NULL) {
129 addrmap = kzalloc(sizeof(*addrmap), GFP_ATOMIC);
130 if (addrmap == NULL)
131 goto cfg_unlbl_map_add_failure;
132 INIT_LIST_HEAD(&addrmap->list4);
133 INIT_LIST_HEAD(&addrmap->list6);
134
135 switch (family) {
Paul Moore1281bc22011-11-29 10:10:54 +0000136 case AF_INET: {
137 const struct in_addr *addr4 = addr;
138 const struct in_addr *mask4 = mask;
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500139 map4 = kzalloc(sizeof(*map4), GFP_ATOMIC);
140 if (map4 == NULL)
141 goto cfg_unlbl_map_add_failure;
Paul Moore6a8b7f02013-08-02 14:45:08 -0400142 map4->def.type = NETLBL_NLTYPE_UNLABELED;
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500143 map4->list.addr = addr4->s_addr & mask4->s_addr;
144 map4->list.mask = mask4->s_addr;
145 map4->list.valid = 1;
146 ret_val = netlbl_af4list_add(&map4->list,
147 &addrmap->list4);
148 if (ret_val != 0)
149 goto cfg_unlbl_map_add_failure;
150 break;
Paul Moore1281bc22011-11-29 10:10:54 +0000151 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000152#if IS_ENABLED(CONFIG_IPV6)
Paul Moore1281bc22011-11-29 10:10:54 +0000153 case AF_INET6: {
154 const struct in6_addr *addr6 = addr;
155 const struct in6_addr *mask6 = mask;
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500156 map6 = kzalloc(sizeof(*map6), GFP_ATOMIC);
Julia Lawallca7daea2009-07-30 04:38:19 +0000157 if (map6 == NULL)
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500158 goto cfg_unlbl_map_add_failure;
Paul Moore6a8b7f02013-08-02 14:45:08 -0400159 map6->def.type = NETLBL_NLTYPE_UNLABELED;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000160 map6->list.addr = *addr6;
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500161 map6->list.addr.s6_addr32[0] &= mask6->s6_addr32[0];
162 map6->list.addr.s6_addr32[1] &= mask6->s6_addr32[1];
163 map6->list.addr.s6_addr32[2] &= mask6->s6_addr32[2];
164 map6->list.addr.s6_addr32[3] &= mask6->s6_addr32[3];
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000165 map6->list.mask = *mask6;
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500166 map6->list.valid = 1;
Dan Carpenter42ca0202011-11-23 21:18:20 +0000167 ret_val = netlbl_af6list_add(&map6->list,
168 &addrmap->list6);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500169 if (ret_val != 0)
170 goto cfg_unlbl_map_add_failure;
171 break;
Paul Moore1281bc22011-11-29 10:10:54 +0000172 }
173#endif /* IPv6 */
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500174 default:
175 goto cfg_unlbl_map_add_failure;
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500176 }
177
Paul Moore6a8b7f02013-08-02 14:45:08 -0400178 entry->def.addrsel = addrmap;
179 entry->def.type = NETLBL_NLTYPE_ADDRSELECT;
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500180 } else {
181 ret_val = -EINVAL;
182 goto cfg_unlbl_map_add_failure;
183 }
Paul Mooreeda61d32008-02-04 22:29:47 -0800184
185 ret_val = netlbl_domhsh_add(entry, audit_info);
186 if (ret_val != 0)
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500187 goto cfg_unlbl_map_add_failure;
Paul Mooreeda61d32008-02-04 22:29:47 -0800188
189 return 0;
190
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500191cfg_unlbl_map_add_failure:
Julia Lawall47943232009-07-27 06:15:43 +0000192 kfree(entry->domain);
Paul Mooreeda61d32008-02-04 22:29:47 -0800193 kfree(entry);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500194 kfree(addrmap);
195 kfree(map4);
196 kfree(map6);
Paul Mooreeda61d32008-02-04 22:29:47 -0800197 return ret_val;
198}
199
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500200
Paul Mooreeda61d32008-02-04 22:29:47 -0800201/**
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500202 * netlbl_cfg_unlbl_static_add - Adds a new static label
203 * @net: network namespace
204 * @dev_name: interface name
205 * @addr: IP address in network byte order (struct in[6]_addr)
206 * @mask: address mask in network byte order (struct in[6]_addr)
207 * @family: address family
208 * @secid: LSM secid value for the entry
Paul Mooreeda61d32008-02-04 22:29:47 -0800209 * @audit_info: NetLabel audit information
210 *
211 * Description:
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500212 * Adds a new NetLabel static label to be used when protocol provided labels
213 * are not present on incoming traffic. If @dev_name is NULL then the default
214 * interface will be used. Returns zero on success, negative values on failure.
Paul Mooreeda61d32008-02-04 22:29:47 -0800215 *
216 */
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500217int netlbl_cfg_unlbl_static_add(struct net *net,
218 const char *dev_name,
219 const void *addr,
220 const void *mask,
221 u16 family,
222 u32 secid,
223 struct netlbl_audit *audit_info)
224{
225 u32 addr_len;
226
227 switch (family) {
228 case AF_INET:
229 addr_len = sizeof(struct in_addr);
230 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000231#if IS_ENABLED(CONFIG_IPV6)
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500232 case AF_INET6:
233 addr_len = sizeof(struct in6_addr);
234 break;
Paul Moore1281bc22011-11-29 10:10:54 +0000235#endif /* IPv6 */
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500236 default:
237 return -EPFNOSUPPORT;
238 }
239
240 return netlbl_unlhsh_add(net,
241 dev_name, addr, mask, addr_len,
242 secid, audit_info);
243}
244
245/**
246 * netlbl_cfg_unlbl_static_del - Removes an existing static label
247 * @net: network namespace
248 * @dev_name: interface name
249 * @addr: IP address in network byte order (struct in[6]_addr)
250 * @mask: address mask in network byte order (struct in[6]_addr)
251 * @family: address family
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500252 * @audit_info: NetLabel audit information
253 *
254 * Description:
255 * Removes an existing NetLabel static label used when protocol provided labels
256 * are not present on incoming traffic. If @dev_name is NULL then the default
257 * interface will be used. Returns zero on success, negative values on failure.
258 *
259 */
260int netlbl_cfg_unlbl_static_del(struct net *net,
261 const char *dev_name,
262 const void *addr,
263 const void *mask,
264 u16 family,
265 struct netlbl_audit *audit_info)
266{
267 u32 addr_len;
268
269 switch (family) {
270 case AF_INET:
271 addr_len = sizeof(struct in_addr);
272 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000273#if IS_ENABLED(CONFIG_IPV6)
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500274 case AF_INET6:
275 addr_len = sizeof(struct in6_addr);
276 break;
Paul Moore1281bc22011-11-29 10:10:54 +0000277#endif /* IPv6 */
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500278 default:
279 return -EPFNOSUPPORT;
280 }
281
282 return netlbl_unlhsh_remove(net,
283 dev_name, addr, mask, addr_len,
284 audit_info);
285}
286
287/**
288 * netlbl_cfg_cipsov4_add - Add a new CIPSOv4 DOI definition
289 * @doi_def: CIPSO DOI definition
290 * @audit_info: NetLabel audit information
291 *
292 * Description:
293 * Add a new CIPSO DOI definition as defined by @doi_def. Returns zero on
294 * success and negative values on failure.
295 *
296 */
297int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def,
298 struct netlbl_audit *audit_info)
299{
300 return cipso_v4_doi_add(doi_def, audit_info);
301}
302
303/**
304 * netlbl_cfg_cipsov4_del - Remove an existing CIPSOv4 DOI definition
305 * @doi: CIPSO DOI
306 * @audit_info: NetLabel audit information
307 *
308 * Description:
309 * Remove an existing CIPSO DOI definition matching @doi. Returns zero on
310 * success and negative values on failure.
311 *
312 */
313void netlbl_cfg_cipsov4_del(u32 doi, struct netlbl_audit *audit_info)
314{
315 cipso_v4_doi_remove(doi, audit_info);
316}
317
318/**
319 * netlbl_cfg_cipsov4_map_add - Add a new CIPSOv4 DOI mapping
320 * @doi: the CIPSO DOI
321 * @domain: the domain mapping to add
322 * @addr: IP address
323 * @mask: IP address mask
324 * @audit_info: NetLabel audit information
325 *
326 * Description:
327 * Add a new NetLabel/LSM domain mapping for the given CIPSO DOI to the NetLabel
328 * subsystem. A @domain value of NULL adds a new default domain mapping.
329 * Returns zero on success, negative values on failure.
330 *
331 */
332int netlbl_cfg_cipsov4_map_add(u32 doi,
Paul Mooreeda61d32008-02-04 22:29:47 -0800333 const char *domain,
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500334 const struct in_addr *addr,
335 const struct in_addr *mask,
Paul Mooreeda61d32008-02-04 22:29:47 -0800336 struct netlbl_audit *audit_info)
337{
338 int ret_val = -ENOMEM;
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500339 struct cipso_v4_doi *doi_def;
Paul Mooreeda61d32008-02-04 22:29:47 -0800340 struct netlbl_dom_map *entry;
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500341 struct netlbl_domaddr_map *addrmap = NULL;
342 struct netlbl_domaddr4_map *addrinfo = NULL;
Paul Mooreeda61d32008-02-04 22:29:47 -0800343
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500344 doi_def = cipso_v4_doi_getdef(doi);
345 if (doi_def == NULL)
346 return -ENOENT;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400347
Paul Mooreeda61d32008-02-04 22:29:47 -0800348 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
349 if (entry == NULL)
Julia Lawall94a80d62011-08-11 00:06:04 +0000350 goto out_entry;
Huw Davies8f18e672016-06-27 15:02:46 -0400351 entry->family = AF_INET;
Paul Mooreeda61d32008-02-04 22:29:47 -0800352 if (domain != NULL) {
353 entry->domain = kstrdup(domain, GFP_ATOMIC);
354 if (entry->domain == NULL)
Julia Lawall94a80d62011-08-11 00:06:04 +0000355 goto out_domain;
Paul Mooreeda61d32008-02-04 22:29:47 -0800356 }
Paul Mooreeda61d32008-02-04 22:29:47 -0800357
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500358 if (addr == NULL && mask == NULL) {
Paul Moore6a8b7f02013-08-02 14:45:08 -0400359 entry->def.cipso = doi_def;
360 entry->def.type = NETLBL_NLTYPE_CIPSOV4;
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500361 } else if (addr != NULL && mask != NULL) {
362 addrmap = kzalloc(sizeof(*addrmap), GFP_ATOMIC);
363 if (addrmap == NULL)
Julia Lawall94a80d62011-08-11 00:06:04 +0000364 goto out_addrmap;
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500365 INIT_LIST_HEAD(&addrmap->list4);
366 INIT_LIST_HEAD(&addrmap->list6);
367
368 addrinfo = kzalloc(sizeof(*addrinfo), GFP_ATOMIC);
369 if (addrinfo == NULL)
Julia Lawall94a80d62011-08-11 00:06:04 +0000370 goto out_addrinfo;
Paul Moore6a8b7f02013-08-02 14:45:08 -0400371 addrinfo->def.cipso = doi_def;
372 addrinfo->def.type = NETLBL_NLTYPE_CIPSOV4;
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500373 addrinfo->list.addr = addr->s_addr & mask->s_addr;
374 addrinfo->list.mask = mask->s_addr;
375 addrinfo->list.valid = 1;
376 ret_val = netlbl_af4list_add(&addrinfo->list, &addrmap->list4);
377 if (ret_val != 0)
378 goto cfg_cipsov4_map_add_failure;
379
Paul Moore6a8b7f02013-08-02 14:45:08 -0400380 entry->def.addrsel = addrmap;
381 entry->def.type = NETLBL_NLTYPE_ADDRSELECT;
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500382 } else {
383 ret_val = -EINVAL;
Julia Lawall94a80d62011-08-11 00:06:04 +0000384 goto out_addrmap;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400385 }
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500386
Paul Mooreeda61d32008-02-04 22:29:47 -0800387 ret_val = netlbl_domhsh_add(entry, audit_info);
388 if (ret_val != 0)
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500389 goto cfg_cipsov4_map_add_failure;
Paul Mooreeda61d32008-02-04 22:29:47 -0800390
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500391 return 0;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400392
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500393cfg_cipsov4_map_add_failure:
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500394 kfree(addrinfo);
Julia Lawall94a80d62011-08-11 00:06:04 +0000395out_addrinfo:
396 kfree(addrmap);
397out_addrmap:
398 kfree(entry->domain);
399out_domain:
400 kfree(entry);
401out_entry:
402 cipso_v4_doi_putdef(doi_def);
Paul Moore6c2e8ac2008-12-31 12:54:11 -0500403 return ret_val;
Paul Mooreeda61d32008-02-04 22:29:47 -0800404}
405
Paul Mooreeda61d32008-02-04 22:29:47 -0800406/*
Paul Moore02752762006-11-29 13:18:18 -0500407 * Security Attribute Functions
408 */
409
Paul Moore4b8feff2014-08-01 11:17:17 -0400410#define _CM_F_NONE 0x00000000
411#define _CM_F_ALLOC 0x00000001
Paul Moored960a612014-08-01 11:17:29 -0400412#define _CM_F_WALK 0x00000002
Paul Moore4b8feff2014-08-01 11:17:17 -0400413
414/**
Paul Moore4fbe63d2014-08-01 11:17:37 -0400415 * _netlbl_catmap_getnode - Get a individual node from a catmap
Paul Moore4b8feff2014-08-01 11:17:17 -0400416 * @catmap: pointer to the category bitmap
417 * @offset: the requested offset
418 * @cm_flags: catmap flags, see _CM_F_*
419 * @gfp_flags: memory allocation flags
420 *
421 * Description:
Paul Moored960a612014-08-01 11:17:29 -0400422 * Iterate through the catmap looking for the node associated with @offset.
423 * If the _CM_F_ALLOC flag is set in @cm_flags and there is no associated node,
424 * one will be created and inserted into the catmap. If the _CM_F_WALK flag is
425 * set in @cm_flags and there is no associated node, the next highest node will
426 * be returned. Returns a pointer to the node on success, NULL on failure.
Paul Moore4b8feff2014-08-01 11:17:17 -0400427 *
428 */
Paul Moore4fbe63d2014-08-01 11:17:37 -0400429static struct netlbl_lsm_catmap *_netlbl_catmap_getnode(
430 struct netlbl_lsm_catmap **catmap,
431 u32 offset,
432 unsigned int cm_flags,
433 gfp_t gfp_flags)
Paul Moore4b8feff2014-08-01 11:17:17 -0400434{
Paul Moore4fbe63d2014-08-01 11:17:37 -0400435 struct netlbl_lsm_catmap *iter = *catmap;
436 struct netlbl_lsm_catmap *prev = NULL;
Paul Moore4b8feff2014-08-01 11:17:17 -0400437
Paul Moored960a612014-08-01 11:17:29 -0400438 if (iter == NULL)
Paul Moore4fbe63d2014-08-01 11:17:37 -0400439 goto catmap_getnode_alloc;
Paul Moored960a612014-08-01 11:17:29 -0400440 if (offset < iter->startbit)
Paul Moore4fbe63d2014-08-01 11:17:37 -0400441 goto catmap_getnode_walk;
Paul Moore4b8feff2014-08-01 11:17:17 -0400442 while (iter && offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
443 prev = iter;
444 iter = iter->next;
445 }
446 if (iter == NULL || offset < iter->startbit)
Paul Moore4fbe63d2014-08-01 11:17:37 -0400447 goto catmap_getnode_walk;
Paul Moore4b8feff2014-08-01 11:17:17 -0400448
449 return iter;
450
Paul Moore4fbe63d2014-08-01 11:17:37 -0400451catmap_getnode_walk:
Paul Moored960a612014-08-01 11:17:29 -0400452 if (cm_flags & _CM_F_WALK)
453 return iter;
Paul Moore4fbe63d2014-08-01 11:17:37 -0400454catmap_getnode_alloc:
Paul Moore4b8feff2014-08-01 11:17:17 -0400455 if (!(cm_flags & _CM_F_ALLOC))
456 return NULL;
457
Paul Moore4fbe63d2014-08-01 11:17:37 -0400458 iter = netlbl_catmap_alloc(gfp_flags);
Paul Moore4b8feff2014-08-01 11:17:17 -0400459 if (iter == NULL)
460 return NULL;
461 iter->startbit = offset & ~(NETLBL_CATMAP_SIZE - 1);
462
463 if (prev == NULL) {
464 iter->next = *catmap;
465 *catmap = iter;
466 } else {
467 iter->next = prev->next;
468 prev->next = iter;
469 }
470
471 return iter;
472}
473
Paul Moore02752762006-11-29 13:18:18 -0500474/**
Paul Moore4fbe63d2014-08-01 11:17:37 -0400475 * netlbl_catmap_walk - Walk a LSM secattr catmap looking for a bit
Paul Moore02752762006-11-29 13:18:18 -0500476 * @catmap: the category bitmap
477 * @offset: the offset to start searching at, in bits
478 *
479 * Description:
480 * This function walks a LSM secattr category bitmap starting at @offset and
481 * returns the spot of the first set bit or -ENOENT if no bits are set.
482 *
483 */
Paul Moore4fbe63d2014-08-01 11:17:37 -0400484int netlbl_catmap_walk(struct netlbl_lsm_catmap *catmap, u32 offset)
Paul Moore02752762006-11-29 13:18:18 -0500485{
Paul Moore4fbe63d2014-08-01 11:17:37 -0400486 struct netlbl_lsm_catmap *iter = catmap;
Paul Moored960a612014-08-01 11:17:29 -0400487 u32 idx;
488 u32 bit;
Paul Moore02752762006-11-29 13:18:18 -0500489 NETLBL_CATMAP_MAPTYPE bitmap;
490
Paul Moore4fbe63d2014-08-01 11:17:37 -0400491 iter = _netlbl_catmap_getnode(&catmap, offset, _CM_F_WALK, 0);
Paul Moored960a612014-08-01 11:17:29 -0400492 if (iter == NULL)
493 return -ENOENT;
Paul Moore02752762006-11-29 13:18:18 -0500494 if (offset > iter->startbit) {
Paul Moored960a612014-08-01 11:17:29 -0400495 offset -= iter->startbit;
496 idx = offset / NETLBL_CATMAP_MAPSIZE;
497 bit = offset % NETLBL_CATMAP_MAPSIZE;
Paul Moore02752762006-11-29 13:18:18 -0500498 } else {
Paul Moored960a612014-08-01 11:17:29 -0400499 idx = 0;
500 bit = 0;
Paul Moore02752762006-11-29 13:18:18 -0500501 }
Paul Moored960a612014-08-01 11:17:29 -0400502 bitmap = iter->bitmap[idx] >> bit;
Paul Moore02752762006-11-29 13:18:18 -0500503
504 for (;;) {
505 if (bitmap != 0) {
506 while ((bitmap & NETLBL_CATMAP_BIT) == 0) {
507 bitmap >>= 1;
Paul Moored960a612014-08-01 11:17:29 -0400508 bit++;
Paul Moore02752762006-11-29 13:18:18 -0500509 }
510 return iter->startbit +
Paul Moored960a612014-08-01 11:17:29 -0400511 (NETLBL_CATMAP_MAPSIZE * idx) + bit;
Paul Moore02752762006-11-29 13:18:18 -0500512 }
Paul Moored960a612014-08-01 11:17:29 -0400513 if (++idx >= NETLBL_CATMAP_MAPCNT) {
Paul Moore02752762006-11-29 13:18:18 -0500514 if (iter->next != NULL) {
515 iter = iter->next;
Paul Moored960a612014-08-01 11:17:29 -0400516 idx = 0;
Paul Moore02752762006-11-29 13:18:18 -0500517 } else
518 return -ENOENT;
519 }
Paul Moored960a612014-08-01 11:17:29 -0400520 bitmap = iter->bitmap[idx];
521 bit = 0;
Paul Moore02752762006-11-29 13:18:18 -0500522 }
523
524 return -ENOENT;
525}
Huw Daviesceba1832016-06-27 15:02:51 -0400526EXPORT_SYMBOL(netlbl_catmap_walk);
Paul Moore02752762006-11-29 13:18:18 -0500527
528/**
Paul Moore4fbe63d2014-08-01 11:17:37 -0400529 * netlbl_catmap_walkrng - Find the end of a string of set bits
Paul Moore02752762006-11-29 13:18:18 -0500530 * @catmap: the category bitmap
531 * @offset: the offset to start searching at, in bits
532 *
533 * Description:
534 * This function walks a LSM secattr category bitmap starting at @offset and
535 * returns the spot of the first cleared bit or -ENOENT if the offset is past
536 * the end of the bitmap.
537 *
538 */
Paul Moore4fbe63d2014-08-01 11:17:37 -0400539int netlbl_catmap_walkrng(struct netlbl_lsm_catmap *catmap, u32 offset)
Paul Moore02752762006-11-29 13:18:18 -0500540{
Paul Moore4fbe63d2014-08-01 11:17:37 -0400541 struct netlbl_lsm_catmap *iter;
542 struct netlbl_lsm_catmap *prev = NULL;
Paul Moored960a612014-08-01 11:17:29 -0400543 u32 idx;
544 u32 bit;
Paul Moore02752762006-11-29 13:18:18 -0500545 NETLBL_CATMAP_MAPTYPE bitmask;
546 NETLBL_CATMAP_MAPTYPE bitmap;
547
Paul Moore4fbe63d2014-08-01 11:17:37 -0400548 iter = _netlbl_catmap_getnode(&catmap, offset, _CM_F_WALK, 0);
Paul Moored960a612014-08-01 11:17:29 -0400549 if (iter == NULL)
550 return -ENOENT;
Paul Moore02752762006-11-29 13:18:18 -0500551 if (offset > iter->startbit) {
Paul Moored960a612014-08-01 11:17:29 -0400552 offset -= iter->startbit;
553 idx = offset / NETLBL_CATMAP_MAPSIZE;
554 bit = offset % NETLBL_CATMAP_MAPSIZE;
Paul Moore02752762006-11-29 13:18:18 -0500555 } else {
Paul Moored960a612014-08-01 11:17:29 -0400556 idx = 0;
557 bit = 0;
Paul Moore02752762006-11-29 13:18:18 -0500558 }
Paul Moored960a612014-08-01 11:17:29 -0400559 bitmask = NETLBL_CATMAP_BIT << bit;
Paul Moore02752762006-11-29 13:18:18 -0500560
561 for (;;) {
Paul Moored960a612014-08-01 11:17:29 -0400562 bitmap = iter->bitmap[idx];
Paul Moore02752762006-11-29 13:18:18 -0500563 while (bitmask != 0 && (bitmap & bitmask) != 0) {
564 bitmask <<= 1;
Paul Moored960a612014-08-01 11:17:29 -0400565 bit++;
Paul Moore02752762006-11-29 13:18:18 -0500566 }
567
Paul Moored960a612014-08-01 11:17:29 -0400568 if (prev && idx == 0 && bit == 0)
569 return prev->startbit + NETLBL_CATMAP_SIZE - 1;
570 else if (bitmask != 0)
Paul Moore02752762006-11-29 13:18:18 -0500571 return iter->startbit +
Paul Moored960a612014-08-01 11:17:29 -0400572 (NETLBL_CATMAP_MAPSIZE * idx) + bit - 1;
573 else if (++idx >= NETLBL_CATMAP_MAPCNT) {
Paul Moore02752762006-11-29 13:18:18 -0500574 if (iter->next == NULL)
Paul Moored960a612014-08-01 11:17:29 -0400575 return iter->startbit + NETLBL_CATMAP_SIZE - 1;
576 prev = iter;
Paul Moore02752762006-11-29 13:18:18 -0500577 iter = iter->next;
Paul Moored960a612014-08-01 11:17:29 -0400578 idx = 0;
Paul Moore02752762006-11-29 13:18:18 -0500579 }
580 bitmask = NETLBL_CATMAP_BIT;
Paul Moored960a612014-08-01 11:17:29 -0400581 bit = 0;
Paul Moore02752762006-11-29 13:18:18 -0500582 }
583
584 return -ENOENT;
585}
586
587/**
Paul Moore4fbe63d2014-08-01 11:17:37 -0400588 * netlbl_catmap_getlong - Export an unsigned long bitmap
Paul Moore4b8feff2014-08-01 11:17:17 -0400589 * @catmap: pointer to the category bitmap
590 * @offset: pointer to the requested offset
591 * @bitmap: the exported bitmap
592 *
593 * Description:
594 * Export a bitmap with an offset greater than or equal to @offset and return
595 * it in @bitmap. The @offset must be aligned to an unsigned long and will be
596 * updated on return if different from what was requested; if the catmap is
597 * empty at the requested offset and beyond, the @offset is set to (u32)-1.
598 * Returns zero on sucess, negative values on failure.
599 *
600 */
Paul Moore4fbe63d2014-08-01 11:17:37 -0400601int netlbl_catmap_getlong(struct netlbl_lsm_catmap *catmap,
602 u32 *offset,
603 unsigned long *bitmap)
Paul Moore4b8feff2014-08-01 11:17:17 -0400604{
Paul Moore4fbe63d2014-08-01 11:17:37 -0400605 struct netlbl_lsm_catmap *iter;
Paul Moore4b8feff2014-08-01 11:17:17 -0400606 u32 off = *offset;
607 u32 idx;
608
609 /* only allow aligned offsets */
610 if ((off & (BITS_PER_LONG - 1)) != 0)
611 return -EINVAL;
612
613 if (off < catmap->startbit) {
614 off = catmap->startbit;
615 *offset = off;
616 }
Paul Moore50b86292016-06-09 10:56:02 -0400617 iter = _netlbl_catmap_getnode(&catmap, off, _CM_F_WALK, 0);
Paul Moore4b8feff2014-08-01 11:17:17 -0400618 if (iter == NULL) {
619 *offset = (u32)-1;
620 return 0;
621 }
622
623 if (off < iter->startbit) {
Paul Moore50b86292016-06-09 10:56:02 -0400624 *offset = iter->startbit;
625 off = 0;
Paul Moore4b8feff2014-08-01 11:17:17 -0400626 } else
627 off -= iter->startbit;
Paul Moore4b8feff2014-08-01 11:17:17 -0400628 idx = off / NETLBL_CATMAP_MAPSIZE;
Paul Moore50b86292016-06-09 10:56:02 -0400629 *bitmap = iter->bitmap[idx] >> (off % NETLBL_CATMAP_MAPSIZE);
Paul Moore4b8feff2014-08-01 11:17:17 -0400630
631 return 0;
632}
633
634/**
Paul Moore4fbe63d2014-08-01 11:17:37 -0400635 * netlbl_catmap_setbit - Set a bit in a LSM secattr catmap
Paul Moore41c3bd22014-08-01 11:17:03 -0400636 * @catmap: pointer to the category bitmap
Paul Moore02752762006-11-29 13:18:18 -0500637 * @bit: the bit to set
638 * @flags: memory allocation flags
639 *
640 * Description:
641 * Set the bit specified by @bit in @catmap. Returns zero on success,
642 * negative values on failure.
643 *
644 */
Paul Moore4fbe63d2014-08-01 11:17:37 -0400645int netlbl_catmap_setbit(struct netlbl_lsm_catmap **catmap,
646 u32 bit,
647 gfp_t flags)
Paul Moore02752762006-11-29 13:18:18 -0500648{
Paul Moore4fbe63d2014-08-01 11:17:37 -0400649 struct netlbl_lsm_catmap *iter;
Paul Moore4b8feff2014-08-01 11:17:17 -0400650 u32 idx;
Paul Moore02752762006-11-29 13:18:18 -0500651
Paul Moore4fbe63d2014-08-01 11:17:37 -0400652 iter = _netlbl_catmap_getnode(catmap, bit, _CM_F_ALLOC, flags);
Paul Moore4b8feff2014-08-01 11:17:17 -0400653 if (iter == NULL)
654 return -ENOMEM;
Paul Moore02752762006-11-29 13:18:18 -0500655
Paul Moore4b8feff2014-08-01 11:17:17 -0400656 bit -= iter->startbit;
657 idx = bit / NETLBL_CATMAP_MAPSIZE;
658 iter->bitmap[idx] |= NETLBL_CATMAP_BIT << (bit % NETLBL_CATMAP_MAPSIZE);
Paul Moore02752762006-11-29 13:18:18 -0500659
660 return 0;
661}
Huw Daviesceba1832016-06-27 15:02:51 -0400662EXPORT_SYMBOL(netlbl_catmap_setbit);
Paul Moore02752762006-11-29 13:18:18 -0500663
664/**
Paul Moore4fbe63d2014-08-01 11:17:37 -0400665 * netlbl_catmap_setrng - Set a range of bits in a LSM secattr catmap
Paul Moore41c3bd22014-08-01 11:17:03 -0400666 * @catmap: pointer to the category bitmap
Paul Moore02752762006-11-29 13:18:18 -0500667 * @start: the starting bit
668 * @end: the last bit in the string
669 * @flags: memory allocation flags
670 *
671 * Description:
672 * Set a range of bits, starting at @start and ending with @end. Returns zero
673 * on success, negative values on failure.
674 *
675 */
Paul Moore4fbe63d2014-08-01 11:17:37 -0400676int netlbl_catmap_setrng(struct netlbl_lsm_catmap **catmap,
677 u32 start,
678 u32 end,
679 gfp_t flags)
Paul Moore02752762006-11-29 13:18:18 -0500680{
Paul Moore4b8feff2014-08-01 11:17:17 -0400681 int rc = 0;
682 u32 spot = start;
Paul Moore02752762006-11-29 13:18:18 -0500683
Paul Moore4b8feff2014-08-01 11:17:17 -0400684 while (rc == 0 && spot <= end) {
Janak Desai341e0cb2016-03-28 11:09:46 -0400685 if (((spot & (BITS_PER_LONG - 1)) == 0) &&
Paul Moore4b8feff2014-08-01 11:17:17 -0400686 ((end - spot) > BITS_PER_LONG)) {
Paul Moore4fbe63d2014-08-01 11:17:37 -0400687 rc = netlbl_catmap_setlong(catmap,
688 spot,
689 (unsigned long)-1,
690 flags);
Paul Moore4b8feff2014-08-01 11:17:17 -0400691 spot += BITS_PER_LONG;
692 } else
Paul Moore4fbe63d2014-08-01 11:17:37 -0400693 rc = netlbl_catmap_setbit(catmap, spot++, flags);
Paul Moore02752762006-11-29 13:18:18 -0500694 }
695
Paul Moore4b8feff2014-08-01 11:17:17 -0400696 return rc;
697}
698
699/**
Paul Moore4fbe63d2014-08-01 11:17:37 -0400700 * netlbl_catmap_setlong - Import an unsigned long bitmap
Paul Moore4b8feff2014-08-01 11:17:17 -0400701 * @catmap: pointer to the category bitmap
702 * @offset: offset to the start of the imported bitmap
703 * @bitmap: the bitmap to import
704 * @flags: memory allocation flags
705 *
706 * Description:
707 * Import the bitmap specified in @bitmap into @catmap, using the offset
708 * in @offset. The offset must be aligned to an unsigned long. Returns zero
709 * on success, negative values on failure.
710 *
711 */
Paul Moore4fbe63d2014-08-01 11:17:37 -0400712int netlbl_catmap_setlong(struct netlbl_lsm_catmap **catmap,
713 u32 offset,
714 unsigned long bitmap,
715 gfp_t flags)
Paul Moore4b8feff2014-08-01 11:17:17 -0400716{
Paul Moore4fbe63d2014-08-01 11:17:37 -0400717 struct netlbl_lsm_catmap *iter;
Paul Moore4b8feff2014-08-01 11:17:17 -0400718 u32 idx;
719
720 /* only allow aligned offsets */
721 if ((offset & (BITS_PER_LONG - 1)) != 0)
722 return -EINVAL;
723
Paul Moore4fbe63d2014-08-01 11:17:37 -0400724 iter = _netlbl_catmap_getnode(catmap, offset, _CM_F_ALLOC, flags);
Paul Moore4b8feff2014-08-01 11:17:17 -0400725 if (iter == NULL)
726 return -ENOMEM;
727
728 offset -= iter->startbit;
729 idx = offset / NETLBL_CATMAP_MAPSIZE;
730 iter->bitmap[idx] |= bitmap << (offset % NETLBL_CATMAP_MAPSIZE);
731
732 return 0;
Paul Moore02752762006-11-29 13:18:18 -0500733}
734
Huw Davies3faa8f92016-06-27 15:02:51 -0400735/* Bitmap functions
736 */
737
738/**
739 * netlbl_bitmap_walk - Walk a bitmap looking for a bit
740 * @bitmap: the bitmap
741 * @bitmap_len: length in bits
742 * @offset: starting offset
743 * @state: if non-zero, look for a set (1) bit else look for a cleared (0) bit
744 *
745 * Description:
746 * Starting at @offset, walk the bitmap from left to right until either the
747 * desired bit is found or we reach the end. Return the bit offset, -1 if
748 * not found, or -2 if error.
749 */
750int netlbl_bitmap_walk(const unsigned char *bitmap, u32 bitmap_len,
751 u32 offset, u8 state)
752{
753 u32 bit_spot;
754 u32 byte_offset;
755 unsigned char bitmask;
756 unsigned char byte;
757
758 byte_offset = offset / 8;
759 byte = bitmap[byte_offset];
760 bit_spot = offset;
761 bitmask = 0x80 >> (offset % 8);
762
763 while (bit_spot < bitmap_len) {
764 if ((state && (byte & bitmask) == bitmask) ||
765 (state == 0 && (byte & bitmask) == 0))
766 return bit_spot;
767
768 bit_spot++;
769 bitmask >>= 1;
770 if (bitmask == 0) {
771 byte = bitmap[++byte_offset];
772 bitmask = 0x80;
773 }
774 }
775
776 return -1;
777}
778EXPORT_SYMBOL(netlbl_bitmap_walk);
779
780/**
781 * netlbl_bitmap_setbit - Sets a single bit in a bitmap
782 * @bitmap: the bitmap
783 * @bit: the bit
784 * @state: if non-zero, set the bit (1) else clear the bit (0)
785 *
786 * Description:
787 * Set a single bit in the bitmask. Returns zero on success, negative values
788 * on error.
789 */
790void netlbl_bitmap_setbit(unsigned char *bitmap, u32 bit, u8 state)
791{
792 u32 byte_spot;
793 u8 bitmask;
794
795 /* gcc always rounds to zero when doing integer division */
796 byte_spot = bit / 8;
797 bitmask = 0x80 >> (bit % 8);
798 if (state)
799 bitmap[byte_spot] |= bitmask;
800 else
801 bitmap[byte_spot] &= ~bitmask;
802}
803EXPORT_SYMBOL(netlbl_bitmap_setbit);
804
Paul Moore02752762006-11-29 13:18:18 -0500805/*
Paul Moored15c3452006-08-03 16:48:37 -0700806 * LSM Functions
807 */
808
809/**
Paul Moore23bcdc12007-07-18 12:28:45 -0400810 * netlbl_enabled - Determine if the NetLabel subsystem is enabled
811 *
812 * Description:
813 * The LSM can use this function to determine if it should use NetLabel
814 * security attributes in it's enforcement mechanism. Currently, NetLabel is
815 * considered to be enabled when it's configuration contains a valid setup for
816 * at least one labeled protocol (i.e. NetLabel can understand incoming
817 * labeled packets of at least one type); otherwise NetLabel is considered to
818 * be disabled.
819 *
820 */
821int netlbl_enabled(void)
822{
823 /* At some point we probably want to expose this mechanism to the user
824 * as well so that admins can toggle NetLabel regardless of the
825 * configuration */
Paul Moorec783f1c2008-01-29 08:37:52 -0500826 return (atomic_read(&netlabel_mgmt_protocount) > 0);
Paul Moore23bcdc12007-07-18 12:28:45 -0400827}
828
829/**
Paul Moore389fb8002009-03-27 17:10:34 -0400830 * netlbl_sock_setattr - Label a socket using the correct protocol
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700831 * @sk: the socket to label
Paul Moore389fb8002009-03-27 17:10:34 -0400832 * @family: protocol family
Paul Moored15c3452006-08-03 16:48:37 -0700833 * @secattr: the security attributes
834 *
835 * Description:
836 * Attach the correct label to the given socket using the security attributes
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700837 * specified in @secattr. This function requires exclusive access to @sk,
838 * which means it either needs to be in the process of being created or locked.
Paul Moore63c41682008-10-10 10:16:32 -0400839 * Returns zero on success, -EDESTADDRREQ if the domain is configured to use
840 * network address selectors (can't blindly label the socket), and negative
841 * values on all other failures.
Paul Moored15c3452006-08-03 16:48:37 -0700842 *
843 */
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700844int netlbl_sock_setattr(struct sock *sk,
Paul Moore389fb8002009-03-27 17:10:34 -0400845 u16 family,
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700846 const struct netlbl_lsm_secattr *secattr)
Paul Moored15c3452006-08-03 16:48:37 -0700847{
Paul Moore389fb8002009-03-27 17:10:34 -0400848 int ret_val;
Paul Moored15c3452006-08-03 16:48:37 -0700849 struct netlbl_dom_map *dom_entry;
850
851 rcu_read_lock();
Huw Davies8f18e672016-06-27 15:02:46 -0400852 dom_entry = netlbl_domhsh_getentry(secattr->domain, family);
Paul Moore389fb8002009-03-27 17:10:34 -0400853 if (dom_entry == NULL) {
854 ret_val = -ENOENT;
Paul Moored15c3452006-08-03 16:48:37 -0700855 goto socket_setattr_return;
Paul Moore389fb8002009-03-27 17:10:34 -0400856 }
857 switch (family) {
858 case AF_INET:
Paul Moore6a8b7f02013-08-02 14:45:08 -0400859 switch (dom_entry->def.type) {
Paul Moore389fb8002009-03-27 17:10:34 -0400860 case NETLBL_NLTYPE_ADDRSELECT:
861 ret_val = -EDESTADDRREQ;
862 break;
863 case NETLBL_NLTYPE_CIPSOV4:
864 ret_val = cipso_v4_sock_setattr(sk,
Paul Moore6a8b7f02013-08-02 14:45:08 -0400865 dom_entry->def.cipso,
866 secattr);
Paul Moore389fb8002009-03-27 17:10:34 -0400867 break;
868 case NETLBL_NLTYPE_UNLABELED:
869 ret_val = 0;
870 break;
871 default:
872 ret_val = -ENOENT;
873 }
Paul Moore63c41682008-10-10 10:16:32 -0400874 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000875#if IS_ENABLED(CONFIG_IPV6)
Paul Moore389fb8002009-03-27 17:10:34 -0400876 case AF_INET6:
Huw Daviesceba1832016-06-27 15:02:51 -0400877 switch (dom_entry->def.type) {
878 case NETLBL_NLTYPE_ADDRSELECT:
879 ret_val = -EDESTADDRREQ;
880 break;
881 case NETLBL_NLTYPE_CALIPSO:
882 ret_val = calipso_sock_setattr(sk,
883 dom_entry->def.calipso,
884 secattr);
885 break;
886 case NETLBL_NLTYPE_UNLABELED:
887 ret_val = 0;
888 break;
889 default:
890 ret_val = -ENOENT;
891 }
Paul Moored15c3452006-08-03 16:48:37 -0700892 break;
Paul Moore389fb8002009-03-27 17:10:34 -0400893#endif /* IPv6 */
Paul Moored15c3452006-08-03 16:48:37 -0700894 default:
Paul Moore389fb8002009-03-27 17:10:34 -0400895 ret_val = -EPROTONOSUPPORT;
Paul Moored15c3452006-08-03 16:48:37 -0700896 }
897
898socket_setattr_return:
899 rcu_read_unlock();
900 return ret_val;
901}
902
903/**
Paul Moore014ab192008-10-10 10:16:33 -0400904 * netlbl_sock_delattr - Delete all the NetLabel labels on a socket
905 * @sk: the socket
906 *
907 * Description:
908 * Remove all the NetLabel labeling from @sk. The caller is responsible for
909 * ensuring that @sk is locked.
910 *
911 */
912void netlbl_sock_delattr(struct sock *sk)
913{
Paul Moore0e0e3672016-06-06 15:17:20 -0400914 switch (sk->sk_family) {
915 case AF_INET:
916 cipso_v4_sock_delattr(sk);
917 break;
Huw Daviesceba1832016-06-27 15:02:51 -0400918#if IS_ENABLED(CONFIG_IPV6)
919 case AF_INET6:
920 calipso_sock_delattr(sk);
921 break;
922#endif /* IPv6 */
Paul Moore0e0e3672016-06-06 15:17:20 -0400923 }
Paul Moore014ab192008-10-10 10:16:33 -0400924}
925
926/**
Paul Moore14a72f52006-09-25 15:52:01 -0700927 * netlbl_sock_getattr - Determine the security attributes of a sock
928 * @sk: the sock
929 * @secattr: the security attributes
930 *
931 * Description:
Paul Moore8cc44572008-01-29 08:44:21 -0500932 * Examines the given sock to see if any NetLabel style labeling has been
Paul Moore14a72f52006-09-25 15:52:01 -0700933 * applied to the sock, if so it parses the socket label and returns the
934 * security attributes in @secattr. Returns zero on success, negative values
935 * on failure.
936 *
937 */
Paul Moore389fb8002009-03-27 17:10:34 -0400938int netlbl_sock_getattr(struct sock *sk,
939 struct netlbl_lsm_secattr *secattr)
Paul Moore14a72f52006-09-25 15:52:01 -0700940{
Paul Moore389fb8002009-03-27 17:10:34 -0400941 int ret_val;
942
943 switch (sk->sk_family) {
944 case AF_INET:
945 ret_val = cipso_v4_sock_getattr(sk, secattr);
946 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000947#if IS_ENABLED(CONFIG_IPV6)
Paul Moore389fb8002009-03-27 17:10:34 -0400948 case AF_INET6:
Huw Daviesceba1832016-06-27 15:02:51 -0400949 ret_val = calipso_sock_getattr(sk, secattr);
Paul Moore389fb8002009-03-27 17:10:34 -0400950 break;
951#endif /* IPv6 */
952 default:
953 ret_val = -EPROTONOSUPPORT;
954 }
955
956 return ret_val;
Paul Moore14a72f52006-09-25 15:52:01 -0700957}
958
959/**
Paul Moore014ab192008-10-10 10:16:33 -0400960 * netlbl_conn_setattr - Label a connected socket using the correct protocol
961 * @sk: the socket to label
962 * @addr: the destination address
963 * @secattr: the security attributes
964 *
965 * Description:
966 * Attach the correct label to the given connected socket using the security
967 * attributes specified in @secattr. The caller is responsible for ensuring
968 * that @sk is locked. Returns zero on success, negative values on failure.
969 *
970 */
971int netlbl_conn_setattr(struct sock *sk,
972 struct sockaddr *addr,
973 const struct netlbl_lsm_secattr *secattr)
974{
975 int ret_val;
976 struct sockaddr_in *addr4;
Huw Daviesceba1832016-06-27 15:02:51 -0400977#if IS_ENABLED(CONFIG_IPV6)
978 struct sockaddr_in6 *addr6;
979#endif
Paul Moore6a8b7f02013-08-02 14:45:08 -0400980 struct netlbl_dommap_def *entry;
Paul Moore014ab192008-10-10 10:16:33 -0400981
982 rcu_read_lock();
983 switch (addr->sa_family) {
984 case AF_INET:
985 addr4 = (struct sockaddr_in *)addr;
Paul Moore6a8b7f02013-08-02 14:45:08 -0400986 entry = netlbl_domhsh_getentry_af4(secattr->domain,
987 addr4->sin_addr.s_addr);
988 if (entry == NULL) {
Paul Moore014ab192008-10-10 10:16:33 -0400989 ret_val = -ENOENT;
990 goto conn_setattr_return;
991 }
Paul Moore6a8b7f02013-08-02 14:45:08 -0400992 switch (entry->type) {
Paul Moore014ab192008-10-10 10:16:33 -0400993 case NETLBL_NLTYPE_CIPSOV4:
994 ret_val = cipso_v4_sock_setattr(sk,
Paul Moore6a8b7f02013-08-02 14:45:08 -0400995 entry->cipso, secattr);
Paul Moore014ab192008-10-10 10:16:33 -0400996 break;
997 case NETLBL_NLTYPE_UNLABELED:
998 /* just delete the protocols we support for right now
999 * but we could remove other protocols if needed */
Huw Daviesceba1832016-06-27 15:02:51 -04001000 netlbl_sock_delattr(sk);
Paul Moore014ab192008-10-10 10:16:33 -04001001 ret_val = 0;
1002 break;
1003 default:
1004 ret_val = -ENOENT;
1005 }
1006 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001007#if IS_ENABLED(CONFIG_IPV6)
Paul Moore014ab192008-10-10 10:16:33 -04001008 case AF_INET6:
Huw Daviesceba1832016-06-27 15:02:51 -04001009 addr6 = (struct sockaddr_in6 *)addr;
1010 entry = netlbl_domhsh_getentry_af6(secattr->domain,
1011 &addr6->sin6_addr);
1012 if (entry == NULL) {
1013 ret_val = -ENOENT;
1014 goto conn_setattr_return;
1015 }
1016 switch (entry->type) {
1017 case NETLBL_NLTYPE_CALIPSO:
1018 ret_val = calipso_sock_setattr(sk,
1019 entry->calipso, secattr);
1020 break;
1021 case NETLBL_NLTYPE_UNLABELED:
1022 /* just delete the protocols we support for right now
1023 * but we could remove other protocols if needed */
1024 netlbl_sock_delattr(sk);
1025 ret_val = 0;
1026 break;
1027 default:
1028 ret_val = -ENOENT;
1029 }
Paul Moore014ab192008-10-10 10:16:33 -04001030 break;
1031#endif /* IPv6 */
1032 default:
Paul Moore389fb8002009-03-27 17:10:34 -04001033 ret_val = -EPROTONOSUPPORT;
Paul Moore014ab192008-10-10 10:16:33 -04001034 }
1035
1036conn_setattr_return:
1037 rcu_read_unlock();
1038 return ret_val;
1039}
1040
1041/**
Paul Moore389fb8002009-03-27 17:10:34 -04001042 * netlbl_req_setattr - Label a request socket using the correct protocol
1043 * @req: the request socket to label
1044 * @secattr: the security attributes
1045 *
1046 * Description:
1047 * Attach the correct label to the given socket using the security attributes
1048 * specified in @secattr. Returns zero on success, negative values on failure.
1049 *
1050 */
1051int netlbl_req_setattr(struct request_sock *req,
1052 const struct netlbl_lsm_secattr *secattr)
1053{
1054 int ret_val;
Paul Moore6a8b7f02013-08-02 14:45:08 -04001055 struct netlbl_dommap_def *entry;
Paul Moore389fb8002009-03-27 17:10:34 -04001056
1057 rcu_read_lock();
Paul Moore389fb8002009-03-27 17:10:34 -04001058 switch (req->rsk_ops->family) {
1059 case AF_INET:
Paul Moore6a8b7f02013-08-02 14:45:08 -04001060 entry = netlbl_domhsh_getentry_af4(secattr->domain,
Eric Dumazet634fb9792013-10-09 15:21:29 -07001061 inet_rsk(req)->ir_rmt_addr);
Paul Moore6a8b7f02013-08-02 14:45:08 -04001062 if (entry == NULL) {
1063 ret_val = -ENOENT;
1064 goto req_setattr_return;
Paul Moore389fb8002009-03-27 17:10:34 -04001065 }
Paul Moore6a8b7f02013-08-02 14:45:08 -04001066 switch (entry->type) {
Paul Moore389fb8002009-03-27 17:10:34 -04001067 case NETLBL_NLTYPE_CIPSOV4:
Paul Moore6a8b7f02013-08-02 14:45:08 -04001068 ret_val = cipso_v4_req_setattr(req,
1069 entry->cipso, secattr);
Paul Moore389fb8002009-03-27 17:10:34 -04001070 break;
1071 case NETLBL_NLTYPE_UNLABELED:
1072 /* just delete the protocols we support for right now
1073 * but we could remove other protocols if needed */
1074 cipso_v4_req_delattr(req);
1075 ret_val = 0;
1076 break;
1077 default:
1078 ret_val = -ENOENT;
1079 }
1080 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001081#if IS_ENABLED(CONFIG_IPV6)
Paul Moore389fb8002009-03-27 17:10:34 -04001082 case AF_INET6:
1083 /* since we don't support any IPv6 labeling protocols right
1084 * now we can optimize everything away until we do */
1085 ret_val = 0;
1086 break;
1087#endif /* IPv6 */
1088 default:
1089 ret_val = -EPROTONOSUPPORT;
1090 }
1091
1092req_setattr_return:
1093 rcu_read_unlock();
1094 return ret_val;
1095}
1096
1097/**
Paul Moore07feee82009-03-27 17:10:54 -04001098* netlbl_req_delattr - Delete all the NetLabel labels on a socket
1099* @req: the socket
1100*
1101* Description:
1102* Remove all the NetLabel labeling from @req.
1103*
1104*/
1105void netlbl_req_delattr(struct request_sock *req)
1106{
Paul Moore0e0e3672016-06-06 15:17:20 -04001107 switch (req->rsk_ops->family) {
1108 case AF_INET:
1109 cipso_v4_req_delattr(req);
1110 break;
1111 }
Paul Moore07feee82009-03-27 17:10:54 -04001112}
1113
1114/**
Paul Moore948bf852008-10-10 10:16:32 -04001115 * netlbl_skbuff_setattr - Label a packet using the correct protocol
1116 * @skb: the packet
1117 * @family: protocol family
1118 * @secattr: the security attributes
1119 *
1120 * Description:
1121 * Attach the correct label to the given packet using the security attributes
1122 * specified in @secattr. Returns zero on success, negative values on failure.
1123 *
1124 */
1125int netlbl_skbuff_setattr(struct sk_buff *skb,
1126 u16 family,
1127 const struct netlbl_lsm_secattr *secattr)
1128{
1129 int ret_val;
1130 struct iphdr *hdr4;
Paul Moore6a8b7f02013-08-02 14:45:08 -04001131 struct netlbl_dommap_def *entry;
Paul Moore948bf852008-10-10 10:16:32 -04001132
1133 rcu_read_lock();
1134 switch (family) {
1135 case AF_INET:
1136 hdr4 = ip_hdr(skb);
Paul Moore6a8b7f02013-08-02 14:45:08 -04001137 entry = netlbl_domhsh_getentry_af4(secattr->domain,hdr4->daddr);
1138 if (entry == NULL) {
Paul Moore948bf852008-10-10 10:16:32 -04001139 ret_val = -ENOENT;
1140 goto skbuff_setattr_return;
1141 }
Paul Moore6a8b7f02013-08-02 14:45:08 -04001142 switch (entry->type) {
Paul Moore948bf852008-10-10 10:16:32 -04001143 case NETLBL_NLTYPE_CIPSOV4:
Paul Moore6a8b7f02013-08-02 14:45:08 -04001144 ret_val = cipso_v4_skbuff_setattr(skb, entry->cipso,
1145 secattr);
Paul Moore948bf852008-10-10 10:16:32 -04001146 break;
1147 case NETLBL_NLTYPE_UNLABELED:
1148 /* just delete the protocols we support for right now
1149 * but we could remove other protocols if needed */
1150 ret_val = cipso_v4_skbuff_delattr(skb);
1151 break;
1152 default:
1153 ret_val = -ENOENT;
1154 }
1155 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001156#if IS_ENABLED(CONFIG_IPV6)
Paul Moore948bf852008-10-10 10:16:32 -04001157 case AF_INET6:
1158 /* since we don't support any IPv6 labeling protocols right
1159 * now we can optimize everything away until we do */
1160 ret_val = 0;
1161 break;
1162#endif /* IPv6 */
1163 default:
Paul Moore389fb8002009-03-27 17:10:34 -04001164 ret_val = -EPROTONOSUPPORT;
Paul Moore948bf852008-10-10 10:16:32 -04001165 }
1166
1167skbuff_setattr_return:
1168 rcu_read_unlock();
1169 return ret_val;
1170}
1171
1172/**
Paul Moored15c3452006-08-03 16:48:37 -07001173 * netlbl_skbuff_getattr - Determine the security attributes of a packet
1174 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -05001175 * @family: protocol family
Paul Moored15c3452006-08-03 16:48:37 -07001176 * @secattr: the security attributes
1177 *
1178 * Description:
1179 * Examines the given packet to see if a recognized form of packet labeling
1180 * is present, if so it parses the packet label and returns the security
1181 * attributes in @secattr. Returns zero on success, negative values on
1182 * failure.
1183 *
1184 */
1185int netlbl_skbuff_getattr(const struct sk_buff *skb,
Paul Moore75e22912008-01-29 08:38:04 -05001186 u16 family,
Paul Moored15c3452006-08-03 16:48:37 -07001187 struct netlbl_lsm_secattr *secattr)
1188{
Paul Moore04f81f02015-02-11 14:46:37 -05001189 unsigned char *ptr;
1190
Paul Moore389fb8002009-03-27 17:10:34 -04001191 switch (family) {
1192 case AF_INET:
Paul Moore04f81f02015-02-11 14:46:37 -05001193 ptr = cipso_v4_optptr(skb);
1194 if (ptr && cipso_v4_getattr(ptr, secattr) == 0)
Paul Moore389fb8002009-03-27 17:10:34 -04001195 return 0;
1196 break;
Eric Dumazetdfd56b82011-12-10 09:48:31 +00001197#if IS_ENABLED(CONFIG_IPV6)
Paul Moore389fb8002009-03-27 17:10:34 -04001198 case AF_INET6:
1199 break;
1200#endif /* IPv6 */
1201 }
Paul Moored15c3452006-08-03 16:48:37 -07001202
Paul Moore8cc44572008-01-29 08:44:21 -05001203 return netlbl_unlabel_getattr(skb, family, secattr);
Paul Moored15c3452006-08-03 16:48:37 -07001204}
1205
1206/**
1207 * netlbl_skbuff_err - Handle a LSM error on a sk_buff
1208 * @skb: the packet
1209 * @error: the error code
Paul Mooredfaebe92008-10-10 10:16:31 -04001210 * @gateway: true if host is acting as a gateway, false otherwise
Paul Moored15c3452006-08-03 16:48:37 -07001211 *
1212 * Description:
1213 * Deal with a LSM problem when handling the packet in @skb, typically this is
1214 * a permission denied problem (-EACCES). The correct action is determined
1215 * according to the packet's labeling protocol.
1216 *
1217 */
Paul Mooredfaebe92008-10-10 10:16:31 -04001218void netlbl_skbuff_err(struct sk_buff *skb, int error, int gateway)
Paul Moored15c3452006-08-03 16:48:37 -07001219{
Paul Moore04f81f02015-02-11 14:46:37 -05001220 if (cipso_v4_optptr(skb))
Paul Mooredfaebe92008-10-10 10:16:31 -04001221 cipso_v4_error(skb, error, gateway);
Paul Moored15c3452006-08-03 16:48:37 -07001222}
1223
1224/**
1225 * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
1226 *
1227 * Description:
1228 * For all of the NetLabel protocols that support some form of label mapping
1229 * cache, invalidate the cache. Returns zero on success, negative values on
1230 * error.
1231 *
1232 */
1233void netlbl_cache_invalidate(void)
1234{
1235 cipso_v4_cache_invalidate();
1236}
1237
1238/**
1239 * netlbl_cache_add - Add an entry to a NetLabel protocol cache
1240 * @skb: the packet
1241 * @secattr: the packet's security attributes
1242 *
1243 * Description:
1244 * Add the LSM security attributes for the given packet to the underlying
1245 * NetLabel protocol's label mapping cache. Returns zero on success, negative
1246 * values on error.
1247 *
1248 */
1249int netlbl_cache_add(const struct sk_buff *skb,
1250 const struct netlbl_lsm_secattr *secattr)
1251{
Paul Moore04f81f02015-02-11 14:46:37 -05001252 unsigned char *ptr;
1253
Paul Moore701a90b2006-11-17 17:38:46 -05001254 if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
Paul Moored15c3452006-08-03 16:48:37 -07001255 return -ENOMSG;
1256
Paul Moore04f81f02015-02-11 14:46:37 -05001257 ptr = cipso_v4_optptr(skb);
1258 if (ptr)
1259 return cipso_v4_cache_add(ptr, secattr);
Paul Moored15c3452006-08-03 16:48:37 -07001260
1261 return -ENOMSG;
1262}
1263
1264/*
Paul Moore6c2e8ac2008-12-31 12:54:11 -05001265 * Protocol Engine Functions
1266 */
1267
1268/**
1269 * netlbl_audit_start - Start an audit message
1270 * @type: audit message type
1271 * @audit_info: NetLabel audit information
1272 *
1273 * Description:
1274 * Start an audit message using the type specified in @type and fill the audit
1275 * message with some fields common to all NetLabel audit messages. This
1276 * function should only be used by protocol engines, not LSMs. Returns a
1277 * pointer to the audit buffer on success, NULL on failure.
1278 *
1279 */
1280struct audit_buffer *netlbl_audit_start(int type,
1281 struct netlbl_audit *audit_info)
1282{
1283 return netlbl_audit_start_common(type, audit_info);
1284}
Huw Daviescb72d382016-06-27 15:02:46 -04001285EXPORT_SYMBOL(netlbl_audit_start);
Paul Moore6c2e8ac2008-12-31 12:54:11 -05001286
1287/*
Paul Moored15c3452006-08-03 16:48:37 -07001288 * Setup Functions
1289 */
1290
1291/**
1292 * netlbl_init - Initialize NetLabel
1293 *
1294 * Description:
1295 * Perform the required NetLabel initialization before first use.
1296 *
1297 */
1298static int __init netlbl_init(void)
1299{
1300 int ret_val;
1301
1302 printk(KERN_INFO "NetLabel: Initializing\n");
1303 printk(KERN_INFO "NetLabel: domain hash size = %u\n",
1304 (1 << NETLBL_DOMHSH_BITSIZE));
1305 printk(KERN_INFO "NetLabel: protocols ="
1306 " UNLABELED"
1307 " CIPSOv4"
1308 "\n");
1309
1310 ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
1311 if (ret_val != 0)
1312 goto init_failure;
1313
Paul Moore8cc44572008-01-29 08:44:21 -05001314 ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);
1315 if (ret_val != 0)
1316 goto init_failure;
1317
Paul Moored15c3452006-08-03 16:48:37 -07001318 ret_val = netlbl_netlink_init();
1319 if (ret_val != 0)
1320 goto init_failure;
1321
1322 ret_val = netlbl_unlabel_defconf();
1323 if (ret_val != 0)
1324 goto init_failure;
1325 printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
1326
1327 return 0;
1328
1329init_failure:
1330 panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
1331}
1332
1333subsys_initcall(netlbl_init);