fib_frontend.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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 <asm/system.h>
  18. #include <linux/bitops.h>
  19. #include <linux/capability.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/string.h>
  24. #include <linux/socket.h>
  25. #include <linux/sockios.h>
  26. #include <linux/errno.h>
  27. #include <linux/in.h>
  28. #include <linux/inet.h>
  29. #include <linux/inetdevice.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/if_addr.h>
  32. #include <linux/if_arp.h>
  33. #include <linux/skbuff.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. #ifndef CONFIG_IP_MULTIPLE_TABLES
  46. static int __net_init fib4_rules_init(struct net *net)
  47. {
  48. struct fib_table *local_table, *main_table;
  49. local_table = fib_trie_table(RT_TABLE_LOCAL);
  50. if (local_table == NULL)
  51. return -ENOMEM;
  52. main_table = fib_trie_table(RT_TABLE_MAIN);
  53. if (main_table == NULL)
  54. goto fail;
  55. hlist_add_head_rcu(&local_table->tb_hlist,
  56. &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX]);
  57. hlist_add_head_rcu(&main_table->tb_hlist,
  58. &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX]);
  59. return 0;
  60. fail:
  61. kfree(local_table);
  62. return -ENOMEM;
  63. }
  64. #else
  65. struct fib_table *fib_new_table(struct net *net, u32 id)
  66. {
  67. struct fib_table *tb;
  68. unsigned int h;
  69. if (id == 0)
  70. id = RT_TABLE_MAIN;
  71. tb = fib_get_table(net, id);
  72. if (tb)
  73. return tb;
  74. tb = fib_trie_table(id);
  75. if (!tb)
  76. return NULL;
  77. h = id & (FIB_TABLE_HASHSZ - 1);
  78. hlist_add_head_rcu(&tb->tb_hlist, &net->ipv4.fib_table_hash[h]);
  79. return tb;
  80. }
  81. struct fib_table *fib_get_table(struct net *net, u32 id)
  82. {
  83. struct fib_table *tb;
  84. struct hlist_node *node;
  85. struct hlist_head *head;
  86. unsigned int h;
  87. if (id == 0)
  88. id = RT_TABLE_MAIN;
  89. h = id & (FIB_TABLE_HASHSZ - 1);
  90. rcu_read_lock();
  91. head = &net->ipv4.fib_table_hash[h];
  92. hlist_for_each_entry_rcu(tb, node, head, tb_hlist) {
  93. if (tb->tb_id == id) {
  94. rcu_read_unlock();
  95. return tb;
  96. }
  97. }
  98. rcu_read_unlock();
  99. return NULL;
  100. }
  101. #endif /* CONFIG_IP_MULTIPLE_TABLES */
  102. static void fib_flush(struct net *net)
  103. {
  104. int flushed = 0;
  105. struct fib_table *tb;
  106. struct hlist_node *node;
  107. struct hlist_head *head;
  108. unsigned int h;
  109. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  110. head = &net->ipv4.fib_table_hash[h];
  111. hlist_for_each_entry(tb, node, head, tb_hlist)
  112. flushed += fib_table_flush(tb);
  113. }
  114. if (flushed)
  115. rt_cache_flush(net, -1);
  116. }
  117. /*
  118. * Find address type as if only "dev" was present in the system. If
  119. * on_dev is NULL then all interfaces are taken into consideration.
  120. */
  121. static inline unsigned __inet_dev_addr_type(struct net *net,
  122. const struct net_device *dev,
  123. __be32 addr)
  124. {
  125. struct flowi fl = { .fl4_dst = addr };
  126. struct fib_result res;
  127. unsigned ret = RTN_BROADCAST;
  128. struct fib_table *local_table;
  129. if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr))
  130. return RTN_BROADCAST;
  131. if (ipv4_is_multicast(addr))
  132. return RTN_MULTICAST;
  133. #ifdef CONFIG_IP_MULTIPLE_TABLES
  134. res.r = NULL;
  135. #endif
  136. local_table = fib_get_table(net, RT_TABLE_LOCAL);
  137. if (local_table) {
  138. ret = RTN_UNICAST;
  139. rcu_read_lock();
  140. if (!fib_table_lookup(local_table, &fl, &res, FIB_LOOKUP_NOREF)) {
  141. if (!dev || dev == res.fi->fib_dev)
  142. ret = res.type;
  143. }
  144. rcu_read_unlock();
  145. }
  146. return ret;
  147. }
  148. unsigned int inet_addr_type(struct net *net, __be32 addr)
  149. {
  150. return __inet_dev_addr_type(net, NULL, addr);
  151. }
  152. EXPORT_SYMBOL(inet_addr_type);
  153. unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev,
  154. __be32 addr)
  155. {
  156. return __inet_dev_addr_type(net, dev, addr);
  157. }
  158. EXPORT_SYMBOL(inet_dev_addr_type);
  159. /* Given (packet source, input interface) and optional (dst, oif, tos):
  160. * - (main) check, that source is valid i.e. not broadcast or our local
  161. * address.
  162. * - figure out what "logical" interface this packet arrived
  163. * and calculate "specific destination" address.
  164. * - check, that packet arrived from expected physical interface.
  165. * called with rcu_read_lock()
  166. */
  167. int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
  168. struct net_device *dev, __be32 *spec_dst,
  169. u32 *itag, u32 mark)
  170. {
  171. struct in_device *in_dev;
  172. struct flowi fl;
  173. struct fib_result res;
  174. int no_addr, rpf, accept_local;
  175. bool dev_match;
  176. int ret;
  177. struct net *net;
  178. fl.oif = 0;
  179. fl.iif = oif;
  180. fl.mark = mark;
  181. fl.fl4_dst = src;
  182. fl.fl4_src = dst;
  183. fl.fl4_tos = tos;
  184. fl.fl4_scope = RT_SCOPE_UNIVERSE;
  185. no_addr = rpf = accept_local = 0;
  186. in_dev = __in_dev_get_rcu(dev);
  187. if (in_dev) {
  188. no_addr = in_dev->ifa_list == NULL;
  189. rpf = IN_DEV_RPFILTER(in_dev);
  190. accept_local = IN_DEV_ACCEPT_LOCAL(in_dev);
  191. if (mark && !IN_DEV_SRC_VMARK(in_dev))
  192. fl.mark = 0;
  193. }
  194. if (in_dev == NULL)
  195. goto e_inval;
  196. net = dev_net(dev);
  197. if (fib_lookup(net, &fl, &res))
  198. goto last_resort;
  199. if (res.type != RTN_UNICAST) {
  200. if (res.type != RTN_LOCAL || !accept_local)
  201. goto e_inval;
  202. }
  203. *spec_dst = FIB_RES_PREFSRC(res);
  204. fib_combine_itag(itag, &res);
  205. dev_match = false;
  206. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  207. for (ret = 0; ret < res.fi->fib_nhs; ret++) {
  208. struct fib_nh *nh = &res.fi->fib_nh[ret];
  209. if (nh->nh_dev == dev) {
  210. dev_match = true;
  211. break;
  212. }
  213. }
  214. #else
  215. if (FIB_RES_DEV(res) == dev)
  216. dev_match = true;
  217. #endif
  218. if (dev_match) {
  219. ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
  220. return ret;
  221. }
  222. if (no_addr)
  223. goto last_resort;
  224. if (rpf == 1)
  225. goto e_rpf;
  226. fl.oif = dev->ifindex;
  227. ret = 0;
  228. if (fib_lookup(net, &fl, &res) == 0) {
  229. if (res.type == RTN_UNICAST) {
  230. *spec_dst = FIB_RES_PREFSRC(res);
  231. ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
  232. }
  233. }
  234. return ret;
  235. last_resort:
  236. if (rpf)
  237. goto e_rpf;
  238. *spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
  239. *itag = 0;
  240. return 0;
  241. e_inval:
  242. return -EINVAL;
  243. e_rpf:
  244. return -EXDEV;
  245. }
  246. static inline __be32 sk_extract_addr(struct sockaddr *addr)
  247. {
  248. return ((struct sockaddr_in *) addr)->sin_addr.s_addr;
  249. }
  250. static int put_rtax(struct nlattr *mx, int len, int type, u32 value)
  251. {
  252. struct nlattr *nla;
  253. nla = (struct nlattr *) ((char *) mx + len);
  254. nla->nla_type = type;
  255. nla->nla_len = nla_attr_size(4);
  256. *(u32 *) nla_data(nla) = value;
  257. return len + nla_total_size(4);
  258. }
  259. static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt,
  260. struct fib_config *cfg)
  261. {
  262. __be32 addr;
  263. int plen;
  264. memset(cfg, 0, sizeof(*cfg));
  265. cfg->fc_nlinfo.nl_net = net;
  266. if (rt->rt_dst.sa_family != AF_INET)
  267. return -EAFNOSUPPORT;
  268. /*
  269. * Check mask for validity:
  270. * a) it must be contiguous.
  271. * b) destination must have all host bits clear.
  272. * c) if application forgot to set correct family (AF_INET),
  273. * reject request unless it is absolutely clear i.e.
  274. * both family and mask are zero.
  275. */
  276. plen = 32;
  277. addr = sk_extract_addr(&rt->rt_dst);
  278. if (!(rt->rt_flags & RTF_HOST)) {
  279. __be32 mask = sk_extract_addr(&rt->rt_genmask);
  280. if (rt->rt_genmask.sa_family != AF_INET) {
  281. if (mask || rt->rt_genmask.sa_family)
  282. return -EAFNOSUPPORT;
  283. }
  284. if (bad_mask(mask, addr))
  285. return -EINVAL;
  286. plen = inet_mask_len(mask);
  287. }
  288. cfg->fc_dst_len = plen;
  289. cfg->fc_dst = addr;
  290. if (cmd != SIOCDELRT) {
  291. cfg->fc_nlflags = NLM_F_CREATE;
  292. cfg->fc_protocol = RTPROT_BOOT;
  293. }
  294. if (rt->rt_metric)
  295. cfg->fc_priority = rt->rt_metric - 1;
  296. if (rt->rt_flags & RTF_REJECT) {
  297. cfg->fc_scope = RT_SCOPE_HOST;
  298. cfg->fc_type = RTN_UNREACHABLE;
  299. return 0;
  300. }
  301. cfg->fc_scope = RT_SCOPE_NOWHERE;
  302. cfg->fc_type = RTN_UNICAST;
  303. if (rt->rt_dev) {
  304. char *colon;
  305. struct net_device *dev;
  306. char devname[IFNAMSIZ];
  307. if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1))
  308. return -EFAULT;
  309. devname[IFNAMSIZ-1] = 0;
  310. colon = strchr(devname, ':');
  311. if (colon)
  312. *colon = 0;
  313. dev = __dev_get_by_name(net, devname);
  314. if (!dev)
  315. return -ENODEV;
  316. cfg->fc_oif = dev->ifindex;
  317. if (colon) {
  318. struct in_ifaddr *ifa;
  319. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  320. if (!in_dev)
  321. return -ENODEV;
  322. *colon = ':';
  323. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next)
  324. if (strcmp(ifa->ifa_label, devname) == 0)
  325. break;
  326. if (ifa == NULL)
  327. return -ENODEV;
  328. cfg->fc_prefsrc = ifa->ifa_local;
  329. }
  330. }
  331. addr = sk_extract_addr(&rt->rt_gateway);
  332. if (rt->rt_gateway.sa_family == AF_INET && addr) {
  333. cfg->fc_gw = addr;
  334. if (rt->rt_flags & RTF_GATEWAY &&
  335. inet_addr_type(net, addr) == RTN_UNICAST)
  336. cfg->fc_scope = RT_SCOPE_UNIVERSE;
  337. }
  338. if (cmd == SIOCDELRT)
  339. return 0;
  340. if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw)
  341. return -EINVAL;
  342. if (cfg->fc_scope == RT_SCOPE_NOWHERE)
  343. cfg->fc_scope = RT_SCOPE_LINK;
  344. if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) {
  345. struct nlattr *mx;
  346. int len = 0;
  347. mx = kzalloc(3 * nla_total_size(4), GFP_KERNEL);
  348. if (mx == NULL)
  349. return -ENOMEM;
  350. if (rt->rt_flags & RTF_MTU)
  351. len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40);
  352. if (rt->rt_flags & RTF_WINDOW)
  353. len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window);
  354. if (rt->rt_flags & RTF_IRTT)
  355. len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3);
  356. cfg->fc_mx = mx;
  357. cfg->fc_mx_len = len;
  358. }
  359. return 0;
  360. }
  361. /*
  362. * Handle IP routing ioctl calls.
  363. * These are used to manipulate the routing tables
  364. */
  365. int ip_rt_ioctl(struct net *net, unsigned int cmd, void __user *arg)
  366. {
  367. struct fib_config cfg;
  368. struct rtentry rt;
  369. int err;
  370. switch (cmd) {
  371. case SIOCADDRT: /* Add a route */
  372. case SIOCDELRT: /* Delete a route */
  373. if (!capable(CAP_NET_ADMIN))
  374. return -EPERM;
  375. if (copy_from_user(&rt, arg, sizeof(rt)))
  376. return -EFAULT;
  377. rtnl_lock();
  378. err = rtentry_to_fib_config(net, cmd, &rt, &cfg);
  379. if (err == 0) {
  380. struct fib_table *tb;
  381. if (cmd == SIOCDELRT) {
  382. tb = fib_get_table(net, cfg.fc_table);
  383. if (tb)
  384. err = fib_table_delete(tb, &cfg);
  385. else
  386. err = -ESRCH;
  387. } else {
  388. tb = fib_new_table(net, cfg.fc_table);
  389. if (tb)
  390. err = fib_table_insert(tb, &cfg);
  391. else
  392. err = -ENOBUFS;
  393. }
  394. /* allocated by rtentry_to_fib_config() */
  395. kfree(cfg.fc_mx);
  396. }
  397. rtnl_unlock();
  398. return err;
  399. }
  400. return -EINVAL;
  401. }
  402. const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = {
  403. [RTA_DST] = { .type = NLA_U32 },
  404. [RTA_SRC] = { .type = NLA_U32 },
  405. [RTA_IIF] = { .type = NLA_U32 },
  406. [RTA_OIF] = { .type = NLA_U32 },
  407. [RTA_GATEWAY] = { .type = NLA_U32 },
  408. [RTA_PRIORITY] = { .type = NLA_U32 },
  409. [RTA_PREFSRC] = { .type = NLA_U32 },
  410. [RTA_METRICS] = { .type = NLA_NESTED },
  411. [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
  412. [RTA_FLOW] = { .type = NLA_U32 },
  413. };
  414. static int rtm_to_fib_config(struct net *net, struct sk_buff *skb,
  415. struct nlmsghdr *nlh, struct fib_config *cfg)
  416. {
  417. struct nlattr *attr;
  418. int err, remaining;
  419. struct rtmsg *rtm;
  420. err = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipv4_policy);
  421. if (err < 0)
  422. goto errout;
  423. memset(cfg, 0, sizeof(*cfg));
  424. rtm = nlmsg_data(nlh);
  425. cfg->fc_dst_len = rtm->rtm_dst_len;
  426. cfg->fc_tos = rtm->rtm_tos;
  427. cfg->fc_table = rtm->rtm_table;
  428. cfg->fc_protocol = rtm->rtm_protocol;
  429. cfg->fc_scope = rtm->rtm_scope;
  430. cfg->fc_type = rtm->rtm_type;
  431. cfg->fc_flags = rtm->rtm_flags;
  432. cfg->fc_nlflags = nlh->nlmsg_flags;
  433. cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
  434. cfg->fc_nlinfo.nlh = nlh;
  435. cfg->fc_nlinfo.nl_net = net;
  436. if (cfg->fc_type > RTN_MAX) {
  437. err = -EINVAL;
  438. goto errout;
  439. }
  440. nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) {
  441. switch (nla_type(attr)) {
  442. case RTA_DST:
  443. cfg->fc_dst = nla_get_be32(attr);
  444. break;
  445. case RTA_OIF:
  446. cfg->fc_oif = nla_get_u32(attr);
  447. break;
  448. case RTA_GATEWAY:
  449. cfg->fc_gw = nla_get_be32(attr);
  450. break;
  451. case RTA_PRIORITY:
  452. cfg->fc_priority = nla_get_u32(attr);
  453. break;
  454. case RTA_PREFSRC:
  455. cfg->fc_prefsrc = nla_get_be32(attr);
  456. break;
  457. case RTA_METRICS:
  458. cfg->fc_mx = nla_data(attr);
  459. cfg->fc_mx_len = nla_len(attr);
  460. break;
  461. case RTA_MULTIPATH:
  462. cfg->fc_mp = nla_data(attr);
  463. cfg->fc_mp_len = nla_len(attr);
  464. break;
  465. case RTA_FLOW:
  466. cfg->fc_flow = nla_get_u32(attr);
  467. break;
  468. case RTA_TABLE:
  469. cfg->fc_table = nla_get_u32(attr);
  470. break;
  471. }
  472. }
  473. return 0;
  474. errout:
  475. return err;
  476. }
  477. static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  478. {
  479. struct net *net = sock_net(skb->sk);
  480. struct fib_config cfg;
  481. struct fib_table *tb;
  482. int err;
  483. err = rtm_to_fib_config(net, skb, nlh, &cfg);
  484. if (err < 0)
  485. goto errout;
  486. tb = fib_get_table(net, cfg.fc_table);
  487. if (tb == NULL) {
  488. err = -ESRCH;
  489. goto errout;
  490. }
  491. err = fib_table_delete(tb, &cfg);
  492. errout:
  493. return err;
  494. }
  495. static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  496. {
  497. struct net *net = sock_net(skb->sk);
  498. struct fib_config cfg;
  499. struct fib_table *tb;
  500. int err;
  501. err = rtm_to_fib_config(net, skb, nlh, &cfg);
  502. if (err < 0)
  503. goto errout;
  504. tb = fib_new_table(net, cfg.fc_table);
  505. if (tb == NULL) {
  506. err = -ENOBUFS;
  507. goto errout;
  508. }
  509. err = fib_table_insert(tb, &cfg);
  510. errout:
  511. return err;
  512. }
  513. static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
  514. {
  515. struct net *net = sock_net(skb->sk);
  516. unsigned int h, s_h;
  517. unsigned int e = 0, s_e;
  518. struct fib_table *tb;
  519. struct hlist_node *node;
  520. struct hlist_head *head;
  521. int dumped = 0;
  522. if (nlmsg_len(cb->nlh) >= sizeof(struct rtmsg) &&
  523. ((struct rtmsg *) nlmsg_data(cb->nlh))->rtm_flags & RTM_F_CLONED)
  524. return ip_rt_dump(skb, cb);
  525. s_h = cb->args[0];
  526. s_e = cb->args[1];
  527. for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
  528. e = 0;
  529. head = &net->ipv4.fib_table_hash[h];
  530. hlist_for_each_entry(tb, node, head, tb_hlist) {
  531. if (e < s_e)
  532. goto next;
  533. if (dumped)
  534. memset(&cb->args[2], 0, sizeof(cb->args) -
  535. 2 * sizeof(cb->args[0]));
  536. if (fib_table_dump(tb, skb, cb) < 0)
  537. goto out;
  538. dumped = 1;
  539. next:
  540. e++;
  541. }
  542. }
  543. out:
  544. cb->args[1] = e;
  545. cb->args[0] = h;
  546. return skb->len;
  547. }
  548. /* Prepare and feed intra-kernel routing request.
  549. * Really, it should be netlink message, but :-( netlink
  550. * can be not configured, so that we feed it directly
  551. * to fib engine. It is legal, because all events occur
  552. * only when netlink is already locked.
  553. */
  554. static void fib_magic(int cmd, int type, __be32 dst, int dst_len, struct in_ifaddr *ifa)
  555. {
  556. struct net *net = dev_net(ifa->ifa_dev->dev);
  557. struct fib_table *tb;
  558. struct fib_config cfg = {
  559. .fc_protocol = RTPROT_KERNEL,
  560. .fc_type = type,
  561. .fc_dst = dst,
  562. .fc_dst_len = dst_len,
  563. .fc_prefsrc = ifa->ifa_local,
  564. .fc_oif = ifa->ifa_dev->dev->ifindex,
  565. .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
  566. .fc_nlinfo = {
  567. .nl_net = net,
  568. },
  569. };
  570. if (type == RTN_UNICAST)
  571. tb = fib_new_table(net, RT_TABLE_MAIN);
  572. else
  573. tb = fib_new_table(net, RT_TABLE_LOCAL);
  574. if (tb == NULL)
  575. return;
  576. cfg.fc_table = tb->tb_id;
  577. if (type != RTN_LOCAL)
  578. cfg.fc_scope = RT_SCOPE_LINK;
  579. else
  580. cfg.fc_scope = RT_SCOPE_HOST;
  581. if (cmd == RTM_NEWROUTE)
  582. fib_table_insert(tb, &cfg);
  583. else
  584. fib_table_delete(tb, &cfg);
  585. }
  586. void fib_add_ifaddr(struct in_ifaddr *ifa)
  587. {
  588. struct in_device *in_dev = ifa->ifa_dev;
  589. struct net_device *dev = in_dev->dev;
  590. struct in_ifaddr *prim = ifa;
  591. __be32 mask = ifa->ifa_mask;
  592. __be32 addr = ifa->ifa_local;
  593. __be32 prefix = ifa->ifa_address & mask;
  594. if (ifa->ifa_flags & IFA_F_SECONDARY) {
  595. prim = inet_ifa_byprefix(in_dev, prefix, mask);
  596. if (prim == NULL) {
  597. printk(KERN_WARNING "fib_add_ifaddr: bug: prim == NULL\n");
  598. return;
  599. }
  600. }
  601. fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim);
  602. if (!(dev->flags & IFF_UP))
  603. return;
  604. /* Add broadcast address, if it is explicitly assigned. */
  605. if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF))
  606. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  607. if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags & IFA_F_SECONDARY) &&
  608. (prefix != addr || ifa->ifa_prefixlen < 32)) {
  609. fib_magic(RTM_NEWROUTE,
  610. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  611. prefix, ifa->ifa_prefixlen, prim);
  612. /* Add network specific broadcasts, when it takes a sense */
  613. if (ifa->ifa_prefixlen < 31) {
  614. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32, prim);
  615. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix | ~mask,
  616. 32, prim);
  617. }
  618. }
  619. }
  620. static void fib_del_ifaddr(struct in_ifaddr *ifa)
  621. {
  622. struct in_device *in_dev = ifa->ifa_dev;
  623. struct net_device *dev = in_dev->dev;
  624. struct in_ifaddr *ifa1;
  625. struct in_ifaddr *prim = ifa;
  626. __be32 brd = ifa->ifa_address | ~ifa->ifa_mask;
  627. __be32 any = ifa->ifa_address & ifa->ifa_mask;
  628. #define LOCAL_OK 1
  629. #define BRD_OK 2
  630. #define BRD0_OK 4
  631. #define BRD1_OK 8
  632. unsigned ok = 0;
  633. if (!(ifa->ifa_flags & IFA_F_SECONDARY))
  634. fib_magic(RTM_DELROUTE,
  635. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  636. any, ifa->ifa_prefixlen, prim);
  637. else {
  638. prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
  639. if (prim == NULL) {
  640. printk(KERN_WARNING "fib_del_ifaddr: bug: prim == NULL\n");
  641. return;
  642. }
  643. }
  644. /* Deletion is more complicated than add.
  645. * We should take care of not to delete too much :-)
  646. *
  647. * Scan address list to be sure that addresses are really gone.
  648. */
  649. for (ifa1 = in_dev->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
  650. if (ifa->ifa_local == ifa1->ifa_local)
  651. ok |= LOCAL_OK;
  652. if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
  653. ok |= BRD_OK;
  654. if (brd == ifa1->ifa_broadcast)
  655. ok |= BRD1_OK;
  656. if (any == ifa1->ifa_broadcast)
  657. ok |= BRD0_OK;
  658. }
  659. if (!(ok & BRD_OK))
  660. fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  661. if (!(ok & BRD1_OK))
  662. fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, prim);
  663. if (!(ok & BRD0_OK))
  664. fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, prim);
  665. if (!(ok & LOCAL_OK)) {
  666. fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim);
  667. /* Check, that this local address finally disappeared. */
  668. if (inet_addr_type(dev_net(dev), ifa->ifa_local) != RTN_LOCAL) {
  669. /* And the last, but not the least thing.
  670. * We must flush stray FIB entries.
  671. *
  672. * First of all, we scan fib_info list searching
  673. * for stray nexthop entries, then ignite fib_flush.
  674. */
  675. if (fib_sync_down_addr(dev_net(dev), ifa->ifa_local))
  676. fib_flush(dev_net(dev));
  677. }
  678. }
  679. #undef LOCAL_OK
  680. #undef BRD_OK
  681. #undef BRD0_OK
  682. #undef BRD1_OK
  683. }
  684. static void nl_fib_lookup(struct fib_result_nl *frn, struct fib_table *tb)
  685. {
  686. struct fib_result res;
  687. struct flowi fl = {
  688. .mark = frn->fl_mark,
  689. .fl4_dst = frn->fl_addr,
  690. .fl4_tos = frn->fl_tos,
  691. .fl4_scope = frn->fl_scope,
  692. };
  693. #ifdef CONFIG_IP_MULTIPLE_TABLES
  694. res.r = NULL;
  695. #endif
  696. frn->err = -ENOENT;
  697. if (tb) {
  698. local_bh_disable();
  699. frn->tb_id = tb->tb_id;
  700. rcu_read_lock();
  701. frn->err = fib_table_lookup(tb, &fl, &res, FIB_LOOKUP_NOREF);
  702. if (!frn->err) {
  703. frn->prefixlen = res.prefixlen;
  704. frn->nh_sel = res.nh_sel;
  705. frn->type = res.type;
  706. frn->scope = res.scope;
  707. }
  708. rcu_read_unlock();
  709. local_bh_enable();
  710. }
  711. }
  712. static void nl_fib_input(struct sk_buff *skb)
  713. {
  714. struct net *net;
  715. struct fib_result_nl *frn;
  716. struct nlmsghdr *nlh;
  717. struct fib_table *tb;
  718. u32 pid;
  719. net = sock_net(skb->sk);
  720. nlh = nlmsg_hdr(skb);
  721. if (skb->len < NLMSG_SPACE(0) || skb->len < nlh->nlmsg_len ||
  722. nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*frn)))
  723. return;
  724. skb = skb_clone(skb, GFP_KERNEL);
  725. if (skb == NULL)
  726. return;
  727. nlh = nlmsg_hdr(skb);
  728. frn = (struct fib_result_nl *) NLMSG_DATA(nlh);
  729. tb = fib_get_table(net, frn->tb_id_in);
  730. nl_fib_lookup(frn, tb);
  731. pid = NETLINK_CB(skb).pid; /* pid of sending process */
  732. NETLINK_CB(skb).pid = 0; /* from kernel */
  733. NETLINK_CB(skb).dst_group = 0; /* unicast */
  734. netlink_unicast(net->ipv4.fibnl, skb, pid, MSG_DONTWAIT);
  735. }
  736. static int __net_init nl_fib_lookup_init(struct net *net)
  737. {
  738. struct sock *sk;
  739. sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, 0,
  740. nl_fib_input, NULL, THIS_MODULE);
  741. if (sk == NULL)
  742. return -EAFNOSUPPORT;
  743. net->ipv4.fibnl = sk;
  744. return 0;
  745. }
  746. static void nl_fib_lookup_exit(struct net *net)
  747. {
  748. netlink_kernel_release(net->ipv4.fibnl);
  749. net->ipv4.fibnl = NULL;
  750. }
  751. static void fib_disable_ip(struct net_device *dev, int force, int delay)
  752. {
  753. if (fib_sync_down_dev(dev, force))
  754. fib_flush(dev_net(dev));
  755. rt_cache_flush(dev_net(dev), delay);
  756. arp_ifdown(dev);
  757. }
  758. static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  759. {
  760. struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
  761. struct net_device *dev = ifa->ifa_dev->dev;
  762. switch (event) {
  763. case NETDEV_UP:
  764. fib_add_ifaddr(ifa);
  765. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  766. fib_sync_up(dev);
  767. #endif
  768. fib_update_nh_saddrs(dev);
  769. rt_cache_flush(dev_net(dev), -1);
  770. break;
  771. case NETDEV_DOWN:
  772. fib_del_ifaddr(ifa);
  773. fib_update_nh_saddrs(dev);
  774. if (ifa->ifa_dev->ifa_list == NULL) {
  775. /* Last address was deleted from this interface.
  776. * Disable IP.
  777. */
  778. fib_disable_ip(dev, 1, 0);
  779. } else {
  780. rt_cache_flush(dev_net(dev), -1);
  781. }
  782. break;
  783. }
  784. return NOTIFY_DONE;
  785. }
  786. static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
  787. {
  788. struct net_device *dev = ptr;
  789. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  790. if (event == NETDEV_UNREGISTER) {
  791. fib_disable_ip(dev, 2, -1);
  792. return NOTIFY_DONE;
  793. }
  794. if (!in_dev)
  795. return NOTIFY_DONE;
  796. switch (event) {
  797. case NETDEV_UP:
  798. for_ifa(in_dev) {
  799. fib_add_ifaddr(ifa);
  800. } endfor_ifa(in_dev);
  801. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  802. fib_sync_up(dev);
  803. #endif
  804. rt_cache_flush(dev_net(dev), -1);
  805. break;
  806. case NETDEV_DOWN:
  807. fib_disable_ip(dev, 0, 0);
  808. break;
  809. case NETDEV_CHANGEMTU:
  810. case NETDEV_CHANGE:
  811. rt_cache_flush(dev_net(dev), 0);
  812. break;
  813. case NETDEV_UNREGISTER_BATCH:
  814. /* The batch unregister is only called on the first
  815. * device in the list of devices being unregistered.
  816. * Therefore we should not pass dev_net(dev) in here.
  817. */
  818. rt_cache_flush_batch(NULL);
  819. break;
  820. }
  821. return NOTIFY_DONE;
  822. }
  823. static struct notifier_block fib_inetaddr_notifier = {
  824. .notifier_call = fib_inetaddr_event,
  825. };
  826. static struct notifier_block fib_netdev_notifier = {
  827. .notifier_call = fib_netdev_event,
  828. };
  829. static int __net_init ip_fib_net_init(struct net *net)
  830. {
  831. int err;
  832. size_t size = sizeof(struct hlist_head) * FIB_TABLE_HASHSZ;
  833. /* Avoid false sharing : Use at least a full cache line */
  834. size = max_t(size_t, size, L1_CACHE_BYTES);
  835. net->ipv4.fib_table_hash = kzalloc(size, GFP_KERNEL);
  836. if (net->ipv4.fib_table_hash == NULL)
  837. return -ENOMEM;
  838. err = fib4_rules_init(net);
  839. if (err < 0)
  840. goto fail;
  841. return 0;
  842. fail:
  843. kfree(net->ipv4.fib_table_hash);
  844. return err;
  845. }
  846. static void ip_fib_net_exit(struct net *net)
  847. {
  848. unsigned int i;
  849. #ifdef CONFIG_IP_MULTIPLE_TABLES
  850. fib4_rules_exit(net);
  851. #endif
  852. for (i = 0; i < FIB_TABLE_HASHSZ; i++) {
  853. struct fib_table *tb;
  854. struct hlist_head *head;
  855. struct hlist_node *node, *tmp;
  856. head = &net->ipv4.fib_table_hash[i];
  857. hlist_for_each_entry_safe(tb, node, tmp, head, tb_hlist) {
  858. hlist_del(node);
  859. fib_table_flush(tb);
  860. fib_free_table(tb);
  861. }
  862. }
  863. kfree(net->ipv4.fib_table_hash);
  864. }
  865. static int __net_init fib_net_init(struct net *net)
  866. {
  867. int error;
  868. error = ip_fib_net_init(net);
  869. if (error < 0)
  870. goto out;
  871. error = nl_fib_lookup_init(net);
  872. if (error < 0)
  873. goto out_nlfl;
  874. error = fib_proc_init(net);
  875. if (error < 0)
  876. goto out_proc;
  877. out:
  878. return error;
  879. out_proc:
  880. nl_fib_lookup_exit(net);
  881. out_nlfl:
  882. ip_fib_net_exit(net);
  883. goto out;
  884. }
  885. static void __net_exit fib_net_exit(struct net *net)
  886. {
  887. fib_proc_exit(net);
  888. nl_fib_lookup_exit(net);
  889. ip_fib_net_exit(net);
  890. }
  891. static struct pernet_operations fib_net_ops = {
  892. .init = fib_net_init,
  893. .exit = fib_net_exit,
  894. };
  895. void __init ip_fib_init(void)
  896. {
  897. rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL);
  898. rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL);
  899. rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib);
  900. register_pernet_subsys(&fib_net_ops);
  901. register_netdevice_notifier(&fib_netdev_notifier);
  902. register_inetaddr_notifier(&fib_inetaddr_notifier);
  903. fib_trie_init();
  904. }