fib_frontend.c 32 KB

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