BGP uses the AS path attribute to prevent routing loops. When a route passes through multiple autonomous systems (AS), each AS prepends its AS number to the AS path. Routers will ignore any updates containing their own AS number in the path to avoid loops. This is demonstrated by enabling debugging on R2 - it rejects an update from R4 containing its own AS 65003 in the path. IBGP within an AS also prevents loops through the split horizon rule and a full mesh of IBGP sessions.
Related topics: