]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/netinet/ip_output.c
xnu-517.9.5.tar.gz
[apple/xnu.git] / bsd / netinet / ip_output.c
index eaff4fc2d28b2645902a94ff83a76cd6c46c6de7..377bf2b08fe86f42974855c4e421f5fe78c3081f 100644 (file)
@@ -84,6 +84,7 @@
 #define DBG_LAYER_BEG          NETDBG_CODE(DBG_NETIP, 1)
 #define DBG_LAYER_END          NETDBG_CODE(DBG_NETIP, 3)
 #define DBG_FNC_IP_OUTPUT      NETDBG_CODE(DBG_NETIP, (1 << 8) | 1)
+#define DBG_FNC_IPSEC4_OUTPUT  NETDBG_CODE(DBG_NETIP, (2 << 8) | 1)
 
 
 #if vax
@@ -131,6 +132,9 @@ static int  ip_pcbopts __P((int, struct mbuf **, struct mbuf *));
 static int     ip_setmoptions
        __P((struct sockopt *, struct ip_moptions **));
 
+int ip_createmoptions(struct ip_moptions **imop);
+int ip_addmembership(struct ip_moptions *imo, struct ip_mreq *mreq);
+int ip_dropmembership(struct ip_moptions *imo, struct ip_mreq *mreq);
 int    ip_optcopy __P((struct ip *, struct ip *));
 extern int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **));
 #ifdef __APPLE__
@@ -141,6 +145,7 @@ static u_long  lo_dl_tag = 0;
 
 void in_delayed_cksum(struct mbuf *m);
 extern int apple_hwcksum_tx;
+extern u_long  route_generation;
 
 extern struct protosw inetsw[];
 
@@ -166,12 +171,11 @@ ip_output(m0, opt, ro, flags, imo)
        struct ip_moptions *imo;
 {
        struct ip *ip, *mhip;
-       struct ifnet *ifp;
-       u_long       dl_tag;
+       struct ifnet *ifp = NULL;
        struct mbuf *m = m0;
        int hlen = sizeof (struct ip);
        int len, off, error = 0;
-       struct sockaddr_in *dst;
+       struct sockaddr_in *dst = NULL;
        struct in_ifaddr *ia = NULL;
        int isbroadcast, sw_csum;
 #if IPSEC
@@ -213,10 +217,10 @@ ip_output(m0, opt, ro, flags, imo)
                        imo = NULL ;
                        dst = ((struct dn_pkt *)m)->dn_dst ;
                        ifp = ((struct dn_pkt *)m)->ifp ;
-                       flags = ((struct dn_pkt *)m)->flags ;
+                       flags = ((struct dn_pkt *)m)->flags;
                        m0 = m = m->m_next ;
 #if IPSEC
-           if (ipsec_bypass == 0) {
+           if (ipsec_bypass == 0 && (flags & IP_NOIPSEC) == 0) {
                so = ipsec_getsocket(m);
                (void)ipsec_setsocket(m, NULL);
            }
@@ -230,7 +234,7 @@ ip_output(m0, opt, ro, flags, imo)
             rule = NULL ;
 #endif
 #if IPSEC
-       if (ipsec_bypass == 0) {
+       if (ipsec_bypass == 0 && (flags & IP_NOIPSEC) == 0) {
                so = ipsec_getsocket(m);
                (void)ipsec_setsocket(m, NULL);
        }
@@ -268,17 +272,29 @@ ip_output(m0, opt, ro, flags, imo)
                     ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len);
 
        dst = (struct sockaddr_in *)&ro->ro_dst;
+
        /*
         * If there is a cached route,
         * check that it is to the same destination
         * and is still up.  If not, free it and try again.
+        * The address family should also be checked in case of sharing the
+        * cache with IPv6.
         */
+
+       if (ro->ro_rt && (ro->ro_rt->generation_id != route_generation) &&
+               ((flags & (IP_ROUTETOIF | IP_FORWARDING)) == 0) && (ip->ip_src.s_addr != INADDR_ANY) &&
+               (ifa_foraddr(ip->ip_src.s_addr) == NULL)) {
+               error = EADDRNOTAVAIL;
+               goto bad;
+       }
        if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
+          dst->sin_family != AF_INET ||
           dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
                rtfree(ro->ro_rt);
                ro->ro_rt = (struct rtentry *)0;
        }
        if (ro->ro_rt == 0) {
+               bzero(dst, sizeof(*dst));
                dst->sin_family = AF_INET;
                dst->sin_len = sizeof(*dst);
                dst->sin_addr = ip->ip_dst;
@@ -297,7 +313,6 @@ ip_output(m0, opt, ro, flags, imo)
                        goto bad;
                }
                ifp = ia->ia_ifp;
-               dl_tag = ia->ia_ifa.ifa_dlt;
                ip->ip_ttl = 1;
                isbroadcast = in_broadcast(dst->sin_addr, ifp);
        } else {
@@ -319,7 +334,6 @@ ip_output(m0, opt, ro, flags, imo)
                }
                ia = ifatoia(ro->ro_rt->rt_ifa);
                ifp = ro->ro_rt->rt_ifp;
-               dl_tag = ro->ro_rt->rt_dlt;
                ro->ro_rt->rt_use++;
                if (ro->ro_rt->rt_flags & RTF_GATEWAY)
                        dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
@@ -342,16 +356,16 @@ ip_output(m0, opt, ro, flags, imo)
                 * See if the caller provided any multicast options
                 */
                if (imo != NULL) {
-                       ip->ip_ttl = imo->imo_multicast_ttl;
+                       if ((flags & IP_RAWOUTPUT) == 0) ip->ip_ttl = imo->imo_multicast_ttl;
                        if (imo->imo_multicast_ifp != NULL) {
                                ifp = imo->imo_multicast_ifp;
-                               dl_tag = ifp->if_data.default_proto;
                        }
-                       if (imo->imo_multicast_vif != -1)
+                       if (imo->imo_multicast_vif != -1 && 
+                               ((flags & IP_RAWOUTPUT) == 0 || ip->ip_src.s_addr == INADDR_ANY))
                                ip->ip_src.s_addr =
-                                   ip_mcast_src(imo->imo_multicast_vif);
+                                       ip_mcast_src(imo->imo_multicast_vif);
                } else
-                       ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
+                       if ((flags & IP_RAWOUTPUT) == 0) ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
                /*
                 * Confirm that the outgoing interface supports multicast.
                 */
@@ -372,8 +386,13 @@ ip_output(m0, opt, ro, flags, imo)
                        TAILQ_FOREACH(ia1, &in_ifaddrhead, ia_link)
                                if (ia1->ia_ifp == ifp) {
                                        ip->ip_src = IA_SIN(ia1)->sin_addr;
+                                       
                                        break;
                                }
+                       if (ip->ip_src.s_addr == INADDR_ANY) {
+                               error = ENETUNREACH;
+                               goto bad;
+                       }
                }
 
                IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
@@ -496,9 +515,11 @@ sendit:
 #if IPSEC
        /* temporary for testing only: bypass ipsec alltogether */
 
-       if (ipsec_bypass != 0)
+       if (ipsec_bypass != 0 || (flags & IP_NOIPSEC) != 0)
                goto skip_ipsec;
 
+       KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_START, 0,0,0,0,0);
+
        /* get SP for this packet */
        if (so == NULL)
                sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error);
@@ -507,6 +528,7 @@ sendit:
 
        if (sp == NULL) {
                ipsecstat.out_inval++;
+               KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END, 0,0,0,0,0);
                goto bad;
        }
 
@@ -519,17 +541,20 @@ sendit:
                 * This packet is just discarded.
                 */
                ipsecstat.out_polvio++;
+               KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END, 1,0,0,0,0);
                goto bad;
 
        case IPSEC_POLICY_BYPASS:
        case IPSEC_POLICY_NONE:
                /* no need to do IPsec. */
+               KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END, 2,0,0,0,0);
                goto skip_ipsec;
        
        case IPSEC_POLICY_IPSEC:
                if (sp->req == NULL) {
                        /* acquire a policy */
                        error = key_spdacquire(sp);
+                       KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END, 3,0,0,0,0);
                        goto bad;
                }
                break;
@@ -565,7 +590,8 @@ sendit:
 
        error = ipsec4_output(&state, sp, flags);
 
-       m = state.m;
+       m0 = m = state.m;
+       
        if (flags & IP_ROUTETOIF) {
                /*
                 * if we have tunnel mode SA, we may need to ignore
@@ -577,6 +603,7 @@ sendit:
                }
        } else
                ro = state.ro;
+
        dst = (struct sockaddr_in *)state.dst;
        if (error) {
                /* mbuf is already reclaimed in ipsec4_output. */
@@ -596,33 +623,48 @@ sendit:
                        error = 0;
                        break;
                }
+               KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END, 4,0,0,0,0);
                goto bad;
        }
     }
 
        /* be sure to update variables that are affected by ipsec4_output() */
        ip = mtod(m, struct ip *);
+       
 #ifdef _IP_VHL
        hlen = IP_VHL_HL(ip->ip_vhl) << 2;
 #else
        hlen = ip->ip_hl << 2;
 #endif
+       /* Check that there wasn't a route change and src is still valid */
+
+       if (ro->ro_rt->generation_id != route_generation) {
+               if (ifa_foraddr(ip->ip_src.s_addr) == NULL && ((flags & (IP_ROUTETOIF | IP_FORWARDING)) == 0)) {
+                       error = EADDRNOTAVAIL;
+                       KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END, 5,0,0,0,0);
+                       goto bad;
+               }
+               rtfree(ro->ro_rt);
+               ro->ro_rt = NULL;
+       }
+
        if (ro->ro_rt == NULL) {
                if ((flags & IP_ROUTETOIF) == 0) {
                        printf("ip_output: "
                                "can't update route after IPsec processing\n");
-                       error = EHOSTUNREACH;   /*XXX*/
+                       error = EHOSTUNREACH;   /*XXX*/ 
+                       KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END, 6,0,0,0,0);
                        goto bad;
                }
        } else {
                ia = ifatoia(ro->ro_rt->rt_ifa);
                ifp = ro->ro_rt->rt_ifp;
-               dl_tag = ia->ia_ifa.ifa_dlt;
        }
 
        /* make it flipped, again. */
        NTOHS(ip->ip_len);
        NTOHS(ip->ip_off);
+       KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END, 7,0xff,0xff,0xff,0xff);
 skip_ipsec:
 #endif /*IPSEC*/
 
@@ -638,7 +680,7 @@ skip_ipsec:
 
                if ((error = (*fr_checkp)(ip, hlen, ifp, 1, &m1)) || !m1)
                        goto done;
-               ip = mtod(m = m1, struct ip *);
+               ip = mtod(m0 = m = m1, struct ip *);
        }
 
        /*
@@ -663,6 +705,7 @@ skip_ipsec:
                  * unsupported rules), but better play safe and drop
                  * packets in case of doubt.
                  */
+               m0 = m;
                if ( (off & IP_FW_PORT_DENY_FLAG) || m == NULL) {
                        if (m)
                                m_freem(m);
@@ -715,7 +758,7 @@ skip_ipsec:
 
                        /* If 'tee', continue with original packet */
                        if (clone != NULL) {
-                               m = clone;
+                               m0 = m = clone;
                                ip = mtod(m, struct ip *);
                                goto pass;
                        }
@@ -775,7 +818,7 @@ skip_ipsec:
                                if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
                                        m->m_pkthdr.csum_flags |=
                                            CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
-                                       m0->m_pkthdr.csum_data = 0xffff;
+                                       m->m_pkthdr.csum_data = 0xffff;
                                }
                                m->m_pkthdr.csum_flags |=
                                    CSUM_IP_CHECKED | CSUM_IP_VALID;
@@ -803,7 +846,6 @@ skip_ipsec:
 
                        ia = ifatoia(ro_fwd->ro_rt->rt_ifa);
                        ifp = ro_fwd->ro_rt->rt_ifp;
-                       dl_tag = ro_fwd->ro_rt->rt_dlt;
                        ro_fwd->ro_rt->rt_use++;
                        if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
                                dst = (struct sockaddr_in *)ro_fwd->ro_rt->rt_gateway;
@@ -836,8 +878,26 @@ skip_ipsec:
        }
 
 pass:
+#if __APPLE__
+       /* Do not allow loopback address to wind up on a wire */
+       if ((ifp->if_flags & IFF_LOOPBACK) == 0 &&
+                ((ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
+                 (ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)) {
+               ipstat.ips_badaddr++;
+               m_freem(m);
+               /*
+                * Simply drop the packet just like a firewall -- we do not want the
+                * the application to feel the pain, not yet...
+                * Returning ENETUNREACH like ip6_output does in some similar cases  
+                * could startle the otherwise clueless process that specifies
+                * loopback as the source address.
+                */
+               goto done;
+       }
+#endif
        m->m_pkthdr.csum_flags |= CSUM_IP;
-       sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
+       sw_csum = m->m_pkthdr.csum_flags 
+               & ~IF_HWASSIST_CSUM_FLAGS(ifp->if_hwassist);
 
        if ((ifp->if_hwassist & CSUM_TCP_SUM16) != 0) {
                /*
@@ -867,7 +927,7 @@ pass:
                m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
        }
        
-       m->m_pkthdr.csum_flags &= ifp->if_hwassist;
+       m->m_pkthdr.csum_flags &= IF_HWASSIST_CSUM_FLAGS(ifp->if_hwassist);
 
        /*
         * If small enough for interface, or the interface will take
@@ -892,11 +952,11 @@ pass:
 
 #if IPSEC
                /* clean ipsec history once it goes out of the node */
-               if (ipsec_bypass == 0)
+               if (ipsec_bypass == 0 && (flags & IP_NOIPSEC) == 0)
                        ipsec_delaux(m);
 #endif
 #if __APPLE__
-               error = dlil_output(dl_tag, m, (void *) ro->ro_rt,
+               error = dlil_output(ifptodlt(ifp, PF_INET), m, (void *) ro->ro_rt,
                                    (struct sockaddr *)dst, 0);
 #else
                error = (*ifp->if_output)(ifp, m,
@@ -1029,7 +1089,7 @@ sendorfree:
                m->m_nextpkt = 0;
 #if IPSEC
                /* clean ipsec history once it goes out of the node */
-               if (ipsec_bypass == 0)
+               if (ipsec_bypass == 0 && (flags & IP_NOIPSEC) == 0)
                        ipsec_delaux(m);
 #endif
                if (error == 0) {
@@ -1042,7 +1102,7 @@ sendorfree:
 #endif
                        
 #if __APPLE__
-                   error = dlil_output(dl_tag, m, (void *) ro->ro_rt,
+                   error = dlil_output(ifptodlt(ifp, PF_INET), m, (void *) ro->ro_rt,
                                        (struct sockaddr *)dst, 0);
 #else
                        error = (*ifp->if_output)(ifp, m,
@@ -1057,7 +1117,7 @@ sendorfree:
     }
 done:
 #if IPSEC
-       if (ipsec_bypass == 0) {
+       if (ipsec_bypass == 0 && (flags & IP_NOIPSEC) == 0) {
        if (ro == &iproute && ro->ro_rt) {
                rtfree(ro->ro_rt);
                ro->ro_rt = NULL;
@@ -1252,6 +1312,7 @@ ip_ctloutput(so, sopt)
                case IP_RECVRETOPTS:
                case IP_RECVDSTADDR:
                case IP_RECVIF:
+               case IP_RECVTTL:
 #if defined(NFAITH) && NFAITH > 0
                case IP_FAITH:
 #endif
@@ -1290,6 +1351,10 @@ ip_ctloutput(so, sopt)
                                OPTSET(INP_RECVIF);
                                break;
 
+                       case IP_RECVTTL:
+                               OPTSET(INP_RECVTTL);
+                               break;
+
 #if defined(NFAITH) && NFAITH > 0
                        case IP_FAITH:
                                OPTSET(INP_FAITH);
@@ -1388,6 +1453,7 @@ ip_ctloutput(so, sopt)
                case IP_RECVRETOPTS:
                case IP_RECVDSTADDR:
                case IP_RECVIF:
+               case IP_RECVTTL:
                case IP_PORTRANGE:
 #if defined(NFAITH) && NFAITH > 0
                case IP_FAITH:
@@ -1420,6 +1486,10 @@ ip_ctloutput(so, sopt)
                                optval = OPTBIT(INP_RECVIF);
                                break;
 
+                       case IP_RECVTTL:
+                               optval = OPTBIT(INP_RECVTTL);
+                               break;
+
                        case IP_PORTRANGE:
                                if (inp->inp_flags & INP_HIGHPORT)
                                        optval = IP_PORTRANGE_HIGH;
@@ -1629,8 +1699,6 @@ ip_setmoptions(sopt, imop)
        struct ip_mreq mreq;
        struct ifnet *ifp = NULL;
        struct ip_moptions *imo = *imop;
-       struct route ro;
-       struct sockaddr_in *dst;
        int ifindex;
        int s;
 
@@ -1639,18 +1707,10 @@ ip_setmoptions(sopt, imop)
                 * No multicast option buffer attached to the pcb;
                 * allocate one and initialize to default values.
                 */
-               imo = (struct ip_moptions*) _MALLOC(sizeof(*imo), M_IPMOPTS,
-                   M_WAITOK);
-
-               if (imo == NULL)
-                       return (ENOBUFS);
-               *imop = imo;
-               imo->imo_multicast_ifp = NULL;
-               imo->imo_multicast_addr.s_addr = INADDR_ANY;
-               imo->imo_multicast_vif = -1;
-               imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
-               imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
-               imo->imo_num_memberships = 0;
+               error = ip_createmoptions(imop);
+               if (error != 0)
+                       return error;
+               imo = *imop;
        }
 
        switch (sopt->sopt_name) {
@@ -1763,78 +1823,8 @@ ip_setmoptions(sopt, imop)
                error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
                if (error)
                        break;
-
-               if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
-                       error = EINVAL;
-                       break;
-               }
-               s = splimp();
-               /*
-                * If no interface address was provided, use the interface of
-                * the route to the given multicast address.
-                */
-               if (mreq.imr_interface.s_addr == INADDR_ANY) {
-                       bzero((caddr_t)&ro, sizeof(ro));
-                       dst = (struct sockaddr_in *)&ro.ro_dst;
-                       dst->sin_len = sizeof(*dst);
-                       dst->sin_family = AF_INET;
-                       dst->sin_addr = mreq.imr_multiaddr;
-                       rtalloc(&ro);
-                       if (ro.ro_rt != NULL) {
-                               ifp = ro.ro_rt->rt_ifp;
-                               rtfree(ro.ro_rt);
-                       }
-                       else {
-                               /* If there's no default route, try using loopback */
-                               mreq.imr_interface.s_addr = INADDR_LOOPBACK;
-                       }
-               }
                
-               if (ifp == NULL) {
-                       ifp = ip_multicast_if(&mreq.imr_interface, NULL);
-               }
-
-               /*
-                * See if we found an interface, and confirm that it
-                * supports multicast.
-                */
-               if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
-                       error = EADDRNOTAVAIL;
-                       splx(s);
-                       break;
-               }
-               /*
-                * See if the membership already exists or if all the
-                * membership slots are full.
-                */
-               for (i = 0; i < imo->imo_num_memberships; ++i) {
-                       if (imo->imo_membership[i]->inm_ifp == ifp &&
-                           imo->imo_membership[i]->inm_addr.s_addr
-                                               == mreq.imr_multiaddr.s_addr)
-                               break;
-               }
-               if (i < imo->imo_num_memberships) {
-                       error = EADDRINUSE;
-                       splx(s);
-                       break;
-               }
-               if (i == IP_MAX_MEMBERSHIPS) {
-                       error = ETOOMANYREFS;
-                       splx(s);
-                       break;
-               }
-               /*
-                * Everything looks good; add a new record to the multicast
-                * address list for the given interface.
-                */
-               if ((imo->imo_membership[i] =
-                   in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
-                       error = ENOBUFS;
-                       splx(s);
-                       break;
-               }
-               ++imo->imo_num_memberships;
-               splx(s);
+               error = ip_addmembership(imo, &mreq);
                break;
 
        case IP_DROP_MEMBERSHIP:
@@ -1845,54 +1835,8 @@ ip_setmoptions(sopt, imop)
                error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
                if (error)
                        break;
-
-               if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
-                       error = EINVAL;
-                       break;
-               }
-
-               s = splimp();
-               /*
-                * If an interface address was specified, get a pointer
-                * to its ifnet structure.
-                */
-               if (mreq.imr_interface.s_addr == INADDR_ANY)
-                       ifp = NULL;
-               else {
-                       ifp = ip_multicast_if(&mreq.imr_interface, NULL);
-                       if (ifp == NULL) {
-                               error = EADDRNOTAVAIL;
-                               splx(s);
-                               break;
-                       }
-               }
-               /*
-                * Find the membership in the membership array.
-                */
-               for (i = 0; i < imo->imo_num_memberships; ++i) {
-                       if ((ifp == NULL ||
-                            imo->imo_membership[i]->inm_ifp == ifp) &&
-                            imo->imo_membership[i]->inm_addr.s_addr ==
-                            mreq.imr_multiaddr.s_addr)
-                               break;
-               }
-               if (i == imo->imo_num_memberships) {
-                       error = EADDRNOTAVAIL;
-                       splx(s);
-                       break;
-               }
-               /*
-                * Give up the multicast address record to which the
-                * membership points.
-                */
-               in_delmulti(imo->imo_membership[i]);
-               /*
-                * Remove the gap in the membership array.
-                */
-               for (++i; i < imo->imo_num_memberships; ++i)
-                       imo->imo_membership[i-1] = imo->imo_membership[i];
-               --imo->imo_num_memberships;
-               splx(s);
+               
+               error = ip_dropmembership(imo, &mreq);
                break;
 
        default:
@@ -1915,6 +1859,184 @@ ip_setmoptions(sopt, imop)
        return (error);
 }
 
+/*
+ * Set the IP multicast options in response to user setsockopt().
+ */
+__private_extern__ int
+ip_createmoptions(
+       struct ip_moptions **imop)
+{
+       struct ip_moptions *imo;
+       imo = (struct ip_moptions*) _MALLOC(sizeof(*imo), M_IPMOPTS,
+               M_WAITOK);
+
+       if (imo == NULL)
+               return (ENOBUFS);
+       *imop = imo;
+       imo->imo_multicast_ifp = NULL;
+       imo->imo_multicast_addr.s_addr = INADDR_ANY;
+       imo->imo_multicast_vif = -1;
+       imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
+       imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
+       imo->imo_num_memberships = 0;
+       
+       return 0;
+}
+
+/*
+ * Add membership to an IPv4 multicast.
+ */
+__private_extern__ int
+ip_addmembership(
+       struct ip_moptions *imo,
+       struct ip_mreq *mreq)
+{
+       struct route ro;
+       struct sockaddr_in *dst;
+       struct ifnet *ifp = NULL;
+       int error = 0;
+       int s = 0;
+       int i;
+       
+       if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
+               error = EINVAL;
+               return error;
+       }
+       s = splimp();
+       /*
+        * If no interface address was provided, use the interface of
+        * the route to the given multicast address.
+        */
+       if (mreq->imr_interface.s_addr == INADDR_ANY) {
+               bzero((caddr_t)&ro, sizeof(ro));
+               dst = (struct sockaddr_in *)&ro.ro_dst;
+               dst->sin_len = sizeof(*dst);
+               dst->sin_family = AF_INET;
+               dst->sin_addr = mreq->imr_multiaddr;
+               rtalloc(&ro);
+               if (ro.ro_rt != NULL) {
+                       ifp = ro.ro_rt->rt_ifp;
+                       rtfree(ro.ro_rt);
+               }
+               else {
+                       /* If there's no default route, try using loopback */
+                       mreq->imr_interface.s_addr = INADDR_LOOPBACK;
+               }
+       }
+       
+       if (ifp == NULL) {
+               ifp = ip_multicast_if(&mreq->imr_interface, NULL);
+       }
+
+       /*
+        * See if we found an interface, and confirm that it
+        * supports multicast.
+        */
+       if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
+               error = EADDRNOTAVAIL;
+               splx(s);
+               return error;
+       }
+       /*
+        * See if the membership already exists or if all the
+        * membership slots are full.
+        */
+       for (i = 0; i < imo->imo_num_memberships; ++i) {
+               if (imo->imo_membership[i]->inm_ifp == ifp &&
+                       imo->imo_membership[i]->inm_addr.s_addr
+                                       == mreq->imr_multiaddr.s_addr)
+                       break;
+       }
+       if (i < imo->imo_num_memberships) {
+               error = EADDRINUSE;
+               splx(s);
+               return error;
+       }
+       if (i == IP_MAX_MEMBERSHIPS) {
+               error = ETOOMANYREFS;
+               splx(s);
+               return error;
+       }
+       /*
+        * Everything looks good; add a new record to the multicast
+        * address list for the given interface.
+        */
+       if ((imo->imo_membership[i] =
+               in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) {
+               error = ENOBUFS;
+               splx(s);
+               return error;
+       }
+       ++imo->imo_num_memberships;
+       splx(s);
+       
+       return error;
+}
+
+/*
+ * Drop membership of an IPv4 multicast.
+ */
+__private_extern__ int
+ip_dropmembership(
+       struct ip_moptions *imo,
+       struct ip_mreq *mreq)
+{
+       int error = 0;
+       int s = 0;
+       struct ifnet* ifp = NULL;
+       int i;
+       
+       if (!IN_MULTICAST(ntohl(mreq->imr_multiaddr.s_addr))) {
+               error = EINVAL;
+               return error;
+       }
+
+       s = splimp();
+       /*
+        * If an interface address was specified, get a pointer
+        * to its ifnet structure.
+        */
+       if (mreq->imr_interface.s_addr == INADDR_ANY)
+               ifp = NULL;
+       else {
+               ifp = ip_multicast_if(&mreq->imr_interface, NULL);
+               if (ifp == NULL) {
+                       error = EADDRNOTAVAIL;
+                       splx(s);
+                       return error;
+               }
+       }
+       /*
+        * Find the membership in the membership array.
+        */
+       for (i = 0; i < imo->imo_num_memberships; ++i) {
+               if ((ifp == NULL ||
+                        imo->imo_membership[i]->inm_ifp == ifp) &&
+                        imo->imo_membership[i]->inm_addr.s_addr ==
+                        mreq->imr_multiaddr.s_addr)
+                       break;
+       }
+       if (i == imo->imo_num_memberships) {
+               error = EADDRNOTAVAIL;
+               splx(s);
+               return error;
+       }
+       /*
+        * Give up the multicast address record to which the
+        * membership points.
+        */
+       in_delmulti(imo->imo_membership[i]);
+       /*
+        * Remove the gap in the membership array.
+        */
+       for (++i; i < imo->imo_num_memberships; ++i)
+               imo->imo_membership[i-1] = imo->imo_membership[i];
+       --imo->imo_num_memberships;
+       splx(s);
+       
+       return error;
+}
+
 /*
  * Return the IP multicast options in response to user getsockopt().
  */
@@ -2058,7 +2180,7 @@ ip_mloopback(ifp, m, dst, hlen)
         * The UDP checksum has not been calculated yet.
         */
         if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
-            if (ifp->if_hwassist) {
+            if (IF_HWASSIST_CSUM_FLAGS(ifp->if_hwassist)) {
                 copym->m_pkthdr.csum_flags |=
                     CSUM_DATA_VALID | CSUM_PSEUDO_HDR |
                     CSUM_IP_CHECKED | CSUM_IP_VALID;