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
8 * Author: Paul Moore <paul.moore@hp.com>
13 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
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.
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.
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
31 #include <linux/init.h>
32 #include <linux/types.h>
34 #include <net/netlabel.h>
35 #include <net/cipso_ipv4.h>
37 #include <asm/atomic.h>
39 #include "netlabel_domainhash.h"
40 #include "netlabel_unlabeled.h"
41 #include "netlabel_user.h"
42 #include "netlabel_mgmt.h"
45 * Security Attribute Functions
49 * netlbl_secattr_catmap_walk - Walk a LSM secattr catmap looking for a bit
50 * @catmap: the category bitmap
51 * @offset: the offset to start searching at, in bits
54 * This function walks a LSM secattr category bitmap starting at @offset and
55 * returns the spot of the first set bit or -ENOENT if no bits are set.
58 int netlbl_secattr_catmap_walk(struct netlbl_lsm_secattr_catmap *catmap,
61 struct netlbl_lsm_secattr_catmap *iter = catmap;
64 NETLBL_CATMAP_MAPTYPE bitmap;
66 if (offset > iter->startbit) {
67 while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
72 node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
73 node_bit = offset - iter->startbit -
74 (NETLBL_CATMAP_MAPSIZE * node_idx);
79 bitmap = iter->bitmap[node_idx] >> node_bit;
83 while ((bitmap & NETLBL_CATMAP_BIT) == 0) {
87 return iter->startbit +
88 (NETLBL_CATMAP_MAPSIZE * node_idx) + node_bit;
90 if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
91 if (iter->next != NULL) {
97 bitmap = iter->bitmap[node_idx];
105 * netlbl_secattr_catmap_walk_rng - Find the end of a string of set bits
106 * @catmap: the category bitmap
107 * @offset: the offset to start searching at, in bits
110 * This function walks a LSM secattr category bitmap starting at @offset and
111 * returns the spot of the first cleared bit or -ENOENT if the offset is past
112 * the end of the bitmap.
115 int netlbl_secattr_catmap_walk_rng(struct netlbl_lsm_secattr_catmap *catmap,
118 struct netlbl_lsm_secattr_catmap *iter = catmap;
121 NETLBL_CATMAP_MAPTYPE bitmask;
122 NETLBL_CATMAP_MAPTYPE bitmap;
124 if (offset > iter->startbit) {
125 while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
130 node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
131 node_bit = offset - iter->startbit -
132 (NETLBL_CATMAP_MAPSIZE * node_idx);
137 bitmask = NETLBL_CATMAP_BIT << node_bit;
140 bitmap = iter->bitmap[node_idx];
141 while (bitmask != 0 && (bitmap & bitmask) != 0) {
147 return iter->startbit +
148 (NETLBL_CATMAP_MAPSIZE * node_idx) +
150 else if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
151 if (iter->next == NULL)
152 return iter->startbit + NETLBL_CATMAP_SIZE - 1;
156 bitmask = NETLBL_CATMAP_BIT;
164 * netlbl_secattr_catmap_setbit - Set a bit in a LSM secattr catmap
165 * @catmap: the category bitmap
166 * @bit: the bit to set
167 * @flags: memory allocation flags
170 * Set the bit specified by @bit in @catmap. Returns zero on success,
171 * negative values on failure.
174 int netlbl_secattr_catmap_setbit(struct netlbl_lsm_secattr_catmap *catmap,
178 struct netlbl_lsm_secattr_catmap *iter = catmap;
182 while (iter->next != NULL &&
183 bit >= (iter->startbit + NETLBL_CATMAP_SIZE))
185 if (bit >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
186 iter->next = netlbl_secattr_catmap_alloc(flags);
187 if (iter->next == NULL)
190 iter->startbit = bit & ~(NETLBL_CATMAP_SIZE - 1);
193 /* gcc always rounds to zero when doing integer division */
194 node_idx = (bit - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
195 node_bit = bit - iter->startbit - (NETLBL_CATMAP_MAPSIZE * node_idx);
196 iter->bitmap[node_idx] |= NETLBL_CATMAP_BIT << node_bit;
202 * netlbl_secattr_catmap_setrng - Set a range of bits in a LSM secattr catmap
203 * @catmap: the category bitmap
204 * @start: the starting bit
205 * @end: the last bit in the string
206 * @flags: memory allocation flags
209 * Set a range of bits, starting at @start and ending with @end. Returns zero
210 * on success, negative values on failure.
213 int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,
219 struct netlbl_lsm_secattr_catmap *iter = catmap;
223 /* XXX - This could probably be made a bit faster by combining writes
224 * to the catmap instead of setting a single bit each time, but for
225 * right now skipping to the start of the range in the catmap should
226 * be a nice improvement over calling the individual setbit function
227 * repeatedly from a loop. */
229 while (iter->next != NULL &&
230 start >= (iter->startbit + NETLBL_CATMAP_SIZE))
232 iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
234 for (spot = start; spot <= end && ret_val == 0; spot++) {
235 if (spot >= iter_max_spot && iter->next != NULL) {
237 iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
239 ret_val = netlbl_secattr_catmap_setbit(iter, spot, GFP_ATOMIC);
250 * netlbl_enabled - Determine if the NetLabel subsystem is enabled
253 * The LSM can use this function to determine if it should use NetLabel
254 * security attributes in it's enforcement mechanism. Currently, NetLabel is
255 * considered to be enabled when it's configuration contains a valid setup for
256 * at least one labeled protocol (i.e. NetLabel can understand incoming
257 * labeled packets of at least one type); otherwise NetLabel is considered to
261 int netlbl_enabled(void)
263 /* At some point we probably want to expose this mechanism to the user
264 * as well so that admins can toggle NetLabel regardless of the
266 return (atomic_read(&netlabel_mgmt_protocount) > 0);
270 * netlbl_socket_setattr - Label a socket using the correct protocol
271 * @sk: the socket to label
272 * @secattr: the security attributes
275 * Attach the correct label to the given socket using the security attributes
276 * specified in @secattr. This function requires exclusive access to @sk,
277 * which means it either needs to be in the process of being created or locked.
278 * Returns zero on success, negative values on failure.
281 int netlbl_sock_setattr(struct sock *sk,
282 const struct netlbl_lsm_secattr *secattr)
284 int ret_val = -ENOENT;
285 struct netlbl_dom_map *dom_entry;
288 dom_entry = netlbl_domhsh_getentry(secattr->domain);
289 if (dom_entry == NULL)
290 goto socket_setattr_return;
291 switch (dom_entry->type) {
292 case NETLBL_NLTYPE_CIPSOV4:
293 ret_val = cipso_v4_sock_setattr(sk,
294 dom_entry->type_def.cipsov4,
297 case NETLBL_NLTYPE_UNLABELED:
304 socket_setattr_return:
310 * netlbl_sock_getattr - Determine the security attributes of a sock
312 * @secattr: the security attributes
315 * Examines the given sock to see any NetLabel style labeling has been
316 * applied to the sock, if so it parses the socket label and returns the
317 * security attributes in @secattr. Returns zero on success, negative values
321 int netlbl_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
325 ret_val = cipso_v4_sock_getattr(sk, secattr);
329 return netlbl_unlabel_getattr(secattr);
333 * netlbl_skbuff_getattr - Determine the security attributes of a packet
335 * @secattr: the security attributes
338 * Examines the given packet to see if a recognized form of packet labeling
339 * is present, if so it parses the packet label and returns the security
340 * attributes in @secattr. Returns zero on success, negative values on
344 int netlbl_skbuff_getattr(const struct sk_buff *skb,
345 struct netlbl_lsm_secattr *secattr)
347 if (CIPSO_V4_OPTEXIST(skb) &&
348 cipso_v4_skbuff_getattr(skb, secattr) == 0)
351 return netlbl_unlabel_getattr(secattr);
355 * netlbl_skbuff_err - Handle a LSM error on a sk_buff
357 * @error: the error code
360 * Deal with a LSM problem when handling the packet in @skb, typically this is
361 * a permission denied problem (-EACCES). The correct action is determined
362 * according to the packet's labeling protocol.
365 void netlbl_skbuff_err(struct sk_buff *skb, int error)
367 if (CIPSO_V4_OPTEXIST(skb))
368 cipso_v4_error(skb, error, 0);
372 * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
375 * For all of the NetLabel protocols that support some form of label mapping
376 * cache, invalidate the cache. Returns zero on success, negative values on
380 void netlbl_cache_invalidate(void)
382 cipso_v4_cache_invalidate();
386 * netlbl_cache_add - Add an entry to a NetLabel protocol cache
388 * @secattr: the packet's security attributes
391 * Add the LSM security attributes for the given packet to the underlying
392 * NetLabel protocol's label mapping cache. Returns zero on success, negative
396 int netlbl_cache_add(const struct sk_buff *skb,
397 const struct netlbl_lsm_secattr *secattr)
399 if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
402 if (CIPSO_V4_OPTEXIST(skb))
403 return cipso_v4_cache_add(skb, secattr);
413 * netlbl_init - Initialize NetLabel
416 * Perform the required NetLabel initialization before first use.
419 static int __init netlbl_init(void)
423 printk(KERN_INFO "NetLabel: Initializing\n");
424 printk(KERN_INFO "NetLabel: domain hash size = %u\n",
425 (1 << NETLBL_DOMHSH_BITSIZE));
426 printk(KERN_INFO "NetLabel: protocols ="
431 ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
435 ret_val = netlbl_netlink_init();
439 ret_val = netlbl_unlabel_defconf();
442 printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
447 panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
450 subsys_initcall(netlbl_init);