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