fib_semantics.c 28 KB

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