Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * INET An implementation of the TCP/IP protocol suite for the LINUX | |
4 | * operating system. INET is implemented using the BSD Socket | |
5 | * interface as the means of communication with the user level. | |
6 | * | |
7 | * The options processing module for ip.c | |
8 | * | |
1da177e4 | 9 | * Authors: A.N.Kuznetsov |
e905a9ed | 10 | * |
1da177e4 LT |
11 | */ |
12 | ||
afd46503 JP |
13 | #define pr_fmt(fmt) "IPv4: " fmt |
14 | ||
4fc268d2 | 15 | #include <linux/capability.h> |
1da177e4 | 16 | #include <linux/module.h> |
5a0e3ad6 | 17 | #include <linux/slab.h> |
1da177e4 | 18 | #include <linux/types.h> |
7c0f6ba6 | 19 | #include <linux/uaccess.h> |
48bdf072 | 20 | #include <asm/unaligned.h> |
1da177e4 LT |
21 | #include <linux/skbuff.h> |
22 | #include <linux/ip.h> | |
23 | #include <linux/icmp.h> | |
24 | #include <linux/netdevice.h> | |
25 | #include <linux/rtnetlink.h> | |
26 | #include <net/sock.h> | |
27 | #include <net/ip.h> | |
28 | #include <net/icmp.h> | |
14c85021 | 29 | #include <net/route.h> |
11a03f78 | 30 | #include <net/cipso_ipv4.h> |
35ebf65e | 31 | #include <net/ip_fib.h> |
1da177e4 | 32 | |
e905a9ed | 33 | /* |
1da177e4 LT |
34 | * Write options to IP header, record destination address to |
35 | * source route option, address of outgoing interface | |
36 | * (we should already know it, so that this function is allowed be | |
37 | * called only after routing decision) and timestamp, | |
38 | * if we originate this datagram. | |
39 | * | |
40 | * daddr is real destination address, next hop is recorded in IP header. | |
41 | * saddr is address of outgoing interface. | |
42 | */ | |
43 | ||
f6d8bd05 | 44 | void ip_options_build(struct sk_buff *skb, struct ip_options *opt, |
4f0e3040 | 45 | __be32 daddr, struct rtable *rt) |
1da177e4 | 46 | { |
d56f90a7 | 47 | unsigned char *iph = skb_network_header(skb); |
1da177e4 LT |
48 | |
49 | memcpy(&(IPCB(skb)->opt), opt, sizeof(struct ip_options)); | |
343d8c60 | 50 | memcpy(iph + sizeof(struct iphdr), opt->__data, opt->optlen); |
1da177e4 | 51 | opt = &(IPCB(skb)->opt); |
1da177e4 LT |
52 | |
53 | if (opt->srr) | |
343d8c60 | 54 | memcpy(iph + opt->srr + iph[opt->srr + 1] - 4, &daddr, 4); |
1da177e4 | 55 | |
4f0e3040 JK |
56 | if (opt->rr_needaddr) |
57 | ip_rt_get_source(iph + opt->rr + iph[opt->rr + 2] - 5, skb, rt); | |
58 | if (opt->ts_needaddr) | |
59 | ip_rt_get_source(iph + opt->ts + iph[opt->ts + 2] - 9, skb, rt); | |
60 | if (opt->ts_needtime) { | |
61 | __be32 midtime; | |
822c8685 | 62 | |
4f0e3040 JK |
63 | midtime = inet_current_timestamp(); |
64 | memcpy(iph + opt->ts + iph[opt->ts + 2] - 5, &midtime, 4); | |
1da177e4 LT |
65 | } |
66 | } | |
67 | ||
e905a9ed | 68 | /* |
1da177e4 LT |
69 | * Provided (sopt, skb) points to received options, |
70 | * build in dopt compiled option set appropriate for answering. | |
71 | * i.e. invert SRR option, copy anothers, | |
72 | * and grab room in RR/TS options. | |
73 | * | |
74 | * NOTE: dopt cannot point to skb. | |
75 | */ | |
76 | ||
91ed1e66 PA |
77 | int __ip_options_echo(struct net *net, struct ip_options *dopt, |
78 | struct sk_buff *skb, const struct ip_options *sopt) | |
1da177e4 | 79 | { |
1da177e4 LT |
80 | unsigned char *sptr, *dptr; |
81 | int soffset, doffset; | |
82 | int optlen; | |
1da177e4 LT |
83 | |
84 | memset(dopt, 0, sizeof(struct ip_options)); | |
85 | ||
f6d8bd05 | 86 | if (sopt->optlen == 0) |
1da177e4 | 87 | return 0; |
1da177e4 | 88 | |
d56f90a7 | 89 | sptr = skb_network_header(skb); |
1da177e4 LT |
90 | dptr = dopt->__data; |
91 | ||
1da177e4 LT |
92 | if (sopt->rr) { |
93 | optlen = sptr[sopt->rr+1]; | |
94 | soffset = sptr[sopt->rr+2]; | |
95 | dopt->rr = dopt->optlen + sizeof(struct iphdr); | |
96 | memcpy(dptr, sptr+sopt->rr, optlen); | |
97 | if (sopt->rr_needaddr && soffset <= optlen) { | |
98 | if (soffset + 3 > optlen) | |
99 | return -EINVAL; | |
100 | dptr[2] = soffset + 4; | |
101 | dopt->rr_needaddr = 1; | |
102 | } | |
103 | dptr += optlen; | |
104 | dopt->optlen += optlen; | |
105 | } | |
106 | if (sopt->ts) { | |
107 | optlen = sptr[sopt->ts+1]; | |
108 | soffset = sptr[sopt->ts+2]; | |
109 | dopt->ts = dopt->optlen + sizeof(struct iphdr); | |
110 | memcpy(dptr, sptr+sopt->ts, optlen); | |
111 | if (soffset <= optlen) { | |
112 | if (sopt->ts_needaddr) { | |
113 | if (soffset + 3 > optlen) | |
114 | return -EINVAL; | |
115 | dopt->ts_needaddr = 1; | |
116 | soffset += 4; | |
117 | } | |
118 | if (sopt->ts_needtime) { | |
119 | if (soffset + 3 > optlen) | |
120 | return -EINVAL; | |
121 | if ((dptr[3]&0xF) != IPOPT_TS_PRESPEC) { | |
122 | dopt->ts_needtime = 1; | |
123 | soffset += 4; | |
124 | } else { | |
125 | dopt->ts_needtime = 0; | |
126 | ||
8628bd8a | 127 | if (soffset + 7 <= optlen) { |
fd683222 | 128 | __be32 addr; |
1da177e4 | 129 | |
8628bd8a | 130 | memcpy(&addr, dptr+soffset-1, 4); |
91ed1e66 | 131 | if (inet_addr_type(net, addr) != RTN_UNICAST) { |
1da177e4 LT |
132 | dopt->ts_needtime = 1; |
133 | soffset += 8; | |
134 | } | |
135 | } | |
136 | } | |
137 | } | |
138 | dptr[2] = soffset; | |
139 | } | |
140 | dptr += optlen; | |
141 | dopt->optlen += optlen; | |
142 | } | |
143 | if (sopt->srr) { | |
f6d8bd05 | 144 | unsigned char *start = sptr+sopt->srr; |
3ca3c68e | 145 | __be32 faddr; |
1da177e4 LT |
146 | |
147 | optlen = start[1]; | |
148 | soffset = start[2]; | |
149 | doffset = 0; | |
150 | if (soffset > optlen) | |
151 | soffset = optlen + 1; | |
152 | soffset -= 4; | |
153 | if (soffset > 3) { | |
154 | memcpy(&faddr, &start[soffset-1], 4); | |
a22318e8 | 155 | for (soffset -= 4, doffset = 4; soffset > 3; soffset -= 4, doffset += 4) |
1da177e4 LT |
156 | memcpy(&dptr[doffset-1], &start[soffset-1], 4); |
157 | /* | |
158 | * RFC1812 requires to fix illegal source routes. | |
159 | */ | |
eddc9ec5 ACM |
160 | if (memcmp(&ip_hdr(skb)->saddr, |
161 | &start[soffset + 3], 4) == 0) | |
1da177e4 LT |
162 | doffset -= 4; |
163 | } | |
164 | if (doffset > 3) { | |
1da177e4 LT |
165 | dopt->faddr = faddr; |
166 | dptr[0] = start[0]; | |
167 | dptr[1] = doffset+3; | |
168 | dptr[2] = 4; | |
169 | dptr += doffset+3; | |
170 | dopt->srr = dopt->optlen + sizeof(struct iphdr); | |
171 | dopt->optlen += doffset+3; | |
172 | dopt->is_strictroute = sopt->is_strictroute; | |
173 | } | |
174 | } | |
11a03f78 PM |
175 | if (sopt->cipso) { |
176 | optlen = sptr[sopt->cipso+1]; | |
177 | dopt->cipso = dopt->optlen+sizeof(struct iphdr); | |
178 | memcpy(dptr, sptr+sopt->cipso, optlen); | |
179 | dptr += optlen; | |
180 | dopt->optlen += optlen; | |
181 | } | |
1da177e4 LT |
182 | while (dopt->optlen & 3) { |
183 | *dptr++ = IPOPT_END; | |
184 | dopt->optlen++; | |
185 | } | |
186 | return 0; | |
187 | } | |
188 | ||
189 | /* | |
190 | * Options "fragmenting", just fill options not | |
191 | * allowed in fragments with NOOPs. | |
192 | * Simple and stupid 8), but the most efficient way. | |
193 | */ | |
194 | ||
5e73ea1a | 195 | void ip_options_fragment(struct sk_buff *skb) |
1da177e4 | 196 | { |
d56f90a7 | 197 | unsigned char *optptr = skb_network_header(skb) + sizeof(struct iphdr); |
5e73ea1a | 198 | struct ip_options *opt = &(IPCB(skb)->opt); |
1da177e4 LT |
199 | int l = opt->optlen; |
200 | int optlen; | |
201 | ||
202 | while (l > 0) { | |
203 | switch (*optptr) { | |
204 | case IPOPT_END: | |
205 | return; | |
206 | case IPOPT_NOOP: | |
207 | l--; | |
208 | optptr++; | |
209 | continue; | |
210 | } | |
211 | optlen = optptr[1]; | |
a22318e8 | 212 | if (optlen < 2 || optlen > l) |
1da177e4 LT |
213 | return; |
214 | if (!IPOPT_COPIED(*optptr)) | |
215 | memset(optptr, IPOPT_NOOP, optlen); | |
216 | l -= optlen; | |
217 | optptr += optlen; | |
218 | } | |
219 | opt->ts = 0; | |
220 | opt->rr = 0; | |
221 | opt->rr_needaddr = 0; | |
222 | opt->ts_needaddr = 0; | |
223 | opt->ts_needtime = 0; | |
1da177e4 LT |
224 | } |
225 | ||
bf5e53e3 ED |
226 | /* helper used by ip_options_compile() to call fib_compute_spec_dst() |
227 | * at most one time. | |
228 | */ | |
229 | static void spec_dst_fill(__be32 *spec_dst, struct sk_buff *skb) | |
230 | { | |
231 | if (*spec_dst == htonl(INADDR_ANY)) | |
232 | *spec_dst = fib_compute_spec_dst(skb); | |
233 | } | |
234 | ||
1da177e4 LT |
235 | /* |
236 | * Verify options and fill pointers in struct options. | |
237 | * Caller should clear *opt, and set opt->data. | |
238 | * If opt == NULL, then skb->data should point to IP header. | |
239 | */ | |
240 | ||
3da1ed7a NS |
241 | int __ip_options_compile(struct net *net, |
242 | struct ip_options *opt, struct sk_buff *skb, | |
243 | __be32 *info) | |
1da177e4 | 244 | { |
bf5e53e3 | 245 | __be32 spec_dst = htonl(INADDR_ANY); |
5e73ea1a | 246 | unsigned char *pp_ptr = NULL; |
11604721 | 247 | struct rtable *rt = NULL; |
35ebf65e DM |
248 | unsigned char *optptr; |
249 | unsigned char *iph; | |
250 | int optlen, l; | |
1da177e4 | 251 | |
00db4124 | 252 | if (skb) { |
11604721 | 253 | rt = skb_rtable(skb); |
22aba383 DL |
254 | optptr = (unsigned char *)&(ip_hdr(skb)[1]); |
255 | } else | |
10fe7d85 | 256 | optptr = opt->__data; |
22aba383 | 257 | iph = optptr - sizeof(struct iphdr); |
1da177e4 LT |
258 | |
259 | for (l = opt->optlen; l > 0; ) { | |
260 | switch (*optptr) { | |
dd9b4559 | 261 | case IPOPT_END: |
a22318e8 | 262 | for (optptr++, l--; l > 0; optptr++, l--) { |
1da177e4 LT |
263 | if (*optptr != IPOPT_END) { |
264 | *optptr = IPOPT_END; | |
265 | opt->is_changed = 1; | |
266 | } | |
267 | } | |
268 | goto eol; | |
dd9b4559 | 269 | case IPOPT_NOOP: |
1da177e4 LT |
270 | l--; |
271 | optptr++; | |
272 | continue; | |
273 | } | |
10ec9472 ED |
274 | if (unlikely(l < 2)) { |
275 | pp_ptr = optptr; | |
276 | goto error; | |
277 | } | |
1da177e4 | 278 | optlen = optptr[1]; |
a22318e8 | 279 | if (optlen < 2 || optlen > l) { |
1da177e4 LT |
280 | pp_ptr = optptr; |
281 | goto error; | |
282 | } | |
283 | switch (*optptr) { | |
dd9b4559 WC |
284 | case IPOPT_SSRR: |
285 | case IPOPT_LSRR: | |
1da177e4 LT |
286 | if (optlen < 3) { |
287 | pp_ptr = optptr + 1; | |
288 | goto error; | |
289 | } | |
290 | if (optptr[2] < 4) { | |
291 | pp_ptr = optptr + 2; | |
292 | goto error; | |
293 | } | |
294 | /* NB: cf RFC-1812 5.2.4.1 */ | |
295 | if (opt->srr) { | |
296 | pp_ptr = optptr; | |
297 | goto error; | |
298 | } | |
299 | if (!skb) { | |
300 | if (optptr[2] != 4 || optlen < 7 || ((optlen-3) & 3)) { | |
301 | pp_ptr = optptr + 1; | |
302 | goto error; | |
303 | } | |
304 | memcpy(&opt->faddr, &optptr[3], 4); | |
305 | if (optlen > 7) | |
306 | memmove(&optptr[3], &optptr[7], optlen-7); | |
307 | } | |
308 | opt->is_strictroute = (optptr[0] == IPOPT_SSRR); | |
309 | opt->srr = optptr - iph; | |
310 | break; | |
dd9b4559 | 311 | case IPOPT_RR: |
1da177e4 LT |
312 | if (opt->rr) { |
313 | pp_ptr = optptr; | |
314 | goto error; | |
315 | } | |
316 | if (optlen < 3) { | |
317 | pp_ptr = optptr + 1; | |
318 | goto error; | |
319 | } | |
320 | if (optptr[2] < 4) { | |
321 | pp_ptr = optptr + 2; | |
322 | goto error; | |
323 | } | |
324 | if (optptr[2] <= optlen) { | |
325 | if (optptr[2]+3 > optlen) { | |
326 | pp_ptr = optptr + 2; | |
327 | goto error; | |
328 | } | |
11604721 | 329 | if (rt) { |
bf5e53e3 | 330 | spec_dst_fill(&spec_dst, skb); |
35ebf65e | 331 | memcpy(&optptr[optptr[2]-1], &spec_dst, 4); |
1da177e4 LT |
332 | opt->is_changed = 1; |
333 | } | |
334 | optptr[2] += 4; | |
335 | opt->rr_needaddr = 1; | |
336 | } | |
337 | opt->rr = optptr - iph; | |
338 | break; | |
dd9b4559 | 339 | case IPOPT_TIMESTAMP: |
1da177e4 LT |
340 | if (opt->ts) { |
341 | pp_ptr = optptr; | |
342 | goto error; | |
343 | } | |
344 | if (optlen < 4) { | |
345 | pp_ptr = optptr + 1; | |
346 | goto error; | |
347 | } | |
348 | if (optptr[2] < 5) { | |
349 | pp_ptr = optptr + 2; | |
350 | goto error; | |
351 | } | |
352 | if (optptr[2] <= optlen) { | |
48bdf072 | 353 | unsigned char *timeptr = NULL; |
5a2b646f | 354 | if (optptr[2]+3 > optlen) { |
1da177e4 LT |
355 | pp_ptr = optptr + 2; |
356 | goto error; | |
357 | } | |
358 | switch (optptr[3]&0xF) { | |
dd9b4559 | 359 | case IPOPT_TS_TSONLY: |
e905a9ed | 360 | if (skb) |
48bdf072 | 361 | timeptr = &optptr[optptr[2]-1]; |
1da177e4 LT |
362 | opt->ts_needtime = 1; |
363 | optptr[2] += 4; | |
364 | break; | |
dd9b4559 | 365 | case IPOPT_TS_TSANDADDR: |
5a2b646f | 366 | if (optptr[2]+7 > optlen) { |
1da177e4 LT |
367 | pp_ptr = optptr + 2; |
368 | goto error; | |
369 | } | |
11604721 | 370 | if (rt) { |
bf5e53e3 | 371 | spec_dst_fill(&spec_dst, skb); |
35ebf65e | 372 | memcpy(&optptr[optptr[2]-1], &spec_dst, 4); |
48bdf072 | 373 | timeptr = &optptr[optptr[2]+3]; |
1da177e4 LT |
374 | } |
375 | opt->ts_needaddr = 1; | |
376 | opt->ts_needtime = 1; | |
377 | optptr[2] += 8; | |
378 | break; | |
dd9b4559 | 379 | case IPOPT_TS_PRESPEC: |
5a2b646f | 380 | if (optptr[2]+7 > optlen) { |
1da177e4 LT |
381 | pp_ptr = optptr + 2; |
382 | goto error; | |
383 | } | |
1da177e4 | 384 | { |
fd683222 | 385 | __be32 addr; |
1da177e4 | 386 | memcpy(&addr, &optptr[optptr[2]-1], 4); |
0e6bd4a1 | 387 | if (inet_addr_type(net, addr) == RTN_UNICAST) |
1da177e4 LT |
388 | break; |
389 | if (skb) | |
48bdf072 | 390 | timeptr = &optptr[optptr[2]+3]; |
1da177e4 LT |
391 | } |
392 | opt->ts_needtime = 1; | |
393 | optptr[2] += 8; | |
394 | break; | |
dd9b4559 | 395 | default: |
52e804c6 | 396 | if (!skb && !ns_capable(net->user_ns, CAP_NET_RAW)) { |
1da177e4 LT |
397 | pp_ptr = optptr + 3; |
398 | goto error; | |
399 | } | |
400 | break; | |
401 | } | |
402 | if (timeptr) { | |
822c8685 DD |
403 | __be32 midtime; |
404 | ||
405 | midtime = inet_current_timestamp(); | |
406 | memcpy(timeptr, &midtime, 4); | |
1da177e4 LT |
407 | opt->is_changed = 1; |
408 | } | |
fa2b04f4 | 409 | } else if ((optptr[3]&0xF) != IPOPT_TS_PRESPEC) { |
95c96174 | 410 | unsigned int overflow = optptr[3]>>4; |
1da177e4 LT |
411 | if (overflow == 15) { |
412 | pp_ptr = optptr + 3; | |
413 | goto error; | |
414 | } | |
1da177e4 LT |
415 | if (skb) { |
416 | optptr[3] = (optptr[3]&0xF)|((overflow+1)<<4); | |
417 | opt->is_changed = 1; | |
418 | } | |
419 | } | |
4660c7f4 | 420 | opt->ts = optptr - iph; |
1da177e4 | 421 | break; |
dd9b4559 | 422 | case IPOPT_RA: |
1da177e4 LT |
423 | if (optlen < 4) { |
424 | pp_ptr = optptr + 1; | |
425 | goto error; | |
426 | } | |
427 | if (optptr[2] == 0 && optptr[3] == 0) | |
428 | opt->router_alert = optptr - iph; | |
429 | break; | |
dd9b4559 | 430 | case IPOPT_CIPSO: |
52e804c6 | 431 | if ((!skb && !ns_capable(net->user_ns, CAP_NET_RAW)) || opt->cipso) { |
11a03f78 PM |
432 | pp_ptr = optptr; |
433 | goto error; | |
434 | } | |
435 | opt->cipso = optptr - iph; | |
15c45f7b | 436 | if (cipso_v4_validate(skb, &optptr)) { |
11a03f78 PM |
437 | pp_ptr = optptr; |
438 | goto error; | |
439 | } | |
440 | break; | |
dd9b4559 WC |
441 | case IPOPT_SEC: |
442 | case IPOPT_SID: | |
443 | default: | |
52e804c6 | 444 | if (!skb && !ns_capable(net->user_ns, CAP_NET_RAW)) { |
1da177e4 LT |
445 | pp_ptr = optptr; |
446 | goto error; | |
447 | } | |
448 | break; | |
449 | } | |
450 | l -= optlen; | |
451 | optptr += optlen; | |
452 | } | |
453 | ||
454 | eol: | |
455 | if (!pp_ptr) | |
456 | return 0; | |
457 | ||
458 | error: | |
3da1ed7a NS |
459 | if (info) |
460 | *info = htonl((pp_ptr-iph)<<24); | |
1da177e4 LT |
461 | return -EINVAL; |
462 | } | |
dbb5281a | 463 | EXPORT_SYMBOL(__ip_options_compile); |
3da1ed7a NS |
464 | |
465 | int ip_options_compile(struct net *net, | |
466 | struct ip_options *opt, struct sk_buff *skb) | |
467 | { | |
468 | int ret; | |
469 | __be32 info; | |
470 | ||
471 | ret = __ip_options_compile(net, opt, skb, &info); | |
472 | if (ret != 0 && skb) | |
473 | icmp_send(skb, ICMP_PARAMETERPROB, 0, info); | |
474 | return ret; | |
475 | } | |
462fb2af | 476 | EXPORT_SYMBOL(ip_options_compile); |
1da177e4 LT |
477 | |
478 | /* | |
479 | * Undo all the changes done by ip_options_compile(). | |
480 | */ | |
481 | ||
5e73ea1a | 482 | void ip_options_undo(struct ip_options *opt) |
1da177e4 LT |
483 | { |
484 | if (opt->srr) { | |
343d8c60 ML |
485 | unsigned char *optptr = opt->__data + opt->srr - sizeof(struct iphdr); |
486 | ||
487 | memmove(optptr + 7, optptr + 3, optptr[1] - 7); | |
488 | memcpy(optptr + 3, &opt->faddr, 4); | |
1da177e4 LT |
489 | } |
490 | if (opt->rr_needaddr) { | |
343d8c60 ML |
491 | unsigned char *optptr = opt->__data + opt->rr - sizeof(struct iphdr); |
492 | ||
1da177e4 | 493 | optptr[2] -= 4; |
343d8c60 | 494 | memset(&optptr[optptr[2] - 1], 0, 4); |
1da177e4 LT |
495 | } |
496 | if (opt->ts) { | |
343d8c60 ML |
497 | unsigned char *optptr = opt->__data + opt->ts - sizeof(struct iphdr); |
498 | ||
1da177e4 LT |
499 | if (opt->ts_needtime) { |
500 | optptr[2] -= 4; | |
343d8c60 ML |
501 | memset(&optptr[optptr[2] - 1], 0, 4); |
502 | if ((optptr[3] & 0xF) == IPOPT_TS_PRESPEC) | |
1da177e4 LT |
503 | optptr[2] -= 4; |
504 | } | |
505 | if (opt->ts_needaddr) { | |
506 | optptr[2] -= 4; | |
343d8c60 | 507 | memset(&optptr[optptr[2] - 1], 0, 4); |
1da177e4 LT |
508 | } |
509 | } | |
510 | } | |
511 | ||
de40a3e8 CH |
512 | int ip_options_get(struct net *net, struct ip_options_rcu **optp, |
513 | sockptr_t data, int optlen) | |
1da177e4 | 514 | { |
de40a3e8 CH |
515 | struct ip_options_rcu *opt; |
516 | ||
517 | opt = kzalloc(sizeof(struct ip_options_rcu) + ((optlen + 3) & ~3), | |
37640703 | 518 | GFP_KERNEL); |
de40a3e8 CH |
519 | if (!opt) |
520 | return -ENOMEM; | |
521 | if (optlen && copy_from_sockptr(opt->opt.__data, data, optlen)) { | |
522 | kfree(opt); | |
523 | return -EFAULT; | |
524 | } | |
1da177e4 | 525 | |
1da177e4 | 526 | while (optlen & 3) |
f6d8bd05 ED |
527 | opt->opt.__data[optlen++] = IPOPT_END; |
528 | opt->opt.optlen = optlen; | |
529 | if (optlen && ip_options_compile(net, &opt->opt, NULL)) { | |
1da177e4 LT |
530 | kfree(opt); |
531 | return -EINVAL; | |
532 | } | |
a51482bd | 533 | kfree(*optp); |
1da177e4 LT |
534 | *optp = opt; |
535 | return 0; | |
536 | } | |
537 | ||
538 | void ip_forward_options(struct sk_buff *skb) | |
539 | { | |
5e73ea1a DB |
540 | struct ip_options *opt = &(IPCB(skb)->opt); |
541 | unsigned char *optptr; | |
511c3f92 | 542 | struct rtable *rt = skb_rtable(skb); |
d56f90a7 | 543 | unsigned char *raw = skb_network_header(skb); |
1da177e4 LT |
544 | |
545 | if (opt->rr_needaddr) { | |
546 | optptr = (unsigned char *)raw + opt->rr; | |
8e36360a | 547 | ip_rt_get_source(&optptr[optptr[2]-5], skb, rt); |
1da177e4 LT |
548 | opt->is_changed = 1; |
549 | } | |
550 | if (opt->srr_is_hit) { | |
551 | int srrptr, srrspace; | |
552 | ||
553 | optptr = raw + opt->srr; | |
554 | ||
a22318e8 | 555 | for ( srrptr = optptr[2], srrspace = optptr[1]; |
1da177e4 LT |
556 | srrptr <= srrspace; |
557 | srrptr += 4 | |
558 | ) { | |
559 | if (srrptr + 3 > srrspace) | |
560 | break; | |
ac8a4810 | 561 | if (memcmp(&opt->nexthop, &optptr[srrptr-1], 4) == 0) |
1da177e4 LT |
562 | break; |
563 | } | |
564 | if (srrptr + 3 <= srrspace) { | |
565 | opt->is_changed = 1; | |
ac8a4810 | 566 | ip_hdr(skb)->daddr = opt->nexthop; |
5dc7883f | 567 | ip_rt_get_source(&optptr[srrptr-1], skb, rt); |
1da177e4 | 568 | optptr[2] = srrptr+4; |
e87cc472 JP |
569 | } else { |
570 | net_crit_ratelimited("%s(): Argh! Destination lost!\n", | |
571 | __func__); | |
572 | } | |
1da177e4 LT |
573 | if (opt->ts_needaddr) { |
574 | optptr = raw + opt->ts; | |
8e36360a | 575 | ip_rt_get_source(&optptr[optptr[2]-9], skb, rt); |
1da177e4 LT |
576 | opt->is_changed = 1; |
577 | } | |
578 | } | |
579 | if (opt->is_changed) { | |
580 | opt->is_changed = 0; | |
eddc9ec5 | 581 | ip_send_check(ip_hdr(skb)); |
1da177e4 LT |
582 | } |
583 | } | |
584 | ||
8c83f2df | 585 | int ip_options_rcv_srr(struct sk_buff *skb, struct net_device *dev) |
1da177e4 LT |
586 | { |
587 | struct ip_options *opt = &(IPCB(skb)->opt); | |
588 | int srrspace, srrptr; | |
9e12bb22 | 589 | __be32 nexthop; |
eddc9ec5 | 590 | struct iphdr *iph = ip_hdr(skb); |
d56f90a7 | 591 | unsigned char *optptr = skb_network_header(skb) + opt->srr; |
511c3f92 | 592 | struct rtable *rt = skb_rtable(skb); |
1da177e4 | 593 | struct rtable *rt2; |
7fee226a | 594 | unsigned long orefdst; |
1da177e4 LT |
595 | int err; |
596 | ||
10949550 | 597 | if (!rt) |
1da177e4 LT |
598 | return 0; |
599 | ||
600 | if (skb->pkt_type != PACKET_HOST) | |
601 | return -EINVAL; | |
602 | if (rt->rt_type == RTN_UNICAST) { | |
603 | if (!opt->is_strictroute) | |
604 | return 0; | |
605 | icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl(16<<24)); | |
606 | return -EINVAL; | |
607 | } | |
608 | if (rt->rt_type != RTN_LOCAL) | |
609 | return -EINVAL; | |
610 | ||
a22318e8 | 611 | for (srrptr = optptr[2], srrspace = optptr[1]; srrptr <= srrspace; srrptr += 4) { |
1da177e4 LT |
612 | if (srrptr + 3 > srrspace) { |
613 | icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((opt->srr+2)<<24)); | |
614 | return -EINVAL; | |
615 | } | |
616 | memcpy(&nexthop, &optptr[srrptr-1], 4); | |
617 | ||
7fee226a | 618 | orefdst = skb->_skb_refdst; |
adf30907 | 619 | skb_dst_set(skb, NULL); |
8c83f2df | 620 | err = ip_route_input(skb, nexthop, iph->saddr, iph->tos, dev); |
511c3f92 | 621 | rt2 = skb_rtable(skb); |
1da177e4 | 622 | if (err || (rt2->rt_type != RTN_UNICAST && rt2->rt_type != RTN_LOCAL)) { |
7fee226a ED |
623 | skb_dst_drop(skb); |
624 | skb->_skb_refdst = orefdst; | |
1da177e4 LT |
625 | return -EINVAL; |
626 | } | |
7fee226a | 627 | refdst_drop(orefdst); |
1da177e4 LT |
628 | if (rt2->rt_type != RTN_LOCAL) |
629 | break; | |
630 | /* Superfast 8) loopback forward */ | |
c30883bd | 631 | iph->daddr = nexthop; |
1da177e4 LT |
632 | opt->is_changed = 1; |
633 | } | |
634 | if (srrptr <= srrspace) { | |
635 | opt->srr_is_hit = 1; | |
ac8a4810 | 636 | opt->nexthop = nexthop; |
1da177e4 LT |
637 | opt->is_changed = 1; |
638 | } | |
639 | return 0; | |
640 | } | |
462fb2af | 641 | EXPORT_SYMBOL(ip_options_rcv_srr); |