ip_options.c 14.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8
/*
 * INET		An implementation of the TCP/IP protocol suite for the LINUX
 *		operating system.  INET is implemented using the  BSD Socket
 *		interface as the means of communication with the user level.
 *
 *		The options processing module for ip.c
 *
 * Authors:	A.N.Kuznetsov
9
 *
L
Linus Torvalds 已提交
10 11
 */

12
#include <linux/capability.h>
L
Linus Torvalds 已提交
13
#include <linux/module.h>
14
#include <linux/slab.h>
L
Linus Torvalds 已提交
15 16
#include <linux/types.h>
#include <asm/uaccess.h>
17
#include <asm/unaligned.h>
L
Linus Torvalds 已提交
18 19 20 21 22 23 24 25
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/icmp.h>
#include <linux/netdevice.h>
#include <linux/rtnetlink.h>
#include <net/sock.h>
#include <net/ip.h>
#include <net/icmp.h>
26
#include <net/route.h>
P
Paul Moore 已提交
27
#include <net/cipso_ipv4.h>
L
Linus Torvalds 已提交
28

29
/*
L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37 38 39
 * Write options to IP header, record destination address to
 * source route option, address of outgoing interface
 * (we should already know it, so that this  function is allowed be
 * called only after routing decision) and timestamp,
 * if we originate this datagram.
 *
 * daddr is real destination address, next hop is recorded in IP header.
 * saddr is address of outgoing interface.
 */

40
void ip_options_build(struct sk_buff *skb, struct ip_options *opt,
41
		      __be32 daddr, struct rtable *rt, int is_frag)
L
Linus Torvalds 已提交
42
{
43
	unsigned char *iph = skb_network_header(skb);
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53

	memcpy(&(IPCB(skb)->opt), opt, sizeof(struct ip_options));
	memcpy(iph+sizeof(struct iphdr), opt->__data, opt->optlen);
	opt = &(IPCB(skb)->opt);

	if (opt->srr)
		memcpy(iph+opt->srr+iph[opt->srr+1]-4, &daddr, 4);

	if (!is_frag) {
		if (opt->rr_needaddr)
54
			ip_rt_get_source(iph+opt->rr+iph[opt->rr+2]-5, skb, rt);
L
Linus Torvalds 已提交
55
		if (opt->ts_needaddr)
56
			ip_rt_get_source(iph+opt->ts+iph[opt->ts+2]-9, skb, rt);
L
Linus Torvalds 已提交
57
		if (opt->ts_needtime) {
58
			struct timespec tv;
59
			__be32 midtime;
60 61
			getnstimeofday(&tv);
			midtime = htonl((tv.tv_sec % 86400) * MSEC_PER_SEC + tv.tv_nsec / NSEC_PER_MSEC);
L
Linus Torvalds 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
			memcpy(iph+opt->ts+iph[opt->ts+2]-5, &midtime, 4);
		}
		return;
	}
	if (opt->rr) {
		memset(iph+opt->rr, IPOPT_NOP, iph[opt->rr+1]);
		opt->rr = 0;
		opt->rr_needaddr = 0;
	}
	if (opt->ts) {
		memset(iph+opt->ts, IPOPT_NOP, iph[opt->ts+1]);
		opt->ts = 0;
		opt->ts_needaddr = opt->ts_needtime = 0;
	}
}

78
/*
L
Linus Torvalds 已提交
79 80 81 82 83 84 85 86
 * Provided (sopt, skb) points to received options,
 * build in dopt compiled option set appropriate for answering.
 * i.e. invert SRR option, copy anothers,
 * and grab room in RR/TS options.
 *
 * NOTE: dopt cannot point to skb.
 */

87
int ip_options_echo(struct ip_options *dopt, struct sk_buff *skb)
L
Linus Torvalds 已提交
88
{
89
	const struct ip_options *sopt;
L
Linus Torvalds 已提交
90 91 92
	unsigned char *sptr, *dptr;
	int soffset, doffset;
	int	optlen;
93
	__be32	daddr;
L
Linus Torvalds 已提交
94 95 96 97 98

	memset(dopt, 0, sizeof(struct ip_options));

	sopt = &(IPCB(skb)->opt);

99
	if (sopt->optlen == 0)
L
Linus Torvalds 已提交
100 101
		return 0;

102
	sptr = skb_network_header(skb);
L
Linus Torvalds 已提交
103 104
	dptr = dopt->__data;

E
Eric Dumazet 已提交
105
	daddr = skb_rtable(skb)->rt_spec_dst;
L
Linus Torvalds 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

	if (sopt->rr) {
		optlen  = sptr[sopt->rr+1];
		soffset = sptr[sopt->rr+2];
		dopt->rr = dopt->optlen + sizeof(struct iphdr);
		memcpy(dptr, sptr+sopt->rr, optlen);
		if (sopt->rr_needaddr && soffset <= optlen) {
			if (soffset + 3 > optlen)
				return -EINVAL;
			dptr[2] = soffset + 4;
			dopt->rr_needaddr = 1;
		}
		dptr += optlen;
		dopt->optlen += optlen;
	}
	if (sopt->ts) {
		optlen = sptr[sopt->ts+1];
		soffset = sptr[sopt->ts+2];
		dopt->ts = dopt->optlen + sizeof(struct iphdr);
		memcpy(dptr, sptr+sopt->ts, optlen);
		if (soffset <= optlen) {
			if (sopt->ts_needaddr) {
				if (soffset + 3 > optlen)
					return -EINVAL;
				dopt->ts_needaddr = 1;
				soffset += 4;
			}
			if (sopt->ts_needtime) {
				if (soffset + 3 > optlen)
					return -EINVAL;
				if ((dptr[3]&0xF) != IPOPT_TS_PRESPEC) {
					dopt->ts_needtime = 1;
					soffset += 4;
				} else {
					dopt->ts_needtime = 0;

142
					if (soffset + 7 <= optlen) {
A
Al Viro 已提交
143
						__be32 addr;
L
Linus Torvalds 已提交
144

145 146
						memcpy(&addr, dptr+soffset-1, 4);
						if (inet_addr_type(dev_net(skb_dst(skb)->dev), addr) != RTN_UNICAST) {
L
Linus Torvalds 已提交
147 148 149 150 151 152 153 154 155 156 157 158
							dopt->ts_needtime = 1;
							soffset += 8;
						}
					}
				}
			}
			dptr[2] = soffset;
		}
		dptr += optlen;
		dopt->optlen += optlen;
	}
	if (sopt->srr) {
159
		unsigned char *start = sptr+sopt->srr;
A
Al Viro 已提交
160
		__be32 faddr;
L
Linus Torvalds 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174

		optlen  = start[1];
		soffset = start[2];
		doffset = 0;
		if (soffset > optlen)
			soffset = optlen + 1;
		soffset -= 4;
		if (soffset > 3) {
			memcpy(&faddr, &start[soffset-1], 4);
			for (soffset-=4, doffset=4; soffset > 3; soffset-=4, doffset+=4)
				memcpy(&dptr[doffset-1], &start[soffset-1], 4);
			/*
			 * RFC1812 requires to fix illegal source routes.
			 */
175 176
			if (memcmp(&ip_hdr(skb)->saddr,
				   &start[soffset + 3], 4) == 0)
L
Linus Torvalds 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190
				doffset -= 4;
		}
		if (doffset > 3) {
			memcpy(&start[doffset-1], &daddr, 4);
			dopt->faddr = faddr;
			dptr[0] = start[0];
			dptr[1] = doffset+3;
			dptr[2] = 4;
			dptr += doffset+3;
			dopt->srr = dopt->optlen + sizeof(struct iphdr);
			dopt->optlen += doffset+3;
			dopt->is_strictroute = sopt->is_strictroute;
		}
	}
P
Paul Moore 已提交
191 192 193 194 195 196 197
	if (sopt->cipso) {
		optlen  = sptr[sopt->cipso+1];
		dopt->cipso = dopt->optlen+sizeof(struct iphdr);
		memcpy(dptr, sptr+sopt->cipso, optlen);
		dptr += optlen;
		dopt->optlen += optlen;
	}
L
Linus Torvalds 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210
	while (dopt->optlen & 3) {
		*dptr++ = IPOPT_END;
		dopt->optlen++;
	}
	return 0;
}

/*
 *	Options "fragmenting", just fill options not
 *	allowed in fragments with NOOPs.
 *	Simple and stupid 8), but the most efficient way.
 */

211
void ip_options_fragment(struct sk_buff * skb)
L
Linus Torvalds 已提交
212
{
213
	unsigned char *optptr = skb_network_header(skb) + sizeof(struct iphdr);
L
Linus Torvalds 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	struct ip_options * opt = &(IPCB(skb)->opt);
	int  l = opt->optlen;
	int  optlen;

	while (l > 0) {
		switch (*optptr) {
		case IPOPT_END:
			return;
		case IPOPT_NOOP:
			l--;
			optptr++;
			continue;
		}
		optlen = optptr[1];
		if (optlen<2 || optlen>l)
		  return;
		if (!IPOPT_COPIED(*optptr))
			memset(optptr, IPOPT_NOOP, optlen);
		l -= optlen;
		optptr += optlen;
	}
	opt->ts = 0;
	opt->rr = 0;
	opt->rr_needaddr = 0;
	opt->ts_needaddr = 0;
	opt->ts_needtime = 0;
}

/*
 * Verify options and fill pointers in struct options.
 * Caller should clear *opt, and set opt->data.
 * If opt == NULL, then skb->data should point to IP header.
 */

248 249
int ip_options_compile(struct net *net,
		       struct ip_options * opt, struct sk_buff * skb)
L
Linus Torvalds 已提交
250 251 252 253 254 255
{
	int l;
	unsigned char * iph;
	unsigned char * optptr;
	int optlen;
	unsigned char * pp_ptr = NULL;
256
	struct rtable *rt = NULL;
L
Linus Torvalds 已提交
257

258
	if (skb != NULL) {
E
Eric Dumazet 已提交
259
		rt = skb_rtable(skb);
260 261
		optptr = (unsigned char *)&(ip_hdr(skb)[1]);
	} else
262
		optptr = opt->__data;
263
	iph = optptr - sizeof(struct iphdr);
L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

	for (l = opt->optlen; l > 0; ) {
		switch (*optptr) {
		      case IPOPT_END:
			for (optptr++, l--; l>0; optptr++, l--) {
				if (*optptr != IPOPT_END) {
					*optptr = IPOPT_END;
					opt->is_changed = 1;
				}
			}
			goto eol;
		      case IPOPT_NOOP:
			l--;
			optptr++;
			continue;
		}
		optlen = optptr[1];
		if (optlen<2 || optlen>l) {
			pp_ptr = optptr;
			goto error;
		}
		switch (*optptr) {
		      case IPOPT_SSRR:
		      case IPOPT_LSRR:
			if (optlen < 3) {
				pp_ptr = optptr + 1;
				goto error;
			}
			if (optptr[2] < 4) {
				pp_ptr = optptr + 2;
				goto error;
			}
			/* NB: cf RFC-1812 5.2.4.1 */
			if (opt->srr) {
				pp_ptr = optptr;
				goto error;
			}
			if (!skb) {
				if (optptr[2] != 4 || optlen < 7 || ((optlen-3) & 3)) {
					pp_ptr = optptr + 1;
					goto error;
				}
				memcpy(&opt->faddr, &optptr[3], 4);
				if (optlen > 7)
					memmove(&optptr[3], &optptr[7], optlen-7);
			}
			opt->is_strictroute = (optptr[0] == IPOPT_SSRR);
			opt->srr = optptr - iph;
			break;
		      case IPOPT_RR:
			if (opt->rr) {
				pp_ptr = optptr;
				goto error;
			}
			if (optlen < 3) {
				pp_ptr = optptr + 1;
				goto error;
			}
			if (optptr[2] < 4) {
				pp_ptr = optptr + 2;
				goto error;
			}
			if (optptr[2] <= optlen) {
				if (optptr[2]+3 > optlen) {
					pp_ptr = optptr + 2;
					goto error;
				}
331
				if (rt) {
L
Linus Torvalds 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
					memcpy(&optptr[optptr[2]-1], &rt->rt_spec_dst, 4);
					opt->is_changed = 1;
				}
				optptr[2] += 4;
				opt->rr_needaddr = 1;
			}
			opt->rr = optptr - iph;
			break;
		      case IPOPT_TIMESTAMP:
			if (opt->ts) {
				pp_ptr = optptr;
				goto error;
			}
			if (optlen < 4) {
				pp_ptr = optptr + 1;
				goto error;
			}
			if (optptr[2] < 5) {
				pp_ptr = optptr + 2;
				goto error;
			}
			if (optptr[2] <= optlen) {
354
				unsigned char *timeptr = NULL;
L
Linus Torvalds 已提交
355 356 357 358 359 360 361
				if (optptr[2]+3 > optptr[1]) {
					pp_ptr = optptr + 2;
					goto error;
				}
				switch (optptr[3]&0xF) {
				      case IPOPT_TS_TSONLY:
					opt->ts = optptr - iph;
362
					if (skb)
363
						timeptr = &optptr[optptr[2]-1];
L
Linus Torvalds 已提交
364 365 366 367 368 369 370 371 372
					opt->ts_needtime = 1;
					optptr[2] += 4;
					break;
				      case IPOPT_TS_TSANDADDR:
					if (optptr[2]+7 > optptr[1]) {
						pp_ptr = optptr + 2;
						goto error;
					}
					opt->ts = optptr - iph;
373
					if (rt)  {
L
Linus Torvalds 已提交
374
						memcpy(&optptr[optptr[2]-1], &rt->rt_spec_dst, 4);
375
						timeptr = &optptr[optptr[2]+3];
L
Linus Torvalds 已提交
376 377 378 379 380 381 382 383 384 385 386 387
					}
					opt->ts_needaddr = 1;
					opt->ts_needtime = 1;
					optptr[2] += 8;
					break;
				      case IPOPT_TS_PRESPEC:
					if (optptr[2]+7 > optptr[1]) {
						pp_ptr = optptr + 2;
						goto error;
					}
					opt->ts = optptr - iph;
					{
A
Al Viro 已提交
388
						__be32 addr;
L
Linus Torvalds 已提交
389
						memcpy(&addr, &optptr[optptr[2]-1], 4);
390
						if (inet_addr_type(net, addr) == RTN_UNICAST)
L
Linus Torvalds 已提交
391 392
							break;
						if (skb)
393
							timeptr = &optptr[optptr[2]+3];
L
Linus Torvalds 已提交
394 395 396 397 398 399 400 401 402 403 404 405
					}
					opt->ts_needtime = 1;
					optptr[2] += 8;
					break;
				      default:
					if (!skb && !capable(CAP_NET_RAW)) {
						pp_ptr = optptr + 3;
						goto error;
					}
					break;
				}
				if (timeptr) {
406
					struct timespec tv;
407
					u32  midtime;
408
					getnstimeofday(&tv);
409 410
					midtime = (tv.tv_sec % 86400) * MSEC_PER_SEC + tv.tv_nsec / NSEC_PER_MSEC;
					put_unaligned_be32(midtime, timeptr);
L
Linus Torvalds 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
					opt->is_changed = 1;
				}
			} else {
				unsigned overflow = optptr[3]>>4;
				if (overflow == 15) {
					pp_ptr = optptr + 3;
					goto error;
				}
				opt->ts = optptr - iph;
				if (skb) {
					optptr[3] = (optptr[3]&0xF)|((overflow+1)<<4);
					opt->is_changed = 1;
				}
			}
			break;
		      case IPOPT_RA:
			if (optlen < 4) {
				pp_ptr = optptr + 1;
				goto error;
			}
			if (optptr[2] == 0 && optptr[3] == 0)
				opt->router_alert = optptr - iph;
			break;
P
Paul Moore 已提交
434
		      case IPOPT_CIPSO:
435
			if ((!skb && !capable(CAP_NET_RAW)) || opt->cipso) {
P
Paul Moore 已提交
436 437 438 439
				pp_ptr = optptr;
				goto error;
			}
			opt->cipso = optptr - iph;
440
			if (cipso_v4_validate(skb, &optptr)) {
P
Paul Moore 已提交
441 442 443 444
				pp_ptr = optptr;
				goto error;
			}
			break;
L
Linus Torvalds 已提交
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
		      case IPOPT_SEC:
		      case IPOPT_SID:
		      default:
			if (!skb && !capable(CAP_NET_RAW)) {
				pp_ptr = optptr;
				goto error;
			}
			break;
		}
		l -= optlen;
		optptr += optlen;
	}

eol:
	if (!pp_ptr)
		return 0;

error:
	if (skb) {
		icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((pp_ptr-iph)<<24));
	}
	return -EINVAL;
}
468
EXPORT_SYMBOL(ip_options_compile);
L
Linus Torvalds 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500

/*
 *	Undo all the changes done by ip_options_compile().
 */

void ip_options_undo(struct ip_options * opt)
{
	if (opt->srr) {
		unsigned  char * optptr = opt->__data+opt->srr-sizeof(struct  iphdr);
		memmove(optptr+7, optptr+3, optptr[1]-7);
		memcpy(optptr+3, &opt->faddr, 4);
	}
	if (opt->rr_needaddr) {
		unsigned  char * optptr = opt->__data+opt->rr-sizeof(struct  iphdr);
		optptr[2] -= 4;
		memset(&optptr[optptr[2]-1], 0, 4);
	}
	if (opt->ts) {
		unsigned  char * optptr = opt->__data+opt->ts-sizeof(struct  iphdr);
		if (opt->ts_needtime) {
			optptr[2] -= 4;
			memset(&optptr[optptr[2]-1], 0, 4);
			if ((optptr[3]&0xF) == IPOPT_TS_PRESPEC)
				optptr[2] -= 4;
		}
		if (opt->ts_needaddr) {
			optptr[2] -= 4;
			memset(&optptr[optptr[2]-1], 0, 4);
		}
	}
}

501
static struct ip_options_rcu *ip_options_get_alloc(const int optlen)
L
Linus Torvalds 已提交
502
{
503
	return kzalloc(sizeof(struct ip_options_rcu) + ((optlen + 3) & ~3),
504
		       GFP_KERNEL);
505
}
L
Linus Torvalds 已提交
506

507 508
static int ip_options_get_finish(struct net *net, struct ip_options_rcu **optp,
				 struct ip_options_rcu *opt, int optlen)
509
{
L
Linus Torvalds 已提交
510
	while (optlen & 3)
511 512 513
		opt->opt.__data[optlen++] = IPOPT_END;
	opt->opt.optlen = optlen;
	if (optlen && ip_options_compile(net, &opt->opt, NULL)) {
L
Linus Torvalds 已提交
514 515 516
		kfree(opt);
		return -EINVAL;
	}
J
Jesper Juhl 已提交
517
	kfree(*optp);
L
Linus Torvalds 已提交
518 519 520 521
	*optp = opt;
	return 0;
}

522
int ip_options_get_from_user(struct net *net, struct ip_options_rcu **optp,
523
			     unsigned char __user *data, int optlen)
524
{
525
	struct ip_options_rcu *opt = ip_options_get_alloc(optlen);
526 527 528

	if (!opt)
		return -ENOMEM;
529
	if (optlen && copy_from_user(opt->opt.__data, data, optlen)) {
530 531 532
		kfree(opt);
		return -EFAULT;
	}
533
	return ip_options_get_finish(net, optp, opt, optlen);
534 535
}

536
int ip_options_get(struct net *net, struct ip_options_rcu **optp,
537
		   unsigned char *data, int optlen)
538
{
539
	struct ip_options_rcu *opt = ip_options_get_alloc(optlen);
540 541 542 543

	if (!opt)
		return -ENOMEM;
	if (optlen)
544
		memcpy(opt->opt.__data, data, optlen);
545
	return ip_options_get_finish(net, optp, opt, optlen);
546 547
}

L
Linus Torvalds 已提交
548 549 550 551
void ip_forward_options(struct sk_buff *skb)
{
	struct   ip_options * opt	= &(IPCB(skb)->opt);
	unsigned char * optptr;
E
Eric Dumazet 已提交
552
	struct rtable *rt = skb_rtable(skb);
553
	unsigned char *raw = skb_network_header(skb);
L
Linus Torvalds 已提交
554 555 556

	if (opt->rr_needaddr) {
		optptr = (unsigned char *)raw + opt->rr;
557
		ip_rt_get_source(&optptr[optptr[2]-5], skb, rt);
L
Linus Torvalds 已提交
558 559 560 561 562 563 564 565 566 567 568 569 570
		opt->is_changed = 1;
	}
	if (opt->srr_is_hit) {
		int srrptr, srrspace;

		optptr = raw + opt->srr;

		for ( srrptr=optptr[2], srrspace = optptr[1];
		     srrptr <= srrspace;
		     srrptr += 4
		     ) {
			if (srrptr + 3 > srrspace)
				break;
571
			if (memcmp(&opt->nexthop, &optptr[srrptr-1], 4) == 0)
L
Linus Torvalds 已提交
572 573 574 575
				break;
		}
		if (srrptr + 3 <= srrspace) {
			opt->is_changed = 1;
576
			ip_hdr(skb)->daddr = opt->nexthop;
577
			ip_rt_get_source(&optptr[srrptr-1], skb, rt);
L
Linus Torvalds 已提交
578 579 580 581 582
			optptr[2] = srrptr+4;
		} else if (net_ratelimit())
			printk(KERN_CRIT "ip_forward(): Argh! Destination lost!\n");
		if (opt->ts_needaddr) {
			optptr = raw + opt->ts;
583
			ip_rt_get_source(&optptr[optptr[2]-9], skb, rt);
L
Linus Torvalds 已提交
584 585 586 587 588
			opt->is_changed = 1;
		}
	}
	if (opt->is_changed) {
		opt->is_changed = 0;
589
		ip_send_check(ip_hdr(skb));
L
Linus Torvalds 已提交
590 591 592 593 594 595 596
	}
}

int ip_options_rcv_srr(struct sk_buff *skb)
{
	struct ip_options *opt = &(IPCB(skb)->opt);
	int srrspace, srrptr;
A
Al Viro 已提交
597
	__be32 nexthop;
598
	struct iphdr *iph = ip_hdr(skb);
599
	unsigned char *optptr = skb_network_header(skb) + opt->srr;
E
Eric Dumazet 已提交
600
	struct rtable *rt = skb_rtable(skb);
L
Linus Torvalds 已提交
601
	struct rtable *rt2;
E
Eric Dumazet 已提交
602
	unsigned long orefdst;
L
Linus Torvalds 已提交
603 604
	int err;

605
	if (!rt)
L
Linus Torvalds 已提交
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
		return 0;

	if (skb->pkt_type != PACKET_HOST)
		return -EINVAL;
	if (rt->rt_type == RTN_UNICAST) {
		if (!opt->is_strictroute)
			return 0;
		icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl(16<<24));
		return -EINVAL;
	}
	if (rt->rt_type != RTN_LOCAL)
		return -EINVAL;

	for (srrptr=optptr[2], srrspace = optptr[1]; srrptr <= srrspace; srrptr += 4) {
		if (srrptr + 3 > srrspace) {
			icmp_send(skb, ICMP_PARAMETERPROB, 0, htonl((opt->srr+2)<<24));
			return -EINVAL;
		}
		memcpy(&nexthop, &optptr[srrptr-1], 4);

E
Eric Dumazet 已提交
626
		orefdst = skb->_skb_refdst;
E
Eric Dumazet 已提交
627
		skb_dst_set(skb, NULL);
L
Linus Torvalds 已提交
628
		err = ip_route_input(skb, nexthop, iph->saddr, iph->tos, skb->dev);
E
Eric Dumazet 已提交
629
		rt2 = skb_rtable(skb);
L
Linus Torvalds 已提交
630
		if (err || (rt2->rt_type != RTN_UNICAST && rt2->rt_type != RTN_LOCAL)) {
E
Eric Dumazet 已提交
631 632
			skb_dst_drop(skb);
			skb->_skb_refdst = orefdst;
L
Linus Torvalds 已提交
633 634
			return -EINVAL;
		}
E
Eric Dumazet 已提交
635
		refdst_drop(orefdst);
L
Linus Torvalds 已提交
636 637 638
		if (rt2->rt_type != RTN_LOCAL)
			break;
		/* Superfast 8) loopback forward */
639
		iph->daddr = nexthop;
L
Linus Torvalds 已提交
640 641 642 643
		opt->is_changed = 1;
	}
	if (srrptr <= srrspace) {
		opt->srr_is_hit = 1;
644
		opt->nexthop = nexthop;
L
Linus Torvalds 已提交
645 646 647 648
		opt->is_changed = 1;
	}
	return 0;
}
649
EXPORT_SYMBOL(ip_options_rcv_srr);