]> git.proxmox.com Git - mirror_frr.git/blob - lib/zclient.h
lib, zebra: slight cleanup after rebase
[mirror_frr.git] / lib / zclient.h
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 /* For input/output buffer to zebra. */
40 #define ZEBRA_MAX_PACKET_SIZ 16384
41
42 /* Zebra header size. */
43 #define ZEBRA_HEADER_SIZE 10
44
45 /* special socket path name to use TCP
46 * @ is used as first character because that's abstract socket names on Linux
47 */
48 #define ZAPI_TCP_PATHNAME "@tcp"
49
50 extern struct sockaddr_storage zclient_addr;
51 extern socklen_t zclient_addr_len;
52
53 /* Zebra message types. */
54 typedef enum {
55 ZEBRA_INTERFACE_ADD,
56 ZEBRA_INTERFACE_DELETE,
57 ZEBRA_INTERFACE_ADDRESS_ADD,
58 ZEBRA_INTERFACE_ADDRESS_DELETE,
59 ZEBRA_INTERFACE_UP,
60 ZEBRA_INTERFACE_DOWN,
61 ZEBRA_INTERFACE_SET_MASTER,
62 ZEBRA_ROUTE_ADD,
63 ZEBRA_ROUTE_DELETE,
64 ZEBRA_ROUTE_NOTIFY_OWNER,
65 ZEBRA_IPV4_ROUTE_ADD,
66 ZEBRA_IPV4_ROUTE_DELETE,
67 ZEBRA_IPV6_ROUTE_ADD,
68 ZEBRA_IPV6_ROUTE_DELETE,
69 ZEBRA_REDISTRIBUTE_ADD,
70 ZEBRA_REDISTRIBUTE_DELETE,
71 ZEBRA_REDISTRIBUTE_DEFAULT_ADD,
72 ZEBRA_REDISTRIBUTE_DEFAULT_DELETE,
73 ZEBRA_ROUTER_ID_ADD,
74 ZEBRA_ROUTER_ID_DELETE,
75 ZEBRA_ROUTER_ID_UPDATE,
76 ZEBRA_HELLO,
77 ZEBRA_NEXTHOP_REGISTER,
78 ZEBRA_NEXTHOP_UNREGISTER,
79 ZEBRA_NEXTHOP_UPDATE,
80 ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
81 ZEBRA_INTERFACE_NBR_ADDRESS_DELETE,
82 ZEBRA_INTERFACE_BFD_DEST_UPDATE,
83 ZEBRA_IMPORT_ROUTE_REGISTER,
84 ZEBRA_IMPORT_ROUTE_UNREGISTER,
85 ZEBRA_IMPORT_CHECK_UPDATE,
86 ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD,
87 ZEBRA_BFD_DEST_REGISTER,
88 ZEBRA_BFD_DEST_DEREGISTER,
89 ZEBRA_BFD_DEST_UPDATE,
90 ZEBRA_BFD_DEST_REPLAY,
91 ZEBRA_REDISTRIBUTE_ROUTE_ADD,
92 ZEBRA_REDISTRIBUTE_ROUTE_DEL,
93 ZEBRA_VRF_UNREGISTER,
94 ZEBRA_VRF_ADD,
95 ZEBRA_VRF_DELETE,
96 ZEBRA_VRF_LABEL,
97 ZEBRA_INTERFACE_VRF_UPDATE,
98 ZEBRA_BFD_CLIENT_REGISTER,
99 ZEBRA_INTERFACE_ENABLE_RADV,
100 ZEBRA_INTERFACE_DISABLE_RADV,
101 ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB,
102 ZEBRA_INTERFACE_LINK_PARAMS,
103 ZEBRA_MPLS_LABELS_ADD,
104 ZEBRA_MPLS_LABELS_DELETE,
105 ZEBRA_IPMR_ROUTE_STATS,
106 ZEBRA_LABEL_MANAGER_CONNECT,
107 ZEBRA_GET_LABEL_CHUNK,
108 ZEBRA_RELEASE_LABEL_CHUNK,
109 ZEBRA_FEC_REGISTER,
110 ZEBRA_FEC_UNREGISTER,
111 ZEBRA_FEC_UPDATE,
112 ZEBRA_ADVERTISE_DEFAULT_GW,
113 ZEBRA_ADVERTISE_SUBNET,
114 ZEBRA_ADVERTISE_ALL_VNI,
115 ZEBRA_VNI_ADD,
116 ZEBRA_VNI_DEL,
117 ZEBRA_L3VNI_ADD,
118 ZEBRA_L3VNI_DEL,
119 ZEBRA_REMOTE_VTEP_ADD,
120 ZEBRA_REMOTE_VTEP_DEL,
121 ZEBRA_MACIP_ADD,
122 ZEBRA_MACIP_DEL,
123 ZEBRA_IP_PREFIX_ROUTE_ADD,
124 ZEBRA_IP_PREFIX_ROUTE_DEL,
125 ZEBRA_REMOTE_MACIP_ADD,
126 ZEBRA_REMOTE_MACIP_DEL,
127 ZEBRA_PW_ADD,
128 ZEBRA_PW_DELETE,
129 ZEBRA_PW_SET,
130 ZEBRA_PW_UNSET,
131 ZEBRA_PW_STATUS_UPDATE,
132 ZEBRA_RULE_ADD,
133 ZEBRA_RULE_DELETE,
134 ZEBRA_RULE_NOTIFY_OWNER,
135 } zebra_message_types_t;
136
137 struct redist_proto {
138 u_char enabled;
139 struct list *instances;
140 };
141
142 /* Structure for the zebra client. */
143 struct zclient {
144 /* The thread master we schedule ourselves on */
145 struct thread_master *master;
146
147 /* Priviledges to change socket values */
148 struct zebra_privs_t *privs;
149
150 /* Do we care about failure events for route install? */
151 bool receive_notify;
152
153 /* Socket to zebra daemon. */
154 int sock;
155
156 /* Connection failure count. */
157 int fail;
158
159 /* Input buffer for zebra message. */
160 struct stream *ibuf;
161
162 /* Output buffer for zebra message. */
163 struct stream *obuf;
164
165 /* Buffer of data waiting to be written to zebra. */
166 struct buffer *wb;
167
168 /* Read and connect thread. */
169 struct thread *t_read;
170 struct thread *t_connect;
171
172 /* Thread to write buffered data to zebra. */
173 struct thread *t_write;
174
175 /* Redistribute information. */
176 u_char redist_default; /* clients protocol */
177 u_short instance;
178 struct redist_proto mi_redist[AFI_MAX][ZEBRA_ROUTE_MAX];
179 vrf_bitmap_t redist[AFI_MAX][ZEBRA_ROUTE_MAX];
180
181 /* Redistribute defauilt. */
182 vrf_bitmap_t default_information;
183
184 /* Pointer to the callback functions. */
185 void (*zebra_connected)(struct zclient *);
186 int (*router_id_update)(int, struct zclient *, uint16_t, vrf_id_t);
187 int (*interface_add)(int, struct zclient *, uint16_t, vrf_id_t);
188 int (*interface_delete)(int, struct zclient *, uint16_t, vrf_id_t);
189 int (*interface_up)(int, struct zclient *, uint16_t, vrf_id_t);
190 int (*interface_down)(int, struct zclient *, uint16_t, vrf_id_t);
191 int (*interface_address_add)(int, struct zclient *, uint16_t, vrf_id_t);
192 int (*interface_address_delete)(int, struct zclient *, uint16_t,
193 vrf_id_t);
194 int (*interface_link_params)(int, struct zclient *, uint16_t);
195 int (*interface_bfd_dest_update)(int, struct zclient *, uint16_t,
196 vrf_id_t);
197 int (*interface_nbr_address_add)(int, struct zclient *, uint16_t,
198 vrf_id_t);
199 int (*interface_nbr_address_delete)(int, struct zclient *, uint16_t,
200 vrf_id_t);
201 int (*interface_vrf_update)(int, struct zclient *, uint16_t, vrf_id_t);
202 int (*nexthop_update)(int, struct zclient *, uint16_t, vrf_id_t);
203 int (*import_check_update)(int, struct zclient *, uint16_t, vrf_id_t);
204 int (*bfd_dest_replay)(int, struct zclient *, uint16_t, vrf_id_t);
205 int (*redistribute_route_add)(int, struct zclient *, uint16_t,
206 vrf_id_t);
207 int (*redistribute_route_del)(int, struct zclient *, uint16_t,
208 vrf_id_t);
209 int (*fec_update)(int, struct zclient *, uint16_t);
210 int (*local_vni_add)(int, struct zclient *, uint16_t, vrf_id_t);
211 int (*local_vni_del)(int, struct zclient *, uint16_t, vrf_id_t);
212 int (*local_l3vni_add)(int, struct zclient *, uint16_t, vrf_id_t);
213 int (*local_l3vni_del)(int, struct zclient *, uint16_t, vrf_id_t);
214 void (*local_ip_prefix_add)(int, struct zclient *, uint16_t, vrf_id_t);
215 void (*local_ip_prefix_del)(int, struct zclient *, uint16_t, vrf_id_t);
216 int (*local_macip_add)(int, struct zclient *, uint16_t, vrf_id_t);
217 int (*local_macip_del)(int, struct zclient *, uint16_t, vrf_id_t);
218 int (*pw_status_update)(int, struct zclient *, uint16_t, vrf_id_t);
219 int (*route_notify_owner)(int command, struct zclient *zclient,
220 uint16_t length, vrf_id_t vrf_id);
221 int (*rule_notify_owner)(int command, struct zclient *zclient,
222 uint16_t length, vrf_id_t vrf_id);
223 };
224
225 /* Zebra API message flag. */
226 #define ZAPI_MESSAGE_NEXTHOP 0x01
227 #define ZAPI_MESSAGE_DISTANCE 0x02
228 #define ZAPI_MESSAGE_METRIC 0x04
229 #define ZAPI_MESSAGE_TAG 0x08
230 #define ZAPI_MESSAGE_MTU 0x10
231 #define ZAPI_MESSAGE_SRCPFX 0x20
232 #define ZAPI_MESSAGE_LABEL 0x40
233 /*
234 * This should only be used by a DAEMON that needs to communicate
235 * the table being used is not in the VRF. You must pass the
236 * default vrf, else this will be ignored.
237 */
238 #define ZAPI_MESSAGE_TABLEID 0x80
239
240 #define ZSERV_VERSION 5
241 /* Zserv protocol message header */
242 struct zmsghdr {
243 uint16_t length;
244 /* Always set to 255 in new zserv */
245 uint8_t marker;
246 uint8_t version;
247 vrf_id_t vrf_id;
248 uint16_t command;
249 };
250
251 struct zapi_nexthop {
252 enum nexthop_types_t type;
253 vrf_id_t vrf_id;
254 ifindex_t ifindex;
255 union {
256 union g_addr gate;
257 enum blackhole_type bh_type;
258 };
259
260 /* MPLS labels for BGP-LU or Segment Routing */
261 uint8_t label_num;
262 mpls_label_t labels[MPLS_MAX_LABELS];
263 };
264
265 /*
266 * Some of these data structures do not map easily to
267 * a actual data structure size giving different compilers
268 * and systems. For those data structures we need
269 * to use the smallest available stream_getX/putX functions
270 * to encode/decode.
271 */
272 struct zapi_route {
273 u_char type;
274 u_short instance;
275
276 u_int32_t flags;
277
278 u_char message;
279
280 /*
281 * This is an enum but we are going to treat it as a uint8_t
282 * for purpose of encoding/decoding
283 */
284 safi_t safi;
285
286 struct prefix prefix;
287 struct prefix_ipv6 src_prefix;
288
289 u_int16_t nexthop_num;
290 struct zapi_nexthop nexthops[MULTIPATH_NUM];
291
292 u_char distance;
293
294 u_int32_t metric;
295
296 route_tag_t tag;
297
298 u_int32_t mtu;
299
300 vrf_id_t vrf_id;
301
302 uint32_t tableid;
303
304 struct ethaddr rmac;
305 };
306
307 /* Zebra IPv4 route message API. */
308 struct zapi_ipv4 {
309 u_char type;
310 u_short instance;
311
312 u_int32_t flags;
313
314 u_char message;
315
316 safi_t safi;
317
318 u_char nexthop_num;
319 struct in_addr **nexthop;
320
321 u_char ifindex_num;
322 ifindex_t *ifindex;
323
324 u_char label_num;
325 unsigned int *label;
326
327 u_char distance;
328
329 u_int32_t metric;
330
331 route_tag_t tag;
332
333 u_int32_t mtu;
334
335 vrf_id_t vrf_id;
336 };
337
338 struct zapi_pw {
339 char ifname[IF_NAMESIZE];
340 ifindex_t ifindex;
341 int type;
342 int af;
343 union g_addr nexthop;
344 uint32_t local_label;
345 uint32_t remote_label;
346 uint8_t flags;
347 union pw_protocol_fields data;
348 uint8_t protocol;
349 };
350
351 struct zapi_pw_status {
352 char ifname[IF_NAMESIZE];
353 ifindex_t ifindex;
354 uint32_t status;
355 };
356
357 enum zapi_route_notify_owner {
358 ZAPI_ROUTE_FAIL_INSTALL,
359 ZAPI_ROUTE_BETTER_ADMIN_WON,
360 ZAPI_ROUTE_INSTALLED,
361 ZAPI_ROUTE_REMOVED,
362 ZAPI_ROUTE_REMOVE_FAIL,
363 };
364
365 enum zapi_rule_notify_owner {
366 ZAPI_RULE_FAIL_INSTALL,
367 ZAPI_RULE_INSTALLED,
368 ZAPI_RULE_REMOVED,
369 };
370
371 /* Zebra MAC types */
372 #define ZEBRA_MACIP_TYPE_STICKY 0x01 /* Sticky MAC*/
373 #define ZEBRA_MACIP_TYPE_GW 0x02 /* gateway (SVI) mac*/
374
375 struct zclient_options {
376 bool receive_notify;
377 };
378
379 /* Prototypes of zebra client service functions. */
380 extern struct zclient *zclient_new(struct thread_master *);
381
382 /* clang-format off */
383 #if CONFDATE > 20181101
384 CPP_NOTICE("zclient_new_notify can take over or zclient_new now");
385 #endif
386 /* clang-format on */
387
388 extern struct zclient_options zclient_options_default;
389
390 extern struct zclient *zclient_new_notify(struct thread_master *m,
391 struct zclient_options *opt);
392
393 #define zclient_new(A) \
394 zclient_new_notify((A), &zclient_options_default); \
395 CPP_WARN("Please transition to using zclient_new_notify");
396
397 extern void zclient_init(struct zclient *, int, u_short,
398 struct zebra_privs_t *privs);
399 extern int zclient_start(struct zclient *);
400 extern void zclient_stop(struct zclient *);
401 extern void zclient_reset(struct zclient *);
402 extern void zclient_free(struct zclient *);
403
404 extern int zclient_socket_connect(struct zclient *);
405
406 extern u_short *redist_check_instance(struct redist_proto *, u_short);
407 extern void redist_add_instance(struct redist_proto *, u_short);
408 extern void redist_del_instance(struct redist_proto *, u_short);
409
410 /*
411 * Send to zebra that the specified vrf is using label to resolve
412 * itself for L3VPN's. Repeated calls of this function with
413 * different labels will cause an effective update of the
414 * label for lookup. If you pass in MPLS_LABEL_NONE
415 * we will cause a delete action and remove this label pop
416 * operation.
417 *
418 * The underlying AF_MPLS doesn't care about afi's
419 * but we can make the zebra_vrf keep track of what
420 * we have installed and play some special games
421 * to get them both installed.
422 */
423 extern void zclient_send_vrf_label(struct zclient *zclient, vrf_id_t vrf_id,
424 afi_t afi, mpls_label_t label,
425 enum lsp_types_t ltype);
426
427 extern void zclient_send_reg_requests(struct zclient *, vrf_id_t);
428 extern void zclient_send_dereg_requests(struct zclient *, vrf_id_t);
429
430 extern void zclient_send_interface_radv_req(struct zclient *zclient,
431 vrf_id_t vrf_id,
432 struct interface *ifp, int enable,
433 int ra_interval);
434
435 /* Send redistribute command to zebra daemon. Do not update zclient state. */
436 extern int zebra_redistribute_send(int command, struct zclient *, afi_t,
437 int type, u_short instance, vrf_id_t vrf_id);
438
439 /* If state has changed, update state and call zebra_redistribute_send. */
440 extern void zclient_redistribute(int command, struct zclient *, afi_t, int type,
441 u_short instance, vrf_id_t vrf_id);
442
443 /* If state has changed, update state and send the command to zebra. */
444 extern void zclient_redistribute_default(int command, struct zclient *,
445 vrf_id_t vrf_id);
446
447 /* Send the message in zclient->obuf to the zebra daemon (or enqueue it).
448 Returns 0 for success or -1 on an I/O error. */
449 extern int zclient_send_message(struct zclient *);
450
451 /* create header for command, length to be filled in by user later */
452 extern void zclient_create_header(struct stream *, uint16_t, vrf_id_t);
453 /*
454 * Read sizeof(struct zmsghdr) bytes from the provided socket and parse the
455 * received data into the specified fields. If this is successful, read the
456 * rest of the packet into the provided stream.
457 *
458 * s
459 * The stream to read into
460 *
461 * sock
462 * The socket to read from
463 *
464 * size
465 * Parsed message size will be placed in the pointed-at integer
466 *
467 * marker
468 * Parsed marker will be placed in the pointed-at byte
469 *
470 * version
471 * Parsed version will be placed in the pointed-at byte
472 *
473 * vrf_id
474 * Parsed VRF ID will be placed in the pointed-at vrf_id_t
475 *
476 * cmd
477 * Parsed command number will be placed in the pointed-at integer
478 *
479 * Returns:
480 * -1 if:
481 * - insufficient data for header was read
482 * - a version mismatch was detected
483 * - a marker mismatch was detected
484 * - header size field specified more data than could be read
485 */
486 extern int zclient_read_header(struct stream *s, int sock, u_int16_t *size,
487 u_char *marker, u_char *version,
488 vrf_id_t *vrf_id, u_int16_t *cmd);
489 /*
490 * Parse header from ZAPI message stream into struct zmsghdr.
491 * This function assumes the stream getp points at the first byte of the header.
492 * If the function is successful then the stream getp will point to the byte
493 * immediately after the last byte of the header.
494 *
495 * zmsg
496 * The stream containing the header
497 *
498 * hdr
499 * The header struct to parse into.
500 *
501 * Returns:
502 * true if parsing succeeded, false otherwise
503 */
504 extern bool zapi_parse_header(struct stream *zmsg, struct zmsghdr *hdr);
505
506 extern void zclient_interface_set_master(struct zclient *client,
507 struct interface *master,
508 struct interface *slave);
509 extern struct interface *zebra_interface_add_read(struct stream *, vrf_id_t);
510 extern struct interface *zebra_interface_state_read(struct stream *s, vrf_id_t);
511 extern struct connected *zebra_interface_address_read(int, struct stream *,
512 vrf_id_t);
513 extern struct nbr_connected *
514 zebra_interface_nbr_address_read(int, struct stream *, vrf_id_t);
515 extern struct interface *zebra_interface_vrf_update_read(struct stream *s,
516 vrf_id_t vrf_id,
517 vrf_id_t *new_vrf_id);
518 extern void zebra_interface_if_set_value(struct stream *, struct interface *);
519 extern void zebra_router_id_update_read(struct stream *s, struct prefix *rid);
520
521 /* clang-format off */
522 #if CONFDATE > 20180823
523 CPP_NOTICE("zapi_ipv4_route, zapi_ipv6_route, zapi_ipv4_route_ipv6_nexthop as well as the zapi_ipv4 and zapi_ipv6 data structures should be removed now");
524 #endif
525 /* clang-format on */
526
527 extern int zapi_ipv4_route(u_char, struct zclient *, struct prefix_ipv4 *,
528 struct zapi_ipv4 *) __attribute__((deprecated));
529
530 extern struct interface *zebra_interface_link_params_read(struct stream *);
531 extern size_t zebra_interface_link_params_write(struct stream *,
532 struct interface *);
533 extern int lm_label_manager_connect(struct zclient *zclient);
534 extern int lm_get_label_chunk(struct zclient *zclient, u_char keep,
535 uint32_t chunk_size, uint32_t *start,
536 uint32_t *end);
537 extern int lm_release_label_chunk(struct zclient *zclient, uint32_t start,
538 uint32_t end);
539 extern int zebra_send_pw(struct zclient *zclient, int command,
540 struct zapi_pw *pw);
541 extern void zebra_read_pw_status_update(int command, struct zclient *zclient,
542 zebra_size_t length, vrf_id_t vrf_id,
543 struct zapi_pw_status *pw);
544
545 /* IPv6 prefix add and delete function prototype. */
546
547 struct zapi_ipv6 {
548 u_char type;
549 u_short instance;
550
551 u_int32_t flags;
552
553 u_char message;
554
555 safi_t safi;
556
557 u_char nexthop_num;
558 struct in6_addr **nexthop;
559
560 u_char ifindex_num;
561 ifindex_t *ifindex;
562
563 u_char label_num;
564 unsigned int *label;
565
566 u_char distance;
567
568 u_int32_t metric;
569
570 route_tag_t tag;
571
572 u_int32_t mtu;
573
574 vrf_id_t vrf_id;
575 };
576
577 extern int zapi_ipv6_route(u_char cmd, struct zclient *zclient,
578 struct prefix_ipv6 *p, struct prefix_ipv6 *src_p,
579 struct zapi_ipv6 *api) __attribute__((deprecated));
580 extern int zapi_ipv4_route_ipv6_nexthop(u_char, struct zclient *,
581 struct prefix_ipv4 *,
582 struct zapi_ipv6 *)
583 __attribute__((deprecated));
584 extern int zclient_route_send(u_char, struct zclient *, struct zapi_route *);
585 extern int zclient_send_rnh(struct zclient *zclient, int command,
586 struct prefix *p, bool exact_match,
587 vrf_id_t vrf_id);
588 extern int zapi_route_encode(u_char, struct stream *, struct zapi_route *);
589 extern int zapi_route_decode(struct stream *, struct zapi_route *);
590 bool zapi_route_notify_decode(struct stream *s, struct prefix *p,
591 uint32_t *tableid,
592 enum zapi_route_notify_owner *note);
593 bool zapi_rule_notify_decode(struct stream *s, uint32_t *seqno,
594 uint32_t *priority, uint32_t *unique,
595 ifindex_t *ifindex,
596 enum zapi_rule_notify_owner *note);
597 extern struct nexthop *nexthop_from_zapi_nexthop(struct zapi_nexthop *znh);
598 extern bool zapi_nexthop_update_decode(struct stream *s,
599 struct zapi_route *nhr);
600
601 static inline void zapi_route_set_blackhole(struct zapi_route *api,
602 enum blackhole_type bh_type)
603 {
604 api->nexthop_num = 1;
605 api->nexthops[0].type = NEXTHOP_TYPE_BLACKHOLE;
606 api->nexthops[0].vrf_id = VRF_DEFAULT;
607 api->nexthops[0].bh_type = bh_type;
608 SET_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP);
609 };
610
611
612 #endif /* _ZEBRA_ZCLIENT_H */