blob: 523541730777db2fe167e2ff5d02f5db7ce5d855 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * The options processing module for ip.c
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Authors: A.N.Kuznetsov
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Joe Perchesafd465032012-03-12 07:03:32 +000012#define pr_fmt(fmt) "IPv4: " fmt
13
Randy Dunlap4fc268d2006-01-11 12:17:47 -080014#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/types.h>
18#include <asm/uaccess.h>
Chris Metcalf48bdf072011-05-29 10:55:44 +000019#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/skbuff.h>
21#include <linux/ip.h>
22#include <linux/icmp.h>
23#include <linux/netdevice.h>
24#include <linux/rtnetlink.h>
25#include <net/sock.h>
26#include <net/ip.h>
27#include <net/icmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020028#include <net/route.h>
Paul Moore11a03f72006-08-03 16:46:20 -070029#include <net/cipso_ipv4.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090031/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * Write options to IP header, record destination address to
33 * source route option, address of outgoing interface
34 * (we should already know it, so that this function is allowed be
35 * called only after routing decision) and timestamp,
36 * if we originate this datagram.
37 *
38 * daddr is real destination address, next hop is recorded in IP header.
39 * saddr is address of outgoing interface.
40 */
41
Eric Dumazetf6d8bd02011-04-21 09:45:37 +000042void ip_options_build(struct sk_buff *skb, struct ip_options *opt,
David S. Miller8e363602011-05-13 17:29:41 -040043 __be32 daddr, struct rtable *rt, int is_frag)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -070045 unsigned char *iph = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47 memcpy(&(IPCB(skb)->opt), opt, sizeof(struct ip_options));
48 memcpy(iph+sizeof(struct iphdr), opt->__data, opt->optlen);
49 opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 if (opt->srr)
52 memcpy(iph+opt->srr+iph[opt->srr+1]-4, &daddr, 4);
53
54 if (!is_frag) {
55 if (opt->rr_needaddr)
David S. Miller8e363602011-05-13 17:29:41 -040056 ip_rt_get_source(iph+opt->rr+iph[opt->rr+2]-5, skb, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 if (opt->ts_needaddr)
David S. Miller8e363602011-05-13 17:29:41 -040058 ip_rt_get_source(iph+opt->ts+iph[opt->ts+2]-9, skb, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (opt->ts_needtime) {
YOSHIFUJI Hideakif25c3d62008-04-21 02:34:08 -070060 struct timespec tv;
Al Viroe25d2ca2006-09-27 18:28:47 -070061 __be32 midtime;
YOSHIFUJI Hideakif25c3d62008-04-21 02:34:08 -070062 getnstimeofday(&tv);
63 midtime = htonl((tv.tv_sec % 86400) * MSEC_PER_SEC + tv.tv_nsec / NSEC_PER_MSEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 memcpy(iph+opt->ts+iph[opt->ts+2]-5, &midtime, 4);
65 }
66 return;
67 }
68 if (opt->rr) {
69 memset(iph+opt->rr, IPOPT_NOP, iph[opt->rr+1]);
70 opt->rr = 0;
71 opt->rr_needaddr = 0;
72 }
73 if (opt->ts) {
74 memset(iph+opt->ts, IPOPT_NOP, iph[opt->ts+1]);
75 opt->ts = 0;
76 opt->ts_needaddr = opt->ts_needtime = 0;
77 }
78}
79
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090080/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * Provided (sopt, skb) points to received options,
82 * build in dopt compiled option set appropriate for answering.
83 * i.e. invert SRR option, copy anothers,
84 * and grab room in RR/TS options.
85 *
86 * NOTE: dopt cannot point to skb.
87 */
88
Eric Dumazetf6d8bd02011-04-21 09:45:37 +000089int ip_options_echo(struct ip_options *dopt, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Eric Dumazetf6d8bd02011-04-21 09:45:37 +000091 const struct ip_options *sopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 unsigned char *sptr, *dptr;
93 int soffset, doffset;
94 int optlen;
Al Viroe25d2ca2006-09-27 18:28:47 -070095 __be32 daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 memset(dopt, 0, sizeof(struct ip_options));
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 sopt = &(IPCB(skb)->opt);
100
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000101 if (sopt->optlen == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700104 sptr = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 dptr = dopt->__data;
106
Eric Dumazet511c3f92009-06-02 05:14:27 +0000107 daddr = skb_rtable(skb)->rt_spec_dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 if (sopt->rr) {
110 optlen = sptr[sopt->rr+1];
111 soffset = sptr[sopt->rr+2];
112 dopt->rr = dopt->optlen + sizeof(struct iphdr);
113 memcpy(dptr, sptr+sopt->rr, optlen);
114 if (sopt->rr_needaddr && soffset <= optlen) {
115 if (soffset + 3 > optlen)
116 return -EINVAL;
117 dptr[2] = soffset + 4;
118 dopt->rr_needaddr = 1;
119 }
120 dptr += optlen;
121 dopt->optlen += optlen;
122 }
123 if (sopt->ts) {
124 optlen = sptr[sopt->ts+1];
125 soffset = sptr[sopt->ts+2];
126 dopt->ts = dopt->optlen + sizeof(struct iphdr);
127 memcpy(dptr, sptr+sopt->ts, optlen);
128 if (soffset <= optlen) {
129 if (sopt->ts_needaddr) {
130 if (soffset + 3 > optlen)
131 return -EINVAL;
132 dopt->ts_needaddr = 1;
133 soffset += 4;
134 }
135 if (sopt->ts_needtime) {
136 if (soffset + 3 > optlen)
137 return -EINVAL;
138 if ((dptr[3]&0xF) != IPOPT_TS_PRESPEC) {
139 dopt->ts_needtime = 1;
140 soffset += 4;
141 } else {
142 dopt->ts_needtime = 0;
143
Jan Luebbe8628bd82011-03-24 07:44:22 +0000144 if (soffset + 7 <= optlen) {
Al Virofd683222006-09-26 22:17:51 -0700145 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jan Luebbe8628bd82011-03-24 07:44:22 +0000147 memcpy(&addr, dptr+soffset-1, 4);
148 if (inet_addr_type(dev_net(skb_dst(skb)->dev), addr) != RTN_UNICAST) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 dopt->ts_needtime = 1;
150 soffset += 8;
151 }
152 }
153 }
154 }
155 dptr[2] = soffset;
156 }
157 dptr += optlen;
158 dopt->optlen += optlen;
159 }
160 if (sopt->srr) {
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000161 unsigned char *start = sptr+sopt->srr;
Al Viro3ca3c682006-09-27 18:28:07 -0700162 __be32 faddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 optlen = start[1];
165 soffset = start[2];
166 doffset = 0;
167 if (soffset > optlen)
168 soffset = optlen + 1;
169 soffset -= 4;
170 if (soffset > 3) {
171 memcpy(&faddr, &start[soffset-1], 4);
172 for (soffset-=4, doffset=4; soffset > 3; soffset-=4, doffset+=4)
173 memcpy(&dptr[doffset-1], &start[soffset-1], 4);
174 /*
175 * RFC1812 requires to fix illegal source routes.
176 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700177 if (memcmp(&ip_hdr(skb)->saddr,
178 &start[soffset + 3], 4) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 doffset -= 4;
180 }
181 if (doffset > 3) {
182 memcpy(&start[doffset-1], &daddr, 4);
183 dopt->faddr = faddr;
184 dptr[0] = start[0];
185 dptr[1] = doffset+3;
186 dptr[2] = 4;
187 dptr += doffset+3;
188 dopt->srr = dopt->optlen + sizeof(struct iphdr);
189 dopt->optlen += doffset+3;
190 dopt->is_strictroute = sopt->is_strictroute;
191 }
192 }
Paul Moore11a03f72006-08-03 16:46:20 -0700193 if (sopt->cipso) {
194 optlen = sptr[sopt->cipso+1];
195 dopt->cipso = dopt->optlen+sizeof(struct iphdr);
196 memcpy(dptr, sptr+sopt->cipso, optlen);
197 dptr += optlen;
198 dopt->optlen += optlen;
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 while (dopt->optlen & 3) {
201 *dptr++ = IPOPT_END;
202 dopt->optlen++;
203 }
204 return 0;
205}
206
207/*
208 * Options "fragmenting", just fill options not
209 * allowed in fragments with NOOPs.
210 * Simple and stupid 8), but the most efficient way.
211 */
212
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900213void ip_options_fragment(struct sk_buff * skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700215 unsigned char *optptr = skb_network_header(skb) + sizeof(struct iphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 struct ip_options * opt = &(IPCB(skb)->opt);
217 int l = opt->optlen;
218 int optlen;
219
220 while (l > 0) {
221 switch (*optptr) {
222 case IPOPT_END:
223 return;
224 case IPOPT_NOOP:
225 l--;
226 optptr++;
227 continue;
228 }
229 optlen = optptr[1];
230 if (optlen<2 || optlen>l)
231 return;
232 if (!IPOPT_COPIED(*optptr))
233 memset(optptr, IPOPT_NOOP, optlen);
234 l -= optlen;
235 optptr += optlen;
236 }
237 opt->ts = 0;
238 opt->rr = 0;
239 opt->rr_needaddr = 0;
240 opt->ts_needaddr = 0;
241 opt->ts_needtime = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244/*
245 * Verify options and fill pointers in struct options.
246 * Caller should clear *opt, and set opt->data.
247 * If opt == NULL, then skb->data should point to IP header.
248 */
249
Denis V. Lunev0e6bd4a2008-03-24 15:29:23 -0700250int ip_options_compile(struct net *net,
251 struct ip_options * opt, struct sk_buff * skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 int l;
254 unsigned char * iph;
255 unsigned char * optptr;
256 int optlen;
257 unsigned char * pp_ptr = NULL;
Denis V. Lunev22aba382008-03-22 16:36:20 -0700258 struct rtable *rt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Denis V. Lunev22aba382008-03-22 16:36:20 -0700260 if (skb != NULL) {
Eric Dumazet511c3f92009-06-02 05:14:27 +0000261 rt = skb_rtable(skb);
Denis V. Lunev22aba382008-03-22 16:36:20 -0700262 optptr = (unsigned char *)&(ip_hdr(skb)[1]);
263 } else
Denis V. Lunev10fe7d82008-03-22 16:35:00 -0700264 optptr = opt->__data;
Denis V. Lunev22aba382008-03-22 16:36:20 -0700265 iph = optptr - sizeof(struct iphdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 for (l = opt->optlen; l > 0; ) {
268 switch (*optptr) {
269 case IPOPT_END:
270 for (optptr++, l--; l>0; optptr++, l--) {
271 if (*optptr != IPOPT_END) {
272 *optptr = IPOPT_END;
273 opt->is_changed = 1;
274 }
275 }
276 goto eol;
277 case IPOPT_NOOP:
278 l--;
279 optptr++;
280 continue;
281 }
Eric Dumazetcd88c7f2014-07-21 07:17:42 +0200282 if (unlikely(l < 2)) {
283 pp_ptr = optptr;
284 goto error;
285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 optlen = optptr[1];
287 if (optlen<2 || optlen>l) {
288 pp_ptr = optptr;
289 goto error;
290 }
291 switch (*optptr) {
292 case IPOPT_SSRR:
293 case IPOPT_LSRR:
294 if (optlen < 3) {
295 pp_ptr = optptr + 1;
296 goto error;
297 }
298 if (optptr[2] < 4) {
299 pp_ptr = optptr + 2;
300 goto error;
301 }
302 /* NB: cf RFC-1812 5.2.4.1 */
303 if (opt->srr) {
304 pp_ptr = optptr;
305 goto error;
306 }
307 if (!skb) {
308 if (optptr[2] != 4 || optlen < 7 || ((optlen-3) & 3)) {
309 pp_ptr = optptr + 1;
310 goto error;
311 }
312 memcpy(&opt->faddr, &optptr[3], 4);
313 if (optlen > 7)
314 memmove(&optptr[3], &optptr[7], optlen-7);
315 }
316 opt->is_strictroute = (optptr[0] == IPOPT_SSRR);
317 opt->srr = optptr - iph;
318 break;
319 case IPOPT_RR:
320 if (opt->rr) {
321 pp_ptr = optptr;
322 goto error;
323 }
324 if (optlen < 3) {
325 pp_ptr = optptr + 1;
326 goto error;
327 }
328 if (optptr[2] < 4) {
329 pp_ptr = optptr + 2;
330 goto error;
331 }
332 if (optptr[2] <= optlen) {
333 if (optptr[2]+3 > optlen) {
334 pp_ptr = optptr + 2;
335 goto error;
336 }
Eric Dumazetc65353d2011-04-14 05:55:37 +0000337 if (rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 memcpy(&optptr[optptr[2]-1], &rt->rt_spec_dst, 4);
339 opt->is_changed = 1;
340 }
341 optptr[2] += 4;
342 opt->rr_needaddr = 1;
343 }
344 opt->rr = optptr - iph;
345 break;
346 case IPOPT_TIMESTAMP:
347 if (opt->ts) {
348 pp_ptr = optptr;
349 goto error;
350 }
351 if (optlen < 4) {
352 pp_ptr = optptr + 1;
353 goto error;
354 }
355 if (optptr[2] < 5) {
356 pp_ptr = optptr + 2;
357 goto error;
358 }
359 if (optptr[2] <= optlen) {
Chris Metcalf48bdf072011-05-29 10:55:44 +0000360 unsigned char *timeptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (optptr[2]+3 > optptr[1]) {
362 pp_ptr = optptr + 2;
363 goto error;
364 }
365 switch (optptr[3]&0xF) {
366 case IPOPT_TS_TSONLY:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900367 if (skb)
Chris Metcalf48bdf072011-05-29 10:55:44 +0000368 timeptr = &optptr[optptr[2]-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 opt->ts_needtime = 1;
370 optptr[2] += 4;
371 break;
372 case IPOPT_TS_TSANDADDR:
373 if (optptr[2]+7 > optptr[1]) {
374 pp_ptr = optptr + 2;
375 goto error;
376 }
Eric Dumazetc65353d2011-04-14 05:55:37 +0000377 if (rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 memcpy(&optptr[optptr[2]-1], &rt->rt_spec_dst, 4);
Chris Metcalf48bdf072011-05-29 10:55:44 +0000379 timeptr = &optptr[optptr[2]+3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381 opt->ts_needaddr = 1;
382 opt->ts_needtime = 1;
383 optptr[2] += 8;
384 break;
385 case IPOPT_TS_PRESPEC:
386 if (optptr[2]+7 > optptr[1]) {
387 pp_ptr = optptr + 2;
388 goto error;
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 {
Al Virofd683222006-09-26 22:17:51 -0700391 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 memcpy(&addr, &optptr[optptr[2]-1], 4);
Denis V. Lunev0e6bd4a2008-03-24 15:29:23 -0700393 if (inet_addr_type(net, addr) == RTN_UNICAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 break;
395 if (skb)
Chris Metcalf48bdf072011-05-29 10:55:44 +0000396 timeptr = &optptr[optptr[2]+3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398 opt->ts_needtime = 1;
399 optptr[2] += 8;
400 break;
401 default:
402 if (!skb && !capable(CAP_NET_RAW)) {
403 pp_ptr = optptr + 3;
404 goto error;
405 }
406 break;
407 }
408 if (timeptr) {
YOSHIFUJI Hideakif25c3d62008-04-21 02:34:08 -0700409 struct timespec tv;
Chris Metcalf48bdf072011-05-29 10:55:44 +0000410 u32 midtime;
YOSHIFUJI Hideakif25c3d62008-04-21 02:34:08 -0700411 getnstimeofday(&tv);
Chris Metcalf48bdf072011-05-29 10:55:44 +0000412 midtime = (tv.tv_sec % 86400) * MSEC_PER_SEC + tv.tv_nsec / NSEC_PER_MSEC;
413 put_unaligned_be32(midtime, timeptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 opt->is_changed = 1;
415 }
416 } else {
417 unsigned overflow = optptr[3]>>4;
418 if (overflow == 15) {
419 pp_ptr = optptr + 3;
420 goto error;
421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (skb) {
423 optptr[3] = (optptr[3]&0xF)|((overflow+1)<<4);
424 opt->is_changed = 1;
425 }
426 }
David Ward0ae508e2013-03-11 10:43:39 +0000427 opt->ts = optptr - iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 break;
429 case IPOPT_RA:
430 if (optlen < 4) {
431 pp_ptr = optptr + 1;
432 goto error;
433 }
434 if (optptr[2] == 0 && optptr[3] == 0)
435 opt->router_alert = optptr - iph;
436 break;
Paul Moore11a03f72006-08-03 16:46:20 -0700437 case IPOPT_CIPSO:
Paul Mooref8687af2006-10-30 15:22:15 -0800438 if ((!skb && !capable(CAP_NET_RAW)) || opt->cipso) {
Paul Moore11a03f72006-08-03 16:46:20 -0700439 pp_ptr = optptr;
440 goto error;
441 }
442 opt->cipso = optptr - iph;
Paul Moore15c45f72008-10-10 10:16:34 -0400443 if (cipso_v4_validate(skb, &optptr)) {
Paul Moore11a03f72006-08-03 16:46:20 -0700444 pp_ptr = optptr;
445 goto error;
446 }
447 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 case IPOPT_SEC:
449 case IPOPT_SID:
450 default:
451 if (!skb && !capable(CAP_NET_RAW)) {
452 pp_ptr = optptr;
453 goto error;
454 }
455 break;
456 }
457 l -= optlen;
458 optptr += optlen;
459 }
460
461eol:
462 if (!pp_ptr)
463 return 0;
464
465error:
466 if (skb) {
467 icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((pp_ptr-iph)<<24));
468 }
469 return -EINVAL;
470}
Bandan Das462fb2a2010-09-19 09:34:33 +0000471EXPORT_SYMBOL(ip_options_compile);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473/*
474 * Undo all the changes done by ip_options_compile().
475 */
476
477void ip_options_undo(struct ip_options * opt)
478{
479 if (opt->srr) {
480 unsigned char * optptr = opt->__data+opt->srr-sizeof(struct iphdr);
481 memmove(optptr+7, optptr+3, optptr[1]-7);
482 memcpy(optptr+3, &opt->faddr, 4);
483 }
484 if (opt->rr_needaddr) {
485 unsigned char * optptr = opt->__data+opt->rr-sizeof(struct iphdr);
486 optptr[2] -= 4;
487 memset(&optptr[optptr[2]-1], 0, 4);
488 }
489 if (opt->ts) {
490 unsigned char * optptr = opt->__data+opt->ts-sizeof(struct iphdr);
491 if (opt->ts_needtime) {
492 optptr[2] -= 4;
493 memset(&optptr[optptr[2]-1], 0, 4);
494 if ((optptr[3]&0xF) == IPOPT_TS_PRESPEC)
495 optptr[2] -= 4;
496 }
497 if (opt->ts_needaddr) {
498 optptr[2] -= 4;
499 memset(&optptr[optptr[2]-1], 0, 4);
500 }
501 }
502}
503
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000504static struct ip_options_rcu *ip_options_get_alloc(const int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000506 return kzalloc(sizeof(struct ip_options_rcu) + ((optlen + 3) & ~3),
Mariusz Kozlowski37640702007-07-31 14:06:45 -0700507 GFP_KERNEL);
Arnaldo Carvalho de Melo4c6ea292005-08-16 19:46:48 -0300508}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000510static int ip_options_get_finish(struct net *net, struct ip_options_rcu **optp,
511 struct ip_options_rcu *opt, int optlen)
Arnaldo Carvalho de Melo4c6ea292005-08-16 19:46:48 -0300512{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 while (optlen & 3)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000514 opt->opt.__data[optlen++] = IPOPT_END;
515 opt->opt.optlen = optlen;
516 if (optlen && ip_options_compile(net, &opt->opt, NULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 kfree(opt);
518 return -EINVAL;
519 }
Jesper Juhla51482b2005-11-08 09:41:34 -0800520 kfree(*optp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 *optp = opt;
522 return 0;
523}
524
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000525int ip_options_get_from_user(struct net *net, struct ip_options_rcu **optp,
Denis V. Lunevf2c48022008-03-24 15:29:55 -0700526 unsigned char __user *data, int optlen)
Arnaldo Carvalho de Melo4c6ea292005-08-16 19:46:48 -0300527{
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000528 struct ip_options_rcu *opt = ip_options_get_alloc(optlen);
Arnaldo Carvalho de Melo4c6ea292005-08-16 19:46:48 -0300529
530 if (!opt)
531 return -ENOMEM;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000532 if (optlen && copy_from_user(opt->opt.__data, data, optlen)) {
Arnaldo Carvalho de Melo4c6ea292005-08-16 19:46:48 -0300533 kfree(opt);
534 return -EFAULT;
535 }
Denis V. Lunevf2c48022008-03-24 15:29:55 -0700536 return ip_options_get_finish(net, optp, opt, optlen);
Arnaldo Carvalho de Melo4c6ea292005-08-16 19:46:48 -0300537}
538
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000539int ip_options_get(struct net *net, struct ip_options_rcu **optp,
Denis V. Lunevf2c48022008-03-24 15:29:55 -0700540 unsigned char *data, int optlen)
Arnaldo Carvalho de Melo4c6ea292005-08-16 19:46:48 -0300541{
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000542 struct ip_options_rcu *opt = ip_options_get_alloc(optlen);
Arnaldo Carvalho de Melo4c6ea292005-08-16 19:46:48 -0300543
544 if (!opt)
545 return -ENOMEM;
546 if (optlen)
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000547 memcpy(opt->opt.__data, data, optlen);
Denis V. Lunevf2c48022008-03-24 15:29:55 -0700548 return ip_options_get_finish(net, optp, opt, optlen);
Arnaldo Carvalho de Melo4c6ea292005-08-16 19:46:48 -0300549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551void ip_forward_options(struct sk_buff *skb)
552{
553 struct ip_options * opt = &(IPCB(skb)->opt);
554 unsigned char * optptr;
Eric Dumazet511c3f92009-06-02 05:14:27 +0000555 struct rtable *rt = skb_rtable(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700556 unsigned char *raw = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 if (opt->rr_needaddr) {
559 optptr = (unsigned char *)raw + opt->rr;
David S. Miller8e363602011-05-13 17:29:41 -0400560 ip_rt_get_source(&optptr[optptr[2]-5], skb, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 opt->is_changed = 1;
562 }
563 if (opt->srr_is_hit) {
564 int srrptr, srrspace;
565
566 optptr = raw + opt->srr;
567
568 for ( srrptr=optptr[2], srrspace = optptr[1];
569 srrptr <= srrspace;
570 srrptr += 4
571 ) {
572 if (srrptr + 3 > srrspace)
573 break;
Li Weiac8a4812011-11-22 23:33:10 +0000574 if (memcmp(&opt->nexthop, &optptr[srrptr-1], 4) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 break;
576 }
577 if (srrptr + 3 <= srrspace) {
578 opt->is_changed = 1;
Li Weiac8a4812011-11-22 23:33:10 +0000579 ip_hdr(skb)->daddr = opt->nexthop;
Li Wei5dc78832012-02-09 21:15:25 +0000580 ip_rt_get_source(&optptr[srrptr-1], skb, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 optptr[2] = srrptr+4;
582 } else if (net_ratelimit())
Joe Perches058bd4d2012-03-11 18:36:11 +0000583 pr_crit("%s(): Argh! Destination lost!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (opt->ts_needaddr) {
585 optptr = raw + opt->ts;
David S. Miller8e363602011-05-13 17:29:41 -0400586 ip_rt_get_source(&optptr[optptr[2]-9], skb, rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 opt->is_changed = 1;
588 }
589 }
590 if (opt->is_changed) {
591 opt->is_changed = 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700592 ip_send_check(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594}
595
596int ip_options_rcv_srr(struct sk_buff *skb)
597{
598 struct ip_options *opt = &(IPCB(skb)->opt);
599 int srrspace, srrptr;
Al Viro9e12bb22006-09-26 21:25:20 -0700600 __be32 nexthop;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700601 struct iphdr *iph = ip_hdr(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700602 unsigned char *optptr = skb_network_header(skb) + opt->srr;
Eric Dumazet511c3f92009-06-02 05:14:27 +0000603 struct rtable *rt = skb_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct rtable *rt2;
Eric Dumazet7fee2262010-05-11 23:19:48 +0000605 unsigned long orefdst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 int err;
607
David S. Miller10949552011-05-12 19:26:57 -0400608 if (!rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return 0;
610
611 if (skb->pkt_type != PACKET_HOST)
612 return -EINVAL;
613 if (rt->rt_type == RTN_UNICAST) {
614 if (!opt->is_strictroute)
615 return 0;
616 icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl(16<<24));
617 return -EINVAL;
618 }
619 if (rt->rt_type != RTN_LOCAL)
620 return -EINVAL;
621
622 for (srrptr=optptr[2], srrspace = optptr[1]; srrptr <= srrspace; srrptr += 4) {
623 if (srrptr + 3 > srrspace) {
624 icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((opt->srr+2)<<24));
625 return -EINVAL;
626 }
627 memcpy(&nexthop, &optptr[srrptr-1], 4);
628
Eric Dumazet7fee2262010-05-11 23:19:48 +0000629 orefdst = skb->_skb_refdst;
Eric Dumazetadf30902009-06-02 05:19:30 +0000630 skb_dst_set(skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 err = ip_route_input(skb, nexthop, iph->saddr, iph->tos, skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +0000632 rt2 = skb_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (err || (rt2->rt_type != RTN_UNICAST && rt2->rt_type != RTN_LOCAL)) {
Eric Dumazet7fee2262010-05-11 23:19:48 +0000634 skb_dst_drop(skb);
635 skb->_skb_refdst = orefdst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return -EINVAL;
637 }
Eric Dumazet7fee2262010-05-11 23:19:48 +0000638 refdst_drop(orefdst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (rt2->rt_type != RTN_LOCAL)
640 break;
641 /* Superfast 8) loopback forward */
David S. Millerc30883b2011-05-12 19:30:58 -0400642 iph->daddr = nexthop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 opt->is_changed = 1;
644 }
645 if (srrptr <= srrspace) {
646 opt->srr_is_hit = 1;
Li Weiac8a4812011-11-22 23:33:10 +0000647 opt->nexthop = nexthop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 opt->is_changed = 1;
649 }
650 return 0;
651}
Bandan Das462fb2a2010-09-19 09:34:33 +0000652EXPORT_SYMBOL(ip_options_rcv_srr);