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->fib_metrics);
  135. kfree(fi);
  136. }
  137. void free_fib_info(struct fib_info *fi)
  138. {
  139. if (fi->fib_dead == 0) {
  140. pr_warning("Freeing alive fib_info %p\n", fi);
  141. return;
  142. }
  143. change_nexthops(fi) {
  144. if (nexthop_nh->nh_dev)
  145. dev_put(nexthop_nh->nh_dev);
  146. nexthop_nh->nh_dev = NULL;
  147. } endfor_nexthops(fi);
  148. fib_info_cnt--;
  149. release_net(fi->fib_net);
  150. call_rcu(&fi->rcu, free_fib_info_rcu);
  151. }
  152. void fib_release_info(struct fib_info *fi)
  153. {
  154. spin_lock_bh(&fib_info_lock);
  155. if (fi && --fi->fib_treeref == 0) {
  156. hlist_del(&fi->fib_hash);
  157. if (fi->fib_prefsrc)
  158. hlist_del(&fi->fib_lhash);
  159. change_nexthops(fi) {
  160. if (!nexthop_nh->nh_dev)
  161. continue;
  162. hlist_del(&nexthop_nh->nh_hash);
  163. } endfor_nexthops(fi)
  164. fi->fib_dead = 1;
  165. fib_info_put(fi);
  166. }
  167. spin_unlock_bh(&fib_info_lock);
  168. }
  169. static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
  170. {
  171. const struct fib_nh *onh = ofi->fib_nh;
  172. for_nexthops(fi) {
  173. if (nh->nh_oif != onh->nh_oif ||
  174. nh->nh_gw != onh->nh_gw ||
  175. nh->nh_scope != onh->nh_scope ||
  176. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  177. nh->nh_weight != onh->nh_weight ||
  178. #endif
  179. #ifdef CONFIG_IP_ROUTE_CLASSID
  180. nh->nh_tclassid != onh->nh_tclassid ||
  181. #endif
  182. ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD))
  183. return -1;
  184. onh++;
  185. } endfor_nexthops(fi);
  186. return 0;
  187. }
  188. static inline unsigned int fib_devindex_hashfn(unsigned int val)
  189. {
  190. unsigned int mask = DEVINDEX_HASHSIZE - 1;
  191. return (val ^
  192. (val >> DEVINDEX_HASHBITS) ^
  193. (val >> (DEVINDEX_HASHBITS * 2))) & mask;
  194. }
  195. static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
  196. {
  197. unsigned int mask = (fib_hash_size - 1);
  198. unsigned int val = fi->fib_nhs;
  199. val ^= fi->fib_protocol;
  200. val ^= (__force u32)fi->fib_prefsrc;
  201. val ^= fi->fib_priority;
  202. for_nexthops(fi) {
  203. val ^= fib_devindex_hashfn(nh->nh_oif);
  204. } endfor_nexthops(fi)
  205. return (val ^ (val >> 7) ^ (val >> 12)) & mask;
  206. }
  207. static struct fib_info *fib_find_info(const struct fib_info *nfi)
  208. {
  209. struct hlist_head *head;
  210. struct hlist_node *node;
  211. struct fib_info *fi;
  212. unsigned int hash;
  213. hash = fib_info_hashfn(nfi);
  214. head = &fib_info_hash[hash];
  215. hlist_for_each_entry(fi, node, head, fib_hash) {
  216. if (!net_eq(fi->fib_net, nfi->fib_net))
  217. continue;
  218. if (fi->fib_nhs != nfi->fib_nhs)
  219. continue;
  220. if (nfi->fib_protocol == fi->fib_protocol &&
  221. nfi->fib_prefsrc == fi->fib_prefsrc &&
  222. nfi->fib_priority == fi->fib_priority &&
  223. memcmp(nfi->fib_metrics, fi->fib_metrics,
  224. sizeof(fi->fib_metrics)) == 0 &&
  225. ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 &&
  226. (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
  227. return fi;
  228. }
  229. return NULL;
  230. }
  231. /* Check, that the gateway is already configured.
  232. * Used only by redirect accept routine.
  233. */
  234. int ip_fib_check_default(__be32 gw, struct net_device *dev)
  235. {
  236. struct hlist_head *head;
  237. struct hlist_node *node;
  238. struct fib_nh *nh;
  239. unsigned int hash;
  240. spin_lock(&fib_info_lock);
  241. hash = fib_devindex_hashfn(dev->ifindex);
  242. head = &fib_info_devhash[hash];
  243. hlist_for_each_entry(nh, node, head, nh_hash) {
  244. if (nh->nh_dev == dev &&
  245. nh->nh_gw == gw &&
  246. !(nh->nh_flags & RTNH_F_DEAD)) {
  247. spin_unlock(&fib_info_lock);
  248. return 0;
  249. }
  250. }
  251. spin_unlock(&fib_info_lock);
  252. return -1;
  253. }
  254. static inline size_t fib_nlmsg_size(struct fib_info *fi)
  255. {
  256. size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
  257. + nla_total_size(4) /* RTA_TABLE */
  258. + nla_total_size(4) /* RTA_DST */
  259. + nla_total_size(4) /* RTA_PRIORITY */
  260. + nla_total_size(4); /* RTA_PREFSRC */
  261. /* space for nested metrics */
  262. payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
  263. if (fi->fib_nhs) {
  264. /* Also handles the special case fib_nhs == 1 */
  265. /* each nexthop is packed in an attribute */
  266. size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
  267. /* may contain flow and gateway attribute */
  268. nhsize += 2 * nla_total_size(4);
  269. /* all nexthops are packed in a nested attribute */
  270. payload += nla_total_size(fi->fib_nhs * nhsize);
  271. }
  272. return payload;
  273. }
  274. void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
  275. int dst_len, u32 tb_id, struct nl_info *info,
  276. unsigned int nlm_flags)
  277. {
  278. struct sk_buff *skb;
  279. u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
  280. int err = -ENOBUFS;
  281. skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
  282. if (skb == NULL)
  283. goto errout;
  284. err = fib_dump_info(skb, info->pid, seq, event, tb_id,
  285. fa->fa_type, fa->fa_scope, key, dst_len,
  286. fa->fa_tos, fa->fa_info, nlm_flags);
  287. if (err < 0) {
  288. /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
  289. WARN_ON(err == -EMSGSIZE);
  290. kfree_skb(skb);
  291. goto errout;
  292. }
  293. rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE,
  294. info->nlh, GFP_KERNEL);
  295. return;
  296. errout:
  297. if (err < 0)
  298. rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
  299. }
  300. /* Return the first fib alias matching TOS with
  301. * priority less than or equal to PRIO.
  302. */
  303. struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio)
  304. {
  305. if (fah) {
  306. struct fib_alias *fa;
  307. list_for_each_entry(fa, fah, fa_list) {
  308. if (fa->fa_tos > tos)
  309. continue;
  310. if (fa->fa_info->fib_priority >= prio ||
  311. fa->fa_tos < tos)
  312. return fa;
  313. }
  314. }
  315. return NULL;
  316. }
  317. int fib_detect_death(struct fib_info *fi, int order,
  318. struct fib_info **last_resort, int *last_idx, int dflt)
  319. {
  320. struct neighbour *n;
  321. int state = NUD_NONE;
  322. n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
  323. if (n) {
  324. state = n->nud_state;
  325. neigh_release(n);
  326. }
  327. if (state == NUD_REACHABLE)
  328. return 0;
  329. if ((state & NUD_VALID) && order != dflt)
  330. return 0;
  331. if ((state & NUD_VALID) ||
  332. (*last_idx < 0 && order > dflt)) {
  333. *last_resort = fi;
  334. *last_idx = order;
  335. }
  336. return 1;
  337. }
  338. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  339. static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining)
  340. {
  341. int nhs = 0;
  342. while (rtnh_ok(rtnh, remaining)) {
  343. nhs++;
  344. rtnh = rtnh_next(rtnh, &remaining);
  345. }
  346. /* leftover implies invalid nexthop configuration, discard it */
  347. return remaining > 0 ? 0 : nhs;
  348. }
  349. static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
  350. int remaining, struct fib_config *cfg)
  351. {
  352. change_nexthops(fi) {
  353. int attrlen;
  354. if (!rtnh_ok(rtnh, remaining))
  355. return -EINVAL;
  356. nexthop_nh->nh_flags =
  357. (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
  358. nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
  359. nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
  360. attrlen = rtnh_attrlen(rtnh);
  361. if (attrlen > 0) {
  362. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  363. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  364. nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0;
  365. #ifdef CONFIG_IP_ROUTE_CLASSID
  366. nla = nla_find(attrs, attrlen, RTA_FLOW);
  367. nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
  368. #endif
  369. }
  370. rtnh = rtnh_next(rtnh, &remaining);
  371. } endfor_nexthops(fi);
  372. return 0;
  373. }
  374. #endif
  375. int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
  376. {
  377. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  378. struct rtnexthop *rtnh;
  379. int remaining;
  380. #endif
  381. if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
  382. return 1;
  383. if (cfg->fc_oif || cfg->fc_gw) {
  384. if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
  385. (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
  386. return 0;
  387. return 1;
  388. }
  389. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  390. if (cfg->fc_mp == NULL)
  391. return 0;
  392. rtnh = cfg->fc_mp;
  393. remaining = cfg->fc_mp_len;
  394. for_nexthops(fi) {
  395. int attrlen;
  396. if (!rtnh_ok(rtnh, remaining))
  397. return -EINVAL;
  398. if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
  399. return 1;
  400. attrlen = rtnh_attrlen(rtnh);
  401. if (attrlen < 0) {
  402. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  403. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  404. if (nla && nla_get_be32(nla) != nh->nh_gw)
  405. return 1;
  406. #ifdef CONFIG_IP_ROUTE_CLASSID
  407. nla = nla_find(attrs, attrlen, RTA_FLOW);
  408. if (nla && nla_get_u32(nla) != nh->nh_tclassid)
  409. return 1;
  410. #endif
  411. }
  412. rtnh = rtnh_next(rtnh, &remaining);
  413. } endfor_nexthops(fi);
  414. #endif
  415. return 0;
  416. }
  417. /*
  418. * Picture
  419. * -------
  420. *
  421. * Semantics of nexthop is very messy by historical reasons.
  422. * We have to take into account, that:
  423. * a) gateway can be actually local interface address,
  424. * so that gatewayed route is direct.
  425. * b) gateway must be on-link address, possibly
  426. * described not by an ifaddr, but also by a direct route.
  427. * c) If both gateway and interface are specified, they should not
  428. * contradict.
  429. * d) If we use tunnel routes, gateway could be not on-link.
  430. *
  431. * Attempt to reconcile all of these (alas, self-contradictory) conditions
  432. * results in pretty ugly and hairy code with obscure logic.
  433. *
  434. * I chose to generalized it instead, so that the size
  435. * of code does not increase practically, but it becomes
  436. * much more general.
  437. * Every prefix is assigned a "scope" value: "host" is local address,
  438. * "link" is direct route,
  439. * [ ... "site" ... "interior" ... ]
  440. * and "universe" is true gateway route with global meaning.
  441. *
  442. * Every prefix refers to a set of "nexthop"s (gw, oif),
  443. * where gw must have narrower scope. This recursion stops
  444. * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
  445. * which means that gw is forced to be on link.
  446. *
  447. * Code is still hairy, but now it is apparently logically
  448. * consistent and very flexible. F.e. as by-product it allows
  449. * to co-exists in peace independent exterior and interior
  450. * routing processes.
  451. *
  452. * Normally it looks as following.
  453. *
  454. * {universe prefix} -> (gw, oif) [scope link]
  455. * |
  456. * |-> {link prefix} -> (gw, oif) [scope local]
  457. * |
  458. * |-> {local prefix} (terminal node)
  459. */
  460. static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
  461. struct fib_nh *nh)
  462. {
  463. int err;
  464. struct net *net;
  465. struct net_device *dev;
  466. net = cfg->fc_nlinfo.nl_net;
  467. if (nh->nh_gw) {
  468. struct fib_result res;
  469. if (nh->nh_flags & RTNH_F_ONLINK) {
  470. if (cfg->fc_scope >= RT_SCOPE_LINK)
  471. return -EINVAL;
  472. if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST)
  473. return -EINVAL;
  474. dev = __dev_get_by_index(net, nh->nh_oif);
  475. if (!dev)
  476. return -ENODEV;
  477. if (!(dev->flags & IFF_UP))
  478. return -ENETDOWN;
  479. nh->nh_dev = dev;
  480. dev_hold(dev);
  481. nh->nh_scope = RT_SCOPE_LINK;
  482. return 0;
  483. }
  484. rcu_read_lock();
  485. {
  486. struct flowi fl = {
  487. .fl4_dst = nh->nh_gw,
  488. .fl4_scope = cfg->fc_scope + 1,
  489. .oif = nh->nh_oif,
  490. };
  491. /* It is not necessary, but requires a bit of thinking */
  492. if (fl.fl4_scope < RT_SCOPE_LINK)
  493. fl.fl4_scope = RT_SCOPE_LINK;
  494. err = fib_lookup(net, &fl, &res);
  495. if (err) {
  496. rcu_read_unlock();
  497. return err;
  498. }
  499. }
  500. err = -EINVAL;
  501. if (res.type != RTN_UNICAST && res.type != RTN_LOCAL)
  502. goto out;
  503. nh->nh_scope = res.scope;
  504. nh->nh_oif = FIB_RES_OIF(res);
  505. nh->nh_dev = dev = FIB_RES_DEV(res);
  506. if (!dev)
  507. goto out;
  508. dev_hold(dev);
  509. err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
  510. } else {
  511. struct in_device *in_dev;
  512. if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK))
  513. return -EINVAL;
  514. rcu_read_lock();
  515. err = -ENODEV;
  516. in_dev = inetdev_by_index(net, nh->nh_oif);
  517. if (in_dev == NULL)
  518. goto out;
  519. err = -ENETDOWN;
  520. if (!(in_dev->dev->flags & IFF_UP))
  521. goto out;
  522. nh->nh_dev = in_dev->dev;
  523. dev_hold(nh->nh_dev);
  524. nh->nh_scope = RT_SCOPE_HOST;
  525. err = 0;
  526. }
  527. out:
  528. rcu_read_unlock();
  529. return err;
  530. }
  531. static inline unsigned int fib_laddr_hashfn(__be32 val)
  532. {
  533. unsigned int mask = (fib_hash_size - 1);
  534. return ((__force u32)val ^
  535. ((__force u32)val >> 7) ^
  536. ((__force u32)val >> 14)) & mask;
  537. }
  538. static struct hlist_head *fib_hash_alloc(int bytes)
  539. {
  540. if (bytes <= PAGE_SIZE)
  541. return kzalloc(bytes, GFP_KERNEL);
  542. else
  543. return (struct hlist_head *)
  544. __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  545. get_order(bytes));
  546. }
  547. static void fib_hash_free(struct hlist_head *hash, int bytes)
  548. {
  549. if (!hash)
  550. return;
  551. if (bytes <= PAGE_SIZE)
  552. kfree(hash);
  553. else
  554. free_pages((unsigned long) hash, get_order(bytes));
  555. }
  556. static void fib_hash_move(struct hlist_head *new_info_hash,
  557. struct hlist_head *new_laddrhash,
  558. unsigned int new_size)
  559. {
  560. struct hlist_head *old_info_hash, *old_laddrhash;
  561. unsigned int old_size = fib_hash_size;
  562. unsigned int i, bytes;
  563. spin_lock_bh(&fib_info_lock);
  564. old_info_hash = fib_info_hash;
  565. old_laddrhash = fib_info_laddrhash;
  566. fib_hash_size = new_size;
  567. for (i = 0; i < old_size; i++) {
  568. struct hlist_head *head = &fib_info_hash[i];
  569. struct hlist_node *node, *n;
  570. struct fib_info *fi;
  571. hlist_for_each_entry_safe(fi, node, n, head, fib_hash) {
  572. struct hlist_head *dest;
  573. unsigned int new_hash;
  574. hlist_del(&fi->fib_hash);
  575. new_hash = fib_info_hashfn(fi);
  576. dest = &new_info_hash[new_hash];
  577. hlist_add_head(&fi->fib_hash, dest);
  578. }
  579. }
  580. fib_info_hash = new_info_hash;
  581. for (i = 0; i < old_size; i++) {
  582. struct hlist_head *lhead = &fib_info_laddrhash[i];
  583. struct hlist_node *node, *n;
  584. struct fib_info *fi;
  585. hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) {
  586. struct hlist_head *ldest;
  587. unsigned int new_hash;
  588. hlist_del(&fi->fib_lhash);
  589. new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
  590. ldest = &new_laddrhash[new_hash];
  591. hlist_add_head(&fi->fib_lhash, ldest);
  592. }
  593. }
  594. fib_info_laddrhash = new_laddrhash;
  595. spin_unlock_bh(&fib_info_lock);
  596. bytes = old_size * sizeof(struct hlist_head *);
  597. fib_hash_free(old_info_hash, bytes);
  598. fib_hash_free(old_laddrhash, bytes);
  599. }
  600. struct fib_info *fib_create_info(struct fib_config *cfg)
  601. {
  602. int err;
  603. struct fib_info *fi = NULL;
  604. struct fib_info *ofi;
  605. int nhs = 1;
  606. struct net *net = cfg->fc_nlinfo.nl_net;
  607. /* Fast check to catch the most weird cases */
  608. if (fib_props[cfg->fc_type].scope > cfg->fc_scope)
  609. goto err_inval;
  610. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  611. if (cfg->fc_mp) {
  612. nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len);
  613. if (nhs == 0)
  614. goto err_inval;
  615. }
  616. #endif
  617. err = -ENOBUFS;
  618. if (fib_info_cnt >= fib_hash_size) {
  619. unsigned int new_size = fib_hash_size << 1;
  620. struct hlist_head *new_info_hash;
  621. struct hlist_head *new_laddrhash;
  622. unsigned int bytes;
  623. if (!new_size)
  624. new_size = 1;
  625. bytes = new_size * sizeof(struct hlist_head *);
  626. new_info_hash = fib_hash_alloc(bytes);
  627. new_laddrhash = fib_hash_alloc(bytes);
  628. if (!new_info_hash || !new_laddrhash) {
  629. fib_hash_free(new_info_hash, bytes);
  630. fib_hash_free(new_laddrhash, bytes);
  631. } else
  632. fib_hash_move(new_info_hash, new_laddrhash, new_size);
  633. if (!fib_hash_size)
  634. goto failure;
  635. }
  636. fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
  637. if (fi == NULL)
  638. goto failure;
  639. fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
  640. if (!fi->fib_metrics)
  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