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