blob: 2d28f22173476210ceac528e1d180a213ff7d929 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 output functions
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Based on linux/net/ipv4/ip_output.c
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 * Changes:
16 * A.N.Kuznetsov : airthmetics in fragmentation.
17 * extension headers are implemented.
18 * route changes now work.
19 * ip6_forward does not confuse sniffers.
20 * etc.
21 *
22 * H. von Brand : Added missing #include <linux/string.h>
Ian Morris67ba4152014-08-24 21:53:10 +010023 * Imran Patel : frag id should be in NBO
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * Kazunori MIYAZAWA @USAGI
25 * : add ip6_append_data and related functions
26 * for datagram xmit
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/errno.h>
Herbert Xuef76bc22008-01-11 19:15:08 -080030#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/string.h>
32#include <linux/socket.h>
33#include <linux/net.h>
34#include <linux/netdevice.h>
35#include <linux/if_arp.h>
36#include <linux/in6.h>
37#include <linux/tcp.h>
38#include <linux/route.h>
Herbert Xub59f45d2006-05-27 23:05:54 -070039#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Daniel Mack9290d742016-11-23 16:52:29 +010042#include <linux/bpf-cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/netfilter.h>
44#include <linux/netfilter_ipv6.h>
45
46#include <net/sock.h>
47#include <net/snmp.h>
48
49#include <net/ipv6.h>
50#include <net/ndisc.h>
51#include <net/protocol.h>
52#include <net/ip6_route.h>
53#include <net/addrconf.h>
54#include <net/rawv6.h>
55#include <net/icmp.h>
56#include <net/xfrm.h>
57#include <net/checksum.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090058#include <linux/mroute6.h>
David Ahernca254492015-10-12 11:47:10 -070059#include <net/l3mdev.h>
Roopa Prabhu14972cb2016-08-24 20:10:43 -070060#include <net/lwtunnel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -050062static int ip6_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Eric Dumazetadf30902009-06-02 05:19:30 +000064 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 struct net_device *dev = dst->dev;
David S. Millerf6b72b62011-07-14 07:53:20 -070066 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明6fd6ce22013-01-17 12:54:00 +000067 struct in6_addr *nexthop;
68 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070070 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
Eric Dumazetadf30902009-06-02 05:19:30 +000071 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
David Miller7026b1d2015-04-05 22:19:04 -040073 if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(sk) &&
Eric W. Biederman78126c42015-09-15 20:04:09 -050074 ((mroute6_socket(net, skb) &&
Benjamin Therybd91b8b2008-12-10 16:07:08 -080075 !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) ||
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090076 ipv6_chk_mcast_addr(dev, &ipv6_hdr(skb)->daddr,
77 &ipv6_hdr(skb)->saddr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
79
80 /* Do not check for IFF_ALLMULTI; multicast routing
81 is not supported in any case.
82 */
83 if (newskb)
Jan Engelhardtb2e0b382010-03-23 04:09:07 +010084 NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING,
Eric W. Biederman29a26a52015-09-15 20:04:16 -050085 net, sk, newskb, NULL, newskb->dev,
Michel Machado95603e22012-06-12 10:16:35 +000086 dev_loopback_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070088 if (ipv6_hdr(skb)->hop_limit == 0) {
Eric W. Biederman78126c42015-09-15 20:04:09 -050089 IP6_INC_STATS(net, idev,
Denis V. Lunev3bd653c2008-10-08 10:54:51 -070090 IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 kfree_skb(skb);
92 return 0;
93 }
94 }
95
Eric W. Biederman78126c42015-09-15 20:04:09 -050096 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, skb->len);
Hannes Frederic Sowadd408512013-02-10 02:33:35 +000097
98 if (IPV6_ADDR_MC_SCOPE(&ipv6_hdr(skb)->daddr) <=
99 IPV6_ADDR_SCOPE_NODELOCAL &&
100 !(dev->flags & IFF_LOOPBACK)) {
101 kfree_skb(skb);
102 return 0;
103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700106 if (lwtunnel_xmit_redirect(dst->lwtstate)) {
107 int res = lwtunnel_xmit(skb);
108
109 if (res < 0 || res == LWTUNNEL_XMIT_DONE)
110 return res;
111 }
112
YOSHIFUJI Hideaki / 吉藤英明6fd6ce22013-01-17 12:54:00 +0000113 rcu_read_lock_bh();
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700114 nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr);
YOSHIFUJI Hideaki / 吉藤英明6fd6ce22013-01-17 12:54:00 +0000115 neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
116 if (unlikely(!neigh))
117 neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
118 if (!IS_ERR(neigh)) {
119 ret = dst_neigh_output(dst, neigh, skb);
120 rcu_read_unlock_bh();
121 return ret;
122 }
123 rcu_read_unlock_bh();
David S. Miller05e3aa02011-07-16 17:26:00 -0700124
Eric W. Biederman78126c42015-09-15 20:04:09 -0500125 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
Jan Engelhardt9e508492010-04-13 15:28:11 +0200126 kfree_skb(skb);
127 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500130static int ip6_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Jan Engelhardt9e508492010-04-13 15:28:11 +0200131{
Daniel Mack9290d742016-11-23 16:52:29 +0100132 int ret;
133
134 ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
135 if (ret) {
136 kfree_skb(skb);
137 return ret;
138 }
139
Jan Engelhardt9e508492010-04-13 15:28:11 +0200140 if ((skb->len > ip6_skb_dst_mtu(skb) && !skb_is_gso(skb)) ||
Jiri Pirko9037c352013-11-06 17:52:19 +0100141 dst_allfrag(skb_dst(skb)) ||
142 (IP6CB(skb)->frag_max_size && skb->len > IP6CB(skb)->frag_max_size))
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500143 return ip6_fragment(net, sk, skb, ip6_finish_output2);
Jan Engelhardt9e508492010-04-13 15:28:11 +0200144 else
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500145 return ip6_finish_output2(net, sk, skb);
Jan Engelhardt9e508492010-04-13 15:28:11 +0200146}
147
Eric W. Biedermanede20592015-10-07 16:48:47 -0500148int ip6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Jan Engelhardt9e508492010-04-13 15:28:11 +0200150 struct net_device *dev = skb_dst(skb)->dev;
Eric Dumazetadf30902009-06-02 05:19:30 +0000151 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
Eric W. Biedermanbe10de02015-09-17 17:21:31 -0500152
Chenbo Feng2c4f58a2017-06-09 12:06:07 -0700153 skb->protocol = htons(ETH_P_IPV6);
154 skb->dev = dev;
155
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900156 if (unlikely(idev->cnf.disable_ipv6)) {
Eric W. Biederman19a06442015-09-15 20:04:10 -0500157 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900158 kfree_skb(skb);
159 return 0;
160 }
161
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500162 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
163 net, sk, skb, NULL, dev,
Jan Engelhardt9c6eb282010-04-13 15:32:16 +0200164 ip6_finish_output,
165 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Ben Hutchings8b0d3e82018-01-22 20:06:42 +0000168bool ip6_autoflowlabel(struct net *net, const struct ipv6_pinfo *np)
Shaohua Lib3b56032017-12-20 12:10:21 -0800169{
170 if (!np->autoflowlabel_set)
171 return ip6_default_np_autolabel(net);
172 else
173 return np->autoflowlabel;
174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/*
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700177 * xmit an sk_buff (used by TCP, SCTP and DCCP)
178 * Note : socket lock is not held for SYNACK packets, but might be modified
179 * by calls to skb_set_owner_w() and ipv6_local_error(),
180 * which are using proper atomic operations or spinlocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 */
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700182int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
Pablo Neira0d4c19e2017-01-26 22:56:21 +0100183 __u32 mark, struct ipv6_txoptions *opt, int tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700185 struct net *net = sock_net(sk);
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700186 const struct ipv6_pinfo *np = inet6_sk(sk);
David S. Miller4c9483b2011-03-12 16:22:43 -0500187 struct in6_addr *first_hop = &fl6->daddr;
Eric Dumazetadf30902009-06-02 05:19:30 +0000188 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 struct ipv6hdr *hdr;
David S. Miller4c9483b2011-03-12 16:22:43 -0500190 u8 proto = fl6->flowi6_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 int seg_len = skb->len;
Gerrit Renkere651f032009-08-09 08:12:48 +0000192 int hlimit = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 u32 mtu;
194
195 if (opt) {
Chuck Leverc2636b42007-10-23 21:07:32 -0700196 unsigned int head_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 /* First: exthdrs may take lots of space (~8K for now)
199 MAX_HEADER is not enough.
200 */
201 head_room = opt->opt_nflen + opt->opt_flen;
202 seg_len += head_room;
203 head_room += sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev);
204
205 if (skb_headroom(skb) < head_room) {
206 struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room);
Ian Morris63159f22015-03-29 14:00:04 +0100207 if (!skb2) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000208 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900209 IPSTATS_MIB_OUTDISCARDS);
210 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return -ENOBUFS;
212 }
Eric Dumazet808db802012-04-24 10:17:59 +0000213 consume_skb(skb);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900214 skb = skb2;
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700215 /* skb_set_owner_w() changes sk->sk_wmem_alloc atomically,
216 * it is safe to call in our context (socket lock not held)
217 */
218 skb_set_owner_w(skb, (struct sock *)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220 if (opt->opt_flen)
221 ipv6_push_frag_opts(skb, opt, &proto);
222 if (opt->opt_nflen)
223 ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop);
224 }
225
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700226 skb_push(skb, sizeof(struct ipv6hdr));
227 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700228 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 /*
231 * Fill in the IPv6 header
232 */
Eric Dumazetb903d322011-10-27 00:44:35 -0400233 if (np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 hlimit = np->hop_limit;
235 if (hlimit < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -0400236 hlimit = ip6_dst_hoplimit(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700238 ip6_flow_hdr(hdr, tclass, ip6_make_flowlabel(net, skb, fl6->flowlabel,
Shaohua Lib3b56032017-12-20 12:10:21 -0800239 ip6_autoflowlabel(net, np), fl6));
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 hdr->payload_len = htons(seg_len);
242 hdr->nexthdr = proto;
243 hdr->hop_limit = hlimit;
244
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000245 hdr->saddr = fl6->saddr;
246 hdr->daddr = *first_hop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Hannes Frederic Sowa9c9c9ad2013-08-26 12:31:23 +0200248 skb->protocol = htons(ETH_P_IPV6);
Patrick McHardya2c20642006-01-08 22:37:26 -0800249 skb->priority = sk->sk_priority;
Pablo Neira0d4c19e2017-01-26 22:56:21 +0100250 skb->mark = mark;
Patrick McHardya2c20642006-01-08 22:37:26 -0800251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 mtu = dst_mtu(dst);
WANG Cong60ff7462014-05-04 16:39:18 -0700253 if ((skb->len <= mtu) || skb->ignore_df || skb_is_gso(skb)) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000254 IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)),
Neil Hormanedf391f2009-04-27 02:45:02 -0700255 IPSTATS_MIB_OUT, skb->len);
David Aherna8e3e1a2016-09-10 12:09:53 -0700256
257 /* if egress device is enslaved to an L3 master device pass the
258 * skb to its handler for processing
259 */
260 skb = l3mdev_ip6_out((struct sock *)sk, skb);
261 if (unlikely(!skb))
262 return 0;
263
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700264 /* hooks should never assume socket lock is held.
265 * we promote our socket to non const
266 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500267 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700268 net, (struct sock *)sk, skb, NULL, dst->dev,
Eric W. Biederman13206b62015-10-07 16:48:35 -0500269 dst_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 skb->dev = dst->dev;
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700273 /* ipv6_local_error() does not require socket lock,
274 * we promote our socket to non const
275 */
276 ipv6_local_error((struct sock *)sk, EMSGSIZE, fl6, mtu);
277
Eric Dumazetadf30902009-06-02 05:19:30 +0000278 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 kfree_skb(skb);
280 return -EMSGSIZE;
281}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900282EXPORT_SYMBOL(ip6_xmit);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284static int ip6_call_ra_chain(struct sk_buff *skb, int sel)
285{
286 struct ip6_ra_chain *ra;
287 struct sock *last = NULL;
288
289 read_lock(&ip6_ra_lock);
290 for (ra = ip6_ra_chain; ra; ra = ra->next) {
291 struct sock *sk = ra->sk;
Andrew McDonald0bd1b592005-08-09 19:44:42 -0700292 if (sk && ra->sel == sel &&
293 (!sk->sk_bound_dev_if ||
294 sk->sk_bound_dev_if == skb->dev->ifindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (last) {
296 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
297 if (skb2)
298 rawv6_rcv(last, skb2);
299 }
300 last = sk;
301 }
302 }
303
304 if (last) {
305 rawv6_rcv(last, skb);
306 read_unlock(&ip6_ra_lock);
307 return 1;
308 }
309 read_unlock(&ip6_ra_lock);
310 return 0;
311}
312
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700313static int ip6_forward_proxy_check(struct sk_buff *skb)
314{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700315 struct ipv6hdr *hdr = ipv6_hdr(skb);
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700316 u8 nexthdr = hdr->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -0800317 __be16 frag_off;
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700318 int offset;
319
320 if (ipv6_ext_hdr(nexthdr)) {
Jesse Gross75f28112011-11-30 17:05:51 -0800321 offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr, &frag_off);
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700322 if (offset < 0)
323 return 0;
324 } else
325 offset = sizeof(struct ipv6hdr);
326
327 if (nexthdr == IPPROTO_ICMPV6) {
328 struct icmp6hdr *icmp6;
329
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700330 if (!pskb_may_pull(skb, (skb_network_header(skb) +
331 offset + 1 - skb->data)))
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700332 return 0;
333
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700334 icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700335
336 switch (icmp6->icmp6_type) {
337 case NDISC_ROUTER_SOLICITATION:
338 case NDISC_ROUTER_ADVERTISEMENT:
339 case NDISC_NEIGHBOUR_SOLICITATION:
340 case NDISC_NEIGHBOUR_ADVERTISEMENT:
341 case NDISC_REDIRECT:
342 /* For reaction involving unicast neighbor discovery
343 * message destined to the proxied address, pass it to
344 * input function.
345 */
346 return 1;
347 default:
348 break;
349 }
350 }
351
Ville Nuorvala74553b02006-09-22 14:42:18 -0700352 /*
353 * The proxying router can't forward traffic sent to a link-local
354 * address, so signal the sender and discard the packet. This
355 * behavior is clarified by the MIPv6 specification.
356 */
357 if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) {
358 dst_link_failure(skb);
359 return -1;
360 }
361
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700362 return 0;
363}
364
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500365static inline int ip6_forward_finish(struct net *net, struct sock *sk,
366 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Jeff Barnhill584d5412018-04-05 21:29:47 +0000368 struct dst_entry *dst = skb_dst(skb);
369
370 __IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS);
371 __IP6_ADD_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTOCTETS, skb->len);
372
Eric W. Biederman13206b62015-10-07 16:48:35 -0500373 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Hannes Frederic Sowa0954cf92014-01-09 10:01:16 +0100376static unsigned int ip6_dst_mtu_forward(const struct dst_entry *dst)
377{
378 unsigned int mtu;
379 struct inet6_dev *idev;
380
381 if (dst_metric_locked(dst, RTAX_MTU)) {
382 mtu = dst_metric_raw(dst, RTAX_MTU);
383 if (mtu)
384 return mtu;
385 }
386
387 mtu = IPV6_MIN_MTU;
388 rcu_read_lock();
389 idev = __in6_dev_get(dst->dev);
390 if (idev)
391 mtu = idev->cnf.mtu6;
392 rcu_read_unlock();
393
394 return mtu;
395}
396
Florian Westphalfe6cc552014-02-13 23:09:12 +0100397static bool ip6_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
398{
Florian Westphal418a3152014-05-05 00:03:34 +0200399 if (skb->len <= mtu)
Florian Westphalfe6cc552014-02-13 23:09:12 +0100400 return false;
401
WANG Cong60ff7462014-05-04 16:39:18 -0700402 /* ipv6 conntrack defrag sets max_frag_size + ignore_df */
Florian Westphalfe6cc552014-02-13 23:09:12 +0100403 if (IP6CB(skb)->frag_max_size && IP6CB(skb)->frag_max_size > mtu)
404 return true;
405
WANG Cong60ff7462014-05-04 16:39:18 -0700406 if (skb->ignore_df)
Florian Westphal418a3152014-05-05 00:03:34 +0200407 return false;
408
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -0300409 if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
Florian Westphalfe6cc552014-02-13 23:09:12 +0100410 return false;
411
412 return true;
413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415int ip6_forward(struct sk_buff *skb)
416{
Eric Dumazetadf30902009-06-02 05:19:30 +0000417 struct dst_entry *dst = skb_dst(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700418 struct ipv6hdr *hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 struct inet6_skb_parm *opt = IP6CB(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900420 struct net *net = dev_net(dst->dev);
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800421 u32 mtu;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900422
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700423 if (net->ipv6.devconf_all->forwarding == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 goto error;
425
Li RongQing090f1162014-03-11 10:40:08 +0800426 if (skb->pkt_type != PACKET_HOST)
427 goto drop;
428
Hannes Frederic Sowa9ef2e962015-10-08 18:19:53 +0200429 if (unlikely(skb->sk))
430 goto drop;
431
Ben Hutchings4497b072008-06-19 16:22:28 -0700432 if (skb_warn_if_lro(skb))
433 goto drop;
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700436 __IP6_INC_STATS(net, ip6_dst_idev(dst),
437 IPSTATS_MIB_INDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 goto drop;
439 }
440
Herbert Xu35fc92a2007-03-26 23:22:20 -0700441 skb_forward_csum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 /*
444 * We DO NOT make any processing on
445 * RA packets, pushing them to user level AS IS
446 * without ane WARRANTY that application will be able
447 * to interpret them. The reason is that we
448 * cannot make anything clever here.
449 *
450 * We are not end-node, so that if packet contains
451 * AH/ESP, we cannot make anything.
452 * Defragmentation also would be mistake, RA packets
453 * cannot be fragmented, because there is no warranty
454 * that different fragments will go along one path. --ANK
455 */
YOSHIFUJI Hideaki / 吉藤英明ab4eb352013-06-22 11:13:13 +0900456 if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
457 if (ip6_call_ra_chain(skb, ntohs(opt->ra)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return 0;
459 }
460
461 /*
462 * check and decrement ttl
463 */
464 if (hdr->hop_limit <= 1) {
465 /* Force OUTPUT device used as source address */
466 skb->dev = dst->dev;
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000467 icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0);
Eric Dumazet1d015502016-04-27 16:44:40 -0700468 __IP6_INC_STATS(net, ip6_dst_idev(dst),
469 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 kfree_skb(skb);
472 return -ETIMEDOUT;
473 }
474
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700475 /* XXX: idev->cnf.proxy_ndp? */
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700476 if (net->ipv6.devconf_all->proxy_ndp &&
Daniel Lezcano8a3edd82008-03-07 11:14:16 -0800477 pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0)) {
Ville Nuorvala74553b02006-09-22 14:42:18 -0700478 int proxied = ip6_forward_proxy_check(skb);
479 if (proxied > 0)
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700480 return ip6_input(skb);
Ville Nuorvala74553b02006-09-22 14:42:18 -0700481 else if (proxied < 0) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700482 __IP6_INC_STATS(net, ip6_dst_idev(dst),
483 IPSTATS_MIB_INDISCARDS);
Ville Nuorvala74553b02006-09-22 14:42:18 -0700484 goto drop;
485 }
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700486 }
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (!xfrm6_route_forward(skb)) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700489 __IP6_INC_STATS(net, ip6_dst_idev(dst),
490 IPSTATS_MIB_INDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 goto drop;
492 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000493 dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 /* IPv6 specs say nothing about it, but it is clear that we cannot
496 send redirects to source routed frames.
Masahide NAKAMURA1e5dc142007-08-24 19:08:55 +0900497 We don't send redirects to frames decapsulated from IPsec.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 */
Stephen Suryaputra869584e2018-06-01 00:05:21 -0400499 if (IP6CB(skb)->iif == dst->dev->ifindex &&
500 opt->srcrt == 0 && !skb_sec_path(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 struct in6_addr *target = NULL;
David S. Millerfbfe95a2012-06-08 23:24:18 -0700502 struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 /*
506 * incoming and outgoing devices are the same
507 * send a redirect.
508 */
509
510 rt = (struct rt6_info *) dst;
David S. Millerc45a3df2012-01-27 15:32:19 -0800511 if (rt->rt6i_flags & RTF_GATEWAY)
512 target = &rt->rt6i_gateway;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 else
514 target = &hdr->daddr;
515
Martin KaFai Laufd0273d2015-05-22 20:55:57 -0700516 peer = inet_getpeer_v6(net->ipv6.peers, &hdr->daddr, 1);
David S. Miller92d86822011-02-04 15:55:25 -0800517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /* Limit redirects both by destination (here)
519 and by source (inside ndisc_send_redirect)
520 */
David S. Millerfbfe95a2012-06-08 23:24:18 -0700521 if (inet_peer_xrlim_allow(peer, 1*HZ))
David S. Miller49919692012-01-27 15:30:48 -0800522 ndisc_send_redirect(skb, target);
David S. Miller1d861aa2012-07-10 03:58:16 -0700523 if (peer)
524 inet_putpeer(peer);
David L Stevens5bb1ab02007-05-09 13:53:44 -0700525 } else {
526 int addrtype = ipv6_addr_type(&hdr->saddr);
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /* This check is security critical. */
YOSHIFUJI Hideakif81b2e72008-06-25 16:55:26 +0900529 if (addrtype == IPV6_ADDR_ANY ||
530 addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
David L Stevens5bb1ab02007-05-09 13:53:44 -0700531 goto error;
532 if (addrtype & IPV6_ADDR_LINKLOCAL) {
533 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000534 ICMPV6_NOT_NEIGHBOUR, 0);
David L Stevens5bb1ab02007-05-09 13:53:44 -0700535 goto error;
536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538
Hannes Frederic Sowa0954cf92014-01-09 10:01:16 +0100539 mtu = ip6_dst_mtu_forward(dst);
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800540 if (mtu < IPV6_MIN_MTU)
541 mtu = IPV6_MIN_MTU;
542
Florian Westphalfe6cc552014-02-13 23:09:12 +0100543 if (ip6_pkt_too_big(skb, mtu)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* Again, force OUTPUT device used as source address */
545 skb->dev = dst->dev;
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800546 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Eric Dumazet1d015502016-04-27 16:44:40 -0700547 __IP6_INC_STATS(net, ip6_dst_idev(dst),
548 IPSTATS_MIB_INTOOBIGERRORS);
549 __IP6_INC_STATS(net, ip6_dst_idev(dst),
550 IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 kfree_skb(skb);
552 return -EMSGSIZE;
553 }
554
555 if (skb_cow(skb, dst->dev->hard_header_len)) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700556 __IP6_INC_STATS(net, ip6_dst_idev(dst),
557 IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 goto drop;
559 }
560
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700561 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 /* Mangling hops number delayed to point after skb COW */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 hdr->hop_limit--;
566
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500567 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD,
568 net, NULL, skb, skb->dev, dst->dev,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800569 ip6_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571error:
Eric Dumazet1d015502016-04-27 16:44:40 -0700572 __IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INADDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573drop:
574 kfree_skb(skb);
575 return -EINVAL;
576}
577
578static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
579{
580 to->pkt_type = from->pkt_type;
581 to->priority = from->priority;
582 to->protocol = from->protocol;
Eric Dumazetadf30902009-06-02 05:19:30 +0000583 skb_dst_drop(to);
584 skb_dst_set(to, dst_clone(skb_dst(from)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 to->dev = from->dev;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800586 to->mark = from->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588#ifdef CONFIG_NET_SCHED
589 to->tc_index = from->tc_index;
590#endif
Yasuyuki Kozakaie7ac05f2007-03-14 16:44:01 -0700591 nf_copy(to, from);
James Morris984bc162006-06-09 00:29:17 -0700592 skb_copy_secmark(to, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500595int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
596 int (*output)(struct net *, struct sock *, struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 struct sk_buff *frag;
Ian Morris67ba4152014-08-24 21:53:10 +0100599 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
hannes@stressinduktion.orgf60e5992015-04-01 17:07:44 +0200600 struct ipv6_pinfo *np = skb->sk && !dev_recursion_level() ?
601 inet6_sk(skb->sk) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 struct ipv6hdr *tmp_hdr;
603 struct frag_hdr *fh;
604 unsigned int mtu, hlen, left, len;
Herbert Xua7ae1992011-11-18 02:20:04 +0000605 int hroom, troom;
Martin KaFai Lau286c2342015-05-22 20:55:56 -0700606 __be32 frag_id;
Ian Morris67ba4152014-08-24 21:53:10 +0100607 int ptr, offset = 0, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 u8 *prevhdr, nexthdr = 0;
609
David S. Miller3fa202e2017-05-17 22:54:11 -0400610 err = ip6_find_1stfragopt(skb, &prevhdr);
611 if (err < 0)
Craig Galleka2c845e2017-05-16 14:36:23 -0400612 goto fail;
David S. Miller3fa202e2017-05-17 22:54:11 -0400613 hlen = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 nexthdr = *prevhdr;
615
John Heffner628a5c52007-04-20 15:53:27 -0700616 mtu = ip6_skb_dst_mtu(skb);
John Heffnerb881ef72007-04-20 15:52:39 -0700617
618 /* We must not fragment if the socket is set to force MTU discovery
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800619 * or if the skb it not generated by a local socket.
John Heffnerb881ef72007-04-20 15:52:39 -0700620 */
Florian Westphal485fca62015-05-22 00:44:16 +0200621 if (unlikely(!skb->ignore_df && skb->len > mtu))
622 goto fail_toobig;
Eric Dumazeta34a1012012-05-18 21:51:44 +0000623
Florian Westphal485fca62015-05-22 00:44:16 +0200624 if (IP6CB(skb)->frag_max_size) {
625 if (IP6CB(skb)->frag_max_size > mtu)
626 goto fail_toobig;
627
628 /* don't send fragments larger than what we received */
629 mtu = IP6CB(skb)->frag_max_size;
630 if (mtu < IPV6_MIN_MTU)
631 mtu = IPV6_MIN_MTU;
John Heffnerb881ef72007-04-20 15:52:39 -0700632 }
633
YOSHIFUJI Hideakid91675f2006-02-24 13:18:33 -0800634 if (np && np->frag_size < mtu) {
635 if (np->frag_size)
636 mtu = np->frag_size;
637 }
Hannes Frederic Sowa89bc7842015-10-28 13:21:04 +0100638 if (mtu < hlen + sizeof(struct frag_hdr) + 8)
Hannes Frederic Sowab72a2b02015-10-16 11:32:43 +0200639 goto fail_toobig;
Hannes Frederic Sowa1e0d69a2015-10-28 13:21:03 +0100640 mtu -= hlen + sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Martin KaFai Laufd0273d2015-05-22 20:55:57 -0700642 frag_id = ipv6_select_ident(net, &ipv6_hdr(skb)->daddr,
643 &ipv6_hdr(skb)->saddr);
Martin KaFai Lau286c2342015-05-22 20:55:56 -0700644
Hannes Frederic Sowa405c92f2015-10-27 22:40:42 +0100645 if (skb->ip_summed == CHECKSUM_PARTIAL &&
646 (err = skb_checksum_help(skb)))
647 goto fail;
648
Florian Westphal1d325d22015-09-16 17:26:14 +0200649 hroom = LL_RESERVED_SPACE(rt->dst.dev);
David S. Miller21dc3302010-08-23 00:13:46 -0700650 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 int first_len = skb_pagelen(skb);
Eric Dumazet3d130082010-09-21 08:47:45 +0000652 struct sk_buff *frag2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 if (first_len - hlen > mtu ||
655 ((first_len - hlen) & 7) ||
Florian Westphal1d325d22015-09-16 17:26:14 +0200656 skb_cloned(skb) ||
657 skb_headroom(skb) < (hroom + sizeof(struct frag_hdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 goto slow_path;
659
David S. Miller4d9092b2009-06-09 00:20:05 -0700660 skb_walk_frags(skb, frag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /* Correct geometry. */
662 if (frag->len > mtu ||
663 ((frag->len & 7) && frag->next) ||
Florian Westphal1d325d22015-09-16 17:26:14 +0200664 skb_headroom(frag) < (hlen + hroom + sizeof(struct frag_hdr)))
Eric Dumazet3d130082010-09-21 08:47:45 +0000665 goto slow_path_clean;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /* Partially cloned skb? */
668 if (skb_shared(frag))
Eric Dumazet3d130082010-09-21 08:47:45 +0000669 goto slow_path_clean;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700670
671 BUG_ON(frag->sk);
672 if (skb->sk) {
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700673 frag->sk = skb->sk;
674 frag->destructor = sock_wfree;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700675 }
Eric Dumazet3d130082010-09-21 08:47:45 +0000676 skb->truesize -= frag->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
679 err = 0;
680 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /* BUILD HEADER */
682
YOSHIFUJI Hideaki9a217a12006-12-05 13:47:21 -0800683 *prevhdr = NEXTHDR_FRAGMENT;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700684 tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (!tmp_hdr) {
Florian Westphal1d325d22015-09-16 17:26:14 +0200686 err = -ENOMEM;
687 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
Florian Westphal1d325d22015-09-16 17:26:14 +0200689 frag = skb_shinfo(skb)->frag_list;
690 skb_frag_list_init(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 __skb_pull(skb, hlen);
Ian Morris67ba4152014-08-24 21:53:10 +0100693 fh = (struct frag_hdr *)__skb_push(skb, sizeof(struct frag_hdr));
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700694 __skb_push(skb, hlen);
695 skb_reset_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700696 memcpy(skb_network_header(skb), tmp_hdr, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 fh->nexthdr = nexthdr;
699 fh->reserved = 0;
700 fh->frag_off = htons(IP6_MF);
Martin KaFai Lau286c2342015-05-22 20:55:56 -0700701 fh->identification = frag_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 first_len = skb_pagelen(skb);
704 skb->data_len = first_len - skb_headlen(skb);
705 skb->len = first_len;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700706 ipv6_hdr(skb)->payload_len = htons(first_len -
707 sizeof(struct ipv6hdr));
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900708
Changli Gaod8d1f302010-06-10 23:31:35 -0700709 dst_hold(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 for (;;) {
712 /* Prepare header of the next frame,
713 * before previous one went down. */
714 if (frag) {
715 frag->ip_summed = CHECKSUM_NONE;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300716 skb_reset_transport_header(frag);
Ian Morris67ba4152014-08-24 21:53:10 +0100717 fh = (struct frag_hdr *)__skb_push(frag, sizeof(struct frag_hdr));
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700718 __skb_push(frag, hlen);
719 skb_reset_network_header(frag);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700720 memcpy(skb_network_header(frag), tmp_hdr,
721 hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 offset += skb->len - hlen - sizeof(struct frag_hdr);
723 fh->nexthdr = nexthdr;
724 fh->reserved = 0;
725 fh->frag_off = htons(offset);
Ian Morris53b24b82015-03-29 14:00:05 +0100726 if (frag->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 fh->frag_off |= htons(IP6_MF);
728 fh->identification = frag_id;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700729 ipv6_hdr(frag)->payload_len =
730 htons(frag->len -
731 sizeof(struct ipv6hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 ip6_copy_metadata(frag, skb);
733 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900734
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500735 err = output(net, sk, skb);
Ian Morris67ba4152014-08-24 21:53:10 +0100736 if (!err)
Changli Gaod8d1f302010-06-10 23:31:35 -0700737 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700738 IPSTATS_MIB_FRAGCREATES);
Wei Dongdafee492006-08-02 13:41:21 -0700739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (err || !frag)
741 break;
742
743 skb = frag;
744 frag = skb->next;
745 skb->next = NULL;
746 }
747
Jesper Juhla51482b2005-11-08 09:41:34 -0800748 kfree(tmp_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 if (err == 0) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700751 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700752 IPSTATS_MIB_FRAGOKS);
Amerigo Wang94e187c2012-10-29 00:13:19 +0000753 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return 0;
755 }
756
Florian Westphal46cfd722014-09-10 01:08:46 +0200757 kfree_skb_list(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Changli Gaod8d1f302010-06-10 23:31:35 -0700759 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700760 IPSTATS_MIB_FRAGFAILS);
Amerigo Wang94e187c2012-10-29 00:13:19 +0000761 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return err;
Eric Dumazet3d130082010-09-21 08:47:45 +0000763
764slow_path_clean:
765 skb_walk_frags(skb, frag2) {
766 if (frag2 == frag)
767 break;
768 frag2->sk = NULL;
769 frag2->destructor = NULL;
770 skb->truesize += frag2->truesize;
771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773
774slow_path:
775 left = skb->len - hlen; /* Space per frame */
776 ptr = hlen; /* Where to start from */
777
778 /*
779 * Fragment the datagram.
780 */
781
Herbert Xua7ae1992011-11-18 02:20:04 +0000782 troom = rt->dst.dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 /*
785 * Keep copying data until we run out.
786 */
Ian Morris67ba4152014-08-24 21:53:10 +0100787 while (left > 0) {
Florian Westphal683100e2017-03-13 16:24:28 +0100788 u8 *fragnexthdr_offset;
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 len = left;
791 /* IF: it doesn't fit, use 'mtu' - the data space left */
792 if (len > mtu)
793 len = mtu;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300794 /* IF: we are not sending up to and including the packet end
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 then align the next start on an eight byte boundary */
796 if (len < left) {
797 len &= ~7;
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Joe Perchescbffccc2014-11-05 14:39:21 -0800800 /* Allocate buffer */
801 frag = alloc_skb(len + hlen + sizeof(struct frag_hdr) +
802 hroom + troom, GFP_ATOMIC);
803 if (!frag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 err = -ENOMEM;
805 goto fail;
806 }
807
808 /*
809 * Set up data on packet
810 */
811
812 ip6_copy_metadata(frag, skb);
Herbert Xua7ae1992011-11-18 02:20:04 +0000813 skb_reserve(frag, hroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 skb_put(frag, len + hlen + sizeof(struct frag_hdr));
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700815 skb_reset_network_header(frag);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300816 fh = (struct frag_hdr *)(skb_network_header(frag) + hlen);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700817 frag->transport_header = (frag->network_header + hlen +
818 sizeof(struct frag_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 /*
821 * Charge the memory for the fragment to any owner
822 * it might possess
823 */
824 if (skb->sk)
825 skb_set_owner_w(frag, skb->sk);
826
827 /*
828 * Copy the packet header into the new buffer.
829 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300830 skb_copy_from_linear_data(skb, skb_network_header(frag), hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Florian Westphal683100e2017-03-13 16:24:28 +0100832 fragnexthdr_offset = skb_network_header(frag);
833 fragnexthdr_offset += prevhdr - skb_network_header(skb);
834 *fragnexthdr_offset = NEXTHDR_FRAGMENT;
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /*
837 * Build fragment header.
838 */
839 fh->nexthdr = nexthdr;
840 fh->reserved = 0;
Martin KaFai Lau286c2342015-05-22 20:55:56 -0700841 fh->identification = frag_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 /*
844 * Copy a block of the IP datagram.
845 */
Himangi Saraogie3f0b862014-07-12 01:57:17 +0530846 BUG_ON(skb_copy_bits(skb, ptr, skb_transport_header(frag),
847 len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 left -= len;
849
850 fh->frag_off = htons(offset);
851 if (left > 0)
852 fh->frag_off |= htons(IP6_MF);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700853 ipv6_hdr(frag)->payload_len = htons(frag->len -
854 sizeof(struct ipv6hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 ptr += len;
857 offset += len;
858
859 /*
860 * Put this fragment into the sending queue.
861 */
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500862 err = output(net, sk, frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (err)
864 goto fail;
Wei Dongdafee492006-08-02 13:41:21 -0700865
Eric Dumazetadf30902009-06-02 05:19:30 +0000866 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700867 IPSTATS_MIB_FRAGCREATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000869 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900870 IPSTATS_MIB_FRAGOKS);
Eric Dumazet808db802012-04-24 10:17:59 +0000871 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return err;
873
Florian Westphal485fca62015-05-22 00:44:16 +0200874fail_toobig:
875 if (skb->sk && dst_allfrag(skb_dst(skb)))
876 sk_nocaps_add(skb->sk, NETIF_F_GSO_MASK);
877
Florian Westphal485fca62015-05-22 00:44:16 +0200878 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
879 err = -EMSGSIZE;
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881fail:
Eric Dumazetadf30902009-06-02 05:19:30 +0000882 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900883 IPSTATS_MIB_FRAGFAILS);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900884 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return err;
886}
887
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000888static inline int ip6_rt_check(const struct rt6key *rt_key,
889 const struct in6_addr *fl_addr,
890 const struct in6_addr *addr_cache)
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700891{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000892 return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
Ian Morris63159f22015-03-29 14:00:04 +0100893 (!addr_cache || !ipv6_addr_equal(fl_addr, addr_cache));
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700894}
895
Herbert Xu497c6152006-07-30 20:19:33 -0700896static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
897 struct dst_entry *dst,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000898 const struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Herbert Xu497c6152006-07-30 20:19:33 -0700900 struct ipv6_pinfo *np = inet6_sk(sk);
Eric Dumazeta963a372013-06-26 04:15:07 -0700901 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Herbert Xu497c6152006-07-30 20:19:33 -0700903 if (!dst)
904 goto out;
905
Eric Dumazeta963a372013-06-26 04:15:07 -0700906 if (dst->ops->family != AF_INET6) {
907 dst_release(dst);
908 return NULL;
909 }
910
911 rt = (struct rt6_info *)dst;
Herbert Xu497c6152006-07-30 20:19:33 -0700912 /* Yes, checking route validity in not connected
913 * case is not very simple. Take into account,
914 * that we do not support routing by source, TOS,
Ian Morris67ba4152014-08-24 21:53:10 +0100915 * and MSG_DONTROUTE --ANK (980726)
Herbert Xu497c6152006-07-30 20:19:33 -0700916 *
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700917 * 1. ip6_rt_check(): If route was host route,
918 * check that cached destination is current.
Herbert Xu497c6152006-07-30 20:19:33 -0700919 * If it is network route, we still may
920 * check its validity using saved pointer
921 * to the last used address: daddr_cache.
922 * We do not want to save whole address now,
923 * (because main consumer of this service
924 * is tcp, which has not this problem),
925 * so that the last trick works only on connected
926 * sockets.
927 * 2. oif also should be the same.
928 */
David S. Miller4c9483b2011-03-12 16:22:43 -0500929 if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr, np->daddr_cache) ||
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700930#ifdef CONFIG_IPV6_SUBTREES
David S. Miller4c9483b2011-03-12 16:22:43 -0500931 ip6_rt_check(&rt->rt6i_src, &fl6->saddr, np->saddr_cache) ||
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700932#endif
David Ahernca254492015-10-12 11:47:10 -0700933 (!(fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) &&
934 (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex))) {
Herbert Xu497c6152006-07-30 20:19:33 -0700935 dst_release(dst);
936 dst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
Herbert Xu497c6152006-07-30 20:19:33 -0700939out:
940 return dst;
941}
942
Eric Dumazet3aef9342015-09-25 07:39:12 -0700943static int ip6_dst_lookup_tail(struct net *net, const struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -0500944 struct dst_entry **dst, struct flowi6 *fl6)
Herbert Xu497c6152006-07-30 20:19:33 -0700945{
David S. Miller69cce1d2011-07-17 23:09:49 -0700946#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
947 struct neighbour *n;
David S. Miller97cac082012-07-02 22:43:47 -0700948 struct rt6_info *rt;
David S. Miller69cce1d2011-07-17 23:09:49 -0700949#endif
950 int err;
Paolo Abeni6f21c962016-01-29 12:30:19 +0100951 int flags = 0;
Herbert Xu497c6152006-07-30 20:19:33 -0700952
Markus Stenberge16e8882015-05-05 13:36:59 +0300953 /* The correct way to handle this would be to do
954 * ip6_route_get_saddr, and then ip6_route_output; however,
955 * the route-specific preferred source forces the
956 * ip6_route_output call _before_ ip6_route_get_saddr.
957 *
958 * In source specific routing (no src=any default route),
959 * ip6_route_output will fail given src=any saddr, though, so
960 * that's why we try it again later.
961 */
962 if (ipv6_addr_any(&fl6->saddr) && (!*dst || !(*dst)->error)) {
963 struct rt6_info *rt;
964 bool had_dst = *dst != NULL;
965
966 if (!had_dst)
967 *dst = ip6_route_output(net, sk, fl6);
968 rt = (*dst)->error ? NULL : (struct rt6_info *)*dst;
969 err = ip6_route_get_saddr(net, rt, &fl6->daddr,
970 sk ? inet6_sk(sk)->srcprefs : 0,
971 &fl6->saddr);
972 if (err)
973 goto out_err_release;
974
975 /* If we had an erroneous initial result, pretend it
976 * never existed and let the SA-enabled version take
977 * over.
978 */
979 if (!had_dst && (*dst)->error) {
980 dst_release(*dst);
981 *dst = NULL;
982 }
Paolo Abeni6f21c962016-01-29 12:30:19 +0100983
984 if (fl6->flowi6_oif)
985 flags |= RT6_LOOKUP_F_IFACE;
Markus Stenberge16e8882015-05-05 13:36:59 +0300986 }
987
Ian Morris63159f22015-03-29 14:00:04 +0100988 if (!*dst)
Paolo Abeni6f21c962016-01-29 12:30:19 +0100989 *dst = ip6_route_output_flags(net, sk, fl6, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Ian Morrise5d08d72014-11-23 21:28:43 +0000991 err = (*dst)->error;
992 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 goto out_err_release;
994
Neil Horman95c385b2007-04-25 17:08:10 -0700995#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Neil Hormane550dfb2008-09-09 13:51:35 -0700996 /*
997 * Here if the dst entry we've looked up
998 * has a neighbour entry that is in the INCOMPLETE
999 * state and the src address from the flow is
1000 * marked as OPTIMISTIC, we release the found
1001 * dst entry and replace it instead with the
1002 * dst entry of the nexthop router
1003 */
Eric Dumazetc56bf6f2012-07-06 09:19:05 +02001004 rt = (struct rt6_info *) *dst;
YOSHIFUJI Hideaki / 吉藤英明707be1f2013-01-17 12:53:55 +00001005 rcu_read_lock_bh();
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -07001006 n = __ipv6_neigh_lookup_noref(rt->dst.dev,
1007 rt6_nexthop(rt, &fl6->daddr));
YOSHIFUJI Hideaki / 吉藤英明707be1f2013-01-17 12:53:55 +00001008 err = n && !(n->nud_state & NUD_VALID) ? -EINVAL : 0;
1009 rcu_read_unlock_bh();
1010
1011 if (err) {
Neil Hormane550dfb2008-09-09 13:51:35 -07001012 struct inet6_ifaddr *ifp;
David S. Miller4c9483b2011-03-12 16:22:43 -05001013 struct flowi6 fl_gw6;
Neil Hormane550dfb2008-09-09 13:51:35 -07001014 int redirect;
Neil Horman95c385b2007-04-25 17:08:10 -07001015
David S. Miller4c9483b2011-03-12 16:22:43 -05001016 ifp = ipv6_get_ifaddr(net, &fl6->saddr,
Neil Hormane550dfb2008-09-09 13:51:35 -07001017 (*dst)->dev, 1);
Neil Horman95c385b2007-04-25 17:08:10 -07001018
Neil Hormane550dfb2008-09-09 13:51:35 -07001019 redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC);
1020 if (ifp)
1021 in6_ifa_put(ifp);
Neil Horman95c385b2007-04-25 17:08:10 -07001022
Neil Hormane550dfb2008-09-09 13:51:35 -07001023 if (redirect) {
1024 /*
1025 * We need to get the dst entry for the
1026 * default router instead
1027 */
1028 dst_release(*dst);
David S. Miller4c9483b2011-03-12 16:22:43 -05001029 memcpy(&fl_gw6, fl6, sizeof(struct flowi6));
1030 memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr));
1031 *dst = ip6_route_output(net, sk, &fl_gw6);
Ian Morrise5d08d72014-11-23 21:28:43 +00001032 err = (*dst)->error;
1033 if (err)
Neil Hormane550dfb2008-09-09 13:51:35 -07001034 goto out_err_release;
Neil Horman95c385b2007-04-25 17:08:10 -07001035 }
Neil Hormane550dfb2008-09-09 13:51:35 -07001036 }
Neil Horman95c385b2007-04-25 17:08:10 -07001037#endif
Jonathan T. Leightoncb351da2017-05-23 21:53:34 -04001038 if (ipv6_addr_v4mapped(&fl6->saddr) &&
Willem de Bruijn98184bb2017-02-18 19:00:45 -05001039 !(ipv6_addr_v4mapped(&fl6->daddr) || ipv6_addr_any(&fl6->daddr))) {
1040 err = -EAFNOSUPPORT;
1041 goto out_err_release;
1042 }
Neil Horman95c385b2007-04-25 17:08:10 -07001043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return 0;
1045
1046out_err_release:
1047 dst_release(*dst);
1048 *dst = NULL;
David Ahern8a966fc2016-09-10 12:09:59 -07001049
David Ahern0d240e72016-06-16 16:24:25 -07001050 if (err == -ENETUNREACH)
1051 IP6_INC_STATS(net, NULL, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return err;
1053}
Adrian Bunk34a0b3c2005-11-29 16:28:56 -08001054
Herbert Xu497c6152006-07-30 20:19:33 -07001055/**
1056 * ip6_dst_lookup - perform route lookup on flow
1057 * @sk: socket which provides route info
1058 * @dst: pointer to dst_entry * for result
David S. Miller4c9483b2011-03-12 16:22:43 -05001059 * @fl6: flow to lookup
Herbert Xu497c6152006-07-30 20:19:33 -07001060 *
1061 * This function performs a route lookup on the given flow.
1062 *
1063 * It returns zero on success, or a standard errno code on error.
1064 */
Roopa Prabhu343d60a2015-07-30 13:34:53 -07001065int ip6_dst_lookup(struct net *net, struct sock *sk, struct dst_entry **dst,
1066 struct flowi6 *fl6)
Herbert Xu497c6152006-07-30 20:19:33 -07001067{
1068 *dst = NULL;
Roopa Prabhu343d60a2015-07-30 13:34:53 -07001069 return ip6_dst_lookup_tail(net, sk, dst, fl6);
Herbert Xu497c6152006-07-30 20:19:33 -07001070}
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -08001071EXPORT_SYMBOL_GPL(ip6_dst_lookup);
1072
Herbert Xu497c6152006-07-30 20:19:33 -07001073/**
David S. Miller68d0c6d2011-03-01 13:19:07 -08001074 * ip6_dst_lookup_flow - perform route lookup on flow with ipsec
1075 * @sk: socket which provides route info
David S. Miller4c9483b2011-03-12 16:22:43 -05001076 * @fl6: flow to lookup
David S. Miller68d0c6d2011-03-01 13:19:07 -08001077 * @final_dst: final destination address for ipsec lookup
David S. Miller68d0c6d2011-03-01 13:19:07 -08001078 *
1079 * This function performs a route lookup on the given flow.
1080 *
1081 * It returns a valid dst pointer on success, or a pointer encoded
1082 * error code.
1083 */
Eric Dumazet3aef9342015-09-25 07:39:12 -07001084struct dst_entry *ip6_dst_lookup_flow(const struct sock *sk, struct flowi6 *fl6,
Steffen Klassert0e0d44a2013-08-28 08:04:14 +02001085 const struct in6_addr *final_dst)
David S. Miller68d0c6d2011-03-01 13:19:07 -08001086{
1087 struct dst_entry *dst = NULL;
1088 int err;
1089
Roopa Prabhu343d60a2015-07-30 13:34:53 -07001090 err = ip6_dst_lookup_tail(sock_net(sk), sk, &dst, fl6);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001091 if (err)
1092 return ERR_PTR(err);
1093 if (final_dst)
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001094 fl6->daddr = *final_dst;
David S. Miller2774c132011-03-01 14:59:04 -08001095
Steffen Klassertf92ee612014-09-16 10:08:40 +02001096 return xfrm_lookup_route(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001097}
1098EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow);
1099
1100/**
1101 * ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow
1102 * @sk: socket which provides the dst cache and route info
David S. Miller4c9483b2011-03-12 16:22:43 -05001103 * @fl6: flow to lookup
David S. Miller68d0c6d2011-03-01 13:19:07 -08001104 * @final_dst: final destination address for ipsec lookup
Herbert Xu497c6152006-07-30 20:19:33 -07001105 *
1106 * This function performs a route lookup on the given flow with the
1107 * possibility of using the cached route in the socket if it is valid.
1108 * It will take the socket dst lock when operating on the dst cache.
1109 * As a result, this function can only be used in process context.
1110 *
David S. Miller68d0c6d2011-03-01 13:19:07 -08001111 * It returns a valid dst pointer on success, or a pointer encoded
1112 * error code.
Herbert Xu497c6152006-07-30 20:19:33 -07001113 */
David S. Miller4c9483b2011-03-12 16:22:43 -05001114struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
Steffen Klassert0e0d44a2013-08-28 08:04:14 +02001115 const struct in6_addr *final_dst)
Herbert Xu497c6152006-07-30 20:19:33 -07001116{
David S. Miller68d0c6d2011-03-01 13:19:07 -08001117 struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
Herbert Xu497c6152006-07-30 20:19:33 -07001118
David S. Miller4c9483b2011-03-12 16:22:43 -05001119 dst = ip6_sk_dst_check(sk, dst, fl6);
Jakub Sitnicki00bc0ef2016-06-08 15:13:34 +02001120 if (!dst)
1121 dst = ip6_dst_lookup_flow(sk, fl6, final_dst);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001122
Jakub Sitnicki00bc0ef2016-06-08 15:13:34 +02001123 return dst;
Herbert Xu497c6152006-07-30 20:19:33 -07001124}
David S. Miller68d0c6d2011-03-01 13:19:07 -08001125EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow);
Herbert Xu497c6152006-07-30 20:19:33 -07001126
Adrian Bunk34a0b3c2005-11-29 16:28:56 -08001127static inline int ip6_ufo_append_data(struct sock *sk,
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001128 struct sk_buff_head *queue,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001129 int getfrag(void *from, char *to, int offset, int len,
1130 int odd, struct sk_buff *skb),
1131 void *from, int length, int hh_len, int fragheaderlen,
Jakub Sitnicki3ba34582016-04-05 18:41:08 +02001132 int exthdrlen, int transhdrlen, int mtu,
1133 unsigned int flags, const struct flowi6 *fl6)
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001134
1135{
1136 struct sk_buff *skb;
1137 int err;
1138
1139 /* There is support for UDP large send offload by network
1140 * device, so create one single skb packet containing complete
1141 * udp datagram
1142 */
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001143 skb = skb_peek_tail(queue);
Ian Morris63159f22015-03-29 14:00:04 +01001144 if (!skb) {
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001145 skb = sock_alloc_send_skb(sk,
1146 hh_len + fragheaderlen + transhdrlen + 20,
1147 (flags & MSG_DONTWAIT), &err);
Ian Morris63159f22015-03-29 14:00:04 +01001148 if (!skb)
Zheng Yan504744e2011-10-28 00:26:00 -04001149 return err;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001150
1151 /* reserve space for Hardware header */
1152 skb_reserve(skb, hh_len);
1153
1154 /* create space for UDP/IP header */
Ian Morris67ba4152014-08-24 21:53:10 +01001155 skb_put(skb, fragheaderlen + transhdrlen);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001156
1157 /* initialize network header pointer */
Jakub Sitnicki3ba34582016-04-05 18:41:08 +02001158 skb_set_network_header(skb, exthdrlen);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001159
1160 /* initialize protocol header pointer */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001161 skb->transport_header = skb->network_header + fragheaderlen;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001162
Hannes Frederic Sowa9c9c9ad2013-08-26 12:31:23 +02001163 skb->protocol = htons(ETH_P_IPV6);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001164 skb->csum = 0;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001165
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001166 __skb_queue_tail(queue, skb);
Jiri Pirkoc547dbf2013-10-19 12:29:16 +02001167 } else if (skb_is_gso(skb)) {
1168 goto append;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001169 }
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001170
Jiri Pirkoc547dbf2013-10-19 12:29:16 +02001171 skb->ip_summed = CHECKSUM_PARTIAL;
1172 /* Specify the length of each IPv6 datagram fragment.
1173 * It has to be a multiple of 8.
1174 */
1175 skb_shinfo(skb)->gso_size = (mtu - fragheaderlen -
1176 sizeof(struct frag_hdr)) & ~7;
1177 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
Martin KaFai Laufd0273d2015-05-22 20:55:57 -07001178 skb_shinfo(skb)->ip6_frag_id = ipv6_select_ident(sock_net(sk),
1179 &fl6->daddr,
1180 &fl6->saddr);
Jiri Pirkoc547dbf2013-10-19 12:29:16 +02001181
1182append:
Hannes Frederic Sowa2811eba2013-09-21 06:27:00 +02001183 return skb_append_datato_frags(sk, skb, getfrag, from,
1184 (length - transhdrlen));
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001185}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Herbert Xu0178b692009-02-05 15:15:50 -08001187static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src,
1188 gfp_t gfp)
1189{
1190 return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
1191}
1192
1193static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src,
1194 gfp_t gfp)
1195{
1196 return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
1197}
1198
Hannes Frederic Sowa75a493e2013-07-02 08:04:05 +02001199static void ip6_append_data_mtu(unsigned int *mtu,
Gao feng0c183372012-05-26 01:30:53 +00001200 int *maxfraglen,
1201 unsigned int fragheaderlen,
1202 struct sk_buff *skb,
Hannes Frederic Sowa75a493e2013-07-02 08:04:05 +02001203 struct rt6_info *rt,
luciene367c2d2014-03-17 12:51:01 +08001204 unsigned int orig_mtu)
Gao feng0c183372012-05-26 01:30:53 +00001205{
1206 if (!(rt->dst.flags & DST_XFRM_TUNNEL)) {
Ian Morris63159f22015-03-29 14:00:04 +01001207 if (!skb) {
Gao feng0c183372012-05-26 01:30:53 +00001208 /* first fragment, reserve header_len */
luciene367c2d2014-03-17 12:51:01 +08001209 *mtu = orig_mtu - rt->dst.header_len;
Gao feng0c183372012-05-26 01:30:53 +00001210
1211 } else {
1212 /*
1213 * this fragment is not first, the headers
1214 * space is regarded as data space.
1215 */
luciene367c2d2014-03-17 12:51:01 +08001216 *mtu = orig_mtu;
Gao feng0c183372012-05-26 01:30:53 +00001217 }
1218 *maxfraglen = ((*mtu - fragheaderlen) & ~7)
1219 + fragheaderlen - sizeof(struct frag_hdr);
1220 }
1221}
1222
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001223static int ip6_setup_cork(struct sock *sk, struct inet_cork_full *cork,
Wei Wang26879da2016-05-02 21:40:07 -07001224 struct inet6_cork *v6_cork, struct ipcm6_cookie *ipc6,
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001225 struct rt6_info *rt, struct flowi6 *fl6)
1226{
1227 struct ipv6_pinfo *np = inet6_sk(sk);
1228 unsigned int mtu;
Wei Wang26879da2016-05-02 21:40:07 -07001229 struct ipv6_txoptions *opt = ipc6->opt;
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001230
1231 /*
1232 * setup for corking
1233 */
1234 if (opt) {
1235 if (WARN_ON(v6_cork->opt))
1236 return -EINVAL;
1237
Eric Dumazet28fa5832017-10-21 12:26:23 -07001238 v6_cork->opt = kzalloc(sizeof(*opt), sk->sk_allocation);
Ian Morris63159f22015-03-29 14:00:04 +01001239 if (unlikely(!v6_cork->opt))
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001240 return -ENOBUFS;
1241
Eric Dumazet28fa5832017-10-21 12:26:23 -07001242 v6_cork->opt->tot_len = sizeof(*opt);
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001243 v6_cork->opt->opt_flen = opt->opt_flen;
1244 v6_cork->opt->opt_nflen = opt->opt_nflen;
1245
1246 v6_cork->opt->dst0opt = ip6_opt_dup(opt->dst0opt,
1247 sk->sk_allocation);
1248 if (opt->dst0opt && !v6_cork->opt->dst0opt)
1249 return -ENOBUFS;
1250
1251 v6_cork->opt->dst1opt = ip6_opt_dup(opt->dst1opt,
1252 sk->sk_allocation);
1253 if (opt->dst1opt && !v6_cork->opt->dst1opt)
1254 return -ENOBUFS;
1255
1256 v6_cork->opt->hopopt = ip6_opt_dup(opt->hopopt,
1257 sk->sk_allocation);
1258 if (opt->hopopt && !v6_cork->opt->hopopt)
1259 return -ENOBUFS;
1260
1261 v6_cork->opt->srcrt = ip6_rthdr_dup(opt->srcrt,
1262 sk->sk_allocation);
1263 if (opt->srcrt && !v6_cork->opt->srcrt)
1264 return -ENOBUFS;
1265
1266 /* need source address above miyazawa*/
1267 }
1268 dst_hold(&rt->dst);
1269 cork->base.dst = &rt->dst;
1270 cork->fl.u.ip6 = *fl6;
Wei Wang26879da2016-05-02 21:40:07 -07001271 v6_cork->hop_limit = ipc6->hlimit;
1272 v6_cork->tclass = ipc6->tclass;
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001273 if (rt->dst.flags & DST_XFRM_TUNNEL)
1274 mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ?
Mike Maloneyfb50d8c2018-01-10 12:45:10 -05001275 READ_ONCE(rt->dst.dev->mtu) : dst_mtu(&rt->dst);
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001276 else
1277 mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ?
Mike Maloneyfb50d8c2018-01-10 12:45:10 -05001278 READ_ONCE(rt->dst.dev->mtu) : dst_mtu(rt->dst.path);
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001279 if (np->frag_size < mtu) {
1280 if (np->frag_size)
1281 mtu = np->frag_size;
1282 }
Mike Maloneyfb50d8c2018-01-10 12:45:10 -05001283 if (mtu < IPV6_MIN_MTU)
1284 return -EINVAL;
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001285 cork->base.fragsize = mtu;
1286 if (dst_allfrag(rt->dst.path))
1287 cork->base.flags |= IPCORK_ALLFRAG;
1288 cork->base.length = 0;
1289
1290 return 0;
1291}
1292
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001293static int __ip6_append_data(struct sock *sk,
1294 struct flowi6 *fl6,
1295 struct sk_buff_head *queue,
1296 struct inet_cork *cork,
1297 struct inet6_cork *v6_cork,
1298 struct page_frag *pfrag,
1299 int getfrag(void *from, char *to, int offset,
1300 int len, int odd, struct sk_buff *skb),
1301 void *from, int length, int transhdrlen,
Wei Wang26879da2016-05-02 21:40:07 -07001302 unsigned int flags, struct ipcm6_cookie *ipc6,
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001303 const struct sockcm_cookie *sockc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304{
Gao feng0c183372012-05-26 01:30:53 +00001305 struct sk_buff *skb, *skb_prev = NULL;
Paolo Abeni81eb03e2018-03-23 14:47:30 +01001306 unsigned int maxfraglen, fragheaderlen, mtu, orig_mtu, pmtu;
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001307 int exthdrlen = 0;
1308 int dst_exthdrlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 int hh_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 int copy;
1311 int err;
1312 int offset = 0;
Anders Berggrena693e692011-02-28 12:32:11 -08001313 __u8 tx_flags = 0;
Willem de Bruijn09c2d252014-08-04 22:11:47 -04001314 u32 tskey = 0;
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001315 struct rt6_info *rt = (struct rt6_info *)cork->dst;
1316 struct ipv6_txoptions *opt = v6_cork->opt;
Vlad Yasevich32dce962015-01-31 10:40:18 -05001317 int csummode = CHECKSUM_NONE;
Hannes Frederic Sowa682b1a92015-10-27 22:40:41 +01001318 unsigned int maxnonfragsize, headersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001320 skb = skb_peek_tail(queue);
1321 if (!skb) {
1322 exthdrlen = opt ? opt->opt_flen : 0;
Romain KUNTZ7efdba52013-01-16 12:47:40 +00001323 dst_exthdrlen = rt->dst.header_len - rt->rt6i_nfheader_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001325
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001326 mtu = cork->fragsize;
luciene367c2d2014-03-17 12:51:01 +08001327 orig_mtu = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Changli Gaod8d1f302010-06-10 23:31:35 -07001329 hh_len = LL_RESERVED_SPACE(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001331 fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len +
Herbert Xub4ce9272007-11-13 21:33:32 -08001332 (opt ? opt->opt_nflen : 0);
Hannes Frederic Sowa4df98e72013-12-16 12:36:44 +01001333 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen -
1334 sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Hannes Frederic Sowa682b1a92015-10-27 22:40:41 +01001336 headersize = sizeof(struct ipv6hdr) +
1337 (opt ? opt->opt_flen + opt->opt_nflen : 0) +
1338 (dst_allfrag(&rt->dst) ?
1339 sizeof(struct frag_hdr) : 0) +
1340 rt->rt6i_nfheader_len;
Hannes Frederic Sowa4df98e72013-12-16 12:36:44 +01001341
Paolo Abeni81eb03e2018-03-23 14:47:30 +01001342 /* as per RFC 7112 section 5, the entire IPv6 Header Chain must fit
1343 * the first fragment
1344 */
1345 if (headersize + transhdrlen > mtu)
1346 goto emsgsize;
1347
Wei Wang26879da2016-05-02 21:40:07 -07001348 if (cork->length + length > mtu - headersize && ipc6->dontfrag &&
Hannes Frederic Sowa682b1a92015-10-27 22:40:41 +01001349 (sk->sk_protocol == IPPROTO_UDP ||
1350 sk->sk_protocol == IPPROTO_RAW)) {
1351 ipv6_local_rxpmtu(sk, fl6, mtu - headersize +
1352 sizeof(struct ipv6hdr));
1353 goto emsgsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355
Hannes Frederic Sowa682b1a92015-10-27 22:40:41 +01001356 if (ip6_sk_ignore_df(sk))
1357 maxnonfragsize = sizeof(struct ipv6hdr) + IPV6_MAXPLEN;
1358 else
1359 maxnonfragsize = mtu;
1360
1361 if (cork->length + length > maxnonfragsize - headersize) {
1362emsgsize:
Paolo Abeni81eb03e2018-03-23 14:47:30 +01001363 pmtu = max_t(int, mtu - headersize + sizeof(struct ipv6hdr), 0);
1364 ipv6_local_error(sk, EMSGSIZE, fl6, pmtu);
Hannes Frederic Sowa682b1a92015-10-27 22:40:41 +01001365 return -EMSGSIZE;
1366 }
1367
1368 /* CHECKSUM_PARTIAL only with no extension headers and when
1369 * we are not going to fragment
1370 */
1371 if (transhdrlen && sk->sk_protocol == IPPROTO_UDP &&
1372 headersize == sizeof(struct ipv6hdr) &&
1373 length < mtu - headersize &&
1374 !(flags & MSG_MORE) &&
Tom Herbertc8cd0982015-12-14 11:19:44 -08001375 rt->dst.dev->features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
Hannes Frederic Sowa682b1a92015-10-27 22:40:41 +01001376 csummode = CHECKSUM_PARTIAL;
1377
Willem de Bruijn09c2d252014-08-04 22:11:47 -04001378 if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_RAW) {
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001379 sock_tx_timestamp(sk, sockc->tsflags, &tx_flags);
Willem de Bruijn09c2d252014-08-04 22:11:47 -04001380 if (tx_flags & SKBTX_ANY_SW_TSTAMP &&
1381 sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)
1382 tskey = sk->sk_tskey++;
1383 }
Anders Berggrena693e692011-02-28 12:32:11 -08001384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 /*
1386 * Let's try using as much space as possible.
1387 * Use MTU if total length of the message fits into the MTU.
1388 * Otherwise, we need to reserve fragment header and
1389 * fragment alignment (= 8-15 octects, in total).
1390 *
1391 * Note that we may need to "move" the data from the tail of
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001392 * of the buffer to the new fragment when we split
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 * the message.
1394 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001395 * FIXME: It may be fragmented into multiple chunks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 * at once if non-fragmentable extension headers
1397 * are too large.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001398 * --yoshfuji
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 */
1400
Hannes Frederic Sowa2811eba2013-09-21 06:27:00 +02001401 cork->length += length;
Willem de Bruijn33dc6a62017-08-10 12:29:19 -04001402 if ((skb && skb_is_gso(skb)) ||
1403 (((length + fragheaderlen) > mtu) &&
1404 (skb_queue_len(queue) <= 1) &&
Hannes Frederic Sowa2811eba2013-09-21 06:27:00 +02001405 (sk->sk_protocol == IPPROTO_UDP) &&
Jakub Sitnickif89c56c2016-10-26 11:21:14 +02001406 (rt->dst.dev->features & NETIF_F_UFO) && !rt->dst.header_len &&
Willem de Bruijn33dc6a62017-08-10 12:29:19 -04001407 (sk->sk_type == SOCK_DGRAM) && !udp_get_no_check6_tx(sk))) {
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001408 err = ip6_ufo_append_data(sk, queue, getfrag, from, length,
Jakub Sitnicki3ba34582016-04-05 18:41:08 +02001409 hh_len, fragheaderlen, exthdrlen,
Martin KaFai Laufd0273d2015-05-22 20:55:57 -07001410 transhdrlen, mtu, flags, fl6);
Hannes Frederic Sowa2811eba2013-09-21 06:27:00 +02001411 if (err)
1412 goto error;
1413 return 0;
1414 }
1415
1416 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 goto alloc_new_skb;
1418
1419 while (length > 0) {
1420 /* Check if the remaining data fits into current packet. */
David S. Millerbdc712b2011-05-06 15:02:07 -07001421 copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (copy < length)
1423 copy = maxfraglen - skb->len;
1424
1425 if (copy <= 0) {
1426 char *data;
1427 unsigned int datalen;
1428 unsigned int fraglen;
1429 unsigned int fraggap;
1430 unsigned int alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431alloc_new_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /* There's no room in the current skb */
Gao feng0c183372012-05-26 01:30:53 +00001433 if (skb)
1434 fraggap = skb->len - maxfraglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 else
1436 fraggap = 0;
Gao feng0c183372012-05-26 01:30:53 +00001437 /* update mtu and maxfraglen if necessary */
Ian Morris63159f22015-03-29 14:00:04 +01001438 if (!skb || !skb_prev)
Gao feng0c183372012-05-26 01:30:53 +00001439 ip6_append_data_mtu(&mtu, &maxfraglen,
Hannes Frederic Sowa75a493e2013-07-02 08:04:05 +02001440 fragheaderlen, skb, rt,
luciene367c2d2014-03-17 12:51:01 +08001441 orig_mtu);
Gao feng0c183372012-05-26 01:30:53 +00001442
1443 skb_prev = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 /*
1446 * If remaining data exceeds the mtu,
1447 * we know we need more fragment(s).
1448 */
1449 datalen = length + fraggap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Gao feng0c183372012-05-26 01:30:53 +00001451 if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
1452 datalen = maxfraglen - fragheaderlen - rt->dst.trailer_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if ((flags & MSG_MORE) &&
Changli Gaod8d1f302010-06-10 23:31:35 -07001454 !(rt->dst.dev->features&NETIF_F_SG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 alloclen = mtu;
1456 else
1457 alloclen = datalen + fragheaderlen;
1458
Steffen Klassert299b0762011-10-11 01:43:33 +00001459 alloclen += dst_exthdrlen;
1460
Gao feng0c183372012-05-26 01:30:53 +00001461 if (datalen != length + fraggap) {
1462 /*
1463 * this is not the last fragment, the trailer
1464 * space is regarded as data space.
1465 */
1466 datalen += rt->dst.trailer_len;
1467 }
1468
1469 alloclen += rt->dst.trailer_len;
1470 fraglen = datalen + fragheaderlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 /*
1473 * We just reserve space for fragment header.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001474 * Note: this may be overallocation if the message
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 * (without MSG_MORE) fits into the MTU.
1476 */
1477 alloclen += sizeof(struct frag_hdr);
1478
Eric Dumazet304b4102017-05-19 14:17:48 -07001479 copy = datalen - transhdrlen - fraggap;
1480 if (copy < 0) {
1481 err = -EINVAL;
1482 goto error;
1483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (transhdrlen) {
1485 skb = sock_alloc_send_skb(sk,
1486 alloclen + hh_len,
1487 (flags & MSG_DONTWAIT), &err);
1488 } else {
1489 skb = NULL;
1490 if (atomic_read(&sk->sk_wmem_alloc) <=
1491 2 * sk->sk_sndbuf)
1492 skb = sock_wmalloc(sk,
1493 alloclen + hh_len, 1,
1494 sk->sk_allocation);
Ian Morris63159f22015-03-29 14:00:04 +01001495 if (unlikely(!skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 err = -ENOBUFS;
1497 }
Ian Morris63159f22015-03-29 14:00:04 +01001498 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 goto error;
1500 /*
1501 * Fill in the control structures
1502 */
Hannes Frederic Sowa9c9c9ad2013-08-26 12:31:23 +02001503 skb->protocol = htons(ETH_P_IPV6);
Vlad Yasevich32dce962015-01-31 10:40:18 -05001504 skb->ip_summed = csummode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 skb->csum = 0;
Gao feng1f858512012-03-19 22:36:10 +00001506 /* reserve for fragmentation and ipsec header */
1507 skb_reserve(skb, hh_len + sizeof(struct frag_hdr) +
1508 dst_exthdrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Willem de Bruijn11878b42014-07-14 17:55:06 -04001510 /* Only the initial fragment is time stamped */
1511 skb_shinfo(skb)->tx_flags = tx_flags;
1512 tx_flags = 0;
Willem de Bruijn09c2d252014-08-04 22:11:47 -04001513 skb_shinfo(skb)->tskey = tskey;
1514 tskey = 0;
Anders Berggrena693e692011-02-28 12:32:11 -08001515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 /*
1517 * Find where to start putting bytes
1518 */
Gao feng1f858512012-03-19 22:36:10 +00001519 data = skb_put(skb, fraglen);
1520 skb_set_network_header(skb, exthdrlen);
1521 data += fragheaderlen;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001522 skb->transport_header = (skb->network_header +
1523 fragheaderlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (fraggap) {
1525 skb->csum = skb_copy_and_csum_bits(
1526 skb_prev, maxfraglen,
1527 data + transhdrlen, fraggap, 0);
1528 skb_prev->csum = csum_sub(skb_prev->csum,
1529 skb->csum);
1530 data += fraggap;
Herbert Xue9fa4f72006-08-13 20:12:58 -07001531 pskb_trim_unique(skb_prev, maxfraglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
Eric Dumazet304b4102017-05-19 14:17:48 -07001533 if (copy > 0 &&
1534 getfrag(from, data + transhdrlen, offset,
1535 copy, fraggap, skb) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 err = -EFAULT;
1537 kfree_skb(skb);
1538 goto error;
1539 }
1540
1541 offset += copy;
1542 length -= datalen - fraggap;
1543 transhdrlen = 0;
1544 exthdrlen = 0;
Steffen Klassert299b0762011-10-11 01:43:33 +00001545 dst_exthdrlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 /*
1548 * Put the packet on the pending queue
1549 */
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001550 __skb_queue_tail(queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 continue;
1552 }
1553
1554 if (copy > length)
1555 copy = length;
1556
Willem de Bruijn2ef22bd2018-05-17 13:13:29 -04001557 if (!(rt->dst.dev->features&NETIF_F_SG) &&
1558 skb_tailroom(skb) >= copy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 unsigned int off;
1560
1561 off = skb->len;
1562 if (getfrag(from, skb_put(skb, copy),
1563 offset, copy, off, skb) < 0) {
1564 __skb_trim(skb, off);
1565 err = -EFAULT;
1566 goto error;
1567 }
1568 } else {
1569 int i = skb_shinfo(skb)->nr_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Eric Dumazet5640f762012-09-23 23:04:42 +00001571 err = -ENOMEM;
1572 if (!sk_page_frag_refill(sk, pfrag))
1573 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Eric Dumazet5640f762012-09-23 23:04:42 +00001575 if (!skb_can_coalesce(skb, i, pfrag->page,
1576 pfrag->offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 err = -EMSGSIZE;
Eric Dumazet5640f762012-09-23 23:04:42 +00001578 if (i == MAX_SKB_FRAGS)
1579 goto error;
1580
1581 __skb_fill_page_desc(skb, i, pfrag->page,
1582 pfrag->offset, 0);
1583 skb_shinfo(skb)->nr_frags = ++i;
1584 get_page(pfrag->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
Eric Dumazet5640f762012-09-23 23:04:42 +00001586 copy = min_t(int, copy, pfrag->size - pfrag->offset);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001587 if (getfrag(from,
Eric Dumazet5640f762012-09-23 23:04:42 +00001588 page_address(pfrag->page) + pfrag->offset,
1589 offset, copy, skb->len, skb) < 0)
1590 goto error_efault;
1591
1592 pfrag->offset += copy;
1593 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 skb->len += copy;
1595 skb->data_len += copy;
Herbert Xuf945fa72008-01-22 22:39:26 -08001596 skb->truesize += copy;
1597 atomic_add(copy, &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599 offset += copy;
1600 length -= copy;
1601 }
Eric Dumazet5640f762012-09-23 23:04:42 +00001602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return 0;
Eric Dumazet5640f762012-09-23 23:04:42 +00001604
1605error_efault:
1606 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607error:
David S. Millerbdc712b2011-05-06 15:02:07 -07001608 cork->length -= length;
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001609 IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return err;
1611}
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001612
1613int ip6_append_data(struct sock *sk,
1614 int getfrag(void *from, char *to, int offset, int len,
1615 int odd, struct sk_buff *skb),
Wei Wang26879da2016-05-02 21:40:07 -07001616 void *from, int length, int transhdrlen,
1617 struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
1618 struct rt6_info *rt, unsigned int flags,
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001619 const struct sockcm_cookie *sockc)
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001620{
1621 struct inet_sock *inet = inet_sk(sk);
1622 struct ipv6_pinfo *np = inet6_sk(sk);
1623 int exthdrlen;
1624 int err;
1625
1626 if (flags&MSG_PROBE)
1627 return 0;
1628 if (skb_queue_empty(&sk->sk_write_queue)) {
1629 /*
1630 * setup for corking
1631 */
Wei Wang26879da2016-05-02 21:40:07 -07001632 err = ip6_setup_cork(sk, &inet->cork, &np->cork,
1633 ipc6, rt, fl6);
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001634 if (err)
1635 return err;
1636
Wei Wang26879da2016-05-02 21:40:07 -07001637 exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0);
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001638 length += exthdrlen;
1639 transhdrlen += exthdrlen;
1640 } else {
1641 fl6 = &inet->cork.fl.u.ip6;
1642 transhdrlen = 0;
1643 }
1644
1645 return __ip6_append_data(sk, fl6, &sk->sk_write_queue, &inet->cork.base,
1646 &np->cork, sk_page_frag(sk), getfrag,
Wei Wang26879da2016-05-02 21:40:07 -07001647 from, length, transhdrlen, flags, ipc6, sockc);
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001648}
Chris Elstona495f832012-04-29 21:48:53 +00001649EXPORT_SYMBOL_GPL(ip6_append_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001651static void ip6_cork_release(struct inet_cork_full *cork,
1652 struct inet6_cork *v6_cork)
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001653{
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001654 if (v6_cork->opt) {
1655 kfree(v6_cork->opt->dst0opt);
1656 kfree(v6_cork->opt->dst1opt);
1657 kfree(v6_cork->opt->hopopt);
1658 kfree(v6_cork->opt->srcrt);
1659 kfree(v6_cork->opt);
1660 v6_cork->opt = NULL;
Herbert Xu0178b692009-02-05 15:15:50 -08001661 }
1662
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001663 if (cork->base.dst) {
1664 dst_release(cork->base.dst);
1665 cork->base.dst = NULL;
1666 cork->base.flags &= ~IPCORK_ALLFRAG;
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001667 }
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001668 memset(&cork->fl, 0, sizeof(cork->fl));
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001669}
1670
Vlad Yasevich64223982015-01-31 10:40:15 -05001671struct sk_buff *__ip6_make_skb(struct sock *sk,
1672 struct sk_buff_head *queue,
1673 struct inet_cork_full *cork,
1674 struct inet6_cork *v6_cork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
1676 struct sk_buff *skb, *tmp_skb;
1677 struct sk_buff **tail_skb;
1678 struct in6_addr final_dst_buf, *final_dst = &final_dst_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 struct ipv6_pinfo *np = inet6_sk(sk);
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001680 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 struct ipv6hdr *hdr;
Vlad Yasevich64223982015-01-31 10:40:15 -05001682 struct ipv6_txoptions *opt = v6_cork->opt;
1683 struct rt6_info *rt = (struct rt6_info *)cork->base.dst;
1684 struct flowi6 *fl6 = &cork->fl.u.ip6;
David S. Miller4c9483b2011-03-12 16:22:43 -05001685 unsigned char proto = fl6->flowi6_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Vlad Yasevich64223982015-01-31 10:40:15 -05001687 skb = __skb_dequeue(queue);
Ian Morris63159f22015-03-29 14:00:04 +01001688 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 goto out;
1690 tail_skb = &(skb_shinfo(skb)->frag_list);
1691
1692 /* move skb->data to ip header from ext header */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001693 if (skb->data < skb_network_header(skb))
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001694 __skb_pull(skb, skb_network_offset(skb));
Vlad Yasevich64223982015-01-31 10:40:15 -05001695 while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001696 __skb_pull(tmp_skb, skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 *tail_skb = tmp_skb;
1698 tail_skb = &(tmp_skb->next);
1699 skb->len += tmp_skb->len;
1700 skb->data_len += tmp_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 skb->truesize += tmp_skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 tmp_skb->destructor = NULL;
1703 tmp_skb->sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
1705
Herbert Xu28a894532008-02-12 18:07:27 -08001706 /* Allow local fragmentation. */
WANG Cong60ff7462014-05-04 16:39:18 -07001707 skb->ignore_df = ip6_sk_ignore_df(sk);
Herbert Xu28a894532008-02-12 18:07:27 -08001708
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001709 *final_dst = fl6->daddr;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001710 __skb_pull(skb, skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 if (opt && opt->opt_flen)
1712 ipv6_push_frag_opts(skb, opt, &proto);
1713 if (opt && opt->opt_nflen)
1714 ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst);
1715
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07001716 skb_push(skb, sizeof(struct ipv6hdr));
1717 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001718 hdr = ipv6_hdr(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001719
Vlad Yasevich64223982015-01-31 10:40:15 -05001720 ip6_flow_hdr(hdr, v6_cork->tclass,
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001721 ip6_make_flowlabel(net, skb, fl6->flowlabel,
Shaohua Lib3b56032017-12-20 12:10:21 -08001722 ip6_autoflowlabel(net, np), fl6));
Vlad Yasevich64223982015-01-31 10:40:15 -05001723 hdr->hop_limit = v6_cork->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 hdr->nexthdr = proto;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001725 hdr->saddr = fl6->saddr;
1726 hdr->daddr = *final_dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Patrick McHardya2c20642006-01-08 22:37:26 -08001728 skb->priority = sk->sk_priority;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -08001729 skb->mark = sk->sk_mark;
Patrick McHardya2c20642006-01-08 22:37:26 -08001730
Changli Gaod8d1f302010-06-10 23:31:35 -07001731 skb_dst_set(skb, dst_clone(&rt->dst));
Neil Hormanedf391f2009-04-27 02:45:02 -07001732 IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
David L Stevens14878f72007-09-16 16:52:35 -07001733 if (proto == IPPROTO_ICMPV6) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001734 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
David L Stevens14878f72007-09-16 16:52:35 -07001735
Hannes Frederic Sowa43a43b62014-03-31 20:14:10 +02001736 ICMP6MSGOUT_INC_STATS(net, idev, icmp6_hdr(skb)->icmp6_type);
1737 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
David L Stevens14878f72007-09-16 16:52:35 -07001738 }
1739
Vlad Yasevich64223982015-01-31 10:40:15 -05001740 ip6_cork_release(cork, v6_cork);
1741out:
1742 return skb;
1743}
1744
1745int ip6_send_skb(struct sk_buff *skb)
1746{
1747 struct net *net = sock_net(skb->sk);
1748 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
1749 int err;
1750
Eric W. Biederman33224b12015-10-07 16:48:46 -05001751 err = ip6_local_out(net, skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (err) {
1753 if (err > 0)
Eric Dumazet6ce9e7b2009-09-02 18:05:33 -07001754 err = net_xmit_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if (err)
Vlad Yasevich64223982015-01-31 10:40:15 -05001756 IP6_INC_STATS(net, rt->rt6i_idev,
1757 IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return err;
Vlad Yasevich64223982015-01-31 10:40:15 -05001761}
1762
1763int ip6_push_pending_frames(struct sock *sk)
1764{
1765 struct sk_buff *skb;
1766
1767 skb = ip6_finish_skb(sk);
1768 if (!skb)
1769 return 0;
1770
1771 return ip6_send_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
Chris Elstona495f832012-04-29 21:48:53 +00001773EXPORT_SYMBOL_GPL(ip6_push_pending_frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001775static void __ip6_flush_pending_frames(struct sock *sk,
Vlad Yasevich64223982015-01-31 10:40:15 -05001776 struct sk_buff_head *queue,
1777 struct inet_cork_full *cork,
1778 struct inet6_cork *v6_cork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 struct sk_buff *skb;
1781
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001782 while ((skb = __skb_dequeue_tail(queue)) != NULL) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001783 if (skb_dst(skb))
1784 IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakie1f522082007-09-11 11:31:43 +02001785 IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 kfree_skb(skb);
1787 }
1788
Vlad Yasevich64223982015-01-31 10:40:15 -05001789 ip6_cork_release(cork, v6_cork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001791
1792void ip6_flush_pending_frames(struct sock *sk)
1793{
Vlad Yasevich64223982015-01-31 10:40:15 -05001794 __ip6_flush_pending_frames(sk, &sk->sk_write_queue,
1795 &inet_sk(sk)->cork, &inet6_sk(sk)->cork);
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001796}
Chris Elstona495f832012-04-29 21:48:53 +00001797EXPORT_SYMBOL_GPL(ip6_flush_pending_frames);
Vlad Yasevich64223982015-01-31 10:40:15 -05001798
1799struct sk_buff *ip6_make_skb(struct sock *sk,
1800 int getfrag(void *from, char *to, int offset,
1801 int len, int odd, struct sk_buff *skb),
1802 void *from, int length, int transhdrlen,
Wei Wang26879da2016-05-02 21:40:07 -07001803 struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
Vlad Yasevich64223982015-01-31 10:40:15 -05001804 struct rt6_info *rt, unsigned int flags,
Wei Wang26879da2016-05-02 21:40:07 -07001805 const struct sockcm_cookie *sockc)
Vlad Yasevich64223982015-01-31 10:40:15 -05001806{
1807 struct inet_cork_full cork;
1808 struct inet6_cork v6_cork;
1809 struct sk_buff_head queue;
Wei Wang26879da2016-05-02 21:40:07 -07001810 int exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0);
Vlad Yasevich64223982015-01-31 10:40:15 -05001811 int err;
1812
1813 if (flags & MSG_PROBE)
1814 return NULL;
1815
1816 __skb_queue_head_init(&queue);
1817
1818 cork.base.flags = 0;
1819 cork.base.addr = 0;
1820 cork.base.opt = NULL;
Eric Dumazetc2ceff12018-01-11 22:31:18 -08001821 cork.base.dst = NULL;
Vlad Yasevich64223982015-01-31 10:40:15 -05001822 v6_cork.opt = NULL;
Wei Wang26879da2016-05-02 21:40:07 -07001823 err = ip6_setup_cork(sk, &cork, &v6_cork, ipc6, rt, fl6);
Eric Dumazetdde00c92018-01-10 03:45:49 -08001824 if (err) {
1825 ip6_cork_release(&cork, &v6_cork);
Vlad Yasevich64223982015-01-31 10:40:15 -05001826 return ERR_PTR(err);
Eric Dumazetdde00c92018-01-10 03:45:49 -08001827 }
Wei Wang26879da2016-05-02 21:40:07 -07001828 if (ipc6->dontfrag < 0)
1829 ipc6->dontfrag = inet6_sk(sk)->dontfrag;
Vlad Yasevich64223982015-01-31 10:40:15 -05001830
1831 err = __ip6_append_data(sk, fl6, &queue, &cork.base, &v6_cork,
1832 &current->task_frag, getfrag, from,
1833 length + exthdrlen, transhdrlen + exthdrlen,
Wei Wang26879da2016-05-02 21:40:07 -07001834 flags, ipc6, sockc);
Vlad Yasevich64223982015-01-31 10:40:15 -05001835 if (err) {
1836 __ip6_flush_pending_frames(sk, &queue, &cork, &v6_cork);
1837 return ERR_PTR(err);
1838 }
1839
1840 return __ip6_make_skb(sk, &queue, &cork, &v6_cork);
1841}