1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (C) 2010: YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
3 * Copyright (C) 2015: Linus Lüssing <linus.luessing@c0d3.blue>
5 * Based on the MLD support added to br_multicast.c by YOSHIFUJI Hideaki.
8 #include <linux/skbuff.h>
11 #include <net/addrconf.h>
12 #include <net/ip6_checksum.h>
14 static int ipv6_mc_check_ip6hdr(struct sk_buff
*skb
)
16 const struct ipv6hdr
*ip6h
;
18 unsigned int offset
= skb_network_offset(skb
) + sizeof(*ip6h
);
20 if (!pskb_may_pull(skb
, offset
))
25 if (ip6h
->version
!= 6)
28 len
= offset
+ ntohs(ip6h
->payload_len
);
29 if (skb
->len
< len
|| len
<= offset
)
32 skb_set_transport_header(skb
, offset
);
37 static int ipv6_mc_check_exthdrs(struct sk_buff
*skb
)
39 const struct ipv6hdr
*ip6h
;
46 if (ip6h
->nexthdr
!= IPPROTO_HOPOPTS
)
49 nexthdr
= ip6h
->nexthdr
;
50 offset
= skb_network_offset(skb
) + sizeof(*ip6h
);
51 offset
= ipv6_skip_exthdr(skb
, offset
, &nexthdr
, &frag_off
);
56 if (nexthdr
!= IPPROTO_ICMPV6
)
59 skb_set_transport_header(skb
, offset
);
64 static int ipv6_mc_check_mld_reportv2(struct sk_buff
*skb
)
66 unsigned int len
= skb_transport_offset(skb
);
68 len
+= sizeof(struct mld2_report
);
70 return ipv6_mc_may_pull(skb
, len
) ? 0 : -EINVAL
;
73 static int ipv6_mc_check_mld_query(struct sk_buff
*skb
)
75 unsigned int transport_len
= ipv6_transport_len(skb
);
79 /* RFC2710+RFC3810 (MLDv1+MLDv2) require link-local source addresses */
80 if (!(ipv6_addr_type(&ipv6_hdr(skb
)->saddr
) & IPV6_ADDR_LINKLOCAL
))
84 if (transport_len
!= sizeof(struct mld_msg
)) {
86 if (transport_len
< sizeof(struct mld2_query
))
89 len
= skb_transport_offset(skb
) + sizeof(struct mld2_query
);
90 if (!ipv6_mc_may_pull(skb
, len
))
94 mld
= (struct mld_msg
*)skb_transport_header(skb
);
96 /* RFC2710+RFC3810 (MLDv1+MLDv2) require the multicast link layer
97 * all-nodes destination address (ff02::1) for general queries
99 if (ipv6_addr_any(&mld
->mld_mca
) &&
100 !ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb
)->daddr
))
106 static int ipv6_mc_check_mld_msg(struct sk_buff
*skb
)
108 unsigned int len
= skb_transport_offset(skb
) + sizeof(struct mld_msg
);
111 if (!ipv6_mc_may_pull(skb
, len
))
114 mld
= (struct mld_msg
*)skb_transport_header(skb
);
116 switch (mld
->mld_type
) {
117 case ICMPV6_MGM_REDUCTION
:
118 case ICMPV6_MGM_REPORT
:
120 case ICMPV6_MLD2_REPORT
:
121 return ipv6_mc_check_mld_reportv2(skb
);
122 case ICMPV6_MGM_QUERY
:
123 return ipv6_mc_check_mld_query(skb
);
129 static inline __sum16
ipv6_mc_validate_checksum(struct sk_buff
*skb
)
131 return skb_checksum_validate(skb
, IPPROTO_ICMPV6
, ip6_compute_pseudo
);
134 int ipv6_mc_check_icmpv6(struct sk_buff
*skb
)
136 unsigned int len
= skb_transport_offset(skb
) + sizeof(struct icmp6hdr
);
137 unsigned int transport_len
= ipv6_transport_len(skb
);
138 struct sk_buff
*skb_chk
;
140 if (!ipv6_mc_may_pull(skb
, len
))
143 skb_chk
= skb_checksum_trimmed(skb
, transport_len
,
144 ipv6_mc_validate_checksum
);
153 EXPORT_SYMBOL(ipv6_mc_check_icmpv6
);
156 * ipv6_mc_check_mld - checks whether this is a sane MLD packet
157 * @skb: the skb to validate
159 * Checks whether an IPv6 packet is a valid MLD packet. If so sets
160 * skb transport header accordingly and returns zero.
162 * -EINVAL: A broken packet was detected, i.e. it violates some internet
164 * -ENOMSG: IP header validation succeeded but it is not an MLD packet.
165 * -ENOMEM: A memory allocation failure happened.
167 * Caller needs to set the skb network header and free any returned skb if it
168 * differs from the provided skb.
170 int ipv6_mc_check_mld(struct sk_buff
*skb
)
174 ret
= ipv6_mc_check_ip6hdr(skb
);
178 ret
= ipv6_mc_check_exthdrs(skb
);
182 ret
= ipv6_mc_check_icmpv6(skb
);
186 return ipv6_mc_check_mld_msg(skb
);
188 EXPORT_SYMBOL(ipv6_mc_check_mld
);