fib_semantics.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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: semantics.
  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 <asm/uaccess.h>
  16. #include <asm/system.h>
  17. #include <linux/bitops.h>
  18. #include <linux/types.h>
  19. #include <linux/kernel.h>
  20. #include <linux/jiffies.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_arp.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/init.h>
  34. #include <linux/slab.h>
  35. #include <net/arp.h>
  36. #include <net/ip.h>
  37. #include <net/protocol.h>
  38. #include <net/route.h>
  39. #include <net/tcp.h>
  40. #include <net/sock.h>
  41. #include <net/ip_fib.h>
  42. #include <net/netlink.h>
  43. #include <net/nexthop.h>
  44. #include "fib_lookup.h"
  45. static DEFINE_SPINLOCK(fib_info_lock);
  46. static struct hlist_head *fib_info_hash;
  47. static struct hlist_head *fib_info_laddrhash;
  48. static unsigned int fib_hash_size;
  49. static unsigned int fib_info_cnt;
  50. #define DEVINDEX_HASHBITS 8
  51. #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
  52. static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
  53. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  54. static DEFINE_SPINLOCK(fib_multipath_lock);
  55. #define for_nexthops(fi) { \
  56. int nhsel; const struct fib_nh *nh; \
  57. for (nhsel = 0, nh = (fi)->fib_nh; \
  58. nhsel < (fi)->fib_nhs; \
  59. nh++, nhsel++)
  60. #define change_nexthops(fi) { \
  61. int nhsel; struct fib_nh *nexthop_nh; \
  62. for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
  63. nhsel < (fi)->fib_nhs; \
  64. nexthop_nh++, nhsel++)
  65. #else /* CONFIG_IP_ROUTE_MULTIPATH */
  66. /* Hope, that gcc will optimize it to get rid of dummy loop */
  67. #define for_nexthops(fi) { \
  68. int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
  69. for (nhsel = 0; nhsel < 1; nhsel++)
  70. #define change_nexthops(fi) { \
  71. int nhsel; \
  72. struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
  73. for (nhsel = 0; nhsel < 1; nhsel++)
  74. #endif /* CONFIG_IP_ROUTE_MULTIPATH */
  75. #define endfor_nexthops(fi) }
  76. static const struct
  77. {
  78. int error;
  79. u8 scope;
  80. } fib_props[RTN_MAX + 1] = {
  81. [RTN_UNSPEC] = {
  82. .error = 0,
  83. .scope = RT_SCOPE_NOWHERE,
  84. },
  85. [RTN_UNICAST] = {
  86. .error = 0,
  87. .scope = RT_SCOPE_UNIVERSE,
  88. },
  89. [RTN_LOCAL] = {
  90. .error = 0,
  91. .scope = RT_SCOPE_HOST,
  92. },
  93. [RTN_BROADCAST] = {
  94. .error = 0,
  95. .scope = RT_SCOPE_LINK,
  96. },
  97. [RTN_ANYCAST] = {
  98. .error = 0,
  99. .scope = RT_SCOPE_LINK,
  100. },
  101. [RTN_MULTICAST] = {
  102. .error = 0,
  103. .scope = RT_SCOPE_UNIVERSE,
  104. },
  105. [RTN_BLACKHOLE] = {
  106. .error = -EINVAL,
  107. .scope = RT_SCOPE_UNIVERSE,
  108. },
  109. [RTN_UNREACHABLE] = {
  110. .error = -EHOSTUNREACH,
  111. .scope = RT_SCOPE_UNIVERSE,
  112. },
  113. [RTN_PROHIBIT] = {
  114. .error = -EACCES,
  115. .scope = RT_SCOPE_UNIVERSE,
  116. },
  117. [RTN_THROW] = {
  118. .error = -EAGAIN,
  119. .scope = RT_SCOPE_UNIVERSE,
  120. },
  121. [RTN_NAT] = {
  122. .error = -EINVAL,
  123. .scope = RT_SCOPE_NOWHERE,
  124. },
  125. [RTN_XRESOLVE] = {
  126. .error = -EINVAL,
  127. .scope = RT_SCOPE_NOWHERE,
  128. },
  129. };
  130. /* Release a nexthop info record */
  131. static void free_fib_info_rcu(struct rcu_head *head)
  132. {
  133. struct fib_info *fi = container_of(head, struct fib_info, rcu);
  134. kfree(fi);
  135. }
  136. void free_fib_info(struct fib_info *fi)
  137. {
  138. if (fi->fib_dead == 0) {
  139. pr_warning("Freeing alive fib_info %p\n", fi);
  140. return;
  141. }
  142. change_nexthops(fi) {
  143. if (nexthop_nh->nh_dev)
  144. dev_put(nexthop_nh->nh_dev);
  145. nexthop_nh->nh_dev = NULL;
  146. } endfor_nexthops(fi);
  147. fib_info_cnt--;
  148. release_net(fi->fib_net);
  149. call_rcu(&fi->rcu, free_fib_info_rcu);
  150. }
  151. void fib_release_info(struct fib_info *fi)
  152. {
  153. spin_lock_bh(&fib_info_lock);
  154. if (fi && --fi->fib_treeref == 0) {
  155. hlist_del(&fi->fib_hash);
  156. if (fi->fib_prefsrc)
  157. hlist_del(&fi->fib_lhash);
  158. change_nexthops(fi) {
  159. if (!nexthop_nh->nh_dev)
  160. continue;
  161. hlist_del(&nexthop_nh->nh_hash);
  162. } endfor_nexthops(fi)
  163. fi->fib_dead = 1;
  164. fib_info_put(fi);
  165. }
  166. spin_unlock_bh(&fib_info_lock);
  167. }
  168. static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
  169. {
  170. const struct fib_nh *onh = ofi->fib_nh;
  171. for_nexthops(fi) {
  172. if (nh->nh_oif != onh->nh_oif ||
  173. nh->nh_gw != onh->nh_gw ||
  174. nh->nh_scope != onh->nh_scope ||
  175. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  176. nh->nh_weight != onh->nh_weight ||
  177. #endif
  178. #ifdef CONFIG_IP_ROUTE_CLASSID
  179. nh->nh_tclassid != onh->nh_tclassid ||
  180. #endif
  181. ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD))
  182. return -1;
  183. onh++;
  184. } endfor_nexthops(fi);
  185. return 0;
  186. }
  187. static inline unsigned int fib_devindex_hashfn(unsigned int val)
  188. {
  189. unsigned int mask = DEVINDEX_HASHSIZE - 1;
  190. return (val ^
  191. (val >> DEVINDEX_HASHBITS) ^
  192. (val >> (DEVINDEX_HASHBITS * 2))) & mask;
  193. }
  194. static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
  195. {
  196. unsigned int mask = (fib_hash_size - 1);
  197. unsigned int val = fi->fib_nhs;
  198. val ^= fi->fib_protocol;
  199. val ^= (__force u32)fi->fib_prefsrc;
  200. val ^= fi->fib_priority;
  201. for_nexthops(fi) {
  202. val ^= fib_devindex_hashfn(nh->nh_oif);
  203. } endfor_nexthops(fi)
  204. return (val ^ (val >> 7) ^ (val >> 12)) & mask;
  205. }
  206. static struct fib_info *fib_find_info(const struct fib_info *nfi)
  207. {
  208. struct hlist_head *head;
  209. struct hlist_node *node;
  210. struct fib_info *fi;
  211. unsigned int hash;
  212. hash = fib_info_hashfn(nfi);
  213. head = &fib_info_hash[hash];
  214. hlist_for_each_entry(fi, node, head, fib_hash) {
  215. if (!net_eq(fi->fib_net, nfi->fib_net))
  216. continue;
  217. if (fi->fib_nhs != nfi->fib_nhs)
  218. continue;
  219. if (nfi->fib_protocol == fi->fib_protocol &&
  220. nfi->fib_prefsrc == fi->fib_prefsrc &&
  221. nfi->fib_priority == fi->fib_priority &&
  222. memcmp(nfi->fib_metrics, fi->fib_metrics,
  223. sizeof(fi->fib_metrics)) == 0 &&
  224. ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 &&
  225. (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
  226. return fi;
  227. }
  228. return NULL;
  229. }
  230. /* Check, that the gateway is already configured.
  231. * Used only by redirect accept routine.
  232. */
  233. int ip_fib_check_default(__be32 gw, struct net_device *dev)
  234. {
  235. struct hlist_head *head;
  236. struct hlist_node *node;
  237. struct fib_nh *nh;
  238. unsigned int hash;
  239. spin_lock(&fib_info_lock);
  240. hash = fib_devindex_hashfn(dev->ifindex);
  241. head = &fib_info_devhash[hash];
  242. hlist_for_each_entry(nh, node, head, nh_hash) {
  243. if (nh->nh_dev == dev &&
  244. nh->nh_gw == gw &&
  245. !(nh->nh_flags & RTNH_F_DEAD)) {
  246. spin_unlock(&fib_info_lock);
  247. return 0;
  248. }
  249. }
  250. spin_unlock(&fib_info_lock);
  251. return -1;
  252. }
  253. static inline size_t fib_nlmsg_size(struct fib_info *fi)
  254. {
  255. size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
  256. + nla_total_size(4) /* RTA_TABLE */
  257. + nla_total_size(4) /* RTA_DST */
  258. + nla_total_size(4) /* RTA_PRIORITY */
  259. + nla_total_size(4); /* RTA_PREFSRC */
  260. /* space for nested metrics */
  261. payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
  262. if (fi->fib_nhs) {
  263. /* Also handles the special case fib_nhs == 1 */
  264. /* each nexthop is packed in an attribute */
  265. size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
  266. /* may contain flow and gateway attribute */
  267. nhsize += 2 * nla_total_size(4);
  268. /* all nexthops are packed in a nested attribute */
  269. payload += nla_total_size(fi->fib_nhs * nhsize);
  270. }
  271. return payload;
  272. }
  273. void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
  274. int dst_len, u32 tb_id, struct nl_info *info,
  275. unsigned int nlm_flags)
  276. {
  277. struct sk_buff *skb;
  278. u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
  279. int err = -ENOBUFS;
  280. skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
  281. if (skb == NULL)
  282. goto errout;
  283. err = fib_dump_info(skb, info->pid, seq, event, tb_id,
  284. fa->fa_type, fa->fa_scope, key, dst_len,
  285. fa->fa_tos, fa->fa_info, nlm_flags);
  286. if (err < 0) {
  287. /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
  288. WARN_ON(err == -EMSGSIZE);
  289. kfree_skb(skb);
  290. goto errout;
  291. }
  292. rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE,
  293. info->nlh, GFP_KERNEL);
  294. return;
  295. errout:
  296. if (err < 0)
  297. rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
  298. }
  299. /* Return the first fib alias matching TOS with
  300. * priority less than or equal to PRIO.
  301. */
  302. struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio)
  303. {
  304. if (fah) {
  305. struct fib_alias *fa;
  306. list_for_each_entry(fa, fah, fa_list) {
  307. if (fa->fa_tos > tos)
  308. continue;
  309. if (fa->fa_info->fib_priority >= prio ||
  310. fa->fa_tos < tos)
  311. return fa;
  312. }
  313. }
  314. return NULL;
  315. }
  316. int fib_detect_death(struct fib_info *fi, int order,
  317. struct fib_info **last_resort, int *last_idx, int dflt)
  318. {
  319. struct neighbour *n;
  320. int state = NUD_NONE;
  321. n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
  322. if (n) {
  323. state = n->nud_state;
  324. neigh_release(n);
  325. }
  326. if (state == NUD_REACHABLE)
  327. return 0;
  328. if ((state & NUD_VALID) && order != dflt)
  329. return 0;
  330. if ((state & NUD_VALID) ||
  331. (*last_idx < 0 && order > dflt)) {
  332. *last_resort = fi;
  333. *last_idx = order;
  334. }
  335. return 1;
  336. }
  337. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  338. static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining)
  339. {
  340. int nhs = 0;
  341. while (rtnh_ok(rtnh, remaining)) {
  342. nhs++;
  343. rtnh = rtnh_next(rtnh, &remaining);
  344. }
  345. /* leftover implies invalid nexthop configuration, discard it */
  346. return remaining > 0 ? 0 : nhs;
  347. }
  348. static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
  349. int remaining, struct fib_config *cfg)
  350. {
  351. change_nexthops(fi) {
  352. int attrlen;
  353. if (!rtnh_ok(rtnh, remaining))
  354. return -EINVAL;
  355. nexthop_nh->nh_flags =
  356. (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
  357. nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
  358. nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
  359. attrlen = rtnh_attrlen(rtnh);
  360. if (attrlen > 0) {
  361. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  362. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  363. nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0;
  364. #ifdef CONFIG_IP_ROUTE_CLASSID
  365. nla = nla_find(attrs, attrlen, RTA_FLOW);
  366. nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
  367. #endif
  368. }
  369. rtnh = rtnh_next(rtnh, &remaining);
  370. } endfor_nexthops(fi);
  371. return 0;
  372. }
  373. #endif
  374. int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
  375. {
  376. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  377. struct rtnexthop *rtnh;
  378. int remaining;
  379. #endif
  380. if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
  381. return 1;
  382. if (cfg->fc_oif || cfg->fc_gw) {
  383. if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
  384. (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
  385. return 0;
  386. return 1;
  387. }
  388. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  389. if (cfg->fc_mp == NULL)
  390. return 0;
  391. rtnh = cfg->fc_mp;
  392. remaining = cfg->fc_mp_len;
  393. for_nexthops(fi) {
  394. int attrlen;
  395. if (!rtnh_ok(rtnh, remaining))
  396. return -EINVAL;
  397. if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
  398. return 1;
  399. attrlen = rtnh_attrlen(rtnh);
  400. if (attrlen < 0) {
  401. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  402. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  403. if (nla && nla_get_be32(nla) != nh->nh_gw)
  404. return 1;
  405. #ifdef CONFIG_IP_ROUTE_CLASSID
  406. nla = nla_find(attrs, attrlen, RTA_FLOW);
  407. if (nla && nla_get_u32(nla) != nh->nh_tclassid)
  408. return 1;
  409. #endif
  410. }
  411. rtnh = rtnh_next(rtnh, &remaining);
  412. } endfor_nexthops(fi);
  413. #endif
  414. return 0;
  415. }
  416. /*
  417. * Picture
  418. * -------
  419. *
  420. * Semantics of nexthop is very messy by historical reasons.
  421. * We have to take into account, that:
  422. * a) gateway can be actually local interface address,
  423. * so that gatewayed route is direct.
  424. * b) gateway must be on-link address, possibly
  425. * described not by an ifaddr, but also by a direct route.
  426. * c) If both gateway and interface are specified, they should not
  427. * contradict.
  428. * d) If we use tunnel routes, gateway could be not on-link.
  429. *
  430. * Attempt to reconcile all of these (alas, self-contradictory) conditions
  431. * results in pretty ugly and hairy code with obscure logic.
  432. *
  433. * I chose to generalized it instead, so that the size
  434. * of code does not increase practically, but it becomes
  435. * much more general.
  436. * Every prefix is assigned a "scope" value: "host" is local address,
  437. * "link" is direct route,
  438. * [ ... "site" ... "interior" ... ]
  439. * and "universe" is true gateway route with global meaning.
  440. *
  441. * Every prefix refers to a set of "nexthop"s (gw, oif),
  442. * where gw must have narrower scope. This recursion stops
  443. * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
  444. * which means that gw is forced to be on link.
  445. *
  446. * Code is still hairy, but now it is apparently logically
  447. * consistent and very flexible. F.e. as by-product it allows
  448. * to co-exists in peace independent exterior and interior
  449. * routing processes.
  450. *
  451. * Normally it looks as following.
  452. *
  453. * {universe prefix} -> (gw, oif) [scope link]
  454. * |
  455. * |-> {link prefix} -> (gw, oif) [scope local]
  456. * |
  457. * |-> {local prefix} (terminal node)
  458. */
  459. static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
  460. struct fib_nh *nh)
  461. {
  462. int err;
  463. struct net *net;
  464. struct net_device *dev;
  465. net = cfg->fc_nlinfo.nl_net;
  466. if (nh->nh_gw) {
  467. struct fib_result res;
  468. if (nh->nh_flags & RTNH_F_ONLINK) {
  469. if (cfg->fc_scope >= RT_SCOPE_LINK)
  470. return -EINVAL;
  471. if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST)
  472. return -EINVAL;
  473. dev = __dev_get_by_index(net, nh->nh_oif);
  474. if (!dev)
  475. return -ENODEV;
  476. if (!(dev->flags & IFF_UP))
  477. return -ENETDOWN;
  478. nh->nh_dev = dev;
  479. dev_hold(dev);
  480. nh->nh_scope = RT_SCOPE_LINK;
  481. return 0;
  482. }
  483. rcu_read_lock();
  484. {
  485. struct flowi fl = {
  486. .nl_u = {
  487. .ip4_u = {
  488. .daddr = nh->nh_gw,
  489. .scope = cfg->fc_scope + 1,
  490. },
  491. },
  492. .oif = nh->nh_oif,
  493. };
  494. /* It is not necessary, but requires a bit of thinking */
  495. if (fl.fl4_scope < RT_SCOPE_LINK)
  496. fl.fl4_scope = RT_SCOPE_LINK;
  497. err = fib_lookup(net, &fl, &res);
  498. if (err) {
  499. rcu_read_unlock();
  500. return err;
  501. }
  502. }
  503. err = -EINVAL;
  504. if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
  505. goto out;
  506. nh->nh_scope = res.scope;
  507. nh->nh_oif = FIB_RES_OIF(res);
  508. nh->nh_dev = dev = FIB_RES_DEV(res);
  509. if (!dev)
  510. goto out;
  511. dev_hold(dev);
  512. err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
  513. } else {
  514. struct in_device *in_dev;
  515. if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK))
  516. return -EINVAL;
  517. rcu_read_lock();
  518. err = -ENODEV;
  519. in_dev = inetdev_by_index(net, nh->nh_oif);
  520. if (in_dev == NULL)
  521. goto out;
  522. err = -ENETDOWN;
  523. if (!(in_dev->dev->flags & IFF_UP))
  524. goto out;
  525. nh->nh_dev = in_dev->dev;
  526. dev_hold(nh->nh_dev);
  527. nh->nh_scope = RT_SCOPE_HOST;
  528. err = 0;
  529. }
  530. out:
  531. rcu_read_unlock();
  532. return err;
  533. }
  534. static inline unsigned int fib_laddr_hashfn(__be32 val)
  535. {
  536. unsigned int mask = (fib_hash_size - 1);
  537. return ((__force u32)val ^
  538. ((__force u32)val >> 7) ^
  539. ((__force u32)val >> 14)) & mask;
  540. }
  541. static struct hlist_head *fib_hash_alloc(int bytes)
  542. {
  543. if (bytes <= PAGE_SIZE)
  544. return kzalloc(bytes, GFP_KERNEL);
  545. else
  546. return (struct hlist_head *)
  547. __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  548. get_order(bytes));
  549. }
  550. static void fib_hash_free(struct hlist_head *hash, int bytes)
  551. {
  552. if (!hash)
  553. return;
  554. if (bytes <= PAGE_SIZE)
  555. kfree(hash);
  556. else
  557. free_pages((unsigned long) hash, get_order(bytes));
  558. }
  559. static void fib_hash_move(struct hlist_head *new_info_hash,
  560. struct hlist_head *new_laddrhash,
  561. unsigned int new_size)
  562. {
  563. struct hlist_head *old_info_hash, *old_laddrhash;
  564. unsigned int old_size = fib_hash_size;
  565. unsigned int i, bytes;
  566. spin_lock_bh(&fib_info_lock);
  567. old_info_hash = fib_info_hash;
  568. old_laddrhash = fib_info_laddrhash;
  569. fib_hash_size = new_size;
  570. for (i = 0; i < old_size; i++) {
  571. struct hlist_head *head = &fib_info_hash[i];
  572. struct hlist_node *node, *n;
  573. struct fib_info *fi;
  574. hlist_for_each_entry_safe(fi, node, n, head, fib_hash) {
  575. struct hlist_head *dest;
  576. unsigned int new_hash;
  577. hlist_del(&fi->fib_hash);
  578. new_hash = fib_info_hashfn(fi);
  579. dest = &new_info_hash[new_hash];
  580. hlist_add_head(&fi->fib_hash, dest);
  581. }
  582. }
  583. fib_info_hash = new_info_hash;
  584. for (i = 0; i < old_size; i++) {
  585. struct hlist_head *lhead = &fib_info_laddrhash[i];
  586. struct hlist_node *node, *n;
  587. struct fib_info *fi;
  588. hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) {
  589. struct hlist_head *ldest;
  590. unsigned int new_hash;
  591. hlist_del(&fi->fib_lhash);
  592. new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
  593. ldest = &new_laddrhash[new_hash];
  594. hlist_add_head(&fi->fib_lhash, ldest);
  595. }
  596. }
  597. fib_info_laddrhash = new_laddrhash;
  598. spin_unlock_bh(&fib_info_lock);
  599. bytes = old_size * sizeof(struct hlist_head *);
  600. fib_hash_free(old_info_hash, bytes);
  601. fib_hash_free(old_laddrhash, bytes);
  602. }
  603. struct fib_info *fib_create_info(struct fib_config *cfg)
  604. {
  605. int err;
  606. struct fib_info *fi = NULL;
  607. struct fib_info *ofi;
  608. int nhs = 1;
  609. struct net *net = cfg->fc_nlinfo.nl_net;
  610. /* Fast check to catch the most weird cases */
  611. if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
  612. goto err_inval;
  613. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  614. if (cfg->fc_mp) {
  615. nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len);
  616. if (nhs == 0)
  617. goto err_inval;
  618. }
  619. #endif
  620. err = -ENOBUFS;
  621. if (fib_info_cnt >= fib_hash_size) {
  622. unsigned int new_size = fib_hash_size << 1;
  623. struct hlist_head *new_info_hash;
  624. struct hlist_head *new_laddrhash;
  625. unsigned int bytes;
  626. if (!new_size)
  627. new_size = 1;
  628. bytes = new_size * sizeof(struct hlist_head *);
  629. new_info_hash = fib_hash_alloc(bytes);
  630. new_laddrhash = fib_hash_alloc(bytes);
  631. if (!new_info_hash || !new_laddrhash) {
  632. fib_hash_free(new_info_hash, bytes);
  633. fib_hash_free(new_laddrhash, bytes);
  634. } else
  635. fib_hash_move(new_info_hash, new_laddrhash, new_size);
  636. if (!fib_hash_size)
  637. goto failure;
  638. }
  639. fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
  640. if (fi == NULL)
  641. goto failure;
  642. fib_info_cnt++;
  643. fi->fib_net = hold_net(net);
  644. fi->fib_protocol = cfg->fc_protocol;
  645. fi->fib_flags = cfg->fc_flags;
  646. fi->fib_priority = cfg->fc_priority;
  647. fi->fib_prefsrc = cfg->fc_prefsrc;
  648. fi->fib_nhs = nhs;
  649. change_nexthops(fi) {
  650. nexthop_nh->nh_parent = fi;
  651. } endfor_nexthops(fi)
  652. if (cfg->fc_mx) {
  653. struct nlattr *nla;
  654. int remaining;
  655. nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
  656. int type = nla_type(nla);
  657. if (type) {
  658. if (type > RTAX_MAX)
  659. goto err_inval;
  660. fi->fib_metrics[type - 1] = nla_get_u32(nla);
  661. }
  662. }
  663. }
  664. if (cfg->fc_mp) {
  665. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  666. err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg);
  667. if (err != 0)
  668. goto failure;
  669. if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif)
  670. goto err_inval;
  671. if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw)
  672. goto err_inval;
  673. #ifdef CONFIG_IP_ROUTE_CLASSID
  674. if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow)
  675. goto err_inval;
  676. #endif
  677. #else
  678. goto err_inval;
  679. #endif
  680. } else {
  681. struct fib_nh *nh = fi->fib_nh;
  682. nh->nh_oif = cfg->fc_oif;
  683. nh->nh_gw = cfg->fc_gw;
  684. nh->nh_flags = cfg->fc_flags;
  685. #ifdef CONFIG_IP_ROUTE_CLASSID
  686. nh->nh_tclassid = cfg->fc_flow;
  687. #endif
  688. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  689. nh->nh_weight = 1;
  690. #endif
  691. }
  692. if (fib_props[cfg->fc_type].error) {
  693. if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp)
  694. goto err_inval;
  695. goto link_it;
  696. }
  697. if (cfg->fc_scope > RT_SCOPE_HOST)
  698. goto err_inval;
  699. if (cfg->fc_scope == RT_SCOPE_HOST) {
  700. struct fib_nh *nh = fi->fib_nh;
  701. /* Local address is added. */
  702. if (nhs != 1 || nh->nh_gw)
  703. goto err_inval;
  704. nh->nh_scope = RT_SCOPE_NOWHERE;
  705. nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
  706. err = -ENODEV;
  707. if (nh->nh_dev == NULL)
  708. goto failure;
  709. } else {
  710. change_nexthops(fi) {
  711. err = fib_check_nh(cfg, fi, nexthop_nh);
  712. if (err != 0)
  713. goto failure;
  714. } endfor_nexthops(fi)
  715. }
  716. if (fi->fib_prefsrc) {
  717. if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
  718. fi->fib_prefsrc != cfg->fc_dst)
  719. if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL)
  720. goto err_inval;
  721. }
  722. link_it:
  723. ofi = fib_find_info(fi);
  724. if (ofi) {
  725. fi->fib_dead = 1;
  726. free_fib_info(fi);
  727. ofi->fib_treeref++;
  728. return ofi;
  729. }
  730. fi->fib_treeref++;
  731. atomic_inc(&fi->fib_clntref);
  732. spin_lock_bh(&fib_info_lock);
  733. hlist_add_head(&fi->fib_hash,
  734. &fib_info_hash[fib_info_hashfn(fi)]);
  735. if (fi->fib_prefsrc) {
  736. struct hlist_head *head;
  737. head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
  738. hlist_add_head(&fi->fib_lhash, head);
  739. }
  740. change_nexthops(fi) {
  741. struct hlist_head *head;
  742. unsigned int hash;
  743. if (!nexthop_nh->nh_dev)
  744. continue;
  745. hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
  746. head = &fib_info_devhash[hash];
  747. hlist_add_head(&nexthop_nh->nh_hash, head);
  748. } endfor_nexthops(fi)
  749. spin_unlock_bh(&fib_info_lock);
  750. return fi;
  751. err_inval:
  752. err = -EINVAL;
  753. failure:
  754. if (fi) {
  755. fi->fib_dead = 1;
  756. free_fib_info(fi);
  757. }
  758. return ERR_PTR(err);
  759. }
  760. /* Note! fib_semantic_match intentionally uses RCU list functions. */
  761. int fib_semantic_match(struct list_head *head, const struct flowi *flp,
  762. struct fib_result *res, int prefixlen, int fib_flags)
  763. {
  764. struct fib_alias *fa;
  765. int nh_sel = 0;
  766. list_for_each_entry_rcu(fa, head, fa_list) {
  767. int err;
  768. if (fa->fa_tos &&
  769. fa->fa_tos != flp->fl4_tos)
  770. continue;
  771. if (fa->fa_scope < flp->fl4_scope)
  772. continue;
  773. fib_alias_accessed(fa);
  774. err = fib_props[fa->fa_type].error;
  775. if (err == 0) {
  776. struct fib_info *fi = fa->fa_info;
  777. if (fi->fib_flags & RTNH_F_DEAD)
  778. continue;
  779. switch (fa->fa_type) {
  780. case RTN_UNICAST:
  781. case RTN_LOCAL:
  782. case RTN_BROADCAST:
  783. case RTN_ANYCAST:
  784. case RTN_MULTICAST:
  785. for_nexthops(fi) {
  786. if (nh->nh_flags & RTNH_F_DEAD)
  787. continue;
  788. if (!flp->oif || flp->oif == nh->nh_oif)
  789. break;
  790. }
  791. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  792. if (nhsel < fi->fib_nhs) {
  793. nh_sel = nhsel;
  794. goto out_fill_res;
  795. }
  796. #else
  797. if (nhsel < 1)
  798. goto out_fill_res;
  799. #endif
  800. endfor_nexthops(fi);
  801. continue;
  802. default:
  803. pr_warning("fib_semantic_match bad type %#x\n",
  804. fa->fa_type);
  805. return -EINVAL;
  806. }
  807. }
  808. return err;
  809. }
  810. return 1;
  811. out_fill_res:
  812. res->prefixlen = prefixlen;
  813. res->nh_sel = nh_sel;
  814. res->type = fa->fa_type;
  815. res->scope = fa->fa_scope;
  816. res->fi = fa->fa_info;
  817. if (!(fib_flags & FIB_LOOKUP_NOREF))
  818. atomic_inc(&res->fi->fib_clntref);
  819. return 0;
  820. }
  821. /* Find appropriate source address to this destination */
  822. __be32 __fib_res_prefsrc(struct fib_result *res)
  823. {
  824. return inet_select_addr(FIB_RES_DEV(*res), FIB_RES_GW(*res), res->scope);
  825. }
  826. int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
  827. u32 tb_id, u8 type, u8 scope, __be32 dst, int dst_len, u8 tos,
  828. struct fib_info *fi, unsigned int flags)
  829. {
  830. struct nlmsghdr *nlh;
  831. struct rtmsg *rtm;
  832. nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags);
  833. if (nlh == NULL)
  834. return -EMSGSIZE;
  835. rtm = nlmsg_data(nlh);
  836. rtm->rtm_family = AF_INET;
  837. rtm->rtm_dst_len = dst_len;
  838. rtm->rtm_src_len = 0;
  839. rtm->rtm_tos = tos;
  840. if (tb_id < 256)
  841. rtm->rtm_table = tb_id;
  842. else
  843. rtm->rtm_table = RT_TABLE_COMPAT;
  844. NLA_PUT_U32(skb, RTA_TABLE, tb_id);
  845. rtm->rtm_type = type;
  846. rtm->rtm_flags = fi->fib_flags;
  847. rtm->rtm_scope = scope;
  848. rtm->rtm_protocol = fi->fib_protocol;
  849. if (rtm->rtm_dst_len)
  850. NLA_PUT_BE32(skb, RTA_DST, dst);
  851. if (fi->fib_priority)
  852. NLA_PUT_U32(skb, RTA_PRIORITY, fi->fib_priority);
  853. if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
  854. goto nla_put_failure;
  855. if (fi->fib_prefsrc)
  856. NLA_PUT_BE32(skb, RTA_PREFSRC, fi->fib_prefsrc);
  857. if (fi->fib_nhs == 1) {
  858. if (fi->fib_nh->nh_gw)
  859. NLA_PUT_BE32(skb, RTA_GATEWAY, fi->fib_nh->nh_gw);
  860. if (fi->fib_nh->nh_oif)
  861. NLA_PUT_U32(skb, RTA_OIF, fi->fib_nh->nh_oif);
  862. #ifdef CONFIG_IP_ROUTE_CLASSID
  863. if (fi->fib_nh[0].nh_tclassid)
  864. NLA_PUT_U32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid);
  865. #endif
  866. }
  867. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  868. if (fi->fib_nhs > 1) {
  869. struct rtnexthop *rtnh;
  870. struct nlattr *mp;
  871. mp = nla_nest_start(skb, RTA_MULTIPATH);
  872. if (mp == NULL)
  873. goto nla_put_failure;
  874. for_nexthops(fi) {
  875. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  876. if (rtnh == NULL)
  877. goto nla_put_failure;
  878. rtnh->rtnh_flags = nh->nh_flags & 0xFF;
  879. rtnh->rtnh_hops = nh->nh_weight - 1;
  880. rtnh->rtnh_ifindex = nh->nh_oif;
  881. if (nh->nh_gw)
  882. NLA_PUT_BE32(skb, RTA_GATEWAY, nh->nh_gw);
  883. #ifdef CONFIG_IP_ROUTE_CLASSID
  884. if (nh->nh_tclassid)
  885. NLA_PUT_U32(skb, RTA_FLOW, nh->nh_tclassid);
  886. #endif
  887. /* length of rtnetlink header + attributes */
  888. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
  889. } endfor_nexthops(fi);
  890. nla_nest_end(skb, mp);
  891. }
  892. #endif
  893. return nlmsg_end(skb, nlh);
  894. nla_put_failure:
  895. nlmsg_cancel(skb, nlh);
  896. return -EMSGSIZE;
  897. }
  898. /*
  899. * Update FIB if:
  900. * - local address disappeared -> we must delete all the entries
  901. * referring to it.
  902. * - device went down -> we must shutdown all nexthops going via it.
  903. */
  904. int fib_sync_down_addr(struct net *net, __be32 local)
  905. {
  906. int ret = 0;
  907. unsigned int hash = fib_laddr_hashfn(local);
  908. struct hlist_head *head = &fib_info_laddrhash[hash];
  909. struct hlist_node *node;
  910. struct fib_info *fi;
  911. if (fib_info_laddrhash == NULL || local == 0)
  912. return 0;
  913. hlist_for_each_entry(fi, node, head, fib_lhash) {
  914. if (!net_eq(fi->fib_net, net))
  915. continue;
  916. if (fi->fib_prefsrc == local) {
  917. fi->fib_flags |= RTNH_F_DEAD;
  918. ret++;
  919. }
  920. }
  921. return ret;
  922. }
  923. int fib_sync_down_dev(struct net_device *dev, int force)
  924. {
  925. int ret = 0;
  926. int scope = RT_SCOPE_NOWHERE;
  927. struct fib_info *prev_fi = NULL;
  928. unsigned int hash = fib_devindex_hashfn(dev->ifindex);
  929. struct hlist_head *head = &fib_info_devhash[hash];
  930. struct hlist_node *node;
  931. struct fib_nh *nh;
  932. if (force)
  933. scope = -1;
  934. hlist_for_each_entry(nh, node, head, nh_hash) {
  935. struct fib_info *fi = nh->nh_parent;
  936. int dead;
  937. BUG_ON(!fi->fib_nhs);
  938. if (nh->nh_dev != dev || fi == prev_fi)
  939. continue;
  940. prev_fi = fi;
  941. dead = 0;
  942. change_nexthops(fi) {
  943. if (nexthop_nh->nh_flags & RTNH_F_DEAD)
  944. dead++;
  945. else if (nexthop_nh->nh_dev == dev &&
  946. nexthop_nh->nh_scope != scope) {
  947. nexthop_nh->nh_flags |= RTNH_F_DEAD;
  948. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  949. spin_lock_bh(&fib_multipath_lock);
  950. fi->fib_power -= nexthop_nh->nh_power;
  951. nexthop_nh->nh_power = 0;
  952. spin_unlock_bh(&fib_multipath_lock);
  953. #endif
  954. dead++;
  955. }
  956. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  957. if (force > 1 && nexthop_nh->nh_dev == dev) {
  958. dead = fi->fib_nhs;
  959. break;
  960. }
  961. #endif
  962. } endfor_nexthops(fi)
  963. if (dead == fi->fib_nhs) {
  964. fi->fib_flags |= RTNH_F_DEAD;
  965. ret++;
  966. }
  967. }
  968. return ret;
  969. }
  970. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  971. /*
  972. * Dead device goes up. We wake up dead nexthops.
  973. * It takes sense only on multipath routes.
  974. */
  975. int fib_sync_up(struct net_device *dev)
  976. {
  977. struct fib_info *prev_fi;
  978. unsigned int hash;
  979. struct hlist_head *head;
  980. struct hlist_node *node;
  981. struct fib_nh *nh;
  982. int ret;
  983. if (!(dev->flags & IFF_UP))
  984. return 0;
  985. prev_fi = NULL;
  986. hash = fib_devindex_hashfn(dev->ifindex);
  987. head = &fib_info_devhash[hash];
  988. ret = 0;
  989. hlist_for_each_entry(nh, node, head, nh_hash) {
  990. struct fib_info *fi = nh->nh_parent;
  991. int alive;
  992. BUG_ON(!fi->fib_nhs);
  993. if (nh->nh_dev != dev || fi == prev_fi)
  994. continue;
  995. prev_fi = fi;
  996. alive = 0;
  997. change_nexthops(fi) {
  998. if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
  999. alive++;
  1000. continue;
  1001. }
  1002. if (nexthop_nh->nh_dev == NULL ||
  1003. !(nexthop_nh->nh_dev->flags & IFF_UP))
  1004. continue;
  1005. if (nexthop_nh->nh_dev != dev ||
  1006. !__in_dev_get_rtnl(dev))
  1007. continue;
  1008. alive++;
  1009. spin_lock_bh(&fib_multipath_lock);
  1010. nexthop_nh->nh_power = 0;
  1011. nexthop_nh->nh_flags &= ~RTNH_F_DEAD;
  1012. spin_unlock_bh(&fib_multipath_lock);
  1013. } endfor_nexthops(fi)
  1014. if (alive > 0) {
  1015. fi->fib_flags &= ~RTNH_F_DEAD;
  1016. ret++;
  1017. }
  1018. }
  1019. return ret;
  1020. }
  1021. /*
  1022. * The algorithm is suboptimal, but it provides really
  1023. * fair weighted route distribution.
  1024. */
  1025. void fib_select_multipath(const struct flowi *flp, struct fib_result *res)
  1026. {
  1027. struct fib_info *fi = res->fi;
  1028. int w;
  1029. spin_lock_bh(&fib_multipath_lock);
  1030. if (fi->fib_power <= 0) {
  1031. int power = 0;
  1032. change_nexthops(fi) {
  1033. if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) {
  1034. power += nexthop_nh->nh_weight;
  1035. nexthop_nh->nh_power = nexthop_nh->nh_weight;
  1036. }
  1037. } endfor_nexthops(fi);
  1038. fi->fib_power = power;
  1039. if (power <= 0) {
  1040. spin_unlock_bh(&fib_multipath_lock);
  1041. /* Race condition: route has just become dead. */
  1042. res->nh_sel = 0;
  1043. return;
  1044. }
  1045. }
  1046. /* w should be random number [0..fi->fib_power-1],
  1047. * it is pretty bad approximation.
  1048. */
  1049. w = jiffies % fi->fib_power;
  1050. change_nexthops(fi) {
  1051. if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) &&
  1052. nexthop_nh->nh_power) {
  1053. w -= nexthop_nh->nh_power;
  1054. if (w <= 0) {
  1055. nexthop_nh->nh_power--;
  1056. fi->fib_power--;
  1057. res->nh_sel = nhsel;
  1058. spin_unlock_bh(&fib_multipath_lock);
  1059. return;
  1060. }
  1061. }
  1062. } endfor_nexthops(fi);
  1063. /* Race condition: route has just become dead. */
  1064. res->nh_sel = 0;
  1065. spin_unlock_bh(&fib_multipath_lock);
  1066. }
  1067. #endif