fib_frontend.c 28 KB

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