]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_input.c
bd5edb4fba6f4482e1b9e76a2fc008426f4c52ec
[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 /*
290 * IP initialization: fill in IP protocol switch table.
291 * All protocols not implemented in kernel go to raw IP protocol handler.
292 */
293 void
294 ip_init()
295 {
296 register struct protosw *pr;
297 register int i;
298 static ip_initialized = 0;
299
300 if (!ip_initialized)
301 {
302 TAILQ_INIT(&in_ifaddrhead);
303 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
304 if (pr == 0)
305 panic("ip_init");
306 for (i = 0; i < IPPROTO_MAX; i++)
307 ip_protox[i] = pr;
308 for (pr = inetdomain.dom_protosw; pr; pr = pr->pr_next)
309 { if(!((unsigned int)pr->pr_domain)) continue; /* If uninitialized, skip */
310 if (pr->pr_domain->dom_family == PF_INET &&
311 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
312 ip_protox[pr->pr_protocol] = pr;
313 }
314 for (i = 0; i < IPREASS_NHASH; i++)
315 ipq[i].next = ipq[i].prev = &ipq[i];
316
317 maxnipq = nmbclusters / 4;
318 ip_maxfragpackets = nmbclusters / 4;
319
320 #if RANDOM_IP_ID
321 ip_id = time_second & 0xffff;
322 #endif
323 ipintrq.ifq_maxlen = ipqmaxlen;
324 ip_initialized = 1;
325 }
326 }
327
328 /* Initialize the PF_INET domain, and add in the pre-defined protos */
329 void
330 in_dinit()
331 { register int i;
332 register struct protosw *pr;
333 register struct domain *dp;
334 static inetdomain_initted = 0;
335 extern int in_proto_count;
336
337 if (!inetdomain_initted)
338 {
339 kprintf("Initing %d protosw entries\n", in_proto_count);
340 dp = &inetdomain;
341
342 for (i=0, pr = &inetsw[0]; i<in_proto_count; i++, pr++)
343 net_add_proto(pr, dp);
344 inetdomain_initted = 1;
345 }
346 }
347
348 static struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
349 static struct route ipforward_rt;
350
351 /*
352 * Ip input routine. Checksum and byte swap header. If fragmented
353 * try to reassemble. Process options. Pass to next level.
354 */
355 void
356 ip_input(struct mbuf *m)
357 {
358 struct ip *ip;
359 struct ipq *fp;
360 struct in_ifaddr *ia = NULL;
361 int i, hlen, mff, checkif;
362 u_short sum;
363 u_int16_t divert_cookie; /* firewall cookie */
364 struct in_addr pkt_dst;
365 #if IPDIVERT
366 u_int32_t divert_info = 0; /* packet divert/tee info */
367 #endif
368 struct ip_fw_chain *rule = NULL;
369
370 #if IPDIVERT
371 /* Get and reset firewall cookie */
372 divert_cookie = ip_divert_cookie;
373 ip_divert_cookie = 0;
374 #else
375 divert_cookie = 0;
376 #endif
377
378 #if IPFIREWALL && DUMMYNET
379 /*
380 * dummynet packet are prepended a vestigial mbuf with
381 * m_type = MT_DUMMYNET and m_data pointing to the matching
382 * rule.
383 */
384 if (m->m_type == MT_DUMMYNET) {
385 rule = (struct ip_fw_chain *)(m->m_data) ;
386 m = m->m_next ;
387 ip = mtod(m, struct ip *);
388 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
389 goto iphack ;
390 } else
391 rule = NULL ;
392 #endif
393
394 #if DIAGNOSTIC
395 if (m == NULL || (m->m_flags & M_PKTHDR) == 0)
396 panic("ip_input no HDR");
397 #endif
398 ipstat.ips_total++;
399
400 if (m->m_pkthdr.len < sizeof(struct ip))
401 goto tooshort;
402
403 if (m->m_len < sizeof (struct ip) &&
404 (m = m_pullup(m, sizeof (struct ip))) == 0) {
405 ipstat.ips_toosmall++;
406 return;
407 }
408 ip = mtod(m, struct ip *);
409
410 KERNEL_DEBUG(DBG_LAYER_BEG, ip->ip_dst.s_addr,
411 ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len);
412
413 if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
414 ipstat.ips_badvers++;
415 goto bad;
416 }
417
418 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
419 if (hlen < sizeof(struct ip)) { /* minimum header length */
420 ipstat.ips_badhlen++;
421 goto bad;
422 }
423 if (hlen > m->m_len) {
424 if ((m = m_pullup(m, hlen)) == 0) {
425 ipstat.ips_badhlen++;
426 return;
427 }
428 ip = mtod(m, struct ip *);
429 }
430
431 /* 127/8 must not appear on wire - RFC1122 */
432 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
433 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
434 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
435 #ifndef __APPLE__
436 ipstat.ips_badaddr++;
437 #endif
438 goto bad;
439 }
440 }
441
442 /* IPv4 Link-Local Addresses as defined in <draft-ietf-zeroconf-ipv4-linklocal-05.txt> */
443 if ((IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) ||
444 IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)))) {
445 ip_linklocal_stat.iplls_in_total++;
446 if (ip->ip_ttl != MAXTTL) {
447 ip_linklocal_stat.iplls_in_badttl++;
448 /* Silently drop link local traffic with bad TTL */
449 if (ip_linklocal_in_allowbadttl != 0)
450 goto bad;
451 }
452 }
453 if (m->m_pkthdr.rcvif->if_hwassist == 0)
454 m->m_pkthdr.csum_flags = 0;
455
456 if ((m->m_pkthdr.csum_flags & CSUM_TCP_SUM16) && ip->ip_p != IPPROTO_TCP)
457 m->m_pkthdr.csum_flags = 0;
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 (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 demon 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 0 /*
777 * Reassembly should be able to treat a mbuf cluster, for later
778 * operation of contiguous protocol headers on the cluster. (KAME)
779 */
780 if (m->m_flags & M_EXT) { /* XXX */
781 if ((m = m_pullup(m, hlen)) == 0) {
782 ipstat.ips_toosmall++;
783 #if IPFIREWALL_FORWARD
784 ip_fw_fwd_addr = NULL;
785 #endif
786 return;
787 }
788 ip = mtod(m, struct ip *);
789 }
790 #endif
791 sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
792 /*
793 * Look for queue of fragments
794 * of this datagram.
795 */
796 for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next)
797 if (ip->ip_id == fp->ipq_id &&
798 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
799 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
800 ip->ip_p == fp->ipq_p)
801 goto found;
802
803 fp = 0;
804
805 /* check if there's a place for the new queue */
806 if (nipq > maxnipq) {
807 /*
808 * drop something from the tail of the current queue
809 * before proceeding further
810 */
811 if (ipq[sum].prev == &ipq[sum]) { /* gak */
812 for (i = 0; i < IPREASS_NHASH; i++) {
813 if (ipq[i].prev != &ipq[i]) {
814 ip_freef(ipq[i].prev);
815 break;
816 }
817 }
818 } else
819 ip_freef(ipq[sum].prev);
820 }
821 found:
822 /*
823 * Adjust ip_len to not reflect header,
824 * set ip_mff if more fragments are expected,
825 * convert offset of this to bytes.
826 */
827 ip->ip_len -= hlen;
828 mff = (ip->ip_off & IP_MF) != 0;
829 if (mff) {
830 /*
831 * Make sure that fragments have a data length
832 * that's a non-zero multiple of 8 bytes.
833 */
834 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
835 ipstat.ips_toosmall++; /* XXX */
836 goto bad;
837 }
838 m->m_flags |= M_FRAG;
839 }
840 ip->ip_off <<= 3;
841
842 /*
843 * If datagram marked as having more fragments
844 * or if this is not the first fragment,
845 * attempt reassembly; if it succeeds, proceed.
846 */
847 if (mff || ip->ip_off) {
848 ipstat.ips_fragments++;
849 m->m_pkthdr.header = ip;
850 #if IPDIVERT
851 m = ip_reass(m,
852 fp, &ipq[sum], &divert_info, &divert_cookie);
853 #else
854 m = ip_reass(m, fp, &ipq[sum]);
855 #endif
856 if (m == 0) {
857 #if IPFIREWALL_FORWARD
858 ip_fw_fwd_addr = NULL;
859 #endif
860 return;
861 }
862 ipstat.ips_reassembled++;
863 ip = mtod(m, struct ip *);
864 /* Get the header length of the reassembled packet */
865 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
866 #if IPDIVERT
867 /* Restore original checksum before diverting packet */
868 if (divert_info != 0) {
869 ip->ip_len += hlen;
870 HTONS(ip->ip_len);
871 HTONS(ip->ip_off);
872 ip->ip_sum = 0;
873 ip->ip_sum = in_cksum(m, hlen);
874 NTOHS(ip->ip_off);
875 NTOHS(ip->ip_len);
876 ip->ip_len -= hlen;
877 }
878 #endif
879 } else
880 if (fp)
881 ip_freef(fp);
882 } else
883 ip->ip_len -= hlen;
884
885 #if IPDIVERT
886 /*
887 * Divert or tee packet to the divert protocol if required.
888 *
889 * If divert_info is zero then cookie should be too, so we shouldn't
890 * need to clear them here. Assume divert_packet() does so also.
891 */
892 if (divert_info != 0) {
893 struct mbuf *clone = NULL;
894
895 /* Clone packet if we're doing a 'tee' */
896 if ((divert_info & IP_FW_PORT_TEE_FLAG) != 0)
897 clone = m_dup(m, M_DONTWAIT);
898
899 /* Restore packet header fields to original values */
900 ip->ip_len += hlen;
901 HTONS(ip->ip_len);
902 HTONS(ip->ip_off);
903
904 /* Deliver packet to divert input routine */
905 ip_divert_cookie = divert_cookie;
906 divert_packet(m, 1, divert_info & 0xffff);
907 ipstat.ips_delivered++;
908
909 /* If 'tee', continue with original packet */
910 if (clone == NULL)
911 return;
912 m = clone;
913 ip = mtod(m, struct ip *);
914 }
915 #endif
916
917 #if IPSEC
918 /*
919 * enforce IPsec policy checking if we are seeing last header.
920 * note that we do not visit this with protocols with pcb layer
921 * code - like udp/tcp/raw ip.
922 */
923 if (ipsec_bypass == 0 && (ip_protox[ip->ip_p]->pr_flags & PR_LASTHDR) != 0 &&
924 ipsec4_in_reject(m, NULL)) {
925 ipsecstat.in_polvio++;
926 goto bad;
927 }
928 #endif
929
930 /*
931 * Switch out to protocol's input routine.
932 */
933 ipstat.ips_delivered++;
934 {
935 KERNEL_DEBUG(DBG_LAYER_END, ip->ip_dst.s_addr,
936 ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len);
937
938 (*ip_protox[ip->ip_p]->pr_input)(m, hlen);
939 #if IPFIREWALL_FORWARD
940 ip_fw_fwd_addr = NULL; /* tcp needed it */
941 #endif
942 return;
943 }
944 bad:
945 #if IPFIREWALL_FORWARD
946 ip_fw_fwd_addr = NULL;
947 #endif
948 KERNEL_DEBUG(DBG_LAYER_END, 0,0,0,0,0);
949 m_freem(m);
950 }
951
952 /*
953 * IP software interrupt routine - to go away sometime soon
954 */
955 void
956 ipintr(void)
957 {
958 int s;
959 struct mbuf *m;
960
961 KERNEL_DEBUG(DBG_FNC_IP_INPUT | DBG_FUNC_START, 0,0,0,0,0);
962
963 while(1) {
964 s = splimp();
965 IF_DEQUEUE(&ipintrq, m);
966 splx(s);
967 if (m == 0) {
968 KERNEL_DEBUG(DBG_FNC_IP_INPUT | DBG_FUNC_END, 0,0,0,0,0);
969 return;
970 }
971
972 ip_input(m);
973 }
974 }
975
976 NETISR_SET(NETISR_IP, ipintr);
977
978 /*
979 * Take incoming datagram fragment and try to reassemble it into
980 * whole datagram. If a chain for reassembly of this datagram already
981 * exists, then it is given as fp; otherwise have to make a chain.
982 *
983 * When IPDIVERT enabled, keep additional state with each packet that
984 * tells us if we need to divert or tee the packet we're building.
985 */
986
987 static struct mbuf *
988 #if IPDIVERT
989 ip_reass(m, fp, where, divinfo, divcookie)
990 #else
991 ip_reass(m, fp, where)
992 #endif
993 register struct mbuf *m;
994 register struct ipq *fp;
995 struct ipq *where;
996 #if IPDIVERT
997 #ifdef IPDIVERT_44
998 u_int32_t *divinfo;
999 #else
1000 u_int16_t *divinfo;
1001 #endif
1002 u_int16_t *divcookie;
1003 #endif
1004 {
1005 struct ip *ip = mtod(m, struct ip *);
1006 register struct mbuf *p = 0, *q, *nq;
1007 struct mbuf *t;
1008 int hlen = IP_VHL_HL(ip->ip_vhl) << 2;
1009 int i, next;
1010
1011 /*
1012 * Presence of header sizes in mbufs
1013 * would confuse code below.
1014 */
1015 m->m_data += hlen;
1016 m->m_len -= hlen;
1017
1018 if (m->m_pkthdr.csum_flags & CSUM_TCP_SUM16)
1019 m->m_pkthdr.csum_flags = 0;
1020 /*
1021 * If first fragment to arrive, create a reassembly queue.
1022 */
1023 if (fp == 0) {
1024 /*
1025 * Enforce upper bound on number of fragmented packets
1026 * for which we attempt reassembly;
1027 * If maxfrag is 0, never accept fragments.
1028 * If maxfrag is -1, accept all fragments without limitation.
1029 */
1030 if ((ip_maxfragpackets >= 0) && (ip_nfragpackets >= ip_maxfragpackets))
1031 goto dropfrag;
1032 ip_nfragpackets++;
1033 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
1034 goto dropfrag;
1035 fp = mtod(t, struct ipq *);
1036 insque((void*)fp, (void*)where);
1037 nipq++;
1038 fp->ipq_ttl = IPFRAGTTL;
1039 fp->ipq_p = ip->ip_p;
1040 fp->ipq_id = ip->ip_id;
1041 fp->ipq_src = ip->ip_src;
1042 fp->ipq_dst = ip->ip_dst;
1043 fp->ipq_frags = m;
1044 m->m_nextpkt = NULL;
1045 #if IPDIVERT
1046 #ifdef IPDIVERT_44
1047 fp->ipq_div_info = 0;
1048 #else
1049 fp->ipq_divert = 0;
1050 #endif
1051 fp->ipq_div_cookie = 0;
1052 #endif
1053 goto inserted;
1054 }
1055
1056 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
1057
1058 /*
1059 * Find a segment which begins after this one does.
1060 */
1061 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
1062 if (GETIP(q)->ip_off > ip->ip_off)
1063 break;
1064
1065 /*
1066 * If there is a preceding segment, it may provide some of
1067 * our data already. If so, drop the data from the incoming
1068 * segment. If it provides all of our data, drop us, otherwise
1069 * stick new segment in the proper place.
1070 *
1071 * If some of the data is dropped from the the preceding
1072 * segment, then it's checksum is invalidated.
1073 */
1074 if (p) {
1075 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
1076 if (i > 0) {
1077 if (i >= ip->ip_len)
1078 goto dropfrag;
1079 m_adj(m, i);
1080 m->m_pkthdr.csum_flags = 0;
1081 ip->ip_off += i;
1082 ip->ip_len -= i;
1083 }
1084 m->m_nextpkt = p->m_nextpkt;
1085 p->m_nextpkt = m;
1086 } else {
1087 m->m_nextpkt = fp->ipq_frags;
1088 fp->ipq_frags = m;
1089 }
1090
1091 /*
1092 * While we overlap succeeding segments trim them or,
1093 * if they are completely covered, dequeue them.
1094 */
1095 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
1096 q = nq) {
1097 i = (ip->ip_off + ip->ip_len) -
1098 GETIP(q)->ip_off;
1099 if (i < GETIP(q)->ip_len) {
1100 GETIP(q)->ip_len -= i;
1101 GETIP(q)->ip_off += i;
1102 m_adj(q, i);
1103 q->m_pkthdr.csum_flags = 0;
1104 break;
1105 }
1106 nq = q->m_nextpkt;
1107 m->m_nextpkt = nq;
1108 m_freem(q);
1109 }
1110
1111 inserted:
1112
1113 #if IPDIVERT
1114 /*
1115 * Transfer firewall instructions to the fragment structure.
1116 * Any fragment diverting causes the whole packet to divert.
1117 */
1118 #ifdef IPDIVERT_44
1119 fp->ipq_div_info = *divinfo;
1120 #else
1121 fp->ipq_divert = *divinfo;
1122 #endif
1123 fp->ipq_div_cookie = *divcookie;
1124 *divinfo = 0;
1125 *divcookie = 0;
1126 #endif
1127
1128 /*
1129 * Check for complete reassembly.
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 return (0);
1135 next += GETIP(q)->ip_len;
1136 }
1137 /* Make sure the last packet didn't have the IP_MF flag */
1138 if (p->m_flags & M_FRAG)
1139 return (0);
1140
1141 /*
1142 * Reassembly is complete. Make sure the packet is a sane size.
1143 */
1144 q = fp->ipq_frags;
1145 ip = GETIP(q);
1146 if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) {
1147 ipstat.ips_toolong++;
1148 ip_freef(fp);
1149 return (0);
1150 }
1151
1152 /*
1153 * Concatenate fragments.
1154 */
1155 m = q;
1156 t = m->m_next;
1157 m->m_next = 0;
1158 m_cat(m, t);
1159 nq = q->m_nextpkt;
1160 q->m_nextpkt = 0;
1161 for (q = nq; q != NULL; q = nq) {
1162 nq = q->m_nextpkt;
1163 q->m_nextpkt = NULL;
1164 if (q->m_pkthdr.csum_flags & CSUM_TCP_SUM16)
1165 m->m_pkthdr.csum_flags = 0;
1166 else {
1167 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1168 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1169 }
1170 m_cat(m, q);
1171 }
1172
1173 #if IPDIVERT
1174 /*
1175 * Extract firewall instructions from the fragment structure.
1176 */
1177 #ifdef IPDIVERT_44
1178 *divinfo = fp->ipq_div_info;
1179 #else
1180 *divinfo = fp->ipq_divert;
1181 #endif
1182 *divcookie = fp->ipq_div_cookie;
1183 #endif
1184
1185 /*
1186 * Create header for new ip packet by
1187 * modifying header of first packet;
1188 * dequeue and discard fragment reassembly header.
1189 * Make header visible.
1190 */
1191 ip->ip_len = next;
1192 ip->ip_src = fp->ipq_src;
1193 ip->ip_dst = fp->ipq_dst;
1194 remque((void*)fp);
1195 nipq--;
1196 (void) m_free(dtom(fp));
1197 ip_nfragpackets--;
1198 m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2);
1199 m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2);
1200 /* some debugging cruft by sklower, below, will go away soon */
1201 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
1202 register int plen = 0;
1203 for (t = m; t; t = t->m_next)
1204 plen += t->m_len;
1205 m->m_pkthdr.len = plen;
1206 }
1207 return (m);
1208
1209 dropfrag:
1210 #if IPDIVERT
1211 *divinfo = 0;
1212 *divcookie = 0;
1213 #endif
1214 ipstat.ips_fragdropped++;
1215 m_freem(m);
1216 return (0);
1217
1218 #undef GETIP
1219 }
1220
1221 /*
1222 * Free a fragment reassembly header and all
1223 * associated datagrams.
1224 */
1225 static void
1226 ip_freef(fp)
1227 struct ipq *fp;
1228 {
1229 register struct mbuf *q;
1230
1231 while (fp->ipq_frags) {
1232 q = fp->ipq_frags;
1233 fp->ipq_frags = q->m_nextpkt;
1234 m_freem(q);
1235 }
1236 remque((void*)fp);
1237 (void) m_free(dtom(fp));
1238 ip_nfragpackets--;
1239 nipq--;
1240 }
1241
1242 /*
1243 * IP timer processing;
1244 * if a timer expires on a reassembly
1245 * queue, discard it.
1246 */
1247 void
1248 ip_slowtimo()
1249 {
1250 register struct ipq *fp;
1251 int s = splnet();
1252 int i;
1253
1254 for (i = 0; i < IPREASS_NHASH; i++) {
1255 fp = ipq[i].next;
1256 if (fp == 0)
1257 continue;
1258 while (fp != &ipq[i]) {
1259 --fp->ipq_ttl;
1260 fp = fp->next;
1261 if (fp->prev->ipq_ttl == 0) {
1262 ipstat.ips_fragtimeout++;
1263 ip_freef(fp->prev);
1264 }
1265 }
1266 }
1267 /*
1268 * If we are over the maximum number of fragments
1269 * (due to the limit being lowered), drain off
1270 * enough to get down to the new limit.
1271 */
1272 for (i = 0; i < IPREASS_NHASH; i++) {
1273 if (ip_maxfragpackets >= 0) {
1274 while ((ip_nfragpackets > ip_maxfragpackets) &&
1275 (ipq[i].next != &ipq[i])) {
1276 ipstat.ips_fragdropped++;
1277 ip_freef(ipq[i].next);
1278 }
1279 }
1280 }
1281 ipflow_slowtimo();
1282 splx(s);
1283 }
1284
1285 /*
1286 * Drain off all datagram fragments.
1287 */
1288 void
1289 ip_drain()
1290 {
1291 int i;
1292
1293 for (i = 0; i < IPREASS_NHASH; i++) {
1294 while (ipq[i].next != &ipq[i]) {
1295 ipstat.ips_fragdropped++;
1296 ip_freef(ipq[i].next);
1297 }
1298 }
1299 in_rtqdrain();
1300 }
1301
1302 /*
1303 * Do option processing on a datagram,
1304 * possibly discarding it if bad options are encountered,
1305 * or forwarding it if source-routed.
1306 * Returns 1 if packet has been forwarded/freed,
1307 * 0 if the packet should be processed further.
1308 */
1309 static int
1310 ip_dooptions(m)
1311 struct mbuf *m;
1312 {
1313 register struct ip *ip = mtod(m, struct ip *);
1314 register u_char *cp;
1315 register struct ip_timestamp *ipt;
1316 register struct in_ifaddr *ia;
1317 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1318 struct in_addr *sin, dst;
1319 n_time ntime;
1320
1321 dst = ip->ip_dst;
1322 cp = (u_char *)(ip + 1);
1323 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1324 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1325 opt = cp[IPOPT_OPTVAL];
1326 if (opt == IPOPT_EOL)
1327 break;
1328 if (opt == IPOPT_NOP)
1329 optlen = 1;
1330 else {
1331 if (cnt < IPOPT_OLEN + sizeof(*cp)) {
1332 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1333 goto bad;
1334 }
1335 optlen = cp[IPOPT_OLEN];
1336 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1337 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1338 goto bad;
1339 }
1340 }
1341 switch (opt) {
1342
1343 default:
1344 break;
1345
1346 /*
1347 * Source routing with record.
1348 * Find interface with current destination address.
1349 * If none on this machine then drop if strictly routed,
1350 * or do nothing if loosely routed.
1351 * Record interface address and bring up next address
1352 * component. If strictly routed make sure next
1353 * address is on directly accessible net.
1354 */
1355 case IPOPT_LSRR:
1356 case IPOPT_SSRR:
1357 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1358 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1359 goto bad;
1360 }
1361 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1362 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1363 goto bad;
1364 }
1365 ipaddr.sin_addr = ip->ip_dst;
1366 ia = (struct in_ifaddr *)
1367 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
1368 if (ia == 0) {
1369 if (opt == IPOPT_SSRR) {
1370 type = ICMP_UNREACH;
1371 code = ICMP_UNREACH_SRCFAIL;
1372 goto bad;
1373 }
1374 if (!ip_dosourceroute)
1375 goto nosourcerouting;
1376 /*
1377 * Loose routing, and not at next destination
1378 * yet; nothing to do except forward.
1379 */
1380 break;
1381 }
1382 off--; /* 0 origin */
1383 if (off > optlen - (int)sizeof(struct in_addr)) {
1384 /*
1385 * End of source route. Should be for us.
1386 */
1387 if (!ip_acceptsourceroute)
1388 goto nosourcerouting;
1389 save_rte(cp, ip->ip_src);
1390 break;
1391 }
1392
1393 if (!ip_dosourceroute) {
1394 if (ipforwarding) {
1395 char buf[16]; /* aaa.bbb.ccc.ddd\0 */
1396 /*
1397 * Acting as a router, so generate ICMP
1398 */
1399 nosourcerouting:
1400 strcpy(buf, inet_ntoa(ip->ip_dst));
1401 log(LOG_WARNING,
1402 "attempted source route from %s to %s\n",
1403 inet_ntoa(ip->ip_src), buf);
1404 type = ICMP_UNREACH;
1405 code = ICMP_UNREACH_SRCFAIL;
1406 goto bad;
1407 } else {
1408 /*
1409 * Not acting as a router, so silently drop.
1410 */
1411 ipstat.ips_cantforward++;
1412 m_freem(m);
1413 return (1);
1414 }
1415 }
1416
1417 /*
1418 * locate outgoing interface
1419 */
1420 (void)memcpy(&ipaddr.sin_addr, cp + off,
1421 sizeof(ipaddr.sin_addr));
1422
1423 if (opt == IPOPT_SSRR) {
1424 #define INA struct in_ifaddr *
1425 #define SA struct sockaddr *
1426 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
1427 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
1428 } else
1429 ia = ip_rtaddr(ipaddr.sin_addr);
1430 if (ia == 0) {
1431 type = ICMP_UNREACH;
1432 code = ICMP_UNREACH_SRCFAIL;
1433 goto bad;
1434 }
1435 ip->ip_dst = ipaddr.sin_addr;
1436 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1437 sizeof(struct in_addr));
1438 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1439 /*
1440 * Let ip_intr's mcast routing check handle mcast pkts
1441 */
1442 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
1443 break;
1444
1445 case IPOPT_RR:
1446 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1447 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1448 goto bad;
1449 }
1450 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1451 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1452 goto bad;
1453 }
1454 /*
1455 * If no space remains, ignore.
1456 */
1457 off--; /* 0 origin */
1458 if (off > optlen - (int)sizeof(struct in_addr))
1459 break;
1460 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
1461 sizeof(ipaddr.sin_addr));
1462 /*
1463 * locate outgoing interface; if we're the destination,
1464 * use the incoming interface (should be same).
1465 */
1466 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
1467 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
1468 type = ICMP_UNREACH;
1469 code = ICMP_UNREACH_HOST;
1470 goto bad;
1471 }
1472 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1473 sizeof(struct in_addr));
1474 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1475 break;
1476
1477 case IPOPT_TS:
1478 code = cp - (u_char *)ip;
1479 ipt = (struct ip_timestamp *)cp;
1480 if (ipt->ipt_len < 4 || ipt->ipt_len > 40) {
1481 code = (u_char *)&ipt->ipt_len - (u_char *)ip;
1482 goto bad;
1483 }
1484 if (ipt->ipt_ptr < 5) {
1485 code = (u_char *)&ipt->ipt_ptr - (u_char *)ip;
1486 goto bad;
1487 }
1488 if (ipt->ipt_ptr >
1489 ipt->ipt_len - (int)sizeof(int32_t)) {
1490 if (++ipt->ipt_oflw == 0) {
1491 code = (u_char *)&ipt->ipt_ptr -
1492 (u_char *)ip;
1493 goto bad;
1494 }
1495 break;
1496 }
1497 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
1498 switch (ipt->ipt_flg) {
1499
1500 case IPOPT_TS_TSONLY:
1501 break;
1502
1503 case IPOPT_TS_TSANDADDR:
1504 if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1505 sizeof(struct in_addr) > ipt->ipt_len) {
1506 code = (u_char *)&ipt->ipt_ptr -
1507 (u_char *)ip;
1508 goto bad;
1509 }
1510 ipaddr.sin_addr = dst;
1511 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
1512 m->m_pkthdr.rcvif);
1513 if (ia == 0)
1514 continue;
1515 (void)memcpy(sin, &IA_SIN(ia)->sin_addr,
1516 sizeof(struct in_addr));
1517 ipt->ipt_ptr += sizeof(struct in_addr);
1518 break;
1519
1520 case IPOPT_TS_PRESPEC:
1521 if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1522 sizeof(struct in_addr) > ipt->ipt_len) {
1523 code = (u_char *)&ipt->ipt_ptr -
1524 (u_char *)ip;
1525 goto bad;
1526 }
1527 (void)memcpy(&ipaddr.sin_addr, sin,
1528 sizeof(struct in_addr));
1529 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
1530 continue;
1531 ipt->ipt_ptr += sizeof(struct in_addr);
1532 break;
1533
1534 default:
1535 /* XXX can't take &ipt->ipt_flg */
1536 code = (u_char *)&ipt->ipt_ptr -
1537 (u_char *)ip + 1;
1538 goto bad;
1539 }
1540 ntime = iptime();
1541 (void)memcpy(cp + ipt->ipt_ptr - 1, &ntime,
1542 sizeof(n_time));
1543 ipt->ipt_ptr += sizeof(n_time);
1544 }
1545 }
1546 if (forward && ipforwarding) {
1547 ip_forward(m, 1);
1548 return (1);
1549 }
1550 return (0);
1551 bad:
1552 ip->ip_len -= IP_VHL_HL(ip->ip_vhl) << 2; /* XXX icmp_error adds in hdr length */
1553 icmp_error(m, type, code, 0, 0);
1554 ipstat.ips_badoptions++;
1555 return (1);
1556 }
1557
1558 /*
1559 * Given address of next destination (final or next hop),
1560 * return internet address info of interface to be used to get there.
1561 */
1562 static struct in_ifaddr *
1563 ip_rtaddr(dst)
1564 struct in_addr dst;
1565 {
1566 register struct sockaddr_in *sin;
1567
1568 sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
1569
1570 if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
1571 if (ipforward_rt.ro_rt) {
1572 rtfree(ipforward_rt.ro_rt);
1573 ipforward_rt.ro_rt = 0;
1574 }
1575 sin->sin_family = AF_INET;
1576 sin->sin_len = sizeof(*sin);
1577 sin->sin_addr = dst;
1578
1579 rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1580 }
1581 if (ipforward_rt.ro_rt == 0)
1582 return ((struct in_ifaddr *)0);
1583 return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
1584 }
1585
1586 /*
1587 * Save incoming source route for use in replies,
1588 * to be picked up later by ip_srcroute if the receiver is interested.
1589 */
1590 void
1591 save_rte(option, dst)
1592 u_char *option;
1593 struct in_addr dst;
1594 {
1595 unsigned olen;
1596
1597 olen = option[IPOPT_OLEN];
1598 #if DIAGNOSTIC
1599 if (ipprintfs)
1600 printf("save_rte: olen %d\n", olen);
1601 #endif
1602 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1603 return;
1604 bcopy(option, ip_srcrt.srcopt, olen);
1605 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1606 ip_srcrt.dst = dst;
1607 }
1608
1609 /*
1610 * Retrieve incoming source route for use in replies,
1611 * in the same form used by setsockopt.
1612 * The first hop is placed before the options, will be removed later.
1613 */
1614 struct mbuf *
1615 ip_srcroute()
1616 {
1617 register struct in_addr *p, *q;
1618 register struct mbuf *m;
1619
1620 if (ip_nhops == 0)
1621 return ((struct mbuf *)0);
1622 m = m_get(M_DONTWAIT, MT_HEADER);
1623 if (m == 0)
1624 return ((struct mbuf *)0);
1625
1626 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1627
1628 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1629 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1630 OPTSIZ;
1631 #if DIAGNOSTIC
1632 if (ipprintfs)
1633 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1634 #endif
1635
1636 /*
1637 * First save first hop for return route
1638 */
1639 p = &ip_srcrt.route[ip_nhops - 1];
1640 *(mtod(m, struct in_addr *)) = *p--;
1641 #if DIAGNOSTIC
1642 if (ipprintfs)
1643 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr));
1644 #endif
1645
1646 /*
1647 * Copy option fields and padding (nop) to mbuf.
1648 */
1649 ip_srcrt.nop = IPOPT_NOP;
1650 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1651 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
1652 &ip_srcrt.nop, OPTSIZ);
1653 q = (struct in_addr *)(mtod(m, caddr_t) +
1654 sizeof(struct in_addr) + OPTSIZ);
1655 #undef OPTSIZ
1656 /*
1657 * Record return path as an IP source route,
1658 * reversing the path (pointers are now aligned).
1659 */
1660 while (p >= ip_srcrt.route) {
1661 #if DIAGNOSTIC
1662 if (ipprintfs)
1663 printf(" %lx", (u_long)ntohl(q->s_addr));
1664 #endif
1665 *q++ = *p--;
1666 }
1667 /*
1668 * Last hop goes to final destination.
1669 */
1670 *q = ip_srcrt.dst;
1671 #if DIAGNOSTIC
1672 if (ipprintfs)
1673 printf(" %lx\n", (u_long)ntohl(q->s_addr));
1674 #endif
1675 return (m);
1676 }
1677
1678 /*
1679 * Strip out IP options, at higher
1680 * level protocol in the kernel.
1681 * Second argument is buffer to which options
1682 * will be moved, and return value is their length.
1683 * XXX should be deleted; last arg currently ignored.
1684 */
1685 void
1686 ip_stripoptions(m, mopt)
1687 register struct mbuf *m;
1688 struct mbuf *mopt;
1689 {
1690 register int i;
1691 struct ip *ip = mtod(m, struct ip *);
1692 register caddr_t opts;
1693 int olen;
1694
1695 olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1696 opts = (caddr_t)(ip + 1);
1697 i = m->m_len - (sizeof (struct ip) + olen);
1698 bcopy(opts + olen, opts, (unsigned)i);
1699 m->m_len -= olen;
1700 if (m->m_flags & M_PKTHDR)
1701 m->m_pkthdr.len -= olen;
1702 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
1703 }
1704
1705 u_char inetctlerrmap[PRC_NCMDS] = {
1706 0, 0, 0, 0,
1707 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1708 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1709 EMSGSIZE, EHOSTUNREACH, 0, 0,
1710 0, 0, 0, 0,
1711 ENOPROTOOPT, ECONNREFUSED
1712 };
1713
1714 /*
1715 * Forward a packet. If some error occurs return the sender
1716 * an icmp packet. Note we can't always generate a meaningful
1717 * icmp message because icmp doesn't have a large enough repertoire
1718 * of codes and types.
1719 *
1720 * If not forwarding, just drop the packet. This could be confusing
1721 * if ipforwarding was zero but some routing protocol was advancing
1722 * us as a gateway to somewhere. However, we must let the routing
1723 * protocol deal with that.
1724 *
1725 * The srcrt parameter indicates whether the packet is being forwarded
1726 * via a source route.
1727 */
1728 static void
1729 ip_forward(m, srcrt)
1730 struct mbuf *m;
1731 int srcrt;
1732 {
1733 register struct ip *ip = mtod(m, struct ip *);
1734 register struct sockaddr_in *sin;
1735 register struct rtentry *rt;
1736 int error, type = 0, code = 0;
1737 struct mbuf *mcopy;
1738 n_long dest;
1739 struct ifnet *destifp;
1740 #if IPSEC
1741 struct ifnet dummyifp;
1742 #endif
1743
1744 dest = 0;
1745 #if DIAGNOSTIC
1746 if (ipprintfs)
1747 printf("forward: src %lx dst %lx ttl %x\n",
1748 (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr,
1749 ip->ip_ttl);
1750 #endif
1751
1752
1753 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1754 ipstat.ips_cantforward++;
1755 m_freem(m);
1756 return;
1757 }
1758 #if IPSTEALTH
1759 if (!ipstealth) {
1760 #endif
1761 if (ip->ip_ttl <= IPTTLDEC) {
1762 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
1763 dest, 0);
1764 return;
1765 }
1766 #if IPSTEALTH
1767 }
1768 #endif
1769
1770 sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
1771 if ((rt = ipforward_rt.ro_rt) == 0 ||
1772 ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
1773 if (ipforward_rt.ro_rt) {
1774 rtfree(ipforward_rt.ro_rt);
1775 ipforward_rt.ro_rt = 0;
1776 }
1777 sin->sin_family = AF_INET;
1778 sin->sin_len = sizeof(*sin);
1779 sin->sin_addr = ip->ip_dst;
1780
1781 rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1782 if (ipforward_rt.ro_rt == 0) {
1783 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1784 return;
1785 }
1786 rt = ipforward_rt.ro_rt;
1787 }
1788
1789 /*
1790 * Save the IP header and at most 8 bytes of the payload,
1791 * in case we need to generate an ICMP message to the src.
1792 *
1793 * We don't use m_copy() because it might return a reference
1794 * to a shared cluster. Both this function and ip_output()
1795 * assume exclusive access to the IP header in `m', so any
1796 * data in a cluster may change before we reach icmp_error().
1797 */
1798 MGET(mcopy, M_DONTWAIT, m->m_type);
1799 if (mcopy != NULL) {
1800 M_COPY_PKTHDR(mcopy, m);
1801 mcopy->m_len = imin((IP_VHL_HL(ip->ip_vhl) << 2) + 8,
1802 (int)ip->ip_len);
1803 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1804 }
1805
1806 #if IPSTEALTH
1807 if (!ipstealth) {
1808 #endif
1809 ip->ip_ttl -= IPTTLDEC;
1810 #if IPSTEALTH
1811 }
1812 #endif
1813
1814 /*
1815 * If forwarding packet using same interface that it came in on,
1816 * perhaps should send a redirect to sender to shortcut a hop.
1817 * Only send redirect if source is sending directly to us,
1818 * and if packet was not source routed (or has any options).
1819 * Also, don't send redirect if forwarding using a default route
1820 * or a route modified by a redirect.
1821 */
1822 #define satosin(sa) ((struct sockaddr_in *)(sa))
1823 if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1824 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1825 satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
1826 ipsendredirects && !srcrt) {
1827 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1828 u_long src = ntohl(ip->ip_src.s_addr);
1829
1830 if (RTA(rt) &&
1831 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1832 if (rt->rt_flags & RTF_GATEWAY)
1833 dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1834 else
1835 dest = ip->ip_dst.s_addr;
1836 /* Router requirements says to only send host redirects */
1837 type = ICMP_REDIRECT;
1838 code = ICMP_REDIRECT_HOST;
1839 #if DIAGNOSTIC
1840 if (ipprintfs)
1841 printf("redirect (%d) to %lx\n", code, (u_long)dest);
1842 #endif
1843 }
1844 }
1845
1846 error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1847 IP_FORWARDING, 0);
1848 if (error)
1849 ipstat.ips_cantforward++;
1850 else {
1851 ipstat.ips_forward++;
1852 if (type)
1853 ipstat.ips_redirectsent++;
1854 else {
1855 if (mcopy) {
1856 ipflow_create(&ipforward_rt, mcopy);
1857 m_freem(mcopy);
1858 }
1859 return;
1860 }
1861 }
1862 if (mcopy == NULL)
1863 return;
1864 destifp = NULL;
1865
1866 switch (error) {
1867
1868 case 0: /* forwarded, but need redirect */
1869 /* type, code set above */
1870 break;
1871
1872 case ENETUNREACH: /* shouldn't happen, checked above */
1873 case EHOSTUNREACH:
1874 case ENETDOWN:
1875 case EHOSTDOWN:
1876 default:
1877 type = ICMP_UNREACH;
1878 code = ICMP_UNREACH_HOST;
1879 break;
1880
1881 case EMSGSIZE:
1882 type = ICMP_UNREACH;
1883 code = ICMP_UNREACH_NEEDFRAG;
1884 #ifndef IPSEC
1885 if (ipforward_rt.ro_rt)
1886 destifp = ipforward_rt.ro_rt->rt_ifp;
1887 #else
1888 /*
1889 * If the packet is routed over IPsec tunnel, tell the
1890 * originator the tunnel MTU.
1891 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1892 * XXX quickhack!!!
1893 */
1894 if (ipforward_rt.ro_rt) {
1895 struct secpolicy *sp = NULL;
1896 int ipsecerror;
1897 int ipsechdr;
1898 struct route *ro;
1899
1900 if (ipsec_bypass) {
1901 destifp = ipforward_rt.ro_rt->rt_ifp;
1902 ipstat.ips_cantfrag++;
1903 break;
1904 }
1905
1906 sp = ipsec4_getpolicybyaddr(mcopy,
1907 IPSEC_DIR_OUTBOUND,
1908 IP_FORWARDING,
1909 &ipsecerror);
1910
1911 if (sp == NULL)
1912 destifp = ipforward_rt.ro_rt->rt_ifp;
1913 else {
1914 /* count IPsec header size */
1915 ipsechdr = ipsec4_hdrsiz(mcopy,
1916 IPSEC_DIR_OUTBOUND,
1917 NULL);
1918
1919 /*
1920 * find the correct route for outer IPv4
1921 * header, compute tunnel MTU.
1922 *
1923 * XXX BUG ALERT
1924 * The "dummyifp" code relies upon the fact
1925 * that icmp_error() touches only ifp->if_mtu.
1926 */
1927 /*XXX*/
1928 destifp = NULL;
1929 if (sp->req != NULL
1930 && sp->req->sav != NULL
1931 && sp->req->sav->sah != NULL) {
1932 ro = &sp->req->sav->sah->sa_route;
1933 if (ro->ro_rt && ro->ro_rt->rt_ifp) {
1934 dummyifp.if_mtu =
1935 ro->ro_rt->rt_ifp->if_mtu;
1936 dummyifp.if_mtu -= ipsechdr;
1937 destifp = &dummyifp;
1938 }
1939 }
1940
1941 key_freesp(sp);
1942 }
1943 }
1944 #endif /*IPSEC*/
1945 ipstat.ips_cantfrag++;
1946 break;
1947
1948 case ENOBUFS:
1949 type = ICMP_SOURCEQUENCH;
1950 code = 0;
1951 break;
1952
1953 case EACCES: /* ipfw denied packet */
1954 m_freem(mcopy);
1955 return;
1956 }
1957 icmp_error(mcopy, type, code, dest, destifp);
1958 }
1959
1960 void
1961 ip_savecontrol(inp, mp, ip, m)
1962 register struct inpcb *inp;
1963 register struct mbuf **mp;
1964 register struct ip *ip;
1965 register struct mbuf *m;
1966 {
1967 if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1968 struct timeval tv;
1969
1970 microtime(&tv);
1971 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1972 SCM_TIMESTAMP, SOL_SOCKET);
1973 if (*mp)
1974 mp = &(*mp)->m_next;
1975 }
1976 if (inp->inp_flags & INP_RECVDSTADDR) {
1977 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1978 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1979 if (*mp)
1980 mp = &(*mp)->m_next;
1981 }
1982 #ifdef notyet
1983 /* XXX
1984 * Moving these out of udp_input() made them even more broken
1985 * than they already were.
1986 */
1987 /* options were tossed already */
1988 if (inp->inp_flags & INP_RECVOPTS) {
1989 *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1990 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1991 if (*mp)
1992 mp = &(*mp)->m_next;
1993 }
1994 /* ip_srcroute doesn't do what we want here, need to fix */
1995 if (inp->inp_flags & INP_RECVRETOPTS) {
1996 *mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1997 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1998 if (*mp)
1999 mp = &(*mp)->m_next;
2000 }
2001 #endif
2002 if (inp->inp_flags & INP_RECVIF) {
2003 struct ifnet *ifp;
2004 struct sdlbuf {
2005 struct sockaddr_dl sdl;
2006 u_char pad[32];
2007 } sdlbuf;
2008 struct sockaddr_dl *sdp;
2009 struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
2010
2011 if (((ifp = m->m_pkthdr.rcvif))
2012 && ( ifp->if_index && (ifp->if_index <= if_index))) {
2013 sdp = (struct sockaddr_dl *)(ifnet_addrs
2014 [ifp->if_index - 1]->ifa_addr);
2015 /*
2016 * Change our mind and don't try copy.
2017 */
2018 if ((sdp->sdl_family != AF_LINK)
2019 || (sdp->sdl_len > sizeof(sdlbuf))) {
2020 goto makedummy;
2021 }
2022 bcopy(sdp, sdl2, sdp->sdl_len);
2023 } else {
2024 makedummy:
2025 sdl2->sdl_len
2026 = offsetof(struct sockaddr_dl, sdl_data[0]);
2027 sdl2->sdl_family = AF_LINK;
2028 sdl2->sdl_index = 0;
2029 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
2030 }
2031 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
2032 IP_RECVIF, IPPROTO_IP);
2033 if (*mp)
2034 mp = &(*mp)->m_next;
2035 }
2036 }
2037
2038 int
2039 ip_rsvp_init(struct socket *so)
2040 {
2041 if (so->so_type != SOCK_RAW ||
2042 so->so_proto->pr_protocol != IPPROTO_RSVP)
2043 return EOPNOTSUPP;
2044
2045 if (ip_rsvpd != NULL)
2046 return EADDRINUSE;
2047
2048 ip_rsvpd = so;
2049 /*
2050 * This may seem silly, but we need to be sure we don't over-increment
2051 * the RSVP counter, in case something slips up.
2052 */
2053 if (!ip_rsvp_on) {
2054 ip_rsvp_on = 1;
2055 rsvp_on++;
2056 }
2057
2058 return 0;
2059 }
2060
2061 int
2062 ip_rsvp_done(void)
2063 {
2064 ip_rsvpd = NULL;
2065 /*
2066 * This may seem silly, but we need to be sure we don't over-decrement
2067 * the RSVP counter, in case something slips up.
2068 */
2069 if (ip_rsvp_on) {
2070 ip_rsvp_on = 0;
2071 rsvp_on--;
2072 }
2073 return 0;
2074 }