inet_sock.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  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. * Definitions for inet_sock
  8. *
  9. * Authors: Many, reorganised here by
  10. * Arnaldo Carvalho de Melo <[email protected]>
  11. */
  12. #ifndef _INET_SOCK_H
  13. #define _INET_SOCK_H
  14. #include <linux/bitops.h>
  15. #include <linux/string.h>
  16. #include <linux/types.h>
  17. #include <linux/jhash.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/android_kabi.h>
  20. #include <net/flow.h>
  21. #include <net/sock.h>
  22. #include <net/request_sock.h>
  23. #include <net/netns/hash.h>
  24. #include <net/tcp_states.h>
  25. #include <net/l3mdev.h>
  26. /** struct ip_options - IP Options
  27. *
  28. * @faddr - Saved first hop address
  29. * @nexthop - Saved nexthop address in LSRR and SSRR
  30. * @is_strictroute - Strict source route
  31. * @srr_is_hit - Packet destination addr was our one
  32. * @is_changed - IP checksum more not valid
  33. * @rr_needaddr - Need to record addr of outgoing dev
  34. * @ts_needtime - Need to record timestamp
  35. * @ts_needaddr - Need to record addr of outgoing dev
  36. */
  37. struct ip_options {
  38. __be32 faddr;
  39. __be32 nexthop;
  40. unsigned char optlen;
  41. unsigned char srr;
  42. unsigned char rr;
  43. unsigned char ts;
  44. unsigned char is_strictroute:1,
  45. srr_is_hit:1,
  46. is_changed:1,
  47. rr_needaddr:1,
  48. ts_needtime:1,
  49. ts_needaddr:1;
  50. unsigned char router_alert;
  51. unsigned char cipso;
  52. unsigned char __pad2;
  53. unsigned char __data[];
  54. };
  55. struct ip_options_rcu {
  56. struct rcu_head rcu;
  57. struct ip_options opt;
  58. };
  59. struct ip_options_data {
  60. struct ip_options_rcu opt;
  61. char data[40];
  62. };
  63. struct inet_request_sock {
  64. struct request_sock req;
  65. #define ir_loc_addr req.__req_common.skc_rcv_saddr
  66. #define ir_rmt_addr req.__req_common.skc_daddr
  67. #define ir_num req.__req_common.skc_num
  68. #define ir_rmt_port req.__req_common.skc_dport
  69. #define ir_v6_rmt_addr req.__req_common.skc_v6_daddr
  70. #define ir_v6_loc_addr req.__req_common.skc_v6_rcv_saddr
  71. #define ir_iif req.__req_common.skc_bound_dev_if
  72. #define ir_cookie req.__req_common.skc_cookie
  73. #define ireq_net req.__req_common.skc_net
  74. #define ireq_state req.__req_common.skc_state
  75. #define ireq_family req.__req_common.skc_family
  76. u16 snd_wscale : 4,
  77. rcv_wscale : 4,
  78. tstamp_ok : 1,
  79. sack_ok : 1,
  80. wscale_ok : 1,
  81. ecn_ok : 1,
  82. acked : 1,
  83. no_srccheck: 1,
  84. smc_ok : 1;
  85. u32 ir_mark;
  86. union {
  87. struct ip_options_rcu __rcu *ireq_opt;
  88. #if IS_ENABLED(CONFIG_IPV6)
  89. struct {
  90. struct ipv6_txoptions *ipv6_opt;
  91. struct sk_buff *pktopts;
  92. };
  93. #endif
  94. };
  95. };
  96. static inline struct inet_request_sock *inet_rsk(const struct request_sock *sk)
  97. {
  98. return (struct inet_request_sock *)sk;
  99. }
  100. static inline u32 inet_request_mark(const struct sock *sk, struct sk_buff *skb)
  101. {
  102. u32 mark = READ_ONCE(sk->sk_mark);
  103. if (!mark && READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fwmark_accept))
  104. return skb->mark;
  105. return mark;
  106. }
  107. static inline int inet_request_bound_dev_if(const struct sock *sk,
  108. struct sk_buff *skb)
  109. {
  110. int bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
  111. #ifdef CONFIG_NET_L3_MASTER_DEV
  112. struct net *net = sock_net(sk);
  113. if (!bound_dev_if && READ_ONCE(net->ipv4.sysctl_tcp_l3mdev_accept))
  114. return l3mdev_master_ifindex_by_index(net, skb->skb_iif);
  115. #endif
  116. return bound_dev_if;
  117. }
  118. static inline int inet_sk_bound_l3mdev(const struct sock *sk)
  119. {
  120. #ifdef CONFIG_NET_L3_MASTER_DEV
  121. struct net *net = sock_net(sk);
  122. if (!READ_ONCE(net->ipv4.sysctl_tcp_l3mdev_accept))
  123. return l3mdev_master_ifindex_by_index(net,
  124. sk->sk_bound_dev_if);
  125. #endif
  126. return 0;
  127. }
  128. static inline bool inet_bound_dev_eq(bool l3mdev_accept, int bound_dev_if,
  129. int dif, int sdif)
  130. {
  131. if (!bound_dev_if)
  132. return !sdif || l3mdev_accept;
  133. return bound_dev_if == dif || bound_dev_if == sdif;
  134. }
  135. static inline bool inet_sk_bound_dev_eq(struct net *net, int bound_dev_if,
  136. int dif, int sdif)
  137. {
  138. #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
  139. return inet_bound_dev_eq(!!READ_ONCE(net->ipv4.sysctl_tcp_l3mdev_accept),
  140. bound_dev_if, dif, sdif);
  141. #else
  142. return inet_bound_dev_eq(true, bound_dev_if, dif, sdif);
  143. #endif
  144. }
  145. struct inet_cork {
  146. unsigned int flags;
  147. __be32 addr;
  148. struct ip_options *opt;
  149. unsigned int fragsize;
  150. int length; /* Total length of all frames */
  151. struct dst_entry *dst;
  152. u8 tx_flags;
  153. __u8 ttl;
  154. __s16 tos;
  155. char priority;
  156. __u16 gso_size;
  157. u64 transmit_time;
  158. u32 mark;
  159. };
  160. struct inet_cork_full {
  161. struct inet_cork base;
  162. struct flowi fl;
  163. };
  164. struct ip_mc_socklist;
  165. struct ipv6_pinfo;
  166. struct rtable;
  167. /** struct inet_sock - representation of INET sockets
  168. *
  169. * @sk - ancestor class
  170. * @pinet6 - pointer to IPv6 control block
  171. * @inet_daddr - Foreign IPv4 addr
  172. * @inet_rcv_saddr - Bound local IPv4 addr
  173. * @inet_dport - Destination port
  174. * @inet_num - Local port
  175. * @inet_saddr - Sending source
  176. * @uc_ttl - Unicast TTL
  177. * @inet_sport - Source port
  178. * @inet_id - ID counter for DF pkts
  179. * @tos - TOS
  180. * @mc_ttl - Multicasting TTL
  181. * @is_icsk - is this an inet_connection_sock?
  182. * @uc_index - Unicast outgoing device index
  183. * @mc_index - Multicast device index
  184. * @mc_list - Group array
  185. * @cork - info to build ip hdr on each ip frag while socket is corked
  186. */
  187. struct inet_sock {
  188. /* sk and pinet6 has to be the first two members of inet_sock */
  189. struct sock sk;
  190. #if IS_ENABLED(CONFIG_IPV6)
  191. struct ipv6_pinfo *pinet6;
  192. #endif
  193. /* Socket demultiplex comparisons on incoming packets. */
  194. #define inet_daddr sk.__sk_common.skc_daddr
  195. #define inet_rcv_saddr sk.__sk_common.skc_rcv_saddr
  196. #define inet_dport sk.__sk_common.skc_dport
  197. #define inet_num sk.__sk_common.skc_num
  198. __be32 inet_saddr;
  199. __s16 uc_ttl;
  200. __u16 cmsg_flags;
  201. struct ip_options_rcu __rcu *inet_opt;
  202. __be16 inet_sport;
  203. __u16 inet_id;
  204. __u8 tos;
  205. __u8 min_ttl;
  206. __u8 mc_ttl;
  207. __u8 pmtudisc;
  208. __u8 recverr:1,
  209. is_icsk:1,
  210. freebind:1,
  211. hdrincl:1,
  212. mc_loop:1,
  213. transparent:1,
  214. mc_all:1,
  215. nodefrag:1;
  216. __u8 bind_address_no_port:1,
  217. recverr_rfc4884:1,
  218. defer_connect:1; /* Indicates that fastopen_connect is set
  219. * and cookie exists so we defer connect
  220. * until first data frame is written
  221. */
  222. __u8 rcv_tos;
  223. __u8 convert_csum;
  224. int uc_index;
  225. int mc_index;
  226. __be32 mc_addr;
  227. struct ip_mc_socklist __rcu *mc_list;
  228. struct inet_cork_full cork;
  229. struct {
  230. __u16 lo;
  231. __u16 hi;
  232. } local_port_range;
  233. ANDROID_KABI_RESERVE(1);
  234. ANDROID_KABI_RESERVE(2);
  235. };
  236. #define IPCORK_OPT 1 /* ip-options has been held in ipcork.opt */
  237. #define IPCORK_ALLFRAG 2 /* always fragment (for ipv6 for now) */
  238. /* cmsg flags for inet */
  239. #define IP_CMSG_PKTINFO BIT(0)
  240. #define IP_CMSG_TTL BIT(1)
  241. #define IP_CMSG_TOS BIT(2)
  242. #define IP_CMSG_RECVOPTS BIT(3)
  243. #define IP_CMSG_RETOPTS BIT(4)
  244. #define IP_CMSG_PASSSEC BIT(5)
  245. #define IP_CMSG_ORIGDSTADDR BIT(6)
  246. #define IP_CMSG_CHECKSUM BIT(7)
  247. #define IP_CMSG_RECVFRAGSIZE BIT(8)
  248. static inline bool sk_is_inet(struct sock *sk)
  249. {
  250. return sk->sk_family == AF_INET || sk->sk_family == AF_INET6;
  251. }
  252. /**
  253. * sk_to_full_sk - Access to a full socket
  254. * @sk: pointer to a socket
  255. *
  256. * SYNACK messages might be attached to request sockets.
  257. * Some places want to reach the listener in this case.
  258. */
  259. static inline struct sock *sk_to_full_sk(struct sock *sk)
  260. {
  261. #ifdef CONFIG_INET
  262. if (sk && sk->sk_state == TCP_NEW_SYN_RECV)
  263. sk = inet_reqsk(sk)->rsk_listener;
  264. #endif
  265. return sk;
  266. }
  267. /* sk_to_full_sk() variant with a const argument */
  268. static inline const struct sock *sk_const_to_full_sk(const struct sock *sk)
  269. {
  270. #ifdef CONFIG_INET
  271. if (sk && sk->sk_state == TCP_NEW_SYN_RECV)
  272. sk = ((const struct request_sock *)sk)->rsk_listener;
  273. #endif
  274. return sk;
  275. }
  276. static inline struct sock *skb_to_full_sk(const struct sk_buff *skb)
  277. {
  278. return sk_to_full_sk(skb->sk);
  279. }
  280. static inline struct inet_sock *inet_sk(const struct sock *sk)
  281. {
  282. return (struct inet_sock *)sk;
  283. }
  284. static inline void __inet_sk_copy_descendant(struct sock *sk_to,
  285. const struct sock *sk_from,
  286. const int ancestor_size)
  287. {
  288. memcpy(inet_sk(sk_to) + 1, inet_sk(sk_from) + 1,
  289. sk_from->sk_prot->obj_size - ancestor_size);
  290. }
  291. int inet_sk_rebuild_header(struct sock *sk);
  292. /**
  293. * inet_sk_state_load - read sk->sk_state for lockless contexts
  294. * @sk: socket pointer
  295. *
  296. * Paired with inet_sk_state_store(). Used in places we don't hold socket lock:
  297. * tcp_diag_get_info(), tcp_get_info(), tcp_poll(), get_tcp4_sock() ...
  298. */
  299. static inline int inet_sk_state_load(const struct sock *sk)
  300. {
  301. /* state change might impact lockless readers. */
  302. return smp_load_acquire(&sk->sk_state);
  303. }
  304. /**
  305. * inet_sk_state_store - update sk->sk_state
  306. * @sk: socket pointer
  307. * @newstate: new state
  308. *
  309. * Paired with inet_sk_state_load(). Should be used in contexts where
  310. * state change might impact lockless readers.
  311. */
  312. void inet_sk_state_store(struct sock *sk, int newstate);
  313. void inet_sk_set_state(struct sock *sk, int state);
  314. static inline unsigned int __inet_ehashfn(const __be32 laddr,
  315. const __u16 lport,
  316. const __be32 faddr,
  317. const __be16 fport,
  318. u32 initval)
  319. {
  320. return jhash_3words((__force __u32) laddr,
  321. (__force __u32) faddr,
  322. ((__u32) lport) << 16 | (__force __u32)fport,
  323. initval);
  324. }
  325. struct request_sock *inet_reqsk_alloc(const struct request_sock_ops *ops,
  326. struct sock *sk_listener,
  327. bool attach_listener);
  328. static inline __u8 inet_sk_flowi_flags(const struct sock *sk)
  329. {
  330. __u8 flags = 0;
  331. if (inet_sk(sk)->transparent || inet_sk(sk)->hdrincl)
  332. flags |= FLOWI_FLAG_ANYSRC;
  333. return flags;
  334. }
  335. static inline void inet_inc_convert_csum(struct sock *sk)
  336. {
  337. inet_sk(sk)->convert_csum++;
  338. }
  339. static inline void inet_dec_convert_csum(struct sock *sk)
  340. {
  341. if (inet_sk(sk)->convert_csum > 0)
  342. inet_sk(sk)->convert_csum--;
  343. }
  344. static inline bool inet_get_convert_csum(struct sock *sk)
  345. {
  346. return !!inet_sk(sk)->convert_csum;
  347. }
  348. static inline bool inet_can_nonlocal_bind(struct net *net,
  349. struct inet_sock *inet)
  350. {
  351. return READ_ONCE(net->ipv4.sysctl_ip_nonlocal_bind) ||
  352. inet->freebind || inet->transparent;
  353. }
  354. static inline bool inet_addr_valid_or_nonlocal(struct net *net,
  355. struct inet_sock *inet,
  356. __be32 addr,
  357. int addr_type)
  358. {
  359. return inet_can_nonlocal_bind(net, inet) ||
  360. addr == htonl(INADDR_ANY) ||
  361. addr_type == RTN_LOCAL ||
  362. addr_type == RTN_MULTICAST ||
  363. addr_type == RTN_BROADCAST;
  364. }
  365. #endif /* _INET_SOCK_H */