1 /* Copyright (C) 2010: YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
2 * Copyright (C) 2015: Linus Lüssing <linus.luessing@c0d3.blue>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 * Based on the MLD support added to br_multicast.c by YOSHIFUJI Hideaki.
20 #include <linux/skbuff.h>
23 #include <net/addrconf.h>
24 #include <net/ip6_checksum.h>
26 static int ipv6_mc_check_ip6hdr(struct sk_buff *skb)
28 const struct ipv6hdr *ip6h;
30 unsigned int offset = skb_network_offset(skb) + sizeof(*ip6h);
32 if (!pskb_may_pull(skb, offset))
37 if (ip6h->version != 6)
40 len = offset + ntohs(ip6h->payload_len);
41 if (skb->len < len || len <= offset)
44 skb_set_transport_header(skb, offset);
49 static int ipv6_mc_check_exthdrs(struct sk_buff *skb)
51 const struct ipv6hdr *ip6h;
58 if (ip6h->nexthdr != IPPROTO_HOPOPTS)
61 nexthdr = ip6h->nexthdr;
62 offset = skb_network_offset(skb) + sizeof(*ip6h);
63 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off);
68 if (nexthdr != IPPROTO_ICMPV6)
71 skb_set_transport_header(skb, offset);
76 static int ipv6_mc_check_mld_reportv2(struct sk_buff *skb)
78 unsigned int len = skb_transport_offset(skb);
80 len += sizeof(struct mld2_report);
82 return ipv6_mc_may_pull(skb, len) ? 0 : -EINVAL;
85 static int ipv6_mc_check_mld_query(struct sk_buff *skb)
87 unsigned int transport_len = ipv6_transport_len(skb);
91 /* RFC2710+RFC3810 (MLDv1+MLDv2) require link-local source addresses */
92 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL))
96 if (transport_len != sizeof(struct mld_msg)) {
98 if (transport_len < sizeof(struct mld2_query))
101 len = skb_transport_offset(skb) + sizeof(struct mld2_query);
102 if (!ipv6_mc_may_pull(skb, len))
106 mld = (struct mld_msg *)skb_transport_header(skb);
108 /* RFC2710+RFC3810 (MLDv1+MLDv2) require the multicast link layer
109 * all-nodes destination address (ff02::1) for general queries
111 if (ipv6_addr_any(&mld->mld_mca) &&
112 !ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr))
118 static int ipv6_mc_check_mld_msg(struct sk_buff *skb)
120 unsigned int len = skb_transport_offset(skb) + sizeof(struct mld_msg);
123 if (!ipv6_mc_may_pull(skb, len))
126 mld = (struct mld_msg *)skb_transport_header(skb);
128 switch (mld->mld_type) {
129 case ICMPV6_MGM_REDUCTION:
130 case ICMPV6_MGM_REPORT:
133 case ICMPV6_MLD2_REPORT:
134 return ipv6_mc_check_mld_reportv2(skb);
135 case ICMPV6_MGM_QUERY:
136 return ipv6_mc_check_mld_query(skb);
142 static inline __sum16 ipv6_mc_validate_checksum(struct sk_buff *skb)
144 return skb_checksum_validate(skb, IPPROTO_ICMPV6, ip6_compute_pseudo);
147 int ipv6_mc_check_icmpv6(struct sk_buff *skb)
149 unsigned int len = skb_transport_offset(skb) + sizeof(struct icmp6hdr);
150 unsigned int transport_len = ipv6_transport_len(skb);
151 struct sk_buff *skb_chk;
153 if (!ipv6_mc_may_pull(skb, len))
156 skb_chk = skb_checksum_trimmed(skb, transport_len,
157 ipv6_mc_validate_checksum);
166 EXPORT_SYMBOL(ipv6_mc_check_icmpv6);
169 * ipv6_mc_check_mld - checks whether this is a sane MLD packet
170 * @skb: the skb to validate
172 * Checks whether an IPv6 packet is a valid MLD packet. If so sets
173 * skb transport header accordingly and returns zero.
175 * -EINVAL: A broken packet was detected, i.e. it violates some internet
177 * -ENOMSG: IP header validation succeeded but it is not an MLD packet.
178 * -ENOMEM: A memory allocation failure happened.
180 * Caller needs to set the skb network header and free any returned skb if it
181 * differs from the provided skb.
183 int ipv6_mc_check_mld(struct sk_buff *skb)
187 ret = ipv6_mc_check_ip6hdr(skb);
191 ret = ipv6_mc_check_exthdrs(skb);
195 ret = ipv6_mc_check_icmpv6(skb);
199 return ipv6_mc_check_mld_msg(skb);
201 EXPORT_SYMBOL(ipv6_mc_check_mld);