proto.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. #include <arpa/inet.h>
  20. #include <netinet/in.h>
  21. #include "netifd.h"
  22. #include "system.h"
  23. #include "interface.h"
  24. #include "interface-ip.h"
  25. #include "proto.h"
  26. static struct avl_tree handlers;
  27. enum {
  28. OPT_IPADDR,
  29. OPT_IP6ADDR,
  30. OPT_NETMASK,
  31. OPT_BROADCAST,
  32. OPT_GATEWAY,
  33. OPT_IP6GW,
  34. OPT_IP6PREFIX,
  35. __OPT_MAX,
  36. };
  37. static const struct blobmsg_policy proto_ip_attributes[__OPT_MAX] = {
  38. [OPT_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_ARRAY },
  39. [OPT_IP6ADDR] = { .name = "ip6addr", .type = BLOBMSG_TYPE_ARRAY },
  40. [OPT_NETMASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
  41. [OPT_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
  42. [OPT_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
  43. [OPT_IP6GW] = { .name = "ip6gw", .type = BLOBMSG_TYPE_STRING },
  44. [OPT_IP6PREFIX] = { .name = "ip6prefix", .type = BLOBMSG_TYPE_ARRAY },
  45. };
  46. static const struct uci_blob_param_info proto_ip_attr_info[__OPT_MAX] = {
  47. [OPT_IPADDR] = { .type = BLOBMSG_TYPE_STRING },
  48. [OPT_IP6ADDR] = { .type = BLOBMSG_TYPE_STRING },
  49. [OPT_IP6PREFIX] = { .type = BLOBMSG_TYPE_STRING },
  50. };
  51. static const char * const proto_ip_validate[__OPT_MAX] = {
  52. [OPT_IPADDR] = "ip4addr",
  53. [OPT_IP6ADDR] = "ip6addr",
  54. [OPT_NETMASK] = "netmask",
  55. [OPT_BROADCAST] = "ipaddr",
  56. [OPT_GATEWAY] = "ip4addr",
  57. [OPT_IP6GW] = "ip6addr",
  58. [OPT_IP6PREFIX] = "ip6addr",
  59. };
  60. const struct uci_blob_param_list proto_ip_attr = {
  61. .n_params = __OPT_MAX,
  62. .params = proto_ip_attributes,
  63. .validate = proto_ip_validate,
  64. .info = proto_ip_attr_info,
  65. };
  66. enum {
  67. ADDR_IPADDR,
  68. ADDR_MASK,
  69. ADDR_BROADCAST,
  70. ADDR_PTP,
  71. ADDR_PREFERRED,
  72. ADDR_VALID,
  73. ADDR_OFFLINK,
  74. ADDR_CLASS,
  75. __ADDR_MAX
  76. };
  77. static const struct blobmsg_policy proto_ip_addr[__ADDR_MAX] = {
  78. [ADDR_IPADDR] = { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING },
  79. [ADDR_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_STRING },
  80. [ADDR_BROADCAST] = { .name = "broadcast", .type = BLOBMSG_TYPE_STRING },
  81. [ADDR_PTP] = { .name = "ptp", .type = BLOBMSG_TYPE_STRING },
  82. [ADDR_PREFERRED] = { .name = "preferred", .type = BLOBMSG_TYPE_INT32 },
  83. [ADDR_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
  84. [ADDR_OFFLINK] = { .name = "offlink", .type = BLOBMSG_TYPE_BOOL },
  85. [ADDR_CLASS] = { .name = "class", .type = BLOBMSG_TYPE_STRING },
  86. };
  87. static struct device_addr *
  88. alloc_device_addr(bool v6, bool ext)
  89. {
  90. struct device_addr *addr;
  91. addr = calloc(1, sizeof(*addr));
  92. if (!addr)
  93. return NULL;
  94. addr->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
  95. if (ext)
  96. addr->flags |= DEVADDR_EXTERNAL;
  97. return addr;
  98. }
  99. static bool
  100. parse_addr(struct interface *iface, const char *str, bool v6, int mask,
  101. bool ext, uint32_t broadcast)
  102. {
  103. struct device_addr *addr;
  104. int af = v6 ? AF_INET6 : AF_INET;
  105. addr = alloc_device_addr(v6, ext);
  106. if (!addr)
  107. return false;
  108. addr->mask = mask;
  109. if (!parse_ip_and_netmask(af, str, &addr->addr, &addr->mask))
  110. goto error;
  111. if (!v6) {
  112. if (IN_EXPERIMENTAL(ntohl(addr->addr.in.s_addr)))
  113. goto error;
  114. } else if (IN6_IS_ADDR_MULTICAST(&addr->addr.in6))
  115. goto error;
  116. if (broadcast)
  117. addr->broadcast = broadcast;
  118. vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
  119. return true;
  120. error:
  121. interface_add_error(iface, "proto", "INVALID_ADDRESS", &str, 1);
  122. free(addr);
  123. return false;
  124. }
  125. static int
  126. parse_static_address_option(struct interface *iface, struct blob_attr *attr,
  127. bool v6, int netmask, bool ext, uint32_t broadcast)
  128. {
  129. struct blob_attr *cur;
  130. int n_addr = 0;
  131. int rem;
  132. blobmsg_for_each_attr(cur, attr, rem) {
  133. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  134. return -1;
  135. n_addr++;
  136. if (!parse_addr(iface, blobmsg_data(cur), v6, netmask, ext,
  137. broadcast))
  138. return -1;
  139. }
  140. return n_addr;
  141. }
  142. static struct device_addr *
  143. parse_address_item(struct blob_attr *attr, bool v6, bool ext)
  144. {
  145. struct device_addr *addr;
  146. struct blob_attr *tb[__ADDR_MAX];
  147. struct blob_attr *cur;
  148. if (blobmsg_type(attr) != BLOBMSG_TYPE_TABLE)
  149. return NULL;
  150. addr = alloc_device_addr(v6, ext);
  151. if (!addr)
  152. return NULL;
  153. blobmsg_parse(proto_ip_addr, __ADDR_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
  154. addr->mask = v6 ? 128 : 32;
  155. if ((cur = tb[ADDR_MASK])) {
  156. unsigned int new_mask;
  157. new_mask = parse_netmask_string(blobmsg_data(cur), v6);
  158. if (new_mask > addr->mask)
  159. goto error;
  160. addr->mask = new_mask;
  161. }
  162. cur = tb[ADDR_IPADDR];
  163. if (!cur)
  164. goto error;
  165. if (!inet_pton(v6 ? AF_INET6 : AF_INET, blobmsg_data(cur), &addr->addr))
  166. goto error;
  167. if ((cur = tb[ADDR_OFFLINK]) && blobmsg_get_bool(cur))
  168. addr->flags |= DEVADDR_OFFLINK;
  169. if (!v6) {
  170. if ((cur = tb[ADDR_BROADCAST]) &&
  171. !inet_pton(AF_INET, blobmsg_data(cur), &addr->broadcast))
  172. goto error;
  173. if ((cur = tb[ADDR_PTP]) &&
  174. !inet_pton(AF_INET, blobmsg_data(cur), &addr->point_to_point))
  175. goto error;
  176. } else {
  177. time_t now = system_get_rtime();
  178. if ((cur = tb[ADDR_PREFERRED])) {
  179. int64_t preferred = blobmsg_get_u32(cur);
  180. int64_t preferred_until = preferred + (int64_t)now;
  181. if (preferred_until <= LONG_MAX && preferred != 0xffffffffLL)
  182. addr->preferred_until = preferred_until;
  183. }
  184. if ((cur = tb[ADDR_VALID])) {
  185. int64_t valid = blobmsg_get_u32(cur);
  186. int64_t valid_until = valid + (int64_t)now;
  187. if (valid_until <= LONG_MAX && valid != 0xffffffffLL)
  188. addr->valid_until = valid_until;
  189. }
  190. if (addr->valid_until) {
  191. if (!addr->preferred_until)
  192. addr->preferred_until = addr->valid_until;
  193. else if (addr->preferred_until > addr->valid_until)
  194. goto error;
  195. }
  196. if ((cur = tb[ADDR_CLASS]))
  197. addr->pclass = strdup(blobmsg_get_string(cur));
  198. }
  199. return addr;
  200. error:
  201. free(addr);
  202. return NULL;
  203. }
  204. static int
  205. parse_address_list(struct interface *iface, struct blob_attr *attr, bool v6,
  206. bool ext)
  207. {
  208. struct device_addr *addr;
  209. struct blob_attr *cur;
  210. int n_addr = 0;
  211. int rem;
  212. blobmsg_for_each_attr(cur, attr, rem) {
  213. addr = parse_address_item(cur, v6, ext);
  214. if (!addr)
  215. return -1;
  216. n_addr++;
  217. vlist_add(&iface->proto_ip.addr, &addr->node, &addr->flags);
  218. }
  219. return n_addr;
  220. }
  221. static bool
  222. parse_gateway_option(struct interface *iface, struct blob_attr *attr, bool v6)
  223. {
  224. struct device_route *route;
  225. const char *str = blobmsg_data(attr);
  226. int af = v6 ? AF_INET6 : AF_INET;
  227. route = calloc(1, sizeof(*route));
  228. if (!route)
  229. return NULL;
  230. if (!inet_pton(af, str, &route->nexthop)) {
  231. interface_add_error(iface, "proto", "INVALID_GATEWAY", &str, 1);
  232. free(route);
  233. return false;
  234. }
  235. route->mask = 0;
  236. route->flags = (v6 ? DEVADDR_INET6 : DEVADDR_INET4);
  237. route->metric = iface->metric;
  238. unsigned int table = (v6) ? iface->ip6table : iface->ip4table;
  239. if (table) {
  240. route->table = table;
  241. route->flags |= DEVROUTE_SRCTABLE;
  242. }
  243. vlist_add(&iface->proto_ip.route, &route->node, route);
  244. return true;
  245. }
  246. static bool
  247. parse_prefix_option(struct interface *iface, const char *str, size_t len)
  248. {
  249. char buf[128] = {0}, *saveptr;
  250. if (len >= sizeof(buf))
  251. return false;
  252. memcpy(buf, str, len);
  253. char *addrstr = strtok_r(buf, "/", &saveptr);
  254. if (!addrstr)
  255. return false;
  256. char *lengthstr = strtok_r(NULL, ",", &saveptr);
  257. if (!lengthstr)
  258. return false;
  259. char *prefstr = strtok_r(NULL, ",", &saveptr);
  260. char *validstr = (!prefstr) ? NULL : strtok_r(NULL, ",", &saveptr);
  261. char *addstr = (!validstr) ? NULL : strtok_r(NULL, ",", &saveptr);
  262. const char *pclass = NULL;
  263. int64_t pref = (!prefstr) ? 0 : strtoul(prefstr, NULL, 10);
  264. int64_t valid = (!validstr) ? 0 : strtoul(validstr, NULL, 10);
  265. uint8_t length = strtoul(lengthstr, NULL, 10), excl_length = 0;
  266. if (length < 1 || length > 64)
  267. return false;
  268. struct in6_addr addr, excluded, *excludedp = NULL;
  269. if (inet_pton(AF_INET6, addrstr, &addr) < 1)
  270. return false;
  271. for (; addstr; addstr = strtok_r(NULL, ",", &saveptr)) {
  272. char *key = NULL, *val = NULL, *addsaveptr;
  273. if (!(key = strtok_r(addstr, "=", &addsaveptr)) ||
  274. !(val = strtok_r(NULL, ",", &addsaveptr)))
  275. continue;
  276. if (!strcmp(key, "excluded")) {
  277. char *sep = strchr(val, '/');
  278. if (!sep)
  279. return false;
  280. *sep = 0;
  281. excl_length = atoi(sep + 1);
  282. if (inet_pton(AF_INET6, val, &excluded) < 1)
  283. return false;
  284. excludedp = &excluded;
  285. } else if (!strcmp(key, "class")) {
  286. pclass = val;
  287. }
  288. }
  289. int64_t now = system_get_rtime();
  290. time_t preferred_until = 0;
  291. if (prefstr && pref != 0xffffffffLL && pref + now <= LONG_MAX)
  292. preferred_until = pref + now;
  293. time_t valid_until = 0;
  294. if (validstr && valid != 0xffffffffLL && valid + now <= LONG_MAX)
  295. valid_until = valid + now;
  296. interface_ip_add_device_prefix(iface, &addr, length,
  297. valid_until, preferred_until,
  298. excludedp, excl_length, pclass);
  299. return true;
  300. }
  301. static int
  302. parse_prefix_list(struct interface *iface, struct blob_attr *attr)
  303. {
  304. struct blob_attr *cur;
  305. int n_addr = 0;
  306. int rem;
  307. blobmsg_for_each_attr(cur, attr, rem) {
  308. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  309. return -1;
  310. n_addr++;
  311. if (!parse_prefix_option(iface, blobmsg_data(cur),
  312. blobmsg_data_len(cur)))
  313. return -1;
  314. }
  315. return n_addr;
  316. }
  317. int
  318. proto_apply_static_ip_settings(struct interface *iface, struct blob_attr *attr)
  319. {
  320. struct blob_attr *tb[__OPT_MAX];
  321. struct blob_attr *cur;
  322. const char *error;
  323. unsigned int netmask = 32;
  324. int n_v4 = 0, n_v6 = 0;
  325. struct in_addr bcast = {};
  326. blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
  327. if ((cur = tb[OPT_NETMASK])) {
  328. netmask = parse_netmask_string(blobmsg_data(cur), false);
  329. if (netmask > 32) {
  330. error = "INVALID_NETMASK";
  331. goto error;
  332. }
  333. }
  334. if ((cur = tb[OPT_BROADCAST])) {
  335. if (!inet_pton(AF_INET, blobmsg_data(cur), &bcast)) {
  336. error = "INVALID_BROADCAST";
  337. goto error;
  338. }
  339. }
  340. if ((cur = tb[OPT_IPADDR]))
  341. n_v4 = parse_static_address_option(iface, cur, false,
  342. netmask, false, bcast.s_addr);
  343. if ((cur = tb[OPT_IP6ADDR]))
  344. n_v6 = parse_static_address_option(iface, cur, true,
  345. 128, false, 0);
  346. if ((cur = tb[OPT_IP6PREFIX]))
  347. if (parse_prefix_list(iface, cur) < 0)
  348. goto out;
  349. if (n_v4 < 0 || n_v6 < 0)
  350. goto out;
  351. if ((cur = tb[OPT_GATEWAY])) {
  352. if (n_v4 && !parse_gateway_option(iface, cur, false))
  353. goto out;
  354. }
  355. if ((cur = tb[OPT_IP6GW])) {
  356. if (n_v6 && !parse_gateway_option(iface, cur, true))
  357. goto out;
  358. }
  359. return 0;
  360. error:
  361. interface_add_error(iface, "proto", error, NULL, 0);
  362. out:
  363. return -1;
  364. }
  365. int
  366. proto_apply_ip_settings(struct interface *iface, struct blob_attr *attr, bool ext)
  367. {
  368. struct blob_attr *tb[__OPT_MAX];
  369. struct blob_attr *cur;
  370. int n_v4 = 0, n_v6 = 0;
  371. blobmsg_parse(proto_ip_attributes, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
  372. if ((cur = tb[OPT_IPADDR]))
  373. n_v4 = parse_address_list(iface, cur, false, ext);
  374. if ((cur = tb[OPT_IP6ADDR]))
  375. n_v6 = parse_address_list(iface, cur, true, ext);
  376. if ((cur = tb[OPT_IP6PREFIX]))
  377. if (parse_prefix_list(iface, cur) < 0)
  378. goto out;
  379. if (n_v4 < 0 || n_v6 < 0)
  380. goto out;
  381. if ((cur = tb[OPT_GATEWAY])) {
  382. if (n_v4 && !parse_gateway_option(iface, cur, false))
  383. goto out;
  384. }
  385. if ((cur = tb[OPT_IP6GW])) {
  386. if (n_v6 && !parse_gateway_option(iface, cur, true))
  387. goto out;
  388. }
  389. return 0;
  390. out:
  391. return -1;
  392. }
  393. void add_proto_handler(struct proto_handler *p)
  394. {
  395. if (!handlers.comp)
  396. avl_init(&handlers, avl_strcmp, false, NULL);
  397. if (p->avl.key)
  398. return;
  399. p->avl.key = p->name;
  400. avl_insert(&handlers, &p->avl);
  401. }
  402. static void
  403. default_proto_free(struct interface_proto_state *proto)
  404. {
  405. free(proto);
  406. }
  407. static int
  408. invalid_proto_handler(struct interface_proto_state *proto,
  409. enum interface_proto_cmd cmd, bool force)
  410. {
  411. return -1;
  412. }
  413. static int
  414. no_proto_handler(struct interface_proto_state *proto,
  415. enum interface_proto_cmd cmd, bool force)
  416. {
  417. return 0;
  418. }
  419. static struct interface_proto_state *
  420. default_proto_attach(const struct proto_handler *h,
  421. struct interface *iface, struct blob_attr *attr)
  422. {
  423. struct interface_proto_state *proto;
  424. proto = calloc(1, sizeof(*proto));
  425. if (!proto)
  426. return NULL;
  427. proto->free = default_proto_free;
  428. proto->cb = no_proto_handler;
  429. return proto;
  430. }
  431. static const struct proto_handler no_proto = {
  432. .name = "none",
  433. .flags = PROTO_FLAG_IMMEDIATE,
  434. .attach = default_proto_attach,
  435. };
  436. static const struct proto_handler *
  437. get_proto_handler(const char *name)
  438. {
  439. struct proto_handler *proto;
  440. if (!strcmp(name, "none"))
  441. return &no_proto;
  442. if (!handlers.comp)
  443. return NULL;
  444. return avl_find_element(&handlers, name, proto, avl);
  445. }
  446. void
  447. proto_dump_handlers(struct blob_buf *b)
  448. {
  449. struct proto_handler *p;
  450. void *c;
  451. avl_for_each_element(&handlers, p, avl) {
  452. void *v;
  453. c = blobmsg_open_table(b, p->name);
  454. if (p->config_params->validate) {
  455. int i;
  456. v = blobmsg_open_table(b, "validate");
  457. for (i = 0; i < p->config_params->n_params; i++)
  458. blobmsg_add_string(b, p->config_params->params[i].name, uci_get_validate_string(p->config_params, i));
  459. blobmsg_close_table(b, v);
  460. }
  461. blobmsg_add_u8(b, "immediate", !!(p->flags & PROTO_FLAG_IMMEDIATE));
  462. blobmsg_add_u8(b, "no_device", !!(p->flags & PROTO_FLAG_NODEV));
  463. blobmsg_add_u8(b, "init_available", !!(p->flags & PROTO_FLAG_INIT_AVAILABLE));
  464. blobmsg_add_u8(b, "renew_available", !!(p->flags & PROTO_FLAG_RENEW_AVAILABLE));
  465. blobmsg_add_u8(b, "force_link_default", !!(p->flags & PROTO_FLAG_FORCE_LINK_DEFAULT));
  466. blobmsg_add_u8(b, "last_error", !!(p->flags & PROTO_FLAG_LASTERROR));
  467. blobmsg_add_u8(b, "teardown_on_l3_link_down", !!(p->flags & PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN));
  468. blobmsg_add_u8(b, "no_task", !!(p->flags & PROTO_FLAG_NO_TASK));
  469. blobmsg_close_table(b, c);
  470. }
  471. }
  472. void
  473. proto_init_interface(struct interface *iface, struct blob_attr *attr)
  474. {
  475. const struct proto_handler *proto = iface->proto_handler;
  476. struct interface_proto_state *state = NULL;
  477. if (!proto)
  478. proto = &no_proto;
  479. state = proto->attach(proto, iface, attr);
  480. if (!state) {
  481. state = no_proto.attach(&no_proto, iface, attr);
  482. state->cb = invalid_proto_handler;
  483. }
  484. state->handler = proto;
  485. interface_set_proto_state(iface, state);
  486. }
  487. void
  488. proto_attach_interface(struct interface *iface, const char *proto_name)
  489. {
  490. const struct proto_handler *proto = &no_proto;
  491. const char *error = NULL;
  492. if (proto_name) {
  493. proto = get_proto_handler(proto_name);
  494. if (!proto) {
  495. error = "INVALID_PROTO";
  496. proto = &no_proto;
  497. }
  498. }
  499. iface->proto_handler = proto;
  500. if (error)
  501. interface_add_error(iface, "proto", error, NULL, 0);
  502. }
  503. int
  504. interface_proto_event(struct interface_proto_state *proto,
  505. enum interface_proto_cmd cmd, bool force)
  506. {
  507. enum interface_proto_event ev;
  508. int ret;
  509. ret = proto->cb(proto, cmd, force);
  510. if (ret || !(proto->handler->flags & PROTO_FLAG_IMMEDIATE))
  511. goto out;
  512. switch(cmd) {
  513. case PROTO_CMD_SETUP:
  514. ev = IFPEV_UP;
  515. break;
  516. case PROTO_CMD_TEARDOWN:
  517. ev = IFPEV_DOWN;
  518. break;
  519. case PROTO_CMD_RENEW:
  520. ev = IFPEV_RENEW;
  521. break;
  522. default:
  523. return -EINVAL;
  524. }
  525. proto->proto_event(proto, ev);
  526. out:
  527. return ret;
  528. }