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