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