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