ip_options.c 15.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9
/*
 * 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
10
 *
L
Linus Torvalds 已提交
11 12
 */

13 14
#define pr_fmt(fmt) "IPv4: " fmt

15
#include <linux/capability.h>
L
Linus Torvalds 已提交
16
#include <linux/module.h>
17
#include <linux/slab.h>
L
Linus Torvalds 已提交
18
#include <linux/types.h>
19
#include <linux/uaccess.h>
20
#include <asm/unaligned.h>
L
Linus Torvalds 已提交
21 22 23 24 25 26 27 28
#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>
29
#include <net/route.h>
P
Paul Moore 已提交
30
#include <net/cipso_ipv4.h>
31
#include <net/ip_fib.h>
L
Linus Torvalds 已提交
32

33
/*
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42 43
 * 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.
 */

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

	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)
58
			ip_rt_get_source(iph+opt->rr+iph[opt->rr+2]-5, skb, rt);
L
Linus Torvalds 已提交
59
		if (opt->ts_needaddr)
60
			ip_rt_get_source(iph+opt->ts+iph[opt->ts+2]-9, skb, rt);
L
Linus Torvalds 已提交
61
		if (opt->ts_needtime) {
62
			__be32 midtime;
63 64

			midtime = inet_current_timestamp();
L
Linus Torvalds 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
			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;
	}
}

81
/*
L
Linus Torvalds 已提交
82 83 84 85 86 87 88 89
 * 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.
 */

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

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

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

102
	sptr = skb_network_header(skb);
L
Linus Torvalds 已提交
103 104 105 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
	dptr = dopt->__data;

	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;

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

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

		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);
W
Weilong Chen 已提交
168
			for (soffset -= 4, doffset = 4; soffset > 3; soffset -= 4, doffset += 4)
L
Linus Torvalds 已提交
169 170 171 172
				memcpy(&dptr[doffset-1], &start[soffset-1], 4);
			/*
			 * RFC1812 requires to fix illegal source routes.
			 */
173 174
			if (memcmp(&ip_hdr(skb)->saddr,
				   &start[soffset + 3], 4) == 0)
L
Linus Torvalds 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187
				doffset -= 4;
		}
		if (doffset > 3) {
			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 已提交
188 189 190 191 192 193 194
	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 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207
	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.
 */

D
Daniel Baluta 已提交
208
void ip_options_fragment(struct sk_buff *skb)
L
Linus Torvalds 已提交
209
{
210
	unsigned char *optptr = skb_network_header(skb) + sizeof(struct iphdr);
D
Daniel Baluta 已提交
211
	struct ip_options *opt = &(IPCB(skb)->opt);
L
Linus Torvalds 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224
	int  l = opt->optlen;
	int  optlen;

	while (l > 0) {
		switch (*optptr) {
		case IPOPT_END:
			return;
		case IPOPT_NOOP:
			l--;
			optptr++;
			continue;
		}
		optlen = optptr[1];
W
Weilong Chen 已提交
225
		if (optlen < 2 || optlen > l)
L
Linus Torvalds 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238
		  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;
}

239 240 241 242 243 244 245 246 247
/* helper used by ip_options_compile() to call fib_compute_spec_dst()
 * at most one time.
 */
static void spec_dst_fill(__be32 *spec_dst, struct sk_buff *skb)
{
	if (*spec_dst == htonl(INADDR_ANY))
		*spec_dst = fib_compute_spec_dst(skb);
}

L
Linus Torvalds 已提交
248 249 250 251 252 253
/*
 * 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.
 */

254 255 256
int __ip_options_compile(struct net *net,
			 struct ip_options *opt, struct sk_buff *skb,
			 __be32 *info)
L
Linus Torvalds 已提交
257
{
258
	__be32 spec_dst = htonl(INADDR_ANY);
D
Daniel Baluta 已提交
259
	unsigned char *pp_ptr = NULL;
260
	struct rtable *rt = NULL;
261 262 263
	unsigned char *optptr;
	unsigned char *iph;
	int optlen, l;
L
Linus Torvalds 已提交
264

265
	if (skb) {
266
		rt = skb_rtable(skb);
267 268
		optptr = (unsigned char *)&(ip_hdr(skb)[1]);
	} else
269
		optptr = opt->__data;
270
	iph = optptr - sizeof(struct iphdr);
L
Linus Torvalds 已提交
271 272 273

	for (l = opt->optlen; l > 0; ) {
		switch (*optptr) {
274
		case IPOPT_END:
W
Weilong Chen 已提交
275
			for (optptr++, l--; l > 0; optptr++, l--) {
L
Linus Torvalds 已提交
276 277 278 279 280 281
				if (*optptr != IPOPT_END) {
					*optptr = IPOPT_END;
					opt->is_changed = 1;
				}
			}
			goto eol;
282
		case IPOPT_NOOP:
L
Linus Torvalds 已提交
283 284 285 286
			l--;
			optptr++;
			continue;
		}
287 288 289 290
		if (unlikely(l < 2)) {
			pp_ptr = optptr;
			goto error;
		}
L
Linus Torvalds 已提交
291
		optlen = optptr[1];
W
Weilong Chen 已提交
292
		if (optlen < 2 || optlen > l) {
L
Linus Torvalds 已提交
293 294 295 296
			pp_ptr = optptr;
			goto error;
		}
		switch (*optptr) {
297 298
		case IPOPT_SSRR:
		case IPOPT_LSRR:
L
Linus Torvalds 已提交
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
			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;
324
		case IPOPT_RR:
L
Linus Torvalds 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
			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;
				}
342
				if (rt) {
343
					spec_dst_fill(&spec_dst, skb);
344
					memcpy(&optptr[optptr[2]-1], &spec_dst, 4);
L
Linus Torvalds 已提交
345 346 347 348 349 350 351
					opt->is_changed = 1;
				}
				optptr[2] += 4;
				opt->rr_needaddr = 1;
			}
			opt->rr = optptr - iph;
			break;
352
		case IPOPT_TIMESTAMP:
L
Linus Torvalds 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365
			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) {
366
				unsigned char *timeptr = NULL;
367
				if (optptr[2]+3 > optlen) {
L
Linus Torvalds 已提交
368 369 370 371
					pp_ptr = optptr + 2;
					goto error;
				}
				switch (optptr[3]&0xF) {
372
				case IPOPT_TS_TSONLY:
373
					if (skb)
374
						timeptr = &optptr[optptr[2]-1];
L
Linus Torvalds 已提交
375 376 377
					opt->ts_needtime = 1;
					optptr[2] += 4;
					break;
378
				case IPOPT_TS_TSANDADDR:
379
					if (optptr[2]+7 > optlen) {
L
Linus Torvalds 已提交
380 381 382
						pp_ptr = optptr + 2;
						goto error;
					}
383
					if (rt)  {
384
						spec_dst_fill(&spec_dst, skb);
385
						memcpy(&optptr[optptr[2]-1], &spec_dst, 4);
386
						timeptr = &optptr[optptr[2]+3];
L
Linus Torvalds 已提交
387 388 389 390 391
					}
					opt->ts_needaddr = 1;
					opt->ts_needtime = 1;
					optptr[2] += 8;
					break;
392
				case IPOPT_TS_PRESPEC:
393
					if (optptr[2]+7 > optlen) {
L
Linus Torvalds 已提交
394 395 396 397
						pp_ptr = optptr + 2;
						goto error;
					}
					{
A
Al Viro 已提交
398
						__be32 addr;
L
Linus Torvalds 已提交
399
						memcpy(&addr, &optptr[optptr[2]-1], 4);
400
						if (inet_addr_type(net, addr) == RTN_UNICAST)
L
Linus Torvalds 已提交
401 402
							break;
						if (skb)
403
							timeptr = &optptr[optptr[2]+3];
L
Linus Torvalds 已提交
404 405 406 407
					}
					opt->ts_needtime = 1;
					optptr[2] += 8;
					break;
408
				default:
409
					if (!skb && !ns_capable(net->user_ns, CAP_NET_RAW)) {
L
Linus Torvalds 已提交
410 411 412 413 414 415
						pp_ptr = optptr + 3;
						goto error;
					}
					break;
				}
				if (timeptr) {
416 417 418 419
					__be32 midtime;

					midtime = inet_current_timestamp();
					memcpy(timeptr, &midtime, 4);
L
Linus Torvalds 已提交
420 421
					opt->is_changed = 1;
				}
422
			} else if ((optptr[3]&0xF) != IPOPT_TS_PRESPEC) {
423
				unsigned int overflow = optptr[3]>>4;
L
Linus Torvalds 已提交
424 425 426 427 428 429 430 431 432
				if (overflow == 15) {
					pp_ptr = optptr + 3;
					goto error;
				}
				if (skb) {
					optptr[3] = (optptr[3]&0xF)|((overflow+1)<<4);
					opt->is_changed = 1;
				}
			}
433
			opt->ts = optptr - iph;
L
Linus Torvalds 已提交
434
			break;
435
		case IPOPT_RA:
L
Linus Torvalds 已提交
436 437 438 439 440 441 442
			if (optlen < 4) {
				pp_ptr = optptr + 1;
				goto error;
			}
			if (optptr[2] == 0 && optptr[3] == 0)
				opt->router_alert = optptr - iph;
			break;
443
		case IPOPT_CIPSO:
444
			if ((!skb && !ns_capable(net->user_ns, CAP_NET_RAW)) || opt->cipso) {
P
Paul Moore 已提交
445 446 447 448
				pp_ptr = optptr;
				goto error;
			}
			opt->cipso = optptr - iph;
449
			if (cipso_v4_validate(skb, &optptr)) {
P
Paul Moore 已提交
450 451 452 453
				pp_ptr = optptr;
				goto error;
			}
			break;
454 455 456
		case IPOPT_SEC:
		case IPOPT_SID:
		default:
457
			if (!skb && !ns_capable(net->user_ns, CAP_NET_RAW)) {
L
Linus Torvalds 已提交
458 459 460 461 462 463 464 465 466 467 468 469 470 471
				pp_ptr = optptr;
				goto error;
			}
			break;
		}
		l -= optlen;
		optptr += optlen;
	}

eol:
	if (!pp_ptr)
		return 0;

error:
472 473
	if (info)
		*info = htonl((pp_ptr-iph)<<24);
L
Linus Torvalds 已提交
474 475
	return -EINVAL;
}
476 477 478 479 480 481 482 483 484 485 486 487

int ip_options_compile(struct net *net,
		       struct ip_options *opt, struct sk_buff *skb)
{
	int ret;
	__be32 info;

	ret = __ip_options_compile(net, opt, skb, &info);
	if (ret != 0 && skb)
		icmp_send(skb, ICMP_PARAMETERPROB, 0, info);
	return ret;
}
488
EXPORT_SYMBOL(ip_options_compile);
L
Linus Torvalds 已提交
489 490 491 492 493

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

D
Daniel Baluta 已提交
494
void ip_options_undo(struct ip_options *opt)
L
Linus Torvalds 已提交
495 496
{
	if (opt->srr) {
D
Daniel Baluta 已提交
497
		unsigned  char *optptr = opt->__data+opt->srr-sizeof(struct  iphdr);
L
Linus Torvalds 已提交
498 499 500 501
		memmove(optptr+7, optptr+3, optptr[1]-7);
		memcpy(optptr+3, &opt->faddr, 4);
	}
	if (opt->rr_needaddr) {
D
Daniel Baluta 已提交
502
		unsigned  char *optptr = opt->__data+opt->rr-sizeof(struct  iphdr);
L
Linus Torvalds 已提交
503 504 505 506
		optptr[2] -= 4;
		memset(&optptr[optptr[2]-1], 0, 4);
	}
	if (opt->ts) {
D
Daniel Baluta 已提交
507
		unsigned  char *optptr = opt->__data+opt->ts-sizeof(struct  iphdr);
L
Linus Torvalds 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520
		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);
		}
	}
}

521
static struct ip_options_rcu *ip_options_get_alloc(const int optlen)
L
Linus Torvalds 已提交
522
{
523
	return kzalloc(sizeof(struct ip_options_rcu) + ((optlen + 3) & ~3),
524
		       GFP_KERNEL);
525
}
L
Linus Torvalds 已提交
526

527 528
static int ip_options_get_finish(struct net *net, struct ip_options_rcu **optp,
				 struct ip_options_rcu *opt, int optlen)
529
{
L
Linus Torvalds 已提交
530
	while (optlen & 3)
531 532 533
		opt->opt.__data[optlen++] = IPOPT_END;
	opt->opt.optlen = optlen;
	if (optlen && ip_options_compile(net, &opt->opt, NULL)) {
L
Linus Torvalds 已提交
534 535 536
		kfree(opt);
		return -EINVAL;
	}
J
Jesper Juhl 已提交
537
	kfree(*optp);
L
Linus Torvalds 已提交
538 539 540 541
	*optp = opt;
	return 0;
}

542
int ip_options_get_from_user(struct net *net, struct ip_options_rcu **optp,
543
			     unsigned char __user *data, int optlen)
544
{
545
	struct ip_options_rcu *opt = ip_options_get_alloc(optlen);
546 547 548

	if (!opt)
		return -ENOMEM;
549
	if (optlen && copy_from_user(opt->opt.__data, data, optlen)) {
550 551 552
		kfree(opt);
		return -EFAULT;
	}
553
	return ip_options_get_finish(net, optp, opt, optlen);
554 555
}

556
int ip_options_get(struct net *net, struct ip_options_rcu **optp,
557
		   unsigned char *data, int optlen)
558
{
559
	struct ip_options_rcu *opt = ip_options_get_alloc(optlen);
560 561 562 563

	if (!opt)
		return -ENOMEM;
	if (optlen)
564
		memcpy(opt->opt.__data, data, optlen);
565
	return ip_options_get_finish(net, optp, opt, optlen);
566 567
}

L
Linus Torvalds 已提交
568 569
void ip_forward_options(struct sk_buff *skb)
{
D
Daniel Baluta 已提交
570 571
	struct   ip_options *opt	= &(IPCB(skb)->opt);
	unsigned char *optptr;
E
Eric Dumazet 已提交
572
	struct rtable *rt = skb_rtable(skb);
573
	unsigned char *raw = skb_network_header(skb);
L
Linus Torvalds 已提交
574 575 576

	if (opt->rr_needaddr) {
		optptr = (unsigned char *)raw + opt->rr;
577
		ip_rt_get_source(&optptr[optptr[2]-5], skb, rt);
L
Linus Torvalds 已提交
578 579 580 581 582 583 584
		opt->is_changed = 1;
	}
	if (opt->srr_is_hit) {
		int srrptr, srrspace;

		optptr = raw + opt->srr;

W
Weilong Chen 已提交
585
		for ( srrptr = optptr[2], srrspace = optptr[1];
L
Linus Torvalds 已提交
586 587 588 589 590
		     srrptr <= srrspace;
		     srrptr += 4
		     ) {
			if (srrptr + 3 > srrspace)
				break;
591
			if (memcmp(&opt->nexthop, &optptr[srrptr-1], 4) == 0)
L
Linus Torvalds 已提交
592 593 594 595
				break;
		}
		if (srrptr + 3 <= srrspace) {
			opt->is_changed = 1;
596
			ip_hdr(skb)->daddr = opt->nexthop;
597
			ip_rt_get_source(&optptr[srrptr-1], skb, rt);
L
Linus Torvalds 已提交
598
			optptr[2] = srrptr+4;
599 600 601 602
		} else {
			net_crit_ratelimited("%s(): Argh! Destination lost!\n",
					     __func__);
		}
L
Linus Torvalds 已提交
603 604
		if (opt->ts_needaddr) {
			optptr = raw + opt->ts;
605
			ip_rt_get_source(&optptr[optptr[2]-9], skb, rt);
L
Linus Torvalds 已提交
606 607 608 609 610
			opt->is_changed = 1;
		}
	}
	if (opt->is_changed) {
		opt->is_changed = 0;
611
		ip_send_check(ip_hdr(skb));
L
Linus Torvalds 已提交
612 613 614 615 616 617 618
	}
}

int ip_options_rcv_srr(struct sk_buff *skb)
{
	struct ip_options *opt = &(IPCB(skb)->opt);
	int srrspace, srrptr;
A
Al Viro 已提交
619
	__be32 nexthop;
620
	struct iphdr *iph = ip_hdr(skb);
621
	unsigned char *optptr = skb_network_header(skb) + opt->srr;
E
Eric Dumazet 已提交
622
	struct rtable *rt = skb_rtable(skb);
L
Linus Torvalds 已提交
623
	struct rtable *rt2;
E
Eric Dumazet 已提交
624
	unsigned long orefdst;
L
Linus Torvalds 已提交
625 626
	int err;

627
	if (!rt)
L
Linus Torvalds 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640
		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;

W
Weilong Chen 已提交
641
	for (srrptr = optptr[2], srrspace = optptr[1]; srrptr <= srrspace; srrptr += 4) {
L
Linus Torvalds 已提交
642 643 644 645 646 647
		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 已提交
648
		orefdst = skb->_skb_refdst;
E
Eric Dumazet 已提交
649
		skb_dst_set(skb, NULL);
L
Linus Torvalds 已提交
650
		err = ip_route_input(skb, nexthop, iph->saddr, iph->tos, skb->dev);
E
Eric Dumazet 已提交
651
		rt2 = skb_rtable(skb);
L
Linus Torvalds 已提交
652
		if (err || (rt2->rt_type != RTN_UNICAST && rt2->rt_type != RTN_LOCAL)) {
E
Eric Dumazet 已提交
653 654
			skb_dst_drop(skb);
			skb->_skb_refdst = orefdst;
L
Linus Torvalds 已提交
655 656
			return -EINVAL;
		}
E
Eric Dumazet 已提交
657
		refdst_drop(orefdst);
L
Linus Torvalds 已提交
658 659 660
		if (rt2->rt_type != RTN_LOCAL)
			break;
		/* Superfast 8) loopback forward */
661
		iph->daddr = nexthop;
L
Linus Torvalds 已提交
662 663 664 665
		opt->is_changed = 1;
	}
	if (srrptr <= srrspace) {
		opt->srr_is_hit = 1;
666
		opt->nexthop = nexthop;
L
Linus Torvalds 已提交
667 668 669 670
		opt->is_changed = 1;
	}
	return 0;
}
671
EXPORT_SYMBOL(ip_options_rcv_srr);