1 // SPDX-License-Identifier: GPL-2.0-only << 2 /* Copyright (C) 2010: YOSHIFUJI Hideaki <yosh 1 /* Copyright (C) 2010: YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org> 3 * Copyright (C) 2015: Linus Lüssing <linus.l 2 * Copyright (C) 2015: Linus Lüssing <linus.luessing@c0d3.blue> 4 * 3 * >> 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. >> 7 * >> 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. >> 12 * >> 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/>. >> 15 * >> 16 * 5 * Based on the MLD support added to br_multic 17 * Based on the MLD support added to br_multicast.c by YOSHIFUJI Hideaki. 6 */ 18 */ 7 19 8 #include <linux/skbuff.h> 20 #include <linux/skbuff.h> 9 #include <net/ipv6.h> 21 #include <net/ipv6.h> 10 #include <net/mld.h> 22 #include <net/mld.h> 11 #include <net/addrconf.h> 23 #include <net/addrconf.h> 12 #include <net/ip6_checksum.h> 24 #include <net/ip6_checksum.h> 13 25 14 static int ipv6_mc_check_ip6hdr(struct sk_buff 26 static int ipv6_mc_check_ip6hdr(struct sk_buff *skb) 15 { 27 { 16 const struct ipv6hdr *ip6h; 28 const struct ipv6hdr *ip6h; 17 unsigned int len; 29 unsigned int len; 18 unsigned int offset = skb_network_offs 30 unsigned int offset = skb_network_offset(skb) + sizeof(*ip6h); 19 31 20 if (!pskb_may_pull(skb, offset)) 32 if (!pskb_may_pull(skb, offset)) 21 return -EINVAL; 33 return -EINVAL; 22 34 23 ip6h = ipv6_hdr(skb); 35 ip6h = ipv6_hdr(skb); 24 36 25 if (ip6h->version != 6) 37 if (ip6h->version != 6) 26 return -EINVAL; 38 return -EINVAL; 27 39 28 len = offset + ntohs(ip6h->payload_len 40 len = offset + ntohs(ip6h->payload_len); 29 if (skb->len < len || len <= offset) 41 if (skb->len < len || len <= offset) 30 return -EINVAL; 42 return -EINVAL; 31 43 32 skb_set_transport_header(skb, offset); 44 skb_set_transport_header(skb, offset); 33 45 34 return 0; 46 return 0; 35 } 47 } 36 48 37 static int ipv6_mc_check_exthdrs(struct sk_buf 49 static int ipv6_mc_check_exthdrs(struct sk_buff *skb) 38 { 50 { 39 const struct ipv6hdr *ip6h; 51 const struct ipv6hdr *ip6h; 40 int offset; 52 int offset; 41 u8 nexthdr; 53 u8 nexthdr; 42 __be16 frag_off; 54 __be16 frag_off; 43 55 44 ip6h = ipv6_hdr(skb); 56 ip6h = ipv6_hdr(skb); 45 57 46 if (ip6h->nexthdr != IPPROTO_HOPOPTS) 58 if (ip6h->nexthdr != IPPROTO_HOPOPTS) 47 return -ENOMSG; 59 return -ENOMSG; 48 60 49 nexthdr = ip6h->nexthdr; 61 nexthdr = ip6h->nexthdr; 50 offset = skb_network_offset(skb) + siz 62 offset = skb_network_offset(skb) + sizeof(*ip6h); 51 offset = ipv6_skip_exthdr(skb, offset, 63 offset = ipv6_skip_exthdr(skb, offset, &nexthdr, &frag_off); 52 64 53 if (offset < 0) 65 if (offset < 0) 54 return -EINVAL; 66 return -EINVAL; 55 67 56 if (nexthdr != IPPROTO_ICMPV6) 68 if (nexthdr != IPPROTO_ICMPV6) 57 return -ENOMSG; 69 return -ENOMSG; 58 70 59 skb_set_transport_header(skb, offset); 71 skb_set_transport_header(skb, offset); 60 72 61 return 0; 73 return 0; 62 } 74 } 63 75 64 static int ipv6_mc_check_mld_reportv2(struct s 76 static int ipv6_mc_check_mld_reportv2(struct sk_buff *skb) 65 { 77 { 66 unsigned int len = skb_transport_offse 78 unsigned int len = skb_transport_offset(skb); 67 79 68 len += sizeof(struct mld2_report); 80 len += sizeof(struct mld2_report); 69 81 70 return ipv6_mc_may_pull(skb, len) ? 0 82 return ipv6_mc_may_pull(skb, len) ? 0 : -EINVAL; 71 } 83 } 72 84 73 static int ipv6_mc_check_mld_query(struct sk_b 85 static int ipv6_mc_check_mld_query(struct sk_buff *skb) 74 { 86 { 75 unsigned int transport_len = ipv6_tran 87 unsigned int transport_len = ipv6_transport_len(skb); 76 struct mld_msg *mld; 88 struct mld_msg *mld; 77 unsigned int len; 89 unsigned int len; 78 90 79 /* RFC2710+RFC3810 (MLDv1+MLDv2) requi 91 /* RFC2710+RFC3810 (MLDv1+MLDv2) require link-local source addresses */ 80 if (!(ipv6_addr_type(&ipv6_hdr(skb)->s 92 if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) 81 return -EINVAL; 93 return -EINVAL; 82 94 83 /* MLDv1? */ 95 /* MLDv1? */ 84 if (transport_len != sizeof(struct mld 96 if (transport_len != sizeof(struct mld_msg)) { 85 /* or MLDv2? */ 97 /* or MLDv2? */ 86 if (transport_len < sizeof(str 98 if (transport_len < sizeof(struct mld2_query)) 87 return -EINVAL; 99 return -EINVAL; 88 100 89 len = skb_transport_offset(skb 101 len = skb_transport_offset(skb) + sizeof(struct mld2_query); 90 if (!ipv6_mc_may_pull(skb, len 102 if (!ipv6_mc_may_pull(skb, len)) 91 return -EINVAL; 103 return -EINVAL; 92 } 104 } 93 105 94 mld = (struct mld_msg *)skb_transport_ 106 mld = (struct mld_msg *)skb_transport_header(skb); 95 107 96 /* RFC2710+RFC3810 (MLDv1+MLDv2) requi 108 /* RFC2710+RFC3810 (MLDv1+MLDv2) require the multicast link layer 97 * all-nodes destination address (ff02 109 * all-nodes destination address (ff02::1) for general queries 98 */ 110 */ 99 if (ipv6_addr_any(&mld->mld_mca) && 111 if (ipv6_addr_any(&mld->mld_mca) && 100 !ipv6_addr_is_ll_all_nodes(&ipv6_h 112 !ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr)) 101 return -EINVAL; 113 return -EINVAL; 102 114 103 return 0; 115 return 0; 104 } 116 } 105 117 106 static int ipv6_mc_check_mld_msg(struct sk_buf 118 static int ipv6_mc_check_mld_msg(struct sk_buff *skb) 107 { 119 { 108 unsigned int len = skb_transport_offse 120 unsigned int len = skb_transport_offset(skb) + sizeof(struct mld_msg); 109 struct mld_msg *mld; 121 struct mld_msg *mld; 110 122 111 if (!ipv6_mc_may_pull(skb, len)) 123 if (!ipv6_mc_may_pull(skb, len)) 112 return -ENODATA; !! 124 return -EINVAL; 113 125 114 mld = (struct mld_msg *)skb_transport_ 126 mld = (struct mld_msg *)skb_transport_header(skb); 115 127 116 switch (mld->mld_type) { 128 switch (mld->mld_type) { 117 case ICMPV6_MGM_REDUCTION: 129 case ICMPV6_MGM_REDUCTION: 118 case ICMPV6_MGM_REPORT: 130 case ICMPV6_MGM_REPORT: 119 return 0; 131 return 0; 120 case ICMPV6_MLD2_REPORT: 132 case ICMPV6_MLD2_REPORT: 121 return ipv6_mc_check_mld_repor 133 return ipv6_mc_check_mld_reportv2(skb); 122 case ICMPV6_MGM_QUERY: 134 case ICMPV6_MGM_QUERY: 123 return ipv6_mc_check_mld_query 135 return ipv6_mc_check_mld_query(skb); 124 default: 136 default: 125 return -ENODATA; !! 137 return -ENOMSG; 126 } 138 } 127 } 139 } 128 140 129 static inline __sum16 ipv6_mc_validate_checksu 141 static inline __sum16 ipv6_mc_validate_checksum(struct sk_buff *skb) 130 { 142 { 131 return skb_checksum_validate(skb, IPPR 143 return skb_checksum_validate(skb, IPPROTO_ICMPV6, ip6_compute_pseudo); 132 } 144 } 133 145 134 static int ipv6_mc_check_icmpv6(struct sk_buff !! 146 int ipv6_mc_check_icmpv6(struct sk_buff *skb) 135 { 147 { 136 unsigned int len = skb_transport_offse 148 unsigned int len = skb_transport_offset(skb) + sizeof(struct icmp6hdr); 137 unsigned int transport_len = ipv6_tran 149 unsigned int transport_len = ipv6_transport_len(skb); 138 struct sk_buff *skb_chk; 150 struct sk_buff *skb_chk; 139 151 140 if (!ipv6_mc_may_pull(skb, len)) 152 if (!ipv6_mc_may_pull(skb, len)) 141 return -EINVAL; 153 return -EINVAL; 142 154 143 skb_chk = skb_checksum_trimmed(skb, tr 155 skb_chk = skb_checksum_trimmed(skb, transport_len, 144 ipv6_mc 156 ipv6_mc_validate_checksum); 145 if (!skb_chk) 157 if (!skb_chk) 146 return -EINVAL; 158 return -EINVAL; 147 159 148 if (skb_chk != skb) 160 if (skb_chk != skb) 149 kfree_skb(skb_chk); 161 kfree_skb(skb_chk); 150 162 151 return 0; 163 return 0; 152 } 164 } >> 165 EXPORT_SYMBOL(ipv6_mc_check_icmpv6); 153 166 154 /** 167 /** 155 * ipv6_mc_check_mld - checks whether this is 168 * ipv6_mc_check_mld - checks whether this is a sane MLD packet 156 * @skb: the skb to validate 169 * @skb: the skb to validate 157 * 170 * 158 * Checks whether an IPv6 packet is a valid ML 171 * Checks whether an IPv6 packet is a valid MLD packet. If so sets 159 * skb transport header accordingly and return 172 * skb transport header accordingly and returns zero. 160 * 173 * 161 * -EINVAL: A broken packet was detected, i.e. 174 * -EINVAL: A broken packet was detected, i.e. it violates some internet 162 * standard 175 * standard 163 * -ENOMSG: IP header validation succeeded but !! 176 * -ENOMSG: IP header validation succeeded but it is not an MLD packet. 164 * with a hop-by-hop option. << 165 * -ENODATA: IP+ICMPv6 header with hop-by-hop << 166 * but it is not an MLD packet. << 167 * -ENOMEM: A memory allocation failure happen 177 * -ENOMEM: A memory allocation failure happened. 168 * 178 * 169 * Caller needs to set the skb network header 179 * Caller needs to set the skb network header and free any returned skb if it 170 * differs from the provided skb. 180 * differs from the provided skb. 171 */ 181 */ 172 int ipv6_mc_check_mld(struct sk_buff *skb) 182 int ipv6_mc_check_mld(struct sk_buff *skb) 173 { 183 { 174 int ret; 184 int ret; 175 185 176 ret = ipv6_mc_check_ip6hdr(skb); 186 ret = ipv6_mc_check_ip6hdr(skb); 177 if (ret < 0) 187 if (ret < 0) 178 return ret; 188 return ret; 179 189 180 ret = ipv6_mc_check_exthdrs(skb); 190 ret = ipv6_mc_check_exthdrs(skb); 181 if (ret < 0) 191 if (ret < 0) 182 return ret; 192 return ret; 183 193 184 ret = ipv6_mc_check_icmpv6(skb); 194 ret = ipv6_mc_check_icmpv6(skb); 185 if (ret < 0) 195 if (ret < 0) 186 return ret; 196 return ret; 187 197 188 return ipv6_mc_check_mld_msg(skb); 198 return ipv6_mc_check_mld_msg(skb); 189 } 199 } 190 EXPORT_SYMBOL(ipv6_mc_check_mld); 200 EXPORT_SYMBOL(ipv6_mc_check_mld); 191 201
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.