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