1 /*
2  *	BIRD -- Bidirectional Forwarding Detection (BFD)
3  *
4  *	Can be freely distributed and used under the terms of the GNU GPL.
5  */
6 
7 #ifndef _BIRD_BFD_H_
8 #define _BIRD_BFD_H_
9 
10 #include <pthread.h>
11 
12 #include "nest/bird.h"
13 #include "nest/cli.h"
14 #include "nest/iface.h"
15 #include "nest/protocol.h"
16 #include "nest/route.h"
17 #include "nest/password.h"
18 #include "conf/conf.h"
19 #include "lib/hash.h"
20 #include "lib/resource.h"
21 #include "lib/socket.h"
22 #include "lib/string.h"
23 
24 #include "nest/bfd.h"
25 #include "io.h"
26 
27 
28 #define BFD_CONTROL_PORT	3784
29 #define BFD_ECHO_PORT		3785
30 #define BFD_MULTI_CTL_PORT	4784
31 
32 #define BFD_DEFAULT_MIN_RX_INT	(10 MS_)
33 #define BFD_DEFAULT_MIN_TX_INT	(100 MS_)
34 #define BFD_DEFAULT_IDLE_TX_INT	(1 S_)
35 #define BFD_DEFAULT_MULTIPLIER	5
36 
37 
38 struct bfd_iface_config;
39 
40 struct bfd_config
41 {
42   struct proto_config c;
43   list patt_list;		/* List of iface configs (struct bfd_iface_config) */
44   list neigh_list;		/* List of configured neighbors (struct bfd_neighbor) */
45   struct bfd_iface_config *multihop; /* Multihop pseudoiface config */
46 };
47 
48 struct bfd_iface_config
49 {
50   struct iface_patt i;
51   u32 min_rx_int;
52   u32 min_tx_int;
53   u32 idle_tx_int;
54   u8 multiplier;
55   u8 passive;
56   u8 auth_type;				/* Authentication type (BFD_AUTH_*) */
57   list *passwords;			/* Passwords for authentication */
58 };
59 
60 struct bfd_neighbor
61 {
62   node n;
63   ip_addr addr;
64   ip_addr local;
65   struct iface *iface;
66 
67   struct neighbor *neigh;
68   struct bfd_request *req;
69 
70   u8 multihop;
71   u8 active;
72 };
73 
74 struct bfd_proto
75 {
76   struct proto p;
77   struct birdloop *loop;
78   pool *tpool;
79   pthread_spinlock_t lock;
80   node bfd_node;
81 
82   slab *session_slab;
83   HASH(struct bfd_session) session_hash_id;
84   HASH(struct bfd_session) session_hash_ip;
85 
86   sock *notify_rs;
87   sock *notify_ws;
88   list notify_list;
89 
90   sock *rx_1;
91   sock *rx_m;
92   list iface_list;
93 };
94 
95 struct bfd_iface
96 {
97   node n;
98   ip_addr local;
99   struct iface *iface;
100   struct bfd_iface_config *cf;
101   struct bfd_proto *bfd;
102 
103   sock *sk;
104   u32 uc;
105   u8 changed;
106 };
107 
108 struct bfd_session
109 {
110   node n;
111   ip_addr addr;				/* Address of session */
112   struct bfd_iface *ifa;		/* Iface associated with session */
113   struct bfd_session *next_id;		/* Next in bfd.session_hash_id */
114   struct bfd_session *next_ip;		/* Next in bfd.session_hash_ip */
115 
116   u8 opened_unused;
117   u8 passive;
118   u8 poll_active;
119   u8 poll_scheduled;
120 
121   u8 loc_state;
122   u8 rem_state;
123   u8 loc_diag;
124   u8 rem_diag;
125   u32 loc_id;				/* Local session ID (local discriminator) */
126   u32 rem_id;				/* Remote session ID (remote discriminator) */
127   u32 des_min_tx_int;			/* Desired min rx interval, local option */
128   u32 des_min_tx_new;			/* Used for des_min_tx_int change */
129   u32 req_min_rx_int;			/* Required min tx interval, local option */
130   u32 req_min_rx_new;			/* Used for req_min_rx_int change */
131   u32 rem_min_tx_int;			/* Last received des_min_tx_int */
132   u32 rem_min_rx_int;			/* Last received req_min_rx_int */
133   u8 demand_mode;			/* Currently unused */
134   u8 rem_demand_mode;
135   u8 detect_mult;			/* Announced detect_mult, local option */
136   u8 rem_detect_mult;			/* Last received detect_mult */
137 
138   btime last_tx;			/* Time of last sent periodic control packet */
139   btime last_rx;			/* Time of last received valid control packet */
140 
141   timer2 *tx_timer;			/* Periodic control packet timer */
142   timer2 *hold_timer;			/* Timer for session down detection time */
143 
144   list request_list;			/* List of client requests (struct bfd_request) */
145   bird_clock_t last_state_change;	/* Time of last state change */
146   u8 notify_running;			/* 1 if notify hooks are running */
147 
148   u8 rx_csn_known;			/* Received crypto sequence number is known */
149   u32 rx_csn;				/* Last received crypto sequence number */
150   u32 tx_csn;				/* Last transmitted crypto sequence number */
151   u32 tx_csn_time;			/* Timestamp of last tx_csn change */
152 };
153 
154 
155 extern const char *bfd_state_names[];
156 
157 #define BFD_STATE_ADMIN_DOWN	0
158 #define BFD_STATE_DOWN		1
159 #define BFD_STATE_INIT		2
160 #define BFD_STATE_UP		3
161 
162 #define BFD_DIAG_NOTHING	0
163 #define BFD_DIAG_TIMEOUT	1
164 #define BFD_DIAG_ECHO_FAILED	2
165 #define BFD_DIAG_NEIGHBOR_DOWN	3
166 #define BFD_DIAG_FWD_RESET	4
167 #define BFD_DIAG_PATH_DOWN	5
168 #define BFD_DIAG_C_PATH_DOWN	6
169 #define BFD_DIAG_ADMIN_DOWN	7
170 #define BFD_DIAG_RC_PATH_DOWN	8
171 
172 #define BFD_POLL_TX		1
173 #define BFD_POLL_RX		2
174 
175 #define BFD_FLAGS		0x3f
176 #define BFD_FLAG_POLL		(1 << 5)
177 #define BFD_FLAG_FINAL		(1 << 4)
178 #define BFD_FLAG_CPI		(1 << 3)
179 #define BFD_FLAG_AP		(1 << 2)
180 #define BFD_FLAG_DEMAND		(1 << 1)
181 #define BFD_FLAG_MULTIPOINT	(1 << 0)
182 
183 #define BFD_AUTH_NONE			0
184 #define BFD_AUTH_SIMPLE			1
185 #define BFD_AUTH_KEYED_MD5		2
186 #define BFD_AUTH_METICULOUS_KEYED_MD5	3
187 #define BFD_AUTH_KEYED_SHA1		4
188 #define BFD_AUTH_METICULOUS_KEYED_SHA1	5
189 
190 extern const u8 bfd_auth_type_to_hash_alg[];
191 
192 
bfd_lock_sessions(struct bfd_proto * p)193 static inline void bfd_lock_sessions(struct bfd_proto *p) { pthread_spin_lock(&p->lock); }
bfd_unlock_sessions(struct bfd_proto * p)194 static inline void bfd_unlock_sessions(struct bfd_proto *p) { pthread_spin_unlock(&p->lock); }
195 
196 /* bfd.c */
197 struct bfd_session * bfd_find_session_by_id(struct bfd_proto *p, u32 id);
198 struct bfd_session * bfd_find_session_by_addr(struct bfd_proto *p, ip_addr addr);
199 void bfd_session_process_ctl(struct bfd_session *s, u8 flags, u32 old_tx_int, u32 old_rx_int);
200 void bfd_show_sessions(struct proto *P);
201 
202 /* packets.c */
203 void bfd_send_ctl(struct bfd_proto *p, struct bfd_session *s, int final);
204 sock * bfd_open_rx_sk(struct bfd_proto *p, int multihop);
205 sock * bfd_open_tx_sk(struct bfd_proto *p, ip_addr local, struct iface *ifa);
206 
207 
208 #endif /* _BIRD_BFD_H_ */
209