inpcb: Don't choose lport which could render same addr/port pair
authorSepherosa Ziehau <sephe@dragonflybsd.org>
Fri, 19 Aug 2011 02:05:47 +0000 (10:05 +0800)
committerSepherosa Ziehau <sephe@dragonflybsd.org>
Fri, 19 Aug 2011 02:43:43 +0000 (10:43 +0800)
commite0808efe65f7e3a18feaf7a71cca261f4100538a
tree44f96cb76905a559d668550ce068f0843d74158c
parent0eed2f27d6e7d8ddb07e73a15eff84a57dbc9337
inpcb: Don't choose lport which could render same addr/port pair

A TCP connect to 127.0.0.1:PORT_A could be successful even if there is
no process listening on PORT_A:
127.0.0.1:PORT_A <---> 127.0.0.1:PORT_A

The problem here is that PORT_A is chosen as the lport for the socket
to be connected to PORT_A, and this actually creates simultaneous connect
on 127.0.0.1 but with only one inpcb.  Socket connected in this way is
useless and could break connect retry for the service listens on the
loopback interface.  Therefore, we skip the lport if the result will be:
(lport == fport && laddr == faddr)

NOTE: This still does not guard against explicit local port and address
chosen using bind(2) and then calling connect(2)
sys/netinet/in_pcb.c