xref: /linux/net/ipv6/exthdrs_core.c (revision 30c71476d842b094651bd7712f6c8ee8716147d0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * IPv6 library code, needed by static components when full IPv6 support is
4  * not configured or static.
5  */
6 #include <linux/export.h>
7 #include <net/ipv6.h>
8 
9 /*
10  * find out if nexthdr is a well-known extension header or a protocol
11  */
12 
13 bool ipv6_ext_hdr(u8 nexthdr)
14 {
15 	/*
16 	 * find out if nexthdr is an extension header or a protocol
17 	 */
18 	return   (nexthdr == NEXTHDR_HOP)	||
19 		 (nexthdr == NEXTHDR_ROUTING)	||
20 		 (nexthdr == NEXTHDR_FRAGMENT)	||
21 		 (nexthdr == NEXTHDR_AUTH)	||
22 		 (nexthdr == NEXTHDR_NONE)	||
23 		 (nexthdr == NEXTHDR_DEST);
24 }
25 EXPORT_SYMBOL(ipv6_ext_hdr);
26 
27 /*
28  * Skip any extension headers. This is used by the ICMP module.
29  *
30  * Note that strictly speaking this conflicts with RFC 2460 4.0:
31  * ...The contents and semantics of each extension header determine whether
32  * or not to proceed to the next header.  Therefore, extension headers must
33  * be processed strictly in the order they appear in the packet; a
34  * receiver must not, for example, scan through a packet looking for a
35  * particular kind of extension header and process that header prior to
36  * processing all preceding ones.
37  *
38  * We do exactly this. This is a protocol bug. We can't decide after a
39  * seeing an unknown discard-with-error flavour TLV option if it's a
40  * ICMP error message or not (errors should never be send in reply to
41  * ICMP error messages).
42  *
43  * But I see no other way to do this. This might need to be reexamined
44  * when Linux implements ESP (and maybe AUTH) headers.
45  * --AK
46  *
47  * This function parses (probably truncated) exthdr set "hdr".
48  * "nexthdrp" initially points to some place,
49  * where type of the first header can be found.
50  *
51  * It skips all well-known exthdrs, and returns pointer to the start
52  * of unparsable area i.e. the first header with unknown type.
53  * If it is not NULL *nexthdr is updated by type/protocol of this header.
54  *
55  * NOTES: - if packet terminated with NEXTHDR_NONE it returns NULL.
56  *        - it may return pointer pointing beyond end of packet,
57  *	    if the last recognized header is truncated in the middle.
58  *        - if packet is truncated, so that all parsed headers are skipped,
59  *	    it returns NULL.
60  *	  - First fragment header is skipped, not-first ones
61  *	    are considered as unparsable.
62  *	  - Reports the offset field of the final fragment header so it is
63  *	    possible to tell whether this is a first fragment, later fragment,
64  *	    or not fragmented.
65  *	  - ESP is unparsable for now and considered like
66  *	    normal payload protocol.
67  *	  - Note also special handling of AUTH header. Thanks to IPsec wizards.
68  *
69  * --ANK (980726)
70  */
71 
72 int ipv6_skip_exthdr(const struct sk_buff *skb, int start, u8 *nexthdrp,
73 		     __be16 *frag_offp)
74 {
75 	u8 nexthdr = *nexthdrp;
76 	int exthdr_cnt = 0;
77 
78 	*frag_offp = 0;
79 
80 	while (ipv6_ext_hdr(nexthdr)) {
81 		struct ipv6_opt_hdr _hdr, *hp;
82 		int hdrlen;
83 
84 		if (nexthdr == NEXTHDR_NONE)
85 			return -1;
86 		if (unlikely(exthdr_cnt++ >= IP6_MAX_EXT_HDRS_CNT))
87 			return -1;
88 		hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);
89 		if (!hp)
90 			return -1;
91 		if (nexthdr == NEXTHDR_FRAGMENT) {
92 			__be16 _frag_off, *fp;
93 			fp = skb_header_pointer(skb,
94 						start+offsetof(struct frag_hdr,
95 							       frag_off),
96 						sizeof(_frag_off),
97 						&_frag_off);
98 			if (!fp)
99 				return -1;
100 
101 			*frag_offp = *fp;
102 			if (ntohs(*frag_offp) & ~0x7)
103 				break;
104 			hdrlen = 8;
105 		} else if (nexthdr == NEXTHDR_AUTH)
106 			hdrlen = ipv6_authlen(hp);
107 		else
108 			hdrlen = ipv6_optlen(hp);
109 
110 		nexthdr = hp->nexthdr;
111 		start += hdrlen;
112 	}
113 
114 	*nexthdrp = nexthdr;
115 	return start;
116 }
117 EXPORT_SYMBOL(ipv6_skip_exthdr);
118 
119 int ipv6_find_tlv(const struct sk_buff *skb, int offset, int type)
120 {
121 	const unsigned char *nh = skb_network_header(skb);
122 	int packet_len = skb_tail_pointer(skb) - skb_network_header(skb);
123 	struct ipv6_opt_hdr *hdr;
124 	int len;
125 
126 	if (offset + 2 > packet_len)
127 		goto bad;
128 	hdr = (struct ipv6_opt_hdr *)(nh + offset);
129 	len = ((hdr->hdrlen + 1) << 3);
130 
131 	if (offset + len > packet_len)
132 		goto bad;
133 
134 	offset += 2;
135 	len -= 2;
136 
137 	while (len > 0) {
138 		int opttype = nh[offset];
139 		int optlen;
140 
141 		switch (opttype) {
142 		case IPV6_TLV_PAD1:
143 			optlen = 1;
144 			break;
145 		default:
146 			if (len < 2)
147 				goto bad;
148 			optlen = nh[offset + 1] + 2;
149 			if (optlen > len)
150 				goto bad;
151 			break;
152 		}
153 		/* Some callers read the option, so it must be in the header. */
154 		if (opttype == type)
155 			return offset;
156 
157 		offset += optlen;
158 		len -= optlen;
159 	}
160 	/* not_found */
161  bad:
162 	return -1;
163 }
164 EXPORT_SYMBOL_GPL(ipv6_find_tlv);
165 
166 /*
167  * find the offset to specified header or the protocol number of last header
168  * if target < 0. "last header" is transport protocol header, ESP, or
169  * "No next header".
170  *
171  * Note that *offset is used as input/output parameter, and if it is not zero,
172  * then it must be a valid offset to an inner IPv6 header. This can be used
173  * to explore inner IPv6 header, eg. ICMPv6 error messages.
174  *
175  * If target header is found, its offset is set in *offset and return protocol
176  * number. Otherwise, return -1.
177  *
178  * If the first fragment doesn't contain the final protocol header or
179  * NEXTHDR_NONE it is considered invalid.
180  *
181  * Note that non-1st fragment is special case that "the protocol number
182  * of last header" is "next header" field in Fragment header. In this case,
183  * *offset is meaningless and fragment offset is stored in *fragoff if fragoff
184  * isn't NULL.
185  *
186  * if flags is not NULL and it's a fragment, then the frag flag
187  * IP6_FH_F_FRAG will be set. If it's an AH header, the
188  * IP6_FH_F_AUTH flag is set and target < 0, then this function will
189  * stop at the AH header. If IP6_FH_F_SKIP_RH flag was passed, then this
190  * function will skip all those routing headers, where segements_left was 0.
191  */
192 int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset,
193 		  int target, unsigned short *fragoff, int *flags)
194 {
195 	unsigned int start = skb_network_offset(skb) + sizeof(struct ipv6hdr);
196 	u8 nexthdr = ipv6_hdr(skb)->nexthdr;
197 	int exthdr_cnt = 0;
198 	bool found;
199 
200 	if (fragoff)
201 		*fragoff = 0;
202 
203 	if (*offset) {
204 		struct ipv6hdr _ip6, *ip6;
205 
206 		ip6 = skb_header_pointer(skb, *offset, sizeof(_ip6), &_ip6);
207 		if (!ip6 || (ip6->version != 6))
208 			return -EBADMSG;
209 		start = *offset + sizeof(struct ipv6hdr);
210 		nexthdr = ip6->nexthdr;
211 	}
212 
213 	do {
214 		struct ipv6_opt_hdr _hdr, *hp;
215 		unsigned int hdrlen;
216 		found = (nexthdr == target);
217 
218 		if ((!ipv6_ext_hdr(nexthdr)) || nexthdr == NEXTHDR_NONE) {
219 			if (target < 0 || found)
220 				break;
221 			return -ENOENT;
222 		}
223 
224 		if (unlikely(exthdr_cnt++ >= IP6_MAX_EXT_HDRS_CNT))
225 			return -EBADMSG;
226 
227 		hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);
228 		if (!hp)
229 			return -EBADMSG;
230 
231 		if (nexthdr == NEXTHDR_ROUTING) {
232 			struct ipv6_rt_hdr _rh, *rh;
233 
234 			rh = skb_header_pointer(skb, start, sizeof(_rh),
235 						&_rh);
236 			if (!rh)
237 				return -EBADMSG;
238 
239 			if (flags && (*flags & IP6_FH_F_SKIP_RH) &&
240 			    rh->segments_left == 0)
241 				found = false;
242 		}
243 
244 		if (nexthdr == NEXTHDR_FRAGMENT) {
245 			unsigned short _frag_off;
246 			__be16 *fp;
247 
248 			if (flags)	/* Indicate that this is a fragment */
249 				*flags |= IP6_FH_F_FRAG;
250 			fp = skb_header_pointer(skb,
251 						start+offsetof(struct frag_hdr,
252 							       frag_off),
253 						sizeof(_frag_off),
254 						&_frag_off);
255 			if (!fp)
256 				return -EBADMSG;
257 
258 			_frag_off = ntohs(*fp) & ~0x7;
259 			if (_frag_off) {
260 				if (target < 0 &&
261 				    ((!ipv6_ext_hdr(hp->nexthdr)) ||
262 				     hp->nexthdr == NEXTHDR_NONE)) {
263 					if (fragoff)
264 						*fragoff = _frag_off;
265 					return hp->nexthdr;
266 				}
267 				if (!found)
268 					return -ENOENT;
269 				if (fragoff)
270 					*fragoff = _frag_off;
271 				break;
272 			}
273 			hdrlen = 8;
274 		} else if (nexthdr == NEXTHDR_AUTH) {
275 			if (flags && (*flags & IP6_FH_F_AUTH) && (target < 0))
276 				break;
277 			hdrlen = ipv6_authlen(hp);
278 		} else
279 			hdrlen = ipv6_optlen(hp);
280 
281 		if (!found) {
282 			if (skb->len - start < hdrlen)
283 				return -EBADMSG;
284 
285 			nexthdr = hp->nexthdr;
286 			start += hdrlen;
287 		}
288 	} while (!found);
289 
290 	*offset = start;
291 	return nexthdr;
292 }
293 EXPORT_SYMBOL(ipv6_find_hdr);
294