netlabel_mgmt.c 21.8 KB
Newer Older
1 2 3 4 5 6 7
/*
 * NetLabel Management Support
 *
 * This file defines the management functions for the NetLabel system.  The
 * NetLabel system manages static and dynamic label mappings for network
 * protocols such as CIPSO and RIPSO.
 *
8
 * Author: Paul Moore <paul@paul-moore.com>
9 10 11 12
 *
 */

/*
13
 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006, 2008
14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This program is free software;  you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY;  without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
26
 * along with this program;  if not, see <http://www.gnu.org/licenses/>.
27 28 29 30 31 32 33
 *
 */

#include <linux/types.h>
#include <linux/socket.h>
#include <linux/string.h>
#include <linux/skbuff.h>
34 35
#include <linux/in.h>
#include <linux/in6.h>
36
#include <linux/slab.h>
37 38 39
#include <net/sock.h>
#include <net/netlink.h>
#include <net/genetlink.h>
40 41
#include <net/ip.h>
#include <net/ipv6.h>
42 43
#include <net/netlabel.h>
#include <net/cipso_ipv4.h>
44
#include <net/calipso.h>
45
#include <linux/atomic.h>
46

47
#include "netlabel_calipso.h"
48 49 50 51
#include "netlabel_domainhash.h"
#include "netlabel_user.h"
#include "netlabel_mgmt.h"

52 53
/* NetLabel configured protocol counter */
atomic_t netlabel_mgmt_protocount = ATOMIC_INIT(0);
54

55 56 57 58 59 60 61
/* Argument struct for netlbl_domhsh_walk() */
struct netlbl_domhsh_walk_arg {
	struct netlink_callback *nl_cb;
	struct sk_buff *skb;
	u32 seq;
};

62 63 64 65 66 67
/* NetLabel Generic NETLINK CIPSOv4 family */
static struct genl_family netlbl_mgmt_gnl_family = {
	.id = GENL_ID_GENERATE,
	.hdrsize = 0,
	.name = NETLBL_NLTYPE_MGMT_NAME,
	.version = NETLBL_PROTO_VERSION,
68
	.maxattr = NLBL_MGMT_A_MAX,
69 70
};

71
/* NetLabel Netlink attribute policy */
72
static const struct nla_policy netlbl_mgmt_genl_policy[NLBL_MGMT_A_MAX + 1] = {
73 74 75 76
	[NLBL_MGMT_A_DOMAIN] = { .type = NLA_NUL_STRING },
	[NLBL_MGMT_A_PROTOCOL] = { .type = NLA_U32 },
	[NLBL_MGMT_A_VERSION] = { .type = NLA_U32 },
	[NLBL_MGMT_A_CV4DOI] = { .type = NLA_U32 },
77
	[NLBL_MGMT_A_FAMILY] = { .type = NLA_U16 },
78
	[NLBL_MGMT_A_CLPDOI] = { .type = NLA_U32 },
79
};
80 81

/*
82
 * Helper Functions
83 84 85 86 87
 */

/**
 * netlbl_mgmt_add - Handle an ADD message
 * @info: the Generic NETLINK info block
88
 * @audit_info: NetLabel audit information
89 90
 *
 * Description:
91 92 93
 * Helper function for the ADD and ADDDEF messages to add the domain mappings
 * from the message to the hash table.  See netlabel.h for a description of the
 * message format.  Returns zero on success, negative values on failure.
94 95
 *
 */
96 97
static int netlbl_mgmt_add_common(struct genl_info *info,
				  struct netlbl_audit *audit_info)
98 99
{
	int ret_val = -EINVAL;
100 101
	struct netlbl_domaddr_map *addrmap = NULL;
	struct cipso_v4_doi *cipsov4 = NULL;
102 103 104
#if IS_ENABLED(CONFIG_IPV6)
	struct calipso_doi *calipso = NULL;
#endif
105
	u32 tmp_val;
106
	struct netlbl_dom_map *entry = kzalloc(sizeof(*entry), GFP_KERNEL);
107

108 109
	if (!entry)
		return -ENOMEM;
110
	entry->def.type = nla_get_u32(info->attrs[NLBL_MGMT_A_PROTOCOL]);
111 112 113 114 115
	if (info->attrs[NLBL_MGMT_A_DOMAIN]) {
		size_t tmp_size = nla_len(info->attrs[NLBL_MGMT_A_DOMAIN]);
		entry->domain = kmalloc(tmp_size, GFP_KERNEL);
		if (entry->domain == NULL) {
			ret_val = -ENOMEM;
116
			goto add_free_entry;
117 118 119 120 121
		}
		nla_strlcpy(entry->domain,
			    info->attrs[NLBL_MGMT_A_DOMAIN], tmp_size);
	}

122
	/* NOTE: internally we allow/use a entry->def.type value of
123 124 125
	 *       NETLBL_NLTYPE_ADDRSELECT but we don't currently allow users
	 *       to pass that as a protocol value because we need to know the
	 *       "real" protocol */
126

127
	switch (entry->def.type) {
128
	case NETLBL_NLTYPE_UNLABELED:
129 130 131 132 133
		if (info->attrs[NLBL_MGMT_A_FAMILY])
			entry->family =
				nla_get_u16(info->attrs[NLBL_MGMT_A_FAMILY]);
		else
			entry->family = AF_UNSPEC;
134 135 136
		break;
	case NETLBL_NLTYPE_CIPSOV4:
		if (!info->attrs[NLBL_MGMT_A_CV4DOI])
137
			goto add_free_domain;
138

139
		tmp_val = nla_get_u32(info->attrs[NLBL_MGMT_A_CV4DOI]);
140 141
		cipsov4 = cipso_v4_doi_getdef(tmp_val);
		if (cipsov4 == NULL)
142
			goto add_free_domain;
143
		entry->family = AF_INET;
144
		entry->def.cipso = cipsov4;
145
		break;
146 147 148 149 150 151 152 153 154 155 156 157 158
#if IS_ENABLED(CONFIG_IPV6)
	case NETLBL_NLTYPE_CALIPSO:
		if (!info->attrs[NLBL_MGMT_A_CLPDOI])
			goto add_free_domain;

		tmp_val = nla_get_u32(info->attrs[NLBL_MGMT_A_CLPDOI]);
		calipso = calipso_doi_getdef(tmp_val);
		if (calipso == NULL)
			goto add_free_domain;
		entry->family = AF_INET6;
		entry->def.calipso = calipso;
		break;
#endif /* IPv6 */
159
	default:
160
		goto add_free_domain;
161
	}
162

163 164 165 166
	if ((entry->family == AF_INET && info->attrs[NLBL_MGMT_A_IPV6ADDR]) ||
	    (entry->family == AF_INET6 && info->attrs[NLBL_MGMT_A_IPV4ADDR]))
		goto add_doi_put_def;

167 168 169 170 171 172 173 174
	if (info->attrs[NLBL_MGMT_A_IPV4ADDR]) {
		struct in_addr *addr;
		struct in_addr *mask;
		struct netlbl_domaddr4_map *map;

		addrmap = kzalloc(sizeof(*addrmap), GFP_KERNEL);
		if (addrmap == NULL) {
			ret_val = -ENOMEM;
175
			goto add_doi_put_def;
176 177 178 179 180 181 182
		}
		INIT_LIST_HEAD(&addrmap->list4);
		INIT_LIST_HEAD(&addrmap->list6);

		if (nla_len(info->attrs[NLBL_MGMT_A_IPV4ADDR]) !=
		    sizeof(struct in_addr)) {
			ret_val = -EINVAL;
183
			goto add_free_addrmap;
184 185 186 187
		}
		if (nla_len(info->attrs[NLBL_MGMT_A_IPV4MASK]) !=
		    sizeof(struct in_addr)) {
			ret_val = -EINVAL;
188
			goto add_free_addrmap;
189 190 191 192 193 194 195
		}
		addr = nla_data(info->attrs[NLBL_MGMT_A_IPV4ADDR]);
		mask = nla_data(info->attrs[NLBL_MGMT_A_IPV4MASK]);

		map = kzalloc(sizeof(*map), GFP_KERNEL);
		if (map == NULL) {
			ret_val = -ENOMEM;
196
			goto add_free_addrmap;
197 198 199 200
		}
		map->list.addr = addr->s_addr & mask->s_addr;
		map->list.mask = mask->s_addr;
		map->list.valid = 1;
201
		map->def.type = entry->def.type;
202
		if (cipsov4)
203
			map->def.cipso = cipsov4;
204 205 206 207

		ret_val = netlbl_af4list_add(&map->list, &addrmap->list4);
		if (ret_val != 0) {
			kfree(map);
208
			goto add_free_addrmap;
209 210
		}

211
		entry->family = AF_INET;
212 213
		entry->def.type = NETLBL_NLTYPE_ADDRSELECT;
		entry->def.addrsel = addrmap;
214
#if IS_ENABLED(CONFIG_IPV6)
215 216 217 218 219 220 221 222
	} else if (info->attrs[NLBL_MGMT_A_IPV6ADDR]) {
		struct in6_addr *addr;
		struct in6_addr *mask;
		struct netlbl_domaddr6_map *map;

		addrmap = kzalloc(sizeof(*addrmap), GFP_KERNEL);
		if (addrmap == NULL) {
			ret_val = -ENOMEM;
223
			goto add_doi_put_def;
224 225 226 227 228 229 230
		}
		INIT_LIST_HEAD(&addrmap->list4);
		INIT_LIST_HEAD(&addrmap->list6);

		if (nla_len(info->attrs[NLBL_MGMT_A_IPV6ADDR]) !=
		    sizeof(struct in6_addr)) {
			ret_val = -EINVAL;
231
			goto add_free_addrmap;
232 233 234 235
		}
		if (nla_len(info->attrs[NLBL_MGMT_A_IPV6MASK]) !=
		    sizeof(struct in6_addr)) {
			ret_val = -EINVAL;
236
			goto add_free_addrmap;
237 238 239 240 241 242 243
		}
		addr = nla_data(info->attrs[NLBL_MGMT_A_IPV6ADDR]);
		mask = nla_data(info->attrs[NLBL_MGMT_A_IPV6MASK]);

		map = kzalloc(sizeof(*map), GFP_KERNEL);
		if (map == NULL) {
			ret_val = -ENOMEM;
244
			goto add_free_addrmap;
245
		}
246
		map->list.addr = *addr;
247 248 249 250
		map->list.addr.s6_addr32[0] &= mask->s6_addr32[0];
		map->list.addr.s6_addr32[1] &= mask->s6_addr32[1];
		map->list.addr.s6_addr32[2] &= mask->s6_addr32[2];
		map->list.addr.s6_addr32[3] &= mask->s6_addr32[3];
251
		map->list.mask = *mask;
252
		map->list.valid = 1;
253
		map->def.type = entry->def.type;
254 255
		if (calipso)
			map->def.calipso = calipso;
256 257 258 259

		ret_val = netlbl_af6list_add(&map->list, &addrmap->list6);
		if (ret_val != 0) {
			kfree(map);
260
			goto add_free_addrmap;
261 262
		}

263
		entry->family = AF_INET6;
264 265
		entry->def.type = NETLBL_NLTYPE_ADDRSELECT;
		entry->def.addrsel = addrmap;
266 267 268 269
#endif /* IPv6 */
	}

	ret_val = netlbl_domhsh_add(entry, audit_info);
270
	if (ret_val != 0)
271
		goto add_free_addrmap;
272 273 274

	return 0;

275
add_free_addrmap:
276
	kfree(addrmap);
277 278
add_doi_put_def:
	cipso_v4_doi_putdef(cipsov4);
279 280 281
#if IS_ENABLED(CONFIG_IPV6)
	calipso_doi_putdef(calipso);
#endif
282 283 284
add_free_domain:
	kfree(entry->domain);
add_free_entry:
285 286 287 288
	kfree(entry);
	return ret_val;
}

289 290 291 292 293 294 295
/**
 * netlbl_mgmt_listentry - List a NetLabel/LSM domain map entry
 * @skb: the NETLINK buffer
 * @entry: the map entry
 *
 * Description:
 * This function is a helper function used by the LISTALL and LISTDEF command
296
 * handlers.  The caller is responsible for ensuring that the RCU read lock
297 298 299 300 301 302
 * is held.  Returns zero on success, negative values on failure.
 *
 */
static int netlbl_mgmt_listentry(struct sk_buff *skb,
				 struct netlbl_dom_map *entry)
{
303
	int ret_val = 0;
304 305 306
	struct nlattr *nla_a;
	struct nlattr *nla_b;
	struct netlbl_af4list *iter4;
307
#if IS_ENABLED(CONFIG_IPV6)
308 309 310 311 312 313 314 315 316 317
	struct netlbl_af6list *iter6;
#endif

	if (entry->domain != NULL) {
		ret_val = nla_put_string(skb,
					 NLBL_MGMT_A_DOMAIN, entry->domain);
		if (ret_val != 0)
			return ret_val;
	}

318 319 320 321
	ret_val = nla_put_u16(skb, NLBL_MGMT_A_FAMILY, entry->family);
	if (ret_val != 0)
		return ret_val;

322
	switch (entry->def.type) {
323 324 325 326 327
	case NETLBL_NLTYPE_ADDRSELECT:
		nla_a = nla_nest_start(skb, NLBL_MGMT_A_SELECTORLIST);
		if (nla_a == NULL)
			return -ENOMEM;

328
		netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4) {
329 330 331 332 333 334 335 336
			struct netlbl_domaddr4_map *map4;
			struct in_addr addr_struct;

			nla_b = nla_nest_start(skb, NLBL_MGMT_A_ADDRSELECTOR);
			if (nla_b == NULL)
				return -ENOMEM;

			addr_struct.s_addr = iter4->addr;
337 338
			ret_val = nla_put_in_addr(skb, NLBL_MGMT_A_IPV4ADDR,
						  addr_struct.s_addr);
339 340 341
			if (ret_val != 0)
				return ret_val;
			addr_struct.s_addr = iter4->mask;
342 343
			ret_val = nla_put_in_addr(skb, NLBL_MGMT_A_IPV4MASK,
						  addr_struct.s_addr);
344 345 346 347
			if (ret_val != 0)
				return ret_val;
			map4 = netlbl_domhsh_addr4_entry(iter4);
			ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL,
348
					      map4->def.type);
349 350
			if (ret_val != 0)
				return ret_val;
351
			switch (map4->def.type) {
352 353
			case NETLBL_NLTYPE_CIPSOV4:
				ret_val = nla_put_u32(skb, NLBL_MGMT_A_CV4DOI,
354
						      map4->def.cipso->doi);
355 356 357 358 359 360 361
				if (ret_val != 0)
					return ret_val;
				break;
			}

			nla_nest_end(skb, nla_b);
		}
362
#if IS_ENABLED(CONFIG_IPV6)
363
		netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6) {
364 365 366 367 368 369
			struct netlbl_domaddr6_map *map6;

			nla_b = nla_nest_start(skb, NLBL_MGMT_A_ADDRSELECTOR);
			if (nla_b == NULL)
				return -ENOMEM;

370 371
			ret_val = nla_put_in6_addr(skb, NLBL_MGMT_A_IPV6ADDR,
						   &iter6->addr);
372 373
			if (ret_val != 0)
				return ret_val;
374 375
			ret_val = nla_put_in6_addr(skb, NLBL_MGMT_A_IPV6MASK,
						   &iter6->mask);
376 377 378 379
			if (ret_val != 0)
				return ret_val;
			map6 = netlbl_domhsh_addr6_entry(iter6);
			ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL,
380
					      map6->def.type);
381 382 383
			if (ret_val != 0)
				return ret_val;

384 385 386 387 388 389 390 391 392
			switch (map6->def.type) {
			case NETLBL_NLTYPE_CALIPSO:
				ret_val = nla_put_u32(skb, NLBL_MGMT_A_CLPDOI,
						      map6->def.calipso->doi);
				if (ret_val != 0)
					return ret_val;
				break;
			}

393 394 395 396 397 398 399
			nla_nest_end(skb, nla_b);
		}
#endif /* IPv6 */

		nla_nest_end(skb, nla_a);
		break;
	case NETLBL_NLTYPE_UNLABELED:
400 401
		ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL,
				      entry->def.type);
402 403
		break;
	case NETLBL_NLTYPE_CIPSOV4:
404 405
		ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL,
				      entry->def.type);
406 407 408
		if (ret_val != 0)
			return ret_val;
		ret_val = nla_put_u32(skb, NLBL_MGMT_A_CV4DOI,
409
				      entry->def.cipso->doi);
410
		break;
411 412 413 414 415 416 417 418
	case NETLBL_NLTYPE_CALIPSO:
		ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL,
				      entry->def.type);
		if (ret_val != 0)
			return ret_val;
		ret_val = nla_put_u32(skb, NLBL_MGMT_A_CLPDOI,
				      entry->def.calipso->doi);
		break;
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
	}

	return ret_val;
}

/*
 * NetLabel Command Handlers
 */

/**
 * netlbl_mgmt_add - Handle an ADD message
 * @skb: the NETLINK buffer
 * @info: the Generic NETLINK info block
 *
 * Description:
 * Process a user generated ADD message and add the domains from the message
 * to the hash table.  See netlabel.h for a description of the message format.
 * Returns zero on success, negative values on failure.
 *
 */
static int netlbl_mgmt_add(struct sk_buff *skb, struct genl_info *info)
{
	struct netlbl_audit audit_info;

	if ((!info->attrs[NLBL_MGMT_A_DOMAIN]) ||
	    (!info->attrs[NLBL_MGMT_A_PROTOCOL]) ||
	    (info->attrs[NLBL_MGMT_A_IPV4ADDR] &&
	     info->attrs[NLBL_MGMT_A_IPV6ADDR]) ||
	    (info->attrs[NLBL_MGMT_A_IPV4MASK] &&
	     info->attrs[NLBL_MGMT_A_IPV6MASK]) ||
	    ((info->attrs[NLBL_MGMT_A_IPV4ADDR] != NULL) ^
	     (info->attrs[NLBL_MGMT_A_IPV4MASK] != NULL)) ||
	    ((info->attrs[NLBL_MGMT_A_IPV6ADDR] != NULL) ^
	     (info->attrs[NLBL_MGMT_A_IPV6MASK] != NULL)))
		return -EINVAL;

	netlbl_netlink_auditinfo(skb, &audit_info);

	return netlbl_mgmt_add_common(info, &audit_info);
}

460 461 462 463 464 465 466 467 468 469 470 471
/**
 * netlbl_mgmt_remove - Handle a REMOVE message
 * @skb: the NETLINK buffer
 * @info: the Generic NETLINK info block
 *
 * Description:
 * Process a user generated REMOVE message and remove the specified domain
 * mappings.  Returns zero on success, negative values on failure.
 *
 */
static int netlbl_mgmt_remove(struct sk_buff *skb, struct genl_info *info)
{
472
	char *domain;
473
	struct netlbl_audit audit_info;
474

475 476
	if (!info->attrs[NLBL_MGMT_A_DOMAIN])
		return -EINVAL;
477

478 479
	netlbl_netlink_auditinfo(skb, &audit_info);

480
	domain = nla_data(info->attrs[NLBL_MGMT_A_DOMAIN]);
481
	return netlbl_domhsh_remove(domain, AF_UNSPEC, &audit_info);
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
}

/**
 * netlbl_mgmt_listall_cb - netlbl_domhsh_walk() callback for LISTALL
 * @entry: the domain mapping hash table entry
 * @arg: the netlbl_domhsh_walk_arg structure
 *
 * Description:
 * This function is designed to be used as a callback to the
 * netlbl_domhsh_walk() function for use in generating a response for a LISTALL
 * message.  Returns the size of the message on success, negative values on
 * failure.
 *
 */
static int netlbl_mgmt_listall_cb(struct netlbl_dom_map *entry, void *arg)
{
	int ret_val = -ENOMEM;
	struct netlbl_domhsh_walk_arg *cb_arg = arg;
	void *data;

502
	data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).portid,
503 504
			   cb_arg->seq, &netlbl_mgmt_gnl_family,
			   NLM_F_MULTI, NLBL_MGMT_C_LISTALL);
505 506 507
	if (data == NULL)
		goto listall_cb_failure;

508
	ret_val = netlbl_mgmt_listentry(cb_arg->skb, entry);
509 510
	if (ret_val != 0)
		goto listall_cb_failure;
511

512
	cb_arg->seq++;
513 514
	genlmsg_end(cb_arg->skb, data);
	return 0;
515

516 517
listall_cb_failure:
	genlmsg_cancel(cb_arg->skb, data);
518 519 520 521
	return ret_val;
}

/**
522
 * netlbl_mgmt_listall - Handle a LISTALL message
523
 * @skb: the NETLINK buffer
524
 * @cb: the NETLINK callback
525 526
 *
 * Description:
527 528 529
 * Process a user generated LISTALL message and dumps the domain hash table in
 * a form suitable for use in a kernel generated LISTALL message.  Returns zero
 * on success, negative values on failure.
530 531
 *
 */
532 533
static int netlbl_mgmt_listall(struct sk_buff *skb,
			       struct netlink_callback *cb)
534
{
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
	struct netlbl_domhsh_walk_arg cb_arg;
	u32 skip_bkt = cb->args[0];
	u32 skip_chain = cb->args[1];

	cb_arg.nl_cb = cb;
	cb_arg.skb = skb;
	cb_arg.seq = cb->nlh->nlmsg_seq;

	netlbl_domhsh_walk(&skip_bkt,
			   &skip_chain,
			   netlbl_mgmt_listall_cb,
			   &cb_arg);

	cb->args[0] = skip_bkt;
	cb->args[1] = skip_chain;
	return skb->len;
551 552 553 554 555 556 557 558 559 560 561 562 563 564
}

/**
 * netlbl_mgmt_adddef - Handle an ADDDEF message
 * @skb: the NETLINK buffer
 * @info: the Generic NETLINK info block
 *
 * Description:
 * Process a user generated ADDDEF message and respond accordingly.  Returns
 * zero on success, negative values on failure.
 *
 */
static int netlbl_mgmt_adddef(struct sk_buff *skb, struct genl_info *info)
{
565
	struct netlbl_audit audit_info;
566

567 568 569 570 571 572 573 574 575 576
	if ((!info->attrs[NLBL_MGMT_A_PROTOCOL]) ||
	    (info->attrs[NLBL_MGMT_A_IPV4ADDR] &&
	     info->attrs[NLBL_MGMT_A_IPV6ADDR]) ||
	    (info->attrs[NLBL_MGMT_A_IPV4MASK] &&
	     info->attrs[NLBL_MGMT_A_IPV6MASK]) ||
	    ((info->attrs[NLBL_MGMT_A_IPV4ADDR] != NULL) ^
	     (info->attrs[NLBL_MGMT_A_IPV4MASK] != NULL)) ||
	    ((info->attrs[NLBL_MGMT_A_IPV6ADDR] != NULL) ^
	     (info->attrs[NLBL_MGMT_A_IPV6MASK] != NULL)))
		return -EINVAL;
577

578 579
	netlbl_netlink_auditinfo(skb, &audit_info);

580
	return netlbl_mgmt_add_common(info, &audit_info);
581 582 583 584 585 586 587 588 589 590 591 592 593 594
}

/**
 * netlbl_mgmt_removedef - Handle a REMOVEDEF message
 * @skb: the NETLINK buffer
 * @info: the Generic NETLINK info block
 *
 * Description:
 * Process a user generated REMOVEDEF message and remove the default domain
 * mapping.  Returns zero on success, negative values on failure.
 *
 */
static int netlbl_mgmt_removedef(struct sk_buff *skb, struct genl_info *info)
{
595 596 597 598
	struct netlbl_audit audit_info;

	netlbl_netlink_auditinfo(skb, &audit_info);

599
	return netlbl_domhsh_remove_default(AF_UNSPEC, &audit_info);
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
}

/**
 * netlbl_mgmt_listdef - Handle a LISTDEF message
 * @skb: the NETLINK buffer
 * @info: the Generic NETLINK info block
 *
 * Description:
 * Process a user generated LISTDEF message and dumps the default domain
 * mapping in a form suitable for use in a kernel generated LISTDEF message.
 * Returns zero on success, negative values on failure.
 *
 */
static int netlbl_mgmt_listdef(struct sk_buff *skb, struct genl_info *info)
{
	int ret_val = -ENOMEM;
616 617 618
	struct sk_buff *ans_skb = NULL;
	void *data;
	struct netlbl_dom_map *entry;
619 620 621 622 623 624
	u16 family;

	if (info->attrs[NLBL_MGMT_A_FAMILY])
		family = nla_get_u16(info->attrs[NLBL_MGMT_A_FAMILY]);
	else
		family = AF_INET;
625

626
	ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
627
	if (ans_skb == NULL)
628
		return -ENOMEM;
629 630
	data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family,
				 0, NLBL_MGMT_C_LISTDEF);
631
	if (data == NULL)
632 633
		goto listdef_failure;

634
	rcu_read_lock();
635
	entry = netlbl_domhsh_getentry(NULL, family);
636 637 638 639
	if (entry == NULL) {
		ret_val = -ENOENT;
		goto listdef_failure_lock;
	}
640
	ret_val = netlbl_mgmt_listentry(ans_skb, entry);
641
	rcu_read_unlock();
642 643
	if (ret_val != 0)
		goto listdef_failure;
644

645
	genlmsg_end(ans_skb, data);
646
	return genlmsg_reply(ans_skb, info);
647

648 649
listdef_failure_lock:
	rcu_read_unlock();
650
listdef_failure:
651
	kfree_skb(ans_skb);
652 653 654 655
	return ret_val;
}

/**
656 657 658 659
 * netlbl_mgmt_protocols_cb - Write an individual PROTOCOL message response
 * @skb: the skb to write to
 * @cb: the NETLINK callback
 * @protocol: the NetLabel protocol to use in the message
660 661
 *
 * Description:
662 663 664
 * This function is to be used in conjunction with netlbl_mgmt_protocols() to
 * answer a application's PROTOCOLS message.  Returns the size of the message
 * on success, negative values on failure.
665 666
 *
 */
667 668 669
static int netlbl_mgmt_protocols_cb(struct sk_buff *skb,
				    struct netlink_callback *cb,
				    u32 protocol)
670 671
{
	int ret_val = -ENOMEM;
672 673
	void *data;

674
	data = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
675 676
			   &netlbl_mgmt_gnl_family, NLM_F_MULTI,
			   NLBL_MGMT_C_PROTOCOLS);
677 678 679 680
	if (data == NULL)
		goto protocols_cb_failure;

	ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, protocol);
681
	if (ret_val != 0)
682
		goto protocols_cb_failure;
683

684 685
	genlmsg_end(skb, data);
	return 0;
686

687 688
protocols_cb_failure:
	genlmsg_cancel(skb, data);
689 690 691
	return ret_val;
}

692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
/**
 * netlbl_mgmt_protocols - Handle a PROTOCOLS message
 * @skb: the NETLINK buffer
 * @cb: the NETLINK callback
 *
 * Description:
 * Process a user generated PROTOCOLS message and respond accordingly.
 *
 */
static int netlbl_mgmt_protocols(struct sk_buff *skb,
				 struct netlink_callback *cb)
{
	u32 protos_sent = cb->args[0];

	if (protos_sent == 0) {
		if (netlbl_mgmt_protocols_cb(skb,
					     cb,
					     NETLBL_NLTYPE_UNLABELED) < 0)
			goto protocols_return;
		protos_sent++;
	}
	if (protos_sent == 1) {
		if (netlbl_mgmt_protocols_cb(skb,
					     cb,
					     NETLBL_NLTYPE_CIPSOV4) < 0)
			goto protocols_return;
		protos_sent++;
	}
720 721 722 723 724 725 726 727 728
#if IS_ENABLED(CONFIG_IPV6)
	if (protos_sent == 2) {
		if (netlbl_mgmt_protocols_cb(skb,
					     cb,
					     NETLBL_NLTYPE_CALIPSO) < 0)
			goto protocols_return;
		protos_sent++;
	}
#endif
729 730 731 732 733 734

protocols_return:
	cb->args[0] = protos_sent;
	return skb->len;
}

735 736 737 738 739 740 741 742 743 744 745 746 747 748
/**
 * netlbl_mgmt_version - Handle a VERSION message
 * @skb: the NETLINK buffer
 * @info: the Generic NETLINK info block
 *
 * Description:
 * Process a user generated VERSION message and respond accordingly.  Returns
 * zero on success, negative values on failure.
 *
 */
static int netlbl_mgmt_version(struct sk_buff *skb, struct genl_info *info)
{
	int ret_val = -ENOMEM;
	struct sk_buff *ans_skb = NULL;
749
	void *data;
750

751
	ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
752
	if (ans_skb == NULL)
753
		return -ENOMEM;
754 755
	data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family,
				 0, NLBL_MGMT_C_VERSION);
756
	if (data == NULL)
757 758
		goto version_failure;

759 760 761
	ret_val = nla_put_u32(ans_skb,
			      NLBL_MGMT_A_VERSION,
			      NETLBL_PROTO_VERSION);
762 763 764
	if (ret_val != 0)
		goto version_failure;

765
	genlmsg_end(ans_skb, data);
766
	return genlmsg_reply(ans_skb, info);
767 768 769 770 771 772 773 774 775 776 777

version_failure:
	kfree_skb(ans_skb);
	return ret_val;
}


/*
 * NetLabel Generic NETLINK Command Definitions
 */

778
static const struct genl_ops netlbl_mgmt_genl_ops[] = {
779
	{
780
	.cmd = NLBL_MGMT_C_ADD,
781 782
	.flags = GENL_ADMIN_PERM,
	.policy = netlbl_mgmt_genl_policy,
783 784
	.doit = netlbl_mgmt_add,
	.dumpit = NULL,
785 786
	},
	{
787
	.cmd = NLBL_MGMT_C_REMOVE,
788 789
	.flags = GENL_ADMIN_PERM,
	.policy = netlbl_mgmt_genl_policy,
790 791
	.doit = netlbl_mgmt_remove,
	.dumpit = NULL,
792 793
	},
	{
794
	.cmd = NLBL_MGMT_C_LISTALL,
795
	.flags = 0,
796 797 798
	.policy = netlbl_mgmt_genl_policy,
	.doit = NULL,
	.dumpit = netlbl_mgmt_listall,
799 800
	},
	{
801
	.cmd = NLBL_MGMT_C_ADDDEF,
802 803
	.flags = GENL_ADMIN_PERM,
	.policy = netlbl_mgmt_genl_policy,
804 805
	.doit = netlbl_mgmt_adddef,
	.dumpit = NULL,
806 807
	},
	{
808
	.cmd = NLBL_MGMT_C_REMOVEDEF,
809 810
	.flags = GENL_ADMIN_PERM,
	.policy = netlbl_mgmt_genl_policy,
811 812
	.doit = netlbl_mgmt_removedef,
	.dumpit = NULL,
813 814
	},
	{
815 816
	.cmd = NLBL_MGMT_C_LISTDEF,
	.flags = 0,
817
	.policy = netlbl_mgmt_genl_policy,
818 819
	.doit = netlbl_mgmt_listdef,
	.dumpit = NULL,
820 821
	},
	{
822
	.cmd = NLBL_MGMT_C_PROTOCOLS,
823
	.flags = 0,
824 825 826
	.policy = netlbl_mgmt_genl_policy,
	.doit = NULL,
	.dumpit = netlbl_mgmt_protocols,
827 828
	},
	{
829 830
	.cmd = NLBL_MGMT_C_VERSION,
	.flags = 0,
831
	.policy = netlbl_mgmt_genl_policy,
832 833
	.doit = netlbl_mgmt_version,
	.dumpit = NULL,
834
	},
835 836 837 838 839 840 841 842 843 844 845 846 847 848
};

/*
 * NetLabel Generic NETLINK Protocol Functions
 */

/**
 * netlbl_mgmt_genl_init - Register the NetLabel management component
 *
 * Description:
 * Register the NetLabel management component with the Generic NETLINK
 * mechanism.  Returns zero on success, negative values on failure.
 *
 */
849
int __init netlbl_mgmt_genl_init(void)
850
{
851
	return genl_register_family_with_ops(&netlbl_mgmt_gnl_family,
852
					     netlbl_mgmt_genl_ops);
853
}