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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
30 * $FreeBSD: releng/5.3/sys/netinet/ip_input.c 136588 2004-10-16 08:43:07Z cvs2svn $
31 */
32
33 #include "opt_bootp.h"
34 #include "opt_ipfw.h"
35 #include "opt_ipstealth.h"
36 #include "opt_ipsec.h"
37 #include "opt_mac.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/mac.h>
42 #include <sys/mbuf.h>
43 #include <sys/malloc.h>
44 #include <sys/domain.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/time.h>
48 #include <sys/kernel.h>
49 #include <sys/syslog.h>
50 #include <sys/sysctl.h>
51
52 #include <net/pfil.h>
53 #include <net/if.h>
54 #include <net/if_types.h>
55 #include <net/if_var.h>
56 #include <net/if_dl.h>
57 #include <net/route.h>
58 #include <net/netisr.h>
59
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/in_var.h>
63 #include <netinet/ip.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/ip_var.h>
66 #include <netinet/ip_icmp.h>
67 #include <machine/in_cksum.h>
68
69 #include <sys/socketvar.h>
70
71 /* XXX: Temporary until ipfw_ether and ipfw_bridge are converted. */
72 #include <netinet/ip_fw.h>
73 #include <netinet/ip_dummynet.h>
74
75 #ifdef IPSEC
76 #include <netinet6/ipsec.h>
77 #include <netkey/key.h>
78 #endif
79
80 #ifdef FAST_IPSEC
81 #include <netipsec/ipsec.h>
82 #include <netipsec/key.h>
83 #endif
84
85 int rsvp_on = 0;
86
87 int ipforwarding = 0;
88 SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW,
89 &ipforwarding, 0, "Enable IP forwarding between interfaces");
90
91 static int ipsendredirects = 1; /* XXX */
92 SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW,
93 &ipsendredirects, 0, "Enable sending IP redirects");
94
95 int ip_defttl = IPDEFTTL;
96 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW,
97 &ip_defttl, 0, "Maximum TTL on IP packets");
98
99 static int ip_dosourceroute = 0;
100 SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW,
101 &ip_dosourceroute, 0, "Enable forwarding source routed IP packets");
102
103 static int ip_acceptsourceroute = 0;
104 SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute,
105 CTLFLAG_RW, &ip_acceptsourceroute, 0,
106 "Enable accepting source routed IP packets");
107
108 int ip_doopts = 1; /* 0 = ignore, 1 = process, 2 = reject */
109 SYSCTL_INT(_net_inet_ip, OID_AUTO, process_options, CTLFLAG_RW,
110 &ip_doopts, 0, "Enable IP options processing ([LS]SRR, RR, TS)");
111
112 static int ip_keepfaith = 0;
113 SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW,
114 &ip_keepfaith, 0,
115 "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
116
117 static int nipq = 0; /* total # of reass queues */
118 static int maxnipq;
119 SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragpackets, CTLFLAG_RW,
120 &maxnipq, 0,
121 "Maximum number of IPv4 fragment reassembly queue entries");
122
123 static int maxfragsperpacket;
124 SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW,
125 &maxfragsperpacket, 0,
126 "Maximum number of IPv4 fragments allowed per packet");
127
128 static int ip_sendsourcequench = 0;
129 SYSCTL_INT(_net_inet_ip, OID_AUTO, sendsourcequench, CTLFLAG_RW,
130 &ip_sendsourcequench, 0,
131 "Enable the transmission of source quench packets");
132
133 int ip_do_randomid = 0;
134 SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW,
135 &ip_do_randomid, 0,
136 "Assign random ip_id values");
137
138 /*
139 * XXX - Setting ip_checkinterface mostly implements the receive side of
140 * the Strong ES model described in RFC 1122, but since the routing table
141 * and transmit implementation do not implement the Strong ES model,
142 * setting this to 1 results in an odd hybrid.
143 *
144 * XXX - ip_checkinterface currently must be disabled if you use ipnat
145 * to translate the destination address to another local interface.
146 *
147 * XXX - ip_checkinterface must be disabled if you add IP aliases
148 * to the loopback interface instead of the interface where the
149 * packets for those addresses are received.
150 */
151 static int ip_checkinterface = 0;
152 SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW,
153 &ip_checkinterface, 0, "Verify packet arrives on correct interface");
154
155 #ifdef DIAGNOSTIC
156 static int ipprintfs = 0;
157 #endif
158
159 struct pfil_head inet_pfil_hook;
160
161 static struct ifqueue ipintrq;
162 static int ipqmaxlen = IFQ_MAXLEN;
163
164 extern struct domain inetdomain;
165 extern struct protosw inetsw[];
166 u_char ip_protox[IPPROTO_MAX];
167 struct in_ifaddrhead in_ifaddrhead; /* first inet address */
168 struct in_ifaddrhashhead *in_ifaddrhashtbl; /* inet addr hash table */
169 u_long in_ifaddrhmask; /* mask for hash table */
170
171 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW,
172 &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue");
173 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD,
174 &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue");
175
176 struct ipstat ipstat;
177 SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RW,
178 &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)");
179
180 /* Packet reassembly stuff */
181 #define IPREASS_NHASH_LOG2 6
182 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
183 #define IPREASS_HMASK (IPREASS_NHASH - 1)
184 #define IPREASS_HASH(x,y) \
185 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
186
187 static TAILQ_HEAD(ipqhead, ipq) ipq[IPREASS_NHASH];
188 struct mtx ipqlock;
189
190 #define IPQ_LOCK() mtx_lock(&ipqlock)
191 #define IPQ_UNLOCK() mtx_unlock(&ipqlock)
192 #define IPQ_LOCK_INIT() mtx_init(&ipqlock, "ipqlock", NULL, MTX_DEF)
193 #define IPQ_LOCK_ASSERT() mtx_assert(&ipqlock, MA_OWNED)
194
195 #ifdef IPCTL_DEFMTU
196 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW,
197 &ip_mtu, 0, "Default MTU");
198 #endif
199
200 #ifdef IPSTEALTH
201 int ipstealth = 0;
202 SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW,
203 &ipstealth, 0, "");
204 #endif
205
206 /*
207 * ipfw_ether and ipfw_bridge hooks.
208 * XXX: Temporary until those are converted to pfil_hooks as well.
209 */
210 ip_fw_chk_t *ip_fw_chk_ptr = NULL;
211 ip_dn_io_t *ip_dn_io_ptr = NULL;
212 int fw_enable = 1;
213 int fw_one_pass = 1;
214
215 /*
216 * XXX this is ugly. IP options source routing magic.
217 */
218 struct ipoptrt {
219 struct in_addr dst; /* final destination */
220 char nop; /* one NOP to align */
221 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */
222 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)];
223 };
224
225 struct ipopt_tag {
226 struct m_tag tag;
227 int ip_nhops;
228 struct ipoptrt ip_srcrt;
229 };
230
231 static void save_rte(struct mbuf *, u_char *, struct in_addr);
232 static int ip_dooptions(struct mbuf *m, int);
233 static void ip_forward(struct mbuf *m, int srcrt);
234 static void ip_freef(struct ipqhead *, struct ipq *);
235
236 /*
237 * IP initialization: fill in IP protocol switch table.
238 * All protocols not implemented in kernel go to raw IP protocol handler.
239 */
240 void
241 ip_init()
242 {
243 register struct protosw *pr;
244 register int i;
245
246 TAILQ_INIT(&in_ifaddrhead);
247 in_ifaddrhashtbl = hashinit(INADDR_NHASH, M_IFADDR, &in_ifaddrhmask);
248 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW);
249 if (pr == 0)
250 panic("ip_init: PF_INET not found");
251
252 /* Initialize the entire ip_protox[] array to IPPROTO_RAW. */
253 for (i = 0; i < IPPROTO_MAX; i++)
254 ip_protox[i] = pr - inetsw;
255 /*
256 * Cycle through IP protocols and put them into the appropriate place
257 * in ip_protox[].
258 */
259 for (pr = inetdomain.dom_protosw;
260 pr < inetdomain.dom_protoswNPROTOSW; pr++)
261 if (pr->pr_domain->dom_family == PF_INET &&
262 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) {
263 /* Be careful to only index valid IP protocols. */
264 if (pr->pr_protocol && pr->pr_protocol < IPPROTO_MAX)
265 ip_protox[pr->pr_protocol] = pr - inetsw;
266 }
267
268 /* Initialize packet filter hooks. */
269 inet_pfil_hook.ph_type = PFIL_TYPE_AF;
270 inet_pfil_hook.ph_af = AF_INET;
271 if ((i = pfil_head_register(&inet_pfil_hook)) != 0)
272 printf("%s: WARNING: unable to register pfil hook, "
273 "error %d\n", __func__, i);
274
275 /* Initialize IP reassembly queue. */
276 IPQ_LOCK_INIT();
277 for (i = 0; i < IPREASS_NHASH; i++)
278 TAILQ_INIT(&ipq[i]);
279 maxnipq = nmbclusters / 32;
280 maxfragsperpacket = 16;
281
282 /* Initialize various other remaining things. */
283 ip_id = time_second & 0xffff;
284 ipintrq.ifq_maxlen = ipqmaxlen;
285 mtx_init(&ipintrq.ifq_mtx, "ip_inq", NULL, MTX_DEF);
286 netisr_register(NETISR_IP, ip_input, &ipintrq, NETISR_MPSAFE);
287 }
288
289 /*
290 * Ip input routine. Checksum and byte swap header. If fragmented
291 * try to reassemble. Process options. Pass to next level.
292 */
293 void
294 ip_input(struct mbuf *m)
295 {
296 struct ip *ip = NULL;
297 struct in_ifaddr *ia = NULL;
298 struct ifaddr *ifa;
299 int checkif, hlen = 0;
300 u_short sum;
301 int dchg = 0; /* dest changed after fw */
302 struct in_addr odst; /* original dst address */
303 #ifdef FAST_IPSEC
304 struct m_tag *mtag;
305 struct tdb_ident *tdbi;
306 struct secpolicy *sp;
307 int s, error;
308 #endif /* FAST_IPSEC */
309
310 M_ASSERTPKTHDR(m);
311
312 if (m->m_flags & M_FASTFWD_OURS) {
313 /*
314 * ip_fastforward firewall changed dest to local.
315 * We expect ip_len and ip_off in host byte order.
316 */
317 m->m_flags &= ~M_FASTFWD_OURS; /* for reflected mbufs */
318 /* Set up some basic stuff */
319 ip = mtod(m, struct ip *);
320 hlen = ip->ip_hl << 2;
321 goto ours;
322 }
323
324 ipstat.ips_total++;
325
326 if (m->m_pkthdr.len < sizeof(struct ip))
327 goto tooshort;
328
329 if (m->m_len < sizeof (struct ip) &&
330 (m = m_pullup(m, sizeof (struct ip))) == NULL) {
331 ipstat.ips_toosmall++;
332 return;
333 }
334 ip = mtod(m, struct ip *);
335
336 if (ip->ip_v != IPVERSION) {
337 ipstat.ips_badvers++;
338 goto bad;
339 }
340
341 hlen = ip->ip_hl << 2;
342 if (hlen < sizeof(struct ip)) { /* minimum header length */
343 ipstat.ips_badhlen++;
344 goto bad;
345 }
346 if (hlen > m->m_len) {
347 if ((m = m_pullup(m, hlen)) == NULL) {
348 ipstat.ips_badhlen++;
349 return;
350 }
351 ip = mtod(m, struct ip *);
352 }
353
354 /* 127/8 must not appear on wire - RFC1122 */
355 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
356 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
357 if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) {
358 ipstat.ips_badaddr++;
359 goto bad;
360 }
361 }
362
363 if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) {
364 sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
365 } else {
366 if (hlen == sizeof(struct ip)) {
367 sum = in_cksum_hdr(ip);
368 } else {
369 sum = in_cksum(m, hlen);
370 }
371 }
372 if (sum) {
373 ipstat.ips_badsum++;
374 goto bad;
375 }
376
377 #ifdef ALTQ
378 if (altq_input != NULL && (*altq_input)(m, AF_INET) == 0)
379 /* packet is dropped by traffic conditioner */
380 return;
381 #endif
382
383 /*
384 * Convert fields to host representation.
385 */
386 ip->ip_len = ntohs(ip->ip_len);
387 if (ip->ip_len < hlen) {
388 ipstat.ips_badlen++;
389 goto bad;
390 }
391 ip->ip_off = ntohs(ip->ip_off);
392
393 /*
394 * Check that the amount of data in the buffers
395 * is as at least much as the IP header would have us expect.
396 * Trim mbufs if longer than we expect.
397 * Drop packet if shorter than we expect.
398 */
399 if (m->m_pkthdr.len < ip->ip_len) {
400 tooshort:
401 ipstat.ips_tooshort++;
402 goto bad;
403 }
404 if (m->m_pkthdr.len > ip->ip_len) {
405 if (m->m_len == m->m_pkthdr.len) {
406 m->m_len = ip->ip_len;
407 m->m_pkthdr.len = ip->ip_len;
408 } else
409 m_adj(m, ip->ip_len - m->m_pkthdr.len);
410 }
411 #if defined(IPSEC) && !defined(IPSEC_FILTERGIF)
412 /*
413 * Bypass packet filtering for packets from a tunnel (gif).
414 */
415 if (ipsec_getnhist(m))
416 goto passin;
417 #endif
418 #if defined(FAST_IPSEC) && !defined(IPSEC_FILTERGIF)
419 /*
420 * Bypass packet filtering for packets from a tunnel (gif).
421 */
422 if (m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL)
423 goto passin;
424 #endif
425
426 /*
427 * Run through list of hooks for input packets.
428 *
429 * NB: Beware of the destination address changing (e.g.
430 * by NAT rewriting). When this happens, tell
431 * ip_forward to do the right thing.
432 */
433
434 /* Jump over all PFIL processing if hooks are not active. */
435 if (inet_pfil_hook.ph_busy_count == -1)
436 goto passin;
437
438 odst = ip->ip_dst;
439 if (pfil_run_hooks(&inet_pfil_hook, &m, m->m_pkthdr.rcvif,
440 PFIL_IN, NULL) != 0)
441 return;
442 if (m == NULL) /* consumed by filter */
443 return;
444
445 ip = mtod(m, struct ip *);
446 dchg = (odst.s_addr != ip->ip_dst.s_addr);
447
448 #ifdef IPFIREWALL_FORWARD
449 if (m->m_flags & M_FASTFWD_OURS) {
450 m->m_flags &= ~M_FASTFWD_OURS;
451 goto ours;
452 }
453 dchg = (m_tag_find(m, PACKET_TAG_IPFORWARD, NULL) != NULL);
454 #endif /* IPFIREWALL_FORWARD */
455
456 passin:
457 /*
458 * Process options and, if not destined for us,
459 * ship it on. ip_dooptions returns 1 when an
460 * error was detected (causing an icmp message
461 * to be sent and the original packet to be freed).
462 */
463 if (hlen > sizeof (struct ip) && ip_dooptions(m, 0))
464 return;
465
466 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
467 * matter if it is destined to another node, or whether it is
468 * a multicast one, RSVP wants it! and prevents it from being forwarded
469 * anywhere else. Also checks if the rsvp daemon is running before
470 * grabbing the packet.
471 */
472 if (rsvp_on && ip->ip_p==IPPROTO_RSVP)
473 goto ours;
474
475 /*
476 * Check our list of addresses, to see if the packet is for us.
477 * If we don't have any addresses, assume any unicast packet
478 * we receive might be for us (and let the upper layers deal
479 * with it).
480 */
481 if (TAILQ_EMPTY(&in_ifaddrhead) &&
482 (m->m_flags & (M_MCAST|M_BCAST)) == 0)
483 goto ours;
484
485 /*
486 * Enable a consistency check between the destination address
487 * and the arrival interface for a unicast packet (the RFC 1122
488 * strong ES model) if IP forwarding is disabled and the packet
489 * is not locally generated and the packet is not subject to
490 * 'ipfw fwd'.
491 *
492 * XXX - Checking also should be disabled if the destination
493 * address is ipnat'ed to a different interface.
494 *
495 * XXX - Checking is incompatible with IP aliases added
496 * to the loopback interface instead of the interface where
497 * the packets are received.
498 */
499 checkif = ip_checkinterface && (ipforwarding == 0) &&
500 m->m_pkthdr.rcvif != NULL &&
501 ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) &&
502 (dchg == 0);
503
504 /*
505 * Check for exact addresses in the hash bucket.
506 */
507 LIST_FOREACH(ia, INADDR_HASH(ip->ip_dst.s_addr), ia_hash) {
508 /*
509 * If the address matches, verify that the packet
510 * arrived via the correct interface if checking is
511 * enabled.
512 */
513 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr &&
514 (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif))
515 goto ours;
516 }
517 /*
518 * Check for broadcast addresses.
519 *
520 * Only accept broadcast packets that arrive via the matching
521 * interface. Reception of forwarded directed broadcasts would
522 * be handled via ip_forward() and ether_output() with the loopback
523 * into the stack for SIMPLEX interfaces handled by ether_output().
524 */
525 if (m->m_pkthdr.rcvif != NULL &&
526 m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) {
527 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) {
528 if (ifa->ifa_addr->sa_family != AF_INET)
529 continue;
530 ia = ifatoia(ifa);
531 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr ==
532 ip->ip_dst.s_addr)
533 goto ours;
534 if (ia->ia_netbroadcast.s_addr == ip->ip_dst.s_addr)
535 goto ours;
536 #ifdef BOOTP_COMPAT
537 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY)
538 goto ours;
539 #endif
540 }
541 }
542 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
543 struct in_multi *inm;
544 if (ip_mrouter) {
545 /*
546 * If we are acting as a multicast router, all
547 * incoming multicast packets are passed to the
548 * kernel-level multicast forwarding function.
549 * The packet is returned (relatively) intact; if
550 * ip_mforward() returns a non-zero value, the packet
551 * must be discarded, else it may be accepted below.
552 */
553 if (ip_mforward &&
554 ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) {
555 ipstat.ips_cantforward++;
556 m_freem(m);
557 return;
558 }
559
560 /*
561 * The process-level routing daemon needs to receive
562 * all multicast IGMP packets, whether or not this
563 * host belongs to their destination groups.
564 */
565 if (ip->ip_p == IPPROTO_IGMP)
566 goto ours;
567 ipstat.ips_forward++;
568 }
569 /*
570 * See if we belong to the destination multicast group on the
571 * arrival interface.
572 */
573 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm);
574 if (inm == NULL) {
575 ipstat.ips_notmember++;
576 m_freem(m);
577 return;
578 }
579 goto ours;
580 }
581 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST)
582 goto ours;
583 if (ip->ip_dst.s_addr == INADDR_ANY)
584 goto ours;
585
586 /*
587 * FAITH(Firewall Aided Internet Translator)
588 */
589 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
590 if (ip_keepfaith) {
591 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP)
592 goto ours;
593 }
594 m_freem(m);
595 return;
596 }
597
598 /*
599 * Not for us; forward if possible and desirable.
600 */
601 if (ipforwarding == 0) {
602 ipstat.ips_cantforward++;
603 m_freem(m);
604 } else {
605 #ifdef IPSEC
606 /*
607 * Enforce inbound IPsec SPD.
608 */
609 if (ipsec4_in_reject(m, NULL)) {
610 ipsecstat.in_polvio++;
611 goto bad;
612 }
613 #endif /* IPSEC */
614 #ifdef FAST_IPSEC
615 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
616 s = splnet();
617 if (mtag != NULL) {
618 tdbi = (struct tdb_ident *)(mtag + 1);
619 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
620 } else {
621 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
622 IP_FORWARDING, &error);
623 }
624 if (sp == NULL) { /* NB: can happen if error */
625 splx(s);
626 /*XXX error stat???*/
627 DPRINTF(("ip_input: no SP for forwarding\n")); /*XXX*/
628 goto bad;
629 }
630
631 /*
632 * Check security policy against packet attributes.
633 */
634 error = ipsec_in_reject(sp, m);
635 KEY_FREESP(&sp);
636 splx(s);
637 if (error) {
638 ipstat.ips_cantforward++;
639 goto bad;
640 }
641 #endif /* FAST_IPSEC */
642 ip_forward(m, dchg);
643 }
644 return;
645
646 ours:
647 #ifdef IPSTEALTH
648 /*
649 * IPSTEALTH: Process non-routing options only
650 * if the packet is destined for us.
651 */
652 if (ipstealth && hlen > sizeof (struct ip) &&
653 ip_dooptions(m, 1))
654 return;
655 #endif /* IPSTEALTH */
656
657 /* Count the packet in the ip address stats */
658 if (ia != NULL) {
659 ia->ia_ifa.if_ipackets++;
660 ia->ia_ifa.if_ibytes += m->m_pkthdr.len;
661 }
662
663 /*
664 * Attempt reassembly; if it succeeds, proceed.
665 * ip_reass() will return a different mbuf.
666 */
667 if (ip->ip_off & (IP_MF | IP_OFFMASK)) {
668 m = ip_reass(m);
669 if (m == NULL)
670 return;
671 ip = mtod(m, struct ip *);
672 /* Get the header length of the reassembled packet */
673 hlen = ip->ip_hl << 2;
674 }
675
676 /*
677 * Further protocols expect the packet length to be w/o the
678 * IP header.
679 */
680 ip->ip_len -= hlen;
681
682 #ifdef IPSEC
683 /*
684 * enforce IPsec policy checking if we are seeing last header.
685 * note that we do not visit this with protocols with pcb layer
686 * code - like udp/tcp/raw ip.
687 */
688 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0 &&
689 ipsec4_in_reject(m, NULL)) {
690 ipsecstat.in_polvio++;
691 goto bad;
692 }
693 #endif
694 #if FAST_IPSEC
695 /*
696 * enforce IPsec policy checking if we are seeing last header.
697 * note that we do not visit this with protocols with pcb layer
698 * code - like udp/tcp/raw ip.
699 */
700 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) {
701 /*
702 * Check if the packet has already had IPsec processing
703 * done. If so, then just pass it along. This tag gets
704 * set during AH, ESP, etc. input handling, before the
705 * packet is returned to the ip input queue for delivery.
706 */
707 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
708 s = splnet();
709 if (mtag != NULL) {
710 tdbi = (struct tdb_ident *)(mtag + 1);
711 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND);
712 } else {
713 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND,
714 IP_FORWARDING, &error);
715 }
716 if (sp != NULL) {
717 /*
718 * Check security policy against packet attributes.
719 */
720 error = ipsec_in_reject(sp, m);
721 KEY_FREESP(&sp);
722 } else {
723 /* XXX error stat??? */
724 error = EINVAL;
725 DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/
726 goto bad;
727 }
728 splx(s);
729 if (error)
730 goto bad;
731 }
732 #endif /* FAST_IPSEC */
733
734 /*
735 * Switch out to protocol's input routine.
736 */
737 ipstat.ips_delivered++;
738
739 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen);
740 return;
741 bad:
742 m_freem(m);
743 }
744
745 /*
746 * Take incoming datagram fragment and try to reassemble it into
747 * whole datagram. If the argument is the first fragment or one
748 * in between the function will return NULL and store the mbuf
749 * in the fragment chain. If the argument is the last fragment
750 * the packet will be reassembled and the pointer to the new
751 * mbuf returned for further processing. Only m_tags attached
752 * to the first packet/fragment are preserved.
753 * The IP header is *NOT* adjusted out of iplen.
754 */
755
756 struct mbuf *
757 ip_reass(struct mbuf *m)
758 {
759 struct ip *ip;
760 struct mbuf *p, *q, *nq, *t;
761 struct ipq *fp = NULL;
762 struct ipqhead *head;
763 int i, hlen, next;
764 u_int8_t ecn, ecn0;
765 u_short hash;
766
767 /* If maxnipq is 0, never accept fragments. */
768 if (maxnipq == 0) {
769 ipstat.ips_fragments++;
770 ipstat.ips_fragdropped++;
771 m_freem(m);
772 return (NULL);
773 }
774
775 ip = mtod(m, struct ip *);
776 hlen = ip->ip_hl << 2;
777
778 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
779 head = &ipq[hash];
780 IPQ_LOCK();
781
782 /*
783 * Look for queue of fragments
784 * of this datagram.
785 */
786 TAILQ_FOREACH(fp, head, ipq_list)
787 if (ip->ip_id == fp->ipq_id &&
788 ip->ip_src.s_addr == fp->ipq_src.s_addr &&
789 ip->ip_dst.s_addr == fp->ipq_dst.s_addr &&
790 #ifdef MAC
791 mac_fragment_match(m, fp) &&
792 #endif
793 ip->ip_p == fp->ipq_p)
794 goto found;
795
796 fp = NULL;
797
798 /*
799 * Enforce upper bound on number of fragmented packets
800 * for which we attempt reassembly;
801 * If maxnipq is -1, accept all fragments without limitation.
802 */
803 if ((nipq > maxnipq) && (maxnipq > 0)) {
804 /*
805 * drop something from the tail of the current queue
806 * before proceeding further
807 */
808 struct ipq *q = TAILQ_LAST(head, ipqhead);
809 if (q == NULL) { /* gak */
810 for (i = 0; i < IPREASS_NHASH; i++) {
811 struct ipq *r = TAILQ_LAST(&ipq[i], ipqhead);
812 if (r) {
813 ipstat.ips_fragtimeout += r->ipq_nfrags;
814 ip_freef(&ipq[i], r);
815 break;
816 }
817 }
818 } else {
819 ipstat.ips_fragtimeout += q->ipq_nfrags;
820 ip_freef(head, q);
821 }
822 }
823
824 found:
825 /*
826 * Adjust ip_len to not reflect header,
827 * convert offset of this to bytes.
828 */
829 ip->ip_len -= hlen;
830 if (ip->ip_off & IP_MF) {
831 /*
832 * Make sure that fragments have a data length
833 * that's a non-zero multiple of 8 bytes.
834 */
835 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) {
836 ipstat.ips_toosmall++; /* XXX */
837 goto dropfrag;
838 }
839 m->m_flags |= M_FRAG;
840 } else
841 m->m_flags &= ~M_FRAG;
842 ip->ip_off <<= 3;
843
844
845 /*
846 * Attempt reassembly; if it succeeds, proceed.
847 * ip_reass() will return a different mbuf.
848 */
849 ipstat.ips_fragments++;
850 m->m_pkthdr.header = ip;
851
852 /* Previous ip_reass() started here. */
853 /*
854 * Presence of header sizes in mbufs
855 * would confuse code below.
856 */
857 m->m_data += hlen;
858 m->m_len -= hlen;
859
860 /*
861 * If first fragment to arrive, create a reassembly queue.
862 */
863 if (fp == NULL) {
864 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL)
865 goto dropfrag;
866 fp = mtod(t, struct ipq *);
867 #ifdef MAC
868 if (mac_init_ipq(fp, M_NOWAIT) != 0) {
869 m_free(t);
870 goto dropfrag;
871 }
872 mac_create_ipq(m, fp);
873 #endif
874 TAILQ_INSERT_HEAD(head, fp, ipq_list);
875 nipq++;
876 fp->ipq_nfrags = 1;
877 fp->ipq_ttl = IPFRAGTTL;
878 fp->ipq_p = ip->ip_p;
879 fp->ipq_id = ip->ip_id;
880 fp->ipq_src = ip->ip_src;
881 fp->ipq_dst = ip->ip_dst;
882 fp->ipq_frags = m;
883 m->m_nextpkt = NULL;
884 goto inserted;
885 } else {
886 fp->ipq_nfrags++;
887 #ifdef MAC
888 mac_update_ipq(m, fp);
889 #endif
890 }
891
892 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
893
894 /*
895 * Handle ECN by comparing this segment with the first one;
896 * if CE is set, do not lose CE.
897 * drop if CE and not-ECT are mixed for the same packet.
898 */
899 ecn = ip->ip_tos & IPTOS_ECN_MASK;
900 ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK;
901 if (ecn == IPTOS_ECN_CE) {
902 if (ecn0 == IPTOS_ECN_NOTECT)
903 goto dropfrag;
904 if (ecn0 != IPTOS_ECN_CE)
905 GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE;
906 }
907 if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT)
908 goto dropfrag;
909
910 /*
911 * Find a segment which begins after this one does.
912 */
913 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt)
914 if (GETIP(q)->ip_off > ip->ip_off)
915 break;
916
917 /*
918 * If there is a preceding segment, it may provide some of
919 * our data already. If so, drop the data from the incoming
920 * segment. If it provides all of our data, drop us, otherwise
921 * stick new segment in the proper place.
922 *
923 * If some of the data is dropped from the the preceding
924 * segment, then it's checksum is invalidated.
925 */
926 if (p) {
927 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off;
928 if (i > 0) {
929 if (i >= ip->ip_len)
930 goto dropfrag;
931 m_adj(m, i);
932 m->m_pkthdr.csum_flags = 0;
933 ip->ip_off += i;
934 ip->ip_len -= i;
935 }
936 m->m_nextpkt = p->m_nextpkt;
937 p->m_nextpkt = m;
938 } else {
939 m->m_nextpkt = fp->ipq_frags;
940 fp->ipq_frags = m;
941 }
942
943 /*
944 * While we overlap succeeding segments trim them or,
945 * if they are completely covered, dequeue them.
946 */
947 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off;
948 q = nq) {
949 i = (ip->ip_off + ip->ip_len) - GETIP(q)->ip_off;
950 if (i < GETIP(q)->ip_len) {
951 GETIP(q)->ip_len -= i;
952 GETIP(q)->ip_off += i;
953 m_adj(q, i);
954 q->m_pkthdr.csum_flags = 0;
955 break;
956 }
957 nq = q->m_nextpkt;
958 m->m_nextpkt = nq;
959 ipstat.ips_fragdropped++;
960 fp->ipq_nfrags--;
961 m_freem(q);
962 }
963
964 inserted:
965
966 /*
967 * Check for complete reassembly and perform frag per packet
968 * limiting.
969 *
970 * Frag limiting is performed here so that the nth frag has
971 * a chance to complete the packet before we drop the packet.
972 * As a result, n+1 frags are actually allowed per packet, but
973 * only n will ever be stored. (n = maxfragsperpacket.)
974 *
975 */
976 next = 0;
977 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) {
978 if (GETIP(q)->ip_off != next) {
979 if (fp->ipq_nfrags > maxfragsperpacket) {
980 ipstat.ips_fragdropped += fp->ipq_nfrags;
981 ip_freef(head, fp);
982 }
983 goto done;
984 }
985 next += GETIP(q)->ip_len;
986 }
987 /* Make sure the last packet didn't have the IP_MF flag */
988 if (p->m_flags & M_FRAG) {
989 if (fp->ipq_nfrags > maxfragsperpacket) {
990 ipstat.ips_fragdropped += fp->ipq_nfrags;
991 ip_freef(head, fp);
992 }
993 goto done;
994 }
995
996 /*
997 * Reassembly is complete. Make sure the packet is a sane size.
998 */
999 q = fp->ipq_frags;
1000 ip = GETIP(q);
1001 if (next + (ip->ip_hl << 2) > IP_MAXPACKET) {
1002 ipstat.ips_toolong++;
1003 ipstat.ips_fragdropped += fp->ipq_nfrags;
1004 ip_freef(head, fp);
1005 goto done;
1006 }
1007
1008 /*
1009 * Concatenate fragments.
1010 */
1011 m = q;
1012 t = m->m_next;
1013 m->m_next = 0;
1014 m_cat(m, t);
1015 nq = q->m_nextpkt;
1016 q->m_nextpkt = 0;
1017 for (q = nq; q != NULL; q = nq) {
1018 nq = q->m_nextpkt;
1019 q->m_nextpkt = NULL;
1020 m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags;
1021 m->m_pkthdr.csum_data += q->m_pkthdr.csum_data;
1022 m_cat(m, q);
1023 }
1024 #ifdef MAC
1025 mac_create_datagram_from_ipq(fp, m);
1026 mac_destroy_ipq(fp);
1027 #endif
1028
1029 /*
1030 * Create header for new ip packet by modifying header of first
1031 * packet; dequeue and discard fragment reassembly header.
1032 * Make header visible.
1033 */
1034 ip->ip_len = (ip->ip_hl << 2) + next;
1035 ip->ip_src = fp->ipq_src;
1036 ip->ip_dst = fp->ipq_dst;
1037 TAILQ_REMOVE(head, fp, ipq_list);
1038 nipq--;
1039 (void) m_free(dtom(fp));
1040 m->m_len += (ip->ip_hl << 2);
1041 m->m_data -= (ip->ip_hl << 2);
1042 /* some debugging cruft by sklower, below, will go away soon */
1043 if (m->m_flags & M_PKTHDR) /* XXX this should be done elsewhere */
1044 m_fixhdr(m);
1045 ipstat.ips_reassembled++;
1046 IPQ_UNLOCK();
1047 return (m);
1048
1049 dropfrag:
1050 ipstat.ips_fragdropped++;
1051 if (fp != NULL)
1052 fp->ipq_nfrags--;
1053 m_freem(m);
1054 done:
1055 IPQ_UNLOCK();
1056 return (NULL);
1057
1058 #undef GETIP
1059 }
1060
1061 /*
1062 * Free a fragment reassembly header and all
1063 * associated datagrams.
1064 */
1065 static void
1066 ip_freef(fhp, fp)
1067 struct ipqhead *fhp;
1068 struct ipq *fp;
1069 {
1070 register struct mbuf *q;
1071
1072 IPQ_LOCK_ASSERT();
1073
1074 while (fp->ipq_frags) {
1075 q = fp->ipq_frags;
1076 fp->ipq_frags = q->m_nextpkt;
1077 m_freem(q);
1078 }
1079 TAILQ_REMOVE(fhp, fp, ipq_list);
1080 (void) m_free(dtom(fp));
1081 nipq--;
1082 }
1083
1084 /*
1085 * IP timer processing;
1086 * if a timer expires on a reassembly
1087 * queue, discard it.
1088 */
1089 void
1090 ip_slowtimo()
1091 {
1092 register struct ipq *fp;
1093 int s = splnet();
1094 int i;
1095
1096 IPQ_LOCK();
1097 for (i = 0; i < IPREASS_NHASH; i++) {
1098 for(fp = TAILQ_FIRST(&ipq[i]); fp;) {
1099 struct ipq *fpp;
1100
1101 fpp = fp;
1102 fp = TAILQ_NEXT(fp, ipq_list);
1103 if(--fpp->ipq_ttl == 0) {
1104 ipstat.ips_fragtimeout += fpp->ipq_nfrags;
1105 ip_freef(&ipq[i], fpp);
1106 }
1107 }
1108 }
1109 /*
1110 * If we are over the maximum number of fragments
1111 * (due to the limit being lowered), drain off
1112 * enough to get down to the new limit.
1113 */
1114 if (maxnipq >= 0 && nipq > maxnipq) {
1115 for (i = 0; i < IPREASS_NHASH; i++) {
1116 while (nipq > maxnipq && !TAILQ_EMPTY(&ipq[i])) {
1117 ipstat.ips_fragdropped +=
1118 TAILQ_FIRST(&ipq[i])->ipq_nfrags;
1119 ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i]));
1120 }
1121 }
1122 }
1123 IPQ_UNLOCK();
1124 splx(s);
1125 }
1126
1127 /*
1128 * Drain off all datagram fragments.
1129 */
1130 void
1131 ip_drain()
1132 {
1133 int i;
1134
1135 IPQ_LOCK();
1136 for (i = 0; i < IPREASS_NHASH; i++) {
1137 while(!TAILQ_EMPTY(&ipq[i])) {
1138 ipstat.ips_fragdropped +=
1139 TAILQ_FIRST(&ipq[i])->ipq_nfrags;
1140 ip_freef(&ipq[i], TAILQ_FIRST(&ipq[i]));
1141 }
1142 }
1143 IPQ_UNLOCK();
1144 in_rtqdrain();
1145 }
1146
1147 /*
1148 * Do option processing on a datagram,
1149 * possibly discarding it if bad options are encountered,
1150 * or forwarding it if source-routed.
1151 * The pass argument is used when operating in the IPSTEALTH
1152 * mode to tell what options to process:
1153 * [LS]SRR (pass 0) or the others (pass 1).
1154 * The reason for as many as two passes is that when doing IPSTEALTH,
1155 * non-routing options should be processed only if the packet is for us.
1156 * Returns 1 if packet has been forwarded/freed,
1157 * 0 if the packet should be processed further.
1158 */
1159 static int
1160 ip_dooptions(struct mbuf *m, int pass)
1161 {
1162 struct ip *ip = mtod(m, struct ip *);
1163 u_char *cp;
1164 struct in_ifaddr *ia;
1165 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0;
1166 struct in_addr *sin, dst;
1167 n_time ntime;
1168 struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET };
1169
1170 /* ignore or reject packets with IP options */
1171 if (ip_doopts == 0)
1172 return 0;
1173 else if (ip_doopts == 2) {
1174 type = ICMP_UNREACH;
1175 code = ICMP_UNREACH_FILTER_PROHIB;
1176 goto bad;
1177 }
1178
1179 dst = ip->ip_dst;
1180 cp = (u_char *)(ip + 1);
1181 cnt = (ip->ip_hl << 2) - sizeof (struct ip);
1182 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1183 opt = cp[IPOPT_OPTVAL];
1184 if (opt == IPOPT_EOL)
1185 break;
1186 if (opt == IPOPT_NOP)
1187 optlen = 1;
1188 else {
1189 if (cnt < IPOPT_OLEN + sizeof(*cp)) {
1190 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1191 goto bad;
1192 }
1193 optlen = cp[IPOPT_OLEN];
1194 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) {
1195 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1196 goto bad;
1197 }
1198 }
1199 switch (opt) {
1200
1201 default:
1202 break;
1203
1204 /*
1205 * Source routing with record.
1206 * Find interface with current destination address.
1207 * If none on this machine then drop if strictly routed,
1208 * or do nothing if loosely routed.
1209 * Record interface address and bring up next address
1210 * component. If strictly routed make sure next
1211 * address is on directly accessible net.
1212 */
1213 case IPOPT_LSRR:
1214 case IPOPT_SSRR:
1215 #ifdef IPSTEALTH
1216 if (ipstealth && pass > 0)
1217 break;
1218 #endif
1219 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1220 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1221 goto bad;
1222 }
1223 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1224 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1225 goto bad;
1226 }
1227 ipaddr.sin_addr = ip->ip_dst;
1228 ia = (struct in_ifaddr *)
1229 ifa_ifwithaddr((struct sockaddr *)&ipaddr);
1230 if (ia == NULL) {
1231 if (opt == IPOPT_SSRR) {
1232 type = ICMP_UNREACH;
1233 code = ICMP_UNREACH_SRCFAIL;
1234 goto bad;
1235 }
1236 if (!ip_dosourceroute)
1237 goto nosourcerouting;
1238 /*
1239 * Loose routing, and not at next destination
1240 * yet; nothing to do except forward.
1241 */
1242 break;
1243 }
1244 off--; /* 0 origin */
1245 if (off > optlen - (int)sizeof(struct in_addr)) {
1246 /*
1247 * End of source route. Should be for us.
1248 */
1249 if (!ip_acceptsourceroute)
1250 goto nosourcerouting;
1251 save_rte(m, cp, ip->ip_src);
1252 break;
1253 }
1254 #ifdef IPSTEALTH
1255 if (ipstealth)
1256 goto dropit;
1257 #endif
1258 if (!ip_dosourceroute) {
1259 if (ipforwarding) {
1260 char buf[16]; /* aaa.bbb.ccc.ddd\0 */
1261 /*
1262 * Acting as a router, so generate ICMP
1263 */
1264 nosourcerouting:
1265 strcpy(buf, inet_ntoa(ip->ip_dst));
1266 log(LOG_WARNING,
1267 "attempted source route from %s to %s\n",
1268 inet_ntoa(ip->ip_src), buf);
1269 type = ICMP_UNREACH;
1270 code = ICMP_UNREACH_SRCFAIL;
1271 goto bad;
1272 } else {
1273 /*
1274 * Not acting as a router, so silently drop.
1275 */
1276 #ifdef IPSTEALTH
1277 dropit:
1278 #endif
1279 ipstat.ips_cantforward++;
1280 m_freem(m);
1281 return (1);
1282 }
1283 }
1284
1285 /*
1286 * locate outgoing interface
1287 */
1288 (void)memcpy(&ipaddr.sin_addr, cp + off,
1289 sizeof(ipaddr.sin_addr));
1290
1291 if (opt == IPOPT_SSRR) {
1292 #define INA struct in_ifaddr *
1293 #define SA struct sockaddr *
1294 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == NULL)
1295 ia = (INA)ifa_ifwithnet((SA)&ipaddr);
1296 } else
1297 ia = ip_rtaddr(ipaddr.sin_addr);
1298 if (ia == NULL) {
1299 type = ICMP_UNREACH;
1300 code = ICMP_UNREACH_SRCFAIL;
1301 goto bad;
1302 }
1303 ip->ip_dst = ipaddr.sin_addr;
1304 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1305 sizeof(struct in_addr));
1306 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1307 /*
1308 * Let ip_intr's mcast routing check handle mcast pkts
1309 */
1310 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr));
1311 break;
1312
1313 case IPOPT_RR:
1314 #ifdef IPSTEALTH
1315 if (ipstealth && pass == 0)
1316 break;
1317 #endif
1318 if (optlen < IPOPT_OFFSET + sizeof(*cp)) {
1319 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1320 goto bad;
1321 }
1322 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) {
1323 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1324 goto bad;
1325 }
1326 /*
1327 * If no space remains, ignore.
1328 */
1329 off--; /* 0 origin */
1330 if (off > optlen - (int)sizeof(struct in_addr))
1331 break;
1332 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst,
1333 sizeof(ipaddr.sin_addr));
1334 /*
1335 * locate outgoing interface; if we're the destination,
1336 * use the incoming interface (should be same).
1337 */
1338 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == NULL &&
1339 (ia = ip_rtaddr(ipaddr.sin_addr)) == NULL) {
1340 type = ICMP_UNREACH;
1341 code = ICMP_UNREACH_HOST;
1342 goto bad;
1343 }
1344 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr),
1345 sizeof(struct in_addr));
1346 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1347 break;
1348
1349 case IPOPT_TS:
1350 #ifdef IPSTEALTH
1351 if (ipstealth && pass == 0)
1352 break;
1353 #endif
1354 code = cp - (u_char *)ip;
1355 if (optlen < 4 || optlen > 40) {
1356 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1357 goto bad;
1358 }
1359 if ((off = cp[IPOPT_OFFSET]) < 5) {
1360 code = &cp[IPOPT_OLEN] - (u_char *)ip;
1361 goto bad;
1362 }
1363 if (off > optlen - (int)sizeof(int32_t)) {
1364 cp[IPOPT_OFFSET + 1] += (1 << 4);
1365 if ((cp[IPOPT_OFFSET + 1] & 0xf0) == 0) {
1366 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1367 goto bad;
1368 }
1369 break;
1370 }
1371 off--; /* 0 origin */
1372 sin = (struct in_addr *)(cp + off);
1373 switch (cp[IPOPT_OFFSET + 1] & 0x0f) {
1374
1375 case IPOPT_TS_TSONLY:
1376 break;
1377
1378 case IPOPT_TS_TSANDADDR:
1379 if (off + sizeof(n_time) +
1380 sizeof(struct in_addr) > optlen) {
1381 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1382 goto bad;
1383 }
1384 ipaddr.sin_addr = dst;
1385 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr,
1386 m->m_pkthdr.rcvif);
1387 if (ia == NULL)
1388 continue;
1389 (void)memcpy(sin, &IA_SIN(ia)->sin_addr,
1390 sizeof(struct in_addr));
1391 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1392 off += sizeof(struct in_addr);
1393 break;
1394
1395 case IPOPT_TS_PRESPEC:
1396 if (off + sizeof(n_time) +
1397 sizeof(struct in_addr) > optlen) {
1398 code = &cp[IPOPT_OFFSET] - (u_char *)ip;
1399 goto bad;
1400 }
1401 (void)memcpy(&ipaddr.sin_addr, sin,
1402 sizeof(struct in_addr));
1403 if (ifa_ifwithaddr((SA)&ipaddr) == NULL)
1404 continue;
1405 cp[IPOPT_OFFSET] += sizeof(struct in_addr);
1406 off += sizeof(struct in_addr);
1407 break;
1408
1409 default:
1410 code = &cp[IPOPT_OFFSET + 1] - (u_char *)ip;
1411 goto bad;
1412 }
1413 ntime = iptime();
1414 (void)memcpy(cp + off, &ntime, sizeof(n_time));
1415 cp[IPOPT_OFFSET] += sizeof(n_time);
1416 }
1417 }
1418 if (forward && ipforwarding) {
1419 ip_forward(m, 1);
1420 return (1);
1421 }
1422 return (0);
1423 bad:
1424 icmp_error(m, type, code, 0, 0);
1425 ipstat.ips_badoptions++;
1426 return (1);
1427 }
1428
1429 /*
1430 * Given address of next destination (final or next hop),
1431 * return internet address info of interface to be used to get there.
1432 */
1433 struct in_ifaddr *
1434 ip_rtaddr(dst)
1435 struct in_addr dst;
1436 {
1437 struct route sro;
1438 struct sockaddr_in *sin;
1439 struct in_ifaddr *ifa;
1440
1441 bzero(&sro, sizeof(sro));
1442 sin = (struct sockaddr_in *)&sro.ro_dst;
1443 sin->sin_family = AF_INET;
1444 sin->sin_len = sizeof(*sin);
1445 sin->sin_addr = dst;
1446 rtalloc_ign(&sro, RTF_CLONING);
1447
1448 if (sro.ro_rt == NULL)
1449 return ((struct in_ifaddr *)0);
1450
1451 ifa = ifatoia(sro.ro_rt->rt_ifa);
1452 RTFREE(sro.ro_rt);
1453 return ifa;
1454 }
1455
1456 /*
1457 * Save incoming source route for use in replies,
1458 * to be picked up later by ip_srcroute if the receiver is interested.
1459 */
1460 static void
1461 save_rte(m, option, dst)
1462 struct mbuf *m;
1463 u_char *option;
1464 struct in_addr dst;
1465 {
1466 unsigned olen;
1467 struct ipopt_tag *opts;
1468
1469 opts = (struct ipopt_tag *)m_tag_get(PACKET_TAG_IPOPTIONS,
1470 sizeof(struct ipopt_tag), M_NOWAIT);
1471 if (opts == NULL)
1472 return;
1473
1474 olen = option[IPOPT_OLEN];
1475 #ifdef DIAGNOSTIC
1476 if (ipprintfs)
1477 printf("save_rte: olen %d\n", olen);
1478 #endif
1479 if (olen > sizeof(opts->ip_srcrt) - (1 + sizeof(dst)))
1480 return;
1481 bcopy(option, opts->ip_srcrt.srcopt, olen);
1482 opts->ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr);
1483 opts->ip_srcrt.dst = dst;
1484 m_tag_prepend(m, (struct m_tag *)opts);
1485 }
1486
1487 /*
1488 * Retrieve incoming source route for use in replies,
1489 * in the same form used by setsockopt.
1490 * The first hop is placed before the options, will be removed later.
1491 */
1492 struct mbuf *
1493 ip_srcroute(m0)
1494 struct mbuf *m0;
1495 {
1496 register struct in_addr *p, *q;
1497 register struct mbuf *m;
1498 struct ipopt_tag *opts;
1499
1500 opts = (struct ipopt_tag *)m_tag_find(m0, PACKET_TAG_IPOPTIONS, NULL);
1501 if (opts == NULL)
1502 return ((struct mbuf *)0);
1503
1504 if (opts->ip_nhops == 0)
1505 return ((struct mbuf *)0);
1506 m = m_get(M_DONTWAIT, MT_HEADER);
1507 if (m == NULL)
1508 return ((struct mbuf *)0);
1509
1510 #define OPTSIZ (sizeof(opts->ip_srcrt.nop) + sizeof(opts->ip_srcrt.srcopt))
1511
1512 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1513 m->m_len = opts->ip_nhops * sizeof(struct in_addr) +
1514 sizeof(struct in_addr) + OPTSIZ;
1515 #ifdef DIAGNOSTIC
1516 if (ipprintfs)
1517 printf("ip_srcroute: nhops %d mlen %d", opts->ip_nhops, m->m_len);
1518 #endif
1519
1520 /*
1521 * First save first hop for return route
1522 */
1523 p = &(opts->ip_srcrt.route[opts->ip_nhops - 1]);
1524 *(mtod(m, struct in_addr *)) = *p--;
1525 #ifdef DIAGNOSTIC
1526 if (ipprintfs)
1527 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr));
1528 #endif
1529
1530 /*
1531 * Copy option fields and padding (nop) to mbuf.
1532 */
1533 opts->ip_srcrt.nop = IPOPT_NOP;
1534 opts->ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF;
1535 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr),
1536 &(opts->ip_srcrt.nop), OPTSIZ);
1537 q = (struct in_addr *)(mtod(m, caddr_t) +
1538 sizeof(struct in_addr) + OPTSIZ);
1539 #undef OPTSIZ
1540 /*
1541 * Record return path as an IP source route,
1542 * reversing the path (pointers are now aligned).
1543 */
1544 while (p >= opts->ip_srcrt.route) {
1545 #ifdef DIAGNOSTIC
1546 if (ipprintfs)
1547 printf(" %lx", (u_long)ntohl(q->s_addr));
1548 #endif
1549 *q++ = *p--;
1550 }
1551 /*
1552 * Last hop goes to final destination.
1553 */
1554 *q = opts->ip_srcrt.dst;
1555 #ifdef DIAGNOSTIC
1556 if (ipprintfs)
1557 printf(" %lx\n", (u_long)ntohl(q->s_addr));
1558 #endif
1559 m_tag_delete(m0, (struct m_tag *)opts);
1560 return (m);
1561 }
1562
1563 /*
1564 * Strip out IP options, at higher
1565 * level protocol in the kernel.
1566 * Second argument is buffer to which options
1567 * will be moved, and return value is their length.
1568 * XXX should be deleted; last arg currently ignored.
1569 */
1570 void
1571 ip_stripoptions(m, mopt)
1572 register struct mbuf *m;
1573 struct mbuf *mopt;
1574 {
1575 register int i;
1576 struct ip *ip = mtod(m, struct ip *);
1577 register caddr_t opts;
1578 int olen;
1579
1580 olen = (ip->ip_hl << 2) - sizeof (struct ip);
1581 opts = (caddr_t)(ip + 1);
1582 i = m->m_len - (sizeof (struct ip) + olen);
1583 bcopy(opts + olen, opts, (unsigned)i);
1584 m->m_len -= olen;
1585 if (m->m_flags & M_PKTHDR)
1586 m->m_pkthdr.len -= olen;
1587 ip->ip_v = IPVERSION;
1588 ip->ip_hl = sizeof(struct ip) >> 2;
1589 }
1590
1591 u_char inetctlerrmap[PRC_NCMDS] = {
1592 0, 0, 0, 0,
1593 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH,
1594 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED,
1595 EMSGSIZE, EHOSTUNREACH, 0, 0,
1596 0, 0, EHOSTUNREACH, 0,
1597 ENOPROTOOPT, ECONNREFUSED
1598 };
1599
1600 /*
1601 * Forward a packet. If some error occurs return the sender
1602 * an icmp packet. Note we can't always generate a meaningful
1603 * icmp message because icmp doesn't have a large enough repertoire
1604 * of codes and types.
1605 *
1606 * If not forwarding, just drop the packet. This could be confusing
1607 * if ipforwarding was zero but some routing protocol was advancing
1608 * us as a gateway to somewhere. However, we must let the routing
1609 * protocol deal with that.
1610 *
1611 * The srcrt parameter indicates whether the packet is being forwarded
1612 * via a source route.
1613 */
1614 void
1615 ip_forward(struct mbuf *m, int srcrt)
1616 {
1617 struct ip *ip = mtod(m, struct ip *);
1618 struct in_ifaddr *ia = NULL;
1619 int error, type = 0, code = 0;
1620 struct mbuf *mcopy;
1621 struct in_addr dest;
1622 struct ifnet *destifp, dummyifp;
1623
1624 #ifdef DIAGNOSTIC
1625 if (ipprintfs)
1626 printf("forward: src %lx dst %lx ttl %x\n",
1627 (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr,
1628 ip->ip_ttl);
1629 #endif
1630
1631
1632 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) {
1633 ipstat.ips_cantforward++;
1634 m_freem(m);
1635 return;
1636 }
1637 #ifdef IPSTEALTH
1638 if (!ipstealth) {
1639 #endif
1640 if (ip->ip_ttl <= IPTTLDEC) {
1641 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS,
1642 0, 0);
1643 return;
1644 }
1645 #ifdef IPSTEALTH
1646 }
1647 #endif
1648
1649 if (!srcrt && (ia = ip_rtaddr(ip->ip_dst)) == NULL) {
1650 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, 0, 0);
1651 return;
1652 }
1653
1654 /*
1655 * Save the IP header and at most 8 bytes of the payload,
1656 * in case we need to generate an ICMP message to the src.
1657 *
1658 * XXX this can be optimized a lot by saving the data in a local
1659 * buffer on the stack (72 bytes at most), and only allocating the
1660 * mbuf if really necessary. The vast majority of the packets
1661 * are forwarded without having to send an ICMP back (either
1662 * because unnecessary, or because rate limited), so we are
1663 * really we are wasting a lot of work here.
1664 *
1665 * We don't use m_copy() because it might return a reference
1666 * to a shared cluster. Both this function and ip_output()
1667 * assume exclusive access to the IP header in `m', so any
1668 * data in a cluster may change before we reach icmp_error().
1669 */
1670 MGET(mcopy, M_DONTWAIT, m->m_type);
1671 if (mcopy != NULL && !m_dup_pkthdr(mcopy, m, M_DONTWAIT)) {
1672 /*
1673 * It's probably ok if the pkthdr dup fails (because
1674 * the deep copy of the tag chain failed), but for now
1675 * be conservative and just discard the copy since
1676 * code below may some day want the tags.
1677 */
1678 m_free(mcopy);
1679 mcopy = NULL;
1680 }
1681 if (mcopy != NULL) {
1682 mcopy->m_len = imin((ip->ip_hl << 2) + 8,
1683 (int)ip->ip_len);
1684 mcopy->m_pkthdr.len = mcopy->m_len;
1685 m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t));
1686 }
1687
1688 #ifdef IPSTEALTH
1689 if (!ipstealth) {
1690 #endif
1691 ip->ip_ttl -= IPTTLDEC;
1692 #ifdef IPSTEALTH
1693 }
1694 #endif
1695
1696 /*
1697 * If forwarding packet using same interface that it came in on,
1698 * perhaps should send a redirect to sender to shortcut a hop.
1699 * Only send redirect if source is sending directly to us,
1700 * and if packet was not source routed (or has any options).
1701 * Also, don't send redirect if forwarding using a default route
1702 * or a route modified by a redirect.
1703 */
1704 dest.s_addr = 0;
1705 if (!srcrt && ipsendredirects && ia->ia_ifp == m->m_pkthdr.rcvif) {
1706 struct sockaddr_in *sin;
1707 struct route ro;
1708 struct rtentry *rt;
1709
1710 bzero(&ro, sizeof(ro));
1711 sin = (struct sockaddr_in *)&ro.ro_dst;
1712 sin->sin_family = AF_INET;
1713 sin->sin_len = sizeof(*sin);
1714 sin->sin_addr = ip->ip_dst;
1715 rtalloc_ign(&ro, RTF_CLONING);
1716
1717 rt = ro.ro_rt;
1718
1719 if (rt && (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 &&
1720 satosin(rt_key(rt))->sin_addr.s_addr != 0) {
1721 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
1722 u_long src = ntohl(ip->ip_src.s_addr);
1723
1724 if (RTA(rt) &&
1725 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) {
1726 if (rt->rt_flags & RTF_GATEWAY)
1727 dest.s_addr = satosin(rt->rt_gateway)->sin_addr.s_addr;
1728 else
1729 dest.s_addr = ip->ip_dst.s_addr;
1730 /* Router requirements says to only send host redirects */
1731 type = ICMP_REDIRECT;
1732 code = ICMP_REDIRECT_HOST;
1733 #ifdef DIAGNOSTIC
1734 if (ipprintfs)
1735 printf("redirect (%d) to %lx\n", code, (u_long)dest.s_addr);
1736 #endif
1737 }
1738 }
1739 if (rt)
1740 RTFREE(rt);
1741 }
1742
1743 error = ip_output(m, (struct mbuf *)0, NULL, IP_FORWARDING, 0, NULL);
1744 if (error)
1745 ipstat.ips_cantforward++;
1746 else {
1747 ipstat.ips_forward++;
1748 if (type)
1749 ipstat.ips_redirectsent++;
1750 else {
1751 if (mcopy)
1752 m_freem(mcopy);
1753 return;
1754 }
1755 }
1756 if (mcopy == NULL)
1757 return;
1758 destifp = NULL;
1759
1760 switch (error) {
1761
1762 case 0: /* forwarded, but need redirect */
1763 /* type, code set above */
1764 break;
1765
1766 case ENETUNREACH: /* shouldn't happen, checked above */
1767 case EHOSTUNREACH:
1768 case ENETDOWN:
1769 case EHOSTDOWN:
1770 default:
1771 type = ICMP_UNREACH;
1772 code = ICMP_UNREACH_HOST;
1773 break;
1774
1775 case EMSGSIZE:
1776 type = ICMP_UNREACH;
1777 code = ICMP_UNREACH_NEEDFRAG;
1778 #if defined(IPSEC) || defined(FAST_IPSEC)
1779 /*
1780 * If the packet is routed over IPsec tunnel, tell the
1781 * originator the tunnel MTU.
1782 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
1783 * XXX quickhack!!!
1784 */
1785 {
1786 struct secpolicy *sp = NULL;
1787 int ipsecerror;
1788 int ipsechdr;
1789 struct route *ro;
1790
1791 #ifdef IPSEC
1792 sp = ipsec4_getpolicybyaddr(mcopy,
1793 IPSEC_DIR_OUTBOUND,
1794 IP_FORWARDING,
1795 &ipsecerror);
1796 #else /* FAST_IPSEC */
1797 sp = ipsec_getpolicybyaddr(mcopy,
1798 IPSEC_DIR_OUTBOUND,
1799 IP_FORWARDING,
1800 &ipsecerror);
1801 #endif
1802 if (sp != NULL) {
1803 /* count IPsec header size */
1804 ipsechdr = ipsec4_hdrsiz(mcopy,
1805 IPSEC_DIR_OUTBOUND,
1806 NULL);
1807
1808 /*
1809 * find the correct route for outer IPv4
1810 * header, compute tunnel MTU.
1811 *
1812 * XXX BUG ALERT
1813 * The "dummyifp" code relies upon the fact
1814 * that icmp_error() touches only ifp->if_mtu.
1815 */
1816 /*XXX*/
1817 destifp = NULL;
1818 if (sp->req != NULL
1819 && sp->req->sav != NULL
1820 && sp->req->sav->sah != NULL) {
1821 ro = &sp->req->sav->sah->sa_route;
1822 if (ro->ro_rt && ro->ro_rt->rt_ifp) {
1823 dummyifp.if_mtu =
1824 ro->ro_rt->rt_rmx.rmx_mtu ?
1825 ro->ro_rt->rt_rmx.rmx_mtu :
1826 ro->ro_rt->rt_ifp->if_mtu;
1827 dummyifp.if_mtu -= ipsechdr;
1828 destifp = &dummyifp;
1829 }
1830 }
1831
1832 #ifdef IPSEC
1833 key_freesp(sp);
1834 #else /* FAST_IPSEC */
1835 KEY_FREESP(&sp);
1836 #endif
1837 ipstat.ips_cantfrag++;
1838 break;
1839 } else
1840 #endif /*IPSEC || FAST_IPSEC*/
1841 /*
1842 * When doing source routing 'ia' can be NULL. Fall back
1843 * to the minimum guaranteed routeable packet size and use
1844 * the same hack as IPSEC to setup a dummyifp for icmp.
1845 */
1846 if (ia == NULL) {
1847 dummyifp.if_mtu = IP_MSS;
1848 destifp = &dummyifp;
1849 } else
1850 destifp = ia->ia_ifp;
1851 #if defined(IPSEC) || defined(FAST_IPSEC)
1852 }
1853 #endif /*IPSEC || FAST_IPSEC*/
1854 ipstat.ips_cantfrag++;
1855 break;
1856
1857 case ENOBUFS:
1858 /*
1859 * A router should not generate ICMP_SOURCEQUENCH as
1860 * required in RFC1812 Requirements for IP Version 4 Routers.
1861 * Source quench could be a big problem under DoS attacks,
1862 * or if the underlying interface is rate-limited.
1863 * Those who need source quench packets may re-enable them
1864 * via the net.inet.ip.sendsourcequench sysctl.
1865 */
1866 if (ip_sendsourcequench == 0) {
1867 m_freem(mcopy);
1868 return;
1869 } else {
1870 type = ICMP_SOURCEQUENCH;
1871 code = 0;
1872 }
1873 break;
1874
1875 case EACCES: /* ipfw denied packet */
1876 m_freem(mcopy);
1877 return;
1878 }
1879 icmp_error(mcopy, type, code, dest.s_addr, destifp);
1880 }
1881
1882 void
1883 ip_savecontrol(inp, mp, ip, m)
1884 register struct inpcb *inp;
1885 register struct mbuf **mp;
1886 register struct ip *ip;
1887 register struct mbuf *m;
1888 {
1889 if (inp->inp_socket->so_options & (SO_BINTIME | SO_TIMESTAMP)) {
1890 struct bintime bt;
1891
1892 bintime(&bt);
1893 if (inp->inp_socket->so_options & SO_BINTIME) {
1894 *mp = sbcreatecontrol((caddr_t) &bt, sizeof(bt),
1895 SCM_BINTIME, SOL_SOCKET);
1896 if (*mp)
1897 mp = &(*mp)->m_next;
1898 }
1899 if (inp->inp_socket->so_options & SO_TIMESTAMP) {
1900 struct timeval tv;
1901
1902 bintime2timeval(&bt, &tv);
1903 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv),
1904 SCM_TIMESTAMP, SOL_SOCKET);
1905 if (*mp)
1906 mp = &(*mp)->m_next;
1907 }
1908 }
1909 if (inp->inp_flags & INP_RECVDSTADDR) {
1910 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst,
1911 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP);
1912 if (*mp)
1913 mp = &(*mp)->m_next;
1914 }
1915 if (inp->inp_flags & INP_RECVTTL) {
1916 *mp = sbcreatecontrol((caddr_t) &ip->ip_ttl,
1917 sizeof(u_char), IP_RECVTTL, IPPROTO_IP);
1918 if (*mp)
1919 mp = &(*mp)->m_next;
1920 }
1921 #ifdef notyet
1922 /* XXX
1923 * Moving these out of udp_input() made them even more broken
1924 * than they already were.
1925 */
1926 /* options were tossed already */
1927 if (inp->inp_flags & INP_RECVOPTS) {
1928 *mp = sbcreatecontrol((caddr_t) opts_deleted_above,
1929 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP);
1930 if (*mp)
1931 mp = &(*mp)->m_next;
1932 }
1933 /* ip_srcroute doesn't do what we want here, need to fix */
1934 if (inp->inp_flags & INP_RECVRETOPTS) {
1935 *mp = sbcreatecontrol((caddr_t) ip_srcroute(m),
1936 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP);
1937 if (*mp)
1938 mp = &(*mp)->m_next;
1939 }
1940 #endif
1941 if (inp->inp_flags & INP_RECVIF) {
1942 struct ifnet *ifp;
1943 struct sdlbuf {
1944 struct sockaddr_dl sdl;
1945 u_char pad[32];
1946 } sdlbuf;
1947 struct sockaddr_dl *sdp;
1948 struct sockaddr_dl *sdl2 = &sdlbuf.sdl;
1949
1950 if (((ifp = m->m_pkthdr.rcvif))
1951 && ( ifp->if_index && (ifp->if_index <= if_index))) {
1952 sdp = (struct sockaddr_dl *)
1953 (ifaddr_byindex(ifp->if_index)->ifa_addr);
1954 /*
1955 * Change our mind and don't try copy.
1956 */
1957 if ((sdp->sdl_family != AF_LINK)
1958 || (sdp->sdl_len > sizeof(sdlbuf))) {
1959 goto makedummy;
1960 }
1961 bcopy(sdp, sdl2, sdp->sdl_len);
1962 } else {
1963 makedummy:
1964 sdl2->sdl_len
1965 = offsetof(struct sockaddr_dl, sdl_data[0]);
1966 sdl2->sdl_family = AF_LINK;
1967 sdl2->sdl_index = 0;
1968 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0;
1969 }
1970 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len,
1971 IP_RECVIF, IPPROTO_IP);
1972 if (*mp)
1973 mp = &(*mp)->m_next;
1974 }
1975 }
1976
1977 /*
1978 * XXX these routines are called from the upper part of the kernel.
1979 * They need to be locked when we remove Giant.
1980 *
1981 * They could also be moved to ip_mroute.c, since all the RSVP
1982 * handling is done there already.
1983 */
1984 static int ip_rsvp_on;
1985 struct socket *ip_rsvpd;
1986 int
1987 ip_rsvp_init(struct socket *so)
1988 {
1989 if (so->so_type != SOCK_RAW ||
1990 so->so_proto->pr_protocol != IPPROTO_RSVP)
1991 return EOPNOTSUPP;
1992
1993 if (ip_rsvpd != NULL)
1994 return EADDRINUSE;
1995
1996 ip_rsvpd = so;
1997 /*
1998 * This may seem silly, but we need to be sure we don't over-increment
1999 * the RSVP counter, in case something slips up.
2000 */
2001 if (!ip_rsvp_on) {
2002 ip_rsvp_on = 1;
2003 rsvp_on++;
2004 }
2005
2006 return 0;
2007 }
2008
2009 int
2010 ip_rsvp_done(void)
2011 {
2012 ip_rsvpd = NULL;
2013 /*
2014 * This may seem silly, but we need to be sure we don't over-decrement
2015 * the RSVP counter, in case something slips up.
2016 */
2017 if (ip_rsvp_on) {
2018 ip_rsvp_on = 0;
2019 rsvp_on--;
2020 }
2021 return 0;
2022 }
2023
2024 void
2025 rsvp_input(struct mbuf *m, int off) /* XXX must fixup manually */
2026 {
2027 if (rsvp_input_p) { /* call the real one if loaded */
2028 rsvp_input_p(m, off);
2029 return;
2030 }
2031
2032 /* Can still get packets with rsvp_on = 0 if there is a local member
2033 * of the group to which the RSVP packet is addressed. But in this
2034 * case we want to throw the packet away.
2035 */
2036
2037 if (!rsvp_on) {
2038 m_freem(m);
2039 return;
2040 }
2041
2042 if (ip_rsvpd != NULL) {
2043 rip_input(m, off);
2044 return;
2045 }
2046 /* Drop the packet */
2047 m_freem(m);
2048 }
Cache object: 89f67fca0c39c4ffa44d3d7496278922
|