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