]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* BGP packet management routine. |
2 | Copyright (C) 1999 Kunihiro Ishiguro | |
3 | ||
4 | This file is part of GNU Zebra. | |
5 | ||
6 | GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU General Public License as published by the | |
8 | Free Software Foundation; either version 2, or (at your option) any | |
9 | later version. | |
10 | ||
11 | GNU Zebra is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Zebra; see the file COPYING. If not, write to the Free | |
18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
19 | 02111-1307, USA. */ | |
20 | ||
21 | #include <zebra.h> | |
22 | ||
23 | #include "thread.h" | |
24 | #include "stream.h" | |
25 | #include "network.h" | |
26 | #include "prefix.h" | |
27 | #include "command.h" | |
28 | #include "log.h" | |
29 | #include "memory.h" | |
30 | #include "sockunion.h" /* for inet_ntop () */ | |
31 | #include "linklist.h" | |
32 | #include "plist.h" | |
33 | ||
34 | #include "bgpd/bgpd.h" | |
35 | #include "bgpd/bgp_table.h" | |
36 | #include "bgpd/bgp_dump.h" | |
37 | #include "bgpd/bgp_attr.h" | |
38 | #include "bgpd/bgp_debug.h" | |
39 | #include "bgpd/bgp_fsm.h" | |
40 | #include "bgpd/bgp_route.h" | |
41 | #include "bgpd/bgp_packet.h" | |
42 | #include "bgpd/bgp_open.h" | |
43 | #include "bgpd/bgp_aspath.h" | |
44 | #include "bgpd/bgp_community.h" | |
45 | #include "bgpd/bgp_ecommunity.h" | |
46 | #include "bgpd/bgp_network.h" | |
47 | #include "bgpd/bgp_mplsvpn.h" | |
48 | #include "bgpd/bgp_advertise.h" | |
93406d87 | 49 | #include "bgpd/bgp_vty.h" |
718e3744 | 50 | |
51 | int stream_put_prefix (struct stream *, struct prefix *); | |
6b0655a2 | 52 | |
718e3744 | 53 | /* Set up BGP packet marker and packet type. */ |
54 | static int | |
55 | bgp_packet_set_marker (struct stream *s, u_char type) | |
56 | { | |
57 | int i; | |
58 | ||
59 | /* Fill in marker. */ | |
60 | for (i = 0; i < BGP_MARKER_SIZE; i++) | |
61 | stream_putc (s, 0xff); | |
62 | ||
63 | /* Dummy total length. This field is should be filled in later on. */ | |
64 | stream_putw (s, 0); | |
65 | ||
66 | /* BGP packet type. */ | |
67 | stream_putc (s, type); | |
68 | ||
69 | /* Return current stream size. */ | |
9985f83c | 70 | return stream_get_endp (s); |
718e3744 | 71 | } |
72 | ||
73 | /* Set BGP packet header size entry. If size is zero then use current | |
74 | stream size. */ | |
75 | static int | |
76 | bgp_packet_set_size (struct stream *s) | |
77 | { | |
78 | int cp; | |
79 | ||
80 | /* Preserve current pointer. */ | |
9985f83c | 81 | cp = stream_get_endp (s); |
82 | stream_putw_at (s, BGP_MARKER_SIZE, cp); | |
718e3744 | 83 | |
84 | return cp; | |
85 | } | |
86 | ||
87 | /* Add new packet to the peer. */ | |
94f2b392 | 88 | static void |
718e3744 | 89 | bgp_packet_add (struct peer *peer, struct stream *s) |
90 | { | |
91 | /* Add packet to the end of list. */ | |
92 | stream_fifo_push (peer->obuf, s); | |
93 | } | |
94 | ||
95 | /* Free first packet. */ | |
94f2b392 | 96 | static void |
718e3744 | 97 | bgp_packet_delete (struct peer *peer) |
98 | { | |
99 | stream_free (stream_fifo_pop (peer->obuf)); | |
100 | } | |
101 | ||
718e3744 | 102 | /* Check file descriptor whether connect is established. */ |
fc9a856f DS |
103 | int |
104 | bgp_connect_check (struct peer *peer, int change_state) | |
718e3744 | 105 | { |
106 | int status; | |
5228ad27 | 107 | socklen_t slen; |
718e3744 | 108 | int ret; |
109 | ||
110 | /* Anyway I have to reset read and write thread. */ | |
111 | BGP_READ_OFF (peer->t_read); | |
112 | BGP_WRITE_OFF (peer->t_write); | |
113 | ||
114 | /* Check file descriptor. */ | |
115 | slen = sizeof (status); | |
eb821189 | 116 | ret = getsockopt(peer->fd, SOL_SOCKET, SO_ERROR, (void *) &status, &slen); |
718e3744 | 117 | |
118 | /* If getsockopt is fail, this is fatal error. */ | |
119 | if (ret < 0) | |
120 | { | |
121 | zlog (peer->log, LOG_INFO, "can't get sockopt for nonblocking connect"); | |
122 | BGP_EVENT_ADD (peer, TCP_fatal_error); | |
fc9a856f | 123 | return -1; |
718e3744 | 124 | } |
125 | ||
126 | /* When status is 0 then TCP connection is established. */ | |
127 | if (status == 0) | |
128 | { | |
129 | BGP_EVENT_ADD (peer, TCP_connection_open); | |
fc9a856f | 130 | return 1; |
718e3744 | 131 | } |
132 | else | |
133 | { | |
134 | if (BGP_DEBUG (events, EVENTS)) | |
6b51474d | 135 | plog_debug (peer->log, "%s [Event] Connect failed (%s)", |
6099b3b5 | 136 | peer->host, safe_strerror (errno)); |
fc9a856f DS |
137 | if (change_state) |
138 | BGP_EVENT_ADD (peer, TCP_connection_open_failed); | |
139 | return 0; | |
718e3744 | 140 | } |
141 | } | |
142 | ||
143 | /* Make BGP update packet. */ | |
94f2b392 | 144 | static struct stream * |
718e3744 | 145 | bgp_update_packet (struct peer *peer, afi_t afi, safi_t safi) |
146 | { | |
147 | struct stream *s; | |
8c71e481 | 148 | struct stream *snlri; |
718e3744 | 149 | struct bgp_adj_out *adj; |
150 | struct bgp_advertise *adv; | |
151 | struct stream *packet; | |
152 | struct bgp_node *rn = NULL; | |
153 | struct bgp_info *binfo = NULL; | |
154 | bgp_size_t total_attr_len = 0; | |
8c71e481 | 155 | unsigned long attrlen_pos = 0; |
0a91ff55 DS |
156 | int space_remaining = 0; |
157 | int space_needed = 0; | |
8c71e481 PM |
158 | size_t mpattrlen_pos = 0; |
159 | size_t mpattr_pos = 0; | |
d889623f | 160 | int num_pfx_adv = 0; |
718e3744 | 161 | |
162 | s = peer->work; | |
163 | stream_reset (s); | |
8c71e481 PM |
164 | snlri = peer->scratch; |
165 | stream_reset (snlri); | |
718e3744 | 166 | |
167 | adv = FIFO_HEAD (&peer->sync[afi][safi]->update); | |
168 | ||
169 | while (adv) | |
170 | { | |
ed3ebfa3 PJ |
171 | assert (adv->rn); |
172 | rn = adv->rn; | |
718e3744 | 173 | adj = adv->adj; |
174 | if (adv->binfo) | |
175 | binfo = adv->binfo; | |
718e3744 | 176 | |
0a91ff55 DS |
177 | space_remaining = STREAM_CONCAT_REMAIN (s, snlri, STREAM_SIZE(s)) - |
178 | BGP_MAX_PACKET_SIZE_OVERFLOW; | |
179 | space_needed = BGP_NLRI_LENGTH + PSIZE (rn->p.prefixlen); | |
180 | ||
718e3744 | 181 | /* When remaining space can't include NLRI and it's length. */ |
0a91ff55 | 182 | if (space_remaining < space_needed) |
718e3744 | 183 | break; |
184 | ||
185 | /* If packet is empty, set attribute. */ | |
186 | if (stream_empty (s)) | |
187 | { | |
ed3ebfa3 | 188 | struct peer *from = NULL; |
8c71e481 | 189 | |
d3ddb22e | 190 | if (binfo) |
8c71e481 PM |
191 | from = binfo->peer; |
192 | ||
193 | /* 1: Write the BGP message header - 16 bytes marker, 2 bytes length, | |
194 | * one byte message type. | |
195 | */ | |
718e3744 | 196 | bgp_packet_set_marker (s, BGP_MSG_UPDATE); |
8c71e481 PM |
197 | |
198 | /* 2: withdrawn routes length */ | |
199 | stream_putw (s, 0); | |
200 | ||
201 | /* 3: total attributes length - attrlen_pos stores the position */ | |
202 | attrlen_pos = stream_get_endp (s); | |
718e3744 | 203 | stream_putw (s, 0); |
8c71e481 PM |
204 | |
205 | /* 4: if there is MP_REACH_NLRI attribute, that should be the first | |
206 | * attribute, according to draft-ietf-idr-error-handling. Save the | |
207 | * position. | |
208 | */ | |
209 | mpattr_pos = stream_get_endp(s); | |
210 | ||
211 | /* 5: Encode all the attributes, except MP_REACH_NLRI attr. */ | |
212 | total_attr_len = bgp_packet_attribute (NULL, peer, s, | |
5228ad27 | 213 | adv->baa->attr, |
8c71e481 PM |
214 | NULL, afi, safi, |
215 | from, NULL, NULL); | |
0a91ff55 DS |
216 | |
217 | space_remaining = STREAM_CONCAT_REMAIN (s, snlri, STREAM_SIZE(s)) - | |
218 | BGP_MAX_PACKET_SIZE_OVERFLOW; | |
219 | space_needed = BGP_NLRI_LENGTH + PSIZE (rn->p.prefixlen); | |
220 | ||
221 | /* If the attributes alone do not leave any room for NLRI then | |
222 | * return */ | |
223 | if (space_remaining < space_needed) | |
224 | { | |
225 | zlog_err ("%s cannot send UPDATE, the attributes do not leave " | |
226 | "room for NLRI", peer->host); | |
227 | /* Flush the FIFO update queue */ | |
228 | while (adv) | |
229 | adv = bgp_advertise_clean (peer, adv->adj, afi, safi); | |
230 | return NULL; | |
231 | } | |
718e3744 | 232 | } |
233 | ||
234 | if (afi == AFI_IP && safi == SAFI_UNICAST) | |
235 | stream_put_prefix (s, &rn->p); | |
8c71e481 PM |
236 | else |
237 | { | |
238 | /* Encode the prefix in MP_REACH_NLRI attribute */ | |
239 | struct prefix_rd *prd = NULL; | |
240 | u_char *tag = NULL; | |
241 | ||
242 | if (rn->prn) | |
243 | prd = (struct prefix_rd *) &rn->prn->p; | |
244 | if (binfo && binfo->extra) | |
245 | tag = binfo->extra->tag; | |
246 | ||
247 | if (stream_empty(snlri)) | |
248 | mpattrlen_pos = bgp_packet_mpattr_start(snlri, afi, safi, | |
249 | adv->baa->attr); | |
250 | bgp_packet_mpattr_prefix(snlri, afi, safi, &rn->p, prd, tag); | |
251 | } | |
d889623f DS |
252 | num_pfx_adv++; |
253 | ||
718e3744 | 254 | if (BGP_DEBUG (update, UPDATE_OUT)) |
14542f3e JBD |
255 | { |
256 | char buf[INET6_BUFSIZ]; | |
257 | ||
258 | zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d", | |
259 | peer->host, | |
260 | inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, INET6_BUFSIZ), | |
261 | rn->p.prefixlen); | |
262 | } | |
718e3744 | 263 | |
264 | /* Synchnorize attribute. */ | |
265 | if (adj->attr) | |
f6f434b2 | 266 | bgp_attr_unintern (&adj->attr); |
718e3744 | 267 | else |
268 | peer->scount[afi][safi]++; | |
269 | ||
270 | adj->attr = bgp_attr_intern (adv->baa->attr); | |
271 | ||
272 | adv = bgp_advertise_clean (peer, adj, afi, safi); | |
718e3744 | 273 | } |
8c71e481 | 274 | |
718e3744 | 275 | if (! stream_empty (s)) |
276 | { | |
8c71e481 PM |
277 | if (!stream_empty(snlri)) |
278 | { | |
279 | bgp_packet_mpattr_end(snlri, mpattrlen_pos); | |
280 | total_attr_len += stream_get_endp(snlri); | |
281 | } | |
282 | ||
283 | /* set the total attribute length correctly */ | |
284 | stream_putw_at (s, attrlen_pos, total_attr_len); | |
285 | ||
286 | if (!stream_empty(snlri)) | |
287 | packet = stream_dupcat(s, snlri, mpattr_pos); | |
288 | else | |
289 | packet = stream_dup (s); | |
290 | bgp_packet_set_size (packet); | |
d889623f DS |
291 | if (BGP_DEBUG (update, UPDATE_OUT)) |
292 | zlog(peer->log, LOG_DEBUG, | |
293 | "%s form UPDATE (adv) total len %d numPfx %d", | |
294 | peer->host, | |
295 | (stream_get_endp (s) - stream_get_getp (s)), num_pfx_adv); | |
718e3744 | 296 | bgp_packet_add (peer, packet); |
718e3744 | 297 | stream_reset (s); |
8c71e481 | 298 | stream_reset (snlri); |
718e3744 | 299 | return packet; |
300 | } | |
301 | return NULL; | |
93406d87 | 302 | } |
303 | ||
94f2b392 | 304 | static struct stream * |
93406d87 | 305 | bgp_update_packet_eor (struct peer *peer, afi_t afi, safi_t safi) |
306 | { | |
307 | struct stream *s; | |
308 | struct stream *packet; | |
309 | ||
750e8146 PJ |
310 | if (DISABLE_BGP_ANNOUNCE) |
311 | return NULL; | |
93406d87 | 312 | |
313 | if (BGP_DEBUG (normal, NORMAL)) | |
314 | zlog_debug ("send End-of-RIB for %s to %s", afi_safi_print (afi, safi), peer->host); | |
315 | ||
316 | s = stream_new (BGP_MAX_PACKET_SIZE); | |
317 | ||
318 | /* Make BGP update packet. */ | |
319 | bgp_packet_set_marker (s, BGP_MSG_UPDATE); | |
320 | ||
321 | /* Unfeasible Routes Length */ | |
322 | stream_putw (s, 0); | |
718e3744 | 323 | |
93406d87 | 324 | if (afi == AFI_IP && safi == SAFI_UNICAST) |
325 | { | |
326 | /* Total Path Attribute Length */ | |
327 | stream_putw (s, 0); | |
328 | } | |
329 | else | |
330 | { | |
331 | /* Total Path Attribute Length */ | |
332 | stream_putw (s, 6); | |
333 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL); | |
334 | stream_putc (s, BGP_ATTR_MP_UNREACH_NLRI); | |
335 | stream_putc (s, 3); | |
336 | stream_putw (s, afi); | |
337 | stream_putc (s, safi); | |
338 | } | |
339 | ||
340 | bgp_packet_set_size (s); | |
e83e2080 | 341 | packet = stream_dup (s); |
93406d87 | 342 | bgp_packet_add (peer, packet); |
343 | stream_free (s); | |
344 | return packet; | |
718e3744 | 345 | } |
346 | ||
347 | /* Make BGP withdraw packet. */ | |
8c71e481 PM |
348 | /* For ipv4 unicast: |
349 | 16-octet marker | 2-octet length | 1-octet type | | |
350 | 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0) | |
351 | */ | |
352 | /* For other afi/safis: | |
353 | 16-octet marker | 2-octet length | 1-octet type | | |
354 | 2-octet withdrawn route length (=0) | 2-octet attrlen | | |
355 | mp_unreach attr type | attr len | afi | safi | withdrawn prefixes | |
356 | */ | |
94f2b392 | 357 | static struct stream * |
718e3744 | 358 | bgp_withdraw_packet (struct peer *peer, afi_t afi, safi_t safi) |
359 | { | |
360 | struct stream *s; | |
361 | struct stream *packet; | |
362 | struct bgp_adj_out *adj; | |
363 | struct bgp_advertise *adv; | |
364 | struct bgp_node *rn; | |
365 | unsigned long pos; | |
366 | bgp_size_t unfeasible_len; | |
367 | bgp_size_t total_attr_len; | |
8c71e481 PM |
368 | size_t mp_start = 0; |
369 | size_t attrlen_pos = 0; | |
370 | size_t mplen_pos = 0; | |
371 | u_char first_time = 1; | |
0a91ff55 DS |
372 | int space_remaining = 0; |
373 | int space_needed = 0; | |
d889623f | 374 | int num_pfx_wd = 0; |
718e3744 | 375 | |
376 | s = peer->work; | |
377 | stream_reset (s); | |
378 | ||
379 | while ((adv = FIFO_HEAD (&peer->sync[afi][safi]->withdraw)) != NULL) | |
380 | { | |
ed3ebfa3 | 381 | assert (adv->rn); |
718e3744 | 382 | adj = adv->adj; |
383 | rn = adv->rn; | |
718e3744 | 384 | |
0a91ff55 DS |
385 | space_remaining = STREAM_REMAIN (s) - |
386 | BGP_MAX_PACKET_SIZE_OVERFLOW; | |
387 | space_needed = (BGP_NLRI_LENGTH + BGP_TOTAL_ATTR_LEN + | |
388 | PSIZE (rn->p.prefixlen)); | |
389 | ||
390 | if (space_remaining < space_needed) | |
718e3744 | 391 | break; |
392 | ||
393 | if (stream_empty (s)) | |
394 | { | |
395 | bgp_packet_set_marker (s, BGP_MSG_UPDATE); | |
8c71e481 | 396 | stream_putw (s, 0); /* unfeasible routes length */ |
718e3744 | 397 | } |
8c71e481 PM |
398 | else |
399 | first_time = 0; | |
718e3744 | 400 | |
401 | if (afi == AFI_IP && safi == SAFI_UNICAST) | |
402 | stream_put_prefix (s, &rn->p); | |
403 | else | |
404 | { | |
a3b6ea56 | 405 | struct prefix_rd *prd = NULL; |
8c71e481 | 406 | |
a3b6ea56 PJ |
407 | if (rn->prn) |
408 | prd = (struct prefix_rd *) &rn->prn->p; | |
8c71e481 PM |
409 | |
410 | /* If first time, format the MP_UNREACH header */ | |
411 | if (first_time) | |
412 | { | |
413 | attrlen_pos = stream_get_endp (s); | |
414 | /* total attr length = 0 for now. reevaluate later */ | |
415 | stream_putw (s, 0); | |
416 | mp_start = stream_get_endp (s); | |
417 | mplen_pos = bgp_packet_mpunreach_start(s, afi, safi); | |
418 | } | |
419 | ||
420 | bgp_packet_mpunreach_prefix(s, &rn->p, afi, safi, prd, NULL); | |
718e3744 | 421 | } |
d889623f | 422 | num_pfx_wd++; |
718e3744 | 423 | |
424 | if (BGP_DEBUG (update, UPDATE_OUT)) | |
14542f3e JBD |
425 | { |
426 | char buf[INET6_BUFSIZ]; | |
427 | ||
428 | zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d -- unreachable", | |
429 | peer->host, | |
430 | inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, INET6_BUFSIZ), | |
431 | rn->p.prefixlen); | |
432 | } | |
718e3744 | 433 | |
434 | peer->scount[afi][safi]--; | |
435 | ||
436 | bgp_adj_out_remove (rn, adj, peer, afi, safi); | |
437 | bgp_unlock_node (rn); | |
718e3744 | 438 | } |
439 | ||
440 | if (! stream_empty (s)) | |
441 | { | |
442 | if (afi == AFI_IP && safi == SAFI_UNICAST) | |
443 | { | |
8c71e481 | 444 | unfeasible_len |
9985f83c | 445 | = stream_get_endp (s) - BGP_HEADER_SIZE - BGP_UNFEASIBLE_LEN; |
718e3744 | 446 | stream_putw_at (s, BGP_HEADER_SIZE, unfeasible_len); |
447 | stream_putw (s, 0); | |
448 | } | |
8c71e481 PM |
449 | else |
450 | { | |
451 | /* Set the mp_unreach attr's length */ | |
452 | bgp_packet_mpunreach_end(s, mplen_pos); | |
453 | ||
454 | /* Set total path attribute length. */ | |
455 | total_attr_len = stream_get_endp(s) - mp_start; | |
456 | stream_putw_at (s, attrlen_pos, total_attr_len); | |
457 | } | |
718e3744 | 458 | bgp_packet_set_size (s); |
d889623f DS |
459 | if (BGP_DEBUG (update, UPDATE_OUT)) |
460 | zlog(peer->log, LOG_DEBUG, | |
461 | "%s form UPDATE (wd) total len %d numPfx %d", | |
462 | peer->host, | |
463 | (stream_get_endp (s) - stream_get_getp (s)), num_pfx_wd); | |
e83e2080 | 464 | packet = stream_dup (s); |
718e3744 | 465 | bgp_packet_add (peer, packet); |
466 | stream_reset (s); | |
467 | return packet; | |
468 | } | |
469 | ||
470 | return NULL; | |
471 | } | |
472 | ||
473 | void | |
474 | bgp_default_update_send (struct peer *peer, struct attr *attr, | |
475 | afi_t afi, safi_t safi, struct peer *from) | |
476 | { | |
477 | struct stream *s; | |
478 | struct stream *packet; | |
479 | struct prefix p; | |
480 | unsigned long pos; | |
481 | bgp_size_t total_attr_len; | |
718e3744 | 482 | |
750e8146 PJ |
483 | if (DISABLE_BGP_ANNOUNCE) |
484 | return; | |
718e3744 | 485 | |
486 | if (afi == AFI_IP) | |
487 | str2prefix ("0.0.0.0/0", &p); | |
488 | #ifdef HAVE_IPV6 | |
489 | else | |
490 | str2prefix ("::/0", &p); | |
491 | #endif /* HAVE_IPV6 */ | |
492 | ||
493 | /* Logging the attribute. */ | |
494 | if (BGP_DEBUG (update, UPDATE_OUT)) | |
495 | { | |
14542f3e JBD |
496 | char attrstr[BUFSIZ]; |
497 | char buf[INET6_BUFSIZ]; | |
498 | attrstr[0] = '\0'; | |
499 | ||
718e3744 | 500 | bgp_dump_attr (peer, attr, attrstr, BUFSIZ); |
6b51474d | 501 | zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d %s", |
14542f3e | 502 | peer->host, inet_ntop(p.family, &(p.u.prefix), buf, INET6_BUFSIZ), |
718e3744 | 503 | p.prefixlen, attrstr); |
504 | } | |
505 | ||
506 | s = stream_new (BGP_MAX_PACKET_SIZE); | |
507 | ||
508 | /* Make BGP update packet. */ | |
509 | bgp_packet_set_marker (s, BGP_MSG_UPDATE); | |
510 | ||
511 | /* Unfeasible Routes Length. */ | |
512 | stream_putw (s, 0); | |
513 | ||
514 | /* Make place for total attribute length. */ | |
9985f83c | 515 | pos = stream_get_endp (s); |
718e3744 | 516 | stream_putw (s, 0); |
517 | total_attr_len = bgp_packet_attribute (NULL, peer, s, attr, &p, afi, safi, from, NULL, NULL); | |
518 | ||
519 | /* Set Total Path Attribute Length. */ | |
520 | stream_putw_at (s, pos, total_attr_len); | |
521 | ||
522 | /* NLRI set. */ | |
523 | if (p.family == AF_INET && safi == SAFI_UNICAST) | |
524 | stream_put_prefix (s, &p); | |
525 | ||
526 | /* Set size. */ | |
527 | bgp_packet_set_size (s); | |
528 | ||
e83e2080 | 529 | packet = stream_dup (s); |
718e3744 | 530 | stream_free (s); |
531 | ||
532 | /* Dump packet if debug option is set. */ | |
533 | #ifdef DEBUG | |
2d74db55 | 534 | /* bgp_packet_dump (packet); */ |
718e3744 | 535 | #endif /* DEBUG */ |
536 | ||
537 | /* Add packet to the peer. */ | |
538 | bgp_packet_add (peer, packet); | |
539 | ||
eb821189 | 540 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
718e3744 | 541 | } |
542 | ||
543 | void | |
544 | bgp_default_withdraw_send (struct peer *peer, afi_t afi, safi_t safi) | |
545 | { | |
546 | struct stream *s; | |
547 | struct stream *packet; | |
548 | struct prefix p; | |
8c71e481 | 549 | unsigned long attrlen_pos = 0; |
718e3744 | 550 | unsigned long cp; |
551 | bgp_size_t unfeasible_len; | |
552 | bgp_size_t total_attr_len; | |
8c71e481 PM |
553 | size_t mp_start = 0; |
554 | size_t mplen_pos = 0; | |
718e3744 | 555 | |
750e8146 PJ |
556 | if (DISABLE_BGP_ANNOUNCE) |
557 | return; | |
718e3744 | 558 | |
559 | if (afi == AFI_IP) | |
560 | str2prefix ("0.0.0.0/0", &p); | |
561 | #ifdef HAVE_IPV6 | |
562 | else | |
563 | str2prefix ("::/0", &p); | |
564 | #endif /* HAVE_IPV6 */ | |
565 | ||
566 | total_attr_len = 0; | |
718e3744 | 567 | |
568 | if (BGP_DEBUG (update, UPDATE_OUT)) | |
14542f3e JBD |
569 | { |
570 | char buf[INET6_BUFSIZ]; | |
571 | ||
572 | zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d -- unreachable", | |
573 | peer->host, inet_ntop(p.family, &(p.u.prefix), buf, INET6_BUFSIZ), | |
574 | p.prefixlen); | |
575 | } | |
718e3744 | 576 | |
577 | s = stream_new (BGP_MAX_PACKET_SIZE); | |
578 | ||
579 | /* Make BGP update packet. */ | |
580 | bgp_packet_set_marker (s, BGP_MSG_UPDATE); | |
581 | ||
582 | /* Unfeasible Routes Length. */; | |
9985f83c | 583 | cp = stream_get_endp (s); |
718e3744 | 584 | stream_putw (s, 0); |
585 | ||
586 | /* Withdrawn Routes. */ | |
587 | if (p.family == AF_INET && safi == SAFI_UNICAST) | |
588 | { | |
589 | stream_put_prefix (s, &p); | |
590 | ||
9985f83c | 591 | unfeasible_len = stream_get_endp (s) - cp - 2; |
718e3744 | 592 | |
593 | /* Set unfeasible len. */ | |
594 | stream_putw_at (s, cp, unfeasible_len); | |
595 | ||
596 | /* Set total path attribute length. */ | |
597 | stream_putw (s, 0); | |
598 | } | |
599 | else | |
600 | { | |
8c71e481 | 601 | attrlen_pos = stream_get_endp (s); |
718e3744 | 602 | stream_putw (s, 0); |
8c71e481 PM |
603 | mp_start = stream_get_endp (s); |
604 | mplen_pos = bgp_packet_mpunreach_start(s, afi, safi); | |
605 | bgp_packet_mpunreach_prefix(s, &p, afi, safi, NULL, NULL); | |
606 | ||
607 | /* Set the mp_unreach attr's length */ | |
608 | bgp_packet_mpunreach_end(s, mplen_pos); | |
718e3744 | 609 | |
610 | /* Set total path attribute length. */ | |
8c71e481 PM |
611 | total_attr_len = stream_get_endp(s) - mp_start; |
612 | stream_putw_at (s, attrlen_pos, total_attr_len); | |
718e3744 | 613 | } |
614 | ||
615 | bgp_packet_set_size (s); | |
616 | ||
e83e2080 | 617 | packet = stream_dup (s); |
718e3744 | 618 | stream_free (s); |
619 | ||
620 | /* Add packet to the peer. */ | |
621 | bgp_packet_add (peer, packet); | |
622 | ||
eb821189 | 623 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
718e3744 | 624 | } |
625 | ||
626 | /* Get next packet to be written. */ | |
94f2b392 | 627 | static struct stream * |
718e3744 | 628 | bgp_write_packet (struct peer *peer) |
629 | { | |
630 | afi_t afi; | |
631 | safi_t safi; | |
632 | struct stream *s = NULL; | |
633 | struct bgp_advertise *adv; | |
634 | ||
635 | s = stream_fifo_head (peer->obuf); | |
636 | if (s) | |
637 | return s; | |
638 | ||
4a16ae86 DS |
639 | /* The code beyond this part deals with update packets, check if updates |
640 | are on hold as part of the update-delay post processing stages. */ | |
641 | if (peer->bgp && (peer->bgp->main_peers_update_hold || | |
642 | peer->bgp->rsclient_peers_update_hold)) | |
643 | return NULL; | |
644 | ||
718e3744 | 645 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
646 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) | |
647 | { | |
648 | adv = FIFO_HEAD (&peer->sync[afi][safi]->withdraw); | |
649 | if (adv) | |
650 | { | |
651 | s = bgp_withdraw_packet (peer, afi, safi); | |
652 | if (s) | |
653 | return s; | |
654 | } | |
655 | } | |
656 | ||
657 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
658 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) | |
659 | { | |
660 | adv = FIFO_HEAD (&peer->sync[afi][safi]->update); | |
661 | if (adv) | |
662 | { | |
cb1faec9 | 663 | if (adv->binfo && adv->binfo->uptime <= peer->synctime) |
93406d87 | 664 | { |
665 | if (CHECK_FLAG (adv->binfo->peer->cap, PEER_CAP_RESTART_RCV) | |
666 | && CHECK_FLAG (adv->binfo->peer->cap, PEER_CAP_RESTART_ADV) | |
fe7d2a48 DS |
667 | && ! (CHECK_FLAG (adv->binfo->peer->cap, |
668 | PEER_CAP_RESTART_BIT_RCV) && | |
669 | CHECK_FLAG (adv->binfo->peer->cap, | |
670 | PEER_CAP_RESTART_BIT_ADV)) | |
93406d87 | 671 | && ! CHECK_FLAG (adv->binfo->flags, BGP_INFO_STALE) |
672 | && safi != SAFI_MPLS_VPN) | |
673 | { | |
674 | if (CHECK_FLAG (adv->binfo->peer->af_sflags[afi][safi], | |
675 | PEER_STATUS_EOR_RECEIVED)) | |
676 | s = bgp_update_packet (peer, afi, safi); | |
677 | } | |
678 | else | |
679 | s = bgp_update_packet (peer, afi, safi); | |
680 | } | |
718e3744 | 681 | |
682 | if (s) | |
683 | return s; | |
684 | } | |
93406d87 | 685 | |
686 | if (CHECK_FLAG (peer->cap, PEER_CAP_RESTART_RCV)) | |
687 | { | |
688 | if (peer->afc_nego[afi][safi] && peer->synctime | |
689 | && ! CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_EOR_SEND) | |
690 | && safi != SAFI_MPLS_VPN) | |
691 | { | |
692 | SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_EOR_SEND); | |
693 | return bgp_update_packet_eor (peer, afi, safi); | |
694 | } | |
695 | } | |
718e3744 | 696 | } |
697 | ||
698 | return NULL; | |
699 | } | |
700 | ||
d889623f DS |
701 | /* Are there prefixes queued for being withdrawn? */ |
702 | int | |
703 | bgp_peer_wd_fifo_exists (struct peer *peer) | |
718e3744 | 704 | { |
705 | afi_t afi; | |
706 | safi_t safi; | |
707 | struct bgp_advertise *adv; | |
708 | ||
718e3744 | 709 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
710 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) | |
711 | if (FIFO_HEAD (&peer->sync[afi][safi]->withdraw)) | |
712 | return 1; | |
713 | ||
d889623f DS |
714 | return 0; |
715 | } | |
716 | ||
717 | /* Are there prefixes queued for being advertised? | |
718 | * Are they recent? | |
719 | */ | |
720 | int | |
721 | bgp_peer_adv_fifo_exists (struct peer *peer, int chk_recent) | |
722 | { | |
723 | afi_t afi; | |
724 | safi_t safi; | |
725 | struct bgp_advertise *adv; | |
726 | ||
718e3744 | 727 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
728 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) | |
729 | if ((adv = FIFO_HEAD (&peer->sync[afi][safi]->update)) != NULL) | |
d889623f DS |
730 | { |
731 | if (!chk_recent) | |
732 | return 1; | |
733 | if (adv->binfo->uptime < peer->synctime) | |
734 | return 1; | |
735 | } | |
736 | ||
737 | return 0; | |
738 | } | |
739 | ||
740 | /* | |
741 | * Schedule updates for the peer, if needed. | |
742 | */ | |
743 | void | |
744 | bgp_peer_schedule_updates(struct peer *peer) | |
745 | { | |
746 | /* If withdraw FIFO exists, immediately schedule write */ | |
747 | if (bgp_peer_wd_fifo_exists(peer) && !peer->t_write) | |
748 | { | |
749 | if (BGP_DEBUG (events, EVENTS)) | |
750 | zlog_debug("%s scheduling write thread", peer->host); | |
751 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); | |
752 | } | |
753 | ||
754 | /* If update FIFO exists, fire MRAI timer */ | |
755 | if (bgp_peer_adv_fifo_exists(peer, 0) && !peer->radv_adjusted) | |
756 | { | |
757 | if (BGP_DEBUG (events, EVENTS)) | |
758 | zlog_debug("%s scheduling MRAI timer", peer->host); | |
759 | bgp_adjust_routeadv(peer); | |
760 | } | |
761 | } | |
762 | ||
763 | /* Is there partially written packet or updates we can send right | |
764 | now. */ | |
765 | static int | |
766 | bgp_write_proceed (struct peer *peer) | |
767 | { | |
768 | /* If queued packet exists, we should try to write it */ | |
769 | if (stream_fifo_head (peer->obuf)) | |
770 | return 1; | |
771 | ||
772 | /* If there are prefixes to be withdrawn or to be advertised (and | |
773 | * queued before last MRAI timer expiry), schedule write | |
774 | */ | |
775 | if (bgp_peer_wd_fifo_exists(peer) | |
776 | || bgp_peer_adv_fifo_exists(peer, 1)) | |
777 | return 1; | |
718e3744 | 778 | |
779 | return 0; | |
780 | } | |
781 | ||
782 | /* Write packet to the peer. */ | |
783 | int | |
784 | bgp_write (struct thread *thread) | |
785 | { | |
786 | struct peer *peer; | |
787 | u_char type; | |
788 | struct stream *s; | |
789 | int num; | |
fd79ac91 | 790 | unsigned int count = 0; |
cb1faec9 | 791 | int oc = 0; |
718e3744 | 792 | |
793 | /* Yes first of all get peer pointer. */ | |
794 | peer = THREAD_ARG (thread); | |
795 | peer->t_write = NULL; | |
796 | ||
797 | /* For non-blocking IO check. */ | |
798 | if (peer->status == Connect) | |
799 | { | |
fc9a856f | 800 | bgp_connect_check (peer, 1); |
718e3744 | 801 | return 0; |
802 | } | |
803 | ||
eac5702d SH |
804 | s = bgp_write_packet (peer); |
805 | if (!s) | |
806 | return 0; /* nothing to send */ | |
807 | ||
808 | sockopt_cork (peer->fd, 1); | |
809 | ||
cb1faec9 DS |
810 | oc = peer->update_out; |
811 | ||
eac5702d SH |
812 | /* Nonblocking write until TCP output buffer is full. */ |
813 | do | |
718e3744 | 814 | { |
815 | int writenum; | |
718e3744 | 816 | |
817 | /* Number of bytes to be sent. */ | |
818 | writenum = stream_get_endp (s) - stream_get_getp (s); | |
819 | ||
820 | /* Call write() system call. */ | |
eb821189 | 821 | num = write (peer->fd, STREAM_PNT (s), writenum); |
35398589 | 822 | if (num < 0) |
718e3744 | 823 | { |
eac5702d SH |
824 | /* write failed either retry needed or error */ |
825 | if (ERRNO_IO_RETRY(errno)) | |
826 | break; | |
827 | ||
828 | BGP_EVENT_ADD (peer, TCP_fatal_error); | |
718e3744 | 829 | return 0; |
830 | } | |
35398589 | 831 | |
718e3744 | 832 | if (num != writenum) |
833 | { | |
35398589 | 834 | /* Partial write */ |
9985f83c | 835 | stream_forward_getp (s, num); |
eac5702d | 836 | break; |
718e3744 | 837 | } |
838 | ||
839 | /* Retrieve BGP packet type. */ | |
840 | stream_set_getp (s, BGP_MARKER_SIZE + 2); | |
841 | type = stream_getc (s); | |
842 | ||
843 | switch (type) | |
844 | { | |
845 | case BGP_MSG_OPEN: | |
846 | peer->open_out++; | |
847 | break; | |
848 | case BGP_MSG_UPDATE: | |
849 | peer->update_out++; | |
850 | break; | |
851 | case BGP_MSG_NOTIFY: | |
852 | peer->notify_out++; | |
853 | /* Double start timer. */ | |
854 | peer->v_start *= 2; | |
855 | ||
856 | /* Overflow check. */ | |
857 | if (peer->v_start >= (60 * 2)) | |
858 | peer->v_start = (60 * 2); | |
859 | ||
ca058a30 | 860 | /* Flush any existing events */ |
dcdf399f | 861 | BGP_EVENT_ADD (peer, BGP_Stop); |
3a69f74a SH |
862 | goto done; |
863 | ||
718e3744 | 864 | case BGP_MSG_KEEPALIVE: |
865 | peer->keepalive_out++; | |
866 | break; | |
867 | case BGP_MSG_ROUTE_REFRESH_NEW: | |
868 | case BGP_MSG_ROUTE_REFRESH_OLD: | |
869 | peer->refresh_out++; | |
870 | break; | |
871 | case BGP_MSG_CAPABILITY: | |
872 | peer->dynamic_cap_out++; | |
873 | break; | |
874 | } | |
875 | ||
876 | /* OK we send packet so delete it. */ | |
877 | bgp_packet_delete (peer); | |
718e3744 | 878 | } |
cb1faec9 | 879 | while (++count < peer->bgp->wpkt_quanta && |
eac5702d | 880 | (s = bgp_write_packet (peer)) != NULL); |
cb1faec9 | 881 | |
718e3744 | 882 | if (bgp_write_proceed (peer)) |
eb821189 | 883 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
3a69f74a SH |
884 | |
885 | done: | |
cb1faec9 DS |
886 | /* Update the last write if some updates were written. */ |
887 | if (peer->update_out > oc) | |
888 | peer->last_write = bgp_clock (); | |
889 | ||
3a69f74a | 890 | sockopt_cork (peer->fd, 0); |
718e3744 | 891 | return 0; |
892 | } | |
893 | ||
894 | /* This is only for sending NOTIFICATION message to neighbor. */ | |
94f2b392 | 895 | static int |
718e3744 | 896 | bgp_write_notify (struct peer *peer) |
897 | { | |
35398589 | 898 | int ret, val; |
718e3744 | 899 | u_char type; |
900 | struct stream *s; | |
901 | ||
902 | /* There should be at least one packet. */ | |
903 | s = stream_fifo_head (peer->obuf); | |
904 | if (!s) | |
905 | return 0; | |
906 | assert (stream_get_endp (s) >= BGP_HEADER_SIZE); | |
907 | ||
86998bc2 LR |
908 | /* Stop collecting data within the socket */ |
909 | sockopt_cork (peer->fd, 0); | |
910 | ||
8ff202e2 DL |
911 | /* socket is in nonblocking mode, if we can't deliver the NOTIFY, well, |
912 | * we only care about getting a clean shutdown at this point. */ | |
86998bc2 | 913 | ret = write (peer->fd, STREAM_DATA (s), stream_get_endp (s)); |
8ff202e2 DL |
914 | |
915 | /* only connection reset/close gets counted as TCP_fatal_error, failure | |
916 | * to write the entire NOTIFY doesn't get different FSM treatment */ | |
718e3744 | 917 | if (ret <= 0) |
918 | { | |
dcdf399f | 919 | BGP_EVENT_ADD (peer, TCP_fatal_error); |
718e3744 | 920 | return 0; |
921 | } | |
922 | ||
86998bc2 LR |
923 | /* Disable Nagle, make NOTIFY packet go out right away */ |
924 | val = 1; | |
925 | (void) setsockopt (peer->fd, IPPROTO_TCP, TCP_NODELAY, | |
926 | (char *) &val, sizeof (val)); | |
927 | ||
718e3744 | 928 | /* Retrieve BGP packet type. */ |
929 | stream_set_getp (s, BGP_MARKER_SIZE + 2); | |
930 | type = stream_getc (s); | |
931 | ||
932 | assert (type == BGP_MSG_NOTIFY); | |
933 | ||
934 | /* Type should be notify. */ | |
935 | peer->notify_out++; | |
936 | ||
937 | /* Double start timer. */ | |
938 | peer->v_start *= 2; | |
939 | ||
940 | /* Overflow check. */ | |
941 | if (peer->v_start >= (60 * 2)) | |
942 | peer->v_start = (60 * 2); | |
943 | ||
1ff9a340 DS |
944 | /* Handle Graceful Restart case where the state changes to |
945 | Connect instead of Idle */ | |
dcdf399f | 946 | BGP_EVENT_ADD (peer, BGP_Stop); |
718e3744 | 947 | |
948 | return 0; | |
949 | } | |
950 | ||
951 | /* Make keepalive packet and send it to the peer. */ | |
952 | void | |
953 | bgp_keepalive_send (struct peer *peer) | |
954 | { | |
955 | struct stream *s; | |
956 | int length; | |
957 | ||
958 | s = stream_new (BGP_MAX_PACKET_SIZE); | |
959 | ||
960 | /* Make keepalive packet. */ | |
961 | bgp_packet_set_marker (s, BGP_MSG_KEEPALIVE); | |
962 | ||
963 | /* Set packet size. */ | |
964 | length = bgp_packet_set_size (s); | |
965 | ||
966 | /* Dump packet if debug option is set. */ | |
967 | /* bgp_packet_dump (s); */ | |
968 | ||
969 | if (BGP_DEBUG (keepalive, KEEPALIVE)) | |
6b51474d | 970 | zlog_debug ("%s sending KEEPALIVE", peer->host); |
718e3744 | 971 | if (BGP_DEBUG (normal, NORMAL)) |
6b51474d | 972 | zlog_debug ("%s send message type %d, length (incl. header) %d", |
718e3744 | 973 | peer->host, BGP_MSG_KEEPALIVE, length); |
974 | ||
975 | /* Add packet to the peer. */ | |
976 | bgp_packet_add (peer, s); | |
977 | ||
eb821189 | 978 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
718e3744 | 979 | } |
980 | ||
981 | /* Make open packet and send it to the peer. */ | |
982 | void | |
983 | bgp_open_send (struct peer *peer) | |
984 | { | |
985 | struct stream *s; | |
986 | int length; | |
987 | u_int16_t send_holdtime; | |
988 | as_t local_as; | |
989 | ||
990 | if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER)) | |
991 | send_holdtime = peer->holdtime; | |
992 | else | |
993 | send_holdtime = peer->bgp->default_holdtime; | |
994 | ||
995 | /* local-as Change */ | |
996 | if (peer->change_local_as) | |
997 | local_as = peer->change_local_as; | |
998 | else | |
999 | local_as = peer->local_as; | |
1000 | ||
1001 | s = stream_new (BGP_MAX_PACKET_SIZE); | |
1002 | ||
1003 | /* Make open packet. */ | |
1004 | bgp_packet_set_marker (s, BGP_MSG_OPEN); | |
1005 | ||
1006 | /* Set open packet values. */ | |
1007 | stream_putc (s, BGP_VERSION_4); /* BGP version */ | |
0b2aa3a0 PJ |
1008 | stream_putw (s, (local_as <= BGP_AS_MAX) ? (u_int16_t) local_as |
1009 | : BGP_AS_TRANS); | |
718e3744 | 1010 | stream_putw (s, send_holdtime); /* Hold Time */ |
1011 | stream_put_in_addr (s, &peer->local_id); /* BGP Identifier */ | |
1012 | ||
1013 | /* Set capability code. */ | |
1014 | bgp_open_capability (s, peer); | |
1015 | ||
1016 | /* Set BGP packet length. */ | |
1017 | length = bgp_packet_set_size (s); | |
1018 | ||
1019 | if (BGP_DEBUG (normal, NORMAL)) | |
aea339f7 | 1020 | zlog_debug ("%s sending OPEN, version %d, my as %u, holdtime %d, id %s", |
718e3744 | 1021 | peer->host, BGP_VERSION_4, local_as, |
1022 | send_holdtime, inet_ntoa (peer->local_id)); | |
1023 | ||
1024 | if (BGP_DEBUG (normal, NORMAL)) | |
6b51474d | 1025 | zlog_debug ("%s send message type %d, length (incl. header) %d", |
718e3744 | 1026 | peer->host, BGP_MSG_OPEN, length); |
1027 | ||
1028 | /* Dump packet if debug option is set. */ | |
1029 | /* bgp_packet_dump (s); */ | |
1030 | ||
1031 | /* Add packet to the peer. */ | |
1032 | bgp_packet_add (peer, s); | |
1033 | ||
eb821189 | 1034 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
718e3744 | 1035 | } |
1036 | ||
1037 | /* Send BGP notify packet with data potion. */ | |
1038 | void | |
1039 | bgp_notify_send_with_data (struct peer *peer, u_char code, u_char sub_code, | |
1040 | u_char *data, size_t datalen) | |
1041 | { | |
1042 | struct stream *s; | |
1043 | int length; | |
1044 | ||
1045 | /* Allocate new stream. */ | |
1046 | s = stream_new (BGP_MAX_PACKET_SIZE); | |
1047 | ||
1048 | /* Make nitify packet. */ | |
1049 | bgp_packet_set_marker (s, BGP_MSG_NOTIFY); | |
1050 | ||
1051 | /* Set notify packet values. */ | |
1052 | stream_putc (s, code); /* BGP notify code */ | |
1053 | stream_putc (s, sub_code); /* BGP notify sub_code */ | |
1054 | ||
1055 | /* If notify data is present. */ | |
1056 | if (data) | |
1057 | stream_write (s, data, datalen); | |
1058 | ||
1059 | /* Set BGP packet length. */ | |
1060 | length = bgp_packet_set_size (s); | |
1061 | ||
1062 | /* Add packet to the peer. */ | |
1063 | stream_fifo_clean (peer->obuf); | |
1064 | bgp_packet_add (peer, s); | |
1065 | ||
1066 | /* For debug */ | |
1067 | { | |
1068 | struct bgp_notify bgp_notify; | |
1069 | int first = 0; | |
1070 | int i; | |
1071 | char c[4]; | |
1072 | ||
1073 | bgp_notify.code = code; | |
1074 | bgp_notify.subcode = sub_code; | |
1075 | bgp_notify.data = NULL; | |
1076 | bgp_notify.length = length - BGP_MSG_NOTIFY_MIN_SIZE; | |
1077 | ||
1078 | if (bgp_notify.length) | |
1079 | { | |
1080 | bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3); | |
1081 | for (i = 0; i < bgp_notify.length; i++) | |
1082 | if (first) | |
1083 | { | |
1084 | sprintf (c, " %02x", data[i]); | |
1085 | strcat (bgp_notify.data, c); | |
1086 | } | |
1087 | else | |
1088 | { | |
1089 | first = 1; | |
1090 | sprintf (c, "%02x", data[i]); | |
1091 | strcpy (bgp_notify.data, c); | |
1092 | } | |
1093 | } | |
1094 | bgp_notify_print (peer, &bgp_notify, "sending"); | |
1095 | if (bgp_notify.data) | |
1096 | XFREE (MTYPE_TMP, bgp_notify.data); | |
1097 | } | |
1098 | ||
1099 | if (BGP_DEBUG (normal, NORMAL)) | |
6b51474d | 1100 | zlog_debug ("%s send message type %d, length (incl. header) %d", |
718e3744 | 1101 | peer->host, BGP_MSG_NOTIFY, length); |
1102 | ||
e0701b79 | 1103 | /* peer reset cause */ |
1104 | if (sub_code != BGP_NOTIFY_CEASE_CONFIG_CHANGE) | |
1105 | { | |
1106 | if (sub_code == BGP_NOTIFY_CEASE_ADMIN_RESET) | |
1212dc19 | 1107 | { |
1108 | peer->last_reset = PEER_DOWN_USER_RESET; | |
1109 | zlog_info ("Notification sent to neighbor %s: User reset", peer->host); | |
1110 | } | |
e0701b79 | 1111 | else if (sub_code == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN) |
1212dc19 | 1112 | { |
1113 | peer->last_reset = PEER_DOWN_USER_SHUTDOWN; | |
1114 | zlog_info ("Notification sent to neighbor %s: shutdown", peer->host); | |
1115 | } | |
e0701b79 | 1116 | else |
1212dc19 | 1117 | { |
1118 | peer->last_reset = PEER_DOWN_NOTIFY_SEND; | |
1119 | zlog_info ("Notification sent to neighbor %s: type %u/%u", | |
1120 | peer->host, code, sub_code); | |
1121 | } | |
e0701b79 | 1122 | } |
1212dc19 | 1123 | else |
1ff9a340 DS |
1124 | zlog_info ("Notification sent to neighbor %s:%d: configuration change", |
1125 | peer->host, peer->fd); | |
e0701b79 | 1126 | |
7ccf5e59 | 1127 | /* Call immediately. */ |
718e3744 | 1128 | BGP_WRITE_OFF (peer->t_write); |
1129 | ||
1130 | bgp_write_notify (peer); | |
1131 | } | |
1132 | ||
1133 | /* Send BGP notify packet. */ | |
1134 | void | |
1135 | bgp_notify_send (struct peer *peer, u_char code, u_char sub_code) | |
1136 | { | |
1137 | bgp_notify_send_with_data (peer, code, sub_code, NULL, 0); | |
1138 | } | |
1139 | ||
718e3744 | 1140 | /* Send route refresh message to the peer. */ |
1141 | void | |
1142 | bgp_route_refresh_send (struct peer *peer, afi_t afi, safi_t safi, | |
1143 | u_char orf_type, u_char when_to_refresh, int remove) | |
1144 | { | |
1145 | struct stream *s; | |
1146 | struct stream *packet; | |
1147 | int length; | |
1148 | struct bgp_filter *filter; | |
1149 | int orf_refresh = 0; | |
1150 | ||
750e8146 PJ |
1151 | if (DISABLE_BGP_ANNOUNCE) |
1152 | return; | |
718e3744 | 1153 | |
1154 | filter = &peer->filter[afi][safi]; | |
1155 | ||
1156 | /* Adjust safi code. */ | |
1157 | if (safi == SAFI_MPLS_VPN) | |
42e6d745 | 1158 | safi = SAFI_MPLS_LABELED_VPN; |
718e3744 | 1159 | |
1160 | s = stream_new (BGP_MAX_PACKET_SIZE); | |
1161 | ||
1162 | /* Make BGP update packet. */ | |
1163 | if (CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV)) | |
1164 | bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_NEW); | |
1165 | else | |
1166 | bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_OLD); | |
1167 | ||
1168 | /* Encode Route Refresh message. */ | |
1169 | stream_putw (s, afi); | |
1170 | stream_putc (s, 0); | |
1171 | stream_putc (s, safi); | |
1172 | ||
1173 | if (orf_type == ORF_TYPE_PREFIX | |
1174 | || orf_type == ORF_TYPE_PREFIX_OLD) | |
1175 | if (remove || filter->plist[FILTER_IN].plist) | |
1176 | { | |
1177 | u_int16_t orf_len; | |
1178 | unsigned long orfp; | |
1179 | ||
1180 | orf_refresh = 1; | |
1181 | stream_putc (s, when_to_refresh); | |
1182 | stream_putc (s, orf_type); | |
9985f83c | 1183 | orfp = stream_get_endp (s); |
718e3744 | 1184 | stream_putw (s, 0); |
1185 | ||
1186 | if (remove) | |
1187 | { | |
1188 | UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND); | |
1189 | stream_putc (s, ORF_COMMON_PART_REMOVE_ALL); | |
1190 | if (BGP_DEBUG (normal, NORMAL)) | |
6b51474d | 1191 | zlog_debug ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d", |
718e3744 | 1192 | peer->host, orf_type, |
1193 | (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"), | |
1194 | afi, safi); | |
1195 | } | |
1196 | else | |
1197 | { | |
1198 | SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND); | |
1199 | prefix_bgp_orf_entry (s, filter->plist[FILTER_IN].plist, | |
1200 | ORF_COMMON_PART_ADD, ORF_COMMON_PART_PERMIT, | |
1201 | ORF_COMMON_PART_DENY); | |
1202 | if (BGP_DEBUG (normal, NORMAL)) | |
6b51474d | 1203 | zlog_debug ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d", |
718e3744 | 1204 | peer->host, orf_type, |
1205 | (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"), | |
1206 | afi, safi); | |
1207 | } | |
1208 | ||
1209 | /* Total ORF Entry Len. */ | |
9985f83c | 1210 | orf_len = stream_get_endp (s) - orfp - 2; |
718e3744 | 1211 | stream_putw_at (s, orfp, orf_len); |
1212 | } | |
1213 | ||
1214 | /* Set packet size. */ | |
1215 | length = bgp_packet_set_size (s); | |
1216 | ||
1217 | if (BGP_DEBUG (normal, NORMAL)) | |
1218 | { | |
1219 | if (! orf_refresh) | |
6b51474d | 1220 | zlog_debug ("%s sending REFRESH_REQ for afi/safi: %d/%d", |
718e3744 | 1221 | peer->host, afi, safi); |
6b51474d | 1222 | zlog_debug ("%s send message type %d, length (incl. header) %d", |
718e3744 | 1223 | peer->host, CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV) ? |
1224 | BGP_MSG_ROUTE_REFRESH_NEW : BGP_MSG_ROUTE_REFRESH_OLD, length); | |
1225 | } | |
1226 | ||
1227 | /* Make real packet. */ | |
e83e2080 | 1228 | packet = stream_dup (s); |
718e3744 | 1229 | stream_free (s); |
1230 | ||
1231 | /* Add packet to the peer. */ | |
1232 | bgp_packet_add (peer, packet); | |
1233 | ||
eb821189 | 1234 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
718e3744 | 1235 | } |
1236 | ||
1237 | /* Send capability message to the peer. */ | |
1238 | void | |
1239 | bgp_capability_send (struct peer *peer, afi_t afi, safi_t safi, | |
1240 | int capability_code, int action) | |
1241 | { | |
1242 | struct stream *s; | |
1243 | struct stream *packet; | |
1244 | int length; | |
1245 | ||
1246 | /* Adjust safi code. */ | |
1247 | if (safi == SAFI_MPLS_VPN) | |
42e6d745 | 1248 | safi = SAFI_MPLS_LABELED_VPN; |
718e3744 | 1249 | |
1250 | s = stream_new (BGP_MAX_PACKET_SIZE); | |
1251 | ||
1252 | /* Make BGP update packet. */ | |
1253 | bgp_packet_set_marker (s, BGP_MSG_CAPABILITY); | |
1254 | ||
1255 | /* Encode MP_EXT capability. */ | |
1256 | if (capability_code == CAPABILITY_CODE_MP) | |
1257 | { | |
1258 | stream_putc (s, action); | |
1259 | stream_putc (s, CAPABILITY_CODE_MP); | |
1260 | stream_putc (s, CAPABILITY_CODE_MP_LEN); | |
1261 | stream_putw (s, afi); | |
1262 | stream_putc (s, 0); | |
1263 | stream_putc (s, safi); | |
1264 | ||
1265 | if (BGP_DEBUG (normal, NORMAL)) | |
6b51474d | 1266 | zlog_debug ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d", |
718e3744 | 1267 | peer->host, action == CAPABILITY_ACTION_SET ? |
1268 | "Advertising" : "Removing", afi, safi); | |
1269 | } | |
1270 | ||
718e3744 | 1271 | /* Set packet size. */ |
1272 | length = bgp_packet_set_size (s); | |
1273 | ||
1274 | /* Make real packet. */ | |
e83e2080 | 1275 | packet = stream_dup (s); |
718e3744 | 1276 | stream_free (s); |
1277 | ||
1278 | /* Add packet to the peer. */ | |
1279 | bgp_packet_add (peer, packet); | |
1280 | ||
1281 | if (BGP_DEBUG (normal, NORMAL)) | |
6b51474d | 1282 | zlog_debug ("%s send message type %d, length (incl. header) %d", |
718e3744 | 1283 | peer->host, BGP_MSG_CAPABILITY, length); |
1284 | ||
eb821189 | 1285 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
718e3744 | 1286 | } |
6b0655a2 | 1287 | |
718e3744 | 1288 | /* RFC1771 6.8 Connection collision detection. */ |
94f2b392 | 1289 | static int |
eb821189 | 1290 | bgp_collision_detect (struct peer *new, struct in_addr remote_id) |
718e3744 | 1291 | { |
eb821189 | 1292 | struct peer *peer; |
718e3744 | 1293 | |
718e3744 | 1294 | /* Upon receipt of an OPEN message, the local system must examine |
1295 | all of its connections that are in the OpenConfirm state. A BGP | |
1296 | speaker may also examine connections in an OpenSent state if it | |
1297 | knows the BGP Identifier of the peer by means outside of the | |
1298 | protocol. If among these connections there is a connection to a | |
1299 | remote BGP speaker whose BGP Identifier equals the one in the | |
1300 | OPEN message, then the local system performs the following | |
1301 | collision resolution procedure: */ | |
1302 | ||
1ff9a340 | 1303 | if ((peer = new->doppelganger) != NULL) |
718e3744 | 1304 | { |
1ff9a340 DS |
1305 | /* Do not accept the new connection in Established or Clearing states. |
1306 | * Note that a peer GR is handled by closing the existing connection | |
1307 | * upon receipt of new one. | |
1308 | */ | |
1309 | if (peer->status == Established || peer->status == Clearing) | |
1310 | { | |
1311 | bgp_notify_send (new, BGP_NOTIFY_CEASE, | |
1312 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
1313 | return (-1); | |
1314 | } | |
1315 | else if ((peer->status == OpenConfirm) || (peer->status == OpenSent)) | |
eb821189 | 1316 | { |
718e3744 | 1317 | /* 1. The BGP Identifier of the local system is compared to |
1318 | the BGP Identifier of the remote system (as specified in | |
1319 | the OPEN message). */ | |
1320 | ||
1321 | if (ntohl (peer->local_id.s_addr) < ntohl (remote_id.s_addr)) | |
1ff9a340 DS |
1322 | if (!CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
1323 | { | |
1324 | /* 2. If the value of the local BGP Identifier is less | |
1325 | than the remote one, the local system closes BGP | |
1326 | connection that already exists (the one that is | |
1327 | already in the OpenConfirm state), and accepts BGP | |
1328 | connection initiated by the remote system. */ | |
1329 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, | |
1330 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
1331 | return 1; | |
1332 | } | |
1333 | else | |
1334 | { | |
1335 | bgp_notify_send (new, BGP_NOTIFY_CEASE, | |
1336 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
1337 | return -1; | |
1338 | } | |
718e3744 | 1339 | else |
1340 | { | |
1341 | /* 3. Otherwise, the local system closes newly created | |
1342 | BGP connection (the one associated with the newly | |
1343 | received OPEN message), and continues to use the | |
1344 | existing one (the one that is already in the | |
1345 | OpenConfirm state). */ | |
1ff9a340 DS |
1346 | if (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
1347 | { | |
1348 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, | |
1349 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
1350 | return 1; | |
1351 | } | |
1352 | else | |
1353 | { | |
1354 | bgp_notify_send (new, BGP_NOTIFY_CEASE, | |
1355 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
1356 | return -1; | |
1357 | } | |
718e3744 | 1358 | } |
eb821189 | 1359 | } |
1360 | } | |
718e3744 | 1361 | return 0; |
1362 | } | |
1363 | ||
94f2b392 | 1364 | static int |
718e3744 | 1365 | bgp_open_receive (struct peer *peer, bgp_size_t size) |
1366 | { | |
1367 | int ret; | |
1368 | u_char version; | |
1369 | u_char optlen; | |
1370 | u_int16_t holdtime; | |
1371 | u_int16_t send_holdtime; | |
1372 | as_t remote_as; | |
0b2aa3a0 | 1373 | as_t as4 = 0; |
1ff9a340 | 1374 | struct peer *active_peer = NULL; |
718e3744 | 1375 | struct in_addr remote_id; |
3b381c32 | 1376 | int mp_capability; |
5228ad27 | 1377 | u_int8_t notify_data_remote_as[2]; |
1378 | u_int8_t notify_data_remote_id[4]; | |
718e3744 | 1379 | |
718e3744 | 1380 | /* Parse open packet. */ |
1381 | version = stream_getc (peer->ibuf); | |
1382 | memcpy (notify_data_remote_as, stream_pnt (peer->ibuf), 2); | |
1383 | remote_as = stream_getw (peer->ibuf); | |
1384 | holdtime = stream_getw (peer->ibuf); | |
1385 | memcpy (notify_data_remote_id, stream_pnt (peer->ibuf), 4); | |
1386 | remote_id.s_addr = stream_get_ipv4 (peer->ibuf); | |
1387 | ||
1388 | /* Receive OPEN message log */ | |
1389 | if (BGP_DEBUG (normal, NORMAL)) | |
aea339f7 | 1390 | zlog_debug ("%s rcv OPEN, version %d, remote-as (in open) %u," |
0b2aa3a0 PJ |
1391 | " holdtime %d, id %s", |
1392 | peer->host, version, remote_as, holdtime, | |
1393 | inet_ntoa (remote_id)); | |
1394 | ||
1395 | /* BEGIN to read the capability here, but dont do it yet */ | |
3b381c32 | 1396 | mp_capability = 0; |
0b2aa3a0 PJ |
1397 | optlen = stream_getc (peer->ibuf); |
1398 | ||
1399 | if (optlen != 0) | |
1400 | { | |
1401 | /* We need the as4 capability value *right now* because | |
1402 | * if it is there, we have not got the remote_as yet, and without | |
1403 | * that we do not know which peer is connecting to us now. | |
1404 | */ | |
1405 | as4 = peek_for_as4_capability (peer, optlen); | |
1406 | } | |
1407 | ||
1408 | /* Just in case we have a silly peer who sends AS4 capability set to 0 */ | |
1409 | if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) && !as4) | |
1410 | { | |
1411 | zlog_err ("%s bad OPEN, got AS4 capability, but AS4 set to 0", | |
1412 | peer->host); | |
1413 | bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR, | |
1414 | BGP_NOTIFY_OPEN_BAD_PEER_AS); | |
1415 | return -1; | |
1416 | } | |
1417 | ||
1418 | if (remote_as == BGP_AS_TRANS) | |
1419 | { | |
1420 | /* Take the AS4 from the capability. We must have received the | |
1421 | * capability now! Otherwise we have a asn16 peer who uses | |
1422 | * BGP_AS_TRANS, for some unknown reason. | |
1423 | */ | |
1424 | if (as4 == BGP_AS_TRANS) | |
1425 | { | |
1426 | zlog_err ("%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed", | |
1427 | peer->host); | |
1ff9a340 DS |
1428 | bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR, |
1429 | BGP_NOTIFY_OPEN_BAD_PEER_AS); | |
0b2aa3a0 PJ |
1430 | return -1; |
1431 | } | |
1432 | ||
1433 | if (!as4 && BGP_DEBUG (as4, AS4)) | |
1434 | zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but no AS4." | |
1435 | " Odd, but proceeding.", peer->host); | |
1436 | else if (as4 < BGP_AS_MAX && BGP_DEBUG (as4, AS4)) | |
0df7c91f | 1437 | zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits " |
0b2aa3a0 PJ |
1438 | "in 2-bytes, very odd peer.", peer->host, as4); |
1439 | if (as4) | |
1440 | remote_as = as4; | |
1441 | } | |
1442 | else | |
1443 | { | |
1444 | /* We may have a partner with AS4 who has an asno < BGP_AS_MAX */ | |
1445 | /* If we have got the capability, peer->as4cap must match remote_as */ | |
1446 | if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) | |
1447 | && as4 != remote_as) | |
1448 | { | |
1449 | /* raise error, log this, close session */ | |
1450 | zlog_err ("%s bad OPEN, got AS4 capability, but remote_as %u" | |
1451 | " mismatch with 16bit 'myasn' %u in open", | |
1452 | peer->host, as4, remote_as); | |
1453 | bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR, | |
1454 | BGP_NOTIFY_OPEN_BAD_PEER_AS); | |
1455 | return -1; | |
1456 | } | |
1457 | } | |
1458 | ||
718e3744 | 1459 | /* remote router-id check. */ |
1460 | if (remote_id.s_addr == 0 | |
733cd9e5 | 1461 | || IPV4_CLASS_DE (ntohl (remote_id.s_addr)) |
718e3744 | 1462 | || ntohl (peer->local_id.s_addr) == ntohl (remote_id.s_addr)) |
1463 | { | |
1464 | if (BGP_DEBUG (normal, NORMAL)) | |
6b51474d | 1465 | zlog_debug ("%s bad OPEN, wrong router identifier %s", |
718e3744 | 1466 | peer->host, inet_ntoa (remote_id)); |
1467 | bgp_notify_send_with_data (peer, | |
1468 | BGP_NOTIFY_OPEN_ERR, | |
1469 | BGP_NOTIFY_OPEN_BAD_BGP_IDENT, | |
1470 | notify_data_remote_id, 4); | |
1471 | return -1; | |
1472 | } | |
1473 | ||
1474 | /* Set remote router-id */ | |
1475 | peer->remote_id = remote_id; | |
1476 | ||
1477 | /* Peer BGP version check. */ | |
1478 | if (version != BGP_VERSION_4) | |
1479 | { | |
a689e6a9 LR |
1480 | u_int16_t maxver = htons(BGP_VERSION_4); |
1481 | /* XXX this reply may not be correct if version < 4 XXX */ | |
718e3744 | 1482 | if (BGP_DEBUG (normal, NORMAL)) |
6b51474d | 1483 | zlog_debug ("%s bad protocol version, remote requested %d, local request %d", |
718e3744 | 1484 | peer->host, version, BGP_VERSION_4); |
a689e6a9 | 1485 | /* Data must be in network byte order here */ |
718e3744 | 1486 | bgp_notify_send_with_data (peer, |
1487 | BGP_NOTIFY_OPEN_ERR, | |
1488 | BGP_NOTIFY_OPEN_UNSUP_VERSION, | |
a689e6a9 | 1489 | (u_int8_t *) &maxver, 2); |
718e3744 | 1490 | return -1; |
1491 | } | |
1492 | ||
1493 | /* Check neighbor as number. */ | |
1494 | if (remote_as != peer->as) | |
1495 | { | |
1496 | if (BGP_DEBUG (normal, NORMAL)) | |
aea339f7 | 1497 | zlog_debug ("%s bad OPEN, remote AS is %u, expected %u", |
718e3744 | 1498 | peer->host, remote_as, peer->as); |
1499 | bgp_notify_send_with_data (peer, | |
1500 | BGP_NOTIFY_OPEN_ERR, | |
1501 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1502 | notify_data_remote_as, 2); | |
1503 | return -1; | |
1504 | } | |
1505 | ||
1506 | /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST | |
1507 | calculate the value of the Hold Timer by using the smaller of its | |
1508 | configured Hold Time and the Hold Time received in the OPEN message. | |
1509 | The Hold Time MUST be either zero or at least three seconds. An | |
1510 | implementation may reject connections on the basis of the Hold Time. */ | |
1511 | ||
1512 | if (holdtime < 3 && holdtime != 0) | |
1513 | { | |
1514 | bgp_notify_send (peer, | |
1515 | BGP_NOTIFY_OPEN_ERR, | |
1516 | BGP_NOTIFY_OPEN_UNACEP_HOLDTIME); | |
1517 | return -1; | |
1518 | } | |
1519 | ||
1520 | /* From the rfc: A reasonable maximum time between KEEPALIVE messages | |
1521 | would be one third of the Hold Time interval. KEEPALIVE messages | |
1522 | MUST NOT be sent more frequently than one per second. An | |
1523 | implementation MAY adjust the rate at which it sends KEEPALIVE | |
1524 | messages as a function of the Hold Time interval. */ | |
1525 | ||
1526 | if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER)) | |
1527 | send_holdtime = peer->holdtime; | |
1528 | else | |
1529 | send_holdtime = peer->bgp->default_holdtime; | |
1530 | ||
1531 | if (holdtime < send_holdtime) | |
1532 | peer->v_holdtime = holdtime; | |
1533 | else | |
1534 | peer->v_holdtime = send_holdtime; | |
1535 | ||
1536 | peer->v_keepalive = peer->v_holdtime / 3; | |
1537 | ||
1538 | /* Open option part parse. */ | |
718e3744 | 1539 | if (optlen != 0) |
1540 | { | |
3b381c32 | 1541 | if ((ret = bgp_open_option_parse (peer, optlen, &mp_capability)) < 0) |
5861739f PJ |
1542 | { |
1543 | bgp_notify_send (peer, | |
1544 | BGP_NOTIFY_OPEN_ERR, | |
1545 | BGP_NOTIFY_OPEN_UNACEP_HOLDTIME); | |
1546 | return ret; | |
1547 | } | |
718e3744 | 1548 | } |
1549 | else | |
1550 | { | |
1551 | if (BGP_DEBUG (normal, NORMAL)) | |
6b51474d | 1552 | zlog_debug ("%s rcvd OPEN w/ OPTION parameter len: 0", |
718e3744 | 1553 | peer->host); |
1554 | } | |
1555 | ||
3b381c32 AS |
1556 | /* |
1557 | * Assume that the peer supports the locally configured set of | |
1558 | * AFI/SAFIs if the peer did not send us any Mulitiprotocol | |
1559 | * capabilities, or if 'override-capability' is configured. | |
1560 | */ | |
1561 | if (! mp_capability || | |
1562 | CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) | |
718e3744 | 1563 | { |
1564 | peer->afc_nego[AFI_IP][SAFI_UNICAST] = peer->afc[AFI_IP][SAFI_UNICAST]; | |
1565 | peer->afc_nego[AFI_IP][SAFI_MULTICAST] = peer->afc[AFI_IP][SAFI_MULTICAST]; | |
1566 | peer->afc_nego[AFI_IP6][SAFI_UNICAST] = peer->afc[AFI_IP6][SAFI_UNICAST]; | |
1567 | peer->afc_nego[AFI_IP6][SAFI_MULTICAST] = peer->afc[AFI_IP6][SAFI_MULTICAST]; | |
1568 | } | |
1569 | ||
1ff9a340 DS |
1570 | /* When collision is detected and this peer is closed. Retrun |
1571 | immidiately. */ | |
1572 | ret = bgp_collision_detect (peer, remote_id); | |
1573 | if (ret < 0) | |
1574 | return ret; | |
1575 | ||
718e3744 | 1576 | /* Get sockname. */ |
1ff9a340 DS |
1577 | if ((ret = bgp_getsockname (peer)) < 0) |
1578 | { | |
1579 | zlog_err("%s: bgp_getsockname() failed for peer: %s", __FUNCTION__, | |
1580 | peer->host); | |
1581 | return (ret); | |
1582 | } | |
718e3744 | 1583 | |
1ff9a340 DS |
1584 | if ((ret = bgp_event_update(peer, Receive_OPEN_message)) < 0) |
1585 | { | |
1586 | zlog_err("%s: BGP event update failed for peer: %s", __FUNCTION__, | |
1587 | peer->host); | |
1588 | /* DD: bgp send notify and reset state */ | |
1589 | return (ret); | |
1590 | } | |
718e3744 | 1591 | |
1592 | peer->packet_size = 0; | |
1593 | if (peer->ibuf) | |
1594 | stream_reset (peer->ibuf); | |
1595 | ||
1596 | return 0; | |
1597 | } | |
1598 | ||
f188f2c4 DS |
1599 | /* Called when there is a change in the EOR(implicit or explicit) status of a peer. |
1600 | Ends the update-delay if all expected peers are done with EORs. */ | |
1601 | void | |
1602 | bgp_check_update_delay(struct bgp *bgp) | |
1603 | { | |
1604 | struct listnode *node, *nnode; | |
1605 | struct peer *peer; | |
1606 | ||
1607 | if (BGP_DEBUG (normal, NORMAL)) | |
1608 | zlog_debug ("Checking update delay, T: %d R: %d I:%d E: %d", bgp->established, | |
1609 | bgp->restarted_peers, bgp->implicit_eors, bgp->explicit_eors); | |
1610 | ||
1611 | if (bgp->established <= | |
1612 | bgp->restarted_peers + bgp->implicit_eors + bgp->explicit_eors) | |
1613 | { | |
1614 | /* This is an extra sanity check to make sure we wait for all the | |
1615 | eligible configured peers. This check is performed if establish wait | |
1616 | timer is on, or establish wait option is not given with the | |
1617 | update-delay command */ | |
1618 | if (bgp->t_establish_wait || | |
1619 | (bgp->v_establish_wait == bgp->v_update_delay)) | |
1620 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) | |
1621 | { | |
1ff9a340 DS |
1622 | if (CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE) |
1623 | && !CHECK_FLAG (peer->flags, PEER_FLAG_SHUTDOWN) | |
f188f2c4 DS |
1624 | && !peer->update_delay_over) |
1625 | { | |
1626 | if (BGP_DEBUG (normal, NORMAL)) | |
1627 | zlog_debug (" Peer %s pending, continuing read-only mode", | |
1628 | peer->host); | |
1629 | return; | |
1630 | } | |
1631 | } | |
1632 | ||
1633 | zlog_info ("Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d", | |
1634 | bgp->restarted_peers, bgp->implicit_eors, bgp->explicit_eors); | |
1635 | bgp_update_delay_end(bgp); | |
1636 | } | |
1637 | } | |
1638 | ||
1639 | /* Called if peer is known to have restarted. The restart-state bit in | |
1640 | Graceful-Restart capability is used for that */ | |
1641 | void | |
1642 | bgp_update_restarted_peers (struct peer *peer) | |
1643 | { | |
1644 | if (!bgp_update_delay_active(peer->bgp)) return; /* BGP update delay has ended */ | |
1645 | if (peer->update_delay_over) return; /* This peer has already been considered */ | |
1646 | ||
1647 | if (BGP_DEBUG (normal, NORMAL)) | |
1648 | zlog_debug ("Peer %s: Checking restarted", peer->host); | |
1649 | ||
1650 | if (peer->status == Established) | |
1651 | { | |
1652 | peer->update_delay_over = 1; | |
1653 | peer->bgp->restarted_peers++; | |
1654 | bgp_check_update_delay(peer->bgp); | |
1655 | } | |
1656 | } | |
1657 | ||
1658 | /* Called as peer receives a keep-alive. Determines if this occurence can be | |
1659 | taken as an implicit EOR for this peer. | |
1660 | NOTE: The very first keep-alive after the Established state of a peer is | |
1661 | considered implicit EOR for the update-delay purposes */ | |
1662 | void | |
1663 | bgp_update_implicit_eors (struct peer *peer) | |
1664 | { | |
1665 | if (!bgp_update_delay_active(peer->bgp)) return; /* BGP update delay has ended */ | |
1666 | if (peer->update_delay_over) return; /* This peer has already been considered */ | |
1667 | ||
1668 | if (BGP_DEBUG (normal, NORMAL)) | |
1669 | zlog_debug ("Peer %s: Checking implicit EORs", peer->host); | |
1670 | ||
1671 | if (peer->status == Established) | |
1672 | { | |
1673 | peer->update_delay_over = 1; | |
1674 | peer->bgp->implicit_eors++; | |
1675 | bgp_check_update_delay(peer->bgp); | |
1676 | } | |
1677 | } | |
1678 | ||
1679 | /* Should be called only when there is a change in the EOR_RECEIVED status | |
1680 | for any afi/safi on a peer */ | |
1681 | static void | |
1682 | bgp_update_explicit_eors (struct peer *peer) | |
1683 | { | |
1684 | afi_t afi; | |
1685 | safi_t safi; | |
1686 | ||
1687 | if (!bgp_update_delay_active(peer->bgp)) return; /* BGP update delay has ended */ | |
1688 | if (peer->update_delay_over) return; /* This peer has already been considered */ | |
1689 | ||
1690 | if (BGP_DEBUG (normal, NORMAL)) | |
1691 | zlog_debug ("Peer %s: Checking explicit EORs", peer->host); | |
1692 | ||
1693 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
1694 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) | |
1695 | { | |
1696 | if (peer->afc_nego[afi][safi] && | |
1697 | !CHECK_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_EOR_RECEIVED)) | |
1698 | { | |
1699 | if (BGP_DEBUG (normal, NORMAL)) | |
1700 | zlog_debug (" afi %d safi %d didnt receive EOR", afi, safi); | |
1701 | return; | |
1702 | } | |
1703 | } | |
1704 | ||
1705 | peer->update_delay_over = 1; | |
1706 | peer->bgp->explicit_eors++; | |
1707 | bgp_check_update_delay(peer->bgp); | |
1708 | } | |
1709 | ||
718e3744 | 1710 | /* Parse BGP Update packet and make attribute object. */ |
94f2b392 | 1711 | static int |
718e3744 | 1712 | bgp_update_receive (struct peer *peer, bgp_size_t size) |
1713 | { | |
1714 | int ret; | |
1715 | u_char *end; | |
1716 | struct stream *s; | |
1717 | struct attr attr; | |
489d005a | 1718 | struct attr_extra extra; |
718e3744 | 1719 | bgp_size_t attribute_len; |
1720 | bgp_size_t update_len; | |
1721 | bgp_size_t withdraw_len; | |
1722 | struct bgp_nlri update; | |
1723 | struct bgp_nlri withdraw; | |
1724 | struct bgp_nlri mp_update; | |
1725 | struct bgp_nlri mp_withdraw; | |
d889623f | 1726 | int num_pfx_adv, num_pfx_wd; |
718e3744 | 1727 | |
1728 | /* Status must be Established. */ | |
1729 | if (peer->status != Established) | |
1730 | { | |
1731 | zlog_err ("%s [FSM] Update packet received under status %s", | |
1732 | peer->host, LOOKUP (bgp_status_msg, peer->status)); | |
1733 | bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0); | |
1734 | return -1; | |
1735 | } | |
1736 | ||
1737 | /* Set initial values. */ | |
1738 | memset (&attr, 0, sizeof (struct attr)); | |
489d005a | 1739 | memset (&extra, 0, sizeof (struct attr_extra)); |
718e3744 | 1740 | memset (&update, 0, sizeof (struct bgp_nlri)); |
1741 | memset (&withdraw, 0, sizeof (struct bgp_nlri)); | |
1742 | memset (&mp_update, 0, sizeof (struct bgp_nlri)); | |
1743 | memset (&mp_withdraw, 0, sizeof (struct bgp_nlri)); | |
489d005a | 1744 | attr.extra = &extra; |
d889623f | 1745 | num_pfx_adv = num_pfx_wd = 0; |
718e3744 | 1746 | |
1747 | s = peer->ibuf; | |
1748 | end = stream_pnt (s) + size; | |
1749 | ||
1750 | /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute | |
1751 | Length is too large (i.e., if Unfeasible Routes Length + Total | |
1752 | Attribute Length + 23 exceeds the message Length), then the Error | |
1753 | Subcode is set to Malformed Attribute List. */ | |
1754 | if (stream_pnt (s) + 2 > end) | |
1755 | { | |
1756 | zlog_err ("%s [Error] Update packet error" | |
1757 | " (packet length is short for unfeasible length)", | |
1758 | peer->host); | |
1759 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, | |
1760 | BGP_NOTIFY_UPDATE_MAL_ATTR); | |
1761 | return -1; | |
1762 | } | |
1763 | ||
1764 | /* Unfeasible Route Length. */ | |
1765 | withdraw_len = stream_getw (s); | |
1766 | ||
1767 | /* Unfeasible Route Length check. */ | |
1768 | if (stream_pnt (s) + withdraw_len > end) | |
1769 | { | |
1770 | zlog_err ("%s [Error] Update packet error" | |
1771 | " (packet unfeasible length overflow %d)", | |
1772 | peer->host, withdraw_len); | |
1773 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, | |
1774 | BGP_NOTIFY_UPDATE_MAL_ATTR); | |
1775 | return -1; | |
1776 | } | |
1777 | ||
1778 | /* Unfeasible Route packet format check. */ | |
1779 | if (withdraw_len > 0) | |
1780 | { | |
d889623f DS |
1781 | ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), withdraw_len, |
1782 | &num_pfx_wd); | |
718e3744 | 1783 | if (ret < 0) |
1784 | return -1; | |
1785 | ||
1786 | if (BGP_DEBUG (packet, PACKET_RECV)) | |
6b51474d | 1787 | zlog_debug ("%s [Update:RECV] Unfeasible NLRI received", peer->host); |
718e3744 | 1788 | |
1789 | withdraw.afi = AFI_IP; | |
1790 | withdraw.safi = SAFI_UNICAST; | |
1791 | withdraw.nlri = stream_pnt (s); | |
1792 | withdraw.length = withdraw_len; | |
9985f83c | 1793 | stream_forward_getp (s, withdraw_len); |
718e3744 | 1794 | } |
1795 | ||
1796 | /* Attribute total length check. */ | |
1797 | if (stream_pnt (s) + 2 > end) | |
1798 | { | |
1799 | zlog_warn ("%s [Error] Packet Error" | |
1800 | " (update packet is short for attribute length)", | |
1801 | peer->host); | |
1802 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, | |
1803 | BGP_NOTIFY_UPDATE_MAL_ATTR); | |
1804 | return -1; | |
1805 | } | |
1806 | ||
1807 | /* Fetch attribute total length. */ | |
1808 | attribute_len = stream_getw (s); | |
1809 | ||
1810 | /* Attribute length check. */ | |
1811 | if (stream_pnt (s) + attribute_len > end) | |
1812 | { | |
1813 | zlog_warn ("%s [Error] Packet Error" | |
1814 | " (update packet attribute length overflow %d)", | |
1815 | peer->host, attribute_len); | |
1816 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, | |
1817 | BGP_NOTIFY_UPDATE_MAL_ATTR); | |
1818 | return -1; | |
1819 | } | |
b881c707 PJ |
1820 | |
1821 | /* Certain attribute parsing errors should not be considered bad enough | |
1822 | * to reset the session for, most particularly any partial/optional | |
1823 | * attributes that have 'tunneled' over speakers that don't understand | |
1824 | * them. Instead we withdraw only the prefix concerned. | |
1825 | * | |
1826 | * Complicates the flow a little though.. | |
1827 | */ | |
1828 | bgp_attr_parse_ret_t attr_parse_ret = BGP_ATTR_PARSE_PROCEED; | |
1829 | /* This define morphs the update case into a withdraw when lower levels | |
1830 | * have signalled an error condition where this is best. | |
1831 | */ | |
1832 | #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL) | |
718e3744 | 1833 | |
1834 | /* Parse attribute when it exists. */ | |
1835 | if (attribute_len) | |
1836 | { | |
b881c707 | 1837 | attr_parse_ret = bgp_attr_parse (peer, &attr, attribute_len, |
718e3744 | 1838 | &mp_update, &mp_withdraw); |
b881c707 | 1839 | if (attr_parse_ret == BGP_ATTR_PARSE_ERROR) |
f80f838b DL |
1840 | { |
1841 | bgp_attr_unintern_sub (&attr); | |
1842 | return -1; | |
1843 | } | |
718e3744 | 1844 | } |
b881c707 | 1845 | |
718e3744 | 1846 | /* Logging the attribute. */ |
b881c707 PJ |
1847 | if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW |
1848 | || BGP_DEBUG (update, UPDATE_IN)) | |
718e3744 | 1849 | { |
14542f3e JBD |
1850 | char attrstr[BUFSIZ]; |
1851 | attrstr[0] = '\0'; | |
1852 | ||
e01f9cbb | 1853 | ret= bgp_dump_attr (peer, &attr, attrstr, BUFSIZ); |
b881c707 PJ |
1854 | int lvl = (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW) |
1855 | ? LOG_ERR : LOG_DEBUG; | |
1856 | ||
1857 | if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW) | |
1858 | zlog (peer->log, LOG_ERR, | |
1859 | "%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.", | |
1860 | peer->host); | |
e01f9cbb | 1861 | |
1862 | if (ret) | |
b881c707 | 1863 | zlog (peer->log, lvl, "%s rcvd UPDATE w/ attr: %s", |
e01f9cbb | 1864 | peer->host, attrstr); |
718e3744 | 1865 | } |
b881c707 | 1866 | |
718e3744 | 1867 | /* Network Layer Reachability Information. */ |
1868 | update_len = end - stream_pnt (s); | |
1869 | ||
1870 | if (update_len) | |
1871 | { | |
1872 | /* Check NLRI packet format and prefix length. */ | |
d889623f DS |
1873 | ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), update_len, |
1874 | &num_pfx_adv); | |
718e3744 | 1875 | if (ret < 0) |
b881c707 PJ |
1876 | { |
1877 | bgp_attr_unintern_sub (&attr); | |
b881c707 PJ |
1878 | return -1; |
1879 | } | |
718e3744 | 1880 | |
1881 | /* Set NLRI portion to structure. */ | |
1882 | update.afi = AFI_IP; | |
1883 | update.safi = SAFI_UNICAST; | |
1884 | update.nlri = stream_pnt (s); | |
1885 | update.length = update_len; | |
9985f83c | 1886 | stream_forward_getp (s, update_len); |
718e3744 | 1887 | } |
1888 | ||
d889623f DS |
1889 | if (BGP_DEBUG (update, UPDATE_IN)) |
1890 | zlog(peer->log, LOG_DEBUG, | |
1891 | "%s rcvd UPDATE wlen %d wpfx %d attrlen %d alen %d apfx %d", | |
1892 | peer->host, withdraw_len, num_pfx_wd, attribute_len, | |
1893 | update_len, num_pfx_adv); | |
1894 | ||
718e3744 | 1895 | /* NLRI is processed only when the peer is configured specific |
1896 | Address Family and Subsequent Address Family. */ | |
1897 | if (peer->afc[AFI_IP][SAFI_UNICAST]) | |
1898 | { | |
1899 | if (withdraw.length) | |
1900 | bgp_nlri_parse (peer, NULL, &withdraw); | |
1901 | ||
1902 | if (update.length) | |
1903 | { | |
1904 | /* We check well-known attribute only for IPv4 unicast | |
1905 | update. */ | |
1906 | ret = bgp_attr_check (peer, &attr); | |
1907 | if (ret < 0) | |
b881c707 PJ |
1908 | { |
1909 | bgp_attr_unintern_sub (&attr); | |
b881c707 PJ |
1910 | return -1; |
1911 | } | |
718e3744 | 1912 | |
b881c707 | 1913 | bgp_nlri_parse (peer, NLRI_ATTR_ARG, &update); |
718e3744 | 1914 | } |
e01f9cbb | 1915 | |
f418446b | 1916 | if (mp_update.length |
1917 | && mp_update.afi == AFI_IP | |
1918 | && mp_update.safi == SAFI_UNICAST) | |
b881c707 | 1919 | bgp_nlri_parse (peer, NLRI_ATTR_ARG, &mp_update); |
f418446b | 1920 | |
1921 | if (mp_withdraw.length | |
1922 | && mp_withdraw.afi == AFI_IP | |
1923 | && mp_withdraw.safi == SAFI_UNICAST) | |
1924 | bgp_nlri_parse (peer, NULL, &mp_withdraw); | |
1925 | ||
e01f9cbb | 1926 | if (! attribute_len && ! withdraw_len) |
1927 | { | |
1928 | /* End-of-RIB received */ | |
f188f2c4 DS |
1929 | if (!CHECK_FLAG(peer->af_sflags[AFI_IP][SAFI_UNICAST], |
1930 | PEER_STATUS_EOR_RECEIVED)) | |
1931 | { | |
1932 | SET_FLAG (peer->af_sflags[AFI_IP][SAFI_UNICAST], | |
1933 | PEER_STATUS_EOR_RECEIVED); | |
1934 | bgp_update_explicit_eors(peer); | |
1935 | } | |
e01f9cbb | 1936 | |
93406d87 | 1937 | /* NSF delete stale route */ |
1938 | if (peer->nsf[AFI_IP][SAFI_UNICAST]) | |
1939 | bgp_clear_stale_route (peer, AFI_IP, SAFI_UNICAST); | |
1940 | ||
1941 | if (BGP_DEBUG (normal, NORMAL)) | |
6b51474d | 1942 | zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv4 Unicast from %s", |
e01f9cbb | 1943 | peer->host); |
1944 | } | |
718e3744 | 1945 | } |
1946 | if (peer->afc[AFI_IP][SAFI_MULTICAST]) | |
1947 | { | |
1948 | if (mp_update.length | |
1949 | && mp_update.afi == AFI_IP | |
1950 | && mp_update.safi == SAFI_MULTICAST) | |
b881c707 | 1951 | bgp_nlri_parse (peer, NLRI_ATTR_ARG, &mp_update); |
718e3744 | 1952 | |
1953 | if (mp_withdraw.length | |
1954 | && mp_withdraw.afi == AFI_IP | |
1955 | && mp_withdraw.safi == SAFI_MULTICAST) | |
1956 | bgp_nlri_parse (peer, NULL, &mp_withdraw); | |
e01f9cbb | 1957 | |
93406d87 | 1958 | if (! withdraw_len |
e01f9cbb | 1959 | && mp_withdraw.afi == AFI_IP |
1960 | && mp_withdraw.safi == SAFI_MULTICAST | |
1961 | && mp_withdraw.length == 0) | |
1962 | { | |
1963 | /* End-of-RIB received */ | |
f188f2c4 DS |
1964 | if (!CHECK_FLAG (peer->af_sflags[AFI_IP][SAFI_MULTICAST], |
1965 | PEER_STATUS_EOR_RECEIVED)) | |
1966 | { | |
1967 | SET_FLAG (peer->af_sflags[AFI_IP][SAFI_MULTICAST], | |
1968 | PEER_STATUS_EOR_RECEIVED); | |
1969 | bgp_update_explicit_eors(peer); | |
1970 | } | |
e01f9cbb | 1971 | |
93406d87 | 1972 | /* NSF delete stale route */ |
1973 | if (peer->nsf[AFI_IP][SAFI_MULTICAST]) | |
1974 | bgp_clear_stale_route (peer, AFI_IP, SAFI_MULTICAST); | |
1975 | ||
1976 | if (BGP_DEBUG (normal, NORMAL)) | |
6b51474d | 1977 | zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv4 Multicast from %s", |
e01f9cbb | 1978 | peer->host); |
1979 | } | |
718e3744 | 1980 | } |
1981 | if (peer->afc[AFI_IP6][SAFI_UNICAST]) | |
1982 | { | |
1983 | if (mp_update.length | |
1984 | && mp_update.afi == AFI_IP6 | |
1985 | && mp_update.safi == SAFI_UNICAST) | |
b881c707 | 1986 | bgp_nlri_parse (peer, NLRI_ATTR_ARG, &mp_update); |
718e3744 | 1987 | |
1988 | if (mp_withdraw.length | |
1989 | && mp_withdraw.afi == AFI_IP6 | |
1990 | && mp_withdraw.safi == SAFI_UNICAST) | |
1991 | bgp_nlri_parse (peer, NULL, &mp_withdraw); | |
e01f9cbb | 1992 | |
93406d87 | 1993 | if (! withdraw_len |
e01f9cbb | 1994 | && mp_withdraw.afi == AFI_IP6 |
1995 | && mp_withdraw.safi == SAFI_UNICAST | |
1996 | && mp_withdraw.length == 0) | |
1997 | { | |
1998 | /* End-of-RIB received */ | |
f188f2c4 DS |
1999 | if (!CHECK_FLAG (peer->af_sflags[AFI_IP6][SAFI_UNICAST], |
2000 | PEER_STATUS_EOR_RECEIVED)) | |
2001 | { | |
2002 | SET_FLAG (peer->af_sflags[AFI_IP6][SAFI_UNICAST], PEER_STATUS_EOR_RECEIVED); | |
2003 | bgp_update_explicit_eors(peer); | |
2004 | } | |
e01f9cbb | 2005 | |
93406d87 | 2006 | /* NSF delete stale route */ |
2007 | if (peer->nsf[AFI_IP6][SAFI_UNICAST]) | |
2008 | bgp_clear_stale_route (peer, AFI_IP6, SAFI_UNICAST); | |
2009 | ||
2010 | if (BGP_DEBUG (normal, NORMAL)) | |
6b51474d | 2011 | zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv6 Unicast from %s", |
e01f9cbb | 2012 | peer->host); |
2013 | } | |
718e3744 | 2014 | } |
2015 | if (peer->afc[AFI_IP6][SAFI_MULTICAST]) | |
2016 | { | |
2017 | if (mp_update.length | |
2018 | && mp_update.afi == AFI_IP6 | |
2019 | && mp_update.safi == SAFI_MULTICAST) | |
b881c707 | 2020 | bgp_nlri_parse (peer, NLRI_ATTR_ARG, &mp_update); |
718e3744 | 2021 | |
2022 | if (mp_withdraw.length | |
2023 | && mp_withdraw.afi == AFI_IP6 | |
2024 | && mp_withdraw.safi == SAFI_MULTICAST) | |
2025 | bgp_nlri_parse (peer, NULL, &mp_withdraw); | |
e01f9cbb | 2026 | |
93406d87 | 2027 | if (! withdraw_len |
e01f9cbb | 2028 | && mp_withdraw.afi == AFI_IP6 |
2029 | && mp_withdraw.safi == SAFI_MULTICAST | |
2030 | && mp_withdraw.length == 0) | |
2031 | { | |
2032 | /* End-of-RIB received */ | |
f188f2c4 DS |
2033 | if (!CHECK_FLAG (peer->af_sflags[AFI_IP6][SAFI_MULTICAST], |
2034 | PEER_STATUS_EOR_RECEIVED)) | |
2035 | { | |
2036 | SET_FLAG (peer->af_sflags[AFI_IP6][SAFI_MULTICAST], PEER_STATUS_EOR_RECEIVED); | |
2037 | bgp_update_explicit_eors(peer); | |
2038 | } | |
2039 | ||
e01f9cbb | 2040 | |
93406d87 | 2041 | /* NSF delete stale route */ |
2042 | if (peer->nsf[AFI_IP6][SAFI_MULTICAST]) | |
2043 | bgp_clear_stale_route (peer, AFI_IP6, SAFI_MULTICAST); | |
2044 | ||
e01f9cbb | 2045 | if (BGP_DEBUG (update, UPDATE_IN)) |
6b51474d | 2046 | zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv6 Multicast from %s", |
e01f9cbb | 2047 | peer->host); |
2048 | } | |
718e3744 | 2049 | } |
2050 | if (peer->afc[AFI_IP][SAFI_MPLS_VPN]) | |
2051 | { | |
2052 | if (mp_update.length | |
2053 | && mp_update.afi == AFI_IP | |
42e6d745 | 2054 | && mp_update.safi == SAFI_MPLS_LABELED_VPN) |
b881c707 | 2055 | bgp_nlri_parse_vpnv4 (peer, NLRI_ATTR_ARG, &mp_update); |
718e3744 | 2056 | |
2057 | if (mp_withdraw.length | |
2058 | && mp_withdraw.afi == AFI_IP | |
42e6d745 | 2059 | && mp_withdraw.safi == SAFI_MPLS_LABELED_VPN) |
718e3744 | 2060 | bgp_nlri_parse_vpnv4 (peer, NULL, &mp_withdraw); |
e01f9cbb | 2061 | |
93406d87 | 2062 | if (! withdraw_len |
e01f9cbb | 2063 | && mp_withdraw.afi == AFI_IP |
42e6d745 | 2064 | && mp_withdraw.safi == SAFI_MPLS_LABELED_VPN |
e01f9cbb | 2065 | && mp_withdraw.length == 0) |
2066 | { | |
2067 | /* End-of-RIB received */ | |
f188f2c4 DS |
2068 | if (!CHECK_FLAG (peer->af_sflags[AFI_IP][SAFI_MPLS_VPN], |
2069 | PEER_STATUS_EOR_RECEIVED)) | |
2070 | { | |
2071 | SET_FLAG (peer->af_sflags[AFI_IP][SAFI_MPLS_VPN], PEER_STATUS_EOR_RECEIVED); | |
2072 | bgp_update_explicit_eors(peer); | |
2073 | } | |
e01f9cbb | 2074 | |
2075 | if (BGP_DEBUG (update, UPDATE_IN)) | |
6b51474d | 2076 | zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for VPNv4 Unicast from %s", |
e01f9cbb | 2077 | peer->host); |
2078 | } | |
718e3744 | 2079 | } |
2080 | ||
2081 | /* Everything is done. We unintern temporary structures which | |
2082 | interned in bgp_attr_parse(). */ | |
b881c707 | 2083 | bgp_attr_unintern_sub (&attr); |
489d005a | 2084 | |
718e3744 | 2085 | /* If peering is stopped due to some reason, do not generate BGP |
2086 | event. */ | |
2087 | if (peer->status != Established) | |
2088 | return 0; | |
2089 | ||
2090 | /* Increment packet counter. */ | |
2091 | peer->update_in++; | |
65957886 | 2092 | peer->update_time = bgp_clock (); |
718e3744 | 2093 | |
e2c38e6c | 2094 | /* Rearm holdtime timer */ |
6a4677b7 | 2095 | BGP_TIMER_OFF (peer->t_holdtime); |
e2c38e6c | 2096 | bgp_timer_set (peer); |
718e3744 | 2097 | |
2098 | return 0; | |
2099 | } | |
2100 | ||
2101 | /* Notify message treatment function. */ | |
94f2b392 | 2102 | static void |
718e3744 | 2103 | bgp_notify_receive (struct peer *peer, bgp_size_t size) |
2104 | { | |
2105 | struct bgp_notify bgp_notify; | |
2106 | ||
2107 | if (peer->notify.data) | |
2108 | { | |
2109 | XFREE (MTYPE_TMP, peer->notify.data); | |
2110 | peer->notify.data = NULL; | |
2111 | peer->notify.length = 0; | |
2112 | } | |
2113 | ||
2114 | bgp_notify.code = stream_getc (peer->ibuf); | |
2115 | bgp_notify.subcode = stream_getc (peer->ibuf); | |
2116 | bgp_notify.length = size - 2; | |
2117 | bgp_notify.data = NULL; | |
2118 | ||
2119 | /* Preserv notify code and sub code. */ | |
2120 | peer->notify.code = bgp_notify.code; | |
2121 | peer->notify.subcode = bgp_notify.subcode; | |
2122 | /* For further diagnostic record returned Data. */ | |
2123 | if (bgp_notify.length) | |
2124 | { | |
2125 | peer->notify.length = size - 2; | |
2126 | peer->notify.data = XMALLOC (MTYPE_TMP, size - 2); | |
2127 | memcpy (peer->notify.data, stream_pnt (peer->ibuf), size - 2); | |
2128 | } | |
2129 | ||
2130 | /* For debug */ | |
2131 | { | |
2132 | int i; | |
2133 | int first = 0; | |
2134 | char c[4]; | |
2135 | ||
2136 | if (bgp_notify.length) | |
2137 | { | |
2138 | bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3); | |
2139 | for (i = 0; i < bgp_notify.length; i++) | |
2140 | if (first) | |
2141 | { | |
2142 | sprintf (c, " %02x", stream_getc (peer->ibuf)); | |
2143 | strcat (bgp_notify.data, c); | |
2144 | } | |
2145 | else | |
2146 | { | |
2147 | first = 1; | |
2148 | sprintf (c, "%02x", stream_getc (peer->ibuf)); | |
2149 | strcpy (bgp_notify.data, c); | |
2150 | } | |
2151 | } | |
2152 | ||
2153 | bgp_notify_print(peer, &bgp_notify, "received"); | |
2154 | if (bgp_notify.data) | |
2155 | XFREE (MTYPE_TMP, bgp_notify.data); | |
2156 | } | |
2157 | ||
2158 | /* peer count update */ | |
2159 | peer->notify_in++; | |
2160 | ||
e0701b79 | 2161 | if (peer->status == Established) |
2162 | peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED; | |
2163 | ||
718e3744 | 2164 | /* We have to check for Notify with Unsupported Optional Parameter. |
2165 | in that case we fallback to open without the capability option. | |
2166 | But this done in bgp_stop. We just mark it here to avoid changing | |
2167 | the fsm tables. */ | |
2168 | if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR && | |
2169 | bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM ) | |
2170 | UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN); | |
2171 | ||
718e3744 | 2172 | BGP_EVENT_ADD (peer, Receive_NOTIFICATION_message); |
2173 | } | |
2174 | ||
2175 | /* Keepalive treatment function -- get keepalive send keepalive */ | |
94f2b392 | 2176 | static void |
718e3744 | 2177 | bgp_keepalive_receive (struct peer *peer, bgp_size_t size) |
2178 | { | |
2179 | if (BGP_DEBUG (keepalive, KEEPALIVE)) | |
6b51474d | 2180 | zlog_debug ("%s KEEPALIVE rcvd", peer->host); |
718e3744 | 2181 | |
2182 | BGP_EVENT_ADD (peer, Receive_KEEPALIVE_message); | |
2183 | } | |
2184 | ||
2185 | /* Route refresh message is received. */ | |
94f2b392 | 2186 | static void |
718e3744 | 2187 | bgp_route_refresh_receive (struct peer *peer, bgp_size_t size) |
2188 | { | |
2189 | afi_t afi; | |
2190 | safi_t safi; | |
2191 | u_char reserved; | |
2192 | struct stream *s; | |
2193 | ||
2194 | /* If peer does not have the capability, send notification. */ | |
2195 | if (! CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_ADV)) | |
2196 | { | |
2197 | plog_err (peer->log, "%s [Error] BGP route refresh is not enabled", | |
2198 | peer->host); | |
2199 | bgp_notify_send (peer, | |
2200 | BGP_NOTIFY_HEADER_ERR, | |
2201 | BGP_NOTIFY_HEADER_BAD_MESTYPE); | |
2202 | return; | |
2203 | } | |
2204 | ||
2205 | /* Status must be Established. */ | |
2206 | if (peer->status != Established) | |
2207 | { | |
2208 | plog_err (peer->log, | |
2209 | "%s [Error] Route refresh packet received under status %s", | |
2210 | peer->host, LOOKUP (bgp_status_msg, peer->status)); | |
2211 | bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0); | |
2212 | return; | |
2213 | } | |
2214 | ||
2215 | s = peer->ibuf; | |
2216 | ||
2217 | /* Parse packet. */ | |
2218 | afi = stream_getw (s); | |
2219 | reserved = stream_getc (s); | |
2220 | safi = stream_getc (s); | |
2221 | ||
2222 | if (BGP_DEBUG (normal, NORMAL)) | |
6b51474d | 2223 | zlog_debug ("%s rcvd REFRESH_REQ for afi/safi: %d/%d", |
718e3744 | 2224 | peer->host, afi, safi); |
2225 | ||
2226 | /* Check AFI and SAFI. */ | |
2227 | if ((afi != AFI_IP && afi != AFI_IP6) | |
2228 | || (safi != SAFI_UNICAST && safi != SAFI_MULTICAST | |
42e6d745 | 2229 | && safi != SAFI_MPLS_LABELED_VPN)) |
718e3744 | 2230 | { |
2231 | if (BGP_DEBUG (normal, NORMAL)) | |
2232 | { | |
6b51474d | 2233 | zlog_debug ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored", |
718e3744 | 2234 | peer->host, afi, safi); |
2235 | } | |
2236 | return; | |
2237 | } | |
2238 | ||
2239 | /* Adjust safi code. */ | |
42e6d745 | 2240 | if (safi == SAFI_MPLS_LABELED_VPN) |
718e3744 | 2241 | safi = SAFI_MPLS_VPN; |
2242 | ||
2243 | if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) | |
2244 | { | |
2245 | u_char *end; | |
2246 | u_char when_to_refresh; | |
2247 | u_char orf_type; | |
2248 | u_int16_t orf_len; | |
2249 | ||
2250 | if (size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) < 5) | |
2251 | { | |
2252 | zlog_info ("%s ORF route refresh length error", peer->host); | |
2253 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); | |
2254 | return; | |
2255 | } | |
2256 | ||
2257 | when_to_refresh = stream_getc (s); | |
2258 | end = stream_pnt (s) + (size - 5); | |
2259 | ||
370b64a2 | 2260 | while ((stream_pnt (s) + 2) < end) |
718e3744 | 2261 | { |
2262 | orf_type = stream_getc (s); | |
2263 | orf_len = stream_getw (s); | |
370b64a2 PJ |
2264 | |
2265 | /* orf_len in bounds? */ | |
2266 | if ((stream_pnt (s) + orf_len) > end) | |
2267 | break; /* XXX: Notify instead?? */ | |
718e3744 | 2268 | if (orf_type == ORF_TYPE_PREFIX |
2269 | || orf_type == ORF_TYPE_PREFIX_OLD) | |
2270 | { | |
2271 | u_char *p_pnt = stream_pnt (s); | |
2272 | u_char *p_end = stream_pnt (s) + orf_len; | |
2273 | struct orf_prefix orfp; | |
2274 | u_char common = 0; | |
2275 | u_int32_t seq; | |
2276 | int psize; | |
2277 | char name[BUFSIZ]; | |
718e3744 | 2278 | int ret; |
2279 | ||
2280 | if (BGP_DEBUG (normal, NORMAL)) | |
2281 | { | |
6b51474d | 2282 | zlog_debug ("%s rcvd Prefixlist ORF(%d) length %d", |
718e3744 | 2283 | peer->host, orf_type, orf_len); |
2284 | } | |
2285 | ||
370b64a2 PJ |
2286 | /* we're going to read at least 1 byte of common ORF header, |
2287 | * and 7 bytes of ORF Address-filter entry from the stream | |
2288 | */ | |
2289 | if (orf_len < 7) | |
2290 | break; | |
2291 | ||
718e3744 | 2292 | /* ORF prefix-list name */ |
2293 | sprintf (name, "%s.%d.%d", peer->host, afi, safi); | |
2294 | ||
2295 | while (p_pnt < p_end) | |
2296 | { | |
d64379e8 CH |
2297 | /* If the ORF entry is malformed, want to read as much of it |
2298 | * as possible without going beyond the bounds of the entry, | |
2299 | * to maximise debug information. | |
2300 | */ | |
fdbc8e77 | 2301 | int ok; |
718e3744 | 2302 | memset (&orfp, 0, sizeof (struct orf_prefix)); |
2303 | common = *p_pnt++; | |
d64379e8 | 2304 | /* after ++: p_pnt <= p_end */ |
718e3744 | 2305 | if (common & ORF_COMMON_PART_REMOVE_ALL) |
2306 | { | |
2307 | if (BGP_DEBUG (normal, NORMAL)) | |
6b51474d | 2308 | zlog_debug ("%s rcvd Remove-All pfxlist ORF request", peer->host); |
718e3744 | 2309 | prefix_bgp_orf_remove_all (name); |
2310 | break; | |
2311 | } | |
d64379e8 | 2312 | ok = ((p_end - p_pnt) >= sizeof(u_int32_t)) ; |
bb915f5f | 2313 | if (ok) |
d64379e8 | 2314 | { |
fdbc8e77 PJ |
2315 | memcpy (&seq, p_pnt, sizeof (u_int32_t)); |
2316 | p_pnt += sizeof (u_int32_t); | |
2317 | orfp.seq = ntohl (seq); | |
d64379e8 CH |
2318 | } |
2319 | else | |
2320 | p_pnt = p_end ; | |
2321 | ||
2322 | if ((ok = (p_pnt < p_end))) | |
2323 | orfp.ge = *p_pnt++ ; /* value checked in prefix_bgp_orf_set() */ | |
2324 | if ((ok = (p_pnt < p_end))) | |
2325 | orfp.le = *p_pnt++ ; /* value checked in prefix_bgp_orf_set() */ | |
2326 | if ((ok = (p_pnt < p_end))) | |
2327 | orfp.p.prefixlen = *p_pnt++ ; | |
2328 | orfp.p.family = afi2family (afi); /* afi checked already */ | |
2329 | ||
2330 | psize = PSIZE (orfp.p.prefixlen); /* 0 if not ok */ | |
2331 | if (psize > prefix_blen(&orfp.p)) /* valid for family ? */ | |
2332 | { | |
2333 | ok = 0 ; | |
2334 | psize = prefix_blen(&orfp.p) ; | |
2335 | } | |
2336 | if (psize > (p_end - p_pnt)) /* valid for packet ? */ | |
2337 | { | |
2338 | ok = 0 ; | |
2339 | psize = p_end - p_pnt ; | |
2340 | } | |
2341 | ||
2342 | if (psize > 0) | |
2343 | memcpy (&orfp.p.u.prefix, p_pnt, psize); | |
718e3744 | 2344 | p_pnt += psize; |
2345 | ||
2346 | if (BGP_DEBUG (normal, NORMAL)) | |
14542f3e JBD |
2347 | { |
2348 | char buf[INET6_BUFSIZ]; | |
2349 | ||
2350 | zlog_debug ("%s rcvd %s %s seq %u %s/%d ge %d le %d%s", | |
2351 | peer->host, | |
2352 | (common & ORF_COMMON_PART_REMOVE ? "Remove" : "Add"), | |
2353 | (common & ORF_COMMON_PART_DENY ? "deny" : "permit"), | |
2354 | orfp.seq, | |
2355 | inet_ntop (orfp.p.family, &orfp.p.u.prefix, buf, INET6_BUFSIZ), | |
2356 | orfp.p.prefixlen, orfp.ge, orfp.le, | |
2357 | ok ? "" : " MALFORMED"); | |
2358 | } | |
2359 | ||
d64379e8 | 2360 | if (ok) |
fdbc8e77 PJ |
2361 | ret = prefix_bgp_orf_set (name, afi, &orfp, |
2362 | (common & ORF_COMMON_PART_DENY ? 0 : 1 ), | |
2363 | (common & ORF_COMMON_PART_REMOVE ? 0 : 1)); | |
718e3744 | 2364 | |
d64379e8 | 2365 | if (!ok || (ret != CMD_SUCCESS)) |
718e3744 | 2366 | { |
2367 | if (BGP_DEBUG (normal, NORMAL)) | |
fdbc8e77 PJ |
2368 | zlog_debug ("%s Received misformatted prefixlist ORF." |
2369 | " Remove All pfxlist", peer->host); | |
718e3744 | 2370 | prefix_bgp_orf_remove_all (name); |
2371 | break; | |
2372 | } | |
2373 | } | |
2374 | peer->orf_plist[afi][safi] = | |
2375 | prefix_list_lookup (AFI_ORF_PREFIX, name); | |
2376 | } | |
9985f83c | 2377 | stream_forward_getp (s, orf_len); |
718e3744 | 2378 | } |
2379 | if (BGP_DEBUG (normal, NORMAL)) | |
6b51474d | 2380 | zlog_debug ("%s rcvd Refresh %s ORF request", peer->host, |
718e3744 | 2381 | when_to_refresh == REFRESH_DEFER ? "Defer" : "Immediate"); |
2382 | if (when_to_refresh == REFRESH_DEFER) | |
2383 | return; | |
2384 | } | |
2385 | ||
2386 | /* First update is deferred until ORF or ROUTE-REFRESH is received */ | |
2387 | if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH)) | |
2388 | UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH); | |
2389 | ||
2390 | /* Perform route refreshment to the peer */ | |
2391 | bgp_announce_route (peer, afi, safi); | |
2392 | } | |
2393 | ||
94f2b392 | 2394 | static int |
718e3744 | 2395 | bgp_capability_msg_parse (struct peer *peer, u_char *pnt, bgp_size_t length) |
2396 | { | |
2397 | u_char *end; | |
6d58272b PJ |
2398 | struct capability_mp_data mpc; |
2399 | struct capability_header *hdr; | |
718e3744 | 2400 | u_char action; |
2401 | struct bgp *bgp; | |
2402 | afi_t afi; | |
2403 | safi_t safi; | |
2404 | ||
2405 | bgp = peer->bgp; | |
2406 | end = pnt + length; | |
2407 | ||
2408 | while (pnt < end) | |
6d58272b | 2409 | { |
718e3744 | 2410 | /* We need at least action, capability code and capability length. */ |
2411 | if (pnt + 3 > end) | |
2412 | { | |
2413 | zlog_info ("%s Capability length error", peer->host); | |
2414 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); | |
2415 | return -1; | |
2416 | } | |
718e3744 | 2417 | action = *pnt; |
6d58272b PJ |
2418 | hdr = (struct capability_header *)(pnt + 1); |
2419 | ||
718e3744 | 2420 | /* Action value check. */ |
2421 | if (action != CAPABILITY_ACTION_SET | |
2422 | && action != CAPABILITY_ACTION_UNSET) | |
2423 | { | |
2424 | zlog_info ("%s Capability Action Value error %d", | |
2425 | peer->host, action); | |
2426 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); | |
2427 | return -1; | |
2428 | } | |
2429 | ||
2430 | if (BGP_DEBUG (normal, NORMAL)) | |
6b51474d | 2431 | zlog_debug ("%s CAPABILITY has action: %d, code: %u, length %u", |
6d58272b | 2432 | peer->host, action, hdr->code, hdr->length); |
718e3744 | 2433 | |
2434 | /* Capability length check. */ | |
6d58272b | 2435 | if ((pnt + hdr->length + 3) > end) |
718e3744 | 2436 | { |
2437 | zlog_info ("%s Capability length error", peer->host); | |
2438 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); | |
2439 | return -1; | |
2440 | } | |
2441 | ||
6d58272b PJ |
2442 | /* Fetch structure to the byte stream. */ |
2443 | memcpy (&mpc, pnt + 3, sizeof (struct capability_mp_data)); | |
2444 | ||
718e3744 | 2445 | /* We know MP Capability Code. */ |
6d58272b | 2446 | if (hdr->code == CAPABILITY_CODE_MP) |
718e3744 | 2447 | { |
6d58272b PJ |
2448 | afi = ntohs (mpc.afi); |
2449 | safi = mpc.safi; | |
718e3744 | 2450 | |
2451 | /* Ignore capability when override-capability is set. */ | |
2452 | if (CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) | |
2453 | continue; | |
6d58272b PJ |
2454 | |
2455 | if (!bgp_afi_safi_valid_indices (afi, &safi)) | |
2456 | { | |
2457 | if (BGP_DEBUG (normal, NORMAL)) | |
0b2aa3a0 PJ |
2458 | zlog_debug ("%s Dynamic Capability MP_EXT afi/safi invalid " |
2459 | "(%u/%u)", peer->host, afi, safi); | |
6d58272b PJ |
2460 | continue; |
2461 | } | |
2462 | ||
718e3744 | 2463 | /* Address family check. */ |
6d58272b PJ |
2464 | if (BGP_DEBUG (normal, NORMAL)) |
2465 | zlog_debug ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u", | |
2466 | peer->host, | |
2467 | action == CAPABILITY_ACTION_SET | |
2468 | ? "Advertising" : "Removing", | |
2469 | ntohs(mpc.afi) , mpc.safi); | |
2470 | ||
2471 | if (action == CAPABILITY_ACTION_SET) | |
2472 | { | |
2473 | peer->afc_recv[afi][safi] = 1; | |
2474 | if (peer->afc[afi][safi]) | |
2475 | { | |
2476 | peer->afc_nego[afi][safi] = 1; | |
2477 | bgp_announce_route (peer, afi, safi); | |
2478 | } | |
2479 | } | |
2480 | else | |
2481 | { | |
2482 | peer->afc_recv[afi][safi] = 0; | |
2483 | peer->afc_nego[afi][safi] = 0; | |
2484 | ||
2485 | if (peer_active_nego (peer)) | |
228da428 | 2486 | bgp_clear_route (peer, afi, safi, BGP_CLEAR_ROUTE_NORMAL); |
6d58272b PJ |
2487 | else |
2488 | BGP_EVENT_ADD (peer, BGP_Stop); | |
2489 | } | |
718e3744 | 2490 | } |
718e3744 | 2491 | else |
2492 | { | |
2493 | zlog_warn ("%s unrecognized capability code: %d - ignored", | |
6d58272b | 2494 | peer->host, hdr->code); |
718e3744 | 2495 | } |
6d58272b | 2496 | pnt += hdr->length + 3; |
718e3744 | 2497 | } |
2498 | return 0; | |
2499 | } | |
2500 | ||
01b7ce2d PJ |
2501 | /* Dynamic Capability is received. |
2502 | * | |
2503 | * This is exported for unit-test purposes | |
2504 | */ | |
6d58272b | 2505 | int |
718e3744 | 2506 | bgp_capability_receive (struct peer *peer, bgp_size_t size) |
2507 | { | |
2508 | u_char *pnt; | |
718e3744 | 2509 | |
2510 | /* Fetch pointer. */ | |
2511 | pnt = stream_pnt (peer->ibuf); | |
2512 | ||
2513 | if (BGP_DEBUG (normal, NORMAL)) | |
6b51474d | 2514 | zlog_debug ("%s rcv CAPABILITY", peer->host); |
718e3744 | 2515 | |
2516 | /* If peer does not have the capability, send notification. */ | |
2517 | if (! CHECK_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV)) | |
2518 | { | |
2519 | plog_err (peer->log, "%s [Error] BGP dynamic capability is not enabled", | |
2520 | peer->host); | |
2521 | bgp_notify_send (peer, | |
2522 | BGP_NOTIFY_HEADER_ERR, | |
2523 | BGP_NOTIFY_HEADER_BAD_MESTYPE); | |
0b2aa3a0 | 2524 | return -1; |
718e3744 | 2525 | } |
2526 | ||
2527 | /* Status must be Established. */ | |
2528 | if (peer->status != Established) | |
2529 | { | |
2530 | plog_err (peer->log, | |
2531 | "%s [Error] Dynamic capability packet received under status %s", peer->host, LOOKUP (bgp_status_msg, peer->status)); | |
2532 | bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0); | |
0b2aa3a0 | 2533 | return -1; |
718e3744 | 2534 | } |
2535 | ||
2536 | /* Parse packet. */ | |
6d58272b | 2537 | return bgp_capability_msg_parse (peer, pnt, size); |
718e3744 | 2538 | } |
6b0655a2 | 2539 | |
718e3744 | 2540 | /* BGP read utility function. */ |
94f2b392 | 2541 | static int |
718e3744 | 2542 | bgp_read_packet (struct peer *peer) |
2543 | { | |
2544 | int nbytes; | |
2545 | int readsize; | |
2546 | ||
9985f83c | 2547 | readsize = peer->packet_size - stream_get_endp (peer->ibuf); |
718e3744 | 2548 | |
2549 | /* If size is zero then return. */ | |
2550 | if (! readsize) | |
2551 | return 0; | |
2552 | ||
2553 | /* Read packet from fd. */ | |
35398589 | 2554 | nbytes = stream_read_try (peer->ibuf, peer->fd, readsize); |
718e3744 | 2555 | |
2556 | /* If read byte is smaller than zero then error occured. */ | |
2557 | if (nbytes < 0) | |
2558 | { | |
35398589 SH |
2559 | /* Transient error should retry */ |
2560 | if (nbytes == -2) | |
718e3744 | 2561 | return -1; |
2562 | ||
2563 | plog_err (peer->log, "%s [Error] bgp_read_packet error: %s", | |
6099b3b5 | 2564 | peer->host, safe_strerror (errno)); |
93406d87 | 2565 | |
2566 | if (peer->status == Established) | |
2567 | { | |
2568 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_MODE)) | |
2569 | { | |
2570 | peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION; | |
2571 | SET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT); | |
2572 | } | |
2573 | else | |
2574 | peer->last_reset = PEER_DOWN_CLOSE_SESSION; | |
2575 | } | |
2576 | ||
718e3744 | 2577 | BGP_EVENT_ADD (peer, TCP_fatal_error); |
2578 | return -1; | |
2579 | } | |
2580 | ||
2581 | /* When read byte is zero : clear bgp peer and return */ | |
2582 | if (nbytes == 0) | |
2583 | { | |
2584 | if (BGP_DEBUG (events, EVENTS)) | |
6b51474d | 2585 | plog_debug (peer->log, "%s [Event] BGP connection closed fd %d", |
eb821189 | 2586 | peer->host, peer->fd); |
e0701b79 | 2587 | |
2588 | if (peer->status == Established) | |
93406d87 | 2589 | { |
2590 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_MODE)) | |
2591 | { | |
2592 | peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION; | |
2593 | SET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT); | |
2594 | } | |
2595 | else | |
2596 | peer->last_reset = PEER_DOWN_CLOSE_SESSION; | |
2597 | } | |
e0701b79 | 2598 | |
718e3744 | 2599 | BGP_EVENT_ADD (peer, TCP_connection_closed); |
2600 | return -1; | |
2601 | } | |
2602 | ||
2603 | /* We read partial packet. */ | |
9985f83c | 2604 | if (stream_get_endp (peer->ibuf) != peer->packet_size) |
718e3744 | 2605 | return -1; |
2606 | ||
2607 | return 0; | |
2608 | } | |
2609 | ||
2610 | /* Marker check. */ | |
94f2b392 | 2611 | static int |
718e3744 | 2612 | bgp_marker_all_one (struct stream *s, int length) |
2613 | { | |
2614 | int i; | |
2615 | ||
2616 | for (i = 0; i < length; i++) | |
2617 | if (s->data[i] != 0xff) | |
2618 | return 0; | |
2619 | ||
2620 | return 1; | |
2621 | } | |
2622 | ||
d61c1bbd SH |
2623 | /* Recent thread time. |
2624 | On same clock base as bgp_clock (MONOTONIC) | |
2625 | but can be time of last context switch to bgp_read thread. */ | |
2626 | static time_t | |
2627 | bgp_recent_clock (void) | |
2628 | { | |
2629 | return recent_relative_time().tv_sec; | |
2630 | } | |
2631 | ||
718e3744 | 2632 | /* Starting point of packet process function. */ |
2633 | int | |
2634 | bgp_read (struct thread *thread) | |
2635 | { | |
2636 | int ret; | |
2637 | u_char type = 0; | |
2638 | struct peer *peer; | |
2639 | bgp_size_t size; | |
2640 | char notify_data_length[2]; | |
d6661008 | 2641 | u_int32_t notify_out; |
718e3744 | 2642 | |
2643 | /* Yes first of all get peer pointer. */ | |
2644 | peer = THREAD_ARG (thread); | |
2645 | peer->t_read = NULL; | |
2646 | ||
d6661008 DS |
2647 | /* Note notify_out so we can check later to see if we sent another one */ |
2648 | notify_out = peer->notify_out; | |
2649 | ||
718e3744 | 2650 | /* For non-blocking IO check. */ |
2651 | if (peer->status == Connect) | |
2652 | { | |
fc9a856f | 2653 | bgp_connect_check (peer, 1); |
718e3744 | 2654 | goto done; |
2655 | } | |
2656 | else | |
2657 | { | |
eb821189 | 2658 | if (peer->fd < 0) |
718e3744 | 2659 | { |
eb821189 | 2660 | zlog_err ("bgp_read peer's fd is negative value %d", peer->fd); |
718e3744 | 2661 | return -1; |
2662 | } | |
eb821189 | 2663 | BGP_READ_ON (peer->t_read, bgp_read, peer->fd); |
718e3744 | 2664 | } |
2665 | ||
2666 | /* Read packet header to determine type of the packet */ | |
2667 | if (peer->packet_size == 0) | |
2668 | peer->packet_size = BGP_HEADER_SIZE; | |
2669 | ||
9985f83c | 2670 | if (stream_get_endp (peer->ibuf) < BGP_HEADER_SIZE) |
718e3744 | 2671 | { |
2672 | ret = bgp_read_packet (peer); | |
2673 | ||
2674 | /* Header read error or partial read packet. */ | |
2675 | if (ret < 0) | |
2676 | goto done; | |
2677 | ||
2678 | /* Get size and type. */ | |
9985f83c | 2679 | stream_forward_getp (peer->ibuf, BGP_MARKER_SIZE); |
718e3744 | 2680 | memcpy (notify_data_length, stream_pnt (peer->ibuf), 2); |
2681 | size = stream_getw (peer->ibuf); | |
2682 | type = stream_getc (peer->ibuf); | |
2683 | ||
2684 | if (BGP_DEBUG (normal, NORMAL) && type != 2 && type != 0) | |
6b51474d | 2685 | zlog_debug ("%s rcv message type %d, length (excl. header) %d", |
718e3744 | 2686 | peer->host, type, size - BGP_HEADER_SIZE); |
2687 | ||
2688 | /* Marker check */ | |
f5ba3874 | 2689 | if (((type == BGP_MSG_OPEN) || (type == BGP_MSG_KEEPALIVE)) |
718e3744 | 2690 | && ! bgp_marker_all_one (peer->ibuf, BGP_MARKER_SIZE)) |
2691 | { | |
2692 | bgp_notify_send (peer, | |
2693 | BGP_NOTIFY_HEADER_ERR, | |
2694 | BGP_NOTIFY_HEADER_NOT_SYNC); | |
2695 | goto done; | |
2696 | } | |
2697 | ||
2698 | /* BGP type check. */ | |
2699 | if (type != BGP_MSG_OPEN && type != BGP_MSG_UPDATE | |
2700 | && type != BGP_MSG_NOTIFY && type != BGP_MSG_KEEPALIVE | |
2701 | && type != BGP_MSG_ROUTE_REFRESH_NEW | |
2702 | && type != BGP_MSG_ROUTE_REFRESH_OLD | |
2703 | && type != BGP_MSG_CAPABILITY) | |
2704 | { | |
2705 | if (BGP_DEBUG (normal, NORMAL)) | |
6b51474d | 2706 | plog_debug (peer->log, |
718e3744 | 2707 | "%s unknown message type 0x%02x", |
2708 | peer->host, type); | |
2709 | bgp_notify_send_with_data (peer, | |
2710 | BGP_NOTIFY_HEADER_ERR, | |
2711 | BGP_NOTIFY_HEADER_BAD_MESTYPE, | |
2712 | &type, 1); | |
2713 | goto done; | |
2714 | } | |
2715 | /* Mimimum packet length check. */ | |
2716 | if ((size < BGP_HEADER_SIZE) | |
2717 | || (size > BGP_MAX_PACKET_SIZE) | |
2718 | || (type == BGP_MSG_OPEN && size < BGP_MSG_OPEN_MIN_SIZE) | |
2719 | || (type == BGP_MSG_UPDATE && size < BGP_MSG_UPDATE_MIN_SIZE) | |
2720 | || (type == BGP_MSG_NOTIFY && size < BGP_MSG_NOTIFY_MIN_SIZE) | |
2721 | || (type == BGP_MSG_KEEPALIVE && size != BGP_MSG_KEEPALIVE_MIN_SIZE) | |
2722 | || (type == BGP_MSG_ROUTE_REFRESH_NEW && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE) | |
2723 | || (type == BGP_MSG_ROUTE_REFRESH_OLD && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE) | |
2724 | || (type == BGP_MSG_CAPABILITY && size < BGP_MSG_CAPABILITY_MIN_SIZE)) | |
2725 | { | |
2726 | if (BGP_DEBUG (normal, NORMAL)) | |
6b51474d | 2727 | plog_debug (peer->log, |
718e3744 | 2728 | "%s bad message length - %d for %s", |
2729 | peer->host, size, | |
2730 | type == 128 ? "ROUTE-REFRESH" : | |
2731 | bgp_type_str[(int) type]); | |
2732 | bgp_notify_send_with_data (peer, | |
2733 | BGP_NOTIFY_HEADER_ERR, | |
2734 | BGP_NOTIFY_HEADER_BAD_MESLEN, | |
c9e52be3 | 2735 | (u_char *) notify_data_length, 2); |
718e3744 | 2736 | goto done; |
2737 | } | |
2738 | ||
2739 | /* Adjust size to message length. */ | |
2740 | peer->packet_size = size; | |
2741 | } | |
2742 | ||
2743 | ret = bgp_read_packet (peer); | |
2744 | if (ret < 0) | |
2745 | goto done; | |
2746 | ||
2747 | /* Get size and type again. */ | |
2748 | size = stream_getw_from (peer->ibuf, BGP_MARKER_SIZE); | |
2749 | type = stream_getc_from (peer->ibuf, BGP_MARKER_SIZE + 2); | |
2750 | ||
2751 | /* BGP packet dump function. */ | |
2752 | bgp_dump_packet (peer, type, peer->ibuf); | |
2753 | ||
2754 | size = (peer->packet_size - BGP_HEADER_SIZE); | |
2755 | ||
2756 | /* Read rest of the packet and call each sort of packet routine */ | |
2757 | switch (type) | |
2758 | { | |
2759 | case BGP_MSG_OPEN: | |
2760 | peer->open_in++; | |
f5ba3874 | 2761 | bgp_open_receive (peer, size); /* XXX return value ignored! */ |
718e3744 | 2762 | break; |
2763 | case BGP_MSG_UPDATE: | |
d61c1bbd | 2764 | peer->readtime = bgp_recent_clock (); |
718e3744 | 2765 | bgp_update_receive (peer, size); |
2766 | break; | |
2767 | case BGP_MSG_NOTIFY: | |
2768 | bgp_notify_receive (peer, size); | |
2769 | break; | |
2770 | case BGP_MSG_KEEPALIVE: | |
d61c1bbd | 2771 | peer->readtime = bgp_recent_clock (); |
718e3744 | 2772 | bgp_keepalive_receive (peer, size); |
2773 | break; | |
2774 | case BGP_MSG_ROUTE_REFRESH_NEW: | |
2775 | case BGP_MSG_ROUTE_REFRESH_OLD: | |
2776 | peer->refresh_in++; | |
2777 | bgp_route_refresh_receive (peer, size); | |
2778 | break; | |
2779 | case BGP_MSG_CAPABILITY: | |
2780 | peer->dynamic_cap_in++; | |
2781 | bgp_capability_receive (peer, size); | |
2782 | break; | |
2783 | } | |
2784 | ||
d6661008 DS |
2785 | /* If reading this packet caused us to send a NOTIFICATION then store a copy |
2786 | * of the packet for troubleshooting purposes | |
2787 | */ | |
2788 | if (notify_out < peer->notify_out) | |
2789 | { | |
2790 | memcpy(peer->last_reset_cause, peer->ibuf->data, peer->packet_size); | |
2791 | peer->last_reset_cause_size = peer->packet_size; | |
2792 | notify_out = peer->notify_out; | |
2793 | } | |
2794 | ||
718e3744 | 2795 | /* Clear input buffer. */ |
2796 | peer->packet_size = 0; | |
2797 | if (peer->ibuf) | |
2798 | stream_reset (peer->ibuf); | |
2799 | ||
2800 | done: | |
d6661008 DS |
2801 | /* If reading this packet caused us to send a NOTIFICATION then store a copy |
2802 | * of the packet for troubleshooting purposes | |
2803 | */ | |
2804 | if (notify_out < peer->notify_out) | |
2805 | { | |
2806 | memcpy(peer->last_reset_cause, peer->ibuf->data, peer->packet_size); | |
2807 | peer->last_reset_cause_size = peer->packet_size; | |
2808 | } | |
2809 | ||
718e3744 | 2810 | return 0; |
2811 | } |