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