Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / network / stacks / AROSTCP / bsdsocket / netinet / ip_input.c
blob6a42706339707f2119f9f82f99b8dd7d3b3e36e7
1 /*
2 * Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
3 * Helsinki University of Technology, Finland.
4 * All rights reserved.
5 * Copyright (C) 2005 Neil Cafferkey
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
24 * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
25 * All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
55 * @(#)ip_input.c 7.19 (Berkeley) 5/25/91
58 #include <conf.h>
60 #include <libraries/miami.h>
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/malloc.h>
64 #include <sys/mbuf.h>
65 #include <sys/domain.h>
66 #include <sys/protosw.h>
67 #include <sys/socket.h>
68 #include <sys/errno.h>
69 #include <sys/kernel.h>
70 #include <sys/synch.h>
72 #include <net/if.h>
73 #include <net/if_protos.h>
74 #include <net/route.h>
75 #include <net/pfil.h>
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/ip.h>
80 #include <netinet/in_pcb.h>
81 #include <netinet/in_var.h>
82 #include <netinet/ip_var.h>
83 #include <netinet/ip_icmp.h>
85 #include <netinet/ip_input_protos.h>
86 #include <netinet/ip_output_protos.h>
87 #include <netinet/ip_icmp_protos.h>
88 #include <netinet/in_cksum_protos.h>
89 #include <netinet/in_protos.h>
90 #include <kern/uipc_domain_protos.h>
92 #ifndef IPFORWARDING
93 #define IPFORWARDING 0
94 #endif
95 #ifndef IPSENDREDIRECTS
96 #define IPSENDREDIRECTS 1
97 #endif
98 #ifndef IPPRINTFS
99 #define IPPRINTFS 0
100 #endif
102 /* These three are now accessed from the configuration module
104 int ipforwarding = IPFORWARDING;
105 int ipsendredirects = IPSENDREDIRECTS;
106 int ipprintfs = IPPRINTFS; /* this has effect only if DIAGNOSTIC */
108 struct ipstat ipstat = {0}; /* ip statistics */
109 struct ipq ipq = {0}; /* ip reass. queue */
110 u_short ip_id = {0}; /* ip packet ctr, for ids */
111 struct ifqueue ipintrq = {0}; /* ip packet input queue */
113 extern struct domain inetdomain;
114 extern struct protosw inetsw[];
115 u_char ip_protox[IPPROTO_MAX] = {0};
116 int ipqmaxlen = IFQ_MAXLEN;
117 struct in_ifaddr *in_ifaddr = NULL; /* first inet address */
120 * We need to save the IP options in case a protocol wants to respond
121 * to an incoming packet over the same route if the packet got here
122 * using IP source routing. This allows connection establishment and
123 * maintenance when the remote end is on a network that is not known
124 * to us.
126 int ip_nhops = 0;
127 static struct ip_srcrt {
128 struct in_addr dst; /* final destination */
129 char nop; /* one NOP to align */
130 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
131 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
132 } ip_srcrt;
134 #if USE_IF_MATRIX
135 extern int if_index;
136 u_long *ip_ifmatrix = NULL;
137 #endif
140 * IP initialization: fill in IP protocol switch table.
141 * All protocols not implemented in kernel go to raw IP protocol handler.
143 void
144 ip_init()
146 register struct protosw *pr;
147 register int i;
149 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
150 if (pr == 0)
151 panic("ip_init");
152 for (i = 0; i < IPPROTO_MAX; i++)
153 ip_protox[i] = pr - inetsw;
154 for (pr = inetdomain.dom_protosw;
155 pr < inetdomain.dom_protoswNPROTOSW; pr++)
156 if (pr->pr_domain->dom_family == PF_INET &&
157 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
158 ip_protox[pr->pr_protocol] = pr - inetsw;
159 ipq.next = ipq.prev = &ipq;
161 struct timeval time;
162 GetSysTime(&time);
163 ip_id = time.tv_sec & 0xffff;
165 ipintrq.ifq_maxlen = ipqmaxlen;
167 #if USE_IF_MATRIX
168 This does not work, since if_index is not constant, but actually
169 increases after this is done! So change this before enabling
170 (if_index is defined in net/if.c)
172 i = (if_index + 1) * (if_index + 1) * sizeof (u_long);
173 if ((ip_ifmatrix = (u_long *) bsd_malloc(i, M_RTABLE, M_WAITOK)) == 0)
174 panic("no memory for ip_ifmatrix");
175 #endif
178 struct ip *ip_reass();
179 struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
180 struct route ipforward_rt = { 0 };
183 * Ip input routine. Checksum and byte swap header. If fragmented
184 * try to reassemble. Process options. Pass to next level.
186 void
187 ipintr()
189 register struct ip *ip;
190 register struct mbuf *m;
191 register struct ipq *fp;
192 register struct in_ifaddr *ia;
193 int hlen;
194 spl_t s;
196 next:
198 * Get next datagram off input queue and get IP header
199 * in first mbuf.
201 s = splimp();
202 IF_DEQUEUE(&ipintrq, m);
203 splx(s);
204 if (m == 0)
205 return;
206 #if DIAGNOSTIC
207 if ((m->m_flags & M_PKTHDR) == 0)
208 panic("ipintr no HDR");
209 #endif
211 * If no IP addresses have been set yet but the interfaces
212 * are receiving, can't do anything with incoming packets yet.
214 if (in_ifaddr == NULL)
215 goto bad;
216 ipstat.ips_total++;
217 if (m->m_len < sizeof (struct ip) &&
218 (m = m_pullup(m, sizeof (struct ip))) == 0) {
219 ipstat.ips_toosmall++;
220 goto next;
222 ip = mtod(m, struct ip *);
223 hlen = ip->ip_hl << 2;
224 if (hlen < sizeof(struct ip)) { /* minimum header length */
225 ipstat.ips_badhlen++;
226 goto bad;
228 if (hlen > m->m_len) {
229 if ((m = m_pullup(m, hlen)) == 0) {
230 ipstat.ips_badhlen++;
231 goto next;
233 ip = mtod(m, struct ip *);
235 if (ip->ip_sum = in_cksum(m, hlen)) {
236 ipstat.ips_badsum++;
237 goto bad;
241 * Convert fields to host representation.
243 NTOHS(ip->ip_len);
244 if (ip->ip_len < hlen) {
245 ipstat.ips_badlen++;
246 goto bad;
248 NTOHS(ip->ip_id);
249 NTOHS(ip->ip_off);
252 * Check that the amount of data in the buffers
253 * is as at least much as the IP header would have us expect.
254 * Trim mbufs if longer than we expect.
255 * Drop packet if shorter than we expect.
257 if (m->m_pkthdr.len < ip->ip_len) {
258 ipstat.ips_tooshort++;
259 goto bad;
261 if (m->m_pkthdr.len > ip->ip_len) {
262 if (m->m_len == m->m_pkthdr.len) {
263 m->m_len = ip->ip_len;
264 m->m_pkthdr.len = ip->ip_len;
265 } else
266 m_adj(m, ip->ip_len - m->m_pkthdr.len);
270 * Run through list of hooks.
272 pfil_run_hooks(m, m->m_pkthdr.rcvif, MIAMIPFBPT_IP);
275 * Process options and, if not destined for us,
276 * ship it on. ip_dooptions returns 1 when an
277 * error was detected (causing an icmp message
278 * to be sent and the original packet to be freed).
280 ip_nhops = 0; /* for source routed packets */
281 if (hlen > sizeof (struct ip) && ip_dooptions(m))
282 goto next;
285 * Check our list of addresses, to see if the packet is for us.
287 for (ia = in_ifaddr; ia; ia = ia->ia_next) {
288 #define satosin(sa) ((struct sockaddr_in *)(sa))
290 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
291 goto ours;
292 if (
293 #ifdef DIRECTED_BROADCAST
294 ia->ia_ifp == m->m_pkthdr.rcvif &&
295 #endif
296 (ia->ia_ifp->if_flags & IFF_BROADCAST)) {
297 u_long t;
299 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
300 ip->ip_dst.s_addr)
301 goto ours;
302 if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr)
303 goto ours;
305 * Look for all-0's host part (old broadcast addr),
306 * either for subnet or net.
308 t = ntohl(ip->ip_dst.s_addr);
309 if (t == ia->ia_subnet)
310 goto ours;
311 if (t == ia->ia_net)
312 goto ours;
315 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
316 goto ours;
317 if (ip->ip_dst.s_addr == INADDR_ANY)
318 goto ours;
321 * Not for us; forward if possible and desirable.
323 if (ipforwarding == 0) {
324 ipstat.ips_cantforward++;
325 m_freem(m);
326 } else
327 ip_forward(m, 0);
328 goto next;
330 ours:
332 * If offset or IP_MF are set, must reassemble.
333 * Otherwise, nothing need be done.
334 * (We could look in the reassembly queue to see
335 * if the packet was previously fragmented,
336 * but it's not worth the time; just let them time out.)
338 if (ip->ip_off &~ IP_DF) {
339 if (m->m_flags & M_EXT) { /* XXX */
340 if ((m = m_pullup(m, sizeof (struct ip))) == 0) {
341 ipstat.ips_toosmall++;
342 goto next;
344 ip = mtod(m, struct ip *);
347 * Look for queue of fragments
348 * of this datagram.
350 for (fp = ipq.next; fp != &ipq; fp = fp->next)
351 if (ip->ip_id == fp->ipq_id &&
352 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
353 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
354 ip->ip_p == fp->ipq_p)
355 goto found;
356 fp = 0;
357 found:
360 * Adjust ip_len to not reflect header,
361 * set ip_mff if more fragments are expected,
362 * convert offset of this to bytes.
364 ip->ip_len -= hlen;
365 ((struct ipasfrag *)ip)->ipf_mff = 0;
366 if (ip->ip_off & IP_MF)
367 ((struct ipasfrag *)ip)->ipf_mff = 1;
368 ip->ip_off <<= 3;
371 * If datagram marked as having more fragments
372 * or if this is not the first fragment,
373 * attempt reassembly; if it succeeds, proceed.
375 if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) {
376 ipstat.ips_fragments++;
377 ip = ip_reass((struct ipasfrag *)ip, fp);
378 if (ip == 0)
379 goto next;
380 else
381 ipstat.ips_reassembled++;
382 m = dtom(ip);
383 } else
384 if (fp)
385 ip_freef(fp);
386 } else
387 ip->ip_len -= hlen;
390 * Switch out to protocol's input routine.
392 ipstat.ips_delivered++;
393 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
394 goto next;
395 bad:
396 m_freem(m);
397 goto next;
401 * Take incoming datagram fragment and try to
402 * reassemble it into whole datagram. If a chain for
403 * reassembly of this datagram already exists, then it
404 * is given as fp; otherwise have to make a chain.
406 struct ip *
407 ip_reass(ip, fp)
408 register struct ipasfrag *ip;
409 register struct ipq *fp;
411 register struct mbuf *m = dtom(ip);
412 register struct ipasfrag *q;
413 struct mbuf *t;
414 int hlen = ip->ip_hl << 2;
415 int i, next;
418 * Presence of header sizes in mbufs
419 * would confuse code below.
421 m->m_data += hlen;
422 m->m_len -= hlen;
425 * If first fragment to arrive, create a reassembly queue.
427 if (fp == 0) {
428 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
429 goto dropfrag;
430 fp = mtod(t, struct ipq *);
431 insque(fp, &ipq);
432 fp->ipq_ttl = IPFRAGTTL;
433 fp->ipq_p = ip->ip_p;
434 fp->ipq_id = ip->ip_id;
435 fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
436 fp->ipq_src = ((struct ip *)ip)->ip_src;
437 fp->ipq_dst = ((struct ip *)ip)->ip_dst;
438 q = (struct ipasfrag *)fp;
439 goto insert;
443 * Find a segment which begins after this one does.
445 for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
446 if (q->ip_off > ip->ip_off)
447 break;
450 * If there is a preceding segment, it may provide some of
451 * our data already. If so, drop the data from the incoming
452 * segment. If it provides all of our data, drop us.
454 if (q->ipf_prev != (struct ipasfrag *)fp) {
455 i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
456 if (i > 0) {
457 if (i >= ip->ip_len)
458 goto dropfrag;
459 m_adj(dtom(ip), i);
460 ip->ip_off += i;
461 ip->ip_len -= i;
466 * While we overlap succeeding segments trim them or,
467 * if they are completely covered, dequeue them.
469 while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
470 i = (ip->ip_off + ip->ip_len) - q->ip_off;
471 if (i < q->ip_len) {
472 q->ip_len -= i;
473 q->ip_off += i;
474 m_adj(dtom(q), i);
475 break;
477 q = q->ipf_next;
478 m_freem(dtom(q->ipf_prev));
479 ip_deq(q->ipf_prev);
482 insert:
484 * Stick new segment in its place;
485 * check for complete reassembly.
487 ip_enq(ip, q->ipf_prev);
488 next = 0;
489 for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
490 if (q->ip_off != next)
491 return (0);
492 next += q->ip_len;
494 if (q->ipf_prev->ipf_mff)
495 return (0);
498 * Reassembly is complete; concatenate fragments.
500 q = fp->ipq_next;
501 m = dtom(q);
502 t = m->m_next;
503 m->m_next = 0;
504 m_cat(m, t);
505 q = q->ipf_next;
506 while (q != (struct ipasfrag *)fp) {
507 t = dtom(q);
508 q = q->ipf_next;
509 m_cat(m, t);
513 * Create header for new ip packet by
514 * modifying header of first packet;
515 * dequeue and discard fragment reassembly header.
516 * Make header visible.
518 ip = fp->ipq_next;
519 ip->ip_len = next;
520 ((struct ip *)ip)->ip_src = fp->ipq_src;
521 ((struct ip *)ip)->ip_dst = fp->ipq_dst;
522 remque(fp);
523 (void) m_free(dtom(fp));
524 m = dtom(ip);
525 m->m_len += (ip->ip_hl << 2);
526 m->m_data -= (ip->ip_hl << 2);
527 /* some debugging cruft by sklower, below, will go away soon */
528 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
529 register int plen = 0;
530 for (t = m; m; m = m->m_next)
531 plen += m->m_len;
532 t->m_pkthdr.len = plen;
534 return ((struct ip *)ip);
536 dropfrag:
537 ipstat.ips_fragdropped++;
538 m_freem(m);
539 return (0);
543 * Free a fragment reassembly header and all
544 * associated datagrams.
546 void
547 ip_freef(fp)
548 struct ipq *fp;
550 register struct ipasfrag *q, *p;
552 for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) {
553 p = q->ipf_next;
554 ip_deq(q);
555 m_freem(dtom(q));
557 remque(fp);
558 (void) m_free(dtom(fp));
562 * Put an ip fragment on a reassembly chain.
563 * Like insque, but pointers in middle of structure.
565 void
566 ip_enq(p, prev)
567 register struct ipasfrag *p, *prev;
570 p->ipf_prev = prev;
571 p->ipf_next = prev->ipf_next;
572 prev->ipf_next->ipf_prev = p;
573 prev->ipf_next = p;
577 * To ip_enq as remque is to insque.
579 void
580 ip_deq(p)
581 register struct ipasfrag *p;
584 p->ipf_prev->ipf_next = p->ipf_next;
585 p->ipf_next->ipf_prev = p->ipf_prev;
589 * IP timer processing;
590 * if a timer expires on a reassembly
591 * queue, discard it.
593 void
594 ip_slowtimo()
596 register struct ipq *fp;
597 spl_t s = splnet();
599 fp = ipq.next;
600 if (fp == 0) {
601 splx(s);
602 return;
604 while (fp != &ipq) {
605 --fp->ipq_ttl;
606 fp = fp->next;
607 if (fp->prev->ipq_ttl == 0) {
608 ipstat.ips_fragtimeout++;
609 ip_freef(fp->prev);
612 splx(s);
616 * Drain off all datagram fragments.
618 void
619 ip_drain()
622 while (ipq.next != &ipq) {
623 ipstat.ips_fragdropped++;
624 ip_freef(ipq.next);
628 extern struct in_ifaddr *ifptoia();
629 struct in_ifaddr *ip_rtaddr();
632 * Do option processing on a datagram,
633 * possibly discarding it if bad options are encountered,
634 * or forwarding it if source-routed.
635 * Returns 1 if packet has been forwarded/freed,
636 * 0 if the packet should be processed further.
639 ip_dooptions(m)
640 struct mbuf *m;
642 register struct ip *ip = mtod(m, struct ip *);
643 register u_char *cp;
644 register struct ip_timestamp *ipt;
645 register struct in_ifaddr *ia;
646 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
647 struct in_addr *sin, dest;
648 n_time ntime;
650 cp = (u_char *)(ip + 1);
651 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
652 for (; cnt > 0; cnt -= optlen, cp += optlen) {
653 opt = cp[IPOPT_OPTVAL];
654 if (opt == IPOPT_EOL)
655 break;
656 if (opt == IPOPT_NOP)
657 optlen = 1;
658 else {
659 optlen = cp[IPOPT_OLEN];
660 if (optlen <= 0 || optlen > cnt) {
661 code = &cp[IPOPT_OLEN] - (u_char *)ip;
662 goto bad;
665 switch (opt) {
667 default:
668 break;
671 * Source routing with record.
672 * Find interface with current destination address.
673 * If none on this machine then drop if strictly routed,
674 * or do nothing if loosely routed.
675 * Record interface address and bring up next address
676 * component. If strictly routed make sure next
677 * address is on directly accessible net.
679 case IPOPT_LSRR:
680 case IPOPT_SSRR:
681 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
682 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
683 goto bad;
685 ipaddr.sin_addr = ip->ip_dst;
686 ia = (struct in_ifaddr *)
687 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
688 if (ia == 0) {
689 if (opt == IPOPT_SSRR) {
690 type = ICMP_UNREACH;
691 code = ICMP_UNREACH_SRCFAIL;
692 goto bad;
695 * Loose routing, and not at next destination
696 * yet; nothing to do except forward.
698 break;
700 off--; /* 0 origin */
701 if (off > optlen - sizeof(struct in_addr)) {
703 * End of source route. Should be for us.
705 save_rte(cp, ip->ip_src);
706 break;
709 * locate outgoing interface
711 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr,
712 sizeof(ipaddr.sin_addr));
713 if (opt == IPOPT_SSRR) {
714 #define INA struct in_ifaddr *
715 #define SA struct sockaddr *
716 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
717 ia = in_iaonnetof(in_netof(ipaddr.sin_addr));
718 } else
719 ia = ip_rtaddr(ipaddr.sin_addr);
720 if (ia == 0) {
721 type = ICMP_UNREACH;
722 code = ICMP_UNREACH_SRCFAIL;
723 goto bad;
725 ip->ip_dst = ipaddr.sin_addr;
726 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
727 (caddr_t)(cp + off), sizeof(struct in_addr));
728 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
729 forward = 1;
730 break;
732 case IPOPT_RR:
733 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
734 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
735 goto bad;
738 * If no space remains, ignore.
740 off--; /* 0 origin */
741 if (off > optlen - sizeof(struct in_addr))
742 break;
743 ipaddr.sin_addr = ip->ip_dst;
745 * locate outgoing interface; if we're the destination,
746 * use the incoming interface (should be same).
748 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
749 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
750 type = ICMP_UNREACH;
751 code = ICMP_UNREACH_HOST;
752 goto bad;
754 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr),
755 (caddr_t)(cp + off), sizeof(struct in_addr));
756 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
757 break;
759 case IPOPT_TS:
760 code = cp - (u_char *)ip;
761 ipt = (struct ip_timestamp *)cp;
762 if (ipt->ipt_len < 5)
763 goto bad;
764 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
765 if (++ipt->ipt_oflw == 0)
766 goto bad;
767 break;
769 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
770 switch (ipt->ipt_flg) {
772 case IPOPT_TS_TSONLY:
773 break;
775 case IPOPT_TS_TSANDADDR:
776 if (ipt->ipt_ptr + sizeof(n_time) +
777 sizeof(struct in_addr) > ipt->ipt_len)
778 goto bad;
779 ia = ifptoia(m->m_pkthdr.rcvif);
780 *sin = IA_SIN(ia)->sin_addr;
781 ipt->ipt_ptr += sizeof(struct in_addr);
782 break;
784 case IPOPT_TS_PRESPEC:
785 if (ipt->ipt_ptr + sizeof(n_time) +
786 sizeof(struct in_addr) > ipt->ipt_len)
787 goto bad;
788 ipaddr.sin_addr = *sin;
789 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
790 continue;
791 ipt->ipt_ptr += sizeof(struct in_addr);
792 break;
794 default:
795 goto bad;
797 ntime = iptime();
798 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1,
799 sizeof(n_time));
800 ipt->ipt_ptr += sizeof(n_time);
803 if (forward) {
804 ip_forward(m, 1);
805 return (1);
806 } else
807 return (0);
808 bad:
809 icmp_error(m, type, code, dest); /* dest is not used */
810 return (1);
814 * Given address of next destination (final or next hop),
815 * return internet address info of interface to be used to get there.
817 struct in_ifaddr *
818 ip_rtaddr(dst)
819 struct in_addr dst;
821 register struct sockaddr_in *sin;
823 sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
825 if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
826 if (ipforward_rt.ro_rt) {
827 RTFREE(ipforward_rt.ro_rt);
828 ipforward_rt.ro_rt = 0;
830 sin->sin_family = AF_INET;
831 sin->sin_len = sizeof(*sin);
832 sin->sin_addr = dst;
834 rtalloc(&ipforward_rt);
836 if (ipforward_rt.ro_rt == 0)
837 return ((struct in_ifaddr *)0);
838 return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
842 * Save incoming source route for use in replies,
843 * to be picked up later by ip_srcroute if the receiver is interested.
845 void
846 save_rte(option, dst)
847 u_char *option;
848 struct in_addr dst;
850 unsigned olen;
852 olen = option[IPOPT_OLEN];
853 #if DIAGNOSTIC
854 if (ipprintfs)
855 printf("save_rte: olen %ld\n", olen);
856 #endif
857 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
858 return;
859 bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen);
860 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
861 ip_srcrt.dst = dst;
865 * Retrieve incoming source route for use in replies,
866 * in the same form used by setsockopt.
867 * The first hop is placed before the options, will be removed later.
869 struct mbuf *
870 ip_srcroute()
872 register struct in_addr *p, *q;
873 register struct mbuf *m;
875 if (ip_nhops == 0)
876 return ((struct mbuf *)0);
877 m = m_get(M_DONTWAIT, MT_SOOPTS);
878 if (m == 0)
879 return ((struct mbuf *)0);
881 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
883 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
884 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
885 OPTSIZ;
886 #if DIAGNOSTIC
887 if (ipprintfs)
888 printf("ip_srcroute: nhops %ld mlen %ld", ip_nhops, m->m_len);
889 #endif
892 * First save first hop for return route
894 p = &ip_srcrt.route[ip_nhops - 1];
895 *(mtod(m, struct in_addr *)) = *p--;
896 #if DIAGNOSTIC
897 if (ipprintfs)
898 printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr));
899 #endif
902 * Copy option fields and padding (nop) to mbuf.
904 ip_srcrt.nop = IPOPT_NOP;
905 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
906 aligned_bcopy_const((caddr_t)&ip_srcrt.nop,
907 mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ);
908 q = (struct in_addr *)(mtod(m, caddr_t) +
909 sizeof(struct in_addr) + OPTSIZ);
910 #undef OPTSIZ
912 * Record return path as an IP source route,
913 * reversing the path (pointers are now aligned).
915 while (p >= ip_srcrt.route) {
916 #if DIAGNOSTIC
917 if (ipprintfs)
918 printf(" %lx", ntohl(q->s_addr));
919 #endif
920 *q++ = *p--;
923 * Last hop goes to final destination.
925 *q = ip_srcrt.dst;
926 #if DIAGNOSTIC
927 if (ipprintfs)
928 printf(" %lx\n", ntohl(q->s_addr));
929 #endif
930 return (m);
934 * Strip out IP options, at higher
935 * level protocol in the kernel.
936 * Second argument is buffer to which options
937 * will be moved, and return value is their length.
938 * XXX should be deleted; last arg currently ignored.
940 void
941 ip_stripoptions(m, mopt)
942 register struct mbuf *m;
943 struct mbuf *mopt;
945 register int i;
946 struct ip *ip = mtod(m, struct ip *);
947 register caddr_t opts;
948 int olen;
950 olen = (ip->ip_hl<<2) - sizeof (struct ip);
951 opts = (caddr_t)(ip + 1);
952 i = m->m_len - (sizeof (struct ip) + olen);
953 aligned_bcopy(opts + olen, opts, (unsigned)i);
954 m->m_len -= olen;
955 if (m->m_flags & M_PKTHDR)
956 m->m_pkthdr.len -= olen;
957 ip->ip_hl = sizeof(struct ip) >> 2;
960 u_char inetctlerrmap[PRC_NCMDS] = {
961 0, 0, 0, 0,
962 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
963 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
964 EMSGSIZE, EHOSTUNREACH, 0, 0,
965 0, 0, 0, 0,
966 ENOPROTOOPT
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.
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.
980 * The srcrt parameter indicates whether the packet is being forwarded
981 * via a source route.
983 void
984 ip_forward(m, srcrt)
985 struct mbuf *m;
986 int srcrt;
988 register struct ip *ip = mtod(m, struct ip *);
989 register struct sockaddr_in *sin;
990 register struct rtentry *rt;
991 int error, type = 0, code = 0;
992 struct mbuf *mcopy;
993 struct in_addr dest;
995 dest.s_addr = 0;
996 #if DIAGNOSTIC
997 if (ipprintfs)
998 printf("forward: src %lx dst %lx ttl %lx\n", ip->ip_src.s_addr,
999 ip->ip_dst.s_addr, ip->ip_ttl);
1000 #endif
1001 if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
1002 ipstat.ips_cantforward++;
1003 m_freem(m);
1004 return;
1006 HTONS(ip->ip_id);
1007 if (ip->ip_ttl <= IPTTLDEC) {
1008 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest);
1009 return;
1011 ip->ip_ttl -= IPTTLDEC;
1013 sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
1014 if ((rt = ipforward_rt.ro_rt) == 0 ||
1015 ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
1016 if (ipforward_rt.ro_rt) {
1017 RTFREE(ipforward_rt.ro_rt);
1018 ipforward_rt.ro_rt = 0;
1020 sin->sin_family = AF_INET;
1021 sin->sin_len = sizeof(*sin);
1022 sin->sin_addr = ip->ip_dst;
1024 rtalloc(&ipforward_rt);
1025 if (ipforward_rt.ro_rt == 0) {
1026 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest);
1027 return;
1029 rt = ipforward_rt.ro_rt;
1033 * Save at most 64 bytes of the packet in case
1034 * we need to generate an ICMP message to the src.
1036 mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64));
1038 #if USE_IF_MATRIX
1039 ip_ifmatrix[rt->rt_ifp->if_index +
1040 if_index * m->m_pkthdr.rcvif->if_index]++;
1041 #endif
1043 * If forwarding packet using same interface that it came in on,
1044 * perhaps should send a redirect to sender to shortcut a hop.
1045 * Only send redirect if source is sending directly to us,
1046 * and if packet was not source routed (or has any options).
1047 * Also, don't send redirect if forwarding using a default route
1048 * or a route modified by a redirect.
1050 #define satosin(sa) ((struct sockaddr_in *)(sa))
1051 if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1052 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1053 satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
1054 ipsendredirects && !srcrt) {
1055 struct in_ifaddr *ia;
1056 u_long src = ntohl(ip->ip_src.s_addr);
1057 u_long dst = ntohl(ip->ip_dst.s_addr);
1059 if ((ia = ifptoia(m->m_pkthdr.rcvif)) &&
1060 (src & ia->ia_subnetmask) == ia->ia_subnet) {
1061 if (rt->rt_flags & RTF_GATEWAY)
1062 dest = satosin(rt->rt_gateway)->sin_addr;
1063 else
1064 dest = ip->ip_dst;
1066 * If the destination is reached by a route to host,
1067 * is on a subnet of a local net, or is directly
1068 * on the attached net (!), use host redirect.
1069 * (We may be the correct first hop for other subnets.)
1071 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1072 type = ICMP_REDIRECT;
1073 if ((rt->rt_flags & RTF_HOST) ||
1074 (rt->rt_flags & RTF_GATEWAY) == 0)
1075 code = ICMP_REDIRECT_HOST;
1076 else if (RTA(rt)->ia_subnetmask != RTA(rt)->ia_netmask &&
1077 (dst & RTA(rt)->ia_netmask) == RTA(rt)->ia_net)
1078 code = ICMP_REDIRECT_HOST;
1079 else
1080 code = ICMP_REDIRECT_NET;
1081 #if DIAGNOSTIC
1082 if (ipprintfs)
1083 printf("redirect (%ld) to %lx\n", code, dest.s_addr);
1084 #endif
1088 error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING);
1089 if (error)
1090 ipstat.ips_cantforward++;
1091 else {
1092 ipstat.ips_forward++;
1093 if (type)
1094 ipstat.ips_redirectsent++;
1095 else {
1096 if (mcopy)
1097 m_freem(mcopy);
1098 return;
1101 if (mcopy == NULL)
1102 return;
1103 switch (error) {
1105 case 0: /* forwarded, but need redirect */
1106 /* type, code set above */
1107 break;
1109 case ENETUNREACH: /* shouldn't happen, checked above */
1110 case EHOSTUNREACH:
1111 case ENETDOWN:
1112 case EHOSTDOWN:
1113 default:
1114 type = ICMP_UNREACH;
1115 code = ICMP_UNREACH_HOST;
1116 break;
1118 case EMSGSIZE:
1119 type = ICMP_UNREACH;
1120 code = ICMP_UNREACH_NEEDFRAG;
1121 ipstat.ips_cantfrag++;
1122 break;
1124 case ENOBUFS:
1125 type = ICMP_SOURCEQUENCH;
1126 code = 0;
1127 break;
1129 icmp_error(mcopy, type, code, dest);