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