Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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