I am studying BGP and learnt that it uses TCP port 179 to form TCP connections with peer routers. My question is, since routers are layer 3 devices, how can they listen on TCP port 179 and form TCP connection?
Any clarification on this will be highly appreciated.
routers are layer 3 devices but the router main cpu implements a full TCP/IP stack for example we can telnet or SSH to them.
Telnet uses TCP 23.
SSH uses TCP 22.
Similarly most routing protocols use some form of L4 encapsulation
RIP uses UDP port 520
EIGRP is protocol 88 over IP
OSPF is protocol 89 over IP.
so listening to TCP port 179 is not an issue for routers as control plane messages are processed by main cpu TCP/IP stack.
Usually, BGP sessions can be formed only with manually configured BGP peers.
The BGP peer must use the expected source address, the expected destination address and must declare to belong to the BGP AS number configured on the local router.
So the router will open the TCP 179 port only on the local address used for the BGP session.
The local address can be different for different BGP sessions: a loopback address for iBGP sessions and the border interface(s) for eBGP sessions.
Hope to help
As I think about it this question becomes more interesting and it reveals a lack of consistency in how we describe network functions. When we describe a switch as a layer 2 device we do interpret it to mean that the switch deals with layer 2 information only and not with information from higher levels. But when we describe a router as a layer 3 device we assume that the router will also be able to use information from higher levels. Those of us who have more experience in networking know this to be the case. But this question helps me recognize that many participants in the community who are not experienced may not recognize the lack of consistency in our description of device layers. @Giuseppe Larosa provides a good discussion and some examples of routers using TCP and UDP information. This makes more sense when we recognize that the router is not limited to only layer 3 information.
yes I have had similar thoughts while writing my post.
We and the networking books should say a router operates at OSI layer3 when routing and forwarding user traffic, but it is not limited to OSI layer3, and it implements a TCP/IP stack for different aspects like management, handling routing protocol messages and so on.
I like the points that @Giuseppe Larosa and @Joseph W. Doherty make, especially the point that the primary function of a router is to forward packets and for that it uses layer 3 information. But the router also can run processes or applications that use higher layer information to perform other functions. And when we think of it in this way even the layer 2 switch can listen for telnet, or SSH, or SNMP, etc and in that sense the layer 2 switch is also listening for and processing higher layer protocol information.
So we should clearly understand that when we describe a device as layer 2 or as layer 3 that we are describing how it accomplishes its primary purpose but not describing limitations on what traffic it might recognize or process.