1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 1988, 1993
5 * The Regents of the University of California.
6 * Copyright (c) 2005 Andre Oppermann, Internet Business Solutions AG.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include "opt_ipstealth.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/mbuf.h>
39 #include <sys/domain.h>
40 #include <sys/protosw.h>
41 #include <sys/socket.h>
42 #include <sys/time.h>
43 #include <sys/kernel.h>
44 #include <sys/syslog.h>
45 #include <sys/sysctl.h>
46
47 #include <net/if.h>
48 #include <net/if_types.h>
49 #include <net/if_var.h>
50 #include <net/if_dl.h>
51 #include <net/route.h>
52 #include <net/route/nhop.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 VNET_DEFINE_STATIC(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 VNET_DEFINE_STATIC(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
ip_dooptions(struct mbuf * m,int pass)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 nhop_object *nh;
109 struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
110
111 NET_EPOCH_ASSERT();
112
113 /* Ignore or reject packets with IP options. */
114 if (V_ip_doopts == 0)
115 return 0;
116 else if (V_ip_doopts == 2) {
117 type = ICMP_UNREACH;
118 code = ICMP_UNREACH_FILTER_PROHIB;
119 goto bad;
120 }
121
122 dst = ip->ip_dst;
123 cp = (u_char *)(ip + 1);
124 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
125 for (; cnt > 0; cnt -= optlen, cp += optlen) {
126 opt = cp[IPOPT_OPTVAL];
127 if (opt == IPOPT_EOL)
128 break;
129 if (opt == IPOPT_NOP)
130 optlen = 1;
131 else {
132 if (cnt < IPOPT_OLEN + sizeof(*cp)) {
133 code = &cp[IPOPT_OLEN] - (u_char *)ip;
134 goto bad;
135 }
136 optlen = cp[IPOPT_OLEN];
137 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
138 code = &cp[IPOPT_OLEN] - (u_char *)ip;
139 goto bad;
140 }
141 }
142 switch (opt) {
143 default:
144 break;
145
146 /*
147 * Source routing with record. Find interface with current
148 * destination address. If none on this machine then drop if
149 * strictly routed, or do nothing if loosely routed. Record
150 * interface address and bring up next address component. If
151 * strictly routed make sure next address is on directly
152 * accessible net.
153 */
154 case IPOPT_LSRR:
155 case IPOPT_SSRR:
156 #ifdef IPSTEALTH
157 if (V_ipstealth && pass > 0)
158 break;
159 #endif
160 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
161 code = &cp[IPOPT_OLEN] - (u_char *)ip;
162 goto bad;
163 }
164 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
165 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
166 goto bad;
167 }
168 ipaddr.sin_addr = ip->ip_dst;
169 if (ifa_ifwithaddr_check((struct sockaddr *)&ipaddr)
170 == 0) {
171 if (opt == IPOPT_SSRR) {
172 type = ICMP_UNREACH;
173 code = ICMP_UNREACH_SRCFAIL;
174 goto bad;
175 }
176 if (!V_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 (!V_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 (!V_ip_dosourceroute) {
199 if (V_ipforwarding) {
200 char srcbuf[INET_ADDRSTRLEN];
201 char dstbuf[INET_ADDRSTRLEN];
202
203 /*
204 * Acting as a router, so generate
205 * ICMP
206 */
207 nosourcerouting:
208 log(LOG_WARNING,
209 "attempted source route from %s "
210 "to %s\n",
211 inet_ntoa_r(ip->ip_src, srcbuf),
212 inet_ntoa_r(ip->ip_dst, dstbuf));
213 type = ICMP_UNREACH;
214 code = ICMP_UNREACH_SRCFAIL;
215 goto bad;
216 } else {
217 /*
218 * Not acting as a router, so
219 * silently drop.
220 */
221 #ifdef IPSTEALTH
222 dropit:
223 #endif
224 IPSTAT_INC(ips_cantforward);
225 m_freem(m);
226 return (1);
227 }
228 }
229
230 /*
231 * locate outgoing interface
232 */
233 (void)memcpy(&ipaddr.sin_addr, cp + off,
234 sizeof(ipaddr.sin_addr));
235
236 type = ICMP_UNREACH;
237 code = ICMP_UNREACH_SRCFAIL;
238
239 if (opt == IPOPT_SSRR) {
240 #define INA struct in_ifaddr *
241 #define SA struct sockaddr *
242 ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr,
243 RT_ALL_FIBS);
244 if (ia == NULL)
245 ia = (INA)ifa_ifwithnet((SA)&ipaddr, 0,
246 RT_ALL_FIBS);
247 if (ia == NULL)
248 goto bad;
249
250 memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
251 sizeof(struct in_addr));
252 } else {
253 /* XXX MRT 0 for routing */
254 nh = fib4_lookup(M_GETFIB(m), ipaddr.sin_addr,
255 0, NHR_NONE, 0);
256 if (nh == NULL)
257 goto bad;
258
259 memcpy(cp + off, &(IA_SIN(nh->nh_ifa)->sin_addr),
260 sizeof(struct in_addr));
261 }
262
263 ip->ip_dst = ipaddr.sin_addr;
264 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
265 /*
266 * Let ip_intr's mcast routing check handle mcast pkts
267 */
268 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
269 break;
270
271 case IPOPT_RR:
272 #ifdef IPSTEALTH
273 if (V_ipstealth && pass == 0)
274 break;
275 #endif
276 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
277 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
278 goto bad;
279 }
280 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
281 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
282 goto bad;
283 }
284 /*
285 * If no space remains, ignore.
286 */
287 off--; /* 0 origin */
288 if (off > optlen - (int)sizeof(struct in_addr))
289 break;
290 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
291 sizeof(ipaddr.sin_addr));
292 /*
293 * Locate outgoing interface; if we're the
294 * destination, use the incoming interface (should be
295 * same).
296 */
297 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) != NULL) {
298 memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
299 sizeof(struct in_addr));
300 } else if ((nh = fib4_lookup(M_GETFIB(m),
301 ipaddr.sin_addr, 0, NHR_NONE, 0)) != NULL) {
302 memcpy(cp + off, &(IA_SIN(nh->nh_ifa)->sin_addr),
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 case IPOPT_TS_TSONLY:
338 break;
339
340 case IPOPT_TS_TSANDADDR:
341 if (off + sizeof(uint32_t) +
342 sizeof(struct in_addr) > optlen) {
343 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
344 goto bad;
345 }
346 ipaddr.sin_addr = dst;
347 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
348 m->m_pkthdr.rcvif);
349 if (ia == NULL)
350 continue;
351 (void)memcpy(sin, &IA_SIN(ia)->sin_addr,
352 sizeof(struct in_addr));
353 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
354 off += sizeof(struct in_addr);
355 break;
356
357 case IPOPT_TS_PRESPEC:
358 if (off + sizeof(uint32_t) +
359 sizeof(struct in_addr) > optlen) {
360 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
361 goto bad;
362 }
363 (void)memcpy(&ipaddr.sin_addr, sin,
364 sizeof(struct in_addr));
365 if (ifa_ifwithaddr_check((SA)&ipaddr) == 0)
366 continue;
367 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
368 off += sizeof(struct in_addr);
369 break;
370
371 default:
372 code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip;
373 goto bad;
374 }
375 ntime = iptime();
376 (void)memcpy(cp + off, &ntime, sizeof(uint32_t));
377 cp[IPOPT_OFFSET] += sizeof(uint32_t);
378 }
379 }
380 if (forward && V_ipforwarding) {
381 ip_forward(m, 1);
382 return (1);
383 }
384 return (0);
385 bad:
386 icmp_error(m, type, code, 0, 0);
387 IPSTAT_INC(ips_badoptions);
388 return (1);
389 }
390
391 /*
392 * Save incoming source route for use in replies, to be picked up later by
393 * ip_srcroute if the receiver is interested.
394 */
395 static void
save_rte(struct mbuf * m,u_char * option,struct in_addr dst)396 save_rte(struct mbuf *m, u_char *option, struct in_addr dst)
397 {
398 unsigned olen;
399 struct ipopt_tag *opts;
400
401 opts = (struct ipopt_tag *)m_tag_get(PACKET_TAG_IPOPTIONS,
402 sizeof(struct ipopt_tag), M_NOWAIT);
403 if (opts == NULL)
404 return;
405
406 olen = option[IPOPT_OLEN];
407 if (olen > sizeof(opts->ip_srcrt) - (1 + sizeof(dst))) {
408 m_tag_free((struct m_tag *)opts);
409 return;
410 }
411 bcopy(option, opts->ip_srcrt.srcopt, olen);
412 opts->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
413 opts->ip_srcrt.dst = dst;
414 m_tag_prepend(m, (struct m_tag *)opts);
415 }
416
417 /*
418 * Retrieve incoming source route for use in replies, in the same form used
419 * by setsockopt. The first hop is placed before the options, will be
420 * removed later.
421 */
422 struct mbuf *
ip_srcroute(struct mbuf * m0)423 ip_srcroute(struct mbuf *m0)
424 {
425 struct in_addr *p, *q;
426 struct mbuf *m;
427 struct ipopt_tag *opts;
428
429 opts = (struct ipopt_tag *)m_tag_find(m0, PACKET_TAG_IPOPTIONS, NULL);
430 if (opts == NULL)
431 return (NULL);
432
433 if (opts->ip_nhops == 0)
434 return (NULL);
435 m = m_get(M_NOWAIT, MT_DATA);
436 if (m == NULL)
437 return (NULL);
438
439 #define OPTSIZ (sizeof(opts->ip_srcrt.nop) + sizeof(opts->ip_srcrt.srcopt))
440
441 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
442 m->m_len = opts->ip_nhops * sizeof(struct in_addr) +
443 sizeof(struct in_addr) + OPTSIZ;
444
445 /*
446 * First, save first hop for return route.
447 */
448 p = &(opts->ip_srcrt.route[opts->ip_nhops - 1]);
449 *(mtod(m, struct in_addr *)) = *p--;
450
451 /*
452 * Copy option fields and padding (nop) to mbuf.
453 */
454 opts->ip_srcrt.nop = IPOPT_NOP;
455 opts->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
456 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
457 &(opts->ip_srcrt.nop), OPTSIZ);
458 q = (struct in_addr *)(mtod(m, caddr_t) +
459 sizeof(struct in_addr) + OPTSIZ);
460 #undef OPTSIZ
461 /*
462 * Record return path as an IP source route, reversing the path
463 * (pointers are now aligned).
464 */
465 while (p >= opts->ip_srcrt.route) {
466 *q++ = *p--;
467 }
468 /*
469 * Last hop goes to final destination.
470 */
471 *q = opts->ip_srcrt.dst;
472 m_tag_delete(m0, (struct m_tag *)opts);
473 return (m);
474 }
475
476 /*
477 * Strip out IP options, at higher level protocol in the kernel.
478 */
479 void
ip_stripoptions(struct mbuf * m)480 ip_stripoptions(struct mbuf *m)
481 {
482 struct ip *ip = mtod(m, struct ip *);
483 int olen;
484
485 olen = (ip->ip_hl << 2) - sizeof(struct ip);
486 m->m_len -= olen;
487 if (m->m_flags & M_PKTHDR)
488 m->m_pkthdr.len -= olen;
489 ip->ip_len = htons(ntohs(ip->ip_len) - olen);
490 ip->ip_hl = sizeof(struct ip) >> 2;
491
492 bcopy((char *)ip + sizeof(struct ip) + olen, (ip + 1),
493 (size_t )(m->m_len - sizeof(struct ip)));
494 }
495
496 /*
497 * Insert IP options into preformed packet. Adjust IP destination as
498 * required for IP source routing, as indicated by a non-zero in_addr at the
499 * start of the options.
500 *
501 * XXX This routine assumes that the packet has no options in place.
502 */
503 struct mbuf *
ip_insertoptions(struct mbuf * m,struct mbuf * opt,int * phlen)504 ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
505 {
506 struct ipoption *p = mtod(opt, struct ipoption *);
507 struct mbuf *n;
508 struct ip *ip = mtod(m, struct ip *);
509 unsigned optlen;
510
511 optlen = opt->m_len - sizeof(p->ipopt_dst);
512 if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET) {
513 *phlen = 0;
514 return (m); /* XXX should fail */
515 }
516 KASSERT((m->m_flags & M_EXTPG) == 0, ("%s: mbuf %p is unmapped",
517 __func__, m));
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
ip_optcopy(struct ip * ip,struct ip * jp)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
ip_pcbopts(struct inpcb * inp,int optname,struct mbuf * m)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 default:
653 break;
654
655 case IPOPT_LSRR:
656 case IPOPT_SSRR:
657 /*
658 * User process specifies route as:
659 *
660 * ->A->B->C->D
661 *
662 * D must be our final destination (but we can't
663 * check that since we may not have connected yet).
664 * A is first hop destination, which doesn't appear
665 * in actual IP option, but is stored before the
666 * options.
667 */
668 /* XXX-BZ PRIV_NETINET_SETHDROPTS? */
669 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
670 goto bad;
671 m->m_len -= sizeof(struct in_addr);
672 cnt -= sizeof(struct in_addr);
673 optlen -= sizeof(struct in_addr);
674 cp[IPOPT_OLEN] = optlen;
675 /*
676 * Move first hop before start of options.
677 */
678 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
679 sizeof(struct in_addr));
680 /*
681 * Then copy rest of options back
682 * to close up the deleted entry.
683 */
684 bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)),
685 &cp[IPOPT_OFFSET+1],
686 (unsigned)cnt - (IPOPT_MINOFF - 1));
687 break;
688 }
689 }
690 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
691 goto bad;
692 *pcbopt = m;
693 return (0);
694
695 bad:
696 (void)m_free(m);
697 return (EINVAL);
698 }
699
700 /*
701 * Check for the presence of the IP Router Alert option [RFC2113]
702 * in the header of an IPv4 datagram.
703 *
704 * This call is not intended for use from the forwarding path; it is here
705 * so that protocol domains may check for the presence of the option.
706 * Given how FreeBSD's IPv4 stack is currently structured, the Router Alert
707 * option does not have much relevance to the implementation, though this
708 * may change in future.
709 * Router alert options SHOULD be passed if running in IPSTEALTH mode and
710 * we are not the endpoint.
711 * Length checks on individual options should already have been performed
712 * by ip_dooptions() therefore they are folded under INVARIANTS here.
713 *
714 * Return zero if not present or options are invalid, non-zero if present.
715 */
716 int
ip_checkrouteralert(struct mbuf * m)717 ip_checkrouteralert(struct mbuf *m)
718 {
719 struct ip *ip = mtod(m, struct ip *);
720 u_char *cp;
721 int opt, optlen, cnt, found_ra;
722
723 found_ra = 0;
724 cp = (u_char *)(ip + 1);
725 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
726 for (; cnt > 0; cnt -= optlen, cp += optlen) {
727 opt = cp[IPOPT_OPTVAL];
728 if (opt == IPOPT_EOL)
729 break;
730 if (opt == IPOPT_NOP)
731 optlen = 1;
732 else {
733 #ifdef INVARIANTS
734 if (cnt < IPOPT_OLEN + sizeof(*cp))
735 break;
736 #endif
737 optlen = cp[IPOPT_OLEN];
738 #ifdef INVARIANTS
739 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
740 break;
741 #endif
742 }
743 switch (opt) {
744 case IPOPT_RA:
745 #ifdef INVARIANTS
746 if (optlen != IPOPT_OFFSET + sizeof(uint16_t) ||
747 (*((uint16_t *)&cp[IPOPT_OFFSET]) != 0))
748 break;
749 else
750 #endif
751 found_ra = 1;
752 break;
753 default:
754 break;
755 }
756 }
757
758 return (found_ra);
759 }
760