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