proto.c 13 KB

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