xref: /linux/include/net/netlabel.h (revision 11a03f78fbf15a866ba3bf6359a75cdfd1ced703)
1 /*
2  * NetLabel System
3  *
4  * The NetLabel system manages static and dynamic label mappings for network
5  * protocols such as CIPSO and RIPSO.
6  *
7  * Author: Paul Moore <paul.moore@hp.com>
8  *
9  */
10 
11 /*
12  * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
13  *
14  * This program is free software;  you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
22  * the GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program;  if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27  *
28  */
29 
30 #ifndef _NETLABEL_H
31 #define _NETLABEL_H
32 
33 #include <linux/types.h>
34 #include <linux/skbuff.h>
35 #include <net/netlink.h>
36 
37 /*
38  * NetLabel - A management interface for maintaining network packet label
39  *            mapping tables for explicit packet labling protocols.
40  *
41  * Network protocols such as CIPSO and RIPSO require a label translation layer
42  * to convert the label on the packet into something meaningful on the host
43  * machine.  In the current Linux implementation these mapping tables live
44  * inside the kernel; NetLabel provides a mechanism for user space applications
45  * to manage these mapping tables.
46  *
47  * NetLabel makes use of the Generic NETLINK mechanism as a transport layer to
48  * send messages between kernel and user space.  The general format of a
49  * NetLabel message is shown below:
50  *
51  *  +-----------------+-------------------+--------- --- -- -
52  *  | struct nlmsghdr | struct genlmsghdr | payload
53  *  +-----------------+-------------------+--------- --- -- -
54  *
55  * The 'nlmsghdr' and 'genlmsghdr' structs should be dealt with like normal.
56  * The payload is dependent on the subsystem specified in the
57  * 'nlmsghdr->nlmsg_type' and should be defined below, supporting functions
58  * should be defined in the corresponding net/netlabel/netlabel_<subsys>.h|c
59  * file.  All of the fields in the NetLabel payload are NETLINK attributes, the
60  * length of each field is the length of the NETLINK attribute payload, see
61  * include/net/netlink.h for more information on NETLINK attributes.
62  *
63  */
64 
65 /*
66  * NetLabel NETLINK protocol
67  */
68 
69 #define NETLBL_PROTO_VERSION            1
70 
71 /* NetLabel NETLINK types/families */
72 #define NETLBL_NLTYPE_NONE              0
73 #define NETLBL_NLTYPE_MGMT              1
74 #define NETLBL_NLTYPE_MGMT_NAME         "NLBL_MGMT"
75 #define NETLBL_NLTYPE_RIPSO             2
76 #define NETLBL_NLTYPE_RIPSO_NAME        "NLBL_RIPSO"
77 #define NETLBL_NLTYPE_CIPSOV4           3
78 #define NETLBL_NLTYPE_CIPSOV4_NAME      "NLBL_CIPSOv4"
79 #define NETLBL_NLTYPE_CIPSOV6           4
80 #define NETLBL_NLTYPE_CIPSOV6_NAME      "NLBL_CIPSOv6"
81 #define NETLBL_NLTYPE_UNLABELED         5
82 #define NETLBL_NLTYPE_UNLABELED_NAME    "NLBL_UNLBL"
83 
84 /* NetLabel return codes */
85 #define NETLBL_E_OK                     0
86 
87 /*
88  * Helper functions
89  */
90 
91 #define NETLBL_LEN_U8                   nla_total_size(sizeof(u8))
92 #define NETLBL_LEN_U16                  nla_total_size(sizeof(u16))
93 #define NETLBL_LEN_U32                  nla_total_size(sizeof(u32))
94 
95 /**
96  * netlbl_netlink_alloc_skb - Allocate a NETLINK message buffer
97  * @head: the amount of headroom in bytes
98  * @body: the desired size (minus headroom) in bytes
99  * @gfp_flags: the alloc flags to pass to alloc_skb()
100  *
101  * Description:
102  * Allocate a NETLINK message buffer based on the sizes given in @head and
103  * @body.  If @head is greater than zero skb_reserve() is called to reserve
104  * @head bytes at the start of the buffer.  Returns a valid sk_buff pointer on
105  * success, NULL on failure.
106  *
107  */
108 static inline struct sk_buff *netlbl_netlink_alloc_skb(size_t head,
109 						       size_t body,
110 						       int gfp_flags)
111 {
112 	struct sk_buff *skb;
113 
114 	skb = alloc_skb(NLMSG_ALIGN(head + body), gfp_flags);
115 	if (skb == NULL)
116 		return NULL;
117 	if (head > 0) {
118 		skb_reserve(skb, head);
119 		if (skb_tailroom(skb) < body) {
120 			kfree_skb(skb);
121 			return NULL;
122 		}
123 	}
124 
125 	return skb;
126 }
127 
128 /*
129  * NetLabel - Kernel API for accessing the network packet label mappings.
130  *
131  * The following functions are provided for use by other kernel modules,
132  * specifically kernel LSM modules, to provide a consistent, transparent API
133  * for dealing with explicit packet labeling protocols such as CIPSO and
134  * RIPSO.  The functions defined here are implemented in the
135  * net/netlabel/netlabel_kapi.c file.
136  *
137  */
138 
139 /* Domain mapping definition struct */
140 struct netlbl_dom_map;
141 
142 /* Domain mapping operations */
143 int netlbl_domhsh_remove(const char *domain);
144 
145 /* LSM security attributes */
146 struct netlbl_lsm_cache {
147 	void (*free) (const void *data);
148 	void *data;
149 };
150 struct netlbl_lsm_secattr {
151 	char *domain;
152 
153 	u32 mls_lvl;
154 	u32 mls_lvl_vld;
155 	unsigned char *mls_cat;
156 	size_t mls_cat_len;
157 
158 	struct netlbl_lsm_cache cache;
159 };
160 
161 /*
162  * LSM security attribute operations
163  */
164 
165 
166 /**
167  * netlbl_secattr_init - Initialize a netlbl_lsm_secattr struct
168  * @secattr: the struct to initialize
169  *
170  * Description:
171  * Initialize an already allocated netlbl_lsm_secattr struct.  Returns zero on
172  * success, negative values on error.
173  *
174  */
175 static inline int netlbl_secattr_init(struct netlbl_lsm_secattr *secattr)
176 {
177 	memset(secattr, 0, sizeof(*secattr));
178 	return 0;
179 }
180 
181 /**
182  * netlbl_secattr_destroy - Clears a netlbl_lsm_secattr struct
183  * @secattr: the struct to clear
184  * @clear_cache: cache clear flag
185  *
186  * Description:
187  * Destroys the @secattr struct, including freeing all of the internal buffers.
188  * If @clear_cache is true then free the cache fields, otherwise leave them
189  * intact.  The struct must be reset with a call to netlbl_secattr_init()
190  * before reuse.
191  *
192  */
193 static inline void netlbl_secattr_destroy(struct netlbl_lsm_secattr *secattr,
194 					  u32 clear_cache)
195 {
196 	if (clear_cache && secattr->cache.data != NULL && secattr->cache.free)
197 		secattr->cache.free(secattr->cache.data);
198 	kfree(secattr->domain);
199 	kfree(secattr->mls_cat);
200 }
201 
202 /**
203  * netlbl_secattr_alloc - Allocate and initialize a netlbl_lsm_secattr struct
204  * @flags: the memory allocation flags
205  *
206  * Description:
207  * Allocate and initialize a netlbl_lsm_secattr struct.  Returns a valid
208  * pointer on success, or NULL on failure.
209  *
210  */
211 static inline struct netlbl_lsm_secattr *netlbl_secattr_alloc(int flags)
212 {
213 	return kzalloc(sizeof(struct netlbl_lsm_secattr), flags);
214 }
215 
216 /**
217  * netlbl_secattr_free - Frees a netlbl_lsm_secattr struct
218  * @secattr: the struct to free
219  * @clear_cache: cache clear flag
220  *
221  * Description:
222  * Frees @secattr including all of the internal buffers.  If @clear_cache is
223  * true then free the cache fields, otherwise leave them intact.
224  *
225  */
226 static inline void netlbl_secattr_free(struct netlbl_lsm_secattr *secattr,
227 				       u32 clear_cache)
228 {
229 	netlbl_secattr_destroy(secattr, clear_cache);
230 	kfree(secattr);
231 }
232 
233 /*
234  * LSM protocol operations
235  */
236 
237 #ifdef CONFIG_NETLABEL
238 int netlbl_socket_setattr(const struct socket *sock,
239 			  const struct netlbl_lsm_secattr *secattr);
240 int netlbl_socket_getattr(const struct socket *sock,
241 			  struct netlbl_lsm_secattr *secattr);
242 int netlbl_skbuff_getattr(const struct sk_buff *skb,
243 			  struct netlbl_lsm_secattr *secattr);
244 void netlbl_skbuff_err(struct sk_buff *skb, int error);
245 #else
246 static inline int netlbl_socket_setattr(const struct socket *sock,
247 				     const struct netlbl_lsm_secattr *secattr)
248 {
249 	return -ENOSYS;
250 }
251 
252 static inline int netlbl_socket_getattr(const struct socket *sock,
253 					struct netlbl_lsm_secattr *secattr)
254 {
255 	return -ENOSYS;
256 }
257 
258 static inline int netlbl_skbuff_getattr(const struct sk_buff *skb,
259 					struct netlbl_lsm_secattr *secattr)
260 {
261 	return -ENOSYS;
262 }
263 
264 static inline void netlbl_skbuff_err(struct sk_buff *skb, int error)
265 {
266 	return;
267 }
268 #endif /* CONFIG_NETLABEL */
269 
270 /*
271  * LSM label mapping cache operations
272  */
273 
274 #ifdef CONFIG_NETLABEL
275 void netlbl_cache_invalidate(void);
276 int netlbl_cache_add(const struct sk_buff *skb,
277 		     const struct netlbl_lsm_secattr *secattr);
278 #else
279 static inline void netlbl_cache_invalidate(void)
280 {
281 	return;
282 }
283 
284 static inline int netlbl_cache_add(const struct sk_buff *skb,
285 				   const struct netlbl_lsm_secattr *secattr)
286 {
287 	return 0;
288 }
289 #endif /* CONFIG_NETLABEL */
290 
291 #endif /* _NETLABEL_H */
292