ACPI: thinkpad-acpi: enable more hotkeys
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / netlabel / netlabel_kapi.c
blobb165712aaa702ae09d4d35c252c089ac5506a906
1 /*
2 * NetLabel Kernel API
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.
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>
33 #include <net/ip.h>
34 #include <net/netlabel.h>
35 #include <net/cipso_ipv4.h>
36 #include <asm/bug.h>
38 #include "netlabel_domainhash.h"
39 #include "netlabel_unlabeled.h"
40 #include "netlabel_user.h"
43 * Security Attribute Functions
46 /**
47 * netlbl_secattr_catmap_walk - Walk a LSM secattr catmap looking for a bit
48 * @catmap: the category bitmap
49 * @offset: the offset to start searching at, in bits
51 * Description:
52 * This function walks a LSM secattr category bitmap starting at @offset and
53 * returns the spot of the first set bit or -ENOENT if no bits are set.
56 int netlbl_secattr_catmap_walk(struct netlbl_lsm_secattr_catmap *catmap,
57 u32 offset)
59 struct netlbl_lsm_secattr_catmap *iter = catmap;
60 u32 node_idx;
61 u32 node_bit;
62 NETLBL_CATMAP_MAPTYPE bitmap;
64 if (offset > iter->startbit) {
65 while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
66 iter = iter->next;
67 if (iter == NULL)
68 return -ENOENT;
70 node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
71 node_bit = offset - iter->startbit -
72 (NETLBL_CATMAP_MAPSIZE * node_idx);
73 } else {
74 node_idx = 0;
75 node_bit = 0;
77 bitmap = iter->bitmap[node_idx] >> node_bit;
79 for (;;) {
80 if (bitmap != 0) {
81 while ((bitmap & NETLBL_CATMAP_BIT) == 0) {
82 bitmap >>= 1;
83 node_bit++;
85 return iter->startbit +
86 (NETLBL_CATMAP_MAPSIZE * node_idx) + node_bit;
88 if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
89 if (iter->next != NULL) {
90 iter = iter->next;
91 node_idx = 0;
92 } else
93 return -ENOENT;
95 bitmap = iter->bitmap[node_idx];
96 node_bit = 0;
99 return -ENOENT;
103 * netlbl_secattr_catmap_walk_rng - Find the end of a string of set bits
104 * @catmap: the category bitmap
105 * @offset: the offset to start searching at, in bits
107 * Description:
108 * This function walks a LSM secattr category bitmap starting at @offset and
109 * returns the spot of the first cleared bit or -ENOENT if the offset is past
110 * the end of the bitmap.
113 int netlbl_secattr_catmap_walk_rng(struct netlbl_lsm_secattr_catmap *catmap,
114 u32 offset)
116 struct netlbl_lsm_secattr_catmap *iter = catmap;
117 u32 node_idx;
118 u32 node_bit;
119 NETLBL_CATMAP_MAPTYPE bitmask;
120 NETLBL_CATMAP_MAPTYPE bitmap;
122 if (offset > iter->startbit) {
123 while (offset >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
124 iter = iter->next;
125 if (iter == NULL)
126 return -ENOENT;
128 node_idx = (offset - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
129 node_bit = offset - iter->startbit -
130 (NETLBL_CATMAP_MAPSIZE * node_idx);
131 } else {
132 node_idx = 0;
133 node_bit = 0;
135 bitmask = NETLBL_CATMAP_BIT << node_bit;
137 for (;;) {
138 bitmap = iter->bitmap[node_idx];
139 while (bitmask != 0 && (bitmap & bitmask) != 0) {
140 bitmask <<= 1;
141 node_bit++;
144 if (bitmask != 0)
145 return iter->startbit +
146 (NETLBL_CATMAP_MAPSIZE * node_idx) +
147 node_bit - 1;
148 else if (++node_idx >= NETLBL_CATMAP_MAPCNT) {
149 if (iter->next == NULL)
150 return iter->startbit + NETLBL_CATMAP_SIZE - 1;
151 iter = iter->next;
152 node_idx = 0;
154 bitmask = NETLBL_CATMAP_BIT;
155 node_bit = 0;
158 return -ENOENT;
162 * netlbl_secattr_catmap_setbit - Set a bit in a LSM secattr catmap
163 * @catmap: the category bitmap
164 * @bit: the bit to set
165 * @flags: memory allocation flags
167 * Description:
168 * Set the bit specified by @bit in @catmap. Returns zero on success,
169 * negative values on failure.
172 int netlbl_secattr_catmap_setbit(struct netlbl_lsm_secattr_catmap *catmap,
173 u32 bit,
174 gfp_t flags)
176 struct netlbl_lsm_secattr_catmap *iter = catmap;
177 u32 node_bit;
178 u32 node_idx;
180 while (iter->next != NULL &&
181 bit >= (iter->startbit + NETLBL_CATMAP_SIZE))
182 iter = iter->next;
183 if (bit >= (iter->startbit + NETLBL_CATMAP_SIZE)) {
184 iter->next = netlbl_secattr_catmap_alloc(flags);
185 if (iter->next == NULL)
186 return -ENOMEM;
187 iter = iter->next;
188 iter->startbit = bit & ~(NETLBL_CATMAP_SIZE - 1);
191 /* gcc always rounds to zero when doing integer division */
192 node_idx = (bit - iter->startbit) / NETLBL_CATMAP_MAPSIZE;
193 node_bit = bit - iter->startbit - (NETLBL_CATMAP_MAPSIZE * node_idx);
194 iter->bitmap[node_idx] |= NETLBL_CATMAP_BIT << node_bit;
196 return 0;
200 * netlbl_secattr_catmap_setrng - Set a range of bits in a LSM secattr catmap
201 * @catmap: the category bitmap
202 * @start: the starting bit
203 * @end: the last bit in the string
204 * @flags: memory allocation flags
206 * Description:
207 * Set a range of bits, starting at @start and ending with @end. Returns zero
208 * on success, negative values on failure.
211 int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap *catmap,
212 u32 start,
213 u32 end,
214 gfp_t flags)
216 int ret_val = 0;
217 struct netlbl_lsm_secattr_catmap *iter = catmap;
218 u32 iter_max_spot;
219 u32 spot;
221 /* XXX - This could probably be made a bit faster by combining writes
222 * to the catmap instead of setting a single bit each time, but for
223 * right now skipping to the start of the range in the catmap should
224 * be a nice improvement over calling the individual setbit function
225 * repeatedly from a loop. */
227 while (iter->next != NULL &&
228 start >= (iter->startbit + NETLBL_CATMAP_SIZE))
229 iter = iter->next;
230 iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
232 for (spot = start; spot <= end && ret_val == 0; spot++) {
233 if (spot >= iter_max_spot && iter->next != NULL) {
234 iter = iter->next;
235 iter_max_spot = iter->startbit + NETLBL_CATMAP_SIZE;
237 ret_val = netlbl_secattr_catmap_setbit(iter, spot, GFP_ATOMIC);
240 return ret_val;
244 * LSM Functions
248 * netlbl_socket_setattr - Label a socket using the correct protocol
249 * @sk: the socket to label
250 * @secattr: the security attributes
252 * Description:
253 * Attach the correct label to the given socket using the security attributes
254 * specified in @secattr. This function requires exclusive access to @sk,
255 * which means it either needs to be in the process of being created or locked.
256 * Returns zero on success, negative values on failure.
259 int netlbl_sock_setattr(struct sock *sk,
260 const struct netlbl_lsm_secattr *secattr)
262 int ret_val = -ENOENT;
263 struct netlbl_dom_map *dom_entry;
265 rcu_read_lock();
266 dom_entry = netlbl_domhsh_getentry(secattr->domain);
267 if (dom_entry == NULL)
268 goto socket_setattr_return;
269 switch (dom_entry->type) {
270 case NETLBL_NLTYPE_CIPSOV4:
271 ret_val = cipso_v4_sock_setattr(sk,
272 dom_entry->type_def.cipsov4,
273 secattr);
274 break;
275 case NETLBL_NLTYPE_UNLABELED:
276 ret_val = 0;
277 break;
278 default:
279 ret_val = -ENOENT;
282 socket_setattr_return:
283 rcu_read_unlock();
284 return ret_val;
288 * netlbl_sock_getattr - Determine the security attributes of a sock
289 * @sk: the sock
290 * @secattr: the security attributes
292 * Description:
293 * Examines the given sock to see any NetLabel style labeling has been
294 * applied to the sock, if so it parses the socket label and returns the
295 * security attributes in @secattr. Returns zero on success, negative values
296 * on failure.
299 int netlbl_sock_getattr(struct sock *sk, struct netlbl_lsm_secattr *secattr)
301 int ret_val;
303 ret_val = cipso_v4_sock_getattr(sk, secattr);
304 if (ret_val == 0)
305 return 0;
307 return netlbl_unlabel_getattr(secattr);
311 * netlbl_skbuff_getattr - Determine the security attributes of a packet
312 * @skb: the packet
313 * @secattr: the security attributes
315 * Description:
316 * Examines the given packet to see if a recognized form of packet labeling
317 * is present, if so it parses the packet label and returns the security
318 * attributes in @secattr. Returns zero on success, negative values on
319 * failure.
322 int netlbl_skbuff_getattr(const struct sk_buff *skb,
323 struct netlbl_lsm_secattr *secattr)
325 if (CIPSO_V4_OPTEXIST(skb) &&
326 cipso_v4_skbuff_getattr(skb, secattr) == 0)
327 return 0;
329 return netlbl_unlabel_getattr(secattr);
333 * netlbl_skbuff_err - Handle a LSM error on a sk_buff
334 * @skb: the packet
335 * @error: the error code
337 * Description:
338 * Deal with a LSM problem when handling the packet in @skb, typically this is
339 * a permission denied problem (-EACCES). The correct action is determined
340 * according to the packet's labeling protocol.
343 void netlbl_skbuff_err(struct sk_buff *skb, int error)
345 if (CIPSO_V4_OPTEXIST(skb))
346 cipso_v4_error(skb, error, 0);
350 * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
352 * Description:
353 * For all of the NetLabel protocols that support some form of label mapping
354 * cache, invalidate the cache. Returns zero on success, negative values on
355 * error.
358 void netlbl_cache_invalidate(void)
360 cipso_v4_cache_invalidate();
364 * netlbl_cache_add - Add an entry to a NetLabel protocol cache
365 * @skb: the packet
366 * @secattr: the packet's security attributes
368 * Description:
369 * Add the LSM security attributes for the given packet to the underlying
370 * NetLabel protocol's label mapping cache. Returns zero on success, negative
371 * values on error.
374 int netlbl_cache_add(const struct sk_buff *skb,
375 const struct netlbl_lsm_secattr *secattr)
377 if ((secattr->flags & NETLBL_SECATTR_CACHE) == 0)
378 return -ENOMSG;
380 if (CIPSO_V4_OPTEXIST(skb))
381 return cipso_v4_cache_add(skb, secattr);
383 return -ENOMSG;
387 * Setup Functions
391 * netlbl_init - Initialize NetLabel
393 * Description:
394 * Perform the required NetLabel initialization before first use.
397 static int __init netlbl_init(void)
399 int ret_val;
401 printk(KERN_INFO "NetLabel: Initializing\n");
402 printk(KERN_INFO "NetLabel: domain hash size = %u\n",
403 (1 << NETLBL_DOMHSH_BITSIZE));
404 printk(KERN_INFO "NetLabel: protocols ="
405 " UNLABELED"
406 " CIPSOv4"
407 "\n");
409 ret_val = netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE);
410 if (ret_val != 0)
411 goto init_failure;
413 ret_val = netlbl_netlink_init();
414 if (ret_val != 0)
415 goto init_failure;
417 ret_val = netlbl_unlabel_defconf();
418 if (ret_val != 0)
419 goto init_failure;
420 printk(KERN_INFO "NetLabel: unlabeled traffic allowed by default\n");
422 return 0;
424 init_failure:
425 panic("NetLabel: failed to initialize properly (%d)\n", ret_val);
428 subsys_initcall(netlbl_init);