hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/net/ipv4/fib_semantics.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * INET An implementation of the TCP/IP protocol suite for the LINUX
34 * operating system. INET is implemented using the BSD Socket
....@@ -6,11 +7,6 @@
67 * IPv4 Forwarding Information Base: semantics.
78 *
89 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9
- *
10
- * This program is free software; you can redistribute it and/or
11
- * modify it under the terms of the GNU General Public License
12
- * as published by the Free Software Foundation; either version
13
- * 2 of the License, or (at your option) any later version.
1410 */
1511
1612 #include <linux/uaccess.h>
....@@ -33,6 +29,7 @@
3329 #include <linux/init.h>
3430 #include <linux/slab.h>
3531 #include <linux/netlink.h>
32
+#include <linux/hash.h>
3633
3734 #include <net/arp.h>
3835 #include <net/ip.h>
....@@ -41,10 +38,13 @@
4138 #include <net/tcp.h>
4239 #include <net/sock.h>
4340 #include <net/ip_fib.h>
44
-#include <net/netlink.h>
41
+#include <net/ip6_fib.h>
4542 #include <net/nexthop.h>
43
+#include <net/netlink.h>
44
+#include <net/rtnh.h>
4645 #include <net/lwtunnel.h>
4746 #include <net/fib_notifier.h>
47
+#include <net/addrconf.h>
4848
4949 #include "fib_lookup.h"
5050
....@@ -58,18 +58,21 @@
5858 #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS)
5959 static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE];
6060
61
+/* for_nexthops and change_nexthops only used when nexthop object
62
+ * is not set in a fib_info. The logic within can reference fib_nh.
63
+ */
6164 #ifdef CONFIG_IP_ROUTE_MULTIPATH
6265
6366 #define for_nexthops(fi) { \
6467 int nhsel; const struct fib_nh *nh; \
6568 for (nhsel = 0, nh = (fi)->fib_nh; \
66
- nhsel < (fi)->fib_nhs; \
69
+ nhsel < fib_info_num_path((fi)); \
6770 nh++, nhsel++)
6871
6972 #define change_nexthops(fi) { \
7073 int nhsel; struct fib_nh *nexthop_nh; \
7174 for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \
72
- nhsel < (fi)->fib_nhs; \
75
+ nhsel < fib_info_num_path((fi)); \
7376 nexthop_nh++, nhsel++)
7477
7578 #else /* CONFIG_IP_ROUTE_MULTIPATH */
....@@ -157,12 +160,12 @@
157160 dst_release_immediate(&rt->dst);
158161 }
159162
160
-static void free_nh_exceptions(struct fib_nh *nh)
163
+static void free_nh_exceptions(struct fib_nh_common *nhc)
161164 {
162165 struct fnhe_hash_bucket *hash;
163166 int i;
164167
165
- hash = rcu_dereference_protected(nh->nh_exceptions, 1);
168
+ hash = rcu_dereference_protected(nhc->nhc_exceptions, 1);
166169 if (!hash)
167170 return;
168171 for (i = 0; i < FNHE_HASH_SIZE; i++) {
....@@ -204,24 +207,42 @@
204207 free_percpu(rtp);
205208 }
206209
210
+void fib_nh_common_release(struct fib_nh_common *nhc)
211
+{
212
+ if (nhc->nhc_dev)
213
+ dev_put(nhc->nhc_dev);
214
+
215
+ lwtstate_put(nhc->nhc_lwtstate);
216
+ rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
217
+ rt_fibinfo_free(&nhc->nhc_rth_input);
218
+ free_nh_exceptions(nhc);
219
+}
220
+EXPORT_SYMBOL_GPL(fib_nh_common_release);
221
+
222
+void fib_nh_release(struct net *net, struct fib_nh *fib_nh)
223
+{
224
+#ifdef CONFIG_IP_ROUTE_CLASSID
225
+ if (fib_nh->nh_tclassid)
226
+ atomic_dec(&net->ipv4.fib_num_tclassid_users);
227
+#endif
228
+ fib_nh_common_release(&fib_nh->nh_common);
229
+}
230
+
207231 /* Release a nexthop info record */
208232 static void free_fib_info_rcu(struct rcu_head *head)
209233 {
210234 struct fib_info *fi = container_of(head, struct fib_info, rcu);
211
- struct dst_metrics *m;
212235
213
- change_nexthops(fi) {
214
- if (nexthop_nh->nh_dev)
215
- dev_put(nexthop_nh->nh_dev);
216
- lwtstate_put(nexthop_nh->nh_lwtstate);
217
- free_nh_exceptions(nexthop_nh);
218
- rt_fibinfo_free_cpus(nexthop_nh->nh_pcpu_rth_output);
219
- rt_fibinfo_free(&nexthop_nh->nh_rth_input);
220
- } endfor_nexthops(fi);
236
+ if (fi->nh) {
237
+ nexthop_put(fi->nh);
238
+ } else {
239
+ change_nexthops(fi) {
240
+ fib_nh_release(fi->fib_net, nexthop_nh);
241
+ } endfor_nexthops(fi);
242
+ }
221243
222
- m = fi->fib_metrics;
223
- if (m != &dst_default_metrics && refcount_dec_and_test(&m->refcnt))
224
- kfree(m);
244
+ ip_fib_metrics_put(fi->fib_metrics);
245
+
225246 kfree(fi);
226247 }
227248
....@@ -231,13 +252,7 @@
231252 pr_warn("Freeing alive fib_info %p\n", fi);
232253 return;
233254 }
234
- fib_info_cnt--;
235
-#ifdef CONFIG_IP_ROUTE_CLASSID
236
- change_nexthops(fi) {
237
- if (nexthop_nh->nh_tclassid)
238
- fi->fib_net->ipv4.fib_num_tclassid_users--;
239
- } endfor_nexthops(fi);
240
-#endif
255
+
241256 call_rcu(&fi->rcu, free_fib_info_rcu);
242257 }
243258 EXPORT_SYMBOL_GPL(free_fib_info);
....@@ -247,66 +262,149 @@
247262 spin_lock_bh(&fib_info_lock);
248263 if (fi && --fi->fib_treeref == 0) {
249264 hlist_del(&fi->fib_hash);
265
+
266
+ /* Paired with READ_ONCE() in fib_create_info(). */
267
+ WRITE_ONCE(fib_info_cnt, fib_info_cnt - 1);
268
+
250269 if (fi->fib_prefsrc)
251270 hlist_del(&fi->fib_lhash);
252
- change_nexthops(fi) {
253
- if (!nexthop_nh->nh_dev)
254
- continue;
255
- hlist_del(&nexthop_nh->nh_hash);
256
- } endfor_nexthops(fi)
271
+ if (fi->nh) {
272
+ list_del(&fi->nh_list);
273
+ } else {
274
+ change_nexthops(fi) {
275
+ if (!nexthop_nh->fib_nh_dev)
276
+ continue;
277
+ hlist_del(&nexthop_nh->nh_hash);
278
+ } endfor_nexthops(fi)
279
+ }
257280 fi->fib_dead = 1;
258281 fib_info_put(fi);
259282 }
260283 spin_unlock_bh(&fib_info_lock);
261284 }
262285
263
-static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
286
+static inline int nh_comp(struct fib_info *fi, struct fib_info *ofi)
264287 {
265
- const struct fib_nh *onh = ofi->fib_nh;
288
+ const struct fib_nh *onh;
289
+
290
+ if (fi->nh || ofi->nh)
291
+ return nexthop_cmp(fi->nh, ofi->nh) ? 0 : -1;
292
+
293
+ if (ofi->fib_nhs == 0)
294
+ return 0;
266295
267296 for_nexthops(fi) {
268
- if (nh->nh_oif != onh->nh_oif ||
269
- nh->nh_gw != onh->nh_gw ||
270
- nh->nh_scope != onh->nh_scope ||
297
+ onh = fib_info_nh(ofi, nhsel);
298
+
299
+ if (nh->fib_nh_oif != onh->fib_nh_oif ||
300
+ nh->fib_nh_gw_family != onh->fib_nh_gw_family ||
301
+ nh->fib_nh_scope != onh->fib_nh_scope ||
271302 #ifdef CONFIG_IP_ROUTE_MULTIPATH
272
- nh->nh_weight != onh->nh_weight ||
303
+ nh->fib_nh_weight != onh->fib_nh_weight ||
273304 #endif
274305 #ifdef CONFIG_IP_ROUTE_CLASSID
275306 nh->nh_tclassid != onh->nh_tclassid ||
276307 #endif
277
- lwtunnel_cmp_encap(nh->nh_lwtstate, onh->nh_lwtstate) ||
278
- ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_COMPARE_MASK))
308
+ lwtunnel_cmp_encap(nh->fib_nh_lws, onh->fib_nh_lws) ||
309
+ ((nh->fib_nh_flags ^ onh->fib_nh_flags) & ~RTNH_COMPARE_MASK))
279310 return -1;
280
- onh++;
311
+
312
+ if (nh->fib_nh_gw_family == AF_INET &&
313
+ nh->fib_nh_gw4 != onh->fib_nh_gw4)
314
+ return -1;
315
+
316
+ if (nh->fib_nh_gw_family == AF_INET6 &&
317
+ ipv6_addr_cmp(&nh->fib_nh_gw6, &onh->fib_nh_gw6))
318
+ return -1;
281319 } endfor_nexthops(fi);
282320 return 0;
283321 }
284322
285323 static inline unsigned int fib_devindex_hashfn(unsigned int val)
286324 {
287
- unsigned int mask = DEVINDEX_HASHSIZE - 1;
288
-
289
- return (val ^
290
- (val >> DEVINDEX_HASHBITS) ^
291
- (val >> (DEVINDEX_HASHBITS * 2))) & mask;
325
+ return hash_32(val, DEVINDEX_HASHBITS);
292326 }
293327
294
-static inline unsigned int fib_info_hashfn(const struct fib_info *fi)
328
+static struct hlist_head *
329
+fib_info_devhash_bucket(const struct net_device *dev)
330
+{
331
+ u32 val = net_hash_mix(dev_net(dev)) ^ dev->ifindex;
332
+
333
+ return &fib_info_devhash[fib_devindex_hashfn(val)];
334
+}
335
+
336
+static unsigned int fib_info_hashfn_1(int init_val, u8 protocol, u8 scope,
337
+ u32 prefsrc, u32 priority)
338
+{
339
+ unsigned int val = init_val;
340
+
341
+ val ^= (protocol << 8) | scope;
342
+ val ^= prefsrc;
343
+ val ^= priority;
344
+
345
+ return val;
346
+}
347
+
348
+static unsigned int fib_info_hashfn_result(unsigned int val)
295349 {
296350 unsigned int mask = (fib_info_hash_size - 1);
297
- unsigned int val = fi->fib_nhs;
298
-
299
- val ^= (fi->fib_protocol << 8) | fi->fib_scope;
300
- val ^= (__force u32)fi->fib_prefsrc;
301
- val ^= fi->fib_priority;
302
- for_nexthops(fi) {
303
- val ^= fib_devindex_hashfn(nh->nh_oif);
304
- } endfor_nexthops(fi)
305351
306352 return (val ^ (val >> 7) ^ (val >> 12)) & mask;
307353 }
308354
309
-static struct fib_info *fib_find_info(const struct fib_info *nfi)
355
+static inline unsigned int fib_info_hashfn(struct fib_info *fi)
356
+{
357
+ unsigned int val;
358
+
359
+ val = fib_info_hashfn_1(fi->fib_nhs, fi->fib_protocol,
360
+ fi->fib_scope, (__force u32)fi->fib_prefsrc,
361
+ fi->fib_priority);
362
+
363
+ if (fi->nh) {
364
+ val ^= fib_devindex_hashfn(fi->nh->id);
365
+ } else {
366
+ for_nexthops(fi) {
367
+ val ^= fib_devindex_hashfn(nh->fib_nh_oif);
368
+ } endfor_nexthops(fi)
369
+ }
370
+
371
+ return fib_info_hashfn_result(val);
372
+}
373
+
374
+/* no metrics, only nexthop id */
375
+static struct fib_info *fib_find_info_nh(struct net *net,
376
+ const struct fib_config *cfg)
377
+{
378
+ struct hlist_head *head;
379
+ struct fib_info *fi;
380
+ unsigned int hash;
381
+
382
+ hash = fib_info_hashfn_1(fib_devindex_hashfn(cfg->fc_nh_id),
383
+ cfg->fc_protocol, cfg->fc_scope,
384
+ (__force u32)cfg->fc_prefsrc,
385
+ cfg->fc_priority);
386
+ hash = fib_info_hashfn_result(hash);
387
+ head = &fib_info_hash[hash];
388
+
389
+ hlist_for_each_entry(fi, head, fib_hash) {
390
+ if (!net_eq(fi->fib_net, net))
391
+ continue;
392
+ if (!fi->nh || fi->nh->id != cfg->fc_nh_id)
393
+ continue;
394
+ if (cfg->fc_protocol == fi->fib_protocol &&
395
+ cfg->fc_scope == fi->fib_scope &&
396
+ cfg->fc_prefsrc == fi->fib_prefsrc &&
397
+ cfg->fc_priority == fi->fib_priority &&
398
+ cfg->fc_type == fi->fib_type &&
399
+ cfg->fc_table == fi->fib_tb_id &&
400
+ !((cfg->fc_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK))
401
+ return fi;
402
+ }
403
+
404
+ return NULL;
405
+}
406
+
407
+static struct fib_info *fib_find_info(struct fib_info *nfi)
310408 {
311409 struct hlist_head *head;
312410 struct fib_info *fi;
....@@ -325,10 +423,11 @@
325423 nfi->fib_prefsrc == fi->fib_prefsrc &&
326424 nfi->fib_priority == fi->fib_priority &&
327425 nfi->fib_type == fi->fib_type &&
426
+ nfi->fib_tb_id == fi->fib_tb_id &&
328427 memcmp(nfi->fib_metrics, fi->fib_metrics,
329428 sizeof(u32) * RTAX_MAX) == 0 &&
330429 !((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_COMPARE_MASK) &&
331
- (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0))
430
+ nh_comp(fi, nfi) == 0)
332431 return fi;
333432 }
334433
....@@ -342,16 +441,15 @@
342441 {
343442 struct hlist_head *head;
344443 struct fib_nh *nh;
345
- unsigned int hash;
346444
347445 spin_lock(&fib_info_lock);
348446
349
- hash = fib_devindex_hashfn(dev->ifindex);
350
- head = &fib_info_devhash[hash];
447
+ head = fib_info_devhash_bucket(dev);
448
+
351449 hlist_for_each_entry(nh, head, nh_hash) {
352
- if (nh->nh_dev == dev &&
353
- nh->nh_gw == gw &&
354
- !(nh->nh_flags & RTNH_F_DEAD)) {
450
+ if (nh->fib_nh_dev == dev &&
451
+ nh->fib_nh_gw4 == gw &&
452
+ !(nh->fib_nh_flags & RTNH_F_DEAD)) {
355453 spin_unlock(&fib_info_lock);
356454 return 0;
357455 }
....@@ -370,34 +468,40 @@
370468 + nla_total_size(4) /* RTA_PRIORITY */
371469 + nla_total_size(4) /* RTA_PREFSRC */
372470 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */
471
+ unsigned int nhs = fib_info_num_path(fi);
373472
374473 /* space for nested metrics */
375474 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
376475
377
- if (fi->fib_nhs) {
476
+ if (fi->nh)
477
+ payload += nla_total_size(4); /* RTA_NH_ID */
478
+
479
+ if (nhs) {
378480 size_t nh_encapsize = 0;
379
- /* Also handles the special case fib_nhs == 1 */
481
+ /* Also handles the special case nhs == 1 */
380482
381483 /* each nexthop is packed in an attribute */
382484 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
485
+ unsigned int i;
383486
384487 /* may contain flow and gateway attribute */
385488 nhsize += 2 * nla_total_size(4);
386489
387490 /* grab encap info */
388
- for_nexthops(fi) {
389
- if (nh->nh_lwtstate) {
491
+ for (i = 0; i < fib_info_num_path(fi); i++) {
492
+ struct fib_nh_common *nhc = fib_info_nhc(fi, i);
493
+
494
+ if (nhc->nhc_lwtstate) {
390495 /* RTA_ENCAP_TYPE */
391496 nh_encapsize += lwtunnel_get_encap_size(
392
- nh->nh_lwtstate);
497
+ nhc->nhc_lwtstate);
393498 /* RTA_ENCAP */
394499 nh_encapsize += nla_total_size(2);
395500 }
396
- } endfor_nexthops(fi);
501
+ }
397502
398503 /* all nexthops are packed in a nested attribute */
399
- payload += nla_total_size((fi->fib_nhs * nhsize) +
400
- nh_encapsize);
504
+ payload += nla_total_size((nhs * nhsize) + nh_encapsize);
401505
402506 }
403507
....@@ -408,6 +512,7 @@
408512 int dst_len, u32 tb_id, const struct nl_info *info,
409513 unsigned int nlm_flags)
410514 {
515
+ struct fib_rt_info fri;
411516 struct sk_buff *skb;
412517 u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0;
413518 int err = -ENOBUFS;
....@@ -416,9 +521,15 @@
416521 if (!skb)
417522 goto errout;
418523
419
- err = fib_dump_info(skb, info->portid, seq, event, tb_id,
420
- fa->fa_type, key, dst_len,
421
- fa->fa_tos, fa->fa_info, nlm_flags);
524
+ fri.fi = fa->fa_info;
525
+ fri.tb_id = tb_id;
526
+ fri.dst = key;
527
+ fri.dst_len = dst_len;
528
+ fri.tos = fa->fa_tos;
529
+ fri.type = fa->fa_type;
530
+ fri.offload = fa->offload;
531
+ fri.trap = fa->trap;
532
+ err = fib_dump_info(skb, info->portid, seq, event, &fri, nlm_flags);
422533 if (err < 0) {
423534 /* -EMSGSIZE implies BUG in fib_nlmsg_size() */
424535 WARN_ON(err == -EMSGSIZE);
....@@ -437,10 +548,18 @@
437548 struct fib_info **last_resort, int *last_idx,
438549 int dflt)
439550 {
551
+ const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
440552 struct neighbour *n;
441553 int state = NUD_NONE;
442554
443
- n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev);
555
+ if (likely(nhc->nhc_gw_family == AF_INET))
556
+ n = neigh_lookup(&arp_tbl, &nhc->nhc_gw.ipv4, nhc->nhc_dev);
557
+ else if (nhc->nhc_gw_family == AF_INET6)
558
+ n = neigh_lookup(ipv6_stub->nd_tbl, &nhc->nhc_gw.ipv6,
559
+ nhc->nhc_dev);
560
+ else
561
+ n = NULL;
562
+
444563 if (n) {
445564 state = n->nud_state;
446565 neigh_release(n);
....@@ -457,6 +576,77 @@
457576 *last_idx = order;
458577 }
459578 return 1;
579
+}
580
+
581
+int fib_nh_common_init(struct net *net, struct fib_nh_common *nhc,
582
+ struct nlattr *encap, u16 encap_type,
583
+ void *cfg, gfp_t gfp_flags,
584
+ struct netlink_ext_ack *extack)
585
+{
586
+ int err;
587
+
588
+ nhc->nhc_pcpu_rth_output = alloc_percpu_gfp(struct rtable __rcu *,
589
+ gfp_flags);
590
+ if (!nhc->nhc_pcpu_rth_output)
591
+ return -ENOMEM;
592
+
593
+ if (encap) {
594
+ struct lwtunnel_state *lwtstate;
595
+
596
+ if (encap_type == LWTUNNEL_ENCAP_NONE) {
597
+ NL_SET_ERR_MSG(extack, "LWT encap type not specified");
598
+ err = -EINVAL;
599
+ goto lwt_failure;
600
+ }
601
+ err = lwtunnel_build_state(net, encap_type, encap,
602
+ nhc->nhc_family, cfg, &lwtstate,
603
+ extack);
604
+ if (err)
605
+ goto lwt_failure;
606
+
607
+ nhc->nhc_lwtstate = lwtstate_get(lwtstate);
608
+ }
609
+
610
+ return 0;
611
+
612
+lwt_failure:
613
+ rt_fibinfo_free_cpus(nhc->nhc_pcpu_rth_output);
614
+ nhc->nhc_pcpu_rth_output = NULL;
615
+ return err;
616
+}
617
+EXPORT_SYMBOL_GPL(fib_nh_common_init);
618
+
619
+int fib_nh_init(struct net *net, struct fib_nh *nh,
620
+ struct fib_config *cfg, int nh_weight,
621
+ struct netlink_ext_ack *extack)
622
+{
623
+ int err;
624
+
625
+ nh->fib_nh_family = AF_INET;
626
+
627
+ err = fib_nh_common_init(net, &nh->nh_common, cfg->fc_encap,
628
+ cfg->fc_encap_type, cfg, GFP_KERNEL, extack);
629
+ if (err)
630
+ return err;
631
+
632
+ nh->fib_nh_oif = cfg->fc_oif;
633
+ nh->fib_nh_gw_family = cfg->fc_gw_family;
634
+ if (cfg->fc_gw_family == AF_INET)
635
+ nh->fib_nh_gw4 = cfg->fc_gw4;
636
+ else if (cfg->fc_gw_family == AF_INET6)
637
+ nh->fib_nh_gw6 = cfg->fc_gw6;
638
+
639
+ nh->fib_nh_flags = cfg->fc_flags;
640
+
641
+#ifdef CONFIG_IP_ROUTE_CLASSID
642
+ nh->nh_tclassid = cfg->fc_flow;
643
+ if (nh->nh_tclassid)
644
+ atomic_inc(&net->ipv4.fib_num_tclassid_users);
645
+#endif
646
+#ifdef CONFIG_IP_ROUTE_MULTIPATH
647
+ nh->fib_nh_weight = nh_weight;
648
+#endif
649
+ return 0;
460650 }
461651
462652 #ifdef CONFIG_IP_ROUTE_MULTIPATH
....@@ -481,14 +671,33 @@
481671 return nhs;
482672 }
483673
674
+static int fib_gw_from_attr(__be32 *gw, struct nlattr *nla,
675
+ struct netlink_ext_ack *extack)
676
+{
677
+ if (nla_len(nla) < sizeof(*gw)) {
678
+ NL_SET_ERR_MSG(extack, "Invalid IPv4 address in RTA_GATEWAY");
679
+ return -EINVAL;
680
+ }
681
+
682
+ *gw = nla_get_in_addr(nla);
683
+
684
+ return 0;
685
+}
686
+
687
+/* only called when fib_nh is integrated into fib_info */
484688 static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
485689 int remaining, struct fib_config *cfg,
486690 struct netlink_ext_ack *extack)
487691 {
692
+ struct net *net = fi->fib_net;
693
+ struct fib_config fib_cfg;
694
+ struct fib_nh *nh;
488695 int ret;
489696
490697 change_nexthops(fi) {
491698 int attrlen;
699
+
700
+ memset(&fib_cfg, 0, sizeof(fib_cfg));
492701
493702 if (!rtnh_ok(rtnh, remaining)) {
494703 NL_SET_ERR_MSG(extack,
....@@ -502,112 +711,145 @@
502711 return -EINVAL;
503712 }
504713
505
- nexthop_nh->nh_flags =
506
- (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
507
- nexthop_nh->nh_oif = rtnh->rtnh_ifindex;
508
- nexthop_nh->nh_weight = rtnh->rtnh_hops + 1;
714
+ fib_cfg.fc_flags = (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags;
715
+ fib_cfg.fc_oif = rtnh->rtnh_ifindex;
509716
510717 attrlen = rtnh_attrlen(rtnh);
511718 if (attrlen > 0) {
512
- struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
719
+ struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
513720
514721 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
515
- nexthop_nh->nh_gw = nla ? nla_get_in_addr(nla) : 0;
516
-#ifdef CONFIG_IP_ROUTE_CLASSID
517
- nla = nla_find(attrs, attrlen, RTA_FLOW);
518
- nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
519
- if (nexthop_nh->nh_tclassid)
520
- fi->fib_net->ipv4.fib_num_tclassid_users++;
521
-#endif
522
- nla = nla_find(attrs, attrlen, RTA_ENCAP);
722
+ nlav = nla_find(attrs, attrlen, RTA_VIA);
723
+ if (nla && nlav) {
724
+ NL_SET_ERR_MSG(extack,
725
+ "Nexthop configuration can not contain both GATEWAY and VIA");
726
+ return -EINVAL;
727
+ }
523728 if (nla) {
524
- struct lwtunnel_state *lwtstate;
525
- struct nlattr *nla_entype;
526
-
527
- nla_entype = nla_find(attrs, attrlen,
528
- RTA_ENCAP_TYPE);
529
- if (!nla_entype) {
530
- NL_SET_BAD_ATTR(extack, nla);
531
- NL_SET_ERR_MSG(extack,
532
- "Encap type is missing");
533
- goto err_inval;
534
- }
535
-
536
- ret = lwtunnel_build_state(nla_get_u16(
537
- nla_entype),
538
- nla, AF_INET, cfg,
539
- &lwtstate, extack);
729
+ ret = fib_gw_from_attr(&fib_cfg.fc_gw4, nla,
730
+ extack);
540731 if (ret)
541732 goto errout;
542
- nexthop_nh->nh_lwtstate =
543
- lwtstate_get(lwtstate);
733
+
734
+ if (fib_cfg.fc_gw4)
735
+ fib_cfg.fc_gw_family = AF_INET;
736
+ } else if (nlav) {
737
+ ret = fib_gw_from_via(&fib_cfg, nlav, extack);
738
+ if (ret)
739
+ goto errout;
544740 }
741
+
742
+ nla = nla_find(attrs, attrlen, RTA_FLOW);
743
+ if (nla) {
744
+ if (nla_len(nla) < sizeof(u32)) {
745
+ NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
746
+ return -EINVAL;
747
+ }
748
+ fib_cfg.fc_flow = nla_get_u32(nla);
749
+ }
750
+
751
+ fib_cfg.fc_encap = nla_find(attrs, attrlen, RTA_ENCAP);
752
+ /* RTA_ENCAP_TYPE length checked in
753
+ * lwtunnel_valid_encap_type_attr
754
+ */
755
+ nla = nla_find(attrs, attrlen, RTA_ENCAP_TYPE);
756
+ if (nla)
757
+ fib_cfg.fc_encap_type = nla_get_u16(nla);
545758 }
759
+
760
+ ret = fib_nh_init(net, nexthop_nh, &fib_cfg,
761
+ rtnh->rtnh_hops + 1, extack);
762
+ if (ret)
763
+ goto errout;
546764
547765 rtnh = rtnh_next(rtnh, &remaining);
548766 } endfor_nexthops(fi);
549767
550
- return 0;
551
-
552
-err_inval:
553768 ret = -EINVAL;
554
-
769
+ nh = fib_info_nh(fi, 0);
770
+ if (cfg->fc_oif && nh->fib_nh_oif != cfg->fc_oif) {
771
+ NL_SET_ERR_MSG(extack,
772
+ "Nexthop device index does not match RTA_OIF");
773
+ goto errout;
774
+ }
775
+ if (cfg->fc_gw_family) {
776
+ if (cfg->fc_gw_family != nh->fib_nh_gw_family ||
777
+ (cfg->fc_gw_family == AF_INET &&
778
+ nh->fib_nh_gw4 != cfg->fc_gw4) ||
779
+ (cfg->fc_gw_family == AF_INET6 &&
780
+ ipv6_addr_cmp(&nh->fib_nh_gw6, &cfg->fc_gw6))) {
781
+ NL_SET_ERR_MSG(extack,
782
+ "Nexthop gateway does not match RTA_GATEWAY or RTA_VIA");
783
+ goto errout;
784
+ }
785
+ }
786
+#ifdef CONFIG_IP_ROUTE_CLASSID
787
+ if (cfg->fc_flow && nh->nh_tclassid != cfg->fc_flow) {
788
+ NL_SET_ERR_MSG(extack,
789
+ "Nexthop class id does not match RTA_FLOW");
790
+ goto errout;
791
+ }
792
+#endif
793
+ ret = 0;
555794 errout:
556795 return ret;
557796 }
558797
798
+/* only called when fib_nh is integrated into fib_info */
559799 static void fib_rebalance(struct fib_info *fi)
560800 {
561801 int total;
562802 int w;
563
- struct in_device *in_dev;
564803
565
- if (fi->fib_nhs < 2)
804
+ if (fib_info_num_path(fi) < 2)
566805 return;
567806
568807 total = 0;
569808 for_nexthops(fi) {
570
- if (nh->nh_flags & RTNH_F_DEAD)
809
+ if (nh->fib_nh_flags & RTNH_F_DEAD)
571810 continue;
572811
573
- in_dev = __in_dev_get_rtnl(nh->nh_dev);
574
-
575
- if (in_dev &&
576
- IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
577
- nh->nh_flags & RTNH_F_LINKDOWN)
812
+ if (ip_ignore_linkdown(nh->fib_nh_dev) &&
813
+ nh->fib_nh_flags & RTNH_F_LINKDOWN)
578814 continue;
579815
580
- total += nh->nh_weight;
816
+ total += nh->fib_nh_weight;
581817 } endfor_nexthops(fi);
582818
583819 w = 0;
584820 change_nexthops(fi) {
585821 int upper_bound;
586822
587
- in_dev = __in_dev_get_rtnl(nexthop_nh->nh_dev);
588
-
589
- if (nexthop_nh->nh_flags & RTNH_F_DEAD) {
823
+ if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD) {
590824 upper_bound = -1;
591
- } else if (in_dev &&
592
- IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
593
- nexthop_nh->nh_flags & RTNH_F_LINKDOWN) {
825
+ } else if (ip_ignore_linkdown(nexthop_nh->fib_nh_dev) &&
826
+ nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN) {
594827 upper_bound = -1;
595828 } else {
596
- w += nexthop_nh->nh_weight;
829
+ w += nexthop_nh->fib_nh_weight;
597830 upper_bound = DIV_ROUND_CLOSEST_ULL((u64)w << 31,
598831 total) - 1;
599832 }
600833
601
- atomic_set(&nexthop_nh->nh_upper_bound, upper_bound);
834
+ atomic_set(&nexthop_nh->fib_nh_upper_bound, upper_bound);
602835 } endfor_nexthops(fi);
603836 }
604837 #else /* CONFIG_IP_ROUTE_MULTIPATH */
838
+
839
+static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
840
+ int remaining, struct fib_config *cfg,
841
+ struct netlink_ext_ack *extack)
842
+{
843
+ NL_SET_ERR_MSG(extack, "Multipath support not enabled in kernel");
844
+
845
+ return -EINVAL;
846
+}
605847
606848 #define fib_rebalance(fi) do { } while (0)
607849
608850 #endif /* CONFIG_IP_ROUTE_MULTIPATH */
609851
610
-static int fib_encap_match(u16 encap_type,
852
+static int fib_encap_match(struct net *net, u16 encap_type,
611853 struct nlattr *encap,
612854 const struct fib_nh *nh,
613855 const struct fib_config *cfg,
....@@ -619,17 +861,17 @@
619861 if (encap_type == LWTUNNEL_ENCAP_NONE)
620862 return 0;
621863
622
- ret = lwtunnel_build_state(encap_type, encap, AF_INET,
864
+ ret = lwtunnel_build_state(net, encap_type, encap, AF_INET,
623865 cfg, &lwtstate, extack);
624866 if (!ret) {
625
- result = lwtunnel_cmp_encap(lwtstate, nh->nh_lwtstate);
867
+ result = lwtunnel_cmp_encap(lwtstate, nh->fib_nh_lws);
626868 lwtstate_free(lwtstate);
627869 }
628870
629871 return result;
630872 }
631873
632
-int fib_nh_match(struct fib_config *cfg, struct fib_info *fi,
874
+int fib_nh_match(struct net *net, struct fib_config *cfg, struct fib_info *fi,
633875 struct netlink_ext_ack *extack)
634876 {
635877 #ifdef CONFIG_IP_ROUTE_MULTIPATH
....@@ -640,21 +882,46 @@
640882 if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority)
641883 return 1;
642884
643
- if (cfg->fc_oif || cfg->fc_gw) {
885
+ if (cfg->fc_nh_id) {
886
+ if (fi->nh && cfg->fc_nh_id == fi->nh->id)
887
+ return 0;
888
+ return 1;
889
+ }
890
+
891
+ if (fi->nh) {
892
+ if (cfg->fc_oif || cfg->fc_gw_family || cfg->fc_mp)
893
+ return 1;
894
+ return 0;
895
+ }
896
+
897
+ if (cfg->fc_oif || cfg->fc_gw_family) {
898
+ struct fib_nh *nh;
899
+
900
+ nh = fib_info_nh(fi, 0);
644901 if (cfg->fc_encap) {
645
- if (fib_encap_match(cfg->fc_encap_type, cfg->fc_encap,
646
- fi->fib_nh, cfg, extack))
902
+ if (fib_encap_match(net, cfg->fc_encap_type,
903
+ cfg->fc_encap, nh, cfg, extack))
647904 return 1;
648905 }
649906 #ifdef CONFIG_IP_ROUTE_CLASSID
650907 if (cfg->fc_flow &&
651
- cfg->fc_flow != fi->fib_nh->nh_tclassid)
908
+ cfg->fc_flow != nh->nh_tclassid)
652909 return 1;
653910 #endif
654
- if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
655
- (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
656
- return 0;
657
- return 1;
911
+ if ((cfg->fc_oif && cfg->fc_oif != nh->fib_nh_oif) ||
912
+ (cfg->fc_gw_family &&
913
+ cfg->fc_gw_family != nh->fib_nh_gw_family))
914
+ return 1;
915
+
916
+ if (cfg->fc_gw_family == AF_INET &&
917
+ cfg->fc_gw4 != nh->fib_nh_gw4)
918
+ return 1;
919
+
920
+ if (cfg->fc_gw_family == AF_INET6 &&
921
+ ipv6_addr_cmp(&cfg->fc_gw6, &nh->fib_nh_gw6))
922
+ return 1;
923
+
924
+ return 0;
658925 }
659926
660927 #ifdef CONFIG_IP_ROUTE_MULTIPATH
....@@ -670,20 +937,64 @@
670937 if (!rtnh_ok(rtnh, remaining))
671938 return -EINVAL;
672939
673
- if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif)
940
+ if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->fib_nh_oif)
674941 return 1;
675942
676943 attrlen = rtnh_attrlen(rtnh);
677944 if (attrlen > 0) {
678
- struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
945
+ struct nlattr *nla, *nlav, *attrs = rtnh_attrs(rtnh);
946
+ int err;
679947
680948 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
681
- if (nla && nla_get_in_addr(nla) != nh->nh_gw)
682
- return 1;
949
+ nlav = nla_find(attrs, attrlen, RTA_VIA);
950
+ if (nla && nlav) {
951
+ NL_SET_ERR_MSG(extack,
952
+ "Nexthop configuration can not contain both GATEWAY and VIA");
953
+ return -EINVAL;
954
+ }
955
+
956
+ if (nla) {
957
+ __be32 gw;
958
+
959
+ err = fib_gw_from_attr(&gw, nla, extack);
960
+ if (err)
961
+ return err;
962
+
963
+ if (nh->fib_nh_gw_family != AF_INET ||
964
+ gw != nh->fib_nh_gw4)
965
+ return 1;
966
+ } else if (nlav) {
967
+ struct fib_config cfg2;
968
+
969
+ err = fib_gw_from_via(&cfg2, nlav, extack);
970
+ if (err)
971
+ return err;
972
+
973
+ switch (nh->fib_nh_gw_family) {
974
+ case AF_INET:
975
+ if (cfg2.fc_gw_family != AF_INET ||
976
+ cfg2.fc_gw4 != nh->fib_nh_gw4)
977
+ return 1;
978
+ break;
979
+ case AF_INET6:
980
+ if (cfg2.fc_gw_family != AF_INET6 ||
981
+ ipv6_addr_cmp(&cfg2.fc_gw6,
982
+ &nh->fib_nh_gw6))
983
+ return 1;
984
+ break;
985
+ }
986
+ }
987
+
683988 #ifdef CONFIG_IP_ROUTE_CLASSID
684989 nla = nla_find(attrs, attrlen, RTA_FLOW);
685
- if (nla && nla_get_u32(nla) != nh->nh_tclassid)
686
- return 1;
990
+ if (nla) {
991
+ if (nla_len(nla) < sizeof(u32)) {
992
+ NL_SET_ERR_MSG(extack, "Invalid RTA_FLOW");
993
+ return -EINVAL;
994
+ }
995
+ if (nla_get_u32(nla) != nh->nh_tclassid)
996
+ return 1;
997
+ }
687998 #endif
688999 }
6891000
....@@ -733,6 +1044,30 @@
7331044 return true;
7341045 }
7351046
1047
+static int fib_check_nh_v6_gw(struct net *net, struct fib_nh *nh,
1048
+ u32 table, struct netlink_ext_ack *extack)
1049
+{
1050
+ struct fib6_config cfg = {
1051
+ .fc_table = table,
1052
+ .fc_flags = nh->fib_nh_flags | RTF_GATEWAY,
1053
+ .fc_ifindex = nh->fib_nh_oif,
1054
+ .fc_gateway = nh->fib_nh_gw6,
1055
+ };
1056
+ struct fib6_nh fib6_nh = {};
1057
+ int err;
1058
+
1059
+ err = ipv6_stub->fib6_nh_init(net, &fib6_nh, &cfg, GFP_KERNEL, extack);
1060
+ if (!err) {
1061
+ nh->fib_nh_dev = fib6_nh.fib_nh_dev;
1062
+ dev_hold(nh->fib_nh_dev);
1063
+ nh->fib_nh_oif = nh->fib_nh_dev->ifindex;
1064
+ nh->fib_nh_scope = RT_SCOPE_LINK;
1065
+
1066
+ ipv6_stub->fib6_nh_release(&fib6_nh);
1067
+ }
1068
+
1069
+ return err;
1070
+}
7361071
7371072 /*
7381073 * Picture
....@@ -777,128 +1112,147 @@
7771112 * |
7781113 * |-> {local prefix} (terminal node)
7791114 */
780
-static int fib_check_nh(struct fib_config *cfg, struct fib_nh *nh,
781
- struct netlink_ext_ack *extack)
1115
+static int fib_check_nh_v4_gw(struct net *net, struct fib_nh *nh, u32 table,
1116
+ u8 scope, struct netlink_ext_ack *extack)
7821117 {
783
- int err = 0;
784
- struct net *net;
7851118 struct net_device *dev;
1119
+ struct fib_result res;
1120
+ int err = 0;
7861121
787
- net = cfg->fc_nlinfo.nl_net;
788
- if (nh->nh_gw) {
789
- struct fib_result res;
1122
+ if (nh->fib_nh_flags & RTNH_F_ONLINK) {
1123
+ unsigned int addr_type;
7901124
791
- if (nh->nh_flags & RTNH_F_ONLINK) {
792
- unsigned int addr_type;
793
-
794
- if (cfg->fc_scope >= RT_SCOPE_LINK) {
795
- NL_SET_ERR_MSG(extack,
796
- "Nexthop has invalid scope");
797
- return -EINVAL;
798
- }
799
- dev = __dev_get_by_index(net, nh->nh_oif);
800
- if (!dev)
801
- return -ENODEV;
802
- if (!(dev->flags & IFF_UP)) {
803
- NL_SET_ERR_MSG(extack,
804
- "Nexthop device is not up");
805
- return -ENETDOWN;
806
- }
807
- addr_type = inet_addr_type_dev_table(net, dev, nh->nh_gw);
808
- if (addr_type != RTN_UNICAST) {
809
- NL_SET_ERR_MSG(extack,
810
- "Nexthop has invalid gateway");
811
- return -EINVAL;
812
- }
813
- if (!netif_carrier_ok(dev))
814
- nh->nh_flags |= RTNH_F_LINKDOWN;
815
- nh->nh_dev = dev;
816
- dev_hold(dev);
817
- nh->nh_scope = RT_SCOPE_LINK;
818
- return 0;
1125
+ if (scope >= RT_SCOPE_LINK) {
1126
+ NL_SET_ERR_MSG(extack, "Nexthop has invalid scope");
1127
+ return -EINVAL;
8191128 }
820
- rcu_read_lock();
821
- {
822
- struct fib_table *tbl = NULL;
823
- struct flowi4 fl4 = {
824
- .daddr = nh->nh_gw,
825
- .flowi4_scope = cfg->fc_scope + 1,
826
- .flowi4_oif = nh->nh_oif,
827
- .flowi4_iif = LOOPBACK_IFINDEX,
828
- };
829
-
830
- /* It is not necessary, but requires a bit of thinking */
831
- if (fl4.flowi4_scope < RT_SCOPE_LINK)
832
- fl4.flowi4_scope = RT_SCOPE_LINK;
833
-
834
- if (cfg->fc_table && cfg->fc_table != RT_TABLE_MAIN)
835
- tbl = fib_get_table(net, cfg->fc_table);
836
-
837
- if (tbl)
838
- err = fib_table_lookup(tbl, &fl4, &res,
839
- FIB_LOOKUP_IGNORE_LINKSTATE |
840
- FIB_LOOKUP_NOREF);
841
-
842
- /* on error or if no table given do full lookup. This
843
- * is needed for example when nexthops are in the local
844
- * table rather than the given table
845
- */
846
- if (!tbl || err) {
847
- err = fib_lookup(net, &fl4, &res,
848
- FIB_LOOKUP_IGNORE_LINKSTATE);
849
- }
850
-
851
- if (err) {
852
- NL_SET_ERR_MSG(extack,
853
- "Nexthop has invalid gateway");
854
- rcu_read_unlock();
855
- return err;
856
- }
1129
+ dev = __dev_get_by_index(net, nh->fib_nh_oif);
1130
+ if (!dev) {
1131
+ NL_SET_ERR_MSG(extack, "Nexthop device required for onlink");
1132
+ return -ENODEV;
8571133 }
858
- err = -EINVAL;
859
- if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
1134
+ if (!(dev->flags & IFF_UP)) {
1135
+ NL_SET_ERR_MSG(extack, "Nexthop device is not up");
1136
+ return -ENETDOWN;
1137
+ }
1138
+ addr_type = inet_addr_type_dev_table(net, dev, nh->fib_nh_gw4);
1139
+ if (addr_type != RTN_UNICAST) {
1140
+ NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1141
+ return -EINVAL;
1142
+ }
1143
+ if (!netif_carrier_ok(dev))
1144
+ nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1145
+ nh->fib_nh_dev = dev;
1146
+ dev_hold(dev);
1147
+ nh->fib_nh_scope = RT_SCOPE_LINK;
1148
+ return 0;
1149
+ }
1150
+ rcu_read_lock();
1151
+ {
1152
+ struct fib_table *tbl = NULL;
1153
+ struct flowi4 fl4 = {
1154
+ .daddr = nh->fib_nh_gw4,
1155
+ .flowi4_scope = scope + 1,
1156
+ .flowi4_oif = nh->fib_nh_oif,
1157
+ .flowi4_iif = LOOPBACK_IFINDEX,
1158
+ };
1159
+
1160
+ /* It is not necessary, but requires a bit of thinking */
1161
+ if (fl4.flowi4_scope < RT_SCOPE_LINK)
1162
+ fl4.flowi4_scope = RT_SCOPE_LINK;
1163
+
1164
+ if (table && table != RT_TABLE_MAIN)
1165
+ tbl = fib_get_table(net, table);
1166
+
1167
+ if (tbl)
1168
+ err = fib_table_lookup(tbl, &fl4, &res,
1169
+ FIB_LOOKUP_IGNORE_LINKSTATE |
1170
+ FIB_LOOKUP_NOREF);
1171
+
1172
+ /* on error or if no table given do full lookup. This
1173
+ * is needed for example when nexthops are in the local
1174
+ * table rather than the given table
1175
+ */
1176
+ if (!tbl || err) {
1177
+ err = fib_lookup(net, &fl4, &res,
1178
+ FIB_LOOKUP_IGNORE_LINKSTATE);
1179
+ }
1180
+
1181
+ if (err) {
8601182 NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
8611183 goto out;
8621184 }
863
- nh->nh_scope = res.scope;
864
- nh->nh_oif = FIB_RES_OIF(res);
865
- nh->nh_dev = dev = FIB_RES_DEV(res);
866
- if (!dev) {
867
- NL_SET_ERR_MSG(extack,
868
- "No egress device for nexthop gateway");
869
- goto out;
870
- }
871
- dev_hold(dev);
872
- if (!netif_carrier_ok(dev))
873
- nh->nh_flags |= RTNH_F_LINKDOWN;
874
- err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
875
- } else {
876
- struct in_device *in_dev;
877
-
878
- if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
879
- NL_SET_ERR_MSG(extack,
880
- "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
881
- return -EINVAL;
882
- }
883
- rcu_read_lock();
884
- err = -ENODEV;
885
- in_dev = inetdev_by_index(net, nh->nh_oif);
886
- if (!in_dev)
887
- goto out;
888
- err = -ENETDOWN;
889
- if (!(in_dev->dev->flags & IFF_UP)) {
890
- NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
891
- goto out;
892
- }
893
- nh->nh_dev = in_dev->dev;
894
- dev_hold(nh->nh_dev);
895
- nh->nh_scope = RT_SCOPE_HOST;
896
- if (!netif_carrier_ok(nh->nh_dev))
897
- nh->nh_flags |= RTNH_F_LINKDOWN;
898
- err = 0;
8991185 }
1186
+
1187
+ err = -EINVAL;
1188
+ if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) {
1189
+ NL_SET_ERR_MSG(extack, "Nexthop has invalid gateway");
1190
+ goto out;
1191
+ }
1192
+ nh->fib_nh_scope = res.scope;
1193
+ nh->fib_nh_oif = FIB_RES_OIF(res);
1194
+ nh->fib_nh_dev = dev = FIB_RES_DEV(res);
1195
+ if (!dev) {
1196
+ NL_SET_ERR_MSG(extack,
1197
+ "No egress device for nexthop gateway");
1198
+ goto out;
1199
+ }
1200
+ dev_hold(dev);
1201
+ if (!netif_carrier_ok(dev))
1202
+ nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1203
+ err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN;
9001204 out:
9011205 rcu_read_unlock();
1206
+ return err;
1207
+}
1208
+
1209
+static int fib_check_nh_nongw(struct net *net, struct fib_nh *nh,
1210
+ struct netlink_ext_ack *extack)
1211
+{
1212
+ struct in_device *in_dev;
1213
+ int err;
1214
+
1215
+ if (nh->fib_nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) {
1216
+ NL_SET_ERR_MSG(extack,
1217
+ "Invalid flags for nexthop - PERVASIVE and ONLINK can not be set");
1218
+ return -EINVAL;
1219
+ }
1220
+
1221
+ rcu_read_lock();
1222
+
1223
+ err = -ENODEV;
1224
+ in_dev = inetdev_by_index(net, nh->fib_nh_oif);
1225
+ if (!in_dev)
1226
+ goto out;
1227
+ err = -ENETDOWN;
1228
+ if (!(in_dev->dev->flags & IFF_UP)) {
1229
+ NL_SET_ERR_MSG(extack, "Device for nexthop is not up");
1230
+ goto out;
1231
+ }
1232
+
1233
+ nh->fib_nh_dev = in_dev->dev;
1234
+ dev_hold(nh->fib_nh_dev);
1235
+ nh->fib_nh_scope = RT_SCOPE_LINK;
1236
+ if (!netif_carrier_ok(nh->fib_nh_dev))
1237
+ nh->fib_nh_flags |= RTNH_F_LINKDOWN;
1238
+ err = 0;
1239
+out:
1240
+ rcu_read_unlock();
1241
+ return err;
1242
+}
1243
+
1244
+int fib_check_nh(struct net *net, struct fib_nh *nh, u32 table, u8 scope,
1245
+ struct netlink_ext_ack *extack)
1246
+{
1247
+ int err;
1248
+
1249
+ if (nh->fib_nh_gw_family == AF_INET)
1250
+ err = fib_check_nh_v4_gw(net, nh, table, scope, extack);
1251
+ else if (nh->fib_nh_gw_family == AF_INET6)
1252
+ err = fib_check_nh_v6_gw(net, nh, table, extack);
1253
+ else
1254
+ err = fib_check_nh_nongw(net, nh, extack);
1255
+
9021256 return err;
9031257 }
9041258
....@@ -984,14 +1338,37 @@
9841338 fib_info_hash_free(old_laddrhash, bytes);
9851339 }
9861340
987
-__be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
1341
+__be32 fib_info_update_nhc_saddr(struct net *net, struct fib_nh_common *nhc,
1342
+ unsigned char scope)
9881343 {
989
- nh->nh_saddr = inet_select_addr(nh->nh_dev,
990
- nh->nh_gw,
991
- nh->nh_parent->fib_scope);
1344
+ struct fib_nh *nh;
1345
+
1346
+ if (nhc->nhc_family != AF_INET)
1347
+ return inet_select_addr(nhc->nhc_dev, 0, scope);
1348
+
1349
+ nh = container_of(nhc, struct fib_nh, nh_common);
1350
+ nh->nh_saddr = inet_select_addr(nh->fib_nh_dev, nh->fib_nh_gw4, scope);
9921351 nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid);
9931352
9941353 return nh->nh_saddr;
1354
+}
1355
+
1356
+__be32 fib_result_prefsrc(struct net *net, struct fib_result *res)
1357
+{
1358
+ struct fib_nh_common *nhc = res->nhc;
1359
+
1360
+ if (res->fi->fib_prefsrc)
1361
+ return res->fi->fib_prefsrc;
1362
+
1363
+ if (nhc->nhc_family == AF_INET) {
1364
+ struct fib_nh *nh;
1365
+
1366
+ nh = container_of(nhc, struct fib_nh, nh_common);
1367
+ if (nh->nh_saddr_genid == atomic_read(&net->ipv4.dev_addr_genid))
1368
+ return nh->nh_saddr;
1369
+ }
1370
+
1371
+ return fib_info_update_nhc_saddr(net, nhc, res->fi->fib_scope);
9951372 }
9961373
9971374 static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
....@@ -1018,18 +1395,12 @@
10181395 return true;
10191396 }
10201397
1021
-static int
1022
-fib_convert_metrics(struct fib_info *fi, const struct fib_config *cfg)
1023
-{
1024
- return ip_metrics_convert(fi->fib_net, cfg->fc_mx, cfg->fc_mx_len,
1025
- fi->fib_metrics->metrics);
1026
-}
1027
-
10281398 struct fib_info *fib_create_info(struct fib_config *cfg,
10291399 struct netlink_ext_ack *extack)
10301400 {
10311401 int err;
10321402 struct fib_info *fi = NULL;
1403
+ struct nexthop *nh = NULL;
10331404 struct fib_info *ofi;
10341405 int nhs = 1;
10351406 struct net *net = cfg->fc_nlinfo.nl_net;
....@@ -1049,6 +1420,23 @@
10491420 goto err_inval;
10501421 }
10511422
1423
+ if (cfg->fc_nh_id) {
1424
+ if (!cfg->fc_mx) {
1425
+ fi = fib_find_info_nh(net, cfg);
1426
+ if (fi) {
1427
+ fi->fib_treeref++;
1428
+ return fi;
1429
+ }
1430
+ }
1431
+
1432
+ nh = nexthop_find_by_id(net, cfg->fc_nh_id);
1433
+ if (!nh) {
1434
+ NL_SET_ERR_MSG(extack, "Nexthop id does not exist");
1435
+ goto err_inval;
1436
+ }
1437
+ nhs = 0;
1438
+ }
1439
+
10521440 #ifdef CONFIG_IP_ROUTE_MULTIPATH
10531441 if (cfg->fc_mp) {
10541442 nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len, extack);
....@@ -1058,7 +1446,9 @@
10581446 #endif
10591447
10601448 err = -ENOBUFS;
1061
- if (fib_info_cnt >= fib_info_hash_size) {
1449
+
1450
+ /* Paired with WRITE_ONCE() in fib_release_info() */
1451
+ if (READ_ONCE(fib_info_cnt) >= fib_info_hash_size) {
10621452 unsigned int new_size = fib_info_hash_size << 1;
10631453 struct hlist_head *new_info_hash;
10641454 struct hlist_head *new_laddrhash;
....@@ -1079,20 +1469,17 @@
10791469 goto failure;
10801470 }
10811471
1082
- fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
1472
+ fi = kzalloc(struct_size(fi, fib_nh, nhs), GFP_KERNEL);
10831473 if (!fi)
10841474 goto failure;
1085
- if (cfg->fc_mx) {
1086
- fi->fib_metrics = kzalloc(sizeof(*fi->fib_metrics), GFP_KERNEL);
1087
- if (unlikely(!fi->fib_metrics)) {
1088
- kfree(fi);
1089
- return ERR_PTR(err);
1090
- }
1091
- refcount_set(&fi->fib_metrics->refcnt, 1);
1092
- } else {
1093
- fi->fib_metrics = (struct dst_metrics *)&dst_default_metrics;
1475
+ fi->fib_metrics = ip_fib_metrics_init(fi->fib_net, cfg->fc_mx,
1476
+ cfg->fc_mx_len, extack);
1477
+ if (IS_ERR(fi->fib_metrics)) {
1478
+ err = PTR_ERR(fi->fib_metrics);
1479
+ kfree(fi);
1480
+ return ERR_PTR(err);
10941481 }
1095
- fib_info_cnt++;
1482
+
10961483 fi->fib_net = net;
10971484 fi->fib_protocol = cfg->fc_protocol;
10981485 fi->fib_scope = cfg->fc_scope;
....@@ -1103,78 +1490,31 @@
11031490 fi->fib_tb_id = cfg->fc_table;
11041491
11051492 fi->fib_nhs = nhs;
1106
- change_nexthops(fi) {
1107
- nexthop_nh->nh_parent = fi;
1108
- nexthop_nh->nh_pcpu_rth_output = alloc_percpu(struct rtable __rcu *);
1109
- if (!nexthop_nh->nh_pcpu_rth_output)
1110
- goto failure;
1111
- } endfor_nexthops(fi)
1112
-
1113
- err = fib_convert_metrics(fi, cfg);
1114
- if (err)
1115
- goto failure;
1116
-
1117
- if (cfg->fc_mp) {
1118
-#ifdef CONFIG_IP_ROUTE_MULTIPATH
1119
- err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg, extack);
1120
- if (err != 0)
1121
- goto failure;
1122
- if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif) {
1123
- NL_SET_ERR_MSG(extack,
1124
- "Nexthop device index does not match RTA_OIF");
1125
- goto err_inval;
1493
+ if (nh) {
1494
+ if (!nexthop_get(nh)) {
1495
+ NL_SET_ERR_MSG(extack, "Nexthop has been deleted");
1496
+ err = -EINVAL;
1497
+ } else {
1498
+ err = 0;
1499
+ fi->nh = nh;
11261500 }
1127
- if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw) {
1128
- NL_SET_ERR_MSG(extack,
1129
- "Nexthop gateway does not match RTA_GATEWAY");
1130
- goto err_inval;
1131
- }
1132
-#ifdef CONFIG_IP_ROUTE_CLASSID
1133
- if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) {
1134
- NL_SET_ERR_MSG(extack,
1135
- "Nexthop class id does not match RTA_FLOW");
1136
- goto err_inval;
1137
- }
1138
-#endif
1139
-#else
1140
- NL_SET_ERR_MSG(extack,
1141
- "Multipath support not enabled in kernel");
1142
- goto err_inval;
1143
-#endif
11441501 } else {
1145
- struct fib_nh *nh = fi->fib_nh;
1502
+ change_nexthops(fi) {
1503
+ nexthop_nh->nh_parent = fi;
1504
+ } endfor_nexthops(fi)
11461505
1147
- if (cfg->fc_encap) {
1148
- struct lwtunnel_state *lwtstate;
1149
-
1150
- if (cfg->fc_encap_type == LWTUNNEL_ENCAP_NONE) {
1151
- NL_SET_ERR_MSG(extack,
1152
- "LWT encap type not specified");
1153
- goto err_inval;
1154
- }
1155
- err = lwtunnel_build_state(cfg->fc_encap_type,
1156
- cfg->fc_encap, AF_INET, cfg,
1157
- &lwtstate, extack);
1158
- if (err)
1159
- goto failure;
1160
-
1161
- nh->nh_lwtstate = lwtstate_get(lwtstate);
1162
- }
1163
- nh->nh_oif = cfg->fc_oif;
1164
- nh->nh_gw = cfg->fc_gw;
1165
- nh->nh_flags = cfg->fc_flags;
1166
-#ifdef CONFIG_IP_ROUTE_CLASSID
1167
- nh->nh_tclassid = cfg->fc_flow;
1168
- if (nh->nh_tclassid)
1169
- fi->fib_net->ipv4.fib_num_tclassid_users++;
1170
-#endif
1171
-#ifdef CONFIG_IP_ROUTE_MULTIPATH
1172
- nh->nh_weight = 1;
1173
-#endif
1506
+ if (cfg->fc_mp)
1507
+ err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg,
1508
+ extack);
1509
+ else
1510
+ err = fib_nh_init(net, fi->fib_nh, cfg, 1, extack);
11741511 }
11751512
1513
+ if (err != 0)
1514
+ goto failure;
1515
+
11761516 if (fib_props[cfg->fc_type].error) {
1177
- if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp) {
1517
+ if (cfg->fc_gw_family || cfg->fc_oif || cfg->fc_mp) {
11781518 NL_SET_ERR_MSG(extack,
11791519 "Gateway, device and multipath can not be specified for this route type");
11801520 goto err_inval;
....@@ -1199,7 +1539,11 @@
11991539 goto err_inval;
12001540 }
12011541
1202
- if (cfg->fc_scope == RT_SCOPE_HOST) {
1542
+ if (fi->nh) {
1543
+ err = fib_check_nexthop(fi->nh, cfg->fc_scope, extack);
1544
+ if (err)
1545
+ goto failure;
1546
+ } else if (cfg->fc_scope == RT_SCOPE_HOST) {
12031547 struct fib_nh *nh = fi->fib_nh;
12041548
12051549 /* Local address is added. */
....@@ -1208,24 +1552,26 @@
12081552 "Route with host scope can not have multiple nexthops");
12091553 goto err_inval;
12101554 }
1211
- if (nh->nh_gw) {
1555
+ if (nh->fib_nh_gw_family) {
12121556 NL_SET_ERR_MSG(extack,
12131557 "Route with host scope can not have a gateway");
12141558 goto err_inval;
12151559 }
1216
- nh->nh_scope = RT_SCOPE_NOWHERE;
1217
- nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
1560
+ nh->fib_nh_scope = RT_SCOPE_NOWHERE;
1561
+ nh->fib_nh_dev = dev_get_by_index(net, nh->fib_nh_oif);
12181562 err = -ENODEV;
1219
- if (!nh->nh_dev)
1563
+ if (!nh->fib_nh_dev)
12201564 goto failure;
12211565 } else {
12221566 int linkdown = 0;
12231567
12241568 change_nexthops(fi) {
1225
- err = fib_check_nh(cfg, nexthop_nh, extack);
1569
+ err = fib_check_nh(cfg->fc_nlinfo.nl_net, nexthop_nh,
1570
+ cfg->fc_table, cfg->fc_scope,
1571
+ extack);
12261572 if (err != 0)
12271573 goto failure;
1228
- if (nexthop_nh->nh_flags & RTNH_F_LINKDOWN)
1574
+ if (nexthop_nh->fib_nh_flags & RTNH_F_LINKDOWN)
12291575 linkdown++;
12301576 } endfor_nexthops(fi)
12311577 if (linkdown == fi->fib_nhs)
....@@ -1237,11 +1583,16 @@
12371583 goto err_inval;
12381584 }
12391585
1240
- change_nexthops(fi) {
1241
- fib_info_update_nh_saddr(net, nexthop_nh);
1242
- } endfor_nexthops(fi)
1586
+ if (!fi->nh) {
1587
+ change_nexthops(fi) {
1588
+ fib_info_update_nhc_saddr(net, &nexthop_nh->nh_common,
1589
+ fi->fib_scope);
1590
+ if (nexthop_nh->fib_nh_gw_family == AF_INET6)
1591
+ fi->fib_nh_is_v6 = true;
1592
+ } endfor_nexthops(fi)
12431593
1244
- fib_rebalance(fi);
1594
+ fib_rebalance(fi);
1595
+ }
12451596
12461597 link_it:
12471598 ofi = fib_find_info(fi);
....@@ -1255,6 +1606,7 @@
12551606 fi->fib_treeref++;
12561607 refcount_set(&fi->fib_clntref, 1);
12571608 spin_lock_bh(&fib_info_lock);
1609
+ fib_info_cnt++;
12581610 hlist_add_head(&fi->fib_hash,
12591611 &fib_info_hash[fib_info_hashfn(fi)]);
12601612 if (fi->fib_prefsrc) {
....@@ -1263,16 +1615,18 @@
12631615 head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)];
12641616 hlist_add_head(&fi->fib_lhash, head);
12651617 }
1266
- change_nexthops(fi) {
1267
- struct hlist_head *head;
1268
- unsigned int hash;
1618
+ if (fi->nh) {
1619
+ list_add(&fi->nh_list, &nh->fi_list);
1620
+ } else {
1621
+ change_nexthops(fi) {
1622
+ struct hlist_head *head;
12691623
1270
- if (!nexthop_nh->nh_dev)
1271
- continue;
1272
- hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex);
1273
- head = &fib_info_devhash[hash];
1274
- hlist_add_head(&nexthop_nh->nh_hash, head);
1275
- } endfor_nexthops(fi)
1624
+ if (!nexthop_nh->fib_nh_dev)
1625
+ continue;
1626
+ head = fib_info_devhash_bucket(nexthop_nh->fib_nh_dev);
1627
+ hlist_add_head(&nexthop_nh->nh_hash, head);
1628
+ } endfor_nexthops(fi)
1629
+ }
12761630 spin_unlock_bh(&fib_info_lock);
12771631 return fi;
12781632
....@@ -1288,10 +1642,157 @@
12881642 return ERR_PTR(err);
12891643 }
12901644
1291
-int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
1292
- u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos,
1293
- struct fib_info *fi, unsigned int flags)
1645
+int fib_nexthop_info(struct sk_buff *skb, const struct fib_nh_common *nhc,
1646
+ u8 rt_family, unsigned char *flags, bool skip_oif)
12941647 {
1648
+ if (nhc->nhc_flags & RTNH_F_DEAD)
1649
+ *flags |= RTNH_F_DEAD;
1650
+
1651
+ if (nhc->nhc_flags & RTNH_F_LINKDOWN) {
1652
+ *flags |= RTNH_F_LINKDOWN;
1653
+
1654
+ rcu_read_lock();
1655
+ switch (nhc->nhc_family) {
1656
+ case AF_INET:
1657
+ if (ip_ignore_linkdown(nhc->nhc_dev))
1658
+ *flags |= RTNH_F_DEAD;
1659
+ break;
1660
+ case AF_INET6:
1661
+ if (ip6_ignore_linkdown(nhc->nhc_dev))
1662
+ *flags |= RTNH_F_DEAD;
1663
+ break;
1664
+ }
1665
+ rcu_read_unlock();
1666
+ }
1667
+
1668
+ switch (nhc->nhc_gw_family) {
1669
+ case AF_INET:
1670
+ if (nla_put_in_addr(skb, RTA_GATEWAY, nhc->nhc_gw.ipv4))
1671
+ goto nla_put_failure;
1672
+ break;
1673
+ case AF_INET6:
1674
+ /* if gateway family does not match nexthop family
1675
+ * gateway is encoded as RTA_VIA
1676
+ */
1677
+ if (rt_family != nhc->nhc_gw_family) {
1678
+ int alen = sizeof(struct in6_addr);
1679
+ struct nlattr *nla;
1680
+ struct rtvia *via;
1681
+
1682
+ nla = nla_reserve(skb, RTA_VIA, alen + 2);
1683
+ if (!nla)
1684
+ goto nla_put_failure;
1685
+
1686
+ via = nla_data(nla);
1687
+ via->rtvia_family = AF_INET6;
1688
+ memcpy(via->rtvia_addr, &nhc->nhc_gw.ipv6, alen);
1689
+ } else if (nla_put_in6_addr(skb, RTA_GATEWAY,
1690
+ &nhc->nhc_gw.ipv6) < 0) {
1691
+ goto nla_put_failure;
1692
+ }
1693
+ break;
1694
+ }
1695
+
1696
+ *flags |= (nhc->nhc_flags & RTNH_F_ONLINK);
1697
+ if (nhc->nhc_flags & RTNH_F_OFFLOAD)
1698
+ *flags |= RTNH_F_OFFLOAD;
1699
+
1700
+ if (!skip_oif && nhc->nhc_dev &&
1701
+ nla_put_u32(skb, RTA_OIF, nhc->nhc_dev->ifindex))
1702
+ goto nla_put_failure;
1703
+
1704
+ if (nhc->nhc_lwtstate &&
1705
+ lwtunnel_fill_encap(skb, nhc->nhc_lwtstate,
1706
+ RTA_ENCAP, RTA_ENCAP_TYPE) < 0)
1707
+ goto nla_put_failure;
1708
+
1709
+ return 0;
1710
+
1711
+nla_put_failure:
1712
+ return -EMSGSIZE;
1713
+}
1714
+EXPORT_SYMBOL_GPL(fib_nexthop_info);
1715
+
1716
+#if IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) || IS_ENABLED(CONFIG_IPV6)
1717
+int fib_add_nexthop(struct sk_buff *skb, const struct fib_nh_common *nhc,
1718
+ int nh_weight, u8 rt_family, u32 nh_tclassid)
1719
+{
1720
+ const struct net_device *dev = nhc->nhc_dev;
1721
+ struct rtnexthop *rtnh;
1722
+ unsigned char flags = 0;
1723
+
1724
+ rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1725
+ if (!rtnh)
1726
+ goto nla_put_failure;
1727
+
1728
+ rtnh->rtnh_hops = nh_weight - 1;
1729
+ rtnh->rtnh_ifindex = dev ? dev->ifindex : 0;
1730
+
1731
+ if (fib_nexthop_info(skb, nhc, rt_family, &flags, true) < 0)
1732
+ goto nla_put_failure;
1733
+
1734
+ rtnh->rtnh_flags = flags;
1735
+
1736
+ if (nh_tclassid && nla_put_u32(skb, RTA_FLOW, nh_tclassid))
1737
+ goto nla_put_failure;
1738
+
1739
+ /* length of rtnetlink header + attributes */
1740
+ rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
1741
+
1742
+ return 0;
1743
+
1744
+nla_put_failure:
1745
+ return -EMSGSIZE;
1746
+}
1747
+EXPORT_SYMBOL_GPL(fib_add_nexthop);
1748
+#endif
1749
+
1750
+#ifdef CONFIG_IP_ROUTE_MULTIPATH
1751
+static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1752
+{
1753
+ struct nlattr *mp;
1754
+
1755
+ mp = nla_nest_start_noflag(skb, RTA_MULTIPATH);
1756
+ if (!mp)
1757
+ goto nla_put_failure;
1758
+
1759
+ if (unlikely(fi->nh)) {
1760
+ if (nexthop_mpath_fill_node(skb, fi->nh, AF_INET) < 0)
1761
+ goto nla_put_failure;
1762
+ goto mp_end;
1763
+ }
1764
+
1765
+ for_nexthops(fi) {
1766
+ u32 nh_tclassid = 0;
1767
+#ifdef CONFIG_IP_ROUTE_CLASSID
1768
+ nh_tclassid = nh->nh_tclassid;
1769
+#endif
1770
+ if (fib_add_nexthop(skb, &nh->nh_common, nh->fib_nh_weight,
1771
+ AF_INET, nh_tclassid) < 0)
1772
+ goto nla_put_failure;
1773
+ } endfor_nexthops(fi);
1774
+
1775
+mp_end:
1776
+ nla_nest_end(skb, mp);
1777
+
1778
+ return 0;
1779
+
1780
+nla_put_failure:
1781
+ return -EMSGSIZE;
1782
+}
1783
+#else
1784
+static int fib_add_multipath(struct sk_buff *skb, struct fib_info *fi)
1785
+{
1786
+ return 0;
1787
+}
1788
+#endif
1789
+
1790
+int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
1791
+ struct fib_rt_info *fri, unsigned int flags)
1792
+{
1793
+ unsigned int nhs = fib_info_num_path(fri->fi);
1794
+ struct fib_info *fi = fri->fi;
1795
+ u32 tb_id = fri->tb_id;
12951796 struct nlmsghdr *nlh;
12961797 struct rtmsg *rtm;
12971798
....@@ -1301,22 +1802,22 @@
13011802
13021803 rtm = nlmsg_data(nlh);
13031804 rtm->rtm_family = AF_INET;
1304
- rtm->rtm_dst_len = dst_len;
1805
+ rtm->rtm_dst_len = fri->dst_len;
13051806 rtm->rtm_src_len = 0;
1306
- rtm->rtm_tos = tos;
1807
+ rtm->rtm_tos = fri->tos;
13071808 if (tb_id < 256)
13081809 rtm->rtm_table = tb_id;
13091810 else
13101811 rtm->rtm_table = RT_TABLE_COMPAT;
13111812 if (nla_put_u32(skb, RTA_TABLE, tb_id))
13121813 goto nla_put_failure;
1313
- rtm->rtm_type = type;
1814
+ rtm->rtm_type = fri->type;
13141815 rtm->rtm_flags = fi->fib_flags;
13151816 rtm->rtm_scope = fi->fib_scope;
13161817 rtm->rtm_protocol = fi->fib_protocol;
13171818
13181819 if (rtm->rtm_dst_len &&
1319
- nla_put_in_addr(skb, RTA_DST, dst))
1820
+ nla_put_in_addr(skb, RTA_DST, fri->dst))
13201821 goto nla_put_failure;
13211822 if (fi->fib_priority &&
13221823 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
....@@ -1327,81 +1828,45 @@
13271828 if (fi->fib_prefsrc &&
13281829 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
13291830 goto nla_put_failure;
1330
- if (fi->fib_nhs == 1) {
1331
- if (fi->fib_nh->nh_gw &&
1332
- nla_put_in_addr(skb, RTA_GATEWAY, fi->fib_nh->nh_gw))
1333
- goto nla_put_failure;
1334
- if (fi->fib_nh->nh_oif &&
1335
- nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif))
1336
- goto nla_put_failure;
1337
- if (fi->fib_nh->nh_flags & RTNH_F_LINKDOWN) {
1338
- struct in_device *in_dev;
13391831
1340
- rcu_read_lock();
1341
- in_dev = __in_dev_get_rcu(fi->fib_nh->nh_dev);
1342
- if (in_dev &&
1343
- IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
1344
- rtm->rtm_flags |= RTNH_F_DEAD;
1345
- rcu_read_unlock();
1346
- }
1347
- if (fi->fib_nh->nh_flags & RTNH_F_OFFLOAD)
1348
- rtm->rtm_flags |= RTNH_F_OFFLOAD;
1349
-#ifdef CONFIG_IP_ROUTE_CLASSID
1350
- if (fi->fib_nh[0].nh_tclassid &&
1351
- nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid))
1832
+ if (fi->nh) {
1833
+ if (nla_put_u32(skb, RTA_NH_ID, fi->nh->id))
13521834 goto nla_put_failure;
1353
-#endif
1354
- if (fi->fib_nh->nh_lwtstate &&
1355
- lwtunnel_fill_encap(skb, fi->fib_nh->nh_lwtstate) < 0)
1356
- goto nla_put_failure;
1835
+ if (nexthop_is_blackhole(fi->nh))
1836
+ rtm->rtm_type = RTN_BLACKHOLE;
1837
+ if (!READ_ONCE(fi->fib_net->ipv4.sysctl_nexthop_compat_mode))
1838
+ goto offload;
13571839 }
1358
-#ifdef CONFIG_IP_ROUTE_MULTIPATH
1359
- if (fi->fib_nhs > 1) {
1360
- struct rtnexthop *rtnh;
1361
- struct nlattr *mp;
13621840
1363
- mp = nla_nest_start(skb, RTA_MULTIPATH);
1364
- if (!mp)
1841
+ if (nhs == 1) {
1842
+ const struct fib_nh_common *nhc = fib_info_nhc(fi, 0);
1843
+ unsigned char flags = 0;
1844
+
1845
+ if (fib_nexthop_info(skb, nhc, AF_INET, &flags, false) < 0)
13651846 goto nla_put_failure;
13661847
1367
- for_nexthops(fi) {
1368
- rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1369
- if (!rtnh)
1370
- goto nla_put_failure;
1371
-
1372
- rtnh->rtnh_flags = nh->nh_flags & 0xFF;
1373
- if (nh->nh_flags & RTNH_F_LINKDOWN) {
1374
- struct in_device *in_dev;
1375
-
1376
- rcu_read_lock();
1377
- in_dev = __in_dev_get_rcu(nh->nh_dev);
1378
- if (in_dev &&
1379
- IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev))
1380
- rtnh->rtnh_flags |= RTNH_F_DEAD;
1381
- rcu_read_unlock();
1382
- }
1383
- rtnh->rtnh_hops = nh->nh_weight - 1;
1384
- rtnh->rtnh_ifindex = nh->nh_oif;
1385
-
1386
- if (nh->nh_gw &&
1387
- nla_put_in_addr(skb, RTA_GATEWAY, nh->nh_gw))
1388
- goto nla_put_failure;
1848
+ rtm->rtm_flags = flags;
13891849 #ifdef CONFIG_IP_ROUTE_CLASSID
1850
+ if (nhc->nhc_family == AF_INET) {
1851
+ struct fib_nh *nh;
1852
+
1853
+ nh = container_of(nhc, struct fib_nh, nh_common);
13901854 if (nh->nh_tclassid &&
13911855 nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
13921856 goto nla_put_failure;
1857
+ }
13931858 #endif
1394
- if (nh->nh_lwtstate &&
1395
- lwtunnel_fill_encap(skb, nh->nh_lwtstate) < 0)
1396
- goto nla_put_failure;
1397
-
1398
- /* length of rtnetlink header + attributes */
1399
- rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
1400
- } endfor_nexthops(fi);
1401
-
1402
- nla_nest_end(skb, mp);
1859
+ } else {
1860
+ if (fib_add_multipath(skb, fi) < 0)
1861
+ goto nla_put_failure;
14031862 }
1404
-#endif
1863
+
1864
+offload:
1865
+ if (fri->offload)
1866
+ rtm->rtm_flags |= RTM_F_OFFLOAD;
1867
+ if (fri->trap)
1868
+ rtm->rtm_flags |= RTM_F_TRAP;
1869
+
14051870 nlmsg_end(skb, nlh);
14061871 return 0;
14071872
....@@ -1440,28 +1905,26 @@
14401905 return ret;
14411906 }
14421907
1443
-static int call_fib_nh_notifiers(struct fib_nh *fib_nh,
1908
+static int call_fib_nh_notifiers(struct fib_nh *nh,
14441909 enum fib_event_type event_type)
14451910 {
1446
- struct in_device *in_dev = __in_dev_get_rtnl(fib_nh->nh_dev);
1911
+ bool ignore_link_down = ip_ignore_linkdown(nh->fib_nh_dev);
14471912 struct fib_nh_notifier_info info = {
1448
- .fib_nh = fib_nh,
1913
+ .fib_nh = nh,
14491914 };
14501915
14511916 switch (event_type) {
14521917 case FIB_EVENT_NH_ADD:
1453
- if (fib_nh->nh_flags & RTNH_F_DEAD)
1918
+ if (nh->fib_nh_flags & RTNH_F_DEAD)
14541919 break;
1455
- if (IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
1456
- fib_nh->nh_flags & RTNH_F_LINKDOWN)
1920
+ if (ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN)
14571921 break;
1458
- return call_fib4_notifiers(dev_net(fib_nh->nh_dev), event_type,
1922
+ return call_fib4_notifiers(dev_net(nh->fib_nh_dev), event_type,
14591923 &info.info);
14601924 case FIB_EVENT_NH_DEL:
1461
- if ((in_dev && IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
1462
- fib_nh->nh_flags & RTNH_F_LINKDOWN) ||
1463
- (fib_nh->nh_flags & RTNH_F_DEAD))
1464
- return call_fib4_notifiers(dev_net(fib_nh->nh_dev),
1925
+ if ((ignore_link_down && nh->fib_nh_flags & RTNH_F_LINKDOWN) ||
1926
+ (nh->fib_nh_flags & RTNH_F_DEAD))
1927
+ return call_fib4_notifiers(dev_net(nh->fib_nh_dev),
14651928 event_type, &info.info);
14661929 default:
14671930 break;
....@@ -1480,12 +1943,12 @@
14801943 * - if the new MTU is greater than the PMTU, don't make any change
14811944 * - otherwise, unlock and set PMTU
14821945 */
1483
-static void nh_update_mtu(struct fib_nh *nh, u32 new, u32 orig)
1946
+void fib_nhc_update_mtu(struct fib_nh_common *nhc, u32 new, u32 orig)
14841947 {
14851948 struct fnhe_hash_bucket *bucket;
14861949 int i;
14871950
1488
- bucket = rcu_dereference_protected(nh->nh_exceptions, 1);
1951
+ bucket = rcu_dereference_protected(nhc->nhc_exceptions, 1);
14891952 if (!bucket)
14901953 return;
14911954
....@@ -1510,13 +1973,12 @@
15101973
15111974 void fib_sync_mtu(struct net_device *dev, u32 orig_mtu)
15121975 {
1513
- unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1514
- struct hlist_head *head = &fib_info_devhash[hash];
1976
+ struct hlist_head *head = fib_info_devhash_bucket(dev);
15151977 struct fib_nh *nh;
15161978
15171979 hlist_for_each_entry(nh, head, nh_hash) {
1518
- if (nh->nh_dev == dev)
1519
- nh_update_mtu(nh, dev->mtu, orig_mtu);
1980
+ if (nh->fib_nh_dev == dev)
1981
+ fib_nhc_update_mtu(&nh->nh_common, dev->mtu, orig_mtu);
15201982 }
15211983 }
15221984
....@@ -1525,15 +1987,16 @@
15251987 * NETDEV_DOWN 0 LINKDOWN|DEAD Link down, not for scope host
15261988 * NETDEV_DOWN 1 LINKDOWN|DEAD Last address removed
15271989 * NETDEV_UNREGISTER 1 LINKDOWN|DEAD Device removed
1990
+ *
1991
+ * only used when fib_nh is built into fib_info
15281992 */
15291993 int fib_sync_down_dev(struct net_device *dev, unsigned long event, bool force)
15301994 {
1531
- int ret = 0;
1532
- int scope = RT_SCOPE_NOWHERE;
1995
+ struct hlist_head *head = fib_info_devhash_bucket(dev);
15331996 struct fib_info *prev_fi = NULL;
1534
- unsigned int hash = fib_devindex_hashfn(dev->ifindex);
1535
- struct hlist_head *head = &fib_info_devhash[hash];
1997
+ int scope = RT_SCOPE_NOWHERE;
15361998 struct fib_nh *nh;
1999
+ int ret = 0;
15372000
15382001 if (force)
15392002 scope = -1;
....@@ -1543,22 +2006,22 @@
15432006 int dead;
15442007
15452008 BUG_ON(!fi->fib_nhs);
1546
- if (nh->nh_dev != dev || fi == prev_fi)
2009
+ if (nh->fib_nh_dev != dev || fi == prev_fi)
15472010 continue;
15482011 prev_fi = fi;
15492012 dead = 0;
15502013 change_nexthops(fi) {
1551
- if (nexthop_nh->nh_flags & RTNH_F_DEAD)
2014
+ if (nexthop_nh->fib_nh_flags & RTNH_F_DEAD)
15522015 dead++;
1553
- else if (nexthop_nh->nh_dev == dev &&
1554
- nexthop_nh->nh_scope != scope) {
2016
+ else if (nexthop_nh->fib_nh_dev == dev &&
2017
+ nexthop_nh->fib_nh_scope != scope) {
15552018 switch (event) {
15562019 case NETDEV_DOWN:
15572020 case NETDEV_UNREGISTER:
1558
- nexthop_nh->nh_flags |= RTNH_F_DEAD;
1559
- /* fall through */
2021
+ nexthop_nh->fib_nh_flags |= RTNH_F_DEAD;
2022
+ fallthrough;
15602023 case NETDEV_CHANGE:
1561
- nexthop_nh->nh_flags |= RTNH_F_LINKDOWN;
2024
+ nexthop_nh->fib_nh_flags |= RTNH_F_LINKDOWN;
15622025 break;
15632026 }
15642027 call_fib_nh_notifiers(nexthop_nh,
....@@ -1567,7 +2030,7 @@
15672030 }
15682031 #ifdef CONFIG_IP_ROUTE_MULTIPATH
15692032 if (event == NETDEV_UNREGISTER &&
1570
- nexthop_nh->nh_dev == dev) {
2033
+ nexthop_nh->fib_nh_dev == dev) {
15712034 dead = fi->fib_nhs;
15722035 break;
15732036 }
....@@ -1578,7 +2041,7 @@
15782041 case NETDEV_DOWN:
15792042 case NETDEV_UNREGISTER:
15802043 fi->fib_flags |= RTNH_F_DEAD;
1581
- /* fall through */
2044
+ fallthrough;
15822045 case NETDEV_CHANGE:
15832046 fi->fib_flags |= RTNH_F_LINKDOWN;
15842047 break;
....@@ -1606,6 +2069,7 @@
16062069
16072070 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
16082071 struct fib_info *next_fi = fa->fa_info;
2072
+ struct fib_nh_common *nhc;
16092073
16102074 if (fa->fa_slen != slen)
16112075 continue;
....@@ -1627,8 +2091,9 @@
16272091 if (next_fi->fib_scope != res->scope ||
16282092 fa->fa_type != RTN_UNICAST)
16292093 continue;
1630
- if (!next_fi->fib_nh[0].nh_gw ||
1631
- next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK)
2094
+
2095
+ nhc = fib_info_nhc(next_fi, 0);
2096
+ if (!nhc->nhc_gw_family || nhc->nhc_scope != RT_SCOPE_LINK)
16322097 continue;
16332098
16342099 fib_alias_accessed(fa);
....@@ -1670,11 +2135,12 @@
16702135 /*
16712136 * Dead device goes up. We wake up dead nexthops.
16722137 * It takes sense only on multipath routes.
2138
+ *
2139
+ * only used when fib_nh is built into fib_info
16732140 */
1674
-int fib_sync_up(struct net_device *dev, unsigned int nh_flags)
2141
+int fib_sync_up(struct net_device *dev, unsigned char nh_flags)
16752142 {
16762143 struct fib_info *prev_fi;
1677
- unsigned int hash;
16782144 struct hlist_head *head;
16792145 struct fib_nh *nh;
16802146 int ret;
....@@ -1690,8 +2156,7 @@
16902156 }
16912157
16922158 prev_fi = NULL;
1693
- hash = fib_devindex_hashfn(dev->ifindex);
1694
- head = &fib_info_devhash[hash];
2159
+ head = fib_info_devhash_bucket(dev);
16952160 ret = 0;
16962161
16972162 hlist_for_each_entry(nh, head, nh_hash) {
....@@ -1699,24 +2164,24 @@
16992164 int alive;
17002165
17012166 BUG_ON(!fi->fib_nhs);
1702
- if (nh->nh_dev != dev || fi == prev_fi)
2167
+ if (nh->fib_nh_dev != dev || fi == prev_fi)
17032168 continue;
17042169
17052170 prev_fi = fi;
17062171 alive = 0;
17072172 change_nexthops(fi) {
1708
- if (!(nexthop_nh->nh_flags & nh_flags)) {
2173
+ if (!(nexthop_nh->fib_nh_flags & nh_flags)) {
17092174 alive++;
17102175 continue;
17112176 }
1712
- if (!nexthop_nh->nh_dev ||
1713
- !(nexthop_nh->nh_dev->flags & IFF_UP))
2177
+ if (!nexthop_nh->fib_nh_dev ||
2178
+ !(nexthop_nh->fib_nh_dev->flags & IFF_UP))
17142179 continue;
1715
- if (nexthop_nh->nh_dev != dev ||
2180
+ if (nexthop_nh->fib_nh_dev != dev ||
17162181 !__in_dev_get_rtnl(dev))
17172182 continue;
17182183 alive++;
1719
- nexthop_nh->nh_flags &= ~nh_flags;
2184
+ nexthop_nh->fib_nh_flags &= ~nh_flags;
17202185 call_fib_nh_notifiers(nexthop_nh, FIB_EVENT_NH_ADD);
17212186 } endfor_nexthops(fi)
17222187
....@@ -1736,13 +2201,19 @@
17362201 {
17372202 int state = NUD_REACHABLE;
17382203
1739
- if (nh->nh_scope == RT_SCOPE_LINK) {
2204
+ if (nh->fib_nh_scope == RT_SCOPE_LINK) {
17402205 struct neighbour *n;
17412206
17422207 rcu_read_lock_bh();
17432208
1744
- n = __ipv4_neigh_lookup_noref(nh->nh_dev,
1745
- (__force u32)nh->nh_gw);
2209
+ if (likely(nh->fib_nh_gw_family == AF_INET))
2210
+ n = __ipv4_neigh_lookup_noref(nh->fib_nh_dev,
2211
+ (__force u32)nh->fib_nh_gw4);
2212
+ else if (nh->fib_nh_gw_family == AF_INET6)
2213
+ n = __ipv6_neigh_lookup_noref_stub(nh->fib_nh_dev,
2214
+ &nh->fib_nh_gw6);
2215
+ else
2216
+ n = NULL;
17462217 if (n)
17472218 state = n->nud_state;
17482219
....@@ -1758,20 +2229,27 @@
17582229 struct net *net = fi->fib_net;
17592230 bool first = false;
17602231
1761
- for_nexthops(fi) {
1762
- if (net->ipv4.sysctl_fib_multipath_use_neigh) {
1763
- if (!fib_good_nh(nh))
2232
+ if (unlikely(res->fi->nh)) {
2233
+ nexthop_path_fib_result(res, hash);
2234
+ return;
2235
+ }
2236
+
2237
+ change_nexthops(fi) {
2238
+ if (READ_ONCE(net->ipv4.sysctl_fib_multipath_use_neigh)) {
2239
+ if (!fib_good_nh(nexthop_nh))
17642240 continue;
17652241 if (!first) {
17662242 res->nh_sel = nhsel;
2243
+ res->nhc = &nexthop_nh->nh_common;
17672244 first = true;
17682245 }
17692246 }
17702247
1771
- if (hash > atomic_read(&nh->nh_upper_bound))
2248
+ if (hash > atomic_read(&nexthop_nh->fib_nh_upper_bound))
17722249 continue;
17732250
17742251 res->nh_sel = nhsel;
2252
+ res->nhc = &nexthop_nh->nh_common;
17752253 return;
17762254 } endfor_nexthops(fi);
17772255 }
....@@ -1784,7 +2262,7 @@
17842262 goto check_saddr;
17852263
17862264 #ifdef CONFIG_IP_ROUTE_MULTIPATH
1787
- if (res->fi->fib_nhs > 1) {
2265
+ if (fib_info_num_path(res->fi) > 1) {
17882266 int h = fib_multipath_hash(net, fl4, skb, NULL);
17892267
17902268 fib_select_multipath(res, h);
....@@ -1798,5 +2276,5 @@
17982276
17992277 check_saddr:
18002278 if (!fl4->saddr)
1801
- fl4->saddr = FIB_RES_PREFSRC(net, *res);
2279
+ fl4->saddr = fib_result_prefsrc(net, res);
18022280 }