forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzapi_msg.c
2621 lines (2210 loc) · 67.1 KB
/
zapi_msg.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 API message creation & consumption.
* Portions:
* Copyright (C) 1997-1999 Kunihiro Ishiguro
* Copyright (C) 2015-2018 Cumulus Networks, Inc.
* et al.
*
* This program 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 of the License, or (at your option)
* any later version.
*
* This program 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 this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
#include <libgen.h>
#include "lib/prefix.h"
#include "lib/command.h"
#include "lib/if.h"
#include "lib/thread.h"
#include "lib/stream.h"
#include "lib/memory.h"
#include "lib/table.h"
#include "lib/network.h"
#include "lib/sockunion.h"
#include "lib/log.h"
#include "lib/zclient.h"
#include "lib/privs.h"
#include "lib/network.h"
#include "lib/buffer.h"
#include "lib/nexthop.h"
#include "lib/vrf.h"
#include "lib/libfrr.h"
#include "lib/sockopt.h"
#include "zebra/zebra_router.h"
#include "zebra/rib.h"
#include "zebra/zebra_memory.h"
#include "zebra/zebra_ns.h"
#include "zebra/zebra_vrf.h"
#include "zebra/router-id.h"
#include "zebra/redistribute.h"
#include "zebra/debug.h"
#include "zebra/zebra_rnh.h"
#include "zebra/rt_netlink.h"
#include "zebra/interface.h"
#include "zebra/zebra_ptm.h"
#include "zebra/rtadv.h"
#include "zebra/zebra_mpls.h"
#include "zebra/zebra_mroute.h"
#include "zebra/zebra_vxlan.h"
#include "zebra/rt.h"
#include "zebra/zebra_pbr.h"
#include "zebra/table_manager.h"
#include "zebra/zapi_msg.h"
#include "zebra/zebra_errors.h"
#include "zebra/zebra_mlag.h"
/* Encoding helpers -------------------------------------------------------- */
static void zserv_encode_interface(struct stream *s, struct interface *ifp)
{
/* Interface information. */
struct zebra_if *zif = ifp->info;
stream_put(s, ifp->name, INTERFACE_NAMSIZ);
stream_putl(s, ifp->ifindex);
stream_putc(s, ifp->status);
stream_putq(s, ifp->flags);
stream_putc(s, ifp->ptm_enable);
stream_putc(s, ifp->ptm_status);
stream_putl(s, ifp->metric);
stream_putl(s, ifp->speed);
stream_putl(s, ifp->mtu);
stream_putl(s, ifp->mtu6);
stream_putl(s, ifp->bandwidth);
stream_putl(s, zif->link_ifindex);
stream_putl(s, ifp->ll_type);
stream_putl(s, ifp->hw_addr_len);
if (ifp->hw_addr_len)
stream_put(s, ifp->hw_addr, ifp->hw_addr_len);
/* Then, Traffic Engineering parameters if any */
if (HAS_LINK_PARAMS(ifp) && IS_LINK_PARAMS_SET(ifp->link_params)) {
stream_putc(s, 1);
zebra_interface_link_params_write(s, ifp);
} else
stream_putc(s, 0);
/* Write packet size. */
stream_putw_at(s, 0, stream_get_endp(s));
}
static void zserv_encode_vrf(struct stream *s, struct zebra_vrf *zvrf)
{
struct vrf_data data;
const char *netns_name = zvrf_ns_name(zvrf);
data.l.table_id = zvrf->table_id;
if (netns_name)
strlcpy(data.l.netns_name, basename((char *)netns_name),
NS_NAMSIZ);
else
memset(data.l.netns_name, 0, NS_NAMSIZ);
/* Pass the tableid and the netns NAME */
stream_put(s, &data, sizeof(struct vrf_data));
/* Interface information. */
stream_put(s, zvrf_name(zvrf), VRF_NAMSIZ);
/* Write packet size. */
stream_putw_at(s, 0, stream_get_endp(s));
}
static int zserv_encode_nexthop(struct stream *s, struct nexthop *nexthop)
{
stream_putl(s, nexthop->vrf_id);
stream_putc(s, nexthop->type);
switch (nexthop->type) {
case NEXTHOP_TYPE_IPV4:
case NEXTHOP_TYPE_IPV4_IFINDEX:
stream_put_in_addr(s, &nexthop->gate.ipv4);
stream_putl(s, nexthop->ifindex);
break;
case NEXTHOP_TYPE_IPV6:
stream_put(s, &nexthop->gate.ipv6, 16);
break;
case NEXTHOP_TYPE_IPV6_IFINDEX:
stream_put(s, &nexthop->gate.ipv6, 16);
stream_putl(s, nexthop->ifindex);
break;
case NEXTHOP_TYPE_IFINDEX:
stream_putl(s, nexthop->ifindex);
break;
default:
/* do nothing */
break;
}
return 1;
}
/* Send handlers ----------------------------------------------------------- */
/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
/*
* This function is called in the following situations:
* - in response to a 3-byte ZEBRA_INTERFACE_ADD request
* from the client.
* - at startup, when zebra figures out the available interfaces
* - when an interface is added (where support for
* RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
* an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
* received)
*/
int zsend_interface_add(struct zserv *client, struct interface *ifp)
{
struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, ZEBRA_INTERFACE_ADD, ifp->vrf_id);
zserv_encode_interface(s, ifp);
client->ifadd_cnt++;
return zserv_send_message(client, s);
}
/* Interface deletion from zebra daemon. */
int zsend_interface_delete(struct zserv *client, struct interface *ifp)
{
struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, ZEBRA_INTERFACE_DELETE, ifp->vrf_id);
zserv_encode_interface(s, ifp);
client->ifdel_cnt++;
return zserv_send_message(client, s);
}
int zsend_vrf_add(struct zserv *client, struct zebra_vrf *zvrf)
{
struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, ZEBRA_VRF_ADD, zvrf_id(zvrf));
zserv_encode_vrf(s, zvrf);
client->vrfadd_cnt++;
return zserv_send_message(client, s);
}
/* VRF deletion from zebra daemon. */
int zsend_vrf_delete(struct zserv *client, struct zebra_vrf *zvrf)
{
struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, ZEBRA_VRF_DELETE, zvrf_id(zvrf));
zserv_encode_vrf(s, zvrf);
client->vrfdel_cnt++;
return zserv_send_message(client, s);
}
int zsend_interface_link_params(struct zserv *client, struct interface *ifp)
{
struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
if (!ifp->link_params) {
stream_free(s);
return 0;
}
zclient_create_header(s, ZEBRA_INTERFACE_LINK_PARAMS, ifp->vrf_id);
/* Add Interface Index */
stream_putl(s, ifp->ifindex);
/* Then TE Link Parameters */
if (zebra_interface_link_params_write(s, ifp) == 0) {
stream_free(s);
return 0;
}
/* Write packet size. */
stream_putw_at(s, 0, stream_get_endp(s));
return zserv_send_message(client, s);
}
/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
* ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
*
* A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
* - in response to a 3-byte ZEBRA_INTERFACE_ADD request
* from the client, after the ZEBRA_INTERFACE_ADD has been
* sent from zebra to the client
* - redistribute new address info to all clients in the following situations
* - at startup, when zebra figures out the available interfaces
* - when an interface is added (where support for
* RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
* an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
* received)
* - for the vty commands "ip address A.B.C.D/M [<label LINE>]"
* and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
* - when an RTM_NEWADDR message is received from the kernel,
*
* The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
*
* zsend_interface_address(DELETE)
* ^
* |
* zebra_interface_address_delete_update
* ^ ^ ^
* | | if_delete_update
* | |
* ip_address_uninstall connected_delete_ipv4
* [ipv6_addresss_uninstall] [connected_delete_ipv6]
* ^ ^
* | |
* | RTM_NEWADDR on routing/netlink socket
* |
* vty commands:
* "no ip address A.B.C.D/M [label LINE]"
* "no ip address A.B.C.D/M"
* ["no ipv6 address X:X::X:X/M"]
*
*/
int zsend_interface_address(int cmd, struct zserv *client,
struct interface *ifp, struct connected *ifc)
{
int blen;
struct prefix *p;
struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, cmd, ifp->vrf_id);
stream_putl(s, ifp->ifindex);
/* Interface address flag. */
stream_putc(s, ifc->flags);
/* Prefix information. */
p = ifc->address;
stream_putc(s, p->family);
blen = prefix_blen(p);
stream_put(s, &p->u.prefix, blen);
/*
* XXX gnu version does not send prefixlen for
* ZEBRA_INTERFACE_ADDRESS_DELETE
* but zebra_interface_address_delete_read() in the gnu version
* expects to find it
*/
stream_putc(s, p->prefixlen);
/* Destination. */
p = ifc->destination;
if (p)
stream_put(s, &p->u.prefix, blen);
else
stream_put(s, NULL, blen);
/* Write packet size. */
stream_putw_at(s, 0, stream_get_endp(s));
client->connected_rt_add_cnt++;
return zserv_send_message(client, s);
}
static int zsend_interface_nbr_address(int cmd, struct zserv *client,
struct interface *ifp,
struct nbr_connected *ifc)
{
int blen;
struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
struct prefix *p;
zclient_create_header(s, cmd, ifp->vrf_id);
stream_putl(s, ifp->ifindex);
/* Prefix information. */
p = ifc->address;
stream_putc(s, p->family);
blen = prefix_blen(p);
stream_put(s, &p->u.prefix, blen);
/*
* XXX gnu version does not send prefixlen for
* ZEBRA_INTERFACE_ADDRESS_DELETE
* but zebra_interface_address_delete_read() in the gnu version
* expects to find it
*/
stream_putc(s, p->prefixlen);
/* Write packet size. */
stream_putw_at(s, 0, stream_get_endp(s));
return zserv_send_message(client, s);
}
/* Interface address addition. */
static void zebra_interface_nbr_address_add_update(struct interface *ifp,
struct nbr_connected *ifc)
{
struct listnode *node, *nnode;
struct zserv *client;
struct prefix *p;
if (IS_ZEBRA_DEBUG_EVENT) {
char buf[INET6_ADDRSTRLEN];
p = ifc->address;
zlog_debug(
"MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
inet_ntop(p->family, &p->u.prefix, buf,
INET6_ADDRSTRLEN),
p->prefixlen, ifc->ifp->name);
}
for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client))
zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
client, ifp, ifc);
}
/* Interface address deletion. */
static void zebra_interface_nbr_address_delete_update(struct interface *ifp,
struct nbr_connected *ifc)
{
struct listnode *node, *nnode;
struct zserv *client;
struct prefix *p;
if (IS_ZEBRA_DEBUG_EVENT) {
char buf[INET6_ADDRSTRLEN];
p = ifc->address;
zlog_debug(
"MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
inet_ntop(p->family, &p->u.prefix, buf,
INET6_ADDRSTRLEN),
p->prefixlen, ifc->ifp->name);
}
for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client))
zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_DELETE,
client, ifp, ifc);
}
/* Send addresses on interface to client */
int zsend_interface_addresses(struct zserv *client, struct interface *ifp)
{
struct listnode *cnode, *cnnode;
struct connected *c;
struct nbr_connected *nc;
/* Send interface addresses. */
for (ALL_LIST_ELEMENTS(ifp->connected, cnode, cnnode, c)) {
if (!CHECK_FLAG(c->conf, ZEBRA_IFC_REAL))
continue;
if (zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD, client,
ifp, c)
< 0)
return -1;
}
/* Send interface neighbors. */
for (ALL_LIST_ELEMENTS(ifp->nbr_connected, cnode, cnnode, nc)) {
if (zsend_interface_nbr_address(ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
client, ifp, nc)
< 0)
return -1;
}
return 0;
}
/* Notify client about interface moving from one VRF to another.
* Whether client is interested in old and new VRF is checked by caller.
*/
int zsend_interface_vrf_update(struct zserv *client, struct interface *ifp,
vrf_id_t vrf_id)
{
struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, ZEBRA_INTERFACE_VRF_UPDATE, ifp->vrf_id);
/* Fill in the name of the interface and its new VRF (id) */
stream_put(s, ifp->name, INTERFACE_NAMSIZ);
stream_putl(s, vrf_id);
/* Write packet size. */
stream_putw_at(s, 0, stream_get_endp(s));
client->if_vrfchg_cnt++;
return zserv_send_message(client, s);
}
/* Add new nbr connected IPv6 address */
void nbr_connected_add_ipv6(struct interface *ifp, struct in6_addr *address)
{
struct nbr_connected *ifc;
struct prefix p;
p.family = AF_INET6;
IPV6_ADDR_COPY(&p.u.prefix6, address);
p.prefixlen = IPV6_MAX_PREFIXLEN;
ifc = listnode_head(ifp->nbr_connected);
if (!ifc) {
/* new addition */
ifc = nbr_connected_new();
ifc->address = prefix_new();
ifc->ifp = ifp;
listnode_add(ifp->nbr_connected, ifc);
}
prefix_copy(ifc->address, &p);
zebra_interface_nbr_address_add_update(ifp, ifc);
if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 1);
}
void nbr_connected_delete_ipv6(struct interface *ifp, struct in6_addr *address)
{
struct nbr_connected *ifc;
struct prefix p;
p.family = AF_INET6;
IPV6_ADDR_COPY(&p.u.prefix6, address);
p.prefixlen = IPV6_MAX_PREFIXLEN;
ifc = nbr_connected_check(ifp, &p);
if (!ifc)
return;
listnode_delete(ifp->nbr_connected, ifc);
zebra_interface_nbr_address_delete_update(ifp, ifc);
if_nbr_ipv6ll_to_ipv4ll_neigh_update(ifp, address, 0);
nbr_connected_free(ifc);
}
/*
* The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
* ZEBRA_INTERFACE_DOWN.
*
* The ZEBRA_INTERFACE_UP message is sent from the zebra server to
* the clients in one of 2 situations:
* - an if_up is detected e.g., as a result of an RTM_IFINFO message
* - a vty command modifying the bandwidth of an interface is received.
* The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
*/
int zsend_interface_update(int cmd, struct zserv *client, struct interface *ifp)
{
struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, cmd, ifp->vrf_id);
zserv_encode_interface(s, ifp);
if (cmd == ZEBRA_INTERFACE_UP)
client->ifup_cnt++;
else
client->ifdown_cnt++;
return zserv_send_message(client, s);
}
int zsend_redistribute_route(int cmd, struct zserv *client,
const struct prefix *p,
const struct prefix *src_p,
const struct route_entry *re)
{
struct zapi_route api;
struct zapi_nexthop *api_nh;
struct nexthop *nexthop;
uint8_t count = 0;
afi_t afi;
size_t stream_size =
MAX(ZEBRA_MAX_PACKET_SIZ, sizeof(struct zapi_route));
memset(&api, 0, sizeof(api));
api.vrf_id = re->vrf_id;
api.type = re->type;
api.safi = SAFI_UNICAST;
api.instance = re->instance;
api.flags = re->flags;
afi = family2afi(p->family);
switch (afi) {
case AFI_IP:
if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
client->redist_v4_add_cnt++;
else
client->redist_v4_del_cnt++;
break;
case AFI_IP6:
if (cmd == ZEBRA_REDISTRIBUTE_ROUTE_ADD)
client->redist_v6_add_cnt++;
else
client->redist_v6_del_cnt++;
break;
default:
break;
}
/* Prefix. */
api.prefix = *p;
if (src_p) {
SET_FLAG(api.message, ZAPI_MESSAGE_SRCPFX);
memcpy(&api.src_prefix, src_p, sizeof(api.src_prefix));
}
for (nexthop = re->nhe->nhg->nexthop;
nexthop; nexthop = nexthop->next) {
if (!CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
continue;
api_nh = &api.nexthops[count];
api_nh->vrf_id = nexthop->vrf_id;
api_nh->type = nexthop->type;
switch (nexthop->type) {
case NEXTHOP_TYPE_BLACKHOLE:
api_nh->bh_type = nexthop->bh_type;
break;
case NEXTHOP_TYPE_IPV4:
api_nh->gate.ipv4 = nexthop->gate.ipv4;
break;
case NEXTHOP_TYPE_IPV4_IFINDEX:
api_nh->gate.ipv4 = nexthop->gate.ipv4;
api_nh->ifindex = nexthop->ifindex;
break;
case NEXTHOP_TYPE_IFINDEX:
api_nh->ifindex = nexthop->ifindex;
break;
case NEXTHOP_TYPE_IPV6:
api_nh->gate.ipv6 = nexthop->gate.ipv6;
break;
case NEXTHOP_TYPE_IPV6_IFINDEX:
api_nh->gate.ipv6 = nexthop->gate.ipv6;
api_nh->ifindex = nexthop->ifindex;
}
count++;
}
/* Nexthops. */
if (count) {
SET_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP);
api.nexthop_num = count;
}
/* Attributes. */
SET_FLAG(api.message, ZAPI_MESSAGE_DISTANCE);
api.distance = re->distance;
SET_FLAG(api.message, ZAPI_MESSAGE_METRIC);
api.metric = re->metric;
if (re->tag) {
SET_FLAG(api.message, ZAPI_MESSAGE_TAG);
api.tag = re->tag;
}
SET_FLAG(api.message, ZAPI_MESSAGE_MTU);
api.mtu = re->mtu;
struct stream *s = stream_new(stream_size);
/* Encode route and send. */
if (zapi_route_encode(cmd, s, &api) < 0) {
stream_free(s);
return -1;
}
if (IS_ZEBRA_DEBUG_SEND) {
char buf_prefix[PREFIX_STRLEN];
prefix2str(&api.prefix, buf_prefix, sizeof(buf_prefix));
zlog_debug("%s: %s to client %s: type %s, vrf_id %d, p %s",
__func__, zserv_command_string(cmd),
zebra_route_string(client->proto),
zebra_route_string(api.type), api.vrf_id,
buf_prefix);
}
return zserv_send_message(client, s);
}
/*
* Modified version of zsend_ipv4_nexthop_lookup(): Query unicast rib if
* nexthop is not found on mrib. Returns both route metric and protocol
* distance.
*/
static int zsend_ipv4_nexthop_lookup_mrib(struct zserv *client,
struct in_addr addr,
struct route_entry *re,
struct zebra_vrf *zvrf)
{
struct stream *s;
unsigned long nump;
uint8_t num;
struct nexthop *nexthop;
/* Get output stream. */
s = stream_new(ZEBRA_MAX_PACKET_SIZ);
stream_reset(s);
/* Fill in result. */
zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, zvrf_id(zvrf));
stream_put_in_addr(s, &addr);
if (re) {
stream_putc(s, re->distance);
stream_putl(s, re->metric);
num = 0;
/* remember position for nexthop_num */
nump = stream_get_endp(s);
/* reserve room for nexthop_num */
stream_putc(s, 0);
/*
* Only non-recursive routes are elegible to resolve the
* nexthop we are looking up. Therefore, we will just iterate
* over the top chain of nexthops.
*/
for (nexthop = re->nhe->nhg->nexthop; nexthop;
nexthop = nexthop->next)
if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
num += zserv_encode_nexthop(s, nexthop);
/* store nexthop_num */
stream_putc_at(s, nump, num);
} else {
stream_putc(s, 0); /* distance */
stream_putl(s, 0); /* metric */
stream_putc(s, 0); /* nexthop_num */
}
stream_putw_at(s, 0, stream_get_endp(s));
return zserv_send_message(client, s);
}
/*
* Common utility send route notification, called from a path using a
* route_entry and from a path using a dataplane context.
*/
static int route_notify_internal(const struct prefix *p, int type,
uint16_t instance, vrf_id_t vrf_id,
uint32_t table_id,
enum zapi_route_notify_owner note)
{
struct zserv *client;
struct stream *s;
uint8_t blen;
client = zserv_find_client(type, instance);
if (!client || !client->notify_owner) {
if (IS_ZEBRA_DEBUG_PACKET) {
char buff[PREFIX_STRLEN];
zlog_debug(
"Not Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
type, prefix2str(p, buff, sizeof(buff)),
table_id, note, vrf_id);
}
return 0;
}
if (IS_ZEBRA_DEBUG_PACKET) {
char buff[PREFIX_STRLEN];
zlog_debug("Notifying Owner: %u about prefix %s(%u) %d vrf: %u",
type, prefix2str(p, buff, sizeof(buff)),
table_id, note, vrf_id);
}
s = stream_new(ZEBRA_MAX_PACKET_SIZ);
stream_reset(s);
zclient_create_header(s, ZEBRA_ROUTE_NOTIFY_OWNER, vrf_id);
stream_put(s, ¬e, sizeof(note));
stream_putc(s, p->family);
blen = prefix_blen(p);
stream_putc(s, p->prefixlen);
stream_put(s, &p->u.prefix, blen);
stream_putl(s, table_id);
stream_putw_at(s, 0, stream_get_endp(s));
return zserv_send_message(client, s);
}
int zsend_route_notify_owner(struct route_entry *re, const struct prefix *p,
enum zapi_route_notify_owner note)
{
return (route_notify_internal(p, re->type, re->instance, re->vrf_id,
re->table, note));
}
/*
* Route-owner notification using info from dataplane update context.
*/
int zsend_route_notify_owner_ctx(const struct zebra_dplane_ctx *ctx,
enum zapi_route_notify_owner note)
{
return (route_notify_internal(dplane_ctx_get_dest(ctx),
dplane_ctx_get_type(ctx),
dplane_ctx_get_instance(ctx),
dplane_ctx_get_vrf(ctx),
dplane_ctx_get_table(ctx),
note));
}
void zsend_rule_notify_owner(struct zebra_pbr_rule *rule,
enum zapi_rule_notify_owner note)
{
struct listnode *node;
struct zserv *client;
struct stream *s;
if (IS_ZEBRA_DEBUG_PACKET)
zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__,
rule->rule.unique);
for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
if (rule->sock == client->sock)
break;
}
if (!client)
return;
s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, ZEBRA_RULE_NOTIFY_OWNER, VRF_DEFAULT);
stream_put(s, ¬e, sizeof(note));
stream_putl(s, rule->rule.seq);
stream_putl(s, rule->rule.priority);
stream_putl(s, rule->rule.unique);
stream_putl(s, rule->rule.ifindex);
stream_putw_at(s, 0, stream_get_endp(s));
zserv_send_message(client, s);
}
void zsend_ipset_notify_owner(struct zebra_pbr_ipset *ipset,
enum zapi_ipset_notify_owner note)
{
struct listnode *node;
struct zserv *client;
struct stream *s;
if (IS_ZEBRA_DEBUG_PACKET)
zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__,
ipset->unique);
for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
if (ipset->sock == client->sock)
break;
}
if (!client)
return;
s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, ZEBRA_IPSET_NOTIFY_OWNER, VRF_DEFAULT);
stream_put(s, ¬e, sizeof(note));
stream_putl(s, ipset->unique);
stream_put(s, ipset->ipset_name, ZEBRA_IPSET_NAME_SIZE);
stream_putw_at(s, 0, stream_get_endp(s));
zserv_send_message(client, s);
}
void zsend_ipset_entry_notify_owner(struct zebra_pbr_ipset_entry *ipset,
enum zapi_ipset_entry_notify_owner note)
{
struct listnode *node;
struct zserv *client;
struct stream *s;
if (IS_ZEBRA_DEBUG_PACKET)
zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__,
ipset->unique);
for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
if (ipset->sock == client->sock)
break;
}
if (!client)
return;
s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, ZEBRA_IPSET_ENTRY_NOTIFY_OWNER, VRF_DEFAULT);
stream_put(s, ¬e, sizeof(note));
stream_putl(s, ipset->unique);
stream_put(s, ipset->backpointer->ipset_name, ZEBRA_IPSET_NAME_SIZE);
stream_putw_at(s, 0, stream_get_endp(s));
zserv_send_message(client, s);
}
void zsend_iptable_notify_owner(struct zebra_pbr_iptable *iptable,
enum zapi_iptable_notify_owner note)
{
struct listnode *node;
struct zserv *client;
struct stream *s;
if (IS_ZEBRA_DEBUG_PACKET)
zlog_debug("%s: Notifying %u", __PRETTY_FUNCTION__,
iptable->unique);
for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client)) {
if (iptable->sock == client->sock)
break;
}
if (!client)
return;
s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, ZEBRA_IPTABLE_NOTIFY_OWNER, VRF_DEFAULT);
stream_put(s, ¬e, sizeof(note));
stream_putl(s, iptable->unique);
stream_putw_at(s, 0, stream_get_endp(s));
zserv_send_message(client, s);
}
/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
int zsend_router_id_update(struct zserv *client, struct prefix *p,
vrf_id_t vrf_id)
{
int blen;
/* Check this client need interface information. */
if (!vrf_bitmap_check(client->ridinfo, vrf_id))
return 0;
struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
/* Message type. */
zclient_create_header(s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
/* Prefix information. */
stream_putc(s, p->family);
blen = prefix_blen(p);
stream_put(s, &p->u.prefix, blen);
stream_putc(s, p->prefixlen);
/* Write packet size. */
stream_putw_at(s, 0, stream_get_endp(s));
return zserv_send_message(client, s);
}
/*
* Function used by Zebra to send a PW status update to LDP daemon
*/
int zsend_pw_update(struct zserv *client, struct zebra_pw *pw)
{
struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, ZEBRA_PW_STATUS_UPDATE, pw->vrf_id);
stream_write(s, pw->ifname, IF_NAMESIZE);
stream_putl(s, pw->ifindex);
stream_putl(s, pw->status);
/* Put length at the first point of the stream. */
stream_putw_at(s, 0, stream_get_endp(s));
return zserv_send_message(client, s);
}
/* Send response to a get label chunk request to client */
int zsend_assign_label_chunk_response(struct zserv *client, vrf_id_t vrf_id,
uint8_t proto, uint16_t instance,
struct label_manager_chunk *lmc)
{
int ret;
struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, vrf_id);
/* proto */
stream_putc(s, proto);
/* instance */
stream_putw(s, instance);
if (lmc) {
/* keep */
stream_putc(s, lmc->keep);
/* start and end labels */
stream_putl(s, lmc->start);
stream_putl(s, lmc->end);
}
/* Write packet size. */
stream_putw_at(s, 0, stream_get_endp(s));
ret = writen(client->sock, s->data, stream_get_endp(s));
stream_free(s);
return ret;
}
/* Send response to a label manager connect request to client */
int zsend_label_manager_connect_response(struct zserv *client, vrf_id_t vrf_id,
unsigned short result)
{
int ret;
struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, vrf_id);
/* proto */
stream_putc(s, client->proto);
/* instance */
stream_putw(s, client->instance);
/* result */
stream_putc(s, result);
/* Write packet size. */
stream_putw_at(s, 0, stream_get_endp(s));
ret = writen(client->sock, s->data, stream_get_endp(s));
stream_free(s);
return ret;
}
/* Send response to a get table chunk request to client */
static int zsend_assign_table_chunk_response(struct zserv *client,
vrf_id_t vrf_id,
struct table_manager_chunk *tmc)
{
struct stream *s = stream_new(ZEBRA_MAX_PACKET_SIZ);
zclient_create_header(s, ZEBRA_GET_TABLE_CHUNK, vrf_id);
if (tmc) {
/* start and end labels */
stream_putl(s, tmc->start);
stream_putl(s, tmc->end);
}