ip_input.c revision 1.114.4.6 1 /* $NetBSD: ip_input.c,v 1.114.4.6 2001/04/24 22:21:20 he Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 1998 The NetBSD Foundation, Inc.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Public Access Networks Corporation ("Panix"). It was developed under
38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the NetBSD
51 * Foundation, Inc. and its contributors.
52 * 4. Neither the name of The NetBSD Foundation nor the names of its
53 * contributors may be used to endorse or promote products derived
54 * from this software without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 * POSSIBILITY OF SUCH DAMAGE.
67 */
68
69 /*
70 * Copyright (c) 1982, 1986, 1988, 1993
71 * The Regents of the University of California. All rights reserved.
72 *
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
75 * are met:
76 * 1. Redistributions of source code must retain the above copyright
77 * notice, this list of conditions and the following disclaimer.
78 * 2. Redistributions in binary form must reproduce the above copyright
79 * notice, this list of conditions and the following disclaimer in the
80 * documentation and/or other materials provided with the distribution.
81 * 3. All advertising materials mentioning features or use of this software
82 * must display the following acknowledgement:
83 * This product includes software developed by the University of
84 * California, Berkeley and its contributors.
85 * 4. Neither the name of the University nor the names of its contributors
86 * may be used to endorse or promote products derived from this software
87 * without specific prior written permission.
88 *
89 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
90 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
91 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
92 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
93 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
94 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
95 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
96 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
97 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
98 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99 * SUCH DAMAGE.
100 *
101 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
102 */
103
104 #include "opt_gateway.h"
105 #include "opt_pfil_hooks.h"
106 #include "opt_ipsec.h"
107 #include "opt_mrouting.h"
108
109 #include <sys/param.h>
110 #include <sys/systm.h>
111 #include <sys/malloc.h>
112 #include <sys/mbuf.h>
113 #include <sys/domain.h>
114 #include <sys/protosw.h>
115 #include <sys/socket.h>
116 #include <sys/socketvar.h>
117 #include <sys/errno.h>
118 #include <sys/time.h>
119 #include <sys/kernel.h>
120 #include <sys/proc.h>
121 #include <sys/pool.h>
122
123 #include <vm/vm.h>
124 #include <sys/sysctl.h>
125
126 #include <net/if.h>
127 #include <net/if_dl.h>
128 #include <net/route.h>
129 #include <net/pfil.h>
130
131 #include <netinet/in.h>
132 #include <netinet/in_systm.h>
133 #include <netinet/ip.h>
134 #include <netinet/in_pcb.h>
135 #include <netinet/in_var.h>
136 #include <netinet/ip_var.h>
137 #include <netinet/ip_icmp.h>
138 /* just for gif_ttl */
139 #include <netinet/in_gif.h>
140 #include "gif.h"
141
142 #ifdef MROUTING
143 #include <netinet/ip_mroute.h>
144 #endif
145
146 #ifdef IPSEC
147 #include <netinet6/ipsec.h>
148 #include <netkey/key.h>
149 #include <netkey/key_debug.h>
150 #endif
151
152 #ifndef IPFORWARDING
153 #ifdef GATEWAY
154 #define IPFORWARDING 1 /* forward IP packets not for us */
155 #else /* GATEWAY */
156 #define IPFORWARDING 0 /* don't forward IP packets not for us */
157 #endif /* GATEWAY */
158 #endif /* IPFORWARDING */
159 #ifndef IPSENDREDIRECTS
160 #define IPSENDREDIRECTS 1
161 #endif
162 #ifndef IPFORWSRCRT
163 #define IPFORWSRCRT 1 /* forward source-routed packets */
164 #endif
165 #ifndef IPALLOWSRCRT
166 #define IPALLOWSRCRT 1 /* allow source-routed packets */
167 #endif
168 #ifndef IPMTUDISC
169 #define IPMTUDISC 0
170 #endif
171 #ifndef IPMTUDISCTIMEOUT
172 #define IPMTUDISCTIMEOUT (10 * 60) /* as per RFC 1191 */
173 #endif
174
175 /*
176 * Note: DIRECTED_BROADCAST is handled this way so that previous
177 * configuration using this option will Just Work.
178 */
179 #ifndef IPDIRECTEDBCAST
180 #ifdef DIRECTED_BROADCAST
181 #define IPDIRECTEDBCAST 1
182 #else
183 #define IPDIRECTEDBCAST 0
184 #endif /* DIRECTED_BROADCAST */
185 #endif /* IPDIRECTEDBCAST */
186 int ipforwarding = IPFORWARDING;
187 int ipsendredirects = IPSENDREDIRECTS;
188 int ip_defttl = IPDEFTTL;
189 int ip_forwsrcrt = IPFORWSRCRT;
190 int ip_directedbcast = IPDIRECTEDBCAST;
191 int ip_allowsrcrt = IPALLOWSRCRT;
192 int ip_mtudisc = IPMTUDISC;
193 u_int ip_mtudisc_timeout = IPMTUDISCTIMEOUT;
194 #ifdef DIAGNOSTIC
195 int ipprintfs = 0;
196 #endif
197
198 struct rttimer_queue *ip_mtudisc_timeout_q = NULL;
199
200 extern struct domain inetdomain;
201 int ipqmaxlen = IFQ_MAXLEN;
202 struct in_ifaddrhead in_ifaddr;
203 struct in_ifaddrhashhead *in_ifaddrhashtbl;
204 struct ifqueue ipintrq;
205 struct ipstat ipstat;
206 u_int16_t ip_id;
207
208 struct ipqhead ipq;
209 int ipq_locked;
210 int ip_nfragpackets = 0;
211 int ip_maxfragpackets = 200;
212
213 static __inline int ipq_lock_try __P((void));
214 static __inline void ipq_unlock __P((void));
215
216 static __inline int
217 ipq_lock_try()
218 {
219 int s;
220
221 s = splimp();
222 if (ipq_locked) {
223 splx(s);
224 return (0);
225 }
226 ipq_locked = 1;
227 splx(s);
228 return (1);
229 }
230
231 static __inline void
232 ipq_unlock()
233 {
234 int s;
235
236 s = splimp();
237 ipq_locked = 0;
238 splx(s);
239 }
240
241 #ifdef DIAGNOSTIC
242 #define IPQ_LOCK() \
243 do { \
244 if (ipq_lock_try() == 0) { \
245 printf("%s:%d: ipq already locked\n", __FILE__, __LINE__); \
246 panic("ipq_lock"); \
247 } \
248 } while (0)
249 #define IPQ_LOCK_CHECK() \
250 do { \
251 if (ipq_locked == 0) { \
252 printf("%s:%d: ipq lock not held\n", __FILE__, __LINE__); \
253 panic("ipq lock check"); \
254 } \
255 } while (0)
256 #else
257 #define IPQ_LOCK() (void) ipq_lock_try()
258 #define IPQ_LOCK_CHECK() /* nothing */
259 #endif
260
261 #define IPQ_UNLOCK() ipq_unlock()
262
263 struct pool ipqent_pool;
264
265 /*
266 * We need to save the IP options in case a protocol wants to respond
267 * to an incoming packet over the same route if the packet got here
268 * using IP source routing. This allows connection establishment and
269 * maintenance when the remote end is on a network that is not known
270 * to us.
271 */
272 int ip_nhops = 0;
273 static struct ip_srcrt {
274 struct in_addr dst; /* final destination */
275 char nop; /* one NOP to align */
276 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
277 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
278 } ip_srcrt;
279
280 static void save_rte __P((u_char *, struct in_addr));
281
282 /*
283 * IP initialization: fill in IP protocol switch table.
284 * All protocols not implemented in kernel go to raw IP protocol handler.
285 */
286 void
287 ip_init()
288 {
289 struct protosw *pr;
290 int i;
291
292 pool_init(&ipqent_pool, sizeof(struct ipqent), 0, 0, 0, "ipqepl",
293 0, NULL, NULL, M_IPQ);
294
295 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
296 if (pr == 0)
297 panic("ip_init");
298 for (i = 0; i < IPPROTO_MAX; i++)
299 ip_protox[i] = pr - inetsw;
300 for (pr = inetdomain.dom_protosw;
301 pr < inetdomain.dom_protoswNPROTOSW; pr++)
302 if (pr->pr_domain->dom_family == PF_INET &&
303 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
304 ip_protox[pr->pr_protocol] = pr - inetsw;
305 LIST_INIT(&ipq);
306 ip_id = time.tv_sec & 0xffff;
307 ipintrq.ifq_maxlen = ipqmaxlen;
308 TAILQ_INIT(&in_ifaddr);
309 in_ifaddrhashtbl =
310 hashinit(IN_IFADDR_HASH_SIZE, M_IFADDR, M_WAITOK, &in_ifaddrhash);
311 if (ip_mtudisc != 0)
312 ip_mtudisc_timeout_q =
313 rt_timer_queue_create(ip_mtudisc_timeout);
314 #ifdef GATEWAY
315 ipflow_init();
316 #endif
317 }
318
319 struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
320 struct route ipforward_rt;
321
322 /*
323 * IP software interrupt routine
324 */
325 void
326 ipintr()
327 {
328 int s;
329 struct mbuf *m;
330
331 while (1) {
332 s = splimp();
333 IF_DEQUEUE(&ipintrq, m);
334 splx(s);
335 if (m == 0)
336 return;
337 ip_input(m);
338 }
339 }
340
341 /*
342 * Ip input routine. Checksum and byte swap header. If fragmented
343 * try to reassemble. Process options. Pass to next level.
344 */
345 void
346 ip_input(struct mbuf *m)
347 {
348 struct ip *ip = NULL;
349 struct ipq *fp;
350 struct in_ifaddr *ia;
351 struct ifaddr *ifa;
352 struct ipqent *ipqe;
353 int hlen = 0, mff, len;
354 int downmatch;
355 #ifdef PFIL_HOOKS
356 struct packet_filter_hook *pfh;
357 struct mbuf *m0;
358 int rv;
359 #endif /* PFIL_HOOKS */
360
361 #ifdef DIAGNOSTIC
362 if ((m->m_flags & M_PKTHDR) == 0)
363 panic("ipintr no HDR");
364 #endif
365 #ifdef IPSEC
366 /*
367 * should the inner packet be considered authentic?
368 * see comment in ah4_input().
369 */
370 if (m) {
371 m->m_flags &= ~M_AUTHIPHDR;
372 m->m_flags &= ~M_AUTHIPDGM;
373 }
374 #endif
375 /*
376 * If no IP addresses have been set yet but the interfaces
377 * are receiving, can't do anything with incoming packets yet.
378 */
379 if (in_ifaddr.tqh_first == 0)
380 goto bad;
381 ipstat.ips_total++;
382 if (m->m_len < sizeof (struct ip) &&
383 (m = m_pullup(m, sizeof (struct ip))) == 0) {
384 ipstat.ips_toosmall++;
385 return;
386 }
387 ip = mtod(m, struct ip *);
388 if (ip->ip_v != IPVERSION) {
389 ipstat.ips_badvers++;
390 goto bad;
391 }
392 hlen = ip->ip_hl << 2;
393 if (hlen < sizeof(struct ip)) { /* minimum header length */
394 ipstat.ips_badhlen++;
395 goto bad;
396 }
397 if (hlen > m->m_len) {
398 if ((m = m_pullup(m, hlen)) == 0) {
399 ipstat.ips_badhlen++;
400 return;
401 }
402 ip = mtod(m, struct ip *);
403 }
404
405 /*
406 * RFC1122: packets with a multicast source address are
407 * not allowed.
408 */
409 if (IN_MULTICAST(ip->ip_src.s_addr)) {
410 /* XXX stat */
411 goto bad;
412 }
413
414 if (in_cksum(m, hlen) != 0) {
415 ipstat.ips_badsum++;
416 goto bad;
417 }
418
419 /*
420 * Convert fields to host representation.
421 */
422 NTOHS(ip->ip_len);
423 NTOHS(ip->ip_off);
424 len = ip->ip_len;
425
426 /*
427 * Check for additional length bogosity
428 */
429 if (len < hlen) {
430 ipstat.ips_badlen++;
431 goto bad;
432 }
433
434 /*
435 * Check that the amount of data in the buffers
436 * is as at least much as the IP header would have us expect.
437 * Trim mbufs if longer than we expect.
438 * Drop packet if shorter than we expect.
439 */
440 if (m->m_pkthdr.len < len) {
441 ipstat.ips_tooshort++;
442 goto bad;
443 }
444 if (m->m_pkthdr.len > len) {
445 if (m->m_len == m->m_pkthdr.len) {
446 m->m_len = len;
447 m->m_pkthdr.len = len;
448 } else
449 m_adj(m, len - m->m_pkthdr.len);
450 }
451
452 #ifdef IPSEC
453 /* ipflow (IP fast fowarding) is not compatible with IPsec. */
454 m->m_flags &= ~M_CANFASTFWD;
455 #else
456 /*
457 * Assume that we can create a fast-forward IP flow entry
458 * based on this packet.
459 */
460 m->m_flags |= M_CANFASTFWD;
461 #endif
462
463 #ifdef PFIL_HOOKS
464 #ifdef IPSEC
465 /*
466 * let ipfilter look at packet on the wire,
467 * not the decapsulated packet.
468 */
469 if (ipsec_gethist(m, NULL))
470 goto nofilt;
471 #endif
472 /*
473 * Run through list of hooks for input packets. If there are any
474 * filters which require that additional packets in the flow are
475 * not fast-forwarded, they must clear the M_CANFASTFWD flag.
476 * Note that filters must _never_ set this flag, as another filter
477 * in the list may have previously cleared it.
478 */
479 m0 = m;
480 pfh = pfil_hook_get(PFIL_IN, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
481 for (; pfh; pfh = pfh->pfil_link.tqe_next)
482 if (pfh->pfil_func) {
483 rv = pfh->pfil_func(ip, hlen,
484 m->m_pkthdr.rcvif, 0, &m0);
485 if (rv)
486 return;
487 m = m0;
488 if (m == NULL)
489 return;
490 ip = mtod(m, struct ip *);
491 }
492 #ifdef IPSEC
493 nofilt:;
494 #endif
495 #endif /* PFIL_HOOKS */
496
497 /*
498 * Process options and, if not destined for us,
499 * ship it on. ip_dooptions returns 1 when an
500 * error was detected (causing an icmp message
501 * to be sent and the original packet to be freed).
502 */
503 ip_nhops = 0; /* for source routed packets */
504 if (hlen > sizeof (struct ip) && ip_dooptions(m))
505 return;
506
507 /*
508 * Check our list of addresses, to see if the packet is for us.
509 *
510 * Traditional 4.4BSD did not consult IFF_UP at all.
511 * The behavior here is to treat addresses on !IFF_UP interface
512 * as not mine.
513 */
514 downmatch = 0;
515 for (ia = IN_IFADDR_HASH(ip->ip_dst.s_addr).lh_first;
516 ia != NULL;
517 ia = ia->ia_hash.le_next) {
518 if (in_hosteq(ia->ia_addr.sin_addr, ip->ip_dst)) {
519 if ((ia->ia_ifp->if_flags & IFF_UP) != 0)
520 break;
521 else
522 downmatch++;
523 }
524 }
525 if (ia != NULL)
526 goto ours;
527 if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
528 for (ifa = m->m_pkthdr.rcvif->if_addrlist.tqh_first;
529 ifa != NULL; ifa = ifa->ifa_list.tqe_next) {
530 if (ifa->ifa_addr->sa_family != AF_INET) continue;
531 ia = ifatoia(ifa);
532 if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
533 in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
534 /*
535 * Look for all-0's host part (old broadcast addr),
536 * either for subnet or net.
537 */
538 ip->ip_dst.s_addr == ia->ia_subnet ||
539 ip->ip_dst.s_addr == ia->ia_net)
540 goto ours;
541 /*
542 * An interface with IP address zero accepts
543 * all packets that arrive on that interface.
544 */
545 if (in_nullhost(ia->ia_addr.sin_addr))
546 goto ours;
547 }
548 }
549 if (IN_MULTICAST(ip->ip_dst.s_addr)) {
550 struct in_multi *inm;
551 #ifdef MROUTING
552 extern struct socket *ip_mrouter;
553
554 if (m->m_flags & M_EXT) {
555 if ((m = m_pullup(m, hlen)) == 0) {
556 ipstat.ips_toosmall++;
557 return;
558 }
559 ip = mtod(m, struct ip *);
560 }
561
562 if (ip_mrouter) {
563 /*
564 * If we are acting as a multicast router, all
565 * incoming multicast packets are passed to the
566 * kernel-level multicast forwarding function.
567 * The packet is returned (relatively) intact; if
568 * ip_mforward() returns a non-zero value, the packet
569 * must be discarded, else it may be accepted below.
570 *
571 * (The IP ident field is put in the same byte order
572 * as expected when ip_mforward() is called from
573 * ip_output().)
574 */
575 if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
576 ipstat.ips_cantforward++;
577 m_freem(m);
578 return;
579 }
580
581 /*
582 * The process-level routing demon needs to receive
583 * all multicast IGMP packets, whether or not this
584 * host belongs to their destination groups.
585 */
586 if (ip->ip_p == IPPROTO_IGMP)
587 goto ours;
588 ipstat.ips_forward++;
589 }
590 #endif
591 /*
592 * See if we belong to the destination multicast group on the
593 * arrival interface.
594 */
595 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
596 if (inm == NULL) {
597 ipstat.ips_cantforward++;
598 m_freem(m);
599 return;
600 }
601 goto ours;
602 }
603 if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
604 in_nullhost(ip->ip_dst))
605 goto ours;
606
607 /*
608 * Not for us; forward if possible and desirable.
609 */
610 if (ipforwarding == 0) {
611 ipstat.ips_cantforward++;
612 m_freem(m);
613 } else {
614 /*
615 * If ip_dst matched any of my address on !IFF_UP interface,
616 * and there's no IFF_UP interface that matches ip_dst,
617 * send icmp unreach. Forwarding it will result in in-kernel
618 * forwarding loop till TTL goes to 0.
619 */
620 if (downmatch) {
621 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
622 ipstat.ips_cantforward++;
623 return;
624 }
625 ip_forward(m, 0);
626 }
627 return;
628
629 ours:
630 /*
631 * If offset or IP_MF are set, must reassemble.
632 * Otherwise, nothing need be done.
633 * (We could look in the reassembly queue to see
634 * if the packet was previously fragmented,
635 * but it's not worth the time; just let them time out.)
636 */
637 if (ip->ip_off & ~(IP_DF|IP_RF)) {
638 /*
639 * Look for queue of fragments
640 * of this datagram.
641 */
642 IPQ_LOCK();
643 for (fp = ipq.lh_first; fp != NULL; fp = fp->ipq_q.le_next)
644 if (ip->ip_id == fp->ipq_id &&
645 in_hosteq(ip->ip_src, fp->ipq_src) &&
646 in_hosteq(ip->ip_dst, fp->ipq_dst) &&
647 ip->ip_p == fp->ipq_p)
648 goto found;
649 fp = 0;
650 found:
651
652 /*
653 * Adjust ip_len to not reflect header,
654 * set ipqe_mff if more fragments are expected,
655 * convert offset of this to bytes.
656 */
657 ip->ip_len -= hlen;
658 mff = (ip->ip_off & IP_MF) != 0;
659 if (mff) {
660 /*
661 * Make sure that fragments have a data length
662 * that's a non-zero multiple of 8 bytes.
663 */
664 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
665 ipstat.ips_badfrags++;
666 IPQ_UNLOCK();
667 goto bad;
668 }
669 }
670 ip->ip_off <<= 3;
671
672 /*
673 * If datagram marked as having more fragments
674 * or if this is not the first fragment,
675 * attempt reassembly; if it succeeds, proceed.
676 */
677 if (mff || ip->ip_off) {
678 ipstat.ips_fragments++;
679 ipqe = pool_get(&ipqent_pool, PR_NOWAIT);
680 if (ipqe == NULL) {
681 ipstat.ips_rcvmemdrop++;
682 IPQ_UNLOCK();
683 goto bad;
684 }
685 ipqe->ipqe_mff = mff;
686 ipqe->ipqe_m = m;
687 ipqe->ipqe_ip = ip;
688 m = ip_reass(ipqe, fp);
689 if (m == 0) {
690 IPQ_UNLOCK();
691 return;
692 }
693 ipstat.ips_reassembled++;
694 ip = mtod(m, struct ip *);
695 hlen = ip->ip_hl << 2;
696 ip->ip_len += hlen;
697 } else
698 if (fp)
699 ip_freef(fp);
700 IPQ_UNLOCK();
701 }
702
703 #ifdef IPSEC
704 /*
705 * enforce IPsec policy checking if we are seeing last header.
706 * note that we do not visit this with protocols with pcb layer
707 * code - like udp/tcp/raw ip.
708 */
709 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
710 ipsec4_in_reject(m, NULL)) {
711 ipsecstat.in_polvio++;
712 goto bad;
713 }
714 #endif
715
716 /*
717 * Switch out to protocol's input routine.
718 */
719 #if IFA_STATS
720 ia->ia_ifa.ifa_data.ifad_inbytes += ip->ip_len;
721 #endif
722 ipstat.ips_delivered++;
723 {
724 int off = hlen, nh = ip->ip_p;
725
726 (*inetsw[ip_protox[nh]].pr_input)(m, off, nh);
727 return;
728 }
729 bad:
730 m_freem(m);
731 }
732
733 /*
734 * Take incoming datagram fragment and try to
735 * reassemble it into whole datagram. If a chain for
736 * reassembly of this datagram already exists, then it
737 * is given as fp; otherwise have to make a chain.
738 */
739 struct mbuf *
740 ip_reass(ipqe, fp)
741 struct ipqent *ipqe;
742 struct ipq *fp;
743 {
744 struct mbuf *m = ipqe->ipqe_m;
745 struct ipqent *nq, *p, *q;
746 struct ip *ip;
747 struct mbuf *t;
748 int hlen = ipqe->ipqe_ip->ip_hl << 2;
749 int i, next;
750
751 IPQ_LOCK_CHECK();
752
753 /*
754 * Presence of header sizes in mbufs
755 * would confuse code below.
756 */
757 m->m_data += hlen;
758 m->m_len -= hlen;
759
760 /*
761 * If first fragment to arrive, create a reassembly queue.
762 */
763 if (fp == 0) {
764 /*
765 * Enforce upper bound on number of fragmented packets
766 * for which we attempt reassembly;
767 * If maxfrag is 0, never accept fragments.
768 * If maxfrag is -1, accept all fragments without limitation.
769 */
770 if (ip_maxfragpackets < 0)
771 ;
772 else if (ip_nfragpackets >= ip_maxfragpackets)
773 goto dropfrag;
774 ip_nfragpackets++;
775 MALLOC(fp, struct ipq *, sizeof (struct ipq),
776 M_FTABLE, M_NOWAIT);
777 if (fp == NULL)
778 goto dropfrag;
779 LIST_INSERT_HEAD(&ipq, fp, ipq_q);
780 fp->ipq_ttl = IPFRAGTTL;
781 fp->ipq_p = ipqe->ipqe_ip->ip_p;
782 fp->ipq_id = ipqe->ipqe_ip->ip_id;
783 LIST_INIT(&fp->ipq_fragq);
784 fp->ipq_src = ipqe->ipqe_ip->ip_src;
785 fp->ipq_dst = ipqe->ipqe_ip->ip_dst;
786 p = NULL;
787 goto insert;
788 }
789
790 /*
791 * Find a segment which begins after this one does.
792 */
793 for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
794 p = q, q = q->ipqe_q.le_next)
795 if (q->ipqe_ip->ip_off > ipqe->ipqe_ip->ip_off)
796 break;
797
798 /*
799 * If there is a preceding segment, it may provide some of
800 * our data already. If so, drop the data from the incoming
801 * segment. If it provides all of our data, drop us.
802 */
803 if (p != NULL) {
804 i = p->ipqe_ip->ip_off + p->ipqe_ip->ip_len -
805 ipqe->ipqe_ip->ip_off;
806 if (i > 0) {
807 if (i >= ipqe->ipqe_ip->ip_len)
808 goto dropfrag;
809 m_adj(ipqe->ipqe_m, i);
810 ipqe->ipqe_ip->ip_off += i;
811 ipqe->ipqe_ip->ip_len -= i;
812 }
813 }
814
815 /*
816 * While we overlap succeeding segments trim them or,
817 * if they are completely covered, dequeue them.
818 */
819 for (; q != NULL && ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len >
820 q->ipqe_ip->ip_off; q = nq) {
821 i = (ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len) -
822 q->ipqe_ip->ip_off;
823 if (i < q->ipqe_ip->ip_len) {
824 q->ipqe_ip->ip_len -= i;
825 q->ipqe_ip->ip_off += i;
826 m_adj(q->ipqe_m, i);
827 break;
828 }
829 nq = q->ipqe_q.le_next;
830 m_freem(q->ipqe_m);
831 LIST_REMOVE(q, ipqe_q);
832 pool_put(&ipqent_pool, q);
833 }
834
835 insert:
836 /*
837 * Stick new segment in its place;
838 * check for complete reassembly.
839 */
840 if (p == NULL) {
841 LIST_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q);
842 } else {
843 LIST_INSERT_AFTER(p, ipqe, ipqe_q);
844 }
845 next = 0;
846 for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
847 p = q, q = q->ipqe_q.le_next) {
848 if (q->ipqe_ip->ip_off != next)
849 return (0);
850 next += q->ipqe_ip->ip_len;
851 }
852 if (p->ipqe_mff)
853 return (0);
854
855 /*
856 * Reassembly is complete. Check for a bogus message size and
857 * concatenate fragments.
858 */
859 q = fp->ipq_fragq.lh_first;
860 ip = q->ipqe_ip;
861 if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) {
862 ipstat.ips_toolong++;
863 ip_freef(fp);
864 return (0);
865 }
866 m = q->ipqe_m;
867 t = m->m_next;
868 m->m_next = 0;
869 m_cat(m, t);
870 nq = q->ipqe_q.le_next;
871 pool_put(&ipqent_pool, q);
872 for (q = nq; q != NULL; q = nq) {
873 t = q->ipqe_m;
874 nq = q->ipqe_q.le_next;
875 pool_put(&ipqent_pool, q);
876 m_cat(m, t);
877 }
878
879 /*
880 * Create header for new ip packet by
881 * modifying header of first packet;
882 * dequeue and discard fragment reassembly header.
883 * Make header visible.
884 */
885 ip->ip_len = next;
886 ip->ip_src = fp->ipq_src;
887 ip->ip_dst = fp->ipq_dst;
888 LIST_REMOVE(fp, ipq_q);
889 FREE(fp, M_FTABLE);
890 ip_nfragpackets--;
891 m->m_len += (ip->ip_hl << 2);
892 m->m_data -= (ip->ip_hl << 2);
893 /* some debugging cruft by sklower, below, will go away soon */
894 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
895 int plen = 0;
896 for (t = m; t; t = t->m_next)
897 plen += t->m_len;
898 m->m_pkthdr.len = plen;
899 }
900 return (m);
901
902 dropfrag:
903 ipstat.ips_fragdropped++;
904 m_freem(m);
905 pool_put(&ipqent_pool, ipqe);
906 return (0);
907 }
908
909 /*
910 * Free a fragment reassembly header and all
911 * associated datagrams.
912 */
913 void
914 ip_freef(fp)
915 struct ipq *fp;
916 {
917 struct ipqent *q, *p;
918
919 IPQ_LOCK_CHECK();
920
921 for (q = fp->ipq_fragq.lh_first; q != NULL; q = p) {
922 p = q->ipqe_q.le_next;
923 m_freem(q->ipqe_m);
924 LIST_REMOVE(q, ipqe_q);
925 pool_put(&ipqent_pool, q);
926 }
927 LIST_REMOVE(fp, ipq_q);
928 FREE(fp, M_FTABLE);
929 ip_nfragpackets--;
930 }
931
932 /*
933 * IP timer processing;
934 * if a timer expires on a reassembly
935 * queue, discard it.
936 */
937 void
938 ip_slowtimo()
939 {
940 struct ipq *fp, *nfp;
941 int s = splsoftnet();
942
943 IPQ_LOCK();
944 for (fp = ipq.lh_first; fp != NULL; fp = nfp) {
945 nfp = fp->ipq_q.le_next;
946 if (--fp->ipq_ttl == 0) {
947 ipstat.ips_fragtimeout++;
948 ip_freef(fp);
949 }
950 }
951 /*
952 * If we are over the maximum number of fragments
953 * (due to the limit being lowered), drain off
954 * enough to get down to the new limit.
955 */
956 if (ip_maxfragpackets < 0)
957 ;
958 else {
959 while (ip_nfragpackets > ip_maxfragpackets && ipq.lh_first)
960 ip_freef(ipq.lh_first);
961 }
962 IPQ_UNLOCK();
963 #ifdef GATEWAY
964 ipflow_slowtimo();
965 #endif
966 splx(s);
967 }
968
969 /*
970 * Drain off all datagram fragments.
971 */
972 void
973 ip_drain()
974 {
975
976 /*
977 * We may be called from a device's interrupt context. If
978 * the ipq is already busy, just bail out now.
979 */
980 if (ipq_lock_try() == 0)
981 return;
982
983 while (ipq.lh_first != NULL) {
984 ipstat.ips_fragdropped++;
985 ip_freef(ipq.lh_first);
986 }
987
988 IPQ_UNLOCK();
989 }
990
991 /*
992 * Do option processing on a datagram,
993 * possibly discarding it if bad options are encountered,
994 * or forwarding it if source-routed.
995 * Returns 1 if packet has been forwarded/freed,
996 * 0 if the packet should be processed further.
997 */
998 int
999 ip_dooptions(m)
1000 struct mbuf *m;
1001 {
1002 struct ip *ip = mtod(m, struct ip *);
1003 u_char *cp, *cp0;
1004 struct ip_timestamp *ipt;
1005 struct in_ifaddr *ia;
1006 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1007 struct in_addr dst;
1008 n_time ntime;
1009
1010 dst = ip->ip_dst;
1011 cp = (u_char *)(ip + 1);
1012 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1013 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1014 opt = cp[IPOPT_OPTVAL];
1015 if (opt == IPOPT_EOL)
1016 break;
1017 if (opt == IPOPT_NOP)
1018 optlen = 1;
1019 else {
1020 if (cnt < IPOPT_OLEN + sizeof(*cp)) {
1021 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1022 goto bad;
1023 }
1024 optlen = cp[IPOPT_OLEN];
1025 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1026 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1027 goto bad;
1028 }
1029 }
1030 switch (opt) {
1031
1032 default:
1033 break;
1034
1035 /*
1036 * Source routing with record.
1037 * Find interface with current destination address.
1038 * If none on this machine then drop if strictly routed,
1039 * or do nothing if loosely routed.
1040 * Record interface address and bring up next address
1041 * component. If strictly routed make sure next
1042 * address is on directly accessible net.
1043 */
1044 case IPOPT_LSRR:
1045 case IPOPT_SSRR:
1046 if (ip_allowsrcrt == 0) {
1047 type = ICMP_UNREACH;
1048 code = ICMP_UNREACH_NET_PROHIB;
1049 goto bad;
1050 }
1051 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1052 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1053 goto bad;
1054 }
1055 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1056 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1057 goto bad;
1058 }
1059 ipaddr.sin_addr = ip->ip_dst;
1060 ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
1061 if (ia == 0) {
1062 if (opt == IPOPT_SSRR) {
1063 type = ICMP_UNREACH;
1064 code = ICMP_UNREACH_SRCFAIL;
1065 goto bad;
1066 }
1067 /*
1068 * Loose routing, and not at next destination
1069 * yet; nothing to do except forward.
1070 */
1071 break;
1072 }
1073 off--; /* 0 origin */
1074 if ((off + sizeof(struct in_addr)) > optlen) {
1075 /*
1076 * End of source route. Should be for us.
1077 */
1078 save_rte(cp, ip->ip_src);
1079 break;
1080 }
1081 /*
1082 * locate outgoing interface
1083 */
1084 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
1085 sizeof(ipaddr.sin_addr));
1086 if (opt == IPOPT_SSRR)
1087 ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
1088 else
1089 ia = ip_rtaddr(ipaddr.sin_addr);
1090 if (ia == 0) {
1091 type = ICMP_UNREACH;
1092 code = ICMP_UNREACH_SRCFAIL;
1093 goto bad;
1094 }
1095 ip->ip_dst = ipaddr.sin_addr;
1096 bcopy((caddr_t)&ia->ia_addr.sin_addr,
1097 (caddr_t)(cp + off), sizeof(struct in_addr));
1098 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1099 /*
1100 * Let ip_intr's mcast routing check handle mcast pkts
1101 */
1102 forward = !IN_MULTICAST(ip->ip_dst.s_addr);
1103 break;
1104
1105 case IPOPT_RR:
1106 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1107 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1108 goto bad;
1109 }
1110 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1111 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1112 goto bad;
1113 }
1114 /*
1115 * If no space remains, ignore.
1116 */
1117 off--; /* 0 origin */
1118 if ((off + sizeof(struct in_addr)) > optlen)
1119 break;
1120 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
1121 sizeof(ipaddr.sin_addr));
1122 /*
1123 * locate outgoing interface; if we're the destination,
1124 * use the incoming interface (should be same).
1125 */
1126 if ((ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr))))
1127 == NULL &&
1128 (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) {
1129 type = ICMP_UNREACH;
1130 code = ICMP_UNREACH_HOST;
1131 goto bad;
1132 }
1133 bcopy((caddr_t)&ia->ia_addr.sin_addr,
1134 (caddr_t)(cp + off), sizeof(struct in_addr));
1135 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1136 break;
1137
1138 case IPOPT_TS:
1139 code = cp - (u_char *)ip;
1140 ipt = (struct ip_timestamp *)cp;
1141 if (ipt->ipt_len < 4 || ipt->ipt_len > 40) {
1142 code = (u_char *)&ipt->ipt_len - (u_char *)ip;
1143 goto bad;
1144 }
1145 if (ipt->ipt_ptr < 5) {
1146 code = (u_char *)&ipt->ipt_ptr - (u_char *)ip;
1147 goto bad;
1148 }
1149 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
1150 if (++ipt->ipt_oflw == 0) {
1151 code = (u_char *)&ipt->ipt_ptr -
1152 (u_char *)ip;
1153 goto bad;
1154 }
1155 break;
1156 }
1157 cp0 = (cp + ipt->ipt_ptr - 1);
1158 switch (ipt->ipt_flg) {
1159
1160 case IPOPT_TS_TSONLY:
1161 break;
1162
1163 case IPOPT_TS_TSANDADDR:
1164 if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1165 sizeof(struct in_addr) > ipt->ipt_len) {
1166 code = (u_char *)&ipt->ipt_ptr -
1167 (u_char *)ip;
1168 goto bad;
1169 }
1170 ipaddr.sin_addr = dst;
1171 ia = ifatoia(ifaof_ifpforaddr(sintosa(&ipaddr),
1172 m->m_pkthdr.rcvif));
1173 if (ia == 0)
1174 continue;
1175 bcopy(&ia->ia_addr.sin_addr,
1176 cp0, sizeof(struct in_addr));
1177 ipt->ipt_ptr += sizeof(struct in_addr);
1178 break;
1179
1180 case IPOPT_TS_PRESPEC:
1181 if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1182 sizeof(struct in_addr) > ipt->ipt_len) {
1183 code = (u_char *)&ipt->ipt_ptr -
1184 (u_char *)ip;
1185 goto bad;
1186 }
1187 bcopy(cp0, &ipaddr.sin_addr,
1188 sizeof(struct in_addr));
1189 if (ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)))
1190 == NULL)
1191 continue;
1192 ipt->ipt_ptr += sizeof(struct in_addr);
1193 break;
1194
1195 default:
1196 /* XXX can't take &ipt->ipt_flg */
1197 code = (u_char *)&ipt->ipt_ptr -
1198 (u_char *)ip + 1;
1199 goto bad;
1200 }
1201 ntime = iptime();
1202 cp0 = (u_char *) &ntime; /* XXX grumble, GCC... */
1203 bcopy(cp0, (caddr_t)cp + ipt->ipt_ptr - 1,
1204 sizeof(n_time));
1205 ipt->ipt_ptr += sizeof(n_time);
1206 }
1207 }
1208 if (forward) {
1209 if (ip_forwsrcrt == 0) {
1210 type = ICMP_UNREACH;
1211 code = ICMP_UNREACH_SRCFAIL;
1212 goto bad;
1213 }
1214 ip_forward(m, 1);
1215 return (1);
1216 }
1217 return (0);
1218 bad:
1219 icmp_error(m, type, code, 0, 0);
1220 ipstat.ips_badoptions++;
1221 return (1);
1222 }
1223
1224 /*
1225 * Given address of next destination (final or next hop),
1226 * return internet address info of interface to be used to get there.
1227 */
1228 struct in_ifaddr *
1229 ip_rtaddr(dst)
1230 struct in_addr dst;
1231 {
1232 struct sockaddr_in *sin;
1233
1234 sin = satosin(&ipforward_rt.ro_dst);
1235
1236 if (ipforward_rt.ro_rt == 0 || !in_hosteq(dst, sin->sin_addr)) {
1237 if (ipforward_rt.ro_rt) {
1238 RTFREE(ipforward_rt.ro_rt);
1239 ipforward_rt.ro_rt = 0;
1240 }
1241 sin->sin_family = AF_INET;
1242 sin->sin_len = sizeof(*sin);
1243 sin->sin_addr = dst;
1244
1245 rtalloc(&ipforward_rt);
1246 }
1247 if (ipforward_rt.ro_rt == 0)
1248 return ((struct in_ifaddr *)0);
1249 return (ifatoia(ipforward_rt.ro_rt->rt_ifa));
1250 }
1251
1252 /*
1253 * Save incoming source route for use in replies,
1254 * to be picked up later by ip_srcroute if the receiver is interested.
1255 */
1256 void
1257 save_rte(option, dst)
1258 u_char *option;
1259 struct in_addr dst;
1260 {
1261 unsigned olen;
1262
1263 olen = option[IPOPT_OLEN];
1264 #ifdef DIAGNOSTIC
1265 if (ipprintfs)
1266 printf("save_rte: olen %d\n", olen);
1267 #endif /* 0 */
1268 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1269 return;
1270 bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
1271 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1272 ip_srcrt.dst = dst;
1273 }
1274
1275 /*
1276 * Retrieve incoming source route for use in replies,
1277 * in the same form used by setsockopt.
1278 * The first hop is placed before the options, will be removed later.
1279 */
1280 struct mbuf *
1281 ip_srcroute()
1282 {
1283 struct in_addr *p, *q;
1284 struct mbuf *m;
1285
1286 if (ip_nhops == 0)
1287 return ((struct mbuf *)0);
1288 m = m_get(M_DONTWAIT, MT_SOOPTS);
1289 if (m == 0)
1290 return ((struct mbuf *)0);
1291
1292 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1293
1294 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1295 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1296 OPTSIZ;
1297 #ifdef DIAGNOSTIC
1298 if (ipprintfs)
1299 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1300 #endif
1301
1302 /*
1303 * First save first hop for return route
1304 */
1305 p = &ip_srcrt.route[ip_nhops - 1];
1306 *(mtod(m, struct in_addr *)) = *p--;
1307 #ifdef DIAGNOSTIC
1308 if (ipprintfs)
1309 printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
1310 #endif
1311
1312 /*
1313 * Copy option fields and padding (nop) to mbuf.
1314 */
1315 ip_srcrt.nop = IPOPT_NOP;
1316 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1317 bcopy((caddr_t)&ip_srcrt.nop,
1318 mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
1319 q = (struct in_addr *)(mtod(m, caddr_t) +
1320 sizeof(struct in_addr) + OPTSIZ);
1321 #undef OPTSIZ
1322 /*
1323 * Record return path as an IP source route,
1324 * reversing the path (pointers are now aligned).
1325 */
1326 while (p >= ip_srcrt.route) {
1327 #ifdef DIAGNOSTIC
1328 if (ipprintfs)
1329 printf(" %x", ntohl(q->s_addr));
1330 #endif
1331 *q++ = *p--;
1332 }
1333 /*
1334 * Last hop goes to final destination.
1335 */
1336 *q = ip_srcrt.dst;
1337 #ifdef DIAGNOSTIC
1338 if (ipprintfs)
1339 printf(" %x\n", ntohl(q->s_addr));
1340 #endif
1341 return (m);
1342 }
1343
1344 /*
1345 * Strip out IP options, at higher
1346 * level protocol in the kernel.
1347 * Second argument is buffer to which options
1348 * will be moved, and return value is their length.
1349 * XXX should be deleted; last arg currently ignored.
1350 */
1351 void
1352 ip_stripoptions(m, mopt)
1353 struct mbuf *m;
1354 struct mbuf *mopt;
1355 {
1356 int i;
1357 struct ip *ip = mtod(m, struct ip *);
1358 caddr_t opts;
1359 int olen;
1360
1361 olen = (ip->ip_hl << 2) - sizeof (struct ip);
1362 opts = (caddr_t)(ip + 1);
1363 i = m->m_len - (sizeof (struct ip) + olen);
1364 bcopy(opts + olen, opts, (unsigned)i);
1365 m->m_len -= olen;
1366 if (m->m_flags & M_PKTHDR)
1367 m->m_pkthdr.len -= olen;
1368 ip->ip_len -= olen;
1369 ip->ip_hl = sizeof (struct ip) >> 2;
1370 }
1371
1372 int inetctlerrmap[PRC_NCMDS] = {
1373 0, 0, 0, 0,
1374 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1375 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1376 EMSGSIZE, EHOSTUNREACH, 0, 0,
1377 0, 0, 0, 0,
1378 ENOPROTOOPT
1379 };
1380
1381 /*
1382 * Forward a packet. If some error occurs return the sender
1383 * an icmp packet. Note we can't always generate a meaningful
1384 * icmp message because icmp doesn't have a large enough repertoire
1385 * of codes and types.
1386 *
1387 * If not forwarding, just drop the packet. This could be confusing
1388 * if ipforwarding was zero but some routing protocol was advancing
1389 * us as a gateway to somewhere. However, we must let the routing
1390 * protocol deal with that.
1391 *
1392 * The srcrt parameter indicates whether the packet is being forwarded
1393 * via a source route.
1394 */
1395 void
1396 ip_forward(m, srcrt)
1397 struct mbuf *m;
1398 int srcrt;
1399 {
1400 struct ip *ip = mtod(m, struct ip *);
1401 struct sockaddr_in *sin;
1402 struct rtentry *rt;
1403 int error, type = 0, code = 0;
1404 struct mbuf *mcopy;
1405 n_long dest;
1406 struct ifnet *destifp;
1407 #ifdef IPSEC
1408 struct ifnet dummyifp;
1409 #endif
1410
1411 dest = 0;
1412 #ifdef DIAGNOSTIC
1413 if (ipprintfs)
1414 printf("forward: src %2.2x dst %2.2x ttl %x\n",
1415 ntohl(ip->ip_src.s_addr),
1416 ntohl(ip->ip_dst.s_addr), ip->ip_ttl);
1417 #endif
1418 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1419 ipstat.ips_cantforward++;
1420 m_freem(m);
1421 return;
1422 }
1423 if (ip->ip_ttl <= IPTTLDEC) {
1424 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1425 return;
1426 }
1427 ip->ip_ttl -= IPTTLDEC;
1428
1429 sin = satosin(&ipforward_rt.ro_dst);
1430 if ((rt = ipforward_rt.ro_rt) == 0 ||
1431 !in_hosteq(ip->ip_dst, sin->sin_addr)) {
1432 if (ipforward_rt.ro_rt) {
1433 RTFREE(ipforward_rt.ro_rt);
1434 ipforward_rt.ro_rt = 0;
1435 }
1436 sin->sin_family = AF_INET;
1437 sin->sin_len = sizeof(struct sockaddr_in);
1438 sin->sin_addr = ip->ip_dst;
1439
1440 rtalloc(&ipforward_rt);
1441 if (ipforward_rt.ro_rt == 0) {
1442 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1443 return;
1444 }
1445 rt = ipforward_rt.ro_rt;
1446 }
1447
1448 /*
1449 * Save at most 68 bytes of the packet in case
1450 * we need to generate an ICMP message to the src.
1451 * Pullup to avoid sharing mbuf cluster between m and mcopy.
1452 */
1453 mcopy = m_copym(m, 0, imin((int)ip->ip_len, 68), M_DONTWAIT);
1454 if (mcopy)
1455 mcopy = m_pullup(mcopy, ip->ip_hl << 2);
1456
1457 /*
1458 * If forwarding packet using same interface that it came in on,
1459 * perhaps should send a redirect to sender to shortcut a hop.
1460 * Only send redirect if source is sending directly to us,
1461 * and if packet was not source routed (or has any options).
1462 * Also, don't send redirect if forwarding using a default route
1463 * or a route modified by a redirect.
1464 */
1465 if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1466 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1467 !in_nullhost(satosin(rt_key(rt))->sin_addr) &&
1468 ipsendredirects && !srcrt) {
1469 if (rt->rt_ifa &&
1470 (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
1471 ifatoia(rt->rt_ifa)->ia_subnet) {
1472 if (rt->rt_flags & RTF_GATEWAY)
1473 dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1474 else
1475 dest = ip->ip_dst.s_addr;
1476 /*
1477 * Router requirements says to only send host
1478 * redirects.
1479 */
1480 type = ICMP_REDIRECT;
1481 code = ICMP_REDIRECT_HOST;
1482 #ifdef DIAGNOSTIC
1483 if (ipprintfs)
1484 printf("redirect (%d) to %x\n", code,
1485 (u_int32_t)dest);
1486 #endif
1487 }
1488 }
1489
1490 #ifdef IPSEC
1491 /* Don't lookup socket in forwading case */
1492 (void)ipsec_setsocket(m, NULL);
1493 #endif
1494 error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1495 (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 0);
1496 if (error)
1497 ipstat.ips_cantforward++;
1498 else {
1499 ipstat.ips_forward++;
1500 if (type)
1501 ipstat.ips_redirectsent++;
1502 else {
1503 if (mcopy) {
1504 #ifdef GATEWAY
1505 if (mcopy->m_flags & M_CANFASTFWD)
1506 ipflow_create(&ipforward_rt, mcopy);
1507 #endif
1508 m_freem(mcopy);
1509 }
1510 return;
1511 }
1512 }
1513 if (mcopy == NULL)
1514 return;
1515 destifp = NULL;
1516
1517 switch (error) {
1518
1519 case 0: /* forwarded, but need redirect */
1520 /* type, code set above */
1521 break;
1522
1523 case ENETUNREACH: /* shouldn't happen, checked above */
1524 case EHOSTUNREACH:
1525 case ENETDOWN:
1526 case EHOSTDOWN:
1527 default:
1528 type = ICMP_UNREACH;
1529 code = ICMP_UNREACH_HOST;
1530 break;
1531
1532 case EMSGSIZE:
1533 type = ICMP_UNREACH;
1534 code = ICMP_UNREACH_NEEDFRAG;
1535 #ifndef IPSEC
1536 if (ipforward_rt.ro_rt)
1537 destifp = ipforward_rt.ro_rt->rt_ifp;
1538 #else
1539 /*
1540 * If the packet is routed over IPsec tunnel, tell the
1541 * originator the tunnel MTU.
1542 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1543 * XXX quickhack!!!
1544 */
1545 if (ipforward_rt.ro_rt) {
1546 struct secpolicy *sp;
1547 int ipsecerror;
1548 size_t ipsechdr;
1549 struct route *ro;
1550
1551 sp = ipsec4_getpolicybyaddr(mcopy,
1552 IPSEC_DIR_OUTBOUND,
1553 IP_FORWARDING,
1554 &ipsecerror);
1555
1556 if (sp == NULL)
1557 destifp = ipforward_rt.ro_rt->rt_ifp;
1558 else {
1559 /* count IPsec header size */
1560 ipsechdr = ipsec4_hdrsiz(mcopy,
1561 IPSEC_DIR_OUTBOUND,
1562 NULL);
1563
1564 /*
1565 * find the correct route for outer IPv4
1566 * header, compute tunnel MTU.
1567 *
1568 * XXX BUG ALERT
1569 * The "dummyifp" code relies upon the fact
1570 * that icmp_error() touches only ifp->if_mtu.
1571 */
1572 /*XXX*/
1573 destifp = NULL;
1574 if (sp->req != NULL
1575 && sp->req->sav != NULL
1576 && sp->req->sav->sah != NULL) {
1577 ro = &sp->req->sav->sah->sa_route;
1578 if (ro->ro_rt && ro->ro_rt->rt_ifp) {
1579 dummyifp.if_mtu =
1580 ro->ro_rt->rt_ifp->if_mtu;
1581 dummyifp.if_mtu -= ipsechdr;
1582 destifp = &dummyifp;
1583 }
1584 }
1585
1586 key_freesp(sp);
1587 }
1588 }
1589 #endif /*IPSEC*/
1590 ipstat.ips_cantfrag++;
1591 break;
1592
1593 case ENOBUFS:
1594 type = ICMP_SOURCEQUENCH;
1595 code = 0;
1596 break;
1597 }
1598 icmp_error(mcopy, type, code, dest, destifp);
1599 }
1600
1601 void
1602 ip_savecontrol(inp, mp, ip, m)
1603 struct inpcb *inp;
1604 struct mbuf **mp;
1605 struct ip *ip;
1606 struct mbuf *m;
1607 {
1608
1609 if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1610 struct timeval tv;
1611
1612 microtime(&tv);
1613 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1614 SCM_TIMESTAMP, SOL_SOCKET);
1615 if (*mp)
1616 mp = &(*mp)->m_next;
1617 }
1618 if (inp->inp_flags & INP_RECVDSTADDR) {
1619 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1620 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1621 if (*mp)
1622 mp = &(*mp)->m_next;
1623 }
1624 #ifdef notyet
1625 /*
1626 * XXX
1627 * Moving these out of udp_input() made them even more broken
1628 * than they already were.
1629 * - fenner (at) parc.xerox.com
1630 */
1631 /* options were tossed already */
1632 if (inp->inp_flags & INP_RECVOPTS) {
1633 *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1634 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1635 if (*mp)
1636 mp = &(*mp)->m_next;
1637 }
1638 /* ip_srcroute doesn't do what we want here, need to fix */
1639 if (inp->inp_flags & INP_RECVRETOPTS) {
1640 *mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1641 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1642 if (*mp)
1643 mp = &(*mp)->m_next;
1644 }
1645 #endif
1646 if (inp->inp_flags & INP_RECVIF) {
1647 struct sockaddr_dl sdl;
1648
1649 sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]);
1650 sdl.sdl_family = AF_LINK;
1651 sdl.sdl_index = m->m_pkthdr.rcvif ?
1652 m->m_pkthdr.rcvif->if_index : 0;
1653 sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0;
1654 *mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len,
1655 IP_RECVIF, IPPROTO_IP);
1656 if (*mp)
1657 mp = &(*mp)->m_next;
1658 }
1659 }
1660
1661 int
1662 ip_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1663 int *name;
1664 u_int namelen;
1665 void *oldp;
1666 size_t *oldlenp;
1667 void *newp;
1668 size_t newlen;
1669 {
1670 extern int subnetsarelocal, hostzeroisbroadcast;
1671
1672 int error, old;
1673
1674 /* All sysctl names at this level are terminal. */
1675 if (namelen != 1)
1676 return (ENOTDIR);
1677
1678 switch (name[0]) {
1679 case IPCTL_FORWARDING:
1680 return (sysctl_int(oldp, oldlenp, newp, newlen, &ipforwarding));
1681 case IPCTL_SENDREDIRECTS:
1682 return (sysctl_int(oldp, oldlenp, newp, newlen,
1683 &ipsendredirects));
1684 case IPCTL_DEFTTL:
1685 return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_defttl));
1686 #ifdef notyet
1687 case IPCTL_DEFMTU:
1688 return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu));
1689 #endif
1690 case IPCTL_FORWSRCRT:
1691 /* Don't allow this to change in a secure environment. */
1692 if (securelevel > 0)
1693 return (sysctl_rdint(oldp, oldlenp, newp,
1694 ip_forwsrcrt));
1695 else
1696 return (sysctl_int(oldp, oldlenp, newp, newlen,
1697 &ip_forwsrcrt));
1698 case IPCTL_DIRECTEDBCAST:
1699 return (sysctl_int(oldp, oldlenp, newp, newlen,
1700 &ip_directedbcast));
1701 case IPCTL_ALLOWSRCRT:
1702 return (sysctl_int(oldp, oldlenp, newp, newlen,
1703 &ip_allowsrcrt));
1704 case IPCTL_SUBNETSARELOCAL:
1705 return (sysctl_int(oldp, oldlenp, newp, newlen,
1706 &subnetsarelocal));
1707 case IPCTL_MTUDISC:
1708 error = sysctl_int(oldp, oldlenp, newp, newlen,
1709 &ip_mtudisc);
1710 if (ip_mtudisc != 0 && ip_mtudisc_timeout_q == NULL) {
1711 ip_mtudisc_timeout_q =
1712 rt_timer_queue_create(ip_mtudisc_timeout);
1713 } else if (ip_mtudisc == 0 && ip_mtudisc_timeout_q != NULL) {
1714 rt_timer_queue_destroy(ip_mtudisc_timeout_q, TRUE);
1715 ip_mtudisc_timeout_q = NULL;
1716 }
1717 return error;
1718 case IPCTL_ANONPORTMIN:
1719 old = anonportmin;
1720 error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmin);
1721 if (anonportmin >= anonportmax || anonportmin < 0
1722 || anonportmin > 65535
1723 #ifndef IPNOPRIVPORTS
1724 || anonportmin < IPPORT_RESERVED
1725 #endif
1726 ) {
1727 anonportmin = old;
1728 return (EINVAL);
1729 }
1730 return (error);
1731 case IPCTL_ANONPORTMAX:
1732 old = anonportmax;
1733 error = sysctl_int(oldp, oldlenp, newp, newlen, &anonportmax);
1734 if (anonportmin >= anonportmax || anonportmax < 0
1735 || anonportmax > 65535
1736 #ifndef IPNOPRIVPORTS
1737 || anonportmax < IPPORT_RESERVED
1738 #endif
1739 ) {
1740 anonportmax = old;
1741 return (EINVAL);
1742 }
1743 return (error);
1744 case IPCTL_MTUDISCTIMEOUT:
1745 error = sysctl_int(oldp, oldlenp, newp, newlen,
1746 &ip_mtudisc_timeout);
1747 if (ip_mtudisc_timeout_q != NULL)
1748 rt_timer_queue_change(ip_mtudisc_timeout_q,
1749 ip_mtudisc_timeout);
1750 return (error);
1751 #ifdef GATEWAY
1752 case IPCTL_MAXFLOWS:
1753 {
1754 int s;
1755
1756 error = sysctl_int(oldp, oldlenp, newp, newlen,
1757 &ip_maxflows);
1758 s = splsoftnet();
1759 ipflow_reap(0);
1760 splx(s);
1761 return (error);
1762 }
1763 #endif
1764 case IPCTL_HOSTZEROBROADCAST:
1765 return (sysctl_int(oldp, oldlenp, newp, newlen,
1766 &hostzeroisbroadcast));
1767 #if NGIF > 0
1768 case IPCTL_GIF_TTL:
1769 return(sysctl_int(oldp, oldlenp, newp, newlen,
1770 &ip_gif_ttl));
1771 #endif
1772
1773 #ifndef IPNOPRIVPORTS
1774 case IPCTL_LOWPORTMIN:
1775 old = lowportmin;
1776 error = sysctl_int(oldp, oldlenp, newp, newlen, &lowportmin);
1777 if (lowportmin >= lowportmax
1778 || lowportmin > IPPORT_RESERVEDMAX
1779 || lowportmin < IPPORT_RESERVEDMIN
1780 ) {
1781 lowportmin = old;
1782 return (EINVAL);
1783 }
1784 return (error);
1785 case IPCTL_LOWPORTMAX:
1786 old = lowportmax;
1787 error = sysctl_int(oldp, oldlenp, newp, newlen, &lowportmax);
1788 if (lowportmin >= lowportmax
1789 || lowportmax > IPPORT_RESERVEDMAX
1790 || lowportmax < IPPORT_RESERVEDMIN
1791 ) {
1792 lowportmax = old;
1793 return (EINVAL);
1794 }
1795 return (error);
1796 #endif
1797
1798 case IPCTL_MAXFRAGPACKETS:
1799 return (sysctl_int(oldp, oldlenp, newp, newlen,
1800 &ip_maxfragpackets));
1801
1802 default:
1803 return (EOPNOTSUPP);
1804 }
1805 /* NOTREACHED */
1806 }
1807