inet_sock.h 6.4 KB

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