fib_frontend.c 25 KB

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