1152592Sandre/*
2152592Sandre * Copyright (c) 1982, 1986, 1988, 1993
3169464Srwatson *      The Regents of the University of California.
4169464Srwatson * Copyright (c) 2005 Andre Oppermann, Internet Business Solutions AG.
5169464Srwatson * All rights reserved.
6152592Sandre *
7152592Sandre * Redistribution and use in source and binary forms, with or without
8152592Sandre * modification, are permitted provided that the following conditions
9152592Sandre * are met:
10152592Sandre * 1. Redistributions of source code must retain the above copyright
11152592Sandre *    notice, this list of conditions and the following disclaimer.
12152592Sandre * 2. Redistributions in binary form must reproduce the above copyright
13152592Sandre *    notice, this list of conditions and the following disclaimer in the
14152592Sandre *    documentation and/or other materials provided with the distribution.
15152592Sandre * 4. Neither the name of the University nor the names of its contributors
16152592Sandre *    may be used to endorse or promote products derived from this software
17152592Sandre *    without specific prior written permission.
18152592Sandre *
19152592Sandre * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20152592Sandre * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21152592Sandre * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22152592Sandre * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23152592Sandre * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24152592Sandre * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25152592Sandre * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26152592Sandre * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27152592Sandre * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28152592Sandre * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29152592Sandre * SUCH DAMAGE.
30152592Sandre */
31152592Sandre
32172467Ssilby#include <sys/cdefs.h>
33172467Ssilby__FBSDID("$FreeBSD: stable/11/sys/netinet/ip_options.c 315456 2017-03-17 14:54:10Z vangyzen $");
34172467Ssilby
35152592Sandre#include "opt_ipstealth.h"
36152592Sandre
37152592Sandre#include <sys/param.h>
38152592Sandre#include <sys/systm.h>
39152592Sandre#include <sys/mbuf.h>
40152592Sandre#include <sys/domain.h>
41152592Sandre#include <sys/protosw.h>
42152592Sandre#include <sys/socket.h>
43152592Sandre#include <sys/time.h>
44152592Sandre#include <sys/kernel.h>
45152592Sandre#include <sys/syslog.h>
46152592Sandre#include <sys/sysctl.h>
47152592Sandre
48152592Sandre#include <net/if.h>
49152592Sandre#include <net/if_types.h>
50152592Sandre#include <net/if_var.h>
51152592Sandre#include <net/if_dl.h>
52152592Sandre#include <net/route.h>
53152592Sandre#include <net/netisr.h>
54196019Srwatson#include <net/vnet.h>
55152592Sandre
56152592Sandre#include <netinet/in.h>
57291993Smelifaro#include <netinet/in_fib.h>
58152592Sandre#include <netinet/in_systm.h>
59152592Sandre#include <netinet/in_var.h>
60152592Sandre#include <netinet/ip.h>
61152592Sandre#include <netinet/in_pcb.h>
62152592Sandre#include <netinet/ip_var.h>
63152592Sandre#include <netinet/ip_options.h>
64152592Sandre#include <netinet/ip_icmp.h>
65152592Sandre#include <machine/in_cksum.h>
66152592Sandre
67152592Sandre#include <sys/socketvar.h>
68152592Sandre
69271610Shrsstatic VNET_DEFINE(int, ip_dosourceroute);
70271628ShrsSYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute,
71271628Shrs    CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_dosourceroute), 0,
72271610Shrs    "Enable forwarding source routed IP packets");
73271610Shrs#define	V_ip_dosourceroute	VNET(ip_dosourceroute)
74152592Sandre
75271610Shrsstatic VNET_DEFINE(int,	ip_acceptsourceroute);
76271628ShrsSYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
77271628Shrs    CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_acceptsourceroute), 0,
78152592Sandre    "Enable accepting source routed IP packets");
79271610Shrs#define	V_ip_acceptsourceroute	VNET(ip_acceptsourceroute)
80152592Sandre
81271610ShrsVNET_DEFINE(int, ip_doopts) = 1; /* 0 = ignore, 1 = process, 2 = reject */
82271628ShrsSYSCTL_INT(_net_inet_ip, OID_AUTO, process_options, CTLFLAG_VNET | CTLFLAG_RW,
83271610Shrs    &VNET_NAME(ip_doopts), 0, "Enable IP options processing ([LS]SRR, RR, TS)");
84152592Sandre
85152592Sandrestatic void	save_rte(struct mbuf *m, u_char *, struct in_addr);
86152592Sandre
87152592Sandre/*
88169464Srwatson * Do option processing on a datagram, possibly discarding it if bad options
89169464Srwatson * are encountered, or forwarding it if source-routed.
90169464Srwatson *
91169464Srwatson * The pass argument is used when operating in the IPSTEALTH mode to tell
92169464Srwatson * what options to process: [LS]SRR (pass 0) or the others (pass 1).  The
93169464Srwatson * reason for as many as two passes is that when doing IPSTEALTH, non-routing
94169464Srwatson * options should be processed only if the packet is for us.
95169464Srwatson *
96169464Srwatson * Returns 1 if packet has been forwarded/freed, 0 if the packet should be
97169464Srwatson * processed further.
98152592Sandre */
99152592Sandreint
100152592Sandreip_dooptions(struct mbuf *m, int pass)
101152592Sandre{
102152592Sandre	struct ip *ip = mtod(m, struct ip *);
103152592Sandre	u_char *cp;
104152592Sandre	struct in_ifaddr *ia;
105152592Sandre	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
106152592Sandre	struct in_addr *sin, dst;
107188578Sluigi	uint32_t ntime;
108291993Smelifaro	struct nhop4_extended nh_ext;
109152592Sandre	struct	sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
110152592Sandre
111169464Srwatson	/* Ignore or reject packets with IP options. */
112271610Shrs	if (V_ip_doopts == 0)
113152592Sandre		return 0;
114271610Shrs	else if (V_ip_doopts == 2) {
115152592Sandre		type = ICMP_UNREACH;
116152592Sandre		code = ICMP_UNREACH_FILTER_PROHIB;
117152592Sandre		goto bad;
118152592Sandre	}
119152592Sandre
120152592Sandre	dst = ip->ip_dst;
121152592Sandre	cp = (u_char *)(ip + 1);
122152592Sandre	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
123152592Sandre	for (; cnt > 0; cnt -= optlen, cp += optlen) {
124152592Sandre		opt = cp[IPOPT_OPTVAL];
125152592Sandre		if (opt == IPOPT_EOL)
126152592Sandre			break;
127152592Sandre		if (opt == IPOPT_NOP)
128152592Sandre			optlen = 1;
129152592Sandre		else {
130152592Sandre			if (cnt < IPOPT_OLEN + sizeof(*cp)) {
131152592Sandre				code = &cp[IPOPT_OLEN] - (u_char *)ip;
132152592Sandre				goto bad;
133152592Sandre			}
134152592Sandre			optlen = cp[IPOPT_OLEN];
135152592Sandre			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
136152592Sandre				code = &cp[IPOPT_OLEN] - (u_char *)ip;
137152592Sandre				goto bad;
138152592Sandre			}
139152592Sandre		}
140152592Sandre		switch (opt) {
141152592Sandre
142152592Sandre		default:
143152592Sandre			break;
144152592Sandre
145152592Sandre		/*
146169464Srwatson		 * Source routing with record.  Find interface with current
147169464Srwatson		 * destination address.  If none on this machine then drop if
148169464Srwatson		 * strictly routed, or do nothing if loosely routed.  Record
149169464Srwatson		 * interface address and bring up next address component.  If
150169464Srwatson		 * strictly routed make sure next address is on directly
151169464Srwatson		 * accessible net.
152152592Sandre		 */
153152592Sandre		case IPOPT_LSRR:
154152592Sandre		case IPOPT_SSRR:
155152592Sandre#ifdef IPSTEALTH
156181803Sbz			if (V_ipstealth && pass > 0)
157152592Sandre				break;
158152592Sandre#endif
159152592Sandre			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
160152592Sandre				code = &cp[IPOPT_OLEN] - (u_char *)ip;
161152592Sandre				goto bad;
162152592Sandre			}
163152592Sandre			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
164152592Sandre				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
165152592Sandre				goto bad;
166152592Sandre			}
167152592Sandre			ipaddr.sin_addr = ip->ip_dst;
168194760Srwatson			if (ifa_ifwithaddr_check((struct sockaddr *)&ipaddr)
169194760Srwatson			    == 0) {
170152592Sandre				if (opt == IPOPT_SSRR) {
171152592Sandre					type = ICMP_UNREACH;
172152592Sandre					code = ICMP_UNREACH_SRCFAIL;
173152592Sandre					goto bad;
174152592Sandre				}
175271610Shrs				if (!V_ip_dosourceroute)
176152592Sandre					goto nosourcerouting;
177152592Sandre				/*
178152592Sandre				 * Loose routing, and not at next destination
179152592Sandre				 * yet; nothing to do except forward.
180152592Sandre				 */
181152592Sandre				break;
182152592Sandre			}
183152592Sandre			off--;			/* 0 origin */
184152592Sandre			if (off > optlen - (int)sizeof(struct in_addr)) {
185152592Sandre				/*
186152592Sandre				 * End of source route.  Should be for us.
187152592Sandre				 */
188271610Shrs				if (!V_ip_acceptsourceroute)
189152592Sandre					goto nosourcerouting;
190152592Sandre				save_rte(m, cp, ip->ip_src);
191152592Sandre				break;
192152592Sandre			}
193152592Sandre#ifdef IPSTEALTH
194181803Sbz			if (V_ipstealth)
195152592Sandre				goto dropit;
196152592Sandre#endif
197271610Shrs			if (!V_ip_dosourceroute) {
198181803Sbz				if (V_ipforwarding) {
199315456Svangyzen					char srcbuf[INET_ADDRSTRLEN];
200315456Svangyzen					char dstbuf[INET_ADDRSTRLEN];
201315456Svangyzen
202152592Sandre					/*
203169464Srwatson					 * Acting as a router, so generate
204169464Srwatson					 * ICMP
205152592Sandre					 */
206152592Sandrenosourcerouting:
207152592Sandre					log(LOG_WARNING,
208315456Svangyzen					    "attempted source route from %s "
209315456Svangyzen					    "to %s\n",
210315456Svangyzen					    inet_ntoa_r(ip->ip_src, srcbuf),
211315456Svangyzen					    inet_ntoa_r(ip->ip_dst, dstbuf));
212152592Sandre					type = ICMP_UNREACH;
213152592Sandre					code = ICMP_UNREACH_SRCFAIL;
214152592Sandre					goto bad;
215152592Sandre				} else {
216152592Sandre					/*
217169464Srwatson					 * Not acting as a router, so
218169464Srwatson					 * silently drop.
219152592Sandre					 */
220152592Sandre#ifdef IPSTEALTH
221152592Sandredropit:
222152592Sandre#endif
223190951Srwatson					IPSTAT_INC(ips_cantforward);
224152592Sandre					m_freem(m);
225152592Sandre					return (1);
226152592Sandre				}
227152592Sandre			}
228152592Sandre
229152592Sandre			/*
230152592Sandre			 * locate outgoing interface
231152592Sandre			 */
232152592Sandre			(void)memcpy(&ipaddr.sin_addr, cp + off,
233152592Sandre			    sizeof(ipaddr.sin_addr));
234152592Sandre
235291993Smelifaro			type = ICMP_UNREACH;
236291993Smelifaro			code = ICMP_UNREACH_SRCFAIL;
237291993Smelifaro
238152592Sandre			if (opt == IPOPT_SSRR) {
239152592Sandre#define	INA	struct in_ifaddr *
240152592Sandre#define	SA	struct sockaddr *
241271438Sasomers			    ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr,
242271438Sasomers					    RT_ALL_FIBS);
243271438Sasomers			    if (ia == NULL)
244271438Sasomers				    ia = (INA)ifa_ifwithnet((SA)&ipaddr, 0,
245271438Sasomers						    RT_ALL_FIBS);
246291993Smelifaro				if (ia == NULL)
247291993Smelifaro					goto bad;
248291993Smelifaro
249291993Smelifaro				memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
250291993Smelifaro				    sizeof(struct in_addr));
251291993Smelifaro				ifa_free(&ia->ia_ifa);
252291993Smelifaro			} else {
253291993Smelifaro				/* XXX MRT 0 for routing */
254291993Smelifaro				if (fib4_lookup_nh_ext(M_GETFIB(m),
255291993Smelifaro				    ipaddr.sin_addr, 0, 0, &nh_ext) != 0)
256291993Smelifaro					goto bad;
257291993Smelifaro
258291993Smelifaro				memcpy(cp + off, &nh_ext.nh_src,
259291993Smelifaro				    sizeof(struct in_addr));
260152592Sandre			}
261291993Smelifaro
262152592Sandre			ip->ip_dst = ipaddr.sin_addr;
263152592Sandre			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
264152592Sandre			/*
265152592Sandre			 * Let ip_intr's mcast routing check handle mcast pkts
266152592Sandre			 */
267152592Sandre			forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
268152592Sandre			break;
269152592Sandre
270152592Sandre		case IPOPT_RR:
271152592Sandre#ifdef IPSTEALTH
272181803Sbz			if (V_ipstealth && pass == 0)
273152592Sandre				break;
274152592Sandre#endif
275152592Sandre			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
276152592Sandre				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
277152592Sandre				goto bad;
278152592Sandre			}
279152592Sandre			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
280152592Sandre				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
281152592Sandre				goto bad;
282152592Sandre			}
283152592Sandre			/*
284152592Sandre			 * If no space remains, ignore.
285152592Sandre			 */
286152592Sandre			off--;			/* 0 origin */
287152592Sandre			if (off > optlen - (int)sizeof(struct in_addr))
288152592Sandre				break;
289152592Sandre			(void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
290152592Sandre			    sizeof(ipaddr.sin_addr));
291152592Sandre			/*
292169464Srwatson			 * Locate outgoing interface; if we're the
293169464Srwatson			 * destination, use the incoming interface (should be
294169464Srwatson			 * same).
295152592Sandre			 */
296292015Smelifaro			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) != NULL) {
297292015Smelifaro				memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
298292015Smelifaro				    sizeof(struct in_addr));
299292015Smelifaro				ifa_free(&ia->ia_ifa);
300292015Smelifaro			} else if (fib4_lookup_nh_ext(M_GETFIB(m),
301292015Smelifaro			    ipaddr.sin_addr, 0, 0, &nh_ext) == 0) {
302292015Smelifaro				memcpy(cp + off, &nh_ext.nh_src,
303292015Smelifaro				    sizeof(struct in_addr));
304292015Smelifaro			} else {
305152592Sandre				type = ICMP_UNREACH;
306152592Sandre				code = ICMP_UNREACH_HOST;
307152592Sandre				goto bad;
308152592Sandre			}
309152592Sandre			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
310152592Sandre			break;
311152592Sandre
312152592Sandre		case IPOPT_TS:
313152592Sandre#ifdef IPSTEALTH
314181803Sbz			if (V_ipstealth && pass == 0)
315152592Sandre				break;
316152592Sandre#endif
317152592Sandre			code = cp - (u_char *)ip;
318152592Sandre			if (optlen < 4 || optlen > 40) {
319152592Sandre				code = &cp[IPOPT_OLEN] - (u_char *)ip;
320152592Sandre				goto bad;
321152592Sandre			}
322152592Sandre			if ((off = cp[IPOPT_OFFSET]) < 5) {
323152592Sandre				code = &cp[IPOPT_OLEN] - (u_char *)ip;
324152592Sandre				goto bad;
325152592Sandre			}
326152592Sandre			if (off > optlen - (int)sizeof(int32_t)) {
327152592Sandre				cp[IPOPT_OFFSET + 1] += (1 << 4);
328152592Sandre				if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) {
329152592Sandre					code = &cp[IPOPT_OFFSET] - (u_char *)ip;
330152592Sandre					goto bad;
331152592Sandre				}
332152592Sandre				break;
333152592Sandre			}
334152592Sandre			off--;				/* 0 origin */
335152592Sandre			sin = (struct in_addr *)(cp + off);
336152592Sandre			switch (cp[IPOPT_OFFSET + 1] & 0x0f) {
337152592Sandre
338152592Sandre			case IPOPT_TS_TSONLY:
339152592Sandre				break;
340152592Sandre
341152592Sandre			case IPOPT_TS_TSANDADDR:
342188578Sluigi				if (off + sizeof(uint32_t) +
343152592Sandre				    sizeof(struct in_addr) > optlen) {
344152592Sandre					code = &cp[IPOPT_OFFSET] - (u_char *)ip;
345152592Sandre					goto bad;
346152592Sandre				}
347152592Sandre				ipaddr.sin_addr = dst;
348152592Sandre				ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
349152592Sandre							    m->m_pkthdr.rcvif);
350152592Sandre				if (ia == NULL)
351152592Sandre					continue;
352152592Sandre				(void)memcpy(sin, &IA_SIN(ia)->sin_addr,
353152592Sandre				    sizeof(struct in_addr));
354194760Srwatson				ifa_free(&ia->ia_ifa);
355152592Sandre				cp[IPOPT_OFFSET] += sizeof(struct in_addr);
356152592Sandre				off += sizeof(struct in_addr);
357152592Sandre				break;
358152592Sandre
359152592Sandre			case IPOPT_TS_PRESPEC:
360188578Sluigi				if (off + sizeof(uint32_t) +
361152592Sandre				    sizeof(struct in_addr) > optlen) {
362152592Sandre					code = &cp[IPOPT_OFFSET] - (u_char *)ip;
363152592Sandre					goto bad;
364152592Sandre				}
365152592Sandre				(void)memcpy(&ipaddr.sin_addr, sin,
366152592Sandre				    sizeof(struct in_addr));
367213832Sbz				if (ifa_ifwithaddr_check((SA)&ipaddr) == 0)
368152592Sandre					continue;
369152592Sandre				cp[IPOPT_OFFSET] += sizeof(struct in_addr);
370152592Sandre				off += sizeof(struct in_addr);
371152592Sandre				break;
372152592Sandre
373152592Sandre			default:
374152592Sandre				code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip;
375152592Sandre				goto bad;
376152592Sandre			}
377152592Sandre			ntime = iptime();
378188578Sluigi			(void)memcpy(cp + off, &ntime, sizeof(uint32_t));
379188578Sluigi			cp[IPOPT_OFFSET] += sizeof(uint32_t);
380152592Sandre		}
381152592Sandre	}
382181803Sbz	if (forward && V_ipforwarding) {
383152592Sandre		ip_forward(m, 1);
384152592Sandre		return (1);
385152592Sandre	}
386152592Sandre	return (0);
387152592Sandrebad:
388152592Sandre	icmp_error(m, type, code, 0, 0);
389190951Srwatson	IPSTAT_INC(ips_badoptions);
390152592Sandre	return (1);
391152592Sandre}
392152592Sandre
393152592Sandre/*
394169464Srwatson * Save incoming source route for use in replies, to be picked up later by
395169464Srwatson * ip_srcroute if the receiver is interested.
396152592Sandre */
397152592Sandrestatic void
398169454Srwatsonsave_rte(struct mbuf *m, u_char *option, struct in_addr dst)
399152592Sandre{
400152592Sandre	unsigned olen;
401152592Sandre	struct ipopt_tag *opts;
402152592Sandre
403152592Sandre	opts = (struct ipopt_tag *)m_tag_get(PACKET_TAG_IPOPTIONS,
404169464Srwatson	    sizeof(struct ipopt_tag), M_NOWAIT);
405152592Sandre	if (opts == NULL)
406152592Sandre		return;
407152592Sandre
408152592Sandre	olen = option[IPOPT_OLEN];
409152592Sandre	if (olen > sizeof(opts->ip_srcrt) - (1 + sizeof(dst))) {
410152592Sandre		m_tag_free((struct m_tag *)opts);
411152592Sandre		return;
412152592Sandre	}
413152592Sandre	bcopy(option, opts->ip_srcrt.srcopt, olen);
414152592Sandre	opts->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
415152592Sandre	opts->ip_srcrt.dst = dst;
416152592Sandre	m_tag_prepend(m, (struct m_tag *)opts);
417152592Sandre}
418152592Sandre
419152592Sandre/*
420169464Srwatson * Retrieve incoming source route for use in replies, in the same form used
421169464Srwatson * by setsockopt.  The first hop is placed before the options, will be
422169464Srwatson * removed later.
423152592Sandre */
424152592Sandrestruct mbuf *
425169454Srwatsonip_srcroute(struct mbuf *m0)
426152592Sandre{
427169464Srwatson	struct in_addr *p, *q;
428169464Srwatson	struct mbuf *m;
429152592Sandre	struct ipopt_tag *opts;
430152592Sandre
431152592Sandre	opts = (struct ipopt_tag *)m_tag_find(m0, PACKET_TAG_IPOPTIONS, NULL);
432152592Sandre	if (opts == NULL)
433152592Sandre		return (NULL);
434152592Sandre
435152592Sandre	if (opts->ip_nhops == 0)
436152592Sandre		return (NULL);
437243882Sglebius	m = m_get(M_NOWAIT, MT_DATA);
438152592Sandre	if (m == NULL)
439152592Sandre		return (NULL);
440152592Sandre
441152592Sandre#define OPTSIZ	(sizeof(opts->ip_srcrt.nop) + sizeof(opts->ip_srcrt.srcopt))
442152592Sandre
443152592Sandre	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
444152592Sandre	m->m_len = opts->ip_nhops * sizeof(struct in_addr) +
445152592Sandre	    sizeof(struct in_addr) + OPTSIZ;
446152592Sandre
447152592Sandre	/*
448169464Srwatson	 * First, save first hop for return route.
449152592Sandre	 */
450152592Sandre	p = &(opts->ip_srcrt.route[opts->ip_nhops - 1]);
451152592Sandre	*(mtod(m, struct in_addr *)) = *p--;
452152592Sandre
453152592Sandre	/*
454152592Sandre	 * Copy option fields and padding (nop) to mbuf.
455152592Sandre	 */
456152592Sandre	opts->ip_srcrt.nop = IPOPT_NOP;
457152592Sandre	opts->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
458152592Sandre	(void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
459152592Sandre	    &(opts->ip_srcrt.nop), OPTSIZ);
460152592Sandre	q = (struct in_addr *)(mtod(m, caddr_t) +
461152592Sandre	    sizeof(struct in_addr) + OPTSIZ);
462152592Sandre#undef OPTSIZ
463152592Sandre	/*
464169464Srwatson	 * Record return path as an IP source route, reversing the path
465169464Srwatson	 * (pointers are now aligned).
466152592Sandre	 */
467152592Sandre	while (p >= opts->ip_srcrt.route) {
468152592Sandre		*q++ = *p--;
469152592Sandre	}
470152592Sandre	/*
471152592Sandre	 * Last hop goes to final destination.
472152592Sandre	 */
473152592Sandre	*q = opts->ip_srcrt.dst;
474152592Sandre	m_tag_delete(m0, (struct m_tag *)opts);
475152592Sandre	return (m);
476152592Sandre}
477152592Sandre
478152592Sandre/*
479241480Sglebius * Strip out IP options, at higher level protocol in the kernel.
480152592Sandre */
481152592Sandrevoid
482241480Sglebiusip_stripoptions(struct mbuf *m)
483152592Sandre{
484152592Sandre	struct ip *ip = mtod(m, struct ip *);
485152592Sandre	int olen;
486152592Sandre
487241925Sglebius	olen = (ip->ip_hl << 2) - sizeof(struct ip);
488152592Sandre	m->m_len -= olen;
489152592Sandre	if (m->m_flags & M_PKTHDR)
490152592Sandre		m->m_pkthdr.len -= olen;
491241923Sglebius	ip->ip_len = htons(ntohs(ip->ip_len) - olen);
492152592Sandre	ip->ip_hl = sizeof(struct ip) >> 2;
493241925Sglebius
494241925Sglebius	bcopy((char *)ip + sizeof(struct ip) + olen, (ip + 1),
495241925Sglebius	    (size_t )(m->m_len - sizeof(struct ip)));
496152592Sandre}
497152592Sandre
498152592Sandre/*
499169464Srwatson * Insert IP options into preformed packet.  Adjust IP destination as
500169464Srwatson * required for IP source routing, as indicated by a non-zero in_addr at the
501169464Srwatson * start of the options.
502152592Sandre *
503152592Sandre * XXX This routine assumes that the packet has no options in place.
504152592Sandre */
505152592Sandrestruct mbuf *
506169454Srwatsonip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
507152592Sandre{
508169464Srwatson	struct ipoption *p = mtod(opt, struct ipoption *);
509152592Sandre	struct mbuf *n;
510169464Srwatson	struct ip *ip = mtod(m, struct ip *);
511152592Sandre	unsigned optlen;
512152592Sandre
513152592Sandre	optlen = opt->m_len - sizeof(p->ipopt_dst);
514241913Sglebius	if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET) {
515152592Sandre		*phlen = 0;
516152592Sandre		return (m);		/* XXX should fail */
517152592Sandre	}
518152592Sandre	if (p->ipopt_dst.s_addr)
519152592Sandre		ip->ip_dst = p->ipopt_dst;
520276752Srwatson	if (!M_WRITABLE(m) || M_LEADINGSPACE(m) < optlen) {
521248324Sglebius		n = m_gethdr(M_NOWAIT, MT_DATA);
522152592Sandre		if (n == NULL) {
523152592Sandre			*phlen = 0;
524152592Sandre			return (m);
525152592Sandre		}
526248373Sglebius		m_move_pkthdr(n, m);
527152592Sandre		n->m_pkthdr.rcvif = NULL;
528152592Sandre		n->m_pkthdr.len += optlen;
529152592Sandre		m->m_len -= sizeof(struct ip);
530152592Sandre		m->m_data += sizeof(struct ip);
531152592Sandre		n->m_next = m;
532152592Sandre		m = n;
533152592Sandre		m->m_len = optlen + sizeof(struct ip);
534152592Sandre		m->m_data += max_linkhdr;
535152592Sandre		bcopy(ip, mtod(m, void *), sizeof(struct ip));
536152592Sandre	} else {
537152592Sandre		m->m_data -= optlen;
538152592Sandre		m->m_len += optlen;
539152592Sandre		m->m_pkthdr.len += optlen;
540152592Sandre		bcopy(ip, mtod(m, void *), sizeof(struct ip));
541152592Sandre	}
542152592Sandre	ip = mtod(m, struct ip *);
543152592Sandre	bcopy(p->ipopt_list, ip + 1, optlen);
544152592Sandre	*phlen = sizeof(struct ip) + optlen;
545152592Sandre	ip->ip_v = IPVERSION;
546152592Sandre	ip->ip_hl = *phlen >> 2;
547241913Sglebius	ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
548152592Sandre	return (m);
549152592Sandre}
550152592Sandre
551152592Sandre/*
552169464Srwatson * Copy options from ip to jp, omitting those not copied during
553169464Srwatson * fragmentation.
554152592Sandre */
555152592Sandreint
556169454Srwatsonip_optcopy(struct ip *ip, struct ip *jp)
557152592Sandre{
558169464Srwatson	u_char *cp, *dp;
559152592Sandre	int opt, optlen, cnt;
560152592Sandre
561152592Sandre	cp = (u_char *)(ip + 1);
562152592Sandre	dp = (u_char *)(jp + 1);
563152592Sandre	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
564152592Sandre	for (; cnt > 0; cnt -= optlen, cp += optlen) {
565152592Sandre		opt = cp[0];
566152592Sandre		if (opt == IPOPT_EOL)
567152592Sandre			break;
568152592Sandre		if (opt == IPOPT_NOP) {
569152592Sandre			/* Preserve for IP mcast tunnel's LSRR alignment. */
570152592Sandre			*dp++ = IPOPT_NOP;
571152592Sandre			optlen = 1;
572152592Sandre			continue;
573152592Sandre		}
574152592Sandre
575152592Sandre		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
576152592Sandre		    ("ip_optcopy: malformed ipv4 option"));
577152592Sandre		optlen = cp[IPOPT_OLEN];
578152592Sandre		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
579152592Sandre		    ("ip_optcopy: malformed ipv4 option"));
580152592Sandre
581169464Srwatson		/* Bogus lengths should have been caught by ip_dooptions. */
582152592Sandre		if (optlen > cnt)
583152592Sandre			optlen = cnt;
584152592Sandre		if (IPOPT_COPIED(opt)) {
585152592Sandre			bcopy(cp, dp, optlen);
586152592Sandre			dp += optlen;
587152592Sandre		}
588152592Sandre	}
589152592Sandre	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
590152592Sandre		*dp++ = IPOPT_EOL;
591152592Sandre	return (optlen);
592152592Sandre}
593152592Sandre
594152592Sandre/*
595169464Srwatson * Set up IP options in pcb for insertion in output packets.  Store in mbuf
596169464Srwatson * with pointer in pcbopt, adding pseudo-option with destination address if
597169464Srwatson * source routed.
598152592Sandre */
599152592Sandreint
600152592Sandreip_pcbopts(struct inpcb *inp, int optname, struct mbuf *m)
601152592Sandre{
602169464Srwatson	int cnt, optlen;
603169464Srwatson	u_char *cp;
604152592Sandre	struct mbuf **pcbopt;
605152592Sandre	u_char opt;
606152592Sandre
607178285Srwatson	INP_WLOCK_ASSERT(inp);
608152592Sandre
609152592Sandre	pcbopt = &inp->inp_options;
610152592Sandre
611152592Sandre	/* turn off any old options */
612152592Sandre	if (*pcbopt)
613152592Sandre		(void)m_free(*pcbopt);
614298066Spfg	*pcbopt = NULL;
615152592Sandre	if (m == NULL || m->m_len == 0) {
616152592Sandre		/*
617152592Sandre		 * Only turning off any previous options.
618152592Sandre		 */
619152592Sandre		if (m != NULL)
620152592Sandre			(void)m_free(m);
621152592Sandre		return (0);
622152592Sandre	}
623152592Sandre
624152592Sandre	if (m->m_len % sizeof(int32_t))
625152592Sandre		goto bad;
626152592Sandre	/*
627169464Srwatson	 * IP first-hop destination address will be stored before actual
628169464Srwatson	 * options; move other options back and clear it when none present.
629152592Sandre	 */
630152592Sandre	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
631152592Sandre		goto bad;
632152592Sandre	cnt = m->m_len;
633152592Sandre	m->m_len += sizeof(struct in_addr);
634152592Sandre	cp = mtod(m, u_char *) + sizeof(struct in_addr);
635152592Sandre	bcopy(mtod(m, void *), cp, (unsigned)cnt);
636152592Sandre	bzero(mtod(m, void *), sizeof(struct in_addr));
637152592Sandre
638152592Sandre	for (; cnt > 0; cnt -= optlen, cp += optlen) {
639152592Sandre		opt = cp[IPOPT_OPTVAL];
640152592Sandre		if (opt == IPOPT_EOL)
641152592Sandre			break;
642152592Sandre		if (opt == IPOPT_NOP)
643152592Sandre			optlen = 1;
644152592Sandre		else {
645152592Sandre			if (cnt < IPOPT_OLEN + sizeof(*cp))
646152592Sandre				goto bad;
647152592Sandre			optlen = cp[IPOPT_OLEN];
648152592Sandre			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
649152592Sandre				goto bad;
650152592Sandre		}
651152592Sandre		switch (opt) {
652152592Sandre
653152592Sandre		default:
654152592Sandre			break;
655152592Sandre
656152592Sandre		case IPOPT_LSRR:
657152592Sandre		case IPOPT_SSRR:
658152592Sandre			/*
659169464Srwatson			 * User process specifies route as:
660169464Srwatson			 *
661152592Sandre			 *	->A->B->C->D
662169464Srwatson			 *
663152592Sandre			 * D must be our final destination (but we can't
664152592Sandre			 * check that since we may not have connected yet).
665169464Srwatson			 * A is first hop destination, which doesn't appear
666169464Srwatson			 * in actual IP option, but is stored before the
667169464Srwatson			 * options.
668152592Sandre			 */
669175630Sbz			/* XXX-BZ PRIV_NETINET_SETHDROPTS? */
670152592Sandre			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
671152592Sandre				goto bad;
672152592Sandre			m->m_len -= sizeof(struct in_addr);
673152592Sandre			cnt -= sizeof(struct in_addr);
674152592Sandre			optlen -= sizeof(struct in_addr);
675152592Sandre			cp[IPOPT_OLEN] = optlen;
676152592Sandre			/*
677152592Sandre			 * Move first hop before start of options.
678152592Sandre			 */
679152592Sandre			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
680152592Sandre			    sizeof(struct in_addr));
681152592Sandre			/*
682152592Sandre			 * Then copy rest of options back
683152592Sandre			 * to close up the deleted entry.
684152592Sandre			 */
685152592Sandre			bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
686152592Sandre			    &cp[IPOPT_OFFSET+1],
687152592Sandre			    (unsigned)cnt - (IPOPT_MINOFF - 1));
688152592Sandre			break;
689152592Sandre		}
690152592Sandre	}
691152592Sandre	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
692152592Sandre		goto bad;
693152592Sandre	*pcbopt = m;
694152592Sandre	return (0);
695152592Sandre
696152592Sandrebad:
697152592Sandre	(void)m_free(m);
698152592Sandre	return (EINVAL);
699152592Sandre}
700189343Sbms
701189343Sbms/*
702189343Sbms * Check for the presence of the IP Router Alert option [RFC2113]
703189343Sbms * in the header of an IPv4 datagram.
704189343Sbms *
705189343Sbms * This call is not intended for use from the forwarding path; it is here
706189343Sbms * so that protocol domains may check for the presence of the option.
707189343Sbms * Given how FreeBSD's IPv4 stack is currently structured, the Router Alert
708189343Sbms * option does not have much relevance to the implementation, though this
709189343Sbms * may change in future.
710189343Sbms * Router alert options SHOULD be passed if running in IPSTEALTH mode and
711189343Sbms * we are not the endpoint.
712298995Spfg * Length checks on individual options should already have been performed
713189343Sbms * by ip_dooptions() therefore they are folded under INVARIANTS here.
714189343Sbms *
715189343Sbms * Return zero if not present or options are invalid, non-zero if present.
716189343Sbms */
717189343Sbmsint
718189343Sbmsip_checkrouteralert(struct mbuf *m)
719189343Sbms{
720189343Sbms	struct ip *ip = mtod(m, struct ip *);
721189343Sbms	u_char *cp;
722189343Sbms	int opt, optlen, cnt, found_ra;
723189343Sbms
724189343Sbms	found_ra = 0;
725189343Sbms	cp = (u_char *)(ip + 1);
726189343Sbms	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
727189343Sbms	for (; cnt > 0; cnt -= optlen, cp += optlen) {
728189343Sbms		opt = cp[IPOPT_OPTVAL];
729189343Sbms		if (opt == IPOPT_EOL)
730189343Sbms			break;
731189343Sbms		if (opt == IPOPT_NOP)
732189343Sbms			optlen = 1;
733189343Sbms		else {
734189343Sbms#ifdef INVARIANTS
735189343Sbms			if (cnt < IPOPT_OLEN + sizeof(*cp))
736189343Sbms				break;
737189343Sbms#endif
738189343Sbms			optlen = cp[IPOPT_OLEN];
739189343Sbms#ifdef INVARIANTS
740189343Sbms			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
741189343Sbms				break;
742189343Sbms#endif
743189343Sbms		}
744189343Sbms		switch (opt) {
745189343Sbms		case IPOPT_RA:
746189343Sbms#ifdef INVARIANTS
747189343Sbms			if (optlen != IPOPT_OFFSET + sizeof(uint16_t) ||
748189343Sbms			    (*((uint16_t *)&cp[IPOPT_OFFSET]) != 0))
749189343Sbms			    break;
750189343Sbms			else
751189343Sbms#endif
752189343Sbms			found_ra = 1;
753189343Sbms			break;
754189343Sbms		default:
755189343Sbms			break;
756189343Sbms		}
757189343Sbms	}
758189343Sbms
759189343Sbms	return (found_ra);
760189343Sbms}
761