]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/bsd_tcpip/v2_0/src/sys/netinet/in_pcb.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / net / bsd_tcpip / v2_0 / src / sys / netinet / in_pcb.c
1 //==========================================================================
2 //
3 //      src/sys/netinet/in_pcb.c
4 //
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
7 //
8 // -------------------------------------------
9 //
10 // Portions of this software may have been derived from OpenBSD, 
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
13 //
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
16 //
17 // -------------------------------------------
18 //
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
21
22 /*
23  * Copyright (c) 1982, 1986, 1991, 1993, 1995
24  *      The Regents of the University of California.  All rights reserved.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions
28  * are met:
29  * 1. Redistributions of source code must retain the above copyright
30  *    notice, this list of conditions and the following disclaimer.
31  * 2. Redistributions in binary form must reproduce the above copyright
32  *    notice, this list of conditions and the following disclaimer in the
33  *    documentation and/or other materials provided with the distribution.
34  * 3. All advertising materials mentioning features or use of this software
35  *    must display the following acknowledgement:
36  *      This product includes software developed by the University of
37  *      California, Berkeley and its contributors.
38  * 4. Neither the name of the University nor the names of its contributors
39  *    may be used to endorse or promote products derived from this software
40  *    without specific prior written permission.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  *
54  *      @(#)in_pcb.c    8.4 (Berkeley) 5/24/95
55  * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.17 2001/08/13 16:26:17 ume Exp $
56  */
57
58 #include <sys/param.h>
59 #include <sys/malloc.h>
60 #include <sys/mbuf.h>
61 #include <sys/domain.h>
62 #include <sys/protosw.h>
63 #include <sys/socket.h>
64 #include <sys/socketvar.h>
65 #include <sys/sysctl.h>
66
67 #include <net/if.h>
68 #include <net/if_types.h>
69 #include <net/route.h>
70
71 #include <netinet/in.h>
72 #include <netinet/in_pcb.h>
73 #include <netinet/in_var.h>
74 #include <netinet/ip_var.h>
75 #ifdef INET6
76 #include <netinet/ip6.h>
77 #include <netinet6/ip6_var.h>
78 #endif /* INET6 */
79
80 #ifdef IPSEC
81 #include <netinet6/ipsec.h>
82 #include <netkey/key.h>
83 #endif /* IPSEC */
84
85 struct  in_addr zeroin_addr;
86
87 /*
88  * These configure the range of local port addresses assigned to
89  * "unspecified" outgoing connections/packets/whatever.
90  */
91 int     ipport_lowfirstauto  = IPPORT_RESERVED - 1;     /* 1023 */
92 int     ipport_lowlastauto = IPPORT_RESERVEDSTART;      /* 600 */
93 int     ipport_firstauto = IPPORT_RESERVED;             /* 1024 */
94 int     ipport_lastauto  = IPPORT_USERRESERVED;         /* 5000 */
95 int     ipport_hifirstauto = IPPORT_HIFIRSTAUTO;        /* 49152 */
96 int     ipport_hilastauto  = IPPORT_HILASTAUTO;         /* 65535 */
97
98 #define RANGECHK(var, min, max) \
99         if ((var) < (min)) { (var) = (min); } \
100         else if ((var) > (max)) { (var) = (max); }
101
102 #ifdef CYGPKG_NET_FREEBSD_SYSCTL
103 static int
104 sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
105 {
106         int error = sysctl_handle_int(oidp,
107                 oidp->oid_arg1, oidp->oid_arg2, req);
108         if (!error) {
109                 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
110                 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
111                 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
112                 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
113                 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
114                 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
115         }
116         return error;
117 }
118 #endif
119 #undef RANGECHK
120
121 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
122
123 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
124            &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
125 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
126            &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
127 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
128            &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
129 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
130            &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
131 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
132            &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
133 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
134            &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
135
136 /*
137  * in_pcb.c: manage the Protocol Control Blocks.
138  *
139  * NOTE: It is assumed that most of these functions will be called at
140  * splnet(). XXX - There are, unfortunately, a few exceptions to this
141  * rule that should be fixed.
142  */
143
144 /*
145  * Allocate a PCB and associate it with the socket.
146  */
147 int
148 in_pcballoc(so, pcbinfo, p)
149         struct socket *so;
150         struct inpcbinfo *pcbinfo;
151         struct proc *p;
152 {
153         register struct inpcb *inp;
154 #ifdef IPSEC
155         int error;
156 #endif
157
158         inp = zalloci(pcbinfo->ipi_zone);
159         if (inp == NULL)
160                 return (ENOBUFS);
161         bzero((caddr_t)inp, sizeof(*inp));
162         inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
163         inp->inp_pcbinfo = pcbinfo;
164         inp->inp_socket = so;
165 #ifdef IPSEC
166         error = ipsec_init_policy(so, &inp->inp_sp);
167         if (error != 0) {
168                 zfreei(pcbinfo->ipi_zone, inp);
169                 return error;
170         }
171 #endif /*IPSEC*/
172 #if defined(INET6)
173         if (INP_SOCKAF(so) == AF_INET6 && !ip6_mapped_addr_on)
174                 inp->inp_flags |= IN6P_IPV6_V6ONLY;
175 #endif
176         LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
177         pcbinfo->ipi_count++;
178         so->so_pcb = (caddr_t)inp;
179 #ifdef INET6
180         if (ip6_auto_flowlabel)
181                 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
182 #endif
183         return (0);
184 }
185
186 int
187 in_pcbbind(inp, nam, p)
188         register struct inpcb *inp;
189         struct sockaddr *nam;
190         struct proc *p;
191 {
192         register struct socket *so = inp->inp_socket;
193         unsigned short *lastport;
194         struct sockaddr_in *sin;
195         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
196         u_short lport = 0;
197         int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
198
199         if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
200                 return (EADDRNOTAVAIL);
201         if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
202                 return (EINVAL);
203         if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
204                 wild = 1;
205         if (nam) {
206                 sin = (struct sockaddr_in *)nam;
207         // HACK
208         if (nam->sa_len == 0) nam->sa_len = sizeof(*sin);
209         // HACK
210                 if (nam->sa_len != sizeof (*sin))
211                         return (EINVAL);
212 #ifdef notdef
213                 /*
214                  * We should check the family, but old programs
215                  * incorrectly fail to initialize it.
216                  */
217                 if (sin->sin_family != AF_INET)
218                         return (EAFNOSUPPORT);
219 #endif
220                 lport = sin->sin_port;
221                 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
222                         /*
223                          * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
224                          * allow complete duplication of binding if
225                          * SO_REUSEPORT is set, or if SO_REUSEADDR is set
226                          * and a multicast address is bound on both
227                          * new and duplicated sockets.
228                          */
229                         if (so->so_options & SO_REUSEADDR)
230                                 reuseport = SO_REUSEADDR|SO_REUSEPORT;
231                 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
232                         sin->sin_port = 0;              /* yech... */
233                         if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
234                                 return (EADDRNOTAVAIL);
235                 }
236                 if (lport) {
237                         struct inpcb *t;
238
239                         /* GROSS */
240                         t = in_pcblookup_local(pcbinfo, sin->sin_addr, lport, wild);
241                         if (t &&
242                             (reuseport & t->inp_socket->so_options) == 0) {
243 #if defined(INET6)
244                                 if (ntohl(sin->sin_addr.s_addr) !=
245                                     INADDR_ANY ||
246                                     ntohl(t->inp_laddr.s_addr) !=
247                                     INADDR_ANY ||
248                                     INP_SOCKAF(so) ==
249                                     INP_SOCKAF(t->inp_socket))
250 #endif /* defined(INET6) */
251                                 return (EADDRINUSE);
252                         }
253                 }
254                 inp->inp_laddr = sin->sin_addr;
255         }
256         if (lport == 0) {
257                 u_short first, last;
258                 int count;
259
260                 inp->inp_flags |= INP_ANONPORT;
261
262                 if (inp->inp_flags & INP_HIGHPORT) {
263                         first = ipport_hifirstauto;     /* sysctl */
264                         last  = ipport_hilastauto;
265                         lastport = &pcbinfo->lasthi;
266                 } else if (inp->inp_flags & INP_LOWPORT) {
267                         first = ipport_lowfirstauto;    /* 1023 */
268                         last  = ipport_lowlastauto;     /* 600 */
269                         lastport = &pcbinfo->lastlow;
270                 } else {
271                         first = ipport_firstauto;       /* sysctl */
272                         last  = ipport_lastauto;
273                         lastport = &pcbinfo->lastport;
274                 }
275                 /*
276                  * Simple check to ensure all ports are not used up causing
277                  * a deadlock here.
278                  *
279                  * We split the two cases (up and down) so that the direction
280                  * is not being tested on each round of the loop.
281                  */
282                 if (first > last) {
283                         /*
284                          * counting down
285                          */
286                          
287 #ifdef CYGPKG_NET_RANDOM_PORTS
288                         *lastport = first - (arc4random() % (first - last));
289 #endif                           
290                         count = first - last;
291
292                         do {
293                                 if (count-- < 0) {      /* completely used? */
294                                         inp->inp_laddr.s_addr = INADDR_ANY;
295                                         return (EADDRNOTAVAIL);
296                                 }
297                                 --*lastport;
298                                 if (*lastport > first || *lastport < last)
299                                         *lastport = first;
300                                 lport = htons(*lastport);
301                         } while (in_pcblookup_local(pcbinfo,
302                                  inp->inp_laddr, lport, wild));
303                 } else {
304                         /*
305                          * counting up
306                          */
307 #ifdef CYGPKG_NET_RANDOM_PORTS
308                         *lastport = first + (arc4random() % (last - first));
309 #endif                           
310                         count = last - first;
311
312                         do {
313                                 if (count-- < 0) {      /* completely used? */
314                                         inp->inp_laddr.s_addr = INADDR_ANY;
315                                         return (EADDRNOTAVAIL);
316                                 }
317                                 ++*lastport;
318                                 if (*lastport < first || *lastport > last)
319                                         *lastport = first;
320                                 lport = htons(*lastport);
321                         } while (in_pcblookup_local(pcbinfo,
322                                  inp->inp_laddr, lport, wild));
323                 }
324         }
325         inp->inp_lport = lport;
326         if (in_pcbinshash(inp) != 0) {
327                 inp->inp_laddr.s_addr = INADDR_ANY;
328                 inp->inp_lport = 0;
329                 return (EAGAIN);
330         }
331         return (0);
332 }
333
334 /*
335  *   Transform old in_pcbconnect() into an inner subroutine for new
336  *   in_pcbconnect(): Do some validity-checking on the remote
337  *   address (in mbuf 'nam') and then determine local host address
338  *   (i.e., which interface) to use to access that remote host.
339  *
340  *   This preserves definition of in_pcbconnect(), while supporting a
341  *   slightly different version for T/TCP.  (This is more than
342  *   a bit of a kludge, but cleaning up the internal interfaces would
343  *   have forced minor changes in every protocol).
344  */
345
346 int
347 in_pcbladdr(inp, nam, plocal_sin)
348         register struct inpcb *inp;
349         struct sockaddr *nam;
350         struct sockaddr_in **plocal_sin;
351 {
352         struct in_ifaddr *ia;
353         register struct sockaddr_in *sin = (struct sockaddr_in *)nam;
354
355         if (nam->sa_len != sizeof (*sin))
356                 return (EINVAL);
357         if (sin->sin_family != AF_INET)
358                 return (EAFNOSUPPORT);
359         if (sin->sin_port == 0)
360                 return (EADDRNOTAVAIL);
361         if (!TAILQ_EMPTY(&in_ifaddrhead)) {
362                 /*
363                  * If the destination address is INADDR_ANY,
364                  * use the primary local address.
365                  * If the supplied address is INADDR_BROADCAST,
366                  * and the primary interface supports broadcast,
367                  * choose the broadcast address for that interface.
368                  */
369 #define satosin(sa)     ((struct sockaddr_in *)(sa))
370 #define sintosa(sin)    ((struct sockaddr *)(sin))
371 #define ifatoia(ifa)    ((struct in_ifaddr *)(ifa))
372                 if (sin->sin_addr.s_addr == INADDR_ANY)
373                     sin->sin_addr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
374                 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
375                   (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags & IFF_BROADCAST))
376                     sin->sin_addr = satosin(&TAILQ_FIRST(&in_ifaddrhead)->ia_broadaddr)->sin_addr;
377         }
378         if (inp->inp_laddr.s_addr == INADDR_ANY) {
379                 register struct route *ro;
380
381                 ia = (struct in_ifaddr *)0;
382                 /*
383                  * If route is known or can be allocated now,
384                  * our src addr is taken from the i/f, else punt.
385                  */
386                 ro = &inp->inp_route;
387                 if (ro->ro_rt &&
388                     (satosin(&ro->ro_dst)->sin_addr.s_addr !=
389                         sin->sin_addr.s_addr ||
390                     inp->inp_socket->so_options & SO_DONTROUTE)) {
391                         RTFREE(ro->ro_rt);
392                         ro->ro_rt = (struct rtentry *)0;
393                 }
394                 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
395                     (ro->ro_rt == (struct rtentry *)0 ||
396                     ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
397                         /* No route yet, so try to acquire one */
398                         ro->ro_dst.sa_family = AF_INET;
399                         ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
400                         ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
401                                 sin->sin_addr;
402                         rtalloc(ro);
403                 }
404                 /*
405                  * If we found a route, use the address
406                  * corresponding to the outgoing interface
407                  * unless it is the loopback (in case a route
408                  * to our address on another net goes to loopback).
409                  */
410                 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK))
411                         ia = ifatoia(ro->ro_rt->rt_ifa);
412                 if (ia == 0) {
413                         u_short fport = sin->sin_port;
414
415                         sin->sin_port = 0;
416                         ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
417                         if (ia == 0)
418                                 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
419                         sin->sin_port = fport;
420                         if (ia == 0)
421                                 ia = TAILQ_FIRST(&in_ifaddrhead);
422                         if (ia == 0)
423                                 return (EADDRNOTAVAIL);
424                 }
425                 /*
426                  * If the destination address is multicast and an outgoing
427                  * interface has been set as a multicast option, use the
428                  * address of that interface as our source address.
429                  */
430                 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
431                     inp->inp_moptions != NULL) {
432                         struct ip_moptions *imo;
433                         struct ifnet *ifp;
434
435                         imo = inp->inp_moptions;
436                         if (imo->imo_multicast_ifp != NULL) {
437                                 ifp = imo->imo_multicast_ifp;
438                                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
439                                         if (ia->ia_ifp == ifp)
440                                                 break;
441                                 if (ia == 0)
442                                         return (EADDRNOTAVAIL);
443                         }
444                 }
445         /*
446          * Don't do pcblookup call here; return interface in plocal_sin
447          * and exit to caller, that will do the lookup.
448          */
449                 *plocal_sin = &ia->ia_addr;
450
451         }
452         return(0);
453 }
454
455 /*
456  * Outer subroutine:
457  * Connect from a socket to a specified address.
458  * Both address and port must be specified in argument sin.
459  * If don't have a local address for this socket yet,
460  * then pick one.
461  */
462 int
463 in_pcbconnect(inp, nam, p)
464         register struct inpcb *inp;
465         struct sockaddr *nam;
466         struct proc *p;
467 {
468         struct sockaddr_in *ifaddr;
469         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
470         int error;
471
472         /*
473          *   Call inner routine, to assign local interface address.
474          */
475         if ((error = in_pcbladdr(inp, nam, &ifaddr)) != 0)
476                 return(error);
477
478         if (in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port,
479             inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
480             inp->inp_lport, 0, NULL) != NULL) {
481                 return (EADDRINUSE);
482         }
483         if (inp->inp_laddr.s_addr == INADDR_ANY) {
484                 if (inp->inp_lport == 0) {
485                         error = in_pcbbind(inp, (struct sockaddr *)0, p);
486                         if (error)
487                             return (error);
488                 }
489                 inp->inp_laddr = ifaddr->sin_addr;
490         }
491         inp->inp_faddr = sin->sin_addr;
492         inp->inp_fport = sin->sin_port;
493         in_pcbrehash(inp);
494 #ifdef IPSEC
495         if (inp->inp_socket->so_type == SOCK_STREAM)
496                 ipsec_pcbconn(inp->inp_sp);
497 #endif
498         return (0);
499 }
500
501 void
502 in_pcbdisconnect(inp)
503         struct inpcb *inp;
504 {
505
506         inp->inp_faddr.s_addr = INADDR_ANY;
507         inp->inp_fport = 0;
508         in_pcbrehash(inp);
509         if (inp->inp_socket->so_state & SS_NOFDREF)
510                 in_pcbdetach(inp);
511 #ifdef IPSEC
512         ipsec_pcbdisconn(inp->inp_sp);
513 #endif
514 }
515
516 void
517 in_pcbdetach(inp)
518         struct inpcb *inp;
519 {
520         struct socket *so = inp->inp_socket;
521         struct inpcbinfo *ipi = inp->inp_pcbinfo;
522         struct rtentry *rt  = inp->inp_route.ro_rt;
523
524 #ifdef IPSEC
525         ipsec4_delete_pcbpolicy(inp);
526 #endif /*IPSEC*/
527         inp->inp_gencnt = ++ipi->ipi_gencnt;
528         in_pcbremlists(inp);
529         so->so_pcb = 0;
530         sofree(so);
531         if (inp->inp_options)
532                 (void)m_free(inp->inp_options);
533         if (rt) {
534                 /* 
535                  * route deletion requires reference count to be <= zero 
536                  */
537                 if ((rt->rt_flags & RTF_DELCLONE) &&
538                     (rt->rt_flags & RTF_WASCLONED) &&
539                     (rt->rt_refcnt <= 1)) {
540                         rt->rt_refcnt--;
541                         rt->rt_flags &= ~RTF_UP;
542                         rtrequest(RTM_DELETE, rt_key(rt),
543                                   rt->rt_gateway, rt_mask(rt),
544                                   rt->rt_flags, (struct rtentry **)0);
545                 }
546                 else
547                         rtfree(rt);
548         }
549         ip_freemoptions(inp->inp_moptions);
550         inp->inp_vflag = 0;
551         zfreei(ipi->ipi_zone, inp);
552 }
553
554 /*
555  * The calling convention of in_setsockaddr() and in_setpeeraddr() was
556  * modified to match the pru_sockaddr() and pru_peeraddr() entry points
557  * in struct pr_usrreqs, so that protocols can just reference then directly
558  * without the need for a wrapper function.  The socket must have a valid
559  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
560  * except through a kernel programming error, so it is acceptable to panic
561  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
562  * because there actually /is/ a programming error somewhere... XXX)
563  */
564 int
565 in_setsockaddr(so, nam)
566         struct socket *so;
567         struct sockaddr **nam;
568 {
569         int s;
570         register struct inpcb *inp;
571         register struct sockaddr_in *sin;
572
573         /*
574          * Do the malloc first in case it blocks.
575          */
576         MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
577                 M_WAITOK | M_ZERO);
578         sin->sin_family = AF_INET;
579         sin->sin_len = sizeof(*sin);
580
581         s = splnet();
582         inp = sotoinpcb(so);
583         if (!inp) {
584                 splx(s);
585                 free(sin, M_SONAME);
586                 return ECONNRESET;
587         }
588         sin->sin_port = inp->inp_lport;
589         sin->sin_addr = inp->inp_laddr;
590         splx(s);
591
592         *nam = (struct sockaddr *)sin;
593         return 0;
594 }
595
596 int
597 in_setpeeraddr(so, nam)
598         struct socket *so;
599         struct sockaddr **nam;
600 {
601         int s;
602         struct inpcb *inp;
603         register struct sockaddr_in *sin;
604
605         /*
606          * Do the malloc first in case it blocks.
607          */
608         MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
609                 M_WAITOK | M_ZERO);
610         sin->sin_family = AF_INET;
611         sin->sin_len = sizeof(*sin);
612
613         s = splnet();
614         inp = sotoinpcb(so);
615         if (!inp) {
616                 splx(s);
617                 free(sin, M_SONAME);
618                 return ECONNRESET;
619         }
620         sin->sin_port = inp->inp_fport;
621         sin->sin_addr = inp->inp_faddr;
622         splx(s);
623
624         *nam = (struct sockaddr *)sin;
625         return 0;
626 }
627
628 void
629 in_pcbnotifyall(head, faddr, _errno, notify)
630         struct inpcbhead *head;
631         struct in_addr faddr;
632         void (*notify) __P((struct inpcb *, int));
633 {
634         struct inpcb *inp, *ninp;
635         int s;
636
637         s = splnet();
638         for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
639                 ninp = LIST_NEXT(inp, inp_list);
640 #ifdef INET6
641                 if ((inp->inp_vflag & INP_IPV4) == 0)
642                         continue;
643 #endif
644                 if (inp->inp_faddr.s_addr != faddr.s_addr ||
645                     inp->inp_socket == NULL)
646                                 continue;
647                 (*notify)(inp, _errno);
648         }
649         splx(s);
650 }
651
652 void
653 in_pcbpurgeif0(head, ifp)
654         struct inpcb *head;
655         struct ifnet *ifp;
656 {
657         struct inpcb *inp;
658         struct ip_moptions *imo;
659         int i, gap;
660
661         for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
662                 imo = inp->inp_moptions;
663                 if ((inp->inp_vflag & INP_IPV4) &&
664                     imo != NULL) {
665                         /*
666                          * Unselect the outgoing interface if it is being
667                          * detached.
668                          */
669                         if (imo->imo_multicast_ifp == ifp)
670                                 imo->imo_multicast_ifp = NULL;
671
672                         /*
673                          * Drop multicast group membership if we joined
674                          * through the interface being detached.
675                          */
676                         for (i = 0, gap = 0; i < imo->imo_num_memberships;
677                             i++) {
678                                 if (imo->imo_membership[i]->inm_ifp == ifp) {
679                                         in_delmulti(imo->imo_membership[i]);
680                                         gap++;
681                                 } else if (gap != 0)
682                                         imo->imo_membership[i - gap] =
683                                             imo->imo_membership[i];
684                         }
685                         imo->imo_num_memberships -= gap;
686                 }
687         }
688 }
689
690 /*
691  * Check for alternatives when higher level complains
692  * about service problems.  For now, invalidate cached
693  * routing information.  If the route was created dynamically
694  * (by a redirect), time to try a default gateway again.
695  */
696 void
697 in_losing(inp)
698         struct inpcb *inp;
699 {
700         register struct rtentry *rt;
701         struct rt_addrinfo info;
702
703         if ((rt = inp->inp_route.ro_rt)) {
704                 bzero((caddr_t)&info, sizeof(info));
705                 info.rti_info[RTAX_DST] =
706                         (struct sockaddr *)&inp->inp_route.ro_dst;
707                 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
708                 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
709                 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
710                 if (rt->rt_flags & RTF_DYNAMIC)
711                         (void) rtrequest(RTM_DELETE, rt_key(rt),
712                                 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
713                                 (struct rtentry **)0);
714                 inp->inp_route.ro_rt = 0;
715                 rtfree(rt);
716                 /*
717                  * A new route can be allocated
718                  * the next time output is attempted.
719                  */
720         }
721 }
722
723 /*
724  * After a routing change, flush old routing
725  * and allocate a (hopefully) better one.
726  */
727 void
728 in_rtchange(inp, _errno)
729         register struct inpcb *inp;
730         int _errno;
731 {
732         if (inp->inp_route.ro_rt) {
733                 rtfree(inp->inp_route.ro_rt);
734                 inp->inp_route.ro_rt = 0;
735                 /*
736                  * A new route can be allocated the next time
737                  * output is attempted.
738                  */
739         }
740 }
741
742 /*
743  * Lookup a PCB based on the local address and port.
744  */
745 struct inpcb *
746 in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
747         struct inpcbinfo *pcbinfo;
748         struct in_addr laddr;
749         u_int lport_arg;
750         int wild_okay;
751 {
752         register struct inpcb *inp;
753         int matchwild = 3, wildcard;
754         u_short lport = lport_arg;
755
756         if (!wild_okay) {
757                 struct inpcbhead *head;
758                 /*
759                  * Look for an unconnected (wildcard foreign addr) PCB that
760                  * matches the local address and port we're looking for.
761                  */
762                 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
763                 LIST_FOREACH(inp, head, inp_hash) {
764 #ifdef INET6
765                         if ((inp->inp_vflag & INP_IPV4) == 0)
766                                 continue;
767 #endif
768                         if (inp->inp_faddr.s_addr == INADDR_ANY &&
769                             inp->inp_laddr.s_addr == laddr.s_addr &&
770                             inp->inp_lport == lport) {
771                                 /*
772                                  * Found.
773                                  */
774                                 return (inp);
775                         }
776                 }
777                 /*
778                  * Not found.
779                  */
780                 return (NULL);
781         } else {
782                 struct inpcbporthead *porthash;
783                 struct inpcbport *phd;
784                 struct inpcb *match = NULL;
785                 /*
786                  * Best fit PCB lookup.
787                  *
788                  * First see if this local port is in use by looking on the
789                  * port hash list.
790                  */
791                 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
792                     pcbinfo->porthashmask)];
793                 LIST_FOREACH(phd, porthash, phd_hash) {
794                         if (phd->phd_port == lport)
795                                 break;
796                 }
797                 if (phd != NULL) {
798                         /*
799                          * Port is in use by one or more PCBs. Look for best
800                          * fit.
801                          */
802                         LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
803                                 wildcard = 0;
804 #ifdef INET6
805                                 if ((inp->inp_vflag & INP_IPV4) == 0)
806                                         continue;
807 #endif
808                                 if (inp->inp_faddr.s_addr != INADDR_ANY)
809                                         wildcard++;
810                                 if (inp->inp_laddr.s_addr != INADDR_ANY) {
811                                         if (laddr.s_addr == INADDR_ANY)
812                                                 wildcard++;
813                                         else if (inp->inp_laddr.s_addr != laddr.s_addr)
814                                                 continue;
815                                 } else {
816                                         if (laddr.s_addr != INADDR_ANY)
817                                                 wildcard++;
818                                 }
819                                 if (wildcard < matchwild) {
820                                         match = inp;
821                                         matchwild = wildcard;
822                                         if (matchwild == 0) {
823                                                 break;
824                                         }
825                                 }
826                         }
827                 }
828                 return (match);
829         }
830 }
831
832 /*
833  * Lookup PCB in hash list.
834  */
835 struct inpcb *
836 in_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard,
837                   ifp)
838         struct inpcbinfo *pcbinfo;
839         struct in_addr faddr, laddr;
840         u_int fport_arg, lport_arg;
841         int wildcard;
842         struct ifnet *ifp;
843 {
844         struct inpcbhead *head;
845         register struct inpcb *inp;
846         u_short fport = fport_arg, lport = lport_arg;
847
848         /*
849          * First look for an exact match.
850          */
851         head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
852         LIST_FOREACH(inp, head, inp_hash) {
853 #ifdef INET6
854                 if ((inp->inp_vflag & INP_IPV4) == 0)
855                         continue;
856 #endif
857                 if (inp->inp_faddr.s_addr == faddr.s_addr &&
858                     inp->inp_laddr.s_addr == laddr.s_addr &&
859                     inp->inp_fport == fport &&
860                     inp->inp_lport == lport) {
861                         /*
862                          * Found.
863                          */
864                         return (inp);
865                 }
866         }
867         if (wildcard) {
868                 struct inpcb *local_wild = NULL;
869 #if defined(INET6)
870                 struct inpcb *local_wild_mapped = NULL;
871 #endif /* defined(INET6) */
872
873                 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
874                 LIST_FOREACH(inp, head, inp_hash) {
875 #ifdef INET6
876                         if ((inp->inp_vflag & INP_IPV4) == 0)
877                                 continue;
878 #endif
879                         if (inp->inp_faddr.s_addr == INADDR_ANY &&
880                             inp->inp_lport == lport) {
881 #if defined(NFAITH) && NFAITH > 0
882                                 if (ifp && ifp->if_type == IFT_FAITH &&
883                                     (inp->inp_flags & INP_FAITH) == 0)
884                                         continue;
885 #endif
886                                 if (inp->inp_laddr.s_addr == laddr.s_addr)
887                                         return (inp);
888                                 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
889 #if defined(INET6)
890                                         if (INP_CHECK_SOCKAF(inp->inp_socket,
891                                                              AF_INET6))
892                                                 local_wild_mapped = inp;
893                                         else
894 #endif /* defined(INET6) */
895                                         local_wild = inp;
896                                 }
897                         }
898                 }
899 #if defined(INET6)
900                 if (local_wild == NULL)
901                         return (local_wild_mapped);
902 #endif /* defined(INET6) */
903                 return (local_wild);
904         }
905
906         /*
907          * Not found.
908          */
909         return (NULL);
910 }
911
912 /*
913  * Insert PCB onto various hash lists.
914  */
915 int
916 in_pcbinshash(inp)
917         struct inpcb *inp;
918 {
919         struct inpcbhead *pcbhash;
920         struct inpcbporthead *pcbporthash;
921         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
922         struct inpcbport *phd;
923         u_int32_t hashkey_faddr;
924
925 #ifdef INET6
926         if (inp->inp_vflag & INP_IPV6)
927                 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
928         else
929 #endif /* INET6 */
930         hashkey_faddr = inp->inp_faddr.s_addr;
931
932         pcbhash = &pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
933                  inp->inp_lport, inp->inp_fport, pcbinfo->hashmask)];
934
935         pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
936             pcbinfo->porthashmask)];
937
938         /*
939          * Go through port list and look for a head for this lport.
940          */
941         LIST_FOREACH(phd, pcbporthash, phd_hash) {
942                 if (phd->phd_port == inp->inp_lport)
943                         break;
944         }
945         /*
946          * If none exists, malloc one and tack it on.
947          */
948         if (phd == NULL) {
949                 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_NOWAIT);
950                 if (phd == NULL) {
951                         return (ENOBUFS); /* XXX */
952                 }
953                 phd->phd_port = inp->inp_lport;
954                 LIST_INIT(&phd->phd_pcblist);
955                 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
956         }
957         inp->inp_phd = phd;
958         LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
959         LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
960         return (0);
961 }
962
963 /*
964  * Move PCB to the proper hash bucket when { faddr, fport } have  been
965  * changed. NOTE: This does not handle the case of the lport changing (the
966  * hashed port list would have to be updated as well), so the lport must
967  * not change after in_pcbinshash() has been called.
968  */
969 void
970 in_pcbrehash(inp)
971         struct inpcb *inp;
972 {
973         struct inpcbhead *head;
974         u_int32_t hashkey_faddr;
975
976 #ifdef INET6
977         if (inp->inp_vflag & INP_IPV6)
978                 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
979         else
980 #endif /* INET6 */
981         hashkey_faddr = inp->inp_faddr.s_addr;
982
983         head = &inp->inp_pcbinfo->hashbase[INP_PCBHASH(hashkey_faddr,
984                 inp->inp_lport, inp->inp_fport, inp->inp_pcbinfo->hashmask)];
985
986         LIST_REMOVE(inp, inp_hash);
987         LIST_INSERT_HEAD(head, inp, inp_hash);
988 }
989
990 /*
991  * Remove PCB from various lists.
992  */
993 void
994 in_pcbremlists(inp)
995         struct inpcb *inp;
996 {
997         inp->inp_gencnt = ++inp->inp_pcbinfo->ipi_gencnt;
998         if (inp->inp_lport) {
999                 struct inpcbport *phd = inp->inp_phd;
1000
1001                 LIST_REMOVE(inp, inp_hash);
1002                 LIST_REMOVE(inp, inp_portlist);
1003                 if (LIST_FIRST(&phd->phd_pcblist) == NULL) {
1004                         LIST_REMOVE(phd, phd_hash);
1005                         free(phd, M_PCB);
1006                 }
1007         }
1008         LIST_REMOVE(inp, inp_list);
1009         inp->inp_pcbinfo->ipi_count--;
1010 }