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