blob: 5a9f4d722f35decddba7e06122569a2ea00cd653 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Arnaldo Carvalho de Melo81297652005-12-13 23:15:24 -08002/*
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
Arnaldo Carvalho de Melo81297652005-12-13 23:15:24 -080010 */
11
Arnaldo Carvalho de Melo81297652005-12-13 23:15:24 -080012#include <linux/module.h>
13#include <linux/in6.h>
14#include <linux/ipv6.h>
15#include <linux/jhash.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Arnaldo Carvalho de Melo81297652005-12-13 23:15:24 -080017
18#include <net/addrconf.h>
19#include <net/inet_connection_sock.h>
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -080020#include <net/inet_ecn.h>
21#include <net/inet_hashtables.h>
22#include <net/ip6_route.h>
Arnaldo Carvalho de Melo81297652005-12-13 23:15:24 -080023#include <net/sock.h>
Adrian Bunk9f5336e2006-01-07 13:24:25 -080024#include <net/inet6_connection_sock.h>
Craig Gallekc125e802016-02-10 11:50:40 -050025#include <net/sock_reuseport.h>
Arnaldo Carvalho de Melo81297652005-12-13 23:15:24 -080026
Eric Dumazet30d50c62015-09-25 07:39:13 -070027struct dst_entry *inet6_csk_route_req(const struct sock *sk,
Neal Cardwell3840a062012-06-28 12:34:19 +000028 struct flowi6 *fl6,
Eric Dumazetf76b33c2015-09-29 07:42:42 -070029 const struct request_sock *req,
30 u8 proto)
David S. Millerae4694b2010-12-02 10:59:22 -080031{
Eric Dumazet634fb9792013-10-09 15:21:29 -070032 struct inet_request_sock *ireq = inet_rsk(req);
Eric Dumazetf76b33c2015-09-29 07:42:42 -070033 const struct ipv6_pinfo *np = inet6_sk(sk);
David S. Millerae4694b2010-12-02 10:59:22 -080034 struct in6_addr *final_p, final;
35 struct dst_entry *dst;
David S. Millerae4694b2010-12-02 10:59:22 -080036
Neal Cardwell3840a062012-06-28 12:34:19 +000037 memset(fl6, 0, sizeof(*fl6));
Eric Dumazetf76b33c2015-09-29 07:42:42 -070038 fl6->flowi6_proto = proto;
Eric Dumazet634fb9792013-10-09 15:21:29 -070039 fl6->daddr = ireq->ir_v6_rmt_addr;
Eric Dumazet45f6fad2015-11-29 19:37:57 -080040 rcu_read_lock();
41 final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
42 rcu_read_unlock();
Eric Dumazet634fb9792013-10-09 15:21:29 -070043 fl6->saddr = ireq->ir_v6_loc_addr;
44 fl6->flowi6_oif = ireq->ir_iif;
Lorenzo Colitti84f39b02014-05-13 10:17:35 -070045 fl6->flowi6_mark = ireq->ir_mark;
Eric Dumazet634fb9792013-10-09 15:21:29 -070046 fl6->fl6_dport = ireq->ir_rmt_port;
Eric Dumazetb44084c2013-10-10 00:04:37 -070047 fl6->fl6_sport = htons(ireq->ir_num);
Lorenzo Colittie2d118a2016-11-04 02:23:43 +090048 fl6->flowi6_uid = sk->sk_uid;
Paul Moore3df98d72020-09-27 22:38:26 -040049 security_req_classify_flow(req, flowi6_to_flowi_common(fl6));
David S. Millerae4694b2010-12-02 10:59:22 -080050
Sabrina Dubrocac4e85f72019-12-04 15:35:52 +010051 dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);
David S. Miller68d0c6d2011-03-01 13:19:07 -080052 if (IS_ERR(dst))
David S. Millerae4694b2010-12-02 10:59:22 -080053 return NULL;
54
55 return dst;
56}
Eric Dumazetf76b33c2015-09-29 07:42:42 -070057EXPORT_SYMBOL(inet6_csk_route_req);
David S. Millerae4694b2010-12-02 10:59:22 -080058
Ian Morris67ba4152014-08-24 21:53:10 +010059void inet6_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr)
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -080060{
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -080061 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) uaddr;
62
63 sin6->sin6_family = AF_INET6;
Eric Dumazetefe42082013-10-03 15:42:29 -070064 sin6->sin6_addr = sk->sk_v6_daddr;
Eric Dumazetc720c7e82009-10-15 06:30:45 +000065 sin6->sin6_port = inet_sk(sk)->inet_dport;
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -080066 /* We do not store received flowlabel for TCP */
67 sin6->sin6_flowinfo = 0;
Hannes Frederic Sowa842df072013-03-08 02:07:19 +000068 sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr,
69 sk->sk_bound_dev_if);
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -080070}
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -080071EXPORT_SYMBOL_GPL(inet6_csk_addr2sockaddr);
72
Noriaki TAKAMIYAa47ed4c2007-09-06 03:31:25 -070073static inline
Noriaki TAKAMIYAa47ed4c2007-09-06 03:31:25 -070074struct dst_entry *__inet6_csk_dst_check(struct sock *sk, u32 cookie)
75{
Nicolas Dichtel6f3118b2012-09-10 22:09:46 +000076 return __sk_dst_check(sk, cookie);
Noriaki TAKAMIYAa47ed4c2007-09-06 03:31:25 -070077}
78
Eric Dumazetd3818c92012-07-17 21:38:04 +000079static struct dst_entry *inet6_csk_route_socket(struct sock *sk,
80 struct flowi6 *fl6)
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -080081{
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -080082 struct inet_sock *inet = inet_sk(sk);
83 struct ipv6_pinfo *np = inet6_sk(sk);
Arnaud Ebalard20c59de2010-06-01 21:35:01 +000084 struct in6_addr *final_p, final;
David S. Miller35ad9b92012-07-16 03:44:56 -070085 struct dst_entry *dst;
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -080086
Eric Dumazetd3818c92012-07-17 21:38:04 +000087 memset(fl6, 0, sizeof(*fl6));
88 fl6->flowi6_proto = sk->sk_protocol;
Eric Dumazetefe42082013-10-03 15:42:29 -070089 fl6->daddr = sk->sk_v6_daddr;
Eric Dumazetd3818c92012-07-17 21:38:04 +000090 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;
Lorenzo Colittie2d118a2016-11-04 02:23:43 +090097 fl6->flowi6_uid = sk->sk_uid;
Paul Moore3df98d72020-09-27 22:38:26 -040098 security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -080099
Eric Dumazet45f6fad2015-11-29 19:37:57 -0800100 rcu_read_lock();
101 final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
102 rcu_read_unlock();
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800103
Noriaki TAKAMIYAa47ed4c2007-09-06 03:31:25 -0700104 dst = __inet6_csk_dst_check(sk, np->dst_cookie);
David S. Miller35ad9b92012-07-16 03:44:56 -0700105 if (!dst) {
Sabrina Dubrocac4e85f72019-12-04 15:35:52 +0100106 dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800107
David S. Miller35ad9b92012-07-16 03:44:56 -0700108 if (!IS_ERR(dst))
Eric Dumazet6bd4f352015-12-02 21:53:57 -0800109 ip6_dst_store(sk, dst, NULL, NULL);
David S. Miller35ad9b92012-07-16 03:44:56 -0700110 }
111 return dst;
112}
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800113
Eric Dumazetb0270e92014-04-15 12:58:34 -0400114int inet6_csk_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl_unused)
David S. Miller35ad9b92012-07-16 03:44:56 -0700115{
David S. Miller35ad9b92012-07-16 03:44:56 -0700116 struct ipv6_pinfo *np = inet6_sk(sk);
117 struct flowi6 fl6;
118 struct dst_entry *dst;
119 int res;
120
Eric Dumazetd3818c92012-07-17 21:38:04 +0000121 dst = inet6_csk_route_socket(sk, &fl6);
David S. Miller35ad9b92012-07-16 03:44:56 -0700122 if (IS_ERR(dst)) {
123 sk->sk_err_soft = -PTR_ERR(dst);
124 sk->sk_route_caps = 0;
125 kfree_skb(skb);
126 return PTR_ERR(dst);
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800127 }
128
Eric Dumazetd14730b2011-07-28 03:43:47 +0000129 rcu_read_lock();
130 skb_dst_set_noref(skb, dst);
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800131
132 /* Restore final destination back after routing done */
Eric Dumazetefe42082013-10-03 15:42:29 -0700133 fl6.daddr = sk->sk_v6_daddr;
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800134
Pablo Neira92e55f42017-01-26 22:56:21 +0100135 res = ip6_xmit(sk, skb, &fl6, sk->sk_mark, rcu_dereference(np->opt),
Eric Dumazet4f6570d2019-09-24 08:01:14 -0700136 np->tclass, sk->sk_priority);
Eric Dumazetd14730b2011-07-28 03:43:47 +0000137 rcu_read_unlock();
138 return res;
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800139}
Arnaldo Carvalho de Melob9750ce2005-12-13 23:22:54 -0800140EXPORT_SYMBOL_GPL(inet6_csk_xmit);
David S. Miller35ad9b92012-07-16 03:44:56 -0700141
142struct dst_entry *inet6_csk_update_pmtu(struct sock *sk, u32 mtu)
143{
Eric Dumazetd3818c92012-07-17 21:38:04 +0000144 struct flowi6 fl6;
145 struct dst_entry *dst = inet6_csk_route_socket(sk, &fl6);
David S. Miller35ad9b92012-07-16 03:44:56 -0700146
147 if (IS_ERR(dst))
148 return NULL;
Hangbin Liubd085ef2019-12-22 10:51:09 +0800149 dst->ops->update_pmtu(dst, sk, NULL, mtu, true);
David S. Miller35ad9b92012-07-16 03:44:56 -0700150
Eric Dumazetb4dd0062012-11-20 15:14:51 -0500151 dst = inet6_csk_route_socket(sk, &fl6);
152 return IS_ERR(dst) ? NULL : dst;
David S. Miller35ad9b92012-07-16 03:44:56 -0700153}
154EXPORT_SYMBOL_GPL(inet6_csk_update_pmtu);