ip_input.c revision 1.37 1 /* $NetBSD: ip_input.c,v 1.37 1996/09/21 19:44:33 perry Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1988, 1993
5 * The Regents of the University of California. 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/domain.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/errno.h>
46 #include <sys/time.h>
47 #include <sys/kernel.h>
48 #include <sys/proc.h>
49
50 #include <vm/vm.h>
51 #include <sys/sysctl.h>
52
53 #include <net/if.h>
54 #include <net/route.h>
55
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/ip.h>
59 #include <netinet/in_pcb.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/ip_icmp.h>
63
64 #ifdef PFIL_HOOKS
65 #include <net/pfil.h>
66 #endif /* PFIL_HOOKS */
67
68 #ifndef IPFORWARDING
69 #ifdef GATEWAY
70 #define IPFORWARDING 1 /* forward IP packets not for us */
71 #else /* GATEWAY */
72 #define IPFORWARDING 0 /* don't forward IP packets not for us */
73 #endif /* GATEWAY */
74 #endif /* IPFORWARDING */
75 #ifndef IPSENDREDIRECTS
76 #define IPSENDREDIRECTS 1
77 #endif
78 #ifndef IPFORWSRCRT
79 #define IPFORWSRCRT 1 /* allow source-routed packets */
80 #endif
81 /*
82 * Note: DIRECTED_BROADCAST is handled this way so that previous
83 * configuration using this option will Just Work.
84 */
85 #ifndef IPDIRECTEDBCAST
86 #ifdef DIRECTED_BROADCAST
87 #define IPDIRECTEDBCAST 1
88 #else
89 #define IPDIRECTEDBCAST 0
90 #endif /* DIRECTED_BROADCAST */
91 #endif /* IPDIRECTEDBCAST */
92 int ipforwarding = IPFORWARDING;
93 int ipsendredirects = IPSENDREDIRECTS;
94 int ip_defttl = IPDEFTTL;
95 int ip_forwsrcrt = IPFORWSRCRT;
96 int ip_directedbcast = IPDIRECTEDBCAST;
97 #ifdef DIAGNOSTIC
98 int ipprintfs = 0;
99 #endif
100
101 extern struct domain inetdomain;
102 extern struct protosw inetsw[];
103 u_char ip_protox[IPPROTO_MAX];
104 int ipqmaxlen = IFQ_MAXLEN;
105 struct in_ifaddrhead in_ifaddr;
106 struct ifqueue ipintrq;
107
108 /*
109 * We need to save the IP options in case a protocol wants to respond
110 * to an incoming packet over the same route if the packet got here
111 * using IP source routing. This allows connection establishment and
112 * maintenance when the remote end is on a network that is not known
113 * to us.
114 */
115 int ip_nhops = 0;
116 static struct ip_srcrt {
117 struct in_addr dst; /* final destination */
118 char nop; /* one NOP to align */
119 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
120 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
121 } ip_srcrt;
122
123 static void save_rte __P((u_char *, struct in_addr));
124
125 /*
126 * IP initialization: fill in IP protocol switch table.
127 * All protocols not implemented in kernel go to raw IP protocol handler.
128 */
129 void
130 ip_init()
131 {
132 register struct protosw *pr;
133 register int i;
134
135 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
136 if (pr == 0)
137 panic("ip_init");
138 for (i = 0; i < IPPROTO_MAX; i++)
139 ip_protox[i] = pr - inetsw;
140 for (pr = inetdomain.dom_protosw;
141 pr < inetdomain.dom_protoswNPROTOSW; pr++)
142 if (pr->pr_domain->dom_family == PF_INET &&
143 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
144 ip_protox[pr->pr_protocol] = pr - inetsw;
145 LIST_INIT(&ipq);
146 ip_id = time.tv_sec & 0xffff;
147 ipintrq.ifq_maxlen = ipqmaxlen;
148 TAILQ_INIT(&in_ifaddr);
149 }
150
151 struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
152 struct route ipforward_rt;
153
154 /*
155 * Ip input routine. Checksum and byte swap header. If fragmented
156 * try to reassemble. Process options. Pass to next level.
157 */
158 void
159 ipintr()
160 {
161 register struct ip *ip = NULL;
162 register struct mbuf *m;
163 register struct ipq *fp;
164 register struct in_ifaddr *ia;
165 struct ipqent *ipqe;
166 int hlen = 0, mff, len, s;
167 #ifdef PFIL_HOOKS
168 struct packet_filter_hook *pfh;
169 struct mbuf *m0;
170 #endif /* PFIL_HOOKS */
171
172 next:
173 /*
174 * Get next datagram off input queue and get IP header
175 * in first mbuf.
176 */
177 s = splimp();
178 IF_DEQUEUE(&ipintrq, m);
179 splx(s);
180 if (m == 0)
181 return;
182 #ifdef DIAGNOSTIC
183 if ((m->m_flags & M_PKTHDR) == 0)
184 panic("ipintr no HDR");
185 #endif
186 /*
187 * If no IP addresses have been set yet but the interfaces
188 * are receiving, can't do anything with incoming packets yet.
189 */
190 if (in_ifaddr.tqh_first == 0)
191 goto bad;
192 ipstat.ips_total++;
193 if (m->m_len < sizeof (struct ip) &&
194 (m = m_pullup(m, sizeof (struct ip))) == 0) {
195 ipstat.ips_toosmall++;
196 goto next;
197 }
198 ip = mtod(m, struct ip *);
199 if (ip->ip_v != IPVERSION) {
200 ipstat.ips_badvers++;
201 goto bad;
202 }
203 hlen = ip->ip_hl << 2;
204 if (hlen < sizeof(struct ip)) { /* minimum header length */
205 ipstat.ips_badhlen++;
206 goto bad;
207 }
208 if (hlen > m->m_len) {
209 if ((m = m_pullup(m, hlen)) == 0) {
210 ipstat.ips_badhlen++;
211 goto next;
212 }
213 ip = mtod(m, struct ip *);
214 }
215 if ((ip->ip_sum = in_cksum(m, hlen)) != 0) {
216 ipstat.ips_badsum++;
217 goto bad;
218 }
219
220 /*
221 * Convert fields to host representation.
222 */
223 NTOHS(ip->ip_len);
224 NTOHS(ip->ip_id);
225 NTOHS(ip->ip_off);
226 len = ip->ip_len;
227
228 /*
229 * Check that the amount of data in the buffers
230 * is as at least much as the IP header would have us expect.
231 * Trim mbufs if longer than we expect.
232 * Drop packet if shorter than we expect.
233 */
234 if (m->m_pkthdr.len < len) {
235 ipstat.ips_tooshort++;
236 goto bad;
237 }
238 if (m->m_pkthdr.len > len) {
239 if (m->m_len == m->m_pkthdr.len) {
240 m->m_len = len;
241 m->m_pkthdr.len = len;
242 } else
243 m_adj(m, len - m->m_pkthdr.len);
244 }
245
246 #ifdef PFIL_HOOKS
247 /*
248 * Run through list of hooks for input packets.
249 */
250 m0 = m;
251 for (pfh = pfil_hook_get(PFIL_IN); pfh; pfh = pfh->pfil_link.le_next)
252 if (pfh->pfil_func) {
253 if (pfh->pfil_func(ip, hlen, m->m_pkthdr.rcvif, 0, &m0))
254 goto bad;
255 ip = mtod(m = m0, struct ip *);
256 }
257 #endif /* PFIL_HOOKS */
258
259 /*
260 * Process options and, if not destined for us,
261 * ship it on. ip_dooptions returns 1 when an
262 * error was detected (causing an icmp message
263 * to be sent and the original packet to be freed).
264 */
265 ip_nhops = 0; /* for source routed packets */
266 if (hlen > sizeof (struct ip) && ip_dooptions(m))
267 goto next;
268
269 /*
270 * Check our list of addresses, to see if the packet is for us.
271 */
272 for (ia = in_ifaddr.tqh_first; ia; ia = ia->ia_list.tqe_next) {
273 if (in_hosteq(ip->ip_dst, ia->ia_addr.sin_addr))
274 goto ours;
275 if (((ip_directedbcast == 0) || (ip_directedbcast &&
276 ia->ia_ifp == m->m_pkthdr.rcvif)) &&
277 (ia->ia_ifp->if_flags & IFF_BROADCAST)) {
278 if (in_hosteq(ip->ip_dst, ia->ia_broadaddr.sin_addr) ||
279 in_hosteq(ip->ip_dst, ia->ia_netbroadcast) ||
280 /*
281 * Look for all-0's host part (old broadcast addr),
282 * either for subnet or net.
283 */
284 ip->ip_dst.s_addr == ia->ia_subnet ||
285 ip->ip_dst.s_addr == ia->ia_net)
286 goto ours;
287 }
288 }
289 if (IN_MULTICAST(ip->ip_dst.s_addr)) {
290 struct in_multi *inm;
291 #ifdef MROUTING
292 extern struct socket *ip_mrouter;
293
294 if (m->m_flags & M_EXT) {
295 if ((m = m_pullup(m, hlen)) == 0) {
296 ipstat.ips_toosmall++;
297 goto next;
298 }
299 ip = mtod(m, struct ip *);
300 }
301
302 if (ip_mrouter) {
303 /*
304 * If we are acting as a multicast router, all
305 * incoming multicast packets are passed to the
306 * kernel-level multicast forwarding function.
307 * The packet is returned (relatively) intact; if
308 * ip_mforward() returns a non-zero value, the packet
309 * must be discarded, else it may be accepted below.
310 *
311 * (The IP ident field is put in the same byte order
312 * as expected when ip_mforward() is called from
313 * ip_output().)
314 */
315 ip->ip_id = htons(ip->ip_id);
316 if (ip_mforward(m, m->m_pkthdr.rcvif) != 0) {
317 ipstat.ips_cantforward++;
318 m_freem(m);
319 goto next;
320 }
321 ip->ip_id = ntohs(ip->ip_id);
322
323 /*
324 * The process-level routing demon needs to receive
325 * all multicast IGMP packets, whether or not this
326 * host belongs to their destination groups.
327 */
328 if (ip->ip_p == IPPROTO_IGMP)
329 goto ours;
330 ipstat.ips_forward++;
331 }
332 #endif
333 /*
334 * See if we belong to the destination multicast group on the
335 * arrival interface.
336 */
337 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
338 if (inm == NULL) {
339 ipstat.ips_cantforward++;
340 m_freem(m);
341 goto next;
342 }
343 goto ours;
344 }
345 if (ip->ip_dst.s_addr == INADDR_BROADCAST ||
346 in_nullhost(ip->ip_dst))
347 goto ours;
348
349 /*
350 * Not for us; forward if possible and desirable.
351 */
352 if (ipforwarding == 0) {
353 ipstat.ips_cantforward++;
354 m_freem(m);
355 } else
356 ip_forward(m, 0);
357 goto next;
358
359 ours:
360 /*
361 * If offset or IP_MF are set, must reassemble.
362 * Otherwise, nothing need be done.
363 * (We could look in the reassembly queue to see
364 * if the packet was previously fragmented,
365 * but it's not worth the time; just let them time out.)
366 */
367 if (ip->ip_off & ~(IP_DF|IP_RF)) {
368 if (m->m_flags & M_EXT) { /* XXX */
369 if ((m = m_pullup(m, sizeof (struct ip))) == 0) {
370 ipstat.ips_toosmall++;
371 goto next;
372 }
373 ip = mtod(m, struct ip *);
374 }
375 /*
376 * Look for queue of fragments
377 * of this datagram.
378 */
379 for (fp = ipq.lh_first; fp != NULL; fp = fp->ipq_q.le_next)
380 if (ip->ip_id == fp->ipq_id &&
381 in_hosteq(ip->ip_src, fp->ipq_src) &&
382 in_hosteq(ip->ip_dst, fp->ipq_dst) &&
383 ip->ip_p == fp->ipq_p)
384 goto found;
385 fp = 0;
386 found:
387
388 /*
389 * Adjust ip_len to not reflect header,
390 * set ipqe_mff if more fragments are expected,
391 * convert offset of this to bytes.
392 */
393 ip->ip_len -= hlen;
394 mff = (ip->ip_off & IP_MF) != 0;
395 if (mff) {
396 /*
397 * Make sure that fragments have a data length
398 * that's a non-zero multiple of 8 bytes.
399 */
400 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
401 ipstat.ips_badfrags++;
402 goto bad;
403 }
404 }
405 ip->ip_off <<= 3;
406
407 /*
408 * If datagram marked as having more fragments
409 * or if this is not the first fragment,
410 * attempt reassembly; if it succeeds, proceed.
411 */
412 if (mff || ip->ip_off) {
413 ipstat.ips_fragments++;
414 MALLOC(ipqe, struct ipqent *, sizeof (struct ipqent),
415 M_IPQ, M_NOWAIT);
416 if (ipqe == NULL) {
417 ipstat.ips_rcvmemdrop++;
418 goto bad;
419 }
420 ipqe->ipqe_mff = mff;
421 ipqe->ipqe_ip = ip;
422 ip = ip_reass(ipqe, fp);
423 if (ip == 0)
424 goto next;
425 ipstat.ips_reassembled++;
426 m = dtom(ip);
427 } else
428 if (fp)
429 ip_freef(fp);
430 } else
431 ip->ip_len -= hlen;
432
433 /*
434 * Switch out to protocol's input routine.
435 */
436 ipstat.ips_delivered++;
437 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
438 goto next;
439 bad:
440 #ifdef PFIL_HOOKS
441 m0 = m;
442 for (pfh = pfil_hook_get(PFIL_BAD); pfh; pfh = pfh->pfil_link.le_next)
443 if (pfh->pfil_func) {
444 (void)pfh->pfil_func(ip, hlen, m->m_pkthdr.rcvif, 2, &m0);
445 ip = mtod(m = m0, struct ip *);
446 }
447 #endif /* PFIL_HOOKS */
448 m_freem(m);
449 goto next;
450 }
451
452 /*
453 * Take incoming datagram fragment and try to
454 * reassemble it into whole datagram. If a chain for
455 * reassembly of this datagram already exists, then it
456 * is given as fp; otherwise have to make a chain.
457 */
458 struct ip *
459 ip_reass(ipqe, fp)
460 register struct ipqent *ipqe;
461 register struct ipq *fp;
462 {
463 register struct mbuf *m = dtom(ipqe->ipqe_ip);
464 register struct ipqent *nq, *p, *q;
465 struct ip *ip;
466 struct mbuf *t;
467 int hlen = ipqe->ipqe_ip->ip_hl << 2;
468 int i, next;
469
470 /*
471 * Presence of header sizes in mbufs
472 * would confuse code below.
473 */
474 m->m_data += hlen;
475 m->m_len -= hlen;
476
477 /*
478 * If first fragment to arrive, create a reassembly queue.
479 */
480 if (fp == 0) {
481 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
482 goto dropfrag;
483 fp = mtod(t, struct ipq *);
484 LIST_INSERT_HEAD(&ipq, fp, ipq_q);
485 fp->ipq_ttl = IPFRAGTTL;
486 fp->ipq_p = ipqe->ipqe_ip->ip_p;
487 fp->ipq_id = ipqe->ipqe_ip->ip_id;
488 LIST_INIT(&fp->ipq_fragq);
489 fp->ipq_src = ipqe->ipqe_ip->ip_src;
490 fp->ipq_dst = ipqe->ipqe_ip->ip_dst;
491 p = NULL;
492 goto insert;
493 }
494
495 /*
496 * Find a segment which begins after this one does.
497 */
498 for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
499 p = q, q = q->ipqe_q.le_next)
500 if (q->ipqe_ip->ip_off > ipqe->ipqe_ip->ip_off)
501 break;
502
503 /*
504 * If there is a preceding segment, it may provide some of
505 * our data already. If so, drop the data from the incoming
506 * segment. If it provides all of our data, drop us.
507 */
508 if (p != NULL) {
509 i = p->ipqe_ip->ip_off + p->ipqe_ip->ip_len -
510 ipqe->ipqe_ip->ip_off;
511 if (i > 0) {
512 if (i >= ipqe->ipqe_ip->ip_len)
513 goto dropfrag;
514 m_adj(dtom(ipqe->ipqe_ip), i);
515 ipqe->ipqe_ip->ip_off += i;
516 ipqe->ipqe_ip->ip_len -= i;
517 }
518 }
519
520 /*
521 * While we overlap succeeding segments trim them or,
522 * if they are completely covered, dequeue them.
523 */
524 for (; q != NULL && ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len >
525 q->ipqe_ip->ip_off; q = nq) {
526 i = (ipqe->ipqe_ip->ip_off + ipqe->ipqe_ip->ip_len) -
527 q->ipqe_ip->ip_off;
528 if (i < q->ipqe_ip->ip_len) {
529 q->ipqe_ip->ip_len -= i;
530 q->ipqe_ip->ip_off += i;
531 m_adj(dtom(q->ipqe_ip), i);
532 break;
533 }
534 nq = q->ipqe_q.le_next;
535 m_freem(dtom(q->ipqe_ip));
536 LIST_REMOVE(q, ipqe_q);
537 FREE(q, M_IPQ);
538 }
539
540 insert:
541 /*
542 * Stick new segment in its place;
543 * check for complete reassembly.
544 */
545 if (p == NULL) {
546 LIST_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q);
547 } else {
548 LIST_INSERT_AFTER(p, ipqe, ipqe_q);
549 }
550 next = 0;
551 for (p = NULL, q = fp->ipq_fragq.lh_first; q != NULL;
552 p = q, q = q->ipqe_q.le_next) {
553 if (q->ipqe_ip->ip_off != next)
554 return (0);
555 next += q->ipqe_ip->ip_len;
556 }
557 if (p->ipqe_mff)
558 return (0);
559
560 /*
561 * Reassembly is complete; concatenate fragments.
562 */
563 q = fp->ipq_fragq.lh_first;
564 ip = q->ipqe_ip;
565 m = dtom(q->ipqe_ip);
566 t = m->m_next;
567 m->m_next = 0;
568 m_cat(m, t);
569 nq = q->ipqe_q.le_next;
570 FREE(q, M_IPQ);
571 for (q = nq; q != NULL; q = nq) {
572 t = dtom(q->ipqe_ip);
573 nq = q->ipqe_q.le_next;
574 FREE(q, M_IPQ);
575 m_cat(m, t);
576 }
577
578 /*
579 * Create header for new ip packet by
580 * modifying header of first packet;
581 * dequeue and discard fragment reassembly header.
582 * Make header visible.
583 */
584 ip->ip_len = next;
585 ip->ip_src = fp->ipq_src;
586 ip->ip_dst = fp->ipq_dst;
587 LIST_REMOVE(fp, ipq_q);
588 (void) m_free(dtom(fp));
589 m->m_len += (ip->ip_hl << 2);
590 m->m_data -= (ip->ip_hl << 2);
591 /* some debugging cruft by sklower, below, will go away soon */
592 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
593 register int plen = 0;
594 for (t = m; m; m = m->m_next)
595 plen += m->m_len;
596 t->m_pkthdr.len = plen;
597 }
598 return (ip);
599
600 dropfrag:
601 ipstat.ips_fragdropped++;
602 m_freem(m);
603 FREE(ipqe, M_IPQ);
604 return (0);
605 }
606
607 /*
608 * Free a fragment reassembly header and all
609 * associated datagrams.
610 */
611 void
612 ip_freef(fp)
613 struct ipq *fp;
614 {
615 register struct ipqent *q, *p;
616
617 for (q = fp->ipq_fragq.lh_first; q != NULL; q = p) {
618 p = q->ipqe_q.le_next;
619 m_freem(dtom(q->ipqe_ip));
620 LIST_REMOVE(q, ipqe_q);
621 FREE(q, M_IPQ);
622 }
623 LIST_REMOVE(fp, ipq_q);
624 (void) m_free(dtom(fp));
625 }
626
627 /*
628 * IP timer processing;
629 * if a timer expires on a reassembly
630 * queue, discard it.
631 */
632 void
633 ip_slowtimo()
634 {
635 register struct ipq *fp, *nfp;
636 int s = splsoftnet();
637
638 for (fp = ipq.lh_first; fp != NULL; fp = nfp) {
639 nfp = fp->ipq_q.le_next;
640 if (--fp->ipq_ttl == 0) {
641 ipstat.ips_fragtimeout++;
642 ip_freef(fp);
643 }
644 }
645 splx(s);
646 }
647
648 /*
649 * Drain off all datagram fragments.
650 */
651 void
652 ip_drain()
653 {
654
655 while (ipq.lh_first != NULL) {
656 ipstat.ips_fragdropped++;
657 ip_freef(ipq.lh_first);
658 }
659 }
660
661 /*
662 * Do option processing on a datagram,
663 * possibly discarding it if bad options are encountered,
664 * or forwarding it if source-routed.
665 * Returns 1 if packet has been forwarded/freed,
666 * 0 if the packet should be processed further.
667 */
668 int
669 ip_dooptions(m)
670 struct mbuf *m;
671 {
672 register struct ip *ip = mtod(m, struct ip *);
673 register u_char *cp;
674 register struct ip_timestamp *ipt;
675 register struct in_ifaddr *ia;
676 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
677 struct in_addr *sin, dst;
678 n_time ntime;
679
680 dst = ip->ip_dst;
681 cp = (u_char *)(ip + 1);
682 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
683 for (; cnt > 0; cnt -= optlen, cp += optlen) {
684 opt = cp[IPOPT_OPTVAL];
685 if (opt == IPOPT_EOL)
686 break;
687 if (opt == IPOPT_NOP)
688 optlen = 1;
689 else {
690 optlen = cp[IPOPT_OLEN];
691 if (optlen <= 0 || optlen > cnt) {
692 code = &cp[IPOPT_OLEN] - (u_char *)ip;
693 goto bad;
694 }
695 }
696 switch (opt) {
697
698 default:
699 break;
700
701 /*
702 * Source routing with record.
703 * Find interface with current destination address.
704 * If none on this machine then drop if strictly routed,
705 * or do nothing if loosely routed.
706 * Record interface address and bring up next address
707 * component. If strictly routed make sure next
708 * address is on directly accessible net.
709 */
710 case IPOPT_LSRR:
711 case IPOPT_SSRR:
712 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
713 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
714 goto bad;
715 }
716 ipaddr.sin_addr = ip->ip_dst;
717 ia = ifatoia(ifa_ifwithaddr(sintosa(&ipaddr)));
718 if (ia == 0) {
719 if (opt == IPOPT_SSRR) {
720 type = ICMP_UNREACH;
721 code = ICMP_UNREACH_SRCFAIL;
722 goto bad;
723 }
724 /*
725 * Loose routing, and not at next destination
726 * yet; nothing to do except forward.
727 */
728 break;
729 }
730 off--; /* 0 origin */
731 if (off > optlen - sizeof(struct in_addr)) {
732 /*
733 * End of source route. Should be for us.
734 */
735 save_rte(cp, ip->ip_src);
736 break;
737 }
738 /*
739 * locate outgoing interface
740 */
741 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
742 sizeof(ipaddr.sin_addr));
743 if (opt == IPOPT_SSRR) {
744 #define INA struct in_ifaddr *
745 #define SA struct sockaddr *
746 ia = (INA)ifa_ifwithladdr((SA)&ipaddr);
747 } else
748 ia = ip_rtaddr(ipaddr.sin_addr);
749 if (ia == 0) {
750 type = ICMP_UNREACH;
751 code = ICMP_UNREACH_SRCFAIL;
752 goto bad;
753 }
754 ip->ip_dst = ipaddr.sin_addr;
755 bcopy((caddr_t)&ia->ia_addr.sin_addr,
756 (caddr_t)(cp + off), sizeof(struct in_addr));
757 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
758 /*
759 * Let ip_intr's mcast routing check handle mcast pkts
760 */
761 forward = !IN_MULTICAST(ip->ip_dst.s_addr);
762 break;
763
764 case IPOPT_RR:
765 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
766 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
767 goto bad;
768 }
769 /*
770 * If no space remains, ignore.
771 */
772 off--; /* 0 origin */
773 if (off > optlen - sizeof(struct in_addr))
774 break;
775 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr,
776 sizeof(ipaddr.sin_addr));
777 /*
778 * locate outgoing interface; if we're the destination,
779 * use the incoming interface (should be same).
780 */
781 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
782 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
783 type = ICMP_UNREACH;
784 code = ICMP_UNREACH_HOST;
785 goto bad;
786 }
787 bcopy((caddr_t)&ia->ia_addr.sin_addr,
788 (caddr_t)(cp + off), sizeof(struct in_addr));
789 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
790 break;
791
792 case IPOPT_TS:
793 code = cp - (u_char *)ip;
794 ipt = (struct ip_timestamp *)cp;
795 if (ipt->ipt_len < 5)
796 goto bad;
797 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (int32_t)) {
798 if (++ipt->ipt_oflw == 0)
799 goto bad;
800 break;
801 }
802 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
803 switch (ipt->ipt_flg) {
804
805 case IPOPT_TS_TSONLY:
806 break;
807
808 case IPOPT_TS_TSANDADDR:
809 if (ipt->ipt_ptr + sizeof(n_time) +
810 sizeof(struct in_addr) > ipt->ipt_len)
811 goto bad;
812 ipaddr.sin_addr = dst;
813 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
814 m->m_pkthdr.rcvif);
815 if (ia == 0)
816 continue;
817 bcopy((caddr_t)&ia->ia_addr.sin_addr,
818 (caddr_t)sin, sizeof(struct in_addr));
819 ipt->ipt_ptr += sizeof(struct in_addr);
820 break;
821
822 case IPOPT_TS_PRESPEC:
823 if (ipt->ipt_ptr + sizeof(n_time) +
824 sizeof(struct in_addr) > ipt->ipt_len)
825 goto bad;
826 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr,
827 sizeof(struct in_addr));
828 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
829 continue;
830 ipt->ipt_ptr += sizeof(struct in_addr);
831 break;
832
833 default:
834 goto bad;
835 }
836 ntime = iptime();
837 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
838 sizeof(n_time));
839 ipt->ipt_ptr += sizeof(n_time);
840 }
841 }
842 if (forward) {
843 if (ip_forwsrcrt == 0) {
844 type = ICMP_UNREACH;
845 code = ICMP_UNREACH_SRCFAIL;
846 goto bad;
847 }
848 ip_forward(m, 1);
849 return (1);
850 }
851 return (0);
852 bad:
853 ip->ip_len -= ip->ip_hl << 2; /* XXX icmp_error adds in hdr length */
854 icmp_error(m, type, code, 0, 0);
855 ipstat.ips_badoptions++;
856 return (1);
857 }
858
859 /*
860 * Given address of next destination (final or next hop),
861 * return internet address info of interface to be used to get there.
862 */
863 struct in_ifaddr *
864 ip_rtaddr(dst)
865 struct in_addr dst;
866 {
867 register struct sockaddr_in *sin;
868
869 sin = satosin(&ipforward_rt.ro_dst);
870
871 if (ipforward_rt.ro_rt == 0 || !in_hosteq(dst, sin->sin_addr)) {
872 if (ipforward_rt.ro_rt) {
873 RTFREE(ipforward_rt.ro_rt);
874 ipforward_rt.ro_rt = 0;
875 }
876 sin->sin_family = AF_INET;
877 sin->sin_len = sizeof(*sin);
878 sin->sin_addr = dst;
879
880 rtalloc(&ipforward_rt);
881 }
882 if (ipforward_rt.ro_rt == 0)
883 return ((struct in_ifaddr *)0);
884 return (ifatoia(ipforward_rt.ro_rt->rt_ifa));
885 }
886
887 /*
888 * Save incoming source route for use in replies,
889 * to be picked up later by ip_srcroute if the receiver is interested.
890 */
891 void
892 save_rte(option, dst)
893 u_char *option;
894 struct in_addr dst;
895 {
896 unsigned olen;
897
898 olen = option[IPOPT_OLEN];
899 #ifdef DIAGNOSTIC
900 if (ipprintfs)
901 printf("save_rte: olen %d\n", olen);
902 #endif
903 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
904 return;
905 bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
906 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
907 ip_srcrt.dst = dst;
908 }
909
910 /*
911 * Retrieve incoming source route for use in replies,
912 * in the same form used by setsockopt.
913 * The first hop is placed before the options, will be removed later.
914 */
915 struct mbuf *
916 ip_srcroute()
917 {
918 register struct in_addr *p, *q;
919 register struct mbuf *m;
920
921 if (ip_nhops == 0)
922 return ((struct mbuf *)0);
923 m = m_get(M_DONTWAIT, MT_SOOPTS);
924 if (m == 0)
925 return ((struct mbuf *)0);
926
927 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
928
929 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
930 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
931 OPTSIZ;
932 #ifdef DIAGNOSTIC
933 if (ipprintfs)
934 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
935 #endif
936
937 /*
938 * First save first hop for return route
939 */
940 p = &ip_srcrt.route[ip_nhops - 1];
941 *(mtod(m, struct in_addr *)) = *p--;
942 #ifdef DIAGNOSTIC
943 if (ipprintfs)
944 printf(" hops %x", ntohl(mtod(m, struct in_addr *)->s_addr));
945 #endif
946
947 /*
948 * Copy option fields and padding (nop) to mbuf.
949 */
950 ip_srcrt.nop = IPOPT_NOP;
951 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
952 bcopy((caddr_t)&ip_srcrt.nop,
953 mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
954 q = (struct in_addr *)(mtod(m, caddr_t) +
955 sizeof(struct in_addr) + OPTSIZ);
956 #undef OPTSIZ
957 /*
958 * Record return path as an IP source route,
959 * reversing the path (pointers are now aligned).
960 */
961 while (p >= ip_srcrt.route) {
962 #ifdef DIAGNOSTIC
963 if (ipprintfs)
964 printf(" %x", ntohl(q->s_addr));
965 #endif
966 *q++ = *p--;
967 }
968 /*
969 * Last hop goes to final destination.
970 */
971 *q = ip_srcrt.dst;
972 #ifdef DIAGNOSTIC
973 if (ipprintfs)
974 printf(" %x\n", ntohl(q->s_addr));
975 #endif
976 return (m);
977 }
978
979 /*
980 * Strip out IP options, at higher
981 * level protocol in the kernel.
982 * Second argument is buffer to which options
983 * will be moved, and return value is their length.
984 * XXX should be deleted; last arg currently ignored.
985 */
986 void
987 ip_stripoptions(m, mopt)
988 register struct mbuf *m;
989 struct mbuf *mopt;
990 {
991 register int i;
992 struct ip *ip = mtod(m, struct ip *);
993 register caddr_t opts;
994 int olen;
995
996 olen = (ip->ip_hl<<2) - sizeof (struct ip);
997 opts = (caddr_t)(ip + 1);
998 i = m->m_len - (sizeof (struct ip) + olen);
999 bcopy(opts + olen, opts, (unsigned)i);
1000 m->m_len -= olen;
1001 if (m->m_flags & M_PKTHDR)
1002 m->m_pkthdr.len -= olen;
1003 ip->ip_hl = sizeof(struct ip) >> 2;
1004 }
1005
1006 int inetctlerrmap[PRC_NCMDS] = {
1007 0, 0, 0, 0,
1008 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1009 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1010 EMSGSIZE, EHOSTUNREACH, 0, 0,
1011 0, 0, 0, 0,
1012 ENOPROTOOPT
1013 };
1014
1015 /*
1016 * Forward a packet. If some error occurs return the sender
1017 * an icmp packet. Note we can't always generate a meaningful
1018 * icmp message because icmp doesn't have a large enough repertoire
1019 * of codes and types.
1020 *
1021 * If not forwarding, just drop the packet. This could be confusing
1022 * if ipforwarding was zero but some routing protocol was advancing
1023 * us as a gateway to somewhere. However, we must let the routing
1024 * protocol deal with that.
1025 *
1026 * The srcrt parameter indicates whether the packet is being forwarded
1027 * via a source route.
1028 */
1029 void
1030 ip_forward(m, srcrt)
1031 struct mbuf *m;
1032 int srcrt;
1033 {
1034 register struct ip *ip = mtod(m, struct ip *);
1035 register struct sockaddr_in *sin;
1036 register struct rtentry *rt;
1037 int error, type = 0, code = 0;
1038 struct mbuf *mcopy;
1039 n_long dest;
1040 struct ifnet *destifp;
1041
1042 dest = 0;
1043 #ifdef DIAGNOSTIC
1044 if (ipprintfs)
1045 printf("forward: src %x dst %x ttl %x\n",
1046 ip->ip_src.s_addr, ip->ip_dst.s_addr, ip->ip_ttl);
1047 #endif
1048 if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
1049 ipstat.ips_cantforward++;
1050 m_freem(m);
1051 return;
1052 }
1053 HTONS(ip->ip_id);
1054 if (ip->ip_ttl <= IPTTLDEC) {
1055 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1056 return;
1057 }
1058 ip->ip_ttl -= IPTTLDEC;
1059
1060 sin = satosin(&ipforward_rt.ro_dst);
1061 if ((rt = ipforward_rt.ro_rt) == 0 ||
1062 !in_hosteq(ip->ip_dst, sin->sin_addr)) {
1063 if (ipforward_rt.ro_rt) {
1064 RTFREE(ipforward_rt.ro_rt);
1065 ipforward_rt.ro_rt = 0;
1066 }
1067 sin->sin_family = AF_INET;
1068 sin->sin_len = sizeof(struct sockaddr_in);
1069 sin->sin_addr = ip->ip_dst;
1070
1071 rtalloc(&ipforward_rt);
1072 if (ipforward_rt.ro_rt == 0) {
1073 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1074 return;
1075 }
1076 rt = ipforward_rt.ro_rt;
1077 }
1078
1079 /*
1080 * Save at most 68 bytes of the packet in case
1081 * we need to generate an ICMP message to the src.
1082 */
1083 mcopy = m_copy(m, 0, imin((int)ip->ip_len, 68));
1084
1085 /*
1086 * If forwarding packet using same interface that it came in on,
1087 * perhaps should send a redirect to sender to shortcut a hop.
1088 * Only send redirect if source is sending directly to us,
1089 * and if packet was not source routed (or has any options).
1090 * Also, don't send redirect if forwarding using a default route
1091 * or a route modified by a redirect.
1092 */
1093 if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1094 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1095 !in_nullhost(satosin(rt_key(rt))->sin_addr) &&
1096 ipsendredirects && !srcrt) {
1097 if (rt->rt_ifa &&
1098 (ip->ip_src.s_addr & ifatoia(rt->rt_ifa)->ia_subnetmask) ==
1099 ifatoia(rt->rt_ifa)->ia_subnet) {
1100 if (rt->rt_flags & RTF_GATEWAY)
1101 dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1102 else
1103 dest = ip->ip_dst.s_addr;
1104 /* Router requirements says to only send host redirects */
1105 type = ICMP_REDIRECT;
1106 code = ICMP_REDIRECT_HOST;
1107 #ifdef DIAGNOSTIC
1108 if (ipprintfs)
1109 printf("redirect (%d) to %x\n", code, (u_int32_t)dest);
1110 #endif
1111 }
1112 }
1113
1114 error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1115 (IP_FORWARDING | (ip_directedbcast ? IP_ALLOWBROADCAST : 0)), 0);
1116 if (error)
1117 ipstat.ips_cantforward++;
1118 else {
1119 ipstat.ips_forward++;
1120 if (type)
1121 ipstat.ips_redirectsent++;
1122 else {
1123 if (mcopy)
1124 m_freem(mcopy);
1125 return;
1126 }
1127 }
1128 if (mcopy == NULL)
1129 return;
1130 destifp = NULL;
1131
1132 switch (error) {
1133
1134 case 0: /* forwarded, but need redirect */
1135 /* type, code set above */
1136 break;
1137
1138 case ENETUNREACH: /* shouldn't happen, checked above */
1139 case EHOSTUNREACH:
1140 case ENETDOWN:
1141 case EHOSTDOWN:
1142 default:
1143 type = ICMP_UNREACH;
1144 code = ICMP_UNREACH_HOST;
1145 break;
1146
1147 case EMSGSIZE:
1148 type = ICMP_UNREACH;
1149 code = ICMP_UNREACH_NEEDFRAG;
1150 if (ipforward_rt.ro_rt)
1151 destifp = ipforward_rt.ro_rt->rt_ifp;
1152 ipstat.ips_cantfrag++;
1153 break;
1154
1155 case ENOBUFS:
1156 type = ICMP_SOURCEQUENCH;
1157 code = 0;
1158 break;
1159 }
1160 icmp_error(mcopy, type, code, dest, destifp);
1161 }
1162
1163 int
1164 ip_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1165 int *name;
1166 u_int namelen;
1167 void *oldp;
1168 size_t *oldlenp;
1169 void *newp;
1170 size_t newlen;
1171 {
1172 /* All sysctl names at this level are terminal. */
1173 if (namelen != 1)
1174 return (ENOTDIR);
1175
1176 switch (name[0]) {
1177 case IPCTL_FORWARDING:
1178 return (sysctl_int(oldp, oldlenp, newp, newlen, &ipforwarding));
1179 case IPCTL_SENDREDIRECTS:
1180 return (sysctl_int(oldp, oldlenp, newp, newlen,
1181 &ipsendredirects));
1182 case IPCTL_DEFTTL:
1183 return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_defttl));
1184 #ifdef notyet
1185 case IPCTL_DEFMTU:
1186 return (sysctl_int(oldp, oldlenp, newp, newlen, &ip_mtu));
1187 #endif
1188 case IPCTL_FORWSRCRT:
1189 /*
1190 * Don't allow this to change in a secure environment.
1191 */
1192 if (securelevel > 0)
1193 return (EPERM);
1194 return (sysctl_int(oldp, oldlenp, newp, newlen,
1195 &ip_forwsrcrt));
1196 case IPCTL_DIRECTEDBCAST:
1197 return (sysctl_int(oldp, oldlenp, newp, newlen,
1198 &ip_directedbcast));
1199 default:
1200 return (EOPNOTSUPP);
1201 }
1202 /* NOTREACHED */
1203 }
1204