blob: 8a113f91b7a1a2121da9374f2e58eb5913e8f882 [file] [log] [blame]
Huw Daviescb72d382016-06-27 15:02:46 -04001/*
2 * NetLabel CALIPSO/IPv6 Support
3 *
4 * This file defines the CALIPSO/IPv6 functions for the NetLabel system. The
5 * NetLabel system manages static and dynamic label mappings for network
6 * protocols such as CIPSO and CALIPSO.
7 *
8 * Authors: Paul Moore <paul@paul-moore.com>
9 * Huw Davies <huw@codeweavers.com>
10 *
11 */
12
13/* (c) Copyright Hewlett-Packard Development Company, L.P., 2006
14 * (c) Copyright Huw Davies <huw@codeweavers.com>, 2015
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
24 * the GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 *
29 */
30
31#include <linux/types.h>
32#include <linux/socket.h>
33#include <linux/string.h>
34#include <linux/skbuff.h>
35#include <linux/audit.h>
36#include <linux/slab.h>
37#include <net/sock.h>
38#include <net/netlink.h>
39#include <net/genetlink.h>
40#include <net/netlabel.h>
41#include <net/calipso.h>
42#include <linux/atomic.h>
43
44#include "netlabel_user.h"
45#include "netlabel_calipso.h"
46#include "netlabel_mgmt.h"
47#include "netlabel_domainhash.h"
48
49/* NetLabel Generic NETLINK CALIPSO family */
50static struct genl_family netlbl_calipso_gnl_family = {
51 .id = GENL_ID_GENERATE,
52 .hdrsize = 0,
53 .name = NETLBL_NLTYPE_CALIPSO_NAME,
54 .version = NETLBL_PROTO_VERSION,
55 .maxattr = NLBL_CALIPSO_A_MAX,
56};
57
58/* NetLabel Netlink attribute policy */
59static const struct nla_policy calipso_genl_policy[NLBL_CALIPSO_A_MAX + 1] = {
60 [NLBL_CALIPSO_A_DOI] = { .type = NLA_U32 },
61 [NLBL_CALIPSO_A_MTYPE] = { .type = NLA_U32 },
62};
63
64/* NetLabel Command Handlers
65 */
66/**
67 * netlbl_calipso_add_pass - Adds a CALIPSO pass DOI definition
68 * @info: the Generic NETLINK info block
69 * @audit_info: NetLabel audit information
70 *
71 * Description:
72 * Create a new CALIPSO_MAP_PASS DOI definition based on the given ADD message
73 * and add it to the CALIPSO engine. Return zero on success and non-zero on
74 * error.
75 *
76 */
77static int netlbl_calipso_add_pass(struct genl_info *info,
78 struct netlbl_audit *audit_info)
79{
80 int ret_val;
81 struct calipso_doi *doi_def = NULL;
82
83 doi_def = kmalloc(sizeof(*doi_def), GFP_KERNEL);
84 if (!doi_def)
85 return -ENOMEM;
86 doi_def->type = CALIPSO_MAP_PASS;
87 doi_def->doi = nla_get_u32(info->attrs[NLBL_CALIPSO_A_DOI]);
88 ret_val = calipso_doi_add(doi_def, audit_info);
89 if (ret_val != 0)
90 calipso_doi_free(doi_def);
91
92 return ret_val;
93}
94
95/**
96 * netlbl_calipso_add - Handle an ADD message
97 * @skb: the NETLINK buffer
98 * @info: the Generic NETLINK info block
99 *
100 * Description:
101 * Create a new DOI definition based on the given ADD message and add it to the
102 * CALIPSO engine. Returns zero on success, negative values on failure.
103 *
104 */
105static int netlbl_calipso_add(struct sk_buff *skb, struct genl_info *info)
106
107{
108 int ret_val = -EINVAL;
109 struct netlbl_audit audit_info;
110
111 if (!info->attrs[NLBL_CALIPSO_A_DOI] ||
112 !info->attrs[NLBL_CALIPSO_A_MTYPE])
113 return -EINVAL;
114
115 netlbl_netlink_auditinfo(skb, &audit_info);
116 switch (nla_get_u32(info->attrs[NLBL_CALIPSO_A_MTYPE])) {
117 case CALIPSO_MAP_PASS:
118 ret_val = netlbl_calipso_add_pass(info, &audit_info);
119 break;
120 }
121 if (ret_val == 0)
122 atomic_inc(&netlabel_mgmt_protocount);
123
124 return ret_val;
125}
126
127/* NetLabel Generic NETLINK Command Definitions
128 */
129
130static const struct genl_ops netlbl_calipso_ops[] = {
131 {
132 .cmd = NLBL_CALIPSO_C_ADD,
133 .flags = GENL_ADMIN_PERM,
134 .policy = calipso_genl_policy,
135 .doit = netlbl_calipso_add,
136 .dumpit = NULL,
137 },
138};
139
140/* NetLabel Generic NETLINK Protocol Functions
141 */
142
143/**
144 * netlbl_calipso_genl_init - Register the CALIPSO NetLabel component
145 *
146 * Description:
147 * Register the CALIPSO packet NetLabel component with the Generic NETLINK
148 * mechanism. Returns zero on success, negative values on failure.
149 *
150 */
151int __init netlbl_calipso_genl_init(void)
152{
153 return genl_register_family_with_ops(&netlbl_calipso_gnl_family,
154 netlbl_calipso_ops);
155}
156
157static const struct netlbl_calipso_ops *calipso_ops;
158
159/**
160 * netlbl_calipso_ops_register - Register the CALIPSO operations
161 *
162 * Description:
163 * Register the CALIPSO packet engine operations.
164 *
165 */
166const struct netlbl_calipso_ops *
167netlbl_calipso_ops_register(const struct netlbl_calipso_ops *ops)
168{
169 return xchg(&calipso_ops, ops);
170}
171EXPORT_SYMBOL(netlbl_calipso_ops_register);
172
173static const struct netlbl_calipso_ops *netlbl_calipso_ops_get(void)
174{
175 return ACCESS_ONCE(calipso_ops);
176}
177
178/**
179 * calipso_doi_add - Add a new DOI to the CALIPSO protocol engine
180 * @doi_def: the DOI structure
181 * @audit_info: NetLabel audit information
182 *
183 * Description:
184 * The caller defines a new DOI for use by the CALIPSO engine and calls this
185 * function to add it to the list of acceptable domains. The caller must
186 * ensure that the mapping table specified in @doi_def->map meets all of the
187 * requirements of the mapping type (see calipso.h for details). Returns
188 * zero on success and non-zero on failure.
189 *
190 */
191int calipso_doi_add(struct calipso_doi *doi_def,
192 struct netlbl_audit *audit_info)
193{
194 int ret_val = -ENOMSG;
195 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
196
197 if (ops)
198 ret_val = ops->doi_add(doi_def, audit_info);
199 return ret_val;
200}
201
202/**
203 * calipso_doi_free - Frees a DOI definition
204 * @doi_def: the DOI definition
205 *
206 * Description:
207 * This function frees all of the memory associated with a DOI definition.
208 *
209 */
210void calipso_doi_free(struct calipso_doi *doi_def)
211{
212 const struct netlbl_calipso_ops *ops = netlbl_calipso_ops_get();
213
214 if (ops)
215 ops->doi_free(doi_def);
216}