> For the complete documentation index, see [llms.txt](https://jncia-workbook.gitbook.io/workbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jncia-workbook.gitbook.io/workbook/junos-routing-policy/10-bgp-outbound-filtering.md).

# 10) BGP Outbound Filtering

### Pre-Work

Load the config called **policy.ospf.into.bgp.cfg** using the **load\_config\_on\_nodes.py** script.

```
python3 load_config_on_nodes.py --lab_dir three-routers --config_filename policy.ospf.into.bgp.cfg
```

### Lab

<figure><img src="/files/gPc4bwAsRBLY1dl5Buvb" alt=""><figcaption></figcaption></figure>

All routing (static routes, OSPFv2, and BGP) is already setup. OSPF is currently redistributed into BGP on R2.

<figure><img src="/files/71c93111nrFQ7tLdkZPf" alt=""><figcaption></figcaption></figure>

Configure R2 so that route advertisements within RFC1918 (`10/8`, `172.16/12`, `192.168/16`) are filtered from being advertised to R3. You must use a prefix-list. Do not edit the export policy that is redistributing OSPF into BGP.

### Answer

<details>

<summary>Expand to reveal</summary>

```
set policy-options prefix-list RFC1918 10.0.0.0/8
set policy-options prefix-list RFC1918 172.16.0.0/12
set policy-options prefix-list RFC1918 192.168.0.0/16

edit policy-options policy-statement R3_OUTBOUND
set term REJECT_RFC1918 from prefix-list-filter RFC1918 orlonger
set term REJECT_RFC1918 then reject
set term ACCEPT_ALL then accept

top
set protocols bgp group EBGP neighbor 10.2.3.3 export R3_OUTBOUND
```

</details>

### Explanation

<details>

<summary>Expand to reveal</summary>

This lab tests your understanding of using a mask filter with prefix-lists. Instead of matching the prefix-list using `from prefix-list <name>`, we use `from prefix-list-filter <name> <match>`:

```
set policy-options policy-statement R3_OUTBOUND from prefix-list-filter RFC1918 orlonger
```

\
There are three match types we can use with a prefix-list-filter, and they work the same as you've seen with inline route-filters:

* `exact`
* `orlonger`
* `longer`

\
By creating a prefix-list that has the three RFC1918 prefixes and using a `prefix-list-filter` with `orlonger`, we match any subnet within RFC1918 with a mask up to a /32. This gives us the same functionality as using `le 32` in a prefix-list on IOS.

\
This lab also demonstrates how to apply route policies to individual BGP peers. Just like with redistribution, we use `import` or `export` statements on the peer.

```
set protocols bgp group EBGP neighbor 10.2.3.3 export R3_OUTBOUND
```

\
We can see that R2 is now filtering routes advertised to R3:

```
admin@R2> show route advertising-protocol bgp 10.2.3.3 

inet.0: 13 destinations, 13 routes (13 active, 0 holddown, 0 hidden)
  Prefix		  Nexthop	       MED     Lclpref    AS path
* 1.1.1.1/32              Self                 1                  I
* 2.2.2.2/32              Self                                    I
```

\
As a side note, applying an export policy directly to a BGP peer has some interesting behavior. When only the OSPF\_TO\_BGP export policy is applied to BGP globally, `2.2.2.2/32` was not redistributed into BGP. This makes sense, as `2.2.2.2/32` is from protocol direct in the RIB, not protocol OSPF. (This is different behavior from IOS, where local prefixes advertiesd into OSPF would also be redistributed).&#x20;

\
However, after adding the R3\_OUTBOUND policy, `2.2.2.2/32` starts being advertised (due to the ACCEPT\_ALL term). It seems that applying the export statement to the peer also "redistributes" prefixes from the RIB into BGP. We need this ACCEPT\_ALL term because a peer-level policy overrides a global policy, and the default BGP export policy is to reject (not redistribute prefixes from the RIB into BGP). This is much further than we need to go for JNCIA, but it is worth noticing. A more proper configuration would probably be to chain the global and peer-level policies together:

```
edit policy-options policy-statement R3_OUTBOUND
set term REJECT_RFC1918 from prefix-list-filter RFC1918 orlonger
set term REJECT_RFC1918 then reject

top
set protocols bgp group EBGP neighbor 10.2.3.3 export [ R3_OUTBOUND OSPF_TO_BGP ]
```

Now only `1.1.1.1/32` is advertised:

```
admin@R2> show route advertising-protocol bgp 10.2.3.3 

inet.0: 13 destinations, 13 routes (13 active, 0 holddown, 0 hidden)
  Prefix		  Nexthop	       MED     Lclpref    AS path
* 1.1.1.1/32              Self                 1                  I

admin@R2> 
```

</details>

### Further Reading

<https://www.juniper.net/documentation/us/en/software/junos/routing-policy/topics/concept/policy-configuring-prefix-lists-for-use-in-routing-policy-match-conditions.html#understanding-prefix-lists-for-use-in-routing-policy-match-conditions__id-10263921>

<https://www.youtube.com/watch?v=FCGv07SmxtQ>

<https://www.juniper.net/documentation/us/en/software/junos/routing-policy/bgp/topics/example/policy-bgp-levels.html>
