blob: 077d43af8226bd202be878885eaaf274776bf648 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * IPv6 output functions
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09007 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * Based on linux/net/ipv4/ip_output.c
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Changes:
12 * A.N.Kuznetsov : airthmetics in fragmentation.
13 * extension headers are implemented.
14 * route changes now work.
15 * ip6_forward does not confuse sniffers.
16 * etc.
17 *
18 * H. von Brand : Added missing #include <linux/string.h>
Ian Morris67ba4152014-08-24 21:53:10 +010019 * Imran Patel : frag id should be in NBO
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 * Kazunori MIYAZAWA @USAGI
21 * : add ip6_append_data and related functions
22 * for datagram xmit
23 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/errno.h>
Herbert Xuef76bc22008-01-11 19:15:08 -080026#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/string.h>
28#include <linux/socket.h>
29#include <linux/net.h>
30#include <linux/netdevice.h>
31#include <linux/if_arp.h>
32#include <linux/in6.h>
33#include <linux/tcp.h>
34#include <linux/route.h>
Herbert Xub59f45d2006-05-27 23:05:54 -070035#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Daniel Mack33b48672016-11-23 16:52:29 +010038#include <linux/bpf-cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/netfilter.h>
40#include <linux/netfilter_ipv6.h>
41
42#include <net/sock.h>
43#include <net/snmp.h>
44
45#include <net/ipv6.h>
46#include <net/ndisc.h>
47#include <net/protocol.h>
48#include <net/ip6_route.h>
49#include <net/addrconf.h>
50#include <net/rawv6.h>
51#include <net/icmp.h>
52#include <net/xfrm.h>
53#include <net/checksum.h>
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090054#include <linux/mroute6.h>
David Ahernca254492015-10-12 11:47:10 -070055#include <net/l3mdev.h>
Roopa Prabhu14972cb2016-08-24 20:10:43 -070056#include <net/lwtunnel.h>
Martin Varghese571912c2020-02-24 10:57:50 +053057#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -050059static int ip6_finish_output2(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Eric Dumazetadf30902009-06-02 05:19:30 +000061 struct dst_entry *dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct net_device *dev = dst->dev;
Nicolas Dichtel9b1c1ef2019-06-24 16:01:08 +020063 const struct in6_addr *nexthop;
David S. Millerf6b72b622011-07-14 07:53:20 -070064 struct neighbour *neigh;
YOSHIFUJI Hideaki / 吉藤英明6fd6ce22013-01-17 12:54:00 +000065 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070067 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
Eric Dumazetadf30902009-06-02 05:19:30 +000068 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
David Miller7026b1d2015-04-05 22:19:04 -040070 if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(sk) &&
Yuval Mintz8571ab42018-02-28 23:29:30 +020071 ((mroute6_is_socket(net, skb) &&
Benjamin Therybd91b8b2008-12-10 16:07:08 -080072 !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) ||
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +090073 ipv6_chk_mcast_addr(dev, &ipv6_hdr(skb)->daddr,
74 &ipv6_hdr(skb)->saddr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
76
77 /* Do not check for IFF_ALLMULTI; multicast routing
78 is not supported in any case.
79 */
80 if (newskb)
Jan Engelhardtb2e0b382010-03-23 04:09:07 +010081 NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING,
Eric W. Biederman29a26a52015-09-15 20:04:16 -050082 net, sk, newskb, NULL, newskb->dev,
Michel Machado95603e22012-06-12 10:16:35 +000083 dev_loopback_xmit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070085 if (ipv6_hdr(skb)->hop_limit == 0) {
Eric W. Biederman78126c42015-09-15 20:04:09 -050086 IP6_INC_STATS(net, idev,
Denis V. Lunev3bd653c2008-10-08 10:54:51 -070087 IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 kfree_skb(skb);
89 return 0;
90 }
91 }
92
Eric W. Biederman78126c42015-09-15 20:04:09 -050093 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, skb->len);
Hannes Frederic Sowadd408512013-02-10 02:33:35 +000094
95 if (IPV6_ADDR_MC_SCOPE(&ipv6_hdr(skb)->daddr) <=
96 IPV6_ADDR_SCOPE_NODELOCAL &&
97 !(dev->flags & IFF_LOOPBACK)) {
98 kfree_skb(skb);
99 return 0;
100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102
Roopa Prabhu14972cb2016-08-24 20:10:43 -0700103 if (lwtunnel_xmit_redirect(dst->lwtstate)) {
104 int res = lwtunnel_xmit(skb);
105
106 if (res < 0 || res == LWTUNNEL_XMIT_DONE)
107 return res;
108 }
109
YOSHIFUJI Hideaki / 吉藤英明6fd6ce22013-01-17 12:54:00 +0000110 rcu_read_lock_bh();
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700111 nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr);
YOSHIFUJI Hideaki / 吉藤英明6fd6ce22013-01-17 12:54:00 +0000112 neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
113 if (unlikely(!neigh))
114 neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
115 if (!IS_ERR(neigh)) {
Julian Anastasov4ff06202017-02-06 23:14:12 +0200116 sock_confirm_neigh(skb, neigh);
David Ahern0353f282019-04-05 16:30:33 -0700117 ret = neigh_output(neigh, skb, false);
YOSHIFUJI Hideaki / 吉藤英明6fd6ce22013-01-17 12:54:00 +0000118 rcu_read_unlock_bh();
119 return ret;
120 }
121 rcu_read_unlock_bh();
David S. Miller05e3aa02011-07-16 17:26:00 -0700122
Eric W. Biederman78126c42015-09-15 20:04:09 -0500123 IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
Jan Engelhardt9e508492010-04-13 15:28:11 +0200124 kfree_skb(skb);
125 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Aya Levinb210de42021-01-07 15:50:18 +0200128static int
129ip6_finish_output_gso_slowpath_drop(struct net *net, struct sock *sk,
130 struct sk_buff *skb, unsigned int mtu)
131{
132 struct sk_buff *segs, *nskb;
133 netdev_features_t features;
134 int ret = 0;
135
136 /* Please see corresponding comment in ip_finish_output_gso
137 * describing the cases where GSO segment length exceeds the
138 * egress MTU.
139 */
140 features = netif_skb_features(skb);
141 segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
142 if (IS_ERR_OR_NULL(segs)) {
143 kfree_skb(skb);
144 return -ENOMEM;
145 }
146
147 consume_skb(skb);
148
149 skb_list_walk_safe(segs, segs, nskb) {
150 int err;
151
152 skb_mark_not_on_list(segs);
153 err = ip6_fragment(net, sk, segs, ip6_finish_output2);
154 if (err && ret == 0)
155 ret = err;
156 }
157
158 return ret;
159}
160
brakmo956fe212019-05-28 16:59:38 -0700161static int __ip6_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Jan Engelhardt9e508492010-04-13 15:28:11 +0200162{
Aya Levinb210de42021-01-07 15:50:18 +0200163 unsigned int mtu;
164
Tobias Brunner09ee9dba2017-12-21 17:32:24 +0100165#if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
166 /* Policy lookup after SNAT yielded a new policy */
167 if (skb_dst(skb)->xfrm) {
168 IPCB(skb)->flags |= IPSKB_REROUTED;
169 return dst_output(net, sk, skb);
170 }
171#endif
172
Aya Levinb210de42021-01-07 15:50:18 +0200173 mtu = ip6_skb_dst_mtu(skb);
174 if (skb_is_gso(skb) && !skb_gso_validate_network_len(skb, mtu))
175 return ip6_finish_output_gso_slowpath_drop(net, sk, skb, mtu);
176
177 if ((skb->len > mtu && !skb_is_gso(skb)) ||
Jiri Pirko9037c352013-11-06 17:52:19 +0100178 dst_allfrag(skb_dst(skb)) ||
179 (IP6CB(skb)->frag_max_size && skb->len > IP6CB(skb)->frag_max_size))
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500180 return ip6_fragment(net, sk, skb, ip6_finish_output2);
Jan Engelhardt9e508492010-04-13 15:28:11 +0200181 else
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500182 return ip6_finish_output2(net, sk, skb);
Jan Engelhardt9e508492010-04-13 15:28:11 +0200183}
184
brakmo956fe212019-05-28 16:59:38 -0700185static int ip6_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
186{
187 int ret;
188
189 ret = BPF_CGROUP_RUN_PROG_INET_EGRESS(sk, skb);
190 switch (ret) {
191 case NET_XMIT_SUCCESS:
192 return __ip6_finish_output(net, sk, skb);
193 case NET_XMIT_CN:
194 return __ip6_finish_output(net, sk, skb) ? : ret;
195 default:
196 kfree_skb(skb);
197 return ret;
198 }
199}
200
Eric W. Biedermanede20592015-10-07 16:48:47 -0500201int ip6_output(struct net *net, struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Phil Sutter28f8bfd2019-11-12 17:14:37 +0100203 struct net_device *dev = skb_dst(skb)->dev, *indev = skb->dev;
Eric Dumazetadf30902009-06-02 05:19:30 +0000204 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
Eric W. Biedermanbe10de0a2015-09-17 17:21:31 -0500205
Chenbo Feng97a7a372017-06-09 12:06:07 -0700206 skb->protocol = htons(ETH_P_IPV6);
207 skb->dev = dev;
208
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900209 if (unlikely(idev->cnf.disable_ipv6)) {
Eric W. Biederman19a06442015-09-15 20:04:10 -0500210 IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS);
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900211 kfree_skb(skb);
212 return 0;
213 }
214
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500215 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
Phil Sutter28f8bfd2019-11-12 17:14:37 +0100216 net, sk, skb, indev, dev,
Jan Engelhardt9c6eb282010-04-13 15:32:16 +0200217 ip6_finish_output,
218 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Ben Hutchingse9191ff2018-01-22 20:06:42 +0000221bool ip6_autoflowlabel(struct net *net, const struct ipv6_pinfo *np)
Shaohua Li513674b2017-12-20 12:10:21 -0800222{
223 if (!np->autoflowlabel_set)
224 return ip6_default_np_autolabel(net);
225 else
226 return np->autoflowlabel;
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229/*
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700230 * xmit an sk_buff (used by TCP, SCTP and DCCP)
231 * Note : socket lock is not held for SYNACK packets, but might be modified
232 * by calls to skb_set_owner_w() and ipv6_local_error(),
233 * which are using proper atomic operations or spinlocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 */
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700235int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
Eric Dumazet4f6570d2019-09-24 08:01:14 -0700236 __u32 mark, struct ipv6_txoptions *opt, int tclass, u32 priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700238 struct net *net = sock_net(sk);
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700239 const struct ipv6_pinfo *np = inet6_sk(sk);
David S. Miller4c9483b2011-03-12 16:22:43 -0500240 struct in6_addr *first_hop = &fl6->daddr;
Eric Dumazetadf30902009-06-02 05:19:30 +0000241 struct dst_entry *dst = skb_dst(skb);
Stefano Brivio66033f42018-12-06 19:30:36 +0100242 unsigned int head_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 struct ipv6hdr *hdr;
David S. Miller4c9483b2011-03-12 16:22:43 -0500244 u8 proto = fl6->flowi6_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 int seg_len = skb->len;
Gerrit Renkere651f032009-08-09 08:12:48 +0000246 int hlimit = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 u32 mtu;
248
Stefano Brivio66033f42018-12-06 19:30:36 +0100249 head_room = sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev);
250 if (opt)
251 head_room += opt->opt_nflen + opt->opt_flen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Stefano Brivio66033f42018-12-06 19:30:36 +0100253 if (unlikely(skb_headroom(skb) < head_room)) {
254 struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room);
255 if (!skb2) {
256 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
257 IPSTATS_MIB_OUTDISCARDS);
258 kfree_skb(skb);
259 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Stefano Brivio66033f42018-12-06 19:30:36 +0100261 if (skb->sk)
262 skb_set_owner_w(skb2, skb->sk);
263 consume_skb(skb);
264 skb = skb2;
265 }
266
267 if (opt) {
268 seg_len += opt->opt_nflen + opt->opt_flen;
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (opt->opt_flen)
271 ipv6_push_frag_opts(skb, opt, &proto);
Stefano Brivio66033f42018-12-06 19:30:36 +0100272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (opt->opt_nflen)
David Lebrun613fa3c2016-11-08 14:59:20 +0100274 ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop,
275 &fl6->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
277
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -0700278 skb_push(skb, sizeof(struct ipv6hdr));
279 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700280 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 /*
283 * Fill in the IPv6 header
284 */
Eric Dumazetb903d322011-10-27 00:44:35 -0400285 if (np)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 hlimit = np->hop_limit;
287 if (hlimit < 0)
YOSHIFUJI Hideaki6b75d092008-03-10 06:00:30 -0400288 hlimit = ip6_dst_hoplimit(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Tom Herbertcb1ce2e2014-07-01 21:33:10 -0700290 ip6_flow_hdr(hdr, tclass, ip6_make_flowlabel(net, skb, fl6->flowlabel,
Shaohua Li513674b2017-12-20 12:10:21 -0800291 ip6_autoflowlabel(net, np), fl6));
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 hdr->payload_len = htons(seg_len);
294 hdr->nexthdr = proto;
295 hdr->hop_limit = hlimit;
296
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000297 hdr->saddr = fl6->saddr;
298 hdr->daddr = *first_hop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Hannes Frederic Sowa9c9c9ad52013-08-26 12:31:23 +0200300 skb->protocol = htons(ETH_P_IPV6);
Eric Dumazet4f6570d2019-09-24 08:01:14 -0700301 skb->priority = priority;
Pablo Neira92e55f42017-01-26 22:56:21 +0100302 skb->mark = mark;
Patrick McHardya2c20642006-01-08 22:37:26 -0800303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 mtu = dst_mtu(dst);
WANG Cong60ff7462014-05-04 16:39:18 -0700305 if ((skb->len <= mtu) || skb->ignore_df || skb_is_gso(skb)) {
Eric Dumazetadf30902009-06-02 05:19:30 +0000306 IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)),
Neil Hormanedf391f2009-04-27 02:45:02 -0700307 IPSTATS_MIB_OUT, skb->len);
David Aherna8e3e1a2016-09-10 12:09:53 -0700308
309 /* if egress device is enslaved to an L3 master device pass the
310 * skb to its handler for processing
311 */
312 skb = l3mdev_ip6_out((struct sock *)sk, skb);
313 if (unlikely(!skb))
314 return 0;
315
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700316 /* hooks should never assume socket lock is held.
317 * we promote our socket to non const
318 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500319 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700320 net, (struct sock *)sk, skb, NULL, dst->dev,
Eric W. Biederman13206b62015-10-07 16:48:35 -0500321 dst_output);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 skb->dev = dst->dev;
Eric Dumazet1c1e9d22015-09-25 07:39:20 -0700325 /* ipv6_local_error() does not require socket lock,
326 * we promote our socket to non const
327 */
328 ipv6_local_error((struct sock *)sk, EMSGSIZE, fl6, mtu);
329
Eric Dumazetadf30902009-06-02 05:19:30 +0000330 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 kfree_skb(skb);
332 return -EMSGSIZE;
333}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900334EXPORT_SYMBOL(ip6_xmit);
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static int ip6_call_ra_chain(struct sk_buff *skb, int sel)
337{
338 struct ip6_ra_chain *ra;
339 struct sock *last = NULL;
340
341 read_lock(&ip6_ra_lock);
342 for (ra = ip6_ra_chain; ra; ra = ra->next) {
343 struct sock *sk = ra->sk;
Andrew McDonald0bd1b592005-08-09 19:44:42 -0700344 if (sk && ra->sel == sel &&
345 (!sk->sk_bound_dev_if ||
346 sk->sk_bound_dev_if == skb->dev->ifindex)) {
Francesco Ruggeri9036b2f2019-03-01 15:31:03 -0800347 struct ipv6_pinfo *np = inet6_sk(sk);
348
349 if (np && np->rtalert_isolate &&
350 !net_eq(sock_net(sk), dev_net(skb->dev))) {
351 continue;
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 if (last) {
354 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
355 if (skb2)
356 rawv6_rcv(last, skb2);
357 }
358 last = sk;
359 }
360 }
361
362 if (last) {
363 rawv6_rcv(last, skb);
364 read_unlock(&ip6_ra_lock);
365 return 1;
366 }
367 read_unlock(&ip6_ra_lock);
368 return 0;
369}
370
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700371static int ip6_forward_proxy_check(struct sk_buff *skb)
372{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700373 struct ipv6hdr *hdr = ipv6_hdr(skb);
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700374 u8 nexthdr = hdr->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -0800375 __be16 frag_off;
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700376 int offset;
377
378 if (ipv6_ext_hdr(nexthdr)) {
Jesse Gross75f28112011-11-30 17:05:51 -0800379 offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr, &frag_off);
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700380 if (offset < 0)
381 return 0;
382 } else
383 offset = sizeof(struct ipv6hdr);
384
385 if (nexthdr == IPPROTO_ICMPV6) {
386 struct icmp6hdr *icmp6;
387
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700388 if (!pskb_may_pull(skb, (skb_network_header(skb) +
389 offset + 1 - skb->data)))
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700390 return 0;
391
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700392 icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700393
394 switch (icmp6->icmp6_type) {
395 case NDISC_ROUTER_SOLICITATION:
396 case NDISC_ROUTER_ADVERTISEMENT:
397 case NDISC_NEIGHBOUR_SOLICITATION:
398 case NDISC_NEIGHBOUR_ADVERTISEMENT:
399 case NDISC_REDIRECT:
400 /* For reaction involving unicast neighbor discovery
401 * message destined to the proxied address, pass it to
402 * input function.
403 */
404 return 1;
405 default:
406 break;
407 }
408 }
409
Ville Nuorvala74553b02006-09-22 14:42:18 -0700410 /*
411 * The proxying router can't forward traffic sent to a link-local
412 * address, so signal the sender and discard the packet. This
413 * behavior is clarified by the MIPv6 specification.
414 */
415 if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) {
416 dst_link_failure(skb);
417 return -1;
418 }
419
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700420 return 0;
421}
422
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500423static inline int ip6_forward_finish(struct net *net, struct sock *sk,
424 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Jeff Barnhill71a1c912018-04-05 21:29:47 +0000426 struct dst_entry *dst = skb_dst(skb);
427
428 __IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS);
429 __IP6_ADD_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTOCTETS, skb->len);
430
Ido Schimmelf839a6c2018-12-04 08:15:11 +0000431#ifdef CONFIG_NET_SWITCHDEV
432 if (skb->offload_l3_fwd_mark) {
433 consume_skb(skb);
434 return 0;
435 }
436#endif
437
Eric Dumazet8203e2d2018-12-14 06:46:49 -0800438 skb->tstamp = 0;
Eric W. Biederman13206b62015-10-07 16:48:35 -0500439 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Florian Westphalfe6cc552014-02-13 23:09:12 +0100442static bool ip6_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
443{
Florian Westphal418a3152014-05-05 00:03:34 +0200444 if (skb->len <= mtu)
Florian Westphalfe6cc552014-02-13 23:09:12 +0100445 return false;
446
WANG Cong60ff7462014-05-04 16:39:18 -0700447 /* ipv6 conntrack defrag sets max_frag_size + ignore_df */
Florian Westphalfe6cc552014-02-13 23:09:12 +0100448 if (IP6CB(skb)->frag_max_size && IP6CB(skb)->frag_max_size > mtu)
449 return true;
450
WANG Cong60ff7462014-05-04 16:39:18 -0700451 if (skb->ignore_df)
Florian Westphal418a3152014-05-05 00:03:34 +0200452 return false;
453
Daniel Axtens779b7932018-03-01 17:13:37 +1100454 if (skb_is_gso(skb) && skb_gso_validate_network_len(skb, mtu))
Florian Westphalfe6cc552014-02-13 23:09:12 +0100455 return false;
456
457 return true;
458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460int ip6_forward(struct sk_buff *skb)
461{
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -0400462 struct inet6_dev *idev = __in6_dev_get_safely(skb->dev);
Eric Dumazetadf30902009-06-02 05:19:30 +0000463 struct dst_entry *dst = skb_dst(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700464 struct ipv6hdr *hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 struct inet6_skb_parm *opt = IP6CB(skb);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900466 struct net *net = dev_net(dst->dev);
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800467 u32 mtu;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900468
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700469 if (net->ipv6.devconf_all->forwarding == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 goto error;
471
Li RongQing090f1162014-03-11 10:40:08 +0800472 if (skb->pkt_type != PACKET_HOST)
473 goto drop;
474
Hannes Frederic Sowa9ef2e962015-10-08 18:19:53 +0200475 if (unlikely(skb->sk))
476 goto drop;
477
Ben Hutchings4497b072008-06-19 16:22:28 -0700478 if (skb_warn_if_lro(skb))
479 goto drop;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -0400482 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 goto drop;
484 }
485
Herbert Xu35fc92a2007-03-26 23:22:20 -0700486 skb_forward_csum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 /*
489 * We DO NOT make any processing on
490 * RA packets, pushing them to user level AS IS
491 * without ane WARRANTY that application will be able
492 * to interpret them. The reason is that we
493 * cannot make anything clever here.
494 *
495 * We are not end-node, so that if packet contains
496 * AH/ESP, we cannot make anything.
497 * Defragmentation also would be mistake, RA packets
498 * cannot be fragmented, because there is no warranty
499 * that different fragments will go along one path. --ANK
500 */
YOSHIFUJI Hideaki / 吉藤英明ab4eb352013-06-22 11:13:13 +0900501 if (unlikely(opt->flags & IP6SKB_ROUTERALERT)) {
502 if (ip6_call_ra_chain(skb, ntohs(opt->ra)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return 0;
504 }
505
506 /*
507 * check and decrement ttl
508 */
509 if (hdr->hop_limit <= 1) {
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000510 icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0);
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -0400511 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 kfree_skb(skb);
514 return -ETIMEDOUT;
515 }
516
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700517 /* XXX: idev->cnf.proxy_ndp? */
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -0700518 if (net->ipv6.devconf_all->proxy_ndp &&
Daniel Lezcano8a3edd82008-03-07 11:14:16 -0800519 pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0)) {
Ville Nuorvala74553b02006-09-22 14:42:18 -0700520 int proxied = ip6_forward_proxy_check(skb);
521 if (proxied > 0)
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700522 return ip6_input(skb);
Ville Nuorvala74553b02006-09-22 14:42:18 -0700523 else if (proxied < 0) {
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -0400524 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
Ville Nuorvala74553b02006-09-22 14:42:18 -0700525 goto drop;
526 }
Ville Nuorvalae21e0b52006-09-22 14:41:44 -0700527 }
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (!xfrm6_route_forward(skb)) {
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -0400530 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto drop;
532 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000533 dst = skb_dst(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 /* IPv6 specs say nothing about it, but it is clear that we cannot
536 send redirects to source routed frames.
Masahide NAKAMURA1e5dc142007-08-24 19:08:55 +0900537 We don't send redirects to frames decapsulated from IPsec.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 */
Stephen Suryaputra2f17bec2018-06-01 00:05:21 -0400539 if (IP6CB(skb)->iif == dst->dev->ifindex &&
540 opt->srcrt == 0 && !skb_sec_path(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct in6_addr *target = NULL;
David S. Millerfbfe95a2012-06-08 23:24:18 -0700542 struct inet_peer *peer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /*
546 * incoming and outgoing devices are the same
547 * send a redirect.
548 */
549
550 rt = (struct rt6_info *) dst;
David S. Millerc45a3df2012-01-27 15:32:19 -0800551 if (rt->rt6i_flags & RTF_GATEWAY)
552 target = &rt->rt6i_gateway;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 else
554 target = &hdr->daddr;
555
Martin KaFai Laufd0273d2015-05-22 20:55:57 -0700556 peer = inet_getpeer_v6(net->ipv6.peers, &hdr->daddr, 1);
David S. Miller92d86822011-02-04 15:55:25 -0800557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /* Limit redirects both by destination (here)
559 and by source (inside ndisc_send_redirect)
560 */
David S. Millerfbfe95a2012-06-08 23:24:18 -0700561 if (inet_peer_xrlim_allow(peer, 1*HZ))
David S. Miller49919692012-01-27 15:30:48 -0800562 ndisc_send_redirect(skb, target);
David S. Miller1d861aa2012-07-10 03:58:16 -0700563 if (peer)
564 inet_putpeer(peer);
David L Stevens5bb1ab02007-05-09 13:53:44 -0700565 } else {
566 int addrtype = ipv6_addr_type(&hdr->saddr);
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* This check is security critical. */
YOSHIFUJI Hideakif81b2e72008-06-25 16:55:26 +0900569 if (addrtype == IPV6_ADDR_ANY ||
570 addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
David L Stevens5bb1ab02007-05-09 13:53:44 -0700571 goto error;
572 if (addrtype & IPV6_ADDR_LINKLOCAL) {
573 icmpv6_send(skb, ICMPV6_DEST_UNREACH,
Alexey Dobriyan3ffe5332010-02-18 08:25:24 +0000574 ICMPV6_NOT_NEIGHBOUR, 0);
David L Stevens5bb1ab02007-05-09 13:53:44 -0700575 goto error;
576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578
Hannes Frederic Sowa0954cf92014-01-09 10:01:16 +0100579 mtu = ip6_dst_mtu_forward(dst);
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800580 if (mtu < IPV6_MIN_MTU)
581 mtu = IPV6_MIN_MTU;
582
Florian Westphalfe6cc552014-02-13 23:09:12 +0100583 if (ip6_pkt_too_big(skb, mtu)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /* Again, force OUTPUT device used as source address */
585 skb->dev = dst->dev;
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800586 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -0400587 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INTOOBIGERRORS);
Eric Dumazet1d015502016-04-27 16:44:40 -0700588 __IP6_INC_STATS(net, ip6_dst_idev(dst),
589 IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 kfree_skb(skb);
591 return -EMSGSIZE;
592 }
593
594 if (skb_cow(skb, dst->dev->hard_header_len)) {
Eric Dumazet1d015502016-04-27 16:44:40 -0700595 __IP6_INC_STATS(net, ip6_dst_idev(dst),
596 IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 goto drop;
598 }
599
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700600 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /* Mangling hops number delayed to point after skb COW */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 hdr->hop_limit--;
605
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500606 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD,
607 net, NULL, skb, skb->dev, dst->dev,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800608 ip6_forward_finish);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610error:
Stephen Suryaputrabdb7cc62018-04-16 13:42:16 -0400611 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INADDRERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612drop:
613 kfree_skb(skb);
614 return -EINVAL;
615}
616
617static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
618{
619 to->pkt_type = from->pkt_type;
620 to->priority = from->priority;
621 to->protocol = from->protocol;
Eric Dumazetadf30902009-06-02 05:19:30 +0000622 skb_dst_drop(to);
623 skb_dst_set(to, dst_clone(skb_dst(from)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 to->dev = from->dev;
Thomas Graf82e91ff2006-11-09 15:19:14 -0800625 to->mark = from->mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Paolo Abeni3dd1c9a2018-07-23 16:50:48 +0200627 skb_copy_hash(to, from);
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629#ifdef CONFIG_NET_SCHED
630 to->tc_index = from->tc_index;
631#endif
Yasuyuki Kozakaie7ac05f2007-03-14 16:44:01 -0700632 nf_copy(to, from);
Florian Westphaldf5042f2018-12-18 17:15:16 +0100633 skb_ext_copy(to, from);
James Morris984bc162006-06-09 00:29:17 -0700634 skb_copy_secmark(to, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Pablo Neira Ayuso0feca612019-05-29 13:25:32 +0200637int ip6_fraglist_init(struct sk_buff *skb, unsigned int hlen, u8 *prevhdr,
638 u8 nexthdr, __be32 frag_id,
639 struct ip6_fraglist_iter *iter)
640{
641 unsigned int first_len;
642 struct frag_hdr *fh;
643
644 /* BUILD HEADER */
645 *prevhdr = NEXTHDR_FRAGMENT;
646 iter->tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC);
647 if (!iter->tmp_hdr)
648 return -ENOMEM;
649
Eric Dumazetb7034142019-06-02 11:24:18 -0700650 iter->frag = skb_shinfo(skb)->frag_list;
Pablo Neira Ayuso0feca612019-05-29 13:25:32 +0200651 skb_frag_list_init(skb);
652
653 iter->offset = 0;
654 iter->hlen = hlen;
655 iter->frag_id = frag_id;
656 iter->nexthdr = nexthdr;
657
658 __skb_pull(skb, hlen);
659 fh = __skb_push(skb, sizeof(struct frag_hdr));
660 __skb_push(skb, hlen);
661 skb_reset_network_header(skb);
662 memcpy(skb_network_header(skb), iter->tmp_hdr, hlen);
663
664 fh->nexthdr = nexthdr;
665 fh->reserved = 0;
666 fh->frag_off = htons(IP6_MF);
667 fh->identification = frag_id;
668
669 first_len = skb_pagelen(skb);
670 skb->data_len = first_len - skb_headlen(skb);
671 skb->len = first_len;
672 ipv6_hdr(skb)->payload_len = htons(first_len - sizeof(struct ipv6hdr));
673
674 return 0;
675}
676EXPORT_SYMBOL(ip6_fraglist_init);
677
678void ip6_fraglist_prepare(struct sk_buff *skb,
679 struct ip6_fraglist_iter *iter)
680{
681 struct sk_buff *frag = iter->frag;
682 unsigned int hlen = iter->hlen;
683 struct frag_hdr *fh;
684
685 frag->ip_summed = CHECKSUM_NONE;
686 skb_reset_transport_header(frag);
687 fh = __skb_push(frag, sizeof(struct frag_hdr));
688 __skb_push(frag, hlen);
689 skb_reset_network_header(frag);
690 memcpy(skb_network_header(frag), iter->tmp_hdr, hlen);
691 iter->offset += skb->len - hlen - sizeof(struct frag_hdr);
692 fh->nexthdr = iter->nexthdr;
693 fh->reserved = 0;
694 fh->frag_off = htons(iter->offset);
695 if (frag->next)
696 fh->frag_off |= htons(IP6_MF);
697 fh->identification = iter->frag_id;
698 ipv6_hdr(frag)->payload_len = htons(frag->len - sizeof(struct ipv6hdr));
699 ip6_copy_metadata(frag, skb);
700}
701EXPORT_SYMBOL(ip6_fraglist_prepare);
702
Pablo Neira Ayuso8a6a1f12019-05-29 13:25:34 +0200703void ip6_frag_init(struct sk_buff *skb, unsigned int hlen, unsigned int mtu,
704 unsigned short needed_tailroom, int hdr_room, u8 *prevhdr,
705 u8 nexthdr, __be32 frag_id, struct ip6_frag_state *state)
706{
707 state->prevhdr = prevhdr;
708 state->nexthdr = nexthdr;
709 state->frag_id = frag_id;
710
711 state->hlen = hlen;
712 state->mtu = mtu;
713
714 state->left = skb->len - hlen; /* Space per frame */
715 state->ptr = hlen; /* Where to start from */
716
717 state->hroom = hdr_room;
718 state->troom = needed_tailroom;
719
720 state->offset = 0;
721}
722EXPORT_SYMBOL(ip6_frag_init);
723
724struct sk_buff *ip6_frag_next(struct sk_buff *skb, struct ip6_frag_state *state)
725{
726 u8 *prevhdr = state->prevhdr, *fragnexthdr_offset;
727 struct sk_buff *frag;
728 struct frag_hdr *fh;
729 unsigned int len;
730
731 len = state->left;
732 /* IF: it doesn't fit, use 'mtu' - the data space left */
733 if (len > state->mtu)
734 len = state->mtu;
735 /* IF: we are not sending up to and including the packet end
736 then align the next start on an eight byte boundary */
737 if (len < state->left)
738 len &= ~7;
739
740 /* Allocate buffer */
741 frag = alloc_skb(len + state->hlen + sizeof(struct frag_hdr) +
742 state->hroom + state->troom, GFP_ATOMIC);
743 if (!frag)
744 return ERR_PTR(-ENOMEM);
745
746 /*
747 * Set up data on packet
748 */
749
750 ip6_copy_metadata(frag, skb);
751 skb_reserve(frag, state->hroom);
752 skb_put(frag, len + state->hlen + sizeof(struct frag_hdr));
753 skb_reset_network_header(frag);
754 fh = (struct frag_hdr *)(skb_network_header(frag) + state->hlen);
755 frag->transport_header = (frag->network_header + state->hlen +
756 sizeof(struct frag_hdr));
757
758 /*
759 * Charge the memory for the fragment to any owner
760 * it might possess
761 */
762 if (skb->sk)
763 skb_set_owner_w(frag, skb->sk);
764
765 /*
766 * Copy the packet header into the new buffer.
767 */
768 skb_copy_from_linear_data(skb, skb_network_header(frag), state->hlen);
769
770 fragnexthdr_offset = skb_network_header(frag);
771 fragnexthdr_offset += prevhdr - skb_network_header(skb);
772 *fragnexthdr_offset = NEXTHDR_FRAGMENT;
773
774 /*
775 * Build fragment header.
776 */
777 fh->nexthdr = state->nexthdr;
778 fh->reserved = 0;
779 fh->identification = state->frag_id;
780
781 /*
782 * Copy a block of the IP datagram.
783 */
784 BUG_ON(skb_copy_bits(skb, state->ptr, skb_transport_header(frag),
785 len));
786 state->left -= len;
787
788 fh->frag_off = htons(state->offset);
789 if (state->left > 0)
790 fh->frag_off |= htons(IP6_MF);
791 ipv6_hdr(frag)->payload_len = htons(frag->len - sizeof(struct ipv6hdr));
792
793 state->ptr += len;
794 state->offset += len;
795
796 return frag;
797}
798EXPORT_SYMBOL(ip6_frag_next);
799
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500800int ip6_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
801 int (*output)(struct net *, struct sock *, struct sk_buff *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct sk_buff *frag;
Ian Morris67ba4152014-08-24 21:53:10 +0100804 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
hannes@stressinduktion.orgf60e5992015-04-01 17:07:44 +0200805 struct ipv6_pinfo *np = skb->sk && !dev_recursion_level() ?
806 inet6_sk(skb->sk) : NULL;
Pablo Neira Ayuso8a6a1f12019-05-29 13:25:34 +0200807 struct ip6_frag_state state;
808 unsigned int mtu, hlen, nexthdr_offset;
Eric Dumazet9669fff2019-10-16 18:00:56 -0700809 ktime_t tstamp = skb->tstamp;
Pablo Neira Ayuso8a6a1f12019-05-29 13:25:34 +0200810 int hroom, err = 0;
Martin KaFai Lau286c2342015-05-22 20:55:56 -0700811 __be32 frag_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 u8 *prevhdr, nexthdr = 0;
813
David S. Miller7dd7eb92017-05-17 22:54:11 -0400814 err = ip6_find_1stfragopt(skb, &prevhdr);
815 if (err < 0)
Craig Gallek24234962017-05-16 14:36:23 -0400816 goto fail;
David S. Miller7dd7eb92017-05-17 22:54:11 -0400817 hlen = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 nexthdr = *prevhdr;
Junwei Huef0efcd2019-04-02 19:38:04 +0800819 nexthdr_offset = prevhdr - skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
John Heffner628a5c52007-04-20 15:53:27 -0700821 mtu = ip6_skb_dst_mtu(skb);
John Heffnerb881ef72007-04-20 15:52:39 -0700822
823 /* We must not fragment if the socket is set to force MTU discovery
Ulrich Weber14f3ad62010-02-26 04:34:49 -0800824 * or if the skb it not generated by a local socket.
John Heffnerb881ef72007-04-20 15:52:39 -0700825 */
Florian Westphal485fca62015-05-22 00:44:16 +0200826 if (unlikely(!skb->ignore_df && skb->len > mtu))
827 goto fail_toobig;
Eric Dumazeta34a1012012-05-18 21:51:44 +0000828
Florian Westphal485fca62015-05-22 00:44:16 +0200829 if (IP6CB(skb)->frag_max_size) {
830 if (IP6CB(skb)->frag_max_size > mtu)
831 goto fail_toobig;
832
833 /* don't send fragments larger than what we received */
834 mtu = IP6CB(skb)->frag_max_size;
835 if (mtu < IPV6_MIN_MTU)
836 mtu = IPV6_MIN_MTU;
John Heffnerb881ef72007-04-20 15:52:39 -0700837 }
838
YOSHIFUJI Hideakid91675f2006-02-24 13:18:33 -0800839 if (np && np->frag_size < mtu) {
840 if (np->frag_size)
841 mtu = np->frag_size;
842 }
Hannes Frederic Sowa89bc7842015-10-28 13:21:04 +0100843 if (mtu < hlen + sizeof(struct frag_hdr) + 8)
Hannes Frederic Sowab72a2b02015-10-16 11:32:43 +0200844 goto fail_toobig;
Hannes Frederic Sowa1e0d69a2015-10-28 13:21:03 +0100845 mtu -= hlen + sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Martin KaFai Laufd0273d2015-05-22 20:55:57 -0700847 frag_id = ipv6_select_ident(net, &ipv6_hdr(skb)->daddr,
848 &ipv6_hdr(skb)->saddr);
Martin KaFai Lau286c2342015-05-22 20:55:56 -0700849
Hannes Frederic Sowa405c92f2015-10-27 22:40:42 +0100850 if (skb->ip_summed == CHECKSUM_PARTIAL &&
851 (err = skb_checksum_help(skb)))
852 goto fail;
853
Junwei Huef0efcd2019-04-02 19:38:04 +0800854 prevhdr = skb_network_header(skb) + nexthdr_offset;
Florian Westphal1d325d22015-09-16 17:26:14 +0200855 hroom = LL_RESERVED_SPACE(rt->dst.dev);
David S. Miller21dc3302010-08-23 00:13:46 -0700856 if (skb_has_frag_list(skb)) {
Alexey Dobriyanc72d8cd2016-11-19 04:08:08 +0300857 unsigned int first_len = skb_pagelen(skb);
Pablo Neira Ayuso0feca612019-05-29 13:25:32 +0200858 struct ip6_fraglist_iter iter;
Eric Dumazet3d130082010-09-21 08:47:45 +0000859 struct sk_buff *frag2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 if (first_len - hlen > mtu ||
862 ((first_len - hlen) & 7) ||
Florian Westphal1d325d22015-09-16 17:26:14 +0200863 skb_cloned(skb) ||
864 skb_headroom(skb) < (hroom + sizeof(struct frag_hdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 goto slow_path;
866
David S. Miller4d9092b2009-06-09 00:20:05 -0700867 skb_walk_frags(skb, frag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* Correct geometry. */
869 if (frag->len > mtu ||
870 ((frag->len & 7) && frag->next) ||
Florian Westphal1d325d22015-09-16 17:26:14 +0200871 skb_headroom(frag) < (hlen + hroom + sizeof(struct frag_hdr)))
Eric Dumazet3d130082010-09-21 08:47:45 +0000872 goto slow_path_clean;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 /* Partially cloned skb? */
875 if (skb_shared(frag))
Eric Dumazet3d130082010-09-21 08:47:45 +0000876 goto slow_path_clean;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700877
878 BUG_ON(frag->sk);
879 if (skb->sk) {
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700880 frag->sk = skb->sk;
881 frag->destructor = sock_wfree;
Herbert Xu2fdba6b2005-05-18 22:52:33 -0700882 }
Eric Dumazet3d130082010-09-21 08:47:45 +0000883 skb->truesize -= frag->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885
Pablo Neira Ayuso0feca612019-05-29 13:25:32 +0200886 err = ip6_fraglist_init(skb, hlen, prevhdr, nexthdr, frag_id,
887 &iter);
888 if (err < 0)
Florian Westphal1d325d22015-09-16 17:26:14 +0200889 goto fail;
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 for (;;) {
892 /* Prepare header of the next frame,
893 * before previous one went down. */
Pablo Neira Ayuso0feca612019-05-29 13:25:32 +0200894 if (iter.frag)
895 ip6_fraglist_prepare(skb, &iter);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900896
Eric Dumazet9669fff2019-10-16 18:00:56 -0700897 skb->tstamp = tstamp;
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500898 err = output(net, sk, skb);
Ian Morris67ba4152014-08-24 21:53:10 +0100899 if (!err)
Changli Gaod8d1f302010-06-10 23:31:35 -0700900 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700901 IPSTATS_MIB_FRAGCREATES);
Wei Dongdafee492006-08-02 13:41:21 -0700902
Pablo Neira Ayuso0feca612019-05-29 13:25:32 +0200903 if (err || !iter.frag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 break;
905
Pablo Neira Ayuso0feca612019-05-29 13:25:32 +0200906 skb = ip6_fraglist_next(&iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908
Pablo Neira Ayuso0feca612019-05-29 13:25:32 +0200909 kfree(iter.tmp_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 if (err == 0) {
Changli Gaod8d1f302010-06-10 23:31:35 -0700912 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700913 IPSTATS_MIB_FRAGOKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return 0;
915 }
916
Eric Dumazetb7034142019-06-02 11:24:18 -0700917 kfree_skb_list(iter.frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Changli Gaod8d1f302010-06-10 23:31:35 -0700919 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700920 IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return err;
Eric Dumazet3d130082010-09-21 08:47:45 +0000922
923slow_path_clean:
924 skb_walk_frags(skb, frag2) {
925 if (frag2 == frag)
926 break;
927 frag2->sk = NULL;
928 frag2->destructor = NULL;
929 skb->truesize += frag2->truesize;
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932
933slow_path:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 /*
935 * Fragment the datagram.
936 */
937
Pablo Neira Ayuso8a6a1f12019-05-29 13:25:34 +0200938 ip6_frag_init(skb, hlen, mtu, rt->dst.dev->needed_tailroom,
939 LL_RESERVED_SPACE(rt->dst.dev), prevhdr, nexthdr, frag_id,
940 &state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 /*
943 * Keep copying data until we run out.
944 */
Florian Westphal79e49502017-03-13 16:24:28 +0100945
Pablo Neira Ayuso8a6a1f12019-05-29 13:25:34 +0200946 while (state.left > 0) {
947 frag = ip6_frag_next(skb, &state);
948 if (IS_ERR(frag)) {
949 err = PTR_ERR(frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 goto fail;
951 }
952
953 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 * Put this fragment into the sending queue.
955 */
Eric Dumazet9669fff2019-10-16 18:00:56 -0700956 frag->tstamp = tstamp;
Eric W. Biederman7d8c6e32015-06-12 22:12:04 -0500957 err = output(net, sk, frag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (err)
959 goto fail;
Wei Dongdafee492006-08-02 13:41:21 -0700960
Eric Dumazetadf30902009-06-02 05:19:30 +0000961 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
Denis V. Lunev3bd653c2008-10-08 10:54:51 -0700962 IPSTATS_MIB_FRAGCREATES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
Eric Dumazetadf30902009-06-02 05:19:30 +0000964 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900965 IPSTATS_MIB_FRAGOKS);
Eric Dumazet808db802012-04-24 10:17:59 +0000966 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return err;
968
Florian Westphal485fca62015-05-22 00:44:16 +0200969fail_toobig:
970 if (skb->sk && dst_allfrag(skb_dst(skb)))
971 sk_nocaps_add(skb->sk, NETIF_F_GSO_MASK);
972
Florian Westphal485fca62015-05-22 00:44:16 +0200973 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
974 err = -EMSGSIZE;
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976fail:
Eric Dumazetadf30902009-06-02 05:19:30 +0000977 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakia11d2062006-11-04 20:11:37 +0900978 IPSTATS_MIB_FRAGFAILS);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900979 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return err;
981}
982
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000983static inline int ip6_rt_check(const struct rt6key *rt_key,
984 const struct in6_addr *fl_addr,
985 const struct in6_addr *addr_cache)
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700986{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000987 return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
Ian Morris63159f22015-03-29 14:00:04 +0100988 (!addr_cache || !ipv6_addr_equal(fl_addr, addr_cache));
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -0700989}
990
Herbert Xu497c6152006-07-30 20:19:33 -0700991static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
992 struct dst_entry *dst,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000993 const struct flowi6 *fl6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Herbert Xu497c6152006-07-30 20:19:33 -0700995 struct ipv6_pinfo *np = inet6_sk(sk);
Eric Dumazeta963a372013-06-26 04:15:07 -0700996 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Herbert Xu497c6152006-07-30 20:19:33 -0700998 if (!dst)
999 goto out;
1000
Eric Dumazeta963a372013-06-26 04:15:07 -07001001 if (dst->ops->family != AF_INET6) {
1002 dst_release(dst);
1003 return NULL;
1004 }
1005
1006 rt = (struct rt6_info *)dst;
Herbert Xu497c6152006-07-30 20:19:33 -07001007 /* Yes, checking route validity in not connected
1008 * case is not very simple. Take into account,
1009 * that we do not support routing by source, TOS,
Ian Morris67ba4152014-08-24 21:53:10 +01001010 * and MSG_DONTROUTE --ANK (980726)
Herbert Xu497c6152006-07-30 20:19:33 -07001011 *
YOSHIFUJI Hideakicf6b1982006-08-23 17:19:18 -07001012 * 1. ip6_rt_check(): If route was host route,
1013 * check that cached destination is current.
Herbert Xu497c6152006-07-30 20:19:33 -07001014 * If it is network route, we still may
1015 * check its validity using saved pointer
1016 * to the last used address: daddr_cache.
1017 * We do not want to save whole address now,
1018 * (because main consumer of this service
1019 * is tcp, which has not this problem),
1020 * so that the last trick works only on connected
1021 * sockets.
1022 * 2. oif also should be the same.
1023 */
David S. Miller4c9483b2011-03-12 16:22:43 -05001024 if (ip6_rt_check(&rt->rt6i_dst, &fl6->daddr, np->daddr_cache) ||
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -07001025#ifdef CONFIG_IPV6_SUBTREES
David S. Miller4c9483b2011-03-12 16:22:43 -05001026 ip6_rt_check(&rt->rt6i_src, &fl6->saddr, np->saddr_cache) ||
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -07001027#endif
David Ahernca254492015-10-12 11:47:10 -07001028 (!(fl6->flowi6_flags & FLOWI_FLAG_SKIP_NH_OIF) &&
1029 (fl6->flowi6_oif && fl6->flowi6_oif != dst->dev->ifindex))) {
Herbert Xu497c6152006-07-30 20:19:33 -07001030 dst_release(dst);
1031 dst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033
Herbert Xu497c6152006-07-30 20:19:33 -07001034out:
1035 return dst;
1036}
1037
Eric Dumazet3aef9342015-09-25 07:39:12 -07001038static int ip6_dst_lookup_tail(struct net *net, const struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -05001039 struct dst_entry **dst, struct flowi6 *fl6)
Herbert Xu497c6152006-07-30 20:19:33 -07001040{
David S. Miller69cce1d2011-07-17 23:09:49 -07001041#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1042 struct neighbour *n;
David S. Miller97cac082012-07-02 22:43:47 -07001043 struct rt6_info *rt;
David S. Miller69cce1d2011-07-17 23:09:49 -07001044#endif
1045 int err;
Paolo Abeni6f21c962016-01-29 12:30:19 +01001046 int flags = 0;
Herbert Xu497c6152006-07-30 20:19:33 -07001047
Markus Stenberge16e8882015-05-05 13:36:59 +03001048 /* The correct way to handle this would be to do
1049 * ip6_route_get_saddr, and then ip6_route_output; however,
1050 * the route-specific preferred source forces the
1051 * ip6_route_output call _before_ ip6_route_get_saddr.
1052 *
1053 * In source specific routing (no src=any default route),
1054 * ip6_route_output will fail given src=any saddr, though, so
1055 * that's why we try it again later.
1056 */
1057 if (ipv6_addr_any(&fl6->saddr) && (!*dst || !(*dst)->error)) {
David Aherna68886a2018-04-20 15:38:02 -07001058 struct fib6_info *from;
Markus Stenberge16e8882015-05-05 13:36:59 +03001059 struct rt6_info *rt;
1060 bool had_dst = *dst != NULL;
1061
1062 if (!had_dst)
1063 *dst = ip6_route_output(net, sk, fl6);
1064 rt = (*dst)->error ? NULL : (struct rt6_info *)*dst;
David Aherna68886a2018-04-20 15:38:02 -07001065
1066 rcu_read_lock();
1067 from = rt ? rcu_dereference(rt->from) : NULL;
1068 err = ip6_route_get_saddr(net, from, &fl6->daddr,
Markus Stenberge16e8882015-05-05 13:36:59 +03001069 sk ? inet6_sk(sk)->srcprefs : 0,
1070 &fl6->saddr);
David Aherna68886a2018-04-20 15:38:02 -07001071 rcu_read_unlock();
1072
Markus Stenberge16e8882015-05-05 13:36:59 +03001073 if (err)
1074 goto out_err_release;
1075
1076 /* If we had an erroneous initial result, pretend it
1077 * never existed and let the SA-enabled version take
1078 * over.
1079 */
1080 if (!had_dst && (*dst)->error) {
1081 dst_release(*dst);
1082 *dst = NULL;
1083 }
Paolo Abeni6f21c962016-01-29 12:30:19 +01001084
1085 if (fl6->flowi6_oif)
1086 flags |= RT6_LOOKUP_F_IFACE;
Markus Stenberge16e8882015-05-05 13:36:59 +03001087 }
1088
Ian Morris63159f22015-03-29 14:00:04 +01001089 if (!*dst)
Paolo Abeni6f21c962016-01-29 12:30:19 +01001090 *dst = ip6_route_output_flags(net, sk, fl6, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Ian Morrise5d08d72014-11-23 21:28:43 +00001092 err = (*dst)->error;
1093 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 goto out_err_release;
1095
Neil Horman95c385b2007-04-25 17:08:10 -07001096#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Neil Hormane550dfb2008-09-09 13:51:35 -07001097 /*
1098 * Here if the dst entry we've looked up
1099 * has a neighbour entry that is in the INCOMPLETE
1100 * state and the src address from the flow is
1101 * marked as OPTIMISTIC, we release the found
1102 * dst entry and replace it instead with the
1103 * dst entry of the nexthop router
1104 */
Eric Dumazetc56bf6f2012-07-06 09:19:05 +02001105 rt = (struct rt6_info *) *dst;
YOSHIFUJI Hideaki / 吉藤英明707be1f2013-01-17 12:53:55 +00001106 rcu_read_lock_bh();
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -07001107 n = __ipv6_neigh_lookup_noref(rt->dst.dev,
1108 rt6_nexthop(rt, &fl6->daddr));
YOSHIFUJI Hideaki / 吉藤英明707be1f2013-01-17 12:53:55 +00001109 err = n && !(n->nud_state & NUD_VALID) ? -EINVAL : 0;
1110 rcu_read_unlock_bh();
1111
1112 if (err) {
Neil Hormane550dfb2008-09-09 13:51:35 -07001113 struct inet6_ifaddr *ifp;
David S. Miller4c9483b2011-03-12 16:22:43 -05001114 struct flowi6 fl_gw6;
Neil Hormane550dfb2008-09-09 13:51:35 -07001115 int redirect;
Neil Horman95c385b2007-04-25 17:08:10 -07001116
David S. Miller4c9483b2011-03-12 16:22:43 -05001117 ifp = ipv6_get_ifaddr(net, &fl6->saddr,
Neil Hormane550dfb2008-09-09 13:51:35 -07001118 (*dst)->dev, 1);
Neil Horman95c385b2007-04-25 17:08:10 -07001119
Neil Hormane550dfb2008-09-09 13:51:35 -07001120 redirect = (ifp && ifp->flags & IFA_F_OPTIMISTIC);
1121 if (ifp)
1122 in6_ifa_put(ifp);
Neil Horman95c385b2007-04-25 17:08:10 -07001123
Neil Hormane550dfb2008-09-09 13:51:35 -07001124 if (redirect) {
1125 /*
1126 * We need to get the dst entry for the
1127 * default router instead
1128 */
1129 dst_release(*dst);
David S. Miller4c9483b2011-03-12 16:22:43 -05001130 memcpy(&fl_gw6, fl6, sizeof(struct flowi6));
1131 memset(&fl_gw6.daddr, 0, sizeof(struct in6_addr));
1132 *dst = ip6_route_output(net, sk, &fl_gw6);
Ian Morrise5d08d72014-11-23 21:28:43 +00001133 err = (*dst)->error;
1134 if (err)
Neil Hormane550dfb2008-09-09 13:51:35 -07001135 goto out_err_release;
Neil Horman95c385b2007-04-25 17:08:10 -07001136 }
Neil Hormane550dfb2008-09-09 13:51:35 -07001137 }
Neil Horman95c385b2007-04-25 17:08:10 -07001138#endif
Jonathan T. Leightonec5e3b02017-02-12 17:26:06 -05001139 if (ipv6_addr_v4mapped(&fl6->saddr) &&
Willem de Bruijn00ea1ce2017-02-18 19:00:45 -05001140 !(ipv6_addr_v4mapped(&fl6->daddr) || ipv6_addr_any(&fl6->daddr))) {
1141 err = -EAFNOSUPPORT;
1142 goto out_err_release;
1143 }
Neil Horman95c385b2007-04-25 17:08:10 -07001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return 0;
1146
1147out_err_release:
1148 dst_release(*dst);
1149 *dst = NULL;
David Ahern8a966fc2016-09-10 12:09:59 -07001150
David Ahern0d240e72016-06-16 16:24:25 -07001151 if (err == -ENETUNREACH)
1152 IP6_INC_STATS(net, NULL, IPSTATS_MIB_OUTNOROUTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return err;
1154}
Adrian Bunk34a0b3c2005-11-29 16:28:56 -08001155
Herbert Xu497c6152006-07-30 20:19:33 -07001156/**
1157 * ip6_dst_lookup - perform route lookup on flow
Andrew Lunnb51cd7c2020-07-13 01:15:03 +02001158 * @net: Network namespace to perform lookup in
Herbert Xu497c6152006-07-30 20:19:33 -07001159 * @sk: socket which provides route info
1160 * @dst: pointer to dst_entry * for result
David S. Miller4c9483b2011-03-12 16:22:43 -05001161 * @fl6: flow to lookup
Herbert Xu497c6152006-07-30 20:19:33 -07001162 *
1163 * This function performs a route lookup on the given flow.
1164 *
1165 * It returns zero on success, or a standard errno code on error.
1166 */
Roopa Prabhu343d60a2015-07-30 13:34:53 -07001167int ip6_dst_lookup(struct net *net, struct sock *sk, struct dst_entry **dst,
1168 struct flowi6 *fl6)
Herbert Xu497c6152006-07-30 20:19:33 -07001169{
1170 *dst = NULL;
Roopa Prabhu343d60a2015-07-30 13:34:53 -07001171 return ip6_dst_lookup_tail(net, sk, dst, fl6);
Herbert Xu497c6152006-07-30 20:19:33 -07001172}
Arnaldo Carvalho de Melo3cf3dc62005-12-13 23:23:20 -08001173EXPORT_SYMBOL_GPL(ip6_dst_lookup);
1174
Herbert Xu497c6152006-07-30 20:19:33 -07001175/**
David S. Miller68d0c6d2011-03-01 13:19:07 -08001176 * ip6_dst_lookup_flow - perform route lookup on flow with ipsec
Andrew Lunnb51cd7c2020-07-13 01:15:03 +02001177 * @net: Network namespace to perform lookup in
David S. Miller68d0c6d2011-03-01 13:19:07 -08001178 * @sk: socket which provides route info
David S. Miller4c9483b2011-03-12 16:22:43 -05001179 * @fl6: flow to lookup
David S. Miller68d0c6d2011-03-01 13:19:07 -08001180 * @final_dst: final destination address for ipsec lookup
David S. Miller68d0c6d2011-03-01 13:19:07 -08001181 *
1182 * This function performs a route lookup on the given flow.
1183 *
1184 * It returns a valid dst pointer on success, or a pointer encoded
1185 * error code.
1186 */
Sabrina Dubrocac4e85f72019-12-04 15:35:52 +01001187struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, struct flowi6 *fl6,
Steffen Klassert0e0d44a2013-08-28 08:04:14 +02001188 const struct in6_addr *final_dst)
David S. Miller68d0c6d2011-03-01 13:19:07 -08001189{
1190 struct dst_entry *dst = NULL;
1191 int err;
1192
Sabrina Dubrocac4e85f72019-12-04 15:35:52 +01001193 err = ip6_dst_lookup_tail(net, sk, &dst, fl6);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001194 if (err)
1195 return ERR_PTR(err);
1196 if (final_dst)
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001197 fl6->daddr = *final_dst;
David S. Miller2774c132011-03-01 14:59:04 -08001198
Sabrina Dubrocac4e85f72019-12-04 15:35:52 +01001199 return xfrm_lookup_route(net, dst, flowi6_to_flowi(fl6), sk, 0);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001200}
1201EXPORT_SYMBOL_GPL(ip6_dst_lookup_flow);
1202
1203/**
1204 * ip6_sk_dst_lookup_flow - perform socket cached route lookup on flow
1205 * @sk: socket which provides the dst cache and route info
David S. Miller4c9483b2011-03-12 16:22:43 -05001206 * @fl6: flow to lookup
David S. Miller68d0c6d2011-03-01 13:19:07 -08001207 * @final_dst: final destination address for ipsec lookup
Alexey Kodanev96818152018-04-03 15:00:08 +03001208 * @connected: whether @sk is connected or not
Herbert Xu497c6152006-07-30 20:19:33 -07001209 *
1210 * This function performs a route lookup on the given flow with the
1211 * possibility of using the cached route in the socket if it is valid.
1212 * It will take the socket dst lock when operating on the dst cache.
1213 * As a result, this function can only be used in process context.
1214 *
Alexey Kodanev96818152018-04-03 15:00:08 +03001215 * In addition, for a connected socket, cache the dst in the socket
1216 * if the current cache is not valid.
1217 *
David S. Miller68d0c6d2011-03-01 13:19:07 -08001218 * It returns a valid dst pointer on success, or a pointer encoded
1219 * error code.
Herbert Xu497c6152006-07-30 20:19:33 -07001220 */
David S. Miller4c9483b2011-03-12 16:22:43 -05001221struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
Alexey Kodanev96818152018-04-03 15:00:08 +03001222 const struct in6_addr *final_dst,
1223 bool connected)
Herbert Xu497c6152006-07-30 20:19:33 -07001224{
David S. Miller68d0c6d2011-03-01 13:19:07 -08001225 struct dst_entry *dst = sk_dst_check(sk, inet6_sk(sk)->dst_cookie);
Herbert Xu497c6152006-07-30 20:19:33 -07001226
David S. Miller4c9483b2011-03-12 16:22:43 -05001227 dst = ip6_sk_dst_check(sk, dst, fl6);
Alexey Kodanev96818152018-04-03 15:00:08 +03001228 if (dst)
1229 return dst;
1230
Sabrina Dubrocac4e85f72019-12-04 15:35:52 +01001231 dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_dst);
Alexey Kodanev96818152018-04-03 15:00:08 +03001232 if (connected && !IS_ERR(dst))
1233 ip6_sk_dst_store_flow(sk, dst_clone(dst), fl6);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001234
Jakub Sitnicki00bc0ef2016-06-08 15:13:34 +02001235 return dst;
Herbert Xu497c6152006-07-30 20:19:33 -07001236}
David S. Miller68d0c6d2011-03-01 13:19:07 -08001237EXPORT_SYMBOL_GPL(ip6_sk_dst_lookup_flow);
Herbert Xu497c6152006-07-30 20:19:33 -07001238
Martin Varghese571912c2020-02-24 10:57:50 +05301239/**
1240 * ip6_dst_lookup_tunnel - perform route lookup on tunnel
1241 * @skb: Packet for which lookup is done
1242 * @dev: Tunnel device
1243 * @net: Network namespace of tunnel device
Andrew Lunnb51cd7c2020-07-13 01:15:03 +02001244 * @sock: Socket which provides route info
Martin Varghese571912c2020-02-24 10:57:50 +05301245 * @saddr: Memory to store the src ip address
1246 * @info: Tunnel information
1247 * @protocol: IP protocol
Andrew Lunnb51cd7c2020-07-13 01:15:03 +02001248 * @use_cache: Flag to enable cache usage
Martin Varghese571912c2020-02-24 10:57:50 +05301249 * This function performs a route lookup on a tunnel
1250 *
1251 * It returns a valid dst pointer and stores src address to be used in
1252 * tunnel in param saddr on success, else a pointer encoded error code.
1253 */
1254
1255struct dst_entry *ip6_dst_lookup_tunnel(struct sk_buff *skb,
1256 struct net_device *dev,
1257 struct net *net,
1258 struct socket *sock,
1259 struct in6_addr *saddr,
1260 const struct ip_tunnel_info *info,
1261 u8 protocol,
1262 bool use_cache)
1263{
1264 struct dst_entry *dst = NULL;
1265#ifdef CONFIG_DST_CACHE
1266 struct dst_cache *dst_cache;
1267#endif
1268 struct flowi6 fl6;
1269 __u8 prio;
1270
1271#ifdef CONFIG_DST_CACHE
1272 dst_cache = (struct dst_cache *)&info->dst_cache;
1273 if (use_cache) {
1274 dst = dst_cache_get_ip6(dst_cache, saddr);
1275 if (dst)
1276 return dst;
1277 }
1278#endif
1279 memset(&fl6, 0, sizeof(fl6));
1280 fl6.flowi6_mark = skb->mark;
1281 fl6.flowi6_proto = protocol;
1282 fl6.daddr = info->key.u.ipv6.dst;
1283 fl6.saddr = info->key.u.ipv6.src;
1284 prio = info->key.tos;
1285 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(prio),
1286 info->key.label);
1287
1288 dst = ipv6_stub->ipv6_dst_lookup_flow(net, sock->sk, &fl6,
1289 NULL);
1290 if (IS_ERR(dst)) {
1291 netdev_dbg(dev, "no route to %pI6\n", &fl6.daddr);
1292 return ERR_PTR(-ENETUNREACH);
1293 }
1294 if (dst->dev == dev) { /* is this necessary? */
1295 netdev_dbg(dev, "circular route to %pI6\n", &fl6.daddr);
1296 dst_release(dst);
1297 return ERR_PTR(-ELOOP);
1298 }
1299#ifdef CONFIG_DST_CACHE
1300 if (use_cache)
1301 dst_cache_set_ip6(dst_cache, dst, &fl6.saddr);
1302#endif
1303 *saddr = fl6.saddr;
1304 return dst;
1305}
1306EXPORT_SYMBOL_GPL(ip6_dst_lookup_tunnel);
1307
Herbert Xu0178b692009-02-05 15:15:50 -08001308static inline struct ipv6_opt_hdr *ip6_opt_dup(struct ipv6_opt_hdr *src,
1309 gfp_t gfp)
1310{
1311 return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
1312}
1313
1314static inline struct ipv6_rt_hdr *ip6_rthdr_dup(struct ipv6_rt_hdr *src,
1315 gfp_t gfp)
1316{
1317 return src ? kmemdup(src, (src->hdrlen + 1) * 8, gfp) : NULL;
1318}
1319
Hannes Frederic Sowa75a493e2013-07-02 08:04:05 +02001320static void ip6_append_data_mtu(unsigned int *mtu,
Gao feng0c183372012-05-26 01:30:53 +00001321 int *maxfraglen,
1322 unsigned int fragheaderlen,
1323 struct sk_buff *skb,
Hannes Frederic Sowa75a493e2013-07-02 08:04:05 +02001324 struct rt6_info *rt,
luciene367c2d2014-03-17 12:51:01 +08001325 unsigned int orig_mtu)
Gao feng0c183372012-05-26 01:30:53 +00001326{
1327 if (!(rt->dst.flags & DST_XFRM_TUNNEL)) {
Ian Morris63159f22015-03-29 14:00:04 +01001328 if (!skb) {
Gao feng0c183372012-05-26 01:30:53 +00001329 /* first fragment, reserve header_len */
luciene367c2d2014-03-17 12:51:01 +08001330 *mtu = orig_mtu - rt->dst.header_len;
Gao feng0c183372012-05-26 01:30:53 +00001331
1332 } else {
1333 /*
1334 * this fragment is not first, the headers
1335 * space is regarded as data space.
1336 */
luciene367c2d2014-03-17 12:51:01 +08001337 *mtu = orig_mtu;
Gao feng0c183372012-05-26 01:30:53 +00001338 }
1339 *maxfraglen = ((*mtu - fragheaderlen) & ~7)
1340 + fragheaderlen - sizeof(struct frag_hdr);
1341 }
1342}
1343
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001344static int ip6_setup_cork(struct sock *sk, struct inet_cork_full *cork,
Wei Wang26879da2016-05-02 21:40:07 -07001345 struct inet6_cork *v6_cork, struct ipcm6_cookie *ipc6,
Willem de Bruijn5fdaa882018-07-06 10:12:57 -04001346 struct rt6_info *rt, struct flowi6 *fl6)
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001347{
1348 struct ipv6_pinfo *np = inet6_sk(sk);
1349 unsigned int mtu;
Wei Wang26879da2016-05-02 21:40:07 -07001350 struct ipv6_txoptions *opt = ipc6->opt;
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001351
1352 /*
1353 * setup for corking
1354 */
1355 if (opt) {
1356 if (WARN_ON(v6_cork->opt))
1357 return -EINVAL;
1358
Eric Dumazet864e2a12017-10-21 12:26:23 -07001359 v6_cork->opt = kzalloc(sizeof(*opt), sk->sk_allocation);
Ian Morris63159f22015-03-29 14:00:04 +01001360 if (unlikely(!v6_cork->opt))
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001361 return -ENOBUFS;
1362
Eric Dumazet864e2a12017-10-21 12:26:23 -07001363 v6_cork->opt->tot_len = sizeof(*opt);
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001364 v6_cork->opt->opt_flen = opt->opt_flen;
1365 v6_cork->opt->opt_nflen = opt->opt_nflen;
1366
1367 v6_cork->opt->dst0opt = ip6_opt_dup(opt->dst0opt,
1368 sk->sk_allocation);
1369 if (opt->dst0opt && !v6_cork->opt->dst0opt)
1370 return -ENOBUFS;
1371
1372 v6_cork->opt->dst1opt = ip6_opt_dup(opt->dst1opt,
1373 sk->sk_allocation);
1374 if (opt->dst1opt && !v6_cork->opt->dst1opt)
1375 return -ENOBUFS;
1376
1377 v6_cork->opt->hopopt = ip6_opt_dup(opt->hopopt,
1378 sk->sk_allocation);
1379 if (opt->hopopt && !v6_cork->opt->hopopt)
1380 return -ENOBUFS;
1381
1382 v6_cork->opt->srcrt = ip6_rthdr_dup(opt->srcrt,
1383 sk->sk_allocation);
1384 if (opt->srcrt && !v6_cork->opt->srcrt)
1385 return -ENOBUFS;
1386
1387 /* need source address above miyazawa*/
1388 }
1389 dst_hold(&rt->dst);
1390 cork->base.dst = &rt->dst;
1391 cork->fl.u.ip6 = *fl6;
Wei Wang26879da2016-05-02 21:40:07 -07001392 v6_cork->hop_limit = ipc6->hlimit;
1393 v6_cork->tclass = ipc6->tclass;
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001394 if (rt->dst.flags & DST_XFRM_TUNNEL)
1395 mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ?
Mike Maloney749439b2018-01-10 12:45:10 -05001396 READ_ONCE(rt->dst.dev->mtu) : dst_mtu(&rt->dst);
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001397 else
1398 mtu = np->pmtudisc >= IPV6_PMTUDISC_PROBE ?
David S. Millerc02b3742018-01-17 00:00:25 -05001399 READ_ONCE(rt->dst.dev->mtu) : dst_mtu(xfrm_dst_path(&rt->dst));
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001400 if (np->frag_size < mtu) {
1401 if (np->frag_size)
1402 mtu = np->frag_size;
1403 }
Mike Maloney749439b2018-01-10 12:45:10 -05001404 if (mtu < IPV6_MIN_MTU)
1405 return -EINVAL;
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001406 cork->base.fragsize = mtu;
Willem de Bruijnfbf47812018-07-06 10:12:59 -04001407 cork->base.gso_size = ipc6->gso_size;
Willem de Bruijn678ca422018-07-06 10:12:58 -04001408 cork->base.tx_flags = 0;
Willem de Bruijnc6af0c22019-09-11 15:50:51 -04001409 cork->base.mark = ipc6->sockc.mark;
Willem de Bruijn678ca422018-07-06 10:12:58 -04001410 sock_tx_timestamp(sk, ipc6->sockc.tsflags, &cork->base.tx_flags);
Willem de Bruijnbec1f6f2018-04-26 13:42:17 -04001411
David Miller0f6c4802017-11-28 15:40:46 -05001412 if (dst_allfrag(xfrm_dst_path(&rt->dst)))
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001413 cork->base.flags |= IPCORK_ALLFRAG;
1414 cork->base.length = 0;
1415
Willem de Bruijn5fdaa882018-07-06 10:12:57 -04001416 cork->base.transmit_time = ipc6->sockc.transmit_time;
Jesus Sanchez-Palenciaa818f752018-07-03 15:42:50 -07001417
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001418 return 0;
1419}
1420
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001421static int __ip6_append_data(struct sock *sk,
1422 struct flowi6 *fl6,
1423 struct sk_buff_head *queue,
1424 struct inet_cork *cork,
1425 struct inet6_cork *v6_cork,
1426 struct page_frag *pfrag,
1427 int getfrag(void *from, char *to, int offset,
1428 int len, int odd, struct sk_buff *skb),
1429 void *from, int length, int transhdrlen,
Willem de Bruijn5fdaa882018-07-06 10:12:57 -04001430 unsigned int flags, struct ipcm6_cookie *ipc6)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Gao feng0c183372012-05-26 01:30:53 +00001432 struct sk_buff *skb, *skb_prev = NULL;
Paolo Abeni10b8a3d2018-03-23 14:47:30 +01001433 unsigned int maxfraglen, fragheaderlen, mtu, orig_mtu, pmtu;
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001434 struct ubuf_info *uarg = NULL;
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001435 int exthdrlen = 0;
1436 int dst_exthdrlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 int hh_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 int copy;
1439 int err;
1440 int offset = 0;
Willem de Bruijn09c2d252014-08-04 22:11:47 -04001441 u32 tskey = 0;
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001442 struct rt6_info *rt = (struct rt6_info *)cork->dst;
1443 struct ipv6_txoptions *opt = v6_cork->opt;
Vlad Yasevich32dce9682015-01-31 10:40:18 -05001444 int csummode = CHECKSUM_NONE;
Hannes Frederic Sowa682b1a92015-10-27 22:40:41 +01001445 unsigned int maxnonfragsize, headersize;
Eric Dumazet1f4c6eb2018-03-31 13:16:26 -07001446 unsigned int wmem_alloc_delta = 0;
Willem de Bruijn100f6d82019-05-30 18:01:21 -04001447 bool paged, extra_uref = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001449 skb = skb_peek_tail(queue);
1450 if (!skb) {
1451 exthdrlen = opt ? opt->opt_flen : 0;
Romain KUNTZ7efdba5b2013-01-16 12:47:40 +00001452 dst_exthdrlen = rt->dst.header_len - rt->rt6i_nfheader_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001454
Willem de Bruijn15e36f52018-04-26 13:42:19 -04001455 paged = !!cork->gso_size;
Willem de Bruijnbec1f6f2018-04-26 13:42:17 -04001456 mtu = cork->gso_size ? IP6_MAX_MTU : cork->fragsize;
luciene367c2d2014-03-17 12:51:01 +08001457 orig_mtu = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Willem de Bruijn678ca422018-07-06 10:12:58 -04001459 if (cork->tx_flags & SKBTX_ANY_SW_TSTAMP &&
1460 sk->sk_tsflags & SOF_TIMESTAMPING_OPT_ID)
1461 tskey = sk->sk_tskey++;
1462
Changli Gaod8d1f302010-06-10 23:31:35 -07001463 hh_len = LL_RESERVED_SPACE(rt->dst.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Masahide NAKAMURAa1b05142007-12-20 20:41:12 -08001465 fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len +
Herbert Xub4ce9272007-11-13 21:33:32 -08001466 (opt ? opt->opt_nflen : 0);
Hannes Frederic Sowa4df98e72013-12-16 12:36:44 +01001467 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen -
1468 sizeof(struct frag_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Hannes Frederic Sowa682b1a92015-10-27 22:40:41 +01001470 headersize = sizeof(struct ipv6hdr) +
1471 (opt ? opt->opt_flen + opt->opt_nflen : 0) +
1472 (dst_allfrag(&rt->dst) ?
1473 sizeof(struct frag_hdr) : 0) +
1474 rt->rt6i_nfheader_len;
Hannes Frederic Sowa4df98e72013-12-16 12:36:44 +01001475
Paolo Abeni10b8a3d2018-03-23 14:47:30 +01001476 /* as per RFC 7112 section 5, the entire IPv6 Header Chain must fit
1477 * the first fragment
1478 */
1479 if (headersize + transhdrlen > mtu)
1480 goto emsgsize;
1481
Wei Wang26879da2016-05-02 21:40:07 -07001482 if (cork->length + length > mtu - headersize && ipc6->dontfrag &&
Hannes Frederic Sowa682b1a92015-10-27 22:40:41 +01001483 (sk->sk_protocol == IPPROTO_UDP ||
1484 sk->sk_protocol == IPPROTO_RAW)) {
1485 ipv6_local_rxpmtu(sk, fl6, mtu - headersize +
1486 sizeof(struct ipv6hdr));
1487 goto emsgsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
1489
Hannes Frederic Sowa682b1a92015-10-27 22:40:41 +01001490 if (ip6_sk_ignore_df(sk))
1491 maxnonfragsize = sizeof(struct ipv6hdr) + IPV6_MAXPLEN;
1492 else
1493 maxnonfragsize = mtu;
1494
1495 if (cork->length + length > maxnonfragsize - headersize) {
1496emsgsize:
Paolo Abeni10b8a3d2018-03-23 14:47:30 +01001497 pmtu = max_t(int, mtu - headersize + sizeof(struct ipv6hdr), 0);
1498 ipv6_local_error(sk, EMSGSIZE, fl6, pmtu);
Hannes Frederic Sowa682b1a92015-10-27 22:40:41 +01001499 return -EMSGSIZE;
1500 }
1501
1502 /* CHECKSUM_PARTIAL only with no extension headers and when
1503 * we are not going to fragment
1504 */
1505 if (transhdrlen && sk->sk_protocol == IPPROTO_UDP &&
1506 headersize == sizeof(struct ipv6hdr) &&
Vlad Yasevich2b89ed62017-01-29 22:52:53 -05001507 length <= mtu - headersize &&
Willem de Bruijnbec1f6f2018-04-26 13:42:17 -04001508 (!(flags & MSG_MORE) || cork->gso_size) &&
Tom Herbertc8cd0982015-12-14 11:19:44 -08001509 rt->dst.dev->features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
Hannes Frederic Sowa682b1a92015-10-27 22:40:41 +01001510 csummode = CHECKSUM_PARTIAL;
1511
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001512 if (flags & MSG_ZEROCOPY && length && sock_flag(sk, SOCK_ZEROCOPY)) {
1513 uarg = sock_zerocopy_realloc(sk, length, skb_zcopy(skb));
1514 if (!uarg)
1515 return -ENOBUFS;
Willem de Bruijn522924b2019-06-07 17:57:48 -04001516 extra_uref = !skb_zcopy(skb); /* only ref on new uarg */
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001517 if (rt->dst.dev->features & NETIF_F_SG &&
1518 csummode == CHECKSUM_PARTIAL) {
1519 paged = true;
1520 } else {
1521 uarg->zerocopy = 0;
Willem de Bruijn52900d22018-11-30 15:32:40 -05001522 skb_zcopy_set(skb, uarg, &extra_uref);
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001523 }
1524 }
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 /*
1527 * Let's try using as much space as possible.
1528 * Use MTU if total length of the message fits into the MTU.
1529 * Otherwise, we need to reserve fragment header and
1530 * fragment alignment (= 8-15 octects, in total).
1531 *
Randy Dunlap634a63e2020-09-17 21:35:17 -07001532 * Note that we may need to "move" the data from the tail
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001533 * of the buffer to the new fragment when we split
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 * the message.
1535 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001536 * FIXME: It may be fragmented into multiple chunks
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 * at once if non-fragmentable extension headers
1538 * are too large.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001539 * --yoshfuji
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 */
1541
Hannes Frederic Sowa2811eba2013-09-21 06:27:00 +02001542 cork->length += length;
Hannes Frederic Sowa2811eba2013-09-21 06:27:00 +02001543 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 goto alloc_new_skb;
1545
1546 while (length > 0) {
1547 /* Check if the remaining data fits into current packet. */
David S. Millerbdc712b2011-05-06 15:02:07 -07001548 copy = (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (copy < length)
1550 copy = maxfraglen - skb->len;
1551
1552 if (copy <= 0) {
1553 char *data;
1554 unsigned int datalen;
1555 unsigned int fraglen;
1556 unsigned int fraggap;
1557 unsigned int alloclen;
Willem de Bruijnaba36932018-11-24 14:21:16 -05001558 unsigned int pagedlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559alloc_new_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 /* There's no room in the current skb */
Gao feng0c183372012-05-26 01:30:53 +00001561 if (skb)
1562 fraggap = skb->len - maxfraglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 else
1564 fraggap = 0;
Gao feng0c183372012-05-26 01:30:53 +00001565 /* update mtu and maxfraglen if necessary */
Ian Morris63159f22015-03-29 14:00:04 +01001566 if (!skb || !skb_prev)
Gao feng0c183372012-05-26 01:30:53 +00001567 ip6_append_data_mtu(&mtu, &maxfraglen,
Hannes Frederic Sowa75a493e2013-07-02 08:04:05 +02001568 fragheaderlen, skb, rt,
luciene367c2d2014-03-17 12:51:01 +08001569 orig_mtu);
Gao feng0c183372012-05-26 01:30:53 +00001570
1571 skb_prev = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 /*
1574 * If remaining data exceeds the mtu,
1575 * we know we need more fragment(s).
1576 */
1577 datalen = length + fraggap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Gao feng0c183372012-05-26 01:30:53 +00001579 if (datalen > (cork->length <= mtu && !(cork->flags & IPCORK_ALLFRAG) ? mtu : maxfraglen) - fragheaderlen)
1580 datalen = maxfraglen - fragheaderlen - rt->dst.trailer_len;
Willem de Bruijn15e36f52018-04-26 13:42:19 -04001581 fraglen = datalen + fragheaderlen;
Willem de Bruijnaba36932018-11-24 14:21:16 -05001582 pagedlen = 0;
Willem de Bruijn15e36f52018-04-26 13:42:19 -04001583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if ((flags & MSG_MORE) &&
Changli Gaod8d1f302010-06-10 23:31:35 -07001585 !(rt->dst.dev->features&NETIF_F_SG))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 alloclen = mtu;
Willem de Bruijn15e36f52018-04-26 13:42:19 -04001587 else if (!paged)
1588 alloclen = fraglen;
1589 else {
1590 alloclen = min_t(int, fraglen, MAX_HEADER);
1591 pagedlen = fraglen - alloclen;
1592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Steffen Klassert299b0762011-10-11 01:43:33 +00001594 alloclen += dst_exthdrlen;
1595
Gao feng0c183372012-05-26 01:30:53 +00001596 if (datalen != length + fraggap) {
1597 /*
1598 * this is not the last fragment, the trailer
1599 * space is regarded as data space.
1600 */
1601 datalen += rt->dst.trailer_len;
1602 }
1603
1604 alloclen += rt->dst.trailer_len;
1605 fraglen = datalen + fragheaderlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 /*
1608 * We just reserve space for fragment header.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001609 * Note: this may be overallocation if the message
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 * (without MSG_MORE) fits into the MTU.
1611 */
1612 alloclen += sizeof(struct frag_hdr);
1613
Willem de Bruijn15e36f52018-04-26 13:42:19 -04001614 copy = datalen - transhdrlen - fraggap - pagedlen;
Eric Dumazet232cd352017-05-19 14:17:48 -07001615 if (copy < 0) {
1616 err = -EINVAL;
1617 goto error;
1618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (transhdrlen) {
1620 skb = sock_alloc_send_skb(sk,
1621 alloclen + hh_len,
1622 (flags & MSG_DONTWAIT), &err);
1623 } else {
1624 skb = NULL;
Eric Dumazet1f4c6eb2018-03-31 13:16:26 -07001625 if (refcount_read(&sk->sk_wmem_alloc) + wmem_alloc_delta <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 2 * sk->sk_sndbuf)
Eric Dumazet1f4c6eb2018-03-31 13:16:26 -07001627 skb = alloc_skb(alloclen + hh_len,
1628 sk->sk_allocation);
Ian Morris63159f22015-03-29 14:00:04 +01001629 if (unlikely(!skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 err = -ENOBUFS;
1631 }
Ian Morris63159f22015-03-29 14:00:04 +01001632 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 goto error;
1634 /*
1635 * Fill in the control structures
1636 */
Hannes Frederic Sowa9c9c9ad52013-08-26 12:31:23 +02001637 skb->protocol = htons(ETH_P_IPV6);
Vlad Yasevich32dce9682015-01-31 10:40:18 -05001638 skb->ip_summed = csummode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 skb->csum = 0;
Gao feng1f858512012-03-19 22:36:10 +00001640 /* reserve for fragmentation and ipsec header */
1641 skb_reserve(skb, hh_len + sizeof(struct frag_hdr) +
1642 dst_exthdrlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 /*
1645 * Find where to start putting bytes
1646 */
Willem de Bruijn15e36f52018-04-26 13:42:19 -04001647 data = skb_put(skb, fraglen - pagedlen);
Gao feng1f858512012-03-19 22:36:10 +00001648 skb_set_network_header(skb, exthdrlen);
1649 data += fragheaderlen;
Arnaldo Carvalho de Melob0e380b12007-04-10 21:21:55 -07001650 skb->transport_header = (skb->network_header +
1651 fragheaderlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (fraggap) {
1653 skb->csum = skb_copy_and_csum_bits(
1654 skb_prev, maxfraglen,
Al Viro8d5930d2020-07-10 20:07:10 -04001655 data + transhdrlen, fraggap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 skb_prev->csum = csum_sub(skb_prev->csum,
1657 skb->csum);
1658 data += fraggap;
Herbert Xue9fa4f72006-08-13 20:12:58 -07001659 pskb_trim_unique(skb_prev, maxfraglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
Eric Dumazet232cd352017-05-19 14:17:48 -07001661 if (copy > 0 &&
1662 getfrag(from, data + transhdrlen, offset,
1663 copy, fraggap, skb) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 err = -EFAULT;
1665 kfree_skb(skb);
1666 goto error;
1667 }
1668
1669 offset += copy;
Willem de Bruijn15e36f52018-04-26 13:42:19 -04001670 length -= copy + transhdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 transhdrlen = 0;
1672 exthdrlen = 0;
Steffen Klassert299b0762011-10-11 01:43:33 +00001673 dst_exthdrlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Willem de Bruijn52900d22018-11-30 15:32:40 -05001675 /* Only the initial fragment is time stamped */
1676 skb_shinfo(skb)->tx_flags = cork->tx_flags;
1677 cork->tx_flags = 0;
1678 skb_shinfo(skb)->tskey = tskey;
1679 tskey = 0;
1680 skb_zcopy_set(skb, uarg, &extra_uref);
1681
Julian Anastasov0dec8792017-02-06 23:14:16 +02001682 if ((flags & MSG_CONFIRM) && !skb_prev)
1683 skb_set_dst_pending_confirm(skb, 1);
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 /*
1686 * Put the packet on the pending queue
1687 */
Eric Dumazet1f4c6eb2018-03-31 13:16:26 -07001688 if (!skb->destructor) {
1689 skb->destructor = sock_wfree;
1690 skb->sk = sk;
1691 wmem_alloc_delta += skb->truesize;
1692 }
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001693 __skb_queue_tail(queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 continue;
1695 }
1696
1697 if (copy > length)
1698 copy = length;
1699
Willem de Bruijn113f99c2018-05-17 13:13:29 -04001700 if (!(rt->dst.dev->features&NETIF_F_SG) &&
1701 skb_tailroom(skb) >= copy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 unsigned int off;
1703
1704 off = skb->len;
1705 if (getfrag(from, skb_put(skb, copy),
1706 offset, copy, off, skb) < 0) {
1707 __skb_trim(skb, off);
1708 err = -EFAULT;
1709 goto error;
1710 }
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001711 } else if (!uarg || !uarg->zerocopy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 int i = skb_shinfo(skb)->nr_frags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Eric Dumazet5640f762012-09-23 23:04:42 +00001714 err = -ENOMEM;
1715 if (!sk_page_frag_refill(sk, pfrag))
1716 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Eric Dumazet5640f762012-09-23 23:04:42 +00001718 if (!skb_can_coalesce(skb, i, pfrag->page,
1719 pfrag->offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 err = -EMSGSIZE;
Eric Dumazet5640f762012-09-23 23:04:42 +00001721 if (i == MAX_SKB_FRAGS)
1722 goto error;
1723
1724 __skb_fill_page_desc(skb, i, pfrag->page,
1725 pfrag->offset, 0);
1726 skb_shinfo(skb)->nr_frags = ++i;
1727 get_page(pfrag->page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
Eric Dumazet5640f762012-09-23 23:04:42 +00001729 copy = min_t(int, copy, pfrag->size - pfrag->offset);
Eric Dumazet9e903e02011-10-18 21:00:24 +00001730 if (getfrag(from,
Eric Dumazet5640f762012-09-23 23:04:42 +00001731 page_address(pfrag->page) + pfrag->offset,
1732 offset, copy, skb->len, skb) < 0)
1733 goto error_efault;
1734
1735 pfrag->offset += copy;
1736 skb_frag_size_add(&skb_shinfo(skb)->frags[i - 1], copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 skb->len += copy;
1738 skb->data_len += copy;
Herbert Xuf945fa72008-01-22 22:39:26 -08001739 skb->truesize += copy;
Eric Dumazet1f4c6eb2018-03-31 13:16:26 -07001740 wmem_alloc_delta += copy;
Willem de Bruijnb5947e52018-11-30 15:32:39 -05001741 } else {
1742 err = skb_zerocopy_iter_dgram(skb, from, copy);
1743 if (err < 0)
1744 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746 offset += copy;
1747 length -= copy;
1748 }
Eric Dumazet5640f762012-09-23 23:04:42 +00001749
Paolo Abeni9e8445a2018-04-04 14:30:01 +02001750 if (wmem_alloc_delta)
1751 refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return 0;
Eric Dumazet5640f762012-09-23 23:04:42 +00001753
1754error_efault:
1755 err = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756error:
Willem de Bruijn97ef7b42018-12-08 06:22:46 -05001757 if (uarg)
1758 sock_zerocopy_put_abort(uarg, extra_uref);
David S. Millerbdc712b2011-05-06 15:02:07 -07001759 cork->length -= length;
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001760 IP6_INC_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
Eric Dumazet1f4c6eb2018-03-31 13:16:26 -07001761 refcount_add(wmem_alloc_delta, &sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return err;
1763}
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001764
1765int ip6_append_data(struct sock *sk,
1766 int getfrag(void *from, char *to, int offset, int len,
1767 int odd, struct sk_buff *skb),
Wei Wang26879da2016-05-02 21:40:07 -07001768 void *from, int length, int transhdrlen,
1769 struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
Willem de Bruijn5fdaa882018-07-06 10:12:57 -04001770 struct rt6_info *rt, unsigned int flags)
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001771{
1772 struct inet_sock *inet = inet_sk(sk);
1773 struct ipv6_pinfo *np = inet6_sk(sk);
1774 int exthdrlen;
1775 int err;
1776
1777 if (flags&MSG_PROBE)
1778 return 0;
1779 if (skb_queue_empty(&sk->sk_write_queue)) {
1780 /*
1781 * setup for corking
1782 */
Wei Wang26879da2016-05-02 21:40:07 -07001783 err = ip6_setup_cork(sk, &inet->cork, &np->cork,
Willem de Bruijn5fdaa882018-07-06 10:12:57 -04001784 ipc6, rt, fl6);
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001785 if (err)
1786 return err;
1787
Wei Wang26879da2016-05-02 21:40:07 -07001788 exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0);
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001789 length += exthdrlen;
1790 transhdrlen += exthdrlen;
1791 } else {
1792 fl6 = &inet->cork.fl.u.ip6;
1793 transhdrlen = 0;
1794 }
1795
1796 return __ip6_append_data(sk, fl6, &sk->sk_write_queue, &inet->cork.base,
1797 &np->cork, sk_page_frag(sk), getfrag,
Willem de Bruijn5fdaa882018-07-06 10:12:57 -04001798 from, length, transhdrlen, flags, ipc6);
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001799}
Chris Elstona495f832012-04-29 21:48:53 +00001800EXPORT_SYMBOL_GPL(ip6_append_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001802static void ip6_cork_release(struct inet_cork_full *cork,
1803 struct inet6_cork *v6_cork)
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001804{
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001805 if (v6_cork->opt) {
1806 kfree(v6_cork->opt->dst0opt);
1807 kfree(v6_cork->opt->dst1opt);
1808 kfree(v6_cork->opt->hopopt);
1809 kfree(v6_cork->opt->srcrt);
1810 kfree(v6_cork->opt);
1811 v6_cork->opt = NULL;
Herbert Xu0178b692009-02-05 15:15:50 -08001812 }
1813
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001814 if (cork->base.dst) {
1815 dst_release(cork->base.dst);
1816 cork->base.dst = NULL;
1817 cork->base.flags &= ~IPCORK_ALLFRAG;
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001818 }
Vlad Yasevich366e41d2015-01-31 10:40:13 -05001819 memset(&cork->fl, 0, sizeof(cork->fl));
Pavel Emelyanovbf138862007-11-05 21:04:31 -08001820}
1821
Vlad Yasevich64223982015-01-31 10:40:15 -05001822struct sk_buff *__ip6_make_skb(struct sock *sk,
1823 struct sk_buff_head *queue,
1824 struct inet_cork_full *cork,
1825 struct inet6_cork *v6_cork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
1827 struct sk_buff *skb, *tmp_skb;
1828 struct sk_buff **tail_skb;
1829 struct in6_addr final_dst_buf, *final_dst = &final_dst_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 struct ipv6_pinfo *np = inet6_sk(sk);
Denis V. Lunev3bd653c2008-10-08 10:54:51 -07001831 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 struct ipv6hdr *hdr;
Vlad Yasevich64223982015-01-31 10:40:15 -05001833 struct ipv6_txoptions *opt = v6_cork->opt;
1834 struct rt6_info *rt = (struct rt6_info *)cork->base.dst;
1835 struct flowi6 *fl6 = &cork->fl.u.ip6;
David S. Miller4c9483b2011-03-12 16:22:43 -05001836 unsigned char proto = fl6->flowi6_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Vlad Yasevich64223982015-01-31 10:40:15 -05001838 skb = __skb_dequeue(queue);
Ian Morris63159f22015-03-29 14:00:04 +01001839 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 goto out;
1841 tail_skb = &(skb_shinfo(skb)->frag_list);
1842
1843 /* move skb->data to ip header from ext header */
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001844 if (skb->data < skb_network_header(skb))
Arnaldo Carvalho de Melobbe735e2007-03-10 22:16:10 -03001845 __skb_pull(skb, skb_network_offset(skb));
Vlad Yasevich64223982015-01-31 10:40:15 -05001846 while ((tmp_skb = __skb_dequeue(queue)) != NULL) {
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001847 __skb_pull(tmp_skb, skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 *tail_skb = tmp_skb;
1849 tail_skb = &(tmp_skb->next);
1850 skb->len += tmp_skb->len;
1851 skb->data_len += tmp_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 skb->truesize += tmp_skb->truesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 tmp_skb->destructor = NULL;
1854 tmp_skb->sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
1856
Herbert Xu28a894532008-02-12 18:07:27 -08001857 /* Allow local fragmentation. */
WANG Cong60ff7462014-05-04 16:39:18 -07001858 skb->ignore_df = ip6_sk_ignore_df(sk);
Herbert Xu28a894532008-02-12 18:07:27 -08001859
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001860 *final_dst = fl6->daddr;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03001861 __skb_pull(skb, skb_network_header_len(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (opt && opt->opt_flen)
1863 ipv6_push_frag_opts(skb, opt, &proto);
1864 if (opt && opt->opt_nflen)
David Lebrun613fa3c2016-11-08 14:59:20 +01001865 ipv6_push_nfrag_opts(skb, opt, &proto, &final_dst, &fl6->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07001867 skb_push(skb, sizeof(struct ipv6hdr));
1868 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001869 hdr = ipv6_hdr(skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001870
Vlad Yasevich64223982015-01-31 10:40:15 -05001871 ip6_flow_hdr(hdr, v6_cork->tclass,
Tom Herbertcb1ce2e2014-07-01 21:33:10 -07001872 ip6_make_flowlabel(net, skb, fl6->flowlabel,
Shaohua Li513674b2017-12-20 12:10:21 -08001873 ip6_autoflowlabel(net, np), fl6));
Vlad Yasevich64223982015-01-31 10:40:15 -05001874 hdr->hop_limit = v6_cork->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 hdr->nexthdr = proto;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001876 hdr->saddr = fl6->saddr;
1877 hdr->daddr = *final_dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Patrick McHardya2c20642006-01-08 22:37:26 -08001879 skb->priority = sk->sk_priority;
Willem de Bruijnc6af0c22019-09-11 15:50:51 -04001880 skb->mark = cork->base.mark;
Patrick McHardya2c20642006-01-08 22:37:26 -08001881
Jesus Sanchez-Palenciaa818f752018-07-03 15:42:50 -07001882 skb->tstamp = cork->base.transmit_time;
1883
Changli Gaod8d1f302010-06-10 23:31:35 -07001884 skb_dst_set(skb, dst_clone(&rt->dst));
Neil Hormanedf391f2009-04-27 02:45:02 -07001885 IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
David L Stevens14878f72007-09-16 16:52:35 -07001886 if (proto == IPPROTO_ICMPV6) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001887 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
David L Stevens14878f72007-09-16 16:52:35 -07001888
Hannes Frederic Sowa43a43b62014-03-31 20:14:10 +02001889 ICMP6MSGOUT_INC_STATS(net, idev, icmp6_hdr(skb)->icmp6_type);
1890 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
David L Stevens14878f72007-09-16 16:52:35 -07001891 }
1892
Vlad Yasevich64223982015-01-31 10:40:15 -05001893 ip6_cork_release(cork, v6_cork);
1894out:
1895 return skb;
1896}
1897
1898int ip6_send_skb(struct sk_buff *skb)
1899{
1900 struct net *net = sock_net(skb->sk);
1901 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
1902 int err;
1903
Eric W. Biederman33224b12015-10-07 16:48:46 -05001904 err = ip6_local_out(net, skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (err) {
1906 if (err > 0)
Eric Dumazet6ce9e7b2009-09-02 18:05:33 -07001907 err = net_xmit_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 if (err)
Vlad Yasevich64223982015-01-31 10:40:15 -05001909 IP6_INC_STATS(net, rt->rt6i_idev,
1910 IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 return err;
Vlad Yasevich64223982015-01-31 10:40:15 -05001914}
1915
1916int ip6_push_pending_frames(struct sock *sk)
1917{
1918 struct sk_buff *skb;
1919
1920 skb = ip6_finish_skb(sk);
1921 if (!skb)
1922 return 0;
1923
1924 return ip6_send_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
Chris Elstona495f832012-04-29 21:48:53 +00001926EXPORT_SYMBOL_GPL(ip6_push_pending_frames);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001928static void __ip6_flush_pending_frames(struct sock *sk,
Vlad Yasevich64223982015-01-31 10:40:15 -05001929 struct sk_buff_head *queue,
1930 struct inet_cork_full *cork,
1931 struct inet6_cork *v6_cork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 struct sk_buff *skb;
1934
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001935 while ((skb = __skb_dequeue_tail(queue)) != NULL) {
Eric Dumazetadf30902009-06-02 05:19:30 +00001936 if (skb_dst(skb))
1937 IP6_INC_STATS(sock_net(sk), ip6_dst_idev(skb_dst(skb)),
YOSHIFUJI Hideakie1f522082007-09-11 11:31:43 +02001938 IPSTATS_MIB_OUTDISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 kfree_skb(skb);
1940 }
1941
Vlad Yasevich64223982015-01-31 10:40:15 -05001942 ip6_cork_release(cork, v6_cork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001944
1945void ip6_flush_pending_frames(struct sock *sk)
1946{
Vlad Yasevich64223982015-01-31 10:40:15 -05001947 __ip6_flush_pending_frames(sk, &sk->sk_write_queue,
1948 &inet_sk(sk)->cork, &inet6_sk(sk)->cork);
Vlad Yasevich0bbe84a2015-01-31 10:40:14 -05001949}
Chris Elstona495f832012-04-29 21:48:53 +00001950EXPORT_SYMBOL_GPL(ip6_flush_pending_frames);
Vlad Yasevich64223982015-01-31 10:40:15 -05001951
1952struct sk_buff *ip6_make_skb(struct sock *sk,
1953 int getfrag(void *from, char *to, int offset,
1954 int len, int odd, struct sk_buff *skb),
1955 void *from, int length, int transhdrlen,
Wei Wang26879da2016-05-02 21:40:07 -07001956 struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
Vlad Yasevich64223982015-01-31 10:40:15 -05001957 struct rt6_info *rt, unsigned int flags,
Willem de Bruijn5fdaa882018-07-06 10:12:57 -04001958 struct inet_cork_full *cork)
Vlad Yasevich64223982015-01-31 10:40:15 -05001959{
Vlad Yasevich64223982015-01-31 10:40:15 -05001960 struct inet6_cork v6_cork;
1961 struct sk_buff_head queue;
Wei Wang26879da2016-05-02 21:40:07 -07001962 int exthdrlen = (ipc6->opt ? ipc6->opt->opt_flen : 0);
Vlad Yasevich64223982015-01-31 10:40:15 -05001963 int err;
1964
1965 if (flags & MSG_PROBE)
1966 return NULL;
1967
1968 __skb_queue_head_init(&queue);
1969
Willem de Bruijn1cd78842018-04-26 13:42:15 -04001970 cork->base.flags = 0;
1971 cork->base.addr = 0;
1972 cork->base.opt = NULL;
1973 cork->base.dst = NULL;
Vlad Yasevich64223982015-01-31 10:40:15 -05001974 v6_cork.opt = NULL;
Willem de Bruijn5fdaa882018-07-06 10:12:57 -04001975 err = ip6_setup_cork(sk, cork, &v6_cork, ipc6, rt, fl6);
Eric Dumazet862c03e2018-01-10 03:45:49 -08001976 if (err) {
Willem de Bruijn1cd78842018-04-26 13:42:15 -04001977 ip6_cork_release(cork, &v6_cork);
Vlad Yasevich64223982015-01-31 10:40:15 -05001978 return ERR_PTR(err);
Eric Dumazet862c03e2018-01-10 03:45:49 -08001979 }
Wei Wang26879da2016-05-02 21:40:07 -07001980 if (ipc6->dontfrag < 0)
1981 ipc6->dontfrag = inet6_sk(sk)->dontfrag;
Vlad Yasevich64223982015-01-31 10:40:15 -05001982
Willem de Bruijn1cd78842018-04-26 13:42:15 -04001983 err = __ip6_append_data(sk, fl6, &queue, &cork->base, &v6_cork,
Vlad Yasevich64223982015-01-31 10:40:15 -05001984 &current->task_frag, getfrag, from,
1985 length + exthdrlen, transhdrlen + exthdrlen,
Willem de Bruijn5fdaa882018-07-06 10:12:57 -04001986 flags, ipc6);
Vlad Yasevich64223982015-01-31 10:40:15 -05001987 if (err) {
Willem de Bruijn1cd78842018-04-26 13:42:15 -04001988 __ip6_flush_pending_frames(sk, &queue, cork, &v6_cork);
Vlad Yasevich64223982015-01-31 10:40:15 -05001989 return ERR_PTR(err);
1990 }
1991
Willem de Bruijn1cd78842018-04-26 13:42:15 -04001992 return __ip6_make_skb(sk, &queue, cork, &v6_cork);
Vlad Yasevich64223982015-01-31 10:40:15 -05001993}