1 /* 2 * netifd - network interface daemon 3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org> 4 * Copyright (C) 2012 Steven Barth <steven@midlink.org> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 8 * as published by the Free Software Foundation 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 */ 15 #include <string.h> 16 #include <stdlib.h> 17 #include <stdio.h> 18 #include <limits.h> 19 20 #include <arpa/inet.h> 21 #include <netinet/in.h> 22 23 #include "netifd.h" 24 #include "system.h" 25 #include "interface.h" 26 #include "interface-ip.h" 27 #include "proto.h" 28 29 static struct avl_tree handlers; 30 31 enum { 32 OPT_IPADDR, 33 OPT_IP6ADDR, 34 OPT_NETMASK, 35 OPT_BROADCAST, 36 OPT_PTPADDR, 37 OPT_GATEWAY, 38 OPT_IP6GW, 39 OPT_IP6PREFIX, 40 OPT_IP6DEPRECATED, 41 __OPT_MAX, 42 }; 43 44 static const struct blobmsg_policy proto_ip_attributes[__OPT_MAX] = { 45 [OPT_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY }, 46 [OPT_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY }, 47 [OPT_NETMASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING }, 48 [OPT_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING }, 49 [OPT_PTPADDR] = { .name = "ptpaddr", .type = BLOBMSG_TYPE_STRING }, 50 [OPT_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING }, 51 [OPT_IP6GW] = { .name = "ip6gw", .type = BLOBMSG_TYPE_STRING }, 52 [OPT_IP6PREFIX] = { .name = "ip6prefix", .type = BLOBMSG_TYPE_ARRAY }, 53 [OPT_IP6DEPRECATED] = { .name = "ip6deprecated", .type = BLOBMSG_TYPE_BOOL }, 54 }; 55 56 static const struct uci_blob_param_info proto_ip_attr_info[__OPT_MAX] = { 57 [OPT_IPADDR] = { .type = BLOBMSG_TYPE_STRING }, 58 [OPT_IP6ADDR] = { .type = BLOBMSG_TYPE_STRING }, 59 [OPT_IP6PREFIX] = { .type = BLOBMSG_TYPE_STRING }, 60 }; 61 62 static const char * const proto_ip_validate[__OPT_MAX] = { 63 [OPT_IPADDR] = "ip4addr", 64 [OPT_IP6ADDR] = "ip6addr", 65 [OPT_NETMASK] = "netmask", 66 [OPT_BROADCAST] = "ipaddr", 67 [OPT_PTPADDR] = "ip4addr", 68 [OPT_GATEWAY] = "ip4addr", 69 [OPT_IP6GW] = "ip6addr", 70 [OPT_IP6PREFIX] = "ip6addr", 71 }; 72 73 const struct uci_blob_param_list proto_ip_attr = { 74 .n_params = __OPT_MAX, 75 .params = proto_ip_attributes, 76 .validate = proto_ip_validate, 77 .info = proto_ip_attr_info, 78 }; 79 80 enum { 81 ADDR_IPADDR, 82 ADDR_MASK, 83 ADDR_BROADCAST, 84 ADDR_PTP, 85 ADDR_PREFERRED, 86 ADDR_VALID, 87 ADDR_OFFLINK, 88 ADDR_CLASS, 89 __ADDR_MAX 90 }; 91 92 static const struct blobmsg_policy proto_ip_addr[__ADDR_MAX] = { 93 [ADDR_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING }, 94 [ADDR_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_STRING }, 95 [ADDR_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING }, 96 [ADDR_PTP] = { .name = "ptp", .type = BLOBMSG_TYPE_STRING }, 97 [ADDR_PREFERRED] = { .name = "preferred", .type = BLOBMSG_TYPE_INT32 }, 98 [ADDR_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 }, 99 [ADDR_OFFLINK] = { .name = "offlink", .type = BLOBMSG_TYPE_BOOL }, 100 [ADDR_CLASS] = { .name = "class", .type = BLOBMSG_TYPE_STRING }, 101 }; 102 103 static struct device_addr * 104 alloc_device_addr(bool v6, bool ext) 105 { 106 struct device_addr *addr; 107 108 addr = calloc(1, sizeof(*addr)); 109 if (!addr) 110 return NULL; 111 112 addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4; 113 if (ext) 114 addr->flags |= DEVADDR_EXTERNAL; 115 116 return addr; 117 } 118 119 static struct device_addr * 120 parse_addr(const char *str, bool v6, int mask, bool ext, uint32_t broadcast, 121 uint32_t ptp, bool deprecated) 122 { 123 struct device_addr *addr; 124 int af = v6 ? AF_INET6 : AF_INET; 125 126 addr = alloc_device_addr(v6, ext); 127 if (!addr) 128 return NULL; 129 130 addr->mask = mask; 131 if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask)) 132 goto error; 133 134 if (v6 && IN6_IS_ADDR_MULTICAST(&addr->addr.in6)) 135 goto error; 136 137 if (broadcast) 138 addr->broadcast = broadcast; 139 140 if (ptp) 141 addr->point_to_point = ptp; 142 143 if (deprecated) 144 addr->preferred_until = system_get_rtime(); 145 146 return addr; 147 148 error: 149 free(addr); 150 151 return NULL; 152 } 153 154 static int 155 parse_static_address_option(struct interface *iface, struct blob_attr *attr, 156 bool v6, int netmask, bool ext, uint32_t broadcast, 157 uint32_t ptp, bool deprecated) 158 { 159 struct blob_attr *cur; 160 struct device_addr *addr; 161 const char *str; 162 int n_addr = 0; 163 size_t rem; 164 165 blobmsg_for_each_attr(cur, attr, rem) { 166 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) 167 return -1; 168 169 str = blobmsg_data(cur); 170 addr = parse_addr(str, v6, netmask, ext, broadcast, ptp, deprecated); 171 if (addr == NULL) { 172 interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1); 173 return -1; 174 } 175 addr->index = n_addr; 176 n_addr++; 177 vlist_add(&iface->proto_ip.addr, &addr->node, addr); 178 } 179 180 return n_addr; 181 } 182 183 static struct device_addr * 184 parse_address_item(struct blob_attr *attr, bool v6, bool ext) 185 { 186 struct device_addr *addr; 187 struct blob_attr *tb[__ADDR_MAX]; 188 struct blob_attr *cur; 189 190 if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE) 191 return NULL; 192 193 addr = alloc_device_addr(v6, ext); 194 if (!addr) 195 return NULL; 196 197 blobmsg_parse_attr(proto_ip_addr, __ADDR_MAX, tb, attr); 198 199 addr->mask = v6 ? 128 : 32; 200 if ((cur = tb[ADDR_MASK])) { 201 unsigned int new_mask; 202 203 new_mask = parse_netmask_string(blobmsg_data(cur), v6); 204 if (new_mask > addr->mask) 205 goto error; 206 207 addr->mask = new_mask; 208 } 209 210 cur = tb[ADDR_IPADDR]; 211 if (!cur) 212 goto error; 213 214 if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(cur), &addr->addr)) 215 goto error; 216 217 if ((cur = tb[ADDR_OFFLINK]) && blobmsg_get_bool(cur)) 218 addr->flags |= DEVADDR_OFFLINK; 219 220 if (!v6) { 221 if ((cur = tb[ADDR_BROADCAST]) && 222 !inet_pton(AF_INET, blobmsg_data(cur), &addr->broadcast)) 223 goto error; 224 if ((cur = tb[ADDR_PTP]) && 225 !inet_pton(AF_INET, blobmsg_data(cur), &addr->point_to_point)) 226 goto error; 227 } else { 228 time_t now = system_get_rtime(); 229 if ((cur = tb[ADDR_PREFERRED])) { 230 int64_t preferred = blobmsg_get_u32(cur); 231 int64_t preferred_until = preferred + (int64_t)now; 232 if (preferred_until <= LONG_MAX && preferred != 0xffffffffLL) 233 addr->preferred_until = preferred_until; 234 } 235 236 if ((cur = tb[ADDR_VALID])) { 237 int64_t valid = blobmsg_get_u32(cur); 238 int64_t valid_until = valid + (int64_t)now; 239 if (valid_until <= LONG_MAX && valid != 0xffffffffLL) 240 addr->valid_until = valid_until; 241 242 } 243 244 if (addr->valid_until) { 245 if (!addr->preferred_until) 246 addr->preferred_until = addr->valid_until; 247 else if (addr->preferred_until > addr->valid_until) 248 goto error; 249 } 250 251 if ((cur = tb[ADDR_CLASS])) 252 addr->pclass = strdup(blobmsg_get_string(cur)); 253 } 254 255 return addr; 256 257 error: 258 free(addr); 259 return NULL; 260 } 261 262 static int 263 parse_address_list(struct interface *iface, struct blob_attr *attr, bool v6, 264 bool ext) 265 { 266 struct device_addr *addr; 267 struct blob_attr *cur; 268 int n_addr = 0; 269 size_t rem; 270 271 blobmsg_for_each_attr(cur, attr, rem) { 272 addr = parse_address_item(cur, v6, ext); 273 if (!addr) 274 return -1; 275 276 addr->index = n_addr; 277 n_addr++; 278 vlist_add(&iface->proto_ip.addr, &addr->node, addr); 279 } 280 281 return n_addr; 282 } 283 284 static bool 285 parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6) 286 { 287 struct device_route *route; 288 const char *str = blobmsg_data(attr); 289 int af = v6 ? AF_INET6 : AF_INET; 290 291 route = calloc(1, sizeof(*route)); 292 if (!route) 293 return NULL; 294 295 if (!inet_pton(af, str, &route->nexthop)) { 296 interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1); 297 free(route); 298 return false; 299 } 300 301 route->mask = 0; 302 route->flags = (v6 ? DEVADDR_INET6 : DEVADDR_INET4); 303 route->metric = iface->metric; 304 305 unsigned int table = (v6) ? iface->ip6table : iface->ip4table; 306 if (table) { 307 route->table = table; 308 route->flags |= DEVROUTE_SRCTABLE; 309 } 310 311 vlist_add(&iface->proto_ip.route, &route->node, route); 312 313 return true; 314 } 315 316 static bool 317 parse_prefix_option(struct interface *iface, const char *str, size_t len) 318 { 319 char buf[128] = {0}, *saveptr; 320 if (len >= sizeof(buf)) 321 return false; 322 323 memcpy(buf, str, len); 324 char *addrstr = strtok_r(buf, "/", &saveptr); 325 if (!addrstr) 326 return false; 327 328 char *lengthstr = strtok_r(NULL, ",", &saveptr); 329 if (!lengthstr) 330 return false; 331 332 char *prefstr = strtok_r(NULL, ",", &saveptr); 333 char *validstr = (!prefstr) ? NULL : strtok_r(NULL, ",", &saveptr); 334 char *addstr = (!validstr) ? NULL : strtok_r(NULL, ",", &saveptr); 335 const char *pclass = NULL; 336 337 int64_t pref = (!prefstr) ? 0 : strtoul(prefstr, NULL, 10); 338 int64_t valid = (!validstr) ? 0 : strtoul(validstr, NULL, 10); 339 340 uint8_t length = strtoul(lengthstr, NULL, 10), excl_length = 0; 341 if (length < 1 || length > 64) 342 return false; 343 344 struct in6_addr addr, excluded, *excludedp = NULL; 345 if (inet_pton(AF_INET6, addrstr, &addr) < 1) 346 return false; 347 348 for (; addstr; addstr = strtok_r(NULL, ",", &saveptr)) { 349 char *key = NULL, *val = NULL, *addsaveptr; 350 if (!(key = strtok_r(addstr, "=", &addsaveptr)) || 351 !(val = strtok_r(NULL, ",", &addsaveptr))) 352 continue; 353 354 if (!strcmp(key, "excluded")) { 355 char *sep = strchr(val, '/'); 356 int excl; 357 358 if (!sep) 359 return false; 360 361 *sep = 0; 362 excl = atoi(sep + 1); 363 if (excl < length || excl > 128) 364 return false; 365 366 /* 367 * Assignments are made in /64 granularity: represent a 368 * longer exclusion by excluding the containing /64 369 */ 370 if (excl > 64) 371 excl = 64; 372 excl_length = excl; 373 374 if (inet_pton(AF_INET6, val, &excluded) < 1) 375 return false; 376 377 excludedp = &excluded; 378 } else if (!strcmp(key, "class")) { 379 pclass = val; 380 } 381 382 } 383 384 385 386 387 int64_t now = system_get_rtime(); 388 time_t preferred_until = 0; 389 if (prefstr && pref != 0xffffffffLL && pref + now <= LONG_MAX) 390 preferred_until = pref + now; 391 392 time_t valid_until = 0; 393 if (validstr && valid != 0xffffffffLL && valid + now <= LONG_MAX) 394 valid_until = valid + now; 395 396 interface_ip_add_device_prefix(iface, &addr, length, 397 valid_until, preferred_until, 398 excludedp, excl_length, pclass); 399 return true; 400 } 401 402 static int 403 parse_prefix_list(struct interface *iface, struct blob_attr *attr) 404 { 405 struct blob_attr *cur; 406 int n_addr = 0; 407 size_t rem; 408 409 blobmsg_for_each_attr(cur, attr, rem) { 410 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING) 411 return -1; 412 413 n_addr++; 414 if (!parse_prefix_option(iface, blobmsg_data(cur), 415 blobmsg_data_len(cur))) 416 return -1; 417 } 418 419 return n_addr; 420 } 421 422 int 423 proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr) 424 { 425 struct blob_attr *tb[__OPT_MAX]; 426 struct blob_attr *cur; 427 const char *error; 428 unsigned int netmask = 32; 429 bool ip6deprecated; 430 int n_v4 = 0, n_v6 = 0; 431 struct in_addr bcast = {0,}, ptp = {0,}; 432 433 blobmsg_parse_attr(proto_ip_attributes, __OPT_MAX, tb, attr); 434 435 if ((cur = tb[OPT_NETMASK])) { 436 netmask = parse_netmask_string(blobmsg_data(cur), false); 437 if (netmask > 32) { 438 error = "INVALID_NETMASK"; 439 goto error; 440 } 441 } 442 443 if ((cur = tb[OPT_BROADCAST])) { 444 if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) { 445 error = "INVALID_BROADCAST"; 446 goto error; 447 } 448 } 449 450 if ((cur = tb[OPT_PTPADDR])) { 451 if (!inet_pton(AF_INET, blobmsg_data(cur), &ptp)) { 452 error = "INVALID_PTPADDR"; 453 goto error; 454 } 455 } 456 457 ip6deprecated = blobmsg_get_bool_default(tb[OPT_IP6DEPRECATED], false); 458 459 if ((cur = tb[OPT_IPADDR])) 460 n_v4 = parse_static_address_option(iface, cur, false, 461 netmask, false, bcast.s_addr, ptp.s_addr, false); 462 463 if ((cur = tb[OPT_IP6ADDR])) 464 n_v6 = parse_static_address_option(iface, cur, true, 465 128, false, 0, 0, ip6deprecated); 466 467 if ((cur = tb[OPT_IP6PREFIX])) 468 if (parse_prefix_list(iface, cur) < 0) 469 goto out; 470 471 if (n_v4 < 0 || n_v6 < 0) 472 goto out; 473 474 if ((cur = tb[OPT_GATEWAY])) { 475 if (n_v4 && !parse_gateway_option(iface, cur, false)) 476 goto out; 477 } 478 479 if ((cur = tb[OPT_IP6GW])) { 480 if (n_v6 && !parse_gateway_option(iface, cur, true)) 481 goto out; 482 } 483 484 return 0; 485 486 error: 487 interface_add_error(iface, "proto", error, NULL, 0); 488 out: 489 return -1; 490 } 491 492 int 493 proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext) 494 { 495 struct blob_attr *tb[__OPT_MAX]; 496 struct blob_attr *cur; 497 int n_v4 = 0, n_v6 = 0; 498 499 blobmsg_parse_attr(proto_ip_attributes, __OPT_MAX, tb, attr); 500 501 if ((cur = tb[OPT_IPADDR])) 502 n_v4 = parse_address_list(iface, cur, false, ext); 503 504 if ((cur = tb[OPT_IP6ADDR])) 505 n_v6 = parse_address_list(iface, cur, true, ext); 506 507 if ((cur = tb[OPT_IP6PREFIX])) 508 if (parse_prefix_list(iface, cur) < 0) 509 goto out; 510 511 if (n_v4 < 0 || n_v6 < 0) 512 goto out; 513 514 if ((cur = tb[OPT_GATEWAY])) { 515 if (n_v4 && !parse_gateway_option(iface, cur, false)) 516 goto out; 517 } 518 519 if ((cur = tb[OPT_IP6GW])) { 520 if (n_v6 && !parse_gateway_option(iface, cur, true)) 521 goto out; 522 } 523 524 return 0; 525 526 out: 527 return -1; 528 } 529 530 void add_proto_handler(struct proto_handler *p) 531 { 532 if (!handlers.comp) 533 avl_init(&handlers, avl_strcmp, false, NULL); 534 535 if (p->avl.key) 536 return; 537 538 p->avl.key = p->name; 539 avl_insert(&handlers, &p->avl); 540 } 541 542 static void 543 default_proto_free(struct interface_proto_state *proto) 544 { 545 free(proto); 546 } 547 548 static int 549 invalid_proto_handler(struct interface_proto_state *proto, 550 enum interface_proto_cmd cmd, bool force) 551 { 552 return -1; 553 } 554 555 static int 556 no_proto_handler(struct interface_proto_state *proto, 557 enum interface_proto_cmd cmd, bool force) 558 { 559 return 0; 560 } 561 562 static struct interface_proto_state * 563 default_proto_attach(const struct proto_handler *h, 564 struct interface *iface, struct blob_attr *attr) 565 { 566 struct interface_proto_state *proto; 567 568 proto = calloc(1, sizeof(*proto)); 569 if (!proto) 570 return NULL; 571 572 proto->free = default_proto_free; 573 proto->cb = no_proto_handler; 574 575 return proto; 576 } 577 578 static const struct proto_handler no_proto = { 579 .name = "none", 580 .flags = PROTO_FLAG_IMMEDIATE, 581 .attach = default_proto_attach, 582 }; 583 584 static const struct proto_handler * 585 get_proto_handler(const char *name) 586 { 587 struct proto_handler *proto; 588 589 if (!strcmp(name, "none")) 590 return &no_proto; 591 592 if (!handlers.comp) 593 return NULL; 594 595 return avl_find_element(&handlers, name, proto, avl); 596 } 597 598 void 599 proto_dump_handlers(struct blob_buf *b) 600 { 601 struct proto_handler *p; 602 void *c; 603 604 avl_for_each_element(&handlers, p, avl) { 605 void *v; 606 607 c = blobmsg_open_table(b, p->name); 608 if (p->config_params && p->config_params->validate) { 609 int i; 610 611 v = blobmsg_open_table(b, "validate"); 612 for (i = 0; i < p->config_params->n_params; i++) 613 blobmsg_add_string(b, p->config_params->params[i].name, uci_get_validate_string(p->config_params, i)); 614 blobmsg_close_table(b, v); 615 } 616 blobmsg_add_u8(b, "immediate", !!(p->flags & PROTO_FLAG_IMMEDIATE)); 617 blobmsg_add_u8(b, "no_device", !!(p->flags & PROTO_FLAG_NODEV)); 618 blobmsg_add_u8(b, "init_available", !!(p->flags & PROTO_FLAG_INIT_AVAILABLE)); 619 blobmsg_add_u8(b, "renew_available", !!(p->flags & PROTO_FLAG_RENEW_AVAILABLE)); 620 blobmsg_add_u8(b, "force_link_default", !!(p->flags & PROTO_FLAG_FORCE_LINK_DEFAULT)); 621 blobmsg_add_u8(b, "last_error", !!(p->flags & PROTO_FLAG_LASTERROR)); 622 blobmsg_add_u8(b, "teardown_on_l3_link_down", !!(p->flags & PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN)); 623 blobmsg_add_u8(b, "no_task", !!(p->flags & PROTO_FLAG_NO_TASK)); 624 blobmsg_close_table(b, c); 625 } 626 } 627 628 void 629 proto_init_interface(struct interface *iface, struct blob_attr *attr) 630 { 631 const struct proto_handler *proto = iface->proto_handler; 632 struct interface_proto_state *state = NULL; 633 634 if (!proto) 635 proto = &no_proto; 636 637 state = proto->attach(proto, iface, attr); 638 if (!state) { 639 state = no_proto.attach(&no_proto, iface, attr); 640 state->cb = invalid_proto_handler; 641 } 642 643 state->handler = proto; 644 interface_set_proto_state(iface, state); 645 } 646 647 void 648 proto_attach_interface(struct interface *iface, const char *proto_name) 649 { 650 const struct proto_handler *proto = &no_proto; 651 const char *error = NULL; 652 653 if (proto_name) { 654 proto = get_proto_handler(proto_name); 655 if (!proto) { 656 error = "INVALID_PROTO"; 657 proto = &no_proto; 658 } 659 } 660 661 iface->proto_handler = proto; 662 663 if (error) 664 interface_add_error(iface, "proto", error, NULL, 0); 665 } 666 667 int 668 interface_proto_event(struct interface_proto_state *proto, 669 enum interface_proto_cmd cmd, bool force) 670 { 671 enum interface_proto_event ev; 672 int ret; 673 674 ret = proto->cb(proto, cmd, force); 675 if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE)) 676 goto out; 677 678 switch(cmd) { 679 case PROTO_CMD_SETUP: 680 ev = IFPEV_UP; 681 break; 682 case PROTO_CMD_TEARDOWN: 683 ev = IFPEV_DOWN; 684 break; 685 case PROTO_CMD_RENEW: 686 case PROTO_CMD_RESTART: 687 ev = IFPEV_RENEW; 688 break; 689 default: 690 return -EINVAL; 691 } 692 proto->proto_event(proto, ev); 693 694 out: 695 return ret; 696 } 697
This page was automatically generated by LXR 0.3.1. • OpenWrt