in_pcb.c revision 1.53 1 /* $NetBSD: in_pcb.c,v 1.53 1998/09/30 21:52:24 tls Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1991, 1993, 1995
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)in_pcb.c 8.4 (Berkeley) 5/24/95
36 */
37
38 /*-
39 * Copyright (c) 1998 The NetBSD Foundation, Inc.
40 * All rights reserved.
41 *
42 * This code is derived from software contributed to The NetBSD Foundation
43 * by Public Access Networks Corporation ("Panix"). It was developed under
44 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * This product includes software developed by the NetBSD
57 * Foundation, Inc. and its contributors.
58 * 4. Neither the name of The NetBSD Foundation nor the names of its
59 * contributors may be used to endorse or promote products derived
60 * from this software without specific prior written permission.
61 *
62 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
63 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
64 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
65 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
66 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
67 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
68 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
69 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
70 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
71 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
72 * POSSIBILITY OF SUCH DAMAGE.
73 */
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/malloc.h>
78 #include <sys/mbuf.h>
79 #include <sys/protosw.h>
80 #include <sys/socket.h>
81 #include <sys/socketvar.h>
82 #include <sys/ioctl.h>
83 #include <sys/errno.h>
84 #include <sys/time.h>
85 #include <sys/pool.h>
86 #include <sys/proc.h>
87
88 #include <net/if.h>
89 #include <net/route.h>
90
91 #include <netinet/in.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet/in_var.h>
96 #include <netinet/ip_var.h>
97
98 struct in_addr zeroin_addr;
99
100 #define INPCBHASH_BIND(table, laddr, lport) \
101 &(table)->inpt_bindhashtbl[ \
102 ((ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_bindhash]
103 #define INPCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
104 &(table)->inpt_connecthashtbl[ \
105 ((ntohl((faddr).s_addr) + ntohs(fport)) + \
106 (ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_connecthash]
107
108 struct inpcb *
109 in_pcblookup_port __P((struct inpcbtable *,
110 struct in_addr, u_int, int));
111
112 int anonportmin = IPPORT_ANONMIN;
113 int anonportmax = IPPORT_ANONMAX;
114
115 struct pool inpcb_pool;
116
117 void
118 in_pcbinit(table, bindhashsize, connecthashsize)
119 struct inpcbtable *table;
120 int bindhashsize, connecthashsize;
121 {
122 static int inpcb_pool_initialized;
123
124 if (inpcb_pool_initialized == 0) {
125 pool_init(&inpcb_pool, sizeof(struct inpcb), 0, 0, 0,
126 "inpcbpl", 0, NULL, NULL, M_PCB);
127 inpcb_pool_initialized = 1;
128 }
129
130 CIRCLEQ_INIT(&table->inpt_queue);
131 table->inpt_bindhashtbl =
132 hashinit(bindhashsize, M_PCB, M_WAITOK, &table->inpt_bindhash);
133 table->inpt_connecthashtbl =
134 hashinit(connecthashsize, M_PCB, M_WAITOK, &table->inpt_connecthash);
135 table->inpt_lastlow = IPPORT_RESERVEDMAX;
136 table->inpt_lastport = (u_int16_t)anonportmax;
137 }
138
139 int
140 in_pcballoc(so, v)
141 struct socket *so;
142 void *v;
143 {
144 struct inpcbtable *table = v;
145 register struct inpcb *inp;
146 int s;
147
148 inp = pool_get(&inpcb_pool, PR_NOWAIT);
149 if (inp == NULL)
150 return (ENOBUFS);
151 bzero((caddr_t)inp, sizeof(*inp));
152 inp->inp_table = table;
153 inp->inp_socket = so;
154 inp->inp_errormtu = -1;
155 so->so_pcb = inp;
156 s = splnet();
157 CIRCLEQ_INSERT_HEAD(&table->inpt_queue, inp, inp_queue);
158 in_pcbstate(inp, INP_ATTACHED);
159 splx(s);
160 return (0);
161 }
162
163 int
164 in_pcbbind(v, nam, p)
165 void *v;
166 struct mbuf *nam;
167 struct proc *p;
168 {
169 register struct inpcb *inp = v;
170 register struct socket *so = inp->inp_socket;
171 register struct inpcbtable *table = inp->inp_table;
172 register struct sockaddr_in *sin;
173 u_int16_t lport = 0;
174 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
175 #ifndef IPNOPRIVPORTS
176 int error;
177 #endif
178
179 if (in_ifaddr.tqh_first == 0)
180 return (EADDRNOTAVAIL);
181 if (inp->inp_lport || !in_nullhost(inp->inp_laddr))
182 return (EINVAL);
183 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
184 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
185 (so->so_options & SO_ACCEPTCONN) == 0))
186 wild = INPLOOKUP_WILDCARD;
187 if (nam == 0)
188 goto noname;
189 sin = mtod(nam, struct sockaddr_in *);
190 if (nam->m_len != sizeof (*sin))
191 return (EINVAL);
192 #ifdef notdef
193 /*
194 * We should check the family, but old programs
195 * incorrectly fail to initialize it.
196 */
197 if (sin->sin_family != AF_INET)
198 return (EAFNOSUPPORT);
199 #endif
200 lport = sin->sin_port;
201 if (IN_MULTICAST(sin->sin_addr.s_addr)) {
202 /*
203 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
204 * allow complete duplication of binding if
205 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
206 * and a multicast address is bound on both
207 * new and duplicated sockets.
208 */
209 if (so->so_options & SO_REUSEADDR)
210 reuseport = SO_REUSEADDR|SO_REUSEPORT;
211 } else if (!in_nullhost(sin->sin_addr)) {
212 sin->sin_port = 0; /* yech... */
213 if (ifa_ifwithaddr(sintosa(sin)) == 0)
214 return (EADDRNOTAVAIL);
215 }
216 if (lport) {
217 struct inpcb *t;
218 #ifndef IPNOPRIVPORTS
219 /* GROSS */
220 if (ntohs(lport) < IPPORT_RESERVED &&
221 (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))))
222 return (EACCES);
223 #endif
224 t = in_pcblookup_port(table, sin->sin_addr, lport, wild);
225 if (t && (reuseport & t->inp_socket->so_options) == 0)
226 return (EADDRINUSE);
227 }
228 inp->inp_laddr = sin->sin_addr;
229
230 noname:
231 if (lport == 0) {
232 int cnt;
233 u_int16_t min, max;
234 u_int16_t *lastport;
235
236 if (inp->inp_flags & INP_LOWPORT) {
237 #ifndef IPNOPRIVPORTS
238 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
239 return (EACCES);
240 #endif
241 min = IPPORT_RESERVEDMIN;
242 max = IPPORT_RESERVEDMAX;
243 lastport = &table->inpt_lastlow;
244 } else {
245 min = anonportmin;
246 max = anonportmax;
247 lastport = &table->inpt_lastport;
248 }
249 if (min > max) { /* sanity check */
250 u_int16_t swp;
251
252 swp = min;
253 min = max;
254 max = swp;
255 }
256
257 lport = *lastport - 1;
258 for (cnt = max - min + 1; cnt; cnt--, lport--) {
259 if (lport < min || lport > max)
260 lport = max;
261 if (!in_pcblookup_port(table, inp->inp_laddr,
262 htons(lport), wild))
263 goto found;
264 }
265 if (!in_nullhost(inp->inp_laddr))
266 inp->inp_laddr.s_addr = INADDR_ANY;
267 return (EAGAIN);
268 found:
269 inp->inp_flags |= INP_ANONPORT;
270 *lastport = lport;
271 lport = htons(lport);
272 }
273 inp->inp_lport = lport;
274 in_pcbstate(inp, INP_BOUND);
275 return (0);
276 }
277
278 /*
279 * Connect from a socket to a specified address.
280 * Both address and port must be specified in argument sin.
281 * If don't have a local address for this socket yet,
282 * then pick one.
283 */
284 int
285 in_pcbconnect(v, nam)
286 register void *v;
287 struct mbuf *nam;
288 {
289 register struct inpcb *inp = v;
290 struct in_ifaddr *ia;
291 struct sockaddr_in *ifaddr = NULL;
292 register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
293 int error;
294
295 if (nam->m_len != sizeof (*sin))
296 return (EINVAL);
297 if (sin->sin_family != AF_INET)
298 return (EAFNOSUPPORT);
299 if (sin->sin_port == 0)
300 return (EADDRNOTAVAIL);
301 if (in_ifaddr.tqh_first != 0) {
302 /*
303 * If the destination address is INADDR_ANY,
304 * use any local address (likely loopback).
305 * If the supplied address is INADDR_BROADCAST,
306 * use the broadcast address of an interface
307 * which supports broadcast. (loopback does not)
308 */
309
310 if (in_nullhost(sin->sin_addr))
311 sin->sin_addr = in_ifaddr.tqh_first->ia_broadaddr.sin_addr;
312 else if (sin->sin_addr.s_addr == INADDR_BROADCAST)
313 for (ia = in_ifaddr.tqh_first; ia != NULL;
314 ia = ia->ia_list.tqe_next)
315 if (ia->ia_ifp->if_flags & IFF_BROADCAST) {
316 sin->sin_addr = ia->ia_broadaddr.sin_addr;
317 break;
318 }
319 }
320 /*
321 * If we haven't bound which network number to use as ours,
322 * we will use the number of the outgoing interface.
323 * This depends on having done a routing lookup, which
324 * we will probably have to do anyway, so we might
325 * as well do it now. On the other hand if we are
326 * sending to multiple destinations we may have already
327 * done the lookup, so see if we can use the route
328 * from before. In any case, we only
329 * chose a port number once, even if sending to multiple
330 * destinations.
331 */
332 if (in_nullhost(inp->inp_laddr)) {
333 register struct route *ro;
334
335 ia = (struct in_ifaddr *)0;
336 /*
337 * If route is known or can be allocated now,
338 * our src addr is taken from the i/f, else punt.
339 */
340 ro = &inp->inp_route;
341 if (ro->ro_rt &&
342 (!in_hosteq(satosin(&ro->ro_dst)->sin_addr,
343 sin->sin_addr) ||
344 inp->inp_socket->so_options & SO_DONTROUTE)) {
345 RTFREE(ro->ro_rt);
346 ro->ro_rt = (struct rtentry *)0;
347 }
348 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
349 (ro->ro_rt == (struct rtentry *)0 ||
350 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
351 /* No route yet, so try to acquire one */
352 ro->ro_dst.sa_family = AF_INET;
353 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
354 satosin(&ro->ro_dst)->sin_addr = sin->sin_addr;
355 rtalloc(ro);
356 }
357 /*
358 * If we found a route, use the address
359 * corresponding to the outgoing interface
360 * unless it is the loopback (in case a route
361 * to our address on another net goes to loopback).
362 *
363 * XXX Is this still true? Do we care?
364 */
365 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
366 ia = ifatoia(ro->ro_rt->rt_ifa);
367 if (ia == 0) {
368 u_int16_t fport = sin->sin_port;
369
370 sin->sin_port = 0;
371 ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
372 sin->sin_port = fport;
373 if (ia == 0)
374 /* Find 1st non-loopback AF_INET address */
375 for (ia = in_ifaddr.tqh_first ; ia != NULL ;
376 ia = ia->ia_list.tqe_next)
377 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
378 break;
379 if (ia == 0)
380 return (EADDRNOTAVAIL);
381 }
382 /*
383 * If the destination address is multicast and an outgoing
384 * interface has been set as a multicast option, use the
385 * address of that interface as our source address.
386 */
387 if (IN_MULTICAST(sin->sin_addr.s_addr) &&
388 inp->inp_moptions != NULL) {
389 struct ip_moptions *imo;
390 struct ifnet *ifp;
391
392 imo = inp->inp_moptions;
393 if (imo->imo_multicast_ifp != NULL) {
394 ifp = imo->imo_multicast_ifp;
395 IFP_TO_IA(ifp, ia); /* XXX */
396 if (ia == 0)
397 return (EADDRNOTAVAIL);
398 }
399 }
400 ifaddr = satosin(&ia->ia_addr);
401 }
402 if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port,
403 !in_nullhost(inp->inp_laddr) ? inp->inp_laddr : ifaddr->sin_addr,
404 inp->inp_lport) != 0)
405 return (EADDRINUSE);
406 if (in_nullhost(inp->inp_laddr)) {
407 if (inp->inp_lport == 0) {
408 error = in_pcbbind(inp, (struct mbuf *)0,
409 (struct proc *)0);
410 /*
411 * This used to ignore the return value
412 * completely, but we need to check for
413 * ephemeral port shortage.
414 * XXX Should we check for other errors, too?
415 */
416 if (error == EAGAIN)
417 return (error);
418 }
419 inp->inp_laddr = ifaddr->sin_addr;
420 }
421 inp->inp_faddr = sin->sin_addr;
422 inp->inp_fport = sin->sin_port;
423 in_pcbstate(inp, INP_CONNECTED);
424 return (0);
425 }
426
427 void
428 in_pcbdisconnect(v)
429 void *v;
430 {
431 struct inpcb *inp = v;
432
433 inp->inp_faddr = zeroin_addr;
434 inp->inp_fport = 0;
435 in_pcbstate(inp, INP_BOUND);
436 if (inp->inp_socket->so_state & SS_NOFDREF)
437 in_pcbdetach(inp);
438 }
439
440 void
441 in_pcbdetach(v)
442 void *v;
443 {
444 struct inpcb *inp = v;
445 struct socket *so = inp->inp_socket;
446 int s;
447
448 so->so_pcb = 0;
449 sofree(so);
450 if (inp->inp_options)
451 (void)m_free(inp->inp_options);
452 if (inp->inp_route.ro_rt)
453 rtfree(inp->inp_route.ro_rt);
454 ip_freemoptions(inp->inp_moptions);
455 s = splnet();
456 in_pcbstate(inp, INP_ATTACHED);
457 CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue);
458 splx(s);
459 pool_put(&inpcb_pool, inp);
460 }
461
462 void
463 in_setsockaddr(inp, nam)
464 register struct inpcb *inp;
465 struct mbuf *nam;
466 {
467 register struct sockaddr_in *sin;
468
469 nam->m_len = sizeof (*sin);
470 sin = mtod(nam, struct sockaddr_in *);
471 bzero((caddr_t)sin, sizeof (*sin));
472 sin->sin_family = AF_INET;
473 sin->sin_len = sizeof(*sin);
474 sin->sin_port = inp->inp_lport;
475 sin->sin_addr = inp->inp_laddr;
476 }
477
478 void
479 in_setpeeraddr(inp, nam)
480 struct inpcb *inp;
481 struct mbuf *nam;
482 {
483 register struct sockaddr_in *sin;
484
485 nam->m_len = sizeof (*sin);
486 sin = mtod(nam, struct sockaddr_in *);
487 bzero((caddr_t)sin, sizeof (*sin));
488 sin->sin_family = AF_INET;
489 sin->sin_len = sizeof(*sin);
490 sin->sin_port = inp->inp_fport;
491 sin->sin_addr = inp->inp_faddr;
492 }
493
494 /*
495 * Pass some notification to all connections of a protocol
496 * associated with address dst. The local address and/or port numbers
497 * may be specified to limit the search. The "usual action" will be
498 * taken, depending on the ctlinput cmd. The caller must filter any
499 * cmds that are uninteresting (e.g., no error in the map).
500 * Call the protocol specific routine (if any) to report
501 * any errors for each matching socket.
502 *
503 * Must be called at splsoftnet.
504 */
505 int
506 in_pcbnotify(table, faddr, fport_arg, laddr, lport_arg, errno, notify)
507 struct inpcbtable *table;
508 struct in_addr faddr, laddr;
509 u_int fport_arg, lport_arg;
510 int errno;
511 void (*notify) __P((struct inpcb *, int));
512 {
513 struct inpcbhead *head;
514 register struct inpcb *inp, *ninp;
515 u_int16_t fport = fport_arg, lport = lport_arg;
516 int nmatch;
517
518 if (in_nullhost(faddr) || notify == 0)
519 return (0);
520
521 nmatch = 0;
522 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
523 for (inp = head->lh_first; inp != NULL; inp = ninp) {
524 ninp = inp->inp_hash.le_next;
525 if (in_hosteq(inp->inp_faddr, faddr) &&
526 inp->inp_fport == fport &&
527 inp->inp_lport == lport &&
528 in_hosteq(inp->inp_laddr, laddr)) {
529 (*notify)(inp, errno);
530 nmatch++;
531 }
532 }
533 return (nmatch);
534 }
535
536 void
537 in_pcbnotifyall(table, faddr, errno, notify)
538 struct inpcbtable *table;
539 struct in_addr faddr;
540 int errno;
541 void (*notify) __P((struct inpcb *, int));
542 {
543 register struct inpcb *inp, *ninp;
544
545 if (in_nullhost(faddr) || notify == 0)
546 return;
547
548 for (inp = table->inpt_queue.cqh_first;
549 inp != (struct inpcb *)&table->inpt_queue;
550 inp = ninp) {
551 ninp = inp->inp_queue.cqe_next;
552 if (in_hosteq(inp->inp_faddr, faddr))
553 (*notify)(inp, errno);
554 }
555 }
556
557 /*
558 * Check for alternatives when higher level complains
559 * about service problems. For now, invalidate cached
560 * routing information. If the route was created dynamically
561 * (by a redirect), time to try a default gateway again.
562 */
563 void
564 in_losing(inp)
565 struct inpcb *inp;
566 {
567 register struct rtentry *rt;
568 struct rt_addrinfo info;
569
570 if ((rt = inp->inp_route.ro_rt)) {
571 inp->inp_route.ro_rt = 0;
572 bzero((caddr_t)&info, sizeof(info));
573 info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst;
574 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
575 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
576 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
577 if (rt->rt_flags & RTF_DYNAMIC)
578 (void) rtrequest(RTM_DELETE, rt_key(rt),
579 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
580 (struct rtentry **)0);
581 else
582 /*
583 * A new route can be allocated
584 * the next time output is attempted.
585 */
586 rtfree(rt);
587 }
588 }
589
590 /*
591 * After a routing change, flush old routing
592 * and allocate a (hopefully) better one.
593 */
594 void
595 in_rtchange(inp, errno)
596 register struct inpcb *inp;
597 int errno;
598 {
599
600 if (inp->inp_route.ro_rt) {
601 rtfree(inp->inp_route.ro_rt);
602 inp->inp_route.ro_rt = 0;
603 /*
604 * A new route can be allocated the next time
605 * output is attempted.
606 */
607 }
608 /* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
609 }
610
611 struct inpcb *
612 in_pcblookup_port(table, laddr, lport_arg, flags)
613 struct inpcbtable *table;
614 struct in_addr laddr;
615 u_int lport_arg;
616 int flags;
617 {
618 register struct inpcb *inp, *match = 0;
619 int matchwild = 3, wildcard;
620 u_int16_t lport = lport_arg;
621
622 for (inp = table->inpt_queue.cqh_first;
623 inp != (struct inpcb *)&table->inpt_queue;
624 inp = inp->inp_queue.cqe_next) {
625 if (inp->inp_lport != lport)
626 continue;
627 wildcard = 0;
628 if (!in_nullhost(inp->inp_faddr))
629 wildcard++;
630 if (in_nullhost(inp->inp_laddr)) {
631 if (!in_nullhost(laddr))
632 wildcard++;
633 } else {
634 if (in_nullhost(laddr))
635 wildcard++;
636 else {
637 if (!in_hosteq(inp->inp_laddr, laddr))
638 continue;
639 }
640 }
641 if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0)
642 continue;
643 if (wildcard < matchwild) {
644 match = inp;
645 matchwild = wildcard;
646 if (matchwild == 0)
647 break;
648 }
649 }
650 return (match);
651 }
652
653 #ifdef DIAGNOSTIC
654 int in_pcbnotifymiss = 0;
655 #endif
656
657 struct inpcb *
658 in_pcblookup_connect(table, faddr, fport_arg, laddr, lport_arg)
659 struct inpcbtable *table;
660 struct in_addr faddr, laddr;
661 u_int fport_arg, lport_arg;
662 {
663 struct inpcbhead *head;
664 register struct inpcb *inp;
665 u_int16_t fport = fport_arg, lport = lport_arg;
666
667 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
668 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
669 if (in_hosteq(inp->inp_faddr, faddr) &&
670 inp->inp_fport == fport &&
671 inp->inp_lport == lport &&
672 in_hosteq(inp->inp_laddr, laddr))
673 goto out;
674 }
675 #ifdef DIAGNOSTIC
676 if (in_pcbnotifymiss) {
677 printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n",
678 ntohl(faddr.s_addr), ntohs(fport),
679 ntohl(laddr.s_addr), ntohs(lport));
680 }
681 #endif
682 return (0);
683
684 out:
685 /* Move this PCB to the head of hash chain. */
686 if (inp != head->lh_first) {
687 LIST_REMOVE(inp, inp_hash);
688 LIST_INSERT_HEAD(head, inp, inp_hash);
689 }
690 return (inp);
691 }
692
693 struct inpcb *
694 in_pcblookup_bind(table, laddr, lport_arg)
695 struct inpcbtable *table;
696 struct in_addr laddr;
697 u_int lport_arg;
698 {
699 struct inpcbhead *head;
700 register struct inpcb *inp;
701 u_int16_t lport = lport_arg;
702
703 head = INPCBHASH_BIND(table, laddr, lport);
704 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
705 if (inp->inp_lport == lport &&
706 in_hosteq(inp->inp_laddr, laddr))
707 goto out;
708 }
709 head = INPCBHASH_BIND(table, zeroin_addr, lport);
710 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
711 if (inp->inp_lport == lport &&
712 in_hosteq(inp->inp_laddr, zeroin_addr))
713 goto out;
714 }
715 #ifdef DIAGNOSTIC
716 if (in_pcbnotifymiss) {
717 printf("in_pcblookup_bind: laddr=%08x lport=%d\n",
718 ntohl(laddr.s_addr), ntohs(lport));
719 }
720 #endif
721 return (0);
722
723 out:
724 /* Move this PCB to the head of hash chain. */
725 if (inp != head->lh_first) {
726 LIST_REMOVE(inp, inp_hash);
727 LIST_INSERT_HEAD(head, inp, inp_hash);
728 }
729 return (inp);
730 }
731
732 void
733 in_pcbstate(inp, state)
734 struct inpcb *inp;
735 int state;
736 {
737
738 if (inp->inp_state > INP_ATTACHED)
739 LIST_REMOVE(inp, inp_hash);
740
741 switch (state) {
742 case INP_BOUND:
743 LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table,
744 inp->inp_laddr, inp->inp_lport), inp, inp_hash);
745 break;
746 case INP_CONNECTED:
747 LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table,
748 inp->inp_faddr, inp->inp_fport,
749 inp->inp_laddr, inp->inp_lport), inp, inp_hash);
750 break;
751 }
752
753 inp->inp_state = state;
754 }
755
756 struct rtentry *
757 in_pcbrtentry(inp)
758 struct inpcb *inp;
759 {
760 struct route *ro;
761
762 ro = &inp->inp_route;
763
764 if (ro->ro_rt == NULL) {
765 /*
766 * No route yet, so try to acquire one.
767 */
768 if (!in_nullhost(inp->inp_faddr)) {
769 ro->ro_dst.sa_family = AF_INET;
770 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
771 satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr;
772 rtalloc(ro);
773 }
774 }
775 return (ro->ro_rt);
776 }
777