fib_frontend.c 25 KB

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