| 1 | /* |
| 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 | * |
| 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 | * 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 |
| 26 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include <linux/types.h> |
| 31 | #include <linux/socket.h> |
| 32 | #include <linux/string.h> |
| 33 | #include <linux/skbuff.h> |
| 34 | #include <linux/in.h> |
| 35 | #include <linux/in6.h> |
| 36 | #include <linux/slab.h> |
| 37 | #include <net/sock.h> |
| 38 | #include <net/netlink.h> |
| 39 | #include <net/genetlink.h> |
| 40 | #include <net/ip.h> |
| 41 | #include <net/ipv6.h> |
| 42 | #include <net/netlabel.h> |
| 43 | #include <net/cipso_ipv4.h> |
| 44 | #include <linux/atomic.h> |
| 45 | |
| 46 | #include "netlabel_domainhash.h" |
| 47 | #include "netlabel_user.h" |
| 48 | #include "netlabel_mgmt.h" |
| 49 | |
| 50 | /* NetLabel configured protocol counter */ |
| 51 | atomic_t netlabel_mgmt_protocount = ATOMIC_INIT(0); |
| 52 | |
| 53 | /* Argument struct for netlbl_domhsh_walk() */ |
| 54 | struct netlbl_domhsh_walk_arg { |
| 55 | struct netlink_callback *nl_cb; |
| 56 | struct sk_buff *skb; |
| 57 | u32 seq; |
| 58 | }; |
| 59 | |
| 60 | /* NetLabel Generic NETLINK CIPSOv4 family */ |
| 61 | static 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, |
| 66 | .maxattr = NLBL_MGMT_A_MAX, |
| 67 | }; |
| 68 | |
| 69 | /* NetLabel Netlink attribute policy */ |
| 70 | static const struct nla_policy netlbl_mgmt_genl_policy[NLBL_MGMT_A_MAX + 1] = { |
| 71 | [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 }, |
| 75 | [NLBL_MGMT_A_FAMILY] = { .type = NLA_U16 }, |
| 76 | }; |
| 77 | |
| 78 | /* |
| 79 | * 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 | */ |
| 93 | static int netlbl_mgmt_add_common(struct genl_info *info, |
| 94 | struct netlbl_audit *audit_info) |
| 95 | { |
| 96 | int ret_val = -EINVAL; |
| 97 | struct netlbl_domaddr_map *addrmap = NULL; |
| 98 | struct cipso_v4_doi *cipsov4 = NULL; |
| 99 | u32 tmp_val; |
| 100 | struct netlbl_dom_map *entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 101 | |
| 102 | if (!entry) |
| 103 | return -ENOMEM; |
| 104 | entry->def.type = nla_get_u32(info->attrs[NLBL_MGMT_A_PROTOCOL]); |
| 105 | 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; |
| 110 | goto add_free_entry; |
| 111 | } |
| 112 | nla_strlcpy(entry->domain, |
| 113 | info->attrs[NLBL_MGMT_A_DOMAIN], tmp_size); |
| 114 | } |
| 115 | |
| 116 | /* NOTE: internally we allow/use a entry->def.type value of |
| 117 | * 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 | |
| 121 | switch (entry->def.type) { |
| 122 | case NETLBL_NLTYPE_UNLABELED: |
| 123 | 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; |
| 128 | break; |
| 129 | case NETLBL_NLTYPE_CIPSOV4: |
| 130 | if (!info->attrs[NLBL_MGMT_A_CV4DOI]) |
| 131 | goto add_free_domain; |
| 132 | |
| 133 | tmp_val = nla_get_u32(info->attrs[NLBL_MGMT_A_CV4DOI]); |
| 134 | cipsov4 = cipso_v4_doi_getdef(tmp_val); |
| 135 | if (cipsov4 == NULL) |
| 136 | goto add_free_domain; |
| 137 | entry->family = AF_INET; |
| 138 | entry->def.cipso = cipsov4; |
| 139 | break; |
| 140 | default: |
| 141 | goto add_free_domain; |
| 142 | } |
| 143 | |
| 144 | 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 | |
| 148 | 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; |
| 156 | goto add_doi_put_def; |
| 157 | } |
| 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; |
| 164 | goto add_free_addrmap; |
| 165 | } |
| 166 | if (nla_len(info->attrs[NLBL_MGMT_A_IPV4MASK]) != |
| 167 | sizeof(struct in_addr)) { |
| 168 | ret_val = -EINVAL; |
| 169 | goto add_free_addrmap; |
| 170 | } |
| 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; |
| 177 | goto add_free_addrmap; |
| 178 | } |
| 179 | map->list.addr = addr->s_addr & mask->s_addr; |
| 180 | map->list.mask = mask->s_addr; |
| 181 | map->list.valid = 1; |
| 182 | map->def.type = entry->def.type; |
| 183 | if (cipsov4) |
| 184 | map->def.cipso = cipsov4; |
| 185 | |
| 186 | ret_val = netlbl_af4list_add(&map->list, &addrmap->list4); |
| 187 | if (ret_val != 0) { |
| 188 | kfree(map); |
| 189 | goto add_free_addrmap; |
| 190 | } |
| 191 | |
| 192 | entry->family = AF_INET; |
| 193 | entry->def.type = NETLBL_NLTYPE_ADDRSELECT; |
| 194 | entry->def.addrsel = addrmap; |
| 195 | #if IS_ENABLED(CONFIG_IPV6) |
| 196 | } 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; |
| 204 | goto add_doi_put_def; |
| 205 | } |
| 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; |
| 212 | goto add_free_addrmap; |
| 213 | } |
| 214 | if (nla_len(info->attrs[NLBL_MGMT_A_IPV6MASK]) != |
| 215 | sizeof(struct in6_addr)) { |
| 216 | ret_val = -EINVAL; |
| 217 | goto add_free_addrmap; |
| 218 | } |
| 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; |
| 225 | goto add_free_addrmap; |
| 226 | } |
| 227 | map->list.addr = *addr; |
| 228 | 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]; |
| 232 | map->list.mask = *mask; |
| 233 | map->list.valid = 1; |
| 234 | map->def.type = entry->def.type; |
| 235 | |
| 236 | ret_val = netlbl_af6list_add(&map->list, &addrmap->list6); |
| 237 | if (ret_val != 0) { |
| 238 | kfree(map); |
| 239 | goto add_free_addrmap; |
| 240 | } |
| 241 | |
| 242 | entry->family = AF_INET6; |
| 243 | entry->def.type = NETLBL_NLTYPE_ADDRSELECT; |
| 244 | entry->def.addrsel = addrmap; |
| 245 | #endif /* IPv6 */ |
| 246 | } |
| 247 | |
| 248 | ret_val = netlbl_domhsh_add(entry, audit_info); |
| 249 | if (ret_val != 0) |
| 250 | goto add_free_addrmap; |
| 251 | |
| 252 | return 0; |
| 253 | |
| 254 | add_free_addrmap: |
| 255 | kfree(addrmap); |
| 256 | add_doi_put_def: |
| 257 | cipso_v4_doi_putdef(cipsov4); |
| 258 | add_free_domain: |
| 259 | kfree(entry->domain); |
| 260 | add_free_entry: |
| 261 | 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 |
| 272 | * handlers. The caller is responsible for ensuring that the RCU read lock |
| 273 | * is held. Returns zero on success, negative values on failure. |
| 274 | * |
| 275 | */ |
| 276 | static int netlbl_mgmt_listentry(struct sk_buff *skb, |
| 277 | struct netlbl_dom_map *entry) |
| 278 | { |
| 279 | int ret_val = 0; |
| 280 | struct nlattr *nla_a; |
| 281 | struct nlattr *nla_b; |
| 282 | struct netlbl_af4list *iter4; |
| 283 | #if IS_ENABLED(CONFIG_IPV6) |
| 284 | 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 | |
| 294 | ret_val = nla_put_u16(skb, NLBL_MGMT_A_FAMILY, entry->family); |
| 295 | if (ret_val != 0) |
| 296 | return ret_val; |
| 297 | |
| 298 | switch (entry->def.type) { |
| 299 | case NETLBL_NLTYPE_ADDRSELECT: |
| 300 | nla_a = nla_nest_start(skb, NLBL_MGMT_A_SELECTORLIST); |
| 301 | if (nla_a == NULL) |
| 302 | return -ENOMEM; |
| 303 | |
| 304 | netlbl_af4list_foreach_rcu(iter4, &entry->def.addrsel->list4) { |
| 305 | 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; |
| 313 | ret_val = nla_put_in_addr(skb, NLBL_MGMT_A_IPV4ADDR, |
| 314 | addr_struct.s_addr); |
| 315 | if (ret_val != 0) |
| 316 | return ret_val; |
| 317 | addr_struct.s_addr = iter4->mask; |
| 318 | ret_val = nla_put_in_addr(skb, NLBL_MGMT_A_IPV4MASK, |
| 319 | addr_struct.s_addr); |
| 320 | 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, |
| 324 | map4->def.type); |
| 325 | if (ret_val != 0) |
| 326 | return ret_val; |
| 327 | switch (map4->def.type) { |
| 328 | case NETLBL_NLTYPE_CIPSOV4: |
| 329 | ret_val = nla_put_u32(skb, NLBL_MGMT_A_CV4DOI, |
| 330 | map4->def.cipso->doi); |
| 331 | if (ret_val != 0) |
| 332 | return ret_val; |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | nla_nest_end(skb, nla_b); |
| 337 | } |
| 338 | #if IS_ENABLED(CONFIG_IPV6) |
| 339 | netlbl_af6list_foreach_rcu(iter6, &entry->def.addrsel->list6) { |
| 340 | 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 | |
| 346 | ret_val = nla_put_in6_addr(skb, NLBL_MGMT_A_IPV6ADDR, |
| 347 | &iter6->addr); |
| 348 | if (ret_val != 0) |
| 349 | return ret_val; |
| 350 | ret_val = nla_put_in6_addr(skb, NLBL_MGMT_A_IPV6MASK, |
| 351 | &iter6->mask); |
| 352 | 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, |
| 356 | map6->def.type); |
| 357 | 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: |
| 367 | ret_val = nla_put_u32(skb,NLBL_MGMT_A_PROTOCOL,entry->def.type); |
| 368 | break; |
| 369 | case NETLBL_NLTYPE_CIPSOV4: |
| 370 | ret_val = nla_put_u32(skb,NLBL_MGMT_A_PROTOCOL,entry->def.type); |
| 371 | if (ret_val != 0) |
| 372 | return ret_val; |
| 373 | ret_val = nla_put_u32(skb, NLBL_MGMT_A_CV4DOI, |
| 374 | entry->def.cipso->doi); |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | return ret_val; |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * 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 | */ |
| 396 | static int netlbl_mgmt_add(struct sk_buff *skb, struct genl_info *info) |
| 397 | { |
| 398 | struct netlbl_audit audit_info; |
| 399 | |
| 400 | 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; |
| 411 | |
| 412 | netlbl_netlink_auditinfo(skb, &audit_info); |
| 413 | |
| 414 | return netlbl_mgmt_add_common(info, &audit_info); |
| 415 | } |
| 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 | */ |
| 427 | static int netlbl_mgmt_remove(struct sk_buff *skb, struct genl_info *info) |
| 428 | { |
| 429 | char *domain; |
| 430 | struct netlbl_audit audit_info; |
| 431 | |
| 432 | if (!info->attrs[NLBL_MGMT_A_DOMAIN]) |
| 433 | return -EINVAL; |
| 434 | |
| 435 | netlbl_netlink_auditinfo(skb, &audit_info); |
| 436 | |
| 437 | domain = nla_data(info->attrs[NLBL_MGMT_A_DOMAIN]); |
| 438 | return netlbl_domhsh_remove(domain, AF_UNSPEC, &audit_info); |
| 439 | } |
| 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 | */ |
| 453 | static 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 | |
| 459 | data = genlmsg_put(cb_arg->skb, NETLINK_CB(cb_arg->nl_cb->skb).portid, |
| 460 | cb_arg->seq, &netlbl_mgmt_gnl_family, |
| 461 | NLM_F_MULTI, NLBL_MGMT_C_LISTALL); |
| 462 | if (data == NULL) |
| 463 | goto listall_cb_failure; |
| 464 | |
| 465 | ret_val = netlbl_mgmt_listentry(cb_arg->skb, entry); |
| 466 | if (ret_val != 0) |
| 467 | goto listall_cb_failure; |
| 468 | |
| 469 | cb_arg->seq++; |
| 470 | genlmsg_end(cb_arg->skb, data); |
| 471 | return 0; |
| 472 | |
| 473 | listall_cb_failure: |
| 474 | genlmsg_cancel(cb_arg->skb, data); |
| 475 | return ret_val; |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * netlbl_mgmt_listall - Handle a LISTALL message |
| 480 | * @skb: the NETLINK buffer |
| 481 | * @cb: the NETLINK callback |
| 482 | * |
| 483 | * Description: |
| 484 | * 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. |
| 487 | * |
| 488 | */ |
| 489 | static int netlbl_mgmt_listall(struct sk_buff *skb, |
| 490 | struct netlink_callback *cb) |
| 491 | { |
| 492 | struct netlbl_domhsh_walk_arg cb_arg; |
| 493 | u32 skip_bkt = cb->args[0]; |
| 494 | u32 skip_chain = cb->args[1]; |
| 495 | |
| 496 | cb_arg.nl_cb = cb; |
| 497 | cb_arg.skb = skb; |
| 498 | cb_arg.seq = cb->nlh->nlmsg_seq; |
| 499 | |
| 500 | netlbl_domhsh_walk(&skip_bkt, |
| 501 | &skip_chain, |
| 502 | netlbl_mgmt_listall_cb, |
| 503 | &cb_arg); |
| 504 | |
| 505 | cb->args[0] = skip_bkt; |
| 506 | cb->args[1] = skip_chain; |
| 507 | return skb->len; |
| 508 | } |
| 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 | */ |
| 520 | static int netlbl_mgmt_adddef(struct sk_buff *skb, struct genl_info *info) |
| 521 | { |
| 522 | struct netlbl_audit audit_info; |
| 523 | |
| 524 | 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; |
| 534 | |
| 535 | netlbl_netlink_auditinfo(skb, &audit_info); |
| 536 | |
| 537 | return netlbl_mgmt_add_common(info, &audit_info); |
| 538 | } |
| 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 | */ |
| 550 | static int netlbl_mgmt_removedef(struct sk_buff *skb, struct genl_info *info) |
| 551 | { |
| 552 | struct netlbl_audit audit_info; |
| 553 | |
| 554 | netlbl_netlink_auditinfo(skb, &audit_info); |
| 555 | |
| 556 | return netlbl_domhsh_remove_default(AF_UNSPEC, &audit_info); |
| 557 | } |
| 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 | */ |
| 570 | static int netlbl_mgmt_listdef(struct sk_buff *skb, struct genl_info *info) |
| 571 | { |
| 572 | int ret_val = -ENOMEM; |
| 573 | struct sk_buff *ans_skb = NULL; |
| 574 | void *data; |
| 575 | struct netlbl_dom_map *entry; |
| 576 | 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; |
| 582 | |
| 583 | ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 584 | if (ans_skb == NULL) |
| 585 | return -ENOMEM; |
| 586 | data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family, |
| 587 | 0, NLBL_MGMT_C_LISTDEF); |
| 588 | if (data == NULL) |
| 589 | goto listdef_failure; |
| 590 | |
| 591 | rcu_read_lock(); |
| 592 | entry = netlbl_domhsh_getentry(NULL, family); |
| 593 | if (entry == NULL) { |
| 594 | ret_val = -ENOENT; |
| 595 | goto listdef_failure_lock; |
| 596 | } |
| 597 | ret_val = netlbl_mgmt_listentry(ans_skb, entry); |
| 598 | rcu_read_unlock(); |
| 599 | if (ret_val != 0) |
| 600 | goto listdef_failure; |
| 601 | |
| 602 | genlmsg_end(ans_skb, data); |
| 603 | return genlmsg_reply(ans_skb, info); |
| 604 | |
| 605 | listdef_failure_lock: |
| 606 | rcu_read_unlock(); |
| 607 | listdef_failure: |
| 608 | kfree_skb(ans_skb); |
| 609 | return ret_val; |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * netlbl_mgmt_protocols_cb - Write an individual PROTOCOL message response |
| 614 | * @skb: the skb to write to |
| 615 | * @cb: the NETLINK callback |
| 616 | * @protocol: the NetLabel protocol to use in the message |
| 617 | * |
| 618 | * Description: |
| 619 | * 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. |
| 622 | * |
| 623 | */ |
| 624 | static int netlbl_mgmt_protocols_cb(struct sk_buff *skb, |
| 625 | struct netlink_callback *cb, |
| 626 | u32 protocol) |
| 627 | { |
| 628 | int ret_val = -ENOMEM; |
| 629 | void *data; |
| 630 | |
| 631 | data = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
| 632 | &netlbl_mgmt_gnl_family, NLM_F_MULTI, |
| 633 | NLBL_MGMT_C_PROTOCOLS); |
| 634 | if (data == NULL) |
| 635 | goto protocols_cb_failure; |
| 636 | |
| 637 | ret_val = nla_put_u32(skb, NLBL_MGMT_A_PROTOCOL, protocol); |
| 638 | if (ret_val != 0) |
| 639 | goto protocols_cb_failure; |
| 640 | |
| 641 | genlmsg_end(skb, data); |
| 642 | return 0; |
| 643 | |
| 644 | protocols_cb_failure: |
| 645 | genlmsg_cancel(skb, data); |
| 646 | return ret_val; |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * 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 | */ |
| 658 | static 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 | |
| 678 | protocols_return: |
| 679 | cb->args[0] = protos_sent; |
| 680 | return skb->len; |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * 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 | */ |
| 693 | static 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; |
| 697 | void *data; |
| 698 | |
| 699 | ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
| 700 | if (ans_skb == NULL) |
| 701 | return -ENOMEM; |
| 702 | data = genlmsg_put_reply(ans_skb, info, &netlbl_mgmt_gnl_family, |
| 703 | 0, NLBL_MGMT_C_VERSION); |
| 704 | if (data == NULL) |
| 705 | goto version_failure; |
| 706 | |
| 707 | ret_val = nla_put_u32(ans_skb, |
| 708 | NLBL_MGMT_A_VERSION, |
| 709 | NETLBL_PROTO_VERSION); |
| 710 | if (ret_val != 0) |
| 711 | goto version_failure; |
| 712 | |
| 713 | genlmsg_end(ans_skb, data); |
| 714 | return genlmsg_reply(ans_skb, info); |
| 715 | |
| 716 | version_failure: |
| 717 | kfree_skb(ans_skb); |
| 718 | return ret_val; |
| 719 | } |
| 720 | |
| 721 | |
| 722 | /* |
| 723 | * NetLabel Generic NETLINK Command Definitions |
| 724 | */ |
| 725 | |
| 726 | static const struct genl_ops netlbl_mgmt_genl_ops[] = { |
| 727 | { |
| 728 | .cmd = NLBL_MGMT_C_ADD, |
| 729 | .flags = GENL_ADMIN_PERM, |
| 730 | .policy = netlbl_mgmt_genl_policy, |
| 731 | .doit = netlbl_mgmt_add, |
| 732 | .dumpit = NULL, |
| 733 | }, |
| 734 | { |
| 735 | .cmd = NLBL_MGMT_C_REMOVE, |
| 736 | .flags = GENL_ADMIN_PERM, |
| 737 | .policy = netlbl_mgmt_genl_policy, |
| 738 | .doit = netlbl_mgmt_remove, |
| 739 | .dumpit = NULL, |
| 740 | }, |
| 741 | { |
| 742 | .cmd = NLBL_MGMT_C_LISTALL, |
| 743 | .flags = 0, |
| 744 | .policy = netlbl_mgmt_genl_policy, |
| 745 | .doit = NULL, |
| 746 | .dumpit = netlbl_mgmt_listall, |
| 747 | }, |
| 748 | { |
| 749 | .cmd = NLBL_MGMT_C_ADDDEF, |
| 750 | .flags = GENL_ADMIN_PERM, |
| 751 | .policy = netlbl_mgmt_genl_policy, |
| 752 | .doit = netlbl_mgmt_adddef, |
| 753 | .dumpit = NULL, |
| 754 | }, |
| 755 | { |
| 756 | .cmd = NLBL_MGMT_C_REMOVEDEF, |
| 757 | .flags = GENL_ADMIN_PERM, |
| 758 | .policy = netlbl_mgmt_genl_policy, |
| 759 | .doit = netlbl_mgmt_removedef, |
| 760 | .dumpit = NULL, |
| 761 | }, |
| 762 | { |
| 763 | .cmd = NLBL_MGMT_C_LISTDEF, |
| 764 | .flags = 0, |
| 765 | .policy = netlbl_mgmt_genl_policy, |
| 766 | .doit = netlbl_mgmt_listdef, |
| 767 | .dumpit = NULL, |
| 768 | }, |
| 769 | { |
| 770 | .cmd = NLBL_MGMT_C_PROTOCOLS, |
| 771 | .flags = 0, |
| 772 | .policy = netlbl_mgmt_genl_policy, |
| 773 | .doit = NULL, |
| 774 | .dumpit = netlbl_mgmt_protocols, |
| 775 | }, |
| 776 | { |
| 777 | .cmd = NLBL_MGMT_C_VERSION, |
| 778 | .flags = 0, |
| 779 | .policy = netlbl_mgmt_genl_policy, |
| 780 | .doit = netlbl_mgmt_version, |
| 781 | .dumpit = NULL, |
| 782 | }, |
| 783 | }; |
| 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 | */ |
| 797 | int __init netlbl_mgmt_genl_init(void) |
| 798 | { |
| 799 | return genl_register_family_with_ops(&netlbl_mgmt_gnl_family, |
| 800 | netlbl_mgmt_genl_ops); |
| 801 | } |