2
0

interface-ip.c 48 KB

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