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