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