4 * The NetLabel system manages static and dynamic label mappings for network
5 * protocols such as CIPSO and RIPSO.
7 * Author: Paul Moore <paul.moore@hp.com>
12 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
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.
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.
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
33 #include <linux/types.h>
34 #include <linux/skbuff.h>
35 #include <net/netlink.h>
38 * NetLabel - A management interface for maintaining network packet label
39 * mapping tables for explicit packet labling protocols.
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.
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:
51 * +-----------------+-------------------+--------- --- -- -
52 * | struct nlmsghdr | struct genlmsghdr | payload
53 * +-----------------+-------------------+--------- --- -- -
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.
66 * NetLabel NETLINK protocol
69 #define NETLBL_PROTO_VERSION 1
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"
84 /* NetLabel return codes */
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))
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()
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.
108 static inline struct sk_buff *netlbl_netlink_alloc_skb(size_t head,
114 skb = alloc_skb(NLMSG_ALIGN(head + body), gfp_flags);
118 skb_reserve(skb, head);
119 if (skb_tailroom(skb) < body) {
129 * NetLabel - Kernel API for accessing the network packet label mappings.
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.
139 /* Domain mapping definition struct */
140 struct netlbl_dom_map;
142 /* Domain mapping operations */
143 int netlbl_domhsh_remove(const char *domain);
145 /* LSM security attributes */
146 struct netlbl_lsm_cache {
147 void (*free) (const void *data);
150 struct netlbl_lsm_secattr {
155 unsigned char *mls_cat;
158 struct netlbl_lsm_cache cache;
162 * LSM security attribute operations
167 * netlbl_secattr_init - Initialize a netlbl_lsm_secattr struct
168 * @secattr: the struct to initialize
171 * Initialize an already allocated netlbl_lsm_secattr struct. Returns zero on
172 * success, negative values on error.
175 static inline int netlbl_secattr_init(struct netlbl_lsm_secattr *secattr)
177 memset(secattr, 0, sizeof(*secattr));
182 * netlbl_secattr_destroy - Clears a netlbl_lsm_secattr struct
183 * @secattr: the struct to clear
184 * @clear_cache: cache clear flag
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()
193 static inline void netlbl_secattr_destroy(struct netlbl_lsm_secattr *secattr,
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);
203 * netlbl_secattr_alloc - Allocate and initialize a netlbl_lsm_secattr struct
204 * @flags: the memory allocation flags
207 * Allocate and initialize a netlbl_lsm_secattr struct. Returns a valid
208 * pointer on success, or NULL on failure.
211 static inline struct netlbl_lsm_secattr *netlbl_secattr_alloc(int flags)
213 return kzalloc(sizeof(struct netlbl_lsm_secattr), flags);
217 * netlbl_secattr_free - Frees a netlbl_lsm_secattr struct
218 * @secattr: the struct to free
219 * @clear_cache: cache clear flag
222 * Frees @secattr including all of the internal buffers. If @clear_cache is
223 * true then free the cache fields, otherwise leave them intact.
226 static inline void netlbl_secattr_free(struct netlbl_lsm_secattr *secattr,
229 netlbl_secattr_destroy(secattr, clear_cache);
234 * LSM protocol operations
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);
246 static inline int netlbl_socket_setattr(const struct socket *sock,
247 const struct netlbl_lsm_secattr *secattr)
252 static inline int netlbl_socket_getattr(const struct socket *sock,
253 struct netlbl_lsm_secattr *secattr)
258 static inline int netlbl_skbuff_getattr(const struct sk_buff *skb,
259 struct netlbl_lsm_secattr *secattr)
264 static inline void netlbl_skbuff_err(struct sk_buff *skb, int error)
268 #endif /* CONFIG_NETLABEL */
271 * LSM label mapping cache operations
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);
279 static inline void netlbl_cache_invalidate(void)
284 static inline int netlbl_cache_add(const struct sk_buff *skb,
285 const struct netlbl_lsm_secattr *secattr)
289 #endif /* CONFIG_NETLABEL */
291 #endif /* _NETLABEL_H */