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