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