fib_semantics.c 28 KB

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