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