fib_frontend.c 31 KB

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