interface-ip.c 39 KB

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