fib_frontend.c 27 KB

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