fib_frontend.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * IPv4 Forwarding Information Base: FIB frontend.
  7. *
  8. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <asm/uaccess.h>
  17. #include <asm/system.h>
  18. #include <linux/bitops.h>
  19. #include <linux/capability.h>
  20. #include <linux/types.h>
  21. #include <linux/kernel.h>
  22. #include <linux/mm.h>
  23. #include <linux/string.h>
  24. #include <linux/socket.h>
  25. #include <linux/sockios.h>
  26. #include <linux/errno.h>
  27. #include <linux/in.h>
  28. #include <linux/inet.h>
  29. #include <linux/inetdevice.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/if_addr.h>
  32. #include <linux/if_arp.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/init.h>
  35. #include <linux/list.h>
  36. #include <linux/slab.h>
  37. #include <net/ip.h>
  38. #include <net/protocol.h>
  39. #include <net/route.h>
  40. #include <net/tcp.h>
  41. #include <net/sock.h>
  42. #include <net/arp.h>
  43. #include <net/ip_fib.h>
  44. #include <net/rtnetlink.h>
  45. #ifndef CONFIG_IP_MULTIPLE_TABLES
  46. static int __net_init fib4_rules_init(struct net *net)
  47. {
  48. struct fib_table *local_table, *main_table;
  49. local_table = fib_trie_table(RT_TABLE_LOCAL);
  50. if (local_table == NULL)
  51. return -ENOMEM;
  52. main_table = fib_trie_table(RT_TABLE_MAIN);
  53. if (main_table == NULL)
  54. goto fail;
  55. hlist_add_head_rcu(&local_table->tb_hlist,
  56. &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX]);
  57. hlist_add_head_rcu(&main_table->tb_hlist,
  58. &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX]);
  59. return 0;
  60. fail:
  61. kfree(local_table);
  62. return -ENOMEM;
  63. }
  64. #else
  65. struct fib_table *fib_new_table(struct net *net, u32 id)
  66. {
  67. struct fib_table *tb;
  68. unsigned int h;
  69. if (id == 0)
  70. id = RT_TABLE_MAIN;
  71. tb = fib_get_table(net, id);
  72. if (tb)
  73. return tb;
  74. tb = fib_trie_table(id);
  75. if (!tb)
  76. return NULL;
  77. h = id & (FIB_TABLE_HASHSZ - 1);
  78. hlist_add_head_rcu(&tb->tb_hlist, &net->ipv4.fib_table_hash[h]);
  79. return tb;
  80. }
  81. struct fib_table *fib_get_table(struct net *net, u32 id)
  82. {
  83. struct fib_table *tb;
  84. struct hlist_node *node;
  85. struct hlist_head *head;
  86. unsigned int h;
  87. if (id == 0)
  88. id = RT_TABLE_MAIN;
  89. h = id & (FIB_TABLE_HASHSZ - 1);
  90. rcu_read_lock();
  91. head = &net->ipv4.fib_table_hash[h];
  92. hlist_for_each_entry_rcu(tb, node, head, tb_hlist) {
  93. if (tb->tb_id == id) {
  94. rcu_read_unlock();
  95. return tb;
  96. }
  97. }
  98. rcu_read_unlock();
  99. return NULL;
  100. }
  101. #endif /* CONFIG_IP_MULTIPLE_TABLES */
  102. static void fib_flush(struct net *net)
  103. {
  104. int flushed = 0;
  105. struct fib_table *tb;
  106. struct hlist_node *node;
  107. struct hlist_head *head;
  108. unsigned int h;
  109. for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
  110. head = &net->ipv4.fib_table_hash[h];
  111. hlist_for_each_entry(tb, node, head, tb_hlist)
  112. flushed += fib_table_flush(tb);
  113. }
  114. if (flushed)
  115. rt_cache_flush(net, -1);
  116. }
  117. /*
  118. * 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 __inet_dev_addr_type(struct net *net,
  122. const struct net_device *dev,
  123. __be32 addr)
  124. {
  125. struct flowi fl = { .fl4_dst = addr };
  126. struct fib_result res;
  127. unsigned 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, &fl, &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(__be32 src, __be32 dst, u8 tos, int oif,
  168. struct net_device *dev, __be32 *spec_dst,
  169. u32 *itag, u32 mark)
  170. {
  171. struct in_device *in_dev;
  172. struct flowi fl = {
  173. .fl4_dst = src,
  174. .fl4_src = dst,
  175. .fl4_tos = tos,
  176. .mark = mark,
  177. .iif = oif
  178. };
  179. struct fib_result res;
  180. int no_addr, rpf, accept_local;
  181. bool dev_match;
  182. int ret;
  183. struct net *net;
  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. rpf = IN_DEV_RPFILTER(in_dev);
  189. accept_local = IN_DEV_ACCEPT_LOCAL(in_dev);
  190. if (mark && !IN_DEV_SRC_VMARK(in_dev))
  191. fl.mark = 0;
  192. }
  193. if (in_dev == NULL)
  194. goto e_inval;
  195. net = dev_net(dev);
  196. if (fib_lookup(net, &fl, &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(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. fl.oif = dev->ifindex;
  226. ret = 0;
  227. if (fib_lookup(net, &fl, &res) == 0) {
  228. if (res.type == RTN_UNICAST) {
  229. *spec_dst = FIB_RES_PREFSRC(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 (!capable(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. };
  413. static int rtm_to_fib_config(struct net *net, struct sk_buff *skb,
  414. struct nlmsghdr *nlh, struct fib_config *cfg)
  415. {
  416. struct nlattr *attr;
  417. int err, remaining;
  418. struct rtmsg *rtm;
  419. err = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipv4_policy);
  420. if (err < 0)
  421. goto errout;
  422. memset(cfg, 0, sizeof(*cfg));
  423. rtm = nlmsg_data(nlh);
  424. cfg->fc_dst_len = rtm->rtm_dst_len;
  425. cfg->fc_tos = rtm->rtm_tos;
  426. cfg->fc_table = rtm->rtm_table;
  427. cfg->fc_protocol = rtm->rtm_protocol;
  428. cfg->fc_scope = rtm->rtm_scope;
  429. cfg->fc_type = rtm->rtm_type;
  430. cfg->fc_flags = rtm->rtm_flags;
  431. cfg->fc_nlflags = nlh->nlmsg_flags;
  432. cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
  433. cfg->fc_nlinfo.nlh = nlh;
  434. cfg->fc_nlinfo.nl_net = net;
  435. if (cfg->fc_type > RTN_MAX) {
  436. err = -EINVAL;
  437. goto errout;
  438. }
  439. nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) {
  440. switch (nla_type(attr)) {
  441. case RTA_DST:
  442. cfg->fc_dst = nla_get_be32(attr);
  443. break;
  444. case RTA_OIF:
  445. cfg->fc_oif = nla_get_u32(attr);
  446. break;
  447. case RTA_GATEWAY:
  448. cfg->fc_gw = nla_get_be32(attr);
  449. break;
  450. case RTA_PRIORITY:
  451. cfg->fc_priority = nla_get_u32(attr);
  452. break;
  453. case RTA_PREFSRC:
  454. cfg->fc_prefsrc = nla_get_be32(attr);
  455. break;
  456. case RTA_METRICS:
  457. cfg->fc_mx = nla_data(attr);
  458. cfg->fc_mx_len = nla_len(attr);
  459. break;
  460. case RTA_MULTIPATH:
  461. cfg->fc_mp = nla_data(attr);
  462. cfg->fc_mp_len = nla_len(attr);
  463. break;
  464. case RTA_FLOW:
  465. cfg->fc_flow = nla_get_u32(attr);
  466. break;
  467. case RTA_TABLE:
  468. cfg->fc_table = nla_get_u32(attr);
  469. break;
  470. }
  471. }
  472. return 0;
  473. errout:
  474. return err;
  475. }
  476. static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  477. {
  478. struct net *net = sock_net(skb->sk);
  479. struct fib_config cfg;
  480. struct fib_table *tb;
  481. int err;
  482. err = rtm_to_fib_config(net, skb, nlh, &cfg);
  483. if (err < 0)
  484. goto errout;
  485. tb = fib_get_table(net, cfg.fc_table);
  486. if (tb == NULL) {
  487. err = -ESRCH;
  488. goto errout;
  489. }
  490. err = fib_table_delete(tb, &cfg);
  491. errout:
  492. return err;
  493. }
  494. static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
  495. {
  496. struct net *net = sock_net(skb->sk);
  497. struct fib_config cfg;
  498. struct fib_table *tb;
  499. int err;
  500. err = rtm_to_fib_config(net, skb, nlh, &cfg);
  501. if (err < 0)
  502. goto errout;
  503. tb = fib_new_table(net, cfg.fc_table);
  504. if (tb == NULL) {
  505. err = -ENOBUFS;
  506. goto errout;
  507. }
  508. err = fib_table_insert(tb, &cfg);
  509. errout:
  510. return err;
  511. }
  512. static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
  513. {
  514. struct net *net = sock_net(skb->sk);
  515. unsigned int h, s_h;
  516. unsigned int e = 0, s_e;
  517. struct fib_table *tb;
  518. struct hlist_node *node;
  519. struct hlist_head *head;
  520. int dumped = 0;
  521. if (nlmsg_len(cb->nlh) >= sizeof(struct rtmsg) &&
  522. ((struct rtmsg *) nlmsg_data(cb->nlh))->rtm_flags & RTM_F_CLONED)
  523. return ip_rt_dump(skb, cb);
  524. s_h = cb->args[0];
  525. s_e = cb->args[1];
  526. for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
  527. e = 0;
  528. head = &net->ipv4.fib_table_hash[h];
  529. hlist_for_each_entry(tb, node, head, tb_hlist) {
  530. if (e < s_e)
  531. goto next;
  532. if (dumped)
  533. memset(&cb->args[2], 0, sizeof(cb->args) -
  534. 2 * sizeof(cb->args[0]));
  535. if (fib_table_dump(tb, skb, cb) < 0)
  536. goto out;
  537. dumped = 1;
  538. next:
  539. e++;
  540. }
  541. }
  542. out:
  543. cb->args[1] = e;
  544. cb->args[0] = h;
  545. return skb->len;
  546. }
  547. /* Prepare and feed intra-kernel routing request.
  548. * Really, it should be netlink message, but :-( netlink
  549. * can be not configured, so that we feed it directly
  550. * to fib engine. It is legal, because all events occur
  551. * only when netlink is already locked.
  552. */
  553. static void fib_magic(int cmd, int type, __be32 dst, int dst_len, struct in_ifaddr *ifa)
  554. {
  555. struct net *net = dev_net(ifa->ifa_dev->dev);
  556. struct fib_table *tb;
  557. struct fib_config cfg = {
  558. .fc_protocol = RTPROT_KERNEL,
  559. .fc_type = type,
  560. .fc_dst = dst,
  561. .fc_dst_len = dst_len,
  562. .fc_prefsrc = ifa->ifa_local,
  563. .fc_oif = ifa->ifa_dev->dev->ifindex,
  564. .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
  565. .fc_nlinfo = {
  566. .nl_net = net,
  567. },
  568. };
  569. if (type == RTN_UNICAST)
  570. tb = fib_new_table(net, RT_TABLE_MAIN);
  571. else
  572. tb = fib_new_table(net, RT_TABLE_LOCAL);
  573. if (tb == NULL)
  574. return;
  575. cfg.fc_table = tb->tb_id;
  576. if (type != RTN_LOCAL)
  577. cfg.fc_scope = RT_SCOPE_LINK;
  578. else
  579. cfg.fc_scope = RT_SCOPE_HOST;
  580. if (cmd == RTM_NEWROUTE)
  581. fib_table_insert(tb, &cfg);
  582. else
  583. fib_table_delete(tb, &cfg);
  584. }
  585. void fib_add_ifaddr(struct in_ifaddr *ifa)
  586. {
  587. struct in_device *in_dev = ifa->ifa_dev;
  588. struct net_device *dev = in_dev->dev;
  589. struct in_ifaddr *prim = ifa;
  590. __be32 mask = ifa->ifa_mask;
  591. __be32 addr = ifa->ifa_local;
  592. __be32 prefix = ifa->ifa_address & mask;
  593. if (ifa->ifa_flags & IFA_F_SECONDARY) {
  594. prim = inet_ifa_byprefix(in_dev, prefix, mask);
  595. if (prim == NULL) {
  596. printk(KERN_WARNING "fib_add_ifaddr: bug: prim == NULL\n");
  597. return;
  598. }
  599. }
  600. fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim);
  601. if (!(dev->flags & IFF_UP))
  602. return;
  603. /* Add broadcast address, if it is explicitly assigned. */
  604. if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF))
  605. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  606. if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags & IFA_F_SECONDARY) &&
  607. (prefix != addr || ifa->ifa_prefixlen < 32)) {
  608. fib_magic(RTM_NEWROUTE,
  609. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  610. prefix, ifa->ifa_prefixlen, prim);
  611. /* Add network specific broadcasts, when it takes a sense */
  612. if (ifa->ifa_prefixlen < 31) {
  613. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32, prim);
  614. fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix | ~mask,
  615. 32, prim);
  616. }
  617. }
  618. }
  619. static void fib_del_ifaddr(struct in_ifaddr *ifa)
  620. {
  621. struct in_device *in_dev = ifa->ifa_dev;
  622. struct net_device *dev = in_dev->dev;
  623. struct in_ifaddr *ifa1;
  624. struct in_ifaddr *prim = ifa;
  625. __be32 brd = ifa->ifa_address | ~ifa->ifa_mask;
  626. __be32 any = ifa->ifa_address & ifa->ifa_mask;
  627. #define LOCAL_OK 1
  628. #define BRD_OK 2
  629. #define BRD0_OK 4
  630. #define BRD1_OK 8
  631. unsigned ok = 0;
  632. if (!(ifa->ifa_flags & IFA_F_SECONDARY))
  633. fib_magic(RTM_DELROUTE,
  634. dev->flags & IFF_LOOPBACK ? RTN_LOCAL : RTN_UNICAST,
  635. any, ifa->ifa_prefixlen, prim);
  636. else {
  637. prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
  638. if (prim == NULL) {
  639. printk(KERN_WARNING "fib_del_ifaddr: bug: prim == NULL\n");
  640. return;
  641. }
  642. }
  643. /* Deletion is more complicated than add.
  644. * We should take care of not to delete too much :-)
  645. *
  646. * Scan address list to be sure that addresses are really gone.
  647. */
  648. for (ifa1 = in_dev->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
  649. if (ifa->ifa_local == ifa1->ifa_local)
  650. ok |= LOCAL_OK;
  651. if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
  652. ok |= BRD_OK;
  653. if (brd == ifa1->ifa_broadcast)
  654. ok |= BRD1_OK;
  655. if (any == ifa1->ifa_broadcast)
  656. ok |= BRD0_OK;
  657. }
  658. if (!(ok & BRD_OK))
  659. fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
  660. if (!(ok & BRD1_OK))
  661. fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, prim);
  662. if (!(ok & BRD0_OK))
  663. fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, prim);
  664. if (!(ok & LOCAL_OK)) {
  665. fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim);
  666. /* Check, that this local address finally disappeared. */
  667. if (inet_addr_type(dev_net(dev), ifa->ifa_local) != RTN_LOCAL) {
  668. /* And the last, but not the least thing.
  669. * We must flush stray FIB entries.
  670. *
  671. * First of all, we scan fib_info list searching
  672. * for stray nexthop entries, then ignite fib_flush.
  673. */
  674. if (fib_sync_down_addr(dev_net(dev), ifa->ifa_local))
  675. fib_flush(dev_net(dev));
  676. }
  677. }
  678. #undef LOCAL_OK
  679. #undef BRD_OK
  680. #undef BRD0_OK
  681. #undef BRD1_OK
  682. }
  683. static void nl_fib_lookup(struct fib_result_nl *frn, struct fib_table *tb)
  684. {
  685. struct fib_result res;
  686. struct flowi fl = {
  687. .mark = frn->fl_mark,
  688. .fl4_dst = frn->fl_addr,
  689. .fl4_tos = frn->fl_tos,
  690. .fl4_scope = frn->fl_scope,
  691. };
  692. #ifdef CONFIG_IP_MULTIPLE_TABLES
  693. res.r = NULL;
  694. #endif
  695. frn->err = -ENOENT;
  696. if (tb) {
  697. local_bh_disable();
  698. frn->tb_id = tb->tb_id;
  699. rcu_read_lock();
  700. frn->err = fib_table_lookup(tb, &fl, &res, FIB_LOOKUP_NOREF);
  701. if (!frn->err) {
  702. frn->prefixlen = res.prefixlen;
  703. frn->nh_sel = res.nh_sel;
  704. frn->type = res.type;
  705. frn->scope = res.scope;
  706. }
  707. rcu_read_unlock();
  708. local_bh_enable();
  709. }
  710. }
  711. static void nl_fib_input(struct sk_buff *skb)
  712. {
  713. struct net *net;
  714. struct fib_result_nl *frn;
  715. struct nlmsghdr *nlh;
  716. struct fib_table *tb;
  717. u32 pid;
  718. net = sock_net(skb->sk);
  719. nlh = nlmsg_hdr(skb);
  720. if (skb->len < NLMSG_SPACE(0) || skb->len < nlh->nlmsg_len ||
  721. nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*frn)))
  722. return;
  723. skb = skb_clone(skb, GFP_KERNEL);
  724. if (skb == NULL)
  725. return;
  726. nlh = nlmsg_hdr(skb);
  727. frn = (struct fib_result_nl *) NLMSG_DATA(nlh);
  728. tb = fib_get_table(net, frn->tb_id_in);
  729. nl_fib_lookup(frn, tb);
  730. pid = NETLINK_CB(skb).pid; /* pid of sending process */
  731. NETLINK_CB(skb).pid = 0; /* from kernel */
  732. NETLINK_CB(skb).dst_group = 0; /* unicast */
  733. netlink_unicast(net->ipv4.fibnl, skb, pid, MSG_DONTWAIT);
  734. }
  735. static int __net_init nl_fib_lookup_init(struct net *net)
  736. {
  737. struct sock *sk;
  738. sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, 0,
  739. nl_fib_input, NULL, THIS_MODULE);
  740. if (sk == NULL)
  741. return -EAFNOSUPPORT;
  742. net->ipv4.fibnl = sk;
  743. return 0;
  744. }
  745. static void nl_fib_lookup_exit(struct net *net)
  746. {
  747. netlink_kernel_release(net->ipv4.fibnl);
  748. net->ipv4.fibnl = NULL;
  749. }
  750. static void fib_disable_ip(struct net_device *dev, int force, int delay)
  751. {
  752. if (fib_sync_down_dev(dev, force))
  753. fib_flush(dev_net(dev));
  754. rt_cache_flush(dev_net(dev), delay);
  755. arp_ifdown(dev);
  756. }
  757. static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
  758. {
  759. struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
  760. struct net_device *dev = ifa->ifa_dev->dev;
  761. switch (event) {
  762. case NETDEV_UP:
  763. fib_add_ifaddr(ifa);
  764. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  765. fib_sync_up(dev);
  766. #endif
  767. rt_cache_flush(dev_net(dev), -1);
  768. break;
  769. case NETDEV_DOWN:
  770. fib_del_ifaddr(ifa);
  771. if (ifa->ifa_dev->ifa_list == NULL) {
  772. /* Last address was deleted from this interface.
  773. * Disable IP.
  774. */
  775. fib_disable_ip(dev, 1, 0);
  776. } else {
  777. rt_cache_flush(dev_net(dev), -1);
  778. }
  779. break;
  780. }
  781. return NOTIFY_DONE;
  782. }
  783. static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
  784. {
  785. struct net_device *dev = ptr;
  786. struct in_device *in_dev = __in_dev_get_rtnl(dev);
  787. if (event == NETDEV_UNREGISTER) {
  788. fib_disable_ip(dev, 2, -1);
  789. return NOTIFY_DONE;
  790. }
  791. if (!in_dev)
  792. return NOTIFY_DONE;
  793. switch (event) {
  794. case NETDEV_UP:
  795. for_ifa(in_dev) {
  796. fib_add_ifaddr(ifa);
  797. } endfor_ifa(in_dev);
  798. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  799. fib_sync_up(dev);
  800. #endif
  801. rt_cache_flush(dev_net(dev), -1);
  802. break;
  803. case NETDEV_DOWN:
  804. fib_disable_ip(dev, 0, 0);
  805. break;
  806. case NETDEV_CHANGEMTU:
  807. case NETDEV_CHANGE:
  808. rt_cache_flush(dev_net(dev), 0);
  809. break;
  810. case NETDEV_UNREGISTER_BATCH:
  811. /* The batch unregister is only called on the first
  812. * device in the list of devices being unregistered.
  813. * Therefore we should not pass dev_net(dev) in here.
  814. */
  815. rt_cache_flush_batch(NULL);
  816. break;
  817. }
  818. return NOTIFY_DONE;
  819. }
  820. static struct notifier_block fib_inetaddr_notifier = {
  821. .notifier_call = fib_inetaddr_event,
  822. };
  823. static struct notifier_block fib_netdev_notifier = {
  824. .notifier_call = fib_netdev_event,
  825. };
  826. static int __net_init ip_fib_net_init(struct net *net)
  827. {
  828. int err;
  829. size_t size = sizeof(struct hlist_head) * FIB_TABLE_HASHSZ;
  830. /* Avoid false sharing : Use at least a full cache line */
  831. size = max_t(size_t, size, L1_CACHE_BYTES);
  832. net->ipv4.fib_table_hash = kzalloc(size, GFP_KERNEL);
  833. if (net->ipv4.fib_table_hash == NULL)
  834. return -ENOMEM;
  835. err = fib4_rules_init(net);
  836. if (err < 0)
  837. goto fail;
  838. return 0;
  839. fail:
  840. kfree(net->ipv4.fib_table_hash);
  841. return err;
  842. }
  843. static void ip_fib_net_exit(struct net *net)
  844. {
  845. unsigned int i;
  846. #ifdef CONFIG_IP_MULTIPLE_TABLES
  847. fib4_rules_exit(net);
  848. #endif
  849. for (i = 0; i < FIB_TABLE_HASHSZ; i++) {
  850. struct fib_table *tb;
  851. struct hlist_head *head;
  852. struct hlist_node *node, *tmp;
  853. head = &net->ipv4.fib_table_hash[i];
  854. hlist_for_each_entry_safe(tb, node, tmp, head, tb_hlist) {
  855. hlist_del(node);
  856. fib_table_flush(tb);
  857. fib_free_table(tb);
  858. }
  859. }
  860. kfree(net->ipv4.fib_table_hash);
  861. }
  862. static int __net_init fib_net_init(struct net *net)
  863. {
  864. int error;
  865. error = ip_fib_net_init(net);
  866. if (error < 0)
  867. goto out;
  868. error = nl_fib_lookup_init(net);
  869. if (error < 0)
  870. goto out_nlfl;
  871. error = fib_proc_init(net);
  872. if (error < 0)
  873. goto out_proc;
  874. out:
  875. return error;
  876. out_proc:
  877. nl_fib_lookup_exit(net);
  878. out_nlfl:
  879. ip_fib_net_exit(net);
  880. goto out;
  881. }
  882. static void __net_exit fib_net_exit(struct net *net)
  883. {
  884. fib_proc_exit(net);
  885. nl_fib_lookup_exit(net);
  886. ip_fib_net_exit(net);
  887. }
  888. static struct pernet_operations fib_net_ops = {
  889. .init = fib_net_init,
  890. .exit = fib_net_exit,
  891. };
  892. void __init ip_fib_init(void)
  893. {
  894. rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL);
  895. rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL);
  896. rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib);
  897. register_pernet_subsys(&fib_net_ops);
  898. register_netdevice_notifier(&fib_netdev_notifier);
  899. register_inetaddr_notifier(&fib_inetaddr_notifier);
  900. fib_trie_init();
  901. }