GNU Linux-libre 4.14.332-gnu1
[releases.git] / net / ipv4 / ip_output.c
1 /*
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  *
8  * Authors:     Ross Biro
9  *              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.
23  *              Bradford Johnson:       Fix faulty handling of some frames when
24  *                                      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.
34  *              Andi Kleen      :       Split fast and slow ip_build_xmit path
35  *                                      for decreased register pressure on x86
36  *                                      and more readibility.
37  *              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 <linux/uaccess.h>
46 #include <linux/module.h>
47 #include <linux/types.h>
48 #include <linux/kernel.h>
49 #include <linux/mm.h>
50 #include <linux/string.h>
51 #include <linux/errno.h>
52 #include <linux/highmem.h>
53 #include <linux/slab.h>
54
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>
69 #include <net/xfrm.h>
70 #include <linux/skbuff.h>
71 #include <net/sock.h>
72 #include <net/arp.h>
73 #include <net/icmp.h>
74 #include <net/checksum.h>
75 #include <net/inetpeer.h>
76 #include <net/inet_ecn.h>
77 #include <net/lwtunnel.h>
78 #include <linux/bpf-cgroup.h>
79 #include <linux/igmp.h>
80 #include <linux/netfilter_ipv4.h>
81 #include <linux/netfilter_bridge.h>
82 #include <linux/netlink.h>
83 #include <linux/tcp.h>
84
85 static int
86 ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
87             unsigned int mtu,
88             int (*output)(struct net *, struct sock *, struct sk_buff *));
89
90 /* Generate a checksum for an outgoing IP datagram. */
91 void ip_send_check(struct iphdr *iph)
92 {
93         iph->check = 0;
94         iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
95 }
96 EXPORT_SYMBOL(ip_send_check);
97
98 int __ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
99 {
100         struct iphdr *iph = ip_hdr(skb);
101
102         iph->tot_len = htons(skb->len);
103         ip_send_check(iph);
104
105         /* if egress device is enslaved to an L3 master device pass the
106          * skb to its handler for processing
107          */
108         skb = l3mdev_ip_out(sk, skb);
109         if (unlikely(!skb))
110                 return 0;
111
112         skb->protocol = htons(ETH_P_IP);
113
114         return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT,
115                        net, sk, skb, NULL, skb_dst(skb)->dev,
116                        dst_output);
117 }
118
119 int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
120 {
121         int err;
122
123         err = __ip_local_out(net, sk, skb);
124         if (likely(err == 1))
125                 err = dst_output(net, sk, skb);
126
127         return err;
128 }
129 EXPORT_SYMBOL_GPL(ip_local_out);
130
131 static inline int ip_select_ttl(struct inet_sock *inet, struct dst_entry *dst)
132 {
133         int ttl = inet->uc_ttl;
134
135         if (ttl < 0)
136                 ttl = ip4_dst_hoplimit(dst);
137         return ttl;
138 }
139
140 /*
141  *              Add an ip header to a skbuff and send it out.
142  *
143  */
144 int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk,
145                           __be32 saddr, __be32 daddr, struct ip_options_rcu *opt)
146 {
147         struct inet_sock *inet = inet_sk(sk);
148         struct rtable *rt = skb_rtable(skb);
149         struct net *net = sock_net(sk);
150         struct iphdr *iph;
151
152         /* Build the IP header. */
153         skb_push(skb, sizeof(struct iphdr) + (opt ? opt->opt.optlen : 0));
154         skb_reset_network_header(skb);
155         iph = ip_hdr(skb);
156         iph->version  = 4;
157         iph->ihl      = 5;
158         iph->tos      = inet->tos;
159         iph->ttl      = ip_select_ttl(inet, &rt->dst);
160         iph->daddr    = (opt && opt->opt.srr ? opt->opt.faddr : daddr);
161         iph->saddr    = saddr;
162         iph->protocol = sk->sk_protocol;
163         /* Do not bother generating IPID for small packets (eg SYNACK) */
164         if (skb->len <= IPV4_MIN_MTU || ip_dont_fragment(sk, &rt->dst)) {
165                 iph->frag_off = htons(IP_DF);
166                 iph->id = 0;
167         } else {
168                 iph->frag_off = 0;
169                 /* TCP packets here are SYNACK with fat IPv4/TCP options.
170                  * Avoid using the hashed IP ident generator.
171                  */
172                 if (sk->sk_protocol == IPPROTO_TCP)
173                         iph->id = (__force __be16)prandom_u32();
174                 else
175                         __ip_select_ident(net, iph, 1);
176         }
177
178         if (opt && opt->opt.optlen) {
179                 iph->ihl += opt->opt.optlen>>2;
180                 ip_options_build(skb, &opt->opt, daddr, rt, 0);
181         }
182
183         skb->priority = sk->sk_priority;
184         if (!skb->mark)
185                 skb->mark = sk->sk_mark;
186
187         /* Send it out. */
188         return ip_local_out(net, skb->sk, skb);
189 }
190 EXPORT_SYMBOL_GPL(ip_build_and_send_pkt);
191
192 static int ip_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
193 {
194         struct dst_entry *dst = skb_dst(skb);
195         struct rtable *rt = (struct rtable *)dst;
196         struct net_device *dev = dst->dev;
197         unsigned int hh_len = LL_RESERVED_SPACE(dev);
198         struct neighbour *neigh;
199         u32 nexthop;
200
201         if (rt->rt_type == RTN_MULTICAST) {
202                 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTMCAST, skb->len);
203         } else if (rt->rt_type == RTN_BROADCAST)
204                 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUTBCAST, skb->len);
205
206         /* Be paranoid, rather than too clever. */
207         if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
208                 struct sk_buff *skb2;
209
210                 skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
211                 if (!skb2) {
212                         kfree_skb(skb);
213                         return -ENOMEM;
214                 }
215                 if (skb->sk)
216                         skb_set_owner_w(skb2, skb->sk);
217                 consume_skb(skb);
218                 skb = skb2;
219         }
220
221         if (lwtunnel_xmit_redirect(dst->lwtstate)) {
222                 int res = lwtunnel_xmit(skb);
223
224                 if (res != LWTUNNEL_XMIT_CONTINUE)
225                         return res;
226         }
227
228         rcu_read_lock_bh();
229         nexthop = (__force u32) rt_nexthop(rt, ip_hdr(skb)->daddr);
230         neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
231         if (unlikely(!neigh))
232                 neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
233         if (!IS_ERR(neigh)) {
234                 int res;
235
236                 sock_confirm_neigh(skb, neigh);
237                 res = neigh_output(neigh, skb);
238
239                 rcu_read_unlock_bh();
240                 return res;
241         }
242         rcu_read_unlock_bh();
243
244         net_dbg_ratelimited("%s: No header cache and no neighbour!\n",
245                             __func__);
246         kfree_skb(skb);
247         return -EINVAL;
248 }
249
250 static int ip_finish_output_gso(struct net *net, struct sock *sk,
251                                 struct sk_buff *skb, unsigned int mtu)
252 {
253         netdev_features_t features;
254         struct sk_buff *segs;
255         int ret = 0;
256
257         /* common case: seglen is <= mtu
258          */
259         if (skb_gso_validate_mtu(skb, mtu))
260                 return ip_finish_output2(net, sk, skb);
261
262         /* Slowpath -  GSO segment length exceeds the egress MTU.
263          *
264          * This can happen in several cases:
265          *  - Forwarding of a TCP GRO skb, when DF flag is not set.
266          *  - Forwarding of an skb that arrived on a virtualization interface
267          *    (virtio-net/vhost/tap) with TSO/GSO size set by other network
268          *    stack.
269          *  - Local GSO skb transmitted on an NETIF_F_TSO tunnel stacked over an
270          *    interface with a smaller MTU.
271          *  - Arriving GRO skb (or GSO skb in a virtualized environment) that is
272          *    bridged to a NETIF_F_TSO tunnel stacked over an interface with an
273          *    insufficent MTU.
274          */
275         features = netif_skb_features(skb);
276         BUILD_BUG_ON(sizeof(*IPCB(skb)) > SKB_SGO_CB_OFFSET);
277         segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
278         if (IS_ERR_OR_NULL(segs)) {
279                 kfree_skb(skb);
280                 return -ENOMEM;
281         }
282
283         consume_skb(skb);
284
285         do {
286                 struct sk_buff *nskb = segs->next;
287                 int err;
288
289                 segs->next = NULL;
290                 err = ip_fragment(net, sk, segs, mtu, ip_finish_output2);
291
292                 if (err && ret == 0)
293                         ret = err;
294                 segs = nskb;
295         } while (segs);
296
297         return ret;
298 }
299
300 static int ip_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
301 {
302         unsigned int mtu;
303         int ret;
304
305         ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
306         if (ret) {
307                 kfree_skb(skb);
308                 return ret;
309         }
310
311 #if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
312         /* Policy lookup after SNAT yielded a new policy */
313         if (skb_dst(skb)->xfrm) {
314                 IPCB(skb)->flags |= IPSKB_REROUTED;
315                 return dst_output(net, sk, skb);
316         }
317 #endif
318         mtu = ip_skb_dst_mtu(sk, skb);
319         if (skb_is_gso(skb))
320                 return ip_finish_output_gso(net, sk, skb, mtu);
321
322         if (skb->len > mtu || IPCB(skb)->frag_max_size)
323                 return ip_fragment(net, sk, skb, mtu, ip_finish_output2);
324
325         return ip_finish_output2(net, sk, skb);
326 }
327
328 static int ip_mc_finish_output(struct net *net, struct sock *sk,
329                                struct sk_buff *skb)
330 {
331         int ret;
332
333         ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
334         if (ret) {
335                 kfree_skb(skb);
336                 return ret;
337         }
338
339         return dev_loopback_xmit(net, sk, skb);
340 }
341
342 int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb)
343 {
344         struct rtable *rt = skb_rtable(skb);
345         struct net_device *dev = rt->dst.dev;
346
347         /*
348          *      If the indicated interface is up and running, send the packet.
349          */
350         IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
351
352         skb->dev = dev;
353         skb->protocol = htons(ETH_P_IP);
354
355         /*
356          *      Multicasts are looped back for other local users
357          */
358
359         if (rt->rt_flags&RTCF_MULTICAST) {
360                 if (sk_mc_loop(sk)
361 #ifdef CONFIG_IP_MROUTE
362                 /* Small optimization: do not loopback not local frames,
363                    which returned after forwarding; they will be  dropped
364                    by ip_mr_input in any case.
365                    Note, that local frames are looped back to be delivered
366                    to local recipients.
367
368                    This check is duplicated in ip_mr_input at the moment.
369                  */
370                     &&
371                     ((rt->rt_flags & RTCF_LOCAL) ||
372                      !(IPCB(skb)->flags & IPSKB_FORWARDED))
373 #endif
374                    ) {
375                         struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
376                         if (newskb)
377                                 NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
378                                         net, sk, newskb, NULL, newskb->dev,
379                                         ip_mc_finish_output);
380                 }
381
382                 /* Multicasts with ttl 0 must not go beyond the host */
383
384                 if (ip_hdr(skb)->ttl == 0) {
385                         kfree_skb(skb);
386                         return 0;
387                 }
388         }
389
390         if (rt->rt_flags&RTCF_BROADCAST) {
391                 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
392                 if (newskb)
393                         NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
394                                 net, sk, newskb, NULL, newskb->dev,
395                                 ip_mc_finish_output);
396         }
397
398         return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
399                             net, sk, skb, NULL, skb->dev,
400                             ip_finish_output,
401                             !(IPCB(skb)->flags & IPSKB_REROUTED));
402 }
403
404 int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb)
405 {
406         struct net_device *dev = skb_dst(skb)->dev;
407
408         IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
409
410         skb->dev = dev;
411         skb->protocol = htons(ETH_P_IP);
412
413         return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
414                             net, sk, skb, NULL, dev,
415                             ip_finish_output,
416                             !(IPCB(skb)->flags & IPSKB_REROUTED));
417 }
418
419 /*
420  * copy saddr and daddr, possibly using 64bit load/stores
421  * Equivalent to :
422  *   iph->saddr = fl4->saddr;
423  *   iph->daddr = fl4->daddr;
424  */
425 static void ip_copy_addrs(struct iphdr *iph, const struct flowi4 *fl4)
426 {
427         BUILD_BUG_ON(offsetof(typeof(*fl4), daddr) !=
428                      offsetof(typeof(*fl4), saddr) + sizeof(fl4->saddr));
429
430         iph->saddr = fl4->saddr;
431         iph->daddr = fl4->daddr;
432 }
433
434 /* Note: skb->sk can be different from sk, in case of tunnels */
435 int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl)
436 {
437         struct inet_sock *inet = inet_sk(sk);
438         struct net *net = sock_net(sk);
439         struct ip_options_rcu *inet_opt;
440         struct flowi4 *fl4;
441         struct rtable *rt;
442         struct iphdr *iph;
443         int res;
444
445         /* Skip all of this if the packet is already routed,
446          * f.e. by something like SCTP.
447          */
448         rcu_read_lock();
449         inet_opt = rcu_dereference(inet->inet_opt);
450         fl4 = &fl->u.ip4;
451         rt = skb_rtable(skb);
452         if (rt)
453                 goto packet_routed;
454
455         /* Make sure we can route this packet. */
456         rt = (struct rtable *)__sk_dst_check(sk, 0);
457         if (!rt) {
458                 __be32 daddr;
459
460                 /* Use correct destination address if we have options. */
461                 daddr = inet->inet_daddr;
462                 if (inet_opt && inet_opt->opt.srr)
463                         daddr = inet_opt->opt.faddr;
464
465                 /* If this fails, retransmit mechanism of transport layer will
466                  * keep trying until route appears or the connection times
467                  * itself out.
468                  */
469                 rt = ip_route_output_ports(net, fl4, sk,
470                                            daddr, inet->inet_saddr,
471                                            inet->inet_dport,
472                                            inet->inet_sport,
473                                            sk->sk_protocol,
474                                            RT_CONN_FLAGS(sk),
475                                            sk->sk_bound_dev_if);
476                 if (IS_ERR(rt))
477                         goto no_route;
478                 sk_setup_caps(sk, &rt->dst);
479         }
480         skb_dst_set_noref(skb, &rt->dst);
481
482 packet_routed:
483         if (inet_opt && inet_opt->opt.is_strictroute && rt->rt_uses_gateway)
484                 goto no_route;
485
486         /* OK, we know where to send it, allocate and build IP header. */
487         skb_push(skb, sizeof(struct iphdr) + (inet_opt ? inet_opt->opt.optlen : 0));
488         skb_reset_network_header(skb);
489         iph = ip_hdr(skb);
490         *((__be16 *)iph) = htons((4 << 12) | (5 << 8) | (inet->tos & 0xff));
491         if (ip_dont_fragment(sk, &rt->dst) && !skb->ignore_df)
492                 iph->frag_off = htons(IP_DF);
493         else
494                 iph->frag_off = 0;
495         iph->ttl      = ip_select_ttl(inet, &rt->dst);
496         iph->protocol = sk->sk_protocol;
497         ip_copy_addrs(iph, fl4);
498
499         /* Transport layer set skb->h.foo itself. */
500
501         if (inet_opt && inet_opt->opt.optlen) {
502                 iph->ihl += inet_opt->opt.optlen >> 2;
503                 ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0);
504         }
505
506         ip_select_ident_segs(net, skb, sk,
507                              skb_shinfo(skb)->gso_segs ?: 1);
508
509         /* TODO : should we use skb->sk here instead of sk ? */
510         skb->priority = sk->sk_priority;
511         skb->mark = sk->sk_mark;
512
513         res = ip_local_out(net, sk, skb);
514         rcu_read_unlock();
515         return res;
516
517 no_route:
518         rcu_read_unlock();
519         IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
520         kfree_skb(skb);
521         return -EHOSTUNREACH;
522 }
523 EXPORT_SYMBOL(ip_queue_xmit);
524
525 static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from)
526 {
527         to->pkt_type = from->pkt_type;
528         to->priority = from->priority;
529         to->protocol = from->protocol;
530         to->skb_iif = from->skb_iif;
531         skb_dst_drop(to);
532         skb_dst_copy(to, from);
533         to->dev = from->dev;
534         to->mark = from->mark;
535
536         skb_copy_hash(to, from);
537
538         /* Copy the flags to each fragment. */
539         IPCB(to)->flags = IPCB(from)->flags;
540
541 #ifdef CONFIG_NET_SCHED
542         to->tc_index = from->tc_index;
543 #endif
544         nf_copy(to, from);
545 #if IS_ENABLED(CONFIG_IP_VS)
546         to->ipvs_property = from->ipvs_property;
547 #endif
548         skb_copy_secmark(to, from);
549 }
550
551 static int ip_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
552                        unsigned int mtu,
553                        int (*output)(struct net *, struct sock *, struct sk_buff *))
554 {
555         struct iphdr *iph = ip_hdr(skb);
556
557         if ((iph->frag_off & htons(IP_DF)) == 0)
558                 return ip_do_fragment(net, sk, skb, output);
559
560         if (unlikely(!skb->ignore_df ||
561                      (IPCB(skb)->frag_max_size &&
562                       IPCB(skb)->frag_max_size > mtu))) {
563                 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
564                 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
565                           htonl(mtu));
566                 kfree_skb(skb);
567                 return -EMSGSIZE;
568         }
569
570         return ip_do_fragment(net, sk, skb, output);
571 }
572
573 /*
574  *      This IP datagram is too large to be sent in one piece.  Break it up into
575  *      smaller pieces (each of size equal to IP header plus
576  *      a block of the data of the original IP data part) that will yet fit in a
577  *      single device frame, and queue such a frame for sending.
578  */
579
580 int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
581                    int (*output)(struct net *, struct sock *, struct sk_buff *))
582 {
583         struct iphdr *iph;
584         int ptr;
585         struct sk_buff *skb2;
586         unsigned int mtu, hlen, left, len, ll_rs;
587         int offset;
588         __be16 not_last_frag;
589         struct rtable *rt = skb_rtable(skb);
590         int err = 0;
591
592         /* for offloaded checksums cleanup checksum before fragmentation */
593         if (skb->ip_summed == CHECKSUM_PARTIAL &&
594             (err = skb_checksum_help(skb)))
595                 goto fail;
596
597         /*
598          *      Point into the IP datagram header.
599          */
600
601         iph = ip_hdr(skb);
602
603         mtu = ip_skb_dst_mtu(sk, skb);
604         if (IPCB(skb)->frag_max_size && IPCB(skb)->frag_max_size < mtu)
605                 mtu = IPCB(skb)->frag_max_size;
606
607         /*
608          *      Setup starting values.
609          */
610
611         hlen = iph->ihl * 4;
612         mtu = mtu - hlen;       /* Size of data space */
613         IPCB(skb)->flags |= IPSKB_FRAG_COMPLETE;
614         ll_rs = LL_RESERVED_SPACE(rt->dst.dev);
615
616         /* When frag_list is given, use it. First, check its validity:
617          * some transformers could create wrong frag_list or break existing
618          * one, it is not prohibited. In this case fall back to copying.
619          *
620          * LATER: this step can be merged to real generation of fragments,
621          * we can switch to copy when see the first bad fragment.
622          */
623         if (skb_has_frag_list(skb)) {
624                 struct sk_buff *frag, *frag2;
625                 unsigned int first_len = skb_pagelen(skb);
626
627                 if (first_len - hlen > mtu ||
628                     ((first_len - hlen) & 7) ||
629                     ip_is_fragment(iph) ||
630                     skb_cloned(skb) ||
631                     skb_headroom(skb) < ll_rs)
632                         goto slow_path;
633
634                 skb_walk_frags(skb, frag) {
635                         /* Correct geometry. */
636                         if (frag->len > mtu ||
637                             ((frag->len & 7) && frag->next) ||
638                             skb_headroom(frag) < hlen + ll_rs)
639                                 goto slow_path_clean;
640
641                         /* Partially cloned skb? */
642                         if (skb_shared(frag))
643                                 goto slow_path_clean;
644
645                         BUG_ON(frag->sk);
646                         if (skb->sk) {
647                                 frag->sk = skb->sk;
648                                 frag->destructor = sock_wfree;
649                         }
650                         skb->truesize -= frag->truesize;
651                 }
652
653                 /* Everything is OK. Generate! */
654
655                 err = 0;
656                 offset = 0;
657                 frag = skb_shinfo(skb)->frag_list;
658                 skb_frag_list_init(skb);
659                 skb->data_len = first_len - skb_headlen(skb);
660                 skb->len = first_len;
661                 iph->tot_len = htons(first_len);
662                 iph->frag_off = htons(IP_MF);
663                 ip_send_check(iph);
664
665                 for (;;) {
666                         /* Prepare header of the next frame,
667                          * before previous one went down. */
668                         if (frag) {
669                                 frag->ip_summed = CHECKSUM_NONE;
670                                 skb_reset_transport_header(frag);
671                                 __skb_push(frag, hlen);
672                                 skb_reset_network_header(frag);
673                                 memcpy(skb_network_header(frag), iph, hlen);
674                                 iph = ip_hdr(frag);
675                                 iph->tot_len = htons(frag->len);
676                                 ip_copy_metadata(frag, skb);
677                                 if (offset == 0)
678                                         ip_options_fragment(frag);
679                                 offset += skb->len - hlen;
680                                 iph->frag_off = htons(offset>>3);
681                                 if (frag->next)
682                                         iph->frag_off |= htons(IP_MF);
683                                 /* Ready, complete checksum */
684                                 ip_send_check(iph);
685                         }
686
687                         err = output(net, sk, skb);
688
689                         if (!err)
690                                 IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES);
691                         if (err || !frag)
692                                 break;
693
694                         skb = frag;
695                         frag = skb->next;
696                         skb->next = NULL;
697                 }
698
699                 if (err == 0) {
700                         IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS);
701                         return 0;
702                 }
703
704                 while (frag) {
705                         skb = frag->next;
706                         kfree_skb(frag);
707                         frag = skb;
708                 }
709                 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
710                 return err;
711
712 slow_path_clean:
713                 skb_walk_frags(skb, frag2) {
714                         if (frag2 == frag)
715                                 break;
716                         frag2->sk = NULL;
717                         frag2->destructor = NULL;
718                         skb->truesize += frag2->truesize;
719                 }
720         }
721
722 slow_path:
723         iph = ip_hdr(skb);
724
725         left = skb->len - hlen;         /* Space per frame */
726         ptr = hlen;             /* Where to start from */
727
728         /*
729          *      Fragment the datagram.
730          */
731
732         offset = (ntohs(iph->frag_off) & IP_OFFSET) << 3;
733         not_last_frag = iph->frag_off & htons(IP_MF);
734
735         /*
736          *      Keep copying data until we run out.
737          */
738
739         while (left > 0) {
740                 len = left;
741                 /* IF: it doesn't fit, use 'mtu' - the data space left */
742                 if (len > mtu)
743                         len = mtu;
744                 /* IF: we are not sending up to and including the packet end
745                    then align the next start on an eight byte boundary */
746                 if (len < left) {
747                         len &= ~7;
748                 }
749
750                 /* Allocate buffer */
751                 skb2 = alloc_skb(len + hlen + ll_rs, GFP_ATOMIC);
752                 if (!skb2) {
753                         err = -ENOMEM;
754                         goto fail;
755                 }
756
757                 /*
758                  *      Set up data on packet
759                  */
760
761                 ip_copy_metadata(skb2, skb);
762                 skb_reserve(skb2, ll_rs);
763                 skb_put(skb2, len + hlen);
764                 skb_reset_network_header(skb2);
765                 skb2->transport_header = skb2->network_header + hlen;
766
767                 /*
768                  *      Charge the memory for the fragment to any owner
769                  *      it might possess
770                  */
771
772                 if (skb->sk)
773                         skb_set_owner_w(skb2, skb->sk);
774
775                 /*
776                  *      Copy the packet header into the new buffer.
777                  */
778
779                 skb_copy_from_linear_data(skb, skb_network_header(skb2), hlen);
780
781                 /*
782                  *      Copy a block of the IP datagram.
783                  */
784                 if (skb_copy_bits(skb, ptr, skb_transport_header(skb2), len))
785                         BUG();
786                 left -= len;
787
788                 /*
789                  *      Fill in the new header fields.
790                  */
791                 iph = ip_hdr(skb2);
792                 iph->frag_off = htons((offset >> 3));
793
794                 if (IPCB(skb)->flags & IPSKB_FRAG_PMTU)
795                         iph->frag_off |= htons(IP_DF);
796
797                 /* ANK: dirty, but effective trick. Upgrade options only if
798                  * the segment to be fragmented was THE FIRST (otherwise,
799                  * options are already fixed) and make it ONCE
800                  * on the initial skb, so that all the following fragments
801                  * will inherit fixed options.
802                  */
803                 if (offset == 0)
804                         ip_options_fragment(skb);
805
806                 /*
807                  *      Added AC : If we are fragmenting a fragment that's not the
808                  *                 last fragment then keep MF on each bit
809                  */
810                 if (left > 0 || not_last_frag)
811                         iph->frag_off |= htons(IP_MF);
812                 ptr += len;
813                 offset += len;
814
815                 /*
816                  *      Put this fragment into the sending queue.
817                  */
818                 iph->tot_len = htons(len + hlen);
819
820                 ip_send_check(iph);
821
822                 err = output(net, sk, skb2);
823                 if (err)
824                         goto fail;
825
826                 IP_INC_STATS(net, IPSTATS_MIB_FRAGCREATES);
827         }
828         consume_skb(skb);
829         IP_INC_STATS(net, IPSTATS_MIB_FRAGOKS);
830         return err;
831
832 fail:
833         kfree_skb(skb);
834         IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
835         return err;
836 }
837 EXPORT_SYMBOL(ip_do_fragment);
838
839 int
840 ip_generic_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
841 {
842         struct msghdr *msg = from;
843
844         if (skb->ip_summed == CHECKSUM_PARTIAL) {
845                 if (!copy_from_iter_full(to, len, &msg->msg_iter))
846                         return -EFAULT;
847         } else {
848                 __wsum csum = 0;
849                 if (!csum_and_copy_from_iter_full(to, len, &csum, &msg->msg_iter))
850                         return -EFAULT;
851                 skb->csum = csum_block_add(skb->csum, csum, odd);
852         }
853         return 0;
854 }
855 EXPORT_SYMBOL(ip_generic_getfrag);
856
857 static inline __wsum
858 csum_page(struct page *page, int offset, int copy)
859 {
860         char *kaddr;
861         __wsum csum;
862         kaddr = kmap(page);
863         csum = csum_partial(kaddr + offset, copy, 0);
864         kunmap(page);
865         return csum;
866 }
867
868 static int __ip_append_data(struct sock *sk,
869                             struct flowi4 *fl4,
870                             struct sk_buff_head *queue,
871                             struct inet_cork *cork,
872                             struct page_frag *pfrag,
873                             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)
877 {
878         struct inet_sock *inet = inet_sk(sk);
879         struct sk_buff *skb;
880
881         struct ip_options *opt = cork->opt;
882         int hh_len;
883         int exthdrlen;
884         int mtu;
885         int copy;
886         int err;
887         int offset = 0;
888         unsigned int maxfraglen, fragheaderlen, maxnonfragsize;
889         int csummode = CHECKSUM_NONE;
890         struct rtable *rt = (struct rtable *)cork->dst;
891         u32 tskey = 0;
892
893         skb = skb_peek_tail(queue);
894
895         exthdrlen = !skb ? rt->dst.header_len : 0;
896         mtu = cork->fragsize;
897         if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP &&
898             sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)
899                 tskey = sk->sk_tskey++;
900
901         hh_len = LL_RESERVED_SPACE(rt->dst.dev);
902
903         fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
904         maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
905         maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
906
907         if (cork->length + length > maxnonfragsize - fragheaderlen) {
908                 ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
909                                mtu - (opt ? opt->optlen : 0));
910                 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 &&
919             rt->dst.dev->features & (NETIF_F_HW_CSUM | NETIF_F_IP_CSUM) &&
920             !(flags & MSG_MORE) &&
921             !exthdrlen)
922                 csummode = CHECKSUM_PARTIAL;
923
924         cork->length += length;
925
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
933         if (!skb)
934                 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;
948 alloc_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
964                         if ((flags & MSG_MORE) &&
965                             !(rt->dst.dev->features&NETIF_F_SG))
966                                 alloclen = mtu;
967                         else
968                                 alloclen = fraglen;
969
970                         alloclen += exthdrlen;
971
972                         /* 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                          */
977                         if (datalen == length + fraggap)
978                                 alloclen += rt->dst.trailer_len;
979
980                         if (transhdrlen) {
981                                 skb = sock_alloc_send_skb(sk,
982                                                 alloclen + hh_len + 15,
983                                                 (flags & MSG_DONTWAIT), &err);
984                         } else {
985                                 skb = NULL;
986                                 if (refcount_read(&sk->sk_wmem_alloc) <=
987                                     2 * sk->sk_sndbuf)
988                                         skb = sock_wmalloc(sk,
989                                                            alloclen + hh_len + 15, 1,
990                                                            sk->sk_allocation);
991                                 if (unlikely(!skb))
992                                         err = -ENOBUFS;
993                         }
994                         if (!skb)
995                                 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);
1003
1004                         /* only the initial fragment is time stamped */
1005                         skb_shinfo(skb)->tx_flags = cork->tx_flags;
1006                         cork->tx_flags = 0;
1007                         skb_shinfo(skb)->tskey = tskey;
1008                         tskey = 0;
1009
1010                         /*
1011                          *      Find where to start putting bytes.
1012                          */
1013                         data = skb_put(skb, fraglen + exthdrlen);
1014                         skb_set_network_header(skb, exthdrlen);
1015                         skb->transport_header = (skb->network_header +
1016                                                  fragheaderlen);
1017                         data += fragheaderlen + exthdrlen;
1018
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;
1026                                 pskb_trim_unique(skb_prev, maxfraglen);
1027                         }
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                         if ((flags & MSG_CONFIRM) && !skb_prev)
1043                                 skb_set_dst_pending_confirm(skb, 1);
1044
1045                         /*
1046                          * Put the packet on the pending queue.
1047                          */
1048                         __skb_queue_tail(queue, skb);
1049                         continue;
1050                 }
1051
1052                 if (copy > length)
1053                         copy = length;
1054
1055                 if (!(rt->dst.dev->features&NETIF_F_SG) &&
1056                     skb_tailroom(skb) >= copy) {
1057                         unsigned int off;
1058
1059                         off = skb->len;
1060                         if (getfrag(from, skb_put(skb, copy),
1061                                         offset, copy, off, skb) < 0) {
1062                                 __skb_trim(skb, off);
1063                                 err = -EFAULT;
1064                                 goto error;
1065                         }
1066                 } else {
1067                         int i = skb_shinfo(skb)->nr_frags;
1068
1069                         err = -ENOMEM;
1070                         if (!sk_page_frag_refill(sk, pfrag))
1071                                 goto error;
1072
1073                         if (!skb_can_coalesce(skb, i, pfrag->page,
1074                                               pfrag->offset)) {
1075                                 err = -EMSGSIZE;
1076                                 if (i == MAX_SKB_FRAGS)
1077                                         goto error;
1078
1079                                 __skb_fill_page_desc(skb, i, pfrag->page,
1080                                                      pfrag->offset, 0);
1081                                 skb_shinfo(skb)->nr_frags = ++i;
1082                                 get_page(pfrag->page);
1083                         }
1084                         copy = min_t(int, copy, pfrag->size - pfrag->offset);
1085                         if (getfrag(from,
1086                                     page_address(pfrag->page) + pfrag->offset,
1087                                     offset, copy, skb->len, skb) < 0)
1088                                 goto error_efault;
1089
1090                         pfrag->offset += copy;
1091                         skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
1092                         skb->len += copy;
1093                         skb->data_len += copy;
1094                         skb->truesize += copy;
1095                         refcount_add(copy, &sk->sk_wmem_alloc);
1096                 }
1097                 offset += copy;
1098                 length -= copy;
1099         }
1100
1101         return 0;
1102
1103 error_efault:
1104         err = -EFAULT;
1105 error:
1106         cork->length -= length;
1107         IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
1108         return err;
1109 }
1110
1111 static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
1112                          struct ipcm_cookie *ipc, struct rtable **rtp)
1113 {
1114         struct ip_options_rcu *opt;
1115         struct rtable *rt;
1116
1117         /*
1118          * setup for corking.
1119          */
1120         opt = ipc->opt;
1121         if (opt) {
1122                 if (!cork->opt) {
1123                         cork->opt = kmalloc(sizeof(struct ip_options) + 40,
1124                                             sk->sk_allocation);
1125                         if (unlikely(!cork->opt))
1126                                 return -ENOBUFS;
1127                 }
1128                 memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen);
1129                 cork->flags |= IPCORK_OPT;
1130                 cork->addr = ipc->addr;
1131         }
1132         rt = *rtp;
1133         if (unlikely(!rt))
1134                 return -EFAULT;
1135
1136         cork->fragsize = ip_sk_use_pmtu(sk) ?
1137                          dst_mtu(&rt->dst) : READ_ONCE(rt->dst.dev->mtu);
1138
1139         if (!inetdev_valid_mtu(cork->fragsize))
1140                 return -ENETUNREACH;
1141
1142         cork->dst = &rt->dst;
1143         /* We stole this route, caller should not release it. */
1144         *rtp = NULL;
1145
1146         cork->length = 0;
1147         cork->ttl = ipc->ttl;
1148         cork->tos = ipc->tos;
1149         cork->priority = ipc->priority;
1150         cork->tx_flags = ipc->tx_flags;
1151
1152         return 0;
1153 }
1154
1155 /*
1156  *      ip_append_data() and ip_append_page() can make one large IP datagram
1157  *      from many pieces of data. Each pieces will be holded on the socket
1158  *      until ip_push_pending_frames() is called. Each piece can be a page
1159  *      or non-page data.
1160  *
1161  *      Not only UDP, other transport protocols - e.g. raw sockets - can use
1162  *      this interface potentially.
1163  *
1164  *      LATER: length must be adjusted by pad at tail, when it is required.
1165  */
1166 int ip_append_data(struct sock *sk, struct flowi4 *fl4,
1167                    int getfrag(void *from, char *to, int offset, int len,
1168                                int odd, struct sk_buff *skb),
1169                    void *from, int length, int transhdrlen,
1170                    struct ipcm_cookie *ipc, struct rtable **rtp,
1171                    unsigned int flags)
1172 {
1173         struct inet_sock *inet = inet_sk(sk);
1174         int err;
1175
1176         if (flags&MSG_PROBE)
1177                 return 0;
1178
1179         if (skb_queue_empty(&sk->sk_write_queue)) {
1180                 err = ip_setup_cork(sk, &inet->cork.base, ipc, rtp);
1181                 if (err)
1182                         return err;
1183         } else {
1184                 transhdrlen = 0;
1185         }
1186
1187         return __ip_append_data(sk, fl4, &sk->sk_write_queue, &inet->cork.base,
1188                                 sk_page_frag(sk), getfrag,
1189                                 from, length, transhdrlen, flags);
1190 }
1191
1192 ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
1193                        int offset, size_t size, int flags)
1194 {
1195         struct inet_sock *inet = inet_sk(sk);
1196         struct sk_buff *skb;
1197         struct rtable *rt;
1198         struct ip_options *opt = NULL;
1199         struct inet_cork *cork;
1200         int hh_len;
1201         int mtu;
1202         int len;
1203         int err;
1204         unsigned int maxfraglen, fragheaderlen, fraggap, maxnonfragsize;
1205
1206         if (inet->hdrincl)
1207                 return -EPERM;
1208
1209         if (flags&MSG_PROBE)
1210                 return 0;
1211
1212         if (skb_queue_empty(&sk->sk_write_queue))
1213                 return -EINVAL;
1214
1215         cork = &inet->cork.base;
1216         rt = (struct rtable *)cork->dst;
1217         if (cork->flags & IPCORK_OPT)
1218                 opt = cork->opt;
1219
1220         if (!(rt->dst.dev->features&NETIF_F_SG))
1221                 return -EOPNOTSUPP;
1222
1223         hh_len = LL_RESERVED_SPACE(rt->dst.dev);
1224         mtu = cork->fragsize;
1225
1226         fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
1227         maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
1228         maxnonfragsize = ip_sk_ignore_df(sk) ? 0xFFFF : mtu;
1229
1230         if (cork->length + size > maxnonfragsize - fragheaderlen) {
1231                 ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
1232                                mtu - (opt ? opt->optlen : 0));
1233                 return -EMSGSIZE;
1234         }
1235
1236         skb = skb_peek_tail(&sk->sk_write_queue);
1237         if (!skb)
1238                 return -EINVAL;
1239
1240         cork->length += size;
1241
1242         while (size > 0) {
1243                 /* Check if the remaining data fits into current packet. */
1244                 len = mtu - skb->len;
1245                 if (len < size)
1246                         len = maxfraglen - skb->len;
1247
1248                 if (len <= 0) {
1249                         struct sk_buff *skb_prev;
1250                         int alloclen;
1251
1252                         skb_prev = skb;
1253                         fraggap = skb_prev->len - maxfraglen;
1254
1255                         alloclen = fragheaderlen + hh_len + fraggap + 15;
1256                         skb = sock_wmalloc(sk, alloclen, 1, sk->sk_allocation);
1257                         if (unlikely(!skb)) {
1258                                 err = -ENOBUFS;
1259                                 goto error;
1260                         }
1261
1262                         /*
1263                          *      Fill in the control structures
1264                          */
1265                         skb->ip_summed = CHECKSUM_NONE;
1266                         skb->csum = 0;
1267                         skb_reserve(skb, hh_len);
1268
1269                         /*
1270                          *      Find where to start putting bytes.
1271                          */
1272                         skb_put(skb, fragheaderlen + fraggap);
1273                         skb_reset_network_header(skb);
1274                         skb->transport_header = (skb->network_header +
1275                                                  fragheaderlen);
1276                         if (fraggap) {
1277                                 skb->csum = skb_copy_and_csum_bits(skb_prev,
1278                                                                    maxfraglen,
1279                                                     skb_transport_header(skb),
1280                                                                    fraggap, 0);
1281                                 skb_prev->csum = csum_sub(skb_prev->csum,
1282                                                           skb->csum);
1283                                 pskb_trim_unique(skb_prev, maxfraglen);
1284                         }
1285
1286                         /*
1287                          * Put the packet on the pending queue.
1288                          */
1289                         __skb_queue_tail(&sk->sk_write_queue, skb);
1290                         continue;
1291                 }
1292
1293                 if (len > size)
1294                         len = size;
1295
1296                 if (skb_append_pagefrags(skb, page, offset, len)) {
1297                         err = -EMSGSIZE;
1298                         goto error;
1299                 }
1300
1301                 if (skb->ip_summed == CHECKSUM_NONE) {
1302                         __wsum csum;
1303                         csum = csum_page(page, offset, len);
1304                         skb->csum = csum_block_add(skb->csum, csum, skb->len);
1305                 }
1306
1307                 skb->len += len;
1308                 skb->data_len += len;
1309                 skb->truesize += len;
1310                 refcount_add(len, &sk->sk_wmem_alloc);
1311                 offset += len;
1312                 size -= len;
1313         }
1314         return 0;
1315
1316 error:
1317         cork->length -= size;
1318         IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTDISCARDS);
1319         return err;
1320 }
1321
1322 static void ip_cork_release(struct inet_cork *cork)
1323 {
1324         cork->flags &= ~IPCORK_OPT;
1325         kfree(cork->opt);
1326         cork->opt = NULL;
1327         dst_release(cork->dst);
1328         cork->dst = NULL;
1329 }
1330
1331 /*
1332  *      Combined all pending IP fragments on the socket as one IP datagram
1333  *      and push them out.
1334  */
1335 struct sk_buff *__ip_make_skb(struct sock *sk,
1336                               struct flowi4 *fl4,
1337                               struct sk_buff_head *queue,
1338                               struct inet_cork *cork)
1339 {
1340         struct sk_buff *skb, *tmp_skb;
1341         struct sk_buff **tail_skb;
1342         struct inet_sock *inet = inet_sk(sk);
1343         struct net *net = sock_net(sk);
1344         struct ip_options *opt = NULL;
1345         struct rtable *rt = (struct rtable *)cork->dst;
1346         struct iphdr *iph;
1347         __be16 df = 0;
1348         __u8 ttl;
1349
1350         skb = __skb_dequeue(queue);
1351         if (!skb)
1352                 goto out;
1353         tail_skb = &(skb_shinfo(skb)->frag_list);
1354
1355         /* move skb->data to ip header from ext header */
1356         if (skb->data < skb_network_header(skb))
1357                 __skb_pull(skb, skb_network_offset(skb));
1358         while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
1359                 __skb_pull(tmp_skb, skb_network_header_len(skb));
1360                 *tail_skb = tmp_skb;
1361                 tail_skb = &(tmp_skb->next);
1362                 skb->len += tmp_skb->len;
1363                 skb->data_len += tmp_skb->len;
1364                 skb->truesize += tmp_skb->truesize;
1365                 tmp_skb->destructor = NULL;
1366                 tmp_skb->sk = NULL;
1367         }
1368
1369         /* Unless user demanded real pmtu discovery (IP_PMTUDISC_DO), we allow
1370          * to fragment the frame generated here. No matter, what transforms
1371          * how transforms change size of the packet, it will come out.
1372          */
1373         skb->ignore_df = ip_sk_ignore_df(sk);
1374
1375         /* DF bit is set when we want to see DF on outgoing frames.
1376          * If ignore_df is set too, we still allow to fragment this frame
1377          * locally. */
1378         if (inet->pmtudisc == IP_PMTUDISC_DO ||
1379             inet->pmtudisc == IP_PMTUDISC_PROBE ||
1380             (skb->len <= dst_mtu(&rt->dst) &&
1381              ip_dont_fragment(sk, &rt->dst)))
1382                 df = htons(IP_DF);
1383
1384         if (cork->flags & IPCORK_OPT)
1385                 opt = cork->opt;
1386
1387         if (cork->ttl != 0)
1388                 ttl = cork->ttl;
1389         else if (rt->rt_type == RTN_MULTICAST)
1390                 ttl = inet->mc_ttl;
1391         else
1392                 ttl = ip_select_ttl(inet, &rt->dst);
1393
1394         iph = ip_hdr(skb);
1395         iph->version = 4;
1396         iph->ihl = 5;
1397         iph->tos = (cork->tos != -1) ? cork->tos : inet->tos;
1398         iph->frag_off = df;
1399         iph->ttl = ttl;
1400         iph->protocol = sk->sk_protocol;
1401         ip_copy_addrs(iph, fl4);
1402         ip_select_ident(net, skb, sk);
1403
1404         if (opt) {
1405                 iph->ihl += opt->optlen>>2;
1406                 ip_options_build(skb, opt, cork->addr, rt, 0);
1407         }
1408
1409         skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority;
1410         skb->mark = sk->sk_mark;
1411         /*
1412          * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec
1413          * on dst refcount
1414          */
1415         cork->dst = NULL;
1416         skb_dst_set(skb, &rt->dst);
1417
1418         if (iph->protocol == IPPROTO_ICMP) {
1419                 u8 icmp_type;
1420
1421                 /* For such sockets, transhdrlen is zero when do ip_append_data(),
1422                  * so icmphdr does not in skb linear region and can not get icmp_type
1423                  * by icmp_hdr(skb)->type.
1424                  */
1425                 if (sk->sk_type == SOCK_RAW && !inet_sk(sk)->hdrincl)
1426                         icmp_type = fl4->fl4_icmp_type;
1427                 else
1428                         icmp_type = icmp_hdr(skb)->type;
1429                 icmp_out_count(net, icmp_type);
1430         }
1431
1432         ip_cork_release(cork);
1433 out:
1434         return skb;
1435 }
1436
1437 int ip_send_skb(struct net *net, struct sk_buff *skb)
1438 {
1439         int err;
1440
1441         err = ip_local_out(net, skb->sk, skb);
1442         if (err) {
1443                 if (err > 0)
1444                         err = net_xmit_errno(err);
1445                 if (err)
1446                         IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
1447         }
1448
1449         return err;
1450 }
1451
1452 int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4)
1453 {
1454         struct sk_buff *skb;
1455
1456         skb = ip_finish_skb(sk, fl4);
1457         if (!skb)
1458                 return 0;
1459
1460         /* Netfilter gets whole the not fragmented skb. */
1461         return ip_send_skb(sock_net(sk), skb);
1462 }
1463
1464 /*
1465  *      Throw away all pending data on the socket.
1466  */
1467 static void __ip_flush_pending_frames(struct sock *sk,
1468                                       struct sk_buff_head *queue,
1469                                       struct inet_cork *cork)
1470 {
1471         struct sk_buff *skb;
1472
1473         while ((skb = __skb_dequeue_tail(queue)) != NULL)
1474                 kfree_skb(skb);
1475
1476         ip_cork_release(cork);
1477 }
1478
1479 void ip_flush_pending_frames(struct sock *sk)
1480 {
1481         __ip_flush_pending_frames(sk, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
1482 }
1483
1484 struct sk_buff *ip_make_skb(struct sock *sk,
1485                             struct flowi4 *fl4,
1486                             int getfrag(void *from, char *to, int offset,
1487                                         int len, int odd, struct sk_buff *skb),
1488                             void *from, int length, int transhdrlen,
1489                             struct ipcm_cookie *ipc, struct rtable **rtp,
1490                             unsigned int flags)
1491 {
1492         struct inet_cork cork;
1493         struct sk_buff_head queue;
1494         int err;
1495
1496         if (flags & MSG_PROBE)
1497                 return NULL;
1498
1499         __skb_queue_head_init(&queue);
1500
1501         cork.flags = 0;
1502         cork.addr = 0;
1503         cork.opt = NULL;
1504         err = ip_setup_cork(sk, &cork, ipc, rtp);
1505         if (err)
1506                 return ERR_PTR(err);
1507
1508         err = __ip_append_data(sk, fl4, &queue, &cork,
1509                                &current->task_frag, getfrag,
1510                                from, length, transhdrlen, flags);
1511         if (err) {
1512                 __ip_flush_pending_frames(sk, &queue, &cork);
1513                 return ERR_PTR(err);
1514         }
1515
1516         return __ip_make_skb(sk, fl4, &queue, &cork);
1517 }
1518
1519 /*
1520  *      Fetch data from kernel space and fill in checksum if needed.
1521  */
1522 static int ip_reply_glue_bits(void *dptr, char *to, int offset,
1523                               int len, int odd, struct sk_buff *skb)
1524 {
1525         __wsum csum;
1526
1527         csum = csum_partial_copy_nocheck(dptr+offset, to, len, 0);
1528         skb->csum = csum_block_add(skb->csum, csum, odd);
1529         return 0;
1530 }
1531
1532 /*
1533  *      Generic function to send a packet as reply to another packet.
1534  *      Used to send some TCP resets/acks so far.
1535  */
1536 void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,
1537                            const struct ip_options *sopt,
1538                            __be32 daddr, __be32 saddr,
1539                            const struct ip_reply_arg *arg,
1540                            unsigned int len)
1541 {
1542         struct ip_options_data replyopts;
1543         struct ipcm_cookie ipc;
1544         struct flowi4 fl4;
1545         struct rtable *rt = skb_rtable(skb);
1546         struct net *net = sock_net(sk);
1547         struct sk_buff *nskb;
1548         int err;
1549         int oif;
1550
1551         if (__ip_options_echo(net, &replyopts.opt.opt, skb, sopt))
1552                 return;
1553
1554         ipc.addr = daddr;
1555         ipc.opt = NULL;
1556         ipc.tx_flags = 0;
1557         ipc.ttl = 0;
1558         ipc.tos = -1;
1559
1560         if (replyopts.opt.opt.optlen) {
1561                 ipc.opt = &replyopts.opt;
1562
1563                 if (replyopts.opt.opt.srr)
1564                         daddr = replyopts.opt.opt.faddr;
1565         }
1566
1567         oif = arg->bound_dev_if;
1568         if (!oif && netif_index_is_l3_master(net, skb->skb_iif))
1569                 oif = skb->skb_iif;
1570
1571         flowi4_init_output(&fl4, oif,
1572                            IP4_REPLY_MARK(net, skb->mark),
1573                            RT_TOS(arg->tos),
1574                            RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol,
1575                            ip_reply_arg_flowi_flags(arg),
1576                            daddr, saddr,
1577                            tcp_hdr(skb)->source, tcp_hdr(skb)->dest,
1578                            arg->uid);
1579         security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
1580         rt = ip_route_output_key(net, &fl4);
1581         if (IS_ERR(rt))
1582                 return;
1583
1584         inet_sk(sk)->tos = arg->tos & ~INET_ECN_MASK;
1585
1586         sk->sk_priority = skb->priority;
1587         sk->sk_protocol = ip_hdr(skb)->protocol;
1588         sk->sk_bound_dev_if = arg->bound_dev_if;
1589         sk->sk_sndbuf = sysctl_wmem_default;
1590         sk->sk_mark = fl4.flowi4_mark;
1591         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
1598         nskb = skb_peek(&sk->sk_write_queue);
1599         if (nskb) {
1600                 if (arg->csumoffset >= 0)
1601                         *((__sum16 *)skb_transport_header(nskb) +
1602                           arg->csumoffset) = csum_fold(csum_add(nskb->csum,
1603                                                                 arg->csum));
1604                 nskb->ip_summed = CHECKSUM_NONE;
1605                 ip_push_pending_frames(sk, &fl4);
1606         }
1607 out:
1608         ip_rt_put(rt);
1609 }
1610
1611 void __init ip_init(void)
1612 {
1613         ip_rt_init();
1614         inet_initpeers();
1615
1616 #if defined(CONFIG_IP_MULTICAST)
1617         igmp_mc_init();
1618 #endif
1619 }