interface-ip.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  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 <libgen.h>
  19. #include <sys/stat.h>
  20. #include <limits.h>
  21. #include <arpa/inet.h>
  22. #include <netinet/in.h>
  23. #include "netifd.h"
  24. #include "device.h"
  25. #include "interface.h"
  26. #include "interface-ip.h"
  27. #include "proto.h"
  28. #include "ubus.h"
  29. #include "system.h"
  30. enum {
  31. ROUTE_INTERFACE,
  32. ROUTE_TARGET,
  33. ROUTE_MASK,
  34. ROUTE_GATEWAY,
  35. ROUTE_METRIC,
  36. ROUTE_MTU,
  37. ROUTE_VALID,
  38. ROUTE_TABLE,
  39. ROUTE_SOURCE,
  40. ROUTE_ONLINK,
  41. ROUTE_TYPE,
  42. ROUTE_PROTO,
  43. ROUTE_DISABLED,
  44. __ROUTE_MAX
  45. };
  46. static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
  47. [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
  48. [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
  49. [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
  50. [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
  51. [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
  52. [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
  53. [ROUTE_TABLE] = { .name = "table", .type = BLOBMSG_TYPE_STRING },
  54. [ROUTE_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
  55. [ROUTE_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_STRING },
  56. [ROUTE_ONLINK] = { .name = "onlink", .type = BLOBMSG_TYPE_BOOL },
  57. [ROUTE_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
  58. [ROUTE_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
  59. [ROUTE_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL },
  60. };
  61. const struct uci_blob_param_list route_attr_list = {
  62. .n_params = __ROUTE_MAX,
  63. .params = route_attr,
  64. };
  65. enum {
  66. NEIGHBOR_INTERFACE,
  67. NEIGHBOR_ADDRESS,
  68. NEIGHBOR_MAC,
  69. NEIGHBOR_PROXY,
  70. NEIGHBOR_ROUTER,
  71. __NEIGHBOR_MAX
  72. };
  73. static const struct blobmsg_policy neighbor_attr[__NEIGHBOR_MAX]={
  74. [NEIGHBOR_INTERFACE]= { .name = "interface", .type = BLOBMSG_TYPE_STRING},
  75. [NEIGHBOR_ADDRESS]= { .name = "ipaddr", .type = BLOBMSG_TYPE_STRING},
  76. [NEIGHBOR_MAC]= { .name = "mac", .type = BLOBMSG_TYPE_STRING},
  77. [NEIGHBOR_PROXY]= { .name = "proxy", .type = BLOBMSG_TYPE_BOOL},
  78. [NEIGHBOR_ROUTER]= {.name = "router", .type = BLOBMSG_TYPE_BOOL},
  79. };
  80. const struct uci_blob_param_list neighbor_attr_list = {
  81. .n_params = __NEIGHBOR_MAX,
  82. .params = neighbor_attr,
  83. };
  84. struct list_head prefixes = LIST_HEAD_INIT(prefixes);
  85. static struct device_prefix *ula_prefix = NULL;
  86. static struct uloop_timeout valid_until_timeout;
  87. static void
  88. clear_if_addr(union if_addr *a, int mask)
  89. {
  90. size_t m_bytes = (mask + 7) / 8;
  91. uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
  92. uint8_t *p = (uint8_t *) a;
  93. if (m_bytes < sizeof(*a))
  94. memset(p + m_bytes, 0, sizeof(*a) - m_bytes);
  95. p[m_bytes - 1] &= ~m_clear;
  96. }
  97. static bool
  98. match_if_addr(union if_addr *a1, union if_addr *a2, int mask)
  99. {
  100. union if_addr *p1, *p2;
  101. p1 = alloca(sizeof(*a1));
  102. p2 = alloca(sizeof(*a2));
  103. memcpy(p1, a1, sizeof(*a1));
  104. clear_if_addr(p1, mask);
  105. memcpy(p2, a2, sizeof(*a2));
  106. clear_if_addr(p2, mask);
  107. return !memcmp(p1, p2, sizeof(*p1));
  108. }
  109. static int set_ip_source_policy(bool add, bool v6, unsigned int priority,
  110. const union if_addr *addr, uint8_t mask, unsigned int table,
  111. struct interface *in_iface, const char *action, bool src)
  112. {
  113. struct iprule rule = {
  114. .flags = IPRULE_PRIORITY,
  115. .priority = priority
  116. };
  117. if (addr) {
  118. if (src) {
  119. rule.flags |= IPRULE_SRC;
  120. rule.src_addr = *addr;
  121. rule.src_mask = mask;
  122. } else {
  123. rule.flags |= IPRULE_DEST;
  124. rule.dest_addr = *addr;
  125. rule.dest_mask = mask;
  126. }
  127. }
  128. if (table) {
  129. rule.flags |= IPRULE_LOOKUP;
  130. rule.lookup = table;
  131. if (!rule.lookup)
  132. return 0;
  133. } else if (action) {
  134. rule.flags |= IPRULE_ACTION;
  135. system_resolve_iprule_action(action, &rule.action);
  136. }
  137. if (in_iface && in_iface->l3_dev.dev) {
  138. rule.flags |= IPRULE_IN;
  139. strcpy(rule.in_dev, in_iface->l3_dev.dev->ifname);
  140. }
  141. rule.flags |= (v6) ? IPRULE_INET6 : IPRULE_INET4;
  142. return (add) ? system_add_iprule(&rule) : system_del_iprule(&rule);
  143. }
  144. static int set_ip_lo_policy(bool add, bool v6, struct interface *iface)
  145. {
  146. struct iprule rule = {
  147. .flags = IPRULE_IN | IPRULE_LOOKUP | IPRULE_PRIORITY,
  148. .priority = IPRULE_PRIORITY_NW + iface->l3_dev.dev->ifindex,
  149. .lookup = (v6) ? iface->ip6table : iface->ip4table,
  150. .in_dev = "lo"
  151. };
  152. if (!rule.lookup)
  153. return 0;
  154. rule.flags |= (v6) ? IPRULE_INET6 : IPRULE_INET4;
  155. return (add) ? system_add_iprule(&rule) : system_del_iprule(&rule);
  156. }
  157. static bool
  158. __find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v6)
  159. {
  160. struct device_addr *addr;
  161. vlist_for_each_element(&ip->addr, addr, node) {
  162. if (!addr->enabled)
  163. continue;
  164. if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
  165. continue;
  166. if (((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4) &&
  167. addr->point_to_point && a->in.s_addr == addr->point_to_point)
  168. return true;
  169. /* Handle offlink addresses correctly */
  170. unsigned int mask = addr->mask;
  171. if ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6 &&
  172. (addr->flags & DEVADDR_OFFLINK))
  173. mask = 128;
  174. if (!match_if_addr(&addr->addr, a, mask))
  175. continue;
  176. return true;
  177. }
  178. return false;
  179. }
  180. static void
  181. __find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a,
  182. bool v6, struct device_route **res)
  183. {
  184. struct device_route *route;
  185. vlist_for_each_element(&ip->route, route, node) {
  186. if (!route->enabled)
  187. continue;
  188. if (v6 != ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
  189. continue;
  190. if (!match_if_addr(&route->addr, a, route->mask))
  191. continue;
  192. if (route->flags & DEVROUTE_TABLE)
  193. continue;
  194. if (!*res || route->mask > (*res)->mask ||
  195. ((route->mask == (*res)->mask) && (route->flags & DEVROUTE_METRIC)
  196. && (route->metric < (*res)->metric)))
  197. *res = route;
  198. }
  199. }
  200. static bool
  201. interface_ip_find_addr_target(struct interface *iface, union if_addr *a, bool v6)
  202. {
  203. return __find_ip_addr_target(&iface->proto_ip, a, v6) ||
  204. __find_ip_addr_target(&iface->config_ip, a, v6);
  205. }
  206. static void
  207. interface_ip_find_route_target(struct interface *iface, union if_addr *a,
  208. bool v6, struct device_route **route)
  209. {
  210. __find_ip_route_target(&iface->proto_ip, a, v6, route);
  211. __find_ip_route_target(&iface->config_ip, a, v6, route);
  212. }
  213. struct interface *
  214. interface_ip_add_target_route(union if_addr *addr, bool v6, struct interface *iface,
  215. bool exclude)
  216. {
  217. struct device_route *route, *r_next = NULL;
  218. bool defaultroute_target = false;
  219. union if_addr addr_zero;
  220. int addrsize = v6 ? sizeof(addr->in6) : sizeof(addr->in);
  221. struct interface *exclude_iface = NULL;
  222. if (exclude) {
  223. exclude_iface = iface;
  224. iface = NULL;
  225. }
  226. memset(&addr_zero, 0, sizeof(addr_zero));
  227. if (memcmp(&addr_zero, addr, addrsize) == 0)
  228. defaultroute_target = true;
  229. if (iface) {
  230. /* look for locally addressable target first */
  231. if (interface_ip_find_addr_target(iface, addr, v6))
  232. return iface;
  233. /* do not stop at the first route, let the lookup compare
  234. * masks to find the best match */
  235. interface_ip_find_route_target(iface, addr, v6, &r_next);
  236. } else {
  237. vlist_for_each_element(&interfaces, iface, node) {
  238. if (iface == exclude_iface)
  239. continue;
  240. /* look for locally addressable target first */
  241. if (interface_ip_find_addr_target(iface, addr, v6))
  242. return iface;
  243. /* do not stop at the first route, let the lookup compare
  244. * masks to find the best match */
  245. interface_ip_find_route_target(iface, addr, v6, &r_next);
  246. }
  247. }
  248. if (!r_next)
  249. return NULL;
  250. iface = r_next->iface;
  251. if (defaultroute_target)
  252. return iface;
  253. route = calloc(1, sizeof(*route));
  254. if (!route)
  255. return NULL;
  256. route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
  257. route->mask = v6 ? 128 : 32;
  258. memcpy(&route->addr, addr, addrsize);
  259. memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop));
  260. route->mtu = r_next->mtu;
  261. route->metric = r_next->metric;
  262. route->table = r_next->table;
  263. route->iface = iface;
  264. vlist_add(&iface->host_routes, &route->node, route);
  265. return iface;
  266. }
  267. static void
  268. interface_set_route_info(struct interface *iface, struct device_route *route)
  269. {
  270. bool v6 = ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6);
  271. if (!iface)
  272. return;
  273. if (!(route->flags & DEVROUTE_METRIC))
  274. route->metric = iface->metric;
  275. if (!(route->flags & DEVROUTE_TABLE)) {
  276. route->table = (v6) ? iface->ip6table : iface->ip4table;
  277. if (route->table)
  278. route->flags |= DEVROUTE_SRCTABLE;
  279. }
  280. }
  281. void
  282. interface_ip_add_neighbor(struct interface *iface, struct blob_attr *attr, bool v6)
  283. {
  284. struct interface_ip_settings *ip;
  285. struct blob_attr *tb[__NEIGHBOR_MAX], *cur;
  286. struct device_neighbor *neighbor;
  287. int af = v6 ? AF_INET6: AF_INET;
  288. struct ether_addr *ea;
  289. blobmsg_parse(neighbor_attr, __NEIGHBOR_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
  290. if (!iface) {
  291. if ((cur = tb[NEIGHBOR_INTERFACE]) == NULL)
  292. return;
  293. iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
  294. if (!iface)
  295. return;
  296. ip = &iface->config_ip;
  297. } else
  298. ip = &iface->proto_ip;
  299. neighbor = calloc(1,sizeof(*neighbor));
  300. if (!neighbor)
  301. return;
  302. neighbor->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
  303. if ((cur = tb[NEIGHBOR_ADDRESS]) != NULL){
  304. if (!inet_pton(af, blobmsg_data(cur), &neighbor->addr))
  305. goto error;
  306. } else
  307. goto error;
  308. if ((cur = tb[NEIGHBOR_MAC]) != NULL) {
  309. neighbor->flags |= DEVNEIGH_MAC;
  310. ea = ether_aton(blobmsg_data(cur));
  311. if (!ea)
  312. goto error;
  313. memcpy(neighbor->macaddr, ea, 6);
  314. }
  315. if ((cur = tb[NEIGHBOR_PROXY]) != NULL)
  316. neighbor->proxy = blobmsg_get_bool(cur);
  317. if ((cur = tb[NEIGHBOR_ROUTER]) != NULL)
  318. neighbor->router = blobmsg_get_bool(cur);
  319. vlist_add(&ip->neighbor, &neighbor->node, neighbor);
  320. return;
  321. error:
  322. free(neighbor);
  323. }
  324. void
  325. interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
  326. {
  327. struct interface_ip_settings *ip;
  328. struct blob_attr *tb[__ROUTE_MAX], *cur;
  329. struct device_route *route;
  330. int af = v6 ? AF_INET6 : AF_INET;
  331. bool no_device = false;
  332. blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
  333. if ((cur = tb[ROUTE_DISABLED]) != NULL && blobmsg_get_bool(cur))
  334. return;
  335. if (!iface) {
  336. if ((cur = tb[ROUTE_INTERFACE]) == NULL) {
  337. iface = vlist_find(&interfaces, "loopback", iface, node);
  338. no_device = true;
  339. } else {
  340. iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
  341. }
  342. if (!iface)
  343. return;
  344. ip = &iface->config_ip;
  345. } else {
  346. ip = &iface->proto_ip;
  347. }
  348. route = calloc(1, sizeof(*route));
  349. if (!route)
  350. return;
  351. route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
  352. route->mask = v6 ? 128 : 32;
  353. if ((cur = tb[ROUTE_MASK]) != NULL) {
  354. route->mask = parse_netmask_string(blobmsg_data(cur), v6);
  355. if (route->mask > (v6 ? 128 : 32))
  356. goto error;
  357. }
  358. if ((cur = tb[ROUTE_TARGET]) != NULL) {
  359. if (!parse_ip_and_netmask(af, blobmsg_data(cur), &route->addr, &route->mask)) {
  360. D(INTERFACE, "Failed to parse route target: %s", (char *) blobmsg_data(cur));
  361. goto error;
  362. }
  363. /* Mask out IPv4 host bits to avoid "Invalid prefix for given prefix length" */
  364. if (af == AF_INET && route->mask < 32)
  365. clear_if_addr(&route->addr, route->mask);
  366. }
  367. if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
  368. if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
  369. D(INTERFACE, "Failed to parse route gateway: %s", (char *) blobmsg_data(cur));
  370. goto error;
  371. }
  372. }
  373. if ((cur = tb[ROUTE_METRIC]) != NULL) {
  374. route->metric = blobmsg_get_u32(cur);
  375. route->flags |= DEVROUTE_METRIC;
  376. }
  377. if ((cur = tb[ROUTE_MTU]) != NULL) {
  378. route->mtu = blobmsg_get_u32(cur);
  379. route->flags |= DEVROUTE_MTU;
  380. }
  381. /* Use source-based routing */
  382. if ((cur = tb[ROUTE_SOURCE]) != NULL) {
  383. char *saveptr, *source = alloca(blobmsg_data_len(cur));
  384. memcpy(source, blobmsg_data(cur), blobmsg_data_len(cur));
  385. const char *addr = strtok_r(source, "/", &saveptr);
  386. const char *mask = strtok_r(NULL, "/", &saveptr);
  387. if (!addr || inet_pton(af, addr, &route->source) < 1) {
  388. D(INTERFACE, "Failed to parse route source: %s", addr ? addr : "NULL");
  389. goto error;
  390. }
  391. route->sourcemask = (mask) ? atoi(mask) : ((af == AF_INET6) ? 128 : 32);
  392. }
  393. if ((cur = tb[ROUTE_ONLINK]) != NULL && blobmsg_get_bool(cur))
  394. route->flags |= DEVROUTE_ONLINK;
  395. if ((cur = tb[ROUTE_TABLE]) != NULL) {
  396. if (!system_resolve_rt_table(blobmsg_data(cur), &route->table)) {
  397. D(INTERFACE, "Failed to resolve routing table: %s", (char *) blobmsg_data(cur));
  398. goto error;
  399. }
  400. /* only set the table flag if not using the main (default) table */
  401. if (system_is_default_rt_table(route->table))
  402. route->table = 0;
  403. if (route->table)
  404. route->flags |= DEVROUTE_TABLE;
  405. }
  406. if ((cur = tb[ROUTE_VALID]) != NULL) {
  407. int64_t valid = blobmsg_get_u32(cur);
  408. int64_t valid_until = valid + (int64_t)system_get_rtime();
  409. if (valid_until <= LONG_MAX && valid != 0xffffffffLL) /* Catch overflow */
  410. route->valid_until = valid_until;
  411. }
  412. if ((cur = tb[ROUTE_TYPE]) != NULL) {
  413. if (!system_resolve_rt_type(blobmsg_data(cur), &route->type)) {
  414. D(INTERFACE, "Failed to resolve routing type: %s", (char *) blobmsg_data(cur));
  415. goto error;
  416. }
  417. route->flags |= DEVROUTE_TYPE;
  418. }
  419. if ((cur = tb[ROUTE_PROTO]) != NULL) {
  420. if (!system_resolve_rt_proto(blobmsg_data(cur), &route->proto)) {
  421. D(INTERFACE, "Failed to resolve proto type: %s", (char *) blobmsg_data(cur));
  422. goto error;
  423. }
  424. route->flags |= DEVROUTE_PROTO;
  425. }
  426. if (no_device)
  427. route->flags |= DEVROUTE_NODEV;
  428. else
  429. interface_set_route_info(iface, route);
  430. vlist_add(&ip->route, &route->node, route);
  431. return;
  432. error:
  433. free(route);
  434. }
  435. static int
  436. addr_cmp(const void *k1, const void *k2, void *ptr)
  437. {
  438. const struct device_addr *a1 = k1;
  439. const struct device_addr *a2 = k2;
  440. const int cmp_offset = offsetof(struct device_addr, flags);
  441. const int cmp_size = sizeof(struct device_addr) - cmp_offset;
  442. if (a1->index != a2->index)
  443. return a1->index - a2->index;
  444. return memcmp(k1+cmp_offset, k2+cmp_offset, cmp_size);
  445. }
  446. static int
  447. neighbor_cmp(const void *k1, const void *k2, void *ptr)
  448. {
  449. const struct device_neighbor *n1 = k1, *n2 = k2;
  450. return memcmp(&n1->addr, &n2->addr, sizeof(n2->addr));
  451. }
  452. static int
  453. route_cmp(const void *k1, const void *k2, void *ptr)
  454. {
  455. const struct device_route *r1 = k1, *r2 = k2;
  456. if (r1->mask != r2->mask)
  457. return r2->mask - r1->mask;
  458. if (r1->metric != r2->metric)
  459. return r1->metric - r2->metric;
  460. if (r1->flags != r2->flags)
  461. return r2->flags - r1->flags;
  462. if (r1->sourcemask != r2->sourcemask)
  463. return r1->sourcemask - r2->sourcemask;
  464. if (r1->table != r2->table)
  465. return r1->table - r2->table;
  466. int maskcmp = memcmp(&r1->source, &r2->source, sizeof(r1->source));
  467. if (maskcmp)
  468. return maskcmp;
  469. return memcmp(&r1->addr, &r2->addr, sizeof(r1->addr));
  470. }
  471. static int
  472. prefix_cmp(const void *k1, const void *k2, void *ptr)
  473. {
  474. return memcmp(k1, k2, offsetof(struct device_prefix, pclass) -
  475. offsetof(struct device_prefix, addr));
  476. }
  477. static void
  478. interface_handle_subnet_route(struct interface *iface, struct device_addr *addr, bool add)
  479. {
  480. struct device *dev = iface->l3_dev.dev;
  481. struct device_route *r = &addr->subnet;
  482. if (addr->flags & DEVADDR_OFFLINK)
  483. return;
  484. if (!add) {
  485. if (!addr->subnet.iface)
  486. return;
  487. system_del_route(dev, r);
  488. memset(r, 0, sizeof(*r));
  489. return;
  490. }
  491. r->iface = iface;
  492. r->flags = addr->flags;
  493. r->mask = addr->mask;
  494. memcpy(&r->addr, &addr->addr, sizeof(r->addr));
  495. clear_if_addr(&r->addr, r->mask);
  496. if (!system_resolve_rt_proto("kernel", &r->proto))
  497. return;
  498. r->flags |= DEVROUTE_PROTO;
  499. system_del_route(dev, r);
  500. r->flags &= ~DEVROUTE_PROTO;
  501. interface_set_route_info(iface, r);
  502. system_add_route(dev, r);
  503. }
  504. static void
  505. interface_add_addr_rules(struct device_addr *addr, bool enabled)
  506. {
  507. bool v6 = (addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6;
  508. set_ip_source_policy(enabled, v6, IPRULE_PRIORITY_ADDR, &addr->addr,
  509. (v6) ? 128 : 32, addr->policy_table, NULL, NULL,
  510. true);
  511. set_ip_source_policy(enabled, v6, IPRULE_PRIORITY_ADDR_MASK,
  512. &addr->addr, addr->mask, addr->policy_table, NULL,
  513. NULL, false);
  514. }
  515. static void
  516. interface_update_proto_addr(struct vlist_tree *tree,
  517. struct vlist_node *node_new,
  518. struct vlist_node *node_old)
  519. {
  520. struct interface_ip_settings *ip;
  521. struct interface *iface;
  522. struct device *dev;
  523. struct device_addr *a_new = NULL, *a_old = NULL;
  524. bool replace = false;
  525. bool keep = false;
  526. bool v6 = false;
  527. ip = container_of(tree, struct interface_ip_settings, addr);
  528. iface = ip->iface;
  529. dev = iface->l3_dev.dev;
  530. if (!node_new || !node_old)
  531. iface->updated |= IUF_ADDRESS;
  532. if (node_new) {
  533. a_new = container_of(node_new, struct device_addr, node);
  534. if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
  535. !a_new->broadcast) {
  536. /* /31 and /32 addressing need 255.255.255.255
  537. * as broadcast address. */
  538. if (a_new->mask >= 31) {
  539. a_new->broadcast = (uint32_t) ~0;
  540. } else {
  541. uint32_t mask = ~0;
  542. uint32_t *a = (uint32_t *) &a_new->addr;
  543. mask >>= a_new->mask;
  544. a_new->broadcast = *a | htonl(mask);
  545. }
  546. }
  547. }
  548. if (node_old)
  549. a_old = container_of(node_old, struct device_addr, node);
  550. if (a_new && a_old) {
  551. keep = true;
  552. if (a_old->flags != a_new->flags || a_old->failed)
  553. keep = false;
  554. if (a_old->valid_until != a_new->valid_until ||
  555. a_old->preferred_until != a_new->preferred_until)
  556. replace = true;
  557. if (((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4) &&
  558. (a_new->broadcast != a_old->broadcast ||
  559. a_new->point_to_point != a_old->point_to_point))
  560. keep = false;
  561. }
  562. if (node_old) {
  563. if (a_old->enabled && !keep) {
  564. /*
  565. * This is needed for source routing to work correctly. If a device
  566. * has two connections to a network using the same subnet, adding
  567. * only the network-rule will cause packets to be routed through the
  568. * first matching network (source IP matches both masks)
  569. */
  570. if (a_old->policy_table)
  571. interface_add_addr_rules(a_old, false);
  572. if (!(a_old->flags & DEVADDR_EXTERNAL)) {
  573. interface_handle_subnet_route(iface, a_old, false);
  574. system_del_address(dev, a_old);
  575. if ((a_old->flags & DEVADDR_OFFLINK) && (a_old->mask < (v6 ? 128 : 32))) {
  576. struct device_route route;
  577. memset(&route, 0, sizeof(route));
  578. route.flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
  579. route.metric = INT32_MAX;
  580. route.mask = a_old->mask;
  581. route.addr = a_old->addr;
  582. clear_if_addr(&route.addr, route.mask);
  583. /* Delete null-route */
  584. system_del_route(NULL, &route);
  585. }
  586. }
  587. }
  588. free(a_old->pclass);
  589. free(a_old);
  590. }
  591. if (node_new) {
  592. a_new->enabled = true;
  593. if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET6)
  594. v6 = true;
  595. a_new->policy_table = (v6) ? iface->ip6table : iface->ip4table;
  596. if (!keep || replace) {
  597. if (!(a_new->flags & DEVADDR_EXTERNAL)) {
  598. if (system_add_address(dev, a_new))
  599. a_new->failed = true;
  600. if (iface->metric || a_new->policy_table)
  601. interface_handle_subnet_route(iface, a_new, true);
  602. }
  603. if (!keep) {
  604. if (!(a_new->flags & DEVADDR_EXTERNAL) &&
  605. (a_new->flags & DEVADDR_OFFLINK) &&
  606. (a_new->mask < (v6 ? 128 : 32))) {
  607. struct device_route route;
  608. memset(&route, 0, sizeof(route));
  609. route.flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
  610. route.metric = INT32_MAX;
  611. route.mask = a_new->mask;
  612. route.addr = a_new->addr;
  613. clear_if_addr(&route.addr, route.mask);
  614. /*
  615. * In case off link is specifed as address property
  616. * add null-route to avoid routing loops
  617. */
  618. system_add_route(NULL, &route);
  619. }
  620. if (a_new->policy_table)
  621. interface_add_addr_rules(a_new, true);
  622. }
  623. }
  624. }
  625. }
  626. static bool
  627. enable_route(struct interface_ip_settings *ip, struct device_route *route)
  628. {
  629. if (ip->no_defaultroute && !route->mask)
  630. return false;
  631. return ip->enabled;
  632. }
  633. static void
  634. interface_update_proto_neighbor(struct vlist_tree *tree,
  635. struct vlist_node * node_new,
  636. struct vlist_node *node_old)
  637. {
  638. struct device *dev;
  639. struct device_neighbor *neighbor_old, *neighbor_new;
  640. struct interface_ip_settings *ip;
  641. bool keep = false;
  642. ip = container_of(tree, struct interface_ip_settings, neighbor);
  643. dev = ip->iface->l3_dev.dev;
  644. neighbor_old = container_of(node_old, struct device_neighbor, node);
  645. neighbor_new = container_of(node_new, struct device_neighbor, node);
  646. if (node_old && node_new) {
  647. keep = (!memcmp(neighbor_old->macaddr, neighbor_new->macaddr, sizeof(neighbor_old->macaddr)) &&
  648. (neighbor_old->proxy == neighbor_new->proxy) &&
  649. (neighbor_old->router == neighbor_new->router));
  650. }
  651. if (node_old) {
  652. if (!keep && neighbor_old->enabled)
  653. system_del_neighbor(dev, neighbor_old);
  654. free(neighbor_old);
  655. }
  656. if (node_new) {
  657. if (!keep && ip->enabled)
  658. if (system_add_neighbor(dev, neighbor_new))
  659. neighbor_new->failed = true;
  660. neighbor_new->enabled = ip->enabled;
  661. }
  662. }
  663. static void
  664. __interface_update_route(struct interface_ip_settings *ip,
  665. struct vlist_node *node_new,
  666. struct vlist_node *node_old)
  667. {
  668. struct interface *iface = ip->iface;
  669. struct device *dev;
  670. struct device_route *route_old, *route_new;
  671. bool keep = false;
  672. dev = iface->l3_dev.dev;
  673. if (!node_new || !node_old)
  674. iface->updated |= IUF_ROUTE;
  675. route_old = container_of(node_old, struct device_route, node);
  676. route_new = container_of(node_new, struct device_route, node);
  677. if (node_old && node_new)
  678. keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop)) &&
  679. (route_old->mtu == route_new->mtu) && (route_old->type == route_new->type) &&
  680. (route_old->proto == route_new->proto) && !route_old->failed;
  681. if (node_old) {
  682. if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
  683. system_del_route(dev, route_old);
  684. free(route_old);
  685. }
  686. if (node_new) {
  687. bool _enabled = enable_route(ip, route_new);
  688. if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
  689. if (system_add_route(dev, route_new))
  690. route_new->failed = true;
  691. route_new->iface = iface;
  692. route_new->enabled = _enabled;
  693. }
  694. }
  695. static void
  696. interface_update_proto_route(struct vlist_tree *tree,
  697. struct vlist_node *node_new,
  698. struct vlist_node *node_old)
  699. {
  700. struct interface_ip_settings *ip;
  701. ip = container_of(tree, struct interface_ip_settings, route);
  702. __interface_update_route(ip, node_new, node_old);
  703. }
  704. static void
  705. interface_update_host_route(struct vlist_tree *tree,
  706. struct vlist_node *node_new,
  707. struct vlist_node *node_old)
  708. {
  709. struct interface *iface;
  710. iface = container_of(tree, struct interface, host_routes);
  711. __interface_update_route(&iface->proto_ip, node_new, node_old);
  712. }
  713. static void
  714. random_ifaceid(struct in6_addr *addr)
  715. {
  716. static bool initialized = false;
  717. struct timeval t;
  718. if (!initialized) {
  719. long int seed = 0;
  720. gettimeofday(&t, NULL);
  721. seed = t.tv_sec ^ t.tv_usec ^ getpid();
  722. srand48(seed);
  723. initialized = true;
  724. }
  725. addr->s6_addr32[2] = (uint32_t)mrand48();
  726. addr->s6_addr32[3] = (uint32_t)mrand48();
  727. }
  728. static bool
  729. eui64_ifaceid(struct interface *iface, struct in6_addr *addr)
  730. {
  731. struct device_settings st;
  732. device_merge_settings(iface->l3_dev.dev, &st);
  733. if (!(st.flags & DEV_OPT_MACADDR))
  734. return false;
  735. /* get mac address */
  736. uint8_t *ifaceid = addr->s6_addr + 8;
  737. memcpy(ifaceid, st.macaddr, 3);
  738. memcpy(ifaceid + 5, st.macaddr + 3, 3);
  739. ifaceid[3] = 0xff;
  740. ifaceid[4] = 0xfe;
  741. ifaceid[0] ^= 0x02;
  742. return true;
  743. }
  744. static bool
  745. generate_ifaceid(struct interface *iface, struct in6_addr *addr)
  746. {
  747. bool ret = true;
  748. /* generate new iface id */
  749. switch (iface->assignment_iface_id_selection) {
  750. case IFID_FIXED:
  751. /* fixed */
  752. /* copy host part from assignment_fixed_iface_id */
  753. memcpy(addr->s6_addr + 8, iface->assignment_fixed_iface_id.s6_addr + 8, 8);
  754. break;
  755. case IFID_RANDOM:
  756. /* randomize last 64 bits */
  757. random_ifaceid(addr);
  758. break;
  759. case IFID_EUI64:
  760. /* eui64 */
  761. ret = eui64_ifaceid(iface, addr);
  762. break;
  763. default:
  764. ret = false;
  765. break;
  766. }
  767. return ret;
  768. }
  769. static void
  770. interface_set_prefix_address(struct device_prefix_assignment *assignment,
  771. const struct device_prefix *prefix, struct interface *iface, bool add)
  772. {
  773. const struct interface *uplink = prefix->iface;
  774. if (!iface->l3_dev.dev)
  775. return;
  776. struct device *l3_downlink = iface->l3_dev.dev;
  777. struct device_addr addr;
  778. struct device_route route;
  779. memset(&addr, 0, sizeof(addr));
  780. memset(&route, 0, sizeof(route));
  781. addr.addr.in6 = assignment->addr;
  782. addr.mask = assignment->length;
  783. addr.flags = DEVADDR_INET6;
  784. addr.preferred_until = prefix->preferred_until;
  785. addr.valid_until = prefix->valid_until;
  786. route.flags = DEVADDR_INET6;
  787. route.mask = addr.mask < 64 ? 64 : addr.mask;
  788. route.addr = addr.addr;
  789. if (!add && assignment->enabled) {
  790. time_t now = system_get_rtime();
  791. if (addr.valid_until && addr.valid_until - 1 <= now) {
  792. addr.valid_until = 0;
  793. addr.preferred_until = 0;
  794. } else {
  795. /* Address is still valid; pass its ownership to kernel (see L-14 RFC 7084). */
  796. addr.preferred_until = now;
  797. if (!addr.valid_until || addr.valid_until > now + 7200)
  798. addr.valid_until = now + 7200;
  799. }
  800. if (iface->ip6table)
  801. set_ip_source_policy(false, true, IPRULE_PRIORITY_ADDR_MASK, &addr.addr,
  802. addr.mask < 64 ? 64 : addr.mask, iface->ip6table, NULL, NULL, false);
  803. if (prefix->iface) {
  804. if (prefix->iface->ip6table)
  805. set_ip_source_policy(false, true, IPRULE_PRIORITY_NW, &addr.addr,
  806. addr.mask, prefix->iface->ip6table, iface, NULL, true);
  807. set_ip_source_policy(false, true, IPRULE_PRIORITY_REJECT, &addr.addr,
  808. addr.mask, 0, iface, "unreachable", true);
  809. }
  810. clear_if_addr(&route.addr, route.mask);
  811. interface_set_route_info(iface, &route);
  812. system_del_route(l3_downlink, &route);
  813. if (addr.valid_until)
  814. system_add_address(l3_downlink, &addr);
  815. else
  816. system_del_address(l3_downlink, &addr);
  817. assignment->addr = in6addr_any;
  818. assignment->enabled = false;
  819. } else if (add && (iface->state == IFS_UP || iface->state == IFS_SETUP)) {
  820. if (IN6_IS_ADDR_UNSPECIFIED(&addr.addr.in6)) {
  821. addr.addr.in6 = prefix->addr;
  822. addr.addr.in6.s6_addr32[1] |= htonl(assignment->assigned);
  823. if (!generate_ifaceid(iface, &addr.addr.in6))
  824. return;
  825. assignment->addr = addr.addr.in6;
  826. route.addr = addr.addr;
  827. }
  828. addr.flags |= DEVADDR_OFFLINK;
  829. if (system_add_address(l3_downlink, &addr))
  830. return;
  831. if (!assignment->enabled) {
  832. if (iface->ip6table)
  833. set_ip_source_policy(true, true, IPRULE_PRIORITY_ADDR_MASK, &addr.addr,
  834. addr.mask < 64 ? 64 : addr.mask, iface->ip6table, NULL, NULL, false);
  835. if (prefix->iface) {
  836. set_ip_source_policy(true, true, IPRULE_PRIORITY_REJECT, &addr.addr,
  837. addr.mask, 0, iface, "unreachable", true);
  838. if (prefix->iface->ip6table)
  839. set_ip_source_policy(true, true, IPRULE_PRIORITY_NW, &addr.addr,
  840. addr.mask, prefix->iface->ip6table, iface, NULL, true);
  841. }
  842. }
  843. clear_if_addr(&route.addr, route.mask);
  844. interface_set_route_info(iface, &route);
  845. system_add_route(l3_downlink, &route);
  846. if (uplink && uplink->l3_dev.dev && !(l3_downlink->settings.flags & DEV_OPT_MTU6)) {
  847. int mtu = system_update_ipv6_mtu(uplink->l3_dev.dev, 0);
  848. int mtu_old = system_update_ipv6_mtu(l3_downlink, 0);
  849. if (mtu > 0 && mtu_old != mtu) {
  850. if (system_update_ipv6_mtu(l3_downlink, mtu) < 0 && mtu < mtu_old)
  851. netifd_log_message(L_WARNING, "Failed to set IPv6 mtu to %d "
  852. "on interface '%s'\n", mtu, iface->name);
  853. }
  854. }
  855. assignment->enabled = true;
  856. }
  857. }
  858. static bool interface_prefix_assign(struct list_head *list,
  859. struct device_prefix_assignment *assign)
  860. {
  861. int32_t current = 0, asize = (1 << (64 - assign->length)) - 1;
  862. struct device_prefix_assignment *c;
  863. list_for_each_entry(c, list, head) {
  864. if (assign->assigned != -1) {
  865. if (assign->assigned >= current && assign->assigned + asize < c->assigned) {
  866. list_add_tail(&assign->head, &c->head);
  867. return true;
  868. }
  869. } else if (assign->assigned == -1) {
  870. current = (current + asize) & (~asize);
  871. if (current + asize < c->assigned) {
  872. assign->assigned = current;
  873. list_add_tail(&assign->head, &c->head);
  874. return true;
  875. }
  876. }
  877. current = (c->assigned + (1 << (64 - c->length)));
  878. }
  879. return false;
  880. }
  881. /*
  882. * Sorting of assignment entries:
  883. * Primary on assignment length: smallest assignment first
  884. * Secondary on assignment weight: highest weight first
  885. * Finally alphabetical order of interface names
  886. */
  887. static int prefix_assignment_cmp(const void *k1, const void *k2, void *ptr)
  888. {
  889. const struct device_prefix_assignment *a1 = k1, *a2 = k2;
  890. if (a1->length != a2->length)
  891. return a1->length - a2->length;
  892. if (a1->weight != a2->weight)
  893. return a2->weight - a1->weight;
  894. return strcmp(a1->name, a2->name);
  895. }
  896. static void interface_update_prefix_assignments(struct device_prefix *prefix, bool setup)
  897. {
  898. struct device_prefix_assignment *c;
  899. struct interface *iface;
  900. /* Delete all assignments */
  901. while (!list_empty(&prefix->assignments)) {
  902. c = list_first_entry(&prefix->assignments,
  903. struct device_prefix_assignment, head);
  904. if ((iface = vlist_find(&interfaces, c->name, iface, node)))
  905. interface_set_prefix_address(c, prefix, iface, false);
  906. list_del(&c->head);
  907. free(c);
  908. }
  909. if (!setup)
  910. return;
  911. /* End-of-assignment sentinel */
  912. c = malloc(sizeof(*c) + 1);
  913. if (!c)
  914. return;
  915. c->assigned = 1 << (64 - prefix->length);
  916. c->length = 64;
  917. c->name[0] = 0;
  918. c->addr = in6addr_any;
  919. list_add(&c->head, &prefix->assignments);
  920. /* Excluded prefix */
  921. if (prefix->excl_length > 0) {
  922. const char name[] = "!excluded";
  923. c = malloc(sizeof(*c) + sizeof(name));
  924. if (c) {
  925. c->assigned = ntohl(prefix->excl_addr.s6_addr32[1]) &
  926. ((1 << (64 - prefix->length)) - 1);
  927. c->length = prefix->excl_length;
  928. c->addr = in6addr_any;
  929. memcpy(c->name, name, sizeof(name));
  930. list_add(&c->head, &prefix->assignments);
  931. }
  932. }
  933. bool assigned_any = false;
  934. struct {
  935. struct avl_node node;
  936. } *entry, *n_entry;
  937. struct avl_tree assign_later;
  938. avl_init(&assign_later, prefix_assignment_cmp, false, NULL);
  939. vlist_for_each_element(&interfaces, iface, node) {
  940. if (iface->assignment_length < 48 ||
  941. iface->assignment_length > 64)
  942. continue;
  943. /* Test whether there is a matching class */
  944. if (!list_empty(&iface->assignment_classes)) {
  945. bool found = false;
  946. struct interface_assignment_class *c;
  947. list_for_each_entry(c, &iface->assignment_classes, head) {
  948. if (!strcmp(c->name, prefix->pclass)) {
  949. found = true;
  950. break;
  951. }
  952. }
  953. if (!found)
  954. continue;
  955. }
  956. size_t namelen = strlen(iface->name) + 1;
  957. c = malloc(sizeof(*c) + namelen);
  958. if (!c)
  959. continue;
  960. c->length = iface->assignment_length;
  961. c->assigned = iface->assignment_hint;
  962. c->weight = iface->assignment_weight;
  963. c->addr = in6addr_any;
  964. c->enabled = false;
  965. memcpy(c->name, iface->name, namelen);
  966. /* First process all custom assignments, put all others in later-list */
  967. if (c->assigned == -1 || !interface_prefix_assign(&prefix->assignments, c)) {
  968. if (c->assigned != -1) {
  969. c->assigned = -1;
  970. netifd_log_message(L_WARNING, "Failed to assign requested subprefix "
  971. "of size %hhu for %s, trying other\n", c->length, c->name);
  972. }
  973. entry = calloc(1, sizeof(*entry));
  974. if (!entry) {
  975. free(c);
  976. continue;
  977. }
  978. entry->node.key = c;
  979. avl_insert(&assign_later, &entry->node);
  980. }
  981. if (c->assigned != -1)
  982. assigned_any = true;
  983. }
  984. /* Then try to assign all other + failed custom assignments */
  985. avl_for_each_element_safe(&assign_later, entry, node, n_entry) {
  986. bool assigned = false;
  987. c = (struct device_prefix_assignment *)entry->node.key;
  988. avl_delete(&assign_later, &entry->node);
  989. do {
  990. assigned = interface_prefix_assign(&prefix->assignments, c);
  991. } while (!assigned && ++c->length <= 64);
  992. if (!assigned) {
  993. netifd_log_message(L_WARNING, "Failed to assign subprefix "
  994. "of size %hhu for %s\n", c->length, c->name);
  995. free(c);
  996. } else
  997. assigned_any = true;
  998. free(entry);
  999. }
  1000. list_for_each_entry(c, &prefix->assignments, head)
  1001. if ((iface = vlist_find(&interfaces, c->name, iface, node)))
  1002. interface_set_prefix_address(c, prefix, iface, true);
  1003. if (!assigned_any)
  1004. netifd_log_message(L_WARNING, "You have delegated IPv6-prefixes but haven't assigned them "
  1005. "to any interface. Did you forget to set option ip6assign on your lan-interfaces?");
  1006. }
  1007. void interface_refresh_assignments(bool hint)
  1008. {
  1009. static bool refresh = false;
  1010. if (!hint && refresh) {
  1011. struct device_prefix *p;
  1012. time_t now = system_get_rtime();
  1013. list_for_each_entry(p, &prefixes, head) {
  1014. bool valid = !(p->valid_until && p->valid_until - 1 <= now);
  1015. interface_update_prefix_assignments(p, valid);
  1016. }
  1017. }
  1018. refresh = hint;
  1019. }
  1020. void interface_update_prefix_delegation(struct interface_ip_settings *ip)
  1021. {
  1022. struct device_prefix *prefix;
  1023. time_t now = system_get_rtime();
  1024. vlist_for_each_element(&ip->prefix, prefix, node) {
  1025. bool valid = !(prefix->valid_until && prefix->valid_until - 1 <= now);
  1026. interface_update_prefix_assignments(prefix, !ip->no_delegation && valid);
  1027. if (ip->no_delegation) {
  1028. if (prefix->head.next)
  1029. list_del(&prefix->head);
  1030. } else
  1031. list_add(&prefix->head, &prefixes);
  1032. }
  1033. }
  1034. static void
  1035. interface_update_prefix(struct vlist_tree *tree,
  1036. struct vlist_node *node_new,
  1037. struct vlist_node *node_old)
  1038. {
  1039. struct device_prefix *prefix_old, *prefix_new;
  1040. prefix_old = container_of(node_old, struct device_prefix, node);
  1041. prefix_new = container_of(node_new, struct device_prefix, node);
  1042. struct interface_ip_settings *ip = container_of(tree, struct interface_ip_settings, prefix);
  1043. if (tree && (!node_new || !node_old))
  1044. ip->iface->updated |= IUF_PREFIX;
  1045. struct device_route route;
  1046. memset(&route, 0, sizeof(route));
  1047. route.flags = DEVADDR_INET6;
  1048. route.metric = INT32_MAX;
  1049. route.mask = (node_new) ? prefix_new->length : prefix_old->length;
  1050. route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr;
  1051. struct device_prefix_assignment *c;
  1052. struct interface *iface;
  1053. bool new_valid = node_new && !(prefix_new->valid_until && prefix_new->valid_until - 1 <= system_get_rtime());
  1054. if (node_old && node_new) {
  1055. /* Move assignments and refresh addresses to update valid times */
  1056. list_splice(&prefix_old->assignments, &prefix_new->assignments);
  1057. list_for_each_entry(c, &prefix_new->assignments, head)
  1058. if ((iface = vlist_find(&interfaces, c->name, iface, node)))
  1059. interface_set_prefix_address(c, prefix_new, iface, new_valid);
  1060. if (prefix_new->preferred_until != prefix_old->preferred_until ||
  1061. prefix_new->valid_until != prefix_old->valid_until)
  1062. ip->iface->updated |= IUF_PREFIX;
  1063. } else if (node_new) {
  1064. /* Set null-route to avoid routing loops */
  1065. system_add_route(NULL, &route);
  1066. if (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation)
  1067. interface_update_prefix_assignments(prefix_new, new_valid);
  1068. } else if (node_old) {
  1069. /* Remove null-route */
  1070. interface_update_prefix_assignments(prefix_old, false);
  1071. system_del_route(NULL, &route);
  1072. }
  1073. if (node_old) {
  1074. if (prefix_old->head.next)
  1075. list_del(&prefix_old->head);
  1076. free(prefix_old);
  1077. }
  1078. if (node_new && (!prefix_new->iface || !prefix_new->iface->proto_ip.no_delegation))
  1079. list_add(&prefix_new->head, &prefixes);
  1080. }
  1081. struct device_prefix*
  1082. interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr,
  1083. uint8_t length, time_t valid_until, time_t preferred_until,
  1084. struct in6_addr *excl_addr, uint8_t excl_length, const char *pclass)
  1085. {
  1086. union if_addr a = { .in6 = *addr };
  1087. if (!pclass)
  1088. pclass = (iface) ? iface->name : "local";
  1089. struct device_prefix *prefix = calloc(1, sizeof(*prefix) + strlen(pclass) + 1);
  1090. if (!prefix)
  1091. return NULL;
  1092. clear_if_addr(&a, length);
  1093. prefix->length = length;
  1094. prefix->addr = a.in6;
  1095. prefix->preferred_until = preferred_until;
  1096. prefix->valid_until = valid_until;
  1097. prefix->iface = iface;
  1098. INIT_LIST_HEAD(&prefix->assignments);
  1099. if (excl_addr) {
  1100. prefix->excl_addr = *excl_addr;
  1101. prefix->excl_length = excl_length;
  1102. }
  1103. strcpy(prefix->pclass, pclass);
  1104. if (iface)
  1105. vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr);
  1106. else
  1107. interface_update_prefix(NULL, &prefix->node, NULL);
  1108. return prefix;
  1109. }
  1110. void
  1111. interface_ip_set_ula_prefix(const char *prefix)
  1112. {
  1113. char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr;
  1114. if (prefix)
  1115. strncpy(buf, prefix, sizeof(buf) - 1);
  1116. char *prefixaddr = strtok_r(buf, "/", &saveptr);
  1117. struct in6_addr addr;
  1118. if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1) {
  1119. if (ula_prefix) {
  1120. interface_update_prefix(NULL, NULL, &ula_prefix->node);
  1121. ula_prefix = NULL;
  1122. }
  1123. return;
  1124. }
  1125. int length;
  1126. char *prefixlen = strtok_r(NULL, ",", &saveptr);
  1127. if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64)
  1128. return;
  1129. if (!ula_prefix || !IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) ||
  1130. ula_prefix->length != length) {
  1131. if (ula_prefix)
  1132. interface_update_prefix(NULL, NULL, &ula_prefix->node);
  1133. ula_prefix = interface_ip_add_device_prefix(NULL, &addr, length,
  1134. 0, 0, NULL, 0, NULL);
  1135. }
  1136. }
  1137. static void
  1138. interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
  1139. {
  1140. struct dns_server *s;
  1141. s = calloc(1, sizeof(*s));
  1142. if (!s)
  1143. return;
  1144. s->af = AF_INET;
  1145. if (inet_pton(s->af, str, &s->addr.in))
  1146. goto add;
  1147. s->af = AF_INET6;
  1148. if (inet_pton(s->af, str, &s->addr.in))
  1149. goto add;
  1150. free(s);
  1151. return;
  1152. add:
  1153. D(INTERFACE, "Add IPv%c DNS server: %s",
  1154. s->af == AF_INET6 ? '6' : '4', str);
  1155. vlist_simple_add(&ip->dns_servers, &s->node);
  1156. }
  1157. void
  1158. interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
  1159. {
  1160. struct blob_attr *cur;
  1161. size_t rem;
  1162. blobmsg_for_each_attr(cur, list, rem) {
  1163. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  1164. continue;
  1165. if (!blobmsg_check_attr(cur, false))
  1166. continue;
  1167. interface_add_dns_server(ip, blobmsg_data(cur));
  1168. }
  1169. }
  1170. static void
  1171. interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
  1172. {
  1173. struct dns_search_domain *s;
  1174. int len = strlen(str);
  1175. s = calloc(1, sizeof(*s) + len + 1);
  1176. if (!s)
  1177. return;
  1178. D(INTERFACE, "Add DNS search domain: %s", str);
  1179. memcpy(s->name, str, len);
  1180. vlist_simple_add(&ip->dns_search, &s->node);
  1181. }
  1182. void
  1183. interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
  1184. {
  1185. struct blob_attr *cur;
  1186. size_t rem;
  1187. blobmsg_for_each_attr(cur, list, rem) {
  1188. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  1189. continue;
  1190. if (!blobmsg_check_attr(cur, false))
  1191. continue;
  1192. interface_add_dns_search_domain(ip, blobmsg_data(cur));
  1193. }
  1194. }
  1195. static void
  1196. write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip, const char *dev)
  1197. {
  1198. struct dns_server *s;
  1199. struct dns_search_domain *d;
  1200. const char *str;
  1201. char buf[INET6_ADDRSTRLEN];
  1202. vlist_simple_for_each_element(&ip->dns_servers, s, node) {
  1203. str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
  1204. if (!str)
  1205. continue;
  1206. if (s->af == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(&s->addr.in6))
  1207. fprintf(f, "nameserver %s%%%s\n", str, dev);
  1208. else
  1209. fprintf(f, "nameserver %s\n", str);
  1210. }
  1211. vlist_simple_for_each_element(&ip->dns_search, d, node) {
  1212. fprintf(f, "search %s\n", d->name);
  1213. }
  1214. }
  1215. /* Sorting of interface resolver entries : */
  1216. /* Primary on interface dns_metric : lowest metric first */
  1217. /* Secondary on interface metric : lowest metric first */
  1218. /* Finally alphabetical order of interface names */
  1219. static int resolv_conf_iface_cmp(const void *k1, const void *k2, void *ptr)
  1220. {
  1221. const struct interface *iface1 = k1, *iface2 = k2;
  1222. if (iface1->dns_metric != iface2->dns_metric)
  1223. return iface1->dns_metric - iface2->dns_metric;
  1224. if (iface1->metric != iface2->metric)
  1225. return iface1->metric - iface2->metric;
  1226. return strcmp(iface1->name, iface2->name);
  1227. }
  1228. static void
  1229. __interface_write_dns_entries(FILE *f, const char *jail)
  1230. {
  1231. struct interface *iface;
  1232. struct {
  1233. struct avl_node node;
  1234. } *entry, *n_entry;
  1235. struct avl_tree resolv_conf_iface_entries;
  1236. avl_init(&resolv_conf_iface_entries, resolv_conf_iface_cmp, false, NULL);
  1237. vlist_for_each_element(&interfaces, iface, node) {
  1238. if (iface->state != IFS_UP)
  1239. continue;
  1240. if (jail && (!iface->jail || strcmp(jail, iface->jail)))
  1241. continue;
  1242. if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
  1243. vlist_simple_empty(&iface->proto_ip.dns_servers) &&
  1244. vlist_simple_empty(&iface->config_ip.dns_search) &&
  1245. vlist_simple_empty(&iface->config_ip.dns_servers))
  1246. continue;
  1247. entry = calloc(1, sizeof(*entry));
  1248. if (!entry)
  1249. continue;
  1250. entry->node.key = iface;
  1251. avl_insert(&resolv_conf_iface_entries, &entry->node);
  1252. }
  1253. avl_for_each_element(&resolv_conf_iface_entries, entry, node) {
  1254. iface = (struct interface *)entry->node.key;
  1255. struct device *dev = iface->l3_dev.dev;
  1256. fprintf(f, "# Interface %s\n", iface->name);
  1257. write_resolv_conf_entries(f, &iface->config_ip, dev->ifname);
  1258. if (!iface->proto_ip.no_dns)
  1259. write_resolv_conf_entries(f, &iface->proto_ip, dev->ifname);
  1260. }
  1261. avl_remove_all_elements(&resolv_conf_iface_entries, entry, node, n_entry)
  1262. free(entry);
  1263. }
  1264. void
  1265. interface_write_resolv_conf(const char *jail)
  1266. {
  1267. size_t plen = (jail ? strlen(jail) + 1 : 0 ) +
  1268. (strlen(resolv_conf) >= strlen(DEFAULT_RESOLV_CONF) ?
  1269. strlen(resolv_conf) : strlen(DEFAULT_RESOLV_CONF) ) + 1;
  1270. char *path = alloca(plen);
  1271. char *dpath = alloca(plen);
  1272. char *tmppath = alloca(plen + 4);
  1273. FILE *f;
  1274. uint32_t crcold, crcnew;
  1275. if (jail) {
  1276. sprintf(path, "/tmp/resolv.conf-%s.d/resolv.conf.auto", jail);
  1277. strcpy(dpath, path);
  1278. dpath = dirname(dpath);
  1279. mkdir(dpath, 0755);
  1280. } else {
  1281. strcpy(path, resolv_conf);
  1282. }
  1283. sprintf(tmppath, "%s.tmp", path);
  1284. unlink(tmppath);
  1285. f = fopen(tmppath, "w+");
  1286. if (!f) {
  1287. D(INTERFACE, "Failed to open %s for writing", path);
  1288. return;
  1289. }
  1290. __interface_write_dns_entries(f, jail);
  1291. fflush(f);
  1292. rewind(f);
  1293. crcnew = crc32_file(f);
  1294. fclose(f);
  1295. crcold = crcnew + 1;
  1296. f = fopen(path, "r");
  1297. if (f) {
  1298. crcold = crc32_file(f);
  1299. fclose(f);
  1300. }
  1301. if (crcold == crcnew) {
  1302. unlink(tmppath);
  1303. } else if (rename(tmppath, path) < 0) {
  1304. D(INTERFACE, "Failed to replace %s", path);
  1305. unlink(tmppath);
  1306. }
  1307. }
  1308. static void
  1309. interface_ip_set_route_enabled(struct interface_ip_settings *ip,
  1310. struct device_route *route, bool enabled)
  1311. {
  1312. struct device *dev = ip->iface->l3_dev.dev;
  1313. if (route->flags & DEVADDR_EXTERNAL)
  1314. return;
  1315. if (!enable_route(ip, route))
  1316. enabled = false;
  1317. if (route->enabled == enabled)
  1318. return;
  1319. if (enabled) {
  1320. interface_set_route_info(ip->iface, route);
  1321. if (system_add_route(dev, route))
  1322. route->failed = true;
  1323. } else
  1324. system_del_route(dev, route);
  1325. route->enabled = enabled;
  1326. }
  1327. void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
  1328. {
  1329. struct device_addr *addr;
  1330. struct device_route *route;
  1331. struct device_neighbor *neighbor;
  1332. struct device *dev;
  1333. struct interface *iface;
  1334. ip->enabled = enabled;
  1335. iface = ip->iface;
  1336. dev = iface->l3_dev.dev;
  1337. if (!dev)
  1338. return;
  1339. vlist_for_each_element(&ip->addr, addr, node) {
  1340. bool v6 = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6) ? true : false;
  1341. if (addr->flags & DEVADDR_EXTERNAL)
  1342. continue;
  1343. if (addr->enabled == enabled)
  1344. continue;
  1345. if (enabled) {
  1346. system_add_address(dev, addr);
  1347. addr->policy_table = (v6) ? iface->ip6table : iface->ip4table;
  1348. if (iface->metric || addr->policy_table)
  1349. interface_handle_subnet_route(iface, addr, true);
  1350. if ((addr->flags & DEVADDR_OFFLINK) && (addr->mask < (v6 ? 128 : 32))) {
  1351. struct device_route route;
  1352. memset(&route, 0, sizeof(route));
  1353. route.flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
  1354. route.metric = INT32_MAX;
  1355. route.mask = addr->mask;
  1356. route.addr = addr->addr;
  1357. clear_if_addr(&route.addr, route.mask);
  1358. /*
  1359. * In case off link is specifed as address property
  1360. * add null-route to avoid routing loops
  1361. */
  1362. system_add_route(NULL, &route);
  1363. }
  1364. if (addr->policy_table)
  1365. interface_add_addr_rules(addr, true);
  1366. } else {
  1367. interface_handle_subnet_route(iface, addr, false);
  1368. system_del_address(dev, addr);
  1369. if ((addr->flags & DEVADDR_OFFLINK) && (addr->mask < (v6 ? 128 : 32))) {
  1370. struct device_route route;
  1371. memset(&route, 0, sizeof(route));
  1372. route.flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
  1373. route.metric = INT32_MAX;
  1374. route.mask = addr->mask;
  1375. route.addr = addr->addr;
  1376. clear_if_addr(&route.addr, route.mask);
  1377. /* Delete null-route */
  1378. system_del_route(NULL, &route);
  1379. }
  1380. if (addr->policy_table)
  1381. interface_add_addr_rules(addr, false);
  1382. }
  1383. addr->enabled = enabled;
  1384. }
  1385. vlist_for_each_element(&ip->route, route, node)
  1386. interface_ip_set_route_enabled(ip, route, enabled);
  1387. if (ip == &iface->proto_ip)
  1388. vlist_for_each_element(&iface->host_routes, route, node)
  1389. interface_ip_set_route_enabled(ip, route, enabled);
  1390. vlist_for_each_element(&ip->neighbor, neighbor, node) {
  1391. if (neighbor->enabled == enabled)
  1392. continue;
  1393. if (enabled) {
  1394. if(system_add_neighbor(dev, neighbor))
  1395. neighbor->failed = true;
  1396. } else
  1397. system_del_neighbor(dev, neighbor);
  1398. neighbor->enabled = enabled;
  1399. }
  1400. struct device_prefix *c;
  1401. struct device_prefix_assignment *a;
  1402. list_for_each_entry(c, &prefixes, head)
  1403. list_for_each_entry(a, &c->assignments, head)
  1404. if (!strcmp(a->name, ip->iface->name))
  1405. interface_set_prefix_address(a, c, ip->iface, enabled);
  1406. if (ip->iface->policy_rules_set != enabled &&
  1407. ip->iface->l3_dev.dev) {
  1408. if (ip->iface->l3_dev.dev->settings.ipv6) {
  1409. set_ip_lo_policy(enabled, true, ip->iface);
  1410. set_ip_source_policy(enabled, true, IPRULE_PRIORITY_REJECT + ip->iface->l3_dev.dev->ifindex,
  1411. NULL, 0, 0, ip->iface, "failed_policy", true);
  1412. }
  1413. set_ip_lo_policy(enabled, false, ip->iface);
  1414. ip->iface->policy_rules_set = enabled;
  1415. }
  1416. }
  1417. void
  1418. interface_ip_update_start(struct interface_ip_settings *ip)
  1419. {
  1420. if (ip != &ip->iface->config_ip) {
  1421. vlist_simple_update(&ip->dns_servers);
  1422. vlist_simple_update(&ip->dns_search);
  1423. }
  1424. vlist_update(&ip->route);
  1425. vlist_update(&ip->addr);
  1426. vlist_update(&ip->prefix);
  1427. vlist_update(&ip->neighbor);
  1428. }
  1429. void
  1430. interface_ip_update_complete(struct interface_ip_settings *ip)
  1431. {
  1432. vlist_simple_flush(&ip->dns_servers);
  1433. vlist_simple_flush(&ip->dns_search);
  1434. vlist_flush(&ip->route);
  1435. vlist_flush(&ip->addr);
  1436. vlist_flush(&ip->prefix);
  1437. vlist_flush(&ip->neighbor);
  1438. interface_write_resolv_conf(ip->iface->jail);
  1439. }
  1440. void
  1441. interface_ip_flush(struct interface_ip_settings *ip)
  1442. {
  1443. if (ip == &ip->iface->proto_ip)
  1444. vlist_flush_all(&ip->iface->host_routes);
  1445. vlist_simple_flush_all(&ip->dns_servers);
  1446. vlist_simple_flush_all(&ip->dns_search);
  1447. vlist_flush_all(&ip->route);
  1448. vlist_flush_all(&ip->addr);
  1449. vlist_flush_all(&ip->neighbor);
  1450. vlist_flush_all(&ip->prefix);
  1451. }
  1452. static void
  1453. __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
  1454. {
  1455. ip->iface = iface;
  1456. ip->enabled = true;
  1457. vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
  1458. vlist_simple_init(&ip->dns_servers, struct dns_server, node);
  1459. vlist_init(&ip->route, route_cmp, interface_update_proto_route);
  1460. vlist_init(&ip->neighbor, neighbor_cmp, interface_update_proto_neighbor);
  1461. vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
  1462. vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);
  1463. }
  1464. void
  1465. interface_ip_init(struct interface *iface)
  1466. {
  1467. __interface_ip_init(&iface->proto_ip, iface);
  1468. __interface_ip_init(&iface->config_ip, iface);
  1469. vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
  1470. }
  1471. static void
  1472. interface_ip_valid_until_handler(struct uloop_timeout *t)
  1473. {
  1474. time_t now = system_get_rtime();
  1475. struct interface *iface;
  1476. vlist_for_each_element(&interfaces, iface, node) {
  1477. if (iface->state != IFS_UP)
  1478. continue;
  1479. struct device_addr *addr, *addrp;
  1480. struct device_route *route, *routep;
  1481. struct device_prefix *pref, *prefp;
  1482. vlist_for_each_element_safe(&iface->proto_ip.addr, addr, node, addrp)
  1483. if (addr->valid_until && addr->valid_until < now)
  1484. vlist_delete(&iface->proto_ip.addr, &addr->node);
  1485. vlist_for_each_element_safe(&iface->proto_ip.route, route, node, routep)
  1486. if (route->valid_until && route->valid_until < now)
  1487. vlist_delete(&iface->proto_ip.route, &route->node);
  1488. vlist_for_each_element_safe(&iface->proto_ip.prefix, pref, node, prefp)
  1489. if (pref->valid_until && pref->valid_until < now)
  1490. vlist_delete(&iface->proto_ip.prefix, &pref->node);
  1491. }
  1492. uloop_timeout_set(t, 1000);
  1493. }
  1494. static void __init
  1495. interface_ip_init_worker(void)
  1496. {
  1497. valid_until_timeout.cb = interface_ip_valid_until_handler;
  1498. uloop_timeout_set(&valid_until_timeout, 1000);
  1499. }