ip_options.c revision 175630
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: head/sys/netinet/ip_options.c 175630 2008-01-24 08:25:59Z bz $");
34172467Ssilby
35152592Sandre#include "opt_ipstealth.h"
36152592Sandre#include "opt_mac.h"
37152592Sandre
38152592Sandre#include <sys/param.h>
39152592Sandre#include <sys/systm.h>
40152592Sandre#include <sys/mbuf.h>
41152592Sandre#include <sys/domain.h>
42152592Sandre#include <sys/protosw.h>
43152592Sandre#include <sys/socket.h>
44152592Sandre#include <sys/time.h>
45152592Sandre#include <sys/kernel.h>
46152592Sandre#include <sys/syslog.h>
47152592Sandre#include <sys/sysctl.h>
48152592Sandre
49152592Sandre#include <net/if.h>
50152592Sandre#include <net/if_types.h>
51152592Sandre#include <net/if_var.h>
52152592Sandre#include <net/if_dl.h>
53152592Sandre#include <net/route.h>
54152592Sandre#include <net/netisr.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
68163606Srwatson#include <security/mac/mac_framework.h>
69163606Srwatson
70152592Sandrestatic int	ip_dosourceroute = 0;
71152592SandreSYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
72152592Sandre    &ip_dosourceroute, 0, "Enable forwarding source routed IP packets");
73152592Sandre
74152592Sandrestatic int	ip_acceptsourceroute = 0;
75152592SandreSYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
76152592Sandre    CTLFLAG_RW, &ip_acceptsourceroute, 0,
77152592Sandre    "Enable accepting source routed IP packets");
78152592Sandre
79152592Sandreint		ip_doopts = 1;	/* 0 = ignore, 1 = process, 2 = reject */
80152592SandreSYSCTL_INT(_net_inet_ip, OID_AUTO, process_options, CTLFLAG_RW,
81152592Sandre    &ip_doopts, 0, "Enable IP options processing ([LS]SRR, RR, TS)");
82152592Sandre
83152592Sandrestatic void	save_rte(struct mbuf *m, u_char *, struct in_addr);
84152592Sandre
85152592Sandre/*
86169464Srwatson * Do option processing on a datagram, possibly discarding it if bad options
87169464Srwatson * are encountered, or forwarding it if source-routed.
88169464Srwatson *
89169464Srwatson * The pass argument is used when operating in the IPSTEALTH mode to tell
90169464Srwatson * what options to process: [LS]SRR (pass 0) or the others (pass 1).  The
91169464Srwatson * reason for as many as two passes is that when doing IPSTEALTH, non-routing
92169464Srwatson * options should be processed only if the packet is for us.
93169464Srwatson *
94169464Srwatson * Returns 1 if packet has been forwarded/freed, 0 if the packet should be
95169464Srwatson * processed further.
96152592Sandre */
97152592Sandreint
98152592Sandreip_dooptions(struct mbuf *m, int pass)
99152592Sandre{
100152592Sandre	struct ip *ip = mtod(m, struct ip *);
101152592Sandre	u_char *cp;
102152592Sandre	struct in_ifaddr *ia;
103152592Sandre	int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
104152592Sandre	struct in_addr *sin, dst;
105152592Sandre	n_time ntime;
106152592Sandre	struct	sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
107152592Sandre
108169464Srwatson	/* Ignore or reject packets with IP options. */
109152592Sandre	if (ip_doopts == 0)
110152592Sandre		return 0;
111152592Sandre	else if (ip_doopts == 2) {
112152592Sandre		type = ICMP_UNREACH;
113152592Sandre		code = ICMP_UNREACH_FILTER_PROHIB;
114152592Sandre		goto bad;
115152592Sandre	}
116152592Sandre
117152592Sandre	dst = ip->ip_dst;
118152592Sandre	cp = (u_char *)(ip + 1);
119152592Sandre	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
120152592Sandre	for (; cnt > 0; cnt -= optlen, cp += optlen) {
121152592Sandre		opt = cp[IPOPT_OPTVAL];
122152592Sandre		if (opt == IPOPT_EOL)
123152592Sandre			break;
124152592Sandre		if (opt == IPOPT_NOP)
125152592Sandre			optlen = 1;
126152592Sandre		else {
127152592Sandre			if (cnt < IPOPT_OLEN + sizeof(*cp)) {
128152592Sandre				code = &cp[IPOPT_OLEN] - (u_char *)ip;
129152592Sandre				goto bad;
130152592Sandre			}
131152592Sandre			optlen = cp[IPOPT_OLEN];
132152592Sandre			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
133152592Sandre				code = &cp[IPOPT_OLEN] - (u_char *)ip;
134152592Sandre				goto bad;
135152592Sandre			}
136152592Sandre		}
137152592Sandre		switch (opt) {
138152592Sandre
139152592Sandre		default:
140152592Sandre			break;
141152592Sandre
142152592Sandre		/*
143169464Srwatson		 * Source routing with record.  Find interface with current
144169464Srwatson		 * destination address.  If none on this machine then drop if
145169464Srwatson		 * strictly routed, or do nothing if loosely routed.  Record
146169464Srwatson		 * interface address and bring up next address component.  If
147169464Srwatson		 * strictly routed make sure next address is on directly
148169464Srwatson		 * accessible net.
149152592Sandre		 */
150152592Sandre		case IPOPT_LSRR:
151152592Sandre		case IPOPT_SSRR:
152152592Sandre#ifdef IPSTEALTH
153152592Sandre			if (ipstealth && pass > 0)
154152592Sandre				break;
155152592Sandre#endif
156152592Sandre			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
157152592Sandre				code = &cp[IPOPT_OLEN] - (u_char *)ip;
158152592Sandre				goto bad;
159152592Sandre			}
160152592Sandre			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
161152592Sandre				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
162152592Sandre				goto bad;
163152592Sandre			}
164152592Sandre			ipaddr.sin_addr = ip->ip_dst;
165152592Sandre			ia = (struct in_ifaddr *)
166152592Sandre				ifa_ifwithaddr((struct sockaddr *)&ipaddr);
167152592Sandre			if (ia == NULL) {
168152592Sandre				if (opt == IPOPT_SSRR) {
169152592Sandre					type = ICMP_UNREACH;
170152592Sandre					code = ICMP_UNREACH_SRCFAIL;
171152592Sandre					goto bad;
172152592Sandre				}
173152592Sandre				if (!ip_dosourceroute)
174152592Sandre					goto nosourcerouting;
175152592Sandre				/*
176152592Sandre				 * Loose routing, and not at next destination
177152592Sandre				 * yet; nothing to do except forward.
178152592Sandre				 */
179152592Sandre				break;
180152592Sandre			}
181152592Sandre			off--;			/* 0 origin */
182152592Sandre			if (off > optlen - (int)sizeof(struct in_addr)) {
183152592Sandre				/*
184152592Sandre				 * End of source route.  Should be for us.
185152592Sandre				 */
186152592Sandre				if (!ip_acceptsourceroute)
187152592Sandre					goto nosourcerouting;
188152592Sandre				save_rte(m, cp, ip->ip_src);
189152592Sandre				break;
190152592Sandre			}
191152592Sandre#ifdef IPSTEALTH
192152592Sandre			if (ipstealth)
193152592Sandre				goto dropit;
194152592Sandre#endif
195152592Sandre			if (!ip_dosourceroute) {
196152592Sandre				if (ipforwarding) {
197152592Sandre					char buf[16]; /* aaa.bbb.ccc.ddd\0 */
198152592Sandre					/*
199169464Srwatson					 * Acting as a router, so generate
200169464Srwatson					 * ICMP
201152592Sandre					 */
202152592Sandrenosourcerouting:
203152592Sandre					strcpy(buf, inet_ntoa(ip->ip_dst));
204152592Sandre					log(LOG_WARNING,
205152592Sandre					    "attempted source route from %s to %s\n",
206152592Sandre					    inet_ntoa(ip->ip_src), buf);
207152592Sandre					type = ICMP_UNREACH;
208152592Sandre					code = ICMP_UNREACH_SRCFAIL;
209152592Sandre					goto bad;
210152592Sandre				} else {
211152592Sandre					/*
212169464Srwatson					 * Not acting as a router, so
213169464Srwatson					 * silently drop.
214152592Sandre					 */
215152592Sandre#ifdef IPSTEALTH
216152592Sandredropit:
217152592Sandre#endif
218152592Sandre					ipstat.ips_cantforward++;
219152592Sandre					m_freem(m);
220152592Sandre					return (1);
221152592Sandre				}
222152592Sandre			}
223152592Sandre
224152592Sandre			/*
225152592Sandre			 * locate outgoing interface
226152592Sandre			 */
227152592Sandre			(void)memcpy(&ipaddr.sin_addr, cp + off,
228152592Sandre			    sizeof(ipaddr.sin_addr));
229152592Sandre
230152592Sandre			if (opt == IPOPT_SSRR) {
231152592Sandre#define	INA	struct in_ifaddr *
232152592Sandre#define	SA	struct sockaddr *
233152592Sandre			    if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == NULL)
234152592Sandre				ia = (INA)ifa_ifwithnet((SA)&ipaddr);
235152592Sandre			} else
236152592Sandre				ia = ip_rtaddr(ipaddr.sin_addr);
237152592Sandre			if (ia == NULL) {
238152592Sandre				type = ICMP_UNREACH;
239152592Sandre				code = ICMP_UNREACH_SRCFAIL;
240152592Sandre				goto bad;
241152592Sandre			}
242152592Sandre			ip->ip_dst = ipaddr.sin_addr;
243152592Sandre			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
244152592Sandre			    sizeof(struct in_addr));
245152592Sandre			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
246152592Sandre			/*
247152592Sandre			 * Let ip_intr's mcast routing check handle mcast pkts
248152592Sandre			 */
249152592Sandre			forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
250152592Sandre			break;
251152592Sandre
252152592Sandre		case IPOPT_RR:
253152592Sandre#ifdef IPSTEALTH
254152592Sandre			if (ipstealth && pass == 0)
255152592Sandre				break;
256152592Sandre#endif
257152592Sandre			if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
258152592Sandre				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
259152592Sandre				goto bad;
260152592Sandre			}
261152592Sandre			if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
262152592Sandre				code = &cp[IPOPT_OFFSET] - (u_char *)ip;
263152592Sandre				goto bad;
264152592Sandre			}
265152592Sandre			/*
266152592Sandre			 * If no space remains, ignore.
267152592Sandre			 */
268152592Sandre			off--;			/* 0 origin */
269152592Sandre			if (off > optlen - (int)sizeof(struct in_addr))
270152592Sandre				break;
271152592Sandre			(void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
272152592Sandre			    sizeof(ipaddr.sin_addr));
273152592Sandre			/*
274169464Srwatson			 * Locate outgoing interface; if we're the
275169464Srwatson			 * destination, use the incoming interface (should be
276169464Srwatson			 * same).
277152592Sandre			 */
278152592Sandre			if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == NULL &&
279152592Sandre			    (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) {
280152592Sandre				type = ICMP_UNREACH;
281152592Sandre				code = ICMP_UNREACH_HOST;
282152592Sandre				goto bad;
283152592Sandre			}
284152592Sandre			(void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
285152592Sandre			    sizeof(struct in_addr));
286152592Sandre			cp[IPOPT_OFFSET] += sizeof(struct in_addr);
287152592Sandre			break;
288152592Sandre
289152592Sandre		case IPOPT_TS:
290152592Sandre#ifdef IPSTEALTH
291152592Sandre			if (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:
319152592Sandre				if (off + sizeof(n_time) +
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));
331152592Sandre				cp[IPOPT_OFFSET] += sizeof(struct in_addr);
332152592Sandre				off += sizeof(struct in_addr);
333152592Sandre				break;
334152592Sandre
335152592Sandre			case IPOPT_TS_PRESPEC:
336152592Sandre				if (off + sizeof(n_time) +
337152592Sandre				    sizeof(struct in_addr) > optlen) {
338152592Sandre					code = &cp[IPOPT_OFFSET] - (u_char *)ip;
339152592Sandre					goto bad;
340152592Sandre				}
341152592Sandre				(void)memcpy(&ipaddr.sin_addr, sin,
342152592Sandre				    sizeof(struct in_addr));
343152592Sandre				if (ifa_ifwithaddr((SA)&ipaddr) == NULL)
344152592Sandre					continue;
345152592Sandre				cp[IPOPT_OFFSET] += sizeof(struct in_addr);
346152592Sandre				off += sizeof(struct in_addr);
347152592Sandre				break;
348152592Sandre
349152592Sandre			default:
350152592Sandre				code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip;
351152592Sandre				goto bad;
352152592Sandre			}
353152592Sandre			ntime = iptime();
354152592Sandre			(void)memcpy(cp + off, &ntime, sizeof(n_time));
355152592Sandre			cp[IPOPT_OFFSET] += sizeof(n_time);
356152592Sandre		}
357152592Sandre	}
358152592Sandre	if (forward && ipforwarding) {
359152592Sandre		ip_forward(m, 1);
360152592Sandre		return (1);
361152592Sandre	}
362152592Sandre	return (0);
363152592Sandrebad:
364152592Sandre	icmp_error(m, type, code, 0, 0);
365152592Sandre	ipstat.ips_badoptions++;
366152592Sandre	return (1);
367152592Sandre}
368152592Sandre
369152592Sandre/*
370169464Srwatson * Save incoming source route for use in replies, to be picked up later by
371169464Srwatson * ip_srcroute if the receiver is interested.
372152592Sandre */
373152592Sandrestatic void
374169454Srwatsonsave_rte(struct mbuf *m, u_char *option, struct in_addr dst)
375152592Sandre{
376152592Sandre	unsigned olen;
377152592Sandre	struct ipopt_tag *opts;
378152592Sandre
379152592Sandre	opts = (struct ipopt_tag *)m_tag_get(PACKET_TAG_IPOPTIONS,
380169464Srwatson	    sizeof(struct ipopt_tag), M_NOWAIT);
381152592Sandre	if (opts == NULL)
382152592Sandre		return;
383152592Sandre
384152592Sandre	olen = option[IPOPT_OLEN];
385152592Sandre	if (olen > sizeof(opts->ip_srcrt) - (1 + sizeof(dst))) {
386152592Sandre		m_tag_free((struct m_tag *)opts);
387152592Sandre		return;
388152592Sandre	}
389152592Sandre	bcopy(option, opts->ip_srcrt.srcopt, olen);
390152592Sandre	opts->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
391152592Sandre	opts->ip_srcrt.dst = dst;
392152592Sandre	m_tag_prepend(m, (struct m_tag *)opts);
393152592Sandre}
394152592Sandre
395152592Sandre/*
396169464Srwatson * Retrieve incoming source route for use in replies, in the same form used
397169464Srwatson * by setsockopt.  The first hop is placed before the options, will be
398169464Srwatson * removed later.
399152592Sandre */
400152592Sandrestruct mbuf *
401169454Srwatsonip_srcroute(struct mbuf *m0)
402152592Sandre{
403169464Srwatson	struct in_addr *p, *q;
404169464Srwatson	struct mbuf *m;
405152592Sandre	struct ipopt_tag *opts;
406152592Sandre
407152592Sandre	opts = (struct ipopt_tag *)m_tag_find(m0, PACKET_TAG_IPOPTIONS, NULL);
408152592Sandre	if (opts == NULL)
409152592Sandre		return (NULL);
410152592Sandre
411152592Sandre	if (opts->ip_nhops == 0)
412152592Sandre		return (NULL);
413152592Sandre	m = m_get(M_DONTWAIT, MT_DATA);
414152592Sandre	if (m == NULL)
415152592Sandre		return (NULL);
416152592Sandre
417152592Sandre#define OPTSIZ	(sizeof(opts->ip_srcrt.nop) + sizeof(opts->ip_srcrt.srcopt))
418152592Sandre
419152592Sandre	/* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
420152592Sandre	m->m_len = opts->ip_nhops * sizeof(struct in_addr) +
421152592Sandre	    sizeof(struct in_addr) + OPTSIZ;
422152592Sandre
423152592Sandre	/*
424169464Srwatson	 * First, save first hop for return route.
425152592Sandre	 */
426152592Sandre	p = &(opts->ip_srcrt.route[opts->ip_nhops - 1]);
427152592Sandre	*(mtod(m, struct in_addr *)) = *p--;
428152592Sandre
429152592Sandre	/*
430152592Sandre	 * Copy option fields and padding (nop) to mbuf.
431152592Sandre	 */
432152592Sandre	opts->ip_srcrt.nop = IPOPT_NOP;
433152592Sandre	opts->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
434152592Sandre	(void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
435152592Sandre	    &(opts->ip_srcrt.nop), OPTSIZ);
436152592Sandre	q = (struct in_addr *)(mtod(m, caddr_t) +
437152592Sandre	    sizeof(struct in_addr) + OPTSIZ);
438152592Sandre#undef OPTSIZ
439152592Sandre	/*
440169464Srwatson	 * Record return path as an IP source route, reversing the path
441169464Srwatson	 * (pointers are now aligned).
442152592Sandre	 */
443152592Sandre	while (p >= opts->ip_srcrt.route) {
444152592Sandre		*q++ = *p--;
445152592Sandre	}
446152592Sandre	/*
447152592Sandre	 * Last hop goes to final destination.
448152592Sandre	 */
449152592Sandre	*q = opts->ip_srcrt.dst;
450152592Sandre	m_tag_delete(m0, (struct m_tag *)opts);
451152592Sandre	return (m);
452152592Sandre}
453152592Sandre
454152592Sandre/*
455169464Srwatson * Strip out IP options, at higher level protocol in the kernel.  Second
456169464Srwatson * argument is buffer to which options will be moved, and return value is
457169464Srwatson * their length.
458169464Srwatson *
459152592Sandre * XXX should be deleted; last arg currently ignored.
460152592Sandre */
461152592Sandrevoid
462169454Srwatsonip_stripoptions(struct mbuf *m, struct mbuf *mopt)
463152592Sandre{
464169464Srwatson	int i;
465152592Sandre	struct ip *ip = mtod(m, struct ip *);
466169464Srwatson	caddr_t opts;
467152592Sandre	int olen;
468152592Sandre
469152592Sandre	olen = (ip->ip_hl << 2) - sizeof (struct ip);
470152592Sandre	opts = (caddr_t)(ip + 1);
471152592Sandre	i = m->m_len - (sizeof (struct ip) + olen);
472152592Sandre	bcopy(opts + olen, opts, (unsigned)i);
473152592Sandre	m->m_len -= olen;
474152592Sandre	if (m->m_flags & M_PKTHDR)
475152592Sandre		m->m_pkthdr.len -= olen;
476152592Sandre	ip->ip_v = IPVERSION;
477152592Sandre	ip->ip_hl = sizeof(struct ip) >> 2;
478152592Sandre}
479152592Sandre
480152592Sandre/*
481169464Srwatson * Insert IP options into preformed packet.  Adjust IP destination as
482169464Srwatson * required for IP source routing, as indicated by a non-zero in_addr at the
483169464Srwatson * start of the options.
484152592Sandre *
485152592Sandre * XXX This routine assumes that the packet has no options in place.
486152592Sandre */
487152592Sandrestruct mbuf *
488169454Srwatsonip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
489152592Sandre{
490169464Srwatson	struct ipoption *p = mtod(opt, struct ipoption *);
491152592Sandre	struct mbuf *n;
492169464Srwatson	struct ip *ip = mtod(m, struct ip *);
493152592Sandre	unsigned optlen;
494152592Sandre
495152592Sandre	optlen = opt->m_len - sizeof(p->ipopt_dst);
496152592Sandre	if (optlen + ip->ip_len > IP_MAXPACKET) {
497152592Sandre		*phlen = 0;
498152592Sandre		return (m);		/* XXX should fail */
499152592Sandre	}
500152592Sandre	if (p->ipopt_dst.s_addr)
501152592Sandre		ip->ip_dst = p->ipopt_dst;
502152592Sandre	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
503152592Sandre		MGETHDR(n, M_DONTWAIT, MT_DATA);
504152592Sandre		if (n == NULL) {
505152592Sandre			*phlen = 0;
506152592Sandre			return (m);
507152592Sandre		}
508152592Sandre		M_MOVE_PKTHDR(n, m);
509152592Sandre		n->m_pkthdr.rcvif = NULL;
510152592Sandre#ifdef MAC
511172930Srwatson		mac_mbuf_copy(m, n);
512152592Sandre#endif
513152592Sandre		n->m_pkthdr.len += optlen;
514152592Sandre		m->m_len -= sizeof(struct ip);
515152592Sandre		m->m_data += sizeof(struct ip);
516152592Sandre		n->m_next = m;
517152592Sandre		m = n;
518152592Sandre		m->m_len = optlen + sizeof(struct ip);
519152592Sandre		m->m_data += max_linkhdr;
520152592Sandre		bcopy(ip, mtod(m, void *), sizeof(struct ip));
521152592Sandre	} else {
522152592Sandre		m->m_data -= optlen;
523152592Sandre		m->m_len += optlen;
524152592Sandre		m->m_pkthdr.len += optlen;
525152592Sandre		bcopy(ip, mtod(m, void *), sizeof(struct ip));
526152592Sandre	}
527152592Sandre	ip = mtod(m, struct ip *);
528152592Sandre	bcopy(p->ipopt_list, ip + 1, optlen);
529152592Sandre	*phlen = sizeof(struct ip) + optlen;
530152592Sandre	ip->ip_v = IPVERSION;
531152592Sandre	ip->ip_hl = *phlen >> 2;
532152592Sandre	ip->ip_len += optlen;
533152592Sandre	return (m);
534152592Sandre}
535152592Sandre
536152592Sandre/*
537169464Srwatson * Copy options from ip to jp, omitting those not copied during
538169464Srwatson * fragmentation.
539152592Sandre */
540152592Sandreint
541169454Srwatsonip_optcopy(struct ip *ip, struct ip *jp)
542152592Sandre{
543169464Srwatson	u_char *cp, *dp;
544152592Sandre	int opt, optlen, cnt;
545152592Sandre
546152592Sandre	cp = (u_char *)(ip + 1);
547152592Sandre	dp = (u_char *)(jp + 1);
548152592Sandre	cnt = (ip->ip_hl << 2) - sizeof (struct ip);
549152592Sandre	for (; cnt > 0; cnt -= optlen, cp += optlen) {
550152592Sandre		opt = cp[0];
551152592Sandre		if (opt == IPOPT_EOL)
552152592Sandre			break;
553152592Sandre		if (opt == IPOPT_NOP) {
554152592Sandre			/* Preserve for IP mcast tunnel's LSRR alignment. */
555152592Sandre			*dp++ = IPOPT_NOP;
556152592Sandre			optlen = 1;
557152592Sandre			continue;
558152592Sandre		}
559152592Sandre
560152592Sandre		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
561152592Sandre		    ("ip_optcopy: malformed ipv4 option"));
562152592Sandre		optlen = cp[IPOPT_OLEN];
563152592Sandre		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
564152592Sandre		    ("ip_optcopy: malformed ipv4 option"));
565152592Sandre
566169464Srwatson		/* Bogus lengths should have been caught by ip_dooptions. */
567152592Sandre		if (optlen > cnt)
568152592Sandre			optlen = cnt;
569152592Sandre		if (IPOPT_COPIED(opt)) {
570152592Sandre			bcopy(cp, dp, optlen);
571152592Sandre			dp += optlen;
572152592Sandre		}
573152592Sandre	}
574152592Sandre	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
575152592Sandre		*dp++ = IPOPT_EOL;
576152592Sandre	return (optlen);
577152592Sandre}
578152592Sandre
579152592Sandre/*
580169464Srwatson * Set up IP options in pcb for insertion in output packets.  Store in mbuf
581169464Srwatson * with pointer in pcbopt, adding pseudo-option with destination address if
582169464Srwatson * source routed.
583152592Sandre */
584152592Sandreint
585152592Sandreip_pcbopts(struct inpcb *inp, int optname, struct mbuf *m)
586152592Sandre{
587169464Srwatson	int cnt, optlen;
588169464Srwatson	u_char *cp;
589152592Sandre	struct mbuf **pcbopt;
590152592Sandre	u_char opt;
591152592Sandre
592152592Sandre	INP_LOCK_ASSERT(inp);
593152592Sandre
594152592Sandre	pcbopt = &inp->inp_options;
595152592Sandre
596152592Sandre	/* turn off any old options */
597152592Sandre	if (*pcbopt)
598152592Sandre		(void)m_free(*pcbopt);
599152592Sandre	*pcbopt = 0;
600152592Sandre	if (m == NULL || m->m_len == 0) {
601152592Sandre		/*
602152592Sandre		 * Only turning off any previous options.
603152592Sandre		 */
604152592Sandre		if (m != NULL)
605152592Sandre			(void)m_free(m);
606152592Sandre		return (0);
607152592Sandre	}
608152592Sandre
609152592Sandre	if (m->m_len % sizeof(int32_t))
610152592Sandre		goto bad;
611152592Sandre	/*
612169464Srwatson	 * IP first-hop destination address will be stored before actual
613169464Srwatson	 * options; move other options back and clear it when none present.
614152592Sandre	 */
615152592Sandre	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
616152592Sandre		goto bad;
617152592Sandre	cnt = m->m_len;
618152592Sandre	m->m_len += sizeof(struct in_addr);
619152592Sandre	cp = mtod(m, u_char *) + sizeof(struct in_addr);
620152592Sandre	bcopy(mtod(m, void *), cp, (unsigned)cnt);
621152592Sandre	bzero(mtod(m, void *), sizeof(struct in_addr));
622152592Sandre
623152592Sandre	for (; cnt > 0; cnt -= optlen, cp += optlen) {
624152592Sandre		opt = cp[IPOPT_OPTVAL];
625152592Sandre		if (opt == IPOPT_EOL)
626152592Sandre			break;
627152592Sandre		if (opt == IPOPT_NOP)
628152592Sandre			optlen = 1;
629152592Sandre		else {
630152592Sandre			if (cnt < IPOPT_OLEN + sizeof(*cp))
631152592Sandre				goto bad;
632152592Sandre			optlen = cp[IPOPT_OLEN];
633152592Sandre			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
634152592Sandre				goto bad;
635152592Sandre		}
636152592Sandre		switch (opt) {
637152592Sandre
638152592Sandre		default:
639152592Sandre			break;
640152592Sandre
641152592Sandre		case IPOPT_LSRR:
642152592Sandre		case IPOPT_SSRR:
643152592Sandre			/*
644169464Srwatson			 * User process specifies route as:
645169464Srwatson			 *
646152592Sandre			 *	->A->B->C->D
647169464Srwatson			 *
648152592Sandre			 * D must be our final destination (but we can't
649152592Sandre			 * check that since we may not have connected yet).
650169464Srwatson			 * A is first hop destination, which doesn't appear
651169464Srwatson			 * in actual IP option, but is stored before the
652169464Srwatson			 * options.
653152592Sandre			 */
654175630Sbz			/* XXX-BZ PRIV_NETINET_SETHDROPTS? */
655152592Sandre			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
656152592Sandre				goto bad;
657152592Sandre			m->m_len -= sizeof(struct in_addr);
658152592Sandre			cnt -= sizeof(struct in_addr);
659152592Sandre			optlen -= sizeof(struct in_addr);
660152592Sandre			cp[IPOPT_OLEN] = optlen;
661152592Sandre			/*
662152592Sandre			 * Move first hop before start of options.
663152592Sandre			 */
664152592Sandre			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
665152592Sandre			    sizeof(struct in_addr));
666152592Sandre			/*
667152592Sandre			 * Then copy rest of options back
668152592Sandre			 * to close up the deleted entry.
669152592Sandre			 */
670152592Sandre			bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
671152592Sandre			    &cp[IPOPT_OFFSET+1],
672152592Sandre			    (unsigned)cnt - (IPOPT_MINOFF - 1));
673152592Sandre			break;
674152592Sandre		}
675152592Sandre	}
676152592Sandre	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
677152592Sandre		goto bad;
678152592Sandre	*pcbopt = m;
679152592Sandre	return (0);
680152592Sandre
681152592Sandrebad:
682152592Sandre	(void)m_free(m);
683152592Sandre	return (EINVAL);
684152592Sandre}
685