Just wanted to expand a little bit...
BGP heavily relies on AS numbers when it comes to loop prevention. If there are multiple routers within the AS, they by definition are sharing the same ASN, making it impossible to rely upon ASN to prevent loops between them. That's why something else should be designed -- routes received from routers with the same ASN are simply not propagated back to routers of that AS. That's where full mesh becomes a requirement so that BGP tables on all iBGP participants stay consistent.
In BGP there are two loop prevention mechanism:
- for EBGP there is AS-Path attribute which states that router will drop BGP advertisement when it sees it own AS number in AS path
- for IBGP there is split horizon rule which states that update sent by one IBGP neighbor should be not send to another IBGP
Quite an important piece of already mentioned RFC 4271: https://www.rfc-editor.org/rfc/rfc4271#section-5.1.2.
AS_PATH is a well-known mandatory attribute. This attribute
identifies the autonomous systems through which routing information
carried in this UPDATE message has passed. The components of this
list can be AS_SETs or AS_SEQUENCEs.
When a BGP speaker propagates a route it learned from another BGP
speaker's UPDATE message, it modifies the route's AS_PATH attribute
based on the location of the BGP speaker to which the route will be
a) When a given BGP speaker advertises the route to an internal
peer, the advertising speaker SHALL NOT modify the AS_PATH
attribute associated with the route.
b) When a given BGP speaker advertises the route to an external
peer, the advertising speaker updates the AS_PATH attribute as
1) if the first path segment of the AS_PATH is of type
AS_SEQUENCE, the local system prepends its own AS number as
the last element of the sequence (put it in the leftmost
position with respect to the position of octets in the
protocol message). If the act of prepending will cause an
overflow in the AS_PATH segment (i.e., more than 255 ASes),
it SHOULD prepend a new segment of type AS_SEQUENCE and
prepend its own AS number to this new segment.
2) if the first path segment of the AS_PATH is of type AS_SET,
the local system prepends a new path segment of type
AS_SEQUENCE to the AS_PATH, including its own AS number in
3) if the AS_PATH is empty, the local system creates a path
segment of type AS_SEQUENCE, places its own AS into that
segment, and places that segment into the AS_PATH.