How to Configure Next-hop Self in Junos BGP - dummies

How to Configure Next-hop Self in Junos BGP

By Walter J. Goralski, Cathy Gadecki, Michael Bushong

After seeing that the route table appears to have all the necessary routes, you probably now think that you can ping the routers in one AS from the other AS. However, that’s not quite the case. Understanding why pinging still doesn’t work requires a little explanation of how BGP routes are exchanged.

Sample topology showing next-hop addressing.
Sample topology showing next-hop addressing.

When BGP shares a route via BGP messages, it includes next-hop information so that a router knows where to send traffic when forwarding to that particular destination. The peers that reside within AS 65502 send their routes through the gateway router 5.

When that route is passed to router 3 via EBGP, the advertisement includes the next-hop information tied to that EBGP link. Specifically, the interface address on router 5 ( is marked as the next hop.

The problem is that when router 2 wants to send traffic to a peer within AS 65002, router 2 must know where the route is. But how does it get this information? That address is not in the local AS 65027! And you really don’t want to duplicate all internal addresses in every AS — that’s why BGP was invented in the first place.

One easy way to ensure that a router has a route to the next-hop address is to set the next-hop address to one that the router already knows how to reach. The address might not be in AS 65027, but the loopback address for router 3 is.

In your AS, the routers all know each other’s loopback addresses by virtue of OSPF. OSPF tells IBGP how to reach the loopback interface so that IBGP can establish its BGP session. So, if router 3 can set the next-hop address to one of its own addresses (such as the loopback), overwriting the previous address, then all the routers in your AS will be able to send traffic.

To overwrite the previous next-hop address and use the router’s own loopback as the next-hop address, use a routing policy to set next-hop self:

[edit policy-options]
policy-statement ibgp-export {
  term export-statics {
   from protocol static;
   then accept;
  term next-hop self {
   then {
     next-hop self;

The addition of a second term accomplishes this task. Routes are evaluated against the first term and accepted if they’re static routes. Then those accepted BGP routes have the next-hop value set to the local router loopback address. When those routes are learned by other peers within your network, those peers will send traffic to the gateway router loopback address, because they know how to reach the loopback address.

The “self” option on router 3 basically says, “Use my loopback address as the next hop when you advertise this route inside AS 65027. Then when routers in AS 65027 have a packet for AS 65002, they send it to me. I have the AS 65005 next hop as, so all is well.”

The alternative way to solve the problem of next-hop self is to ensure that you have a route to the peering subnet between your AS and the adjacent AS. If you run an IGP, the subnets are automatically advertised through that IGP to all your internal routers.

If you run OSPF or another IGP passively on that link (meaning that you don’t want to establish an adjacency and flood routes there), your routers will learn the subnet. In many organizations, next-hop self or IGP passive is the preferred method for BGP next hops.