fib_frontend.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  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/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;
  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. no_addr = idev->ifa_list == NULL;
  286. fl4.flowi4_mark = IN_DEV_SRC_VMARK(idev) ? skb->mark : 0;
  287. trace_fib_validate_source(dev, &fl4);
  288. net = dev_net(dev);
  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(dev_net(dev)) &&
  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. else
  496. err = -ESRCH;
  497. } else {
  498. tb = fib_new_table(net, cfg.fc_table);
  499. if (tb)
  500. err = fib_table_insert(net, tb, &cfg);
  501. else
  502. err = -ENOBUFS;
  503. }
  504. /* allocated by rtentry_to_fib_config() */
  505. kfree(cfg.fc_mx);
  506. }
  507. rtnl_unlock();
  508. return err;
  509. }
  510. return -EINVAL;
  511. }
  512. const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = {
  513. [RTA_DST] = { .type = NLA_U32 },
  514. [RTA_SRC] = { .type = NLA_U32 },
  515. [RTA_IIF] = { .type = NLA_U32 },
  516. [RTA_OIF] = { .type = NLA_U32 },
  517. [RTA_GATEWAY] = { .type = NLA_U32 },
  518. [RTA_PRIORITY] = { .type = NLA_U32 },
  519. [RTA_PREFSRC] = { .type = NLA_U32 },
  520. [RTA_METRICS] = { .type = NLA_NESTED },
  521. [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
  522. [RTA_FLOW] = { .type = NLA_U32 },
  523. [RTA_ENCAP_TYPE] = { .type = NLA_U16 },
  524. [RTA_ENCAP] = { .type = NLA_NESTED },
  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. err = lwtunnel_valid_encap_type_attr(nla_data(attr),
  575. nla_len(attr));
  576. if (err < 0)
  577. goto errout;
  578. cfg->fc_mp = nla_data(attr);
  579. cfg->fc_mp_len = nla_len(attr);
  580. break;
  581. case RTA_FLOW:
  582. cfg->fc_flow = nla_get_u32(attr);
  583. break;
  584. case RTA_TABLE:
  585. cfg->fc_table = nla_get_u32(attr);
  586. break;
  587. case RTA_ENCAP:
  588. cfg->fc_encap = attr;
  589. break;
  590. case RTA_ENCAP_TYPE:
  591. cfg->fc_encap_type = nla_get_u16(attr);
  592. err = lwtunnel_valid_encap_type(cfg->fc_encap_type);
  593. if (err < 0)
  594. goto errout;
  595. break;
  596. }
  597. }
  598. return 0;
  599. errout:
  600. return err;
  601. }
  602. static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  603. {
  604. struct net *net = sock_net(skb->sk);
  605. struct fib_config cfg;
  606. struct fib_table *tb;
  607. int err;
  608. err = rtm_to_fib_config(net, skb, nlh, &cfg);
  609. if (err < 0)
  610. goto errout;
  611. tb = fib_get_table(net, cfg.fc_table);
  612. if (!tb) {
  613. err = -ESRCH;
  614. goto errout;
  615. }
  616. err = fib_table_delete(net, tb, &cfg);
  617. errout:
  618. return err;
  619. }
  620. static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  621. {
  622. struct net *net = sock_net(skb->sk);
  623. struct fib_config cfg;
  624. struct fib_table *tb;
  625. int err;
  626. err = rtm_to_fib_config(net, skb, nlh, &cfg);
  627. if (err < 0)
  628. goto errout;
  629. tb = fib_new_table(net, cfg.fc_table);
  630. if (!tb) {
  631. err = -ENOBUFS;
  632. goto errout;
  633. }
  634. err = fib_table_insert(net, tb, &cfg);
  635. errout:
  636. return err;
  637. }
  638. static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
  639. {
  640. struct net *net = sock_net(skb->sk);
  641. unsigned int h, s_h;
  642. unsigned int e = 0, s_e;
  643. struct fib_table *tb;
  644. struct hlist_head *head;
  645. int dumped = 0, err;
  646. if (nlmsg_len(cb->nlh) >= sizeof(struct rtmsg) &&
  647. ((struct rtmsg *) nlmsg_data(cb->nlh))->rtm_flags & RTM_F_CLONED)
  648. return skb->len;
  649. s_h = cb->args[0];
  650. s_e = cb->args[1];
  651. rcu_read_lock();
  652. for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
  653. e = 0;
  654. head = &net->ipv4.fib_table_hash[h];
  655. hlist_for_each_entry_rcu(tb, head, tb_hlist) {
  656. if (e < s_e)
  657. goto next;
  658. if (dumped)
  659. memset(&cb->args[2], 0, sizeof(cb->args) -
  660. 2 * sizeof(cb->args[0]));
  661. err = fib_table_dump(tb, skb, cb);
  662. if (err < 0) {
  663. if (likely(skb->len))
  664. goto out;
  665. goto out_err;
  666. }
  667. dumped = 1;
  668. next:
  669. e++;
  670. }
  671. }
  672. out:
  673. err = skb->len;
  674. out_err:
  675. rcu_read_unlock();
  676. cb->args[1] = e;
  677. cb->args[0] = h;
  678. return err;
  679. }
  680. /* Prepare and feed intra-kernel routing request.
  681. * Really, it should be netlink message, but :-( netlink
  682. * can be not configured, so that we feed it directly
  683. * to fib engine. It is legal, because all events occur
  684. * only when netlink is already locked.
  685. */
  686. static void fib_magic(int cmd, int type, __be32 dst, int dst_len, struct in_ifaddr *ifa)
  687. {
  688. struct net *net = dev_net(ifa->ifa_dev->dev);
  689. u32 tb_id = l3mdev_fib_table(ifa->ifa_dev->dev);
  690. struct fib_table *tb;
  691. struct fib_config cfg = {
  692. .fc_protocol = RTPROT_KERNEL,
  693. .fc_type = type,
  694. .fc_dst = dst,
  695. .fc_dst_len = dst_len,
  696. .fc_prefsrc = ifa->ifa_local,
  697. .fc_oif = ifa->ifa_dev->dev->ifindex,
  698. .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
  699. .fc_nlinfo = {
  700. .nl_net = net,
  701. },
  702. };
  703. if (!tb_id)
  704. tb_id = (type == RTN_UNICAST) ? RT_TABLE_MAIN : RT_TABLE_LOCAL;
  705. tb = fib_new_table(net, tb_id);
  706. if (!tb)
  707. return;
  708. cfg.fc_table = tb->tb_id;
  709. if (type != RTN_LOCAL)
  710. cfg.fc_scope = RT_SCOPE_LINK;
  711. else
  712. cfg.fc_scope = RT_SCOPE_HOST;
  713. if (cmd == RTM_NEWROUTE)
  714. fib_table_insert(net, tb, &cfg);
  715. else
  716. fib_table_delete(net, tb, &cfg);
  717. }
  718. void fib_add_ifaddr(struct in_ifaddr *ifa)
  719. {
  720. struct in_device *in_dev = ifa->ifa_dev;
  721. struct net_device *dev = in_dev->dev;
  722. struct in_ifaddr *prim = ifa;
  723. __be32 mask = ifa->ifa_mask;
  724. __be32 addr = ifa->ifa_local;
  725. __be32 prefix = ifa->ifa_address & mask;
  726. if (ifa->ifa_flags & IFA_F_SECONDARY) {
  727. prim = inet_ifa_byprefix(in_dev, prefix, mask);
  728. if (!prim) {
  729. pr_warn("%s: bug: prim == NULL\n", __func__);
  730. return;
  731. }
  732. }
  733. fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim);
  734. if (!(dev->flags & IFF_UP))
  735. return;
  736. /* Add broadcast address, if it is explicitly assigned. */
  737. if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF))
  738. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  739. if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags & IFA_F_SECONDARY) &&
  740. (prefix != addr || ifa->ifa_prefixlen < 32)) {
  741. if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE))
  742. fib_magic(RTM_NEWROUTE,
  743. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  744. prefix, ifa->ifa_prefixlen, prim);
  745. /* Add network specific broadcasts, when it takes a sense */
  746. if (ifa->ifa_prefixlen < 31) {
  747. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32, prim);
  748. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix | ~mask,
  749. 32, prim);
  750. }
  751. }
  752. }
  753. /* Delete primary or secondary address.
  754. * Optionally, on secondary address promotion consider the addresses
  755. * from subnet iprim as deleted, even if they are in device list.
  756. * In this case the secondary ifa can be in device list.
  757. */
  758. void fib_del_ifaddr(struct in_ifaddr *ifa, struct in_ifaddr *iprim)
  759. {
  760. struct in_device *in_dev = ifa->ifa_dev;
  761. struct net_device *dev = in_dev->dev;
  762. struct in_ifaddr *ifa1;
  763. struct in_ifaddr *prim = ifa, *prim1 = NULL;
  764. __be32 brd = ifa->ifa_address | ~ifa->ifa_mask;
  765. __be32 any = ifa->ifa_address & ifa->ifa_mask;
  766. #define LOCAL_OK 1
  767. #define BRD_OK 2
  768. #define BRD0_OK 4
  769. #define BRD1_OK 8
  770. unsigned int ok = 0;
  771. int subnet = 0; /* Primary network */
  772. int gone = 1; /* Address is missing */
  773. int same_prefsrc = 0; /* Another primary with same IP */
  774. if (ifa->ifa_flags & IFA_F_SECONDARY) {
  775. prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
  776. if (!prim) {
  777. /* if the device has been deleted, we don't perform
  778. * address promotion
  779. */
  780. if (!in_dev->dead)
  781. pr_warn("%s: bug: prim == NULL\n", __func__);
  782. return;
  783. }
  784. if (iprim && iprim != prim) {
  785. pr_warn("%s: bug: iprim != prim\n", __func__);
  786. return;
  787. }
  788. } else if (!ipv4_is_zeronet(any) &&
  789. (any != ifa->ifa_local || ifa->ifa_prefixlen < 32)) {
  790. if (!(ifa->ifa_flags & IFA_F_NOPREFIXROUTE))
  791. fib_magic(RTM_DELROUTE,
  792. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  793. any, ifa->ifa_prefixlen, prim);
  794. subnet = 1;
  795. }
  796. if (in_dev->dead)
  797. goto no_promotions;
  798. /* Deletion is more complicated than add.
  799. * We should take care of not to delete too much :-)
  800. *
  801. * Scan address list to be sure that addresses are really gone.
  802. */
  803. for (ifa1 = in_dev->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
  804. if (ifa1 == ifa) {
  805. /* promotion, keep the IP */
  806. gone = 0;
  807. continue;
  808. }
  809. /* Ignore IFAs from our subnet */
  810. if (iprim && ifa1->ifa_mask == iprim->ifa_mask &&
  811. inet_ifa_match(ifa1->ifa_address, iprim))
  812. continue;
  813. /* Ignore ifa1 if it uses different primary IP (prefsrc) */
  814. if (ifa1->ifa_flags & IFA_F_SECONDARY) {
  815. /* Another address from our subnet? */
  816. if (ifa1->ifa_mask == prim->ifa_mask &&
  817. inet_ifa_match(ifa1->ifa_address, prim))
  818. prim1 = prim;
  819. else {
  820. /* We reached the secondaries, so
  821. * same_prefsrc should be determined.
  822. */
  823. if (!same_prefsrc)
  824. continue;
  825. /* Search new prim1 if ifa1 is not
  826. * using the current prim1
  827. */
  828. if (!prim1 ||
  829. ifa1->ifa_mask != prim1->ifa_mask ||
  830. !inet_ifa_match(ifa1->ifa_address, prim1))
  831. prim1 = inet_ifa_byprefix(in_dev,
  832. ifa1->ifa_address,
  833. ifa1->ifa_mask);
  834. if (!prim1)
  835. continue;
  836. if (prim1->ifa_local != prim->ifa_local)
  837. continue;
  838. }
  839. } else {
  840. if (prim->ifa_local != ifa1->ifa_local)
  841. continue;
  842. prim1 = ifa1;
  843. if (prim != prim1)
  844. same_prefsrc = 1;
  845. }
  846. if (ifa->ifa_local == ifa1->ifa_local)
  847. ok |= LOCAL_OK;
  848. if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
  849. ok |= BRD_OK;
  850. if (brd == ifa1->ifa_broadcast)
  851. ok |= BRD1_OK;
  852. if (any == ifa1->ifa_broadcast)
  853. ok |= BRD0_OK;
  854. /* primary has network specific broadcasts */
  855. if (prim1 == ifa1 && ifa1->ifa_prefixlen < 31) {
  856. __be32 brd1 = ifa1->ifa_address | ~ifa1->ifa_mask;
  857. __be32 any1 = ifa1->ifa_address & ifa1->ifa_mask;
  858. if (!ipv4_is_zeronet(any1)) {
  859. if (ifa->ifa_broadcast == brd1 ||
  860. ifa->ifa_broadcast == any1)
  861. ok |= BRD_OK;
  862. if (brd == brd1 || brd == any1)
  863. ok |= BRD1_OK;
  864. if (any == brd1 || any == any1)
  865. ok |= BRD0_OK;
  866. }
  867. }
  868. }
  869. no_promotions:
  870. if (!(ok & BRD_OK))
  871. fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  872. if (subnet && ifa->ifa_prefixlen < 31) {
  873. if (!(ok & BRD1_OK))
  874. fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, prim);
  875. if (!(ok & BRD0_OK))
  876. fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, prim);
  877. }
  878. if (!(ok & LOCAL_OK)) {
  879. unsigned int addr_type;
  880. fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim);
  881. /* Check, that this local address finally disappeared. */
  882. addr_type = inet_addr_type_dev_table(dev_net(dev), dev,
  883. ifa->ifa_local);
  884. if (gone && addr_type != RTN_LOCAL) {
  885. /* And the last, but not the least thing.
  886. * We must flush stray FIB entries.
  887. *
  888. * First of all, we scan fib_info list searching
  889. * for stray nexthop entries, then ignite fib_flush.
  890. */
  891. if (fib_sync_down_addr(dev, ifa->ifa_local))
  892. fib_flush(dev_net(dev));
  893. }
  894. }
  895. #undef LOCAL_OK
  896. #undef BRD_OK
  897. #undef BRD0_OK
  898. #undef BRD1_OK
  899. }
  900. static void nl_fib_lookup(struct net *net, struct fib_result_nl *frn)
  901. {
  902. struct fib_result res;
  903. struct flowi4 fl4 = {
  904. .flowi4_mark = frn->fl_mark,
  905. .daddr = frn->fl_addr,
  906. .flowi4_tos = frn->fl_tos,
  907. .flowi4_scope = frn->fl_scope,
  908. };
  909. struct fib_table *tb;
  910. rcu_read_lock();
  911. tb = fib_get_table(net, frn->tb_id_in);
  912. frn->err = -ENOENT;
  913. if (tb) {
  914. local_bh_disable();
  915. frn->tb_id = tb->tb_id;
  916. frn->err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
  917. if (!frn->err) {
  918. frn->prefixlen = res.prefixlen;
  919. frn->nh_sel = res.nh_sel;
  920. frn->type = res.type;
  921. frn->scope = res.scope;
  922. }
  923. local_bh_enable();
  924. }
  925. rcu_read_unlock();
  926. }
  927. static void nl_fib_input(struct sk_buff *skb)
  928. {
  929. struct net *net;
  930. struct fib_result_nl *frn;
  931. struct nlmsghdr *nlh;
  932. u32 portid;
  933. net = sock_net(skb->sk);
  934. nlh = nlmsg_hdr(skb);
  935. if (skb->len < nlmsg_total_size(sizeof(*frn)) ||
  936. skb->len < nlh->nlmsg_len ||
  937. nlmsg_len(nlh) < sizeof(*frn))
  938. return;
  939. skb = netlink_skb_clone(skb, GFP_KERNEL);
  940. if (!skb)
  941. return;
  942. nlh = nlmsg_hdr(skb);
  943. frn = (struct fib_result_nl *) nlmsg_data(nlh);
  944. nl_fib_lookup(net, frn);
  945. portid = NETLINK_CB(skb).portid; /* netlink portid */
  946. NETLINK_CB(skb).portid = 0; /* from kernel */
  947. NETLINK_CB(skb).dst_group = 0; /* unicast */
  948. netlink_unicast(net->ipv4.fibnl, skb, portid, MSG_DONTWAIT);
  949. }
  950. static int __net_init nl_fib_lookup_init(struct net *net)
  951. {
  952. struct sock *sk;
  953. struct netlink_kernel_cfg cfg = {
  954. .input = nl_fib_input,
  955. };
  956. sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, &cfg);
  957. if (!sk)
  958. return -EAFNOSUPPORT;
  959. net->ipv4.fibnl = sk;
  960. return 0;
  961. }
  962. static void nl_fib_lookup_exit(struct net *net)
  963. {
  964. netlink_kernel_release(net->ipv4.fibnl);
  965. net->ipv4.fibnl = NULL;
  966. }
  967. static void fib_disable_ip(struct net_device *dev, unsigned long event,
  968. bool force)
  969. {
  970. if (fib_sync_down_dev(dev, event, force))
  971. fib_flush(dev_net(dev));
  972. rt_cache_flush(dev_net(dev));
  973. arp_ifdown(dev);
  974. }
  975. static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  976. {
  977. struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
  978. struct net_device *dev = ifa->ifa_dev->dev;
  979. struct net *net = dev_net(dev);
  980. switch (event) {
  981. case NETDEV_UP:
  982. fib_add_ifaddr(ifa);
  983. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  984. fib_sync_up(dev, RTNH_F_DEAD);
  985. #endif
  986. atomic_inc(&net->ipv4.dev_addr_genid);
  987. rt_cache_flush(dev_net(dev));
  988. break;
  989. case NETDEV_DOWN:
  990. fib_del_ifaddr(ifa, NULL);
  991. atomic_inc(&net->ipv4.dev_addr_genid);
  992. if (!ifa->ifa_dev->ifa_list) {
  993. /* Last address was deleted from this interface.
  994. * Disable IP.
  995. */
  996. fib_disable_ip(dev, event, true);
  997. } else {
  998. rt_cache_flush(dev_net(dev));
  999. }
  1000. break;
  1001. }
  1002. return NOTIFY_DONE;
  1003. }
  1004. static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
  1005. {
  1006. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1007. struct netdev_notifier_changeupper_info *info;
  1008. struct in_device *in_dev;
  1009. struct net *net = dev_net(dev);
  1010. unsigned int flags;
  1011. if (event == NETDEV_UNREGISTER) {
  1012. fib_disable_ip(dev, event, true);
  1013. rt_flush_dev(dev);
  1014. return NOTIFY_DONE;
  1015. }
  1016. in_dev = __in_dev_get_rtnl(dev);
  1017. if (!in_dev)
  1018. return NOTIFY_DONE;
  1019. switch (event) {
  1020. case NETDEV_UP:
  1021. for_ifa(in_dev) {
  1022. fib_add_ifaddr(ifa);
  1023. } endfor_ifa(in_dev);
  1024. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1025. fib_sync_up(dev, RTNH_F_DEAD);
  1026. #endif
  1027. atomic_inc(&net->ipv4.dev_addr_genid);
  1028. rt_cache_flush(net);
  1029. break;
  1030. case NETDEV_DOWN:
  1031. fib_disable_ip(dev, event, false);
  1032. break;
  1033. case NETDEV_CHANGE:
  1034. flags = dev_get_flags(dev);
  1035. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1036. fib_sync_up(dev, RTNH_F_LINKDOWN);
  1037. else
  1038. fib_sync_down_dev(dev, event, false);
  1039. /* fall through */
  1040. case NETDEV_CHANGEMTU:
  1041. rt_cache_flush(net);
  1042. break;
  1043. case NETDEV_CHANGEUPPER:
  1044. info = ptr;
  1045. /* flush all routes if dev is linked to or unlinked from
  1046. * an L3 master device (e.g., VRF)
  1047. */
  1048. if (info->upper_dev && netif_is_l3_master(info->upper_dev))
  1049. fib_disable_ip(dev, NETDEV_DOWN, true);
  1050. break;
  1051. }
  1052. return NOTIFY_DONE;
  1053. }
  1054. static struct notifier_block fib_inetaddr_notifier = {
  1055. .notifier_call = fib_inetaddr_event,
  1056. };
  1057. static struct notifier_block fib_netdev_notifier = {
  1058. .notifier_call = fib_netdev_event,
  1059. };
  1060. static int __net_init ip_fib_net_init(struct net *net)
  1061. {
  1062. int err;
  1063. size_t size = sizeof(struct hlist_head) * FIB_TABLE_HASHSZ;
  1064. /* Avoid false sharing : Use at least a full cache line */
  1065. size = max_t(size_t, size, L1_CACHE_BYTES);
  1066. net->ipv4.fib_table_hash = kzalloc(size, GFP_KERNEL);
  1067. if (!net->ipv4.fib_table_hash)
  1068. return -ENOMEM;
  1069. err = fib4_rules_init(net);
  1070. if (err < 0)
  1071. goto fail;
  1072. return 0;
  1073. fail:
  1074. kfree(net->ipv4.fib_table_hash);
  1075. return err;
  1076. }
  1077. static void ip_fib_net_exit(struct net *net)
  1078. {
  1079. unsigned int i;
  1080. rtnl_lock();
  1081. #ifdef CONFIG_IP_MULTIPLE_TABLES
  1082. RCU_INIT_POINTER(net->ipv4.fib_main, NULL);
  1083. RCU_INIT_POINTER(net->ipv4.fib_default, NULL);
  1084. #endif
  1085. for (i = 0; i < FIB_TABLE_HASHSZ; i++) {
  1086. struct hlist_head *head = &net->ipv4.fib_table_hash[i];
  1087. struct hlist_node *tmp;
  1088. struct fib_table *tb;
  1089. hlist_for_each_entry_safe(tb, tmp, head, tb_hlist) {
  1090. hlist_del(&tb->tb_hlist);
  1091. fib_table_flush(net, tb);
  1092. fib_free_table(tb);
  1093. }
  1094. }
  1095. #ifdef CONFIG_IP_MULTIPLE_TABLES
  1096. fib4_rules_exit(net);
  1097. #endif
  1098. rtnl_unlock();
  1099. kfree(net->ipv4.fib_table_hash);
  1100. }
  1101. static int __net_init fib_net_init(struct net *net)
  1102. {
  1103. int error;
  1104. #ifdef CONFIG_IP_ROUTE_CLASSID
  1105. net->ipv4.fib_num_tclassid_users = 0;
  1106. #endif
  1107. error = ip_fib_net_init(net);
  1108. if (error < 0)
  1109. goto out;
  1110. error = nl_fib_lookup_init(net);
  1111. if (error < 0)
  1112. goto out_nlfl;
  1113. error = fib_proc_init(net);
  1114. if (error < 0)
  1115. goto out_proc;
  1116. out:
  1117. return error;
  1118. out_proc:
  1119. nl_fib_lookup_exit(net);
  1120. out_nlfl:
  1121. ip_fib_net_exit(net);
  1122. goto out;
  1123. }
  1124. static void __net_exit fib_net_exit(struct net *net)
  1125. {
  1126. fib_proc_exit(net);
  1127. nl_fib_lookup_exit(net);
  1128. ip_fib_net_exit(net);
  1129. }
  1130. static struct pernet_operations fib_net_ops = {
  1131. .init = fib_net_init,
  1132. .exit = fib_net_exit,
  1133. };
  1134. void __init ip_fib_init(void)
  1135. {
  1136. fib_trie_init();
  1137. register_pernet_subsys(&fib_net_ops);
  1138. register_netdevice_notifier(&fib_netdev_notifier);
  1139. register_inetaddr_notifier(&fib_inetaddr_notifier);
  1140. rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL, NULL);
  1141. rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL, NULL);
  1142. rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib, NULL);
  1143. }