BGP MASTERCLASS · CHAPTER 2

BGP session, messages and states

How a BGP session is born, lives and dies: the state machine, the five messages and your first advertised prefix, read in Wireshark.

16 min readVideos: Day 1, parts 2 and 3Level: intermediate

From point A to point B

Point A: you brought up your first eBGP session between R1 and R2 and saw the three-way handshake, but the session is a black box: you don't know what the routers say to each other or why it sometimes gets stuck in Active. Point B: by the end of this chapter you will read every BGP state and message, advertise your first prefix and cause (and fix) a peering failure.

What you will see
  1. The finite state machine, from Idle to Established
  2. The five messages: OPEN, UPDATE, NOTIFICATION, KEEPALIVE and ROUTE-REFRESH
  3. network with an exact mask and how to read show ip bgp
  4. The anatomy of an UPDATE and why BGP is an overlay
  5. Distance 20, recursive next-hop, MD5, NOTIFICATION and neighbor shutdown

The finite state machine

Every BGP neighbor lives in one of six states. This is the finite state machine, and it is what you see in the State/PfxRcd column of show ip bgp summary. Reading it well is half of troubleshooting.

StateWhat happensMoves to
IdleWaits for a start event, such as configuring the neighbor. Accepts no connections.Connect
ConnectWaits for the three-way handshake to TCP 179 to complete.Success: OpenSent · Failure: Active
ActiveThe handshake failed. The router keeps actively trying to establish the TCP connection.Success: OpenSent
OpenSentSent its OPEN and waits for the neighbor's OPEN. The hold time is negotiated here: the lower value wins.OpenConfirm · error: Idle
OpenConfirmReceived the neighbor's OPEN and waits for its KEEPALIVE.Established · error: Idle
EstablishedSession complete. Only here are UPDATE messages exchanged.error: Idle
The BGP finite state machine Six states. Idle waits for a start event. Connect waits for the TCP three-way handshake to port 179. If TCP fails, the peer goes to Active and keeps trying; when TCP succeeds, from Connect or Active, it sends its OPEN and moves to OpenSent, where the hold time is negotiated and the lower value wins. When the neighbor's OPEN arrives it moves to OpenConfirm, and when the first KEEPALIVE arrives it reaches Established, the only state where UPDATE messages are exchanged. Any error, a NOTIFICATION or neighbor shutdown sends the session back to Idle. Idle waits for a start event · no TCP Connect TCP three-way handshake to :179 OpenSent OPEN sent · waits for the peer's OPEN OpenConfirm OPEN received · waits for KEEPALIVE Established UPDATE · KEEPALIVE NOTIFICATION Active TCP failed · keeps trying to connect TCP fails TCP OK · send OPEN error · NOTIFICATION · hold timer expired · neighbor shutdown → back to Idle OpenSent: hold time negotiated, the lower value wins Cisco defaults: hold time 180 s · keepalive 60 s Only Established exchanges UPDATE messages show ip bgp summary: Idle, Active, OpenSent… or the number of prefixes received when Established

The BGP state machine. Any error, a NOTIFICATION or a neighbor shutdown sends the session back to Idle. Diagram: The House of Routing.

Jesús explains it as two people introducing themselves. In Connect they say hello (SYN, SYN-ACK, ACK). In OpenSent and OpenConfirm each one says what they can do: “I speak Fortinet”, “me too, and MikroTik as well”. That exchange of capabilities takes milliseconds. The KEEPALIVE is the final handshake: Established.

The five BGP messages

Once the TCP handshake is done, everything on port 179 is one of these five types. Every message starts with a 19-byte header: a 16-byte marker, the length and the type.

TypeMessageWhat it is for
1OPENOpens the session: version 4, AS, hold time, BGP identifier and capabilities.
2UPDATEAdvertises and withdraws NLRI with its attributes.
3NOTIFICATIONReports an error and closes the session.
4KEEPALIVEConfirms the OPEN and keeps the session alive.
5ROUTE-REFRESHAsks the neighbor to resend its prefixes without closing the session (RFC 2918).

The OPEN and its capabilities

In the capture, R1 sends its OPEN right after the ACK that closes the handshake. It carries version 4, its AS (100), a hold time of 180 seconds and its BGP identifier (1.1.1.1). At the end come the optional parameters: the capabilities, what the router can do.

  • Multiprotocol Extensions: AFI IPv4 and SAFI unicast, the active address family.
  • Route refresh: R1 advertises two, Cisco's earlier proprietary version and the standard one.

Capabilities explain why we have been on BGP-4 for decades. Instead of inventing a BGP 5 or a BGP 6, new capabilities are added to the same protocol. That is Multiprotocol BGP.

The KEEPALIVE has two jobs

R2 replies with its own OPEN and, next to it, a KEEPALIVE. That first KEEPALIVE works as the acknowledgment of the OPEN. After that, KEEPALIVEs keep the session up: in the capture they go out at 14:52:45, 14:53:45 and 14:54:45, one every 60 seconds. If 180 seconds go by (the hold time, three KEEPALIVEs) without news from the neighbor, the session drops.

Notice something: the session is already Established and there is not a single UPDATE. Nobody has advertised anything. The neighborship is independent of what gets advertised. First you build the bridge; then the trucks drive across.

Advertising a prefix with network

To see an UPDATE, R1 advertises loopback 11 with the network command. Here BGP is strict: the prefix and the mask must exactly match an entry in the routing table.

R1(config)# interface loopback 11
R1(config-if)# ip address 11.11.11.11 255.255.255.0
R1(config-if)# exit
R1(config)# router bgp 100
R1(config-router)# network 11.11.0.0 mask 255.255.0.0
! accepted, but nothing is advertised: there is no 11.11.0.0/16 in the routing table
R1(config-router)# no network 11.11.0.0 mask 255.255.0.0
R1(config-router)# network 11.11.11.0 mask 255.255.255.0
R1(config-router)# do show ip bgp

With mask 255.255.0.0, IOS accepts the command but advertises nothing: the table holds 11.11.11.0/24, not a /16. Not even a soft refresh helps. Once the mask is fixed to /24, the UPDATE goes out immediately.

Soft refresh. clear ip bgp * soft out (or clear ip bgp * out) tells BGP to resend its advertisements to every neighbor without resetting the sessions. clear ip bgp *, without soft, does reset every session: use it with care.

Reading show ip bgp

show ip bgp shows the local BGP table, one of the databases BGP works with. This is how 11.11.11.0/24 looks on both routers:

R1# show ip bgp
     Network          Next Hop            Metric LocPrf Weight Path
 *>  11.11.11.0/24    0.0.0.0                  0         32768 i

R2# show ip bgp
     Network          Next Hop            Metric LocPrf Weight Path
 *>  11.11.11.0/24    172.16.1.1               0             0 100 i
  • * means the route is valid and > means it is the best. The best path is the one BGP hands to the routing table.
  • Next hop 0.0.0.0 on R1 means the router itself originated the prefix.
  • Weight is a Cisco-proprietary attribute, local to the router; 32768 is the value for locally originated routes.
  • LocPrf (local preference) helps decide where traffic leaves your AS, and Metric (MED) helps influence traffic between providers.
  • Path shows the AS_PATH and, at the end, the ORIGIN code: i (IGP) means the prefix entered BGP with network.

On R2, the next-hop is 172.16.1.1: in eBGP, the next-hop is by default the IP of the interface of the neighbor that advertised the prefix. And the AS_PATH reads 100. R2 does not care how many routers live inside AS 100: it can be one or 400. BGP sees hops between autonomous systems, not between routers.

Inside an UPDATE

In Wireshark, R1's UPDATE is the type 2 message. Open it and you will find three blocks: what is withdrawn, the attributes and what is advertised.

Anatomy of a BGP UPDATE message The UPDATE that R1 sends to R2 for 11.11.11.0/24. A 19-byte header with a 16-byte marker, the length and type 2. Then the withdrawn routes length and the withdrawn routes, empty here. Then the total path attribute length and the path attributes: ORIGIN IGP, AS_PATH 100 and NEXT_HOP 172.16.1.1, the three well-known mandatory attributes, plus MULTI_EXIT_DISC 0, which is optional. Last comes the NLRI, the prefix 11.11.11.0/24. The same message type announces and withdraws prefixes. UPDATE · R1 (AS 100) → R2 (AS 200) Marker 16 bytes, all ones header · 19 bytes Length · Type type 2 = UPDATE Withdrawn Routes Length 0 Withdrawn Routes empty prefixes to remove Total Path Attribute Length bytes Path Attributes ORIGIN · IGP (i) AS_PATH · 100 NEXT_HOP · 172.16.1.1 MED · 0 red: well-known mandatory · dark: optional (Cisco sends MED 0) how to reach it (attributes travel with the prefix) NLRI 11.11.11.0/24 what is reachable Wireshark filter: bgp · TCP 179 · one UPDATE can announce and withdraw at the same time

The anatomy of the UPDATE that R1 sends to R2 for 11.11.11.0/24. Diagram: The House of Routing.

  • Withdrawn Routes: BGP uses the same UPDATE to advertise and to withdraw. When a network disappears, it travels in this field.
  • Path Attributes: ORIGIN (IGP), AS_PATH (100) and NEXT_HOP (172.16.1.1). These are the well-known mandatory attributes: every BGP router recognizes them and every UPDATE that advertises a prefix carries them. IOS adds MULTI_EXIT_DISC, which is optional.
  • NLRI: 11.11.11.0/24.

That is why in BGP we talk about NLRI and not “routes”. “Route” sounds like only the prefix travels. What actually travels is the information needed to reach the network: the prefix plus its attributes. With a single line, network 11.11.11.0 mask 255.255.255.0, BGP builds all of this for you.

BGP as an overlay

During the class, Marcos asked whether you should advertise the network that joins the eBGP peers with network. The answer is no. Transit links between peers, eBGP or iBGP, are not advertised. It is good practice to keep them hidden.

BGP builds an end-to-end overlay on top of the routed network. What you advertise are your customers' and services' networks. What BGP does need is a solid IGP underneath: OSPF, EIGRP or any other, telling it how to reach each next-hop.

Jesús explains it with DHL. If you ship a package from Panama to Spain, you don't need to know how to get from one airport to the other: that is DHL's job. The pilot doesn't know what is in the boxes either; they only need to know how to reach the next airport. BGP is the plane: if it can't reach its next-hop, everything else disappears.

There and back: next-hop and distance 20

R2 now learns 11.11.11.0/24 and the ping to 11.11.11.11 works. But there's a catch: the ping leaves from the serial interface, and R1 knows that network because it is directly connected. Now R2 creates loopback 0 (20.20.20.20/24), which represents a customer, and pings from it.

R2(config)# interface loopback 0
R2(config-if)# ip address 20.20.20.20 255.255.255.0
R2(config-if)# do ping 11.11.11.11 source loopback 0
! fails: R1 has no route back to 20.20.20.0/24
R2(config-if)# exit
R2(config)# router bgp 200
R2(config-router)# network 20.20.20.0 mask 255.255.255.0
R2(config-router)# do clear ip bgp * soft out
R2(config-router)# do ping 11.11.11.11 source loopback 0

Without advertising 20.20.20.0/24, the ping fails. The echo request reaches R1, but R1 doesn't know how to send it back. Routing has to work in both directions. Once the network is advertised with the exact mask and the soft refresh is applied, the ping works.

R1# show ip route 20.20.20.0
! B 20.20.20.0/24 [20/0] via 172.16.1.2  -> eBGP, administrative distance 20
R1# show ip route 172.16.1.2
! 172.16.1.0/24 is directly connected, Serial0/0  -> the next-hop resolves

Two details in R1's table:

  • The route shows up as B with an administrative distance of 20. Every eBGP route has distance 20, so it is preferred over IGP routes (OSPF 110, internal EIGRP 90).
  • The next-hop 172.16.1.2 is not an interface on R1. The router does a recursive lookup: how do I reach 172.16.1.2? Through the directly connected network on Serial0/0. If that second lookup fails, the BGP route is useless.

When the session goes down

MD5 authentication on one side only

Protecting the session with MD5 is a common recommendation. To show what happens when it is misconfigured, Jesús sets the key on R1 only.

R1(config)# router bgp 100
R1(config-router)# neighbor 172.16.1.2 password thor123
! only one side configured: TCP segments fail the MD5 check,
! the hold timer expires and the session falls to Active

R2(config)# router bgp 200
R2(config-router)# neighbor 172.16.1.1 password thor123
! same key on both sides: SYN, SYN-ACK, ACK, OPEN, KEEPALIVE, UPDATE

With MD5, every TCP segment of the session carries a digest that the other side checks. R1 starts dropping R2's segments with the error “No MD5 digest from 172.16.1.2:179”. Without valid KEEPALIVEs, the session drops: R1 sends a NOTIFICATION (type 3) because the timer expired, TCP closes with FIN, PSH, ACK in both directions and the neighbor stays in Active.

This case is very real. You run a maintenance window, everything looks right and the session doesn't come back: it stays in Active. Typical causes are an extra digit in the key or a neighbor you forgot to activate in its address family.

With the same key on R2, the session comes up on its own. Wireshark shows the full sequence of a new peering, the same you will see with an upstream provider: SYN, SYN-ACK, ACK, OPEN and OPEN, KEEPALIVE and KEEPALIVE, and the UPDATEs with the prefixes already configured. The MD5 option shows up inside the TCP header.

neighbor shutdown

If a peering goes down and up again and again (flapping) and destabilizes your BGP domain, you can shut it down administratively, for that neighbor only.

R2(config)# router bgp 200
R2(config-router)# neighbor 172.16.1.1 shutdown
R2(config-router)# do show ip bgp summary
! State/PfxRcd: Idle (Admin)
R2(config-router)# no neighbor 172.16.1.1 shutdown

The debug shows the jump straight from Established to Idle, and show ip bgp summary reads Idle (Admin). R1 keeps trying with SYNs, and R2 refuses them while it stays shut down. With no neighbor 172.16.1.1 shutdown the session goes through Idle, Connect, OpenSent, OpenConfirm and Established again.

Captures without Wireshark. During the class someone asked how to capture packets in production. Cisco IOS can capture traffic on the router itself and export it as a PCAP file to open in Wireshark, without SPAN or RSPAN. Cisco calls this feature Embedded Packet Capture. It is a good alternative to debugs, which consume resources on a production router.

Class videos

These two parts of the 1st BGP Marathon were recorded live on February 26, 2021. The audio is in Spanish, with subtitles in Spanish, English, French and Portuguese. Part 2 covers the messages and the first advertisement; part 3 covers the return path, authentication and the states.

Recorded live; Spanish audio with Spanish, English, French and Portuguese subtitles. When you play it, the video loads from YouTube. Day 1 playlist on YouTube

Test what you learned

1. The session is Established, but Wireshark only shows KEEPALIVEs and no UPDATE. Is something wrong?

Answer. Not necessarily. The neighborship is independent of what gets advertised. If no router has a network statement (or another way to inject prefixes), there is no NLRI to send and you will only see a KEEPALIVE every 60 seconds.

2. You configure network 11.11.0.0 mask 255.255.0.0 and the table only holds 11.11.11.0/24. What happens?

Answer. IOS accepts the command, but BGP advertises nothing. The network command requires the prefix and the mask to exactly match an entry in the routing table.

3. In show ip bgp you see a prefix with next-hop 0.0.0.0. What does it mean?

Answer. That the router itself originated the prefix, for example with the network command. On the eBGP neighbor, the same prefix shows the next-hop of the interface of whoever advertised it.

4. What are the three well-known mandatory attributes you saw in the UPDATE?

Answer. ORIGIN, AS_PATH and NEXT_HOP. Every BGP router recognizes them, and they must travel in every UPDATE that advertises NLRI. MULTI_EXIT_DISC also showed up, but it is optional.

5. After a maintenance window, a neighbor stays in Active and the log says No MD5 digest. What do you check first?

Answer. The neighbor password key on both ends. With MD5, if the keys don't match or one side has none, the TCP segments are dropped and the session does not come up.

What you take away

  • Six states: Idle, Connect, Active, OpenSent, OpenConfirm and Established; any error goes back to Idle.
  • Five messages: OPEN (1), UPDATE (2), NOTIFICATION (3), KEEPALIVE (4) and ROUTE-REFRESH (5).
  • The OPEN negotiates version, AS, hold time and capabilities; the KEEPALIVE confirms it and then keeps the session alive.
  • network only advertises with an exact prefix and mask match; an UPDATE carries attributes and NLRI.
  • Next-hop 0.0.0.0 means local origin; in eBGP the next-hop is the neighbor's interface and must resolve recursively.
  • eBGP has distance 20, and BGP is an overlay: it needs an IGP to resolve the next-hop.
  • MD5 must match on both sides; neighbor shutdown turns a peering off without deleting its configuration.

Official resources

Source and credits. Chapter written from the BGP Masterclass that Jesús Espinoza recorded live in 2021. Text drafted with AI assistance and reviewed; original diagrams by The House of Routing. Technical details and configuration were checked against official Cisco documentation and IETF RFCs.

Navigate