1 /* Zebra's client header.
2  * Copyright (C) 1999 Kunihiro Ishiguro
3  *
4  * This file is part of GNU Zebra.
5  *
6  * GNU Zebra is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * GNU Zebra is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; see the file COPYING; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef _ZEBRA_ZCLIENT_H
22 #define _ZEBRA_ZCLIENT_H
23 
24 /* For struct zapi_route. */
25 #include "prefix.h"
26 
27 /* For struct interface and struct connected. */
28 #include "if.h"
29 
30 /* For vrf_bitmap_t. */
31 #include "vrf.h"
32 
33 /* For union g_addr */
34 #include "nexthop.h"
35 
36 /* For union pw_protocol_fields */
37 #include "pw.h"
38 
39 #include "mlag.h"
40 #include "srte.h"
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 /* Zebra types. Used in Zserv message header. */
47 typedef uint16_t zebra_size_t;
48 
49 /* Marker value used in new Zserv, in the byte location corresponding
50  * the command value in the old zserv header. To allow old and new
51  * Zserv headers to be distinguished from each other.
52  */
53 #define ZEBRA_HEADER_MARKER              254
54 
55 /* For input/output buffer to zebra. */
56 #define ZEBRA_MAX_PACKET_SIZ          16384U
57 
58 /* Zebra header size. */
59 #define ZEBRA_HEADER_SIZE             10
60 
61 /* special socket path name to use TCP
62  * @ is used as first character because that's abstract socket names on Linux
63  */
64 #define ZAPI_TCP_PATHNAME             "@tcp"
65 
66 /* IPset size name stands for the name of the ipset entry
67  * that can be created by using some zapi interfaces
68  */
69 #define ZEBRA_IPSET_NAME_SIZE   32
70 
71 /* IPTable action is defined by two values: either
72  * forward or drop
73  */
74 #define ZEBRA_IPTABLES_FORWARD 0
75 #define ZEBRA_IPTABLES_DROP    1
76 
77 /* Zebra FEC register command flags. */
78 #define ZEBRA_FEC_REGISTER_LABEL          0x1
79 #define ZEBRA_FEC_REGISTER_LABEL_INDEX    0x2
80 
81 /* Client capabilities */
82 enum zserv_client_capabilities {
83 	ZEBRA_CLIENT_GR_CAPABILITIES = 1,
84 	ZEBRA_CLIENT_ROUTE_UPDATE_COMPLETE = 2,
85 	ZEBRA_CLIENT_ROUTE_UPDATE_PENDING = 3,
86 	ZEBRA_CLIENT_GR_DISABLE = 4,
87 	ZEBRA_CLIENT_RIB_STALE_TIME
88 };
89 
90 /* Macro to check if there GR enabled. */
91 #define ZEBRA_CLIENT_GR_ENABLED(X) (X == ZEBRA_CLIENT_GR_CAPABILITIES)
92 
93 #define ZEBRA_SR_POLICY_NAME_MAX_LENGTH 100
94 
95 extern struct sockaddr_storage zclient_addr;
96 extern socklen_t zclient_addr_len;
97 
98 /* Zebra message types. */
99 typedef enum {
100 	ZEBRA_INTERFACE_ADD,
101 	ZEBRA_INTERFACE_DELETE,
102 	ZEBRA_INTERFACE_ADDRESS_ADD,
103 	ZEBRA_INTERFACE_ADDRESS_DELETE,
104 	ZEBRA_INTERFACE_UP,
105 	ZEBRA_INTERFACE_DOWN,
106 	ZEBRA_INTERFACE_SET_MASTER,
107 	ZEBRA_INTERFACE_SET_PROTODOWN,
108 	ZEBRA_ROUTE_ADD,
109 	ZEBRA_ROUTE_DELETE,
110 	ZEBRA_ROUTE_NOTIFY_OWNER,
111 	ZEBRA_REDISTRIBUTE_ADD,
112 	ZEBRA_REDISTRIBUTE_DELETE,
113 	ZEBRA_REDISTRIBUTE_DEFAULT_ADD,
114 	ZEBRA_REDISTRIBUTE_DEFAULT_DELETE,
115 	ZEBRA_ROUTER_ID_ADD,
116 	ZEBRA_ROUTER_ID_DELETE,
117 	ZEBRA_ROUTER_ID_UPDATE,
118 	ZEBRA_HELLO,
119 	ZEBRA_CAPABILITIES,
120 	ZEBRA_NEXTHOP_REGISTER,
121 	ZEBRA_NEXTHOP_UNREGISTER,
122 	ZEBRA_NEXTHOP_UPDATE,
123 	ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
124 	ZEBRA_INTERFACE_NBR_ADDRESS_DELETE,
125 	ZEBRA_INTERFACE_BFD_DEST_UPDATE,
126 	ZEBRA_IMPORT_ROUTE_REGISTER,
127 	ZEBRA_IMPORT_ROUTE_UNREGISTER,
128 	ZEBRA_IMPORT_CHECK_UPDATE,
129 	ZEBRA_BFD_DEST_REGISTER,
130 	ZEBRA_BFD_DEST_DEREGISTER,
131 	ZEBRA_BFD_DEST_UPDATE,
132 	ZEBRA_BFD_DEST_REPLAY,
133 	ZEBRA_REDISTRIBUTE_ROUTE_ADD,
134 	ZEBRA_REDISTRIBUTE_ROUTE_DEL,
135 	ZEBRA_VRF_UNREGISTER,
136 	ZEBRA_VRF_ADD,
137 	ZEBRA_VRF_DELETE,
138 	ZEBRA_VRF_LABEL,
139 	ZEBRA_INTERFACE_VRF_UPDATE,
140 	ZEBRA_BFD_CLIENT_REGISTER,
141 	ZEBRA_BFD_CLIENT_DEREGISTER,
142 	ZEBRA_INTERFACE_ENABLE_RADV,
143 	ZEBRA_INTERFACE_DISABLE_RADV,
144 	ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB,
145 	ZEBRA_INTERFACE_LINK_PARAMS,
146 	ZEBRA_MPLS_LABELS_ADD,
147 	ZEBRA_MPLS_LABELS_DELETE,
148 	ZEBRA_MPLS_LABELS_REPLACE,
149 	ZEBRA_SR_POLICY_SET,
150 	ZEBRA_SR_POLICY_DELETE,
151 	ZEBRA_SR_POLICY_NOTIFY_STATUS,
152 	ZEBRA_IPMR_ROUTE_STATS,
153 	ZEBRA_LABEL_MANAGER_CONNECT,
154 	ZEBRA_LABEL_MANAGER_CONNECT_ASYNC,
155 	ZEBRA_GET_LABEL_CHUNK,
156 	ZEBRA_RELEASE_LABEL_CHUNK,
157 	ZEBRA_FEC_REGISTER,
158 	ZEBRA_FEC_UNREGISTER,
159 	ZEBRA_FEC_UPDATE,
160 	ZEBRA_ADVERTISE_DEFAULT_GW,
161 	ZEBRA_ADVERTISE_SVI_MACIP,
162 	ZEBRA_ADVERTISE_SUBNET,
163 	ZEBRA_ADVERTISE_ALL_VNI,
164 	ZEBRA_LOCAL_ES_ADD,
165 	ZEBRA_LOCAL_ES_DEL,
166 	ZEBRA_REMOTE_ES_VTEP_ADD,
167 	ZEBRA_REMOTE_ES_VTEP_DEL,
168 	ZEBRA_LOCAL_ES_EVI_ADD,
169 	ZEBRA_LOCAL_ES_EVI_DEL,
170 	ZEBRA_VNI_ADD,
171 	ZEBRA_VNI_DEL,
172 	ZEBRA_L3VNI_ADD,
173 	ZEBRA_L3VNI_DEL,
174 	ZEBRA_REMOTE_VTEP_ADD,
175 	ZEBRA_REMOTE_VTEP_DEL,
176 	ZEBRA_MACIP_ADD,
177 	ZEBRA_MACIP_DEL,
178 	ZEBRA_IP_PREFIX_ROUTE_ADD,
179 	ZEBRA_IP_PREFIX_ROUTE_DEL,
180 	ZEBRA_REMOTE_MACIP_ADD,
181 	ZEBRA_REMOTE_MACIP_DEL,
182 	ZEBRA_DUPLICATE_ADDR_DETECTION,
183 	ZEBRA_PW_ADD,
184 	ZEBRA_PW_DELETE,
185 	ZEBRA_PW_SET,
186 	ZEBRA_PW_UNSET,
187 	ZEBRA_PW_STATUS_UPDATE,
188 	ZEBRA_RULE_ADD,
189 	ZEBRA_RULE_DELETE,
190 	ZEBRA_RULE_NOTIFY_OWNER,
191 	ZEBRA_TABLE_MANAGER_CONNECT,
192 	ZEBRA_GET_TABLE_CHUNK,
193 	ZEBRA_RELEASE_TABLE_CHUNK,
194 	ZEBRA_IPSET_CREATE,
195 	ZEBRA_IPSET_DESTROY,
196 	ZEBRA_IPSET_ENTRY_ADD,
197 	ZEBRA_IPSET_ENTRY_DELETE,
198 	ZEBRA_IPSET_NOTIFY_OWNER,
199 	ZEBRA_IPSET_ENTRY_NOTIFY_OWNER,
200 	ZEBRA_IPTABLE_ADD,
201 	ZEBRA_IPTABLE_DELETE,
202 	ZEBRA_IPTABLE_NOTIFY_OWNER,
203 	ZEBRA_VXLAN_FLOOD_CONTROL,
204 	ZEBRA_VXLAN_SG_ADD,
205 	ZEBRA_VXLAN_SG_DEL,
206 	ZEBRA_VXLAN_SG_REPLAY,
207 	ZEBRA_MLAG_PROCESS_UP,
208 	ZEBRA_MLAG_PROCESS_DOWN,
209 	ZEBRA_MLAG_CLIENT_REGISTER,
210 	ZEBRA_MLAG_CLIENT_UNREGISTER,
211 	ZEBRA_MLAG_FORWARD_MSG,
212 	ZEBRA_ERROR,
213 	ZEBRA_CLIENT_CAPABILITIES,
214 	ZEBRA_OPAQUE_MESSAGE,
215 	ZEBRA_OPAQUE_REGISTER,
216 	ZEBRA_OPAQUE_UNREGISTER,
217 	ZEBRA_NEIGH_DISCOVER,
218 } zebra_message_types_t;
219 
220 enum zebra_error_types {
221 	ZEBRA_UNKNOWN_ERROR,    /* Error of unknown type */
222 	ZEBRA_NO_VRF,		/* Vrf in header was not found */
223 	ZEBRA_INVALID_MSG_TYPE, /* No handler found for msg type */
224 };
225 
zebra_error_type2str(enum zebra_error_types type)226 static inline const char *zebra_error_type2str(enum zebra_error_types type)
227 {
228 	const char *ret = "UNKNOWN";
229 
230 	switch (type) {
231 	case ZEBRA_UNKNOWN_ERROR:
232 		ret = "ZEBRA_UNKNOWN_ERROR";
233 		break;
234 	case ZEBRA_NO_VRF:
235 		ret = "ZEBRA_NO_VRF";
236 		break;
237 	case ZEBRA_INVALID_MSG_TYPE:
238 		ret = "ZEBRA_INVALID_MSG_TYPE";
239 		break;
240 	}
241 
242 	return ret;
243 }
244 
245 struct redist_proto {
246 	uint8_t enabled;
247 	struct list *instances;
248 };
249 
250 struct zclient_capabilities {
251 	uint32_t ecmp;
252 	bool mpls_enabled;
253 	enum mlag_role role;
254 };
255 
256 /* Graceful Restart Capabilities message */
257 struct zapi_cap {
258 	enum zserv_client_capabilities cap;
259 	uint32_t stale_removal_time;
260 	afi_t afi;
261 	safi_t safi;
262 	vrf_id_t vrf_id;
263 };
264 
265 /* Structure for the zebra client. */
266 struct zclient {
267 	/* The thread master we schedule ourselves on */
268 	struct thread_master *master;
269 
270 	/* Priviledges to change socket values */
271 	struct zebra_privs_t *privs;
272 
273 	/* Do we care about failure events for route install? */
274 	bool receive_notify;
275 
276 	/* Is this a synchronous client? */
277 	bool synchronous;
278 
279 	/* Session id (optional) to support clients with multiple sessions */
280 	uint32_t session_id;
281 
282 	/* Socket to zebra daemon. */
283 	int sock;
284 
285 	/* Connection failure count. */
286 	int fail;
287 
288 	/* Input buffer for zebra message. */
289 	struct stream *ibuf;
290 
291 	/* Output buffer for zebra message. */
292 	struct stream *obuf;
293 
294 	/* Buffer of data waiting to be written to zebra. */
295 	struct buffer *wb;
296 
297 	/* Read and connect thread. */
298 	struct thread *t_read;
299 	struct thread *t_connect;
300 
301 	/* Thread to write buffered data to zebra. */
302 	struct thread *t_write;
303 
304 	/* Redistribute information. */
305 	uint8_t redist_default; /* clients protocol */
306 	unsigned short instance;
307 	struct redist_proto mi_redist[AFI_MAX][ZEBRA_ROUTE_MAX];
308 	vrf_bitmap_t redist[AFI_MAX][ZEBRA_ROUTE_MAX];
309 
310 	/* Redistribute defauilt. */
311 	vrf_bitmap_t default_information[AFI_MAX];
312 
313 #define ZAPI_CALLBACK_ARGS                                                     \
314 	int cmd, struct zclient *zclient, uint16_t length, vrf_id_t vrf_id
315 
316 	/* Pointer to the callback functions. */
317 	void (*zebra_connected)(struct zclient *);
318 	void (*zebra_capabilities)(struct zclient_capabilities *cap);
319 	int (*router_id_update)(ZAPI_CALLBACK_ARGS);
320 	int (*interface_address_add)(ZAPI_CALLBACK_ARGS);
321 	int (*interface_address_delete)(ZAPI_CALLBACK_ARGS);
322 	int (*interface_link_params)(ZAPI_CALLBACK_ARGS);
323 	int (*interface_bfd_dest_update)(ZAPI_CALLBACK_ARGS);
324 	int (*interface_nbr_address_add)(ZAPI_CALLBACK_ARGS);
325 	int (*interface_nbr_address_delete)(ZAPI_CALLBACK_ARGS);
326 	int (*interface_vrf_update)(ZAPI_CALLBACK_ARGS);
327 	int (*nexthop_update)(ZAPI_CALLBACK_ARGS);
328 	int (*import_check_update)(ZAPI_CALLBACK_ARGS);
329 	int (*bfd_dest_replay)(ZAPI_CALLBACK_ARGS);
330 	int (*redistribute_route_add)(ZAPI_CALLBACK_ARGS);
331 	int (*redistribute_route_del)(ZAPI_CALLBACK_ARGS);
332 	int (*fec_update)(int, struct zclient *, uint16_t);
333 	int (*local_es_add)(ZAPI_CALLBACK_ARGS);
334 	int (*local_es_del)(ZAPI_CALLBACK_ARGS);
335 	int (*local_es_evi_add)(ZAPI_CALLBACK_ARGS);
336 	int (*local_es_evi_del)(ZAPI_CALLBACK_ARGS);
337 	int (*local_vni_add)(ZAPI_CALLBACK_ARGS);
338 	int (*local_vni_del)(ZAPI_CALLBACK_ARGS);
339 	int (*local_l3vni_add)(ZAPI_CALLBACK_ARGS);
340 	int (*local_l3vni_del)(ZAPI_CALLBACK_ARGS);
341 	void (*local_ip_prefix_add)(ZAPI_CALLBACK_ARGS);
342 	void (*local_ip_prefix_del)(ZAPI_CALLBACK_ARGS);
343 	int (*local_macip_add)(ZAPI_CALLBACK_ARGS);
344 	int (*local_macip_del)(ZAPI_CALLBACK_ARGS);
345 	int (*pw_status_update)(ZAPI_CALLBACK_ARGS);
346 	int (*route_notify_owner)(ZAPI_CALLBACK_ARGS);
347 	int (*rule_notify_owner)(ZAPI_CALLBACK_ARGS);
348 	void (*label_chunk)(ZAPI_CALLBACK_ARGS);
349 	int (*ipset_notify_owner)(ZAPI_CALLBACK_ARGS);
350 	int (*ipset_entry_notify_owner)(ZAPI_CALLBACK_ARGS);
351 	int (*iptable_notify_owner)(ZAPI_CALLBACK_ARGS);
352 	int (*vxlan_sg_add)(ZAPI_CALLBACK_ARGS);
353 	int (*vxlan_sg_del)(ZAPI_CALLBACK_ARGS);
354 	int (*mlag_process_up)(void);
355 	int (*mlag_process_down)(void);
356 	int (*mlag_handle_msg)(struct stream *msg, int len);
357 	int (*handle_error)(enum zebra_error_types error);
358 	int (*opaque_msg_handler)(ZAPI_CALLBACK_ARGS);
359 	int (*opaque_register_handler)(ZAPI_CALLBACK_ARGS);
360 	int (*opaque_unregister_handler)(ZAPI_CALLBACK_ARGS);
361 	int (*sr_policy_notify_status)(ZAPI_CALLBACK_ARGS);
362 };
363 
364 /* Zebra API message flag. */
365 #define ZAPI_MESSAGE_NEXTHOP  0x01
366 #define ZAPI_MESSAGE_DISTANCE 0x02
367 #define ZAPI_MESSAGE_METRIC   0x04
368 #define ZAPI_MESSAGE_TAG      0x08
369 #define ZAPI_MESSAGE_MTU      0x10
370 #define ZAPI_MESSAGE_SRCPFX   0x20
371 /* Backup nexthops are present */
372 #define ZAPI_MESSAGE_BACKUP_NEXTHOPS 0x40
373 
374 /*
375  * This should only be used by a DAEMON that needs to communicate
376  * the table being used is not in the VRF.  You must pass the
377  * default vrf, else this will be ignored.
378  */
379 #define ZAPI_MESSAGE_TABLEID 0x0080
380 #define ZAPI_MESSAGE_SRTE 0x0100
381 
382 #define ZSERV_VERSION 6
383 /* Zserv protocol message header */
384 struct zmsghdr {
385 	uint16_t length;
386 	/* Always set to 255 in new zserv */
387 	uint8_t marker;
388 	uint8_t version;
389 	vrf_id_t vrf_id;
390 	uint16_t command;
391 } __attribute__((packed));
392 #define ZAPI_HEADER_CMD_LOCATION offsetof(struct zmsghdr, command)
393 
394 /*
395  * ZAPI nexthop. Note that these are sorted when associated with ZAPI routes,
396  * and that sorting must be aligned with the sorting of nexthops in
397  * lib/nexthop.c. Any new fields must be accounted for in zapi_nexthop_cmp().
398  */
399 struct zapi_nexthop {
400 	enum nexthop_types_t type;
401 	vrf_id_t vrf_id;
402 	ifindex_t ifindex;
403 	uint8_t flags;
404 	union {
405 		union g_addr gate;
406 		enum blackhole_type bh_type;
407 	};
408 
409 	/* MPLS labels for BGP-LU or Segment Routing */
410 	uint8_t label_num;
411 	mpls_label_t labels[MPLS_MAX_LABELS];
412 
413 	struct ethaddr rmac;
414 
415 	uint32_t weight;
416 
417 	/* Backup nexthops, for IP-FRR, TI-LFA, etc */
418 	uint8_t backup_num;
419 	uint8_t backup_idx[NEXTHOP_MAX_BACKUPS];
420 
421 	/* SR-TE color. */
422 	uint32_t srte_color;
423 };
424 
425 /*
426  * ZAPI nexthop flags values - we're encoding a single octet
427  * initially, so ensure that the on-the-wire encoding continues
428  * to match the number of valid flags.
429  */
430 
431 #define ZAPI_NEXTHOP_FLAG_ONLINK	0x01
432 #define ZAPI_NEXTHOP_FLAG_LABEL		0x02
433 #define ZAPI_NEXTHOP_FLAG_WEIGHT	0x04
434 #define ZAPI_NEXTHOP_FLAG_HAS_BACKUP	0x08 /* Nexthop has a backup */
435 
436 /*
437  * Some of these data structures do not map easily to
438  * a actual data structure size giving different compilers
439  * and systems.  For those data structures we need
440  * to use the smallest available stream_getX/putX functions
441  * to encode/decode.
442  */
443 struct zapi_route {
444 	uint8_t type;
445 	unsigned short instance;
446 
447 	uint32_t flags;
448 /*
449  * Cause Zebra to consider this routes nexthops recursively
450  */
451 #define ZEBRA_FLAG_ALLOW_RECURSION    0x01
452 /*
453  * This is a route that is read in on startup that was left around
454  * from a previous run of FRR
455  */
456 #define ZEBRA_FLAG_SELFROUTE          0x02
457 /*
458  * This flag is used to tell Zebra that the BGP route being passed
459  * down is a IBGP route
460  */
461 #define ZEBRA_FLAG_IBGP               0x04
462 /*
463  * This is a route that has been selected for FIB installation.
464  * This flag is set in zebra and can be passed up to routing daemons
465  */
466 #define ZEBRA_FLAG_SELECTED           0x08
467 /*
468  * This is a route that we are telling Zebra that this route *must*
469  * win and will be installed even over ZEBRA_FLAG_SELECTED
470  */
471 #define ZEBRA_FLAG_FIB_OVERRIDE       0x10
472 /*
473  * This flag tells Zebra that the route is a EVPN route and should
474  * be treated specially
475  */
476 #define ZEBRA_FLAG_EVPN_ROUTE         0x20
477 /*
478  * This flag tells Zebra that it should treat the distance passed
479  * down as an additional discriminator for route selection of the
480  * route entry.  This mainly is used for backup static routes.
481  */
482 #define ZEBRA_FLAG_RR_USE_DISTANCE    0x40
483 
484 	/* The older XXX_MESSAGE flags live here */
485 	uint32_t message;
486 
487 	/*
488 	 * This is an enum but we are going to treat it as a uint8_t
489 	 * for purpose of encoding/decoding
490 	 */
491 	safi_t safi;
492 
493 	struct prefix prefix;
494 	struct prefix_ipv6 src_prefix;
495 
496 	uint16_t nexthop_num;
497 	struct zapi_nexthop nexthops[MULTIPATH_NUM];
498 
499 	/* Support backup routes for IP FRR, TI-LFA, traffic engineering */
500 	uint16_t backup_nexthop_num;
501 	struct zapi_nexthop backup_nexthops[MULTIPATH_NUM];
502 
503 	uint8_t distance;
504 
505 	uint32_t metric;
506 
507 	route_tag_t tag;
508 
509 	uint32_t mtu;
510 
511 	vrf_id_t vrf_id;
512 
513 	uint32_t tableid;
514 
515 	/* SR-TE color (used for nexthop updates only). */
516 	uint32_t srte_color;
517 };
518 
519 struct zapi_labels {
520 	uint8_t message;
521 #define ZAPI_LABELS_FTN           0x01
522 #define ZAPI_LABELS_HAS_BACKUPS   0x02
523 	enum lsp_types_t type;
524 	mpls_label_t local_label;
525 	struct {
526 		struct prefix prefix;
527 		uint8_t type;
528 		unsigned short instance;
529 	} route;
530 
531 	uint16_t nexthop_num;
532 	struct zapi_nexthop nexthops[MULTIPATH_NUM];
533 
534 	/* Backup nexthops, if present */
535 	uint16_t backup_nexthop_num;
536 	struct zapi_nexthop backup_nexthops[MULTIPATH_NUM];
537 };
538 
539 struct zapi_srte_tunnel {
540 	enum lsp_types_t type;
541 	mpls_label_t local_label;
542 	uint8_t label_num;
543 	mpls_label_t labels[MPLS_MAX_LABELS];
544 };
545 
546 struct zapi_sr_policy {
547 	uint32_t color;
548 	struct ipaddr endpoint;
549 	char name[SRTE_POLICY_NAME_MAX_LENGTH];
550 	struct zapi_srte_tunnel segment_list;
551 	int status;
552 };
553 
554 struct zapi_pw {
555 	char ifname[IF_NAMESIZE];
556 	ifindex_t ifindex;
557 	int type;
558 	int af;
559 	union g_addr nexthop;
560 	uint32_t local_label;
561 	uint32_t remote_label;
562 	uint8_t flags;
563 	union pw_protocol_fields data;
564 	uint8_t protocol;
565 };
566 
567 struct zapi_pw_status {
568 	char ifname[IF_NAMESIZE];
569 	ifindex_t ifindex;
570 	uint32_t status;
571 };
572 
573 enum zapi_route_notify_owner {
574 	ZAPI_ROUTE_FAIL_INSTALL,
575 	ZAPI_ROUTE_BETTER_ADMIN_WON,
576 	ZAPI_ROUTE_INSTALLED,
577 	ZAPI_ROUTE_REMOVED,
578 	ZAPI_ROUTE_REMOVE_FAIL,
579 };
580 
581 enum zapi_rule_notify_owner {
582 	ZAPI_RULE_FAIL_INSTALL,
583 	ZAPI_RULE_INSTALLED,
584 	ZAPI_RULE_REMOVED,
585 	ZAPI_RULE_FAIL_REMOVE,
586 };
587 
588 enum ipset_type {
589 	IPSET_NET_NET = 1,
590 	IPSET_NET_PORT_NET,
591 	IPSET_NET_PORT,
592 	IPSET_NET
593 };
594 
595 enum zapi_ipset_notify_owner {
596 	ZAPI_IPSET_FAIL_INSTALL,
597 	ZAPI_IPSET_INSTALLED,
598 	ZAPI_IPSET_REMOVED,
599 	ZAPI_IPSET_FAIL_REMOVE,
600 };
601 
602 enum zapi_ipset_entry_notify_owner {
603 	ZAPI_IPSET_ENTRY_FAIL_INSTALL,
604 	ZAPI_IPSET_ENTRY_INSTALLED,
605 	ZAPI_IPSET_ENTRY_REMOVED,
606 	ZAPI_IPSET_ENTRY_FAIL_REMOVE,
607 };
608 
609 enum zapi_iptable_notify_owner {
610 	ZAPI_IPTABLE_FAIL_INSTALL,
611 	ZAPI_IPTABLE_INSTALLED,
612 	ZAPI_IPTABLE_REMOVED,
613 	ZAPI_IPTABLE_FAIL_REMOVE,
614 };
615 
616 static inline const char *
zapi_rule_notify_owner2str(enum zapi_rule_notify_owner note)617 zapi_rule_notify_owner2str(enum zapi_rule_notify_owner note)
618 {
619 	const char *ret = "UNKNOWN";
620 
621 	switch (note) {
622 	case ZAPI_RULE_FAIL_INSTALL:
623 		ret = "ZAPI_RULE_FAIL_INSTALL";
624 		break;
625 	case ZAPI_RULE_INSTALLED:
626 		ret = "ZAPI_RULE_INSTALLED";
627 		break;
628 	case ZAPI_RULE_FAIL_REMOVE:
629 		ret = "ZAPI_RULE_FAIL_REMOVE";
630 		break;
631 	case ZAPI_RULE_REMOVED:
632 		ret = "ZAPI_RULE_REMOVED";
633 		break;
634 	}
635 
636 	return ret;
637 }
638 
639 /* Zebra MAC types */
640 #define ZEBRA_MACIP_TYPE_STICKY                0x01 /* Sticky MAC*/
641 #define ZEBRA_MACIP_TYPE_GW                    0x02 /* gateway (SVI) mac*/
642 #define ZEBRA_MACIP_TYPE_ROUTER_FLAG           0x04 /* Router Flag - proxy NA */
643 #define ZEBRA_MACIP_TYPE_OVERRIDE_FLAG         0x08 /* Override Flag */
644 #define ZEBRA_MACIP_TYPE_SVI_IP                0x10 /* SVI MAC-IP */
645 #define ZEBRA_MACIP_TYPE_PROXY_ADVERT          0x20 /* Not locally active */
646 #define ZEBRA_MACIP_TYPE_SYNC_PATH             0x40 /* sync path */
647 /* XXX - flags is an u8; that needs to be changed to u32 if you need
648  * to allocate past 0x80
649  */
650 
651 enum zebra_neigh_state { ZEBRA_NEIGH_INACTIVE = 0, ZEBRA_NEIGH_ACTIVE = 1 };
652 
653 struct zclient_options {
654 	bool receive_notify;
655 	bool synchronous;
656 };
657 
658 extern struct zclient_options zclient_options_default;
659 
660 extern struct zclient *zclient_new(struct thread_master *m,
661 				   struct zclient_options *opt);
662 
663 extern void zclient_init(struct zclient *, int, unsigned short,
664 			 struct zebra_privs_t *privs);
665 extern int zclient_start(struct zclient *);
666 extern void zclient_stop(struct zclient *);
667 extern void zclient_reset(struct zclient *);
668 extern void zclient_free(struct zclient *);
669 
670 extern int zclient_socket_connect(struct zclient *);
671 
672 extern unsigned short *redist_check_instance(struct redist_proto *,
673 					     unsigned short);
674 extern void redist_add_instance(struct redist_proto *, unsigned short);
675 extern void redist_del_instance(struct redist_proto *, unsigned short);
676 extern void redist_del_all_instances(struct redist_proto *red);
677 
678 /*
679  * Send to zebra that the specified vrf is using label to resolve
680  * itself for L3VPN's.  Repeated calls of this function with
681  * different labels will cause an effective update of the
682  * label for lookup.  If you pass in MPLS_LABEL_NONE
683  * we will cause a delete action and remove this label pop
684  * operation.
685  *
686  * The underlying AF_MPLS doesn't care about afi's
687  * but we can make the zebra_vrf keep track of what
688  * we have installed and play some special games
689  * to get them both installed.
690  */
691 extern void zclient_send_vrf_label(struct zclient *zclient, vrf_id_t vrf_id,
692 				   afi_t afi, mpls_label_t label,
693 				   enum lsp_types_t ltype);
694 
695 extern void zclient_send_reg_requests(struct zclient *, vrf_id_t);
696 extern void zclient_send_dereg_requests(struct zclient *, vrf_id_t);
697 extern int zclient_send_router_id_update(struct zclient *zclient,
698 					 zebra_message_types_t type, afi_t afi,
699 					 vrf_id_t vrf_id);
700 
701 extern void zclient_send_interface_radv_req(struct zclient *zclient,
702 					    vrf_id_t vrf_id,
703 					    struct interface *ifp, int enable,
704 					    int ra_interval);
705 extern int zclient_send_interface_protodown(struct zclient *zclient,
706 					    vrf_id_t vrf_id,
707 					    struct interface *ifp, bool down);
708 
709 /* Send redistribute command to zebra daemon. Do not update zclient state. */
710 extern int zebra_redistribute_send(int command, struct zclient *, afi_t,
711 				   int type, unsigned short instance,
712 				   vrf_id_t vrf_id);
713 
714 extern int zebra_redistribute_default_send(int command, struct zclient *zclient,
715 					   afi_t afi, vrf_id_t vrf_id);
716 
717 /* If state has changed, update state and call zebra_redistribute_send. */
718 extern void zclient_redistribute(int command, struct zclient *, afi_t, int type,
719 				 unsigned short instance, vrf_id_t vrf_id);
720 
721 /* If state has changed, update state and send the command to zebra. */
722 extern void zclient_redistribute_default(int command, struct zclient *,
723 					 afi_t, vrf_id_t vrf_id);
724 
725 /* Send the message in zclient->obuf to the zebra daemon (or enqueue it).
726    Returns 0 for success or -1 on an I/O error. */
727 extern int zclient_send_message(struct zclient *);
728 
729 /* create header for command, length to be filled in by user later */
730 extern void zclient_create_header(struct stream *, uint16_t, vrf_id_t);
731 /*
732  * Read sizeof(struct zmsghdr) bytes from the provided socket and parse the
733  * received data into the specified fields. If this is successful, read the
734  * rest of the packet into the provided stream.
735  *
736  * s
737  *    The stream to read into
738  *
739  * sock
740  *    The socket to read from
741  *
742  * size
743  *    Parsed message size will be placed in the pointed-at integer
744  *
745  * marker
746  *    Parsed marker will be placed in the pointed-at byte
747  *
748  * version
749  *    Parsed version will be placed in the pointed-at byte
750  *
751  * vrf_id
752  *    Parsed VRF ID will be placed in the pointed-at vrf_id_t
753  *
754  * cmd
755  *    Parsed command number will be placed in the pointed-at integer
756  *
757  * Returns:
758  *    -1 if:
759  *    - insufficient data for header was read
760  *    - a version mismatch was detected
761  *    - a marker mismatch was detected
762  *    - header size field specified more data than could be read
763  */
764 extern int zclient_read_header(struct stream *s, int sock, uint16_t *size,
765 			       uint8_t *marker, uint8_t *version,
766 			       vrf_id_t *vrf_id, uint16_t *cmd);
767 /*
768  * Parse header from ZAPI message stream into struct zmsghdr.
769  * This function assumes the stream getp points at the first byte of the header.
770  * If the function is successful then the stream getp will point to the byte
771  * immediately after the last byte of the header.
772  *
773  * zmsg
774  *    The stream containing the header
775  *
776  * hdr
777  *    The header struct to parse into.
778  *
779  * Returns:
780  *    true if parsing succeeded, false otherwise
781  */
782 extern bool zapi_parse_header(struct stream *zmsg, struct zmsghdr *hdr);
783 
784 extern void zclient_interface_set_master(struct zclient *client,
785 					 struct interface *master,
786 					 struct interface *slave);
787 extern struct interface *zebra_interface_state_read(struct stream *s, vrf_id_t);
788 extern struct connected *zebra_interface_address_read(int, struct stream *,
789 						      vrf_id_t);
790 extern struct nbr_connected *
791 zebra_interface_nbr_address_read(int, struct stream *, vrf_id_t);
792 extern struct interface *zebra_interface_vrf_update_read(struct stream *s,
793 							 vrf_id_t vrf_id,
794 							 vrf_id_t *new_vrf_id);
795 extern int zebra_router_id_update_read(struct stream *s, struct prefix *rid);
796 
797 extern struct interface *zebra_interface_link_params_read(struct stream *s,
798 							  vrf_id_t vrf_id);
799 extern size_t zebra_interface_link_params_write(struct stream *,
800 						struct interface *);
801 extern int zclient_send_get_label_chunk(struct zclient *zclient, uint8_t keep,
802 					uint32_t chunk_size, uint32_t base);
803 
804 extern int lm_label_manager_connect(struct zclient *zclient, int async);
805 extern int lm_get_label_chunk(struct zclient *zclient, uint8_t keep,
806 			      uint32_t base, uint32_t chunk_size,
807 			      uint32_t *start, uint32_t *end);
808 extern int lm_release_label_chunk(struct zclient *zclient, uint32_t start,
809 				  uint32_t end);
810 extern int tm_table_manager_connect(struct zclient *zclient);
811 extern int tm_get_table_chunk(struct zclient *zclient, uint32_t chunk_size,
812 			      uint32_t *start, uint32_t *end);
813 extern int tm_release_table_chunk(struct zclient *zclient, uint32_t start,
814 				  uint32_t end);
815 
816 extern int zebra_send_sr_policy(struct zclient *zclient, int cmd,
817 				struct zapi_sr_policy *zp);
818 extern int zapi_sr_policy_encode(struct stream *s, int cmd,
819 				 struct zapi_sr_policy *zp);
820 extern int zapi_sr_policy_decode(struct stream *s, struct zapi_sr_policy *zp);
821 extern int zapi_sr_policy_notify_status_decode(struct stream *s,
822 					       struct zapi_sr_policy *zp);
823 
824 extern int zebra_send_mpls_labels(struct zclient *zclient, int cmd,
825 				  struct zapi_labels *zl);
826 extern int zapi_labels_encode(struct stream *s, int cmd,
827 			      struct zapi_labels *zl);
828 extern int zapi_labels_decode(struct stream *s, struct zapi_labels *zl);
829 
830 extern int zebra_send_pw(struct zclient *zclient, int command,
831 			 struct zapi_pw *pw);
832 extern int zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS,
833 				       struct zapi_pw_status *pw);
834 
835 extern int zclient_route_send(uint8_t, struct zclient *, struct zapi_route *);
836 extern int zclient_send_rnh(struct zclient *zclient, int command,
837 			    const struct prefix *p, bool exact_match,
838 			    vrf_id_t vrf_id);
839 int zapi_nexthop_encode(struct stream *s, const struct zapi_nexthop *api_nh,
840 			uint32_t api_flags, uint32_t api_message);
841 extern int zapi_route_encode(uint8_t, struct stream *, struct zapi_route *);
842 extern int zapi_route_decode(struct stream *, struct zapi_route *);
843 bool zapi_route_notify_decode(struct stream *s, struct prefix *p,
844 			      uint32_t *tableid,
845 			      enum zapi_route_notify_owner *note);
846 bool zapi_rule_notify_decode(struct stream *s, uint32_t *seqno,
847 			     uint32_t *priority, uint32_t *unique, char *ifname,
848 			     enum zapi_rule_notify_owner *note);
849 bool zapi_ipset_notify_decode(struct stream *s,
850 			      uint32_t *unique,
851 			     enum zapi_ipset_notify_owner *note);
852 
853 #define ZEBRA_IPSET_NAME_SIZE   32
854 
855 bool zapi_ipset_entry_notify_decode(struct stream *s,
856 	    uint32_t *unique,
857 	    char *ipset_name,
858 	    enum zapi_ipset_entry_notify_owner *note);
859 bool zapi_iptable_notify_decode(struct stream *s,
860 		uint32_t *unique,
861 		enum zapi_iptable_notify_owner *note);
862 
863 extern struct nexthop *
864 nexthop_from_zapi_nexthop(const struct zapi_nexthop *znh);
865 int zapi_nexthop_from_nexthop(struct zapi_nexthop *znh,
866 			      const struct nexthop *nh);
867 int zapi_backup_nexthop_from_nexthop(struct zapi_nexthop *znh,
868 				     const struct nexthop *nh);
869 extern bool zapi_nexthop_update_decode(struct stream *s,
870 				       struct zapi_route *nhr);
871 const char *zapi_nexthop2str(const struct zapi_nexthop *znh, char *buf,
872 			     int bufsize);
873 
874 /* Decode the zebra error message */
875 extern bool zapi_error_decode(struct stream *s, enum zebra_error_types *error);
876 
877 /* Encode and decode restart capabilities */
878 extern int32_t zclient_capabilities_send(uint32_t cmd, struct zclient *zclient,
879 					 struct zapi_cap *api);
880 extern int32_t zapi_capabilities_decode(struct stream *s, struct zapi_cap *api);
881 
zapi_route_set_blackhole(struct zapi_route * api,enum blackhole_type bh_type)882 static inline void zapi_route_set_blackhole(struct zapi_route *api,
883 					    enum blackhole_type bh_type)
884 {
885 	api->nexthop_num = 1;
886 	api->nexthops[0].type = NEXTHOP_TYPE_BLACKHOLE;
887 	api->nexthops[0].vrf_id = VRF_DEFAULT;
888 	api->nexthops[0].bh_type = bh_type;
889 	SET_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP);
890 };
891 
892 extern void zclient_send_mlag_register(struct zclient *client,
893 				       uint32_t bit_map);
894 extern void zclient_send_mlag_deregister(struct zclient *client);
895 
896 extern void zclient_send_mlag_data(struct zclient *client,
897 				   struct stream *client_s);
898 
899 /*
900  * Send an OPAQUE message, contents opaque to zebra - but note that
901  * the length of the payload is restricted by the zclient's
902  * outgoing message buffer.
903  * The message header is a message subtype; please use the registry
904  * below to avoid sub-type collisions. Clients use the registration
905  * apis to manage the specific opaque subtypes they want to receive.
906  */
907 int zclient_send_opaque(struct zclient *zclient, uint32_t type,
908 			const uint8_t *data, size_t datasize);
909 
910 int zclient_send_opaque_unicast(struct zclient *zclient, uint32_t type,
911 				uint8_t proto, uint16_t instance,
912 				uint32_t session_id, const uint8_t *data,
913 				size_t datasize);
914 
915 /* Struct representing the decoded opaque header info */
916 struct zapi_opaque_msg {
917 	uint32_t type; /* Subtype */
918 	uint16_t len;  /* len after zapi header and this info */
919 	uint16_t flags;
920 
921 	/* Client-specific info - *if* UNICAST flag is set */
922 	uint8_t proto;
923 	uint16_t instance;
924 	uint32_t session_id;
925 };
926 
927 #define ZAPI_OPAQUE_FLAG_UNICAST   0x01
928 
929 /* Simple struct to convey registration/unreg requests */
930 struct zapi_opaque_reg_info {
931 	/* Message subtype */
932 	uint32_t type;
933 
934 	/* Client session tuple */
935 	uint8_t proto;
936 	uint16_t instance;
937 	uint32_t session_id;
938 };
939 
940 /* Decode incoming opaque */
941 int zclient_opaque_decode(struct stream *msg, struct zapi_opaque_msg *info);
942 
943 int zclient_register_opaque(struct zclient *zclient, uint32_t type);
944 int zclient_unregister_opaque(struct zclient *zclient, uint32_t type);
945 int zapi_opaque_reg_decode(struct stream *msg,
946 			   struct zapi_opaque_reg_info *info);
947 
948 /*
949  * Registry of opaque message types. Please do not reuse an in-use
950  * type code; some daemons are likely relying on it.
951  */
952 enum zapi_opaque_registry {
953 	/* Request link-state database dump, at restart for example */
954 	LINK_STATE_REQUEST = 1,
955 	/* Update containing link-state db info */
956 	LINK_STATE_UPDATE = 2,
957 };
958 
959 /* Send the hello message.
960  * Returns 0 for success or -1 on an I/O error.
961  */
962 extern int zclient_send_hello(struct zclient *client);
963 
964 extern int zclient_send_neigh_discovery_req(struct zclient *zclient,
965 					    const struct interface *ifp,
966 					    const struct prefix *p);
967 
968 #ifdef __cplusplus
969 }
970 #endif
971 
972 #endif /* _ZEBRA_ZCLIENT_H */
973