xref: /openbmc/linux/include/net/netlabel.h (revision 14a72f53fb1bb5d5c2bdd8cf172219519664729a)
111a03f78SPaul Moore /*
211a03f78SPaul Moore  * NetLabel System
311a03f78SPaul Moore  *
411a03f78SPaul Moore  * The NetLabel system manages static and dynamic label mappings for network
511a03f78SPaul Moore  * protocols such as CIPSO and RIPSO.
611a03f78SPaul Moore  *
711a03f78SPaul Moore  * Author: Paul Moore <paul.moore@hp.com>
811a03f78SPaul Moore  *
911a03f78SPaul Moore  */
1011a03f78SPaul Moore 
1111a03f78SPaul Moore /*
1211a03f78SPaul Moore  * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
1311a03f78SPaul Moore  *
1411a03f78SPaul Moore  * This program is free software;  you can redistribute it and/or modify
1511a03f78SPaul Moore  * it under the terms of the GNU General Public License as published by
1611a03f78SPaul Moore  * the Free Software Foundation; either version 2 of the License, or
1711a03f78SPaul Moore  * (at your option) any later version.
1811a03f78SPaul Moore  *
1911a03f78SPaul Moore  * This program is distributed in the hope that it will be useful,
2011a03f78SPaul Moore  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
2111a03f78SPaul Moore  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
2211a03f78SPaul Moore  * the GNU General Public License for more details.
2311a03f78SPaul Moore  *
2411a03f78SPaul Moore  * You should have received a copy of the GNU General Public License
2511a03f78SPaul Moore  * along with this program;  if not, write to the Free Software
2611a03f78SPaul Moore  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2711a03f78SPaul Moore  *
2811a03f78SPaul Moore  */
2911a03f78SPaul Moore 
3011a03f78SPaul Moore #ifndef _NETLABEL_H
3111a03f78SPaul Moore #define _NETLABEL_H
3211a03f78SPaul Moore 
3311a03f78SPaul Moore #include <linux/types.h>
347a0e1d60SPaul Moore #include <linux/net.h>
3511a03f78SPaul Moore #include <linux/skbuff.h>
3611a03f78SPaul Moore #include <net/netlink.h>
3711a03f78SPaul Moore 
3811a03f78SPaul Moore /*
3911a03f78SPaul Moore  * NetLabel - A management interface for maintaining network packet label
4011a03f78SPaul Moore  *            mapping tables for explicit packet labling protocols.
4111a03f78SPaul Moore  *
4211a03f78SPaul Moore  * Network protocols such as CIPSO and RIPSO require a label translation layer
4311a03f78SPaul Moore  * to convert the label on the packet into something meaningful on the host
4411a03f78SPaul Moore  * machine.  In the current Linux implementation these mapping tables live
4511a03f78SPaul Moore  * inside the kernel; NetLabel provides a mechanism for user space applications
4611a03f78SPaul Moore  * to manage these mapping tables.
4711a03f78SPaul Moore  *
4811a03f78SPaul Moore  * NetLabel makes use of the Generic NETLINK mechanism as a transport layer to
4911a03f78SPaul Moore  * send messages between kernel and user space.  The general format of a
5011a03f78SPaul Moore  * NetLabel message is shown below:
5111a03f78SPaul Moore  *
5211a03f78SPaul Moore  *  +-----------------+-------------------+--------- --- -- -
5311a03f78SPaul Moore  *  | struct nlmsghdr | struct genlmsghdr | payload
5411a03f78SPaul Moore  *  +-----------------+-------------------+--------- --- -- -
5511a03f78SPaul Moore  *
5611a03f78SPaul Moore  * The 'nlmsghdr' and 'genlmsghdr' structs should be dealt with like normal.
5711a03f78SPaul Moore  * The payload is dependent on the subsystem specified in the
5811a03f78SPaul Moore  * 'nlmsghdr->nlmsg_type' and should be defined below, supporting functions
5911a03f78SPaul Moore  * should be defined in the corresponding net/netlabel/netlabel_<subsys>.h|c
6011a03f78SPaul Moore  * file.  All of the fields in the NetLabel payload are NETLINK attributes, the
6111a03f78SPaul Moore  * length of each field is the length of the NETLINK attribute payload, see
6211a03f78SPaul Moore  * include/net/netlink.h for more information on NETLINK attributes.
6311a03f78SPaul Moore  *
6411a03f78SPaul Moore  */
6511a03f78SPaul Moore 
6611a03f78SPaul Moore /*
6711a03f78SPaul Moore  * NetLabel NETLINK protocol
6811a03f78SPaul Moore  */
6911a03f78SPaul Moore 
7011a03f78SPaul Moore #define NETLBL_PROTO_VERSION            1
7111a03f78SPaul Moore 
7211a03f78SPaul Moore /* NetLabel NETLINK types/families */
7311a03f78SPaul Moore #define NETLBL_NLTYPE_NONE              0
7411a03f78SPaul Moore #define NETLBL_NLTYPE_MGMT              1
7511a03f78SPaul Moore #define NETLBL_NLTYPE_MGMT_NAME         "NLBL_MGMT"
7611a03f78SPaul Moore #define NETLBL_NLTYPE_RIPSO             2
7711a03f78SPaul Moore #define NETLBL_NLTYPE_RIPSO_NAME        "NLBL_RIPSO"
7811a03f78SPaul Moore #define NETLBL_NLTYPE_CIPSOV4           3
7911a03f78SPaul Moore #define NETLBL_NLTYPE_CIPSOV4_NAME      "NLBL_CIPSOv4"
8011a03f78SPaul Moore #define NETLBL_NLTYPE_CIPSOV6           4
8111a03f78SPaul Moore #define NETLBL_NLTYPE_CIPSOV6_NAME      "NLBL_CIPSOv6"
8211a03f78SPaul Moore #define NETLBL_NLTYPE_UNLABELED         5
8311a03f78SPaul Moore #define NETLBL_NLTYPE_UNLABELED_NAME    "NLBL_UNLBL"
8411a03f78SPaul Moore 
8511a03f78SPaul Moore /* NetLabel return codes */
8611a03f78SPaul Moore #define NETLBL_E_OK                     0
8711a03f78SPaul Moore 
8811a03f78SPaul Moore /*
8911a03f78SPaul Moore  * Helper functions
9011a03f78SPaul Moore  */
9111a03f78SPaul Moore 
9211a03f78SPaul Moore #define NETLBL_LEN_U8                   nla_total_size(sizeof(u8))
9311a03f78SPaul Moore #define NETLBL_LEN_U16                  nla_total_size(sizeof(u16))
9411a03f78SPaul Moore #define NETLBL_LEN_U32                  nla_total_size(sizeof(u32))
9511a03f78SPaul Moore 
9611a03f78SPaul Moore /**
9711a03f78SPaul Moore  * netlbl_netlink_alloc_skb - Allocate a NETLINK message buffer
9811a03f78SPaul Moore  * @head: the amount of headroom in bytes
9911a03f78SPaul Moore  * @body: the desired size (minus headroom) in bytes
10011a03f78SPaul Moore  * @gfp_flags: the alloc flags to pass to alloc_skb()
10111a03f78SPaul Moore  *
10211a03f78SPaul Moore  * Description:
10311a03f78SPaul Moore  * Allocate a NETLINK message buffer based on the sizes given in @head and
10411a03f78SPaul Moore  * @body.  If @head is greater than zero skb_reserve() is called to reserve
10511a03f78SPaul Moore  * @head bytes at the start of the buffer.  Returns a valid sk_buff pointer on
10611a03f78SPaul Moore  * success, NULL on failure.
10711a03f78SPaul Moore  *
10811a03f78SPaul Moore  */
10911a03f78SPaul Moore static inline struct sk_buff *netlbl_netlink_alloc_skb(size_t head,
11011a03f78SPaul Moore 						       size_t body,
1111db2ea39SAl Viro 						       gfp_t gfp_flags)
11211a03f78SPaul Moore {
11311a03f78SPaul Moore 	struct sk_buff *skb;
11411a03f78SPaul Moore 
11511a03f78SPaul Moore 	skb = alloc_skb(NLMSG_ALIGN(head + body), gfp_flags);
11611a03f78SPaul Moore 	if (skb == NULL)
11711a03f78SPaul Moore 		return NULL;
11811a03f78SPaul Moore 	if (head > 0) {
11911a03f78SPaul Moore 		skb_reserve(skb, head);
12011a03f78SPaul Moore 		if (skb_tailroom(skb) < body) {
12111a03f78SPaul Moore 			kfree_skb(skb);
12211a03f78SPaul Moore 			return NULL;
12311a03f78SPaul Moore 		}
12411a03f78SPaul Moore 	}
12511a03f78SPaul Moore 
12611a03f78SPaul Moore 	return skb;
12711a03f78SPaul Moore }
12811a03f78SPaul Moore 
12911a03f78SPaul Moore /*
13011a03f78SPaul Moore  * NetLabel - Kernel API for accessing the network packet label mappings.
13111a03f78SPaul Moore  *
13211a03f78SPaul Moore  * The following functions are provided for use by other kernel modules,
13311a03f78SPaul Moore  * specifically kernel LSM modules, to provide a consistent, transparent API
13411a03f78SPaul Moore  * for dealing with explicit packet labeling protocols such as CIPSO and
13511a03f78SPaul Moore  * RIPSO.  The functions defined here are implemented in the
13611a03f78SPaul Moore  * net/netlabel/netlabel_kapi.c file.
13711a03f78SPaul Moore  *
13811a03f78SPaul Moore  */
13911a03f78SPaul Moore 
14011a03f78SPaul Moore /* Domain mapping definition struct */
14111a03f78SPaul Moore struct netlbl_dom_map;
14211a03f78SPaul Moore 
14311a03f78SPaul Moore /* Domain mapping operations */
14411a03f78SPaul Moore int netlbl_domhsh_remove(const char *domain);
14511a03f78SPaul Moore 
14611a03f78SPaul Moore /* LSM security attributes */
14711a03f78SPaul Moore struct netlbl_lsm_cache {
14811a03f78SPaul Moore 	void (*free) (const void *data);
14911a03f78SPaul Moore 	void *data;
15011a03f78SPaul Moore };
15111a03f78SPaul Moore struct netlbl_lsm_secattr {
15211a03f78SPaul Moore 	char *domain;
15311a03f78SPaul Moore 
15411a03f78SPaul Moore 	u32 mls_lvl;
15511a03f78SPaul Moore 	u32 mls_lvl_vld;
15611a03f78SPaul Moore 	unsigned char *mls_cat;
15711a03f78SPaul Moore 	size_t mls_cat_len;
15811a03f78SPaul Moore 
15911a03f78SPaul Moore 	struct netlbl_lsm_cache cache;
16011a03f78SPaul Moore };
16111a03f78SPaul Moore 
16211a03f78SPaul Moore /*
16311a03f78SPaul Moore  * LSM security attribute operations
16411a03f78SPaul Moore  */
16511a03f78SPaul Moore 
16611a03f78SPaul Moore 
16711a03f78SPaul Moore /**
16811a03f78SPaul Moore  * netlbl_secattr_init - Initialize a netlbl_lsm_secattr struct
16911a03f78SPaul Moore  * @secattr: the struct to initialize
17011a03f78SPaul Moore  *
17111a03f78SPaul Moore  * Description:
17211a03f78SPaul Moore  * Initialize an already allocated netlbl_lsm_secattr struct.  Returns zero on
17311a03f78SPaul Moore  * success, negative values on error.
17411a03f78SPaul Moore  *
17511a03f78SPaul Moore  */
17611a03f78SPaul Moore static inline int netlbl_secattr_init(struct netlbl_lsm_secattr *secattr)
17711a03f78SPaul Moore {
17811a03f78SPaul Moore 	memset(secattr, 0, sizeof(*secattr));
17911a03f78SPaul Moore 	return 0;
18011a03f78SPaul Moore }
18111a03f78SPaul Moore 
18211a03f78SPaul Moore /**
18311a03f78SPaul Moore  * netlbl_secattr_destroy - Clears a netlbl_lsm_secattr struct
18411a03f78SPaul Moore  * @secattr: the struct to clear
18511a03f78SPaul Moore  * @clear_cache: cache clear flag
18611a03f78SPaul Moore  *
18711a03f78SPaul Moore  * Description:
18811a03f78SPaul Moore  * Destroys the @secattr struct, including freeing all of the internal buffers.
18911a03f78SPaul Moore  * If @clear_cache is true then free the cache fields, otherwise leave them
19011a03f78SPaul Moore  * intact.  The struct must be reset with a call to netlbl_secattr_init()
19111a03f78SPaul Moore  * before reuse.
19211a03f78SPaul Moore  *
19311a03f78SPaul Moore  */
19411a03f78SPaul Moore static inline void netlbl_secattr_destroy(struct netlbl_lsm_secattr *secattr,
19511a03f78SPaul Moore 					  u32 clear_cache)
19611a03f78SPaul Moore {
19711a03f78SPaul Moore 	if (clear_cache && secattr->cache.data != NULL && secattr->cache.free)
19811a03f78SPaul Moore 		secattr->cache.free(secattr->cache.data);
19911a03f78SPaul Moore 	kfree(secattr->domain);
20011a03f78SPaul Moore 	kfree(secattr->mls_cat);
20111a03f78SPaul Moore }
20211a03f78SPaul Moore 
20311a03f78SPaul Moore /**
20411a03f78SPaul Moore  * netlbl_secattr_alloc - Allocate and initialize a netlbl_lsm_secattr struct
20511a03f78SPaul Moore  * @flags: the memory allocation flags
20611a03f78SPaul Moore  *
20711a03f78SPaul Moore  * Description:
20811a03f78SPaul Moore  * Allocate and initialize a netlbl_lsm_secattr struct.  Returns a valid
20911a03f78SPaul Moore  * pointer on success, or NULL on failure.
21011a03f78SPaul Moore  *
21111a03f78SPaul Moore  */
21211a03f78SPaul Moore static inline struct netlbl_lsm_secattr *netlbl_secattr_alloc(int flags)
21311a03f78SPaul Moore {
21411a03f78SPaul Moore 	return kzalloc(sizeof(struct netlbl_lsm_secattr), flags);
21511a03f78SPaul Moore }
21611a03f78SPaul Moore 
21711a03f78SPaul Moore /**
21811a03f78SPaul Moore  * netlbl_secattr_free - Frees a netlbl_lsm_secattr struct
21911a03f78SPaul Moore  * @secattr: the struct to free
22011a03f78SPaul Moore  * @clear_cache: cache clear flag
22111a03f78SPaul Moore  *
22211a03f78SPaul Moore  * Description:
22311a03f78SPaul Moore  * Frees @secattr including all of the internal buffers.  If @clear_cache is
22411a03f78SPaul Moore  * true then free the cache fields, otherwise leave them intact.
22511a03f78SPaul Moore  *
22611a03f78SPaul Moore  */
22711a03f78SPaul Moore static inline void netlbl_secattr_free(struct netlbl_lsm_secattr *secattr,
22811a03f78SPaul Moore 				       u32 clear_cache)
22911a03f78SPaul Moore {
23011a03f78SPaul Moore 	netlbl_secattr_destroy(secattr, clear_cache);
23111a03f78SPaul Moore 	kfree(secattr);
23211a03f78SPaul Moore }
23311a03f78SPaul Moore 
23411a03f78SPaul Moore /*
23511a03f78SPaul Moore  * LSM protocol operations
23611a03f78SPaul Moore  */
23711a03f78SPaul Moore 
23811a03f78SPaul Moore #ifdef CONFIG_NETLABEL
23911a03f78SPaul Moore int netlbl_socket_setattr(const struct socket *sock,
24011a03f78SPaul Moore 			  const struct netlbl_lsm_secattr *secattr);
241*14a72f53SPaul Moore int netlbl_sock_getattr(struct sock *sk,
242*14a72f53SPaul Moore 			struct netlbl_lsm_secattr *secattr);
24311a03f78SPaul Moore int netlbl_socket_getattr(const struct socket *sock,
24411a03f78SPaul Moore 			  struct netlbl_lsm_secattr *secattr);
24511a03f78SPaul Moore int netlbl_skbuff_getattr(const struct sk_buff *skb,
24611a03f78SPaul Moore 			  struct netlbl_lsm_secattr *secattr);
24711a03f78SPaul Moore void netlbl_skbuff_err(struct sk_buff *skb, int error);
24811a03f78SPaul Moore #else
24911a03f78SPaul Moore static inline int netlbl_socket_setattr(const struct socket *sock,
25011a03f78SPaul Moore 				     const struct netlbl_lsm_secattr *secattr)
25111a03f78SPaul Moore {
25211a03f78SPaul Moore 	return -ENOSYS;
25311a03f78SPaul Moore }
25411a03f78SPaul Moore 
255*14a72f53SPaul Moore static inline int netlbl_sock_getattr(struct sock *sk,
256*14a72f53SPaul Moore 				      struct netlbl_lsm_secattr *secattr)
257*14a72f53SPaul Moore {
258*14a72f53SPaul Moore 	return -ENOSYS;
259*14a72f53SPaul Moore }
260*14a72f53SPaul Moore 
26111a03f78SPaul Moore static inline int netlbl_socket_getattr(const struct socket *sock,
26211a03f78SPaul Moore 					struct netlbl_lsm_secattr *secattr)
26311a03f78SPaul Moore {
26411a03f78SPaul Moore 	return -ENOSYS;
26511a03f78SPaul Moore }
26611a03f78SPaul Moore 
26711a03f78SPaul Moore static inline int netlbl_skbuff_getattr(const struct sk_buff *skb,
26811a03f78SPaul Moore 					struct netlbl_lsm_secattr *secattr)
26911a03f78SPaul Moore {
27011a03f78SPaul Moore 	return -ENOSYS;
27111a03f78SPaul Moore }
27211a03f78SPaul Moore 
27311a03f78SPaul Moore static inline void netlbl_skbuff_err(struct sk_buff *skb, int error)
27411a03f78SPaul Moore {
27511a03f78SPaul Moore 	return;
27611a03f78SPaul Moore }
27711a03f78SPaul Moore #endif /* CONFIG_NETLABEL */
27811a03f78SPaul Moore 
27911a03f78SPaul Moore /*
28011a03f78SPaul Moore  * LSM label mapping cache operations
28111a03f78SPaul Moore  */
28211a03f78SPaul Moore 
28311a03f78SPaul Moore #ifdef CONFIG_NETLABEL
28411a03f78SPaul Moore void netlbl_cache_invalidate(void);
28511a03f78SPaul Moore int netlbl_cache_add(const struct sk_buff *skb,
28611a03f78SPaul Moore 		     const struct netlbl_lsm_secattr *secattr);
28711a03f78SPaul Moore #else
28811a03f78SPaul Moore static inline void netlbl_cache_invalidate(void)
28911a03f78SPaul Moore {
29011a03f78SPaul Moore 	return;
29111a03f78SPaul Moore }
29211a03f78SPaul Moore 
29311a03f78SPaul Moore static inline int netlbl_cache_add(const struct sk_buff *skb,
29411a03f78SPaul Moore 				   const struct netlbl_lsm_secattr *secattr)
29511a03f78SPaul Moore {
29611a03f78SPaul Moore 	return 0;
29711a03f78SPaul Moore }
29811a03f78SPaul Moore #endif /* CONFIG_NETLABEL */
29911a03f78SPaul Moore 
30011a03f78SPaul Moore #endif /* _NETLABEL_H */
301