exthdrs_core.c 3.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
/*
 * IPv6 library code, needed by static components when full IPv6 support is
 * not configured or static.
 */
#include <net/ipv6.h>

7
/*
L
Linus Torvalds 已提交
8 9 10 11 12
 * find out if nexthdr is a well-known extension header or a protocol
 */

int ipv6_ext_hdr(u8 nexthdr)
{
13
	/*
L
Linus Torvalds 已提交
14 15
	 * find out if nexthdr is an extension header or a protocol
	 */
E
Eric Dumazet 已提交
16
	return   (nexthdr == NEXTHDR_HOP)	||
L
Linus Torvalds 已提交
17 18 19 20
		 (nexthdr == NEXTHDR_ROUTING)	||
		 (nexthdr == NEXTHDR_FRAGMENT)	||
		 (nexthdr == NEXTHDR_AUTH)	||
		 (nexthdr == NEXTHDR_NONE)	||
E
Eric Dumazet 已提交
21
		 (nexthdr == NEXTHDR_DEST);
L
Linus Torvalds 已提交
22 23 24 25 26 27
}

/*
 * Skip any extension headers. This is used by the ICMP module.
 *
 * Note that strictly speaking this conflicts with RFC 2460 4.0:
28
 * ...The contents and semantics of each extension header determine whether
L
Linus Torvalds 已提交
29 30 31 32 33
 * or not to proceed to the next header.  Therefore, extension headers must
 * be processed strictly in the order they appear in the packet; a
 * receiver must not, for example, scan through a packet looking for a
 * particular kind of extension header and process that header prior to
 * processing all preceding ones.
34
 *
L
Linus Torvalds 已提交
35
 * We do exactly this. This is a protocol bug. We can't decide after a
36
 * seeing an unknown discard-with-error flavour TLV option if it's a
L
Linus Torvalds 已提交
37 38
 * ICMP error message or not (errors should never be send in reply to
 * ICMP error messages).
39
 *
L
Linus Torvalds 已提交
40 41 42 43
 * But I see no other way to do this. This might need to be reexamined
 * when Linux implements ESP (and maybe AUTH) headers.
 * --AK
 *
44 45
 * This function parses (probably truncated) exthdr set "hdr".
 * "nexthdrp" initially points to some place,
L
Linus Torvalds 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
 * where type of the first header can be found.
 *
 * It skips all well-known exthdrs, and returns pointer to the start
 * of unparsable area i.e. the first header with unknown type.
 * If it is not NULL *nexthdr is updated by type/protocol of this header.
 *
 * NOTES: - if packet terminated with NEXTHDR_NONE it returns NULL.
 *        - it may return pointer pointing beyond end of packet,
 *	    if the last recognized header is truncated in the middle.
 *        - if packet is truncated, so that all parsed headers are skipped,
 *	    it returns NULL.
 *	  - First fragment header is skipped, not-first ones
 *	    are considered as unparsable.
 *	  - ESP is unparsable for now and considered like
 *	    normal payload protocol.
 *	  - Note also special handling of AUTH header. Thanks to IPsec wizards.
 *
 * --ANK (980726)
 */

66
int ipv6_skip_exthdr(const struct sk_buff *skb, int start, u8 *nexthdrp)
L
Linus Torvalds 已提交
67 68 69 70 71 72 73 74 75 76 77
{
	u8 nexthdr = *nexthdrp;

	while (ipv6_ext_hdr(nexthdr)) {
		struct ipv6_opt_hdr _hdr, *hp;
		int hdrlen;

		if (nexthdr == NEXTHDR_NONE)
			return -1;
		hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);
		if (hp == NULL)
78
			return -1;
L
Linus Torvalds 已提交
79
		if (nexthdr == NEXTHDR_FRAGMENT) {
A
Al Viro 已提交
80
			__be16 _frag_off, *fp;
L
Linus Torvalds 已提交
81 82 83 84 85 86 87 88 89 90 91 92
			fp = skb_header_pointer(skb,
						start+offsetof(struct frag_hdr,
							       frag_off),
						sizeof(_frag_off),
						&_frag_off);
			if (fp == NULL)
				return -1;

			if (ntohs(*fp) & ~0x7)
				break;
			hdrlen = 8;
		} else if (nexthdr == NEXTHDR_AUTH)
93
			hdrlen = (hp->hdrlen+2)<<2;
L
Linus Torvalds 已提交
94
		else
95
			hdrlen = ipv6_optlen(hp);
L
Linus Torvalds 已提交
96 97 98 99 100 101 102 103 104 105 106

		nexthdr = hp->nexthdr;
		start += hdrlen;
	}

	*nexthdrp = nexthdr;
	return start;
}

EXPORT_SYMBOL(ipv6_ext_hdr);
EXPORT_SYMBOL(ipv6_skip_exthdr);