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