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