]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_packet.c
bgpd: move bgp i/o to a separate source file
[mirror_frr.git] / bgpd / bgp_packet.c
1 /* BGP packet management routine.
2 * Contains utility functions for constructing and consuming BGP messages.
3 * Copyright (C) 2017 Cumulus Networks
4 * Copyright (C) 1999 Kunihiro Ishiguro
5 *
6 * This file is part of GNU Zebra.
7 *
8 * GNU Zebra is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <zebra.h>
24 #include <sys/time.h>
25
26 #include "thread.h"
27 #include "stream.h"
28 #include "network.h"
29 #include "prefix.h"
30 #include "command.h"
31 #include "log.h"
32 #include "memory.h"
33 #include "sockunion.h" /* for inet_ntop () */
34 #include "sockopt.h"
35 #include "linklist.h"
36 #include "plist.h"
37 #include "queue.h"
38 #include "filter.h"
39
40 #include "bgpd/bgpd.h"
41 #include "bgpd/bgp_table.h"
42 #include "bgpd/bgp_dump.h"
43 #include "bgpd/bgp_attr.h"
44 #include "bgpd/bgp_debug.h"
45 #include "bgpd/bgp_fsm.h"
46 #include "bgpd/bgp_route.h"
47 #include "bgpd/bgp_packet.h"
48 #include "bgpd/bgp_open.h"
49 #include "bgpd/bgp_aspath.h"
50 #include "bgpd/bgp_community.h"
51 #include "bgpd/bgp_ecommunity.h"
52 #include "bgpd/bgp_lcommunity.h"
53 #include "bgpd/bgp_network.h"
54 #include "bgpd/bgp_mplsvpn.h"
55 #include "bgpd/bgp_evpn.h"
56 #include "bgpd/bgp_advertise.h"
57 #include "bgpd/bgp_vty.h"
58 #include "bgpd/bgp_updgrp.h"
59 #include "bgpd/bgp_label.h"
60 #include "bgpd/bgp_io.h"
61
62 /* Linked list of active peers */
63 static pthread_mutex_t *plist_mtx;
64 static pthread_cond_t *write_cond;
65 static struct list *plist;
66
67 /* periodically scheduled thread to generate update-group updates */
68 static struct thread *t_generate_updgrp_packets;
69
70 bool bgp_packet_writes_thread_run = false;
71
72 /* Set up BGP packet marker and packet type. */
73 int bgp_packet_set_marker(struct stream *s, u_char type)
74 {
75 int i;
76
77 /* Fill in marker. */
78 for (i = 0; i < BGP_MARKER_SIZE; i++)
79 stream_putc(s, 0xff);
80
81 /* Dummy total length. This field is should be filled in later on. */
82 stream_putw(s, 0);
83
84 /* BGP packet type. */
85 stream_putc(s, type);
86
87 /* Return current stream size. */
88 return stream_get_endp(s);
89 }
90
91 /* Set BGP packet header size entry. If size is zero then use current
92 stream size. */
93 int bgp_packet_set_size(struct stream *s)
94 {
95 int cp;
96
97 /* Preserve current pointer. */
98 cp = stream_get_endp(s);
99 stream_putw_at(s, BGP_MARKER_SIZE, cp);
100
101 return cp;
102 }
103
104 /*
105 * Push a packet onto the beginning of the peer's output queue.
106 * This function acquires the peer's write mutex before proceeding.
107 */
108 static void bgp_packet_add(struct peer *peer, struct stream *s)
109 {
110 pthread_mutex_lock(&peer->obuf_mtx);
111 stream_fifo_push(peer->obuf, s);
112 pthread_mutex_unlock(&peer->obuf_mtx);
113
114 peer_writes_wake();
115 }
116
117 static struct stream *bgp_update_packet_eor(struct peer *peer, afi_t afi,
118 safi_t safi)
119 {
120 struct stream *s;
121 iana_afi_t pkt_afi;
122 iana_safi_t pkt_safi;
123
124 if (DISABLE_BGP_ANNOUNCE)
125 return NULL;
126
127 if (bgp_debug_neighbor_events(peer))
128 zlog_debug("send End-of-RIB for %s to %s",
129 afi_safi_print(afi, safi), peer->host);
130
131 s = stream_new(BGP_MAX_PACKET_SIZE);
132
133 /* Make BGP update packet. */
134 bgp_packet_set_marker(s, BGP_MSG_UPDATE);
135
136 /* Unfeasible Routes Length */
137 stream_putw(s, 0);
138
139 if (afi == AFI_IP && safi == SAFI_UNICAST) {
140 /* Total Path Attribute Length */
141 stream_putw(s, 0);
142 } else {
143 /* Convert AFI, SAFI to values for packet. */
144 bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
145
146 /* Total Path Attribute Length */
147 stream_putw(s, 6);
148 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
149 stream_putc(s, BGP_ATTR_MP_UNREACH_NLRI);
150 stream_putc(s, 3);
151 stream_putw(s, pkt_afi);
152 stream_putc(s, pkt_safi);
153 }
154
155 bgp_packet_set_size(s);
156 return s;
157 }
158
159 /*
160 * Enqueue onto the peer's output buffer any packets which are pending for the
161 * update group it is a member of.
162 *
163 * XXX: Severely needs performance work.
164 */
165 int bgp_generate_updgrp_packets(struct thread *thread)
166 {
167 struct peer *peer = THREAD_ARG(thread);
168 peer->t_generate_updgrp_packets = NULL;
169
170 struct stream *s;
171 struct peer_af *paf;
172 struct bpacket *next_pkt;
173 afi_t afi;
174 safi_t safi;
175
176 /*
177 * The code beyond this part deals with update packets, proceed only
178 * if peer is Established and updates are not on hold (as part of
179 * update-delay post processing).
180 */
181 if (peer->status != Established)
182 return 0;
183
184 if (peer->bgp && peer->bgp->main_peers_update_hold)
185 return 0;
186
187 do {
188 s = NULL;
189 for (afi = AFI_IP; afi < AFI_MAX; afi++)
190 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) {
191 paf = peer_af_find(peer, afi, safi);
192 if (!paf || !PAF_SUBGRP(paf))
193 continue;
194 next_pkt = paf->next_pkt_to_send;
195
196 /* Try to generate a packet for the peer if we
197 * are at the end of
198 * the list. Always try to push out WITHDRAWs
199 * first. */
200 if (!next_pkt || !next_pkt->buffer) {
201 next_pkt = subgroup_withdraw_packet(
202 PAF_SUBGRP(paf));
203 if (!next_pkt || !next_pkt->buffer)
204 subgroup_update_packet(
205 PAF_SUBGRP(paf));
206 next_pkt = paf->next_pkt_to_send;
207 }
208
209 /* If we still don't have a packet to send to
210 * the peer, then
211 * try to find out out if we have to send eor or
212 * if not, skip to
213 * the next AFI, SAFI.
214 * Don't send the EOR prematurely... if the
215 * subgroup's coalesce
216 * timer is running, the adjacency-out structure
217 * is not created
218 * yet.
219 */
220 if (!next_pkt || !next_pkt->buffer) {
221 if (CHECK_FLAG(peer->cap,
222 PEER_CAP_RESTART_RCV)) {
223 if (!(PAF_SUBGRP(paf))
224 ->t_coalesce
225 && peer->afc_nego[afi][safi]
226 && peer->synctime
227 && !CHECK_FLAG(
228 peer->af_sflags
229 [afi]
230 [safi],
231 PEER_STATUS_EOR_SEND)) {
232 SET_FLAG(
233 peer->af_sflags
234 [afi]
235 [safi],
236 PEER_STATUS_EOR_SEND);
237
238 if ((s = bgp_update_packet_eor(
239 peer, afi,
240 safi)))
241 bgp_packet_add(
242 peer,
243 s);
244 }
245 }
246 continue;
247 }
248
249
250 /* Found a packet template to send, overwrite
251 * packet with appropriate
252 * attributes from peer and advance peer */
253 s = bpacket_reformat_for_peer(next_pkt, paf);
254 bgp_packet_add(peer, s);
255 bpacket_queue_advance_peer(paf);
256 }
257 } while (s);
258
259 return 0;
260 }
261
262 /*
263 * Creates a BGP Keepalive packet and appends it to the peer's output queue.
264 */
265 void bgp_keepalive_send(struct peer *peer)
266 {
267 struct stream *s;
268
269 s = stream_new(BGP_MAX_PACKET_SIZE);
270
271 /* Make keepalive packet. */
272 bgp_packet_set_marker(s, BGP_MSG_KEEPALIVE);
273
274 /* Set packet size. */
275 (void)bgp_packet_set_size(s);
276
277 /* Dump packet if debug option is set. */
278 /* bgp_packet_dump (s); */
279
280 if (bgp_debug_keepalive(peer))
281 zlog_debug("%s sending KEEPALIVE", peer->host);
282
283 /* Add packet to the peer. */
284 bgp_packet_add(peer, s);
285 }
286
287 /*
288 * Creates a BGP Open packet and appends it to the peer's output queue.
289 * Sets capabilities as necessary.
290 */
291 void bgp_open_send(struct peer *peer)
292 {
293 struct stream *s;
294 u_int16_t send_holdtime;
295 as_t local_as;
296
297 if (PEER_OR_GROUP_TIMER_SET(peer))
298 send_holdtime = peer->holdtime;
299 else
300 send_holdtime = peer->bgp->default_holdtime;
301
302 /* local-as Change */
303 if (peer->change_local_as)
304 local_as = peer->change_local_as;
305 else
306 local_as = peer->local_as;
307
308 s = stream_new(BGP_MAX_PACKET_SIZE);
309
310 /* Make open packet. */
311 bgp_packet_set_marker(s, BGP_MSG_OPEN);
312
313 /* Set open packet values. */
314 stream_putc(s, BGP_VERSION_4); /* BGP version */
315 stream_putw(s,
316 (local_as <= BGP_AS_MAX) ? (u_int16_t)local_as
317 : BGP_AS_TRANS);
318 stream_putw(s, send_holdtime); /* Hold Time */
319 stream_put_in_addr(s, &peer->local_id); /* BGP Identifier */
320
321 /* Set capability code. */
322 bgp_open_capability(s, peer);
323
324 /* Set BGP packet length. */
325 (void)bgp_packet_set_size(s);
326
327 if (bgp_debug_neighbor_events(peer))
328 zlog_debug(
329 "%s sending OPEN, version %d, my as %u, holdtime %d, id %s",
330 peer->host, BGP_VERSION_4, local_as, send_holdtime,
331 inet_ntoa(peer->local_id));
332
333 /* Dump packet if debug option is set. */
334 /* bgp_packet_dump (s); */
335
336 /* Add packet to the peer. */
337 bgp_packet_add(peer, s);
338 }
339
340 /*
341 * Creates a BGP Notify and appends it to the peer's output queue.
342 *
343 * This function awakens the write thread to ensure the packet
344 * gets out ASAP.
345 *
346 * @param peer
347 * @param code BGP error code
348 * @param sub_code BGP error subcode
349 * @param data Data portion
350 * @param datalen length of data portion
351 */
352 void bgp_notify_send_with_data(struct peer *peer, u_char code, u_char sub_code,
353 u_char *data, size_t datalen)
354 {
355 struct stream *s;
356 int length;
357
358 /* Allocate new stream. */
359 s = stream_new(BGP_MAX_PACKET_SIZE);
360
361 /* Make notify packet. */
362 bgp_packet_set_marker(s, BGP_MSG_NOTIFY);
363
364 /* Set notify packet values. */
365 stream_putc(s, code); /* BGP notify code */
366 stream_putc(s, sub_code); /* BGP notify sub_code */
367
368 /* If notify data is present. */
369 if (data)
370 stream_write(s, data, datalen);
371
372 /* Set BGP packet length. */
373 length = bgp_packet_set_size(s);
374
375 /* Add packet to the peer. */
376 pthread_mutex_lock(&peer->obuf_mtx);
377 stream_fifo_clean(peer->obuf);
378 pthread_mutex_unlock(&peer->obuf_mtx);
379
380 /* For debug */
381 {
382 struct bgp_notify bgp_notify;
383 int first = 0;
384 int i;
385 char c[4];
386
387 bgp_notify.code = code;
388 bgp_notify.subcode = sub_code;
389 bgp_notify.data = NULL;
390 bgp_notify.length = length - BGP_MSG_NOTIFY_MIN_SIZE;
391 bgp_notify.raw_data = data;
392
393 peer->notify.code = bgp_notify.code;
394 peer->notify.subcode = bgp_notify.subcode;
395
396 if (bgp_notify.length) {
397 bgp_notify.data =
398 XMALLOC(MTYPE_TMP, bgp_notify.length * 3);
399 for (i = 0; i < bgp_notify.length; i++)
400 if (first) {
401 sprintf(c, " %02x", data[i]);
402 strcat(bgp_notify.data, c);
403 } else {
404 first = 1;
405 sprintf(c, "%02x", data[i]);
406 strcpy(bgp_notify.data, c);
407 }
408 }
409 bgp_notify_print(peer, &bgp_notify, "sending");
410
411 if (bgp_notify.data) {
412 XFREE(MTYPE_TMP, bgp_notify.data);
413 bgp_notify.data = NULL;
414 bgp_notify.length = 0;
415 }
416 }
417
418 /* peer reset cause */
419 if (code == BGP_NOTIFY_CEASE) {
420 if (sub_code == BGP_NOTIFY_CEASE_ADMIN_RESET)
421 peer->last_reset = PEER_DOWN_USER_RESET;
422 else if (sub_code == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN)
423 peer->last_reset = PEER_DOWN_USER_SHUTDOWN;
424 else
425 peer->last_reset = PEER_DOWN_NOTIFY_SEND;
426 } else
427 peer->last_reset = PEER_DOWN_NOTIFY_SEND;
428
429 /* Add packet to peer's output queue */
430 bgp_packet_add(peer, s);
431 /* Wake up the write thread to get the notify out ASAP */
432 peer_writes_wake();
433 }
434
435 /*
436 * Creates a BGP Notify and appends it to the peer's output queue.
437 *
438 * This function awakens the write thread to ensure the packet
439 * gets out ASAP.
440 *
441 * @param peer
442 * @param code BGP error code
443 * @param sub_code BGP error subcode
444 */
445 void bgp_notify_send(struct peer *peer, u_char code, u_char sub_code)
446 {
447 bgp_notify_send_with_data(peer, code, sub_code, NULL, 0);
448 }
449
450 /*
451 * Creates BGP Route Refresh packet and appends it to the peer's output queue.
452 *
453 * @param peer
454 * @param afi Address Family Identifier
455 * @param safi Subsequent Address Family Identifier
456 * @param orf_type Outbound Route Filtering type
457 * @param when_to_refresh Whether to refresh immediately or defer
458 * @param remove Whether to remove ORF for specified AFI/SAFI
459 */
460 void bgp_route_refresh_send(struct peer *peer, afi_t afi, safi_t safi,
461 u_char orf_type, u_char when_to_refresh, int remove)
462 {
463 struct stream *s;
464 struct bgp_filter *filter;
465 int orf_refresh = 0;
466 iana_afi_t pkt_afi;
467 iana_safi_t pkt_safi;
468
469 if (DISABLE_BGP_ANNOUNCE)
470 return;
471
472 filter = &peer->filter[afi][safi];
473
474 /* Convert AFI, SAFI to values for packet. */
475 bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
476
477 s = stream_new(BGP_MAX_PACKET_SIZE);
478
479 /* Make BGP update packet. */
480 if (CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_NEW_RCV))
481 bgp_packet_set_marker(s, BGP_MSG_ROUTE_REFRESH_NEW);
482 else
483 bgp_packet_set_marker(s, BGP_MSG_ROUTE_REFRESH_OLD);
484
485 /* Encode Route Refresh message. */
486 stream_putw(s, pkt_afi);
487 stream_putc(s, 0);
488 stream_putc(s, pkt_safi);
489
490 if (orf_type == ORF_TYPE_PREFIX || orf_type == ORF_TYPE_PREFIX_OLD)
491 if (remove || filter->plist[FILTER_IN].plist) {
492 u_int16_t orf_len;
493 unsigned long orfp;
494
495 orf_refresh = 1;
496 stream_putc(s, when_to_refresh);
497 stream_putc(s, orf_type);
498 orfp = stream_get_endp(s);
499 stream_putw(s, 0);
500
501 if (remove) {
502 UNSET_FLAG(peer->af_sflags[afi][safi],
503 PEER_STATUS_ORF_PREFIX_SEND);
504 stream_putc(s, ORF_COMMON_PART_REMOVE_ALL);
505 if (bgp_debug_neighbor_events(peer))
506 zlog_debug(
507 "%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
508 peer->host, orf_type,
509 (when_to_refresh == REFRESH_DEFER
510 ? "defer"
511 : "immediate"),
512 pkt_afi, pkt_safi);
513 } else {
514 SET_FLAG(peer->af_sflags[afi][safi],
515 PEER_STATUS_ORF_PREFIX_SEND);
516 prefix_bgp_orf_entry(
517 s, filter->plist[FILTER_IN].plist,
518 ORF_COMMON_PART_ADD,
519 ORF_COMMON_PART_PERMIT,
520 ORF_COMMON_PART_DENY);
521 if (bgp_debug_neighbor_events(peer))
522 zlog_debug(
523 "%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
524 peer->host, orf_type,
525 (when_to_refresh == REFRESH_DEFER
526 ? "defer"
527 : "immediate"),
528 pkt_afi, pkt_safi);
529 }
530
531 /* Total ORF Entry Len. */
532 orf_len = stream_get_endp(s) - orfp - 2;
533 stream_putw_at(s, orfp, orf_len);
534 }
535
536 /* Set packet size. */
537 (void)bgp_packet_set_size(s);
538
539 if (bgp_debug_neighbor_events(peer)) {
540 if (!orf_refresh)
541 zlog_debug("%s sending REFRESH_REQ for afi/safi: %d/%d",
542 peer->host, pkt_afi, pkt_safi);
543 }
544
545 /* Add packet to the peer. */
546 bgp_packet_add(peer, s);
547 }
548
549 /*
550 * Create a BGP Capability packet and append it to the peer's output queue.
551 *
552 * @param peer
553 * @param afi Address Family Identifier
554 * @param safi Subsequent Address Family Identifier
555 * @param capability_code BGP Capability Code
556 * @param action Set or Remove capability
557 */
558 void bgp_capability_send(struct peer *peer, afi_t afi, safi_t safi,
559 int capability_code, int action)
560 {
561 struct stream *s;
562 iana_afi_t pkt_afi;
563 iana_safi_t pkt_safi;
564
565 /* Convert AFI, SAFI to values for packet. */
566 bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
567
568 s = stream_new(BGP_MAX_PACKET_SIZE);
569
570 /* Make BGP update packet. */
571 bgp_packet_set_marker(s, BGP_MSG_CAPABILITY);
572
573 /* Encode MP_EXT capability. */
574 if (capability_code == CAPABILITY_CODE_MP) {
575 stream_putc(s, action);
576 stream_putc(s, CAPABILITY_CODE_MP);
577 stream_putc(s, CAPABILITY_CODE_MP_LEN);
578 stream_putw(s, pkt_afi);
579 stream_putc(s, 0);
580 stream_putc(s, pkt_safi);
581
582 if (bgp_debug_neighbor_events(peer))
583 zlog_debug(
584 "%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
585 peer->host,
586 action == CAPABILITY_ACTION_SET ? "Advertising"
587 : "Removing",
588 pkt_afi, pkt_safi);
589 }
590
591 /* Set packet size. */
592 (void)bgp_packet_set_size(s);
593
594 /* Add packet to the peer. */
595 bgp_packet_add(peer, s);
596 }
597
598 /* RFC1771 6.8 Connection collision detection. */
599 static int bgp_collision_detect(struct peer *new, struct in_addr remote_id)
600 {
601 struct peer *peer;
602
603 /* Upon receipt of an OPEN message, the local system must examine
604 all of its connections that are in the OpenConfirm state. A BGP
605 speaker may also examine connections in an OpenSent state if it
606 knows the BGP Identifier of the peer by means outside of the
607 protocol. If among these connections there is a connection to a
608 remote BGP speaker whose BGP Identifier equals the one in the
609 OPEN message, then the local system performs the following
610 collision resolution procedure: */
611
612 if ((peer = new->doppelganger) != NULL) {
613 /* Do not accept the new connection in Established or Clearing
614 * states.
615 * Note that a peer GR is handled by closing the existing
616 * connection
617 * upon receipt of new one.
618 */
619 if (peer->status == Established || peer->status == Clearing) {
620 bgp_notify_send(new, BGP_NOTIFY_CEASE,
621 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
622 return (-1);
623 } else if ((peer->status == OpenConfirm)
624 || (peer->status == OpenSent)) {
625 /* 1. The BGP Identifier of the local system is compared
626 to
627 the BGP Identifier of the remote system (as specified
628 in
629 the OPEN message). */
630
631 if (ntohl(peer->local_id.s_addr)
632 < ntohl(remote_id.s_addr))
633 if (!CHECK_FLAG(peer->sflags,
634 PEER_STATUS_ACCEPT_PEER)) {
635 /* 2. If the value of the local BGP
636 Identifier is less
637 than the remote one, the local system
638 closes BGP
639 connection that already exists (the
640 one that is
641 already in the OpenConfirm state),
642 and accepts BGP
643 connection initiated by the remote
644 system. */
645 bgp_notify_send(
646 peer, BGP_NOTIFY_CEASE,
647 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
648 return 1;
649 } else {
650 bgp_notify_send(
651 new, BGP_NOTIFY_CEASE,
652 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
653 return -1;
654 }
655 else {
656 /* 3. Otherwise, the local system closes newly
657 created
658 BGP connection (the one associated with the
659 newly
660 received OPEN message), and continues to use
661 the
662 existing one (the one that is already in the
663 OpenConfirm state). */
664 if (CHECK_FLAG(peer->sflags,
665 PEER_STATUS_ACCEPT_PEER)) {
666 bgp_notify_send(
667 peer, BGP_NOTIFY_CEASE,
668 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
669 return 1;
670 } else {
671 bgp_notify_send(
672 new, BGP_NOTIFY_CEASE,
673 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
674 return -1;
675 }
676 }
677 }
678 }
679 return 0;
680 }
681
682 static int bgp_open_receive(struct peer *peer, bgp_size_t size)
683 {
684 int ret;
685 u_char version;
686 u_char optlen;
687 u_int16_t holdtime;
688 u_int16_t send_holdtime;
689 as_t remote_as;
690 as_t as4 = 0;
691 struct in_addr remote_id;
692 int mp_capability;
693 u_int8_t notify_data_remote_as[2];
694 u_int8_t notify_data_remote_as4[4];
695 u_int8_t notify_data_remote_id[4];
696 u_int16_t *holdtime_ptr;
697
698 /* Parse open packet. */
699 version = stream_getc(peer->ibuf);
700 memcpy(notify_data_remote_as, stream_pnt(peer->ibuf), 2);
701 remote_as = stream_getw(peer->ibuf);
702 holdtime_ptr = (u_int16_t *)stream_pnt(peer->ibuf);
703 holdtime = stream_getw(peer->ibuf);
704 memcpy(notify_data_remote_id, stream_pnt(peer->ibuf), 4);
705 remote_id.s_addr = stream_get_ipv4(peer->ibuf);
706
707 /* Receive OPEN message log */
708 if (bgp_debug_neighbor_events(peer))
709 zlog_debug(
710 "%s rcv OPEN, version %d, remote-as (in open) %u,"
711 " holdtime %d, id %s",
712 peer->host, version, remote_as, holdtime,
713 inet_ntoa(remote_id));
714
715 /* BEGIN to read the capability here, but dont do it yet */
716 mp_capability = 0;
717 optlen = stream_getc(peer->ibuf);
718
719 if (optlen != 0) {
720 /* If not enough bytes, it is an error. */
721 if (STREAM_READABLE(peer->ibuf) < optlen) {
722 bgp_notify_send(peer, BGP_NOTIFY_OPEN_ERR,
723 BGP_NOTIFY_OPEN_MALFORMED_ATTR);
724 return -1;
725 }
726
727 /* We need the as4 capability value *right now* because
728 * if it is there, we have not got the remote_as yet, and
729 * without
730 * that we do not know which peer is connecting to us now.
731 */
732 as4 = peek_for_as4_capability(peer, optlen);
733 memcpy(notify_data_remote_as4, &as4, 4);
734 }
735
736 /* Just in case we have a silly peer who sends AS4 capability set to 0
737 */
738 if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV) && !as4) {
739 zlog_err("%s bad OPEN, got AS4 capability, but AS4 set to 0",
740 peer->host);
741 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
742 BGP_NOTIFY_OPEN_BAD_PEER_AS,
743 notify_data_remote_as4, 4);
744 return -1;
745 }
746
747 if (remote_as == BGP_AS_TRANS) {
748 /* Take the AS4 from the capability. We must have received the
749 * capability now! Otherwise we have a asn16 peer who uses
750 * BGP_AS_TRANS, for some unknown reason.
751 */
752 if (as4 == BGP_AS_TRANS) {
753 zlog_err(
754 "%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
755 peer->host);
756 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
757 BGP_NOTIFY_OPEN_BAD_PEER_AS,
758 notify_data_remote_as4, 4);
759 return -1;
760 }
761
762 if (!as4 && BGP_DEBUG(as4, AS4))
763 zlog_debug(
764 "%s [AS4] OPEN remote_as is AS_TRANS, but no AS4."
765 " Odd, but proceeding.",
766 peer->host);
767 else if (as4 < BGP_AS_MAX && BGP_DEBUG(as4, AS4))
768 zlog_debug(
769 "%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits "
770 "in 2-bytes, very odd peer.",
771 peer->host, as4);
772 if (as4)
773 remote_as = as4;
774 } else {
775 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX
776 */
777 /* If we have got the capability, peer->as4cap must match
778 * remote_as */
779 if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV)
780 && as4 != remote_as) {
781 /* raise error, log this, close session */
782 zlog_err(
783 "%s bad OPEN, got AS4 capability, but remote_as %u"
784 " mismatch with 16bit 'myasn' %u in open",
785 peer->host, as4, remote_as);
786 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
787 BGP_NOTIFY_OPEN_BAD_PEER_AS,
788 notify_data_remote_as4, 4);
789 return -1;
790 }
791 }
792
793 /* remote router-id check. */
794 if (remote_id.s_addr == 0 || IPV4_CLASS_DE(ntohl(remote_id.s_addr))
795 || ntohl(peer->local_id.s_addr) == ntohl(remote_id.s_addr)) {
796 if (bgp_debug_neighbor_events(peer))
797 zlog_debug("%s bad OPEN, wrong router identifier %s",
798 peer->host, inet_ntoa(remote_id));
799 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
800 BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
801 notify_data_remote_id, 4);
802 return -1;
803 }
804
805 /* Set remote router-id */
806 peer->remote_id = remote_id;
807
808 /* Peer BGP version check. */
809 if (version != BGP_VERSION_4) {
810 u_int16_t maxver = htons(BGP_VERSION_4);
811 /* XXX this reply may not be correct if version < 4 XXX */
812 if (bgp_debug_neighbor_events(peer))
813 zlog_debug(
814 "%s bad protocol version, remote requested %d, local request %d",
815 peer->host, version, BGP_VERSION_4);
816 /* Data must be in network byte order here */
817 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
818 BGP_NOTIFY_OPEN_UNSUP_VERSION,
819 (u_int8_t *)&maxver, 2);
820 return -1;
821 }
822
823 /* Check neighbor as number. */
824 if (peer->as_type == AS_UNSPECIFIED) {
825 if (bgp_debug_neighbor_events(peer))
826 zlog_debug(
827 "%s bad OPEN, remote AS is unspecified currently",
828 peer->host);
829 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
830 BGP_NOTIFY_OPEN_BAD_PEER_AS,
831 notify_data_remote_as, 2);
832 return -1;
833 } else if (peer->as_type == AS_INTERNAL) {
834 if (remote_as != peer->bgp->as) {
835 if (bgp_debug_neighbor_events(peer))
836 zlog_debug(
837 "%s bad OPEN, remote AS is %u, internal specified",
838 peer->host, remote_as);
839 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
840 BGP_NOTIFY_OPEN_BAD_PEER_AS,
841 notify_data_remote_as, 2);
842 return -1;
843 }
844 peer->as = peer->local_as;
845 } else if (peer->as_type == AS_EXTERNAL) {
846 if (remote_as == peer->bgp->as) {
847 if (bgp_debug_neighbor_events(peer))
848 zlog_debug(
849 "%s bad OPEN, remote AS is %u, external specified",
850 peer->host, remote_as);
851 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
852 BGP_NOTIFY_OPEN_BAD_PEER_AS,
853 notify_data_remote_as, 2);
854 return -1;
855 }
856 peer->as = remote_as;
857 } else if ((peer->as_type == AS_SPECIFIED) && (remote_as != peer->as)) {
858 if (bgp_debug_neighbor_events(peer))
859 zlog_debug("%s bad OPEN, remote AS is %u, expected %u",
860 peer->host, remote_as, peer->as);
861 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
862 BGP_NOTIFY_OPEN_BAD_PEER_AS,
863 notify_data_remote_as, 2);
864 return -1;
865 }
866
867 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
868 calculate the value of the Hold Timer by using the smaller of its
869 configured Hold Time and the Hold Time received in the OPEN message.
870 The Hold Time MUST be either zero or at least three seconds. An
871 implementation may reject connections on the basis of the Hold Time.
872 */
873
874 if (holdtime < 3 && holdtime != 0) {
875 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
876 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME,
877 (u_char *)holdtime_ptr, 2);
878 return -1;
879 }
880
881 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
882 would be one third of the Hold Time interval. KEEPALIVE messages
883 MUST NOT be sent more frequently than one per second. An
884 implementation MAY adjust the rate at which it sends KEEPALIVE
885 messages as a function of the Hold Time interval. */
886
887 if (PEER_OR_GROUP_TIMER_SET(peer))
888 send_holdtime = peer->holdtime;
889 else
890 send_holdtime = peer->bgp->default_holdtime;
891
892 if (holdtime < send_holdtime)
893 peer->v_holdtime = holdtime;
894 else
895 peer->v_holdtime = send_holdtime;
896
897 if ((PEER_OR_GROUP_TIMER_SET(peer))
898 && (peer->keepalive < peer->v_holdtime / 3))
899 peer->v_keepalive = peer->keepalive;
900 else
901 peer->v_keepalive = peer->v_holdtime / 3;
902
903 /* Open option part parse. */
904 if (optlen != 0) {
905 if ((ret = bgp_open_option_parse(peer, optlen, &mp_capability))
906 < 0)
907 return ret;
908 } else {
909 if (bgp_debug_neighbor_events(peer))
910 zlog_debug("%s rcvd OPEN w/ OPTION parameter len: 0",
911 peer->host);
912 }
913
914 /*
915 * Assume that the peer supports the locally configured set of
916 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
917 * capabilities, or if 'override-capability' is configured.
918 */
919 if (!mp_capability
920 || CHECK_FLAG(peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) {
921 peer->afc_nego[AFI_IP][SAFI_UNICAST] =
922 peer->afc[AFI_IP][SAFI_UNICAST];
923 peer->afc_nego[AFI_IP][SAFI_MULTICAST] =
924 peer->afc[AFI_IP][SAFI_MULTICAST];
925 peer->afc_nego[AFI_IP][SAFI_LABELED_UNICAST] =
926 peer->afc[AFI_IP][SAFI_LABELED_UNICAST];
927 peer->afc_nego[AFI_IP6][SAFI_UNICAST] =
928 peer->afc[AFI_IP6][SAFI_UNICAST];
929 peer->afc_nego[AFI_IP6][SAFI_MULTICAST] =
930 peer->afc[AFI_IP6][SAFI_MULTICAST];
931 peer->afc_nego[AFI_IP6][SAFI_LABELED_UNICAST] =
932 peer->afc[AFI_IP6][SAFI_LABELED_UNICAST];
933 peer->afc_nego[AFI_L2VPN][SAFI_EVPN] =
934 peer->afc[AFI_L2VPN][SAFI_EVPN];
935 }
936
937 /* When collision is detected and this peer is closed. Retrun
938 immidiately. */
939 ret = bgp_collision_detect(peer, remote_id);
940 if (ret < 0)
941 return ret;
942
943 /* Get sockname. */
944 if ((ret = bgp_getsockname(peer)) < 0) {
945 zlog_err("%s: bgp_getsockname() failed for peer: %s",
946 __FUNCTION__, peer->host);
947 return (ret);
948 }
949
950 /* Verify valid local address present based on negotiated
951 * address-families. */
952 if (peer->afc_nego[AFI_IP][SAFI_UNICAST]
953 || peer->afc_nego[AFI_IP][SAFI_LABELED_UNICAST]
954 || peer->afc_nego[AFI_IP][SAFI_MULTICAST]
955 || peer->afc_nego[AFI_IP][SAFI_MPLS_VPN]
956 || peer->afc_nego[AFI_IP][SAFI_ENCAP]) {
957 if (!peer->nexthop.v4.s_addr) {
958 #if defined(HAVE_CUMULUS)
959 zlog_err(
960 "%s: No local IPv4 addr resetting connection, fd %d",
961 peer->host, peer->fd);
962 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
963 BGP_NOTIFY_SUBCODE_UNSPECIFIC);
964 return -1;
965 #endif
966 }
967 }
968 if (peer->afc_nego[AFI_IP6][SAFI_UNICAST]
969 || peer->afc_nego[AFI_IP6][SAFI_LABELED_UNICAST]
970 || peer->afc_nego[AFI_IP6][SAFI_MULTICAST]
971 || peer->afc_nego[AFI_IP6][SAFI_MPLS_VPN]
972 || peer->afc_nego[AFI_IP6][SAFI_ENCAP]) {
973 if (IN6_IS_ADDR_UNSPECIFIED(&peer->nexthop.v6_global)) {
974 #if defined(HAVE_CUMULUS)
975 zlog_err(
976 "%s: No local IPv6 addr resetting connection, fd %d",
977 peer->host, peer->fd);
978 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
979 BGP_NOTIFY_SUBCODE_UNSPECIFIC);
980 return -1;
981 #endif
982 }
983 }
984 peer->rtt = sockopt_tcp_rtt(peer->fd);
985
986 if ((ret = bgp_event_update(peer, Receive_OPEN_message)) < 0) {
987 zlog_err("%s: BGP event update failed for peer: %s",
988 __FUNCTION__, peer->host);
989 /* DD: bgp send notify and reset state */
990 return (ret);
991 }
992
993 peer->packet_size = 0;
994 if (peer->ibuf)
995 stream_reset(peer->ibuf);
996
997 return 0;
998 }
999
1000 /* Called when there is a change in the EOR(implicit or explicit) status of a
1001 peer.
1002 Ends the update-delay if all expected peers are done with EORs. */
1003 void bgp_check_update_delay(struct bgp *bgp)
1004 {
1005 struct listnode *node, *nnode;
1006 struct peer *peer = NULL;
1007
1008 if (bgp_debug_neighbor_events(peer))
1009 zlog_debug("Checking update delay, T: %d R: %d I:%d E: %d",
1010 bgp->established, bgp->restarted_peers,
1011 bgp->implicit_eors, bgp->explicit_eors);
1012
1013 if (bgp->established
1014 <= bgp->restarted_peers + bgp->implicit_eors + bgp->explicit_eors) {
1015 /* This is an extra sanity check to make sure we wait for all
1016 the
1017 eligible configured peers. This check is performed if
1018 establish wait
1019 timer is on, or establish wait option is not given with the
1020 update-delay command */
1021 if (bgp->t_establish_wait
1022 || (bgp->v_establish_wait == bgp->v_update_delay))
1023 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
1024 if (CHECK_FLAG(peer->flags,
1025 PEER_FLAG_CONFIG_NODE)
1026 && !CHECK_FLAG(peer->flags,
1027 PEER_FLAG_SHUTDOWN)
1028 && !peer->update_delay_over) {
1029 if (bgp_debug_neighbor_events(peer))
1030 zlog_debug(
1031 " Peer %s pending, continuing read-only mode",
1032 peer->host);
1033 return;
1034 }
1035 }
1036
1037 zlog_info(
1038 "Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d",
1039 bgp->restarted_peers, bgp->implicit_eors,
1040 bgp->explicit_eors);
1041 bgp_update_delay_end(bgp);
1042 }
1043 }
1044
1045 /* Called if peer is known to have restarted. The restart-state bit in
1046 Graceful-Restart capability is used for that */
1047 void bgp_update_restarted_peers(struct peer *peer)
1048 {
1049 if (!bgp_update_delay_active(peer->bgp))
1050 return; /* BGP update delay has ended */
1051 if (peer->update_delay_over)
1052 return; /* This peer has already been considered */
1053
1054 if (bgp_debug_neighbor_events(peer))
1055 zlog_debug("Peer %s: Checking restarted", peer->host);
1056
1057 if (peer->status == Established) {
1058 peer->update_delay_over = 1;
1059 peer->bgp->restarted_peers++;
1060 bgp_check_update_delay(peer->bgp);
1061 }
1062 }
1063
1064 /* Called as peer receives a keep-alive. Determines if this occurence can be
1065 taken as an implicit EOR for this peer.
1066 NOTE: The very first keep-alive after the Established state of a peer is
1067 considered implicit EOR for the update-delay purposes */
1068 void bgp_update_implicit_eors(struct peer *peer)
1069 {
1070 if (!bgp_update_delay_active(peer->bgp))
1071 return; /* BGP update delay has ended */
1072 if (peer->update_delay_over)
1073 return; /* This peer has already been considered */
1074
1075 if (bgp_debug_neighbor_events(peer))
1076 zlog_debug("Peer %s: Checking implicit EORs", peer->host);
1077
1078 if (peer->status == Established) {
1079 peer->update_delay_over = 1;
1080 peer->bgp->implicit_eors++;
1081 bgp_check_update_delay(peer->bgp);
1082 }
1083 }
1084
1085 /* Should be called only when there is a change in the EOR_RECEIVED status
1086 for any afi/safi on a peer */
1087 static void bgp_update_explicit_eors(struct peer *peer)
1088 {
1089 afi_t afi;
1090 safi_t safi;
1091
1092 if (!bgp_update_delay_active(peer->bgp))
1093 return; /* BGP update delay has ended */
1094 if (peer->update_delay_over)
1095 return; /* This peer has already been considered */
1096
1097 if (bgp_debug_neighbor_events(peer))
1098 zlog_debug("Peer %s: Checking explicit EORs", peer->host);
1099
1100 FOREACH_AFI_SAFI (afi, safi) {
1101 if (peer->afc_nego[afi][safi]
1102 && !CHECK_FLAG(peer->af_sflags[afi][safi],
1103 PEER_STATUS_EOR_RECEIVED)) {
1104 if (bgp_debug_neighbor_events(peer))
1105 zlog_debug(
1106 " afi %d safi %d didnt receive EOR",
1107 afi, safi);
1108 return;
1109 }
1110 }
1111
1112 peer->update_delay_over = 1;
1113 peer->bgp->explicit_eors++;
1114 bgp_check_update_delay(peer->bgp);
1115 }
1116
1117 /* Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers
1118 * mp_withdraw, if set, is used to nullify attr structure on most of the calling
1119 * safi function
1120 * and for evpn, passed as parameter
1121 */
1122 int bgp_nlri_parse(struct peer *peer, struct attr *attr,
1123 struct bgp_nlri *packet, int mp_withdraw)
1124 {
1125 switch (packet->safi) {
1126 case SAFI_UNICAST:
1127 case SAFI_MULTICAST:
1128 return bgp_nlri_parse_ip(peer, mp_withdraw ? NULL : attr,
1129 packet);
1130 case SAFI_LABELED_UNICAST:
1131 return bgp_nlri_parse_label(peer, mp_withdraw ? NULL : attr,
1132 packet);
1133 case SAFI_MPLS_VPN:
1134 return bgp_nlri_parse_vpn(peer, mp_withdraw ? NULL : attr,
1135 packet);
1136 case SAFI_EVPN:
1137 return bgp_nlri_parse_evpn(peer, attr, packet, mp_withdraw);
1138 default:
1139 return -1;
1140 }
1141 }
1142
1143 /* Parse BGP Update packet and make attribute object. */
1144 static int bgp_update_receive(struct peer *peer, bgp_size_t size)
1145 {
1146 int ret, nlri_ret;
1147 u_char *end;
1148 struct stream *s;
1149 struct attr attr;
1150 bgp_size_t attribute_len;
1151 bgp_size_t update_len;
1152 bgp_size_t withdraw_len;
1153
1154 enum NLRI_TYPES {
1155 NLRI_UPDATE,
1156 NLRI_WITHDRAW,
1157 NLRI_MP_UPDATE,
1158 NLRI_MP_WITHDRAW,
1159 NLRI_TYPE_MAX
1160 };
1161 struct bgp_nlri nlris[NLRI_TYPE_MAX];
1162
1163 /* Status must be Established. */
1164 if (peer->status != Established) {
1165 zlog_err("%s [FSM] Update packet received under status %s",
1166 peer->host,
1167 lookup_msg(bgp_status_msg, peer->status, NULL));
1168 bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR, 0);
1169 return -1;
1170 }
1171
1172 /* Set initial values. */
1173 memset(&attr, 0, sizeof(struct attr));
1174 attr.label_index = BGP_INVALID_LABEL_INDEX;
1175 attr.label = MPLS_INVALID_LABEL;
1176 memset(&nlris, 0, sizeof(nlris));
1177 memset(peer->rcvd_attr_str, 0, BUFSIZ);
1178 peer->rcvd_attr_printed = 0;
1179
1180 s = peer->ibuf;
1181 end = stream_pnt(s) + size;
1182
1183 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1184 Length is too large (i.e., if Unfeasible Routes Length + Total
1185 Attribute Length + 23 exceeds the message Length), then the Error
1186 Subcode is set to Malformed Attribute List. */
1187 if (stream_pnt(s) + 2 > end) {
1188 zlog_err(
1189 "%s [Error] Update packet error"
1190 " (packet length is short for unfeasible length)",
1191 peer->host);
1192 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
1193 BGP_NOTIFY_UPDATE_MAL_ATTR);
1194 return -1;
1195 }
1196
1197 /* Unfeasible Route Length. */
1198 withdraw_len = stream_getw(s);
1199
1200 /* Unfeasible Route Length check. */
1201 if (stream_pnt(s) + withdraw_len > end) {
1202 zlog_err(
1203 "%s [Error] Update packet error"
1204 " (packet unfeasible length overflow %d)",
1205 peer->host, withdraw_len);
1206 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
1207 BGP_NOTIFY_UPDATE_MAL_ATTR);
1208 return -1;
1209 }
1210
1211 /* Unfeasible Route packet format check. */
1212 if (withdraw_len > 0) {
1213 nlris[NLRI_WITHDRAW].afi = AFI_IP;
1214 nlris[NLRI_WITHDRAW].safi = SAFI_UNICAST;
1215 nlris[NLRI_WITHDRAW].nlri = stream_pnt(s);
1216 nlris[NLRI_WITHDRAW].length = withdraw_len;
1217 stream_forward_getp(s, withdraw_len);
1218 }
1219
1220 /* Attribute total length check. */
1221 if (stream_pnt(s) + 2 > end) {
1222 zlog_warn(
1223 "%s [Error] Packet Error"
1224 " (update packet is short for attribute length)",
1225 peer->host);
1226 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
1227 BGP_NOTIFY_UPDATE_MAL_ATTR);
1228 return -1;
1229 }
1230
1231 /* Fetch attribute total length. */
1232 attribute_len = stream_getw(s);
1233
1234 /* Attribute length check. */
1235 if (stream_pnt(s) + attribute_len > end) {
1236 zlog_warn(
1237 "%s [Error] Packet Error"
1238 " (update packet attribute length overflow %d)",
1239 peer->host, attribute_len);
1240 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
1241 BGP_NOTIFY_UPDATE_MAL_ATTR);
1242 return -1;
1243 }
1244
1245 /* Certain attribute parsing errors should not be considered bad enough
1246 * to reset the session for, most particularly any partial/optional
1247 * attributes that have 'tunneled' over speakers that don't understand
1248 * them. Instead we withdraw only the prefix concerned.
1249 *
1250 * Complicates the flow a little though..
1251 */
1252 bgp_attr_parse_ret_t attr_parse_ret = BGP_ATTR_PARSE_PROCEED;
1253 /* This define morphs the update case into a withdraw when lower levels
1254 * have signalled an error condition where this is best.
1255 */
1256 #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
1257
1258 /* Parse attribute when it exists. */
1259 if (attribute_len) {
1260 attr_parse_ret = bgp_attr_parse(peer, &attr, attribute_len,
1261 &nlris[NLRI_MP_UPDATE],
1262 &nlris[NLRI_MP_WITHDRAW]);
1263 if (attr_parse_ret == BGP_ATTR_PARSE_ERROR) {
1264 bgp_attr_unintern_sub(&attr);
1265 return -1;
1266 }
1267 }
1268
1269 /* Logging the attribute. */
1270 if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW
1271 || BGP_DEBUG(update, UPDATE_IN)
1272 || BGP_DEBUG(update, UPDATE_PREFIX)) {
1273 ret = bgp_dump_attr(&attr, peer->rcvd_attr_str, BUFSIZ);
1274
1275 if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW)
1276 zlog_err(
1277 "%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1278 peer->host);
1279
1280 if (ret && bgp_debug_update(peer, NULL, NULL, 1)) {
1281 zlog_debug("%s rcvd UPDATE w/ attr: %s", peer->host,
1282 peer->rcvd_attr_str);
1283 peer->rcvd_attr_printed = 1;
1284 }
1285 }
1286
1287 /* Network Layer Reachability Information. */
1288 update_len = end - stream_pnt(s);
1289
1290 if (update_len) {
1291 /* Set NLRI portion to structure. */
1292 nlris[NLRI_UPDATE].afi = AFI_IP;
1293 nlris[NLRI_UPDATE].safi = SAFI_UNICAST;
1294 nlris[NLRI_UPDATE].nlri = stream_pnt(s);
1295 nlris[NLRI_UPDATE].length = update_len;
1296 stream_forward_getp(s, update_len);
1297 }
1298
1299 if (BGP_DEBUG(update, UPDATE_IN))
1300 zlog_debug("%s rcvd UPDATE wlen %d attrlen %d alen %d",
1301 peer->host, withdraw_len, attribute_len, update_len);
1302
1303 /* Parse any given NLRIs */
1304 for (int i = NLRI_UPDATE; i < NLRI_TYPE_MAX; i++) {
1305 if (!nlris[i].nlri)
1306 continue;
1307
1308 /* NLRI is processed iff the peer if configured for the specific
1309 * afi/safi */
1310 if (!peer->afc[nlris[i].afi][nlris[i].safi]) {
1311 zlog_info(
1312 "%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u",
1313 peer->host, nlris[i].afi, nlris[i].safi);
1314 continue;
1315 }
1316
1317 /* EoR handled later */
1318 if (nlris[i].length == 0)
1319 continue;
1320
1321 switch (i) {
1322 case NLRI_UPDATE:
1323 case NLRI_MP_UPDATE:
1324 nlri_ret = bgp_nlri_parse(peer, NLRI_ATTR_ARG,
1325 &nlris[i], 0);
1326 break;
1327 case NLRI_WITHDRAW:
1328 case NLRI_MP_WITHDRAW:
1329 nlri_ret = bgp_nlri_parse(peer, &attr, &nlris[i], 1);
1330 break;
1331 default:
1332 nlri_ret = -1;
1333 }
1334
1335 if (nlri_ret < 0) {
1336 zlog_err("%s [Error] Error parsing NLRI", peer->host);
1337 if (peer->status == Established)
1338 bgp_notify_send(
1339 peer, BGP_NOTIFY_UPDATE_ERR,
1340 i <= NLRI_WITHDRAW
1341 ? BGP_NOTIFY_UPDATE_INVAL_NETWORK
1342 : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR);
1343 bgp_attr_unintern_sub(&attr);
1344 return -1;
1345 }
1346 }
1347
1348 /* EoR checks
1349 *
1350 * Non-MP IPv4/Unicast EoR is a completely empty UPDATE
1351 * and MP EoR should have only an empty MP_UNREACH
1352 */
1353 if ((!update_len && !withdraw_len &&
1354 nlris[NLRI_MP_UPDATE].length == 0) ||
1355 (attr_parse_ret == BGP_ATTR_PARSE_EOR)) {
1356 afi_t afi = 0;
1357 safi_t safi;
1358
1359 /* Non-MP IPv4/Unicast is a completely emtpy UPDATE - already
1360 * checked
1361 * update and withdraw NLRI lengths are 0.
1362 */
1363 if (!attribute_len) {
1364 afi = AFI_IP;
1365 safi = SAFI_UNICAST;
1366 } else if (attr.flag & ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI)
1367 && nlris[NLRI_MP_WITHDRAW].length == 0) {
1368 afi = nlris[NLRI_MP_WITHDRAW].afi;
1369 safi = nlris[NLRI_MP_WITHDRAW].safi;
1370 } else if (attr_parse_ret == BGP_ATTR_PARSE_EOR) {
1371 afi = nlris[NLRI_MP_UPDATE].afi;
1372 safi = nlris[NLRI_MP_UPDATE].safi;
1373 }
1374
1375 if (afi && peer->afc[afi][safi]) {
1376 /* End-of-RIB received */
1377 if (!CHECK_FLAG(peer->af_sflags[afi][safi],
1378 PEER_STATUS_EOR_RECEIVED)) {
1379 SET_FLAG(peer->af_sflags[afi][safi],
1380 PEER_STATUS_EOR_RECEIVED);
1381 bgp_update_explicit_eors(peer);
1382 }
1383
1384 /* NSF delete stale route */
1385 if (peer->nsf[afi][safi])
1386 bgp_clear_stale_route(peer, afi, safi);
1387
1388 if (bgp_debug_neighbor_events(peer)) {
1389 zlog_debug("rcvd End-of-RIB for %s from %s",
1390 afi_safi_print(afi, safi),
1391 peer->host);
1392 }
1393 }
1394 }
1395
1396 /* Everything is done. We unintern temporary structures which
1397 interned in bgp_attr_parse(). */
1398 bgp_attr_unintern_sub(&attr);
1399
1400 /* If peering is stopped due to some reason, do not generate BGP
1401 event. */
1402 if (peer->status != Established)
1403 return 0;
1404
1405 /* Increment packet counter. */
1406 peer->update_in++;
1407 peer->update_time = bgp_clock();
1408
1409 /* Rearm holdtime timer */
1410 BGP_TIMER_OFF(peer->t_holdtime);
1411 bgp_timer_set(peer);
1412
1413 return 0;
1414 }
1415
1416 /* Notify message treatment function. */
1417 static void bgp_notify_receive(struct peer *peer, bgp_size_t size)
1418 {
1419 struct bgp_notify bgp_notify;
1420
1421 if (peer->notify.data) {
1422 XFREE(MTYPE_TMP, peer->notify.data);
1423 peer->notify.data = NULL;
1424 peer->notify.length = 0;
1425 }
1426
1427 bgp_notify.code = stream_getc(peer->ibuf);
1428 bgp_notify.subcode = stream_getc(peer->ibuf);
1429 bgp_notify.length = size - 2;
1430 bgp_notify.data = NULL;
1431
1432 /* Preserv notify code and sub code. */
1433 peer->notify.code = bgp_notify.code;
1434 peer->notify.subcode = bgp_notify.subcode;
1435 /* For further diagnostic record returned Data. */
1436 if (bgp_notify.length) {
1437 peer->notify.length = size - 2;
1438 peer->notify.data = XMALLOC(MTYPE_TMP, size - 2);
1439 memcpy(peer->notify.data, stream_pnt(peer->ibuf), size - 2);
1440 }
1441
1442 /* For debug */
1443 {
1444 int i;
1445 int first = 0;
1446 char c[4];
1447
1448 if (bgp_notify.length) {
1449 bgp_notify.data =
1450 XMALLOC(MTYPE_TMP, bgp_notify.length * 3);
1451 for (i = 0; i < bgp_notify.length; i++)
1452 if (first) {
1453 sprintf(c, " %02x",
1454 stream_getc(peer->ibuf));
1455 strcat(bgp_notify.data, c);
1456 } else {
1457 first = 1;
1458 sprintf(c, "%02x",
1459 stream_getc(peer->ibuf));
1460 strcpy(bgp_notify.data, c);
1461 }
1462 bgp_notify.raw_data = (u_char *)peer->notify.data;
1463 }
1464
1465 bgp_notify_print(peer, &bgp_notify, "received");
1466 if (bgp_notify.data) {
1467 XFREE(MTYPE_TMP, bgp_notify.data);
1468 bgp_notify.data = NULL;
1469 bgp_notify.length = 0;
1470 }
1471 }
1472
1473 /* peer count update */
1474 peer->notify_in++;
1475
1476 peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED;
1477
1478 /* We have to check for Notify with Unsupported Optional Parameter.
1479 in that case we fallback to open without the capability option.
1480 But this done in bgp_stop. We just mark it here to avoid changing
1481 the fsm tables. */
1482 if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR
1483 && bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM)
1484 UNSET_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
1485
1486 BGP_EVENT_ADD(peer, Receive_NOTIFICATION_message);
1487 }
1488
1489 /* Keepalive treatment function -- get keepalive send keepalive */
1490 static void bgp_keepalive_receive(struct peer *peer, bgp_size_t size)
1491 {
1492 if (bgp_debug_keepalive(peer))
1493 zlog_debug("%s KEEPALIVE rcvd", peer->host);
1494
1495 BGP_EVENT_ADD(peer, Receive_KEEPALIVE_message);
1496 }
1497
1498 /* Route refresh message is received. */
1499 static void bgp_route_refresh_receive(struct peer *peer, bgp_size_t size)
1500 {
1501 iana_afi_t pkt_afi;
1502 afi_t afi;
1503 iana_safi_t pkt_safi;
1504 safi_t safi;
1505 struct stream *s;
1506 struct peer_af *paf;
1507 struct update_group *updgrp;
1508 struct peer *updgrp_peer;
1509
1510 /* If peer does not have the capability, send notification. */
1511 if (!CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_ADV)) {
1512 zlog_err("%s [Error] BGP route refresh is not enabled",
1513 peer->host);
1514 bgp_notify_send(peer, BGP_NOTIFY_HEADER_ERR,
1515 BGP_NOTIFY_HEADER_BAD_MESTYPE);
1516 return;
1517 }
1518
1519 /* Status must be Established. */
1520 if (peer->status != Established) {
1521 zlog_err(
1522 "%s [Error] Route refresh packet received under status %s",
1523 peer->host,
1524 lookup_msg(bgp_status_msg, peer->status, NULL));
1525 bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR, 0);
1526 return;
1527 }
1528
1529 s = peer->ibuf;
1530
1531 /* Parse packet. */
1532 pkt_afi = stream_getw(s);
1533 (void)stream_getc(s);
1534 pkt_safi = stream_getc(s);
1535
1536 if (bgp_debug_update(peer, NULL, NULL, 0))
1537 zlog_debug("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
1538 peer->host, pkt_afi, pkt_safi);
1539
1540 /* Convert AFI, SAFI to internal values and check. */
1541 if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi, &safi)) {
1542 zlog_info(
1543 "%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
1544 peer->host, pkt_afi, pkt_safi);
1545 return;
1546 }
1547
1548 if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) {
1549 u_char *end;
1550 u_char when_to_refresh;
1551 u_char orf_type;
1552 u_int16_t orf_len;
1553
1554 if (size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE)
1555 < 5) {
1556 zlog_info("%s ORF route refresh length error",
1557 peer->host);
1558 bgp_notify_send(peer, BGP_NOTIFY_CEASE, 0);
1559 return;
1560 }
1561
1562 when_to_refresh = stream_getc(s);
1563 end = stream_pnt(s) + (size - 5);
1564
1565 while ((stream_pnt(s) + 2) < end) {
1566 orf_type = stream_getc(s);
1567 orf_len = stream_getw(s);
1568
1569 /* orf_len in bounds? */
1570 if ((stream_pnt(s) + orf_len) > end)
1571 break; /* XXX: Notify instead?? */
1572 if (orf_type == ORF_TYPE_PREFIX
1573 || orf_type == ORF_TYPE_PREFIX_OLD) {
1574 uint8_t *p_pnt = stream_pnt(s);
1575 uint8_t *p_end = stream_pnt(s) + orf_len;
1576 struct orf_prefix orfp;
1577 u_char common = 0;
1578 u_int32_t seq;
1579 int psize;
1580 char name[BUFSIZ];
1581 int ret = CMD_SUCCESS;
1582
1583 if (bgp_debug_neighbor_events(peer)) {
1584 zlog_debug(
1585 "%s rcvd Prefixlist ORF(%d) length %d",
1586 peer->host, orf_type, orf_len);
1587 }
1588
1589 /* we're going to read at least 1 byte of common
1590 * ORF header,
1591 * and 7 bytes of ORF Address-filter entry from
1592 * the stream
1593 */
1594 if (orf_len < 7)
1595 break;
1596
1597 /* ORF prefix-list name */
1598 sprintf(name, "%s.%d.%d", peer->host, afi,
1599 safi);
1600
1601 while (p_pnt < p_end) {
1602 /* If the ORF entry is malformed, want
1603 * to read as much of it
1604 * as possible without going beyond the
1605 * bounds of the entry,
1606 * to maximise debug information.
1607 */
1608 int ok;
1609 memset(&orfp, 0,
1610 sizeof(struct orf_prefix));
1611 common = *p_pnt++;
1612 /* after ++: p_pnt <= p_end */
1613 if (common
1614 & ORF_COMMON_PART_REMOVE_ALL) {
1615 if (bgp_debug_neighbor_events(
1616 peer))
1617 zlog_debug(
1618 "%s rcvd Remove-All pfxlist ORF request",
1619 peer->host);
1620 prefix_bgp_orf_remove_all(afi,
1621 name);
1622 break;
1623 }
1624 ok = ((u_int32_t)(p_end - p_pnt)
1625 >= sizeof(u_int32_t));
1626 if (ok) {
1627 memcpy(&seq, p_pnt,
1628 sizeof(u_int32_t));
1629 p_pnt += sizeof(u_int32_t);
1630 orfp.seq = ntohl(seq);
1631 } else
1632 p_pnt = p_end;
1633
1634 if ((ok = (p_pnt < p_end)))
1635 orfp.ge =
1636 *p_pnt++; /* value
1637 checked in
1638 prefix_bgp_orf_set()
1639 */
1640 if ((ok = (p_pnt < p_end)))
1641 orfp.le =
1642 *p_pnt++; /* value
1643 checked in
1644 prefix_bgp_orf_set()
1645 */
1646 if ((ok = (p_pnt < p_end)))
1647 orfp.p.prefixlen = *p_pnt++;
1648 orfp.p.family = afi2family(
1649 afi); /* afi checked already */
1650
1651 psize = PSIZE(
1652 orfp.p.prefixlen); /* 0 if not
1653 ok */
1654 if (psize
1655 > prefix_blen(
1656 &orfp.p)) /* valid for
1657 family ? */
1658 {
1659 ok = 0;
1660 psize = prefix_blen(&orfp.p);
1661 }
1662 if (psize
1663 > (p_end - p_pnt)) /* valid for
1664 packet ? */
1665 {
1666 ok = 0;
1667 psize = p_end - p_pnt;
1668 }
1669
1670 if (psize > 0)
1671 memcpy(&orfp.p.u.prefix, p_pnt,
1672 psize);
1673 p_pnt += psize;
1674
1675 if (bgp_debug_neighbor_events(peer)) {
1676 char buf[INET6_BUFSIZ];
1677
1678 zlog_debug(
1679 "%s rcvd %s %s seq %u %s/%d ge %d le %d%s",
1680 peer->host,
1681 (common & ORF_COMMON_PART_REMOVE
1682 ? "Remove"
1683 : "Add"),
1684 (common & ORF_COMMON_PART_DENY
1685 ? "deny"
1686 : "permit"),
1687 orfp.seq,
1688 inet_ntop(
1689 orfp.p.family,
1690 &orfp.p.u.prefix,
1691 buf,
1692 INET6_BUFSIZ),
1693 orfp.p.prefixlen,
1694 orfp.ge, orfp.le,
1695 ok ? "" : " MALFORMED");
1696 }
1697
1698 if (ok)
1699 ret = prefix_bgp_orf_set(
1700 name, afi, &orfp,
1701 (common & ORF_COMMON_PART_DENY
1702 ? 0
1703 : 1),
1704 (common & ORF_COMMON_PART_REMOVE
1705 ? 0
1706 : 1));
1707
1708 if (!ok || (ok && ret != CMD_SUCCESS)) {
1709 zlog_info(
1710 "%s Received misformatted prefixlist ORF."
1711 " Remove All pfxlist",
1712 peer->host);
1713 prefix_bgp_orf_remove_all(afi,
1714 name);
1715 break;
1716 }
1717 }
1718
1719 peer->orf_plist[afi][safi] =
1720 prefix_bgp_orf_lookup(afi, name);
1721 }
1722 stream_forward_getp(s, orf_len);
1723 }
1724 if (bgp_debug_neighbor_events(peer))
1725 zlog_debug("%s rcvd Refresh %s ORF request", peer->host,
1726 when_to_refresh == REFRESH_DEFER
1727 ? "Defer"
1728 : "Immediate");
1729 if (when_to_refresh == REFRESH_DEFER)
1730 return;
1731 }
1732
1733 /* First update is deferred until ORF or ROUTE-REFRESH is received */
1734 if (CHECK_FLAG(peer->af_sflags[afi][safi],
1735 PEER_STATUS_ORF_WAIT_REFRESH))
1736 UNSET_FLAG(peer->af_sflags[afi][safi],
1737 PEER_STATUS_ORF_WAIT_REFRESH);
1738
1739 paf = peer_af_find(peer, afi, safi);
1740 if (paf && paf->subgroup) {
1741 if (peer->orf_plist[afi][safi]) {
1742 updgrp = PAF_UPDGRP(paf);
1743 updgrp_peer = UPDGRP_PEER(updgrp);
1744 updgrp_peer->orf_plist[afi][safi] =
1745 peer->orf_plist[afi][safi];
1746 }
1747
1748 /* If the peer is configured for default-originate clear the
1749 * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will
1750 * re-advertise the
1751 * default
1752 */
1753 if (CHECK_FLAG(paf->subgroup->sflags,
1754 SUBGRP_STATUS_DEFAULT_ORIGINATE))
1755 UNSET_FLAG(paf->subgroup->sflags,
1756 SUBGRP_STATUS_DEFAULT_ORIGINATE);
1757 }
1758
1759 /* Perform route refreshment to the peer */
1760 bgp_announce_route(peer, afi, safi);
1761 }
1762
1763 static int bgp_capability_msg_parse(struct peer *peer, u_char *pnt,
1764 bgp_size_t length)
1765 {
1766 u_char *end;
1767 struct capability_mp_data mpc;
1768 struct capability_header *hdr;
1769 u_char action;
1770 iana_afi_t pkt_afi;
1771 afi_t afi;
1772 iana_safi_t pkt_safi;
1773 safi_t safi;
1774
1775 end = pnt + length;
1776
1777 while (pnt < end) {
1778 /* We need at least action, capability code and capability
1779 * length. */
1780 if (pnt + 3 > end) {
1781 zlog_info("%s Capability length error", peer->host);
1782 bgp_notify_send(peer, BGP_NOTIFY_CEASE, 0);
1783 return -1;
1784 }
1785 action = *pnt;
1786 hdr = (struct capability_header *)(pnt + 1);
1787
1788 /* Action value check. */
1789 if (action != CAPABILITY_ACTION_SET
1790 && action != CAPABILITY_ACTION_UNSET) {
1791 zlog_info("%s Capability Action Value error %d",
1792 peer->host, action);
1793 bgp_notify_send(peer, BGP_NOTIFY_CEASE, 0);
1794 return -1;
1795 }
1796
1797 if (bgp_debug_neighbor_events(peer))
1798 zlog_debug(
1799 "%s CAPABILITY has action: %d, code: %u, length %u",
1800 peer->host, action, hdr->code, hdr->length);
1801
1802 /* Capability length check. */
1803 if ((pnt + hdr->length + 3) > end) {
1804 zlog_info("%s Capability length error", peer->host);
1805 bgp_notify_send(peer, BGP_NOTIFY_CEASE, 0);
1806 return -1;
1807 }
1808
1809 /* Fetch structure to the byte stream. */
1810 memcpy(&mpc, pnt + 3, sizeof(struct capability_mp_data));
1811 pnt += hdr->length + 3;
1812
1813 /* We know MP Capability Code. */
1814 if (hdr->code == CAPABILITY_CODE_MP) {
1815 pkt_afi = ntohs(mpc.afi);
1816 pkt_safi = mpc.safi;
1817
1818 /* Ignore capability when override-capability is set. */
1819 if (CHECK_FLAG(peer->flags,
1820 PEER_FLAG_OVERRIDE_CAPABILITY))
1821 continue;
1822
1823 /* Convert AFI, SAFI to internal values. */
1824 if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi,
1825 &safi)) {
1826 if (bgp_debug_neighbor_events(peer))
1827 zlog_debug(
1828 "%s Dynamic Capability MP_EXT afi/safi invalid "
1829 "(%u/%u)",
1830 peer->host, pkt_afi, pkt_safi);
1831 continue;
1832 }
1833
1834 /* Address family check. */
1835 if (bgp_debug_neighbor_events(peer))
1836 zlog_debug(
1837 "%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
1838 peer->host,
1839 action == CAPABILITY_ACTION_SET
1840 ? "Advertising"
1841 : "Removing",
1842 pkt_afi, pkt_safi);
1843
1844 if (action == CAPABILITY_ACTION_SET) {
1845 peer->afc_recv[afi][safi] = 1;
1846 if (peer->afc[afi][safi]) {
1847 peer->afc_nego[afi][safi] = 1;
1848 bgp_announce_route(peer, afi, safi);
1849 }
1850 } else {
1851 peer->afc_recv[afi][safi] = 0;
1852 peer->afc_nego[afi][safi] = 0;
1853
1854 if (peer_active_nego(peer))
1855 bgp_clear_route(peer, afi, safi);
1856 else
1857 BGP_EVENT_ADD(peer, BGP_Stop);
1858 }
1859 } else {
1860 zlog_warn(
1861 "%s unrecognized capability code: %d - ignored",
1862 peer->host, hdr->code);
1863 }
1864 }
1865 return 0;
1866 }
1867
1868 /* Dynamic Capability is received.
1869 *
1870 * This is exported for unit-test purposes
1871 */
1872 int bgp_capability_receive(struct peer *peer, bgp_size_t size)
1873 {
1874 u_char *pnt;
1875
1876 /* Fetch pointer. */
1877 pnt = stream_pnt(peer->ibuf);
1878
1879 if (bgp_debug_neighbor_events(peer))
1880 zlog_debug("%s rcv CAPABILITY", peer->host);
1881
1882 /* If peer does not have the capability, send notification. */
1883 if (!CHECK_FLAG(peer->cap, PEER_CAP_DYNAMIC_ADV)) {
1884 zlog_err("%s [Error] BGP dynamic capability is not enabled",
1885 peer->host);
1886 bgp_notify_send(peer, BGP_NOTIFY_HEADER_ERR,
1887 BGP_NOTIFY_HEADER_BAD_MESTYPE);
1888 return -1;
1889 }
1890
1891 /* Status must be Established. */
1892 if (peer->status != Established) {
1893 zlog_err(
1894 "%s [Error] Dynamic capability packet received under status %s",
1895 peer->host,
1896 lookup_msg(bgp_status_msg, peer->status, NULL));
1897 bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR, 0);
1898 return -1;
1899 }
1900
1901 /* Parse packet. */
1902 return bgp_capability_msg_parse(peer, pnt, size);
1903 }
1904
1905 /* BGP read utility function. */
1906 static int bgp_read_packet(struct peer *peer)
1907 {
1908 int nbytes;
1909 int readsize;
1910
1911 readsize = peer->packet_size - stream_get_endp(peer->ibuf);
1912
1913 /* If size is zero then return. */
1914 if (!readsize)
1915 return 0;
1916
1917 /* Read packet from fd. */
1918 nbytes = stream_read_try(peer->ibuf, peer->fd, readsize);
1919
1920 /* If read byte is smaller than zero then error occured. */
1921 if (nbytes < 0) {
1922 /* Transient error should retry */
1923 if (nbytes == -2)
1924 return -1;
1925
1926 zlog_err("%s [Error] bgp_read_packet error: %s", peer->host,
1927 safe_strerror(errno));
1928
1929 if (peer->status == Established) {
1930 if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_MODE)) {
1931 peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
1932 SET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
1933 } else
1934 peer->last_reset = PEER_DOWN_CLOSE_SESSION;
1935 }
1936
1937 BGP_EVENT_ADD(peer, TCP_fatal_error);
1938 return -1;
1939 }
1940
1941 /* When read byte is zero : clear bgp peer and return */
1942 if (nbytes == 0) {
1943 if (bgp_debug_neighbor_events(peer))
1944 zlog_debug("%s [Event] BGP connection closed fd %d",
1945 peer->host, peer->fd);
1946
1947 if (peer->status == Established) {
1948 if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_MODE)) {
1949 peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
1950 SET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
1951 } else
1952 peer->last_reset = PEER_DOWN_CLOSE_SESSION;
1953 }
1954
1955 BGP_EVENT_ADD(peer, TCP_connection_closed);
1956 return -1;
1957 }
1958
1959 /* We read partial packet. */
1960 if (stream_get_endp(peer->ibuf) != peer->packet_size)
1961 return -1;
1962
1963 return 0;
1964 }
1965
1966 /* Marker check. */
1967 static int bgp_marker_all_one(struct stream *s, int length)
1968 {
1969 int i;
1970
1971 for (i = 0; i < length; i++)
1972 if (s->data[i] != 0xff)
1973 return 0;
1974
1975 return 1;
1976 }
1977
1978 /* Starting point of packet process function. */
1979 int bgp_read(struct thread *thread)
1980 {
1981 int ret;
1982 u_char type = 0;
1983 struct peer *peer;
1984 bgp_size_t size;
1985 char notify_data_length[2];
1986 u_int32_t notify_out;
1987
1988 /* Yes first of all get peer pointer. */
1989 peer = THREAD_ARG(thread);
1990 peer->t_read = NULL;
1991
1992 /* Note notify_out so we can check later to see if we sent another one
1993 */
1994 notify_out = peer->notify_out;
1995
1996 if (peer->fd < 0) {
1997 zlog_err("bgp_read(): peer's fd is negative value %d",
1998 peer->fd);
1999 return -1;
2000 }
2001
2002 BGP_READ_ON(peer->t_read, bgp_read, peer->fd);
2003
2004 /* Read packet header to determine type of the packet */
2005 if (peer->packet_size == 0)
2006 peer->packet_size = BGP_HEADER_SIZE;
2007
2008 if (stream_get_endp(peer->ibuf) < BGP_HEADER_SIZE) {
2009 ret = bgp_read_packet(peer);
2010
2011 /* Header read error or partial read packet. */
2012 if (ret < 0)
2013 goto done;
2014
2015 /* Get size and type. */
2016 stream_forward_getp(peer->ibuf, BGP_MARKER_SIZE);
2017 memcpy(notify_data_length, stream_pnt(peer->ibuf), 2);
2018 size = stream_getw(peer->ibuf);
2019 type = stream_getc(peer->ibuf);
2020
2021 /* Marker check */
2022 if (((type == BGP_MSG_OPEN) || (type == BGP_MSG_KEEPALIVE))
2023 && !bgp_marker_all_one(peer->ibuf, BGP_MARKER_SIZE)) {
2024 bgp_notify_send(peer, BGP_NOTIFY_HEADER_ERR,
2025 BGP_NOTIFY_HEADER_NOT_SYNC);
2026 goto done;
2027 }
2028
2029 /* BGP type check. */
2030 if (type != BGP_MSG_OPEN && type != BGP_MSG_UPDATE
2031 && type != BGP_MSG_NOTIFY && type != BGP_MSG_KEEPALIVE
2032 && type != BGP_MSG_ROUTE_REFRESH_NEW
2033 && type != BGP_MSG_ROUTE_REFRESH_OLD
2034 && type != BGP_MSG_CAPABILITY) {
2035 if (bgp_debug_neighbor_events(peer))
2036 zlog_debug("%s unknown message type 0x%02x",
2037 peer->host, type);
2038 bgp_notify_send_with_data(peer, BGP_NOTIFY_HEADER_ERR,
2039 BGP_NOTIFY_HEADER_BAD_MESTYPE,
2040 &type, 1);
2041 goto done;
2042 }
2043 /* Mimimum packet length check. */
2044 if ((size < BGP_HEADER_SIZE) || (size > BGP_MAX_PACKET_SIZE)
2045 || (type == BGP_MSG_OPEN && size < BGP_MSG_OPEN_MIN_SIZE)
2046 || (type == BGP_MSG_UPDATE
2047 && size < BGP_MSG_UPDATE_MIN_SIZE)
2048 || (type == BGP_MSG_NOTIFY
2049 && size < BGP_MSG_NOTIFY_MIN_SIZE)
2050 || (type == BGP_MSG_KEEPALIVE
2051 && size != BGP_MSG_KEEPALIVE_MIN_SIZE)
2052 || (type == BGP_MSG_ROUTE_REFRESH_NEW
2053 && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE)
2054 || (type == BGP_MSG_ROUTE_REFRESH_OLD
2055 && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE)
2056 || (type == BGP_MSG_CAPABILITY
2057 && size < BGP_MSG_CAPABILITY_MIN_SIZE)) {
2058 if (bgp_debug_neighbor_events(peer))
2059 zlog_debug("%s bad message length - %d for %s",
2060 peer->host, size,
2061 type == 128
2062 ? "ROUTE-REFRESH"
2063 : bgp_type_str[(int)type]);
2064 bgp_notify_send_with_data(peer, BGP_NOTIFY_HEADER_ERR,
2065 BGP_NOTIFY_HEADER_BAD_MESLEN,
2066 (u_char *)notify_data_length,
2067 2);
2068 goto done;
2069 }
2070
2071 /* Adjust size to message length. */
2072 peer->packet_size = size;
2073 }
2074
2075 ret = bgp_read_packet(peer);
2076 if (ret < 0)
2077 goto done;
2078
2079 /* Get size and type again. */
2080 (void)stream_getw_from(peer->ibuf, BGP_MARKER_SIZE);
2081 type = stream_getc_from(peer->ibuf, BGP_MARKER_SIZE + 2);
2082
2083 /* BGP packet dump function. */
2084 bgp_dump_packet(peer, type, peer->ibuf);
2085
2086 size = (peer->packet_size - BGP_HEADER_SIZE);
2087
2088 /* Read rest of the packet and call each sort of packet routine */
2089 switch (type) {
2090 case BGP_MSG_OPEN:
2091 peer->open_in++;
2092 bgp_open_receive(peer, size); /* XXX return value ignored! */
2093 break;
2094 case BGP_MSG_UPDATE:
2095 peer->readtime = monotime(NULL);
2096 bgp_update_receive(peer, size);
2097 break;
2098 case BGP_MSG_NOTIFY:
2099 bgp_notify_receive(peer, size);
2100 break;
2101 case BGP_MSG_KEEPALIVE:
2102 peer->readtime = monotime(NULL);
2103 bgp_keepalive_receive(peer, size);
2104 break;
2105 case BGP_MSG_ROUTE_REFRESH_NEW:
2106 case BGP_MSG_ROUTE_REFRESH_OLD:
2107 peer->refresh_in++;
2108 bgp_route_refresh_receive(peer, size);
2109 break;
2110 case BGP_MSG_CAPABILITY:
2111 peer->dynamic_cap_in++;
2112 bgp_capability_receive(peer, size);
2113 break;
2114 }
2115
2116 /* If reading this packet caused us to send a NOTIFICATION then store a
2117 * copy
2118 * of the packet for troubleshooting purposes
2119 */
2120 if (notify_out < peer->notify_out) {
2121 memcpy(peer->last_reset_cause, peer->ibuf->data,
2122 peer->packet_size);
2123 peer->last_reset_cause_size = peer->packet_size;
2124 notify_out = peer->notify_out;
2125 }
2126
2127 /* Clear input buffer. */
2128 peer->packet_size = 0;
2129 if (peer->ibuf)
2130 stream_reset(peer->ibuf);
2131
2132 done:
2133 /* If reading this packet caused us to send a NOTIFICATION then store a
2134 * copy
2135 * of the packet for troubleshooting purposes
2136 */
2137 if (notify_out < peer->notify_out) {
2138 memcpy(peer->last_reset_cause, peer->ibuf->data,
2139 peer->packet_size);
2140 peer->last_reset_cause_size = peer->packet_size;
2141 }
2142
2143 return 0;
2144 }