blob: 911ea739049a69e98cedd6590a0a8616b4285ab5 [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
Eric W. Biederman694869b2015-06-12 21:55:31 -050086static int
87ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
88 unsigned int mtu,
89 int (*output)(struct net *, struct sock *, struct sk_buff *));
Andy Zhou49d16b22015-05-15 14:15:37 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* Generate a checksum for an outgoing IP datagram. */
Denis Efremov2fbd9672013-05-08 23:19:42 +000092void ip_send_check(struct iphdr *iph)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 iph->check = 0;
95 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
96}
Eric Dumazet4bc2f182010-07-09 21:22:10 +000097EXPORT_SYMBOL(ip_send_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Eric Dumazet7d771aa2015-05-12 06:31:48 -070099static int __ip_local_out_sk(struct sock *sk, struct sk_buff *skb)
Herbert Xuc439cb22008-01-11 19:14:00 -0800100{
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500101 struct net *net = dev_net(skb_dst(skb)->dev);
Herbert Xuc439cb22008-01-11 19:14:00 -0800102 struct iphdr *iph = ip_hdr(skb);
103
104 iph->tot_len = htons(skb->len);
105 ip_send_check(iph);
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500106 return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT,
107 net, sk, skb, NULL, skb_dst(skb)->dev,
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500108 dst_output_okfn);
David Miller7026b1d2015-04-05 22:19:04 -0400109}
110
111int __ip_local_out(struct sk_buff *skb)
112{
113 return __ip_local_out_sk(skb->sk, skb);
Herbert Xuc439cb22008-01-11 19:14:00 -0800114}
115
Eric Dumazetaad88722014-04-15 13:47:15 -0400116int ip_local_out_sk(struct sock *sk, struct sk_buff *skb)
Herbert Xuc439cb22008-01-11 19:14:00 -0800117{
118 int err;
119
120 err = __ip_local_out(skb);
121 if (likely(err == 1))
Eric W. Biederman5a706492015-09-15 20:03:53 -0500122 err = dst_output(sk, skb);
Herbert Xuc439cb22008-01-11 19:14:00 -0800123
124 return err;
125}
Eric Dumazetaad88722014-04-15 13:47:15 -0400126EXPORT_SYMBOL_GPL(ip_local_out_sk);
Herbert Xuc439cb22008-01-11 19:14:00 -0800127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static inline int ip_select_ttl(struct inet_sock *inet, struct dst_entry *dst)
129{
130 int ttl = inet->uc_ttl;
131
132 if (ttl < 0)
David S. Miller323e1262010-12-12 21:55:08 -0800133 ttl = ip4_dst_hoplimit(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return ttl;
135}
136
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900137/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * Add an ip header to a skbuff and send it out.
139 *
140 */
Eric Dumazetcfe673b2015-09-25 07:39:16 -0700141int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk,
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000142 __be32 saddr, __be32 daddr, struct ip_options_rcu *opt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet511c3f92009-06-02 05:14:27 +0000145 struct rtable *rt = skb_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct iphdr *iph;
147
148 /* Build the IP header. */
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000149 skb_push(skb, sizeof(struct iphdr) + (opt ? opt->opt.optlen : 0));
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -0300150 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700151 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 iph->version = 4;
153 iph->ihl = 5;
154 iph->tos = inet->tos;
Changli Gaod8d1f302010-06-10 23:31:35 -0700155 iph->ttl = ip_select_ttl(inet, &rt->dst);
David S. Millerdd927a22011-05-04 12:03:30 -0700156 iph->daddr = (opt && opt->opt.srr ? opt->opt.faddr : daddr);
157 iph->saddr = saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 iph->protocol = sk->sk_protocol;
Eric Dumazetcfe673b2015-09-25 07:39:16 -0700159 if (ip_dont_fragment(sk, &rt->dst)) {
160 iph->frag_off = htons(IP_DF);
161 iph->id = 0;
162 } else {
163 iph->frag_off = 0;
164 __ip_select_ident(sock_net(sk), iph, 1);
165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000167 if (opt && opt->opt.optlen) {
168 iph->ihl += opt->opt.optlen>>2;
169 ip_options_build(skb, &opt->opt, daddr, rt, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 skb->priority = sk->sk_priority;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800173 skb->mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 /* Send it out. */
Herbert Xuc439cb22008-01-11 19:14:00 -0800176 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
Arnaldo Carvalho de Melod8c97a92005-08-09 20:12:12 -0700178EXPORT_SYMBOL_GPL(ip_build_and_send_pkt);
179
Eric W. Biederman694869b2015-06-12 21:55:31 -0500180static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Eric Dumazetadf30902009-06-02 05:19:30 +0000182 struct dst_entry *dst = skb_dst(skb);
Mitsuru Chinen80787eb2007-04-30 00:48:20 -0700183 struct rtable *rt = (struct rtable *)dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct net_device *dev = dst->dev;
Chuck Leverc2636b42007-10-23 21:07:32 -0700185 unsigned int hh_len = LL_RESERVED_SPACE(dev);
David S. Millerf6b72b622011-07-14 07:53:20 -0700186 struct neighbour *neigh;
David S. Millera263b302012-07-02 02:02:15 -0700187 u32 nexthop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Neil Hormanedf391f2009-04-27 02:45:02 -0700189 if (rt->rt_type == RTN_MULTICAST) {
Eric W. Biederman4ba1bf42015-09-15 20:04:01 -0500190 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTMCAST, skb->len);
Neil Hormanedf391f2009-04-27 02:45:02 -0700191 } else if (rt->rt_type == RTN_BROADCAST)
Eric W. Biederman4ba1bf42015-09-15 20:04:01 -0500192 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTBCAST, skb->len);
Mitsuru Chinen80787eb2007-04-30 00:48:20 -0700193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 /* Be paranoid, rather than too clever. */
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700195 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 struct sk_buff *skb2;
197
198 skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
Ian Morris51456b22015-04-03 09:17:26 +0100199 if (!skb2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 kfree_skb(skb);
201 return -ENOMEM;
202 }
203 if (skb->sk)
204 skb_set_owner_w(skb2, skb->sk);
Eric Dumazet5d0ba552012-06-04 01:17:19 +0000205 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 skb = skb2;
207 }
208
David S. Millera263b302012-07-02 02:02:15 -0700209 rcu_read_lock_bh();
Julian Anastasov155e8332012-10-08 11:41:18 +0000210 nexthop = (__force u32) rt_nexthop(rt, ip_hdr(skb)->daddr);
David S. Millera263b302012-07-02 02:02:15 -0700211 neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
212 if (unlikely(!neigh))
213 neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
Vasiliy Kulikov9871f1a2012-08-06 03:55:29 +0000214 if (!IS_ERR(neigh)) {
David S. Miller5110effe2012-07-02 02:21:03 -0700215 int res = dst_neigh_output(dst, neigh, skb);
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000216
David S. Millera263b302012-07-02 02:02:15 -0700217 rcu_read_unlock_bh();
Eric Dumazetf2c31e32011-07-29 19:00:53 +0000218 return res;
219 }
David S. Millera263b302012-07-02 02:02:15 -0700220 rcu_read_unlock_bh();
David S. Miller05e3aa02011-07-16 17:26:00 -0700221
Joe Perchese87cc472012-05-13 21:56:26 +0000222 net_dbg_ratelimited("%s: No header cache and no neighbour!\n",
223 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 kfree_skb(skb);
225 return -EINVAL;
226}
227
Eric W. Biederman694869b2015-06-12 21:55:31 -0500228static int ip_finish_output_gso(struct net *net, struct sock *sk,
229 struct sk_buff *skb, unsigned int mtu)
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200230{
231 netdev_features_t features;
232 struct sk_buff *segs;
233 int ret = 0;
234
235 /* common case: locally created skb or seglen is <= mtu */
236 if (((IPCB(skb)->flags & IPSKB_FORWARDED) == 0) ||
Florian Westphalc5501eb2015-05-22 16:32:50 +0200237 skb_gso_network_seglen(skb) <= mtu)
Eric W. Biederman694869b2015-06-12 21:55:31 -0500238 return ip_finish_output2(net, sk, skb);
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200239
240 /* Slowpath - GSO segment length is exceeding the dst MTU.
241 *
242 * This can happen in two cases:
243 * 1) TCP GRO packet, DF bit not set
244 * 2) skb arrived via virtio-net, we thus get TSO/GSO skbs directly
245 * from host network stack.
246 */
247 features = netif_skb_features(skb);
248 segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
Florian Westphal330966e2014-10-20 13:49:17 +0200249 if (IS_ERR_OR_NULL(segs)) {
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200250 kfree_skb(skb);
251 return -ENOMEM;
252 }
253
254 consume_skb(skb);
255
256 do {
257 struct sk_buff *nskb = segs->next;
258 int err;
259
260 segs->next = NULL;
Eric W. Biederman694869b2015-06-12 21:55:31 -0500261 err = ip_fragment(net, sk, segs, mtu, ip_finish_output2);
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200262
263 if (err && ret == 0)
264 ret = err;
265 segs = nskb;
266 } while (segs);
267
268 return ret;
269}
270
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500271static int ip_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Florian Westphalc5501eb2015-05-22 16:32:50 +0200273 unsigned int mtu;
274
Patrick McHardy5c901da2006-01-06 23:05:36 -0800275#if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
276 /* Policy lookup after SNAT yielded a new policy */
Ian Morris00db4122015-04-03 09:17:27 +0100277 if (skb_dst(skb)->xfrm) {
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800278 IPCB(skb)->flags |= IPSKB_REROUTED;
Eric W. Biederman5a706492015-09-15 20:03:53 -0500279 return dst_output(sk, skb);
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800280 }
Patrick McHardy5c901da2006-01-06 23:05:36 -0800281#endif
Florian Westphalc5501eb2015-05-22 16:32:50 +0200282 mtu = ip_skb_dst_mtu(skb);
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200283 if (skb_is_gso(skb))
Eric W. Biederman694869b2015-06-12 21:55:31 -0500284 return ip_finish_output_gso(net, sk, skb, mtu);
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200285
Florian Westphald6b915e2015-05-22 16:32:51 +0200286 if (skb->len > mtu || (IPCB(skb)->flags & IPSKB_FRAG_PMTU))
Eric W. Biederman694869b2015-06-12 21:55:31 -0500287 return ip_fragment(net, sk, skb, mtu, ip_finish_output2);
Florian Westphalc7ba65d2014-05-05 15:00:43 +0200288
Eric W. Biederman694869b2015-06-12 21:55:31 -0500289 return ip_finish_output2(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Eric Dumazetaad88722014-04-15 13:47:15 -0400292int ip_mc_output(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Eric Dumazet511c3f92009-06-02 05:14:27 +0000294 struct rtable *rt = skb_rtable(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -0700295 struct net_device *dev = rt->dst.dev;
Eric W. Biederman88f5cc22015-09-15 20:03:57 -0500296 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 /*
299 * If the indicated interface is up and running, send the packet.
300 */
Eric W. Biederman88f5cc22015-09-15 20:03:57 -0500301 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 skb->dev = dev;
304 skb->protocol = htons(ETH_P_IP);
305
306 /*
307 * Multicasts are looped back for other local users
308 */
309
310 if (rt->rt_flags&RTCF_MULTICAST) {
Octavian Purdila7ad68482010-01-06 20:37:01 -0800311 if (sk_mc_loop(sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312#ifdef CONFIG_IP_MROUTE
313 /* Small optimization: do not loopback not local frames,
314 which returned after forwarding; they will be dropped
315 by ip_mr_input in any case.
316 Note, that local frames are looped back to be delivered
317 to local recipients.
318
319 This check is duplicated in ip_mr_input at the moment.
320 */
Joe Perches9d4fb272009-11-23 10:41:23 -0800321 &&
322 ((rt->rt_flags & RTCF_LOCAL) ||
323 !(IPCB(skb)->flags & IPSKB_FORWARDED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324#endif
Joe Perches9d4fb272009-11-23 10:41:23 -0800325 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
327 if (newskb)
Jan Engelhardt9bbc7682010-03-23 04:07:29 +0100328 NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500329 net, sk, newskb, NULL, newskb->dev,
Michel Machado95603e22012-06-12 10:16:35 +0000330 dev_loopback_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332
333 /* Multicasts with ttl 0 must not go beyond the host */
334
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700335 if (ip_hdr(skb)->ttl == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 kfree_skb(skb);
337 return 0;
338 }
339 }
340
341 if (rt->rt_flags&RTCF_BROADCAST) {
342 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
343 if (newskb)
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500344 NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
345 net, sk, newskb, NULL, newskb->dev,
346 dev_loopback_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500349 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
350 net, sk, skb, NULL, skb->dev,
351 ip_finish_output,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800352 !(IPCB(skb)->flags & IPSKB_REROUTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Eric Dumazetaad88722014-04-15 13:47:15 -0400355int ip_output(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Eric Dumazetadf30902009-06-02 05:19:30 +0000357 struct net_device *dev = skb_dst(skb)->dev;
Eric W. Biederman88f5cc22015-09-15 20:03:57 -0500358 struct net *net = dev_net(dev);
Patrick McHardy1bd9bef2006-01-05 12:20:59 -0800359
Eric W. Biederman88f5cc22015-09-15 20:03:57 -0500360 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Patrick McHardy1bd9bef2006-01-05 12:20:59 -0800362 skb->dev = dev;
363 skb->protocol = htons(ETH_P_IP);
364
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500365 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
366 net, sk, skb, NULL, dev,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900367 ip_finish_output,
Patrick McHardy48d5cad2006-02-15 15:10:22 -0800368 !(IPCB(skb)->flags & IPSKB_REROUTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Eric Dumazet84f93072011-11-30 19:00:53 +0000371/*
372 * copy saddr and daddr, possibly using 64bit load/stores
373 * Equivalent to :
374 * iph->saddr = fl4->saddr;
375 * iph->daddr = fl4->daddr;
376 */
377static void ip_copy_addrs(struct iphdr *iph, const struct flowi4 *fl4)
378{
379 BUILD_BUG_ON(offsetof(typeof(*fl4), daddr) !=
380 offsetof(typeof(*fl4), saddr) + sizeof(fl4->saddr));
381 memcpy(&iph->saddr, &fl4->saddr,
382 sizeof(fl4->saddr) + sizeof(fl4->daddr));
383}
384
Eric Dumazetb0270e92014-04-15 12:58:34 -0400385/* Note: skb->sk can be different from sk, in case of tunnels */
386int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct inet_sock *inet = inet_sk(sk);
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000389 struct ip_options_rcu *inet_opt;
David S. Millerb57ae012011-05-06 16:24:06 -0700390 struct flowi4 *fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 struct rtable *rt;
392 struct iphdr *iph;
Eric Dumazetab6e3fe2010-05-10 11:31:49 +0000393 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /* Skip all of this if the packet is already routed,
396 * f.e. by something like SCTP.
397 */
Eric Dumazetab6e3fe2010-05-10 11:31:49 +0000398 rcu_read_lock();
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000399 inet_opt = rcu_dereference(inet->inet_opt);
David S. Millerea4fc0d2011-05-06 22:30:20 -0700400 fl4 = &fl->u.ip4;
Eric Dumazet511c3f92009-06-02 05:14:27 +0000401 rt = skb_rtable(skb);
Ian Morris00db4122015-04-03 09:17:27 +0100402 if (rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 goto packet_routed;
404
405 /* Make sure we can route this packet. */
406 rt = (struct rtable *)__sk_dst_check(sk, 0);
Ian Morris51456b22015-04-03 09:17:26 +0100407 if (!rt) {
Al Viro3ca3c682006-09-27 18:28:07 -0700408 __be32 daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /* Use correct destination address if we have options. */
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000411 daddr = inet->inet_daddr;
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000412 if (inet_opt && inet_opt->opt.srr)
413 daddr = inet_opt->opt.faddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
David S. Miller78fbfd82011-03-12 00:00:52 -0500415 /* If this fails, retransmit mechanism of transport layer will
416 * keep trying until route appears or the connection times
417 * itself out.
418 */
David S. Millerb57ae012011-05-06 16:24:06 -0700419 rt = ip_route_output_ports(sock_net(sk), fl4, sk,
David S. Miller78fbfd82011-03-12 00:00:52 -0500420 daddr, inet->inet_saddr,
421 inet->inet_dport,
422 inet->inet_sport,
423 sk->sk_protocol,
424 RT_CONN_FLAGS(sk),
425 sk->sk_bound_dev_if);
426 if (IS_ERR(rt))
427 goto no_route;
Changli Gaod8d1f302010-06-10 23:31:35 -0700428 sk_setup_caps(sk, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700430 skb_dst_set_noref(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432packet_routed:
Julian Anastasov155e8332012-10-08 11:41:18 +0000433 if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_uses_gateway)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 goto no_route;
435
436 /* OK, we know where to send it, allocate and build IP header. */
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000437 skb_push(skb, sizeof(struct iphdr) + (inet_opt ? inet_opt->opt.optlen : 0));
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -0300438 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700439 iph = ip_hdr(skb);
Al Viro714e85b2006-11-14 20:51:49 -0800440 *((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (inet->tos & 0xff));
WANG Cong60ff7462014-05-04 16:39:18 -0700441 if (ip_dont_fragment(sk, &rt->dst) && !skb->ignore_df)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 iph->frag_off = htons(IP_DF);
443 else
444 iph->frag_off = 0;
Changli Gaod8d1f302010-06-10 23:31:35 -0700445 iph->ttl = ip_select_ttl(inet, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 iph->protocol = sk->sk_protocol;
Eric Dumazet84f93072011-11-30 19:00:53 +0000447 ip_copy_addrs(iph, fl4);
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /* Transport layer set skb->h.foo itself. */
450
Eric Dumazetf6d8bd02011-04-21 09:45:37 +0000451 if (inet_opt && inet_opt->opt.optlen) {
452 iph->ihl += inet_opt->opt.optlen >> 2;
453 ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100456 ip_select_ident_segs(sock_net(sk), skb, sk,
457 skb_shinfo(skb)->gso_segs ?: 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Eric Dumazetb0270e92014-04-15 12:58:34 -0400459 /* TODO : should we use skb->sk here instead of sk ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 skb->priority = sk->sk_priority;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -0800461 skb->mark = sk->sk_mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Eric Dumazetab6e3fe2010-05-10 11:31:49 +0000463 res = ip_local_out(skb);
464 rcu_read_unlock();
465 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467no_route:
Eric Dumazetab6e3fe2010-05-10 11:31:49 +0000468 rcu_read_unlock();
Pavel Emelyanov5e38e272008-07-16 20:19:49 -0700469 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 kfree_skb(skb);
471 return -EHOSTUNREACH;
472}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000473EXPORT_SYMBOL(ip_queue_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from)
476{
477 to->pkt_type = from->pkt_type;
478 to->priority = from->priority;
479 to->protocol = from->protocol;
Eric Dumazetadf30902009-06-02 05:19:30 +0000480 skb_dst_drop(to);
Eric Dumazetfe76cda2010-07-01 23:48:22 +0000481 skb_dst_copy(to, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 to->dev = from->dev;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800483 to->mark = from->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 /* Copy the flags to each fragment. */
486 IPCB(to)->flags = IPCB(from)->flags;
487
488#ifdef CONFIG_NET_SCHED
489 to->tc_index = from->tc_index;
490#endif
Yasuyuki Kozakaie7ac05f2007-03-14 16:44:01 -0700491 nf_copy(to, from);
Julian Anastasovc98d80e2005-10-22 13:39:21 +0300492#if defined(CONFIG_IP_VS) || defined(CONFIG_IP_VS_MODULE)
493 to->ipvs_property = from->ipvs_property;
494#endif
James Morris984bc162006-06-09 00:29:17 -0700495 skb_copy_secmark(to, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Eric W. Biederman694869b2015-06-12 21:55:31 -0500498static int ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
Florian Westphalc5501eb2015-05-22 16:32:50 +0200499 unsigned int mtu,
Eric W. Biederman694869b2015-06-12 21:55:31 -0500500 int (*output)(struct net *, struct sock *, struct sk_buff *))
Andy Zhou49d16b22015-05-15 14:15:37 -0700501{
502 struct iphdr *iph = ip_hdr(skb);
Andy Zhou49d16b22015-05-15 14:15:37 -0700503
Florian Westphald6b915e2015-05-22 16:32:51 +0200504 if ((iph->frag_off & htons(IP_DF)) == 0)
Eric W. Biederman694869b2015-06-12 21:55:31 -0500505 return ip_do_fragment(net, sk, skb, output);
Florian Westphald6b915e2015-05-22 16:32:51 +0200506
507 if (unlikely(!skb->ignore_df ||
Andy Zhou49d16b22015-05-15 14:15:37 -0700508 (IPCB(skb)->frag_max_size &&
509 IPCB(skb)->frag_max_size > mtu))) {
Eric W. Biederman9479b0a2015-09-15 20:04:00 -0500510 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Andy Zhou49d16b22015-05-15 14:15:37 -0700511 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
512 htonl(mtu));
513 kfree_skb(skb);
514 return -EMSGSIZE;
515 }
516
Eric W. Biederman694869b2015-06-12 21:55:31 -0500517 return ip_do_fragment(net, sk, skb, output);
Andy Zhou49d16b22015-05-15 14:15:37 -0700518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520/*
521 * This IP datagram is too large to be sent in one piece. Break it up into
522 * smaller pieces (each of size equal to IP header plus
523 * a block of the data of the original IP data part) that will yet fit in a
524 * single device frame, and queue such a frame for sending.
525 */
526
Eric W. Biederman694869b2015-06-12 21:55:31 -0500527int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
528 int (*output)(struct net *, struct sock *, struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int ptr;
532 struct net_device *dev;
533 struct sk_buff *skb2;
Changli Gaoc893b802010-07-31 13:25:08 +0000534 unsigned int mtu, hlen, left, len, ll_rs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 int offset;
Alexey Dobriyan76ab6082006-01-06 13:24:29 -0800536 __be16 not_last_frag;
Eric Dumazet511c3f92009-06-02 05:14:27 +0000537 struct rtable *rt = skb_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 int err = 0;
539
Changli Gaod8d1f302010-06-10 23:31:35 -0700540 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 /*
543 * Point into the IP datagram header.
544 */
545
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700546 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Hannes Frederic Sowa69647ce2014-02-26 01:20:41 +0100548 mtu = ip_skb_dst_mtu(skb);
Florian Westphald6b915e2015-05-22 16:32:51 +0200549 if (IPCB(skb)->frag_max_size && IPCB(skb)->frag_max_size < mtu)
550 mtu = IPCB(skb)->frag_max_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 /*
553 * Setup starting values.
554 */
555
556 hlen = iph->ihl * 4;
Hannes Frederic Sowaf87c10a2014-01-09 10:01:15 +0100557 mtu = mtu - hlen; /* Size of data space */
Herbert Xu89cee8b2005-12-13 23:14:27 -0800558 IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /* When frag_list is given, use it. First, check its validity:
561 * some transformers could create wrong frag_list or break existing
562 * one, it is not prohibited. In this case fall back to copying.
563 *
564 * LATER: this step can be merged to real generation of fragments,
565 * we can switch to copy when see the first bad fragment.
566 */
David S. Miller21dc3302010-08-23 00:13:46 -0700567 if (skb_has_frag_list(skb)) {
Eric Dumazet3d130082010-09-21 08:47:45 +0000568 struct sk_buff *frag, *frag2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 int first_len = skb_pagelen(skb);
570
571 if (first_len - hlen > mtu ||
572 ((first_len - hlen) & 7) ||
Paul Gortmaker56f8a752011-06-21 20:33:34 -0700573 ip_is_fragment(iph) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 skb_cloned(skb))
575 goto slow_path;
576
David S. Millerd7fcf1a2009-06-09 00:19:37 -0700577 skb_walk_frags(skb, frag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* Correct geometry. */
579 if (frag->len > mtu ||
580 ((frag->len & 7) && frag->next) ||
581 skb_headroom(frag) < hlen)
Eric Dumazet3d130082010-09-21 08:47:45 +0000582 goto slow_path_clean;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 /* Partially cloned skb? */
585 if (skb_shared(frag))
Eric Dumazet3d130082010-09-21 08:47:45 +0000586 goto slow_path_clean;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700587
588 BUG_ON(frag->sk);
589 if (skb->sk) {
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700590 frag->sk = skb->sk;
591 frag->destructor = sock_wfree;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700592 }
Eric Dumazet3d130082010-09-21 08:47:45 +0000593 skb->truesize -= frag->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595
596 /* Everything is OK. Generate! */
597
598 err = 0;
599 offset = 0;
600 frag = skb_shinfo(skb)->frag_list;
David S. Millerd7fcf1a2009-06-09 00:19:37 -0700601 skb_frag_list_init(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 skb->data_len = first_len - skb_headlen(skb);
603 skb->len = first_len;
604 iph->tot_len = htons(first_len);
605 iph->frag_off = htons(IP_MF);
606 ip_send_check(iph);
607
608 for (;;) {
609 /* Prepare header of the next frame,
610 * before previous one went down. */
611 if (frag) {
612 frag->ip_summed = CHECKSUM_NONE;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300613 skb_reset_transport_header(frag);
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700614 __skb_push(frag, hlen);
615 skb_reset_network_header(frag);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700616 memcpy(skb_network_header(frag), iph, hlen);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700617 iph = ip_hdr(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 iph->tot_len = htons(frag->len);
619 ip_copy_metadata(frag, skb);
620 if (offset == 0)
621 ip_options_fragment(frag);
622 offset += skb->len - hlen;
623 iph->frag_off = htons(offset>>3);
Ian Morris00db4122015-04-03 09:17:27 +0100624 if (frag->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 iph->frag_off |= htons(IP_MF);
626 /* Ready, complete checksum */
627 ip_send_check(iph);
628 }
629
Eric W. Biederman694869b2015-06-12 21:55:31 -0500630 err = output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Wei Dongdafee492006-08-02 13:41:21 -0700632 if (!err)
Eric W. Biederman26a949d2015-09-15 20:03:59 -0500633 IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (err || !frag)
635 break;
636
637 skb = frag;
638 frag = skb->next;
639 skb->next = NULL;
640 }
641
642 if (err == 0) {
Eric W. Biederman26a949d2015-09-15 20:03:59 -0500643 IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return 0;
645 }
646
647 while (frag) {
648 skb = frag->next;
649 kfree_skb(frag);
650 frag = skb;
651 }
Eric W. Biederman26a949d2015-09-15 20:03:59 -0500652 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return err;
Eric Dumazet3d130082010-09-21 08:47:45 +0000654
655slow_path_clean:
656 skb_walk_frags(skb, frag2) {
657 if (frag2 == frag)
658 break;
659 frag2->sk = NULL;
660 frag2->destructor = NULL;
661 skb->truesize += frag2->truesize;
662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
665slow_path:
Alexander Duyckfc70fb62012-12-07 14:14:15 +0000666 /* for offloaded checksums cleanup checksum before fragmentation */
667 if ((skb->ip_summed == CHECKSUM_PARTIAL) && skb_checksum_help(skb))
668 goto fail;
Pravin B Shelarc9af6db2013-02-11 09:27:41 +0000669 iph = ip_hdr(skb);
Alexander Duyckfc70fb62012-12-07 14:14:15 +0000670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 left = skb->len - hlen; /* Space per frame */
George Kadianakis49085bd2010-07-06 11:44:12 +0000672 ptr = hlen; /* Where to start from */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Florian Westphal8d045162015-03-18 20:55:31 +0100674 ll_rs = LL_RESERVED_SPACE(rt->dst.dev);
Stephen Hemminger9bcfcaf2006-08-29 17:48:57 -0700675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /*
677 * Fragment the datagram.
678 */
679
680 offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3;
681 not_last_frag = iph->frag_off & htons(IP_MF);
682
683 /*
684 * Keep copying data until we run out.
685 */
686
Stephen Hemminger132adf52007-03-08 20:44:43 -0800687 while (left > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 len = left;
689 /* IF: it doesn't fit, use 'mtu' - the data space left */
690 if (len > mtu)
691 len = mtu;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300692 /* IF: we are not sending up to and including the packet end
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 then align the next start on an eight byte boundary */
694 if (len < left) {
695 len &= ~7;
696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Joe Perchescbffccc2014-11-05 14:39:21 -0800698 /* Allocate buffer */
699 skb2 = alloc_skb(len + hlen + ll_rs, GFP_ATOMIC);
700 if (!skb2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 err = -ENOMEM;
702 goto fail;
703 }
704
705 /*
706 * Set up data on packet
707 */
708
709 ip_copy_metadata(skb2, skb);
710 skb_reserve(skb2, ll_rs);
711 skb_put(skb2, len + hlen);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700712 skb_reset_network_header(skb2);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700713 skb2->transport_header = skb2->network_header + hlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 /*
716 * Charge the memory for the fragment to any owner
717 * it might possess
718 */
719
720 if (skb->sk)
721 skb_set_owner_w(skb2, skb->sk);
722
723 /*
724 * Copy the packet header into the new buffer.
725 */
726
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300727 skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 /*
730 * Copy a block of the IP datagram.
731 */
Arnaldo Carvalho de Melobff9b612007-03-16 17:19:57 -0300732 if (skb_copy_bits(skb, ptr, skb_transport_header(skb2), len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 BUG();
734 left -= len;
735
736 /*
737 * Fill in the new header fields.
738 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700739 iph = ip_hdr(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 iph->frag_off = htons((offset >> 3));
741
Florian Westphald6b915e2015-05-22 16:32:51 +0200742 if (IPCB(skb)->flags & IPSKB_FRAG_PMTU)
743 iph->frag_off |= htons(IP_DF);
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /* ANK: dirty, but effective trick. Upgrade options only if
746 * the segment to be fragmented was THE FIRST (otherwise,
747 * options are already fixed) and make it ONCE
748 * on the initial skb, so that all the following fragments
749 * will inherit fixed options.
750 */
751 if (offset == 0)
752 ip_options_fragment(skb);
753
754 /*
755 * Added AC : If we are fragmenting a fragment that's not the
756 * last fragment then keep MF on each bit
757 */
758 if (left > 0 || not_last_frag)
759 iph->frag_off |= htons(IP_MF);
760 ptr += len;
761 offset += len;
762
763 /*
764 * Put this fragment into the sending queue.
765 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 iph->tot_len = htons(len + hlen);
767
768 ip_send_check(iph);
769
Eric W. Biederman694869b2015-06-12 21:55:31 -0500770 err = output(net, sk, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (err)
772 goto fail;
Wei Dongdafee492006-08-02 13:41:21 -0700773
Eric W. Biederman26a949d2015-09-15 20:03:59 -0500774 IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
Eric Dumazet5d0ba552012-06-04 01:17:19 +0000776 consume_skb(skb);
Eric W. Biederman26a949d2015-09-15 20:03:59 -0500777 IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return err;
779
780fail:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900781 kfree_skb(skb);
Eric W. Biederman26a949d2015-09-15 20:03:59 -0500782 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return err;
784}
Andy Zhou49d16b22015-05-15 14:15:37 -0700785EXPORT_SYMBOL(ip_do_fragment);
Patrick McHardy2e2f7ae2006-04-04 13:42:35 -0700786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787int
788ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
789{
Al Virof69e6d12014-11-24 13:23:40 -0500790 struct msghdr *msg = from;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Patrick McHardy84fa7932006-08-29 16:44:56 -0700792 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Al Viro21226ab2014-11-28 15:48:29 -0500793 if (copy_from_iter(to, len, &msg->msg_iter) != len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return -EFAULT;
795 } else {
Al Viro44bb9362006-11-14 21:36:14 -0800796 __wsum csum = 0;
Al Viro21226ab2014-11-28 15:48:29 -0500797 if (csum_and_copy_from_iter(to, len, &csum, &msg->msg_iter) != len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return -EFAULT;
799 skb->csum = csum_block_add(skb->csum, csum, odd);
800 }
801 return 0;
802}
Eric Dumazet4bc2f182010-07-09 21:22:10 +0000803EXPORT_SYMBOL(ip_generic_getfrag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Al Viro44bb9362006-11-14 21:36:14 -0800805static inline __wsum
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806csum_page(struct page *page, int offset, int copy)
807{
808 char *kaddr;
Al Viro44bb9362006-11-14 21:36:14 -0800809 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 kaddr = kmap(page);
811 csum = csum_partial(kaddr + offset, copy, 0);
812 kunmap(page);
813 return csum;
814}
815
Adrian Bunk4b30b1c2005-11-29 16:27:20 -0800816static inline int ip_ufo_append_data(struct sock *sk,
Herbert Xu1470ddf2011-03-01 02:36:47 +0000817 struct sk_buff_head *queue,
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700818 int getfrag(void *from, char *to, int offset, int len,
819 int odd, struct sk_buff *skb),
820 void *from, int length, int hh_len, int fragheaderlen,
Bill Sommerfeldd9be4f72011-07-19 15:22:33 +0000821 int transhdrlen, int maxfraglen, unsigned int flags)
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700822{
823 struct sk_buff *skb;
824 int err;
825
826 /* There is support for UDP fragmentation offload by network
827 * device, so create one single skb packet containing complete
828 * udp datagram
829 */
Ian Morris51456b22015-04-03 09:17:26 +0100830 skb = skb_peek_tail(queue);
831 if (!skb) {
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700832 skb = sock_alloc_send_skb(sk,
833 hh_len + fragheaderlen + transhdrlen + 20,
834 (flags & MSG_DONTWAIT), &err);
835
Ian Morris51456b22015-04-03 09:17:26 +0100836 if (!skb)
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700837 return err;
838
839 /* reserve space for Hardware header */
840 skb_reserve(skb, hh_len);
841
842 /* create space for UDP/IP header */
Jianjun Kongd93191002008-11-03 00:23:42 -0800843 skb_put(skb, fragheaderlen + transhdrlen);
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700844
845 /* initialize network header pointer */
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700846 skb_reset_network_header(skb);
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700847
848 /* initialize protocol header pointer */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700849 skb->transport_header = skb->network_header + fragheaderlen;
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700850
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700851 skb->csum = 0;
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700852
Herbert Xu1470ddf2011-03-01 02:36:47 +0000853 __skb_queue_tail(queue, skb);
Jiri Pirkoe93b7d72013-10-19 12:29:17 +0200854 } else if (skb_is_gso(skb)) {
855 goto append;
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700856 }
Kostya Bbe9164e2008-04-29 22:36:30 -0700857
Jiri Pirkoe93b7d72013-10-19 12:29:17 +0200858 skb->ip_summed = CHECKSUM_PARTIAL;
859 /* specify the length of each IP datagram fragment */
860 skb_shinfo(skb)->gso_size = maxfraglen - fragheaderlen;
861 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
862
863append:
Kostya Bbe9164e2008-04-29 22:36:30 -0700864 return skb_append_datato_frags(sk, skb, getfrag, from,
865 (length - transhdrlen));
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700866}
867
David S. Millerf5fca602011-05-08 17:24:10 -0700868static int __ip_append_data(struct sock *sk,
869 struct flowi4 *fl4,
870 struct sk_buff_head *queue,
Herbert Xu1470ddf2011-03-01 02:36:47 +0000871 struct inet_cork *cork,
Eric Dumazet5640f762012-09-23 23:04:42 +0000872 struct page_frag *pfrag,
Herbert Xu1470ddf2011-03-01 02:36:47 +0000873 int getfrag(void *from, char *to, int offset,
874 int len, int odd, struct sk_buff *skb),
875 void *from, int length, int transhdrlen,
876 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 struct inet_sock *inet = inet_sk(sk);
879 struct sk_buff *skb;
880
Herbert Xu07df5292011-03-01 23:00:58 -0800881 struct ip_options *opt = cork->opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 int hh_len;
883 int exthdrlen;
884 int mtu;
885 int copy;
886 int err;
887 int offset = 0;
Hannes Frederic Sowadaba2872013-10-27 17:29:11 +0100888 unsigned int maxfraglen, fragheaderlen, maxnonfragsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 int csummode = CHECKSUM_NONE;
Herbert Xu1470ddf2011-03-01 02:36:47 +0000890 struct rtable *rt = (struct rtable *)cork->dst;
Willem de Bruijn09c2d252014-08-04 22:11:47 -0400891 u32 tskey = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Steffen Klassert96d73032011-06-05 20:48:47 +0000893 skb = skb_peek_tail(queue);
894
895 exthdrlen = !skb ? rt->dst.header_len : 0;
Herbert Xu07df5292011-03-01 23:00:58 -0800896 mtu = cork->fragsize;
Willem de Bruijn09c2d252014-08-04 22:11:47 -0400897 if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP &&
898 sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)
899 tskey = sk->sk_tskey++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Changli Gaod8d1f302010-06-10 23:31:35 -0700901 hh_len = LL_RESERVED_SPACE(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
904 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
WANG Cong60ff7462014-05-04 16:39:18 -0700905 maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Hannes Frederic Sowadaba2872013-10-27 17:29:11 +0100907 if (cork->length + length > maxnonfragsize - fragheaderlen) {
David S. Millerf5fca602011-05-08 17:24:10 -0700908 ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
Hannes Frederic Sowa61e7f092013-12-19 02:13:36 +0100909 mtu - (opt ? opt->optlen : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return -EMSGSIZE;
911 }
912
913 /*
914 * transhdrlen > 0 means that this is the first fragment and we wish
915 * it won't be fragmented in the future.
916 */
917 if (transhdrlen &&
918 length + fragheaderlen <= mtu &&
Changli Gaod8d1f302010-06-10 23:31:35 -0700919 rt->dst.dev->features & NETIF_F_V4_CSUM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 !exthdrlen)
Patrick McHardy84fa7932006-08-29 16:44:56 -0700921 csummode = CHECKSUM_PARTIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Herbert Xu1470ddf2011-03-01 02:36:47 +0000923 cork->length += length;
Herbert Xu26cde9f2010-06-15 01:52:25 +0000924 if (((length > mtu) || (skb && skb_is_gso(skb))) &&
Kostya Bbe9164e2008-04-29 22:36:30 -0700925 (sk->sk_protocol == IPPROTO_UDP) &&
Michal Kubečekacf8dd02015-03-02 18:27:11 +0100926 (rt->dst.dev->features & NETIF_F_UFO) && !rt->dst.header_len &&
927 (sk->sk_type == SOCK_DGRAM)) {
Herbert Xu1470ddf2011-03-01 02:36:47 +0000928 err = ip_ufo_append_data(sk, queue, getfrag, from, length,
929 hh_len, fragheaderlen, transhdrlen,
Bill Sommerfeldd9be4f72011-07-19 15:22:33 +0000930 maxfraglen, flags);
Patrick McHardybaa829d2006-03-12 20:35:12 -0800931 if (err)
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700932 goto error;
Ananda Rajue89e9cf2005-10-18 15:46:41 -0700933 return 0;
934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 /* So, what's going on in the loop below?
937 *
938 * We use calculated fragment length to generate chained skb,
939 * each of segments is IP fragment ready for sending to network after
940 * adding appropriate IP header.
941 */
942
Herbert Xu26cde9f2010-06-15 01:52:25 +0000943 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 goto alloc_new_skb;
945
946 while (length > 0) {
947 /* Check if the remaining data fits into current packet. */
948 copy = mtu - skb->len;
949 if (copy < length)
950 copy = maxfraglen - skb->len;
951 if (copy <= 0) {
952 char *data;
953 unsigned int datalen;
954 unsigned int fraglen;
955 unsigned int fraggap;
956 unsigned int alloclen;
957 struct sk_buff *skb_prev;
958alloc_new_skb:
959 skb_prev = skb;
960 if (skb_prev)
961 fraggap = skb_prev->len - maxfraglen;
962 else
963 fraggap = 0;
964
965 /*
966 * If remaining data exceeds the mtu,
967 * we know we need more fragment(s).
968 */
969 datalen = length + fraggap;
970 if (datalen > mtu - fragheaderlen)
971 datalen = maxfraglen - fragheaderlen;
972 fraglen = datalen + fragheaderlen;
973
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900974 if ((flags & MSG_MORE) &&
Changli Gaod8d1f302010-06-10 23:31:35 -0700975 !(rt->dst.dev->features&NETIF_F_SG))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 alloclen = mtu;
977 else
Eric Dumazet59104f02010-09-20 20:16:27 +0000978 alloclen = fraglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Steffen Klassert353e5c92011-06-22 01:05:37 +0000980 alloclen += exthdrlen;
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /* The last fragment gets additional space at tail.
983 * Note, with MSG_MORE we overallocate on fragments,
984 * because we have no idea what fragment will be
985 * the last.
986 */
Steffen Klassert33f99dc2011-06-22 01:04:37 +0000987 if (datalen == length + fraggap)
Changli Gaod8d1f302010-06-10 23:31:35 -0700988 alloclen += rt->dst.trailer_len;
Steffen Klassert33f99dc2011-06-22 01:04:37 +0000989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (transhdrlen) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900991 skb = sock_alloc_send_skb(sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 alloclen + hh_len + 15,
993 (flags & MSG_DONTWAIT), &err);
994 } else {
995 skb = NULL;
996 if (atomic_read(&sk->sk_wmem_alloc) <=
997 2 * sk->sk_sndbuf)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900998 skb = sock_wmalloc(sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 alloclen + hh_len + 15, 1,
1000 sk->sk_allocation);
Ian Morris51456b22015-04-03 09:17:26 +01001001 if (unlikely(!skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 err = -ENOBUFS;
1003 }
Ian Morris51456b22015-04-03 09:17:26 +01001004 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 goto error;
1006
1007 /*
1008 * Fill in the control structures
1009 */
1010 skb->ip_summed = csummode;
1011 skb->csum = 0;
1012 skb_reserve(skb, hh_len);
Willem de Bruijn11878b42014-07-14 17:55:06 -04001013
1014 /* only the initial fragment is time stamped */
Herbert Xu1470ddf2011-03-01 02:36:47 +00001015 skb_shinfo(skb)->tx_flags = cork->tx_flags;
Willem de Bruijn11878b42014-07-14 17:55:06 -04001016 cork->tx_flags = 0;
Willem de Bruijn09c2d252014-08-04 22:11:47 -04001017 skb_shinfo(skb)->tskey = tskey;
1018 tskey = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 /*
1021 * Find where to start putting bytes.
1022 */
Steffen Klassert353e5c92011-06-22 01:05:37 +00001023 data = skb_put(skb, fraglen + exthdrlen);
Arnaldo Carvalho de Meloc14d2452007-03-11 22:39:41 -03001024 skb_set_network_header(skb, exthdrlen);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001025 skb->transport_header = (skb->network_header +
1026 fragheaderlen);
Steffen Klassert353e5c92011-06-22 01:05:37 +00001027 data += fragheaderlen + exthdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 if (fraggap) {
1030 skb->csum = skb_copy_and_csum_bits(
1031 skb_prev, maxfraglen,
1032 data + transhdrlen, fraggap, 0);
1033 skb_prev->csum = csum_sub(skb_prev->csum,
1034 skb->csum);
1035 data += fraggap;
Herbert Xue9fa4f72006-08-13 20:12:58 -07001036 pskb_trim_unique(skb_prev, maxfraglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 }
1038
1039 copy = datalen - transhdrlen - fraggap;
1040 if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
1041 err = -EFAULT;
1042 kfree_skb(skb);
1043 goto error;
1044 }
1045
1046 offset += copy;
1047 length -= datalen - fraggap;
1048 transhdrlen = 0;
1049 exthdrlen = 0;
1050 csummode = CHECKSUM_NONE;
1051
1052 /*
1053 * Put the packet on the pending queue.
1054 */
Herbert Xu1470ddf2011-03-01 02:36:47 +00001055 __skb_queue_tail(queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 continue;
1057 }
1058
1059 if (copy > length)
1060 copy = length;
1061
Changli Gaod8d1f302010-06-10 23:31:35 -07001062 if (!(rt->dst.dev->features&NETIF_F_SG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 unsigned int off;
1064
1065 off = skb->len;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001066 if (getfrag(from, skb_put(skb, copy),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 offset, copy, off, skb) < 0) {
1068 __skb_trim(skb, off);
1069 err = -EFAULT;
1070 goto error;
1071 }
1072 } else {
1073 int i = skb_shinfo(skb)->nr_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Eric Dumazet5640f762012-09-23 23:04:42 +00001075 err = -ENOMEM;
1076 if (!sk_page_frag_refill(sk, pfrag))
1077 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Eric Dumazet5640f762012-09-23 23:04:42 +00001079 if (!skb_can_coalesce(skb, i, pfrag->page,
1080 pfrag->offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 err = -EMSGSIZE;
Eric Dumazet5640f762012-09-23 23:04:42 +00001082 if (i == MAX_SKB_FRAGS)
1083 goto error;
1084
1085 __skb_fill_page_desc(skb, i, pfrag->page,
1086 pfrag->offset, 0);
1087 skb_shinfo(skb)->nr_frags = ++i;
1088 get_page(pfrag->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
Eric Dumazet5640f762012-09-23 23:04:42 +00001090 copy = min_t(int, copy, pfrag->size - pfrag->offset);
1091 if (getfrag(from,
1092 page_address(pfrag->page) + pfrag->offset,
1093 offset, copy, skb->len, skb) < 0)
1094 goto error_efault;
1095
1096 pfrag->offset += copy;
1097 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 skb->len += copy;
1099 skb->data_len += copy;
Herbert Xuf945fa72008-01-22 22:39:26 -08001100 skb->truesize += copy;
1101 atomic_add(copy, &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
1103 offset += copy;
1104 length -= copy;
1105 }
1106
1107 return 0;
1108
Eric Dumazet5640f762012-09-23 23:04:42 +00001109error_efault:
1110 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111error:
Herbert Xu1470ddf2011-03-01 02:36:47 +00001112 cork->length -= length;
Pavel Emelyanov5e38e272008-07-16 20:19:49 -07001113 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001114 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
Herbert Xu1470ddf2011-03-01 02:36:47 +00001117static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
1118 struct ipcm_cookie *ipc, struct rtable **rtp)
1119{
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001120 struct ip_options_rcu *opt;
Herbert Xu1470ddf2011-03-01 02:36:47 +00001121 struct rtable *rt;
1122
1123 /*
1124 * setup for corking.
1125 */
1126 opt = ipc->opt;
1127 if (opt) {
Ian Morris51456b22015-04-03 09:17:26 +01001128 if (!cork->opt) {
Herbert Xu1470ddf2011-03-01 02:36:47 +00001129 cork->opt = kmalloc(sizeof(struct ip_options) + 40,
1130 sk->sk_allocation);
Ian Morris51456b22015-04-03 09:17:26 +01001131 if (unlikely(!cork->opt))
Herbert Xu1470ddf2011-03-01 02:36:47 +00001132 return -ENOBUFS;
1133 }
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001134 memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen);
Herbert Xu1470ddf2011-03-01 02:36:47 +00001135 cork->flags |= IPCORK_OPT;
1136 cork->addr = ipc->addr;
1137 }
1138 rt = *rtp;
1139 if (unlikely(!rt))
1140 return -EFAULT;
1141 /*
1142 * We steal reference to this route, caller should not release it
1143 */
1144 *rtp = NULL;
Hannes Frederic Sowa482fc602013-11-05 02:24:17 +01001145 cork->fragsize = ip_sk_use_pmtu(sk) ?
1146 dst_mtu(&rt->dst) : rt->dst.dev->mtu;
Herbert Xu1470ddf2011-03-01 02:36:47 +00001147 cork->dst = &rt->dst;
1148 cork->length = 0;
Francesco Fuscoaa661582013-09-24 15:43:09 +02001149 cork->ttl = ipc->ttl;
1150 cork->tos = ipc->tos;
1151 cork->priority = ipc->priority;
Herbert Xu1470ddf2011-03-01 02:36:47 +00001152 cork->tx_flags = ipc->tx_flags;
Herbert Xu1470ddf2011-03-01 02:36:47 +00001153
1154 return 0;
1155}
1156
1157/*
1158 * ip_append_data() and ip_append_page() can make one large IP datagram
1159 * from many pieces of data. Each pieces will be holded on the socket
1160 * until ip_push_pending_frames() is called. Each piece can be a page
1161 * or non-page data.
1162 *
1163 * Not only UDP, other transport protocols - e.g. raw sockets - can use
1164 * this interface potentially.
1165 *
1166 * LATER: length must be adjusted by pad at tail, when it is required.
1167 */
David S. Millerf5fca602011-05-08 17:24:10 -07001168int ip_append_data(struct sock *sk, struct flowi4 *fl4,
Herbert Xu1470ddf2011-03-01 02:36:47 +00001169 int getfrag(void *from, char *to, int offset, int len,
1170 int odd, struct sk_buff *skb),
1171 void *from, int length, int transhdrlen,
1172 struct ipcm_cookie *ipc, struct rtable **rtp,
1173 unsigned int flags)
1174{
1175 struct inet_sock *inet = inet_sk(sk);
1176 int err;
1177
1178 if (flags&MSG_PROBE)
1179 return 0;
1180
1181 if (skb_queue_empty(&sk->sk_write_queue)) {
David S. Millerbdc712b2011-05-06 15:02:07 -07001182 err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp);
Herbert Xu1470ddf2011-03-01 02:36:47 +00001183 if (err)
1184 return err;
1185 } else {
1186 transhdrlen = 0;
1187 }
1188
Eric Dumazet5640f762012-09-23 23:04:42 +00001189 return __ip_append_data(sk, fl4, &sk->sk_write_queue, &inet->cork.base,
1190 sk_page_frag(sk), getfrag,
Herbert Xu1470ddf2011-03-01 02:36:47 +00001191 from, length, transhdrlen, flags);
1192}
1193
David S. Millerf5fca602011-05-08 17:24:10 -07001194ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 int offset, size_t size, int flags)
1196{
1197 struct inet_sock *inet = inet_sk(sk);
1198 struct sk_buff *skb;
1199 struct rtable *rt;
1200 struct ip_options *opt = NULL;
David S. Millerbdc712b2011-05-06 15:02:07 -07001201 struct inet_cork *cork;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 int hh_len;
1203 int mtu;
1204 int len;
1205 int err;
Hannes Frederic Sowadaba2872013-10-27 17:29:11 +01001206 unsigned int maxfraglen, fragheaderlen, fraggap, maxnonfragsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 if (inet->hdrincl)
1209 return -EPERM;
1210
1211 if (flags&MSG_PROBE)
1212 return 0;
1213
1214 if (skb_queue_empty(&sk->sk_write_queue))
1215 return -EINVAL;
1216
David S. Millerbdc712b2011-05-06 15:02:07 -07001217 cork = &inet->cork.base;
1218 rt = (struct rtable *)cork->dst;
1219 if (cork->flags & IPCORK_OPT)
1220 opt = cork->opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Changli Gaod8d1f302010-06-10 23:31:35 -07001222 if (!(rt->dst.dev->features&NETIF_F_SG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return -EOPNOTSUPP;
1224
Changli Gaod8d1f302010-06-10 23:31:35 -07001225 hh_len = LL_RESERVED_SPACE(rt->dst.dev);
David S. Millerbdc712b2011-05-06 15:02:07 -07001226 mtu = cork->fragsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
1229 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
WANG Cong60ff7462014-05-04 16:39:18 -07001230 maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Hannes Frederic Sowadaba2872013-10-27 17:29:11 +01001232 if (cork->length + size > maxnonfragsize - fragheaderlen) {
Hannes Frederic Sowa61e7f092013-12-19 02:13:36 +01001233 ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
1234 mtu - (opt ? opt->optlen : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return -EMSGSIZE;
1236 }
1237
Ian Morris51456b22015-04-03 09:17:26 +01001238 skb = skb_peek_tail(&sk->sk_write_queue);
1239 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return -EINVAL;
1241
David S. Millerbdc712b2011-05-06 15:02:07 -07001242 cork->length += size;
Herbert Xu26cde9f2010-06-15 01:52:25 +00001243 if ((size + skb->len > mtu) &&
1244 (sk->sk_protocol == IPPROTO_UDP) &&
Changli Gaod8d1f302010-06-10 23:31:35 -07001245 (rt->dst.dev->features & NETIF_F_UFO)) {
Herbert Xu79671682006-06-22 02:40:14 -07001246 skb_shinfo(skb)->gso_size = mtu - fragheaderlen;
Herbert Xuf83ef8c2006-06-30 13:37:03 -07001247 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
Herbert Xu79671682006-06-22 02:40:14 -07001248 }
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 while (size > 0) {
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02001251 if (skb_is_gso(skb)) {
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001252 len = size;
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02001253 } else {
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001254
1255 /* Check if the remaining data fits into current packet. */
1256 len = mtu - skb->len;
1257 if (len < size)
1258 len = maxfraglen - skb->len;
1259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (len <= 0) {
1261 struct sk_buff *skb_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int alloclen;
1263
1264 skb_prev = skb;
Jayachandran C0d0d2bb2005-10-13 11:43:02 -07001265 fraggap = skb_prev->len - maxfraglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 alloclen = fragheaderlen + hh_len + fraggap + 15;
1268 skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation);
1269 if (unlikely(!skb)) {
1270 err = -ENOBUFS;
1271 goto error;
1272 }
1273
1274 /*
1275 * Fill in the control structures
1276 */
1277 skb->ip_summed = CHECKSUM_NONE;
1278 skb->csum = 0;
1279 skb_reserve(skb, hh_len);
1280
1281 /*
1282 * Find where to start putting bytes.
1283 */
Arnaldo Carvalho de Melo967b05f2007-03-13 13:51:52 -03001284 skb_put(skb, fragheaderlen + fraggap);
Arnaldo Carvalho de Melo2ca9e6f2007-03-10 19:15:25 -03001285 skb_reset_network_header(skb);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001286 skb->transport_header = (skb->network_header +
1287 fragheaderlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (fraggap) {
Arnaldo Carvalho de Melo967b05f2007-03-13 13:51:52 -03001289 skb->csum = skb_copy_and_csum_bits(skb_prev,
1290 maxfraglen,
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001291 skb_transport_header(skb),
Arnaldo Carvalho de Melo967b05f2007-03-13 13:51:52 -03001292 fraggap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 skb_prev->csum = csum_sub(skb_prev->csum,
1294 skb->csum);
Herbert Xue9fa4f72006-08-13 20:12:58 -07001295 pskb_trim_unique(skb_prev, maxfraglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297
1298 /*
1299 * Put the packet on the pending queue.
1300 */
1301 __skb_queue_tail(&sk->sk_write_queue, skb);
1302 continue;
1303 }
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (len > size)
1306 len = size;
Hannes Frederic Sowabe12a1f2015-05-21 16:59:58 +02001307
1308 if (skb_append_pagefrags(skb, page, offset, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 err = -EMSGSIZE;
1310 goto error;
1311 }
1312
1313 if (skb->ip_summed == CHECKSUM_NONE) {
Al Viro44bb9362006-11-14 21:36:14 -08001314 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 csum = csum_page(page, offset, len);
1316 skb->csum = csum_block_add(skb->csum, csum, skb->len);
1317 }
1318
1319 skb->len += len;
1320 skb->data_len += len;
David S. Miller1e34a112008-01-22 23:44:31 -08001321 skb->truesize += len;
1322 atomic_add(len, &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 offset += len;
1324 size -= len;
1325 }
1326 return 0;
1327
1328error:
David S. Millerbdc712b2011-05-06 15:02:07 -07001329 cork->length -= size;
Pavel Emelyanov5e38e272008-07-16 20:19:49 -07001330 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return err;
1332}
1333
Herbert Xu1470ddf2011-03-01 02:36:47 +00001334static void ip_cork_release(struct inet_cork *cork)
Pavel Emelyanov429f08e2007-11-05 21:03:24 -08001335{
Herbert Xu1470ddf2011-03-01 02:36:47 +00001336 cork->flags &= ~IPCORK_OPT;
1337 kfree(cork->opt);
1338 cork->opt = NULL;
1339 dst_release(cork->dst);
1340 cork->dst = NULL;
Pavel Emelyanov429f08e2007-11-05 21:03:24 -08001341}
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343/*
1344 * Combined all pending IP fragments on the socket as one IP datagram
1345 * and push them out.
1346 */
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001347struct sk_buff *__ip_make_skb(struct sock *sk,
David S. Miller77968b72011-05-08 17:12:19 -07001348 struct flowi4 *fl4,
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001349 struct sk_buff_head *queue,
1350 struct inet_cork *cork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 struct sk_buff *skb, *tmp_skb;
1353 struct sk_buff **tail_skb;
1354 struct inet_sock *inet = inet_sk(sk);
Pavel Emelyanov0388b002008-07-14 23:00:43 -07001355 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 struct ip_options *opt = NULL;
Herbert Xu1470ddf2011-03-01 02:36:47 +00001357 struct rtable *rt = (struct rtable *)cork->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 struct iphdr *iph;
Alexey Dobriyan76ab6082006-01-06 13:24:29 -08001359 __be16 df = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 __u8 ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Ian Morris51456b22015-04-03 09:17:26 +01001362 skb = __skb_dequeue(queue);
1363 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 goto out;
1365 tail_skb = &(skb_shinfo(skb)->frag_list);
1366
1367 /* move skb->data to ip header from ext header */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001368 if (skb->data < skb_network_header(skb))
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001369 __skb_pull(skb, skb_network_offset(skb));
Herbert Xu1470ddf2011-03-01 02:36:47 +00001370 while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001371 __skb_pull(tmp_skb, skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 *tail_skb = tmp_skb;
1373 tail_skb = &(tmp_skb->next);
1374 skb->len += tmp_skb->len;
1375 skb->data_len += tmp_skb->len;
1376 skb->truesize += tmp_skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 tmp_skb->destructor = NULL;
1378 tmp_skb->sk = NULL;
1379 }
1380
1381 /* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow
1382 * to fragment the frame generated here. No matter, what transforms
1383 * how transforms change size of the packet, it will come out.
1384 */
WANG Cong60ff7462014-05-04 16:39:18 -07001385 skb->ignore_df = ip_sk_ignore_df(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 /* DF bit is set when we want to see DF on outgoing frames.
WANG Cong60ff7462014-05-04 16:39:18 -07001388 * If ignore_df is set too, we still allow to fragment this frame
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 * locally. */
Hannes Frederic Sowa482fc602013-11-05 02:24:17 +01001390 if (inet->pmtudisc == IP_PMTUDISC_DO ||
1391 inet->pmtudisc == IP_PMTUDISC_PROBE ||
Changli Gaod8d1f302010-06-10 23:31:35 -07001392 (skb->len <= dst_mtu(&rt->dst) &&
1393 ip_dont_fragment(sk, &rt->dst)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 df = htons(IP_DF);
1395
Herbert Xu1470ddf2011-03-01 02:36:47 +00001396 if (cork->flags & IPCORK_OPT)
1397 opt = cork->opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Francesco Fuscoaa661582013-09-24 15:43:09 +02001399 if (cork->ttl != 0)
1400 ttl = cork->ttl;
1401 else if (rt->rt_type == RTN_MULTICAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 ttl = inet->mc_ttl;
1403 else
Changli Gaod8d1f302010-06-10 23:31:35 -07001404 ttl = ip_select_ttl(inet, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Ansis Atteka749154a2013-09-18 15:29:52 -07001406 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 iph->version = 4;
1408 iph->ihl = 5;
Francesco Fuscoaa661582013-09-24 15:43:09 +02001409 iph->tos = (cork->tos != -1) ? cork->tos : inet->tos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 iph->frag_off = df;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 iph->ttl = ttl;
1412 iph->protocol = sk->sk_protocol;
Eric Dumazet84f93072011-11-30 19:00:53 +00001413 ip_copy_addrs(iph, fl4);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001414 ip_select_ident(net, skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
David S. Miller22f728f2011-05-13 17:21:27 -04001416 if (opt) {
1417 iph->ihl += opt->optlen>>2;
1418 ip_options_build(skb, opt, cork->addr, rt, 0);
1419 }
1420
Francesco Fuscoaa661582013-09-24 15:43:09 +02001421 skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -08001422 skb->mark = sk->sk_mark;
Eric Dumazeta21bba92008-11-24 16:07:50 -08001423 /*
1424 * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec
1425 * on dst refcount
1426 */
Herbert Xu1470ddf2011-03-01 02:36:47 +00001427 cork->dst = NULL;
Changli Gaod8d1f302010-06-10 23:31:35 -07001428 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
David L Stevens96793b42007-09-17 09:57:33 -07001430 if (iph->protocol == IPPROTO_ICMP)
Pavel Emelyanov0388b002008-07-14 23:00:43 -07001431 icmp_out_count(net, ((struct icmphdr *)
David L Stevens96793b42007-09-17 09:57:33 -07001432 skb_transport_header(skb))->type);
1433
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001434 ip_cork_release(cork);
1435out:
1436 return skb;
1437}
1438
Eric Dumazetb5ec8ee2012-08-10 02:22:47 +00001439int ip_send_skb(struct net *net, struct sk_buff *skb)
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001440{
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001441 int err;
1442
Herbert Xuc439cb22008-01-11 19:14:00 -08001443 err = ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (err) {
1445 if (err > 0)
Eric Dumazet6ce9e7b2009-09-02 18:05:33 -07001446 err = net_xmit_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (err)
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001448 IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
David S. Miller77968b72011-05-08 17:12:19 -07001454int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4)
Herbert Xu1470ddf2011-03-01 02:36:47 +00001455{
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001456 struct sk_buff *skb;
1457
David S. Miller77968b72011-05-08 17:12:19 -07001458 skb = ip_finish_skb(sk, fl4);
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001459 if (!skb)
1460 return 0;
1461
1462 /* Netfilter gets whole the not fragmented skb. */
Eric Dumazetb5ec8ee2012-08-10 02:22:47 +00001463 return ip_send_skb(sock_net(sk), skb);
Herbert Xu1470ddf2011-03-01 02:36:47 +00001464}
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466/*
1467 * Throw away all pending data on the socket.
1468 */
Herbert Xu1470ddf2011-03-01 02:36:47 +00001469static void __ip_flush_pending_frames(struct sock *sk,
1470 struct sk_buff_head *queue,
1471 struct inet_cork *cork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 struct sk_buff *skb;
1474
Herbert Xu1470ddf2011-03-01 02:36:47 +00001475 while ((skb = __skb_dequeue_tail(queue)) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 kfree_skb(skb);
1477
Herbert Xu1470ddf2011-03-01 02:36:47 +00001478 ip_cork_release(cork);
1479}
1480
1481void ip_flush_pending_frames(struct sock *sk)
1482{
David S. Millerbdc712b2011-05-06 15:02:07 -07001483 __ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001486struct sk_buff *ip_make_skb(struct sock *sk,
David S. Miller77968b72011-05-08 17:12:19 -07001487 struct flowi4 *fl4,
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001488 int getfrag(void *from, char *to, int offset,
1489 int len, int odd, struct sk_buff *skb),
1490 void *from, int length, int transhdrlen,
1491 struct ipcm_cookie *ipc, struct rtable **rtp,
1492 unsigned int flags)
1493{
David S. Millerb80d7222011-05-06 15:06:01 -07001494 struct inet_cork cork;
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001495 struct sk_buff_head queue;
1496 int err;
1497
1498 if (flags & MSG_PROBE)
1499 return NULL;
1500
1501 __skb_queue_head_init(&queue);
1502
David S. Millerb80d7222011-05-06 15:06:01 -07001503 cork.flags = 0;
1504 cork.addr = 0;
David S. Miller70652722011-05-06 16:01:15 -07001505 cork.opt = NULL;
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001506 err = ip_setup_cork(sk, &cork, ipc, rtp);
1507 if (err)
1508 return ERR_PTR(err);
1509
Eric Dumazet5640f762012-09-23 23:04:42 +00001510 err = __ip_append_data(sk, fl4, &queue, &cork,
1511 &current->task_frag, getfrag,
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001512 from, length, transhdrlen, flags);
1513 if (err) {
1514 __ip_flush_pending_frames(sk, &queue, &cork);
1515 return ERR_PTR(err);
1516 }
1517
David S. Miller77968b72011-05-08 17:12:19 -07001518 return __ip_make_skb(sk, fl4, &queue, &cork);
Herbert Xu1c32c5a2011-03-01 02:36:47 +00001519}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521/*
1522 * Fetch data from kernel space and fill in checksum if needed.
1523 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001524static int ip_reply_glue_bits(void *dptr, char *to, int offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 int len, int odd, struct sk_buff *skb)
1526{
Al Viro50842052006-11-14 21:36:34 -08001527 __wsum csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 csum = csum_partial_copy_nocheck(dptr+offset, to, len, 0);
1530 skb->csum = csum_block_add(skb->csum, csum, odd);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001531 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001534/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 * Generic function to send a packet as reply to another packet.
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001536 * Used to send some TCP resets/acks so far.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 */
Eric Dumazetbdbbb852015-01-29 21:35:05 -08001538void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,
Eric Dumazet24a2d432014-09-27 09:50:55 -07001539 const struct ip_options *sopt,
1540 __be32 daddr, __be32 saddr,
1541 const struct ip_reply_arg *arg,
David S. Miller70e73412012-06-28 03:21:41 -07001542 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001544 struct ip_options_data replyopts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 struct ipcm_cookie ipc;
David S. Miller77968b72011-05-08 17:12:19 -07001546 struct flowi4 fl4;
Eric Dumazet511c3f92009-06-02 05:14:27 +00001547 struct rtable *rt = skb_rtable(skb);
Eric Dumazetbdbbb852015-01-29 21:35:05 -08001548 struct net *net = sock_net(sk);
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001549 struct sk_buff *nskb;
Vasily Averin40620902014-10-15 16:24:02 +04001550 int err;
David Ahernf7ba8682015-08-13 14:59:08 -06001551 int oif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Eric Dumazet24a2d432014-09-27 09:50:55 -07001553 if (__ip_options_echo(&replyopts.opt.opt, skb, sopt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return;
1555
David S. Miller0a5ebb82011-05-09 13:22:43 -07001556 ipc.addr = daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 ipc.opt = NULL;
Oliver Hartkopp2244d072010-08-17 08:59:14 +00001558 ipc.tx_flags = 0;
Francesco Fuscoaa661582013-09-24 15:43:09 +02001559 ipc.ttl = 0;
1560 ipc.tos = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001562 if (replyopts.opt.opt.optlen) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 ipc.opt = &replyopts.opt;
1564
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001565 if (replyopts.opt.opt.srr)
1566 daddr = replyopts.opt.opt.faddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568
David Ahernf7ba8682015-08-13 14:59:08 -06001569 oif = arg->bound_dev_if;
David Ahern007979e2015-09-29 20:07:10 -07001570 if (!oif && netif_index_is_l3_master(net, skb->skb_iif))
David Ahernf7ba8682015-08-13 14:59:08 -06001571 oif = skb->skb_iif;
1572
1573 flowi4_init_output(&fl4, oif,
Lorenzo Colittie1108612014-05-13 10:17:33 -07001574 IP4_REPLY_MARK(net, skb->mark),
Eric Dumazet66b13d92011-10-24 03:06:21 -04001575 RT_TOS(arg->tos),
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001576 RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol,
David S. Miller77968b72011-05-08 17:12:19 -07001577 ip_reply_arg_flowi_flags(arg),
David S. Miller70e73412012-06-28 03:21:41 -07001578 daddr, saddr,
David S. Miller77968b72011-05-08 17:12:19 -07001579 tcp_hdr(skb)->source, tcp_hdr(skb)->dest);
1580 security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001581 rt = ip_route_output_key(net, &fl4);
David S. Miller77968b72011-05-08 17:12:19 -07001582 if (IS_ERR(rt))
1583 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Eric Dumazetbdbbb852015-01-29 21:35:05 -08001585 inet_sk(sk)->tos = arg->tos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 sk->sk_priority = skb->priority;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001588 sk->sk_protocol = ip_hdr(skb)->protocol;
Patrick McHardyf0e48db2007-06-04 21:32:46 -07001589 sk->sk_bound_dev_if = arg->bound_dev_if;
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001590 sk->sk_sndbuf = sysctl_wmem_default;
Vasily Averin40620902014-10-15 16:24:02 +04001591 err = ip_append_data(sk, &fl4, ip_reply_glue_bits, arg->iov->iov_base,
1592 len, 0, &ipc, &rt, MSG_DONTWAIT);
1593 if (unlikely(err)) {
1594 ip_flush_pending_frames(sk);
1595 goto out;
1596 }
1597
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001598 nskb = skb_peek(&sk->sk_write_queue);
1599 if (nskb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (arg->csumoffset >= 0)
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001601 *((__sum16 *)skb_transport_header(nskb) +
1602 arg->csumoffset) = csum_fold(csum_add(nskb->csum,
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001603 arg->csum));
Eric Dumazetbe9f4a42012-07-19 07:34:03 +00001604 nskb->ip_summed = CHECKSUM_NONE;
1605 skb_set_queue_mapping(nskb, skb_get_queue_mapping(skb));
David S. Miller77968b72011-05-08 17:12:19 -07001606 ip_push_pending_frames(sk, &fl4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
Vasily Averin40620902014-10-15 16:24:02 +04001608out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 ip_rt_put(rt);
1610}
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612void __init ip_init(void)
1613{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 ip_rt_init();
1615 inet_initpeers();
1616
WANG Cong72c1d3b2014-01-10 16:09:45 -08001617#if defined(CONFIG_IP_MULTICAST)
1618 igmp_mc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619#endif
1620}