fib_frontend.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * IPv4 Forwarding Information Base: FIB frontend.
  7. *
  8. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/bitops.h>
  18. #include <linux/capability.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/string.h>
  23. #include <linux/socket.h>
  24. #include <linux/sockios.h>
  25. #include <linux/errno.h>
  26. #include <linux/in.h>
  27. #include <linux/inet.h>
  28. #include <linux/inetdevice.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/if_addr.h>
  31. #include <linux/if_arp.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/cache.h>
  34. #include <linux/init.h>
  35. #include <linux/list.h>
  36. #include <linux/slab.h>
  37. #include <net/ip.h>
  38. #include <net/protocol.h>
  39. #include <net/route.h>
  40. #include <net/tcp.h>
  41. #include <net/sock.h>
  42. #include <net/arp.h>
  43. #include <net/ip_fib.h>
  44. #include <net/rtnetlink.h>
  45. #include <net/xfrm.h>
  46. #include <net/l3mdev.h>
  47. #include <net/lwtunnel.h>
  48. #include <trace/events/fib.h>
  49. #ifndef CONFIG_IP_MULTIPLE_TABLES
  50. static int __net_init fib4_rules_init(struct net *net)
  51. {
  52. struct fib_table *local_table, *main_table;
  53. main_table = fib_trie_table(RT_TABLE_MAIN, NULL);
  54. if (!main_table)
  55. return -ENOMEM;
  56. local_table = fib_trie_table(RT_TABLE_LOCAL, main_table);
  57. if (!local_table)
  58. goto fail;
  59. hlist_add_head_rcu(&local_table->tb_hlist,
  60. &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX]);
  61. hlist_add_head_rcu(&main_table->tb_hlist,
  62. &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX]);
  63. return 0;
  64. fail:
  65. fib_free_table(main_table);
  66. return -ENOMEM;
  67. }
  68. static bool fib4_has_custom_rules(struct net *net)
  69. {
  70. return false;
  71. }
  72. #else
  73. struct fib_table *fib_new_table(struct net *net, u32 id)
  74. {
  75. struct fib_table *tb, *alias = NULL;
  76. unsigned int h;
  77. if (id == 0)
  78. id = RT_TABLE_MAIN;
  79. tb = fib_get_table(net, id);
  80. if (tb)
  81. return tb;
  82. if (id == RT_TABLE_LOCAL && !net->ipv4.fib_has_custom_rules)
  83. alias = fib_new_table(net, RT_TABLE_MAIN);
  84. tb = fib_trie_table(id, alias);
  85. if (!tb)
  86. return NULL;
  87. switch (id) {
  88. case RT_TABLE_MAIN:
  89. rcu_assign_pointer(net->ipv4.fib_main, tb);
  90. break;
  91. case RT_TABLE_DEFAULT:
  92. rcu_assign_pointer(net->ipv4.fib_default, tb);
  93. break;
  94. default:
  95. break;
  96. }
  97. h = id & (FIB_TABLE_HASHSZ - 1);
  98. hlist_add_head_rcu(&tb->tb_hlist, &net->ipv4.fib_table_hash[h]);
  99. return tb;
  100. }
  101. EXPORT_SYMBOL_GPL(fib_new_table);
  102. /* caller must hold either rtnl or rcu read lock */
  103. struct fib_table *fib_get_table(struct net *net, u32 id)
  104. {
  105. struct fib_table *tb;
  106. struct hlist_head *head;
  107. unsigned int h;
  108. if (id == 0)
  109. id = RT_TABLE_MAIN;
  110. h = id & (FIB_TABLE_HASHSZ - 1);
  111. head = &net->ipv4.fib_table_hash[h];
  112. hlist_for_each_entry_rcu(tb, head, tb_hlist) {
  113. if (tb->tb_id == id)
  114. return tb;
  115. }
  116. return NULL;
  117. }
  118. static bool fib4_has_custom_rules(struct net *net)
  119. {
  120. return net->ipv4.fib_has_custom_rules;
  121. }
  122. #endif /* CONFIG_IP_MULTIPLE_TABLES */
  123. static void fib_replace_table(struct net *net, struct fib_table *old,
  124. struct fib_table *new)
  125. {
  126. #ifdef CONFIG_IP_MULTIPLE_TABLES
  127. switch (new->tb_id) {
  128. case RT_TABLE_MAIN:
  129. rcu_assign_pointer(net->ipv4.fib_main, new);
  130. break;
  131. case RT_TABLE_DEFAULT:
  132. rcu_assign_pointer(net->ipv4.fib_default, new);
  133. break;
  134. default:
  135. break;
  136. }
  137. #endif
  138. /* replace the old table in the hlist */
  139. hlist_replace_rcu(&old->tb_hlist, &new->tb_hlist);
  140. }
  141. int fib_unmerge(struct net *net)
  142. {
  143. struct fib_table *old, *new, *main_table;
  144. /* attempt to fetch local table if it has been allocated */
  145. old = fib_get_table(net, RT_TABLE_LOCAL);
  146. if (!old)
  147. return 0;
  148. new = fib_trie_unmerge(old);
  149. if (!new)
  150. return -ENOMEM;
  151. /* table is already unmerged */
  152. if (new == old)
  153. return 0;
  154. /* replace merged table with clean table */
  155. fib_replace_table(net, old, new);
  156. fib_free_table(old);
  157. /* attempt to fetch main table if it has been allocated */
  158. main_table = fib_get_table(net, RT_TABLE_MAIN);
  159. if (!main_table)
  160. return 0;
  161. /* flush local entries from main table */
  162. fib_table_flush_external(main_table);
  163. return 0;
  164. }
  165. static void fib_flush(struct net *net)
  166. {
  167. int flushed = 0;
  168. unsigned int h;
  169. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  170. struct hlist_head *head = &net->ipv4.fib_table_hash[h];
  171. struct hlist_node *tmp;
  172. struct fib_table *tb;
  173. hlist_for_each_entry_safe(tb, tmp, head, tb_hlist)
  174. flushed += fib_table_flush(net, tb);
  175. }
  176. if (flushed)
  177. rt_cache_flush(net);
  178. }
  179. /*
  180. * Find address type as if only "dev" was present in the system. If
  181. * on_dev is NULL then all interfaces are taken into consideration.
  182. */
  183. static inline unsigned int __inet_dev_addr_type(struct net *net,
  184. const struct net_device *dev,
  185. __be32 addr, u32 tb_id)
  186. {
  187. struct flowi4 fl4 = { .daddr = addr };
  188. struct fib_result res;
  189. unsigned int ret = RTN_BROADCAST;
  190. struct fib_table *table;
  191. if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr))
  192. return RTN_BROADCAST;
  193. if (ipv4_is_multicast(addr))
  194. return RTN_MULTICAST;
  195. rcu_read_lock();
  196. table = fib_get_table(net, tb_id);
  197. if (table) {
  198. ret = RTN_UNICAST;
  199. if (!fib_table_lookup(table, &fl4, &res, FIB_LOOKUP_NOREF)) {
  200. if (!dev || dev == res.fi->fib_dev)
  201. ret = res.type;
  202. }
  203. }
  204. rcu_read_unlock();
  205. return ret;
  206. }
  207. unsigned int inet_addr_type_table(struct net *net, __be32 addr, u32 tb_id)
  208. {
  209. return __inet_dev_addr_type(net, NULL, addr, tb_id);
  210. }
  211. EXPORT_SYMBOL(inet_addr_type_table);
  212. unsigned int inet_addr_type(struct net *net, __be32 addr)
  213. {
  214. return __inet_dev_addr_type(net, NULL, addr, RT_TABLE_LOCAL);
  215. }
  216. EXPORT_SYMBOL(inet_addr_type);
  217. unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev,
  218. __be32 addr)
  219. {
  220. u32 rt_table = l3mdev_fib_table(dev) ? : RT_TABLE_LOCAL;
  221. return __inet_dev_addr_type(net, dev, addr, rt_table);
  222. }
  223. EXPORT_SYMBOL(inet_dev_addr_type);
  224. /* inet_addr_type with dev == NULL but using the table from a dev
  225. * if one is associated
  226. */
  227. unsigned int inet_addr_type_dev_table(struct net *net,
  228. const struct net_device *dev,
  229. __be32 addr)
  230. {
  231. u32 rt_table = l3mdev_fib_table(dev) ? : RT_TABLE_LOCAL;
  232. return __inet_dev_addr_type(net, NULL, addr, rt_table);
  233. }
  234. EXPORT_SYMBOL(inet_addr_type_dev_table);
  235. __be32 fib_compute_spec_dst(struct sk_buff *skb)
  236. {
  237. struct net_device *dev = skb->dev;
  238. struct in_device *in_dev;
  239. struct fib_result res;
  240. struct rtable *rt;
  241. struct net *net;
  242. int scope;
  243. rt = skb_rtable(skb);
  244. if ((rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST | RTCF_LOCAL)) ==
  245. RTCF_LOCAL)
  246. return ip_hdr(skb)->daddr;
  247. in_dev = __in_dev_get_rcu(dev);
  248. BUG_ON(!in_dev);
  249. net = dev_net(dev);
  250. scope = RT_SCOPE_UNIVERSE;
  251. if (!ipv4_is_zeronet(ip_hdr(skb)->saddr)) {
  252. struct flowi4 fl4 = {
  253. .flowi4_iif = LOOPBACK_IFINDEX,
  254. .daddr = ip_hdr(skb)->saddr,
  255. .flowi4_tos = RT_TOS(ip_hdr(skb)->tos),
  256. .flowi4_scope = scope,
  257. .flowi4_mark = IN_DEV_SRC_VMARK(in_dev) ? skb->mark : 0,
  258. };
  259. if (!fib_lookup(net, &fl4, &res, 0))
  260. return FIB_RES_PREFSRC(net, res);
  261. } else {
  262. scope = RT_SCOPE_LINK;
  263. }
  264. return inet_select_addr(dev, ip_hdr(skb)->saddr, scope);
  265. }
  266. /* Given (packet source, input interface) and optional (dst, oif, tos):
  267. * - (main) check, that source is valid i.e. not broadcast or our local
  268. * address.
  269. * - figure out what "logical" interface this packet arrived
  270. * and calculate "specific destination" address.
  271. * - check, that packet arrived from expected physical interface.
  272. * called with rcu_read_lock()
  273. */
  274. static int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
  275. u8 tos, int oif, struct net_device *dev,
  276. int rpf, struct in_device *idev, u32 *itag)
  277. {
  278. int ret, no_addr;
  279. struct fib_result res;
  280. struct flowi4 fl4;
  281. struct net *net = dev_net(dev);
  282. bool dev_match;
  283. fl4.flowi4_oif = 0;
  284. fl4.flowi4_iif = l3mdev_master_ifindex_rcu(dev);
  285. if (!fl4.flowi4_iif)
  286. fl4.flowi4_iif = oif ? : LOOPBACK_IFINDEX;
  287. fl4.daddr = src;
  288. fl4.saddr = dst;
  289. fl4.flowi4_tos = tos;
  290. fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
  291. fl4.flowi4_tun_key.tun_id = 0;
  292. fl4.flowi4_flags = 0;
  293. fl4.flowi4_uid = sock_net_uid(net, NULL);
  294. no_addr = idev->ifa_list == NULL;
  295. fl4.flowi4_mark = IN_DEV_SRC_VMARK(idev) ? skb->mark : 0;
  296. trace_fib_validate_source(dev, &fl4);
  297. if (fib_lookup(net, &fl4, &res, 0))
  298. goto last_resort;
  299. if (res.type != RTN_UNICAST &&
  300. (res.type != RTN_LOCAL || !IN_DEV_ACCEPT_LOCAL(idev)))
  301. goto e_inval;
  302. fib_combine_itag(itag, &res);
  303. dev_match = false;
  304. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  305. for (ret = 0; ret < res.fi->fib_nhs; ret++) {
  306. struct fib_nh *nh = &res.fi->fib_nh[ret];
  307. if (nh->nh_dev == dev) {
  308. dev_match = true;
  309. break;
  310. } else if (l3mdev_master_ifindex_rcu(nh->nh_dev) == dev->ifindex) {
  311. dev_match = true;
  312. break;
  313. }
  314. }
  315. #else
  316. if (FIB_RES_DEV(res) == dev)
  317. dev_match = true;
  318. #endif
  319. if (dev_match) {
  320. ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
  321. return ret;
  322. }
  323. if (no_addr)
  324. goto last_resort;
  325. if (rpf == 1)
  326. goto e_rpf;
  327. fl4.flowi4_oif = dev->ifindex;
  328. ret = 0;
  329. if (fib_lookup(net, &fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE) == 0) {
  330. if (res.type == RTN_UNICAST)
  331. ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
  332. }
  333. return ret;
  334. last_resort:
  335. if (rpf)
  336. goto e_rpf;
  337. *itag = 0;
  338. return 0;
  339. e_inval:
  340. return -EINVAL;
  341. e_rpf:
  342. return -EXDEV;
  343. }
  344. /* Ignore rp_filter for packets protected by IPsec. */
  345. int fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
  346. u8 tos, int oif, struct net_device *dev,
  347. struct in_device *idev, u32 *itag)
  348. {
  349. int r = secpath_exists(skb) ? 0 : IN_DEV_RPFILTER(idev);
  350. struct net *net = dev_net(dev);
  351. if (!r && !fib_num_tclassid_users(net) &&
  352. (dev->ifindex != oif || !IN_DEV_TX_REDIRECTS(idev))) {
  353. if (IN_DEV_ACCEPT_LOCAL(idev))
  354. goto ok;
  355. /* with custom local routes in place, checking local addresses
  356. * only will be too optimistic, with custom rules, checking
  357. * local addresses only can be too strict, e.g. due to vrf
  358. */
  359. if (net->ipv4.fib_has_custom_local_routes ||
  360. fib4_has_custom_rules(net))
  361. goto full_check;
  362. if (inet_lookup_ifaddr_rcu(net, src))
  363. return -EINVAL;
  364. ok:
  365. *itag = 0;
  366. return 0;
  367. }
  368. full_check:
  369. return __fib_validate_source(skb, src, dst, tos, oif, dev, r, idev, itag);
  370. }
  371. static inline __be32 sk_extract_addr(struct sockaddr *addr)
  372. {
  373. return ((struct sockaddr_in *) addr)->sin_addr.s_addr;
  374. }
  375. static int put_rtax(struct nlattr *mx, int len, int type, u32 value)
  376. {
  377. struct nlattr *nla;
  378. nla = (struct nlattr *) ((char *) mx + len);
  379. nla->nla_type = type;
  380. nla->nla_len = nla_attr_size(4);
  381. *(u32 *) nla_data(nla) = value;
  382. return len + nla_total_size(4);
  383. }
  384. static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt,
  385. struct fib_config *cfg)
  386. {
  387. __be32 addr;
  388. int plen;
  389. memset(cfg, 0, sizeof(*cfg));
  390. cfg->fc_nlinfo.nl_net = net;
  391. if (rt->rt_dst.sa_family != AF_INET)
  392. return -EAFNOSUPPORT;
  393. /*
  394. * Check mask for validity:
  395. * a) it must be contiguous.
  396. * b) destination must have all host bits clear.
  397. * c) if application forgot to set correct family (AF_INET),
  398. * reject request unless it is absolutely clear i.e.
  399. * both family and mask are zero.
  400. */
  401. plen = 32;
  402. addr = sk_extract_addr(&rt->rt_dst);
  403. if (!(rt->rt_flags & RTF_HOST)) {
  404. __be32 mask = sk_extract_addr(&rt->rt_genmask);
  405. if (rt->rt_genmask.sa_family != AF_INET) {
  406. if (mask || rt->rt_genmask.sa_family)
  407. return -EAFNOSUPPORT;
  408. }
  409. if (bad_mask(mask, addr))
  410. return -EINVAL;
  411. plen = inet_mask_len(mask);
  412. }
  413. cfg->fc_dst_len = plen;
  414. cfg->fc_dst = addr;
  415. if (cmd != SIOCDELRT) {
  416. cfg->fc_nlflags = NLM_F_CREATE;
  417. cfg->fc_protocol = RTPROT_BOOT;
  418. }
  419. if (rt->rt_metric)
  420. cfg->fc_priority = rt->rt_metric - 1;
  421. if (rt->rt_flags & RTF_REJECT) {
  422. cfg->fc_scope = RT_SCOPE_HOST;
  423. cfg->fc_type = RTN_UNREACHABLE;
  424. return 0;
  425. }
  426. cfg->fc_scope = RT_SCOPE_NOWHERE;
  427. cfg->fc_type = RTN_UNICAST;
  428. if (rt->rt_dev) {
  429. char *colon;
  430. struct net_device *dev;
  431. char devname[IFNAMSIZ];
  432. if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1))
  433. return -EFAULT;
  434. devname[IFNAMSIZ-1] = 0;
  435. colon = strchr(devname, ':');
  436. if (colon)
  437. *colon = 0;
  438. dev = __dev_get_by_name(net, devname);
  439. if (!dev)
  440. return -ENODEV;
  441. cfg->fc_oif = dev->ifindex;
  442. cfg->fc_table = l3mdev_fib_table(dev);
  443. if (colon) {
  444. struct in_ifaddr *ifa;
  445. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  446. if (!in_dev)
  447. return -ENODEV;
  448. *colon = ':';
  449. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next)
  450. if (strcmp(ifa->ifa_label, devname) == 0)
  451. break;
  452. if (!ifa)
  453. return -ENODEV;
  454. cfg->fc_prefsrc = ifa->ifa_local;
  455. }
  456. }
  457. addr = sk_extract_addr(&rt->rt_gateway);
  458. if (rt->rt_gateway.sa_family == AF_INET && addr) {
  459. unsigned int addr_type;
  460. cfg->fc_gw = addr;
  461. addr_type = inet_addr_type_table(net, addr, cfg->fc_table);
  462. if (rt->rt_flags & RTF_GATEWAY &&
  463. addr_type == RTN_UNICAST)
  464. cfg->fc_scope = RT_SCOPE_UNIVERSE;
  465. }
  466. if (cmd == SIOCDELRT)
  467. return 0;
  468. if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw)
  469. return -EINVAL;
  470. if (cfg->fc_scope == RT_SCOPE_NOWHERE)
  471. cfg->fc_scope = RT_SCOPE_LINK;
  472. if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) {
  473. struct nlattr *mx;
  474. int len = 0;
  475. mx = kzalloc(3 * nla_total_size(4), GFP_KERNEL);
  476. if (!mx)
  477. return -ENOMEM;
  478. if (rt->rt_flags & RTF_MTU)
  479. len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40);
  480. if (rt->rt_flags & RTF_WINDOW)
  481. len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window);
  482. if (rt->rt_flags & RTF_IRTT)
  483. len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3);
  484. cfg->fc_mx = mx;
  485. cfg->fc_mx_len = len;
  486. }
  487. return 0;
  488. }
  489. /*
  490. * Handle IP routing ioctl calls.
  491. * These are used to manipulate the routing tables
  492. */
  493. int ip_rt_ioctl(struct net *net, unsigned int cmd, void __user *arg)
  494. {
  495. struct fib_config cfg;
  496. struct rtentry rt;
  497. int err;
  498. switch (cmd) {
  499. case SIOCADDRT: /* Add a route */
  500. case SIOCDELRT: /* Delete a route */
  501. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  502. return -EPERM;
  503. if (copy_from_user(&rt, arg, sizeof(rt)))
  504. return -EFAULT;
  505. rtnl_lock();
  506. err = rtentry_to_fib_config(net, cmd, &rt, &cfg);
  507. if (err == 0) {
  508. struct fib_table *tb;
  509. if (cmd == SIOCDELRT) {
  510. tb = fib_get_table(net, cfg.fc_table);
  511. if (tb)
  512. err = fib_table_delete(net, tb, &cfg,
  513. NULL);
  514. else
  515. err = -ESRCH;
  516. } else {
  517. tb = fib_new_table(net, cfg.fc_table);
  518. if (tb)
  519. err = fib_table_insert(net, tb,
  520. &cfg, NULL);
  521. else
  522. err = -ENOBUFS;
  523. }
  524. /* allocated by rtentry_to_fib_config() */
  525. kfree(cfg.fc_mx);
  526. }
  527. rtnl_unlock();
  528. return err;
  529. }
  530. return -EINVAL;
  531. }
  532. const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = {
  533. [RTA_DST] = { .type = NLA_U32 },
  534. [RTA_SRC] = { .type = NLA_U32 },
  535. [RTA_IIF] = { .type = NLA_U32 },
  536. [RTA_OIF] = { .type = NLA_U32 },
  537. [RTA_GATEWAY] = { .type = NLA_U32 },
  538. [RTA_PRIORITY] = { .type = NLA_U32 },
  539. [RTA_PREFSRC] = { .type = NLA_U32 },
  540. [RTA_METRICS] = { .type = NLA_NESTED },
  541. [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
  542. [RTA_FLOW] = { .type = NLA_U32 },
  543. [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
  544. [RTA_ENCAP] = { .type = NLA_NESTED },
  545. [RTA_UID] = { .type = NLA_U32 },
  546. [RTA_MARK] = { .type = NLA_U32 },
  547. };
  548. static int rtm_to_fib_config(struct net *net, struct sk_buff *skb,
  549. struct nlmsghdr *nlh, struct fib_config *cfg,
  550. struct netlink_ext_ack *extack)
  551. {
  552. struct nlattr *attr;
  553. int err, remaining;
  554. struct rtmsg *rtm;
  555. err = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipv4_policy,
  556. extack);
  557. if (err < 0)
  558. goto errout;
  559. memset(cfg, 0, sizeof(*cfg));
  560. rtm = nlmsg_data(nlh);
  561. cfg->fc_dst_len = rtm->rtm_dst_len;
  562. cfg->fc_tos = rtm->rtm_tos;
  563. cfg->fc_table = rtm->rtm_table;
  564. cfg->fc_protocol = rtm->rtm_protocol;
  565. cfg->fc_scope = rtm->rtm_scope;
  566. cfg->fc_type = rtm->rtm_type;
  567. cfg->fc_flags = rtm->rtm_flags;
  568. cfg->fc_nlflags = nlh->nlmsg_flags;
  569. cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
  570. cfg->fc_nlinfo.nlh = nlh;
  571. cfg->fc_nlinfo.nl_net = net;
  572. if (cfg->fc_type > RTN_MAX) {
  573. NL_SET_ERR_MSG(extack, "Invalid route type");
  574. err = -EINVAL;
  575. goto errout;
  576. }
  577. nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) {
  578. switch (nla_type(attr)) {
  579. case RTA_DST:
  580. cfg->fc_dst = nla_get_be32(attr);
  581. break;
  582. case RTA_OIF:
  583. cfg->fc_oif = nla_get_u32(attr);
  584. break;
  585. case RTA_GATEWAY:
  586. cfg->fc_gw = nla_get_be32(attr);
  587. break;
  588. case RTA_PRIORITY:
  589. cfg->fc_priority = nla_get_u32(attr);
  590. break;
  591. case RTA_PREFSRC:
  592. cfg->fc_prefsrc = nla_get_be32(attr);
  593. break;
  594. case RTA_METRICS:
  595. cfg->fc_mx = nla_data(attr);
  596. cfg->fc_mx_len = nla_len(attr);
  597. break;
  598. case RTA_MULTIPATH:
  599. err = lwtunnel_valid_encap_type_attr(nla_data(attr),
  600. nla_len(attr),
  601. extack);
  602. if (err < 0)
  603. goto errout;
  604. cfg->fc_mp = nla_data(attr);
  605. cfg->fc_mp_len = nla_len(attr);
  606. break;
  607. case RTA_FLOW:
  608. cfg->fc_flow = nla_get_u32(attr);
  609. break;
  610. case RTA_TABLE:
  611. cfg->fc_table = nla_get_u32(attr);
  612. break;
  613. case RTA_ENCAP:
  614. cfg->fc_encap = attr;
  615. break;
  616. case RTA_ENCAP_TYPE:
  617. cfg->fc_encap_type = nla_get_u16(attr);
  618. err = lwtunnel_valid_encap_type(cfg->fc_encap_type,
  619. extack);
  620. if (err < 0)
  621. goto errout;
  622. break;
  623. }
  624. }
  625. return 0;
  626. errout:
  627. return err;
  628. }
  629. static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  630. struct netlink_ext_ack *extack)
  631. {
  632. struct net *net = sock_net(skb->sk);
  633. struct fib_config cfg;
  634. struct fib_table *tb;
  635. int err;
  636. err = rtm_to_fib_config(net, skb, nlh, &cfg, extack);
  637. if (err < 0)
  638. goto errout;
  639. tb = fib_get_table(net, cfg.fc_table);
  640. if (!tb) {
  641. NL_SET_ERR_MSG(extack, "FIB table does not exist");
  642. err = -ESRCH;
  643. goto errout;
  644. }
  645. err = fib_table_delete(net, tb, &cfg, extack);
  646. errout:
  647. return err;
  648. }
  649. static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  650. struct netlink_ext_ack *extack)
  651. {
  652. struct net *net = sock_net(skb->sk);
  653. struct fib_config cfg;
  654. struct fib_table *tb;
  655. int err;
  656. err = rtm_to_fib_config(net, skb, nlh, &cfg, extack);
  657. if (err < 0)
  658. goto errout;
  659. tb = fib_new_table(net, cfg.fc_table);
  660. if (!tb) {
  661. err = -ENOBUFS;
  662. goto errout;
  663. }
  664. err = fib_table_insert(net, tb, &cfg, extack);
  665. if (!err && cfg.fc_type == RTN_LOCAL)
  666. net->ipv4.fib_has_custom_local_routes = true;
  667. errout:
  668. return err;
  669. }
  670. static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
  671. {
  672. struct net *net = sock_net(skb->sk);
  673. unsigned int h, s_h;
  674. unsigned int e = 0, s_e;
  675. struct fib_table *tb;
  676. struct hlist_head *head;
  677. int dumped = 0, err;
  678. if (nlmsg_len(cb->nlh) >= sizeof(struct rtmsg) &&
  679. ((struct rtmsg *) nlmsg_data(cb->nlh))->rtm_flags & RTM_F_CLONED)
  680. return skb->len;
  681. s_h = cb->args[0];
  682. s_e = cb->args[1];
  683. rcu_read_lock();
  684. for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
  685. e = 0;
  686. head = &net->ipv4.fib_table_hash[h];
  687. hlist_for_each_entry_rcu(tb, head, tb_hlist) {
  688. if (e < s_e)
  689. goto next;
  690. if (dumped)
  691. memset(&cb->args[2], 0, sizeof(cb->args) -
  692. 2 * sizeof(cb->args[0]));
  693. err = fib_table_dump(tb, skb, cb);
  694. if (err < 0) {
  695. if (likely(skb->len))
  696. goto out;
  697. goto out_err;
  698. }
  699. dumped = 1;
  700. next:
  701. e++;
  702. }
  703. }
  704. out:
  705. err = skb->len;
  706. out_err:
  707. rcu_read_unlock();
  708. cb->args[1] = e;
  709. cb->args[0] = h;
  710. return err;
  711. }
  712. /* Prepare and feed intra-kernel routing request.
  713. * Really, it should be netlink message, but :-( netlink
  714. * can be not configured, so that we feed it directly
  715. * to fib engine. It is legal, because all events occur
  716. * only when netlink is already locked.
  717. */
  718. static void fib_magic(int cmd, int type, __be32 dst, int dst_len, struct in_ifaddr *ifa)
  719. {
  720. struct net *net = dev_net(ifa->ifa_dev->dev);
  721. u32 tb_id = l3mdev_fib_table(ifa->ifa_dev->dev);
  722. struct fib_table *tb;
  723. struct fib_config cfg = {
  724. .fc_protocol = RTPROT_KERNEL,
  725. .fc_type = type,
  726. .fc_dst = dst,
  727. .fc_dst_len = dst_len,
  728. .fc_prefsrc = ifa->ifa_local,
  729. .fc_oif = ifa->ifa_dev->dev->ifindex,
  730. .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
  731. .fc_nlinfo = {
  732. .nl_net = net,
  733. },
  734. };
  735. if (!tb_id)
  736. tb_id = (type == RTN_UNICAST) ? RT_TABLE_MAIN : RT_TABLE_LOCAL;
  737. tb = fib_new_table(net, tb_id);
  738. if (!tb)
  739. return;
  740. cfg.fc_table = tb->tb_id;
  741. if (type != RTN_LOCAL)
  742. cfg.fc_scope = RT_SCOPE_LINK;
  743. else
  744. cfg.fc_scope = RT_SCOPE_HOST;
  745. if (cmd == RTM_NEWROUTE)
  746. fib_table_insert(net, tb, &cfg, NULL);
  747. else
  748. fib_table_delete(net, tb, &cfg, NULL);
  749. }
  750. void fib_add_ifaddr(struct in_ifaddr *ifa)
  751. {
  752. struct in_device *in_dev = ifa->ifa_dev;
  753. struct net_device *dev = in_dev->dev;
  754. struct in_ifaddr *prim = ifa;
  755. __be32 mask = ifa->ifa_mask;
  756. __be32 addr = ifa->ifa_local;
  757. __be32 prefix = ifa->ifa_address & mask;
  758. if (ifa->ifa_flags & IFA_F_SECONDARY) {
  759. prim = inet_ifa_byprefix(in_dev, prefix, mask);
  760. if (!prim) {
  761. pr_warn("%s: bug: prim == NULL\n", __func__);
  762. return;
  763. }
  764. }
  765. fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim);
  766. if (!(dev->flags & IFF_UP))
  767. return;
  768. /* Add broadcast address, if it is explicitly assigned. */
  769. if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF))
  770. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  771. if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags & IFA_F_SECONDARY) &&
  772. (prefix != addr || ifa->ifa_prefixlen < 32)) {
  773. if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE))
  774. fib_magic(RTM_NEWROUTE,
  775. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  776. prefix, ifa->ifa_prefixlen, prim);
  777. /* Add network specific broadcasts, when it takes a sense */
  778. if (ifa->ifa_prefixlen < 31) {
  779. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32, prim);
  780. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix | ~mask,
  781. 32, prim);
  782. }
  783. }
  784. }
  785. /* Delete primary or secondary address.
  786. * Optionally, on secondary address promotion consider the addresses
  787. * from subnet iprim as deleted, even if they are in device list.
  788. * In this case the secondary ifa can be in device list.
  789. */
  790. void fib_del_ifaddr(struct in_ifaddr *ifa, struct in_ifaddr *iprim)
  791. {
  792. struct in_device *in_dev = ifa->ifa_dev;
  793. struct net_device *dev = in_dev->dev;
  794. struct in_ifaddr *ifa1;
  795. struct in_ifaddr *prim = ifa, *prim1 = NULL;
  796. __be32 brd = ifa->ifa_address | ~ifa->ifa_mask;
  797. __be32 any = ifa->ifa_address & ifa->ifa_mask;
  798. #define LOCAL_OK 1
  799. #define BRD_OK 2
  800. #define BRD0_OK 4
  801. #define BRD1_OK 8
  802. unsigned int ok = 0;
  803. int subnet = 0; /* Primary network */
  804. int gone = 1; /* Address is missing */
  805. int same_prefsrc = 0; /* Another primary with same IP */
  806. if (ifa->ifa_flags & IFA_F_SECONDARY) {
  807. prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
  808. if (!prim) {
  809. /* if the device has been deleted, we don't perform
  810. * address promotion
  811. */
  812. if (!in_dev->dead)
  813. pr_warn("%s: bug: prim == NULL\n", __func__);
  814. return;
  815. }
  816. if (iprim && iprim != prim) {
  817. pr_warn("%s: bug: iprim != prim\n", __func__);
  818. return;
  819. }
  820. } else if (!ipv4_is_zeronet(any) &&
  821. (any != ifa->ifa_local || ifa->ifa_prefixlen < 32)) {
  822. if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE))
  823. fib_magic(RTM_DELROUTE,
  824. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  825. any, ifa->ifa_prefixlen, prim);
  826. subnet = 1;
  827. }
  828. if (in_dev->dead)
  829. goto no_promotions;
  830. /* Deletion is more complicated than add.
  831. * We should take care of not to delete too much :-)
  832. *
  833. * Scan address list to be sure that addresses are really gone.
  834. */
  835. for (ifa1 = in_dev->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
  836. if (ifa1 == ifa) {
  837. /* promotion, keep the IP */
  838. gone = 0;
  839. continue;
  840. }
  841. /* Ignore IFAs from our subnet */
  842. if (iprim && ifa1->ifa_mask == iprim->ifa_mask &&
  843. inet_ifa_match(ifa1->ifa_address, iprim))
  844. continue;
  845. /* Ignore ifa1 if it uses different primary IP (prefsrc) */
  846. if (ifa1->ifa_flags & IFA_F_SECONDARY) {
  847. /* Another address from our subnet? */
  848. if (ifa1->ifa_mask == prim->ifa_mask &&
  849. inet_ifa_match(ifa1->ifa_address, prim))
  850. prim1 = prim;
  851. else {
  852. /* We reached the secondaries, so
  853. * same_prefsrc should be determined.
  854. */
  855. if (!same_prefsrc)
  856. continue;
  857. /* Search new prim1 if ifa1 is not
  858. * using the current prim1
  859. */
  860. if (!prim1 ||
  861. ifa1->ifa_mask != prim1->ifa_mask ||
  862. !inet_ifa_match(ifa1->ifa_address, prim1))
  863. prim1 = inet_ifa_byprefix(in_dev,
  864. ifa1->ifa_address,
  865. ifa1->ifa_mask);
  866. if (!prim1)
  867. continue;
  868. if (prim1->ifa_local != prim->ifa_local)
  869. continue;
  870. }
  871. } else {
  872. if (prim->ifa_local != ifa1->ifa_local)
  873. continue;
  874. prim1 = ifa1;
  875. if (prim != prim1)
  876. same_prefsrc = 1;
  877. }
  878. if (ifa->ifa_local == ifa1->ifa_local)
  879. ok |= LOCAL_OK;
  880. if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
  881. ok |= BRD_OK;
  882. if (brd == ifa1->ifa_broadcast)
  883. ok |= BRD1_OK;
  884. if (any == ifa1->ifa_broadcast)
  885. ok |= BRD0_OK;
  886. /* primary has network specific broadcasts */
  887. if (prim1 == ifa1 && ifa1->ifa_prefixlen < 31) {
  888. __be32 brd1 = ifa1->ifa_address | ~ifa1->ifa_mask;
  889. __be32 any1 = ifa1->ifa_address & ifa1->ifa_mask;
  890. if (!ipv4_is_zeronet(any1)) {
  891. if (ifa->ifa_broadcast == brd1 ||
  892. ifa->ifa_broadcast == any1)
  893. ok |= BRD_OK;
  894. if (brd == brd1 || brd == any1)
  895. ok |= BRD1_OK;
  896. if (any == brd1 || any == any1)
  897. ok |= BRD0_OK;
  898. }
  899. }
  900. }
  901. no_promotions:
  902. if (!(ok & BRD_OK))
  903. fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  904. if (subnet && ifa->ifa_prefixlen < 31) {
  905. if (!(ok & BRD1_OK))
  906. fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, prim);
  907. if (!(ok & BRD0_OK))
  908. fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, prim);
  909. }
  910. if (!(ok & LOCAL_OK)) {
  911. unsigned int addr_type;
  912. fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim);
  913. /* Check, that this local address finally disappeared. */
  914. addr_type = inet_addr_type_dev_table(dev_net(dev), dev,
  915. ifa->ifa_local);
  916. if (gone && addr_type != RTN_LOCAL) {
  917. /* And the last, but not the least thing.
  918. * We must flush stray FIB entries.
  919. *
  920. * First of all, we scan fib_info list searching
  921. * for stray nexthop entries, then ignite fib_flush.
  922. */
  923. if (fib_sync_down_addr(dev, ifa->ifa_local))
  924. fib_flush(dev_net(dev));
  925. }
  926. }
  927. #undef LOCAL_OK
  928. #undef BRD_OK
  929. #undef BRD0_OK
  930. #undef BRD1_OK
  931. }
  932. static void nl_fib_lookup(struct net *net, struct fib_result_nl *frn)
  933. {
  934. struct fib_result res;
  935. struct flowi4 fl4 = {
  936. .flowi4_mark = frn->fl_mark,
  937. .daddr = frn->fl_addr,
  938. .flowi4_tos = frn->fl_tos,
  939. .flowi4_scope = frn->fl_scope,
  940. };
  941. struct fib_table *tb;
  942. rcu_read_lock();
  943. tb = fib_get_table(net, frn->tb_id_in);
  944. frn->err = -ENOENT;
  945. if (tb) {
  946. local_bh_disable();
  947. frn->tb_id = tb->tb_id;
  948. frn->err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
  949. if (!frn->err) {
  950. frn->prefixlen = res.prefixlen;
  951. frn->nh_sel = res.nh_sel;
  952. frn->type = res.type;
  953. frn->scope = res.scope;
  954. }
  955. local_bh_enable();
  956. }
  957. rcu_read_unlock();
  958. }
  959. static void nl_fib_input(struct sk_buff *skb)
  960. {
  961. struct net *net;
  962. struct fib_result_nl *frn;
  963. struct nlmsghdr *nlh;
  964. u32 portid;
  965. net = sock_net(skb->sk);
  966. nlh = nlmsg_hdr(skb);
  967. if (skb->len < nlmsg_total_size(sizeof(*frn)) ||
  968. skb->len < nlh->nlmsg_len ||
  969. nlmsg_len(nlh) < sizeof(*frn))
  970. return;
  971. skb = netlink_skb_clone(skb, GFP_KERNEL);
  972. if (!skb)
  973. return;
  974. nlh = nlmsg_hdr(skb);
  975. frn = (struct fib_result_nl *) nlmsg_data(nlh);
  976. nl_fib_lookup(net, frn);
  977. portid = NETLINK_CB(skb).portid; /* netlink portid */
  978. NETLINK_CB(skb).portid = 0; /* from kernel */
  979. NETLINK_CB(skb).dst_group = 0; /* unicast */
  980. netlink_unicast(net->ipv4.fibnl, skb, portid, MSG_DONTWAIT);
  981. }
  982. static int __net_init nl_fib_lookup_init(struct net *net)
  983. {
  984. struct sock *sk;
  985. struct netlink_kernel_cfg cfg = {
  986. .input = nl_fib_input,
  987. };
  988. sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, &cfg);
  989. if (!sk)
  990. return -EAFNOSUPPORT;
  991. net->ipv4.fibnl = sk;
  992. return 0;
  993. }
  994. static void nl_fib_lookup_exit(struct net *net)
  995. {
  996. netlink_kernel_release(net->ipv4.fibnl);
  997. net->ipv4.fibnl = NULL;
  998. }
  999. static void fib_disable_ip(struct net_device *dev, unsigned long event,
  1000. bool force)
  1001. {
  1002. if (fib_sync_down_dev(dev, event, force))
  1003. fib_flush(dev_net(dev));
  1004. else
  1005. rt_cache_flush(dev_net(dev));
  1006. arp_ifdown(dev);
  1007. }
  1008. static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  1009. {
  1010. struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
  1011. struct net_device *dev = ifa->ifa_dev->dev;
  1012. struct net *net = dev_net(dev);
  1013. switch (event) {
  1014. case NETDEV_UP:
  1015. fib_add_ifaddr(ifa);
  1016. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1017. fib_sync_up(dev, RTNH_F_DEAD);
  1018. #endif
  1019. atomic_inc(&net->ipv4.dev_addr_genid);
  1020. rt_cache_flush(dev_net(dev));
  1021. break;
  1022. case NETDEV_DOWN:
  1023. fib_del_ifaddr(ifa, NULL);
  1024. atomic_inc(&net->ipv4.dev_addr_genid);
  1025. if (!ifa->ifa_dev->ifa_list) {
  1026. /* Last address was deleted from this interface.
  1027. * Disable IP.
  1028. */
  1029. fib_disable_ip(dev, event, true);
  1030. } else {
  1031. rt_cache_flush(dev_net(dev));
  1032. }
  1033. break;
  1034. }
  1035. return NOTIFY_DONE;
  1036. }
  1037. static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
  1038. {
  1039. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1040. struct netdev_notifier_changeupper_info *info;
  1041. struct in_device *in_dev;
  1042. struct net *net = dev_net(dev);
  1043. unsigned int flags;
  1044. if (event == NETDEV_UNREGISTER) {
  1045. fib_disable_ip(dev, event, true);
  1046. rt_flush_dev(dev);
  1047. return NOTIFY_DONE;
  1048. }
  1049. in_dev = __in_dev_get_rtnl(dev);
  1050. if (!in_dev)
  1051. return NOTIFY_DONE;
  1052. switch (event) {
  1053. case NETDEV_UP:
  1054. for_ifa(in_dev) {
  1055. fib_add_ifaddr(ifa);
  1056. } endfor_ifa(in_dev);
  1057. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1058. fib_sync_up(dev, RTNH_F_DEAD);
  1059. #endif
  1060. atomic_inc(&net->ipv4.dev_addr_genid);
  1061. rt_cache_flush(net);
  1062. break;
  1063. case NETDEV_DOWN:
  1064. fib_disable_ip(dev, event, false);
  1065. break;
  1066. case NETDEV_CHANGE:
  1067. flags = dev_get_flags(dev);
  1068. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1069. fib_sync_up(dev, RTNH_F_LINKDOWN);
  1070. else
  1071. fib_sync_down_dev(dev, event, false);
  1072. /* fall through */
  1073. case NETDEV_CHANGEMTU:
  1074. rt_cache_flush(net);
  1075. break;
  1076. case NETDEV_CHANGEUPPER:
  1077. info = ptr;
  1078. /* flush all routes if dev is linked to or unlinked from
  1079. * an L3 master device (e.g., VRF)
  1080. */
  1081. if (info->upper_dev && netif_is_l3_master(info->upper_dev))
  1082. fib_disable_ip(dev, NETDEV_DOWN, true);
  1083. break;
  1084. }
  1085. return NOTIFY_DONE;
  1086. }
  1087. static struct notifier_block fib_inetaddr_notifier = {
  1088. .notifier_call = fib_inetaddr_event,
  1089. };
  1090. static struct notifier_block fib_netdev_notifier = {
  1091. .notifier_call = fib_netdev_event,
  1092. };
  1093. static int __net_init ip_fib_net_init(struct net *net)
  1094. {
  1095. int err;
  1096. size_t size = sizeof(struct hlist_head) * FIB_TABLE_HASHSZ;
  1097. err = fib4_notifier_init(net);
  1098. if (err)
  1099. return err;
  1100. /* Avoid false sharing : Use at least a full cache line */
  1101. size = max_t(size_t, size, L1_CACHE_BYTES);
  1102. net->ipv4.fib_table_hash = kzalloc(size, GFP_KERNEL);
  1103. if (!net->ipv4.fib_table_hash) {
  1104. err = -ENOMEM;
  1105. goto err_table_hash_alloc;
  1106. }
  1107. err = fib4_rules_init(net);
  1108. if (err < 0)
  1109. goto err_rules_init;
  1110. return 0;
  1111. err_rules_init:
  1112. kfree(net->ipv4.fib_table_hash);
  1113. err_table_hash_alloc:
  1114. fib4_notifier_exit(net);
  1115. return err;
  1116. }
  1117. static void ip_fib_net_exit(struct net *net)
  1118. {
  1119. unsigned int i;
  1120. rtnl_lock();
  1121. #ifdef CONFIG_IP_MULTIPLE_TABLES
  1122. RCU_INIT_POINTER(net->ipv4.fib_main, NULL);
  1123. RCU_INIT_POINTER(net->ipv4.fib_default, NULL);
  1124. #endif
  1125. for (i = 0; i < FIB_TABLE_HASHSZ; i++) {
  1126. struct hlist_head *head = &net->ipv4.fib_table_hash[i];
  1127. struct hlist_node *tmp;
  1128. struct fib_table *tb;
  1129. hlist_for_each_entry_safe(tb, tmp, head, tb_hlist) {
  1130. hlist_del(&tb->tb_hlist);
  1131. fib_table_flush(net, tb);
  1132. fib_free_table(tb);
  1133. }
  1134. }
  1135. #ifdef CONFIG_IP_MULTIPLE_TABLES
  1136. fib4_rules_exit(net);
  1137. #endif
  1138. rtnl_unlock();
  1139. kfree(net->ipv4.fib_table_hash);
  1140. fib4_notifier_exit(net);
  1141. }
  1142. static int __net_init fib_net_init(struct net *net)
  1143. {
  1144. int error;
  1145. #ifdef CONFIG_IP_ROUTE_CLASSID
  1146. net->ipv4.fib_num_tclassid_users = 0;
  1147. #endif
  1148. error = ip_fib_net_init(net);
  1149. if (error < 0)
  1150. goto out;
  1151. error = nl_fib_lookup_init(net);
  1152. if (error < 0)
  1153. goto out_nlfl;
  1154. error = fib_proc_init(net);
  1155. if (error < 0)
  1156. goto out_proc;
  1157. out:
  1158. return error;
  1159. out_proc:
  1160. nl_fib_lookup_exit(net);
  1161. out_nlfl:
  1162. ip_fib_net_exit(net);
  1163. goto out;
  1164. }
  1165. static void __net_exit fib_net_exit(struct net *net)
  1166. {
  1167. fib_proc_exit(net);
  1168. nl_fib_lookup_exit(net);
  1169. ip_fib_net_exit(net);
  1170. }
  1171. static struct pernet_operations fib_net_ops = {
  1172. .init = fib_net_init,
  1173. .exit = fib_net_exit,
  1174. };
  1175. void __init ip_fib_init(void)
  1176. {
  1177. fib_trie_init();
  1178. register_pernet_subsys(&fib_net_ops);
  1179. register_netdevice_notifier(&fib_netdev_notifier);
  1180. register_inetaddr_notifier(&fib_inetaddr_notifier);
  1181. rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL, 0);
  1182. rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL, 0);
  1183. rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib, 0);
  1184. }