fib_frontend.c 32 KB

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