]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/netlabel/netlabel_kapi.c
ipv6: Allow request socks to contain IPv6 options.
[mirror_ubuntu-jammy-kernel.git] / net / netlabel / netlabel_kapi.c
CommitLineData
d15c345f
PM
1/*
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 *
82c21bfa 8 * Author: Paul Moore <paul@paul-moore.com>
d15c345f
PM
9 *
10 */
11
12/*
014ab19a 13 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
d15c345f
PM
14 *
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
d484ff15 26 * along with this program; if not, see <http://www.gnu.org/licenses/>.
d15c345f
PM
27 *
28 */
29
30#include <linux/init.h>
31#include <linux/types.h>
5a0e3ad6 32#include <linux/slab.h>
eda61d32 33#include <linux/audit.h>
6c2e8ac0
PM
34#include <linux/in.h>
35#include <linux/in6.h>
d15c345f 36#include <net/ip.h>
6c2e8ac0 37#include <net/ipv6.h>
d15c345f
PM
38#include <net/netlabel.h>
39#include <net/cipso_ipv4.h>
ceba1832 40#include <net/calipso.h>
d15c345f 41#include <asm/bug.h>
60063497 42#include <linux/atomic.h>
d15c345f
PM
43
44#include "netlabel_domainhash.h"
45#include "netlabel_unlabeled.h"
eda61d32 46#include "netlabel_cipso_v4.h"
ceba1832 47#include "netlabel_calipso.h"
d15c345f 48#include "netlabel_user.h"
23bcdc1a 49#include "netlabel_mgmt.h"
6c2e8ac0 50#include "netlabel_addrlist.h"
d15c345f 51
eda61d32
PM
52/*
53 * Configuration Functions
54 */
55
56/**
57 * netlbl_cfg_map_del - Remove a NetLabel/LSM domain mapping
58 * @domain: the domain mapping to remove
6c2e8ac0
PM
59 * @family: address family
60 * @addr: IP address
61 * @mask: IP address mask
eda61d32
PM
62 * @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 */
6c2e8ac0
PM
70int netlbl_cfg_map_del(const char *domain,
71 u16 family,
72 const void *addr,
73 const void *mask,
74 struct netlbl_audit *audit_info)
eda61d32 75{
6c2e8ac0 76 if (addr == NULL && mask == NULL) {
8f18e675 77 return netlbl_domhsh_remove(domain, family, audit_info);
6c2e8ac0
PM
78 } 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;
eda61d32
PM
88}
89
90/**
6c2e8ac0 91 * netlbl_cfg_unlbl_map_add - Add a new unlabeled mapping
eda61d32 92 * @domain: the domain mapping to add
6c2e8ac0
PM
93 * @family: address family
94 * @addr: IP address
95 * @mask: IP address mask
eda61d32
PM
96 * @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 */
6c2e8ac0
PM
104int netlbl_cfg_unlbl_map_add(const char *domain,
105 u16 family,
106 const void *addr,
107 const void *mask,
eda61d32
PM
108 struct netlbl_audit *audit_info)
109{
110 int ret_val = -ENOMEM;
111 struct netlbl_dom_map *entry;
6c2e8ac0
PM
112 struct netlbl_domaddr_map *addrmap = NULL;
113 struct netlbl_domaddr4_map *map4 = NULL;
114 struct netlbl_domaddr6_map *map6 = NULL;
eda61d32
PM
115
116 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
117 if (entry == NULL)
948a7243 118 return -ENOMEM;
eda61d32
PM
119 if (domain != NULL) {
120 entry->domain = kstrdup(domain, GFP_ATOMIC);
121 if (entry->domain == NULL)
6c2e8ac0
PM
122 goto cfg_unlbl_map_add_failure;
123 }
8f18e675 124 entry->family = family;
6c2e8ac0
PM
125
126 if (addr == NULL && mask == NULL)
6a8b7f0c 127 entry->def.type = NETLBL_NLTYPE_UNLABELED;
6c2e8ac0
PM
128 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) {
1281bc25
PM
136 case AF_INET: {
137 const struct in_addr *addr4 = addr;
138 const struct in_addr *mask4 = mask;
6c2e8ac0
PM
139 map4 = kzalloc(sizeof(*map4), GFP_ATOMIC);
140 if (map4 == NULL)
141 goto cfg_unlbl_map_add_failure;
6a8b7f0c 142 map4->def.type = NETLBL_NLTYPE_UNLABELED;
6c2e8ac0
PM
143 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;
1281bc25 151 }
dfd56b8b 152#if IS_ENABLED(CONFIG_IPV6)
1281bc25
PM
153 case AF_INET6: {
154 const struct in6_addr *addr6 = addr;
155 const struct in6_addr *mask6 = mask;
6c2e8ac0 156 map6 = kzalloc(sizeof(*map6), GFP_ATOMIC);
ca7daea6 157 if (map6 == NULL)
6c2e8ac0 158 goto cfg_unlbl_map_add_failure;
6a8b7f0c 159 map6->def.type = NETLBL_NLTYPE_UNLABELED;
4e3fd7a0 160 map6->list.addr = *addr6;
6c2e8ac0
PM
161 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];
4e3fd7a0 165 map6->list.mask = *mask6;
6c2e8ac0 166 map6->list.valid = 1;
42ca0203
DC
167 ret_val = netlbl_af6list_add(&map6->list,
168 &addrmap->list6);
6c2e8ac0
PM
169 if (ret_val != 0)
170 goto cfg_unlbl_map_add_failure;
171 break;
1281bc25
PM
172 }
173#endif /* IPv6 */
6c2e8ac0
PM
174 default:
175 goto cfg_unlbl_map_add_failure;
6c2e8ac0
PM
176 }
177
6a8b7f0c
PM
178 entry->def.addrsel = addrmap;
179 entry->def.type = NETLBL_NLTYPE_ADDRSELECT;
6c2e8ac0
PM
180 } else {
181 ret_val = -EINVAL;
182 goto cfg_unlbl_map_add_failure;
eda61d32 183 }
eda61d32
PM
184
185 ret_val = netlbl_domhsh_add(entry, audit_info);
186 if (ret_val != 0)
6c2e8ac0 187 goto cfg_unlbl_map_add_failure;
eda61d32
PM
188
189 return 0;
190
6c2e8ac0 191cfg_unlbl_map_add_failure:
47943234 192 kfree(entry->domain);
eda61d32 193 kfree(entry);
6c2e8ac0
PM
194 kfree(addrmap);
195 kfree(map4);
196 kfree(map6);
eda61d32
PM
197 return ret_val;
198}
199
6c2e8ac0
PM
200
201/**
202 * 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
209 * @audit_info: NetLabel audit information
210 *
211 * Description:
212 * 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.
215 *
216 */
217int 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;
dfd56b8b 231#if IS_ENABLED(CONFIG_IPV6)
6c2e8ac0
PM
232 case AF_INET6:
233 addr_len = sizeof(struct in6_addr);
234 break;
1281bc25 235#endif /* IPv6 */
6c2e8ac0
PM
236 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
6c2e8ac0
PM
252 * @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;
dfd56b8b 273#if IS_ENABLED(CONFIG_IPV6)
6c2e8ac0
PM
274 case AF_INET6:
275 addr_len = sizeof(struct in6_addr);
276 break;
1281bc25 277#endif /* IPv6 */
6c2e8ac0
PM
278 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
eda61d32 318/**
6c2e8ac0
PM
319 * netlbl_cfg_cipsov4_map_add - Add a new CIPSOv4 DOI mapping
320 * @doi: the CIPSO DOI
eda61d32 321 * @domain: the domain mapping to add
6c2e8ac0
PM
322 * @addr: IP address
323 * @mask: IP address mask
eda61d32
PM
324 * @audit_info: NetLabel audit information
325 *
326 * Description:
6c2e8ac0
PM
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.
eda61d32
PM
330 *
331 */
6c2e8ac0 332int netlbl_cfg_cipsov4_map_add(u32 doi,
eda61d32 333 const char *domain,
6c2e8ac0
PM
334 const struct in_addr *addr,
335 const struct in_addr *mask,
eda61d32
PM
336 struct netlbl_audit *audit_info)
337{
338 int ret_val = -ENOMEM;
6c2e8ac0 339 struct cipso_v4_doi *doi_def;
eda61d32 340 struct netlbl_dom_map *entry;
6c2e8ac0
PM
341 struct netlbl_domaddr_map *addrmap = NULL;
342 struct netlbl_domaddr4_map *addrinfo = NULL;
eda61d32 343
6c2e8ac0
PM
344 doi_def = cipso_v4_doi_getdef(doi);
345 if (doi_def == NULL)
346 return -ENOENT;
b1edeb10 347
eda61d32
PM
348 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
349 if (entry == NULL)
94a80d63 350 goto out_entry;
8f18e675 351 entry->family = AF_INET;
eda61d32
PM
352 if (domain != NULL) {
353 entry->domain = kstrdup(domain, GFP_ATOMIC);
354 if (entry->domain == NULL)
94a80d63 355 goto out_domain;
eda61d32 356 }
eda61d32 357
6c2e8ac0 358 if (addr == NULL && mask == NULL) {
6a8b7f0c
PM
359 entry->def.cipso = doi_def;
360 entry->def.type = NETLBL_NLTYPE_CIPSOV4;
6c2e8ac0
PM
361 } else if (addr != NULL && mask != NULL) {
362 addrmap = kzalloc(sizeof(*addrmap), GFP_ATOMIC);
363 if (addrmap == NULL)
94a80d63 364 goto out_addrmap;
6c2e8ac0
PM
365 INIT_LIST_HEAD(&addrmap->list4);
366 INIT_LIST_HEAD(&addrmap->list6);
367
368 addrinfo = kzalloc(sizeof(*addrinfo), GFP_ATOMIC);
369 if (addrinfo == NULL)
94a80d63 370 goto out_addrinfo;
6a8b7f0c
PM
371 addrinfo->def.cipso = doi_def;
372 addrinfo->def.type = NETLBL_NLTYPE_CIPSOV4;
6c2e8ac0
PM
373 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
6a8b7f0c
PM
380 entry->def.addrsel = addrmap;
381 entry->def.type = NETLBL_NLTYPE_ADDRSELECT;
6c2e8ac0
PM
382 } else {
383 ret_val = -EINVAL;
94a80d63 384 goto out_addrmap;
b1edeb10 385 }
6c2e8ac0 386
eda61d32
PM
387 ret_val = netlbl_domhsh_add(entry, audit_info);
388 if (ret_val != 0)
6c2e8ac0 389 goto cfg_cipsov4_map_add_failure;
b1edeb10 390
6c2e8ac0 391 return 0;
b1edeb10 392
6c2e8ac0 393cfg_cipsov4_map_add_failure:
94a80d63
JL
394 kfree(addrinfo);
395out_addrinfo:
396 kfree(addrmap);
397out_addrmap:
47943234 398 kfree(entry->domain);
94a80d63 399out_domain:
eda61d32 400 kfree(entry);
94a80d63
JL
401out_entry:
402 cipso_v4_doi_putdef(doi_def);
6c2e8ac0 403 return ret_val;
eda61d32
PM
404}
405
02752760
PM
406/*
407 * Security Attribute Functions
408 */
409
4b8feff2
PM
410#define _CM_F_NONE 0x00000000
411#define _CM_F_ALLOC 0x00000001
d960a618 412#define _CM_F_WALK 0x00000002
4b8feff2
PM
413
414/**
4fbe63d1 415 * _netlbl_catmap_getnode - Get a individual node from a catmap
4b8feff2
PM
416 * @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:
d960a618
PM
422 * 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.
4b8feff2
PM
427 *
428 */
4fbe63d1
PM
429static 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)
4b8feff2 434{
4fbe63d1
PM
435 struct netlbl_lsm_catmap *iter = *catmap;
436 struct netlbl_lsm_catmap *prev = NULL;
4b8feff2 437
d960a618 438 if (iter == NULL)
4fbe63d1 439 goto catmap_getnode_alloc;
d960a618 440 if (offset < iter->startbit)
4fbe63d1 441 goto catmap_getnode_walk;
4b8feff2
PM
442 while (iter && offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
443 prev = iter;
444 iter = iter->next;
445 }
446 if (iter == NULL || offset < iter->startbit)
4fbe63d1 447 goto catmap_getnode_walk;
4b8feff2
PM
448
449 return iter;
450
4fbe63d1 451catmap_getnode_walk:
d960a618
PM
452 if (cm_flags & _CM_F_WALK)
453 return iter;
4fbe63d1 454catmap_getnode_alloc:
4b8feff2
PM
455 if (!(cm_flags & _CM_F_ALLOC))
456 return NULL;
457
4fbe63d1 458 iter = netlbl_catmap_alloc(gfp_flags);
4b8feff2
PM
459 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
02752760 474/**
4fbe63d1 475 * netlbl_catmap_walk - Walk a LSM secattr catmap looking for a bit
02752760
PM
476 * @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 */
4fbe63d1 484int netlbl_catmap_walk(struct netlbl_lsm_catmap *catmap, u32 offset)
02752760 485{
4fbe63d1 486 struct netlbl_lsm_catmap *iter = catmap;
d960a618
PM
487 u32 idx;
488 u32 bit;
02752760
PM
489 NETLBL_CATMAP_MAPTYPE bitmap;
490
4fbe63d1 491 iter = _netlbl_catmap_getnode(&catmap, offset, _CM_F_WALK, 0);
d960a618
PM
492 if (iter == NULL)
493 return -ENOENT;
02752760 494 if (offset > iter->startbit) {
d960a618
PM
495 offset -= iter->startbit;
496 idx = offset / NETLBL_CATMAP_MAPSIZE;
497 bit = offset % NETLBL_CATMAP_MAPSIZE;
02752760 498 } else {
d960a618
PM
499 idx = 0;
500 bit = 0;
02752760 501 }
d960a618 502 bitmap = iter->bitmap[idx] >> bit;
02752760
PM
503
504 for (;;) {
505 if (bitmap != 0) {
506 while ((bitmap & NETLBL_CATMAP_BIT) == 0) {
507 bitmap >>= 1;
d960a618 508 bit++;
02752760
PM
509 }
510 return iter->startbit +
d960a618 511 (NETLBL_CATMAP_MAPSIZE * idx) + bit;
02752760 512 }
d960a618 513 if (++idx >= NETLBL_CATMAP_MAPCNT) {
02752760
PM
514 if (iter->next != NULL) {
515 iter = iter->next;
d960a618 516 idx = 0;
02752760
PM
517 } else
518 return -ENOENT;
519 }
d960a618
PM
520 bitmap = iter->bitmap[idx];
521 bit = 0;
02752760
PM
522 }
523
524 return -ENOENT;
525}
ceba1832 526EXPORT_SYMBOL(netlbl_catmap_walk);
02752760
PM
527
528/**
4fbe63d1 529 * netlbl_catmap_walkrng - Find the end of a string of set bits
02752760
PM
530 * @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 */
4fbe63d1 539int netlbl_catmap_walkrng(struct netlbl_lsm_catmap *catmap, u32 offset)
02752760 540{
4fbe63d1
PM
541 struct netlbl_lsm_catmap *iter;
542 struct netlbl_lsm_catmap *prev = NULL;
d960a618
PM
543 u32 idx;
544 u32 bit;
02752760
PM
545 NETLBL_CATMAP_MAPTYPE bitmask;
546 NETLBL_CATMAP_MAPTYPE bitmap;
547
4fbe63d1 548 iter = _netlbl_catmap_getnode(&catmap, offset, _CM_F_WALK, 0);
d960a618
PM
549 if (iter == NULL)
550 return -ENOENT;
02752760 551 if (offset > iter->startbit) {
d960a618
PM
552 offset -= iter->startbit;
553 idx = offset / NETLBL_CATMAP_MAPSIZE;
554 bit = offset % NETLBL_CATMAP_MAPSIZE;
02752760 555 } else {
d960a618
PM
556 idx = 0;
557 bit = 0;
02752760 558 }
d960a618 559 bitmask = NETLBL_CATMAP_BIT << bit;
02752760
PM
560
561 for (;;) {
d960a618 562 bitmap = iter->bitmap[idx];
02752760
PM
563 while (bitmask != 0 && (bitmap & bitmask) != 0) {
564 bitmask <<= 1;
d960a618 565 bit++;
02752760
PM
566 }
567
d960a618
PM
568 if (prev && idx == 0 && bit == 0)
569 return prev->startbit + NETLBL_CATMAP_SIZE - 1;
570 else if (bitmask != 0)
02752760 571 return iter->startbit +
d960a618
PM
572 (NETLBL_CATMAP_MAPSIZE * idx) + bit - 1;
573 else if (++idx >= NETLBL_CATMAP_MAPCNT) {
02752760 574 if (iter->next == NULL)
d960a618
PM
575 return iter->startbit + NETLBL_CATMAP_SIZE - 1;
576 prev = iter;
02752760 577 iter = iter->next;
d960a618 578 idx = 0;
02752760
PM
579 }
580 bitmask = NETLBL_CATMAP_BIT;
d960a618 581 bit = 0;
02752760
PM
582 }
583
584 return -ENOENT;
585}
586
4b8feff2 587/**
4fbe63d1 588 * netlbl_catmap_getlong - Export an unsigned long bitmap
4b8feff2
PM
589 * @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 */
4fbe63d1
PM
601int netlbl_catmap_getlong(struct netlbl_lsm_catmap *catmap,
602 u32 *offset,
603 unsigned long *bitmap)
4b8feff2 604{
4fbe63d1 605 struct netlbl_lsm_catmap *iter;
4b8feff2
PM
606 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 }
50b8629a 617 iter = _netlbl_catmap_getnode(&catmap, off, _CM_F_WALK, 0);
4b8feff2
PM
618 if (iter == NULL) {
619 *offset = (u32)-1;
620 return 0;
621 }
622
623 if (off < iter->startbit) {
50b8629a
PM
624 *offset = iter->startbit;
625 off = 0;
4b8feff2
PM
626 } else
627 off -= iter->startbit;
4b8feff2 628 idx = off / NETLBL_CATMAP_MAPSIZE;
50b8629a 629 *bitmap = iter->bitmap[idx] >> (off % NETLBL_CATMAP_MAPSIZE);
4b8feff2
PM
630
631 return 0;
632}
633
02752760 634/**
4fbe63d1 635 * netlbl_catmap_setbit - Set a bit in a LSM secattr catmap
41c3bd20 636 * @catmap: pointer to the category bitmap
02752760
PM
637 * @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 */
4fbe63d1
PM
645int netlbl_catmap_setbit(struct netlbl_lsm_catmap **catmap,
646 u32 bit,
647 gfp_t flags)
02752760 648{
4fbe63d1 649 struct netlbl_lsm_catmap *iter;
4b8feff2 650 u32 idx;
02752760 651
4fbe63d1 652 iter = _netlbl_catmap_getnode(catmap, bit, _CM_F_ALLOC, flags);
4b8feff2
PM
653 if (iter == NULL)
654 return -ENOMEM;
02752760 655
4b8feff2
PM
656 bit -= iter->startbit;
657 idx = bit / NETLBL_CATMAP_MAPSIZE;
658 iter->bitmap[idx] |= NETLBL_CATMAP_BIT << (bit % NETLBL_CATMAP_MAPSIZE);
02752760
PM
659
660 return 0;
661}
ceba1832 662EXPORT_SYMBOL(netlbl_catmap_setbit);
02752760
PM
663
664/**
4fbe63d1 665 * netlbl_catmap_setrng - Set a range of bits in a LSM secattr catmap
41c3bd20 666 * @catmap: pointer to the category bitmap
02752760
PM
667 * @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 */
4fbe63d1
PM
676int netlbl_catmap_setrng(struct netlbl_lsm_catmap **catmap,
677 u32 start,
678 u32 end,
679 gfp_t flags)
02752760 680{
4b8feff2
PM
681 int rc = 0;
682 u32 spot = start;
683
684 while (rc == 0 && spot <= end) {
341e0cb5 685 if (((spot & (BITS_PER_LONG - 1)) == 0) &&
4b8feff2 686 ((end - spot) > BITS_PER_LONG)) {
4fbe63d1
PM
687 rc = netlbl_catmap_setlong(catmap,
688 spot,
689 (unsigned long)-1,
690 flags);
4b8feff2
PM
691 spot += BITS_PER_LONG;
692 } else
4fbe63d1 693 rc = netlbl_catmap_setbit(catmap, spot++, flags);
02752760
PM
694 }
695
4b8feff2
PM
696 return rc;
697}
698
699/**
4fbe63d1 700 * netlbl_catmap_setlong - Import an unsigned long bitmap
4b8feff2
PM
701 * @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 */
4fbe63d1
PM
712int netlbl_catmap_setlong(struct netlbl_lsm_catmap **catmap,
713 u32 offset,
714 unsigned long bitmap,
715 gfp_t flags)
4b8feff2 716{
4fbe63d1 717 struct netlbl_lsm_catmap *iter;
4b8feff2
PM
718 u32 idx;
719
720 /* only allow aligned offsets */
721 if ((offset & (BITS_PER_LONG - 1)) != 0)
722 return -EINVAL;
723
4fbe63d1 724 iter = _netlbl_catmap_getnode(catmap, offset, _CM_F_ALLOC, flags);
4b8feff2
PM
725 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;
02752760
PM
733}
734
3faa8f98
HD
735/* 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
d15c345f
PM
805/*
806 * LSM Functions
807 */
808
23bcdc1a
PM
809/**
810 * 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 */
c783f1ce 826 return (atomic_read(&netlabel_mgmt_protocount) > 0);
23bcdc1a
PM
827}
828
d15c345f 829/**
389fb800 830 * netlbl_sock_setattr - Label a socket using the correct protocol
ba6ff9f2 831 * @sk: the socket to label
389fb800 832 * @family: protocol family
d15c345f
PM
833 * @secattr: the security attributes
834 *
835 * Description:
836 * Attach the correct label to the given socket using the security attributes
ba6ff9f2
PM
837 * 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.
63c41688
PM
839 * 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.
d15c345f
PM
842 *
843 */
ba6ff9f2 844int netlbl_sock_setattr(struct sock *sk,
389fb800 845 u16 family,
ba6ff9f2 846 const struct netlbl_lsm_secattr *secattr)
d15c345f 847{
389fb800 848 int ret_val;
d15c345f
PM
849 struct netlbl_dom_map *dom_entry;
850
851 rcu_read_lock();
8f18e675 852 dom_entry = netlbl_domhsh_getentry(secattr->domain, family);
389fb800
PM
853 if (dom_entry == NULL) {
854 ret_val = -ENOENT;
d15c345f 855 goto socket_setattr_return;
389fb800
PM
856 }
857 switch (family) {
858 case AF_INET:
6a8b7f0c 859 switch (dom_entry->def.type) {
389fb800
PM
860 case NETLBL_NLTYPE_ADDRSELECT:
861 ret_val = -EDESTADDRREQ;
862 break;
863 case NETLBL_NLTYPE_CIPSOV4:
864 ret_val = cipso_v4_sock_setattr(sk,
6a8b7f0c
PM
865 dom_entry->def.cipso,
866 secattr);
389fb800
PM
867 break;
868 case NETLBL_NLTYPE_UNLABELED:
869 ret_val = 0;
870 break;
871 default:
872 ret_val = -ENOENT;
873 }
d15c345f 874 break;
dfd56b8b 875#if IS_ENABLED(CONFIG_IPV6)
389fb800 876 case AF_INET6:
ceba1832
HD
877 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 }
d15c345f 892 break;
389fb800 893#endif /* IPv6 */
d15c345f 894 default:
389fb800 895 ret_val = -EPROTONOSUPPORT;
d15c345f
PM
896 }
897
898socket_setattr_return:
899 rcu_read_unlock();
900 return ret_val;
901}
902
014ab19a
PM
903/**
904 * 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{
0e0e3677
PM
914 switch (sk->sk_family) {
915 case AF_INET:
916 cipso_v4_sock_delattr(sk);
917 break;
ceba1832
HD
918#if IS_ENABLED(CONFIG_IPV6)
919 case AF_INET6:
920 calipso_sock_delattr(sk);
921 break;
922#endif /* IPv6 */
0e0e3677 923 }
014ab19a
PM
924}
925
14a72f53
PM
926/**
927 * netlbl_sock_getattr - Determine the security attributes of a sock
928 * @sk: the sock
929 * @secattr: the security attributes
930 *
931 * Description:
8cc44579 932 * Examines the given sock to see if any NetLabel style labeling has been
14a72f53
PM
933 * 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 */
389fb800
PM
938int netlbl_sock_getattr(struct sock *sk,
939 struct netlbl_lsm_secattr *secattr)
14a72f53 940{
389fb800
PM
941 int ret_val;
942
943 switch (sk->sk_family) {
944 case AF_INET:
945 ret_val = cipso_v4_sock_getattr(sk, secattr);
946 break;
dfd56b8b 947#if IS_ENABLED(CONFIG_IPV6)
389fb800 948 case AF_INET6:
ceba1832 949 ret_val = calipso_sock_getattr(sk, secattr);
389fb800
PM
950 break;
951#endif /* IPv6 */
952 default:
953 ret_val = -EPROTONOSUPPORT;
954 }
955
956 return ret_val;
14a72f53
PM
957}
958
014ab19a
PM
959/**
960 * 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;
ceba1832
HD
977#if IS_ENABLED(CONFIG_IPV6)
978 struct sockaddr_in6 *addr6;
979#endif
6a8b7f0c 980 struct netlbl_dommap_def *entry;
014ab19a
PM
981
982 rcu_read_lock();
983 switch (addr->sa_family) {
984 case AF_INET:
985 addr4 = (struct sockaddr_in *)addr;
6a8b7f0c
PM
986 entry = netlbl_domhsh_getentry_af4(secattr->domain,
987 addr4->sin_addr.s_addr);
988 if (entry == NULL) {
014ab19a
PM
989 ret_val = -ENOENT;
990 goto conn_setattr_return;
991 }
6a8b7f0c 992 switch (entry->type) {
014ab19a
PM
993 case NETLBL_NLTYPE_CIPSOV4:
994 ret_val = cipso_v4_sock_setattr(sk,
6a8b7f0c 995 entry->cipso, secattr);
014ab19a
PM
996 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 */
ceba1832 1000 netlbl_sock_delattr(sk);
014ab19a
PM
1001 ret_val = 0;
1002 break;
1003 default:
1004 ret_val = -ENOENT;
1005 }
1006 break;
dfd56b8b 1007#if IS_ENABLED(CONFIG_IPV6)
014ab19a 1008 case AF_INET6:
ceba1832
HD
1009 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 }
014ab19a
PM
1030 break;
1031#endif /* IPv6 */
1032 default:
389fb800 1033 ret_val = -EPROTONOSUPPORT;
014ab19a
PM
1034 }
1035
1036conn_setattr_return:
1037 rcu_read_unlock();
1038 return ret_val;
1039}
1040
389fb800
PM
1041/**
1042 * 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;
6a8b7f0c 1055 struct netlbl_dommap_def *entry;
389fb800
PM
1056
1057 rcu_read_lock();
389fb800
PM
1058 switch (req->rsk_ops->family) {
1059 case AF_INET:
6a8b7f0c 1060 entry = netlbl_domhsh_getentry_af4(secattr->domain,
634fb979 1061 inet_rsk(req)->ir_rmt_addr);
6a8b7f0c
PM
1062 if (entry == NULL) {
1063 ret_val = -ENOENT;
1064 goto req_setattr_return;
389fb800 1065 }
6a8b7f0c 1066 switch (entry->type) {
389fb800 1067 case NETLBL_NLTYPE_CIPSOV4:
6a8b7f0c
PM
1068 ret_val = cipso_v4_req_setattr(req,
1069 entry->cipso, secattr);
389fb800
PM
1070 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;
dfd56b8b 1081#if IS_ENABLED(CONFIG_IPV6)
389fb800
PM
1082 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
07feee8f
PM
1097/**
1098* 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{
0e0e3677
PM
1107 switch (req->rsk_ops->family) {
1108 case AF_INET:
1109 cipso_v4_req_delattr(req);
1110 break;
1111 }
07feee8f
PM
1112}
1113
948bf85c
PM
1114/**
1115 * 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;
6a8b7f0c 1131 struct netlbl_dommap_def *entry;
948bf85c
PM
1132
1133 rcu_read_lock();
1134 switch (family) {
1135 case AF_INET:
1136 hdr4 = ip_hdr(skb);
6a8b7f0c
PM
1137 entry = netlbl_domhsh_getentry_af4(secattr->domain,hdr4->daddr);
1138 if (entry == NULL) {
948bf85c
PM
1139 ret_val = -ENOENT;
1140 goto skbuff_setattr_return;
1141 }
6a8b7f0c 1142 switch (entry->type) {
948bf85c 1143 case NETLBL_NLTYPE_CIPSOV4:
6a8b7f0c
PM
1144 ret_val = cipso_v4_skbuff_setattr(skb, entry->cipso,
1145 secattr);
948bf85c
PM
1146 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;
dfd56b8b 1156#if IS_ENABLED(CONFIG_IPV6)
948bf85c
PM
1157 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:
389fb800 1164 ret_val = -EPROTONOSUPPORT;
948bf85c
PM
1165 }
1166
1167skbuff_setattr_return:
1168 rcu_read_unlock();
1169 return ret_val;
1170}
1171
d15c345f
PM
1172/**
1173 * netlbl_skbuff_getattr - Determine the security attributes of a packet
1174 * @skb: the packet
75e22910 1175 * @family: protocol family
d15c345f
PM
1176 * @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,
75e22910 1186 u16 family,
d15c345f
PM
1187 struct netlbl_lsm_secattr *secattr)
1188{
04f81f01
PM
1189 unsigned char *ptr;
1190
389fb800
PM
1191 switch (family) {
1192 case AF_INET:
04f81f01
PM
1193 ptr = cipso_v4_optptr(skb);
1194 if (ptr && cipso_v4_getattr(ptr, secattr) == 0)
389fb800
PM
1195 return 0;
1196 break;
dfd56b8b 1197#if IS_ENABLED(CONFIG_IPV6)
389fb800
PM
1198 case AF_INET6:
1199 break;
1200#endif /* IPv6 */
1201 }
d15c345f 1202
8cc44579 1203 return netlbl_unlabel_getattr(skb, family, secattr);
d15c345f
PM
1204}
1205
1206/**
1207 * netlbl_skbuff_err - Handle a LSM error on a sk_buff
1208 * @skb: the packet
1209 * @error: the error code
dfaebe98 1210 * @gateway: true if host is acting as a gateway, false otherwise
d15c345f
PM
1211 *
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 */
dfaebe98 1218void netlbl_skbuff_err(struct sk_buff *skb, int error, int gateway)
d15c345f 1219{
04f81f01 1220 if (cipso_v4_optptr(skb))
dfaebe98 1221 cipso_v4_error(skb, error, gateway);
d15c345f
PM
1222}
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{
04f81f01
PM
1252 unsigned char *ptr;
1253
701a90ba 1254 if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
d15c345f
PM
1255 return -ENOMSG;
1256
04f81f01
PM
1257 ptr = cipso_v4_optptr(skb);
1258 if (ptr)
1259 return cipso_v4_cache_add(ptr, secattr);
d15c345f
PM
1260
1261 return -ENOMSG;
1262}
1263
6c2e8ac0
PM
1264/*
1265 * 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}
cb72d382 1285EXPORT_SYMBOL(netlbl_audit_start);
6c2e8ac0 1286
d15c345f
PM
1287/*
1288 * 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
8cc44579
PM
1314 ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);
1315 if (ret_val != 0)
1316 goto init_failure;
1317
d15c345f
PM
1318 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);