inet_sock.h 8.1 KB

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