]> git.proxmox.com Git - mirror_frr.git/blame - bfdd/bfd.h
bfdd: generate random session identificators
[mirror_frr.git] / bfdd / bfd.h
CommitLineData
e9e2c950
RZ
1/*********************************************************************
2 * Copyright 2014,2015,2016,2017 Cumulus Networks, Inc. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 2 of the License, or (at your option)
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; see the file COPYING; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * bfd.h: implements the BFD protocol.
19 */
20
21#ifndef _BFD_H_
22#define _BFD_H_
23
24#include <netinet/in.h>
25
26#include <stdbool.h>
27#include <stdarg.h>
28#include <stdint.h>
29
30#include "lib/hash.h"
31#include "lib/libfrr.h"
32#include "lib/qobj.h"
33#include "lib/queue.h"
80edb675 34#include "lib/vrf.h"
e9e2c950
RZ
35
36#include "bfdctl.h"
37
38#define ETHERNET_ADDRESS_LENGTH 6
39
40#ifdef BFD_DEBUG
41#define BFDD_JSON_CONV_OPTIONS (JSON_C_TO_STRING_PRETTY)
42#else
43#define BFDD_JSON_CONV_OPTIONS (0)
44#endif
45
46DECLARE_MGROUP(BFDD);
47DECLARE_MTYPE(BFDD_TMP);
48DECLARE_MTYPE(BFDD_CONFIG);
49DECLARE_MTYPE(BFDD_LABEL);
50DECLARE_MTYPE(BFDD_CONTROL);
51DECLARE_MTYPE(BFDD_NOTIFICATION);
52
53struct bfd_timers {
54 uint32_t desired_min_tx;
55 uint32_t required_min_rx;
56 uint32_t required_min_echo;
57};
58
59struct bfd_discrs {
60 uint32_t my_discr;
61 uint32_t remote_discr;
62};
63
64/*
65 * Format of control packet. From section 4)
66 */
67struct bfd_pkt {
68 union {
69 uint32_t byteFields;
70 struct {
71 uint8_t diag;
72 uint8_t flags;
73 uint8_t detect_mult;
74 uint8_t len;
75 };
76 };
77 struct bfd_discrs discrs;
78 struct bfd_timers timers;
79};
80
81/*
82 * Format of Echo packet.
83 */
84struct bfd_echo_pkt {
85 union {
86 uint32_t byteFields;
87 struct {
88 uint8_t ver;
89 uint8_t len;
90 uint16_t reserved;
91 };
92 };
93 uint32_t my_discr;
94 uint8_t pad[16];
95};
96
97
98/* Macros for manipulating control packets */
99#define BFD_VERMASK 0x03
100#define BFD_DIAGMASK 0x1F
101#define BFD_GETVER(diag) ((diag >> 5) & BFD_VERMASK)
102#define BFD_SETVER(diag, val) ((diag) |= (val & BFD_VERMASK) << 5)
103#define BFD_VERSION 1
104#define BFD_PBIT 0x20
105#define BFD_FBIT 0x10
106#define BFD_CBIT 0x08
107#define BFD_ABIT 0x04
108#define BFD_DEMANDBIT 0x02
e9e2c950
RZ
109#define BFD_SETDEMANDBIT(flags, val) \
110 { \
111 if ((val)) \
112 flags |= BFD_DEMANDBIT; \
113 }
114#define BFD_SETPBIT(flags, val) \
115 { \
116 if ((val)) \
117 flags |= BFD_PBIT; \
118 }
119#define BFD_GETPBIT(flags) (flags & BFD_PBIT)
120#define BFD_SETFBIT(flags, val) \
121 { \
122 if ((val)) \
123 flags |= BFD_FBIT; \
124 }
125#define BFD_GETFBIT(flags) (flags & BFD_FBIT)
126#define BFD_SETSTATE(flags, val) \
127 { \
128 if ((val)) \
129 flags |= (val & 0x3) << 6; \
130 }
131#define BFD_GETSTATE(flags) ((flags >> 6) & 0x3)
132#define BFD_ECHO_VERSION 1
133#define BFD_ECHO_PKT_LEN sizeof(struct bfd_echo_pkt)
e9e2c950 134
40675ea9
RZ
135enum bfd_diagnosticis {
136 BD_OK = 0,
137 /* Control Detection Time Expired. */
138 BD_CONTROL_EXPIRED = 1,
139 /* Echo Function Failed. */
140 BD_ECHO_FAILED = 2,
141 /* Neighbor Signaled Session Down. */
142 BD_NEIGHBOR_DOWN = 3,
143 /* Forwarding Plane Reset. */
144 BD_FORWARDING_RESET = 4,
145 /* Path Down. */
146 BD_PATH_DOWN = 5,
147 /* Concatenated Path Down. */
148 BD_CONCATPATH_DOWN = 6,
149 /* Administratively Down. */
150 BD_ADMIN_DOWN = 7,
151 /* Reverse Concatenated Path Down. */
152 BD_REVCONCATPATH_DOWN = 8,
153 /* 9..31: reserved. */
154};
155
e9e2c950
RZ
156/* BFD session flags */
157enum bfd_session_flags {
158 BFD_SESS_FLAG_NONE = 0,
159 BFD_SESS_FLAG_ECHO = 1 << 0, /* BFD Echo functionality */
160 BFD_SESS_FLAG_ECHO_ACTIVE = 1 << 1, /* BFD Echo Packets are being sent
161 * actively
162 */
163 BFD_SESS_FLAG_MH = 1 << 2, /* BFD Multi-hop session */
e9e2c950
RZ
164 BFD_SESS_FLAG_IPV6 = 1 << 4, /* BFD IPv6 session */
165 BFD_SESS_FLAG_SEND_EVT_ACTIVE = 1 << 5, /* send event timer active */
166 BFD_SESS_FLAG_SEND_EVT_IGNORE = 1 << 6, /* ignore send event when timer
167 * expires
168 */
169 BFD_SESS_FLAG_SHUTDOWN = 1 << 7, /* disable BGP peer function */
170};
171
172#define BFD_SET_FLAG(field, flag) (field |= flag)
173#define BFD_UNSET_FLAG(field, flag) (field &= ~flag)
174#define BFD_CHECK_FLAG(field, flag) (field & flag)
175
176/* BFD session hash keys */
177struct bfd_shop_key {
178 struct sockaddr_any peer;
b333abc2 179 ifindex_t ifindex;
e9e2c950
RZ
180};
181
182struct bfd_mhop_key {
183 struct sockaddr_any peer;
184 struct sockaddr_any local;
b333abc2 185 vrf_id_t vrfid;
e9e2c950
RZ
186};
187
188struct bfd_session_stats {
189 uint64_t rx_ctrl_pkt;
190 uint64_t tx_ctrl_pkt;
191 uint64_t rx_echo_pkt;
192 uint64_t tx_echo_pkt;
0684c9b1
RZ
193 uint64_t session_up;
194 uint64_t session_down;
195 uint64_t znotification;
e9e2c950
RZ
196};
197
e9e2c950
RZ
198/* bfd_session shortcut label forwarding. */
199struct peer_label;
200
201/*
202 * Session state information
203 */
204struct bfd_session {
205
206 /* protocol state per RFC 5880*/
207 uint8_t ses_state;
208 struct bfd_discrs discrs;
209 uint8_t local_diag;
210 uint8_t demand_mode;
211 uint8_t detect_mult;
212 uint8_t remote_detect_mult;
213 uint8_t mh_ttl;
214
215 /* Timers */
216 struct bfd_timers timers;
f43b9368 217 struct bfd_timers cur_timers;
e9e2c950
RZ
218 uint64_t detect_TO;
219 struct thread *echo_recvtimer_ev;
220 struct thread *recvtimer_ev;
221 uint64_t xmt_TO;
222 uint64_t echo_xmt_TO;
223 struct thread *xmttimer_ev;
224 struct thread *echo_xmttimer_ev;
225 uint64_t echo_detect_TO;
226
227 /* software object state */
228 uint8_t polling;
229
230 /* This and the localDiscr are the keys to state info */
231 struct peer_label *pl;
232 union {
233 struct bfd_shop_key shop;
234 struct bfd_mhop_key mhop;
235 };
236 int sock;
237
238 struct sockaddr_any local_address;
239 struct sockaddr_any local_ip;
80edb675 240 struct interface *ifp;
b333abc2 241 struct vrf *vrf;
e9e2c950
RZ
242 uint8_t local_mac[ETHERNET_ADDRESS_LENGTH];
243 uint8_t peer_mac[ETHERNET_ADDRESS_LENGTH];
e9e2c950
RZ
244
245 /* BFD session flags */
246 enum bfd_session_flags flags;
247
e9e2c950 248 struct bfd_session_stats stats;
e9e2c950
RZ
249
250 struct timeval uptime; /* last up time */
251 struct timeval downtime; /* last down time */
252
253 /* Remote peer data (for debugging mostly) */
254 uint8_t remote_diag;
255 struct bfd_timers remote_timers;
256
257 uint64_t refcount; /* number of pointers referencing this. */
258
259 /* VTY context data. */
260 QOBJ_FIELDS;
261};
262DECLARE_QOBJ_TYPE(bfd_session);
263
264struct peer_label {
265 TAILQ_ENTRY(peer_label) pl_entry;
266
267 struct bfd_session *pl_bs;
268 char pl_label[MAXNAMELEN];
269};
270TAILQ_HEAD(pllist, peer_label);
271
272struct bfd_diag_str_list {
273 const char *str;
274 int type;
275};
276
277struct bfd_state_str_list {
278 const char *str;
279 int type;
280};
281
e9e2c950
RZ
282
283/* States defined per 4.1 */
284#define PTM_BFD_ADM_DOWN 0
285#define PTM_BFD_DOWN 1
286#define PTM_BFD_INIT 2
287#define PTM_BFD_UP 3
288
289
290/* Various constants */
291/* Retrieved from ptm_timer.h from Cumulus PTM sources. */
292#define MSEC_PER_SEC 1000L
293#define NSEC_PER_MSEC 1000000L
294
295#define BFD_DEF_DEMAND 0
296#define BFD_DEFDETECTMULT 3
297#define BFD_DEFDESIREDMINTX (300 * MSEC_PER_SEC)
298#define BFD_DEFREQUIREDMINRX (300 * MSEC_PER_SEC)
299#define BFD_DEF_REQ_MIN_ECHO (50 * MSEC_PER_SEC)
1f2d5f91 300#define BFD_DEF_SLOWTX (1000 * MSEC_PER_SEC)
e9e2c950
RZ
301#define BFD_DEF_MHOP_TTL 5
302#define BFD_PKT_LEN 24 /* Length of control packet */
303#define BFD_TTL_VAL 255
304#define BFD_RCV_TTL_VAL 1
305#define BFD_TOS_VAL 0xC0
306#define BFD_PKT_INFO_VAL 1
307#define BFD_IPV6_PKT_INFO_VAL 1
308#define BFD_IPV6_ONLY_VAL 1
309#define BFD_SRCPORTINIT 49142
310#define BFD_SRCPORTMAX 65536
311#define BFD_DEFDESTPORT 3784
312#define BFD_DEF_ECHO_PORT 3785
313#define BFD_DEF_MHOP_DEST_PORT 4784
314#define BFD_CMD_STRING_LEN (MAXNAMELEN + 50)
315#define BFD_BUFFER_LEN (BFD_CMD_STRING_LEN + MAXNAMELEN + 1)
316
317/*
318 * control.c
319 *
320 * Daemon control code to speak with local consumers.
321 */
322
323/* See 'bfdctrl.h' for client protocol definitions. */
324
325struct bfd_control_buffer {
326 size_t bcb_left;
327 size_t bcb_pos;
328 union {
329 struct bfd_control_msg *bcb_bcm;
330 uint8_t *bcb_buf;
331 };
332};
333
334struct bfd_control_queue {
335 TAILQ_ENTRY(bfd_control_queue) bcq_entry;
336
337 struct bfd_control_buffer bcq_bcb;
338};
339TAILQ_HEAD(bcqueue, bfd_control_queue);
340
341struct bfd_notify_peer {
342 TAILQ_ENTRY(bfd_notify_peer) bnp_entry;
343
344 struct bfd_session *bnp_bs;
345};
346TAILQ_HEAD(bnplist, bfd_notify_peer);
347
348struct bfd_control_socket {
349 TAILQ_ENTRY(bfd_control_socket) bcs_entry;
350
351 int bcs_sd;
352 struct thread *bcs_ev;
353 struct thread *bcs_outev;
354 struct bcqueue bcs_bcqueue;
355
356 /* Notification data */
357 uint64_t bcs_notify;
358 struct bnplist bcs_bnplist;
359
360 enum bc_msg_version bcs_version;
361 enum bc_msg_type bcs_type;
362
363 /* Message buffering */
364 struct bfd_control_buffer bcs_bin;
365 struct bfd_control_buffer *bcs_bout;
366};
367TAILQ_HEAD(bcslist, bfd_control_socket);
368
369int control_init(const char *path);
370void control_shutdown(void);
371int control_notify(struct bfd_session *bs);
372int control_notify_config(const char *op, struct bfd_session *bs);
373int control_accept(struct thread *t);
374
375
376/*
377 * bfdd.c
378 *
379 * Daemon specific code.
380 */
381struct bfd_global {
382 int bg_shop;
383 int bg_mhop;
384 int bg_shop6;
385 int bg_mhop6;
386 int bg_echo;
2f11c53f 387 int bg_echov6;
e9e2c950
RZ
388 struct thread *bg_ev[6];
389
390 int bg_csock;
391 struct thread *bg_csockev;
392 struct bcslist bg_bcslist;
393
394 struct pllist bg_pllist;
395};
396extern struct bfd_global bglobal;
397extern struct bfd_diag_str_list diag_list[];
398extern struct bfd_state_str_list state_list[];
399
400void socket_close(int *s);
401
402
403/*
404 * config.c
405 *
406 * Contains the code related with loading/reloading configuration.
407 */
408int parse_config(const char *fname);
409int config_request_add(const char *jsonstr);
410int config_request_del(const char *jsonstr);
411char *config_response(const char *status, const char *error);
412char *config_notify(struct bfd_session *bs);
413char *config_notify_config(const char *op, struct bfd_session *bs);
414
415typedef int (*bpc_handle)(struct bfd_peer_cfg *, void *arg);
416int config_notify_request(struct bfd_control_socket *bcs, const char *jsonstr,
417 bpc_handle bh);
418
419struct peer_label *pl_new(const char *label, struct bfd_session *bs);
420struct peer_label *pl_find(const char *label);
421void pl_free(struct peer_label *pl);
422
423
424/*
425 * log.c
426 *
427 * Contains code that does the logging procedures. Might implement multiple
428 * backends (e.g. zebra log, syslog or other logging lib).
429 */
430enum blog_level {
431 /* level vs syslog equivalent */
432 BLOG_DEBUG = 0, /* LOG_DEBUG */
433 BLOG_INFO = 1, /* LOG_INFO */
434 BLOG_WARNING = 2, /* LOG_WARNING */
435 BLOG_ERROR = 3, /* LOG_ERR */
436 BLOG_FATAL = 4, /* LOG_CRIT */
437};
438
439void log_init(int foreground, enum blog_level level,
440 struct frr_daemon_info *fdi);
441void log_info(const char *fmt, ...);
442void log_debug(const char *fmt, ...);
443void log_warning(const char *fmt, ...);
444void log_error(const char *fmt, ...);
445void log_fatal(const char *fmt, ...);
446
e9e2c950
RZ
447
448/*
449 * bfd_packet.c
450 *
451 * Contains the code related with receiving/seding, packing/unpacking BFD data.
452 */
6e01e275
RZ
453int bp_set_ttlv6(int sd, uint8_t value);
454int bp_set_ttl(int sd, uint8_t value);
455int bp_set_tosv6(int sd, uint8_t value);
456int bp_set_tos(int sd, uint8_t value);
e9e2c950
RZ
457int bp_bind_dev(int sd, const char *dev);
458
459int bp_udp_shop(void);
460int bp_udp_mhop(void);
461int bp_udp6_shop(void);
462int bp_udp6_mhop(void);
463int bp_peer_socket(struct bfd_peer_cfg *bpc);
464int bp_peer_socketv6(struct bfd_peer_cfg *bpc);
2f11c53f
RZ
465int bp_echo_socket(void);
466int bp_echov6_socket(void);
e9e2c950
RZ
467
468void ptm_bfd_snd(struct bfd_session *bfd, int fbit);
469void ptm_bfd_echo_snd(struct bfd_session *bfd);
470
471int bfd_recv_cb(struct thread *t);
472
e9e2c950
RZ
473
474/*
475 * event.c
476 *
477 * Contains the code related with event loop.
478 */
479typedef void (*bfd_ev_cb)(struct thread *t);
480
481void bfd_recvtimer_update(struct bfd_session *bs);
482void bfd_echo_recvtimer_update(struct bfd_session *bs);
483void bfd_xmttimer_update(struct bfd_session *bs, uint64_t jitter);
484void bfd_echo_xmttimer_update(struct bfd_session *bs, uint64_t jitter);
485
486void bfd_xmttimer_delete(struct bfd_session *bs);
487void bfd_echo_xmttimer_delete(struct bfd_session *bs);
488void bfd_recvtimer_delete(struct bfd_session *bs);
489void bfd_echo_recvtimer_delete(struct bfd_session *bs);
490
491void bfd_recvtimer_assign(struct bfd_session *bs, bfd_ev_cb cb, int sd);
492void bfd_echo_recvtimer_assign(struct bfd_session *bs, bfd_ev_cb cb, int sd);
493void bfd_xmttimer_assign(struct bfd_session *bs, bfd_ev_cb cb);
494void bfd_echo_xmttimer_assign(struct bfd_session *bs, bfd_ev_cb cb);
495
496
497/*
498 * bfd.c
499 *
500 * BFD protocol specific code.
501 */
502struct bfd_session *ptm_bfd_sess_new(struct bfd_peer_cfg *bpc);
503int ptm_bfd_ses_del(struct bfd_peer_cfg *bpc);
504void ptm_bfd_ses_dn(struct bfd_session *bfd, uint8_t diag);
505void ptm_bfd_ses_up(struct bfd_session *bfd);
8bd859f6 506void ptm_bfd_echo_stop(struct bfd_session *bfd);
e9e2c950
RZ
507void ptm_bfd_echo_start(struct bfd_session *bfd);
508void ptm_bfd_xmt_TO(struct bfd_session *bfd, int fbit);
509void ptm_bfd_start_xmt_timer(struct bfd_session *bfd, bool is_echo);
b333abc2
RZ
510struct bfd_session *ptm_bfd_sess_find(struct bfd_pkt *, struct sockaddr_any *,
511 struct sockaddr_any *, ifindex_t,
512 vrf_id_t, bool);
e9e2c950
RZ
513
514struct bfd_session *bs_peer_find(struct bfd_peer_cfg *bpc);
515int bfd_session_update_label(struct bfd_session *bs, const char *nlabel);
d3f3a2c4 516void bfd_set_polling(struct bfd_session *bs);
aef131af 517void bs_state_handler(struct bfd_session *, int);
c0ef9a8a
RZ
518void bs_echo_timer_handler(struct bfd_session *);
519void bs_final_handler(struct bfd_session *);
b912b189 520void bs_set_slow_timers(struct bfd_session *bs);
e9e2c950
RZ
521const char *satostr(struct sockaddr_any *sa);
522const char *diag2str(uint8_t diag);
523int strtosa(const char *addr, struct sockaddr_any *sa);
524void integer2timestr(uint64_t time, char *buf, size_t buflen);
03e7f088 525const char *bs_to_string(struct bfd_session *bs);
e9e2c950
RZ
526
527/* BFD hash data structures interface */
528void bfd_initialize(void);
529void bfd_shutdown(void);
530struct bfd_session *bfd_id_lookup(uint32_t id);
531struct bfd_session *bfd_shop_lookup(struct bfd_shop_key shop);
532struct bfd_session *bfd_mhop_lookup(struct bfd_mhop_key mhop);
533struct bfd_vrf *bfd_vrf_lookup(int vrf_id);
534struct bfd_iface *bfd_iface_lookup(const char *ifname);
535
536struct bfd_session *bfd_id_delete(uint32_t id);
537struct bfd_session *bfd_shop_delete(struct bfd_shop_key shop);
538struct bfd_session *bfd_mhop_delete(struct bfd_mhop_key mhop);
539struct bfd_vrf *bfd_vrf_delete(int vrf_id);
540struct bfd_iface *bfd_iface_delete(const char *ifname);
541
542bool bfd_id_insert(struct bfd_session *bs);
543bool bfd_shop_insert(struct bfd_session *bs);
544bool bfd_mhop_insert(struct bfd_session *bs);
545bool bfd_vrf_insert(struct bfd_vrf *vrf);
546bool bfd_iface_insert(struct bfd_iface *iface);
547
548typedef void (*hash_iter_func)(struct hash_backet *hb, void *arg);
549void bfd_id_iterate(hash_iter_func hif, void *arg);
550void bfd_shop_iterate(hash_iter_func hif, void *arg);
551void bfd_mhop_iterate(hash_iter_func hif, void *arg);
552void bfd_vrf_iterate(hash_iter_func hif, void *arg);
553void bfd_iface_iterate(hash_iter_func hif, void *arg);
554
555/* Export callback functions for `event.c`. */
556extern struct thread_master *master;
557
558int bfd_recvtimer_cb(struct thread *t);
559int bfd_echo_recvtimer_cb(struct thread *t);
560int bfd_xmt_cb(struct thread *t);
561int bfd_echo_xmt_cb(struct thread *t);
562
563
c2f29cf3
RZ
564/*
565 * bfdd_vty.c
566 *
567 * BFD daemon vty shell commands.
568 */
569void bfdd_vty_init(void);
570
571
d3af6147
RZ
572/*
573 * ptm_adapter.c
574 */
575void bfdd_zclient_init(struct zebra_privs_t *bfdd_priv);
576void bfdd_zclient_stop(void);
577
578int ptm_bfd_notify(struct bfd_session *bs);
579
e9e2c950 580#endif /* _BFD_H_ */