From 325356c1cc1e19c3304756fd8c62f4a38ffe0e65 Mon Sep 17 00:00:00 2001
From: hanhui <hanhui03@163.com>
Date: Sat, 27 Jan 2018 09:24:24 +0100
Subject: [PATCH] add multicast filter and IGMPv3 support

- RFC compliant source address multicast filters and IGMPv3 implementations.
- raw support multicast.
- Compatible with Linux socket multicast filter settings command.
---
 src/api/api_msg.c            |  121 +++--
 src/api/sockets.c            | 1001 +++++++++++++++++++++++++++++-------------
 src/core/ipv4/igmp.c         |  473 +++++++++++++++++++-
 src/core/raw.c               |   60 ++-
 src/core/tcp_in.c            |   11 +
 src/core/udp.c               |   28 ++
 src/include/lwip/api.h       |    2 +
 src/include/lwip/igmp.h      |   21 +
 src/include/lwip/ip.h        |    4 +-
 src/include/lwip/mld6.h      |    7 +
 src/include/lwip/netbuf.h    |    5 +
 src/include/lwip/opt.h       |   17 +
 src/include/lwip/prot/igmp.h |   75 ++++
 src/include/lwip/raw.h       |   14 +
 src/include/lwip/sockets.h   |   94 +++-
 src/include/lwip/udp.h       |    4 +
 16 files changed, 1572 insertions(+), 365 deletions(-)

diff --git a/src/api/api_msg.c b/src/api/api_msg.c
index 39531024..31cb4634 100644
--- a/src/api/api_msg.c
+++ b/src/api/api_msg.c
@@ -186,9 +186,35 @@ recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p,
 
       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);
@@ -265,6 +291,15 @@ recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
       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 */
   }
 
@@ -2017,36 +2052,31 @@ lwip_netconn_do_join_leave_group(void *m)
 
   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);
 }
@@ -2070,36 +2100,31 @@ lwip_netconn_do_join_leave_group_netif(void *m)
 
   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 --git a/src/api/sockets.c b/src/api/sockets.c
index a6f2dba7..6a0373e2 100644
--- a/src/api/sockets.c
+++ b/src/api/sockets.c
@@ -213,49 +213,6 @@ union sockaddr_aligned {
 #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];
 
@@ -1155,7 +1112,7 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16
   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) {
@@ -1169,19 +1126,49 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16
           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 */
@@ -3034,38 +3021,127 @@ lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *opt
           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));
@@ -3125,6 +3201,107 @@ lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *opt
     /* 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);
@@ -3479,65 +3656,281 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_
           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",
@@ -3612,40 +4005,217 @@ lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_
           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;
+#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",
@@ -3979,183 +4549,4 @@ lwip_inet_pton(int af, const char *src, void *dst)
   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 --git a/src/core/ipv4/igmp.c b/src/core/ipv4/igmp.c
index b655aa3a..8be11fce 100644
--- a/src/core/ipv4/igmp.c
+++ b/src/core/ipv4/igmp.c
@@ -64,14 +64,19 @@ The router alert rfc 2113 is implemented in outgoing packets
 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 @@ Steve Reynolds
 #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 void   igmp_delaying_member(struct igmp_group *group, u8_t maxresp);
 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 @@ igmp_init(void)
 
   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 @@ igmp_start(struct netif *netif)
   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_report_groups(struct netif *netif)
     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 @@ igmp_lookup_group(struct netif *ifp, const ip4_addr_t *addr)
     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 @@ igmp_input(struct pbuf *p, struct netif *inp, const ip4_addr_t *dest)
   /* 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 @@ igmp_joingroup(const ip4_addr_t *ifaddr, const ip4_addr_t *groupaddr)
 
   /* 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 @@ igmp_joingroup_netif(struct netif *netif, const ip4_addr_t *groupaddr)
 
   /* 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,16 @@ igmp_joingroup_netif(struct netif *netif, const ip4_addr_t *groupaddr)
 
       /* 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 +672,9 @@ igmp_leavegroup(const ip4_addr_t *ifaddr, const ip4_addr_t *groupaddr)
 
   /* 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 +708,9 @@ igmp_leavegroup_netif(struct netif *netif, const ip4_addr_t *groupaddr)
 
   /* 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 +736,13 @@ igmp_leavegroup_netif(struct netif *netif, const ip4_addr_t *groupaddr)
         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 +785,19 @@ igmp_tmr(void)
       }
       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 +825,36 @@ igmp_timeout(struct netif *netif, struct igmp_group *group)
   }
 }
 
+#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 +877,51 @@ igmp_start_timer(struct igmp_group *group, u8_t max_time)
   }
 }
 
+#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
  *
@@ -721,6 +940,33 @@ igmp_delaying_member(struct igmp_group *group, u8_t maxresp)
 }
 
 
+#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
  * and calculates the IP header checksum. If the source IP address is NULL,
@@ -798,4 +1044,219 @@ igmp_send(struct netif *netif, struct igmp_group *group, u8_t type)
   }
 }
 
+#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 --git a/src/core/raw.c b/src/core/raw.c
index 3b34544b..d77a50e9 100644
--- a/src/core/raw.c
+++ b/src/core/raw.c
@@ -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 @@ raw_input_local_match(struct raw_pcb *pcb, u8_t broadcast)
   }
 #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 @@ raw_input(struct pbuf *p, struct netif *inp)
     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 @@ raw_sendto(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr)
       /* 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 @@ raw_sendto_if_src(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
     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 @@ raw_remove(struct raw_pcb *pcb)
 {
   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 @@ raw_new(u8_t proto)
 #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 --git a/src/core/tcp_in.c b/src/core/tcp_in.c
index 428a6f48..e437c377 100644
--- a/src/core/tcp_in.c
+++ b/src/core/tcp_in.c
@@ -263,6 +263,17 @@ tcp_input(struct pbuf *p, struct netif *inp)
         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 --git a/src/core/udp.c b/src/core/udp.c
index ac0e4d6e..e96f8451 100644
--- a/src/core/udp.c
+++ b/src/core/udp.c
@@ -151,6 +151,12 @@ udp_input_local_match(struct udp_pcb *pcb, struct netif *inp, u8_t broadcast)
     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
@@ -289,6 +295,21 @@ udp_input(struct pbuf *p, struct netif *inp)
       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
@@ -1184,6 +1205,10 @@ udp_remove(struct udp_pcb *pcb)
 
   LWIP_ERROR("udp_remove: invalid pcb", pcb != NULL, return);
 
+#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) {
@@ -1233,6 +1258,9 @@ udp_new(void)
 #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 --git a/src/include/lwip/api.h b/src/include/lwip/api.h
index f8426a7d..abda5c11 100644
--- a/src/include/lwip/api.h
+++ b/src/include/lwip/api.h
@@ -89,6 +89,8 @@ extern "C" {
 #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 TODO: duplicate
 #endif /* LWIP_NETBUF_RECVINFO */
 /** A FIN has been received but not passed to the application yet */
 #define NETCONN_FIN_RX_PENDING                0x80
diff --git a/src/include/lwip/igmp.h b/src/include/lwip/igmp.h
index ffd80e68..fd1e49d5 100644
--- a/src/include/lwip/igmp.h
+++ b/src/include/lwip/igmp.h
@@ -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 @@ extern "C" {
 #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
@@ -84,6 +95,16 @@ struct igmp_group {
   u16_t              timer;
   /** counter of simultaneous uses */
   u8_t               use;
+#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 */
 };
 
 /*  Prototypes */
diff --git a/src/include/lwip/ip.h b/src/include/lwip/ip.h
index 653c3b2f..ddcc2b46 100644
--- a/src/include/lwip/ip.h
+++ b/src/include/lwip/ip.h
@@ -84,7 +84,9 @@ extern "C" {
   /* 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 --git a/src/include/lwip/mld6.h b/src/include/lwip/mld6.h
index 7fa0797f..9cb99ae4 100644
--- a/src/include/lwip/mld6.h
+++ b/src/include/lwip/mld6.h
@@ -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 --git a/src/include/lwip/netbuf.h b/src/include/lwip/netbuf.h
index 42a911bc..e7f0713b 100644
--- a/src/include/lwip/netbuf.h
+++ b/src/include/lwip/netbuf.h
@@ -55,6 +55,8 @@ extern "C" {
 #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 @@ struct netbuf {
   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 --git a/src/include/lwip/opt.h b/src/include/lwip/opt.h
index 3f3cdd01..5c4ddf8b 100644
--- a/src/include/lwip/opt.h
+++ b/src/include/lwip/opt.h
@@ -1079,9 +1079,19 @@
 #if !defined LWIP_IGMP || defined __DOXYGEN__
 #define LWIP_IGMP                       0
 #endif
+
+/**
+ * LWIP_IGMP_V3==1: Turn on IGMPv3 module.
+ */
+#if !defined LWIP_IGMP_V3 || defined __DOXYGEN__
+#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
 /**
  * @}
@@ -2516,6 +2526,13 @@
 #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
+
+/**
  * MEMP_NUM_MLD6_GROUP: Max number of IPv6 multicast groups that can be joined.
  * There must be enough groups so that each netif can join the solicited-node
  * multicast group for each of its local addresses, plus one for MDNS if
diff --git a/src/include/lwip/prot/igmp.h b/src/include/lwip/prot/igmp.h
index d60cb31e..253f9de1 100644
--- a/src/include/lwip/prot/igmp.h
+++ b/src/include/lwip/prot/igmp.h
@@ -49,6 +49,7 @@ extern "C" {
  */
 #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 @@ extern "C" {
 #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
@@ -83,6 +85,79 @@ PACK_STRUCT_END
 #  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
 }
 #endif
diff --git a/src/include/lwip/raw.h b/src/include/lwip/raw.h
index c94f3bf4..d3b49960 100644
--- a/src/include/lwip/raw.h
+++ b/src/include/lwip/raw.h
@@ -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 @@ struct raw_pcb {
   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 @@ struct raw_pcb {
   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 @@ void             raw_recv       (struct raw_pcb *pcb, raw_recv_fn recv, void *re
 #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 --git a/src/include/lwip/sockets.h b/src/include/lwip/sockets.h
index d70d36c4..7eadf36d 100644
--- a/src/include/lwip/sockets.h
+++ b/src/include/lwip/sockets.h
@@ -271,6 +271,17 @@ struct linger {
 #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
 /*
@@ -317,11 +328,78 @@ struct linger {
 #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 */
@@ -329,7 +407,19 @@ struct in_pktinfo {
 };
 #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 --git a/src/include/lwip/udp.h b/src/include/lwip/udp.h
index 4f4a36d3..a0cc43a3 100644
--- a/src/include/lwip/udp.h
+++ b/src/include/lwip/udp.h
@@ -101,6 +101,10 @@ struct udp_pcb {
   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 */
   u16_t chksum_len_rx, chksum_len_tx;
-- 
2.11.0.windows.1

