xref: /freebsd/sys/netinet/ip_options.c (revision 298cf604ccf133b101c6fad42d1a078a1fac58ca)
1 /*
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *      The Regents of the University of California.
4  * Copyright (c) 2005 Andre Oppermann, Internet Business Solutions AG.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 4. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_ipstealth.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mbuf.h>
40 #include <sys/domain.h>
41 #include <sys/protosw.h>
42 #include <sys/socket.h>
43 #include <sys/time.h>
44 #include <sys/kernel.h>
45 #include <sys/syslog.h>
46 #include <sys/sysctl.h>
47 
48 #include <net/if.h>
49 #include <net/if_types.h>
50 #include <net/if_var.h>
51 #include <net/if_dl.h>
52 #include <net/route.h>
53 #include <net/netisr.h>
54 #include <net/vnet.h>
55 
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60 #include <netinet/in_pcb.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/ip_options.h>
63 #include <netinet/ip_icmp.h>
64 #include <machine/in_cksum.h>
65 
66 #include <sys/socketvar.h>
67 
68 static int	ip_dosourceroute = 0;
69 SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
70     &ip_dosourceroute, 0, "Enable forwarding source routed IP packets");
71 
72 static int	ip_acceptsourceroute = 0;
73 SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
74     CTLFLAG_RW, &ip_acceptsourceroute, 0,
75     "Enable accepting source routed IP packets");
76 
77 int		ip_doopts = 1;	/* 0 = ignore, 1 = process, 2 = reject */
78 SYSCTL_INT(_net_inet_ip, OID_AUTO, process_options, CTLFLAG_RW,
79     &ip_doopts, 0, "Enable IP options processing ([LS]SRR, RR, TS)");
80 
81 static void	save_rte(struct mbuf *m, u_char *, struct in_addr);
82 
83 /*
84  * Do option processing on a datagram, possibly discarding it if bad options
85  * are encountered, or forwarding it if source-routed.
86  *
87  * The pass argument is used when operating in the IPSTEALTH mode to tell
88  * what options to process: [LS]SRR (pass 0) or the others (pass 1).  The
89  * reason for as many as two passes is that when doing IPSTEALTH, non-routing
90  * options should be processed only if the packet is for us.
91  *
92  * Returns 1 if packet has been forwarded/freed, 0 if the packet should be
93  * processed further.
94  */
95 int
96 ip_dooptions(struct mbuf *m, int pass)
97 {
98 	struct ip *ip = mtod(m, struct ip *);
99 	u_char *cp;
100 	struct in_ifaddr *ia;
101 	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
102 	struct in_addr *sin, dst;
103 	uint32_t ntime;
104 	struct	sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
105 
106 	/* Ignore or reject packets with IP options. */
107 	if (ip_doopts == 0)
108 		return 0;
109 	else if (ip_doopts == 2) {
110 		type = ICMP_UNREACH;
111 		code = ICMP_UNREACH_FILTER_PROHIB;
112 		goto bad;
113 	}
114 
115 	dst = ip->ip_dst;
116 	cp = (u_char *)(ip + 1);
117 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
118 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
119 		opt = cp[IPOPT_OPTVAL];
120 		if (opt == IPOPT_EOL)
121 			break;
122 		if (opt == IPOPT_NOP)
123 			optlen = 1;
124 		else {
125 			if (cnt < IPOPT_OLEN + sizeof(*cp)) {
126 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
127 				goto bad;
128 			}
129 			optlen = cp[IPOPT_OLEN];
130 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
131 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
132 				goto bad;
133 			}
134 		}
135 		switch (opt) {
136 
137 		default:
138 			break;
139 
140 		/*
141 		 * Source routing with record.  Find interface with current
142 		 * destination address.  If none on this machine then drop if
143 		 * strictly routed, or do nothing if loosely routed.  Record
144 		 * interface address and bring up next address component.  If
145 		 * strictly routed make sure next address is on directly
146 		 * accessible net.
147 		 */
148 		case IPOPT_LSRR:
149 		case IPOPT_SSRR:
150 #ifdef IPSTEALTH
151 			if (V_ipstealth && pass > 0)
152 				break;
153 #endif
154 			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
155 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
156 				goto bad;
157 			}
158 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
159 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
160 				goto bad;
161 			}
162 			ipaddr.sin_addr = ip->ip_dst;
163 			if (ifa_ifwithaddr_check((struct sockaddr *)&ipaddr)
164 			    == 0) {
165 				if (opt == IPOPT_SSRR) {
166 					type = ICMP_UNREACH;
167 					code = ICMP_UNREACH_SRCFAIL;
168 					goto bad;
169 				}
170 				if (!ip_dosourceroute)
171 					goto nosourcerouting;
172 				/*
173 				 * Loose routing, and not at next destination
174 				 * yet; nothing to do except forward.
175 				 */
176 				break;
177 			}
178 			off--;			/* 0 origin */
179 			if (off > optlen - (int)sizeof(struct in_addr)) {
180 				/*
181 				 * End of source route.  Should be for us.
182 				 */
183 				if (!ip_acceptsourceroute)
184 					goto nosourcerouting;
185 				save_rte(m, cp, ip->ip_src);
186 				break;
187 			}
188 #ifdef IPSTEALTH
189 			if (V_ipstealth)
190 				goto dropit;
191 #endif
192 			if (!ip_dosourceroute) {
193 				if (V_ipforwarding) {
194 					char buf[16]; /* aaa.bbb.ccc.ddd\0 */
195 					/*
196 					 * Acting as a router, so generate
197 					 * ICMP
198 					 */
199 nosourcerouting:
200 					strcpy(buf, inet_ntoa(ip->ip_dst));
201 					log(LOG_WARNING,
202 					    "attempted source route from %s to %s\n",
203 					    inet_ntoa(ip->ip_src), buf);
204 					type = ICMP_UNREACH;
205 					code = ICMP_UNREACH_SRCFAIL;
206 					goto bad;
207 				} else {
208 					/*
209 					 * Not acting as a router, so
210 					 * silently drop.
211 					 */
212 #ifdef IPSTEALTH
213 dropit:
214 #endif
215 					IPSTAT_INC(ips_cantforward);
216 					m_freem(m);
217 					return (1);
218 				}
219 			}
220 
221 			/*
222 			 * locate outgoing interface
223 			 */
224 			(void)memcpy(&ipaddr.sin_addr, cp + off,
225 			    sizeof(ipaddr.sin_addr));
226 
227 			if (opt == IPOPT_SSRR) {
228 #define	INA	struct in_ifaddr *
229 #define	SA	struct sockaddr *
230 			    if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == NULL)
231 				    ia = (INA)ifa_ifwithnet((SA)&ipaddr, 0);
232 			} else
233 /* XXX MRT 0 for routing */
234 				ia = ip_rtaddr(ipaddr.sin_addr, M_GETFIB(m));
235 			if (ia == NULL) {
236 				type = ICMP_UNREACH;
237 				code = ICMP_UNREACH_SRCFAIL;
238 				goto bad;
239 			}
240 			ip->ip_dst = ipaddr.sin_addr;
241 			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
242 			    sizeof(struct in_addr));
243 			ifa_free(&ia->ia_ifa);
244 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
245 			/*
246 			 * Let ip_intr's mcast routing check handle mcast pkts
247 			 */
248 			forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
249 			break;
250 
251 		case IPOPT_RR:
252 #ifdef IPSTEALTH
253 			if (V_ipstealth && pass == 0)
254 				break;
255 #endif
256 			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
257 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
258 				goto bad;
259 			}
260 			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
261 				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
262 				goto bad;
263 			}
264 			/*
265 			 * If no space remains, ignore.
266 			 */
267 			off--;			/* 0 origin */
268 			if (off > optlen - (int)sizeof(struct in_addr))
269 				break;
270 			(void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
271 			    sizeof(ipaddr.sin_addr));
272 			/*
273 			 * Locate outgoing interface; if we're the
274 			 * destination, use the incoming interface (should be
275 			 * same).
276 			 */
277 			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == NULL &&
278 			    (ia = ip_rtaddr(ipaddr.sin_addr, M_GETFIB(m))) == NULL) {
279 				type = ICMP_UNREACH;
280 				code = ICMP_UNREACH_HOST;
281 				goto bad;
282 			}
283 			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
284 			    sizeof(struct in_addr));
285 			ifa_free(&ia->ia_ifa);
286 			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
287 			break;
288 
289 		case IPOPT_TS:
290 #ifdef IPSTEALTH
291 			if (V_ipstealth && pass == 0)
292 				break;
293 #endif
294 			code = cp - (u_char *)ip;
295 			if (optlen < 4 || optlen > 40) {
296 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
297 				goto bad;
298 			}
299 			if ((off = cp[IPOPT_OFFSET]) < 5) {
300 				code = &cp[IPOPT_OLEN] - (u_char *)ip;
301 				goto bad;
302 			}
303 			if (off > optlen - (int)sizeof(int32_t)) {
304 				cp[IPOPT_OFFSET + 1] += (1 << 4);
305 				if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) {
306 					code = &cp[IPOPT_OFFSET] - (u_char *)ip;
307 					goto bad;
308 				}
309 				break;
310 			}
311 			off--;				/* 0 origin */
312 			sin = (struct in_addr *)(cp + off);
313 			switch (cp[IPOPT_OFFSET + 1] & 0x0f) {
314 
315 			case IPOPT_TS_TSONLY:
316 				break;
317 
318 			case IPOPT_TS_TSANDADDR:
319 				if (off + sizeof(uint32_t) +
320 				    sizeof(struct in_addr) > optlen) {
321 					code = &cp[IPOPT_OFFSET] - (u_char *)ip;
322 					goto bad;
323 				}
324 				ipaddr.sin_addr = dst;
325 				ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
326 							    m->m_pkthdr.rcvif);
327 				if (ia == NULL)
328 					continue;
329 				(void)memcpy(sin, &IA_SIN(ia)->sin_addr,
330 				    sizeof(struct in_addr));
331 				ifa_free(&ia->ia_ifa);
332 				cp[IPOPT_OFFSET] += sizeof(struct in_addr);
333 				off += sizeof(struct in_addr);
334 				break;
335 
336 			case IPOPT_TS_PRESPEC:
337 				if (off + sizeof(uint32_t) +
338 				    sizeof(struct in_addr) > optlen) {
339 					code = &cp[IPOPT_OFFSET] - (u_char *)ip;
340 					goto bad;
341 				}
342 				(void)memcpy(&ipaddr.sin_addr, sin,
343 				    sizeof(struct in_addr));
344 				if (ifa_ifwithaddr_check((SA)&ipaddr) == 0)
345 					continue;
346 				cp[IPOPT_OFFSET] += sizeof(struct in_addr);
347 				off += sizeof(struct in_addr);
348 				break;
349 
350 			default:
351 				code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip;
352 				goto bad;
353 			}
354 			ntime = iptime();
355 			(void)memcpy(cp + off, &ntime, sizeof(uint32_t));
356 			cp[IPOPT_OFFSET] += sizeof(uint32_t);
357 		}
358 	}
359 	if (forward && V_ipforwarding) {
360 		ip_forward(m, 1);
361 		return (1);
362 	}
363 	return (0);
364 bad:
365 	icmp_error(m, type, code, 0, 0);
366 	IPSTAT_INC(ips_badoptions);
367 	return (1);
368 }
369 
370 /*
371  * Save incoming source route for use in replies, to be picked up later by
372  * ip_srcroute if the receiver is interested.
373  */
374 static void
375 save_rte(struct mbuf *m, u_char *option, struct in_addr dst)
376 {
377 	unsigned olen;
378 	struct ipopt_tag *opts;
379 
380 	opts = (struct ipopt_tag *)m_tag_get(PACKET_TAG_IPOPTIONS,
381 	    sizeof(struct ipopt_tag), M_NOWAIT);
382 	if (opts == NULL)
383 		return;
384 
385 	olen = option[IPOPT_OLEN];
386 	if (olen > sizeof(opts->ip_srcrt) - (1 + sizeof(dst))) {
387 		m_tag_free((struct m_tag *)opts);
388 		return;
389 	}
390 	bcopy(option, opts->ip_srcrt.srcopt, olen);
391 	opts->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
392 	opts->ip_srcrt.dst = dst;
393 	m_tag_prepend(m, (struct m_tag *)opts);
394 }
395 
396 /*
397  * Retrieve incoming source route for use in replies, in the same form used
398  * by setsockopt.  The first hop is placed before the options, will be
399  * removed later.
400  */
401 struct mbuf *
402 ip_srcroute(struct mbuf *m0)
403 {
404 	struct in_addr *p, *q;
405 	struct mbuf *m;
406 	struct ipopt_tag *opts;
407 
408 	opts = (struct ipopt_tag *)m_tag_find(m0, PACKET_TAG_IPOPTIONS, NULL);
409 	if (opts == NULL)
410 		return (NULL);
411 
412 	if (opts->ip_nhops == 0)
413 		return (NULL);
414 	m = m_get(M_NOWAIT, MT_DATA);
415 	if (m == NULL)
416 		return (NULL);
417 
418 #define OPTSIZ	(sizeof(opts->ip_srcrt.nop) + sizeof(opts->ip_srcrt.srcopt))
419 
420 	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
421 	m->m_len = opts->ip_nhops * sizeof(struct in_addr) +
422 	    sizeof(struct in_addr) + OPTSIZ;
423 
424 	/*
425 	 * First, save first hop for return route.
426 	 */
427 	p = &(opts->ip_srcrt.route[opts->ip_nhops - 1]);
428 	*(mtod(m, struct in_addr *)) = *p--;
429 
430 	/*
431 	 * Copy option fields and padding (nop) to mbuf.
432 	 */
433 	opts->ip_srcrt.nop = IPOPT_NOP;
434 	opts->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
435 	(void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
436 	    &(opts->ip_srcrt.nop), OPTSIZ);
437 	q = (struct in_addr *)(mtod(m, caddr_t) +
438 	    sizeof(struct in_addr) + OPTSIZ);
439 #undef OPTSIZ
440 	/*
441 	 * Record return path as an IP source route, reversing the path
442 	 * (pointers are now aligned).
443 	 */
444 	while (p >= opts->ip_srcrt.route) {
445 		*q++ = *p--;
446 	}
447 	/*
448 	 * Last hop goes to final destination.
449 	 */
450 	*q = opts->ip_srcrt.dst;
451 	m_tag_delete(m0, (struct m_tag *)opts);
452 	return (m);
453 }
454 
455 /*
456  * Strip out IP options, at higher level protocol in the kernel.
457  */
458 void
459 ip_stripoptions(struct mbuf *m)
460 {
461 	struct ip *ip = mtod(m, struct ip *);
462 	int olen;
463 
464 	olen = (ip->ip_hl << 2) - sizeof(struct ip);
465 	m->m_len -= olen;
466 	if (m->m_flags & M_PKTHDR)
467 		m->m_pkthdr.len -= olen;
468 	ip->ip_len = htons(ntohs(ip->ip_len) - olen);
469 	ip->ip_hl = sizeof(struct ip) >> 2;
470 
471 	bcopy((char *)ip + sizeof(struct ip) + olen, (ip + 1),
472 	    (size_t )(m->m_len - sizeof(struct ip)));
473 }
474 
475 /*
476  * Insert IP options into preformed packet.  Adjust IP destination as
477  * required for IP source routing, as indicated by a non-zero in_addr at the
478  * start of the options.
479  *
480  * XXX This routine assumes that the packet has no options in place.
481  */
482 struct mbuf *
483 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
484 {
485 	struct ipoption *p = mtod(opt, struct ipoption *);
486 	struct mbuf *n;
487 	struct ip *ip = mtod(m, struct ip *);
488 	unsigned optlen;
489 
490 	optlen = opt->m_len - sizeof(p->ipopt_dst);
491 	if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET) {
492 		*phlen = 0;
493 		return (m);		/* XXX should fail */
494 	}
495 	if (p->ipopt_dst.s_addr)
496 		ip->ip_dst = p->ipopt_dst;
497 	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
498 		MGETHDR(n, M_NOWAIT, MT_DATA);
499 		if (n == NULL) {
500 			*phlen = 0;
501 			return (m);
502 		}
503 		M_MOVE_PKTHDR(n, m);
504 		n->m_pkthdr.rcvif = NULL;
505 		n->m_pkthdr.len += optlen;
506 		m->m_len -= sizeof(struct ip);
507 		m->m_data += sizeof(struct ip);
508 		n->m_next = m;
509 		m = n;
510 		m->m_len = optlen + sizeof(struct ip);
511 		m->m_data += max_linkhdr;
512 		bcopy(ip, mtod(m, void *), sizeof(struct ip));
513 	} else {
514 		m->m_data -= optlen;
515 		m->m_len += optlen;
516 		m->m_pkthdr.len += optlen;
517 		bcopy(ip, mtod(m, void *), sizeof(struct ip));
518 	}
519 	ip = mtod(m, struct ip *);
520 	bcopy(p->ipopt_list, ip + 1, optlen);
521 	*phlen = sizeof(struct ip) + optlen;
522 	ip->ip_v = IPVERSION;
523 	ip->ip_hl = *phlen >> 2;
524 	ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
525 	return (m);
526 }
527 
528 /*
529  * Copy options from ip to jp, omitting those not copied during
530  * fragmentation.
531  */
532 int
533 ip_optcopy(struct ip *ip, struct ip *jp)
534 {
535 	u_char *cp, *dp;
536 	int opt, optlen, cnt;
537 
538 	cp = (u_char *)(ip + 1);
539 	dp = (u_char *)(jp + 1);
540 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
541 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
542 		opt = cp[0];
543 		if (opt == IPOPT_EOL)
544 			break;
545 		if (opt == IPOPT_NOP) {
546 			/* Preserve for IP mcast tunnel's LSRR alignment. */
547 			*dp++ = IPOPT_NOP;
548 			optlen = 1;
549 			continue;
550 		}
551 
552 		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
553 		    ("ip_optcopy: malformed ipv4 option"));
554 		optlen = cp[IPOPT_OLEN];
555 		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
556 		    ("ip_optcopy: malformed ipv4 option"));
557 
558 		/* Bogus lengths should have been caught by ip_dooptions. */
559 		if (optlen > cnt)
560 			optlen = cnt;
561 		if (IPOPT_COPIED(opt)) {
562 			bcopy(cp, dp, optlen);
563 			dp += optlen;
564 		}
565 	}
566 	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
567 		*dp++ = IPOPT_EOL;
568 	return (optlen);
569 }
570 
571 /*
572  * Set up IP options in pcb for insertion in output packets.  Store in mbuf
573  * with pointer in pcbopt, adding pseudo-option with destination address if
574  * source routed.
575  */
576 int
577 ip_pcbopts(struct inpcb *inp, int optname, struct mbuf *m)
578 {
579 	int cnt, optlen;
580 	u_char *cp;
581 	struct mbuf **pcbopt;
582 	u_char opt;
583 
584 	INP_WLOCK_ASSERT(inp);
585 
586 	pcbopt = &inp->inp_options;
587 
588 	/* turn off any old options */
589 	if (*pcbopt)
590 		(void)m_free(*pcbopt);
591 	*pcbopt = 0;
592 	if (m == NULL || m->m_len == 0) {
593 		/*
594 		 * Only turning off any previous options.
595 		 */
596 		if (m != NULL)
597 			(void)m_free(m);
598 		return (0);
599 	}
600 
601 	if (m->m_len % sizeof(int32_t))
602 		goto bad;
603 	/*
604 	 * IP first-hop destination address will be stored before actual
605 	 * options; move other options back and clear it when none present.
606 	 */
607 	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
608 		goto bad;
609 	cnt = m->m_len;
610 	m->m_len += sizeof(struct in_addr);
611 	cp = mtod(m, u_char *) + sizeof(struct in_addr);
612 	bcopy(mtod(m, void *), cp, (unsigned)cnt);
613 	bzero(mtod(m, void *), sizeof(struct in_addr));
614 
615 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
616 		opt = cp[IPOPT_OPTVAL];
617 		if (opt == IPOPT_EOL)
618 			break;
619 		if (opt == IPOPT_NOP)
620 			optlen = 1;
621 		else {
622 			if (cnt < IPOPT_OLEN + sizeof(*cp))
623 				goto bad;
624 			optlen = cp[IPOPT_OLEN];
625 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
626 				goto bad;
627 		}
628 		switch (opt) {
629 
630 		default:
631 			break;
632 
633 		case IPOPT_LSRR:
634 		case IPOPT_SSRR:
635 			/*
636 			 * User process specifies route as:
637 			 *
638 			 *	->A->B->C->D
639 			 *
640 			 * D must be our final destination (but we can't
641 			 * check that since we may not have connected yet).
642 			 * A is first hop destination, which doesn't appear
643 			 * in actual IP option, but is stored before the
644 			 * options.
645 			 */
646 			/* XXX-BZ PRIV_NETINET_SETHDROPTS? */
647 			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
648 				goto bad;
649 			m->m_len -= sizeof(struct in_addr);
650 			cnt -= sizeof(struct in_addr);
651 			optlen -= sizeof(struct in_addr);
652 			cp[IPOPT_OLEN] = optlen;
653 			/*
654 			 * Move first hop before start of options.
655 			 */
656 			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
657 			    sizeof(struct in_addr));
658 			/*
659 			 * Then copy rest of options back
660 			 * to close up the deleted entry.
661 			 */
662 			bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
663 			    &cp[IPOPT_OFFSET+1],
664 			    (unsigned)cnt - (IPOPT_MINOFF - 1));
665 			break;
666 		}
667 	}
668 	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
669 		goto bad;
670 	*pcbopt = m;
671 	return (0);
672 
673 bad:
674 	(void)m_free(m);
675 	return (EINVAL);
676 }
677 
678 /*
679  * Check for the presence of the IP Router Alert option [RFC2113]
680  * in the header of an IPv4 datagram.
681  *
682  * This call is not intended for use from the forwarding path; it is here
683  * so that protocol domains may check for the presence of the option.
684  * Given how FreeBSD's IPv4 stack is currently structured, the Router Alert
685  * option does not have much relevance to the implementation, though this
686  * may change in future.
687  * Router alert options SHOULD be passed if running in IPSTEALTH mode and
688  * we are not the endpoint.
689  * Length checks on individual options should already have been peformed
690  * by ip_dooptions() therefore they are folded under INVARIANTS here.
691  *
692  * Return zero if not present or options are invalid, non-zero if present.
693  */
694 int
695 ip_checkrouteralert(struct mbuf *m)
696 {
697 	struct ip *ip = mtod(m, struct ip *);
698 	u_char *cp;
699 	int opt, optlen, cnt, found_ra;
700 
701 	found_ra = 0;
702 	cp = (u_char *)(ip + 1);
703 	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
704 	for (; cnt > 0; cnt -= optlen, cp += optlen) {
705 		opt = cp[IPOPT_OPTVAL];
706 		if (opt == IPOPT_EOL)
707 			break;
708 		if (opt == IPOPT_NOP)
709 			optlen = 1;
710 		else {
711 #ifdef INVARIANTS
712 			if (cnt < IPOPT_OLEN + sizeof(*cp))
713 				break;
714 #endif
715 			optlen = cp[IPOPT_OLEN];
716 #ifdef INVARIANTS
717 			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
718 				break;
719 #endif
720 		}
721 		switch (opt) {
722 		case IPOPT_RA:
723 #ifdef INVARIANTS
724 			if (optlen != IPOPT_OFFSET + sizeof(uint16_t) ||
725 			    (*((uint16_t *)&cp[IPOPT_OFFSET]) != 0))
726 			    break;
727 			else
728 #endif
729 			found_ra = 1;
730 			break;
731 		default:
732 			break;
733 		}
734 	}
735 
736 	return (found_ra);
737 }
738