2 * NetLabel CIPSO/IPv4 Support
4 * This file defines the CIPSO/IPv4 functions for the NetLabel system. The
5 * NetLabel system manages static and dynamic label mappings for network
6 * protocols such 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/types.h>
32 #include <linux/socket.h>
33 #include <linux/string.h>
34 #include <linux/skbuff.h>
36 #include <net/netlink.h>
37 #include <net/genetlink.h>
38 #include <net/netlabel.h>
39 #include <net/cipso_ipv4.h>
41 #include "netlabel_user.h"
42 #include "netlabel_cipso_v4.h"
44 /* NetLabel Generic NETLINK CIPSOv4 family */
45 static struct genl_family netlbl_cipsov4_gnl_family
= {
46 .id
= GENL_ID_GENERATE
,
48 .name
= NETLBL_NLTYPE_CIPSOV4_NAME
,
49 .version
= NETLBL_PROTO_VERSION
,
59 * netlbl_cipsov4_doi_free - Frees a CIPSO V4 DOI definition
60 * @entry: the entry's RCU field
63 * This function is designed to be used as a callback to the call_rcu()
64 * function so that the memory allocated to the DOI definition can be released
68 static void netlbl_cipsov4_doi_free(struct rcu_head
*entry
)
70 struct cipso_v4_doi
*ptr
;
72 ptr
= container_of(entry
, struct cipso_v4_doi
, rcu
);
74 case CIPSO_V4_MAP_STD
:
75 kfree(ptr
->map
.std
->lvl
.cipso
);
76 kfree(ptr
->map
.std
->lvl
.local
);
77 kfree(ptr
->map
.std
->cat
.cipso
);
78 kfree(ptr
->map
.std
->cat
.local
);
86 * NetLabel Command Handlers
90 * netlbl_cipsov4_add_std - Adds a CIPSO V4 DOI definition
92 * @msg: the ADD message data
93 * @msg_size: the size of the ADD message buffer
96 * Create a new CIPSO_V4_MAP_STD DOI definition based on the given ADD message
97 * and add it to the CIPSO V4 engine. Return zero on success and non-zero on
101 static int netlbl_cipsov4_add_std(u32 doi
, struct nlattr
*msg
, size_t msg_size
)
103 int ret_val
= -EINVAL
;
104 int msg_len
= msg_size
;
108 struct cipso_v4_doi
*doi_def
= NULL
;
113 if (msg_len
< NETLBL_LEN_U32
)
114 goto add_std_failure
;
115 num_tags
= netlbl_getinc_u32(&msg
, &msg_len
);
116 if (num_tags
== 0 || num_tags
> CIPSO_V4_TAG_MAXCNT
)
117 goto add_std_failure
;
119 doi_def
= kmalloc(sizeof(*doi_def
), GFP_KERNEL
);
120 if (doi_def
== NULL
) {
122 goto add_std_failure
;
124 doi_def
->map
.std
= kzalloc(sizeof(*doi_def
->map
.std
), GFP_KERNEL
);
125 if (doi_def
->map
.std
== NULL
) {
127 goto add_std_failure
;
129 doi_def
->type
= CIPSO_V4_MAP_STD
;
131 for (iter
= 0; iter
< num_tags
; iter
++) {
132 if (msg_len
< NETLBL_LEN_U8
)
133 goto add_std_failure
;
134 doi_def
->tags
[iter
] = netlbl_getinc_u8(&msg
, &msg_len
);
135 switch (doi_def
->tags
[iter
]) {
136 case CIPSO_V4_TAG_RBITMAP
:
139 goto add_std_failure
;
142 if (iter
< CIPSO_V4_TAG_MAXCNT
)
143 doi_def
->tags
[iter
] = CIPSO_V4_TAG_INVALID
;
145 if (msg_len
< 6 * NETLBL_LEN_U32
)
146 goto add_std_failure
;
148 num_lvls
= netlbl_getinc_u32(&msg
, &msg_len
);
150 goto add_std_failure
;
151 doi_def
->map
.std
->lvl
.local_size
= netlbl_getinc_u32(&msg
, &msg_len
);
152 if (doi_def
->map
.std
->lvl
.local_size
> CIPSO_V4_MAX_LOC_LVLS
)
153 goto add_std_failure
;
154 doi_def
->map
.std
->lvl
.local
= kcalloc(doi_def
->map
.std
->lvl
.local_size
,
157 if (doi_def
->map
.std
->lvl
.local
== NULL
) {
159 goto add_std_failure
;
161 doi_def
->map
.std
->lvl
.cipso_size
= netlbl_getinc_u8(&msg
, &msg_len
);
162 if (doi_def
->map
.std
->lvl
.cipso_size
> CIPSO_V4_MAX_REM_LVLS
)
163 goto add_std_failure
;
164 doi_def
->map
.std
->lvl
.cipso
= kcalloc(doi_def
->map
.std
->lvl
.cipso_size
,
167 if (doi_def
->map
.std
->lvl
.cipso
== NULL
) {
169 goto add_std_failure
;
172 num_cats
= netlbl_getinc_u32(&msg
, &msg_len
);
173 doi_def
->map
.std
->cat
.local_size
= netlbl_getinc_u32(&msg
, &msg_len
);
174 if (doi_def
->map
.std
->cat
.local_size
> CIPSO_V4_MAX_LOC_CATS
)
175 goto add_std_failure
;
176 doi_def
->map
.std
->cat
.local
= kcalloc(doi_def
->map
.std
->cat
.local_size
,
179 if (doi_def
->map
.std
->cat
.local
== NULL
) {
181 goto add_std_failure
;
183 doi_def
->map
.std
->cat
.cipso_size
= netlbl_getinc_u16(&msg
, &msg_len
);
184 if (doi_def
->map
.std
->cat
.cipso_size
> CIPSO_V4_MAX_REM_CATS
)
185 goto add_std_failure
;
186 doi_def
->map
.std
->cat
.cipso
= kcalloc(doi_def
->map
.std
->cat
.cipso_size
,
189 if (doi_def
->map
.std
->cat
.cipso
== NULL
) {
191 goto add_std_failure
;
195 num_lvls
* (NETLBL_LEN_U32
+ NETLBL_LEN_U8
) +
196 num_cats
* (NETLBL_LEN_U32
+ NETLBL_LEN_U16
))
197 goto add_std_failure
;
199 for (iter
= 0; iter
< doi_def
->map
.std
->lvl
.cipso_size
; iter
++)
200 doi_def
->map
.std
->lvl
.cipso
[iter
] = CIPSO_V4_INV_LVL
;
201 for (iter
= 0; iter
< doi_def
->map
.std
->lvl
.local_size
; iter
++)
202 doi_def
->map
.std
->lvl
.local
[iter
] = CIPSO_V4_INV_LVL
;
203 for (iter
= 0; iter
< doi_def
->map
.std
->cat
.cipso_size
; iter
++)
204 doi_def
->map
.std
->cat
.cipso
[iter
] = CIPSO_V4_INV_CAT
;
205 for (iter
= 0; iter
< doi_def
->map
.std
->cat
.local_size
; iter
++)
206 doi_def
->map
.std
->cat
.local
[iter
] = CIPSO_V4_INV_CAT
;
208 for (iter
= 0; iter
< num_lvls
; iter
++) {
209 tmp_val_a
= netlbl_getinc_u32(&msg
, &msg_len
);
210 tmp_val_b
= netlbl_getinc_u8(&msg
, &msg_len
);
212 if (tmp_val_a
>= doi_def
->map
.std
->lvl
.local_size
||
213 tmp_val_b
>= doi_def
->map
.std
->lvl
.cipso_size
)
214 goto add_std_failure
;
216 doi_def
->map
.std
->lvl
.cipso
[tmp_val_b
] = tmp_val_a
;
217 doi_def
->map
.std
->lvl
.local
[tmp_val_a
] = tmp_val_b
;
220 for (iter
= 0; iter
< num_cats
; iter
++) {
221 tmp_val_a
= netlbl_getinc_u32(&msg
, &msg_len
);
222 tmp_val_b
= netlbl_getinc_u16(&msg
, &msg_len
);
224 if (tmp_val_a
>= doi_def
->map
.std
->cat
.local_size
||
225 tmp_val_b
>= doi_def
->map
.std
->cat
.cipso_size
)
226 goto add_std_failure
;
228 doi_def
->map
.std
->cat
.cipso
[tmp_val_b
] = tmp_val_a
;
229 doi_def
->map
.std
->cat
.local
[tmp_val_a
] = tmp_val_b
;
233 ret_val
= cipso_v4_doi_add(doi_def
);
235 goto add_std_failure
;
240 netlbl_cipsov4_doi_free(&doi_def
->rcu
);
245 * netlbl_cipsov4_add_pass - Adds a CIPSO V4 DOI definition
246 * @doi: the DOI value
247 * @msg: the ADD message data
248 * @msg_size: the size of the ADD message buffer
251 * Create a new CIPSO_V4_MAP_PASS DOI definition based on the given ADD message
252 * and add it to the CIPSO V4 engine. Return zero on success and non-zero on
256 static int netlbl_cipsov4_add_pass(u32 doi
,
260 int ret_val
= -EINVAL
;
261 int msg_len
= msg_size
;
263 struct cipso_v4_doi
*doi_def
= NULL
;
266 if (msg_len
< NETLBL_LEN_U32
)
267 goto add_pass_failure
;
268 num_tags
= netlbl_getinc_u32(&msg
, &msg_len
);
269 if (num_tags
== 0 || num_tags
> CIPSO_V4_TAG_MAXCNT
)
270 goto add_pass_failure
;
272 doi_def
= kmalloc(sizeof(*doi_def
), GFP_KERNEL
);
273 if (doi_def
== NULL
) {
275 goto add_pass_failure
;
277 doi_def
->type
= CIPSO_V4_MAP_PASS
;
279 for (iter
= 0; iter
< num_tags
; iter
++) {
280 if (msg_len
< NETLBL_LEN_U8
)
281 goto add_pass_failure
;
282 doi_def
->tags
[iter
] = netlbl_getinc_u8(&msg
, &msg_len
);
283 switch (doi_def
->tags
[iter
]) {
284 case CIPSO_V4_TAG_RBITMAP
:
287 goto add_pass_failure
;
290 if (iter
< CIPSO_V4_TAG_MAXCNT
)
291 doi_def
->tags
[iter
] = CIPSO_V4_TAG_INVALID
;
294 ret_val
= cipso_v4_doi_add(doi_def
);
296 goto add_pass_failure
;
301 netlbl_cipsov4_doi_free(&doi_def
->rcu
);
306 * netlbl_cipsov4_add - Handle an ADD message
307 * @skb: the NETLINK buffer
308 * @info: the Generic NETLINK info block
311 * Create a new DOI definition based on the given ADD message and add it to the
312 * CIPSO V4 engine. Returns zero on success, negative values on failure.
315 static int netlbl_cipsov4_add(struct sk_buff
*skb
, struct genl_info
*info
)
318 int ret_val
= -EINVAL
;
321 int msg_len
= netlbl_netlink_payload_len(skb
);
322 struct nlattr
*msg
= netlbl_netlink_payload_data(skb
);
324 ret_val
= netlbl_netlink_cap_check(skb
, CAP_NET_ADMIN
);
328 if (msg_len
< 2 * NETLBL_LEN_U32
)
331 doi
= netlbl_getinc_u32(&msg
, &msg_len
);
332 map_type
= netlbl_getinc_u32(&msg
, &msg_len
);
334 case CIPSO_V4_MAP_STD
:
335 ret_val
= netlbl_cipsov4_add_std(doi
, msg
, msg_len
);
337 case CIPSO_V4_MAP_PASS
:
338 ret_val
= netlbl_cipsov4_add_pass(doi
, msg
, msg_len
);
343 netlbl_netlink_send_ack(info
,
344 netlbl_cipsov4_gnl_family
.id
,
351 * netlbl_cipsov4_list - Handle a LIST message
352 * @skb: the NETLINK buffer
353 * @info: the Generic NETLINK info block
356 * Process a user generated LIST message and respond accordingly. Returns
357 * zero on success and negative values on error.
360 static int netlbl_cipsov4_list(struct sk_buff
*skb
, struct genl_info
*info
)
362 int ret_val
= -EINVAL
;
364 struct nlattr
*msg
= netlbl_netlink_payload_data(skb
);
365 struct sk_buff
*ans_skb
;
367 if (netlbl_netlink_payload_len(skb
) != NETLBL_LEN_U32
)
370 doi
= nla_get_u32(msg
);
371 ans_skb
= cipso_v4_doi_dump(doi
, NLMSG_SPACE(GENL_HDRLEN
));
372 if (ans_skb
== NULL
) {
376 netlbl_netlink_hdr_push(ans_skb
,
379 netlbl_cipsov4_gnl_family
.id
,
380 NLBL_CIPSOV4_C_LIST
);
382 ret_val
= netlbl_netlink_snd(ans_skb
, info
->snd_pid
);
389 netlbl_netlink_send_ack(info
,
390 netlbl_cipsov4_gnl_family
.id
,
397 * netlbl_cipsov4_listall - Handle a LISTALL message
398 * @skb: the NETLINK buffer
399 * @info: the Generic NETLINK info block
402 * Process a user generated LISTALL message and respond accordingly. Returns
403 * zero on success and negative values on error.
406 static int netlbl_cipsov4_listall(struct sk_buff
*skb
, struct genl_info
*info
)
408 int ret_val
= -EINVAL
;
409 struct sk_buff
*ans_skb
;
411 ans_skb
= cipso_v4_doi_dump_all(NLMSG_SPACE(GENL_HDRLEN
));
412 if (ans_skb
== NULL
) {
414 goto listall_failure
;
416 netlbl_netlink_hdr_push(ans_skb
,
419 netlbl_cipsov4_gnl_family
.id
,
420 NLBL_CIPSOV4_C_LISTALL
);
422 ret_val
= netlbl_netlink_snd(ans_skb
, info
->snd_pid
);
424 goto listall_failure
;
429 netlbl_netlink_send_ack(info
,
430 netlbl_cipsov4_gnl_family
.id
,
437 * netlbl_cipsov4_remove - Handle a REMOVE message
438 * @skb: the NETLINK buffer
439 * @info: the Generic NETLINK info block
442 * Process a user generated REMOVE message and respond accordingly. Returns
443 * zero on success, negative values on failure.
446 static int netlbl_cipsov4_remove(struct sk_buff
*skb
, struct genl_info
*info
)
450 struct nlattr
*msg
= netlbl_netlink_payload_data(skb
);
452 ret_val
= netlbl_netlink_cap_check(skb
, CAP_NET_ADMIN
);
456 if (netlbl_netlink_payload_len(skb
) != NETLBL_LEN_U32
) {
461 doi
= nla_get_u32(msg
);
462 ret_val
= cipso_v4_doi_remove(doi
, netlbl_cipsov4_doi_free
);
465 netlbl_netlink_send_ack(info
,
466 netlbl_cipsov4_gnl_family
.id
,
473 * NetLabel Generic NETLINK Command Definitions
476 static struct genl_ops netlbl_cipsov4_genl_c_add
= {
477 .cmd
= NLBL_CIPSOV4_C_ADD
,
479 .doit
= netlbl_cipsov4_add
,
483 static struct genl_ops netlbl_cipsov4_genl_c_remove
= {
484 .cmd
= NLBL_CIPSOV4_C_REMOVE
,
486 .doit
= netlbl_cipsov4_remove
,
490 static struct genl_ops netlbl_cipsov4_genl_c_list
= {
491 .cmd
= NLBL_CIPSOV4_C_LIST
,
493 .doit
= netlbl_cipsov4_list
,
497 static struct genl_ops netlbl_cipsov4_genl_c_listall
= {
498 .cmd
= NLBL_CIPSOV4_C_LISTALL
,
500 .doit
= netlbl_cipsov4_listall
,
505 * NetLabel Generic NETLINK Protocol Functions
509 * netlbl_cipsov4_genl_init - Register the CIPSOv4 NetLabel component
512 * Register the CIPSOv4 packet NetLabel component with the Generic NETLINK
513 * mechanism. Returns zero on success, negative values on failure.
516 int netlbl_cipsov4_genl_init(void)
520 ret_val
= genl_register_family(&netlbl_cipsov4_gnl_family
);
524 ret_val
= genl_register_ops(&netlbl_cipsov4_gnl_family
,
525 &netlbl_cipsov4_genl_c_add
);
528 ret_val
= genl_register_ops(&netlbl_cipsov4_gnl_family
,
529 &netlbl_cipsov4_genl_c_remove
);
532 ret_val
= genl_register_ops(&netlbl_cipsov4_gnl_family
,
533 &netlbl_cipsov4_genl_c_list
);
536 ret_val
= genl_register_ops(&netlbl_cipsov4_gnl_family
,
537 &netlbl_cipsov4_genl_c_listall
);