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