Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * INET        An implementation of the TCP/IP protocol suite for the LINUX
0004  *             operating system.  INET is implemented using the  BSD Socket
0005  *             interface as the means of communication with the user level.
0006  *
0007  *             Support for INET6 connection oriented protocols.
0008  *
0009  * Authors:    See the TCPv6 sources
0010  */
0011 
0012 #include <linux/module.h>
0013 #include <linux/in6.h>
0014 #include <linux/ipv6.h>
0015 #include <linux/jhash.h>
0016 #include <linux/slab.h>
0017 
0018 #include <net/addrconf.h>
0019 #include <net/inet_connection_sock.h>
0020 #include <net/inet_ecn.h>
0021 #include <net/inet_hashtables.h>
0022 #include <net/ip6_route.h>
0023 #include <net/sock.h>
0024 #include <net/inet6_connection_sock.h>
0025 #include <net/sock_reuseport.h>
0026 
0027 struct dst_entry *inet6_csk_route_req(const struct sock *sk,
0028                       struct flowi6 *fl6,
0029                       const struct request_sock *req,
0030                       u8 proto)
0031 {
0032     struct inet_request_sock *ireq = inet_rsk(req);
0033     const struct ipv6_pinfo *np = inet6_sk(sk);
0034     struct in6_addr *final_p, final;
0035     struct dst_entry *dst;
0036 
0037     memset(fl6, 0, sizeof(*fl6));
0038     fl6->flowi6_proto = proto;
0039     fl6->daddr = ireq->ir_v6_rmt_addr;
0040     rcu_read_lock();
0041     final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
0042     rcu_read_unlock();
0043     fl6->saddr = ireq->ir_v6_loc_addr;
0044     fl6->flowi6_oif = ireq->ir_iif;
0045     fl6->flowi6_mark = ireq->ir_mark;
0046     fl6->fl6_dport = ireq->ir_rmt_port;
0047     fl6->fl6_sport = htons(ireq->ir_num);
0048     fl6->flowi6_uid = sk->sk_uid;
0049     security_req_classify_flow(req, flowi6_to_flowi_common(fl6));
0050 
0051     dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);
0052     if (IS_ERR(dst))
0053         return NULL;
0054 
0055     return dst;
0056 }
0057 EXPORT_SYMBOL(inet6_csk_route_req);
0058 
0059 void inet6_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr)
0060 {
0061     struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) uaddr;
0062 
0063     sin6->sin6_family = AF_INET6;
0064     sin6->sin6_addr = sk->sk_v6_daddr;
0065     sin6->sin6_port = inet_sk(sk)->inet_dport;
0066     /* We do not store received flowlabel for TCP */
0067     sin6->sin6_flowinfo = 0;
0068     sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr,
0069                           sk->sk_bound_dev_if);
0070 }
0071 EXPORT_SYMBOL_GPL(inet6_csk_addr2sockaddr);
0072 
0073 static inline
0074 struct dst_entry *__inet6_csk_dst_check(struct sock *sk, u32 cookie)
0075 {
0076     return __sk_dst_check(sk, cookie);
0077 }
0078 
0079 static struct dst_entry *inet6_csk_route_socket(struct sock *sk,
0080                         struct flowi6 *fl6)
0081 {
0082     struct inet_sock *inet = inet_sk(sk);
0083     struct ipv6_pinfo *np = inet6_sk(sk);
0084     struct in6_addr *final_p, final;
0085     struct dst_entry *dst;
0086 
0087     memset(fl6, 0, sizeof(*fl6));
0088     fl6->flowi6_proto = sk->sk_protocol;
0089     fl6->daddr = sk->sk_v6_daddr;
0090     fl6->saddr = np->saddr;
0091     fl6->flowlabel = np->flow_label;
0092     IP6_ECN_flow_xmit(sk, fl6->flowlabel);
0093     fl6->flowi6_oif = sk->sk_bound_dev_if;
0094     fl6->flowi6_mark = sk->sk_mark;
0095     fl6->fl6_sport = inet->inet_sport;
0096     fl6->fl6_dport = inet->inet_dport;
0097     fl6->flowi6_uid = sk->sk_uid;
0098     security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
0099 
0100     rcu_read_lock();
0101     final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
0102     rcu_read_unlock();
0103 
0104     dst = __inet6_csk_dst_check(sk, np->dst_cookie);
0105     if (!dst) {
0106         dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);
0107 
0108         if (!IS_ERR(dst))
0109             ip6_dst_store(sk, dst, NULL, NULL);
0110     }
0111     return dst;
0112 }
0113 
0114 int inet6_csk_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl_unused)
0115 {
0116     struct ipv6_pinfo *np = inet6_sk(sk);
0117     struct flowi6 fl6;
0118     struct dst_entry *dst;
0119     int res;
0120 
0121     dst = inet6_csk_route_socket(sk, &fl6);
0122     if (IS_ERR(dst)) {
0123         sk->sk_err_soft = -PTR_ERR(dst);
0124         sk->sk_route_caps = 0;
0125         kfree_skb(skb);
0126         return PTR_ERR(dst);
0127     }
0128 
0129     rcu_read_lock();
0130     skb_dst_set_noref(skb, dst);
0131 
0132     /* Restore final destination back after routing done */
0133     fl6.daddr = sk->sk_v6_daddr;
0134 
0135     res = ip6_xmit(sk, skb, &fl6, sk->sk_mark, rcu_dereference(np->opt),
0136                np->tclass,  sk->sk_priority);
0137     rcu_read_unlock();
0138     return res;
0139 }
0140 EXPORT_SYMBOL_GPL(inet6_csk_xmit);
0141 
0142 struct dst_entry *inet6_csk_update_pmtu(struct sock *sk, u32 mtu)
0143 {
0144     struct flowi6 fl6;
0145     struct dst_entry *dst = inet6_csk_route_socket(sk, &fl6);
0146 
0147     if (IS_ERR(dst))
0148         return NULL;
0149     dst->ops->update_pmtu(dst, sk, NULL, mtu, true);
0150 
0151     dst = inet6_csk_route_socket(sk, &fl6);
0152     return IS_ERR(dst) ? NULL : dst;
0153 }
0154 EXPORT_SYMBOL_GPL(inet6_csk_update_pmtu);