inet_sock.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 <acme@mandriva.com>
  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 <net/flow.h>
  20. #include <net/sock.h>
  21. #include <net/request_sock.h>
  22. #include <net/netns/hash.h>
  23. #include <net/tcp_states.h>
  24. #include <net/l3mdev.h>
  25. /** struct ip_options - IP Options
  26. *
  27. * @faddr - Saved first hop address
  28. * @nexthop - Saved nexthop address in LSRR and SSRR
  29. * @is_strictroute - Strict source route
  30. * @srr_is_hit - Packet destination addr was our one
  31. * @is_changed - IP checksum more not valid
  32. * @rr_needaddr - Need to record addr of outgoing dev
  33. * @ts_needtime - Need to record timestamp
  34. * @ts_needaddr - Need to record addr of outgoing dev
  35. */
  36. struct ip_options {
  37. __be32 faddr;
  38. __be32 nexthop;
  39. unsigned char optlen;
  40. unsigned char srr;
  41. unsigned char rr;
  42. unsigned char ts;
  43. unsigned char is_strictroute:1,
  44. srr_is_hit:1,
  45. is_changed:1,
  46. rr_needaddr:1,
  47. ts_needtime:1,
  48. ts_needaddr:1;
  49. unsigned char router_alert;
  50. unsigned char cipso;
  51. unsigned char __pad2;
  52. unsigned char __data[];
  53. };
  54. struct ip_options_rcu {
  55. struct rcu_head rcu;
  56. struct ip_options opt;
  57. };
  58. struct ip_options_data {
  59. struct ip_options_rcu opt;
  60. char data[40];
  61. };
  62. struct inet_request_sock {
  63. struct request_sock req;
  64. #define ir_loc_addr req.__req_common.skc_rcv_saddr
  65. #define ir_rmt_addr req.__req_common.skc_daddr
  66. #define ir_num req.__req_common.skc_num
  67. #define ir_rmt_port req.__req_common.skc_dport
  68. #define ir_v6_rmt_addr req.__req_common.skc_v6_daddr
  69. #define ir_v6_loc_addr req.__req_common.skc_v6_rcv_saddr
  70. #define ir_iif req.__req_common.skc_bound_dev_if
  71. #define ir_cookie req.__req_common.skc_cookie
  72. #define ireq_net req.__req_common.skc_net
  73. #define ireq_state req.__req_common.skc_state
  74. #define ireq_family req.__req_common.skc_family
  75. u16 snd_wscale : 4,
  76. rcv_wscale : 4,
  77. tstamp_ok : 1,
  78. sack_ok : 1,
  79. wscale_ok : 1,
  80. ecn_ok : 1,
  81. acked : 1,
  82. no_srccheck: 1,
  83. smc_ok : 1;
  84. u32 ir_mark;
  85. union {
  86. struct ip_options_rcu __rcu *ireq_opt;
  87. #if IS_ENABLED(CONFIG_IPV6)
  88. struct {
  89. struct ipv6_txoptions *ipv6_opt;
  90. struct sk_buff *pktopts;
  91. };
  92. #endif
  93. };
  94. };
  95. static inline struct inet_request_sock *inet_rsk(const struct request_sock *sk)
  96. {
  97. return (struct inet_request_sock *)sk;
  98. }
  99. static inline u32 inet_request_mark(const struct sock *sk, struct sk_buff *skb)
  100. {
  101. u32 mark = READ_ONCE(sk->sk_mark);
  102. if (!mark && READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_fwmark_accept))
  103. return skb->mark;
  104. return mark;
  105. }
  106. static inline int inet_request_bound_dev_if(const struct sock *sk,
  107. struct sk_buff *skb)
  108. {
  109. int bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
  110. #ifdef CONFIG_NET_L3_MASTER_DEV
  111. struct net *net = sock_net(sk);
  112. if (!bound_dev_if && READ_ONCE(net->ipv4.sysctl_tcp_l3mdev_accept))
  113. return l3mdev_master_ifindex_by_index(net, skb->skb_iif);
  114. #endif
  115. return bound_dev_if;
  116. }
  117. static inline int inet_sk_bound_l3mdev(const struct sock *sk)
  118. {
  119. #ifdef CONFIG_NET_L3_MASTER_DEV
  120. struct net *net = sock_net(sk);
  121. if (!READ_ONCE(net->ipv4.sysctl_tcp_l3mdev_accept))
  122. return l3mdev_master_ifindex_by_index(net,
  123. sk->sk_bound_dev_if);
  124. #endif
  125. return 0;
  126. }
  127. static inline bool inet_bound_dev_eq(bool l3mdev_accept, int bound_dev_if,
  128. int dif, int sdif)
  129. {
  130. if (!bound_dev_if)
  131. return !sdif || l3mdev_accept;
  132. return bound_dev_if == dif || bound_dev_if == sdif;
  133. }
  134. static inline bool inet_sk_bound_dev_eq(const struct net *net,
  135. 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_flags - various atomic flags
  176. * @inet_saddr - Sending source
  177. * @uc_ttl - Unicast TTL
  178. * @inet_sport - Source port
  179. * @inet_id - ID counter for DF pkts
  180. * @tos - TOS
  181. * @mc_ttl - Multicasting TTL
  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. unsigned long inet_flags;
  199. __be32 inet_saddr;
  200. __s16 uc_ttl;
  201. __be16 inet_sport;
  202. struct ip_options_rcu __rcu *inet_opt;
  203. atomic_t inet_id;
  204. __u8 tos;
  205. __u8 min_ttl;
  206. __u8 mc_ttl;
  207. __u8 pmtudisc;
  208. __u8 rcv_tos;
  209. __u8 convert_csum;
  210. int uc_index;
  211. int mc_index;
  212. __be32 mc_addr;
  213. u32 local_port_range; /* high << 16 | low */
  214. struct ip_mc_socklist __rcu *mc_list;
  215. struct inet_cork_full cork;
  216. };
  217. #define IPCORK_OPT 1 /* ip-options has been held in ipcork.opt */
  218. enum {
  219. INET_FLAGS_PKTINFO = 0,
  220. INET_FLAGS_TTL = 1,
  221. INET_FLAGS_TOS = 2,
  222. INET_FLAGS_RECVOPTS = 3,
  223. INET_FLAGS_RETOPTS = 4,
  224. INET_FLAGS_PASSSEC = 5,
  225. INET_FLAGS_ORIGDSTADDR = 6,
  226. INET_FLAGS_CHECKSUM = 7,
  227. INET_FLAGS_RECVFRAGSIZE = 8,
  228. INET_FLAGS_RECVERR = 9,
  229. INET_FLAGS_RECVERR_RFC4884 = 10,
  230. INET_FLAGS_FREEBIND = 11,
  231. INET_FLAGS_HDRINCL = 12,
  232. INET_FLAGS_MC_LOOP = 13,
  233. INET_FLAGS_MC_ALL = 14,
  234. INET_FLAGS_TRANSPARENT = 15,
  235. INET_FLAGS_IS_ICSK = 16,
  236. INET_FLAGS_NODEFRAG = 17,
  237. INET_FLAGS_BIND_ADDRESS_NO_PORT = 18,
  238. INET_FLAGS_DEFER_CONNECT = 19,
  239. INET_FLAGS_MC6_LOOP = 20,
  240. INET_FLAGS_RECVERR6_RFC4884 = 21,
  241. INET_FLAGS_MC6_ALL = 22,
  242. INET_FLAGS_AUTOFLOWLABEL_SET = 23,
  243. INET_FLAGS_AUTOFLOWLABEL = 24,
  244. INET_FLAGS_DONTFRAG = 25,
  245. INET_FLAGS_RECVERR6 = 26,
  246. INET_FLAGS_REPFLOW = 27,
  247. INET_FLAGS_RTALERT_ISOLATE = 28,
  248. INET_FLAGS_SNDFLOW = 29,
  249. INET_FLAGS_RTALERT = 30,
  250. };
  251. /* cmsg flags for inet */
  252. #define IP_CMSG_PKTINFO BIT(INET_FLAGS_PKTINFO)
  253. #define IP_CMSG_TTL BIT(INET_FLAGS_TTL)
  254. #define IP_CMSG_TOS BIT(INET_FLAGS_TOS)
  255. #define IP_CMSG_RECVOPTS BIT(INET_FLAGS_RECVOPTS)
  256. #define IP_CMSG_RETOPTS BIT(INET_FLAGS_RETOPTS)
  257. #define IP_CMSG_PASSSEC BIT(INET_FLAGS_PASSSEC)
  258. #define IP_CMSG_ORIGDSTADDR BIT(INET_FLAGS_ORIGDSTADDR)
  259. #define IP_CMSG_CHECKSUM BIT(INET_FLAGS_CHECKSUM)
  260. #define IP_CMSG_RECVFRAGSIZE BIT(INET_FLAGS_RECVFRAGSIZE)
  261. #define IP_CMSG_ALL (IP_CMSG_PKTINFO | IP_CMSG_TTL | \
  262. IP_CMSG_TOS | IP_CMSG_RECVOPTS | \
  263. IP_CMSG_RETOPTS | IP_CMSG_PASSSEC | \
  264. IP_CMSG_ORIGDSTADDR | IP_CMSG_CHECKSUM | \
  265. IP_CMSG_RECVFRAGSIZE)
  266. static inline unsigned long inet_cmsg_flags(const struct inet_sock *inet)
  267. {
  268. return READ_ONCE(inet->inet_flags) & IP_CMSG_ALL;
  269. }
  270. #define inet_test_bit(nr, sk) \
  271. test_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags)
  272. #define inet_set_bit(nr, sk) \
  273. set_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags)
  274. #define inet_clear_bit(nr, sk) \
  275. clear_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags)
  276. #define inet_assign_bit(nr, sk, val) \
  277. assign_bit(INET_FLAGS_##nr, &inet_sk(sk)->inet_flags, val)
  278. /**
  279. * sk_to_full_sk - Access to a full socket
  280. * @sk: pointer to a socket
  281. *
  282. * SYNACK messages might be attached to request sockets.
  283. * Some places want to reach the listener in this case.
  284. */
  285. static inline struct sock *sk_to_full_sk(struct sock *sk)
  286. {
  287. #ifdef CONFIG_INET
  288. if (sk && sk->sk_state == TCP_NEW_SYN_RECV)
  289. sk = inet_reqsk(sk)->rsk_listener;
  290. #endif
  291. return sk;
  292. }
  293. /* sk_to_full_sk() variant with a const argument */
  294. static inline const struct sock *sk_const_to_full_sk(const struct sock *sk)
  295. {
  296. #ifdef CONFIG_INET
  297. if (sk && sk->sk_state == TCP_NEW_SYN_RECV)
  298. sk = ((const struct request_sock *)sk)->rsk_listener;
  299. #endif
  300. return sk;
  301. }
  302. static inline struct sock *skb_to_full_sk(const struct sk_buff *skb)
  303. {
  304. return sk_to_full_sk(skb->sk);
  305. }
  306. #define inet_sk(ptr) container_of_const(ptr, struct inet_sock, sk)
  307. static inline void __inet_sk_copy_descendant(struct sock *sk_to,
  308. const struct sock *sk_from,
  309. const int ancestor_size)
  310. {
  311. memcpy(inet_sk(sk_to) + 1, inet_sk(sk_from) + 1,
  312. sk_from->sk_prot->obj_size - ancestor_size);
  313. }
  314. int inet_sk_rebuild_header(struct sock *sk);
  315. /**
  316. * inet_sk_state_load - read sk->sk_state for lockless contexts
  317. * @sk: socket pointer
  318. *
  319. * Paired with inet_sk_state_store(). Used in places we don't hold socket lock:
  320. * tcp_diag_get_info(), tcp_get_info(), tcp_poll(), get_tcp4_sock() ...
  321. */
  322. static inline int inet_sk_state_load(const struct sock *sk)
  323. {
  324. /* state change might impact lockless readers. */
  325. return smp_load_acquire(&sk->sk_state);
  326. }
  327. /**
  328. * inet_sk_state_store - update sk->sk_state
  329. * @sk: socket pointer
  330. * @newstate: new state
  331. *
  332. * Paired with inet_sk_state_load(). Should be used in contexts where
  333. * state change might impact lockless readers.
  334. */
  335. void inet_sk_state_store(struct sock *sk, int newstate);
  336. void inet_sk_set_state(struct sock *sk, int state);
  337. static inline unsigned int __inet_ehashfn(const __be32 laddr,
  338. const __u16 lport,
  339. const __be32 faddr,
  340. const __be16 fport,
  341. u32 initval)
  342. {
  343. return jhash_3words((__force __u32) laddr,
  344. (__force __u32) faddr,
  345. ((__u32) lport) << 16 | (__force __u32)fport,
  346. initval);
  347. }
  348. struct request_sock *inet_reqsk_alloc(const struct request_sock_ops *ops,
  349. struct sock *sk_listener,
  350. bool attach_listener);
  351. static inline __u8 inet_sk_flowi_flags(const struct sock *sk)
  352. {
  353. __u8 flags = 0;
  354. if (inet_test_bit(TRANSPARENT, sk) || inet_test_bit(HDRINCL, sk))
  355. flags |= FLOWI_FLAG_ANYSRC;
  356. return flags;
  357. }
  358. static inline void inet_inc_convert_csum(struct sock *sk)
  359. {
  360. inet_sk(sk)->convert_csum++;
  361. }
  362. static inline void inet_dec_convert_csum(struct sock *sk)
  363. {
  364. if (inet_sk(sk)->convert_csum > 0)
  365. inet_sk(sk)->convert_csum--;
  366. }
  367. static inline bool inet_get_convert_csum(struct sock *sk)
  368. {
  369. return !!inet_sk(sk)->convert_csum;
  370. }
  371. static inline bool inet_can_nonlocal_bind(struct net *net,
  372. struct inet_sock *inet)
  373. {
  374. return READ_ONCE(net->ipv4.sysctl_ip_nonlocal_bind) ||
  375. test_bit(INET_FLAGS_FREEBIND, &inet->inet_flags) ||
  376. test_bit(INET_FLAGS_TRANSPARENT, &inet->inet_flags);
  377. }
  378. static inline bool inet_addr_valid_or_nonlocal(struct net *net,
  379. struct inet_sock *inet,
  380. __be32 addr,
  381. int addr_type)
  382. {
  383. return inet_can_nonlocal_bind(net, inet) ||
  384. addr == htonl(INADDR_ANY) ||
  385. addr_type == RTN_LOCAL ||
  386. addr_type == RTN_MULTICAST ||
  387. addr_type == RTN_BROADCAST;
  388. }
  389. #endif /* _INET_SOCK_H */