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$");
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>
57152592Sandre#include <netinet/in_systm.h>
58152592Sandre#include <netinet/in_var.h>
59152592Sandre#include <netinet/ip.h>
60152592Sandre#include <netinet/in_pcb.h>
61152592Sandre#include <netinet/ip_var.h>
62152592Sandre#include <netinet/ip_options.h>
63152592Sandre#include <netinet/ip_icmp.h>
64152592Sandre#include <machine/in_cksum.h>
65152592Sandre
66152592Sandre#include <sys/socketvar.h>
67152592Sandre
68152592Sandrestatic int	ip_dosourceroute = 0;
69152592SandreSYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
70152592Sandre    &ip_dosourceroute, 0, "Enable forwarding source routed IP packets");
71152592Sandre
72152592Sandrestatic int	ip_acceptsourceroute = 0;
73152592SandreSYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
74152592Sandre    CTLFLAG_RW, &ip_acceptsourceroute, 0,
75152592Sandre    "Enable accepting source routed IP packets");
76152592Sandre
77152592Sandreint		ip_doopts = 1;	/* 0 = ignore, 1 = process, 2 = reject */
78152592SandreSYSCTL_INT(_net_inet_ip, OID_AUTO, process_options, CTLFLAG_RW,
79152592Sandre    &ip_doopts, 0, "Enable IP options processing ([LS]SRR, RR, TS)");
80152592Sandre
81152592Sandrestatic void	save_rte(struct mbuf *m, u_char *, struct in_addr);
82152592Sandre
83152592Sandre/*
84169464Srwatson * Do option processing on a datagram, possibly discarding it if bad options
85169464Srwatson * are encountered, or forwarding it if source-routed.
86169464Srwatson *
87169464Srwatson * The pass argument is used when operating in the IPSTEALTH mode to tell
88169464Srwatson * what options to process: [LS]SRR (pass 0) or the others (pass 1).  The
89169464Srwatson * reason for as many as two passes is that when doing IPSTEALTH, non-routing
90169464Srwatson * options should be processed only if the packet is for us.
91169464Srwatson *
92169464Srwatson * Returns 1 if packet has been forwarded/freed, 0 if the packet should be
93169464Srwatson * processed further.
94152592Sandre */
95152592Sandreint
96152592Sandreip_dooptions(struct mbuf *m, int pass)
97152592Sandre{
98152592Sandre	struct ip *ip = mtod(m, struct ip *);
99152592Sandre	u_char *cp;
100152592Sandre	struct in_ifaddr *ia;
101152592Sandre	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
102152592Sandre	struct in_addr *sin, dst;
103188578Sluigi	uint32_t ntime;
104152592Sandre	struct	sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
105152592Sandre
106169464Srwatson	/* Ignore or reject packets with IP options. */
107152592Sandre	if (ip_doopts == 0)
108152592Sandre		return 0;
109152592Sandre	else if (ip_doopts == 2) {
110152592Sandre		type = ICMP_UNREACH;
111152592Sandre		code = ICMP_UNREACH_FILTER_PROHIB;
112152592Sandre		goto bad;
113152592Sandre	}
114152592Sandre
115152592Sandre	dst = ip->ip_dst;
116152592Sandre	cp = (u_char *)(ip + 1);
117152592Sandre	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
118152592Sandre	for (; cnt > 0; cnt -= optlen, cp += optlen) {
119152592Sandre		opt = cp[IPOPT_OPTVAL];
120152592Sandre		if (opt == IPOPT_EOL)
121152592Sandre			break;
122152592Sandre		if (opt == IPOPT_NOP)
123152592Sandre			optlen = 1;
124152592Sandre		else {
125152592Sandre			if (cnt < IPOPT_OLEN + sizeof(*cp)) {
126152592Sandre				code = &cp[IPOPT_OLEN] - (u_char *)ip;
127152592Sandre				goto bad;
128152592Sandre			}
129152592Sandre			optlen = cp[IPOPT_OLEN];
130152592Sandre			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
131152592Sandre				code = &cp[IPOPT_OLEN] - (u_char *)ip;
132152592Sandre				goto bad;
133152592Sandre			}
134152592Sandre		}
135152592Sandre		switch (opt) {
136152592Sandre
137152592Sandre		default:
138152592Sandre			break;
139152592Sandre
140152592Sandre		/*
141169464Srwatson		 * Source routing with record.  Find interface with current
142169464Srwatson		 * destination address.  If none on this machine then drop if
143169464Srwatson		 * strictly routed, or do nothing if loosely routed.  Record
144169464Srwatson		 * interface address and bring up next address component.  If
145169464Srwatson		 * strictly routed make sure next address is on directly
146169464Srwatson		 * accessible net.
147152592Sandre		 */
148152592Sandre		case IPOPT_LSRR:
149152592Sandre		case IPOPT_SSRR:
150152592Sandre#ifdef IPSTEALTH
151181803Sbz			if (V_ipstealth && pass > 0)
152152592Sandre				break;
153152592Sandre#endif
154152592Sandre			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
155152592Sandre				code = &cp[IPOPT_OLEN] - (u_char *)ip;
156152592Sandre				goto bad;
157152592Sandre			}
158152592Sandre			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
159152592Sandre				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
160152592Sandre				goto bad;
161152592Sandre			}
162152592Sandre			ipaddr.sin_addr = ip->ip_dst;
163194760Srwatson			if (ifa_ifwithaddr_check((struct sockaddr *)&ipaddr)
164194760Srwatson			    == 0) {
165152592Sandre				if (opt == IPOPT_SSRR) {
166152592Sandre					type = ICMP_UNREACH;
167152592Sandre					code = ICMP_UNREACH_SRCFAIL;
168152592Sandre					goto bad;
169152592Sandre				}
170152592Sandre				if (!ip_dosourceroute)
171152592Sandre					goto nosourcerouting;
172152592Sandre				/*
173152592Sandre				 * Loose routing, and not at next destination
174152592Sandre				 * yet; nothing to do except forward.
175152592Sandre				 */
176152592Sandre				break;
177152592Sandre			}
178152592Sandre			off--;			/* 0 origin */
179152592Sandre			if (off > optlen - (int)sizeof(struct in_addr)) {
180152592Sandre				/*
181152592Sandre				 * End of source route.  Should be for us.
182152592Sandre				 */
183152592Sandre				if (!ip_acceptsourceroute)
184152592Sandre					goto nosourcerouting;
185152592Sandre				save_rte(m, cp, ip->ip_src);
186152592Sandre				break;
187152592Sandre			}
188152592Sandre#ifdef IPSTEALTH
189181803Sbz			if (V_ipstealth)
190152592Sandre				goto dropit;
191152592Sandre#endif
192152592Sandre			if (!ip_dosourceroute) {
193181803Sbz				if (V_ipforwarding) {
194152592Sandre					char buf[16]; /* aaa.bbb.ccc.ddd\0 */
195152592Sandre					/*
196169464Srwatson					 * Acting as a router, so generate
197169464Srwatson					 * ICMP
198152592Sandre					 */
199152592Sandrenosourcerouting:
200152592Sandre					strcpy(buf, inet_ntoa(ip->ip_dst));
201152592Sandre					log(LOG_WARNING,
202152592Sandre					    "attempted source route from %s to %s\n",
203152592Sandre					    inet_ntoa(ip->ip_src), buf);
204152592Sandre					type = ICMP_UNREACH;
205152592Sandre					code = ICMP_UNREACH_SRCFAIL;
206152592Sandre					goto bad;
207152592Sandre				} else {
208152592Sandre					/*
209169464Srwatson					 * Not acting as a router, so
210169464Srwatson					 * silently drop.
211152592Sandre					 */
212152592Sandre#ifdef IPSTEALTH
213152592Sandredropit:
214152592Sandre#endif
215190951Srwatson					IPSTAT_INC(ips_cantforward);
216152592Sandre					m_freem(m);
217152592Sandre					return (1);
218152592Sandre				}
219152592Sandre			}
220152592Sandre
221152592Sandre			/*
222152592Sandre			 * locate outgoing interface
223152592Sandre			 */
224152592Sandre			(void)memcpy(&ipaddr.sin_addr, cp + off,
225152592Sandre			    sizeof(ipaddr.sin_addr));
226152592Sandre
227152592Sandre			if (opt == IPOPT_SSRR) {
228152592Sandre#define	INA	struct in_ifaddr *
229152592Sandre#define	SA	struct sockaddr *
230152592Sandre			    if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == NULL)
231208553Sqingli				    ia = (INA)ifa_ifwithnet((SA)&ipaddr, 0);
232152592Sandre			} else
233178888Sjulian/* XXX MRT 0 for routing */
234178888Sjulian				ia = ip_rtaddr(ipaddr.sin_addr, M_GETFIB(m));
235152592Sandre			if (ia == NULL) {
236152592Sandre				type = ICMP_UNREACH;
237152592Sandre				code = ICMP_UNREACH_SRCFAIL;
238152592Sandre				goto bad;
239152592Sandre			}
240152592Sandre			ip->ip_dst = ipaddr.sin_addr;
241152592Sandre			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
242152592Sandre			    sizeof(struct in_addr));
243194760Srwatson			ifa_free(&ia->ia_ifa);
244152592Sandre			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
245152592Sandre			/*
246152592Sandre			 * Let ip_intr's mcast routing check handle mcast pkts
247152592Sandre			 */
248152592Sandre			forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
249152592Sandre			break;
250152592Sandre
251152592Sandre		case IPOPT_RR:
252152592Sandre#ifdef IPSTEALTH
253181803Sbz			if (V_ipstealth && pass == 0)
254152592Sandre				break;
255152592Sandre#endif
256152592Sandre			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
257152592Sandre				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
258152592Sandre				goto bad;
259152592Sandre			}
260152592Sandre			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
261152592Sandre				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
262152592Sandre				goto bad;
263152592Sandre			}
264152592Sandre			/*
265152592Sandre			 * If no space remains, ignore.
266152592Sandre			 */
267152592Sandre			off--;			/* 0 origin */
268152592Sandre			if (off > optlen - (int)sizeof(struct in_addr))
269152592Sandre				break;
270152592Sandre			(void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
271152592Sandre			    sizeof(ipaddr.sin_addr));
272152592Sandre			/*
273169464Srwatson			 * Locate outgoing interface; if we're the
274169464Srwatson			 * destination, use the incoming interface (should be
275169464Srwatson			 * same).
276152592Sandre			 */
277152592Sandre			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == NULL &&
278178888Sjulian			    (ia = ip_rtaddr(ipaddr.sin_addr, M_GETFIB(m))) == NULL) {
279152592Sandre				type = ICMP_UNREACH;
280152592Sandre				code = ICMP_UNREACH_HOST;
281152592Sandre				goto bad;
282152592Sandre			}
283152592Sandre			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
284152592Sandre			    sizeof(struct in_addr));
285194760Srwatson			ifa_free(&ia->ia_ifa);
286152592Sandre			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
287152592Sandre			break;
288152592Sandre
289152592Sandre		case IPOPT_TS:
290152592Sandre#ifdef IPSTEALTH
291181803Sbz			if (V_ipstealth && pass == 0)
292152592Sandre				break;
293152592Sandre#endif
294152592Sandre			code = cp - (u_char *)ip;
295152592Sandre			if (optlen < 4 || optlen > 40) {
296152592Sandre				code = &cp[IPOPT_OLEN] - (u_char *)ip;
297152592Sandre				goto bad;
298152592Sandre			}
299152592Sandre			if ((off = cp[IPOPT_OFFSET]) < 5) {
300152592Sandre				code = &cp[IPOPT_OLEN] - (u_char *)ip;
301152592Sandre				goto bad;
302152592Sandre			}
303152592Sandre			if (off > optlen - (int)sizeof(int32_t)) {
304152592Sandre				cp[IPOPT_OFFSET + 1] += (1 << 4);
305152592Sandre				if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) {
306152592Sandre					code = &cp[IPOPT_OFFSET] - (u_char *)ip;
307152592Sandre					goto bad;
308152592Sandre				}
309152592Sandre				break;
310152592Sandre			}
311152592Sandre			off--;				/* 0 origin */
312152592Sandre			sin = (struct in_addr *)(cp + off);
313152592Sandre			switch (cp[IPOPT_OFFSET + 1] & 0x0f) {
314152592Sandre
315152592Sandre			case IPOPT_TS_TSONLY:
316152592Sandre				break;
317152592Sandre
318152592Sandre			case IPOPT_TS_TSANDADDR:
319188578Sluigi				if (off + sizeof(uint32_t) +
320152592Sandre				    sizeof(struct in_addr) > optlen) {
321152592Sandre					code = &cp[IPOPT_OFFSET] - (u_char *)ip;
322152592Sandre					goto bad;
323152592Sandre				}
324152592Sandre				ipaddr.sin_addr = dst;
325152592Sandre				ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
326152592Sandre							    m->m_pkthdr.rcvif);
327152592Sandre				if (ia == NULL)
328152592Sandre					continue;
329152592Sandre				(void)memcpy(sin, &IA_SIN(ia)->sin_addr,
330152592Sandre				    sizeof(struct in_addr));
331194760Srwatson				ifa_free(&ia->ia_ifa);
332152592Sandre				cp[IPOPT_OFFSET] += sizeof(struct in_addr);
333152592Sandre				off += sizeof(struct in_addr);
334152592Sandre				break;
335152592Sandre
336152592Sandre			case IPOPT_TS_PRESPEC:
337188578Sluigi				if (off + sizeof(uint32_t) +
338152592Sandre				    sizeof(struct in_addr) > optlen) {
339152592Sandre					code = &cp[IPOPT_OFFSET] - (u_char *)ip;
340152592Sandre					goto bad;
341152592Sandre				}
342152592Sandre				(void)memcpy(&ipaddr.sin_addr, sin,
343152592Sandre				    sizeof(struct in_addr));
344213832Sbz				if (ifa_ifwithaddr_check((SA)&ipaddr) == 0)
345152592Sandre					continue;
346152592Sandre				cp[IPOPT_OFFSET] += sizeof(struct in_addr);
347152592Sandre				off += sizeof(struct in_addr);
348152592Sandre				break;
349152592Sandre
350152592Sandre			default:
351152592Sandre				code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip;
352152592Sandre				goto bad;
353152592Sandre			}
354152592Sandre			ntime = iptime();
355188578Sluigi			(void)memcpy(cp + off, &ntime, sizeof(uint32_t));
356188578Sluigi			cp[IPOPT_OFFSET] += sizeof(uint32_t);
357152592Sandre		}
358152592Sandre	}
359181803Sbz	if (forward && V_ipforwarding) {
360152592Sandre		ip_forward(m, 1);
361152592Sandre		return (1);
362152592Sandre	}
363152592Sandre	return (0);
364152592Sandrebad:
365152592Sandre	icmp_error(m, type, code, 0, 0);
366190951Srwatson	IPSTAT_INC(ips_badoptions);
367152592Sandre	return (1);
368152592Sandre}
369152592Sandre
370152592Sandre/*
371169464Srwatson * Save incoming source route for use in replies, to be picked up later by
372169464Srwatson * ip_srcroute if the receiver is interested.
373152592Sandre */
374152592Sandrestatic void
375169454Srwatsonsave_rte(struct mbuf *m, u_char *option, struct in_addr dst)
376152592Sandre{
377152592Sandre	unsigned olen;
378152592Sandre	struct ipopt_tag *opts;
379152592Sandre
380152592Sandre	opts = (struct ipopt_tag *)m_tag_get(PACKET_TAG_IPOPTIONS,
381169464Srwatson	    sizeof(struct ipopt_tag), M_NOWAIT);
382152592Sandre	if (opts == NULL)
383152592Sandre		return;
384152592Sandre
385152592Sandre	olen = option[IPOPT_OLEN];
386152592Sandre	if (olen > sizeof(opts->ip_srcrt) - (1 + sizeof(dst))) {
387152592Sandre		m_tag_free((struct m_tag *)opts);
388152592Sandre		return;
389152592Sandre	}
390152592Sandre	bcopy(option, opts->ip_srcrt.srcopt, olen);
391152592Sandre	opts->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
392152592Sandre	opts->ip_srcrt.dst = dst;
393152592Sandre	m_tag_prepend(m, (struct m_tag *)opts);
394152592Sandre}
395152592Sandre
396152592Sandre/*
397169464Srwatson * Retrieve incoming source route for use in replies, in the same form used
398169464Srwatson * by setsockopt.  The first hop is placed before the options, will be
399169464Srwatson * removed later.
400152592Sandre */
401152592Sandrestruct mbuf *
402169454Srwatsonip_srcroute(struct mbuf *m0)
403152592Sandre{
404169464Srwatson	struct in_addr *p, *q;
405169464Srwatson	struct mbuf *m;
406152592Sandre	struct ipopt_tag *opts;
407152592Sandre
408152592Sandre	opts = (struct ipopt_tag *)m_tag_find(m0, PACKET_TAG_IPOPTIONS, NULL);
409152592Sandre	if (opts == NULL)
410152592Sandre		return (NULL);
411152592Sandre
412152592Sandre	if (opts->ip_nhops == 0)
413152592Sandre		return (NULL);
414152592Sandre	m = m_get(M_DONTWAIT, MT_DATA);
415152592Sandre	if (m == NULL)
416152592Sandre		return (NULL);
417152592Sandre
418152592Sandre#define OPTSIZ	(sizeof(opts->ip_srcrt.nop) + sizeof(opts->ip_srcrt.srcopt))
419152592Sandre
420152592Sandre	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
421152592Sandre	m->m_len = opts->ip_nhops * sizeof(struct in_addr) +
422152592Sandre	    sizeof(struct in_addr) + OPTSIZ;
423152592Sandre
424152592Sandre	/*
425169464Srwatson	 * First, save first hop for return route.
426152592Sandre	 */
427152592Sandre	p = &(opts->ip_srcrt.route[opts->ip_nhops - 1]);
428152592Sandre	*(mtod(m, struct in_addr *)) = *p--;
429152592Sandre
430152592Sandre	/*
431152592Sandre	 * Copy option fields and padding (nop) to mbuf.
432152592Sandre	 */
433152592Sandre	opts->ip_srcrt.nop = IPOPT_NOP;
434152592Sandre	opts->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
435152592Sandre	(void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
436152592Sandre	    &(opts->ip_srcrt.nop), OPTSIZ);
437152592Sandre	q = (struct in_addr *)(mtod(m, caddr_t) +
438152592Sandre	    sizeof(struct in_addr) + OPTSIZ);
439152592Sandre#undef OPTSIZ
440152592Sandre	/*
441169464Srwatson	 * Record return path as an IP source route, reversing the path
442169464Srwatson	 * (pointers are now aligned).
443152592Sandre	 */
444152592Sandre	while (p >= opts->ip_srcrt.route) {
445152592Sandre		*q++ = *p--;
446152592Sandre	}
447152592Sandre	/*
448152592Sandre	 * Last hop goes to final destination.
449152592Sandre	 */
450152592Sandre	*q = opts->ip_srcrt.dst;
451152592Sandre	m_tag_delete(m0, (struct m_tag *)opts);
452152592Sandre	return (m);
453152592Sandre}
454152592Sandre
455152592Sandre/*
456169464Srwatson * Strip out IP options, at higher level protocol in the kernel.  Second
457169464Srwatson * argument is buffer to which options will be moved, and return value is
458169464Srwatson * their length.
459169464Srwatson *
460152592Sandre * XXX should be deleted; last arg currently ignored.
461152592Sandre */
462152592Sandrevoid
463169454Srwatsonip_stripoptions(struct mbuf *m, struct mbuf *mopt)
464152592Sandre{
465169464Srwatson	int i;
466152592Sandre	struct ip *ip = mtod(m, struct ip *);
467169464Srwatson	caddr_t opts;
468152592Sandre	int olen;
469152592Sandre
470152592Sandre	olen = (ip->ip_hl << 2) - sizeof (struct ip);
471152592Sandre	opts = (caddr_t)(ip + 1);
472152592Sandre	i = m->m_len - (sizeof (struct ip) + olen);
473152592Sandre	bcopy(opts + olen, opts, (unsigned)i);
474152592Sandre	m->m_len -= olen;
475152592Sandre	if (m->m_flags & M_PKTHDR)
476152592Sandre		m->m_pkthdr.len -= olen;
477152592Sandre	ip->ip_v = IPVERSION;
478152592Sandre	ip->ip_hl = sizeof(struct ip) >> 2;
479152592Sandre}
480152592Sandre
481152592Sandre/*
482169464Srwatson * Insert IP options into preformed packet.  Adjust IP destination as
483169464Srwatson * required for IP source routing, as indicated by a non-zero in_addr at the
484169464Srwatson * start of the options.
485152592Sandre *
486152592Sandre * XXX This routine assumes that the packet has no options in place.
487152592Sandre */
488152592Sandrestruct mbuf *
489169454Srwatsonip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
490152592Sandre{
491169464Srwatson	struct ipoption *p = mtod(opt, struct ipoption *);
492152592Sandre	struct mbuf *n;
493169464Srwatson	struct ip *ip = mtod(m, struct ip *);
494152592Sandre	unsigned optlen;
495152592Sandre
496152592Sandre	optlen = opt->m_len - sizeof(p->ipopt_dst);
497152592Sandre	if (optlen + ip->ip_len > IP_MAXPACKET) {
498152592Sandre		*phlen = 0;
499152592Sandre		return (m);		/* XXX should fail */
500152592Sandre	}
501152592Sandre	if (p->ipopt_dst.s_addr)
502152592Sandre		ip->ip_dst = p->ipopt_dst;
503152592Sandre	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
504152592Sandre		MGETHDR(n, M_DONTWAIT, MT_DATA);
505152592Sandre		if (n == NULL) {
506152592Sandre			*phlen = 0;
507152592Sandre			return (m);
508152592Sandre		}
509152592Sandre		M_MOVE_PKTHDR(n, m);
510152592Sandre		n->m_pkthdr.rcvif = NULL;
511152592Sandre		n->m_pkthdr.len += optlen;
512152592Sandre		m->m_len -= sizeof(struct ip);
513152592Sandre		m->m_data += sizeof(struct ip);
514152592Sandre		n->m_next = m;
515152592Sandre		m = n;
516152592Sandre		m->m_len = optlen + sizeof(struct ip);
517152592Sandre		m->m_data += max_linkhdr;
518152592Sandre		bcopy(ip, mtod(m, void *), sizeof(struct ip));
519152592Sandre	} else {
520152592Sandre		m->m_data -= optlen;
521152592Sandre		m->m_len += optlen;
522152592Sandre		m->m_pkthdr.len += optlen;
523152592Sandre		bcopy(ip, mtod(m, void *), sizeof(struct ip));
524152592Sandre	}
525152592Sandre	ip = mtod(m, struct ip *);
526152592Sandre	bcopy(p->ipopt_list, ip + 1, optlen);
527152592Sandre	*phlen = sizeof(struct ip) + optlen;
528152592Sandre	ip->ip_v = IPVERSION;
529152592Sandre	ip->ip_hl = *phlen >> 2;
530152592Sandre	ip->ip_len += optlen;
531152592Sandre	return (m);
532152592Sandre}
533152592Sandre
534152592Sandre/*
535169464Srwatson * Copy options from ip to jp, omitting those not copied during
536169464Srwatson * fragmentation.
537152592Sandre */
538152592Sandreint
539169454Srwatsonip_optcopy(struct ip *ip, struct ip *jp)
540152592Sandre{
541169464Srwatson	u_char *cp, *dp;
542152592Sandre	int opt, optlen, cnt;
543152592Sandre
544152592Sandre	cp = (u_char *)(ip + 1);
545152592Sandre	dp = (u_char *)(jp + 1);
546152592Sandre	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
547152592Sandre	for (; cnt > 0; cnt -= optlen, cp += optlen) {
548152592Sandre		opt = cp[0];
549152592Sandre		if (opt == IPOPT_EOL)
550152592Sandre			break;
551152592Sandre		if (opt == IPOPT_NOP) {
552152592Sandre			/* Preserve for IP mcast tunnel's LSRR alignment. */
553152592Sandre			*dp++ = IPOPT_NOP;
554152592Sandre			optlen = 1;
555152592Sandre			continue;
556152592Sandre		}
557152592Sandre
558152592Sandre		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
559152592Sandre		    ("ip_optcopy: malformed ipv4 option"));
560152592Sandre		optlen = cp[IPOPT_OLEN];
561152592Sandre		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
562152592Sandre		    ("ip_optcopy: malformed ipv4 option"));
563152592Sandre
564169464Srwatson		/* Bogus lengths should have been caught by ip_dooptions. */
565152592Sandre		if (optlen > cnt)
566152592Sandre			optlen = cnt;
567152592Sandre		if (IPOPT_COPIED(opt)) {
568152592Sandre			bcopy(cp, dp, optlen);
569152592Sandre			dp += optlen;
570152592Sandre		}
571152592Sandre	}
572152592Sandre	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
573152592Sandre		*dp++ = IPOPT_EOL;
574152592Sandre	return (optlen);
575152592Sandre}
576152592Sandre
577152592Sandre/*
578169464Srwatson * Set up IP options in pcb for insertion in output packets.  Store in mbuf
579169464Srwatson * with pointer in pcbopt, adding pseudo-option with destination address if
580169464Srwatson * source routed.
581152592Sandre */
582152592Sandreint
583152592Sandreip_pcbopts(struct inpcb *inp, int optname, struct mbuf *m)
584152592Sandre{
585169464Srwatson	int cnt, optlen;
586169464Srwatson	u_char *cp;
587152592Sandre	struct mbuf **pcbopt;
588152592Sandre	u_char opt;
589152592Sandre
590178285Srwatson	INP_WLOCK_ASSERT(inp);
591152592Sandre
592152592Sandre	pcbopt = &inp->inp_options;
593152592Sandre
594152592Sandre	/* turn off any old options */
595152592Sandre	if (*pcbopt)
596152592Sandre		(void)m_free(*pcbopt);
597152592Sandre	*pcbopt = 0;
598152592Sandre	if (m == NULL || m->m_len == 0) {
599152592Sandre		/*
600152592Sandre		 * Only turning off any previous options.
601152592Sandre		 */
602152592Sandre		if (m != NULL)
603152592Sandre			(void)m_free(m);
604152592Sandre		return (0);
605152592Sandre	}
606152592Sandre
607152592Sandre	if (m->m_len % sizeof(int32_t))
608152592Sandre		goto bad;
609152592Sandre	/*
610169464Srwatson	 * IP first-hop destination address will be stored before actual
611169464Srwatson	 * options; move other options back and clear it when none present.
612152592Sandre	 */
613152592Sandre	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
614152592Sandre		goto bad;
615152592Sandre	cnt = m->m_len;
616152592Sandre	m->m_len += sizeof(struct in_addr);
617152592Sandre	cp = mtod(m, u_char *) + sizeof(struct in_addr);
618152592Sandre	bcopy(mtod(m, void *), cp, (unsigned)cnt);
619152592Sandre	bzero(mtod(m, void *), sizeof(struct in_addr));
620152592Sandre
621152592Sandre	for (; cnt > 0; cnt -= optlen, cp += optlen) {
622152592Sandre		opt = cp[IPOPT_OPTVAL];
623152592Sandre		if (opt == IPOPT_EOL)
624152592Sandre			break;
625152592Sandre		if (opt == IPOPT_NOP)
626152592Sandre			optlen = 1;
627152592Sandre		else {
628152592Sandre			if (cnt < IPOPT_OLEN + sizeof(*cp))
629152592Sandre				goto bad;
630152592Sandre			optlen = cp[IPOPT_OLEN];
631152592Sandre			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
632152592Sandre				goto bad;
633152592Sandre		}
634152592Sandre		switch (opt) {
635152592Sandre
636152592Sandre		default:
637152592Sandre			break;
638152592Sandre
639152592Sandre		case IPOPT_LSRR:
640152592Sandre		case IPOPT_SSRR:
641152592Sandre			/*
642169464Srwatson			 * User process specifies route as:
643169464Srwatson			 *
644152592Sandre			 *	->A->B->C->D
645169464Srwatson			 *
646152592Sandre			 * D must be our final destination (but we can't
647152592Sandre			 * check that since we may not have connected yet).
648169464Srwatson			 * A is first hop destination, which doesn't appear
649169464Srwatson			 * in actual IP option, but is stored before the
650169464Srwatson			 * options.
651152592Sandre			 */
652175630Sbz			/* XXX-BZ PRIV_NETINET_SETHDROPTS? */
653152592Sandre			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
654152592Sandre				goto bad;
655152592Sandre			m->m_len -= sizeof(struct in_addr);
656152592Sandre			cnt -= sizeof(struct in_addr);
657152592Sandre			optlen -= sizeof(struct in_addr);
658152592Sandre			cp[IPOPT_OLEN] = optlen;
659152592Sandre			/*
660152592Sandre			 * Move first hop before start of options.
661152592Sandre			 */
662152592Sandre			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
663152592Sandre			    sizeof(struct in_addr));
664152592Sandre			/*
665152592Sandre			 * Then copy rest of options back
666152592Sandre			 * to close up the deleted entry.
667152592Sandre			 */
668152592Sandre			bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
669152592Sandre			    &cp[IPOPT_OFFSET+1],
670152592Sandre			    (unsigned)cnt - (IPOPT_MINOFF - 1));
671152592Sandre			break;
672152592Sandre		}
673152592Sandre	}
674152592Sandre	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
675152592Sandre		goto bad;
676152592Sandre	*pcbopt = m;
677152592Sandre	return (0);
678152592Sandre
679152592Sandrebad:
680152592Sandre	(void)m_free(m);
681152592Sandre	return (EINVAL);
682152592Sandre}
683189343Sbms
684189343Sbms/*
685189343Sbms * Check for the presence of the IP Router Alert option [RFC2113]
686189343Sbms * in the header of an IPv4 datagram.
687189343Sbms *
688189343Sbms * This call is not intended for use from the forwarding path; it is here
689189343Sbms * so that protocol domains may check for the presence of the option.
690189343Sbms * Given how FreeBSD's IPv4 stack is currently structured, the Router Alert
691189343Sbms * option does not have much relevance to the implementation, though this
692189343Sbms * may change in future.
693189343Sbms * Router alert options SHOULD be passed if running in IPSTEALTH mode and
694189343Sbms * we are not the endpoint.
695189343Sbms * Length checks on individual options should already have been peformed
696189343Sbms * by ip_dooptions() therefore they are folded under INVARIANTS here.
697189343Sbms *
698189343Sbms * Return zero if not present or options are invalid, non-zero if present.
699189343Sbms */
700189343Sbmsint
701189343Sbmsip_checkrouteralert(struct mbuf *m)
702189343Sbms{
703189343Sbms	struct ip *ip = mtod(m, struct ip *);
704189343Sbms	u_char *cp;
705189343Sbms	int opt, optlen, cnt, found_ra;
706189343Sbms
707189343Sbms	found_ra = 0;
708189343Sbms	cp = (u_char *)(ip + 1);
709189343Sbms	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
710189343Sbms	for (; cnt > 0; cnt -= optlen, cp += optlen) {
711189343Sbms		opt = cp[IPOPT_OPTVAL];
712189343Sbms		if (opt == IPOPT_EOL)
713189343Sbms			break;
714189343Sbms		if (opt == IPOPT_NOP)
715189343Sbms			optlen = 1;
716189343Sbms		else {
717189343Sbms#ifdef INVARIANTS
718189343Sbms			if (cnt < IPOPT_OLEN + sizeof(*cp))
719189343Sbms				break;
720189343Sbms#endif
721189343Sbms			optlen = cp[IPOPT_OLEN];
722189343Sbms#ifdef INVARIANTS
723189343Sbms			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
724189343Sbms				break;
725189343Sbms#endif
726189343Sbms		}
727189343Sbms		switch (opt) {
728189343Sbms		case IPOPT_RA:
729189343Sbms#ifdef INVARIANTS
730189343Sbms			if (optlen != IPOPT_OFFSET + sizeof(uint16_t) ||
731189343Sbms			    (*((uint16_t *)&cp[IPOPT_OFFSET]) != 0))
732189343Sbms			    break;
733189343Sbms			else
734189343Sbms#endif
735189343Sbms			found_ra = 1;
736189343Sbms			break;
737189343Sbms		default:
738189343Sbms			break;
739189343Sbms		}
740189343Sbms	}
741189343Sbms
742189343Sbms	return (found_ra);
743189343Sbms}
744