inet_sock.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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/kmemcheck.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. /** struct ip_options - IP Options
  28. *
  29. * @faddr - Saved first hop address
  30. * @is_data - Options in __data, rather than skb
  31. * @is_strictroute - Strict source route
  32. * @srr_is_hit - Packet destination addr was our one
  33. * @is_changed - IP checksum more not valid
  34. * @rr_needaddr - Need to record addr of outgoing dev
  35. * @ts_needtime - Need to record timestamp
  36. * @ts_needaddr - Need to record addr of outgoing dev
  37. */
  38. struct ip_options {
  39. __be32 faddr;
  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[0];
  54. };
  55. #define optlength(opt) (sizeof(struct ip_options) + opt->optlen)
  56. struct inet_request_sock {
  57. struct request_sock req;
  58. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  59. u16 inet6_rsk_offset;
  60. #endif
  61. __be16 loc_port;
  62. __be32 loc_addr;
  63. __be32 rmt_addr;
  64. __be16 rmt_port;
  65. kmemcheck_bitfield_begin(flags);
  66. u16 snd_wscale : 4,
  67. rcv_wscale : 4,
  68. tstamp_ok : 1,
  69. sack_ok : 1,
  70. wscale_ok : 1,
  71. ecn_ok : 1,
  72. acked : 1,
  73. no_srccheck: 1;
  74. kmemcheck_bitfield_end(flags);
  75. struct ip_options *opt;
  76. };
  77. static inline struct inet_request_sock *inet_rsk(const struct request_sock *sk)
  78. {
  79. return (struct inet_request_sock *)sk;
  80. }
  81. struct ip_mc_socklist;
  82. struct ipv6_pinfo;
  83. struct rtable;
  84. /** struct inet_sock - representation of INET sockets
  85. *
  86. * @sk - ancestor class
  87. * @pinet6 - pointer to IPv6 control block
  88. * @inet_daddr - Foreign IPv4 addr
  89. * @inet_rcv_saddr - Bound local IPv4 addr
  90. * @inet_dport - Destination port
  91. * @inet_num - Local port
  92. * @inet_saddr - Sending source
  93. * @uc_ttl - Unicast TTL
  94. * @inet_sport - Source port
  95. * @inet_id - ID counter for DF pkts
  96. * @rxhash - flow hash received from netif layer
  97. * @tos - TOS
  98. * @mc_ttl - Multicasting TTL
  99. * @is_icsk - is this an inet_connection_sock?
  100. * @mc_index - Multicast device index
  101. * @mc_list - Group array
  102. * @cork - info to build ip hdr on each ip frag while socket is corked
  103. */
  104. struct inet_sock {
  105. /* sk and pinet6 has to be the first two members of inet_sock */
  106. struct sock sk;
  107. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  108. struct ipv6_pinfo *pinet6;
  109. #endif
  110. /* Socket demultiplex comparisons on incoming packets. */
  111. __be32 inet_daddr;
  112. __be32 inet_rcv_saddr;
  113. __be16 inet_dport;
  114. __u16 inet_num;
  115. __be32 inet_saddr;
  116. __s16 uc_ttl;
  117. __u16 cmsg_flags;
  118. __be16 inet_sport;
  119. __u16 inet_id;
  120. #ifdef CONFIG_RPS
  121. __u32 rxhash;
  122. #endif
  123. struct ip_options *opt;
  124. __u8 tos;
  125. __u8 min_ttl;
  126. __u8 mc_ttl;
  127. __u8 pmtudisc;
  128. __u8 recverr:1,
  129. is_icsk:1,
  130. freebind:1,
  131. hdrincl:1,
  132. mc_loop:1,
  133. transparent:1,
  134. mc_all:1;
  135. int mc_index;
  136. __be32 mc_addr;
  137. struct ip_mc_socklist *mc_list;
  138. struct {
  139. unsigned int flags;
  140. unsigned int fragsize;
  141. struct ip_options *opt;
  142. struct dst_entry *dst;
  143. int length; /* Total length of all frames */
  144. __be32 addr;
  145. struct flowi fl;
  146. } cork;
  147. };
  148. #define IPCORK_OPT 1 /* ip-options has been held in ipcork.opt */
  149. #define IPCORK_ALLFRAG 2 /* always fragment (for ipv6 for now) */
  150. static inline struct inet_sock *inet_sk(const struct sock *sk)
  151. {
  152. return (struct inet_sock *)sk;
  153. }
  154. static inline void __inet_sk_copy_descendant(struct sock *sk_to,
  155. const struct sock *sk_from,
  156. const int ancestor_size)
  157. {
  158. memcpy(inet_sk(sk_to) + 1, inet_sk(sk_from) + 1,
  159. sk_from->sk_prot->obj_size - ancestor_size);
  160. }
  161. #if !(defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE))
  162. static inline void inet_sk_copy_descendant(struct sock *sk_to,
  163. const struct sock *sk_from)
  164. {
  165. __inet_sk_copy_descendant(sk_to, sk_from, sizeof(struct inet_sock));
  166. }
  167. #endif
  168. extern int inet_sk_rebuild_header(struct sock *sk);
  169. extern u32 inet_ehash_secret;
  170. extern void build_ehash_secret(void);
  171. static inline unsigned int inet_ehashfn(struct net *net,
  172. const __be32 laddr, const __u16 lport,
  173. const __be32 faddr, const __be16 fport)
  174. {
  175. return jhash_3words((__force __u32) laddr,
  176. (__force __u32) faddr,
  177. ((__u32) lport) << 16 | (__force __u32)fport,
  178. inet_ehash_secret + net_hash_mix(net));
  179. }
  180. static inline int inet_sk_ehashfn(const struct sock *sk)
  181. {
  182. const struct inet_sock *inet = inet_sk(sk);
  183. const __be32 laddr = inet->inet_rcv_saddr;
  184. const __u16 lport = inet->inet_num;
  185. const __be32 faddr = inet->inet_daddr;
  186. const __be16 fport = inet->inet_dport;
  187. struct net *net = sock_net(sk);
  188. return inet_ehashfn(net, laddr, lport, faddr, fport);
  189. }
  190. static inline struct request_sock *inet_reqsk_alloc(struct request_sock_ops *ops)
  191. {
  192. struct request_sock *req = reqsk_alloc(ops);
  193. struct inet_request_sock *ireq = inet_rsk(req);
  194. if (req != NULL) {
  195. kmemcheck_annotate_bitfield(ireq, flags);
  196. ireq->opt = NULL;
  197. }
  198. return req;
  199. }
  200. static inline __u8 inet_sk_flowi_flags(const struct sock *sk)
  201. {
  202. return inet_sk(sk)->transparent ? FLOWI_FLAG_ANYSRC : 0;
  203. }
  204. static inline void inet_rps_record_flow(const struct sock *sk)
  205. {
  206. #ifdef CONFIG_RPS
  207. struct rps_sock_flow_table *sock_flow_table;
  208. rcu_read_lock();
  209. sock_flow_table = rcu_dereference(rps_sock_flow_table);
  210. rps_record_sock_flow(sock_flow_table, inet_sk(sk)->rxhash);
  211. rcu_read_unlock();
  212. #endif
  213. }
  214. static inline void inet_rps_reset_flow(const struct sock *sk)
  215. {
  216. #ifdef CONFIG_RPS
  217. struct rps_sock_flow_table *sock_flow_table;
  218. rcu_read_lock();
  219. sock_flow_table = rcu_dereference(rps_sock_flow_table);
  220. rps_reset_sock_flow(sock_flow_table, inet_sk(sk)->rxhash);
  221. rcu_read_unlock();
  222. #endif
  223. }
  224. static inline void inet_rps_save_rxhash(struct sock *sk, u32 rxhash)
  225. {
  226. #ifdef CONFIG_RPS
  227. if (unlikely(inet_sk(sk)->rxhash != rxhash)) {
  228. inet_rps_reset_flow(sk);
  229. inet_sk(sk)->rxhash = rxhash;
  230. }
  231. #endif
  232. }
  233. #endif /* _INET_SOCK_H */