fib_frontend.c 30 KB

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