fib_frontend.c 27 KB

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