Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Multipath TCP
0004  *
0005  * Copyright (c) 2017 - 2019, Intel Corporation.
0006  */
0007 
0008 #ifndef __NET_MPTCP_H
0009 #define __NET_MPTCP_H
0010 
0011 #include <linux/skbuff.h>
0012 #include <linux/tcp.h>
0013 #include <linux/types.h>
0014 
0015 struct mptcp_info;
0016 struct mptcp_sock;
0017 struct seq_file;
0018 
0019 /* MPTCP sk_buff extension data */
0020 struct mptcp_ext {
0021     union {
0022         u64 data_ack;
0023         u32 data_ack32;
0024     };
0025     u64     data_seq;
0026     u32     subflow_seq;
0027     u16     data_len;
0028     __sum16     csum;
0029     u8      use_map:1,
0030             dsn64:1,
0031             data_fin:1,
0032             use_ack:1,
0033             ack64:1,
0034             mpc_map:1,
0035             frozen:1,
0036             reset_transient:1;
0037     u8      reset_reason:4,
0038             csum_reqd:1,
0039             infinite_map:1;
0040 };
0041 
0042 #define MPTCPOPT_HMAC_LEN   20
0043 #define MPTCP_RM_IDS_MAX    8
0044 
0045 struct mptcp_rm_list {
0046     u8 ids[MPTCP_RM_IDS_MAX];
0047     u8 nr;
0048 };
0049 
0050 struct mptcp_addr_info {
0051     u8          id;
0052     sa_family_t     family;
0053     __be16          port;
0054     union {
0055         struct in_addr  addr;
0056 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
0057         struct in6_addr addr6;
0058 #endif
0059     };
0060 };
0061 
0062 struct mptcp_out_options {
0063 #if IS_ENABLED(CONFIG_MPTCP)
0064     u16 suboptions;
0065     struct mptcp_rm_list rm_list;
0066     u8 join_id;
0067     u8 backup;
0068     u8 reset_reason:4,
0069        reset_transient:1,
0070        csum_reqd:1,
0071        allow_join_id0:1;
0072     union {
0073         struct {
0074             u64 sndr_key;
0075             u64 rcvr_key;
0076             u64 data_seq;
0077             u32 subflow_seq;
0078             u16 data_len;
0079             __sum16 csum;
0080         };
0081         struct {
0082             struct mptcp_addr_info addr;
0083             u64 ahmac;
0084         };
0085         struct {
0086             struct mptcp_ext ext_copy;
0087             u64 fail_seq;
0088         };
0089         struct {
0090             u32 nonce;
0091             u32 token;
0092             u64 thmac;
0093             u8 hmac[MPTCPOPT_HMAC_LEN];
0094         };
0095     };
0096 #endif
0097 };
0098 
0099 #ifdef CONFIG_MPTCP
0100 extern struct request_sock_ops mptcp_subflow_request_sock_ops;
0101 
0102 void mptcp_init(void);
0103 
0104 static inline bool sk_is_mptcp(const struct sock *sk)
0105 {
0106     return tcp_sk(sk)->is_mptcp;
0107 }
0108 
0109 static inline bool rsk_is_mptcp(const struct request_sock *req)
0110 {
0111     return tcp_rsk(req)->is_mptcp;
0112 }
0113 
0114 static inline bool rsk_drop_req(const struct request_sock *req)
0115 {
0116     return tcp_rsk(req)->is_mptcp && tcp_rsk(req)->drop_req;
0117 }
0118 
0119 void mptcp_space(const struct sock *ssk, int *space, int *full_space);
0120 bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
0121                unsigned int *size, struct mptcp_out_options *opts);
0122 bool mptcp_synack_options(const struct request_sock *req, unsigned int *size,
0123               struct mptcp_out_options *opts);
0124 bool mptcp_established_options(struct sock *sk, struct sk_buff *skb,
0125                    unsigned int *size, unsigned int remaining,
0126                    struct mptcp_out_options *opts);
0127 bool mptcp_incoming_options(struct sock *sk, struct sk_buff *skb);
0128 
0129 void mptcp_write_options(struct tcphdr *th, __be32 *ptr, struct tcp_sock *tp,
0130              struct mptcp_out_options *opts);
0131 
0132 void mptcp_diag_fill_info(struct mptcp_sock *msk, struct mptcp_info *info);
0133 
0134 /* move the skb extension owership, with the assumption that 'to' is
0135  * newly allocated
0136  */
0137 static inline void mptcp_skb_ext_move(struct sk_buff *to,
0138                       struct sk_buff *from)
0139 {
0140     if (!skb_ext_exist(from, SKB_EXT_MPTCP))
0141         return;
0142 
0143     if (WARN_ON_ONCE(to->active_extensions))
0144         skb_ext_put(to);
0145 
0146     to->active_extensions = from->active_extensions;
0147     to->extensions = from->extensions;
0148     from->active_extensions = 0;
0149 }
0150 
0151 static inline void mptcp_skb_ext_copy(struct sk_buff *to,
0152                       struct sk_buff *from)
0153 {
0154     struct mptcp_ext *from_ext;
0155 
0156     from_ext = skb_ext_find(from, SKB_EXT_MPTCP);
0157     if (!from_ext)
0158         return;
0159 
0160     from_ext->frozen = 1;
0161     skb_ext_copy(to, from);
0162 }
0163 
0164 static inline bool mptcp_ext_matches(const struct mptcp_ext *to_ext,
0165                      const struct mptcp_ext *from_ext)
0166 {
0167     /* MPTCP always clears the ext when adding it to the skb, so
0168      * holes do not bother us here
0169      */
0170     return !from_ext ||
0171            (to_ext && from_ext &&
0172             !memcmp(from_ext, to_ext, sizeof(struct mptcp_ext)));
0173 }
0174 
0175 /* check if skbs can be collapsed.
0176  * MPTCP collapse is allowed if neither @to or @from carry an mptcp data
0177  * mapping, or if the extension of @to is the same as @from.
0178  * Collapsing is not possible if @to lacks an extension, but @from carries one.
0179  */
0180 static inline bool mptcp_skb_can_collapse(const struct sk_buff *to,
0181                       const struct sk_buff *from)
0182 {
0183     return mptcp_ext_matches(skb_ext_find(to, SKB_EXT_MPTCP),
0184                  skb_ext_find(from, SKB_EXT_MPTCP));
0185 }
0186 
0187 void mptcp_seq_show(struct seq_file *seq);
0188 int mptcp_subflow_init_cookie_req(struct request_sock *req,
0189                   const struct sock *sk_listener,
0190                   struct sk_buff *skb);
0191 
0192 __be32 mptcp_get_reset_option(const struct sk_buff *skb);
0193 
0194 static inline __be32 mptcp_reset_option(const struct sk_buff *skb)
0195 {
0196     if (skb_ext_exist(skb, SKB_EXT_MPTCP))
0197         return mptcp_get_reset_option(skb);
0198 
0199     return htonl(0u);
0200 }
0201 #else
0202 
0203 static inline void mptcp_init(void)
0204 {
0205 }
0206 
0207 static inline bool sk_is_mptcp(const struct sock *sk)
0208 {
0209     return false;
0210 }
0211 
0212 static inline bool rsk_is_mptcp(const struct request_sock *req)
0213 {
0214     return false;
0215 }
0216 
0217 static inline bool rsk_drop_req(const struct request_sock *req)
0218 {
0219     return false;
0220 }
0221 
0222 static inline bool mptcp_syn_options(struct sock *sk, const struct sk_buff *skb,
0223                      unsigned int *size,
0224                      struct mptcp_out_options *opts)
0225 {
0226     return false;
0227 }
0228 
0229 static inline bool mptcp_synack_options(const struct request_sock *req,
0230                     unsigned int *size,
0231                     struct mptcp_out_options *opts)
0232 {
0233     return false;
0234 }
0235 
0236 static inline bool mptcp_established_options(struct sock *sk,
0237                          struct sk_buff *skb,
0238                          unsigned int *size,
0239                          unsigned int remaining,
0240                          struct mptcp_out_options *opts)
0241 {
0242     return false;
0243 }
0244 
0245 static inline bool mptcp_incoming_options(struct sock *sk,
0246                       struct sk_buff *skb)
0247 {
0248     return true;
0249 }
0250 
0251 static inline void mptcp_skb_ext_move(struct sk_buff *to,
0252                       const struct sk_buff *from)
0253 {
0254 }
0255 
0256 static inline void mptcp_skb_ext_copy(struct sk_buff *to,
0257                       struct sk_buff *from)
0258 {
0259 }
0260 
0261 static inline bool mptcp_skb_can_collapse(const struct sk_buff *to,
0262                       const struct sk_buff *from)
0263 {
0264     return true;
0265 }
0266 
0267 static inline void mptcp_space(const struct sock *ssk, int *s, int *fs) { }
0268 static inline void mptcp_seq_show(struct seq_file *seq) { }
0269 
0270 static inline int mptcp_subflow_init_cookie_req(struct request_sock *req,
0271                         const struct sock *sk_listener,
0272                         struct sk_buff *skb)
0273 {
0274     return 0; /* TCP fallback */
0275 }
0276 
0277 static inline __be32 mptcp_reset_option(const struct sk_buff *skb)  { return htonl(0u); }
0278 #endif /* CONFIG_MPTCP */
0279 
0280 #if IS_ENABLED(CONFIG_MPTCP_IPV6)
0281 int mptcpv6_init(void);
0282 void mptcpv6_handle_mapped(struct sock *sk, bool mapped);
0283 #elif IS_ENABLED(CONFIG_IPV6)
0284 static inline int mptcpv6_init(void) { return 0; }
0285 static inline void mptcpv6_handle_mapped(struct sock *sk, bool mapped) { }
0286 #endif
0287 
0288 #if defined(CONFIG_MPTCP) && defined(CONFIG_BPF_SYSCALL)
0289 struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk);
0290 #else
0291 static inline struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk) { return NULL; }
0292 #endif
0293 
0294 #if !IS_ENABLED(CONFIG_MPTCP)
0295 struct mptcp_sock { };
0296 #endif
0297 
0298 #endif /* __NET_MPTCP_H */