fib_frontend.c 27 KB

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