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