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