ip_output.c revision 170613
1/*-
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)ip_output.c	8.3 (Berkeley) 1/21/94
30 * $FreeBSD: head/sys/netinet/ip_output.c 170613 2007-06-12 16:24:56Z bms $
31 */
32
33#include "opt_ipfw.h"
34#include "opt_ipsec.h"
35#include "opt_mac.h"
36#include "opt_mbuf_stress_test.h"
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/malloc.h>
42#include <sys/mbuf.h>
43#include <sys/priv.h>
44#include <sys/protosw.h>
45#include <sys/socket.h>
46#include <sys/socketvar.h>
47#include <sys/sysctl.h>
48
49#include <net/if.h>
50#include <net/netisr.h>
51#include <net/pfil.h>
52#include <net/route.h>
53
54#include <netinet/in.h>
55#include <netinet/in_systm.h>
56#include <netinet/ip.h>
57#include <netinet/in_pcb.h>
58#include <netinet/in_var.h>
59#include <netinet/ip_var.h>
60#include <netinet/ip_options.h>
61
62#if defined(IPSEC) || defined(FAST_IPSEC)
63#include <netinet/ip_ipsec.h>
64#ifdef IPSEC
65#include <netinet6/ipsec.h>
66#endif
67#ifdef FAST_IPSEC
68#include <netipsec/ipsec.h>
69#endif
70#endif /*IPSEC*/
71
72#include <machine/in_cksum.h>
73
74#include <security/mac/mac_framework.h>
75
76#define print_ip(x, a, y)	 printf("%s %d.%d.%d.%d%s",\
77				x, (ntohl(a.s_addr)>>24)&0xFF,\
78				  (ntohl(a.s_addr)>>16)&0xFF,\
79				  (ntohl(a.s_addr)>>8)&0xFF,\
80				  (ntohl(a.s_addr))&0xFF, y);
81
82u_short ip_id;
83
84#ifdef MBUF_STRESS_TEST
85int mbuf_frag_size = 0;
86SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW,
87	&mbuf_frag_size, 0, "Fragment outgoing mbufs to this size");
88#endif
89
90static void	ip_mloopback
91	(struct ifnet *, struct mbuf *, struct sockaddr_in *, int);
92
93
94extern	struct protosw inetsw[];
95
96/*
97 * IP output.  The packet in mbuf chain m contains a skeletal IP
98 * header (with len, off, ttl, proto, tos, src, dst).
99 * The mbuf chain containing the packet will be freed.
100 * The mbuf opt, if present, will not be freed.
101 * In the IP forwarding case, the packet will arrive with options already
102 * inserted, so must have a NULL opt pointer.
103 */
104int
105ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
106    struct ip_moptions *imo, struct inpcb *inp)
107{
108	struct ip *ip;
109	struct ifnet *ifp = NULL;	/* keep compiler happy */
110	struct mbuf *m0;
111	int hlen = sizeof (struct ip);
112	int mtu;
113	int len, error = 0;
114	struct sockaddr_in *dst = NULL;	/* keep compiler happy */
115	struct in_ifaddr *ia = NULL;
116	int isbroadcast, sw_csum;
117	struct route iproute;
118	struct in_addr odst;
119#ifdef IPFIREWALL_FORWARD
120	struct m_tag *fwd_tag = NULL;
121#endif
122	M_ASSERTPKTHDR(m);
123
124	if (ro == NULL) {
125		ro = &iproute;
126		bzero(ro, sizeof (*ro));
127	}
128
129	if (inp != NULL)
130		INP_LOCK_ASSERT(inp);
131
132	if (opt) {
133		len = 0;
134		m = ip_insertoptions(m, opt, &len);
135		if (len != 0)
136			hlen = len;
137	}
138	ip = mtod(m, struct ip *);
139
140	/*
141	 * Fill in IP header.  If we are not allowing fragmentation,
142	 * then the ip_id field is meaningless, but we don't set it
143	 * to zero.  Doing so causes various problems when devices along
144	 * the path (routers, load balancers, firewalls, etc.) illegally
145	 * disable DF on our packet.  Note that a 16-bit counter
146	 * will wrap around in less than 10 seconds at 100 Mbit/s on a
147	 * medium with MTU 1500.  See Steven M. Bellovin, "A Technique
148	 * for Counting NATted Hosts", Proc. IMW'02, available at
149	 * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>.
150	 */
151	if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) {
152		ip->ip_v = IPVERSION;
153		ip->ip_hl = hlen >> 2;
154		ip->ip_id = ip_newid();
155		ipstat.ips_localout++;
156	} else {
157		hlen = ip->ip_hl << 2;
158	}
159
160	dst = (struct sockaddr_in *)&ro->ro_dst;
161again:
162	/*
163	 * If there is a cached route,
164	 * check that it is to the same destination
165	 * and is still up.  If not, free it and try again.
166	 * The address family should also be checked in case of sharing the
167	 * cache with IPv6.
168	 */
169	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
170			  dst->sin_family != AF_INET ||
171			  dst->sin_addr.s_addr != ip->ip_dst.s_addr)) {
172		RTFREE(ro->ro_rt);
173		ro->ro_rt = (struct rtentry *)NULL;
174	}
175#ifdef IPFIREWALL_FORWARD
176	if (ro->ro_rt == NULL && fwd_tag == NULL) {
177#else
178	if (ro->ro_rt == NULL) {
179#endif
180		bzero(dst, sizeof(*dst));
181		dst->sin_family = AF_INET;
182		dst->sin_len = sizeof(*dst);
183		dst->sin_addr = ip->ip_dst;
184	}
185	/*
186	 * If routing to interface only, short circuit routing lookup.
187	 * The use of an all-ones broadcast address implies this; an
188	 * interface is specified by the broadcast address of an interface,
189	 * or the destination address of a ptp interface.
190	 */
191	if (flags & IP_SENDONES) {
192		if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL &&
193		    (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL) {
194			ipstat.ips_noroute++;
195			error = ENETUNREACH;
196			goto bad;
197		}
198		ip->ip_dst.s_addr = INADDR_BROADCAST;
199		dst->sin_addr = ip->ip_dst;
200		ifp = ia->ia_ifp;
201		ip->ip_ttl = 1;
202		isbroadcast = 1;
203	} else if (flags & IP_ROUTETOIF) {
204		if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL &&
205		    (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) {
206			ipstat.ips_noroute++;
207			error = ENETUNREACH;
208			goto bad;
209		}
210		ifp = ia->ia_ifp;
211		ip->ip_ttl = 1;
212		isbroadcast = in_broadcast(dst->sin_addr, ifp);
213	} else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
214	    imo != NULL && imo->imo_multicast_ifp != NULL) {
215		/*
216		 * Bypass the normal routing lookup for multicast
217		 * packets if the interface is specified.
218		 */
219		ifp = imo->imo_multicast_ifp;
220		IFP_TO_IA(ifp, ia);
221		isbroadcast = 0;	/* fool gcc */
222	} else {
223		/*
224		 * We want to do any cloning requested by the link layer,
225		 * as this is probably required in all cases for correct
226		 * operation (as it is for ARP).
227		 */
228		if (ro->ro_rt == NULL)
229			rtalloc_ign(ro, 0);
230		if (ro->ro_rt == NULL) {
231			ipstat.ips_noroute++;
232			error = EHOSTUNREACH;
233			goto bad;
234		}
235		ia = ifatoia(ro->ro_rt->rt_ifa);
236		ifp = ro->ro_rt->rt_ifp;
237		ro->ro_rt->rt_rmx.rmx_pksent++;
238		if (ro->ro_rt->rt_flags & RTF_GATEWAY)
239			dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
240		if (ro->ro_rt->rt_flags & RTF_HOST)
241			isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST);
242		else
243			isbroadcast = in_broadcast(dst->sin_addr, ifp);
244	}
245	/*
246	 * Calculate MTU.  If we have a route that is up, use that,
247	 * otherwise use the interface's MTU.
248	 */
249	if (ro->ro_rt != NULL && (ro->ro_rt->rt_flags & (RTF_UP|RTF_HOST))) {
250		/*
251		 * This case can happen if the user changed the MTU
252		 * of an interface after enabling IP on it.  Because
253		 * most netifs don't keep track of routes pointing to
254		 * them, there is no way for one to update all its
255		 * routes when the MTU is changed.
256		 */
257		if (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)
258			ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
259		mtu = ro->ro_rt->rt_rmx.rmx_mtu;
260	} else {
261		mtu = ifp->if_mtu;
262	}
263	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
264		struct in_multi *inm;
265
266		m->m_flags |= M_MCAST;
267		/*
268		 * IP destination address is multicast.  Make sure "dst"
269		 * still points to the address in "ro".  (It may have been
270		 * changed to point to a gateway address, above.)
271		 */
272		dst = (struct sockaddr_in *)&ro->ro_dst;
273		/*
274		 * See if the caller provided any multicast options
275		 */
276		if (imo != NULL) {
277			ip->ip_ttl = imo->imo_multicast_ttl;
278			if (imo->imo_multicast_vif != -1)
279				ip->ip_src.s_addr =
280				    ip_mcast_src ?
281				    ip_mcast_src(imo->imo_multicast_vif) :
282				    INADDR_ANY;
283		} else
284			ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL;
285		/*
286		 * Confirm that the outgoing interface supports multicast.
287		 */
288		if ((imo == NULL) || (imo->imo_multicast_vif == -1)) {
289			if ((ifp->if_flags & IFF_MULTICAST) == 0) {
290				ipstat.ips_noroute++;
291				error = ENETUNREACH;
292				goto bad;
293			}
294		}
295		/*
296		 * If source address not specified yet, use address
297		 * of outgoing interface.
298		 */
299		if (ip->ip_src.s_addr == INADDR_ANY) {
300			/* Interface may have no addresses. */
301			if (ia != NULL)
302				ip->ip_src = IA_SIN(ia)->sin_addr;
303		}
304
305		IN_MULTI_LOCK();
306		IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm);
307		if (inm != NULL &&
308		   (imo == NULL || imo->imo_multicast_loop)) {
309			IN_MULTI_UNLOCK();
310			/*
311			 * If we belong to the destination multicast group
312			 * on the outgoing interface, and the caller did not
313			 * forbid loopback, loop back a copy.
314			 */
315			ip_mloopback(ifp, m, dst, hlen);
316		}
317		else {
318			IN_MULTI_UNLOCK();
319			/*
320			 * If we are acting as a multicast router, perform
321			 * multicast forwarding as if the packet had just
322			 * arrived on the interface to which we are about
323			 * to send.  The multicast forwarding function
324			 * recursively calls this function, using the
325			 * IP_FORWARDING flag to prevent infinite recursion.
326			 *
327			 * Multicasts that are looped back by ip_mloopback(),
328			 * above, will be forwarded by the ip_input() routine,
329			 * if necessary.
330			 */
331			if (ip_mrouter && (flags & IP_FORWARDING) == 0) {
332				/*
333				 * If rsvp daemon is not running, do not
334				 * set ip_moptions. This ensures that the packet
335				 * is multicast and not just sent down one link
336				 * as prescribed by rsvpd.
337				 */
338				if (!rsvp_on)
339					imo = NULL;
340				if (ip_mforward &&
341				    ip_mforward(ip, ifp, m, imo) != 0) {
342					m_freem(m);
343					goto done;
344				}
345			}
346		}
347
348		/*
349		 * Multicasts with a time-to-live of zero may be looped-
350		 * back, above, but must not be transmitted on a network.
351		 * Also, multicasts addressed to the loopback interface
352		 * are not sent -- the above call to ip_mloopback() will
353		 * loop back a copy if this host actually belongs to the
354		 * destination group on the loopback interface.
355		 */
356		if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
357			m_freem(m);
358			goto done;
359		}
360
361		goto sendit;
362	}
363
364	/*
365	 * If the source address is not specified yet, use the address
366	 * of the outoing interface.
367	 */
368	if (ip->ip_src.s_addr == INADDR_ANY) {
369		/* Interface may have no addresses. */
370		if (ia != NULL) {
371			ip->ip_src = IA_SIN(ia)->sin_addr;
372		}
373	}
374
375	/*
376	 * Verify that we have any chance at all of being able to queue the
377	 * packet or packet fragments, unless ALTQ is enabled on the given
378	 * interface in which case packetdrop should be done by queueing.
379	 */
380#ifdef ALTQ
381	if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) &&
382	    ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >=
383	    ifp->if_snd.ifq_maxlen))
384#else
385	if ((ifp->if_snd.ifq_len + ip->ip_len / mtu + 1) >=
386	    ifp->if_snd.ifq_maxlen)
387#endif /* ALTQ */
388	{
389		error = ENOBUFS;
390		ipstat.ips_odropped++;
391		ifp->if_snd.ifq_drops += (ip->ip_len / ifp->if_mtu + 1);
392		goto bad;
393	}
394
395	/*
396	 * Look for broadcast address and
397	 * verify user is allowed to send
398	 * such a packet.
399	 */
400	if (isbroadcast) {
401		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
402			error = EADDRNOTAVAIL;
403			goto bad;
404		}
405		if ((flags & IP_ALLOWBROADCAST) == 0) {
406			error = EACCES;
407			goto bad;
408		}
409		/* don't allow broadcast messages to be fragmented */
410		if (ip->ip_len > mtu) {
411			error = EMSGSIZE;
412			goto bad;
413		}
414		m->m_flags |= M_BCAST;
415	} else {
416		m->m_flags &= ~M_BCAST;
417	}
418
419sendit:
420#if defined(IPSEC) || defined(FAST_IPSEC)
421	switch(ip_ipsec_output(&m, inp, &flags, &error, &ro, &iproute, &dst, &ia, &ifp)) {
422	case 1:
423		goto bad;
424	case -1:
425		goto done;
426	case 0:
427	default:
428		break;	/* Continue with packet processing. */
429	}
430	/* Update variables that are affected by ipsec4_output(). */
431	ip = mtod(m, struct ip *);
432	hlen = ip->ip_hl << 2;
433#endif /* IPSEC */
434
435	/* Jump over all PFIL processing if hooks are not active. */
436	if (!PFIL_HOOKED(&inet_pfil_hook))
437		goto passout;
438
439	/* Run through list of hooks for output packets. */
440	odst.s_addr = ip->ip_dst.s_addr;
441	error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp);
442	if (error != 0 || m == NULL)
443		goto done;
444
445	ip = mtod(m, struct ip *);
446
447	/* See if destination IP address was changed by packet filter. */
448	if (odst.s_addr != ip->ip_dst.s_addr) {
449		m->m_flags |= M_SKIP_FIREWALL;
450		/* If destination is now ourself drop to ip_input(). */
451		if (in_localip(ip->ip_dst)) {
452			m->m_flags |= M_FASTFWD_OURS;
453			if (m->m_pkthdr.rcvif == NULL)
454				m->m_pkthdr.rcvif = loif;
455			if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
456				m->m_pkthdr.csum_flags |=
457				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
458				m->m_pkthdr.csum_data = 0xffff;
459			}
460			m->m_pkthdr.csum_flags |=
461			    CSUM_IP_CHECKED | CSUM_IP_VALID;
462
463			error = netisr_queue(NETISR_IP, m);
464			goto done;
465		} else
466			goto again;	/* Redo the routing table lookup. */
467	}
468
469#ifdef IPFIREWALL_FORWARD
470	/* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */
471	if (m->m_flags & M_FASTFWD_OURS) {
472		if (m->m_pkthdr.rcvif == NULL)
473			m->m_pkthdr.rcvif = loif;
474		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
475			m->m_pkthdr.csum_flags |=
476			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
477			m->m_pkthdr.csum_data = 0xffff;
478		}
479		m->m_pkthdr.csum_flags |=
480			    CSUM_IP_CHECKED | CSUM_IP_VALID;
481
482		error = netisr_queue(NETISR_IP, m);
483		goto done;
484	}
485	/* Or forward to some other address? */
486	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
487	if (fwd_tag) {
488		dst = (struct sockaddr_in *)&ro->ro_dst;
489		bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in));
490		m->m_flags |= M_SKIP_FIREWALL;
491		m_tag_delete(m, fwd_tag);
492		goto again;
493	}
494#endif /* IPFIREWALL_FORWARD */
495
496passout:
497	/* 127/8 must not appear on wire - RFC1122. */
498	if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
499	    (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) {
500		if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
501			ipstat.ips_badaddr++;
502			error = EADDRNOTAVAIL;
503			goto bad;
504		}
505	}
506
507	m->m_pkthdr.csum_flags |= CSUM_IP;
508	sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist;
509	if (sw_csum & CSUM_DELAY_DATA) {
510		in_delayed_cksum(m);
511		sw_csum &= ~CSUM_DELAY_DATA;
512	}
513	m->m_pkthdr.csum_flags &= ifp->if_hwassist;
514
515	/*
516	 * If small enough for interface, or the interface will take
517	 * care of the fragmentation for us, we can just send directly.
518	 */
519	if (ip->ip_len <= mtu ||
520	    (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 ||
521	    ((ip->ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) {
522		ip->ip_len = htons(ip->ip_len);
523		ip->ip_off = htons(ip->ip_off);
524		ip->ip_sum = 0;
525		if (sw_csum & CSUM_DELAY_IP)
526			ip->ip_sum = in_cksum(m, hlen);
527
528		/*
529		 * Record statistics for this interface address.
530		 * With CSUM_TSO the byte/packet count will be slightly
531		 * incorrect because we count the IP+TCP headers only
532		 * once instead of for every generated packet.
533		 */
534		if (!(flags & IP_FORWARDING) && ia) {
535			if (m->m_pkthdr.csum_flags & CSUM_TSO)
536				ia->ia_ifa.if_opackets +=
537				    m->m_pkthdr.len / m->m_pkthdr.tso_segsz;
538			else
539				ia->ia_ifa.if_opackets++;
540			ia->ia_ifa.if_obytes += m->m_pkthdr.len;
541		}
542#ifdef IPSEC
543		/* clean ipsec history once it goes out of the node */
544		ipsec_delaux(m);
545#endif
546#ifdef MBUF_STRESS_TEST
547		if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size)
548			m = m_fragment(m, M_DONTWAIT, mbuf_frag_size);
549#endif
550		/*
551		 * Reset layer specific mbuf flags
552		 * to avoid confusing lower layers.
553		 */
554		m->m_flags &= ~(M_PROTOFLAGS);
555
556		error = (*ifp->if_output)(ifp, m,
557				(struct sockaddr *)dst, ro->ro_rt);
558		goto done;
559	}
560
561	/* Balk when DF bit is set or the interface didn't support TSO. */
562	if ((ip->ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) {
563		error = EMSGSIZE;
564		ipstat.ips_cantfrag++;
565		goto bad;
566	}
567
568	/*
569	 * Too large for interface; fragment if possible. If successful,
570	 * on return, m will point to a list of packets to be sent.
571	 */
572	error = ip_fragment(ip, &m, mtu, ifp->if_hwassist, sw_csum);
573	if (error)
574		goto bad;
575	for (; m; m = m0) {
576		m0 = m->m_nextpkt;
577		m->m_nextpkt = 0;
578#ifdef IPSEC
579		/* clean ipsec history once it goes out of the node */
580		ipsec_delaux(m);
581#endif
582		if (error == 0) {
583			/* Record statistics for this interface address. */
584			if (ia != NULL) {
585				ia->ia_ifa.if_opackets++;
586				ia->ia_ifa.if_obytes += m->m_pkthdr.len;
587			}
588			/*
589			 * Reset layer specific mbuf flags
590			 * to avoid confusing upper layers.
591			 */
592			m->m_flags &= ~(M_PROTOFLAGS);
593
594			error = (*ifp->if_output)(ifp, m,
595			    (struct sockaddr *)dst, ro->ro_rt);
596		} else
597			m_freem(m);
598	}
599
600	if (error == 0)
601		ipstat.ips_fragmented++;
602
603done:
604	if (ro == &iproute && ro->ro_rt) {
605		RTFREE(ro->ro_rt);
606	}
607	return (error);
608bad:
609	m_freem(m);
610	goto done;
611}
612
613/*
614 * Create a chain of fragments which fit the given mtu. m_frag points to the
615 * mbuf to be fragmented; on return it points to the chain with the fragments.
616 * Return 0 if no error. If error, m_frag may contain a partially built
617 * chain of fragments that should be freed by the caller.
618 *
619 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist)
620 * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP).
621 */
622int
623ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu,
624    u_long if_hwassist_flags, int sw_csum)
625{
626	int error = 0;
627	int hlen = ip->ip_hl << 2;
628	int len = (mtu - hlen) & ~7;	/* size of payload in each fragment */
629	int off;
630	struct mbuf *m0 = *m_frag;	/* the original packet		*/
631	int firstlen;
632	struct mbuf **mnext;
633	int nfrags;
634
635	if (ip->ip_off & IP_DF) {	/* Fragmentation not allowed */
636		ipstat.ips_cantfrag++;
637		return EMSGSIZE;
638	}
639
640	/*
641	 * Must be able to put at least 8 bytes per fragment.
642	 */
643	if (len < 8)
644		return EMSGSIZE;
645
646	/*
647	 * If the interface will not calculate checksums on
648	 * fragmented packets, then do it here.
649	 */
650	if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA &&
651	    (if_hwassist_flags & CSUM_IP_FRAGS) == 0) {
652		in_delayed_cksum(m0);
653		m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
654	}
655
656	if (len > PAGE_SIZE) {
657		/*
658		 * Fragment large datagrams such that each segment
659		 * contains a multiple of PAGE_SIZE amount of data,
660		 * plus headers. This enables a receiver to perform
661		 * page-flipping zero-copy optimizations.
662		 *
663		 * XXX When does this help given that sender and receiver
664		 * could have different page sizes, and also mtu could
665		 * be less than the receiver's page size ?
666		 */
667		int newlen;
668		struct mbuf *m;
669
670		for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next)
671			off += m->m_len;
672
673		/*
674		 * firstlen (off - hlen) must be aligned on an
675		 * 8-byte boundary
676		 */
677		if (off < hlen)
678			goto smart_frag_failure;
679		off = ((off - hlen) & ~7) + hlen;
680		newlen = (~PAGE_MASK) & mtu;
681		if ((newlen + sizeof (struct ip)) > mtu) {
682			/* we failed, go back the default */
683smart_frag_failure:
684			newlen = len;
685			off = hlen + len;
686		}
687		len = newlen;
688
689	} else {
690		off = hlen + len;
691	}
692
693	firstlen = off - hlen;
694	mnext = &m0->m_nextpkt;		/* pointer to next packet */
695
696	/*
697	 * Loop through length of segment after first fragment,
698	 * make new header and copy data of each part and link onto chain.
699	 * Here, m0 is the original packet, m is the fragment being created.
700	 * The fragments are linked off the m_nextpkt of the original
701	 * packet, which after processing serves as the first fragment.
702	 */
703	for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) {
704		struct ip *mhip;	/* ip header on the fragment */
705		struct mbuf *m;
706		int mhlen = sizeof (struct ip);
707
708		MGETHDR(m, M_DONTWAIT, MT_DATA);
709		if (m == NULL) {
710			error = ENOBUFS;
711			ipstat.ips_odropped++;
712			goto done;
713		}
714		m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
715		/*
716		 * In the first mbuf, leave room for the link header, then
717		 * copy the original IP header including options. The payload
718		 * goes into an additional mbuf chain returned by m_copy().
719		 */
720		m->m_data += max_linkhdr;
721		mhip = mtod(m, struct ip *);
722		*mhip = *ip;
723		if (hlen > sizeof (struct ip)) {
724			mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
725			mhip->ip_v = IPVERSION;
726			mhip->ip_hl = mhlen >> 2;
727		}
728		m->m_len = mhlen;
729		/* XXX do we need to add ip->ip_off below ? */
730		mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off;
731		if (off + len >= ip->ip_len) {	/* last fragment */
732			len = ip->ip_len - off;
733			m->m_flags |= M_LASTFRAG;
734		} else
735			mhip->ip_off |= IP_MF;
736		mhip->ip_len = htons((u_short)(len + mhlen));
737		m->m_next = m_copy(m0, off, len);
738		if (m->m_next == NULL) {	/* copy failed */
739			m_free(m);
740			error = ENOBUFS;	/* ??? */
741			ipstat.ips_odropped++;
742			goto done;
743		}
744		m->m_pkthdr.len = mhlen + len;
745		m->m_pkthdr.rcvif = NULL;
746#ifdef MAC
747		mac_create_fragment(m0, m);
748#endif
749		m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
750		mhip->ip_off = htons(mhip->ip_off);
751		mhip->ip_sum = 0;
752		if (sw_csum & CSUM_DELAY_IP)
753			mhip->ip_sum = in_cksum(m, mhlen);
754		*mnext = m;
755		mnext = &m->m_nextpkt;
756	}
757	ipstat.ips_ofragments += nfrags;
758
759	/* set first marker for fragment chain */
760	m0->m_flags |= M_FIRSTFRAG | M_FRAG;
761	m0->m_pkthdr.csum_data = nfrags;
762
763	/*
764	 * Update first fragment by trimming what's been copied out
765	 * and updating header.
766	 */
767	m_adj(m0, hlen + firstlen - ip->ip_len);
768	m0->m_pkthdr.len = hlen + firstlen;
769	ip->ip_len = htons((u_short)m0->m_pkthdr.len);
770	ip->ip_off |= IP_MF;
771	ip->ip_off = htons(ip->ip_off);
772	ip->ip_sum = 0;
773	if (sw_csum & CSUM_DELAY_IP)
774		ip->ip_sum = in_cksum(m0, hlen);
775
776done:
777	*m_frag = m0;
778	return error;
779}
780
781void
782in_delayed_cksum(struct mbuf *m)
783{
784	struct ip *ip;
785	u_short csum, offset;
786
787	ip = mtod(m, struct ip *);
788	offset = ip->ip_hl << 2 ;
789	csum = in_cksum_skip(m, ip->ip_len, offset);
790	if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0)
791		csum = 0xffff;
792	offset += m->m_pkthdr.csum_data;	/* checksum offset */
793
794	if (offset + sizeof(u_short) > m->m_len) {
795		printf("delayed m_pullup, m->len: %d  off: %d  p: %d\n",
796		    m->m_len, offset, ip->ip_p);
797		/*
798		 * XXX
799		 * this shouldn't happen, but if it does, the
800		 * correct behavior may be to insert the checksum
801		 * in the appropriate next mbuf in the chain.
802		 */
803		return;
804	}
805	*(u_short *)(m->m_data + offset) = csum;
806}
807
808/*
809 * IP socket option processing.
810 */
811int
812ip_ctloutput(struct socket *so, struct sockopt *sopt)
813{
814	struct	inpcb *inp = sotoinpcb(so);
815	int	error, optval;
816
817	error = optval = 0;
818	if (sopt->sopt_level != IPPROTO_IP) {
819		return (EINVAL);
820	}
821
822	switch (sopt->sopt_dir) {
823	case SOPT_SET:
824		switch (sopt->sopt_name) {
825		case IP_OPTIONS:
826#ifdef notyet
827		case IP_RETOPTS:
828#endif
829		{
830			struct mbuf *m;
831			if (sopt->sopt_valsize > MLEN) {
832				error = EMSGSIZE;
833				break;
834			}
835			MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_DATA);
836			if (m == NULL) {
837				error = ENOBUFS;
838				break;
839			}
840			m->m_len = sopt->sopt_valsize;
841			error = sooptcopyin(sopt, mtod(m, char *), m->m_len,
842					    m->m_len);
843			if (error) {
844				m_free(m);
845				break;
846			}
847			INP_LOCK(inp);
848			error = ip_pcbopts(inp, sopt->sopt_name, m);
849			INP_UNLOCK(inp);
850			return (error);
851		}
852
853		case IP_TOS:
854		case IP_TTL:
855		case IP_MINTTL:
856		case IP_RECVOPTS:
857		case IP_RECVRETOPTS:
858		case IP_RECVDSTADDR:
859		case IP_RECVTTL:
860		case IP_RECVIF:
861		case IP_FAITH:
862		case IP_ONESBCAST:
863		case IP_DONTFRAG:
864			error = sooptcopyin(sopt, &optval, sizeof optval,
865					    sizeof optval);
866			if (error)
867				break;
868
869			switch (sopt->sopt_name) {
870			case IP_TOS:
871				inp->inp_ip_tos = optval;
872				break;
873
874			case IP_TTL:
875				inp->inp_ip_ttl = optval;
876				break;
877
878			case IP_MINTTL:
879				if (optval > 0 && optval <= MAXTTL)
880					inp->inp_ip_minttl = optval;
881				else
882					error = EINVAL;
883				break;
884
885#define	OPTSET(bit) do {						\
886	INP_LOCK(inp);							\
887	if (optval)							\
888		inp->inp_flags |= bit;					\
889	else								\
890		inp->inp_flags &= ~bit;					\
891	INP_UNLOCK(inp);						\
892} while (0)
893
894			case IP_RECVOPTS:
895				OPTSET(INP_RECVOPTS);
896				break;
897
898			case IP_RECVRETOPTS:
899				OPTSET(INP_RECVRETOPTS);
900				break;
901
902			case IP_RECVDSTADDR:
903				OPTSET(INP_RECVDSTADDR);
904				break;
905
906			case IP_RECVTTL:
907				OPTSET(INP_RECVTTL);
908				break;
909
910			case IP_RECVIF:
911				OPTSET(INP_RECVIF);
912				break;
913
914			case IP_FAITH:
915				OPTSET(INP_FAITH);
916				break;
917
918			case IP_ONESBCAST:
919				OPTSET(INP_ONESBCAST);
920				break;
921			case IP_DONTFRAG:
922				OPTSET(INP_DONTFRAG);
923				break;
924			}
925			break;
926#undef OPTSET
927
928		/*
929		 * Multicast socket options are processed by the in_mcast
930		 * module.
931		 */
932		case IP_MULTICAST_IF:
933		case IP_MULTICAST_VIF:
934		case IP_MULTICAST_TTL:
935		case IP_MULTICAST_LOOP:
936		case IP_ADD_MEMBERSHIP:
937		case IP_DROP_MEMBERSHIP:
938		case IP_ADD_SOURCE_MEMBERSHIP:
939		case IP_DROP_SOURCE_MEMBERSHIP:
940		case IP_BLOCK_SOURCE:
941		case IP_UNBLOCK_SOURCE:
942		case IP_MSFILTER:
943		case MCAST_JOIN_GROUP:
944		case MCAST_LEAVE_GROUP:
945		case MCAST_JOIN_SOURCE_GROUP:
946		case MCAST_LEAVE_SOURCE_GROUP:
947		case MCAST_BLOCK_SOURCE:
948		case MCAST_UNBLOCK_SOURCE:
949			error = inp_setmoptions(inp, sopt);
950			break;
951
952		case IP_PORTRANGE:
953			error = sooptcopyin(sopt, &optval, sizeof optval,
954					    sizeof optval);
955			if (error)
956				break;
957
958			INP_LOCK(inp);
959			switch (optval) {
960			case IP_PORTRANGE_DEFAULT:
961				inp->inp_flags &= ~(INP_LOWPORT);
962				inp->inp_flags &= ~(INP_HIGHPORT);
963				break;
964
965			case IP_PORTRANGE_HIGH:
966				inp->inp_flags &= ~(INP_LOWPORT);
967				inp->inp_flags |= INP_HIGHPORT;
968				break;
969
970			case IP_PORTRANGE_LOW:
971				inp->inp_flags &= ~(INP_HIGHPORT);
972				inp->inp_flags |= INP_LOWPORT;
973				break;
974
975			default:
976				error = EINVAL;
977				break;
978			}
979			INP_UNLOCK(inp);
980			break;
981
982#if defined(IPSEC) || defined(FAST_IPSEC)
983		case IP_IPSEC_POLICY:
984		{
985			caddr_t req;
986			size_t len = 0;
987			int priv;
988			struct mbuf *m;
989			int optname;
990
991			if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
992				break;
993			if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
994				break;
995			if (sopt->sopt_td != NULL) {
996				/*
997				 * XXXRW: Would be more desirable to do this
998				 * one layer down so that we only exercise
999				 * privilege if it is needed.
1000				 */
1001				error = priv_check(sopt->sopt_td,
1002				    PRIV_NETINET_IPSEC);
1003				if (error)
1004					priv = 0;
1005				else
1006					priv = 1;
1007			} else
1008				priv = 1;
1009			req = mtod(m, caddr_t);
1010			len = m->m_len;
1011			optname = sopt->sopt_name;
1012			error = ipsec4_set_policy(inp, optname, req, len, priv);
1013			m_freem(m);
1014			break;
1015		}
1016#endif /*IPSEC*/
1017
1018		default:
1019			error = ENOPROTOOPT;
1020			break;
1021		}
1022		break;
1023
1024	case SOPT_GET:
1025		switch (sopt->sopt_name) {
1026		case IP_OPTIONS:
1027		case IP_RETOPTS:
1028			if (inp->inp_options)
1029				error = sooptcopyout(sopt,
1030						     mtod(inp->inp_options,
1031							  char *),
1032						     inp->inp_options->m_len);
1033			else
1034				sopt->sopt_valsize = 0;
1035			break;
1036
1037		case IP_TOS:
1038		case IP_TTL:
1039		case IP_MINTTL:
1040		case IP_RECVOPTS:
1041		case IP_RECVRETOPTS:
1042		case IP_RECVDSTADDR:
1043		case IP_RECVTTL:
1044		case IP_RECVIF:
1045		case IP_PORTRANGE:
1046		case IP_FAITH:
1047		case IP_ONESBCAST:
1048		case IP_DONTFRAG:
1049			switch (sopt->sopt_name) {
1050
1051			case IP_TOS:
1052				optval = inp->inp_ip_tos;
1053				break;
1054
1055			case IP_TTL:
1056				optval = inp->inp_ip_ttl;
1057				break;
1058
1059			case IP_MINTTL:
1060				optval = inp->inp_ip_minttl;
1061				break;
1062
1063#define	OPTBIT(bit)	(inp->inp_flags & bit ? 1 : 0)
1064
1065			case IP_RECVOPTS:
1066				optval = OPTBIT(INP_RECVOPTS);
1067				break;
1068
1069			case IP_RECVRETOPTS:
1070				optval = OPTBIT(INP_RECVRETOPTS);
1071				break;
1072
1073			case IP_RECVDSTADDR:
1074				optval = OPTBIT(INP_RECVDSTADDR);
1075				break;
1076
1077			case IP_RECVTTL:
1078				optval = OPTBIT(INP_RECVTTL);
1079				break;
1080
1081			case IP_RECVIF:
1082				optval = OPTBIT(INP_RECVIF);
1083				break;
1084
1085			case IP_PORTRANGE:
1086				if (inp->inp_flags & INP_HIGHPORT)
1087					optval = IP_PORTRANGE_HIGH;
1088				else if (inp->inp_flags & INP_LOWPORT)
1089					optval = IP_PORTRANGE_LOW;
1090				else
1091					optval = 0;
1092				break;
1093
1094			case IP_FAITH:
1095				optval = OPTBIT(INP_FAITH);
1096				break;
1097
1098			case IP_ONESBCAST:
1099				optval = OPTBIT(INP_ONESBCAST);
1100				break;
1101			case IP_DONTFRAG:
1102				optval = OPTBIT(INP_DONTFRAG);
1103				break;
1104			}
1105			error = sooptcopyout(sopt, &optval, sizeof optval);
1106			break;
1107
1108		/*
1109		 * Multicast socket options are processed by the in_mcast
1110		 * module.
1111		 */
1112		case IP_MULTICAST_IF:
1113		case IP_MULTICAST_VIF:
1114		case IP_MULTICAST_TTL:
1115		case IP_MULTICAST_LOOP:
1116		case IP_MSFILTER:
1117			error = inp_getmoptions(inp, sopt);
1118			break;
1119
1120#if defined(IPSEC) || defined(FAST_IPSEC)
1121		case IP_IPSEC_POLICY:
1122		{
1123			struct mbuf *m = NULL;
1124			caddr_t req = NULL;
1125			size_t len = 0;
1126
1127			if (m != 0) {
1128				req = mtod(m, caddr_t);
1129				len = m->m_len;
1130			}
1131			error = ipsec4_get_policy(sotoinpcb(so), req, len, &m);
1132			if (error == 0)
1133				error = soopt_mcopyout(sopt, m); /* XXX */
1134			if (error == 0)
1135				m_freem(m);
1136			break;
1137		}
1138#endif /*IPSEC*/
1139
1140		default:
1141			error = ENOPROTOOPT;
1142			break;
1143		}
1144		break;
1145	}
1146	return (error);
1147}
1148
1149/*
1150 * Routine called from ip_output() to loop back a copy of an IP multicast
1151 * packet to the input queue of a specified interface.  Note that this
1152 * calls the output routine of the loopback "driver", but with an interface
1153 * pointer that might NOT be a loopback interface -- evil, but easier than
1154 * replicating that code here.
1155 */
1156static void
1157ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst,
1158    int hlen)
1159{
1160	register struct ip *ip;
1161	struct mbuf *copym;
1162
1163	copym = m_copy(m, 0, M_COPYALL);
1164	if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen))
1165		copym = m_pullup(copym, hlen);
1166	if (copym != NULL) {
1167		/* If needed, compute the checksum and mark it as valid. */
1168		if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
1169			in_delayed_cksum(copym);
1170			copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
1171			copym->m_pkthdr.csum_flags |=
1172			    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1173			copym->m_pkthdr.csum_data = 0xffff;
1174		}
1175		/*
1176		 * We don't bother to fragment if the IP length is greater
1177		 * than the interface's MTU.  Can this possibly matter?
1178		 */
1179		ip = mtod(copym, struct ip *);
1180		ip->ip_len = htons(ip->ip_len);
1181		ip->ip_off = htons(ip->ip_off);
1182		ip->ip_sum = 0;
1183		ip->ip_sum = in_cksum(copym, hlen);
1184		/*
1185		 * NB:
1186		 * It's not clear whether there are any lingering
1187		 * reentrancy problems in other areas which might
1188		 * be exposed by using ip_input directly (in
1189		 * particular, everything which modifies the packet
1190		 * in-place).  Yet another option is using the
1191		 * protosw directly to deliver the looped back
1192		 * packet.  For the moment, we'll err on the side
1193		 * of safety by using if_simloop().
1194		 */
1195#if 1 /* XXX */
1196		if (dst->sin_family != AF_INET) {
1197			printf("ip_mloopback: bad address family %d\n",
1198						dst->sin_family);
1199			dst->sin_family = AF_INET;
1200		}
1201#endif
1202
1203#ifdef notdef
1204		copym->m_pkthdr.rcvif = ifp;
1205		ip_input(copym);
1206#else
1207		if_simloop(ifp, copym, dst->sin_family, 0);
1208#endif
1209	}
1210}
1211