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