]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_input.c
fd35302366aa8bea9f388fa95a37932fcd0d2434
[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 #if defined(IPFILTER_LKM) || defined(IPFILTER)
210 int iplattach __P((void));
211 int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **)) = NULL;
212 #endif
213
214
215 /*
216 * We need to save the IP options in case a protocol wants to respond
217 * to an incoming packet over the same route if the packet got here
218 * using IP source routing. This allows connection establishment and
219 * maintenance when the remote end is on a network that is not known
220 * to us.
221 */
222 static int ip_nhops = 0;
223 static struct ip_srcrt {
224 struct in_addr dst; /* final destination */
225 char nop; /* one NOP to align */
226 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
227 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
228 } ip_srcrt;
229
230 #if IPDIVERT
231 /*
232 * Shared variable between ip_input() and ip_reass() to communicate
233 * about which packets, once assembled from fragments, get diverted,
234 * and to which port.
235 */
236 static u_short frag_divert_port;
237 #endif
238
239 struct sockaddr_in *ip_fw_fwd_addr;
240
241 static void save_rte __P((u_char *, struct in_addr));
242 static int ip_dooptions __P((struct mbuf *));
243 #ifndef NATPT
244 static
245 #endif
246 void ip_forward __P((struct mbuf *, int));
247 static void ip_freef __P((struct ipq *));
248 static struct ip *
249 ip_reass __P((struct mbuf *, struct ipq *, struct ipq *));
250 static struct in_ifaddr *
251 ip_rtaddr __P((struct in_addr));
252 void ipintr __P((void));
253
254 #if PM
255 extern int doNatFil;
256 extern int doRoute;
257
258 extern int pm_in __P((struct ifnet *, struct ip *, struct mbuf *));
259 extern struct route *pm_route __P((struct mbuf *));
260 #endif
261
262 #if defined(NATPT)
263 extern int ip6_protocol_tr;
264
265 int natpt_in4 __P((struct mbuf *, struct mbuf **));
266
267 #endif /* NATPT */
268
269 /*
270 * IP initialization: fill in IP protocol switch table.
271 * All protocols not implemented in kernel go to raw IP protocol handler.
272 */
273 void
274 ip_init()
275 {
276 register struct protosw *pr;
277 register int i;
278 static ip_initialized = 0;
279
280 if (!ip_initialized)
281 {
282 TAILQ_INIT(&in_ifaddrhead);
283 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
284 if (pr == 0)
285 panic("ip_init");
286 for (i = 0; i < IPPROTO_MAX; i++)
287 ip_protox[i] = pr;
288 for (pr = inetdomain.dom_protosw; pr; pr = pr->pr_next)
289 { if(!((unsigned int)pr->pr_domain)) continue; /* If uninitialized, skip */
290 if (pr->pr_domain->dom_family == PF_INET &&
291 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
292 ip_protox[pr->pr_protocol] = pr;
293 }
294 for (i = 0; i < IPREASS_NHASH; i++)
295 ipq[i].next = ipq[i].prev = &ipq[i];
296
297 maxnipq = nmbclusters/4;
298
299 ip_id = time_second & 0xffff;
300 ipintrq.ifq_maxlen = ipqmaxlen;
301 #if DUMMYNET
302 ip_dn_init();
303 #endif
304 #if IPNAT
305 ip_nat_init();
306 #endif
307 #if IPFILTER
308 iplattach();
309 #endif
310 ip_initialized = 1;
311 }
312 }
313
314 /* Initialize the PF_INET domain, and add in the pre-defined protos */
315 void
316 in_dinit()
317 { register int i;
318 register struct protosw *pr;
319 register struct domain *dp;
320 static inetdomain_initted = 0;
321 extern int in_proto_count;
322
323 if (!inetdomain_initted)
324 { kprintf("Initing %d protosw entries\n", in_proto_count);
325 dp = &inetdomain;
326
327 for (i=0, pr = &inetsw[0]; i<in_proto_count; i++, pr++)
328 net_add_proto(pr, dp);
329 inetdomain_initted = 1;
330 }
331 }
332
333 static struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
334 static struct route ipforward_rt;
335
336 /*
337 * Ip input routine. Checksum and byte swap header. If fragmented
338 * try to reassemble. Process options. Pass to next level.
339 */
340 void
341 ip_input(struct mbuf *m)
342 {
343 struct ip *ip;
344 struct ipq *fp;
345 struct in_ifaddr *ia;
346 int i, hlen, mff;
347 u_short sum;
348 #if !IPDIVERT /* dummy variable for the firewall code to play with */
349 u_short ip_divert_cookie = 0 ;
350 #endif
351 #if COMPAT_IPFW
352 struct ip_fw_chain *rule = NULL ;
353 #endif
354
355 #if IPFIREWALL && DUMMYNET
356 /*
357 * dummynet packet are prepended a vestigial mbuf with
358 * m_type = MT_DUMMYNET and m_data pointing to the matching
359 * rule.
360 */
361 if (m->m_type == MT_DUMMYNET) {
362 struct mbuf *m0 = m ;
363 rule = (struct ip_fw_chain *)(m->m_data) ;
364 m = m->m_next ;
365 FREE(m0, M_IPFW);
366 ip = mtod(m, struct ip *);
367 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
368 goto iphack ;
369 } else
370 rule = NULL ;
371 #endif
372
373 #if DIAGNOSTIC
374 if (m == NULL || (m->m_flags & M_PKTHDR) == 0)
375 panic("ip_input no HDR");
376 #endif
377 /*
378 * If no IP addresses have been set yet but the interfaces
379 * are receiving, can't do anything with incoming packets yet.
380 * XXX This is broken! We should be able to receive broadcasts
381 * and multicasts even without any local addresses configured.
382 */
383 if (TAILQ_EMPTY(&in_ifaddrhead))
384 goto bad;
385 ipstat.ips_total++;
386
387 if (m->m_pkthdr.len < sizeof(struct ip))
388 goto tooshort;
389
390 if (m->m_len < sizeof (struct ip) &&
391 (m = m_pullup(m, sizeof (struct ip))) == 0) {
392 ipstat.ips_toosmall++;
393 return;
394 }
395 ip = mtod(m, struct ip *);
396
397 KERNEL_DEBUG(DBG_LAYER_BEG, ip->ip_dst.s_addr,
398 ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len);
399
400 if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
401 ipstat.ips_badvers++;
402 goto bad;
403 }
404
405 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
406 if (hlen < sizeof(struct ip)) { /* minimum header length */
407 ipstat.ips_badhlen++;
408 goto bad;
409 }
410 if (hlen > m->m_len) {
411 if ((m = m_pullup(m, hlen)) == 0) {
412 ipstat.ips_badhlen++;
413 return;
414 }
415 ip = mtod(m, struct ip *);
416 }
417
418 sum = in_cksum(m, hlen);
419
420 if (sum) {
421 ipstat.ips_badsum++;
422 goto bad;
423 }
424
425 /*
426 * Convert fields to host representation.
427 */
428 NTOHS(ip->ip_len);
429 if (ip->ip_len < hlen) {
430 ipstat.ips_badlen++;
431 goto bad;
432 }
433 NTOHS(ip->ip_id);
434 NTOHS(ip->ip_off);
435
436 /*
437 * Check that the amount of data in the buffers
438 * is as at least much as the IP header would have us expect.
439 * Trim mbufs if longer than we expect.
440 * Drop packet if shorter than we expect.
441 */
442 if (m->m_pkthdr.len < ip->ip_len) {
443 tooshort:
444 ipstat.ips_tooshort++;
445 goto bad;
446 }
447 if (m->m_pkthdr.len > ip->ip_len) {
448 if (m->m_len == m->m_pkthdr.len) {
449 m->m_len = ip->ip_len;
450 m->m_pkthdr.len = ip->ip_len;
451 } else
452 m_adj(m, ip->ip_len - m->m_pkthdr.len);
453 }
454 /*
455 * IpHack's section.
456 * Right now when no processing on packet has done
457 * and it is still fresh out of network we do our black
458 * deals with it.
459 * - Firewall: deny/allow/divert
460 * - Xlate: translate packet's addr/port (NAT).
461 * - Pipe: pass pkt through dummynet.
462 * - Wrap: fake packet's addr/port <unimpl.>
463 * - Encapsulate: put it in another IP and send out. <unimp.>
464 */
465
466 #if defined(IPFIREWALL) && defined(DUMMYNET)
467 iphack:
468 #endif
469 #if defined(IPFILTER) || defined(IPFILTER_LKM)
470 /*
471 * Check if we want to allow this packet to be processed.
472 * Consider it to be bad if not.
473 */
474 if (fr_checkp) {
475 struct mbuf *m1 = m;
476
477 if ((*fr_checkp)(ip, hlen, m->m_pkthdr.rcvif, 0, &m1) || !m1)
478 return;
479 ip = mtod(m = m1, struct ip *);
480 }
481 #endif
482 #if COMPAT_IPFW
483 if (ip_fw_chk_ptr) {
484 #if IPFIREWALL_FORWARD
485 /*
486 * If we've been forwarded from the output side, then
487 * skip the firewall a second time
488 */
489 if (ip_fw_fwd_addr)
490 goto ours;
491 #endif /* IPFIREWALL_FORWARD */
492 i = (*ip_fw_chk_ptr)(&ip, hlen, NULL, &ip_divert_cookie,
493 &m, &rule, &ip_fw_fwd_addr);
494 /*
495 * see the comment in ip_output for the return values
496 * produced by the firewall.
497 */
498 if (!m) /* packet discarded by firewall */
499 return ;
500 if (i == 0 && ip_fw_fwd_addr == NULL) /* common case */
501 goto pass ;
502 #if DUMMYNET
503 if (i & 0x10000) {
504 /* send packet to the appropriate pipe */
505 dummynet_io(i&0xffff,DN_TO_IP_IN,m,NULL,NULL,0, rule);
506 return ;
507 }
508 #endif
509 #if IPDIVERT
510 if (i > 0 && i < 0x10000) {
511 /* Divert packet */
512 frag_divert_port = i & 0xffff ;
513 goto ours;
514 }
515 #endif
516 #if IPFIREWALL_FORWARD
517 if (i == 0 && ip_fw_fwd_addr != NULL)
518 goto pass ;
519 #endif
520 /*
521 * if we get here, the packet must be dropped
522 */
523 m_freem(m);
524 return;
525 }
526 pass:
527
528 if (ip_nat_ptr && !(*ip_nat_ptr)(&ip, &m, m->m_pkthdr.rcvif, IP_NAT_IN)) {
529 #if IPFIREWALL_FORWARD
530 ip_fw_fwd_addr = NULL;
531 #endif
532 return;
533 }
534 #endif /* !COMPAT_IPFW */
535
536 #if defined(PM)
537 /*
538 * Process ip-filter/NAT.
539 * Return TRUE if this packed is discarded.
540 * Return FALSE if this packed is accepted.
541 */
542
543 if (doNatFil && pm_in(m->m_pkthdr.rcvif, ip, m))
544 return;
545 #endif
546
547 #if defined(NATPT)
548 /*
549 *
550 */
551 if (ip6_protocol_tr)
552 {
553 struct mbuf *m1 = NULL;
554
555 switch (natpt_in4(m, &m1))
556 {
557 case IPPROTO_IP: goto dooptions;
558 case IPPROTO_IPV4: ip_forward(m1, 0); break;
559 case IPPROTO_IPV6: ip6_forward(m1, 1); break;
560 case IPPROTO_MAX: /* discard this packet */
561 default:
562 }
563
564 if (m != m1)
565 m_freem(m);
566
567 return;
568 }
569 dooptions:
570 #endif
571
572 /*
573 * Process options and, if not destined for us,
574 * ship it on. ip_dooptions returns 1 when an
575 * error was detected (causing an icmp message
576 * to be sent and the original packet to be freed).
577 */
578 ip_nhops = 0; /* for source routed packets */
579 if (hlen > sizeof (struct ip) && ip_dooptions(m)) {
580 #if IPFIREWALL_FORWARD
581 ip_fw_fwd_addr = NULL;
582 #endif
583 return;
584 }
585
586 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
587 * matter if it is destined to another node, or whether it is
588 * a multicast one, RSVP wants it! and prevents it from being forwarded
589 * anywhere else. Also checks if the rsvp daemon is running before
590 * grabbing the packet.
591 */
592 if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
593 goto ours;
594
595 /*
596 * Check our list of addresses, to see if the packet is for us.
597 */
598 for (ia = TAILQ_FIRST(&in_ifaddrhead); ia;
599 ia = TAILQ_NEXT(ia, ia_link)) {
600 #define satosin(sa) ((struct sockaddr_in *)(sa))
601
602 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
603 goto ours;
604
605 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
606 goto ours;
607
608 #if IPFIREWALL_FORWARD
609 /*
610 * If the addr to forward to is one of ours, we pretend to
611 * be the destination for this packet.
612 */
613 if (ip_fw_fwd_addr == NULL) {
614 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
615 goto ours;
616 } else if (IA_SIN(ia)->sin_addr.s_addr ==
617 ip_fw_fwd_addr->sin_addr.s_addr)
618 goto ours;
619 #else
620 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
621 goto ours;
622 #endif
623 if (ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) {
624 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
625 ip->ip_dst.s_addr)
626 goto ours;
627 if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr)
628 goto ours;
629 }
630 }
631 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
632 struct in_multi *inm;
633 if (ip_mrouter) {
634 /*
635 * If we are acting as a multicast router, all
636 * incoming multicast packets are passed to the
637 * kernel-level multicast forwarding function.
638 * The packet is returned (relatively) intact; if
639 * ip_mforward() returns a non-zero value, the packet
640 * must be discarded, else it may be accepted below.
641 *
642 * (The IP ident field is put in the same byte order
643 * as expected when ip_mforward() is called from
644 * ip_output().)
645 */
646 ip->ip_id = htons(ip->ip_id);
647 if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
648 ipstat.ips_cantforward++;
649 m_freem(m);
650 return;
651 }
652 ip->ip_id = ntohs(ip->ip_id);
653
654 /*
655 * The process-level routing demon needs to receive
656 * all multicast IGMP packets, whether or not this
657 * host belongs to their destination groups.
658 */
659 if (ip->ip_p == IPPROTO_IGMP)
660 goto ours;
661 ipstat.ips_forward++;
662 }
663 /*
664 * See if we belong to the destination multicast group on the
665 * arrival interface.
666 */
667 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
668 if (inm == NULL) {
669 ipstat.ips_notmember++;
670 m_freem(m);
671 return;
672 }
673 goto ours;
674 }
675 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
676 goto ours;
677 if (ip->ip_dst.s_addr == INADDR_ANY)
678 goto ours;
679
680 #if defined(NFAITH) && NFAITH > 0
681 /*
682 * FAITH(Firewall Aided Internet Translator)
683 */
684 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
685 if (ip_keepfaith) {
686 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
687 goto ours;
688 }
689 m_freem(m);
690 return;
691 }
692 #endif
693 /*
694 * Not for us; forward if possible and desirable.
695 */
696 if (ipforwarding == 0) {
697 ipstat.ips_cantforward++;
698 m_freem(m);
699 } else
700 ip_forward(m, 0);
701 #if IPFIREWALL_FORWARD
702 ip_fw_fwd_addr = NULL;
703 #endif
704 return;
705
706 ours:
707
708 /*
709 * If offset or IP_MF are set, must reassemble.
710 * Otherwise, nothing need be done.
711 * (We could look in the reassembly queue to see
712 * if the packet was previously fragmented,
713 * but it's not worth the time; just let them time out.)
714 */
715 if (ip->ip_off & (IP_MF | IP_OFFMASK | IP_RF)) {
716 if (m->m_flags & M_EXT) { /* XXX */
717 if ((m = m_pullup(m, hlen)) == 0) {
718 ipstat.ips_toosmall++;
719 #if IPDIVERT
720 frag_divert_port = 0;
721 ip_divert_cookie = 0;
722 #endif
723 #if IPFIREWALL_FORWARD
724 ip_fw_fwd_addr = NULL;
725 #endif
726 return;
727 }
728 ip = mtod(m, struct ip *);
729 }
730 sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
731 /*
732 * Look for queue of fragments
733 * of this datagram.
734 */
735 for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next)
736 if (ip->ip_id == fp->ipq_id &&
737 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
738 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
739 ip->ip_p == fp->ipq_p)
740 goto found;
741
742 fp = 0;
743
744 /* check if there's a place for the new queue */
745 if (nipq > maxnipq) {
746 /*
747 * drop something from the tail of the current queue
748 * before proceeding further
749 */
750 if (ipq[sum].prev == &ipq[sum]) { /* gak */
751 for (i = 0; i < IPREASS_NHASH; i++) {
752 if (ipq[i].prev != &ipq[i]) {
753 ip_freef(ipq[i].prev);
754 break;
755 }
756 }
757 } else
758 ip_freef(ipq[sum].prev);
759 }
760 found:
761 /*
762 * Adjust ip_len to not reflect header,
763 * set ip_mff if more fragments are expected,
764 * convert offset of this to bytes.
765 */
766 ip->ip_len -= hlen;
767 mff = (ip->ip_off & IP_MF) != 0;
768 if (mff) {
769 /*
770 * Make sure that fragments have a data length
771 * that's a non-zero multiple of 8 bytes.
772 */
773 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
774 ipstat.ips_toosmall++; /* XXX */
775 goto bad;
776 }
777 m->m_flags |= M_FRAG;
778 }
779 ip->ip_off <<= 3;
780
781 /*
782 * If datagram marked as having more fragments
783 * or if this is not the first fragment,
784 * attempt reassembly; if it succeeds, proceed.
785 */
786 if (mff || ip->ip_off) {
787 ipstat.ips_fragments++;
788 m->m_pkthdr.header = ip;
789 ip = ip_reass(m, fp, &ipq[sum]);
790 if (ip == 0) {
791 #if IPFIREWALL_FORWARD
792 ip_fw_fwd_addr = NULL;
793 #endif
794 return;
795 }
796 /* Get the length of the reassembled packets header */
797 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
798 ipstat.ips_reassembled++;
799 m = dtom(ip);
800 #if IPDIVERT
801 if (frag_divert_port) {
802 struct mbuf m;
803 m.m_next = 0;
804 m.m_len = hlen;
805 m.m_data = (char *) ip;
806 ip->ip_len += hlen;
807 HTONS(ip->ip_len);
808 HTONS(ip->ip_off);
809 HTONS(ip->ip_id);
810 ip->ip_sum = 0;
811 ip->ip_sum = in_cksum(&m, hlen);
812 NTOHS(ip->ip_id);
813 NTOHS(ip->ip_off);
814 NTOHS(ip->ip_len);
815 ip->ip_len -= hlen;
816 }
817 #endif
818 } else
819 if (fp)
820 ip_freef(fp);
821 } else
822 ip->ip_len -= hlen;
823
824 #if IPDIVERT
825 /*
826 * Divert reassembled packets to the divert protocol if required
827 * If divert port is null then cookie should be too,
828 * so we shouldn't need to clear them here. Assume ip_divert does so.
829 */
830 if (frag_divert_port) {
831 ipstat.ips_delivered++;
832 ip_divert_port = frag_divert_port;
833 frag_divert_port = 0;
834 (*ip_protox[IPPROTO_DIVERT]->pr_input)(m, hlen);
835 return;
836 }
837
838 /* Don't let packets divert themselves */
839 if (ip->ip_p == IPPROTO_DIVERT) {
840 ipstat.ips_noproto++;
841 goto bad;
842 }
843
844 #endif
845
846 /*
847 * Switch out to protocol's input routine.
848 */
849 ipstat.ips_delivered++;
850
851 KERNEL_DEBUG(DBG_LAYER_END, ip->ip_dst.s_addr,
852 ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len);
853
854 (*ip_protox[ip->ip_p]->pr_input)(m, hlen);
855 #if IPFIREWALL_FORWARD
856 ip_fw_fwd_addr = NULL; /* tcp needed it */
857 #endif
858 return;
859 bad:
860 #if IPFIREWALL_FORWARD
861 ip_fw_fwd_addr = NULL;
862 #endif
863 KERNEL_DEBUG(DBG_LAYER_END, 0,0,0,0,0);
864 m_freem(m);
865 }
866
867 /*
868 * IP software interrupt routine - to go away sometime soon
869 */
870 void
871 ipintr(void)
872 {
873 int s;
874 struct mbuf *m;
875
876 KERNEL_DEBUG(DBG_FNC_IP_INPUT | DBG_FUNC_START, 0,0,0,0,0);
877
878 while(1) {
879 s = splimp();
880 IF_DEQUEUE(&ipintrq, m);
881 splx(s);
882 if (m == 0) {
883 KERNEL_DEBUG(DBG_FNC_IP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
884 return;
885 }
886
887 ip_input(m);
888 }
889 }
890
891 NETISR_SET(NETISR_IP, ipintr);
892
893 /*
894 * Take incoming datagram fragment and try to
895 * reassemble it into whole datagram. If a chain for
896 * reassembly of this datagram already exists, then it
897 * is given as fp; otherwise have to make a chain.
898 */
899 static struct ip *
900 ip_reass(m, fp, where)
901 register struct mbuf *m;
902 register struct ipq *fp;
903 struct ipq *where;
904 {
905 struct ip *ip = mtod(m, struct ip *);
906 register struct mbuf *p = 0, *q, *nq;
907 struct mbuf *t;
908 int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
909 int i, next;
910
911 /*
912 * Presence of header sizes in mbufs
913 * would confuse code below.
914 */
915 m->m_data += hlen;
916 m->m_len -= hlen;
917
918 /*
919 * If first fragment to arrive, create a reassembly queue.
920 */
921 if (fp == 0) {
922 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
923 goto dropfrag;
924 fp = mtod(t, struct ipq *);
925 insque((void *) fp, (void *) where);
926 nipq++;
927 fp->ipq_ttl = IPFRAGTTL;
928 fp->ipq_p = ip->ip_p;
929 fp->ipq_id = ip->ip_id;
930 fp->ipq_src = ip->ip_src;
931 fp->ipq_dst = ip->ip_dst;
932 fp->ipq_frags = m;
933 m->m_nextpkt = NULL;
934 #if IPDIVERT
935 fp->ipq_divert = 0;
936 fp->ipq_div_cookie = 0;
937 #endif
938 goto inserted;
939 }
940
941 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
942
943 /*
944 * Find a segment which begins after this one does.
945 */
946 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
947 if (GETIP(q)->ip_off > ip->ip_off)
948 break;
949
950 /*
951 * If there is a preceding segment, it may provide some of
952 * our data already. If so, drop the data from the incoming
953 * segment. If it provides all of our data, drop us, otherwise
954 * stick new segment in the proper place.
955 */
956 if (p) {
957 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
958 if (i > 0) {
959 if (i >= ip->ip_len)
960 goto dropfrag;
961 m_adj(dtom(ip), i);
962 ip->ip_off += i;
963 ip->ip_len -= i;
964 }
965 m->m_nextpkt = p->m_nextpkt;
966 p->m_nextpkt = m;
967 } else {
968 m->m_nextpkt = fp->ipq_frags;
969 fp->ipq_frags = m;
970 }
971
972 /*
973 * While we overlap succeeding segments trim them or,
974 * if they are completely covered, dequeue them.
975 */
976 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
977 q = nq) {
978 i = (ip->ip_off + ip->ip_len) -
979 GETIP(q)->ip_off;
980 if (i < GETIP(q)->ip_len) {
981 GETIP(q)->ip_len -= i;
982 GETIP(q)->ip_off += i;
983 m_adj(q, i);
984 break;
985 }
986 nq = q->m_nextpkt;
987 m->m_nextpkt = nq;
988 m_freem(q);
989 }
990
991 inserted:
992
993 #if IPDIVERT
994 /*
995 * Any fragment diverting causes the whole packet to divert
996 */
997 if (frag_divert_port) {
998 fp->ipq_divert = frag_divert_port;
999 fp->ipq_div_cookie = ip_divert_cookie;
1000 }
1001 frag_divert_port = 0;
1002 ip_divert_cookie = 0;
1003 #endif
1004
1005 /*
1006 * Check for complete reassembly.
1007 */
1008 next = 0;
1009 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
1010 if (GETIP(q)->ip_off != next)
1011 return (0);
1012 next += GETIP(q)->ip_len;
1013 }
1014 /* Make sure the last packet didn't have the IP_MF flag */
1015 if (p->m_flags & M_FRAG)
1016 return (0);
1017
1018 /*
1019 * Reassembly is complete. Make sure the packet is a sane size.
1020 */
1021 q = fp->ipq_frags;
1022 ip = GETIP(q);
1023 if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) {
1024 ipstat.ips_toolong++;
1025 ip_freef(fp);
1026 return (0);
1027 }
1028
1029 /*
1030 * Concatenate fragments.
1031 */
1032 m = q;
1033 t = m->m_next;
1034 m->m_next = 0;
1035 m_cat(m, t);
1036 nq = q->m_nextpkt;
1037 q->m_nextpkt = 0;
1038 for (q = nq; q != NULL; q = nq) {
1039 nq = q->m_nextpkt;
1040 q->m_nextpkt = NULL;
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 }