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: releng/11.0/sys/netinet/ip_options.c 298995 2016-05-03 18:05:43Z pfg $");
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) {
199152592Sandre					char buf[16]; /* aaa.bbb.ccc.ddd\0 */
200152592Sandre					/*
201169464Srwatson					 * Acting as a router, so generate
202169464Srwatson					 * ICMP
203152592Sandre					 */
204152592Sandrenosourcerouting:
205152592Sandre					strcpy(buf, inet_ntoa(ip->ip_dst));
206152592Sandre					log(LOG_WARNING,
207152592Sandre					    "attempted source route from %s to %s\n",
208152592Sandre					    inet_ntoa(ip->ip_src), buf);
209152592Sandre					type = ICMP_UNREACH;
210152592Sandre					code = ICMP_UNREACH_SRCFAIL;
211152592Sandre					goto bad;
212152592Sandre				} else {
213152592Sandre					/*
214169464Srwatson					 * Not acting as a router, so
215169464Srwatson					 * silently drop.
216152592Sandre					 */
217152592Sandre#ifdef IPSTEALTH
218152592Sandredropit:
219152592Sandre#endif
220190951Srwatson					IPSTAT_INC(ips_cantforward);
221152592Sandre					m_freem(m);
222152592Sandre					return (1);
223152592Sandre				}
224152592Sandre			}
225152592Sandre
226152592Sandre			/*
227152592Sandre			 * locate outgoing interface
228152592Sandre			 */
229152592Sandre			(void)memcpy(&ipaddr.sin_addr, cp + off,
230152592Sandre			    sizeof(ipaddr.sin_addr));
231152592Sandre
232291993Smelifaro			type = ICMP_UNREACH;
233291993Smelifaro			code = ICMP_UNREACH_SRCFAIL;
234291993Smelifaro
235152592Sandre			if (opt == IPOPT_SSRR) {
236152592Sandre#define	INA	struct in_ifaddr *
237152592Sandre#define	SA	struct sockaddr *
238271438Sasomers			    ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr,
239271438Sasomers					    RT_ALL_FIBS);
240271438Sasomers			    if (ia == NULL)
241271438Sasomers				    ia = (INA)ifa_ifwithnet((SA)&ipaddr, 0,
242271438Sasomers						    RT_ALL_FIBS);
243291993Smelifaro				if (ia == NULL)
244291993Smelifaro					goto bad;
245291993Smelifaro
246291993Smelifaro				memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
247291993Smelifaro				    sizeof(struct in_addr));
248291993Smelifaro				ifa_free(&ia->ia_ifa);
249291993Smelifaro			} else {
250291993Smelifaro				/* XXX MRT 0 for routing */
251291993Smelifaro				if (fib4_lookup_nh_ext(M_GETFIB(m),
252291993Smelifaro				    ipaddr.sin_addr, 0, 0, &nh_ext) != 0)
253291993Smelifaro					goto bad;
254291993Smelifaro
255291993Smelifaro				memcpy(cp + off, &nh_ext.nh_src,
256291993Smelifaro				    sizeof(struct in_addr));
257152592Sandre			}
258291993Smelifaro
259152592Sandre			ip->ip_dst = ipaddr.sin_addr;
260152592Sandre			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
261152592Sandre			/*
262152592Sandre			 * Let ip_intr's mcast routing check handle mcast pkts
263152592Sandre			 */
264152592Sandre			forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
265152592Sandre			break;
266152592Sandre
267152592Sandre		case IPOPT_RR:
268152592Sandre#ifdef IPSTEALTH
269181803Sbz			if (V_ipstealth && pass == 0)
270152592Sandre				break;
271152592Sandre#endif
272152592Sandre			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
273152592Sandre				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
274152592Sandre				goto bad;
275152592Sandre			}
276152592Sandre			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
277152592Sandre				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
278152592Sandre				goto bad;
279152592Sandre			}
280152592Sandre			/*
281152592Sandre			 * If no space remains, ignore.
282152592Sandre			 */
283152592Sandre			off--;			/* 0 origin */
284152592Sandre			if (off > optlen - (int)sizeof(struct in_addr))
285152592Sandre				break;
286152592Sandre			(void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
287152592Sandre			    sizeof(ipaddr.sin_addr));
288152592Sandre			/*
289169464Srwatson			 * Locate outgoing interface; if we're the
290169464Srwatson			 * destination, use the incoming interface (should be
291169464Srwatson			 * same).
292152592Sandre			 */
293292015Smelifaro			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) != NULL) {
294292015Smelifaro				memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
295292015Smelifaro				    sizeof(struct in_addr));
296292015Smelifaro				ifa_free(&ia->ia_ifa);
297292015Smelifaro			} else if (fib4_lookup_nh_ext(M_GETFIB(m),
298292015Smelifaro			    ipaddr.sin_addr, 0, 0, &nh_ext) == 0) {
299292015Smelifaro				memcpy(cp + off, &nh_ext.nh_src,
300292015Smelifaro				    sizeof(struct in_addr));
301292015Smelifaro			} else {
302152592Sandre				type = ICMP_UNREACH;
303152592Sandre				code = ICMP_UNREACH_HOST;
304152592Sandre				goto bad;
305152592Sandre			}
306152592Sandre			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
307152592Sandre			break;
308152592Sandre
309152592Sandre		case IPOPT_TS:
310152592Sandre#ifdef IPSTEALTH
311181803Sbz			if (V_ipstealth && pass == 0)
312152592Sandre				break;
313152592Sandre#endif
314152592Sandre			code = cp - (u_char *)ip;
315152592Sandre			if (optlen < 4 || optlen > 40) {
316152592Sandre				code = &cp[IPOPT_OLEN] - (u_char *)ip;
317152592Sandre				goto bad;
318152592Sandre			}
319152592Sandre			if ((off = cp[IPOPT_OFFSET]) < 5) {
320152592Sandre				code = &cp[IPOPT_OLEN] - (u_char *)ip;
321152592Sandre				goto bad;
322152592Sandre			}
323152592Sandre			if (off > optlen - (int)sizeof(int32_t)) {
324152592Sandre				cp[IPOPT_OFFSET + 1] += (1 << 4);
325152592Sandre				if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) {
326152592Sandre					code = &cp[IPOPT_OFFSET] - (u_char *)ip;
327152592Sandre					goto bad;
328152592Sandre				}
329152592Sandre				break;
330152592Sandre			}
331152592Sandre			off--;				/* 0 origin */
332152592Sandre			sin = (struct in_addr *)(cp + off);
333152592Sandre			switch (cp[IPOPT_OFFSET + 1] & 0x0f) {
334152592Sandre
335152592Sandre			case IPOPT_TS_TSONLY:
336152592Sandre				break;
337152592Sandre
338152592Sandre			case IPOPT_TS_TSANDADDR:
339188578Sluigi				if (off + sizeof(uint32_t) +
340152592Sandre				    sizeof(struct in_addr) > optlen) {
341152592Sandre					code = &cp[IPOPT_OFFSET] - (u_char *)ip;
342152592Sandre					goto bad;
343152592Sandre				}
344152592Sandre				ipaddr.sin_addr = dst;
345152592Sandre				ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
346152592Sandre							    m->m_pkthdr.rcvif);
347152592Sandre				if (ia == NULL)
348152592Sandre					continue;
349152592Sandre				(void)memcpy(sin, &IA_SIN(ia)->sin_addr,
350152592Sandre				    sizeof(struct in_addr));
351194760Srwatson				ifa_free(&ia->ia_ifa);
352152592Sandre				cp[IPOPT_OFFSET] += sizeof(struct in_addr);
353152592Sandre				off += sizeof(struct in_addr);
354152592Sandre				break;
355152592Sandre
356152592Sandre			case IPOPT_TS_PRESPEC:
357188578Sluigi				if (off + sizeof(uint32_t) +
358152592Sandre				    sizeof(struct in_addr) > optlen) {
359152592Sandre					code = &cp[IPOPT_OFFSET] - (u_char *)ip;
360152592Sandre					goto bad;
361152592Sandre				}
362152592Sandre				(void)memcpy(&ipaddr.sin_addr, sin,
363152592Sandre				    sizeof(struct in_addr));
364213832Sbz				if (ifa_ifwithaddr_check((SA)&ipaddr) == 0)
365152592Sandre					continue;
366152592Sandre				cp[IPOPT_OFFSET] += sizeof(struct in_addr);
367152592Sandre				off += sizeof(struct in_addr);
368152592Sandre				break;
369152592Sandre
370152592Sandre			default:
371152592Sandre				code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip;
372152592Sandre				goto bad;
373152592Sandre			}
374152592Sandre			ntime = iptime();
375188578Sluigi			(void)memcpy(cp + off, &ntime, sizeof(uint32_t));
376188578Sluigi			cp[IPOPT_OFFSET] += sizeof(uint32_t);
377152592Sandre		}
378152592Sandre	}
379181803Sbz	if (forward && V_ipforwarding) {
380152592Sandre		ip_forward(m, 1);
381152592Sandre		return (1);
382152592Sandre	}
383152592Sandre	return (0);
384152592Sandrebad:
385152592Sandre	icmp_error(m, type, code, 0, 0);
386190951Srwatson	IPSTAT_INC(ips_badoptions);
387152592Sandre	return (1);
388152592Sandre}
389152592Sandre
390152592Sandre/*
391169464Srwatson * Save incoming source route for use in replies, to be picked up later by
392169464Srwatson * ip_srcroute if the receiver is interested.
393152592Sandre */
394152592Sandrestatic void
395169454Srwatsonsave_rte(struct mbuf *m, u_char *option, struct in_addr dst)
396152592Sandre{
397152592Sandre	unsigned olen;
398152592Sandre	struct ipopt_tag *opts;
399152592Sandre
400152592Sandre	opts = (struct ipopt_tag *)m_tag_get(PACKET_TAG_IPOPTIONS,
401169464Srwatson	    sizeof(struct ipopt_tag), M_NOWAIT);
402152592Sandre	if (opts == NULL)
403152592Sandre		return;
404152592Sandre
405152592Sandre	olen = option[IPOPT_OLEN];
406152592Sandre	if (olen > sizeof(opts->ip_srcrt) - (1 + sizeof(dst))) {
407152592Sandre		m_tag_free((struct m_tag *)opts);
408152592Sandre		return;
409152592Sandre	}
410152592Sandre	bcopy(option, opts->ip_srcrt.srcopt, olen);
411152592Sandre	opts->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
412152592Sandre	opts->ip_srcrt.dst = dst;
413152592Sandre	m_tag_prepend(m, (struct m_tag *)opts);
414152592Sandre}
415152592Sandre
416152592Sandre/*
417169464Srwatson * Retrieve incoming source route for use in replies, in the same form used
418169464Srwatson * by setsockopt.  The first hop is placed before the options, will be
419169464Srwatson * removed later.
420152592Sandre */
421152592Sandrestruct mbuf *
422169454Srwatsonip_srcroute(struct mbuf *m0)
423152592Sandre{
424169464Srwatson	struct in_addr *p, *q;
425169464Srwatson	struct mbuf *m;
426152592Sandre	struct ipopt_tag *opts;
427152592Sandre
428152592Sandre	opts = (struct ipopt_tag *)m_tag_find(m0, PACKET_TAG_IPOPTIONS, NULL);
429152592Sandre	if (opts == NULL)
430152592Sandre		return (NULL);
431152592Sandre
432152592Sandre	if (opts->ip_nhops == 0)
433152592Sandre		return (NULL);
434243882Sglebius	m = m_get(M_NOWAIT, MT_DATA);
435152592Sandre	if (m == NULL)
436152592Sandre		return (NULL);
437152592Sandre
438152592Sandre#define OPTSIZ	(sizeof(opts->ip_srcrt.nop) + sizeof(opts->ip_srcrt.srcopt))
439152592Sandre
440152592Sandre	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
441152592Sandre	m->m_len = opts->ip_nhops * sizeof(struct in_addr) +
442152592Sandre	    sizeof(struct in_addr) + OPTSIZ;
443152592Sandre
444152592Sandre	/*
445169464Srwatson	 * First, save first hop for return route.
446152592Sandre	 */
447152592Sandre	p = &(opts->ip_srcrt.route[opts->ip_nhops - 1]);
448152592Sandre	*(mtod(m, struct in_addr *)) = *p--;
449152592Sandre
450152592Sandre	/*
451152592Sandre	 * Copy option fields and padding (nop) to mbuf.
452152592Sandre	 */
453152592Sandre	opts->ip_srcrt.nop = IPOPT_NOP;
454152592Sandre	opts->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
455152592Sandre	(void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
456152592Sandre	    &(opts->ip_srcrt.nop), OPTSIZ);
457152592Sandre	q = (struct in_addr *)(mtod(m, caddr_t) +
458152592Sandre	    sizeof(struct in_addr) + OPTSIZ);
459152592Sandre#undef OPTSIZ
460152592Sandre	/*
461169464Srwatson	 * Record return path as an IP source route, reversing the path
462169464Srwatson	 * (pointers are now aligned).
463152592Sandre	 */
464152592Sandre	while (p >= opts->ip_srcrt.route) {
465152592Sandre		*q++ = *p--;
466152592Sandre	}
467152592Sandre	/*
468152592Sandre	 * Last hop goes to final destination.
469152592Sandre	 */
470152592Sandre	*q = opts->ip_srcrt.dst;
471152592Sandre	m_tag_delete(m0, (struct m_tag *)opts);
472152592Sandre	return (m);
473152592Sandre}
474152592Sandre
475152592Sandre/*
476241480Sglebius * Strip out IP options, at higher level protocol in the kernel.
477152592Sandre */
478152592Sandrevoid
479241480Sglebiusip_stripoptions(struct mbuf *m)
480152592Sandre{
481152592Sandre	struct ip *ip = mtod(m, struct ip *);
482152592Sandre	int olen;
483152592Sandre
484241925Sglebius	olen = (ip->ip_hl << 2) - sizeof(struct ip);
485152592Sandre	m->m_len -= olen;
486152592Sandre	if (m->m_flags & M_PKTHDR)
487152592Sandre		m->m_pkthdr.len -= olen;
488241923Sglebius	ip->ip_len = htons(ntohs(ip->ip_len) - olen);
489152592Sandre	ip->ip_hl = sizeof(struct ip) >> 2;
490241925Sglebius
491241925Sglebius	bcopy((char *)ip + sizeof(struct ip) + olen, (ip + 1),
492241925Sglebius	    (size_t )(m->m_len - sizeof(struct ip)));
493152592Sandre}
494152592Sandre
495152592Sandre/*
496169464Srwatson * Insert IP options into preformed packet.  Adjust IP destination as
497169464Srwatson * required for IP source routing, as indicated by a non-zero in_addr at the
498169464Srwatson * start of the options.
499152592Sandre *
500152592Sandre * XXX This routine assumes that the packet has no options in place.
501152592Sandre */
502152592Sandrestruct mbuf *
503169454Srwatsonip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
504152592Sandre{
505169464Srwatson	struct ipoption *p = mtod(opt, struct ipoption *);
506152592Sandre	struct mbuf *n;
507169464Srwatson	struct ip *ip = mtod(m, struct ip *);
508152592Sandre	unsigned optlen;
509152592Sandre
510152592Sandre	optlen = opt->m_len - sizeof(p->ipopt_dst);
511241913Sglebius	if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET) {
512152592Sandre		*phlen = 0;
513152592Sandre		return (m);		/* XXX should fail */
514152592Sandre	}
515152592Sandre	if (p->ipopt_dst.s_addr)
516152592Sandre		ip->ip_dst = p->ipopt_dst;
517276752Srwatson	if (!M_WRITABLE(m) || M_LEADINGSPACE(m) < optlen) {
518248324Sglebius		n = m_gethdr(M_NOWAIT, MT_DATA);
519152592Sandre		if (n == NULL) {
520152592Sandre			*phlen = 0;
521152592Sandre			return (m);
522152592Sandre		}
523248373Sglebius		m_move_pkthdr(n, m);
524152592Sandre		n->m_pkthdr.rcvif = NULL;
525152592Sandre		n->m_pkthdr.len += optlen;
526152592Sandre		m->m_len -= sizeof(struct ip);
527152592Sandre		m->m_data += sizeof(struct ip);
528152592Sandre		n->m_next = m;
529152592Sandre		m = n;
530152592Sandre		m->m_len = optlen + sizeof(struct ip);
531152592Sandre		m->m_data += max_linkhdr;
532152592Sandre		bcopy(ip, mtod(m, void *), sizeof(struct ip));
533152592Sandre	} else {
534152592Sandre		m->m_data -= optlen;
535152592Sandre		m->m_len += optlen;
536152592Sandre		m->m_pkthdr.len += optlen;
537152592Sandre		bcopy(ip, mtod(m, void *), sizeof(struct ip));
538152592Sandre	}
539152592Sandre	ip = mtod(m, struct ip *);
540152592Sandre	bcopy(p->ipopt_list, ip + 1, optlen);
541152592Sandre	*phlen = sizeof(struct ip) + optlen;
542152592Sandre	ip->ip_v = IPVERSION;
543152592Sandre	ip->ip_hl = *phlen >> 2;
544241913Sglebius	ip->ip_len = htons(ntohs(ip->ip_len) + optlen);
545152592Sandre	return (m);
546152592Sandre}
547152592Sandre
548152592Sandre/*
549169464Srwatson * Copy options from ip to jp, omitting those not copied during
550169464Srwatson * fragmentation.
551152592Sandre */
552152592Sandreint
553169454Srwatsonip_optcopy(struct ip *ip, struct ip *jp)
554152592Sandre{
555169464Srwatson	u_char *cp, *dp;
556152592Sandre	int opt, optlen, cnt;
557152592Sandre
558152592Sandre	cp = (u_char *)(ip + 1);
559152592Sandre	dp = (u_char *)(jp + 1);
560152592Sandre	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
561152592Sandre	for (; cnt > 0; cnt -= optlen, cp += optlen) {
562152592Sandre		opt = cp[0];
563152592Sandre		if (opt == IPOPT_EOL)
564152592Sandre			break;
565152592Sandre		if (opt == IPOPT_NOP) {
566152592Sandre			/* Preserve for IP mcast tunnel's LSRR alignment. */
567152592Sandre			*dp++ = IPOPT_NOP;
568152592Sandre			optlen = 1;
569152592Sandre			continue;
570152592Sandre		}
571152592Sandre
572152592Sandre		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
573152592Sandre		    ("ip_optcopy: malformed ipv4 option"));
574152592Sandre		optlen = cp[IPOPT_OLEN];
575152592Sandre		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
576152592Sandre		    ("ip_optcopy: malformed ipv4 option"));
577152592Sandre
578169464Srwatson		/* Bogus lengths should have been caught by ip_dooptions. */
579152592Sandre		if (optlen > cnt)
580152592Sandre			optlen = cnt;
581152592Sandre		if (IPOPT_COPIED(opt)) {
582152592Sandre			bcopy(cp, dp, optlen);
583152592Sandre			dp += optlen;
584152592Sandre		}
585152592Sandre	}
586152592Sandre	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
587152592Sandre		*dp++ = IPOPT_EOL;
588152592Sandre	return (optlen);
589152592Sandre}
590152592Sandre
591152592Sandre/*
592169464Srwatson * Set up IP options in pcb for insertion in output packets.  Store in mbuf
593169464Srwatson * with pointer in pcbopt, adding pseudo-option with destination address if
594169464Srwatson * source routed.
595152592Sandre */
596152592Sandreint
597152592Sandreip_pcbopts(struct inpcb *inp, int optname, struct mbuf *m)
598152592Sandre{
599169464Srwatson	int cnt, optlen;
600169464Srwatson	u_char *cp;
601152592Sandre	struct mbuf **pcbopt;
602152592Sandre	u_char opt;
603152592Sandre
604178285Srwatson	INP_WLOCK_ASSERT(inp);
605152592Sandre
606152592Sandre	pcbopt = &inp->inp_options;
607152592Sandre
608152592Sandre	/* turn off any old options */
609152592Sandre	if (*pcbopt)
610152592Sandre		(void)m_free(*pcbopt);
611298066Spfg	*pcbopt = NULL;
612152592Sandre	if (m == NULL || m->m_len == 0) {
613152592Sandre		/*
614152592Sandre		 * Only turning off any previous options.
615152592Sandre		 */
616152592Sandre		if (m != NULL)
617152592Sandre			(void)m_free(m);
618152592Sandre		return (0);
619152592Sandre	}
620152592Sandre
621152592Sandre	if (m->m_len % sizeof(int32_t))
622152592Sandre		goto bad;
623152592Sandre	/*
624169464Srwatson	 * IP first-hop destination address will be stored before actual
625169464Srwatson	 * options; move other options back and clear it when none present.
626152592Sandre	 */
627152592Sandre	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
628152592Sandre		goto bad;
629152592Sandre	cnt = m->m_len;
630152592Sandre	m->m_len += sizeof(struct in_addr);
631152592Sandre	cp = mtod(m, u_char *) + sizeof(struct in_addr);
632152592Sandre	bcopy(mtod(m, void *), cp, (unsigned)cnt);
633152592Sandre	bzero(mtod(m, void *), sizeof(struct in_addr));
634152592Sandre
635152592Sandre	for (; cnt > 0; cnt -= optlen, cp += optlen) {
636152592Sandre		opt = cp[IPOPT_OPTVAL];
637152592Sandre		if (opt == IPOPT_EOL)
638152592Sandre			break;
639152592Sandre		if (opt == IPOPT_NOP)
640152592Sandre			optlen = 1;
641152592Sandre		else {
642152592Sandre			if (cnt < IPOPT_OLEN + sizeof(*cp))
643152592Sandre				goto bad;
644152592Sandre			optlen = cp[IPOPT_OLEN];
645152592Sandre			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
646152592Sandre				goto bad;
647152592Sandre		}
648152592Sandre		switch (opt) {
649152592Sandre
650152592Sandre		default:
651152592Sandre			break;
652152592Sandre
653152592Sandre		case IPOPT_LSRR:
654152592Sandre		case IPOPT_SSRR:
655152592Sandre			/*
656169464Srwatson			 * User process specifies route as:
657169464Srwatson			 *
658152592Sandre			 *	->A->B->C->D
659169464Srwatson			 *
660152592Sandre			 * D must be our final destination (but we can't
661152592Sandre			 * check that since we may not have connected yet).
662169464Srwatson			 * A is first hop destination, which doesn't appear
663169464Srwatson			 * in actual IP option, but is stored before the
664169464Srwatson			 * options.
665152592Sandre			 */
666175630Sbz			/* XXX-BZ PRIV_NETINET_SETHDROPTS? */
667152592Sandre			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
668152592Sandre				goto bad;
669152592Sandre			m->m_len -= sizeof(struct in_addr);
670152592Sandre			cnt -= sizeof(struct in_addr);
671152592Sandre			optlen -= sizeof(struct in_addr);
672152592Sandre			cp[IPOPT_OLEN] = optlen;
673152592Sandre			/*
674152592Sandre			 * Move first hop before start of options.
675152592Sandre			 */
676152592Sandre			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
677152592Sandre			    sizeof(struct in_addr));
678152592Sandre			/*
679152592Sandre			 * Then copy rest of options back
680152592Sandre			 * to close up the deleted entry.
681152592Sandre			 */
682152592Sandre			bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
683152592Sandre			    &cp[IPOPT_OFFSET+1],
684152592Sandre			    (unsigned)cnt - (IPOPT_MINOFF - 1));
685152592Sandre			break;
686152592Sandre		}
687152592Sandre	}
688152592Sandre	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
689152592Sandre		goto bad;
690152592Sandre	*pcbopt = m;
691152592Sandre	return (0);
692152592Sandre
693152592Sandrebad:
694152592Sandre	(void)m_free(m);
695152592Sandre	return (EINVAL);
696152592Sandre}
697189343Sbms
698189343Sbms/*
699189343Sbms * Check for the presence of the IP Router Alert option [RFC2113]
700189343Sbms * in the header of an IPv4 datagram.
701189343Sbms *
702189343Sbms * This call is not intended for use from the forwarding path; it is here
703189343Sbms * so that protocol domains may check for the presence of the option.
704189343Sbms * Given how FreeBSD's IPv4 stack is currently structured, the Router Alert
705189343Sbms * option does not have much relevance to the implementation, though this
706189343Sbms * may change in future.
707189343Sbms * Router alert options SHOULD be passed if running in IPSTEALTH mode and
708189343Sbms * we are not the endpoint.
709298995Spfg * Length checks on individual options should already have been performed
710189343Sbms * by ip_dooptions() therefore they are folded under INVARIANTS here.
711189343Sbms *
712189343Sbms * Return zero if not present or options are invalid, non-zero if present.
713189343Sbms */
714189343Sbmsint
715189343Sbmsip_checkrouteralert(struct mbuf *m)
716189343Sbms{
717189343Sbms	struct ip *ip = mtod(m, struct ip *);
718189343Sbms	u_char *cp;
719189343Sbms	int opt, optlen, cnt, found_ra;
720189343Sbms
721189343Sbms	found_ra = 0;
722189343Sbms	cp = (u_char *)(ip + 1);
723189343Sbms	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
724189343Sbms	for (; cnt > 0; cnt -= optlen, cp += optlen) {
725189343Sbms		opt = cp[IPOPT_OPTVAL];
726189343Sbms		if (opt == IPOPT_EOL)
727189343Sbms			break;
728189343Sbms		if (opt == IPOPT_NOP)
729189343Sbms			optlen = 1;
730189343Sbms		else {
731189343Sbms#ifdef INVARIANTS
732189343Sbms			if (cnt < IPOPT_OLEN + sizeof(*cp))
733189343Sbms				break;
734189343Sbms#endif
735189343Sbms			optlen = cp[IPOPT_OLEN];
736189343Sbms#ifdef INVARIANTS
737189343Sbms			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
738189343Sbms				break;
739189343Sbms#endif
740189343Sbms		}
741189343Sbms		switch (opt) {
742189343Sbms		case IPOPT_RA:
743189343Sbms#ifdef INVARIANTS
744189343Sbms			if (optlen != IPOPT_OFFSET + sizeof(uint16_t) ||
745189343Sbms			    (*((uint16_t *)&cp[IPOPT_OFFSET]) != 0))
746189343Sbms			    break;
747189343Sbms			else
748189343Sbms#endif
749189343Sbms			found_ra = 1;
750189343Sbms			break;
751189343Sbms		default:
752189343Sbms			break;
753189343Sbms		}
754189343Sbms	}
755189343Sbms
756189343Sbms	return (found_ra);
757189343Sbms}
758