ip_output.c revision 105194
1177633Sdfr/*
2177633Sdfr * Copyright (c) 1982, 1986, 1988, 1990, 1993
3177633Sdfr *	The Regents of the University of California.  All rights reserved.
4177633Sdfr *
5177633Sdfr * Redistribution and use in source and binary forms, with or without
6177633Sdfr * modification, are permitted provided that the following conditions
7177633Sdfr * are met:
8177633Sdfr * 1. Redistributions of source code must retain the above copyright
9177633Sdfr *    notice, this list of conditions and the following disclaimer.
10177633Sdfr * 2. Redistributions in binary form must reproduce the above copyright
11177633Sdfr *    notice, this list of conditions and the following disclaimer in the
12177633Sdfr *    documentation and/or other materials provided with the distribution.
13177633Sdfr * 3. All advertising materials mentioning features or use of this software
14177633Sdfr *    must display the following acknowledgement:
15177633Sdfr *	This product includes software developed by the University of
16177633Sdfr *	California, Berkeley and its contributors.
17177633Sdfr * 4. Neither the name of the University nor the names of its contributors
18177633Sdfr *    may be used to endorse or promote products derived from this software
19177633Sdfr *    without specific prior written permission.
20177633Sdfr *
21177633Sdfr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22177633Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23177633Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24177633Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25177633Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26177633Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27177633Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28177633Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29177633Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30177633Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31177633Sdfr * SUCH DAMAGE.
32177633Sdfr *
33177633Sdfr *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
34177633Sdfr * $FreeBSD: head/sys/netinet/ip_output.c 105194 2002-10-16 01:54:46Z sam $
35177633Sdfr */
36177633Sdfr
37177633Sdfr#define _IP_VHL
38177633Sdfr
39177633Sdfr#include "opt_ipfw.h"
40177633Sdfr#include "opt_ipdn.h"
41177633Sdfr#include "opt_ipdivert.h"
42177633Sdfr#include "opt_ipfilter.h"
43177633Sdfr#include "opt_ipsec.h"
44177633Sdfr#include "opt_mac.h"
45177633Sdfr#include "opt_pfil_hooks.h"
46177633Sdfr#include "opt_random_ip_id.h"
47177633Sdfr
48180025Sdfr#include <sys/param.h>
49177633Sdfr#include <sys/systm.h>
50177633Sdfr#include <sys/kernel.h>
51177633Sdfr#include <sys/mac.h>
52177633Sdfr#include <sys/malloc.h>
53177633Sdfr#include <sys/mbuf.h>
54177633Sdfr#include <sys/protosw.h>
55177633Sdfr#include <sys/socket.h>
56177633Sdfr#include <sys/socketvar.h>
57177633Sdfr
58177633Sdfr#include <net/if.h>
59177633Sdfr#include <net/route.h>
60196503Szec
61196503Szec#include <netinet/in.h>
62177633Sdfr#include <netinet/in_systm.h>
63177685Sdfr#include <netinet/ip.h>
64177633Sdfr#include <netinet/in_pcb.h>
65177633Sdfr#include <netinet/in_var.h>
66177633Sdfr#include <netinet/ip_var.h>
67177633Sdfr
68177633Sdfr#include <machine/in_cksum.h>
69177633Sdfr
70177633Sdfrstatic MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
71177633Sdfr
72177633Sdfr#ifdef IPSEC
73177633Sdfr#include <netinet6/ipsec.h>
74177633Sdfr#include <netkey/key.h>
75177633Sdfr#ifdef IPSEC_DEBUG
76180025Sdfr#include <netkey/key_debug.h>
77184588Sdfr#else
78177633Sdfr#define	KEYDEBUG(lev,arg)
79177633Sdfr#endif
80177633Sdfr#endif /*IPSEC*/
81177633Sdfr
82184588Sdfr#include <netinet/ip_fw.h>
83177633Sdfr#include <netinet/ip_dummynet.h>
84193272Sjhb
85177633Sdfr#define print_ip(x, a, y)	 printf("%s %d.%d.%d.%d%s",\
86177633Sdfr				x, (ntohl(a.s_addr)>>24)&0xFF,\
87177633Sdfr				  (ntohl(a.s_addr)>>16)&0xFF,\
88177633Sdfr				  (ntohl(a.s_addr)>>8)&0xFF,\
89177633Sdfr				  (ntohl(a.s_addr))&0xFF, y);
90177633Sdfr
91184588Sdfru_short ip_id;
92177633Sdfr
93177633Sdfrstatic struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
94177633Sdfrstatic struct ifnet *ip_multicast_if(struct in_addr *, int *);
95177633Sdfrstatic void	ip_mloopback
96177633Sdfr	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
97177633Sdfrstatic int	ip_getmoptions
98177633Sdfr	(struct sockopt *, struct ip_moptions *);
99180025Sdfrstatic int	ip_pcbopts(int, struct mbuf **, struct mbuf *);
100180025Sdfrstatic int	ip_setmoptions
101180025Sdfr	(struct sockopt *, struct ip_moptions **);
102177633Sdfr
103177633Sdfrint	ip_optcopy(struct ip *, struct ip *);
104177633Sdfr
105180025Sdfr
106177633Sdfrextern	struct protosw inetsw[];
107177633Sdfr
108177633Sdfr/*
109184588Sdfr * IP output.  The packet in mbuf chain m contains a skeletal IP
110177633Sdfr * header (with len, off, ttl, proto, tos, src, dst).
111177633Sdfr * The mbuf chain containing the packet will be freed.
112177633Sdfr * The mbuf opt, if present, will not be freed.
113177633Sdfr */
114177633Sdfrint
115177633Sdfrip_output(m0, opt, ro, flags, imo, inp)
116177633Sdfr	struct mbuf *m0;
117193272Sjhb	struct mbuf *opt;
118177633Sdfr	struct route *ro;
119177633Sdfr	int flags;
120177633Sdfr	struct ip_moptions *imo;
121177633Sdfr	struct inpcb *inp;
122177633Sdfr{
123177633Sdfr	struct ip *ip, *mhip;
124177633Sdfr	struct ifnet *ifp = NULL;	/* keep compiler happy */
125177633Sdfr	struct mbuf *m;
126177633Sdfr	int hlen = sizeof (struct ip);
127177633Sdfr	int len, off, error = 0;
128193437Srmacklem	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
129177633Sdfr	struct in_ifaddr *ia = NULL;
130177633Sdfr	int isbroadcast, sw_csum;
131193437Srmacklem	struct in_addr pkt_dst;
132193437Srmacklem#ifdef IPSEC
133177633Sdfr	struct route iproute;
134177633Sdfr	struct secpolicy *sp = NULL;
135177633Sdfr	struct socket *so = inp ? inp->inp_socket : NULL;
136177633Sdfr#endif
137180025Sdfr	struct ip_fw_args args;
138184588Sdfr	int src_was_INADDR_ANY = 0;	/* as the name says... */
139184588Sdfr#ifdef PFIL_HOOKS
140177633Sdfr	struct packet_filter_hook *pfh;
141177633Sdfr	struct mbuf *m1;
142177633Sdfr	int rv;
143177633Sdfr#endif /* PFIL_HOOKS */
144177633Sdfr
145177633Sdfr	args.eh = NULL;
146177633Sdfr	args.rule = NULL;
147177633Sdfr	args.next_hop = NULL;
148177633Sdfr	args.divert_rule = 0;			/* divert cookie */
149177633Sdfr
150177633Sdfr	/* Grab info from MT_TAG mbufs prepended to the chain. */
151177633Sdfr	for (; m0 && m0->m_type == MT_TAG; m0 = m0->m_next) {
152177633Sdfr		switch(m0->_m_tag_id) {
153177633Sdfr		default:
154177633Sdfr			printf("ip_output: unrecognised MT_TAG tag %d\n",
155177633Sdfr			    m0->_m_tag_id);
156177633Sdfr			break;
157184588Sdfr
158184588Sdfr		case PACKET_TAG_DUMMYNET:
159184588Sdfr			/*
160177633Sdfr			 * the packet was already tagged, so part of the
161177633Sdfr			 * processing was already done, and we need to go down.
162184588Sdfr			 * Get parameters from the header.
163184588Sdfr			 */
164184588Sdfr			args.rule = ((struct dn_pkt *)m0)->rule;
165177633Sdfr			opt = NULL ;
166177633Sdfr			ro = & ( ((struct dn_pkt *)m0)->ro ) ;
167177633Sdfr			imo = NULL ;
168177633Sdfr			dst = ((struct dn_pkt *)m0)->dn_dst ;
169177633Sdfr			ifp = ((struct dn_pkt *)m0)->ifp ;
170177633Sdfr			flags = ((struct dn_pkt *)m0)->flags ;
171177633Sdfr			break;
172177633Sdfr
173177633Sdfr		case PACKET_TAG_DIVERT:
174177633Sdfr			args.divert_rule = (intptr_t)m0->m_data & 0xffff;
175177633Sdfr			break;
176177633Sdfr
177177633Sdfr		case PACKET_TAG_IPFORWARD:
178177633Sdfr			args.next_hop = (struct sockaddr_in *)m0->m_data;
179177633Sdfr			break;
180177633Sdfr		}
181177633Sdfr	}
182177633Sdfr	m = m0;
183177633Sdfr
184177633Sdfr	KASSERT(!m || (m->m_flags & M_PKTHDR) != 0, ("ip_output: no HDR"));
185177633Sdfr
186177633Sdfr	if (args.rule != NULL) {	/* dummynet already saw us */
187177633Sdfr		ip = mtod(m, struct ip *);
188177633Sdfr		hlen = IP_VHL_HL(ip->ip_vhl) << 2 ;
189177633Sdfr		if (ro->ro_rt)
190177633Sdfr			ia = ifatoia(ro->ro_rt->rt_ifa);
191193272Sjhb		goto sendit;
192177633Sdfr	}
193177633Sdfr
194177633Sdfr	if (opt) {
195177633Sdfr		len = 0;
196213756Srmacklem		m = ip_insertoptions(m, opt, &len);
197177633Sdfr		if (len != 0)
198177633Sdfr			hlen = len;
199177633Sdfr	}
200177633Sdfr	ip = mtod(m, struct ip *);
201177633Sdfr	pkt_dst = args.next_hop ? args.next_hop->sin_addr : ip->ip_dst;
202177633Sdfr
203177633Sdfr	/*
204177633Sdfr	 * Fill in IP header.
205177633Sdfr	 */
206177633Sdfr	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
207177633Sdfr		ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
208177633Sdfr		ip->ip_off &= IP_DF;
209177633Sdfr#ifdef RANDOM_IP_ID
210177633Sdfr		ip->ip_id = ip_randomid();
211177633Sdfr#else
212177633Sdfr		ip->ip_id = htons(ip_id++);
213177633Sdfr#endif
214177633Sdfr		ipstat.ips_localout++;
215177633Sdfr	} else {
216177633Sdfr		hlen = IP_VHL_HL(ip->ip_vhl) << 2;
217177633Sdfr	}
218177633Sdfr
219177633Sdfr	dst = (struct sockaddr_in *)&ro->ro_dst;
220177633Sdfr	/*
221177633Sdfr	 * If there is a cached route,
222177633Sdfr	 * check that it is to the same destination
223177633Sdfr	 * and is still up.  If not, free it and try again.
224177633Sdfr	 * The address family should also be checked in case of sharing the
225177633Sdfr	 * cache with IPv6.
226177633Sdfr	 */
227177633Sdfr	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
228180025Sdfr			  dst->sin_family != AF_INET ||
229180025Sdfr			  dst->sin_addr.s_addr != pkt_dst.s_addr)) {
230184588Sdfr		RTFREE(ro->ro_rt);
231177633Sdfr		ro->ro_rt = (struct rtentry *)0;
232177633Sdfr	}
233177633Sdfr	if (ro->ro_rt == 0) {
234180025Sdfr		bzero(dst, sizeof(*dst));
235177633Sdfr		dst->sin_family = AF_INET;
236177633Sdfr		dst->sin_len = sizeof(*dst);
237177633Sdfr		dst->sin_addr = pkt_dst;
238177633Sdfr	}
239177633Sdfr	/*
240177633Sdfr	 * If routing to interface only,
241177633Sdfr	 * short circuit routing lookup.
242177633Sdfr	 */
243177633Sdfr	if (flags & IP_ROUTETOIF) {
244177633Sdfr		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 &&
245184588Sdfr		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) {
246184588Sdfr			ipstat.ips_noroute++;
247184588Sdfr			error = ENETUNREACH;
248177633Sdfr			goto bad;
249177633Sdfr		}
250177633Sdfr		ifp = ia->ia_ifp;
251177633Sdfr		ip->ip_ttl = 1;
252177633Sdfr		isbroadcast = in_broadcast(dst->sin_addr, ifp);
253177633Sdfr	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
254177633Sdfr	    imo != NULL && imo->imo_multicast_ifp != NULL) {
255177633Sdfr		/*
256177633Sdfr		 * Bypass the normal routing lookup for multicast
257177633Sdfr		 * packets if the interface is specified.
258177633Sdfr		 */
259201758Smbr		ifp = imo->imo_multicast_ifp;
260177633Sdfr		IFP_TO_IA(ifp, ia);
261177633Sdfr		isbroadcast = 0;	/* fool gcc */
262177633Sdfr	} else {
263177633Sdfr		/*
264177633Sdfr		 * If this is the case, we probably don't want to allocate
265177633Sdfr		 * a protocol-cloned route since we didn't get one from the
266177633Sdfr		 * ULP.  This lets TCP do its thing, while not burdening
267177633Sdfr		 * forwarding or ICMP with the overhead of cloning a route.
268213756Srmacklem		 * Of course, we still want to do any cloning requested by
269213756Srmacklem		 * the link layer, as this is probably required in all cases
270213756Srmacklem		 * for correct operation (as it is for ARP).
271213756Srmacklem		 */
272213756Srmacklem		if (ro->ro_rt == 0)
273213756Srmacklem			rtalloc_ign(ro, RTF_PRCLONING);
274177633Sdfr		if (ro->ro_rt == 0) {
275193272Sjhb			ipstat.ips_noroute++;
276177633Sdfr			error = EHOSTUNREACH;
277177633Sdfr			goto bad;
278193272Sjhb		}
279193272Sjhb		ia = ifatoia(ro->ro_rt->rt_ifa);
280177633Sdfr		ifp = ro->ro_rt->rt_ifp;
281177633Sdfr		ro->ro_rt->rt_use++;
282177633Sdfr		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
283177633Sdfr			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
284193272Sjhb		if (ro->ro_rt->rt_flags & RTF_HOST)
285177633Sdfr			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
286177633Sdfr		else
287177633Sdfr			isbroadcast = in_broadcast(dst->sin_addr, ifp);
288177633Sdfr	}
289177633Sdfr	if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) {
290177633Sdfr		struct in_multi *inm;
291177633Sdfr
292177633Sdfr		m->m_flags |= M_MCAST;
293193272Sjhb		/*
294177633Sdfr		 * IP destination address is multicast.  Make sure "dst"
295193272Sjhb		 * still points to the address in "ro".  (It may have been
296193272Sjhb		 * changed to point to a gateway address, above.)
297177633Sdfr		 */
298177633Sdfr		dst = (struct sockaddr_in *)&ro->ro_dst;
299177633Sdfr		/*
300177633Sdfr		 * See if the caller provided any multicast options
301177633Sdfr		 */
302177633Sdfr		if (imo != NULL) {
303177633Sdfr			ip->ip_ttl = imo->imo_multicast_ttl;
304177633Sdfr			if (imo->imo_multicast_vif != -1)
305193437Srmacklem				ip->ip_src.s_addr =
306177633Sdfr				    ip_mcast_src(imo->imo_multicast_vif);
307193272Sjhb		} else
308177633Sdfr			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
309177633Sdfr		/*
310177633Sdfr		 * Confirm that the outgoing interface supports multicast.
311180025Sdfr		 */
312177633Sdfr		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
313177633Sdfr			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
314177633Sdfr				ipstat.ips_noroute++;
315177633Sdfr				error = ENETUNREACH;
316177633Sdfr				goto bad;
317177633Sdfr			}
318177633Sdfr		}
319177633Sdfr		/*
320177633Sdfr		 * If source address not specified yet, use address
321177633Sdfr		 * of outgoing interface.
322177633Sdfr		 */
323177633Sdfr		if (ip->ip_src.s_addr == INADDR_ANY) {
324177633Sdfr			/* Interface may have no addresses. */
325177633Sdfr			if (ia != NULL)
326177633Sdfr				ip->ip_src = IA_SIN(ia)->sin_addr;
327177633Sdfr		}
328177633Sdfr
329180025Sdfr		if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
330180025Sdfr			/*
331177633Sdfr			 * XXX
332184588Sdfr			 * delayed checksums are not currently
333184588Sdfr			 * compatible with IP multicast routing
334177633Sdfr			 */
335177633Sdfr			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
336177633Sdfr				in_delayed_cksum(m);
337193272Sjhb				m->m_pkthdr.csum_flags &=
338184588Sdfr					~CSUM_DELAY_DATA;
339180025Sdfr			}
340184588Sdfr		}
341184588Sdfr		IN_LOOKUP_MULTI(pkt_dst, ifp, inm);
342177633Sdfr		if (inm != NULL &&
343177633Sdfr		   (imo == NULL || imo->imo_multicast_loop)) {
344177633Sdfr			/*
345180025Sdfr			 * If we belong to the destination multicast group
346177633Sdfr			 * on the outgoing interface, and the caller did not
347180025Sdfr			 * forbid loopback, loop back a copy.
348180025Sdfr			 */
349180025Sdfr			ip_mloopback(ifp, m, dst, hlen);
350184588Sdfr		}
351177633Sdfr		else {
352177633Sdfr			/*
353184588Sdfr			 * If we are acting as a multicast router, perform
354184588Sdfr			 * multicast forwarding as if the packet had just
355180025Sdfr			 * arrived on the interface to which we are about
356177633Sdfr			 * to send.  The multicast forwarding function
357177633Sdfr			 * recursively calls this function, using the
358193272Sjhb			 * IP_FORWARDING flag to prevent infinite recursion.
359180025Sdfr			 *
360180025Sdfr			 * Multicasts that are looped back by ip_mloopback(),
361177633Sdfr			 * above, will be forwarded by the ip_input() routine,
362177633Sdfr			 * if necessary.
363184588Sdfr			 */
364180025Sdfr			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
365180025Sdfr				/*
366180025Sdfr				 * Check if rsvp daemon is running. If not, don't
367180025Sdfr				 * set ip_moptions. This ensures that the packet
368180025Sdfr				 * is multicast and not just sent down one link
369177633Sdfr				 * as prescribed by rsvpd.
370184588Sdfr				 */
371180025Sdfr				if (!rsvp_on)
372184588Sdfr				  imo = NULL;
373184588Sdfr				if (ip_mforward(ip, ifp, m, imo) != 0) {
374180025Sdfr					m_freem(m);
375184588Sdfr					goto done;
376184588Sdfr				}
377180025Sdfr			}
378180025Sdfr		}
379180025Sdfr
380180025Sdfr		/*
381180025Sdfr		 * Multicasts with a time-to-live of zero may be looped-
382177633Sdfr		 * back, above, but must not be transmitted on a network.
383180025Sdfr		 * Also, multicasts addressed to the loopback interface
384177633Sdfr		 * are not sent -- the above call to ip_mloopback() will
385180025Sdfr		 * loop back a copy if this host actually belongs to the
386177633Sdfr		 * destination group on the loopback interface.
387180025Sdfr		 */
388180025Sdfr		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
389180025Sdfr			m_freem(m);
390180025Sdfr			goto done;
391177633Sdfr		}
392177633Sdfr
393177633Sdfr		goto sendit;
394177633Sdfr	}
395177633Sdfr#ifndef notdef
396177633Sdfr	/*
397177633Sdfr	 * If the source address is not specified yet, use the address
398184588Sdfr	 * of the outoing interface. In case, keep note we did that, so
399184588Sdfr	 * if the the firewall changes the next-hop causing the output
400177633Sdfr	 * interface to change, we can fix that.
401177633Sdfr	 */
402177633Sdfr	if (ip->ip_src.s_addr == INADDR_ANY) {
403177633Sdfr		/* Interface may have no addresses. */
404177633Sdfr		if (ia != NULL) {
405177633Sdfr			ip->ip_src = IA_SIN(ia)->sin_addr;
406177633Sdfr			src_was_INADDR_ANY = 1;
407177633Sdfr		}
408177633Sdfr	}
409177633Sdfr#endif /* notdef */
410177633Sdfr	/*
411180025Sdfr	 * Verify that we have any chance at all of being able to queue
412180025Sdfr	 *      the packet or packet fragments
413184588Sdfr	 */
414184588Sdfr	if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >=
415184588Sdfr		ifp->if_snd.ifq_maxlen) {
416184588Sdfr			error = ENOBUFS;
417184588Sdfr			ipstat.ips_odropped++;
418184588Sdfr			goto bad;
419184588Sdfr	}
420184588Sdfr
421184588Sdfr	/*
422177633Sdfr	 * Look for broadcast address and
423180025Sdfr	 * verify user is allowed to send
424177633Sdfr	 * such a packet.
425177633Sdfr	 */
426177633Sdfr	if (isbroadcast) {
427177633Sdfr		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
428177633Sdfr			error = EADDRNOTAVAIL;
429177633Sdfr			goto bad;
430177633Sdfr		}
431177633Sdfr		if ((flags & IP_ALLOWBROADCAST) == 0) {
432177633Sdfr			error = EACCES;
433177633Sdfr			goto bad;
434248195Sglebius		}
435184588Sdfr		/* don't allow broadcast messages to be fragmented */
436184588Sdfr		if ((u_short)ip->ip_len > ifp->if_mtu) {
437184588Sdfr			error = EMSGSIZE;
438177633Sdfr			goto bad;
439177633Sdfr		}
440177633Sdfr		m->m_flags |= M_BCAST;
441177633Sdfr	} else {
442177633Sdfr		m->m_flags &= ~M_BCAST;
443177633Sdfr	}
444177633Sdfr
445177633Sdfrsendit:
446184588Sdfr#ifdef IPSEC
447177633Sdfr	/* get SP for this packet */
448177633Sdfr	if (so == NULL)
449177633Sdfr		sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error);
450184588Sdfr	else
451184588Sdfr		sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error);
452184588Sdfr
453177633Sdfr	if (sp == NULL) {
454177633Sdfr		ipsecstat.out_inval++;
455177633Sdfr		goto bad;
456184588Sdfr	}
457177633Sdfr
458180025Sdfr	error = 0;
459177633Sdfr
460184588Sdfr	/* check policy */
461184588Sdfr	switch (sp->policy) {
462184588Sdfr	case IPSEC_POLICY_DISCARD:
463184588Sdfr		/*
464184588Sdfr		 * This packet is just discarded.
465184588Sdfr		 */
466184588Sdfr		ipsecstat.out_polvio++;
467184588Sdfr		goto bad;
468184588Sdfr
469184588Sdfr	case IPSEC_POLICY_BYPASS:
470225234Sart	case IPSEC_POLICY_NONE:
471225234Sart		/* no need to do IPsec. */
472225234Sart		goto skip_ipsec;
473225234Sart
474184588Sdfr	case IPSEC_POLICY_IPSEC:
475184588Sdfr		if (sp->req == NULL) {
476184588Sdfr			/* acquire a policy */
477184588Sdfr			error = key_spdacquire(sp);
478184588Sdfr			goto bad;
479180025Sdfr		}
480177633Sdfr		break;
481177633Sdfr
482177633Sdfr	case IPSEC_POLICY_ENTRUST:
483177633Sdfr	default:
484177633Sdfr		printf("ip_output: Invalid policy found. %d\n", sp->policy);
485177633Sdfr	}
486177633Sdfr    {
487177633Sdfr	struct ipsec_output_state state;
488177633Sdfr	bzero(&state, sizeof(state));
489177633Sdfr	state.m = m;
490177633Sdfr	if (flags & IP_ROUTETOIF) {
491177633Sdfr		state.ro = &iproute;
492177633Sdfr		bzero(&iproute, sizeof(iproute));
493184588Sdfr	} else
494184588Sdfr		state.ro = ro;
495184588Sdfr	state.dst = (struct sockaddr *)dst;
496184588Sdfr
497184588Sdfr	ip->ip_sum = 0;
498177633Sdfr
499177633Sdfr	/*
500177633Sdfr	 * XXX
501180025Sdfr	 * delayed checksums are not currently compatible with IPsec
502184588Sdfr	 */
503184588Sdfr	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
504184588Sdfr		in_delayed_cksum(m);
505184588Sdfr		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
506184588Sdfr	}
507184588Sdfr
508184588Sdfr	ip->ip_len = htons(ip->ip_len);
509177633Sdfr	ip->ip_off = htons(ip->ip_off);
510177633Sdfr
511177633Sdfr	error = ipsec4_output(&state, sp, flags);
512177633Sdfr
513177633Sdfr	m = state.m;
514180025Sdfr	if (flags & IP_ROUTETOIF) {
515177633Sdfr		/*
516180025Sdfr		 * if we have tunnel mode SA, we may need to ignore
517180025Sdfr		 * IP_ROUTETOIF.
518184588Sdfr		 */
519184588Sdfr		if (state.ro != &iproute || state.ro->ro_rt != NULL) {
520184588Sdfr			flags &= ~IP_ROUTETOIF;
521184588Sdfr			ro = state.ro;
522184588Sdfr		}
523184588Sdfr	} else
524184588Sdfr		ro = state.ro;
525177633Sdfr	dst = (struct sockaddr_in *)state.dst;
526177633Sdfr	if (error) {
527180025Sdfr		/* mbuf is already reclaimed in ipsec4_output. */
528180025Sdfr		m0 = NULL;
529184588Sdfr		switch (error) {
530184588Sdfr		case EHOSTUNREACH:
531184588Sdfr		case ENETUNREACH:
532184588Sdfr		case EMSGSIZE:
533184588Sdfr		case ENOBUFS:
534177633Sdfr		case ENOMEM:
535177633Sdfr			break;
536177633Sdfr		default:
537177633Sdfr			printf("ip4_output (ipsec): error code %d\n", error);
538177633Sdfr			/*fall through*/
539177633Sdfr		case ENOENT:
540180025Sdfr			/* don't show these error codes to the user */
541180025Sdfr			error = 0;
542184588Sdfr			break;
543184588Sdfr		}
544184588Sdfr		goto bad;
545184588Sdfr	}
546184588Sdfr    }
547184588Sdfr
548177633Sdfr	/* be sure to update variables that are affected by ipsec4_output() */
549177633Sdfr	ip = mtod(m, struct ip *);
550177633Sdfr#ifdef _IP_VHL
551177633Sdfr	hlen = IP_VHL_HL(ip->ip_vhl) << 2;
552177633Sdfr#else
553177633Sdfr	hlen = ip->ip_hl << 2;
554180025Sdfr#endif
555177633Sdfr	if (ro->ro_rt == NULL) {
556180025Sdfr		if ((flags & IP_ROUTETOIF) == 0) {
557180025Sdfr			printf("ip_output: "
558180025Sdfr				"can't update route after IPsec processing\n");
559180025Sdfr			error = EHOSTUNREACH;	/*XXX*/
560180025Sdfr			goto bad;
561195245Srmacklem		}
562180025Sdfr	} else {
563195245Srmacklem		ia = ifatoia(ro->ro_rt->rt_ifa);
564195245Srmacklem		ifp = ro->ro_rt->rt_ifp;
565180025Sdfr	}
566180025Sdfr
567195245Srmacklem	/* make it flipped, again. */
568177633Sdfr	ip->ip_len = ntohs(ip->ip_len);
569180025Sdfr	ip->ip_off = ntohs(ip->ip_off);
570177633Sdfrskip_ipsec:
571177633Sdfr#endif /*IPSEC*/
572180025Sdfr
573184588Sdfr	/*
574184588Sdfr	 * IpHack's section.
575184588Sdfr	 * - Xlate: translate packet's addr/port (NAT).
576184588Sdfr	 * - Firewall: deny/allow/etc.
577184588Sdfr	 * - Wrap: fake packet's addr/port <unimpl.>
578177633Sdfr	 * - Encapsulate: put it in another IP and send out. <unimp.>
579177633Sdfr	 */
580177633Sdfr#ifdef PFIL_HOOKS
581177633Sdfr	/*
582177633Sdfr	 * Run through list of hooks for output packets.
583177633Sdfr	 */
584177633Sdfr	m1 = m;
585180025Sdfr	pfh = pfil_hook_get(PFIL_OUT, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh);
586184588Sdfr	for (; pfh; pfh = TAILQ_NEXT(pfh, pfil_link))
587184588Sdfr		if (pfh->pfil_func) {
588177633Sdfr			rv = pfh->pfil_func(ip, hlen, ifp, 1, &m1);
589177633Sdfr			if (rv) {
590184588Sdfr				error = EHOSTUNREACH;
591184588Sdfr				goto done;
592184588Sdfr			}
593184588Sdfr			m = m1;
594184588Sdfr			if (m == NULL)
595184588Sdfr				goto done;
596184588Sdfr			ip = mtod(m, struct ip *);
597184588Sdfr		}
598184588Sdfr#endif /* PFIL_HOOKS */
599184588Sdfr
600184588Sdfr	/*
601184588Sdfr	 * Check with the firewall...
602184588Sdfr	 * but not if we are already being fwd'd from a firewall.
603184588Sdfr	 */
604184588Sdfr	if (fw_enable && IPFW_LOADED && !args.next_hop) {
605184588Sdfr		struct sockaddr_in *old = dst;
606184588Sdfr
607184588Sdfr		args.m = m;
608184588Sdfr		args.next_hop = dst;
609184588Sdfr		args.oif = ifp;
610184588Sdfr		off = ip_fw_chk_ptr(&args);
611184588Sdfr		m = args.m;
612184588Sdfr		dst = args.next_hop;
613184588Sdfr
614184588Sdfr                /*
615184588Sdfr		 * On return we must do the following:
616184588Sdfr		 * m == NULL	-> drop the pkt (old interface, deprecated)
617184588Sdfr		 * (off & IP_FW_PORT_DENY_FLAG)	-> drop the pkt (new interface)
618184588Sdfr		 * 1<=off<= 0xffff		-> DIVERT
619184588Sdfr		 * (off & IP_FW_PORT_DYNT_FLAG)	-> send to a DUMMYNET pipe
620184588Sdfr		 * (off & IP_FW_PORT_TEE_FLAG)	-> TEE the packet
621184588Sdfr		 * dst != old			-> IPFIREWALL_FORWARD
622184588Sdfr		 * off==0, dst==old		-> accept
623184588Sdfr		 * If some of the above modules are not compiled in, then
624184588Sdfr		 * we should't have to check the corresponding condition
625184588Sdfr		 * (because the ipfw control socket should not accept
626184588Sdfr		 * unsupported rules), but better play safe and drop
627184588Sdfr		 * packets in case of doubt.
628184588Sdfr		 */
629184588Sdfr		if ( (off & IP_FW_PORT_DENY_FLAG) || m == NULL) {
630184588Sdfr			if (m)
631184588Sdfr				m_freem(m);
632177633Sdfr			error = EACCES;
633177633Sdfr			goto done;
634177633Sdfr		}
635177633Sdfr		ip = mtod(m, struct ip *);
636177633Sdfr		if (off == 0 && dst == old)		/* common case */
637177633Sdfr			goto pass;
638177633Sdfr                if (DUMMYNET_LOADED && (off & IP_FW_PORT_DYNT_FLAG) != 0) {
639177633Sdfr			/*
640177633Sdfr			 * pass the pkt to dummynet. Need to include
641184588Sdfr			 * pipe number, m, ifp, ro, dst because these are
642225234Sart			 * not recomputed in the next pass.
643184588Sdfr			 * All other parameters have been already used and
644177633Sdfr			 * so they are not needed anymore.
645184588Sdfr			 * XXX note: if the ifp or ro entry are deleted
646177633Sdfr			 * while a pkt is in dummynet, we are in trouble!
647177633Sdfr			 */
648177633Sdfr			args.ro = ro;
649180025Sdfr			args.dst = dst;
650177633Sdfr			args.flags = flags;
651177633Sdfr
652180025Sdfr			error = ip_dn_io_ptr(m, off & 0xffff, DN_TO_IP_OUT,
653184588Sdfr				&args);
654184588Sdfr			goto done;
655177633Sdfr		}
656177633Sdfr#ifdef IPDIVERT
657177633Sdfr		if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) {
658177633Sdfr			struct mbuf *clone = NULL;
659180025Sdfr
660184588Sdfr			/* Clone packet if we're doing a 'tee' */
661184588Sdfr			if ((off & IP_FW_PORT_TEE_FLAG) != 0)
662184588Sdfr				clone = m_dup(m, M_DONTWAIT);
663180025Sdfr
664180025Sdfr			/*
665180025Sdfr			 * XXX
666180025Sdfr			 * delayed checksums are not currently compatible
667180025Sdfr			 * with divert sockets.
668180025Sdfr			 */
669180025Sdfr			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
670180025Sdfr				in_delayed_cksum(m);
671180025Sdfr				m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
672180025Sdfr			}
673184588Sdfr
674184588Sdfr			/* Restore packet header fields to original values */
675184588Sdfr			ip->ip_len = htons(ip->ip_len);
676180025Sdfr			ip->ip_off = htons(ip->ip_off);
677180025Sdfr
678180025Sdfr			/* Deliver packet to divert input routine */
679177633Sdfr			divert_packet(m, 0, off & 0xffff, args.divert_rule);
680180025Sdfr
681180025Sdfr			/* If 'tee', continue with original packet */
682180025Sdfr			if (clone != NULL) {
683177633Sdfr				m = clone;
684177633Sdfr				ip = mtod(m, struct ip *);
685255284Srmacklem				goto pass;
686180025Sdfr			}
687177633Sdfr			goto done;
688177633Sdfr		}
689177633Sdfr#endif
690177633Sdfr
691177633Sdfr		/* IPFIREWALL_FORWARD */
692177633Sdfr		/*
693177633Sdfr		 * Check dst to make sure it is directly reachable on the
694177633Sdfr		 * interface we previously thought it was.
695177633Sdfr		 * If it isn't (which may be likely in some situations) we have
696180025Sdfr		 * to re-route it (ie, find a route for the next-hop and the
697180025Sdfr		 * associated interface) and set them here. This is nested
698180025Sdfr		 * forwarding which in most cases is undesirable, except where
699180025Sdfr		 * such control is nigh impossible. So we do it here.
700177633Sdfr		 * And I'm babbling.
701180025Sdfr		 */
702177633Sdfr		if (off == 0 && old != dst) { /* FORWARD, dst has changed */
703177633Sdfr#if 0
704177633Sdfr			/*
705184588Sdfr			 * XXX To improve readability, this block should be
706184588Sdfr			 * changed into a function call as below:
707177633Sdfr			 */
708184588Sdfr			error = ip_ipforward(&m, &dst, &ifp);
709177633Sdfr			if (error)
710184588Sdfr				goto bad;
711184588Sdfr			if (m == NULL) /* ip_input consumed the mbuf */
712184588Sdfr				goto done;
713184588Sdfr#else
714184588Sdfr			struct in_ifaddr *ia;
715184588Sdfr
716184588Sdfr			/*
717184588Sdfr			 * XXX sro_fwd below is static, and a pointer
718184588Sdfr			 * to it gets passed to routines downstream.
719184588Sdfr			 * This could have surprisingly bad results in
720184588Sdfr			 * practice, because its content is overwritten
721184588Sdfr			 * by subsequent packets.
722184588Sdfr			 */
723184588Sdfr			/* There must be a better way to do this next line... */
724184588Sdfr			static struct route sro_fwd;
725184588Sdfr			struct route *ro_fwd = &sro_fwd;
726184588Sdfr
727184588Sdfr#if 0
728184588Sdfr			print_ip("IPFIREWALL_FORWARD: New dst ip: ",
729184588Sdfr			    dst->sin_addr, "\n");
730184588Sdfr#endif
731184588Sdfr
732184588Sdfr			/*
733184588Sdfr			 * We need to figure out if we have been forwarded
734184588Sdfr			 * to a local socket. If so, then we should somehow
735184588Sdfr			 * "loop back" to ip_input, and get directed to the
736184588Sdfr			 * PCB as if we had received this packet. This is
737255284Srmacklem			 * because it may be dificult to identify the packets
738184588Sdfr			 * you want to forward until they are being output
739184588Sdfr			 * and have selected an interface. (e.g. locally
740184588Sdfr			 * initiated packets) If we used the loopback inteface,
741184588Sdfr			 * we would not be able to control what happens
742184588Sdfr			 * as the packet runs through ip_input() as
743184588Sdfr			 * it is done through a ISR.
744184588Sdfr			 */
745177633Sdfr			LIST_FOREACH(ia,
746177633Sdfr			    INADDR_HASH(dst->sin_addr.s_addr), ia_hash) {
747177633Sdfr				/*
748177633Sdfr				 * If the addr to forward to is one
749177633Sdfr				 * of ours, we pretend to
750177633Sdfr				 * be the destination for this packet.
751184588Sdfr				 */
752177633Sdfr				if (IA_SIN(ia)->sin_addr.s_addr ==
753177633Sdfr						 dst->sin_addr.s_addr)
754184588Sdfr					break;
755177633Sdfr			}
756184588Sdfr			if (ia) {	/* tell ip_input "dont filter" */
757184588Sdfr				struct m_hdr tag;
758177633Sdfr
759177633Sdfr				tag.mh_type = MT_TAG;
760177633Sdfr				tag.mh_flags = PACKET_TAG_IPFORWARD;
761177633Sdfr				tag.mh_data = (caddr_t)args.next_hop;
762177633Sdfr				tag.mh_next = m;
763184588Sdfr
764177633Sdfr				if (m->m_pkthdr.rcvif == NULL)
765177633Sdfr					m->m_pkthdr.rcvif = ifunit("lo0");
766184588Sdfr				if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
767184588Sdfr					m->m_pkthdr.csum_flags |=
768177633Sdfr					    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
769177633Sdfr					m0->m_pkthdr.csum_data = 0xffff;
770177633Sdfr				}
771177633Sdfr				m->m_pkthdr.csum_flags |=
772177633Sdfr				    CSUM_IP_CHECKED | CSUM_IP_VALID;
773180025Sdfr				ip->ip_len = htons(ip->ip_len);
774180025Sdfr				ip->ip_off = htons(ip->ip_off);
775177633Sdfr				ip_input((struct mbuf *)&tag);
776180025Sdfr				goto done;
777180025Sdfr			}
778180025Sdfr			/* Some of the logic for this was
779180025Sdfr			 * nicked from above.
780177633Sdfr			 *
781180025Sdfr			 * This rewrites the cached route in a local PCB.
782184588Sdfr			 * Is this what we want to do?
783184588Sdfr			 */
784184588Sdfr			bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst));
785180025Sdfr
786180025Sdfr			ro_fwd->ro_rt = 0;
787184588Sdfr			rtalloc_ign(ro_fwd, RTF_PRCLONING);
788177633Sdfr
789177633Sdfr			if (ro_fwd->ro_rt == 0) {
790177633Sdfr				ipstat.ips_noroute++;
791177633Sdfr				error = EHOSTUNREACH;
792177633Sdfr				goto bad;
793177633Sdfr			}
794177633Sdfr
795177633Sdfr			ia = ifatoia(ro_fwd->ro_rt->rt_ifa);
796177633Sdfr			ifp = ro_fwd->ro_rt->rt_ifp;
797177633Sdfr			ro_fwd->ro_rt->rt_use++;
798177633Sdfr			if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
799177633Sdfr				dst = (struct sockaddr_in *)
800177633Sdfr					ro_fwd->ro_rt->rt_gateway;
801177633Sdfr			if (ro_fwd->ro_rt->rt_flags & RTF_HOST)
802177633Sdfr				isbroadcast =
803177633Sdfr				    (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST);
804177633Sdfr			else
805177633Sdfr				isbroadcast = in_broadcast(dst->sin_addr, ifp);
806177633Sdfr			if (ro->ro_rt)
807177633Sdfr				RTFREE(ro->ro_rt);
808177633Sdfr			ro->ro_rt = ro_fwd->ro_rt;
809177633Sdfr			dst = (struct sockaddr_in *)&ro_fwd->ro_dst;
810177633Sdfr
811177633Sdfr#endif	/* ... block to be put into a function */
812177633Sdfr			/*
813177633Sdfr			 * If we added a default src ip earlier,
814177633Sdfr			 * which would have been gotten from the-then
815177633Sdfr			 * interface, do it again, from the new one.
816177633Sdfr			 */
817177633Sdfr			if (src_was_INADDR_ANY)
818177633Sdfr				ip->ip_src = IA_SIN(ia)->sin_addr;
819177633Sdfr			goto pass ;
820193272Sjhb		}
821177633Sdfr
822177633Sdfr                /*
823193272Sjhb                 * if we get here, none of the above matches, and
824177633Sdfr                 * we have to drop the pkt
825177633Sdfr                 */
826177633Sdfr		m_freem(m);
827177633Sdfr                error = EACCES; /* not sure this is the right error msg */
828177633Sdfr                goto done;
829177633Sdfr	}
830177633Sdfr
831177633Sdfrpass:
832177633Sdfr	/* 127/8 must not appear on wire - RFC1122. */
833177633Sdfr	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
834177633Sdfr	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
835177633Sdfr		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
836177633Sdfr			ipstat.ips_badaddr++;
837177633Sdfr			error = EADDRNOTAVAIL;
838177633Sdfr			goto bad;
839177633Sdfr		}
840177633Sdfr	}
841177633Sdfr
842177633Sdfr	m->m_pkthdr.csum_flags |= CSUM_IP;
843177633Sdfr	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
844177633Sdfr	if (sw_csum & CSUM_DELAY_DATA) {
845177633Sdfr		in_delayed_cksum(m);
846177633Sdfr		sw_csum &= ~CSUM_DELAY_DATA;
847177633Sdfr	}
848177633Sdfr	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
849177633Sdfr
850177633Sdfr	/*
851177633Sdfr	 * If small enough for interface, or the interface will take
852177633Sdfr	 * care of the fragmentation for us, can just send directly.
853177633Sdfr	 */
854177633Sdfr	if ((u_short)ip->ip_len <= ifp->if_mtu ||
855177633Sdfr	    ifp->if_hwassist & CSUM_FRAGMENT) {
856177633Sdfr		ip->ip_len = htons(ip->ip_len);
857177633Sdfr		ip->ip_off = htons(ip->ip_off);
858177633Sdfr		ip->ip_sum = 0;
859177633Sdfr		if (sw_csum & CSUM_DELAY_IP) {
860177633Sdfr			if (ip->ip_vhl == IP_VHL_BORING) {
861177633Sdfr				ip->ip_sum = in_cksum_hdr(ip);
862177633Sdfr			} else {
863177633Sdfr				ip->ip_sum = in_cksum(m, hlen);
864177633Sdfr			}
865177633Sdfr		}
866177633Sdfr
867177633Sdfr		/* Record statistics for this interface address. */
868177633Sdfr		if (!(flags & IP_FORWARDING) && ia) {
869177633Sdfr			ia->ia_ifa.if_opackets++;
870177633Sdfr			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
871177633Sdfr		}
872177633Sdfr
873177633Sdfr#ifdef IPSEC
874177633Sdfr		/* clean ipsec history once it goes out of the node */
875177633Sdfr		ipsec_delaux(m);
876177633Sdfr#endif
877177633Sdfr
878177633Sdfr		error = (*ifp->if_output)(ifp, m,
879177633Sdfr				(struct sockaddr *)dst, ro->ro_rt);
880177633Sdfr		goto done;
881177633Sdfr	}
882177633Sdfr	/*
883177633Sdfr	 * Too large for interface; fragment if possible.
884177633Sdfr	 * Must be able to put at least 8 bytes per fragment.
885177633Sdfr	 */
886177633Sdfr	if (ip->ip_off & IP_DF) {
887177633Sdfr		error = EMSGSIZE;
888177633Sdfr		/*
889177633Sdfr		 * This case can happen if the user changed the MTU
890177633Sdfr		 * of an interface after enabling IP on it.  Because
891177633Sdfr		 * most netifs don't keep track of routes pointing to
892177633Sdfr		 * them, there is no way for one to update all its
893177633Sdfr		 * routes when the MTU is changed.
894177633Sdfr		 */
895177633Sdfr		if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST))
896177633Sdfr		    && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU)
897177633Sdfr		    && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
898177633Sdfr			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
899177633Sdfr		}
900177633Sdfr		ipstat.ips_cantfrag++;
901177633Sdfr		goto bad;
902177633Sdfr	}
903177633Sdfr	len = (ifp->if_mtu - hlen) &~ 7;
904177633Sdfr	if (len < 8) {
905177633Sdfr		error = EMSGSIZE;
906177633Sdfr		goto bad;
907177633Sdfr	}
908177633Sdfr
909177633Sdfr	/*
910177633Sdfr	 * if the interface will not calculate checksums on
911177633Sdfr	 * fragmented packets, then do it here.
912177633Sdfr	 */
913177633Sdfr	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
914177633Sdfr	    (ifp->if_hwassist & CSUM_IP_FRAGS) == 0) {
915177633Sdfr		in_delayed_cksum(m);
916177633Sdfr		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
917177633Sdfr	}
918177633Sdfr
919177633Sdfr	if (len > PAGE_SIZE) {
920177633Sdfr		/*
921177633Sdfr		 * Fragement large datagrams such that each segment
922177633Sdfr		 * contains a multiple of PAGE_SIZE amount of data,
923177633Sdfr		 * plus headers. This enables a receiver to perform
924184588Sdfr		 * page-flipping zero-copy optimizations.
925177633Sdfr		 */
926177633Sdfr
927177633Sdfr		int newlen;
928177633Sdfr		struct mbuf *mtmp;
929177633Sdfr
930177633Sdfr		for (mtmp = m, off = 0;
931177633Sdfr		     mtmp && ((off + mtmp->m_len) <= ifp->if_mtu);
932177633Sdfr		     mtmp = mtmp->m_next) {
933177633Sdfr			off += mtmp->m_len;
934177633Sdfr		}
935177633Sdfr		/*
936177633Sdfr		 * firstlen (off - hlen) must be aligned on an
937177633Sdfr		 * 8-byte boundary
938177633Sdfr		 */
939177633Sdfr		if (off < hlen)
940177633Sdfr			goto smart_frag_failure;
941177633Sdfr		off = ((off - hlen) & ~7) + hlen;
942177633Sdfr		newlen = (~PAGE_MASK) & ifp->if_mtu;
943177633Sdfr		if ((newlen + sizeof (struct ip)) > ifp->if_mtu) {
944177633Sdfr			/* we failed, go back the default */
945177633Sdfrsmart_frag_failure:
946177633Sdfr			newlen = len;
947177633Sdfr			off = hlen + len;
948177633Sdfr		}
949177633Sdfr
950184588Sdfr/*		printf("ipfrag: len = %d, hlen = %d, mhlen = %d, newlen = %d, off = %d\n",
951177633Sdfr		len, hlen, sizeof (struct ip), newlen, off);*/
952177633Sdfr
953193272Sjhb		len = newlen;
954180025Sdfr
955177633Sdfr	} else {
956193272Sjhb		off = hlen + len;
957180025Sdfr	}
958177633Sdfr
959184588Sdfr
960184588Sdfr
961184588Sdfr    {
962184588Sdfr	int mhlen, firstlen = off - hlen;
963184588Sdfr	struct mbuf **mnext = &m->m_nextpkt;
964184588Sdfr	int nfrags = 1;
965184588Sdfr
966184588Sdfr	/*
967184588Sdfr	 * Loop through length of segment after first fragment,
968184588Sdfr	 * make new header and copy data of each part and link onto chain.
969184588Sdfr	 */
970184588Sdfr	m0 = m;
971184588Sdfr	mhlen = sizeof (struct ip);
972180025Sdfr	for (; off < (u_short)ip->ip_len; off += len) {
973180025Sdfr		MGETHDR(m, M_DONTWAIT, MT_HEADER);
974180025Sdfr		if (m == 0) {
975180025Sdfr			error = ENOBUFS;
976180025Sdfr			ipstat.ips_odropped++;
977180025Sdfr			goto sendorfree;
978180025Sdfr		}
979180025Sdfr		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
980180025Sdfr		m->m_data += max_linkhdr;
981180025Sdfr		mhip = mtod(m, struct ip *);
982180025Sdfr		*mhip = *ip;
983180025Sdfr		if (hlen > sizeof (struct ip)) {
984180025Sdfr			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
985180025Sdfr			mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
986180025Sdfr		}
987180025Sdfr		m->m_len = mhlen;
988184588Sdfr		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
989184588Sdfr		if (off + len >= (u_short)ip->ip_len)
990184588Sdfr			len = (u_short)ip->ip_len - off;
991184588Sdfr		else
992184588Sdfr			mhip->ip_off |= IP_MF;
993184588Sdfr		mhip->ip_len = htons((u_short)(len + mhlen));
994184588Sdfr		m->m_next = m_copy(m0, off, len);
995184588Sdfr		if (m->m_next == 0) {
996184588Sdfr			(void) m_free(m);
997184588Sdfr			error = ENOBUFS;	/* ??? */
998184588Sdfr			ipstat.ips_odropped++;
999193272Sjhb			goto sendorfree;
1000184588Sdfr		}
1001184588Sdfr		m->m_pkthdr.len = mhlen + len;
1002184588Sdfr		m->m_pkthdr.rcvif = (struct ifnet *)0;
1003193272Sjhb#ifdef MAC
1004184588Sdfr		mac_create_fragment(m0, m);
1005184588Sdfr#endif
1006227059Srmacklem		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
1007184588Sdfr		mhip->ip_off = htons(mhip->ip_off);
1008184588Sdfr		mhip->ip_sum = 0;
1009177633Sdfr		if (sw_csum & CSUM_DELAY_IP) {
1010177633Sdfr			if (mhip->ip_vhl == IP_VHL_BORING) {
1011193437Srmacklem				mhip->ip_sum = in_cksum_hdr(mhip);
1012193272Sjhb			} else {
1013193437Srmacklem				mhip->ip_sum = in_cksum(m, mhlen);
1014177633Sdfr			}
1015193437Srmacklem		}
1016177633Sdfr		*mnext = m;
1017177633Sdfr		mnext = &m->m_nextpkt;
1018177633Sdfr		nfrags++;
1019177633Sdfr	}
1020227059Srmacklem	ipstat.ips_ofragments += nfrags;
1021177633Sdfr
1022177633Sdfr	/* set first/last markers for fragment chain */
1023177633Sdfr	m->m_flags |= M_LASTFRAG;
1024180025Sdfr	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
1025177633Sdfr	m0->m_pkthdr.csum_data = nfrags;
1026177633Sdfr
1027177633Sdfr	/*
1028177633Sdfr	 * Update first fragment by trimming what's been copied out
1029177633Sdfr	 * and updating header, then send each fragment (in order).
1030177633Sdfr	 */
1031177633Sdfr	m = m0;
1032177633Sdfr	m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
1033177633Sdfr	m->m_pkthdr.len = hlen + firstlen;
1034177633Sdfr	ip->ip_len = htons((u_short)m->m_pkthdr.len);
1035177633Sdfr	ip->ip_off |= IP_MF;
1036177633Sdfr	ip->ip_off = htons(ip->ip_off);
1037177633Sdfr	ip->ip_sum = 0;
1038177633Sdfr	if (sw_csum & CSUM_DELAY_IP) {
1039177633Sdfr		if (ip->ip_vhl == IP_VHL_BORING) {
1040177633Sdfr			ip->ip_sum = in_cksum_hdr(ip);
1041177633Sdfr		} else {
1042177633Sdfr			ip->ip_sum = in_cksum(m, hlen);
1043177633Sdfr		}
1044177633Sdfr	}
1045177633Sdfrsendorfree:
1046177633Sdfr	for (m = m0; m; m = m0) {
1047177633Sdfr		m0 = m->m_nextpkt;
1048177633Sdfr		m->m_nextpkt = 0;
1049177633Sdfr#ifdef IPSEC
1050193272Sjhb		/* clean ipsec history once it goes out of the node */
1051177633Sdfr		ipsec_delaux(m);
1052177633Sdfr#endif
1053177633Sdfr		if (error == 0) {
1054177633Sdfr			/* Record statistics for this interface address. */
1055177633Sdfr			if (ia != NULL) {
1056177633Sdfr				ia->ia_ifa.if_opackets++;
1057177633Sdfr				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
1058177633Sdfr			}
1059177633Sdfr
1060177633Sdfr			error = (*ifp->if_output)(ifp, m,
1061193437Srmacklem			    (struct sockaddr *)dst, ro->ro_rt);
1062177633Sdfr		} else
1063177633Sdfr			m_freem(m);
1064177633Sdfr	}
1065193272Sjhb
1066177633Sdfr	if (error == 0)
1067177633Sdfr		ipstat.ips_fragmented++;
1068177633Sdfr    }
1069177633Sdfrdone:
1070177633Sdfr#ifdef IPSEC
1071177633Sdfr	if (ro == &iproute && ro->ro_rt) {
1072193272Sjhb		RTFREE(ro->ro_rt);
1073177633Sdfr		ro->ro_rt = NULL;
1074177633Sdfr	}
1075177633Sdfr	if (sp != NULL) {
1076177633Sdfr		KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
1077177633Sdfr			printf("DP ip_output call free SP:%p\n", sp));
1078177633Sdfr		key_freesp(sp);
1079177633Sdfr	}
1080177633Sdfr#endif /* IPSEC */
1081177633Sdfr	return (error);
1082177633Sdfrbad:
1083177633Sdfr	m_freem(m);
1084180025Sdfr	goto done;
1085177633Sdfr}
1086177633Sdfr
1087177633Sdfrvoid
1088177633Sdfrin_delayed_cksum(struct mbuf *m)
1089177633Sdfr{
1090177633Sdfr	struct ip *ip;
1091177633Sdfr	u_short csum, offset;
1092177633Sdfr
1093177633Sdfr	ip = mtod(m, struct ip *);
1094177633Sdfr	offset = IP_VHL_HL(ip->ip_vhl) << 2 ;
1095220585Srmacklem	csum = in_cksum_skip(m, ip->ip_len, offset);
1096180025Sdfr	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
1097180025Sdfr		csum = 0xffff;
1098180025Sdfr	offset += m->m_pkthdr.csum_data;	/* checksum offset */
1099220585Srmacklem
1100180025Sdfr	if (offset + sizeof(u_short) > m->m_len) {
1101220585Srmacklem		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
1102180025Sdfr		    m->m_len, offset, ip->ip_p);
1103217242Srmacklem		/*
1104217242Srmacklem		 * XXX
1105177633Sdfr		 * this shouldn't happen, but if it does, the
1106177633Sdfr		 * correct behavior may be to insert the checksum
1107177633Sdfr		 * in the existing chain instead of rearranging it.
1108177633Sdfr		 */
1109177633Sdfr		m = m_pullup(m, offset + sizeof(u_short));
1110177633Sdfr	}
1111177633Sdfr	*(u_short *)(m->m_data + offset) = csum;
1112177633Sdfr}
1113177633Sdfr
1114180025Sdfr/*
1115177633Sdfr * Insert IP options into preformed packet.
1116180025Sdfr * Adjust IP destination as required for IP source routing,
1117180025Sdfr * as indicated by a non-zero in_addr at the start of the options.
1118180025Sdfr *
1119180025Sdfr * XXX This routine assumes that the packet has no options in place.
1120177633Sdfr */
1121177633Sdfrstatic struct mbuf *
1122177633Sdfrip_insertoptions(m, opt, phlen)
1123177633Sdfr	register struct mbuf *m;
1124177633Sdfr	struct mbuf *opt;
1125177633Sdfr	int *phlen;
1126177633Sdfr{
1127177633Sdfr	register struct ipoption *p = mtod(opt, struct ipoption *);
1128177633Sdfr	struct mbuf *n;
1129177633Sdfr	register struct ip *ip = mtod(m, struct ip *);
1130177633Sdfr	unsigned optlen;
1131177633Sdfr
1132177633Sdfr	optlen = opt->m_len - sizeof(p->ipopt_dst);
1133177633Sdfr	if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) {
1134177633Sdfr		*phlen = 0;
1135177633Sdfr		return (m);		/* XXX should fail */
1136177633Sdfr	}
1137177633Sdfr	if (p->ipopt_dst.s_addr)
1138193437Srmacklem		ip->ip_dst = p->ipopt_dst;
1139193437Srmacklem	if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
1140193437Srmacklem		MGETHDR(n, M_DONTWAIT, MT_HEADER);
1141193437Srmacklem		if (n == 0) {
1142193437Srmacklem			*phlen = 0;
1143193272Sjhb			return (m);
1144177633Sdfr		}
1145177633Sdfr		n->m_pkthdr.rcvif = (struct ifnet *)0;
1146193437Srmacklem#ifdef MAC
1147193437Srmacklem		mac_create_mbuf_from_mbuf(m, n);
1148193437Srmacklem#endif
1149193437Srmacklem		n->m_pkthdr.len = m->m_pkthdr.len + optlen;
1150193437Srmacklem		m->m_len -= sizeof(struct ip);
1151193437Srmacklem		m->m_data += sizeof(struct ip);
1152193437Srmacklem		n->m_next = m;
1153193437Srmacklem		m = n;
1154193437Srmacklem		m->m_len = optlen + sizeof(struct ip);
1155193437Srmacklem		m->m_data += max_linkhdr;
1156193437Srmacklem		(void)memcpy(mtod(m, void *), ip, sizeof(struct ip));
1157193437Srmacklem	} else {
1158193437Srmacklem		m->m_data -= optlen;
1159		m->m_len += optlen;
1160		m->m_pkthdr.len += optlen;
1161		ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
1162	}
1163	ip = mtod(m, struct ip *);
1164	bcopy(p->ipopt_list, ip + 1, optlen);
1165	*phlen = sizeof(struct ip) + optlen;
1166	ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
1167	ip->ip_len += optlen;
1168	return (m);
1169}
1170
1171/*
1172 * Copy options from ip to jp,
1173 * omitting those not copied during fragmentation.
1174 */
1175int
1176ip_optcopy(ip, jp)
1177	struct ip *ip, *jp;
1178{
1179	register u_char *cp, *dp;
1180	int opt, optlen, cnt;
1181
1182	cp = (u_char *)(ip + 1);
1183	dp = (u_char *)(jp + 1);
1184	cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
1185	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1186		opt = cp[0];
1187		if (opt == IPOPT_EOL)
1188			break;
1189		if (opt == IPOPT_NOP) {
1190			/* Preserve for IP mcast tunnel's LSRR alignment. */
1191			*dp++ = IPOPT_NOP;
1192			optlen = 1;
1193			continue;
1194		}
1195
1196		KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp),
1197		    ("ip_optcopy: malformed ipv4 option"));
1198		optlen = cp[IPOPT_OLEN];
1199		KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt,
1200		    ("ip_optcopy: malformed ipv4 option"));
1201
1202		/* bogus lengths should have been caught by ip_dooptions */
1203		if (optlen > cnt)
1204			optlen = cnt;
1205		if (IPOPT_COPIED(opt)) {
1206			bcopy(cp, dp, optlen);
1207			dp += optlen;
1208		}
1209	}
1210	for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
1211		*dp++ = IPOPT_EOL;
1212	return (optlen);
1213}
1214
1215/*
1216 * IP socket option processing.
1217 */
1218int
1219ip_ctloutput(so, sopt)
1220	struct socket *so;
1221	struct sockopt *sopt;
1222{
1223	struct	inpcb *inp = sotoinpcb(so);
1224	int	error, optval;
1225
1226	error = optval = 0;
1227	if (sopt->sopt_level != IPPROTO_IP) {
1228		return (EINVAL);
1229	}
1230
1231	switch (sopt->sopt_dir) {
1232	case SOPT_SET:
1233		switch (sopt->sopt_name) {
1234		case IP_OPTIONS:
1235#ifdef notyet
1236		case IP_RETOPTS:
1237#endif
1238		{
1239			struct mbuf *m;
1240			if (sopt->sopt_valsize > MLEN) {
1241				error = EMSGSIZE;
1242				break;
1243			}
1244			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER);
1245			if (m == 0) {
1246				error = ENOBUFS;
1247				break;
1248			}
1249			m->m_len = sopt->sopt_valsize;
1250			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
1251					    m->m_len);
1252
1253			return (ip_pcbopts(sopt->sopt_name, &inp->inp_options,
1254					   m));
1255		}
1256
1257		case IP_TOS:
1258		case IP_TTL:
1259		case IP_RECVOPTS:
1260		case IP_RECVRETOPTS:
1261		case IP_RECVDSTADDR:
1262		case IP_RECVIF:
1263		case IP_FAITH:
1264			error = sooptcopyin(sopt, &optval, sizeof optval,
1265					    sizeof optval);
1266			if (error)
1267				break;
1268
1269			switch (sopt->sopt_name) {
1270			case IP_TOS:
1271				inp->inp_ip_tos = optval;
1272				break;
1273
1274			case IP_TTL:
1275				inp->inp_ip_ttl = optval;
1276				break;
1277#define	OPTSET(bit) \
1278	if (optval) \
1279		inp->inp_flags |= bit; \
1280	else \
1281		inp->inp_flags &= ~bit;
1282
1283			case IP_RECVOPTS:
1284				OPTSET(INP_RECVOPTS);
1285				break;
1286
1287			case IP_RECVRETOPTS:
1288				OPTSET(INP_RECVRETOPTS);
1289				break;
1290
1291			case IP_RECVDSTADDR:
1292				OPTSET(INP_RECVDSTADDR);
1293				break;
1294
1295			case IP_RECVIF:
1296				OPTSET(INP_RECVIF);
1297				break;
1298
1299			case IP_FAITH:
1300				OPTSET(INP_FAITH);
1301				break;
1302			}
1303			break;
1304#undef OPTSET
1305
1306		case IP_MULTICAST_IF:
1307		case IP_MULTICAST_VIF:
1308		case IP_MULTICAST_TTL:
1309		case IP_MULTICAST_LOOP:
1310		case IP_ADD_MEMBERSHIP:
1311		case IP_DROP_MEMBERSHIP:
1312			error = ip_setmoptions(sopt, &inp->inp_moptions);
1313			break;
1314
1315		case IP_PORTRANGE:
1316			error = sooptcopyin(sopt, &optval, sizeof optval,
1317					    sizeof optval);
1318			if (error)
1319				break;
1320
1321			switch (optval) {
1322			case IP_PORTRANGE_DEFAULT:
1323				inp->inp_flags &= ~(INP_LOWPORT);
1324				inp->inp_flags &= ~(INP_HIGHPORT);
1325				break;
1326
1327			case IP_PORTRANGE_HIGH:
1328				inp->inp_flags &= ~(INP_LOWPORT);
1329				inp->inp_flags |= INP_HIGHPORT;
1330				break;
1331
1332			case IP_PORTRANGE_LOW:
1333				inp->inp_flags &= ~(INP_HIGHPORT);
1334				inp->inp_flags |= INP_LOWPORT;
1335				break;
1336
1337			default:
1338				error = EINVAL;
1339				break;
1340			}
1341			break;
1342
1343#ifdef IPSEC
1344		case IP_IPSEC_POLICY:
1345		{
1346			caddr_t req;
1347			size_t len = 0;
1348			int priv;
1349			struct mbuf *m;
1350			int optname;
1351
1352			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1353				break;
1354			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1355				break;
1356			priv = (sopt->sopt_td != NULL &&
1357				suser(sopt->sopt_td) != 0) ? 0 : 1;
1358			req = mtod(m, caddr_t);
1359			len = m->m_len;
1360			optname = sopt->sopt_name;
1361			error = ipsec4_set_policy(inp, optname, req, len, priv);
1362			m_freem(m);
1363			break;
1364		}
1365#endif /*IPSEC*/
1366
1367		default:
1368			error = ENOPROTOOPT;
1369			break;
1370		}
1371		break;
1372
1373	case SOPT_GET:
1374		switch (sopt->sopt_name) {
1375		case IP_OPTIONS:
1376		case IP_RETOPTS:
1377			if (inp->inp_options)
1378				error = sooptcopyout(sopt,
1379						     mtod(inp->inp_options,
1380							  char *),
1381						     inp->inp_options->m_len);
1382			else
1383				sopt->sopt_valsize = 0;
1384			break;
1385
1386		case IP_TOS:
1387		case IP_TTL:
1388		case IP_RECVOPTS:
1389		case IP_RECVRETOPTS:
1390		case IP_RECVDSTADDR:
1391		case IP_RECVIF:
1392		case IP_PORTRANGE:
1393		case IP_FAITH:
1394			switch (sopt->sopt_name) {
1395
1396			case IP_TOS:
1397				optval = inp->inp_ip_tos;
1398				break;
1399
1400			case IP_TTL:
1401				optval = inp->inp_ip_ttl;
1402				break;
1403
1404#define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1405
1406			case IP_RECVOPTS:
1407				optval = OPTBIT(INP_RECVOPTS);
1408				break;
1409
1410			case IP_RECVRETOPTS:
1411				optval = OPTBIT(INP_RECVRETOPTS);
1412				break;
1413
1414			case IP_RECVDSTADDR:
1415				optval = OPTBIT(INP_RECVDSTADDR);
1416				break;
1417
1418			case IP_RECVIF:
1419				optval = OPTBIT(INP_RECVIF);
1420				break;
1421
1422			case IP_PORTRANGE:
1423				if (inp->inp_flags & INP_HIGHPORT)
1424					optval = IP_PORTRANGE_HIGH;
1425				else if (inp->inp_flags & INP_LOWPORT)
1426					optval = IP_PORTRANGE_LOW;
1427				else
1428					optval = 0;
1429				break;
1430
1431			case IP_FAITH:
1432				optval = OPTBIT(INP_FAITH);
1433				break;
1434			}
1435			error = sooptcopyout(sopt, &optval, sizeof optval);
1436			break;
1437
1438		case IP_MULTICAST_IF:
1439		case IP_MULTICAST_VIF:
1440		case IP_MULTICAST_TTL:
1441		case IP_MULTICAST_LOOP:
1442		case IP_ADD_MEMBERSHIP:
1443		case IP_DROP_MEMBERSHIP:
1444			error = ip_getmoptions(sopt, inp->inp_moptions);
1445			break;
1446
1447#ifdef IPSEC
1448		case IP_IPSEC_POLICY:
1449		{
1450			struct mbuf *m = NULL;
1451			caddr_t req = NULL;
1452			size_t len = 0;
1453
1454			if (m != 0) {
1455				req = mtod(m, caddr_t);
1456				len = m->m_len;
1457			}
1458			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
1459			if (error == 0)
1460				error = soopt_mcopyout(sopt, m); /* XXX */
1461			if (error == 0)
1462				m_freem(m);
1463			break;
1464		}
1465#endif /*IPSEC*/
1466
1467		default:
1468			error = ENOPROTOOPT;
1469			break;
1470		}
1471		break;
1472	}
1473	return (error);
1474}
1475
1476/*
1477 * Set up IP options in pcb for insertion in output packets.
1478 * Store in mbuf with pointer in pcbopt, adding pseudo-option
1479 * with destination address if source routed.
1480 */
1481static int
1482ip_pcbopts(optname, pcbopt, m)
1483	int optname;
1484	struct mbuf **pcbopt;
1485	register struct mbuf *m;
1486{
1487	register int cnt, optlen;
1488	register u_char *cp;
1489	u_char opt;
1490
1491	/* turn off any old options */
1492	if (*pcbopt)
1493		(void)m_free(*pcbopt);
1494	*pcbopt = 0;
1495	if (m == (struct mbuf *)0 || m->m_len == 0) {
1496		/*
1497		 * Only turning off any previous options.
1498		 */
1499		if (m)
1500			(void)m_free(m);
1501		return (0);
1502	}
1503
1504	if (m->m_len % sizeof(int32_t))
1505		goto bad;
1506	/*
1507	 * IP first-hop destination address will be stored before
1508	 * actual options; move other options back
1509	 * and clear it when none present.
1510	 */
1511	if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN])
1512		goto bad;
1513	cnt = m->m_len;
1514	m->m_len += sizeof(struct in_addr);
1515	cp = mtod(m, u_char *) + sizeof(struct in_addr);
1516	ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
1517	bzero(mtod(m, caddr_t), sizeof(struct in_addr));
1518
1519	for (; cnt > 0; cnt -= optlen, cp += optlen) {
1520		opt = cp[IPOPT_OPTVAL];
1521		if (opt == IPOPT_EOL)
1522			break;
1523		if (opt == IPOPT_NOP)
1524			optlen = 1;
1525		else {
1526			if (cnt < IPOPT_OLEN + sizeof(*cp))
1527				goto bad;
1528			optlen = cp[IPOPT_OLEN];
1529			if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt)
1530				goto bad;
1531		}
1532		switch (opt) {
1533
1534		default:
1535			break;
1536
1537		case IPOPT_LSRR:
1538		case IPOPT_SSRR:
1539			/*
1540			 * user process specifies route as:
1541			 *	->A->B->C->D
1542			 * D must be our final destination (but we can't
1543			 * check that since we may not have connected yet).
1544			 * A is first hop destination, which doesn't appear in
1545			 * actual IP option, but is stored before the options.
1546			 */
1547			if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr))
1548				goto bad;
1549			m->m_len -= sizeof(struct in_addr);
1550			cnt -= sizeof(struct in_addr);
1551			optlen -= sizeof(struct in_addr);
1552			cp[IPOPT_OLEN] = optlen;
1553			/*
1554			 * Move first hop before start of options.
1555			 */
1556			bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
1557			    sizeof(struct in_addr));
1558			/*
1559			 * Then copy rest of options back
1560			 * to close up the deleted entry.
1561			 */
1562			ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
1563			    sizeof(struct in_addr)),
1564			    (caddr_t)&cp[IPOPT_OFFSET+1],
1565			    (unsigned)cnt + sizeof(struct in_addr));
1566			break;
1567		}
1568	}
1569	if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr))
1570		goto bad;
1571	*pcbopt = m;
1572	return (0);
1573
1574bad:
1575	(void)m_free(m);
1576	return (EINVAL);
1577}
1578
1579/*
1580 * XXX
1581 * The whole multicast option thing needs to be re-thought.
1582 * Several of these options are equally applicable to non-multicast
1583 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a
1584 * standard option (IP_TTL).
1585 */
1586
1587/*
1588 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index.
1589 */
1590static struct ifnet *
1591ip_multicast_if(a, ifindexp)
1592	struct in_addr *a;
1593	int *ifindexp;
1594{
1595	int ifindex;
1596	struct ifnet *ifp;
1597
1598	if (ifindexp)
1599		*ifindexp = 0;
1600	if (ntohl(a->s_addr) >> 24 == 0) {
1601		ifindex = ntohl(a->s_addr) & 0xffffff;
1602		if (ifindex < 0 || if_index < ifindex)
1603			return NULL;
1604		ifp = ifnet_byindex(ifindex);
1605		if (ifindexp)
1606			*ifindexp = ifindex;
1607	} else {
1608		INADDR_TO_IFP(*a, ifp);
1609	}
1610	return ifp;
1611}
1612
1613/*
1614 * Set the IP multicast options in response to user setsockopt().
1615 */
1616static int
1617ip_setmoptions(sopt, imop)
1618	struct sockopt *sopt;
1619	struct ip_moptions **imop;
1620{
1621	int error = 0;
1622	int i;
1623	struct in_addr addr;
1624	struct ip_mreq mreq;
1625	struct ifnet *ifp;
1626	struct ip_moptions *imo = *imop;
1627	struct route ro;
1628	struct sockaddr_in *dst;
1629	int ifindex;
1630	int s;
1631
1632	if (imo == NULL) {
1633		/*
1634		 * No multicast option buffer attached to the pcb;
1635		 * allocate one and initialize to default values.
1636		 */
1637		imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS,
1638		    M_WAITOK);
1639
1640		if (imo == NULL)
1641			return (ENOBUFS);
1642		*imop = imo;
1643		imo->imo_multicast_ifp = NULL;
1644		imo->imo_multicast_addr.s_addr = INADDR_ANY;
1645		imo->imo_multicast_vif = -1;
1646		imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1647		imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
1648		imo->imo_num_memberships = 0;
1649	}
1650
1651	switch (sopt->sopt_name) {
1652	/* store an index number for the vif you wanna use in the send */
1653	case IP_MULTICAST_VIF:
1654		if (legal_vif_num == 0) {
1655			error = EOPNOTSUPP;
1656			break;
1657		}
1658		error = sooptcopyin(sopt, &i, sizeof i, sizeof i);
1659		if (error)
1660			break;
1661		if (!legal_vif_num(i) && (i != -1)) {
1662			error = EINVAL;
1663			break;
1664		}
1665		imo->imo_multicast_vif = i;
1666		break;
1667
1668	case IP_MULTICAST_IF:
1669		/*
1670		 * Select the interface for outgoing multicast packets.
1671		 */
1672		error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr);
1673		if (error)
1674			break;
1675		/*
1676		 * INADDR_ANY is used to remove a previous selection.
1677		 * When no interface is selected, a default one is
1678		 * chosen every time a multicast packet is sent.
1679		 */
1680		if (addr.s_addr == INADDR_ANY) {
1681			imo->imo_multicast_ifp = NULL;
1682			break;
1683		}
1684		/*
1685		 * The selected interface is identified by its local
1686		 * IP address.  Find the interface and confirm that
1687		 * it supports multicasting.
1688		 */
1689		s = splimp();
1690		ifp = ip_multicast_if(&addr, &ifindex);
1691		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1692			splx(s);
1693			error = EADDRNOTAVAIL;
1694			break;
1695		}
1696		imo->imo_multicast_ifp = ifp;
1697		if (ifindex)
1698			imo->imo_multicast_addr = addr;
1699		else
1700			imo->imo_multicast_addr.s_addr = INADDR_ANY;
1701		splx(s);
1702		break;
1703
1704	case IP_MULTICAST_TTL:
1705		/*
1706		 * Set the IP time-to-live for outgoing multicast packets.
1707		 * The original multicast API required a char argument,
1708		 * which is inconsistent with the rest of the socket API.
1709		 * We allow either a char or an int.
1710		 */
1711		if (sopt->sopt_valsize == 1) {
1712			u_char ttl;
1713			error = sooptcopyin(sopt, &ttl, 1, 1);
1714			if (error)
1715				break;
1716			imo->imo_multicast_ttl = ttl;
1717		} else {
1718			u_int ttl;
1719			error = sooptcopyin(sopt, &ttl, sizeof ttl,
1720					    sizeof ttl);
1721			if (error)
1722				break;
1723			if (ttl > 255)
1724				error = EINVAL;
1725			else
1726				imo->imo_multicast_ttl = ttl;
1727		}
1728		break;
1729
1730	case IP_MULTICAST_LOOP:
1731		/*
1732		 * Set the loopback flag for outgoing multicast packets.
1733		 * Must be zero or one.  The original multicast API required a
1734		 * char argument, which is inconsistent with the rest
1735		 * of the socket API.  We allow either a char or an int.
1736		 */
1737		if (sopt->sopt_valsize == 1) {
1738			u_char loop;
1739			error = sooptcopyin(sopt, &loop, 1, 1);
1740			if (error)
1741				break;
1742			imo->imo_multicast_loop = !!loop;
1743		} else {
1744			u_int loop;
1745			error = sooptcopyin(sopt, &loop, sizeof loop,
1746					    sizeof loop);
1747			if (error)
1748				break;
1749			imo->imo_multicast_loop = !!loop;
1750		}
1751		break;
1752
1753	case IP_ADD_MEMBERSHIP:
1754		/*
1755		 * Add a multicast group membership.
1756		 * Group must be a valid IP multicast address.
1757		 */
1758		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1759		if (error)
1760			break;
1761
1762		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1763			error = EINVAL;
1764			break;
1765		}
1766		s = splimp();
1767		/*
1768		 * If no interface address was provided, use the interface of
1769		 * the route to the given multicast address.
1770		 */
1771		if (mreq.imr_interface.s_addr == INADDR_ANY) {
1772			bzero((caddr_t)&ro, sizeof(ro));
1773			dst = (struct sockaddr_in *)&ro.ro_dst;
1774			dst->sin_len = sizeof(*dst);
1775			dst->sin_family = AF_INET;
1776			dst->sin_addr = mreq.imr_multiaddr;
1777			rtalloc(&ro);
1778			if (ro.ro_rt == NULL) {
1779				error = EADDRNOTAVAIL;
1780				splx(s);
1781				break;
1782			}
1783			ifp = ro.ro_rt->rt_ifp;
1784			rtfree(ro.ro_rt);
1785		}
1786		else {
1787			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1788		}
1789
1790		/*
1791		 * See if we found an interface, and confirm that it
1792		 * supports multicast.
1793		 */
1794		if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) {
1795			error = EADDRNOTAVAIL;
1796			splx(s);
1797			break;
1798		}
1799		/*
1800		 * See if the membership already exists or if all the
1801		 * membership slots are full.
1802		 */
1803		for (i = 0; i < imo->imo_num_memberships; ++i) {
1804			if (imo->imo_membership[i]->inm_ifp == ifp &&
1805			    imo->imo_membership[i]->inm_addr.s_addr
1806						== mreq.imr_multiaddr.s_addr)
1807				break;
1808		}
1809		if (i < imo->imo_num_memberships) {
1810			error = EADDRINUSE;
1811			splx(s);
1812			break;
1813		}
1814		if (i == IP_MAX_MEMBERSHIPS) {
1815			error = ETOOMANYREFS;
1816			splx(s);
1817			break;
1818		}
1819		/*
1820		 * Everything looks good; add a new record to the multicast
1821		 * address list for the given interface.
1822		 */
1823		if ((imo->imo_membership[i] =
1824		    in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) {
1825			error = ENOBUFS;
1826			splx(s);
1827			break;
1828		}
1829		++imo->imo_num_memberships;
1830		splx(s);
1831		break;
1832
1833	case IP_DROP_MEMBERSHIP:
1834		/*
1835		 * Drop a multicast group membership.
1836		 * Group must be a valid IP multicast address.
1837		 */
1838		error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq);
1839		if (error)
1840			break;
1841
1842		if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) {
1843			error = EINVAL;
1844			break;
1845		}
1846
1847		s = splimp();
1848		/*
1849		 * If an interface address was specified, get a pointer
1850		 * to its ifnet structure.
1851		 */
1852		if (mreq.imr_interface.s_addr == INADDR_ANY)
1853			ifp = NULL;
1854		else {
1855			ifp = ip_multicast_if(&mreq.imr_interface, NULL);
1856			if (ifp == NULL) {
1857				error = EADDRNOTAVAIL;
1858				splx(s);
1859				break;
1860			}
1861		}
1862		/*
1863		 * Find the membership in the membership array.
1864		 */
1865		for (i = 0; i < imo->imo_num_memberships; ++i) {
1866			if ((ifp == NULL ||
1867			     imo->imo_membership[i]->inm_ifp == ifp) &&
1868			     imo->imo_membership[i]->inm_addr.s_addr ==
1869			     mreq.imr_multiaddr.s_addr)
1870				break;
1871		}
1872		if (i == imo->imo_num_memberships) {
1873			error = EADDRNOTAVAIL;
1874			splx(s);
1875			break;
1876		}
1877		/*
1878		 * Give up the multicast address record to which the
1879		 * membership points.
1880		 */
1881		in_delmulti(imo->imo_membership[i]);
1882		/*
1883		 * Remove the gap in the membership array.
1884		 */
1885		for (++i; i < imo->imo_num_memberships; ++i)
1886			imo->imo_membership[i-1] = imo->imo_membership[i];
1887		--imo->imo_num_memberships;
1888		splx(s);
1889		break;
1890
1891	default:
1892		error = EOPNOTSUPP;
1893		break;
1894	}
1895
1896	/*
1897	 * If all options have default values, no need to keep the mbuf.
1898	 */
1899	if (imo->imo_multicast_ifp == NULL &&
1900	    imo->imo_multicast_vif == -1 &&
1901	    imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL &&
1902	    imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP &&
1903	    imo->imo_num_memberships == 0) {
1904		free(*imop, M_IPMOPTS);
1905		*imop = NULL;
1906	}
1907
1908	return (error);
1909}
1910
1911/*
1912 * Return the IP multicast options in response to user getsockopt().
1913 */
1914static int
1915ip_getmoptions(sopt, imo)
1916	struct sockopt *sopt;
1917	register struct ip_moptions *imo;
1918{
1919	struct in_addr addr;
1920	struct in_ifaddr *ia;
1921	int error, optval;
1922	u_char coptval;
1923
1924	error = 0;
1925	switch (sopt->sopt_name) {
1926	case IP_MULTICAST_VIF:
1927		if (imo != NULL)
1928			optval = imo->imo_multicast_vif;
1929		else
1930			optval = -1;
1931		error = sooptcopyout(sopt, &optval, sizeof optval);
1932		break;
1933
1934	case IP_MULTICAST_IF:
1935		if (imo == NULL || imo->imo_multicast_ifp == NULL)
1936			addr.s_addr = INADDR_ANY;
1937		else if (imo->imo_multicast_addr.s_addr) {
1938			/* return the value user has set */
1939			addr = imo->imo_multicast_addr;
1940		} else {
1941			IFP_TO_IA(imo->imo_multicast_ifp, ia);
1942			addr.s_addr = (ia == NULL) ? INADDR_ANY
1943				: IA_SIN(ia)->sin_addr.s_addr;
1944		}
1945		error = sooptcopyout(sopt, &addr, sizeof addr);
1946		break;
1947
1948	case IP_MULTICAST_TTL:
1949		if (imo == 0)
1950			optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1951		else
1952			optval = coptval = imo->imo_multicast_ttl;
1953		if (sopt->sopt_valsize == 1)
1954			error = sooptcopyout(sopt, &coptval, 1);
1955		else
1956			error = sooptcopyout(sopt, &optval, sizeof optval);
1957		break;
1958
1959	case IP_MULTICAST_LOOP:
1960		if (imo == 0)
1961			optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1962		else
1963			optval = coptval = imo->imo_multicast_loop;
1964		if (sopt->sopt_valsize == 1)
1965			error = sooptcopyout(sopt, &coptval, 1);
1966		else
1967			error = sooptcopyout(sopt, &optval, sizeof optval);
1968		break;
1969
1970	default:
1971		error = ENOPROTOOPT;
1972		break;
1973	}
1974	return (error);
1975}
1976
1977/*
1978 * Discard the IP multicast options.
1979 */
1980void
1981ip_freemoptions(imo)
1982	register struct ip_moptions *imo;
1983{
1984	register int i;
1985
1986	if (imo != NULL) {
1987		for (i = 0; i < imo->imo_num_memberships; ++i)
1988			in_delmulti(imo->imo_membership[i]);
1989		free(imo, M_IPMOPTS);
1990	}
1991}
1992
1993/*
1994 * Routine called from ip_output() to loop back a copy of an IP multicast
1995 * packet to the input queue of a specified interface.  Note that this
1996 * calls the output routine of the loopback "driver", but with an interface
1997 * pointer that might NOT be a loopback interface -- evil, but easier than
1998 * replicating that code here.
1999 */
2000static void
2001ip_mloopback(ifp, m, dst, hlen)
2002	struct ifnet *ifp;
2003	register struct mbuf *m;
2004	register struct sockaddr_in *dst;
2005	int hlen;
2006{
2007	register struct ip *ip;
2008	struct mbuf *copym;
2009
2010	copym = m_copy(m, 0, M_COPYALL);
2011	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
2012		copym = m_pullup(copym, hlen);
2013	if (copym != NULL) {
2014		/*
2015		 * We don't bother to fragment if the IP length is greater
2016		 * than the interface's MTU.  Can this possibly matter?
2017		 */
2018		ip = mtod(copym, struct ip *);
2019		ip->ip_len = htons(ip->ip_len);
2020		ip->ip_off = htons(ip->ip_off);
2021		ip->ip_sum = 0;
2022		if (ip->ip_vhl == IP_VHL_BORING) {
2023			ip->ip_sum = in_cksum_hdr(ip);
2024		} else {
2025			ip->ip_sum = in_cksum(copym, hlen);
2026		}
2027		/*
2028		 * NB:
2029		 * It's not clear whether there are any lingering
2030		 * reentrancy problems in other areas which might
2031		 * be exposed by using ip_input directly (in
2032		 * particular, everything which modifies the packet
2033		 * in-place).  Yet another option is using the
2034		 * protosw directly to deliver the looped back
2035		 * packet.  For the moment, we'll err on the side
2036		 * of safety by using if_simloop().
2037		 */
2038#if 1 /* XXX */
2039		if (dst->sin_family != AF_INET) {
2040			printf("ip_mloopback: bad address family %d\n",
2041						dst->sin_family);
2042			dst->sin_family = AF_INET;
2043		}
2044#endif
2045
2046#ifdef notdef
2047		copym->m_pkthdr.rcvif = ifp;
2048		ip_input(copym);
2049#else
2050		/* if the checksum hasn't been computed, mark it as valid */
2051		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2052			copym->m_pkthdr.csum_flags |=
2053			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2054			copym->m_pkthdr.csum_data = 0xffff;
2055		}
2056		if_simloop(ifp, copym, dst->sin_family, 0);
2057#endif
2058	}
2059}
2060