]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_input.c
9ce6d6f1c0858750d02aa97cc05872db123f36cd
[apple/xnu.git] / bsd / netinet / ip_input.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1982, 1986, 1988, 1993
24 * The Regents of the University of California. All rights reserved.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 *
54 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
55 * $FreeBSD: src/sys/netinet/ip_input.c,v 1.130.2.25 2001/08/29 21:41:37 jesper Exp $
56 */
57
58 #define _IP_VHL
59
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/mbuf.h>
63 #include <sys/malloc.h>
64 #include <sys/domain.h>
65 #include <sys/protosw.h>
66 #include <sys/socket.h>
67 #include <sys/time.h>
68 #include <sys/kernel.h>
69 #include <sys/syslog.h>
70 #include <sys/sysctl.h>
71
72 #include <kern/queue.h>
73
74 #include <net/if.h>
75 #include <net/if_var.h>
76 #include <net/if_dl.h>
77 #include <net/route.h>
78 #include <net/netisr.h>
79
80 #include <netinet/in.h>
81 #include <netinet/in_systm.h>
82 #include <netinet/in_var.h>
83 #include <netinet/ip.h>
84 #include <netinet/in_pcb.h>
85 #include <netinet/ip_var.h>
86 #include <netinet/ip_icmp.h>
87 #include <sys/socketvar.h>
88
89 #include <netinet/ip_fw.h>
90
91 /* needed for AUTOCONFIGURING: */
92 #include <netinet/udp.h>
93 #include <netinet/udp_var.h>
94 #include <netinet/bootp.h>
95
96 #include <sys/kdebug.h>
97
98 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 0)
99 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 2)
100 #define DBG_FNC_IP_INPUT NETDBG_CODE(DBG_NETIP, (2 << 8))
101
102
103 #if IPSEC
104 #include <netinet6/ipsec.h>
105 #include <netkey/key.h>
106 #endif
107
108 #include "faith.h"
109 #if defined(NFAITH) && NFAITH > 0
110 #include <net/if_types.h>
111 #endif
112
113 #if DUMMYNET
114 #include <netinet/ip_dummynet.h>
115 #endif
116
117 #if IPSEC
118 extern int ipsec_bypass;
119 #endif
120
121 int rsvp_on = 0;
122 static int ip_rsvp_on;
123 struct socket *ip_rsvpd;
124
125 int ipforwarding = 0;
126 SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
127 &ipforwarding, 0, "Enable IP forwarding between interfaces");
128
129 static int ipsendredirects = 1; /* XXX */
130 SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
131 &ipsendredirects, 0, "Enable sending IP redirects");
132
133 int ip_defttl = IPDEFTTL;
134 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
135 &ip_defttl, 0, "Maximum TTL on IP packets");
136
137 static int ip_dosourceroute = 0;
138 SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
139 &ip_dosourceroute, 0, "Enable forwarding source routed IP packets");
140
141 static int ip_acceptsourceroute = 0;
142 SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
143 CTLFLAG_RW, &ip_acceptsourceroute, 0,
144 "Enable accepting source routed IP packets");
145
146 static int ip_keepfaith = 0;
147 SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
148 &ip_keepfaith, 0,
149 "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
150
151 static int ip_nfragpackets = 0;
152 static int ip_maxfragpackets; /* initialized in ip_init() */
153 SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragpackets, CTLFLAG_RW,
154 &ip_maxfragpackets, 0,
155 "Maximum number of IPv4 fragment reassembly queue entries");
156
157 /*
158 * XXX - Setting ip_checkinterface mostly implements the receive side of
159 * the Strong ES model described in RFC 1122, but since the routing table
160 * and transmit implementation do not implement the Strong ES model,
161 * setting this to 1 results in an odd hybrid.
162 *
163 * XXX - ip_checkinterface currently must be disabled if you use ipnat
164 * to translate the destination address to another local interface.
165 *
166 * XXX - ip_checkinterface must be disabled if you add IP aliases
167 * to the loopback interface instead of the interface where the
168 * packets for those addresses are received.
169 */
170 static int ip_checkinterface = 0;
171 SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
172 &ip_checkinterface, 0, "Verify packet arrives on correct interface");
173
174 #if DIAGNOSTIC
175 static int ipprintfs = 0;
176 #endif
177
178 extern struct domain inetdomain;
179 extern struct protosw inetsw[];
180 struct protosw *ip_protox[IPPROTO_MAX];
181 static int ipqmaxlen = IFQ_MAXLEN;
182 struct in_ifaddrhead in_ifaddrhead; /* first inet address */
183 struct ifqueue ipintrq;
184 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
185 &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
186 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
187 &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue");
188
189 struct ipstat ipstat;
190 SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RD,
191 &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
192
193 /* Packet reassembly stuff */
194 #define IPREASS_NHASH_LOG2 6
195 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
196 #define IPREASS_HMASK (IPREASS_NHASH - 1)
197 #define IPREASS_HASH(x,y) \
198 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
199
200 static struct ipq ipq[IPREASS_NHASH];
201 static int nipq = 0; /* total # of reass queues */
202 static int maxnipq;
203 const int ipintrq_present = 1;
204
205 #if IPCTL_DEFMTU
206 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
207 &ip_mtu, 0, "Default MTU");
208 #endif
209
210 #if IPSTEALTH
211 static int ipstealth = 0;
212 SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
213 &ipstealth, 0, "");
214 #endif
215
216
217 /* Firewall hooks */
218 ip_fw_chk_t *ip_fw_chk_ptr;
219 ip_fw_ctl_t *ip_fw_ctl_ptr;
220 int fw_enable = 1 ;
221
222 #if DUMMYNET
223 ip_dn_ctl_t *ip_dn_ctl_ptr;
224 #endif
225
226 int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **)) = NULL;
227
228 SYSCTL_NODE(_net_inet_ip, OID_AUTO, linklocal, CTLFLAG_RW, 0, "link local");
229
230 struct ip_linklocal_stat ip_linklocal_stat;
231 SYSCTL_STRUCT(_net_inet_ip_linklocal, OID_AUTO, stat, CTLFLAG_RD,
232 &ip_linklocal_stat, ip_linklocal_stat,
233 "Number of link local packets with TTL less than 255");
234
235 SYSCTL_NODE(_net_inet_ip_linklocal, OID_AUTO, in, CTLFLAG_RW, 0, "link local input");
236
237 int ip_linklocal_in_allowbadttl = 0;
238 SYSCTL_INT(_net_inet_ip_linklocal_in, OID_AUTO, allowbadttl, CTLFLAG_RW,
239 &ip_linklocal_in_allowbadttl, 0,
240 "Allow incoming link local packets with TTL less than 255");
241
242
243 /*
244 * We need to save the IP options in case a protocol wants to respond
245 * to an incoming packet over the same route if the packet got here
246 * using IP source routing. This allows connection establishment and
247 * maintenance when the remote end is on a network that is not known
248 * to us.
249 */
250 static int ip_nhops = 0;
251 static struct ip_srcrt {
252 struct in_addr dst; /* final destination */
253 char nop; /* one NOP to align */
254 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
255 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
256 } ip_srcrt;
257
258 struct sockaddr_in *ip_fw_fwd_addr;
259
260 #ifdef __APPLE__
261 extern struct mbuf* m_dup(register struct mbuf *m, int how);
262 #endif
263
264 static void save_rte __P((u_char *, struct in_addr));
265 static int ip_dooptions __P((struct mbuf *));
266 static void ip_forward __P((struct mbuf *, int));
267 static void ip_freef __P((struct ipq *));
268 #if IPDIVERT
269 #ifdef IPDIVERT_44
270 static struct mbuf *ip_reass __P((struct mbuf *,
271 struct ipq *, struct ipq *, u_int32_t *, u_int16_t *));
272 #else
273 static struct mbuf *ip_reass __P((struct mbuf *,
274 struct ipq *, struct ipq *, u_int16_t *, u_int16_t *));
275 #endif
276 #else
277 static struct mbuf *ip_reass __P((struct mbuf *, struct ipq *, struct ipq *));
278 #endif
279 static struct in_ifaddr *ip_rtaddr __P((struct in_addr));
280 void ipintr __P((void));
281
282 #if RANDOM_IP_ID
283 extern u_short ip_id;
284 #endif
285
286 extern u_long route_generation;
287 extern int apple_hwcksum_rx;
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_int16_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 ipstat.ips_badaddr++;
436 goto bad;
437 }
438 }
439
440 /* IPv4 Link-Local Addresses as defined in <draft-ietf-zeroconf-ipv4-linklocal-05.txt> */
441 if ((IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) ||
442 IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)))) {
443 ip_linklocal_stat.iplls_in_total++;
444 if (ip->ip_ttl != MAXTTL) {
445 ip_linklocal_stat.iplls_in_badttl++;
446 /* Silently drop link local traffic with bad TTL */
447 if (ip_linklocal_in_allowbadttl != 0)
448 goto bad;
449 }
450 }
451 if ((IF_HWASSIST_CSUM_FLAGS(m->m_pkthdr.rcvif->if_hwassist) == 0)
452 || (apple_hwcksum_rx == 0) ||
453 ((m->m_pkthdr.csum_flags & CSUM_TCP_SUM16) && ip->ip_p != IPPROTO_TCP))
454 m->m_pkthdr.csum_flags = 0; /* invalidate HW generated checksum flags */
455
456 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
457 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
458 } else {
459 sum = in_cksum(m, hlen);
460 }
461 if (sum) {
462 ipstat.ips_badsum++;
463 goto bad;
464 }
465
466 /*
467 * Convert fields to host representation.
468 */
469 NTOHS(ip->ip_len);
470 if (ip->ip_len < hlen) {
471 ipstat.ips_badlen++;
472 goto bad;
473 }
474 NTOHS(ip->ip_off);
475
476 /*
477 * Check that the amount of data in the buffers
478 * is as at least much as the IP header would have us expect.
479 * Trim mbufs if longer than we expect.
480 * Drop packet if shorter than we expect.
481 */
482 if (m->m_pkthdr.len < ip->ip_len) {
483 tooshort:
484 ipstat.ips_tooshort++;
485 goto bad;
486 }
487 if (m->m_pkthdr.len > ip->ip_len) {
488 /* Invalidate hwcksuming */
489 m->m_pkthdr.csum_flags = 0;
490 m->m_pkthdr.csum_data = 0;
491
492 if (m->m_len == m->m_pkthdr.len) {
493 m->m_len = ip->ip_len;
494 m->m_pkthdr.len = ip->ip_len;
495 } else
496 m_adj(m, ip->ip_len - m->m_pkthdr.len);
497 }
498
499 #if IPSEC
500 if (ipsec_bypass == 0 && ipsec_gethist(m, NULL))
501 goto pass;
502 #endif
503
504 /*
505 * IpHack's section.
506 * Right now when no processing on packet has done
507 * and it is still fresh out of network we do our black
508 * deals with it.
509 * - Firewall: deny/allow/divert
510 * - Xlate: translate packet's addr/port (NAT).
511 * - Pipe: pass pkt through dummynet.
512 * - Wrap: fake packet's addr/port <unimpl.>
513 * - Encapsulate: put it in another IP and send out. <unimp.>
514 */
515
516 #if defined(IPFIREWALL) && defined(DUMMYNET)
517 iphack:
518 #endif
519 /*
520 * Check if we want to allow this packet to be processed.
521 * Consider it to be bad if not.
522 */
523 if (fr_checkp) {
524 struct mbuf *m1 = m;
525
526 if ((*fr_checkp)(ip, hlen, m->m_pkthdr.rcvif, 0, &m1) || !m1)
527 return;
528 ip = mtod(m = m1, struct ip *);
529 }
530 if (fw_enable && ip_fw_chk_ptr) {
531 #if IPFIREWALL_FORWARD
532 /*
533 * If we've been forwarded from the output side, then
534 * skip the firewall a second time
535 */
536 if (ip_fw_fwd_addr)
537 goto ours;
538 #endif /* IPFIREWALL_FORWARD */
539 /*
540 * See the comment in ip_output for the return values
541 * produced by the firewall.
542 */
543 i = (*ip_fw_chk_ptr)(&ip,
544 hlen, NULL, &divert_cookie, &m, &rule, &ip_fw_fwd_addr);
545 if ( (i & IP_FW_PORT_DENY_FLAG) || m == NULL) { /* drop */
546 if (m)
547 m_freem(m);
548 return;
549 }
550 ip = mtod(m, struct ip *); /* just in case m changed */
551 if (i == 0 && ip_fw_fwd_addr == NULL) /* common case */
552 goto pass;
553 #if DUMMYNET
554 if ((i & IP_FW_PORT_DYNT_FLAG) != 0) {
555 /* send packet to the appropriate pipe */
556 dummynet_io(i&0xffff,DN_TO_IP_IN,m,NULL,NULL,0, rule);
557 return;
558 }
559 #endif
560 #if IPDIVERT
561 if (i != 0 && (i & IP_FW_PORT_DYNT_FLAG) == 0) {
562 /* Divert or tee packet */
563 divert_info = i;
564 goto ours;
565 }
566 #endif
567 #if IPFIREWALL_FORWARD
568 if (i == 0 && ip_fw_fwd_addr != NULL)
569 goto pass;
570 #endif
571 /*
572 * if we get here, the packet must be dropped
573 */
574 m_freem(m);
575 return;
576 }
577 pass:
578
579 /*
580 * Process options and, if not destined for us,
581 * ship it on. ip_dooptions returns 1 when an
582 * error was detected (causing an icmp message
583 * to be sent and the original packet to be freed).
584 */
585 ip_nhops = 0; /* for source routed packets */
586 if (hlen > sizeof (struct ip) && ip_dooptions(m)) {
587 #if IPFIREWALL_FORWARD
588 ip_fw_fwd_addr = NULL;
589 #endif
590 return;
591 }
592
593 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
594 * matter if it is destined to another node, or whether it is
595 * a multicast one, RSVP wants it! and prevents it from being forwarded
596 * anywhere else. Also checks if the rsvp daemon is running before
597 * grabbing the packet.
598 */
599 if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
600 goto ours;
601
602 /*
603 * Check our list of addresses, to see if the packet is for us.
604 * If we don't have any addresses, assume any unicast packet
605 * we receive might be for us (and let the upper layers deal
606 * with it).
607 */
608 if (TAILQ_EMPTY(&in_ifaddrhead) &&
609 (m->m_flags & (M_MCAST|M_BCAST)) == 0)
610 goto ours;
611
612 /*
613 * Cache the destination address of the packet; this may be
614 * changed by use of 'ipfw fwd'.
615 */
616 pkt_dst = ip_fw_fwd_addr == NULL ?
617 ip->ip_dst : ip_fw_fwd_addr->sin_addr;
618
619 /*
620 * Enable a consistency check between the destination address
621 * and the arrival interface for a unicast packet (the RFC 1122
622 * strong ES model) if IP forwarding is disabled and the packet
623 * is not locally generated and the packet is not subject to
624 * 'ipfw fwd'.
625 *
626 * XXX - Checking also should be disabled if the destination
627 * address is ipnat'ed to a different interface.
628 *
629 * XXX - Checking is incompatible with IP aliases added
630 * to the loopback interface instead of the interface where
631 * the packets are received.
632 */
633 checkif = ip_checkinterface && (ipforwarding == 0) &&
634 ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) &&
635 (ip_fw_fwd_addr == NULL);
636
637 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
638 #define satosin(sa) ((struct sockaddr_in *)(sa))
639
640 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
641 goto ours;
642
643 /*
644 * If the address matches, verify that the packet
645 * arrived via the correct interface if checking is
646 * enabled.
647 */
648 if (IA_SIN(ia)->sin_addr.s_addr == pkt_dst.s_addr &&
649 (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif))
650 goto ours;
651 /*
652 * Only accept broadcast packets that arrive via the
653 * matching interface. Reception of forwarded directed
654 * broadcasts would be handled via ip_forward() and
655 * ether_output() with the loopback into the stack for
656 * SIMPLEX interfaces handled by ether_output().
657 */
658 if ((!checkif || ia->ia_ifp == m->m_pkthdr.rcvif) &&
659 ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) {
660 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
661 pkt_dst.s_addr)
662 goto ours;
663 if (ia->ia_netbroadcast.s_addr == pkt_dst.s_addr)
664 goto ours;
665 }
666 }
667 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
668 struct in_multi *inm;
669 if (ip_mrouter) {
670 /*
671 * If we are acting as a multicast router, all
672 * incoming multicast packets are passed to the
673 * kernel-level multicast forwarding function.
674 * The packet is returned (relatively) intact; if
675 * ip_mforward() returns a non-zero value, the packet
676 * must be discarded, else it may be accepted below.
677 */
678 if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
679 ipstat.ips_cantforward++;
680 m_freem(m);
681 return;
682 }
683
684 /*
685 * The process-level routing daemon needs to receive
686 * all multicast IGMP packets, whether or not this
687 * host belongs to their destination groups.
688 */
689 if (ip->ip_p == IPPROTO_IGMP)
690 goto ours;
691 ipstat.ips_forward++;
692 }
693 /*
694 * See if we belong to the destination multicast group on the
695 * arrival interface.
696 */
697 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
698 if (inm == NULL) {
699 ipstat.ips_notmember++;
700 m_freem(m);
701 return;
702 }
703 goto ours;
704 }
705 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
706 goto ours;
707 if (ip->ip_dst.s_addr == INADDR_ANY)
708 goto ours;
709
710 /* Allow DHCP/BootP responses through */
711 if (m->m_pkthdr.rcvif != NULL
712 && (m->m_pkthdr.rcvif->if_eflags & IFEF_AUTOCONFIGURING)
713 && hlen == sizeof(struct ip)
714 && ip->ip_p == IPPROTO_UDP) {
715 struct udpiphdr *ui;
716 if (m->m_len < sizeof(struct udpiphdr)
717 && (m = m_pullup(m, sizeof(struct udpiphdr))) == 0) {
718 udpstat.udps_hdrops++;
719 return;
720 }
721 ui = mtod(m, struct udpiphdr *);
722 if (ntohs(ui->ui_dport) == IPPORT_BOOTPC) {
723 goto ours;
724 }
725 ip = mtod(m, struct ip *); /* in case it changed */
726 }
727
728 #if defined(NFAITH) && 0 < NFAITH
729 /*
730 * FAITH(Firewall Aided Internet Translator)
731 */
732 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
733 if (ip_keepfaith) {
734 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
735 goto ours;
736 }
737 m_freem(m);
738 return;
739 }
740 #endif
741 /*
742 * Not for us; forward if possible and desirable.
743 */
744 if (ipforwarding == 0) {
745 ipstat.ips_cantforward++;
746 m_freem(m);
747 } else
748 ip_forward(m, 0);
749 #if IPFIREWALL_FORWARD
750 ip_fw_fwd_addr = NULL;
751 #endif
752 return;
753
754 ours:
755 #ifndef __APPLE__
756 /* Darwin does not have an if_data in ifaddr */
757 /* Count the packet in the ip address stats */
758 if (ia != NULL) {
759 ia->ia_ifa.if_ipackets++;
760 ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
761 }
762 #endif
763
764 /*
765 * If offset or IP_MF are set, must reassemble.
766 * Otherwise, nothing need be done.
767 * (We could look in the reassembly queue to see
768 * if the packet was previously fragmented,
769 * but it's not worth the time; just let them time out.)
770 */
771 if (ip->ip_off & (IP_MF | IP_OFFMASK | IP_RF)) {
772
773 #if 0 /*
774 * Reassembly should be able to treat a mbuf cluster, for later
775 * operation of contiguous protocol headers on the cluster. (KAME)
776 */
777 if (m->m_flags & M_EXT) { /* XXX */
778 if ((m = m_pullup(m, hlen)) == 0) {
779 ipstat.ips_toosmall++;
780 #if IPFIREWALL_FORWARD
781 ip_fw_fwd_addr = NULL;
782 #endif
783 return;
784 }
785 ip = mtod(m, struct ip *);
786 }
787 #endif
788 sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
789 /*
790 * Look for queue of fragments
791 * of this datagram.
792 */
793 for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next)
794 if (ip->ip_id == fp->ipq_id &&
795 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
796 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
797 ip->ip_p == fp->ipq_p)
798 goto found;
799
800 fp = 0;
801
802 /* check if there's a place for the new queue */
803 if (nipq > maxnipq) {
804 /*
805 * drop something from the tail of the current queue
806 * before proceeding further
807 */
808 if (ipq[sum].prev == &ipq[sum]) { /* gak */
809 for (i = 0; i < IPREASS_NHASH; i++) {
810 if (ipq[i].prev != &ipq[i]) {
811 ip_freef(ipq[i].prev);
812 break;
813 }
814 }
815 } else
816 ip_freef(ipq[sum].prev);
817 }
818 found:
819 /*
820 * Adjust ip_len to not reflect header,
821 * set ip_mff if more fragments are expected,
822 * convert offset of this to bytes.
823 */
824 ip->ip_len -= hlen;
825 mff = (ip->ip_off & IP_MF) != 0;
826 if (mff) {
827 /*
828 * Make sure that fragments have a data length
829 * that's a non-zero multiple of 8 bytes.
830 */
831 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
832 ipstat.ips_toosmall++; /* XXX */
833 goto bad;
834 }
835 m->m_flags |= M_FRAG;
836 } else {
837 /* Clear the flag in case packet comes from loopback */
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 ipforward_rt.ro_rt->generation_id != route_generation) {
1572 if (ipforward_rt.ro_rt) {
1573 rtfree(ipforward_rt.ro_rt);
1574 ipforward_rt.ro_rt = 0;
1575 }
1576 sin->sin_family = AF_INET;
1577 sin->sin_len = sizeof(*sin);
1578 sin->sin_addr = dst;
1579
1580 rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1581 }
1582 if (ipforward_rt.ro_rt == 0)
1583 return ((struct in_ifaddr *)0);
1584 return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
1585 }
1586
1587 /*
1588 * Save incoming source route for use in replies,
1589 * to be picked up later by ip_srcroute if the receiver is interested.
1590 */
1591 void
1592 save_rte(option, dst)
1593 u_char *option;
1594 struct in_addr dst;
1595 {
1596 unsigned olen;
1597
1598 olen = option[IPOPT_OLEN];
1599 #if DIAGNOSTIC
1600 if (ipprintfs)
1601 printf("save_rte: olen %d\n", olen);
1602 #endif
1603 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1604 return;
1605 bcopy(option, ip_srcrt.srcopt, olen);
1606 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1607 ip_srcrt.dst = dst;
1608 }
1609
1610 /*
1611 * Retrieve incoming source route for use in replies,
1612 * in the same form used by setsockopt.
1613 * The first hop is placed before the options, will be removed later.
1614 */
1615 struct mbuf *
1616 ip_srcroute()
1617 {
1618 register struct in_addr *p, *q;
1619 register struct mbuf *m;
1620
1621 if (ip_nhops == 0)
1622 return ((struct mbuf *)0);
1623 m = m_get(M_DONTWAIT, MT_HEADER);
1624 if (m == 0)
1625 return ((struct mbuf *)0);
1626
1627 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1628
1629 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1630 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1631 OPTSIZ;
1632 #if DIAGNOSTIC
1633 if (ipprintfs)
1634 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1635 #endif
1636
1637 /*
1638 * First save first hop for return route
1639 */
1640 p = &ip_srcrt.route[ip_nhops - 1];
1641 *(mtod(m, struct in_addr *)) = *p--;
1642 #if DIAGNOSTIC
1643 if (ipprintfs)
1644 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr));
1645 #endif
1646
1647 /*
1648 * Copy option fields and padding (nop) to mbuf.
1649 */
1650 ip_srcrt.nop = IPOPT_NOP;
1651 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1652 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
1653 &ip_srcrt.nop, OPTSIZ);
1654 q = (struct in_addr *)(mtod(m, caddr_t) +
1655 sizeof(struct in_addr) + OPTSIZ);
1656 #undef OPTSIZ
1657 /*
1658 * Record return path as an IP source route,
1659 * reversing the path (pointers are now aligned).
1660 */
1661 while (p >= ip_srcrt.route) {
1662 #if DIAGNOSTIC
1663 if (ipprintfs)
1664 printf(" %lx", (u_long)ntohl(q->s_addr));
1665 #endif
1666 *q++ = *p--;
1667 }
1668 /*
1669 * Last hop goes to final destination.
1670 */
1671 *q = ip_srcrt.dst;
1672 #if DIAGNOSTIC
1673 if (ipprintfs)
1674 printf(" %lx\n", (u_long)ntohl(q->s_addr));
1675 #endif
1676 return (m);
1677 }
1678
1679 /*
1680 * Strip out IP options, at higher
1681 * level protocol in the kernel.
1682 * Second argument is buffer to which options
1683 * will be moved, and return value is their length.
1684 * XXX should be deleted; last arg currently ignored.
1685 */
1686 void
1687 ip_stripoptions(m, mopt)
1688 register struct mbuf *m;
1689 struct mbuf *mopt;
1690 {
1691 register int i;
1692 struct ip *ip = mtod(m, struct ip *);
1693 register caddr_t opts;
1694 int olen;
1695
1696 olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1697 opts = (caddr_t)(ip + 1);
1698 i = m->m_len - (sizeof (struct ip) + olen);
1699 bcopy(opts + olen, opts, (unsigned)i);
1700 m->m_len -= olen;
1701 if (m->m_flags & M_PKTHDR)
1702 m->m_pkthdr.len -= olen;
1703 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
1704 }
1705
1706 u_char inetctlerrmap[PRC_NCMDS] = {
1707 0, 0, 0, 0,
1708 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1709 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1710 EMSGSIZE, EHOSTUNREACH, 0, 0,
1711 0, 0, 0, 0,
1712 ENOPROTOOPT, ECONNREFUSED
1713 };
1714
1715 /*
1716 * Forward a packet. If some error occurs return the sender
1717 * an icmp packet. Note we can't always generate a meaningful
1718 * icmp message because icmp doesn't have a large enough repertoire
1719 * of codes and types.
1720 *
1721 * If not forwarding, just drop the packet. This could be confusing
1722 * if ipforwarding was zero but some routing protocol was advancing
1723 * us as a gateway to somewhere. However, we must let the routing
1724 * protocol deal with that.
1725 *
1726 * The srcrt parameter indicates whether the packet is being forwarded
1727 * via a source route.
1728 */
1729 static void
1730 ip_forward(m, srcrt)
1731 struct mbuf *m;
1732 int srcrt;
1733 {
1734 register struct ip *ip = mtod(m, struct ip *);
1735 register struct sockaddr_in *sin;
1736 register struct rtentry *rt;
1737 int error, type = 0, code = 0;
1738 struct mbuf *mcopy;
1739 n_long dest;
1740 struct ifnet *destifp;
1741 #if IPSEC
1742 struct ifnet dummyifp;
1743 #endif
1744
1745 dest = 0;
1746 #if DIAGNOSTIC
1747 if (ipprintfs)
1748 printf("forward: src %lx dst %lx ttl %x\n",
1749 (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr,
1750 ip->ip_ttl);
1751 #endif
1752
1753
1754 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1755 ipstat.ips_cantforward++;
1756 m_freem(m);
1757 return;
1758 }
1759 #if IPSTEALTH
1760 if (!ipstealth) {
1761 #endif
1762 if (ip->ip_ttl <= IPTTLDEC) {
1763 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
1764 dest, 0);
1765 return;
1766 }
1767 #if IPSTEALTH
1768 }
1769 #endif
1770
1771 sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
1772 if ((rt = ipforward_rt.ro_rt) == 0 ||
1773 ip->ip_dst.s_addr != sin->sin_addr.s_addr ||
1774 ipforward_rt.ro_rt->generation_id != route_generation) {
1775 if (ipforward_rt.ro_rt) {
1776 rtfree(ipforward_rt.ro_rt);
1777 ipforward_rt.ro_rt = 0;
1778 }
1779 sin->sin_family = AF_INET;
1780 sin->sin_len = sizeof(*sin);
1781 sin->sin_addr = ip->ip_dst;
1782
1783 rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1784 if (ipforward_rt.ro_rt == 0) {
1785 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1786 return;
1787 }
1788 rt = ipforward_rt.ro_rt;
1789 }
1790
1791 /*
1792 * Save the IP header and at most 8 bytes of the payload,
1793 * in case we need to generate an ICMP message to the src.
1794 *
1795 * We don't use m_copy() because it might return a reference
1796 * to a shared cluster. Both this function and ip_output()
1797 * assume exclusive access to the IP header in `m', so any
1798 * data in a cluster may change before we reach icmp_error().
1799 */
1800 MGET(mcopy, M_DONTWAIT, m->m_type);
1801 if (mcopy != NULL) {
1802 M_COPY_PKTHDR(mcopy, m);
1803 mcopy->m_len = imin((IP_VHL_HL(ip->ip_vhl) << 2) + 8,
1804 (int)ip->ip_len);
1805 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1806 }
1807
1808 #if IPSTEALTH
1809 if (!ipstealth) {
1810 #endif
1811 ip->ip_ttl -= IPTTLDEC;
1812 #if IPSTEALTH
1813 }
1814 #endif
1815
1816 /*
1817 * If forwarding packet using same interface that it came in on,
1818 * perhaps should send a redirect to sender to shortcut a hop.
1819 * Only send redirect if source is sending directly to us,
1820 * and if packet was not source routed (or has any options).
1821 * Also, don't send redirect if forwarding using a default route
1822 * or a route modified by a redirect.
1823 */
1824 #define satosin(sa) ((struct sockaddr_in *)(sa))
1825 if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1826 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1827 satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
1828 ipsendredirects && !srcrt) {
1829 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1830 u_long src = ntohl(ip->ip_src.s_addr);
1831
1832 if (RTA(rt) &&
1833 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1834 if (rt->rt_flags & RTF_GATEWAY)
1835 dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1836 else
1837 dest = ip->ip_dst.s_addr;
1838 /* Router requirements says to only send host redirects */
1839 type = ICMP_REDIRECT;
1840 code = ICMP_REDIRECT_HOST;
1841 #if DIAGNOSTIC
1842 if (ipprintfs)
1843 printf("redirect (%d) to %lx\n", code, (u_long)dest);
1844 #endif
1845 }
1846 }
1847
1848 error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1849 IP_FORWARDING, 0);
1850 if (error)
1851 ipstat.ips_cantforward++;
1852 else {
1853 ipstat.ips_forward++;
1854 if (type)
1855 ipstat.ips_redirectsent++;
1856 else {
1857 if (mcopy) {
1858 ipflow_create(&ipforward_rt, mcopy);
1859 m_freem(mcopy);
1860 }
1861 return;
1862 }
1863 }
1864 if (mcopy == NULL)
1865 return;
1866 destifp = NULL;
1867
1868 switch (error) {
1869
1870 case 0: /* forwarded, but need redirect */
1871 /* type, code set above */
1872 break;
1873
1874 case ENETUNREACH: /* shouldn't happen, checked above */
1875 case EHOSTUNREACH:
1876 case ENETDOWN:
1877 case EHOSTDOWN:
1878 default:
1879 type = ICMP_UNREACH;
1880 code = ICMP_UNREACH_HOST;
1881 break;
1882
1883 case EMSGSIZE:
1884 type = ICMP_UNREACH;
1885 code = ICMP_UNREACH_NEEDFRAG;
1886 #ifndef IPSEC
1887 if (ipforward_rt.ro_rt)
1888 destifp = ipforward_rt.ro_rt->rt_ifp;
1889 #else
1890 /*
1891 * If the packet is routed over IPsec tunnel, tell the
1892 * originator the tunnel MTU.
1893 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1894 * XXX quickhack!!!
1895 */
1896 if (ipforward_rt.ro_rt) {
1897 struct secpolicy *sp = NULL;
1898 int ipsecerror;
1899 int ipsechdr;
1900 struct route *ro;
1901
1902 if (ipsec_bypass) {
1903 destifp = ipforward_rt.ro_rt->rt_ifp;
1904 ipstat.ips_cantfrag++;
1905 break;
1906 }
1907
1908 sp = ipsec4_getpolicybyaddr(mcopy,
1909 IPSEC_DIR_OUTBOUND,
1910 IP_FORWARDING,
1911 &ipsecerror);
1912
1913 if (sp == NULL)
1914 destifp = ipforward_rt.ro_rt->rt_ifp;
1915 else {
1916 /* count IPsec header size */
1917 ipsechdr = ipsec4_hdrsiz(mcopy,
1918 IPSEC_DIR_OUTBOUND,
1919 NULL);
1920
1921 /*
1922 * find the correct route for outer IPv4
1923 * header, compute tunnel MTU.
1924 *
1925 * XXX BUG ALERT
1926 * The "dummyifp" code relies upon the fact
1927 * that icmp_error() touches only ifp->if_mtu.
1928 */
1929 /*XXX*/
1930 destifp = NULL;
1931 if (sp->req != NULL
1932 && sp->req->sav != NULL
1933 && sp->req->sav->sah != NULL) {
1934 ro = &sp->req->sav->sah->sa_route;
1935 if (ro->ro_rt && ro->ro_rt->rt_ifp) {
1936 dummyifp.if_mtu =
1937 ro->ro_rt->rt_ifp->if_mtu;
1938 dummyifp.if_mtu -= ipsechdr;
1939 destifp = &dummyifp;
1940 }
1941 }
1942
1943 key_freesp(sp);
1944 }
1945 }
1946 #endif /*IPSEC*/
1947 ipstat.ips_cantfrag++;
1948 break;
1949
1950 case ENOBUFS:
1951 type = ICMP_SOURCEQUENCH;
1952 code = 0;
1953 break;
1954
1955 case EACCES: /* ipfw denied packet */
1956 m_freem(mcopy);
1957 return;
1958 }
1959 icmp_error(mcopy, type, code, dest, destifp);
1960 }
1961
1962 void
1963 ip_savecontrol(inp, mp, ip, m)
1964 register struct inpcb *inp;
1965 register struct mbuf **mp;
1966 register struct ip *ip;
1967 register struct mbuf *m;
1968 {
1969 if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1970 struct timeval tv;
1971
1972 microtime(&tv);
1973 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1974 SCM_TIMESTAMP, SOL_SOCKET);
1975 if (*mp)
1976 mp = &(*mp)->m_next;
1977 }
1978 if (inp->inp_flags & INP_RECVDSTADDR) {
1979 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1980 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1981 if (*mp)
1982 mp = &(*mp)->m_next;
1983 }
1984 #ifdef notyet
1985 /* XXX
1986 * Moving these out of udp_input() made them even more broken
1987 * than they already were.
1988 */
1989 /* options were tossed already */
1990 if (inp->inp_flags & INP_RECVOPTS) {
1991 *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1992 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1993 if (*mp)
1994 mp = &(*mp)->m_next;
1995 }
1996 /* ip_srcroute doesn't do what we want here, need to fix */
1997 if (inp->inp_flags & INP_RECVRETOPTS) {
1998 *mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1999 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
2000 if (*mp)
2001 mp = &(*mp)->m_next;
2002 }
2003 #endif
2004 if (inp->inp_flags & INP_RECVIF) {
2005 struct ifnet *ifp;
2006 struct sdlbuf {
2007 struct sockaddr_dl sdl;
2008 u_char pad[32];
2009 } sdlbuf;
2010 struct sockaddr_dl *sdp;
2011 struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
2012
2013 if (((ifp = m->m_pkthdr.rcvif))
2014 && ( ifp->if_index && (ifp->if_index <= if_index))) {
2015 sdp = (struct sockaddr_dl *)(ifnet_addrs
2016 [ifp->if_index - 1]->ifa_addr);
2017 /*
2018 * Change our mind and don't try copy.
2019 */
2020 if ((sdp->sdl_family != AF_LINK)
2021 || (sdp->sdl_len > sizeof(sdlbuf))) {
2022 goto makedummy;
2023 }
2024 bcopy(sdp, sdl2, sdp->sdl_len);
2025 } else {
2026 makedummy:
2027 sdl2->sdl_len
2028 = offsetof(struct sockaddr_dl, sdl_data[0]);
2029 sdl2->sdl_family = AF_LINK;
2030 sdl2->sdl_index = 0;
2031 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
2032 }
2033 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
2034 IP_RECVIF, IPPROTO_IP);
2035 if (*mp)
2036 mp = &(*mp)->m_next;
2037 }
2038 if (inp->inp_flags & INP_RECVTTL) {
2039 *mp = sbcreatecontrol((caddr_t)&ip->ip_ttl, sizeof(ip->ip_ttl), IP_RECVTTL, IPPROTO_IP);
2040 if (*mp) mp = &(*mp)->m_next;
2041 }
2042 }
2043
2044 int
2045 ip_rsvp_init(struct socket *so)
2046 {
2047 if (so->so_type != SOCK_RAW ||
2048 so->so_proto->pr_protocol != IPPROTO_RSVP)
2049 return EOPNOTSUPP;
2050
2051 if (ip_rsvpd != NULL)
2052 return EADDRINUSE;
2053
2054 ip_rsvpd = so;
2055 /*
2056 * This may seem silly, but we need to be sure we don't over-increment
2057 * the RSVP counter, in case something slips up.
2058 */
2059 if (!ip_rsvp_on) {
2060 ip_rsvp_on = 1;
2061 rsvp_on++;
2062 }
2063
2064 return 0;
2065 }
2066
2067 int
2068 ip_rsvp_done(void)
2069 {
2070 ip_rsvpd = NULL;
2071 /*
2072 * This may seem silly, but we need to be sure we don't over-decrement
2073 * the RSVP counter, in case something slips up.
2074 */
2075 if (ip_rsvp_on) {
2076 ip_rsvp_on = 0;
2077 rsvp_on--;
2078 }
2079 return 0;
2080 }