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>
38 #include "netlabel_domainhash.h"
39 #include "netlabel_unlabeled.h"
40 #include "netlabel_user.h"
41 #include "netlabel_mgmt.h"
44 * Security Attribute Functions
48 * netlbl_secattr_catmap_walk - Walk a LSM secattr catmap looking for a bit
49 * @catmap: the category bitmap
50 * @offset: the offset to start searching at, in bits
53 * This function walks a LSM secattr category bitmap starting at @offset and
54 * returns the spot of the first set bit or -ENOENT if no bits are set.
57 int netlbl_secattr_catmap_walk(struct netlbl_lsm_secattr_catmap
*catmap
,
60 struct netlbl_lsm_secattr_catmap
*iter
= catmap
;
63 NETLBL_CATMAP_MAPTYPE bitmap
;
65 if (offset
> iter
->startbit
) {
66 while (offset
>= (iter
->startbit
+ NETLBL_CATMAP_SIZE
)) {
71 node_idx
= (offset
- iter
->startbit
) / NETLBL_CATMAP_MAPSIZE
;
72 node_bit
= offset
- iter
->startbit
-
73 (NETLBL_CATMAP_MAPSIZE
* node_idx
);
78 bitmap
= iter
->bitmap
[node_idx
] >> node_bit
;
82 while ((bitmap
& NETLBL_CATMAP_BIT
) == 0) {
86 return iter
->startbit
+
87 (NETLBL_CATMAP_MAPSIZE
* node_idx
) + node_bit
;
89 if (++node_idx
>= NETLBL_CATMAP_MAPCNT
) {
90 if (iter
->next
!= NULL
) {
96 bitmap
= iter
->bitmap
[node_idx
];
104 * netlbl_secattr_catmap_walk_rng - Find the end of a string of set bits
105 * @catmap: the category bitmap
106 * @offset: the offset to start searching at, in bits
109 * This function walks a LSM secattr category bitmap starting at @offset and
110 * returns the spot of the first cleared bit or -ENOENT if the offset is past
111 * the end of the bitmap.
114 int netlbl_secattr_catmap_walk_rng(struct netlbl_lsm_secattr_catmap
*catmap
,
117 struct netlbl_lsm_secattr_catmap
*iter
= catmap
;
120 NETLBL_CATMAP_MAPTYPE bitmask
;
121 NETLBL_CATMAP_MAPTYPE bitmap
;
123 if (offset
> iter
->startbit
) {
124 while (offset
>= (iter
->startbit
+ NETLBL_CATMAP_SIZE
)) {
129 node_idx
= (offset
- iter
->startbit
) / NETLBL_CATMAP_MAPSIZE
;
130 node_bit
= offset
- iter
->startbit
-
131 (NETLBL_CATMAP_MAPSIZE
* node_idx
);
136 bitmask
= NETLBL_CATMAP_BIT
<< node_bit
;
139 bitmap
= iter
->bitmap
[node_idx
];
140 while (bitmask
!= 0 && (bitmap
& bitmask
) != 0) {
146 return iter
->startbit
+
147 (NETLBL_CATMAP_MAPSIZE
* node_idx
) +
149 else if (++node_idx
>= NETLBL_CATMAP_MAPCNT
) {
150 if (iter
->next
== NULL
)
151 return iter
->startbit
+ NETLBL_CATMAP_SIZE
- 1;
155 bitmask
= NETLBL_CATMAP_BIT
;
163 * netlbl_secattr_catmap_setbit - Set a bit in a LSM secattr catmap
164 * @catmap: the category bitmap
165 * @bit: the bit to set
166 * @flags: memory allocation flags
169 * Set the bit specified by @bit in @catmap. Returns zero on success,
170 * negative values on failure.
173 int netlbl_secattr_catmap_setbit(struct netlbl_lsm_secattr_catmap
*catmap
,
177 struct netlbl_lsm_secattr_catmap
*iter
= catmap
;
181 while (iter
->next
!= NULL
&&
182 bit
>= (iter
->startbit
+ NETLBL_CATMAP_SIZE
))
184 if (bit
>= (iter
->startbit
+ NETLBL_CATMAP_SIZE
)) {
185 iter
->next
= netlbl_secattr_catmap_alloc(flags
);
186 if (iter
->next
== NULL
)
189 iter
->startbit
= bit
& ~(NETLBL_CATMAP_SIZE
- 1);
192 /* gcc always rounds to zero when doing integer division */
193 node_idx
= (bit
- iter
->startbit
) / NETLBL_CATMAP_MAPSIZE
;
194 node_bit
= bit
- iter
->startbit
- (NETLBL_CATMAP_MAPSIZE
* node_idx
);
195 iter
->bitmap
[node_idx
] |= NETLBL_CATMAP_BIT
<< node_bit
;
201 * netlbl_secattr_catmap_setrng - Set a range of bits in a LSM secattr catmap
202 * @catmap: the category bitmap
203 * @start: the starting bit
204 * @end: the last bit in the string
205 * @flags: memory allocation flags
208 * Set a range of bits, starting at @start and ending with @end. Returns zero
209 * on success, negative values on failure.
212 int netlbl_secattr_catmap_setrng(struct netlbl_lsm_secattr_catmap
*catmap
,
218 struct netlbl_lsm_secattr_catmap
*iter
= catmap
;
222 /* XXX - This could probably be made a bit faster by combining writes
223 * to the catmap instead of setting a single bit each time, but for
224 * right now skipping to the start of the range in the catmap should
225 * be a nice improvement over calling the individual setbit function
226 * repeatedly from a loop. */
228 while (iter
->next
!= NULL
&&
229 start
>= (iter
->startbit
+ NETLBL_CATMAP_SIZE
))
231 iter_max_spot
= iter
->startbit
+ NETLBL_CATMAP_SIZE
;
233 for (spot
= start
; spot
<= end
&& ret_val
== 0; spot
++) {
234 if (spot
>= iter_max_spot
&& iter
->next
!= NULL
) {
236 iter_max_spot
= iter
->startbit
+ NETLBL_CATMAP_SIZE
;
238 ret_val
= netlbl_secattr_catmap_setbit(iter
, spot
, GFP_ATOMIC
);
249 * netlbl_enabled - Determine if the NetLabel subsystem is enabled
252 * The LSM can use this function to determine if it should use NetLabel
253 * security attributes in it's enforcement mechanism. Currently, NetLabel is
254 * considered to be enabled when it's configuration contains a valid setup for
255 * at least one labeled protocol (i.e. NetLabel can understand incoming
256 * labeled packets of at least one type); otherwise NetLabel is considered to
260 int netlbl_enabled(void)
262 /* At some point we probably want to expose this mechanism to the user
263 * as well so that admins can toggle NetLabel regardless of the
265 return (netlbl_mgmt_protocount_value() > 0 ? 1 : 0);
269 * netlbl_socket_setattr - Label a socket using the correct protocol
270 * @sk: the socket to label
271 * @secattr: the security attributes
274 * Attach the correct label to the given socket using the security attributes
275 * specified in @secattr. This function requires exclusive access to @sk,
276 * which means it either needs to be in the process of being created or locked.
277 * Returns zero on success, negative values on failure.
280 int netlbl_sock_setattr(struct sock
*sk
,
281 const struct netlbl_lsm_secattr
*secattr
)
283 int ret_val
= -ENOENT
;
284 struct netlbl_dom_map
*dom_entry
;
287 dom_entry
= netlbl_domhsh_getentry(secattr
->domain
);
288 if (dom_entry
== NULL
)
289 goto socket_setattr_return
;
290 switch (dom_entry
->type
) {
291 case NETLBL_NLTYPE_CIPSOV4
:
292 ret_val
= cipso_v4_sock_setattr(sk
,
293 dom_entry
->type_def
.cipsov4
,
296 case NETLBL_NLTYPE_UNLABELED
:
303 socket_setattr_return
:
309 * netlbl_sock_getattr - Determine the security attributes of a sock
311 * @secattr: the security attributes
314 * Examines the given sock to see any NetLabel style labeling has been
315 * applied to the sock, if so it parses the socket label and returns the
316 * security attributes in @secattr. Returns zero on success, negative values
320 int netlbl_sock_getattr(struct sock
*sk
, struct netlbl_lsm_secattr
*secattr
)
324 ret_val
= cipso_v4_sock_getattr(sk
, secattr
);
328 return netlbl_unlabel_getattr(secattr
);
332 * netlbl_skbuff_getattr - Determine the security attributes of a packet
334 * @secattr: the security attributes
337 * Examines the given packet to see if a recognized form of packet labeling
338 * is present, if so it parses the packet label and returns the security
339 * attributes in @secattr. Returns zero on success, negative values on
343 int netlbl_skbuff_getattr(const struct sk_buff
*skb
,
344 struct netlbl_lsm_secattr
*secattr
)
346 if (CIPSO_V4_OPTEXIST(skb
) &&
347 cipso_v4_skbuff_getattr(skb
, secattr
) == 0)
350 return netlbl_unlabel_getattr(secattr
);
354 * netlbl_skbuff_err - Handle a LSM error on a sk_buff
356 * @error: the error code
359 * Deal with a LSM problem when handling the packet in @skb, typically this is
360 * a permission denied problem (-EACCES). The correct action is determined
361 * according to the packet's labeling protocol.
364 void netlbl_skbuff_err(struct sk_buff
*skb
, int error
)
366 if (CIPSO_V4_OPTEXIST(skb
))
367 cipso_v4_error(skb
, error
, 0);
371 * netlbl_cache_invalidate - Invalidate all of the NetLabel protocol caches
374 * For all of the NetLabel protocols that support some form of label mapping
375 * cache, invalidate the cache. Returns zero on success, negative values on
379 void netlbl_cache_invalidate(void)
381 cipso_v4_cache_invalidate();
385 * netlbl_cache_add - Add an entry to a NetLabel protocol cache
387 * @secattr: the packet's security attributes
390 * Add the LSM security attributes for the given packet to the underlying
391 * NetLabel protocol's label mapping cache. Returns zero on success, negative
395 int netlbl_cache_add(const struct sk_buff
*skb
,
396 const struct netlbl_lsm_secattr
*secattr
)
398 if ((secattr
->flags
& NETLBL_SECATTR_CACHE
) == 0)
401 if (CIPSO_V4_OPTEXIST(skb
))
402 return cipso_v4_cache_add(skb
, secattr
);
412 * netlbl_init - Initialize NetLabel
415 * Perform the required NetLabel initialization before first use.
418 static int __init
netlbl_init(void)
422 printk(KERN_INFO
"NetLabel: Initializing\n");
423 printk(KERN_INFO
"NetLabel: domain hash size = %u\n",
424 (1 << NETLBL_DOMHSH_BITSIZE
));
425 printk(KERN_INFO
"NetLabel: protocols ="
430 ret_val
= netlbl_domhsh_init(NETLBL_DOMHSH_BITSIZE
);
434 ret_val
= netlbl_netlink_init();
438 ret_val
= netlbl_unlabel_defconf();
441 printk(KERN_INFO
"NetLabel: unlabeled traffic allowed by default\n");
446 panic("NetLabel: failed to initialize properly (%d)\n", ret_val
);
449 subsys_initcall(netlbl_init
);