inet_sock.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * Definitions for inet_sock
  7. *
  8. * Authors: Many, reorganised here by
  9. * Arnaldo Carvalho de Melo <acme@mandriva.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #ifndef _INET_SOCK_H
  17. #define _INET_SOCK_H
  18. #include <linux/bitops.h>
  19. #include <linux/string.h>
  20. #include <linux/types.h>
  21. #include <linux/jhash.h>
  22. #include <linux/netdevice.h>
  23. #include <net/flow.h>
  24. #include <net/sock.h>
  25. #include <net/request_sock.h>
  26. #include <net/netns/hash.h>
  27. #include <net/tcp_states.h>
  28. #include <net/l3mdev.h>
  29. /** struct ip_options - IP Options
  30. *
  31. * @faddr - Saved first hop address
  32. * @nexthop - Saved nexthop address in LSRR and SSRR
  33. * @is_strictroute - Strict source route
  34. * @srr_is_hit - Packet destination addr was our one
  35. * @is_changed - IP checksum more not valid
  36. * @rr_needaddr - Need to record addr of outgoing dev
  37. * @ts_needtime - Need to record timestamp
  38. * @ts_needaddr - Need to record addr of outgoing dev
  39. */
  40. struct ip_options {
  41. __be32 faddr;
  42. __be32 nexthop;
  43. unsigned char optlen;
  44. unsigned char srr;
  45. unsigned char rr;
  46. unsigned char ts;
  47. unsigned char is_strictroute:1,
  48. srr_is_hit:1,
  49. is_changed:1,
  50. rr_needaddr:1,
  51. ts_needtime:1,
  52. ts_needaddr:1;
  53. unsigned char router_alert;
  54. unsigned char cipso;
  55. unsigned char __pad2;
  56. unsigned char __data[0];
  57. };
  58. struct ip_options_rcu {
  59. struct rcu_head rcu;
  60. struct ip_options opt;
  61. };
  62. struct ip_options_data {
  63. struct ip_options_rcu opt;
  64. char data[40];
  65. };
  66. struct inet_request_sock {
  67. struct request_sock req;
  68. #define ir_loc_addr req.__req_common.skc_rcv_saddr
  69. #define ir_rmt_addr req.__req_common.skc_daddr
  70. #define ir_num req.__req_common.skc_num
  71. #define ir_rmt_port req.__req_common.skc_dport
  72. #define ir_v6_rmt_addr req.__req_common.skc_v6_daddr
  73. #define ir_v6_loc_addr req.__req_common.skc_v6_rcv_saddr
  74. #define ir_iif req.__req_common.skc_bound_dev_if
  75. #define ir_cookie req.__req_common.skc_cookie
  76. #define ireq_net req.__req_common.skc_net
  77. #define ireq_state req.__req_common.skc_state
  78. #define ireq_family req.__req_common.skc_family
  79. u16 snd_wscale : 4,
  80. rcv_wscale : 4,
  81. tstamp_ok : 1,
  82. sack_ok : 1,
  83. wscale_ok : 1,
  84. ecn_ok : 1,
  85. acked : 1,
  86. no_srccheck: 1;
  87. u32 ir_mark;
  88. union {
  89. struct ip_options_rcu __rcu *ireq_opt;
  90. #if IS_ENABLED(CONFIG_IPV6)
  91. struct {
  92. struct ipv6_txoptions *ipv6_opt;
  93. struct sk_buff *pktopts;
  94. };
  95. #endif
  96. };
  97. };
  98. static inline struct inet_request_sock *inet_rsk(const struct request_sock *sk)
  99. {
  100. return (struct inet_request_sock *)sk;
  101. }
  102. static inline u32 inet_request_mark(const struct sock *sk, struct sk_buff *skb)
  103. {
  104. if (!sk->sk_mark && sock_net(sk)->ipv4.sysctl_tcp_fwmark_accept)
  105. return skb->mark;
  106. return sk->sk_mark;
  107. }
  108. static inline int inet_request_bound_dev_if(const struct sock *sk,
  109. struct sk_buff *skb)
  110. {
  111. #ifdef CONFIG_NET_L3_MASTER_DEV
  112. struct net *net = sock_net(sk);
  113. if (!sk->sk_bound_dev_if && net->ipv4.sysctl_tcp_l3mdev_accept)
  114. return l3mdev_master_ifindex_by_index(net, skb->skb_iif);
  115. #endif
  116. return sk->sk_bound_dev_if;
  117. }
  118. struct inet_cork {
  119. unsigned int flags;
  120. __be32 addr;
  121. struct ip_options *opt;
  122. unsigned int fragsize;
  123. int length; /* Total length of all frames */
  124. struct dst_entry *dst;
  125. u8 tx_flags;
  126. __u8 ttl;
  127. __s16 tos;
  128. char priority;
  129. __u16 gso_size;
  130. };
  131. struct inet_cork_full {
  132. struct inet_cork base;
  133. struct flowi fl;
  134. };
  135. struct ip_mc_socklist;
  136. struct ipv6_pinfo;
  137. struct rtable;
  138. /** struct inet_sock - representation of INET sockets
  139. *
  140. * @sk - ancestor class
  141. * @pinet6 - pointer to IPv6 control block
  142. * @inet_daddr - Foreign IPv4 addr
  143. * @inet_rcv_saddr - Bound local IPv4 addr
  144. * @inet_dport - Destination port
  145. * @inet_num - Local port
  146. * @inet_saddr - Sending source
  147. * @uc_ttl - Unicast TTL
  148. * @inet_sport - Source port
  149. * @inet_id - ID counter for DF pkts
  150. * @tos - TOS
  151. * @mc_ttl - Multicasting TTL
  152. * @is_icsk - is this an inet_connection_sock?
  153. * @uc_index - Unicast outgoing device index
  154. * @mc_index - Multicast device index
  155. * @mc_list - Group array
  156. * @cork - info to build ip hdr on each ip frag while socket is corked
  157. */
  158. struct inet_sock {
  159. /* sk and pinet6 has to be the first two members of inet_sock */
  160. struct sock sk;
  161. #if IS_ENABLED(CONFIG_IPV6)
  162. struct ipv6_pinfo *pinet6;
  163. #endif
  164. /* Socket demultiplex comparisons on incoming packets. */
  165. #define inet_daddr sk.__sk_common.skc_daddr
  166. #define inet_rcv_saddr sk.__sk_common.skc_rcv_saddr
  167. #define inet_dport sk.__sk_common.skc_dport
  168. #define inet_num sk.__sk_common.skc_num
  169. __be32 inet_saddr;
  170. __s16 uc_ttl;
  171. __u16 cmsg_flags;
  172. __be16 inet_sport;
  173. __u16 inet_id;
  174. struct ip_options_rcu __rcu *inet_opt;
  175. int rx_dst_ifindex;
  176. __u8 tos;
  177. __u8 min_ttl;
  178. __u8 mc_ttl;
  179. __u8 pmtudisc;
  180. __u8 recverr:1,
  181. is_icsk:1,
  182. freebind:1,
  183. hdrincl:1,
  184. mc_loop:1,
  185. transparent:1,
  186. mc_all:1,
  187. nodefrag:1;
  188. __u8 bind_address_no_port:1,
  189. defer_connect:1; /* Indicates that fastopen_connect is set
  190. * and cookie exists so we defer connect
  191. * until first data frame is written
  192. */
  193. __u8 rcv_tos;
  194. __u8 convert_csum;
  195. int uc_index;
  196. int mc_index;
  197. __be32 mc_addr;
  198. struct ip_mc_socklist __rcu *mc_list;
  199. struct inet_cork_full cork;
  200. };
  201. #define IPCORK_OPT 1 /* ip-options has been held in ipcork.opt */
  202. #define IPCORK_ALLFRAG 2 /* always fragment (for ipv6 for now) */
  203. /* cmsg flags for inet */
  204. #define IP_CMSG_PKTINFO BIT(0)
  205. #define IP_CMSG_TTL BIT(1)
  206. #define IP_CMSG_TOS BIT(2)
  207. #define IP_CMSG_RECVOPTS BIT(3)
  208. #define IP_CMSG_RETOPTS BIT(4)
  209. #define IP_CMSG_PASSSEC BIT(5)
  210. #define IP_CMSG_ORIGDSTADDR BIT(6)
  211. #define IP_CMSG_CHECKSUM BIT(7)
  212. #define IP_CMSG_RECVFRAGSIZE BIT(8)
  213. /**
  214. * sk_to_full_sk - Access to a full socket
  215. * @sk: pointer to a socket
  216. *
  217. * SYNACK messages might be attached to request sockets.
  218. * Some places want to reach the listener in this case.
  219. */
  220. static inline struct sock *sk_to_full_sk(struct sock *sk)
  221. {
  222. #ifdef CONFIG_INET
  223. if (sk && sk->sk_state == TCP_NEW_SYN_RECV)
  224. sk = inet_reqsk(sk)->rsk_listener;
  225. #endif
  226. return sk;
  227. }
  228. /* sk_to_full_sk() variant with a const argument */
  229. static inline const struct sock *sk_const_to_full_sk(const struct sock *sk)
  230. {
  231. #ifdef CONFIG_INET
  232. if (sk && sk->sk_state == TCP_NEW_SYN_RECV)
  233. sk = ((const struct request_sock *)sk)->rsk_listener;
  234. #endif
  235. return sk;
  236. }
  237. static inline struct sock *skb_to_full_sk(const struct sk_buff *skb)
  238. {
  239. return sk_to_full_sk(skb->sk);
  240. }
  241. static inline struct inet_sock *inet_sk(const struct sock *sk)
  242. {
  243. return (struct inet_sock *)sk;
  244. }
  245. static inline void __inet_sk_copy_descendant(struct sock *sk_to,
  246. const struct sock *sk_from,
  247. const int ancestor_size)
  248. {
  249. memcpy(inet_sk(sk_to) + 1, inet_sk(sk_from) + 1,
  250. sk_from->sk_prot->obj_size - ancestor_size);
  251. }
  252. #if !(IS_ENABLED(CONFIG_IPV6))
  253. static inline void inet_sk_copy_descendant(struct sock *sk_to,
  254. const struct sock *sk_from)
  255. {
  256. __inet_sk_copy_descendant(sk_to, sk_from, sizeof(struct inet_sock));
  257. }
  258. #endif
  259. int inet_sk_rebuild_header(struct sock *sk);
  260. static inline unsigned int __inet_ehashfn(const __be32 laddr,
  261. const __u16 lport,
  262. const __be32 faddr,
  263. const __be16 fport,
  264. u32 initval)
  265. {
  266. return jhash_3words((__force __u32) laddr,
  267. (__force __u32) faddr,
  268. ((__u32) lport) << 16 | (__force __u32)fport,
  269. initval);
  270. }
  271. struct request_sock *inet_reqsk_alloc(const struct request_sock_ops *ops,
  272. struct sock *sk_listener,
  273. bool attach_listener);
  274. static inline __u8 inet_sk_flowi_flags(const struct sock *sk)
  275. {
  276. __u8 flags = 0;
  277. if (inet_sk(sk)->transparent || inet_sk(sk)->hdrincl)
  278. flags |= FLOWI_FLAG_ANYSRC;
  279. return flags;
  280. }
  281. static inline void inet_inc_convert_csum(struct sock *sk)
  282. {
  283. inet_sk(sk)->convert_csum++;
  284. }
  285. static inline void inet_dec_convert_csum(struct sock *sk)
  286. {
  287. if (inet_sk(sk)->convert_csum > 0)
  288. inet_sk(sk)->convert_csum--;
  289. }
  290. static inline bool inet_get_convert_csum(struct sock *sk)
  291. {
  292. return !!inet_sk(sk)->convert_csum;
  293. }
  294. #endif /* _INET_SOCK_H */