blob: 8ea253ad35b1762e4b30dfa7e20ea71d092bc642 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 library code, needed by static components when full IPv6 support is
3 * not configured or static.
4 */
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -04005#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <net/ipv6.h>
7
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09008/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * find out if nexthdr is a well-known extension header or a protocol
10 */
11
Eric Dumazeta50feda2012-05-18 18:57:34 +000012bool ipv6_ext_hdr(u8 nexthdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090014 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * find out if nexthdr is an extension header or a protocol
16 */
Eric Dumazeta02cec22010-09-22 20:43:57 +000017 return (nexthdr == NEXTHDR_HOP) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 (nexthdr == NEXTHDR_ROUTING) ||
19 (nexthdr == NEXTHDR_FRAGMENT) ||
20 (nexthdr == NEXTHDR_AUTH) ||
21 (nexthdr == NEXTHDR_NONE) ||
Eric Dumazeta02cec22010-09-22 20:43:57 +000022 (nexthdr == NEXTHDR_DEST);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023}
Eldad Zack81bd60b2012-04-01 07:49:05 +000024EXPORT_SYMBOL(ipv6_ext_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/*
27 * Skip any extension headers. This is used by the ICMP module.
28 *
29 * Note that strictly speaking this conflicts with RFC 2460 4.0:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090030 * ...The contents and semantics of each extension header determine whether
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * or not to proceed to the next header. Therefore, extension headers must
32 * be processed strictly in the order they appear in the packet; a
33 * receiver must not, for example, scan through a packet looking for a
34 * particular kind of extension header and process that header prior to
35 * processing all preceding ones.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090036 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * We do exactly this. This is a protocol bug. We can't decide after a
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090038 * seeing an unknown discard-with-error flavour TLV option if it's a
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * ICMP error message or not (errors should never be send in reply to
40 * ICMP error messages).
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090041 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * But I see no other way to do this. This might need to be reexamined
43 * when Linux implements ESP (and maybe AUTH) headers.
44 * --AK
45 *
Herbert Xu0d3d0772005-04-24 20:16:19 -070046 * This function parses (probably truncated) exthdr set "hdr".
47 * "nexthdrp" initially points to some place,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * where type of the first header can be found.
49 *
50 * It skips all well-known exthdrs, and returns pointer to the start
51 * of unparsable area i.e. the first header with unknown type.
52 * If it is not NULL *nexthdr is updated by type/protocol of this header.
53 *
54 * NOTES: - if packet terminated with NEXTHDR_NONE it returns NULL.
55 * - it may return pointer pointing beyond end of packet,
56 * if the last recognized header is truncated in the middle.
57 * - if packet is truncated, so that all parsed headers are skipped,
58 * it returns NULL.
59 * - First fragment header is skipped, not-first ones
60 * are considered as unparsable.
Jesse Gross75f28112011-11-30 17:05:51 -080061 * - Reports the offset field of the final fragment header so it is
62 * possible to tell whether this is a first fragment, later fragment,
63 * or not fragmented.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * - ESP is unparsable for now and considered like
65 * normal payload protocol.
66 * - Note also special handling of AUTH header. Thanks to IPsec wizards.
67 *
68 * --ANK (980726)
69 */
70
Jesse Gross75f28112011-11-30 17:05:51 -080071int ipv6_skip_exthdr(const struct sk_buff *skb, int start, u8 *nexthdrp,
72 __be16 *frag_offp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 u8 nexthdr = *nexthdrp;
75
Jesse Gross75f28112011-11-30 17:05:51 -080076 *frag_offp = 0;
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 while (ipv6_ext_hdr(nexthdr)) {
79 struct ipv6_opt_hdr _hdr, *hp;
80 int hdrlen;
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (nexthdr == NEXTHDR_NONE)
83 return -1;
84 hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);
85 if (hp == NULL)
Herbert Xu0d3d0772005-04-24 20:16:19 -070086 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (nexthdr == NEXTHDR_FRAGMENT) {
Al Viroe69a4adc2006-11-14 20:56:00 -080088 __be16 _frag_off, *fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 fp = skb_header_pointer(skb,
90 start+offsetof(struct frag_hdr,
91 frag_off),
92 sizeof(_frag_off),
93 &_frag_off);
94 if (fp == NULL)
95 return -1;
96
Jesse Gross75f28112011-11-30 17:05:51 -080097 *frag_offp = *fp;
98 if (ntohs(*frag_offp) & ~0x7)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 break;
100 hdrlen = 8;
101 } else if (nexthdr == NEXTHDR_AUTH)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900102 hdrlen = (hp->hdrlen+2)<<2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 else
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900104 hdrlen = ipv6_optlen(hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 nexthdr = hp->nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 start += hdrlen;
108 }
109
110 *nexthdrp = nexthdr;
111 return start;
112}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113EXPORT_SYMBOL(ipv6_skip_exthdr);
Jesse Grossf8f62672012-11-09 17:05:07 -0800114
115/*
116 * find the offset to specified header or the protocol number of last header
117 * if target < 0. "last header" is transport protocol header, ESP, or
118 * "No next header".
119 *
120 * Note that *offset is used as input/output parameter. an if it is not zero,
121 * then it must be a valid offset to an inner IPv6 header. This can be used
122 * to explore inner IPv6 header, eg. ICMPv6 error messages.
123 *
124 * If target header is found, its offset is set in *offset and return protocol
125 * number. Otherwise, return -1.
126 *
127 * If the first fragment doesn't contain the final protocol header or
128 * NEXTHDR_NONE it is considered invalid.
129 *
130 * Note that non-1st fragment is special case that "the protocol number
131 * of last header" is "next header" field in Fragment header. In this case,
132 * *offset is meaningless and fragment offset is stored in *fragoff if fragoff
133 * isn't NULL.
134 *
135 * if flags is not NULL and it's a fragment, then the frag flag IP6_FH_F_FRAG
136 * will be set. If it's an AH header, the IP6_FH_F_AUTH flag is set and
137 * target < 0, then this function will stop at the AH header.
138 */
139int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset,
140 int target, unsigned short *fragoff, int *flags)
141{
142 unsigned int start = skb_network_offset(skb) + sizeof(struct ipv6hdr);
143 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
144 unsigned int len;
145
146 if (fragoff)
147 *fragoff = 0;
148
149 if (*offset) {
150 struct ipv6hdr _ip6, *ip6;
151
152 ip6 = skb_header_pointer(skb, *offset, sizeof(_ip6), &_ip6);
153 if (!ip6 || (ip6->version != 6)) {
154 printk(KERN_ERR "IPv6 header not found\n");
155 return -EBADMSG;
156 }
157 start = *offset + sizeof(struct ipv6hdr);
158 nexthdr = ip6->nexthdr;
159 }
160 len = skb->len - start;
161
162 while (nexthdr != target) {
163 struct ipv6_opt_hdr _hdr, *hp;
164 unsigned int hdrlen;
165
166 if ((!ipv6_ext_hdr(nexthdr)) || nexthdr == NEXTHDR_NONE) {
167 if (target < 0)
168 break;
169 return -ENOENT;
170 }
171
172 hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);
173 if (hp == NULL)
174 return -EBADMSG;
175 if (nexthdr == NEXTHDR_FRAGMENT) {
176 unsigned short _frag_off;
177 __be16 *fp;
178
179 if (flags) /* Indicate that this is a fragment */
180 *flags |= IP6_FH_F_FRAG;
181 fp = skb_header_pointer(skb,
182 start+offsetof(struct frag_hdr,
183 frag_off),
184 sizeof(_frag_off),
185 &_frag_off);
186 if (fp == NULL)
187 return -EBADMSG;
188
189 _frag_off = ntohs(*fp) & ~0x7;
190 if (_frag_off) {
191 if (target < 0 &&
192 ((!ipv6_ext_hdr(hp->nexthdr)) ||
193 hp->nexthdr == NEXTHDR_NONE)) {
194 if (fragoff)
195 *fragoff = _frag_off;
196 return hp->nexthdr;
197 }
198 return -ENOENT;
199 }
200 hdrlen = 8;
201 } else if (nexthdr == NEXTHDR_AUTH) {
202 if (flags && (*flags & IP6_FH_F_AUTH) && (target < 0))
203 break;
204 hdrlen = (hp->hdrlen + 2) << 2;
205 } else
206 hdrlen = ipv6_optlen(hp);
207
208 nexthdr = hp->nexthdr;
209 len -= hdrlen;
210 start += hdrlen;
211 }
212
213 *offset = start;
214 return nexthdr;
215}
216EXPORT_SYMBOL(ipv6_find_hdr);