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