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