This source file includes following definitions.
- ipv6_ext_hdr
- ipv6_skip_exthdr
- ipv6_find_tlv
- ipv6_find_hdr
1
2
3
4
5
6 #include <linux/export.h>
7 #include <net/ipv6.h>
8
9
10
11
12
13 bool ipv6_ext_hdr(u8 nexthdr)
14 {
15
16
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
77 *frag_offp = 0;
78
79 while (ipv6_ext_hdr(nexthdr)) {
80 struct ipv6_opt_hdr _hdr, *hp;
81 int hdrlen;
82
83 if (nexthdr == NEXTHDR_NONE)
84 return -1;
85 hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);
86 if (!hp)
87 return -1;
88 if (nexthdr == NEXTHDR_FRAGMENT) {
89 __be16 _frag_off, *fp;
90 fp = skb_header_pointer(skb,
91 start+offsetof(struct frag_hdr,
92 frag_off),
93 sizeof(_frag_off),
94 &_frag_off);
95 if (!fp)
96 return -1;
97
98 *frag_offp = *fp;
99 if (ntohs(*frag_offp) & ~0x7)
100 break;
101 hdrlen = 8;
102 } else if (nexthdr == NEXTHDR_AUTH)
103 hdrlen = ipv6_authlen(hp);
104 else
105 hdrlen = ipv6_optlen(hp);
106
107 nexthdr = hp->nexthdr;
108 start += hdrlen;
109 }
110
111 *nexthdrp = nexthdr;
112 return start;
113 }
114 EXPORT_SYMBOL(ipv6_skip_exthdr);
115
116 int ipv6_find_tlv(const struct sk_buff *skb, int offset, int type)
117 {
118 const unsigned char *nh = skb_network_header(skb);
119 int packet_len = skb_tail_pointer(skb) - skb_network_header(skb);
120 struct ipv6_opt_hdr *hdr;
121 int len;
122
123 if (offset + 2 > packet_len)
124 goto bad;
125 hdr = (struct ipv6_opt_hdr *)(nh + offset);
126 len = ((hdr->hdrlen + 1) << 3);
127
128 if (offset + len > packet_len)
129 goto bad;
130
131 offset += 2;
132 len -= 2;
133
134 while (len > 0) {
135 int opttype = nh[offset];
136 int optlen;
137
138 if (opttype == type)
139 return offset;
140
141 switch (opttype) {
142 case IPV6_TLV_PAD1:
143 optlen = 1;
144 break;
145 default:
146 optlen = nh[offset + 1] + 2;
147 if (optlen > len)
148 goto bad;
149 break;
150 }
151 offset += optlen;
152 len -= optlen;
153 }
154
155 bad:
156 return -1;
157 }
158 EXPORT_SYMBOL_GPL(ipv6_find_tlv);
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186 int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset,
187 int target, unsigned short *fragoff, int *flags)
188 {
189 unsigned int start = skb_network_offset(skb) + sizeof(struct ipv6hdr);
190 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
191 bool found;
192
193 if (fragoff)
194 *fragoff = 0;
195
196 if (*offset) {
197 struct ipv6hdr _ip6, *ip6;
198
199 ip6 = skb_header_pointer(skb, *offset, sizeof(_ip6), &_ip6);
200 if (!ip6 || (ip6->version != 6))
201 return -EBADMSG;
202 start = *offset + sizeof(struct ipv6hdr);
203 nexthdr = ip6->nexthdr;
204 }
205
206 do {
207 struct ipv6_opt_hdr _hdr, *hp;
208 unsigned int hdrlen;
209 found = (nexthdr == target);
210
211 if ((!ipv6_ext_hdr(nexthdr)) || nexthdr == NEXTHDR_NONE) {
212 if (target < 0 || found)
213 break;
214 return -ENOENT;
215 }
216
217 hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);
218 if (!hp)
219 return -EBADMSG;
220
221 if (nexthdr == NEXTHDR_ROUTING) {
222 struct ipv6_rt_hdr _rh, *rh;
223
224 rh = skb_header_pointer(skb, start, sizeof(_rh),
225 &_rh);
226 if (!rh)
227 return -EBADMSG;
228
229 if (flags && (*flags & IP6_FH_F_SKIP_RH) &&
230 rh->segments_left == 0)
231 found = false;
232 }
233
234 if (nexthdr == NEXTHDR_FRAGMENT) {
235 unsigned short _frag_off;
236 __be16 *fp;
237
238 if (flags)
239 *flags |= IP6_FH_F_FRAG;
240 fp = skb_header_pointer(skb,
241 start+offsetof(struct frag_hdr,
242 frag_off),
243 sizeof(_frag_off),
244 &_frag_off);
245 if (!fp)
246 return -EBADMSG;
247
248 _frag_off = ntohs(*fp) & ~0x7;
249 if (_frag_off) {
250 if (target < 0 &&
251 ((!ipv6_ext_hdr(hp->nexthdr)) ||
252 hp->nexthdr == NEXTHDR_NONE)) {
253 if (fragoff)
254 *fragoff = _frag_off;
255 return hp->nexthdr;
256 }
257 if (!found)
258 return -ENOENT;
259 if (fragoff)
260 *fragoff = _frag_off;
261 break;
262 }
263 hdrlen = 8;
264 } else if (nexthdr == NEXTHDR_AUTH) {
265 if (flags && (*flags & IP6_FH_F_AUTH) && (target < 0))
266 break;
267 hdrlen = ipv6_authlen(hp);
268 } else
269 hdrlen = ipv6_optlen(hp);
270
271 if (!found) {
272 nexthdr = hp->nexthdr;
273 start += hdrlen;
274 }
275 } while (!found);
276
277 *offset = start;
278 return nexthdr;
279 }
280 EXPORT_SYMBOL(ipv6_find_hdr);