inet_sock.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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/string.h>
  19. #include <linux/types.h>
  20. #include <linux/jhash.h>
  21. #include <net/flow.h>
  22. #include <net/sock.h>
  23. #include <net/request_sock.h>
  24. #include <net/netns/hash.h>
  25. /** struct ip_options - IP Options
  26. *
  27. * @faddr - Saved first hop address
  28. * @is_data - Options in __data, rather than skb
  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. unsigned char optlen;
  39. unsigned char srr;
  40. unsigned char rr;
  41. unsigned char ts;
  42. unsigned char is_strictroute:1,
  43. srr_is_hit:1,
  44. is_changed:1,
  45. rr_needaddr:1,
  46. ts_needtime:1,
  47. ts_needaddr:1;
  48. unsigned char router_alert;
  49. unsigned char cipso;
  50. unsigned char __pad2;
  51. unsigned char __data[0];
  52. };
  53. #define optlength(opt) (sizeof(struct ip_options) + opt->optlen)
  54. struct inet_request_sock {
  55. struct request_sock req;
  56. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  57. u16 inet6_rsk_offset;
  58. #endif
  59. __be16 loc_port;
  60. __be32 loc_addr;
  61. __be32 rmt_addr;
  62. __be16 rmt_port;
  63. u16 snd_wscale : 4,
  64. rcv_wscale : 4,
  65. tstamp_ok : 1,
  66. sack_ok : 1,
  67. wscale_ok : 1,
  68. ecn_ok : 1,
  69. acked : 1,
  70. no_srccheck: 1;
  71. struct ip_options *opt;
  72. };
  73. static inline struct inet_request_sock *inet_rsk(const struct request_sock *sk)
  74. {
  75. return (struct inet_request_sock *)sk;
  76. }
  77. struct ip_mc_socklist;
  78. struct ipv6_pinfo;
  79. struct rtable;
  80. /** struct inet_sock - representation of INET sockets
  81. *
  82. * @sk - ancestor class
  83. * @pinet6 - pointer to IPv6 control block
  84. * @daddr - Foreign IPv4 addr
  85. * @rcv_saddr - Bound local IPv4 addr
  86. * @dport - Destination port
  87. * @num - Local port
  88. * @saddr - Sending source
  89. * @uc_ttl - Unicast TTL
  90. * @sport - Source port
  91. * @id - ID counter for DF pkts
  92. * @tos - TOS
  93. * @mc_ttl - Multicasting TTL
  94. * @is_icsk - is this an inet_connection_sock?
  95. * @mc_index - Multicast device index
  96. * @mc_list - Group array
  97. * @cork - info to build ip hdr on each ip frag while socket is corked
  98. */
  99. struct inet_sock {
  100. /* sk and pinet6 has to be the first two members of inet_sock */
  101. struct sock sk;
  102. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  103. struct ipv6_pinfo *pinet6;
  104. #endif
  105. /* Socket demultiplex comparisons on incoming packets. */
  106. __be32 daddr;
  107. __be32 rcv_saddr;
  108. __be16 dport;
  109. __u16 num;
  110. __be32 saddr;
  111. __s16 uc_ttl;
  112. __u16 cmsg_flags;
  113. struct ip_options *opt;
  114. __be16 sport;
  115. __u16 id;
  116. __u8 tos;
  117. __u8 mc_ttl;
  118. __u8 pmtudisc;
  119. __u8 recverr:1,
  120. is_icsk:1,
  121. freebind:1,
  122. hdrincl:1,
  123. mc_loop:1,
  124. transparent:1;
  125. int mc_index;
  126. __be32 mc_addr;
  127. struct ip_mc_socklist *mc_list;
  128. struct {
  129. unsigned int flags;
  130. unsigned int fragsize;
  131. struct ip_options *opt;
  132. struct dst_entry *dst;
  133. int length; /* Total length of all frames */
  134. __be32 addr;
  135. struct flowi fl;
  136. } cork;
  137. };
  138. #define IPCORK_OPT 1 /* ip-options has been held in ipcork.opt */
  139. #define IPCORK_ALLFRAG 2 /* always fragment (for ipv6 for now) */
  140. static inline struct inet_sock *inet_sk(const struct sock *sk)
  141. {
  142. return (struct inet_sock *)sk;
  143. }
  144. static inline void __inet_sk_copy_descendant(struct sock *sk_to,
  145. const struct sock *sk_from,
  146. const int ancestor_size)
  147. {
  148. memcpy(inet_sk(sk_to) + 1, inet_sk(sk_from) + 1,
  149. sk_from->sk_prot->obj_size - ancestor_size);
  150. }
  151. #if !(defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE))
  152. static inline void inet_sk_copy_descendant(struct sock *sk_to,
  153. const struct sock *sk_from)
  154. {
  155. __inet_sk_copy_descendant(sk_to, sk_from, sizeof(struct inet_sock));
  156. }
  157. #endif
  158. extern int inet_sk_rebuild_header(struct sock *sk);
  159. extern u32 inet_ehash_secret;
  160. extern void build_ehash_secret(void);
  161. static inline unsigned int inet_ehashfn(struct net *net,
  162. const __be32 laddr, const __u16 lport,
  163. const __be32 faddr, const __be16 fport)
  164. {
  165. return jhash_3words((__force __u32) laddr,
  166. (__force __u32) faddr,
  167. ((__u32) lport) << 16 | (__force __u32)fport,
  168. inet_ehash_secret + net_hash_mix(net));
  169. }
  170. static inline int inet_sk_ehashfn(const struct sock *sk)
  171. {
  172. const struct inet_sock *inet = inet_sk(sk);
  173. const __be32 laddr = inet->rcv_saddr;
  174. const __u16 lport = inet->num;
  175. const __be32 faddr = inet->daddr;
  176. const __be16 fport = inet->dport;
  177. struct net *net = sock_net(sk);
  178. return inet_ehashfn(net, laddr, lport, faddr, fport);
  179. }
  180. static inline struct request_sock *inet_reqsk_alloc(struct request_sock_ops *ops)
  181. {
  182. struct request_sock *req = reqsk_alloc(ops);
  183. if (req != NULL)
  184. inet_rsk(req)->opt = NULL;
  185. return req;
  186. }
  187. static inline __u8 inet_sk_flowi_flags(const struct sock *sk)
  188. {
  189. return inet_sk(sk)->transparent ? FLOWI_FLAG_ANYSRC : 0;
  190. }
  191. #endif /* _INET_SOCK_H */