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