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