fib_frontend.c 29 KB

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