]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/net/netlabel.h
NetLabel: change netlbl_secattr_init() to return void
[mirror_ubuntu-bionic-kernel.git] / include / net / netlabel.h
CommitLineData
11a03f78
PM
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>
7a0e1d60 34#include <linux/net.h>
11a03f78
PM
35#include <linux/skbuff.h>
36#include <net/netlink.h>
ffb733c6 37#include <asm/atomic.h>
11a03f78
PM
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
fcd48280
PM
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.
11a03f78
PM
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
11a03f78
PM
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
95d4e6be
PM
96/* NetLabel audit information */
97struct netlbl_audit {
98 u32 secid;
99 uid_t loginuid;
100};
101
11a03f78
PM
102/* Domain mapping definition struct */
103struct netlbl_dom_map;
104
105/* Domain mapping operations */
95d4e6be 106int netlbl_domhsh_remove(const char *domain, struct netlbl_audit *audit_info);
11a03f78
PM
107
108/* LSM security attributes */
109struct netlbl_lsm_cache {
ffb733c6 110 atomic_t refcount;
11a03f78
PM
111 void (*free) (const void *data);
112 void *data;
113};
114struct 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
ffb733c6 122 struct netlbl_lsm_cache *cache;
11a03f78
PM
123};
124
125/*
126 * LSM security attribute operations
127 */
128
129
ffb733c6 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 */
645408d1 139static inline struct netlbl_lsm_cache *netlbl_secattr_cache_alloc(gfp_t flags)
ffb733c6 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 */
157static 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
11a03f78
PM
167/**
168 * netlbl_secattr_init - Initialize a netlbl_lsm_secattr struct
169 * @secattr: the struct to initialize
170 *
171 * Description:
c6fa82a9 172 * Initialize an already allocated netlbl_lsm_secattr struct.
11a03f78
PM
173 *
174 */
c6fa82a9 175static inline void netlbl_secattr_init(struct netlbl_lsm_secattr *secattr)
11a03f78
PM
176{
177 memset(secattr, 0, sizeof(*secattr));
11a03f78
PM
178}
179
180/**
181 * netlbl_secattr_destroy - Clears a netlbl_lsm_secattr struct
182 * @secattr: the struct to clear
11a03f78
PM
183 *
184 * Description:
185 * Destroys the @secattr struct, including freeing all of the internal buffers.
ffb733c6 186 * The struct must be reset with a call to netlbl_secattr_init() before reuse.
11a03f78
PM
187 *
188 */
ffb733c6 189static inline void netlbl_secattr_destroy(struct netlbl_lsm_secattr *secattr)
11a03f78 190{
ffb733c6 191 if (secattr->cache)
192 netlbl_secattr_cache_free(secattr->cache);
11a03f78
PM
193 kfree(secattr->domain);
194 kfree(secattr->mls_cat);
195}
196
197/**
198 * netlbl_secattr_alloc - Allocate and initialize a netlbl_lsm_secattr struct
199 * @flags: the memory allocation flags
200 *
201 * Description:
202 * Allocate and initialize a netlbl_lsm_secattr struct. Returns a valid
203 * pointer on success, or NULL on failure.
204 *
205 */
1f758d93 206static inline struct netlbl_lsm_secattr *netlbl_secattr_alloc(gfp_t flags)
11a03f78
PM
207{
208 return kzalloc(sizeof(struct netlbl_lsm_secattr), flags);
209}
210
211/**
212 * netlbl_secattr_free - Frees a netlbl_lsm_secattr struct
213 * @secattr: the struct to free
11a03f78
PM
214 *
215 * Description:
ffb733c6 216 * Frees @secattr including all of the internal buffers.
11a03f78
PM
217 *
218 */
ffb733c6 219static inline void netlbl_secattr_free(struct netlbl_lsm_secattr *secattr)
11a03f78 220{
ffb733c6 221 netlbl_secattr_destroy(secattr);
11a03f78
PM
222 kfree(secattr);
223}
224
225/*
226 * LSM protocol operations
227 */
228
229#ifdef CONFIG_NETLABEL
230int netlbl_socket_setattr(const struct socket *sock,
231 const struct netlbl_lsm_secattr *secattr);
14a72f53
PM
232int netlbl_sock_getattr(struct sock *sk,
233 struct netlbl_lsm_secattr *secattr);
11a03f78
PM
234int netlbl_socket_getattr(const struct socket *sock,
235 struct netlbl_lsm_secattr *secattr);
236int netlbl_skbuff_getattr(const struct sk_buff *skb,
237 struct netlbl_lsm_secattr *secattr);
238void netlbl_skbuff_err(struct sk_buff *skb, int error);
239#else
240static inline int netlbl_socket_setattr(const struct socket *sock,
241 const struct netlbl_lsm_secattr *secattr)
242{
243 return -ENOSYS;
244}
245
14a72f53
PM
246static inline int netlbl_sock_getattr(struct sock *sk,
247 struct netlbl_lsm_secattr *secattr)
248{
249 return -ENOSYS;
250}
251
11a03f78
PM
252static inline int netlbl_socket_getattr(const struct socket *sock,
253 struct netlbl_lsm_secattr *secattr)
254{
255 return -ENOSYS;
256}
257
258static inline int netlbl_skbuff_getattr(const struct sk_buff *skb,
259 struct netlbl_lsm_secattr *secattr)
260{
261 return -ENOSYS;
262}
263
264static 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
275void netlbl_cache_invalidate(void);
276int netlbl_cache_add(const struct sk_buff *skb,
277 const struct netlbl_lsm_secattr *secattr);
278#else
279static inline void netlbl_cache_invalidate(void)
280{
281 return;
282}
283
284static 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 */