fib_frontend.c 25 KB

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