in_pcb.c revision 1.39.2.4 1 /* $NetBSD: in_pcb.c,v 1.39.2.4 1999/01/18 03:22:58 cgd Exp $ */
2
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. 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.2 (Berkeley) 1/4/94
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/proc.h>
86
87 #include <net/if.h>
88 #include <net/route.h>
89
90 #include <netinet/in.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/ip.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet/in_var.h>
95 #include <netinet/ip_var.h>
96
97 struct in_addr zeroin_addr;
98
99 #define INPCBHASH_BIND(table, laddr, lport) \
100 &(table)->inpt_bindhashtbl[ \
101 ((ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_bindhash]
102 #define INPCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
103 &(table)->inpt_connecthashtbl[ \
104 ((ntohl((faddr).s_addr) + ntohs(fport)) + \
105 (ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_connecthash]
106
107 struct inpcb *
108 in_pcblookup_port __P((struct inpcbtable *,
109 struct in_addr, u_int, int));
110
111 void
112 in_pcbinit(table, bindhashsize, connecthashsize)
113 struct inpcbtable *table;
114 int bindhashsize, connecthashsize;
115 {
116
117 CIRCLEQ_INIT(&table->inpt_queue);
118 table->inpt_bindhashtbl =
119 hashinit(bindhashsize, M_PCB, &table->inpt_bindhash);
120 table->inpt_connecthashtbl =
121 hashinit(connecthashsize, M_PCB, &table->inpt_connecthash);
122 table->inpt_lastport = IPPORT_RESERVED;
123 }
124
125 int
126 in_pcballoc(so, v)
127 struct socket *so;
128 void *v;
129 {
130 struct inpcbtable *table = v;
131 register struct inpcb *inp;
132 int s;
133
134 MALLOC(inp, struct inpcb *, sizeof(*inp), M_PCB, M_WAITOK);
135 if (inp == NULL)
136 return (ENOBUFS);
137 bzero((caddr_t)inp, sizeof(*inp));
138 inp->inp_table = table;
139 inp->inp_socket = so;
140 inp->inp_errormtu = -1;
141 so->so_pcb = inp;
142 s = splnet();
143 CIRCLEQ_INSERT_HEAD(&table->inpt_queue, inp, inp_queue);
144 in_pcbstate(inp, INP_ATTACHED);
145 splx(s);
146 return (0);
147 }
148
149 int
150 in_pcbbind(v, nam, p)
151 void *v;
152 struct mbuf *nam;
153 struct proc *p;
154 {
155 register struct inpcb *inp = v;
156 register struct socket *so = inp->inp_socket;
157 register struct inpcbtable *table = inp->inp_table;
158 register struct sockaddr_in *sin;
159 u_int16_t lport = 0;
160 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
161 #ifndef IPNOPRIVPORTS
162 int error;
163 #endif
164
165 if (in_ifaddr.tqh_first == 0)
166 return (EADDRNOTAVAIL);
167 if (inp->inp_lport || !in_nullhost(inp->inp_laddr))
168 return (EINVAL);
169 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
170 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
171 (so->so_options & SO_ACCEPTCONN) == 0))
172 wild = INPLOOKUP_WILDCARD;
173 if (nam == 0)
174 goto noname;
175 sin = mtod(nam, struct sockaddr_in *);
176 if (nam->m_len != sizeof (*sin))
177 return (EINVAL);
178 #ifdef notdef
179 /*
180 * We should check the family, but old programs
181 * incorrectly fail to initialize it.
182 */
183 if (sin->sin_family != AF_INET)
184 return (EAFNOSUPPORT);
185 #endif
186 lport = sin->sin_port;
187 if (IN_MULTICAST(sin->sin_addr.s_addr)) {
188 /*
189 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
190 * allow complete duplication of binding if
191 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
192 * and a multicast address is bound on both
193 * new and duplicated sockets.
194 */
195 if (so->so_options & SO_REUSEADDR)
196 reuseport = SO_REUSEADDR|SO_REUSEPORT;
197 } else if (!in_nullhost(sin->sin_addr)) {
198 sin->sin_port = 0; /* yech... */
199 if (ifa_ifwithaddr(sintosa(sin)) == 0)
200 return (EADDRNOTAVAIL);
201 }
202 if (lport) {
203 struct inpcb *t;
204 #ifndef IPNOPRIVPORTS
205 /* GROSS */
206 if (ntohs(lport) < IPPORT_RESERVED &&
207 (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))))
208 return (EACCES);
209 #endif
210 t = in_pcblookup_port(table, sin->sin_addr, lport, wild);
211 if (t && (reuseport & t->inp_socket->so_options) == 0)
212 return (EADDRINUSE);
213 }
214 inp->inp_laddr = sin->sin_addr;
215 noname:
216 if (lport == 0) {
217 for (lport = table->inpt_lastport + 1;
218 lport < IPPORT_USERRESERVED; lport++)
219 if (!in_pcblookup_port(table, inp->inp_laddr,
220 htons(lport), wild))
221 goto found;
222 for (lport = IPPORT_RESERVED;
223 lport <= table->inpt_lastport; lport++)
224 if (!in_pcblookup_port(table, inp->inp_laddr,
225 htons(lport), wild))
226 goto found;
227 if (!in_nullhost(inp->inp_laddr))
228 inp->inp_laddr.s_addr = INADDR_ANY;
229 return (EAGAIN);
230 found:
231 table->inpt_lastport = lport;
232 lport = htons(lport);
233 }
234 inp->inp_lport = lport;
235 in_pcbstate(inp, INP_BOUND);
236 return (0);
237 }
238
239 /*
240 * Connect from a socket to a specified address.
241 * Both address and port must be specified in argument sin.
242 * If don't have a local address for this socket yet,
243 * then pick one.
244 */
245 int
246 in_pcbconnect(v, nam)
247 register void *v;
248 struct mbuf *nam;
249 {
250 register struct inpcb *inp = v;
251 struct in_ifaddr *ia;
252 struct sockaddr_in *ifaddr = NULL;
253 register struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
254 int error;
255
256 if (nam->m_len != sizeof (*sin))
257 return (EINVAL);
258 if (sin->sin_family != AF_INET)
259 return (EAFNOSUPPORT);
260 if (sin->sin_port == 0)
261 return (EADDRNOTAVAIL);
262 if (in_ifaddr.tqh_first != 0) {
263 /*
264 * If the destination address is INADDR_ANY,
265 * use any local address (likely loopback).
266 * If the supplied address is INADDR_BROADCAST,
267 * use the broadcast address of an interface
268 * which supports broadcast. (loopback does not)
269 */
270
271 if (in_nullhost(sin->sin_addr))
272 sin->sin_addr = in_ifaddr.tqh_first->ia_addr.sin_addr;
273 else if (sin->sin_addr.s_addr == INADDR_BROADCAST)
274 for (ia = in_ifaddr.tqh_first; ia != NULL;
275 ia = ia->ia_list.tqe_next)
276 if (ia->ia_ifp->if_flags & IFF_BROADCAST) {
277 sin->sin_addr = ia->ia_broadaddr.sin_addr;
278 break;
279 }
280 }
281 /*
282 * If we haven't bound which network number to use as ours,
283 * we will use the number of the outgoing interface.
284 * This depends on having done a routing lookup, which
285 * we will probably have to do anyway, so we might
286 * as well do it now. On the other hand if we are
287 * sending to multiple destinations we may have already
288 * done the lookup, so see if we can use the route
289 * from before. In any case, we only
290 * chose a port number once, even if sending to multiple
291 * destinations.
292 */
293 if (in_nullhost(inp->inp_laddr)) {
294 register struct route *ro;
295
296 ia = (struct in_ifaddr *)0;
297 /*
298 * If route is known or can be allocated now,
299 * our src addr is taken from the i/f, else punt.
300 */
301 ro = &inp->inp_route;
302 if (ro->ro_rt &&
303 (!in_hosteq(satosin(&ro->ro_dst)->sin_addr,
304 sin->sin_addr) ||
305 inp->inp_socket->so_options & SO_DONTROUTE)) {
306 RTFREE(ro->ro_rt);
307 ro->ro_rt = (struct rtentry *)0;
308 }
309 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
310 (ro->ro_rt == (struct rtentry *)0 ||
311 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
312 /* No route yet, so try to acquire one */
313 ro->ro_dst.sa_family = AF_INET;
314 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
315 satosin(&ro->ro_dst)->sin_addr = sin->sin_addr;
316 rtalloc(ro);
317 }
318 /*
319 * If we found a route, use the address
320 * corresponding to the outgoing interface
321 * unless it is the loopback (in case a route
322 * to our address on another net goes to loopback).
323 *
324 * XXX Is this still true? Do we care?
325 */
326 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
327 ia = ifatoia(ro->ro_rt->rt_ifa);
328 if (ia == 0) {
329 u_int16_t fport = sin->sin_port;
330
331 sin->sin_port = 0;
332 ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
333 sin->sin_port = fport;
334 if (ia == 0)
335 /* Find 1st non-loopback AF_INET address */
336 for (ia = in_ifaddr.tqh_first ; ia != NULL ;
337 ia = ia->ia_list.tqe_next)
338 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
339 break;
340 if (ia == 0)
341 return (EADDRNOTAVAIL);
342 }
343 /*
344 * If the destination address is multicast and an outgoing
345 * interface has been set as a multicast option, use the
346 * address of that interface as our source address.
347 */
348 if (IN_MULTICAST(sin->sin_addr.s_addr) &&
349 inp->inp_moptions != NULL) {
350 struct ip_moptions *imo;
351 struct ifnet *ifp;
352
353 imo = inp->inp_moptions;
354 if (imo->imo_multicast_ifp != NULL) {
355 ifp = imo->imo_multicast_ifp;
356 IFP_TO_IA(ifp, ia); /* XXX */
357 if (ia == 0)
358 return (EADDRNOTAVAIL);
359 }
360 }
361 ifaddr = satosin(&ia->ia_addr);
362 }
363 if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port,
364 !in_nullhost(inp->inp_laddr) ? inp->inp_laddr : ifaddr->sin_addr,
365 inp->inp_lport) != 0)
366 return (EADDRINUSE);
367 if (in_nullhost(inp->inp_laddr)) {
368 if (inp->inp_lport == 0) {
369 error = in_pcbbind(inp, (struct mbuf *)0,
370 (struct proc *)0);
371 /*
372 * This used to ignore the return value
373 * completely, but we need to check for
374 * ephemeral port shortage.
375 * XXX Should we check for other errors, too?
376 */
377 if (error == EAGAIN)
378 return (error);
379 }
380 inp->inp_laddr = ifaddr->sin_addr;
381 }
382 inp->inp_faddr = sin->sin_addr;
383 inp->inp_fport = sin->sin_port;
384 in_pcbstate(inp, INP_CONNECTED);
385 return (0);
386 }
387
388 void
389 in_pcbdisconnect(v)
390 void *v;
391 {
392 struct inpcb *inp = v;
393
394 inp->inp_faddr = zeroin_addr;
395 inp->inp_fport = 0;
396 in_pcbstate(inp, INP_BOUND);
397 if (inp->inp_socket->so_state & SS_NOFDREF)
398 in_pcbdetach(inp);
399 }
400
401 void
402 in_pcbdetach(v)
403 void *v;
404 {
405 struct inpcb *inp = v;
406 struct socket *so = inp->inp_socket;
407 int s;
408
409 so->so_pcb = 0;
410 sofree(so);
411 if (inp->inp_options)
412 (void)m_free(inp->inp_options);
413 if (inp->inp_route.ro_rt)
414 rtfree(inp->inp_route.ro_rt);
415 ip_freemoptions(inp->inp_moptions);
416 s = splnet();
417 in_pcbstate(inp, INP_ATTACHED);
418 CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue);
419 splx(s);
420 FREE(inp, M_PCB);
421 }
422
423 void
424 in_setsockaddr(inp, nam)
425 register struct inpcb *inp;
426 struct mbuf *nam;
427 {
428 register struct sockaddr_in *sin;
429
430 nam->m_len = sizeof (*sin);
431 sin = mtod(nam, struct sockaddr_in *);
432 bzero((caddr_t)sin, sizeof (*sin));
433 sin->sin_family = AF_INET;
434 sin->sin_len = sizeof(*sin);
435 sin->sin_port = inp->inp_lport;
436 sin->sin_addr = inp->inp_laddr;
437 }
438
439 void
440 in_setpeeraddr(inp, nam)
441 struct inpcb *inp;
442 struct mbuf *nam;
443 {
444 register struct sockaddr_in *sin;
445
446 nam->m_len = sizeof (*sin);
447 sin = mtod(nam, struct sockaddr_in *);
448 bzero((caddr_t)sin, sizeof (*sin));
449 sin->sin_family = AF_INET;
450 sin->sin_len = sizeof(*sin);
451 sin->sin_port = inp->inp_fport;
452 sin->sin_addr = inp->inp_faddr;
453 }
454
455 /*
456 * Pass some notification to all connections of a protocol
457 * associated with address dst. The local address and/or port numbers
458 * may be specified to limit the search. The "usual action" will be
459 * taken, depending on the ctlinput cmd. The caller must filter any
460 * cmds that are uninteresting (e.g., no error in the map).
461 * Call the protocol specific routine (if any) to report
462 * any errors for each matching socket.
463 *
464 * Must be called at splsoftnet.
465 */
466 int
467 in_pcbnotify(table, faddr, fport_arg, laddr, lport_arg, errno, notify)
468 struct inpcbtable *table;
469 struct in_addr faddr, laddr;
470 u_int fport_arg, lport_arg;
471 int errno;
472 void (*notify) __P((struct inpcb *, int));
473 {
474 struct inpcbhead *head;
475 register struct inpcb *inp, *ninp;
476 u_int16_t fport = fport_arg, lport = lport_arg;
477 int nmatch;
478
479 if (in_nullhost(faddr) || notify == 0)
480 return (0);
481
482 nmatch = 0;
483 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
484 for (inp = head->lh_first; inp != NULL; inp = ninp) {
485 ninp = inp->inp_hash.le_next;
486 if (in_hosteq(inp->inp_faddr, faddr) &&
487 inp->inp_fport == fport &&
488 inp->inp_lport == lport &&
489 in_hosteq(inp->inp_laddr, laddr)) {
490 (*notify)(inp, errno);
491 nmatch++;
492 }
493 }
494 return (nmatch);
495 }
496
497 void
498 in_pcbnotifyall(table, faddr, errno, notify)
499 struct inpcbtable *table;
500 struct in_addr faddr;
501 int errno;
502 void (*notify) __P((struct inpcb *, int));
503 {
504 register struct inpcb *inp, *ninp;
505
506 if (in_nullhost(faddr) || notify == 0)
507 return;
508
509 for (inp = table->inpt_queue.cqh_first;
510 inp != (struct inpcb *)&table->inpt_queue;
511 inp = ninp) {
512 ninp = inp->inp_queue.cqe_next;
513 if (in_hosteq(inp->inp_faddr, faddr))
514 (*notify)(inp, errno);
515 }
516 }
517
518 /*
519 * Check for alternatives when higher level complains
520 * about service problems. For now, invalidate cached
521 * routing information. If the route was created dynamically
522 * (by a redirect), time to try a default gateway again.
523 */
524 void
525 in_losing(inp)
526 struct inpcb *inp;
527 {
528 register struct rtentry *rt;
529 struct rt_addrinfo info;
530
531 if ((rt = inp->inp_route.ro_rt)) {
532 inp->inp_route.ro_rt = 0;
533 bzero((caddr_t)&info, sizeof(info));
534 info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst;
535 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
536 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
537 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
538 if (rt->rt_flags & RTF_DYNAMIC)
539 (void) rtrequest(RTM_DELETE, rt_key(rt),
540 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
541 (struct rtentry **)0);
542 else
543 /*
544 * A new route can be allocated
545 * the next time output is attempted.
546 */
547 rtfree(rt);
548 }
549 }
550
551 /*
552 * After a routing change, flush old routing
553 * and allocate a (hopefully) better one.
554 */
555 void
556 in_rtchange(inp, errno)
557 register struct inpcb *inp;
558 int errno;
559 {
560
561 if (inp->inp_route.ro_rt) {
562 rtfree(inp->inp_route.ro_rt);
563 inp->inp_route.ro_rt = 0;
564 /*
565 * A new route can be allocated the next time
566 * output is attempted.
567 */
568 }
569 /* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
570 }
571
572 struct inpcb *
573 in_pcblookup_port(table, laddr, lport_arg, flags)
574 struct inpcbtable *table;
575 struct in_addr laddr;
576 u_int lport_arg;
577 int flags;
578 {
579 register struct inpcb *inp, *match = 0;
580 int matchwild = 3, wildcard;
581 u_int16_t lport = lport_arg;
582
583 for (inp = table->inpt_queue.cqh_first;
584 inp != (struct inpcb *)&table->inpt_queue;
585 inp = inp->inp_queue.cqe_next) {
586 if (inp->inp_lport != lport)
587 continue;
588 wildcard = 0;
589 if (!in_nullhost(inp->inp_faddr))
590 wildcard++;
591 if (in_nullhost(inp->inp_laddr)) {
592 if (!in_nullhost(laddr))
593 wildcard++;
594 } else {
595 if (in_nullhost(laddr))
596 wildcard++;
597 else {
598 if (!in_hosteq(inp->inp_laddr, laddr))
599 continue;
600 }
601 }
602 if (wildcard && (flags & INPLOOKUP_WILDCARD) == 0)
603 continue;
604 if (wildcard < matchwild) {
605 match = inp;
606 matchwild = wildcard;
607 if (matchwild == 0)
608 break;
609 }
610 }
611 return (match);
612 }
613
614 #ifdef DIAGNOSTIC
615 int in_pcbnotifymiss = 0;
616 #endif
617
618 struct inpcb *
619 in_pcblookup_connect(table, faddr, fport_arg, laddr, lport_arg)
620 struct inpcbtable *table;
621 struct in_addr faddr, laddr;
622 u_int fport_arg, lport_arg;
623 {
624 struct inpcbhead *head;
625 register struct inpcb *inp;
626 u_int16_t fport = fport_arg, lport = lport_arg;
627
628 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
629 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
630 if (in_hosteq(inp->inp_faddr, faddr) &&
631 inp->inp_fport == fport &&
632 inp->inp_lport == lport &&
633 in_hosteq(inp->inp_laddr, laddr))
634 goto out;
635 }
636 #ifdef DIAGNOSTIC
637 if (in_pcbnotifymiss) {
638 printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n",
639 ntohl(faddr.s_addr), ntohs(fport),
640 ntohl(laddr.s_addr), ntohs(lport));
641 }
642 #endif
643 return (0);
644
645 out:
646 /* Move this PCB to the head of hash chain. */
647 if (inp != head->lh_first) {
648 LIST_REMOVE(inp, inp_hash);
649 LIST_INSERT_HEAD(head, inp, inp_hash);
650 }
651 return (inp);
652 }
653
654 struct inpcb *
655 in_pcblookup_bind(table, laddr, lport_arg)
656 struct inpcbtable *table;
657 struct in_addr laddr;
658 u_int lport_arg;
659 {
660 struct inpcbhead *head;
661 register struct inpcb *inp;
662 u_int16_t lport = lport_arg;
663
664 head = INPCBHASH_BIND(table, laddr, lport);
665 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
666 if (inp->inp_lport == lport &&
667 in_hosteq(inp->inp_laddr, laddr))
668 goto out;
669 }
670 head = INPCBHASH_BIND(table, zeroin_addr, lport);
671 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
672 if (inp->inp_lport == lport &&
673 in_hosteq(inp->inp_laddr, zeroin_addr))
674 goto out;
675 }
676 #ifdef DIAGNOSTIC
677 if (in_pcbnotifymiss) {
678 printf("in_pcblookup_bind: laddr=%08x lport=%d\n",
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 void
694 in_pcbstate(inp, state)
695 struct inpcb *inp;
696 int state;
697 {
698
699 if (inp->inp_state > INP_ATTACHED)
700 LIST_REMOVE(inp, inp_hash);
701
702 switch (state) {
703 case INP_BOUND:
704 LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table,
705 inp->inp_laddr, inp->inp_lport), inp, inp_hash);
706 break;
707 case INP_CONNECTED:
708 LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table,
709 inp->inp_faddr, inp->inp_fport,
710 inp->inp_laddr, inp->inp_lport), inp, inp_hash);
711 break;
712 }
713
714 inp->inp_state = state;
715 }
716
717 struct rtentry *
718 in_pcbrtentry(inp)
719 struct inpcb *inp;
720 {
721 struct route *ro;
722
723 ro = &inp->inp_route;
724
725 if (ro->ro_rt == NULL) {
726 /*
727 * No route yet, so try to acquire one.
728 */
729 if (!in_nullhost(inp->inp_faddr)) {
730 ro->ro_dst.sa_family = AF_INET;
731 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
732 satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr;
733 rtalloc(ro);
734 }
735 }
736 return (ro->ro_rt);
737 }
738