OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [net/] [bsd_tcpip/] [current/] [src/] [sys/] [netinet/] [in_pcb.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      src/sys/netinet/in_pcb.c
4
//
5
//==========================================================================
6
// ####BSDCOPYRIGHTBEGIN####                                    
7
// -------------------------------------------                  
8
// This file is part of eCos, the Embedded Configurable Operating System.
9
//
10
// Portions of this software may have been derived from FreeBSD 
11
// or other sources, and if so are covered by the appropriate copyright
12
// and license included herein.                                 
13
//
14
// Portions created by the Free Software Foundation are         
15
// Copyright (C) 2002 Free Software Foundation, Inc.            
16
// -------------------------------------------                  
17
// ####BSDCOPYRIGHTEND####                                      
18
//==========================================================================
19
 
20
/*
21
 * Copyright (c) 1982, 1986, 1991, 1993, 1995
22
 *      The Regents of the University of California.  All rights reserved.
23
 *
24
 * Redistribution and use in source and binary forms, with or without
25
 * modification, are permitted provided that the following conditions
26
 * are met:
27
 * 1. Redistributions of source code must retain the above copyright
28
 *    notice, this list of conditions and the following disclaimer.
29
 * 2. Redistributions in binary form must reproduce the above copyright
30
 *    notice, this list of conditions and the following disclaimer in the
31
 *    documentation and/or other materials provided with the distribution.
32
 * 3. All advertising materials mentioning features or use of this software
33
 *    must display the following acknowledgement:
34
 *      This product includes software developed by the University of
35
 *      California, Berkeley and its contributors.
36
 * 4. Neither the name of the University nor the names of its contributors
37
 *    may be used to endorse or promote products derived from this software
38
 *    without specific prior written permission.
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
41
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
44
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
 * SUCH DAMAGE.
51
 *
52
 *      @(#)in_pcb.c    8.4 (Berkeley) 5/24/95
53
 * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.17 2001/08/13 16:26:17 ume Exp $
54
 */
55
 
56
#include <sys/param.h>
57
#include <sys/malloc.h>
58
#include <sys/mbuf.h>
59
#include <sys/domain.h>
60
#include <sys/protosw.h>
61
#include <sys/socket.h>
62
#include <sys/socketvar.h>
63
#include <sys/sysctl.h>
64
 
65
#include <net/if.h>
66
#include <net/if_types.h>
67
#include <net/route.h>
68
 
69
#include <netinet/in.h>
70
#include <netinet/in_pcb.h>
71
#include <netinet/in_var.h>
72
#include <netinet/ip_var.h>
73
#ifdef INET6
74
#include <netinet/ip6.h>
75
#include <netinet6/ip6_var.h>
76
#endif /* INET6 */
77
 
78
#ifdef IPSEC
79
#include <netinet6/ipsec.h>
80
#include <netkey/key.h>
81
#endif /* IPSEC */
82
 
83
struct  in_addr zeroin_addr;
84
 
85
/*
86
 * These configure the range of local port addresses assigned to
87
 * "unspecified" outgoing connections/packets/whatever.
88
 */
89
int     ipport_lowfirstauto  = IPPORT_RESERVED - 1;     /* 1023 */
90
int     ipport_lowlastauto = IPPORT_RESERVEDSTART;      /* 600 */
91
int     ipport_firstauto = IPPORT_RESERVED;             /* 1024 */
92
int     ipport_lastauto  = IPPORT_USERRESERVED;         /* 5000 */
93
int     ipport_hifirstauto = IPPORT_HIFIRSTAUTO;        /* 49152 */
94
int     ipport_hilastauto  = IPPORT_HILASTAUTO;         /* 65535 */
95
 
96
#define RANGECHK(var, min, max) \
97
        if ((var) < (min)) { (var) = (min); } \
98
        else if ((var) > (max)) { (var) = (max); }
99
 
100
#ifdef CYGPKG_NET_FREEBSD_SYSCTL
101
static int
102
sysctl_net_ipport_check(SYSCTL_HANDLER_ARGS)
103
{
104
        int error = sysctl_handle_int(oidp,
105
                oidp->oid_arg1, oidp->oid_arg2, req);
106
        if (!error) {
107
                RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
108
                RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
109
                RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
110
                RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
111
                RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
112
                RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
113
        }
114
        return error;
115
}
116
#endif
117
#undef RANGECHK
118
 
119
SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
120
 
121
SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
122
           &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
123
SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
124
           &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
125
SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
126
           &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
127
SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
128
           &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
129
SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
130
           &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
131
SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
132
           &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
133
 
134
/*
135
 * in_pcb.c: manage the Protocol Control Blocks.
136
 *
137
 * NOTE: It is assumed that most of these functions will be called at
138
 * splnet(). XXX - There are, unfortunately, a few exceptions to this
139
 * rule that should be fixed.
140
 */
141
 
142
/*
143
 * Allocate a PCB and associate it with the socket.
144
 */
145
int
146
in_pcballoc(so, pcbinfo, p)
147
        struct socket *so;
148
        struct inpcbinfo *pcbinfo;
149
        struct proc *p;
150
{
151
        register struct inpcb *inp;
152
#ifdef IPSEC
153
        int error;
154
#endif
155
 
156
        inp = zalloci(pcbinfo->ipi_zone);
157
        if (inp == NULL)
158
                return (ENOBUFS);
159
        bzero((caddr_t)inp, sizeof(*inp));
160
        inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
161
        inp->inp_pcbinfo = pcbinfo;
162
        inp->inp_socket = so;
163
#ifdef IPSEC
164
        error = ipsec_init_policy(so, &inp->inp_sp);
165
        if (error != 0) {
166
                zfreei(pcbinfo->ipi_zone, inp);
167
                return error;
168
        }
169
#endif /*IPSEC*/
170
#if defined(INET6)
171
        if (INP_SOCKAF(so) == AF_INET6 && !ip6_mapped_addr_on)
172
                inp->inp_flags |= IN6P_IPV6_V6ONLY;
173
#endif
174
        LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
175
        pcbinfo->ipi_count++;
176
        so->so_pcb = (caddr_t)inp;
177
#ifdef INET6
178
        if (ip6_auto_flowlabel)
179
                inp->inp_flags |= IN6P_AUTOFLOWLABEL;
180
#endif
181
        return (0);
182
}
183
 
184
int
185
in_pcbbind(inp, nam, p)
186
        register struct inpcb *inp;
187
        struct sockaddr *nam;
188
        struct proc *p;
189
{
190
        register struct socket *so = inp->inp_socket;
191
        unsigned short *lastport;
192
        struct sockaddr_in *sin;
193
        struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
194
        u_short lport = 0;
195
        int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
196
 
197
        if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
198
                return (EADDRNOTAVAIL);
199
        if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
200
                return (EINVAL);
201
        if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
202
                wild = 1;
203
        if (nam) {
204
                sin = (struct sockaddr_in *)nam;
205
        // HACK
206
        if (nam->sa_len == 0) nam->sa_len = sizeof(*sin);
207
        // HACK
208
                if (nam->sa_len != sizeof (*sin))
209
                        return (EINVAL);
210
#ifdef notdef
211
                /*
212
                 * We should check the family, but old programs
213
                 * incorrectly fail to initialize it.
214
                 */
215
                if (sin->sin_family != AF_INET)
216
                        return (EAFNOSUPPORT);
217
#endif
218
                lport = sin->sin_port;
219
                if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
220
                        /*
221
                         * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
222
                         * allow complete duplication of binding if
223
                         * SO_REUSEPORT is set, or if SO_REUSEADDR is set
224
                         * and a multicast address is bound on both
225
                         * new and duplicated sockets.
226
                         */
227
                        if (so->so_options & SO_REUSEADDR)
228
                                reuseport = SO_REUSEADDR|SO_REUSEPORT;
229
                } else if (sin->sin_addr.s_addr != INADDR_ANY) {
230
                        sin->sin_port = 0;               /* yech... */
231
                        if (ifa_ifwithaddr((struct sockaddr *)sin) == 0)
232
                                return (EADDRNOTAVAIL);
233
                }
234
                if (lport) {
235
                        struct inpcb *t;
236
 
237
                        /* GROSS */
238
                        t = in_pcblookup_local(pcbinfo, sin->sin_addr, lport, wild);
239
                        if (t &&
240
                            (reuseport & t->inp_socket->so_options) == 0) {
241
#if defined(INET6)
242
                                if (ntohl(sin->sin_addr.s_addr) !=
243
                                    INADDR_ANY ||
244
                                    ntohl(t->inp_laddr.s_addr) !=
245
                                    INADDR_ANY ||
246
                                    INP_SOCKAF(so) ==
247
                                    INP_SOCKAF(t->inp_socket))
248
#endif /* defined(INET6) */
249
                                return (EADDRINUSE);
250
                        }
251
                }
252
                inp->inp_laddr = sin->sin_addr;
253
        }
254
        if (lport == 0) {
255
                u_short first, last;
256
                int count;
257
 
258
                inp->inp_flags |= INP_ANONPORT;
259
 
260
                if (inp->inp_flags & INP_HIGHPORT) {
261
                        first = ipport_hifirstauto;     /* sysctl */
262
                        last  = ipport_hilastauto;
263
                        lastport = &pcbinfo->lasthi;
264
                } else if (inp->inp_flags & INP_LOWPORT) {
265
                        first = ipport_lowfirstauto;    /* 1023 */
266
                        last  = ipport_lowlastauto;     /* 600 */
267
                        lastport = &pcbinfo->lastlow;
268
                } else {
269
                        first = ipport_firstauto;       /* sysctl */
270
                        last  = ipport_lastauto;
271
                        lastport = &pcbinfo->lastport;
272
                }
273
                /*
274
                 * Simple check to ensure all ports are not used up causing
275
                 * a deadlock here.
276
                 *
277
                 * We split the two cases (up and down) so that the direction
278
                 * is not being tested on each round of the loop.
279
                 */
280
                if (first > last) {
281
                        /*
282
                         * counting down
283
                         */
284
 
285
#ifdef CYGPKG_NET_RANDOM_PORTS
286
                        *lastport = first - (arc4random() % (first - last));
287
#endif                           
288
                        count = first - last;
289
 
290
                        do {
291
                                if (count-- < 0) {       /* completely used? */
292
                                        inp->inp_laddr.s_addr = INADDR_ANY;
293
                                        return (EADDRNOTAVAIL);
294
                                }
295
                                --*lastport;
296
                                if (*lastport > first || *lastport < last)
297
                                        *lastport = first;
298
                                lport = htons(*lastport);
299
                        } while (in_pcblookup_local(pcbinfo,
300
                                 inp->inp_laddr, lport, wild));
301
                } else {
302
                        /*
303
                         * counting up
304
                         */
305
#ifdef CYGPKG_NET_RANDOM_PORTS
306
                        *lastport = first + (arc4random() % (last - first));
307
#endif                           
308
                        count = last - first;
309
 
310
                        do {
311
                                if (count-- < 0) {       /* completely used? */
312
                                        inp->inp_laddr.s_addr = INADDR_ANY;
313
                                        return (EADDRNOTAVAIL);
314
                                }
315
                                ++*lastport;
316
                                if (*lastport < first || *lastport > last)
317
                                        *lastport = first;
318
                                lport = htons(*lastport);
319
                        } while (in_pcblookup_local(pcbinfo,
320
                                 inp->inp_laddr, lport, wild));
321
                }
322
        }
323
        inp->inp_lport = lport;
324
        if (in_pcbinshash(inp) != 0) {
325
                inp->inp_laddr.s_addr = INADDR_ANY;
326
                inp->inp_lport = 0;
327
                return (EAGAIN);
328
        }
329
        return (0);
330
}
331
 
332
/*
333
 *   Transform old in_pcbconnect() into an inner subroutine for new
334
 *   in_pcbconnect(): Do some validity-checking on the remote
335
 *   address (in mbuf 'nam') and then determine local host address
336
 *   (i.e., which interface) to use to access that remote host.
337
 *
338
 *   This preserves definition of in_pcbconnect(), while supporting a
339
 *   slightly different version for T/TCP.  (This is more than
340
 *   a bit of a kludge, but cleaning up the internal interfaces would
341
 *   have forced minor changes in every protocol).
342
 */
343
 
344
int
345
in_pcbladdr(inp, nam, plocal_sin)
346
        register struct inpcb *inp;
347
        struct sockaddr *nam;
348
        struct sockaddr_in **plocal_sin;
349
{
350
        struct in_ifaddr *ia;
351
        register struct sockaddr_in *sin = (struct sockaddr_in *)nam;
352
 
353
        if (nam->sa_len != sizeof (*sin))
354
                return (EINVAL);
355
        if (sin->sin_family != AF_INET)
356
                return (EAFNOSUPPORT);
357
        if (sin->sin_port == 0)
358
                return (EADDRNOTAVAIL);
359
        if (!TAILQ_EMPTY(&in_ifaddrhead)) {
360
                /*
361
                 * If the destination address is INADDR_ANY,
362
                 * use the primary local address.
363
                 * If the supplied address is INADDR_BROADCAST,
364
                 * and the primary interface supports broadcast,
365
                 * choose the broadcast address for that interface.
366
                 */
367
#define satosin(sa)     ((struct sockaddr_in *)(sa))
368
#define sintosa(sin)    ((struct sockaddr *)(sin))
369
#define ifatoia(ifa)    ((struct in_ifaddr *)(ifa))
370
                if (sin->sin_addr.s_addr == INADDR_ANY)
371
                    sin->sin_addr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
372
#ifdef CYGOPT_NET_FREEBSD_FORCE_DIRECTED_BROADCAST
373
                else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
374
                  (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags & IFF_BROADCAST))
375
                    sin->sin_addr = satosin(&TAILQ_FIRST(&in_ifaddrhead)->ia_broadaddr)->sin_addr;
376
#endif
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
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.