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