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