fib_frontend.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * IPv4 Forwarding Information Base: FIB frontend.
  7. *
  8. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <asm/uaccess.h>
  17. #include <linux/bitops.h>
  18. #include <linux/capability.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/string.h>
  23. #include <linux/socket.h>
  24. #include <linux/sockios.h>
  25. #include <linux/errno.h>
  26. #include <linux/in.h>
  27. #include <linux/inet.h>
  28. #include <linux/inetdevice.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/if_addr.h>
  31. #include <linux/if_arp.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/init.h>
  34. #include <linux/list.h>
  35. #include <linux/slab.h>
  36. #include <net/ip.h>
  37. #include <net/protocol.h>
  38. #include <net/route.h>
  39. #include <net/tcp.h>
  40. #include <net/sock.h>
  41. #include <net/arp.h>
  42. #include <net/ip_fib.h>
  43. #include <net/rtnetlink.h>
  44. #include <net/xfrm.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 int __inet_dev_addr_type(struct net *net,
  122. const struct net_device *dev,
  123. __be32 addr)
  124. {
  125. struct flowi4 fl4 = { .daddr = addr };
  126. struct fib_result res;
  127. unsigned int 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, &fl4, &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(struct sk_buff *skb, __be32 src, __be32 dst, u8 tos,
  168. int oif, struct net_device *dev, __be32 *spec_dst,
  169. u32 *itag)
  170. {
  171. struct in_device *in_dev;
  172. struct flowi4 fl4;
  173. struct fib_result res;
  174. int no_addr, rpf, accept_local;
  175. bool dev_match;
  176. int ret;
  177. struct net *net;
  178. fl4.flowi4_oif = 0;
  179. fl4.flowi4_iif = oif ? : LOOPBACK_IFINDEX;
  180. fl4.daddr = src;
  181. fl4.saddr = dst;
  182. fl4.flowi4_tos = tos;
  183. fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
  184. no_addr = rpf = accept_local = 0;
  185. in_dev = __in_dev_get_rcu(dev);
  186. if (in_dev) {
  187. no_addr = in_dev->ifa_list == NULL;
  188. /* Ignore rp_filter for packets protected by IPsec. */
  189. rpf = secpath_exists(skb) ? 0 : IN_DEV_RPFILTER(in_dev);
  190. accept_local = IN_DEV_ACCEPT_LOCAL(in_dev);
  191. fl4.flowi4_mark = IN_DEV_SRC_VMARK(in_dev) ? skb->mark : 0;
  192. }
  193. if (in_dev == NULL)
  194. goto e_inval;
  195. net = dev_net(dev);
  196. if (fib_lookup(net, &fl4, &res))
  197. goto last_resort;
  198. if (res.type != RTN_UNICAST) {
  199. if (res.type != RTN_LOCAL || !accept_local)
  200. goto e_inval;
  201. }
  202. *spec_dst = FIB_RES_PREFSRC(net, res);
  203. fib_combine_itag(itag, &res);
  204. dev_match = false;
  205. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  206. for (ret = 0; ret < res.fi->fib_nhs; ret++) {
  207. struct fib_nh *nh = &res.fi->fib_nh[ret];
  208. if (nh->nh_dev == dev) {
  209. dev_match = true;
  210. break;
  211. }
  212. }
  213. #else
  214. if (FIB_RES_DEV(res) == dev)
  215. dev_match = true;
  216. #endif
  217. if (dev_match) {
  218. ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
  219. return ret;
  220. }
  221. if (no_addr)
  222. goto last_resort;
  223. if (rpf == 1)
  224. goto e_rpf;
  225. fl4.flowi4_oif = dev->ifindex;
  226. ret = 0;
  227. if (fib_lookup(net, &fl4, &res) == 0) {
  228. if (res.type == RTN_UNICAST) {
  229. *spec_dst = FIB_RES_PREFSRC(net, res);
  230. ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
  231. }
  232. }
  233. return ret;
  234. last_resort:
  235. if (rpf)
  236. goto e_rpf;
  237. *spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
  238. *itag = 0;
  239. return 0;
  240. e_inval:
  241. return -EINVAL;
  242. e_rpf:
  243. return -EXDEV;
  244. }
  245. static inline __be32 sk_extract_addr(struct sockaddr *addr)
  246. {
  247. return ((struct sockaddr_in *) addr)->sin_addr.s_addr;
  248. }
  249. static int put_rtax(struct nlattr *mx, int len, int type, u32 value)
  250. {
  251. struct nlattr *nla;
  252. nla = (struct nlattr *) ((char *) mx + len);
  253. nla->nla_type = type;
  254. nla->nla_len = nla_attr_size(4);
  255. *(u32 *) nla_data(nla) = value;
  256. return len + nla_total_size(4);
  257. }
  258. static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt,
  259. struct fib_config *cfg)
  260. {
  261. __be32 addr;
  262. int plen;
  263. memset(cfg, 0, sizeof(*cfg));
  264. cfg->fc_nlinfo.nl_net = net;
  265. if (rt->rt_dst.sa_family != AF_INET)
  266. return -EAFNOSUPPORT;
  267. /*
  268. * Check mask for validity:
  269. * a) it must be contiguous.
  270. * b) destination must have all host bits clear.
  271. * c) if application forgot to set correct family (AF_INET),
  272. * reject request unless it is absolutely clear i.e.
  273. * both family and mask are zero.
  274. */
  275. plen = 32;
  276. addr = sk_extract_addr(&rt->rt_dst);
  277. if (!(rt->rt_flags & RTF_HOST)) {
  278. __be32 mask = sk_extract_addr(&rt->rt_genmask);
  279. if (rt->rt_genmask.sa_family != AF_INET) {
  280. if (mask || rt->rt_genmask.sa_family)
  281. return -EAFNOSUPPORT;
  282. }
  283. if (bad_mask(mask, addr))
  284. return -EINVAL;
  285. plen = inet_mask_len(mask);
  286. }
  287. cfg->fc_dst_len = plen;
  288. cfg->fc_dst = addr;
  289. if (cmd != SIOCDELRT) {
  290. cfg->fc_nlflags = NLM_F_CREATE;
  291. cfg->fc_protocol = RTPROT_BOOT;
  292. }
  293. if (rt->rt_metric)
  294. cfg->fc_priority = rt->rt_metric - 1;
  295. if (rt->rt_flags & RTF_REJECT) {
  296. cfg->fc_scope = RT_SCOPE_HOST;
  297. cfg->fc_type = RTN_UNREACHABLE;
  298. return 0;
  299. }
  300. cfg->fc_scope = RT_SCOPE_NOWHERE;
  301. cfg->fc_type = RTN_UNICAST;
  302. if (rt->rt_dev) {
  303. char *colon;
  304. struct net_device *dev;
  305. char devname[IFNAMSIZ];
  306. if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1))
  307. return -EFAULT;
  308. devname[IFNAMSIZ-1] = 0;
  309. colon = strchr(devname, ':');
  310. if (colon)
  311. *colon = 0;
  312. dev = __dev_get_by_name(net, devname);
  313. if (!dev)
  314. return -ENODEV;
  315. cfg->fc_oif = dev->ifindex;
  316. if (colon) {
  317. struct in_ifaddr *ifa;
  318. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  319. if (!in_dev)
  320. return -ENODEV;
  321. *colon = ':';
  322. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next)
  323. if (strcmp(ifa->ifa_label, devname) == 0)
  324. break;
  325. if (ifa == NULL)
  326. return -ENODEV;
  327. cfg->fc_prefsrc = ifa->ifa_local;
  328. }
  329. }
  330. addr = sk_extract_addr(&rt->rt_gateway);
  331. if (rt->rt_gateway.sa_family == AF_INET && addr) {
  332. cfg->fc_gw = addr;
  333. if (rt->rt_flags & RTF_GATEWAY &&
  334. inet_addr_type(net, addr) == RTN_UNICAST)
  335. cfg->fc_scope = RT_SCOPE_UNIVERSE;
  336. }
  337. if (cmd == SIOCDELRT)
  338. return 0;
  339. if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw)
  340. return -EINVAL;
  341. if (cfg->fc_scope == RT_SCOPE_NOWHERE)
  342. cfg->fc_scope = RT_SCOPE_LINK;
  343. if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) {
  344. struct nlattr *mx;
  345. int len = 0;
  346. mx = kzalloc(3 * nla_total_size(4), GFP_KERNEL);
  347. if (mx == NULL)
  348. return -ENOMEM;
  349. if (rt->rt_flags & RTF_MTU)
  350. len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40);
  351. if (rt->rt_flags & RTF_WINDOW)
  352. len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window);
  353. if (rt->rt_flags & RTF_IRTT)
  354. len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3);
  355. cfg->fc_mx = mx;
  356. cfg->fc_mx_len = len;
  357. }
  358. return 0;
  359. }
  360. /*
  361. * Handle IP routing ioctl calls.
  362. * These are used to manipulate the routing tables
  363. */
  364. int ip_rt_ioctl(struct net *net, unsigned int cmd, void __user *arg)
  365. {
  366. struct fib_config cfg;
  367. struct rtentry rt;
  368. int err;
  369. switch (cmd) {
  370. case SIOCADDRT: /* Add a route */
  371. case SIOCDELRT: /* Delete a route */
  372. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  373. return -EPERM;
  374. if (copy_from_user(&rt, arg, sizeof(rt)))
  375. return -EFAULT;
  376. rtnl_lock();
  377. err = rtentry_to_fib_config(net, cmd, &rt, &cfg);
  378. if (err == 0) {
  379. struct fib_table *tb;
  380. if (cmd == SIOCDELRT) {
  381. tb = fib_get_table(net, cfg.fc_table);
  382. if (tb)
  383. err = fib_table_delete(tb, &cfg);
  384. else
  385. err = -ESRCH;
  386. } else {
  387. tb = fib_new_table(net, cfg.fc_table);
  388. if (tb)
  389. err = fib_table_insert(tb, &cfg);
  390. else
  391. err = -ENOBUFS;
  392. }
  393. /* allocated by rtentry_to_fib_config() */
  394. kfree(cfg.fc_mx);
  395. }
  396. rtnl_unlock();
  397. return err;
  398. }
  399. return -EINVAL;
  400. }
  401. const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = {
  402. [RTA_DST] = { .type = NLA_U32 },
  403. [RTA_SRC] = { .type = NLA_U32 },
  404. [RTA_IIF] = { .type = NLA_U32 },
  405. [RTA_OIF] = { .type = NLA_U32 },
  406. [RTA_GATEWAY] = { .type = NLA_U32 },
  407. [RTA_PRIORITY] = { .type = NLA_U32 },
  408. [RTA_PREFSRC] = { .type = NLA_U32 },
  409. [RTA_METRICS] = { .type = NLA_NESTED },
  410. [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
  411. [RTA_FLOW] = { .type = NLA_U32 },
  412. [RTA_UID] = { .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. pr_warn("%s: bug: prim == NULL\n", __func__);
  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. /* Delete primary or secondary address.
  621. * Optionally, on secondary address promotion consider the addresses
  622. * from subnet iprim as deleted, even if they are in device list.
  623. * In this case the secondary ifa can be in device list.
  624. */
  625. void fib_del_ifaddr(struct in_ifaddr *ifa, struct in_ifaddr *iprim)
  626. {
  627. struct in_device *in_dev = ifa->ifa_dev;
  628. struct net_device *dev = in_dev->dev;
  629. struct in_ifaddr *ifa1;
  630. struct in_ifaddr *prim = ifa, *prim1 = NULL;
  631. __be32 brd = ifa->ifa_address | ~ifa->ifa_mask;
  632. __be32 any = ifa->ifa_address & ifa->ifa_mask;
  633. #define LOCAL_OK 1
  634. #define BRD_OK 2
  635. #define BRD0_OK 4
  636. #define BRD1_OK 8
  637. unsigned int ok = 0;
  638. int subnet = 0; /* Primary network */
  639. int gone = 1; /* Address is missing */
  640. int same_prefsrc = 0; /* Another primary with same IP */
  641. if (ifa->ifa_flags & IFA_F_SECONDARY) {
  642. prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
  643. if (prim == NULL) {
  644. pr_warn("%s: bug: prim == NULL\n", __func__);
  645. return;
  646. }
  647. if (iprim && iprim != prim) {
  648. pr_warn("%s: bug: iprim != prim\n", __func__);
  649. return;
  650. }
  651. } else if (!ipv4_is_zeronet(any) &&
  652. (any != ifa->ifa_local || ifa->ifa_prefixlen < 32)) {
  653. fib_magic(RTM_DELROUTE,
  654. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  655. any, ifa->ifa_prefixlen, prim);
  656. subnet = 1;
  657. }
  658. if (in_dev->dead)
  659. goto no_promotions;
  660. /* Deletion is more complicated than add.
  661. * We should take care of not to delete too much :-)
  662. *
  663. * Scan address list to be sure that addresses are really gone.
  664. */
  665. for (ifa1 = in_dev->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
  666. if (ifa1 == ifa) {
  667. /* promotion, keep the IP */
  668. gone = 0;
  669. continue;
  670. }
  671. /* Ignore IFAs from our subnet */
  672. if (iprim && ifa1->ifa_mask == iprim->ifa_mask &&
  673. inet_ifa_match(ifa1->ifa_address, iprim))
  674. continue;
  675. /* Ignore ifa1 if it uses different primary IP (prefsrc) */
  676. if (ifa1->ifa_flags & IFA_F_SECONDARY) {
  677. /* Another address from our subnet? */
  678. if (ifa1->ifa_mask == prim->ifa_mask &&
  679. inet_ifa_match(ifa1->ifa_address, prim))
  680. prim1 = prim;
  681. else {
  682. /* We reached the secondaries, so
  683. * same_prefsrc should be determined.
  684. */
  685. if (!same_prefsrc)
  686. continue;
  687. /* Search new prim1 if ifa1 is not
  688. * using the current prim1
  689. */
  690. if (!prim1 ||
  691. ifa1->ifa_mask != prim1->ifa_mask ||
  692. !inet_ifa_match(ifa1->ifa_address, prim1))
  693. prim1 = inet_ifa_byprefix(in_dev,
  694. ifa1->ifa_address,
  695. ifa1->ifa_mask);
  696. if (!prim1)
  697. continue;
  698. if (prim1->ifa_local != prim->ifa_local)
  699. continue;
  700. }
  701. } else {
  702. if (prim->ifa_local != ifa1->ifa_local)
  703. continue;
  704. prim1 = ifa1;
  705. if (prim != prim1)
  706. same_prefsrc = 1;
  707. }
  708. if (ifa->ifa_local == ifa1->ifa_local)
  709. ok |= LOCAL_OK;
  710. if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
  711. ok |= BRD_OK;
  712. if (brd == ifa1->ifa_broadcast)
  713. ok |= BRD1_OK;
  714. if (any == ifa1->ifa_broadcast)
  715. ok |= BRD0_OK;
  716. /* primary has network specific broadcasts */
  717. if (prim1 == ifa1 && ifa1->ifa_prefixlen < 31) {
  718. __be32 brd1 = ifa1->ifa_address | ~ifa1->ifa_mask;
  719. __be32 any1 = ifa1->ifa_address & ifa1->ifa_mask;
  720. if (!ipv4_is_zeronet(any1)) {
  721. if (ifa->ifa_broadcast == brd1 ||
  722. ifa->ifa_broadcast == any1)
  723. ok |= BRD_OK;
  724. if (brd == brd1 || brd == any1)
  725. ok |= BRD1_OK;
  726. if (any == brd1 || any == any1)
  727. ok |= BRD0_OK;
  728. }
  729. }
  730. }
  731. no_promotions:
  732. if (!(ok & BRD_OK))
  733. fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  734. if (subnet && ifa->ifa_prefixlen < 31) {
  735. if (!(ok & BRD1_OK))
  736. fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, prim);
  737. if (!(ok & BRD0_OK))
  738. fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, prim);
  739. }
  740. if (!(ok & LOCAL_OK)) {
  741. fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim);
  742. /* Check, that this local address finally disappeared. */
  743. if (gone &&
  744. inet_addr_type(dev_net(dev), ifa->ifa_local) != RTN_LOCAL) {
  745. /* And the last, but not the least thing.
  746. * We must flush stray FIB entries.
  747. *
  748. * First of all, we scan fib_info list searching
  749. * for stray nexthop entries, then ignite fib_flush.
  750. */
  751. if (fib_sync_down_addr(dev_net(dev), ifa->ifa_local))
  752. fib_flush(dev_net(dev));
  753. }
  754. }
  755. #undef LOCAL_OK
  756. #undef BRD_OK
  757. #undef BRD0_OK
  758. #undef BRD1_OK
  759. }
  760. static void nl_fib_lookup(struct fib_result_nl *frn, struct fib_table *tb)
  761. {
  762. struct fib_result res;
  763. struct flowi4 fl4 = {
  764. .flowi4_mark = frn->fl_mark,
  765. .daddr = frn->fl_addr,
  766. .flowi4_tos = frn->fl_tos,
  767. .flowi4_scope = frn->fl_scope,
  768. };
  769. #ifdef CONFIG_IP_MULTIPLE_TABLES
  770. res.r = NULL;
  771. #endif
  772. frn->err = -ENOENT;
  773. if (tb) {
  774. local_bh_disable();
  775. frn->tb_id = tb->tb_id;
  776. rcu_read_lock();
  777. frn->err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
  778. if (!frn->err) {
  779. frn->prefixlen = res.prefixlen;
  780. frn->nh_sel = res.nh_sel;
  781. frn->type = res.type;
  782. frn->scope = res.scope;
  783. }
  784. rcu_read_unlock();
  785. local_bh_enable();
  786. }
  787. }
  788. static void nl_fib_input(struct sk_buff *skb)
  789. {
  790. struct net *net;
  791. struct fib_result_nl *frn;
  792. struct nlmsghdr *nlh;
  793. struct fib_table *tb;
  794. u32 pid;
  795. net = sock_net(skb->sk);
  796. nlh = nlmsg_hdr(skb);
  797. if (skb->len < NLMSG_SPACE(0) || skb->len < nlh->nlmsg_len ||
  798. nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*frn)))
  799. return;
  800. skb = skb_clone(skb, GFP_KERNEL);
  801. if (skb == NULL)
  802. return;
  803. nlh = nlmsg_hdr(skb);
  804. frn = (struct fib_result_nl *) NLMSG_DATA(nlh);
  805. tb = fib_get_table(net, frn->tb_id_in);
  806. nl_fib_lookup(frn, tb);
  807. pid = NETLINK_CB(skb).pid; /* pid of sending process */
  808. NETLINK_CB(skb).pid = 0; /* from kernel */
  809. NETLINK_CB(skb).dst_group = 0; /* unicast */
  810. netlink_unicast(net->ipv4.fibnl, skb, pid, MSG_DONTWAIT);
  811. }
  812. static int __net_init nl_fib_lookup_init(struct net *net)
  813. {
  814. struct sock *sk;
  815. sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, 0,
  816. nl_fib_input, NULL, THIS_MODULE);
  817. if (sk == NULL)
  818. return -EAFNOSUPPORT;
  819. net->ipv4.fibnl = sk;
  820. return 0;
  821. }
  822. static void nl_fib_lookup_exit(struct net *net)
  823. {
  824. netlink_kernel_release(net->ipv4.fibnl);
  825. net->ipv4.fibnl = NULL;
  826. }
  827. static void fib_disable_ip(struct net_device *dev, int force, int delay)
  828. {
  829. if (fib_sync_down_dev(dev, force))
  830. fib_flush(dev_net(dev));
  831. rt_cache_flush(dev_net(dev), delay);
  832. arp_ifdown(dev);
  833. }
  834. static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  835. {
  836. struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
  837. struct net_device *dev = ifa->ifa_dev->dev;
  838. struct net *net = dev_net(dev);
  839. switch (event) {
  840. case NETDEV_UP:
  841. fib_add_ifaddr(ifa);
  842. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  843. fib_sync_up(dev);
  844. #endif
  845. atomic_inc(&net->ipv4.dev_addr_genid);
  846. rt_cache_flush(dev_net(dev), -1);
  847. break;
  848. case NETDEV_DOWN:
  849. fib_del_ifaddr(ifa, NULL);
  850. atomic_inc(&net->ipv4.dev_addr_genid);
  851. if (ifa->ifa_dev->ifa_list == NULL) {
  852. /* Last address was deleted from this interface.
  853. * Disable IP.
  854. */
  855. fib_disable_ip(dev, 1, 0);
  856. } else {
  857. rt_cache_flush(dev_net(dev), -1);
  858. }
  859. break;
  860. }
  861. return NOTIFY_DONE;
  862. }
  863. static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
  864. {
  865. struct net_device *dev = ptr;
  866. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  867. struct net *net = dev_net(dev);
  868. if (event == NETDEV_UNREGISTER) {
  869. fib_disable_ip(dev, 2, -1);
  870. return NOTIFY_DONE;
  871. }
  872. if (!in_dev)
  873. return NOTIFY_DONE;
  874. switch (event) {
  875. case NETDEV_UP:
  876. for_ifa(in_dev) {
  877. fib_add_ifaddr(ifa);
  878. } endfor_ifa(in_dev);
  879. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  880. fib_sync_up(dev);
  881. #endif
  882. atomic_inc(&net->ipv4.dev_addr_genid);
  883. rt_cache_flush(dev_net(dev), -1);
  884. break;
  885. case NETDEV_DOWN:
  886. fib_disable_ip(dev, 0, 0);
  887. break;
  888. case NETDEV_CHANGEMTU:
  889. case NETDEV_CHANGE:
  890. rt_cache_flush(dev_net(dev), 0);
  891. break;
  892. case NETDEV_UNREGISTER_BATCH:
  893. /* The batch unregister is only called on the first
  894. * device in the list of devices being unregistered.
  895. * Therefore we should not pass dev_net(dev) in here.
  896. */
  897. rt_cache_flush_batch(NULL);
  898. break;
  899. }
  900. return NOTIFY_DONE;
  901. }
  902. static struct notifier_block fib_inetaddr_notifier = {
  903. .notifier_call = fib_inetaddr_event,
  904. };
  905. static struct notifier_block fib_netdev_notifier = {
  906. .notifier_call = fib_netdev_event,
  907. };
  908. static int __net_init ip_fib_net_init(struct net *net)
  909. {
  910. int err;
  911. size_t size = sizeof(struct hlist_head) * FIB_TABLE_HASHSZ;
  912. /* Avoid false sharing : Use at least a full cache line */
  913. size = max_t(size_t, size, L1_CACHE_BYTES);
  914. net->ipv4.fib_table_hash = kzalloc(size, GFP_KERNEL);
  915. if (net->ipv4.fib_table_hash == NULL)
  916. return -ENOMEM;
  917. err = fib4_rules_init(net);
  918. if (err < 0)
  919. goto fail;
  920. return 0;
  921. fail:
  922. kfree(net->ipv4.fib_table_hash);
  923. return err;
  924. }
  925. static void ip_fib_net_exit(struct net *net)
  926. {
  927. unsigned int i;
  928. #ifdef CONFIG_IP_MULTIPLE_TABLES
  929. fib4_rules_exit(net);
  930. #endif
  931. rtnl_lock();
  932. for (i = 0; i < FIB_TABLE_HASHSZ; i++) {
  933. struct fib_table *tb;
  934. struct hlist_head *head;
  935. struct hlist_node *node, *tmp;
  936. head = &net->ipv4.fib_table_hash[i];
  937. hlist_for_each_entry_safe(tb, node, tmp, head, tb_hlist) {
  938. hlist_del(node);
  939. fib_table_flush(tb);
  940. fib_free_table(tb);
  941. }
  942. }
  943. rtnl_unlock();
  944. kfree(net->ipv4.fib_table_hash);
  945. }
  946. static int __net_init fib_net_init(struct net *net)
  947. {
  948. int error;
  949. error = ip_fib_net_init(net);
  950. if (error < 0)
  951. goto out;
  952. error = nl_fib_lookup_init(net);
  953. if (error < 0)
  954. goto out_nlfl;
  955. error = fib_proc_init(net);
  956. if (error < 0)
  957. goto out_proc;
  958. out:
  959. return error;
  960. out_proc:
  961. nl_fib_lookup_exit(net);
  962. out_nlfl:
  963. ip_fib_net_exit(net);
  964. goto out;
  965. }
  966. static void __net_exit fib_net_exit(struct net *net)
  967. {
  968. fib_proc_exit(net);
  969. nl_fib_lookup_exit(net);
  970. ip_fib_net_exit(net);
  971. }
  972. static struct pernet_operations fib_net_ops = {
  973. .init = fib_net_init,
  974. .exit = fib_net_exit,
  975. };
  976. void __init ip_fib_init(void)
  977. {
  978. rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL, NULL);
  979. rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL, NULL);
  980. rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib, NULL);
  981. register_pernet_subsys(&fib_net_ops);
  982. register_netdevice_notifier(&fib_netdev_notifier);
  983. register_inetaddr_notifier(&fib_inetaddr_notifier);
  984. fib_trie_init();
  985. }