blob: b2aeb5d446016032238b6281c580ce313b262897 [file] [log] [blame]
Paul Moored15c3452006-08-03 16:48:37 -07001/*
2 * NetLabel Management Support
3 *
4 * This file defines the management functions for the NetLabel system. The
5 * NetLabel system manages static and dynamic label mappings for network
6 * protocols such as CIPSO and RIPSO.
7 *
Paul Moore82c21bf2011-08-01 11:10:33 +00008 * Author: Paul Moore <paul@paul-moore.com>
Paul Moored15c3452006-08-03 16:48:37 -07009 *
10 */
11
12/*
Paul Moore63c41682008-10-10 10:16:32 -040013 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
Paul Moored15c3452006-08-03 16:48:37 -070014 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
23 * the GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
Jeff Kirsherd484ff12013-12-06 09:13:41 -080026 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Paul Moored15c3452006-08-03 16:48:37 -070027 *
28 */
29
30#include <linux/types.h>
31#include <linux/socket.h>
32#include <linux/string.h>
33#include <linux/skbuff.h>
Paul Moore63c41682008-10-10 10:16:32 -040034#include <linux/in.h>
35#include <linux/in6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Paul Moored15c3452006-08-03 16:48:37 -070037#include <net/sock.h>
38#include <net/netlink.h>
39#include <net/genetlink.h>
Paul Moore63c41682008-10-10 10:16:32 -040040#include <net/ip.h>
41#include <net/ipv6.h>
Paul Moored15c3452006-08-03 16:48:37 -070042#include <net/netlabel.h>
43#include <net/cipso_ipv4.h>
Arun Sharma600634972011-07-26 16:09:06 -070044#include <linux/atomic.h>
Paul Moored15c3452006-08-03 16:48:37 -070045
46#include "netlabel_domainhash.h"
47#include "netlabel_user.h"
48#include "netlabel_mgmt.h"
49
Paul Moorec783f1c2008-01-29 08:37:52 -050050/* NetLabel configured protocol counter */
51atomic_t netlabel_mgmt_protocount = ATOMIC_INIT(0);
Paul Moore23bcdc12007-07-18 12:28:45 -040052
Paul Moorefd385852006-09-25 15:56:37 -070053/* Argument struct for netlbl_domhsh_walk() */
54struct netlbl_domhsh_walk_arg {
55 struct netlink_callback *nl_cb;
56 struct sk_buff *skb;
57 u32 seq;
58};
59
Paul Moored15c3452006-08-03 16:48:37 -070060/* NetLabel Generic NETLINK CIPSOv4 family */
61static struct genl_family netlbl_mgmt_gnl_family = {
62 .id = GENL_ID_GENERATE,
63 .hdrsize = 0,
64 .name = NETLBL_NLTYPE_MGMT_NAME,
65 .version = NETLBL_PROTO_VERSION,
Paul Moorefd385852006-09-25 15:56:37 -070066 .maxattr = NLBL_MGMT_A_MAX,
Paul Moored15c3452006-08-03 16:48:37 -070067};
68
Paul Moorefd385852006-09-25 15:56:37 -070069/* NetLabel Netlink attribute policy */
Patrick McHardyef7c79e2007-06-05 12:38:30 -070070static const struct nla_policy netlbl_mgmt_genl_policy[NLBL_MGMT_A_MAX + 1] = {
Paul Moorefd385852006-09-25 15:56:37 -070071 [NLBL_MGMT_A_DOMAIN] = { .type = NLA_NUL_STRING },
72 [NLBL_MGMT_A_PROTOCOL] = { .type = NLA_U32 },
73 [NLBL_MGMT_A_VERSION] = { .type = NLA_U32 },
74 [NLBL_MGMT_A_CV4DOI] = { .type = NLA_U32 },
Huw Davies8f18e672016-06-27 15:02:46 -040075 [NLBL_MGMT_A_FAMILY] = { .type = NLA_U16 },
Paul Moorefd385852006-09-25 15:56:37 -070076};
Paul Moored15c3452006-08-03 16:48:37 -070077
78/*
Paul Moore63c41682008-10-10 10:16:32 -040079 * Helper Functions
80 */
81
82/**
83 * netlbl_mgmt_add - Handle an ADD message
84 * @info: the Generic NETLINK info block
85 * @audit_info: NetLabel audit information
86 *
87 * Description:
88 * Helper function for the ADD and ADDDEF messages to add the domain mappings
89 * from the message to the hash table. See netlabel.h for a description of the
90 * message format. Returns zero on success, negative values on failure.
91 *
92 */
93static int netlbl_mgmt_add_common(struct genl_info *info,
94 struct netlbl_audit *audit_info)
95{
96 int ret_val = -EINVAL;
Paul Moore63c41682008-10-10 10:16:32 -040097 struct netlbl_domaddr_map *addrmap = NULL;
98 struct cipso_v4_doi *cipsov4 = NULL;
99 u32 tmp_val;
Markus Elfring4de46d52015-02-02 11:00:24 +0100100 struct netlbl_dom_map *entry = kzalloc(sizeof(*entry), GFP_KERNEL);
Paul Moore63c41682008-10-10 10:16:32 -0400101
Markus Elfring4de46d52015-02-02 11:00:24 +0100102 if (!entry)
103 return -ENOMEM;
Paul Moore6a8b7f02013-08-02 14:45:08 -0400104 entry->def.type = nla_get_u32(info->attrs[NLBL_MGMT_A_PROTOCOL]);
Paul Moore63c41682008-10-10 10:16:32 -0400105 if (info->attrs[NLBL_MGMT_A_DOMAIN]) {
106 size_t tmp_size = nla_len(info->attrs[NLBL_MGMT_A_DOMAIN]);
107 entry->domain = kmalloc(tmp_size, GFP_KERNEL);
108 if (entry->domain == NULL) {
109 ret_val = -ENOMEM;
Markus Elfring4de46d52015-02-02 11:00:24 +0100110 goto add_free_entry;
Paul Moore63c41682008-10-10 10:16:32 -0400111 }
112 nla_strlcpy(entry->domain,
113 info->attrs[NLBL_MGMT_A_DOMAIN], tmp_size);
114 }
115
Paul Moore6a8b7f02013-08-02 14:45:08 -0400116 /* NOTE: internally we allow/use a entry->def.type value of
Paul Moore63c41682008-10-10 10:16:32 -0400117 * NETLBL_NLTYPE_ADDRSELECT but we don't currently allow users
118 * to pass that as a protocol value because we need to know the
119 * "real" protocol */
120
Paul Moore6a8b7f02013-08-02 14:45:08 -0400121 switch (entry->def.type) {
Paul Moore63c41682008-10-10 10:16:32 -0400122 case NETLBL_NLTYPE_UNLABELED:
Huw Davies8f18e672016-06-27 15:02:46 -0400123 if (info->attrs[NLBL_MGMT_A_FAMILY])
124 entry->family =
125 nla_get_u16(info->attrs[NLBL_MGMT_A_FAMILY]);
126 else
127 entry->family = AF_UNSPEC;
Paul Moore63c41682008-10-10 10:16:32 -0400128 break;
129 case NETLBL_NLTYPE_CIPSOV4:
130 if (!info->attrs[NLBL_MGMT_A_CV4DOI])
Markus Elfring4de46d52015-02-02 11:00:24 +0100131 goto add_free_domain;
Paul Moore63c41682008-10-10 10:16:32 -0400132
133 tmp_val = nla_get_u32(info->attrs[NLBL_MGMT_A_CV4DOI]);
134 cipsov4 = cipso_v4_doi_getdef(tmp_val);
135 if (cipsov4 == NULL)
Markus Elfring4de46d52015-02-02 11:00:24 +0100136 goto add_free_domain;
Huw Davies8f18e672016-06-27 15:02:46 -0400137 entry->family = AF_INET;
Paul Moore6a8b7f02013-08-02 14:45:08 -0400138 entry->def.cipso = cipsov4;
Paul Moore63c41682008-10-10 10:16:32 -0400139 break;
140 default:
Markus Elfring4de46d52015-02-02 11:00:24 +0100141 goto add_free_domain;
Paul Moore63c41682008-10-10 10:16:32 -0400142 }
143
Huw Davies8f18e672016-06-27 15:02:46 -0400144 if ((entry->family == AF_INET && info->attrs[NLBL_MGMT_A_IPV6ADDR]) ||
145 (entry->family == AF_INET6 && info->attrs[NLBL_MGMT_A_IPV4ADDR]))
146 goto add_doi_put_def;
147
Paul Moore63c41682008-10-10 10:16:32 -0400148 if (info->attrs[NLBL_MGMT_A_IPV4ADDR]) {
149 struct in_addr *addr;
150 struct in_addr *mask;
151 struct netlbl_domaddr4_map *map;
152
153 addrmap = kzalloc(sizeof(*addrmap), GFP_KERNEL);
154 if (addrmap == NULL) {
155 ret_val = -ENOMEM;
Markus Elfring4de46d52015-02-02 11:00:24 +0100156 goto add_doi_put_def;
Paul Moore63c41682008-10-10 10:16:32 -0400157 }
158 INIT_LIST_HEAD(&addrmap->list4);
159 INIT_LIST_HEAD(&addrmap->list6);
160
161 if (nla_len(info->attrs[NLBL_MGMT_A_IPV4ADDR]) !=
162 sizeof(struct in_addr)) {
163 ret_val = -EINVAL;
Markus Elfring4de46d52015-02-02 11:00:24 +0100164 goto add_free_addrmap;
Paul Moore63c41682008-10-10 10:16:32 -0400165 }
166 if (nla_len(info->attrs[NLBL_MGMT_A_IPV4MASK]) !=
167 sizeof(struct in_addr)) {
168 ret_val = -EINVAL;
Markus Elfring4de46d52015-02-02 11:00:24 +0100169 goto add_free_addrmap;
Paul Moore63c41682008-10-10 10:16:32 -0400170 }
171 addr = nla_data(info->attrs[NLBL_MGMT_A_IPV4ADDR]);
172 mask = nla_data(info->attrs[NLBL_MGMT_A_IPV4MASK]);
173
174 map = kzalloc(sizeof(*map), GFP_KERNEL);
175 if (map == NULL) {
176 ret_val = -ENOMEM;
Markus Elfring4de46d52015-02-02 11:00:24 +0100177 goto add_free_addrmap;
Paul Moore63c41682008-10-10 10:16:32 -0400178 }
179 map->list.addr = addr->s_addr & mask->s_addr;
180 map->list.mask = mask->s_addr;
181 map->list.valid = 1;
Paul Moore6a8b7f02013-08-02 14:45:08 -0400182 map->def.type = entry->def.type;
Paul Moore63c41682008-10-10 10:16:32 -0400183 if (cipsov4)
Paul Moore6a8b7f02013-08-02 14:45:08 -0400184 map->def.cipso = cipsov4;
Paul Moore63c41682008-10-10 10:16:32 -0400185
186 ret_val = netlbl_af4list_add(&map->list, &addrmap->list4);
187 if (ret_val != 0) {
188 kfree(map);
Markus Elfring4de46d52015-02-02 11:00:24 +0100189 goto add_free_addrmap;
Paul Moore63c41682008-10-10 10:16:32 -0400190 }
191
Huw Davies8f18e672016-06-27 15:02:46 -0400192 entry->family = AF_INET;
Paul Moore6a8b7f02013-08-02 14:45:08 -0400193 entry->def.type = NETLBL_NLTYPE_ADDRSELECT;
194 entry->def.addrsel = addrmap;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000195#if IS_ENABLED(CONFIG_IPV6)
Paul Moore63c41682008-10-10 10:16:32 -0400196 } else if (info->attrs[NLBL_MGMT_A_IPV6ADDR]) {
197 struct in6_addr *addr;
198 struct in6_addr *mask;
199 struct netlbl_domaddr6_map *map;
200
201 addrmap = kzalloc(sizeof(*addrmap), GFP_KERNEL);
202 if (addrmap == NULL) {
203 ret_val = -ENOMEM;
Markus Elfring4de46d52015-02-02 11:00:24 +0100204 goto add_doi_put_def;
Paul Moore63c41682008-10-10 10:16:32 -0400205 }
206 INIT_LIST_HEAD(&addrmap->list4);
207 INIT_LIST_HEAD(&addrmap->list6);
208
209 if (nla_len(info->attrs[NLBL_MGMT_A_IPV6ADDR]) !=
210 sizeof(struct in6_addr)) {
211 ret_val = -EINVAL;
Markus Elfring4de46d52015-02-02 11:00:24 +0100212 goto add_free_addrmap;
Paul Moore63c41682008-10-10 10:16:32 -0400213 }
214 if (nla_len(info->attrs[NLBL_MGMT_A_IPV6MASK]) !=
215 sizeof(struct in6_addr)) {
216 ret_val = -EINVAL;
Markus Elfring4de46d52015-02-02 11:00:24 +0100217 goto add_free_addrmap;
Paul Moore63c41682008-10-10 10:16:32 -0400218 }
219 addr = nla_data(info->attrs[NLBL_MGMT_A_IPV6ADDR]);
220 mask = nla_data(info->attrs[NLBL_MGMT_A_IPV6MASK]);
221
222 map = kzalloc(sizeof(*map), GFP_KERNEL);
223 if (map == NULL) {
224 ret_val = -ENOMEM;
Markus Elfring4de46d52015-02-02 11:00:24 +0100225 goto add_free_addrmap;
Paul Moore63c41682008-10-10 10:16:32 -0400226 }
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000227 map->list.addr = *addr;
Paul Moore63c41682008-10-10 10:16:32 -0400228 map->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
229 map->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
230 map->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
231 map->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000232 map->list.mask = *mask;
Paul Moore63c41682008-10-10 10:16:32 -0400233 map->list.valid = 1;
Paul Moore6a8b7f02013-08-02 14:45:08 -0400234 map->def.type = entry->def.type;
Paul Moore63c41682008-10-10 10:16:32 -0400235
236 ret_val = netlbl_af6list_add(&map->list, &addrmap->list6);
237 if (ret_val != 0) {
238 kfree(map);
Markus Elfring4de46d52015-02-02 11:00:24 +0100239 goto add_free_addrmap;
Paul Moore63c41682008-10-10 10:16:32 -0400240 }
241
Huw Davies8f18e672016-06-27 15:02:46 -0400242 entry->family = AF_INET6;
Paul Moore6a8b7f02013-08-02 14:45:08 -0400243 entry->def.type = NETLBL_NLTYPE_ADDRSELECT;
244 entry->def.addrsel = addrmap;
Paul Moore63c41682008-10-10 10:16:32 -0400245#endif /* IPv6 */
246 }
247
248 ret_val = netlbl_domhsh_add(entry, audit_info);
249 if (ret_val != 0)
Markus Elfring4de46d52015-02-02 11:00:24 +0100250 goto add_free_addrmap;
Paul Moore63c41682008-10-10 10:16:32 -0400251
252 return 0;
253
Markus Elfring4de46d52015-02-02 11:00:24 +0100254add_free_addrmap:
Paul Moore63c41682008-10-10 10:16:32 -0400255 kfree(addrmap);
Markus Elfring4de46d52015-02-02 11:00:24 +0100256add_doi_put_def:
257 cipso_v4_doi_putdef(cipsov4);
258add_free_domain:
259 kfree(entry->domain);
260add_free_entry:
Paul Moore63c41682008-10-10 10:16:32 -0400261 kfree(entry);
262 return ret_val;
263}
264
265/**
266 * netlbl_mgmt_listentry - List a NetLabel/LSM domain map entry
267 * @skb: the NETLINK buffer
268 * @entry: the map entry
269 *
270 * Description:
271 * This function is a helper function used by the LISTALL and LISTDEF command
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300272 * handlers. The caller is responsible for ensuring that the RCU read lock
Paul Moore63c41682008-10-10 10:16:32 -0400273 * is held. Returns zero on success, negative values on failure.
274 *
275 */
276static int netlbl_mgmt_listentry(struct sk_buff *skb,
277 struct netlbl_dom_map *entry)
278{
Paul Mooref8a02472008-10-29 16:09:12 -0400279 int ret_val = 0;
Paul Moore63c41682008-10-10 10:16:32 -0400280 struct nlattr *nla_a;
281 struct nlattr *nla_b;
282 struct netlbl_af4list *iter4;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000283#if IS_ENABLED(CONFIG_IPV6)
Paul Moore63c41682008-10-10 10:16:32 -0400284 struct netlbl_af6list *iter6;
285#endif
286
287 if (entry->domain != NULL) {
288 ret_val = nla_put_string(skb,
289 NLBL_MGMT_A_DOMAIN, entry->domain);
290 if (ret_val != 0)
291 return ret_val;
292 }
293
Huw Davies8f18e672016-06-27 15:02:46 -0400294 ret_val = nla_put_u16(skb, NLBL_MGMT_A_FAMILY, entry->family);
295 if (ret_val != 0)
296 return ret_val;
297
Paul Moore6a8b7f02013-08-02 14:45:08 -0400298 switch (entry->def.type) {
Paul Moore63c41682008-10-10 10:16:32 -0400299 case NETLBL_NLTYPE_ADDRSELECT:
300 nla_a = nla_nest_start(skb, NLBL_MGMT_A_SELECTORLIST);
301 if (nla_a == NULL)
302 return -ENOMEM;
303
Paul Moore6a8b7f02013-08-02 14:45:08 -0400304 netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4) {
Paul Moore63c41682008-10-10 10:16:32 -0400305 struct netlbl_domaddr4_map *map4;
306 struct in_addr addr_struct;
307
308 nla_b = nla_nest_start(skb, NLBL_MGMT_A_ADDRSELECTOR);
309 if (nla_b == NULL)
310 return -ENOMEM;
311
312 addr_struct.s_addr = iter4->addr;
Jiri Benc930345e2015-03-29 16:59:25 +0200313 ret_val = nla_put_in_addr(skb, NLBL_MGMT_A_IPV4ADDR,
314 addr_struct.s_addr);
Paul Moore63c41682008-10-10 10:16:32 -0400315 if (ret_val != 0)
316 return ret_val;
317 addr_struct.s_addr = iter4->mask;
Jiri Benc930345e2015-03-29 16:59:25 +0200318 ret_val = nla_put_in_addr(skb, NLBL_MGMT_A_IPV4MASK,
319 addr_struct.s_addr);
Paul Moore63c41682008-10-10 10:16:32 -0400320 if (ret_val != 0)
321 return ret_val;
322 map4 = netlbl_domhsh_addr4_entry(iter4);
323 ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL,
Paul Moore6a8b7f02013-08-02 14:45:08 -0400324 map4->def.type);
Paul Moore63c41682008-10-10 10:16:32 -0400325 if (ret_val != 0)
326 return ret_val;
Paul Moore6a8b7f02013-08-02 14:45:08 -0400327 switch (map4->def.type) {
Paul Moore63c41682008-10-10 10:16:32 -0400328 case NETLBL_NLTYPE_CIPSOV4:
329 ret_val = nla_put_u32(skb, NLBL_MGMT_A_CV4DOI,
Paul Moore6a8b7f02013-08-02 14:45:08 -0400330 map4->def.cipso->doi);
Paul Moore63c41682008-10-10 10:16:32 -0400331 if (ret_val != 0)
332 return ret_val;
333 break;
334 }
335
336 nla_nest_end(skb, nla_b);
337 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000338#if IS_ENABLED(CONFIG_IPV6)
Paul Moore6a8b7f02013-08-02 14:45:08 -0400339 netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6) {
Paul Moore63c41682008-10-10 10:16:32 -0400340 struct netlbl_domaddr6_map *map6;
341
342 nla_b = nla_nest_start(skb, NLBL_MGMT_A_ADDRSELECTOR);
343 if (nla_b == NULL)
344 return -ENOMEM;
345
Jiri Benc930345e2015-03-29 16:59:25 +0200346 ret_val = nla_put_in6_addr(skb, NLBL_MGMT_A_IPV6ADDR,
347 &iter6->addr);
Paul Moore63c41682008-10-10 10:16:32 -0400348 if (ret_val != 0)
349 return ret_val;
Jiri Benc930345e2015-03-29 16:59:25 +0200350 ret_val = nla_put_in6_addr(skb, NLBL_MGMT_A_IPV6MASK,
351 &iter6->mask);
Paul Moore63c41682008-10-10 10:16:32 -0400352 if (ret_val != 0)
353 return ret_val;
354 map6 = netlbl_domhsh_addr6_entry(iter6);
355 ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL,
Paul Moore6a8b7f02013-08-02 14:45:08 -0400356 map6->def.type);
Paul Moore63c41682008-10-10 10:16:32 -0400357 if (ret_val != 0)
358 return ret_val;
359
360 nla_nest_end(skb, nla_b);
361 }
362#endif /* IPv6 */
363
364 nla_nest_end(skb, nla_a);
365 break;
366 case NETLBL_NLTYPE_UNLABELED:
Paul Moore6a8b7f02013-08-02 14:45:08 -0400367 ret_val = nla_put_u32(skb,NLBL_MGMT_A_PROTOCOL,entry->def.type);
Paul Moore63c41682008-10-10 10:16:32 -0400368 break;
369 case NETLBL_NLTYPE_CIPSOV4:
Paul Moore6a8b7f02013-08-02 14:45:08 -0400370 ret_val = nla_put_u32(skb,NLBL_MGMT_A_PROTOCOL,entry->def.type);
Paul Moore63c41682008-10-10 10:16:32 -0400371 if (ret_val != 0)
372 return ret_val;
373 ret_val = nla_put_u32(skb, NLBL_MGMT_A_CV4DOI,
Paul Moore6a8b7f02013-08-02 14:45:08 -0400374 entry->def.cipso->doi);
Paul Moore63c41682008-10-10 10:16:32 -0400375 break;
376 }
377
378 return ret_val;
379}
380
381/*
Paul Moored15c3452006-08-03 16:48:37 -0700382 * NetLabel Command Handlers
383 */
384
385/**
386 * netlbl_mgmt_add - Handle an ADD message
387 * @skb: the NETLINK buffer
388 * @info: the Generic NETLINK info block
389 *
390 * Description:
391 * Process a user generated ADD message and add the domains from the message
392 * to the hash table. See netlabel.h for a description of the message format.
393 * Returns zero on success, negative values on failure.
394 *
395 */
396static int netlbl_mgmt_add(struct sk_buff *skb, struct genl_info *info)
397{
Paul Moore95d4e6b2006-09-29 17:05:05 -0700398 struct netlbl_audit audit_info;
Paul Moored15c3452006-08-03 16:48:37 -0700399
Paul Moore63c41682008-10-10 10:16:32 -0400400 if ((!info->attrs[NLBL_MGMT_A_DOMAIN]) ||
401 (!info->attrs[NLBL_MGMT_A_PROTOCOL]) ||
402 (info->attrs[NLBL_MGMT_A_IPV4ADDR] &&
403 info->attrs[NLBL_MGMT_A_IPV6ADDR]) ||
404 (info->attrs[NLBL_MGMT_A_IPV4MASK] &&
405 info->attrs[NLBL_MGMT_A_IPV6MASK]) ||
406 ((info->attrs[NLBL_MGMT_A_IPV4ADDR] != NULL) ^
407 (info->attrs[NLBL_MGMT_A_IPV4MASK] != NULL)) ||
408 ((info->attrs[NLBL_MGMT_A_IPV6ADDR] != NULL) ^
409 (info->attrs[NLBL_MGMT_A_IPV6MASK] != NULL)))
410 return -EINVAL;
Paul Moorefd385852006-09-25 15:56:37 -0700411
Paul Moore95d4e6b2006-09-29 17:05:05 -0700412 netlbl_netlink_auditinfo(skb, &audit_info);
413
Paul Moore63c41682008-10-10 10:16:32 -0400414 return netlbl_mgmt_add_common(info, &audit_info);
Paul Moored15c3452006-08-03 16:48:37 -0700415}
416
417/**
418 * netlbl_mgmt_remove - Handle a REMOVE message
419 * @skb: the NETLINK buffer
420 * @info: the Generic NETLINK info block
421 *
422 * Description:
423 * Process a user generated REMOVE message and remove the specified domain
424 * mappings. Returns zero on success, negative values on failure.
425 *
426 */
427static int netlbl_mgmt_remove(struct sk_buff *skb, struct genl_info *info)
428{
Paul Moorefd385852006-09-25 15:56:37 -0700429 char *domain;
Paul Moore95d4e6b2006-09-29 17:05:05 -0700430 struct netlbl_audit audit_info;
Paul Moored15c3452006-08-03 16:48:37 -0700431
Paul Moorefd385852006-09-25 15:56:37 -0700432 if (!info->attrs[NLBL_MGMT_A_DOMAIN])
433 return -EINVAL;
434
Paul Moore95d4e6b2006-09-29 17:05:05 -0700435 netlbl_netlink_auditinfo(skb, &audit_info);
436
Paul Moorefd385852006-09-25 15:56:37 -0700437 domain = nla_data(info->attrs[NLBL_MGMT_A_DOMAIN]);
Huw Davies8f18e672016-06-27 15:02:46 -0400438 return netlbl_domhsh_remove(domain, AF_UNSPEC, &audit_info);
Paul Moorefd385852006-09-25 15:56:37 -0700439}
440
441/**
442 * netlbl_mgmt_listall_cb - netlbl_domhsh_walk() callback for LISTALL
443 * @entry: the domain mapping hash table entry
444 * @arg: the netlbl_domhsh_walk_arg structure
445 *
446 * Description:
447 * This function is designed to be used as a callback to the
448 * netlbl_domhsh_walk() function for use in generating a response for a LISTALL
449 * message. Returns the size of the message on success, negative values on
450 * failure.
451 *
452 */
453static int netlbl_mgmt_listall_cb(struct netlbl_dom_map *entry, void *arg)
454{
455 int ret_val = -ENOMEM;
456 struct netlbl_domhsh_walk_arg *cb_arg = arg;
457 void *data;
458
Eric W. Biederman15e47302012-09-07 20:12:54 +0000459 data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).portid,
Thomas Graf17c157c2006-11-14 19:46:02 -0800460 cb_arg->seq, &netlbl_mgmt_gnl_family,
461 NLM_F_MULTI, NLBL_MGMT_C_LISTALL);
Paul Moorefd385852006-09-25 15:56:37 -0700462 if (data == NULL)
463 goto listall_cb_failure;
464
Paul Moore63c41682008-10-10 10:16:32 -0400465 ret_val = netlbl_mgmt_listentry(cb_arg->skb, entry);
Paul Moored15c3452006-08-03 16:48:37 -0700466 if (ret_val != 0)
Paul Moorefd385852006-09-25 15:56:37 -0700467 goto listall_cb_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700468
Paul Moorefd385852006-09-25 15:56:37 -0700469 cb_arg->seq++;
Johannes Berg053c0952015-01-16 22:09:00 +0100470 genlmsg_end(cb_arg->skb, data);
471 return 0;
Paul Moored15c3452006-08-03 16:48:37 -0700472
Paul Moorefd385852006-09-25 15:56:37 -0700473listall_cb_failure:
474 genlmsg_cancel(cb_arg->skb, data);
Paul Moored15c3452006-08-03 16:48:37 -0700475 return ret_val;
476}
477
478/**
Paul Moorefd385852006-09-25 15:56:37 -0700479 * netlbl_mgmt_listall - Handle a LISTALL message
Paul Moored15c3452006-08-03 16:48:37 -0700480 * @skb: the NETLINK buffer
Paul Moorefd385852006-09-25 15:56:37 -0700481 * @cb: the NETLINK callback
Paul Moored15c3452006-08-03 16:48:37 -0700482 *
483 * Description:
Paul Moorefd385852006-09-25 15:56:37 -0700484 * Process a user generated LISTALL message and dumps the domain hash table in
485 * a form suitable for use in a kernel generated LISTALL message. Returns zero
486 * on success, negative values on failure.
Paul Moored15c3452006-08-03 16:48:37 -0700487 *
488 */
Paul Moorefd385852006-09-25 15:56:37 -0700489static int netlbl_mgmt_listall(struct sk_buff *skb,
490 struct netlink_callback *cb)
Paul Moored15c3452006-08-03 16:48:37 -0700491{
Paul Moorefd385852006-09-25 15:56:37 -0700492 struct netlbl_domhsh_walk_arg cb_arg;
493 u32 skip_bkt = cb->args[0];
494 u32 skip_chain = cb->args[1];
Paul Moored15c3452006-08-03 16:48:37 -0700495
Paul Moorefd385852006-09-25 15:56:37 -0700496 cb_arg.nl_cb = cb;
497 cb_arg.skb = skb;
498 cb_arg.seq = cb->nlh->nlmsg_seq;
Paul Moored15c3452006-08-03 16:48:37 -0700499
Paul Moorefd385852006-09-25 15:56:37 -0700500 netlbl_domhsh_walk(&skip_bkt,
501 &skip_chain,
502 netlbl_mgmt_listall_cb,
503 &cb_arg);
Paul Moored15c3452006-08-03 16:48:37 -0700504
Paul Moorefd385852006-09-25 15:56:37 -0700505 cb->args[0] = skip_bkt;
506 cb->args[1] = skip_chain;
507 return skb->len;
Paul Moored15c3452006-08-03 16:48:37 -0700508}
509
510/**
511 * netlbl_mgmt_adddef - Handle an ADDDEF message
512 * @skb: the NETLINK buffer
513 * @info: the Generic NETLINK info block
514 *
515 * Description:
516 * Process a user generated ADDDEF message and respond accordingly. Returns
517 * zero on success, negative values on failure.
518 *
519 */
520static int netlbl_mgmt_adddef(struct sk_buff *skb, struct genl_info *info)
521{
Paul Moore95d4e6b2006-09-29 17:05:05 -0700522 struct netlbl_audit audit_info;
Paul Moored15c3452006-08-03 16:48:37 -0700523
Paul Moore63c41682008-10-10 10:16:32 -0400524 if ((!info->attrs[NLBL_MGMT_A_PROTOCOL]) ||
525 (info->attrs[NLBL_MGMT_A_IPV4ADDR] &&
526 info->attrs[NLBL_MGMT_A_IPV6ADDR]) ||
527 (info->attrs[NLBL_MGMT_A_IPV4MASK] &&
528 info->attrs[NLBL_MGMT_A_IPV6MASK]) ||
529 ((info->attrs[NLBL_MGMT_A_IPV4ADDR] != NULL) ^
530 (info->attrs[NLBL_MGMT_A_IPV4MASK] != NULL)) ||
531 ((info->attrs[NLBL_MGMT_A_IPV6ADDR] != NULL) ^
532 (info->attrs[NLBL_MGMT_A_IPV6MASK] != NULL)))
533 return -EINVAL;
Paul Moored15c3452006-08-03 16:48:37 -0700534
Paul Moore95d4e6b2006-09-29 17:05:05 -0700535 netlbl_netlink_auditinfo(skb, &audit_info);
536
Paul Moore63c41682008-10-10 10:16:32 -0400537 return netlbl_mgmt_add_common(info, &audit_info);
Paul Moored15c3452006-08-03 16:48:37 -0700538}
539
540/**
541 * netlbl_mgmt_removedef - Handle a REMOVEDEF message
542 * @skb: the NETLINK buffer
543 * @info: the Generic NETLINK info block
544 *
545 * Description:
546 * Process a user generated REMOVEDEF message and remove the default domain
547 * mapping. Returns zero on success, negative values on failure.
548 *
549 */
550static int netlbl_mgmt_removedef(struct sk_buff *skb, struct genl_info *info)
551{
Paul Moore95d4e6b2006-09-29 17:05:05 -0700552 struct netlbl_audit audit_info;
553
554 netlbl_netlink_auditinfo(skb, &audit_info);
555
Huw Davies8f18e672016-06-27 15:02:46 -0400556 return netlbl_domhsh_remove_default(AF_UNSPEC, &audit_info);
Paul Moored15c3452006-08-03 16:48:37 -0700557}
558
559/**
560 * netlbl_mgmt_listdef - Handle a LISTDEF message
561 * @skb: the NETLINK buffer
562 * @info: the Generic NETLINK info block
563 *
564 * Description:
565 * Process a user generated LISTDEF message and dumps the default domain
566 * mapping in a form suitable for use in a kernel generated LISTDEF message.
567 * Returns zero on success, negative values on failure.
568 *
569 */
570static int netlbl_mgmt_listdef(struct sk_buff *skb, struct genl_info *info)
571{
572 int ret_val = -ENOMEM;
Paul Moorefd385852006-09-25 15:56:37 -0700573 struct sk_buff *ans_skb = NULL;
574 void *data;
575 struct netlbl_dom_map *entry;
Huw Davies8f18e672016-06-27 15:02:46 -0400576 u16 family;
577
578 if (info->attrs[NLBL_MGMT_A_FAMILY])
579 family = nla_get_u16(info->attrs[NLBL_MGMT_A_FAMILY]);
580 else
581 family = AF_INET;
Paul Moored15c3452006-08-03 16:48:37 -0700582
Thomas Graf339bf982006-11-10 14:10:15 -0800583 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Paul Moored15c3452006-08-03 16:48:37 -0700584 if (ans_skb == NULL)
Paul Moorefd385852006-09-25 15:56:37 -0700585 return -ENOMEM;
Thomas Graf17c157c2006-11-14 19:46:02 -0800586 data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family,
587 0, NLBL_MGMT_C_LISTDEF);
Paul Moorefd385852006-09-25 15:56:37 -0700588 if (data == NULL)
Paul Moored15c3452006-08-03 16:48:37 -0700589 goto listdef_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700590
Paul Moorefd385852006-09-25 15:56:37 -0700591 rcu_read_lock();
Huw Davies8f18e672016-06-27 15:02:46 -0400592 entry = netlbl_domhsh_getentry(NULL, family);
Paul Moorefd385852006-09-25 15:56:37 -0700593 if (entry == NULL) {
594 ret_val = -ENOENT;
595 goto listdef_failure_lock;
596 }
Paul Moore63c41682008-10-10 10:16:32 -0400597 ret_val = netlbl_mgmt_listentry(ans_skb, entry);
Paul Moorefd385852006-09-25 15:56:37 -0700598 rcu_read_unlock();
Paul Moore63c41682008-10-10 10:16:32 -0400599 if (ret_val != 0)
600 goto listdef_failure;
Paul Moorefd385852006-09-25 15:56:37 -0700601
602 genlmsg_end(ans_skb, data);
Denis V. Lunevfe785be2008-07-10 16:53:39 -0700603 return genlmsg_reply(ans_skb, info);
Paul Moored15c3452006-08-03 16:48:37 -0700604
Paul Moorefd385852006-09-25 15:56:37 -0700605listdef_failure_lock:
606 rcu_read_unlock();
Paul Moored15c3452006-08-03 16:48:37 -0700607listdef_failure:
Paul Moorefd385852006-09-25 15:56:37 -0700608 kfree_skb(ans_skb);
Paul Moored15c3452006-08-03 16:48:37 -0700609 return ret_val;
610}
611
612/**
Paul Moorefd385852006-09-25 15:56:37 -0700613 * netlbl_mgmt_protocols_cb - Write an individual PROTOCOL message response
614 * @skb: the skb to write to
Paul Moorefd385852006-09-25 15:56:37 -0700615 * @cb: the NETLINK callback
616 * @protocol: the NetLabel protocol to use in the message
Paul Moored15c3452006-08-03 16:48:37 -0700617 *
618 * Description:
Paul Moorefd385852006-09-25 15:56:37 -0700619 * This function is to be used in conjunction with netlbl_mgmt_protocols() to
620 * answer a application's PROTOCOLS message. Returns the size of the message
621 * on success, negative values on failure.
Paul Moored15c3452006-08-03 16:48:37 -0700622 *
623 */
Paul Moorefd385852006-09-25 15:56:37 -0700624static int netlbl_mgmt_protocols_cb(struct sk_buff *skb,
625 struct netlink_callback *cb,
626 u32 protocol)
Paul Moored15c3452006-08-03 16:48:37 -0700627{
628 int ret_val = -ENOMEM;
Paul Moorefd385852006-09-25 15:56:37 -0700629 void *data;
Paul Moored15c3452006-08-03 16:48:37 -0700630
Eric W. Biederman15e47302012-09-07 20:12:54 +0000631 data = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Thomas Graf17c157c2006-11-14 19:46:02 -0800632 &netlbl_mgmt_gnl_family, NLM_F_MULTI,
633 NLBL_MGMT_C_PROTOCOLS);
Paul Moorefd385852006-09-25 15:56:37 -0700634 if (data == NULL)
635 goto protocols_cb_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700636
Paul Moorefd385852006-09-25 15:56:37 -0700637 ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, protocol);
Paul Moored15c3452006-08-03 16:48:37 -0700638 if (ret_val != 0)
Paul Moorefd385852006-09-25 15:56:37 -0700639 goto protocols_cb_failure;
Paul Moored15c3452006-08-03 16:48:37 -0700640
Johannes Berg053c0952015-01-16 22:09:00 +0100641 genlmsg_end(skb, data);
642 return 0;
Paul Moored15c3452006-08-03 16:48:37 -0700643
Paul Moorefd385852006-09-25 15:56:37 -0700644protocols_cb_failure:
645 genlmsg_cancel(skb, data);
Paul Moored15c3452006-08-03 16:48:37 -0700646 return ret_val;
647}
648
649/**
Paul Moorefd385852006-09-25 15:56:37 -0700650 * netlbl_mgmt_protocols - Handle a PROTOCOLS message
651 * @skb: the NETLINK buffer
652 * @cb: the NETLINK callback
653 *
654 * Description:
655 * Process a user generated PROTOCOLS message and respond accordingly.
656 *
657 */
658static int netlbl_mgmt_protocols(struct sk_buff *skb,
659 struct netlink_callback *cb)
660{
661 u32 protos_sent = cb->args[0];
662
663 if (protos_sent == 0) {
664 if (netlbl_mgmt_protocols_cb(skb,
665 cb,
666 NETLBL_NLTYPE_UNLABELED) < 0)
667 goto protocols_return;
668 protos_sent++;
669 }
670 if (protos_sent == 1) {
671 if (netlbl_mgmt_protocols_cb(skb,
672 cb,
673 NETLBL_NLTYPE_CIPSOV4) < 0)
674 goto protocols_return;
675 protos_sent++;
676 }
677
678protocols_return:
679 cb->args[0] = protos_sent;
680 return skb->len;
681}
682
683/**
Paul Moored15c3452006-08-03 16:48:37 -0700684 * netlbl_mgmt_version - Handle a VERSION message
685 * @skb: the NETLINK buffer
686 * @info: the Generic NETLINK info block
687 *
688 * Description:
689 * Process a user generated VERSION message and respond accordingly. Returns
690 * zero on success, negative values on failure.
691 *
692 */
693static int netlbl_mgmt_version(struct sk_buff *skb, struct genl_info *info)
694{
695 int ret_val = -ENOMEM;
696 struct sk_buff *ans_skb = NULL;
Paul Moorefd385852006-09-25 15:56:37 -0700697 void *data;
Paul Moored15c3452006-08-03 16:48:37 -0700698
Thomas Graf339bf982006-11-10 14:10:15 -0800699 ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Paul Moored15c3452006-08-03 16:48:37 -0700700 if (ans_skb == NULL)
Paul Moorefd385852006-09-25 15:56:37 -0700701 return -ENOMEM;
Thomas Graf17c157c2006-11-14 19:46:02 -0800702 data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family,
703 0, NLBL_MGMT_C_VERSION);
Paul Moorefd385852006-09-25 15:56:37 -0700704 if (data == NULL)
Paul Moored15c3452006-08-03 16:48:37 -0700705 goto version_failure;
706
Paul Moorefd385852006-09-25 15:56:37 -0700707 ret_val = nla_put_u32(ans_skb,
708 NLBL_MGMT_A_VERSION,
709 NETLBL_PROTO_VERSION);
Paul Moored15c3452006-08-03 16:48:37 -0700710 if (ret_val != 0)
711 goto version_failure;
712
Paul Moorefd385852006-09-25 15:56:37 -0700713 genlmsg_end(ans_skb, data);
Denis V. Lunevfe785be2008-07-10 16:53:39 -0700714 return genlmsg_reply(ans_skb, info);
Paul Moored15c3452006-08-03 16:48:37 -0700715
716version_failure:
717 kfree_skb(ans_skb);
Paul Moored15c3452006-08-03 16:48:37 -0700718 return ret_val;
719}
720
721
722/*
723 * NetLabel Generic NETLINK Command Definitions
724 */
725
Johannes Berg4534de82013-11-14 17:14:46 +0100726static const struct genl_ops netlbl_mgmt_genl_ops[] = {
Pavel Emelyanov227c43c2008-02-17 22:33:16 -0800727 {
Paul Moored15c3452006-08-03 16:48:37 -0700728 .cmd = NLBL_MGMT_C_ADD,
Paul Moorefd385852006-09-25 15:56:37 -0700729 .flags = GENL_ADMIN_PERM,
730 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700731 .doit = netlbl_mgmt_add,
732 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -0800733 },
734 {
Paul Moored15c3452006-08-03 16:48:37 -0700735 .cmd = NLBL_MGMT_C_REMOVE,
Paul Moorefd385852006-09-25 15:56:37 -0700736 .flags = GENL_ADMIN_PERM,
737 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700738 .doit = netlbl_mgmt_remove,
739 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -0800740 },
741 {
Paul Moorefd385852006-09-25 15:56:37 -0700742 .cmd = NLBL_MGMT_C_LISTALL,
Paul Moored15c3452006-08-03 16:48:37 -0700743 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700744 .policy = netlbl_mgmt_genl_policy,
745 .doit = NULL,
746 .dumpit = netlbl_mgmt_listall,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -0800747 },
748 {
Paul Moored15c3452006-08-03 16:48:37 -0700749 .cmd = NLBL_MGMT_C_ADDDEF,
Paul Moorefd385852006-09-25 15:56:37 -0700750 .flags = GENL_ADMIN_PERM,
751 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700752 .doit = netlbl_mgmt_adddef,
753 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -0800754 },
755 {
Paul Moored15c3452006-08-03 16:48:37 -0700756 .cmd = NLBL_MGMT_C_REMOVEDEF,
Paul Moorefd385852006-09-25 15:56:37 -0700757 .flags = GENL_ADMIN_PERM,
758 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700759 .doit = netlbl_mgmt_removedef,
760 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -0800761 },
762 {
Paul Moored15c3452006-08-03 16:48:37 -0700763 .cmd = NLBL_MGMT_C_LISTDEF,
764 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700765 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700766 .doit = netlbl_mgmt_listdef,
767 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -0800768 },
769 {
Paul Moorefd385852006-09-25 15:56:37 -0700770 .cmd = NLBL_MGMT_C_PROTOCOLS,
Paul Moored15c3452006-08-03 16:48:37 -0700771 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700772 .policy = netlbl_mgmt_genl_policy,
773 .doit = NULL,
774 .dumpit = netlbl_mgmt_protocols,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -0800775 },
776 {
Paul Moored15c3452006-08-03 16:48:37 -0700777 .cmd = NLBL_MGMT_C_VERSION,
778 .flags = 0,
Paul Moorefd385852006-09-25 15:56:37 -0700779 .policy = netlbl_mgmt_genl_policy,
Paul Moored15c3452006-08-03 16:48:37 -0700780 .doit = netlbl_mgmt_version,
781 .dumpit = NULL,
Pavel Emelyanov227c43c2008-02-17 22:33:16 -0800782 },
Paul Moored15c3452006-08-03 16:48:37 -0700783};
784
785/*
786 * NetLabel Generic NETLINK Protocol Functions
787 */
788
789/**
790 * netlbl_mgmt_genl_init - Register the NetLabel management component
791 *
792 * Description:
793 * Register the NetLabel management component with the Generic NETLINK
794 * mechanism. Returns zero on success, negative values on failure.
795 *
796 */
Pavel Emelyanov05705e42008-02-17 22:33:57 -0800797int __init netlbl_mgmt_genl_init(void)
Paul Moored15c3452006-08-03 16:48:37 -0700798{
Michał Mirosław7ae740d2009-05-21 10:34:05 +0000799 return genl_register_family_with_ops(&netlbl_mgmt_gnl_family,
Johannes Bergc53ed742013-11-19 15:19:31 +0100800 netlbl_mgmt_genl_ops);
Paul Moored15c3452006-08-03 16:48:37 -0700801}