blob: 78fc557689b2ad19bc6f40ba396147b9a3078157 [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 *
8 * Author: Paul Moore <paul.moore@hp.com>
9 *
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
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 *
29 */
30
31#include <linux/init.h>
32#include <linux/types.h>
Paul Mooreeda61d32008-02-04 22:29:47 -080033#include <linux/audit.h>
Paul Moored15c3452006-08-03 16:48:37 -070034#include <net/ip.h>
35#include <net/netlabel.h>
36#include <net/cipso_ipv4.h>
37#include <asm/bug.h>
Paul Moorec783f1c2008-01-29 08:37:52 -050038#include <asm/atomic.h>
Paul Moored15c3452006-08-03 16:48:37 -070039
40#include "netlabel_domainhash.h"
41#include "netlabel_unlabeled.h"
Paul Mooreeda61d32008-02-04 22:29:47 -080042#include "netlabel_cipso_v4.h"
Paul Moored15c3452006-08-03 16:48:37 -070043#include "netlabel_user.h"
Paul Moore23bcdc12007-07-18 12:28:45 -040044#include "netlabel_mgmt.h"
Paul Moored15c3452006-08-03 16:48:37 -070045
46/*
Paul Mooreeda61d32008-02-04 22:29:47 -080047 * Configuration Functions
48 */
49
50/**
51 * netlbl_cfg_map_del - Remove a NetLabel/LSM domain mapping
52 * @domain: the domain mapping to remove
53 * @audit_info: NetLabel audit information
54 *
55 * Description:
56 * Removes a NetLabel/LSM domain mapping. A @domain value of NULL causes the
57 * default domain mapping to be removed. Returns zero on success, negative
58 * values on failure.
59 *
60 */
61int netlbl_cfg_map_del(const char *domain, struct netlbl_audit *audit_info)
62{
63 return netlbl_domhsh_remove(domain, audit_info);
64}
65
66/**
67 * netlbl_cfg_unlbl_add_map - Add an unlabeled NetLabel/LSM domain mapping
68 * @domain: the domain mapping to add
69 * @audit_info: NetLabel audit information
70 *
71 * Description:
72 * Adds a new unlabeled NetLabel/LSM domain mapping. A @domain value of NULL
73 * causes a new default domain mapping to be added. Returns zero on success,
74 * negative values on failure.
75 *
76 */
77int netlbl_cfg_unlbl_add_map(const char *domain,
78 struct netlbl_audit *audit_info)
79{
80 int ret_val = -ENOMEM;
81 struct netlbl_dom_map *entry;
82
83 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
84 if (entry == NULL)
Paul Moore948a7242008-10-10 10:16:30 -040085 return -ENOMEM;
Paul Mooreeda61d32008-02-04 22:29:47 -080086 if (domain != NULL) {
87 entry->domain = kstrdup(domain, GFP_ATOMIC);
88 if (entry->domain == NULL)
89 goto cfg_unlbl_add_map_failure;
90 }
91 entry->type = NETLBL_NLTYPE_UNLABELED;
92
93 ret_val = netlbl_domhsh_add(entry, audit_info);
94 if (ret_val != 0)
95 goto cfg_unlbl_add_map_failure;
96
97 return 0;
98
99cfg_unlbl_add_map_failure:
100 if (entry != NULL)
101 kfree(entry->domain);
102 kfree(entry);
103 return ret_val;
104}
105
106/**
Paul Mooreeda61d32008-02-04 22:29:47 -0800107 * netlbl_cfg_cipsov4_add_map - Add a new CIPSOv4 DOI definition and mapping
108 * @doi_def: the DOI definition
109 * @domain: the domain mapping to add
110 * @audit_info: NetLabel audit information
111 *
112 * Description:
113 * Add a new CIPSOv4 DOI definition and NetLabel/LSM domain mapping for this
114 * new DOI definition to the NetLabel subsystem. A @domain value of NULL adds
115 * a new default domain mapping. Returns zero on success, negative values on
116 * failure.
117 *
118 */
119int netlbl_cfg_cipsov4_add_map(struct cipso_v4_doi *doi_def,
120 const char *domain,
121 struct netlbl_audit *audit_info)
122{
123 int ret_val = -ENOMEM;
Paul Mooreb1edeb12008-10-10 10:16:31 -0400124 u32 doi;
125 u32 doi_type;
Paul Mooreeda61d32008-02-04 22:29:47 -0800126 struct netlbl_dom_map *entry;
Paul Moore948a7242008-10-10 10:16:30 -0400127 const char *type_str;
128 struct audit_buffer *audit_buf;
Paul Mooreeda61d32008-02-04 22:29:47 -0800129
Paul Mooreb1edeb12008-10-10 10:16:31 -0400130 doi = doi_def->doi;
131 doi_type = doi_def->type;
132
Paul Mooreeda61d32008-02-04 22:29:47 -0800133 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
134 if (entry == NULL)
Paul Moore948a7242008-10-10 10:16:30 -0400135 return -ENOMEM;
Paul Mooreeda61d32008-02-04 22:29:47 -0800136 if (domain != NULL) {
137 entry->domain = kstrdup(domain, GFP_ATOMIC);
138 if (entry->domain == NULL)
139 goto cfg_cipsov4_add_map_failure;
140 }
Paul Mooreeda61d32008-02-04 22:29:47 -0800141
Paul Moore948a7242008-10-10 10:16:30 -0400142 ret_val = cipso_v4_doi_add(doi_def);
Paul Mooreeda61d32008-02-04 22:29:47 -0800143 if (ret_val != 0)
Paul Mooreb1edeb12008-10-10 10:16:31 -0400144 goto cfg_cipsov4_add_map_failure_remove_doi;
145 entry->type = NETLBL_NLTYPE_CIPSOV4;
146 entry->type_def.cipsov4 = cipso_v4_doi_getdef(doi);
147 if (entry->type_def.cipsov4 == NULL) {
148 ret_val = -ENOENT;
149 goto cfg_cipsov4_add_map_failure_remove_doi;
150 }
Paul Mooreeda61d32008-02-04 22:29:47 -0800151 ret_val = netlbl_domhsh_add(entry, audit_info);
152 if (ret_val != 0)
Paul Mooreb1edeb12008-10-10 10:16:31 -0400153 goto cfg_cipsov4_add_map_failure_release_doi;
Paul Mooreeda61d32008-02-04 22:29:47 -0800154
Paul Mooreb1edeb12008-10-10 10:16:31 -0400155cfg_cipsov4_add_map_return:
Paul Moore948a7242008-10-10 10:16:30 -0400156 audit_buf = netlbl_audit_start_common(AUDIT_MAC_CIPSOV4_ADD,
157 audit_info);
158 if (audit_buf != NULL) {
Paul Mooreb1edeb12008-10-10 10:16:31 -0400159 switch (doi_type) {
Paul Moore948a7242008-10-10 10:16:30 -0400160 case CIPSO_V4_MAP_STD:
161 type_str = "std";
162 break;
163 case CIPSO_V4_MAP_PASS:
164 type_str = "pass";
165 break;
166 default:
167 type_str = "(unknown)";
168 }
169 audit_log_format(audit_buf,
170 " cipso_doi=%u cipso_type=%s res=%u",
Paul Mooreb1edeb12008-10-10 10:16:31 -0400171 doi, type_str, ret_val == 0 ? 1 : 0);
Paul Moore948a7242008-10-10 10:16:30 -0400172 audit_log_end(audit_buf);
173 }
Paul Mooreb1edeb12008-10-10 10:16:31 -0400174
175 return ret_val;
176
177cfg_cipsov4_add_map_failure_release_doi:
178 cipso_v4_doi_putdef(doi_def);
179cfg_cipsov4_add_map_failure_remove_doi:
180 cipso_v4_doi_remove(doi, audit_info);
Paul Mooreeda61d32008-02-04 22:29:47 -0800181cfg_cipsov4_add_map_failure:
182 if (entry != NULL)
183 kfree(entry->domain);
184 kfree(entry);
Paul Mooreb1edeb12008-10-10 10:16:31 -0400185 goto cfg_cipsov4_add_map_return;
Paul Mooreeda61d32008-02-04 22:29:47 -0800186}
187
Paul Mooreeda61d32008-02-04 22:29:47 -0800188/*
Paul Moore02752762006-11-29 13:18:18 -0500189 * Security Attribute Functions
190 */
191
192/**
193 * netlbl_secattr_catmap_walk - Walk a LSM secattr catmap looking for a bit
194 * @catmap: the category bitmap
195 * @offset: the offset to start searching at, in bits
196 *
197 * Description:
198 * This function walks a LSM secattr category bitmap starting at @offset and
199 * returns the spot of the first set bit or -ENOENT if no bits are set.
200 *
201 */
202int netlbl_secattr_catmap_walk(struct netlbl_lsm_secattr_catmap *catmap,
203 u32 offset)
204{
205 struct netlbl_lsm_secattr_catmap *iter = catmap;
206 u32 node_idx;
207 u32 node_bit;
208 NETLBL_CATMAP_MAPTYPE bitmap;
209
210 if (offset > iter->startbit) {
211 while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
212 iter = iter->next;
213 if (iter == NULL)
214 return -ENOENT;
215 }
216 node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
217 node_bit = offset - iter->startbit -
218 (NETLBL_CATMAP_MAPSIZE * node_idx);
219 } else {
220 node_idx = 0;
221 node_bit = 0;
222 }
223 bitmap = iter->bitmap[node_idx] >> node_bit;
224
225 for (;;) {
226 if (bitmap != 0) {
227 while ((bitmap & NETLBL_CATMAP_BIT) == 0) {
228 bitmap >>= 1;
229 node_bit++;
230 }
231 return iter->startbit +
232 (NETLBL_CATMAP_MAPSIZE * node_idx) + node_bit;
233 }
234 if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
235 if (iter->next != NULL) {
236 iter = iter->next;
237 node_idx = 0;
238 } else
239 return -ENOENT;
240 }
241 bitmap = iter->bitmap[node_idx];
242 node_bit = 0;
243 }
244
245 return -ENOENT;
246}
247
248/**
249 * netlbl_secattr_catmap_walk_rng - Find the end of a string of set bits
250 * @catmap: the category bitmap
251 * @offset: the offset to start searching at, in bits
252 *
253 * Description:
254 * This function walks a LSM secattr category bitmap starting at @offset and
255 * returns the spot of the first cleared bit or -ENOENT if the offset is past
256 * the end of the bitmap.
257 *
258 */
259int netlbl_secattr_catmap_walk_rng(struct netlbl_lsm_secattr_catmap *catmap,
260 u32 offset)
261{
262 struct netlbl_lsm_secattr_catmap *iter = catmap;
263 u32 node_idx;
264 u32 node_bit;
265 NETLBL_CATMAP_MAPTYPE bitmask;
266 NETLBL_CATMAP_MAPTYPE bitmap;
267
268 if (offset > iter->startbit) {
269 while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
270 iter = iter->next;
271 if (iter == NULL)
272 return -ENOENT;
273 }
274 node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
275 node_bit = offset - iter->startbit -
276 (NETLBL_CATMAP_MAPSIZE * node_idx);
277 } else {
278 node_idx = 0;
279 node_bit = 0;
280 }
281 bitmask = NETLBL_CATMAP_BIT << node_bit;
282
283 for (;;) {
284 bitmap = iter->bitmap[node_idx];
285 while (bitmask != 0 && (bitmap & bitmask) != 0) {
286 bitmask <<= 1;
287 node_bit++;
288 }
289
290 if (bitmask != 0)
291 return iter->startbit +
292 (NETLBL_CATMAP_MAPSIZE * node_idx) +
293 node_bit - 1;
294 else if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
295 if (iter->next == NULL)
296 return iter->startbit + NETLBL_CATMAP_SIZE - 1;
297 iter = iter->next;
298 node_idx = 0;
299 }
300 bitmask = NETLBL_CATMAP_BIT;
301 node_bit = 0;
302 }
303
304 return -ENOENT;
305}
306
307/**
308 * netlbl_secattr_catmap_setbit - Set a bit in a LSM secattr catmap
309 * @catmap: the category bitmap
310 * @bit: the bit to set
311 * @flags: memory allocation flags
312 *
313 * Description:
314 * Set the bit specified by @bit in @catmap. Returns zero on success,
315 * negative values on failure.
316 *
317 */
318int netlbl_secattr_catmap_setbit(struct netlbl_lsm_secattr_catmap *catmap,
319 u32 bit,
320 gfp_t flags)
321{
322 struct netlbl_lsm_secattr_catmap *iter = catmap;
323 u32 node_bit;
324 u32 node_idx;
325
326 while (iter->next != NULL &&
327 bit >= (iter->startbit + NETLBL_CATMAP_SIZE))
328 iter = iter->next;
329 if (bit >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
330 iter->next = netlbl_secattr_catmap_alloc(flags);
331 if (iter->next == NULL)
332 return -ENOMEM;
333 iter = iter->next;
334 iter->startbit = bit & ~(NETLBL_CATMAP_SIZE - 1);
335 }
336
337 /* gcc always rounds to zero when doing integer division */
338 node_idx = (bit - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
339 node_bit = bit - iter->startbit - (NETLBL_CATMAP_MAPSIZE * node_idx);
340 iter->bitmap[node_idx] |= NETLBL_CATMAP_BIT << node_bit;
341
342 return 0;
343}
344
345/**
346 * netlbl_secattr_catmap_setrng - Set a range of bits in a LSM secattr catmap
347 * @catmap: the category bitmap
348 * @start: the starting bit
349 * @end: the last bit in the string
350 * @flags: memory allocation flags
351 *
352 * Description:
353 * Set a range of bits, starting at @start and ending with @end. Returns zero
354 * on success, negative values on failure.
355 *
356 */
357int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,
358 u32 start,
359 u32 end,
360 gfp_t flags)
361{
362 int ret_val = 0;
363 struct netlbl_lsm_secattr_catmap *iter = catmap;
364 u32 iter_max_spot;
365 u32 spot;
366
367 /* XXX - This could probably be made a bit faster by combining writes
368 * to the catmap instead of setting a single bit each time, but for
369 * right now skipping to the start of the range in the catmap should
370 * be a nice improvement over calling the individual setbit function
371 * repeatedly from a loop. */
372
373 while (iter->next != NULL &&
374 start >= (iter->startbit + NETLBL_CATMAP_SIZE))
375 iter = iter->next;
376 iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
377
378 for (spot = start; spot <= end && ret_val == 0; spot++) {
379 if (spot >= iter_max_spot && iter->next != NULL) {
380 iter = iter->next;
381 iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
382 }
383 ret_val = netlbl_secattr_catmap_setbit(iter, spot, GFP_ATOMIC);
384 }
385
386 return ret_val;
387}
388
389/*
Paul Moored15c3452006-08-03 16:48:37 -0700390 * LSM Functions
391 */
392
393/**
Paul Moore23bcdc12007-07-18 12:28:45 -0400394 * netlbl_enabled - Determine if the NetLabel subsystem is enabled
395 *
396 * Description:
397 * The LSM can use this function to determine if it should use NetLabel
398 * security attributes in it's enforcement mechanism. Currently, NetLabel is
399 * considered to be enabled when it's configuration contains a valid setup for
400 * at least one labeled protocol (i.e. NetLabel can understand incoming
401 * labeled packets of at least one type); otherwise NetLabel is considered to
402 * be disabled.
403 *
404 */
405int netlbl_enabled(void)
406{
407 /* At some point we probably want to expose this mechanism to the user
408 * as well so that admins can toggle NetLabel regardless of the
409 * configuration */
Paul Moorec783f1c2008-01-29 08:37:52 -0500410 return (atomic_read(&netlabel_mgmt_protocount) > 0);
Paul Moore23bcdc12007-07-18 12:28:45 -0400411}
412
413/**
Paul Moored15c3452006-08-03 16:48:37 -0700414 * netlbl_socket_setattr - Label a socket using the correct protocol
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700415 * @sk: the socket to label
Paul Moored15c3452006-08-03 16:48:37 -0700416 * @secattr: the security attributes
417 *
418 * Description:
419 * Attach the correct label to the given socket using the security attributes
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700420 * specified in @secattr. This function requires exclusive access to @sk,
421 * which means it either needs to be in the process of being created or locked.
Paul Moore63c41682008-10-10 10:16:32 -0400422 * Returns zero on success, -EDESTADDRREQ if the domain is configured to use
423 * network address selectors (can't blindly label the socket), and negative
424 * values on all other failures.
Paul Moored15c3452006-08-03 16:48:37 -0700425 *
426 */
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700427int netlbl_sock_setattr(struct sock *sk,
428 const struct netlbl_lsm_secattr *secattr)
Paul Moored15c3452006-08-03 16:48:37 -0700429{
430 int ret_val = -ENOENT;
431 struct netlbl_dom_map *dom_entry;
432
433 rcu_read_lock();
434 dom_entry = netlbl_domhsh_getentry(secattr->domain);
435 if (dom_entry == NULL)
436 goto socket_setattr_return;
437 switch (dom_entry->type) {
Paul Moore63c41682008-10-10 10:16:32 -0400438 case NETLBL_NLTYPE_ADDRSELECT:
439 ret_val = -EDESTADDRREQ;
440 break;
Paul Moored15c3452006-08-03 16:48:37 -0700441 case NETLBL_NLTYPE_CIPSOV4:
Paul Mooreba6ff9f2007-06-07 18:37:15 -0700442 ret_val = cipso_v4_sock_setattr(sk,
443 dom_entry->type_def.cipsov4,
444 secattr);
Paul Moored15c3452006-08-03 16:48:37 -0700445 break;
446 case NETLBL_NLTYPE_UNLABELED:
447 ret_val = 0;
448 break;
449 default:
450 ret_val = -ENOENT;
451 }
452
453socket_setattr_return:
454 rcu_read_unlock();
455 return ret_val;
456}
457
458/**
Paul Moore014ab192008-10-10 10:16:33 -0400459 * netlbl_sock_delattr - Delete all the NetLabel labels on a socket
460 * @sk: the socket
461 *
462 * Description:
463 * Remove all the NetLabel labeling from @sk. The caller is responsible for
464 * ensuring that @sk is locked.
465 *
466 */
467void netlbl_sock_delattr(struct sock *sk)
468{
469 cipso_v4_sock_delattr(sk);
470}
471
472/**
Paul Moore14a72f52006-09-25 15:52:01 -0700473 * netlbl_sock_getattr - Determine the security attributes of a sock
474 * @sk: the sock
475 * @secattr: the security attributes
476 *
477 * Description:
Paul Moore8cc44572008-01-29 08:44:21 -0500478 * Examines the given sock to see if any NetLabel style labeling has been
Paul Moore14a72f52006-09-25 15:52:01 -0700479 * applied to the sock, if so it parses the socket label and returns the
480 * security attributes in @secattr. Returns zero on success, negative values
481 * on failure.
482 *
483 */
484int netlbl_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
485{
Paul Moore8cc44572008-01-29 08:44:21 -0500486 return cipso_v4_sock_getattr(sk, secattr);
Paul Moore14a72f52006-09-25 15:52:01 -0700487}
488
489/**
Paul Moore014ab192008-10-10 10:16:33 -0400490 * netlbl_conn_setattr - Label a connected socket using the correct protocol
491 * @sk: the socket to label
492 * @addr: the destination address
493 * @secattr: the security attributes
494 *
495 * Description:
496 * Attach the correct label to the given connected socket using the security
497 * attributes specified in @secattr. The caller is responsible for ensuring
498 * that @sk is locked. Returns zero on success, negative values on failure.
499 *
500 */
501int netlbl_conn_setattr(struct sock *sk,
502 struct sockaddr *addr,
503 const struct netlbl_lsm_secattr *secattr)
504{
505 int ret_val;
506 struct sockaddr_in *addr4;
507 struct netlbl_domaddr4_map *af4_entry;
508
509 rcu_read_lock();
510 switch (addr->sa_family) {
511 case AF_INET:
512 addr4 = (struct sockaddr_in *)addr;
513 af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,
514 addr4->sin_addr.s_addr);
515 if (af4_entry == NULL) {
516 ret_val = -ENOENT;
517 goto conn_setattr_return;
518 }
519 switch (af4_entry->type) {
520 case NETLBL_NLTYPE_CIPSOV4:
521 ret_val = cipso_v4_sock_setattr(sk,
522 af4_entry->type_def.cipsov4,
523 secattr);
524 break;
525 case NETLBL_NLTYPE_UNLABELED:
526 /* just delete the protocols we support for right now
527 * but we could remove other protocols if needed */
528 cipso_v4_sock_delattr(sk);
529 ret_val = 0;
530 break;
531 default:
532 ret_val = -ENOENT;
533 }
534 break;
535#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
536 case AF_INET6:
537 /* since we don't support any IPv6 labeling protocols right
538 * now we can optimize everything away until we do */
539 ret_val = 0;
540 break;
541#endif /* IPv6 */
542 default:
543 ret_val = 0;
544 }
545
546conn_setattr_return:
547 rcu_read_unlock();
548 return ret_val;
549}
550
551/**
Paul Moore948bf852008-10-10 10:16:32 -0400552 * netlbl_skbuff_setattr - Label a packet using the correct protocol
553 * @skb: the packet
554 * @family: protocol family
555 * @secattr: the security attributes
556 *
557 * Description:
558 * Attach the correct label to the given packet using the security attributes
559 * specified in @secattr. Returns zero on success, negative values on failure.
560 *
561 */
562int netlbl_skbuff_setattr(struct sk_buff *skb,
563 u16 family,
564 const struct netlbl_lsm_secattr *secattr)
565{
566 int ret_val;
567 struct iphdr *hdr4;
568 struct netlbl_domaddr4_map *af4_entry;
569
570 rcu_read_lock();
571 switch (family) {
572 case AF_INET:
573 hdr4 = ip_hdr(skb);
574 af4_entry = netlbl_domhsh_getentry_af4(secattr->domain,
575 hdr4->daddr);
576 if (af4_entry == NULL) {
577 ret_val = -ENOENT;
578 goto skbuff_setattr_return;
579 }
580 switch (af4_entry->type) {
581 case NETLBL_NLTYPE_CIPSOV4:
582 ret_val = cipso_v4_skbuff_setattr(skb,
583 af4_entry->type_def.cipsov4,
584 secattr);
585 break;
586 case NETLBL_NLTYPE_UNLABELED:
587 /* just delete the protocols we support for right now
588 * but we could remove other protocols if needed */
589 ret_val = cipso_v4_skbuff_delattr(skb);
590 break;
591 default:
592 ret_val = -ENOENT;
593 }
594 break;
595#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
596 case AF_INET6:
597 /* since we don't support any IPv6 labeling protocols right
598 * now we can optimize everything away until we do */
599 ret_val = 0;
600 break;
601#endif /* IPv6 */
602 default:
603 ret_val = 0;
604 }
605
606skbuff_setattr_return:
607 rcu_read_unlock();
608 return ret_val;
609}
610
611/**
Paul Moored15c3452006-08-03 16:48:37 -0700612 * netlbl_skbuff_getattr - Determine the security attributes of a packet
613 * @skb: the packet
Paul Moore75e22912008-01-29 08:38:04 -0500614 * @family: protocol family
Paul Moored15c3452006-08-03 16:48:37 -0700615 * @secattr: the security attributes
616 *
617 * Description:
618 * Examines the given packet to see if a recognized form of packet labeling
619 * is present, if so it parses the packet label and returns the security
620 * attributes in @secattr. Returns zero on success, negative values on
621 * failure.
622 *
623 */
624int netlbl_skbuff_getattr(const struct sk_buff *skb,
Paul Moore75e22912008-01-29 08:38:04 -0500625 u16 family,
Paul Moored15c3452006-08-03 16:48:37 -0700626 struct netlbl_lsm_secattr *secattr)
627{
Paul Moore05e00cb2006-11-17 17:38:47 -0500628 if (CIPSO_V4_OPTEXIST(skb) &&
629 cipso_v4_skbuff_getattr(skb, secattr) == 0)
Paul Moored15c3452006-08-03 16:48:37 -0700630 return 0;
631
Paul Moore8cc44572008-01-29 08:44:21 -0500632 return netlbl_unlabel_getattr(skb, family, secattr);
Paul Moored15c3452006-08-03 16:48:37 -0700633}
634
635/**
636 * netlbl_skbuff_err - Handle a LSM error on a sk_buff
637 * @skb: the packet
638 * @error: the error code
Paul Mooredfaebe92008-10-10 10:16:31 -0400639 * @gateway: true if host is acting as a gateway, false otherwise
Paul Moored15c3452006-08-03 16:48:37 -0700640 *
641 * Description:
642 * Deal with a LSM problem when handling the packet in @skb, typically this is
643 * a permission denied problem (-EACCES). The correct action is determined
644 * according to the packet's labeling protocol.
645 *
646 */
Paul Mooredfaebe92008-10-10 10:16:31 -0400647void netlbl_skbuff_err(struct sk_buff *skb, int error, int gateway)
Paul Moored15c3452006-08-03 16:48:37 -0700648{
649 if (CIPSO_V4_OPTEXIST(skb))
Paul Mooredfaebe92008-10-10 10:16:31 -0400650 cipso_v4_error(skb, error, gateway);
Paul Moored15c3452006-08-03 16:48:37 -0700651}
652
653/**
654 * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
655 *
656 * Description:
657 * For all of the NetLabel protocols that support some form of label mapping
658 * cache, invalidate the cache. Returns zero on success, negative values on
659 * error.
660 *
661 */
662void netlbl_cache_invalidate(void)
663{
664 cipso_v4_cache_invalidate();
665}
666
667/**
668 * netlbl_cache_add - Add an entry to a NetLabel protocol cache
669 * @skb: the packet
670 * @secattr: the packet's security attributes
671 *
672 * Description:
673 * Add the LSM security attributes for the given packet to the underlying
674 * NetLabel protocol's label mapping cache. Returns zero on success, negative
675 * values on error.
676 *
677 */
678int netlbl_cache_add(const struct sk_buff *skb,
679 const struct netlbl_lsm_secattr *secattr)
680{
Paul Moore701a90b2006-11-17 17:38:46 -0500681 if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
Paul Moored15c3452006-08-03 16:48:37 -0700682 return -ENOMSG;
683
684 if (CIPSO_V4_OPTEXIST(skb))
685 return cipso_v4_cache_add(skb, secattr);
686
687 return -ENOMSG;
688}
689
690/*
691 * Setup Functions
692 */
693
694/**
695 * netlbl_init - Initialize NetLabel
696 *
697 * Description:
698 * Perform the required NetLabel initialization before first use.
699 *
700 */
701static int __init netlbl_init(void)
702{
703 int ret_val;
704
705 printk(KERN_INFO "NetLabel: Initializing\n");
706 printk(KERN_INFO "NetLabel: domain hash size = %u\n",
707 (1 << NETLBL_DOMHSH_BITSIZE));
708 printk(KERN_INFO "NetLabel: protocols ="
709 " UNLABELED"
710 " CIPSOv4"
711 "\n");
712
713 ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
714 if (ret_val != 0)
715 goto init_failure;
716
Paul Moore8cc44572008-01-29 08:44:21 -0500717 ret_val = netlbl_unlabel_init(NETLBL_UNLHSH_BITSIZE);
718 if (ret_val != 0)
719 goto init_failure;
720
Paul Moored15c3452006-08-03 16:48:37 -0700721 ret_val = netlbl_netlink_init();
722 if (ret_val != 0)
723 goto init_failure;
724
725 ret_val = netlbl_unlabel_defconf();
726 if (ret_val != 0)
727 goto init_failure;
728 printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
729
730 return 0;
731
732init_failure:
733 panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
734}
735
736subsys_initcall(netlbl_init);