fib_frontend.c 25 KB

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