]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/bsd_tcpip/v2_0/src/sys/netinet/ip_input.c
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / packages / net / bsd_tcpip / v2_0 / src / sys / netinet / ip_input.c
1 //==========================================================================
2 //
3 //      src/sys/netinet/ip_input.c
4 //
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
7 //
8 // -------------------------------------------
9 //
10 // Portions of this software may have been derived from OpenBSD, 
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
13 //
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
16 //
17 // -------------------------------------------
18 //
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
21
22 /*
23  * Copyright (c) 1982, 1986, 1988, 1993
24  *      The Regents of the University of California.  All rights reserved.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions
28  * are met:
29  * 1. Redistributions of source code must retain the above copyright
30  *    notice, this list of conditions and the following disclaimer.
31  * 2. Redistributions in binary form must reproduce the above copyright
32  *    notice, this list of conditions and the following disclaimer in the
33  *    documentation and/or other materials provided with the distribution.
34  * 3. All advertising materials mentioning features or use of this software
35  *    must display the following acknowledgement:
36  *      This product includes software developed by the University of
37  *      California, Berkeley and its contributors.
38  * 4. Neither the name of the University nor the names of its contributors
39  *    may be used to endorse or promote products derived from this software
40  *    without specific prior written permission.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  *
54  *      @(#)ip_input.c  8.2 (Berkeley) 1/4/94
55  * $FreeBSD: src/sys/netinet/ip_input.c,v 1.130.2.25 2001/08/29 21:41:37 jesper Exp $
56  */
57
58 #define _IP_VHL
59
60 #include <sys/param.h>
61 #include <sys/mbuf.h>
62 #include <sys/malloc.h>
63 #include <sys/domain.h>
64 #include <sys/protosw.h>
65 #include <sys/socket.h>
66 #include <sys/sysctl.h>
67
68 #include <net/if.h>
69 #include <net/if_var.h>
70 #include <net/if_dl.h>
71 #include <net/route.h>
72 #include <net/netisr.h>
73 #include <net/intrq.h>
74
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/in_var.h>
78 #include <netinet/ip.h>
79 #include <netinet/in_pcb.h>
80 #include <netinet/ip_var.h>
81 #include <netinet/ip_icmp.h>
82
83 #include <sys/socketvar.h>
84
85 #include <netinet/ip_fw.h>
86
87 #ifdef IPSEC
88 #include <netinet6/ipsec.h>
89 #include <netkey/key.h>
90 #endif
91
92 #ifdef DUMMYNET
93 #include <netinet/ip_dummynet.h>
94 #endif
95
96 int rsvp_on = 0;
97 static int ip_rsvp_on;
98 struct socket *ip_rsvpd;
99
100 int     ipforwarding = 0;
101 SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
102     &ipforwarding, 0, "Enable IP forwarding between interfaces");
103
104 static int      ipsendredirects = 1; /* XXX */
105 SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
106     &ipsendredirects, 0, "Enable sending IP redirects");
107
108 int     ip_defttl = IPDEFTTL;
109 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
110     &ip_defttl, 0, "Maximum TTL on IP packets");
111
112 static int      ip_dosourceroute = 0;
113 SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
114     &ip_dosourceroute, 0, "Enable forwarding source routed IP packets");
115
116 static int      ip_acceptsourceroute = 0;
117 SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute, 
118     CTLFLAG_RW, &ip_acceptsourceroute, 0, 
119     "Enable accepting source routed IP packets");
120
121 #if defined(NFAITH) && 0 < NFAITH || defined(CYGPKG_NET_FREEBSD_SYSCTL)
122 static int      ip_keepfaith = 0;
123 #endif
124
125 SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
126         &ip_keepfaith,  0,
127         "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
128
129 #ifdef CYGPKG_NET_FREEBSD_SYSCTL
130 static int      ip_maxfragpackets;      /* initialized in ip_init() */
131 #endif
132 SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragpackets, CTLFLAG_RW,
133         &ip_maxfragpackets, 0,
134         "Maximum number of IPv4 fragment reassembly queue entries");
135
136 static int    nipq = 0;         /* total # of reass queues */
137 static int    maxnipq;
138
139 /*
140  * XXX - Setting ip_checkinterface mostly implements the receive side of
141  * the Strong ES model described in RFC 1122, but since the routing table
142  * and transmit implementation do not implement the Strong ES model,
143  * setting this to 1 results in an odd hybrid.
144  *
145  * XXX - ip_checkinterface currently must be disabled if you use ipnat
146  * to translate the destination address to another local interface.
147  *
148  * XXX - ip_checkinterface must be disabled if you add IP aliases
149  * to the loopback interface instead of the interface where the
150  * packets for those addresses are received.
151  */
152 static int      ip_checkinterface = 0;
153 SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
154     &ip_checkinterface, 0, "Verify packet arrives on correct interface");
155
156 #ifdef DIAGNOSTIC
157 static int      ipprintfs = 0;
158 #endif
159
160 extern  struct domain inetdomain;
161 extern  struct protosw inetsw[];
162 u_char  ip_protox[IPPROTO_MAX];
163 static int      ipqmaxlen = IFQ_MAXLEN;
164 struct  in_ifaddrhead in_ifaddrhead; /* first inet address */
165 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
166     &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
167 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
168     &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue");
169
170 struct ipstat ipstat;
171 SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RD,
172     &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
173
174 /* Packet reassembly stuff */
175 #define IPREASS_NHASH_LOG2      6
176 #define IPREASS_NHASH           (1 << IPREASS_NHASH_LOG2)
177 #define IPREASS_HMASK           (IPREASS_NHASH - 1)
178 #define IPREASS_HASH(x,y) \
179         (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
180
181 static struct ipq ipq[IPREASS_NHASH];
182 const  int    ipintrq_present = 1;
183
184 #ifdef IPCTL_DEFMTU
185 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
186     &ip_mtu, 0, "Default MTU");
187 #endif
188
189 #ifdef IPSTEALTH
190 static int      ipstealth = 0;
191 SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
192     &ipstealth, 0, "");
193 #endif
194
195
196 /* Firewall hooks */
197 ip_fw_chk_t *ip_fw_chk_ptr;
198 ip_fw_ctl_t *ip_fw_ctl_ptr;
199 int fw_enable = 1 ;
200
201 #ifdef DUMMYNET
202 ip_dn_ctl_t *ip_dn_ctl_ptr;
203 #endif
204
205 int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **)) = NULL;
206
207
208 /*
209  * We need to save the IP options in case a protocol wants to respond
210  * to an incoming packet over the same route if the packet got here
211  * using IP source routing.  This allows connection establishment and
212  * maintenance when the remote end is on a network that is not known
213  * to us.
214  */
215 static int      ip_nhops = 0;
216 static  struct ip_srcrt {
217         struct  in_addr dst;                    /* final destination */
218         char    nop;                            /* one NOP to align */
219         char    srcopt[IPOPT_OFFSET + 1];       /* OPTVAL, OLEN and OFFSET */
220         struct  in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
221 } ip_srcrt;
222
223 struct sockaddr_in *ip_fw_fwd_addr;
224
225 static void     save_rte __P((u_char *, struct in_addr));
226 static int      ip_dooptions __P((struct mbuf *));
227 #ifdef NATPT
228        void     ip_forward __P((struct mbuf *, int));
229 #else
230 static void     ip_forward __P((struct mbuf *, int));
231 #endif
232 static void     ip_freef __P((struct ipq *));
233 #ifdef IPDIVERT
234 static struct   mbuf *ip_reass __P((struct mbuf *,
235                         struct ipq *, struct ipq *, u_int32_t *, u_int16_t *));
236 #else
237 static struct   mbuf *ip_reass __P((struct mbuf *, struct ipq *, struct ipq *));
238 #endif
239 static struct   in_ifaddr *ip_rtaddr __P((struct in_addr));
240 static void     ipintr __P((void));
241
242 #ifdef NATPT
243 extern  int                     ip6_protocol_tr;
244 int     natpt_in4               __P((struct mbuf *, struct mbuf **));
245 extern  void ip6_forward        __P((struct mbuf *, int));
246 #endif  /* NATPT */
247
248 /*
249  * IP initialization: fill in IP protocol switch table.
250  * All protocols not implemented in kernel go to raw IP protocol handler.
251  */
252 void
253 ip_init()
254 {
255         register struct protosw *pr;
256         register int i;
257
258         TAILQ_INIT(&in_ifaddrhead);
259         pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
260         if (pr == 0)
261                 panic("ip_init");
262         for (i = 0; i < IPPROTO_MAX; i++)
263                 ip_protox[i] = pr - inetsw;
264         for (pr = inetdomain.dom_protosw;
265             pr < inetdomain.dom_protoswNPROTOSW; pr++)
266                 if (pr->pr_domain->dom_family == PF_INET &&
267                     pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
268                         ip_protox[pr->pr_protocol] = pr - inetsw;
269
270         for (i = 0; i < IPREASS_NHASH; i++)
271             ipq[i].next = ipq[i].prev = &ipq[i];
272
273         maxnipq = nmbclusters / 4;
274
275 #ifndef RANDOM_IP_ID
276         ip_id = time_second & 0xffff;
277 #endif
278         ipintrq.ifq_maxlen = ipqmaxlen;
279
280         register_netisr(NETISR_IP, ipintr);
281 }
282
283 static struct   sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
284 static struct   route ipforward_rt;
285
286 /*
287  * Ip input routine.  Checksum and byte swap header.  If fragmented
288  * try to reassemble.  Process options.  Pass to next level.
289  */
290 void
291 ip_input(struct mbuf *m)
292 {
293         struct ip *ip;
294         struct ipq *fp;
295         struct in_ifaddr *ia = NULL;
296         int    i, hlen, mff, checkif;
297         u_short sum;
298         u_int16_t divert_cookie;                /* firewall cookie */
299         struct in_addr pkt_dst;
300 #ifdef IPDIVERT
301         u_int32_t divert_info = 0;              /* packet divert/tee info */
302 #endif
303         struct ip_fw_chain *rule = NULL;
304
305 #ifdef IPDIVERT
306         /* Get and reset firewall cookie */
307         divert_cookie = ip_divert_cookie;
308         ip_divert_cookie = 0;
309 #else
310         divert_cookie = 0;
311 #endif
312
313 #if defined(IPFIREWALL) && defined(DUMMYNET)
314         /*
315          * dummynet packet are prepended a vestigial mbuf with
316          * m_type = MT_DUMMYNET and m_data pointing to the matching
317          * rule.
318          */
319         if (m->m_type == MT_DUMMYNET) {
320             rule = (struct ip_fw_chain *)(m->m_data) ;
321             m = m->m_next ;
322             ip = mtod(m, struct ip *);
323             hlen = IP_VHL_HL(ip->ip_vhl) << 2;
324             goto iphack ;
325         } else
326             rule = NULL ;
327 #endif
328
329 #ifdef  DIAGNOSTIC
330         if (m == NULL || (m->m_flags & M_PKTHDR) == 0)
331                 panic("ip_input no HDR");
332 #endif
333         ipstat.ips_total++;
334
335         if (m->m_pkthdr.len < sizeof(struct ip))
336                 goto tooshort;
337
338         if (m->m_len < sizeof (struct ip) &&
339             (m = m_pullup(m, sizeof (struct ip))) == 0) {
340                 ipstat.ips_toosmall++;
341                 return;
342         }
343         ip = mtod(m, struct ip *);
344
345         if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
346                 ipstat.ips_badvers++;
347                 goto bad;
348         }
349
350         hlen = IP_VHL_HL(ip->ip_vhl) << 2;
351         if (hlen < sizeof(struct ip)) { /* minimum header length */
352                 ipstat.ips_badhlen++;
353                 goto bad;
354         }
355         if (hlen > m->m_len) {
356                 if ((m = m_pullup(m, hlen)) == 0) {
357                         ipstat.ips_badhlen++;
358                         return;
359                 }
360                 ip = mtod(m, struct ip *);
361         }
362
363         /* 127/8 must not appear on wire - RFC1122 */
364         if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
365             (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
366                 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
367                         ipstat.ips_badaddr++;
368                         goto bad;
369                 }
370         }
371
372         if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
373                 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
374         } else {
375                 if (hlen == sizeof(struct ip)) {
376                         sum = in_cksum_hdr(ip);
377                 } else {
378                         sum = in_cksum(m, hlen);
379                 }
380         }
381         if (sum) {
382                 ipstat.ips_badsum++;
383                 goto bad;
384         }
385
386 #ifdef ALTQ
387         if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
388                 /* packet is dropped by traffic conditioner */
389                 return;
390 #endif
391         /*
392          * Convert fields to host representation.
393          */
394         NTOHS(ip->ip_len);
395         if (ip->ip_len < hlen) {
396                 ipstat.ips_badlen++;
397                 goto bad;
398         }
399         NTOHS(ip->ip_off);
400
401         /*
402          * Check that the amount of data in the buffers
403          * is as at least much as the IP header would have us expect.
404          * Trim mbufs if longer than we expect.
405          * Drop packet if shorter than we expect.
406          */
407         if (m->m_pkthdr.len < ip->ip_len) {
408 tooshort:
409                 ipstat.ips_tooshort++;
410                 goto bad;
411         }
412         if (m->m_pkthdr.len > ip->ip_len) {
413                 if (m->m_len == m->m_pkthdr.len) {
414                         m->m_len = ip->ip_len;
415                         m->m_pkthdr.len = ip->ip_len;
416                 } else
417                         m_adj(m, ip->ip_len - m->m_pkthdr.len);
418         }
419
420 #ifdef IPSEC
421         if (ipsec_getnhist(m))
422                 goto pass;
423 #endif
424
425         /*
426          * IpHack's section.
427          * Right now when no processing on packet has done
428          * and it is still fresh out of network we do our black
429          * deals with it.
430          * - Firewall: deny/allow/divert
431          * - Xlate: translate packet's addr/port (NAT).
432          * - Pipe: pass pkt through dummynet.
433          * - Wrap: fake packet's addr/port <unimpl.>
434          * - Encapsulate: put it in another IP and send out. <unimp.>
435          */
436
437 #if defined(IPFIREWALL) && defined(DUMMYNET)
438 iphack:
439 #endif
440         /*
441          * Check if we want to allow this packet to be processed.
442          * Consider it to be bad if not.
443          */
444         if (fr_checkp) {
445                 struct  mbuf    *m1 = m;
446
447                 if ((*fr_checkp)(ip, hlen, m->m_pkthdr.rcvif, 0, &m1) || !m1)
448                         return;
449                 ip = mtod(m = m1, struct ip *);
450         }
451         if (fw_enable && ip_fw_chk_ptr) {
452 #ifdef IPFIREWALL_FORWARD
453                 /*
454                  * If we've been forwarded from the output side, then
455                  * skip the firewall a second time
456                  */
457                 if (ip_fw_fwd_addr)
458                         goto ours;
459 #endif  /* IPFIREWALL_FORWARD */
460                 /*
461                  * See the comment in ip_output for the return values
462                  * produced by the firewall.
463                  */
464                 i = (*ip_fw_chk_ptr)(&ip,
465                     hlen, NULL, &divert_cookie, &m, &rule, &ip_fw_fwd_addr);
466                 if ( (i & IP_FW_PORT_DENY_FLAG) || m == NULL) { /* drop */
467                         if (m)
468                                 m_freem(m);
469                         return ;
470                 }
471                 ip = mtod(m, struct ip *); /* just in case m changed */
472                 if (i == 0 && ip_fw_fwd_addr == NULL)   /* common case */
473                         goto pass;
474 #ifdef DUMMYNET
475                 if ((i & IP_FW_PORT_DYNT_FLAG) != 0) {
476                         /* Send packet to the appropriate pipe */
477                         dummynet_io(i&0xffff,DN_TO_IP_IN,m,NULL,NULL,0, rule,
478                                     0);
479                         return;
480                 }
481 #endif
482 #ifdef IPDIVERT
483                 if (i != 0 && (i & IP_FW_PORT_DYNT_FLAG) == 0) {
484                         /* Divert or tee packet */
485                         divert_info = i;
486                         goto ours;
487                 }
488 #endif
489 #ifdef IPFIREWALL_FORWARD
490                 if (i == 0 && ip_fw_fwd_addr != NULL)
491                         goto pass;
492 #endif
493                 /*
494                  * if we get here, the packet must be dropped
495                  */
496                 m_freem(m);
497                 return;
498         }
499 pass:
500
501         /*
502          * Process options and, if not destined for us,
503          * ship it on.  ip_dooptions returns 1 when an
504          * error was detected (causing an icmp message
505          * to be sent and the original packet to be freed).
506          */
507         ip_nhops = 0;           /* for source routed packets */
508         if (hlen > sizeof (struct ip) && ip_dooptions(m)) {
509 #ifdef IPFIREWALL_FORWARD
510                 ip_fw_fwd_addr = NULL;
511 #endif
512                 return;
513         }
514
515         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
516          * matter if it is destined to another node, or whether it is 
517          * a multicast one, RSVP wants it! and prevents it from being forwarded
518          * anywhere else. Also checks if the rsvp daemon is running before
519          * grabbing the packet.
520          */
521         if (rsvp_on && ip->ip_p==IPPROTO_RSVP) 
522                 goto ours;
523
524 #ifdef NATPT
525         /*
526          * NATPT (Network Address Translation - Protocol Translation)
527          */
528         if (ip6_protocol_tr) {
529                 struct mbuf     *m1 = NULL;
530
531                 switch (natpt_in4(m, &m1)) {
532                 case IPPROTO_IP:        /* this packet is not changed   */
533                         goto checkaddresses;
534
535                 case IPPROTO_IPV4:
536                         ip_forward(m1, 0);
537                         break;
538
539                 case IPPROTO_IPV6:
540                         ip6_forward(m1, 1);
541                         break;
542
543                 case IPPROTO_DONE:      /* discard without free */
544                         return;
545
546                 case IPPROTO_MAX:       /* discard this packet  */
547                 default:
548                         break;
549                 }
550
551                 if (m != m1)
552                         m_freem(m);
553
554                 return;
555         }
556 checkaddresses:;
557 #endif
558
559         /*
560          * Check our list of addresses, to see if the packet is for us.
561          * If we don't have any addresses, assume any unicast packet
562          * we receive might be for us (and let the upper layers deal
563          * with it).
564          */
565         if (TAILQ_EMPTY(&in_ifaddrhead) &&
566             (m->m_flags & (M_MCAST|M_BCAST)) == 0)
567                 goto ours;
568
569         /*
570          * Cache the destination address of the packet; this may be
571          * changed by use of 'ipfw fwd'.
572          */
573         pkt_dst = ip_fw_fwd_addr == NULL ?
574             ip->ip_dst : ip_fw_fwd_addr->sin_addr;
575
576         /*
577          * Enable a consistency check between the destination address
578          * and the arrival interface for a unicast packet (the RFC 1122
579          * strong ES model) if IP forwarding is disabled and the packet
580          * is not locally generated and the packet is not subject to
581          * 'ipfw fwd'.
582          *
583          * XXX - Checking also should be disabled if the destination
584          * address is ipnat'ed to a different interface.
585          *
586          * XXX - Checking is incompatible with IP aliases added
587          * to the loopback interface instead of the interface where
588          * the packets are received.
589          */
590         checkif = ip_checkinterface && (ipforwarding == 0) && 
591             ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) &&
592             (ip_fw_fwd_addr == NULL);
593
594         TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
595 #define satosin(sa)     ((struct sockaddr_in *)(sa))
596
597 #ifdef BOOTP_COMPAT
598                 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
599                         goto ours;
600 #endif
601                 /*
602                  * If the address matches, verify that the packet
603                  * arrived via the correct interface if checking is
604                  * enabled.
605                  */
606                 if (IA_SIN(ia)->sin_addr.s_addr == pkt_dst.s_addr && 
607                     (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif))
608                         goto ours;
609                 /*
610                  * Only accept broadcast packets that arrive via the
611                  * matching interface.  Reception of forwarded directed
612                  * broadcasts would be handled via ip_forward() and
613                  * ether_output() with the loopback into the stack for
614                  * SIMPLEX interfaces handled by ether_output().
615                  */
616                 if (ia->ia_ifp == m->m_pkthdr.rcvif &&
617                     ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) {
618                         if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
619                             pkt_dst.s_addr)
620                                 goto ours;
621                         if (ia->ia_netbroadcast.s_addr == pkt_dst.s_addr)
622                                 goto ours;
623                 }
624         }
625         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
626                 struct in_multi *inm;
627                 if (ip_mrouter) {
628                         /*
629                          * If we are acting as a multicast router, all
630                          * incoming multicast packets are passed to the
631                          * kernel-level multicast forwarding function.
632                          * The packet is returned (relatively) intact; if
633                          * ip_mforward() returns a non-zero value, the packet
634                          * must be discarded, else it may be accepted below.
635                          */
636                         if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
637                                 ipstat.ips_cantforward++;
638                                 m_freem(m);
639                                 return;
640                         }
641
642                         /*
643                          * The process-level routing demon needs to receive
644                          * all multicast IGMP packets, whether or not this
645                          * host belongs to their destination groups.
646                          */
647                         if (ip->ip_p == IPPROTO_IGMP)
648                                 goto ours;
649                         ipstat.ips_forward++;
650                 }
651                 /*
652                  * See if we belong to the destination multicast group on the
653                  * arrival interface.
654                  */
655                 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
656                 if (inm == NULL) {
657                         ipstat.ips_notmember++;
658                         m_freem(m);
659                         return;
660                 }
661                 goto ours;
662         }
663         if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
664                 goto ours;
665         if (ip->ip_dst.s_addr == INADDR_ANY)
666                 goto ours;
667
668 #if defined(NFAITH) && 0 < NFAITH
669         /*
670          * FAITH(Firewall Aided Internet Translator)
671          */
672         if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
673                 if (ip_keepfaith) {
674                         if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP) 
675                                 goto ours;
676                 }
677                 m_freem(m);
678                 return;
679         }
680 #endif
681         /*
682          * Not for us; forward if possible and desirable.
683          */
684         if (ipforwarding == 0) {
685                 ipstat.ips_cantforward++;
686                 m_freem(m);
687         } else
688                 ip_forward(m, 0);
689 #ifdef IPFIREWALL_FORWARD
690         ip_fw_fwd_addr = NULL;
691 #endif
692         return;
693
694 ours:
695         /* Count the packet in the ip address stats */
696         if (ia != NULL) {
697                 ia->ia_ifa.if_ipackets++;
698                 ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
699         }
700
701         /*
702          * If offset or IP_MF are set, must reassemble.
703          * Otherwise, nothing need be done.
704          * (We could look in the reassembly queue to see
705          * if the packet was previously fragmented,
706          * but it's not worth the time; just let them time out.)
707          */
708         if (ip->ip_off & (IP_MF | IP_OFFMASK | IP_RF)) {
709
710 #if 0   /*
711          * Reassembly should be able to treat a mbuf cluster, for later
712          * operation of contiguous protocol headers on the cluster. (KAME)
713          */
714                 if (m->m_flags & M_EXT) {               /* XXX */
715                         if ((m = m_pullup(m, hlen)) == 0) {
716                                 ipstat.ips_toosmall++;
717 #ifdef IPFIREWALL_FORWARD
718                                 ip_fw_fwd_addr = NULL;
719 #endif
720                                 return;
721                         }
722                         ip = mtod(m, struct ip *);
723                 }
724 #endif
725
726                 /* If maxnipq is 0, never accept fragments. */
727                 if (maxnipq == 0) {
728                         ipstat.ips_fragments++;
729                         ipstat.ips_fragdropped++;
730                         goto bad;
731                 }
732
733                 sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
734                 /*
735                  * Look for queue of fragments
736                  * of this datagram.
737                  */
738                 for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next)
739                         if (ip->ip_id == fp->ipq_id &&
740                             ip->ip_src.s_addr == fp->ipq_src.s_addr &&
741                             ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
742                             ip->ip_p == fp->ipq_p)
743                                 goto found;
744
745                 fp = 0;
746
747                 /*
748                  * Enforce upper bound on number of fragmented packets
749                  * for which we attempt reassembly;
750                  * If maxnipq is -1, accept all fragments without limitation.
751                  */
752                 if ((nipq > maxnipq) && (maxnipq > 0)) {
753                     /*
754                      * drop something from the tail of the current queue
755                      * before proceeding further
756                      */
757                     if (ipq[sum].prev == &ipq[sum]) {   /* gak */
758                         for (i = 0; i < IPREASS_NHASH; i++) {
759                             if (ipq[i].prev != &ipq[i]) {
760                                 ip_freef(ipq[i].prev);
761                                 ipstat.ips_fragtimeout++;
762                                 break;
763                             }
764                         }
765                     } else {
766                         ip_freef(ipq[sum].prev);
767                         ipstat.ips_fragtimeout++;
768                     }
769                 }
770 found:
771                 /*
772                  * Adjust ip_len to not reflect header,
773                  * set ip_mff if more fragments are expected,
774                  * convert offset of this to bytes.
775                  */
776                 ip->ip_len -= hlen;
777                 mff = (ip->ip_off & IP_MF) != 0;
778                 if (mff) {
779                         /*
780                          * Make sure that fragments have a data length
781                          * that's a non-zero multiple of 8 bytes.
782                          */
783                         if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
784                                 ipstat.ips_toosmall++; /* XXX */
785                                 goto bad;
786                         }
787                         m->m_flags |= M_FRAG;
788                 }
789                 ip->ip_off <<= 3;
790
791                 /*
792                  * If datagram marked as having more fragments
793                  * or if this is not the first fragment,
794                  * attempt reassembly; if it succeeds, proceed.
795                  */
796                 if (mff || ip->ip_off) {
797                         ipstat.ips_fragments++;
798                         m->m_pkthdr.header = ip;
799 #ifdef IPDIVERT
800                         m = ip_reass(m,
801                             fp, &ipq[sum], &divert_info, &divert_cookie);
802 #else
803                         m = ip_reass(m, fp, &ipq[sum]);
804 #endif
805                         if (m == 0) {
806 #ifdef IPFIREWALL_FORWARD
807                                 ip_fw_fwd_addr = NULL;
808 #endif
809                                 return;
810                         }
811                         ipstat.ips_reassembled++;
812                         ip = mtod(m, struct ip *);
813                         /* Get the header length of the reassembled packet */
814                         hlen = IP_VHL_HL(ip->ip_vhl) << 2;
815 #ifdef IPDIVERT
816                         /* Restore original checksum before diverting packet */
817                         if (divert_info != 0) {
818                                 ip->ip_len += hlen;
819                                 HTONS(ip->ip_len);
820                                 HTONS(ip->ip_off);
821                                 ip->ip_sum = 0;
822                                 if (hlen == sizeof(struct ip))
823                                         ip->ip_sum = in_cksum_hdr(ip);
824                                 else
825                                         ip->ip_sum = in_cksum(m, hlen);
826                                 NTOHS(ip->ip_off);
827                                 NTOHS(ip->ip_len);
828                                 ip->ip_len -= hlen;
829                         }
830 #endif
831                 } else
832                         if (fp)
833                                 ip_freef(fp);
834         } else
835                 ip->ip_len -= hlen;
836
837 #ifdef IPDIVERT
838         /*
839          * Divert or tee packet to the divert protocol if required.
840          *
841          * If divert_info is zero then cookie should be too, so we shouldn't
842          * need to clear them here.  Assume divert_packet() does so also.
843          */
844         if (divert_info != 0) {
845                 struct mbuf *clone = NULL;
846
847                 /* Clone packet if we're doing a 'tee' */
848                 if ((divert_info & IP_FW_PORT_TEE_FLAG) != 0)
849                         clone = m_dup(m, M_DONTWAIT);
850
851                 /* Restore packet header fields to original values */
852                 ip->ip_len += hlen;
853                 HTONS(ip->ip_len);
854                 HTONS(ip->ip_off);
855
856                 /* Deliver packet to divert input routine */
857                 ip_divert_cookie = divert_cookie;
858                 divert_packet(m, 1, divert_info & 0xffff);
859                 ipstat.ips_delivered++;
860
861                 /* If 'tee', continue with original packet */
862                 if (clone == NULL)
863                         return;
864                 m = clone;
865                 ip = mtod(m, struct ip *);
866         }
867 #endif
868
869 #ifdef IPSEC
870         /*
871          * enforce IPsec policy checking if we are seeing last header.
872          * note that we do not visit this with protocols with pcb layer
873          * code - like udp/tcp/raw ip.
874          */
875         if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
876             ipsec4_in_reject(m, NULL)) {
877                 ipsecstat.in_polvio++;
878                 goto bad;
879         }
880 #endif
881
882         /*
883          * Switch out to protocol's input routine.
884          */
885         ipstat.ips_delivered++;
886     {
887         int off = hlen;
888
889         (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, off);
890 #ifdef  IPFIREWALL_FORWARD
891         ip_fw_fwd_addr = NULL;  /* tcp needed it */
892 #endif
893         return;
894     }
895 bad:
896 #ifdef  IPFIREWALL_FORWARD
897         ip_fw_fwd_addr = NULL;
898 #endif
899         m_freem(m);
900 }
901
902 /*
903  * IP software interrupt routine - to go away sometime soon
904  */
905 static void
906 ipintr(void)
907 {
908         int s;
909         struct mbuf *m;
910
911         while(1) {
912                 s = splimp();
913                 IF_DEQUEUE(&ipintrq, m);
914                 splx(s);
915                 if (m == 0)
916                         return;
917                 ip_input(m);
918         }
919 }
920
921 /*
922  * Take incoming datagram fragment and try to reassemble it into
923  * whole datagram.  If a chain for reassembly of this datagram already
924  * exists, then it is given as fp; otherwise have to make a chain.
925  *
926  * When IPDIVERT enabled, keep additional state with each packet that
927  * tells us if we need to divert or tee the packet we're building.
928  */
929
930 static struct mbuf *
931 #ifdef IPDIVERT
932 ip_reass(m, fp, where, divinfo, divcookie)
933 #else
934 ip_reass(m, fp, where)
935 #endif
936         register struct mbuf *m;
937         register struct ipq *fp;
938         struct   ipq    *where;
939 #ifdef IPDIVERT
940         u_int32_t *divinfo;
941         u_int16_t *divcookie;
942 #endif
943 {
944         struct ip *ip = mtod(m, struct ip *);
945         register struct mbuf *p = 0, *q, *nq;
946         struct mbuf *t;
947         int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
948         int i, next;
949
950         /*
951          * Presence of header sizes in mbufs
952          * would confuse code below.
953          */
954         m->m_data += hlen;
955         m->m_len -= hlen;
956
957         /*
958          * If first fragment to arrive, create a reassembly queue.
959          */
960         if (fp == 0) {
961                 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
962                         goto dropfrag;
963                 fp = mtod(t, struct ipq *);
964                 insque(fp, where);
965                 nipq++;
966                 fp->ipq_ttl = IPFRAGTTL;
967                 fp->ipq_p = ip->ip_p;
968                 fp->ipq_id = ip->ip_id;
969                 fp->ipq_src = ip->ip_src;
970                 fp->ipq_dst = ip->ip_dst;
971                 fp->ipq_frags = m;
972                 m->m_nextpkt = NULL;
973 #ifdef IPDIVERT
974                 fp->ipq_div_info = 0;
975                 fp->ipq_div_cookie = 0;
976 #endif
977                 goto inserted;
978         }
979
980 #define GETIP(m)        ((struct ip*)((m)->m_pkthdr.header))
981
982         /*
983          * Find a segment which begins after this one does.
984          */
985         for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
986                 if (GETIP(q)->ip_off > ip->ip_off)
987                         break;
988
989         /*
990          * If there is a preceding segment, it may provide some of
991          * our data already.  If so, drop the data from the incoming
992          * segment.  If it provides all of our data, drop us, otherwise
993          * stick new segment in the proper place.
994          *
995          * If some of the data is dropped from the the preceding
996          * segment, then it's checksum is invalidated.
997          */
998         if (p) {
999                 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
1000                 if (i > 0) {
1001                         if (i >= ip->ip_len)
1002                                 goto dropfrag;
1003                         m_adj(m, i);
1004                         m->m_pkthdr.csum_flags = 0;
1005                         ip->ip_off += i;
1006                         ip->ip_len -= i;
1007                 }
1008                 m->m_nextpkt = p->m_nextpkt;
1009                 p->m_nextpkt = m;
1010         } else {
1011                 m->m_nextpkt = fp->ipq_frags;
1012                 fp->ipq_frags = m;
1013         }
1014
1015         /*
1016          * While we overlap succeeding segments trim them or,
1017          * if they are completely covered, dequeue them.
1018          */
1019         for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
1020              q = nq) {
1021                 i = (ip->ip_off + ip->ip_len) -
1022                     GETIP(q)->ip_off;
1023                 if (i < GETIP(q)->ip_len) {
1024                         GETIP(q)->ip_len -= i;
1025                         GETIP(q)->ip_off += i;
1026                         m_adj(q, i);
1027                         q->m_pkthdr.csum_flags = 0;
1028                         break;
1029                 }
1030                 nq = q->m_nextpkt;
1031                 m->m_nextpkt = nq;
1032                 m_freem(q);
1033         }
1034
1035 inserted:
1036
1037 #ifdef IPDIVERT
1038         /*
1039          * Transfer firewall instructions to the fragment structure.
1040          * Any fragment diverting causes the whole packet to divert.
1041          */
1042         fp->ipq_div_info = *divinfo;
1043         fp->ipq_div_cookie = *divcookie;
1044         *divinfo = 0;
1045         *divcookie = 0;
1046 #endif
1047
1048         /*
1049          * Check for complete reassembly.
1050          */
1051         next = 0;
1052         for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
1053                 if (GETIP(q)->ip_off != next)
1054                         return (0);
1055                 next += GETIP(q)->ip_len;
1056         }
1057         /* Make sure the last packet didn't have the IP_MF flag */
1058         if (p->m_flags & M_FRAG)
1059                 return (0);
1060
1061         /*
1062          * Reassembly is complete.  Make sure the packet is a sane size.
1063          */
1064         q = fp->ipq_frags;
1065         ip = GETIP(q);
1066         if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) {
1067                 ipstat.ips_toolong++;
1068                 ip_freef(fp);
1069                 return (0);
1070         }
1071
1072         /*
1073          * Concatenate fragments.
1074          */
1075         m = q;
1076         t = m->m_next;
1077         m->m_next = 0;
1078         m_cat(m, t);
1079         nq = q->m_nextpkt;
1080         q->m_nextpkt = 0;
1081         for (q = nq; q != NULL; q = nq) {
1082                 nq = q->m_nextpkt;
1083                 q->m_nextpkt = NULL;
1084                 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1085                 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1086                 m_cat(m, q);
1087         }
1088
1089 #ifdef IPDIVERT
1090         /*
1091          * Extract firewall instructions from the fragment structure.
1092          */
1093         *divinfo = fp->ipq_div_info;
1094         *divcookie = fp->ipq_div_cookie;
1095 #endif
1096
1097         /*
1098          * Create header for new ip packet by
1099          * modifying header of first packet;
1100          * dequeue and discard fragment reassembly header.
1101          * Make header visible.
1102          */
1103         ip->ip_len = next;
1104         ip->ip_src = fp->ipq_src;
1105         ip->ip_dst = fp->ipq_dst;
1106         remque(fp);
1107         nipq--;
1108         (void) m_free(dtom(fp));
1109         m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2);
1110         m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2);
1111         /* some debugging cruft by sklower, below, will go away soon */
1112         if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
1113                 register int plen = 0;
1114                 for (t = m; t; t = t->m_next)
1115                         plen += t->m_len;
1116                 m->m_pkthdr.len = plen;
1117         }
1118         return (m);
1119
1120 dropfrag:
1121 #ifdef IPDIVERT
1122         *divinfo = 0;
1123         *divcookie = 0;
1124 #endif
1125         ipstat.ips_fragdropped++;
1126         m_freem(m);
1127         return (0);
1128
1129 #undef GETIP
1130 }
1131
1132 /*
1133  * Free a fragment reassembly header and all
1134  * associated datagrams.
1135  */
1136 static void
1137 ip_freef(fp)
1138         struct ipq *fp;
1139 {
1140         register struct mbuf *q;
1141
1142         while (fp->ipq_frags) {
1143                 q = fp->ipq_frags;
1144                 fp->ipq_frags = q->m_nextpkt;
1145                 m_freem(q);
1146         }
1147         remque(fp);
1148         (void) m_free(dtom(fp));
1149         nipq--;
1150 }
1151
1152 /*
1153  * IP timer processing;
1154  * if a timer expires on a reassembly
1155  * queue, discard it.
1156  */
1157 void
1158 ip_slowtimo()
1159 {
1160         register struct ipq *fp;
1161         int s = splnet();
1162         int i;
1163
1164         for (i = 0; i < IPREASS_NHASH; i++) {
1165                 fp = ipq[i].next;
1166                 if (fp == 0)
1167                         continue;
1168                 while (fp != &ipq[i]) {
1169                         --fp->ipq_ttl;
1170                         fp = fp->next;
1171                         if (fp->prev->ipq_ttl == 0) {
1172                                 ipstat.ips_fragtimeout++;
1173                                 ip_freef(fp->prev);
1174                         }
1175                 }
1176         }
1177         /*
1178          * If we are over the maximum number of fragments
1179          * (due to the limit being lowered), drain off
1180          * enough to get down to the new limit.
1181          */
1182         if (maxnipq >= 0 && nipq > maxnipq) {
1183                 for (i = 0; i < IPREASS_NHASH; i++) {
1184                         while ((nipq > maxnipq) &&
1185                                 (ipq[i].next != &ipq[i])) {
1186                                 ipstat.ips_fragdropped++;
1187                                 ip_freef(ipq[i].next);
1188                         }
1189                 }
1190         }
1191         ipflow_slowtimo();
1192         splx(s);
1193 }
1194
1195 /*
1196  * Drain off all datagram fragments.
1197  */
1198 void
1199 ip_drain()
1200 {
1201         int     i;
1202
1203         for (i = 0; i < IPREASS_NHASH; i++) {
1204                 while (ipq[i].next != &ipq[i]) {
1205                         ipstat.ips_fragdropped++;
1206                         ip_freef(ipq[i].next);
1207                 }
1208         }
1209         in_rtqdrain();
1210 }
1211
1212 /*
1213  * Do option processing on a datagram,
1214  * possibly discarding it if bad options are encountered,
1215  * or forwarding it if source-routed.
1216  * Returns 1 if packet has been forwarded/freed,
1217  * 0 if the packet should be processed further.
1218  */
1219 static int
1220 ip_dooptions(m)
1221         struct mbuf *m;
1222 {
1223         register struct ip *ip = mtod(m, struct ip *);
1224         register u_char *cp;
1225         register struct ip_timestamp *ipt;
1226         register struct in_ifaddr *ia;
1227         int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1228         struct in_addr *sin, dst;
1229         n_time ntime;
1230
1231         dst = ip->ip_dst;
1232         cp = (u_char *)(ip + 1);
1233         cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1234         for (; cnt > 0; cnt -= optlen, cp += optlen) {
1235                 opt = cp[IPOPT_OPTVAL];
1236                 if (opt == IPOPT_EOL)
1237                         break;
1238                 if (opt == IPOPT_NOP)
1239                         optlen = 1;
1240                 else {
1241                         if (cnt < IPOPT_OLEN + sizeof(*cp)) {
1242                                 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1243                                 goto bad;
1244                         }
1245                         optlen = cp[IPOPT_OLEN];
1246                         if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1247                                 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1248                                 goto bad;
1249                         }
1250                 }
1251                 switch (opt) {
1252
1253                 default:
1254                         break;
1255
1256                 /*
1257                  * Source routing with record.
1258                  * Find interface with current destination address.
1259                  * If none on this machine then drop if strictly routed,
1260                  * or do nothing if loosely routed.
1261                  * Record interface address and bring up next address
1262                  * component.  If strictly routed make sure next
1263                  * address is on directly accessible net.
1264                  */
1265                 case IPOPT_LSRR:
1266                 case IPOPT_SSRR:
1267                         if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1268                                 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1269                                 goto bad;
1270                         }
1271                         if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1272                                 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1273                                 goto bad;
1274                         }
1275                         ipaddr.sin_addr = ip->ip_dst;
1276                         ia = (struct in_ifaddr *)
1277                                 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
1278                         if (ia == 0) {
1279                                 if (opt == IPOPT_SSRR) {
1280                                         type = ICMP_UNREACH;
1281                                         code = ICMP_UNREACH_SRCFAIL;
1282                                         goto bad;
1283                                 }
1284                                 if (!ip_dosourceroute)
1285                                         goto nosourcerouting;
1286                                 /*
1287                                  * Loose routing, and not at next destination
1288                                  * yet; nothing to do except forward.
1289                                  */
1290                                 break;
1291                         }
1292                         off--;                  /* 0 origin */
1293                         if (off > optlen - (int)sizeof(struct in_addr)) {
1294                                 /*
1295                                  * End of source route.  Should be for us.
1296                                  */
1297                                 if (!ip_acceptsourceroute)
1298                                         goto nosourcerouting;
1299                                 save_rte(cp, ip->ip_src);
1300                                 break;
1301                         }
1302
1303                         if (!ip_dosourceroute) {
1304                                 if (ipforwarding) {
1305                                         char buf[16]; /* aaa.bbb.ccc.ddd\0 */
1306                                         /*
1307                                          * Acting as a router, so generate ICMP
1308                                          */
1309 nosourcerouting:
1310                                         strcpy(buf, inet_ntoa(ip->ip_dst));
1311                                         log(LOG_WARNING, 
1312                                             "attempted source route from %s to %s\n",
1313                                             inet_ntoa(ip->ip_src), buf);
1314                                         type = ICMP_UNREACH;
1315                                         code = ICMP_UNREACH_SRCFAIL;
1316                                         goto bad;
1317                                 } else {
1318                                         /*
1319                                          * Not acting as a router, so silently drop.
1320                                          */
1321                                         ipstat.ips_cantforward++;
1322                                         m_freem(m);
1323                                         return (1);
1324                                 }
1325                         }
1326
1327                         /*
1328                          * locate outgoing interface
1329                          */
1330                         (void)memcpy(&ipaddr.sin_addr, cp + off,
1331                             sizeof(ipaddr.sin_addr));
1332
1333                         if (opt == IPOPT_SSRR) {
1334 #define INA     struct in_ifaddr *
1335 #define SA      struct sockaddr *
1336                             if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
1337                                 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
1338                         } else
1339                                 ia = ip_rtaddr(ipaddr.sin_addr);
1340                         if (ia == 0) {
1341                                 type = ICMP_UNREACH;
1342                                 code = ICMP_UNREACH_SRCFAIL;
1343                                 goto bad;
1344                         }
1345                         ip->ip_dst = ipaddr.sin_addr;
1346                         (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1347                             sizeof(struct in_addr));
1348                         cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1349                         /*
1350                          * Let ip_intr's mcast routing check handle mcast pkts
1351                          */
1352                         forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
1353                         break;
1354
1355                 case IPOPT_RR:
1356                         if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1357                                 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1358                                 goto bad;
1359                         }
1360                         if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1361                                 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1362                                 goto bad;
1363                         }
1364                         /*
1365                          * If no space remains, ignore.
1366                          */
1367                         off--;                  /* 0 origin */
1368                         if (off > optlen - (int)sizeof(struct in_addr))
1369                                 break;
1370                         (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
1371                             sizeof(ipaddr.sin_addr));
1372                         /*
1373                          * locate outgoing interface; if we're the destination,
1374                          * use the incoming interface (should be same).
1375                          */
1376                         if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
1377                             (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
1378                                 type = ICMP_UNREACH;
1379                                 code = ICMP_UNREACH_HOST;
1380                                 goto bad;
1381                         }
1382                         (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1383                             sizeof(struct in_addr));
1384                         cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1385                         break;
1386
1387                 case IPOPT_TS:
1388                         code = cp - (u_char *)ip;
1389                         ipt = (struct ip_timestamp *)cp;
1390                         if (ipt->ipt_len < 4 || ipt->ipt_len > 40) {
1391                                 code = (u_char *)&ipt->ipt_len - (u_char *)ip;
1392                                 goto bad;
1393                         }
1394                         if (ipt->ipt_ptr < 5) {
1395                                 code = (u_char *)&ipt->ipt_ptr - (u_char *)ip;
1396                                 goto bad;
1397                         }
1398                         if (ipt->ipt_ptr >
1399                             ipt->ipt_len - (int)sizeof(int32_t)) {
1400                                 if (++ipt->ipt_oflw == 0) {
1401                                         code = (u_char *)&ipt->ipt_ptr -
1402                                             (u_char *)ip;
1403                                         goto bad;
1404                                 }
1405                                 break;
1406                         }
1407                         sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
1408                         switch (ipt->ipt_flg) {
1409
1410                         case IPOPT_TS_TSONLY:
1411                                 break;
1412
1413                         case IPOPT_TS_TSANDADDR:
1414                                 if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1415                                     sizeof(struct in_addr) > ipt->ipt_len) {
1416                                         code = (u_char *)&ipt->ipt_ptr -
1417                                             (u_char *)ip;
1418                                         goto bad;
1419                                 }
1420                                 ipaddr.sin_addr = dst;
1421                                 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
1422                                                             m->m_pkthdr.rcvif);
1423                                 if (ia == 0)
1424                                         continue;
1425                                 (void)memcpy(sin, &IA_SIN(ia)->sin_addr,
1426                                     sizeof(struct in_addr));
1427                                 ipt->ipt_ptr += sizeof(struct in_addr);
1428                                 break;
1429
1430                         case IPOPT_TS_PRESPEC:
1431                                 if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1432                                     sizeof(struct in_addr) > ipt->ipt_len) {
1433                                         code = (u_char *)&ipt->ipt_ptr -
1434                                             (u_char *)ip;
1435                                         goto bad;
1436                                 }
1437                                 (void)memcpy(&ipaddr.sin_addr, sin,
1438                                     sizeof(struct in_addr));
1439                                 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
1440                                         continue;
1441                                 ipt->ipt_ptr += sizeof(struct in_addr);
1442                                 break;
1443
1444                         default:
1445                                 /* XXX can't take &ipt->ipt_flg */
1446                                 code = (u_char *)&ipt->ipt_ptr -
1447                                     (u_char *)ip + 1;
1448                                 goto bad;
1449                         }
1450                         ntime = iptime();
1451                         (void)memcpy(cp + ipt->ipt_ptr - 1, &ntime,
1452                             sizeof(n_time));
1453                         ipt->ipt_ptr += sizeof(n_time);
1454                 }
1455         }
1456         if (forward && ipforwarding) {
1457                 ip_forward(m, 1);
1458                 return (1);
1459         }
1460         return (0);
1461 bad:
1462         icmp_error(m, type, code, 0, 0);
1463         ipstat.ips_badoptions++;
1464         return (1);
1465 }
1466
1467 /*
1468  * Given address of next destination (final or next hop),
1469  * return internet address info of interface to be used to get there.
1470  */
1471 static struct in_ifaddr *
1472 ip_rtaddr(dst)
1473          struct in_addr dst;
1474 {
1475         register struct sockaddr_in *sin;
1476
1477         sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
1478
1479         if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
1480                 if (ipforward_rt.ro_rt) {
1481                         RTFREE(ipforward_rt.ro_rt);
1482                         ipforward_rt.ro_rt = 0;
1483                 }
1484                 sin->sin_family = AF_INET;
1485                 sin->sin_len = sizeof(*sin);
1486                 sin->sin_addr = dst;
1487
1488                 rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1489         }
1490         if (ipforward_rt.ro_rt == 0)
1491                 return ((struct in_ifaddr *)0);
1492         return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
1493 }
1494
1495 /*
1496  * Save incoming source route for use in replies,
1497  * to be picked up later by ip_srcroute if the receiver is interested.
1498  */
1499 void
1500 save_rte(option, dst)
1501         u_char *option;
1502         struct in_addr dst;
1503 {
1504         unsigned olen;
1505
1506         olen = option[IPOPT_OLEN];
1507 #ifdef DIAGNOSTIC
1508         if (ipprintfs)
1509                 printf("save_rte: olen %d\n", olen);
1510 #endif
1511         if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1512                 return;
1513         bcopy(option, ip_srcrt.srcopt, olen);
1514         ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1515         ip_srcrt.dst = dst;
1516 }
1517
1518 /*
1519  * Retrieve incoming source route for use in replies,
1520  * in the same form used by setsockopt.
1521  * The first hop is placed before the options, will be removed later.
1522  */
1523 struct mbuf *
1524 ip_srcroute()
1525 {
1526         register struct in_addr *p, *q;
1527         register struct mbuf *m;
1528
1529         if (ip_nhops == 0)
1530                 return ((struct mbuf *)0);
1531         m = m_get(M_DONTWAIT, MT_HEADER);
1532         if (m == 0)
1533                 return ((struct mbuf *)0);
1534
1535 #define OPTSIZ  (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1536
1537         /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1538         m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1539             OPTSIZ;
1540 #ifdef DIAGNOSTIC
1541         if (ipprintfs)
1542                 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1543 #endif
1544
1545         /*
1546          * First save first hop for return route
1547          */
1548         p = &ip_srcrt.route[ip_nhops - 1];
1549         *(mtod(m, struct in_addr *)) = *p--;
1550 #ifdef DIAGNOSTIC
1551         if (ipprintfs)
1552                 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr));
1553 #endif
1554
1555         /*
1556          * Copy option fields and padding (nop) to mbuf.
1557          */
1558         ip_srcrt.nop = IPOPT_NOP;
1559         ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1560         (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
1561             &ip_srcrt.nop, OPTSIZ);
1562         q = (struct in_addr *)(mtod(m, caddr_t) +
1563             sizeof(struct in_addr) + OPTSIZ);
1564 #undef OPTSIZ
1565         /*
1566          * Record return path as an IP source route,
1567          * reversing the path (pointers are now aligned).
1568          */
1569         while (p >= ip_srcrt.route) {
1570 #ifdef DIAGNOSTIC
1571                 if (ipprintfs)
1572                         printf(" %lx", (u_long)ntohl(q->s_addr));
1573 #endif
1574                 *q++ = *p--;
1575         }
1576         /*
1577          * Last hop goes to final destination.
1578          */
1579         *q = ip_srcrt.dst;
1580 #ifdef DIAGNOSTIC
1581         if (ipprintfs)
1582                 printf(" %lx\n", (u_long)ntohl(q->s_addr));
1583 #endif
1584         return (m);
1585 }
1586
1587 /*
1588  * Strip out IP options, at higher
1589  * level protocol in the kernel.
1590  * Second argument is buffer to which options
1591  * will be moved, and return value is their length.
1592  * XXX should be deleted; last arg currently ignored.
1593  */
1594 void
1595 ip_stripoptions(m, mopt)
1596         register struct mbuf *m;
1597         struct mbuf *mopt;
1598 {
1599         register int i;
1600         struct ip *ip = mtod(m, struct ip *);
1601         register caddr_t opts;
1602         int olen;
1603
1604         olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1605         opts = (caddr_t)(ip + 1);
1606         i = m->m_len - (sizeof (struct ip) + olen);
1607         bcopy(opts + olen, opts, (unsigned)i);
1608         m->m_len -= olen;
1609         if (m->m_flags & M_PKTHDR)
1610                 m->m_pkthdr.len -= olen;
1611         ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
1612 }
1613
1614 int inetctlerrmap[PRC_NCMDS] = {
1615         0,              0,              0,              0,
1616         0,              EMSGSIZE,       EHOSTDOWN,      EHOSTUNREACH,
1617         EHOSTUNREACH,   EHOSTUNREACH,   ECONNREFUSED,   ECONNREFUSED,
1618         EMSGSIZE,       EHOSTUNREACH,   0,              0,
1619         0,              0,              0,              0,
1620         ENOPROTOOPT,    ECONNREFUSED
1621 };
1622
1623 /*
1624  * Forward a packet.  If some error occurs return the sender
1625  * an icmp packet.  Note we can't always generate a meaningful
1626  * icmp message because icmp doesn't have a large enough repertoire
1627  * of codes and types.
1628  *
1629  * If not forwarding, just drop the packet.  This could be confusing
1630  * if ipforwarding was zero but some routing protocol was advancing
1631  * us as a gateway to somewhere.  However, we must let the routing
1632  * protocol deal with that.
1633  *
1634  * The srcrt parameter indicates whether the packet is being forwarded
1635  * via a source route.
1636  */
1637 #ifdef NATPT
1638 void
1639 #else
1640 static void
1641 #endif
1642 ip_forward(m, srcrt)
1643         struct mbuf *m;
1644         int srcrt;
1645 {
1646         register struct ip *ip = mtod(m, struct ip *);
1647         register struct sockaddr_in *sin;
1648         register struct rtentry *rt;
1649         int error, type = 0, code = 0;
1650         struct mbuf *mcopy;
1651         n_long dest;
1652         struct ifnet *destifp;
1653 #ifdef IPSEC
1654         struct ifnet dummyifp;
1655 #endif
1656
1657         dest = 0;
1658 #ifdef DIAGNOSTIC
1659         if (ipprintfs)
1660                 printf("forward: src %lx dst %lx ttl %x\n",
1661                     (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr,
1662                     ip->ip_ttl);
1663 #endif
1664
1665
1666         if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1667                 ipstat.ips_cantforward++;
1668                 m_freem(m);
1669                 return;
1670         }
1671 #ifdef IPSTEALTH
1672         if (!ipstealth) {
1673 #endif
1674                 if (ip->ip_ttl <= IPTTLDEC) {
1675                         icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
1676                             dest, 0);
1677                         return;
1678                 }
1679 #ifdef IPSTEALTH
1680         }
1681 #endif
1682
1683         sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
1684         if ((rt = ipforward_rt.ro_rt) == 0 ||
1685             ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
1686                 if (ipforward_rt.ro_rt) {
1687                         RTFREE(ipforward_rt.ro_rt);
1688                         ipforward_rt.ro_rt = 0;
1689                 }
1690                 sin->sin_family = AF_INET;
1691                 sin->sin_len = sizeof(*sin);
1692                 sin->sin_addr = ip->ip_dst;
1693
1694                 rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1695                 if (ipforward_rt.ro_rt == 0) {
1696                         icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1697                         return;
1698                 }
1699                 rt = ipforward_rt.ro_rt;
1700         }
1701
1702         /*
1703          * Save the IP header and at most 8 bytes of the payload,
1704          * in case we need to generate an ICMP message to the src.
1705          *
1706          * We don't use m_copy() because it might return a reference
1707          * to a shared cluster. Both this function and ip_output()
1708          * assume exclusive access to the IP header in `m', so any
1709          * data in a cluster may change before we reach icmp_error().
1710          */
1711         MGET(mcopy, M_DONTWAIT, m->m_type);
1712         if (mcopy != NULL) {
1713                 M_COPY_PKTHDR(mcopy, m);
1714                 mcopy->m_len = imin((IP_VHL_HL(ip->ip_vhl) << 2) + 8,
1715                     (int)ip->ip_len);
1716                 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1717         }
1718
1719 #ifdef IPSTEALTH
1720         if (!ipstealth) {
1721 #endif
1722                 ip->ip_ttl -= IPTTLDEC;
1723 #ifdef IPSTEALTH
1724         }
1725 #endif
1726
1727         /*
1728          * If forwarding packet using same interface that it came in on,
1729          * perhaps should send a redirect to sender to shortcut a hop.
1730          * Only send redirect if source is sending directly to us,
1731          * and if packet was not source routed (or has any options).
1732          * Also, don't send redirect if forwarding using a default route
1733          * or a route modified by a redirect.
1734          */
1735 #define satosin(sa)     ((struct sockaddr_in *)(sa))
1736         if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1737             (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1738             satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
1739             ipsendredirects && !srcrt) {
1740 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1741                 u_long src = ntohl(ip->ip_src.s_addr);
1742
1743                 if (RTA(rt) &&
1744                     (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1745                     if (rt->rt_flags & RTF_GATEWAY)
1746                         dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1747                     else
1748                         dest = ip->ip_dst.s_addr;
1749                     /* Router requirements says to only send host redirects */
1750                     type = ICMP_REDIRECT;
1751                     code = ICMP_REDIRECT_HOST;
1752 #ifdef DIAGNOSTIC
1753                     if (ipprintfs)
1754                         printf("redirect (%d) to %lx\n", code, (u_long)dest);
1755 #endif
1756                 }
1757         }
1758
1759         error = ip_output(m, (struct mbuf *)0, &ipforward_rt, 
1760                           IP_FORWARDING, 0);
1761         if (error)
1762                 ipstat.ips_cantforward++;
1763         else {
1764                 ipstat.ips_forward++;
1765                 if (type)
1766                         ipstat.ips_redirectsent++;
1767                 else {
1768                         if (mcopy) {
1769                                 ipflow_create(&ipforward_rt, mcopy);
1770                                 m_freem(mcopy);
1771                         }
1772                         return;
1773                 }
1774         }
1775         if (mcopy == NULL)
1776                 return;
1777         destifp = NULL;
1778
1779         switch (error) {
1780
1781         case 0:                         /* forwarded, but need redirect */
1782                 /* type, code set above */
1783                 break;
1784
1785         case ENETUNREACH:               /* shouldn't happen, checked above */
1786         case EHOSTUNREACH:
1787         case ENETDOWN:
1788         case EHOSTDOWN:
1789         default:
1790                 type = ICMP_UNREACH;
1791                 code = ICMP_UNREACH_HOST;
1792                 break;
1793
1794         case EMSGSIZE:
1795                 type = ICMP_UNREACH;
1796                 code = ICMP_UNREACH_NEEDFRAG;
1797 #ifndef IPSEC
1798                 if (ipforward_rt.ro_rt)
1799                         destifp = ipforward_rt.ro_rt->rt_ifp;
1800 #else
1801                 /*
1802                  * If the packet is routed over IPsec tunnel, tell the
1803                  * originator the tunnel MTU.
1804                  *      tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1805                  * XXX quickhack!!!
1806                  */
1807                 if (ipforward_rt.ro_rt) {
1808                         struct secpolicy *sp = NULL;
1809                         int ipsecerror;
1810                         int ipsechdr;
1811                         struct route *ro;
1812
1813                         sp = ipsec4_getpolicybyaddr(mcopy,
1814                                                     IPSEC_DIR_OUTBOUND,
1815                                                     IP_FORWARDING,
1816                                                     &ipsecerror);
1817
1818                         if (sp == NULL)
1819                                 destifp = ipforward_rt.ro_rt->rt_ifp;
1820                         else {
1821                                 /* count IPsec header size */
1822                                 ipsechdr = ipsec4_hdrsiz(mcopy,
1823                                                          IPSEC_DIR_OUTBOUND,
1824                                                          NULL);
1825
1826                                 /*
1827                                  * find the correct route for outer IPv4
1828                                  * header, compute tunnel MTU.
1829                                  *
1830                                  * XXX BUG ALERT
1831                                  * The "dummyifp" code relies upon the fact
1832                                  * that icmp_error() touches only ifp->if_mtu.
1833                                  */
1834                                 /*XXX*/
1835                                 destifp = NULL;
1836                                 if (sp->req != NULL
1837                                  && sp->req->sav != NULL
1838                                  && sp->req->sav->sah != NULL) {
1839                                         ro = &sp->req->sav->sah->sa_route;
1840                                         if (ro->ro_rt && ro->ro_rt->rt_ifp) {
1841                                                 dummyifp.if_mtu =
1842                                                     ro->ro_rt->rt_ifp->if_mtu;
1843                                                 dummyifp.if_mtu -= ipsechdr;
1844                                                 destifp = &dummyifp;
1845                                         }
1846                                 }
1847
1848                                 key_freesp(sp);
1849                         }
1850                 }
1851 #endif /*IPSEC*/
1852                 ipstat.ips_cantfrag++;
1853                 break;
1854
1855         case ENOBUFS:
1856 #ifdef ALTQ
1857                 /*
1858                  * don't generate ICMP_SOURCEQUENCH
1859                  * (RFC1812 Requirements for IP Version 4 Routers)
1860                  */
1861                 if (mcopy)
1862                         m_freem(mcopy);
1863                 return;
1864 #else
1865                 type = ICMP_SOURCEQUENCH;
1866                 code = 0;
1867                 break;
1868 #endif
1869
1870         case EACCES:                    /* ipfw denied packet */
1871                 m_freem(mcopy);
1872                 return;
1873         }
1874         icmp_error(mcopy, type, code, dest, destifp);
1875 }
1876
1877 void
1878 ip_savecontrol(inp, mp, ip, m)
1879         register struct inpcb *inp;
1880         register struct mbuf **mp;
1881         register struct ip *ip;
1882         register struct mbuf *m;
1883 {
1884         if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1885                 struct timeval tv;
1886
1887                 microtime(&tv);
1888                 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1889                         SCM_TIMESTAMP, SOL_SOCKET);
1890                 if (*mp)
1891                         mp = &(*mp)->m_next;
1892         }
1893         if (inp->inp_flags & INP_RECVDSTADDR) {
1894                 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1895                     sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1896                 if (*mp)
1897                         mp = &(*mp)->m_next;
1898         }
1899 #ifdef notyet
1900         /* XXX
1901          * Moving these out of udp_input() made them even more broken
1902          * than they already were.
1903          */
1904         /* options were tossed already */
1905         if (inp->inp_flags & INP_RECVOPTS) {
1906                 *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1907                     sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1908                 if (*mp)
1909                         mp = &(*mp)->m_next;
1910         }
1911         /* ip_srcroute doesn't do what we want here, need to fix */
1912         if (inp->inp_flags & INP_RECVRETOPTS) {
1913                 *mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1914                     sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1915                 if (*mp)
1916                         mp = &(*mp)->m_next;
1917         }
1918 #endif
1919         if (inp->inp_flags & INP_RECVIF) {
1920                 struct ifnet *ifp;
1921                 struct sdlbuf {
1922                         struct sockaddr_dl sdl;
1923                         u_char  pad[32];
1924                 } sdlbuf;
1925                 struct sockaddr_dl *sdp;
1926                 struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
1927
1928                 if (((ifp = m->m_pkthdr.rcvif)) 
1929                 && ( ifp->if_index && (ifp->if_index <= if_index))) {
1930                         sdp = (struct sockaddr_dl *)(ifnet_addrs
1931                                         [ifp->if_index - 1]->ifa_addr);
1932                         /*
1933                          * Change our mind and don't try copy.
1934                          */
1935                         if ((sdp->sdl_family != AF_LINK)
1936                         || (sdp->sdl_len > sizeof(sdlbuf))) {
1937                                 goto makedummy;
1938                         }
1939                         bcopy(sdp, sdl2, sdp->sdl_len);
1940                 } else {
1941 makedummy:      
1942                         sdl2->sdl_len
1943                                 = offsetof(struct sockaddr_dl, sdl_data[0]);
1944                         sdl2->sdl_family = AF_LINK;
1945                         sdl2->sdl_index = 0;
1946                         sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1947                 }
1948                 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
1949                         IP_RECVIF, IPPROTO_IP);
1950                 if (*mp)
1951                         mp = &(*mp)->m_next;
1952         }
1953 }
1954
1955 int
1956 ip_rsvp_init(struct socket *so)
1957 {
1958         if (so->so_type != SOCK_RAW ||
1959             so->so_proto->pr_protocol != IPPROTO_RSVP)
1960           return EOPNOTSUPP;
1961
1962         if (ip_rsvpd != NULL)
1963           return EADDRINUSE;
1964
1965         ip_rsvpd = so;
1966         /*
1967          * This may seem silly, but we need to be sure we don't over-increment
1968          * the RSVP counter, in case something slips up.
1969          */
1970         if (!ip_rsvp_on) {
1971                 ip_rsvp_on = 1;
1972                 rsvp_on++;
1973         }
1974
1975         return 0;
1976 }
1977
1978 int
1979 ip_rsvp_done(void)
1980 {
1981         ip_rsvpd = NULL;
1982         /*
1983          * This may seem silly, but we need to be sure we don't over-decrement
1984          * the RSVP counter, in case something slips up.
1985          */
1986         if (ip_rsvp_on) {
1987                 ip_rsvp_on = 0;
1988                 rsvp_on--;
1989         }
1990         return 0;
1991 }