-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
rt_netlink.c
5139 lines (4394 loc) · 133 KB
/
rt_netlink.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
/* Kernel routing table updates using netlink over GNU/Linux system.
* Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
*/
#include <zebra.h>
#ifdef HAVE_NETLINK
/* The following definition is to workaround an issue in the Linux kernel
* header files with redefinition of 'struct in6_addr' in both
* netinet/in.h and linux/in6.h.
* Reference - https://sourceware.org/ml/libc-alpha/2013-01/msg00599.html
*/
#define _LINUX_IN6_H
#include <net/if_arp.h>
#include <linux/lwtunnel.h>
#include <linux/mpls_iptunnel.h>
#include <linux/seg6_iptunnel.h>
#include <linux/seg6_local.h>
#include <linux/neighbour.h>
#include <linux/rtnetlink.h>
#include <linux/nexthop.h>
/* Hack for GNU libc version 2. */
#ifndef MSG_TRUNC
#define MSG_TRUNC 0x20
#endif /* MSG_TRUNC */
#include "linklist.h"
#include "if.h"
#include "log.h"
#include "prefix.h"
#include "plist.h"
#include "plist_int.h"
#include "connected.h"
#include "table.h"
#include "memory.h"
#include "rib.h"
#include "frrevent.h"
#include "privs.h"
#include "nexthop.h"
#include "vrf.h"
#include "vty.h"
#include "mpls.h"
#include "vxlan.h"
#include "printfrr.h"
#include "zebra/zapi_msg.h"
#include "zebra/zebra_ns.h"
#include "zebra/zebra_vrf.h"
#include "zebra/rt.h"
#include "zebra/redistribute.h"
#include "zebra/interface.h"
#include "zebra/debug.h"
#include "zebra/rtadv.h"
#include "zebra/zebra_ptm.h"
#include "zebra/zebra_mpls.h"
#include "zebra/kernel_netlink.h"
#include "zebra/rt_netlink.h"
#include "zebra/zebra_nhg.h"
#include "zebra/zebra_mroute.h"
#include "zebra/zebra_vxlan.h"
#include "zebra/zebra_errors.h"
#include "zebra/zebra_evpn_mh.h"
#include "zebra/zebra_trace.h"
#include "zebra/zebra_neigh.h"
#include "lib/srv6.h"
#ifndef AF_MPLS
#define AF_MPLS 28
#endif
/* Re-defining as I am unable to include <linux/if_bridge.h> which has the
* UAPI for MAC sync. */
#ifndef _UAPI_LINUX_IF_BRIDGE_H
#define BR_SPH_LIST_SIZE 10
#endif
DEFINE_MTYPE_STATIC(LIB, NH_SRV6, "Nexthop srv6");
static vlanid_t filter_vlan = 0;
/* We capture whether the current kernel supports nexthop ids; by
* default, we'll use them if possible. There's also a configuration
* available to _disable_ use of kernel nexthops.
*/
static bool supports_nh;
struct gw_family_t {
uint16_t filler;
uint16_t family;
union g_addr gate;
};
static const char ipv4_ll_buf[16] = "169.254.0.1";
static struct in_addr ipv4_ll;
/* Is this a ipv4 over ipv6 route? */
static bool is_route_v4_over_v6(unsigned char rtm_family,
enum nexthop_types_t nexthop_type)
{
if (rtm_family == AF_INET
&& (nexthop_type == NEXTHOP_TYPE_IPV6
|| nexthop_type == NEXTHOP_TYPE_IPV6_IFINDEX))
return true;
return false;
}
/* Helper to control use of kernel-level nexthop ids */
static bool kernel_nexthops_supported(void)
{
return (supports_nh && !vrf_is_backend_netns()
&& zebra_nhg_kernel_nexthops_enabled());
}
/*
* Some people may only want to use NHGs created by protos and not
* implicitly created by Zebra. This check accounts for that.
*/
static bool proto_nexthops_only(void)
{
return zebra_nhg_proto_nexthops_only();
}
/* Is this a proto created NHG? */
static bool is_proto_nhg(uint32_t id, int type)
{
/* If type is available, use it as the source of truth */
if (type) {
if (type != ZEBRA_ROUTE_NHG)
return true;
return false;
}
if (id >= ZEBRA_NHG_PROTO_LOWER)
return true;
return false;
}
/* Is vni mcast group */
static bool is_mac_vni_mcast_group(struct ethaddr *mac, vni_t vni,
struct in_addr grp_addr)
{
if (!vni)
return false;
if (!is_zero_mac(mac))
return false;
if (!IN_MULTICAST(ntohl(grp_addr.s_addr)))
return false;
return true;
}
/*
* The ipv4_ll data structure is used for all 5549
* additions to the kernel. Let's figure out the
* correct value one time instead for every
* install/remove of a 5549 type route
*/
void rt_netlink_init(void)
{
inet_pton(AF_INET, ipv4_ll_buf, &ipv4_ll);
}
/*
* Mapping from dataplane neighbor flags to netlink flags
*/
static uint8_t neigh_flags_to_netlink(uint8_t dplane_flags)
{
uint8_t flags = 0;
if (dplane_flags & DPLANE_NTF_EXT_LEARNED)
flags |= NTF_EXT_LEARNED;
if (dplane_flags & DPLANE_NTF_ROUTER)
flags |= NTF_ROUTER;
if (dplane_flags & DPLANE_NTF_USE)
flags |= NTF_USE;
return flags;
}
/*
* Mapping from dataplane neighbor state to netlink state
*/
static uint16_t neigh_state_to_netlink(uint16_t dplane_state)
{
uint16_t state = 0;
if (dplane_state & DPLANE_NUD_REACHABLE)
state |= NUD_REACHABLE;
if (dplane_state & DPLANE_NUD_STALE)
state |= NUD_STALE;
if (dplane_state & DPLANE_NUD_NOARP)
state |= NUD_NOARP;
if (dplane_state & DPLANE_NUD_PROBE)
state |= NUD_PROBE;
if (dplane_state & DPLANE_NUD_INCOMPLETE)
state |= NUD_INCOMPLETE;
if (dplane_state & DPLANE_NUD_PERMANENT)
state |= NUD_PERMANENT;
if (dplane_state & DPLANE_NUD_FAILED)
state |= NUD_FAILED;
return state;
}
static inline bool is_selfroute(int proto)
{
if ((proto == RTPROT_BGP) || (proto == RTPROT_OSPF)
|| (proto == RTPROT_ZSTATIC) || (proto == RTPROT_ZEBRA)
|| (proto == RTPROT_ISIS) || (proto == RTPROT_RIPNG)
|| (proto == RTPROT_NHRP) || (proto == RTPROT_EIGRP)
|| (proto == RTPROT_LDP) || (proto == RTPROT_BABEL)
|| (proto == RTPROT_RIP) || (proto == RTPROT_SHARP)
|| (proto == RTPROT_PBR) || (proto == RTPROT_OPENFABRIC)
|| (proto == RTPROT_SRTE)) {
return true;
}
return false;
}
int zebra2proto(int proto)
{
switch (proto) {
case ZEBRA_ROUTE_BABEL:
proto = RTPROT_BABEL;
break;
case ZEBRA_ROUTE_BGP:
proto = RTPROT_BGP;
break;
case ZEBRA_ROUTE_OSPF:
case ZEBRA_ROUTE_OSPF6:
proto = RTPROT_OSPF;
break;
case ZEBRA_ROUTE_STATIC:
proto = RTPROT_ZSTATIC;
break;
case ZEBRA_ROUTE_ISIS:
proto = RTPROT_ISIS;
break;
case ZEBRA_ROUTE_RIP:
proto = RTPROT_RIP;
break;
case ZEBRA_ROUTE_RIPNG:
proto = RTPROT_RIPNG;
break;
case ZEBRA_ROUTE_NHRP:
proto = RTPROT_NHRP;
break;
case ZEBRA_ROUTE_EIGRP:
proto = RTPROT_EIGRP;
break;
case ZEBRA_ROUTE_LDP:
proto = RTPROT_LDP;
break;
case ZEBRA_ROUTE_SHARP:
proto = RTPROT_SHARP;
break;
case ZEBRA_ROUTE_PBR:
proto = RTPROT_PBR;
break;
case ZEBRA_ROUTE_OPENFABRIC:
proto = RTPROT_OPENFABRIC;
break;
case ZEBRA_ROUTE_SRTE:
proto = RTPROT_SRTE;
break;
case ZEBRA_ROUTE_TABLE:
case ZEBRA_ROUTE_NHG:
proto = RTPROT_ZEBRA;
break;
case ZEBRA_ROUTE_CONNECT:
case ZEBRA_ROUTE_LOCAL:
case ZEBRA_ROUTE_KERNEL:
proto = RTPROT_KERNEL;
break;
default:
/*
* When a user adds a new protocol this will show up
* to let them know to do something about it. This
* is intentionally a warn because we should see
* this as part of development of a new protocol
*/
zlog_debug(
"%s: Please add this protocol(%d) to proper rt_netlink.c handling",
__func__, proto);
proto = RTPROT_ZEBRA;
break;
}
return proto;
}
static inline int proto2zebra(int proto, int family, bool is_nexthop)
{
switch (proto) {
case RTPROT_BABEL:
proto = ZEBRA_ROUTE_BABEL;
break;
case RTPROT_BGP:
proto = ZEBRA_ROUTE_BGP;
break;
case RTPROT_OSPF:
proto = (family == AF_INET) ? ZEBRA_ROUTE_OSPF
: ZEBRA_ROUTE_OSPF6;
break;
case RTPROT_ISIS:
proto = ZEBRA_ROUTE_ISIS;
break;
case RTPROT_RIP:
proto = ZEBRA_ROUTE_RIP;
break;
case RTPROT_RIPNG:
proto = ZEBRA_ROUTE_RIPNG;
break;
case RTPROT_NHRP:
proto = ZEBRA_ROUTE_NHRP;
break;
case RTPROT_EIGRP:
proto = ZEBRA_ROUTE_EIGRP;
break;
case RTPROT_LDP:
proto = ZEBRA_ROUTE_LDP;
break;
case RTPROT_STATIC:
case RTPROT_ZSTATIC:
proto = ZEBRA_ROUTE_STATIC;
break;
case RTPROT_SHARP:
proto = ZEBRA_ROUTE_SHARP;
break;
case RTPROT_PBR:
proto = ZEBRA_ROUTE_PBR;
break;
case RTPROT_OPENFABRIC:
proto = ZEBRA_ROUTE_OPENFABRIC;
break;
case RTPROT_SRTE:
proto = ZEBRA_ROUTE_SRTE;
break;
case RTPROT_UNSPEC:
case RTPROT_REDIRECT:
case RTPROT_KERNEL:
case RTPROT_BOOT:
case RTPROT_GATED:
case RTPROT_RA:
case RTPROT_MRT:
case RTPROT_BIRD:
case RTPROT_DNROUTED:
case RTPROT_XORP:
case RTPROT_NTK:
case RTPROT_MROUTED:
case RTPROT_KEEPALIVED:
case RTPROT_OPENR:
proto = ZEBRA_ROUTE_KERNEL;
break;
case RTPROT_ZEBRA:
if (is_nexthop) {
proto = ZEBRA_ROUTE_NHG;
break;
}
proto = ZEBRA_ROUTE_KERNEL;
break;
default:
/*
* When a user adds a new protocol this will show up
* to let them know to do something about it. This
* is intentionally a warn because we should see
* this as part of development of a new protocol
*/
zlog_debug(
"%s: Please add this protocol(%d) to proper rt_netlink.c handling",
__func__, proto);
proto = ZEBRA_ROUTE_KERNEL;
break;
}
return proto;
}
/**
* @parse_encap_mpls() - Parses encapsulated mpls attributes
* @tb: Pointer to rtattr to look for nested items in.
* @labels: Pointer to store labels in.
*
* Return: Number of mpls labels found.
*/
static int parse_encap_mpls(struct rtattr *tb, mpls_label_t *labels)
{
struct rtattr *tb_encap[MPLS_IPTUNNEL_MAX + 1] = {0};
mpls_lse_t *lses = NULL;
int num_labels = 0;
uint32_t ttl = 0;
uint32_t bos = 0;
uint32_t exp = 0;
mpls_label_t label = 0;
netlink_parse_rtattr_nested(tb_encap, MPLS_IPTUNNEL_MAX, tb);
lses = (mpls_lse_t *)RTA_DATA(tb_encap[MPLS_IPTUNNEL_DST]);
while (!bos && num_labels < MPLS_MAX_LABELS) {
mpls_lse_decode(lses[num_labels], &label, &ttl, &exp, &bos);
labels[num_labels++] = label;
}
return num_labels;
}
/**
* @parse_encap_seg6local_flavors() - Parses encapsulated SRv6 flavors
* attributes
* @tb: Pointer to rtattr to look for nested items in.
* @flv: Pointer to store SRv6 flavors info in.
*
* Return: 0 on success, non-zero on error
*/
static int parse_encap_seg6local_flavors(struct rtattr *tb,
struct seg6local_flavors_info *flv)
{
struct rtattr *tb_encap[SEG6_LOCAL_FLV_MAX + 1] = {};
netlink_parse_rtattr_nested(tb_encap, SEG6_LOCAL_FLV_MAX, tb);
if (tb_encap[SEG6_LOCAL_FLV_OPERATION])
flv->flv_ops = *(uint32_t *)RTA_DATA(
tb_encap[SEG6_LOCAL_FLV_OPERATION]);
if (tb_encap[SEG6_LOCAL_FLV_LCBLOCK_BITS])
flv->lcblock_len = *(uint8_t *)RTA_DATA(
tb_encap[SEG6_LOCAL_FLV_LCBLOCK_BITS]);
if (tb_encap[SEG6_LOCAL_FLV_LCNODE_FN_BITS])
flv->lcnode_func_len = *(uint8_t *)RTA_DATA(
tb_encap[SEG6_LOCAL_FLV_LCNODE_FN_BITS]);
return 0;
}
static enum seg6local_action_t
parse_encap_seg6local(struct rtattr *tb,
struct seg6local_context *ctx)
{
struct rtattr *tb_encap[SEG6_LOCAL_MAX + 1] = {};
enum seg6local_action_t act = ZEBRA_SEG6_LOCAL_ACTION_UNSPEC;
netlink_parse_rtattr_nested(tb_encap, SEG6_LOCAL_MAX, tb);
if (tb_encap[SEG6_LOCAL_ACTION])
act = *(uint32_t *)RTA_DATA(tb_encap[SEG6_LOCAL_ACTION]);
if (tb_encap[SEG6_LOCAL_NH4])
ctx->nh4 = *(struct in_addr *)RTA_DATA(
tb_encap[SEG6_LOCAL_NH4]);
if (tb_encap[SEG6_LOCAL_NH6])
ctx->nh6 = *(struct in6_addr *)RTA_DATA(
tb_encap[SEG6_LOCAL_NH6]);
if (tb_encap[SEG6_LOCAL_TABLE])
ctx->table = *(uint32_t *)RTA_DATA(tb_encap[SEG6_LOCAL_TABLE]);
if (tb_encap[SEG6_LOCAL_VRFTABLE])
ctx->table =
*(uint32_t *)RTA_DATA(tb_encap[SEG6_LOCAL_VRFTABLE]);
if (tb_encap[SEG6_LOCAL_FLAVORS]) {
parse_encap_seg6local_flavors(tb_encap[SEG6_LOCAL_FLAVORS],
&ctx->flv);
}
return act;
}
static int parse_encap_seg6(struct rtattr *tb, struct in6_addr *segs)
{
struct rtattr *tb_encap[SEG6_IPTUNNEL_MAX + 1] = {};
struct seg6_iptunnel_encap *ipt = NULL;
int i;
netlink_parse_rtattr_nested(tb_encap, SEG6_IPTUNNEL_MAX, tb);
if (tb_encap[SEG6_IPTUNNEL_SRH]) {
ipt = (struct seg6_iptunnel_encap *)
RTA_DATA(tb_encap[SEG6_IPTUNNEL_SRH]);
for (i = ipt->srh[0].first_segment; i >= 0; i--)
memcpy(&segs[i], &ipt->srh[0].segments[i],
sizeof(struct in6_addr));
return ipt->srh[0].first_segment + 1;
}
return 0;
}
static struct nexthop
parse_nexthop_unicast(ns_id_t ns_id, struct rtmsg *rtm, struct rtattr **tb,
enum blackhole_type bh_type, int index, void *prefsrc,
void *gate, afi_t afi, vrf_id_t vrf_id)
{
struct interface *ifp = NULL;
struct nexthop nh = {.weight = 1};
mpls_label_t labels[MPLS_MAX_LABELS] = {0};
int num_labels = 0;
enum seg6local_action_t seg6l_act = ZEBRA_SEG6_LOCAL_ACTION_UNSPEC;
struct seg6local_context seg6l_ctx = {};
struct in6_addr segs[SRV6_MAX_SIDS] = {};
int num_segs = 0;
vrf_id_t nh_vrf_id = vrf_id;
size_t sz = (afi == AFI_IP) ? 4 : 16;
if (bh_type == BLACKHOLE_UNSPEC) {
if (index && !gate)
nh.type = NEXTHOP_TYPE_IFINDEX;
else if (index && gate)
nh.type = (afi == AFI_IP) ? NEXTHOP_TYPE_IPV4_IFINDEX
: NEXTHOP_TYPE_IPV6_IFINDEX;
else if (!index && gate)
nh.type = (afi == AFI_IP) ? NEXTHOP_TYPE_IPV4
: NEXTHOP_TYPE_IPV6;
else {
nh.type = NEXTHOP_TYPE_BLACKHOLE;
nh.bh_type = bh_type;
}
} else {
nh.type = NEXTHOP_TYPE_BLACKHOLE;
nh.bh_type = bh_type;
}
nh.ifindex = index;
if (prefsrc)
memcpy(&nh.src, prefsrc, sz);
if (gate)
memcpy(&nh.gate, gate, sz);
if (index) {
ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id), index);
if (ifp)
nh_vrf_id = ifp->vrf->vrf_id;
}
nh.vrf_id = nh_vrf_id;
if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE]
&& *(uint16_t *)RTA_DATA(tb[RTA_ENCAP_TYPE])
== LWTUNNEL_ENCAP_MPLS) {
num_labels = parse_encap_mpls(tb[RTA_ENCAP], labels);
}
if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE]
&& *(uint16_t *)RTA_DATA(tb[RTA_ENCAP_TYPE])
== LWTUNNEL_ENCAP_SEG6_LOCAL) {
seg6l_act = parse_encap_seg6local(tb[RTA_ENCAP], &seg6l_ctx);
}
if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE]
&& *(uint16_t *)RTA_DATA(tb[RTA_ENCAP_TYPE])
== LWTUNNEL_ENCAP_SEG6) {
num_segs = parse_encap_seg6(tb[RTA_ENCAP], segs);
}
if (rtm->rtm_flags & RTNH_F_ONLINK)
SET_FLAG(nh.flags, NEXTHOP_FLAG_ONLINK);
if (rtm->rtm_flags & RTNH_F_LINKDOWN)
SET_FLAG(nh.flags, NEXTHOP_FLAG_LINKDOWN);
if (num_labels)
nexthop_add_labels(&nh, ZEBRA_LSP_STATIC, num_labels, labels);
/* Resolve default values for SRv6 flavors */
if (seg6l_ctx.flv.flv_ops != ZEBRA_SEG6_LOCAL_FLV_OP_UNSPEC) {
if (seg6l_ctx.flv.lcblock_len == 0)
seg6l_ctx.flv.lcblock_len =
ZEBRA_DEFAULT_SEG6_LOCAL_FLV_LCBLOCK_LEN;
if (seg6l_ctx.flv.lcnode_func_len == 0)
seg6l_ctx.flv.lcnode_func_len =
ZEBRA_DEFAULT_SEG6_LOCAL_FLV_LCNODE_FN_LEN;
}
if (seg6l_act != ZEBRA_SEG6_LOCAL_ACTION_UNSPEC)
nexthop_add_srv6_seg6local(&nh, seg6l_act, &seg6l_ctx);
if (num_segs)
nexthop_add_srv6_seg6(&nh, segs, num_segs);
return nh;
}
static uint16_t parse_multipath_nexthops_unicast(ns_id_t ns_id, struct nexthop_group *ng,
struct rtmsg *rtm, struct rtnexthop *rtnh,
struct rtattr **tb, void *prefsrc, vrf_id_t vrf_id)
{
void *gate = NULL;
struct interface *ifp = NULL;
int index = 0;
/* MPLS labels */
mpls_label_t labels[MPLS_MAX_LABELS] = {0};
int num_labels = 0;
enum seg6local_action_t seg6l_act = ZEBRA_SEG6_LOCAL_ACTION_UNSPEC;
struct seg6local_context seg6l_ctx = {};
struct in6_addr segs[SRV6_MAX_SIDS] = {};
int num_segs = 0;
struct rtattr *rtnh_tb[RTA_MAX + 1] = {};
int len = RTA_PAYLOAD(tb[RTA_MULTIPATH]);
vrf_id_t nh_vrf_id = vrf_id;
for (;;) {
struct nexthop *nh = NULL;
if (len < (int)sizeof(*rtnh) || rtnh->rtnh_len > len)
break;
index = rtnh->rtnh_ifindex;
if (index) {
/*
* Yes we are looking this up
* for every nexthop and just
* using the last one looked
* up right now
*/
ifp = if_lookup_by_index_per_ns(zebra_ns_lookup(ns_id),
index);
if (ifp)
nh_vrf_id = ifp->vrf->vrf_id;
else {
flog_warn(
EC_ZEBRA_UNKNOWN_INTERFACE,
"%s: Unknown interface %u specified, defaulting to VRF_DEFAULT",
__func__, index);
nh_vrf_id = VRF_DEFAULT;
}
} else
nh_vrf_id = vrf_id;
if (rtnh->rtnh_len > sizeof(*rtnh)) {
netlink_parse_rtattr(rtnh_tb, RTA_MAX, RTNH_DATA(rtnh),
rtnh->rtnh_len - sizeof(*rtnh));
if (rtnh_tb[RTA_GATEWAY])
gate = RTA_DATA(rtnh_tb[RTA_GATEWAY]);
if (rtnh_tb[RTA_ENCAP] && rtnh_tb[RTA_ENCAP_TYPE]
&& *(uint16_t *)RTA_DATA(rtnh_tb[RTA_ENCAP_TYPE])
== LWTUNNEL_ENCAP_MPLS) {
num_labels = parse_encap_mpls(
rtnh_tb[RTA_ENCAP], labels);
}
if (rtnh_tb[RTA_ENCAP] && rtnh_tb[RTA_ENCAP_TYPE]
&& *(uint16_t *)RTA_DATA(rtnh_tb[RTA_ENCAP_TYPE])
== LWTUNNEL_ENCAP_SEG6_LOCAL) {
seg6l_act = parse_encap_seg6local(
rtnh_tb[RTA_ENCAP], &seg6l_ctx);
}
if (rtnh_tb[RTA_ENCAP] && rtnh_tb[RTA_ENCAP_TYPE]
&& *(uint16_t *)RTA_DATA(rtnh_tb[RTA_ENCAP_TYPE])
== LWTUNNEL_ENCAP_SEG6) {
num_segs = parse_encap_seg6(rtnh_tb[RTA_ENCAP],
segs);
}
}
if (gate && rtm->rtm_family == AF_INET) {
if (index)
nh = nexthop_from_ipv4_ifindex(
gate, prefsrc, index, nh_vrf_id);
else
nh = nexthop_from_ipv4(gate, prefsrc,
nh_vrf_id);
} else if (gate && rtm->rtm_family == AF_INET6) {
if (index)
nh = nexthop_from_ipv6_ifindex(
gate, index, nh_vrf_id);
else
nh = nexthop_from_ipv6(gate, nh_vrf_id);
} else
nh = nexthop_from_ifindex(index, nh_vrf_id);
if (nh) {
nh->weight = rtnh->rtnh_hops + 1;
if (num_labels)
nexthop_add_labels(nh, ZEBRA_LSP_STATIC,
num_labels, labels);
/* Resolve default values for SRv6 flavors */
if (seg6l_ctx.flv.flv_ops !=
ZEBRA_SEG6_LOCAL_FLV_OP_UNSPEC) {
if (seg6l_ctx.flv.lcblock_len == 0)
seg6l_ctx.flv.lcblock_len =
ZEBRA_DEFAULT_SEG6_LOCAL_FLV_LCBLOCK_LEN;
if (seg6l_ctx.flv.lcnode_func_len == 0)
seg6l_ctx.flv.lcnode_func_len =
ZEBRA_DEFAULT_SEG6_LOCAL_FLV_LCNODE_FN_LEN;
}
if (seg6l_act != ZEBRA_SEG6_LOCAL_ACTION_UNSPEC)
nexthop_add_srv6_seg6local(nh, seg6l_act,
&seg6l_ctx);
if (num_segs)
nexthop_add_srv6_seg6(nh, segs, num_segs);
if (rtnh->rtnh_flags & RTNH_F_ONLINK)
SET_FLAG(nh->flags, NEXTHOP_FLAG_ONLINK);
/* Add to temporary list */
nexthop_group_add_sorted(ng, nh);
}
if (rtnh->rtnh_len == 0)
break;
len -= NLMSG_ALIGN(rtnh->rtnh_len);
rtnh = RTNH_NEXT(rtnh);
}
uint16_t nhop_num = nexthop_group_nexthop_num(ng);
return nhop_num;
}
/* Looking up routing table by netlink interface. */
int netlink_route_change_read_unicast_internal(struct nlmsghdr *h,
ns_id_t ns_id, int startup,
struct zebra_dplane_ctx *ctx)
{
int len;
struct rtmsg *rtm;
struct rtattr *tb[RTA_MAX + 1];
uint32_t flags = 0;
struct prefix p;
struct prefix_ipv6 src_p = {};
vrf_id_t vrf_id;
bool selfroute;
char anyaddr[16] = {0};
int proto = ZEBRA_ROUTE_KERNEL;
int index = 0;
int table;
int metric = 0;
uint32_t mtu = 0;
uint8_t distance = 0;
route_tag_t tag = 0;
uint32_t nhe_id = 0;
void *dest = NULL;
void *gate = NULL;
void *prefsrc = NULL; /* IPv4 preferred source host address */
void *src = NULL; /* IPv6 srcdest source prefix */
enum blackhole_type bh_type = BLACKHOLE_UNSPEC;
frrtrace(3, frr_zebra, netlink_route_change_read_unicast, h, ns_id,
startup);
rtm = NLMSG_DATA(h);
if (startup && h->nlmsg_type != RTM_NEWROUTE)
return 0;
switch (rtm->rtm_type) {
case RTN_UNICAST:
break;
case RTN_BLACKHOLE:
bh_type = BLACKHOLE_NULL;
break;
case RTN_UNREACHABLE:
bh_type = BLACKHOLE_REJECT;
break;
case RTN_PROHIBIT:
bh_type = BLACKHOLE_ADMINPROHIB;
break;
default:
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("Route rtm_type: %s(%d) intentionally ignoring",
nl_rttype_to_str(rtm->rtm_type),
rtm->rtm_type);
return 0;
}
len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct rtmsg));
if (len < 0) {
zlog_err(
"%s: Message received from netlink is of a broken size %d %zu",
__func__, h->nlmsg_len,
(size_t)NLMSG_LENGTH(sizeof(struct rtmsg)));
return -1;
}
netlink_parse_rtattr(tb, RTA_MAX, RTM_RTA(rtm), len);
if (rtm->rtm_flags & RTM_F_CLONED)
return 0;
if (rtm->rtm_protocol == RTPROT_REDIRECT)
return 0;
selfroute = is_selfroute(rtm->rtm_protocol);
if (!startup && selfroute && h->nlmsg_type == RTM_NEWROUTE &&
!zrouter.asic_offloaded && !ctx) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("Route type: %d Received that we think we have originated, ignoring",
rtm->rtm_protocol);
return 0;
}
/* We don't care about change notifications for the MPLS table. */
/* TODO: Revisit this. */
if (rtm->rtm_family == AF_MPLS)
return 0;
/* Table corresponding to route. */
if (tb[RTA_TABLE])
table = *(int *)RTA_DATA(tb[RTA_TABLE]);
else
table = rtm->rtm_table;
/* Map to VRF */
vrf_id = zebra_vrf_lookup_by_table(table, ns_id);
if (vrf_id == VRF_DEFAULT) {
if (!is_zebra_valid_kernel_table(table)
&& !is_zebra_main_routing_table(table))
return 0;
}
if (rtm->rtm_flags & RTM_F_TRAP)
flags |= ZEBRA_FLAG_TRAPPED;
if (rtm->rtm_flags & RTM_F_OFFLOAD)
flags |= ZEBRA_FLAG_OFFLOADED;
if (rtm->rtm_flags & RTM_F_OFFLOAD_FAILED)
flags |= ZEBRA_FLAG_OFFLOAD_FAILED;
if (h->nlmsg_flags & NLM_F_APPEND)
flags |= ZEBRA_FLAG_OUTOFSYNC;
/* Route which inserted by Zebra. */
if (selfroute) {
flags |= ZEBRA_FLAG_SELFROUTE;
proto = proto2zebra(rtm->rtm_protocol, rtm->rtm_family, false);
}
if (tb[RTA_OIF])
index = *(int *)RTA_DATA(tb[RTA_OIF]);
if (tb[RTA_DST])
dest = RTA_DATA(tb[RTA_DST]);
else
dest = anyaddr;
if (tb[RTA_SRC])
src = RTA_DATA(tb[RTA_SRC]);
else
src = anyaddr;
if (tb[RTA_PREFSRC])
prefsrc = RTA_DATA(tb[RTA_PREFSRC]);
if (tb[RTA_GATEWAY])
gate = RTA_DATA(tb[RTA_GATEWAY]);
if (tb[RTA_NH_ID])
nhe_id = *(uint32_t *)RTA_DATA(tb[RTA_NH_ID]);
if (tb[RTA_PRIORITY])
metric = *(int *)RTA_DATA(tb[RTA_PRIORITY]);
#if defined(SUPPORT_REALMS)
if (tb[RTA_FLOW])
tag = *(uint32_t *)RTA_DATA(tb[RTA_FLOW]);
#endif
if (tb[RTA_METRICS]) {
struct rtattr *mxrta[RTAX_MAX + 1];
netlink_parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(tb[RTA_METRICS]),
RTA_PAYLOAD(tb[RTA_METRICS]));
if (mxrta[RTAX_MTU])
mtu = *(uint32_t *)RTA_DATA(mxrta[RTAX_MTU]);
}
if (rtm->rtm_family == AF_INET) {
p.family = AF_INET;
if (rtm->rtm_dst_len > IPV4_MAX_BITLEN) {
zlog_err(
"Invalid destination prefix length: %u received from kernel route change",
rtm->rtm_dst_len);
return -1;
}
memcpy(&p.u.prefix4, dest, 4);
p.prefixlen = rtm->rtm_dst_len;
if (rtm->rtm_src_len != 0) {
flog_warn(
EC_ZEBRA_UNSUPPORTED_V4_SRCDEST,
"unsupported IPv4 sourcedest route (dest %pFX vrf %u)",
&p, vrf_id);
return 0;
}
/* Force debug below to not display anything for source */
src_p.prefixlen = 0;
} else if (rtm->rtm_family == AF_INET6) {
p.family = AF_INET6;
if (rtm->rtm_dst_len > IPV6_MAX_BITLEN) {
zlog_err(
"Invalid destination prefix length: %u received from kernel route change",
rtm->rtm_dst_len);
return -1;
}
memcpy(&p.u.prefix6, dest, 16);
p.prefixlen = rtm->rtm_dst_len;
src_p.family = AF_INET6;
if (rtm->rtm_src_len > IPV6_MAX_BITLEN) {
zlog_err(
"Invalid source prefix length: %u received from kernel route change",
rtm->rtm_src_len);
return -1;
}
memcpy(&src_p.prefix, src, 16);
src_p.prefixlen = rtm->rtm_src_len;
} else {
/* We only handle the AFs we handle... */
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("%s: unknown address-family %u", __func__,
rtm->rtm_family);
return 0;
}
/*
* For ZEBRA_ROUTE_KERNEL types:
*
* The metric/priority of the route received from the kernel
* is a 32 bit number. We are going to interpret the high
* order byte as the Admin Distance and the low order 3 bytes
* as the metric.
*
* This will allow us to do two things:
* 1) Allow the creation of kernel routes that can be
* overridden by zebra.
* 2) Allow the old behavior for 'most' kernel route types
* if a user enters 'ip route ...' v4 routes get a metric
* of 0 and v6 routes get a metric of 1024. Both of these
* values will end up with a admin distance of 0, which
* will cause them to win for the purposes of zebra.
*/
if (proto == ZEBRA_ROUTE_KERNEL) {
distance = (metric >> 24) & 0xFF;
metric = (metric & 0x00FFFFFF);
}
if (IS_ZEBRA_DEBUG_KERNEL) {
char buf2[PREFIX_STRLEN];
zlog_debug(
"%s %pFX%s%s vrf %s(%u) table_id: %u metric: %d Admin Distance: %d",
nl_msg_type_to_str(h->nlmsg_type), &p,
src_p.prefixlen ? " from " : "",
src_p.prefixlen ? prefix2str(&src_p, buf2, sizeof(buf2))
: "",
vrf_id_to_name(vrf_id), vrf_id, table, metric,
distance);
}
afi_t afi = AFI_IP;
if (rtm->rtm_family == AF_INET6)
afi = AFI_IP6;
if (h->nlmsg_type == RTM_NEWROUTE) {
struct route_entry *re;
struct nexthop_group *ng = NULL;
re = zebra_rib_route_entry_new(vrf_id, proto, 0, flags, nhe_id,
table, metric, mtu, distance,
tag);
if (!nhe_id)
ng = nexthop_group_new();
if (!tb[RTA_MULTIPATH]) {
struct nexthop *nexthop, nh;
if (!nhe_id) {
nh = parse_nexthop_unicast(
ns_id, rtm, tb, bh_type, index, prefsrc,
gate, afi, vrf_id);
nexthop = nexthop_new();
*nexthop = nh;
nexthop_group_add_sorted(ng, nexthop);
}
} else {
/* This is a multipath route */
struct rtnexthop *rtnh =
(struct rtnexthop *)RTA_DATA(tb[RTA_MULTIPATH]);
if (!nhe_id) {
uint16_t nhop_num;