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