f2fs: Provide a splice-read wrapper
[linux-block.git] / net / ipv6 / inet6_connection_sock.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
8129765a
ACM
2/*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * Support for INET6 connection oriented protocols.
8 *
9 * Authors: See the TCPv6 sources
8129765a
ACM
10 */
11
8129765a
ACM
12#include <linux/module.h>
13#include <linux/in6.h>
14#include <linux/ipv6.h>
15#include <linux/jhash.h>
5a0e3ad6 16#include <linux/slab.h>
8129765a
ACM
17
18#include <net/addrconf.h>
19#include <net/inet_connection_sock.h>
b9750ce1
ACM
20#include <net/inet_ecn.h>
21#include <net/inet_hashtables.h>
22#include <net/ip6_route.h>
8129765a 23#include <net/sock.h>
9f5336e2 24#include <net/inet6_connection_sock.h>
c125e80b 25#include <net/sock_reuseport.h>
8129765a 26
30d50c61 27struct dst_entry *inet6_csk_route_req(const struct sock *sk,
3840a06e 28 struct flowi6 *fl6,
f76b33c3
ED
29 const struct request_sock *req,
30 u8 proto)
ae4694b2 31{
634fb979 32 struct inet_request_sock *ireq = inet_rsk(req);
f76b33c3 33 const struct ipv6_pinfo *np = inet6_sk(sk);
ae4694b2
DM
34 struct in6_addr *final_p, final;
35 struct dst_entry *dst;
4c9483b2 36
3840a06e 37 memset(fl6, 0, sizeof(*fl6));
f76b33c3 38 fl6->flowi6_proto = proto;
634fb979 39 fl6->daddr = ireq->ir_v6_rmt_addr;
45f6fad8
ED
40 rcu_read_lock();
41 final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
42 rcu_read_unlock();
634fb979
ED
43 fl6->saddr = ireq->ir_v6_loc_addr;
44 fl6->flowi6_oif = ireq->ir_iif;
84f39b08 45 fl6->flowi6_mark = ireq->ir_mark;
634fb979 46 fl6->fl6_dport = ireq->ir_rmt_port;
b44084c2 47 fl6->fl6_sport = htons(ireq->ir_num);
e2d118a1 48 fl6->flowi6_uid = sk->sk_uid;
3df98d79 49 security_req_classify_flow(req, flowi6_to_flowi_common(fl6));
4c9483b2 50
c4e85f73 51 dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);
68d0c6d3 52 if (IS_ERR(dst))
ae4694b2
DM
53 return NULL;
54
55 return dst;
56}
f76b33c3 57EXPORT_SYMBOL(inet6_csk_route_req);
ae4694b2 58
67ba4152 59void inet6_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr)
b9750ce1 60{
b9750ce1
ACM
61 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) uaddr;
62
63 sin6->sin6_family = AF_INET6;
efe4208f 64 sin6->sin6_addr = sk->sk_v6_daddr;
c720c7e8 65 sin6->sin6_port = inet_sk(sk)->inet_dport;
b9750ce1
ACM
66 /* We do not store received flowlabel for TCP */
67 sin6->sin6_flowinfo = 0;
842df073
HFS
68 sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr,
69 sk->sk_bound_dev_if);
b9750ce1 70}
b9750ce1
ACM
71EXPORT_SYMBOL_GPL(inet6_csk_addr2sockaddr);
72
a47ed4cd
NT
73static inline
74struct dst_entry *__inet6_csk_dst_check(struct sock *sk, u32 cookie)
75{
6f3118b5 76 return __sk_dst_check(sk, cookie);
a47ed4cd
NT
77}
78
d3818c92
ED
79static struct dst_entry *inet6_csk_route_socket(struct sock *sk,
80 struct flowi6 *fl6)
b9750ce1 81{
b9750ce1
ACM
82 struct inet_sock *inet = inet_sk(sk);
83 struct ipv6_pinfo *np = inet6_sk(sk);
20c59de2 84 struct in6_addr *final_p, final;
35ad9b9c 85 struct dst_entry *dst;
b9750ce1 86
d3818c92
ED
87 memset(fl6, 0, sizeof(*fl6));
88 fl6->flowi6_proto = sk->sk_protocol;
efe4208f 89 fl6->daddr = sk->sk_v6_daddr;
d3818c92
ED
90 fl6->saddr = np->saddr;
91 fl6->flowlabel = np->flow_label;
92 IP6_ECN_flow_xmit(sk, fl6->flowlabel);
93 fl6->flowi6_oif = sk->sk_bound_dev_if;
94 fl6->flowi6_mark = sk->sk_mark;
95 fl6->fl6_sport = inet->inet_sport;
96 fl6->fl6_dport = inet->inet_dport;
e2d118a1 97 fl6->flowi6_uid = sk->sk_uid;
3df98d79 98 security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
b9750ce1 99
45f6fad8
ED
100 rcu_read_lock();
101 final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
102 rcu_read_unlock();
b9750ce1 103
a47ed4cd 104 dst = __inet6_csk_dst_check(sk, np->dst_cookie);
35ad9b9c 105 if (!dst) {
c4e85f73 106 dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);
b9750ce1 107
35ad9b9c 108 if (!IS_ERR(dst))
6bd4f355 109 ip6_dst_store(sk, dst, NULL, NULL);
35ad9b9c
DM
110 }
111 return dst;
112}
b9750ce1 113
b0270e91 114int inet6_csk_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl_unused)
35ad9b9c 115{
35ad9b9c
DM
116 struct ipv6_pinfo *np = inet6_sk(sk);
117 struct flowi6 fl6;
118 struct dst_entry *dst;
119 int res;
120
d3818c92 121 dst = inet6_csk_route_socket(sk, &fl6);
35ad9b9c 122 if (IS_ERR(dst)) {
2f2d9972 123 WRITE_ONCE(sk->sk_err_soft, -PTR_ERR(dst));
35ad9b9c
DM
124 sk->sk_route_caps = 0;
125 kfree_skb(skb);
126 return PTR_ERR(dst);
b9750ce1
ACM
127 }
128
d14730b8
ED
129 rcu_read_lock();
130 skb_dst_set_noref(skb, dst);
b9750ce1
ACM
131
132 /* Restore final destination back after routing done */
efe4208f 133 fl6.daddr = sk->sk_v6_daddr;
b9750ce1 134
92e55f41 135 res = ip6_xmit(sk, skb, &fl6, sk->sk_mark, rcu_dereference(np->opt),
4f6570d7 136 np->tclass, sk->sk_priority);
d14730b8
ED
137 rcu_read_unlock();
138 return res;
b9750ce1 139}
b9750ce1 140EXPORT_SYMBOL_GPL(inet6_csk_xmit);
35ad9b9c
DM
141
142struct dst_entry *inet6_csk_update_pmtu(struct sock *sk, u32 mtu)
143{
d3818c92
ED
144 struct flowi6 fl6;
145 struct dst_entry *dst = inet6_csk_route_socket(sk, &fl6);
35ad9b9c
DM
146
147 if (IS_ERR(dst))
148 return NULL;
bd085ef6 149 dst->ops->update_pmtu(dst, sk, NULL, mtu, true);
35ad9b9c 150
b4dd0067
ED
151 dst = inet6_csk_route_socket(sk, &fl6);
152 return IS_ERR(dst) ? NULL : dst;
35ad9b9c
DM
153}
154EXPORT_SYMBOL_GPL(inet6_csk_update_pmtu);