blob: a2158022e260e9e731d1ddf2d3c5dd677f81abc2 [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
70 skb->protocol = htons(ETH_P_IPV6);
71 skb->dev = dev;
72
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070073 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
Eric Dumazetadf30902009-06-02 05:19:30 +000074 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
David Miller7026b1d2015-04-05 22:19:04 -040076 if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(sk) &&
Eric W. Biederman78126c42015-09-15 20:04:09 -050077 ((mroute6_socket(net, skb) &&
Benjamin Therybd91b8b2008-12-10 16:07:08 -080078 !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) ||
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090079 ipv6_chk_mcast_addr(dev, &ipv6_hdr(skb)->daddr,
80 &ipv6_hdr(skb)->saddr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
82
83 /* Do not check for IFF_ALLMULTI; multicast routing
84 is not supported in any case.
85 */
86 if (newskb)
Jan Engelhardtb2e0b382010-03-23 04:09:07 +010087 NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING,
Eric W. Biederman29a26a52015-09-15 20:04:16 -050088 net, sk, newskb, NULL, newskb->dev,
Michel Machado95603e22012-06-12 10:16:35 +000089 dev_loopback_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070091 if (ipv6_hdr(skb)->hop_limit == 0) {
Eric W. Biederman78126c42015-09-15 20:04:09 -050092 IP6_INC_STATS(net, idev,
Denis V. Lunev3bd653c2008-10-08 10:54:51 -070093 IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 kfree_skb(skb);
95 return 0;
96 }
97 }
98
Eric W. Biederman78126c42015-09-15 20:04:09 -050099 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, skb->len);
Hannes Frederic Sowadd408512013-02-10 02:33:35 +0000100
101 if (IPV6_ADDR_MC_SCOPE(&ipv6_hdr(skb)->daddr) <=
102 IPV6_ADDR_SCOPE_NODELOCAL &&
103 !(dev->flags & IFF_LOOPBACK)) {
104 kfree_skb(skb);
105 return 0;
106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700109 if (lwtunnel_xmit_redirect(dst->lwtstate)) {
110 int res = lwtunnel_xmit(skb);
111
112 if (res < 0 || res == LWTUNNEL_XMIT_DONE)
113 return res;
114 }
115
YOSHIFUJI Hideaki / 吉藤英明6fd6ce22013-01-17 12:54:00 +0000116 rcu_read_lock_bh();
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700117 nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr);
YOSHIFUJI Hideaki / 吉藤英明6fd6ce22013-01-17 12:54:00 +0000118 neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
119 if (unlikely(!neigh))
120 neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
121 if (!IS_ERR(neigh)) {
122 ret = dst_neigh_output(dst, neigh, skb);
123 rcu_read_unlock_bh();
124 return ret;
125 }
126 rcu_read_unlock_bh();
David S. Miller05e3aa02011-07-16 17:26:00 -0700127
Eric W. Biederman78126c42015-09-15 20:04:09 -0500128 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
Jan Engelhardt9e508492010-04-13 15:28:11 +0200129 kfree_skb(skb);
130 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500133static int ip6_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Jan Engelhardt9e508492010-04-13 15:28:11 +0200134{
Daniel Mack9290d742016-11-23 16:52:29 +0100135 int ret;
136
137 ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
138 if (ret) {
139 kfree_skb(skb);
140 return ret;
141 }
142
Jan Engelhardt9e508492010-04-13 15:28:11 +0200143 if ((skb->len > ip6_skb_dst_mtu(skb) && !skb_is_gso(skb)) ||
Jiri Pirko9037c352013-11-06 17:52:19 +0100144 dst_allfrag(skb_dst(skb)) ||
145 (IP6CB(skb)->frag_max_size && skb->len > IP6CB(skb)->frag_max_size))
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500146 return ip6_fragment(net, sk, skb, ip6_finish_output2);
Jan Engelhardt9e508492010-04-13 15:28:11 +0200147 else
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500148 return ip6_finish_output2(net, sk, skb);
Jan Engelhardt9e508492010-04-13 15:28:11 +0200149}
150
Eric W. Biedermanede20592015-10-07 16:48:47 -0500151int ip6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Jan Engelhardt9e508492010-04-13 15:28:11 +0200153 struct net_device *dev = skb_dst(skb)->dev;
Eric Dumazetadf30902009-06-02 05:19:30 +0000154 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
Eric W. Biedermanbe10de02015-09-17 17:21:31 -0500155
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/*
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700169 * xmit an sk_buff (used by TCP, SCTP and DCCP)
170 * Note : socket lock is not held for SYNACK packets, but might be modified
171 * by calls to skb_set_owner_w() and ipv6_local_error(),
172 * which are using proper atomic operations or spinlocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 */
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700174int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
Pablo Neira0d4c19e2017-01-26 22:56:21 +0100175 __u32 mark, struct ipv6_txoptions *opt, int tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700177 struct net *net = sock_net(sk);
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700178 const struct ipv6_pinfo *np = inet6_sk(sk);
David S. Miller4c9483b2011-03-12 16:22:43 -0500179 struct in6_addr *first_hop = &fl6->daddr;
Eric Dumazetadf30902009-06-02 05:19:30 +0000180 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 struct ipv6hdr *hdr;
David S. Miller4c9483b2011-03-12 16:22:43 -0500182 u8 proto = fl6->flowi6_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int seg_len = skb->len;
Gerrit Renkere651f032009-08-09 08:12:48 +0000184 int hlimit = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 u32 mtu;
186
187 if (opt) {
Chuck Leverc2636b42007-10-23 21:07:32 -0700188 unsigned int head_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 /* First: exthdrs may take lots of space (~8K for now)
191 MAX_HEADER is not enough.
192 */
193 head_room = opt->opt_nflen + opt->opt_flen;
194 seg_len += head_room;
195 head_room += sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev);
196
197 if (skb_headroom(skb) < head_room) {
198 struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room);
Ian Morris63159f22015-03-29 14:00:04 +0100199 if (!skb2) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000200 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900201 IPSTATS_MIB_OUTDISCARDS);
202 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return -ENOBUFS;
204 }
Eric Dumazet808db802012-04-24 10:17:59 +0000205 consume_skb(skb);
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900206 skb = skb2;
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700207 /* skb_set_owner_w() changes sk->sk_wmem_alloc atomically,
208 * it is safe to call in our context (socket lock not held)
209 */
210 skb_set_owner_w(skb, (struct sock *)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212 if (opt->opt_flen)
213 ipv6_push_frag_opts(skb, opt, &proto);
214 if (opt->opt_nflen)
215 ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop);
216 }
217
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700218 skb_push(skb, sizeof(struct ipv6hdr));
219 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700220 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /*
223 * Fill in the IPv6 header
224 */
Eric Dumazetb903d322011-10-27 00:44:35 -0400225 if (np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 hlimit = np->hop_limit;
227 if (hlimit < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -0400228 hlimit = ip6_dst_hoplimit(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700230 ip6_flow_hdr(hdr, tclass, ip6_make_flowlabel(net, skb, fl6->flowlabel,
Tom Herbert67800f92015-07-31 16:52:11 -0700231 np->autoflowlabel, fl6));
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 hdr->payload_len = htons(seg_len);
234 hdr->nexthdr = proto;
235 hdr->hop_limit = hlimit;
236
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000237 hdr->saddr = fl6->saddr;
238 hdr->daddr = *first_hop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Hannes Frederic Sowa9c9c9ad2013-08-26 12:31:23 +0200240 skb->protocol = htons(ETH_P_IPV6);
Patrick McHardya2c20642006-01-08 22:37:26 -0800241 skb->priority = sk->sk_priority;
Pablo Neira0d4c19e2017-01-26 22:56:21 +0100242 skb->mark = mark;
Patrick McHardya2c20642006-01-08 22:37:26 -0800243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 mtu = dst_mtu(dst);
WANG Cong60ff7462014-05-04 16:39:18 -0700245 if ((skb->len <= mtu) || skb->ignore_df || skb_is_gso(skb)) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000246 IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)),
Neil Hormanedf391f2009-04-27 02:45:02 -0700247 IPSTATS_MIB_OUT, skb->len);
David Aherna8e3e1a2016-09-10 12:09:53 -0700248
249 /* if egress device is enslaved to an L3 master device pass the
250 * skb to its handler for processing
251 */
252 skb = l3mdev_ip6_out((struct sock *)sk, skb);
253 if (unlikely(!skb))
254 return 0;
255
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700256 /* hooks should never assume socket lock is held.
257 * we promote our socket to non const
258 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500259 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700260 net, (struct sock *)sk, skb, NULL, dst->dev,
Eric W. Biederman13206b62015-10-07 16:48:35 -0500261 dst_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 skb->dev = dst->dev;
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700265 /* ipv6_local_error() does not require socket lock,
266 * we promote our socket to non const
267 */
268 ipv6_local_error((struct sock *)sk, EMSGSIZE, fl6, mtu);
269
Eric Dumazetadf30902009-06-02 05:19:30 +0000270 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 kfree_skb(skb);
272 return -EMSGSIZE;
273}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900274EXPORT_SYMBOL(ip6_xmit);
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276static int ip6_call_ra_chain(struct sk_buff *skb, int sel)
277{
278 struct ip6_ra_chain *ra;
279 struct sock *last = NULL;
280
281 read_lock(&ip6_ra_lock);
282 for (ra = ip6_ra_chain; ra; ra = ra->next) {
283 struct sock *sk = ra->sk;
Andrew McDonald0bd1b592005-08-09 19:44:42 -0700284 if (sk && ra->sel == sel &&
285 (!sk->sk_bound_dev_if ||
286 sk->sk_bound_dev_if == skb->dev->ifindex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (last) {
288 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
289 if (skb2)
290 rawv6_rcv(last, skb2);
291 }
292 last = sk;
293 }
294 }
295
296 if (last) {
297 rawv6_rcv(last, skb);
298 read_unlock(&ip6_ra_lock);
299 return 1;
300 }
301 read_unlock(&ip6_ra_lock);
302 return 0;
303}
304
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700305static int ip6_forward_proxy_check(struct sk_buff *skb)
306{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700307 struct ipv6hdr *hdr = ipv6_hdr(skb);
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700308 u8 nexthdr = hdr->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -0800309 __be16 frag_off;
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700310 int offset;
311
312 if (ipv6_ext_hdr(nexthdr)) {
Jesse Gross75f28112011-11-30 17:05:51 -0800313 offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr, &frag_off);
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700314 if (offset < 0)
315 return 0;
316 } else
317 offset = sizeof(struct ipv6hdr);
318
319 if (nexthdr == IPPROTO_ICMPV6) {
320 struct icmp6hdr *icmp6;
321
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700322 if (!pskb_may_pull(skb, (skb_network_header(skb) +
323 offset + 1 - skb->data)))
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700324 return 0;
325
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700326 icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700327
328 switch (icmp6->icmp6_type) {
329 case NDISC_ROUTER_SOLICITATION:
330 case NDISC_ROUTER_ADVERTISEMENT:
331 case NDISC_NEIGHBOUR_SOLICITATION:
332 case NDISC_NEIGHBOUR_ADVERTISEMENT:
333 case NDISC_REDIRECT:
334 /* For reaction involving unicast neighbor discovery
335 * message destined to the proxied address, pass it to
336 * input function.
337 */
338 return 1;
339 default:
340 break;
341 }
342 }
343
Ville Nuorvala74553b02006-09-22 14:42:18 -0700344 /*
345 * The proxying router can't forward traffic sent to a link-local
346 * address, so signal the sender and discard the packet. This
347 * behavior is clarified by the MIPv6 specification.
348 */
349 if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) {
350 dst_link_failure(skb);
351 return -1;
352 }
353
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700354 return 0;
355}
356
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500357static inline int ip6_forward_finish(struct net *net, struct sock *sk,
358 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Eric W. Biederman13206b62015-10-07 16:48:35 -0500360 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Hannes Frederic Sowa0954cf92014-01-09 10:01:16 +0100363static unsigned int ip6_dst_mtu_forward(const struct dst_entry *dst)
364{
365 unsigned int mtu;
366 struct inet6_dev *idev;
367
368 if (dst_metric_locked(dst, RTAX_MTU)) {
369 mtu = dst_metric_raw(dst, RTAX_MTU);
370 if (mtu)
371 return mtu;
372 }
373
374 mtu = IPV6_MIN_MTU;
375 rcu_read_lock();
376 idev = __in6_dev_get(dst->dev);
377 if (idev)
378 mtu = idev->cnf.mtu6;
379 rcu_read_unlock();
380
381 return mtu;
382}
383
Florian Westphalfe6cc552014-02-13 23:09:12 +0100384static bool ip6_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
385{
Florian Westphal418a3152014-05-05 00:03:34 +0200386 if (skb->len <= mtu)
Florian Westphalfe6cc552014-02-13 23:09:12 +0100387 return false;
388
WANG Cong60ff7462014-05-04 16:39:18 -0700389 /* ipv6 conntrack defrag sets max_frag_size + ignore_df */
Florian Westphalfe6cc552014-02-13 23:09:12 +0100390 if (IP6CB(skb)->frag_max_size && IP6CB(skb)->frag_max_size > mtu)
391 return true;
392
WANG Cong60ff7462014-05-04 16:39:18 -0700393 if (skb->ignore_df)
Florian Westphal418a3152014-05-05 00:03:34 +0200394 return false;
395
Marcelo Ricardo Leitnerae7ef812016-06-02 15:05:41 -0300396 if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
Florian Westphalfe6cc552014-02-13 23:09:12 +0100397 return false;
398
399 return true;
400}
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402int ip6_forward(struct sk_buff *skb)
403{
Eric Dumazetadf30902009-06-02 05:19:30 +0000404 struct dst_entry *dst = skb_dst(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700405 struct ipv6hdr *hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 struct inet6_skb_parm *opt = IP6CB(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900407 struct net *net = dev_net(dst->dev);
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800408 u32 mtu;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900409
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700410 if (net->ipv6.devconf_all->forwarding == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 goto error;
412
Li RongQing090f1162014-03-11 10:40:08 +0800413 if (skb->pkt_type != PACKET_HOST)
414 goto drop;
415
Hannes Frederic Sowa9ef2e962015-10-08 18:19:53 +0200416 if (unlikely(skb->sk))
417 goto drop;
418
Ben Hutchings4497b072008-06-19 16:22:28 -0700419 if (skb_warn_if_lro(skb))
420 goto drop;
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700423 __IP6_INC_STATS(net, ip6_dst_idev(dst),
424 IPSTATS_MIB_INDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 goto drop;
426 }
427
Herbert Xu35fc92a2007-03-26 23:22:20 -0700428 skb_forward_csum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 /*
431 * We DO NOT make any processing on
432 * RA packets, pushing them to user level AS IS
433 * without ane WARRANTY that application will be able
434 * to interpret them. The reason is that we
435 * cannot make anything clever here.
436 *
437 * We are not end-node, so that if packet contains
438 * AH/ESP, we cannot make anything.
439 * Defragmentation also would be mistake, RA packets
440 * cannot be fragmented, because there is no warranty
441 * that different fragments will go along one path. --ANK
442 */
YOSHIFUJI Hideaki / 吉藤英明ab4eb352013-06-22 11:13:13 +0900443 if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
444 if (ip6_call_ra_chain(skb, ntohs(opt->ra)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return 0;
446 }
447
448 /*
449 * check and decrement ttl
450 */
451 if (hdr->hop_limit <= 1) {
452 /* Force OUTPUT device used as source address */
453 skb->dev = dst->dev;
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000454 icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0);
Eric Dumazet1d015502016-04-27 16:44:40 -0700455 __IP6_INC_STATS(net, ip6_dst_idev(dst),
456 IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 kfree_skb(skb);
459 return -ETIMEDOUT;
460 }
461
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700462 /* XXX: idev->cnf.proxy_ndp? */
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700463 if (net->ipv6.devconf_all->proxy_ndp &&
Daniel Lezcano8a3edd82008-03-07 11:14:16 -0800464 pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0)) {
Ville Nuorvala74553b02006-09-22 14:42:18 -0700465 int proxied = ip6_forward_proxy_check(skb);
466 if (proxied > 0)
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700467 return ip6_input(skb);
Ville Nuorvala74553b02006-09-22 14:42:18 -0700468 else if (proxied < 0) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700469 __IP6_INC_STATS(net, ip6_dst_idev(dst),
470 IPSTATS_MIB_INDISCARDS);
Ville Nuorvala74553b02006-09-22 14:42:18 -0700471 goto drop;
472 }
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700473 }
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (!xfrm6_route_forward(skb)) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700476 __IP6_INC_STATS(net, ip6_dst_idev(dst),
477 IPSTATS_MIB_INDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 goto drop;
479 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000480 dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /* IPv6 specs say nothing about it, but it is clear that we cannot
483 send redirects to source routed frames.
Masahide NAKAMURA1e5dc142007-08-24 19:08:55 +0900484 We don't send redirects to frames decapsulated from IPsec.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 */
David S. Millerc45a3df2012-01-27 15:32:19 -0800486 if (skb->dev == dst->dev && opt->srcrt == 0 && !skb_sec_path(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 struct in6_addr *target = NULL;
David S. Millerfbfe95a2012-06-08 23:24:18 -0700488 struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 /*
492 * incoming and outgoing devices are the same
493 * send a redirect.
494 */
495
496 rt = (struct rt6_info *) dst;
David S. Millerc45a3df2012-01-27 15:32:19 -0800497 if (rt->rt6i_flags & RTF_GATEWAY)
498 target = &rt->rt6i_gateway;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 else
500 target = &hdr->daddr;
501
Martin KaFai Laufd0273d2015-05-22 20:55:57 -0700502 peer = inet_getpeer_v6(net->ipv6.peers, &hdr->daddr, 1);
David S. Miller92d86822011-02-04 15:55:25 -0800503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /* Limit redirects both by destination (here)
505 and by source (inside ndisc_send_redirect)
506 */
David S. Millerfbfe95a2012-06-08 23:24:18 -0700507 if (inet_peer_xrlim_allow(peer, 1*HZ))
David S. Miller49919692012-01-27 15:30:48 -0800508 ndisc_send_redirect(skb, target);
David S. Miller1d861aa2012-07-10 03:58:16 -0700509 if (peer)
510 inet_putpeer(peer);
David L Stevens5bb1ab02007-05-09 13:53:44 -0700511 } else {
512 int addrtype = ipv6_addr_type(&hdr->saddr);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* This check is security critical. */
YOSHIFUJI Hideakif81b2e72008-06-25 16:55:26 +0900515 if (addrtype == IPV6_ADDR_ANY ||
516 addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
David L Stevens5bb1ab02007-05-09 13:53:44 -0700517 goto error;
518 if (addrtype & IPV6_ADDR_LINKLOCAL) {
519 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000520 ICMPV6_NOT_NEIGHBOUR, 0);
David L Stevens5bb1ab02007-05-09 13:53:44 -0700521 goto error;
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524
Hannes Frederic Sowa0954cf92014-01-09 10:01:16 +0100525 mtu = ip6_dst_mtu_forward(dst);
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800526 if (mtu < IPV6_MIN_MTU)
527 mtu = IPV6_MIN_MTU;
528
Florian Westphalfe6cc552014-02-13 23:09:12 +0100529 if (ip6_pkt_too_big(skb, mtu)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /* Again, force OUTPUT device used as source address */
531 skb->dev = dst->dev;
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800532 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Eric Dumazet1d015502016-04-27 16:44:40 -0700533 __IP6_INC_STATS(net, ip6_dst_idev(dst),
534 IPSTATS_MIB_INTOOBIGERRORS);
535 __IP6_INC_STATS(net, ip6_dst_idev(dst),
536 IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 kfree_skb(skb);
538 return -EMSGSIZE;
539 }
540
541 if (skb_cow(skb, dst->dev->hard_header_len)) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700542 __IP6_INC_STATS(net, ip6_dst_idev(dst),
543 IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 goto drop;
545 }
546
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700547 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 /* Mangling hops number delayed to point after skb COW */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 hdr->hop_limit--;
552
Eric Dumazet1d015502016-04-27 16:44:40 -0700553 __IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS);
554 __IP6_ADD_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTOCTETS, skb->len);
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500555 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD,
556 net, NULL, skb, skb->dev, dst->dev,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800557 ip6_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559error:
Eric Dumazet1d015502016-04-27 16:44:40 -0700560 __IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INADDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561drop:
562 kfree_skb(skb);
563 return -EINVAL;
564}
565
566static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
567{
568 to->pkt_type = from->pkt_type;
569 to->priority = from->priority;
570 to->protocol = from->protocol;
Eric Dumazetadf30902009-06-02 05:19:30 +0000571 skb_dst_drop(to);
572 skb_dst_set(to, dst_clone(skb_dst(from)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 to->dev = from->dev;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800574 to->mark = from->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576#ifdef CONFIG_NET_SCHED
577 to->tc_index = from->tc_index;
578#endif
Yasuyuki Kozakaie7ac05f2007-03-14 16:44:01 -0700579 nf_copy(to, from);
James Morris984bc162006-06-09 00:29:17 -0700580 skb_copy_secmark(to, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500583int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
584 int (*output)(struct net *, struct sock *, struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 struct sk_buff *frag;
Ian Morris67ba4152014-08-24 21:53:10 +0100587 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
hannes@stressinduktion.orgf60e5992015-04-01 17:07:44 +0200588 struct ipv6_pinfo *np = skb->sk && !dev_recursion_level() ?
589 inet6_sk(skb->sk) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 struct ipv6hdr *tmp_hdr;
591 struct frag_hdr *fh;
592 unsigned int mtu, hlen, left, len;
Herbert Xua7ae1992011-11-18 02:20:04 +0000593 int hroom, troom;
Martin KaFai Lau286c2342015-05-22 20:55:56 -0700594 __be32 frag_id;
Ian Morris67ba4152014-08-24 21:53:10 +0100595 int ptr, offset = 0, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 u8 *prevhdr, nexthdr = 0;
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 hlen = ip6_find_1stfragopt(skb, &prevhdr);
599 nexthdr = *prevhdr;
600
John Heffner628a5c52007-04-20 15:53:27 -0700601 mtu = ip6_skb_dst_mtu(skb);
John Heffnerb881ef72007-04-20 15:52:39 -0700602
603 /* We must not fragment if the socket is set to force MTU discovery
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800604 * or if the skb it not generated by a local socket.
John Heffnerb881ef72007-04-20 15:52:39 -0700605 */
Florian Westphal485fca62015-05-22 00:44:16 +0200606 if (unlikely(!skb->ignore_df && skb->len > mtu))
607 goto fail_toobig;
Eric Dumazeta34a1012012-05-18 21:51:44 +0000608
Florian Westphal485fca62015-05-22 00:44:16 +0200609 if (IP6CB(skb)->frag_max_size) {
610 if (IP6CB(skb)->frag_max_size > mtu)
611 goto fail_toobig;
612
613 /* don't send fragments larger than what we received */
614 mtu = IP6CB(skb)->frag_max_size;
615 if (mtu < IPV6_MIN_MTU)
616 mtu = IPV6_MIN_MTU;
John Heffnerb881ef72007-04-20 15:52:39 -0700617 }
618
YOSHIFUJI Hideakid91675f2006-02-24 13:18:33 -0800619 if (np && np->frag_size < mtu) {
620 if (np->frag_size)
621 mtu = np->frag_size;
622 }
Hannes Frederic Sowa89bc7842015-10-28 13:21:04 +0100623 if (mtu < hlen + sizeof(struct frag_hdr) + 8)
Hannes Frederic Sowab72a2b02015-10-16 11:32:43 +0200624 goto fail_toobig;
Hannes Frederic Sowa1e0d69a2015-10-28 13:21:03 +0100625 mtu -= hlen + sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Martin KaFai Laufd0273d2015-05-22 20:55:57 -0700627 frag_id = ipv6_select_ident(net, &ipv6_hdr(skb)->daddr,
628 &ipv6_hdr(skb)->saddr);
Martin KaFai Lau286c2342015-05-22 20:55:56 -0700629
Hannes Frederic Sowa405c92f2015-10-27 22:40:42 +0100630 if (skb->ip_summed == CHECKSUM_PARTIAL &&
631 (err = skb_checksum_help(skb)))
632 goto fail;
633
Florian Westphal1d325d22015-09-16 17:26:14 +0200634 hroom = LL_RESERVED_SPACE(rt->dst.dev);
David S. Miller21dc3302010-08-23 00:13:46 -0700635 if (skb_has_frag_list(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 int first_len = skb_pagelen(skb);
Eric Dumazet3d130082010-09-21 08:47:45 +0000637 struct sk_buff *frag2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (first_len - hlen > mtu ||
640 ((first_len - hlen) & 7) ||
Florian Westphal1d325d22015-09-16 17:26:14 +0200641 skb_cloned(skb) ||
642 skb_headroom(skb) < (hroom + sizeof(struct frag_hdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 goto slow_path;
644
David S. Miller4d9092b2009-06-09 00:20:05 -0700645 skb_walk_frags(skb, frag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 /* Correct geometry. */
647 if (frag->len > mtu ||
648 ((frag->len & 7) && frag->next) ||
Florian Westphal1d325d22015-09-16 17:26:14 +0200649 skb_headroom(frag) < (hlen + hroom + sizeof(struct frag_hdr)))
Eric Dumazet3d130082010-09-21 08:47:45 +0000650 goto slow_path_clean;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /* Partially cloned skb? */
653 if (skb_shared(frag))
Eric Dumazet3d130082010-09-21 08:47:45 +0000654 goto slow_path_clean;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700655
656 BUG_ON(frag->sk);
657 if (skb->sk) {
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700658 frag->sk = skb->sk;
659 frag->destructor = sock_wfree;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700660 }
Eric Dumazet3d130082010-09-21 08:47:45 +0000661 skb->truesize -= frag->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
663
664 err = 0;
665 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /* BUILD HEADER */
667
YOSHIFUJI Hideaki9a217a12006-12-05 13:47:21 -0800668 *prevhdr = NEXTHDR_FRAGMENT;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700669 tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (!tmp_hdr) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000671 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700672 IPSTATS_MIB_FRAGFAILS);
Florian Westphal1d325d22015-09-16 17:26:14 +0200673 err = -ENOMEM;
674 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Florian Westphal1d325d22015-09-16 17:26:14 +0200676 frag = skb_shinfo(skb)->frag_list;
677 skb_frag_list_init(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 __skb_pull(skb, hlen);
Ian Morris67ba4152014-08-24 21:53:10 +0100680 fh = (struct frag_hdr *)__skb_push(skb, sizeof(struct frag_hdr));
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700681 __skb_push(skb, hlen);
682 skb_reset_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700683 memcpy(skb_network_header(skb), tmp_hdr, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 fh->nexthdr = nexthdr;
686 fh->reserved = 0;
687 fh->frag_off = htons(IP6_MF);
Martin KaFai Lau286c2342015-05-22 20:55:56 -0700688 fh->identification = frag_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 first_len = skb_pagelen(skb);
691 skb->data_len = first_len - skb_headlen(skb);
692 skb->len = first_len;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700693 ipv6_hdr(skb)->payload_len = htons(first_len -
694 sizeof(struct ipv6hdr));
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900695
Changli Gaod8d1f302010-06-10 23:31:35 -0700696 dst_hold(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 for (;;) {
699 /* Prepare header of the next frame,
700 * before previous one went down. */
701 if (frag) {
702 frag->ip_summed = CHECKSUM_NONE;
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300703 skb_reset_transport_header(frag);
Ian Morris67ba4152014-08-24 21:53:10 +0100704 fh = (struct frag_hdr *)__skb_push(frag, sizeof(struct frag_hdr));
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700705 __skb_push(frag, hlen);
706 skb_reset_network_header(frag);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700707 memcpy(skb_network_header(frag), tmp_hdr,
708 hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 offset += skb->len - hlen - sizeof(struct frag_hdr);
710 fh->nexthdr = nexthdr;
711 fh->reserved = 0;
712 fh->frag_off = htons(offset);
Ian Morris53b24b82015-03-29 14:00:05 +0100713 if (frag->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 fh->frag_off |= htons(IP6_MF);
715 fh->identification = frag_id;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700716 ipv6_hdr(frag)->payload_len =
717 htons(frag->len -
718 sizeof(struct ipv6hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 ip6_copy_metadata(frag, skb);
720 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900721
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500722 err = output(net, sk, skb);
Ian Morris67ba4152014-08-24 21:53:10 +0100723 if (!err)
Changli Gaod8d1f302010-06-10 23:31:35 -0700724 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700725 IPSTATS_MIB_FRAGCREATES);
Wei Dongdafee492006-08-02 13:41:21 -0700726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (err || !frag)
728 break;
729
730 skb = frag;
731 frag = skb->next;
732 skb->next = NULL;
733 }
734
Jesper Juhla51482b2005-11-08 09:41:34 -0800735 kfree(tmp_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 if (err == 0) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700738 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700739 IPSTATS_MIB_FRAGOKS);
Amerigo Wang94e187c2012-10-29 00:13:19 +0000740 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return 0;
742 }
743
Florian Westphal46cfd722014-09-10 01:08:46 +0200744 kfree_skb_list(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Changli Gaod8d1f302010-06-10 23:31:35 -0700746 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700747 IPSTATS_MIB_FRAGFAILS);
Amerigo Wang94e187c2012-10-29 00:13:19 +0000748 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 return err;
Eric Dumazet3d130082010-09-21 08:47:45 +0000750
751slow_path_clean:
752 skb_walk_frags(skb, frag2) {
753 if (frag2 == frag)
754 break;
755 frag2->sk = NULL;
756 frag2->destructor = NULL;
757 skb->truesize += frag2->truesize;
758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
761slow_path:
762 left = skb->len - hlen; /* Space per frame */
763 ptr = hlen; /* Where to start from */
764
765 /*
766 * Fragment the datagram.
767 */
768
Herbert Xua7ae1992011-11-18 02:20:04 +0000769 troom = rt->dst.dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 /*
772 * Keep copying data until we run out.
773 */
Ian Morris67ba4152014-08-24 21:53:10 +0100774 while (left > 0) {
Florian Westphal683100e2017-03-13 16:24:28 +0100775 u8 *fragnexthdr_offset;
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 len = left;
778 /* IF: it doesn't fit, use 'mtu' - the data space left */
779 if (len > mtu)
780 len = mtu;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300781 /* IF: we are not sending up to and including the packet end
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 then align the next start on an eight byte boundary */
783 if (len < left) {
784 len &= ~7;
785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Joe Perchescbffccc2014-11-05 14:39:21 -0800787 /* Allocate buffer */
788 frag = alloc_skb(len + hlen + sizeof(struct frag_hdr) +
789 hroom + troom, GFP_ATOMIC);
790 if (!frag) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000791 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900792 IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 err = -ENOMEM;
794 goto fail;
795 }
796
797 /*
798 * Set up data on packet
799 */
800
801 ip6_copy_metadata(frag, skb);
Herbert Xua7ae1992011-11-18 02:20:04 +0000802 skb_reserve(frag, hroom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 skb_put(frag, len + hlen + sizeof(struct frag_hdr));
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700804 skb_reset_network_header(frag);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300805 fh = (struct frag_hdr *)(skb_network_header(frag) + hlen);
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700806 frag->transport_header = (frag->network_header + hlen +
807 sizeof(struct frag_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 /*
810 * Charge the memory for the fragment to any owner
811 * it might possess
812 */
813 if (skb->sk)
814 skb_set_owner_w(frag, skb->sk);
815
816 /*
817 * Copy the packet header into the new buffer.
818 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300819 skb_copy_from_linear_data(skb, skb_network_header(frag), hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Florian Westphal683100e2017-03-13 16:24:28 +0100821 fragnexthdr_offset = skb_network_header(frag);
822 fragnexthdr_offset += prevhdr - skb_network_header(skb);
823 *fragnexthdr_offset = NEXTHDR_FRAGMENT;
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 /*
826 * Build fragment header.
827 */
828 fh->nexthdr = nexthdr;
829 fh->reserved = 0;
Martin KaFai Lau286c2342015-05-22 20:55:56 -0700830 fh->identification = frag_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 /*
833 * Copy a block of the IP datagram.
834 */
Himangi Saraogie3f0b862014-07-12 01:57:17 +0530835 BUG_ON(skb_copy_bits(skb, ptr, skb_transport_header(frag),
836 len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 left -= len;
838
839 fh->frag_off = htons(offset);
840 if (left > 0)
841 fh->frag_off |= htons(IP6_MF);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700842 ipv6_hdr(frag)->payload_len = htons(frag->len -
843 sizeof(struct ipv6hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 ptr += len;
846 offset += len;
847
848 /*
849 * Put this fragment into the sending queue.
850 */
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500851 err = output(net, sk, frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (err)
853 goto fail;
Wei Dongdafee492006-08-02 13:41:21 -0700854
Eric Dumazetadf30902009-06-02 05:19:30 +0000855 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700856 IPSTATS_MIB_FRAGCREATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000858 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900859 IPSTATS_MIB_FRAGOKS);
Eric Dumazet808db802012-04-24 10:17:59 +0000860 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return err;
862
Florian Westphal485fca62015-05-22 00:44:16 +0200863fail_toobig:
864 if (skb->sk && dst_allfrag(skb_dst(skb)))
865 sk_nocaps_add(skb->sk, NETIF_F_GSO_MASK);
866
867 skb->dev = skb_dst(skb)->dev;
868 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
869 err = -EMSGSIZE;
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871fail:
Eric Dumazetadf30902009-06-02 05:19:30 +0000872 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900873 IPSTATS_MIB_FRAGFAILS);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900874 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return err;
876}
877
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000878static inline int ip6_rt_check(const struct rt6key *rt_key,
879 const struct in6_addr *fl_addr,
880 const struct in6_addr *addr_cache)
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700881{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000882 return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
Ian Morris63159f22015-03-29 14:00:04 +0100883 (!addr_cache || !ipv6_addr_equal(fl_addr, addr_cache));
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700884}
885
Herbert Xu497c6152006-07-30 20:19:33 -0700886static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
887 struct dst_entry *dst,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000888 const struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Herbert Xu497c6152006-07-30 20:19:33 -0700890 struct ipv6_pinfo *np = inet6_sk(sk);
Eric Dumazeta963a372013-06-26 04:15:07 -0700891 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Herbert Xu497c6152006-07-30 20:19:33 -0700893 if (!dst)
894 goto out;
895
Eric Dumazeta963a372013-06-26 04:15:07 -0700896 if (dst->ops->family != AF_INET6) {
897 dst_release(dst);
898 return NULL;
899 }
900
901 rt = (struct rt6_info *)dst;
Herbert Xu497c6152006-07-30 20:19:33 -0700902 /* Yes, checking route validity in not connected
903 * case is not very simple. Take into account,
904 * that we do not support routing by source, TOS,
Ian Morris67ba4152014-08-24 21:53:10 +0100905 * and MSG_DONTROUTE --ANK (980726)
Herbert Xu497c6152006-07-30 20:19:33 -0700906 *
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700907 * 1. ip6_rt_check(): If route was host route,
908 * check that cached destination is current.
Herbert Xu497c6152006-07-30 20:19:33 -0700909 * If it is network route, we still may
910 * check its validity using saved pointer
911 * to the last used address: daddr_cache.
912 * We do not want to save whole address now,
913 * (because main consumer of this service
914 * is tcp, which has not this problem),
915 * so that the last trick works only on connected
916 * sockets.
917 * 2. oif also should be the same.
918 */
David S. Miller4c9483b2011-03-12 16:22:43 -0500919 if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr, np->daddr_cache) ||
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700920#ifdef CONFIG_IPV6_SUBTREES
David S. Miller4c9483b2011-03-12 16:22:43 -0500921 ip6_rt_check(&rt->rt6i_src, &fl6->saddr, np->saddr_cache) ||
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700922#endif
David Ahernca254492015-10-12 11:47:10 -0700923 (!(fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) &&
924 (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex))) {
Herbert Xu497c6152006-07-30 20:19:33 -0700925 dst_release(dst);
926 dst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928
Herbert Xu497c6152006-07-30 20:19:33 -0700929out:
930 return dst;
931}
932
Eric Dumazet3aef9342015-09-25 07:39:12 -0700933static int ip6_dst_lookup_tail(struct net *net, const struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -0500934 struct dst_entry **dst, struct flowi6 *fl6)
Herbert Xu497c6152006-07-30 20:19:33 -0700935{
David S. Miller69cce1d2011-07-17 23:09:49 -0700936#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
937 struct neighbour *n;
David S. Miller97cac082012-07-02 22:43:47 -0700938 struct rt6_info *rt;
David S. Miller69cce1d2011-07-17 23:09:49 -0700939#endif
940 int err;
Paolo Abeni6f21c962016-01-29 12:30:19 +0100941 int flags = 0;
Herbert Xu497c6152006-07-30 20:19:33 -0700942
Markus Stenberge16e8882015-05-05 13:36:59 +0300943 /* The correct way to handle this would be to do
944 * ip6_route_get_saddr, and then ip6_route_output; however,
945 * the route-specific preferred source forces the
946 * ip6_route_output call _before_ ip6_route_get_saddr.
947 *
948 * In source specific routing (no src=any default route),
949 * ip6_route_output will fail given src=any saddr, though, so
950 * that's why we try it again later.
951 */
952 if (ipv6_addr_any(&fl6->saddr) && (!*dst || !(*dst)->error)) {
953 struct rt6_info *rt;
954 bool had_dst = *dst != NULL;
955
956 if (!had_dst)
957 *dst = ip6_route_output(net, sk, fl6);
958 rt = (*dst)->error ? NULL : (struct rt6_info *)*dst;
959 err = ip6_route_get_saddr(net, rt, &fl6->daddr,
960 sk ? inet6_sk(sk)->srcprefs : 0,
961 &fl6->saddr);
962 if (err)
963 goto out_err_release;
964
965 /* If we had an erroneous initial result, pretend it
966 * never existed and let the SA-enabled version take
967 * over.
968 */
969 if (!had_dst && (*dst)->error) {
970 dst_release(*dst);
971 *dst = NULL;
972 }
Paolo Abeni6f21c962016-01-29 12:30:19 +0100973
974 if (fl6->flowi6_oif)
975 flags |= RT6_LOOKUP_F_IFACE;
Markus Stenberge16e8882015-05-05 13:36:59 +0300976 }
977
Ian Morris63159f22015-03-29 14:00:04 +0100978 if (!*dst)
Paolo Abeni6f21c962016-01-29 12:30:19 +0100979 *dst = ip6_route_output_flags(net, sk, fl6, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Ian Morrise5d08d72014-11-23 21:28:43 +0000981 err = (*dst)->error;
982 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 goto out_err_release;
984
Neil Horman95c385b2007-04-25 17:08:10 -0700985#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Neil Hormane550dfb2008-09-09 13:51:35 -0700986 /*
987 * Here if the dst entry we've looked up
988 * has a neighbour entry that is in the INCOMPLETE
989 * state and the src address from the flow is
990 * marked as OPTIMISTIC, we release the found
991 * dst entry and replace it instead with the
992 * dst entry of the nexthop router
993 */
Eric Dumazetc56bf6f2012-07-06 09:19:05 +0200994 rt = (struct rt6_info *) *dst;
YOSHIFUJI Hideaki / 吉藤英明707be1f2013-01-17 12:53:55 +0000995 rcu_read_lock_bh();
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700996 n = __ipv6_neigh_lookup_noref(rt->dst.dev,
997 rt6_nexthop(rt, &fl6->daddr));
YOSHIFUJI Hideaki / 吉藤英明707be1f2013-01-17 12:53:55 +0000998 err = n && !(n->nud_state & NUD_VALID) ? -EINVAL : 0;
999 rcu_read_unlock_bh();
1000
1001 if (err) {
Neil Hormane550dfb2008-09-09 13:51:35 -07001002 struct inet6_ifaddr *ifp;
David S. Miller4c9483b2011-03-12 16:22:43 -05001003 struct flowi6 fl_gw6;
Neil Hormane550dfb2008-09-09 13:51:35 -07001004 int redirect;
Neil Horman95c385b2007-04-25 17:08:10 -07001005
David S. Miller4c9483b2011-03-12 16:22:43 -05001006 ifp = ipv6_get_ifaddr(net, &fl6->saddr,
Neil Hormane550dfb2008-09-09 13:51:35 -07001007 (*dst)->dev, 1);
Neil Horman95c385b2007-04-25 17:08:10 -07001008
Neil Hormane550dfb2008-09-09 13:51:35 -07001009 redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC);
1010 if (ifp)
1011 in6_ifa_put(ifp);
Neil Horman95c385b2007-04-25 17:08:10 -07001012
Neil Hormane550dfb2008-09-09 13:51:35 -07001013 if (redirect) {
1014 /*
1015 * We need to get the dst entry for the
1016 * default router instead
1017 */
1018 dst_release(*dst);
David S. Miller4c9483b2011-03-12 16:22:43 -05001019 memcpy(&fl_gw6, fl6, sizeof(struct flowi6));
1020 memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr));
1021 *dst = ip6_route_output(net, sk, &fl_gw6);
Ian Morrise5d08d72014-11-23 21:28:43 +00001022 err = (*dst)->error;
1023 if (err)
Neil Hormane550dfb2008-09-09 13:51:35 -07001024 goto out_err_release;
Neil Horman95c385b2007-04-25 17:08:10 -07001025 }
Neil Hormane550dfb2008-09-09 13:51:35 -07001026 }
Neil Horman95c385b2007-04-25 17:08:10 -07001027#endif
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return 0;
1030
1031out_err_release:
1032 dst_release(*dst);
1033 *dst = NULL;
David Ahern8a966fc2016-09-10 12:09:59 -07001034
David Ahern0d240e72016-06-16 16:24:25 -07001035 if (err == -ENETUNREACH)
1036 IP6_INC_STATS(net, NULL, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return err;
1038}
Adrian Bunk34a0b3c2005-11-29 16:28:56 -08001039
Herbert Xu497c6152006-07-30 20:19:33 -07001040/**
1041 * ip6_dst_lookup - perform route lookup on flow
1042 * @sk: socket which provides route info
1043 * @dst: pointer to dst_entry * for result
David S. Miller4c9483b2011-03-12 16:22:43 -05001044 * @fl6: flow to lookup
Herbert Xu497c6152006-07-30 20:19:33 -07001045 *
1046 * This function performs a route lookup on the given flow.
1047 *
1048 * It returns zero on success, or a standard errno code on error.
1049 */
Roopa Prabhu343d60a2015-07-30 13:34:53 -07001050int ip6_dst_lookup(struct net *net, struct sock *sk, struct dst_entry **dst,
1051 struct flowi6 *fl6)
Herbert Xu497c6152006-07-30 20:19:33 -07001052{
1053 *dst = NULL;
Roopa Prabhu343d60a2015-07-30 13:34:53 -07001054 return ip6_dst_lookup_tail(net, sk, dst, fl6);
Herbert Xu497c6152006-07-30 20:19:33 -07001055}
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -08001056EXPORT_SYMBOL_GPL(ip6_dst_lookup);
1057
Herbert Xu497c6152006-07-30 20:19:33 -07001058/**
David S. Miller68d0c6d2011-03-01 13:19:07 -08001059 * ip6_dst_lookup_flow - perform route lookup on flow with ipsec
1060 * @sk: socket which provides route info
David S. Miller4c9483b2011-03-12 16:22:43 -05001061 * @fl6: flow to lookup
David S. Miller68d0c6d2011-03-01 13:19:07 -08001062 * @final_dst: final destination address for ipsec lookup
David S. Miller68d0c6d2011-03-01 13:19:07 -08001063 *
1064 * This function performs a route lookup on the given flow.
1065 *
1066 * It returns a valid dst pointer on success, or a pointer encoded
1067 * error code.
1068 */
Eric Dumazet3aef9342015-09-25 07:39:12 -07001069struct dst_entry *ip6_dst_lookup_flow(const struct sock *sk, struct flowi6 *fl6,
Steffen Klassert0e0d44a2013-08-28 08:04:14 +02001070 const struct in6_addr *final_dst)
David S. Miller68d0c6d2011-03-01 13:19:07 -08001071{
1072 struct dst_entry *dst = NULL;
1073 int err;
1074
Roopa Prabhu343d60a2015-07-30 13:34:53 -07001075 err = ip6_dst_lookup_tail(sock_net(sk), sk, &dst, fl6);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001076 if (err)
1077 return ERR_PTR(err);
1078 if (final_dst)
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001079 fl6->daddr = *final_dst;
David S. Miller2774c132011-03-01 14:59:04 -08001080
Steffen Klassertf92ee612014-09-16 10:08:40 +02001081 return xfrm_lookup_route(sock_net(sk), dst, flowi6_to_flowi(fl6), sk, 0);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001082}
1083EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow);
1084
1085/**
1086 * ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow
1087 * @sk: socket which provides the dst cache and route info
David S. Miller4c9483b2011-03-12 16:22:43 -05001088 * @fl6: flow to lookup
David S. Miller68d0c6d2011-03-01 13:19:07 -08001089 * @final_dst: final destination address for ipsec lookup
Herbert Xu497c6152006-07-30 20:19:33 -07001090 *
1091 * This function performs a route lookup on the given flow with the
1092 * possibility of using the cached route in the socket if it is valid.
1093 * It will take the socket dst lock when operating on the dst cache.
1094 * As a result, this function can only be used in process context.
1095 *
David S. Miller68d0c6d2011-03-01 13:19:07 -08001096 * It returns a valid dst pointer on success, or a pointer encoded
1097 * error code.
Herbert Xu497c6152006-07-30 20:19:33 -07001098 */
David S. Miller4c9483b2011-03-12 16:22:43 -05001099struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
Steffen Klassert0e0d44a2013-08-28 08:04:14 +02001100 const struct in6_addr *final_dst)
Herbert Xu497c6152006-07-30 20:19:33 -07001101{
David S. Miller68d0c6d2011-03-01 13:19:07 -08001102 struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
Herbert Xu497c6152006-07-30 20:19:33 -07001103
David S. Miller4c9483b2011-03-12 16:22:43 -05001104 dst = ip6_sk_dst_check(sk, dst, fl6);
Jakub Sitnicki00bc0ef2016-06-08 15:13:34 +02001105 if (!dst)
1106 dst = ip6_dst_lookup_flow(sk, fl6, final_dst);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001107
Jakub Sitnicki00bc0ef2016-06-08 15:13:34 +02001108 return dst;
Herbert Xu497c6152006-07-30 20:19:33 -07001109}
David S. Miller68d0c6d2011-03-01 13:19:07 -08001110EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow);
Herbert Xu497c6152006-07-30 20:19:33 -07001111
Adrian Bunk34a0b3c2005-11-29 16:28:56 -08001112static inline int ip6_ufo_append_data(struct sock *sk,
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001113 struct sk_buff_head *queue,
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001114 int getfrag(void *from, char *to, int offset, int len,
1115 int odd, struct sk_buff *skb),
1116 void *from, int length, int hh_len, int fragheaderlen,
Jakub Sitnicki3ba34582016-04-05 18:41:08 +02001117 int exthdrlen, int transhdrlen, int mtu,
1118 unsigned int flags, const struct flowi6 *fl6)
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001119
1120{
1121 struct sk_buff *skb;
1122 int err;
1123
1124 /* There is support for UDP large send offload by network
1125 * device, so create one single skb packet containing complete
1126 * udp datagram
1127 */
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001128 skb = skb_peek_tail(queue);
Ian Morris63159f22015-03-29 14:00:04 +01001129 if (!skb) {
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001130 skb = sock_alloc_send_skb(sk,
1131 hh_len + fragheaderlen + transhdrlen + 20,
1132 (flags & MSG_DONTWAIT), &err);
Ian Morris63159f22015-03-29 14:00:04 +01001133 if (!skb)
Zheng Yan504744e2011-10-28 00:26:00 -04001134 return err;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001135
1136 /* reserve space for Hardware header */
1137 skb_reserve(skb, hh_len);
1138
1139 /* create space for UDP/IP header */
Ian Morris67ba4152014-08-24 21:53:10 +01001140 skb_put(skb, fragheaderlen + transhdrlen);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001141
1142 /* initialize network header pointer */
Jakub Sitnicki3ba34582016-04-05 18:41:08 +02001143 skb_set_network_header(skb, exthdrlen);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001144
1145 /* initialize protocol header pointer */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001146 skb->transport_header = skb->network_header + fragheaderlen;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001147
Hannes Frederic Sowa9c9c9ad2013-08-26 12:31:23 +02001148 skb->protocol = htons(ETH_P_IPV6);
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001149 skb->csum = 0;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001150
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001151 __skb_queue_tail(queue, skb);
Jiri Pirkoc547dbf2013-10-19 12:29:16 +02001152 } else if (skb_is_gso(skb)) {
1153 goto append;
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001154 }
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001155
Jiri Pirkoc547dbf2013-10-19 12:29:16 +02001156 skb->ip_summed = CHECKSUM_PARTIAL;
1157 /* Specify the length of each IPv6 datagram fragment.
1158 * It has to be a multiple of 8.
1159 */
1160 skb_shinfo(skb)->gso_size = (mtu - fragheaderlen -
1161 sizeof(struct frag_hdr)) & ~7;
1162 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
Martin KaFai Laufd0273d2015-05-22 20:55:57 -07001163 skb_shinfo(skb)->ip6_frag_id = ipv6_select_ident(sock_net(sk),
1164 &fl6->daddr,
1165 &fl6->saddr);
Jiri Pirkoc547dbf2013-10-19 12:29:16 +02001166
1167append:
Hannes Frederic Sowa2811eba2013-09-21 06:27:00 +02001168 return skb_append_datato_frags(sk, skb, getfrag, from,
1169 (length - transhdrlen));
Ananda Rajue89e9cf2005-10-18 15:46:41 -07001170}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Herbert Xu0178b692009-02-05 15:15:50 -08001172static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src,
1173 gfp_t gfp)
1174{
1175 return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
1176}
1177
1178static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src,
1179 gfp_t gfp)
1180{
1181 return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
1182}
1183
Hannes Frederic Sowa75a493e2013-07-02 08:04:05 +02001184static void ip6_append_data_mtu(unsigned int *mtu,
Gao feng0c183372012-05-26 01:30:53 +00001185 int *maxfraglen,
1186 unsigned int fragheaderlen,
1187 struct sk_buff *skb,
Hannes Frederic Sowa75a493e2013-07-02 08:04:05 +02001188 struct rt6_info *rt,
luciene367c2d2014-03-17 12:51:01 +08001189 unsigned int orig_mtu)
Gao feng0c183372012-05-26 01:30:53 +00001190{
1191 if (!(rt->dst.flags & DST_XFRM_TUNNEL)) {
Ian Morris63159f22015-03-29 14:00:04 +01001192 if (!skb) {
Gao feng0c183372012-05-26 01:30:53 +00001193 /* first fragment, reserve header_len */
luciene367c2d2014-03-17 12:51:01 +08001194 *mtu = orig_mtu - rt->dst.header_len;
Gao feng0c183372012-05-26 01:30:53 +00001195
1196 } else {
1197 /*
1198 * this fragment is not first, the headers
1199 * space is regarded as data space.
1200 */
luciene367c2d2014-03-17 12:51:01 +08001201 *mtu = orig_mtu;
Gao feng0c183372012-05-26 01:30:53 +00001202 }
1203 *maxfraglen = ((*mtu - fragheaderlen) & ~7)
1204 + fragheaderlen - sizeof(struct frag_hdr);
1205 }
1206}
1207
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001208static int ip6_setup_cork(struct sock *sk, struct inet_cork_full *cork,
Wei Wang26879da2016-05-02 21:40:07 -07001209 struct inet6_cork *v6_cork, struct ipcm6_cookie *ipc6,
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001210 struct rt6_info *rt, struct flowi6 *fl6)
1211{
1212 struct ipv6_pinfo *np = inet6_sk(sk);
1213 unsigned int mtu;
Wei Wang26879da2016-05-02 21:40:07 -07001214 struct ipv6_txoptions *opt = ipc6->opt;
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001215
1216 /*
1217 * setup for corking
1218 */
1219 if (opt) {
1220 if (WARN_ON(v6_cork->opt))
1221 return -EINVAL;
1222
1223 v6_cork->opt = kzalloc(opt->tot_len, sk->sk_allocation);
Ian Morris63159f22015-03-29 14:00:04 +01001224 if (unlikely(!v6_cork->opt))
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001225 return -ENOBUFS;
1226
1227 v6_cork->opt->tot_len = opt->tot_len;
1228 v6_cork->opt->opt_flen = opt->opt_flen;
1229 v6_cork->opt->opt_nflen = opt->opt_nflen;
1230
1231 v6_cork->opt->dst0opt = ip6_opt_dup(opt->dst0opt,
1232 sk->sk_allocation);
1233 if (opt->dst0opt && !v6_cork->opt->dst0opt)
1234 return -ENOBUFS;
1235
1236 v6_cork->opt->dst1opt = ip6_opt_dup(opt->dst1opt,
1237 sk->sk_allocation);
1238 if (opt->dst1opt && !v6_cork->opt->dst1opt)
1239 return -ENOBUFS;
1240
1241 v6_cork->opt->hopopt = ip6_opt_dup(opt->hopopt,
1242 sk->sk_allocation);
1243 if (opt->hopopt && !v6_cork->opt->hopopt)
1244 return -ENOBUFS;
1245
1246 v6_cork->opt->srcrt = ip6_rthdr_dup(opt->srcrt,
1247 sk->sk_allocation);
1248 if (opt->srcrt && !v6_cork->opt->srcrt)
1249 return -ENOBUFS;
1250
1251 /* need source address above miyazawa*/
1252 }
1253 dst_hold(&rt->dst);
1254 cork->base.dst = &rt->dst;
1255 cork->fl.u.ip6 = *fl6;
Wei Wang26879da2016-05-02 21:40:07 -07001256 v6_cork->hop_limit = ipc6->hlimit;
1257 v6_cork->tclass = ipc6->tclass;
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001258 if (rt->dst.flags & DST_XFRM_TUNNEL)
1259 mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ?
1260 rt->dst.dev->mtu : dst_mtu(&rt->dst);
1261 else
1262 mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ?
1263 rt->dst.dev->mtu : dst_mtu(rt->dst.path);
1264 if (np->frag_size < mtu) {
1265 if (np->frag_size)
1266 mtu = np->frag_size;
1267 }
1268 cork->base.fragsize = mtu;
1269 if (dst_allfrag(rt->dst.path))
1270 cork->base.flags |= IPCORK_ALLFRAG;
1271 cork->base.length = 0;
1272
1273 return 0;
1274}
1275
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001276static int __ip6_append_data(struct sock *sk,
1277 struct flowi6 *fl6,
1278 struct sk_buff_head *queue,
1279 struct inet_cork *cork,
1280 struct inet6_cork *v6_cork,
1281 struct page_frag *pfrag,
1282 int getfrag(void *from, char *to, int offset,
1283 int len, int odd, struct sk_buff *skb),
1284 void *from, int length, int transhdrlen,
Wei Wang26879da2016-05-02 21:40:07 -07001285 unsigned int flags, struct ipcm6_cookie *ipc6,
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001286 const struct sockcm_cookie *sockc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Gao feng0c183372012-05-26 01:30:53 +00001288 struct sk_buff *skb, *skb_prev = NULL;
luciene367c2d2014-03-17 12:51:01 +08001289 unsigned int maxfraglen, fragheaderlen, mtu, orig_mtu;
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001290 int exthdrlen = 0;
1291 int dst_exthdrlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 int hh_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 int copy;
1294 int err;
1295 int offset = 0;
Anders Berggrena693e692011-02-28 12:32:11 -08001296 __u8 tx_flags = 0;
Willem de Bruijn09c2d252014-08-04 22:11:47 -04001297 u32 tskey = 0;
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001298 struct rt6_info *rt = (struct rt6_info *)cork->dst;
1299 struct ipv6_txoptions *opt = v6_cork->opt;
Vlad Yasevich32dce962015-01-31 10:40:18 -05001300 int csummode = CHECKSUM_NONE;
Hannes Frederic Sowa682b1a92015-10-27 22:40:41 +01001301 unsigned int maxnonfragsize, headersize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001303 skb = skb_peek_tail(queue);
1304 if (!skb) {
1305 exthdrlen = opt ? opt->opt_flen : 0;
Romain KUNTZ7efdba52013-01-16 12:47:40 +00001306 dst_exthdrlen = rt->dst.header_len - rt->rt6i_nfheader_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001308
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001309 mtu = cork->fragsize;
luciene367c2d2014-03-17 12:51:01 +08001310 orig_mtu = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Changli Gaod8d1f302010-06-10 23:31:35 -07001312 hh_len = LL_RESERVED_SPACE(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001314 fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len +
Herbert Xub4ce9272007-11-13 21:33:32 -08001315 (opt ? opt->opt_nflen : 0);
Hannes Frederic Sowa4df98e72013-12-16 12:36:44 +01001316 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen -
1317 sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Hannes Frederic Sowa682b1a92015-10-27 22:40:41 +01001319 headersize = sizeof(struct ipv6hdr) +
1320 (opt ? opt->opt_flen + opt->opt_nflen : 0) +
1321 (dst_allfrag(&rt->dst) ?
1322 sizeof(struct frag_hdr) : 0) +
1323 rt->rt6i_nfheader_len;
Hannes Frederic Sowa4df98e72013-12-16 12:36:44 +01001324
Wei Wang26879da2016-05-02 21:40:07 -07001325 if (cork->length + length > mtu - headersize && ipc6->dontfrag &&
Hannes Frederic Sowa682b1a92015-10-27 22:40:41 +01001326 (sk->sk_protocol == IPPROTO_UDP ||
1327 sk->sk_protocol == IPPROTO_RAW)) {
1328 ipv6_local_rxpmtu(sk, fl6, mtu - headersize +
1329 sizeof(struct ipv6hdr));
1330 goto emsgsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332
Hannes Frederic Sowa682b1a92015-10-27 22:40:41 +01001333 if (ip6_sk_ignore_df(sk))
1334 maxnonfragsize = sizeof(struct ipv6hdr) + IPV6_MAXPLEN;
1335 else
1336 maxnonfragsize = mtu;
1337
1338 if (cork->length + length > maxnonfragsize - headersize) {
1339emsgsize:
1340 ipv6_local_error(sk, EMSGSIZE, fl6,
1341 mtu - headersize +
1342 sizeof(struct ipv6hdr));
1343 return -EMSGSIZE;
1344 }
1345
1346 /* CHECKSUM_PARTIAL only with no extension headers and when
1347 * we are not going to fragment
1348 */
1349 if (transhdrlen && sk->sk_protocol == IPPROTO_UDP &&
1350 headersize == sizeof(struct ipv6hdr) &&
1351 length < mtu - headersize &&
1352 !(flags & MSG_MORE) &&
Tom Herbertc8cd0982015-12-14 11:19:44 -08001353 rt->dst.dev->features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
Hannes Frederic Sowa682b1a92015-10-27 22:40:41 +01001354 csummode = CHECKSUM_PARTIAL;
1355
Willem de Bruijn09c2d252014-08-04 22:11:47 -04001356 if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_RAW) {
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001357 sock_tx_timestamp(sk, sockc->tsflags, &tx_flags);
Willem de Bruijn09c2d252014-08-04 22:11:47 -04001358 if (tx_flags & SKBTX_ANY_SW_TSTAMP &&
1359 sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)
1360 tskey = sk->sk_tskey++;
1361 }
Anders Berggrena693e692011-02-28 12:32:11 -08001362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 /*
1364 * Let's try using as much space as possible.
1365 * Use MTU if total length of the message fits into the MTU.
1366 * Otherwise, we need to reserve fragment header and
1367 * fragment alignment (= 8-15 octects, in total).
1368 *
1369 * Note that we may need to "move" the data from the tail of
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001370 * of the buffer to the new fragment when we split
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 * the message.
1372 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001373 * FIXME: It may be fragmented into multiple chunks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 * at once if non-fragmentable extension headers
1375 * are too large.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001376 * --yoshfuji
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 */
1378
Hannes Frederic Sowa2811eba2013-09-21 06:27:00 +02001379 cork->length += length;
1380 if (((length > mtu) ||
1381 (skb && skb_is_gso(skb))) &&
1382 (sk->sk_protocol == IPPROTO_UDP) &&
Jakub Sitnickif89c56c2016-10-26 11:21:14 +02001383 (rt->dst.dev->features & NETIF_F_UFO) && !rt->dst.header_len &&
Michal Kubeček40ba3302016-01-11 07:50:30 +01001384 (sk->sk_type == SOCK_DGRAM) && !udp_get_no_check6_tx(sk)) {
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001385 err = ip6_ufo_append_data(sk, queue, getfrag, from, length,
Jakub Sitnicki3ba34582016-04-05 18:41:08 +02001386 hh_len, fragheaderlen, exthdrlen,
Martin KaFai Laufd0273d2015-05-22 20:55:57 -07001387 transhdrlen, mtu, flags, fl6);
Hannes Frederic Sowa2811eba2013-09-21 06:27:00 +02001388 if (err)
1389 goto error;
1390 return 0;
1391 }
1392
1393 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 goto alloc_new_skb;
1395
1396 while (length > 0) {
1397 /* Check if the remaining data fits into current packet. */
David S. Millerbdc712b2011-05-06 15:02:07 -07001398 copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (copy < length)
1400 copy = maxfraglen - skb->len;
1401
1402 if (copy <= 0) {
1403 char *data;
1404 unsigned int datalen;
1405 unsigned int fraglen;
1406 unsigned int fraggap;
1407 unsigned int alloclen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408alloc_new_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 /* There's no room in the current skb */
Gao feng0c183372012-05-26 01:30:53 +00001410 if (skb)
1411 fraggap = skb->len - maxfraglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 else
1413 fraggap = 0;
Gao feng0c183372012-05-26 01:30:53 +00001414 /* update mtu and maxfraglen if necessary */
Ian Morris63159f22015-03-29 14:00:04 +01001415 if (!skb || !skb_prev)
Gao feng0c183372012-05-26 01:30:53 +00001416 ip6_append_data_mtu(&mtu, &maxfraglen,
Hannes Frederic Sowa75a493e2013-07-02 08:04:05 +02001417 fragheaderlen, skb, rt,
luciene367c2d2014-03-17 12:51:01 +08001418 orig_mtu);
Gao feng0c183372012-05-26 01:30:53 +00001419
1420 skb_prev = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 /*
1423 * If remaining data exceeds the mtu,
1424 * we know we need more fragment(s).
1425 */
1426 datalen = length + fraggap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Gao feng0c183372012-05-26 01:30:53 +00001428 if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
1429 datalen = maxfraglen - fragheaderlen - rt->dst.trailer_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if ((flags & MSG_MORE) &&
Changli Gaod8d1f302010-06-10 23:31:35 -07001431 !(rt->dst.dev->features&NETIF_F_SG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 alloclen = mtu;
1433 else
1434 alloclen = datalen + fragheaderlen;
1435
Steffen Klassert299b0762011-10-11 01:43:33 +00001436 alloclen += dst_exthdrlen;
1437
Gao feng0c183372012-05-26 01:30:53 +00001438 if (datalen != length + fraggap) {
1439 /*
1440 * this is not the last fragment, the trailer
1441 * space is regarded as data space.
1442 */
1443 datalen += rt->dst.trailer_len;
1444 }
1445
1446 alloclen += rt->dst.trailer_len;
1447 fraglen = datalen + fragheaderlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 /*
1450 * We just reserve space for fragment header.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001451 * Note: this may be overallocation if the message
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 * (without MSG_MORE) fits into the MTU.
1453 */
1454 alloclen += sizeof(struct frag_hdr);
1455
1456 if (transhdrlen) {
1457 skb = sock_alloc_send_skb(sk,
1458 alloclen + hh_len,
1459 (flags & MSG_DONTWAIT), &err);
1460 } else {
1461 skb = NULL;
1462 if (atomic_read(&sk->sk_wmem_alloc) <=
1463 2 * sk->sk_sndbuf)
1464 skb = sock_wmalloc(sk,
1465 alloclen + hh_len, 1,
1466 sk->sk_allocation);
Ian Morris63159f22015-03-29 14:00:04 +01001467 if (unlikely(!skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 err = -ENOBUFS;
1469 }
Ian Morris63159f22015-03-29 14:00:04 +01001470 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 goto error;
1472 /*
1473 * Fill in the control structures
1474 */
Hannes Frederic Sowa9c9c9ad2013-08-26 12:31:23 +02001475 skb->protocol = htons(ETH_P_IPV6);
Vlad Yasevich32dce962015-01-31 10:40:18 -05001476 skb->ip_summed = csummode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 skb->csum = 0;
Gao feng1f858512012-03-19 22:36:10 +00001478 /* reserve for fragmentation and ipsec header */
1479 skb_reserve(skb, hh_len + sizeof(struct frag_hdr) +
1480 dst_exthdrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Willem de Bruijn11878b42014-07-14 17:55:06 -04001482 /* Only the initial fragment is time stamped */
1483 skb_shinfo(skb)->tx_flags = tx_flags;
1484 tx_flags = 0;
Willem de Bruijn09c2d252014-08-04 22:11:47 -04001485 skb_shinfo(skb)->tskey = tskey;
1486 tskey = 0;
Anders Berggrena693e692011-02-28 12:32:11 -08001487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 /*
1489 * Find where to start putting bytes
1490 */
Gao feng1f858512012-03-19 22:36:10 +00001491 data = skb_put(skb, fraglen);
1492 skb_set_network_header(skb, exthdrlen);
1493 data += fragheaderlen;
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001494 skb->transport_header = (skb->network_header +
1495 fragheaderlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (fraggap) {
1497 skb->csum = skb_copy_and_csum_bits(
1498 skb_prev, maxfraglen,
1499 data + transhdrlen, fraggap, 0);
1500 skb_prev->csum = csum_sub(skb_prev->csum,
1501 skb->csum);
1502 data += fraggap;
Herbert Xue9fa4f72006-08-13 20:12:58 -07001503 pskb_trim_unique(skb_prev, maxfraglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
1505 copy = datalen - transhdrlen - fraggap;
Steffen Klassert299b0762011-10-11 01:43:33 +00001506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (copy < 0) {
1508 err = -EINVAL;
1509 kfree_skb(skb);
1510 goto error;
1511 } else if (copy > 0 && getfrag(from, data + transhdrlen, offset, copy, fraggap, skb) < 0) {
1512 err = -EFAULT;
1513 kfree_skb(skb);
1514 goto error;
1515 }
1516
1517 offset += copy;
1518 length -= datalen - fraggap;
1519 transhdrlen = 0;
1520 exthdrlen = 0;
Steffen Klassert299b0762011-10-11 01:43:33 +00001521 dst_exthdrlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 /*
1524 * Put the packet on the pending queue
1525 */
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001526 __skb_queue_tail(queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 continue;
1528 }
1529
1530 if (copy > length)
1531 copy = length;
1532
Changli Gaod8d1f302010-06-10 23:31:35 -07001533 if (!(rt->dst.dev->features&NETIF_F_SG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 unsigned int off;
1535
1536 off = skb->len;
1537 if (getfrag(from, skb_put(skb, copy),
1538 offset, copy, off, skb) < 0) {
1539 __skb_trim(skb, off);
1540 err = -EFAULT;
1541 goto error;
1542 }
1543 } else {
1544 int i = skb_shinfo(skb)->nr_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Eric Dumazet5640f762012-09-23 23:04:42 +00001546 err = -ENOMEM;
1547 if (!sk_page_frag_refill(sk, pfrag))
1548 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Eric Dumazet5640f762012-09-23 23:04:42 +00001550 if (!skb_can_coalesce(skb, i, pfrag->page,
1551 pfrag->offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 err = -EMSGSIZE;
Eric Dumazet5640f762012-09-23 23:04:42 +00001553 if (i == MAX_SKB_FRAGS)
1554 goto error;
1555
1556 __skb_fill_page_desc(skb, i, pfrag->page,
1557 pfrag->offset, 0);
1558 skb_shinfo(skb)->nr_frags = ++i;
1559 get_page(pfrag->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
Eric Dumazet5640f762012-09-23 23:04:42 +00001561 copy = min_t(int, copy, pfrag->size - pfrag->offset);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001562 if (getfrag(from,
Eric Dumazet5640f762012-09-23 23:04:42 +00001563 page_address(pfrag->page) + pfrag->offset,
1564 offset, copy, skb->len, skb) < 0)
1565 goto error_efault;
1566
1567 pfrag->offset += copy;
1568 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 skb->len += copy;
1570 skb->data_len += copy;
Herbert Xuf945fa72008-01-22 22:39:26 -08001571 skb->truesize += copy;
1572 atomic_add(copy, &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574 offset += copy;
1575 length -= copy;
1576 }
Eric Dumazet5640f762012-09-23 23:04:42 +00001577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return 0;
Eric Dumazet5640f762012-09-23 23:04:42 +00001579
1580error_efault:
1581 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582error:
David S. Millerbdc712b2011-05-06 15:02:07 -07001583 cork->length -= length;
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001584 IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return err;
1586}
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001587
1588int ip6_append_data(struct sock *sk,
1589 int getfrag(void *from, char *to, int offset, int len,
1590 int odd, struct sk_buff *skb),
Wei Wang26879da2016-05-02 21:40:07 -07001591 void *from, int length, int transhdrlen,
1592 struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
1593 struct rt6_info *rt, unsigned int flags,
Soheil Hassas Yeganehc14ac942016-04-02 23:08:12 -04001594 const struct sockcm_cookie *sockc)
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001595{
1596 struct inet_sock *inet = inet_sk(sk);
1597 struct ipv6_pinfo *np = inet6_sk(sk);
1598 int exthdrlen;
1599 int err;
1600
1601 if (flags&MSG_PROBE)
1602 return 0;
1603 if (skb_queue_empty(&sk->sk_write_queue)) {
1604 /*
1605 * setup for corking
1606 */
Wei Wang26879da2016-05-02 21:40:07 -07001607 err = ip6_setup_cork(sk, &inet->cork, &np->cork,
1608 ipc6, rt, fl6);
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001609 if (err)
1610 return err;
1611
Wei Wang26879da2016-05-02 21:40:07 -07001612 exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0);
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001613 length += exthdrlen;
1614 transhdrlen += exthdrlen;
1615 } else {
1616 fl6 = &inet->cork.fl.u.ip6;
1617 transhdrlen = 0;
1618 }
1619
1620 return __ip6_append_data(sk, fl6, &sk->sk_write_queue, &inet->cork.base,
1621 &np->cork, sk_page_frag(sk), getfrag,
Wei Wang26879da2016-05-02 21:40:07 -07001622 from, length, transhdrlen, flags, ipc6, sockc);
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001623}
Chris Elstona495f832012-04-29 21:48:53 +00001624EXPORT_SYMBOL_GPL(ip6_append_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001626static void ip6_cork_release(struct inet_cork_full *cork,
1627 struct inet6_cork *v6_cork)
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001628{
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001629 if (v6_cork->opt) {
1630 kfree(v6_cork->opt->dst0opt);
1631 kfree(v6_cork->opt->dst1opt);
1632 kfree(v6_cork->opt->hopopt);
1633 kfree(v6_cork->opt->srcrt);
1634 kfree(v6_cork->opt);
1635 v6_cork->opt = NULL;
Herbert Xu0178b692009-02-05 15:15:50 -08001636 }
1637
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001638 if (cork->base.dst) {
1639 dst_release(cork->base.dst);
1640 cork->base.dst = NULL;
1641 cork->base.flags &= ~IPCORK_ALLFRAG;
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001642 }
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001643 memset(&cork->fl, 0, sizeof(cork->fl));
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001644}
1645
Vlad Yasevich64223982015-01-31 10:40:15 -05001646struct sk_buff *__ip6_make_skb(struct sock *sk,
1647 struct sk_buff_head *queue,
1648 struct inet_cork_full *cork,
1649 struct inet6_cork *v6_cork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 struct sk_buff *skb, *tmp_skb;
1652 struct sk_buff **tail_skb;
1653 struct in6_addr final_dst_buf, *final_dst = &final_dst_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 struct ipv6_pinfo *np = inet6_sk(sk);
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001655 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 struct ipv6hdr *hdr;
Vlad Yasevich64223982015-01-31 10:40:15 -05001657 struct ipv6_txoptions *opt = v6_cork->opt;
1658 struct rt6_info *rt = (struct rt6_info *)cork->base.dst;
1659 struct flowi6 *fl6 = &cork->fl.u.ip6;
David S. Miller4c9483b2011-03-12 16:22:43 -05001660 unsigned char proto = fl6->flowi6_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Vlad Yasevich64223982015-01-31 10:40:15 -05001662 skb = __skb_dequeue(queue);
Ian Morris63159f22015-03-29 14:00:04 +01001663 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 goto out;
1665 tail_skb = &(skb_shinfo(skb)->frag_list);
1666
1667 /* move skb->data to ip header from ext header */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001668 if (skb->data < skb_network_header(skb))
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001669 __skb_pull(skb, skb_network_offset(skb));
Vlad Yasevich64223982015-01-31 10:40:15 -05001670 while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001671 __skb_pull(tmp_skb, skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 *tail_skb = tmp_skb;
1673 tail_skb = &(tmp_skb->next);
1674 skb->len += tmp_skb->len;
1675 skb->data_len += tmp_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 skb->truesize += tmp_skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 tmp_skb->destructor = NULL;
1678 tmp_skb->sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680
Herbert Xu28a894532008-02-12 18:07:27 -08001681 /* Allow local fragmentation. */
WANG Cong60ff7462014-05-04 16:39:18 -07001682 skb->ignore_df = ip6_sk_ignore_df(sk);
Herbert Xu28a894532008-02-12 18:07:27 -08001683
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001684 *final_dst = fl6->daddr;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001685 __skb_pull(skb, skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (opt && opt->opt_flen)
1687 ipv6_push_frag_opts(skb, opt, &proto);
1688 if (opt && opt->opt_nflen)
1689 ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst);
1690
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07001691 skb_push(skb, sizeof(struct ipv6hdr));
1692 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001693 hdr = ipv6_hdr(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001694
Vlad Yasevich64223982015-01-31 10:40:15 -05001695 ip6_flow_hdr(hdr, v6_cork->tclass,
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001696 ip6_make_flowlabel(net, skb, fl6->flowlabel,
Tom Herbert67800f92015-07-31 16:52:11 -07001697 np->autoflowlabel, fl6));
Vlad Yasevich64223982015-01-31 10:40:15 -05001698 hdr->hop_limit = v6_cork->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 hdr->nexthdr = proto;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001700 hdr->saddr = fl6->saddr;
1701 hdr->daddr = *final_dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Patrick McHardya2c20642006-01-08 22:37:26 -08001703 skb->priority = sk->sk_priority;
Laszlo Attila Toth4a19ec52008-01-30 19:08:16 -08001704 skb->mark = sk->sk_mark;
Patrick McHardya2c20642006-01-08 22:37:26 -08001705
Changli Gaod8d1f302010-06-10 23:31:35 -07001706 skb_dst_set(skb, dst_clone(&rt->dst));
Neil Hormanedf391f2009-04-27 02:45:02 -07001707 IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
David L Stevens14878f72007-09-16 16:52:35 -07001708 if (proto == IPPROTO_ICMPV6) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001709 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
David L Stevens14878f72007-09-16 16:52:35 -07001710
Hannes Frederic Sowa43a43b62014-03-31 20:14:10 +02001711 ICMP6MSGOUT_INC_STATS(net, idev, icmp6_hdr(skb)->icmp6_type);
1712 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
David L Stevens14878f72007-09-16 16:52:35 -07001713 }
1714
Vlad Yasevich64223982015-01-31 10:40:15 -05001715 ip6_cork_release(cork, v6_cork);
1716out:
1717 return skb;
1718}
1719
1720int ip6_send_skb(struct sk_buff *skb)
1721{
1722 struct net *net = sock_net(skb->sk);
1723 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
1724 int err;
1725
Eric W. Biederman33224b12015-10-07 16:48:46 -05001726 err = ip6_local_out(net, skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (err) {
1728 if (err > 0)
Eric Dumazet6ce9e7b2009-09-02 18:05:33 -07001729 err = net_xmit_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 if (err)
Vlad Yasevich64223982015-01-31 10:40:15 -05001731 IP6_INC_STATS(net, rt->rt6i_idev,
1732 IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 }
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return err;
Vlad Yasevich64223982015-01-31 10:40:15 -05001736}
1737
1738int ip6_push_pending_frames(struct sock *sk)
1739{
1740 struct sk_buff *skb;
1741
1742 skb = ip6_finish_skb(sk);
1743 if (!skb)
1744 return 0;
1745
1746 return ip6_send_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
Chris Elstona495f832012-04-29 21:48:53 +00001748EXPORT_SYMBOL_GPL(ip6_push_pending_frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001750static void __ip6_flush_pending_frames(struct sock *sk,
Vlad Yasevich64223982015-01-31 10:40:15 -05001751 struct sk_buff_head *queue,
1752 struct inet_cork_full *cork,
1753 struct inet6_cork *v6_cork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 struct sk_buff *skb;
1756
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001757 while ((skb = __skb_dequeue_tail(queue)) != NULL) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001758 if (skb_dst(skb))
1759 IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakie1f522082007-09-11 11:31:43 +02001760 IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 kfree_skb(skb);
1762 }
1763
Vlad Yasevich64223982015-01-31 10:40:15 -05001764 ip6_cork_release(cork, v6_cork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001766
1767void ip6_flush_pending_frames(struct sock *sk)
1768{
Vlad Yasevich64223982015-01-31 10:40:15 -05001769 __ip6_flush_pending_frames(sk, &sk->sk_write_queue,
1770 &inet_sk(sk)->cork, &inet6_sk(sk)->cork);
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001771}
Chris Elstona495f832012-04-29 21:48:53 +00001772EXPORT_SYMBOL_GPL(ip6_flush_pending_frames);
Vlad Yasevich64223982015-01-31 10:40:15 -05001773
1774struct sk_buff *ip6_make_skb(struct sock *sk,
1775 int getfrag(void *from, char *to, int offset,
1776 int len, int odd, struct sk_buff *skb),
1777 void *from, int length, int transhdrlen,
Wei Wang26879da2016-05-02 21:40:07 -07001778 struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
Vlad Yasevich64223982015-01-31 10:40:15 -05001779 struct rt6_info *rt, unsigned int flags,
Wei Wang26879da2016-05-02 21:40:07 -07001780 const struct sockcm_cookie *sockc)
Vlad Yasevich64223982015-01-31 10:40:15 -05001781{
1782 struct inet_cork_full cork;
1783 struct inet6_cork v6_cork;
1784 struct sk_buff_head queue;
Wei Wang26879da2016-05-02 21:40:07 -07001785 int exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0);
Vlad Yasevich64223982015-01-31 10:40:15 -05001786 int err;
1787
1788 if (flags & MSG_PROBE)
1789 return NULL;
1790
1791 __skb_queue_head_init(&queue);
1792
1793 cork.base.flags = 0;
1794 cork.base.addr = 0;
1795 cork.base.opt = NULL;
1796 v6_cork.opt = NULL;
Wei Wang26879da2016-05-02 21:40:07 -07001797 err = ip6_setup_cork(sk, &cork, &v6_cork, ipc6, rt, fl6);
Vlad Yasevich64223982015-01-31 10:40:15 -05001798 if (err)
1799 return ERR_PTR(err);
1800
Wei Wang26879da2016-05-02 21:40:07 -07001801 if (ipc6->dontfrag < 0)
1802 ipc6->dontfrag = inet6_sk(sk)->dontfrag;
Vlad Yasevich64223982015-01-31 10:40:15 -05001803
1804 err = __ip6_append_data(sk, fl6, &queue, &cork.base, &v6_cork,
1805 &current->task_frag, getfrag, from,
1806 length + exthdrlen, transhdrlen + exthdrlen,
Wei Wang26879da2016-05-02 21:40:07 -07001807 flags, ipc6, sockc);
Vlad Yasevich64223982015-01-31 10:40:15 -05001808 if (err) {
1809 __ip6_flush_pending_frames(sk, &queue, &cork, &v6_cork);
1810 return ERR_PTR(err);
1811 }
1812
1813 return __ip6_make_skb(sk, &queue, &cork, &v6_cork);
1814}