diff -Nur lwip/src/api/api_msg.c lwip-mcast/src/api/api_msg.c
--- lwip/src/api/api_msg.c	Fri Jan 26 13:32:36 2018
+++ lwip-mcast/src/api/api_msg.c	Sat Jan 27 15:34:01 2018
@@ -165,9 +165,35 @@
 
       buf->p = q;
       buf->ptr = q;
-      ip_addr_copy(buf->addr, *ip_current_src_addr());
+      ip_addr_copy(buf->addr, *addr);
       buf->port = pcb->protocol;
 
+#if LWIP_NETBUF_RECVINFO
+      if (conn->flags & NETCONN_FLAG_PKTINFO) {
+        buf->flags = NETBUF_FLAG_DESTADDR;
+        /* If we use mroute this packet maybe a mroute fake packet so we must get ipv4/ipv6 header */
+        if (IP_IS_V4(addr)) {
+          struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
+          ip_addr_copy_from_ip4(buf->toaddr, iphdr->dest);
+        } else 
+#if LWIP_IPV6
+        {
+          struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
+          ip_addr_copy_from_ip6_packed(buf->toaddr, ip6hdr->dest);
+        }
+#endif /* LWIP_IPV6 */
+      }
+#if LWIP_IPV6
+      if (conn->flags & NETCONN_FLAG_HOPLIM) { /* IPV6_HOPLIMIT */
+        if (IP_IS_V6(addr)) {
+          struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
+          buf->flags |= NETBUF_FLAG_HOPLIM; /* IPV6_RECVHOPLIMIT (Hop limit) */
+          buf->hoplim = IP6H_HOPLIM(ip6hdr);
+        }
+      }
+#endif /* LWIP_IPV6 */
+#endif /* LWIP_NETBUF_RECVINFO */
+
       len = q->tot_len;
       if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
         netbuf_delete(buf);
@@ -244,6 +270,15 @@
       ip_addr_set(&buf->toaddr, ip_current_dest_addr());
       buf->toport_chksum = udphdr->dest;
     }
+#if LWIP_IPV6
+    if (conn->flags & NETCONN_FLAG_HOPLIM) { /* IPV6_HOPLIMIT */
+      if (IP_IS_V6(addr)) {
+        struct ip6_hdr *ip6hdr = (struct ip6_hdr *)ip6_current_header();
+        buf->flags |= NETBUF_FLAG_HOPLIM; /* IPV6_RECVHOPLIMIT (Hop limit) */
+        buf->hoplim = IP6H_HOPLIM(ip6hdr);
+      }
+    }
+#endif /* LWIP_IPV6 */
 #endif /* LWIP_NETBUF_RECVINFO */
   }
 
@@ -1946,36 +1981,31 @@
 
   msg->err = ERR_CONN;
   if (msg->conn->pcb.tcp != NULL) {
+#if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
     if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
 #if LWIP_UDP
-#if LWIP_IPV6 && LWIP_IPV6_MLD
-      if (NETCONNTYPE_ISIPV6(msg->conn->type)) {
-        if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
-          msg->err = mld6_joingroup(ip_2_ip6(API_EXPR_REF(msg->msg.jl.netif_addr)),
-                                    ip_2_ip6(API_EXPR_REF(msg->msg.jl.multiaddr)));
-        } else {
-          msg->err = mld6_leavegroup(ip_2_ip6(API_EXPR_REF(msg->msg.jl.netif_addr)),
-                                     ip_2_ip6(API_EXPR_REF(msg->msg.jl.multiaddr)));
-        }
-      } else
-#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
-      {
-#if LWIP_IGMP
-        if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
-          msg->err = igmp_joingroup(ip_2_ip4(API_EXPR_REF(msg->msg.jl.netif_addr)),
-                                    ip_2_ip4(API_EXPR_REF(msg->msg.jl.multiaddr)));
-        } else {
-          msg->err = igmp_leavegroup(ip_2_ip4(API_EXPR_REF(msg->msg.jl.netif_addr)),
-                                     ip_2_ip4(API_EXPR_REF(msg->msg.jl.multiaddr)));
-        }
-#endif /* LWIP_IGMP */
+      if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
+        msg->err = mcast_join_group(&msg->conn->pcb.udp->ipmc, API_EXPR_REF(msg->msg.jl.netif_addr), API_EXPR_REF(msg->msg.jl.multiaddr), NULL);
+      } else {
+        msg->err = mcast_leave_group(&msg->conn->pcb.udp->ipmc, API_EXPR_REF(msg->msg.jl.netif_addr), API_EXPR_REF(msg->msg.jl.multiaddr), NULL);
       }
+#else
+      msg->err = ERR_VAL;
 #endif /* LWIP_UDP */
-#if (LWIP_TCP || LWIP_RAW)
+    } else if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_RAW) {
+#if LWIP_RAW
+      if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
+        msg->err = mcast_join_group(&msg->conn->pcb.raw->ipmc, API_EXPR_REF(msg->msg.jl.netif_addr), API_EXPR_REF(msg->msg.jl.multiaddr), NULL);
+      } else {
+        msg->err = mcast_leave_group(&msg->conn->pcb.raw->ipmc, API_EXPR_REF(msg->msg.jl.netif_addr), API_EXPR_REF(msg->msg.jl.multiaddr), NULL);
+      }
+#else
+      msg->err = ERR_VAL;
+#endif /* LWIP_RAW */
     } else {
       msg->err = ERR_VAL;
-#endif /* (LWIP_TCP || LWIP_RAW) */
     }
+#endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
   }
   TCPIP_APIMSG_ACK(msg);
 }
@@ -1999,36 +2029,31 @@
 
   msg->err = ERR_CONN;
   if (msg->conn->pcb.tcp != NULL) {
+#if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
     if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
 #if LWIP_UDP
-#if LWIP_IPV6 && LWIP_IPV6_MLD
-      if (NETCONNTYPE_ISIPV6(msg->conn->type)) {
-        if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
-          msg->err = mld6_joingroup_netif(netif,
-                                          ip_2_ip6(API_EXPR_REF(msg->msg.jl.multiaddr)));
-        } else {
-          msg->err = mld6_leavegroup_netif(netif,
-                                           ip_2_ip6(API_EXPR_REF(msg->msg.jl.multiaddr)));
-        }
-      } else
-#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
-      {
-#if LWIP_IGMP
-        if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
-          msg->err = igmp_joingroup_netif(netif,
-                                          ip_2_ip4(API_EXPR_REF(msg->msg.jl.multiaddr)));
-        } else {
-          msg->err = igmp_leavegroup_netif(netif,
-                                           ip_2_ip4(API_EXPR_REF(msg->msg.jl.multiaddr)));
-        }
-#endif /* LWIP_IGMP */
+      if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
+        msg->err = mcast_join_netif(&msg->conn->pcb.udp->ipmc, netif, API_EXPR_REF(msg->msg.jl.multiaddr), NULL);
+      } else {
+        msg->err = mcast_leave_netif(&msg->conn->pcb.udp->ipmc, netif, API_EXPR_REF(msg->msg.jl.multiaddr), NULL);
       }
+#else
+      msg->err = ERR_VAL;
 #endif /* LWIP_UDP */
-#if (LWIP_TCP || LWIP_RAW)
+    } else if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_RAW) {
+#if LWIP_RAW
+      if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
+        msg->err = mcast_join_netif(&msg->conn->pcb.raw->ipmc, netif, API_EXPR_REF(msg->msg.jl.multiaddr), NULL);
+      } else {
+        msg->err = mcast_leave_netif(&msg->conn->pcb.raw->ipmc, netif, API_EXPR_REF(msg->msg.jl.multiaddr), NULL);
+      }
+#else
+      msg->err = ERR_VAL;
+#endif /* LWIP_RAW */
     } else {
       msg->err = ERR_VAL;
-#endif /* (LWIP_TCP || LWIP_RAW) */
     }
+#endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
   }
 
 done:
diff -Nur lwip/src/api/sockets.c lwip-mcast/src/api/sockets.c
--- lwip/src/api/sockets.c	Fri Jan 26 13:32:36 2018
+++ lwip-mcast/src/api/sockets.c	Sat Jan 27 16:39:04 2018
@@ -209,49 +209,6 @@
 #endif /* LWIP_IPV4 */
 };
 
-/* Define the number of IPv4 multicast memberships, default is one per socket */
-#ifndef LWIP_SOCKET_MAX_MEMBERSHIPS
-#define LWIP_SOCKET_MAX_MEMBERSHIPS NUM_SOCKETS
-#endif
-
-#if LWIP_IGMP
-/* This is to keep track of IP_ADD_MEMBERSHIP calls to drop the membership when
-   a socket is closed */
-struct lwip_socket_multicast_pair {
-  /** the socket */
-  struct lwip_sock *sock;
-  /** the interface address */
-  ip4_addr_t if_addr;
-  /** the group address */
-  ip4_addr_t multi_addr;
-};
-
-struct lwip_socket_multicast_pair socket_ipv4_multicast_memberships[LWIP_SOCKET_MAX_MEMBERSHIPS];
-
-static int  lwip_socket_register_membership(int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr);
-static void lwip_socket_unregister_membership(int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr);
-static void lwip_socket_drop_registered_memberships(int s);
-#endif /* LWIP_IGMP */
-
-#if LWIP_IPV6_MLD
-/* This is to keep track of IP_JOIN_GROUP calls to drop the membership when
-   a socket is closed */
-struct lwip_socket_multicast_mld6_pair {
-  /** the socket */
-  struct lwip_sock *sock;
-  /** the interface index */
-  u8_t if_idx;
-  /** the group address */
-  ip6_addr_t multi_addr;
-};
-
-struct lwip_socket_multicast_mld6_pair socket_ipv6_multicast_memberships[LWIP_SOCKET_MAX_MEMBERSHIPS];
-
-static int  lwip_socket_register_mld6_membership(int s, unsigned int if_idx, const ip6_addr_t *multi_addr);
-static void lwip_socket_unregister_mld6_membership(int s, unsigned int if_idx, const ip6_addr_t *multi_addr);
-static void lwip_socket_drop_registered_mld6_memberships(int s);
-#endif /* LWIP_IPV6_MLD */
-
 /** The global array of available sockets */
 static struct lwip_sock sockets[NUM_SOCKETS];
 
@@ -1102,7 +1059,7 @@
   msg->msg_flags = 0;
 
   if (msg->msg_control) {
-    u8_t wrote_msg = 0;
+    socklen_t wrote_msg = 0;
 #if LWIP_NETBUF_RECVINFO
     /* Check if packet info was recorded */
     if (buf->flags & NETBUF_FLAG_DESTADDR) {
@@ -1116,19 +1073,49 @@
           chdr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
           pkti->ipi_ifindex = buf->p->if_idx;
           inet_addr_from_ip4addr(&pkti->ipi_addr, ip_2_ip4(netbuf_destaddr(buf)));
-          msg->msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo));
-          wrote_msg = 1;
+          wrote_msg = CMSG_SPACE(sizeof(struct in_pktinfo));
         } else {
           msg->msg_flags |= MSG_CTRUNC;
         }
 #endif /* LWIP_IPV4 */
       }
+#if LWIP_IPV6
+        else if (IP_IS_V6(&buf->toaddr)) {
+        if (msg->msg_controllen >= CMSG_SPACE(sizeof(struct in6_pktinfo))) {
+          struct cmsghdr *chdr = CMSG_FIRSTHDR(msg); /* This will always return a header!! */
+          struct in6_pktinfo *pkti = (struct in6_pktinfo *)CMSG_DATA(chdr);
+          chdr->cmsg_level = IPPROTO_IPV6;
+          chdr->cmsg_type = IPV6_PKTINFO;
+          chdr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
+          pkti->ipi6_ifindex = buf->p->if_idx;
+          inet6_addr_from_ip6addr(&pkti->ipi6_addr, ip_2_ip6(netbuf_destaddr(buf)));
+          wrote_msg = CMSG_SPACE(sizeof(struct in6_pktinfo));
+        } else {
+          msg->msg_flags |= MSG_CTRUNC;
+        }
+      }
+#endif /* LWIP_IPV6 */
+    }
+#if LWIP_IPV6
+    if (buf->flags & NETBUF_FLAG_HOPLIM) {
+      if (IP_IS_V6(&buf->toaddr)) {
+        if (msg->msg_controllen >= wrote_msg + CMSG_SPACE(sizeof(int))) {
+          struct cmsghdr *chdr = (struct cmsghdr *)((u8_t *)msg->msg_control + wrote_msg);
+          int *hoplim = (int *)CMSG_DATA(chdr);
+          chdr->cmsg_level = IPPROTO_IPV6;
+          chdr->cmsg_type = IPV6_HOPLIMIT;
+          chdr->cmsg_len = CMSG_LEN(sizeof(int));
+          *hoplim = buf->hoplim;
+          wrote_msg += CMSG_SPACE(sizeof(int));
+        } else {
+          msg->msg_flags |= MSG_CTRUNC;
+        }
+      }
     }
+#endif /* LWIP_IPV6 */
 #endif /* LWIP_NETBUF_RECVINFO */
 
-    if (!wrote_msg) {
-      msg->msg_controllen = 0;
-    }
+    msg->msg_controllen = wrote_msg;
   }
 
   /* If we don't peek the incoming message: zero lastdata pointer and free the netbuf */
@@ -2975,38 +2962,127 @@
           LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TOS) = %d\n",
                                       s, *(int *)optval));
           break;
-#if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP
+        case IP_MINTTL:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int);
+          *(int *)optval = sock->conn->pcb.ip->min_ttl;
+          LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MINTTL) = %d\n",
+                                      s, *(int *)optval));
+          break;
+        case IP_HDRINCL:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, *optlen, int, NETCONN_RAW);
+          if (sock->conn->pcb.raw->flags & RAW_FLAGS_HDRINCL) {
+            *(int *)optval = 1;
+          } else {
+            *(int *)optval = 0;
+          }
+          break;
+#if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS
         case IP_MULTICAST_TTL:
           LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, u8_t);
-          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_UDP) {
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            *(u8_t *)optval = udp_get_multicast_ttl(sock->conn->pcb.udp);
+          } else
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            *(u8_t *)optval = raw_get_multicast_ttl(sock->conn->pcb.raw);
+          } else
+#endif /* LWIP_RAW */
+          {
             done_socket(sock);
             return ENOPROTOOPT;
           }
-          *(u8_t *)optval = udp_get_multicast_ttl(sock->conn->pcb.udp);
           LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_TTL) = %d\n",
                                       s, *(int *)optval));
           break;
         case IP_MULTICAST_IF:
           LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, struct in_addr);
-          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_UDP) {
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            inet_addr_from_ip4addr((struct in_addr *)optval, udp_get_multicast_netif_addr(sock->conn->pcb.udp));
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            inet_addr_from_ip4addr((struct in_addr *)optval, raw_get_multicast_netif_addr(sock->conn->pcb.raw));
+          } else 
+#endif /* LWIP_RAW */
+          {
             done_socket(sock);
             return ENOPROTOOPT;
           }
-          inet_addr_from_ip4addr((struct in_addr *)optval, udp_get_multicast_netif_addr(sock->conn->pcb.udp));
           LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_IF) = 0x%"X32_F"\n",
                                       s, *(u32_t *)optval));
           break;
         case IP_MULTICAST_LOOP:
           LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, u8_t);
-          if ((sock->conn->pcb.udp->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) {
-            *(u8_t *)optval = 1;
-          } else {
-            *(u8_t *)optval = 0;
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            if ((sock->conn->pcb.udp->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) {
+              *(u8_t *)optval = 1;
+            } else {
+              *(u8_t *)optval = 0;
+            }
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            if ((sock->conn->pcb.raw->flags & RAW_FLAGS_MULTICAST_LOOP) != 0) {
+              *(u8_t *)optval = 1;
+            } else {
+              *(u8_t *)optval = 0;
+            }
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
           }
           LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_LOOP) = %d\n",
                                       s, *(int *)optval));
           break;
-#endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP */
+#endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS */
+#if LWIP_IGMP
+        case IP_MSFILTER:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, struct ip_msfilter);
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            err = mcast_sock_get_msfilter(&sock->conn->pcb.udp->ipmc, optname, (struct ip_msfilter *)optval, optlen);
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            err = mcast_sock_get_msfilter(&sock->conn->pcb.raw->ipmc, optname, (struct ip_msfilter *)optval, optlen);
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          break;
+        case MCAST_MSFILTER:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, struct group_filter);
+          if (((const struct group_filter *)optval)->gf_group.ss_family != AF_INET) {
+            done_socket(sock);
+            return EINVAL;
+          }
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            err = mcast_sock_get_groupfilter(&sock->conn->pcb.udp->ipmc, optname, (struct group_filter *)optval, optlen);
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            err = mcast_sock_get_groupfilter(&sock->conn->pcb.raw->ipmc, optname, (struct group_filter *)optval, optlen);
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          break;
+#endif /* LWIP_IGMP */
         default:
           LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n",
                                       s, optname));
@@ -3066,6 +3142,107 @@
     /* Level: IPPROTO_IPV6 */
     case IPPROTO_IPV6:
       switch (optname) {
+        case IPV6_MINHOPCNT:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int);
+          *(int *)optval = sock->conn->pcb.ip->min_ttl;
+          LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IPV6, IPV6_MINHOPCNT) = %d\n",
+                                      s, *(int *)optval));
+          break;
+        case IPV6_UNICAST_HOPS:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int);
+          *(int *)optval = sock->conn->pcb.ip->ttl;
+          LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IPV6, IPV6_UNICAST_HOPS) = %d\n",
+                                      s, *(int *)optval));
+          break;
+#if LWIP_MULTICAST_TX_OPTIONS
+        case IPV6_MULTICAST_IF:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int);
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            *(int *)optval = udp_get_multicast_netif_index(sock->conn->pcb.udp);
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            *(int *)optval = raw_get_multicast_netif_index(sock->conn->pcb.raw);
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IPV6, IPV6_MULTICAST_IF) = 0x%"X32_F"\n",
+                                      s, *(u32_t *)optval));
+          break;
+        case IPV6_MULTICAST_HOPS:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, u8_t);
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            *(u8_t *)optval = udp_get_multicast_ttl(sock->conn->pcb.udp);
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            *(u8_t *)optval = raw_get_multicast_ttl(sock->conn->pcb.raw);
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IPV6, IPV6_MULTICAST_HOPS) = 0x%"X8_F"\n",
+                                      s, *(u8_t *)optval));
+          break;
+        case IPV6_MULTICAST_LOOP:
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            if ((sock->conn->pcb.udp->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) {
+              *(u8_t *)optval = 1;
+            } else {
+              *(u8_t *)optval = 0;
+            }
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            if ((sock->conn->pcb.raw->flags & RAW_FLAGS_MULTICAST_LOOP) != 0) {
+              *(u8_t *)optval = 1;
+            } else {
+              *(u8_t *)optval = 0;
+            }
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IPV6, IPV6_MULTICAST_LOOP) = 0x%"X8_F"\n",
+                                      s, *(u8_t *)optval));
+          break;
+#endif /* LWIP_MULTICAST_TX_OPTIONS */
+#if LWIP_IPV6_MLD
+        case MCAST_MSFILTER:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, struct group_filter);
+          if (((const struct group_filter *)optval)->gf_group.ss_family != AF_INET6) {
+            done_socket(sock);
+            return EINVAL;
+          }
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            err = mcast_sock_get_groupfilter(&sock->conn->pcb.udp->ipmc, optname, (struct group_filter *)optval, optlen);
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            err = mcast_sock_get_groupfilter(&sock->conn->pcb.raw->ipmc, optname, (struct group_filter *)optval, optlen);
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          break;
+#endif /* LWIP_IPV6_MLD */
         case IPV6_V6ONLY:
           LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, *optlen, int);
           *(int *)optval = (netconn_get_ipv6only(sock->conn) ? 1 : 0);
@@ -3414,65 +3591,281 @@
           LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TOS, ..)-> %d\n",
                                       s, sock->conn->pcb.ip->tos));
           break;
+        case IP_MINTTL:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int);
+          sock->conn->pcb.ip->min_ttl = (u8_t)(*(const int *)optval);
+          LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_MINTTL, ..) -> %d\n",
+                                      s, sock->conn->pcb.ip->min_ttl));
+          break;
+        case IP_HDRINCL:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, int, NETCONN_RAW);
+          if (*(int *)optval) {
+            sock->conn->pcb.raw->flags |= RAW_FLAGS_HDRINCL;
+          } else {
+            sock->conn->pcb.raw->flags &= ~RAW_FLAGS_HDRINCL;
+          }
+          LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_HDRINCL, ..)-> %d\n",
+                                      s, *(int *)optval));
+          break;
 #if LWIP_NETBUF_RECVINFO
         case IP_PKTINFO:
-          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, int, NETCONN_UDP);
-          if (*(const int *)optval) {
-            sock->conn->flags |= NETCONN_FLAG_PKTINFO;
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int);
+          if ((NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) ||
+              (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW)) {
+            if (*(const int *)optval) {
+              sock->conn->flags |= NETCONN_FLAG_PKTINFO;
+            } else {
+              sock->conn->flags &= ~NETCONN_FLAG_PKTINFO;
+            }
           } else {
-            sock->conn->flags &= ~NETCONN_FLAG_PKTINFO;
+            done_socket(sock);
+            return ENOPROTOOPT;
           }
           break;
 #endif /* LWIP_NETBUF_RECVINFO */
-#if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP
+#if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS
         case IP_MULTICAST_TTL:
-          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, u8_t, NETCONN_UDP);
-          udp_set_multicast_ttl(sock->conn->pcb.udp, (u8_t)(*(const u8_t *)optval));
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, u8_t);
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            udp_set_multicast_ttl(sock->conn->pcb.udp, (u8_t)(*(const u8_t *)optval));
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            raw_set_multicast_ttl(sock->conn->pcb.raw, (u8_t)(*(const u8_t *)optval));
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
           break;
         case IP_MULTICAST_IF: {
           ip4_addr_t if_addr;
-          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, struct in_addr, NETCONN_UDP);
-          inet_addr_to_ip4addr(&if_addr, (const struct in_addr *)optval);
-          udp_set_multicast_netif_addr(sock->conn->pcb.udp, &if_addr);
+          u8_t idx;
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, struct in_addr);
+          if (!(((const struct in_addr *)optval)->s_addr & PP_HTONL(IP_CLASSA_NET))) { /* IS a BSD style index ? */
+            idx = (u8_t)PP_NTOHL(((const struct in_addr *)optval)->s_addr);
+            if (idx == NETIF_NO_INDEX) {
+              done_socket(sock);
+              return EINVAL;
+            }
+          } else {
+            idx = NETIF_NO_INDEX;
+            inet_addr_to_ip4addr(&if_addr, (const struct in_addr *)optval); /* IS a POSIX in_addr */
+          }
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            if (idx != NETIF_NO_INDEX) {
+              udp_set_multicast_netif_index(sock->conn->pcb.udp, idx);
+            } else {
+              udp_set_multicast_netif_addr(sock->conn->pcb.udp, &if_addr);
+            }
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            if (idx != NETIF_NO_INDEX) {
+              raw_set_multicast_netif_index(sock->conn->pcb.raw, idx);
+            } else {
+              raw_set_multicast_netif_addr(sock->conn->pcb.raw, &if_addr);
+            }
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
         }
         break;
         case IP_MULTICAST_LOOP:
-          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, u8_t, NETCONN_UDP);
-          if (*(const u8_t *)optval) {
-            udp_set_flags(sock->conn->pcb.udp, UDP_FLAGS_MULTICAST_LOOP);
-          } else {
-            udp_clear_flags(sock->conn->pcb.udp, UDP_FLAGS_MULTICAST_LOOP);
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, u8_t);
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            if (*(const u8_t *)optval) {
+              udp_set_flags(sock->conn->pcb.udp, UDP_FLAGS_MULTICAST_LOOP);
+            } else {
+              udp_clear_flags(sock->conn->pcb.udp, UDP_FLAGS_MULTICAST_LOOP);
+            }
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            if (*(const u8_t *)optval) {
+              raw_set_flags(sock->conn->pcb.raw, RAW_FLAGS_MULTICAST_LOOP);
+            } else {
+              raw_clear_flags(sock->conn->pcb.raw, RAW_FLAGS_MULTICAST_LOOP);
+            }
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
           }
           break;
-#endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP */
+#endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS */
 #if LWIP_IGMP
         case IP_ADD_MEMBERSHIP:
         case IP_DROP_MEMBERSHIP: {
-          /* If this is a TCP or a RAW socket, ignore these options. */
-          err_t igmp_err;
-          const struct ip_mreq *imr = (const struct ip_mreq *)optval;
-          ip4_addr_t if_addr;
-          ip4_addr_t multi_addr;
-          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, struct ip_mreq, NETCONN_UDP);
-          inet_addr_to_ip4addr(&if_addr, &imr->imr_interface);
-          inet_addr_to_ip4addr(&multi_addr, &imr->imr_multiaddr);
-          if (optname == IP_ADD_MEMBERSHIP) {
-            if (!lwip_socket_register_membership(s, &if_addr, &multi_addr)) {
-              /* cannot track membership (out of memory) */
-              err = ENOMEM;
-              igmp_err = ERR_OK;
-            } else {
-              igmp_err = igmp_joingroup(&if_addr, &multi_addr);
-            }
-          } else {
-            igmp_err = igmp_leavegroup(&if_addr, &multi_addr);
-            lwip_socket_unregister_membership(s, &if_addr, &multi_addr);
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, struct ip_mreq);
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            err = mcast_sock_add_drop_membership(&sock->conn->pcb.udp->ipmc, optname, (const struct ip_mreq *)optval);
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            err = mcast_sock_add_drop_membership(&sock->conn->pcb.raw->ipmc, optname, (const struct ip_mreq *)optval);
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
           }
-          if (igmp_err != ERR_OK) {
-            err = EADDRNOTAVAIL;
+          break;
+        case IP_ADD_SOURCE_MEMBERSHIP:
+        case IP_DROP_SOURCE_MEMBERSHIP:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, struct ip_mreq_source);
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            err = mcast_sock_add_drop_source_membership(&sock->conn->pcb.udp->ipmc, optname, (const struct ip_mreq_source *)optval);
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            err = mcast_sock_add_drop_source_membership(&sock->conn->pcb.raw->ipmc, optname, (const struct ip_mreq_source *)optval);
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
           }
-        }
-        break;
+          break;
+        case IP_BLOCK_SOURCE:
+        case IP_UNBLOCK_SOURCE:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, struct ip_mreq_source);
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            err = mcast_sock_block_unblock_source(&sock->conn->pcb.udp->ipmc, optname, (const struct ip_mreq_source *)optval);
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            err = mcast_sock_block_unblock_source(&sock->conn->pcb.raw->ipmc, optname, (const struct ip_mreq_source *)optval);
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          break;
+        case IP_MSFILTER:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, struct ip_msfilter);
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            err = mcast_sock_set_msfilter(&sock->conn->pcb.udp->ipmc, optname, (const struct ip_msfilter *)optval);
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            err = mcast_sock_set_msfilter(&sock->conn->pcb.raw->ipmc, optname, (const struct ip_msfilter *)optval);
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          break;
+        case MCAST_JOIN_GROUP:
+        case MCAST_LEAVE_GROUP:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, struct group_req);
+          if (((const struct group_req *)optval)->gr_group.ss_family != AF_INET) {
+            done_socket(sock);
+            return EINVAL;
+          }
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            err = mcast_sock_join_leave_group(&sock->conn->pcb.udp->ipmc, optname, (const struct group_req *)optval);
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            err = mcast_sock_join_leave_group(&sock->conn->pcb.raw->ipmc, optname, (const struct group_req *)optval);
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          break;
+        case MCAST_JOIN_SOURCE_GROUP:
+        case MCAST_LEAVE_SOURCE_GROUP:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, struct group_source_req);
+          if ((((const struct group_source_req *)optval)->gsr_group.ss_family != AF_INET) ||
+              (((const struct group_source_req *)optval)->gsr_source.ss_family != AF_INET)) {
+            done_socket(sock);
+            return EINVAL;
+          }
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            err = mcast_sock_join_leave_source_group(&sock->conn->pcb.udp->ipmc, optname, (const struct group_source_req *)optval);
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            err = mcast_sock_join_leave_source_group(&sock->conn->pcb.raw->ipmc, optname, (const struct group_source_req *)optval);
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          break;
+        case MCAST_BLOCK_SOURCE:
+        case MCAST_UNBLOCK_SOURCE:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, struct group_source_req);
+          if ((((const struct group_source_req *)optval)->gsr_group.ss_family != AF_INET) ||
+              (((const struct group_source_req *)optval)->gsr_source.ss_family != AF_INET)) {
+            done_socket(sock);
+            return EINVAL;
+          }
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            err = mcast_sock_block_unblock_source_group(&sock->conn->pcb.udp->ipmc, optname, (const struct group_source_req *)optval);
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            err = mcast_sock_block_unblock_source_group(&sock->conn->pcb.raw->ipmc, optname, (const struct group_source_req *)optval);
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          break;
+        case MCAST_MSFILTER:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, struct group_filter);
+          if (((const struct group_filter *)optval)->gf_group.ss_family != AF_INET) {
+            done_socket(sock);
+            return EINVAL;
+          }
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP) {
+            err = mcast_sock_set_groupfilter(&sock->conn->pcb.udp->ipmc, optname, (const struct group_filter *)optval);
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW) {
+            err = mcast_sock_set_groupfilter(&sock->conn->pcb.raw->ipmc, optname, (const struct group_filter *)optval);
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          break;
 #endif /* LWIP_IGMP */
         default:
           LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n",
@@ -3547,40 +3940,218 @@
           LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IPV6, IPV6_V6ONLY, ..) -> %d\n",
                                       s, (netconn_get_ipv6only(sock->conn) ? 1 : 0)));
           break;
-#if LWIP_IPV6_MLD
-        case IPV6_JOIN_GROUP:
-        case IPV6_LEAVE_GROUP: {
-          /* If this is a TCP or a RAW socket, ignore these options. */
-          err_t mld6_err;
-          struct netif *netif;
-          ip6_addr_t multi_addr;
-          const struct ipv6_mreq *imr = (const struct ipv6_mreq *)optval;
-          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, struct ipv6_mreq, NETCONN_UDP);
-          inet6_addr_to_ip6addr(&multi_addr, &imr->ipv6mr_multiaddr);
-          LWIP_ASSERT("Invalid netif index", imr->ipv6mr_interface <= 0xFFu);
-          netif = netif_get_by_index((u8_t)imr->ipv6mr_interface);
-          if (netif == NULL) {
-            err = EADDRNOTAVAIL;
-            break;
+#if LWIP_NETBUF_RECVINFO
+        case IPV6_MINHOPCNT:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int);
+          sock->conn->pcb.ip->min_ttl = (u8_t)(*(const int *)optval);
+          LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IPV6, IPV6_MINHOPCNT, ..) -> %d\n",
+                                      s, sock->conn->pcb.ip->min_ttl));
+          break;
+        case IPV6_RECVPKTINFO:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int);
+          if ((NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP_IPV6) ||
+              (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW_IPV6)) {
+            if (*(const int *)optval) {
+              sock->conn->flags |= NETCONN_FLAG_PKTINFO;
+            } else {
+              sock->conn->flags &= ~NETCONN_FLAG_PKTINFO;
+            }
+          } else {
+            done_socket(sock);
+            return ENOPROTOOPT;
           }
-
-          if (optname == IPV6_JOIN_GROUP) {
-            if (!lwip_socket_register_mld6_membership(s, imr->ipv6mr_interface, &multi_addr)) {
-              /* cannot track membership (out of memory) */
-              err = ENOMEM;
-              mld6_err = ERR_OK;
+          break;
+        case IPV6_RECVHOPLIMIT:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int);
+          if ((NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP_IPV6) ||
+              (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW_IPV6)) {
+            if (*(const int *)optval) {
+              sock->conn->flags |= NETCONN_FLAG_HOPLIM;
             } else {
-              mld6_err = mld6_joingroup_netif(netif, &multi_addr);
+              sock->conn->flags &= ~NETCONN_FLAG_HOPLIM;
             }
           } else {
-            mld6_err = mld6_leavegroup_netif(netif, &multi_addr);
-            lwip_socket_unregister_mld6_membership(s, imr->ipv6mr_interface, &multi_addr);
+            done_socket(sock);
+            return ENOPROTOOPT;
           }
-          if (mld6_err != ERR_OK) {
-            err = EADDRNOTAVAIL;
+          break;
+          break;
+#endif /* LWIP_NETBUF_RECVINFO */
+        case IPV6_UNICAST_HOPS:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int);
+          sock->conn->pcb.ip->ttl = (u8_t)(*(const int *)optval);
+          LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IPV6, IPV6_UNICAST_HOPS, ..) -> %d\n",
+                                      s, sock->conn->pcb.ip->ttl));
+          break;
+#if LWIP_MULTICAST_TX_OPTIONS
+        case IPV6_MULTICAST_IF:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int);
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP_IPV6) {
+            udp_set_multicast_netif_index(sock->conn->pcb.udp, (u8_t)(*(int *)optval));
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW_IPV6) {
+            raw_set_multicast_netif_index(sock->conn->pcb.raw, (u8_t)(*(int *)optval));
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
           }
-        }
-        break;
+          break;
+        case IPV6_MULTICAST_HOPS:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, u8_t);
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP_IPV6) {
+            udp_set_multicast_ttl(sock->conn->pcb.udp, (u8_t)(*(const u8_t *)optval));
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW_IPV6) {
+            raw_set_multicast_ttl(sock->conn->pcb.raw, (u8_t)(*(const u8_t *)optval));
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          break;
+        case IPV6_MULTICAST_LOOP:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, u8_t);
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP_IPV6) {
+            if (*(const u8_t *)optval) {
+              udp_set_flags(sock->conn->pcb.udp, UDP_FLAGS_MULTICAST_LOOP);
+            } else {
+              udp_clear_flags(sock->conn->pcb.udp, UDP_FLAGS_MULTICAST_LOOP);
+            }
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW_IPV6) {
+            if (*(const u8_t *)optval) {
+              raw_set_flags(sock->conn->pcb.raw, RAW_FLAGS_MULTICAST_LOOP);
+            } else {
+              raw_clear_flags(sock->conn->pcb.raw, RAW_FLAGS_MULTICAST_LOOP);
+            }
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          break;
+#endif /* LWIP_MULTICAST_TX_OPTIONS */
+#if LWIP_IPV6_MLD
+        case IPV6_JOIN_GROUP:
+        case IPV6_LEAVE_GROUP: {
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, struct ipv6_mreq);
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP_IPV6) {
+            err = mcast_sock_ipv6_add_drop_membership(&sock->conn->pcb.udp->ipmc, optname, (const struct ipv6_mreq *)optval);
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW_IPV6) {
+            err = mcast_sock_ipv6_add_drop_membership(&sock->conn->pcb.raw->ipmc, optname, (const struct ipv6_mreq *)optval);
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          break;
+        case MCAST_JOIN_GROUP:
+        case MCAST_LEAVE_GROUP:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, struct group_req);
+          if (((const struct group_req *)optval)->gr_group.ss_family != AF_INET6) {
+            done_socket(sock);
+            return EINVAL;
+          }
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP_IPV6) {
+            err = mcast_sock_join_leave_group(&sock->conn->pcb.udp->ipmc, optname, (const struct group_req *)optval);
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW_IPV6) {
+            err = mcast_sock_join_leave_group(&sock->conn->pcb.raw->ipmc, optname, (const struct group_req *)optval);
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          break;
+        case MCAST_JOIN_SOURCE_GROUP:
+        case MCAST_LEAVE_SOURCE_GROUP:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, struct group_source_req);
+          if ((((const struct group_source_req *)optval)->gsr_group.ss_family != AF_INET6) ||
+              (((const struct group_source_req *)optval)->gsr_source.ss_family != AF_INET6)) {
+            done_socket(sock);
+            return EINVAL;
+          }
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP_IPV6) {
+            err = mcast_sock_join_leave_source_group(&sock->conn->pcb.udp->ipmc, optname, (const struct group_source_req *)optval);
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW_IPV6) {
+            err = mcast_sock_join_leave_source_group(&sock->conn->pcb.raw->ipmc, optname, (const struct group_source_req *)optval);
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          break;
+        case MCAST_BLOCK_SOURCE:
+        case MCAST_UNBLOCK_SOURCE:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, struct group_source_req);
+          if ((((const struct group_source_req *)optval)->gsr_group.ss_family != AF_INET6) ||
+              (((const struct group_source_req *)optval)->gsr_source.ss_family != AF_INET6)) {
+            done_socket(sock);
+            return EINVAL;
+          }
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP_IPV6) {
+            err = mcast_sock_block_unblock_source_group(&sock->conn->pcb.udp->ipmc, optname, (const struct group_source_req *)optval);
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW_IPV6) {
+            err = mcast_sock_block_unblock_source_group(&sock->conn->pcb.raw->ipmc, optname, (const struct group_source_req *)optval);
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          break;
+        case MCAST_MSFILTER:
+          LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, struct group_filter);
+          if (((const struct group_filter *)optval)->gf_group.ss_family != AF_INET6) {
+            done_socket(sock);
+            return EINVAL;
+          }
+#if LWIP_UDP
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_UDP_IPV6) {
+            err = mcast_sock_set_groupfilter(&sock->conn->pcb.udp->ipmc, optname, (const struct group_filter *)optval);
+          } else 
+#endif /* LWIP_UDP */
+#if LWIP_RAW
+          if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_RAW_IPV6) {
+            err = mcast_sock_set_groupfilter(&sock->conn->pcb.raw->ipmc, optname, (const struct group_filter *)optval);
+          } else 
+#endif /* LWIP_RAW */
+          {
+            done_socket(sock);
+            return ENOPROTOOPT;
+          }
+          break;
 #endif /* LWIP_IPV6_MLD */
         default:
           LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IPV6, UNIMPL: optname=0x%x, ..)\n",
@@ -3727,9 +4298,9 @@
       /* Check if there is data left from the last recv operation. /maq 041215 */
       if (sock->lastdata.netbuf) {
         if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_TCP) {
-          recv_avail += sock->lastdata.pbuf->tot_len;
-        } else {
           recv_avail += sock->lastdata.netbuf->p->tot_len;
+        } else {
+          recv_avail += sock->lastdata.pbuf->tot_len;
         }
       }
       *((int *)argp) = recv_avail;
@@ -3913,184 +4484,4 @@
   }
   return err;
 }
-
-#if LWIP_IGMP
-/** Register a new IGMP membership. On socket close, the membership is dropped automatically.
- *
- * ATTENTION: this function is called from tcpip_thread (or under CORE_LOCK).
- *
- * @return 1 on success, 0 on failure
- */
-static int
-lwip_socket_register_membership(int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr)
-{
-  struct lwip_sock *sock = get_socket(s);
-  int i;
-
-  if (!sock) {
-    return 0;
-  }
-
-  for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) {
-    if (socket_ipv4_multicast_memberships[i].sock == NULL) {
-      socket_ipv4_multicast_memberships[i].sock = sock;
-      ip4_addr_copy(socket_ipv4_multicast_memberships[i].if_addr, *if_addr);
-      ip4_addr_copy(socket_ipv4_multicast_memberships[i].multi_addr, *multi_addr);
-      done_socket(sock);
-      return 1;
-    }
-  }
-  done_socket(sock);
-  return 0;
-}
-
-/** Unregister a previously registered membership. This prevents dropping the membership
- * on socket close.
- *
- * ATTENTION: this function is called from tcpip_thread (or under CORE_LOCK).
- */
-static void
-lwip_socket_unregister_membership(int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr)
-{
-  struct lwip_sock *sock = get_socket(s);
-  int i;
-
-  if (!sock) {
-    return;
-  }
-
-  for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) {
-    if ((socket_ipv4_multicast_memberships[i].sock == sock) &&
-        ip4_addr_cmp(&socket_ipv4_multicast_memberships[i].if_addr, if_addr) &&
-        ip4_addr_cmp(&socket_ipv4_multicast_memberships[i].multi_addr, multi_addr)) {
-      socket_ipv4_multicast_memberships[i].sock = NULL;
-      ip4_addr_set_zero(&socket_ipv4_multicast_memberships[i].if_addr);
-      ip4_addr_set_zero(&socket_ipv4_multicast_memberships[i].multi_addr);
-      break;
-    }
-  }
-  done_socket(sock);
-}
-
-/** Drop all memberships of a socket that were not dropped explicitly via setsockopt.
- *
- * ATTENTION: this function is NOT called from tcpip_thread (or under CORE_LOCK).
- */
-static void
-lwip_socket_drop_registered_memberships(int s)
-{
-  struct lwip_sock *sock = get_socket(s);
-  int i;
-
-  if (!sock) {
-    return;
-  }
-
-  for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) {
-    if (socket_ipv4_multicast_memberships[i].sock == sock) {
-      ip_addr_t multi_addr, if_addr;
-      ip_addr_copy_from_ip4(multi_addr, socket_ipv4_multicast_memberships[i].multi_addr);
-      ip_addr_copy_from_ip4(if_addr, socket_ipv4_multicast_memberships[i].if_addr);
-      socket_ipv4_multicast_memberships[i].sock = NULL;
-      ip4_addr_set_zero(&socket_ipv4_multicast_memberships[i].if_addr);
-      ip4_addr_set_zero(&socket_ipv4_multicast_memberships[i].multi_addr);
-
-      netconn_join_leave_group(sock->conn, &multi_addr, &if_addr, NETCONN_LEAVE);
-    }
-  }
-  done_socket(sock);
-}
-#endif /* LWIP_IGMP */
-
-#if LWIP_IPV6_MLD
-/** Register a new MLD6 membership. On socket close, the membership is dropped automatically.
- *
- * ATTENTION: this function is called from tcpip_thread (or under CORE_LOCK).
- *
- * @return 1 on success, 0 on failure
- */
-static int
-lwip_socket_register_mld6_membership(int s, unsigned int if_idx, const ip6_addr_t *multi_addr)
-{
-  struct lwip_sock *sock = get_socket(s);
-  int i;
-
-  if (!sock) {
-    return 0;
-  }
-
-  for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) {
-    if (socket_ipv6_multicast_memberships[i].sock == NULL) {
-      socket_ipv6_multicast_memberships[i].sock   = sock;
-      socket_ipv6_multicast_memberships[i].if_idx = (u8_t)if_idx;
-      ip6_addr_copy(socket_ipv6_multicast_memberships[i].multi_addr, *multi_addr);
-      done_socket(sock);
-      return 1;
-    }
-  }
-  done_socket(sock);
-  return 0;
-}
-
-/** Unregister a previously registered MLD6 membership. This prevents dropping the membership
- * on socket close.
- *
- * ATTENTION: this function is called from tcpip_thread (or under CORE_LOCK).
- */
-static void
-lwip_socket_unregister_mld6_membership(int s, unsigned int if_idx, const ip6_addr_t *multi_addr)
-{
-  struct lwip_sock *sock = get_socket(s);
-  int i;
-
-  if (!sock) {
-    return;
-  }
-
-  for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) {
-    if ((socket_ipv6_multicast_memberships[i].sock   == sock) &&
-        (socket_ipv6_multicast_memberships[i].if_idx == if_idx) &&
-        ip6_addr_cmp(&socket_ipv6_multicast_memberships[i].multi_addr, multi_addr)) {
-      socket_ipv6_multicast_memberships[i].sock   = NULL;
-      socket_ipv6_multicast_memberships[i].if_idx = NETIF_NO_INDEX;
-      ip6_addr_set_zero(&socket_ipv6_multicast_memberships[i].multi_addr);
-      break;
-    }
-  }
-  done_socket(sock);
-}
-
-/** Drop all MLD6 memberships of a socket that were not dropped explicitly via setsockopt.
- *
- * ATTENTION: this function is NOT called from tcpip_thread (or under CORE_LOCK).
- */
-static void
-lwip_socket_drop_registered_mld6_memberships(int s)
-{
-  struct lwip_sock *sock = get_socket(s);
-  int i;
-
-  if (!sock) {
-    return;
-  }
-
-  for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) {
-    if (socket_ipv6_multicast_memberships[i].sock == sock) {
-      ip_addr_t multi_addr;
-      u8_t if_idx;
-
-      ip_addr_copy_from_ip6(multi_addr, socket_ipv6_multicast_memberships[i].multi_addr);
-      if_idx = socket_ipv6_multicast_memberships[i].if_idx;
-
-      socket_ipv6_multicast_memberships[i].sock   = NULL;
-      socket_ipv6_multicast_memberships[i].if_idx = NETIF_NO_INDEX;
-      ip6_addr_set_zero(&socket_ipv6_multicast_memberships[i].multi_addr);
-
-      netconn_join_leave_group_netif(sock->conn, &multi_addr, if_idx, NETCONN_LEAVE);
-    }
-  }
-  done_socket(sock);
-}
-#endif /* LWIP_IPV6_MLD */
-
 #endif /* LWIP_SOCKET */
diff -Nur lwip/src/core/ipv4/igmp.c lwip-mcast/src/core/ipv4/igmp.c
--- lwip/src/core/ipv4/igmp.c	Fri Jan 26 13:32:36 2018
+++ lwip-mcast/src/core/ipv4/igmp.c	Sat Jan 27 16:47:40 2018
@@ -64,14 +64,19 @@
 but not checked rigorously incoming
 -------------------------------------------------------------
 Steve Reynolds
+-------------------------------------------------------------
+Note 4)
+Add support to IGMPv3
+-------------------------------------------------------------
+Han.hui
 ------------------------------------------------------------*/
 
 /*-----------------------------------------------------------------------------
  * RFC 988  - Host extensions for IP multicasting                         - V0
  * RFC 1054 - Host extensions for IP multicasting                         -
  * RFC 1112 - Host extensions for IP multicasting                         - V1
- * RFC 2236 - Internet Group Management Protocol, Version 2               - V2  <- this code is based on this RFC (it's the "de facto" standard)
- * RFC 3376 - Internet Group Management Protocol, Version 3               - V3
+ * RFC 2236 - Internet Group Management Protocol, Version 2               - V2
+ * RFC 3376 - Internet Group Management Protocol, Version 3               - V3  <- this code is based on this RFC
  * RFC 4604 - Using Internet Group Management Protocol Version 3...       - V3+
  * RFC 2113 - IP Router Alert Option                                      -
  *----------------------------------------------------------------------------*/
@@ -93,6 +98,7 @@
 #include "lwip/netif.h"
 #include "lwip/stats.h"
 #include "lwip/prot/igmp.h"
+#include "lwip/mcast.h"
 
 #include "string.h"
 
@@ -104,9 +110,22 @@
 static err_t  igmp_ip_output_if(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, struct netif *netif);
 static void   igmp_send(struct netif *netif, struct igmp_group *group, u8_t type);
 
+#if LWIP_IGMP_V3
+static void   igmp_v3_timeout(struct netif *netif, struct igmp_group *group);
+static void   igmp_v3_start_timer(struct igmp_group *group, u8_t max_time);
+static void   igmp_v3_delaying_member(struct igmp_group *group, u8_t maxresp);
+static void   igmp_v3_send(struct netif *netif, struct igmp_group *group, u8_t type);
+static void   igmp_v3_send_allgroups(struct netif *netif);
+static void   igmp_v3_trigger(struct netif *netif, const ip4_addr_t *groupaddr);
+#endif /* LWIP_IGMP_V3 */
+
 static ip4_addr_t     allsystems;
 static ip4_addr_t     allrouters;
 
+#if LWIP_IGMP_V3
+static ip4_addr_t     v3_routers;
+#endif /* LWIP_IGMP_V3 */
+
 /**
  * Initialize the IGMP module
  */
@@ -117,6 +136,12 @@
 
   IP4_ADDR(&allsystems, 224, 0, 0, 1);
   IP4_ADDR(&allrouters, 224, 0, 0, 2);
+
+#if LWIP_IGMP_V3
+  IP4_ADDR(&v3_routers, 224, 0, 0, 22);
+  
+  mcast_ip4_filter_trigger_callback(igmp_v3_trigger); /* install multicast filter trigger callback */
+#endif /* LWIP_IGMP_V3 */
 }
 
 /**
@@ -136,6 +161,9 @@
   if (group != NULL) {
     group->group_state = IGMP_GROUP_IDLE_MEMBER;
     group->use++;
+#if LWIP_IGMP_V3
+    group->v3_group_state = IGMP_GROUP_IDLE_MEMBER;
+#endif /* LWIP_IGMP_V3 */
 
     /* Allow the igmp messages at the MAC level */
     if (netif->igmp_mac_filter != NULL) {
@@ -204,6 +232,15 @@
     igmp_delaying_member(group, IGMP_JOIN_DELAYING_MEMBER_TMR);
     group = group->next;
   }
+
+#if LWIP_IGMP_V3
+  group = netif_igmp_data(netif);
+  
+  if (group) {
+    /* We use first group to determine report all groups */
+    igmp_v3_delaying_member(group, IGMP_JOIN_DELAYING_MEMBER_TMR);
+  }
+#endif /* LWIP_IGMP_V3 */
 }
 
 /**
@@ -261,6 +298,12 @@
     group->group_state        = IGMP_GROUP_NON_MEMBER;
     group->last_reporter_flag = 0;
     group->use                = 0;
+#if LWIP_IGMP_V3
+    group->v3_fmode              = IGMP_FMODE_INIT; /* Non mode with init-stat */
+    group->v3_timer              = 0; /* Not running */
+    group->v3_group_state        = IGMP_GROUP_NON_MEMBER;
+    group->v3_last_reporter_flag = 0;
+#endif /* LWIP_IGMP_V3 */
 
     /* Ensure allsystems group is always first in list */
     if (list_head == NULL) {
@@ -365,6 +408,69 @@
   /* NOW ACT ON THE INCOMING MESSAGE TYPE... */
   switch (igmp->igmp_msgtype) {
     case IGMP_MEMB_QUERY:
+#if LWIP_IGMP_V3
+      if (p->len >= IGMP_V3_MINLEN) { /* this is a igmp v3 query packet */
+        struct igmp_v3_query  *igmp_v3 = (struct igmp_v3_query *)p->payload;
+        
+        if ((ip4_addr_cmp(dest, &allsystems)) && ip4_addr_isany(&igmp_v3->igmp_v3_group_address)) {
+          /* THIS IS THE GENERAL QUERY */
+          groupref = netif_igmp_data(inp);
+          
+          if (groupref) {
+            /* We use first group to determine report all groups */
+            igmp_v3_delaying_member(groupref, igmp_v3->igmp_v3_maxresp);
+          }
+        
+        } else {
+          /* IGMP_MEMB_QUERY to a specific group ? */
+          if (!ip4_addr_isany(&igmp_v3->igmp_v3_group_address)) {
+            u16_t src_cnt = PP_NTOHS(igmp_v3->igmp_v3_srccnt);
+            u16_t src_buf_size = src_cnt << 2;
+            u8_t need_free;
+            ip4_addr_p_t *src_buf;
+            
+            if (p->tot_len < (IGMP_V3_QUERY_HLEN + src_buf_size)) {
+              /* packet len error */
+              pbuf_free(p);
+              IGMP_STATS_INC(igmp.lenerr);
+              LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: length error\n"));
+              return;
+            }
+            
+            if (src_cnt) { /* Seach interest */
+              if (p->len < (IGMP_V3_QUERY_HLEN + src_buf_size)) { /* Unfortunately! the source address memory is not contiguous */
+                src_buf = (ip4_addr_p_t *)mem_malloc(src_buf_size);
+                if (src_buf == NULL) {
+                  pbuf_free(p);
+                  IGMP_STATS_INC(igmp.memerr);
+                  LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: not enough memory for igmp_input\n"));
+                  return;
+                }
+                need_free = 1;
+                pbuf_copy_partial(p, src_buf, src_buf_size, IGMP_V3_QUERY_HLEN);
+
+              } else {
+                need_free = 0;
+                src_buf = (ip4_addr_p_t *)((u8_t *)p->payload + IGMP_V3_QUERY_HLEN);
+              }
+              
+              LWIP_ASSERT("igmp_v3_query packet source address array not aligned!", !((mem_ptr_t)src_buf & 0x3));
+              if (mcast_ip4_filter_interest(inp, (const ip4_addr_t *)&igmp_v3->igmp_v3_group_address, src_buf, src_cnt)) {
+                /* We interest! */
+                igmp_v3_delaying_member(group, igmp_v3->igmp_v3_maxresp);
+              }
+              
+              if (need_free) {
+                mem_free(src_buf);
+              }
+
+            } else { /* Report! */
+              igmp_v3_delaying_member(group, igmp_v3->igmp_v3_maxresp);
+            }
+          }
+        }
+      }
+#endif /* LWIP_IGMP_V3 */
       /* IGMP_MEMB_QUERY to the "all systems" address ? */
       if ((ip4_addr_cmp(dest, &allsystems)) && ip4_addr_isany(&igmp->igmp_group_address)) {
         /* THIS IS THE GENERAL QUERY */
@@ -455,7 +561,9 @@
 
   /* make sure it is multicast address */
   LWIP_ERROR("igmp_joingroup: attempt to join non-multicast address", ip4_addr_ismulticast(groupaddr), return ERR_VAL;);
-  LWIP_ERROR("igmp_joingroup: attempt to join allsystems address", (!ip4_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;);
+  if (ip4_addr_cmp(groupaddr, &allsystems)) {
+    return (ERR_OK); /* already in allsystem group */
+  }
 
   /* loop through netif's */
   NETIF_FOREACH(netif) {
@@ -490,7 +598,9 @@
 
   /* make sure it is multicast address */
   LWIP_ERROR("igmp_joingroup_netif: attempt to join non-multicast address", ip4_addr_ismulticast(groupaddr), return ERR_VAL;);
-  LWIP_ERROR("igmp_joingroup_netif: attempt to join allsystems address", (!ip4_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;);
+  if (ip4_addr_cmp(groupaddr, &allsystems)) {
+    return (ERR_OK); /* already in allsystem group */
+  }
 
   /* make sure it is an igmp-enabled netif */
   LWIP_ERROR("igmp_joingroup_netif: attempt to join on non-IGMP netif", netif->flags & NETIF_FLAG_IGMP, return ERR_VAL;);
@@ -523,6 +633,15 @@
 
       /* Need to work out where this timer comes from */
       group->group_state = IGMP_GROUP_DELAYING_MEMBER;
+
+#if LWIP_IGMP_V3
+      igmp_v3_send(netif, group, IGMP_V3_MEMB_REPORT);
+      
+      igmp_v3_start_timer(group, IGMP_JOIN_DELAYING_MEMBER_TMR);
+      
+      /* Need to work out where this timer comes from */
+      group->v3_group_state = IGMP_GROUP_DELAYING_MEMBER;
+#endif /* LWIP_IGMP_V3 */
     }
     /* Increment group use */
     group->use++;
@@ -552,7 +671,9 @@
 
   /* make sure it is multicast address */
   LWIP_ERROR("igmp_leavegroup: attempt to leave non-multicast address", ip4_addr_ismulticast(groupaddr), return ERR_VAL;);
-  LWIP_ERROR("igmp_leavegroup: attempt to leave allsystems address", (!ip4_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;);
+  if (ip4_addr_cmp(groupaddr, &allsystems)) {
+    return (ERR_OK); /* do not leave allsystem group */
+  }
 
   /* loop through netif's */
   NETIF_FOREACH(netif) {
@@ -586,7 +707,9 @@
 
   /* make sure it is multicast address */
   LWIP_ERROR("igmp_leavegroup_netif: attempt to leave non-multicast address", ip4_addr_ismulticast(groupaddr), return ERR_VAL;);
-  LWIP_ERROR("igmp_leavegroup_netif: attempt to leave allsystems address", (!ip4_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;);
+  if (ip4_addr_cmp(groupaddr, &allsystems)) {
+    return (ERR_OK); /* do not leave allsystem group */
+  }
 
   /* make sure it is an igmp-enabled netif */
   LWIP_ERROR("igmp_leavegroup_netif: attempt to leave on non-IGMP netif", netif->flags & NETIF_FLAG_IGMP, return ERR_VAL;);
@@ -612,6 +735,13 @@
         igmp_send(netif, group, IGMP_LEAVE_GROUP);
       }
 
+#if LWIP_IGMP_V3
+      /* If we are the last reporter for this group */
+      if (group->v3_last_reporter_flag) {
+        igmp_v3_send(netif, group, IGMP_LEAVE_GROUP);
+      }
+#endif /* LWIP_IGMP_V3 */
+
       /* Disable the group at the MAC level */
       if (netif->igmp_mac_filter != NULL) {
         LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup_netif: igmp_mac_filter(DEL "));
@@ -654,6 +784,19 @@
       }
       group = group->next;
     }
+    
+#if LWIP_IGMP_V3
+    group = netif_igmp_data(netif);
+    while (group != NULL) {
+      if (group->v3_timer > 0) {
+        group->v3_timer--;
+        if (group->v3_timer == 0) {
+          igmp_v3_timeout(netif, group);
+        }
+      }
+      group = group->next;
+    }
+#endif /* LWIP_IGMP_V3 */
   }
 }
 
@@ -681,6 +824,36 @@
   }
 }
 
+#if LWIP_IGMP_V3
+/**
+ * Called if a timeout for one group is reached.
+ * Sends a report for this group.
+ *
+ * @param group an igmp_group for which a timeout is reached
+ */
+static void
+igmp_v3_timeout(struct netif *netif, struct igmp_group *group)
+{
+  /* If the state is IGMP_GROUP_DELAYING_MEMBER then we send a report for this group
+     (if it is the allsystems group to all groups) */
+  if (group->v3_group_state == IGMP_GROUP_DELAYING_MEMBER) {
+    group->group_state = IGMP_GROUP_IDLE_MEMBER;
+    if (ip4_addr_cmp(&(group->group_address), &allsystems)) {
+      LWIP_DEBUGF(IGMP_DEBUG, ("igmp_v3_timeout: report all membership\n"));
+      IGMP_STATS_INC(igmp.tx_report);
+      igmp_v3_send_allgroups(netif);
+      
+    } else {
+      LWIP_DEBUGF(IGMP_DEBUG, ("igmp_v3_timeout: report membership for group with address "));
+      ip4_addr_debug_print(IGMP_DEBUG, &(group->group_address));
+      LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", (void *)netif));
+      IGMP_STATS_INC(igmp.tx_report);
+      igmp_v3_send(netif, group, IGMP_V3_MEMB_REPORT);
+    }
+  }
+}
+#endif /* LWIP_IGMP_V3 */
+
 /**
  * Start a timer for an igmp group
  *
@@ -703,6 +876,51 @@
   }
 }
 
+#if LWIP_IGMP_V3
+/**
+ * Start a timer for an igmp group
+ *
+ * @param group the igmp_group for which to start a timer
+ * @param max_time the time in multiples of IGMP_TMR_INTERVAL (decrease with
+ *        every call to igmp_tmr())
+ */
+static void
+igmp_v3_start_timer(struct igmp_group *group, u8_t max_time)
+{
+  /*
+   * If Max Resp Code < 128, Max Resp Time = Max Resp Code
+   *
+   * If Max Resp Code >= 128, Max Resp Code represents a floating-point
+   * value as follows:
+   *
+   *   0 1 2 3 4 5 6 7
+   *  +-+-+-+-+-+-+-+-+
+   *  |1| exp | mant  |
+   *  +-+-+-+-+-+-+-+-+
+   *
+   *  Max Resp Time = (mant | 0x10) << (exp + 3)
+   */
+  u16_t delay;
+  
+  if (max_time < 128) {
+    delay = max_time;
+  } else {
+    delay = ((max_time & 0xf) | 0x10) << (((max_time >> 4) & 7) + 3);
+  }
+
+#ifdef LWIP_RAND
+  group->v3_timer = (u16_t)(delay > 2 ? (LWIP_RAND() % delay) : 1);
+#else /* LWIP_RAND */
+  /* ATTENTION: use this only if absolutely necessary! */
+  group->v3_timer = delay / 2;
+#endif /* LWIP_RAND */
+
+  if (group->v3_timer == 0) {
+    group->v3_timer = 1;
+  }
+}
+#endif /* LWIP_IGMP_V3 */
+
 /**
  * Delaying membership report for a group if necessary
  *
@@ -720,6 +938,32 @@
   }
 }
 
+#if LWIP_IGMP_V3
+/**
+ * Delaying membership report for a group if necessary
+ *
+ * @param group the igmp_group for which "delaying" membership report
+ * @param maxresp query delay
+ */
+static void
+igmp_v3_delaying_member(struct igmp_group *group, u8_t maxresp)
+{
+  u16_t delay;
+  
+  if (maxresp < 128) {
+    delay = maxresp;
+  } else {
+    delay = ((maxresp & 0xf) | 0x10) << (((maxresp >> 4) & 7) + 3);
+  }
+
+  if ((group->v3_group_state == IGMP_GROUP_IDLE_MEMBER) ||
+      ((group->v3_group_state == IGMP_GROUP_DELAYING_MEMBER) &&
+       ((group->v3_timer == 0) || (delay < group->timer)))) {
+    igmp_v3_start_timer(group, maxresp);
+    group->v3_group_state = IGMP_GROUP_DELAYING_MEMBER;
+  }
+}
+#endif /* LWIP_IGMP_V3 */
 
 /**
  * Sends an IP packet on a network interface. This function constructs the IP header
@@ -798,4 +1042,218 @@
   }
 }
 
+#if LWIP_IGMP_V3
+/**
+ * Build a igmp v3 record to a specific group.
+ *
+ * @param rec record to build
+ * @param group the group to which to send the packet
+ * @param fmode filter mode
+ * @param src_array source addr array
+ * @param src_cnt source addr cnt
+ */
+static void
+igmp_v3_build_record(struct igmp_v3_record *rec, struct igmp_group *group, u8_t fmode, ip4_addr_p_t *src_array, u32_t src_cnt)
+{
+  ip4_addr_p_t *src_copy;
+  
+  if (fmode == MCAST_EXCLUDE) {
+    if (group->v3_fmode != MCAST_EXCLUDE) {
+      group->v3_fmode = MCAST_EXCLUDE;
+      rec->igmp_v3_rc_type = IGMP_V3_REC_TO_EX;
+    } else {
+      rec->igmp_v3_rc_type = IGMP_V3_REC_IS_EX;
+    }
+
+  } else {
+    if (group->v3_fmode != MCAST_INCLUDE) {
+      group->v3_fmode = MCAST_INCLUDE;
+      rec->igmp_v3_rc_type = IGMP_V3_REC_TO_IN;
+    } else {
+      rec->igmp_v3_rc_type = IGMP_V3_REC_IS_IN;
+    }
+  }
+  
+  rec->igmp_v3_rc_auxlen = 0;
+  rec->igmp_v3_rc_srccnt = PP_HTONS(src_cnt);
+  ip4_addr_set(&rec->igmp_v3_rc_group_address, &group->group_address);
+
+  if (src_cnt) {
+    src_copy = (ip4_addr_p_t *)(rec + 1);
+    MEMCPY(src_copy, src_array, (src_cnt << 2));
+  }
+}
+/**
+ * Send an igmp v3 packet to a specific group.
+ *
+ * @param group the group to which to send the packet
+ * @param type the type of igmp packet to send
+ */
+static void
+igmp_v3_send(struct netif *netif, struct igmp_group *group, u8_t type)
+{
+  struct igmp_v3_report *rep;
+  struct igmp_v3_record *rec;
+  ip4_addr_t src;
+  struct pbuf *p;
+  
+  ip4_addr_copy(src, *netif_ip4_addr(netif));
+  
+  if (type == IGMP_LEAVE_GROUP) { /* Leave from a group */
+    p = pbuf_alloc(PBUF_TRANSPORT, IGMP_V3_REPORT_HLEN + IGMP_V3_RECORD_LEN(0), PBUF_RAM);
+    if (p == NULL) {
+      LWIP_DEBUGF(IGMP_DEBUG, ("igmp_v3_send: not enough memory for igmp_v3_send\n"));
+      IGMP_STATS_INC(igmp.memerr);
+      return;
+    }
+    
+    rep = (struct igmp_v3_report *)p->payload;
+    rep->igmp_v3_msgtype  = IGMP_V3_MEMB_REPORT;
+    rep->igmp_v3_reserve1 = 0;
+    rep->igmp_v3_checksum = 0;
+    rep->igmp_v3_reserve2 = 0;
+    rep->igmp_v3_reccnt   = PP_HTONS(1);
+    
+    rec = (struct igmp_v3_record *)(rep + 1);
+    if (group->v3_fmode == MCAST_EXCLUDE) {
+      rec->igmp_v3_rc_type = IGMP_V3_REC_TO_IN;
+    } else {
+      rec->igmp_v3_rc_type = IGMP_V3_REC_IS_IN;
+    }
+    rec->igmp_v3_rc_auxlen = 0;
+    rec->igmp_v3_rc_srccnt = 0; /* IS_IN (NULL) mean drop group */
+    ip4_addr_set(&rec->igmp_v3_rc_group_address, &group->group_address);
+    rep->igmp_v3_checksum = inet_chksum(rep, IGMP_V3_REPORT_HLEN + IGMP_V3_RECORD_LEN(0));
+    
+  } else { /* Report a group */
+    ip4_addr_p_t src_array[LWIP_MCAST_SRC_TBL_SIZE];
+    u16_t src_cnt;
+    u8_t fmode;
+    
+    src_cnt = mcast_ip4_filter_info(netif, &group->group_address, src_array, LWIP_MCAST_SRC_TBL_SIZE, &fmode);
+    LWIP_ASSERT("igmp_v3_send: multicast filter error!", !(!src_cnt && (fmode == MCAST_INCLUDE)));
+    
+    p = pbuf_alloc(PBUF_TRANSPORT, IGMP_V3_REPORT_HLEN + IGMP_V3_RECORD_LEN(src_cnt), PBUF_RAM);
+    if (p == NULL) {
+      LWIP_DEBUGF(IGMP_DEBUG, ("igmp_v3_send: not enough memory for igmp_v3_send\n"));
+      IGMP_STATS_INC(igmp.memerr);
+      return;
+    }
+    
+    rep = (struct igmp_v3_report *)p->payload;
+    rep->igmp_v3_msgtype  = IGMP_V3_MEMB_REPORT;
+    rep->igmp_v3_reserve1 = 0;
+    rep->igmp_v3_checksum = 0;
+    rep->igmp_v3_reserve2 = 0;
+    rep->igmp_v3_reccnt   = PP_HTONS(1);
+    
+    rec = (struct igmp_v3_record *)(rep + 1);
+    igmp_v3_build_record(rec, group, fmode, src_array, src_cnt);
+    rep->igmp_v3_checksum = inet_chksum(rep, IGMP_V3_REPORT_HLEN + IGMP_V3_RECORD_LEN(src_cnt));
+    group->v3_last_reporter_flag = 1; /* Remember we were the last to report */
+  }
+
+  igmp_ip_output_if(p, &src, &v3_routers, netif);
+  pbuf_free(p);
+}
+
+/**
+ * Send igmp v3 packet to report all groups.
+ */
+static void
+igmp_v3_send_allgroups(struct netif *netif)
+{
+  struct igmp_group *group;
+  struct igmp_v3_report *rep;
+  struct igmp_v3_record *rec;
+  struct pbuf *p, *p_rec;
+  ip4_addr_t src;
+  ip4_addr_p_t src_array[LWIP_MCAST_SRC_TBL_SIZE];
+  u16_t src_cnt;
+  u16_t max_pkt_len = (u16_t)(netif->mtu ? netif->mtu : 0xffff);
+  u8_t fmode;
+  
+  ip4_addr_copy(src, *netif_ip4_addr(netif));
+  
+  group = netif_igmp_data(netif);
+  if (group) {
+    group = group->next; /* do not report allsystem group */
+  }
+  
+  if (!group) {
+    return; /* no group */
+  }
+  
+  src_cnt = mcast_ip4_filter_info(netif, &group->group_address, src_array, LWIP_MCAST_SRC_TBL_SIZE, &fmode);
+  LWIP_ASSERT("igmp_v3_send: multicast filter error!", !(!src_cnt && (fmode == MCAST_INCLUDE)));
+  
+  while (group) {
+    p = pbuf_alloc(PBUF_TRANSPORT, IGMP_V3_REPORT_HLEN + IGMP_V3_RECORD_LEN(src_cnt), PBUF_RAM);
+    if (p == NULL) {
+      LWIP_DEBUGF(IGMP_DEBUG, ("igmp_v3_send_allgroups: not enough memory for igmp_v3_send_allgroups\n"));
+      IGMP_STATS_INC(igmp.memerr);
+      return;
+    }
+    
+    rep = (struct igmp_v3_report *)p->payload;
+    rep->igmp_v3_msgtype  = IGMP_V3_MEMB_REPORT;
+    rep->igmp_v3_reserve1 = 0;
+    rep->igmp_v3_checksum = 0;
+    rep->igmp_v3_reserve2 = 0;
+    rep->igmp_v3_reccnt   = PP_HTONS(1);
+    
+    rec = (struct igmp_v3_record *)(rep + 1);
+    igmp_v3_build_record(rec, group, fmode, src_array, src_cnt);
+    group->v3_last_reporter_flag = 1; /* Remember we were the last to report */
+    
+cat_rec:
+    group = group->next; /* the next group */
+    if (group) {
+      src_cnt = mcast_ip4_filter_info(netif, &group->group_address, src_array, LWIP_MCAST_SRC_TBL_SIZE, &fmode);
+      LWIP_ASSERT("igmp_v3_send: multicast filter error!", !(!src_cnt && (fmode == MCAST_INCLUDE)));
+      
+      if (p->tot_len + IGMP_V3_RECORD_LEN(src_cnt) < max_pkt_len) { /* can add a record? */
+        p_rec = pbuf_alloc(PBUF_RAW, IGMP_V3_RECORD_LEN(src_cnt), PBUF_RAM);
+        if (p_rec) {
+          rep->igmp_v3_reccnt = PP_HTONS(PP_NTOHS(rep->igmp_v3_reccnt) + 1); /* add a record */
+          
+          rec = (struct igmp_v3_record *)p_rec->payload;
+          igmp_v3_build_record(rec, group, fmode, src_array, src_cnt);
+          group->v3_last_reporter_flag = 1; /* Remember we were the last to report */
+          pbuf_cat(p, p_rec); /* cat to tail */
+          goto cat_rec;
+        
+        } else {
+          LWIP_DEBUGF(IGMP_DEBUG, ("igmp_v3_send_allgroups: not enough memory for igmp_v3_send_allgroups\n"));
+          IGMP_STATS_INC(igmp.memerr);
+        }
+      }
+    }
+    
+    rep->igmp_v3_checksum = inet_chksum_pbuf(p);
+    igmp_ip_output_if(p, &src, &v3_routers, netif);
+    pbuf_free(p);
+  }
+}
+
+/**
+ * igmp v3 report trigger.
+ */
+static void
+igmp_v3_trigger(struct netif *netif, const ip4_addr_t *groupaddr)
+{
+  struct igmp_group *group;
+
+  /* find group */
+  group = igmp_lookfor_group(netif, groupaddr);
+  if (group) {
+    igmp_v3_send(netif, group, IGMP_V3_MEMB_REPORT);
+      
+    igmp_v3_start_timer(group, IGMP_JOIN_DELAYING_MEMBER_TMR);
+      
+    /* Need to work out where this timer comes from */
+    group->v3_group_state = IGMP_GROUP_DELAYING_MEMBER;
+  }
+}
+#endif /* LWIP_IGMP_V3 */
 #endif /* LWIP_IPV4 && LWIP_IGMP */
diff -Nur lwip/src/core/mcast.c lwip-mcast/src/core/mcast.c
--- lwip/src/core/mcast.c	Thu Jan  1 08:00:00 1970
+++ lwip-mcast/src/core/mcast.c	Tue Jan 23 16:06:34 2018
@@ -0,0 +1,1951 @@
+/**
+ * @file
+ * Multicast filter module\n
+ */
+
+/*
+ * Copyright (c) 2018 Han.hui
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
+ * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
+ * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * This file is part of the lwIP TCP/IP stack.
+ *
+ * Author: Han.hui <hanhui@acoinfo.com>
+ *
+ */
+
+#include "lwip/opt.h"
+
+#if LWIP_UDP || LWIP_RAW /* don't build if not configured for use in lwipopts.h */
+
+#include "lwip/err.h"
+#include "lwip/udp.h"
+#include "lwip/raw.h"
+#include "lwip/def.h"
+#include "lwip/mem.h"
+#include "lwip/netif.h"
+#include "lwip/errno.h"
+#include "lwip/ip_addr.h"
+#include "lwip/sockets.h"
+
+#include <stddef.h> /* We need offsetof() */
+
+#if LWIP_IPV4 && LWIP_IGMP
+/**
+ * ipv4 multicast filter trigger function
+ */
+#if LWIP_IGMP_V3
+static mcast_trigger_ip4_fn mcast_ip4_trigger;
+#define IP4_MC_TRIGGER_CALL(netif, multi_addr) \
+  if (mcast_ip4_trigger) { \
+    mcast_ip4_trigger((netif), (multi_addr)); \
+  }
+#else
+#define IP4_MC_TRIGGER_CALL(netif, multi_addr)
+#endif /* !LWIP_IGMP_V3 */
+
+/**
+ * ipv4 multicast filter find
+ */
+struct ip4_mc *
+mcast_ip4_mc_find(struct ip_mc *ipmc, struct netif *netif, const ip4_addr_t *multi_addr, struct ip4_mc **mc_prev)
+{
+  struct ip4_mc *prev = NULL;
+  struct ip4_mc *mc;
+  
+  IP4_MC_FOREACH(ipmc, mc) {
+    if (ip4_addr_cmp(&mc->if_addr, netif_ip4_addr(netif)) && 
+        ip4_addr_cmp(&mc->multi_addr, multi_addr)) { /* check interface and multicast address */
+      if (mc_prev) {
+        *mc_prev = prev;
+      }
+      return mc; /* found! */
+    }
+    prev = mc;
+  }
+  
+  return NULL; /* not found! */
+}
+
+/**
+ * ipv4 multicast filter find source
+ */
+static struct igmp_src *
+mcast_ip4_mc_src_find(struct ip4_mc *mc, const ip4_addr_t *src_addr, struct igmp_src **src_prev)
+{
+  struct igmp_src *prev = NULL;
+  struct igmp_src *src;
+  
+  IP4_MC_SRC_FOREACH(mc, src) {
+    if (ip4_addr_cmp(&src->src_addr, src_addr)) { /* check source address */
+      if (src_prev) {
+        *src_prev = prev;
+      }
+      return src; /* found! */
+    }
+    prev = src;
+  }
+  
+  return NULL; /* not found! */
+}
+
+/**
+ * ipv4 multicast filter remove all source
+ */
+static void
+mcast_ip4_mc_src_remove(struct igmp_src *src)
+{
+  struct igmp_src *next;
+  
+  while (src) {
+    next = src->next;
+    mem_free(src);
+    src = next;
+  }
+}
+
+#if LWIP_IGMP_V3
+/**
+ * install ipv4 multicast filter trigger function
+ */
+void
+mcast_ip4_filter_trigger_callback(mcast_trigger_ip4_fn trigger_callback)
+{
+  mcast_ip4_trigger = trigger_callback;
+}
+
+/**
+ * ipv4 multicast filter group source infomation (use ip4_addr_p_t for IGMPv3 speedup)
+ */
+u16_t
+mcast_ip4_filter_info(struct netif *netif, const ip4_addr_t *multi_addr, ip4_addr_p_t addr_array[], u16_t arr_cnt, u8_t *fmode)
+{
+  static ip4_addr_p_t in_tbl[LWIP_MCAST_SRC_TBL_SIZE];
+  static ip4_addr_p_t ex_tbl[LWIP_MCAST_SRC_TBL_SIZE];
+  
+  struct ip4_mc *mc;
+  struct igmp_src *src;
+  ip4_addr_t addr; /* for compare speed */
+  u16_t i, j;
+  u16_t cnt, in_cnt = 0, ex_cnt = 0;
+  u16_t max_cnt = (u16_t)((arr_cnt > LWIP_MCAST_SRC_TBL_SIZE) ? LWIP_MCAST_SRC_TBL_SIZE : arr_cnt);
+  u8_t match = 0;
+  
+#define LWIP_IP4_MC_GET(pcb, pcbs, type, tbl, c) \
+  for ((pcb) = (pcbs); (pcb) != NULL; (pcb) = (pcb)->next) { \
+    mc = mcast_ip4_mc_find(&(pcb)->ipmc, netif, multi_addr, NULL); \
+    if ((mc == NULL) || (mc->fmode != (type))) { \
+      continue; \
+    } \
+    match = 1; /* group matched */ \
+    IP4_MC_SRC_FOREACH(mc, src) { \
+      if ((c) < max_cnt) { \
+        ip4_addr_set(&(tbl)[(c)], &src->src_addr); /* save a source */ \
+        (c)++; \
+      } else { \
+        *fmode = MCAST_EXCLUDE; /* table overflow, we need all this group packet */ \
+        return (0); \
+      } \
+    } \
+  }
+  
+#if LWIP_UDP
+  {
+    struct udp_pcb *pcb;
+    LWIP_IP4_MC_GET(pcb, udp_pcbs, MCAST_INCLUDE, in_tbl, in_cnt); /* copy all udp include source address to in_tbl[] */
+  }
+#endif /* LWIP_UDP */
+    
+#if LWIP_RAW
+  {
+    struct raw_pcb *pcb;
+    LWIP_IP4_MC_GET(pcb, raw_pcbs, MCAST_INCLUDE, in_tbl, in_cnt); /* copy all raw include source address to in_tbl[] */
+  }
+#endif /* LWIP_UDP */
+
+#if LWIP_UDP
+  {
+    struct udp_pcb *pcb;
+    LWIP_IP4_MC_GET(pcb, udp_pcbs, MCAST_EXCLUDE, ex_tbl, ex_cnt); /* copy all udp exclude source address to ex_tbl[] */
+  }
+#endif /* LWIP_UDP */
+    
+#if LWIP_RAW
+  {
+    struct raw_pcb *pcb;
+    LWIP_IP4_MC_GET(pcb, raw_pcbs, MCAST_EXCLUDE, ex_tbl, ex_cnt); /* copy all raw exclude source address to ex_tbl[] */
+  }
+#endif /* LWIP_UDP */
+
+  if (ex_cnt) { /* at least have one exclude source address */
+    *fmode = MCAST_EXCLUDE;
+    for (i = 0; i < ex_cnt; i++) {
+      ip4_addr_set(&addr, &ex_tbl[i]);
+      for (j = 0; j < in_cnt; j++) {
+        if (ip4_addr_cmp(&addr, &in_tbl[j])) { /* check exclude conflict with include table */
+          ip4_addr_set_any(&ex_tbl[i]); /* remove from exclude table */
+          break;
+        }
+      }
+    }
+    
+    for (i = 0, cnt = 0; i < ex_cnt; i++) {
+      if (!ip4_addr_isany(&ex_tbl[i])) {
+        ip4_addr_set(&addr_array[cnt], &ex_tbl[i]);
+        cnt++;
+      }
+    }
+    
+  } else if (in_cnt) { /* at least have one include source address */
+    *fmode = MCAST_INCLUDE;
+    for (i = 0; i < in_cnt; i++) {
+      ip4_addr_set(&addr_array[i], &in_tbl[i]);
+    }
+    cnt = i;
+  
+  } else {
+    if (match) { /* at least have one pcb matched */
+      *fmode = MCAST_EXCLUDE;
+    } else { /* no match! */
+      *fmode = MCAST_INCLUDE;
+    }
+    cnt = 0;
+  }
+
+  return (cnt);
+}
+
+/**
+ * ipv4 multicast filter source address interest (use ip4_addr_p_t for IGMPv3 speedup)
+ */
+u8_t
+mcast_ip4_filter_interest(struct netif *netif, const ip4_addr_t *multi_addr, const ip4_addr_p_t src_addr[], u16_t arr_cnt)
+{
+  static ip4_addr_p_t ip_tbl[LWIP_MCAST_SRC_TBL_SIZE];
+  u16_t i, j, cnt;
+  u8_t fmode;
+  
+  cnt = mcast_ip4_filter_info(netif, multi_addr, ip_tbl, LWIP_MCAST_SRC_TBL_SIZE, &fmode);
+  if (fmode == MCAST_EXCLUDE) {
+    for (i = 0; i < cnt; i++) {
+      for (j = 0; j < arr_cnt; j++) {
+        if (ip4_addr_cmp(&src_addr[j], &ip_tbl[i])) {
+          return 0;
+        }
+      }
+    }
+    return 1;
+    
+  } else {
+    for (i = 0; i < cnt; i++) {
+      for (j = 0; j < arr_cnt; j++) {
+        if (ip4_addr_cmp(&src_addr[j], &ip_tbl[i])) {
+          return 1;
+        }
+      }
+    }
+    return 0;
+  }
+}
+#endif /* LWIP_IGMP_V3 */
+#endif /* LWIP_IPV4 && LWIP_IGMP */
+
+#if LWIP_IPV6 && LWIP_IPV6_MLD
+/**
+ * ipv6 multicast filter trigger function
+ */
+#if LWIP_IPV6_MLD_V2
+static mcast_trigger_ip6_fn mcast_ip6_trigger;
+#define IP6_MC_TRIGGER_CALL(netif, multi_addr) \
+  if (mcast_ip6_trigger) { \
+    mcast_ip6_trigger((netif), (multi_addr)); \
+  }
+#else
+#define IP6_MC_TRIGGER_CALL(netif, multi_addr)
+#endif /* !LWIP_IPV6_MLD_V2 */
+
+/**
+ * ipv6 multicast filter find
+ */
+static struct ip6_mc *
+mcast_ip6_mc_find(struct ip_mc *ipmc, struct netif *netif, const ip6_addr_t *multi_addr, struct ip6_mc **mc_prev)
+{
+  struct ip6_mc *prev = NULL;
+  struct ip6_mc *mc;
+  
+  IP6_MC_FOREACH(ipmc, mc) {
+    if (((mc->if_idx == NETIF_NO_INDEX) || (mc->if_idx == netif_get_index(netif))) && 
+        ip6_addr_cmp_zoneless(&mc->multi_addr, multi_addr)) { /* check interface and multicast address */
+      if (mc_prev) {
+        *mc_prev = prev;
+      }
+      return mc; /* found! */
+    }
+    prev = mc;
+  }
+  
+  return NULL; /* not found! */
+}
+
+/**
+ * ipv6 multicast filter find source
+ */
+static struct mld6_src *
+mcast_ip6_mc_src_find(struct ip6_mc *mc, const ip6_addr_t *src_addr, struct mld6_src **src_prev)
+{
+  struct mld6_src *prev = NULL;
+  struct mld6_src *src;
+  
+  IP6_MC_SRC_FOREACH(mc, src) {
+    if (ip6_addr_cmp_zoneless(&src->src_addr, src_addr)) { /* check source address */
+      if (src_prev) {
+        *src_prev = prev;
+      }
+      return src; /* found! */
+    }
+    prev = src;
+  }
+  
+  return NULL; /* not found! */
+}
+
+/**
+ * ipv6 multicast filter remove all source
+ */
+static void
+mcast_ip6_mc_src_remove(struct mld6_src *src)
+{
+  struct mld6_src *next;
+  
+  while (src) {
+    next = src->next;
+    mem_free(src);
+    src = next;
+  }
+}
+
+#if LWIP_IPV6_MLD_V2
+/**
+ * install ipv6 multicast filter trigger function
+ */
+void
+mcast_ip6_filter_trigger_callback(mcast_trigger_ip6_fn trigger_callback)
+{
+  mcast_ip6_trigger = trigger_callback;
+}
+
+/**
+ * ipv6 multicast filter group source infomation (use ip6_addr_p_t for MLDv2 speedup)
+ */
+u16_t
+mcast_ip6_filter_info(struct netif *netif, const ip6_addr_t *multi_addr, ip6_addr_p_t addr_array[], u16_t arr_cnt, u8_t *fmode)
+{
+  static ip6_addr_p_t in_tbl[LWIP_MCAST_SRC_TBL_SIZE];
+  static ip6_addr_p_t ex_tbl[LWIP_MCAST_SRC_TBL_SIZE];
+  
+  struct ip6_mc *mc;
+  struct mld6_src *src;
+  ip6_addr_t addr; /* for compare speed */
+  u16_t i, j;
+  u16_t cnt, in_cnt = 0, ex_cnt = 0;
+  u16_t max_cnt = (u16_t)((arr_cnt > LWIP_MCAST_SRC_TBL_SIZE) ? LWIP_MCAST_SRC_TBL_SIZE : arr_cnt);
+  u8_t match = 0;
+  
+#define LWIP_IP6_MC_GET(pcb, pcbs, type, tbl, c) \
+  for ((pcb) = (pcbs); (pcb) != NULL; (pcb) = (pcb)->next) { \
+    mc = mcast_ip6_mc_find(&(pcb)->ipmc, netif, multi_addr, NULL); \
+    if ((mc == NULL) || (mc->fmode != (type))) { \
+      continue; \
+    } \
+    match = 1; \
+    IP6_MC_SRC_FOREACH(mc, src) { \
+      if ((c) < max_cnt) { \
+        ip6_addr_copy_to_packed((tbl)[(c)], src->src_addr); /* save a source */ \
+        (c)++; \
+      } else { \
+        *fmode = MCAST_EXCLUDE; /* table overflow, we need all this group packet */ \
+        return (0); \
+      } \
+    } \
+  }
+  
+#if LWIP_UDP
+  {
+    struct udp_pcb *pcb;
+    LWIP_IP6_MC_GET(pcb, udp_pcbs, MCAST_INCLUDE, in_tbl, in_cnt); /* copy all udp include source address to in_tbl[] */
+  }
+#endif /* LWIP_UDP */
+    
+#if LWIP_RAW
+  {
+    struct raw_pcb *pcb;
+    LWIP_IP6_MC_GET(pcb, raw_pcbs, MCAST_INCLUDE, in_tbl, in_cnt); /* copy all raw include source address to in_tbl[] */
+  }
+#endif /* LWIP_UDP */
+
+#if LWIP_UDP
+  {
+    struct udp_pcb *pcb;
+    LWIP_IP6_MC_GET(pcb, udp_pcbs, MCAST_EXCLUDE, ex_tbl, ex_cnt); /* copy all udp exclude source address to ex_tbl[] */
+  }
+#endif /* LWIP_UDP */
+    
+#if LWIP_RAW
+  {
+    struct raw_pcb *pcb;
+    LWIP_IP6_MC_GET(pcb, raw_pcbs, MCAST_EXCLUDE, ex_tbl, ex_cnt); /* copy all raw exclude source address to ex_tbl[] */
+  }
+#endif /* LWIP_UDP */
+
+  if (ex_cnt) { /* at least have one exclude source address */
+    *fmode = MCAST_EXCLUDE;
+    for (i = 0; i < ex_cnt; i++) {
+      ip6_addr_copy_from_packed(addr, ex_tbl[i]);
+      for (j = 0; j < in_cnt; j++) {
+        if (ip6_addr_cmp_zoneless(&addr, &in_tbl[j])) { /* check exclude conflict with include table */
+          ip6_addr_copy_to_packed(ex_tbl[i], *IP6_ADDR_ANY6); /* remove from exclude table */
+          break;
+        }
+      }
+    }
+    
+    for (i = 0, cnt = 0; i < ex_cnt; i++) {
+      if (!ip6_addr_isany(&ex_tbl[i])) {
+        ip6_addr_copy_to_packed(addr_array[cnt], ex_tbl[i]);
+        cnt++;
+      }
+    }
+    
+  } else if (in_cnt) { /* at least have one include source address */
+    *fmode = MCAST_INCLUDE;
+    for (i = 0; i < in_cnt; i++) {
+      ip6_addr_copy_to_packed(addr_array[i], in_tbl[i]);
+    }
+    cnt = i;
+  
+  } else {
+    if (match) { /* at least have one pcb matched */
+      *fmode = MCAST_EXCLUDE;
+    } else { /* no match! */
+      *fmode = MCAST_INCLUDE;
+    }
+    cnt = 0;
+  }
+
+  return (cnt);
+}
+
+/**
+ * ipv6 multicast filter source address interest (use ip6_addr_p_t for MLDv2 speedup)
+ */
+u8_t
+mcast_ip6_filter_interest(struct netif *netif, const ip6_addr_t *multi_addr, const ip6_addr_p_t src_addr[], u16_t arr_cnt)
+{
+  static ip6_addr_p_t ip_tbl[LWIP_MCAST_SRC_TBL_SIZE];
+  u16_t i, j, cnt;
+  u8_t fmode;
+  
+  cnt = mcast_ip6_filter_info(netif, multi_addr, ip_tbl, LWIP_MCAST_SRC_TBL_SIZE, &fmode);
+  if (fmode == MCAST_EXCLUDE) {
+    for (i = 0; i < cnt; i++) {
+      for (j = 0; j < arr_cnt; j++) {
+        if (ip6_addr_cmp_zoneless(&src_addr[j], &ip_tbl[i])) {
+          return 0;
+        }
+      }
+    }
+    return 1;
+    
+  } else {
+    for (i = 0; i < cnt; i++) {
+      for (j = 0; j < arr_cnt; j++) {
+        if (ip6_addr_cmp_zoneless(&src_addr[j], &ip_tbl[i])) {
+          return 1;
+        }
+      }
+    }
+    return 0;
+  }
+}
+#endif /* LWIP_IPV6_MLD_V2 */
+#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
+
+#if (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD)
+/** Join a multicast group (Can with a source specified)
+ *
+ * @param ipmc multicast filter control block
+ * @param netif the network interface which should join a new group.
+ * @param multi_addr the ipv6 address of the group to join
+ * @param src_addr multicast source address (can be NULL)
+ * @return lwIP error definitions
+ */
+err_t
+mcast_join_netif(struct ip_mc *ipmc, struct netif *netif, const ip_addr_t *multi_addr, const ip_addr_t *src_addr)
+{
+#if LWIP_UDP
+  if (ipmc->proto == IPPROTO_UDP) {
+    err_t err;
+    struct udp_pcb *pcb;
+    /* prepare UDP pcb to udp_pcbs list */
+    pcb = (struct udp_pcb *)((u8_t *)ipmc - offsetof(struct udp_pcb, ipmc));
+    if (pcb->local_port == 0) {
+      err = udp_bind(pcb, &pcb->local_ip, pcb->local_port);
+      if (err != ERR_OK) {
+        LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("mcast_join_netif: forced port bind failed\n"));
+        return err;
+      }
+    }
+  }
+#endif /* LWIP_UDP */
+
+#if LWIP_IPV4 && LWIP_IGMP
+  if (IP_IS_V4(multi_addr)) {
+    struct ip4_mc *mc;
+    struct igmp_src *src;
+    
+    mc = mcast_ip4_mc_find(ipmc, netif, ip_2_ip4(multi_addr), NULL);
+    if (mc) {
+      if (src_addr) {
+        if ((mc->fmode == MCAST_EXCLUDE) && (mc->src)) {
+          return ERR_VAL; /* filter mode not include mode */
+        }
+        src = mcast_ip4_mc_src_find(mc, ip_2_ip4(src_addr), NULL);
+        if (src) {
+          return ERR_ALREADY; /* already in source list */
+        }
+        
+        src = (struct igmp_src *)mem_malloc(sizeof(struct igmp_src));
+        if (src == NULL) {
+          return ERR_MEM; /* no memory */
+        }
+        ip4_addr_set(&src->src_addr, ip_2_ip4(src_addr));
+        src->next = mc->src;
+        mc->src = src;
+        mc->fmode = MCAST_INCLUDE; /* change to include mode */
+        IP4_MC_TRIGGER_CALL(netif, ip_2_ip4(multi_addr)); /* trigger a report */
+      }
+      return ERR_OK;
+    }
+    
+    mc = (struct ip4_mc *)mem_malloc(sizeof(struct ip4_mc)); /* Make a new mc */
+    if (mc == NULL) {
+      igmp_leavegroup_netif(netif, ip_2_ip4(multi_addr));
+      return ERR_MEM; /* no memory */
+    }
+    ip4_addr_set(&mc->if_addr, netif_ip4_addr(netif));
+    ip4_addr_set(&mc->multi_addr, ip_2_ip4(multi_addr));
+    
+    if (src_addr) { /* have a source specified */
+      mc->fmode = MCAST_INCLUDE;
+      src = (struct igmp_src *)mem_malloc(sizeof(struct igmp_src));
+      if (src == NULL) {
+        mem_free(mc);
+        return ERR_MEM; /* no memory */
+      }
+      ip4_addr_set(&src->src_addr, ip_2_ip4(src_addr));
+      src->next = NULL;
+      mc->src = src;
+      
+    } else {
+      mc->fmode = MCAST_EXCLUDE; /* no source specified */
+      mc->src = NULL;
+    }
+    
+    mc->next = ipmc->mc4;
+    ipmc->mc4 = mc;
+    igmp_joingroup_netif(netif, ip_2_ip4(multi_addr));
+  } else 
+#endif /* LWIP_IPV4 && LWIP_IGMP */
+  {
+#if LWIP_IPV6 && LWIP_IPV6_MLD
+    struct ip6_mc *mc;
+    struct mld6_src *src;
+    
+    mc = mcast_ip6_mc_find(ipmc, netif, ip_2_ip6(multi_addr), NULL);
+    if (mc) {
+      if (src_addr) {
+        if ((mc->fmode == MCAST_EXCLUDE) && (mc->src)) {
+          return ERR_VAL; /* filter mode not include mode */
+        }
+        src = mcast_ip6_mc_src_find(mc, ip_2_ip6(src_addr), NULL);
+        if (src) {
+          return ERR_ALREADY; /* already in source list */
+        }
+        
+        src = (struct mld6_src *)mem_malloc(sizeof(struct mld6_src));
+        if (src == NULL) {
+          return ERR_MEM; /* no memory */
+        }
+        ip6_addr_set(&src->src_addr, ip_2_ip6(src_addr));
+        src->next = mc->src;
+        mc->src = src;
+        mc->fmode = MCAST_INCLUDE; /* change to include mode */
+        IP6_MC_TRIGGER_CALL(netif, ip_2_ip6(multi_addr)); /* trigger a report */
+      }
+      return ERR_OK;
+    }
+    
+    mc = (struct ip6_mc *)mem_malloc(sizeof(struct ip6_mc)); /* Make a new mc */
+    if (mc == NULL) {
+      mld6_leavegroup_netif(netif, ip_2_ip6(multi_addr));
+      return ERR_MEM; /* no memory */
+    }
+    mc->if_idx = netif_get_index(netif);
+    ip6_addr_set(&mc->multi_addr, ip_2_ip6(multi_addr));
+    
+    if (src_addr) {
+      mc->fmode = MCAST_INCLUDE;
+      src = (struct mld6_src *)mem_malloc(sizeof(struct mld6_src));
+      if (src == NULL) {
+        mld6_leavegroup_netif(netif, ip_2_ip6(multi_addr));
+        mem_free(mc);
+        return ERR_MEM; /* no memory */
+      }
+      ip6_addr_set(&src->src_addr, ip_2_ip6(src_addr));
+      src->next = NULL;
+      mc->src = src;
+      
+    } else {
+      mc->fmode = MCAST_EXCLUDE; /* no source specified */
+      mc->src = NULL;
+    }
+    
+    mc->next = ipmc->mc6;
+    ipmc->mc6 = mc;
+    mld6_joingroup_netif(netif, ip_2_ip6(multi_addr));
+#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
+  }
+  return ERR_OK;
+}
+
+/** Join a multicast group (Can with a source specified)
+ *
+ * @param ipmc multicast filter control block
+ * @param if_addr the network interface address.
+ * @param multi_addr the ipv6 address of the group to join
+ * @param src_addr multicast source address (can be NULL)
+ * @return lwIP error definitions
+ */
+err_t
+mcast_join_group(struct ip_mc *ipmc, const ip_addr_t *if_addr, const ip_addr_t *multi_addr, const ip_addr_t *src_addr)
+{
+  err_t err = ERR_VAL; /* no matching interface */
+  
+#if LWIP_IPV4 && LWIP_IGMP
+  if (IP_IS_V4(multi_addr)) {
+    struct netif *netif;
+    
+    NETIF_FOREACH(netif) {
+      if ((netif->flags & NETIF_FLAG_IGMP) && ((ip4_addr_isany(ip_2_ip4(if_addr)) || ip4_addr_cmp(netif_ip4_addr(netif), ip_2_ip4(if_addr))))) {
+        err = mcast_join_netif(ipmc, netif, multi_addr, src_addr);
+        if (err != ERR_OK) {
+          return (err);
+        }
+      }
+    }
+  } else 
+#endif /* LWIP_IPV4 && LWIP_IGMP */
+  {
+#if LWIP_IPV6 && LWIP_IPV6_MLD
+    struct netif *netif;
+    
+    NETIF_FOREACH(netif) {
+      if (ip6_addr_isany(ip_2_ip6(if_addr)) ||
+          netif_get_ip6_addr_match(netif, ip_2_ip6(if_addr)) >= 0) {
+        err = mcast_join_netif(ipmc, netif, multi_addr, src_addr);
+        if (err != ERR_OK) {
+          return (err);
+        }
+      }
+    }
+#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
+  }
+  return err;
+}
+
+/** Leave or drop a source from group on a network interface.
+ *
+ * @param ipmc multicast filter control block
+ * @param netif the network interface which should leave group.
+ * @param multi_addr the address of the group to leave
+ * @param src_addr multicast source address
+ * @return lwIP error definitions
+ */
+err_t
+mcast_leave_netif(struct ip_mc *ipmc, struct netif *netif, const ip_addr_t *multi_addr, const ip_addr_t *src_addr)
+{
+#if LWIP_IPV4 && LWIP_IGMP
+  if (IP_IS_V4(multi_addr)) {
+    struct ip4_mc *mc_prev;
+    struct ip4_mc *mc;
+    struct igmp_src *src_prev;
+    struct igmp_src *src;
+    
+    mc = mcast_ip4_mc_find(ipmc, netif, ip_2_ip4(multi_addr), &mc_prev);
+    if (mc == NULL) {
+      return ERR_VAL;
+    }
+    
+    if (src_addr) {
+      if ((mc->fmode == MCAST_EXCLUDE) && (mc->src)) {
+        return ERR_VAL; /* drop source membership must in include mode */
+      }
+      
+      src = mcast_ip4_mc_src_find(mc, ip_2_ip4(src_addr), &src_prev);
+      if (src) {
+        if (src_prev) {
+          src_prev->next = src->next;
+        } else {
+          mc->src = src->next;
+        }
+        mem_free(src);
+      } else {
+        return ERR_VAL;
+      }
+      if (mc->src) {
+        IP4_MC_TRIGGER_CALL(netif, ip_2_ip4(multi_addr)); /* trigger a report */
+        return ERR_OK;
+      }
+    } else { /* we want drop this group */
+      mcast_ip4_mc_src_remove(mc->src);
+    }
+    
+    igmp_leavegroup_netif(netif, ip_2_ip4(multi_addr));
+    if (mc_prev) {
+      mc_prev->next = mc->next;
+    } else {
+      ipmc->mc4 = mc->next;
+    }
+    mem_free(mc);
+  } else
+#endif /* LWIP_IPV4 && LWIP_IGMP */
+  {
+#if LWIP_IPV6 && LWIP_IPV6_MLD
+    struct ip6_mc *mc_prev;
+    struct ip6_mc *mc;
+    struct mld6_src *src_prev;
+    struct mld6_src *src;
+    
+    mc = mcast_ip6_mc_find(ipmc, netif, ip_2_ip6(multi_addr), &mc_prev);
+    if (mc == NULL) {
+      return ERR_VAL;
+    }
+    
+    if (src_addr) {
+      if ((mc->fmode == MCAST_EXCLUDE) && (mc->src)) {
+        return ERR_VAL; /* drop source membership must in include mode */
+      }
+      
+      src = mcast_ip6_mc_src_find(mc, ip_2_ip6(src_addr), &src_prev);
+      if (src) {
+        if (src_prev) {
+          src_prev->next = src->next;
+        } else {
+          mc->src = src->next;
+        }
+        mem_free(src);
+      } else {
+        return ERR_VAL;
+      }
+      if (mc->src) {
+        IP6_MC_TRIGGER_CALL(netif, ip_2_ip6(multi_addr)); /* trigger a report */
+        return ERR_OK;
+      }
+    } else { /* we want drop this group */
+      mcast_ip6_mc_src_remove(mc->src);
+    }
+
+    mld6_leavegroup_netif(netif, ip_2_ip6(multi_addr));
+    if (mc_prev) {
+      mc_prev->next = mc->next;
+    } else {
+      ipmc->mc6 = mc->next;
+    }
+    mem_free(mc);
+#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
+  }
+  return ERR_OK;
+}
+
+/** Leave or drop a source from group on a network interface.
+ *
+ * @param ipmc multicast filter control block
+ * @param if_addr the network interface address.
+ * @param multi_addr the address of the group to leave
+ * @param src_addr multicast source address
+ * @return lwIP error definitions
+ */
+err_t
+mcast_leave_group(struct ip_mc *ipmc, const ip_addr_t *if_addr, const ip_addr_t *multi_addr, const ip_addr_t *src_addr)
+{
+  err_t res, err = ERR_VAL; /* no matching interface */
+
+#if LWIP_IPV4 && LWIP_IGMP
+  if (IP_IS_V4(multi_addr)) {
+    struct netif *netif;
+    
+    NETIF_FOREACH(netif) {
+      if ((netif->flags & NETIF_FLAG_IGMP) && ((ip4_addr_isany(ip_2_ip4(if_addr)) || ip4_addr_cmp(netif_ip4_addr(netif), ip_2_ip4(if_addr))))) {
+        res = mcast_leave_netif(ipmc, netif, multi_addr, src_addr);
+        if (err != ERR_OK) {
+          err = res;
+        }
+      }
+    }
+  } else 
+#endif /* LWIP_IPV4 && LWIP_IGMP */
+  {
+#if LWIP_IPV6 && LWIP_IPV6_MLD
+    struct netif *netif;
+    
+    NETIF_FOREACH(netif) {
+      if (ip6_addr_isany(ip_2_ip6(if_addr)) ||
+          netif_get_ip6_addr_match(netif, ip_2_ip6(if_addr)) >= 0) {
+        res = mcast_leave_netif(ipmc, netif, multi_addr, src_addr);
+        if (err != ERR_OK) {
+          err = res;
+        }
+      }
+    }
+#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
+  }
+  return err;
+}
+
+/** Add a block source address to a multicast group
+ *
+ * @param ipmc multicast filter control block
+ * @param netif the network interface which group we already join.
+ * @param multi_addr the address of the group to add source
+ * @param blk_addr block multicast source address
+ * @return lwIP error definitions
+ */
+err_t
+mcast_block_netif(struct ip_mc *ipmc, struct netif *netif, const ip_addr_t *multi_addr, const ip_addr_t *blk_addr)
+{
+#if LWIP_IPV4 && LWIP_IGMP
+  if (IP_IS_V4(multi_addr)) {
+    struct ip4_mc *mc;
+    struct igmp_src *src;
+    
+    mc = mcast_ip4_mc_find(ipmc, netif, ip_2_ip4(multi_addr), NULL);
+    if (mc == NULL) {
+      return ERR_VAL;
+    }
+    if (mc->fmode != MCAST_EXCLUDE) { /* we must in exclude mode */
+      return ERR_VAL;
+    }
+    
+    src = mcast_ip4_mc_src_find(mc, ip_2_ip4(blk_addr), NULL);
+    if (src == NULL) {
+      src = (struct igmp_src *)mem_malloc(sizeof(struct igmp_src));
+      if (src == NULL) {
+        return ERR_MEM;
+      }
+      ip4_addr_set(&src->src_addr, ip_2_ip4(blk_addr));
+      src->next = mc->src;
+      mc->src = src;
+      IP4_MC_TRIGGER_CALL(netif, ip_2_ip4(multi_addr)); /* trigger a report */
+    }
+  } else
+#endif /* LWIP_IPV4 && LWIP_IGMP */
+  {
+#if LWIP_IPV6 && LWIP_IPV6_MLD
+    struct ip6_mc *mc;
+    struct mld6_src *src;
+    
+    mc = mcast_ip6_mc_find(ipmc, netif, ip_2_ip6(multi_addr), NULL);
+    if (mc == NULL) {
+      return ERR_VAL;
+    }
+    if (mc->fmode != MCAST_EXCLUDE) { /* we must in exclude mode */
+      return ERR_VAL;
+    }
+    
+    src = mcast_ip6_mc_src_find(mc, ip_2_ip6(blk_addr), NULL);
+    if (src == NULL) {
+      src = (struct mld6_src *)mem_malloc(sizeof(struct mld6_src));
+      if (src == NULL) {
+        return ERR_MEM;
+      }
+      ip6_addr_set(&src->src_addr, ip_2_ip6(blk_addr));
+      src->next = mc->src;
+      mc->src = src;
+      IP6_MC_TRIGGER_CALL(netif, ip_2_ip6(multi_addr)); /* trigger a report */
+    }
+#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
+  }
+  return ERR_OK;
+}
+
+/** Add a block source address to a multicast group
+ *
+ * @param ipmc multicast filter control block
+ * @param if_addr the network interface address.
+ * @param multi_addr the address of the group to add source
+ * @param blk_addr block multicast source address
+ * @return lwIP error definitions
+ */
+err_t
+mcast_block_group(struct ip_mc *ipmc, const ip_addr_t *if_addr, const ip_addr_t *multi_addr, const ip_addr_t *blk_addr)
+{
+  err_t err = ERR_VAL; /* no matching interface */
+  
+#if LWIP_IPV4 && LWIP_IGMP
+  if (IP_IS_V4(multi_addr)) {
+    struct netif *netif;
+    
+    NETIF_FOREACH(netif) {
+      if ((netif->flags & NETIF_FLAG_IGMP) && ((ip4_addr_isany(ip_2_ip4(if_addr)) || ip4_addr_cmp(netif_ip4_addr(netif), ip_2_ip4(if_addr))))) {
+        err = mcast_block_netif(ipmc, netif, multi_addr, blk_addr);
+        if (err != ERR_OK) {
+          return (err);
+        }
+      }
+    }
+  } else 
+#endif /* LWIP_IPV4 && LWIP_IGMP */
+  {
+#if LWIP_IPV6 && LWIP_IPV6_MLD
+    struct netif *netif;
+    
+    NETIF_FOREACH(netif) {
+      if (ip6_addr_isany(ip_2_ip6(if_addr)) ||
+          netif_get_ip6_addr_match(netif, ip_2_ip6(if_addr)) >= 0) {
+        err = mcast_block_netif(ipmc, netif, multi_addr, blk_addr);
+        if (err != ERR_OK) {
+          return (err);
+        }
+      }
+    }
+#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
+  }
+  return err;
+}
+
+/** Remove a block source address from a multicast group
+ *
+ * @param ipmc multicast filter control block
+ * @param netif the network interface which group we already join.
+ * @param multi_addr the address of the group to add source
+ * @param unblk_addr unblock multicast source address
+ * @return lwIP error definitions
+ */
+err_t
+mcast_unblock_netif(struct ip_mc *ipmc, struct netif *netif, const ip_addr_t *multi_addr, const ip_addr_t *unblk_addr)
+{
+#if LWIP_IPV4 && LWIP_IGMP
+  if (IP_IS_V4(multi_addr)) {
+    struct ip4_mc *mc;
+    struct igmp_src *src_prev;
+    struct igmp_src *src;
+    
+    mc = mcast_ip4_mc_find(ipmc, netif, ip_2_ip4(multi_addr), NULL);
+    if (mc == NULL) {
+      return ERR_VAL;
+    }
+    if (mc->fmode != MCAST_EXCLUDE) { /* we must in exclude mode */
+      return ERR_VAL;
+    }
+    
+    src = mcast_ip4_mc_src_find(mc, ip_2_ip4(unblk_addr), &src_prev);
+    if (src == NULL) {
+      return ERR_VAL;
+    }
+    if (src_prev) {
+      src_prev->next = src->next;
+    } else {
+      mc->src = src->next;
+    }
+    mem_free(src);
+    IP4_MC_TRIGGER_CALL(netif, ip_2_ip4(multi_addr)); /* trigger a report */
+  } else
+#endif /* LWIP_IPV4 && LWIP_IGMP */
+  {
+#if LWIP_IPV6 && LWIP_IPV6_MLD
+    struct ip6_mc *mc;
+    struct mld6_src *src_prev;
+    struct mld6_src *src;
+    
+    mc = mcast_ip6_mc_find(ipmc, netif, ip_2_ip6(multi_addr), NULL);
+    if (mc == NULL) {
+      return ERR_VAL;
+    }
+    if (mc->fmode != MCAST_EXCLUDE) { /* we must in exclude mode */
+      return ERR_VAL;
+    }
+    
+    src = mcast_ip6_mc_src_find(mc, ip_2_ip6(unblk_addr), &src_prev);
+    if (src == NULL) {
+      return ERR_VAL;
+    }
+    if (src_prev) {
+      src_prev->next = src->next;
+    } else {
+      mc->src = src->next;
+    }
+    mem_free(src);
+    IP6_MC_TRIGGER_CALL(netif, ip_2_ip6(multi_addr)); /* trigger a report */
+#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
+  }
+  return ERR_OK;
+}
+
+/** Remove a block source address from a multicast group
+ *
+ * @param ipmc multicast filter control block
+ * @param if_addr the network interface address.
+ * @param multi_addr the address of the group to add source
+ * @param unblk_addr unblock multicast source address
+ * @return lwIP error definitions
+ */
+err_t
+mcast_unblock_group(struct ip_mc *ipmc, const ip_addr_t *if_addr, const ip_addr_t *multi_addr, const ip_addr_t *unblk_addr)
+{
+  err_t res, err = ERR_VAL; /* no matching interface */
+
+#if LWIP_IPV4 && LWIP_IGMP
+  if (IP_IS_V4(multi_addr)) {
+    struct netif *netif;
+    
+    NETIF_FOREACH(netif) {
+      if ((netif->flags & NETIF_FLAG_IGMP) && ((ip4_addr_isany(ip_2_ip4(if_addr)) || ip4_addr_cmp(netif_ip4_addr(netif), ip_2_ip4(if_addr))))) {
+        res = mcast_unblock_netif(ipmc, netif, multi_addr, unblk_addr);
+        if (err != ERR_OK) {
+          err = res;
+        }
+      }
+    }
+  } else 
+#endif /* LWIP_IPV4 && LWIP_IGMP */
+  {
+#if LWIP_IPV6 && LWIP_IPV6_MLD
+    struct netif *netif;
+    
+    NETIF_FOREACH(netif) {
+      if (ip6_addr_isany(ip_2_ip6(if_addr)) ||
+          netif_get_ip6_addr_match(netif, ip_2_ip6(if_addr)) >= 0) {
+        res = mcast_unblock_netif(ipmc, netif, multi_addr, unblk_addr);
+        if (err != ERR_OK) {
+          err = res;
+        }
+      }
+    }
+#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
+  }
+  return err;
+}
+
+#if LWIP_SOCKET
+/** Set filter to a multicast group (ip_msfilter)
+ *
+ * @param ipmc multicast filter control block
+ * @param netif the network interface which group we already join.
+ * @param multi_addr the address of the group to add source
+ * @param imsf filter
+ * @return lwIP error definitions
+ */
+err_t
+mcast_set_msfilter_netif(struct ip_mc *ipmc, struct netif *netif, const ip_addr_t *multi_addr, const struct ip_msfilter *imsf)
+{
+#if LWIP_IPV4 && LWIP_IGMP
+  if (IP_IS_V4(multi_addr)) {
+    struct ip4_mc *mc;
+    struct igmp_src *src, *head = NULL;
+    u32_t i;
+    
+    mc = mcast_ip4_mc_find(ipmc, netif, ip_2_ip4(multi_addr), NULL);
+    if (mc == NULL) {
+      return ERR_VAL;
+    }
+    
+    for (i = 0; i < imsf->imsf_numsrc; i++) {
+      src = (struct igmp_src *)mem_malloc(sizeof(struct igmp_src));
+      if (src == NULL) {
+        mcast_ip4_mc_src_remove(head);
+        return ERR_MEM;
+      }
+      inet_addr_to_ip4addr(&src->src_addr, &imsf->imsf_slist[i]);
+      src->next = head;
+      head = src;
+    }
+    
+    mc->fmode = (u8_t)imsf->imsf_fmode;
+    mcast_ip4_mc_src_remove(mc->src);
+    mc->src = head;
+    IP4_MC_TRIGGER_CALL(netif, ip_2_ip4(multi_addr)); /* trigger a report */
+  } else
+#endif /* LWIP_IPV4 && LWIP_IGMP */
+  {
+#if LWIP_IPV6 && LWIP_IPV6_MLD
+    return EADDRNOTAVAIL;
+#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
+  }
+  return ERR_OK;
+}
+
+/** Set filter to a multicast group (group_filter)
+ *
+ * @param ipmc multicast filter control block
+ * @param netif the network interface which group we already join.
+ * @param multi_addr the address of the group to add source
+ * @param gf filter
+ * @return lwIP error definitions
+ */
+err_t
+mcast_set_groupfilter_netif(struct ip_mc *ipmc, struct netif *netif, const ip_addr_t *multi_addr, const struct group_filter *gf)
+{
+#if LWIP_IPV4 && LWIP_IGMP
+  if (IP_IS_V4(multi_addr)) {
+    struct ip4_mc *mc;
+    struct igmp_src *src, *head = NULL;
+    u32_t i;
+    
+    mc = mcast_ip4_mc_find(ipmc, netif, ip_2_ip4(multi_addr), NULL);
+    if (mc == NULL) {
+      return ERR_VAL;
+    }
+    
+    for (i = 0; i < gf->gf_numsrc; i++) {
+      src = (struct igmp_src *)mem_malloc(sizeof(struct igmp_src));
+      if (src == NULL) {
+        mcast_ip4_mc_src_remove(head);
+        return ERR_MEM;
+      }
+      inet_addr_to_ip4addr(&src->src_addr, &(((struct sockaddr_in *)&(gf->gf_slist[i]))->sin_addr));
+      src->next = head;
+      head = src;
+    }
+    
+    mc->fmode = (u8_t)gf->gf_fmode;
+    mcast_ip4_mc_src_remove(mc->src);
+    mc->src = head;
+    IP4_MC_TRIGGER_CALL(netif, ip_2_ip4(multi_addr)); /* trigger a report */
+  } else
+#endif /* LWIP_IPV4 && LWIP_IGMP */
+  {
+#if LWIP_IPV6 && LWIP_IPV6_MLD
+    struct ip6_mc *mc;
+    struct mld6_src *src, *head = NULL;
+    u32_t i;
+    
+    mc = mcast_ip6_mc_find(ipmc, netif, ip_2_ip6(multi_addr), NULL);
+    if (mc == NULL) {
+      return ERR_VAL;
+    }
+    
+    for (i = 0; i < gf->gf_numsrc; i++) {
+      src = (struct mld6_src *)mem_malloc(sizeof(struct mld6_src));
+      if (src == NULL) {
+        mcast_ip6_mc_src_remove(head);
+        return ERR_MEM;
+      }
+      inet6_addr_to_ip6addr(&src->src_addr, &(((struct sockaddr_in6 *)&(gf->gf_slist[i]))->sin6_addr));
+      src->next = head;
+      head = src;
+    }
+    
+    mc->fmode = (u8_t)gf->gf_fmode;
+    mcast_ip6_mc_src_remove(mc->src);
+    mc->src = head;
+    IP6_MC_TRIGGER_CALL(netif, ip_2_ip6(multi_addr)); /* trigger a report */
+#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
+  }
+  return ERR_OK;
+}
+
+/** Get filter from a multicast group (ip_msfilter)
+ *
+ * @param ipmc multicast filter control block
+ * @param netif the network interface which group we already join.
+ * @param multi_addr the address of the group to add source
+ * @param imsf filter
+ * @param bufsz filter buffer size
+ * @return lwIP error definitions
+ */
+err_t
+mcast_get_msfilter_netif(struct ip_mc *ipmc, struct netif *netif, const ip_addr_t *multi_addr, struct ip_msfilter *imsf, socklen_t *bufsz)
+{
+#if LWIP_IPV4 && LWIP_IGMP
+  if (IP_IS_V4(multi_addr)) {
+    struct ip4_mc *mc;
+    struct igmp_src *src;
+    u32_t i, cnt;
+    
+    mc = mcast_ip4_mc_find(ipmc, netif, ip_2_ip4(multi_addr), NULL);
+    if (mc == NULL) {
+      return ERR_VAL;
+    }
+    
+    cnt = 0;
+    src = mc->src;
+    while (src) {
+      cnt++;
+      src = src->next;
+    }
+    if (IP_MSFILTER_SIZE(cnt) > *bufsz) {
+      return ERR_BUF;
+    }
+    *bufsz = IP_MSFILTER_SIZE(cnt);
+    
+    imsf->imsf_numsrc = cnt;
+    src = mc->src;
+    for (i = 0; i < cnt; i++) {
+      inet_addr_from_ip4addr(&imsf->imsf_slist[i], &src->src_addr);
+      src = src->next;
+    }
+    imsf->imsf_fmode = mc->fmode;
+  } else
+#endif /* LWIP_IPV4 && LWIP_IGMP */
+  {
+#if LWIP_IPV6 && LWIP_IPV6_MLD
+    return ERR_VAL;
+#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
+  }
+  return ERR_OK;
+}
+
+/** Get filter from a multicast group (group_filter)
+ *
+ * @param ipmc multicast filter control block
+ * @param netif the network interface which group we already join.
+ * @param multi_addr the address of the group to add source
+ * @param gf filter
+ * @param bufsz filter buffer size
+ * @return lwIP error definitions
+ */
+err_t
+mcast_get_groupfilter_netif(struct ip_mc *ipmc, struct netif *netif, const ip_addr_t *multi_addr, struct group_filter *gf, socklen_t *bufsz)
+{
+#if LWIP_IPV4 && LWIP_IGMP
+  if (IP_IS_V4(multi_addr)) {
+    struct ip4_mc *mc;
+    struct igmp_src *src;
+    u32_t i, cnt;
+    
+    mc = mcast_ip4_mc_find(ipmc, netif, ip_2_ip4(multi_addr), NULL);
+    if (mc == NULL) {
+      return ERR_VAL;
+    }
+    
+    cnt = 0;
+    src = mc->src;
+    while (src) {
+      cnt++;
+      src = src->next;
+    }
+    if (GROUP_FILTER_SIZE(cnt) > *bufsz) {
+      return ERR_BUF;
+    }
+    *bufsz = GROUP_FILTER_SIZE(cnt);
+    
+    gf->gf_numsrc = cnt;
+    src = mc->src;
+    for (i = 0; i < cnt; i++) {
+      inet_addr_from_ip4addr(&(((struct sockaddr_in *)&(gf->gf_slist[i]))->sin_addr), &src->src_addr);
+      gf->gf_slist[i].ss_len = sizeof(struct sockaddr_in);
+      gf->gf_slist[i].ss_family = AF_INET;
+      src = src->next;
+    }
+    gf->gf_fmode = mc->fmode;
+  } else
+#endif /* LWIP_IPV4 && LWIP_IGMP */
+  {
+#if LWIP_IPV6 && LWIP_IPV6_MLD
+    struct ip6_mc *mc;
+    struct mld6_src *src;
+    u32_t i, cnt;
+    
+    mc = mcast_ip6_mc_find(ipmc, netif, ip_2_ip6(multi_addr), NULL);
+    if (mc == NULL) {
+      return ERR_VAL;
+    }
+    
+    cnt = 0;
+    src = mc->src;
+    while (src) {
+      cnt++;
+      src = src->next;
+    }
+    if (GROUP_FILTER_SIZE(cnt) > *bufsz) {
+      return ERR_BUF;
+    }
+    *bufsz = GROUP_FILTER_SIZE(cnt);
+    
+    gf->gf_numsrc = cnt;
+    src = mc->src;
+    for (i = 0; i < cnt; i++) {
+      inet6_addr_from_ip6addr(&(((struct sockaddr_in6 *)&(gf->gf_slist[i]))->sin6_addr), &src->src_addr);
+      gf->gf_slist[i].ss_len = sizeof(struct sockaddr_in6);
+      gf->gf_slist[i].ss_family = AF_INET6;
+      src = src->next;
+    }
+    gf->gf_fmode = mc->fmode;
+#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
+  }
+  return ERR_OK;
+}
+#endif /* LWIP_SOCKET */
+
+/** Remove multicast filter when pcb remove
+ *
+ * @param ipmc multicast filter control block
+ */
+void
+mcast_pcb_remove(struct ip_mc *ipmc)
+{
+#if LWIP_IPV4 && LWIP_IGMP
+  {
+    struct ip4_mc *mc, *next;
+    
+    mc = ipmc->mc4;
+    while (mc) {
+      next = mc->next;
+      mcast_ip4_mc_src_remove(mc->src);
+      igmp_leavegroup(&mc->if_addr, &mc->multi_addr);
+      mem_free(mc);
+      mc = next;
+    }
+  }
+#endif /* LWIP_IPV4 && LWIP_IGMP */
+
+#if LWIP_IPV6 && LWIP_IPV6_MLD
+  {
+    struct ip6_mc *mc, *next;
+    struct netif *netif;
+    
+    mc = ipmc->mc6;
+    while (mc) {
+      next = mc->next;
+      mcast_ip6_mc_src_remove(mc->src);
+      netif = netif_get_by_index(mc->if_idx);
+      if (netif) {
+        mld6_leavegroup_netif(netif, &mc->multi_addr);
+      }
+      mem_free(mc);
+      mc = next;
+    }
+  }
+#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
+}
+
+/** Common code to see if the current input multicast packet matches the pcb
+ * (current input packet is accessed via ip(4/6)_current_* macros)
+ *
+ * @param ipmc multicast filter control block
+ * @param inp network interface on which the datagram was received (only used for IPv4)
+ * @return 1 on match, 0 otherwise
+ */
+u8_t
+mcast_input_local_match(struct ip_mc *ipmc, struct netif *inp)
+{
+#if LWIP_IPV4 && LWIP_IGMP
+  if (!ip_current_is_v6()) {
+    struct igmp_src *src;
+    struct ip4_mc *mc;
+    ip4_addr_t *multi_addr = ip4_current_dest_addr();
+    ip4_addr_t *src_addr = ip4_current_src_addr();
+    
+    IP4_MC_FOREACH(ipmc, mc) {
+      if (ip4_addr_cmp(&mc->if_addr, netif_ip4_addr(inp)) && 
+          ip4_addr_cmp(&mc->multi_addr, multi_addr)) {
+        if (mc->fmode == MCAST_EXCLUDE) {
+          IP4_MC_SRC_FOREACH(mc, src) {
+            if (ip4_addr_cmp(&src->src_addr, src_addr)) {
+              return 0;
+            }
+          }
+          return 1; /* MCAST_EXCLUDE src_addr must not in src list */
+        
+        } else { /* MCAST_INCLUDE */
+          IP4_MC_SRC_FOREACH(mc, src) {
+            if (ip4_addr_cmp(&src->src_addr, src_addr)) {
+              return 1;
+            }
+          }
+          return 0; /* MCAST_INCLUDE src_addr must in src list */
+        }
+      }
+    }
+  } else 
+#endif /* LWIP_IPV4 && LWIP_IGMP */
+  {
+#if LWIP_IPV6 && LWIP_IPV6_MLD
+    struct mld6_src *src;
+    struct ip6_mc *mc;
+    ip6_addr_t *multi_addr = ip6_current_dest_addr();
+    ip6_addr_t *src_addr = ip6_current_src_addr();
+    
+    IP6_MC_FOREACH(ipmc, mc) {
+      if (((mc->if_idx == NETIF_NO_INDEX) || (mc->if_idx == netif_get_index(inp))) &&
+          ip6_addr_cmp_zoneless(&mc->multi_addr, multi_addr)) {
+        if (mc->fmode == MCAST_EXCLUDE) {
+          IP6_MC_SRC_FOREACH(mc, src) {
+            if (ip6_addr_cmp_zoneless(&src->src_addr, src_addr)) {
+              return 0;
+            }
+          }
+          return 1; /* MCAST_EXCLUDE src_addr must not in src list */
+          
+        } else { /* MCAST_INCLUDE */
+          IP6_MC_SRC_FOREACH(mc, src) {
+            if (ip6_addr_cmp_zoneless(&src->src_addr, src_addr)) {
+              return 1;
+            }
+          }
+          return 0; /* MCAST_INCLUDE src_addr must in src list */
+        }
+      }
+    }
+#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
+  }
+  return 0;
+}
+
+#if LWIP_SOCKET
+/**
+ * setsockopt() IP_ADD_MEMBERSHIP / IP_DROP_MEMBERSHIP command
+ */
+int
+mcast_sock_add_drop_membership(struct ip_mc *ipmc, int optname, const struct ip_mreq *imr)
+{
+  struct netif *netif;
+  ip_addr_t if_addr;
+  ip_addr_t multi_addr;
+  err_t err;
+  
+  inet_addr_to_ip4addr(ip_2_ip4(&if_addr), &imr->imr_interface);
+  inet_addr_to_ip4addr(ip_2_ip4(&multi_addr), &imr->imr_multiaddr);
+  IP_SET_TYPE_VAL(if_addr, IPADDR_TYPE_V4);
+  IP_SET_TYPE_VAL(multi_addr, IPADDR_TYPE_V4);
+  
+  if (!ip4_addr_ismulticast(ip_2_ip4(&multi_addr))) {
+    return EADDRNOTAVAIL;
+  }
+  
+  if (ip4_addr_isany(ip_2_ip4(&if_addr))) { /* To all network interface */
+    if (optname == IP_ADD_MEMBERSHIP) {
+      err = mcast_join_group(ipmc, &if_addr, &multi_addr, NULL);
+    } else {
+      err = mcast_leave_group(ipmc, &if_addr, &multi_addr, NULL);
+    }
+    
+  } else { /* To specified network interface */
+    if (!(ip_2_ip4(&if_addr)->addr & PP_HTONL(IP_CLASSA_NET))) { /* IS a BSD style index ? */
+      u8_t idx = (u8_t)PP_NTOHL(ip_2_ip4(&if_addr)->addr);
+      netif = netif_get_by_index(idx);
+      if (netif == NULL) {
+        return ENXIO;
+      }
+      *ip_2_ip4(&if_addr) = *netif_ip4_addr(netif);
+  
+    } else {
+      NETIF_FOREACH(netif) {
+        if (ip4_addr_cmp(ip_2_ip4(&if_addr), netif_ip4_addr(netif))) {
+          break;
+        }
+      }
+      if (netif == NULL) {
+        return ENXIO;
+      }
+    }
+  
+    if (optname == IP_ADD_MEMBERSHIP) {
+      err = mcast_join_netif(ipmc, netif, &multi_addr, NULL);
+    } else {
+      err = mcast_leave_netif(ipmc, netif, &multi_addr, NULL);
+    }
+  }
+  
+  return (u8_t)err_to_errno(err);
+}
+
+/**
+ * setsockopt() IP_ADD_SOURCE_MEMBERSHIP / IP_DROP_SOURCE_MEMBERSHIP command
+ */
+int
+mcast_sock_add_drop_source_membership(struct ip_mc *ipmc, int optname, const struct ip_mreq_source *imr)
+{
+  struct netif *netif;
+  ip_addr_t if_addr;
+  ip_addr_t multi_addr;
+  ip_addr_t src_addr;
+  err_t err;
+  
+  inet_addr_to_ip4addr(ip_2_ip4(&if_addr), &imr->imr_interface);
+  inet_addr_to_ip4addr(ip_2_ip4(&multi_addr), &imr->imr_multiaddr);
+  inet_addr_to_ip4addr(ip_2_ip4(&src_addr), &imr->imr_sourceaddr);
+  IP_SET_TYPE_VAL(if_addr, IPADDR_TYPE_V4);
+  IP_SET_TYPE_VAL(multi_addr, IPADDR_TYPE_V4);
+  IP_SET_TYPE_VAL(src_addr, IPADDR_TYPE_V4);
+  
+  if (!ip4_addr_ismulticast(ip_2_ip4(&multi_addr)) || ip4_addr_isany(ip_2_ip4(&src_addr))) {
+    return EADDRNOTAVAIL;
+  }
+  
+  if (ip4_addr_isany(ip_2_ip4(&if_addr))) { /* To all network interface */
+    if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
+      err = mcast_join_group(ipmc, &if_addr, &multi_addr, &src_addr);
+    } else {
+      err = mcast_leave_group(ipmc, &if_addr, &multi_addr, &src_addr);
+    }
+  
+  } else { /* To specified network interface */
+    if (!(ip_2_ip4(&if_addr)->addr & PP_HTONL(IP_CLASSA_NET))) { /* IS a BSD style index ? */
+      u8_t idx = (u8_t)PP_NTOHL(ip_2_ip4(&if_addr)->addr);
+      netif = netif_get_by_index(idx);
+      if (netif == NULL) {
+        return ENXIO;
+      }
+      *ip_2_ip4(&if_addr) = *netif_ip4_addr(netif);
+
+    } else {
+      NETIF_FOREACH(netif) {
+        if (ip4_addr_cmp(ip_2_ip4(&if_addr), netif_ip4_addr(netif))) {
+          break;
+        }
+      }
+      if (netif == NULL) {
+        return ENXIO;
+      }
+    }
+  
+    if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
+      err = mcast_join_netif(ipmc, netif, &multi_addr, &src_addr);
+    } else {
+      err = mcast_leave_netif(ipmc, netif, &multi_addr, &src_addr);
+    }
+  }
+  
+  return err_to_errno(err);
+}
+
+/**
+ * setsockopt() IP_BLOCK_SOURCE / IP_UNBLOCK_SOURCE command
+ */
+int
+mcast_sock_block_unblock_source(struct ip_mc *ipmc, int optname, const struct ip_mreq_source *imr)
+{
+  struct netif *netif;
+  ip_addr_t if_addr;
+  ip_addr_t multi_addr;
+  ip_addr_t blk_addr;
+  err_t err;
+  
+  inet_addr_to_ip4addr(ip_2_ip4(&if_addr), &imr->imr_interface);
+  inet_addr_to_ip4addr(ip_2_ip4(&multi_addr), &imr->imr_multiaddr);
+  inet_addr_to_ip4addr(ip_2_ip4(&blk_addr), &imr->imr_sourceaddr);
+  IP_SET_TYPE_VAL(if_addr, IPADDR_TYPE_V4);
+  IP_SET_TYPE_VAL(multi_addr, IPADDR_TYPE_V4);
+  IP_SET_TYPE_VAL(blk_addr, IPADDR_TYPE_V4);
+  
+  if (!ip4_addr_ismulticast(ip_2_ip4(&multi_addr)) || ip4_addr_isany(ip_2_ip4(&blk_addr))) {
+    return EADDRNOTAVAIL;
+  }
+  
+  if (ip4_addr_isany(ip_2_ip4(&if_addr))) { /* To all network interface */
+    if (optname == IP_BLOCK_SOURCE) {
+      err = mcast_block_group(ipmc, &if_addr, &multi_addr, &blk_addr);
+    } else {
+      err = mcast_unblock_group(ipmc, &if_addr, &multi_addr, &blk_addr);
+    }
+    
+  } else { /* To specified network interface */
+    if (!(ip_2_ip4(&if_addr)->addr & PP_HTONL(IP_CLASSA_NET))) { /* IS a BSD style index ? */
+      u8_t idx = (u8_t)PP_NTOHL(ip_2_ip4(&if_addr)->addr);
+      netif = netif_get_by_index(idx);
+      if (netif == NULL) {
+        return ENXIO;
+      }
+      *ip_2_ip4(&if_addr) = *netif_ip4_addr(netif);
+  
+    } else {
+      NETIF_FOREACH(netif) {
+        if (ip4_addr_cmp(ip_2_ip4(&if_addr), netif_ip4_addr(netif))) {
+          break;
+        }
+      }
+      if (netif == NULL) {
+        return ENXIO;
+      }
+    }
+    
+    if (optname == IP_BLOCK_SOURCE) {
+      err = mcast_block_netif(ipmc, netif, &multi_addr, &blk_addr);
+    } else {
+      err = mcast_unblock_netif(ipmc, netif, &multi_addr, &blk_addr);
+    }
+  }
+  
+  return err_to_errno(err);
+}
+
+/**
+ * setsockopt() IP_MSFILTER command
+ */
+int
+mcast_sock_set_msfilter(struct ip_mc *ipmc, int optname, const struct ip_msfilter *imsf)
+{
+  struct netif *netif;
+  ip_addr_t if_addr;
+  ip_addr_t multi_addr;
+  
+  inet_addr_to_ip4addr(ip_2_ip4(&if_addr), &imsf->imsf_interface);
+  inet_addr_to_ip4addr(ip_2_ip4(&multi_addr), &imsf->imsf_multiaddr);
+  IP_SET_TYPE_VAL(if_addr, IPADDR_TYPE_V4);
+  IP_SET_TYPE_VAL(multi_addr, IPADDR_TYPE_V4);
+  
+  if (!ip4_addr_ismulticast(ip_2_ip4(&multi_addr))) {
+    return EADDRNOTAVAIL;
+  }
+  
+  if (!(ip_2_ip4(&if_addr)->addr & PP_HTONL(IP_CLASSA_NET))) { /* IS a BSD style index ? */
+    u8_t idx = (u8_t)PP_NTOHL(ip_2_ip4(&if_addr)->addr);
+    netif = netif_get_by_index(idx);
+    if (netif == NULL) {
+      return ENXIO;
+    }
+    *ip_2_ip4(&if_addr) = *netif_ip4_addr(netif);
+  
+  } else {
+    NETIF_FOREACH(netif) {
+      if (ip4_addr_cmp(ip_2_ip4(&if_addr), netif_ip4_addr(netif))) {
+        break;
+      }
+    }
+    if (netif == NULL) {
+      return ENXIO;
+    }
+  }
+  
+  return err_to_errno(mcast_set_msfilter_netif(ipmc, netif, &multi_addr, imsf));
+}
+
+/**
+ * getsockopt() IP_MSFILTER command
+ */
+int
+mcast_sock_get_msfilter(struct ip_mc *ipmc, int optname, struct ip_msfilter *imsf, socklen_t *size)
+{
+  struct netif *netif;
+  ip_addr_t if_addr;
+  ip_addr_t multi_addr;
+  
+  inet_addr_to_ip4addr(ip_2_ip4(&if_addr), &imsf->imsf_interface);
+  inet_addr_to_ip4addr(ip_2_ip4(&multi_addr), &imsf->imsf_multiaddr);
+  IP_SET_TYPE_VAL(if_addr, IPADDR_TYPE_V4);
+  IP_SET_TYPE_VAL(multi_addr, IPADDR_TYPE_V4);
+  
+  if (!ip4_addr_ismulticast(ip_2_ip4(&multi_addr))) {
+    return EADDRNOTAVAIL;
+  }
+  
+  if (!(ip_2_ip4(&if_addr)->addr & PP_HTONL(IP_CLASSA_NET))) { /* IS a BSD style index ? */
+    u8_t idx = (u8_t)PP_NTOHL(ip_2_ip4(&if_addr)->addr);
+    netif = netif_get_by_index(idx);
+    if (netif == NULL) {
+      return ENXIO;
+    }
+    *ip_2_ip4(&if_addr) = *netif_ip4_addr(netif);
+  
+  } else {
+    NETIF_FOREACH(netif) {
+      if (ip4_addr_cmp(ip_2_ip4(&if_addr), netif_ip4_addr(netif))) {
+        break;
+      }
+    }
+    if (netif == NULL) {
+      return ENXIO;
+    }
+  }
+  
+  return err_to_errno(mcast_get_msfilter_netif(ipmc, netif, &multi_addr, imsf, size));
+}
+
+/**
+ * setsockopt() MCAST_JOIN_GROUP / MCAST_LEAVE_GROUP command
+ */
+int
+mcast_sock_join_leave_group(struct ip_mc *ipmc, int optname, const struct group_req *gr)
+{
+  struct netif *netif;
+  ip_addr_t multi_addr;
+  err_t err;
+  
+  if (gr->gr_group.ss_family == AF_INET) {
+    inet_addr_to_ip4addr(ip_2_ip4(&multi_addr), &(((struct sockaddr_in *)&(gr->gr_group))->sin_addr));
+    IP_SET_TYPE_VAL(multi_addr, IPADDR_TYPE_V4);
+    if (!ip4_addr_ismulticast(ip_2_ip4(&multi_addr))) {
+      return EADDRNOTAVAIL;
+    }
+  
+  } else if (gr->gr_group.ss_family == AF_INET6) {
+    inet6_addr_to_ip6addr(ip_2_ip6(&multi_addr), &(((struct sockaddr_in6 *)&(gr->gr_group))->sin6_addr));
+    IP_SET_TYPE_VAL(multi_addr, IPADDR_TYPE_V6);
+    if (!ip6_addr_ismulticast(ip_2_ip6(&multi_addr))) {
+      return EADDRNOTAVAIL;
+    }
+
+  } else {
+    return EADDRNOTAVAIL;
+  }
+
+  if (gr->gr_interface) {
+    netif = netif_get_by_index((u8_t)gr->gr_interface);
+  } else {
+    netif = netif_default; /* To default network interface */
+  }
+  if (netif == NULL) {
+    return ENXIO;
+  }
+  
+  if (optname == MCAST_JOIN_GROUP) {
+    err = mcast_join_netif(ipmc, netif, &multi_addr, NULL);
+  } else {
+    err = mcast_leave_netif(ipmc, netif, &multi_addr, NULL);
+  }
+  
+  return err_to_errno(err);
+}
+
+/**
+ * setsockopt() MCAST_JOIN_SOURCE_GROUP / MCAST_LEAVE_SOURCE_GROUP command
+ */
+int
+mcast_sock_join_leave_source_group(struct ip_mc *ipmc, int optname, const struct group_source_req *gsr)
+{
+  struct netif *netif;
+  ip_addr_t multi_addr;
+  ip_addr_t src_addr;
+  err_t err;
+  
+  if (gsr->gsr_group.ss_family == AF_INET) {
+    inet_addr_to_ip4addr(ip_2_ip4(&multi_addr), &(((struct sockaddr_in *)&(gsr->gsr_group))->sin_addr));
+    inet_addr_to_ip4addr(ip_2_ip4(&src_addr), &(((struct sockaddr_in *)&(gsr->gsr_source))->sin_addr));
+    IP_SET_TYPE_VAL(multi_addr, IPADDR_TYPE_V4);
+    IP_SET_TYPE_VAL(src_addr, IPADDR_TYPE_V4);
+    if (!ip4_addr_ismulticast(ip_2_ip4(&multi_addr)) || ip4_addr_isany(ip_2_ip4(&src_addr))) {
+      return EADDRNOTAVAIL;
+    }
+  
+  } else if (gsr->gsr_group.ss_family == AF_INET6) {
+    inet6_addr_to_ip6addr(ip_2_ip6(&multi_addr), &(((struct sockaddr_in6 *)&(gsr->gsr_group))->sin6_addr));
+    inet6_addr_to_ip6addr(ip_2_ip6(&src_addr), &(((struct sockaddr_in6 *)&(gsr->gsr_source))->sin6_addr));
+    IP_SET_TYPE_VAL(multi_addr, IPADDR_TYPE_V6);
+    IP_SET_TYPE_VAL(src_addr, IPADDR_TYPE_V6);
+    if (!ip6_addr_ismulticast(ip_2_ip6(&multi_addr)) || ip6_addr_isany(ip_2_ip6(&src_addr))) {
+      return EADDRNOTAVAIL;
+    }
+  
+  } else {
+    return EADDRNOTAVAIL;
+  }
+  
+  if (gsr->gsr_interface) {
+    netif = netif_get_by_index((u8_t)gsr->gsr_interface);
+  } else {
+    netif = netif_default; /* To default network interface */
+  }
+  if (netif == NULL) {
+    return ENXIO;
+  }
+  
+  if (optname == MCAST_JOIN_SOURCE_GROUP) {
+    err = mcast_join_netif(ipmc, netif, &multi_addr, &src_addr);
+  } else {
+    err = mcast_leave_netif(ipmc, netif, &multi_addr, &src_addr);
+  }
+  
+  return err_to_errno(err);
+}
+
+/**
+ * setsockopt() MCAST_BLOCK_SOURCE / MCAST_UNBLOCK_SOURCE command
+ */
+int
+mcast_sock_block_unblock_source_group(struct ip_mc *ipmc, int optname, const struct group_source_req *gsr)
+{
+  struct netif *netif;
+  ip_addr_t multi_addr;
+  ip_addr_t blk_addr;
+  err_t err;
+  
+  if (gsr->gsr_group.ss_family == AF_INET) {
+    inet_addr_to_ip4addr(ip_2_ip4(&multi_addr), &(((struct sockaddr_in *)&(gsr->gsr_group))->sin_addr));
+    inet_addr_to_ip4addr(ip_2_ip4(&blk_addr), &(((struct sockaddr_in *)&(gsr->gsr_source))->sin_addr));
+    IP_SET_TYPE_VAL(multi_addr, IPADDR_TYPE_V4);
+    IP_SET_TYPE_VAL(blk_addr, IPADDR_TYPE_V4);
+    if (!ip4_addr_ismulticast(ip_2_ip4(&multi_addr)) || ip4_addr_isany(ip_2_ip4(&blk_addr))) {
+      return EADDRNOTAVAIL;
+    }
+  
+  } else if (gsr->gsr_group.ss_family == AF_INET6) {
+    inet6_addr_to_ip6addr(ip_2_ip6(&multi_addr), &(((struct sockaddr_in6 *)&(gsr->gsr_group))->sin6_addr));
+    inet6_addr_to_ip6addr(ip_2_ip6(&blk_addr), &(((struct sockaddr_in6 *)&(gsr->gsr_source))->sin6_addr));
+    IP_SET_TYPE_VAL(multi_addr, IPADDR_TYPE_V6);
+    IP_SET_TYPE_VAL(blk_addr, IPADDR_TYPE_V6);
+    if (!ip6_addr_ismulticast(ip_2_ip6(&multi_addr)) || ip6_addr_isany(ip_2_ip6(&blk_addr))) {
+      return EADDRNOTAVAIL;
+    }
+  
+  } else {
+    return EADDRNOTAVAIL;
+  }
+  
+  if (gsr->gsr_interface) {
+    netif = netif_get_by_index((u8_t)gsr->gsr_interface);
+  } else {
+    netif = netif_default; /* To default network interface */
+  }
+  if (netif == NULL) {
+    return ENXIO;
+  }
+  
+  if (optname == MCAST_BLOCK_SOURCE) {
+    err = mcast_block_netif(ipmc, netif, &multi_addr, &blk_addr);
+  } else {
+    err = mcast_unblock_netif(ipmc, netif, &multi_addr, &blk_addr);
+  }
+  
+  return err_to_errno(err);
+}
+
+/**
+ * setsockopt() MCAST_MSFILTER command
+ */
+int
+mcast_sock_set_groupfilter(struct ip_mc *ipmc, int optname, const struct group_filter *gf)
+{
+  struct netif *netif;
+  ip_addr_t multi_addr;
+  
+  if (gf->gf_group.ss_family == AF_INET) {
+    inet_addr_to_ip4addr(ip_2_ip4(&multi_addr), &(((struct sockaddr_in *)&(gf->gf_group))->sin_addr));
+    IP_SET_TYPE_VAL(multi_addr, IPADDR_TYPE_V4);
+    if (!ip4_addr_ismulticast(ip_2_ip4(&multi_addr))) {
+      return EADDRNOTAVAIL;
+    }
+  
+  } else if (gf->gf_group.ss_family == AF_INET6) {
+    inet6_addr_to_ip6addr(ip_2_ip6(&multi_addr), &(((struct sockaddr_in6 *)&(gf->gf_group))->sin6_addr));
+    IP_SET_TYPE_VAL(multi_addr, IPADDR_TYPE_V6);
+    if (!ip6_addr_ismulticast(ip_2_ip6(&multi_addr))) {
+      return EADDRNOTAVAIL;
+    }
+  
+  } else {
+    return EADDRNOTAVAIL;
+  }
+  
+  if (gf->gf_interface) {
+    netif = netif_get_by_index((u8_t)gf->gf_interface);
+  } else {
+    netif = netif_default; /* To default network interface */
+  }
+  if (netif == NULL) {
+    return ENXIO;
+  }
+  
+  return err_to_errno(mcast_set_groupfilter_netif(ipmc, netif, &multi_addr, gf));
+}
+
+/**
+ * getsockopt() MCAST_MSFILTER command
+ */
+int
+mcast_sock_get_groupfilter(struct ip_mc *ipmc, int optname, struct group_filter *gf, socklen_t *size)
+{
+  struct netif *netif;
+  ip_addr_t multi_addr;
+  
+  if (gf->gf_group.ss_family == AF_INET) {
+    inet_addr_to_ip4addr(ip_2_ip4(&multi_addr), &(((struct sockaddr_in *)&(gf->gf_group))->sin_addr));
+    IP_SET_TYPE_VAL(multi_addr, IPADDR_TYPE_V4);
+    if (!ip4_addr_ismulticast(ip_2_ip4(&multi_addr))) {
+      return EADDRNOTAVAIL;
+    }
+  
+  } else if (gf->gf_group.ss_family == AF_INET6) {
+    inet6_addr_to_ip6addr(ip_2_ip6(&multi_addr), &(((struct sockaddr_in6 *)&(gf->gf_group))->sin6_addr));
+    IP_SET_TYPE_VAL(multi_addr, IPADDR_TYPE_V6);
+    if (!ip6_addr_ismulticast(ip_2_ip6(&multi_addr))) {
+      return EADDRNOTAVAIL;
+    }
+  
+  } else {
+    return EADDRNOTAVAIL;
+  }
+  
+  if (gf->gf_interface) {
+    netif = netif_get_by_index((u8_t)gf->gf_interface);
+  } else {
+    netif = netif_default; /* To default network interface */
+  }
+  if (netif == NULL) {
+    return ENXIO;
+  }
+  
+  return err_to_errno(mcast_get_groupfilter_netif(ipmc, netif, &multi_addr, gf, size));
+}
+
+/**
+ * setsockopt() IPV6_JOIN_GROUP / IPV6_LEAVE_GROUP command
+ */
+int
+mcast_sock_ipv6_add_drop_membership(struct ip_mc *ipmc, int optname, const struct ipv6_mreq *imr)
+{
+  struct netif *netif;
+  ip_addr_t multi_addr;
+  err_t err;
+  
+  inet6_addr_to_ip6addr(ip_2_ip6(&multi_addr), &imr->ipv6mr_multiaddr);
+  IP_SET_TYPE_VAL(multi_addr, IPADDR_TYPE_V6);
+  
+  if (!ip6_addr_ismulticast(ip_2_ip6(&multi_addr))) {
+    return EADDRNOTAVAIL;
+  }
+  
+  if (imr->ipv6mr_interface) {
+    netif = netif_get_by_index((u8_t)imr->ipv6mr_interface);
+  } else {
+    netif = netif_default; /* To default network interface */
+  }
+  if (netif == NULL) {
+    return ENXIO;
+  }
+  
+  if (optname == IPV6_JOIN_GROUP) {
+    err = mcast_join_netif(ipmc, netif, &multi_addr, NULL);
+  } else {
+    err = mcast_leave_netif(ipmc, netif, &multi_addr, NULL);
+  }
+  
+  return err_to_errno(err);
+}
+
+#endif /* LWIP_SOCKET */
+
+#endif /* (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD) */
+
+#endif /* LWIP_UDP || LWIP_RAW */
diff -Nur lwip/src/core/raw.c lwip-mcast/src/core/raw.c
--- lwip/src/core/raw.c	Fri Jan 26 13:32:36 2018
+++ lwip-mcast/src/core/raw.c	Sat Jan 27 16:55:18 2018
@@ -63,10 +63,10 @@
 #include <string.h>
 
 /** The list of RAW PCBs */
-static struct raw_pcb *raw_pcbs;
+struct raw_pcb *raw_pcbs;
 
 static u8_t
-raw_input_local_match(struct raw_pcb *pcb, u8_t broadcast)
+raw_input_local_match(struct raw_pcb *pcb, u8_t broadcast, struct netif *inp)
 {
   LWIP_UNUSED_ARG(broadcast); /* in IPv6 only case */
 
@@ -88,6 +88,12 @@
   }
 #endif /* LWIP_IPV4 && LWIP_IPV6 */
 
+#if (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD)
+  if (ip_addr_ismulticast(ip_current_dest_addr())) {
+    return mcast_input_local_match(&pcb->ipmc, inp);
+  }
+#endif /* (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD) */
+
   /* Only need to check PCB if incoming IP version matches PCB IP version */
   if (IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) {
 #if LWIP_IPV4
@@ -167,6 +173,17 @@
     if ((pcb->protocol == proto) && raw_input_local_match(pcb, broadcast) &&
         (((pcb->flags & RAW_FLAGS_CONNECTED) == 0) ||
          ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()))) {
+      if (pcb->min_ttl) {
+        if (ip_current_is_v6()) {
+          if (pcb->min_ttl > IP6H_HOPLIM(ip6_current_header())) {
+            continue;
+          }
+        } else {
+          if (pcb->min_ttl > IPH_TTL(ip4_current_header())) {
+            continue;
+          }
+        }
+      }
       /* receive callback function available? */
       if (pcb->recv != NULL) {
         u8_t eaten;
@@ -371,7 +388,26 @@
       /* For multicast-destined packets, use the user-provided interface index to
        * determine the outgoing interface, if an interface index is set and a
        * matching netif can be found. Otherwise, fall back to regular routing. */
-      netif = netif_get_by_index(pcb->mcast_ifindex);
+      if (pcb->mcast_ifindex != NETIF_NO_INDEX) {
+        netif = netif_get_by_index(pcb->mcast_ifindex);
+      }
+#if LWIP_IPV4
+      else
+#if LWIP_IPV6
+        if (IP_IS_V4(ipaddr))
+#endif /* LWIP_IPV6 */
+        {
+          /* IPv4 does not use source-based routing by default, so we use an
+             administratively selected interface for multicast by default.
+             However, this can be overridden by setting an interface address
+             in pcb->mcast_ip4 that is used for routing. If this routing lookup
+             fails, we try regular routing as though no override was set. */
+          if (!ip4_addr_isany_val(pcb->mcast_ip4) &&
+              !ip4_addr_cmp(&pcb->mcast_ip4, IP4_ADDR_BROADCAST)) {
+            netif = ip4_route_src(ip_2_ip4(&pcb->local_ip), &pcb->mcast_ip4);
+          }
+        }
+#endif /* LWIP_IPV4 */
     }
 
     if (netif == NULL)
@@ -449,6 +485,16 @@
     if (p->len < header_size) {
       return ERR_VAL;
     }
+#if CHECKSUM_GEN_IP
+    if (IP_IS_V4(dst_ip)) {
+      struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
+      if (IPH_CHKSUM(iphdr) == 0) {
+        IF__NETIF_CHECKSUM_ENABLED(netif, NETIF_CHECKSUM_GEN_IP) {
+          IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, IPH_HL(iphdr) << 2));
+        }
+      }
+    } 
+#endif /* CHECKSUM_GEN_IP */
     /* @todo multicast loop support, if at all desired for this scenario.. */
     NETIF_SET_HINTS(netif, &pcb->netif_hints);
     err = ip_output_if_hdrincl(p, src_ip, dst_ip, netif);
@@ -562,6 +608,11 @@
 {
   struct raw_pcb *pcb2;
   LWIP_ASSERT_CORE_LOCKED();
+
+#if (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD)
+  mcast_pcb_remove(&pcb->ipmc);
+#endif /* (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD) */
+
   /* pcb to be removed is first in list? */
   if (raw_pcbs == pcb) {
     /* make list start at 2nd pcb */
@@ -609,6 +660,9 @@
 #if LWIP_MULTICAST_TX_OPTIONS
     raw_set_multicast_ttl(pcb, RAW_TTL);
 #endif /* LWIP_MULTICAST_TX_OPTIONS */
+#if (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD)
+    pcb->ipmc.proto = IPPROTO_RAW;
+#endif /* (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD) */
     pcb->next = raw_pcbs;
     raw_pcbs = pcb;
   }
diff -Nur lwip/src/core/tcp_in.c lwip-mcast/src/core/tcp_in.c
--- lwip/src/core/tcp_in.c	Fri Jan 26 13:32:36 2018
+++ lwip-mcast/src/core/tcp_in.c	Sat Jan 27 16:56:21 2018
@@ -258,6 +258,17 @@
         pcb->local_port == tcphdr->dest &&
         ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()) &&
         ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) {
+      if (pcb->min_ttl) {
+        if (ip_current_is_v6()) {
+          if (pcb->min_ttl > IP6H_HOPLIM(ip6_current_header())) {
+            goto dropped;
+          }
+        } else {
+          if (pcb->min_ttl > IPH_TTL(ip4_current_header())) {
+            goto dropped;
+          }
+        }
+      }
       /* Move this PCB to the front of the list so that subsequent
          lookups will be faster (we exploit locality in TCP segment
          arrivals). */
diff -Nur lwip/src/core/udp.c lwip-mcast/src/core/udp.c
--- lwip/src/core/udp.c	Fri Jan 26 13:32:36 2018
+++ lwip-mcast/src/core/udp.c	Sat Jan 27 16:58:51 2018
@@ -148,6 +148,12 @@
     return 1;
   }
 
+#if (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD)
+  if (ip_addr_ismulticast(ip_current_dest_addr())) {
+    return mcast_input_local_match(&pcb->ipmc, inp);
+  }
+#endif /* (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD) */
+
   /* Only need to check PCB if incoming IP version matches PCB IP version */
   if (IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) {
 #if LWIP_IPV4
@@ -269,6 +275,21 @@
       if ((pcb->remote_port == src) &&
           (ip_addr_isany_val(pcb->remote_ip) ||
            ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()))) {
+        if (pcb->min_ttl) {
+          if (ip_current_is_v6()) {
+            if (pcb->min_ttl > IP6H_HOPLIM(ip6_current_header())) {
+              UDP_STATS_INC(udp.drop);
+              pbuf_free(p);
+              goto end;
+            }
+          } else {
+            if (pcb->min_ttl > IPH_TTL(ip4_current_header())) {
+              UDP_STATS_INC(udp.drop);
+              pbuf_free(p);
+              goto end;
+            }
+          }
+        }
         /* the first fully matching PCB */
         if (prev != NULL) {
           /* move the pcb to the front of udp_pcbs so that is
@@ -1138,6 +1159,10 @@
 
   LWIP_ASSERT_CORE_LOCKED();
 
+#if (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD)
+  mcast_pcb_remove(&pcb->ipmc);
+#endif /* (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD) */
+
   mib2_udp_unbind(pcb);
   /* pcb to be removed is first in list? */
   if (udp_pcbs == pcb) {
@@ -1187,6 +1212,9 @@
 #if LWIP_MULTICAST_TX_OPTIONS
     udp_set_multicast_ttl(pcb, UDP_TTL);
 #endif /* LWIP_MULTICAST_TX_OPTIONS */
+#if (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD)
+    pcb->ipmc.proto = IPPROTO_UDP;
+#endif /* (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD) */
   }
   return pcb;
 }
diff -Nur lwip/src/include/lwip/api.h lwip-mcast/src/include/lwip/api.h
--- lwip/src/include/lwip/api.h	Fri Jan 26 13:32:36 2018
+++ lwip-mcast/src/include/lwip/api.h	Sat Jan 27 17:03:22 2018
@@ -84,6 +84,8 @@
 #if LWIP_NETBUF_RECVINFO
 /** Received packet info will be recorded for this netconn */
 #define NETCONN_FLAG_PKTINFO                  0x40
+/** Received hoplim will be recorded for this netconn */
+#define NETCONN_FLAG_HOPLIM                   0x80
 #endif /* LWIP_NETBUF_RECVINFO */
 
 
diff -Nur lwip/src/include/lwip/igmp.h lwip-mcast/src/include/lwip/igmp.h
--- lwip/src/include/lwip/igmp.h	Fri Jan 26 13:32:36 2018
+++ lwip-mcast/src/include/lwip/igmp.h	Sat Jan 27 17:05:10 2018
@@ -51,6 +51,13 @@
 extern "C" {
 #endif
 
+/* Multicast filter support */
+struct igmp_src {
+  struct igmp_src *next;
+  /** the source address */
+  ip4_addr_t src_addr;
+};
+
 /* IGMP timer */
 #define IGMP_TMR_INTERVAL              100 /* Milliseconds */
 #define IGMP_V1_DELAYING_MEMBER_TMR   (1000/IGMP_TMR_INTERVAL)
@@ -60,6 +67,10 @@
 #define IGMP_DEL_MAC_FILTER            NETIF_DEL_MAC_FILTER
 #define IGMP_ADD_MAC_FILTER            NETIF_ADD_MAC_FILTER
 
+#if LWIP_IGMP_V3
+#define IGMP_FMODE_INIT                2 /* Not MCAST_INCLUDE or MCAST_EXCLUDE */
+#endif /* LWIP_IGMP_V3 */
+
 /**
  * igmp group structure - there is
  * a list of groups for each interface
@@ -82,6 +93,16 @@
   u8_t               group_state;
   /** timer for reporting, negative is OFF */
   u16_t              timer;
+#if LWIP_IGMP_V3
+  /** Last report fmode */
+  u8_t               v3_fmode;
+  /** signifies we were the last person to report */
+  u8_t               v3_last_reporter_flag;
+  /** current state of the group */
+  u8_t               v3_group_state;
+  /** timer for reporting, negative is OFF */
+  u16_t              v3_timer;
+#endif /* LWIP_IGMP_V3 */
   /** counter of simultaneous uses */
   u8_t               use;
 };
diff -Nur lwip/src/include/lwip/ip.h lwip-mcast/src/include/lwip/ip.h
--- lwip/src/include/lwip/ip.h	Fri Jan 26 13:32:36 2018
+++ lwip-mcast/src/include/lwip/ip.h	Sat Jan 27 17:06:02 2018
@@ -84,7 +84,9 @@
   /* Type Of Service */                    \
   u8_t tos;                                \
   /* Time To Live */                       \
-  u8_t ttl                                 \
+  u8_t ttl;                                \
+  /* min TTL */                            \
+  u8_t min_ttl                             \
   /* link layer address resolution hint */ \
   IP_PCB_NETIFHINT
 
diff -Nur lwip/src/include/lwip/mcast.h lwip-mcast/src/include/lwip/mcast.h
--- lwip/src/include/lwip/mcast.h	Thu Jan  1 08:00:00 1970
+++ lwip-mcast/src/include/lwip/mcast.h	Tue Jan 23 15:35:02 2018
@@ -0,0 +1,175 @@
+/**
+ * @file
+ * Multicast filter module\n
+ */
+
+/*
+ * Copyright (c) 2018 Han.hui
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ *    this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
+ * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
+ * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * This file is part of the lwIP TCP/IP stack.
+ *
+ * Author: Han.hui <hanhui@acoinfo.com>
+ *
+ */
+#ifndef LWIP_HDR_MCAST_H
+#define LWIP_HDR_MCAST_H
+
+#include "lwip/opt.h"
+
+#if LWIP_UDP || LWIP_RAW /* don't build if not configured for use in lwipopts.h */
+
+#include "lwip/def.h"
+#include "lwip/ip.h"
+#include "lwip/ip_addr.h"
+#include "lwip/ip6_addr.h"
+#include "lwip/netif.h"
+#include "lwip/igmp.h"
+#include "lwip/mld6.h"
+#include "lwip/sockets.h"
+#include "lwip/prot/ip4.h"
+#include "lwip/prot/ip6.h"
+
+#ifndef LWIP_MCAST_SRC_TBL_SIZE
+#define LWIP_MCAST_SRC_TBL_SIZE  32
+#endif /* !LWIP_MCAST_TABLE_SIZE */
+
+#if LWIP_IPV4 && LWIP_IGMP
+/** the IPv4 multicast filter */
+struct ip4_mc {
+  struct ip4_mc *next;
+  /** the interface address */
+  ip4_addr_t if_addr;
+  /** the group address */
+  ip4_addr_t multi_addr;
+  /** the source address list filter mode 0: EXCLUDE 1: INCLUDE */
+  u8_t fmode;
+  /** the source address list */
+  struct igmp_src *src;
+};
+
+/** The list of ip4_mc. */
+#define IP4_MC_FOREACH(ipmc, mc) \
+  for ((mc) = (ipmc)->mc4; (mc) != NULL; (mc) = (mc)->next)
+#define IP4_MC_SRC_FOREACH(mc, src) \
+  for ((src) = (mc)->src; (src) != NULL; (src) = (src)->next)
+#endif /* LWIP_IPV4 && LWIP_IGMP */
+
+#if LWIP_IPV6 && LWIP_IPV6_MLD
+/** the IPv6 multicast filter */
+struct ip6_mc {
+  struct ip6_mc *next;
+  /** the interface index */
+  u8_t if_idx;
+  /** the group address */
+  ip6_addr_t multi_addr;
+  /** the source address list filter mode 0: EXCLUDE 1: INCLUDE */
+  u8_t fmode;
+  /** the source address list */
+  struct mld6_src *src;
+};
+
+/** The list of ip6_mc. */
+#define IP6_MC_FOREACH(ipmc, mc) \
+  for ((mc) = (ipmc)->mc6; (mc) != NULL; (mc) = (mc)->next)
+#define IP6_MC_SRC_FOREACH(mc, src) \
+  for ((src) = (mc)->src; (src) != NULL; (src) = (src)->next)
+#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
+
+#if (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD)
+/* multicast filter control block */
+struct ip_mc {
+#if !LWIP_SOCKET
+#define IPPROTO_UDP 17
+#define IPPROTO_RAW 255
+#endif /* !LWIP_SOCKET */
+  u8_t proto; /* IPPROTO_UDP or IPPROTO_RAW */
+
+#if LWIP_IPV4 && LWIP_IGMP
+  struct ip4_mc *mc4;
+#endif /* LWIP_IPV4 && LWIP_IGMP */
+
+#if LWIP_IPV6 && LWIP_IPV6_MLD
+  struct ip6_mc *mc6;
+#endif /* LWIP_IPV6 && LWIP_IPV6_MLD */
+};
+
+#if LWIP_IPV4 && LWIP_IGMP && LWIP_IGMP_V3
+/* IGMPv3 must install a trigger function to report multicast filter changes */
+typedef void (*mcast_trigger_ip4_fn)(struct netif *netif, const ip4_addr_t *multi_addr);
+void    mcast_ip4_filter_trigger_callback(mcast_trigger_ip4_fn trigger_callback);
+/* IGMPv3 use the following function to get specified group of total multicast filter source address array */
+u16_t   mcast_ip4_filter_info(struct netif *netif, const ip4_addr_t *multi_addr, ip4_addr_p_t addr_array[], u16_t arr_cnt, u8_t *fmode);
+u8_t    mcast_ip4_filter_interest(struct netif *netif, const ip4_addr_t *multi_addr, const ip4_addr_p_t src_addr[], u16_t arr_cnt);
+#endif /* LWIP_IPV4 && LWIP_IGMP && LWIP_IGMP_V3 */
+
+#if LWIP_IPV6 && LWIP_IPV6_MLD && LWIP_IPV6_MLD_V2
+/* MLDv2 must install a trigger function to report multicast filter changes */
+typedef void (*mcast_trigger_ip6_fn)(struct netif *netif, const ip6_addr_t *multi_addr);
+void    mcast_ip6_filter_trigger_callback(mcast_trigger_ip6_fn trigger_callback);
+/* MLDv2 use the following function to get specified group of total multicast filter source address array */
+u16_t   mcast_ip6_filter_info(struct netif *netif, const ip6_addr_t *multi_addr, ip6_addr_p_t addr_array[], u16_t arr_cnt, u8_t *fmode);
+u8_t    mcast_ip6_filter_interest(struct netif *netif, const ip6_addr_t *multi_addr, const ip6_addr_p_t src_addr[], u16_t arr_cnt);
+#endif /* LWIP_IPV6 && LWIP_IPV6_MLD && LWIP_IPV6_MLD_V2 */
+
+/* UDP or RAW use the following functions */
+void    mcast_pcb_remove(struct ip_mc *ipmc);
+u8_t    mcast_input_local_match(struct ip_mc *ipmc, struct netif *inp);
+
+/* The following functions is for Non-Socket API user */
+err_t   mcast_join_netif(struct ip_mc *ipmc, struct netif *netif, const ip_addr_t *multi_addr, const ip_addr_t *src_addr);
+err_t   mcast_join_group(struct ip_mc *ipmc, const ip_addr_t *if_addr, const ip_addr_t *multi_addr, const ip_addr_t *src_addr);
+err_t   mcast_leave_netif(struct ip_mc *ipmc, struct netif *netif, const ip_addr_t *multi_addr, const ip_addr_t *src_addr);
+err_t   mcast_leave_group(struct ip_mc *ipmc, const ip_addr_t *if_addr, const ip_addr_t *multi_addr, const ip_addr_t *src_addr);
+err_t   mcast_block_netif(struct ip_mc *ipmc, struct netif *netif, const ip_addr_t *multi_addr, const ip_addr_t *blk_addr);
+err_t   mcast_block_group(struct ip_mc *ipmc, const ip_addr_t *if_addr, const ip_addr_t *multi_addr, const ip_addr_t *blk_addr);
+err_t   mcast_unblock_netif(struct ip_mc *ipmc, struct netif *netif, const ip_addr_t *multi_addr, const ip_addr_t *unblk_addr);
+err_t   mcast_unblock_group(struct ip_mc *ipmc, const ip_addr_t *if_addr, const ip_addr_t *multi_addr, const ip_addr_t *unblk_addr);
+
+/* The following functions is for socket API use */
+#if LWIP_SOCKET
+err_t   mcast_set_msfilter_netif(struct ip_mc *ipmc, struct netif *netif, const ip_addr_t *multi_addr, const struct ip_msfilter *imsf);
+err_t   mcast_set_groupfilter_netif(struct ip_mc *ipmc, struct netif *netif, const ip_addr_t *multi_addr, const struct group_filter *gf);
+err_t   mcast_get_msfilter_netif(struct ip_mc *ipmc, struct netif *netif, const ip_addr_t *multi_addr, struct ip_msfilter *imsf, socklen_t *bufsz);
+err_t   mcast_get_groupfilter_netif(struct ip_mc *ipmc, struct netif *netif, const ip_addr_t *multi_addr, struct group_filter *gf, socklen_t *bufsz);
+
+int     mcast_sock_add_drop_membership(struct ip_mc *ipmc, int optname, const struct ip_mreq *imr);
+int     mcast_sock_add_drop_source_membership(struct ip_mc *ipmc, int optname, const struct ip_mreq_source *imr);
+int     mcast_sock_block_unblock_source(struct ip_mc *ipmc, int optname, const struct ip_mreq_source *imr);
+int     mcast_sock_set_msfilter(struct ip_mc *ipmc, int optname, const struct ip_msfilter *imsf);
+int     mcast_sock_get_msfilter(struct ip_mc *ipmc, int optname, struct ip_msfilter *imsf, socklen_t *size);
+int     mcast_sock_join_leave_group(struct ip_mc *ipmc, int optname, const struct group_req *gr);
+int     mcast_sock_join_leave_source_group(struct ip_mc *ipmc, int optname, const struct group_source_req *gsr);
+int     mcast_sock_block_unblock_source_group(struct ip_mc *ipmc, int optname, const struct group_source_req *gsr);
+int     mcast_sock_set_groupfilter(struct ip_mc *ipmc, int optname, const struct group_filter *gf);
+int     mcast_sock_get_groupfilter(struct ip_mc *ipmc, int optname, struct group_filter *gf, socklen_t *size);
+int     mcast_sock_ipv6_add_drop_membership(struct ip_mc *ipmc, int optname, const struct ipv6_mreq *imr);
+#endif /* LWIP_SOCKET */
+
+#endif /* (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD) */
+
+#endif /* LWIP_UDP || LWIP_RAW */
+
+#endif /* LWIP_HDR_MCAST_H */
diff -Nur lwip/src/include/lwip/mld6.h lwip-mcast/src/include/lwip/mld6.h
--- lwip/src/include/lwip/mld6.h	Fri Jan 26 13:32:36 2018
+++ lwip-mcast/src/include/lwip/mld6.h	Sat Jan 27 17:07:13 2018
@@ -54,6 +54,13 @@
 extern "C" {
 #endif
 
+/* Multicast filter support */
+struct mld6_src {
+  struct mld6_src *next;
+  /** the source address */
+  ip6_addr_t src_addr;
+};
+
 /** MLD group */
 struct mld_group {
   /** next link */
diff -Nur lwip/src/include/lwip/netbuf.h lwip-mcast/src/include/lwip/netbuf.h
--- lwip/src/include/lwip/netbuf.h	Fri Jan 26 13:32:36 2018
+++ lwip-mcast/src/include/lwip/netbuf.h	Sat Jan 27 17:08:11 2018
@@ -55,6 +55,8 @@
 #define NETBUF_FLAG_DESTADDR    0x01
 /** This netbuf includes a checksum */
 #define NETBUF_FLAG_CHKSUM      0x02
+/** This netbuf has hoplim set */
+#define NETBUF_FLAG_HOPLIM      0x80
 
 /** "Network buffer" - contains data and addressing info */
 struct netbuf {
@@ -66,6 +68,9 @@
   u16_t toport_chksum;
 #if LWIP_NETBUF_RECVINFO
   ip_addr_t toaddr;
+#if LWIP_IPV6
+  u8_t hoplim;
+#endif /* LWIP_IPV6 */
 #endif /* LWIP_NETBUF_RECVINFO */
 #endif /* LWIP_NETBUF_RECVINFO || LWIP_CHECKSUM_ON_COPY */
 };
diff -Nur lwip/src/include/lwip/opt.h lwip-mcast/src/include/lwip/opt.h
--- lwip/src/include/lwip/opt.h	Fri Jan 26 13:32:36 2018
+++ lwip-mcast/src/include/lwip/opt.h	Sat Jan 27 17:09:46 2018
@@ -1063,10 +1063,16 @@
  */
 #if !defined LWIP_IGMP || defined __DOXYGEN__
 #define LWIP_IGMP                       0
+/**
+ * LWIP_IGMP_V3==1: Turn on IGMPv3 module.
+ */
+#define LWIP_IGMP_V3                    0
 #endif
 #if !LWIP_IPV4
 #undef LWIP_IGMP
 #define LWIP_IGMP                       0
+#undef LWIP_IGMP_V3
+#define LWIP_IGMP_V3                    0
 #endif
 /**
  * @}
@@ -2489,6 +2495,13 @@
  */
 #if !defined LWIP_IPV6_MLD || defined __DOXYGEN__
 #define LWIP_IPV6_MLD                   (LWIP_IPV6)
+#endif
+
+/**
+ * LWIP_IPV6_MLD_V2==1: Enable multicast listener discovery protocol v2.
+ */
+#if !defined LWIP_IPV6_MLD_V2 || defined __DOXYGEN__
+#define LWIP_IPV6_MLD_V2                0
 #endif
 
 /**
diff -Nur lwip/src/include/lwip/prot/igmp.h lwip-mcast/src/include/lwip/prot/igmp.h
--- lwip/src/include/lwip/prot/igmp.h	Fri Jan 26 13:32:36 2018
+++ lwip-mcast/src/include/lwip/prot/igmp.h	Sat Jan 27 17:01:29 2018
@@ -49,6 +49,7 @@
  */
 #define IGMP_TTL                       1
 #define IGMP_MINLEN                    8
+#define IGMP_V3_MINLEN                 12
 #define ROUTER_ALERT                   0x9404U
 #define ROUTER_ALERTLEN                4
 
@@ -59,6 +60,7 @@
 #define IGMP_V1_MEMB_REPORT            0x12 /* Ver. 1 membership report */
 #define IGMP_V2_MEMB_REPORT            0x16 /* Ver. 2 membership report */
 #define IGMP_LEAVE_GROUP               0x17 /* Leave-group message      */
+#define IGMP_V3_MEMB_REPORT            0x22 /* Ver. 3 membership report */
 
 /* Group  membership states */
 #define IGMP_GROUP_NON_MEMBER          0
@@ -82,6 +84,79 @@
 #ifdef PACK_STRUCT_USE_INCLUDES
 #  include "arch/epstruct.h"
 #endif
+
+#if LWIP_IGMP_V3 /* RFC 3367 */
+/**
+ * IGMPv3 query packet format.
+ */
+#ifdef PACK_STRUCT_USE_INCLUDES
+#  include "arch/bpstruct.h"
+#endif
+PACK_STRUCT_BEGIN
+struct igmp_v3_query {
+  PACK_STRUCT_FLD_8(u8_t         igmp_v3_msgtype);
+  PACK_STRUCT_FLD_8(u8_t         igmp_v3_maxresp);
+  PACK_STRUCT_FIELD(u16_t        igmp_v3_checksum);
+  PACK_STRUCT_FLD_S(ip4_addr_p_t igmp_v3_group_address);
+  PACK_STRUCT_FLD_8(u8_t         igmp_v3_sqrv);
+  PACK_STRUCT_FLD_8(u8_t         igmp_v3_qqic);
+  PACK_STRUCT_FIELD(u16_t        igmp_v3_srccnt);
+} PACK_STRUCT_STRUCT;
+PACK_STRUCT_END
+#ifdef PACK_STRUCT_USE_INCLUDES
+#  include "arch/epstruct.h"
+#endif
+
+#define IGMP_V3_QUERY_HLEN  sizeof(struct igmp_v3_query)
+
+/**
+ * IGMPv3 report packet header format.
+ */
+#ifdef PACK_STRUCT_USE_INCLUDES
+#  include "arch/bpstruct.h"
+#endif
+PACK_STRUCT_BEGIN
+struct igmp_v3_report {
+  PACK_STRUCT_FLD_8(u8_t         igmp_v3_msgtype);
+  PACK_STRUCT_FLD_8(u8_t         igmp_v3_reserve1);
+  PACK_STRUCT_FIELD(u16_t        igmp_v3_checksum);
+  PACK_STRUCT_FIELD(u16_t        igmp_v3_reserve2);
+  PACK_STRUCT_FIELD(u16_t        igmp_v3_reccnt);
+} PACK_STRUCT_STRUCT;
+PACK_STRUCT_END
+#ifdef PACK_STRUCT_USE_INCLUDES
+#  include "arch/epstruct.h"
+#endif
+
+#define IGMP_V3_REPORT_HLEN  sizeof(struct igmp_v3_report)
+
+#define IGMP_V3_REC_IS_IN   0x01    /* Type MODE_IS_INCLUDE */
+#define IGMP_V3_REC_IS_EX   0x02    /* Type MODE_IS_EXCLUDE */
+#define IGMP_V3_REC_TO_IN   0x03    /* Type CHANGE_TO_INCLUDE_MODE */
+#define IGMP_V3_REC_TO_EX   0x04    /* Type CHANGE_TO_EXCLUDE_MODE */
+#define IGMP_V3_REC_ALLOW   0x05    /* Type ALLOW_NEW_SOURCES */
+#define IGMP_V3_REC_BLOCK   0x06    /* Type BLOCK_OLD_SOURCES */
+
+/**
+ * IGMPv3 report packet group record format.
+ */
+#ifdef PACK_STRUCT_USE_INCLUDES
+#  include "arch/bpstruct.h"
+#endif
+PACK_STRUCT_BEGIN
+struct igmp_v3_record {
+  PACK_STRUCT_FLD_8(u8_t         igmp_v3_rc_type);
+  PACK_STRUCT_FLD_8(u8_t         igmp_v3_rc_auxlen);
+  PACK_STRUCT_FIELD(u16_t        igmp_v3_rc_srccnt);
+  PACK_STRUCT_FLD_S(ip4_addr_p_t igmp_v3_rc_group_address);
+} PACK_STRUCT_STRUCT;
+PACK_STRUCT_END
+#ifdef PACK_STRUCT_USE_INCLUDES
+#  include "arch/epstruct.h"
+#endif
+
+#define IGMP_V3_RECORD_LEN(src_cnt)  (sizeof(struct igmp_v3_record) + (src_cnt << 2))
+#endif /* LWIP_IGMP_V3 */
 
 #ifdef __cplusplus
 }
diff -Nur lwip/src/include/lwip/raw.h lwip-mcast/src/include/lwip/raw.h
--- lwip/src/include/lwip/raw.h	Fri Jan 26 13:32:36 2018
+++ lwip-mcast/src/include/lwip/raw.h	Sat Jan 27 17:11:28 2018
@@ -47,6 +47,7 @@
 #include "lwip/ip.h"
 #include "lwip/ip_addr.h"
 #include "lwip/ip6_addr.h"
+#include "lwip/mcast.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -82,12 +83,20 @@
   u8_t flags;
 
 #if LWIP_MULTICAST_TX_OPTIONS
+#if LWIP_IPV4
+  /** outgoing network interface for multicast packets, by IPv4 address (if not 'any') */
+  ip4_addr_t mcast_ip4;
+#endif /* LWIP_IPV4 */
   /** outgoing network interface for multicast packets, by interface index (if nonzero) */
   u8_t mcast_ifindex;
   /** TTL for outgoing multicast packets */
   u8_t mcast_ttl;
 #endif /* LWIP_MULTICAST_TX_OPTIONS */
 
+#if (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD)
+  struct ip_mc ipmc;
+#endif /* (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD) */
+
   /** receive callback function */
   raw_recv_fn recv;
   /* user-supplied argument for the recv callback */
@@ -98,6 +107,7 @@
   u8_t  chksum_reqd;
 #endif
 };
+extern struct raw_pcb *raw_pcbs;
 
 /* The following functions is the application layer interface to the
    RAW code. */
@@ -128,6 +138,10 @@
 #define raw_new_ip6(proto) raw_new_ip_type(IPADDR_TYPE_V6, proto)
 
 #if LWIP_MULTICAST_TX_OPTIONS
+#if LWIP_IPV4
+#define raw_set_multicast_netif_addr(pcb, ip4addr) ip4_addr_copy((pcb)->mcast_ip4, *(ip4addr))
+#define raw_get_multicast_netif_addr(pcb)          (&(pcb)->mcast_ip4)
+#endif /* LWIP_IPV4 */
 #define raw_set_multicast_netif_index(pcb, idx) ((pcb)->mcast_ifindex = (idx))
 #define raw_get_multicast_netif_index(pcb)      ((pcb)->mcast_ifindex)
 #define raw_set_multicast_ttl(pcb, value)       ((pcb)->mcast_ttl = (value))
diff -Nur lwip/src/include/lwip/sockets.h lwip-mcast/src/include/lwip/sockets.h
--- lwip/src/include/lwip/sockets.h	Fri Jan 26 13:32:36 2018
+++ lwip-mcast/src/include/lwip/sockets.h	Sat Jan 27 17:16:08 2018
@@ -269,6 +269,17 @@
 #define IP_TOS             1
 #define IP_TTL             2
 #define IP_PKTINFO         8
+#define IP_MINTTL          99
+#define IP_HDRINCL         100
+#define IP_OPTIONS         101
+
+#if LWIP_IPV6
+#define IPV6_RECVPKTINFO    49  /* Recv IPV6_PKTINFO message */
+#define IPV6_PKTINFO        50
+#define IPV6_RECVHOPLIMIT   51  /* Recv IPV6_HOPLIMIT message */
+#define IPV6_HOPLIMIT       52
+#define IPV6_MINHOPCNT      99
+#endif /* LWIP_IPV6 */
 
 #if LWIP_TCP
 /*
@@ -315,11 +326,78 @@
 #define IP_DROP_MEMBERSHIP 4
 
 typedef struct ip_mreq {
-    struct in_addr imr_multiaddr; /* IP multicast address of group */
-    struct in_addr imr_interface; /* local IP address of interface */
+  struct in_addr imr_multiaddr; /* IP multicast address of group */
+  struct in_addr imr_interface; /* local IP address of interface */
 } ip_mreq;
+
+/* IPv4 Source Filter Multicast API [RFC3678] */
+#define IP_UNBLOCK_SOURCE           37   /* unblock a source */
+#define IP_BLOCK_SOURCE             38   /* block a source */
+#define IP_ADD_SOURCE_MEMBERSHIP    39   /* join a source-specific group */
+#define IP_DROP_SOURCE_MEMBERSHIP   40   /* drop a single source */
+#define IP_MSFILTER                 41   /* set/get msfilter */
+/*
+ * Argument structure for IPv4 Multicast Source Filter APIs. [RFC3678]
+ */
+struct ip_mreq_source {
+  struct in_addr imr_multiaddr;   /* IP multicast address of group */
+  struct in_addr imr_sourceaddr;  /* IP address of source */
+  struct in_addr imr_interface;   /* local IP address of interface */
+};
+
+#define IP_MSFILTER_SIZE(numsrc) \
+  (sizeof(struct ip_msfilter) - sizeof(struct in_addr) \
+   + (numsrc) * sizeof(struct in_addr))
+
+struct ip_msfilter {
+  struct in_addr imsf_multiaddr;
+  struct in_addr imsf_interface;
+  u32_t          imsf_fmode;
+#define MCAST_EXCLUDE   0
+#define MCAST_INCLUDE   1
+  u32_t          imsf_numsrc;
+  struct in_addr imsf_slist[1];
+};
 #endif /* LWIP_IGMP */
 
+#if (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD)
+/* Protocol Independent Multicast API [RFC3678] */
+#define MCAST_JOIN_GROUP            42   /* join an any-source group */
+#define MCAST_BLOCK_SOURCE          43   /* block a source */
+#define MCAST_UNBLOCK_SOURCE        44   /* unblock a source */
+#define MCAST_LEAVE_GROUP           45   /* leave all sources for group */
+#define MCAST_JOIN_SOURCE_GROUP     46   /* join a source-specific group */
+#define MCAST_LEAVE_SOURCE_GROUP    47   /* leave a single source */
+#define MCAST_MSFILTER              48
+
+/*
+ * Argument structures for Protocol-Independent Multicast Source
+ * Filter APIs. [RFC3678]
+ */
+struct group_req {
+  u32_t                     gr_interface;   /* interface index */
+  struct sockaddr_storage   gr_group;       /* group address */
+};
+
+struct group_source_req {
+  u32_t                     gsr_interface;  /* interface index */
+  struct sockaddr_storage   gsr_group;      /* group address */
+  struct sockaddr_storage   gsr_source;     /* source address */
+};
+
+#define GROUP_FILTER_SIZE(numsrc) \
+    (sizeof(struct group_filter) - sizeof(struct sockaddr_storage) \
+     + (numsrc) * sizeof(struct sockaddr_storage))
+
+struct group_filter {
+    u32_t                   gf_interface;   /* interface index */
+    struct sockaddr_storage gf_group;       /* multicast address */
+    u32_t                   gf_fmode;       /* filter mode */
+    u32_t                   gf_numsrc;      /* number of sources */
+    struct sockaddr_storage gf_slist[1];    /* interface index */
+};
+#endif /* #if (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD) */
+
 #if LWIP_IPV4
 struct in_pktinfo {
   unsigned int   ipi_ifindex;  /* Interface index */
@@ -327,7 +405,19 @@
 };
 #endif /* LWIP_IPV4 */
 
+#if LWIP_IPV6
+struct in6_pktinfo {
+  struct in6_addr ipi6_addr;
+  int             ipi6_ifindex;
+};
+#endif /* LWIP_IPV6 */
+
 #if LWIP_IPV6_MLD
+#define IPV6_UNICAST_HOPS    16 /* IPv6 ttl */
+#define IPV6_MULTICAST_IF    17 /* Set mcast ifindex, (The argument is a pointer to an interface index) */
+#define IPV6_MULTICAST_HOPS  18 /* IPv6 mcast ttl */
+#define IPV6_MULTICAST_LOOP  19 /* Mcast loop */
+
 /*
  * Options and types related to IPv6 multicast membership
  */
diff -Nur lwip/src/include/lwip/udp.h lwip-mcast/src/include/lwip/udp.h
--- lwip/src/include/lwip/udp.h	Fri Jan 26 13:32:36 2018
+++ lwip-mcast/src/include/lwip/udp.h	Sat Jan 27 17:12:28 2018
@@ -48,6 +48,7 @@
 #include "lwip/ip.h"
 #include "lwip/ip6_addr.h"
 #include "lwip/prot/udp.h"
+#include "lwip/mcast.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -100,6 +101,10 @@
   /** TTL for outgoing multicast packets */
   u8_t mcast_ttl;
 #endif /* LWIP_MULTICAST_TX_OPTIONS */
+
+#if (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD)
+  struct ip_mc ipmc;
+#endif /* (LWIP_IPV4 && LWIP_IGMP) || (LWIP_IPV6 && LWIP_IPV6_MLD) */
 
 #if LWIP_UDPLITE
   /** used for UDP_LITE only */
