0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef _INET_SOCK_H
0013 #define _INET_SOCK_H
0014
0015 #include <linux/bitops.h>
0016 #include <linux/string.h>
0017 #include <linux/types.h>
0018 #include <linux/jhash.h>
0019 #include <linux/netdevice.h>
0020
0021 #include <net/flow.h>
0022 #include <net/sock.h>
0023 #include <net/request_sock.h>
0024 #include <net/netns/hash.h>
0025 #include <net/tcp_states.h>
0026 #include <net/l3mdev.h>
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 struct ip_options {
0040 __be32 faddr;
0041 __be32 nexthop;
0042 unsigned char optlen;
0043 unsigned char srr;
0044 unsigned char rr;
0045 unsigned char ts;
0046 unsigned char is_strictroute:1,
0047 srr_is_hit:1,
0048 is_changed:1,
0049 rr_needaddr:1,
0050 ts_needtime:1,
0051 ts_needaddr:1;
0052 unsigned char router_alert;
0053 unsigned char cipso;
0054 unsigned char __pad2;
0055 unsigned char __data[];
0056 };
0057
0058 struct ip_options_rcu {
0059 struct rcu_head rcu;
0060 struct ip_options opt;
0061 };
0062
0063 struct ip_options_data {
0064 struct ip_options_rcu opt;
0065 char data[40];
0066 };
0067
0068 struct inet_request_sock {
0069 struct request_sock req;
0070 #define ir_loc_addr req.__req_common.skc_rcv_saddr
0071 #define ir_rmt_addr req.__req_common.skc_daddr
0072 #define ir_num req.__req_common.skc_num
0073 #define ir_rmt_port req.__req_common.skc_dport
0074 #define ir_v6_rmt_addr req.__req_common.skc_v6_daddr
0075 #define ir_v6_loc_addr req.__req_common.skc_v6_rcv_saddr
0076 #define ir_iif req.__req_common.skc_bound_dev_if
0077 #define ir_cookie req.__req_common.skc_cookie
0078 #define ireq_net req.__req_common.skc_net
0079 #define ireq_state req.__req_common.skc_state
0080 #define ireq_family req.__req_common.skc_family
0081
0082 u16 snd_wscale : 4,
0083 rcv_wscale : 4,
0084 tstamp_ok : 1,
0085 sack_ok : 1,
0086 wscale_ok : 1,
0087 ecn_ok : 1,
0088 acked : 1,
0089 no_srccheck: 1,
0090 smc_ok : 1;
0091 u32 ir_mark;
0092 union {
0093 struct ip_options_rcu __rcu *ireq_opt;
0094 #if IS_ENABLED(CONFIG_IPV6)
0095 struct {
0096 struct ipv6_txoptions *ipv6_opt;
0097 struct sk_buff *pktopts;
0098 };
0099 #endif
0100 };
0101 };
0102
0103 static inline struct inet_request_sock *inet_rsk(const struct request_sock *sk)
0104 {
0105 return (struct inet_request_sock *)sk;
0106 }
0107
0108 static inline u32 inet_request_mark(const struct sock *sk, struct sk_buff *skb)
0109 {
0110 if (!sk->sk_mark &&
0111 READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fwmark_accept))
0112 return skb->mark;
0113
0114 return sk->sk_mark;
0115 }
0116
0117 static inline int inet_request_bound_dev_if(const struct sock *sk,
0118 struct sk_buff *skb)
0119 {
0120 int bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
0121 #ifdef CONFIG_NET_L3_MASTER_DEV
0122 struct net *net = sock_net(sk);
0123
0124 if (!bound_dev_if && READ_ONCE(net->ipv4.sysctl_tcp_l3mdev_accept))
0125 return l3mdev_master_ifindex_by_index(net, skb->skb_iif);
0126 #endif
0127
0128 return bound_dev_if;
0129 }
0130
0131 static inline int inet_sk_bound_l3mdev(const struct sock *sk)
0132 {
0133 #ifdef CONFIG_NET_L3_MASTER_DEV
0134 struct net *net = sock_net(sk);
0135
0136 if (!READ_ONCE(net->ipv4.sysctl_tcp_l3mdev_accept))
0137 return l3mdev_master_ifindex_by_index(net,
0138 sk->sk_bound_dev_if);
0139 #endif
0140
0141 return 0;
0142 }
0143
0144 static inline bool inet_bound_dev_eq(bool l3mdev_accept, int bound_dev_if,
0145 int dif, int sdif)
0146 {
0147 if (!bound_dev_if)
0148 return !sdif || l3mdev_accept;
0149 return bound_dev_if == dif || bound_dev_if == sdif;
0150 }
0151
0152 static inline bool inet_sk_bound_dev_eq(struct net *net, int bound_dev_if,
0153 int dif, int sdif)
0154 {
0155 #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
0156 return inet_bound_dev_eq(!!READ_ONCE(net->ipv4.sysctl_tcp_l3mdev_accept),
0157 bound_dev_if, dif, sdif);
0158 #else
0159 return inet_bound_dev_eq(true, bound_dev_if, dif, sdif);
0160 #endif
0161 }
0162
0163 struct inet_cork {
0164 unsigned int flags;
0165 __be32 addr;
0166 struct ip_options *opt;
0167 unsigned int fragsize;
0168 int length;
0169 struct dst_entry *dst;
0170 u8 tx_flags;
0171 __u8 ttl;
0172 __s16 tos;
0173 char priority;
0174 __u16 gso_size;
0175 u64 transmit_time;
0176 u32 mark;
0177 };
0178
0179 struct inet_cork_full {
0180 struct inet_cork base;
0181 struct flowi fl;
0182 };
0183
0184 struct ip_mc_socklist;
0185 struct ipv6_pinfo;
0186 struct rtable;
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208 struct inet_sock {
0209
0210 struct sock sk;
0211 #if IS_ENABLED(CONFIG_IPV6)
0212 struct ipv6_pinfo *pinet6;
0213 #endif
0214
0215 #define inet_daddr sk.__sk_common.skc_daddr
0216 #define inet_rcv_saddr sk.__sk_common.skc_rcv_saddr
0217 #define inet_dport sk.__sk_common.skc_dport
0218 #define inet_num sk.__sk_common.skc_num
0219
0220 __be32 inet_saddr;
0221 __s16 uc_ttl;
0222 __u16 cmsg_flags;
0223 struct ip_options_rcu __rcu *inet_opt;
0224 __be16 inet_sport;
0225 __u16 inet_id;
0226
0227 __u8 tos;
0228 __u8 min_ttl;
0229 __u8 mc_ttl;
0230 __u8 pmtudisc;
0231 __u8 recverr:1,
0232 is_icsk:1,
0233 freebind:1,
0234 hdrincl:1,
0235 mc_loop:1,
0236 transparent:1,
0237 mc_all:1,
0238 nodefrag:1;
0239 __u8 bind_address_no_port:1,
0240 recverr_rfc4884:1,
0241 defer_connect:1;
0242
0243
0244
0245 __u8 rcv_tos;
0246 __u8 convert_csum;
0247 int uc_index;
0248 int mc_index;
0249 __be32 mc_addr;
0250 struct ip_mc_socklist __rcu *mc_list;
0251 struct inet_cork_full cork;
0252 };
0253
0254 #define IPCORK_OPT 1
0255 #define IPCORK_ALLFRAG 2
0256
0257
0258 #define IP_CMSG_PKTINFO BIT(0)
0259 #define IP_CMSG_TTL BIT(1)
0260 #define IP_CMSG_TOS BIT(2)
0261 #define IP_CMSG_RECVOPTS BIT(3)
0262 #define IP_CMSG_RETOPTS BIT(4)
0263 #define IP_CMSG_PASSSEC BIT(5)
0264 #define IP_CMSG_ORIGDSTADDR BIT(6)
0265 #define IP_CMSG_CHECKSUM BIT(7)
0266 #define IP_CMSG_RECVFRAGSIZE BIT(8)
0267
0268 static inline bool sk_is_inet(struct sock *sk)
0269 {
0270 return sk->sk_family == AF_INET || sk->sk_family == AF_INET6;
0271 }
0272
0273
0274
0275
0276
0277
0278
0279
0280 static inline struct sock *sk_to_full_sk(struct sock *sk)
0281 {
0282 #ifdef CONFIG_INET
0283 if (sk && sk->sk_state == TCP_NEW_SYN_RECV)
0284 sk = inet_reqsk(sk)->rsk_listener;
0285 #endif
0286 return sk;
0287 }
0288
0289
0290 static inline const struct sock *sk_const_to_full_sk(const struct sock *sk)
0291 {
0292 #ifdef CONFIG_INET
0293 if (sk && sk->sk_state == TCP_NEW_SYN_RECV)
0294 sk = ((const struct request_sock *)sk)->rsk_listener;
0295 #endif
0296 return sk;
0297 }
0298
0299 static inline struct sock *skb_to_full_sk(const struct sk_buff *skb)
0300 {
0301 return sk_to_full_sk(skb->sk);
0302 }
0303
0304 static inline struct inet_sock *inet_sk(const struct sock *sk)
0305 {
0306 return (struct inet_sock *)sk;
0307 }
0308
0309 static inline void __inet_sk_copy_descendant(struct sock *sk_to,
0310 const struct sock *sk_from,
0311 const int ancestor_size)
0312 {
0313 memcpy(inet_sk(sk_to) + 1, inet_sk(sk_from) + 1,
0314 sk_from->sk_prot->obj_size - ancestor_size);
0315 }
0316
0317 int inet_sk_rebuild_header(struct sock *sk);
0318
0319
0320
0321
0322
0323
0324
0325
0326 static inline int inet_sk_state_load(const struct sock *sk)
0327 {
0328
0329 return smp_load_acquire(&sk->sk_state);
0330 }
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340 void inet_sk_state_store(struct sock *sk, int newstate);
0341
0342 void inet_sk_set_state(struct sock *sk, int state);
0343
0344 static inline unsigned int __inet_ehashfn(const __be32 laddr,
0345 const __u16 lport,
0346 const __be32 faddr,
0347 const __be16 fport,
0348 u32 initval)
0349 {
0350 return jhash_3words((__force __u32) laddr,
0351 (__force __u32) faddr,
0352 ((__u32) lport) << 16 | (__force __u32)fport,
0353 initval);
0354 }
0355
0356 struct request_sock *inet_reqsk_alloc(const struct request_sock_ops *ops,
0357 struct sock *sk_listener,
0358 bool attach_listener);
0359
0360 static inline __u8 inet_sk_flowi_flags(const struct sock *sk)
0361 {
0362 __u8 flags = 0;
0363
0364 if (inet_sk(sk)->transparent || inet_sk(sk)->hdrincl)
0365 flags |= FLOWI_FLAG_ANYSRC;
0366 return flags;
0367 }
0368
0369 static inline void inet_inc_convert_csum(struct sock *sk)
0370 {
0371 inet_sk(sk)->convert_csum++;
0372 }
0373
0374 static inline void inet_dec_convert_csum(struct sock *sk)
0375 {
0376 if (inet_sk(sk)->convert_csum > 0)
0377 inet_sk(sk)->convert_csum--;
0378 }
0379
0380 static inline bool inet_get_convert_csum(struct sock *sk)
0381 {
0382 return !!inet_sk(sk)->convert_csum;
0383 }
0384
0385
0386 static inline bool inet_can_nonlocal_bind(struct net *net,
0387 struct inet_sock *inet)
0388 {
0389 return READ_ONCE(net->ipv4.sysctl_ip_nonlocal_bind) ||
0390 inet->freebind || inet->transparent;
0391 }
0392
0393 static inline bool inet_addr_valid_or_nonlocal(struct net *net,
0394 struct inet_sock *inet,
0395 __be32 addr,
0396 int addr_type)
0397 {
0398 return inet_can_nonlocal_bind(net, inet) ||
0399 addr == htonl(INADDR_ANY) ||
0400 addr_type == RTN_LOCAL ||
0401 addr_type == RTN_MULTICAST ||
0402 addr_type == RTN_BROADCAST;
0403 }
0404
0405 #endif