1 /* SPDX-License-Identifier: GPL-2.0-or-later */
5 * The NetLabel system manages static and dynamic label mappings for network
6 * protocols such as CIPSO and RIPSO.
8 * Author: Paul Moore <paul@paul-moore.com>
12 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
18 #include <linux/types.h>
19 #include <linux/slab.h>
20 #include <linux/net.h>
21 #include <linux/skbuff.h>
23 #include <linux/in6.h>
24 #include <net/netlink.h>
25 #include <net/request_sock.h>
26 #include <linux/refcount.h>
32 * NetLabel - A management interface for maintaining network packet label
33 * mapping tables for explicit packet labling protocols.
35 * Network protocols such as CIPSO and RIPSO require a label translation layer
36 * to convert the label on the packet into something meaningful on the host
37 * machine. In the current Linux implementation these mapping tables live
38 * inside the kernel; NetLabel provides a mechanism for user space applications
39 * to manage these mapping tables.
41 * NetLabel makes use of the Generic NETLINK mechanism as a transport layer to
42 * send messages between kernel and user space. The general format of a
43 * NetLabel message is shown below:
45 * +-----------------+-------------------+--------- --- -- -
46 * | struct nlmsghdr | struct genlmsghdr | payload
47 * +-----------------+-------------------+--------- --- -- -
49 * The 'nlmsghdr' and 'genlmsghdr' structs should be dealt with like normal.
50 * The payload is dependent on the subsystem specified in the
51 * 'nlmsghdr->nlmsg_type' and should be defined below, supporting functions
52 * should be defined in the corresponding net/netlabel/netlabel_<subsys>.h|c
53 * file. All of the fields in the NetLabel payload are NETLINK attributes, see
54 * the include/net/netlink.h file for more information on NETLINK attributes.
59 * NetLabel NETLINK protocol
62 /* NetLabel NETLINK protocol version
64 * 2: added static labels for unlabeled connections
65 * 3: network selectors added to the NetLabel/LSM domain mapping and the
66 * CIPSO_V4_MAP_LOCAL CIPSO mapping was added
68 #define NETLBL_PROTO_VERSION 3
70 /* NetLabel NETLINK types/families */
71 #define NETLBL_NLTYPE_NONE 0
72 #define NETLBL_NLTYPE_MGMT 1
73 #define NETLBL_NLTYPE_MGMT_NAME "NLBL_MGMT"
74 #define NETLBL_NLTYPE_RIPSO 2
75 #define NETLBL_NLTYPE_RIPSO_NAME "NLBL_RIPSO"
76 #define NETLBL_NLTYPE_CIPSOV4 3
77 #define NETLBL_NLTYPE_CIPSOV4_NAME "NLBL_CIPSOv4"
78 #define NETLBL_NLTYPE_CIPSOV6 4
79 #define NETLBL_NLTYPE_CIPSOV6_NAME "NLBL_CIPSOv6"
80 #define NETLBL_NLTYPE_UNLABELED 5
81 #define NETLBL_NLTYPE_UNLABELED_NAME "NLBL_UNLBL"
82 #define NETLBL_NLTYPE_ADDRSELECT 6
83 #define NETLBL_NLTYPE_ADDRSELECT_NAME "NLBL_ADRSEL"
84 #define NETLBL_NLTYPE_CALIPSO 7
85 #define NETLBL_NLTYPE_CALIPSO_NAME "NLBL_CALIPSO"
88 * NetLabel - Kernel API for accessing the network packet label mappings.
90 * The following functions are provided for use by other kernel modules,
91 * specifically kernel LSM modules, to provide a consistent, transparent API
92 * for dealing with explicit packet labeling protocols such as CIPSO and
93 * RIPSO. The functions defined here are implemented in the
94 * net/netlabel/netlabel_kapi.c file.
98 /* NetLabel audit information */
102 unsigned int sessionid;
106 * LSM security attributes
110 * struct netlbl_lsm_cache - NetLabel LSM security attribute cache
111 * @refcount: atomic reference counter
112 * @free: LSM supplied function to free the cache data
113 * @data: LSM supplied cache data
116 * This structure is provided for LSMs which wish to make use of the NetLabel
117 * caching mechanism to store LSM specific data/attributes in the NetLabel
118 * cache. If the LSM has to perform a lot of translation from the NetLabel
119 * security attributes into it's own internal representation then the cache
120 * mechanism can provide a way to eliminate some or all of that translation
121 * overhead on a cache hit.
124 struct netlbl_lsm_cache {
126 void (*free) (const void *data);
131 * struct netlbl_lsm_catmap - NetLabel LSM secattr category bitmap
132 * @startbit: the value of the lowest order bit in the bitmap
133 * @bitmap: the category bitmap
134 * @next: pointer to the next bitmap "node" or NULL
137 * This structure is used to represent category bitmaps. Due to the large
138 * number of categories supported by most labeling protocols it is not
139 * practical to transfer a full bitmap internally so NetLabel adopts a sparse
140 * bitmap structure modeled after SELinux's ebitmap structure.
141 * The catmap bitmap field MUST be a power of two in length and large
142 * enough to hold at least 240 bits. Special care (i.e. check the code!)
143 * should be used when changing these values as the LSM implementation
144 * probably has functions which rely on the sizes of these types to speed
148 #define NETLBL_CATMAP_MAPCNT 4
149 #define NETLBL_CATMAP_MAPSIZE (sizeof(u64) * 8)
150 #define NETLBL_CATMAP_SIZE (NETLBL_CATMAP_MAPSIZE * \
151 NETLBL_CATMAP_MAPCNT)
152 #define NETLBL_CATMAP_BIT ((u64)0x01)
153 struct netlbl_lsm_catmap {
155 u64 bitmap[NETLBL_CATMAP_MAPCNT];
156 struct netlbl_lsm_catmap *next;
160 * struct netlbl_lsm_secattr - NetLabel LSM security attributes
161 * @flags: indicate structure attributes, see NETLBL_SECATTR_*
162 * @type: indicate the NLTYPE of the attributes
163 * @domain: the NetLabel LSM domain
164 * @cache: NetLabel LSM specific cache
165 * @attr.mls: MLS sensitivity label
166 * @attr.mls.cat: MLS category bitmap
167 * @attr.mls.lvl: MLS sensitivity level
168 * @attr.secid: LSM specific secid token
171 * This structure is used to pass security attributes between NetLabel and the
172 * LSM modules. The flags field is used to specify which fields within the
173 * struct are valid and valid values can be created by bitwise OR'ing the
174 * NETLBL_SECATTR_* defines. The domain field is typically set by the LSM to
175 * specify domain specific configuration settings and is not usually used by
176 * NetLabel itself when returning security attributes to the LSM.
179 struct netlbl_lsm_secattr {
181 /* bitmap values for 'flags' */
182 #define NETLBL_SECATTR_NONE 0x00000000
183 #define NETLBL_SECATTR_DOMAIN 0x00000001
184 #define NETLBL_SECATTR_DOMAIN_CPY (NETLBL_SECATTR_DOMAIN | \
185 NETLBL_SECATTR_FREE_DOMAIN)
186 #define NETLBL_SECATTR_CACHE 0x00000002
187 #define NETLBL_SECATTR_MLS_LVL 0x00000004
188 #define NETLBL_SECATTR_MLS_CAT 0x00000008
189 #define NETLBL_SECATTR_SECID 0x00000010
190 /* bitmap meta-values for 'flags' */
191 #define NETLBL_SECATTR_FREE_DOMAIN 0x01000000
192 #define NETLBL_SECATTR_CACHEABLE (NETLBL_SECATTR_MLS_LVL | \
193 NETLBL_SECATTR_MLS_CAT | \
194 NETLBL_SECATTR_SECID)
197 struct netlbl_lsm_cache *cache;
200 struct netlbl_lsm_catmap *cat;
208 * struct netlbl_calipso_ops - NetLabel CALIPSO operations
209 * @doi_add: add a CALIPSO DOI
210 * @doi_free: free a CALIPSO DOI
211 * @doi_getdef: returns a reference to a DOI
212 * @doi_putdef: releases a reference of a DOI
213 * @doi_walk: enumerate the DOI list
214 * @sock_getattr: retrieve the socket's attr
215 * @sock_setattr: set the socket's attr
216 * @sock_delattr: remove the socket's attr
217 * @req_setattr: set the req socket's attr
218 * @req_delattr: remove the req socket's attr
219 * @opt_getattr: retrieve attr from memory block
220 * @skbuff_optptr: find option in packet
221 * @skbuff_setattr: set the skbuff's attr
222 * @skbuff_delattr: remove the skbuff's attr
223 * @cache_invalidate: invalidate cache
224 * @cache_add: add cache entry
227 * This structure is filled out by the CALIPSO engine and passed
228 * to the NetLabel core via a call to netlbl_calipso_ops_register().
229 * It enables the CALIPSO engine (and hence IPv6) to be compiled
232 struct netlbl_calipso_ops {
233 int (*doi_add)(struct calipso_doi *doi_def,
234 struct netlbl_audit *audit_info);
235 void (*doi_free)(struct calipso_doi *doi_def);
236 int (*doi_remove)(u32 doi, struct netlbl_audit *audit_info);
237 struct calipso_doi *(*doi_getdef)(u32 doi);
238 void (*doi_putdef)(struct calipso_doi *doi_def);
239 int (*doi_walk)(u32 *skip_cnt,
240 int (*callback)(struct calipso_doi *doi_def, void *arg),
242 int (*sock_getattr)(struct sock *sk,
243 struct netlbl_lsm_secattr *secattr);
244 int (*sock_setattr)(struct sock *sk,
245 const struct calipso_doi *doi_def,
246 const struct netlbl_lsm_secattr *secattr);
247 void (*sock_delattr)(struct sock *sk);
248 int (*req_setattr)(struct request_sock *req,
249 const struct calipso_doi *doi_def,
250 const struct netlbl_lsm_secattr *secattr);
251 void (*req_delattr)(struct request_sock *req);
252 int (*opt_getattr)(const unsigned char *calipso,
253 struct netlbl_lsm_secattr *secattr);
254 unsigned char *(*skbuff_optptr)(const struct sk_buff *skb);
255 int (*skbuff_setattr)(struct sk_buff *skb,
256 const struct calipso_doi *doi_def,
257 const struct netlbl_lsm_secattr *secattr);
258 int (*skbuff_delattr)(struct sk_buff *skb);
259 void (*cache_invalidate)(void);
260 int (*cache_add)(const unsigned char *calipso_ptr,
261 const struct netlbl_lsm_secattr *secattr);
265 * LSM security attribute operations (inline)
269 * netlbl_secattr_cache_alloc - Allocate and initialize a secattr cache
270 * @flags: the memory allocation flags
273 * Allocate and initialize a netlbl_lsm_cache structure. Returns a pointer
274 * on success, NULL on failure.
277 static inline struct netlbl_lsm_cache *netlbl_secattr_cache_alloc(gfp_t flags)
279 struct netlbl_lsm_cache *cache;
281 cache = kzalloc(sizeof(*cache), flags);
283 refcount_set(&cache->refcount, 1);
288 * netlbl_secattr_cache_free - Frees a netlbl_lsm_cache struct
289 * @cache: the struct to free
292 * Frees @secattr including all of the internal buffers.
295 static inline void netlbl_secattr_cache_free(struct netlbl_lsm_cache *cache)
297 if (!refcount_dec_and_test(&cache->refcount))
301 cache->free(cache->data);
306 * netlbl_catmap_alloc - Allocate a LSM secattr catmap
307 * @flags: memory allocation flags
310 * Allocate memory for a LSM secattr catmap, returns a pointer on success, NULL
314 static inline struct netlbl_lsm_catmap *netlbl_catmap_alloc(gfp_t flags)
316 return kzalloc(sizeof(struct netlbl_lsm_catmap), flags);
320 * netlbl_catmap_free - Free a LSM secattr catmap
321 * @catmap: the category bitmap
324 * Free a LSM secattr catmap.
327 static inline void netlbl_catmap_free(struct netlbl_lsm_catmap *catmap)
329 struct netlbl_lsm_catmap *iter;
333 catmap = catmap->next;
339 * netlbl_secattr_init - Initialize a netlbl_lsm_secattr struct
340 * @secattr: the struct to initialize
343 * Initialize an already allocated netlbl_lsm_secattr struct.
346 static inline void netlbl_secattr_init(struct netlbl_lsm_secattr *secattr)
348 memset(secattr, 0, sizeof(*secattr));
352 * netlbl_secattr_destroy - Clears a netlbl_lsm_secattr struct
353 * @secattr: the struct to clear
356 * Destroys the @secattr struct, including freeing all of the internal buffers.
357 * The struct must be reset with a call to netlbl_secattr_init() before reuse.
360 static inline void netlbl_secattr_destroy(struct netlbl_lsm_secattr *secattr)
362 if (secattr->flags & NETLBL_SECATTR_FREE_DOMAIN)
363 kfree(secattr->domain);
364 if (secattr->flags & NETLBL_SECATTR_CACHE)
365 netlbl_secattr_cache_free(secattr->cache);
366 if (secattr->flags & NETLBL_SECATTR_MLS_CAT)
367 netlbl_catmap_free(secattr->attr.mls.cat);
371 * netlbl_secattr_alloc - Allocate and initialize a netlbl_lsm_secattr struct
372 * @flags: the memory allocation flags
375 * Allocate and initialize a netlbl_lsm_secattr struct. Returns a valid
376 * pointer on success, or NULL on failure.
379 static inline struct netlbl_lsm_secattr *netlbl_secattr_alloc(gfp_t flags)
381 return kzalloc(sizeof(struct netlbl_lsm_secattr), flags);
385 * netlbl_secattr_free - Frees a netlbl_lsm_secattr struct
386 * @secattr: the struct to free
389 * Frees @secattr including all of the internal buffers.
392 static inline void netlbl_secattr_free(struct netlbl_lsm_secattr *secattr)
394 netlbl_secattr_destroy(secattr);
398 #ifdef CONFIG_NETLABEL
400 * LSM configuration operations
402 int netlbl_cfg_map_del(const char *domain,
406 struct netlbl_audit *audit_info);
407 int netlbl_cfg_unlbl_map_add(const char *domain,
411 struct netlbl_audit *audit_info);
412 int netlbl_cfg_unlbl_static_add(struct net *net,
413 const char *dev_name,
418 struct netlbl_audit *audit_info);
419 int netlbl_cfg_unlbl_static_del(struct net *net,
420 const char *dev_name,
424 struct netlbl_audit *audit_info);
425 int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def,
426 struct netlbl_audit *audit_info);
427 void netlbl_cfg_cipsov4_del(u32 doi, struct netlbl_audit *audit_info);
428 int netlbl_cfg_cipsov4_map_add(u32 doi,
430 const struct in_addr *addr,
431 const struct in_addr *mask,
432 struct netlbl_audit *audit_info);
433 int netlbl_cfg_calipso_add(struct calipso_doi *doi_def,
434 struct netlbl_audit *audit_info);
435 void netlbl_cfg_calipso_del(u32 doi, struct netlbl_audit *audit_info);
436 int netlbl_cfg_calipso_map_add(u32 doi,
438 const struct in6_addr *addr,
439 const struct in6_addr *mask,
440 struct netlbl_audit *audit_info);
442 * LSM security attribute operations
444 int netlbl_catmap_walk(struct netlbl_lsm_catmap *catmap, u32 offset);
445 int netlbl_catmap_walkrng(struct netlbl_lsm_catmap *catmap, u32 offset);
446 int netlbl_catmap_getlong(struct netlbl_lsm_catmap *catmap,
448 unsigned long *bitmap);
449 int netlbl_catmap_setbit(struct netlbl_lsm_catmap **catmap,
452 int netlbl_catmap_setrng(struct netlbl_lsm_catmap **catmap,
456 int netlbl_catmap_setlong(struct netlbl_lsm_catmap **catmap,
458 unsigned long bitmap,
463 int netlbl_bitmap_walk(const unsigned char *bitmap, u32 bitmap_len,
464 u32 offset, u8 state);
465 void netlbl_bitmap_setbit(unsigned char *bitmap, u32 bit, u8 state);
468 * LSM protocol operations (NetLabel LSM/kernel API)
470 int netlbl_enabled(void);
471 int netlbl_sock_setattr(struct sock *sk,
473 const struct netlbl_lsm_secattr *secattr);
474 void netlbl_sock_delattr(struct sock *sk);
475 int netlbl_sock_getattr(struct sock *sk,
476 struct netlbl_lsm_secattr *secattr);
477 int netlbl_conn_setattr(struct sock *sk,
478 struct sockaddr *addr,
479 const struct netlbl_lsm_secattr *secattr);
480 int netlbl_req_setattr(struct request_sock *req,
481 const struct netlbl_lsm_secattr *secattr);
482 void netlbl_req_delattr(struct request_sock *req);
483 int netlbl_skbuff_setattr(struct sk_buff *skb,
485 const struct netlbl_lsm_secattr *secattr);
486 int netlbl_skbuff_getattr(const struct sk_buff *skb,
488 struct netlbl_lsm_secattr *secattr);
489 void netlbl_skbuff_err(struct sk_buff *skb, u16 family, int error, int gateway);
492 * LSM label mapping cache operations
494 void netlbl_cache_invalidate(void);
495 int netlbl_cache_add(const struct sk_buff *skb, u16 family,
496 const struct netlbl_lsm_secattr *secattr);
499 * Protocol engine operations
501 struct audit_buffer *netlbl_audit_start(int type,
502 struct netlbl_audit *audit_info);
504 static inline int netlbl_cfg_map_del(const char *domain,
508 struct netlbl_audit *audit_info)
512 static inline int netlbl_cfg_unlbl_map_add(const char *domain,
516 struct netlbl_audit *audit_info)
520 static inline int netlbl_cfg_unlbl_static_add(struct net *net,
521 const char *dev_name,
526 struct netlbl_audit *audit_info)
530 static inline int netlbl_cfg_unlbl_static_del(struct net *net,
531 const char *dev_name,
535 struct netlbl_audit *audit_info)
539 static inline int netlbl_cfg_cipsov4_add(struct cipso_v4_doi *doi_def,
540 struct netlbl_audit *audit_info)
544 static inline void netlbl_cfg_cipsov4_del(u32 doi,
545 struct netlbl_audit *audit_info)
549 static inline int netlbl_cfg_cipsov4_map_add(u32 doi,
551 const struct in_addr *addr,
552 const struct in_addr *mask,
553 struct netlbl_audit *audit_info)
557 static inline int netlbl_cfg_calipso_add(struct calipso_doi *doi_def,
558 struct netlbl_audit *audit_info)
562 static inline void netlbl_cfg_calipso_del(u32 doi,
563 struct netlbl_audit *audit_info)
567 static inline int netlbl_cfg_calipso_map_add(u32 doi,
569 const struct in6_addr *addr,
570 const struct in6_addr *mask,
571 struct netlbl_audit *audit_info)
575 static inline int netlbl_catmap_walk(struct netlbl_lsm_catmap *catmap,
580 static inline int netlbl_catmap_walkrng(struct netlbl_lsm_catmap *catmap,
585 static inline int netlbl_catmap_getlong(struct netlbl_lsm_catmap *catmap,
587 unsigned long *bitmap)
591 static inline int netlbl_catmap_setbit(struct netlbl_lsm_catmap **catmap,
597 static inline int netlbl_catmap_setrng(struct netlbl_lsm_catmap **catmap,
604 static inline int netlbl_catmap_setlong(struct netlbl_lsm_catmap **catmap,
606 unsigned long bitmap,
611 static inline int netlbl_enabled(void)
615 static inline int netlbl_sock_setattr(struct sock *sk,
617 const struct netlbl_lsm_secattr *secattr)
621 static inline void netlbl_sock_delattr(struct sock *sk)
624 static inline int netlbl_sock_getattr(struct sock *sk,
625 struct netlbl_lsm_secattr *secattr)
629 static inline int netlbl_conn_setattr(struct sock *sk,
630 struct sockaddr *addr,
631 const struct netlbl_lsm_secattr *secattr)
635 static inline int netlbl_req_setattr(struct request_sock *req,
636 const struct netlbl_lsm_secattr *secattr)
640 static inline void netlbl_req_delattr(struct request_sock *req)
644 static inline int netlbl_skbuff_setattr(struct sk_buff *skb,
646 const struct netlbl_lsm_secattr *secattr)
650 static inline int netlbl_skbuff_getattr(const struct sk_buff *skb,
652 struct netlbl_lsm_secattr *secattr)
656 static inline void netlbl_skbuff_err(struct sk_buff *skb,
662 static inline void netlbl_cache_invalidate(void)
666 static inline int netlbl_cache_add(const struct sk_buff *skb, u16 family,
667 const struct netlbl_lsm_secattr *secattr)
671 static inline struct audit_buffer *netlbl_audit_start(int type,
672 struct netlbl_audit *audit_info)
676 #endif /* CONFIG_NETLABEL */
678 const struct netlbl_calipso_ops *
679 netlbl_calipso_ops_register(const struct netlbl_calipso_ops *ops);
681 #endif /* _NETLABEL_H */