source: rtems/cpukit/libnetworking/netinet/in.c @ e5d6315e

4.104.114.84.95
Last change on this file since e5d6315e was e5d6315e, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/28/07 at 05:23:18

Eliminate P().

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