blob: 3824b7abecf7e34f8275bcedace7af2ac0e55b17 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000010 */
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>
Olivier Deprez157378f2022-04-04 15:47:50 +020032#include <linux/hash.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000033
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>
David Brazdil0f672f62019-12-10 10:32:29 +000041#include <net/ip6_fib.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000042#include <net/nexthop.h>
David Brazdil0f672f62019-12-10 10:32:29 +000043#include <net/netlink.h>
44#include <net/rtnh.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000045#include <net/lwtunnel.h>
46#include <net/fib_notifier.h>
David Brazdil0f672f62019-12-10 10:32:29 +000047#include <net/addrconf.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000048
49#include "fib_lookup.h"
50
51static DEFINE_SPINLOCK(fib_info_lock);
52static struct hlist_head *fib_info_hash;
53static struct hlist_head *fib_info_laddrhash;
54static unsigned int fib_info_hash_size;
55static unsigned int fib_info_cnt;
56
57#define DEVINDEX_HASHBITS 8
58#define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
59static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
60
David Brazdil0f672f62019-12-10 10:32:29 +000061/* 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 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000064#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; \
David Brazdil0f672f62019-12-10 10:32:29 +000069 nhsel < fib_info_num_path((fi)); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070 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); \
David Brazdil0f672f62019-12-10 10:32:29 +000075 nhsel < fib_info_num_path((fi)); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076 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
96const 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
147static 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
David Brazdil0f672f62019-12-10 10:32:29 +0000163static void free_nh_exceptions(struct fib_nh_common *nhc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000164{
165 struct fnhe_hash_bucket *hash;
166 int i;
167
David Brazdil0f672f62019-12-10 10:32:29 +0000168 hash = rcu_dereference_protected(nhc->nhc_exceptions, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000169 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
191static 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
David Brazdil0f672f62019-12-10 10:32:29 +0000210void 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}
220EXPORT_SYMBOL_GPL(fib_nh_common_release);
221
222void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
223{
224#ifdef CONFIG_IP_ROUTE_CLASSID
225 if (fib_nh->nh_tclassid)
Olivier Deprez157378f2022-04-04 15:47:50 +0200226 atomic_dec(&net->ipv4.fib_num_tclassid_users);
David Brazdil0f672f62019-12-10 10:32:29 +0000227#endif
228 fib_nh_common_release(&fib_nh->nh_common);
229}
230
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000231/* Release a nexthop info record */
232static void free_fib_info_rcu(struct rcu_head *head)
233{
234 struct fib_info *fi = container_of(head, struct fib_info, rcu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000235
David Brazdil0f672f62019-12-10 10:32:29 +0000236 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 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000243
David Brazdil0f672f62019-12-10 10:32:29 +0000244 ip_fib_metrics_put(fi->fib_metrics);
245
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000246 kfree(fi);
247}
248
249void 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 }
David Brazdil0f672f62019-12-10 10:32:29 +0000255
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000256 call_rcu(&fi->rcu, free_fib_info_rcu);
257}
258EXPORT_SYMBOL_GPL(free_fib_info);
259
260void 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);
Olivier Deprez157378f2022-04-04 15:47:50 +0200265
266 /* Paired with READ_ONCE() in fib_create_info(). */
267 WRITE_ONCE(fib_info_cnt, fib_info_cnt - 1);
268
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000269 if (fi->fib_prefsrc)
270 hlist_del(&fi->fib_lhash);
David Brazdil0f672f62019-12-10 10:32:29 +0000271 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 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000280 fi->fib_dead = 1;
281 fib_info_put(fi);
282 }
283 spin_unlock_bh(&fib_info_lock);
284}
285
David Brazdil0f672f62019-12-10 10:32:29 +0000286static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000287{
David Brazdil0f672f62019-12-10 10:32:29 +0000288 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000295
296 for_nexthops(fi) {
David Brazdil0f672f62019-12-10 10:32:29 +0000297 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 ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000302#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Brazdil0f672f62019-12-10 10:32:29 +0000303 nh->fib_nh_weight != onh->fib_nh_weight ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000304#endif
305#ifdef CONFIG_IP_ROUTE_CLASSID
306 nh->nh_tclassid != onh->nh_tclassid ||
307#endif
David Brazdil0f672f62019-12-10 10:32:29 +0000308 lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
309 ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000310 return -1;
David Brazdil0f672f62019-12-10 10:32:29 +0000311
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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000319 } endfor_nexthops(fi);
320 return 0;
321}
322
323static inline unsigned int fib_devindex_hashfn(unsigned int val)
324{
Olivier Deprez157378f2022-04-04 15:47:50 +0200325 return hash_32(val, DEVINDEX_HASHBITS);
326}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000327
Olivier Deprez157378f2022-04-04 15:47:50 +0200328static struct hlist_head *
329fib_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)];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000334}
335
David Brazdil0f672f62019-12-10 10:32:29 +0000336static 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
348static unsigned int fib_info_hashfn_result(unsigned int val)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000349{
350 unsigned int mask = (fib_info_hash_size - 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000351
352 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
353}
354
David Brazdil0f672f62019-12-10 10:32:29 +0000355static 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 */
375static 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
407static struct fib_info *fib_find_info(struct fib_info *nfi)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000408{
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) &&
David Brazdil0f672f62019-12-10 10:32:29 +0000429 nh_comp(fi, nfi) == 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000430 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 */
439int ip_fib_check_default(__be32 gw, struct net_device *dev)
440{
441 struct hlist_head *head;
442 struct fib_nh *nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000443
444 spin_lock(&fib_info_lock);
445
Olivier Deprez157378f2022-04-04 15:47:50 +0200446 head = fib_info_devhash_bucket(dev);
447
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000448 hlist_for_each_entry(nh, head, nh_hash) {
David Brazdil0f672f62019-12-10 10:32:29 +0000449 if (nh->fib_nh_dev == dev &&
450 nh->fib_nh_gw4 == gw &&
451 !(nh->fib_nh_flags & RTNH_F_DEAD)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000452 spin_unlock(&fib_info_lock);
453 return 0;
454 }
455 }
456
457 spin_unlock(&fib_info_lock);
458
459 return -1;
460}
461
462static 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 */
David Brazdil0f672f62019-12-10 10:32:29 +0000470 unsigned int nhs = fib_info_num_path(fi);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000471
472 /* space for nested metrics */
473 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
474
David Brazdil0f672f62019-12-10 10:32:29 +0000475 if (fi->nh)
476 payload += nla_total_size(4); /* RTA_NH_ID */
477
478 if (nhs) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000479 size_t nh_encapsize = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000480 /* Also handles the special case nhs == 1 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000481
482 /* each nexthop is packed in an attribute */
483 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
David Brazdil0f672f62019-12-10 10:32:29 +0000484 unsigned int i;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000485
486 /* may contain flow and gateway attribute */
487 nhsize += 2 * nla_total_size(4);
488
489 /* grab encap info */
David Brazdil0f672f62019-12-10 10:32:29 +0000490 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) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000494 /* RTA_ENCAP_TYPE */
495 nh_encapsize += lwtunnel_get_encap_size(
David Brazdil0f672f62019-12-10 10:32:29 +0000496 nhc->nhc_lwtstate);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000497 /* RTA_ENCAP */
498 nh_encapsize += nla_total_size(2);
499 }
David Brazdil0f672f62019-12-10 10:32:29 +0000500 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000501
502 /* all nexthops are packed in a nested attribute */
David Brazdil0f672f62019-12-10 10:32:29 +0000503 payload += nla_total_size((nhs * nhsize) + nh_encapsize);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000504
505 }
506
507 return payload;
508}
509
510void 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{
Olivier Deprez157378f2022-04-04 15:47:50 +0200514 struct fib_rt_info fri;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000515 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
Olivier Deprez157378f2022-04-04 15:47:50 +0200523 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000532 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;
541errout:
542 if (err < 0)
543 rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err);
544}
545
546static int fib_detect_death(struct fib_info *fi, int order,
547 struct fib_info **last_resort, int *last_idx,
548 int dflt)
549{
David Brazdil0f672f62019-12-10 10:32:29 +0000550 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000551 struct neighbour *n;
552 int state = NUD_NONE;
553
David Brazdil0f672f62019-12-10 10:32:29 +0000554 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000562 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
Olivier Deprez157378f2022-04-04 15:47:50 +0200580int 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,
David Brazdil0f672f62019-12-10 10:32:29 +0000583 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 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200600 err = lwtunnel_build_state(net, encap_type, encap,
601 nhc->nhc_family, cfg, &lwtstate,
602 extack);
David Brazdil0f672f62019-12-10 10:32:29 +0000603 if (err)
604 goto lwt_failure;
605
606 nhc->nhc_lwtstate = lwtstate_get(lwtstate);
607 }
608
609 return 0;
610
611lwt_failure:
612 rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
613 nhc->nhc_pcpu_rth_output = NULL;
614 return err;
615}
616EXPORT_SYMBOL_GPL(fib_nh_common_init);
617
618int 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
Olivier Deprez157378f2022-04-04 15:47:50 +0200626 err = fib_nh_common_init(net, &nh->nh_common, cfg->fc_encap,
David Brazdil0f672f62019-12-10 10:32:29 +0000627 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)
Olivier Deprez157378f2022-04-04 15:47:50 +0200643 atomic_inc(&net->ipv4.fib_num_tclassid_users);
David Brazdil0f672f62019-12-10 10:32:29 +0000644#endif
645#ifdef CONFIG_IP_ROUTE_MULTIPATH
646 nh->fib_nh_weight = nh_weight;
647#endif
648 return 0;
649}
650
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000651#ifdef CONFIG_IP_ROUTE_MULTIPATH
652
653static 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
Olivier Deprez157378f2022-04-04 15:47:50 +0200673static 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
David Brazdil0f672f62019-12-10 10:32:29 +0000686/* only called when fib_nh is integrated into fib_info */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000687static 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{
David Brazdil0f672f62019-12-10 10:32:29 +0000691 struct net *net = fi->fib_net;
692 struct fib_config fib_cfg;
693 struct fib_nh *nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000694 int ret;
695
696 change_nexthops(fi) {
697 int attrlen;
698
David Brazdil0f672f62019-12-10 10:32:29 +0000699 memset(&fib_cfg, 0, sizeof(fib_cfg));
700
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000701 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
David Brazdil0f672f62019-12-10 10:32:29 +0000713 fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
714 fib_cfg.fc_oif = rtnh->rtnh_ifindex;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000715
716 attrlen = rtnh_attrlen(rtnh);
717 if (attrlen > 0) {
David Brazdil0f672f62019-12-10 10:32:29 +0000718 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000719
720 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David Brazdil0f672f62019-12-10 10:32:29 +0000721 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 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000727 if (nla) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200728 ret = fib_gw_from_attr(&fib_cfg.fc_gw4, nla,
729 extack);
730 if (ret)
731 goto errout;
732
David Brazdil0f672f62019-12-10 10:32:29 +0000733 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000737 if (ret)
738 goto errout;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000739 }
David Brazdil0f672f62019-12-10 10:32:29 +0000740
741 nla = nla_find(attrs, attrlen, RTA_FLOW);
Olivier Deprez157378f2022-04-04 15:47:50 +0200742 if (nla) {
743 if (nla_len(nla) < sizeof(u32)) {
744 NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
745 return -EINVAL;
746 }
David Brazdil0f672f62019-12-10 10:32:29 +0000747 fib_cfg.fc_flow = nla_get_u32(nla);
Olivier Deprez157378f2022-04-04 15:47:50 +0200748 }
David Brazdil0f672f62019-12-10 10:32:29 +0000749
750 fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
Olivier Deprez157378f2022-04-04 15:47:50 +0200751 /* RTA_ENCAP_TYPE length checked in
752 * lwtunnel_valid_encap_type_attr
753 */
David Brazdil0f672f62019-12-10 10:32:29 +0000754 nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
755 if (nla)
756 fib_cfg.fc_encap_type = nla_get_u16(nla);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000757 }
758
David Brazdil0f672f62019-12-10 10:32:29 +0000759 ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
760 rtnh->rtnh_hops + 1, extack);
761 if (ret)
762 goto errout;
763
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000764 rtnh = rtnh_next(rtnh, &remaining);
765 } endfor_nexthops(fi);
766
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000767 ret = -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +0000768 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000793errout:
794 return ret;
795}
796
David Brazdil0f672f62019-12-10 10:32:29 +0000797/* only called when fib_nh is integrated into fib_info */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000798static void fib_rebalance(struct fib_info *fi)
799{
800 int total;
801 int w;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000802
David Brazdil0f672f62019-12-10 10:32:29 +0000803 if (fib_info_num_path(fi) < 2)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000804 return;
805
806 total = 0;
807 for_nexthops(fi) {
David Brazdil0f672f62019-12-10 10:32:29 +0000808 if (nh->fib_nh_flags & RTNH_F_DEAD)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000809 continue;
810
David Brazdil0f672f62019-12-10 10:32:29 +0000811 if (ip_ignore_linkdown(nh->fib_nh_dev) &&
812 nh->fib_nh_flags & RTNH_F_LINKDOWN)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000813 continue;
814
David Brazdil0f672f62019-12-10 10:32:29 +0000815 total += nh->fib_nh_weight;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000816 } endfor_nexthops(fi);
817
818 w = 0;
819 change_nexthops(fi) {
820 int upper_bound;
821
David Brazdil0f672f62019-12-10 10:32:29 +0000822 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000823 upper_bound = -1;
David Brazdil0f672f62019-12-10 10:32:29 +0000824 } else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
825 nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000826 upper_bound = -1;
827 } else {
David Brazdil0f672f62019-12-10 10:32:29 +0000828 w += nexthop_nh->fib_nh_weight;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000829 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
830 total) - 1;
831 }
832
David Brazdil0f672f62019-12-10 10:32:29 +0000833 atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000834 } endfor_nexthops(fi);
835}
836#else /* CONFIG_IP_ROUTE_MULTIPATH */
837
David Brazdil0f672f62019-12-10 10:32:29 +0000838static 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000847#define fib_rebalance(fi) do { } while (0)
848
849#endif /* CONFIG_IP_ROUTE_MULTIPATH */
850
Olivier Deprez157378f2022-04-04 15:47:50 +0200851static int fib_encap_match(struct net *net, u16 encap_type,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000852 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
Olivier Deprez157378f2022-04-04 15:47:50 +0200863 ret = lwtunnel_build_state(net, encap_type, encap, AF_INET,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000864 cfg, &lwtstate, extack);
865 if (!ret) {
David Brazdil0f672f62019-12-10 10:32:29 +0000866 result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000867 lwtstate_free(lwtstate);
868 }
869
870 return result;
871}
872
Olivier Deprez157378f2022-04-04 15:47:50 +0200873int fib_nh_match(struct net *net, struct fib_config *cfg, struct fib_info *fi,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000874 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
David Brazdil0f672f62019-12-10 10:32:29 +0000884 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 if (cfg->fc_oif || cfg->fc_gw_family) {
Olivier Deprez92d4c212022-12-06 15:05:30 +0100891 struct fib_nh *nh;
David Brazdil0f672f62019-12-10 10:32:29 +0000892
Olivier Deprez92d4c212022-12-06 15:05:30 +0100893 /* cannot match on nexthop object attributes */
894 if (fi->nh)
895 return 1;
896
897 nh = fib_info_nh(fi, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000898 if (cfg->fc_encap) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200899 if (fib_encap_match(net, cfg->fc_encap_type,
900 cfg->fc_encap, nh, cfg, extack))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000901 return 1;
902 }
903#ifdef CONFIG_IP_ROUTE_CLASSID
904 if (cfg->fc_flow &&
David Brazdil0f672f62019-12-10 10:32:29 +0000905 cfg->fc_flow != nh->nh_tclassid)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000906 return 1;
907#endif
David Brazdil0f672f62019-12-10 10:32:29 +0000908 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000922 }
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
David Brazdil0f672f62019-12-10 10:32:29 +0000937 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000938 return 1;
939
940 attrlen = rtnh_attrlen(rtnh);
941 if (attrlen > 0) {
David Brazdil0f672f62019-12-10 10:32:29 +0000942 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
Olivier Deprez157378f2022-04-04 15:47:50 +0200943 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000944
945 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David Brazdil0f672f62019-12-10 10:32:29 +0000946 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) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200954 __be32 gw;
955
956 err = fib_gw_from_attr(&gw, nla, extack);
957 if (err)
958 return err;
959
David Brazdil0f672f62019-12-10 10:32:29 +0000960 if (nh->fib_nh_gw_family != AF_INET ||
Olivier Deprez157378f2022-04-04 15:47:50 +0200961 gw != nh->fib_nh_gw4)
David Brazdil0f672f62019-12-10 10:32:29 +0000962 return 1;
963 } else if (nlav) {
964 struct fib_config cfg2;
David Brazdil0f672f62019-12-10 10:32:29 +0000965
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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000985#ifdef CONFIG_IP_ROUTE_CLASSID
986 nla = nla_find(attrs, attrlen, RTA_FLOW);
Olivier Deprez157378f2022-04-04 15:47:50 +0200987 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 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000995#endif
996 }
997
998 rtnh = rtnh_next(rtnh, &remaining);
999 } endfor_nexthops(fi);
1000#endif
1001 return 0;
1002}
1003
1004bool 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
David Brazdil0f672f62019-12-10 10:32:29 +00001044static 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}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001068
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 */
David Brazdil0f672f62019-12-10 10:32:29 +00001112static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
1113 u8 scope, struct netlink_ext_ack *extack)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001114{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001115 struct net_device *dev;
David Brazdil0f672f62019-12-10 10:32:29 +00001116 struct fib_result res;
1117 int err = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001118
David Brazdil0f672f62019-12-10 10:32:29 +00001119 if (nh->fib_nh_flags & RTNH_F_ONLINK) {
1120 unsigned int addr_type;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001121
David Brazdil0f672f62019-12-10 10:32:29 +00001122 if (scope >= RT_SCOPE_LINK) {
1123 NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
1124 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001125 }
David Brazdil0f672f62019-12-10 10:32:29 +00001126 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001130 }
David Brazdil0f672f62019-12-10 10:32:29 +00001131 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
Olivier Deprez0e641232021-09-23 10:07:05 +02001161 if (table && table != RT_TABLE_MAIN)
David Brazdil0f672f62019-12-10 10:32:29 +00001162 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) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001179 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1180 goto out;
1181 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001182 }
David Brazdil0f672f62019-12-10 10:32:29 +00001183
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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001201out:
1202 rcu_read_unlock();
1203 return err;
1204}
1205
David Brazdil0f672f62019-12-10 10:32:29 +00001206static 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);
Olivier Deprez92d4c212022-12-06 15:05:30 +01001232 nh->fib_nh_scope = RT_SCOPE_LINK;
David Brazdil0f672f62019-12-10 10:32:29 +00001233 if (!netif_carrier_ok(nh->fib_nh_dev))
1234 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1235 err = 0;
1236out:
1237 rcu_read_unlock();
1238 return err;
1239}
1240
1241int 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001256static 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
1265static 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
1275static 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
1286static 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
David Brazdil0f672f62019-12-10 10:32:29 +00001338__be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
1339 unsigned char scope)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001340{
David Brazdil0f672f62019-12-10 10:32:29 +00001341 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001348 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
1349
1350 return nh->nh_saddr;
1351}
1352
David Brazdil0f672f62019-12-10 10:32:29 +00001353__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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001371static 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001395struct 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;
David Brazdil0f672f62019-12-10 10:32:29 +00001400 struct nexthop *nh = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001401 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
David Brazdil0f672f62019-12-10 10:32:29 +00001420 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001437#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;
Olivier Deprez157378f2022-04-04 15:47:50 +02001446
1447 /* Paired with WRITE_ONCE() in fib_release_info() */
1448 if (READ_ONCE(fib_info_cnt) >= fib_info_hash_size) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001449 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
David Brazdil0f672f62019-12-10 10:32:29 +00001469 fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001470 if (!fi)
1471 goto failure;
David Brazdil0f672f62019-12-10 10:32:29 +00001472 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001478 }
David Brazdil0f672f62019-12-10 10:32:29 +00001479
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001480 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;
David Brazdil0f672f62019-12-10 10:32:29 +00001490 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001497 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001498 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00001499 change_nexthops(fi) {
1500 nexthop_nh->nh_parent = fi;
1501 } endfor_nexthops(fi)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001502
David Brazdil0f672f62019-12-10 10:32:29 +00001503 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001508 }
1509
David Brazdil0f672f62019-12-10 10:32:29 +00001510 if (err != 0)
1511 goto failure;
1512
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001513 if (fib_props[cfg->fc_type].error) {
David Brazdil0f672f62019-12-10 10:32:29 +00001514 if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001515 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
David Brazdil0f672f62019-12-10 10:32:29 +00001539 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) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001544 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 }
David Brazdil0f672f62019-12-10 10:32:29 +00001552 if (nh->fib_nh_gw_family) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001553 NL_SET_ERR_MSG(extack,
1554 "Route with host scope can not have a gateway");
1555 goto err_inval;
1556 }
David Brazdil0f672f62019-12-10 10:32:29 +00001557 nh->fib_nh_scope = RT_SCOPE_NOWHERE;
1558 nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001559 err = -ENODEV;
David Brazdil0f672f62019-12-10 10:32:29 +00001560 if (!nh->fib_nh_dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001561 goto failure;
1562 } else {
1563 int linkdown = 0;
1564
1565 change_nexthops(fi) {
David Brazdil0f672f62019-12-10 10:32:29 +00001566 err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh,
1567 cfg->fc_table, cfg->fc_scope,
1568 extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001569 if (err != 0)
1570 goto failure;
David Brazdil0f672f62019-12-10 10:32:29 +00001571 if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001572 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
David Brazdil0f672f62019-12-10 10:32:29 +00001583 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)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001590
David Brazdil0f672f62019-12-10 10:32:29 +00001591 fib_rebalance(fi);
1592 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001593
1594link_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);
Olivier Deprez157378f2022-04-04 15:47:50 +02001606 fib_info_cnt++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001607 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 }
David Brazdil0f672f62019-12-10 10:32:29 +00001615 if (fi->nh) {
1616 list_add(&fi->nh_list, &nh->fi_list);
1617 } else {
1618 change_nexthops(fi) {
1619 struct hlist_head *head;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001620
David Brazdil0f672f62019-12-10 10:32:29 +00001621 if (!nexthop_nh->fib_nh_dev)
1622 continue;
Olivier Deprez157378f2022-04-04 15:47:50 +02001623 head = fib_info_devhash_bucket(nexthop_nh->fib_nh_dev);
David Brazdil0f672f62019-12-10 10:32:29 +00001624 hlist_add_head(&nexthop_nh->nh_hash, head);
1625 } endfor_nexthops(fi)
1626 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001627 spin_unlock_bh(&fib_info_lock);
1628 return fi;
1629
1630err_inval:
1631 err = -EINVAL;
1632
1633failure:
1634 if (fi) {
1635 fi->fib_dead = 1;
1636 free_fib_info(fi);
1637 }
1638
1639 return ERR_PTR(err);
1640}
1641
David Brazdil0f672f62019-12-10 10:32:29 +00001642int 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
1708nla_put_failure:
1709 return -EMSGSIZE;
1710}
1711EXPORT_SYMBOL_GPL(fib_nexthop_info);
1712
1713#if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
1714int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
Olivier Deprez157378f2022-04-04 15:47:50 +02001715 int nh_weight, u8 rt_family, u32 nh_tclassid)
David Brazdil0f672f62019-12-10 10:32:29 +00001716{
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
Olivier Deprez157378f2022-04-04 15:47:50 +02001733 if (nh_tclassid && nla_put_u32(skb, RTA_FLOW, nh_tclassid))
1734 goto nla_put_failure;
1735
David Brazdil0f672f62019-12-10 10:32:29 +00001736 /* length of rtnetlink header + attributes */
1737 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1738
1739 return 0;
1740
1741nla_put_failure:
1742 return -EMSGSIZE;
1743}
1744EXPORT_SYMBOL_GPL(fib_add_nexthop);
1745#endif
1746
1747#ifdef CONFIG_IP_ROUTE_MULTIPATH
1748static 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) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001763 u32 nh_tclassid = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001764#ifdef CONFIG_IP_ROUTE_CLASSID
Olivier Deprez157378f2022-04-04 15:47:50 +02001765 nh_tclassid = nh->nh_tclassid;
David Brazdil0f672f62019-12-10 10:32:29 +00001766#endif
Olivier Deprez157378f2022-04-04 15:47:50 +02001767 if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight,
1768 AF_INET, nh_tclassid) < 0)
1769 goto nla_put_failure;
David Brazdil0f672f62019-12-10 10:32:29 +00001770 } endfor_nexthops(fi);
1771
1772mp_end:
1773 nla_nest_end(skb, mp);
1774
1775 return 0;
1776
1777nla_put_failure:
1778 return -EMSGSIZE;
1779}
1780#else
1781static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1782{
1783 return 0;
1784}
1785#endif
1786
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001787int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
Olivier Deprez157378f2022-04-04 15:47:50 +02001788 struct fib_rt_info *fri, unsigned int flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001789{
Olivier Deprez157378f2022-04-04 15:47:50 +02001790 unsigned int nhs = fib_info_num_path(fri->fi);
1791 struct fib_info *fi = fri->fi;
1792 u32 tb_id = fri->tb_id;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001793 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;
Olivier Deprez157378f2022-04-04 15:47:50 +02001802 rtm->rtm_dst_len = fri->dst_len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001803 rtm->rtm_src_len = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02001804 rtm->rtm_tos = fri->tos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001805 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;
Olivier Deprez157378f2022-04-04 15:47:50 +02001811 rtm->rtm_type = fri->type;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001812 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 &&
Olivier Deprez157378f2022-04-04 15:47:50 +02001817 nla_put_in_addr(skb, RTA_DST, fri->dst))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001818 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001828
David Brazdil0f672f62019-12-10 10:32:29 +00001829 if (fi->nh) {
1830 if (nla_put_u32(skb, RTA_NH_ID, fi->nh->id))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001831 goto nla_put_failure;
David Brazdil0f672f62019-12-10 10:32:29 +00001832 if (nexthop_is_blackhole(fi->nh))
1833 rtm->rtm_type = RTN_BLACKHOLE;
Olivier Deprez92d4c212022-12-06 15:05:30 +01001834 if (!READ_ONCE(fi->fib_net->ipv4.sysctl_nexthop_compat_mode))
Olivier Deprez157378f2022-04-04 15:47:50 +02001835 goto offload;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001836 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001837
David Brazdil0f672f62019-12-10 10:32:29 +00001838 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)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001843 goto nla_put_failure;
1844
David Brazdil0f672f62019-12-10 10:32:29 +00001845 rtm->rtm_flags = flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001846#ifdef CONFIG_IP_ROUTE_CLASSID
David Brazdil0f672f62019-12-10 10:32:29 +00001847 if (nhc->nhc_family == AF_INET) {
1848 struct fib_nh *nh;
1849
1850 nh = container_of(nhc, struct fib_nh, nh_common);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001851 if (nh->nh_tclassid &&
1852 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1853 goto nla_put_failure;
David Brazdil0f672f62019-12-10 10:32:29 +00001854 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001855#endif
David Brazdil0f672f62019-12-10 10:32:29 +00001856 } else {
1857 if (fib_add_multipath(skb, fi) < 0)
1858 goto nla_put_failure;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001859 }
David Brazdil0f672f62019-12-10 10:32:29 +00001860
Olivier Deprez157378f2022-04-04 15:47:50 +02001861offload:
1862 if (fri->offload)
1863 rtm->rtm_flags |= RTM_F_OFFLOAD;
1864 if (fri->trap)
1865 rtm->rtm_flags |= RTM_F_TRAP;
1866
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001867 nlmsg_end(skb, nlh);
1868 return 0;
1869
1870nla_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 */
1881int 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];
David Brazdil0f672f62019-12-10 10:32:29 +00001886 int tb_id = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001887 struct net *net = dev_net(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001888 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
David Brazdil0f672f62019-12-10 10:32:29 +00001905static int call_fib_nh_notifiers(struct fib_nh *nh,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001906 enum fib_event_type event_type)
1907{
David Brazdil0f672f62019-12-10 10:32:29 +00001908 bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001909 struct fib_nh_notifier_info info = {
David Brazdil0f672f62019-12-10 10:32:29 +00001910 .fib_nh = nh,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001911 };
1912
1913 switch (event_type) {
1914 case FIB_EVENT_NH_ADD:
David Brazdil0f672f62019-12-10 10:32:29 +00001915 if (nh->fib_nh_flags & RTNH_F_DEAD)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001916 break;
David Brazdil0f672f62019-12-10 10:32:29 +00001917 if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001918 break;
David Brazdil0f672f62019-12-10 10:32:29 +00001919 return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001920 &info.info);
1921 case FIB_EVENT_NH_DEL:
David Brazdil0f672f62019-12-10 10:32:29 +00001922 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),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001925 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 */
David Brazdil0f672f62019-12-10 10:32:29 +00001943void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001944{
1945 struct fnhe_hash_bucket *bucket;
1946 int i;
1947
David Brazdil0f672f62019-12-10 10:32:29 +00001948 bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001949 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
1971void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1972{
Olivier Deprez157378f2022-04-04 15:47:50 +02001973 struct hlist_head *head = fib_info_devhash_bucket(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001974 struct fib_nh *nh;
1975
1976 hlist_for_each_entry(nh, head, nh_hash) {
David Brazdil0f672f62019-12-10 10:32:29 +00001977 if (nh->fib_nh_dev == dev)
1978 fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001979 }
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
David Brazdil0f672f62019-12-10 10:32:29 +00001987 *
1988 * only used when fib_nh is built into fib_info
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001989 */
1990int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
1991{
Olivier Deprez157378f2022-04-04 15:47:50 +02001992 struct hlist_head *head = fib_info_devhash_bucket(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001993 struct fib_info *prev_fi = NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +02001994 int scope = RT_SCOPE_NOWHERE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001995 struct fib_nh *nh;
Olivier Deprez157378f2022-04-04 15:47:50 +02001996 int ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001997
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);
David Brazdil0f672f62019-12-10 10:32:29 +00002006 if (nh->fib_nh_dev != dev || fi == prev_fi)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002007 continue;
2008 prev_fi = fi;
2009 dead = 0;
2010 change_nexthops(fi) {
David Brazdil0f672f62019-12-10 10:32:29 +00002011 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002012 dead++;
David Brazdil0f672f62019-12-10 10:32:29 +00002013 else if (nexthop_nh->fib_nh_dev == dev &&
2014 nexthop_nh->fib_nh_scope != scope) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002015 switch (event) {
2016 case NETDEV_DOWN:
2017 case NETDEV_UNREGISTER:
David Brazdil0f672f62019-12-10 10:32:29 +00002018 nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
Olivier Deprez157378f2022-04-04 15:47:50 +02002019 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002020 case NETDEV_CHANGE:
David Brazdil0f672f62019-12-10 10:32:29 +00002021 nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002022 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 &&
David Brazdil0f672f62019-12-10 10:32:29 +00002030 nexthop_nh->fib_nh_dev == dev) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002031 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;
Olivier Deprez157378f2022-04-04 15:47:50 +02002041 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002042 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. */
2056static 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;
Olivier Deprez0e641232021-09-23 10:07:05 +02002069 struct fib_nh_common *nhc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002070
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;
David Brazdil0f672f62019-12-10 10:32:29 +00002091
Olivier Deprez0e641232021-09-23 10:07:05 +02002092 nhc = fib_info_nhc(next_fi, 0);
2093 if (!nhc->nhc_gw_family || nhc->nhc_scope != RT_SCOPE_LINK)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002094 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;
2128out:
2129 return;
2130}
2131
2132/*
2133 * Dead device goes up. We wake up dead nexthops.
2134 * It takes sense only on multipath routes.
David Brazdil0f672f62019-12-10 10:32:29 +00002135 *
2136 * only used when fib_nh is built into fib_info
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002137 */
David Brazdil0f672f62019-12-10 10:32:29 +00002138int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002139{
2140 struct fib_info *prev_fi;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002141 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;
Olivier Deprez157378f2022-04-04 15:47:50 +02002156 head = fib_info_devhash_bucket(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002157 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);
David Brazdil0f672f62019-12-10 10:32:29 +00002164 if (nh->fib_nh_dev != dev || fi == prev_fi)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002165 continue;
2166
2167 prev_fi = fi;
2168 alive = 0;
2169 change_nexthops(fi) {
David Brazdil0f672f62019-12-10 10:32:29 +00002170 if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002171 alive++;
2172 continue;
2173 }
David Brazdil0f672f62019-12-10 10:32:29 +00002174 if (!nexthop_nh->fib_nh_dev ||
2175 !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002176 continue;
David Brazdil0f672f62019-12-10 10:32:29 +00002177 if (nexthop_nh->fib_nh_dev != dev ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002178 !__in_dev_get_rtnl(dev))
2179 continue;
2180 alive++;
David Brazdil0f672f62019-12-10 10:32:29 +00002181 nexthop_nh->fib_nh_flags &= ~nh_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002182 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
2197static bool fib_good_nh(const struct fib_nh *nh)
2198{
2199 int state = NUD_REACHABLE;
2200
David Brazdil0f672f62019-12-10 10:32:29 +00002201 if (nh->fib_nh_scope == RT_SCOPE_LINK) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002202 struct neighbour *n;
2203
2204 rcu_read_lock_bh();
2205
David Brazdil0f672f62019-12-10 10:32:29 +00002206 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;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002214 if (n)
2215 state = n->nud_state;
2216
2217 rcu_read_unlock_bh();
2218 }
2219
2220 return !!(state & NUD_VALID);
2221}
2222
2223void 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
David Brazdil0f672f62019-12-10 10:32:29 +00002229 if (unlikely(res->fi->nh)) {
2230 nexthop_path_fib_result(res, hash);
2231 return;
2232 }
2233
2234 change_nexthops(fi) {
Olivier Deprez92d4c212022-12-06 15:05:30 +01002235 if (READ_ONCE(net->ipv4.sysctl_fib_multipath_use_neigh)) {
David Brazdil0f672f62019-12-10 10:32:29 +00002236 if (!fib_good_nh(nexthop_nh))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002237 continue;
2238 if (!first) {
2239 res->nh_sel = nhsel;
David Brazdil0f672f62019-12-10 10:32:29 +00002240 res->nhc = &nexthop_nh->nh_common;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002241 first = true;
2242 }
2243 }
2244
David Brazdil0f672f62019-12-10 10:32:29 +00002245 if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002246 continue;
2247
2248 res->nh_sel = nhsel;
David Brazdil0f672f62019-12-10 10:32:29 +00002249 res->nhc = &nexthop_nh->nh_common;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002250 return;
2251 } endfor_nexthops(fi);
2252}
2253#endif
2254
2255void 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
David Brazdil0f672f62019-12-10 10:32:29 +00002262 if (fib_info_num_path(res->fi) > 1) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002263 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
2274check_saddr:
2275 if (!fl4->saddr)
David Brazdil0f672f62019-12-10 10:32:29 +00002276 fl4->saddr = fib_result_prefsrc(net, res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002277}