fib_frontend.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  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 == NULL)
  52. return -ENOMEM;
  53. local_table = fib_trie_table(RT_TABLE_LOCAL, main_table);
  54. if (local_table == NULL)
  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))
  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))
  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) == 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 == NULL)
  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 == NULL)
  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. };
  503. static int rtm_to_fib_config(struct net *net, struct sk_buff *skb,
  504. struct nlmsghdr *nlh, struct fib_config *cfg)
  505. {
  506. struct nlattr *attr;
  507. int err, remaining;
  508. struct rtmsg *rtm;
  509. err = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipv4_policy);
  510. if (err < 0)
  511. goto errout;
  512. memset(cfg, 0, sizeof(*cfg));
  513. rtm = nlmsg_data(nlh);
  514. cfg->fc_dst_len = rtm->rtm_dst_len;
  515. cfg->fc_tos = rtm->rtm_tos;
  516. cfg->fc_table = rtm->rtm_table;
  517. cfg->fc_protocol = rtm->rtm_protocol;
  518. cfg->fc_scope = rtm->rtm_scope;
  519. cfg->fc_type = rtm->rtm_type;
  520. cfg->fc_flags = rtm->rtm_flags;
  521. cfg->fc_nlflags = nlh->nlmsg_flags;
  522. cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid;
  523. cfg->fc_nlinfo.nlh = nlh;
  524. cfg->fc_nlinfo.nl_net = net;
  525. if (cfg->fc_type > RTN_MAX) {
  526. err = -EINVAL;
  527. goto errout;
  528. }
  529. nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) {
  530. switch (nla_type(attr)) {
  531. case RTA_DST:
  532. cfg->fc_dst = nla_get_be32(attr);
  533. break;
  534. case RTA_OIF:
  535. cfg->fc_oif = nla_get_u32(attr);
  536. break;
  537. case RTA_GATEWAY:
  538. cfg->fc_gw = nla_get_be32(attr);
  539. break;
  540. case RTA_PRIORITY:
  541. cfg->fc_priority = nla_get_u32(attr);
  542. break;
  543. case RTA_PREFSRC:
  544. cfg->fc_prefsrc = nla_get_be32(attr);
  545. break;
  546. case RTA_METRICS:
  547. cfg->fc_mx = nla_data(attr);
  548. cfg->fc_mx_len = nla_len(attr);
  549. break;
  550. case RTA_MULTIPATH:
  551. cfg->fc_mp = nla_data(attr);
  552. cfg->fc_mp_len = nla_len(attr);
  553. break;
  554. case RTA_FLOW:
  555. cfg->fc_flow = nla_get_u32(attr);
  556. break;
  557. case RTA_TABLE:
  558. cfg->fc_table = nla_get_u32(attr);
  559. break;
  560. }
  561. }
  562. return 0;
  563. errout:
  564. return err;
  565. }
  566. static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  567. {
  568. struct net *net = sock_net(skb->sk);
  569. struct fib_config cfg;
  570. struct fib_table *tb;
  571. int err;
  572. err = rtm_to_fib_config(net, skb, nlh, &cfg);
  573. if (err < 0)
  574. goto errout;
  575. tb = fib_get_table(net, cfg.fc_table);
  576. if (tb == NULL) {
  577. err = -ESRCH;
  578. goto errout;
  579. }
  580. err = fib_table_delete(tb, &cfg);
  581. errout:
  582. return err;
  583. }
  584. static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
  585. {
  586. struct net *net = sock_net(skb->sk);
  587. struct fib_config cfg;
  588. struct fib_table *tb;
  589. int err;
  590. err = rtm_to_fib_config(net, skb, nlh, &cfg);
  591. if (err < 0)
  592. goto errout;
  593. tb = fib_new_table(net, cfg.fc_table);
  594. if (tb == NULL) {
  595. err = -ENOBUFS;
  596. goto errout;
  597. }
  598. err = fib_table_insert(tb, &cfg);
  599. errout:
  600. return err;
  601. }
  602. static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
  603. {
  604. struct net *net = sock_net(skb->sk);
  605. unsigned int h, s_h;
  606. unsigned int e = 0, s_e;
  607. struct fib_table *tb;
  608. struct hlist_head *head;
  609. int dumped = 0;
  610. if (nlmsg_len(cb->nlh) >= sizeof(struct rtmsg) &&
  611. ((struct rtmsg *) nlmsg_data(cb->nlh))->rtm_flags & RTM_F_CLONED)
  612. return skb->len;
  613. s_h = cb->args[0];
  614. s_e = cb->args[1];
  615. rcu_read_lock();
  616. for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
  617. e = 0;
  618. head = &net->ipv4.fib_table_hash[h];
  619. hlist_for_each_entry_rcu(tb, head, tb_hlist) {
  620. if (e < s_e)
  621. goto next;
  622. if (dumped)
  623. memset(&cb->args[2], 0, sizeof(cb->args) -
  624. 2 * sizeof(cb->args[0]));
  625. if (fib_table_dump(tb, skb, cb) < 0)
  626. goto out;
  627. dumped = 1;
  628. next:
  629. e++;
  630. }
  631. }
  632. out:
  633. rcu_read_unlock();
  634. cb->args[1] = e;
  635. cb->args[0] = h;
  636. return skb->len;
  637. }
  638. /* Prepare and feed intra-kernel routing request.
  639. * Really, it should be netlink message, but :-( netlink
  640. * can be not configured, so that we feed it directly
  641. * to fib engine. It is legal, because all events occur
  642. * only when netlink is already locked.
  643. */
  644. static void fib_magic(int cmd, int type, __be32 dst, int dst_len, struct in_ifaddr *ifa)
  645. {
  646. struct net *net = dev_net(ifa->ifa_dev->dev);
  647. struct fib_table *tb;
  648. struct fib_config cfg = {
  649. .fc_protocol = RTPROT_KERNEL,
  650. .fc_type = type,
  651. .fc_dst = dst,
  652. .fc_dst_len = dst_len,
  653. .fc_prefsrc = ifa->ifa_local,
  654. .fc_oif = ifa->ifa_dev->dev->ifindex,
  655. .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
  656. .fc_nlinfo = {
  657. .nl_net = net,
  658. },
  659. };
  660. if (type == RTN_UNICAST)
  661. tb = fib_new_table(net, RT_TABLE_MAIN);
  662. else
  663. tb = fib_new_table(net, RT_TABLE_LOCAL);
  664. if (tb == NULL)
  665. return;
  666. cfg.fc_table = tb->tb_id;
  667. if (type != RTN_LOCAL)
  668. cfg.fc_scope = RT_SCOPE_LINK;
  669. else
  670. cfg.fc_scope = RT_SCOPE_HOST;
  671. if (cmd == RTM_NEWROUTE)
  672. fib_table_insert(tb, &cfg);
  673. else
  674. fib_table_delete(tb, &cfg);
  675. }
  676. void fib_add_ifaddr(struct in_ifaddr *ifa)
  677. {
  678. struct in_device *in_dev = ifa->ifa_dev;
  679. struct net_device *dev = in_dev->dev;
  680. struct in_ifaddr *prim = ifa;
  681. __be32 mask = ifa->ifa_mask;
  682. __be32 addr = ifa->ifa_local;
  683. __be32 prefix = ifa->ifa_address & mask;
  684. if (ifa->ifa_flags & IFA_F_SECONDARY) {
  685. prim = inet_ifa_byprefix(in_dev, prefix, mask);
  686. if (prim == NULL) {
  687. pr_warn("%s: bug: prim == NULL\n", __func__);
  688. return;
  689. }
  690. }
  691. fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim);
  692. if (!(dev->flags & IFF_UP))
  693. return;
  694. /* Add broadcast address, if it is explicitly assigned. */
  695. if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF))
  696. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  697. if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags & IFA_F_SECONDARY) &&
  698. (prefix != addr || ifa->ifa_prefixlen < 32)) {
  699. fib_magic(RTM_NEWROUTE,
  700. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  701. prefix, ifa->ifa_prefixlen, prim);
  702. /* Add network specific broadcasts, when it takes a sense */
  703. if (ifa->ifa_prefixlen < 31) {
  704. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32, prim);
  705. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix | ~mask,
  706. 32, prim);
  707. }
  708. }
  709. }
  710. /* Delete primary or secondary address.
  711. * Optionally, on secondary address promotion consider the addresses
  712. * from subnet iprim as deleted, even if they are in device list.
  713. * In this case the secondary ifa can be in device list.
  714. */
  715. void fib_del_ifaddr(struct in_ifaddr *ifa, struct in_ifaddr *iprim)
  716. {
  717. struct in_device *in_dev = ifa->ifa_dev;
  718. struct net_device *dev = in_dev->dev;
  719. struct in_ifaddr *ifa1;
  720. struct in_ifaddr *prim = ifa, *prim1 = NULL;
  721. __be32 brd = ifa->ifa_address | ~ifa->ifa_mask;
  722. __be32 any = ifa->ifa_address & ifa->ifa_mask;
  723. #define LOCAL_OK 1
  724. #define BRD_OK 2
  725. #define BRD0_OK 4
  726. #define BRD1_OK 8
  727. unsigned int ok = 0;
  728. int subnet = 0; /* Primary network */
  729. int gone = 1; /* Address is missing */
  730. int same_prefsrc = 0; /* Another primary with same IP */
  731. if (ifa->ifa_flags & IFA_F_SECONDARY) {
  732. prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
  733. if (prim == NULL) {
  734. pr_warn("%s: bug: prim == NULL\n", __func__);
  735. return;
  736. }
  737. if (iprim && iprim != prim) {
  738. pr_warn("%s: bug: iprim != prim\n", __func__);
  739. return;
  740. }
  741. } else if (!ipv4_is_zeronet(any) &&
  742. (any != ifa->ifa_local || ifa->ifa_prefixlen < 32)) {
  743. fib_magic(RTM_DELROUTE,
  744. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  745. any, ifa->ifa_prefixlen, prim);
  746. subnet = 1;
  747. }
  748. /* Deletion is more complicated than add.
  749. * We should take care of not to delete too much :-)
  750. *
  751. * Scan address list to be sure that addresses are really gone.
  752. */
  753. for (ifa1 = in_dev->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
  754. if (ifa1 == ifa) {
  755. /* promotion, keep the IP */
  756. gone = 0;
  757. continue;
  758. }
  759. /* Ignore IFAs from our subnet */
  760. if (iprim && ifa1->ifa_mask == iprim->ifa_mask &&
  761. inet_ifa_match(ifa1->ifa_address, iprim))
  762. continue;
  763. /* Ignore ifa1 if it uses different primary IP (prefsrc) */
  764. if (ifa1->ifa_flags & IFA_F_SECONDARY) {
  765. /* Another address from our subnet? */
  766. if (ifa1->ifa_mask == prim->ifa_mask &&
  767. inet_ifa_match(ifa1->ifa_address, prim))
  768. prim1 = prim;
  769. else {
  770. /* We reached the secondaries, so
  771. * same_prefsrc should be determined.
  772. */
  773. if (!same_prefsrc)
  774. continue;
  775. /* Search new prim1 if ifa1 is not
  776. * using the current prim1
  777. */
  778. if (!prim1 ||
  779. ifa1->ifa_mask != prim1->ifa_mask ||
  780. !inet_ifa_match(ifa1->ifa_address, prim1))
  781. prim1 = inet_ifa_byprefix(in_dev,
  782. ifa1->ifa_address,
  783. ifa1->ifa_mask);
  784. if (!prim1)
  785. continue;
  786. if (prim1->ifa_local != prim->ifa_local)
  787. continue;
  788. }
  789. } else {
  790. if (prim->ifa_local != ifa1->ifa_local)
  791. continue;
  792. prim1 = ifa1;
  793. if (prim != prim1)
  794. same_prefsrc = 1;
  795. }
  796. if (ifa->ifa_local == ifa1->ifa_local)
  797. ok |= LOCAL_OK;
  798. if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
  799. ok |= BRD_OK;
  800. if (brd == ifa1->ifa_broadcast)
  801. ok |= BRD1_OK;
  802. if (any == ifa1->ifa_broadcast)
  803. ok |= BRD0_OK;
  804. /* primary has network specific broadcasts */
  805. if (prim1 == ifa1 && ifa1->ifa_prefixlen < 31) {
  806. __be32 brd1 = ifa1->ifa_address | ~ifa1->ifa_mask;
  807. __be32 any1 = ifa1->ifa_address & ifa1->ifa_mask;
  808. if (!ipv4_is_zeronet(any1)) {
  809. if (ifa->ifa_broadcast == brd1 ||
  810. ifa->ifa_broadcast == any1)
  811. ok |= BRD_OK;
  812. if (brd == brd1 || brd == any1)
  813. ok |= BRD1_OK;
  814. if (any == brd1 || any == any1)
  815. ok |= BRD0_OK;
  816. }
  817. }
  818. }
  819. if (!(ok & BRD_OK))
  820. fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  821. if (subnet && ifa->ifa_prefixlen < 31) {
  822. if (!(ok & BRD1_OK))
  823. fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, prim);
  824. if (!(ok & BRD0_OK))
  825. fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, prim);
  826. }
  827. if (!(ok & LOCAL_OK)) {
  828. fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim);
  829. /* Check, that this local address finally disappeared. */
  830. if (gone &&
  831. inet_addr_type(dev_net(dev), ifa->ifa_local) != RTN_LOCAL) {
  832. /* And the last, but not the least thing.
  833. * We must flush stray FIB entries.
  834. *
  835. * First of all, we scan fib_info list searching
  836. * for stray nexthop entries, then ignite fib_flush.
  837. */
  838. if (fib_sync_down_addr(dev_net(dev), ifa->ifa_local))
  839. fib_flush(dev_net(dev));
  840. }
  841. }
  842. #undef LOCAL_OK
  843. #undef BRD_OK
  844. #undef BRD0_OK
  845. #undef BRD1_OK
  846. }
  847. static void nl_fib_lookup(struct net *net, struct fib_result_nl *frn)
  848. {
  849. struct fib_result res;
  850. struct flowi4 fl4 = {
  851. .flowi4_mark = frn->fl_mark,
  852. .daddr = frn->fl_addr,
  853. .flowi4_tos = frn->fl_tos,
  854. .flowi4_scope = frn->fl_scope,
  855. };
  856. struct fib_table *tb;
  857. rcu_read_lock();
  858. tb = fib_get_table(net, frn->tb_id_in);
  859. frn->err = -ENOENT;
  860. if (tb) {
  861. local_bh_disable();
  862. frn->tb_id = tb->tb_id;
  863. frn->err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
  864. if (!frn->err) {
  865. frn->prefixlen = res.prefixlen;
  866. frn->nh_sel = res.nh_sel;
  867. frn->type = res.type;
  868. frn->scope = res.scope;
  869. }
  870. local_bh_enable();
  871. }
  872. rcu_read_unlock();
  873. }
  874. static void nl_fib_input(struct sk_buff *skb)
  875. {
  876. struct net *net;
  877. struct fib_result_nl *frn;
  878. struct nlmsghdr *nlh;
  879. u32 portid;
  880. net = sock_net(skb->sk);
  881. nlh = nlmsg_hdr(skb);
  882. if (skb->len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len ||
  883. nlmsg_len(nlh) < sizeof(*frn))
  884. return;
  885. skb = netlink_skb_clone(skb, GFP_KERNEL);
  886. if (skb == NULL)
  887. return;
  888. nlh = nlmsg_hdr(skb);
  889. frn = (struct fib_result_nl *) nlmsg_data(nlh);
  890. nl_fib_lookup(net, frn);
  891. portid = NETLINK_CB(skb).portid; /* netlink portid */
  892. NETLINK_CB(skb).portid = 0; /* from kernel */
  893. NETLINK_CB(skb).dst_group = 0; /* unicast */
  894. netlink_unicast(net->ipv4.fibnl, skb, portid, MSG_DONTWAIT);
  895. }
  896. static int __net_init nl_fib_lookup_init(struct net *net)
  897. {
  898. struct sock *sk;
  899. struct netlink_kernel_cfg cfg = {
  900. .input = nl_fib_input,
  901. };
  902. sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, &cfg);
  903. if (sk == NULL)
  904. return -EAFNOSUPPORT;
  905. net->ipv4.fibnl = sk;
  906. return 0;
  907. }
  908. static void nl_fib_lookup_exit(struct net *net)
  909. {
  910. netlink_kernel_release(net->ipv4.fibnl);
  911. net->ipv4.fibnl = NULL;
  912. }
  913. static void fib_disable_ip(struct net_device *dev, int force)
  914. {
  915. if (fib_sync_down_dev(dev, force))
  916. fib_flush(dev_net(dev));
  917. rt_cache_flush(dev_net(dev));
  918. arp_ifdown(dev);
  919. }
  920. static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  921. {
  922. struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
  923. struct net_device *dev = ifa->ifa_dev->dev;
  924. struct net *net = dev_net(dev);
  925. switch (event) {
  926. case NETDEV_UP:
  927. fib_add_ifaddr(ifa);
  928. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  929. fib_sync_up(dev);
  930. #endif
  931. atomic_inc(&net->ipv4.dev_addr_genid);
  932. rt_cache_flush(dev_net(dev));
  933. break;
  934. case NETDEV_DOWN:
  935. fib_del_ifaddr(ifa, NULL);
  936. atomic_inc(&net->ipv4.dev_addr_genid);
  937. if (ifa->ifa_dev->ifa_list == NULL) {
  938. /* Last address was deleted from this interface.
  939. * Disable IP.
  940. */
  941. fib_disable_ip(dev, 1);
  942. } else {
  943. rt_cache_flush(dev_net(dev));
  944. }
  945. break;
  946. }
  947. return NOTIFY_DONE;
  948. }
  949. static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
  950. {
  951. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  952. struct in_device *in_dev;
  953. struct net *net = dev_net(dev);
  954. if (event == NETDEV_UNREGISTER) {
  955. fib_disable_ip(dev, 2);
  956. rt_flush_dev(dev);
  957. return NOTIFY_DONE;
  958. }
  959. in_dev = __in_dev_get_rtnl(dev);
  960. if (!in_dev)
  961. return NOTIFY_DONE;
  962. switch (event) {
  963. case NETDEV_UP:
  964. for_ifa(in_dev) {
  965. fib_add_ifaddr(ifa);
  966. } endfor_ifa(in_dev);
  967. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  968. fib_sync_up(dev);
  969. #endif
  970. atomic_inc(&net->ipv4.dev_addr_genid);
  971. rt_cache_flush(net);
  972. break;
  973. case NETDEV_DOWN:
  974. fib_disable_ip(dev, 0);
  975. break;
  976. case NETDEV_CHANGEMTU:
  977. case NETDEV_CHANGE:
  978. rt_cache_flush(net);
  979. break;
  980. }
  981. return NOTIFY_DONE;
  982. }
  983. static struct notifier_block fib_inetaddr_notifier = {
  984. .notifier_call = fib_inetaddr_event,
  985. };
  986. static struct notifier_block fib_netdev_notifier = {
  987. .notifier_call = fib_netdev_event,
  988. };
  989. static int __net_init ip_fib_net_init(struct net *net)
  990. {
  991. int err;
  992. size_t size = sizeof(struct hlist_head) * FIB_TABLE_HASHSZ;
  993. /* Avoid false sharing : Use at least a full cache line */
  994. size = max_t(size_t, size, L1_CACHE_BYTES);
  995. net->ipv4.fib_table_hash = kzalloc(size, GFP_KERNEL);
  996. if (net->ipv4.fib_table_hash == NULL)
  997. return -ENOMEM;
  998. err = fib4_rules_init(net);
  999. if (err < 0)
  1000. goto fail;
  1001. return 0;
  1002. fail:
  1003. kfree(net->ipv4.fib_table_hash);
  1004. return err;
  1005. }
  1006. static void ip_fib_net_exit(struct net *net)
  1007. {
  1008. unsigned int i;
  1009. rtnl_lock();
  1010. #ifdef CONFIG_IP_MULTIPLE_TABLES
  1011. fib4_rules_exit(net);
  1012. #endif
  1013. for (i = 0; i < FIB_TABLE_HASHSZ; i++) {
  1014. struct hlist_head *head = &net->ipv4.fib_table_hash[i];
  1015. struct hlist_node *tmp;
  1016. struct fib_table *tb;
  1017. /* this is done in two passes as flushing the table could
  1018. * cause it to be reallocated in order to accommodate new
  1019. * tnodes at the root as the table shrinks.
  1020. */
  1021. hlist_for_each_entry_safe(tb, tmp, head, tb_hlist)
  1022. fib_table_flush(tb);
  1023. hlist_for_each_entry_safe(tb, tmp, head, tb_hlist) {
  1024. #ifdef CONFIG_IP_MULTIPLE_TABLES
  1025. switch (tb->tb_id) {
  1026. case RT_TABLE_LOCAL:
  1027. RCU_INIT_POINTER(net->ipv4.fib_local, NULL);
  1028. break;
  1029. case RT_TABLE_MAIN:
  1030. RCU_INIT_POINTER(net->ipv4.fib_main, NULL);
  1031. break;
  1032. case RT_TABLE_DEFAULT:
  1033. RCU_INIT_POINTER(net->ipv4.fib_default, NULL);
  1034. break;
  1035. default:
  1036. break;
  1037. }
  1038. #endif
  1039. hlist_del(&tb->tb_hlist);
  1040. fib_free_table(tb);
  1041. }
  1042. }
  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. }