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