in_pcb.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. /* $OpenBSD: in_pcb.c,v 1.172 2015/07/19 02:35:35 deraadt Exp $ */
  2. /* $NetBSD: in_pcb.c,v 1.25 1996/02/13 23:41:53 christos Exp $ */
  3. /*
  4. * Copyright (c) 1982, 1986, 1991, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the University nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995
  32. *
  33. * NRL grants permission for redistribution and use in source and binary
  34. * forms, with or without modification, of the software and documentation
  35. * created at NRL provided that the following conditions are met:
  36. *
  37. * 1. Redistributions of source code must retain the above copyright
  38. * notice, this list of conditions and the following disclaimer.
  39. * 2. Redistributions in binary form must reproduce the above copyright
  40. * notice, this list of conditions and the following disclaimer in the
  41. * documentation and/or other materials provided with the distribution.
  42. * 3. All advertising materials mentioning features or use of this software
  43. * must display the following acknowledgements:
  44. * This product includes software developed by the University of
  45. * California, Berkeley and its contributors.
  46. * This product includes software developed at the Information
  47. * Technology Division, US Naval Research Laboratory.
  48. * 4. Neither the name of the NRL nor the names of its contributors
  49. * may be used to endorse or promote products derived from this software
  50. * without specific prior written permission.
  51. *
  52. * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
  53. * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  54. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  55. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR
  56. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  57. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  58. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  59. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  60. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  61. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  62. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  63. *
  64. * The views and conclusions contained in the software and documentation
  65. * are those of the authors and should not be interpreted as representing
  66. * official policies, either expressed or implied, of the US Naval
  67. * Research Laboratory (NRL).
  68. */
  69. #include "pf.h"
  70. #include <sys/param.h>
  71. #include <sys/systm.h>
  72. #include <sys/mbuf.h>
  73. #include <sys/protosw.h>
  74. #include <sys/socket.h>
  75. #include <sys/socketvar.h>
  76. #include <sys/proc.h>
  77. #include <sys/domain.h>
  78. #include <sys/pool.h>
  79. #include <sys/tame.h>
  80. #include <net/if.h>
  81. #include <net/if_var.h>
  82. #include <net/route.h>
  83. #include <netinet/in.h>
  84. #include <netinet/ip.h>
  85. #include <netinet/in_pcb.h>
  86. #include <netinet/in_var.h>
  87. #include <netinet/ip_var.h>
  88. #include <net/pfvar.h>
  89. #include <sys/mount.h>
  90. #include <nfs/nfsproto.h>
  91. #ifdef INET6
  92. #include <netinet6/in6_var.h>
  93. #include <netinet6/ip6_var.h>
  94. #endif /* INET6 */
  95. #ifdef IPSEC
  96. #include <netinet/ip_esp.h>
  97. #endif /* IPSEC */
  98. struct in_addr zeroin_addr;
  99. /*
  100. * These configure the range of local port addresses assigned to
  101. * "unspecified" outgoing connections/packets/whatever.
  102. */
  103. int ipport_firstauto = IPPORT_RESERVED;
  104. int ipport_lastauto = IPPORT_USERRESERVED;
  105. int ipport_hifirstauto = IPPORT_HIFIRSTAUTO;
  106. int ipport_hilastauto = IPPORT_HILASTAUTO;
  107. struct baddynamicports baddynamicports;
  108. struct pool inpcb_pool;
  109. int inpcb_pool_initialized = 0;
  110. int in_pcbresize (struct inpcbtable *, int);
  111. #define INPCBHASH_LOADFACTOR(_x) (((_x) * 3) / 4)
  112. struct inpcbhead *in_pcbhash(struct inpcbtable *, int,
  113. const struct in_addr *, u_short, const struct in_addr *, u_short);
  114. struct inpcbhead *in6_pcbhash(struct inpcbtable *, int,
  115. const struct in6_addr *, u_short, const struct in6_addr *, u_short);
  116. struct inpcbhead *in_pcblhash(struct inpcbtable *, int, u_short);
  117. struct inpcbhead *
  118. in_pcbhash(struct inpcbtable *table, int rdom,
  119. const struct in_addr *faddr, u_short fport,
  120. const struct in_addr *laddr, u_short lport)
  121. {
  122. SIPHASH_CTX ctx;
  123. u_int32_t nrdom = htonl(rdom);
  124. SipHash24_Init(&ctx, &table->inpt_key);
  125. SipHash24_Update(&ctx, &nrdom, sizeof(nrdom));
  126. SipHash24_Update(&ctx, faddr, sizeof(*faddr));
  127. SipHash24_Update(&ctx, &fport, sizeof(fport));
  128. SipHash24_Update(&ctx, laddr, sizeof(*laddr));
  129. SipHash24_Update(&ctx, &lport, sizeof(lport));
  130. return (&table->inpt_hashtbl[SipHash24_End(&ctx) & table->inpt_hash]);
  131. }
  132. #define INPCBHASH(table, faddr, fport, laddr, lport, rdom) \
  133. in_pcbhash(table, rdom, faddr, fport, laddr, lport)
  134. struct inpcbhead *
  135. in6_pcbhash(struct inpcbtable *table, int rdom,
  136. const struct in6_addr *faddr, u_short fport,
  137. const struct in6_addr *laddr, u_short lport)
  138. {
  139. SIPHASH_CTX ctx;
  140. u_int32_t nrdom = htonl(rdom);
  141. SipHash24_Init(&ctx, &table->inpt_key);
  142. SipHash24_Update(&ctx, &nrdom, sizeof(nrdom));
  143. SipHash24_Update(&ctx, faddr, sizeof(*faddr));
  144. SipHash24_Update(&ctx, &fport, sizeof(fport));
  145. SipHash24_Update(&ctx, laddr, sizeof(*laddr));
  146. SipHash24_Update(&ctx, &lport, sizeof(lport));
  147. return (&table->inpt_hashtbl[SipHash24_End(&ctx) & table->inpt_hash]);
  148. }
  149. #define IN6PCBHASH(table, faddr, fport, laddr, lport, rdom) \
  150. in6_pcbhash(table, rdom, faddr, fport, laddr, lport)
  151. struct inpcbhead *
  152. in_pcblhash(struct inpcbtable *table, int rdom, u_short lport)
  153. {
  154. SIPHASH_CTX ctx;
  155. u_int32_t nrdom = htonl(rdom);
  156. SipHash24_Init(&ctx, &table->inpt_key);
  157. SipHash24_Update(&ctx, &nrdom, sizeof(nrdom));
  158. SipHash24_Update(&ctx, &lport, sizeof(lport));
  159. return (&table->inpt_lhashtbl[SipHash24_End(&ctx) & table->inpt_lhash]);
  160. }
  161. #define INPCBLHASH(table, lport, rdom) in_pcblhash(table, rdom, lport)
  162. void
  163. in_pcbinit(struct inpcbtable *table, int hashsize)
  164. {
  165. TAILQ_INIT(&table->inpt_queue);
  166. table->inpt_hashtbl = hashinit(hashsize, M_PCB, M_NOWAIT,
  167. &table->inpt_hash);
  168. if (table->inpt_hashtbl == NULL)
  169. panic("in_pcbinit: hashinit failed");
  170. table->inpt_lhashtbl = hashinit(hashsize, M_PCB, M_NOWAIT,
  171. &table->inpt_lhash);
  172. if (table->inpt_lhashtbl == NULL)
  173. panic("in_pcbinit: hashinit failed for lport");
  174. table->inpt_lastport = 0;
  175. table->inpt_count = 0;
  176. arc4random_buf(&table->inpt_key, sizeof(table->inpt_key));
  177. }
  178. /*
  179. * Check if the specified port is invalid for dynamic allocation.
  180. */
  181. int
  182. in_baddynamic(u_int16_t port, u_int16_t proto)
  183. {
  184. switch (proto) {
  185. case IPPROTO_TCP:
  186. return (DP_ISSET(baddynamicports.tcp, port));
  187. case IPPROTO_UDP:
  188. #ifdef IPSEC
  189. /* Cannot preset this as it is a sysctl */
  190. if (port == udpencap_port)
  191. return (1);
  192. #endif
  193. return (DP_ISSET(baddynamicports.udp, port));
  194. default:
  195. return (0);
  196. }
  197. }
  198. int
  199. in_pcballoc(struct socket *so, struct inpcbtable *table)
  200. {
  201. struct inpcb *inp;
  202. int s;
  203. struct inpcbhead *head;
  204. splsoftassert(IPL_SOFTNET);
  205. if (inpcb_pool_initialized == 0) {
  206. pool_init(&inpcb_pool, sizeof(struct inpcb), 0, 0, 0,
  207. "inpcbpl", NULL);
  208. inpcb_pool_initialized = 1;
  209. }
  210. inp = pool_get(&inpcb_pool, PR_NOWAIT|PR_ZERO);
  211. if (inp == NULL)
  212. return (ENOBUFS);
  213. inp->inp_table = table;
  214. inp->inp_socket = so;
  215. inp->inp_seclevel[SL_AUTH] = IPSEC_AUTH_LEVEL_DEFAULT;
  216. inp->inp_seclevel[SL_ESP_TRANS] = IPSEC_ESP_TRANS_LEVEL_DEFAULT;
  217. inp->inp_seclevel[SL_ESP_NETWORK] = IPSEC_ESP_NETWORK_LEVEL_DEFAULT;
  218. inp->inp_seclevel[SL_IPCOMP] = IPSEC_IPCOMP_LEVEL_DEFAULT;
  219. inp->inp_rtableid = curproc->p_p->ps_rtableid;
  220. s = splnet();
  221. if (table->inpt_hash != 0 &&
  222. table->inpt_count++ > INPCBHASH_LOADFACTOR(table->inpt_hash))
  223. (void)in_pcbresize(table, (table->inpt_hash + 1) * 2);
  224. TAILQ_INSERT_HEAD(&table->inpt_queue, inp, inp_queue);
  225. head = INPCBLHASH(table, inp->inp_lport, inp->inp_rtableid);
  226. LIST_INSERT_HEAD(head, inp, inp_lhash);
  227. head = INPCBHASH(table, &inp->inp_faddr, inp->inp_fport,
  228. &inp->inp_laddr, inp->inp_lport, rtable_l2(inp->inp_rtableid));
  229. LIST_INSERT_HEAD(head, inp, inp_hash);
  230. splx(s);
  231. so->so_pcb = inp;
  232. inp->inp_hops = -1;
  233. #ifdef INET6
  234. /*
  235. * Small change in this function to set the INP_IPV6 flag so routines
  236. * outside pcb-specific routines don't need to use sotopf(), and all
  237. * of its pointer chasing, later.
  238. */
  239. if (sotopf(so) == PF_INET6)
  240. inp->inp_flags = INP_IPV6;
  241. inp->inp_cksum6 = -1;
  242. #endif /* INET6 */
  243. return (0);
  244. }
  245. int
  246. in_pcbbind(struct inpcb *inp, struct mbuf *nam, struct proc *p)
  247. {
  248. struct socket *so = inp->inp_socket;
  249. struct inpcbtable *table = inp->inp_table;
  250. u_int16_t *lastport = &inp->inp_table->inpt_lastport;
  251. struct sockaddr_in *sin;
  252. u_int16_t lport = 0;
  253. int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
  254. int error;
  255. #ifdef INET6
  256. if (sotopf(so) == PF_INET6)
  257. return in6_pcbbind(inp, nam, p);
  258. #endif /* INET6 */
  259. if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
  260. return (EINVAL);
  261. if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
  262. ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
  263. (so->so_options & SO_ACCEPTCONN) == 0))
  264. wild = INPLOOKUP_WILDCARD;
  265. if (nam) {
  266. sin = mtod(nam, struct sockaddr_in *);
  267. if (nam->m_len != sizeof(*sin))
  268. return (EINVAL);
  269. #ifdef notdef
  270. /*
  271. * We should check the family, but old programs
  272. * incorrectly fail to initialize it.
  273. */
  274. if (sin->sin_family != AF_INET)
  275. return (EAFNOSUPPORT);
  276. #endif
  277. lport = sin->sin_port;
  278. if (IN_MULTICAST(sin->sin_addr.s_addr)) {
  279. /*
  280. * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
  281. * allow complete duplication of binding if
  282. * SO_REUSEPORT is set, or if SO_REUSEADDR is set
  283. * and a multicast address is bound on both
  284. * new and duplicated sockets.
  285. */
  286. if (so->so_options & SO_REUSEADDR)
  287. reuseport = SO_REUSEADDR|SO_REUSEPORT;
  288. } else if (sin->sin_addr.s_addr != INADDR_ANY) {
  289. sin->sin_port = 0; /* yech... */
  290. /* ... must also clear the zeropad in the sockaddr */
  291. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  292. if (!((so->so_options & SO_BINDANY) ||
  293. (sin->sin_addr.s_addr == INADDR_BROADCAST &&
  294. so->so_type == SOCK_DGRAM))) {
  295. struct in_ifaddr *ia;
  296. ia = ifatoia(ifa_ifwithaddr(sintosa(sin),
  297. inp->inp_rtableid));
  298. if (ia == NULL)
  299. return (EADDRNOTAVAIL);
  300. /* SOCK_RAW does not use in_pcbbind() */
  301. if (so->so_type != SOCK_DGRAM &&
  302. sin->sin_addr.s_addr !=
  303. ia->ia_addr.sin_addr.s_addr)
  304. return (EADDRNOTAVAIL);
  305. }
  306. }
  307. if (lport) {
  308. struct inpcb *t;
  309. /* GROSS */
  310. if (ntohs(lport) < IPPORT_RESERVED &&
  311. (error = suser(p, 0)))
  312. return (EACCES);
  313. if (so->so_euid) {
  314. t = in_pcblookup(table, &zeroin_addr, 0,
  315. &sin->sin_addr, lport, INPLOOKUP_WILDCARD,
  316. inp->inp_rtableid);
  317. if (t && (so->so_euid != t->inp_socket->so_euid))
  318. return (EADDRINUSE);
  319. }
  320. t = in_pcblookup(table, &zeroin_addr, 0,
  321. &sin->sin_addr, lport, wild, inp->inp_rtableid);
  322. if (t && (reuseport & t->inp_socket->so_options) == 0)
  323. return (EADDRINUSE);
  324. }
  325. inp->inp_laddr = sin->sin_addr;
  326. }
  327. if (lport == 0) {
  328. u_int16_t first, last;
  329. int count;
  330. if (inp->inp_flags & INP_HIGHPORT) {
  331. first = ipport_hifirstauto; /* sysctl */
  332. last = ipport_hilastauto;
  333. } else if (inp->inp_flags & INP_LOWPORT) {
  334. if ((error = suser(p, 0)))
  335. return (EACCES);
  336. first = IPPORT_RESERVED-1; /* 1023 */
  337. last = 600; /* not IPPORT_RESERVED/2 */
  338. } else {
  339. first = ipport_firstauto; /* sysctl */
  340. last = ipport_lastauto;
  341. }
  342. /*
  343. * Simple check to ensure all ports are not used up causing
  344. * a deadlock here.
  345. *
  346. * We split the two cases (up and down) so that the direction
  347. * is not being tested on each round of the loop.
  348. */
  349. if (first > last) {
  350. /*
  351. * counting down
  352. */
  353. count = first - last;
  354. if (count)
  355. *lastport = first - arc4random_uniform(count);
  356. do {
  357. if (count-- < 0) /* completely used? */
  358. return (EADDRNOTAVAIL);
  359. --*lastport;
  360. if (*lastport > first || *lastport < last)
  361. *lastport = first;
  362. lport = htons(*lastport);
  363. } while (in_baddynamic(*lastport, so->so_proto->pr_protocol) ||
  364. in_pcblookup(table, &zeroin_addr, 0,
  365. &inp->inp_laddr, lport, wild, inp->inp_rtableid));
  366. } else {
  367. /*
  368. * counting up
  369. */
  370. count = last - first;
  371. if (count)
  372. *lastport = first + arc4random_uniform(count);
  373. do {
  374. if (count-- < 0) /* completely used? */
  375. return (EADDRNOTAVAIL);
  376. ++*lastport;
  377. if (*lastport < first || *lastport > last)
  378. *lastport = first;
  379. lport = htons(*lastport);
  380. } while (in_baddynamic(*lastport, so->so_proto->pr_protocol) ||
  381. in_pcblookup(table, &zeroin_addr, 0,
  382. &inp->inp_laddr, lport, wild, inp->inp_rtableid));
  383. }
  384. }
  385. inp->inp_lport = lport;
  386. in_pcbrehash(inp);
  387. return (0);
  388. }
  389. /*
  390. * Connect from a socket to a specified address.
  391. * Both address and port must be specified in argument sin.
  392. * If don't have a local address for this socket yet,
  393. * then pick one.
  394. */
  395. int
  396. in_pcbconnect(struct inpcb *inp, struct mbuf *nam)
  397. {
  398. struct in_addr *ina = NULL;
  399. struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
  400. struct proc *p = curproc;
  401. int error;
  402. #ifdef INET6
  403. if (sotopf(inp->inp_socket) == PF_INET6)
  404. return (in6_pcbconnect(inp, nam));
  405. if ((inp->inp_flags & INP_IPV6) != 0)
  406. panic("IPv6 pcb passed into in_pcbconnect");
  407. #endif /* INET6 */
  408. if (nam->m_len != sizeof(*sin))
  409. return (EINVAL);
  410. if (sin->sin_family != AF_INET)
  411. return (EAFNOSUPPORT);
  412. if (sin->sin_port == 0)
  413. return (EADDRNOTAVAIL);
  414. if (tame_dns_check(p, sin->sin_port))
  415. return (tame_fail(p, EPERM, TAME_DNS));
  416. error = in_selectsrc(&ina, sin, inp->inp_moptions, &inp->inp_route,
  417. &inp->inp_laddr, inp->inp_rtableid);
  418. if (error)
  419. return (error);
  420. if (in_pcbhashlookup(inp->inp_table, sin->sin_addr, sin->sin_port,
  421. *ina, inp->inp_lport, inp->inp_rtableid) != 0)
  422. return (EADDRINUSE);
  423. KASSERT(inp->inp_laddr.s_addr == INADDR_ANY || inp->inp_lport);
  424. if (inp->inp_laddr.s_addr == INADDR_ANY) {
  425. if (inp->inp_lport == 0 &&
  426. in_pcbbind(inp, NULL, curproc) == EADDRNOTAVAIL)
  427. return (EADDRNOTAVAIL);
  428. inp->inp_laddr = *ina;
  429. }
  430. inp->inp_faddr = sin->sin_addr;
  431. inp->inp_fport = sin->sin_port;
  432. in_pcbrehash(inp);
  433. #ifdef IPSEC
  434. {
  435. /* Cause an IPsec SA to be established. */
  436. /* error is just ignored */
  437. ipsp_spd_inp(NULL, AF_INET, 0, &error, IPSP_DIRECTION_OUT,
  438. NULL, inp, NULL);
  439. }
  440. #endif
  441. return (0);
  442. }
  443. void
  444. in_pcbdisconnect(struct inpcb *inp)
  445. {
  446. switch (sotopf(inp->inp_socket)) {
  447. #ifdef INET6
  448. case PF_INET6:
  449. inp->inp_faddr6 = in6addr_any;
  450. break;
  451. #endif
  452. case PF_INET:
  453. inp->inp_faddr.s_addr = INADDR_ANY;
  454. break;
  455. }
  456. inp->inp_fport = 0;
  457. in_pcbrehash(inp);
  458. if (inp->inp_socket->so_state & SS_NOFDREF)
  459. in_pcbdetach(inp);
  460. }
  461. void
  462. in_pcbdetach(struct inpcb *inp)
  463. {
  464. struct socket *so = inp->inp_socket;
  465. int s;
  466. splsoftassert(IPL_SOFTNET);
  467. so->so_pcb = 0;
  468. sofree(so);
  469. m_freem(inp->inp_options);
  470. if (inp->inp_route.ro_rt)
  471. rtfree(inp->inp_route.ro_rt);
  472. #ifdef INET6
  473. if (inp->inp_flags & INP_IPV6) {
  474. ip6_freepcbopts(inp->inp_outputopts6);
  475. ip6_freemoptions(inp->inp_moptions6);
  476. } else
  477. #endif
  478. ip_freemoptions(inp->inp_moptions);
  479. #if NPF > 0
  480. if (inp->inp_pf_sk) {
  481. struct pf_state_key *sk;
  482. struct pf_state_item *si;
  483. sk = inp->inp_pf_sk;
  484. TAILQ_FOREACH(si, &sk->states, entry)
  485. if (sk == si->s->key[PF_SK_STACK] && si->s->rule.ptr &&
  486. si->s->rule.ptr->divert.port) {
  487. pf_unlink_state(si->s);
  488. break;
  489. }
  490. /* pf_unlink_state() may have detached the state */
  491. if (inp->inp_pf_sk)
  492. inp->inp_pf_sk->inp = NULL;
  493. }
  494. #endif
  495. s = splnet();
  496. LIST_REMOVE(inp, inp_lhash);
  497. LIST_REMOVE(inp, inp_hash);
  498. TAILQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue);
  499. inp->inp_table->inpt_count--;
  500. splx(s);
  501. pool_put(&inpcb_pool, inp);
  502. }
  503. void
  504. in_setsockaddr(struct inpcb *inp, struct mbuf *nam)
  505. {
  506. struct sockaddr_in *sin;
  507. nam->m_len = sizeof(*sin);
  508. sin = mtod(nam, struct sockaddr_in *);
  509. memset(sin, 0, sizeof(*sin));
  510. sin->sin_family = AF_INET;
  511. sin->sin_len = sizeof(*sin);
  512. sin->sin_port = inp->inp_lport;
  513. sin->sin_addr = inp->inp_laddr;
  514. }
  515. void
  516. in_setpeeraddr(struct inpcb *inp, struct mbuf *nam)
  517. {
  518. struct sockaddr_in *sin;
  519. #ifdef INET6
  520. if (sotopf(inp->inp_socket) == PF_INET6) {
  521. in6_setpeeraddr(inp, nam);
  522. return;
  523. }
  524. #endif /* INET6 */
  525. nam->m_len = sizeof(*sin);
  526. sin = mtod(nam, struct sockaddr_in *);
  527. memset(sin, 0, sizeof(*sin));
  528. sin->sin_family = AF_INET;
  529. sin->sin_len = sizeof(*sin);
  530. sin->sin_port = inp->inp_fport;
  531. sin->sin_addr = inp->inp_faddr;
  532. }
  533. /*
  534. * Pass some notification to all connections of a protocol
  535. * associated with address dst. The "usual action" will be
  536. * taken, depending on the ctlinput cmd. The caller must filter any
  537. * cmds that are uninteresting (e.g., no error in the map).
  538. * Call the protocol specific routine (if any) to report
  539. * any errors for each matching socket.
  540. *
  541. * Must be called at splsoftnet.
  542. */
  543. void
  544. in_pcbnotifyall(struct inpcbtable *table, struct sockaddr *dst, u_int rdomain,
  545. int errno, void (*notify)(struct inpcb *, int))
  546. {
  547. struct inpcb *inp, *ninp;
  548. struct in_addr faddr;
  549. splsoftassert(IPL_SOFTNET);
  550. #ifdef INET6
  551. /*
  552. * See in6_pcbnotify() for IPv6 codepath. By the time this
  553. * gets called, the addresses passed are either definitely IPv4 or
  554. * IPv6; *_pcbnotify() never gets called with v4-mapped v6 addresses.
  555. */
  556. #endif /* INET6 */
  557. if (dst->sa_family != AF_INET)
  558. return;
  559. faddr = satosin(dst)->sin_addr;
  560. if (faddr.s_addr == INADDR_ANY)
  561. return;
  562. rdomain = rtable_l2(rdomain);
  563. TAILQ_FOREACH_SAFE(inp, &table->inpt_queue, inp_queue, ninp) {
  564. #ifdef INET6
  565. if (inp->inp_flags & INP_IPV6)
  566. continue;
  567. #endif
  568. if (inp->inp_faddr.s_addr != faddr.s_addr ||
  569. rtable_l2(inp->inp_rtableid) != rdomain ||
  570. inp->inp_socket == 0) {
  571. continue;
  572. }
  573. if (notify)
  574. (*notify)(inp, errno);
  575. }
  576. }
  577. /*
  578. * Check for alternatives when higher level complains
  579. * about service problems. For now, invalidate cached
  580. * routing information. If the route was created dynamically
  581. * (by a redirect), time to try a default gateway again.
  582. */
  583. void
  584. in_losing(struct inpcb *inp)
  585. {
  586. struct rtentry *rt;
  587. struct rt_addrinfo info;
  588. if ((rt = inp->inp_route.ro_rt)) {
  589. inp->inp_route.ro_rt = 0;
  590. memset(&info, 0, sizeof(info));
  591. info.rti_flags = rt->rt_flags;
  592. info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst;
  593. info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
  594. info.rti_info[RTAX_NETMASK] = rt_mask(rt);
  595. rt_missmsg(RTM_LOSING, &info, rt->rt_flags, rt->rt_ifp, 0,
  596. inp->inp_rtableid);
  597. if (rt->rt_flags & RTF_DYNAMIC)
  598. (void)rtrequest1(RTM_DELETE, &info, rt->rt_priority,
  599. (struct rtentry **)0, inp->inp_rtableid);
  600. /*
  601. * A new route can be allocated
  602. * the next time output is attempted.
  603. * rtfree() needs to be called in anycase because the inp
  604. * is still holding a reference to rt.
  605. */
  606. rtfree(rt);
  607. }
  608. }
  609. /*
  610. * After a routing change, flush old routing
  611. * and allocate a (hopefully) better one.
  612. */
  613. void
  614. in_rtchange(struct inpcb *inp, int errno)
  615. {
  616. if (inp->inp_route.ro_rt) {
  617. rtfree(inp->inp_route.ro_rt);
  618. inp->inp_route.ro_rt = 0;
  619. /*
  620. * A new route can be allocated the next time
  621. * output is attempted.
  622. */
  623. }
  624. }
  625. struct inpcb *
  626. in_pcblookup(struct inpcbtable *table, void *faddrp, u_int fport_arg,
  627. void *laddrp, u_int lport_arg, int flags, u_int rdomain)
  628. {
  629. struct inpcb *inp, *match = NULL;
  630. int matchwild = 3, wildcard;
  631. u_int16_t fport = fport_arg, lport = lport_arg;
  632. struct in_addr faddr = *(struct in_addr *)faddrp;
  633. struct in_addr laddr = *(struct in_addr *)laddrp;
  634. struct inpcbhead *head;
  635. rdomain = rtable_l2(rdomain); /* convert passed rtableid to rdomain */
  636. head = INPCBLHASH(table, lport, rdomain);
  637. LIST_FOREACH(inp, head, inp_lhash) {
  638. if (rtable_l2(inp->inp_rtableid) != rdomain)
  639. continue;
  640. if (inp->inp_lport != lport)
  641. continue;
  642. wildcard = 0;
  643. #ifdef INET6
  644. if (flags & INPLOOKUP_IPV6) {
  645. struct in6_addr *laddr6 = (struct in6_addr *)laddrp;
  646. struct in6_addr *faddr6 = (struct in6_addr *)faddrp;
  647. if (!(inp->inp_flags & INP_IPV6))
  648. continue;
  649. if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6)) {
  650. if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
  651. wildcard++;
  652. else if (!IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, laddr6))
  653. continue;
  654. } else {
  655. if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
  656. wildcard++;
  657. }
  658. if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6)) {
  659. if (IN6_IS_ADDR_UNSPECIFIED(faddr6))
  660. wildcard++;
  661. else if (!IN6_ARE_ADDR_EQUAL(&inp->inp_faddr6,
  662. faddr6) || inp->inp_fport != fport)
  663. continue;
  664. } else {
  665. if (!IN6_IS_ADDR_UNSPECIFIED(faddr6))
  666. wildcard++;
  667. }
  668. } else
  669. #endif /* INET6 */
  670. {
  671. #ifdef INET6
  672. if (inp->inp_flags & INP_IPV6)
  673. continue;
  674. #endif /* INET6 */
  675. if (inp->inp_faddr.s_addr != INADDR_ANY) {
  676. if (faddr.s_addr == INADDR_ANY)
  677. wildcard++;
  678. else if (inp->inp_faddr.s_addr != faddr.s_addr ||
  679. inp->inp_fport != fport)
  680. continue;
  681. } else {
  682. if (faddr.s_addr != INADDR_ANY)
  683. wildcard++;
  684. }
  685. if (inp->inp_laddr.s_addr != INADDR_ANY) {
  686. if (laddr.s_addr == INADDR_ANY)
  687. wildcard++;
  688. else if (inp->inp_laddr.s_addr != laddr.s_addr)
  689. continue;
  690. } else {
  691. if (laddr.s_addr != INADDR_ANY)
  692. wildcard++;
  693. }
  694. }
  695. if ((!wildcard || (flags & INPLOOKUP_WILDCARD)) &&
  696. wildcard < matchwild) {
  697. match = inp;
  698. if ((matchwild = wildcard) == 0)
  699. break;
  700. }
  701. }
  702. return (match);
  703. }
  704. struct rtentry *
  705. in_pcbrtentry(struct inpcb *inp)
  706. {
  707. struct route *ro;
  708. ro = &inp->inp_route;
  709. /* check if route is still valid */
  710. if (ro->ro_rt && (ro->ro_rt->rt_flags & RTF_UP) == 0) {
  711. rtfree(ro->ro_rt);
  712. ro->ro_rt = NULL;
  713. }
  714. /*
  715. * No route yet, so try to acquire one.
  716. */
  717. if (ro->ro_rt == NULL) {
  718. #ifdef INET6
  719. memset(ro, 0, sizeof(struct route_in6));
  720. #else
  721. memset(ro, 0, sizeof(struct route));
  722. #endif
  723. switch(sotopf(inp->inp_socket)) {
  724. #ifdef INET6
  725. case PF_INET6:
  726. if (IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6))
  727. break;
  728. ro->ro_dst.sa_family = AF_INET6;
  729. ro->ro_dst.sa_len = sizeof(struct sockaddr_in6);
  730. satosin6(&ro->ro_dst)->sin6_addr = inp->inp_faddr6;
  731. ro->ro_tableid = inp->inp_rtableid;
  732. ro->ro_rt = rtalloc_mpath(&ro->ro_dst,
  733. &inp->inp_laddr6.s6_addr32[0], ro->ro_tableid);
  734. break;
  735. #endif /* INET6 */
  736. case PF_INET:
  737. if (inp->inp_faddr.s_addr == INADDR_ANY)
  738. break;
  739. ro->ro_dst.sa_family = AF_INET;
  740. ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
  741. satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr;
  742. ro->ro_tableid = inp->inp_rtableid;
  743. ro->ro_rt = rtalloc_mpath(&ro->ro_dst,
  744. &inp->inp_laddr.s_addr, ro->ro_tableid);
  745. break;
  746. }
  747. }
  748. return (ro->ro_rt);
  749. }
  750. /*
  751. * Return an IPv4 address, which is the most appropriate for a given
  752. * destination.
  753. * If necessary, this function lookups the routing table and returns
  754. * an entry to the caller for later use.
  755. */
  756. int
  757. in_selectsrc(struct in_addr **insrc, struct sockaddr_in *sin,
  758. struct ip_moptions *mopts, struct route *ro, struct in_addr *laddr,
  759. u_int rtableid)
  760. {
  761. struct sockaddr_in *sin2;
  762. struct in_ifaddr *ia = NULL;
  763. /*
  764. * If the socket(if any) is already bound, use that bound address
  765. * unless it is INADDR_ANY or INADDR_BROADCAST.
  766. */
  767. if (laddr && laddr->s_addr != INADDR_ANY &&
  768. laddr->s_addr != INADDR_BROADCAST) {
  769. *insrc = laddr;
  770. return (0);
  771. }
  772. /*
  773. * If the destination address is multicast and an outgoing
  774. * interface has been set as a multicast option, use the
  775. * address of that interface as our source address.
  776. */
  777. if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
  778. struct ifnet *ifp;
  779. ifp = if_get(mopts->imo_ifidx);
  780. if (ifp != NULL) {
  781. if (ifp->if_rdomain == rtable_l2(rtableid))
  782. IFP_TO_IA(ifp, ia);
  783. if (ia == NULL)
  784. return (EADDRNOTAVAIL);
  785. *insrc = &ia->ia_addr.sin_addr;
  786. return (0);
  787. }
  788. }
  789. /*
  790. * If route is known or can be allocated now,
  791. * our src addr is taken from the i/f, else punt.
  792. */
  793. if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
  794. (satosin(&ro->ro_dst)->sin_addr.s_addr != sin->sin_addr.s_addr))) {
  795. rtfree(ro->ro_rt);
  796. ro->ro_rt = NULL;
  797. }
  798. if ((ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL)) {
  799. /* No route yet, so try to acquire one */
  800. ro->ro_dst.sa_family = AF_INET;
  801. ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
  802. satosin(&ro->ro_dst)->sin_addr = sin->sin_addr;
  803. ro->ro_tableid = rtableid;
  804. ro->ro_rt = rtalloc_mpath(&ro->ro_dst, NULL, ro->ro_tableid);
  805. /*
  806. * It is important to zero out the rest of the
  807. * struct sockaddr_in when mixing v6 & v4!
  808. */
  809. sin2 = (struct sockaddr_in *)&ro->ro_dst;
  810. memset(sin2->sin_zero, 0, sizeof(sin2->sin_zero));
  811. }
  812. /*
  813. * If we found a route, use the address
  814. * corresponding to the outgoing interface.
  815. */
  816. if (ro->ro_rt && ro->ro_rt->rt_ifp)
  817. ia = ifatoia(ro->ro_rt->rt_ifa);
  818. if (ia == NULL)
  819. return (EADDRNOTAVAIL);
  820. *insrc = &ia->ia_addr.sin_addr;
  821. return (0);
  822. }
  823. void
  824. in_pcbrehash(struct inpcb *inp)
  825. {
  826. struct inpcbtable *table = inp->inp_table;
  827. int s;
  828. struct inpcbhead *head;
  829. s = splnet();
  830. LIST_REMOVE(inp, inp_lhash);
  831. head = INPCBLHASH(table, inp->inp_lport, inp->inp_rtableid);
  832. LIST_INSERT_HEAD(head, inp, inp_lhash);
  833. LIST_REMOVE(inp, inp_hash);
  834. #ifdef INET6
  835. if (inp->inp_flags & INP_IPV6)
  836. head = IN6PCBHASH(table, &inp->inp_faddr6, inp->inp_fport,
  837. &inp->inp_laddr6, inp->inp_lport,
  838. rtable_l2(inp->inp_rtableid));
  839. else
  840. #endif /* INET6 */
  841. head = INPCBHASH(table, &inp->inp_faddr, inp->inp_fport,
  842. &inp->inp_laddr, inp->inp_lport,
  843. rtable_l2(inp->inp_rtableid));
  844. LIST_INSERT_HEAD(head, inp, inp_hash);
  845. splx(s);
  846. }
  847. int
  848. in_pcbresize(struct inpcbtable *table, int hashsize)
  849. {
  850. u_long nhash, nlhash;
  851. void *nhashtbl, *nlhashtbl, *ohashtbl, *olhashtbl;
  852. struct inpcb *inp0, *inp1;
  853. ohashtbl = table->inpt_hashtbl;
  854. olhashtbl = table->inpt_lhashtbl;
  855. nhashtbl = hashinit(hashsize, M_PCB, M_NOWAIT, &nhash);
  856. nlhashtbl = hashinit(hashsize, M_PCB, M_NOWAIT, &nlhash);
  857. if (nhashtbl == NULL || nlhashtbl == NULL) {
  858. if (nhashtbl != NULL)
  859. free(nhashtbl, M_PCB, 0);
  860. if (nlhashtbl != NULL)
  861. free(nlhashtbl, M_PCB, 0);
  862. return (ENOBUFS);
  863. }
  864. table->inpt_hashtbl = nhashtbl;
  865. table->inpt_lhashtbl = nlhashtbl;
  866. table->inpt_hash = nhash;
  867. table->inpt_lhash = nlhash;
  868. arc4random_buf(&table->inpt_key, sizeof(table->inpt_key));
  869. TAILQ_FOREACH_SAFE(inp0, &table->inpt_queue, inp_queue, inp1) {
  870. in_pcbrehash(inp0);
  871. }
  872. free(ohashtbl, M_PCB, 0);
  873. free(olhashtbl, M_PCB, 0);
  874. return (0);
  875. }
  876. #ifdef DIAGNOSTIC
  877. int in_pcbnotifymiss = 0;
  878. #endif
  879. /*
  880. * The in(6)_pcbhashlookup functions are used to locate connected sockets
  881. * quickly:
  882. * faddr.fport <-> laddr.lport
  883. * No wildcard matching is done so that listening sockets are not found.
  884. * If the functions return NULL in(6)_pcblookup_listen can be used to
  885. * find a listening/bound socket that may accept the connection.
  886. * After those two lookups no other are necessary.
  887. */
  888. struct inpcb *
  889. in_pcbhashlookup(struct inpcbtable *table, struct in_addr faddr,
  890. u_int fport_arg, struct in_addr laddr, u_int lport_arg, u_int rdomain)
  891. {
  892. struct inpcbhead *head;
  893. struct inpcb *inp;
  894. u_int16_t fport = fport_arg, lport = lport_arg;
  895. rdomain = rtable_l2(rdomain); /* convert passed rtableid to rdomain */
  896. head = INPCBHASH(table, &faddr, fport, &laddr, lport, rdomain);
  897. LIST_FOREACH(inp, head, inp_hash) {
  898. #ifdef INET6
  899. if (inp->inp_flags & INP_IPV6)
  900. continue; /*XXX*/
  901. #endif
  902. if (inp->inp_faddr.s_addr == faddr.s_addr &&
  903. inp->inp_fport == fport && inp->inp_lport == lport &&
  904. inp->inp_laddr.s_addr == laddr.s_addr &&
  905. rtable_l2(inp->inp_rtableid) == rdomain) {
  906. /*
  907. * Move this PCB to the head of hash chain so that
  908. * repeated accesses are quicker. This is analogous to
  909. * the historic single-entry PCB cache.
  910. */
  911. if (inp != LIST_FIRST(head)) {
  912. LIST_REMOVE(inp, inp_hash);
  913. LIST_INSERT_HEAD(head, inp, inp_hash);
  914. }
  915. break;
  916. }
  917. }
  918. #ifdef DIAGNOSTIC
  919. if (inp == NULL && in_pcbnotifymiss) {
  920. printf("in_pcbhashlookup: faddr=%08x fport=%d laddr=%08x lport=%d rdom=%d\n",
  921. ntohl(faddr.s_addr), ntohs(fport),
  922. ntohl(laddr.s_addr), ntohs(lport), rdomain);
  923. }
  924. #endif
  925. return (inp);
  926. }
  927. #ifdef INET6
  928. struct inpcb *
  929. in6_pcbhashlookup(struct inpcbtable *table, const struct in6_addr *faddr,
  930. u_int fport_arg, const struct in6_addr *laddr, u_int lport_arg,
  931. u_int rtable)
  932. {
  933. struct inpcbhead *head;
  934. struct inpcb *inp;
  935. u_int16_t fport = fport_arg, lport = lport_arg;
  936. rtable = rtable_l2(rtable); /* convert passed rtableid to rdomain */
  937. head = IN6PCBHASH(table, faddr, fport, laddr, lport, rtable);
  938. LIST_FOREACH(inp, head, inp_hash) {
  939. if (!(inp->inp_flags & INP_IPV6))
  940. continue;
  941. if (IN6_ARE_ADDR_EQUAL(&inp->inp_faddr6, faddr) &&
  942. inp->inp_fport == fport && inp->inp_lport == lport &&
  943. IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, laddr) &&
  944. rtable_l2(inp->inp_rtableid) == rtable) {
  945. /*
  946. * Move this PCB to the head of hash chain so that
  947. * repeated accesses are quicker. This is analogous to
  948. * the historic single-entry PCB cache.
  949. */
  950. if (inp != LIST_FIRST(head)) {
  951. LIST_REMOVE(inp, inp_hash);
  952. LIST_INSERT_HEAD(head, inp, inp_hash);
  953. }
  954. break;
  955. }
  956. }
  957. #ifdef DIAGNOSTIC
  958. if (inp == NULL && in_pcbnotifymiss) {
  959. printf("in6_pcbhashlookup: faddr=");
  960. printf(" fport=%d laddr=", ntohs(fport));
  961. printf(" lport=%d\n", ntohs(lport));
  962. }
  963. #endif
  964. return (inp);
  965. }
  966. #endif /* INET6 */
  967. /*
  968. * The in(6)_pcblookup_listen functions are used to locate listening
  969. * sockets quickly. This are sockets with unspecified foreign address
  970. * and port:
  971. * *.* <-> laddr.lport
  972. * *.* <-> *.lport
  973. */
  974. struct inpcb *
  975. in_pcblookup_listen(struct inpcbtable *table, struct in_addr laddr,
  976. u_int lport_arg, int reverse, struct mbuf *m, u_int rdomain)
  977. {
  978. struct inpcbhead *head;
  979. struct in_addr *key1, *key2;
  980. struct inpcb *inp;
  981. u_int16_t lport = lport_arg;
  982. rdomain = rtable_l2(rdomain); /* convert passed rtableid to rdomain */
  983. #if NPF > 0
  984. if (m && m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) {
  985. struct pf_divert *divert;
  986. if ((divert = pf_find_divert(m)) == NULL)
  987. return (NULL);
  988. key1 = key2 = &divert->addr.v4;
  989. lport = divert->port;
  990. } else
  991. #endif
  992. if (reverse) {
  993. key1 = &zeroin_addr;
  994. key2 = &laddr;
  995. } else {
  996. key1 = &laddr;
  997. key2 = &zeroin_addr;
  998. }
  999. head = INPCBHASH(table, &zeroin_addr, 0, key1, lport, rdomain);
  1000. LIST_FOREACH(inp, head, inp_hash) {
  1001. #ifdef INET6
  1002. if (inp->inp_flags & INP_IPV6)
  1003. continue; /*XXX*/
  1004. #endif
  1005. if (inp->inp_lport == lport && inp->inp_fport == 0 &&
  1006. inp->inp_laddr.s_addr == key1->s_addr &&
  1007. inp->inp_faddr.s_addr == INADDR_ANY &&
  1008. rtable_l2(inp->inp_rtableid) == rdomain)
  1009. break;
  1010. }
  1011. if (inp == NULL && key1->s_addr != key2->s_addr) {
  1012. head = INPCBHASH(table, &zeroin_addr, 0, key2, lport, rdomain);
  1013. LIST_FOREACH(inp, head, inp_hash) {
  1014. #ifdef INET6
  1015. if (inp->inp_flags & INP_IPV6)
  1016. continue; /*XXX*/
  1017. #endif
  1018. if (inp->inp_lport == lport && inp->inp_fport == 0 &&
  1019. inp->inp_laddr.s_addr == key2->s_addr &&
  1020. inp->inp_faddr.s_addr == INADDR_ANY &&
  1021. rtable_l2(inp->inp_rtableid) == rdomain)
  1022. break;
  1023. }
  1024. }
  1025. #ifdef DIAGNOSTIC
  1026. if (inp == NULL && in_pcbnotifymiss) {
  1027. printf("in_pcblookup_listen: laddr=%08x lport=%d\n",
  1028. ntohl(laddr.s_addr), ntohs(lport));
  1029. }
  1030. #endif
  1031. /*
  1032. * Move this PCB to the head of hash chain so that
  1033. * repeated accesses are quicker. This is analogous to
  1034. * the historic single-entry PCB cache.
  1035. */
  1036. if (inp != NULL && inp != LIST_FIRST(head)) {
  1037. LIST_REMOVE(inp, inp_hash);
  1038. LIST_INSERT_HEAD(head, inp, inp_hash);
  1039. }
  1040. return (inp);
  1041. }
  1042. #ifdef INET6
  1043. struct inpcb *
  1044. in6_pcblookup_listen(struct inpcbtable *table, struct in6_addr *laddr,
  1045. u_int lport_arg, int reverse, struct mbuf *m, u_int rtable)
  1046. {
  1047. struct inpcbhead *head;
  1048. struct in6_addr *key1, *key2;
  1049. struct inpcb *inp;
  1050. u_int16_t lport = lport_arg;
  1051. rtable = rtable_l2(rtable); /* convert passed rtableid to rdomain */
  1052. #if NPF > 0
  1053. if (m && m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) {
  1054. struct pf_divert *divert;
  1055. if ((divert = pf_find_divert(m)) == NULL)
  1056. return (NULL);
  1057. key1 = key2 = &divert->addr.v6;
  1058. lport = divert->port;
  1059. } else
  1060. #endif
  1061. if (reverse) {
  1062. key1 = &zeroin6_addr;
  1063. key2 = laddr;
  1064. } else {
  1065. key1 = laddr;
  1066. key2 = &zeroin6_addr;
  1067. }
  1068. head = IN6PCBHASH(table, &zeroin6_addr, 0, key1, lport, rtable);
  1069. LIST_FOREACH(inp, head, inp_hash) {
  1070. if (!(inp->inp_flags & INP_IPV6))
  1071. continue;
  1072. if (inp->inp_lport == lport && inp->inp_fport == 0 &&
  1073. IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, key1) &&
  1074. IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6) &&
  1075. rtable_l2(inp->inp_rtableid) == rtable)
  1076. break;
  1077. }
  1078. if (inp == NULL && ! IN6_ARE_ADDR_EQUAL(key1, key2)) {
  1079. head = IN6PCBHASH(table, &zeroin6_addr, 0, key2, lport, rtable);
  1080. LIST_FOREACH(inp, head, inp_hash) {
  1081. if (!(inp->inp_flags & INP_IPV6))
  1082. continue;
  1083. if (inp->inp_lport == lport && inp->inp_fport == 0 &&
  1084. IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, key2) &&
  1085. IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6) &&
  1086. rtable_l2(inp->inp_rtableid) == rtable)
  1087. break;
  1088. }
  1089. }
  1090. #ifdef DIAGNOSTIC
  1091. if (inp == NULL && in_pcbnotifymiss) {
  1092. printf("in6_pcblookup_listen: laddr= lport=%d\n",
  1093. ntohs(lport));
  1094. }
  1095. #endif
  1096. /*
  1097. * Move this PCB to the head of hash chain so that
  1098. * repeated accesses are quicker. This is analogous to
  1099. * the historic single-entry PCB cache.
  1100. */
  1101. if (inp != NULL && inp != LIST_FIRST(head)) {
  1102. LIST_REMOVE(inp, inp_hash);
  1103. LIST_INSERT_HEAD(head, inp, inp_hash);
  1104. }
  1105. return (inp);
  1106. }
  1107. #endif /* INET6 */