forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbgp_zebra.c
2163 lines (1828 loc) · 59.2 KB
/
bgp_zebra.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
/* zebra client
Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
This file is part of GNU Zebra.
GNU Zebra is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
GNU Zebra is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Zebra; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#include <zebra.h>
#include "command.h"
#include "stream.h"
#include "network.h"
#include "prefix.h"
#include "log.h"
#include "sockunion.h"
#include "zclient.h"
#include "routemap.h"
#include "thread.h"
#include "queue.h"
#include "memory.h"
#include "lib/json.h"
#include "lib/bfd.h"
#include "filter.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_nexthop.h"
#include "bgpd/bgp_zebra.h"
#include "bgpd/bgp_fsm.h"
#include "bgpd/bgp_debug.h"
#include "bgpd/bgp_mpath.h"
#include "bgpd/bgp_nexthop.h"
#include "bgpd/bgp_nht.h"
#include "bgpd/bgp_bfd.h"
#if ENABLE_BGP_VNC
# include "bgpd/rfapi/rfapi_backend.h"
# include "bgpd/rfapi/vnc_export_bgp.h"
#endif
/* All information about zebra. */
struct zclient *zclient = NULL;
/* Growable buffer for nexthops sent to zebra */
struct stream *bgp_nexthop_buf = NULL;
struct stream *bgp_ifindices_buf = NULL;
/* These array buffers are used in making a copy of the attributes for
route-map apply. Arrays are being used here to minimize mallocs and
frees for the temporary copy of the attributes.
Given the zapi api expects the nexthop buffer to contain pointer to
pointers for nexthops, we couldnt have used a single nexthop variable
on the stack, hence we had two options:
1. maintain a linked-list and free it after zapi_*_route call
2. use an array to avoid number of mallocs.
Number of supported next-hops are finite, use of arrays should be ok. */
struct attr attr_cp[MULTIPATH_NUM];
struct attr_extra attr_extra_cp[MULTIPATH_NUM];
int attr_index = 0;
/* Once per address-family initialization of the attribute array */
#define BGP_INFO_ATTR_BUF_INIT()\
do {\
memset(attr_cp, 0, MULTIPATH_NUM * sizeof(struct attr));\
memset(attr_extra_cp, 0, MULTIPATH_NUM * sizeof(struct attr_extra));\
attr_index = 0;\
} while (0)
#define BGP_INFO_ATTR_BUF_COPY(info_src, info_dst)\
do { \
*info_dst = *info_src; \
assert(attr_index != MULTIPATH_NUM);\
attr_cp[attr_index].extra = &attr_extra_cp[attr_index]; \
bgp_attr_dup (&attr_cp[attr_index], info_src->attr); \
bgp_attr_deep_dup (&attr_cp[attr_index], info_src->attr); \
info_dst->attr = &attr_cp[attr_index]; \
attr_index++;\
} while (0)
#define BGP_INFO_ATTR_BUF_FREE(info) \
do { \
bgp_attr_deep_free(info->attr); \
} while (0)
/* Can we install into zebra? */
static inline int
bgp_install_info_to_zebra (struct bgp *bgp)
{
if (zclient->sock <= 0)
return 0;
if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bgp))
return 0;
return 1;
}
int zclient_num_connects;
/* Router-id update message from zebra. */
static int
bgp_router_id_update (int command, struct zclient *zclient, zebra_size_t length,
vrf_id_t vrf_id)
{
struct prefix router_id;
zebra_router_id_update_read(zclient->ibuf,&router_id);
if (BGP_DEBUG (zebra, ZEBRA))
{
char buf[PREFIX2STR_BUFFER];
prefix2str(&router_id, buf, sizeof(buf));
zlog_debug("Rx Router Id update VRF %u Id %s", vrf_id, buf);
}
bgp_router_id_zebra_bump (vrf_id, &router_id);
return 0;
}
/* Nexthop update message from zebra. */
static int
bgp_read_nexthop_update (int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
bgp_parse_nexthop_update(command, vrf_id);
return 0;
}
static int
bgp_read_import_check_update(int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
bgp_parse_nexthop_update(command, vrf_id);
return 0;
}
/* Set or clear interface on which unnumbered neighbor is configured. This
* would in turn cause BGP to initiate or turn off IPv6 RAs on this
* interface.
*/
static void
bgp_update_interface_nbrs (struct bgp *bgp, struct interface *ifp,
struct interface *upd_ifp)
{
struct listnode *node, *nnode;
struct peer *peer;
for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
{
if (peer->conf_if &&
(strcmp (peer->conf_if, ifp->name) == 0))
{
if (upd_ifp)
{
peer->ifp = upd_ifp;
bgp_zebra_initiate_radv (bgp, peer);
}
else
{
bgp_zebra_terminate_radv (bgp, peer);
peer->ifp = upd_ifp;
}
}
}
}
static void
bgp_start_interface_nbrs (struct bgp *bgp, struct interface *ifp)
{
struct listnode *node, *nnode;
struct peer *peer;
for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
{
if (peer->conf_if &&
(strcmp (peer->conf_if, ifp->name) == 0) &&
peer->status != Established)
{
if (peer_active(peer))
BGP_EVENT_ADD (peer, BGP_Stop);
BGP_EVENT_ADD (peer, BGP_Start);
}
}
}
static void
bgp_nbr_connected_add (struct bgp *bgp, struct nbr_connected *ifc)
{
struct listnode *node;
struct connected *connected;
struct interface *ifp;
struct prefix *p;
/* Kick-off the FSM for any relevant peers only if there is a
* valid local address on the interface.
*/
ifp = ifc->ifp;
for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
{
p = connected->address;
if (p->family == AF_INET6 &&
IN6_IS_ADDR_LINKLOCAL (&p->u.prefix6))
break;
}
if (!connected)
return;
bgp_start_interface_nbrs (bgp, ifp);
}
static void
bgp_nbr_connected_delete (struct bgp *bgp, struct nbr_connected *ifc, int del)
{
struct listnode *node, *nnode;
struct peer *peer;
struct interface *ifp;
for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
{
if (peer->conf_if && (strcmp (peer->conf_if, ifc->ifp->name) == 0))
{
peer->last_reset = PEER_DOWN_NBR_ADDR_DEL;
BGP_EVENT_ADD (peer, BGP_Stop);
}
}
/* Free neighbor also, if we're asked to. */
if (del)
{
ifp = ifc->ifp;
listnode_delete (ifp->nbr_connected, ifc);
nbr_connected_free (ifc);
}
}
/* Inteface addition message from zebra. */
static int
bgp_interface_add (int command, struct zclient *zclient, zebra_size_t length,
vrf_id_t vrf_id)
{
struct interface *ifp;
struct bgp *bgp;
ifp = zebra_interface_add_read (zclient->ibuf, vrf_id);
if (!ifp) // unexpected
return 0;
if (BGP_DEBUG (zebra, ZEBRA) && ifp)
zlog_debug("Rx Intf add VRF %u IF %s", vrf_id, ifp->name);
bgp = bgp_lookup_by_vrf_id (vrf_id);
if (!bgp)
return 0;
bgp_update_interface_nbrs (bgp, ifp, ifp);
return 0;
}
static int
bgp_interface_delete (int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct stream *s;
struct interface *ifp;
struct bgp *bgp;
s = zclient->ibuf;
ifp = zebra_interface_state_read (s, vrf_id);
if (!ifp) /* This may happen if we've just unregistered for a VRF. */
return 0;
ifp->ifindex = IFINDEX_DELETED;
if (BGP_DEBUG (zebra, ZEBRA))
zlog_debug("Rx Intf del VRF %u IF %s", vrf_id, ifp->name);
bgp = bgp_lookup_by_vrf_id (vrf_id);
if (!bgp)
return 0;
bgp_update_interface_nbrs (bgp, ifp, NULL);
return 0;
}
static int
bgp_interface_up (int command, struct zclient *zclient, zebra_size_t length,
vrf_id_t vrf_id)
{
struct stream *s;
struct interface *ifp;
struct connected *c;
struct nbr_connected *nc;
struct listnode *node, *nnode;
struct bgp *bgp;
s = zclient->ibuf;
ifp = zebra_interface_state_read (s, vrf_id);
if (! ifp)
return 0;
if (BGP_DEBUG (zebra, ZEBRA))
zlog_debug("Rx Intf up VRF %u IF %s", vrf_id, ifp->name);
bgp = bgp_lookup_by_vrf_id (vrf_id);
if (!bgp)
return 0;
for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
bgp_connected_add (bgp, c);
for (ALL_LIST_ELEMENTS (ifp->nbr_connected, node, nnode, nc))
bgp_nbr_connected_add (bgp, nc);
return 0;
}
static int
bgp_interface_down (int command, struct zclient *zclient, zebra_size_t length,
vrf_id_t vrf_id)
{
struct stream *s;
struct interface *ifp;
struct connected *c;
struct nbr_connected *nc;
struct listnode *node, *nnode;
struct bgp *bgp;
s = zclient->ibuf;
ifp = zebra_interface_state_read (s, vrf_id);
if (! ifp)
return 0;
if (BGP_DEBUG (zebra, ZEBRA))
zlog_debug("Rx Intf down VRF %u IF %s", vrf_id, ifp->name);
bgp = bgp_lookup_by_vrf_id (vrf_id);
if (!bgp)
return 0;
for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
bgp_connected_delete (bgp, c);
for (ALL_LIST_ELEMENTS (ifp->nbr_connected, node, nnode, nc))
bgp_nbr_connected_delete (bgp, nc, 1);
/* Fast external-failover */
{
struct peer *peer;
if (CHECK_FLAG (bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
return 0;
for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
{
#if defined(HAVE_CUMULUS)
/* Take down directly connected EBGP peers as well as 1-hop BFD
* tracked (directly connected) IBGP peers.
*/
if ((peer->ttl != 1) && (peer->gtsm_hops != 1) &&
(!peer->bfd_info || bgp_bfd_is_peer_multihop(peer)))
#else
/* Take down directly connected EBGP peers */
if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
#endif
continue;
if (ifp == peer->nexthop.ifp)
{
BGP_EVENT_ADD (peer, BGP_Stop);
peer->last_reset = PEER_DOWN_IF_DOWN;
}
}
}
return 0;
}
static int
bgp_interface_address_add (int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct connected *ifc;
ifc = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
if (ifc == NULL)
return 0;
if (bgp_debug_zebra(ifc->address))
{
char buf[PREFIX2STR_BUFFER];
prefix2str(ifc->address, buf, sizeof(buf));
zlog_debug("Rx Intf address add VRF %u IF %s addr %s",
vrf_id, ifc->ifp->name, buf);
}
if (if_is_operative (ifc->ifp))
{
struct bgp *bgp;
bgp = bgp_lookup_by_vrf_id (vrf_id);
if (!bgp)
return 0;
bgp_connected_add (bgp, ifc);
/* If we have learnt of any neighbors on this interface,
* check to kick off any BGP interface-based neighbors,
* but only if this is a link-local address.
*/
if (IN6_IS_ADDR_LINKLOCAL(&ifc->address->u.prefix6) &&
!list_isempty(ifc->ifp->nbr_connected))
bgp_start_interface_nbrs (bgp, ifc->ifp);
}
return 0;
}
static int
bgp_interface_address_delete (int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct connected *ifc;
struct bgp *bgp;
ifc = zebra_interface_address_read (command, zclient->ibuf, vrf_id);
if (ifc == NULL)
return 0;
if (bgp_debug_zebra(ifc->address))
{
char buf[PREFIX2STR_BUFFER];
prefix2str(ifc->address, buf, sizeof(buf));
zlog_debug("Rx Intf address del VRF %u IF %s addr %s",
vrf_id, ifc->ifp->name, buf);
}
if (if_is_operative (ifc->ifp))
{
bgp = bgp_lookup_by_vrf_id (vrf_id);
if (bgp)
bgp_connected_delete (bgp, ifc);
}
connected_free (ifc);
return 0;
}
static int
bgp_interface_nbr_address_add (int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct nbr_connected *ifc = NULL;
struct bgp *bgp;
ifc = zebra_interface_nbr_address_read (command, zclient->ibuf, vrf_id);
if (ifc == NULL)
return 0;
if (bgp_debug_zebra(ifc->address))
{
char buf[PREFIX2STR_BUFFER];
prefix2str(ifc->address, buf, sizeof(buf));
zlog_debug("Rx Intf neighbor add VRF %u IF %s addr %s",
vrf_id, ifc->ifp->name, buf);
}
if (if_is_operative (ifc->ifp))
{
bgp = bgp_lookup_by_vrf_id (vrf_id);
if (bgp)
bgp_nbr_connected_add (bgp, ifc);
}
return 0;
}
static int
bgp_interface_nbr_address_delete (int command, struct zclient *zclient,
zebra_size_t length, vrf_id_t vrf_id)
{
struct nbr_connected *ifc = NULL;
struct bgp *bgp;
ifc = zebra_interface_nbr_address_read (command, zclient->ibuf, vrf_id);
if (ifc == NULL)
return 0;
if (bgp_debug_zebra(ifc->address))
{
char buf[PREFIX2STR_BUFFER];
prefix2str(ifc->address, buf, sizeof(buf));
zlog_debug("Rx Intf neighbor del VRF %u IF %s addr %s",
vrf_id, ifc->ifp->name, buf);
}
if (if_is_operative (ifc->ifp))
{
bgp = bgp_lookup_by_vrf_id (vrf_id);
if (bgp)
bgp_nbr_connected_delete (bgp, ifc, 0);
}
nbr_connected_free (ifc);
return 0;
}
/* VRF update for an interface. */
static int
bgp_interface_vrf_update (int command, struct zclient *zclient, zebra_size_t length,
vrf_id_t vrf_id)
{
struct interface *ifp;
vrf_id_t new_vrf_id;
struct connected *c;
struct nbr_connected *nc;
struct listnode *node, *nnode;
struct bgp *bgp;
ifp = zebra_interface_vrf_update_read (zclient->ibuf, vrf_id, &new_vrf_id);
if (! ifp)
return 0;
if (BGP_DEBUG (zebra, ZEBRA) && ifp)
zlog_debug("Rx Intf VRF change VRF %u IF %s NewVRF %u",
vrf_id, ifp->name, new_vrf_id);
bgp = bgp_lookup_by_vrf_id (vrf_id);
if (!bgp)
return 0;
for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
bgp_connected_delete (bgp, c);
for (ALL_LIST_ELEMENTS (ifp->nbr_connected, node, nnode, nc))
bgp_nbr_connected_delete (bgp, nc, 1);
/* Fast external-failover */
{
struct peer *peer;
if (CHECK_FLAG (bgp->flags, BGP_FLAG_NO_FAST_EXT_FAILOVER))
return 0;
for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
{
if ((peer->ttl != 1) && (peer->gtsm_hops != 1))
continue;
if (ifp == peer->nexthop.ifp)
BGP_EVENT_ADD (peer, BGP_Stop);
}
}
if_update_vrf (ifp, ifp->name, strlen (ifp->name), new_vrf_id);
bgp = bgp_lookup_by_vrf_id (new_vrf_id);
if (!bgp)
return 0;
for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, c))
bgp_connected_add (bgp, c);
for (ALL_LIST_ELEMENTS (ifp->nbr_connected, node, nnode, nc))
bgp_nbr_connected_add (bgp, nc);
return 0;
}
/* Zebra route add and delete treatment. */
static int
zebra_read_ipv4 (int command, struct zclient *zclient, zebra_size_t length,
vrf_id_t vrf_id)
{
struct stream *s;
struct zapi_ipv4 api;
struct in_addr nexthop;
struct prefix_ipv4 p;
unsigned int ifindex;
int i;
struct bgp *bgp;
bgp = bgp_lookup_by_vrf_id (vrf_id);
if (!bgp)
return 0;
s = zclient->ibuf;
nexthop.s_addr = 0;
/* Type, flags, message. */
api.type = stream_getc (s);
api.instance = stream_getw (s);
api.flags = stream_getl (s);
api.message = stream_getc (s);
/* IPv4 prefix. */
memset (&p, 0, sizeof (struct prefix_ipv4));
p.family = AF_INET;
p.prefixlen = MIN(IPV4_MAX_PREFIXLEN, stream_getc (s));
stream_get (&p.prefix, s, PSIZE (p.prefixlen));
/* Nexthop, ifindex, distance, metric. */
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
{
api.nexthop_num = stream_getc (s);
nexthop.s_addr = stream_get_ipv4 (s);
}
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
{
api.ifindex_num = stream_getc (s);
ifindex = stream_getl (s); /* ifindex, unused */
}
else
{
ifindex = 0;
}
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
api.distance = stream_getc (s);
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
api.metric = stream_getl (s);
else
api.metric = 0;
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
api.tag = stream_getl (s);
else
api.tag = 0;
if (command == ZEBRA_REDISTRIBUTE_IPV4_ADD)
{
if (bgp_debug_zebra((struct prefix *)&p))
{
char buf[2][INET_ADDRSTRLEN];
zlog_debug("Rx IPv4 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %"ROUTE_TAG_PRI,
vrf_id,
zebra_route_string(api.type), api.instance,
inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])),
p.prefixlen,
inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
api.metric,
api.tag);
}
/*
* The ADD message is actually an UPDATE and there is no explicit DEL
* for a prior redistributed route, if any. So, perform an implicit
* DEL processing for the same redistributed route from any other
* source type.
*/
for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
{
if (i != api.type)
bgp_redistribute_delete(bgp, (struct prefix *)&p, i, api.instance);
}
/* Now perform the add/update. */
bgp_redistribute_add(bgp, (struct prefix *)&p, &nexthop, NULL, ifindex,
api.metric, api.type, api.instance, api.tag);
}
else if (command == ZEBRA_REDISTRIBUTE_IPV4_DEL)
{
if (bgp_debug_zebra((struct prefix *)&p))
{
char buf[2][INET_ADDRSTRLEN];
zlog_debug("Rx IPv4 route delete VRF %u %s[%d] %s/%d "
"nexthop %s metric %u tag %"ROUTE_TAG_PRI,
vrf_id,
zebra_route_string(api.type), api.instance,
inet_ntop(AF_INET, &p.prefix, buf[0], sizeof(buf[0])),
p.prefixlen,
inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
api.metric,
api.tag);
}
bgp_redistribute_delete(bgp, (struct prefix *)&p, api.type, api.instance);
}
return 0;
}
/* Zebra route add and delete treatment. */
static int
zebra_read_ipv6 (int command, struct zclient *zclient, zebra_size_t length,
vrf_id_t vrf_id)
{
struct stream *s;
struct zapi_ipv6 api;
struct in6_addr nexthop;
struct prefix_ipv6 p, src_p;
unsigned int ifindex;
int i;
struct bgp *bgp;
bgp = bgp_lookup_by_vrf_id (vrf_id);
if (!bgp)
return 0;
s = zclient->ibuf;
memset (&nexthop, 0, sizeof (struct in6_addr));
/* Type, flags, message. */
api.type = stream_getc (s);
api.instance = stream_getw (s);
api.flags = stream_getl (s);
api.message = stream_getc (s);
/* IPv6 prefix. */
memset (&p, 0, sizeof (struct prefix_ipv6));
p.family = AF_INET6;
p.prefixlen = MIN(IPV6_MAX_PREFIXLEN, stream_getc (s));
stream_get (&p.prefix, s, PSIZE (p.prefixlen));
memset (&src_p, 0, sizeof (struct prefix_ipv6));
src_p.family = AF_INET6;
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_SRCPFX))
{
src_p.prefixlen = stream_getc (s);
stream_get (&src_p.prefix, s, PSIZE (src_p.prefixlen));
}
if (src_p.prefixlen)
/* we completely ignore srcdest routes for now. */
return 0;
/* Nexthop, ifindex, distance, metric. */
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
{
api.nexthop_num = stream_getc (s);
stream_get (&nexthop, s, 16);
}
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_IFINDEX))
{
api.ifindex_num = stream_getc (s);
ifindex = stream_getl (s); /* ifindex, unused */
}
else
{
ifindex = 0;
}
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
api.distance = stream_getc (s);
else
api.distance = 0;
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
api.metric = stream_getl (s);
else
api.metric = 0;
if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
api.tag = stream_getl (s);
else
api.tag = 0;
/* Simply ignore link-local address. */
if (IN6_IS_ADDR_LINKLOCAL (&p.prefix))
return 0;
if (command == ZEBRA_REDISTRIBUTE_IPV6_ADD)
{
if (bgp_debug_zebra((struct prefix *)&p))
{
char buf[2][INET6_ADDRSTRLEN];
zlog_debug("Rx IPv6 route add VRF %u %s[%d] %s/%d nexthop %s metric %u tag %"ROUTE_TAG_PRI,
vrf_id,
zebra_route_string(api.type), api.instance,
inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
p.prefixlen,
inet_ntop(AF_INET, &nexthop, buf[1], sizeof(buf[1])),
api.metric,
api.tag);
}
/*
* The ADD message is actually an UPDATE and there is no explicit DEL
* for a prior redistributed route, if any. So, perform an implicit
* DEL processing for the same redistributed route from any other
* source type.
*/
for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
{
if (i != api.type)
bgp_redistribute_delete(bgp, (struct prefix *)&p, i, api.instance);
}
bgp_redistribute_add (bgp, (struct prefix *)&p, NULL, &nexthop, ifindex,
api.metric, api.type, api.instance, api.tag);
}
else if (command == ZEBRA_REDISTRIBUTE_IPV6_DEL)
{
if (bgp_debug_zebra((struct prefix *)&p))
{
char buf[2][INET6_ADDRSTRLEN];
zlog_debug("Rx IPv6 route delete VRF %u %s[%d] %s/%d "
"nexthop %s metric %u tag %"ROUTE_TAG_PRI,
vrf_id,
zebra_route_string(api.type), api.instance,
inet_ntop(AF_INET6, &p.prefix, buf[0], sizeof(buf[0])),
p.prefixlen,
inet_ntop(AF_INET6, &nexthop, buf[1], sizeof(buf[1])),
api.metric,
api.tag);
}
bgp_redistribute_delete (bgp, (struct prefix *) &p, api.type, api.instance);
}
return 0;
}
struct interface *
if_lookup_by_ipv4 (struct in_addr *addr, vrf_id_t vrf_id)
{
struct listnode *ifnode;
struct listnode *cnode;
struct interface *ifp;
struct connected *connected;
struct prefix_ipv4 p;
struct prefix *cp;
p.family = AF_INET;
p.prefix = *addr;
p.prefixlen = IPV4_MAX_BITLEN;
for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id), ifnode, ifp))
{
for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
{
cp = connected->address;
if (cp->family == AF_INET)
if (prefix_match (cp, (struct prefix *)&p))
return ifp;
}
}
return NULL;
}
struct interface *
if_lookup_by_ipv4_exact (struct in_addr *addr, vrf_id_t vrf_id)
{
struct listnode *ifnode;
struct listnode *cnode;
struct interface *ifp;
struct connected *connected;
struct prefix *cp;
for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id), ifnode, ifp))
{
for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
{
cp = connected->address;
if (cp->family == AF_INET)
if (IPV4_ADDR_SAME (&cp->u.prefix4, addr))
return ifp;
}
}
return NULL;
}
struct interface *
if_lookup_by_ipv6 (struct in6_addr *addr, ifindex_t ifindex, vrf_id_t vrf_id)
{
struct listnode *ifnode;
struct listnode *cnode;
struct interface *ifp;
struct connected *connected;
struct prefix_ipv6 p;
struct prefix *cp;
p.family = AF_INET6;
p.prefix = *addr;
p.prefixlen = IPV6_MAX_BITLEN;
for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id), ifnode, ifp))
{
for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
{
cp = connected->address;
if (cp->family == AF_INET6)
if (prefix_match (cp, (struct prefix *)&p))
{
if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6))
{
if (ifindex == ifp->ifindex)
return ifp;
}
else
return ifp;
}
}
}
return NULL;
}
struct interface *
if_lookup_by_ipv6_exact (struct in6_addr *addr, ifindex_t ifindex, vrf_id_t vrf_id)
{
struct listnode *ifnode;
struct listnode *cnode;
struct interface *ifp;
struct connected *connected;
struct prefix *cp;
for (ALL_LIST_ELEMENTS_RO (vrf_iflist(vrf_id), ifnode, ifp))
{
for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
{
cp = connected->address;
if (cp->family == AF_INET6)
if (IPV6_ADDR_SAME (&cp->u.prefix6, addr))
{
if (IN6_IS_ADDR_LINKLOCAL(&cp->u.prefix6))
{
if (ifindex == ifp->ifindex)
return ifp;
}
else
return ifp;
}
}
}
return NULL;
}
static int
if_get_ipv6_global (struct interface *ifp, struct in6_addr *addr)
{
struct listnode *cnode;
struct connected *connected;
struct prefix *cp;
for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
{
cp = connected->address;
if (cp->family == AF_INET6)
if (! IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
{
memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
return 1;
}
}
return 0;
}
static int
if_get_ipv6_local (struct interface *ifp, struct in6_addr *addr)
{
struct listnode *cnode;
struct connected *connected;
struct prefix *cp;
for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
{
cp = connected->address;
if (cp->family == AF_INET6)
if (IN6_IS_ADDR_LINKLOCAL (&cp->u.prefix6))
{
memcpy (addr, &cp->u.prefix6, IPV6_MAX_BYTELEN);
return 1;
}
}
return 0;
}
static int
if_get_ipv4_address (struct interface *ifp, struct in_addr *addr)
{
struct listnode *cnode;
struct connected *connected;
struct prefix *cp;
for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
{