fib_semantics.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  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 <linux/uaccess.h>
  16. #include <linux/bitops.h>
  17. #include <linux/types.h>
  18. #include <linux/kernel.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/mm.h>
  21. #include <linux/string.h>
  22. #include <linux/socket.h>
  23. #include <linux/sockios.h>
  24. #include <linux/errno.h>
  25. #include <linux/in.h>
  26. #include <linux/inet.h>
  27. #include <linux/inetdevice.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/if_arp.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/init.h>
  33. #include <linux/slab.h>
  34. #include <linux/netlink.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 <net/lwtunnel.h>
  45. #include <net/fib_notifier.h>
  46. #include "fib_lookup.h"
  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_info_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. #define for_nexthops(fi) { \
  57. int nhsel; const struct fib_nh *nh; \
  58. for (nhsel = 0, nh = (fi)->fib_nh; \
  59. nhsel < (fi)->fib_nhs; \
  60. nh++, nhsel++)
  61. #define change_nexthops(fi) { \
  62. int nhsel; struct fib_nh *nexthop_nh; \
  63. for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
  64. nhsel < (fi)->fib_nhs; \
  65. nexthop_nh++, nhsel++)
  66. #else /* CONFIG_IP_ROUTE_MULTIPATH */
  67. /* Hope, that gcc will optimize it to get rid of dummy loop */
  68. #define for_nexthops(fi) { \
  69. int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
  70. for (nhsel = 0; nhsel < 1; nhsel++)
  71. #define change_nexthops(fi) { \
  72. int nhsel; \
  73. struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
  74. for (nhsel = 0; nhsel < 1; nhsel++)
  75. #endif /* CONFIG_IP_ROUTE_MULTIPATH */
  76. #define endfor_nexthops(fi) }
  77. const struct fib_prop fib_props[RTN_MAX + 1] = {
  78. [RTN_UNSPEC] = {
  79. .error = 0,
  80. .scope = RT_SCOPE_NOWHERE,
  81. },
  82. [RTN_UNICAST] = {
  83. .error = 0,
  84. .scope = RT_SCOPE_UNIVERSE,
  85. },
  86. [RTN_LOCAL] = {
  87. .error = 0,
  88. .scope = RT_SCOPE_HOST,
  89. },
  90. [RTN_BROADCAST] = {
  91. .error = 0,
  92. .scope = RT_SCOPE_LINK,
  93. },
  94. [RTN_ANYCAST] = {
  95. .error = 0,
  96. .scope = RT_SCOPE_LINK,
  97. },
  98. [RTN_MULTICAST] = {
  99. .error = 0,
  100. .scope = RT_SCOPE_UNIVERSE,
  101. },
  102. [RTN_BLACKHOLE] = {
  103. .error = -EINVAL,
  104. .scope = RT_SCOPE_UNIVERSE,
  105. },
  106. [RTN_UNREACHABLE] = {
  107. .error = -EHOSTUNREACH,
  108. .scope = RT_SCOPE_UNIVERSE,
  109. },
  110. [RTN_PROHIBIT] = {
  111. .error = -EACCES,
  112. .scope = RT_SCOPE_UNIVERSE,
  113. },
  114. [RTN_THROW] = {
  115. .error = -EAGAIN,
  116. .scope = RT_SCOPE_UNIVERSE,
  117. },
  118. [RTN_NAT] = {
  119. .error = -EINVAL,
  120. .scope = RT_SCOPE_NOWHERE,
  121. },
  122. [RTN_XRESOLVE] = {
  123. .error = -EINVAL,
  124. .scope = RT_SCOPE_NOWHERE,
  125. },
  126. };
  127. static void rt_fibinfo_free(struct rtable __rcu **rtp)
  128. {
  129. struct rtable *rt = rcu_dereference_protected(*rtp, 1);
  130. if (!rt)
  131. return;
  132. /* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
  133. * because we waited an RCU grace period before calling
  134. * free_fib_info_rcu()
  135. */
  136. dst_dev_put(&rt->dst);
  137. dst_release_immediate(&rt->dst);
  138. }
  139. static void free_nh_exceptions(struct fib_nh *nh)
  140. {
  141. struct fnhe_hash_bucket *hash;
  142. int i;
  143. hash = rcu_dereference_protected(nh->nh_exceptions, 1);
  144. if (!hash)
  145. return;
  146. for (i = 0; i < FNHE_HASH_SIZE; i++) {
  147. struct fib_nh_exception *fnhe;
  148. fnhe = rcu_dereference_protected(hash[i].chain, 1);
  149. while (fnhe) {
  150. struct fib_nh_exception *next;
  151. next = rcu_dereference_protected(fnhe->fnhe_next, 1);
  152. rt_fibinfo_free(&fnhe->fnhe_rth_input);
  153. rt_fibinfo_free(&fnhe->fnhe_rth_output);
  154. kfree(fnhe);
  155. fnhe = next;
  156. }
  157. }
  158. kfree(hash);
  159. }
  160. static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
  161. {
  162. int cpu;
  163. if (!rtp)
  164. return;
  165. for_each_possible_cpu(cpu) {
  166. struct rtable *rt;
  167. rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
  168. if (rt) {
  169. dst_dev_put(&rt->dst);
  170. dst_release_immediate(&rt->dst);
  171. }
  172. }
  173. free_percpu(rtp);
  174. }
  175. /* Release a nexthop info record */
  176. static void free_fib_info_rcu(struct rcu_head *head)
  177. {
  178. struct fib_info *fi = container_of(head, struct fib_info, rcu);
  179. struct dst_metrics *m;
  180. change_nexthops(fi) {
  181. if (nexthop_nh->nh_dev)
  182. dev_put(nexthop_nh->nh_dev);
  183. lwtstate_put(nexthop_nh->nh_lwtstate);
  184. free_nh_exceptions(nexthop_nh);
  185. rt_fibinfo_free_cpus(nexthop_nh->nh_pcpu_rth_output);
  186. rt_fibinfo_free(&nexthop_nh->nh_rth_input);
  187. } endfor_nexthops(fi);
  188. m = fi->fib_metrics;
  189. if (m != &dst_default_metrics && refcount_dec_and_test(&m->refcnt))
  190. kfree(m);
  191. kfree(fi);
  192. }
  193. void free_fib_info(struct fib_info *fi)
  194. {
  195. if (fi->fib_dead == 0) {
  196. pr_warn("Freeing alive fib_info %p\n", fi);
  197. return;
  198. }
  199. fib_info_cnt--;
  200. #ifdef CONFIG_IP_ROUTE_CLASSID
  201. change_nexthops(fi) {
  202. if (nexthop_nh->nh_tclassid)
  203. fi->fib_net->ipv4.fib_num_tclassid_users--;
  204. } endfor_nexthops(fi);
  205. #endif
  206. call_rcu(&fi->rcu, free_fib_info_rcu);
  207. }
  208. EXPORT_SYMBOL_GPL(free_fib_info);
  209. void fib_release_info(struct fib_info *fi)
  210. {
  211. spin_lock_bh(&fib_info_lock);
  212. if (fi && --fi->fib_treeref == 0) {
  213. hlist_del(&fi->fib_hash);
  214. if (fi->fib_prefsrc)
  215. hlist_del(&fi->fib_lhash);
  216. change_nexthops(fi) {
  217. if (!nexthop_nh->nh_dev)
  218. continue;
  219. hlist_del(&nexthop_nh->nh_hash);
  220. } endfor_nexthops(fi)
  221. fi->fib_dead = 1;
  222. fib_info_put(fi);
  223. }
  224. spin_unlock_bh(&fib_info_lock);
  225. }
  226. static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
  227. {
  228. const struct fib_nh *onh = ofi->fib_nh;
  229. for_nexthops(fi) {
  230. if (nh->nh_oif != onh->nh_oif ||
  231. nh->nh_gw != onh->nh_gw ||
  232. nh->nh_scope != onh->nh_scope ||
  233. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  234. nh->nh_weight != onh->nh_weight ||
  235. #endif
  236. #ifdef CONFIG_IP_ROUTE_CLASSID
  237. nh->nh_tclassid != onh->nh_tclassid ||
  238. #endif
  239. lwtunnel_cmp_encap(nh->nh_lwtstate, onh->nh_lwtstate) ||
  240. ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_COMPARE_MASK))
  241. return -1;
  242. onh++;
  243. } endfor_nexthops(fi);
  244. return 0;
  245. }
  246. static inline unsigned int fib_devindex_hashfn(unsigned int val)
  247. {
  248. unsigned int mask = DEVINDEX_HASHSIZE - 1;
  249. return (val ^
  250. (val >> DEVINDEX_HASHBITS) ^
  251. (val >> (DEVINDEX_HASHBITS * 2))) & mask;
  252. }
  253. static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
  254. {
  255. unsigned int mask = (fib_info_hash_size - 1);
  256. unsigned int val = fi->fib_nhs;
  257. val ^= (fi->fib_protocol << 8) | fi->fib_scope;
  258. val ^= (__force u32)fi->fib_prefsrc;
  259. val ^= fi->fib_priority;
  260. for_nexthops(fi) {
  261. val ^= fib_devindex_hashfn(nh->nh_oif);
  262. } endfor_nexthops(fi)
  263. return (val ^ (val >> 7) ^ (val >> 12)) & mask;
  264. }
  265. static struct fib_info *fib_find_info(const struct fib_info *nfi)
  266. {
  267. struct hlist_head *head;
  268. struct fib_info *fi;
  269. unsigned int hash;
  270. hash = fib_info_hashfn(nfi);
  271. head = &fib_info_hash[hash];
  272. hlist_for_each_entry(fi, head, fib_hash) {
  273. if (!net_eq(fi->fib_net, nfi->fib_net))
  274. continue;
  275. if (fi->fib_nhs != nfi->fib_nhs)
  276. continue;
  277. if (nfi->fib_protocol == fi->fib_protocol &&
  278. nfi->fib_scope == fi->fib_scope &&
  279. nfi->fib_prefsrc == fi->fib_prefsrc &&
  280. nfi->fib_priority == fi->fib_priority &&
  281. nfi->fib_type == fi->fib_type &&
  282. memcmp(nfi->fib_metrics, fi->fib_metrics,
  283. sizeof(u32) * RTAX_MAX) == 0 &&
  284. !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
  285. (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
  286. return fi;
  287. }
  288. return NULL;
  289. }
  290. /* Check, that the gateway is already configured.
  291. * Used only by redirect accept routine.
  292. */
  293. int ip_fib_check_default(__be32 gw, struct net_device *dev)
  294. {
  295. struct hlist_head *head;
  296. struct fib_nh *nh;
  297. unsigned int hash;
  298. spin_lock(&fib_info_lock);
  299. hash = fib_devindex_hashfn(dev->ifindex);
  300. head = &fib_info_devhash[hash];
  301. hlist_for_each_entry(nh, head, nh_hash) {
  302. if (nh->nh_dev == dev &&
  303. nh->nh_gw == gw &&
  304. !(nh->nh_flags & RTNH_F_DEAD)) {
  305. spin_unlock(&fib_info_lock);
  306. return 0;
  307. }
  308. }
  309. spin_unlock(&fib_info_lock);
  310. return -1;
  311. }
  312. static inline size_t fib_nlmsg_size(struct fib_info *fi)
  313. {
  314. size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
  315. + nla_total_size(4) /* RTA_TABLE */
  316. + nla_total_size(4) /* RTA_DST */
  317. + nla_total_size(4) /* RTA_PRIORITY */
  318. + nla_total_size(4) /* RTA_PREFSRC */
  319. + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
  320. /* space for nested metrics */
  321. payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
  322. if (fi->fib_nhs) {
  323. size_t nh_encapsize = 0;
  324. /* Also handles the special case fib_nhs == 1 */
  325. /* each nexthop is packed in an attribute */
  326. size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
  327. /* may contain flow and gateway attribute */
  328. nhsize += 2 * nla_total_size(4);
  329. /* grab encap info */
  330. for_nexthops(fi) {
  331. if (nh->nh_lwtstate) {
  332. /* RTA_ENCAP_TYPE */
  333. nh_encapsize += lwtunnel_get_encap_size(
  334. nh->nh_lwtstate);
  335. /* RTA_ENCAP */
  336. nh_encapsize += nla_total_size(2);
  337. }
  338. } endfor_nexthops(fi);
  339. /* all nexthops are packed in a nested attribute */
  340. payload += nla_total_size((fi->fib_nhs * nhsize) +
  341. nh_encapsize);
  342. }
  343. return payload;
  344. }
  345. void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
  346. int dst_len, u32 tb_id, const struct nl_info *info,
  347. unsigned int nlm_flags)
  348. {
  349. struct sk_buff *skb;
  350. u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
  351. int err = -ENOBUFS;
  352. skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
  353. if (!skb)
  354. goto errout;
  355. err = fib_dump_info(skb, info->portid, seq, event, tb_id,
  356. fa->fa_type, key, dst_len,
  357. fa->fa_tos, fa->fa_info, nlm_flags);
  358. if (err < 0) {
  359. /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
  360. WARN_ON(err == -EMSGSIZE);
  361. kfree_skb(skb);
  362. goto errout;
  363. }
  364. rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
  365. info->nlh, GFP_KERNEL);
  366. return;
  367. errout:
  368. if (err < 0)
  369. rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
  370. }
  371. static int fib_detect_death(struct fib_info *fi, int order,
  372. struct fib_info **last_resort, int *last_idx,
  373. int dflt)
  374. {
  375. struct neighbour *n;
  376. int state = NUD_NONE;
  377. n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
  378. if (n) {
  379. state = n->nud_state;
  380. neigh_release(n);
  381. } else {
  382. return 0;
  383. }
  384. if (state == NUD_REACHABLE)
  385. return 0;
  386. if ((state & NUD_VALID) && order != dflt)
  387. return 0;
  388. if ((state & NUD_VALID) ||
  389. (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
  390. *last_resort = fi;
  391. *last_idx = order;
  392. }
  393. return 1;
  394. }
  395. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  396. static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
  397. struct netlink_ext_ack *extack)
  398. {
  399. int nhs = 0;
  400. while (rtnh_ok(rtnh, remaining)) {
  401. nhs++;
  402. rtnh = rtnh_next(rtnh, &remaining);
  403. }
  404. /* leftover implies invalid nexthop configuration, discard it */
  405. if (remaining > 0) {
  406. NL_SET_ERR_MSG(extack,
  407. "Invalid nexthop configuration - extra data after nexthops");
  408. nhs = 0;
  409. }
  410. return nhs;
  411. }
  412. static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
  413. int remaining, struct fib_config *cfg,
  414. struct netlink_ext_ack *extack)
  415. {
  416. int ret;
  417. change_nexthops(fi) {
  418. int attrlen;
  419. if (!rtnh_ok(rtnh, remaining)) {
  420. NL_SET_ERR_MSG(extack,
  421. "Invalid nexthop configuration - extra data after nexthop");
  422. return -EINVAL;
  423. }
  424. if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
  425. NL_SET_ERR_MSG(extack,
  426. "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
  427. return -EINVAL;
  428. }
  429. nexthop_nh->nh_flags =
  430. (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
  431. nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
  432. nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
  433. attrlen = rtnh_attrlen(rtnh);
  434. if (attrlen > 0) {
  435. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  436. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  437. nexthop_nh->nh_gw = nla ? nla_get_in_addr(nla) : 0;
  438. #ifdef CONFIG_IP_ROUTE_CLASSID
  439. nla = nla_find(attrs, attrlen, RTA_FLOW);
  440. nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
  441. if (nexthop_nh->nh_tclassid)
  442. fi->fib_net->ipv4.fib_num_tclassid_users++;
  443. #endif
  444. nla = nla_find(attrs, attrlen, RTA_ENCAP);
  445. if (nla) {
  446. struct lwtunnel_state *lwtstate;
  447. struct nlattr *nla_entype;
  448. nla_entype = nla_find(attrs, attrlen,
  449. RTA_ENCAP_TYPE);
  450. if (!nla_entype) {
  451. NL_SET_BAD_ATTR(extack, nla);
  452. NL_SET_ERR_MSG(extack,
  453. "Encap type is missing");
  454. goto err_inval;
  455. }
  456. ret = lwtunnel_build_state(nla_get_u16(
  457. nla_entype),
  458. nla, AF_INET, cfg,
  459. &lwtstate, extack);
  460. if (ret)
  461. goto errout;
  462. nexthop_nh->nh_lwtstate =
  463. lwtstate_get(lwtstate);
  464. }
  465. }
  466. rtnh = rtnh_next(rtnh, &remaining);
  467. } endfor_nexthops(fi);
  468. return 0;
  469. err_inval:
  470. ret = -EINVAL;
  471. errout:
  472. return ret;
  473. }
  474. static void fib_rebalance(struct fib_info *fi)
  475. {
  476. int total;
  477. int w;
  478. struct in_device *in_dev;
  479. if (fi->fib_nhs < 2)
  480. return;
  481. total = 0;
  482. for_nexthops(fi) {
  483. if (nh->nh_flags & RTNH_F_DEAD)
  484. continue;
  485. in_dev = __in_dev_get_rtnl(nh->nh_dev);
  486. if (in_dev &&
  487. IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
  488. nh->nh_flags & RTNH_F_LINKDOWN)
  489. continue;
  490. total += nh->nh_weight;
  491. } endfor_nexthops(fi);
  492. w = 0;
  493. change_nexthops(fi) {
  494. int upper_bound;
  495. in_dev = __in_dev_get_rtnl(nexthop_nh->nh_dev);
  496. if (nexthop_nh->nh_flags & RTNH_F_DEAD) {
  497. upper_bound = -1;
  498. } else if (in_dev &&
  499. IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
  500. nexthop_nh->nh_flags & RTNH_F_LINKDOWN) {
  501. upper_bound = -1;
  502. } else {
  503. w += nexthop_nh->nh_weight;
  504. upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
  505. total) - 1;
  506. }
  507. atomic_set(&nexthop_nh->nh_upper_bound, upper_bound);
  508. } endfor_nexthops(fi);
  509. }
  510. static inline void fib_add_weight(struct fib_info *fi,
  511. const struct fib_nh *nh)
  512. {
  513. fi->fib_weight += nh->nh_weight;
  514. }
  515. #else /* CONFIG_IP_ROUTE_MULTIPATH */
  516. #define fib_rebalance(fi) do { } while (0)
  517. #define fib_add_weight(fi, nh) do { } while (0)
  518. #endif /* CONFIG_IP_ROUTE_MULTIPATH */
  519. static int fib_encap_match(u16 encap_type,
  520. struct nlattr *encap,
  521. const struct fib_nh *nh,
  522. const struct fib_config *cfg,
  523. struct netlink_ext_ack *extack)
  524. {
  525. struct lwtunnel_state *lwtstate;
  526. int ret, result = 0;
  527. if (encap_type == LWTUNNEL_ENCAP_NONE)
  528. return 0;
  529. ret = lwtunnel_build_state(encap_type, encap, AF_INET,
  530. cfg, &lwtstate, extack);
  531. if (!ret) {
  532. result = lwtunnel_cmp_encap(lwtstate, nh->nh_lwtstate);
  533. lwtstate_free(lwtstate);
  534. }
  535. return result;
  536. }
  537. int fib_nh_match(struct fib_config *cfg, struct fib_info *fi,
  538. struct netlink_ext_ack *extack)
  539. {
  540. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  541. struct rtnexthop *rtnh;
  542. int remaining;
  543. #endif
  544. if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
  545. return 1;
  546. if (cfg->fc_oif || cfg->fc_gw) {
  547. if (cfg->fc_encap) {
  548. if (fib_encap_match(cfg->fc_encap_type, cfg->fc_encap,
  549. fi->fib_nh, cfg, extack))
  550. return 1;
  551. }
  552. if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
  553. (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
  554. return 0;
  555. return 1;
  556. }
  557. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  558. if (!cfg->fc_mp)
  559. return 0;
  560. rtnh = cfg->fc_mp;
  561. remaining = cfg->fc_mp_len;
  562. for_nexthops(fi) {
  563. int attrlen;
  564. if (!rtnh_ok(rtnh, remaining))
  565. return -EINVAL;
  566. if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
  567. return 1;
  568. attrlen = rtnh_attrlen(rtnh);
  569. if (attrlen > 0) {
  570. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  571. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  572. if (nla && nla_get_in_addr(nla) != nh->nh_gw)
  573. return 1;
  574. #ifdef CONFIG_IP_ROUTE_CLASSID
  575. nla = nla_find(attrs, attrlen, RTA_FLOW);
  576. if (nla && nla_get_u32(nla) != nh->nh_tclassid)
  577. return 1;
  578. #endif
  579. }
  580. rtnh = rtnh_next(rtnh, &remaining);
  581. } endfor_nexthops(fi);
  582. #endif
  583. return 0;
  584. }
  585. bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
  586. {
  587. struct nlattr *nla;
  588. int remaining;
  589. if (!cfg->fc_mx)
  590. return true;
  591. nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
  592. int type = nla_type(nla);
  593. u32 val;
  594. if (!type)
  595. continue;
  596. if (type > RTAX_MAX)
  597. return false;
  598. if (type == RTAX_CC_ALGO) {
  599. char tmp[TCP_CA_NAME_MAX];
  600. bool ecn_ca = false;
  601. nla_strlcpy(tmp, nla, sizeof(tmp));
  602. val = tcp_ca_get_key_by_name(tmp, &ecn_ca);
  603. } else {
  604. val = nla_get_u32(nla);
  605. }
  606. if (fi->fib_metrics->metrics[type - 1] != val)
  607. return false;
  608. }
  609. return true;
  610. }
  611. /*
  612. * Picture
  613. * -------
  614. *
  615. * Semantics of nexthop is very messy by historical reasons.
  616. * We have to take into account, that:
  617. * a) gateway can be actually local interface address,
  618. * so that gatewayed route is direct.
  619. * b) gateway must be on-link address, possibly
  620. * described not by an ifaddr, but also by a direct route.
  621. * c) If both gateway and interface are specified, they should not
  622. * contradict.
  623. * d) If we use tunnel routes, gateway could be not on-link.
  624. *
  625. * Attempt to reconcile all of these (alas, self-contradictory) conditions
  626. * results in pretty ugly and hairy code with obscure logic.
  627. *
  628. * I chose to generalized it instead, so that the size
  629. * of code does not increase practically, but it becomes
  630. * much more general.
  631. * Every prefix is assigned a "scope" value: "host" is local address,
  632. * "link" is direct route,
  633. * [ ... "site" ... "interior" ... ]
  634. * and "universe" is true gateway route with global meaning.
  635. *
  636. * Every prefix refers to a set of "nexthop"s (gw, oif),
  637. * where gw must have narrower scope. This recursion stops
  638. * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
  639. * which means that gw is forced to be on link.
  640. *
  641. * Code is still hairy, but now it is apparently logically
  642. * consistent and very flexible. F.e. as by-product it allows
  643. * to co-exists in peace independent exterior and interior
  644. * routing processes.
  645. *
  646. * Normally it looks as following.
  647. *
  648. * {universe prefix} -> (gw, oif) [scope link]
  649. * |
  650. * |-> {link prefix} -> (gw, oif) [scope local]
  651. * |
  652. * |-> {local prefix} (terminal node)
  653. */
  654. static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
  655. struct fib_nh *nh, struct netlink_ext_ack *extack)
  656. {
  657. int err = 0;
  658. struct net *net;
  659. struct net_device *dev;
  660. net = cfg->fc_nlinfo.nl_net;
  661. if (nh->nh_gw) {
  662. struct fib_result res;
  663. if (nh->nh_flags & RTNH_F_ONLINK) {
  664. unsigned int addr_type;
  665. if (cfg->fc_scope >= RT_SCOPE_LINK) {
  666. NL_SET_ERR_MSG(extack,
  667. "Nexthop has invalid scope");
  668. return -EINVAL;
  669. }
  670. dev = __dev_get_by_index(net, nh->nh_oif);
  671. if (!dev)
  672. return -ENODEV;
  673. if (!(dev->flags & IFF_UP)) {
  674. NL_SET_ERR_MSG(extack,
  675. "Nexthop device is not up");
  676. return -ENETDOWN;
  677. }
  678. addr_type = inet_addr_type_dev_table(net, dev, nh->nh_gw);
  679. if (addr_type != RTN_UNICAST) {
  680. NL_SET_ERR_MSG(extack,
  681. "Nexthop has invalid gateway");
  682. return -EINVAL;
  683. }
  684. if (!netif_carrier_ok(dev))
  685. nh->nh_flags |= RTNH_F_LINKDOWN;
  686. nh->nh_dev = dev;
  687. dev_hold(dev);
  688. nh->nh_scope = RT_SCOPE_LINK;
  689. return 0;
  690. }
  691. rcu_read_lock();
  692. {
  693. struct fib_table *tbl = NULL;
  694. struct flowi4 fl4 = {
  695. .daddr = nh->nh_gw,
  696. .flowi4_scope = cfg->fc_scope + 1,
  697. .flowi4_oif = nh->nh_oif,
  698. .flowi4_iif = LOOPBACK_IFINDEX,
  699. };
  700. /* It is not necessary, but requires a bit of thinking */
  701. if (fl4.flowi4_scope < RT_SCOPE_LINK)
  702. fl4.flowi4_scope = RT_SCOPE_LINK;
  703. if (cfg->fc_table)
  704. tbl = fib_get_table(net, cfg->fc_table);
  705. if (tbl)
  706. err = fib_table_lookup(tbl, &fl4, &res,
  707. FIB_LOOKUP_IGNORE_LINKSTATE |
  708. FIB_LOOKUP_NOREF);
  709. /* on error or if no table given do full lookup. This
  710. * is needed for example when nexthops are in the local
  711. * table rather than the given table
  712. */
  713. if (!tbl || err) {
  714. err = fib_lookup(net, &fl4, &res,
  715. FIB_LOOKUP_IGNORE_LINKSTATE);
  716. }
  717. if (err) {
  718. NL_SET_ERR_MSG(extack,
  719. "Nexthop has invalid gateway");
  720. rcu_read_unlock();
  721. return err;
  722. }
  723. }
  724. err = -EINVAL;
  725. if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
  726. NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
  727. goto out;
  728. }
  729. nh->nh_scope = res.scope;
  730. nh->nh_oif = FIB_RES_OIF(res);
  731. nh->nh_dev = dev = FIB_RES_DEV(res);
  732. if (!dev) {
  733. NL_SET_ERR_MSG(extack,
  734. "No egress device for nexthop gateway");
  735. goto out;
  736. }
  737. dev_hold(dev);
  738. if (!netif_carrier_ok(dev))
  739. nh->nh_flags |= RTNH_F_LINKDOWN;
  740. err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
  741. } else {
  742. struct in_device *in_dev;
  743. if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
  744. NL_SET_ERR_MSG(extack,
  745. "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
  746. return -EINVAL;
  747. }
  748. rcu_read_lock();
  749. err = -ENODEV;
  750. in_dev = inetdev_by_index(net, nh->nh_oif);
  751. if (!in_dev)
  752. goto out;
  753. err = -ENETDOWN;
  754. if (!(in_dev->dev->flags & IFF_UP)) {
  755. NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
  756. goto out;
  757. }
  758. nh->nh_dev = in_dev->dev;
  759. dev_hold(nh->nh_dev);
  760. nh->nh_scope = RT_SCOPE_HOST;
  761. if (!netif_carrier_ok(nh->nh_dev))
  762. nh->nh_flags |= RTNH_F_LINKDOWN;
  763. err = 0;
  764. }
  765. out:
  766. rcu_read_unlock();
  767. return err;
  768. }
  769. static inline unsigned int fib_laddr_hashfn(__be32 val)
  770. {
  771. unsigned int mask = (fib_info_hash_size - 1);
  772. return ((__force u32)val ^
  773. ((__force u32)val >> 7) ^
  774. ((__force u32)val >> 14)) & mask;
  775. }
  776. static struct hlist_head *fib_info_hash_alloc(int bytes)
  777. {
  778. if (bytes <= PAGE_SIZE)
  779. return kzalloc(bytes, GFP_KERNEL);
  780. else
  781. return (struct hlist_head *)
  782. __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  783. get_order(bytes));
  784. }
  785. static void fib_info_hash_free(struct hlist_head *hash, int bytes)
  786. {
  787. if (!hash)
  788. return;
  789. if (bytes <= PAGE_SIZE)
  790. kfree(hash);
  791. else
  792. free_pages((unsigned long) hash, get_order(bytes));
  793. }
  794. static void fib_info_hash_move(struct hlist_head *new_info_hash,
  795. struct hlist_head *new_laddrhash,
  796. unsigned int new_size)
  797. {
  798. struct hlist_head *old_info_hash, *old_laddrhash;
  799. unsigned int old_size = fib_info_hash_size;
  800. unsigned int i, bytes;
  801. spin_lock_bh(&fib_info_lock);
  802. old_info_hash = fib_info_hash;
  803. old_laddrhash = fib_info_laddrhash;
  804. fib_info_hash_size = new_size;
  805. for (i = 0; i < old_size; i++) {
  806. struct hlist_head *head = &fib_info_hash[i];
  807. struct hlist_node *n;
  808. struct fib_info *fi;
  809. hlist_for_each_entry_safe(fi, n, head, fib_hash) {
  810. struct hlist_head *dest;
  811. unsigned int new_hash;
  812. new_hash = fib_info_hashfn(fi);
  813. dest = &new_info_hash[new_hash];
  814. hlist_add_head(&fi->fib_hash, dest);
  815. }
  816. }
  817. fib_info_hash = new_info_hash;
  818. for (i = 0; i < old_size; i++) {
  819. struct hlist_head *lhead = &fib_info_laddrhash[i];
  820. struct hlist_node *n;
  821. struct fib_info *fi;
  822. hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
  823. struct hlist_head *ldest;
  824. unsigned int new_hash;
  825. new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
  826. ldest = &new_laddrhash[new_hash];
  827. hlist_add_head(&fi->fib_lhash, ldest);
  828. }
  829. }
  830. fib_info_laddrhash = new_laddrhash;
  831. spin_unlock_bh(&fib_info_lock);
  832. bytes = old_size * sizeof(struct hlist_head *);
  833. fib_info_hash_free(old_info_hash, bytes);
  834. fib_info_hash_free(old_laddrhash, bytes);
  835. }
  836. __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
  837. {
  838. nh->nh_saddr = inet_select_addr(nh->nh_dev,
  839. nh->nh_gw,
  840. nh->nh_parent->fib_scope);
  841. nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
  842. return nh->nh_saddr;
  843. }
  844. static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
  845. {
  846. if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
  847. fib_prefsrc != cfg->fc_dst) {
  848. u32 tb_id = cfg->fc_table;
  849. int rc;
  850. if (tb_id == RT_TABLE_MAIN)
  851. tb_id = RT_TABLE_LOCAL;
  852. rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
  853. fib_prefsrc, tb_id);
  854. if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
  855. rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
  856. fib_prefsrc, RT_TABLE_LOCAL);
  857. }
  858. if (rc != RTN_LOCAL)
  859. return false;
  860. }
  861. return true;
  862. }
  863. static int
  864. fib_convert_metrics(struct fib_info *fi, const struct fib_config *cfg)
  865. {
  866. bool ecn_ca = false;
  867. struct nlattr *nla;
  868. int remaining;
  869. if (!cfg->fc_mx)
  870. return 0;
  871. nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
  872. int type = nla_type(nla);
  873. u32 val;
  874. if (!type)
  875. continue;
  876. if (type > RTAX_MAX)
  877. return -EINVAL;
  878. if (type == RTAX_CC_ALGO) {
  879. char tmp[TCP_CA_NAME_MAX];
  880. nla_strlcpy(tmp, nla, sizeof(tmp));
  881. val = tcp_ca_get_key_by_name(tmp, &ecn_ca);
  882. if (val == TCP_CA_UNSPEC)
  883. return -EINVAL;
  884. } else {
  885. val = nla_get_u32(nla);
  886. }
  887. if (type == RTAX_ADVMSS && val > 65535 - 40)
  888. val = 65535 - 40;
  889. if (type == RTAX_MTU && val > 65535 - 15)
  890. val = 65535 - 15;
  891. if (type == RTAX_HOPLIMIT && val > 255)
  892. val = 255;
  893. if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK))
  894. return -EINVAL;
  895. fi->fib_metrics->metrics[type - 1] = val;
  896. }
  897. if (ecn_ca)
  898. fi->fib_metrics->metrics[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA;
  899. return 0;
  900. }
  901. struct fib_info *fib_create_info(struct fib_config *cfg,
  902. struct netlink_ext_ack *extack)
  903. {
  904. int err;
  905. struct fib_info *fi = NULL;
  906. struct fib_info *ofi;
  907. int nhs = 1;
  908. struct net *net = cfg->fc_nlinfo.nl_net;
  909. if (cfg->fc_type > RTN_MAX)
  910. goto err_inval;
  911. /* Fast check to catch the most weird cases */
  912. if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
  913. NL_SET_ERR_MSG(extack, "Invalid scope");
  914. goto err_inval;
  915. }
  916. if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
  917. NL_SET_ERR_MSG(extack,
  918. "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
  919. goto err_inval;
  920. }
  921. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  922. if (cfg->fc_mp) {
  923. nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
  924. if (nhs == 0)
  925. goto err_inval;
  926. }
  927. #endif
  928. err = -ENOBUFS;
  929. if (fib_info_cnt >= fib_info_hash_size) {
  930. unsigned int new_size = fib_info_hash_size << 1;
  931. struct hlist_head *new_info_hash;
  932. struct hlist_head *new_laddrhash;
  933. unsigned int bytes;
  934. if (!new_size)
  935. new_size = 16;
  936. bytes = new_size * sizeof(struct hlist_head *);
  937. new_info_hash = fib_info_hash_alloc(bytes);
  938. new_laddrhash = fib_info_hash_alloc(bytes);
  939. if (!new_info_hash || !new_laddrhash) {
  940. fib_info_hash_free(new_info_hash, bytes);
  941. fib_info_hash_free(new_laddrhash, bytes);
  942. } else
  943. fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
  944. if (!fib_info_hash_size)
  945. goto failure;
  946. }
  947. fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
  948. if (!fi)
  949. goto failure;
  950. if (cfg->fc_mx) {
  951. fi->fib_metrics = kzalloc(sizeof(*fi->fib_metrics), GFP_KERNEL);
  952. if (unlikely(!fi->fib_metrics)) {
  953. kfree(fi);
  954. return ERR_PTR(err);
  955. }
  956. refcount_set(&fi->fib_metrics->refcnt, 1);
  957. } else {
  958. fi->fib_metrics = (struct dst_metrics *)&dst_default_metrics;
  959. }
  960. fib_info_cnt++;
  961. fi->fib_net = net;
  962. fi->fib_protocol = cfg->fc_protocol;
  963. fi->fib_scope = cfg->fc_scope;
  964. fi->fib_flags = cfg->fc_flags;
  965. fi->fib_priority = cfg->fc_priority;
  966. fi->fib_prefsrc = cfg->fc_prefsrc;
  967. fi->fib_type = cfg->fc_type;
  968. fi->fib_tb_id = cfg->fc_table;
  969. fi->fib_nhs = nhs;
  970. change_nexthops(fi) {
  971. nexthop_nh->nh_parent = fi;
  972. nexthop_nh->nh_pcpu_rth_output = alloc_percpu(struct rtable __rcu *);
  973. if (!nexthop_nh->nh_pcpu_rth_output)
  974. goto failure;
  975. } endfor_nexthops(fi)
  976. err = fib_convert_metrics(fi, cfg);
  977. if (err)
  978. goto failure;
  979. if (cfg->fc_mp) {
  980. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  981. err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg, extack);
  982. if (err != 0)
  983. goto failure;
  984. if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif) {
  985. NL_SET_ERR_MSG(extack,
  986. "Nexthop device index does not match RTA_OIF");
  987. goto err_inval;
  988. }
  989. if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw) {
  990. NL_SET_ERR_MSG(extack,
  991. "Nexthop gateway does not match RTA_GATEWAY");
  992. goto err_inval;
  993. }
  994. #ifdef CONFIG_IP_ROUTE_CLASSID
  995. if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) {
  996. NL_SET_ERR_MSG(extack,
  997. "Nexthop class id does not match RTA_FLOW");
  998. goto err_inval;
  999. }
  1000. #endif
  1001. #else
  1002. NL_SET_ERR_MSG(extack,
  1003. "Multipath support not enabled in kernel");
  1004. goto err_inval;
  1005. #endif
  1006. } else {
  1007. struct fib_nh *nh = fi->fib_nh;
  1008. if (cfg->fc_encap) {
  1009. struct lwtunnel_state *lwtstate;
  1010. if (cfg->fc_encap_type == LWTUNNEL_ENCAP_NONE) {
  1011. NL_SET_ERR_MSG(extack,
  1012. "LWT encap type not specified");
  1013. goto err_inval;
  1014. }
  1015. err = lwtunnel_build_state(cfg->fc_encap_type,
  1016. cfg->fc_encap, AF_INET, cfg,
  1017. &lwtstate, extack);
  1018. if (err)
  1019. goto failure;
  1020. nh->nh_lwtstate = lwtstate_get(lwtstate);
  1021. }
  1022. nh->nh_oif = cfg->fc_oif;
  1023. nh->nh_gw = cfg->fc_gw;
  1024. nh->nh_flags = cfg->fc_flags;
  1025. #ifdef CONFIG_IP_ROUTE_CLASSID
  1026. nh->nh_tclassid = cfg->fc_flow;
  1027. if (nh->nh_tclassid)
  1028. fi->fib_net->ipv4.fib_num_tclassid_users++;
  1029. #endif
  1030. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1031. nh->nh_weight = 1;
  1032. #endif
  1033. }
  1034. if (fib_props[cfg->fc_type].error) {
  1035. if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp) {
  1036. NL_SET_ERR_MSG(extack,
  1037. "Gateway, device and multipath can not be specified for this route type");
  1038. goto err_inval;
  1039. }
  1040. goto link_it;
  1041. } else {
  1042. switch (cfg->fc_type) {
  1043. case RTN_UNICAST:
  1044. case RTN_LOCAL:
  1045. case RTN_BROADCAST:
  1046. case RTN_ANYCAST:
  1047. case RTN_MULTICAST:
  1048. break;
  1049. default:
  1050. NL_SET_ERR_MSG(extack, "Invalid route type");
  1051. goto err_inval;
  1052. }
  1053. }
  1054. if (cfg->fc_scope > RT_SCOPE_HOST) {
  1055. NL_SET_ERR_MSG(extack, "Invalid scope");
  1056. goto err_inval;
  1057. }
  1058. if (cfg->fc_scope == RT_SCOPE_HOST) {
  1059. struct fib_nh *nh = fi->fib_nh;
  1060. /* Local address is added. */
  1061. if (nhs != 1) {
  1062. NL_SET_ERR_MSG(extack,
  1063. "Route with host scope can not have multiple nexthops");
  1064. goto err_inval;
  1065. }
  1066. if (nh->nh_gw) {
  1067. NL_SET_ERR_MSG(extack,
  1068. "Route with host scope can not have a gateway");
  1069. goto err_inval;
  1070. }
  1071. nh->nh_scope = RT_SCOPE_NOWHERE;
  1072. nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
  1073. err = -ENODEV;
  1074. if (!nh->nh_dev)
  1075. goto failure;
  1076. } else {
  1077. int linkdown = 0;
  1078. change_nexthops(fi) {
  1079. err = fib_check_nh(cfg, fi, nexthop_nh, extack);
  1080. if (err != 0)
  1081. goto failure;
  1082. if (nexthop_nh->nh_flags & RTNH_F_LINKDOWN)
  1083. linkdown++;
  1084. } endfor_nexthops(fi)
  1085. if (linkdown == fi->fib_nhs)
  1086. fi->fib_flags |= RTNH_F_LINKDOWN;
  1087. }
  1088. if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
  1089. NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
  1090. goto err_inval;
  1091. }
  1092. change_nexthops(fi) {
  1093. fib_info_update_nh_saddr(net, nexthop_nh);
  1094. fib_add_weight(fi, nexthop_nh);
  1095. } endfor_nexthops(fi)
  1096. fib_rebalance(fi);
  1097. link_it:
  1098. ofi = fib_find_info(fi);
  1099. if (ofi) {
  1100. fi->fib_dead = 1;
  1101. free_fib_info(fi);
  1102. ofi->fib_treeref++;
  1103. return ofi;
  1104. }
  1105. fi->fib_treeref++;
  1106. refcount_set(&fi->fib_clntref, 1);
  1107. spin_lock_bh(&fib_info_lock);
  1108. hlist_add_head(&fi->fib_hash,
  1109. &fib_info_hash[fib_info_hashfn(fi)]);
  1110. if (fi->fib_prefsrc) {
  1111. struct hlist_head *head;
  1112. head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
  1113. hlist_add_head(&fi->fib_lhash, head);
  1114. }
  1115. change_nexthops(fi) {
  1116. struct hlist_head *head;
  1117. unsigned int hash;
  1118. if (!nexthop_nh->nh_dev)
  1119. continue;
  1120. hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
  1121. head = &fib_info_devhash[hash];
  1122. hlist_add_head(&nexthop_nh->nh_hash, head);
  1123. } endfor_nexthops(fi)
  1124. spin_unlock_bh(&fib_info_lock);
  1125. return fi;
  1126. err_inval:
  1127. err = -EINVAL;
  1128. failure:
  1129. if (fi) {
  1130. fi->fib_dead = 1;
  1131. free_fib_info(fi);
  1132. }
  1133. return ERR_PTR(err);
  1134. }
  1135. int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1136. u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
  1137. struct fib_info *fi, unsigned int flags)
  1138. {
  1139. struct nlmsghdr *nlh;
  1140. struct rtmsg *rtm;
  1141. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1142. if (!nlh)
  1143. return -EMSGSIZE;
  1144. rtm = nlmsg_data(nlh);
  1145. rtm->rtm_family = AF_INET;
  1146. rtm->rtm_dst_len = dst_len;
  1147. rtm->rtm_src_len = 0;
  1148. rtm->rtm_tos = tos;
  1149. if (tb_id < 256)
  1150. rtm->rtm_table = tb_id;
  1151. else
  1152. rtm->rtm_table = RT_TABLE_COMPAT;
  1153. if (nla_put_u32(skb, RTA_TABLE, tb_id))
  1154. goto nla_put_failure;
  1155. rtm->rtm_type = type;
  1156. rtm->rtm_flags = fi->fib_flags;
  1157. rtm->rtm_scope = fi->fib_scope;
  1158. rtm->rtm_protocol = fi->fib_protocol;
  1159. if (rtm->rtm_dst_len &&
  1160. nla_put_in_addr(skb, RTA_DST, dst))
  1161. goto nla_put_failure;
  1162. if (fi->fib_priority &&
  1163. nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
  1164. goto nla_put_failure;
  1165. if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
  1166. goto nla_put_failure;
  1167. if (fi->fib_prefsrc &&
  1168. nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
  1169. goto nla_put_failure;
  1170. if (fi->fib_nhs == 1) {
  1171. if (fi->fib_nh->nh_gw &&
  1172. nla_put_in_addr(skb, RTA_GATEWAY, fi->fib_nh->nh_gw))
  1173. goto nla_put_failure;
  1174. if (fi->fib_nh->nh_oif &&
  1175. nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif))
  1176. goto nla_put_failure;
  1177. if (fi->fib_nh->nh_flags & RTNH_F_LINKDOWN) {
  1178. struct in_device *in_dev;
  1179. rcu_read_lock();
  1180. in_dev = __in_dev_get_rcu(fi->fib_nh->nh_dev);
  1181. if (in_dev &&
  1182. IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
  1183. rtm->rtm_flags |= RTNH_F_DEAD;
  1184. rcu_read_unlock();
  1185. }
  1186. if (fi->fib_nh->nh_flags & RTNH_F_OFFLOAD)
  1187. rtm->rtm_flags |= RTNH_F_OFFLOAD;
  1188. #ifdef CONFIG_IP_ROUTE_CLASSID
  1189. if (fi->fib_nh[0].nh_tclassid &&
  1190. nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid))
  1191. goto nla_put_failure;
  1192. #endif
  1193. if (fi->fib_nh->nh_lwtstate &&
  1194. lwtunnel_fill_encap(skb, fi->fib_nh->nh_lwtstate) < 0)
  1195. goto nla_put_failure;
  1196. }
  1197. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1198. if (fi->fib_nhs > 1) {
  1199. struct rtnexthop *rtnh;
  1200. struct nlattr *mp;
  1201. mp = nla_nest_start(skb, RTA_MULTIPATH);
  1202. if (!mp)
  1203. goto nla_put_failure;
  1204. for_nexthops(fi) {
  1205. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1206. if (!rtnh)
  1207. goto nla_put_failure;
  1208. rtnh->rtnh_flags = nh->nh_flags & 0xFF;
  1209. if (nh->nh_flags & RTNH_F_LINKDOWN) {
  1210. struct in_device *in_dev;
  1211. rcu_read_lock();
  1212. in_dev = __in_dev_get_rcu(nh->nh_dev);
  1213. if (in_dev &&
  1214. IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
  1215. rtnh->rtnh_flags |= RTNH_F_DEAD;
  1216. rcu_read_unlock();
  1217. }
  1218. rtnh->rtnh_hops = nh->nh_weight - 1;
  1219. rtnh->rtnh_ifindex = nh->nh_oif;
  1220. if (nh->nh_gw &&
  1221. nla_put_in_addr(skb, RTA_GATEWAY, nh->nh_gw))
  1222. goto nla_put_failure;
  1223. #ifdef CONFIG_IP_ROUTE_CLASSID
  1224. if (nh->nh_tclassid &&
  1225. nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
  1226. goto nla_put_failure;
  1227. #endif
  1228. if (nh->nh_lwtstate &&
  1229. lwtunnel_fill_encap(skb, nh->nh_lwtstate) < 0)
  1230. goto nla_put_failure;
  1231. /* length of rtnetlink header + attributes */
  1232. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
  1233. } endfor_nexthops(fi);
  1234. nla_nest_end(skb, mp);
  1235. }
  1236. #endif
  1237. nlmsg_end(skb, nlh);
  1238. return 0;
  1239. nla_put_failure:
  1240. nlmsg_cancel(skb, nlh);
  1241. return -EMSGSIZE;
  1242. }
  1243. /*
  1244. * Update FIB if:
  1245. * - local address disappeared -> we must delete all the entries
  1246. * referring to it.
  1247. * - device went down -> we must shutdown all nexthops going via it.
  1248. */
  1249. int fib_sync_down_addr(struct net_device *dev, __be32 local)
  1250. {
  1251. int ret = 0;
  1252. unsigned int hash = fib_laddr_hashfn(local);
  1253. struct hlist_head *head = &fib_info_laddrhash[hash];
  1254. struct net *net = dev_net(dev);
  1255. int tb_id = l3mdev_fib_table(dev);
  1256. struct fib_info *fi;
  1257. if (!fib_info_laddrhash || local == 0)
  1258. return 0;
  1259. hlist_for_each_entry(fi, head, fib_lhash) {
  1260. if (!net_eq(fi->fib_net, net) ||
  1261. fi->fib_tb_id != tb_id)
  1262. continue;
  1263. if (fi->fib_prefsrc == local) {
  1264. fi->fib_flags |= RTNH_F_DEAD;
  1265. ret++;
  1266. }
  1267. }
  1268. return ret;
  1269. }
  1270. static int call_fib_nh_notifiers(struct fib_nh *fib_nh,
  1271. enum fib_event_type event_type)
  1272. {
  1273. struct in_device *in_dev = __in_dev_get_rtnl(fib_nh->nh_dev);
  1274. struct fib_nh_notifier_info info = {
  1275. .fib_nh = fib_nh,
  1276. };
  1277. switch (event_type) {
  1278. case FIB_EVENT_NH_ADD:
  1279. if (fib_nh->nh_flags & RTNH_F_DEAD)
  1280. break;
  1281. if (IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
  1282. fib_nh->nh_flags & RTNH_F_LINKDOWN)
  1283. break;
  1284. return call_fib4_notifiers(dev_net(fib_nh->nh_dev), event_type,
  1285. &info.info);
  1286. case FIB_EVENT_NH_DEL:
  1287. if ((in_dev && IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
  1288. fib_nh->nh_flags & RTNH_F_LINKDOWN) ||
  1289. (fib_nh->nh_flags & RTNH_F_DEAD))
  1290. return call_fib4_notifiers(dev_net(fib_nh->nh_dev),
  1291. event_type, &info.info);
  1292. default:
  1293. break;
  1294. }
  1295. return NOTIFY_DONE;
  1296. }
  1297. /* Event force Flags Description
  1298. * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
  1299. * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
  1300. * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
  1301. * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
  1302. */
  1303. int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
  1304. {
  1305. int ret = 0;
  1306. int scope = RT_SCOPE_NOWHERE;
  1307. struct fib_info *prev_fi = NULL;
  1308. unsigned int hash = fib_devindex_hashfn(dev->ifindex);
  1309. struct hlist_head *head = &fib_info_devhash[hash];
  1310. struct fib_nh *nh;
  1311. if (force)
  1312. scope = -1;
  1313. hlist_for_each_entry(nh, head, nh_hash) {
  1314. struct fib_info *fi = nh->nh_parent;
  1315. int dead;
  1316. BUG_ON(!fi->fib_nhs);
  1317. if (nh->nh_dev != dev || fi == prev_fi)
  1318. continue;
  1319. prev_fi = fi;
  1320. dead = 0;
  1321. change_nexthops(fi) {
  1322. if (nexthop_nh->nh_flags & RTNH_F_DEAD)
  1323. dead++;
  1324. else if (nexthop_nh->nh_dev == dev &&
  1325. nexthop_nh->nh_scope != scope) {
  1326. switch (event) {
  1327. case NETDEV_DOWN:
  1328. case NETDEV_UNREGISTER:
  1329. nexthop_nh->nh_flags |= RTNH_F_DEAD;
  1330. /* fall through */
  1331. case NETDEV_CHANGE:
  1332. nexthop_nh->nh_flags |= RTNH_F_LINKDOWN;
  1333. break;
  1334. }
  1335. call_fib_nh_notifiers(nexthop_nh,
  1336. FIB_EVENT_NH_DEL);
  1337. dead++;
  1338. }
  1339. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1340. if (event == NETDEV_UNREGISTER &&
  1341. nexthop_nh->nh_dev == dev) {
  1342. dead = fi->fib_nhs;
  1343. break;
  1344. }
  1345. #endif
  1346. } endfor_nexthops(fi)
  1347. if (dead == fi->fib_nhs) {
  1348. switch (event) {
  1349. case NETDEV_DOWN:
  1350. case NETDEV_UNREGISTER:
  1351. fi->fib_flags |= RTNH_F_DEAD;
  1352. /* fall through */
  1353. case NETDEV_CHANGE:
  1354. fi->fib_flags |= RTNH_F_LINKDOWN;
  1355. break;
  1356. }
  1357. ret++;
  1358. }
  1359. fib_rebalance(fi);
  1360. }
  1361. return ret;
  1362. }
  1363. /* Must be invoked inside of an RCU protected region. */
  1364. static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
  1365. {
  1366. struct fib_info *fi = NULL, *last_resort = NULL;
  1367. struct hlist_head *fa_head = res->fa_head;
  1368. struct fib_table *tb = res->table;
  1369. u8 slen = 32 - res->prefixlen;
  1370. int order = -1, last_idx = -1;
  1371. struct fib_alias *fa, *fa1 = NULL;
  1372. u32 last_prio = res->fi->fib_priority;
  1373. u8 last_tos = 0;
  1374. hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
  1375. struct fib_info *next_fi = fa->fa_info;
  1376. if (fa->fa_slen != slen)
  1377. continue;
  1378. if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
  1379. continue;
  1380. if (fa->tb_id != tb->tb_id)
  1381. continue;
  1382. if (next_fi->fib_priority > last_prio &&
  1383. fa->fa_tos == last_tos) {
  1384. if (last_tos)
  1385. continue;
  1386. break;
  1387. }
  1388. if (next_fi->fib_flags & RTNH_F_DEAD)
  1389. continue;
  1390. last_tos = fa->fa_tos;
  1391. last_prio = next_fi->fib_priority;
  1392. if (next_fi->fib_scope != res->scope ||
  1393. fa->fa_type != RTN_UNICAST)
  1394. continue;
  1395. if (!next_fi->fib_nh[0].nh_gw ||
  1396. next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
  1397. continue;
  1398. fib_alias_accessed(fa);
  1399. if (!fi) {
  1400. if (next_fi != res->fi)
  1401. break;
  1402. fa1 = fa;
  1403. } else if (!fib_detect_death(fi, order, &last_resort,
  1404. &last_idx, fa1->fa_default)) {
  1405. fib_result_assign(res, fi);
  1406. fa1->fa_default = order;
  1407. goto out;
  1408. }
  1409. fi = next_fi;
  1410. order++;
  1411. }
  1412. if (order <= 0 || !fi) {
  1413. if (fa1)
  1414. fa1->fa_default = -1;
  1415. goto out;
  1416. }
  1417. if (!fib_detect_death(fi, order, &last_resort, &last_idx,
  1418. fa1->fa_default)) {
  1419. fib_result_assign(res, fi);
  1420. fa1->fa_default = order;
  1421. goto out;
  1422. }
  1423. if (last_idx >= 0)
  1424. fib_result_assign(res, last_resort);
  1425. fa1->fa_default = last_idx;
  1426. out:
  1427. return;
  1428. }
  1429. /*
  1430. * Dead device goes up. We wake up dead nexthops.
  1431. * It takes sense only on multipath routes.
  1432. */
  1433. int fib_sync_up(struct net_device *dev, unsigned int nh_flags)
  1434. {
  1435. struct fib_info *prev_fi;
  1436. unsigned int hash;
  1437. struct hlist_head *head;
  1438. struct fib_nh *nh;
  1439. int ret;
  1440. if (!(dev->flags & IFF_UP))
  1441. return 0;
  1442. if (nh_flags & RTNH_F_DEAD) {
  1443. unsigned int flags = dev_get_flags(dev);
  1444. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1445. nh_flags |= RTNH_F_LINKDOWN;
  1446. }
  1447. prev_fi = NULL;
  1448. hash = fib_devindex_hashfn(dev->ifindex);
  1449. head = &fib_info_devhash[hash];
  1450. ret = 0;
  1451. hlist_for_each_entry(nh, head, nh_hash) {
  1452. struct fib_info *fi = nh->nh_parent;
  1453. int alive;
  1454. BUG_ON(!fi->fib_nhs);
  1455. if (nh->nh_dev != dev || fi == prev_fi)
  1456. continue;
  1457. prev_fi = fi;
  1458. alive = 0;
  1459. change_nexthops(fi) {
  1460. if (!(nexthop_nh->nh_flags & nh_flags)) {
  1461. alive++;
  1462. continue;
  1463. }
  1464. if (!nexthop_nh->nh_dev ||
  1465. !(nexthop_nh->nh_dev->flags & IFF_UP))
  1466. continue;
  1467. if (nexthop_nh->nh_dev != dev ||
  1468. !__in_dev_get_rtnl(dev))
  1469. continue;
  1470. alive++;
  1471. nexthop_nh->nh_flags &= ~nh_flags;
  1472. call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
  1473. } endfor_nexthops(fi)
  1474. if (alive > 0) {
  1475. fi->fib_flags &= ~nh_flags;
  1476. ret++;
  1477. }
  1478. fib_rebalance(fi);
  1479. }
  1480. return ret;
  1481. }
  1482. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1483. static bool fib_good_nh(const struct fib_nh *nh)
  1484. {
  1485. int state = NUD_REACHABLE;
  1486. if (nh->nh_scope == RT_SCOPE_LINK) {
  1487. struct neighbour *n;
  1488. rcu_read_lock_bh();
  1489. n = __ipv4_neigh_lookup_noref(nh->nh_dev,
  1490. (__force u32)nh->nh_gw);
  1491. if (n)
  1492. state = n->nud_state;
  1493. rcu_read_unlock_bh();
  1494. }
  1495. return !!(state & NUD_VALID);
  1496. }
  1497. void fib_select_multipath(struct fib_result *res, int hash)
  1498. {
  1499. struct fib_info *fi = res->fi;
  1500. struct net *net = fi->fib_net;
  1501. bool first = false;
  1502. for_nexthops(fi) {
  1503. if (hash > atomic_read(&nh->nh_upper_bound))
  1504. continue;
  1505. if (!net->ipv4.sysctl_fib_multipath_use_neigh ||
  1506. fib_good_nh(nh)) {
  1507. res->nh_sel = nhsel;
  1508. return;
  1509. }
  1510. if (!first) {
  1511. res->nh_sel = nhsel;
  1512. first = true;
  1513. }
  1514. } endfor_nexthops(fi);
  1515. }
  1516. #endif
  1517. void fib_select_path(struct net *net, struct fib_result *res,
  1518. struct flowi4 *fl4, const struct sk_buff *skb)
  1519. {
  1520. bool oif_check;
  1521. oif_check = (fl4->flowi4_oif == 0 ||
  1522. fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF);
  1523. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1524. if (res->fi->fib_nhs > 1 && oif_check) {
  1525. int h = fib_multipath_hash(res->fi, fl4, skb);
  1526. fib_select_multipath(res, h);
  1527. }
  1528. else
  1529. #endif
  1530. if (!res->prefixlen &&
  1531. res->table->tb_num_default > 1 &&
  1532. res->type == RTN_UNICAST && oif_check)
  1533. fib_select_default(fl4, res);
  1534. if (!fl4->saddr)
  1535. fl4->saddr = FIB_RES_PREFSRC(net, *res);
  1536. }
  1537. EXPORT_SYMBOL_GPL(fib_select_path);