๐ŸŒ BGP

Routing 3: three companies, three autonomous systems. Peer them, read the AS path, stop a hijack, and time a failover.

Video: BGP (Networking video)
Prefer to watch? This short video runs this page's main commands on three real FRR routers in Docker, with on-screen captions and a synthetic voiceover (an AI voice trained on the author's own recording). Click to play, or watch it on YouTube.
Routing 3: BGP between three networks

What you will learn:
   what an autonomous system (AS) is, and how eBGP neighbors are set up
   why FRR sends and accepts nothing until you write a policy (and Cisco IOS does)
   how to read the BGP table and the AS path, and why the shortest AS path wins
   how BGP stops loops, with the counter that proves it
   a route hijack: one wrong /25 that cut h1 off from h3, and the prefix filter that stops it
   why pulling one cable cost 169 seconds of traffic, and how BFD made it 0.8
   steering traffic: local preference (how you leave) and AS-path prepending (how others come in)
   the Cisco IOS command for each step, side by side

OSPF (Routing 2) is for the routers of ONE organisation: they share everything and build one map. BGP (Border Gateway Protocol) connects networks run by DIFFERENT people, which is how the internet's tens of thousands of networks exchange routes. Each network is an autonomous system with its own number (an ASN). BGP routers do not share a map; each one tells its neighbors "I can reach these networks, and here is the list of ASes on the way" (the AS path), and each neighbor decides for itself, by policy, what to believe and what to pass on.
Every command and every line of output on this page was run for real on 2026-11-22 in the same lab as Routings 1 and 2. The routers run FRRouting (FRR) 10.7.1, whose commands were modelled on Cisco's. Where Cisco IOS is different, the Cisco command is shown next to it (Cisco lines are standard IOS syntax for reference; they were not run here).

The lab: three companies

The same triangle as Routing 2, but now each router belongs to a different company, its own AS. We use private AS numbers (64512-65534 are for private use, like 10.x addresses):

                      link 13  10.0.13.0/24
            +------------------------------------+
            |                                    |
   h1 -- r1 (AS 65001) ---- r2 (AS 65002) ---- r3 (AS 65003) -- h3
                  link 12            link 23

   r1: 10.1.0.1 (eth0, LAN 1)   10.0.12.1 (eth1, link 12)   10.0.13.1 (eth2, link 13)
   r2: 10.0.12.2 (eth0, link 12) 10.0.23.2 (eth1, link 23)
   r3: 10.0.23.3 (eth0, link 23) 10.3.0.1 (eth1, LAN 3)    10.0.13.3 (eth2, link 13)

r1 announces LAN 1 (10.1.0.0/24), r3 announces LAN 3 (10.3.0.0/24), and r2 is a company in the middle with no LAN of its own.

We started from a clean lab with no routes and no OSPF. FRR runs BGP as its own program (bgpd), switched on in /etc/frr/daemons along with bfdd:
$ ps -eo comm | grep -E "zebra|bgpd|bfdd"
zebra
bfdd
bgpd

r1# show running-config
frr version 10.7.1_git
frr defaults traditional
hostname r1

h1$ ping -c2 -W1 10.3.0.10
2 packets transmitted, 0 packets received, 100% packet loss

Step 1: start BGP and name the neighbors

On r1: start BGP for AS 65001, give it a router ID, list the two neighbors and the AS each one is in, and announce LAN 1.
r1# configure terminal
r1(config)# router bgp 65001
r1(config-router)# bgp router-id 1.1.1.1
r1(config-router)# neighbor 10.0.12.2 remote-as 65002
r1(config-router)# neighbor 10.0.13.3 remote-as 65003
r1(config-router)# address-family ipv4 unicast
r1(config-router-af)# network 10.1.0.0/24
r1(config-router-af)# end

Cisco IOS:
   router bgp 65001
    bgp router-id 1.1.1.1
    neighbor 10.0.12.2 remote-as 65002
    neighbor 10.0.13.3 remote-as 65003
    network 10.1.0.0 mask 255.255.255.0

What these lines mean:
   router bgp 65001          "I am AS 65001". There is one BGP process per router, named by its AS
   neighbor X remote-as Y    BGP neighbors are configured by hand, one by one. Nothing is discovered, unlike OSPF. A different AS means external BGP (eBGP)
   network 10.1.0.0/24       announce this network. Unlike OSPF's network command, this one really is "advertise this prefix", and only if the exact prefix is already in the routing table (here it is: LAN 1 is connected)

r2 and r3 the same way:
r2(config)# router bgp 65002
r2(config-router)# bgp router-id 2.2.2.2
r2(config-router)# neighbor 10.0.12.1 remote-as 65001
r2(config-router)# neighbor 10.0.23.3 remote-as 65003

r3(config)# router bgp 65003
r3(config-router)# bgp router-id 3.3.3.3
r3(config-router)# neighbor 10.0.23.2 remote-as 65002
r3(config-router)# neighbor 10.0.13.1 remote-as 65001
r3(config-router)# address-family ipv4 unicast
r3(config-router-af)# network 10.3.0.0/24

Step 2: connected, but nothing is shared (Policy)

Fourteen seconds later the sessions are up (Up/Down shows a time), yet where the prefix counts should be, FRR says (Policy):
r1# show bgp summary
Neighbor        V         AS   MsgRcvd   MsgSent   TblVer  InQ OutQ  Up/Down State/PfxRcd   PfxSnt Desc
10.0.12.2       4      65002         3         3        1    0    0 00:00:14     (Policy) (Policy) N/A
10.0.13.3       4      65003         3         3        1    0    0 00:00:14     (Policy) (Policy) N/A

r1's BGP table holds only its own LAN, and the ping still fails:
     Network          Next Hop            Metric LocPrf Weight Path
 *>  10.1.0.0/24      0.0.0.0                  0         32768 i

This is on purpose. RFC 8212 says an eBGP router must not accept or send any routes until you give it a policy, because accidentally passing on everything you hear is how big internet outages start. FRR follows it (bgp ebgp-requires-policy is on by default). Cisco IOS does NOT: on IOS the routes would be flowing already. Cisco IOS XR behaves like FRR.

The smallest policy is a route-map that permits everything, applied in and out on each neighbor:
r1(config)# route-map ALLOW permit 10
r1(config-route-map)# exit
r1(config)# router bgp 65001
r1(config-router)# address-family ipv4 unicast
r1(config-router-af)# neighbor 10.0.12.2 route-map ALLOW in
r1(config-router-af)# neighbor 10.0.12.2 route-map ALLOW out
r1(config-router-af)# neighbor 10.0.13.3 route-map ALLOW in
r1(config-router-af)# neighbor 10.0.13.3 route-map ALLOW out
(and the same on r2 and r3 for their neighbors)

r1# show bgp summary
Neighbor        V         AS   MsgRcvd   MsgSent   TblVer  InQ OutQ  Up/Down State/PfxRcd   PfxSnt Desc
10.0.12.2       4      65002        15        14        2    0    0 00:00:22            1        2 N/A
10.0.13.3       4      65003        14        14        2    0    0 00:00:22            1        2 N/A

Now there are numbers: 1 prefix received from each neighbor, 2 sent. The sessions did not even restart.
"Permit everything" is fine for a lab. On a real border router, the in and out policies are where you say exactly what you accept and announce (see the hijack below). The no bgp ebgp-requires-policy command switches the check off; don't.

Step 3: the BGP table and the AS path

r3# show bgp ipv4 unicast
     Network          Next Hop            Metric LocPrf Weight Path
 S>  10.1.0.0/24      10.0.13.1                0             0 65001 i
 S                    10.0.23.2                              0 65002 65001 i
 *>  10.3.0.0/24      0.0.0.0                  0         32768 i

r3 heard about LAN 1 twice:
   from r1 directly over link 13, path "65001"
   from r2, path "65002 65001" (r2 heard it from r1 and passed it on, adding its own AS in front)
">" marks the best path. ("S" means Stale for a moment: the sessions had just been given their new policy and were refreshing.) Why the first one?
r3# show bgp ipv4 unicast 10.1.0.0/24
Paths: (2 available, best #1, table default)
  65001
    10.0.13.1 from 10.0.13.1 (1.1.1.1)
      Origin IGP, metric 0, valid, external, best (AS Path)
  65002 65001
    10.0.23.2 from 10.0.23.2 (2.2.2.2)
      Origin IGP, valid, external

"best (AS Path)": the shorter list of ASes won. BGP has a long list of tie-breakers (weight, local preference, AS path length, origin, MED, ...), and with nothing else set, the AS path length decides. Note it counts ASes, not routers or link speed: a path through one huge slow AS beats a path through two fast ones.

The best path goes into the routing table, marked B, with distance 20 for eBGP:
r1# show ip route bgp
B>* 10.3.0.0/24 [20/0] via 10.0.13.3, eth2, weight 1, 00:00:06

h1$ ping -c3 -W1 10.3.0.10
3 packets transmitted, 3 packets received, 0% packet loss
h1$ traceroute -n -w1 -q1 10.3.0.10
 1  10.1.0.1  0.009 ms
 2  10.0.13.3  0.006 ms
 3  10.3.0.10  0.006 ms

Cisco IOS: show ip bgp summary, show ip bgp, show ip bgp 10.1.0.0, show ip route bgp.

How BGP stops loops

r3 passes LAN 1 back towards r1, and r2 does too. So why doesn't r1 learn its own network back and start a loop? Because the AS path is also a loop detector: a router throws away any route whose path already contains its own AS. The counter is in the neighbor details:
r1# show bgp neighbors 10.0.13.3
  BGP state = Established, up for 00:00:25
  Hold time is 180 seconds, keepalive interval is 60 seconds
  ...
  Prefix statistics:
    Inbound filtered: 0
    AS-PATH loop: 5
  ...
  Route map for incoming advertisements is *ALLOW
  Route map for outgoing advertisements is *ALLOW
  1 accepted, 2 sent prefixes, 4 received duplicates

Five updates from r3 already carried 65001 in their path and were dropped. Note the hold time too: 180 seconds. It matters in a minute.

Step 4: a route hijack

BGP believes what neighbors tell it. Here r2's admin makes a mistake: a typo announces 10.3.0.0/25, half of r3's LAN, and r2 has nothing there (a blackhole route stands in for "no real network behind it"):
r2(config)# ip route 10.3.0.0/25 blackhole
r2(config)# router bgp 65002
r2(config-router)# address-family ipv4 unicast
r2(config-router-af)# network 10.3.0.0/25

r1# show ip route bgp
B>* 10.3.0.0/24 [20/0] via 10.0.13.3, eth2, weight 1, 00:00:12
B>* 10.3.0.0/25 [20/0] via 10.0.12.2, eth1, weight 1, 00:00:04

h1$ ping -c3 -W1 10.3.0.10
3 packets transmitted, 0 packets received, 100% packet loss

Routers always use the LONGEST matching prefix: a /25 beats a /24, whatever the AS path says. So r1 sends everything for 10.3.0.0 to 10.3.0.127 to r2, which drops it. Even r3, the real owner, was fooled:
r3# show ip route 10.3.0.10
Routing entry for 10.3.0.0/25
  Known via "bgp", distance 20, metric 0, best
  * 10.0.23.2, via eth0, weight 1

This is how real internet outages happen. In 2008 a provider in Pakistan meant to block YouTube inside the country, announced a more specific prefix of YouTube's addresses, and its upstream passed it on: much of the world's YouTube traffic went to Pakistan for about two hours.

Step 5: filter what you accept

The defence is an inbound filter. A common rule on the internet: accept nothing smaller than a /24. A prefix list does it:
r1(config)# ip prefix-list NO-SMALLER seq 5 permit 0.0.0.0/0 le 24
r1(config)# router bgp 65001
r1(config-router)# address-family ipv4 unicast
r1(config-router-af)# neighbor 10.0.12.2 prefix-list NO-SMALLER in
(and the same on r3 for its neighbor r2, 10.0.23.2)

"0.0.0.0/0 le 24" means any prefix with a length of /24 or shorter (/8, /16, /24...). A /25 does not match, and a prefix list denies anything it does not permit.

r1# show ip route bgp
B>* 10.3.0.0/24 [20/0] via 10.0.13.3, eth2, weight 1, 00:00:20
h1$ ping -c3 -W1 10.3.0.10
3 packets transmitted, 3 packets received, 0% packet loss

r1# show bgp neighbors 10.0.12.2
  Prefix statistics:
    Inbound filtered: 1
  Incoming update prefix filter list is *NO-SMALLER

One route filtered. Cisco IOS: the same ip prefix-list line, and neighbor 10.0.12.2 prefix-list NO-SMALLER in.
Why on r3 as well? We first filtered only on r1, and r3 accepted r2's /25 and passed it on to r1 over link 13. Every router has to filter for itself. In real life, providers filter what their customers may announce (the customer's own prefixes and nothing else), and RPKI lets the owner of a prefix sign which AS may announce it; FRR supports both.
Then r2 fixed its typo (no network 10.3.0.0/25, no ip route 10.3.0.0/25 blackhole).

Step 6: pull the cable (169 seconds)

In Routing 2, OSPF lost not one ping when we pulled r1's end of link 13. Same test with BGP: h1 pings h3 every 0.2 seconds and we take r1's link 13 interface down. Every 10 seconds we ask r3 what it thinks of its BGP session with r1:
r1$ ip link set eth2 down
  10s: r3 still sees r1 (BGP): Established, up 00:00:54
  20s: r3 still sees r1 (BGP): Established, up 00:01:04
  ...
  150s: r3 still sees r1 (BGP): Established, up 00:03:16
  160s: r3 still sees r1 (BGP): Established, up 00:03:26
  170s: r3 still sees r1 (BGP): Active, up 00:00:03
pings answered: 181, lost: 843 = 168.6 seconds without a path

Almost three minutes. What happened:
   r1 saw its interface go down, dropped the session at once and switched to the path through r2. h1's pings reached h3 fine.
   r3's interface stayed up (we only pulled r1's end). r3 kept the session and kept sending the REPLIES straight into link 13, until no keepalive had arrived for the hold time, 180 seconds (the last one had arrived about 11 seconds before the pull).
   Unlike OSPF, BGP neighbors share no map. r1 cannot tell r3 through r2 "link 13 is gone"; r2 only passes on routes, and r1's route to LAN 1 had not changed for r2. So r3 had to find out by itself.

Afterwards r3 used the long way:
r3# show bgp ipv4 unicast 10.1.0.0/24
  65002 65001
    10.0.23.2 from 10.0.23.2 (2.2.2.2)
      Origin IGP, valid, external, best (First path received)

A silent failure (both ends up, every packet dropped, tc netem as in Routing 2) was the same story: 175.6 seconds.

Fix: BFD (0.8 seconds)

BFD again: one line per neighbor, on both ends.
r1(config)# router bgp 65001
r1(config-router)# neighbor 10.0.13.3 bfd
r3(config)# router bgp 65003
r3(config-router)# neighbor 10.0.13.1 bfd

r1# show bfd peers brief
SessionId  LocalAddress     PeerAddress      Status
2260755460 10.0.13.1        10.0.13.3        up

Pull the cable on r1 again:  pings answered: 48, lost: 4 = 0.8 seconds without a path
Silent failure:              pings answered: 48, lost: 4 = 0.8 seconds without a path

From 169 seconds to 0.8. BFD notices within 900 ms (3 missed checks at 300 ms) on BOTH ends, and BGP drops the session at once.
Cisco IOS (reference):
   interface GigabitEthernet0/2
    bfd interval 300 min_rx 300 multiplier 3
   router bgp 65001
    neighbor 10.0.13.3 fall-over bfd

Without BFD, shorter BGP timers help too. We tried keepalive 3 / hold 9 on both ends instead of BFD:
r1(config-router)# neighbor 10.0.13.3 timers 3 9
r1# show bgp neighbors 10.0.13.3
  Hold time is 9 seconds, keepalive interval is 3 seconds
(the session has to restart to use new timers: clear bgp 10.0.13.3)
Silent failure: 7.6 seconds without a path. Better than 176, still ten times slower than BFD. Cisco IOS: neighbor 10.0.13.3 timers 3 9.

Steering traffic out: local preference

BGP lets each network choose by policy, not by speed. r1 decides to send its traffic for LAN 3 through r2 (maybe r2 is the cheaper provider). Local preference is r1's own score for a path; higher wins, default 100, and it beats AS path length:
r1(config)# route-map PREFER permit 10
r1(config-route-map)# set local-preference 200
r1(config-route-map)# exit
r1(config)# router bgp 65001
r1(config-router)# address-family ipv4 unicast
r1(config-router-af)# neighbor 10.0.12.2 route-map PREFER in

r1# show bgp ipv4 unicast
     Network          Next Hop            Metric LocPrf Weight Path
 *>  10.1.0.0/24      0.0.0.0                  0         32768 i
 *>  10.3.0.0/24      10.0.12.2                     200      0 65002 65003 i
 *                    10.0.13.3                0             0 65003 i

The longer path (65002 65003) wins because of LocPrf 200.
h1$ traceroute -n -w1 -q1 10.3.0.10
 1  10.1.0.1  0.007 ms
 2  10.0.12.2  0.004 ms
 3  10.0.13.3  0.004 ms
 4  10.3.0.10  0.005 ms
(Hop 3 shows r3's link 13 address: r3 answers from the interface its reply leaves by, and r3 still sends to LAN 1 over link 13.)

PREFER replaced ALLOW on that neighbor: one route-map per direction per neighbor, and a route-map with a permit and no match statement permits everything, so PREFER is also a valid policy.

But the way back did not change:
r3# show ip route bgp
B>* 10.1.0.0/24 [20/0] via 10.0.13.1, eth2, weight 1, 00:00:07

Local preference only steers how YOUR traffic leaves. r3 makes its own decisions. (h3's traceroute showed "*" for hop 2: r1 answered from its link 12 address, 10.0.12.1, and nobody announced link 12 in BGP, so r3 had no route back to it. BGP only carries the networks you announce.)
Cisco IOS: the same route-map, and neighbor 10.0.12.2 route-map PREFER in.

Steering traffic in: AS-path prepending

You cannot set the other side's local preference, but you can make your direct path look worse to them. r1 adds its own AS twice more to what it tells r3:
r1(config)# route-map LONGER permit 10
r1(config-route-map)# set as-path prepend 65001 65001
r1(config-route-map)# exit
r1(config)# router bgp 65001
r1(config-router)# address-family ipv4 unicast
r1(config-router-af)# neighbor 10.0.13.3 route-map LONGER out

r3# show bgp ipv4 unicast
     Network          Next Hop            Metric LocPrf Weight Path
 *>  10.1.0.0/24      10.0.23.2                              0 65002 65001 i
 *                    10.0.13.1                0             0 65001 65001 65001 i
 *>  10.3.0.0/24      0.0.0.0                  0         32768 i

r3# show ip route bgp
B>* 10.1.0.0/24 [20/0] via 10.0.23.2, eth0, weight 1, 00:00:05

Three ASes direct against two through r2: r3 now sends to LAN 1 through r2. Both directions avoid link 13:
h3$ traceroute -n -w1 -q1 10.1.0.10
 1  10.3.0.1  0.007 ms
 2  10.0.23.2  0.003 ms
 3  *
 4  10.1.0.10  0.016 ms
(The "*" is again a router answering from a link address that BGP does not carry.)

Prepending is a request, not an order: r3 could still override it with its own local preference. Cisco IOS: set as-path prepend 65001 65001, same syntax.

Save it

r1# write memory
Configuration saved to /etc/frr/zebra.conf
Configuration saved to /etc/frr/bgpd.conf
Configuration saved to /etc/frr/bfdd.conf
...

r1$ cat /etc/frr/bgpd.conf
route-map ALLOW permit 10
exit
!
route-map PREFER permit 10
 set local-preference 200
exit
!
route-map LONGER permit 10
 set as-path prepend 65001 65001
exit
!
router bgp 65001
 bgp router-id 1.1.1.1
 neighbor 10.0.12.2 remote-as 65002
 neighbor 10.0.13.3 remote-as 65003
 neighbor 10.0.13.3 bfd
 !
 address-family ipv4 unicast
  network 10.1.0.0/24
  neighbor 10.0.12.2 prefix-list NO-SMALLER in
  neighbor 10.0.12.2 route-map PREFER in
  neighbor 10.0.12.2 route-map ALLOW out
  neighbor 10.0.13.3 route-map ALLOW in
  neighbor 10.0.13.3 route-map LONGER out
 exit-address-family
exit
!
ip prefix-list NO-SMALLER seq 5 permit 0.0.0.0/0 le 24

The whole lesson's policy on r1 in one file. Cisco IOS: copy running-config startup-config.

Build the same lab yourself

Start with the Routing 2 lab (the triangle, with bgpd and bfdd switched on in /etc/frr/daemons), but do not configure OSPF. Then follow the steps above.
To fake failures: ip link set eth2 down / up inside r1 pulls its end of link 13. tc qdisc add dev eth2 root netem loss 100% in both r1 and r3 makes link 13 fail silently; tc qdisc del dev eth2 root heals it.
After a link comes back, the BGP session takes a few seconds to come back (r3 retries the connection every 30 seconds by default if it gave up first).

Cheat sheet: FRR and Cisco IOS

Task                        FRR (vtysh)                                   Cisco IOS
start BGP                   router bgp 65001                              router bgp 65001
router ID                   bgp router-id 1.1.1.1                         bgp router-id 1.1.1.1
eBGP neighbor               neighbor 10.0.12.2 remote-as 65002            neighbor 10.0.12.2 remote-as 65002
announce a network          network 10.1.0.0/24 (address-family ipv4)     network 10.1.0.0 mask 255.255.255.0
allow-all policy            route-map ALLOW permit 10 + neighbor ... route-map ALLOW in/out   (not required on IOS)
sessions and counts         show bgp summary                              show ip bgp summary
BGP table                   show bgp ipv4 unicast                         show ip bgp
one prefix, why best        show bgp ipv4 unicast 10.1.0.0/24             show ip bgp 10.1.0.0
neighbor details            show bgp neighbors 10.0.13.3                  show ip bgp neighbors 10.0.13.3
max /24 filter              ip prefix-list NO-SMALLER seq 5 permit 0.0.0.0/0 le 24   same
apply the filter            neighbor 10.0.12.2 prefix-list NO-SMALLER in  same
BFD                         neighbor 10.0.13.3 bfd                        neighbor 10.0.13.3 fall-over bfd (+ bfd interval on the interface)
faster timers               neighbor 10.0.13.3 timers 3 9                 same
prefer a neighbor           set local-preference 200 (route-map, in)      same
look worse to a neighbor    set as-path prepend 65001 65001 (route-map, out)   same
restart one session         clear bgp 10.0.13.3                           clear ip bgp 10.0.13.3
save                        write memory                                  copy running-config startup-config
(Cisco column: standard IOS syntax, for reference; not run in this lab.)

Common problems

Session up but (Policy) instead of prefix counts: FRR requires an inbound and outbound policy on eBGP neighbors (RFC 8212). Add route-maps.
Session stuck in Active or Connect: wrong neighbor address or remote-as, no route to the neighbor, or TCP port 179 blocked.
A network is not announced: the network statement must match a prefix that is already in the routing table exactly (same length).
Traffic suddenly goes somewhere strange: a more specific prefix (longer match) arrived from someone. Check show ip route for the address, and filter what you accept.
Failover takes about 3 minutes: the 180-second hold timer, especially when only one end saw the link go down. Use BFD.
Your local preference does not change the return traffic: it only steers how you leave. Prepend (or ask the other network) for the way in.
New timers not used: the session has to restart (clear bgp NEIGHBOR).

What's next

That completes the routing trilogy: static routes, OSPF inside one network, BGP between networks. Next on the site: Networking 101: Subnetting, the foundations (addressing, subnetting, ARP), on the way to a CCNA study series.
Missed the start? Routing 1: Static Routes builds this lab; Routing 2: OSPF adds link 13 and dynamic routing.