fib_semantics.c 28 KB

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