fib_semantics.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  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. #else /* CONFIG_IP_ROUTE_MULTIPATH */
  511. #define fib_rebalance(fi) do { } while (0)
  512. #endif /* CONFIG_IP_ROUTE_MULTIPATH */
  513. static int fib_encap_match(u16 encap_type,
  514. struct nlattr *encap,
  515. const struct fib_nh *nh,
  516. const struct fib_config *cfg,
  517. struct netlink_ext_ack *extack)
  518. {
  519. struct lwtunnel_state *lwtstate;
  520. int ret, result = 0;
  521. if (encap_type == LWTUNNEL_ENCAP_NONE)
  522. return 0;
  523. ret = lwtunnel_build_state(encap_type, encap, AF_INET,
  524. cfg, &lwtstate, extack);
  525. if (!ret) {
  526. result = lwtunnel_cmp_encap(lwtstate, nh->nh_lwtstate);
  527. lwtstate_free(lwtstate);
  528. }
  529. return result;
  530. }
  531. int fib_nh_match(struct fib_config *cfg, struct fib_info *fi,
  532. struct netlink_ext_ack *extack)
  533. {
  534. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  535. struct rtnexthop *rtnh;
  536. int remaining;
  537. #endif
  538. if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
  539. return 1;
  540. if (cfg->fc_oif || cfg->fc_gw) {
  541. if (cfg->fc_encap) {
  542. if (fib_encap_match(cfg->fc_encap_type, cfg->fc_encap,
  543. fi->fib_nh, cfg, extack))
  544. return 1;
  545. }
  546. #ifdef CONFIG_IP_ROUTE_CLASSID
  547. if (cfg->fc_flow &&
  548. cfg->fc_flow != fi->fib_nh->nh_tclassid)
  549. return 1;
  550. #endif
  551. if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
  552. (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
  553. return 0;
  554. return 1;
  555. }
  556. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  557. if (!cfg->fc_mp)
  558. return 0;
  559. rtnh = cfg->fc_mp;
  560. remaining = cfg->fc_mp_len;
  561. for_nexthops(fi) {
  562. int attrlen;
  563. if (!rtnh_ok(rtnh, remaining))
  564. return -EINVAL;
  565. if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
  566. return 1;
  567. attrlen = rtnh_attrlen(rtnh);
  568. if (attrlen > 0) {
  569. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  570. nla = nla_find(attrs, attrlen, RTA_GATEWAY);
  571. if (nla && nla_get_in_addr(nla) != nh->nh_gw)
  572. return 1;
  573. #ifdef CONFIG_IP_ROUTE_CLASSID
  574. nla = nla_find(attrs, attrlen, RTA_FLOW);
  575. if (nla && nla_get_u32(nla) != nh->nh_tclassid)
  576. return 1;
  577. #endif
  578. }
  579. rtnh = rtnh_next(rtnh, &remaining);
  580. } endfor_nexthops(fi);
  581. #endif
  582. return 0;
  583. }
  584. bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
  585. {
  586. struct nlattr *nla;
  587. int remaining;
  588. if (!cfg->fc_mx)
  589. return true;
  590. nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
  591. int type = nla_type(nla);
  592. u32 fi_val, val;
  593. if (!type)
  594. continue;
  595. if (type > RTAX_MAX)
  596. return false;
  597. if (type == RTAX_CC_ALGO) {
  598. char tmp[TCP_CA_NAME_MAX];
  599. bool ecn_ca = false;
  600. nla_strlcpy(tmp, nla, sizeof(tmp));
  601. val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
  602. } else {
  603. if (nla_len(nla) != sizeof(u32))
  604. return false;
  605. val = nla_get_u32(nla);
  606. }
  607. fi_val = fi->fib_metrics->metrics[type - 1];
  608. if (type == RTAX_FEATURES)
  609. fi_val &= ~DST_FEATURE_ECN_CA;
  610. if (fi_val != val)
  611. return false;
  612. }
  613. return true;
  614. }
  615. /*
  616. * Picture
  617. * -------
  618. *
  619. * Semantics of nexthop is very messy by historical reasons.
  620. * We have to take into account, that:
  621. * a) gateway can be actually local interface address,
  622. * so that gatewayed route is direct.
  623. * b) gateway must be on-link address, possibly
  624. * described not by an ifaddr, but also by a direct route.
  625. * c) If both gateway and interface are specified, they should not
  626. * contradict.
  627. * d) If we use tunnel routes, gateway could be not on-link.
  628. *
  629. * Attempt to reconcile all of these (alas, self-contradictory) conditions
  630. * results in pretty ugly and hairy code with obscure logic.
  631. *
  632. * I chose to generalized it instead, so that the size
  633. * of code does not increase practically, but it becomes
  634. * much more general.
  635. * Every prefix is assigned a "scope" value: "host" is local address,
  636. * "link" is direct route,
  637. * [ ... "site" ... "interior" ... ]
  638. * and "universe" is true gateway route with global meaning.
  639. *
  640. * Every prefix refers to a set of "nexthop"s (gw, oif),
  641. * where gw must have narrower scope. This recursion stops
  642. * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
  643. * which means that gw is forced to be on link.
  644. *
  645. * Code is still hairy, but now it is apparently logically
  646. * consistent and very flexible. F.e. as by-product it allows
  647. * to co-exists in peace independent exterior and interior
  648. * routing processes.
  649. *
  650. * Normally it looks as following.
  651. *
  652. * {universe prefix} -> (gw, oif) [scope link]
  653. * |
  654. * |-> {link prefix} -> (gw, oif) [scope local]
  655. * |
  656. * |-> {local prefix} (terminal node)
  657. */
  658. static int fib_check_nh(struct fib_config *cfg, struct fib_nh *nh,
  659. struct netlink_ext_ack *extack)
  660. {
  661. int err = 0;
  662. struct net *net;
  663. struct net_device *dev;
  664. net = cfg->fc_nlinfo.nl_net;
  665. if (nh->nh_gw) {
  666. struct fib_result res;
  667. if (nh->nh_flags & RTNH_F_ONLINK) {
  668. unsigned int addr_type;
  669. if (cfg->fc_scope >= RT_SCOPE_LINK) {
  670. NL_SET_ERR_MSG(extack,
  671. "Nexthop has invalid scope");
  672. return -EINVAL;
  673. }
  674. dev = __dev_get_by_index(net, nh->nh_oif);
  675. if (!dev)
  676. return -ENODEV;
  677. if (!(dev->flags & IFF_UP)) {
  678. NL_SET_ERR_MSG(extack,
  679. "Nexthop device is not up");
  680. return -ENETDOWN;
  681. }
  682. addr_type = inet_addr_type_dev_table(net, dev, nh->nh_gw);
  683. if (addr_type != RTN_UNICAST) {
  684. NL_SET_ERR_MSG(extack,
  685. "Nexthop has invalid gateway");
  686. return -EINVAL;
  687. }
  688. if (!netif_carrier_ok(dev))
  689. nh->nh_flags |= RTNH_F_LINKDOWN;
  690. nh->nh_dev = dev;
  691. dev_hold(dev);
  692. nh->nh_scope = RT_SCOPE_LINK;
  693. return 0;
  694. }
  695. rcu_read_lock();
  696. {
  697. struct fib_table *tbl = NULL;
  698. struct flowi4 fl4 = {
  699. .daddr = nh->nh_gw,
  700. .flowi4_scope = cfg->fc_scope + 1,
  701. .flowi4_oif = nh->nh_oif,
  702. .flowi4_iif = LOOPBACK_IFINDEX,
  703. };
  704. /* It is not necessary, but requires a bit of thinking */
  705. if (fl4.flowi4_scope < RT_SCOPE_LINK)
  706. fl4.flowi4_scope = RT_SCOPE_LINK;
  707. if (cfg->fc_table)
  708. tbl = fib_get_table(net, cfg->fc_table);
  709. if (tbl)
  710. err = fib_table_lookup(tbl, &fl4, &res,
  711. FIB_LOOKUP_IGNORE_LINKSTATE |
  712. FIB_LOOKUP_NOREF);
  713. /* on error or if no table given do full lookup. This
  714. * is needed for example when nexthops are in the local
  715. * table rather than the given table
  716. */
  717. if (!tbl || err) {
  718. err = fib_lookup(net, &fl4, &res,
  719. FIB_LOOKUP_IGNORE_LINKSTATE);
  720. }
  721. if (err) {
  722. NL_SET_ERR_MSG(extack,
  723. "Nexthop has invalid gateway");
  724. rcu_read_unlock();
  725. return err;
  726. }
  727. }
  728. err = -EINVAL;
  729. if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
  730. NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
  731. goto out;
  732. }
  733. nh->nh_scope = res.scope;
  734. nh->nh_oif = FIB_RES_OIF(res);
  735. nh->nh_dev = dev = FIB_RES_DEV(res);
  736. if (!dev) {
  737. NL_SET_ERR_MSG(extack,
  738. "No egress device for nexthop gateway");
  739. goto out;
  740. }
  741. dev_hold(dev);
  742. if (!netif_carrier_ok(dev))
  743. nh->nh_flags |= RTNH_F_LINKDOWN;
  744. err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
  745. } else {
  746. struct in_device *in_dev;
  747. if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
  748. NL_SET_ERR_MSG(extack,
  749. "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
  750. return -EINVAL;
  751. }
  752. rcu_read_lock();
  753. err = -ENODEV;
  754. in_dev = inetdev_by_index(net, nh->nh_oif);
  755. if (!in_dev)
  756. goto out;
  757. err = -ENETDOWN;
  758. if (!(in_dev->dev->flags & IFF_UP)) {
  759. NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
  760. goto out;
  761. }
  762. nh->nh_dev = in_dev->dev;
  763. dev_hold(nh->nh_dev);
  764. nh->nh_scope = RT_SCOPE_HOST;
  765. if (!netif_carrier_ok(nh->nh_dev))
  766. nh->nh_flags |= RTNH_F_LINKDOWN;
  767. err = 0;
  768. }
  769. out:
  770. rcu_read_unlock();
  771. return err;
  772. }
  773. static inline unsigned int fib_laddr_hashfn(__be32 val)
  774. {
  775. unsigned int mask = (fib_info_hash_size - 1);
  776. return ((__force u32)val ^
  777. ((__force u32)val >> 7) ^
  778. ((__force u32)val >> 14)) & mask;
  779. }
  780. static struct hlist_head *fib_info_hash_alloc(int bytes)
  781. {
  782. if (bytes <= PAGE_SIZE)
  783. return kzalloc(bytes, GFP_KERNEL);
  784. else
  785. return (struct hlist_head *)
  786. __get_free_pages(GFP_KERNEL | __GFP_ZERO,
  787. get_order(bytes));
  788. }
  789. static void fib_info_hash_free(struct hlist_head *hash, int bytes)
  790. {
  791. if (!hash)
  792. return;
  793. if (bytes <= PAGE_SIZE)
  794. kfree(hash);
  795. else
  796. free_pages((unsigned long) hash, get_order(bytes));
  797. }
  798. static void fib_info_hash_move(struct hlist_head *new_info_hash,
  799. struct hlist_head *new_laddrhash,
  800. unsigned int new_size)
  801. {
  802. struct hlist_head *old_info_hash, *old_laddrhash;
  803. unsigned int old_size = fib_info_hash_size;
  804. unsigned int i, bytes;
  805. spin_lock_bh(&fib_info_lock);
  806. old_info_hash = fib_info_hash;
  807. old_laddrhash = fib_info_laddrhash;
  808. fib_info_hash_size = new_size;
  809. for (i = 0; i < old_size; i++) {
  810. struct hlist_head *head = &fib_info_hash[i];
  811. struct hlist_node *n;
  812. struct fib_info *fi;
  813. hlist_for_each_entry_safe(fi, n, head, fib_hash) {
  814. struct hlist_head *dest;
  815. unsigned int new_hash;
  816. new_hash = fib_info_hashfn(fi);
  817. dest = &new_info_hash[new_hash];
  818. hlist_add_head(&fi->fib_hash, dest);
  819. }
  820. }
  821. fib_info_hash = new_info_hash;
  822. for (i = 0; i < old_size; i++) {
  823. struct hlist_head *lhead = &fib_info_laddrhash[i];
  824. struct hlist_node *n;
  825. struct fib_info *fi;
  826. hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
  827. struct hlist_head *ldest;
  828. unsigned int new_hash;
  829. new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
  830. ldest = &new_laddrhash[new_hash];
  831. hlist_add_head(&fi->fib_lhash, ldest);
  832. }
  833. }
  834. fib_info_laddrhash = new_laddrhash;
  835. spin_unlock_bh(&fib_info_lock);
  836. bytes = old_size * sizeof(struct hlist_head *);
  837. fib_info_hash_free(old_info_hash, bytes);
  838. fib_info_hash_free(old_laddrhash, bytes);
  839. }
  840. __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
  841. {
  842. nh->nh_saddr = inet_select_addr(nh->nh_dev,
  843. nh->nh_gw,
  844. nh->nh_parent->fib_scope);
  845. nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
  846. return nh->nh_saddr;
  847. }
  848. static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
  849. {
  850. if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
  851. fib_prefsrc != cfg->fc_dst) {
  852. u32 tb_id = cfg->fc_table;
  853. int rc;
  854. if (tb_id == RT_TABLE_MAIN)
  855. tb_id = RT_TABLE_LOCAL;
  856. rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
  857. fib_prefsrc, tb_id);
  858. if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
  859. rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
  860. fib_prefsrc, RT_TABLE_LOCAL);
  861. }
  862. if (rc != RTN_LOCAL)
  863. return false;
  864. }
  865. return true;
  866. }
  867. static int
  868. fib_convert_metrics(struct fib_info *fi, const struct fib_config *cfg)
  869. {
  870. return ip_metrics_convert(fi->fib_net, cfg->fc_mx, cfg->fc_mx_len,
  871. fi->fib_metrics->metrics);
  872. }
  873. struct fib_info *fib_create_info(struct fib_config *cfg,
  874. struct netlink_ext_ack *extack)
  875. {
  876. int err;
  877. struct fib_info *fi = NULL;
  878. struct fib_info *ofi;
  879. int nhs = 1;
  880. struct net *net = cfg->fc_nlinfo.nl_net;
  881. if (cfg->fc_type > RTN_MAX)
  882. goto err_inval;
  883. /* Fast check to catch the most weird cases */
  884. if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
  885. NL_SET_ERR_MSG(extack, "Invalid scope");
  886. goto err_inval;
  887. }
  888. if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
  889. NL_SET_ERR_MSG(extack,
  890. "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
  891. goto err_inval;
  892. }
  893. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  894. if (cfg->fc_mp) {
  895. nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
  896. if (nhs == 0)
  897. goto err_inval;
  898. }
  899. #endif
  900. err = -ENOBUFS;
  901. if (fib_info_cnt >= fib_info_hash_size) {
  902. unsigned int new_size = fib_info_hash_size << 1;
  903. struct hlist_head *new_info_hash;
  904. struct hlist_head *new_laddrhash;
  905. unsigned int bytes;
  906. if (!new_size)
  907. new_size = 16;
  908. bytes = new_size * sizeof(struct hlist_head *);
  909. new_info_hash = fib_info_hash_alloc(bytes);
  910. new_laddrhash = fib_info_hash_alloc(bytes);
  911. if (!new_info_hash || !new_laddrhash) {
  912. fib_info_hash_free(new_info_hash, bytes);
  913. fib_info_hash_free(new_laddrhash, bytes);
  914. } else
  915. fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
  916. if (!fib_info_hash_size)
  917. goto failure;
  918. }
  919. fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
  920. if (!fi)
  921. goto failure;
  922. if (cfg->fc_mx) {
  923. fi->fib_metrics = kzalloc(sizeof(*fi->fib_metrics), GFP_KERNEL);
  924. if (unlikely(!fi->fib_metrics)) {
  925. kfree(fi);
  926. return ERR_PTR(err);
  927. }
  928. refcount_set(&fi->fib_metrics->refcnt, 1);
  929. } else {
  930. fi->fib_metrics = (struct dst_metrics *)&dst_default_metrics;
  931. }
  932. fib_info_cnt++;
  933. fi->fib_net = net;
  934. fi->fib_protocol = cfg->fc_protocol;
  935. fi->fib_scope = cfg->fc_scope;
  936. fi->fib_flags = cfg->fc_flags;
  937. fi->fib_priority = cfg->fc_priority;
  938. fi->fib_prefsrc = cfg->fc_prefsrc;
  939. fi->fib_type = cfg->fc_type;
  940. fi->fib_tb_id = cfg->fc_table;
  941. fi->fib_nhs = nhs;
  942. change_nexthops(fi) {
  943. nexthop_nh->nh_parent = fi;
  944. nexthop_nh->nh_pcpu_rth_output = alloc_percpu(struct rtable __rcu *);
  945. if (!nexthop_nh->nh_pcpu_rth_output)
  946. goto failure;
  947. } endfor_nexthops(fi)
  948. err = fib_convert_metrics(fi, cfg);
  949. if (err)
  950. goto failure;
  951. if (cfg->fc_mp) {
  952. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  953. err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg, extack);
  954. if (err != 0)
  955. goto failure;
  956. if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif) {
  957. NL_SET_ERR_MSG(extack,
  958. "Nexthop device index does not match RTA_OIF");
  959. goto err_inval;
  960. }
  961. if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw) {
  962. NL_SET_ERR_MSG(extack,
  963. "Nexthop gateway does not match RTA_GATEWAY");
  964. goto err_inval;
  965. }
  966. #ifdef CONFIG_IP_ROUTE_CLASSID
  967. if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) {
  968. NL_SET_ERR_MSG(extack,
  969. "Nexthop class id does not match RTA_FLOW");
  970. goto err_inval;
  971. }
  972. #endif
  973. #else
  974. NL_SET_ERR_MSG(extack,
  975. "Multipath support not enabled in kernel");
  976. goto err_inval;
  977. #endif
  978. } else {
  979. struct fib_nh *nh = fi->fib_nh;
  980. if (cfg->fc_encap) {
  981. struct lwtunnel_state *lwtstate;
  982. if (cfg->fc_encap_type == LWTUNNEL_ENCAP_NONE) {
  983. NL_SET_ERR_MSG(extack,
  984. "LWT encap type not specified");
  985. goto err_inval;
  986. }
  987. err = lwtunnel_build_state(cfg->fc_encap_type,
  988. cfg->fc_encap, AF_INET, cfg,
  989. &lwtstate, extack);
  990. if (err)
  991. goto failure;
  992. nh->nh_lwtstate = lwtstate_get(lwtstate);
  993. }
  994. nh->nh_oif = cfg->fc_oif;
  995. nh->nh_gw = cfg->fc_gw;
  996. nh->nh_flags = cfg->fc_flags;
  997. #ifdef CONFIG_IP_ROUTE_CLASSID
  998. nh->nh_tclassid = cfg->fc_flow;
  999. if (nh->nh_tclassid)
  1000. fi->fib_net->ipv4.fib_num_tclassid_users++;
  1001. #endif
  1002. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1003. nh->nh_weight = 1;
  1004. #endif
  1005. }
  1006. if (fib_props[cfg->fc_type].error) {
  1007. if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp) {
  1008. NL_SET_ERR_MSG(extack,
  1009. "Gateway, device and multipath can not be specified for this route type");
  1010. goto err_inval;
  1011. }
  1012. goto link_it;
  1013. } else {
  1014. switch (cfg->fc_type) {
  1015. case RTN_UNICAST:
  1016. case RTN_LOCAL:
  1017. case RTN_BROADCAST:
  1018. case RTN_ANYCAST:
  1019. case RTN_MULTICAST:
  1020. break;
  1021. default:
  1022. NL_SET_ERR_MSG(extack, "Invalid route type");
  1023. goto err_inval;
  1024. }
  1025. }
  1026. if (cfg->fc_scope > RT_SCOPE_HOST) {
  1027. NL_SET_ERR_MSG(extack, "Invalid scope");
  1028. goto err_inval;
  1029. }
  1030. if (cfg->fc_scope == RT_SCOPE_HOST) {
  1031. struct fib_nh *nh = fi->fib_nh;
  1032. /* Local address is added. */
  1033. if (nhs != 1) {
  1034. NL_SET_ERR_MSG(extack,
  1035. "Route with host scope can not have multiple nexthops");
  1036. goto err_inval;
  1037. }
  1038. if (nh->nh_gw) {
  1039. NL_SET_ERR_MSG(extack,
  1040. "Route with host scope can not have a gateway");
  1041. goto err_inval;
  1042. }
  1043. nh->nh_scope = RT_SCOPE_NOWHERE;
  1044. nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
  1045. err = -ENODEV;
  1046. if (!nh->nh_dev)
  1047. goto failure;
  1048. } else {
  1049. int linkdown = 0;
  1050. change_nexthops(fi) {
  1051. err = fib_check_nh(cfg, nexthop_nh, extack);
  1052. if (err != 0)
  1053. goto failure;
  1054. if (nexthop_nh->nh_flags & RTNH_F_LINKDOWN)
  1055. linkdown++;
  1056. } endfor_nexthops(fi)
  1057. if (linkdown == fi->fib_nhs)
  1058. fi->fib_flags |= RTNH_F_LINKDOWN;
  1059. }
  1060. if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
  1061. NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
  1062. goto err_inval;
  1063. }
  1064. change_nexthops(fi) {
  1065. fib_info_update_nh_saddr(net, nexthop_nh);
  1066. } endfor_nexthops(fi)
  1067. fib_rebalance(fi);
  1068. link_it:
  1069. ofi = fib_find_info(fi);
  1070. if (ofi) {
  1071. fi->fib_dead = 1;
  1072. free_fib_info(fi);
  1073. ofi->fib_treeref++;
  1074. return ofi;
  1075. }
  1076. fi->fib_treeref++;
  1077. refcount_set(&fi->fib_clntref, 1);
  1078. spin_lock_bh(&fib_info_lock);
  1079. hlist_add_head(&fi->fib_hash,
  1080. &fib_info_hash[fib_info_hashfn(fi)]);
  1081. if (fi->fib_prefsrc) {
  1082. struct hlist_head *head;
  1083. head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
  1084. hlist_add_head(&fi->fib_lhash, head);
  1085. }
  1086. change_nexthops(fi) {
  1087. struct hlist_head *head;
  1088. unsigned int hash;
  1089. if (!nexthop_nh->nh_dev)
  1090. continue;
  1091. hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
  1092. head = &fib_info_devhash[hash];
  1093. hlist_add_head(&nexthop_nh->nh_hash, head);
  1094. } endfor_nexthops(fi)
  1095. spin_unlock_bh(&fib_info_lock);
  1096. return fi;
  1097. err_inval:
  1098. err = -EINVAL;
  1099. failure:
  1100. if (fi) {
  1101. fi->fib_dead = 1;
  1102. free_fib_info(fi);
  1103. }
  1104. return ERR_PTR(err);
  1105. }
  1106. int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1107. u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
  1108. struct fib_info *fi, unsigned int flags)
  1109. {
  1110. struct nlmsghdr *nlh;
  1111. struct rtmsg *rtm;
  1112. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1113. if (!nlh)
  1114. return -EMSGSIZE;
  1115. rtm = nlmsg_data(nlh);
  1116. rtm->rtm_family = AF_INET;
  1117. rtm->rtm_dst_len = dst_len;
  1118. rtm->rtm_src_len = 0;
  1119. rtm->rtm_tos = tos;
  1120. if (tb_id < 256)
  1121. rtm->rtm_table = tb_id;
  1122. else
  1123. rtm->rtm_table = RT_TABLE_COMPAT;
  1124. if (nla_put_u32(skb, RTA_TABLE, tb_id))
  1125. goto nla_put_failure;
  1126. rtm->rtm_type = type;
  1127. rtm->rtm_flags = fi->fib_flags;
  1128. rtm->rtm_scope = fi->fib_scope;
  1129. rtm->rtm_protocol = fi->fib_protocol;
  1130. if (rtm->rtm_dst_len &&
  1131. nla_put_in_addr(skb, RTA_DST, dst))
  1132. goto nla_put_failure;
  1133. if (fi->fib_priority &&
  1134. nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
  1135. goto nla_put_failure;
  1136. if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
  1137. goto nla_put_failure;
  1138. if (fi->fib_prefsrc &&
  1139. nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
  1140. goto nla_put_failure;
  1141. if (fi->fib_nhs == 1) {
  1142. if (fi->fib_nh->nh_gw &&
  1143. nla_put_in_addr(skb, RTA_GATEWAY, fi->fib_nh->nh_gw))
  1144. goto nla_put_failure;
  1145. if (fi->fib_nh->nh_oif &&
  1146. nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif))
  1147. goto nla_put_failure;
  1148. if (fi->fib_nh->nh_flags & RTNH_F_LINKDOWN) {
  1149. struct in_device *in_dev;
  1150. rcu_read_lock();
  1151. in_dev = __in_dev_get_rcu(fi->fib_nh->nh_dev);
  1152. if (in_dev &&
  1153. IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
  1154. rtm->rtm_flags |= RTNH_F_DEAD;
  1155. rcu_read_unlock();
  1156. }
  1157. if (fi->fib_nh->nh_flags & RTNH_F_OFFLOAD)
  1158. rtm->rtm_flags |= RTNH_F_OFFLOAD;
  1159. #ifdef CONFIG_IP_ROUTE_CLASSID
  1160. if (fi->fib_nh[0].nh_tclassid &&
  1161. nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid))
  1162. goto nla_put_failure;
  1163. #endif
  1164. if (fi->fib_nh->nh_lwtstate &&
  1165. lwtunnel_fill_encap(skb, fi->fib_nh->nh_lwtstate) < 0)
  1166. goto nla_put_failure;
  1167. }
  1168. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1169. if (fi->fib_nhs > 1) {
  1170. struct rtnexthop *rtnh;
  1171. struct nlattr *mp;
  1172. mp = nla_nest_start(skb, RTA_MULTIPATH);
  1173. if (!mp)
  1174. goto nla_put_failure;
  1175. for_nexthops(fi) {
  1176. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1177. if (!rtnh)
  1178. goto nla_put_failure;
  1179. rtnh->rtnh_flags = nh->nh_flags & 0xFF;
  1180. if (nh->nh_flags & RTNH_F_LINKDOWN) {
  1181. struct in_device *in_dev;
  1182. rcu_read_lock();
  1183. in_dev = __in_dev_get_rcu(nh->nh_dev);
  1184. if (in_dev &&
  1185. IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
  1186. rtnh->rtnh_flags |= RTNH_F_DEAD;
  1187. rcu_read_unlock();
  1188. }
  1189. rtnh->rtnh_hops = nh->nh_weight - 1;
  1190. rtnh->rtnh_ifindex = nh->nh_oif;
  1191. if (nh->nh_gw &&
  1192. nla_put_in_addr(skb, RTA_GATEWAY, nh->nh_gw))
  1193. goto nla_put_failure;
  1194. #ifdef CONFIG_IP_ROUTE_CLASSID
  1195. if (nh->nh_tclassid &&
  1196. nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
  1197. goto nla_put_failure;
  1198. #endif
  1199. if (nh->nh_lwtstate &&
  1200. lwtunnel_fill_encap(skb, nh->nh_lwtstate) < 0)
  1201. goto nla_put_failure;
  1202. /* length of rtnetlink header + attributes */
  1203. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
  1204. } endfor_nexthops(fi);
  1205. nla_nest_end(skb, mp);
  1206. }
  1207. #endif
  1208. nlmsg_end(skb, nlh);
  1209. return 0;
  1210. nla_put_failure:
  1211. nlmsg_cancel(skb, nlh);
  1212. return -EMSGSIZE;
  1213. }
  1214. /*
  1215. * Update FIB if:
  1216. * - local address disappeared -> we must delete all the entries
  1217. * referring to it.
  1218. * - device went down -> we must shutdown all nexthops going via it.
  1219. */
  1220. int fib_sync_down_addr(struct net_device *dev, __be32 local)
  1221. {
  1222. int ret = 0;
  1223. unsigned int hash = fib_laddr_hashfn(local);
  1224. struct hlist_head *head = &fib_info_laddrhash[hash];
  1225. int tb_id = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
  1226. struct net *net = dev_net(dev);
  1227. struct fib_info *fi;
  1228. if (!fib_info_laddrhash || local == 0)
  1229. return 0;
  1230. hlist_for_each_entry(fi, head, fib_lhash) {
  1231. if (!net_eq(fi->fib_net, net) ||
  1232. fi->fib_tb_id != tb_id)
  1233. continue;
  1234. if (fi->fib_prefsrc == local) {
  1235. fi->fib_flags |= RTNH_F_DEAD;
  1236. ret++;
  1237. }
  1238. }
  1239. return ret;
  1240. }
  1241. static int call_fib_nh_notifiers(struct fib_nh *fib_nh,
  1242. enum fib_event_type event_type)
  1243. {
  1244. struct in_device *in_dev = __in_dev_get_rtnl(fib_nh->nh_dev);
  1245. struct fib_nh_notifier_info info = {
  1246. .fib_nh = fib_nh,
  1247. };
  1248. switch (event_type) {
  1249. case FIB_EVENT_NH_ADD:
  1250. if (fib_nh->nh_flags & RTNH_F_DEAD)
  1251. break;
  1252. if (IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
  1253. fib_nh->nh_flags & RTNH_F_LINKDOWN)
  1254. break;
  1255. return call_fib4_notifiers(dev_net(fib_nh->nh_dev), event_type,
  1256. &info.info);
  1257. case FIB_EVENT_NH_DEL:
  1258. if ((in_dev && IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
  1259. fib_nh->nh_flags & RTNH_F_LINKDOWN) ||
  1260. (fib_nh->nh_flags & RTNH_F_DEAD))
  1261. return call_fib4_notifiers(dev_net(fib_nh->nh_dev),
  1262. event_type, &info.info);
  1263. default:
  1264. break;
  1265. }
  1266. return NOTIFY_DONE;
  1267. }
  1268. /* Update the PMTU of exceptions when:
  1269. * - the new MTU of the first hop becomes smaller than the PMTU
  1270. * - the old MTU was the same as the PMTU, and it limited discovery of
  1271. * larger MTUs on the path. With that limit raised, we can now
  1272. * discover larger MTUs
  1273. * A special case is locked exceptions, for which the PMTU is smaller
  1274. * than the minimal accepted PMTU:
  1275. * - if the new MTU is greater than the PMTU, don't make any change
  1276. * - otherwise, unlock and set PMTU
  1277. */
  1278. static void nh_update_mtu(struct fib_nh *nh, u32 new, u32 orig)
  1279. {
  1280. struct fnhe_hash_bucket *bucket;
  1281. int i;
  1282. bucket = rcu_dereference_protected(nh->nh_exceptions, 1);
  1283. if (!bucket)
  1284. return;
  1285. for (i = 0; i < FNHE_HASH_SIZE; i++) {
  1286. struct fib_nh_exception *fnhe;
  1287. for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
  1288. fnhe;
  1289. fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
  1290. if (fnhe->fnhe_mtu_locked) {
  1291. if (new <= fnhe->fnhe_pmtu) {
  1292. fnhe->fnhe_pmtu = new;
  1293. fnhe->fnhe_mtu_locked = false;
  1294. }
  1295. } else if (new < fnhe->fnhe_pmtu ||
  1296. orig == fnhe->fnhe_pmtu) {
  1297. fnhe->fnhe_pmtu = new;
  1298. }
  1299. }
  1300. }
  1301. }
  1302. void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
  1303. {
  1304. unsigned int hash = fib_devindex_hashfn(dev->ifindex);
  1305. struct hlist_head *head = &fib_info_devhash[hash];
  1306. struct fib_nh *nh;
  1307. hlist_for_each_entry(nh, head, nh_hash) {
  1308. if (nh->nh_dev == dev)
  1309. nh_update_mtu(nh, dev->mtu, orig_mtu);
  1310. }
  1311. }
  1312. /* Event force Flags Description
  1313. * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
  1314. * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
  1315. * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
  1316. * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
  1317. */
  1318. int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
  1319. {
  1320. int ret = 0;
  1321. int scope = RT_SCOPE_NOWHERE;
  1322. struct fib_info *prev_fi = NULL;
  1323. unsigned int hash = fib_devindex_hashfn(dev->ifindex);
  1324. struct hlist_head *head = &fib_info_devhash[hash];
  1325. struct fib_nh *nh;
  1326. if (force)
  1327. scope = -1;
  1328. hlist_for_each_entry(nh, head, nh_hash) {
  1329. struct fib_info *fi = nh->nh_parent;
  1330. int dead;
  1331. BUG_ON(!fi->fib_nhs);
  1332. if (nh->nh_dev != dev || fi == prev_fi)
  1333. continue;
  1334. prev_fi = fi;
  1335. dead = 0;
  1336. change_nexthops(fi) {
  1337. if (nexthop_nh->nh_flags & RTNH_F_DEAD)
  1338. dead++;
  1339. else if (nexthop_nh->nh_dev == dev &&
  1340. nexthop_nh->nh_scope != scope) {
  1341. switch (event) {
  1342. case NETDEV_DOWN:
  1343. case NETDEV_UNREGISTER:
  1344. nexthop_nh->nh_flags |= RTNH_F_DEAD;
  1345. /* fall through */
  1346. case NETDEV_CHANGE:
  1347. nexthop_nh->nh_flags |= RTNH_F_LINKDOWN;
  1348. break;
  1349. }
  1350. call_fib_nh_notifiers(nexthop_nh,
  1351. FIB_EVENT_NH_DEL);
  1352. dead++;
  1353. }
  1354. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1355. if (event == NETDEV_UNREGISTER &&
  1356. nexthop_nh->nh_dev == dev) {
  1357. dead = fi->fib_nhs;
  1358. break;
  1359. }
  1360. #endif
  1361. } endfor_nexthops(fi)
  1362. if (dead == fi->fib_nhs) {
  1363. switch (event) {
  1364. case NETDEV_DOWN:
  1365. case NETDEV_UNREGISTER:
  1366. fi->fib_flags |= RTNH_F_DEAD;
  1367. /* fall through */
  1368. case NETDEV_CHANGE:
  1369. fi->fib_flags |= RTNH_F_LINKDOWN;
  1370. break;
  1371. }
  1372. ret++;
  1373. }
  1374. fib_rebalance(fi);
  1375. }
  1376. return ret;
  1377. }
  1378. /* Must be invoked inside of an RCU protected region. */
  1379. static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
  1380. {
  1381. struct fib_info *fi = NULL, *last_resort = NULL;
  1382. struct hlist_head *fa_head = res->fa_head;
  1383. struct fib_table *tb = res->table;
  1384. u8 slen = 32 - res->prefixlen;
  1385. int order = -1, last_idx = -1;
  1386. struct fib_alias *fa, *fa1 = NULL;
  1387. u32 last_prio = res->fi->fib_priority;
  1388. u8 last_tos = 0;
  1389. hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
  1390. struct fib_info *next_fi = fa->fa_info;
  1391. if (fa->fa_slen != slen)
  1392. continue;
  1393. if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
  1394. continue;
  1395. if (fa->tb_id != tb->tb_id)
  1396. continue;
  1397. if (next_fi->fib_priority > last_prio &&
  1398. fa->fa_tos == last_tos) {
  1399. if (last_tos)
  1400. continue;
  1401. break;
  1402. }
  1403. if (next_fi->fib_flags & RTNH_F_DEAD)
  1404. continue;
  1405. last_tos = fa->fa_tos;
  1406. last_prio = next_fi->fib_priority;
  1407. if (next_fi->fib_scope != res->scope ||
  1408. fa->fa_type != RTN_UNICAST)
  1409. continue;
  1410. if (!next_fi->fib_nh[0].nh_gw ||
  1411. next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
  1412. continue;
  1413. fib_alias_accessed(fa);
  1414. if (!fi) {
  1415. if (next_fi != res->fi)
  1416. break;
  1417. fa1 = fa;
  1418. } else if (!fib_detect_death(fi, order, &last_resort,
  1419. &last_idx, fa1->fa_default)) {
  1420. fib_result_assign(res, fi);
  1421. fa1->fa_default = order;
  1422. goto out;
  1423. }
  1424. fi = next_fi;
  1425. order++;
  1426. }
  1427. if (order <= 0 || !fi) {
  1428. if (fa1)
  1429. fa1->fa_default = -1;
  1430. goto out;
  1431. }
  1432. if (!fib_detect_death(fi, order, &last_resort, &last_idx,
  1433. fa1->fa_default)) {
  1434. fib_result_assign(res, fi);
  1435. fa1->fa_default = order;
  1436. goto out;
  1437. }
  1438. if (last_idx >= 0)
  1439. fib_result_assign(res, last_resort);
  1440. fa1->fa_default = last_idx;
  1441. out:
  1442. return;
  1443. }
  1444. /*
  1445. * Dead device goes up. We wake up dead nexthops.
  1446. * It takes sense only on multipath routes.
  1447. */
  1448. int fib_sync_up(struct net_device *dev, unsigned int nh_flags)
  1449. {
  1450. struct fib_info *prev_fi;
  1451. unsigned int hash;
  1452. struct hlist_head *head;
  1453. struct fib_nh *nh;
  1454. int ret;
  1455. if (!(dev->flags & IFF_UP))
  1456. return 0;
  1457. if (nh_flags & RTNH_F_DEAD) {
  1458. unsigned int flags = dev_get_flags(dev);
  1459. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1460. nh_flags |= RTNH_F_LINKDOWN;
  1461. }
  1462. prev_fi = NULL;
  1463. hash = fib_devindex_hashfn(dev->ifindex);
  1464. head = &fib_info_devhash[hash];
  1465. ret = 0;
  1466. hlist_for_each_entry(nh, head, nh_hash) {
  1467. struct fib_info *fi = nh->nh_parent;
  1468. int alive;
  1469. BUG_ON(!fi->fib_nhs);
  1470. if (nh->nh_dev != dev || fi == prev_fi)
  1471. continue;
  1472. prev_fi = fi;
  1473. alive = 0;
  1474. change_nexthops(fi) {
  1475. if (!(nexthop_nh->nh_flags & nh_flags)) {
  1476. alive++;
  1477. continue;
  1478. }
  1479. if (!nexthop_nh->nh_dev ||
  1480. !(nexthop_nh->nh_dev->flags & IFF_UP))
  1481. continue;
  1482. if (nexthop_nh->nh_dev != dev ||
  1483. !__in_dev_get_rtnl(dev))
  1484. continue;
  1485. alive++;
  1486. nexthop_nh->nh_flags &= ~nh_flags;
  1487. call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
  1488. } endfor_nexthops(fi)
  1489. if (alive > 0) {
  1490. fi->fib_flags &= ~nh_flags;
  1491. ret++;
  1492. }
  1493. fib_rebalance(fi);
  1494. }
  1495. return ret;
  1496. }
  1497. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1498. static bool fib_good_nh(const struct fib_nh *nh)
  1499. {
  1500. int state = NUD_REACHABLE;
  1501. if (nh->nh_scope == RT_SCOPE_LINK) {
  1502. struct neighbour *n;
  1503. rcu_read_lock_bh();
  1504. n = __ipv4_neigh_lookup_noref(nh->nh_dev,
  1505. (__force u32)nh->nh_gw);
  1506. if (n)
  1507. state = n->nud_state;
  1508. rcu_read_unlock_bh();
  1509. }
  1510. return !!(state & NUD_VALID);
  1511. }
  1512. void fib_select_multipath(struct fib_result *res, int hash)
  1513. {
  1514. struct fib_info *fi = res->fi;
  1515. struct net *net = fi->fib_net;
  1516. bool first = false;
  1517. for_nexthops(fi) {
  1518. if (net->ipv4.sysctl_fib_multipath_use_neigh) {
  1519. if (!fib_good_nh(nh))
  1520. continue;
  1521. if (!first) {
  1522. res->nh_sel = nhsel;
  1523. first = true;
  1524. }
  1525. }
  1526. if (hash > atomic_read(&nh->nh_upper_bound))
  1527. continue;
  1528. res->nh_sel = nhsel;
  1529. return;
  1530. } endfor_nexthops(fi);
  1531. }
  1532. #endif
  1533. void fib_select_path(struct net *net, struct fib_result *res,
  1534. struct flowi4 *fl4, const struct sk_buff *skb)
  1535. {
  1536. if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF))
  1537. goto check_saddr;
  1538. #ifdef CONFIG_IP_ROUTE_MULTIPATH
  1539. if (res->fi->fib_nhs > 1) {
  1540. int h = fib_multipath_hash(net, fl4, skb, NULL);
  1541. fib_select_multipath(res, h);
  1542. }
  1543. else
  1544. #endif
  1545. if (!res->prefixlen &&
  1546. res->table->tb_num_default > 1 &&
  1547. res->type == RTN_UNICAST)
  1548. fib_select_default(fl4, res);
  1549. check_saddr:
  1550. if (!fl4->saddr)
  1551. fl4->saddr = FIB_RES_PREFSRC(net, *res);
  1552. }