fib_frontend.c 26 KB

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