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