inet_sock.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. 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. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  66. u16 inet6_rsk_offset;
  67. #endif
  68. __be16 loc_port;
  69. __be32 loc_addr;
  70. __be32 rmt_addr;
  71. __be16 rmt_port;
  72. kmemcheck_bitfield_begin(flags);
  73. u16 snd_wscale : 4,
  74. rcv_wscale : 4,
  75. tstamp_ok : 1,
  76. sack_ok : 1,
  77. wscale_ok : 1,
  78. ecn_ok : 1,
  79. acked : 1,
  80. no_srccheck: 1;
  81. kmemcheck_bitfield_end(flags);
  82. struct ip_options_rcu *opt;
  83. };
  84. static inline struct inet_request_sock *inet_rsk(const struct request_sock *sk)
  85. {
  86. return (struct inet_request_sock *)sk;
  87. }
  88. struct inet_cork {
  89. unsigned int flags;
  90. __be32 addr;
  91. struct ip_options *opt;
  92. unsigned int fragsize;
  93. struct dst_entry *dst;
  94. int length; /* Total length of all frames */
  95. struct page *page;
  96. u32 off;
  97. u8 tx_flags;
  98. };
  99. struct inet_cork_full {
  100. struct inet_cork base;
  101. struct flowi fl;
  102. };
  103. struct ip_mc_socklist;
  104. struct ipv6_pinfo;
  105. struct rtable;
  106. /** struct inet_sock - representation of INET sockets
  107. *
  108. * @sk - ancestor class
  109. * @pinet6 - pointer to IPv6 control block
  110. * @inet_daddr - Foreign IPv4 addr
  111. * @inet_rcv_saddr - Bound local IPv4 addr
  112. * @inet_dport - Destination port
  113. * @inet_num - Local port
  114. * @inet_saddr - Sending source
  115. * @uc_ttl - Unicast TTL
  116. * @inet_sport - Source port
  117. * @inet_id - ID counter for DF pkts
  118. * @tos - TOS
  119. * @mc_ttl - Multicasting TTL
  120. * @is_icsk - is this an inet_connection_sock?
  121. * @mc_index - Multicast device index
  122. * @mc_list - Group array
  123. * @cork - info to build ip hdr on each ip frag while socket is corked
  124. */
  125. struct inet_sock {
  126. /* sk and pinet6 has to be the first two members of inet_sock */
  127. struct sock sk;
  128. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  129. struct ipv6_pinfo *pinet6;
  130. #endif
  131. /* Socket demultiplex comparisons on incoming packets. */
  132. #define inet_daddr sk.__sk_common.skc_daddr
  133. #define inet_rcv_saddr sk.__sk_common.skc_rcv_saddr
  134. __be16 inet_dport;
  135. __u16 inet_num;
  136. __be32 inet_saddr;
  137. __s16 uc_ttl;
  138. __u16 cmsg_flags;
  139. __be16 inet_sport;
  140. __u16 inet_id;
  141. struct ip_options_rcu __rcu *inet_opt;
  142. __u8 tos;
  143. __u8 min_ttl;
  144. __u8 mc_ttl;
  145. __u8 pmtudisc;
  146. __u8 recverr:1,
  147. is_icsk:1,
  148. freebind:1,
  149. hdrincl:1,
  150. mc_loop:1,
  151. transparent:1,
  152. mc_all:1,
  153. nodefrag:1;
  154. int mc_index;
  155. __be32 mc_addr;
  156. struct ip_mc_socklist __rcu *mc_list;
  157. struct inet_cork_full cork;
  158. };
  159. #define IPCORK_OPT 1 /* ip-options has been held in ipcork.opt */
  160. #define IPCORK_ALLFRAG 2 /* always fragment (for ipv6 for now) */
  161. static inline struct inet_sock *inet_sk(const struct sock *sk)
  162. {
  163. return (struct inet_sock *)sk;
  164. }
  165. static inline void __inet_sk_copy_descendant(struct sock *sk_to,
  166. const struct sock *sk_from,
  167. const int ancestor_size)
  168. {
  169. memcpy(inet_sk(sk_to) + 1, inet_sk(sk_from) + 1,
  170. sk_from->sk_prot->obj_size - ancestor_size);
  171. }
  172. #if !(defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE))
  173. static inline void inet_sk_copy_descendant(struct sock *sk_to,
  174. const struct sock *sk_from)
  175. {
  176. __inet_sk_copy_descendant(sk_to, sk_from, sizeof(struct inet_sock));
  177. }
  178. #endif
  179. extern int inet_sk_rebuild_header(struct sock *sk);
  180. extern u32 inet_ehash_secret;
  181. extern void build_ehash_secret(void);
  182. static inline unsigned int inet_ehashfn(struct net *net,
  183. const __be32 laddr, const __u16 lport,
  184. const __be32 faddr, const __be16 fport)
  185. {
  186. return jhash_3words((__force __u32) laddr,
  187. (__force __u32) faddr,
  188. ((__u32) lport) << 16 | (__force __u32)fport,
  189. inet_ehash_secret + net_hash_mix(net));
  190. }
  191. static inline int inet_sk_ehashfn(const struct sock *sk)
  192. {
  193. const struct inet_sock *inet = inet_sk(sk);
  194. const __be32 laddr = inet->inet_rcv_saddr;
  195. const __u16 lport = inet->inet_num;
  196. const __be32 faddr = inet->inet_daddr;
  197. const __be16 fport = inet->inet_dport;
  198. struct net *net = sock_net(sk);
  199. return inet_ehashfn(net, laddr, lport, faddr, fport);
  200. }
  201. static inline struct request_sock *inet_reqsk_alloc(struct request_sock_ops *ops)
  202. {
  203. struct request_sock *req = reqsk_alloc(ops);
  204. struct inet_request_sock *ireq = inet_rsk(req);
  205. if (req != NULL) {
  206. kmemcheck_annotate_bitfield(ireq, flags);
  207. ireq->opt = NULL;
  208. }
  209. return req;
  210. }
  211. static inline __u8 inet_sk_flowi_flags(const struct sock *sk)
  212. {
  213. __u8 flags = 0;
  214. if (inet_sk(sk)->transparent)
  215. flags |= FLOWI_FLAG_ANYSRC;
  216. if (sk->sk_protocol == IPPROTO_TCP)
  217. flags |= FLOWI_FLAG_PRECOW_METRICS;
  218. return flags;
  219. }
  220. #endif /* _INET_SOCK_H */