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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [libnetworking/] [netinet/] [in.c] - Blame information for rev 30

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 30 unneback
/*
2
 * Copyright (c) 1982, 1986, 1991, 1993
3
 *      The Regents of the University of California.  All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 * 3. All advertising materials mentioning features or use of this software
14
 *    must display the following acknowledgement:
15
 *      This product includes software developed by the University of
16
 *      California, Berkeley and its contributors.
17
 * 4. Neither the name of the University nor the names of its contributors
18
 *    may be used to endorse or promote products derived from this software
19
 *    without specific prior written permission.
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
 * SUCH DAMAGE.
32
 *
33
 *      @(#)in.c        8.4 (Berkeley) 1/9/95
34
 *      $Id: in.c,v 1.2 2001-09-27 12:01:55 chris Exp $
35
 */
36
 
37
#include <sys/param.h>
38
#include <sys/queue.h>
39
#include <sys/systm.h>
40
#include <sys/ioctl.h>
41
#include <sys/errno.h>
42
#include <sys/malloc.h>
43
#include <sys/socket.h>
44
#include <sys/socketvar.h>
45
#include <sys/kernel.h>
46
#include <sys/sysctl.h>
47
 
48
#include <net/if.h>
49
#include <net/route.h>
50
 
51
#include <netinet/in_systm.h>
52
#include <netinet/in.h>
53
#include <netinet/in_var.h>
54
#include <netinet/if_ether.h>
55
 
56
#include <netinet/igmp_var.h>
57
 
58
/*
59
 * This structure is used to keep track of in_multi chains which belong to
60
 * deleted interface addresses.
61
 */
62
static LIST_HEAD(, multi_kludge) in_mk; /* XXX BSS initialization */
63
 
64
struct multi_kludge {
65
        LIST_ENTRY(multi_kludge) mk_entry;
66
        struct ifnet *mk_ifp;
67
        struct in_multihead mk_head;
68
};
69
 
70
static void     in_socktrim __P((struct sockaddr_in *));
71
static int      in_ifinit __P((struct ifnet *,
72
            struct in_ifaddr *, struct sockaddr_in *, int));
73
static void     in_ifscrub __P((struct ifnet *, struct in_ifaddr *));
74
 
75
static int subnetsarelocal = 0;
76
SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
77
        &subnetsarelocal, 0, "");
78
/*
79
 * Return 1 if an internet address is for a ``local'' host
80
 * (one to which we have a connection).  If subnetsarelocal
81
 * is true, this includes other subnets of the local net.
82
 * Otherwise, it includes only the directly-connected (sub)nets.
83
 */
84
int
85
in_localaddr(in)
86
        struct in_addr in;
87
{
88
        register u_long i = ntohl(in.s_addr);
89
        register struct in_ifaddr *ia;
90
 
91
        if (subnetsarelocal) {
92
                for (ia = in_ifaddr; ia; ia = ia->ia_next)
93
                        if ((i & ia->ia_netmask) == ia->ia_net)
94
                                return (1);
95
        } else {
96
                for (ia = in_ifaddr; ia; ia = ia->ia_next)
97
                        if ((i & ia->ia_subnetmask) == ia->ia_subnet)
98
                                return (1);
99
        }
100
        return (0);
101
}
102
 
103
/*
104
 * Determine whether an IP address is in a reserved set of addresses
105
 * that may not be forwarded, or whether datagrams to that destination
106
 * may be forwarded.
107
 */
108
int
109
in_canforward(in)
110
        struct in_addr in;
111
{
112
        register u_long i = ntohl(in.s_addr);
113
        register u_long net;
114
 
115
        if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
116
                return (0);
117
        if (IN_CLASSA(i)) {
118
                net = i & IN_CLASSA_NET;
119
                if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
120
                        return (0);
121
        }
122
        return (1);
123
}
124
 
125
/*
126
 * Trim a mask in a sockaddr
127
 */
128
static void
129
in_socktrim(ap)
130
struct sockaddr_in *ap;
131
{
132
    register char *cplim = (char *) &ap->sin_addr;
133
    register char *cp = (char *) (&ap->sin_addr + 1);
134
 
135
    ap->sin_len = 0;
136
    while (--cp >= cplim)
137
        if (*cp) {
138
            (ap)->sin_len = cp - (char *) (ap) + 1;
139
            break;
140
        }
141
}
142
 
143
static int in_interfaces;       /* number of external internet interfaces */
144
 
145
/*
146
 * Generic internet control operations (ioctl's).
147
 * Ifp is 0 if not an interface-specific ioctl.
148
 */
149
/* ARGSUSED */
150
int
151
in_control(so, cmd, data, ifp)
152
        struct socket *so;
153
        u_long cmd;
154
        caddr_t data;
155
        register struct ifnet *ifp;
156
{
157
        register struct ifreq *ifr = (struct ifreq *)data;
158
        register struct in_ifaddr *ia = 0, *iap;
159
        register struct ifaddr *ifa;
160
        struct in_ifaddr *oia;
161
        struct in_aliasreq *ifra = (struct in_aliasreq *)data;
162
        struct sockaddr_in oldaddr;
163
        int error, hostIsNew, maskIsNew, s;
164
        u_long i;
165
        struct multi_kludge *mk;
166
 
167
        /*
168
         * Find address for this interface, if it exists.
169
         *
170
         * If an alias address was specified, find that one instead of
171
         * the first one on the interface.
172
         */
173
        if (ifp)
174
                for (iap = in_ifaddr; iap; iap = iap->ia_next)
175
                        if (iap->ia_ifp == ifp) {
176
                                if (((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr ==
177
                                    iap->ia_addr.sin_addr.s_addr) {
178
                                        ia = iap;
179
                                        break;
180
                                } else if (ia == NULL) {
181
                                        ia = iap;
182
                                        if (ifr->ifr_addr.sa_family != AF_INET)
183
                                                break;
184
                                }
185
                        }
186
 
187
        switch (cmd) {
188
 
189
        case SIOCAIFADDR:
190
        case SIOCDIFADDR:
191
                if (ifra->ifra_addr.sin_family == AF_INET) {
192
                        for (oia = ia; ia; ia = ia->ia_next) {
193
                                if (ia->ia_ifp == ifp  &&
194
                                    ia->ia_addr.sin_addr.s_addr ==
195
                                    ifra->ifra_addr.sin_addr.s_addr)
196
                                        break;
197
                        }
198
                        if ((ifp->if_flags & IFF_POINTOPOINT)
199
                            && (cmd == SIOCAIFADDR)
200
                            && (ifra->ifra_dstaddr.sin_addr.s_addr
201
                                == INADDR_ANY)) {
202
                                return EDESTADDRREQ;
203
                        }
204
                }
205
                if (cmd == SIOCDIFADDR && ia == 0)
206
                        return (EADDRNOTAVAIL);
207
                /* FALLTHROUGH */
208
        case SIOCSIFADDR:
209
        case SIOCSIFNETMASK:
210
        case SIOCSIFDSTADDR:
211
                if ((so->so_state & SS_PRIV) == 0)
212
                        return (EPERM);
213
 
214
                if (ifp == 0)
215
                        panic("in_control");
216
                if (ia == (struct in_ifaddr *)0) {
217
                        oia = (struct in_ifaddr *)
218
                                malloc(sizeof *oia, M_IFADDR, M_WAITOK);
219
                        if (oia == (struct in_ifaddr *)NULL)
220
                                return (ENOBUFS);
221
                        bzero((caddr_t)oia, sizeof *oia);
222
                        ia = in_ifaddr;
223
                        /*
224
                         * Protect from ipintr() traversing address list
225
                         * while we're modifying it.
226
                         */
227
                        s = splnet();
228
 
229
                        if (ia) {
230
                                for ( ; ia->ia_next; ia = ia->ia_next)
231
                                        continue;
232
                                ia->ia_next = oia;
233
                        } else
234
                                in_ifaddr = oia;
235
                        ia = oia;
236
                        ifa = ifp->if_addrlist;
237
                        if (ifa) {
238
                                for ( ; ifa->ifa_next; ifa = ifa->ifa_next)
239
                                        continue;
240
                                ifa->ifa_next = (struct ifaddr *) ia;
241
                        } else
242
                                ifp->if_addrlist = (struct ifaddr *) ia;
243
                        ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
244
                        ia->ia_ifa.ifa_dstaddr
245
                                        = (struct sockaddr *)&ia->ia_dstaddr;
246
                        ia->ia_ifa.ifa_netmask
247
                                        = (struct sockaddr *)&ia->ia_sockmask;
248
                        ia->ia_sockmask.sin_len = 8;
249
                        if (ifp->if_flags & IFF_BROADCAST) {
250
                                ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
251
                                ia->ia_broadaddr.sin_family = AF_INET;
252
                        }
253
                        ia->ia_ifp = ifp;
254
                        if (!(ifp->if_flags & IFF_LOOPBACK))
255
                                in_interfaces++;
256
                        splx(s);
257
                }
258
                break;
259
 
260
        case SIOCSIFBRDADDR:
261
                if ((so->so_state & SS_PRIV) == 0)
262
                        return (EPERM);
263
                /* FALLTHROUGH */
264
 
265
        case SIOCGIFADDR:
266
        case SIOCGIFNETMASK:
267
        case SIOCGIFDSTADDR:
268
        case SIOCGIFBRDADDR:
269
                if (ia == (struct in_ifaddr *)0)
270
                        return (EADDRNOTAVAIL);
271
                break;
272
        }
273
        switch (cmd) {
274
 
275
        case SIOCGIFADDR:
276
                *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
277
                break;
278
 
279
        case SIOCGIFBRDADDR:
280
                if ((ifp->if_flags & IFF_BROADCAST) == 0)
281
                        return (EINVAL);
282
                *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
283
                break;
284
 
285
        case SIOCGIFDSTADDR:
286
                if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
287
                        return (EINVAL);
288
                *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
289
                break;
290
 
291
        case SIOCGIFNETMASK:
292
                *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
293
                break;
294
 
295
        case SIOCSIFDSTADDR:
296
                if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
297
                        return (EINVAL);
298
                oldaddr = ia->ia_dstaddr;
299
                ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
300
                if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
301
                                        (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
302
                        ia->ia_dstaddr = oldaddr;
303
                        return (error);
304
                }
305
                if (ia->ia_flags & IFA_ROUTE) {
306
                        ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
307
                        rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
308
                        ia->ia_ifa.ifa_dstaddr =
309
                                        (struct sockaddr *)&ia->ia_dstaddr;
310
                        rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
311
                }
312
                break;
313
 
314
        case SIOCSIFBRDADDR:
315
                if ((ifp->if_flags & IFF_BROADCAST) == 0)
316
                        return (EINVAL);
317
                ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
318
                break;
319
 
320
        case SIOCSIFADDR:
321
                return (in_ifinit(ifp, ia,
322
                    (struct sockaddr_in *) &ifr->ifr_addr, 1));
323
 
324
        case SIOCSIFNETMASK:
325
                i = ifra->ifra_addr.sin_addr.s_addr;
326
                ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr = i);
327
                break;
328
 
329
        case SIOCAIFADDR:
330
                maskIsNew = 0;
331
                hostIsNew = 1;
332
                error = 0;
333
                if (ia->ia_addr.sin_family == AF_INET) {
334
                        if (ifra->ifra_addr.sin_len == 0) {
335
                                ifra->ifra_addr = ia->ia_addr;
336
                                hostIsNew = 0;
337
                        } else if (ifra->ifra_addr.sin_addr.s_addr ==
338
                                               ia->ia_addr.sin_addr.s_addr)
339
                                hostIsNew = 0;
340
                }
341
                if (ifra->ifra_mask.sin_len) {
342
                        in_ifscrub(ifp, ia);
343
                        ia->ia_sockmask = ifra->ifra_mask;
344
                        ia->ia_subnetmask =
345
                             ntohl(ia->ia_sockmask.sin_addr.s_addr);
346
                        maskIsNew = 1;
347
                }
348
                if ((ifp->if_flags & IFF_POINTOPOINT) &&
349
                    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
350
                        in_ifscrub(ifp, ia);
351
                        ia->ia_dstaddr = ifra->ifra_dstaddr;
352
                        maskIsNew  = 1; /* We lie; but the effect's the same */
353
                }
354
                if (ifra->ifra_addr.sin_family == AF_INET &&
355
                    (hostIsNew || maskIsNew))
356
                        error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
357
                if ((ifp->if_flags & IFF_BROADCAST) &&
358
                    (ifra->ifra_broadaddr.sin_family == AF_INET))
359
                        ia->ia_broadaddr = ifra->ifra_broadaddr;
360
                return (error);
361
 
362
        case SIOCDIFADDR:
363
                mk = malloc(sizeof *mk, M_IPMADDR, M_WAITOK);
364
                if (!mk)
365
                        return ENOBUFS;
366
 
367
                in_ifscrub(ifp, ia);
368
                /*
369
                 * Protect from ipintr() traversing address list
370
                 * while we're modifying it.
371
                 */
372
                s = splnet();
373
 
374
                if ((ifa = ifp->if_addrlist) == (struct ifaddr *)ia)
375
                        ifp->if_addrlist = ifa->ifa_next;
376
                else {
377
                        while (ifa->ifa_next &&
378
                               (ifa->ifa_next != (struct ifaddr *)ia))
379
                                    ifa = ifa->ifa_next;
380
                        if (ifa->ifa_next)
381
                                ifa->ifa_next = ((struct ifaddr *)ia)->ifa_next;
382
                        else
383
                                printf("Couldn't unlink inifaddr from ifp\n");
384
                }
385
                oia = ia;
386
                if (oia == (ia = in_ifaddr))
387
                        in_ifaddr = ia->ia_next;
388
                else {
389
                        while (ia->ia_next && (ia->ia_next != oia))
390
                                ia = ia->ia_next;
391
                        if (ia->ia_next)
392
                                ia->ia_next = oia->ia_next;
393
                        else
394
                                printf("Didn't unlink inifadr from list\n");
395
                }
396
 
397
                if (!oia->ia_multiaddrs.lh_first) {
398
                        IFAFREE(&oia->ia_ifa);
399
                        FREE(mk, M_IPMADDR);
400
                        splx(s);
401
                        break;
402
                }
403
 
404
                /*
405
                 * Multicast address kludge:
406
                 * If there were any multicast addresses attached to this
407
                 * interface address, either move them to another address
408
                 * on this interface, or save them until such time as this
409
                 * interface is reconfigured for IP.
410
                 */
411
                IFP_TO_IA(oia->ia_ifp, ia);
412
                if (ia) {       /* there is another address */
413
                        struct in_multi *inm;
414
                        for(inm = oia->ia_multiaddrs.lh_first; inm;
415
                            inm = inm->inm_entry.le_next) {
416
                                IFAFREE(&inm->inm_ia->ia_ifa);
417
                                ia->ia_ifa.ifa_refcnt++;
418
                                inm->inm_ia = ia;
419
                                LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm,
420
                                                 inm_entry);
421
                        }
422
                        FREE(mk, M_IPMADDR);
423
                } else {        /* last address on this if deleted, save */
424
                        struct in_multi *inm;
425
 
426
                        LIST_INIT(&mk->mk_head);
427
                        mk->mk_ifp = ifp;
428
 
429
                        for(inm = oia->ia_multiaddrs.lh_first; inm;
430
                            inm = inm->inm_entry.le_next) {
431
                                LIST_INSERT_HEAD(&mk->mk_head, inm, inm_entry);
432
                        }
433
 
434
                        if (mk->mk_head.lh_first) {
435
                                LIST_INSERT_HEAD(&in_mk, mk, mk_entry);
436
                        } else {
437
                                FREE(mk, M_IPMADDR);
438
                        }
439
                }
440
 
441
                IFAFREE((&oia->ia_ifa));
442
                splx(s);
443
                break;
444
 
445
        default:
446
                if (ifp == 0 || ifp->if_ioctl == 0)
447
                        return (EOPNOTSUPP);
448
                return ((*ifp->if_ioctl)(ifp, cmd, data));
449
        }
450
        return (0);
451
}
452
 
453
/*
454
 * Delete any existing route for an interface.
455
 */
456
static void
457
in_ifscrub(ifp, ia)
458
        register struct ifnet *ifp;
459
        register struct in_ifaddr *ia;
460
{
461
 
462
        if ((ia->ia_flags & IFA_ROUTE) == 0)
463
                return;
464
        if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
465
                rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
466
        else
467
                rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
468
        ia->ia_flags &= ~IFA_ROUTE;
469
}
470
 
471
/*
472
 * Initialize an interface's internet address
473
 * and routing table entry.
474
 */
475
static int
476
in_ifinit(ifp, ia, sin, scrub)
477
        register struct ifnet *ifp;
478
        register struct in_ifaddr *ia;
479
        struct sockaddr_in *sin;
480
        int scrub;
481
{
482
        register u_long i = ntohl(sin->sin_addr.s_addr);
483
        struct sockaddr_in oldaddr;
484
        int s = splimp(), flags = RTF_UP, error;
485
        struct multi_kludge *mk;
486
 
487
        oldaddr = ia->ia_addr;
488
        ia->ia_addr = *sin;
489
        /*
490
         * Give the interface a chance to initialize
491
         * if this is its first address,
492
         * and to validate the address if necessary.
493
         */
494
        if (ifp->if_ioctl &&
495
            (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
496
                splx(s);
497
                ia->ia_addr = oldaddr;
498
                return (error);
499
        }
500
        splx(s);
501
        if (scrub) {
502
                ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
503
                in_ifscrub(ifp, ia);
504
                ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
505
        }
506
        if (IN_CLASSA(i))
507
                ia->ia_netmask = IN_CLASSA_NET;
508
        else if (IN_CLASSB(i))
509
                ia->ia_netmask = IN_CLASSB_NET;
510
        else
511
                ia->ia_netmask = IN_CLASSC_NET;
512
        /*
513
         * The subnet mask usually includes at least the standard network part,
514
         * but may may be smaller in the case of supernetting.
515
         * If it is set, we believe it.
516
         */
517
        if (ia->ia_subnetmask == 0) {
518
                ia->ia_subnetmask = ia->ia_netmask;
519
                ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
520
        } else
521
                ia->ia_netmask &= ia->ia_subnetmask;
522
        ia->ia_net = i & ia->ia_netmask;
523
        ia->ia_subnet = i & ia->ia_subnetmask;
524
        in_socktrim(&ia->ia_sockmask);
525
        /*
526
         * Add route for the network.
527
         */
528
        ia->ia_ifa.ifa_metric = ifp->if_metric;
529
        if (ifp->if_flags & IFF_BROADCAST) {
530
                ia->ia_broadaddr.sin_addr.s_addr =
531
                        htonl(ia->ia_subnet | ~ia->ia_subnetmask);
532
                ia->ia_netbroadcast.s_addr =
533
                        htonl(ia->ia_net | ~ ia->ia_netmask);
534
        } else if (ifp->if_flags & IFF_LOOPBACK) {
535
                ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
536
                flags |= RTF_HOST;
537
        } else if (ifp->if_flags & IFF_POINTOPOINT) {
538
                if (ia->ia_dstaddr.sin_family != AF_INET)
539
                        return (0);
540
                flags |= RTF_HOST;
541
        }
542
        if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
543
                ia->ia_flags |= IFA_ROUTE;
544
 
545
        LIST_INIT(&ia->ia_multiaddrs);
546
        /*
547
         * If the interface supports multicast, join the "all hosts"
548
         * multicast group on that interface.
549
         */
550
        if (ifp->if_flags & IFF_MULTICAST) {
551
                struct in_addr addr;
552
 
553
                /*
554
                 * Continuation of multicast address hack:
555
                 * If there was a multicast group list previously saved
556
                 * for this interface, then we re-attach it to the first
557
                 * address configured on the i/f.
558
                 */
559
                for(mk = in_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
560
                        if(mk->mk_ifp == ifp) {
561
                                struct in_multi *inm;
562
 
563
                                for(inm = mk->mk_head.lh_first; inm;
564
                                    inm = inm->inm_entry.le_next) {
565
                                        IFAFREE(&inm->inm_ia->ia_ifa);
566
                                        ia->ia_ifa.ifa_refcnt++;
567
                                        inm->inm_ia = ia;
568
                                        LIST_INSERT_HEAD(&ia->ia_multiaddrs,
569
                                                         inm, inm_entry);
570
                                }
571
                                LIST_REMOVE(mk, mk_entry);
572
                                free(mk, M_IPMADDR);
573
                                break;
574
                        }
575
                }
576
 
577
                addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
578
                in_addmulti(&addr, ifp);
579
        }
580
        return (error);
581
}
582
 
583
 
584
/*
585
 * Return 1 if the address might be a local broadcast address.
586
 */
587
int
588
in_broadcast(in, ifp)
589
        struct in_addr in;
590
        struct ifnet *ifp;
591
{
592
        register struct ifaddr *ifa;
593
        u_long t;
594
 
595
        if (in.s_addr == INADDR_BROADCAST ||
596
            in.s_addr == INADDR_ANY)
597
                return 1;
598
        if ((ifp->if_flags & IFF_BROADCAST) == 0)
599
                return 0;
600
        t = ntohl(in.s_addr);
601
        /*
602
         * Look through the list of addresses for a match
603
         * with a broadcast address.
604
         */
605
#define ia ((struct in_ifaddr *)ifa)
606
        for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
607
                if (ifa->ifa_addr->sa_family == AF_INET &&
608
                    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
609
                     in.s_addr == ia->ia_netbroadcast.s_addr ||
610
                     /*
611
                      * Check for old-style (host 0) broadcast.
612
                      */
613
                     t == ia->ia_subnet || t == ia->ia_net) &&
614
                     /*
615
                      * Check for an all one subnetmask. These
616
                      * only exist when an interface gets a secondary
617
                      * address.
618
                      */
619
                     ia->ia_subnetmask != (u_long)0xffffffff)
620
                            return 1;
621
        return (0);
622
#undef ia
623
}
624
/*
625
 * Add an address to the list of IP multicast addresses for a given interface.
626
 */
627
struct in_multi *
628
in_addmulti(ap, ifp)
629
        register struct in_addr *ap;
630
        register struct ifnet *ifp;
631
{
632
        register struct in_multi *inm;
633
        struct ifreq ifr;
634
        struct in_ifaddr *ia;
635
        int s = splnet();
636
 
637
        /*
638
         * See if address already in list.
639
         */
640
        IN_LOOKUP_MULTI(*ap, ifp, inm);
641
        if (inm != NULL) {
642
                /*
643
                 * Found it; just increment the reference count.
644
                 */
645
                ++inm->inm_refcount;
646
        }
647
        else {
648
                /*
649
                 * New address; allocate a new multicast record
650
                 * and link it into the interface's multicast list.
651
                 */
652
                inm = (struct in_multi *)malloc(sizeof(*inm),
653
                    M_IPMADDR, M_NOWAIT);
654
                if (inm == NULL) {
655
                        splx(s);
656
                        return (NULL);
657
                }
658
                inm->inm_addr = *ap;
659
                inm->inm_ifp = ifp;
660
                inm->inm_refcount = 1;
661
                IFP_TO_IA(ifp, ia);
662
                if (ia == NULL) {
663
                        free(inm, M_IPMADDR);
664
                        splx(s);
665
                        return (NULL);
666
                }
667
                inm->inm_ia = ia;
668
                ia->ia_ifa.ifa_refcnt++; /* gain a reference */
669
                LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_entry);
670
 
671
                /*
672
                 * Ask the network driver to update its multicast reception
673
                 * filter appropriately for the new address.
674
                 */
675
                ((struct sockaddr_in *)&ifr.ifr_addr)->sin_family = AF_INET;
676
                ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr = *ap;
677
                if ((ifp->if_ioctl == NULL) ||
678
                    (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
679
                        LIST_REMOVE(inm, inm_entry);
680
                        IFAFREE(&ia->ia_ifa); /* release reference */
681
                        free(inm, M_IPMADDR);
682
                        splx(s);
683
                        return (NULL);
684
                }
685
                /*
686
                 * Let IGMP know that we have joined a new IP multicast group.
687
                 */
688
                igmp_joingroup(inm);
689
        }
690
        splx(s);
691
        return (inm);
692
}
693
 
694
/*
695
 * Delete a multicast address record.
696
 */
697
void
698
in_delmulti(inm)
699
        register struct in_multi *inm;
700
{
701
        struct ifreq ifr;
702
        int s = splnet();
703
 
704
        if (--inm->inm_refcount == 0) {
705
                /*
706
                 * No remaining claims to this record; let IGMP know that
707
                 * we are leaving the multicast group.
708
                 */
709
                igmp_leavegroup(inm);
710
                /*
711
                 * Unlink from list.
712
                 */
713
                LIST_REMOVE(inm, inm_entry);
714
                IFAFREE(&inm->inm_ia->ia_ifa); /* release reference */
715
 
716
                /*
717
                 * Notify the network driver to update its multicast reception
718
                 * filter.
719
                 */
720
                ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET;
721
                ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr =
722
                                                                inm->inm_addr;
723
                (*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
724
                                                             (caddr_t)&ifr);
725
                free(inm, M_IPMADDR);
726
        }
727
        splx(s);
728
}

powered by: WebSVN 2.1.0

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