fib_frontend.c 25 KB

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