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