Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux Kernel - what function holds the source where port numbers are randomly chosen?

Anyone know what function or file in linux holds the algorithm that finds a random port to use for the bind() system call? I'm hunting all over and cannot find the method that contains this algorithm in the Linux source.

Thanks!

like image 377
Ntek Avatar asked Feb 27 '10 01:02

Ntek


People also ask

How is source port chosen?

The source port is most often randomly picked by the computer. The source port can be used by routers and firewalls in the network to distinguish between different communication flows or sessions since each session will have a different random source port associated with it.

How TCP source port are chosen?

The port number is chosen by the TCP implementation software from a range of port numbers called Ephemeral Ports. The exact mechanism for choosing the port number and the range to be used is Operating System dependent.

Why source port is always random?

Source ports are randomly generated from the unregistered port range. The source/destination port works similar to your IP. The port you send from, is the port the service will reply too. For instance; a website is simply a server listening for connections on port 80 (or 443).

Who assigns the source port number?

Registered port numbers are currently assigned by the Internet Assigned Numbers Authority (IANA) and were assigned by Internet Corporation for Assigned Names and Numbers (ICANN) before March 21, 2001, and were assigned by the Information Sciences Institute (USC/ISI) before 1998.


1 Answers

It's a long and complicated piece of code, which I'm not keen to try to grok. :-)

Have a look at the inet_csk_get_port function (in net/ipv4/inet_connection_sock.c) for TCP, and udp_lib_get_port (in net/ipv4/udp.c) for UDP. It's valid, at least, for 2.6.31, though it may vary for different versions.

like image 116
Chris Jester-Young Avatar answered Sep 19 '22 21:09

Chris Jester-Young