1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * IPv4 Forwarding Information Base: semantics.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 */
11
12 #include <linux/uaccess.h>
13 #include <linux/bitops.h>
14 #include <linux/types.h>
15 #include <linux/kernel.h>
16 #include <linux/jiffies.h>
17 #include <linux/mm.h>
18 #include <linux/string.h>
19 #include <linux/socket.h>
20 #include <linux/sockios.h>
21 #include <linux/errno.h>
22 #include <linux/in.h>
23 #include <linux/inet.h>
24 #include <linux/inetdevice.h>
25 #include <linux/netdevice.h>
26 #include <linux/if_arp.h>
27 #include <linux/proc_fs.h>
28 #include <linux/skbuff.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/netlink.h>
32
33 #include <net/arp.h>
34 #include <net/ip.h>
35 #include <net/protocol.h>
36 #include <net/route.h>
37 #include <net/tcp.h>
38 #include <net/sock.h>
39 #include <net/ip_fib.h>
40 #include <net/ip6_fib.h>
41 #include <net/nexthop.h>
42 #include <net/netlink.h>
43 #include <net/rtnh.h>
44 #include <net/lwtunnel.h>
45 #include <net/fib_notifier.h>
46 #include <net/addrconf.h>
47
48 #include "fib_lookup.h"
49
50 static DEFINE_SPINLOCK(fib_info_lock);
51 static struct hlist_head *fib_info_hash;
52 static struct hlist_head *fib_info_laddrhash;
53 static unsigned int fib_info_hash_size;
54 static unsigned int fib_info_cnt;
55
56 #define DEVINDEX_HASHBITS 8
57 #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
58 static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
59
60 /* for_nexthops and change_nexthops only used when nexthop object
61 * is not set in a fib_info. The logic within can reference fib_nh.
62 */
63 #ifdef CONFIG_IP_ROUTE_MULTIPATH
64
65 #define for_nexthops(fi) { \
66 int nhsel; const struct fib_nh *nh; \
67 for (nhsel = 0, nh = (fi)->fib_nh; \
68 nhsel < fib_info_num_path((fi)); \
69 nh++, nhsel++)
70
71 #define change_nexthops(fi) { \
72 int nhsel; struct fib_nh *nexthop_nh; \
73 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
74 nhsel < fib_info_num_path((fi)); \
75 nexthop_nh++, nhsel++)
76
77 #else /* CONFIG_IP_ROUTE_MULTIPATH */
78
79 /* Hope, that gcc will optimize it to get rid of dummy loop */
80
81 #define for_nexthops(fi) { \
82 int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \
83 for (nhsel = 0; nhsel < 1; nhsel++)
84
85 #define change_nexthops(fi) { \
86 int nhsel; \
87 struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
88 for (nhsel = 0; nhsel < 1; nhsel++)
89
90 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
91
92 #define endfor_nexthops(fi) }
93
94
95 const struct fib_prop fib_props[RTN_MAX + 1] = {
96 [RTN_UNSPEC] = {
97 .error = 0,
98 .scope = RT_SCOPE_NOWHERE,
99 },
100 [RTN_UNICAST] = {
101 .error = 0,
102 .scope = RT_SCOPE_UNIVERSE,
103 },
104 [RTN_LOCAL] = {
105 .error = 0,
106 .scope = RT_SCOPE_HOST,
107 },
108 [RTN_BROADCAST] = {
109 .error = 0,
110 .scope = RT_SCOPE_LINK,
111 },
112 [RTN_ANYCAST] = {
113 .error = 0,
114 .scope = RT_SCOPE_LINK,
115 },
116 [RTN_MULTICAST] = {
117 .error = 0,
118 .scope = RT_SCOPE_UNIVERSE,
119 },
120 [RTN_BLACKHOLE] = {
121 .error = -EINVAL,
122 .scope = RT_SCOPE_UNIVERSE,
123 },
124 [RTN_UNREACHABLE] = {
125 .error = -EHOSTUNREACH,
126 .scope = RT_SCOPE_UNIVERSE,
127 },
128 [RTN_PROHIBIT] = {
129 .error = -EACCES,
130 .scope = RT_SCOPE_UNIVERSE,
131 },
132 [RTN_THROW] = {
133 .error = -EAGAIN,
134 .scope = RT_SCOPE_UNIVERSE,
135 },
136 [RTN_NAT] = {
137 .error = -EINVAL,
138 .scope = RT_SCOPE_NOWHERE,
139 },
140 [RTN_XRESOLVE] = {
141 .error = -EINVAL,
142 .scope = RT_SCOPE_NOWHERE,
143 },
144 };
145
rt_fibinfo_free(struct rtable __rcu ** rtp)146 static void rt_fibinfo_free(struct rtable __rcu **rtp)
147 {
148 struct rtable *rt = rcu_dereference_protected(*rtp, 1);
149
150 if (!rt)
151 return;
152
153 /* Not even needed : RCU_INIT_POINTER(*rtp, NULL);
154 * because we waited an RCU grace period before calling
155 * free_fib_info_rcu()
156 */
157
158 dst_dev_put(&rt->dst);
159 dst_release_immediate(&rt->dst);
160 }
161
free_nh_exceptions(struct fib_nh_common * nhc)162 static void free_nh_exceptions(struct fib_nh_common *nhc)
163 {
164 struct fnhe_hash_bucket *hash;
165 int i;
166
167 hash = rcu_dereference_protected(nhc->nhc_exceptions, 1);
168 if (!hash)
169 return;
170 for (i = 0; i < FNHE_HASH_SIZE; i++) {
171 struct fib_nh_exception *fnhe;
172
173 fnhe = rcu_dereference_protected(hash[i].chain, 1);
174 while (fnhe) {
175 struct fib_nh_exception *next;
176
177 next = rcu_dereference_protected(fnhe->fnhe_next, 1);
178
179 rt_fibinfo_free(&fnhe->fnhe_rth_input);
180 rt_fibinfo_free(&fnhe->fnhe_rth_output);
181
182 kfree(fnhe);
183
184 fnhe = next;
185 }
186 }
187 kfree(hash);
188 }
189
rt_fibinfo_free_cpus(struct rtable __rcu * __percpu * rtp)190 static void rt_fibinfo_free_cpus(struct rtable __rcu * __percpu *rtp)
191 {
192 int cpu;
193
194 if (!rtp)
195 return;
196
197 for_each_possible_cpu(cpu) {
198 struct rtable *rt;
199
200 rt = rcu_dereference_protected(*per_cpu_ptr(rtp, cpu), 1);
201 if (rt) {
202 dst_dev_put(&rt->dst);
203 dst_release_immediate(&rt->dst);
204 }
205 }
206 free_percpu(rtp);
207 }
208
fib_nh_common_release(struct fib_nh_common * nhc)209 void fib_nh_common_release(struct fib_nh_common *nhc)
210 {
211 if (nhc->nhc_dev)
212 dev_put(nhc->nhc_dev);
213
214 lwtstate_put(nhc->nhc_lwtstate);
215 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
216 rt_fibinfo_free(&nhc->nhc_rth_input);
217 free_nh_exceptions(nhc);
218 }
219 EXPORT_SYMBOL_GPL(fib_nh_common_release);
220
fib_nh_release(struct net * net,struct fib_nh * fib_nh)221 void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
222 {
223 #ifdef CONFIG_IP_ROUTE_CLASSID
224 if (fib_nh->nh_tclassid)
225 net->ipv4.fib_num_tclassid_users--;
226 #endif
227 fib_nh_common_release(&fib_nh->nh_common);
228 }
229
230 /* Release a nexthop info record */
free_fib_info_rcu(struct rcu_head * head)231 static void free_fib_info_rcu(struct rcu_head *head)
232 {
233 struct fib_info *fi = container_of(head, struct fib_info, rcu);
234
235 if (fi->nh) {
236 nexthop_put(fi->nh);
237 } else {
238 change_nexthops(fi) {
239 fib_nh_release(fi->fib_net, nexthop_nh);
240 } endfor_nexthops(fi);
241 }
242
243 ip_fib_metrics_put(fi->fib_metrics);
244
245 kfree(fi);
246 }
247
free_fib_info(struct fib_info * fi)248 void free_fib_info(struct fib_info *fi)
249 {
250 if (fi->fib_dead == 0) {
251 pr_warn("Freeing alive fib_info %p\n", fi);
252 return;
253 }
254 fib_info_cnt--;
255
256 call_rcu(&fi->rcu, free_fib_info_rcu);
257 }
258 EXPORT_SYMBOL_GPL(free_fib_info);
259
fib_release_info(struct fib_info * fi)260 void fib_release_info(struct fib_info *fi)
261 {
262 spin_lock_bh(&fib_info_lock);
263 if (fi && --fi->fib_treeref == 0) {
264 hlist_del(&fi->fib_hash);
265 if (fi->fib_prefsrc)
266 hlist_del(&fi->fib_lhash);
267 if (fi->nh) {
268 list_del(&fi->nh_list);
269 } else {
270 change_nexthops(fi) {
271 if (!nexthop_nh->fib_nh_dev)
272 continue;
273 hlist_del(&nexthop_nh->nh_hash);
274 } endfor_nexthops(fi)
275 }
276 fi->fib_dead = 1;
277 fib_info_put(fi);
278 }
279 spin_unlock_bh(&fib_info_lock);
280 }
281
nh_comp(struct fib_info * fi,struct fib_info * ofi)282 static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi)
283 {
284 const struct fib_nh *onh;
285
286 if (fi->nh || ofi->nh)
287 return nexthop_cmp(fi->nh, ofi->nh) ? 0 : -1;
288
289 if (ofi->fib_nhs == 0)
290 return 0;
291
292 for_nexthops(fi) {
293 onh = fib_info_nh(ofi, nhsel);
294
295 if (nh->fib_nh_oif != onh->fib_nh_oif ||
296 nh->fib_nh_gw_family != onh->fib_nh_gw_family ||
297 nh->fib_nh_scope != onh->fib_nh_scope ||
298 #ifdef CONFIG_IP_ROUTE_MULTIPATH
299 nh->fib_nh_weight != onh->fib_nh_weight ||
300 #endif
301 #ifdef CONFIG_IP_ROUTE_CLASSID
302 nh->nh_tclassid != onh->nh_tclassid ||
303 #endif
304 lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
305 ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
306 return -1;
307
308 if (nh->fib_nh_gw_family == AF_INET &&
309 nh->fib_nh_gw4 != onh->fib_nh_gw4)
310 return -1;
311
312 if (nh->fib_nh_gw_family == AF_INET6 &&
313 ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6))
314 return -1;
315 } endfor_nexthops(fi);
316 return 0;
317 }
318
fib_devindex_hashfn(unsigned int val)319 static inline unsigned int fib_devindex_hashfn(unsigned int val)
320 {
321 unsigned int mask = DEVINDEX_HASHSIZE - 1;
322
323 return (val ^
324 (val >> DEVINDEX_HASHBITS) ^
325 (val >> (DEVINDEX_HASHBITS * 2))) & mask;
326 }
327
fib_info_hashfn_1(int init_val,u8 protocol,u8 scope,u32 prefsrc,u32 priority)328 static unsigned int fib_info_hashfn_1(int init_val, u8 protocol, u8 scope,
329 u32 prefsrc, u32 priority)
330 {
331 unsigned int val = init_val;
332
333 val ^= (protocol << 8) | scope;
334 val ^= prefsrc;
335 val ^= priority;
336
337 return val;
338 }
339
fib_info_hashfn_result(unsigned int val)340 static unsigned int fib_info_hashfn_result(unsigned int val)
341 {
342 unsigned int mask = (fib_info_hash_size - 1);
343
344 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
345 }
346
fib_info_hashfn(struct fib_info * fi)347 static inline unsigned int fib_info_hashfn(struct fib_info *fi)
348 {
349 unsigned int val;
350
351 val = fib_info_hashfn_1(fi->fib_nhs, fi->fib_protocol,
352 fi->fib_scope, (__force u32)fi->fib_prefsrc,
353 fi->fib_priority);
354
355 if (fi->nh) {
356 val ^= fib_devindex_hashfn(fi->nh->id);
357 } else {
358 for_nexthops(fi) {
359 val ^= fib_devindex_hashfn(nh->fib_nh_oif);
360 } endfor_nexthops(fi)
361 }
362
363 return fib_info_hashfn_result(val);
364 }
365
366 /* no metrics, only nexthop id */
fib_find_info_nh(struct net * net,const struct fib_config * cfg)367 static struct fib_info *fib_find_info_nh(struct net *net,
368 const struct fib_config *cfg)
369 {
370 struct hlist_head *head;
371 struct fib_info *fi;
372 unsigned int hash;
373
374 hash = fib_info_hashfn_1(fib_devindex_hashfn(cfg->fc_nh_id),
375 cfg->fc_protocol, cfg->fc_scope,
376 (__force u32)cfg->fc_prefsrc,
377 cfg->fc_priority);
378 hash = fib_info_hashfn_result(hash);
379 head = &fib_info_hash[hash];
380
381 hlist_for_each_entry(fi, head, fib_hash) {
382 if (!net_eq(fi->fib_net, net))
383 continue;
384 if (!fi->nh || fi->nh->id != cfg->fc_nh_id)
385 continue;
386 if (cfg->fc_protocol == fi->fib_protocol &&
387 cfg->fc_scope == fi->fib_scope &&
388 cfg->fc_prefsrc == fi->fib_prefsrc &&
389 cfg->fc_priority == fi->fib_priority &&
390 cfg->fc_type == fi->fib_type &&
391 cfg->fc_table == fi->fib_tb_id &&
392 !((cfg->fc_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK))
393 return fi;
394 }
395
396 return NULL;
397 }
398
fib_find_info(struct fib_info * nfi)399 static struct fib_info *fib_find_info(struct fib_info *nfi)
400 {
401 struct hlist_head *head;
402 struct fib_info *fi;
403 unsigned int hash;
404
405 hash = fib_info_hashfn(nfi);
406 head = &fib_info_hash[hash];
407
408 hlist_for_each_entry(fi, head, fib_hash) {
409 if (!net_eq(fi->fib_net, nfi->fib_net))
410 continue;
411 if (fi->fib_nhs != nfi->fib_nhs)
412 continue;
413 if (nfi->fib_protocol == fi->fib_protocol &&
414 nfi->fib_scope == fi->fib_scope &&
415 nfi->fib_prefsrc == fi->fib_prefsrc &&
416 nfi->fib_priority == fi->fib_priority &&
417 nfi->fib_type == fi->fib_type &&
418 memcmp(nfi->fib_metrics, fi->fib_metrics,
419 sizeof(u32) * RTAX_MAX) == 0 &&
420 !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
421 nh_comp(fi, nfi) == 0)
422 return fi;
423 }
424
425 return NULL;
426 }
427
428 /* Check, that the gateway is already configured.
429 * Used only by redirect accept routine.
430 */
ip_fib_check_default(__be32 gw,struct net_device * dev)431 int ip_fib_check_default(__be32 gw, struct net_device *dev)
432 {
433 struct hlist_head *head;
434 struct fib_nh *nh;
435 unsigned int hash;
436
437 spin_lock(&fib_info_lock);
438
439 hash = fib_devindex_hashfn(dev->ifindex);
440 head = &fib_info_devhash[hash];
441 hlist_for_each_entry(nh, head, nh_hash) {
442 if (nh->fib_nh_dev == dev &&
443 nh->fib_nh_gw4 == gw &&
444 !(nh->fib_nh_flags & RTNH_F_DEAD)) {
445 spin_unlock(&fib_info_lock);
446 return 0;
447 }
448 }
449
450 spin_unlock(&fib_info_lock);
451
452 return -1;
453 }
454
fib_nlmsg_size(struct fib_info * fi)455 static inline size_t fib_nlmsg_size(struct fib_info *fi)
456 {
457 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
458 + nla_total_size(4) /* RTA_TABLE */
459 + nla_total_size(4) /* RTA_DST */
460 + nla_total_size(4) /* RTA_PRIORITY */
461 + nla_total_size(4) /* RTA_PREFSRC */
462 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
463 unsigned int nhs = fib_info_num_path(fi);
464
465 /* space for nested metrics */
466 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
467
468 if (fi->nh)
469 payload += nla_total_size(4); /* RTA_NH_ID */
470
471 if (nhs) {
472 size_t nh_encapsize = 0;
473 /* Also handles the special case nhs == 1 */
474
475 /* each nexthop is packed in an attribute */
476 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
477 unsigned int i;
478
479 /* may contain flow and gateway attribute */
480 nhsize += 2 * nla_total_size(4);
481
482 /* grab encap info */
483 for (i = 0; i < fib_info_num_path(fi); i++) {
484 struct fib_nh_common *nhc = fib_info_nhc(fi, i);
485
486 if (nhc->nhc_lwtstate) {
487 /* RTA_ENCAP_TYPE */
488 nh_encapsize += lwtunnel_get_encap_size(
489 nhc->nhc_lwtstate);
490 /* RTA_ENCAP */
491 nh_encapsize += nla_total_size(2);
492 }
493 }
494
495 /* all nexthops are packed in a nested attribute */
496 payload += nla_total_size((nhs * nhsize) + nh_encapsize);
497
498 }
499
500 return payload;
501 }
502
rtmsg_fib(int event,__be32 key,struct fib_alias * fa,int dst_len,u32 tb_id,const struct nl_info * info,unsigned int nlm_flags)503 void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
504 int dst_len, u32 tb_id, const struct nl_info *info,
505 unsigned int nlm_flags)
506 {
507 struct fib_rt_info fri;
508 struct sk_buff *skb;
509 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
510 int err = -ENOBUFS;
511
512 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
513 if (!skb)
514 goto errout;
515
516 fri.fi = fa->fa_info;
517 fri.tb_id = tb_id;
518 fri.dst = key;
519 fri.dst_len = dst_len;
520 fri.tos = fa->fa_tos;
521 fri.type = fa->fa_type;
522 fri.offload = fa->offload;
523 fri.trap = fa->trap;
524 err = fib_dump_info(skb, info->portid, seq, event, &fri, nlm_flags);
525 if (err < 0) {
526 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
527 WARN_ON(err == -EMSGSIZE);
528 kfree_skb(skb);
529 goto errout;
530 }
531 rtnl_notify(skb, info->nl_net, info->portid, RTNLGRP_IPV4_ROUTE,
532 info->nlh, GFP_KERNEL);
533 return;
534 errout:
535 if (err < 0)
536 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
537 }
538
fib_detect_death(struct fib_info * fi,int order,struct fib_info ** last_resort,int * last_idx,int dflt)539 static int fib_detect_death(struct fib_info *fi, int order,
540 struct fib_info **last_resort, int *last_idx,
541 int dflt)
542 {
543 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
544 struct neighbour *n;
545 int state = NUD_NONE;
546
547 if (likely(nhc->nhc_gw_family == AF_INET))
548 n = neigh_lookup(&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev);
549 else if (nhc->nhc_gw_family == AF_INET6)
550 n = neigh_lookup(ipv6_stub->nd_tbl, &nhc->nhc_gw.ipv6,
551 nhc->nhc_dev);
552 else
553 n = NULL;
554
555 if (n) {
556 state = n->nud_state;
557 neigh_release(n);
558 } else {
559 return 0;
560 }
561 if (state == NUD_REACHABLE)
562 return 0;
563 if ((state & NUD_VALID) && order != dflt)
564 return 0;
565 if ((state & NUD_VALID) ||
566 (*last_idx < 0 && order > dflt && state != NUD_INCOMPLETE)) {
567 *last_resort = fi;
568 *last_idx = order;
569 }
570 return 1;
571 }
572
fib_nh_common_init(struct net * net,struct fib_nh_common * nhc,struct nlattr * encap,u16 encap_type,void * cfg,gfp_t gfp_flags,struct netlink_ext_ack * extack)573 int fib_nh_common_init(struct net *net, struct fib_nh_common *nhc,
574 struct nlattr *encap, u16 encap_type,
575 void *cfg, gfp_t gfp_flags,
576 struct netlink_ext_ack *extack)
577 {
578 int err;
579
580 nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *,
581 gfp_flags);
582 if (!nhc->nhc_pcpu_rth_output)
583 return -ENOMEM;
584
585 if (encap) {
586 struct lwtunnel_state *lwtstate;
587
588 if (encap_type == LWTUNNEL_ENCAP_NONE) {
589 NL_SET_ERR_MSG(extack, "LWT encap type not specified");
590 err = -EINVAL;
591 goto lwt_failure;
592 }
593 err = lwtunnel_build_state(net, encap_type, encap,
594 nhc->nhc_family, cfg, &lwtstate,
595 extack);
596 if (err)
597 goto lwt_failure;
598
599 nhc->nhc_lwtstate = lwtstate_get(lwtstate);
600 }
601
602 return 0;
603
604 lwt_failure:
605 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
606 nhc->nhc_pcpu_rth_output = NULL;
607 return err;
608 }
609 EXPORT_SYMBOL_GPL(fib_nh_common_init);
610
fib_nh_init(struct net * net,struct fib_nh * nh,struct fib_config * cfg,int nh_weight,struct netlink_ext_ack * extack)611 int fib_nh_init(struct net *net, struct fib_nh *nh,
612 struct fib_config *cfg, int nh_weight,
613 struct netlink_ext_ack *extack)
614 {
615 int err;
616
617 nh->fib_nh_family = AF_INET;
618
619 err = fib_nh_common_init(net, &nh->nh_common, cfg->fc_encap,
620 cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
621 if (err)
622 return err;
623
624 nh->fib_nh_oif = cfg->fc_oif;
625 nh->fib_nh_gw_family = cfg->fc_gw_family;
626 if (cfg->fc_gw_family == AF_INET)
627 nh->fib_nh_gw4 = cfg->fc_gw4;
628 else if (cfg->fc_gw_family == AF_INET6)
629 nh->fib_nh_gw6 = cfg->fc_gw6;
630
631 nh->fib_nh_flags = cfg->fc_flags;
632
633 #ifdef CONFIG_IP_ROUTE_CLASSID
634 nh->nh_tclassid = cfg->fc_flow;
635 if (nh->nh_tclassid)
636 net->ipv4.fib_num_tclassid_users++;
637 #endif
638 #ifdef CONFIG_IP_ROUTE_MULTIPATH
639 nh->fib_nh_weight = nh_weight;
640 #endif
641 return 0;
642 }
643
644 #ifdef CONFIG_IP_ROUTE_MULTIPATH
645
fib_count_nexthops(struct rtnexthop * rtnh,int remaining,struct netlink_ext_ack * extack)646 static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining,
647 struct netlink_ext_ack *extack)
648 {
649 int nhs = 0;
650
651 while (rtnh_ok(rtnh, remaining)) {
652 nhs++;
653 rtnh = rtnh_next(rtnh, &remaining);
654 }
655
656 /* leftover implies invalid nexthop configuration, discard it */
657 if (remaining > 0) {
658 NL_SET_ERR_MSG(extack,
659 "Invalid nexthop configuration - extra data after nexthops");
660 nhs = 0;
661 }
662
663 return nhs;
664 }
665
666 /* only called when fib_nh is integrated into fib_info */
fib_get_nhs(struct fib_info * fi,struct rtnexthop * rtnh,int remaining,struct fib_config * cfg,struct netlink_ext_ack * extack)667 static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
668 int remaining, struct fib_config *cfg,
669 struct netlink_ext_ack *extack)
670 {
671 struct net *net = fi->fib_net;
672 struct fib_config fib_cfg;
673 struct fib_nh *nh;
674 int ret;
675
676 change_nexthops(fi) {
677 int attrlen;
678
679 memset(&fib_cfg, 0, sizeof(fib_cfg));
680
681 if (!rtnh_ok(rtnh, remaining)) {
682 NL_SET_ERR_MSG(extack,
683 "Invalid nexthop configuration - extra data after nexthop");
684 return -EINVAL;
685 }
686
687 if (rtnh->rtnh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
688 NL_SET_ERR_MSG(extack,
689 "Invalid flags for nexthop - can not contain DEAD or LINKDOWN");
690 return -EINVAL;
691 }
692
693 fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
694 fib_cfg.fc_oif = rtnh->rtnh_ifindex;
695
696 attrlen = rtnh_attrlen(rtnh);
697 if (attrlen > 0) {
698 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
699
700 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
701 nlav = nla_find(attrs, attrlen, RTA_VIA);
702 if (nla && nlav) {
703 NL_SET_ERR_MSG(extack,
704 "Nexthop configuration can not contain both GATEWAY and VIA");
705 return -EINVAL;
706 }
707 if (nla) {
708 fib_cfg.fc_gw4 = nla_get_in_addr(nla);
709 if (fib_cfg.fc_gw4)
710 fib_cfg.fc_gw_family = AF_INET;
711 } else if (nlav) {
712 ret = fib_gw_from_via(&fib_cfg, nlav, extack);
713 if (ret)
714 goto errout;
715 }
716
717 nla = nla_find(attrs, attrlen, RTA_FLOW);
718 if (nla)
719 fib_cfg.fc_flow = nla_get_u32(nla);
720
721 fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
722 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
723 if (nla)
724 fib_cfg.fc_encap_type = nla_get_u16(nla);
725 }
726
727 ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
728 rtnh->rtnh_hops + 1, extack);
729 if (ret)
730 goto errout;
731
732 rtnh = rtnh_next(rtnh, &remaining);
733 } endfor_nexthops(fi);
734
735 ret = -EINVAL;
736 nh = fib_info_nh(fi, 0);
737 if (cfg->fc_oif && nh->fib_nh_oif != cfg->fc_oif) {
738 NL_SET_ERR_MSG(extack,
739 "Nexthop device index does not match RTA_OIF");
740 goto errout;
741 }
742 if (cfg->fc_gw_family) {
743 if (cfg->fc_gw_family != nh->fib_nh_gw_family ||
744 (cfg->fc_gw_family == AF_INET &&
745 nh->fib_nh_gw4 != cfg->fc_gw4) ||
746 (cfg->fc_gw_family == AF_INET6 &&
747 ipv6_addr_cmp(&nh->fib_nh_gw6, &cfg->fc_gw6))) {
748 NL_SET_ERR_MSG(extack,
749 "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA");
750 goto errout;
751 }
752 }
753 #ifdef CONFIG_IP_ROUTE_CLASSID
754 if (cfg->fc_flow && nh->nh_tclassid != cfg->fc_flow) {
755 NL_SET_ERR_MSG(extack,
756 "Nexthop class id does not match RTA_FLOW");
757 goto errout;
758 }
759 #endif
760 ret = 0;
761 errout:
762 return ret;
763 }
764
765 /* only called when fib_nh is integrated into fib_info */
fib_rebalance(struct fib_info * fi)766 static void fib_rebalance(struct fib_info *fi)
767 {
768 int total;
769 int w;
770
771 if (fib_info_num_path(fi) < 2)
772 return;
773
774 total = 0;
775 for_nexthops(fi) {
776 if (nh->fib_nh_flags & RTNH_F_DEAD)
777 continue;
778
779 if (ip_ignore_linkdown(nh->fib_nh_dev) &&
780 nh->fib_nh_flags & RTNH_F_LINKDOWN)
781 continue;
782
783 total += nh->fib_nh_weight;
784 } endfor_nexthops(fi);
785
786 w = 0;
787 change_nexthops(fi) {
788 int upper_bound;
789
790 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
791 upper_bound = -1;
792 } else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
793 nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
794 upper_bound = -1;
795 } else {
796 w += nexthop_nh->fib_nh_weight;
797 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
798 total) - 1;
799 }
800
801 atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
802 } endfor_nexthops(fi);
803 }
804 #else /* CONFIG_IP_ROUTE_MULTIPATH */
805
fib_get_nhs(struct fib_info * fi,struct rtnexthop * rtnh,int remaining,struct fib_config * cfg,struct netlink_ext_ack * extack)806 static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
807 int remaining, struct fib_config *cfg,
808 struct netlink_ext_ack *extack)
809 {
810 NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
811
812 return -EINVAL;
813 }
814
815 #define fib_rebalance(fi) do { } while (0)
816
817 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
818
fib_encap_match(struct net * net,u16 encap_type,struct nlattr * encap,const struct fib_nh * nh,const struct fib_config * cfg,struct netlink_ext_ack * extack)819 static int fib_encap_match(struct net *net, u16 encap_type,
820 struct nlattr *encap,
821 const struct fib_nh *nh,
822 const struct fib_config *cfg,
823 struct netlink_ext_ack *extack)
824 {
825 struct lwtunnel_state *lwtstate;
826 int ret, result = 0;
827
828 if (encap_type == LWTUNNEL_ENCAP_NONE)
829 return 0;
830
831 ret = lwtunnel_build_state(net, encap_type, encap, AF_INET,
832 cfg, &lwtstate, extack);
833 if (!ret) {
834 result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
835 lwtstate_free(lwtstate);
836 }
837
838 return result;
839 }
840
fib_nh_match(struct net * net,struct fib_config * cfg,struct fib_info * fi,struct netlink_ext_ack * extack)841 int fib_nh_match(struct net *net, struct fib_config *cfg, struct fib_info *fi,
842 struct netlink_ext_ack *extack)
843 {
844 #ifdef CONFIG_IP_ROUTE_MULTIPATH
845 struct rtnexthop *rtnh;
846 int remaining;
847 #endif
848
849 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
850 return 1;
851
852 if (cfg->fc_nh_id) {
853 if (fi->nh && cfg->fc_nh_id == fi->nh->id)
854 return 0;
855 return 1;
856 }
857
858 /* cannot match on nexthop object attributes */
859 if (fi->nh)
860 return 1;
861
862 if (cfg->fc_oif || cfg->fc_gw_family) {
863 struct fib_nh *nh;
864
865 nh = fib_info_nh(fi, 0);
866 if (cfg->fc_encap) {
867 if (fib_encap_match(net, cfg->fc_encap_type,
868 cfg->fc_encap, nh, cfg, extack))
869 return 1;
870 }
871 #ifdef CONFIG_IP_ROUTE_CLASSID
872 if (cfg->fc_flow &&
873 cfg->fc_flow != nh->nh_tclassid)
874 return 1;
875 #endif
876 if ((cfg->fc_oif && cfg->fc_oif != nh->fib_nh_oif) ||
877 (cfg->fc_gw_family &&
878 cfg->fc_gw_family != nh->fib_nh_gw_family))
879 return 1;
880
881 if (cfg->fc_gw_family == AF_INET &&
882 cfg->fc_gw4 != nh->fib_nh_gw4)
883 return 1;
884
885 if (cfg->fc_gw_family == AF_INET6 &&
886 ipv6_addr_cmp(&cfg->fc_gw6, &nh->fib_nh_gw6))
887 return 1;
888
889 return 0;
890 }
891
892 #ifdef CONFIG_IP_ROUTE_MULTIPATH
893 if (!cfg->fc_mp)
894 return 0;
895
896 rtnh = cfg->fc_mp;
897 remaining = cfg->fc_mp_len;
898
899 for_nexthops(fi) {
900 int attrlen;
901
902 if (!rtnh_ok(rtnh, remaining))
903 return -EINVAL;
904
905 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
906 return 1;
907
908 attrlen = rtnh_attrlen(rtnh);
909 if (attrlen > 0) {
910 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
911
912 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
913 nlav = nla_find(attrs, attrlen, RTA_VIA);
914 if (nla && nlav) {
915 NL_SET_ERR_MSG(extack,
916 "Nexthop configuration can not contain both GATEWAY and VIA");
917 return -EINVAL;
918 }
919
920 if (nla) {
921 if (nh->fib_nh_gw_family != AF_INET ||
922 nla_get_in_addr(nla) != nh->fib_nh_gw4)
923 return 1;
924 } else if (nlav) {
925 struct fib_config cfg2;
926 int err;
927
928 err = fib_gw_from_via(&cfg2, nlav, extack);
929 if (err)
930 return err;
931
932 switch (nh->fib_nh_gw_family) {
933 case AF_INET:
934 if (cfg2.fc_gw_family != AF_INET ||
935 cfg2.fc_gw4 != nh->fib_nh_gw4)
936 return 1;
937 break;
938 case AF_INET6:
939 if (cfg2.fc_gw_family != AF_INET6 ||
940 ipv6_addr_cmp(&cfg2.fc_gw6,
941 &nh->fib_nh_gw6))
942 return 1;
943 break;
944 }
945 }
946
947 #ifdef CONFIG_IP_ROUTE_CLASSID
948 nla = nla_find(attrs, attrlen, RTA_FLOW);
949 if (nla && nla_get_u32(nla) != nh->nh_tclassid)
950 return 1;
951 #endif
952 }
953
954 rtnh = rtnh_next(rtnh, &remaining);
955 } endfor_nexthops(fi);
956 #endif
957 return 0;
958 }
959
fib_metrics_match(struct fib_config * cfg,struct fib_info * fi)960 bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
961 {
962 struct nlattr *nla;
963 int remaining;
964
965 if (!cfg->fc_mx)
966 return true;
967
968 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
969 int type = nla_type(nla);
970 u32 fi_val, val;
971
972 if (!type)
973 continue;
974 if (type > RTAX_MAX)
975 return false;
976
977 if (type == RTAX_CC_ALGO) {
978 char tmp[TCP_CA_NAME_MAX];
979 bool ecn_ca = false;
980
981 nla_strlcpy(tmp, nla, sizeof(tmp));
982 val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
983 } else {
984 if (nla_len(nla) != sizeof(u32))
985 return false;
986 val = nla_get_u32(nla);
987 }
988
989 fi_val = fi->fib_metrics->metrics[type - 1];
990 if (type == RTAX_FEATURES)
991 fi_val &= ~DST_FEATURE_ECN_CA;
992
993 if (fi_val != val)
994 return false;
995 }
996
997 return true;
998 }
999
fib_check_nh_v6_gw(struct net * net,struct fib_nh * nh,u32 table,struct netlink_ext_ack * extack)1000 static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh,
1001 u32 table, struct netlink_ext_ack *extack)
1002 {
1003 struct fib6_config cfg = {
1004 .fc_table = table,
1005 .fc_flags = nh->fib_nh_flags | RTF_GATEWAY,
1006 .fc_ifindex = nh->fib_nh_oif,
1007 .fc_gateway = nh->fib_nh_gw6,
1008 };
1009 struct fib6_nh fib6_nh = {};
1010 int err;
1011
1012 err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack);
1013 if (!err) {
1014 nh->fib_nh_dev = fib6_nh.fib_nh_dev;
1015 dev_hold(nh->fib_nh_dev);
1016 nh->fib_nh_oif = nh->fib_nh_dev->ifindex;
1017 nh->fib_nh_scope = RT_SCOPE_LINK;
1018
1019 ipv6_stub->fib6_nh_release(&fib6_nh);
1020 }
1021
1022 return err;
1023 }
1024
1025 /*
1026 * Picture
1027 * -------
1028 *
1029 * Semantics of nexthop is very messy by historical reasons.
1030 * We have to take into account, that:
1031 * a) gateway can be actually local interface address,
1032 * so that gatewayed route is direct.
1033 * b) gateway must be on-link address, possibly
1034 * described not by an ifaddr, but also by a direct route.
1035 * c) If both gateway and interface are specified, they should not
1036 * contradict.
1037 * d) If we use tunnel routes, gateway could be not on-link.
1038 *
1039 * Attempt to reconcile all of these (alas, self-contradictory) conditions
1040 * results in pretty ugly and hairy code with obscure logic.
1041 *
1042 * I chose to generalized it instead, so that the size
1043 * of code does not increase practically, but it becomes
1044 * much more general.
1045 * Every prefix is assigned a "scope" value: "host" is local address,
1046 * "link" is direct route,
1047 * [ ... "site" ... "interior" ... ]
1048 * and "universe" is true gateway route with global meaning.
1049 *
1050 * Every prefix refers to a set of "nexthop"s (gw, oif),
1051 * where gw must have narrower scope. This recursion stops
1052 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
1053 * which means that gw is forced to be on link.
1054 *
1055 * Code is still hairy, but now it is apparently logically
1056 * consistent and very flexible. F.e. as by-product it allows
1057 * to co-exists in peace independent exterior and interior
1058 * routing processes.
1059 *
1060 * Normally it looks as following.
1061 *
1062 * {universe prefix} -> (gw, oif) [scope link]
1063 * |
1064 * |-> {link prefix} -> (gw, oif) [scope local]
1065 * |
1066 * |-> {local prefix} (terminal node)
1067 */
fib_check_nh_v4_gw(struct net * net,struct fib_nh * nh,u32 table,u8 scope,struct netlink_ext_ack * extack)1068 static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
1069 u8 scope, struct netlink_ext_ack *extack)
1070 {
1071 struct net_device *dev;
1072 struct fib_result res;
1073 int err = 0;
1074
1075 if (nh->fib_nh_flags & RTNH_F_ONLINK) {
1076 unsigned int addr_type;
1077
1078 if (scope >= RT_SCOPE_LINK) {
1079 NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
1080 return -EINVAL;
1081 }
1082 dev = __dev_get_by_index(net, nh->fib_nh_oif);
1083 if (!dev) {
1084 NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
1085 return -ENODEV;
1086 }
1087 if (!(dev->flags & IFF_UP)) {
1088 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
1089 return -ENETDOWN;
1090 }
1091 addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
1092 if (addr_type != RTN_UNICAST) {
1093 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1094 return -EINVAL;
1095 }
1096 if (!netif_carrier_ok(dev))
1097 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1098 nh->fib_nh_dev = dev;
1099 dev_hold(dev);
1100 nh->fib_nh_scope = RT_SCOPE_LINK;
1101 return 0;
1102 }
1103 rcu_read_lock();
1104 {
1105 struct fib_table *tbl = NULL;
1106 struct flowi4 fl4 = {
1107 .daddr = nh->fib_nh_gw4,
1108 .flowi4_scope = scope + 1,
1109 .flowi4_oif = nh->fib_nh_oif,
1110 .flowi4_iif = LOOPBACK_IFINDEX,
1111 };
1112
1113 /* It is not necessary, but requires a bit of thinking */
1114 if (fl4.flowi4_scope < RT_SCOPE_LINK)
1115 fl4.flowi4_scope = RT_SCOPE_LINK;
1116
1117 if (table && table != RT_TABLE_MAIN)
1118 tbl = fib_get_table(net, table);
1119
1120 if (tbl)
1121 err = fib_table_lookup(tbl, &fl4, &res,
1122 FIB_LOOKUP_IGNORE_LINKSTATE |
1123 FIB_LOOKUP_NOREF);
1124
1125 /* on error or if no table given do full lookup. This
1126 * is needed for example when nexthops are in the local
1127 * table rather than the given table
1128 */
1129 if (!tbl || err) {
1130 err = fib_lookup(net, &fl4, &res,
1131 FIB_LOOKUP_IGNORE_LINKSTATE);
1132 }
1133
1134 if (err) {
1135 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1136 goto out;
1137 }
1138 }
1139
1140 err = -EINVAL;
1141 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
1142 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1143 goto out;
1144 }
1145 nh->fib_nh_scope = res.scope;
1146 nh->fib_nh_oif = FIB_RES_OIF(res);
1147 nh->fib_nh_dev = dev = FIB_RES_DEV(res);
1148 if (!dev) {
1149 NL_SET_ERR_MSG(extack,
1150 "No egress device for nexthop gateway");
1151 goto out;
1152 }
1153 dev_hold(dev);
1154 if (!netif_carrier_ok(dev))
1155 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1156 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
1157 out:
1158 rcu_read_unlock();
1159 return err;
1160 }
1161
fib_check_nh_nongw(struct net * net,struct fib_nh * nh,struct netlink_ext_ack * extack)1162 static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
1163 struct netlink_ext_ack *extack)
1164 {
1165 struct in_device *in_dev;
1166 int err;
1167
1168 if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
1169 NL_SET_ERR_MSG(extack,
1170 "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
1171 return -EINVAL;
1172 }
1173
1174 rcu_read_lock();
1175
1176 err = -ENODEV;
1177 in_dev = inetdev_by_index(net, nh->fib_nh_oif);
1178 if (!in_dev)
1179 goto out;
1180 err = -ENETDOWN;
1181 if (!(in_dev->dev->flags & IFF_UP)) {
1182 NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
1183 goto out;
1184 }
1185
1186 nh->fib_nh_dev = in_dev->dev;
1187 dev_hold(nh->fib_nh_dev);
1188 nh->fib_nh_scope = RT_SCOPE_HOST;
1189 if (!netif_carrier_ok(nh->fib_nh_dev))
1190 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1191 err = 0;
1192 out:
1193 rcu_read_unlock();
1194 return err;
1195 }
1196
fib_check_nh(struct net * net,struct fib_nh * nh,u32 table,u8 scope,struct netlink_ext_ack * extack)1197 int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
1198 struct netlink_ext_ack *extack)
1199 {
1200 int err;
1201
1202 if (nh->fib_nh_gw_family == AF_INET)
1203 err = fib_check_nh_v4_gw(net, nh, table, scope, extack);
1204 else if (nh->fib_nh_gw_family == AF_INET6)
1205 err = fib_check_nh_v6_gw(net, nh, table, extack);
1206 else
1207 err = fib_check_nh_nongw(net, nh, extack);
1208
1209 return err;
1210 }
1211
fib_laddr_hashfn(__be32 val)1212 static inline unsigned int fib_laddr_hashfn(__be32 val)
1213 {
1214 unsigned int mask = (fib_info_hash_size - 1);
1215
1216 return ((__force u32)val ^
1217 ((__force u32)val >> 7) ^
1218 ((__force u32)val >> 14)) & mask;
1219 }
1220
fib_info_hash_alloc(int bytes)1221 static struct hlist_head *fib_info_hash_alloc(int bytes)
1222 {
1223 if (bytes <= PAGE_SIZE)
1224 return kzalloc(bytes, GFP_KERNEL);
1225 else
1226 return (struct hlist_head *)
1227 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1228 get_order(bytes));
1229 }
1230
fib_info_hash_free(struct hlist_head * hash,int bytes)1231 static void fib_info_hash_free(struct hlist_head *hash, int bytes)
1232 {
1233 if (!hash)
1234 return;
1235
1236 if (bytes <= PAGE_SIZE)
1237 kfree(hash);
1238 else
1239 free_pages((unsigned long) hash, get_order(bytes));
1240 }
1241
fib_info_hash_move(struct hlist_head * new_info_hash,struct hlist_head * new_laddrhash,unsigned int new_size)1242 static void fib_info_hash_move(struct hlist_head *new_info_hash,
1243 struct hlist_head *new_laddrhash,
1244 unsigned int new_size)
1245 {
1246 struct hlist_head *old_info_hash, *old_laddrhash;
1247 unsigned int old_size = fib_info_hash_size;
1248 unsigned int i, bytes;
1249
1250 spin_lock_bh(&fib_info_lock);
1251 old_info_hash = fib_info_hash;
1252 old_laddrhash = fib_info_laddrhash;
1253 fib_info_hash_size = new_size;
1254
1255 for (i = 0; i < old_size; i++) {
1256 struct hlist_head *head = &fib_info_hash[i];
1257 struct hlist_node *n;
1258 struct fib_info *fi;
1259
1260 hlist_for_each_entry_safe(fi, n, head, fib_hash) {
1261 struct hlist_head *dest;
1262 unsigned int new_hash;
1263
1264 new_hash = fib_info_hashfn(fi);
1265 dest = &new_info_hash[new_hash];
1266 hlist_add_head(&fi->fib_hash, dest);
1267 }
1268 }
1269 fib_info_hash = new_info_hash;
1270
1271 for (i = 0; i < old_size; i++) {
1272 struct hlist_head *lhead = &fib_info_laddrhash[i];
1273 struct hlist_node *n;
1274 struct fib_info *fi;
1275
1276 hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
1277 struct hlist_head *ldest;
1278 unsigned int new_hash;
1279
1280 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
1281 ldest = &new_laddrhash[new_hash];
1282 hlist_add_head(&fi->fib_lhash, ldest);
1283 }
1284 }
1285 fib_info_laddrhash = new_laddrhash;
1286
1287 spin_unlock_bh(&fib_info_lock);
1288
1289 bytes = old_size * sizeof(struct hlist_head *);
1290 fib_info_hash_free(old_info_hash, bytes);
1291 fib_info_hash_free(old_laddrhash, bytes);
1292 }
1293
fib_info_update_nhc_saddr(struct net * net,struct fib_nh_common * nhc,unsigned char scope)1294 __be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
1295 unsigned char scope)
1296 {
1297 struct fib_nh *nh;
1298
1299 if (nhc->nhc_family != AF_INET)
1300 return inet_select_addr(nhc->nhc_dev, 0, scope);
1301
1302 nh = container_of(nhc, struct fib_nh, nh_common);
1303 nh->nh_saddr = inet_select_addr(nh->fib_nh_dev, nh->fib_nh_gw4, scope);
1304 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
1305
1306 return nh->nh_saddr;
1307 }
1308
fib_result_prefsrc(struct net * net,struct fib_result * res)1309 __be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1310 {
1311 struct fib_nh_common *nhc = res->nhc;
1312
1313 if (res->fi->fib_prefsrc)
1314 return res->fi->fib_prefsrc;
1315
1316 if (nhc->nhc_family == AF_INET) {
1317 struct fib_nh *nh;
1318
1319 nh = container_of(nhc, struct fib_nh, nh_common);
1320 if (nh->nh_saddr_genid == atomic_read(&net->ipv4.dev_addr_genid))
1321 return nh->nh_saddr;
1322 }
1323
1324 return fib_info_update_nhc_saddr(net, nhc, res->fi->fib_scope);
1325 }
1326
fib_valid_prefsrc(struct fib_config * cfg,__be32 fib_prefsrc)1327 static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1328 {
1329 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1330 fib_prefsrc != cfg->fc_dst) {
1331 u32 tb_id = cfg->fc_table;
1332 int rc;
1333
1334 if (tb_id == RT_TABLE_MAIN)
1335 tb_id = RT_TABLE_LOCAL;
1336
1337 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1338 fib_prefsrc, tb_id);
1339
1340 if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1341 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1342 fib_prefsrc, RT_TABLE_LOCAL);
1343 }
1344
1345 if (rc != RTN_LOCAL)
1346 return false;
1347 }
1348 return true;
1349 }
1350
fib_create_info(struct fib_config * cfg,struct netlink_ext_ack * extack)1351 struct fib_info *fib_create_info(struct fib_config *cfg,
1352 struct netlink_ext_ack *extack)
1353 {
1354 int err;
1355 struct fib_info *fi = NULL;
1356 struct nexthop *nh = NULL;
1357 struct fib_info *ofi;
1358 int nhs = 1;
1359 struct net *net = cfg->fc_nlinfo.nl_net;
1360
1361 if (cfg->fc_type > RTN_MAX)
1362 goto err_inval;
1363
1364 /* Fast check to catch the most weird cases */
1365 if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1366 NL_SET_ERR_MSG(extack, "Invalid scope");
1367 goto err_inval;
1368 }
1369
1370 if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1371 NL_SET_ERR_MSG(extack,
1372 "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
1373 goto err_inval;
1374 }
1375
1376 if (cfg->fc_nh_id) {
1377 if (!cfg->fc_mx) {
1378 fi = fib_find_info_nh(net, cfg);
1379 if (fi) {
1380 fi->fib_treeref++;
1381 return fi;
1382 }
1383 }
1384
1385 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
1386 if (!nh) {
1387 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
1388 goto err_inval;
1389 }
1390 nhs = 0;
1391 }
1392
1393 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1394 if (cfg->fc_mp) {
1395 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
1396 if (nhs == 0)
1397 goto err_inval;
1398 }
1399 #endif
1400
1401 err = -ENOBUFS;
1402 if (fib_info_cnt >= fib_info_hash_size) {
1403 unsigned int new_size = fib_info_hash_size << 1;
1404 struct hlist_head *new_info_hash;
1405 struct hlist_head *new_laddrhash;
1406 unsigned int bytes;
1407
1408 if (!new_size)
1409 new_size = 16;
1410 bytes = new_size * sizeof(struct hlist_head *);
1411 new_info_hash = fib_info_hash_alloc(bytes);
1412 new_laddrhash = fib_info_hash_alloc(bytes);
1413 if (!new_info_hash || !new_laddrhash) {
1414 fib_info_hash_free(new_info_hash, bytes);
1415 fib_info_hash_free(new_laddrhash, bytes);
1416 } else
1417 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
1418
1419 if (!fib_info_hash_size)
1420 goto failure;
1421 }
1422
1423 fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
1424 if (!fi)
1425 goto failure;
1426 fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx,
1427 cfg->fc_mx_len, extack);
1428 if (IS_ERR(fi->fib_metrics)) {
1429 err = PTR_ERR(fi->fib_metrics);
1430 kfree(fi);
1431 return ERR_PTR(err);
1432 }
1433
1434 fib_info_cnt++;
1435 fi->fib_net = net;
1436 fi->fib_protocol = cfg->fc_protocol;
1437 fi->fib_scope = cfg->fc_scope;
1438 fi->fib_flags = cfg->fc_flags;
1439 fi->fib_priority = cfg->fc_priority;
1440 fi->fib_prefsrc = cfg->fc_prefsrc;
1441 fi->fib_type = cfg->fc_type;
1442 fi->fib_tb_id = cfg->fc_table;
1443
1444 fi->fib_nhs = nhs;
1445 if (nh) {
1446 if (!nexthop_get(nh)) {
1447 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
1448 err = -EINVAL;
1449 } else {
1450 err = 0;
1451 fi->nh = nh;
1452 }
1453 } else {
1454 change_nexthops(fi) {
1455 nexthop_nh->nh_parent = fi;
1456 } endfor_nexthops(fi)
1457
1458 if (cfg->fc_mp)
1459 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg,
1460 extack);
1461 else
1462 err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
1463 }
1464
1465 if (err != 0)
1466 goto failure;
1467
1468 if (fib_props[cfg->fc_type].error) {
1469 if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
1470 NL_SET_ERR_MSG(extack,
1471 "Gateway, device and multipath can not be specified for this route type");
1472 goto err_inval;
1473 }
1474 goto link_it;
1475 } else {
1476 switch (cfg->fc_type) {
1477 case RTN_UNICAST:
1478 case RTN_LOCAL:
1479 case RTN_BROADCAST:
1480 case RTN_ANYCAST:
1481 case RTN_MULTICAST:
1482 break;
1483 default:
1484 NL_SET_ERR_MSG(extack, "Invalid route type");
1485 goto err_inval;
1486 }
1487 }
1488
1489 if (cfg->fc_scope > RT_SCOPE_HOST) {
1490 NL_SET_ERR_MSG(extack, "Invalid scope");
1491 goto err_inval;
1492 }
1493
1494 if (fi->nh) {
1495 err = fib_check_nexthop(fi->nh, cfg->fc_scope, extack);
1496 if (err)
1497 goto failure;
1498 } else if (cfg->fc_scope == RT_SCOPE_HOST) {
1499 struct fib_nh *nh = fi->fib_nh;
1500
1501 /* Local address is added. */
1502 if (nhs != 1) {
1503 NL_SET_ERR_MSG(extack,
1504 "Route with host scope can not have multiple nexthops");
1505 goto err_inval;
1506 }
1507 if (nh->fib_nh_gw_family) {
1508 NL_SET_ERR_MSG(extack,
1509 "Route with host scope can not have a gateway");
1510 goto err_inval;
1511 }
1512 nh->fib_nh_scope = RT_SCOPE_NOWHERE;
1513 nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif);
1514 err = -ENODEV;
1515 if (!nh->fib_nh_dev)
1516 goto failure;
1517 } else {
1518 int linkdown = 0;
1519
1520 change_nexthops(fi) {
1521 err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh,
1522 cfg->fc_table, cfg->fc_scope,
1523 extack);
1524 if (err != 0)
1525 goto failure;
1526 if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
1527 linkdown++;
1528 } endfor_nexthops(fi)
1529 if (linkdown == fi->fib_nhs)
1530 fi->fib_flags |= RTNH_F_LINKDOWN;
1531 }
1532
1533 if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1534 NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
1535 goto err_inval;
1536 }
1537
1538 if (!fi->nh) {
1539 change_nexthops(fi) {
1540 fib_info_update_nhc_saddr(net, &nexthop_nh->nh_common,
1541 fi->fib_scope);
1542 if (nexthop_nh->fib_nh_gw_family == AF_INET6)
1543 fi->fib_nh_is_v6 = true;
1544 } endfor_nexthops(fi)
1545
1546 fib_rebalance(fi);
1547 }
1548
1549 link_it:
1550 ofi = fib_find_info(fi);
1551 if (ofi) {
1552 fi->fib_dead = 1;
1553 free_fib_info(fi);
1554 ofi->fib_treeref++;
1555 return ofi;
1556 }
1557
1558 fi->fib_treeref++;
1559 refcount_set(&fi->fib_clntref, 1);
1560 spin_lock_bh(&fib_info_lock);
1561 hlist_add_head(&fi->fib_hash,
1562 &fib_info_hash[fib_info_hashfn(fi)]);
1563 if (fi->fib_prefsrc) {
1564 struct hlist_head *head;
1565
1566 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
1567 hlist_add_head(&fi->fib_lhash, head);
1568 }
1569 if (fi->nh) {
1570 list_add(&fi->nh_list, &nh->fi_list);
1571 } else {
1572 change_nexthops(fi) {
1573 struct hlist_head *head;
1574 unsigned int hash;
1575
1576 if (!nexthop_nh->fib_nh_dev)
1577 continue;
1578 hash = fib_devindex_hashfn(nexthop_nh->fib_nh_dev->ifindex);
1579 head = &fib_info_devhash[hash];
1580 hlist_add_head(&nexthop_nh->nh_hash, head);
1581 } endfor_nexthops(fi)
1582 }
1583 spin_unlock_bh(&fib_info_lock);
1584 return fi;
1585
1586 err_inval:
1587 err = -EINVAL;
1588
1589 failure:
1590 if (fi) {
1591 fi->fib_dead = 1;
1592 free_fib_info(fi);
1593 }
1594
1595 return ERR_PTR(err);
1596 }
1597
fib_nexthop_info(struct sk_buff * skb,const struct fib_nh_common * nhc,u8 rt_family,unsigned char * flags,bool skip_oif)1598 int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
1599 u8 rt_family, unsigned char *flags, bool skip_oif)
1600 {
1601 if (nhc->nhc_flags & RTNH_F_DEAD)
1602 *flags |= RTNH_F_DEAD;
1603
1604 if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
1605 *flags |= RTNH_F_LINKDOWN;
1606
1607 rcu_read_lock();
1608 switch (nhc->nhc_family) {
1609 case AF_INET:
1610 if (ip_ignore_linkdown(nhc->nhc_dev))
1611 *flags |= RTNH_F_DEAD;
1612 break;
1613 case AF_INET6:
1614 if (ip6_ignore_linkdown(nhc->nhc_dev))
1615 *flags |= RTNH_F_DEAD;
1616 break;
1617 }
1618 rcu_read_unlock();
1619 }
1620
1621 switch (nhc->nhc_gw_family) {
1622 case AF_INET:
1623 if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1624 goto nla_put_failure;
1625 break;
1626 case AF_INET6:
1627 /* if gateway family does not match nexthop family
1628 * gateway is encoded as RTA_VIA
1629 */
1630 if (rt_family != nhc->nhc_gw_family) {
1631 int alen = sizeof(struct in6_addr);
1632 struct nlattr *nla;
1633 struct rtvia *via;
1634
1635 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1636 if (!nla)
1637 goto nla_put_failure;
1638
1639 via = nla_data(nla);
1640 via->rtvia_family = AF_INET6;
1641 memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen);
1642 } else if (nla_put_in6_addr(skb, RTA_GATEWAY,
1643 &nhc->nhc_gw.ipv6) < 0) {
1644 goto nla_put_failure;
1645 }
1646 break;
1647 }
1648
1649 *flags |= (nhc->nhc_flags & RTNH_F_ONLINK);
1650 if (nhc->nhc_flags & RTNH_F_OFFLOAD)
1651 *flags |= RTNH_F_OFFLOAD;
1652
1653 if (!skip_oif && nhc->nhc_dev &&
1654 nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
1655 goto nla_put_failure;
1656
1657 if (nhc->nhc_lwtstate &&
1658 lwtunnel_fill_encap(skb, nhc->nhc_lwtstate,
1659 RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
1660 goto nla_put_failure;
1661
1662 return 0;
1663
1664 nla_put_failure:
1665 return -EMSGSIZE;
1666 }
1667 EXPORT_SYMBOL_GPL(fib_nexthop_info);
1668
1669 #if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
fib_add_nexthop(struct sk_buff * skb,const struct fib_nh_common * nhc,int nh_weight,u8 rt_family,u32 nh_tclassid)1670 int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
1671 int nh_weight, u8 rt_family, u32 nh_tclassid)
1672 {
1673 const struct net_device *dev = nhc->nhc_dev;
1674 struct rtnexthop *rtnh;
1675 unsigned char flags = 0;
1676
1677 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1678 if (!rtnh)
1679 goto nla_put_failure;
1680
1681 rtnh->rtnh_hops = nh_weight - 1;
1682 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1683
1684 if (fib_nexthop_info(skb, nhc, rt_family, &flags, true) < 0)
1685 goto nla_put_failure;
1686
1687 rtnh->rtnh_flags = flags;
1688
1689 if (nh_tclassid && nla_put_u32(skb, RTA_FLOW, nh_tclassid))
1690 goto nla_put_failure;
1691
1692 /* length of rtnetlink header + attributes */
1693 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1694
1695 return 0;
1696
1697 nla_put_failure:
1698 return -EMSGSIZE;
1699 }
1700 EXPORT_SYMBOL_GPL(fib_add_nexthop);
1701 #endif
1702
1703 #ifdef CONFIG_IP_ROUTE_MULTIPATH
fib_add_multipath(struct sk_buff * skb,struct fib_info * fi)1704 static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1705 {
1706 struct nlattr *mp;
1707
1708 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
1709 if (!mp)
1710 goto nla_put_failure;
1711
1712 if (unlikely(fi->nh)) {
1713 if (nexthop_mpath_fill_node(skb, fi->nh, AF_INET) < 0)
1714 goto nla_put_failure;
1715 goto mp_end;
1716 }
1717
1718 for_nexthops(fi) {
1719 u32 nh_tclassid = 0;
1720 #ifdef CONFIG_IP_ROUTE_CLASSID
1721 nh_tclassid = nh->nh_tclassid;
1722 #endif
1723 if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight,
1724 AF_INET, nh_tclassid) < 0)
1725 goto nla_put_failure;
1726 } endfor_nexthops(fi);
1727
1728 mp_end:
1729 nla_nest_end(skb, mp);
1730
1731 return 0;
1732
1733 nla_put_failure:
1734 return -EMSGSIZE;
1735 }
1736 #else
fib_add_multipath(struct sk_buff * skb,struct fib_info * fi)1737 static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1738 {
1739 return 0;
1740 }
1741 #endif
1742
fib_dump_info(struct sk_buff * skb,u32 portid,u32 seq,int event,struct fib_rt_info * fri,unsigned int flags)1743 int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
1744 struct fib_rt_info *fri, unsigned int flags)
1745 {
1746 unsigned int nhs = fib_info_num_path(fri->fi);
1747 struct fib_info *fi = fri->fi;
1748 u32 tb_id = fri->tb_id;
1749 struct nlmsghdr *nlh;
1750 struct rtmsg *rtm;
1751
1752 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1753 if (!nlh)
1754 return -EMSGSIZE;
1755
1756 rtm = nlmsg_data(nlh);
1757 rtm->rtm_family = AF_INET;
1758 rtm->rtm_dst_len = fri->dst_len;
1759 rtm->rtm_src_len = 0;
1760 rtm->rtm_tos = fri->tos;
1761 if (tb_id < 256)
1762 rtm->rtm_table = tb_id;
1763 else
1764 rtm->rtm_table = RT_TABLE_COMPAT;
1765 if (nla_put_u32(skb, RTA_TABLE, tb_id))
1766 goto nla_put_failure;
1767 rtm->rtm_type = fri->type;
1768 rtm->rtm_flags = fi->fib_flags;
1769 rtm->rtm_scope = fi->fib_scope;
1770 rtm->rtm_protocol = fi->fib_protocol;
1771
1772 if (rtm->rtm_dst_len &&
1773 nla_put_in_addr(skb, RTA_DST, fri->dst))
1774 goto nla_put_failure;
1775 if (fi->fib_priority &&
1776 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1777 goto nla_put_failure;
1778 if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
1779 goto nla_put_failure;
1780
1781 if (fi->fib_prefsrc &&
1782 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
1783 goto nla_put_failure;
1784
1785 if (fi->nh) {
1786 if (nla_put_u32(skb, RTA_NH_ID, fi->nh->id))
1787 goto nla_put_failure;
1788 if (nexthop_is_blackhole(fi->nh))
1789 rtm->rtm_type = RTN_BLACKHOLE;
1790 if (!fi->fib_net->ipv4.sysctl_nexthop_compat_mode)
1791 goto offload;
1792 }
1793
1794 if (nhs == 1) {
1795 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
1796 unsigned char flags = 0;
1797
1798 if (fib_nexthop_info(skb, nhc, AF_INET, &flags, false) < 0)
1799 goto nla_put_failure;
1800
1801 rtm->rtm_flags = flags;
1802 #ifdef CONFIG_IP_ROUTE_CLASSID
1803 if (nhc->nhc_family == AF_INET) {
1804 struct fib_nh *nh;
1805
1806 nh = container_of(nhc, struct fib_nh, nh_common);
1807 if (nh->nh_tclassid &&
1808 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1809 goto nla_put_failure;
1810 }
1811 #endif
1812 } else {
1813 if (fib_add_multipath(skb, fi) < 0)
1814 goto nla_put_failure;
1815 }
1816
1817 offload:
1818 if (fri->offload)
1819 rtm->rtm_flags |= RTM_F_OFFLOAD;
1820 if (fri->trap)
1821 rtm->rtm_flags |= RTM_F_TRAP;
1822
1823 nlmsg_end(skb, nlh);
1824 return 0;
1825
1826 nla_put_failure:
1827 nlmsg_cancel(skb, nlh);
1828 return -EMSGSIZE;
1829 }
1830
1831 /*
1832 * Update FIB if:
1833 * - local address disappeared -> we must delete all the entries
1834 * referring to it.
1835 * - device went down -> we must shutdown all nexthops going via it.
1836 */
fib_sync_down_addr(struct net_device * dev,__be32 local)1837 int fib_sync_down_addr(struct net_device *dev, __be32 local)
1838 {
1839 int ret = 0;
1840 unsigned int hash = fib_laddr_hashfn(local);
1841 struct hlist_head *head = &fib_info_laddrhash[hash];
1842 int tb_id = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
1843 struct net *net = dev_net(dev);
1844 struct fib_info *fi;
1845
1846 if (!fib_info_laddrhash || local == 0)
1847 return 0;
1848
1849 hlist_for_each_entry(fi, head, fib_lhash) {
1850 if (!net_eq(fi->fib_net, net) ||
1851 fi->fib_tb_id != tb_id)
1852 continue;
1853 if (fi->fib_prefsrc == local) {
1854 fi->fib_flags |= RTNH_F_DEAD;
1855 ret++;
1856 }
1857 }
1858 return ret;
1859 }
1860
call_fib_nh_notifiers(struct fib_nh * nh,enum fib_event_type event_type)1861 static int call_fib_nh_notifiers(struct fib_nh *nh,
1862 enum fib_event_type event_type)
1863 {
1864 bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
1865 struct fib_nh_notifier_info info = {
1866 .fib_nh = nh,
1867 };
1868
1869 switch (event_type) {
1870 case FIB_EVENT_NH_ADD:
1871 if (nh->fib_nh_flags & RTNH_F_DEAD)
1872 break;
1873 if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
1874 break;
1875 return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
1876 &info.info);
1877 case FIB_EVENT_NH_DEL:
1878 if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1879 (nh->fib_nh_flags & RTNH_F_DEAD))
1880 return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
1881 event_type, &info.info);
1882 default:
1883 break;
1884 }
1885
1886 return NOTIFY_DONE;
1887 }
1888
1889 /* Update the PMTU of exceptions when:
1890 * - the new MTU of the first hop becomes smaller than the PMTU
1891 * - the old MTU was the same as the PMTU, and it limited discovery of
1892 * larger MTUs on the path. With that limit raised, we can now
1893 * discover larger MTUs
1894 * A special case is locked exceptions, for which the PMTU is smaller
1895 * than the minimal accepted PMTU:
1896 * - if the new MTU is greater than the PMTU, don't make any change
1897 * - otherwise, unlock and set PMTU
1898 */
fib_nhc_update_mtu(struct fib_nh_common * nhc,u32 new,u32 orig)1899 void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
1900 {
1901 struct fnhe_hash_bucket *bucket;
1902 int i;
1903
1904 bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1);
1905 if (!bucket)
1906 return;
1907
1908 for (i = 0; i < FNHE_HASH_SIZE; i++) {
1909 struct fib_nh_exception *fnhe;
1910
1911 for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1912 fnhe;
1913 fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1914 if (fnhe->fnhe_mtu_locked) {
1915 if (new <= fnhe->fnhe_pmtu) {
1916 fnhe->fnhe_pmtu = new;
1917 fnhe->fnhe_mtu_locked = false;
1918 }
1919 } else if (new < fnhe->fnhe_pmtu ||
1920 orig == fnhe->fnhe_pmtu) {
1921 fnhe->fnhe_pmtu = new;
1922 }
1923 }
1924 }
1925 }
1926
fib_sync_mtu(struct net_device * dev,u32 orig_mtu)1927 void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1928 {
1929 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1930 struct hlist_head *head = &fib_info_devhash[hash];
1931 struct fib_nh *nh;
1932
1933 hlist_for_each_entry(nh, head, nh_hash) {
1934 if (nh->fib_nh_dev == dev)
1935 fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
1936 }
1937 }
1938
1939 /* Event force Flags Description
1940 * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
1941 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
1942 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
1943 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
1944 *
1945 * only used when fib_nh is built into fib_info
1946 */
fib_sync_down_dev(struct net_device * dev,unsigned long event,bool force)1947 int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
1948 {
1949 int ret = 0;
1950 int scope = RT_SCOPE_NOWHERE;
1951 struct fib_info *prev_fi = NULL;
1952 unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1953 struct hlist_head *head = &fib_info_devhash[hash];
1954 struct fib_nh *nh;
1955
1956 if (force)
1957 scope = -1;
1958
1959 hlist_for_each_entry(nh, head, nh_hash) {
1960 struct fib_info *fi = nh->nh_parent;
1961 int dead;
1962
1963 BUG_ON(!fi->fib_nhs);
1964 if (nh->fib_nh_dev != dev || fi == prev_fi)
1965 continue;
1966 prev_fi = fi;
1967 dead = 0;
1968 change_nexthops(fi) {
1969 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
1970 dead++;
1971 else if (nexthop_nh->fib_nh_dev == dev &&
1972 nexthop_nh->fib_nh_scope != scope) {
1973 switch (event) {
1974 case NETDEV_DOWN:
1975 case NETDEV_UNREGISTER:
1976 nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
1977 fallthrough;
1978 case NETDEV_CHANGE:
1979 nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1980 break;
1981 }
1982 call_fib_nh_notifiers(nexthop_nh,
1983 FIB_EVENT_NH_DEL);
1984 dead++;
1985 }
1986 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1987 if (event == NETDEV_UNREGISTER &&
1988 nexthop_nh->fib_nh_dev == dev) {
1989 dead = fi->fib_nhs;
1990 break;
1991 }
1992 #endif
1993 } endfor_nexthops(fi)
1994 if (dead == fi->fib_nhs) {
1995 switch (event) {
1996 case NETDEV_DOWN:
1997 case NETDEV_UNREGISTER:
1998 fi->fib_flags |= RTNH_F_DEAD;
1999 fallthrough;
2000 case NETDEV_CHANGE:
2001 fi->fib_flags |= RTNH_F_LINKDOWN;
2002 break;
2003 }
2004 ret++;
2005 }
2006
2007 fib_rebalance(fi);
2008 }
2009
2010 return ret;
2011 }
2012
2013 /* Must be invoked inside of an RCU protected region. */
fib_select_default(const struct flowi4 * flp,struct fib_result * res)2014 static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
2015 {
2016 struct fib_info *fi = NULL, *last_resort = NULL;
2017 struct hlist_head *fa_head = res->fa_head;
2018 struct fib_table *tb = res->table;
2019 u8 slen = 32 - res->prefixlen;
2020 int order = -1, last_idx = -1;
2021 struct fib_alias *fa, *fa1 = NULL;
2022 u32 last_prio = res->fi->fib_priority;
2023 u8 last_tos = 0;
2024
2025 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
2026 struct fib_info *next_fi = fa->fa_info;
2027 struct fib_nh_common *nhc;
2028
2029 if (fa->fa_slen != slen)
2030 continue;
2031 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
2032 continue;
2033 if (fa->tb_id != tb->tb_id)
2034 continue;
2035 if (next_fi->fib_priority > last_prio &&
2036 fa->fa_tos == last_tos) {
2037 if (last_tos)
2038 continue;
2039 break;
2040 }
2041 if (next_fi->fib_flags & RTNH_F_DEAD)
2042 continue;
2043 last_tos = fa->fa_tos;
2044 last_prio = next_fi->fib_priority;
2045
2046 if (next_fi->fib_scope != res->scope ||
2047 fa->fa_type != RTN_UNICAST)
2048 continue;
2049
2050 nhc = fib_info_nhc(next_fi, 0);
2051 if (!nhc->nhc_gw_family || nhc->nhc_scope != RT_SCOPE_LINK)
2052 continue;
2053
2054 fib_alias_accessed(fa);
2055
2056 if (!fi) {
2057 if (next_fi != res->fi)
2058 break;
2059 fa1 = fa;
2060 } else if (!fib_detect_death(fi, order, &last_resort,
2061 &last_idx, fa1->fa_default)) {
2062 fib_result_assign(res, fi);
2063 fa1->fa_default = order;
2064 goto out;
2065 }
2066 fi = next_fi;
2067 order++;
2068 }
2069
2070 if (order <= 0 || !fi) {
2071 if (fa1)
2072 fa1->fa_default = -1;
2073 goto out;
2074 }
2075
2076 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
2077 fa1->fa_default)) {
2078 fib_result_assign(res, fi);
2079 fa1->fa_default = order;
2080 goto out;
2081 }
2082
2083 if (last_idx >= 0)
2084 fib_result_assign(res, last_resort);
2085 fa1->fa_default = last_idx;
2086 out:
2087 return;
2088 }
2089
2090 /*
2091 * Dead device goes up. We wake up dead nexthops.
2092 * It takes sense only on multipath routes.
2093 *
2094 * only used when fib_nh is built into fib_info
2095 */
fib_sync_up(struct net_device * dev,unsigned char nh_flags)2096 int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
2097 {
2098 struct fib_info *prev_fi;
2099 unsigned int hash;
2100 struct hlist_head *head;
2101 struct fib_nh *nh;
2102 int ret;
2103
2104 if (!(dev->flags & IFF_UP))
2105 return 0;
2106
2107 if (nh_flags & RTNH_F_DEAD) {
2108 unsigned int flags = dev_get_flags(dev);
2109
2110 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
2111 nh_flags |= RTNH_F_LINKDOWN;
2112 }
2113
2114 prev_fi = NULL;
2115 hash = fib_devindex_hashfn(dev->ifindex);
2116 head = &fib_info_devhash[hash];
2117 ret = 0;
2118
2119 hlist_for_each_entry(nh, head, nh_hash) {
2120 struct fib_info *fi = nh->nh_parent;
2121 int alive;
2122
2123 BUG_ON(!fi->fib_nhs);
2124 if (nh->fib_nh_dev != dev || fi == prev_fi)
2125 continue;
2126
2127 prev_fi = fi;
2128 alive = 0;
2129 change_nexthops(fi) {
2130 if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
2131 alive++;
2132 continue;
2133 }
2134 if (!nexthop_nh->fib_nh_dev ||
2135 !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
2136 continue;
2137 if (nexthop_nh->fib_nh_dev != dev ||
2138 !__in_dev_get_rtnl(dev))
2139 continue;
2140 alive++;
2141 nexthop_nh->fib_nh_flags &= ~nh_flags;
2142 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
2143 } endfor_nexthops(fi)
2144
2145 if (alive > 0) {
2146 fi->fib_flags &= ~nh_flags;
2147 ret++;
2148 }
2149
2150 fib_rebalance(fi);
2151 }
2152
2153 return ret;
2154 }
2155
2156 #ifdef CONFIG_IP_ROUTE_MULTIPATH
fib_good_nh(const struct fib_nh * nh)2157 static bool fib_good_nh(const struct fib_nh *nh)
2158 {
2159 int state = NUD_REACHABLE;
2160
2161 if (nh->fib_nh_scope == RT_SCOPE_LINK) {
2162 struct neighbour *n;
2163
2164 rcu_read_lock_bh();
2165
2166 if (likely(nh->fib_nh_gw_family == AF_INET))
2167 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
2168 (__force u32)nh->fib_nh_gw4);
2169 else if (nh->fib_nh_gw_family == AF_INET6)
2170 n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev,
2171 &nh->fib_nh_gw6);
2172 else
2173 n = NULL;
2174 if (n)
2175 state = n->nud_state;
2176
2177 rcu_read_unlock_bh();
2178 }
2179
2180 return !!(state & NUD_VALID);
2181 }
2182
fib_select_multipath(struct fib_result * res,int hash)2183 void fib_select_multipath(struct fib_result *res, int hash)
2184 {
2185 struct fib_info *fi = res->fi;
2186 struct net *net = fi->fib_net;
2187 bool first = false;
2188
2189 if (unlikely(res->fi->nh)) {
2190 nexthop_path_fib_result(res, hash);
2191 return;
2192 }
2193
2194 change_nexthops(fi) {
2195 if (net->ipv4.sysctl_fib_multipath_use_neigh) {
2196 if (!fib_good_nh(nexthop_nh))
2197 continue;
2198 if (!first) {
2199 res->nh_sel = nhsel;
2200 res->nhc = &nexthop_nh->nh_common;
2201 first = true;
2202 }
2203 }
2204
2205 if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
2206 continue;
2207
2208 res->nh_sel = nhsel;
2209 res->nhc = &nexthop_nh->nh_common;
2210 return;
2211 } endfor_nexthops(fi);
2212 }
2213 #endif
2214
fib_select_path(struct net * net,struct fib_result * res,struct flowi4 * fl4,const struct sk_buff * skb)2215 void fib_select_path(struct net *net, struct fib_result *res,
2216 struct flowi4 *fl4, const struct sk_buff *skb)
2217 {
2218 if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF))
2219 goto check_saddr;
2220
2221 #ifdef CONFIG_IP_ROUTE_MULTIPATH
2222 if (fib_info_num_path(res->fi) > 1) {
2223 int h = fib_multipath_hash(net, fl4, skb, NULL);
2224
2225 fib_select_multipath(res, h);
2226 }
2227 else
2228 #endif
2229 if (!res->prefixlen &&
2230 res->table->tb_num_default > 1 &&
2231 res->type == RTN_UNICAST)
2232 fib_select_default(fl4, res);
2233
2234 check_saddr:
2235 if (!fl4->saddr)
2236 fl4->saddr = fib_result_prefsrc(net, res);
2237 }
2238