fib_frontend.c 31 KB

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