blob: 838a876c168caf5a39af46ce90afea11691fd035 [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) {
891 struct fib_nh *nh = fib_info_nh(fi, 0);
892
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000893 if (cfg->fc_encap) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200894 if (fib_encap_match(net, cfg->fc_encap_type,
895 cfg->fc_encap, nh, cfg, extack))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000896 return 1;
897 }
898#ifdef CONFIG_IP_ROUTE_CLASSID
899 if (cfg->fc_flow &&
David Brazdil0f672f62019-12-10 10:32:29 +0000900 cfg->fc_flow != nh->nh_tclassid)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000901 return 1;
902#endif
David Brazdil0f672f62019-12-10 10:32:29 +0000903 if ((cfg->fc_oif && cfg->fc_oif != nh->fib_nh_oif) ||
904 (cfg->fc_gw_family &&
905 cfg->fc_gw_family != nh->fib_nh_gw_family))
906 return 1;
907
908 if (cfg->fc_gw_family == AF_INET &&
909 cfg->fc_gw4 != nh->fib_nh_gw4)
910 return 1;
911
912 if (cfg->fc_gw_family == AF_INET6 &&
913 ipv6_addr_cmp(&cfg->fc_gw6, &nh->fib_nh_gw6))
914 return 1;
915
916 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000917 }
918
919#ifdef CONFIG_IP_ROUTE_MULTIPATH
920 if (!cfg->fc_mp)
921 return 0;
922
923 rtnh = cfg->fc_mp;
924 remaining = cfg->fc_mp_len;
925
926 for_nexthops(fi) {
927 int attrlen;
928
929 if (!rtnh_ok(rtnh, remaining))
930 return -EINVAL;
931
David Brazdil0f672f62019-12-10 10:32:29 +0000932 if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000933 return 1;
934
935 attrlen = rtnh_attrlen(rtnh);
936 if (attrlen > 0) {
David Brazdil0f672f62019-12-10 10:32:29 +0000937 struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
Olivier Deprez157378f2022-04-04 15:47:50 +0200938 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000939
940 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
David Brazdil0f672f62019-12-10 10:32:29 +0000941 nlav = nla_find(attrs, attrlen, RTA_VIA);
942 if (nla && nlav) {
943 NL_SET_ERR_MSG(extack,
944 "Nexthop configuration can not contain both GATEWAY and VIA");
945 return -EINVAL;
946 }
947
948 if (nla) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200949 __be32 gw;
950
951 err = fib_gw_from_attr(&gw, nla, extack);
952 if (err)
953 return err;
954
David Brazdil0f672f62019-12-10 10:32:29 +0000955 if (nh->fib_nh_gw_family != AF_INET ||
Olivier Deprez157378f2022-04-04 15:47:50 +0200956 gw != nh->fib_nh_gw4)
David Brazdil0f672f62019-12-10 10:32:29 +0000957 return 1;
958 } else if (nlav) {
959 struct fib_config cfg2;
David Brazdil0f672f62019-12-10 10:32:29 +0000960
961 err = fib_gw_from_via(&cfg2, nlav, extack);
962 if (err)
963 return err;
964
965 switch (nh->fib_nh_gw_family) {
966 case AF_INET:
967 if (cfg2.fc_gw_family != AF_INET ||
968 cfg2.fc_gw4 != nh->fib_nh_gw4)
969 return 1;
970 break;
971 case AF_INET6:
972 if (cfg2.fc_gw_family != AF_INET6 ||
973 ipv6_addr_cmp(&cfg2.fc_gw6,
974 &nh->fib_nh_gw6))
975 return 1;
976 break;
977 }
978 }
979
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000980#ifdef CONFIG_IP_ROUTE_CLASSID
981 nla = nla_find(attrs, attrlen, RTA_FLOW);
Olivier Deprez157378f2022-04-04 15:47:50 +0200982 if (nla) {
983 if (nla_len(nla) < sizeof(u32)) {
984 NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
985 return -EINVAL;
986 }
987 if (nla_get_u32(nla) != nh->nh_tclassid)
988 return 1;
989 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000990#endif
991 }
992
993 rtnh = rtnh_next(rtnh, &remaining);
994 } endfor_nexthops(fi);
995#endif
996 return 0;
997}
998
999bool fib_metrics_match(struct fib_config *cfg, struct fib_info *fi)
1000{
1001 struct nlattr *nla;
1002 int remaining;
1003
1004 if (!cfg->fc_mx)
1005 return true;
1006
1007 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1008 int type = nla_type(nla);
1009 u32 fi_val, val;
1010
1011 if (!type)
1012 continue;
1013 if (type > RTAX_MAX)
1014 return false;
1015
1016 if (type == RTAX_CC_ALGO) {
1017 char tmp[TCP_CA_NAME_MAX];
1018 bool ecn_ca = false;
1019
1020 nla_strlcpy(tmp, nla, sizeof(tmp));
1021 val = tcp_ca_get_key_by_name(fi->fib_net, tmp, &ecn_ca);
1022 } else {
1023 if (nla_len(nla) != sizeof(u32))
1024 return false;
1025 val = nla_get_u32(nla);
1026 }
1027
1028 fi_val = fi->fib_metrics->metrics[type - 1];
1029 if (type == RTAX_FEATURES)
1030 fi_val &= ~DST_FEATURE_ECN_CA;
1031
1032 if (fi_val != val)
1033 return false;
1034 }
1035
1036 return true;
1037}
1038
David Brazdil0f672f62019-12-10 10:32:29 +00001039static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh,
1040 u32 table, struct netlink_ext_ack *extack)
1041{
1042 struct fib6_config cfg = {
1043 .fc_table = table,
1044 .fc_flags = nh->fib_nh_flags | RTF_GATEWAY,
1045 .fc_ifindex = nh->fib_nh_oif,
1046 .fc_gateway = nh->fib_nh_gw6,
1047 };
1048 struct fib6_nh fib6_nh = {};
1049 int err;
1050
1051 err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack);
1052 if (!err) {
1053 nh->fib_nh_dev = fib6_nh.fib_nh_dev;
1054 dev_hold(nh->fib_nh_dev);
1055 nh->fib_nh_oif = nh->fib_nh_dev->ifindex;
1056 nh->fib_nh_scope = RT_SCOPE_LINK;
1057
1058 ipv6_stub->fib6_nh_release(&fib6_nh);
1059 }
1060
1061 return err;
1062}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001063
1064/*
1065 * Picture
1066 * -------
1067 *
1068 * Semantics of nexthop is very messy by historical reasons.
1069 * We have to take into account, that:
1070 * a) gateway can be actually local interface address,
1071 * so that gatewayed route is direct.
1072 * b) gateway must be on-link address, possibly
1073 * described not by an ifaddr, but also by a direct route.
1074 * c) If both gateway and interface are specified, they should not
1075 * contradict.
1076 * d) If we use tunnel routes, gateway could be not on-link.
1077 *
1078 * Attempt to reconcile all of these (alas, self-contradictory) conditions
1079 * results in pretty ugly and hairy code with obscure logic.
1080 *
1081 * I chose to generalized it instead, so that the size
1082 * of code does not increase practically, but it becomes
1083 * much more general.
1084 * Every prefix is assigned a "scope" value: "host" is local address,
1085 * "link" is direct route,
1086 * [ ... "site" ... "interior" ... ]
1087 * and "universe" is true gateway route with global meaning.
1088 *
1089 * Every prefix refers to a set of "nexthop"s (gw, oif),
1090 * where gw must have narrower scope. This recursion stops
1091 * when gw has LOCAL scope or if "nexthop" is declared ONLINK,
1092 * which means that gw is forced to be on link.
1093 *
1094 * Code is still hairy, but now it is apparently logically
1095 * consistent and very flexible. F.e. as by-product it allows
1096 * to co-exists in peace independent exterior and interior
1097 * routing processes.
1098 *
1099 * Normally it looks as following.
1100 *
1101 * {universe prefix} -> (gw, oif) [scope link]
1102 * |
1103 * |-> {link prefix} -> (gw, oif) [scope local]
1104 * |
1105 * |-> {local prefix} (terminal node)
1106 */
David Brazdil0f672f62019-12-10 10:32:29 +00001107static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
1108 u8 scope, struct netlink_ext_ack *extack)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001109{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001110 struct net_device *dev;
David Brazdil0f672f62019-12-10 10:32:29 +00001111 struct fib_result res;
1112 int err = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001113
David Brazdil0f672f62019-12-10 10:32:29 +00001114 if (nh->fib_nh_flags & RTNH_F_ONLINK) {
1115 unsigned int addr_type;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001116
David Brazdil0f672f62019-12-10 10:32:29 +00001117 if (scope >= RT_SCOPE_LINK) {
1118 NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
1119 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001120 }
David Brazdil0f672f62019-12-10 10:32:29 +00001121 dev = __dev_get_by_index(net, nh->fib_nh_oif);
1122 if (!dev) {
1123 NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
1124 return -ENODEV;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001125 }
David Brazdil0f672f62019-12-10 10:32:29 +00001126 if (!(dev->flags & IFF_UP)) {
1127 NL_SET_ERR_MSG(extack, "Nexthop device is not up");
1128 return -ENETDOWN;
1129 }
1130 addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
1131 if (addr_type != RTN_UNICAST) {
1132 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1133 return -EINVAL;
1134 }
1135 if (!netif_carrier_ok(dev))
1136 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1137 nh->fib_nh_dev = dev;
1138 dev_hold(dev);
1139 nh->fib_nh_scope = RT_SCOPE_LINK;
1140 return 0;
1141 }
1142 rcu_read_lock();
1143 {
1144 struct fib_table *tbl = NULL;
1145 struct flowi4 fl4 = {
1146 .daddr = nh->fib_nh_gw4,
1147 .flowi4_scope = scope + 1,
1148 .flowi4_oif = nh->fib_nh_oif,
1149 .flowi4_iif = LOOPBACK_IFINDEX,
1150 };
1151
1152 /* It is not necessary, but requires a bit of thinking */
1153 if (fl4.flowi4_scope < RT_SCOPE_LINK)
1154 fl4.flowi4_scope = RT_SCOPE_LINK;
1155
Olivier Deprez0e641232021-09-23 10:07:05 +02001156 if (table && table != RT_TABLE_MAIN)
David Brazdil0f672f62019-12-10 10:32:29 +00001157 tbl = fib_get_table(net, table);
1158
1159 if (tbl)
1160 err = fib_table_lookup(tbl, &fl4, &res,
1161 FIB_LOOKUP_IGNORE_LINKSTATE |
1162 FIB_LOOKUP_NOREF);
1163
1164 /* on error or if no table given do full lookup. This
1165 * is needed for example when nexthops are in the local
1166 * table rather than the given table
1167 */
1168 if (!tbl || err) {
1169 err = fib_lookup(net, &fl4, &res,
1170 FIB_LOOKUP_IGNORE_LINKSTATE);
1171 }
1172
1173 if (err) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001174 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1175 goto out;
1176 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001177 }
David Brazdil0f672f62019-12-10 10:32:29 +00001178
1179 err = -EINVAL;
1180 if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
1181 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1182 goto out;
1183 }
1184 nh->fib_nh_scope = res.scope;
1185 nh->fib_nh_oif = FIB_RES_OIF(res);
1186 nh->fib_nh_dev = dev = FIB_RES_DEV(res);
1187 if (!dev) {
1188 NL_SET_ERR_MSG(extack,
1189 "No egress device for nexthop gateway");
1190 goto out;
1191 }
1192 dev_hold(dev);
1193 if (!netif_carrier_ok(dev))
1194 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1195 err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001196out:
1197 rcu_read_unlock();
1198 return err;
1199}
1200
David Brazdil0f672f62019-12-10 10:32:29 +00001201static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
1202 struct netlink_ext_ack *extack)
1203{
1204 struct in_device *in_dev;
1205 int err;
1206
1207 if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
1208 NL_SET_ERR_MSG(extack,
1209 "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
1210 return -EINVAL;
1211 }
1212
1213 rcu_read_lock();
1214
1215 err = -ENODEV;
1216 in_dev = inetdev_by_index(net, nh->fib_nh_oif);
1217 if (!in_dev)
1218 goto out;
1219 err = -ENETDOWN;
1220 if (!(in_dev->dev->flags & IFF_UP)) {
1221 NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
1222 goto out;
1223 }
1224
1225 nh->fib_nh_dev = in_dev->dev;
1226 dev_hold(nh->fib_nh_dev);
1227 nh->fib_nh_scope = RT_SCOPE_HOST;
1228 if (!netif_carrier_ok(nh->fib_nh_dev))
1229 nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1230 err = 0;
1231out:
1232 rcu_read_unlock();
1233 return err;
1234}
1235
1236int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
1237 struct netlink_ext_ack *extack)
1238{
1239 int err;
1240
1241 if (nh->fib_nh_gw_family == AF_INET)
1242 err = fib_check_nh_v4_gw(net, nh, table, scope, extack);
1243 else if (nh->fib_nh_gw_family == AF_INET6)
1244 err = fib_check_nh_v6_gw(net, nh, table, extack);
1245 else
1246 err = fib_check_nh_nongw(net, nh, extack);
1247
1248 return err;
1249}
1250
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001251static inline unsigned int fib_laddr_hashfn(__be32 val)
1252{
1253 unsigned int mask = (fib_info_hash_size - 1);
1254
1255 return ((__force u32)val ^
1256 ((__force u32)val >> 7) ^
1257 ((__force u32)val >> 14)) & mask;
1258}
1259
1260static struct hlist_head *fib_info_hash_alloc(int bytes)
1261{
1262 if (bytes <= PAGE_SIZE)
1263 return kzalloc(bytes, GFP_KERNEL);
1264 else
1265 return (struct hlist_head *)
1266 __get_free_pages(GFP_KERNEL | __GFP_ZERO,
1267 get_order(bytes));
1268}
1269
1270static void fib_info_hash_free(struct hlist_head *hash, int bytes)
1271{
1272 if (!hash)
1273 return;
1274
1275 if (bytes <= PAGE_SIZE)
1276 kfree(hash);
1277 else
1278 free_pages((unsigned long) hash, get_order(bytes));
1279}
1280
1281static void fib_info_hash_move(struct hlist_head *new_info_hash,
1282 struct hlist_head *new_laddrhash,
1283 unsigned int new_size)
1284{
1285 struct hlist_head *old_info_hash, *old_laddrhash;
1286 unsigned int old_size = fib_info_hash_size;
1287 unsigned int i, bytes;
1288
1289 spin_lock_bh(&fib_info_lock);
1290 old_info_hash = fib_info_hash;
1291 old_laddrhash = fib_info_laddrhash;
1292 fib_info_hash_size = new_size;
1293
1294 for (i = 0; i < old_size; i++) {
1295 struct hlist_head *head = &fib_info_hash[i];
1296 struct hlist_node *n;
1297 struct fib_info *fi;
1298
1299 hlist_for_each_entry_safe(fi, n, head, fib_hash) {
1300 struct hlist_head *dest;
1301 unsigned int new_hash;
1302
1303 new_hash = fib_info_hashfn(fi);
1304 dest = &new_info_hash[new_hash];
1305 hlist_add_head(&fi->fib_hash, dest);
1306 }
1307 }
1308 fib_info_hash = new_info_hash;
1309
1310 for (i = 0; i < old_size; i++) {
1311 struct hlist_head *lhead = &fib_info_laddrhash[i];
1312 struct hlist_node *n;
1313 struct fib_info *fi;
1314
1315 hlist_for_each_entry_safe(fi, n, lhead, fib_lhash) {
1316 struct hlist_head *ldest;
1317 unsigned int new_hash;
1318
1319 new_hash = fib_laddr_hashfn(fi->fib_prefsrc);
1320 ldest = &new_laddrhash[new_hash];
1321 hlist_add_head(&fi->fib_lhash, ldest);
1322 }
1323 }
1324 fib_info_laddrhash = new_laddrhash;
1325
1326 spin_unlock_bh(&fib_info_lock);
1327
1328 bytes = old_size * sizeof(struct hlist_head *);
1329 fib_info_hash_free(old_info_hash, bytes);
1330 fib_info_hash_free(old_laddrhash, bytes);
1331}
1332
David Brazdil0f672f62019-12-10 10:32:29 +00001333__be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
1334 unsigned char scope)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001335{
David Brazdil0f672f62019-12-10 10:32:29 +00001336 struct fib_nh *nh;
1337
1338 if (nhc->nhc_family != AF_INET)
1339 return inet_select_addr(nhc->nhc_dev, 0, scope);
1340
1341 nh = container_of(nhc, struct fib_nh, nh_common);
1342 nh->nh_saddr = inet_select_addr(nh->fib_nh_dev, nh->fib_nh_gw4, scope);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001343 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
1344
1345 return nh->nh_saddr;
1346}
1347
David Brazdil0f672f62019-12-10 10:32:29 +00001348__be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1349{
1350 struct fib_nh_common *nhc = res->nhc;
1351
1352 if (res->fi->fib_prefsrc)
1353 return res->fi->fib_prefsrc;
1354
1355 if (nhc->nhc_family == AF_INET) {
1356 struct fib_nh *nh;
1357
1358 nh = container_of(nhc, struct fib_nh, nh_common);
1359 if (nh->nh_saddr_genid == atomic_read(&net->ipv4.dev_addr_genid))
1360 return nh->nh_saddr;
1361 }
1362
1363 return fib_info_update_nhc_saddr(net, nhc, res->fi->fib_scope);
1364}
1365
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001366static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
1367{
1368 if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
1369 fib_prefsrc != cfg->fc_dst) {
1370 u32 tb_id = cfg->fc_table;
1371 int rc;
1372
1373 if (tb_id == RT_TABLE_MAIN)
1374 tb_id = RT_TABLE_LOCAL;
1375
1376 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1377 fib_prefsrc, tb_id);
1378
1379 if (rc != RTN_LOCAL && tb_id != RT_TABLE_LOCAL) {
1380 rc = inet_addr_type_table(cfg->fc_nlinfo.nl_net,
1381 fib_prefsrc, RT_TABLE_LOCAL);
1382 }
1383
1384 if (rc != RTN_LOCAL)
1385 return false;
1386 }
1387 return true;
1388}
1389
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001390struct fib_info *fib_create_info(struct fib_config *cfg,
1391 struct netlink_ext_ack *extack)
1392{
1393 int err;
1394 struct fib_info *fi = NULL;
David Brazdil0f672f62019-12-10 10:32:29 +00001395 struct nexthop *nh = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001396 struct fib_info *ofi;
1397 int nhs = 1;
1398 struct net *net = cfg->fc_nlinfo.nl_net;
1399
1400 if (cfg->fc_type > RTN_MAX)
1401 goto err_inval;
1402
1403 /* Fast check to catch the most weird cases */
1404 if (fib_props[cfg->fc_type].scope > cfg->fc_scope) {
1405 NL_SET_ERR_MSG(extack, "Invalid scope");
1406 goto err_inval;
1407 }
1408
1409 if (cfg->fc_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)) {
1410 NL_SET_ERR_MSG(extack,
1411 "Invalid rtm_flags - can not contain DEAD or LINKDOWN");
1412 goto err_inval;
1413 }
1414
David Brazdil0f672f62019-12-10 10:32:29 +00001415 if (cfg->fc_nh_id) {
1416 if (!cfg->fc_mx) {
1417 fi = fib_find_info_nh(net, cfg);
1418 if (fi) {
1419 fi->fib_treeref++;
1420 return fi;
1421 }
1422 }
1423
1424 nh = nexthop_find_by_id(net, cfg->fc_nh_id);
1425 if (!nh) {
1426 NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
1427 goto err_inval;
1428 }
1429 nhs = 0;
1430 }
1431
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001432#ifdef CONFIG_IP_ROUTE_MULTIPATH
1433 if (cfg->fc_mp) {
1434 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
1435 if (nhs == 0)
1436 goto err_inval;
1437 }
1438#endif
1439
1440 err = -ENOBUFS;
Olivier Deprez157378f2022-04-04 15:47:50 +02001441
1442 /* Paired with WRITE_ONCE() in fib_release_info() */
1443 if (READ_ONCE(fib_info_cnt) >= fib_info_hash_size) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001444 unsigned int new_size = fib_info_hash_size << 1;
1445 struct hlist_head *new_info_hash;
1446 struct hlist_head *new_laddrhash;
1447 unsigned int bytes;
1448
1449 if (!new_size)
1450 new_size = 16;
1451 bytes = new_size * sizeof(struct hlist_head *);
1452 new_info_hash = fib_info_hash_alloc(bytes);
1453 new_laddrhash = fib_info_hash_alloc(bytes);
1454 if (!new_info_hash || !new_laddrhash) {
1455 fib_info_hash_free(new_info_hash, bytes);
1456 fib_info_hash_free(new_laddrhash, bytes);
1457 } else
1458 fib_info_hash_move(new_info_hash, new_laddrhash, new_size);
1459
1460 if (!fib_info_hash_size)
1461 goto failure;
1462 }
1463
David Brazdil0f672f62019-12-10 10:32:29 +00001464 fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001465 if (!fi)
1466 goto failure;
David Brazdil0f672f62019-12-10 10:32:29 +00001467 fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx,
1468 cfg->fc_mx_len, extack);
1469 if (IS_ERR(fi->fib_metrics)) {
1470 err = PTR_ERR(fi->fib_metrics);
1471 kfree(fi);
1472 return ERR_PTR(err);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001473 }
David Brazdil0f672f62019-12-10 10:32:29 +00001474
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001475 fi->fib_net = net;
1476 fi->fib_protocol = cfg->fc_protocol;
1477 fi->fib_scope = cfg->fc_scope;
1478 fi->fib_flags = cfg->fc_flags;
1479 fi->fib_priority = cfg->fc_priority;
1480 fi->fib_prefsrc = cfg->fc_prefsrc;
1481 fi->fib_type = cfg->fc_type;
1482 fi->fib_tb_id = cfg->fc_table;
1483
1484 fi->fib_nhs = nhs;
David Brazdil0f672f62019-12-10 10:32:29 +00001485 if (nh) {
1486 if (!nexthop_get(nh)) {
1487 NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
1488 err = -EINVAL;
1489 } else {
1490 err = 0;
1491 fi->nh = nh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001492 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001493 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00001494 change_nexthops(fi) {
1495 nexthop_nh->nh_parent = fi;
1496 } endfor_nexthops(fi)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001497
David Brazdil0f672f62019-12-10 10:32:29 +00001498 if (cfg->fc_mp)
1499 err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg,
1500 extack);
1501 else
1502 err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001503 }
1504
David Brazdil0f672f62019-12-10 10:32:29 +00001505 if (err != 0)
1506 goto failure;
1507
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001508 if (fib_props[cfg->fc_type].error) {
David Brazdil0f672f62019-12-10 10:32:29 +00001509 if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001510 NL_SET_ERR_MSG(extack,
1511 "Gateway, device and multipath can not be specified for this route type");
1512 goto err_inval;
1513 }
1514 goto link_it;
1515 } else {
1516 switch (cfg->fc_type) {
1517 case RTN_UNICAST:
1518 case RTN_LOCAL:
1519 case RTN_BROADCAST:
1520 case RTN_ANYCAST:
1521 case RTN_MULTICAST:
1522 break;
1523 default:
1524 NL_SET_ERR_MSG(extack, "Invalid route type");
1525 goto err_inval;
1526 }
1527 }
1528
1529 if (cfg->fc_scope > RT_SCOPE_HOST) {
1530 NL_SET_ERR_MSG(extack, "Invalid scope");
1531 goto err_inval;
1532 }
1533
David Brazdil0f672f62019-12-10 10:32:29 +00001534 if (fi->nh) {
1535 err = fib_check_nexthop(fi->nh, cfg->fc_scope, extack);
1536 if (err)
1537 goto failure;
1538 } else if (cfg->fc_scope == RT_SCOPE_HOST) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001539 struct fib_nh *nh = fi->fib_nh;
1540
1541 /* Local address is added. */
1542 if (nhs != 1) {
1543 NL_SET_ERR_MSG(extack,
1544 "Route with host scope can not have multiple nexthops");
1545 goto err_inval;
1546 }
David Brazdil0f672f62019-12-10 10:32:29 +00001547 if (nh->fib_nh_gw_family) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001548 NL_SET_ERR_MSG(extack,
1549 "Route with host scope can not have a gateway");
1550 goto err_inval;
1551 }
David Brazdil0f672f62019-12-10 10:32:29 +00001552 nh->fib_nh_scope = RT_SCOPE_NOWHERE;
1553 nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001554 err = -ENODEV;
David Brazdil0f672f62019-12-10 10:32:29 +00001555 if (!nh->fib_nh_dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001556 goto failure;
1557 } else {
1558 int linkdown = 0;
1559
1560 change_nexthops(fi) {
David Brazdil0f672f62019-12-10 10:32:29 +00001561 err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh,
1562 cfg->fc_table, cfg->fc_scope,
1563 extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001564 if (err != 0)
1565 goto failure;
David Brazdil0f672f62019-12-10 10:32:29 +00001566 if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001567 linkdown++;
1568 } endfor_nexthops(fi)
1569 if (linkdown == fi->fib_nhs)
1570 fi->fib_flags |= RTNH_F_LINKDOWN;
1571 }
1572
1573 if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc)) {
1574 NL_SET_ERR_MSG(extack, "Invalid prefsrc address");
1575 goto err_inval;
1576 }
1577
David Brazdil0f672f62019-12-10 10:32:29 +00001578 if (!fi->nh) {
1579 change_nexthops(fi) {
1580 fib_info_update_nhc_saddr(net, &nexthop_nh->nh_common,
1581 fi->fib_scope);
1582 if (nexthop_nh->fib_nh_gw_family == AF_INET6)
1583 fi->fib_nh_is_v6 = true;
1584 } endfor_nexthops(fi)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001585
David Brazdil0f672f62019-12-10 10:32:29 +00001586 fib_rebalance(fi);
1587 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001588
1589link_it:
1590 ofi = fib_find_info(fi);
1591 if (ofi) {
1592 fi->fib_dead = 1;
1593 free_fib_info(fi);
1594 ofi->fib_treeref++;
1595 return ofi;
1596 }
1597
1598 fi->fib_treeref++;
1599 refcount_set(&fi->fib_clntref, 1);
1600 spin_lock_bh(&fib_info_lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02001601 fib_info_cnt++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001602 hlist_add_head(&fi->fib_hash,
1603 &fib_info_hash[fib_info_hashfn(fi)]);
1604 if (fi->fib_prefsrc) {
1605 struct hlist_head *head;
1606
1607 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
1608 hlist_add_head(&fi->fib_lhash, head);
1609 }
David Brazdil0f672f62019-12-10 10:32:29 +00001610 if (fi->nh) {
1611 list_add(&fi->nh_list, &nh->fi_list);
1612 } else {
1613 change_nexthops(fi) {
1614 struct hlist_head *head;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001615
David Brazdil0f672f62019-12-10 10:32:29 +00001616 if (!nexthop_nh->fib_nh_dev)
1617 continue;
Olivier Deprez157378f2022-04-04 15:47:50 +02001618 head = fib_info_devhash_bucket(nexthop_nh->fib_nh_dev);
David Brazdil0f672f62019-12-10 10:32:29 +00001619 hlist_add_head(&nexthop_nh->nh_hash, head);
1620 } endfor_nexthops(fi)
1621 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001622 spin_unlock_bh(&fib_info_lock);
1623 return fi;
1624
1625err_inval:
1626 err = -EINVAL;
1627
1628failure:
1629 if (fi) {
1630 fi->fib_dead = 1;
1631 free_fib_info(fi);
1632 }
1633
1634 return ERR_PTR(err);
1635}
1636
David Brazdil0f672f62019-12-10 10:32:29 +00001637int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
1638 u8 rt_family, unsigned char *flags, bool skip_oif)
1639{
1640 if (nhc->nhc_flags & RTNH_F_DEAD)
1641 *flags |= RTNH_F_DEAD;
1642
1643 if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
1644 *flags |= RTNH_F_LINKDOWN;
1645
1646 rcu_read_lock();
1647 switch (nhc->nhc_family) {
1648 case AF_INET:
1649 if (ip_ignore_linkdown(nhc->nhc_dev))
1650 *flags |= RTNH_F_DEAD;
1651 break;
1652 case AF_INET6:
1653 if (ip6_ignore_linkdown(nhc->nhc_dev))
1654 *flags |= RTNH_F_DEAD;
1655 break;
1656 }
1657 rcu_read_unlock();
1658 }
1659
1660 switch (nhc->nhc_gw_family) {
1661 case AF_INET:
1662 if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1663 goto nla_put_failure;
1664 break;
1665 case AF_INET6:
1666 /* if gateway family does not match nexthop family
1667 * gateway is encoded as RTA_VIA
1668 */
1669 if (rt_family != nhc->nhc_gw_family) {
1670 int alen = sizeof(struct in6_addr);
1671 struct nlattr *nla;
1672 struct rtvia *via;
1673
1674 nla = nla_reserve(skb, RTA_VIA, alen + 2);
1675 if (!nla)
1676 goto nla_put_failure;
1677
1678 via = nla_data(nla);
1679 via->rtvia_family = AF_INET6;
1680 memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen);
1681 } else if (nla_put_in6_addr(skb, RTA_GATEWAY,
1682 &nhc->nhc_gw.ipv6) < 0) {
1683 goto nla_put_failure;
1684 }
1685 break;
1686 }
1687
1688 *flags |= (nhc->nhc_flags & RTNH_F_ONLINK);
1689 if (nhc->nhc_flags & RTNH_F_OFFLOAD)
1690 *flags |= RTNH_F_OFFLOAD;
1691
1692 if (!skip_oif && nhc->nhc_dev &&
1693 nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
1694 goto nla_put_failure;
1695
1696 if (nhc->nhc_lwtstate &&
1697 lwtunnel_fill_encap(skb, nhc->nhc_lwtstate,
1698 RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
1699 goto nla_put_failure;
1700
1701 return 0;
1702
1703nla_put_failure:
1704 return -EMSGSIZE;
1705}
1706EXPORT_SYMBOL_GPL(fib_nexthop_info);
1707
1708#if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
1709int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
Olivier Deprez157378f2022-04-04 15:47:50 +02001710 int nh_weight, u8 rt_family, u32 nh_tclassid)
David Brazdil0f672f62019-12-10 10:32:29 +00001711{
1712 const struct net_device *dev = nhc->nhc_dev;
1713 struct rtnexthop *rtnh;
1714 unsigned char flags = 0;
1715
1716 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1717 if (!rtnh)
1718 goto nla_put_failure;
1719
1720 rtnh->rtnh_hops = nh_weight - 1;
1721 rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1722
1723 if (fib_nexthop_info(skb, nhc, rt_family, &flags, true) < 0)
1724 goto nla_put_failure;
1725
1726 rtnh->rtnh_flags = flags;
1727
Olivier Deprez157378f2022-04-04 15:47:50 +02001728 if (nh_tclassid && nla_put_u32(skb, RTA_FLOW, nh_tclassid))
1729 goto nla_put_failure;
1730
David Brazdil0f672f62019-12-10 10:32:29 +00001731 /* length of rtnetlink header + attributes */
1732 rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1733
1734 return 0;
1735
1736nla_put_failure:
1737 return -EMSGSIZE;
1738}
1739EXPORT_SYMBOL_GPL(fib_add_nexthop);
1740#endif
1741
1742#ifdef CONFIG_IP_ROUTE_MULTIPATH
1743static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1744{
1745 struct nlattr *mp;
1746
1747 mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
1748 if (!mp)
1749 goto nla_put_failure;
1750
1751 if (unlikely(fi->nh)) {
1752 if (nexthop_mpath_fill_node(skb, fi->nh, AF_INET) < 0)
1753 goto nla_put_failure;
1754 goto mp_end;
1755 }
1756
1757 for_nexthops(fi) {
Olivier Deprez157378f2022-04-04 15:47:50 +02001758 u32 nh_tclassid = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001759#ifdef CONFIG_IP_ROUTE_CLASSID
Olivier Deprez157378f2022-04-04 15:47:50 +02001760 nh_tclassid = nh->nh_tclassid;
David Brazdil0f672f62019-12-10 10:32:29 +00001761#endif
Olivier Deprez157378f2022-04-04 15:47:50 +02001762 if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight,
1763 AF_INET, nh_tclassid) < 0)
1764 goto nla_put_failure;
David Brazdil0f672f62019-12-10 10:32:29 +00001765 } endfor_nexthops(fi);
1766
1767mp_end:
1768 nla_nest_end(skb, mp);
1769
1770 return 0;
1771
1772nla_put_failure:
1773 return -EMSGSIZE;
1774}
1775#else
1776static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1777{
1778 return 0;
1779}
1780#endif
1781
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001782int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
Olivier Deprez157378f2022-04-04 15:47:50 +02001783 struct fib_rt_info *fri, unsigned int flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001784{
Olivier Deprez157378f2022-04-04 15:47:50 +02001785 unsigned int nhs = fib_info_num_path(fri->fi);
1786 struct fib_info *fi = fri->fi;
1787 u32 tb_id = fri->tb_id;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001788 struct nlmsghdr *nlh;
1789 struct rtmsg *rtm;
1790
1791 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
1792 if (!nlh)
1793 return -EMSGSIZE;
1794
1795 rtm = nlmsg_data(nlh);
1796 rtm->rtm_family = AF_INET;
Olivier Deprez157378f2022-04-04 15:47:50 +02001797 rtm->rtm_dst_len = fri->dst_len;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001798 rtm->rtm_src_len = 0;
Olivier Deprez157378f2022-04-04 15:47:50 +02001799 rtm->rtm_tos = fri->tos;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001800 if (tb_id < 256)
1801 rtm->rtm_table = tb_id;
1802 else
1803 rtm->rtm_table = RT_TABLE_COMPAT;
1804 if (nla_put_u32(skb, RTA_TABLE, tb_id))
1805 goto nla_put_failure;
Olivier Deprez157378f2022-04-04 15:47:50 +02001806 rtm->rtm_type = fri->type;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001807 rtm->rtm_flags = fi->fib_flags;
1808 rtm->rtm_scope = fi->fib_scope;
1809 rtm->rtm_protocol = fi->fib_protocol;
1810
1811 if (rtm->rtm_dst_len &&
Olivier Deprez157378f2022-04-04 15:47:50 +02001812 nla_put_in_addr(skb, RTA_DST, fri->dst))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001813 goto nla_put_failure;
1814 if (fi->fib_priority &&
1815 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
1816 goto nla_put_failure;
1817 if (rtnetlink_put_metrics(skb, fi->fib_metrics->metrics) < 0)
1818 goto nla_put_failure;
1819
1820 if (fi->fib_prefsrc &&
1821 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
1822 goto nla_put_failure;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001823
David Brazdil0f672f62019-12-10 10:32:29 +00001824 if (fi->nh) {
1825 if (nla_put_u32(skb, RTA_NH_ID, fi->nh->id))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001826 goto nla_put_failure;
David Brazdil0f672f62019-12-10 10:32:29 +00001827 if (nexthop_is_blackhole(fi->nh))
1828 rtm->rtm_type = RTN_BLACKHOLE;
Olivier Deprez157378f2022-04-04 15:47:50 +02001829 if (!fi->fib_net->ipv4.sysctl_nexthop_compat_mode)
1830 goto offload;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001831 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001832
David Brazdil0f672f62019-12-10 10:32:29 +00001833 if (nhs == 1) {
1834 const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
1835 unsigned char flags = 0;
1836
1837 if (fib_nexthop_info(skb, nhc, AF_INET, &flags, false) < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001838 goto nla_put_failure;
1839
David Brazdil0f672f62019-12-10 10:32:29 +00001840 rtm->rtm_flags = flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001841#ifdef CONFIG_IP_ROUTE_CLASSID
David Brazdil0f672f62019-12-10 10:32:29 +00001842 if (nhc->nhc_family == AF_INET) {
1843 struct fib_nh *nh;
1844
1845 nh = container_of(nhc, struct fib_nh, nh_common);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001846 if (nh->nh_tclassid &&
1847 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
1848 goto nla_put_failure;
David Brazdil0f672f62019-12-10 10:32:29 +00001849 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001850#endif
David Brazdil0f672f62019-12-10 10:32:29 +00001851 } else {
1852 if (fib_add_multipath(skb, fi) < 0)
1853 goto nla_put_failure;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001854 }
David Brazdil0f672f62019-12-10 10:32:29 +00001855
Olivier Deprez157378f2022-04-04 15:47:50 +02001856offload:
1857 if (fri->offload)
1858 rtm->rtm_flags |= RTM_F_OFFLOAD;
1859 if (fri->trap)
1860 rtm->rtm_flags |= RTM_F_TRAP;
1861
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001862 nlmsg_end(skb, nlh);
1863 return 0;
1864
1865nla_put_failure:
1866 nlmsg_cancel(skb, nlh);
1867 return -EMSGSIZE;
1868}
1869
1870/*
1871 * Update FIB if:
1872 * - local address disappeared -> we must delete all the entries
1873 * referring to it.
1874 * - device went down -> we must shutdown all nexthops going via it.
1875 */
1876int fib_sync_down_addr(struct net_device *dev, __be32 local)
1877{
1878 int ret = 0;
1879 unsigned int hash = fib_laddr_hashfn(local);
1880 struct hlist_head *head = &fib_info_laddrhash[hash];
David Brazdil0f672f62019-12-10 10:32:29 +00001881 int tb_id = l3mdev_fib_table(dev) ? : RT_TABLE_MAIN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001882 struct net *net = dev_net(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001883 struct fib_info *fi;
1884
1885 if (!fib_info_laddrhash || local == 0)
1886 return 0;
1887
1888 hlist_for_each_entry(fi, head, fib_lhash) {
1889 if (!net_eq(fi->fib_net, net) ||
1890 fi->fib_tb_id != tb_id)
1891 continue;
1892 if (fi->fib_prefsrc == local) {
1893 fi->fib_flags |= RTNH_F_DEAD;
1894 ret++;
1895 }
1896 }
1897 return ret;
1898}
1899
David Brazdil0f672f62019-12-10 10:32:29 +00001900static int call_fib_nh_notifiers(struct fib_nh *nh,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001901 enum fib_event_type event_type)
1902{
David Brazdil0f672f62019-12-10 10:32:29 +00001903 bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001904 struct fib_nh_notifier_info info = {
David Brazdil0f672f62019-12-10 10:32:29 +00001905 .fib_nh = nh,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001906 };
1907
1908 switch (event_type) {
1909 case FIB_EVENT_NH_ADD:
David Brazdil0f672f62019-12-10 10:32:29 +00001910 if (nh->fib_nh_flags & RTNH_F_DEAD)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001911 break;
David Brazdil0f672f62019-12-10 10:32:29 +00001912 if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001913 break;
David Brazdil0f672f62019-12-10 10:32:29 +00001914 return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001915 &info.info);
1916 case FIB_EVENT_NH_DEL:
David Brazdil0f672f62019-12-10 10:32:29 +00001917 if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1918 (nh->fib_nh_flags & RTNH_F_DEAD))
1919 return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001920 event_type, &info.info);
1921 default:
1922 break;
1923 }
1924
1925 return NOTIFY_DONE;
1926}
1927
1928/* Update the PMTU of exceptions when:
1929 * - the new MTU of the first hop becomes smaller than the PMTU
1930 * - the old MTU was the same as the PMTU, and it limited discovery of
1931 * larger MTUs on the path. With that limit raised, we can now
1932 * discover larger MTUs
1933 * A special case is locked exceptions, for which the PMTU is smaller
1934 * than the minimal accepted PMTU:
1935 * - if the new MTU is greater than the PMTU, don't make any change
1936 * - otherwise, unlock and set PMTU
1937 */
David Brazdil0f672f62019-12-10 10:32:29 +00001938void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001939{
1940 struct fnhe_hash_bucket *bucket;
1941 int i;
1942
David Brazdil0f672f62019-12-10 10:32:29 +00001943 bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001944 if (!bucket)
1945 return;
1946
1947 for (i = 0; i < FNHE_HASH_SIZE; i++) {
1948 struct fib_nh_exception *fnhe;
1949
1950 for (fnhe = rcu_dereference_protected(bucket[i].chain, 1);
1951 fnhe;
1952 fnhe = rcu_dereference_protected(fnhe->fnhe_next, 1)) {
1953 if (fnhe->fnhe_mtu_locked) {
1954 if (new <= fnhe->fnhe_pmtu) {
1955 fnhe->fnhe_pmtu = new;
1956 fnhe->fnhe_mtu_locked = false;
1957 }
1958 } else if (new < fnhe->fnhe_pmtu ||
1959 orig == fnhe->fnhe_pmtu) {
1960 fnhe->fnhe_pmtu = new;
1961 }
1962 }
1963 }
1964}
1965
1966void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
1967{
Olivier Deprez157378f2022-04-04 15:47:50 +02001968 struct hlist_head *head = fib_info_devhash_bucket(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001969 struct fib_nh *nh;
1970
1971 hlist_for_each_entry(nh, head, nh_hash) {
David Brazdil0f672f62019-12-10 10:32:29 +00001972 if (nh->fib_nh_dev == dev)
1973 fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001974 }
1975}
1976
1977/* Event force Flags Description
1978 * NETDEV_CHANGE 0 LINKDOWN Carrier OFF, not for scope host
1979 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
1980 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
1981 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
David Brazdil0f672f62019-12-10 10:32:29 +00001982 *
1983 * only used when fib_nh is built into fib_info
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001984 */
1985int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
1986{
Olivier Deprez157378f2022-04-04 15:47:50 +02001987 struct hlist_head *head = fib_info_devhash_bucket(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001988 struct fib_info *prev_fi = NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +02001989 int scope = RT_SCOPE_NOWHERE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001990 struct fib_nh *nh;
Olivier Deprez157378f2022-04-04 15:47:50 +02001991 int ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001992
1993 if (force)
1994 scope = -1;
1995
1996 hlist_for_each_entry(nh, head, nh_hash) {
1997 struct fib_info *fi = nh->nh_parent;
1998 int dead;
1999
2000 BUG_ON(!fi->fib_nhs);
David Brazdil0f672f62019-12-10 10:32:29 +00002001 if (nh->fib_nh_dev != dev || fi == prev_fi)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002002 continue;
2003 prev_fi = fi;
2004 dead = 0;
2005 change_nexthops(fi) {
David Brazdil0f672f62019-12-10 10:32:29 +00002006 if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002007 dead++;
David Brazdil0f672f62019-12-10 10:32:29 +00002008 else if (nexthop_nh->fib_nh_dev == dev &&
2009 nexthop_nh->fib_nh_scope != scope) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002010 switch (event) {
2011 case NETDEV_DOWN:
2012 case NETDEV_UNREGISTER:
David Brazdil0f672f62019-12-10 10:32:29 +00002013 nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
Olivier Deprez157378f2022-04-04 15:47:50 +02002014 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002015 case NETDEV_CHANGE:
David Brazdil0f672f62019-12-10 10:32:29 +00002016 nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002017 break;
2018 }
2019 call_fib_nh_notifiers(nexthop_nh,
2020 FIB_EVENT_NH_DEL);
2021 dead++;
2022 }
2023#ifdef CONFIG_IP_ROUTE_MULTIPATH
2024 if (event == NETDEV_UNREGISTER &&
David Brazdil0f672f62019-12-10 10:32:29 +00002025 nexthop_nh->fib_nh_dev == dev) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002026 dead = fi->fib_nhs;
2027 break;
2028 }
2029#endif
2030 } endfor_nexthops(fi)
2031 if (dead == fi->fib_nhs) {
2032 switch (event) {
2033 case NETDEV_DOWN:
2034 case NETDEV_UNREGISTER:
2035 fi->fib_flags |= RTNH_F_DEAD;
Olivier Deprez157378f2022-04-04 15:47:50 +02002036 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002037 case NETDEV_CHANGE:
2038 fi->fib_flags |= RTNH_F_LINKDOWN;
2039 break;
2040 }
2041 ret++;
2042 }
2043
2044 fib_rebalance(fi);
2045 }
2046
2047 return ret;
2048}
2049
2050/* Must be invoked inside of an RCU protected region. */
2051static void fib_select_default(const struct flowi4 *flp, struct fib_result *res)
2052{
2053 struct fib_info *fi = NULL, *last_resort = NULL;
2054 struct hlist_head *fa_head = res->fa_head;
2055 struct fib_table *tb = res->table;
2056 u8 slen = 32 - res->prefixlen;
2057 int order = -1, last_idx = -1;
2058 struct fib_alias *fa, *fa1 = NULL;
2059 u32 last_prio = res->fi->fib_priority;
2060 u8 last_tos = 0;
2061
2062 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
2063 struct fib_info *next_fi = fa->fa_info;
Olivier Deprez0e641232021-09-23 10:07:05 +02002064 struct fib_nh_common *nhc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002065
2066 if (fa->fa_slen != slen)
2067 continue;
2068 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
2069 continue;
2070 if (fa->tb_id != tb->tb_id)
2071 continue;
2072 if (next_fi->fib_priority > last_prio &&
2073 fa->fa_tos == last_tos) {
2074 if (last_tos)
2075 continue;
2076 break;
2077 }
2078 if (next_fi->fib_flags & RTNH_F_DEAD)
2079 continue;
2080 last_tos = fa->fa_tos;
2081 last_prio = next_fi->fib_priority;
2082
2083 if (next_fi->fib_scope != res->scope ||
2084 fa->fa_type != RTN_UNICAST)
2085 continue;
David Brazdil0f672f62019-12-10 10:32:29 +00002086
Olivier Deprez0e641232021-09-23 10:07:05 +02002087 nhc = fib_info_nhc(next_fi, 0);
2088 if (!nhc->nhc_gw_family || nhc->nhc_scope != RT_SCOPE_LINK)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002089 continue;
2090
2091 fib_alias_accessed(fa);
2092
2093 if (!fi) {
2094 if (next_fi != res->fi)
2095 break;
2096 fa1 = fa;
2097 } else if (!fib_detect_death(fi, order, &last_resort,
2098 &last_idx, fa1->fa_default)) {
2099 fib_result_assign(res, fi);
2100 fa1->fa_default = order;
2101 goto out;
2102 }
2103 fi = next_fi;
2104 order++;
2105 }
2106
2107 if (order <= 0 || !fi) {
2108 if (fa1)
2109 fa1->fa_default = -1;
2110 goto out;
2111 }
2112
2113 if (!fib_detect_death(fi, order, &last_resort, &last_idx,
2114 fa1->fa_default)) {
2115 fib_result_assign(res, fi);
2116 fa1->fa_default = order;
2117 goto out;
2118 }
2119
2120 if (last_idx >= 0)
2121 fib_result_assign(res, last_resort);
2122 fa1->fa_default = last_idx;
2123out:
2124 return;
2125}
2126
2127/*
2128 * Dead device goes up. We wake up dead nexthops.
2129 * It takes sense only on multipath routes.
David Brazdil0f672f62019-12-10 10:32:29 +00002130 *
2131 * only used when fib_nh is built into fib_info
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002132 */
David Brazdil0f672f62019-12-10 10:32:29 +00002133int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002134{
2135 struct fib_info *prev_fi;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002136 struct hlist_head *head;
2137 struct fib_nh *nh;
2138 int ret;
2139
2140 if (!(dev->flags & IFF_UP))
2141 return 0;
2142
2143 if (nh_flags & RTNH_F_DEAD) {
2144 unsigned int flags = dev_get_flags(dev);
2145
2146 if (flags & (IFF_RUNNING | IFF_LOWER_UP))
2147 nh_flags |= RTNH_F_LINKDOWN;
2148 }
2149
2150 prev_fi = NULL;
Olivier Deprez157378f2022-04-04 15:47:50 +02002151 head = fib_info_devhash_bucket(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002152 ret = 0;
2153
2154 hlist_for_each_entry(nh, head, nh_hash) {
2155 struct fib_info *fi = nh->nh_parent;
2156 int alive;
2157
2158 BUG_ON(!fi->fib_nhs);
David Brazdil0f672f62019-12-10 10:32:29 +00002159 if (nh->fib_nh_dev != dev || fi == prev_fi)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002160 continue;
2161
2162 prev_fi = fi;
2163 alive = 0;
2164 change_nexthops(fi) {
David Brazdil0f672f62019-12-10 10:32:29 +00002165 if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002166 alive++;
2167 continue;
2168 }
David Brazdil0f672f62019-12-10 10:32:29 +00002169 if (!nexthop_nh->fib_nh_dev ||
2170 !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002171 continue;
David Brazdil0f672f62019-12-10 10:32:29 +00002172 if (nexthop_nh->fib_nh_dev != dev ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002173 !__in_dev_get_rtnl(dev))
2174 continue;
2175 alive++;
David Brazdil0f672f62019-12-10 10:32:29 +00002176 nexthop_nh->fib_nh_flags &= ~nh_flags;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002177 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
2178 } endfor_nexthops(fi)
2179
2180 if (alive > 0) {
2181 fi->fib_flags &= ~nh_flags;
2182 ret++;
2183 }
2184
2185 fib_rebalance(fi);
2186 }
2187
2188 return ret;
2189}
2190
2191#ifdef CONFIG_IP_ROUTE_MULTIPATH
2192static bool fib_good_nh(const struct fib_nh *nh)
2193{
2194 int state = NUD_REACHABLE;
2195
David Brazdil0f672f62019-12-10 10:32:29 +00002196 if (nh->fib_nh_scope == RT_SCOPE_LINK) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002197 struct neighbour *n;
2198
2199 rcu_read_lock_bh();
2200
David Brazdil0f672f62019-12-10 10:32:29 +00002201 if (likely(nh->fib_nh_gw_family == AF_INET))
2202 n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
2203 (__force u32)nh->fib_nh_gw4);
2204 else if (nh->fib_nh_gw_family == AF_INET6)
2205 n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev,
2206 &nh->fib_nh_gw6);
2207 else
2208 n = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002209 if (n)
2210 state = n->nud_state;
2211
2212 rcu_read_unlock_bh();
2213 }
2214
2215 return !!(state & NUD_VALID);
2216}
2217
2218void fib_select_multipath(struct fib_result *res, int hash)
2219{
2220 struct fib_info *fi = res->fi;
2221 struct net *net = fi->fib_net;
2222 bool first = false;
2223
David Brazdil0f672f62019-12-10 10:32:29 +00002224 if (unlikely(res->fi->nh)) {
2225 nexthop_path_fib_result(res, hash);
2226 return;
2227 }
2228
2229 change_nexthops(fi) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002230 if (net->ipv4.sysctl_fib_multipath_use_neigh) {
David Brazdil0f672f62019-12-10 10:32:29 +00002231 if (!fib_good_nh(nexthop_nh))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002232 continue;
2233 if (!first) {
2234 res->nh_sel = nhsel;
David Brazdil0f672f62019-12-10 10:32:29 +00002235 res->nhc = &nexthop_nh->nh_common;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002236 first = true;
2237 }
2238 }
2239
David Brazdil0f672f62019-12-10 10:32:29 +00002240 if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002241 continue;
2242
2243 res->nh_sel = nhsel;
David Brazdil0f672f62019-12-10 10:32:29 +00002244 res->nhc = &nexthop_nh->nh_common;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002245 return;
2246 } endfor_nexthops(fi);
2247}
2248#endif
2249
2250void fib_select_path(struct net *net, struct fib_result *res,
2251 struct flowi4 *fl4, const struct sk_buff *skb)
2252{
2253 if (fl4->flowi4_oif && !(fl4->flowi4_flags & FLOWI_FLAG_SKIP_NH_OIF))
2254 goto check_saddr;
2255
2256#ifdef CONFIG_IP_ROUTE_MULTIPATH
David Brazdil0f672f62019-12-10 10:32:29 +00002257 if (fib_info_num_path(res->fi) > 1) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002258 int h = fib_multipath_hash(net, fl4, skb, NULL);
2259
2260 fib_select_multipath(res, h);
2261 }
2262 else
2263#endif
2264 if (!res->prefixlen &&
2265 res->table->tb_num_default > 1 &&
2266 res->type == RTN_UNICAST)
2267 fib_select_default(fl4, res);
2268
2269check_saddr:
2270 if (!fl4->saddr)
David Brazdil0f672f62019-12-10 10:32:29 +00002271 fl4->saddr = fib_result_prefsrc(net, res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002272}