blob: d6dd8ba044414b1ca2c4977e68387eba8d29e0dc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * The Internet Protocol (IP) output module.
7 *
Jesper Juhl02c30a82005-05-05 16:16:16 -07008 * Authors: Ross Biro
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Donald Becker, <becker@super.org>
11 * Alan Cox, <Alan.Cox@linux.org>
12 * Richard Underwood
13 * Stefan Becker, <stefanb@yello.ping.de>
14 * Jorge Cwik, <jorge@laser.satlink.net>
15 * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
16 * Hirokazu Takahashi, <taka@valinux.co.jp>
17 *
18 * See ip_input.c for original log
19 *
20 * Fixes:
21 * Alan Cox : Missing nonblock feature in ip_build_xmit.
22 * Mike Kilburn : htons() missing in ip_build_xmit.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090023 * Bradford Johnson: Fix faulty handling of some frames when
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * no route is found.
25 * Alexander Demenshin: Missing sk/skb free in ip_queue_xmit
26 * (in case if packet not accepted by
27 * output firewall rules)
28 * Mike McLagan : Routing by source
29 * Alexey Kuznetsov: use new route cache
30 * Andi Kleen: Fix broken PMTU recovery and remove
31 * some redundant tests.
32 * Vitaly E. Lavrov : Transparent proxy revived after year coma.
33 * Andi Kleen : Replace ip_reply with ip_send_reply.
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090034 * Andi Kleen : Split fast and slow ip_build_xmit path
35 * for decreased register pressure on x86
36 * and more readibility.
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * Marc Boucher : When call_out_firewall returns FW_QUEUE,
38 * silently drop skb instead of failing with -EPERM.
39 * Detlev Wengorz : Copy protocol for fragments.
40 * Hirokazu Takahashi: HW checksumming for outgoing UDP
41 * datagrams.
42 * Hirokazu Takahashi: sendfile() on UDP works now.
43 */
44
45#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/module.h>
47#include <linux/types.h>
48#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/mm.h>
50#include <linux/string.h>
51#include <linux/errno.h>
Al Viroa1f8e7f72006-10-19 16:08:53 -040052#include <linux/highmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#include <linux/socket.h>
56#include <linux/sockios.h>
57#include <linux/in.h>
58#include <linux/inet.h>
59#include <linux/netdevice.h>
60#include <linux/etherdevice.h>
61#include <linux/proc_fs.h>
62#include <linux/stat.h>
63#include <linux/init.h>
64
65#include <net/snmp.h>
66#include <net/ip.h>
67#include <net/protocol.h>
68#include <net/route.h>
Patrick McHardycfacb052006-01-08 22:36:54 -080069#include <net/xfrm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/skbuff.h>
71#include <net/sock.h>
72#include <net/arp.h>
73#include <net/icmp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <net/checksum.h>
75#include <net/inetpeer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/igmp.h>
77#include <linux/netfilter_ipv4.h>
78#include <linux/netfilter_bridge.h>
79#include <linux/mroute.h>
80#include <linux/netlink.h>
Arnaldo Carvalho de Melo6cbb0df2005-08-09 19:49:02 -070081#include <linux/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Brian Haleyab32ea52006-09-22 14:15:41 -070083int sysctl_ip_default_ttl __read_mostly = IPDEFTTL;
David S. Miller323e1262010-12-12 21:55:08 -080084EXPORT_SYMBOL(sysctl_ip_default_ttl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Andy Zhou49d16b22015-05-15 14:15:37 -070086static int ip_fragment(struct sock *sk, struct sk_buff *skb,
Florian Westphalc5501eb2015-05-22 16:32:50 +020087 unsigned int mtu,
Andy Zhou49d16b22015-05-15 14:15:37 -070088 int (*output)(struct sock *, struct sk_buff *));
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Generate a checksum for an outgoing IP datagram. */
Denis Efremov2fbd9672013-05-08 23:19:42 +000091void ip_send_check(struct iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 iph->check = 0;
94 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
95}
Eric Dumazet4bc2f182010-07-09 21:22:10 +000096EXPORT_SYMBOL(ip_send_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Eric Dumazet7d771aa2015-05-12 06:31:48 -070098static int __ip_local_out_sk(struct sock *sk, struct sk_buff *skb)
Herbert Xuc439cb22008-01-11 19:14:00 -080099{
100 struct iphdr *iph = ip_hdr(skb);
101
102 iph->tot_len = htons(skb->len);
103 ip_send_check(iph);
David Miller7026b1d2015-04-05 22:19:04 -0400104 return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, sk, skb, NULL,
105 skb_dst(skb)->dev, dst_output_sk);
106}
107
108int __ip_local_out(struct sk_buff *skb)
109{
110 return __ip_local_out_sk(skb->sk, skb);
Herbert Xuc439cb22008-01-11 19:14:00 -0800111}
112
Eric Dumazetaad88722014-04-15 13:47:15 -0400113int ip_local_out_sk(struct sock *sk, struct sk_buff *skb)
Herbert Xuc439cb22008-01-11 19:14:00 -0800114{
115 int err;
116
117 err = __ip_local_out(skb);
118 if (likely(err == 1))
Eric Dumazetaad88722014-04-15 13:47:15 -0400119 err = dst_output_sk(sk, skb);
Herbert Xuc439cb22008-01-11 19:14:00 -0800120
121 return err;
122}
Eric Dumazetaad88722014-04-15 13:47:15 -0400123EXPORT_SYMBOL_GPL(ip_local_out_sk);
Herbert Xuc439cb22008-01-11 19:14:00 -0800124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static inline int ip_select_ttl(struct inet_sock *inet, struct dst_entry *dst)
126{
127 int ttl = inet->uc_ttl;
128
129 if (ttl < 0)
David S. Miller323e1262010-12-12 21:55:08 -0800130 ttl = ip4_dst_hoplimit(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return ttl;
132}
133
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900134/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * Add an ip header to a skbuff and send it out.
136 *
137 */
138int ip_build_and_send_pkt(struct sk_buff *skb, struct sock *sk,
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000139 __be32 saddr, __be32 daddr, struct ip_options_rcu *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet511c3f92009-06-02 05:14:27 +0000142 struct rtable *rt = skb_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 struct iphdr *iph;
144
145 /* Build the IP header. */
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000146 skb_push(skb, sizeof(struct iphdr) + (opt ? opt->opt.optlen : 0));
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -0300147 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700148 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 iph->version = 4;
150 iph->ihl = 5;
151 iph->tos = inet->tos;
Changli Gaod8d1f302010-06-10 23:31:35 -0700152 if (ip_dont_fragment(sk, &rt->dst))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 iph->frag_off = htons(IP_DF);
154 else
155 iph->frag_off = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -0700156 iph->ttl = ip_select_ttl(inet, &rt->dst);
David S. Millerdd927a22011-05-04 12:03:30 -0700157 iph->daddr = (opt && opt->opt.srr ? opt->opt.faddr : daddr);
158 iph->saddr = saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 iph->protocol = sk->sk_protocol;
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100160 ip_select_ident(sock_net(sk), skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000162 if (opt && opt->opt.optlen) {
163 iph->ihl += opt->opt.optlen>>2;
164 ip_options_build(skb, &opt->opt, daddr, rt, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 skb->priority = sk->sk_priority;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800168 skb->mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /* Send it out. */
Herbert Xuc439cb22008-01-11 19:14:00 -0800171 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
Arnaldo Carvalho de Melod8c97a92005-08-09 20:12:12 -0700173EXPORT_SYMBOL_GPL(ip_build_and_send_pkt);
174
David Miller7026b1d2015-04-05 22:19:04 -0400175static inline int ip_finish_output2(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Eric Dumazetadf30902009-06-02 05:19:30 +0000177 struct dst_entry *dst = skb_dst(skb);
Mitsuru Chinen80787eb2007-04-30 00:48:20 -0700178 struct rtable *rt = (struct rtable *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 struct net_device *dev = dst->dev;
Chuck Leverc2636b42007-10-23 21:07:32 -0700180 unsigned int hh_len = LL_RESERVED_SPACE(dev);
David S. Millerf6b72b62011-07-14 07:53:20 -0700181 struct neighbour *neigh;
David S. Millera263b302012-07-02 02:02:15 -0700182 u32 nexthop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Neil Hormanedf391f2009-04-27 02:45:02 -0700184 if (rt->rt_type == RTN_MULTICAST) {
185 IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUTMCAST, skb->len);
186 } else if (rt->rt_type == RTN_BROADCAST)
187 IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUTBCAST, skb->len);
Mitsuru Chinen80787eb2007-04-30 00:48:20 -0700188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* Be paranoid, rather than too clever. */
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700190 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 struct sk_buff *skb2;
192
193 skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
Ian Morris51456b22015-04-03 09:17:26 +0100194 if (!skb2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 kfree_skb(skb);
196 return -ENOMEM;
197 }
198 if (skb->sk)
199 skb_set_owner_w(skb2, skb->sk);
Eric Dumazet5d0ba552012-06-04 01:17:19 +0000200 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 skb = skb2;
202 }
203
David S. Millera263b302012-07-02 02:02:15 -0700204 rcu_read_lock_bh();
Julian Anastasov155e8332012-10-08 11:41:18 +0000205 nexthop = (__force u32) rt_nexthop(rt, ip_hdr(skb)->daddr);
David S. Millera263b302012-07-02 02:02:15 -0700206 neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
207 if (unlikely(!neigh))
208 neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
Vasiliy Kulikov9871f1a2012-08-06 03:55:29 +0000209 if (!IS_ERR(neigh)) {
David S. Miller5110effe2012-07-02 02:21:03 -0700210 int res = dst_neigh_output(dst, neigh, skb);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000211
David S. Millera263b302012-07-02 02:02:15 -0700212 rcu_read_unlock_bh();
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000213 return res;
214 }
David S. Millera263b302012-07-02 02:02:15 -0700215 rcu_read_unlock_bh();
David S. Miller05e3aa02011-07-16 17:26:00 -0700216
Joe Perchese87cc472012-05-13 21:56:26 +0000217 net_dbg_ratelimited("%s: No header cache and no neighbour!\n",
218 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 kfree_skb(skb);
220 return -EINVAL;
221}
222
Florian Westphalc5501eb2015-05-22 16:32:50 +0200223static int ip_finish_output_gso(struct sock *sk, struct sk_buff *skb,
224 unsigned int mtu)
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200225{
226 netdev_features_t features;
227 struct sk_buff *segs;
228 int ret = 0;
229
230 /* common case: locally created skb or seglen is <= mtu */
231 if (((IPCB(skb)->flags & IPSKB_FORWARDED) == 0) ||
Florian Westphalc5501eb2015-05-22 16:32:50 +0200232 skb_gso_network_seglen(skb) <= mtu)
David Miller7026b1d2015-04-05 22:19:04 -0400233 return ip_finish_output2(sk, skb);
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200234
235 /* Slowpath - GSO segment length is exceeding the dst MTU.
236 *
237 * This can happen in two cases:
238 * 1) TCP GRO packet, DF bit not set
239 * 2) skb arrived via virtio-net, we thus get TSO/GSO skbs directly
240 * from host network stack.
241 */
242 features = netif_skb_features(skb);
243 segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
Florian Westphal330966e2014-10-20 13:49:17 +0200244 if (IS_ERR_OR_NULL(segs)) {
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200245 kfree_skb(skb);
246 return -ENOMEM;
247 }
248
249 consume_skb(skb);
250
251 do {
252 struct sk_buff *nskb = segs->next;
253 int err;
254
255 segs->next = NULL;
Florian Westphalc5501eb2015-05-22 16:32:50 +0200256 err = ip_fragment(sk, segs, mtu, ip_finish_output2);
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200257
258 if (err && ret == 0)
259 ret = err;
260 segs = nskb;
261 } while (segs);
262
263 return ret;
264}
265
David Miller7026b1d2015-04-05 22:19:04 -0400266static int ip_finish_output(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Florian Westphalc5501eb2015-05-22 16:32:50 +0200268 unsigned int mtu;
269
Patrick McHardy5c901da2006-01-06 23:05:36 -0800270#if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
271 /* Policy lookup after SNAT yielded a new policy */
Ian Morris00db4122015-04-03 09:17:27 +0100272 if (skb_dst(skb)->xfrm) {
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800273 IPCB(skb)->flags |= IPSKB_REROUTED;
David Miller7026b1d2015-04-05 22:19:04 -0400274 return dst_output_sk(sk, skb);
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800275 }
Patrick McHardy5c901da2006-01-06 23:05:36 -0800276#endif
Florian Westphalc5501eb2015-05-22 16:32:50 +0200277 mtu = ip_skb_dst_mtu(skb);
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200278 if (skb_is_gso(skb))
Florian Westphalc5501eb2015-05-22 16:32:50 +0200279 return ip_finish_output_gso(sk, skb, mtu);
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200280
Florian Westphalc5501eb2015-05-22 16:32:50 +0200281 if (skb->len > mtu)
282 return ip_fragment(sk, skb, mtu, ip_finish_output2);
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200283
David Miller7026b1d2015-04-05 22:19:04 -0400284 return ip_finish_output2(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Eric Dumazetaad88722014-04-15 13:47:15 -0400287int ip_mc_output(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Eric Dumazet511c3f92009-06-02 05:14:27 +0000289 struct rtable *rt = skb_rtable(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700290 struct net_device *dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /*
293 * If the indicated interface is up and running, send the packet.
294 */
Neil Hormanedf391f2009-04-27 02:45:02 -0700295 IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 skb->dev = dev;
298 skb->protocol = htons(ETH_P_IP);
299
300 /*
301 * Multicasts are looped back for other local users
302 */
303
304 if (rt->rt_flags&RTCF_MULTICAST) {
Octavian Purdila7ad68482010-01-06 20:37:01 -0800305 if (sk_mc_loop(sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306#ifdef CONFIG_IP_MROUTE
307 /* Small optimization: do not loopback not local frames,
308 which returned after forwarding; they will be dropped
309 by ip_mr_input in any case.
310 Note, that local frames are looped back to be delivered
311 to local recipients.
312
313 This check is duplicated in ip_mr_input at the moment.
314 */
Joe Perches9d4fb272009-11-23 10:41:23 -0800315 &&
316 ((rt->rt_flags & RTCF_LOCAL) ||
317 !(IPCB(skb)->flags & IPSKB_FORWARDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318#endif
Joe Perches9d4fb272009-11-23 10:41:23 -0800319 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
321 if (newskb)
Jan Engelhardt9bbc7682010-03-23 04:07:29 +0100322 NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
David Miller7026b1d2015-04-05 22:19:04 -0400323 sk, newskb, NULL, newskb->dev,
Michel Machado95603e22012-06-12 10:16:35 +0000324 dev_loopback_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326
327 /* Multicasts with ttl 0 must not go beyond the host */
328
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700329 if (ip_hdr(skb)->ttl == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 kfree_skb(skb);
331 return 0;
332 }
333 }
334
335 if (rt->rt_flags&RTCF_BROADCAST) {
336 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
337 if (newskb)
David Miller7026b1d2015-04-05 22:19:04 -0400338 NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, sk, newskb,
Michel Machado95603e22012-06-12 10:16:35 +0000339 NULL, newskb->dev, dev_loopback_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341
David Miller7026b1d2015-04-05 22:19:04 -0400342 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, sk, skb, NULL,
Jan Engelhardt9bbc7682010-03-23 04:07:29 +0100343 skb->dev, ip_finish_output,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800344 !(IPCB(skb)->flags & IPSKB_REROUTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Eric Dumazetaad88722014-04-15 13:47:15 -0400347int ip_output(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Eric Dumazetadf30902009-06-02 05:19:30 +0000349 struct net_device *dev = skb_dst(skb)->dev;
Patrick McHardy1bd9bef2006-01-05 12:20:59 -0800350
Neil Hormanedf391f2009-04-27 02:45:02 -0700351 IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Patrick McHardy1bd9bef2006-01-05 12:20:59 -0800353 skb->dev = dev;
354 skb->protocol = htons(ETH_P_IP);
355
David Miller7026b1d2015-04-05 22:19:04 -0400356 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, sk, skb,
357 NULL, dev,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900358 ip_finish_output,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800359 !(IPCB(skb)->flags & IPSKB_REROUTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Eric Dumazet84f93072011-11-30 19:00:53 +0000362/*
363 * copy saddr and daddr, possibly using 64bit load/stores
364 * Equivalent to :
365 * iph->saddr = fl4->saddr;
366 * iph->daddr = fl4->daddr;
367 */
368static void ip_copy_addrs(struct iphdr *iph, const struct flowi4 *fl4)
369{
370 BUILD_BUG_ON(offsetof(typeof(*fl4), daddr) !=
371 offsetof(typeof(*fl4), saddr) + sizeof(fl4->saddr));
372 memcpy(&iph->saddr, &fl4->saddr,
373 sizeof(fl4->saddr) + sizeof(fl4->daddr));
374}
375
Eric Dumazetb0270e92014-04-15 12:58:34 -0400376/* Note: skb->sk can be different from sk, in case of tunnels */
377int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 struct inet_sock *inet = inet_sk(sk);
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000380 struct ip_options_rcu *inet_opt;
David S. Millerb57ae012011-05-06 16:24:06 -0700381 struct flowi4 *fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct rtable *rt;
383 struct iphdr *iph;
Eric Dumazetab6e3fe2010-05-10 11:31:49 +0000384 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 /* Skip all of this if the packet is already routed,
387 * f.e. by something like SCTP.
388 */
Eric Dumazetab6e3fe2010-05-10 11:31:49 +0000389 rcu_read_lock();
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000390 inet_opt = rcu_dereference(inet->inet_opt);
David S. Millerea4fc0d2011-05-06 22:30:20 -0700391 fl4 = &fl->u.ip4;
Eric Dumazet511c3f92009-06-02 05:14:27 +0000392 rt = skb_rtable(skb);
Ian Morris00db4122015-04-03 09:17:27 +0100393 if (rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 goto packet_routed;
395
396 /* Make sure we can route this packet. */
397 rt = (struct rtable *)__sk_dst_check(sk, 0);
Ian Morris51456b22015-04-03 09:17:26 +0100398 if (!rt) {
Al Viro3ca3c682006-09-27 18:28:07 -0700399 __be32 daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /* Use correct destination address if we have options. */
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000402 daddr = inet->inet_daddr;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000403 if (inet_opt && inet_opt->opt.srr)
404 daddr = inet_opt->opt.faddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
David S. Miller78fbfd82011-03-12 00:00:52 -0500406 /* If this fails, retransmit mechanism of transport layer will
407 * keep trying until route appears or the connection times
408 * itself out.
409 */
David S. Millerb57ae012011-05-06 16:24:06 -0700410 rt = ip_route_output_ports(sock_net(sk), fl4, sk,
David S. Miller78fbfd82011-03-12 00:00:52 -0500411 daddr, inet->inet_saddr,
412 inet->inet_dport,
413 inet->inet_sport,
414 sk->sk_protocol,
415 RT_CONN_FLAGS(sk),
416 sk->sk_bound_dev_if);
417 if (IS_ERR(rt))
418 goto no_route;
Changli Gaod8d1f302010-06-10 23:31:35 -0700419 sk_setup_caps(sk, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700421 skb_dst_set_noref(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423packet_routed:
Julian Anastasov155e8332012-10-08 11:41:18 +0000424 if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_uses_gateway)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 goto no_route;
426
427 /* OK, we know where to send it, allocate and build IP header. */
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000428 skb_push(skb, sizeof(struct iphdr) + (inet_opt ? inet_opt->opt.optlen : 0));
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -0300429 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700430 iph = ip_hdr(skb);
Al Viro714e85b2006-11-14 20:51:49 -0800431 *((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (inet->tos & 0xff));
WANG Cong60ff7462014-05-04 16:39:18 -0700432 if (ip_dont_fragment(sk, &rt->dst) && !skb->ignore_df)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 iph->frag_off = htons(IP_DF);
434 else
435 iph->frag_off = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -0700436 iph->ttl = ip_select_ttl(inet, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 iph->protocol = sk->sk_protocol;
Eric Dumazet84f93072011-11-30 19:00:53 +0000438 ip_copy_addrs(iph, fl4);
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /* Transport layer set skb->h.foo itself. */
441
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000442 if (inet_opt && inet_opt->opt.optlen) {
443 iph->ihl += inet_opt->opt.optlen >> 2;
444 ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100447 ip_select_ident_segs(sock_net(sk), skb, sk,
448 skb_shinfo(skb)->gso_segs ?: 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Eric Dumazetb0270e92014-04-15 12:58:34 -0400450 /* TODO : should we use skb->sk here instead of sk ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 skb->priority = sk->sk_priority;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800452 skb->mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Eric Dumazetab6e3fe2010-05-10 11:31:49 +0000454 res = ip_local_out(skb);
455 rcu_read_unlock();
456 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458no_route:
Eric Dumazetab6e3fe2010-05-10 11:31:49 +0000459 rcu_read_unlock();
Pavel Emelyanov5e38e272008-07-16 20:19:49 -0700460 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 kfree_skb(skb);
462 return -EHOSTUNREACH;
463}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000464EXPORT_SYMBOL(ip_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from)
467{
468 to->pkt_type = from->pkt_type;
469 to->priority = from->priority;
470 to->protocol = from->protocol;
Eric Dumazetadf30902009-06-02 05:19:30 +0000471 skb_dst_drop(to);
Eric Dumazetfe76cda2010-07-01 23:48:22 +0000472 skb_dst_copy(to, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 to->dev = from->dev;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800474 to->mark = from->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 /* Copy the flags to each fragment. */
477 IPCB(to)->flags = IPCB(from)->flags;
478
479#ifdef CONFIG_NET_SCHED
480 to->tc_index = from->tc_index;
481#endif
Yasuyuki Kozakaie7ac05f2007-03-14 16:44:01 -0700482 nf_copy(to, from);
Julian Anastasovc98d80e2005-10-22 13:39:21 +0300483#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
484 to->ipvs_property = from->ipvs_property;
485#endif
James Morris984bc162006-06-09 00:29:17 -0700486 skb_copy_secmark(to, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Andy Zhou49d16b22015-05-15 14:15:37 -0700489static int ip_fragment(struct sock *sk, struct sk_buff *skb,
Florian Westphalc5501eb2015-05-22 16:32:50 +0200490 unsigned int mtu,
Andy Zhou49d16b22015-05-15 14:15:37 -0700491 int (*output)(struct sock *, struct sk_buff *))
492{
493 struct iphdr *iph = ip_hdr(skb);
Andy Zhou49d16b22015-05-15 14:15:37 -0700494
495 if (unlikely(((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) ||
496 (IPCB(skb)->frag_max_size &&
497 IPCB(skb)->frag_max_size > mtu))) {
498 struct rtable *rt = skb_rtable(skb);
499 struct net_device *dev = rt->dst.dev;
500
501 IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
502 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
503 htonl(mtu));
504 kfree_skb(skb);
505 return -EMSGSIZE;
506 }
507
508 return ip_do_fragment(sk, skb, output);
509}
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511/*
512 * This IP datagram is too large to be sent in one piece. Break it up into
513 * smaller pieces (each of size equal to IP header plus
514 * a block of the data of the original IP data part) that will yet fit in a
515 * single device frame, and queue such a frame for sending.
516 */
517
Andy Zhou49d16b22015-05-15 14:15:37 -0700518int ip_do_fragment(struct sock *sk, struct sk_buff *skb,
519 int (*output)(struct sock *, struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int ptr;
523 struct net_device *dev;
524 struct sk_buff *skb2;
Changli Gaoc893b802010-07-31 13:25:08 +0000525 unsigned int mtu, hlen, left, len, ll_rs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 int offset;
Alexey Dobriyan76ab6082006-01-06 13:24:29 -0800527 __be16 not_last_frag;
Eric Dumazet511c3f92009-06-02 05:14:27 +0000528 struct rtable *rt = skb_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 int err = 0;
530
Changli Gaod8d1f302010-06-10 23:31:35 -0700531 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /*
534 * Point into the IP datagram header.
535 */
536
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700537 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Hannes Frederic Sowa69647ce2014-02-26 01:20:41 +0100539 mtu = ip_skb_dst_mtu(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /*
542 * Setup starting values.
543 */
544
545 hlen = iph->ihl * 4;
Hannes Frederic Sowaf87c10a2014-01-09 10:01:15 +0100546 mtu = mtu - hlen; /* Size of data space */
Pablo Neira Ayuso1109a902014-10-01 11:19:17 +0200547#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
Bart De Schuymer6c79bf02010-04-20 16:22:01 +0200548 if (skb->nf_bridge)
549 mtu -= nf_bridge_mtu_reduction(skb);
550#endif
Herbert Xu89cee8b2005-12-13 23:14:27 -0800551 IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 /* When frag_list is given, use it. First, check its validity:
554 * some transformers could create wrong frag_list or break existing
555 * one, it is not prohibited. In this case fall back to copying.
556 *
557 * LATER: this step can be merged to real generation of fragments,
558 * we can switch to copy when see the first bad fragment.
559 */
David S. Miller21dc3302010-08-23 00:13:46 -0700560 if (skb_has_frag_list(skb)) {
Eric Dumazet3d130082010-09-21 08:47:45 +0000561 struct sk_buff *frag, *frag2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 int first_len = skb_pagelen(skb);
563
564 if (first_len - hlen > mtu ||
565 ((first_len - hlen) & 7) ||
Paul Gortmaker56f8a752011-06-21 20:33:34 -0700566 ip_is_fragment(iph) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 skb_cloned(skb))
568 goto slow_path;
569
David S. Millerd7fcf1a2009-06-09 00:19:37 -0700570 skb_walk_frags(skb, frag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* Correct geometry. */
572 if (frag->len > mtu ||
573 ((frag->len & 7) && frag->next) ||
574 skb_headroom(frag) < hlen)
Eric Dumazet3d130082010-09-21 08:47:45 +0000575 goto slow_path_clean;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 /* Partially cloned skb? */
578 if (skb_shared(frag))
Eric Dumazet3d130082010-09-21 08:47:45 +0000579 goto slow_path_clean;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700580
581 BUG_ON(frag->sk);
582 if (skb->sk) {
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700583 frag->sk = skb->sk;
584 frag->destructor = sock_wfree;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700585 }
Eric Dumazet3d130082010-09-21 08:47:45 +0000586 skb->truesize -= frag->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588
589 /* Everything is OK. Generate! */
590
591 err = 0;
592 offset = 0;
593 frag = skb_shinfo(skb)->frag_list;
David S. Millerd7fcf1a2009-06-09 00:19:37 -0700594 skb_frag_list_init(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 skb->data_len = first_len - skb_headlen(skb);
596 skb->len = first_len;
597 iph->tot_len = htons(first_len);
598 iph->frag_off = htons(IP_MF);
599 ip_send_check(iph);
600
601 for (;;) {
602 /* Prepare header of the next frame,
603 * before previous one went down. */
604 if (frag) {
605 frag->ip_summed = CHECKSUM_NONE;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300606 skb_reset_transport_header(frag);
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700607 __skb_push(frag, hlen);
608 skb_reset_network_header(frag);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700609 memcpy(skb_network_header(frag), iph, hlen);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700610 iph = ip_hdr(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 iph->tot_len = htons(frag->len);
612 ip_copy_metadata(frag, skb);
613 if (offset == 0)
614 ip_options_fragment(frag);
615 offset += skb->len - hlen;
616 iph->frag_off = htons(offset>>3);
Ian Morris00db4122015-04-03 09:17:27 +0100617 if (frag->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 iph->frag_off |= htons(IP_MF);
619 /* Ready, complete checksum */
620 ip_send_check(iph);
621 }
622
David Miller7026b1d2015-04-05 22:19:04 -0400623 err = output(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Wei Dongdafee492006-08-02 13:41:21 -0700625 if (!err)
Pavel Emelyanov5e38e272008-07-16 20:19:49 -0700626 IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGCREATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (err || !frag)
628 break;
629
630 skb = frag;
631 frag = skb->next;
632 skb->next = NULL;
633 }
634
635 if (err == 0) {
Pavel Emelyanov5e38e272008-07-16 20:19:49 -0700636 IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return 0;
638 }
639
640 while (frag) {
641 skb = frag->next;
642 kfree_skb(frag);
643 frag = skb;
644 }
Pavel Emelyanov5e38e272008-07-16 20:19:49 -0700645 IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return err;
Eric Dumazet3d130082010-09-21 08:47:45 +0000647
648slow_path_clean:
649 skb_walk_frags(skb, frag2) {
650 if (frag2 == frag)
651 break;
652 frag2->sk = NULL;
653 frag2->destructor = NULL;
654 skb->truesize += frag2->truesize;
655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657
658slow_path:
Alexander Duyckfc70fb62012-12-07 14:14:15 +0000659 /* for offloaded checksums cleanup checksum before fragmentation */
660 if ((skb->ip_summed == CHECKSUM_PARTIAL) && skb_checksum_help(skb))
661 goto fail;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +0000662 iph = ip_hdr(skb);
Alexander Duyckfc70fb62012-12-07 14:14:15 +0000663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 left = skb->len - hlen; /* Space per frame */
George Kadianakis49085bd2010-07-06 11:44:12 +0000665 ptr = hlen; /* Where to start from */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Florian Westphal8d045162015-03-18 20:55:31 +0100667 ll_rs = LL_RESERVED_SPACE(rt->dst.dev);
Stephen Hemminger9bcfcaf2006-08-29 17:48:57 -0700668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /*
670 * Fragment the datagram.
671 */
672
673 offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3;
674 not_last_frag = iph->frag_off & htons(IP_MF);
675
676 /*
677 * Keep copying data until we run out.
678 */
679
Stephen Hemminger132adf52007-03-08 20:44:43 -0800680 while (left > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 len = left;
682 /* IF: it doesn't fit, use 'mtu' - the data space left */
683 if (len > mtu)
684 len = mtu;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300685 /* IF: we are not sending up to and including the packet end
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 then align the next start on an eight byte boundary */
687 if (len < left) {
688 len &= ~7;
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Joe Perchescbffccc2014-11-05 14:39:21 -0800691 /* Allocate buffer */
692 skb2 = alloc_skb(len + hlen + ll_rs, GFP_ATOMIC);
693 if (!skb2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 err = -ENOMEM;
695 goto fail;
696 }
697
698 /*
699 * Set up data on packet
700 */
701
702 ip_copy_metadata(skb2, skb);
703 skb_reserve(skb2, ll_rs);
704 skb_put(skb2, len + hlen);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700705 skb_reset_network_header(skb2);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700706 skb2->transport_header = skb2->network_header + hlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 /*
709 * Charge the memory for the fragment to any owner
710 * it might possess
711 */
712
713 if (skb->sk)
714 skb_set_owner_w(skb2, skb->sk);
715
716 /*
717 * Copy the packet header into the new buffer.
718 */
719
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300720 skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 /*
723 * Copy a block of the IP datagram.
724 */
Arnaldo Carvalho de Melobff9b612007-03-16 17:19:57 -0300725 if (skb_copy_bits(skb, ptr, skb_transport_header(skb2), len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 BUG();
727 left -= len;
728
729 /*
730 * Fill in the new header fields.
731 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700732 iph = ip_hdr(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 iph->frag_off = htons((offset >> 3));
734
735 /* ANK: dirty, but effective trick. Upgrade options only if
736 * the segment to be fragmented was THE FIRST (otherwise,
737 * options are already fixed) and make it ONCE
738 * on the initial skb, so that all the following fragments
739 * will inherit fixed options.
740 */
741 if (offset == 0)
742 ip_options_fragment(skb);
743
744 /*
745 * Added AC : If we are fragmenting a fragment that's not the
746 * last fragment then keep MF on each bit
747 */
748 if (left > 0 || not_last_frag)
749 iph->frag_off |= htons(IP_MF);
750 ptr += len;
751 offset += len;
752
753 /*
754 * Put this fragment into the sending queue.
755 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 iph->tot_len = htons(len + hlen);
757
758 ip_send_check(iph);
759
David Miller7026b1d2015-04-05 22:19:04 -0400760 err = output(sk, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (err)
762 goto fail;
Wei Dongdafee492006-08-02 13:41:21 -0700763
Pavel Emelyanov5e38e272008-07-16 20:19:49 -0700764 IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGCREATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
Eric Dumazet5d0ba552012-06-04 01:17:19 +0000766 consume_skb(skb);
Pavel Emelyanov5e38e272008-07-16 20:19:49 -0700767 IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return err;
769
770fail:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900771 kfree_skb(skb);
Pavel Emelyanov5e38e272008-07-16 20:19:49 -0700772 IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return err;
774}
Andy Zhou49d16b22015-05-15 14:15:37 -0700775EXPORT_SYMBOL(ip_do_fragment);
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777int
778ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
779{
Al Virof69e6d12014-11-24 13:23:40 -0500780 struct msghdr *msg = from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Patrick McHardy84fa7932006-08-29 16:44:56 -0700782 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viro21226ab2014-11-28 15:48:29 -0500783 if (copy_from_iter(to, len, &msg->msg_iter) != len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 return -EFAULT;
785 } else {
Al Viro44bb9362006-11-14 21:36:14 -0800786 __wsum csum = 0;
Al Viro21226ab2014-11-28 15:48:29 -0500787 if (csum_and_copy_from_iter(to, len, &csum, &msg->msg_iter) != len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return -EFAULT;
789 skb->csum = csum_block_add(skb->csum, csum, odd);
790 }
791 return 0;
792}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000793EXPORT_SYMBOL(ip_generic_getfrag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Al Viro44bb9362006-11-14 21:36:14 -0800795static inline __wsum
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796csum_page(struct page *page, int offset, int copy)
797{
798 char *kaddr;
Al Viro44bb9362006-11-14 21:36:14 -0800799 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 kaddr = kmap(page);
801 csum = csum_partial(kaddr + offset, copy, 0);
802 kunmap(page);
803 return csum;
804}
805
Adrian Bunk4b30b1c2005-11-29 16:27:20 -0800806static inline int ip_ufo_append_data(struct sock *sk,
Herbert Xu1470ddf2011-03-01 02:36:47 +0000807 struct sk_buff_head *queue,
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700808 int getfrag(void *from, char *to, int offset, int len,
809 int odd, struct sk_buff *skb),
810 void *from, int length, int hh_len, int fragheaderlen,
Bill Sommerfeldd9be4f72011-07-19 15:22:33 +0000811 int transhdrlen, int maxfraglen, unsigned int flags)
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700812{
813 struct sk_buff *skb;
814 int err;
815
816 /* There is support for UDP fragmentation offload by network
817 * device, so create one single skb packet containing complete
818 * udp datagram
819 */
Ian Morris51456b22015-04-03 09:17:26 +0100820 skb = skb_peek_tail(queue);
821 if (!skb) {
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700822 skb = sock_alloc_send_skb(sk,
823 hh_len + fragheaderlen + transhdrlen + 20,
824 (flags & MSG_DONTWAIT), &err);
825
Ian Morris51456b22015-04-03 09:17:26 +0100826 if (!skb)
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700827 return err;
828
829 /* reserve space for Hardware header */
830 skb_reserve(skb, hh_len);
831
832 /* create space for UDP/IP header */
Jianjun Kongd93191002008-11-03 00:23:42 -0800833 skb_put(skb, fragheaderlen + transhdrlen);
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700834
835 /* initialize network header pointer */
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700836 skb_reset_network_header(skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700837
838 /* initialize protocol header pointer */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700839 skb->transport_header = skb->network_header + fragheaderlen;
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700840
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700841 skb->csum = 0;
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700842
Herbert Xu1470ddf2011-03-01 02:36:47 +0000843 __skb_queue_tail(queue, skb);
Jiri Pirkoe93b7d72013-10-19 12:29:17 +0200844 } else if (skb_is_gso(skb)) {
845 goto append;
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700846 }
Kostya Bbe9164e2008-04-29 22:36:30 -0700847
Jiri Pirkoe93b7d72013-10-19 12:29:17 +0200848 skb->ip_summed = CHECKSUM_PARTIAL;
849 /* specify the length of each IP datagram fragment */
850 skb_shinfo(skb)->gso_size = maxfraglen - fragheaderlen;
851 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
852
853append:
Kostya Bbe9164e2008-04-29 22:36:30 -0700854 return skb_append_datato_frags(sk, skb, getfrag, from,
855 (length - transhdrlen));
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700856}
857
David S. Millerf5fca602011-05-08 17:24:10 -0700858static int __ip_append_data(struct sock *sk,
859 struct flowi4 *fl4,
860 struct sk_buff_head *queue,
Herbert Xu1470ddf2011-03-01 02:36:47 +0000861 struct inet_cork *cork,
Eric Dumazet5640f762012-09-23 23:04:42 +0000862 struct page_frag *pfrag,
Herbert Xu1470ddf2011-03-01 02:36:47 +0000863 int getfrag(void *from, char *to, int offset,
864 int len, int odd, struct sk_buff *skb),
865 void *from, int length, int transhdrlen,
866 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 struct inet_sock *inet = inet_sk(sk);
869 struct sk_buff *skb;
870
Herbert Xu07df5292011-03-01 23:00:58 -0800871 struct ip_options *opt = cork->opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 int hh_len;
873 int exthdrlen;
874 int mtu;
875 int copy;
876 int err;
877 int offset = 0;
Hannes Frederic Sowadaba2872013-10-27 17:29:11 +0100878 unsigned int maxfraglen, fragheaderlen, maxnonfragsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 int csummode = CHECKSUM_NONE;
Herbert Xu1470ddf2011-03-01 02:36:47 +0000880 struct rtable *rt = (struct rtable *)cork->dst;
Willem de Bruijn09c2d252014-08-04 22:11:47 -0400881 u32 tskey = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Steffen Klassert96d73032011-06-05 20:48:47 +0000883 skb = skb_peek_tail(queue);
884
885 exthdrlen = !skb ? rt->dst.header_len : 0;
Herbert Xu07df5292011-03-01 23:00:58 -0800886 mtu = cork->fragsize;
Willem de Bruijn09c2d252014-08-04 22:11:47 -0400887 if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP &&
888 sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)
889 tskey = sk->sk_tskey++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Changli Gaod8d1f302010-06-10 23:31:35 -0700891 hh_len = LL_RESERVED_SPACE(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
894 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
WANG Cong60ff7462014-05-04 16:39:18 -0700895 maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Hannes Frederic Sowadaba2872013-10-27 17:29:11 +0100897 if (cork->length + length > maxnonfragsize - fragheaderlen) {
David S. Millerf5fca602011-05-08 17:24:10 -0700898 ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
Hannes Frederic Sowa61e7f092013-12-19 02:13:36 +0100899 mtu - (opt ? opt->optlen : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return -EMSGSIZE;
901 }
902
903 /*
904 * transhdrlen > 0 means that this is the first fragment and we wish
905 * it won't be fragmented in the future.
906 */
907 if (transhdrlen &&
908 length + fragheaderlen <= mtu &&
Changli Gaod8d1f302010-06-10 23:31:35 -0700909 rt->dst.dev->features & NETIF_F_V4_CSUM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 !exthdrlen)
Patrick McHardy84fa7932006-08-29 16:44:56 -0700911 csummode = CHECKSUM_PARTIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Herbert Xu1470ddf2011-03-01 02:36:47 +0000913 cork->length += length;
Herbert Xu26cde9f2010-06-15 01:52:25 +0000914 if (((length > mtu) || (skb && skb_is_gso(skb))) &&
Kostya Bbe9164e2008-04-29 22:36:30 -0700915 (sk->sk_protocol == IPPROTO_UDP) &&
Michal Kubečekacf8dd02015-03-02 18:27:11 +0100916 (rt->dst.dev->features & NETIF_F_UFO) && !rt->dst.header_len &&
917 (sk->sk_type == SOCK_DGRAM)) {
Herbert Xu1470ddf2011-03-01 02:36:47 +0000918 err = ip_ufo_append_data(sk, queue, getfrag, from, length,
919 hh_len, fragheaderlen, transhdrlen,
Bill Sommerfeldd9be4f72011-07-19 15:22:33 +0000920 maxfraglen, flags);
Patrick McHardybaa829d2006-03-12 20:35:12 -0800921 if (err)
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700922 goto error;
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700923 return 0;
924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 /* So, what's going on in the loop below?
927 *
928 * We use calculated fragment length to generate chained skb,
929 * each of segments is IP fragment ready for sending to network after
930 * adding appropriate IP header.
931 */
932
Herbert Xu26cde9f2010-06-15 01:52:25 +0000933 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 goto alloc_new_skb;
935
936 while (length > 0) {
937 /* Check if the remaining data fits into current packet. */
938 copy = mtu - skb->len;
939 if (copy < length)
940 copy = maxfraglen - skb->len;
941 if (copy <= 0) {
942 char *data;
943 unsigned int datalen;
944 unsigned int fraglen;
945 unsigned int fraggap;
946 unsigned int alloclen;
947 struct sk_buff *skb_prev;
948alloc_new_skb:
949 skb_prev = skb;
950 if (skb_prev)
951 fraggap = skb_prev->len - maxfraglen;
952 else
953 fraggap = 0;
954
955 /*
956 * If remaining data exceeds the mtu,
957 * we know we need more fragment(s).
958 */
959 datalen = length + fraggap;
960 if (datalen > mtu - fragheaderlen)
961 datalen = maxfraglen - fragheaderlen;
962 fraglen = datalen + fragheaderlen;
963
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900964 if ((flags & MSG_MORE) &&
Changli Gaod8d1f302010-06-10 23:31:35 -0700965 !(rt->dst.dev->features&NETIF_F_SG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 alloclen = mtu;
967 else
Eric Dumazet59104f02010-09-20 20:16:27 +0000968 alloclen = fraglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Steffen Klassert353e5c92011-06-22 01:05:37 +0000970 alloclen += exthdrlen;
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* The last fragment gets additional space at tail.
973 * Note, with MSG_MORE we overallocate on fragments,
974 * because we have no idea what fragment will be
975 * the last.
976 */
Steffen Klassert33f99dc2011-06-22 01:04:37 +0000977 if (datalen == length + fraggap)
Changli Gaod8d1f302010-06-10 23:31:35 -0700978 alloclen += rt->dst.trailer_len;
Steffen Klassert33f99dc2011-06-22 01:04:37 +0000979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (transhdrlen) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900981 skb = sock_alloc_send_skb(sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 alloclen + hh_len + 15,
983 (flags & MSG_DONTWAIT), &err);
984 } else {
985 skb = NULL;
986 if (atomic_read(&sk->sk_wmem_alloc) <=
987 2 * sk->sk_sndbuf)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900988 skb = sock_wmalloc(sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 alloclen + hh_len + 15, 1,
990 sk->sk_allocation);
Ian Morris51456b22015-04-03 09:17:26 +0100991 if (unlikely(!skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 err = -ENOBUFS;
993 }
Ian Morris51456b22015-04-03 09:17:26 +0100994 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 goto error;
996
997 /*
998 * Fill in the control structures
999 */
1000 skb->ip_summed = csummode;
1001 skb->csum = 0;
1002 skb_reserve(skb, hh_len);
Willem de Bruijn11878b42014-07-14 17:55:06 -04001003
1004 /* only the initial fragment is time stamped */
Herbert Xu1470ddf2011-03-01 02:36:47 +00001005 skb_shinfo(skb)->tx_flags = cork->tx_flags;
Willem de Bruijn11878b42014-07-14 17:55:06 -04001006 cork->tx_flags = 0;
Willem de Bruijn09c2d252014-08-04 22:11:47 -04001007 skb_shinfo(skb)->tskey = tskey;
1008 tskey = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 /*
1011 * Find where to start putting bytes.
1012 */
Steffen Klassert353e5c92011-06-22 01:05:37 +00001013 data = skb_put(skb, fraglen + exthdrlen);
Arnaldo Carvalho de Meloc14d2452007-03-11 22:39:41 -03001014 skb_set_network_header(skb, exthdrlen);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001015 skb->transport_header = (skb->network_header +
1016 fragheaderlen);
Steffen Klassert353e5c92011-06-22 01:05:37 +00001017 data += fragheaderlen + exthdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 if (fraggap) {
1020 skb->csum = skb_copy_and_csum_bits(
1021 skb_prev, maxfraglen,
1022 data + transhdrlen, fraggap, 0);
1023 skb_prev->csum = csum_sub(skb_prev->csum,
1024 skb->csum);
1025 data += fraggap;
Herbert Xue9fa4f72006-08-13 20:12:58 -07001026 pskb_trim_unique(skb_prev, maxfraglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028
1029 copy = datalen - transhdrlen - fraggap;
1030 if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
1031 err = -EFAULT;
1032 kfree_skb(skb);
1033 goto error;
1034 }
1035
1036 offset += copy;
1037 length -= datalen - fraggap;
1038 transhdrlen = 0;
1039 exthdrlen = 0;
1040 csummode = CHECKSUM_NONE;
1041
1042 /*
1043 * Put the packet on the pending queue.
1044 */
Herbert Xu1470ddf2011-03-01 02:36:47 +00001045 __skb_queue_tail(queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 continue;
1047 }
1048
1049 if (copy > length)
1050 copy = length;
1051
Changli Gaod8d1f302010-06-10 23:31:35 -07001052 if (!(rt->dst.dev->features&NETIF_F_SG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 unsigned int off;
1054
1055 off = skb->len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001056 if (getfrag(from, skb_put(skb, copy),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 offset, copy, off, skb) < 0) {
1058 __skb_trim(skb, off);
1059 err = -EFAULT;
1060 goto error;
1061 }
1062 } else {
1063 int i = skb_shinfo(skb)->nr_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Eric Dumazet5640f762012-09-23 23:04:42 +00001065 err = -ENOMEM;
1066 if (!sk_page_frag_refill(sk, pfrag))
1067 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Eric Dumazet5640f762012-09-23 23:04:42 +00001069 if (!skb_can_coalesce(skb, i, pfrag->page,
1070 pfrag->offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 err = -EMSGSIZE;
Eric Dumazet5640f762012-09-23 23:04:42 +00001072 if (i == MAX_SKB_FRAGS)
1073 goto error;
1074
1075 __skb_fill_page_desc(skb, i, pfrag->page,
1076 pfrag->offset, 0);
1077 skb_shinfo(skb)->nr_frags = ++i;
1078 get_page(pfrag->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
Eric Dumazet5640f762012-09-23 23:04:42 +00001080 copy = min_t(int, copy, pfrag->size - pfrag->offset);
1081 if (getfrag(from,
1082 page_address(pfrag->page) + pfrag->offset,
1083 offset, copy, skb->len, skb) < 0)
1084 goto error_efault;
1085
1086 pfrag->offset += copy;
1087 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 skb->len += copy;
1089 skb->data_len += copy;
Herbert Xuf945fa72008-01-22 22:39:26 -08001090 skb->truesize += copy;
1091 atomic_add(copy, &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093 offset += copy;
1094 length -= copy;
1095 }
1096
1097 return 0;
1098
Eric Dumazet5640f762012-09-23 23:04:42 +00001099error_efault:
1100 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101error:
Herbert Xu1470ddf2011-03-01 02:36:47 +00001102 cork->length -= length;
Pavel Emelyanov5e38e272008-07-16 20:19:49 -07001103 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001104 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
Herbert Xu1470ddf2011-03-01 02:36:47 +00001107static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
1108 struct ipcm_cookie *ipc, struct rtable **rtp)
1109{
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001110 struct ip_options_rcu *opt;
Herbert Xu1470ddf2011-03-01 02:36:47 +00001111 struct rtable *rt;
1112
1113 /*
1114 * setup for corking.
1115 */
1116 opt = ipc->opt;
1117 if (opt) {
Ian Morris51456b22015-04-03 09:17:26 +01001118 if (!cork->opt) {
Herbert Xu1470ddf2011-03-01 02:36:47 +00001119 cork->opt = kmalloc(sizeof(struct ip_options) + 40,
1120 sk->sk_allocation);
Ian Morris51456b22015-04-03 09:17:26 +01001121 if (unlikely(!cork->opt))
Herbert Xu1470ddf2011-03-01 02:36:47 +00001122 return -ENOBUFS;
1123 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001124 memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen);
Herbert Xu1470ddf2011-03-01 02:36:47 +00001125 cork->flags |= IPCORK_OPT;
1126 cork->addr = ipc->addr;
1127 }
1128 rt = *rtp;
1129 if (unlikely(!rt))
1130 return -EFAULT;
1131 /*
1132 * We steal reference to this route, caller should not release it
1133 */
1134 *rtp = NULL;
Hannes Frederic Sowa482fc602013-11-05 02:24:17 +01001135 cork->fragsize = ip_sk_use_pmtu(sk) ?
1136 dst_mtu(&rt->dst) : rt->dst.dev->mtu;
Herbert Xu1470ddf2011-03-01 02:36:47 +00001137 cork->dst = &rt->dst;
1138 cork->length = 0;
Francesco Fuscoaa661582013-09-24 15:43:09 +02001139 cork->ttl = ipc->ttl;
1140 cork->tos = ipc->tos;
1141 cork->priority = ipc->priority;
Herbert Xu1470ddf2011-03-01 02:36:47 +00001142 cork->tx_flags = ipc->tx_flags;
Herbert Xu1470ddf2011-03-01 02:36:47 +00001143
1144 return 0;
1145}
1146
1147/*
1148 * ip_append_data() and ip_append_page() can make one large IP datagram
1149 * from many pieces of data. Each pieces will be holded on the socket
1150 * until ip_push_pending_frames() is called. Each piece can be a page
1151 * or non-page data.
1152 *
1153 * Not only UDP, other transport protocols - e.g. raw sockets - can use
1154 * this interface potentially.
1155 *
1156 * LATER: length must be adjusted by pad at tail, when it is required.
1157 */
David S. Millerf5fca602011-05-08 17:24:10 -07001158int ip_append_data(struct sock *sk, struct flowi4 *fl4,
Herbert Xu1470ddf2011-03-01 02:36:47 +00001159 int getfrag(void *from, char *to, int offset, int len,
1160 int odd, struct sk_buff *skb),
1161 void *from, int length, int transhdrlen,
1162 struct ipcm_cookie *ipc, struct rtable **rtp,
1163 unsigned int flags)
1164{
1165 struct inet_sock *inet = inet_sk(sk);
1166 int err;
1167
1168 if (flags&MSG_PROBE)
1169 return 0;
1170
1171 if (skb_queue_empty(&sk->sk_write_queue)) {
David S. Millerbdc712b2011-05-06 15:02:07 -07001172 err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp);
Herbert Xu1470ddf2011-03-01 02:36:47 +00001173 if (err)
1174 return err;
1175 } else {
1176 transhdrlen = 0;
1177 }
1178
Eric Dumazet5640f762012-09-23 23:04:42 +00001179 return __ip_append_data(sk, fl4, &sk->sk_write_queue, &inet->cork.base,
1180 sk_page_frag(sk), getfrag,
Herbert Xu1470ddf2011-03-01 02:36:47 +00001181 from, length, transhdrlen, flags);
1182}
1183
David S. Millerf5fca602011-05-08 17:24:10 -07001184ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 int offset, size_t size, int flags)
1186{
1187 struct inet_sock *inet = inet_sk(sk);
1188 struct sk_buff *skb;
1189 struct rtable *rt;
1190 struct ip_options *opt = NULL;
David S. Millerbdc712b2011-05-06 15:02:07 -07001191 struct inet_cork *cork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 int hh_len;
1193 int mtu;
1194 int len;
1195 int err;
Hannes Frederic Sowadaba2872013-10-27 17:29:11 +01001196 unsigned int maxfraglen, fragheaderlen, fraggap, maxnonfragsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 if (inet->hdrincl)
1199 return -EPERM;
1200
1201 if (flags&MSG_PROBE)
1202 return 0;
1203
1204 if (skb_queue_empty(&sk->sk_write_queue))
1205 return -EINVAL;
1206
David S. Millerbdc712b2011-05-06 15:02:07 -07001207 cork = &inet->cork.base;
1208 rt = (struct rtable *)cork->dst;
1209 if (cork->flags & IPCORK_OPT)
1210 opt = cork->opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Changli Gaod8d1f302010-06-10 23:31:35 -07001212 if (!(rt->dst.dev->features&NETIF_F_SG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return -EOPNOTSUPP;
1214
Changli Gaod8d1f302010-06-10 23:31:35 -07001215 hh_len = LL_RESERVED_SPACE(rt->dst.dev);
David S. Millerbdc712b2011-05-06 15:02:07 -07001216 mtu = cork->fragsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
1219 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
WANG Cong60ff7462014-05-04 16:39:18 -07001220 maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Hannes Frederic Sowadaba2872013-10-27 17:29:11 +01001222 if (cork->length + size > maxnonfragsize - fragheaderlen) {
Hannes Frederic Sowa61e7f092013-12-19 02:13:36 +01001223 ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
1224 mtu - (opt ? opt->optlen : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return -EMSGSIZE;
1226 }
1227
Ian Morris51456b22015-04-03 09:17:26 +01001228 skb = skb_peek_tail(&sk->sk_write_queue);
1229 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return -EINVAL;
1231
David S. Millerbdc712b2011-05-06 15:02:07 -07001232 cork->length += size;
Herbert Xu26cde9f2010-06-15 01:52:25 +00001233 if ((size + skb->len > mtu) &&
1234 (sk->sk_protocol == IPPROTO_UDP) &&
Changli Gaod8d1f302010-06-10 23:31:35 -07001235 (rt->dst.dev->features & NETIF_F_UFO)) {
Herbert Xu79671682006-06-22 02:40:14 -07001236 skb_shinfo(skb)->gso_size = mtu - fragheaderlen;
Herbert Xuf83ef8c2006-06-30 13:37:03 -07001237 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
Herbert Xu79671682006-06-22 02:40:14 -07001238 }
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 while (size > 0) {
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02001241 if (skb_is_gso(skb)) {
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001242 len = size;
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02001243 } else {
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001244
1245 /* Check if the remaining data fits into current packet. */
1246 len = mtu - skb->len;
1247 if (len < size)
1248 len = maxfraglen - skb->len;
1249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (len <= 0) {
1251 struct sk_buff *skb_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 int alloclen;
1253
1254 skb_prev = skb;
Jayachandran C0d0d2bb2005-10-13 11:43:02 -07001255 fraggap = skb_prev->len - maxfraglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 alloclen = fragheaderlen + hh_len + fraggap + 15;
1258 skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation);
1259 if (unlikely(!skb)) {
1260 err = -ENOBUFS;
1261 goto error;
1262 }
1263
1264 /*
1265 * Fill in the control structures
1266 */
1267 skb->ip_summed = CHECKSUM_NONE;
1268 skb->csum = 0;
1269 skb_reserve(skb, hh_len);
1270
1271 /*
1272 * Find where to start putting bytes.
1273 */
Arnaldo Carvalho de Melo967b05f2007-03-13 13:51:52 -03001274 skb_put(skb, fragheaderlen + fraggap);
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -03001275 skb_reset_network_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001276 skb->transport_header = (skb->network_header +
1277 fragheaderlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (fraggap) {
Arnaldo Carvalho de Melo967b05f2007-03-13 13:51:52 -03001279 skb->csum = skb_copy_and_csum_bits(skb_prev,
1280 maxfraglen,
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001281 skb_transport_header(skb),
Arnaldo Carvalho de Melo967b05f2007-03-13 13:51:52 -03001282 fraggap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 skb_prev->csum = csum_sub(skb_prev->csum,
1284 skb->csum);
Herbert Xue9fa4f72006-08-13 20:12:58 -07001285 pskb_trim_unique(skb_prev, maxfraglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
1287
1288 /*
1289 * Put the packet on the pending queue.
1290 */
1291 __skb_queue_tail(&sk->sk_write_queue, skb);
1292 continue;
1293 }
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (len > size)
1296 len = size;
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02001297
1298 if (skb_append_pagefrags(skb, page, offset, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 err = -EMSGSIZE;
1300 goto error;
1301 }
1302
1303 if (skb->ip_summed == CHECKSUM_NONE) {
Al Viro44bb9362006-11-14 21:36:14 -08001304 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 csum = csum_page(page, offset, len);
1306 skb->csum = csum_block_add(skb->csum, csum, skb->len);
1307 }
1308
1309 skb->len += len;
1310 skb->data_len += len;
David S. Miller1e34a112008-01-22 23:44:31 -08001311 skb->truesize += len;
1312 atomic_add(len, &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 offset += len;
1314 size -= len;
1315 }
1316 return 0;
1317
1318error:
David S. Millerbdc712b2011-05-06 15:02:07 -07001319 cork->length -= size;
Pavel Emelyanov5e38e272008-07-16 20:19:49 -07001320 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return err;
1322}
1323
Herbert Xu1470ddf2011-03-01 02:36:47 +00001324static void ip_cork_release(struct inet_cork *cork)
Pavel Emelyanov429f08e2007-11-05 21:03:24 -08001325{
Herbert Xu1470ddf2011-03-01 02:36:47 +00001326 cork->flags &= ~IPCORK_OPT;
1327 kfree(cork->opt);
1328 cork->opt = NULL;
1329 dst_release(cork->dst);
1330 cork->dst = NULL;
Pavel Emelyanov429f08e2007-11-05 21:03:24 -08001331}
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333/*
1334 * Combined all pending IP fragments on the socket as one IP datagram
1335 * and push them out.
1336 */
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001337struct sk_buff *__ip_make_skb(struct sock *sk,
David S. Miller77968b72011-05-08 17:12:19 -07001338 struct flowi4 *fl4,
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001339 struct sk_buff_head *queue,
1340 struct inet_cork *cork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 struct sk_buff *skb, *tmp_skb;
1343 struct sk_buff **tail_skb;
1344 struct inet_sock *inet = inet_sk(sk);
Pavel Emelyanov0388b002008-07-14 23:00:43 -07001345 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 struct ip_options *opt = NULL;
Herbert Xu1470ddf2011-03-01 02:36:47 +00001347 struct rtable *rt = (struct rtable *)cork->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 struct iphdr *iph;
Alexey Dobriyan76ab6082006-01-06 13:24:29 -08001349 __be16 df = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 __u8 ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Ian Morris51456b22015-04-03 09:17:26 +01001352 skb = __skb_dequeue(queue);
1353 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 goto out;
1355 tail_skb = &(skb_shinfo(skb)->frag_list);
1356
1357 /* move skb->data to ip header from ext header */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001358 if (skb->data < skb_network_header(skb))
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001359 __skb_pull(skb, skb_network_offset(skb));
Herbert Xu1470ddf2011-03-01 02:36:47 +00001360 while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001361 __skb_pull(tmp_skb, skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 *tail_skb = tmp_skb;
1363 tail_skb = &(tmp_skb->next);
1364 skb->len += tmp_skb->len;
1365 skb->data_len += tmp_skb->len;
1366 skb->truesize += tmp_skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 tmp_skb->destructor = NULL;
1368 tmp_skb->sk = NULL;
1369 }
1370
1371 /* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow
1372 * to fragment the frame generated here. No matter, what transforms
1373 * how transforms change size of the packet, it will come out.
1374 */
WANG Cong60ff7462014-05-04 16:39:18 -07001375 skb->ignore_df = ip_sk_ignore_df(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 /* DF bit is set when we want to see DF on outgoing frames.
WANG Cong60ff7462014-05-04 16:39:18 -07001378 * If ignore_df is set too, we still allow to fragment this frame
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 * locally. */
Hannes Frederic Sowa482fc602013-11-05 02:24:17 +01001380 if (inet->pmtudisc == IP_PMTUDISC_DO ||
1381 inet->pmtudisc == IP_PMTUDISC_PROBE ||
Changli Gaod8d1f302010-06-10 23:31:35 -07001382 (skb->len <= dst_mtu(&rt->dst) &&
1383 ip_dont_fragment(sk, &rt->dst)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 df = htons(IP_DF);
1385
Herbert Xu1470ddf2011-03-01 02:36:47 +00001386 if (cork->flags & IPCORK_OPT)
1387 opt = cork->opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Francesco Fuscoaa661582013-09-24 15:43:09 +02001389 if (cork->ttl != 0)
1390 ttl = cork->ttl;
1391 else if (rt->rt_type == RTN_MULTICAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 ttl = inet->mc_ttl;
1393 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001394 ttl = ip_select_ttl(inet, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Ansis Atteka749154a2013-09-18 15:29:52 -07001396 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 iph->version = 4;
1398 iph->ihl = 5;
Francesco Fuscoaa661582013-09-24 15:43:09 +02001399 iph->tos = (cork->tos != -1) ? cork->tos : inet->tos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 iph->frag_off = df;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 iph->ttl = ttl;
1402 iph->protocol = sk->sk_protocol;
Eric Dumazet84f93072011-11-30 19:00:53 +00001403 ip_copy_addrs(iph, fl4);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001404 ip_select_ident(net, skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
David S. Miller22f728f2011-05-13 17:21:27 -04001406 if (opt) {
1407 iph->ihl += opt->optlen>>2;
1408 ip_options_build(skb, opt, cork->addr, rt, 0);
1409 }
1410
Francesco Fuscoaa661582013-09-24 15:43:09 +02001411 skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -08001412 skb->mark = sk->sk_mark;
Eric Dumazeta21bba92008-11-24 16:07:50 -08001413 /*
1414 * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec
1415 * on dst refcount
1416 */
Herbert Xu1470ddf2011-03-01 02:36:47 +00001417 cork->dst = NULL;
Changli Gaod8d1f302010-06-10 23:31:35 -07001418 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
David L Stevens96793b42007-09-17 09:57:33 -07001420 if (iph->protocol == IPPROTO_ICMP)
Pavel Emelyanov0388b002008-07-14 23:00:43 -07001421 icmp_out_count(net, ((struct icmphdr *)
David L Stevens96793b42007-09-17 09:57:33 -07001422 skb_transport_header(skb))->type);
1423
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001424 ip_cork_release(cork);
1425out:
1426 return skb;
1427}
1428
Eric Dumazetb5ec8ee2012-08-10 02:22:47 +00001429int ip_send_skb(struct net *net, struct sk_buff *skb)
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001430{
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001431 int err;
1432
Herbert Xuc439cb22008-01-11 19:14:00 -08001433 err = ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (err) {
1435 if (err > 0)
Eric Dumazet6ce9e7b2009-09-02 18:05:33 -07001436 err = net_xmit_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 if (err)
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001438 IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
David S. Miller77968b72011-05-08 17:12:19 -07001444int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4)
Herbert Xu1470ddf2011-03-01 02:36:47 +00001445{
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001446 struct sk_buff *skb;
1447
David S. Miller77968b72011-05-08 17:12:19 -07001448 skb = ip_finish_skb(sk, fl4);
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001449 if (!skb)
1450 return 0;
1451
1452 /* Netfilter gets whole the not fragmented skb. */
Eric Dumazetb5ec8ee2012-08-10 02:22:47 +00001453 return ip_send_skb(sock_net(sk), skb);
Herbert Xu1470ddf2011-03-01 02:36:47 +00001454}
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456/*
1457 * Throw away all pending data on the socket.
1458 */
Herbert Xu1470ddf2011-03-01 02:36:47 +00001459static void __ip_flush_pending_frames(struct sock *sk,
1460 struct sk_buff_head *queue,
1461 struct inet_cork *cork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 struct sk_buff *skb;
1464
Herbert Xu1470ddf2011-03-01 02:36:47 +00001465 while ((skb = __skb_dequeue_tail(queue)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 kfree_skb(skb);
1467
Herbert Xu1470ddf2011-03-01 02:36:47 +00001468 ip_cork_release(cork);
1469}
1470
1471void ip_flush_pending_frames(struct sock *sk)
1472{
David S. Millerbdc712b2011-05-06 15:02:07 -07001473 __ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001476struct sk_buff *ip_make_skb(struct sock *sk,
David S. Miller77968b72011-05-08 17:12:19 -07001477 struct flowi4 *fl4,
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001478 int getfrag(void *from, char *to, int offset,
1479 int len, int odd, struct sk_buff *skb),
1480 void *from, int length, int transhdrlen,
1481 struct ipcm_cookie *ipc, struct rtable **rtp,
1482 unsigned int flags)
1483{
David S. Millerb80d7222011-05-06 15:06:01 -07001484 struct inet_cork cork;
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001485 struct sk_buff_head queue;
1486 int err;
1487
1488 if (flags & MSG_PROBE)
1489 return NULL;
1490
1491 __skb_queue_head_init(&queue);
1492
David S. Millerb80d7222011-05-06 15:06:01 -07001493 cork.flags = 0;
1494 cork.addr = 0;
David S. Miller70652722011-05-06 16:01:15 -07001495 cork.opt = NULL;
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001496 err = ip_setup_cork(sk, &cork, ipc, rtp);
1497 if (err)
1498 return ERR_PTR(err);
1499
Eric Dumazet5640f762012-09-23 23:04:42 +00001500 err = __ip_append_data(sk, fl4, &queue, &cork,
1501 &current->task_frag, getfrag,
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001502 from, length, transhdrlen, flags);
1503 if (err) {
1504 __ip_flush_pending_frames(sk, &queue, &cork);
1505 return ERR_PTR(err);
1506 }
1507
David S. Miller77968b72011-05-08 17:12:19 -07001508 return __ip_make_skb(sk, fl4, &queue, &cork);
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001509}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511/*
1512 * Fetch data from kernel space and fill in checksum if needed.
1513 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001514static int ip_reply_glue_bits(void *dptr, char *to, int offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 int len, int odd, struct sk_buff *skb)
1516{
Al Viro50842052006-11-14 21:36:34 -08001517 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 csum = csum_partial_copy_nocheck(dptr+offset, to, len, 0);
1520 skb->csum = csum_block_add(skb->csum, csum, odd);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001521 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001524/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 * Generic function to send a packet as reply to another packet.
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001526 * Used to send some TCP resets/acks so far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 */
Eric Dumazetbdbbb852015-01-29 21:35:05 -08001528void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,
Eric Dumazet24a2d432014-09-27 09:50:55 -07001529 const struct ip_options *sopt,
1530 __be32 daddr, __be32 saddr,
1531 const struct ip_reply_arg *arg,
David S. Miller70e73412012-06-28 03:21:41 -07001532 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001534 struct ip_options_data replyopts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 struct ipcm_cookie ipc;
David S. Miller77968b72011-05-08 17:12:19 -07001536 struct flowi4 fl4;
Eric Dumazet511c3f92009-06-02 05:14:27 +00001537 struct rtable *rt = skb_rtable(skb);
Eric Dumazetbdbbb852015-01-29 21:35:05 -08001538 struct net *net = sock_net(sk);
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001539 struct sk_buff *nskb;
Vasily Averin40620902014-10-15 16:24:02 +04001540 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Eric Dumazet24a2d432014-09-27 09:50:55 -07001542 if (__ip_options_echo(&replyopts.opt.opt, skb, sopt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return;
1544
David S. Miller0a5ebb82011-05-09 13:22:43 -07001545 ipc.addr = daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 ipc.opt = NULL;
Oliver Hartkopp2244d072010-08-17 08:59:14 +00001547 ipc.tx_flags = 0;
Francesco Fuscoaa661582013-09-24 15:43:09 +02001548 ipc.ttl = 0;
1549 ipc.tos = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001551 if (replyopts.opt.opt.optlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 ipc.opt = &replyopts.opt;
1553
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001554 if (replyopts.opt.opt.srr)
1555 daddr = replyopts.opt.opt.faddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
1557
Lorenzo Colittie1108612014-05-13 10:17:33 -07001558 flowi4_init_output(&fl4, arg->bound_dev_if,
1559 IP4_REPLY_MARK(net, skb->mark),
Eric Dumazet66b13d92011-10-24 03:06:21 -04001560 RT_TOS(arg->tos),
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001561 RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol,
David S. Miller77968b72011-05-08 17:12:19 -07001562 ip_reply_arg_flowi_flags(arg),
David S. Miller70e73412012-06-28 03:21:41 -07001563 daddr, saddr,
David S. Miller77968b72011-05-08 17:12:19 -07001564 tcp_hdr(skb)->source, tcp_hdr(skb)->dest);
1565 security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001566 rt = ip_route_output_key(net, &fl4);
David S. Miller77968b72011-05-08 17:12:19 -07001567 if (IS_ERR(rt))
1568 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Eric Dumazetbdbbb852015-01-29 21:35:05 -08001570 inet_sk(sk)->tos = arg->tos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 sk->sk_priority = skb->priority;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001573 sk->sk_protocol = ip_hdr(skb)->protocol;
Patrick McHardyf0e48db2007-06-04 21:32:46 -07001574 sk->sk_bound_dev_if = arg->bound_dev_if;
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001575 sk->sk_sndbuf = sysctl_wmem_default;
Vasily Averin40620902014-10-15 16:24:02 +04001576 err = ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base,
1577 len, 0, &ipc, &rt, MSG_DONTWAIT);
1578 if (unlikely(err)) {
1579 ip_flush_pending_frames(sk);
1580 goto out;
1581 }
1582
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001583 nskb = skb_peek(&sk->sk_write_queue);
1584 if (nskb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (arg->csumoffset >= 0)
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001586 *((__sum16 *)skb_transport_header(nskb) +
1587 arg->csumoffset) = csum_fold(csum_add(nskb->csum,
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001588 arg->csum));
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001589 nskb->ip_summed = CHECKSUM_NONE;
1590 skb_set_queue_mapping(nskb, skb_get_queue_mapping(skb));
David S. Miller77968b72011-05-08 17:12:19 -07001591 ip_push_pending_frames(sk, &fl4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
Vasily Averin40620902014-10-15 16:24:02 +04001593out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 ip_rt_put(rt);
1595}
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597void __init ip_init(void)
1598{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 ip_rt_init();
1600 inet_initpeers();
1601
WANG Cong72c1d3b2014-01-10 16:09:45 -08001602#if defined(CONFIG_IP_MULTICAST)
1603 igmp_mc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604#endif
1605}