xref: /openbmc/linux/include/net/netlabel.h (revision 9ff4680e9958508bebc5c683b98f37b66617e088)
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/net.h>
35 #include <linux/skbuff.h>
36 #include <net/netlink.h>
37 #include <asm/atomic.h>
38 
39 /*
40  * NetLabel - A management interface for maintaining network packet label
41  *            mapping tables for explicit packet labling protocols.
42  *
43  * Network protocols such as CIPSO and RIPSO require a label translation layer
44  * to convert the label on the packet into something meaningful on the host
45  * machine.  In the current Linux implementation these mapping tables live
46  * inside the kernel; NetLabel provides a mechanism for user space applications
47  * to manage these mapping tables.
48  *
49  * NetLabel makes use of the Generic NETLINK mechanism as a transport layer to
50  * send messages between kernel and user space.  The general format of a
51  * NetLabel message is shown below:
52  *
53  *  +-----------------+-------------------+--------- --- -- -
54  *  | struct nlmsghdr | struct genlmsghdr | payload
55  *  +-----------------+-------------------+--------- --- -- -
56  *
57  * The 'nlmsghdr' and 'genlmsghdr' structs should be dealt with like normal.
58  * The payload is dependent on the subsystem specified in the
59  * 'nlmsghdr->nlmsg_type' and should be defined below, supporting functions
60  * should be defined in the corresponding net/netlabel/netlabel_<subsys>.h|c
61  * file.  All of the fields in the NetLabel payload are NETLINK attributes, see
62  * the include/net/netlink.h file for more information on NETLINK attributes.
63  *
64  */
65 
66 /*
67  * NetLabel NETLINK protocol
68  */
69 
70 #define NETLBL_PROTO_VERSION            1
71 
72 /* NetLabel NETLINK types/families */
73 #define NETLBL_NLTYPE_NONE              0
74 #define NETLBL_NLTYPE_MGMT              1
75 #define NETLBL_NLTYPE_MGMT_NAME         "NLBL_MGMT"
76 #define NETLBL_NLTYPE_RIPSO             2
77 #define NETLBL_NLTYPE_RIPSO_NAME        "NLBL_RIPSO"
78 #define NETLBL_NLTYPE_CIPSOV4           3
79 #define NETLBL_NLTYPE_CIPSOV4_NAME      "NLBL_CIPSOv4"
80 #define NETLBL_NLTYPE_CIPSOV6           4
81 #define NETLBL_NLTYPE_CIPSOV6_NAME      "NLBL_CIPSOv6"
82 #define NETLBL_NLTYPE_UNLABELED         5
83 #define NETLBL_NLTYPE_UNLABELED_NAME    "NLBL_UNLBL"
84 
85 /*
86  * NetLabel - Kernel API for accessing the network packet label mappings.
87  *
88  * The following functions are provided for use by other kernel modules,
89  * specifically kernel LSM modules, to provide a consistent, transparent API
90  * for dealing with explicit packet labeling protocols such as CIPSO and
91  * RIPSO.  The functions defined here are implemented in the
92  * net/netlabel/netlabel_kapi.c file.
93  *
94  */
95 
96 /* NetLabel audit information */
97 struct netlbl_audit {
98 	u32 secid;
99 	uid_t loginuid;
100 };
101 
102 /* Domain mapping definition struct */
103 struct netlbl_dom_map;
104 
105 /* Domain mapping operations */
106 int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info);
107 
108 /* LSM security attributes */
109 struct netlbl_lsm_cache {
110 	atomic_t refcount;
111 	void (*free) (const void *data);
112 	void *data;
113 };
114 struct netlbl_lsm_secattr {
115 	char *domain;
116 
117 	u32 mls_lvl;
118 	u32 mls_lvl_vld;
119 	unsigned char *mls_cat;
120 	size_t mls_cat_len;
121 
122 	struct netlbl_lsm_cache *cache;
123 };
124 
125 /*
126  * LSM security attribute operations
127  */
128 
129 
130 /**
131  * netlbl_secattr_cache_alloc - Allocate and initialize a secattr cache
132  * @flags: the memory allocation flags
133  *
134  * Description:
135  * Allocate and initialize a netlbl_lsm_cache structure.  Returns a pointer
136  * on success, NULL on failure.
137  *
138  */
139 static inline struct netlbl_lsm_cache *netlbl_secattr_cache_alloc(int flags)
140 {
141 	struct netlbl_lsm_cache *cache;
142 
143 	cache = kzalloc(sizeof(*cache), flags);
144 	if (cache)
145 		atomic_set(&cache->refcount, 1);
146 	return cache;
147 }
148 
149 /**
150  * netlbl_secattr_cache_free - Frees a netlbl_lsm_cache struct
151  * @cache: the struct to free
152  *
153  * Description:
154  * Frees @secattr including all of the internal buffers.
155  *
156  */
157 static inline void netlbl_secattr_cache_free(struct netlbl_lsm_cache *cache)
158 {
159 	if (!atomic_dec_and_test(&cache->refcount))
160 		return;
161 
162 	if (cache->free)
163 		cache->free(cache->data);
164 	kfree(cache);
165 }
166 
167 /**
168  * netlbl_secattr_init - Initialize a netlbl_lsm_secattr struct
169  * @secattr: the struct to initialize
170  *
171  * Description:
172  * Initialize an already allocated netlbl_lsm_secattr struct.  Returns zero on
173  * success, negative values on error.
174  *
175  */
176 static inline int netlbl_secattr_init(struct netlbl_lsm_secattr *secattr)
177 {
178 	memset(secattr, 0, sizeof(*secattr));
179 	return 0;
180 }
181 
182 /**
183  * netlbl_secattr_destroy - Clears a netlbl_lsm_secattr struct
184  * @secattr: the struct to clear
185  *
186  * Description:
187  * Destroys the @secattr struct, including freeing all of the internal buffers.
188  * The struct must be reset with a call to netlbl_secattr_init() before reuse.
189  *
190  */
191 static inline void netlbl_secattr_destroy(struct netlbl_lsm_secattr *secattr)
192 {
193 	if (secattr->cache)
194 		netlbl_secattr_cache_free(secattr->cache);
195 	kfree(secattr->domain);
196 	kfree(secattr->mls_cat);
197 }
198 
199 /**
200  * netlbl_secattr_alloc - Allocate and initialize a netlbl_lsm_secattr struct
201  * @flags: the memory allocation flags
202  *
203  * Description:
204  * Allocate and initialize a netlbl_lsm_secattr struct.  Returns a valid
205  * pointer on success, or NULL on failure.
206  *
207  */
208 static inline struct netlbl_lsm_secattr *netlbl_secattr_alloc(int flags)
209 {
210 	return kzalloc(sizeof(struct netlbl_lsm_secattr), flags);
211 }
212 
213 /**
214  * netlbl_secattr_free - Frees a netlbl_lsm_secattr struct
215  * @secattr: the struct to free
216  *
217  * Description:
218  * Frees @secattr including all of the internal buffers.
219  *
220  */
221 static inline void netlbl_secattr_free(struct netlbl_lsm_secattr *secattr)
222 {
223 	netlbl_secattr_destroy(secattr);
224 	kfree(secattr);
225 }
226 
227 /*
228  * LSM protocol operations
229  */
230 
231 #ifdef CONFIG_NETLABEL
232 int netlbl_socket_setattr(const struct socket *sock,
233 			  const struct netlbl_lsm_secattr *secattr);
234 int netlbl_sock_getattr(struct sock *sk,
235 			struct netlbl_lsm_secattr *secattr);
236 int netlbl_socket_getattr(const struct socket *sock,
237 			  struct netlbl_lsm_secattr *secattr);
238 int netlbl_skbuff_getattr(const struct sk_buff *skb,
239 			  struct netlbl_lsm_secattr *secattr);
240 void netlbl_skbuff_err(struct sk_buff *skb, int error);
241 #else
242 static inline int netlbl_socket_setattr(const struct socket *sock,
243 				     const struct netlbl_lsm_secattr *secattr)
244 {
245 	return -ENOSYS;
246 }
247 
248 static inline int netlbl_sock_getattr(struct sock *sk,
249 				      struct netlbl_lsm_secattr *secattr)
250 {
251 	return -ENOSYS;
252 }
253 
254 static inline int netlbl_socket_getattr(const struct socket *sock,
255 					struct netlbl_lsm_secattr *secattr)
256 {
257 	return -ENOSYS;
258 }
259 
260 static inline int netlbl_skbuff_getattr(const struct sk_buff *skb,
261 					struct netlbl_lsm_secattr *secattr)
262 {
263 	return -ENOSYS;
264 }
265 
266 static inline void netlbl_skbuff_err(struct sk_buff *skb, int error)
267 {
268 	return;
269 }
270 #endif /* CONFIG_NETLABEL */
271 
272 /*
273  * LSM label mapping cache operations
274  */
275 
276 #ifdef CONFIG_NETLABEL
277 void netlbl_cache_invalidate(void);
278 int netlbl_cache_add(const struct sk_buff *skb,
279 		     const struct netlbl_lsm_secattr *secattr);
280 #else
281 static inline void netlbl_cache_invalidate(void)
282 {
283 	return;
284 }
285 
286 static inline int netlbl_cache_add(const struct sk_buff *skb,
287 				   const struct netlbl_lsm_secattr *secattr)
288 {
289 	return 0;
290 }
291 #endif /* CONFIG_NETLABEL */
292 
293 #endif /* _NETLABEL_H */
294