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