proto.c 14 KB

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