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