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