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 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
34 * $FreeBSD: src/sys/netinet/ip_input.c,v 1.50.2.25 2000/06/26 20:49:21 alfred Exp $
35 * $ANA: ip_input.c,v 1.5 1996/09/18 14:34:59 wollman Exp $
36 */
37
38 #define _IP_VHL
39
40 #include "opt_ipfw.h"
41
42 #include <stddef.h>
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/domain.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/errno.h>
52 #include <sys/time.h>
53 #include <sys/kernel.h>
54 #include <sys/syslog.h>
55 #include <sys/sysctl.h>
56
57 #include <net/if.h>
58 #include <net/if_dl.h>
59 #include <net/route.h>
60 #include <net/netisr.h>
61
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip.h>
66 #include <netinet/in_pcb.h>
67 #include <netinet/in_var.h>
68 #include <netinet/ip_var.h>
69 #include <netinet/ip_icmp.h>
70 #include <machine/in_cksum.h>
71
72 #include <sys/socketvar.h>
73
74 #ifdef IPFIREWALL
75 #include <netinet/ip_fw.h>
76 #endif
77
78 #ifdef DUMMYNET
79 #include <netinet/ip_dummynet.h>
80 #endif
81 int rsvp_on = 0;
82 static int ip_rsvp_on;
83 struct socket *ip_rsvpd;
84
85 static int ipforwarding = 0;
86 SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
87 &ipforwarding, 0, "");
88
89 static int ipsendredirects = 1; /* XXX */
90 SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
91 &ipsendredirects, 0, "");
92
93 int ip_defttl = IPDEFTTL;
94 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
95 &ip_defttl, 0, "");
96
97 static int ip_dosourceroute = 0;
98 SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
99 &ip_dosourceroute, 0, "");
100
101 static int ip_acceptsourceroute = 0;
102 SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
103 CTLFLAG_RW, &ip_acceptsourceroute, 0, "");
104 #ifdef DIAGNOSTIC
105 static int ipprintfs = 0;
106 #endif
107
108 extern struct domain inetdomain;
109 extern struct protosw inetsw[];
110 u_char ip_protox[IPPROTO_MAX];
111 static int ipqmaxlen = IFQ_MAXLEN;
112 struct in_ifaddr *in_ifaddr; /* first inet address */
113 struct ifqueue ipintrq;
114 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RD,
115 &ipintrq.ifq_maxlen, 0, "");
116 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
117 &ipintrq.ifq_drops, 0, "");
118
119 struct ipstat ipstat;
120 SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RD,
121 &ipstat , ipstat, "");
122
123 /* Packet reassembly stuff */
124 #define IPREASS_NHASH_LOG2 6
125 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
126 #define IPREASS_HMASK (IPREASS_NHASH - 1)
127 #define IPREASS_HASH(x,y) \
128 ((((x) & 0xF | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
129
130 static struct ipq ipq[IPREASS_NHASH];
131 static int nipq = 0; /* total # of reass queues */
132 static int maxnipq;
133
134 #ifdef IPCTL_DEFMTU
135 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
136 &ip_mtu, 0, "");
137 #endif
138
139 #if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1
140 #undef COMPAT_IPFW
141 #define COMPAT_IPFW 1
142 #else
143 #undef COMPAT_IPFW
144 #endif
145
146 #ifdef COMPAT_IPFW
147 /* Firewall hooks */
148 ip_fw_chk_t *ip_fw_chk_ptr = NULL ;
149 ip_fw_ctl_t *ip_fw_ctl_ptr = NULL ;
150
151 #ifdef DUMMYNET
152 ip_dn_ctl_t *ip_dn_ctl_ptr;
153 #endif
154
155 /* IP Network Address Translation (NAT) hooks */
156 /* there is no kernel nat in 2.2... */
157 ip_nat_t *ip_nat_ptr = NULL;
158 ip_nat_ctl_t *ip_nat_ctl_ptr = NULL;
159 #endif
160
161 /*
162 * We need to save the IP options in case a protocol wants to respond
163 * to an incoming packet over the same route if the packet got here
164 * using IP source routing. This allows connection establishment and
165 * maintenance when the remote end is on a network that is not known
166 * to us.
167 */
168 static int ip_nhops = 0;
169 static struct ip_srcrt {
170 struct in_addr dst; /* final destination */
171 char nop; /* one NOP to align */
172 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
173 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
174 } ip_srcrt;
175
176 #ifdef IPDIVERT
177 /*
178 * Shared variable between ip_input() and ip_reass() to communicate
179 * about which packets, once assembled from fragments, get diverted,
180 * and to which port.
181 */
182 static u_short frag_divert_port;
183 #endif
184
185 static void save_rte __P((u_char *, struct in_addr));
186 static void ip_deq __P((struct ipasfrag *));
187 static int ip_dooptions __P((struct mbuf *));
188 static void ip_enq __P((struct ipasfrag *, struct ipasfrag *));
189 static void ip_forward __P((struct mbuf *, int));
190 static void ip_freef __P((struct ipq *));
191 static struct ip *
192 ip_reass __P((struct ipasfrag *, struct ipq *, struct ipq *));
193 static struct in_ifaddr *
194 ip_rtaddr __P((struct in_addr));
195 static void ipintr __P((void));
196 /*
197 * IP initialization: fill in IP protocol switch table.
198 * All protocols not implemented in kernel go to raw IP protocol handler.
199 */
200 void
201 ip_init()
202 {
203 register struct protosw *pr;
204 register int i;
205
206 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
207 if (pr == 0)
208 panic("ip_init");
209 for (i = 0; i < IPPROTO_MAX; i++)
210 ip_protox[i] = pr - inetsw;
211 for (pr = inetdomain.dom_protosw;
212 pr < inetdomain.dom_protoswNPROTOSW; pr++)
213 if (pr->pr_domain->dom_family == PF_INET &&
214 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW)
215 ip_protox[pr->pr_protocol] = pr - inetsw;
216
217 for (i = 0; i < IPREASS_NHASH; i++)
218 ipq[i].next = ipq[i].prev = &ipq[i];
219
220 maxnipq = nmbclusters/4;
221
222 ip_id = time.tv_sec & 0xffff;
223 ipintrq.ifq_maxlen = ipqmaxlen;
224 #ifdef IPFIREWALL
225 ip_fw_init();
226 #endif
227 #ifdef IPNAT
228 ip_nat_init();
229 #endif
230 #ifdef DUMMYNET
231 ip_dn_init();
232 #endif
233
234 }
235
236 static struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
237 static struct route ipforward_rt;
238
239 /*
240 * Ip input routine. Checksum and byte swap header. If fragmented
241 * try to reassemble. Process options. Pass to next level.
242 */
243 void
244 ip_input(struct mbuf *m)
245 {
246 struct ip *ip;
247 struct ipq *fp;
248 struct in_ifaddr *ia;
249 int i, hlen;
250 u_short sum;
251 #ifndef IPDIVERT /* dummy variable for the firewall code to play with */
252 u_short ip_divert_cookie = 0 ;
253 #endif
254 #ifdef COMPAT_IPFW
255 struct ip_fw_chain *rule = NULL ;
256 #endif
257
258 #if defined(IPFIREWALL) && defined(DUMMYNET)
259 /*
260 * dummynet packet are prepended a vestigial mbuf with
261 * m_type = MT_DUMMYNET and m_data pointing to the matching
262 * rule.
263 */
264 if (m->m_type == MT_DUMMYNET) {
265 rule = (struct ip_fw_chain *)(m->m_data) ;
266 m = m->m_next ;
267 ip = mtod(m, struct ip *);
268 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
269 goto iphack ;
270 }
271 #endif
272 #ifdef DIAGNOSTIC
273 if ((m->m_flags & M_PKTHDR) == 0)
274 panic("ip_input no HDR");
275 #endif
276 /*
277 * If no IP addresses have been set yet but the interfaces
278 * are receiving, can't do anything with incoming packets yet.
279 */
280 if (in_ifaddr == NULL)
281 goto bad;
282 ipstat.ips_total++;
283
284 if (m->m_pkthdr.len < sizeof(struct ip))
285 goto tooshort;
286
287 #ifdef DIAGNOSTIC
288 if (m->m_len < sizeof(struct ip))
289 panic("ipintr mbuf too short");
290 #endif
291
292 if (m->m_len < sizeof (struct ip) &&
293 (m = m_pullup(m, sizeof (struct ip))) == 0) {
294 ipstat.ips_toosmall++;
295 return;
296 }
297 ip = mtod(m, struct ip *);
298
299 if (IP_VHL_V(ip->ip_vhl) != IPVERSION) {
300 ipstat.ips_badvers++;
301 goto bad;
302 }
303
304 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
305 if (hlen < sizeof(struct ip)) { /* minimum header length */
306 ipstat.ips_badhlen++;
307 goto bad;
308 }
309 if (hlen > m->m_len) {
310 if ((m = m_pullup(m, hlen)) == 0) {
311 ipstat.ips_badhlen++;
312 return;
313 }
314 ip = mtod(m, struct ip *);
315 }
316 if (hlen == sizeof(struct ip)) {
317 sum = in_cksum_hdr(ip);
318 } else {
319 sum = in_cksum(m, hlen);
320 }
321 if (sum) {
322 ipstat.ips_badsum++;
323 goto bad;
324 }
325
326 /*
327 * Convert fields to host representation.
328 */
329 NTOHS(ip->ip_len);
330 if (ip->ip_len < hlen) {
331 ipstat.ips_badlen++;
332 goto bad;
333 }
334 NTOHS(ip->ip_id);
335 NTOHS(ip->ip_off);
336
337 /*
338 * Check that the amount of data in the buffers
339 * is as at least much as the IP header would have us expect.
340 * Trim mbufs if longer than we expect.
341 * Drop packet if shorter than we expect.
342 */
343 if (m->m_pkthdr.len < ip->ip_len) {
344 tooshort:
345 ipstat.ips_tooshort++;
346 goto bad;
347 }
348 if (m->m_pkthdr.len > ip->ip_len) {
349 if (m->m_len == m->m_pkthdr.len) {
350 m->m_len = ip->ip_len;
351 m->m_pkthdr.len = ip->ip_len;
352 } else
353 m_adj(m, ip->ip_len - m->m_pkthdr.len);
354 }
355 /*
356 * IpHack's section.
357 * Right now when no processing on packet has done
358 * and it is still fresh out of network we do our black
359 * deals with it.
360 * - Firewall: deny/allow/divert
361 * - Xlate: translate packet's addr/port (NAT).
362 * - Pipe: pass pkt through dummynet.
363 * - Wrap: fake packet's addr/port <unimpl.>
364 * - Encapsulate: put it in another IP and send out. <unimp.>
365 */
366
367 iphack:
368 #ifdef COMPAT_IPFW
369 if (ip_fw_chk_ptr) {
370 i = (*ip_fw_chk_ptr)(&ip,hlen,NULL, &ip_divert_cookie, &m, &rule);
371 if (!m) /* packet discarded by firewall */
372 return;
373 if (i) { /* divert, dummynet, and other hacks... */
374 #ifdef DUMMYNET
375 if (i & 0x10000) {
376 /* send packet to the appropriate pipe */
377 dummynet_io(i&0xffff, DN_TO_IP_IN, m, NULL, NULL,0, rule);
378 return ;
379 }
380 #endif
381 #ifdef IPDIVERT
382 if (i <= 0xffff) { /* Divert packet */
383 frag_divert_port = i;
384 goto ours;
385 }
386 #endif
387 /* no match above, we have to just drop packet */
388 m_freem(m);
389 m = NULL;
390 return ;
391 }
392 }
393
394 if (ip_nat_ptr && !(*ip_nat_ptr)(&ip, &m, m->m_pkthdr.rcvif, IP_NAT_IN))
395 return;
396 #endif
397
398 /*
399 * Process options and, if not destined for us,
400 * ship it on. ip_dooptions returns 1 when an
401 * error was detected (causing an icmp message
402 * to be sent and the original packet to be freed).
403 */
404 ip_nhops = 0; /* for source routed packets */
405 if (hlen > sizeof (struct ip) && ip_dooptions(m))
406 return;
407
408 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
409 * matter if it is destined to another node, or whether it is
410 * a multicast one, RSVP wants it! and prevents it from being forwarded
411 * anywhere else. Also checks if the rsvp daemon is running before
412 * grabbing the packet.
413 */
414 if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
415 goto ours;
416
417 /*
418 * Check our list of addresses, to see if the packet is for us.
419 */
420 for (ia = in_ifaddr; ia; ia = ia->ia_next) {
421 #define satosin(sa) ((struct sockaddr_in *)(sa))
422
423 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr)
424 goto ours;
425 #ifdef BOOTP_COMPAT
426 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
427 goto ours;
428 #endif
429 if (ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) {
430 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
431 ip->ip_dst.s_addr)
432 goto ours;
433 if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr)
434 goto ours;
435 }
436 }
437 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
438 struct in_multi *inm;
439 if (ip_mrouter) {
440 /*
441 * If we are acting as a multicast router, all
442 * incoming multicast packets are passed to the
443 * kernel-level multicast forwarding function.
444 * The packet is returned (relatively) intact; if
445 * ip_mforward() returns a non-zero value, the packet
446 * must be discarded, else it may be accepted below.
447 *
448 * (The IP ident field is put in the same byte order
449 * as expected when ip_mforward() is called from
450 * ip_output().)
451 */
452 ip->ip_id = htons(ip->ip_id);
453 if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
454 ipstat.ips_cantforward++;
455 m_freem(m);
456 return;
457 }
458 ip->ip_id = ntohs(ip->ip_id);
459
460 /*
461 * The process-level routing demon needs to receive
462 * all multicast IGMP packets, whether or not this
463 * host belongs to their destination groups.
464 */
465 if (ip->ip_p == IPPROTO_IGMP)
466 goto ours;
467 ipstat.ips_forward++;
468 }
469 /*
470 * See if we belong to the destination multicast group on the
471 * arrival interface.
472 */
473 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
474 if (inm == NULL) {
475 ipstat.ips_cantforward++;
476 m_freem(m);
477 return;
478 }
479 goto ours;
480 }
481 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
482 goto ours;
483 if (ip->ip_dst.s_addr == INADDR_ANY)
484 goto ours;
485
486 /*
487 * Not for us; forward if possible and desirable.
488 */
489 if (ipforwarding == 0) {
490 ipstat.ips_cantforward++;
491 m_freem(m);
492 } else
493 ip_forward(m, 0);
494 return;
495
496 ours:
497
498 /*
499 * If offset or IP_MF are set, must reassemble.
500 * Otherwise, nothing need be done.
501 * (We could look in the reassembly queue to see
502 * if the packet was previously fragmented,
503 * but it's not worth the time; just let them time out.)
504 */
505 if (ip->ip_off &~ (IP_DF | IP_RF)) {
506 if (m->m_flags & M_EXT) { /* XXX */
507 if ((m = m_pullup(m, hlen)) == 0) {
508 ipstat.ips_toosmall++;
509 #ifdef IPDIVERT
510 frag_divert_port = 0;
511 ip_divert_cookie = 0;
512 #endif
513 return;
514 }
515 ip = mtod(m, struct ip *);
516 }
517 sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
518 /*
519 * Look for queue of fragments
520 * of this datagram.
521 */
522 for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next)
523 if (ip->ip_id == fp->ipq_id &&
524 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
525 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
526 ip->ip_p == fp->ipq_p)
527 goto found;
528
529 fp = 0;
530
531 /* check if there's a place for the new queue */
532 if (nipq > maxnipq) {
533 /*
534 * drop something from the tail of the current queue
535 * before proceeding further
536 */
537 if (ipq[sum].prev == &ipq[sum]) { /* gak */
538 for (i = 0; i < IPREASS_NHASH; i++) {
539 if (ipq[i].prev != &ipq[i]) {
540 ip_freef(ipq[i].prev);
541 break;
542 }
543 }
544 } else
545 ip_freef(ipq[sum].prev);
546 }
547 found:
548 /*
549 * Adjust ip_len to not reflect header,
550 * set ip_mff if more fragments are expected,
551 * convert offset of this to bytes.
552 */
553 ip->ip_len -= hlen;
554 ((struct ipasfrag *)ip)->ipf_mff &= ~1;
555 if (ip->ip_off & IP_MF)
556 ((struct ipasfrag *)ip)->ipf_mff |= 1;
557 ip->ip_off <<= 3;
558
559 /*
560 * If datagram marked as having more fragments
561 * or if this is not the first fragment,
562 * attempt reassembly; if it succeeds, proceed.
563 */
564 if (((struct ipasfrag *)ip)->ipf_mff & 1 || ip->ip_off) {
565 ipstat.ips_fragments++;
566 ip = ip_reass((struct ipasfrag *)ip, fp, &ipq[sum]);
567 if (ip == 0)
568 return;
569 ipstat.ips_reassembled++;
570 m = dtom(ip);
571 #ifdef IPDIVERT
572 if (frag_divert_port) {
573 ip->ip_len += hlen;
574 HTONS(ip->ip_len);
575 HTONS(ip->ip_off);
576 HTONS(ip->ip_id);
577 ip->ip_sum = 0;
578 ip->ip_sum = in_cksum_hdr(ip);
579 NTOHS(ip->ip_id);
580 NTOHS(ip->ip_off);
581 NTOHS(ip->ip_len);
582 ip->ip_len -= hlen;
583 }
584 #endif
585 } else
586 if (fp)
587 ip_freef(fp);
588 } else
589 ip->ip_len -= hlen;
590
591 #ifdef IPDIVERT
592 /*
593 * Divert reassembled packets to the divert protocol if required
594 * If divert port is null then cookie should be too,
595 * so we shouldn't need to clear them here. Assume ip_divert does so.
596 */
597 if (frag_divert_port) {
598 ipstat.ips_delivered++;
599 ip_divert_port = frag_divert_port;
600 frag_divert_port = 0;
601 (*inetsw[ip_protox[IPPROTO_DIVERT]].pr_input)(m, hlen);
602 return;
603 }
604
605 /* Don't let packets divert themselves */
606 if (ip->ip_p == IPPROTO_DIVERT) {
607 ipstat.ips_noproto++;
608 goto bad;
609 }
610 #endif
611
612 /*
613 * Switch out to protocol's input routine.
614 */
615 ipstat.ips_delivered++;
616 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
617 return;
618 bad:
619 m_freem(m);
620 }
621
622 /*
623 * IP software interrupt routine - to go away sometime soon
624 */
625 static void
626 ipintr(void)
627 {
628 int s;
629 struct mbuf *m;
630
631 while(1) {
632 s = splimp();
633 IF_DEQUEUE(&ipintrq, m);
634 splx(s);
635 if (m == 0)
636 return;
637 ip_input(m);
638 }
639 }
640
641 NETISR_SET(NETISR_IP, ipintr);
642
643 /*
644 * Take incoming datagram fragment and try to
645 * reassemble it into whole datagram. If a chain for
646 * reassembly of this datagram already exists, then it
647 * is given as fp; otherwise have to make a chain.
648 */
649 static struct ip *
650 ip_reass(ip, fp, where)
651 register struct ipasfrag *ip;
652 register struct ipq *fp;
653 struct ipq *where;
654 {
655 register struct mbuf *m = dtom(ip);
656 register struct ipasfrag *q;
657 struct mbuf *t;
658 int hlen = ip->ip_hl << 2;
659 int i, next;
660
661 /*
662 * Presence of header sizes in mbufs
663 * would confuse code below.
664 */
665 m->m_data += hlen;
666 m->m_len -= hlen;
667
668 /*
669 * If first fragment to arrive, create a reassembly queue.
670 */
671 if (fp == 0) {
672 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
673 goto dropfrag;
674 fp = mtod(t, struct ipq *);
675 insque(fp, where);
676 nipq++;
677 fp->ipq_ttl = IPFRAGTTL;
678 fp->ipq_p = ip->ip_p;
679 fp->ipq_id = ip->ip_id;
680 fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp;
681 fp->ipq_src = ((struct ip *)ip)->ip_src;
682 fp->ipq_dst = ((struct ip *)ip)->ip_dst;
683 #ifdef IPDIVERT
684 fp->ipq_divert = 0;
685 #ifdef IPFW_DIVERT_RESTART
686 fp->ipq_div_cookie = 0;
687 #endif /* IPFW_DIVERT_RESTART */
688 #endif
689 q = (struct ipasfrag *)fp;
690 goto insert;
691 }
692
693 /*
694 * Find a segment which begins after this one does.
695 */
696 for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next)
697 if (q->ip_off > ip->ip_off)
698 break;
699
700 /*
701 * If there is a preceding segment, it may provide some of
702 * our data already. If so, drop the data from the incoming
703 * segment. If it provides all of our data, drop us.
704 */
705 if (q->ipf_prev != (struct ipasfrag *)fp) {
706 i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off;
707 if (i > 0) {
708 if (i >= ip->ip_len)
709 goto dropfrag;
710 m_adj(dtom(ip), i);
711 ip->ip_off += i;
712 ip->ip_len -= i;
713 }
714 }
715
716 /*
717 * While we overlap succeeding segments trim them or,
718 * if they are completely covered, dequeue them.
719 */
720 while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) {
721 struct mbuf *m0;
722
723 i = (ip->ip_off + ip->ip_len) - q->ip_off;
724 if (i < q->ip_len) {
725 q->ip_len -= i;
726 q->ip_off += i;
727 m_adj(dtom(q), i);
728 break;
729 }
730 m0 = dtom(q);
731 q = q->ipf_next;
732 ip_deq(q->ipf_prev);
733 m_freem(m0);
734 }
735
736 insert:
737
738 #ifdef IPDIVERT
739 /*
740 * Any fragment diverting causes the whole packet to divert
741 */
742 if (frag_divert_port) {
743 fp->ipq_divert = frag_divert_port;
744 #ifdef IPFW_DIVERT_RESTART
745 fp->ipq_div_cookie = ip_divert_cookie;
746 #endif /* IPFW_DIVERT_RESTART */
747 }
748 frag_divert_port = 0;
749 ip_divert_cookie = 0;
750 #endif
751
752 /*
753 * Stick new segment in its place;
754 * check for complete reassembly.
755 */
756 ip_enq(ip, q->ipf_prev);
757 next = 0;
758 for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) {
759 if (q->ip_off != next)
760 return (0);
761 next += q->ip_len;
762 }
763 if (q->ipf_prev->ipf_mff & 1)
764 return (0);
765
766 /*
767 * Reassembly is complete. Make sure the packet is a sane size.
768 */
769 if (next + (IP_VHL_HL(((struct ip *)fp->ipq_next)->ip_vhl) << 2)
770 > IP_MAXPACKET) {
771 ipstat.ips_toolong++;
772 ip_freef(fp);
773 return (0);
774 }
775
776 /*
777 * Concatenate fragments.
778 */
779 q = fp->ipq_next;
780 m = dtom(q);
781 t = m->m_next;
782 m->m_next = 0;
783 m_cat(m, t);
784 q = q->ipf_next;
785 while (q != (struct ipasfrag *)fp) {
786 t = dtom(q);
787 q = q->ipf_next;
788 m_cat(m, t);
789 }
790
791 #ifdef IPDIVERT
792 /*
793 * Record divert port for packet, if any
794 */
795 frag_divert_port = fp->ipq_divert;
796 #ifdef IPFW_DIVERT_RESTART
797 ip_divert_cookie = fp->ipq_div_cookie;
798 #endif /* IPFW_DIVERT_RESTART */
799 #endif
800
801 /*
802 * Create header for new ip packet by
803 * modifying header of first packet;
804 * dequeue and discard fragment reassembly header.
805 * Make header visible.
806 */
807 ip = fp->ipq_next;
808 ip->ip_len = next;
809 ip->ipf_mff &= ~1;
810 ((struct ip *)ip)->ip_src = fp->ipq_src;
811 ((struct ip *)ip)->ip_dst = fp->ipq_dst;
812 remque(fp);
813 nipq--;
814 (void) m_free(dtom(fp));
815 m = dtom(ip);
816 m->m_len += (ip->ip_hl << 2);
817 m->m_data -= (ip->ip_hl << 2);
818 /* some debugging cruft by sklower, below, will go away soon */
819 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
820 register int plen = 0;
821 for (t = m; m; m = m->m_next)
822 plen += m->m_len;
823 t->m_pkthdr.len = plen;
824 }
825 return ((struct ip *)ip);
826
827 dropfrag:
828 #ifdef IPDIVERT
829 frag_divert_port = 0;
830 ip_divert_cookie = 0;
831 #endif
832 ipstat.ips_fragdropped++;
833 m_freem(m);
834 return (0);
835 }
836
837 /*
838 * Free a fragment reassembly header and all
839 * associated datagrams.
840 */
841 static void
842 ip_freef(fp)
843 struct ipq *fp;
844 {
845 register struct ipasfrag *q, *p;
846
847 for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) {
848 p = q->ipf_next;
849 ip_deq(q);
850 m_freem(dtom(q));
851 }
852 remque(fp);
853 (void) m_free(dtom(fp));
854 nipq--;
855 }
856
857 /*
858 * Put an ip fragment on a reassembly chain.
859 * Like insque, but pointers in middle of structure.
860 */
861 static void
862 ip_enq(p, prev)
863 register struct ipasfrag *p, *prev;
864 {
865
866 p->ipf_prev = prev;
867 p->ipf_next = prev->ipf_next;
868 prev->ipf_next->ipf_prev = p;
869 prev->ipf_next = p;
870 }
871
872 /*
873 * To ip_enq as remque is to insque.
874 */
875 static void
876 ip_deq(p)
877 register struct ipasfrag *p;
878 {
879
880 p->ipf_prev->ipf_next = p->ipf_next;
881 p->ipf_next->ipf_prev = p->ipf_prev;
882 }
883
884 /*
885 * IP timer processing;
886 * if a timer expires on a reassembly
887 * queue, discard it.
888 */
889 void
890 ip_slowtimo()
891 {
892 register struct ipq *fp;
893 int s = splnet();
894 int i;
895
896 for (i = 0; i < IPREASS_NHASH; i++) {
897 fp = ipq[i].next;
898 if (fp == 0)
899 continue;
900 while (fp != &ipq[i]) {
901 --fp->ipq_ttl;
902 fp = fp->next;
903 if (fp->prev->ipq_ttl == 0) {
904 ipstat.ips_fragtimeout++;
905 ip_freef(fp->prev);
906 }
907 }
908 }
909 splx(s);
910 }
911
912 /*
913 * Drain off all datagram fragments.
914 */
915 void
916 ip_drain()
917 {
918 int i;
919
920 for (i = 0; i < IPREASS_NHASH; i++) {
921 while (ipq[i].next != &ipq[i]) {
922 ipstat.ips_fragdropped++;
923 ip_freef(ipq[i].next);
924 }
925 }
926 in_rtqdrain();
927 }
928
929 /*
930 * Do option processing on a datagram,
931 * possibly discarding it if bad options are encountered,
932 * or forwarding it if source-routed.
933 * Returns 1 if packet has been forwarded/freed,
934 * 0 if the packet should be processed further.
935 */
936 static int
937 ip_dooptions(m)
938 struct mbuf *m;
939 {
940 register struct ip *ip = mtod(m, struct ip *);
941 register u_char *cp;
942 register struct ip_timestamp *ipt;
943 register struct in_ifaddr *ia;
944 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
945 struct in_addr *sin, dst;
946 n_time ntime;
947
948 dst = ip->ip_dst;
949 cp = (u_char *)(ip + 1);
950 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
951 for (; cnt > 0; cnt -= optlen, cp += optlen) {
952 opt = cp[IPOPT_OPTVAL];
953 if (opt == IPOPT_EOL)
954 break;
955 if (opt == IPOPT_NOP)
956 optlen = 1;
957 else {
958 if (cnt < IPOPT_OLEN + sizeof(*cp)) {
959 code = &cp[IPOPT_OLEN] - (u_char *)ip;
960 goto bad;
961 }
962 optlen = cp[IPOPT_OLEN];
963 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
964 code = &cp[IPOPT_OLEN] - (u_char *)ip;
965 goto bad;
966 }
967 }
968 switch (opt) {
969
970 default:
971 break;
972
973 /*
974 * Source routing with record.
975 * Find interface with current destination address.
976 * If none on this machine then drop if strictly routed,
977 * or do nothing if loosely routed.
978 * Record interface address and bring up next address
979 * component. If strictly routed make sure next
980 * address is on directly accessible net.
981 */
982 case IPOPT_LSRR:
983 case IPOPT_SSRR:
984 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
985 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
986 goto bad;
987 }
988 ipaddr.sin_addr = ip->ip_dst;
989 ia = (struct in_ifaddr *)
990 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
991 if (ia == 0) {
992 if (opt == IPOPT_SSRR) {
993 type = ICMP_UNREACH;
994 code = ICMP_UNREACH_SRCFAIL;
995 goto bad;
996 }
997 if (!ip_dosourceroute)
998 goto nosourcerouting;
999 /*
1000 * Loose routing, and not at next destination
1001 * yet; nothing to do except forward.
1002 */
1003 break;
1004 }
1005 off--; /* 0 origin */
1006 if (off > optlen - sizeof(struct in_addr)) {
1007 /*
1008 * End of source route. Should be for us.
1009 */
1010 if (!ip_acceptsourceroute)
1011 goto nosourcerouting;
1012 save_rte(cp, ip->ip_src);
1013 break;
1014 }
1015
1016 if (!ip_dosourceroute) {
1017 if (ipforwarding) {
1018 char buf[16]; /* aaa.bbb.ccc.ddd\0 */
1019 /*
1020 * Acting as a router, so generate ICMP
1021 */
1022 nosourcerouting:
1023 strcpy(buf, inet_ntoa(ip->ip_dst));
1024 log(LOG_WARNING,
1025 "attempted source route from %s to %s\n",
1026 inet_ntoa(ip->ip_src), buf);
1027 type = ICMP_UNREACH;
1028 code = ICMP_UNREACH_SRCFAIL;
1029 goto bad;
1030 } else {
1031 /*
1032 * Not acting as a router, so silently drop.
1033 */
1034 ipstat.ips_cantforward++;
1035 m_freem(m);
1036 return (1);
1037 }
1038 }
1039
1040 /*
1041 * locate outgoing interface
1042 */
1043 (void)memcpy(&ipaddr.sin_addr, cp + off,
1044 sizeof(ipaddr.sin_addr));
1045
1046 if (opt == IPOPT_SSRR) {
1047 #define INA struct in_ifaddr *
1048 #define SA struct sockaddr *
1049 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0)
1050 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
1051 } else
1052 ia = ip_rtaddr(ipaddr.sin_addr);
1053 if (ia == 0) {
1054 type = ICMP_UNREACH;
1055 code = ICMP_UNREACH_SRCFAIL;
1056 goto bad;
1057 }
1058 ip->ip_dst = ipaddr.sin_addr;
1059 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1060 sizeof(struct in_addr));
1061 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1062 /*
1063 * Let ip_intr's mcast routing check handle mcast pkts
1064 */
1065 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
1066 break;
1067
1068 case IPOPT_RR:
1069 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1070 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1071 goto bad;
1072 }
1073 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1074 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1075 goto bad;
1076 }
1077 /*
1078 * If no space remains, ignore.
1079 */
1080 off--; /* 0 origin */
1081 if (off > optlen - sizeof(struct in_addr))
1082 break;
1083 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
1084 sizeof(ipaddr.sin_addr));
1085 /*
1086 * locate outgoing interface; if we're the destination,
1087 * use the incoming interface (should be same).
1088 */
1089 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 &&
1090 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) {
1091 type = ICMP_UNREACH;
1092 code = ICMP_UNREACH_HOST;
1093 goto bad;
1094 }
1095 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1096 sizeof(struct in_addr));
1097 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1098 break;
1099
1100 case IPOPT_TS:
1101 code = cp - (u_char *)ip;
1102 ipt = (struct ip_timestamp *)cp;
1103 if (ipt->ipt_len < 5)
1104 goto bad;
1105 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) {
1106 if (++ipt->ipt_oflw == 0)
1107 goto bad;
1108 break;
1109 }
1110 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1);
1111 switch (ipt->ipt_flg) {
1112
1113 case IPOPT_TS_TSONLY:
1114 break;
1115
1116 case IPOPT_TS_TSANDADDR:
1117 if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1118 sizeof(struct in_addr) > ipt->ipt_len)
1119 goto bad;
1120 ipaddr.sin_addr = dst;
1121 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
1122 m->m_pkthdr.rcvif);
1123 if (ia == 0)
1124 continue;
1125 (void)memcpy(sin, &IA_SIN(ia)->sin_addr,
1126 sizeof(struct in_addr));
1127 ipt->ipt_ptr += sizeof(struct in_addr);
1128 break;
1129
1130 case IPOPT_TS_PRESPEC:
1131 if (ipt->ipt_ptr - 1 + sizeof(n_time) +
1132 sizeof(struct in_addr) > ipt->ipt_len)
1133 goto bad;
1134 (void)memcpy(&ipaddr.sin_addr, sin,
1135 sizeof(struct in_addr));
1136 if (ifa_ifwithaddr((SA)&ipaddr) == 0)
1137 continue;
1138 ipt->ipt_ptr += sizeof(struct in_addr);
1139 break;
1140
1141 default:
1142 goto bad;
1143 }
1144 ntime = iptime();
1145 (void)memcpy(cp + ipt->ipt_ptr - 1, &ntime,
1146 sizeof(n_time));
1147 ipt->ipt_ptr += sizeof(n_time);
1148 }
1149 }
1150 if (forward && ipforwarding) {
1151 ip_forward(m, 1);
1152 return (1);
1153 }
1154 return (0);
1155 bad:
1156 ip->ip_len -= IP_VHL_HL(ip->ip_vhl) << 2; /* XXX icmp_error adds in hdr length */
1157 icmp_error(m, type, code, 0, 0);
1158 ipstat.ips_badoptions++;
1159 return (1);
1160 }
1161
1162 /*
1163 * Given address of next destination (final or next hop),
1164 * return internet address info of interface to be used to get there.
1165 */
1166 static struct in_ifaddr *
1167 ip_rtaddr(dst)
1168 struct in_addr dst;
1169 {
1170 register struct sockaddr_in *sin;
1171
1172 sin = (struct sockaddr_in *) &ipforward_rt.ro_dst;
1173
1174 if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) {
1175 if (ipforward_rt.ro_rt) {
1176 RTFREE(ipforward_rt.ro_rt);
1177 ipforward_rt.ro_rt = 0;
1178 }
1179 sin->sin_family = AF_INET;
1180 sin->sin_len = sizeof(*sin);
1181 sin->sin_addr = dst;
1182
1183 rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1184 }
1185 if (ipforward_rt.ro_rt == 0)
1186 return ((struct in_ifaddr *)0);
1187 return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa);
1188 }
1189
1190 /*
1191 * Save incoming source route for use in replies,
1192 * to be picked up later by ip_srcroute if the receiver is interested.
1193 */
1194 void
1195 save_rte(option, dst)
1196 u_char *option;
1197 struct in_addr dst;
1198 {
1199 unsigned olen;
1200
1201 olen = option[IPOPT_OLEN];
1202 #ifdef DIAGNOSTIC
1203 if (ipprintfs)
1204 printf("save_rte: olen %d\n", olen);
1205 #endif
1206 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst)))
1207 return;
1208 bcopy(option, ip_srcrt.srcopt, olen);
1209 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1210 ip_srcrt.dst = dst;
1211 }
1212
1213 /*
1214 * Retrieve incoming source route for use in replies,
1215 * in the same form used by setsockopt.
1216 * The first hop is placed before the options, will be removed later.
1217 */
1218 struct mbuf *
1219 ip_srcroute()
1220 {
1221 register struct in_addr *p, *q;
1222 register struct mbuf *m;
1223
1224 if (ip_nhops == 0)
1225 return ((struct mbuf *)0);
1226 m = m_get(M_DONTWAIT, MT_SOOPTS);
1227 if (m == 0)
1228 return ((struct mbuf *)0);
1229
1230 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1231
1232 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1233 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) +
1234 OPTSIZ;
1235 #ifdef DIAGNOSTIC
1236 if (ipprintfs)
1237 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len);
1238 #endif
1239
1240 /*
1241 * First save first hop for return route
1242 */
1243 p = &ip_srcrt.route[ip_nhops - 1];
1244 *(mtod(m, struct in_addr *)) = *p--;
1245 #ifdef DIAGNOSTIC
1246 if (ipprintfs)
1247 printf(" hops %lx", ntohl(mtod(m, struct in_addr *)->s_addr));
1248 #endif
1249
1250 /*
1251 * Copy option fields and padding (nop) to mbuf.
1252 */
1253 ip_srcrt.nop = IPOPT_NOP;
1254 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1255 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
1256 &ip_srcrt.nop, OPTSIZ);
1257 q = (struct in_addr *)(mtod(m, caddr_t) +
1258 sizeof(struct in_addr) + OPTSIZ);
1259 #undef OPTSIZ
1260 /*
1261 * Record return path as an IP source route,
1262 * reversing the path (pointers are now aligned).
1263 */
1264 while (p >= ip_srcrt.route) {
1265 #ifdef DIAGNOSTIC
1266 if (ipprintfs)
1267 printf(" %lx", ntohl(q->s_addr));
1268 #endif
1269 *q++ = *p--;
1270 }
1271 /*
1272 * Last hop goes to final destination.
1273 */
1274 *q = ip_srcrt.dst;
1275 #ifdef DIAGNOSTIC
1276 if (ipprintfs)
1277 printf(" %lx\n", ntohl(q->s_addr));
1278 #endif
1279 return (m);
1280 }
1281
1282 /*
1283 * Strip out IP options, at higher
1284 * level protocol in the kernel.
1285 * Second argument is buffer to which options
1286 * will be moved, and return value is their length.
1287 * XXX should be deleted; last arg currently ignored.
1288 */
1289 void
1290 ip_stripoptions(m, mopt)
1291 register struct mbuf *m;
1292 struct mbuf *mopt;
1293 {
1294 register int i;
1295 struct ip *ip = mtod(m, struct ip *);
1296 register caddr_t opts;
1297 int olen;
1298
1299 olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1300 opts = (caddr_t)(ip + 1);
1301 i = m->m_len - (sizeof (struct ip) + olen);
1302 bcopy(opts + olen, opts, (unsigned)i);
1303 m->m_len -= olen;
1304 if (m->m_flags & M_PKTHDR)
1305 m->m_pkthdr.len -= olen;
1306 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2);
1307 }
1308
1309 u_char inetctlerrmap[PRC_NCMDS] = {
1310 0, 0, 0, 0,
1311 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1312 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1313 EMSGSIZE, EHOSTUNREACH, 0, 0,
1314 0, 0, 0, 0,
1315 ENOPROTOOPT
1316 };
1317
1318 /*
1319 * Forward a packet. If some error occurs return the sender
1320 * an icmp packet. Note we can't always generate a meaningful
1321 * icmp message because icmp doesn't have a large enough repertoire
1322 * of codes and types.
1323 *
1324 * If not forwarding, just drop the packet. This could be confusing
1325 * if ipforwarding was zero but some routing protocol was advancing
1326 * us as a gateway to somewhere. However, we must let the routing
1327 * protocol deal with that.
1328 *
1329 * The srcrt parameter indicates whether the packet is being forwarded
1330 * via a source route.
1331 */
1332 static void
1333 ip_forward(m, srcrt)
1334 struct mbuf *m;
1335 int srcrt;
1336 {
1337 register struct ip *ip = mtod(m, struct ip *);
1338 register struct sockaddr_in *sin;
1339 register struct rtentry *rt;
1340 int error, type = 0, code = 0;
1341 struct mbuf *mcopy;
1342 n_long dest;
1343 struct ifnet *destifp;
1344
1345 dest = 0;
1346 #ifdef DIAGNOSTIC
1347 if (ipprintfs)
1348 printf("forward: src %lx dst %lx ttl %x\n",
1349 ip->ip_src.s_addr, ip->ip_dst.s_addr, ip->ip_ttl);
1350 #endif
1351
1352
1353 if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) {
1354 ipstat.ips_cantforward++;
1355 m_freem(m);
1356 return;
1357 }
1358 HTONS(ip->ip_id);
1359 if (ip->ip_ttl <= IPTTLDEC) {
1360 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest, 0);
1361 return;
1362 }
1363 ip->ip_ttl -= IPTTLDEC;
1364
1365 sin = (struct sockaddr_in *)&ipforward_rt.ro_dst;
1366 if ((rt = ipforward_rt.ro_rt) == 0 ||
1367 ip->ip_dst.s_addr != sin->sin_addr.s_addr) {
1368 if (ipforward_rt.ro_rt) {
1369 RTFREE(ipforward_rt.ro_rt);
1370 ipforward_rt.ro_rt = 0;
1371 }
1372 sin->sin_family = AF_INET;
1373 sin->sin_len = sizeof(*sin);
1374 sin->sin_addr = ip->ip_dst;
1375
1376 rtalloc_ign(&ipforward_rt, RTF_PRCLONING);
1377 if (ipforward_rt.ro_rt == 0) {
1378 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0);
1379 return;
1380 }
1381 rt = ipforward_rt.ro_rt;
1382 }
1383
1384 /*
1385 * Save at most 64 bytes of the packet in case
1386 * we need to generate an ICMP message to the src.
1387 */
1388 mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64));
1389
1390 /*
1391 * If forwarding packet using same interface that it came in on,
1392 * perhaps should send a redirect to sender to shortcut a hop.
1393 * Only send redirect if source is sending directly to us,
1394 * and if packet was not source routed (or has any options).
1395 * Also, don't send redirect if forwarding using a default route
1396 * or a route modified by a redirect.
1397 */
1398 #define satosin(sa) ((struct sockaddr_in *)(sa))
1399 if (rt->rt_ifp == m->m_pkthdr.rcvif &&
1400 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1401 satosin(rt_key(rt))->sin_addr.s_addr != 0 &&
1402 ipsendredirects && !srcrt) {
1403 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1404 u_long src = ntohl(ip->ip_src.s_addr);
1405
1406 if (RTA(rt) &&
1407 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1408 if (rt->rt_flags & RTF_GATEWAY)
1409 dest = satosin(rt->rt_gateway)->sin_addr.s_addr;
1410 else
1411 dest = ip->ip_dst.s_addr;
1412 /* Router requirements says to only send host redirects */
1413 type = ICMP_REDIRECT;
1414 code = ICMP_REDIRECT_HOST;
1415 #ifdef DIAGNOSTIC
1416 if (ipprintfs)
1417 printf("redirect (%d) to %lx\n", code, (u_long)dest);
1418 #endif
1419 }
1420 }
1421
1422 error = ip_output(m, (struct mbuf *)0, &ipforward_rt,
1423 IP_FORWARDING, 0);
1424 if (error)
1425 ipstat.ips_cantforward++;
1426 else {
1427 ipstat.ips_forward++;
1428 if (type)
1429 ipstat.ips_redirectsent++;
1430 else {
1431 if (mcopy)
1432 m_freem(mcopy);
1433 return;
1434 }
1435 }
1436 if (mcopy == NULL)
1437 return;
1438 destifp = NULL;
1439
1440 switch (error) {
1441
1442 case 0: /* forwarded, but need redirect */
1443 /* type, code set above */
1444 break;
1445
1446 case ENETUNREACH: /* shouldn't happen, checked above */
1447 case EHOSTUNREACH:
1448 case ENETDOWN:
1449 case EHOSTDOWN:
1450 default:
1451 type = ICMP_UNREACH;
1452 code = ICMP_UNREACH_HOST;
1453 break;
1454
1455 case EMSGSIZE:
1456 type = ICMP_UNREACH;
1457 code = ICMP_UNREACH_NEEDFRAG;
1458 if (ipforward_rt.ro_rt)
1459 destifp = ipforward_rt.ro_rt->rt_ifp;
1460 ipstat.ips_cantfrag++;
1461 break;
1462
1463 case ENOBUFS:
1464 type = ICMP_SOURCEQUENCH;
1465 code = 0;
1466 break;
1467 }
1468 icmp_error(mcopy, type, code, dest, destifp);
1469 }
1470
1471 void
1472 ip_savecontrol(inp, mp, ip, m)
1473 register struct inpcb *inp;
1474 register struct mbuf **mp;
1475 register struct ip *ip;
1476 register struct mbuf *m;
1477 {
1478 if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1479 struct timeval tv;
1480
1481 microtime(&tv);
1482 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1483 SCM_TIMESTAMP, SOL_SOCKET);
1484 if (*mp)
1485 mp = &(*mp)->m_next;
1486 }
1487 if (inp->inp_flags & INP_RECVDSTADDR) {
1488 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1489 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1490 if (*mp)
1491 mp = &(*mp)->m_next;
1492 }
1493 #ifdef notyet
1494 /* XXX
1495 * Moving these out of udp_input() made them even more broken
1496 * than they already were.
1497 */
1498 /* options were tossed already */
1499 if (inp->inp_flags & INP_RECVOPTS) {
1500 *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1501 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1502 if (*mp)
1503 mp = &(*mp)->m_next;
1504 }
1505 /* ip_srcroute doesn't do what we want here, need to fix */
1506 if (inp->inp_flags & INP_RECVRETOPTS) {
1507 *mp = sbcreatecontrol((caddr_t) ip_srcroute(),
1508 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1509 if (*mp)
1510 mp = &(*mp)->m_next;
1511 }
1512 #endif
1513 if (inp->inp_flags & INP_RECVIF) {
1514 struct sockaddr_dl sdl;
1515
1516 sdl.sdl_len = offsetof(struct sockaddr_dl, sdl_data[0]);
1517 sdl.sdl_family = AF_LINK;
1518 sdl.sdl_index = m->m_pkthdr.rcvif ?
1519 m->m_pkthdr.rcvif->if_index : 0;
1520 sdl.sdl_nlen = sdl.sdl_alen = sdl.sdl_slen = 0;
1521 *mp = sbcreatecontrol((caddr_t) &sdl, sdl.sdl_len,
1522 IP_RECVIF, IPPROTO_IP);
1523 if (*mp)
1524 mp = &(*mp)->m_next;
1525 }
1526 }
1527
1528 int
1529 ip_rsvp_init(struct socket *so)
1530 {
1531 if (so->so_type != SOCK_RAW ||
1532 so->so_proto->pr_protocol != IPPROTO_RSVP)
1533 return EOPNOTSUPP;
1534
1535 if (ip_rsvpd != NULL)
1536 return EADDRINUSE;
1537
1538 ip_rsvpd = so;
1539 /*
1540 * This may seem silly, but we need to be sure we don't over-increment
1541 * the RSVP counter, in case something slips up.
1542 */
1543 if (!ip_rsvp_on) {
1544 ip_rsvp_on = 1;
1545 rsvp_on++;
1546 }
1547
1548 return 0;
1549 }
1550
1551 int
1552 ip_rsvp_done(void)
1553 {
1554 ip_rsvpd = NULL;
1555 /*
1556 * This may seem silly, but we need to be sure we don't over-decrement
1557 * the RSVP counter, in case something slips up.
1558 */
1559 if (ip_rsvp_on) {
1560 ip_rsvp_on = 0;
1561 rsvp_on--;
1562 }
1563 return 0;
1564 }
Cache object: 17023a0173ca70ed8970ecf709de5d2a
|