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