fib_frontend.c 27 KB

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