fib_semantics.c 28 KB

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