xref: /openbmc/linux/net/ipv4/ip_output.c (revision 02c30a84e6298b6b20a56f0896ac80b47839e134)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  INET is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		The Internet Protocol (IP) output module.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Version:	$Id: ip_output.c,v 1.100 2002/02/01 22:01:03 davem Exp $
91da177e4SLinus Torvalds  *
10*02c30a84SJesper Juhl  * Authors:	Ross Biro
111da177e4SLinus Torvalds  *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
121da177e4SLinus Torvalds  *		Donald Becker, <becker@super.org>
131da177e4SLinus Torvalds  *		Alan Cox, <Alan.Cox@linux.org>
141da177e4SLinus Torvalds  *		Richard Underwood
151da177e4SLinus Torvalds  *		Stefan Becker, <stefanb@yello.ping.de>
161da177e4SLinus Torvalds  *		Jorge Cwik, <jorge@laser.satlink.net>
171da177e4SLinus Torvalds  *		Arnt Gulbrandsen, <agulbra@nvg.unit.no>
181da177e4SLinus Torvalds  *		Hirokazu Takahashi, <taka@valinux.co.jp>
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *	See ip_input.c for original log
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  *	Fixes:
231da177e4SLinus Torvalds  *		Alan Cox	:	Missing nonblock feature in ip_build_xmit.
241da177e4SLinus Torvalds  *		Mike Kilburn	:	htons() missing in ip_build_xmit.
251da177e4SLinus Torvalds  *		Bradford Johnson:	Fix faulty handling of some frames when
261da177e4SLinus Torvalds  *					no route is found.
271da177e4SLinus Torvalds  *		Alexander Demenshin:	Missing sk/skb free in ip_queue_xmit
281da177e4SLinus Torvalds  *					(in case if packet not accepted by
291da177e4SLinus Torvalds  *					output firewall rules)
301da177e4SLinus Torvalds  *		Mike McLagan	:	Routing by source
311da177e4SLinus Torvalds  *		Alexey Kuznetsov:	use new route cache
321da177e4SLinus Torvalds  *		Andi Kleen:		Fix broken PMTU recovery and remove
331da177e4SLinus Torvalds  *					some redundant tests.
341da177e4SLinus Torvalds  *	Vitaly E. Lavrov	:	Transparent proxy revived after year coma.
351da177e4SLinus Torvalds  *		Andi Kleen	: 	Replace ip_reply with ip_send_reply.
361da177e4SLinus Torvalds  *		Andi Kleen	:	Split fast and slow ip_build_xmit path
371da177e4SLinus Torvalds  *					for decreased register pressure on x86
381da177e4SLinus Torvalds  *					and more readibility.
391da177e4SLinus Torvalds  *		Marc Boucher	:	When call_out_firewall returns FW_QUEUE,
401da177e4SLinus Torvalds  *					silently drop skb instead of failing with -EPERM.
411da177e4SLinus Torvalds  *		Detlev Wengorz	:	Copy protocol for fragments.
421da177e4SLinus Torvalds  *		Hirokazu Takahashi:	HW checksumming for outgoing UDP
431da177e4SLinus Torvalds  *					datagrams.
441da177e4SLinus Torvalds  *		Hirokazu Takahashi:	sendfile() on UDP works now.
451da177e4SLinus Torvalds  */
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #include <asm/uaccess.h>
481da177e4SLinus Torvalds #include <asm/system.h>
491da177e4SLinus Torvalds #include <linux/module.h>
501da177e4SLinus Torvalds #include <linux/types.h>
511da177e4SLinus Torvalds #include <linux/kernel.h>
521da177e4SLinus Torvalds #include <linux/sched.h>
531da177e4SLinus Torvalds #include <linux/mm.h>
541da177e4SLinus Torvalds #include <linux/string.h>
551da177e4SLinus Torvalds #include <linux/errno.h>
561da177e4SLinus Torvalds #include <linux/config.h>
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds #include <linux/socket.h>
591da177e4SLinus Torvalds #include <linux/sockios.h>
601da177e4SLinus Torvalds #include <linux/in.h>
611da177e4SLinus Torvalds #include <linux/inet.h>
621da177e4SLinus Torvalds #include <linux/netdevice.h>
631da177e4SLinus Torvalds #include <linux/etherdevice.h>
641da177e4SLinus Torvalds #include <linux/proc_fs.h>
651da177e4SLinus Torvalds #include <linux/stat.h>
661da177e4SLinus Torvalds #include <linux/init.h>
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #include <net/snmp.h>
691da177e4SLinus Torvalds #include <net/ip.h>
701da177e4SLinus Torvalds #include <net/protocol.h>
711da177e4SLinus Torvalds #include <net/route.h>
721da177e4SLinus Torvalds #include <net/tcp.h>
731da177e4SLinus Torvalds #include <net/udp.h>
741da177e4SLinus Torvalds #include <linux/skbuff.h>
751da177e4SLinus Torvalds #include <net/sock.h>
761da177e4SLinus Torvalds #include <net/arp.h>
771da177e4SLinus Torvalds #include <net/icmp.h>
781da177e4SLinus Torvalds #include <net/raw.h>
791da177e4SLinus Torvalds #include <net/checksum.h>
801da177e4SLinus Torvalds #include <net/inetpeer.h>
811da177e4SLinus Torvalds #include <net/checksum.h>
821da177e4SLinus Torvalds #include <linux/igmp.h>
831da177e4SLinus Torvalds #include <linux/netfilter_ipv4.h>
841da177e4SLinus Torvalds #include <linux/netfilter_bridge.h>
851da177e4SLinus Torvalds #include <linux/mroute.h>
861da177e4SLinus Torvalds #include <linux/netlink.h>
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds /*
891da177e4SLinus Torvalds  *      Shall we try to damage output packets if routing dev changes?
901da177e4SLinus Torvalds  */
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds int sysctl_ip_dynaddr;
931da177e4SLinus Torvalds int sysctl_ip_default_ttl = IPDEFTTL;
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds /* Generate a checksum for an outgoing IP datagram. */
961da177e4SLinus Torvalds __inline__ void ip_send_check(struct iphdr *iph)
971da177e4SLinus Torvalds {
981da177e4SLinus Torvalds 	iph->check = 0;
991da177e4SLinus Torvalds 	iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds /* dev_loopback_xmit for use with netfilter. */
1031da177e4SLinus Torvalds static int ip_dev_loopback_xmit(struct sk_buff *newskb)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	newskb->mac.raw = newskb->data;
1061da177e4SLinus Torvalds 	__skb_pull(newskb, newskb->nh.raw - newskb->data);
1071da177e4SLinus Torvalds 	newskb->pkt_type = PACKET_LOOPBACK;
1081da177e4SLinus Torvalds 	newskb->ip_summed = CHECKSUM_UNNECESSARY;
1091da177e4SLinus Torvalds 	BUG_TRAP(newskb->dst);
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER_DEBUG
1121da177e4SLinus Torvalds 	nf_debug_ip_loopback_xmit(newskb);
1131da177e4SLinus Torvalds #endif
114bd96535bSPatrick McHardy 	nf_reset(newskb);
1151da177e4SLinus Torvalds 	netif_rx(newskb);
1161da177e4SLinus Torvalds 	return 0;
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds static inline int ip_select_ttl(struct inet_sock *inet, struct dst_entry *dst)
1201da177e4SLinus Torvalds {
1211da177e4SLinus Torvalds 	int ttl = inet->uc_ttl;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	if (ttl < 0)
1241da177e4SLinus Torvalds 		ttl = dst_metric(dst, RTAX_HOPLIMIT);
1251da177e4SLinus Torvalds 	return ttl;
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /*
1291da177e4SLinus Torvalds  *		Add an ip header to a skbuff and send it out.
1301da177e4SLinus Torvalds  *
1311da177e4SLinus Torvalds  */
1321da177e4SLinus Torvalds int ip_build_and_send_pkt(struct sk_buff *skb, struct sock *sk,
1331da177e4SLinus Torvalds 			  u32 saddr, u32 daddr, struct ip_options *opt)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
1361da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable *)skb->dst;
1371da177e4SLinus Torvalds 	struct iphdr *iph;
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	/* Build the IP header. */
1401da177e4SLinus Torvalds 	if (opt)
1411da177e4SLinus Torvalds 		iph=(struct iphdr *)skb_push(skb,sizeof(struct iphdr) + opt->optlen);
1421da177e4SLinus Torvalds 	else
1431da177e4SLinus Torvalds 		iph=(struct iphdr *)skb_push(skb,sizeof(struct iphdr));
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	iph->version  = 4;
1461da177e4SLinus Torvalds 	iph->ihl      = 5;
1471da177e4SLinus Torvalds 	iph->tos      = inet->tos;
1481da177e4SLinus Torvalds 	if (ip_dont_fragment(sk, &rt->u.dst))
1491da177e4SLinus Torvalds 		iph->frag_off = htons(IP_DF);
1501da177e4SLinus Torvalds 	else
1511da177e4SLinus Torvalds 		iph->frag_off = 0;
1521da177e4SLinus Torvalds 	iph->ttl      = ip_select_ttl(inet, &rt->u.dst);
1531da177e4SLinus Torvalds 	iph->daddr    = rt->rt_dst;
1541da177e4SLinus Torvalds 	iph->saddr    = rt->rt_src;
1551da177e4SLinus Torvalds 	iph->protocol = sk->sk_protocol;
1561da177e4SLinus Torvalds 	iph->tot_len  = htons(skb->len);
1571da177e4SLinus Torvalds 	ip_select_ident(iph, &rt->u.dst, sk);
1581da177e4SLinus Torvalds 	skb->nh.iph   = iph;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	if (opt && opt->optlen) {
1611da177e4SLinus Torvalds 		iph->ihl += opt->optlen>>2;
1621da177e4SLinus Torvalds 		ip_options_build(skb, opt, daddr, rt, 0);
1631da177e4SLinus Torvalds 	}
1641da177e4SLinus Torvalds 	ip_send_check(iph);
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	/* Send it out. */
1691da177e4SLinus Torvalds 	return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
1701da177e4SLinus Torvalds 		       dst_output);
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds static inline int ip_finish_output2(struct sk_buff *skb)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	struct dst_entry *dst = skb->dst;
1761da177e4SLinus Torvalds 	struct hh_cache *hh = dst->hh;
1771da177e4SLinus Torvalds 	struct net_device *dev = dst->dev;
1781da177e4SLinus Torvalds 	int hh_len = LL_RESERVED_SPACE(dev);
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	/* Be paranoid, rather than too clever. */
1811da177e4SLinus Torvalds 	if (unlikely(skb_headroom(skb) < hh_len && dev->hard_header)) {
1821da177e4SLinus Torvalds 		struct sk_buff *skb2;
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 		skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
1851da177e4SLinus Torvalds 		if (skb2 == NULL) {
1861da177e4SLinus Torvalds 			kfree_skb(skb);
1871da177e4SLinus Torvalds 			return -ENOMEM;
1881da177e4SLinus Torvalds 		}
1891da177e4SLinus Torvalds 		if (skb->sk)
1901da177e4SLinus Torvalds 			skb_set_owner_w(skb2, skb->sk);
1911da177e4SLinus Torvalds 		kfree_skb(skb);
1921da177e4SLinus Torvalds 		skb = skb2;
1931da177e4SLinus Torvalds 	}
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER_DEBUG
1961da177e4SLinus Torvalds 	nf_debug_ip_finish_output2(skb);
1971da177e4SLinus Torvalds #endif /*CONFIG_NETFILTER_DEBUG*/
1981da177e4SLinus Torvalds 
199b31e5b1bSPatrick McHardy 	nf_reset(skb);
200b31e5b1bSPatrick McHardy 
2011da177e4SLinus Torvalds 	if (hh) {
2021da177e4SLinus Torvalds 		int hh_alen;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 		read_lock_bh(&hh->hh_lock);
2051da177e4SLinus Torvalds 		hh_alen = HH_DATA_ALIGN(hh->hh_len);
2061da177e4SLinus Torvalds   		memcpy(skb->data - hh_alen, hh->hh_data, hh_alen);
2071da177e4SLinus Torvalds 		read_unlock_bh(&hh->hh_lock);
2081da177e4SLinus Torvalds 	        skb_push(skb, hh->hh_len);
2091da177e4SLinus Torvalds 		return hh->hh_output(skb);
2101da177e4SLinus Torvalds 	} else if (dst->neighbour)
2111da177e4SLinus Torvalds 		return dst->neighbour->output(skb);
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	if (net_ratelimit())
2141da177e4SLinus Torvalds 		printk(KERN_DEBUG "ip_finish_output2: No header cache and no neighbour!\n");
2151da177e4SLinus Torvalds 	kfree_skb(skb);
2161da177e4SLinus Torvalds 	return -EINVAL;
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds int ip_finish_output(struct sk_buff *skb)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds 	struct net_device *dev = skb->dst->dev;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	skb->dev = dev;
2241da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_IP);
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 	return NF_HOOK(PF_INET, NF_IP_POST_ROUTING, skb, NULL, dev,
2271da177e4SLinus Torvalds 		       ip_finish_output2);
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds int ip_mc_output(struct sk_buff *skb)
2311da177e4SLinus Torvalds {
2321da177e4SLinus Torvalds 	struct sock *sk = skb->sk;
2331da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable*)skb->dst;
2341da177e4SLinus Torvalds 	struct net_device *dev = rt->u.dst.dev;
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	/*
2371da177e4SLinus Torvalds 	 *	If the indicated interface is up and running, send the packet.
2381da177e4SLinus Torvalds 	 */
2391da177e4SLinus Torvalds 	IP_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	skb->dev = dev;
2421da177e4SLinus Torvalds 	skb->protocol = htons(ETH_P_IP);
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	/*
2451da177e4SLinus Torvalds 	 *	Multicasts are looped back for other local users
2461da177e4SLinus Torvalds 	 */
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	if (rt->rt_flags&RTCF_MULTICAST) {
2491da177e4SLinus Torvalds 		if ((!sk || inet_sk(sk)->mc_loop)
2501da177e4SLinus Torvalds #ifdef CONFIG_IP_MROUTE
2511da177e4SLinus Torvalds 		/* Small optimization: do not loopback not local frames,
2521da177e4SLinus Torvalds 		   which returned after forwarding; they will be  dropped
2531da177e4SLinus Torvalds 		   by ip_mr_input in any case.
2541da177e4SLinus Torvalds 		   Note, that local frames are looped back to be delivered
2551da177e4SLinus Torvalds 		   to local recipients.
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 		   This check is duplicated in ip_mr_input at the moment.
2581da177e4SLinus Torvalds 		 */
2591da177e4SLinus Torvalds 		    && ((rt->rt_flags&RTCF_LOCAL) || !(IPCB(skb)->flags&IPSKB_FORWARDED))
2601da177e4SLinus Torvalds #endif
2611da177e4SLinus Torvalds 		) {
2621da177e4SLinus Torvalds 			struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
2631da177e4SLinus Torvalds 			if (newskb)
2641da177e4SLinus Torvalds 				NF_HOOK(PF_INET, NF_IP_POST_ROUTING, newskb, NULL,
2651da177e4SLinus Torvalds 					newskb->dev,
2661da177e4SLinus Torvalds 					ip_dev_loopback_xmit);
2671da177e4SLinus Torvalds 		}
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 		/* Multicasts with ttl 0 must not go beyond the host */
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 		if (skb->nh.iph->ttl == 0) {
2721da177e4SLinus Torvalds 			kfree_skb(skb);
2731da177e4SLinus Torvalds 			return 0;
2741da177e4SLinus Torvalds 		}
2751da177e4SLinus Torvalds 	}
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	if (rt->rt_flags&RTCF_BROADCAST) {
2781da177e4SLinus Torvalds 		struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
2791da177e4SLinus Torvalds 		if (newskb)
2801da177e4SLinus Torvalds 			NF_HOOK(PF_INET, NF_IP_POST_ROUTING, newskb, NULL,
2811da177e4SLinus Torvalds 				newskb->dev, ip_dev_loopback_xmit);
2821da177e4SLinus Torvalds 	}
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	if (skb->len > dst_mtu(&rt->u.dst))
2851da177e4SLinus Torvalds 		return ip_fragment(skb, ip_finish_output);
2861da177e4SLinus Torvalds 	else
2871da177e4SLinus Torvalds 		return ip_finish_output(skb);
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds int ip_output(struct sk_buff *skb)
2911da177e4SLinus Torvalds {
2921da177e4SLinus Torvalds 	IP_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	if (skb->len > dst_mtu(skb->dst) && !skb_shinfo(skb)->tso_size)
2951da177e4SLinus Torvalds 		return ip_fragment(skb, ip_finish_output);
2961da177e4SLinus Torvalds 	else
2971da177e4SLinus Torvalds 		return ip_finish_output(skb);
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds int ip_queue_xmit(struct sk_buff *skb, int ipfragok)
3011da177e4SLinus Torvalds {
3021da177e4SLinus Torvalds 	struct sock *sk = skb->sk;
3031da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
3041da177e4SLinus Torvalds 	struct ip_options *opt = inet->opt;
3051da177e4SLinus Torvalds 	struct rtable *rt;
3061da177e4SLinus Torvalds 	struct iphdr *iph;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	/* Skip all of this if the packet is already routed,
3091da177e4SLinus Torvalds 	 * f.e. by something like SCTP.
3101da177e4SLinus Torvalds 	 */
3111da177e4SLinus Torvalds 	rt = (struct rtable *) skb->dst;
3121da177e4SLinus Torvalds 	if (rt != NULL)
3131da177e4SLinus Torvalds 		goto packet_routed;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	/* Make sure we can route this packet. */
3161da177e4SLinus Torvalds 	rt = (struct rtable *)__sk_dst_check(sk, 0);
3171da177e4SLinus Torvalds 	if (rt == NULL) {
3181da177e4SLinus Torvalds 		u32 daddr;
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 		/* Use correct destination address if we have options. */
3211da177e4SLinus Torvalds 		daddr = inet->daddr;
3221da177e4SLinus Torvalds 		if(opt && opt->srr)
3231da177e4SLinus Torvalds 			daddr = opt->faddr;
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 		{
3261da177e4SLinus Torvalds 			struct flowi fl = { .oif = sk->sk_bound_dev_if,
3271da177e4SLinus Torvalds 					    .nl_u = { .ip4_u =
3281da177e4SLinus Torvalds 						      { .daddr = daddr,
3291da177e4SLinus Torvalds 							.saddr = inet->saddr,
3301da177e4SLinus Torvalds 							.tos = RT_CONN_FLAGS(sk) } },
3311da177e4SLinus Torvalds 					    .proto = sk->sk_protocol,
3321da177e4SLinus Torvalds 					    .uli_u = { .ports =
3331da177e4SLinus Torvalds 						       { .sport = inet->sport,
3341da177e4SLinus Torvalds 							 .dport = inet->dport } } };
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 			/* If this fails, retransmit mechanism of transport layer will
3371da177e4SLinus Torvalds 			 * keep trying until route appears or the connection times
3381da177e4SLinus Torvalds 			 * itself out.
3391da177e4SLinus Torvalds 			 */
3401da177e4SLinus Torvalds 			if (ip_route_output_flow(&rt, &fl, sk, 0))
3411da177e4SLinus Torvalds 				goto no_route;
3421da177e4SLinus Torvalds 		}
3431da177e4SLinus Torvalds 		__sk_dst_set(sk, &rt->u.dst);
3441da177e4SLinus Torvalds 		tcp_v4_setup_caps(sk, &rt->u.dst);
3451da177e4SLinus Torvalds 	}
3461da177e4SLinus Torvalds 	skb->dst = dst_clone(&rt->u.dst);
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds packet_routed:
3491da177e4SLinus Torvalds 	if (opt && opt->is_strictroute && rt->rt_dst != rt->rt_gateway)
3501da177e4SLinus Torvalds 		goto no_route;
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	/* OK, we know where to send it, allocate and build IP header. */
3531da177e4SLinus Torvalds 	iph = (struct iphdr *) skb_push(skb, sizeof(struct iphdr) + (opt ? opt->optlen : 0));
3541da177e4SLinus Torvalds 	*((__u16 *)iph)	= htons((4 << 12) | (5 << 8) | (inet->tos & 0xff));
3551da177e4SLinus Torvalds 	iph->tot_len = htons(skb->len);
3561da177e4SLinus Torvalds 	if (ip_dont_fragment(sk, &rt->u.dst) && !ipfragok)
3571da177e4SLinus Torvalds 		iph->frag_off = htons(IP_DF);
3581da177e4SLinus Torvalds 	else
3591da177e4SLinus Torvalds 		iph->frag_off = 0;
3601da177e4SLinus Torvalds 	iph->ttl      = ip_select_ttl(inet, &rt->u.dst);
3611da177e4SLinus Torvalds 	iph->protocol = sk->sk_protocol;
3621da177e4SLinus Torvalds 	iph->saddr    = rt->rt_src;
3631da177e4SLinus Torvalds 	iph->daddr    = rt->rt_dst;
3641da177e4SLinus Torvalds 	skb->nh.iph   = iph;
3651da177e4SLinus Torvalds 	/* Transport layer set skb->h.foo itself. */
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	if (opt && opt->optlen) {
3681da177e4SLinus Torvalds 		iph->ihl += opt->optlen >> 2;
3691da177e4SLinus Torvalds 		ip_options_build(skb, opt, inet->daddr, rt, 0);
3701da177e4SLinus Torvalds 	}
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	ip_select_ident_more(iph, &rt->u.dst, sk, skb_shinfo(skb)->tso_segs);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	/* Add an IP checksum. */
3751da177e4SLinus Torvalds 	ip_send_check(iph);
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
3801da177e4SLinus Torvalds 		       dst_output);
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds no_route:
3831da177e4SLinus Torvalds 	IP_INC_STATS(IPSTATS_MIB_OUTNOROUTES);
3841da177e4SLinus Torvalds 	kfree_skb(skb);
3851da177e4SLinus Torvalds 	return -EHOSTUNREACH;
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from)
3901da177e4SLinus Torvalds {
3911da177e4SLinus Torvalds 	to->pkt_type = from->pkt_type;
3921da177e4SLinus Torvalds 	to->priority = from->priority;
3931da177e4SLinus Torvalds 	to->protocol = from->protocol;
3941da177e4SLinus Torvalds 	to->security = from->security;
3951da177e4SLinus Torvalds 	dst_release(to->dst);
3961da177e4SLinus Torvalds 	to->dst = dst_clone(from->dst);
3971da177e4SLinus Torvalds 	to->dev = from->dev;
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	/* Copy the flags to each fragment. */
4001da177e4SLinus Torvalds 	IPCB(to)->flags = IPCB(from)->flags;
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds #ifdef CONFIG_NET_SCHED
4031da177e4SLinus Torvalds 	to->tc_index = from->tc_index;
4041da177e4SLinus Torvalds #endif
4051da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER
4061da177e4SLinus Torvalds 	to->nfmark = from->nfmark;
4071da177e4SLinus Torvalds 	to->nfcache = from->nfcache;
4081da177e4SLinus Torvalds 	/* Connection association is same as pre-frag packet */
4091da177e4SLinus Torvalds 	nf_conntrack_put(to->nfct);
4101da177e4SLinus Torvalds 	to->nfct = from->nfct;
4111da177e4SLinus Torvalds 	nf_conntrack_get(to->nfct);
4121da177e4SLinus Torvalds 	to->nfctinfo = from->nfctinfo;
4131da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER
4141da177e4SLinus Torvalds 	nf_bridge_put(to->nf_bridge);
4151da177e4SLinus Torvalds 	to->nf_bridge = from->nf_bridge;
4161da177e4SLinus Torvalds 	nf_bridge_get(to->nf_bridge);
4171da177e4SLinus Torvalds #endif
4181da177e4SLinus Torvalds #ifdef CONFIG_NETFILTER_DEBUG
4191da177e4SLinus Torvalds 	to->nf_debug = from->nf_debug;
4201da177e4SLinus Torvalds #endif
4211da177e4SLinus Torvalds #endif
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds /*
4251da177e4SLinus Torvalds  *	This IP datagram is too large to be sent in one piece.  Break it up into
4261da177e4SLinus Torvalds  *	smaller pieces (each of size equal to IP header plus
4271da177e4SLinus Torvalds  *	a block of the data of the original IP data part) that will yet fit in a
4281da177e4SLinus Torvalds  *	single device frame, and queue such a frame for sending.
4291da177e4SLinus Torvalds  */
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds int ip_fragment(struct sk_buff *skb, int (*output)(struct sk_buff*))
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	struct iphdr *iph;
4341da177e4SLinus Torvalds 	int raw = 0;
4351da177e4SLinus Torvalds 	int ptr;
4361da177e4SLinus Torvalds 	struct net_device *dev;
4371da177e4SLinus Torvalds 	struct sk_buff *skb2;
4381da177e4SLinus Torvalds 	unsigned int mtu, hlen, left, len, ll_rs;
4391da177e4SLinus Torvalds 	int offset;
4401da177e4SLinus Torvalds 	int not_last_frag;
4411da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable*)skb->dst;
4421da177e4SLinus Torvalds 	int err = 0;
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds 	dev = rt->u.dst.dev;
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	/*
4471da177e4SLinus Torvalds 	 *	Point into the IP datagram header.
4481da177e4SLinus Torvalds 	 */
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	iph = skb->nh.iph;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	if (unlikely((iph->frag_off & htons(IP_DF)) && !skb->local_df)) {
4531da177e4SLinus Torvalds 		icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
4541da177e4SLinus Torvalds 			  htonl(dst_mtu(&rt->u.dst)));
4551da177e4SLinus Torvalds 		kfree_skb(skb);
4561da177e4SLinus Torvalds 		return -EMSGSIZE;
4571da177e4SLinus Torvalds 	}
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	/*
4601da177e4SLinus Torvalds 	 *	Setup starting values.
4611da177e4SLinus Torvalds 	 */
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 	hlen = iph->ihl * 4;
4641da177e4SLinus Torvalds 	mtu = dst_mtu(&rt->u.dst) - hlen;	/* Size of data space */
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	/* When frag_list is given, use it. First, check its validity:
4671da177e4SLinus Torvalds 	 * some transformers could create wrong frag_list or break existing
4681da177e4SLinus Torvalds 	 * one, it is not prohibited. In this case fall back to copying.
4691da177e4SLinus Torvalds 	 *
4701da177e4SLinus Torvalds 	 * LATER: this step can be merged to real generation of fragments,
4711da177e4SLinus Torvalds 	 * we can switch to copy when see the first bad fragment.
4721da177e4SLinus Torvalds 	 */
4731da177e4SLinus Torvalds 	if (skb_shinfo(skb)->frag_list) {
4741da177e4SLinus Torvalds 		struct sk_buff *frag;
4751da177e4SLinus Torvalds 		int first_len = skb_pagelen(skb);
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 		if (first_len - hlen > mtu ||
4781da177e4SLinus Torvalds 		    ((first_len - hlen) & 7) ||
4791da177e4SLinus Torvalds 		    (iph->frag_off & htons(IP_MF|IP_OFFSET)) ||
4801da177e4SLinus Torvalds 		    skb_cloned(skb))
4811da177e4SLinus Torvalds 			goto slow_path;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 		for (frag = skb_shinfo(skb)->frag_list; frag; frag = frag->next) {
4841da177e4SLinus Torvalds 			/* Correct geometry. */
4851da177e4SLinus Torvalds 			if (frag->len > mtu ||
4861da177e4SLinus Torvalds 			    ((frag->len & 7) && frag->next) ||
4871da177e4SLinus Torvalds 			    skb_headroom(frag) < hlen)
4881da177e4SLinus Torvalds 			    goto slow_path;
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 			/* Partially cloned skb? */
4911da177e4SLinus Torvalds 			if (skb_shared(frag))
4921da177e4SLinus Torvalds 				goto slow_path;
4931da177e4SLinus Torvalds 		}
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 		/* Everything is OK. Generate! */
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 		err = 0;
4981da177e4SLinus Torvalds 		offset = 0;
4991da177e4SLinus Torvalds 		frag = skb_shinfo(skb)->frag_list;
5001da177e4SLinus Torvalds 		skb_shinfo(skb)->frag_list = NULL;
5011da177e4SLinus Torvalds 		skb->data_len = first_len - skb_headlen(skb);
5021da177e4SLinus Torvalds 		skb->len = first_len;
5031da177e4SLinus Torvalds 		iph->tot_len = htons(first_len);
5041da177e4SLinus Torvalds 		iph->frag_off = htons(IP_MF);
5051da177e4SLinus Torvalds 		ip_send_check(iph);
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 		for (;;) {
5081da177e4SLinus Torvalds 			/* Prepare header of the next frame,
5091da177e4SLinus Torvalds 			 * before previous one went down. */
5101da177e4SLinus Torvalds 			if (frag) {
5111da177e4SLinus Torvalds 				frag->ip_summed = CHECKSUM_NONE;
5121da177e4SLinus Torvalds 				frag->h.raw = frag->data;
5131da177e4SLinus Torvalds 				frag->nh.raw = __skb_push(frag, hlen);
5141da177e4SLinus Torvalds 				memcpy(frag->nh.raw, iph, hlen);
5151da177e4SLinus Torvalds 				iph = frag->nh.iph;
5161da177e4SLinus Torvalds 				iph->tot_len = htons(frag->len);
5171da177e4SLinus Torvalds 				ip_copy_metadata(frag, skb);
5181da177e4SLinus Torvalds 				if (offset == 0)
5191da177e4SLinus Torvalds 					ip_options_fragment(frag);
5201da177e4SLinus Torvalds 				offset += skb->len - hlen;
5211da177e4SLinus Torvalds 				iph->frag_off = htons(offset>>3);
5221da177e4SLinus Torvalds 				if (frag->next != NULL)
5231da177e4SLinus Torvalds 					iph->frag_off |= htons(IP_MF);
5241da177e4SLinus Torvalds 				/* Ready, complete checksum */
5251da177e4SLinus Torvalds 				ip_send_check(iph);
5261da177e4SLinus Torvalds 			}
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 			err = output(skb);
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 			if (err || !frag)
5311da177e4SLinus Torvalds 				break;
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 			skb = frag;
5341da177e4SLinus Torvalds 			frag = skb->next;
5351da177e4SLinus Torvalds 			skb->next = NULL;
5361da177e4SLinus Torvalds 		}
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 		if (err == 0) {
5391da177e4SLinus Torvalds 			IP_INC_STATS(IPSTATS_MIB_FRAGOKS);
5401da177e4SLinus Torvalds 			return 0;
5411da177e4SLinus Torvalds 		}
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 		while (frag) {
5441da177e4SLinus Torvalds 			skb = frag->next;
5451da177e4SLinus Torvalds 			kfree_skb(frag);
5461da177e4SLinus Torvalds 			frag = skb;
5471da177e4SLinus Torvalds 		}
5481da177e4SLinus Torvalds 		IP_INC_STATS(IPSTATS_MIB_FRAGFAILS);
5491da177e4SLinus Torvalds 		return err;
5501da177e4SLinus Torvalds 	}
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds slow_path:
5531da177e4SLinus Torvalds 	left = skb->len - hlen;		/* Space per frame */
5541da177e4SLinus Torvalds 	ptr = raw + hlen;		/* Where to start from */
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds #ifdef CONFIG_BRIDGE_NETFILTER
5571da177e4SLinus Torvalds 	/* for bridged IP traffic encapsulated inside f.e. a vlan header,
5581da177e4SLinus Torvalds 	 * we need to make room for the encapsulating header */
5591da177e4SLinus Torvalds 	ll_rs = LL_RESERVED_SPACE_EXTRA(rt->u.dst.dev, nf_bridge_pad(skb));
5601da177e4SLinus Torvalds 	mtu -= nf_bridge_pad(skb);
5611da177e4SLinus Torvalds #else
5621da177e4SLinus Torvalds 	ll_rs = LL_RESERVED_SPACE(rt->u.dst.dev);
5631da177e4SLinus Torvalds #endif
5641da177e4SLinus Torvalds 	/*
5651da177e4SLinus Torvalds 	 *	Fragment the datagram.
5661da177e4SLinus Torvalds 	 */
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3;
5691da177e4SLinus Torvalds 	not_last_frag = iph->frag_off & htons(IP_MF);
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	/*
5721da177e4SLinus Torvalds 	 *	Keep copying data until we run out.
5731da177e4SLinus Torvalds 	 */
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	while(left > 0)	{
5761da177e4SLinus Torvalds 		len = left;
5771da177e4SLinus Torvalds 		/* IF: it doesn't fit, use 'mtu' - the data space left */
5781da177e4SLinus Torvalds 		if (len > mtu)
5791da177e4SLinus Torvalds 			len = mtu;
5801da177e4SLinus Torvalds 		/* IF: we are not sending upto and including the packet end
5811da177e4SLinus Torvalds 		   then align the next start on an eight byte boundary */
5821da177e4SLinus Torvalds 		if (len < left)	{
5831da177e4SLinus Torvalds 			len &= ~7;
5841da177e4SLinus Torvalds 		}
5851da177e4SLinus Torvalds 		/*
5861da177e4SLinus Torvalds 		 *	Allocate buffer.
5871da177e4SLinus Torvalds 		 */
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 		if ((skb2 = alloc_skb(len+hlen+ll_rs, GFP_ATOMIC)) == NULL) {
5901da177e4SLinus Torvalds 			NETDEBUG(printk(KERN_INFO "IP: frag: no memory for new fragment!\n"));
5911da177e4SLinus Torvalds 			err = -ENOMEM;
5921da177e4SLinus Torvalds 			goto fail;
5931da177e4SLinus Torvalds 		}
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds 		/*
5961da177e4SLinus Torvalds 		 *	Set up data on packet
5971da177e4SLinus Torvalds 		 */
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 		ip_copy_metadata(skb2, skb);
6001da177e4SLinus Torvalds 		skb_reserve(skb2, ll_rs);
6011da177e4SLinus Torvalds 		skb_put(skb2, len + hlen);
6021da177e4SLinus Torvalds 		skb2->nh.raw = skb2->data;
6031da177e4SLinus Torvalds 		skb2->h.raw = skb2->data + hlen;
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 		/*
6061da177e4SLinus Torvalds 		 *	Charge the memory for the fragment to any owner
6071da177e4SLinus Torvalds 		 *	it might possess
6081da177e4SLinus Torvalds 		 */
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 		if (skb->sk)
6111da177e4SLinus Torvalds 			skb_set_owner_w(skb2, skb->sk);
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 		/*
6141da177e4SLinus Torvalds 		 *	Copy the packet header into the new buffer.
6151da177e4SLinus Torvalds 		 */
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 		memcpy(skb2->nh.raw, skb->data, hlen);
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 		/*
6201da177e4SLinus Torvalds 		 *	Copy a block of the IP datagram.
6211da177e4SLinus Torvalds 		 */
6221da177e4SLinus Torvalds 		if (skb_copy_bits(skb, ptr, skb2->h.raw, len))
6231da177e4SLinus Torvalds 			BUG();
6241da177e4SLinus Torvalds 		left -= len;
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 		/*
6271da177e4SLinus Torvalds 		 *	Fill in the new header fields.
6281da177e4SLinus Torvalds 		 */
6291da177e4SLinus Torvalds 		iph = skb2->nh.iph;
6301da177e4SLinus Torvalds 		iph->frag_off = htons((offset >> 3));
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 		/* ANK: dirty, but effective trick. Upgrade options only if
6331da177e4SLinus Torvalds 		 * the segment to be fragmented was THE FIRST (otherwise,
6341da177e4SLinus Torvalds 		 * options are already fixed) and make it ONCE
6351da177e4SLinus Torvalds 		 * on the initial skb, so that all the following fragments
6361da177e4SLinus Torvalds 		 * will inherit fixed options.
6371da177e4SLinus Torvalds 		 */
6381da177e4SLinus Torvalds 		if (offset == 0)
6391da177e4SLinus Torvalds 			ip_options_fragment(skb);
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 		/*
6421da177e4SLinus Torvalds 		 *	Added AC : If we are fragmenting a fragment that's not the
6431da177e4SLinus Torvalds 		 *		   last fragment then keep MF on each bit
6441da177e4SLinus Torvalds 		 */
6451da177e4SLinus Torvalds 		if (left > 0 || not_last_frag)
6461da177e4SLinus Torvalds 			iph->frag_off |= htons(IP_MF);
6471da177e4SLinus Torvalds 		ptr += len;
6481da177e4SLinus Torvalds 		offset += len;
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 		/*
6511da177e4SLinus Torvalds 		 *	Put this fragment into the sending queue.
6521da177e4SLinus Torvalds 		 */
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 		IP_INC_STATS(IPSTATS_MIB_FRAGCREATES);
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 		iph->tot_len = htons(len + hlen);
6571da177e4SLinus Torvalds 
6581da177e4SLinus Torvalds 		ip_send_check(iph);
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 		err = output(skb2);
6611da177e4SLinus Torvalds 		if (err)
6621da177e4SLinus Torvalds 			goto fail;
6631da177e4SLinus Torvalds 	}
6641da177e4SLinus Torvalds 	kfree_skb(skb);
6651da177e4SLinus Torvalds 	IP_INC_STATS(IPSTATS_MIB_FRAGOKS);
6661da177e4SLinus Torvalds 	return err;
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds fail:
6691da177e4SLinus Torvalds 	kfree_skb(skb);
6701da177e4SLinus Torvalds 	IP_INC_STATS(IPSTATS_MIB_FRAGFAILS);
6711da177e4SLinus Torvalds 	return err;
6721da177e4SLinus Torvalds }
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds int
6751da177e4SLinus Torvalds ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
6761da177e4SLinus Torvalds {
6771da177e4SLinus Torvalds 	struct iovec *iov = from;
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	if (skb->ip_summed == CHECKSUM_HW) {
6801da177e4SLinus Torvalds 		if (memcpy_fromiovecend(to, iov, offset, len) < 0)
6811da177e4SLinus Torvalds 			return -EFAULT;
6821da177e4SLinus Torvalds 	} else {
6831da177e4SLinus Torvalds 		unsigned int csum = 0;
6841da177e4SLinus Torvalds 		if (csum_partial_copy_fromiovecend(to, iov, offset, len, &csum) < 0)
6851da177e4SLinus Torvalds 			return -EFAULT;
6861da177e4SLinus Torvalds 		skb->csum = csum_block_add(skb->csum, csum, odd);
6871da177e4SLinus Torvalds 	}
6881da177e4SLinus Torvalds 	return 0;
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds static inline unsigned int
6921da177e4SLinus Torvalds csum_page(struct page *page, int offset, int copy)
6931da177e4SLinus Torvalds {
6941da177e4SLinus Torvalds 	char *kaddr;
6951da177e4SLinus Torvalds 	unsigned int csum;
6961da177e4SLinus Torvalds 	kaddr = kmap(page);
6971da177e4SLinus Torvalds 	csum = csum_partial(kaddr + offset, copy, 0);
6981da177e4SLinus Torvalds 	kunmap(page);
6991da177e4SLinus Torvalds 	return csum;
7001da177e4SLinus Torvalds }
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds /*
7031da177e4SLinus Torvalds  *	ip_append_data() and ip_append_page() can make one large IP datagram
7041da177e4SLinus Torvalds  *	from many pieces of data. Each pieces will be holded on the socket
7051da177e4SLinus Torvalds  *	until ip_push_pending_frames() is called. Each piece can be a page
7061da177e4SLinus Torvalds  *	or non-page data.
7071da177e4SLinus Torvalds  *
7081da177e4SLinus Torvalds  *	Not only UDP, other transport protocols - e.g. raw sockets - can use
7091da177e4SLinus Torvalds  *	this interface potentially.
7101da177e4SLinus Torvalds  *
7111da177e4SLinus Torvalds  *	LATER: length must be adjusted by pad at tail, when it is required.
7121da177e4SLinus Torvalds  */
7131da177e4SLinus Torvalds int ip_append_data(struct sock *sk,
7141da177e4SLinus Torvalds 		   int getfrag(void *from, char *to, int offset, int len,
7151da177e4SLinus Torvalds 			       int odd, struct sk_buff *skb),
7161da177e4SLinus Torvalds 		   void *from, int length, int transhdrlen,
7171da177e4SLinus Torvalds 		   struct ipcm_cookie *ipc, struct rtable *rt,
7181da177e4SLinus Torvalds 		   unsigned int flags)
7191da177e4SLinus Torvalds {
7201da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
7211da177e4SLinus Torvalds 	struct sk_buff *skb;
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	struct ip_options *opt = NULL;
7241da177e4SLinus Torvalds 	int hh_len;
7251da177e4SLinus Torvalds 	int exthdrlen;
7261da177e4SLinus Torvalds 	int mtu;
7271da177e4SLinus Torvalds 	int copy;
7281da177e4SLinus Torvalds 	int err;
7291da177e4SLinus Torvalds 	int offset = 0;
7301da177e4SLinus Torvalds 	unsigned int maxfraglen, fragheaderlen;
7311da177e4SLinus Torvalds 	int csummode = CHECKSUM_NONE;
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	if (flags&MSG_PROBE)
7341da177e4SLinus Torvalds 		return 0;
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	if (skb_queue_empty(&sk->sk_write_queue)) {
7371da177e4SLinus Torvalds 		/*
7381da177e4SLinus Torvalds 		 * setup for corking.
7391da177e4SLinus Torvalds 		 */
7401da177e4SLinus Torvalds 		opt = ipc->opt;
7411da177e4SLinus Torvalds 		if (opt) {
7421da177e4SLinus Torvalds 			if (inet->cork.opt == NULL) {
7431da177e4SLinus Torvalds 				inet->cork.opt = kmalloc(sizeof(struct ip_options) + 40, sk->sk_allocation);
7441da177e4SLinus Torvalds 				if (unlikely(inet->cork.opt == NULL))
7451da177e4SLinus Torvalds 					return -ENOBUFS;
7461da177e4SLinus Torvalds 			}
7471da177e4SLinus Torvalds 			memcpy(inet->cork.opt, opt, sizeof(struct ip_options)+opt->optlen);
7481da177e4SLinus Torvalds 			inet->cork.flags |= IPCORK_OPT;
7491da177e4SLinus Torvalds 			inet->cork.addr = ipc->addr;
7501da177e4SLinus Torvalds 		}
7511da177e4SLinus Torvalds 		dst_hold(&rt->u.dst);
7521da177e4SLinus Torvalds 		inet->cork.fragsize = mtu = dst_mtu(rt->u.dst.path);
7531da177e4SLinus Torvalds 		inet->cork.rt = rt;
7541da177e4SLinus Torvalds 		inet->cork.length = 0;
7551da177e4SLinus Torvalds 		sk->sk_sndmsg_page = NULL;
7561da177e4SLinus Torvalds 		sk->sk_sndmsg_off = 0;
7571da177e4SLinus Torvalds 		if ((exthdrlen = rt->u.dst.header_len) != 0) {
7581da177e4SLinus Torvalds 			length += exthdrlen;
7591da177e4SLinus Torvalds 			transhdrlen += exthdrlen;
7601da177e4SLinus Torvalds 		}
7611da177e4SLinus Torvalds 	} else {
7621da177e4SLinus Torvalds 		rt = inet->cork.rt;
7631da177e4SLinus Torvalds 		if (inet->cork.flags & IPCORK_OPT)
7641da177e4SLinus Torvalds 			opt = inet->cork.opt;
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 		transhdrlen = 0;
7671da177e4SLinus Torvalds 		exthdrlen = 0;
7681da177e4SLinus Torvalds 		mtu = inet->cork.fragsize;
7691da177e4SLinus Torvalds 	}
7701da177e4SLinus Torvalds 	hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
7731da177e4SLinus Torvalds 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	if (inet->cork.length + length > 0xFFFF - fragheaderlen) {
7761da177e4SLinus Torvalds 		ip_local_error(sk, EMSGSIZE, rt->rt_dst, inet->dport, mtu-exthdrlen);
7771da177e4SLinus Torvalds 		return -EMSGSIZE;
7781da177e4SLinus Torvalds 	}
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	/*
7811da177e4SLinus Torvalds 	 * transhdrlen > 0 means that this is the first fragment and we wish
7821da177e4SLinus Torvalds 	 * it won't be fragmented in the future.
7831da177e4SLinus Torvalds 	 */
7841da177e4SLinus Torvalds 	if (transhdrlen &&
7851da177e4SLinus Torvalds 	    length + fragheaderlen <= mtu &&
7861da177e4SLinus Torvalds 	    rt->u.dst.dev->features&(NETIF_F_IP_CSUM|NETIF_F_NO_CSUM|NETIF_F_HW_CSUM) &&
7871da177e4SLinus Torvalds 	    !exthdrlen)
7881da177e4SLinus Torvalds 		csummode = CHECKSUM_HW;
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	inet->cork.length += length;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	/* So, what's going on in the loop below?
7931da177e4SLinus Torvalds 	 *
7941da177e4SLinus Torvalds 	 * We use calculated fragment length to generate chained skb,
7951da177e4SLinus Torvalds 	 * each of segments is IP fragment ready for sending to network after
7961da177e4SLinus Torvalds 	 * adding appropriate IP header.
7971da177e4SLinus Torvalds 	 */
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL)
8001da177e4SLinus Torvalds 		goto alloc_new_skb;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	while (length > 0) {
8031da177e4SLinus Torvalds 		/* Check if the remaining data fits into current packet. */
8041da177e4SLinus Torvalds 		copy = mtu - skb->len;
8051da177e4SLinus Torvalds 		if (copy < length)
8061da177e4SLinus Torvalds 			copy = maxfraglen - skb->len;
8071da177e4SLinus Torvalds 		if (copy <= 0) {
8081da177e4SLinus Torvalds 			char *data;
8091da177e4SLinus Torvalds 			unsigned int datalen;
8101da177e4SLinus Torvalds 			unsigned int fraglen;
8111da177e4SLinus Torvalds 			unsigned int fraggap;
8121da177e4SLinus Torvalds 			unsigned int alloclen;
8131da177e4SLinus Torvalds 			struct sk_buff *skb_prev;
8141da177e4SLinus Torvalds alloc_new_skb:
8151da177e4SLinus Torvalds 			skb_prev = skb;
8161da177e4SLinus Torvalds 			if (skb_prev)
8171da177e4SLinus Torvalds 				fraggap = skb_prev->len - maxfraglen;
8181da177e4SLinus Torvalds 			else
8191da177e4SLinus Torvalds 				fraggap = 0;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 			/*
8221da177e4SLinus Torvalds 			 * If remaining data exceeds the mtu,
8231da177e4SLinus Torvalds 			 * we know we need more fragment(s).
8241da177e4SLinus Torvalds 			 */
8251da177e4SLinus Torvalds 			datalen = length + fraggap;
8261da177e4SLinus Torvalds 			if (datalen > mtu - fragheaderlen)
8271da177e4SLinus Torvalds 				datalen = maxfraglen - fragheaderlen;
8281da177e4SLinus Torvalds 			fraglen = datalen + fragheaderlen;
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 			if ((flags & MSG_MORE) &&
8311da177e4SLinus Torvalds 			    !(rt->u.dst.dev->features&NETIF_F_SG))
8321da177e4SLinus Torvalds 				alloclen = mtu;
8331da177e4SLinus Torvalds 			else
8341da177e4SLinus Torvalds 				alloclen = datalen + fragheaderlen;
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 			/* The last fragment gets additional space at tail.
8371da177e4SLinus Torvalds 			 * Note, with MSG_MORE we overallocate on fragments,
8381da177e4SLinus Torvalds 			 * because we have no idea what fragment will be
8391da177e4SLinus Torvalds 			 * the last.
8401da177e4SLinus Torvalds 			 */
8411da177e4SLinus Torvalds 			if (datalen == length)
8421da177e4SLinus Torvalds 				alloclen += rt->u.dst.trailer_len;
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 			if (transhdrlen) {
8451da177e4SLinus Torvalds 				skb = sock_alloc_send_skb(sk,
8461da177e4SLinus Torvalds 						alloclen + hh_len + 15,
8471da177e4SLinus Torvalds 						(flags & MSG_DONTWAIT), &err);
8481da177e4SLinus Torvalds 			} else {
8491da177e4SLinus Torvalds 				skb = NULL;
8501da177e4SLinus Torvalds 				if (atomic_read(&sk->sk_wmem_alloc) <=
8511da177e4SLinus Torvalds 				    2 * sk->sk_sndbuf)
8521da177e4SLinus Torvalds 					skb = sock_wmalloc(sk,
8531da177e4SLinus Torvalds 							   alloclen + hh_len + 15, 1,
8541da177e4SLinus Torvalds 							   sk->sk_allocation);
8551da177e4SLinus Torvalds 				if (unlikely(skb == NULL))
8561da177e4SLinus Torvalds 					err = -ENOBUFS;
8571da177e4SLinus Torvalds 			}
8581da177e4SLinus Torvalds 			if (skb == NULL)
8591da177e4SLinus Torvalds 				goto error;
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 			/*
8621da177e4SLinus Torvalds 			 *	Fill in the control structures
8631da177e4SLinus Torvalds 			 */
8641da177e4SLinus Torvalds 			skb->ip_summed = csummode;
8651da177e4SLinus Torvalds 			skb->csum = 0;
8661da177e4SLinus Torvalds 			skb_reserve(skb, hh_len);
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 			/*
8691da177e4SLinus Torvalds 			 *	Find where to start putting bytes.
8701da177e4SLinus Torvalds 			 */
8711da177e4SLinus Torvalds 			data = skb_put(skb, fraglen);
8721da177e4SLinus Torvalds 			skb->nh.raw = data + exthdrlen;
8731da177e4SLinus Torvalds 			data += fragheaderlen;
8741da177e4SLinus Torvalds 			skb->h.raw = data + exthdrlen;
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 			if (fraggap) {
8771da177e4SLinus Torvalds 				skb->csum = skb_copy_and_csum_bits(
8781da177e4SLinus Torvalds 					skb_prev, maxfraglen,
8791da177e4SLinus Torvalds 					data + transhdrlen, fraggap, 0);
8801da177e4SLinus Torvalds 				skb_prev->csum = csum_sub(skb_prev->csum,
8811da177e4SLinus Torvalds 							  skb->csum);
8821da177e4SLinus Torvalds 				data += fraggap;
8831da177e4SLinus Torvalds 				skb_trim(skb_prev, maxfraglen);
8841da177e4SLinus Torvalds 			}
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 			copy = datalen - transhdrlen - fraggap;
8871da177e4SLinus Torvalds 			if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
8881da177e4SLinus Torvalds 				err = -EFAULT;
8891da177e4SLinus Torvalds 				kfree_skb(skb);
8901da177e4SLinus Torvalds 				goto error;
8911da177e4SLinus Torvalds 			}
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds 			offset += copy;
8941da177e4SLinus Torvalds 			length -= datalen - fraggap;
8951da177e4SLinus Torvalds 			transhdrlen = 0;
8961da177e4SLinus Torvalds 			exthdrlen = 0;
8971da177e4SLinus Torvalds 			csummode = CHECKSUM_NONE;
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 			/*
9001da177e4SLinus Torvalds 			 * Put the packet on the pending queue.
9011da177e4SLinus Torvalds 			 */
9021da177e4SLinus Torvalds 			__skb_queue_tail(&sk->sk_write_queue, skb);
9031da177e4SLinus Torvalds 			continue;
9041da177e4SLinus Torvalds 		}
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 		if (copy > length)
9071da177e4SLinus Torvalds 			copy = length;
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds 		if (!(rt->u.dst.dev->features&NETIF_F_SG)) {
9101da177e4SLinus Torvalds 			unsigned int off;
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 			off = skb->len;
9131da177e4SLinus Torvalds 			if (getfrag(from, skb_put(skb, copy),
9141da177e4SLinus Torvalds 					offset, copy, off, skb) < 0) {
9151da177e4SLinus Torvalds 				__skb_trim(skb, off);
9161da177e4SLinus Torvalds 				err = -EFAULT;
9171da177e4SLinus Torvalds 				goto error;
9181da177e4SLinus Torvalds 			}
9191da177e4SLinus Torvalds 		} else {
9201da177e4SLinus Torvalds 			int i = skb_shinfo(skb)->nr_frags;
9211da177e4SLinus Torvalds 			skb_frag_t *frag = &skb_shinfo(skb)->frags[i-1];
9221da177e4SLinus Torvalds 			struct page *page = sk->sk_sndmsg_page;
9231da177e4SLinus Torvalds 			int off = sk->sk_sndmsg_off;
9241da177e4SLinus Torvalds 			unsigned int left;
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 			if (page && (left = PAGE_SIZE - off) > 0) {
9271da177e4SLinus Torvalds 				if (copy >= left)
9281da177e4SLinus Torvalds 					copy = left;
9291da177e4SLinus Torvalds 				if (page != frag->page) {
9301da177e4SLinus Torvalds 					if (i == MAX_SKB_FRAGS) {
9311da177e4SLinus Torvalds 						err = -EMSGSIZE;
9321da177e4SLinus Torvalds 						goto error;
9331da177e4SLinus Torvalds 					}
9341da177e4SLinus Torvalds 					get_page(page);
9351da177e4SLinus Torvalds 	 				skb_fill_page_desc(skb, i, page, sk->sk_sndmsg_off, 0);
9361da177e4SLinus Torvalds 					frag = &skb_shinfo(skb)->frags[i];
9371da177e4SLinus Torvalds 				}
9381da177e4SLinus Torvalds 			} else if (i < MAX_SKB_FRAGS) {
9391da177e4SLinus Torvalds 				if (copy > PAGE_SIZE)
9401da177e4SLinus Torvalds 					copy = PAGE_SIZE;
9411da177e4SLinus Torvalds 				page = alloc_pages(sk->sk_allocation, 0);
9421da177e4SLinus Torvalds 				if (page == NULL)  {
9431da177e4SLinus Torvalds 					err = -ENOMEM;
9441da177e4SLinus Torvalds 					goto error;
9451da177e4SLinus Torvalds 				}
9461da177e4SLinus Torvalds 				sk->sk_sndmsg_page = page;
9471da177e4SLinus Torvalds 				sk->sk_sndmsg_off = 0;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 				skb_fill_page_desc(skb, i, page, 0, 0);
9501da177e4SLinus Torvalds 				frag = &skb_shinfo(skb)->frags[i];
9511da177e4SLinus Torvalds 				skb->truesize += PAGE_SIZE;
9521da177e4SLinus Torvalds 				atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
9531da177e4SLinus Torvalds 			} else {
9541da177e4SLinus Torvalds 				err = -EMSGSIZE;
9551da177e4SLinus Torvalds 				goto error;
9561da177e4SLinus Torvalds 			}
9571da177e4SLinus Torvalds 			if (getfrag(from, page_address(frag->page)+frag->page_offset+frag->size, offset, copy, skb->len, skb) < 0) {
9581da177e4SLinus Torvalds 				err = -EFAULT;
9591da177e4SLinus Torvalds 				goto error;
9601da177e4SLinus Torvalds 			}
9611da177e4SLinus Torvalds 			sk->sk_sndmsg_off += copy;
9621da177e4SLinus Torvalds 			frag->size += copy;
9631da177e4SLinus Torvalds 			skb->len += copy;
9641da177e4SLinus Torvalds 			skb->data_len += copy;
9651da177e4SLinus Torvalds 		}
9661da177e4SLinus Torvalds 		offset += copy;
9671da177e4SLinus Torvalds 		length -= copy;
9681da177e4SLinus Torvalds 	}
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	return 0;
9711da177e4SLinus Torvalds 
9721da177e4SLinus Torvalds error:
9731da177e4SLinus Torvalds 	inet->cork.length -= length;
9741da177e4SLinus Torvalds 	IP_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
9751da177e4SLinus Torvalds 	return err;
9761da177e4SLinus Torvalds }
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds ssize_t	ip_append_page(struct sock *sk, struct page *page,
9791da177e4SLinus Torvalds 		       int offset, size_t size, int flags)
9801da177e4SLinus Torvalds {
9811da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
9821da177e4SLinus Torvalds 	struct sk_buff *skb;
9831da177e4SLinus Torvalds 	struct rtable *rt;
9841da177e4SLinus Torvalds 	struct ip_options *opt = NULL;
9851da177e4SLinus Torvalds 	int hh_len;
9861da177e4SLinus Torvalds 	int mtu;
9871da177e4SLinus Torvalds 	int len;
9881da177e4SLinus Torvalds 	int err;
9891da177e4SLinus Torvalds 	unsigned int maxfraglen, fragheaderlen, fraggap;
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds 	if (inet->hdrincl)
9921da177e4SLinus Torvalds 		return -EPERM;
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 	if (flags&MSG_PROBE)
9951da177e4SLinus Torvalds 		return 0;
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds 	if (skb_queue_empty(&sk->sk_write_queue))
9981da177e4SLinus Torvalds 		return -EINVAL;
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds 	rt = inet->cork.rt;
10011da177e4SLinus Torvalds 	if (inet->cork.flags & IPCORK_OPT)
10021da177e4SLinus Torvalds 		opt = inet->cork.opt;
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	if (!(rt->u.dst.dev->features&NETIF_F_SG))
10051da177e4SLinus Torvalds 		return -EOPNOTSUPP;
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
10081da177e4SLinus Torvalds 	mtu = inet->cork.fragsize;
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds 	fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
10111da177e4SLinus Torvalds 	maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 	if (inet->cork.length + size > 0xFFFF - fragheaderlen) {
10141da177e4SLinus Torvalds 		ip_local_error(sk, EMSGSIZE, rt->rt_dst, inet->dport, mtu);
10151da177e4SLinus Torvalds 		return -EMSGSIZE;
10161da177e4SLinus Torvalds 	}
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL)
10191da177e4SLinus Torvalds 		return -EINVAL;
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	inet->cork.length += size;
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	while (size > 0) {
10241da177e4SLinus Torvalds 		int i;
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 		/* Check if the remaining data fits into current packet. */
10271da177e4SLinus Torvalds 		len = mtu - skb->len;
10281da177e4SLinus Torvalds 		if (len < size)
10291da177e4SLinus Torvalds 			len = maxfraglen - skb->len;
10301da177e4SLinus Torvalds 		if (len <= 0) {
10311da177e4SLinus Torvalds 			struct sk_buff *skb_prev;
10321da177e4SLinus Torvalds 			char *data;
10331da177e4SLinus Torvalds 			struct iphdr *iph;
10341da177e4SLinus Torvalds 			int alloclen;
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 			skb_prev = skb;
10371da177e4SLinus Torvalds 			if (skb_prev)
10381da177e4SLinus Torvalds 				fraggap = skb_prev->len - maxfraglen;
10391da177e4SLinus Torvalds 			else
10401da177e4SLinus Torvalds 				fraggap = 0;
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 			alloclen = fragheaderlen + hh_len + fraggap + 15;
10431da177e4SLinus Torvalds 			skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation);
10441da177e4SLinus Torvalds 			if (unlikely(!skb)) {
10451da177e4SLinus Torvalds 				err = -ENOBUFS;
10461da177e4SLinus Torvalds 				goto error;
10471da177e4SLinus Torvalds 			}
10481da177e4SLinus Torvalds 
10491da177e4SLinus Torvalds 			/*
10501da177e4SLinus Torvalds 			 *	Fill in the control structures
10511da177e4SLinus Torvalds 			 */
10521da177e4SLinus Torvalds 			skb->ip_summed = CHECKSUM_NONE;
10531da177e4SLinus Torvalds 			skb->csum = 0;
10541da177e4SLinus Torvalds 			skb_reserve(skb, hh_len);
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 			/*
10571da177e4SLinus Torvalds 			 *	Find where to start putting bytes.
10581da177e4SLinus Torvalds 			 */
10591da177e4SLinus Torvalds 			data = skb_put(skb, fragheaderlen + fraggap);
10601da177e4SLinus Torvalds 			skb->nh.iph = iph = (struct iphdr *)data;
10611da177e4SLinus Torvalds 			data += fragheaderlen;
10621da177e4SLinus Torvalds 			skb->h.raw = data;
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds 			if (fraggap) {
10651da177e4SLinus Torvalds 				skb->csum = skb_copy_and_csum_bits(
10661da177e4SLinus Torvalds 					skb_prev, maxfraglen,
10671da177e4SLinus Torvalds 					data, fraggap, 0);
10681da177e4SLinus Torvalds 				skb_prev->csum = csum_sub(skb_prev->csum,
10691da177e4SLinus Torvalds 							  skb->csum);
10701da177e4SLinus Torvalds 				skb_trim(skb_prev, maxfraglen);
10711da177e4SLinus Torvalds 			}
10721da177e4SLinus Torvalds 
10731da177e4SLinus Torvalds 			/*
10741da177e4SLinus Torvalds 			 * Put the packet on the pending queue.
10751da177e4SLinus Torvalds 			 */
10761da177e4SLinus Torvalds 			__skb_queue_tail(&sk->sk_write_queue, skb);
10771da177e4SLinus Torvalds 			continue;
10781da177e4SLinus Torvalds 		}
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds 		i = skb_shinfo(skb)->nr_frags;
10811da177e4SLinus Torvalds 		if (len > size)
10821da177e4SLinus Torvalds 			len = size;
10831da177e4SLinus Torvalds 		if (skb_can_coalesce(skb, i, page, offset)) {
10841da177e4SLinus Torvalds 			skb_shinfo(skb)->frags[i-1].size += len;
10851da177e4SLinus Torvalds 		} else if (i < MAX_SKB_FRAGS) {
10861da177e4SLinus Torvalds 			get_page(page);
10871da177e4SLinus Torvalds 			skb_fill_page_desc(skb, i, page, offset, len);
10881da177e4SLinus Torvalds 		} else {
10891da177e4SLinus Torvalds 			err = -EMSGSIZE;
10901da177e4SLinus Torvalds 			goto error;
10911da177e4SLinus Torvalds 		}
10921da177e4SLinus Torvalds 
10931da177e4SLinus Torvalds 		if (skb->ip_summed == CHECKSUM_NONE) {
10941da177e4SLinus Torvalds 			unsigned int csum;
10951da177e4SLinus Torvalds 			csum = csum_page(page, offset, len);
10961da177e4SLinus Torvalds 			skb->csum = csum_block_add(skb->csum, csum, skb->len);
10971da177e4SLinus Torvalds 		}
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 		skb->len += len;
11001da177e4SLinus Torvalds 		skb->data_len += len;
11011da177e4SLinus Torvalds 		offset += len;
11021da177e4SLinus Torvalds 		size -= len;
11031da177e4SLinus Torvalds 	}
11041da177e4SLinus Torvalds 	return 0;
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds error:
11071da177e4SLinus Torvalds 	inet->cork.length -= size;
11081da177e4SLinus Torvalds 	IP_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
11091da177e4SLinus Torvalds 	return err;
11101da177e4SLinus Torvalds }
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds /*
11131da177e4SLinus Torvalds  *	Combined all pending IP fragments on the socket as one IP datagram
11141da177e4SLinus Torvalds  *	and push them out.
11151da177e4SLinus Torvalds  */
11161da177e4SLinus Torvalds int ip_push_pending_frames(struct sock *sk)
11171da177e4SLinus Torvalds {
11181da177e4SLinus Torvalds 	struct sk_buff *skb, *tmp_skb;
11191da177e4SLinus Torvalds 	struct sk_buff **tail_skb;
11201da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
11211da177e4SLinus Torvalds 	struct ip_options *opt = NULL;
11221da177e4SLinus Torvalds 	struct rtable *rt = inet->cork.rt;
11231da177e4SLinus Torvalds 	struct iphdr *iph;
11241da177e4SLinus Torvalds 	int df = 0;
11251da177e4SLinus Torvalds 	__u8 ttl;
11261da177e4SLinus Torvalds 	int err = 0;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	if ((skb = __skb_dequeue(&sk->sk_write_queue)) == NULL)
11291da177e4SLinus Torvalds 		goto out;
11301da177e4SLinus Torvalds 	tail_skb = &(skb_shinfo(skb)->frag_list);
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 	/* move skb->data to ip header from ext header */
11331da177e4SLinus Torvalds 	if (skb->data < skb->nh.raw)
11341da177e4SLinus Torvalds 		__skb_pull(skb, skb->nh.raw - skb->data);
11351da177e4SLinus Torvalds 	while ((tmp_skb = __skb_dequeue(&sk->sk_write_queue)) != NULL) {
11361da177e4SLinus Torvalds 		__skb_pull(tmp_skb, skb->h.raw - skb->nh.raw);
11371da177e4SLinus Torvalds 		*tail_skb = tmp_skb;
11381da177e4SLinus Torvalds 		tail_skb = &(tmp_skb->next);
11391da177e4SLinus Torvalds 		skb->len += tmp_skb->len;
11401da177e4SLinus Torvalds 		skb->data_len += tmp_skb->len;
11411da177e4SLinus Torvalds 		skb->truesize += tmp_skb->truesize;
11421da177e4SLinus Torvalds 		__sock_put(tmp_skb->sk);
11431da177e4SLinus Torvalds 		tmp_skb->destructor = NULL;
11441da177e4SLinus Torvalds 		tmp_skb->sk = NULL;
11451da177e4SLinus Torvalds 	}
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	/* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow
11481da177e4SLinus Torvalds 	 * to fragment the frame generated here. No matter, what transforms
11491da177e4SLinus Torvalds 	 * how transforms change size of the packet, it will come out.
11501da177e4SLinus Torvalds 	 */
11511da177e4SLinus Torvalds 	if (inet->pmtudisc != IP_PMTUDISC_DO)
11521da177e4SLinus Torvalds 		skb->local_df = 1;
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds 	/* DF bit is set when we want to see DF on outgoing frames.
11551da177e4SLinus Torvalds 	 * If local_df is set too, we still allow to fragment this frame
11561da177e4SLinus Torvalds 	 * locally. */
11571da177e4SLinus Torvalds 	if (inet->pmtudisc == IP_PMTUDISC_DO ||
11581da177e4SLinus Torvalds 	    (skb->len <= dst_mtu(&rt->u.dst) &&
11591da177e4SLinus Torvalds 	     ip_dont_fragment(sk, &rt->u.dst)))
11601da177e4SLinus Torvalds 		df = htons(IP_DF);
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 	if (inet->cork.flags & IPCORK_OPT)
11631da177e4SLinus Torvalds 		opt = inet->cork.opt;
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	if (rt->rt_type == RTN_MULTICAST)
11661da177e4SLinus Torvalds 		ttl = inet->mc_ttl;
11671da177e4SLinus Torvalds 	else
11681da177e4SLinus Torvalds 		ttl = ip_select_ttl(inet, &rt->u.dst);
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	iph = (struct iphdr *)skb->data;
11711da177e4SLinus Torvalds 	iph->version = 4;
11721da177e4SLinus Torvalds 	iph->ihl = 5;
11731da177e4SLinus Torvalds 	if (opt) {
11741da177e4SLinus Torvalds 		iph->ihl += opt->optlen>>2;
11751da177e4SLinus Torvalds 		ip_options_build(skb, opt, inet->cork.addr, rt, 0);
11761da177e4SLinus Torvalds 	}
11771da177e4SLinus Torvalds 	iph->tos = inet->tos;
11781da177e4SLinus Torvalds 	iph->tot_len = htons(skb->len);
11791da177e4SLinus Torvalds 	iph->frag_off = df;
11801da177e4SLinus Torvalds 	if (!df) {
11811da177e4SLinus Torvalds 		__ip_select_ident(iph, &rt->u.dst, 0);
11821da177e4SLinus Torvalds 	} else {
11831da177e4SLinus Torvalds 		iph->id = htons(inet->id++);
11841da177e4SLinus Torvalds 	}
11851da177e4SLinus Torvalds 	iph->ttl = ttl;
11861da177e4SLinus Torvalds 	iph->protocol = sk->sk_protocol;
11871da177e4SLinus Torvalds 	iph->saddr = rt->rt_src;
11881da177e4SLinus Torvalds 	iph->daddr = rt->rt_dst;
11891da177e4SLinus Torvalds 	ip_send_check(iph);
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 	skb->priority = sk->sk_priority;
11921da177e4SLinus Torvalds 	skb->dst = dst_clone(&rt->u.dst);
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds 	/* Netfilter gets whole the not fragmented skb. */
11951da177e4SLinus Torvalds 	err = NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL,
11961da177e4SLinus Torvalds 		      skb->dst->dev, dst_output);
11971da177e4SLinus Torvalds 	if (err) {
11981da177e4SLinus Torvalds 		if (err > 0)
11991da177e4SLinus Torvalds 			err = inet->recverr ? net_xmit_errno(err) : 0;
12001da177e4SLinus Torvalds 		if (err)
12011da177e4SLinus Torvalds 			goto error;
12021da177e4SLinus Torvalds 	}
12031da177e4SLinus Torvalds 
12041da177e4SLinus Torvalds out:
12051da177e4SLinus Torvalds 	inet->cork.flags &= ~IPCORK_OPT;
12061da177e4SLinus Torvalds 	if (inet->cork.opt) {
12071da177e4SLinus Torvalds 		kfree(inet->cork.opt);
12081da177e4SLinus Torvalds 		inet->cork.opt = NULL;
12091da177e4SLinus Torvalds 	}
12101da177e4SLinus Torvalds 	if (inet->cork.rt) {
12111da177e4SLinus Torvalds 		ip_rt_put(inet->cork.rt);
12121da177e4SLinus Torvalds 		inet->cork.rt = NULL;
12131da177e4SLinus Torvalds 	}
12141da177e4SLinus Torvalds 	return err;
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds error:
12171da177e4SLinus Torvalds 	IP_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
12181da177e4SLinus Torvalds 	goto out;
12191da177e4SLinus Torvalds }
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds /*
12221da177e4SLinus Torvalds  *	Throw away all pending data on the socket.
12231da177e4SLinus Torvalds  */
12241da177e4SLinus Torvalds void ip_flush_pending_frames(struct sock *sk)
12251da177e4SLinus Torvalds {
12261da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
12271da177e4SLinus Torvalds 	struct sk_buff *skb;
12281da177e4SLinus Torvalds 
12291da177e4SLinus Torvalds 	while ((skb = __skb_dequeue_tail(&sk->sk_write_queue)) != NULL)
12301da177e4SLinus Torvalds 		kfree_skb(skb);
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 	inet->cork.flags &= ~IPCORK_OPT;
12331da177e4SLinus Torvalds 	if (inet->cork.opt) {
12341da177e4SLinus Torvalds 		kfree(inet->cork.opt);
12351da177e4SLinus Torvalds 		inet->cork.opt = NULL;
12361da177e4SLinus Torvalds 	}
12371da177e4SLinus Torvalds 	if (inet->cork.rt) {
12381da177e4SLinus Torvalds 		ip_rt_put(inet->cork.rt);
12391da177e4SLinus Torvalds 		inet->cork.rt = NULL;
12401da177e4SLinus Torvalds 	}
12411da177e4SLinus Torvalds }
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 
12441da177e4SLinus Torvalds /*
12451da177e4SLinus Torvalds  *	Fetch data from kernel space and fill in checksum if needed.
12461da177e4SLinus Torvalds  */
12471da177e4SLinus Torvalds static int ip_reply_glue_bits(void *dptr, char *to, int offset,
12481da177e4SLinus Torvalds 			      int len, int odd, struct sk_buff *skb)
12491da177e4SLinus Torvalds {
12501da177e4SLinus Torvalds 	unsigned int csum;
12511da177e4SLinus Torvalds 
12521da177e4SLinus Torvalds 	csum = csum_partial_copy_nocheck(dptr+offset, to, len, 0);
12531da177e4SLinus Torvalds 	skb->csum = csum_block_add(skb->csum, csum, odd);
12541da177e4SLinus Torvalds 	return 0;
12551da177e4SLinus Torvalds }
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds /*
12581da177e4SLinus Torvalds  *	Generic function to send a packet as reply to another packet.
12591da177e4SLinus Torvalds  *	Used to send TCP resets so far. ICMP should use this function too.
12601da177e4SLinus Torvalds  *
12611da177e4SLinus Torvalds  *	Should run single threaded per socket because it uses the sock
12621da177e4SLinus Torvalds  *     	structure to pass arguments.
12631da177e4SLinus Torvalds  *
12641da177e4SLinus Torvalds  *	LATER: switch from ip_build_xmit to ip_append_*
12651da177e4SLinus Torvalds  */
12661da177e4SLinus Torvalds void ip_send_reply(struct sock *sk, struct sk_buff *skb, struct ip_reply_arg *arg,
12671da177e4SLinus Torvalds 		   unsigned int len)
12681da177e4SLinus Torvalds {
12691da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
12701da177e4SLinus Torvalds 	struct {
12711da177e4SLinus Torvalds 		struct ip_options	opt;
12721da177e4SLinus Torvalds 		char			data[40];
12731da177e4SLinus Torvalds 	} replyopts;
12741da177e4SLinus Torvalds 	struct ipcm_cookie ipc;
12751da177e4SLinus Torvalds 	u32 daddr;
12761da177e4SLinus Torvalds 	struct rtable *rt = (struct rtable*)skb->dst;
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 	if (ip_options_echo(&replyopts.opt, skb))
12791da177e4SLinus Torvalds 		return;
12801da177e4SLinus Torvalds 
12811da177e4SLinus Torvalds 	daddr = ipc.addr = rt->rt_src;
12821da177e4SLinus Torvalds 	ipc.opt = NULL;
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 	if (replyopts.opt.optlen) {
12851da177e4SLinus Torvalds 		ipc.opt = &replyopts.opt;
12861da177e4SLinus Torvalds 
12871da177e4SLinus Torvalds 		if (ipc.opt->srr)
12881da177e4SLinus Torvalds 			daddr = replyopts.opt.faddr;
12891da177e4SLinus Torvalds 	}
12901da177e4SLinus Torvalds 
12911da177e4SLinus Torvalds 	{
12921da177e4SLinus Torvalds 		struct flowi fl = { .nl_u = { .ip4_u =
12931da177e4SLinus Torvalds 					      { .daddr = daddr,
12941da177e4SLinus Torvalds 						.saddr = rt->rt_spec_dst,
12951da177e4SLinus Torvalds 						.tos = RT_TOS(skb->nh.iph->tos) } },
12961da177e4SLinus Torvalds 				    /* Not quite clean, but right. */
12971da177e4SLinus Torvalds 				    .uli_u = { .ports =
12981da177e4SLinus Torvalds 					       { .sport = skb->h.th->dest,
12991da177e4SLinus Torvalds 					         .dport = skb->h.th->source } },
13001da177e4SLinus Torvalds 				    .proto = sk->sk_protocol };
13011da177e4SLinus Torvalds 		if (ip_route_output_key(&rt, &fl))
13021da177e4SLinus Torvalds 			return;
13031da177e4SLinus Torvalds 	}
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds 	/* And let IP do all the hard work.
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds 	   This chunk is not reenterable, hence spinlock.
13081da177e4SLinus Torvalds 	   Note that it uses the fact, that this function is called
13091da177e4SLinus Torvalds 	   with locally disabled BH and that sk cannot be already spinlocked.
13101da177e4SLinus Torvalds 	 */
13111da177e4SLinus Torvalds 	bh_lock_sock(sk);
13121da177e4SLinus Torvalds 	inet->tos = skb->nh.iph->tos;
13131da177e4SLinus Torvalds 	sk->sk_priority = skb->priority;
13141da177e4SLinus Torvalds 	sk->sk_protocol = skb->nh.iph->protocol;
13151da177e4SLinus Torvalds 	ip_append_data(sk, ip_reply_glue_bits, arg->iov->iov_base, len, 0,
13161da177e4SLinus Torvalds 		       &ipc, rt, MSG_DONTWAIT);
13171da177e4SLinus Torvalds 	if ((skb = skb_peek(&sk->sk_write_queue)) != NULL) {
13181da177e4SLinus Torvalds 		if (arg->csumoffset >= 0)
13191da177e4SLinus Torvalds 			*((u16 *)skb->h.raw + arg->csumoffset) = csum_fold(csum_add(skb->csum, arg->csum));
13201da177e4SLinus Torvalds 		skb->ip_summed = CHECKSUM_NONE;
13211da177e4SLinus Torvalds 		ip_push_pending_frames(sk);
13221da177e4SLinus Torvalds 	}
13231da177e4SLinus Torvalds 
13241da177e4SLinus Torvalds 	bh_unlock_sock(sk);
13251da177e4SLinus Torvalds 
13261da177e4SLinus Torvalds 	ip_rt_put(rt);
13271da177e4SLinus Torvalds }
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds /*
13301da177e4SLinus Torvalds  *	IP protocol layer initialiser
13311da177e4SLinus Torvalds  */
13321da177e4SLinus Torvalds 
13331da177e4SLinus Torvalds static struct packet_type ip_packet_type = {
13341da177e4SLinus Torvalds 	.type = __constant_htons(ETH_P_IP),
13351da177e4SLinus Torvalds 	.func = ip_rcv,
13361da177e4SLinus Torvalds };
13371da177e4SLinus Torvalds 
13381da177e4SLinus Torvalds /*
13391da177e4SLinus Torvalds  *	IP registers the packet type and then calls the subprotocol initialisers
13401da177e4SLinus Torvalds  */
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds void __init ip_init(void)
13431da177e4SLinus Torvalds {
13441da177e4SLinus Torvalds 	dev_add_pack(&ip_packet_type);
13451da177e4SLinus Torvalds 
13461da177e4SLinus Torvalds 	ip_rt_init();
13471da177e4SLinus Torvalds 	inet_initpeers();
13481da177e4SLinus Torvalds 
13491da177e4SLinus Torvalds #if defined(CONFIG_IP_MULTICAST) && defined(CONFIG_PROC_FS)
13501da177e4SLinus Torvalds 	igmp_mc_proc_init();
13511da177e4SLinus Torvalds #endif
13521da177e4SLinus Torvalds }
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds EXPORT_SYMBOL(ip_finish_output);
13551da177e4SLinus Torvalds EXPORT_SYMBOL(ip_fragment);
13561da177e4SLinus Torvalds EXPORT_SYMBOL(ip_generic_getfrag);
13571da177e4SLinus Torvalds EXPORT_SYMBOL(ip_queue_xmit);
13581da177e4SLinus Torvalds EXPORT_SYMBOL(ip_send_check);
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL
13611da177e4SLinus Torvalds EXPORT_SYMBOL(sysctl_ip_default_ttl);
13621da177e4SLinus Torvalds #endif
1363