ipv6: improve ipv6_find_hdr() to skip empty routing headers
[deliverable/linux.git] / net / ipv6 / exthdrs_core.c
1 /*
2 * IPv6 library code, needed by static components when full IPv6 support is
3 * not configured or static.
4 */
5 #include <linux/export.h>
6 #include <net/ipv6.h>
7
8 /*
9 * find out if nexthdr is a well-known extension header or a protocol
10 */
11
12 bool ipv6_ext_hdr(u8 nexthdr)
13 {
14 /*
15 * find out if nexthdr is an extension header or a protocol
16 */
17 return (nexthdr == NEXTHDR_HOP) ||
18 (nexthdr == NEXTHDR_ROUTING) ||
19 (nexthdr == NEXTHDR_FRAGMENT) ||
20 (nexthdr == NEXTHDR_AUTH) ||
21 (nexthdr == NEXTHDR_NONE) ||
22 (nexthdr == NEXTHDR_DEST);
23 }
24 EXPORT_SYMBOL(ipv6_ext_hdr);
25
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:
30 * ...The contents and semantics of each extension header determine whether
31 * 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.
36 *
37 * We do exactly this. This is a protocol bug. We can't decide after a
38 * seeing an unknown discard-with-error flavour TLV option if it's a
39 * ICMP error message or not (errors should never be send in reply to
40 * ICMP error messages).
41 *
42 * 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 *
46 * This function parses (probably truncated) exthdr set "hdr".
47 * "nexthdrp" initially points to some place,
48 * 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.
61 * - 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.
64 * - 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
71 int ipv6_skip_exthdr(const struct sk_buff *skb, int start, u8 *nexthdrp,
72 __be16 *frag_offp)
73 {
74 u8 nexthdr = *nexthdrp;
75
76 *frag_offp = 0;
77
78 while (ipv6_ext_hdr(nexthdr)) {
79 struct ipv6_opt_hdr _hdr, *hp;
80 int hdrlen;
81
82 if (nexthdr == NEXTHDR_NONE)
83 return -1;
84 hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);
85 if (hp == NULL)
86 return -1;
87 if (nexthdr == NEXTHDR_FRAGMENT) {
88 __be16 _frag_off, *fp;
89 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
97 *frag_offp = *fp;
98 if (ntohs(*frag_offp) & ~0x7)
99 break;
100 hdrlen = 8;
101 } else if (nexthdr == NEXTHDR_AUTH)
102 hdrlen = (hp->hdrlen+2)<<2;
103 else
104 hdrlen = ipv6_optlen(hp);
105
106 nexthdr = hp->nexthdr;
107 start += hdrlen;
108 }
109
110 *nexthdrp = nexthdr;
111 return start;
112 }
113 EXPORT_SYMBOL(ipv6_skip_exthdr);
114
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
136 * IP6_FH_F_FRAG will be set. If it's an AH header, the
137 * IP6_FH_F_AUTH flag is set and target < 0, then this function will
138 * stop at the AH header. If IP6_FH_F_SKIP_RH flag was passed, then this
139 * function will skip all those routing headers, where segements_left was 0.
140 */
141 int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset,
142 int target, unsigned short *fragoff, int *flags)
143 {
144 unsigned int start = skb_network_offset(skb) + sizeof(struct ipv6hdr);
145 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
146 unsigned int len;
147 bool found;
148
149 if (fragoff)
150 *fragoff = 0;
151
152 if (*offset) {
153 struct ipv6hdr _ip6, *ip6;
154
155 ip6 = skb_header_pointer(skb, *offset, sizeof(_ip6), &_ip6);
156 if (!ip6 || (ip6->version != 6)) {
157 printk(KERN_ERR "IPv6 header not found\n");
158 return -EBADMSG;
159 }
160 start = *offset + sizeof(struct ipv6hdr);
161 nexthdr = ip6->nexthdr;
162 }
163 len = skb->len - start;
164
165 do {
166 struct ipv6_opt_hdr _hdr, *hp;
167 unsigned int hdrlen;
168 found = (nexthdr == target);
169
170 if ((!ipv6_ext_hdr(nexthdr)) || nexthdr == NEXTHDR_NONE) {
171 if (target < 0)
172 break;
173 return -ENOENT;
174 }
175
176 hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);
177 if (hp == NULL)
178 return -EBADMSG;
179
180 if (nexthdr == NEXTHDR_ROUTING) {
181 struct ipv6_rt_hdr _rh, *rh;
182
183 rh = skb_header_pointer(skb, start, sizeof(_rh),
184 &_rh);
185 if (rh == NULL)
186 return -EBADMSG;
187
188 if (flags && (*flags & IP6_FH_F_SKIP_RH) &&
189 rh->segments_left == 0)
190 found = false;
191 }
192
193 if (nexthdr == NEXTHDR_FRAGMENT) {
194 unsigned short _frag_off;
195 __be16 *fp;
196
197 if (flags) /* Indicate that this is a fragment */
198 *flags |= IP6_FH_F_FRAG;
199 fp = skb_header_pointer(skb,
200 start+offsetof(struct frag_hdr,
201 frag_off),
202 sizeof(_frag_off),
203 &_frag_off);
204 if (fp == NULL)
205 return -EBADMSG;
206
207 _frag_off = ntohs(*fp) & ~0x7;
208 if (_frag_off) {
209 if (target < 0 &&
210 ((!ipv6_ext_hdr(hp->nexthdr)) ||
211 hp->nexthdr == NEXTHDR_NONE)) {
212 if (fragoff)
213 *fragoff = _frag_off;
214 return hp->nexthdr;
215 }
216 return -ENOENT;
217 }
218 hdrlen = 8;
219 } else if (nexthdr == NEXTHDR_AUTH) {
220 if (flags && (*flags & IP6_FH_F_AUTH) && (target < 0))
221 break;
222 hdrlen = (hp->hdrlen + 2) << 2;
223 } else
224 hdrlen = ipv6_optlen(hp);
225
226 if (!found) {
227 nexthdr = hp->nexthdr;
228 len -= hdrlen;
229 start += hdrlen;
230 }
231 } while (!found);
232
233 *offset = start;
234 return nexthdr;
235 }
236 EXPORT_SYMBOL(ipv6_find_hdr);
This page took 0.15994 seconds and 6 git commands to generate.