in_pcb.c revision 1.83 1 /* $NetBSD: in_pcb.c,v 1.83 2003/06/26 00:19:13 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/cdefs.h>
105 __KERNEL_RCSID(0, "$NetBSD: in_pcb.c,v 1.83 2003/06/26 00:19:13 itojun Exp $");
106
107 #include "opt_ipsec.h"
108
109 #include <sys/param.h>
110 #include <sys/systm.h>
111 #include <sys/malloc.h>
112 #include <sys/mbuf.h>
113 #include <sys/protosw.h>
114 #include <sys/socket.h>
115 #include <sys/socketvar.h>
116 #include <sys/ioctl.h>
117 #include <sys/errno.h>
118 #include <sys/time.h>
119 #include <sys/pool.h>
120 #include <sys/proc.h>
121
122 #include <net/if.h>
123 #include <net/route.h>
124
125 #include <netinet/in.h>
126 #include <netinet/in_systm.h>
127 #include <netinet/ip.h>
128 #include <netinet/in_pcb.h>
129 #include <netinet/in_var.h>
130 #include <netinet/ip_var.h>
131
132 #ifdef IPSEC
133 #include <netinet6/ipsec.h>
134 #include <netkey/key.h>
135 #endif /* IPSEC */
136
137 struct in_addr zeroin_addr;
138
139 #define INPCBHASH_BIND(table, laddr, lport) \
140 &(table)->inpt_bindhashtbl[ \
141 ((ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_bindhash]
142 #define INPCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
143 &(table)->inpt_connecthashtbl[ \
144 ((ntohl((faddr).s_addr) + ntohs(fport)) + \
145 (ntohl((laddr).s_addr) + ntohs(lport))) & (table)->inpt_connecthash]
146
147 struct inpcb *
148 in_pcblookup_port __P((struct inpcbtable *,
149 struct in_addr, u_int, int));
150
151 int anonportmin = IPPORT_ANONMIN;
152 int anonportmax = IPPORT_ANONMAX;
153 int lowportmin = IPPORT_RESERVEDMIN;
154 int lowportmax = IPPORT_RESERVEDMAX;
155
156 struct pool inpcb_pool;
157
158 void
159 in_pcbinit(table, bindhashsize, connecthashsize)
160 struct inpcbtable *table;
161 int bindhashsize, connecthashsize;
162 {
163 static int inpcb_pool_initialized;
164
165 if (inpcb_pool_initialized == 0) {
166 pool_init(&inpcb_pool, sizeof(struct inpcb), 0, 0, 0,
167 "inpcbpl", NULL);
168 inpcb_pool_initialized = 1;
169 }
170
171 CIRCLEQ_INIT(&table->inpt_queue);
172 table->inpt_bindhashtbl = hashinit(bindhashsize, HASH_LIST, M_PCB,
173 M_WAITOK, &table->inpt_bindhash);
174 table->inpt_connecthashtbl = hashinit(connecthashsize, HASH_LIST,
175 M_PCB, M_WAITOK, &table->inpt_connecthash);
176 table->inpt_lastlow = IPPORT_RESERVEDMAX;
177 table->inpt_lastport = (u_int16_t)anonportmax;
178 }
179
180 int
181 in_pcballoc(so, v)
182 struct socket *so;
183 void *v;
184 {
185 struct inpcbtable *table = v;
186 struct inpcb *inp;
187 int s;
188 #ifdef IPSEC
189 int error;
190 #endif
191
192 inp = pool_get(&inpcb_pool, PR_NOWAIT);
193 if (inp == NULL)
194 return (ENOBUFS);
195 bzero((caddr_t)inp, sizeof(*inp));
196 inp->inp_table = table;
197 inp->inp_socket = so;
198 inp->inp_errormtu = -1;
199 #ifdef IPSEC
200 error = ipsec_init_pcbpolicy(so, &inp->inp_sp);
201 if (error != 0) {
202 pool_put(&inpcb_pool, inp);
203 return error;
204 }
205 #endif
206 so->so_pcb = inp;
207 s = splnet();
208 CIRCLEQ_INSERT_HEAD(&table->inpt_queue, inp, inp_queue);
209 in_pcbstate(inp, INP_ATTACHED);
210 splx(s);
211 return (0);
212 }
213
214 int
215 in_pcbbind(v, nam, p)
216 void *v;
217 struct mbuf *nam;
218 struct proc *p;
219 {
220 struct in_ifaddr *ia = NULL;
221 struct inpcb *inp = v;
222 struct socket *so = inp->inp_socket;
223 struct inpcbtable *table = inp->inp_table;
224 struct sockaddr_in *sin;
225 u_int16_t lport = 0;
226 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
227
228 if (TAILQ_FIRST(&in_ifaddr) == 0)
229 return (EADDRNOTAVAIL);
230 if (inp->inp_lport || !in_nullhost(inp->inp_laddr))
231 return (EINVAL);
232 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
233 wild = 1;
234 if (nam == 0)
235 goto noname;
236 sin = mtod(nam, struct sockaddr_in *);
237 if (nam->m_len != sizeof (*sin))
238 return (EINVAL);
239 if (sin->sin_family != AF_INET)
240 return (EAFNOSUPPORT);
241 lport = sin->sin_port;
242 if (IN_MULTICAST(sin->sin_addr.s_addr)) {
243 /*
244 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
245 * allow complete duplication of binding if
246 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
247 * and a multicast address is bound on both
248 * new and duplicated sockets.
249 */
250 if (so->so_options & SO_REUSEADDR)
251 reuseport = SO_REUSEADDR|SO_REUSEPORT;
252 } else if (!in_nullhost(sin->sin_addr)) {
253 sin->sin_port = 0; /* yech... */
254 INADDR_TO_IA(sin->sin_addr, ia);
255 if (ia == NULL)
256 return (EADDRNOTAVAIL);
257 }
258 if (lport) {
259 struct inpcb *t;
260 #ifndef IPNOPRIVPORTS
261 /* GROSS */
262 if (ntohs(lport) < IPPORT_RESERVED &&
263 (p == 0 || suser(p->p_ucred, &p->p_acflag)))
264 return (EACCES);
265 #endif
266 if (so->so_uid && !IN_MULTICAST(sin->sin_addr.s_addr)) {
267 t = in_pcblookup_port(table, sin->sin_addr, lport, 1);
268 /*
269 * XXX: investigate ramifications of loosening this
270 * restriction so that as long as both ports have
271 * SO_REUSEPORT allow the bind
272 */
273 if (t &&
274 (!in_nullhost(sin->sin_addr) ||
275 !in_nullhost(t->inp_laddr) ||
276 (t->inp_socket->so_options & SO_REUSEPORT) == 0)
277 && (so->so_uid != t->inp_socket->so_uid)) {
278 return (EADDRINUSE);
279 }
280 }
281 t = in_pcblookup_port(table, sin->sin_addr, lport, wild);
282 if (t && (reuseport & t->inp_socket->so_options) == 0)
283 return (EADDRINUSE);
284 }
285 if (!in_nullhost(inp->inp_laddr)) {
286 KASSERT(inp->inp_ia != NULL);
287 LIST_REMOVE(inp, inp_ialink);
288 IFAFREE(&inp->inp_ia->ia_ifa);
289 inp->inp_ia = NULL;
290 }
291 inp->inp_laddr = sin->sin_addr;
292 if (ia != NULL) {
293 inp->inp_ia = ia;
294 LIST_INSERT_HEAD(&ia->ia_inpcbs, inp, inp_ialink);
295 IFAREF(&ia->ia_ifa);
296 }
297
298
299 noname:
300 if (lport == 0) {
301 int cnt;
302 u_int16_t min, max;
303 u_int16_t *lastport;
304
305 if (inp->inp_flags & INP_LOWPORT) {
306 #ifndef IPNOPRIVPORTS
307 if (p == 0 || suser(p->p_ucred, &p->p_acflag))
308 return (EACCES);
309 #endif
310 min = lowportmin;
311 max = lowportmax;
312 lastport = &table->inpt_lastlow;
313 } else {
314 min = anonportmin;
315 max = anonportmax;
316 lastport = &table->inpt_lastport;
317 }
318 if (min > max) { /* sanity check */
319 u_int16_t swp;
320
321 swp = min;
322 min = max;
323 max = swp;
324 }
325
326 lport = *lastport - 1;
327 for (cnt = max - min + 1; cnt; cnt--, lport--) {
328 if (lport < min || lport > max)
329 lport = max;
330 if (!in_pcblookup_port(table, inp->inp_laddr,
331 htons(lport), 1))
332 goto found;
333 }
334 if (!in_nullhost(inp->inp_laddr)) {
335 if (inp->inp_ia != NULL) {
336 LIST_REMOVE(inp, inp_ialink);
337 IFAFREE(&inp->inp_ia->ia_ifa);
338 inp->inp_ia = NULL;
339 }
340 inp->inp_laddr.s_addr = INADDR_ANY;
341 }
342 return (EAGAIN);
343 found:
344 inp->inp_flags |= INP_ANONPORT;
345 *lastport = lport;
346 lport = htons(lport);
347 }
348 inp->inp_lport = lport;
349 in_pcbstate(inp, INP_BOUND);
350 return (0);
351 }
352
353 /*
354 * Connect from a socket to a specified address.
355 * Both address and port must be specified in argument sin.
356 * If don't have a local address for this socket yet,
357 * then pick one.
358 */
359 int
360 in_pcbconnect(v, nam)
361 void *v;
362 struct mbuf *nam;
363 {
364 struct inpcb *inp = v;
365 struct in_ifaddr *ia = NULL;
366 struct sockaddr_in *ifaddr = NULL;
367 struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
368 int error;
369
370 if (nam->m_len != sizeof (*sin))
371 return (EINVAL);
372 if (sin->sin_family != AF_INET)
373 return (EAFNOSUPPORT);
374 if (sin->sin_port == 0)
375 return (EADDRNOTAVAIL);
376 if (TAILQ_FIRST(&in_ifaddr) != 0) {
377 /*
378 * If the destination address is INADDR_ANY,
379 * use any local address (likely loopback).
380 * If the supplied address is INADDR_BROADCAST,
381 * use the broadcast address of an interface
382 * which supports broadcast. (loopback does not)
383 */
384
385 if (in_nullhost(sin->sin_addr)) {
386 sin->sin_addr =
387 TAILQ_FIRST(&in_ifaddr)->ia_addr.sin_addr;
388 } else if (sin->sin_addr.s_addr == INADDR_BROADCAST) {
389 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
390 if (ia->ia_ifp->if_flags & IFF_BROADCAST) {
391 sin->sin_addr =
392 ia->ia_broadaddr.sin_addr;
393 break;
394 }
395 }
396 }
397 }
398 /*
399 * If we haven't bound which network number to use as ours,
400 * we will use the number of the outgoing interface.
401 * This depends on having done a routing lookup, which
402 * we will probably have to do anyway, so we might
403 * as well do it now. On the other hand if we are
404 * sending to multiple destinations we may have already
405 * done the lookup, so see if we can use the route
406 * from before. In any case, we only
407 * chose a port number once, even if sending to multiple
408 * destinations.
409 */
410 if (in_nullhost(inp->inp_laddr)) {
411 int error;
412 ifaddr = in_selectsrc(sin, &inp->inp_route,
413 inp->inp_socket->so_options, inp->inp_moptions, &error);
414 if (ifaddr == NULL) {
415 if (error == 0)
416 error = EADDRNOTAVAIL;
417 return error;
418 }
419 INADDR_TO_IA(ifaddr->sin_addr, ia);
420 if (ia == NULL)
421 return (EADDRNOTAVAIL);
422 }
423 if (in_pcblookup_connect(inp->inp_table, sin->sin_addr, sin->sin_port,
424 !in_nullhost(inp->inp_laddr) ? inp->inp_laddr : ifaddr->sin_addr,
425 inp->inp_lport) != 0)
426 return (EADDRINUSE);
427 if (in_nullhost(inp->inp_laddr)) {
428 if (inp->inp_lport == 0) {
429 error = in_pcbbind(inp, (struct mbuf *)0,
430 (struct proc *)0);
431 /*
432 * This used to ignore the return value
433 * completely, but we need to check for
434 * ephemeral port shortage.
435 * XXX Should we check for other errors, too?
436 */
437 if (error == EAGAIN)
438 return (error);
439 }
440 KASSERT(inp->inp_ia == NULL);
441 inp->inp_laddr = ia->ia_addr.sin_addr;
442 inp->inp_ia = ia;
443 LIST_INSERT_HEAD(&ia->ia_inpcbs, inp, inp_ialink);
444 IFAREF(&ia->ia_ifa);
445 inp->inp_laddr = ifaddr->sin_addr;
446 }
447 inp->inp_faddr = sin->sin_addr;
448 inp->inp_fport = sin->sin_port;
449 in_pcbstate(inp, INP_CONNECTED);
450 #ifdef IPSEC
451 if (inp->inp_socket->so_type == SOCK_STREAM)
452 ipsec_pcbconn(inp->inp_sp);
453 #endif
454 return (0);
455 }
456
457 void
458 in_pcbdisconnect(v)
459 void *v;
460 {
461 struct inpcb *inp = v;
462
463 inp->inp_faddr = zeroin_addr;
464 inp->inp_fport = 0;
465 in_pcbstate(inp, INP_BOUND);
466 if (inp->inp_socket->so_state & SS_NOFDREF)
467 in_pcbdetach(inp);
468 #ifdef IPSEC
469 ipsec_pcbdisconn(inp->inp_sp);
470 #endif
471 }
472
473 void
474 in_pcbdetach(v)
475 void *v;
476 {
477 struct inpcb *inp = v;
478 struct socket *so = inp->inp_socket;
479 int s;
480
481 #ifdef IPSEC
482 ipsec4_delete_pcbpolicy(inp);
483 #endif /*IPSEC*/
484 so->so_pcb = 0;
485 sofree(so);
486 if (inp->inp_options)
487 (void)m_free(inp->inp_options);
488 if (inp->inp_route.ro_rt)
489 rtfree(inp->inp_route.ro_rt);
490 ip_freemoptions(inp->inp_moptions);
491 if (inp->inp_ia != NULL) {
492 LIST_REMOVE(inp, inp_ialink);
493 IFAFREE(&inp->inp_ia->ia_ifa);
494 inp->inp_ia = NULL;
495 }
496 s = splnet();
497 in_pcbstate(inp, INP_ATTACHED);
498 CIRCLEQ_REMOVE(&inp->inp_table->inpt_queue, inp, inp_queue);
499 splx(s);
500 pool_put(&inpcb_pool, inp);
501 }
502
503 void
504 in_setsockaddr(inp, nam)
505 struct inpcb *inp;
506 struct mbuf *nam;
507 {
508 struct sockaddr_in *sin;
509
510 nam->m_len = sizeof (*sin);
511 sin = mtod(nam, struct sockaddr_in *);
512 bzero((caddr_t)sin, sizeof (*sin));
513 sin->sin_family = AF_INET;
514 sin->sin_len = sizeof(*sin);
515 sin->sin_port = inp->inp_lport;
516 sin->sin_addr = inp->inp_laddr;
517 }
518
519 void
520 in_setpeeraddr(inp, nam)
521 struct inpcb *inp;
522 struct mbuf *nam;
523 {
524 struct sockaddr_in *sin;
525
526 nam->m_len = sizeof (*sin);
527 sin = mtod(nam, struct sockaddr_in *);
528 bzero((caddr_t)sin, sizeof (*sin));
529 sin->sin_family = AF_INET;
530 sin->sin_len = sizeof(*sin);
531 sin->sin_port = inp->inp_fport;
532 sin->sin_addr = inp->inp_faddr;
533 }
534
535 /*
536 * Pass some notification to all connections of a protocol
537 * associated with address dst. The local address and/or port numbers
538 * may be specified to limit the search. The "usual action" will be
539 * taken, depending on the ctlinput cmd. The caller must filter any
540 * cmds that are uninteresting (e.g., no error in the map).
541 * Call the protocol specific routine (if any) to report
542 * any errors for each matching socket.
543 *
544 * Must be called at splsoftnet.
545 */
546 int
547 in_pcbnotify(table, faddr, fport_arg, laddr, lport_arg, errno, notify)
548 struct inpcbtable *table;
549 struct in_addr faddr, laddr;
550 u_int fport_arg, lport_arg;
551 int errno;
552 void (*notify) __P((struct inpcb *, int));
553 {
554 struct inpcbhead *head;
555 struct inpcb *inp, *ninp;
556 u_int16_t fport = fport_arg, lport = lport_arg;
557 int nmatch;
558
559 if (in_nullhost(faddr) || notify == 0)
560 return (0);
561
562 nmatch = 0;
563 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
564 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
565 ninp = LIST_NEXT(inp, inp_hash);
566 if (in_hosteq(inp->inp_faddr, faddr) &&
567 inp->inp_fport == fport &&
568 inp->inp_lport == lport &&
569 in_hosteq(inp->inp_laddr, laddr)) {
570 (*notify)(inp, errno);
571 nmatch++;
572 }
573 }
574 return (nmatch);
575 }
576
577 void
578 in_pcbnotifyall(table, faddr, errno, notify)
579 struct inpcbtable *table;
580 struct in_addr faddr;
581 int errno;
582 void (*notify) __P((struct inpcb *, int));
583 {
584 struct inpcb *inp, *ninp;
585
586 if (in_nullhost(faddr) || notify == 0)
587 return;
588
589 for (inp = CIRCLEQ_FIRST(&table->inpt_queue);
590 inp != (void *)&table->inpt_queue;
591 inp = ninp) {
592 ninp = CIRCLEQ_NEXT(inp, inp_queue);
593 if (in_hosteq(inp->inp_faddr, faddr))
594 (*notify)(inp, errno);
595 }
596 }
597
598 void
599 in_pcbpurgeif0(table, ifp)
600 struct inpcbtable *table;
601 struct ifnet *ifp;
602 {
603 struct inpcb *inp, *ninp;
604 struct ip_moptions *imo;
605 int i, gap;
606
607 for (inp = CIRCLEQ_FIRST(&table->inpt_queue);
608 inp != (void *)&table->inpt_queue;
609 inp = ninp) {
610 ninp = CIRCLEQ_NEXT(inp, inp_queue);
611 imo = inp->inp_moptions;
612 if (imo != NULL) {
613 /*
614 * Unselect the outgoing interface if it is being
615 * detached.
616 */
617 if (imo->imo_multicast_ifp == ifp)
618 imo->imo_multicast_ifp = NULL;
619
620 /*
621 * Drop multicast group membership if we joined
622 * through the interface being detached.
623 */
624 for (i = 0, gap = 0; i < imo->imo_num_memberships;
625 i++) {
626 if (imo->imo_membership[i]->inm_ifp == ifp) {
627 in_delmulti(imo->imo_membership[i]);
628 gap++;
629 } else if (gap != 0)
630 imo->imo_membership[i - gap] =
631 imo->imo_membership[i];
632 }
633 imo->imo_num_memberships -= gap;
634 }
635 }
636 }
637
638 void
639 in_pcbpurgeif(table, ifp)
640 struct inpcbtable *table;
641 struct ifnet *ifp;
642 {
643 struct inpcb *inp, *ninp;
644
645 for (inp = CIRCLEQ_FIRST(&table->inpt_queue);
646 inp != (void *)&table->inpt_queue;
647 inp = ninp) {
648 ninp = CIRCLEQ_NEXT(inp, inp_queue);
649 if (inp->inp_route.ro_rt != NULL &&
650 inp->inp_route.ro_rt->rt_ifp == ifp)
651 in_rtchange(inp, 0);
652 }
653 }
654
655 /*
656 * Check for alternatives when higher level complains
657 * about service problems. For now, invalidate cached
658 * routing information. If the route was created dynamically
659 * (by a redirect), time to try a default gateway again.
660 */
661 void
662 in_losing(inp)
663 struct inpcb *inp;
664 {
665 struct rtentry *rt;
666 struct rt_addrinfo info;
667
668 if ((rt = inp->inp_route.ro_rt)) {
669 inp->inp_route.ro_rt = 0;
670 bzero((caddr_t)&info, sizeof(info));
671 info.rti_info[RTAX_DST] = &inp->inp_route.ro_dst;
672 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
673 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
674 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
675 if (rt->rt_flags & RTF_DYNAMIC)
676 (void) rtrequest(RTM_DELETE, rt_key(rt),
677 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
678 (struct rtentry **)0);
679 else
680 /*
681 * A new route can be allocated
682 * the next time output is attempted.
683 */
684 rtfree(rt);
685 }
686 }
687
688 /*
689 * After a routing change, flush old routing
690 * and allocate a (hopefully) better one.
691 */
692 void
693 in_rtchange(inp, errno)
694 struct inpcb *inp;
695 int errno;
696 {
697
698 if (inp->inp_route.ro_rt) {
699 rtfree(inp->inp_route.ro_rt);
700 inp->inp_route.ro_rt = 0;
701 /*
702 * A new route can be allocated the next time
703 * output is attempted.
704 */
705 }
706 /* XXX SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
707 }
708
709 struct inpcb *
710 in_pcblookup_port(table, laddr, lport_arg, lookup_wildcard)
711 struct inpcbtable *table;
712 struct in_addr laddr;
713 u_int lport_arg;
714 int lookup_wildcard;
715 {
716 struct inpcb *inp, *match = 0;
717 int matchwild = 3, wildcard;
718 u_int16_t lport = lport_arg;
719
720 CIRCLEQ_FOREACH(inp, &table->inpt_queue, inp_queue) {
721 if (inp->inp_lport != lport)
722 continue;
723 wildcard = 0;
724 if (!in_nullhost(inp->inp_faddr))
725 wildcard++;
726 if (in_nullhost(inp->inp_laddr)) {
727 if (!in_nullhost(laddr))
728 wildcard++;
729 } else {
730 if (in_nullhost(laddr))
731 wildcard++;
732 else {
733 if (!in_hosteq(inp->inp_laddr, laddr))
734 continue;
735 }
736 }
737 if (wildcard && !lookup_wildcard)
738 continue;
739 if (wildcard < matchwild) {
740 match = inp;
741 matchwild = wildcard;
742 if (matchwild == 0)
743 break;
744 }
745 }
746 return (match);
747 }
748
749 #ifdef DIAGNOSTIC
750 int in_pcbnotifymiss = 0;
751 #endif
752
753 struct inpcb *
754 in_pcblookup_connect(table, faddr, fport_arg, laddr, lport_arg)
755 struct inpcbtable *table;
756 struct in_addr faddr, laddr;
757 u_int fport_arg, lport_arg;
758 {
759 struct inpcbhead *head;
760 struct inpcb *inp;
761 u_int16_t fport = fport_arg, lport = lport_arg;
762
763 head = INPCBHASH_CONNECT(table, faddr, fport, laddr, lport);
764 LIST_FOREACH(inp, head, inp_hash) {
765 if (in_hosteq(inp->inp_faddr, faddr) &&
766 inp->inp_fport == fport &&
767 inp->inp_lport == lport &&
768 in_hosteq(inp->inp_laddr, laddr))
769 goto out;
770 }
771 #ifdef DIAGNOSTIC
772 if (in_pcbnotifymiss) {
773 printf("in_pcblookup_connect: faddr=%08x fport=%d laddr=%08x lport=%d\n",
774 ntohl(faddr.s_addr), ntohs(fport),
775 ntohl(laddr.s_addr), ntohs(lport));
776 }
777 #endif
778 return (0);
779
780 out:
781 /* Move this PCB to the head of hash chain. */
782 if (inp != LIST_FIRST(head)) {
783 LIST_REMOVE(inp, inp_hash);
784 LIST_INSERT_HEAD(head, inp, inp_hash);
785 }
786 return (inp);
787 }
788
789 struct inpcb *
790 in_pcblookup_bind(table, laddr, lport_arg)
791 struct inpcbtable *table;
792 struct in_addr laddr;
793 u_int lport_arg;
794 {
795 struct inpcbhead *head;
796 struct inpcb *inp;
797 u_int16_t lport = lport_arg;
798
799 head = INPCBHASH_BIND(table, laddr, lport);
800 LIST_FOREACH(inp, head, inp_hash) {
801 if (inp->inp_lport == lport &&
802 in_hosteq(inp->inp_laddr, laddr))
803 goto out;
804 }
805 head = INPCBHASH_BIND(table, zeroin_addr, lport);
806 LIST_FOREACH(inp, head, inp_hash) {
807 if (inp->inp_lport == lport &&
808 in_hosteq(inp->inp_laddr, zeroin_addr))
809 goto out;
810 }
811 #ifdef DIAGNOSTIC
812 if (in_pcbnotifymiss) {
813 printf("in_pcblookup_bind: laddr=%08x lport=%d\n",
814 ntohl(laddr.s_addr), ntohs(lport));
815 }
816 #endif
817 return (0);
818
819 out:
820 /* Move this PCB to the head of hash chain. */
821 if (inp != LIST_FIRST(head)) {
822 LIST_REMOVE(inp, inp_hash);
823 LIST_INSERT_HEAD(head, inp, inp_hash);
824 }
825 return (inp);
826 }
827
828 void
829 in_pcbstate(inp, state)
830 struct inpcb *inp;
831 int state;
832 {
833
834 if (inp->inp_state > INP_ATTACHED)
835 LIST_REMOVE(inp, inp_hash);
836
837 switch (state) {
838 case INP_BOUND:
839 LIST_INSERT_HEAD(INPCBHASH_BIND(inp->inp_table,
840 inp->inp_laddr, inp->inp_lport), inp, inp_hash);
841 break;
842 case INP_CONNECTED:
843 LIST_INSERT_HEAD(INPCBHASH_CONNECT(inp->inp_table,
844 inp->inp_faddr, inp->inp_fport,
845 inp->inp_laddr, inp->inp_lport), inp, inp_hash);
846 break;
847 }
848
849 inp->inp_state = state;
850 }
851
852 struct rtentry *
853 in_pcbrtentry(inp)
854 struct inpcb *inp;
855 {
856 struct route *ro;
857
858 ro = &inp->inp_route;
859
860 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
861 !in_hosteq(satosin(&ro->ro_dst)->sin_addr, inp->inp_faddr))) {
862 RTFREE(ro->ro_rt);
863 ro->ro_rt = (struct rtentry *)NULL;
864 }
865 if (ro->ro_rt == (struct rtentry *)NULL &&
866 !in_nullhost(inp->inp_faddr)) {
867 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
868 ro->ro_dst.sa_family = AF_INET;
869 ro->ro_dst.sa_len = sizeof(ro->ro_dst);
870 satosin(&ro->ro_dst)->sin_addr = inp->inp_faddr;
871 rtalloc(ro);
872 }
873 return (ro->ro_rt);
874 }
875
876 struct sockaddr_in *
877 in_selectsrc(sin, ro, soopts, mopts, errorp)
878 struct sockaddr_in *sin;
879 struct route *ro;
880 int soopts;
881 struct ip_moptions *mopts;
882 int *errorp;
883 {
884 struct in_ifaddr *ia;
885
886 ia = (struct in_ifaddr *)0;
887 /*
888 * If route is known or can be allocated now,
889 * our src addr is taken from the i/f, else punt.
890 * Note that we should check the address family of the cached
891 * destination, in case of sharing the cache with IPv6.
892 */
893 if (ro->ro_rt &&
894 (ro->ro_dst.sa_family != AF_INET ||
895 !in_hosteq(satosin(&ro->ro_dst)->sin_addr, sin->sin_addr) ||
896 soopts & SO_DONTROUTE)) {
897 RTFREE(ro->ro_rt);
898 ro->ro_rt = (struct rtentry *)0;
899 }
900 if ((soopts & SO_DONTROUTE) == 0 && /*XXX*/
901 (ro->ro_rt == (struct rtentry *)0 ||
902 ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
903 /* No route yet, so try to acquire one */
904 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
905 ro->ro_dst.sa_family = AF_INET;
906 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
907 satosin(&ro->ro_dst)->sin_addr = sin->sin_addr;
908 rtalloc(ro);
909 }
910 /*
911 * If we found a route, use the address
912 * corresponding to the outgoing interface
913 * unless it is the loopback (in case a route
914 * to our address on another net goes to loopback).
915 *
916 * XXX Is this still true? Do we care?
917 */
918 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
919 ia = ifatoia(ro->ro_rt->rt_ifa);
920 if (ia == NULL) {
921 u_int16_t fport = sin->sin_port;
922
923 sin->sin_port = 0;
924 ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
925 sin->sin_port = fport;
926 if (ia == 0) {
927 /* Find 1st non-loopback AF_INET address */
928 TAILQ_FOREACH(ia, &in_ifaddr, ia_list) {
929 if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
930 break;
931 }
932 }
933 if (ia == NULL) {
934 *errorp = EADDRNOTAVAIL;
935 return NULL;
936 }
937 }
938 /*
939 * If the destination address is multicast and an outgoing
940 * interface has been set as a multicast option, use the
941 * address of that interface as our source address.
942 */
943 if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
944 struct ip_moptions *imo;
945 struct ifnet *ifp;
946
947 imo = mopts;
948 if (imo->imo_multicast_ifp != NULL) {
949 ifp = imo->imo_multicast_ifp;
950 IFP_TO_IA(ifp, ia); /* XXX */
951 if (ia == 0) {
952 *errorp = EADDRNOTAVAIL;
953 return NULL;
954 }
955 }
956 }
957 return satosin(&ia->ia_addr);
958 }
959