]>
Commit | Line | Data |
---|---|---|
2638249d MM |
1 | /* |
2 | * BIRD -- The Border Gateway Protocol | |
3 | * | |
4 | * (c) 2000 Martin Mares <mj@ucw.cz> | |
d15b0b0a OZ |
5 | * (c) 2008--2016 Ondrej Zajicek <santiago@crfreenet.org> |
6 | * (c) 2008--2016 CZ.NIC z.s.p.o. | |
2638249d MM |
7 | * |
8 | * Can be freely distributed and used under the terms of the GNU GPL. | |
9 | */ | |
10 | ||
54e55169 MM |
11 | /** |
12 | * DOC: Border Gateway Protocol | |
13 | * | |
d15b0b0a OZ |
14 | * The BGP protocol is implemented in three parts: |bgp.c| which takes care of |
15 | * the connection and most of the interface with BIRD core, |packets.c| handling | |
54e55169 MM |
16 | * both incoming and outgoing BGP packets and |attrs.c| containing functions for |
17 | * manipulation with BGP attribute lists. | |
18 | * | |
d15b0b0a OZ |
19 | * As opposed to the other existing routing daemons, BIRD has a sophisticated |
20 | * core architecture which is able to keep all the information needed by BGP in | |
21 | * the primary routing table, therefore no complex data structures like a | |
22 | * central BGP table are needed. This increases memory footprint of a BGP router | |
23 | * with many connections, but not too much and, which is more important, it | |
24 | * makes BGP much easier to implement. | |
54e55169 | 25 | * |
d15b0b0a OZ |
26 | * Each instance of BGP (corresponding to a single BGP peer) is described by a |
27 | * &bgp_proto structure to which are attached individual connections represented | |
28 | * by &bgp_connection (usually, there exists only one connection, but during BGP | |
29 | * session setup, there can be more of them). The connections are handled | |
30 | * according to the BGP state machine defined in the RFC with all the timers and | |
31 | * all the parameters configurable. | |
54e55169 | 32 | * |
d15b0b0a OZ |
33 | * In incoming direction, we listen on the connection's socket and each time we |
34 | * receive some input, we pass it to bgp_rx(). It decodes packet headers and the | |
35 | * markers and passes complete packets to bgp_rx_packet() which distributes the | |
36 | * packet according to its type. | |
54e55169 | 37 | * |
d15b0b0a OZ |
38 | * In outgoing direction, we gather all the routing updates and sort them to |
39 | * buckets (&bgp_bucket) according to their attributes (we keep a hash table for | |
40 | * fast comparison of &rta's and a &fib which helps us to find if we already | |
41 | * have another route for the same destination queued for sending, so that we | |
42 | * can replace it with the new one immediately instead of sending both | |
43 | * updates). There also exists a special bucket holding all the route | |
44 | * withdrawals which cannot be queued anywhere else as they don't have any | |
45 | * attributes. If we have any packet to send (due to either new routes or the | |
46 | * connection tracking code wanting to send a Open, Keepalive or Notification | |
47 | * message), we call bgp_schedule_packet() which sets the corresponding bit in a | |
48 | * @packet_to_send bit field in &bgp_conn and as soon as the transmit socket | |
49 | * buffer becomes empty, we call bgp_fire_tx(). It inspects state of all the | |
50 | * packet type bits and calls the corresponding bgp_create_xx() functions, | |
51 | * eventually rescheduling the same packet type if we have more data of the same | |
52 | * type to send. | |
54e55169 | 53 | * |
d15b0b0a OZ |
54 | * The processing of attributes consists of two functions: bgp_decode_attrs() |
55 | * for checking of the attribute blocks and translating them to the language of | |
56 | * BIRD's extended attributes and bgp_encode_attrs() which does the | |
57 | * converse. Both functions are built around a @bgp_attr_table array describing | |
58 | * all important characteristics of all known attributes. Unknown transitive | |
59 | * attributes are attached to the route as %EAF_TYPE_OPAQUE byte streams. | |
6eda3f13 OZ |
60 | * |
61 | * BGP protocol implements graceful restart in both restarting (local restart) | |
62 | * and receiving (neighbor restart) roles. The first is handled mostly by the | |
63 | * graceful restart code in the nest, BGP protocol just handles capabilities, | |
64 | * sets @gr_wait and locks graceful restart until end-of-RIB mark is received. | |
65 | * The second is implemented by internal restart of the BGP state to %BS_IDLE | |
66 | * and protocol state to %PS_START, but keeping the protocol up from the core | |
67 | * point of view and therefore maintaining received routes. Routing table | |
68 | * refresh cycle (rt_refresh_begin(), rt_refresh_end()) is used for removing | |
69 | * stale routes after reestablishment of BGP session during graceful restart. | |
c49e4a65 OZ |
70 | * |
71 | * Supported standards: | |
72 | * <itemize> | |
73 | * <item> <rfc id="4271"> - Border Gateway Protocol 4 (BGP) | |
74 | * <item> <rfc id="1997"> - BGP Communities Attribute | |
75 | * <item> <rfc id="2385"> - Protection of BGP Sessions via TCP MD5 Signature | |
76 | * <item> <rfc id="2545"> - Use of BGP Multiprotocol Extensions for IPv6 | |
77 | * <item> <rfc id="2918"> - Route Refresh Capability | |
78 | * <item> <rfc id="3107"> - Carrying Label Information in BGP | |
79 | * <item> <rfc id="4360"> - BGP Extended Communities Attribute | |
80 | * <item> <rfc id="4364"> - BGP/MPLS IPv4 Virtual Private Networks | |
81 | * <item> <rfc id="4456"> - BGP Route Reflection | |
82 | * <item> <rfc id="4486"> - Subcodes for BGP Cease Notification Message | |
83 | * <item> <rfc id="4659"> - BGP/MPLS IPv6 Virtual Private Networks | |
84 | * <item> <rfc id="4724"> - Graceful Restart Mechanism for BGP | |
85 | * <item> <rfc id="4760"> - Multiprotocol extensions for BGP | |
86 | * <item> <rfc id="4798"> - Connecting IPv6 Islands over IPv4 MPLS | |
87 | * <item> <rfc id="5065"> - AS confederations for BGP | |
88 | * <item> <rfc id="5082"> - Generalized TTL Security Mechanism | |
89 | * <item> <rfc id="5492"> - Capabilities Advertisement with BGP | |
90 | * <item> <rfc id="5549"> - Advertising IPv4 NLRI with an IPv6 Next Hop | |
91 | * <item> <rfc id="5575"> - Dissemination of Flow Specification Rules | |
92 | * <item> <rfc id="5668"> - 4-Octet AS Specific BGP Extended Community | |
93 | * <item> <rfc id="6286"> - AS-Wide Unique BGP Identifier | |
94 | * <item> <rfc id="6608"> - Subcodes for BGP Finite State Machine Error | |
95 | * <item> <rfc id="6793"> - BGP Support for 4-Octet AS Numbers | |
96 | * <item> <rfc id="7313"> - Enhanced Route Refresh Capability for BGP | |
97 | * <item> <rfc id="7606"> - Revised Error Handling for BGP UPDATE Messages | |
98 | * <item> <rfc id="7911"> - Advertisement of Multiple Paths in BGP | |
99 | * <item> <rfc id="7947"> - Internet Exchange BGP Route Server | |
100 | * <item> <rfc id="8092"> - BGP Large Communities Attribute | |
7fc55925 | 101 | * <item> <rfc id="8203"> - BGP Administrative Shutdown Communication |
3831b619 | 102 | * <item> <rfc id="8212"> - Default EBGP Route Propagation Behavior without Policies |
c49e4a65 OZ |
103 | * </itemize> |
104 | */ | |
54e55169 | 105 | |
48d79d52 | 106 | #undef LOCAL_DEBUG |
2638249d | 107 | |
02552526 OZ |
108 | #include <stdlib.h> |
109 | ||
2638249d MM |
110 | #include "nest/bird.h" |
111 | #include "nest/iface.h" | |
112 | #include "nest/protocol.h" | |
113 | #include "nest/route.h" | |
b8113a5e | 114 | #include "nest/cli.h" |
1ec52253 | 115 | #include "nest/locks.h" |
2638249d | 116 | #include "conf/conf.h" |
3831b619 | 117 | #include "filter/filter.h" |
c01e3741 | 118 | #include "lib/socket.h" |
973399ae | 119 | #include "lib/resource.h" |
7d875e09 | 120 | #include "lib/string.h" |
2638249d MM |
121 | |
122 | #include "bgp.h" | |
123 | ||
e7d2ac44 | 124 | |
973399ae | 125 | struct linpool *bgp_linpool; /* Global temporary pool */ |
1e37e35c | 126 | struct linpool *bgp_linpool2; /* Global temporary pool for bgp_rt_notify() */ |
d15b0b0a OZ |
127 | static list bgp_sockets; /* Global list of listening sockets */ |
128 | ||
c01e3741 | 129 | |
c01e3741 | 130 | static void bgp_connect(struct bgp_proto *p); |
dd91e467 | 131 | static void bgp_active(struct bgp_proto *p); |
1ec52253 | 132 | static void bgp_update_bfd(struct bgp_proto *p, int use_bfd); |
2638249d | 133 | |
d15b0b0a OZ |
134 | static int bgp_incoming_connection(sock *sk, uint dummy UNUSED); |
135 | static void bgp_listen_sock_err(sock *sk UNUSED, int err); | |
11cb6202 | 136 | |
11b32d91 OZ |
137 | /** |
138 | * bgp_open - open a BGP instance | |
139 | * @p: BGP instance | |
140 | * | |
d15b0b0a OZ |
141 | * This function allocates and configures shared BGP resources, mainly listening |
142 | * sockets. Should be called as the last step during initialization (when lock | |
143 | * is acquired and neighbor is ready). When error, caller should change state to | |
144 | * PS_DOWN and return immediately. | |
11b32d91 OZ |
145 | */ |
146 | static int | |
147 | bgp_open(struct bgp_proto *p) | |
148 | { | |
d15b0b0a OZ |
149 | struct bgp_socket *bs = NULL; |
150 | struct iface *ifa = p->cf->strict_bind ? p->cf->iface : NULL; | |
151 | ip_addr addr = p->cf->strict_bind ? p->cf->local_ip : | |
152 | (ipa_is_ip4(p->cf->remote_ip) ? IPA_NONE4 : IPA_NONE6); | |
153 | uint port = p->cf->local_port; | |
154 | ||
155 | /* FIXME: Add some global init? */ | |
156 | if (!bgp_linpool) | |
157 | init_list(&bgp_sockets); | |
b1b19433 | 158 | |
d15b0b0a | 159 | /* We assume that cf->iface is defined iff cf->local_ip is link-local */ |
11b32d91 | 160 | |
d15b0b0a | 161 | WALK_LIST(bs, bgp_sockets) |
e19d0805 OZ |
162 | if (ipa_equal(bs->sk->saddr, addr) && (bs->sk->sport == port) && |
163 | (bs->sk->iface == ifa) && (bs->sk->vrf == p->p.vrf)) | |
a34b0934 | 164 | { |
d15b0b0a OZ |
165 | bs->uc++; |
166 | p->sock = bs; | |
167 | return 0; | |
a34b0934 OZ |
168 | } |
169 | ||
d15b0b0a OZ |
170 | sock *sk = sk_new(proto_pool); |
171 | sk->type = SK_TCP_PASSIVE; | |
172 | sk->ttl = 255; | |
173 | sk->saddr = addr; | |
174 | sk->sport = port; | |
e19d0805 OZ |
175 | sk->iface = ifa; |
176 | sk->vrf = p->p.vrf; | |
d15b0b0a OZ |
177 | sk->flags = 0; |
178 | sk->tos = IP_PREC_INTERNET_CONTROL; | |
179 | sk->rbsize = BGP_RX_BUFFER_SIZE; | |
180 | sk->tbsize = BGP_TX_BUFFER_SIZE; | |
181 | sk->rx_hook = bgp_incoming_connection; | |
182 | sk->err_hook = bgp_listen_sock_err; | |
183 | ||
184 | if (sk_open(sk) < 0) | |
185 | goto err; | |
11b32d91 | 186 | |
d15b0b0a OZ |
187 | bs = mb_allocz(proto_pool, sizeof(struct bgp_socket)); |
188 | bs->sk = sk; | |
189 | bs->uc = 1; | |
190 | p->sock = bs; | |
05476c4d | 191 | |
d15b0b0a OZ |
192 | add_tail(&bgp_sockets, &bs->n); |
193 | ||
194 | if (!bgp_linpool) | |
1e37e35c | 195 | { |
05d47bd5 JMM |
196 | bgp_linpool = lp_new_default(proto_pool); |
197 | bgp_linpool2 = lp_new_default(proto_pool); | |
1e37e35c | 198 | } |
11b32d91 | 199 | |
11b32d91 | 200 | return 0; |
b1b19433 OZ |
201 | |
202 | err: | |
d15b0b0a OZ |
203 | sk_log_error(sk, p->p.name); |
204 | log(L_ERR "%s: Cannot open listening socket", p->p.name); | |
205 | rfree(sk); | |
b1b19433 | 206 | return -1; |
11b32d91 OZ |
207 | } |
208 | ||
d15b0b0a OZ |
209 | /** |
210 | * bgp_close - close a BGP instance | |
211 | * @p: BGP instance | |
212 | * | |
213 | * This function frees and deconfigures shared BGP resources. | |
214 | */ | |
215 | static void | |
216 | bgp_close(struct bgp_proto *p) | |
217 | { | |
218 | struct bgp_socket *bs = p->sock; | |
219 | ||
220 | ASSERT(bs && bs->uc); | |
221 | ||
222 | if (--bs->uc) | |
223 | return; | |
224 | ||
225 | rfree(bs->sk); | |
226 | rem_node(&bs->n); | |
227 | mb_free(bs); | |
228 | ||
229 | if (!EMPTY_LIST(bgp_sockets)) | |
230 | return; | |
231 | ||
232 | rfree(bgp_linpool); | |
233 | bgp_linpool = NULL; | |
1e37e35c OZ |
234 | |
235 | rfree(bgp_linpool2); | |
236 | bgp_linpool2 = NULL; | |
d15b0b0a OZ |
237 | } |
238 | ||
239 | static inline int | |
240 | bgp_setup_auth(struct bgp_proto *p, int enable) | |
241 | { | |
242 | if (p->cf->password) | |
243 | { | |
244 | int rv = sk_set_md5_auth(p->sock->sk, | |
245 | p->cf->local_ip, p->cf->remote_ip, p->cf->iface, | |
246 | enable ? p->cf->password : NULL, p->cf->setkey); | |
247 | ||
248 | if (rv < 0) | |
249 | sk_log_error(p->sock->sk, p->p.name); | |
250 | ||
251 | return rv; | |
252 | } | |
253 | else | |
254 | return 0; | |
255 | } | |
256 | ||
257 | static inline struct bgp_channel * | |
258 | bgp_find_channel(struct bgp_proto *p, u32 afi) | |
259 | { | |
260 | struct bgp_channel *c; | |
261 | WALK_LIST(c, p->p.channels) | |
262 | if (c->afi == afi) | |
263 | return c; | |
264 | ||
265 | return NULL; | |
266 | } | |
267 | ||
dd91e467 OZ |
268 | static void |
269 | bgp_startup(struct bgp_proto *p) | |
270 | { | |
271 | BGP_TRACE(D_EVENTS, "Started"); | |
d15b0b0a | 272 | p->start_state = BSS_CONNECT; |
be6e39eb OZ |
273 | |
274 | if (!p->cf->passive) | |
275 | bgp_active(p); | |
dd91e467 OZ |
276 | } |
277 | ||
278 | static void | |
279 | bgp_startup_timeout(timer *t) | |
280 | { | |
281 | bgp_startup(t->data); | |
282 | } | |
283 | ||
284 | ||
285 | static void | |
286 | bgp_initiate(struct bgp_proto *p) | |
287 | { | |
d15b0b0a OZ |
288 | int err_val; |
289 | ||
290 | if (bgp_open(p) < 0) | |
291 | { err_val = BEM_NO_SOCKET; goto err1; } | |
292 | ||
293 | if (bgp_setup_auth(p, 1) < 0) | |
294 | { err_val = BEM_INVALID_MD5; goto err2; } | |
9be9a264 | 295 | |
1ec52253 OZ |
296 | if (p->cf->bfd) |
297 | bgp_update_bfd(p, p->cf->bfd); | |
298 | ||
dd91e467 | 299 | if (p->startup_delay) |
d15b0b0a OZ |
300 | { |
301 | p->start_state = BSS_DELAY; | |
302 | BGP_TRACE(D_EVENTS, "Startup delayed by %d seconds due to errors", p->startup_delay); | |
303 | bgp_start_timer(p->startup_timer, p->startup_delay); | |
304 | } | |
dd91e467 OZ |
305 | else |
306 | bgp_startup(p); | |
dd91e467 | 307 | |
d15b0b0a | 308 | return; |
d51aa281 | 309 | |
d15b0b0a OZ |
310 | err2: |
311 | bgp_close(p); | |
312 | err1: | |
313 | p->p.disabled = 1; | |
314 | bgp_store_error(p, NULL, BE_MISC, err_val); | |
315 | proto_notify_state(&p->p, PS_DOWN); | |
d51aa281 | 316 | |
d15b0b0a | 317 | return; |
c01e3741 MM |
318 | } |
319 | ||
54e55169 MM |
320 | /** |
321 | * bgp_start_timer - start a BGP timer | |
322 | * @t: timer | |
cc881bd1 | 323 | * @value: time (in seconds) to fire (0 to disable the timer) |
54e55169 | 324 | * |
d15b0b0a OZ |
325 | * This functions calls tm_start() on @t with time @value and the amount of |
326 | * randomization suggested by the BGP standard. Please use it for all BGP | |
327 | * timers. | |
54e55169 | 328 | */ |
3fdbafb6 | 329 | void |
cc881bd1 | 330 | bgp_start_timer(timer *t, uint value) |
c01e3741 | 331 | { |
3fdbafb6 | 332 | if (value) |
d15b0b0a | 333 | { |
cc881bd1 OZ |
334 | /* The randomization procedure is specified in RFC 4271 section 10 */ |
335 | btime time = value S; | |
336 | btime randomize = random() % ((time / 4) + 1); | |
a6f79ca5 | 337 | tm_start(t, time - randomize); |
d15b0b0a | 338 | } |
b552ecc4 | 339 | else |
a6f79ca5 | 340 | tm_stop(t); |
b552ecc4 MM |
341 | } |
342 | ||
54e55169 MM |
343 | /** |
344 | * bgp_close_conn - close a BGP connection | |
345 | * @conn: connection to close | |
346 | * | |
d15b0b0a OZ |
347 | * This function takes a connection described by the &bgp_conn structure, closes |
348 | * its socket and frees all resources associated with it. | |
54e55169 | 349 | */ |
b552ecc4 MM |
350 | void |
351 | bgp_close_conn(struct bgp_conn *conn) | |
352 | { | |
e81b440f | 353 | // struct bgp_proto *p = conn->bgp; |
b552ecc4 MM |
354 | |
355 | DBG("BGP: Closing connection\n"); | |
356 | conn->packets_to_send = 0; | |
d15b0b0a OZ |
357 | conn->channels_to_send = 0; |
358 | rfree(conn->connect_timer); | |
359 | conn->connect_timer = NULL; | |
b552ecc4 MM |
360 | rfree(conn->keepalive_timer); |
361 | conn->keepalive_timer = NULL; | |
362 | rfree(conn->hold_timer); | |
363 | conn->hold_timer = NULL; | |
11b32d91 OZ |
364 | rfree(conn->tx_ev); |
365 | conn->tx_ev = NULL; | |
d15b0b0a OZ |
366 | rfree(conn->sk); |
367 | conn->sk = NULL; | |
368 | ||
369 | mb_free(conn->local_caps); | |
370 | conn->local_caps = NULL; | |
371 | mb_free(conn->remote_caps); | |
372 | conn->remote_caps = NULL; | |
11b32d91 OZ |
373 | } |
374 | ||
375 | ||
376 | /** | |
377 | * bgp_update_startup_delay - update a startup delay | |
378 | * @p: BGP instance | |
11b32d91 | 379 | * |
d15b0b0a OZ |
380 | * This function updates a startup delay that is used to postpone next BGP |
381 | * connect. It also handles disable_after_error and might stop BGP instance | |
382 | * when error happened and disable_after_error is on. | |
11b32d91 OZ |
383 | * |
384 | * It should be called when BGP protocol error happened. | |
385 | */ | |
386 | void | |
b99d3786 | 387 | bgp_update_startup_delay(struct bgp_proto *p) |
11b32d91 OZ |
388 | { |
389 | struct bgp_config *cf = p->cf; | |
390 | ||
b99d3786 | 391 | DBG("BGP: Updating startup delay\n"); |
11b32d91 | 392 | |
cc881bd1 | 393 | if (p->last_proto_error && ((current_time() - p->last_proto_error) >= cf->error_amnesia_time S)) |
72382626 OZ |
394 | p->startup_delay = 0; |
395 | ||
cc881bd1 | 396 | p->last_proto_error = current_time(); |
11b32d91 OZ |
397 | |
398 | if (cf->disable_after_error) | |
d15b0b0a OZ |
399 | { |
400 | p->startup_delay = 0; | |
401 | p->p.disabled = 1; | |
402 | return; | |
403 | } | |
11b32d91 | 404 | |
11b32d91 OZ |
405 | if (!p->startup_delay) |
406 | p->startup_delay = cf->error_delay_time_min; | |
407 | else | |
b99d3786 | 408 | p->startup_delay = MIN(2 * p->startup_delay, cf->error_delay_time_max); |
c01e3741 MM |
409 | } |
410 | ||
11b32d91 | 411 | static void |
cd1d9961 | 412 | bgp_graceful_close_conn(struct bgp_conn *conn, uint subcode, byte *data, uint len) |
48e842cc | 413 | { |
11b32d91 | 414 | switch (conn->state) |
d15b0b0a OZ |
415 | { |
416 | case BS_IDLE: | |
417 | case BS_CLOSE: | |
418 | return; | |
419 | ||
420 | case BS_CONNECT: | |
421 | case BS_ACTIVE: | |
422 | bgp_conn_enter_idle_state(conn); | |
423 | return; | |
424 | ||
425 | case BS_OPENSENT: | |
426 | case BS_OPENCONFIRM: | |
427 | case BS_ESTABLISHED: | |
830ba75e | 428 | bgp_error(conn, 6, subcode, data, len); |
d15b0b0a OZ |
429 | return; |
430 | ||
431 | default: | |
432 | bug("bgp_graceful_close_conn: Unknown state %d", conn->state); | |
433 | } | |
48e842cc MM |
434 | } |
435 | ||
11b32d91 OZ |
436 | static void |
437 | bgp_down(struct bgp_proto *p) | |
438 | { | |
439 | if (p->start_state > BSS_PREPARE) | |
d15b0b0a OZ |
440 | { |
441 | bgp_setup_auth(p, 0); | |
442 | bgp_close(p); | |
443 | } | |
11b32d91 | 444 | |
b99d3786 | 445 | BGP_TRACE(D_EVENTS, "Down"); |
11b32d91 OZ |
446 | proto_notify_state(&p->p, PS_DOWN); |
447 | } | |
448 | ||
449 | static void | |
450 | bgp_decision(void *vp) | |
451 | { | |
452 | struct bgp_proto *p = vp; | |
453 | ||
454 | DBG("BGP: Decision start\n"); | |
d15b0b0a OZ |
455 | if ((p->p.proto_state == PS_START) && |
456 | (p->outgoing_conn.state == BS_IDLE) && | |
457 | (p->incoming_conn.state != BS_OPENCONFIRM) && | |
458 | !p->cf->passive) | |
dd91e467 | 459 | bgp_active(p); |
11b32d91 | 460 | |
d15b0b0a OZ |
461 | if ((p->p.proto_state == PS_STOP) && |
462 | (p->outgoing_conn.state == BS_IDLE) && | |
463 | (p->incoming_conn.state == BS_IDLE)) | |
11b32d91 OZ |
464 | bgp_down(p); |
465 | } | |
466 | ||
b99d3786 | 467 | void |
cd1d9961 | 468 | bgp_stop(struct bgp_proto *p, uint subcode, byte *data, uint len) |
11b32d91 OZ |
469 | { |
470 | proto_notify_state(&p->p, PS_STOP); | |
cd1d9961 OZ |
471 | bgp_graceful_close_conn(&p->outgoing_conn, subcode, data, len); |
472 | bgp_graceful_close_conn(&p->incoming_conn, subcode, data, len); | |
11b32d91 OZ |
473 | ev_schedule(p->event); |
474 | } | |
475 | ||
cf31112f | 476 | static inline void |
d15b0b0a | 477 | bgp_conn_set_state(struct bgp_conn *conn, uint new_state) |
cf31112f OZ |
478 | { |
479 | if (conn->bgp->p.mrtdump & MD_STATES) | |
863ecfc7 | 480 | bgp_dump_state_change(conn, conn->state, new_state); |
cf31112f OZ |
481 | |
482 | conn->state = new_state; | |
483 | } | |
484 | ||
485 | void | |
486 | bgp_conn_enter_openconfirm_state(struct bgp_conn *conn) | |
487 | { | |
488 | /* Really, most of the work is done in bgp_rx_open(). */ | |
489 | bgp_conn_set_state(conn, BS_OPENCONFIRM); | |
490 | } | |
491 | ||
d15b0b0a OZ |
492 | static const struct bgp_af_caps dummy_af_caps = { }; |
493 | ||
11b32d91 OZ |
494 | void |
495 | bgp_conn_enter_established_state(struct bgp_conn *conn) | |
496 | { | |
497 | struct bgp_proto *p = conn->bgp; | |
d15b0b0a OZ |
498 | struct bgp_caps *local = conn->local_caps; |
499 | struct bgp_caps *peer = conn->remote_caps; | |
500 | struct bgp_channel *c; | |
523f020b | 501 | |
11b32d91 | 502 | BGP_TRACE(D_EVENTS, "BGP session established"); |
11b32d91 | 503 | |
9be9a264 OZ |
504 | /* For multi-hop BGP sessions */ |
505 | if (ipa_zero(p->source_addr)) | |
0c791f87 | 506 | p->source_addr = conn->sk->saddr; |
9be9a264 | 507 | |
9e7b3ebd OZ |
508 | conn->sk->fast_rx = 0; |
509 | ||
11b32d91 OZ |
510 | p->conn = conn; |
511 | p->last_error_class = 0; | |
512 | p->last_error_code = 0; | |
094d2bdb | 513 | |
d15b0b0a OZ |
514 | p->as4_session = conn->as4_session; |
515 | ||
516 | p->route_refresh = peer->route_refresh; | |
517 | p->enhanced_refresh = local->enhanced_refresh && peer->enhanced_refresh; | |
0c791f87 | 518 | |
5bd73431 OZ |
519 | /* Whether we may handle possible GR/LLGR of peer (it has some AF GR-able) */ |
520 | p->gr_ready = p->llgr_ready = 0; /* Updated later */ | |
0c791f87 | 521 | |
d15b0b0a OZ |
522 | /* Whether peer is ready to handle our GR recovery */ |
523 | int peer_gr_ready = peer->gr_aware && !(peer->gr_flags & BGP_GRF_RESTART); | |
0c791f87 | 524 | |
d15b0b0a | 525 | if (p->gr_active_num) |
a6f79ca5 | 526 | tm_stop(p->gr_timer); |
0c791f87 | 527 | |
d15b0b0a OZ |
528 | /* Number of active channels */ |
529 | int num = 0; | |
530 | ||
863ecfc7 OZ |
531 | /* Summary state of ADD_PATH RX for active channels */ |
532 | uint summary_add_path_rx = 0; | |
533 | ||
d15b0b0a OZ |
534 | WALK_LIST(c, p->p.channels) |
535 | { | |
536 | const struct bgp_af_caps *loc = bgp_find_af_caps(local, c->afi); | |
537 | const struct bgp_af_caps *rem = bgp_find_af_caps(peer, c->afi); | |
538 | ||
539 | /* Ignore AFIs that were not announced in multiprotocol capability */ | |
540 | if (!loc || !loc->ready) | |
541 | loc = &dummy_af_caps; | |
542 | ||
543 | if (!rem || !rem->ready) | |
544 | rem = &dummy_af_caps; | |
545 | ||
546 | int active = loc->ready && rem->ready; | |
547 | c->c.disabled = !active; | |
548 | c->c.reloadable = p->route_refresh; | |
549 | ||
550 | c->index = active ? num++ : 0; | |
551 | ||
552 | c->feed_state = BFS_NONE; | |
553 | c->load_state = BFS_NONE; | |
554 | ||
555 | /* Channels where peer may do GR */ | |
5bd73431 OZ |
556 | uint gr_ready = active && local->gr_aware && rem->gr_able; |
557 | uint llgr_ready = active && local->llgr_aware && rem->llgr_able; | |
558 | ||
559 | c->gr_ready = gr_ready || llgr_ready; | |
d15b0b0a | 560 | p->gr_ready = p->gr_ready || c->gr_ready; |
5bd73431 OZ |
561 | p->llgr_ready = p->llgr_ready || llgr_ready; |
562 | ||
563 | /* Remember last LLGR stale time */ | |
564 | c->stale_time = local->llgr_aware ? rem->llgr_time : 0; | |
0c791f87 | 565 | |
d15b0b0a OZ |
566 | /* Channels not able to recover gracefully */ |
567 | if (p->p.gr_recovery && (!active || !peer_gr_ready)) | |
568 | channel_graceful_restart_unlock(&c->c); | |
9aed29e6 | 569 | |
d15b0b0a OZ |
570 | /* Channels waiting for local convergence */ |
571 | if (p->p.gr_recovery && loc->gr_able && peer_gr_ready) | |
572 | c->c.gr_wait = 1; | |
573 | ||
5bd73431 OZ |
574 | /* Channels where regular graceful restart failed */ |
575 | if ((c->gr_active == BGP_GRS_ACTIVE) && | |
576 | !(active && rem->gr_able && (rem->gr_af_flags & BGP_GRF_FORWARDING))) | |
577 | bgp_graceful_restart_done(c); | |
578 | ||
579 | /* Channels where regular long-lived restart failed */ | |
580 | if ((c->gr_active == BGP_GRS_LLGR) && | |
581 | !(active && rem->llgr_able && (rem->gr_af_flags & BGP_LLGRF_FORWARDING))) | |
d15b0b0a OZ |
582 | bgp_graceful_restart_done(c); |
583 | ||
584 | /* GR capability implies that neighbor will send End-of-RIB */ | |
585 | if (peer->gr_aware) | |
586 | c->load_state = BFS_LOADING; | |
587 | ||
d8022d26 | 588 | c->ext_next_hop = c->cf->ext_next_hop && (bgp_channel_is_ipv6(c) || rem->ext_next_hop); |
d15b0b0a OZ |
589 | c->add_path_rx = (loc->add_path & BGP_ADD_PATH_RX) && (rem->add_path & BGP_ADD_PATH_TX); |
590 | c->add_path_tx = (loc->add_path & BGP_ADD_PATH_TX) && (rem->add_path & BGP_ADD_PATH_RX); | |
591 | ||
863ecfc7 OZ |
592 | if (active) |
593 | summary_add_path_rx |= !c->add_path_rx ? 1 : 2; | |
594 | ||
f8aad5d5 | 595 | /* Update RA mode */ |
d15b0b0a OZ |
596 | if (c->add_path_tx) |
597 | c->c.ra_mode = RA_ANY; | |
f8aad5d5 OZ |
598 | else if (c->cf->secondary) |
599 | c->c.ra_mode = RA_ACCEPTED; | |
600 | else | |
601 | c->c.ra_mode = RA_OPTIMAL; | |
d15b0b0a OZ |
602 | } |
603 | ||
604 | p->afi_map = mb_alloc(p->p.pool, num * sizeof(u32)); | |
605 | p->channel_map = mb_alloc(p->p.pool, num * sizeof(void *)); | |
606 | p->channel_count = num; | |
863ecfc7 | 607 | p->summary_add_path_rx = summary_add_path_rx; |
d15b0b0a OZ |
608 | |
609 | WALK_LIST(c, p->p.channels) | |
610 | { | |
611 | if (c->c.disabled) | |
612 | continue; | |
613 | ||
614 | p->afi_map[c->index] = c->afi; | |
615 | p->channel_map[c->index] = c; | |
616 | } | |
617 | ||
618 | /* proto_notify_state() will likely call bgp_feed_begin(), setting c->feed_state */ | |
9aed29e6 | 619 | |
cf31112f | 620 | bgp_conn_set_state(conn, BS_ESTABLISHED); |
11b32d91 OZ |
621 | proto_notify_state(&p->p, PS_UP); |
622 | } | |
623 | ||
624 | static void | |
625 | bgp_conn_leave_established_state(struct bgp_proto *p) | |
626 | { | |
627 | BGP_TRACE(D_EVENTS, "BGP session closed"); | |
628 | p->conn = NULL; | |
629 | ||
630 | if (p->p.proto_state == PS_UP) | |
cd1d9961 | 631 | bgp_stop(p, 0, NULL, 0); |
11b32d91 OZ |
632 | } |
633 | ||
634 | void | |
635 | bgp_conn_enter_close_state(struct bgp_conn *conn) | |
636 | { | |
637 | struct bgp_proto *p = conn->bgp; | |
638 | int os = conn->state; | |
639 | ||
cf31112f | 640 | bgp_conn_set_state(conn, BS_CLOSE); |
a6f79ca5 | 641 | tm_stop(conn->keepalive_timer); |
11b32d91 OZ |
642 | conn->sk->rx_hook = NULL; |
643 | ||
48b15ef1 OZ |
644 | /* Timeout for CLOSE state, if we cannot send notification soon then we just hangup */ |
645 | bgp_start_timer(conn->hold_timer, 10); | |
646 | ||
11b32d91 OZ |
647 | if (os == BS_ESTABLISHED) |
648 | bgp_conn_leave_established_state(p); | |
649 | } | |
650 | ||
651 | void | |
652 | bgp_conn_enter_idle_state(struct bgp_conn *conn) | |
653 | { | |
654 | struct bgp_proto *p = conn->bgp; | |
655 | int os = conn->state; | |
656 | ||
657 | bgp_close_conn(conn); | |
cf31112f | 658 | bgp_conn_set_state(conn, BS_IDLE); |
11b32d91 OZ |
659 | ev_schedule(p->event); |
660 | ||
661 | if (os == BS_ESTABLISHED) | |
662 | bgp_conn_leave_established_state(p); | |
663 | } | |
664 | ||
6eda3f13 OZ |
665 | /** |
666 | * bgp_handle_graceful_restart - handle detected BGP graceful restart | |
667 | * @p: BGP instance | |
668 | * | |
669 | * This function is called when a BGP graceful restart of the neighbor is | |
670 | * detected (when the TCP connection fails or when a new TCP connection | |
671 | * appears). The function activates processing of the restart - starts routing | |
672 | * table refresh cycle and activates BGP restart timer. The protocol state goes | |
673 | * back to %PS_START, but changing BGP state back to %BS_IDLE is left for the | |
674 | * caller. | |
675 | */ | |
0c791f87 OZ |
676 | void |
677 | bgp_handle_graceful_restart(struct bgp_proto *p) | |
678 | { | |
679 | ASSERT(p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready); | |
680 | ||
681 | BGP_TRACE(D_EVENTS, "Neighbor graceful restart detected%s", | |
d15b0b0a OZ |
682 | p->gr_active_num ? " - already pending" : ""); |
683 | ||
684 | p->gr_active_num = 0; | |
0c791f87 | 685 | |
d15b0b0a OZ |
686 | struct bgp_channel *c; |
687 | WALK_LIST(c, p->p.channels) | |
688 | { | |
7fc55925 OZ |
689 | /* FIXME: perhaps check for channel state instead of disabled flag? */ |
690 | if (c->c.disabled) | |
691 | continue; | |
692 | ||
d15b0b0a OZ |
693 | if (c->gr_ready) |
694 | { | |
5bd73431 OZ |
695 | p->gr_active_num++; |
696 | ||
697 | switch (c->gr_active) | |
698 | { | |
699 | case BGP_GRS_NONE: | |
700 | c->gr_active = BGP_GRS_ACTIVE; | |
701 | rt_refresh_begin(c->c.table, &c->c); | |
702 | break; | |
703 | ||
704 | case BGP_GRS_ACTIVE: | |
d15b0b0a | 705 | rt_refresh_end(c->c.table, &c->c); |
5bd73431 OZ |
706 | rt_refresh_begin(c->c.table, &c->c); |
707 | break; | |
0c791f87 | 708 | |
5bd73431 OZ |
709 | case BGP_GRS_LLGR: |
710 | rt_refresh_begin(c->c.table, &c->c); | |
711 | rt_modify_stale(c->c.table, &c->c); | |
712 | break; | |
713 | } | |
d15b0b0a OZ |
714 | } |
715 | else | |
716 | { | |
717 | /* Just flush the routes */ | |
718 | rt_refresh_begin(c->c.table, &c->c); | |
719 | rt_refresh_end(c->c.table, &c->c); | |
720 | } | |
7fc55925 OZ |
721 | |
722 | /* Reset bucket and prefix tables */ | |
723 | bgp_free_bucket_table(c); | |
724 | bgp_free_prefix_table(c); | |
725 | bgp_init_bucket_table(c); | |
726 | bgp_init_prefix_table(c); | |
727 | c->packets_to_send = 0; | |
d15b0b0a OZ |
728 | } |
729 | ||
e62cd033 OZ |
730 | /* p->gr_ready -> at least one active channel is c->gr_ready */ |
731 | ASSERT(p->gr_active_num > 0); | |
732 | ||
d15b0b0a | 733 | proto_notify_state(&p->p, PS_START); |
5bd73431 | 734 | tm_start(p->gr_timer, p->conn->remote_caps->gr_time S); |
0c791f87 OZ |
735 | } |
736 | ||
6eda3f13 OZ |
737 | /** |
738 | * bgp_graceful_restart_done - finish active BGP graceful restart | |
d15b0b0a | 739 | * @c: BGP channel |
6eda3f13 OZ |
740 | * |
741 | * This function is called when the active BGP graceful restart of the neighbor | |
d15b0b0a OZ |
742 | * should be finished for channel @c - either successfully (the neighbor sends |
743 | * all paths and reports end-of-RIB for given AFI/SAFI on the new session) or | |
744 | * unsuccessfully (the neighbor does not support BGP graceful restart on the new | |
745 | * session). The function ends the routing table refresh cycle. | |
6eda3f13 | 746 | */ |
0c791f87 | 747 | void |
d15b0b0a | 748 | bgp_graceful_restart_done(struct bgp_channel *c) |
0c791f87 | 749 | { |
d15b0b0a OZ |
750 | struct bgp_proto *p = (void *) c->c.proto; |
751 | ||
752 | ASSERT(c->gr_active); | |
753 | c->gr_active = 0; | |
754 | p->gr_active_num--; | |
755 | ||
756 | if (!p->gr_active_num) | |
757 | BGP_TRACE(D_EVENTS, "Neighbor graceful restart done"); | |
758 | ||
5bd73431 | 759 | tm_stop(c->stale_timer); |
d15b0b0a | 760 | rt_refresh_end(c->c.table, &c->c); |
0c791f87 OZ |
761 | } |
762 | ||
6eda3f13 OZ |
763 | /** |
764 | * bgp_graceful_restart_timeout - timeout of graceful restart 'restart timer' | |
765 | * @t: timer | |
766 | * | |
767 | * This function is a timeout hook for @gr_timer, implementing BGP restart time | |
768 | * limit for reestablisment of the BGP session after the graceful restart. When | |
769 | * fired, we just proceed with the usual protocol restart. | |
770 | */ | |
771 | ||
0c791f87 OZ |
772 | static void |
773 | bgp_graceful_restart_timeout(timer *t) | |
774 | { | |
775 | struct bgp_proto *p = t->data; | |
776 | ||
777 | BGP_TRACE(D_EVENTS, "Neighbor graceful restart timeout"); | |
5bd73431 OZ |
778 | |
779 | if (p->llgr_ready) | |
780 | { | |
781 | struct bgp_channel *c; | |
782 | WALK_LIST(c, p->p.channels) | |
783 | { | |
784 | /* Channel is not in GR and is already flushed */ | |
785 | if (!c->gr_active) | |
786 | continue; | |
787 | ||
788 | /* Channel is already in LLGR from past restart */ | |
789 | if (c->gr_active == BGP_GRS_LLGR) | |
790 | continue; | |
791 | ||
792 | /* Channel is in GR, but does not support LLGR -> stop GR */ | |
793 | if (!c->stale_time) | |
794 | { | |
795 | bgp_graceful_restart_done(c); | |
796 | continue; | |
797 | } | |
798 | ||
799 | /* Channel is in GR, and supports LLGR -> start LLGR */ | |
800 | c->gr_active = BGP_GRS_LLGR; | |
801 | tm_start(c->stale_timer, c->stale_time S); | |
802 | rt_modify_stale(c->c.table, &c->c); | |
803 | } | |
804 | } | |
805 | else | |
806 | bgp_stop(p, 0, NULL, 0); | |
807 | } | |
808 | ||
809 | static void | |
810 | bgp_long_lived_stale_timeout(timer *t) | |
811 | { | |
812 | struct bgp_channel *c = t->data; | |
813 | struct bgp_proto *p = (void *) c->c.proto; | |
814 | ||
815 | BGP_TRACE(D_EVENTS, "Long-lived stale timeout"); | |
816 | ||
817 | bgp_graceful_restart_done(c); | |
0c791f87 OZ |
818 | } |
819 | ||
9aed29e6 OZ |
820 | |
821 | /** | |
822 | * bgp_refresh_begin - start incoming enhanced route refresh sequence | |
d15b0b0a | 823 | * @c: BGP channel |
9aed29e6 OZ |
824 | * |
825 | * This function is called when an incoming enhanced route refresh sequence is | |
826 | * started by the neighbor, demarcated by the BoRR packet. The function updates | |
827 | * the load state and starts the routing table refresh cycle. Note that graceful | |
828 | * restart also uses routing table refresh cycle, but RFC 7313 and load states | |
829 | * ensure that these two sequences do not overlap. | |
830 | */ | |
831 | void | |
d15b0b0a | 832 | bgp_refresh_begin(struct bgp_channel *c) |
9aed29e6 | 833 | { |
d15b0b0a OZ |
834 | struct bgp_proto *p = (void *) c->c.proto; |
835 | ||
836 | if (c->load_state == BFS_LOADING) | |
837 | { log(L_WARN "%s: BEGIN-OF-RR received before END-OF-RIB, ignoring", p->p.name); return; } | |
9aed29e6 | 838 | |
d15b0b0a OZ |
839 | c->load_state = BFS_REFRESHING; |
840 | rt_refresh_begin(c->c.table, &c->c); | |
9aed29e6 OZ |
841 | } |
842 | ||
843 | /** | |
844 | * bgp_refresh_end - finish incoming enhanced route refresh sequence | |
d15b0b0a | 845 | * @c: BGP channel |
9aed29e6 OZ |
846 | * |
847 | * This function is called when an incoming enhanced route refresh sequence is | |
848 | * finished by the neighbor, demarcated by the EoRR packet. The function updates | |
849 | * the load state and ends the routing table refresh cycle. Routes not received | |
850 | * during the sequence are removed by the nest. | |
851 | */ | |
852 | void | |
d15b0b0a | 853 | bgp_refresh_end(struct bgp_channel *c) |
9aed29e6 | 854 | { |
d15b0b0a | 855 | struct bgp_proto *p = (void *) c->c.proto; |
9aed29e6 | 856 | |
d15b0b0a OZ |
857 | if (c->load_state != BFS_REFRESHING) |
858 | { log(L_WARN "%s: END-OF-RR received without prior BEGIN-OF-RR, ignoring", p->p.name); return; } | |
859 | ||
860 | c->load_state = BFS_NONE; | |
861 | rt_refresh_end(c->c.table, &c->c); | |
9aed29e6 OZ |
862 | } |
863 | ||
864 | ||
c01e3741 MM |
865 | static void |
866 | bgp_send_open(struct bgp_conn *conn) | |
867 | { | |
868 | DBG("BGP: Sending open\n"); | |
869 | conn->sk->rx_hook = bgp_rx; | |
b552ecc4 | 870 | conn->sk->tx_hook = bgp_tx; |
a6f79ca5 | 871 | tm_stop(conn->connect_timer); |
d15b0b0a | 872 | bgp_schedule_packet(conn, NULL, PKT_OPEN); |
cf31112f | 873 | bgp_conn_set_state(conn, BS_OPENSENT); |
3fdbafb6 | 874 | bgp_start_timer(conn->hold_timer, conn->bgp->cf->initial_hold_time); |
c01e3741 MM |
875 | } |
876 | ||
3fdbafb6 MM |
877 | static void |
878 | bgp_connected(sock *sk) | |
c01e3741 MM |
879 | { |
880 | struct bgp_conn *conn = sk->data; | |
85368cd4 | 881 | struct bgp_proto *p = conn->bgp; |
c01e3741 | 882 | |
85368cd4 | 883 | BGP_TRACE(D_EVENTS, "Connected"); |
c01e3741 | 884 | bgp_send_open(conn); |
c01e3741 MM |
885 | } |
886 | ||
887 | static void | |
888 | bgp_connect_timeout(timer *t) | |
889 | { | |
3fdbafb6 | 890 | struct bgp_conn *conn = t->data; |
85368cd4 | 891 | struct bgp_proto *p = conn->bgp; |
c01e3741 | 892 | |
85368cd4 | 893 | DBG("BGP: connect_timeout\n"); |
11b32d91 | 894 | if (p->p.proto_state == PS_START) |
d15b0b0a OZ |
895 | { |
896 | bgp_close_conn(conn); | |
897 | bgp_connect(p); | |
898 | } | |
11b32d91 OZ |
899 | else |
900 | bgp_conn_enter_idle_state(conn); | |
c01e3741 MM |
901 | } |
902 | ||
903 | static void | |
3fdbafb6 | 904 | bgp_sock_err(sock *sk, int err) |
c01e3741 MM |
905 | { |
906 | struct bgp_conn *conn = sk->data; | |
85368cd4 | 907 | struct bgp_proto *p = conn->bgp; |
c01e3741 | 908 | |
47597724 OZ |
909 | /* |
910 | * This error hook may be called either asynchronously from main | |
911 | * loop, or synchronously from sk_send(). But sk_send() is called | |
912 | * only from bgp_tx() and bgp_kick_tx(), which are both called | |
913 | * asynchronously from main loop. Moreover, they end if err hook is | |
914 | * called. Therefore, we could suppose that it is always called | |
915 | * asynchronously. | |
916 | */ | |
917 | ||
11b32d91 OZ |
918 | bgp_store_error(p, conn, BE_SOCKET, err); |
919 | ||
53943a00 MM |
920 | if (err) |
921 | BGP_TRACE(D_EVENTS, "Connection lost (%M)", err); | |
922 | else | |
923 | BGP_TRACE(D_EVENTS, "Connection closed"); | |
11b32d91 | 924 | |
0c791f87 OZ |
925 | if ((conn->state == BS_ESTABLISHED) && p->gr_ready) |
926 | bgp_handle_graceful_restart(p); | |
927 | ||
11b32d91 | 928 | bgp_conn_enter_idle_state(conn); |
c01e3741 MM |
929 | } |
930 | ||
3fdbafb6 MM |
931 | static void |
932 | bgp_hold_timeout(timer *t) | |
933 | { | |
934 | struct bgp_conn *conn = t->data; | |
48b15ef1 | 935 | struct bgp_proto *p = conn->bgp; |
3fdbafb6 | 936 | |
ea89da38 OZ |
937 | DBG("BGP: Hold timeout\n"); |
938 | ||
48b15ef1 OZ |
939 | /* We are already closing the connection - just do hangup */ |
940 | if (conn->state == BS_CLOSE) | |
941 | { | |
942 | BGP_TRACE(D_EVENTS, "Connection stalled"); | |
943 | bgp_conn_enter_idle_state(conn); | |
944 | return; | |
945 | } | |
946 | ||
ea89da38 OZ |
947 | /* If there is something in input queue, we are probably congested |
948 | and perhaps just not processed BGP packets in time. */ | |
949 | ||
950 | if (sk_rx_ready(conn->sk) > 0) | |
951 | bgp_start_timer(conn->hold_timer, 10); | |
5bd73431 OZ |
952 | else if ((conn->state == BS_ESTABLISHED) && p->llgr_ready) |
953 | { | |
954 | BGP_TRACE(D_EVENTS, "Hold timer expired"); | |
955 | bgp_handle_graceful_restart(p); | |
956 | bgp_conn_enter_idle_state(conn); | |
957 | } | |
ea89da38 OZ |
958 | else |
959 | bgp_error(conn, 4, 0, NULL, 0); | |
3fdbafb6 MM |
960 | } |
961 | ||
962 | static void | |
963 | bgp_keepalive_timeout(timer *t) | |
964 | { | |
965 | struct bgp_conn *conn = t->data; | |
966 | ||
967 | DBG("BGP: Keepalive timer\n"); | |
d15b0b0a | 968 | bgp_schedule_packet(conn, NULL, PKT_KEEPALIVE); |
bd22d7f4 OZ |
969 | |
970 | /* Kick TX a bit faster */ | |
971 | if (ev_active(conn->tx_ev)) | |
972 | ev_run(conn->tx_ev); | |
3fdbafb6 MM |
973 | } |
974 | ||
c01e3741 | 975 | static void |
6fd766c1 | 976 | bgp_setup_conn(struct bgp_proto *p, struct bgp_conn *conn) |
c01e3741 | 977 | { |
6fd766c1 | 978 | conn->sk = NULL; |
c01e3741 | 979 | conn->bgp = p; |
d15b0b0a | 980 | |
72a6ef11 | 981 | conn->packets_to_send = 0; |
d15b0b0a OZ |
982 | conn->channels_to_send = 0; |
983 | conn->last_channel = 0; | |
984 | conn->last_channel_count = 0; | |
985 | ||
a6f79ca5 OZ |
986 | conn->connect_timer = tm_new_init(p->p.pool, bgp_connect_timeout, conn, 0, 0); |
987 | conn->hold_timer = tm_new_init(p->p.pool, bgp_hold_timeout, conn, 0, 0); | |
988 | conn->keepalive_timer = tm_new_init(p->p.pool, bgp_keepalive_timeout, conn, 0, 0); | |
c01e3741 | 989 | |
961671c0 | 990 | conn->tx_ev = ev_new_init(p->p.pool, bgp_kick_tx, conn); |
c01e3741 MM |
991 | } |
992 | ||
6fd766c1 | 993 | static void |
e81b440f | 994 | bgp_setup_sk(struct bgp_conn *conn, sock *s) |
6fd766c1 MM |
995 | { |
996 | s->data = conn; | |
6fd766c1 | 997 | s->err_hook = bgp_sock_err; |
9e7b3ebd | 998 | s->fast_rx = 1; |
6fd766c1 MM |
999 | conn->sk = s; |
1000 | } | |
1001 | ||
11b32d91 | 1002 | static void |
dd91e467 | 1003 | bgp_active(struct bgp_proto *p) |
11b32d91 | 1004 | { |
6cf72d7a | 1005 | int delay = MAX(1, p->cf->connect_delay_time); |
11b32d91 OZ |
1006 | struct bgp_conn *conn = &p->outgoing_conn; |
1007 | ||
1008 | BGP_TRACE(D_EVENTS, "Connect delayed by %d seconds", delay); | |
1009 | bgp_setup_conn(p, conn); | |
cf31112f | 1010 | bgp_conn_set_state(conn, BS_ACTIVE); |
d15b0b0a | 1011 | bgp_start_timer(conn->connect_timer, delay); |
11b32d91 OZ |
1012 | } |
1013 | ||
54e55169 MM |
1014 | /** |
1015 | * bgp_connect - initiate an outgoing connection | |
1016 | * @p: BGP instance | |
1017 | * | |
1018 | * The bgp_connect() function creates a new &bgp_conn and initiates | |
1019 | * a TCP connection to the peer. The rest of connection setup is governed | |
1020 | * by the BGP state machine as described in the standard. | |
1021 | */ | |
c01e3741 MM |
1022 | static void |
1023 | bgp_connect(struct bgp_proto *p) /* Enter Connect state and start establishing connection */ | |
1024 | { | |
b552ecc4 | 1025 | struct bgp_conn *conn = &p->outgoing_conn; |
b1b19433 | 1026 | int hops = p->cf->multihop ? : 1; |
c01e3741 MM |
1027 | |
1028 | DBG("BGP: Connecting\n"); | |
d15b0b0a | 1029 | sock *s = sk_new(p->p.pool); |
c01e3741 | 1030 | s->type = SK_TCP_ACTIVE; |
ad440a57 | 1031 | s->saddr = p->source_addr; |
c01e3741 | 1032 | s->daddr = p->cf->remote_ip; |
dcde7ae5 | 1033 | s->dport = p->cf->remote_port; |
53ffbff3 | 1034 | s->iface = p->neigh ? p->neigh->iface : NULL; |
943478b0 | 1035 | s->vrf = p->p.vrf; |
b1b19433 | 1036 | s->ttl = p->cf->ttl_security ? 255 : hops; |
06e0d1b6 OZ |
1037 | s->rbsize = p->cf->enable_extended_messages ? BGP_RX_BUFFER_EXT_SIZE : BGP_RX_BUFFER_SIZE; |
1038 | s->tbsize = p->cf->enable_extended_messages ? BGP_TX_BUFFER_EXT_SIZE : BGP_TX_BUFFER_SIZE; | |
a39b165e OZ |
1039 | s->tos = IP_PREC_INTERNET_CONTROL; |
1040 | s->password = p->cf->password; | |
1041 | s->tx_hook = bgp_connected; | |
53ffbff3 | 1042 | BGP_TRACE(D_EVENTS, "Connecting to %I%J from local address %I%J", s->daddr, p->cf->iface, |
88a183c6 | 1043 | s->saddr, ipa_is_link_local(s->saddr) ? s->iface : NULL); |
6fd766c1 | 1044 | bgp_setup_conn(p, conn); |
e81b440f | 1045 | bgp_setup_sk(conn, s); |
cf31112f | 1046 | bgp_conn_set_state(conn, BS_CONNECT); |
b1b19433 OZ |
1047 | |
1048 | if (sk_open(s) < 0) | |
05476c4d | 1049 | goto err; |
b1b19433 OZ |
1050 | |
1051 | /* Set minimal receive TTL if needed */ | |
1052 | if (p->cf->ttl_security) | |
b1b19433 | 1053 | if (sk_set_min_ttl(s, 256 - hops) < 0) |
05476c4d | 1054 | goto err; |
b1b19433 | 1055 | |
c01e3741 | 1056 | DBG("BGP: Waiting for connect success\n"); |
d15b0b0a | 1057 | bgp_start_timer(conn->connect_timer, p->cf->connect_retry_time); |
05476c4d OZ |
1058 | return; |
1059 | ||
d15b0b0a | 1060 | err: |
05476c4d OZ |
1061 | sk_log_error(s, p->p.name); |
1062 | bgp_sock_err(s, 0); | |
1063 | return; | |
c01e3741 MM |
1064 | } |
1065 | ||
374917ad OZ |
1066 | /** |
1067 | * bgp_find_proto - find existing proto for incoming connection | |
1068 | * @sk: TCP socket | |
1069 | * | |
1070 | */ | |
1071 | static struct bgp_proto * | |
1072 | bgp_find_proto(sock *sk) | |
1073 | { | |
d15b0b0a | 1074 | struct bgp_proto *p; |
374917ad | 1075 | |
d15b0b0a OZ |
1076 | WALK_LIST(p, proto_list) |
1077 | if ((p->p.proto == &proto_bgp) && | |
1078 | ipa_equal(p->cf->remote_ip, sk->daddr) && | |
e919601a | 1079 | (!p->cf->iface || (p->cf->iface == sk->iface)) && |
d15b0b0a OZ |
1080 | (ipa_zero(p->cf->local_ip) || ipa_equal(p->cf->local_ip, sk->saddr)) && |
1081 | (p->cf->local_port == sk->sport)) | |
1082 | return p; | |
374917ad OZ |
1083 | |
1084 | return NULL; | |
1085 | } | |
1086 | ||
54e55169 MM |
1087 | /** |
1088 | * bgp_incoming_connection - handle an incoming connection | |
1089 | * @sk: TCP socket | |
1090 | * @dummy: unused | |
1091 | * | |
1092 | * This function serves as a socket hook for accepting of new BGP | |
1093 | * connections. It searches a BGP instance corresponding to the peer | |
1094 | * which has connected and if such an instance exists, it creates a | |
1095 | * &bgp_conn structure, attaches it to the instance and either sends | |
1096 | * an Open message or (if there already is an active connection) it | |
1097 | * closes the new connection by sending a Notification message. | |
1098 | */ | |
48e842cc | 1099 | static int |
3e236955 | 1100 | bgp_incoming_connection(sock *sk, uint dummy UNUSED) |
c01e3741 | 1101 | { |
374917ad OZ |
1102 | struct bgp_proto *p; |
1103 | int acc, hops; | |
c01e3741 | 1104 | |
48e842cc | 1105 | DBG("BGP: Incoming connection from %I port %d\n", sk->daddr, sk->dport); |
374917ad OZ |
1106 | p = bgp_find_proto(sk); |
1107 | if (!p) | |
d15b0b0a OZ |
1108 | { |
1109 | log(L_WARN "BGP: Unexpected connect from unknown address %I%J (port %d)", | |
1110 | sk->daddr, ipa_is_link_local(sk->daddr) ? sk->iface : NULL, sk->dport); | |
1111 | rfree(sk); | |
1112 | return 0; | |
1113 | } | |
374917ad | 1114 | |
487c6961 OZ |
1115 | /* |
1116 | * BIRD should keep multiple incoming connections in OpenSent state (for | |
1117 | * details RFC 4271 8.2.1 par 3), but it keeps just one. Duplicate incoming | |
1118 | * connections are rejected istead. The exception is the case where an | |
1119 | * incoming connection triggers a graceful restart. | |
1120 | */ | |
1121 | ||
374917ad OZ |
1122 | acc = (p->p.proto_state == PS_START || p->p.proto_state == PS_UP) && |
1123 | (p->start_state >= BSS_CONNECT) && (!p->incoming_conn.sk); | |
dd91e467 | 1124 | |
374917ad | 1125 | if (p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready) |
d15b0b0a OZ |
1126 | { |
1127 | bgp_store_error(p, NULL, BE_MISC, BEM_GRACEFUL_RESTART); | |
1128 | bgp_handle_graceful_restart(p); | |
1129 | bgp_conn_enter_idle_state(p->conn); | |
1130 | acc = 1; | |
1131 | ||
1132 | /* There might be separate incoming connection in OpenSent state */ | |
1133 | if (p->incoming_conn.state > BS_ACTIVE) | |
1134 | bgp_close_conn(&p->incoming_conn); | |
1135 | } | |
374917ad OZ |
1136 | |
1137 | BGP_TRACE(D_EVENTS, "Incoming connection from %I%J (port %d) %s", | |
1138 | sk->daddr, ipa_is_link_local(sk->daddr) ? sk->iface : NULL, | |
1139 | sk->dport, acc ? "accepted" : "rejected"); | |
1140 | ||
1141 | if (!acc) | |
d15b0b0a OZ |
1142 | { |
1143 | rfree(sk); | |
1144 | return 0; | |
1145 | } | |
374917ad OZ |
1146 | |
1147 | hops = p->cf->multihop ? : 1; | |
1148 | ||
1149 | if (sk_set_ttl(sk, p->cf->ttl_security ? 255 : hops) < 0) | |
1150 | goto err; | |
1151 | ||
1152 | if (p->cf->ttl_security) | |
1153 | if (sk_set_min_ttl(sk, 256 - hops) < 0) | |
1154 | goto err; | |
1155 | ||
06e0d1b6 | 1156 | if (p->cf->enable_extended_messages) |
d15b0b0a OZ |
1157 | { |
1158 | sk->rbsize = BGP_RX_BUFFER_EXT_SIZE; | |
1159 | sk->tbsize = BGP_TX_BUFFER_EXT_SIZE; | |
1160 | sk_reallocate(sk); | |
1161 | } | |
06e0d1b6 | 1162 | |
374917ad OZ |
1163 | bgp_setup_conn(p, &p->incoming_conn); |
1164 | bgp_setup_sk(&p->incoming_conn, sk); | |
1165 | bgp_send_open(&p->incoming_conn); | |
1166 | return 0; | |
1167 | ||
1168 | err: | |
1169 | sk_log_error(sk, p->p.name); | |
1170 | log(L_ERR "%s: Incoming connection aborted", p->p.name); | |
48e842cc MM |
1171 | rfree(sk); |
1172 | return 0; | |
1173 | } | |
1174 | ||
2af25a97 | 1175 | static void |
e81b440f | 1176 | bgp_listen_sock_err(sock *sk UNUSED, int err) |
2af25a97 OZ |
1177 | { |
1178 | if (err == ECONNABORTED) | |
1179 | log(L_WARN "BGP: Incoming connection aborted"); | |
1180 | else | |
a34b0934 | 1181 | log(L_ERR "BGP: Error on listening socket: %M", err); |
2af25a97 OZ |
1182 | } |
1183 | ||
acfce55c MM |
1184 | static void |
1185 | bgp_start_neighbor(struct bgp_proto *p) | |
1186 | { | |
9be9a264 OZ |
1187 | /* Called only for single-hop BGP sessions */ |
1188 | ||
1189 | if (ipa_zero(p->source_addr)) | |
523f020b | 1190 | p->source_addr = p->neigh->ifa->ip; |
ad440a57 | 1191 | |
d15b0b0a OZ |
1192 | if (ipa_is_link_local(p->source_addr)) |
1193 | p->link_addr = p->source_addr; | |
153f02da OZ |
1194 | else if (p->neigh->iface->llv6) |
1195 | p->link_addr = p->neigh->iface->llv6->ip; | |
11b32d91 | 1196 | |
6fd766c1 | 1197 | bgp_initiate(p); |
48e842cc MM |
1198 | } |
1199 | ||
1200 | static void | |
1201 | bgp_neigh_notify(neighbor *n) | |
1202 | { | |
1203 | struct bgp_proto *p = (struct bgp_proto *) n->proto; | |
523f020b OZ |
1204 | int ps = p->p.proto_state; |
1205 | ||
1206 | if (n != p->neigh) | |
1207 | return; | |
48e842cc | 1208 | |
523f020b | 1209 | if ((ps == PS_DOWN) || (ps == PS_STOP)) |
b21955e0 OZ |
1210 | return; |
1211 | ||
523f020b OZ |
1212 | int prepare = (ps == PS_START) && (p->start_state == BSS_PREPARE); |
1213 | ||
1214 | if (n->scope <= 0) | |
d15b0b0a OZ |
1215 | { |
1216 | if (!prepare) | |
48e842cc | 1217 | { |
d15b0b0a OZ |
1218 | BGP_TRACE(D_EVENTS, "Neighbor lost"); |
1219 | bgp_store_error(p, NULL, BE_MISC, BEM_NEIGHBOR_LOST); | |
1220 | /* Perhaps also run bgp_update_startup_delay(p)? */ | |
830ba75e | 1221 | bgp_stop(p, 0, NULL, 0); |
523f020b | 1222 | } |
d15b0b0a | 1223 | } |
523f020b | 1224 | else if (p->cf->check_link && !(n->iface->flags & IF_LINK_UP)) |
d15b0b0a OZ |
1225 | { |
1226 | if (!prepare) | |
523f020b | 1227 | { |
d15b0b0a OZ |
1228 | BGP_TRACE(D_EVENTS, "Link down"); |
1229 | bgp_store_error(p, NULL, BE_MISC, BEM_LINK_DOWN); | |
1230 | if (ps == PS_UP) | |
1231 | bgp_update_startup_delay(p); | |
830ba75e | 1232 | bgp_stop(p, 0, NULL, 0); |
48e842cc | 1233 | } |
d15b0b0a | 1234 | } |
48e842cc | 1235 | else |
d15b0b0a OZ |
1236 | { |
1237 | if (prepare) | |
48e842cc | 1238 | { |
d15b0b0a OZ |
1239 | BGP_TRACE(D_EVENTS, "Neighbor ready"); |
1240 | bgp_start_neighbor(p); | |
48e842cc | 1241 | } |
d15b0b0a | 1242 | } |
48e842cc MM |
1243 | } |
1244 | ||
1ec52253 OZ |
1245 | static void |
1246 | bgp_bfd_notify(struct bfd_request *req) | |
1247 | { | |
1248 | struct bgp_proto *p = req->data; | |
1249 | int ps = p->p.proto_state; | |
1250 | ||
1251 | if (req->down && ((ps == PS_START) || (ps == PS_UP))) | |
d15b0b0a OZ |
1252 | { |
1253 | BGP_TRACE(D_EVENTS, "BFD session down"); | |
1254 | bgp_store_error(p, NULL, BE_MISC, BEM_BFD_DOWN); | |
5bd73431 OZ |
1255 | |
1256 | if (p->cf->bfd == BGP_BFD_GRACEFUL) | |
1257 | { | |
1258 | /* Trigger graceful restart */ | |
1259 | if (p->conn && (p->conn->state == BS_ESTABLISHED) && p->gr_ready) | |
1260 | bgp_handle_graceful_restart(p); | |
1261 | ||
1262 | if (p->incoming_conn.state > BS_IDLE) | |
1263 | bgp_conn_enter_idle_state(&p->incoming_conn); | |
1264 | ||
1265 | if (p->outgoing_conn.state > BS_IDLE) | |
1266 | bgp_conn_enter_idle_state(&p->outgoing_conn); | |
1267 | } | |
1268 | else | |
1269 | { | |
1270 | /* Trigger session down */ | |
1271 | if (ps == PS_UP) | |
1272 | bgp_update_startup_delay(p); | |
1273 | bgp_stop(p, 0, NULL, 0); | |
1274 | } | |
d15b0b0a | 1275 | } |
1ec52253 OZ |
1276 | } |
1277 | ||
1278 | static void | |
1279 | bgp_update_bfd(struct bgp_proto *p, int use_bfd) | |
1280 | { | |
1281 | if (use_bfd && !p->bfd_req) | |
1282 | p->bfd_req = bfd_request_session(p->p.pool, p->cf->remote_ip, p->source_addr, | |
1283 | p->cf->multihop ? NULL : p->neigh->iface, | |
1284 | bgp_bfd_notify, p); | |
1285 | ||
1286 | if (!use_bfd && p->bfd_req) | |
d15b0b0a OZ |
1287 | { |
1288 | rfree(p->bfd_req); | |
1289 | p->bfd_req = NULL; | |
1290 | } | |
1ec52253 OZ |
1291 | } |
1292 | ||
d15b0b0a OZ |
1293 | static void |
1294 | bgp_reload_routes(struct channel *C) | |
bf47fe4b | 1295 | { |
d15b0b0a OZ |
1296 | struct bgp_proto *p = (void *) C->proto; |
1297 | struct bgp_channel *c = (void *) C; | |
bf47fe4b | 1298 | |
d15b0b0a OZ |
1299 | ASSERT(p->conn && p->route_refresh); |
1300 | ||
1301 | bgp_schedule_packet(p->conn, c, PKT_ROUTE_REFRESH); | |
bf47fe4b OZ |
1302 | } |
1303 | ||
0c791f87 | 1304 | static void |
d15b0b0a | 1305 | bgp_feed_begin(struct channel *C, int initial) |
0c791f87 | 1306 | { |
d15b0b0a OZ |
1307 | struct bgp_proto *p = (void *) C->proto; |
1308 | struct bgp_channel *c = (void *) C; | |
9aed29e6 OZ |
1309 | |
1310 | /* This should not happen */ | |
1311 | if (!p->conn) | |
0c791f87 OZ |
1312 | return; |
1313 | ||
9aed29e6 | 1314 | if (initial && p->cf->gr_mode) |
d15b0b0a | 1315 | c->feed_state = BFS_LOADING; |
9aed29e6 OZ |
1316 | |
1317 | /* It is refeed and both sides support enhanced route refresh */ | |
d15b0b0a OZ |
1318 | if (!initial && p->enhanced_refresh) |
1319 | { | |
1320 | /* BoRR must not be sent before End-of-RIB */ | |
1321 | if (c->feed_state == BFS_LOADING || c->feed_state == BFS_LOADED) | |
1322 | return; | |
9aed29e6 | 1323 | |
d15b0b0a OZ |
1324 | c->feed_state = BFS_REFRESHING; |
1325 | bgp_schedule_packet(p->conn, c, PKT_BEGIN_REFRESH); | |
1326 | } | |
9aed29e6 OZ |
1327 | } |
1328 | ||
1329 | static void | |
d15b0b0a | 1330 | bgp_feed_end(struct channel *C) |
9aed29e6 | 1331 | { |
d15b0b0a OZ |
1332 | struct bgp_proto *p = (void *) C->proto; |
1333 | struct bgp_channel *c = (void *) C; | |
9aed29e6 OZ |
1334 | |
1335 | /* This should not happen */ | |
1336 | if (!p->conn) | |
1337 | return; | |
1338 | ||
1339 | /* Non-demarcated feed ended, nothing to do */ | |
d15b0b0a | 1340 | if (c->feed_state == BFS_NONE) |
9aed29e6 OZ |
1341 | return; |
1342 | ||
1343 | /* Schedule End-of-RIB packet */ | |
d15b0b0a OZ |
1344 | if (c->feed_state == BFS_LOADING) |
1345 | c->feed_state = BFS_LOADED; | |
9aed29e6 OZ |
1346 | |
1347 | /* Schedule EoRR packet */ | |
d15b0b0a OZ |
1348 | if (c->feed_state == BFS_REFRESHING) |
1349 | c->feed_state = BFS_REFRESHED; | |
9aed29e6 OZ |
1350 | |
1351 | /* Kick TX hook */ | |
d15b0b0a | 1352 | bgp_schedule_packet(p->conn, c, PKT_UPDATE); |
0c791f87 OZ |
1353 | } |
1354 | ||
9aed29e6 | 1355 | |
48e842cc MM |
1356 | static void |
1357 | bgp_start_locked(struct object_lock *lock) | |
1358 | { | |
1359 | struct bgp_proto *p = lock->data; | |
1360 | struct bgp_config *cf = p->cf; | |
1361 | ||
11b32d91 | 1362 | if (p->p.proto_state != PS_START) |
d15b0b0a OZ |
1363 | { |
1364 | DBG("BGP: Got lock in different state %d\n", p->p.proto_state); | |
1365 | return; | |
1366 | } | |
11b32d91 | 1367 | |
48e842cc | 1368 | DBG("BGP: Got lock\n"); |
4847a894 | 1369 | |
9be9a264 | 1370 | if (cf->multihop) |
d15b0b0a OZ |
1371 | { |
1372 | /* Multi-hop sessions do not use neighbor entries */ | |
1373 | bgp_initiate(p); | |
1374 | return; | |
1375 | } | |
4847a894 | 1376 | |
586c1800 | 1377 | neighbor *n = neigh_find(&p->p, cf->remote_ip, cf->iface, NEF_STICKY); |
523f020b | 1378 | if (!n) |
d15b0b0a OZ |
1379 | { |
1380 | log(L_ERR "%s: Invalid remote address %I%J", p->p.name, cf->remote_ip, cf->iface); | |
1381 | /* As we do not start yet, we can just disable protocol */ | |
1382 | p->p.disabled = 1; | |
1383 | bgp_store_error(p, NULL, BE_MISC, BEM_INVALID_NEXT_HOP); | |
1384 | proto_notify_state(&p->p, PS_DOWN); | |
1385 | return; | |
1386 | } | |
523f020b OZ |
1387 | |
1388 | p->neigh = n; | |
1389 | ||
1390 | if (n->scope <= 0) | |
53ffbff3 | 1391 | BGP_TRACE(D_EVENTS, "Waiting for %I%J to become my neighbor", cf->remote_ip, cf->iface); |
523f020b OZ |
1392 | else if (p->cf->check_link && !(n->iface->flags & IF_LINK_UP)) |
1393 | BGP_TRACE(D_EVENTS, "Waiting for link on %s", n->iface->name); | |
1394 | else | |
1395 | bgp_start_neighbor(p); | |
c01e3741 MM |
1396 | } |
1397 | ||
2638249d MM |
1398 | static int |
1399 | bgp_start(struct proto *P) | |
1400 | { | |
c01e3741 MM |
1401 | struct bgp_proto *p = (struct bgp_proto *) P; |
1402 | struct object_lock *lock; | |
1403 | ||
b552ecc4 | 1404 | DBG("BGP: Startup.\n"); |
11b32d91 | 1405 | p->start_state = BSS_PREPARE; |
b552ecc4 MM |
1406 | p->outgoing_conn.state = BS_IDLE; |
1407 | p->incoming_conn.state = BS_IDLE; | |
bcbdcbb6 | 1408 | p->neigh = NULL; |
1ec52253 | 1409 | p->bfd_req = NULL; |
0c791f87 | 1410 | p->gr_ready = 0; |
d15b0b0a | 1411 | p->gr_active_num = 0; |
cfe34a31 | 1412 | |
961671c0 | 1413 | p->event = ev_new_init(p->p.pool, bgp_decision, p); |
a6f79ca5 OZ |
1414 | p->startup_timer = tm_new_init(p->p.pool, bgp_startup_timeout, p, 0, 0); |
1415 | p->gr_timer = tm_new_init(p->p.pool, bgp_graceful_restart_timeout, p, 0, 0); | |
0c791f87 | 1416 | |
4ef09506 OZ |
1417 | p->local_id = proto_get_router_id(P->cf); |
1418 | if (p->rr_client) | |
1419 | p->rr_cluster_id = p->cf->rr_cluster_id ? p->cf->rr_cluster_id : p->local_id; | |
1420 | ||
9be9a264 | 1421 | p->remote_id = 0; |
d15b0b0a | 1422 | p->source_addr = p->cf->local_ip; |
ef57b70f | 1423 | p->link_addr = IPA_NONE; |
9be9a264 | 1424 | |
7fc55925 | 1425 | /* Lock all channels when in GR recovery mode */ |
6eda3f13 | 1426 | if (p->p.gr_recovery && p->cf->gr_mode) |
d15b0b0a OZ |
1427 | { |
1428 | struct bgp_channel *c; | |
1429 | WALK_LIST(c, p->p.channels) | |
1430 | channel_graceful_restart_lock(&c->c); | |
1431 | } | |
0c791f87 | 1432 | |
c01e3741 | 1433 | /* |
d15b0b0a OZ |
1434 | * Before attempting to create the connection, we need to lock the port, |
1435 | * so that we are the only instance attempting to talk with that neighbor. | |
c01e3741 MM |
1436 | */ |
1437 | ||
c01e3741 MM |
1438 | lock = p->lock = olock_new(P->pool); |
1439 | lock->addr = p->cf->remote_ip; | |
dcde7ae5 | 1440 | lock->port = p->cf->remote_port; |
53ffbff3 | 1441 | lock->iface = p->cf->iface; |
9f4908fe | 1442 | lock->vrf = p->cf->iface ? NULL : p->p.vrf; |
c01e3741 | 1443 | lock->type = OBJLOCK_TCP; |
c01e3741 MM |
1444 | lock->hook = bgp_start_locked; |
1445 | lock->data = p; | |
1446 | olock_acquire(lock); | |
d51aa281 | 1447 | |
c01e3741 | 1448 | return PS_START; |
2638249d MM |
1449 | } |
1450 | ||
d9b77cc2 OZ |
1451 | extern int proto_restart; |
1452 | ||
2638249d MM |
1453 | static int |
1454 | bgp_shutdown(struct proto *P) | |
1455 | { | |
c01e3741 | 1456 | struct bgp_proto *p = (struct bgp_proto *) P; |
d15b0b0a | 1457 | uint subcode = 0; |
c01e3741 | 1458 | |
cd1d9961 OZ |
1459 | char *message = NULL; |
1460 | byte *data = NULL; | |
1461 | uint len = 0; | |
c01e3741 | 1462 | |
85368cd4 | 1463 | BGP_TRACE(D_EVENTS, "Shutdown requested"); |
b99d3786 | 1464 | |
ebecb6f6 | 1465 | switch (P->down_code) |
d15b0b0a OZ |
1466 | { |
1467 | case PDC_CF_REMOVE: | |
1468 | case PDC_CF_DISABLE: | |
1469 | subcode = 3; // Errcode 6, 3 - peer de-configured | |
1470 | break; | |
1471 | ||
1472 | case PDC_CF_RESTART: | |
1473 | subcode = 6; // Errcode 6, 6 - other configuration change | |
1474 | break; | |
1475 | ||
1476 | case PDC_CMD_DISABLE: | |
1477 | case PDC_CMD_SHUTDOWN: | |
1478 | subcode = 2; // Errcode 6, 2 - administrative shutdown | |
830ba75e | 1479 | message = P->message; |
d15b0b0a OZ |
1480 | break; |
1481 | ||
1482 | case PDC_CMD_RESTART: | |
1483 | subcode = 4; // Errcode 6, 4 - administrative reset | |
830ba75e | 1484 | message = P->message; |
d15b0b0a OZ |
1485 | break; |
1486 | ||
1487 | case PDC_RX_LIMIT_HIT: | |
1488 | case PDC_IN_LIMIT_HIT: | |
1489 | subcode = 1; // Errcode 6, 1 - max number of prefixes reached | |
1490 | /* log message for compatibility */ | |
1491 | log(L_WARN "%s: Route limit exceeded, shutting down", p->p.name); | |
1492 | goto limit; | |
1493 | ||
1494 | case PDC_OUT_LIMIT_HIT: | |
1495 | subcode = proto_restart ? 4 : 2; // Administrative reset or shutdown | |
1496 | ||
1497 | limit: | |
1498 | bgp_store_error(p, NULL, BE_AUTO_DOWN, BEA_ROUTE_LIMIT_EXCEEDED); | |
1499 | if (proto_restart) | |
1500 | bgp_update_startup_delay(p); | |
1501 | else | |
1502 | p->startup_delay = 0; | |
1503 | goto done; | |
1504 | } | |
b99d3786 | 1505 | |
ebecb6f6 | 1506 | bgp_store_error(p, NULL, BE_MAN_DOWN, 0); |
11b32d91 | 1507 | p->startup_delay = 0; |
c01e3741 | 1508 | |
cd1d9961 OZ |
1509 | /* RFC 8203 - shutdown communication */ |
1510 | if (message) | |
1511 | { | |
1512 | uint msg_len = strlen(message); | |
1513 | msg_len = MIN(msg_len, 128); | |
1514 | ||
1515 | /* Buffer will be freed automatically by protocol shutdown */ | |
1516 | data = mb_alloc(p->p.pool, msg_len + 1); | |
1517 | len = msg_len + 1; | |
1518 | ||
1519 | data[0] = msg_len; | |
1520 | memcpy(data+1, message, msg_len); | |
1521 | } | |
1522 | ||
d15b0b0a | 1523 | done: |
cd1d9961 | 1524 | bgp_stop(p, subcode, data, len); |
11b32d91 | 1525 | return p->p.proto_state; |
2638249d MM |
1526 | } |
1527 | ||
48e842cc | 1528 | static struct proto * |
d15b0b0a | 1529 | bgp_init(struct proto_config *CF) |
48e842cc | 1530 | { |
d15b0b0a | 1531 | struct proto *P = proto_new(CF); |
48e842cc | 1532 | struct bgp_proto *p = (struct bgp_proto *) P; |
d15b0b0a | 1533 | struct bgp_config *cf = (struct bgp_config *) CF; |
48e842cc MM |
1534 | |
1535 | P->rt_notify = bgp_rt_notify; | |
48e842cc MM |
1536 | P->import_control = bgp_import_control; |
1537 | P->neigh_notify = bgp_neigh_notify; | |
bf47fe4b | 1538 | P->reload_routes = bgp_reload_routes; |
9aed29e6 OZ |
1539 | P->feed_begin = bgp_feed_begin; |
1540 | P->feed_end = bgp_feed_end; | |
094d2bdb | 1541 | P->rte_better = bgp_rte_better; |
8d9eef17 | 1542 | P->rte_mergable = bgp_rte_mergable; |
d15b0b0a | 1543 | P->rte_recalculate = cf->deterministic_med ? bgp_rte_recalculate : NULL; |
5bd73431 | 1544 | P->rte_modify = bgp_rte_modify_stale; |
d15b0b0a OZ |
1545 | |
1546 | p->cf = cf; | |
1547 | p->local_as = cf->local_as; | |
1548 | p->remote_as = cf->remote_as; | |
1549 | p->public_as = cf->local_as; | |
1550 | p->is_internal = (cf->local_as == cf->remote_as); | |
1551 | p->is_interior = p->is_internal || cf->confederation_member; | |
1552 | p->rs_client = cf->rs_client; | |
1553 | p->rr_client = cf->rr_client; | |
1554 | ||
1555 | /* Confederation ID is used for truly external peers */ | |
1556 | if (cf->confederation && !p->is_interior) | |
1557 | p->public_as = cf->confederation; | |
1558 | ||
1559 | /* Add all channels */ | |
1560 | struct bgp_channel_config *cc; | |
1561 | WALK_LIST(cc, CF->channels) | |
1562 | proto_add_channel(P, &cc->c); | |
9be9a264 | 1563 | |
48e842cc MM |
1564 | return P; |
1565 | } | |
1566 | ||
d15b0b0a OZ |
1567 | static void |
1568 | bgp_channel_init(struct channel *C, struct channel_config *CF) | |
1569 | { | |
1570 | struct bgp_channel *c = (void *) C; | |
1571 | struct bgp_channel_config *cf = (void *) CF; | |
1572 | ||
d15b0b0a OZ |
1573 | c->cf = cf; |
1574 | c->afi = cf->afi; | |
ef57b70f OZ |
1575 | c->desc = cf->desc; |
1576 | ||
1577 | if (cf->igp_table_ip4) | |
1578 | c->igp_table_ip4 = cf->igp_table_ip4->table; | |
1579 | ||
1580 | if (cf->igp_table_ip6) | |
1581 | c->igp_table_ip6 = cf->igp_table_ip6->table; | |
d15b0b0a OZ |
1582 | } |
1583 | ||
1584 | static int | |
1585 | bgp_channel_start(struct channel *C) | |
1586 | { | |
1587 | struct bgp_proto *p = (void *) C->proto; | |
1588 | struct bgp_channel *c = (void *) C; | |
1589 | ip_addr src = p->source_addr; | |
1590 | ||
ef57b70f OZ |
1591 | if (c->igp_table_ip4) |
1592 | rt_lock_table(c->igp_table_ip4); | |
1593 | ||
1594 | if (c->igp_table_ip6) | |
1595 | rt_lock_table(c->igp_table_ip6); | |
d15b0b0a OZ |
1596 | |
1597 | c->pool = p->p.pool; // XXXX | |
1598 | bgp_init_bucket_table(c); | |
1599 | bgp_init_prefix_table(c); | |
1600 | ||
5bd73431 OZ |
1601 | c->stale_timer = tm_new_init(c->pool, bgp_long_lived_stale_timeout, c, 0, 0); |
1602 | ||
d15b0b0a OZ |
1603 | c->next_hop_addr = c->cf->next_hop_addr; |
1604 | c->link_addr = IPA_NONE; | |
1605 | c->packets_to_send = 0; | |
1606 | ||
1607 | /* Try to use source address as next hop address */ | |
1608 | if (ipa_zero(c->next_hop_addr)) | |
1609 | { | |
ef57b70f | 1610 | if (bgp_channel_is_ipv4(c) && (ipa_is_ip4(src) || c->ext_next_hop)) |
d15b0b0a OZ |
1611 | c->next_hop_addr = src; |
1612 | ||
ef57b70f | 1613 | if (bgp_channel_is_ipv6(c) && (ipa_is_ip6(src) || c->ext_next_hop)) |
d15b0b0a OZ |
1614 | c->next_hop_addr = src; |
1615 | } | |
1616 | ||
ccee67ca OZ |
1617 | /* Use preferred addresses associated with interface / source address */ |
1618 | if (ipa_zero(c->next_hop_addr)) | |
1619 | { | |
1620 | /* We know the iface for single-hop, we make lookup for multihop */ | |
586c1800 | 1621 | struct neighbor *nbr = p->neigh ?: neigh_find(&p->p, src, NULL, 0); |
ccee67ca OZ |
1622 | struct iface *iface = nbr ? nbr->iface : NULL; |
1623 | ||
1624 | if (bgp_channel_is_ipv4(c) && iface && iface->addr4) | |
1625 | c->next_hop_addr = iface->addr4->ip; | |
1626 | ||
1627 | if (bgp_channel_is_ipv6(c) && iface && iface->addr6) | |
1628 | c->next_hop_addr = iface->addr6->ip; | |
1629 | } | |
1630 | ||
ef57b70f OZ |
1631 | /* Exit if no feasible next hop address is found */ |
1632 | if (ipa_zero(c->next_hop_addr)) | |
1633 | { | |
1634 | log(L_WARN "%s: Missing next hop address", p->p.name); | |
1635 | return 0; | |
1636 | } | |
1637 | ||
d15b0b0a | 1638 | /* Set link-local address for IPv6 single-hop BGP */ |
ef57b70f | 1639 | if (ipa_is_ip6(c->next_hop_addr) && p->neigh) |
d15b0b0a OZ |
1640 | { |
1641 | c->link_addr = p->link_addr; | |
1642 | ||
1643 | if (ipa_zero(c->link_addr)) | |
1644 | log(L_WARN "%s: Missing link-local address", p->p.name); | |
1645 | } | |
1646 | ||
ef57b70f OZ |
1647 | /* Link local address is already in c->link_addr */ |
1648 | if (ipa_is_link_local(c->next_hop_addr)) | |
1649 | c->next_hop_addr = IPA_NONE; | |
d15b0b0a OZ |
1650 | |
1651 | return 0; /* XXXX: Currently undefined */ | |
1652 | } | |
1653 | ||
1654 | static void | |
1655 | bgp_channel_shutdown(struct channel *C) | |
1656 | { | |
1657 | struct bgp_channel *c = (void *) C; | |
1658 | ||
d15b0b0a OZ |
1659 | c->next_hop_addr = IPA_NONE; |
1660 | c->link_addr = IPA_NONE; | |
7fc55925 | 1661 | c->packets_to_send = 0; |
d15b0b0a OZ |
1662 | } |
1663 | ||
1664 | static void | |
1665 | bgp_channel_cleanup(struct channel *C) | |
1666 | { | |
1667 | struct bgp_channel *c = (void *) C; | |
1668 | ||
ef57b70f OZ |
1669 | if (c->igp_table_ip4) |
1670 | rt_unlock_table(c->igp_table_ip4); | |
1671 | ||
1672 | if (c->igp_table_ip6) | |
1673 | rt_unlock_table(c->igp_table_ip6); | |
1674 | } | |
1675 | ||
1676 | static inline struct bgp_channel_config * | |
1677 | bgp_find_channel_config(struct bgp_config *cf, u32 afi) | |
1678 | { | |
1679 | struct bgp_channel_config *cc; | |
1680 | ||
1681 | WALK_LIST(cc, cf->c.channels) | |
1682 | if (cc->afi == afi) | |
1683 | return cc; | |
1684 | ||
1685 | return NULL; | |
d15b0b0a | 1686 | } |
a7f23f58 | 1687 | |
ef57b70f OZ |
1688 | struct rtable_config * |
1689 | bgp_default_igp_table(struct bgp_config *cf, struct bgp_channel_config *cc, u32 type) | |
1690 | { | |
1691 | struct bgp_channel_config *cc2; | |
1692 | struct rtable_config *tab; | |
1693 | ||
1694 | /* First, try table connected by the channel */ | |
1695 | if (cc->c.table->addr_type == type) | |
1696 | return cc->c.table; | |
1697 | ||
1698 | /* Find paired channel with the same SAFI but the other AFI */ | |
1699 | u32 afi2 = cc->afi ^ 0x30000; | |
1700 | cc2 = bgp_find_channel_config(cf, afi2); | |
1701 | ||
1702 | /* Second, try IGP table configured in the paired channel */ | |
1703 | if (cc2 && (tab = (type == NET_IP4) ? cc2->igp_table_ip4 : cc2->igp_table_ip6)) | |
1704 | return tab; | |
1705 | ||
1706 | /* Third, try table connected by the paired channel */ | |
1707 | if (cc2 && (cc2->c.table->addr_type == type)) | |
1708 | return cc2->c.table; | |
1709 | ||
1710 | /* Last, try default table of given type */ | |
1711 | if (tab = cf->c.global->def_tables[type]) | |
1712 | return tab; | |
1713 | ||
1714 | cf_error("Undefined IGP table"); | |
1715 | } | |
1716 | ||
1717 | ||
a7f23f58 | 1718 | void |
d15b0b0a | 1719 | bgp_postconfig(struct proto_config *CF) |
a7f23f58 | 1720 | { |
d15b0b0a OZ |
1721 | struct bgp_config *cf = (void *) CF; |
1722 | int internal = (cf->local_as == cf->remote_as); | |
3831b619 | 1723 | int interior = internal || cf->confederation_member; |
a7f23f58 OZ |
1724 | |
1725 | /* Do not check templates at all */ | |
d15b0b0a | 1726 | if (cf->c.class == SYM_TEMPLATE) |
a7f23f58 OZ |
1727 | return; |
1728 | ||
f3e59178 OZ |
1729 | |
1730 | /* EBGP direct by default, IBGP multihop by default */ | |
d15b0b0a OZ |
1731 | if (cf->multihop < 0) |
1732 | cf->multihop = internal ? 64 : 0; | |
f3e59178 | 1733 | |
5bd73431 OZ |
1734 | /* LLGR mode default based on GR mode */ |
1735 | if (cf->llgr_mode < 0) | |
1736 | cf->llgr_mode = cf->gr_mode ? BGP_LLGR_AWARE : 0; | |
1737 | ||
dea98864 OZ |
1738 | /* Link check for single-hop BGP by default */ |
1739 | if (cf->check_link < 0) | |
1740 | cf->check_link = !cf->multihop; | |
1741 | ||
f3e59178 | 1742 | |
d15b0b0a | 1743 | if (!cf->local_as) |
a7f23f58 OZ |
1744 | cf_error("Local AS number must be set"); |
1745 | ||
d15b0b0a | 1746 | if (ipa_zero(cf->remote_ip)) |
a7f23f58 OZ |
1747 | cf_error("Neighbor must be configured"); |
1748 | ||
d15b0b0a | 1749 | if (!cf->remote_as) |
a1beb8f3 OZ |
1750 | cf_error("Remote AS number must be set"); |
1751 | ||
e919601a | 1752 | if (ipa_is_link_local(cf->remote_ip) && !cf->iface) |
33b6c292 | 1753 | cf_error("Link-local neighbor address requires specified interface"); |
a1beb8f3 | 1754 | |
d15b0b0a | 1755 | if (!(cf->capabilities && cf->enable_as4) && (cf->remote_as > 0xFFFF)) |
a7f23f58 OZ |
1756 | cf_error("Neighbor AS number out of range (AS4 not available)"); |
1757 | ||
d15b0b0a | 1758 | if (!internal && cf->rr_client) |
a7f23f58 OZ |
1759 | cf_error("Only internal neighbor can be RR client"); |
1760 | ||
d15b0b0a | 1761 | if (internal && cf->rs_client) |
a7f23f58 OZ |
1762 | cf_error("Only external neighbor can be RS client"); |
1763 | ||
d15b0b0a OZ |
1764 | if (!cf->confederation && cf->confederation_member) |
1765 | cf_error("Confederation ID must be set for member sessions"); | |
a7f23f58 | 1766 | |
d15b0b0a OZ |
1767 | if (cf->multihop && (ipa_is_link_local(cf->local_ip) || |
1768 | ipa_is_link_local(cf->remote_ip))) | |
53ffbff3 OZ |
1769 | cf_error("Multihop BGP cannot be used with link-local addresses"); |
1770 | ||
e919601a | 1771 | if (cf->multihop && cf->iface) |
33b6c292 OZ |
1772 | cf_error("Multihop BGP cannot be bound to interface"); |
1773 | ||
d15b0b0a | 1774 | if (cf->multihop && cf->check_link) |
523f020b OZ |
1775 | cf_error("Multihop BGP cannot depend on link state"); |
1776 | ||
d15b0b0a OZ |
1777 | if (cf->multihop && cf->bfd && ipa_zero(cf->local_ip)) |
1778 | cf_error("Multihop BGP with BFD requires specified local address"); | |
1779 | ||
5bd73431 OZ |
1780 | if (!cf->gr_mode && cf->llgr_mode) |
1781 | cf_error("Long-lived graceful restart requires basic graceful restart"); | |
1782 | ||
d15b0b0a OZ |
1783 | |
1784 | struct bgp_channel_config *cc; | |
1785 | WALK_LIST(cc, CF->channels) | |
1786 | { | |
3831b619 OZ |
1787 | /* Handle undefined import filter */ |
1788 | if (cc->c.in_filter == FILTER_UNDEF) | |
1789 | if (interior) | |
1790 | cc->c.in_filter = FILTER_ACCEPT; | |
1791 | else | |
1792 | cf_error("EBGP requires explicit import policy"); | |
1793 | ||
1794 | /* Handle undefined export filter */ | |
1795 | if (cc->c.out_filter == FILTER_UNDEF) | |
1796 | if (interior) | |
1797 | cc->c.out_filter = FILTER_REJECT; | |
1798 | else | |
1799 | cf_error("EBGP requires explicit export policy"); | |
1800 | ||
d15b0b0a OZ |
1801 | /* Disable after error incompatible with restart limit action */ |
1802 | if ((cc->c.in_limit.action == PLA_RESTART) && cf->disable_after_error) | |
1803 | cc->c.in_limit.action = PLA_DISABLE; | |
1804 | ||
1805 | /* Different default based on rs_client */ | |
1806 | if (!cc->missing_lladdr) | |
1807 | cc->missing_lladdr = cf->rs_client ? MLL_IGNORE : MLL_SELF; | |
1808 | ||
1809 | /* Different default for gw_mode */ | |
1810 | if (!cc->gw_mode) | |
1811 | cc->gw_mode = cf->multihop ? GW_RECURSIVE : GW_DIRECT; | |
1ec52253 | 1812 | |
5bd73431 | 1813 | /* Defaults based on proto config */ |
d15b0b0a OZ |
1814 | if (cc->gr_able == 0xff) |
1815 | cc->gr_able = (cf->gr_mode == BGP_GR_ABLE); | |
26822d8f | 1816 | |
5bd73431 OZ |
1817 | if (cc->llgr_able == 0xff) |
1818 | cc->llgr_able = (cf->llgr_mode == BGP_LLGR_ABLE); | |
1819 | ||
1820 | if (cc->llgr_time == ~0U) | |
1821 | cc->llgr_time = cf->llgr_time; | |
1822 | ||
6fe11c99 | 1823 | /* Default values of IGP tables */ |
ef57b70f OZ |
1824 | if ((cc->gw_mode == GW_RECURSIVE) && !cc->desc->no_igp) |
1825 | { | |
1826 | if (!cc->igp_table_ip4 && (bgp_cc_is_ipv4(cc) || cc->ext_next_hop)) | |
1827 | cc->igp_table_ip4 = bgp_default_igp_table(cf, cc, NET_IP4); | |
1828 | ||
1829 | if (!cc->igp_table_ip6 && (bgp_cc_is_ipv6(cc) || cc->ext_next_hop)) | |
1830 | cc->igp_table_ip6 = bgp_default_igp_table(cf, cc, NET_IP6); | |
6fe11c99 OZ |
1831 | |
1832 | if (cc->igp_table_ip4 && bgp_cc_is_ipv6(cc) && !cc->ext_next_hop) | |
1833 | cf_error("Mismatched IGP table type"); | |
1834 | ||
1835 | if (cc->igp_table_ip6 && bgp_cc_is_ipv4(cc) && !cc->ext_next_hop) | |
1836 | cf_error("Mismatched IGP table type"); | |
ef57b70f OZ |
1837 | } |
1838 | ||
d15b0b0a OZ |
1839 | if (cf->multihop && (cc->gw_mode == GW_DIRECT)) |
1840 | cf_error("Multihop BGP cannot use direct gateway mode"); | |
26822d8f | 1841 | |
d15b0b0a OZ |
1842 | if ((cc->gw_mode == GW_RECURSIVE) && cc->c.table->sorted) |
1843 | cf_error("BGP in recursive mode prohibits sorted table"); | |
1844 | ||
1845 | if (cf->deterministic_med && cc->c.table->sorted) | |
1846 | cf_error("BGP with deterministic MED prohibits sorted table"); | |
1847 | ||
1848 | if (cc->secondary && !cc->c.table->sorted) | |
1849 | cf_error("BGP with secondary option requires sorted table"); | |
1850 | } | |
a7f23f58 OZ |
1851 | } |
1852 | ||
1853 | static int | |
d15b0b0a | 1854 | bgp_reconfigure(struct proto *P, struct proto_config *CF) |
a7f23f58 | 1855 | { |
d15b0b0a OZ |
1856 | struct bgp_proto *p = (void *) P; |
1857 | struct bgp_config *new = (void *) CF; | |
a7f23f58 OZ |
1858 | struct bgp_config *old = p->cf; |
1859 | ||
d15b0b0a | 1860 | if (proto_get_router_id(CF) != p->local_id) |
79b4e12e OZ |
1861 | return 0; |
1862 | ||
a7f23f58 OZ |
1863 | int same = !memcmp(((byte *) old) + sizeof(struct proto_config), |
1864 | ((byte *) new) + sizeof(struct proto_config), | |
1865 | // password item is last and must be checked separately | |
1866 | OFFSETOF(struct bgp_config, password) - sizeof(struct proto_config)) | |
1867 | && ((!old->password && !new->password) | |
d15b0b0a OZ |
1868 | || (old->password && new->password && !strcmp(old->password, new->password))); |
1869 | ||
1870 | /* FIXME: Move channel reconfiguration to generic protocol code ? */ | |
1871 | struct channel *C, *C2; | |
1872 | struct bgp_channel_config *cc; | |
1873 | ||
1874 | WALK_LIST(C, p->p.channels) | |
1875 | C->stale = 1; | |
1876 | ||
1877 | WALK_LIST(cc, new->c.channels) | |
1878 | { | |
1879 | C = (struct channel *) bgp_find_channel(p, cc->afi); | |
1880 | same = proto_configure_channel(P, &C, &cc->c) && same; | |
1881 | C->stale = 0; | |
1882 | } | |
1883 | ||
1884 | WALK_LIST_DELSAFE(C, C2, p->p.channels) | |
1885 | if (C->stale) | |
1886 | same = proto_configure_channel(P, &C, NULL) && same; | |
1887 | ||
a7f23f58 | 1888 | |
1ec52253 OZ |
1889 | if (same && (p->start_state > BSS_PREPARE)) |
1890 | bgp_update_bfd(p, new->bfd); | |
1891 | ||
a7f23f58 OZ |
1892 | /* We should update our copy of configuration ptr as old configuration will be freed */ |
1893 | if (same) | |
1894 | p->cf = new; | |
1895 | ||
1896 | return same; | |
1897 | } | |
1898 | ||
ffb38dfb OZ |
1899 | #define IGP_TABLE(cf, sym) ((cf)->igp_table_##sym ? (cf)->igp_table_##sym ->table : NULL ) |
1900 | ||
d15b0b0a OZ |
1901 | static int |
1902 | bgp_channel_reconfigure(struct channel *C, struct channel_config *CC) | |
1903 | { | |
1904 | struct bgp_channel *c = (void *) C; | |
1905 | struct bgp_channel_config *new = (void *) CC; | |
1906 | struct bgp_channel_config *old = c->cf; | |
1907 | ||
1908 | if (memcmp(((byte *) old) + sizeof(struct channel_config), | |
1909 | ((byte *) new) + sizeof(struct channel_config), | |
ef57b70f OZ |
1910 | /* Remaining items must be checked separately */ |
1911 | OFFSETOF(struct bgp_channel_config, rest) - sizeof(struct channel_config))) | |
d15b0b0a OZ |
1912 | return 0; |
1913 | ||
ef57b70f | 1914 | /* Check change in IGP tables */ |
ffb38dfb OZ |
1915 | if ((IGP_TABLE(old, ip4) != IGP_TABLE(new, ip4)) || |
1916 | (IGP_TABLE(old, ip6) != IGP_TABLE(new, ip6))) | |
d15b0b0a OZ |
1917 | return 0; |
1918 | ||
1919 | c->cf = new; | |
1920 | return 1; | |
1921 | } | |
1922 | ||
a7f23f58 | 1923 | static void |
d15b0b0a | 1924 | bgp_copy_config(struct proto_config *dest UNUSED, struct proto_config *src UNUSED) |
a7f23f58 OZ |
1925 | { |
1926 | /* Just a shallow copy */ | |
a7f23f58 OZ |
1927 | } |
1928 | ||
1929 | ||
54e55169 MM |
1930 | /** |
1931 | * bgp_error - report a protocol error | |
1932 | * @c: connection | |
1933 | * @code: error code (according to the RFC) | |
2e9b2421 | 1934 | * @subcode: error sub-code |
54e55169 MM |
1935 | * @data: data to be passed in the Notification message |
1936 | * @len: length of the data | |
1937 | * | |
1938 | * bgp_error() sends a notification packet to tell the other side that a protocol | |
2e9b2421 | 1939 | * error has occurred (including the data considered erroneous if possible) and |
54e55169 MM |
1940 | * closes the connection. |
1941 | */ | |
3fdbafb6 | 1942 | void |
d15b0b0a | 1943 | bgp_error(struct bgp_conn *c, uint code, uint subcode, byte *data, int len) |
3fdbafb6 | 1944 | { |
b99d3786 OZ |
1945 | struct bgp_proto *p = c->bgp; |
1946 | ||
11b32d91 | 1947 | if (c->state == BS_CLOSE) |
3fdbafb6 | 1948 | return; |
11b32d91 | 1949 | |
d15b0b0a | 1950 | bgp_log_error(p, BE_BGP_TX, "Error", code, subcode, data, ABS(len)); |
b99d3786 | 1951 | bgp_store_error(p, c, BE_BGP_TX, (code << 16) | subcode); |
11b32d91 OZ |
1952 | bgp_conn_enter_close_state(c); |
1953 | ||
3fdbafb6 MM |
1954 | c->notify_code = code; |
1955 | c->notify_subcode = subcode; | |
efcece2d MM |
1956 | c->notify_data = data; |
1957 | c->notify_size = (len > 0) ? len : 0; | |
d15b0b0a | 1958 | bgp_schedule_packet(c, NULL, PKT_NOTIFICATION); |
b99d3786 OZ |
1959 | |
1960 | if (code != 6) | |
d15b0b0a OZ |
1961 | { |
1962 | bgp_update_startup_delay(p); | |
830ba75e | 1963 | bgp_stop(p, 0, NULL, 0); |
d15b0b0a | 1964 | } |
3fdbafb6 MM |
1965 | } |
1966 | ||
11b32d91 OZ |
1967 | /** |
1968 | * bgp_store_error - store last error for status report | |
1969 | * @p: BGP instance | |
1970 | * @c: connection | |
1971 | * @class: error class (BE_xxx constants) | |
1972 | * @code: error code (class specific) | |
1973 | * | |
1974 | * bgp_store_error() decides whether given error is interesting enough | |
1975 | * and store that error to last_error variables of @p | |
1976 | */ | |
1977 | void | |
1978 | bgp_store_error(struct bgp_proto *p, struct bgp_conn *c, u8 class, u32 code) | |
1979 | { | |
1980 | /* During PS_UP, we ignore errors on secondary connection */ | |
1981 | if ((p->p.proto_state == PS_UP) && c && (c != p->conn)) | |
1982 | return; | |
1983 | ||
1984 | /* During PS_STOP, we ignore any errors, as we want to report | |
1985 | * the error that caused transition to PS_STOP | |
1986 | */ | |
1987 | if (p->p.proto_state == PS_STOP) | |
1988 | return; | |
1989 | ||
1990 | p->last_error_class = class; | |
1991 | p->last_error_code = code; | |
1992 | } | |
1993 | ||
11b32d91 | 1994 | static char *bgp_state_names[] = { "Idle", "Connect", "Active", "OpenSent", "OpenConfirm", "Established", "Close" }; |
72b28a04 | 1995 | static char *bgp_err_classes[] = { "", "Error: ", "Socket: ", "Received: ", "BGP Error: ", "Automatic shutdown: ", ""}; |
523f020b | 1996 | static char *bgp_misc_errors[] = { "", "Neighbor lost", "Invalid next hop", "Kernel MD5 auth failed", "No listening socket", "Link down", "BFD session down", "Graceful restart"}; |
72b28a04 | 1997 | static char *bgp_auto_errors[] = { "", "Route limit exceeded"}; |
5bd73431 | 1998 | static char *bgp_gr_states[] = { "None", "Regular", "Long-lived"}; |
11b32d91 | 1999 | |
b8113a5e OZ |
2000 | static const char * |
2001 | bgp_last_errmsg(struct bgp_proto *p) | |
973399ae | 2002 | { |
11b32d91 | 2003 | switch (p->last_error_class) |
d15b0b0a OZ |
2004 | { |
2005 | case BE_MISC: | |
2006 | return bgp_misc_errors[p->last_error_code]; | |
2007 | case BE_SOCKET: | |
2008 | return (p->last_error_code == 0) ? "Connection closed" : strerror(p->last_error_code); | |
2009 | case BE_BGP_RX: | |
2010 | case BE_BGP_TX: | |
2011 | return bgp_error_dsc(p->last_error_code >> 16, p->last_error_code & 0xFF); | |
2012 | case BE_AUTO_DOWN: | |
2013 | return bgp_auto_errors[p->last_error_code]; | |
2014 | default: | |
2015 | return ""; | |
2016 | } | |
b8113a5e OZ |
2017 | } |
2018 | ||
2019 | static const char * | |
2020 | bgp_state_dsc(struct bgp_proto *p) | |
2021 | { | |
51947659 OZ |
2022 | if (p->p.proto_state == PS_DOWN) |
2023 | return "Down"; | |
b8113a5e OZ |
2024 | |
2025 | int state = MAX(p->incoming_conn.state, p->outgoing_conn.state); | |
2026 | if ((state == BS_IDLE) && (p->start_state >= BSS_CONNECT) && p->cf->passive) | |
2027 | return "Passive"; | |
2028 | ||
2029 | return bgp_state_names[state]; | |
2030 | } | |
2031 | ||
2032 | static void | |
2033 | bgp_get_status(struct proto *P, byte *buf) | |
2034 | { | |
2035 | struct bgp_proto *p = (struct bgp_proto *) P; | |
2036 | ||
2037 | const char *err1 = bgp_err_classes[p->last_error_class]; | |
2038 | const char *err2 = bgp_last_errmsg(p); | |
11b32d91 | 2039 | |
f4ab2317 | 2040 | if (P->proto_state == PS_DOWN) |
11b32d91 | 2041 | bsprintf(buf, "%s%s", err1, err2); |
f4ab2317 | 2042 | else |
b8113a5e OZ |
2043 | bsprintf(buf, "%-14s%s%s", bgp_state_dsc(p), err1, err2); |
2044 | } | |
2045 | ||
256cc8ee OZ |
2046 | static void |
2047 | bgp_show_afis(int code, char *s, u32 *afis, uint count) | |
2048 | { | |
2049 | buffer b; | |
2050 | LOG_BUFFER_INIT(b); | |
2051 | ||
2052 | buffer_puts(&b, s); | |
2053 | ||
2054 | for (u32 *af = afis; af < (afis + count); af++) | |
2055 | { | |
2056 | const struct bgp_af_desc *desc = bgp_get_af_desc(*af); | |
2057 | if (desc) | |
2058 | buffer_print(&b, " %s", desc->name); | |
2059 | else | |
2060 | buffer_print(&b, " <%u/%u>", BGP_AFI(*af), BGP_SAFI(*af)); | |
2061 | } | |
2062 | ||
2063 | if (b.pos == b.end) | |
2064 | strcpy(b.end - 32, " ... <too long>"); | |
2065 | ||
2066 | cli_msg(code, b.start); | |
2067 | } | |
2068 | ||
2069 | static void | |
2070 | bgp_show_capabilities(struct bgp_proto *p UNUSED, struct bgp_caps *caps) | |
2071 | { | |
2072 | struct bgp_af_caps *ac; | |
2073 | uint any_mp_bgp = 0; | |
2074 | uint any_gr_able = 0; | |
2075 | uint any_add_path = 0; | |
d8022d26 | 2076 | uint any_ext_next_hop = 0; |
5bd73431 | 2077 | uint any_llgr_able = 0; |
256cc8ee OZ |
2078 | u32 *afl1 = alloca(caps->af_count * sizeof(u32)); |
2079 | u32 *afl2 = alloca(caps->af_count * sizeof(u32)); | |
2080 | uint afn1, afn2; | |
2081 | ||
2082 | WALK_AF_CAPS(caps, ac) | |
2083 | { | |
2084 | any_mp_bgp |= ac->ready; | |
2085 | any_gr_able |= ac->gr_able; | |
2086 | any_add_path |= ac->add_path; | |
d8022d26 | 2087 | any_ext_next_hop |= ac->ext_next_hop; |
5bd73431 | 2088 | any_llgr_able |= ac->llgr_able; |
256cc8ee OZ |
2089 | } |
2090 | ||
2091 | if (any_mp_bgp) | |
2092 | { | |
2093 | cli_msg(-1006, " Multiprotocol"); | |
2094 | ||
2095 | afn1 = 0; | |
2096 | WALK_AF_CAPS(caps, ac) | |
2097 | if (ac->ready) | |
2098 | afl1[afn1++] = ac->afi; | |
2099 | ||
2100 | bgp_show_afis(-1006, " AF announced:", afl1, afn1); | |
2101 | } | |
2102 | ||
2103 | if (caps->route_refresh) | |
2104 | cli_msg(-1006, " Route refresh"); | |
2105 | ||
d8022d26 OZ |
2106 | if (any_ext_next_hop) |
2107 | { | |
2108 | cli_msg(-1006, " Extended next hop"); | |
2109 | ||
2110 | afn1 = 0; | |
2111 | WALK_AF_CAPS(caps, ac) | |
2112 | if (ac->ext_next_hop) | |
2113 | afl1[afn1++] = ac->afi; | |
2114 | ||
2115 | bgp_show_afis(-1006, " IPv6 nexthop:", afl1, afn1); | |
2116 | } | |
2117 | ||
256cc8ee OZ |
2118 | if (caps->ext_messages) |
2119 | cli_msg(-1006, " Extended message"); | |
2120 | ||
2121 | if (caps->gr_aware) | |
2122 | cli_msg(-1006, " Graceful restart"); | |
2123 | ||
2124 | if (any_gr_able) | |
2125 | { | |
2126 | /* Continues from gr_aware */ | |
2127 | cli_msg(-1006, " Restart time: %u", caps->gr_time); | |
2128 | if (caps->gr_flags & BGP_GRF_RESTART) | |
2129 | cli_msg(-1006, " Restart recovery"); | |
2130 | ||
2131 | afn1 = afn2 = 0; | |
2132 | WALK_AF_CAPS(caps, ac) | |
2133 | { | |
2134 | if (ac->gr_able) | |
2135 | afl1[afn1++] = ac->afi; | |
2136 | ||
2137 | if (ac->gr_af_flags & BGP_GRF_FORWARDING) | |
2138 | afl2[afn2++] = ac->afi; | |
2139 | } | |
2140 | ||
2141 | bgp_show_afis(-1006, " AF supported:", afl1, afn1); | |
2142 | bgp_show_afis(-1006, " AF preserved:", afl2, afn2); | |
2143 | } | |
2144 | ||
2145 | if (caps->as4_support) | |
2146 | cli_msg(-1006, " 4-octet AS numbers"); | |
2147 | ||
2148 | if (any_add_path) | |
2149 | { | |
2150 | cli_msg(-1006, " ADD-PATH"); | |
2151 | ||
2152 | afn1 = afn2 = 0; | |
2153 | WALK_AF_CAPS(caps, ac) | |
2154 | { | |
2155 | if (ac->add_path & BGP_ADD_PATH_RX) | |
2156 | afl1[afn1++] = ac->afi; | |
2157 | ||
2158 | if (ac->add_path & BGP_ADD_PATH_TX) | |
2159 | afl2[afn2++] = ac->afi; | |
2160 | } | |
2161 | ||
2162 | bgp_show_afis(-1006, " RX:", afl1, afn1); | |
2163 | bgp_show_afis(-1006, " TX:", afl2, afn2); | |
2164 | } | |
2165 | ||
2166 | if (caps->enhanced_refresh) | |
2167 | cli_msg(-1006, " Enhanced refresh"); | |
5bd73431 OZ |
2168 | |
2169 | if (caps->llgr_aware) | |
2170 | cli_msg(-1006, " Long-lived graceful restart"); | |
2171 | ||
2172 | if (any_llgr_able) | |
2173 | { | |
2174 | u32 stale_time = 0; | |
2175 | ||
2176 | afn1 = afn2 = 0; | |
2177 | WALK_AF_CAPS(caps, ac) | |
2178 | { | |
2179 | stale_time = MAX(stale_time, ac->llgr_time); | |
2180 | ||
2181 | if (ac->llgr_able && ac->llgr_time) | |
2182 | afl1[afn1++] = ac->afi; | |
2183 | ||
2184 | if (ac->llgr_flags & BGP_GRF_FORWARDING) | |
2185 | afl2[afn2++] = ac->afi; | |
2186 | } | |
2187 | ||
2188 | /* Continues from llgr_aware */ | |
2189 | cli_msg(-1006, " LL stale time: %u", stale_time); | |
2190 | ||
2191 | bgp_show_afis(-1006, " AF supported:", afl1, afn1); | |
2192 | bgp_show_afis(-1006, " AF preserved:", afl2, afn2); | |
2193 | } | |
256cc8ee OZ |
2194 | } |
2195 | ||
b8113a5e OZ |
2196 | static void |
2197 | bgp_show_proto_info(struct proto *P) | |
2198 | { | |
2199 | struct bgp_proto *p = (struct bgp_proto *) P; | |
b8113a5e | 2200 | |
b8113a5e | 2201 | cli_msg(-1006, " BGP state: %s", bgp_state_dsc(p)); |
53ffbff3 | 2202 | cli_msg(-1006, " Neighbor address: %I%J", p->cf->remote_ip, p->cf->iface); |
51947659 | 2203 | cli_msg(-1006, " Neighbor AS: %u", p->remote_as); |
b8113a5e | 2204 | |
d15b0b0a | 2205 | if (p->gr_active_num) |
0c791f87 OZ |
2206 | cli_msg(-1006, " Neighbor graceful restart active"); |
2207 | ||
b8113a5e | 2208 | if (P->proto_state == PS_START) |
d15b0b0a OZ |
2209 | { |
2210 | struct bgp_conn *oc = &p->outgoing_conn; | |
b8113a5e | 2211 | |
d15b0b0a | 2212 | if ((p->start_state < BSS_CONNECT) && |
a6f79ca5 | 2213 | (tm_active(p->startup_timer))) |
d3fa9e84 | 2214 | cli_msg(-1006, " Error wait: %t/%u", |
a6f79ca5 | 2215 | tm_remains(p->startup_timer), p->startup_delay); |
b8113a5e | 2216 | |
d15b0b0a | 2217 | if ((oc->state == BS_ACTIVE) && |
a6f79ca5 | 2218 | (tm_active(oc->connect_timer))) |
d3fa9e84 | 2219 | cli_msg(-1006, " Connect delay: %t/%u", |
a6f79ca5 | 2220 | tm_remains(oc->connect_timer), p->cf->connect_delay_time); |
0c791f87 | 2221 | |
a6f79ca5 | 2222 | if (p->gr_active_num && tm_active(p->gr_timer)) |
d3fa9e84 | 2223 | cli_msg(-1006, " Restart timer: %t/-", |
a6f79ca5 | 2224 | tm_remains(p->gr_timer)); |
d15b0b0a | 2225 | } |
b8113a5e | 2226 | else if (P->proto_state == PS_UP) |
d15b0b0a OZ |
2227 | { |
2228 | cli_msg(-1006, " Neighbor ID: %R", p->remote_id); | |
256cc8ee OZ |
2229 | cli_msg(-1006, " Local capabilities"); |
2230 | bgp_show_capabilities(p, p->conn->local_caps); | |
2231 | cli_msg(-1006, " Neighbor capabilities"); | |
2232 | bgp_show_capabilities(p, p->conn->remote_caps); | |
7fc55925 OZ |
2233 | cli_msg(-1006, " Session: %s%s%s%s%s", |
2234 | p->is_internal ? "internal" : "external", | |
2235 | p->cf->multihop ? " multihop" : "", | |
2236 | p->rr_client ? " route-reflector" : "", | |
2237 | p->rs_client ? " route-server" : "", | |
2238 | p->as4_session ? " AS4" : ""); | |
d15b0b0a | 2239 | cli_msg(-1006, " Source address: %I", p->source_addr); |
d3fa9e84 | 2240 | cli_msg(-1006, " Hold timer: %t/%u", |
a6f79ca5 | 2241 | tm_remains(p->conn->hold_timer), p->conn->hold_time); |
d3fa9e84 | 2242 | cli_msg(-1006, " Keepalive timer: %t/%u", |
a6f79ca5 | 2243 | tm_remains(p->conn->keepalive_timer), p->conn->keepalive_time); |
d15b0b0a | 2244 | } |
b8113a5e | 2245 | |
523f020b | 2246 | if ((p->last_error_class != BE_NONE) && |
b8113a5e | 2247 | (p->last_error_class != BE_MAN_DOWN)) |
d15b0b0a OZ |
2248 | { |
2249 | const char *err1 = bgp_err_classes[p->last_error_class]; | |
2250 | const char *err2 = bgp_last_errmsg(p); | |
2251 | cli_msg(-1006, " Last error: %s%s", err1, err2); | |
2252 | } | |
2253 | ||
2254 | { | |
ef57b70f | 2255 | struct bgp_channel *c; |
d15b0b0a | 2256 | WALK_LIST(c, p->p.channels) |
ef57b70f OZ |
2257 | { |
2258 | channel_show_info(&c->c); | |
2259 | ||
5bd73431 OZ |
2260 | if (p->gr_active_num) |
2261 | cli_msg(-1006, " Neighbor GR: %s", bgp_gr_states[c->gr_active]); | |
2262 | ||
0db7a1d6 | 2263 | if (c->stale_timer && tm_active(c->stale_timer)) |
5bd73431 OZ |
2264 | cli_msg(-1006, " LL stale timer: %t/-", tm_remains(c->stale_timer)); |
2265 | ||
7fc55925 OZ |
2266 | if (c->c.channel_state == CS_UP) |
2267 | { | |
2268 | if (ipa_zero(c->link_addr)) | |
2269 | cli_msg(-1006, " BGP Next hop: %I", c->next_hop_addr); | |
2270 | else | |
2271 | cli_msg(-1006, " BGP Next hop: %I %I", c->next_hop_addr, c->link_addr); | |
2272 | } | |
ccee67ca | 2273 | |
ef57b70f OZ |
2274 | if (c->igp_table_ip4) |
2275 | cli_msg(-1006, " IGP IPv4 table: %s", c->igp_table_ip4->name); | |
2276 | ||
2277 | if (c->igp_table_ip6) | |
2278 | cli_msg(-1006, " IGP IPv6 table: %s", c->igp_table_ip6->name); | |
2279 | } | |
d15b0b0a | 2280 | } |
973399ae MM |
2281 | } |
2282 | ||
d15b0b0a OZ |
2283 | struct channel_class channel_bgp = { |
2284 | .channel_size = sizeof(struct bgp_channel), | |
2285 | .config_size = sizeof(struct bgp_channel_config), | |
2286 | .init = bgp_channel_init, | |
2287 | .start = bgp_channel_start, | |
2288 | .shutdown = bgp_channel_shutdown, | |
2289 | .cleanup = bgp_channel_cleanup, | |
2290 | .reconfigure = bgp_channel_reconfigure, | |
2291 | }; | |
2292 | ||
2638249d | 2293 | struct protocol proto_bgp = { |
4a591d4b PT |
2294 | .name = "BGP", |
2295 | .template = "bgp%d", | |
ee7e2ffd | 2296 | .class = PROTOCOL_BGP, |
4a591d4b | 2297 | .preference = DEF_PREF_BGP, |
1e37e35c | 2298 | .channel_mask = NB_IP | NB_VPN | NB_FLOW, |
d15b0b0a | 2299 | .proto_size = sizeof(struct bgp_proto), |
2bbc3083 | 2300 | .config_size = sizeof(struct bgp_config), |
d15b0b0a | 2301 | .postconfig = bgp_postconfig, |
4a591d4b PT |
2302 | .init = bgp_init, |
2303 | .start = bgp_start, | |
2304 | .shutdown = bgp_shutdown, | |
4a591d4b PT |
2305 | .reconfigure = bgp_reconfigure, |
2306 | .copy_config = bgp_copy_config, | |
2307 | .get_status = bgp_get_status, | |
2308 | .get_attr = bgp_get_attr, | |
2309 | .get_route_info = bgp_get_route_info, | |
2310 | .show_proto_info = bgp_show_proto_info | |
2638249d | 2311 | }; |