inet_sock.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. /** struct ip_options - IP Options
  29. *
  30. * @faddr - Saved first hop address
  31. * @nexthop - Saved nexthop address in LSRR and SSRR
  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. #define ir_loc_addr req.__req_common.skc_rcv_saddr
  68. #define ir_rmt_addr req.__req_common.skc_daddr
  69. #define ir_num req.__req_common.skc_num
  70. #define ir_rmt_port req.__req_common.skc_dport
  71. #define ir_v6_rmt_addr req.__req_common.skc_v6_daddr
  72. #define ir_v6_loc_addr req.__req_common.skc_v6_rcv_saddr
  73. #define ir_iif req.__req_common.skc_bound_dev_if
  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. union {
  85. struct ip_options_rcu *opt;
  86. struct sk_buff *pktopts;
  87. };
  88. u32 ir_mark;
  89. };
  90. static inline struct inet_request_sock *inet_rsk(const struct request_sock *sk)
  91. {
  92. return (struct inet_request_sock *)sk;
  93. }
  94. static inline u32 inet_request_mark(struct sock *sk, struct sk_buff *skb)
  95. {
  96. if (!sk->sk_mark && sock_net(sk)->ipv4.sysctl_tcp_fwmark_accept) {
  97. return skb->mark;
  98. } else {
  99. return sk->sk_mark;
  100. }
  101. }
  102. struct inet_cork {
  103. unsigned int flags;
  104. __be32 addr;
  105. struct ip_options *opt;
  106. unsigned int fragsize;
  107. int length; /* Total length of all frames */
  108. struct dst_entry *dst;
  109. u8 tx_flags;
  110. __u8 ttl;
  111. __s16 tos;
  112. char priority;
  113. };
  114. struct inet_cork_full {
  115. struct inet_cork base;
  116. struct flowi fl;
  117. };
  118. struct ip_mc_socklist;
  119. struct ipv6_pinfo;
  120. struct rtable;
  121. /** struct inet_sock - representation of INET sockets
  122. *
  123. * @sk - ancestor class
  124. * @pinet6 - pointer to IPv6 control block
  125. * @inet_daddr - Foreign IPv4 addr
  126. * @inet_rcv_saddr - Bound local IPv4 addr
  127. * @inet_dport - Destination port
  128. * @inet_num - Local port
  129. * @inet_saddr - Sending source
  130. * @uc_ttl - Unicast TTL
  131. * @inet_sport - Source port
  132. * @inet_id - ID counter for DF pkts
  133. * @tos - TOS
  134. * @mc_ttl - Multicasting TTL
  135. * @is_icsk - is this an inet_connection_sock?
  136. * @uc_index - Unicast outgoing device index
  137. * @mc_index - Multicast device index
  138. * @mc_list - Group array
  139. * @cork - info to build ip hdr on each ip frag while socket is corked
  140. */
  141. struct inet_sock {
  142. /* sk and pinet6 has to be the first two members of inet_sock */
  143. struct sock sk;
  144. #if IS_ENABLED(CONFIG_IPV6)
  145. struct ipv6_pinfo *pinet6;
  146. #endif
  147. /* Socket demultiplex comparisons on incoming packets. */
  148. #define inet_daddr sk.__sk_common.skc_daddr
  149. #define inet_rcv_saddr sk.__sk_common.skc_rcv_saddr
  150. #define inet_dport sk.__sk_common.skc_dport
  151. #define inet_num sk.__sk_common.skc_num
  152. __be32 inet_saddr;
  153. __s16 uc_ttl;
  154. __u16 cmsg_flags;
  155. __be16 inet_sport;
  156. __u16 inet_id;
  157. struct ip_options_rcu __rcu *inet_opt;
  158. int rx_dst_ifindex;
  159. __u8 tos;
  160. __u8 min_ttl;
  161. __u8 mc_ttl;
  162. __u8 pmtudisc;
  163. __u8 recverr:1,
  164. is_icsk:1,
  165. freebind:1,
  166. hdrincl:1,
  167. mc_loop:1,
  168. transparent:1,
  169. mc_all:1,
  170. nodefrag:1;
  171. __u8 rcv_tos;
  172. __u8 convert_csum;
  173. int uc_index;
  174. int mc_index;
  175. __be32 mc_addr;
  176. struct ip_mc_socklist __rcu *mc_list;
  177. struct inet_cork_full cork;
  178. };
  179. #define IPCORK_OPT 1 /* ip-options has been held in ipcork.opt */
  180. #define IPCORK_ALLFRAG 2 /* always fragment (for ipv6 for now) */
  181. /* cmsg flags for inet */
  182. #define IP_CMSG_PKTINFO BIT(0)
  183. #define IP_CMSG_TTL BIT(1)
  184. #define IP_CMSG_TOS BIT(2)
  185. #define IP_CMSG_RECVOPTS BIT(3)
  186. #define IP_CMSG_RETOPTS BIT(4)
  187. #define IP_CMSG_PASSSEC BIT(5)
  188. #define IP_CMSG_ORIGDSTADDR BIT(6)
  189. #define IP_CMSG_CHECKSUM BIT(7)
  190. static inline struct inet_sock *inet_sk(const struct sock *sk)
  191. {
  192. return (struct inet_sock *)sk;
  193. }
  194. static inline void __inet_sk_copy_descendant(struct sock *sk_to,
  195. const struct sock *sk_from,
  196. const int ancestor_size)
  197. {
  198. memcpy(inet_sk(sk_to) + 1, inet_sk(sk_from) + 1,
  199. sk_from->sk_prot->obj_size - ancestor_size);
  200. }
  201. #if !(IS_ENABLED(CONFIG_IPV6))
  202. static inline void inet_sk_copy_descendant(struct sock *sk_to,
  203. const struct sock *sk_from)
  204. {
  205. __inet_sk_copy_descendant(sk_to, sk_from, sizeof(struct inet_sock));
  206. }
  207. #endif
  208. int inet_sk_rebuild_header(struct sock *sk);
  209. static inline unsigned int __inet_ehashfn(const __be32 laddr,
  210. const __u16 lport,
  211. const __be32 faddr,
  212. const __be16 fport,
  213. u32 initval)
  214. {
  215. return jhash_3words((__force __u32) laddr,
  216. (__force __u32) faddr,
  217. ((__u32) lport) << 16 | (__force __u32)fport,
  218. initval);
  219. }
  220. static inline struct request_sock *inet_reqsk_alloc(struct request_sock_ops *ops)
  221. {
  222. struct request_sock *req = reqsk_alloc(ops);
  223. struct inet_request_sock *ireq = inet_rsk(req);
  224. if (req != NULL) {
  225. kmemcheck_annotate_bitfield(ireq, flags);
  226. ireq->opt = NULL;
  227. }
  228. return req;
  229. }
  230. static inline __u8 inet_sk_flowi_flags(const struct sock *sk)
  231. {
  232. __u8 flags = 0;
  233. if (inet_sk(sk)->transparent || inet_sk(sk)->hdrincl)
  234. flags |= FLOWI_FLAG_ANYSRC;
  235. return flags;
  236. }
  237. static inline void inet_inc_convert_csum(struct sock *sk)
  238. {
  239. inet_sk(sk)->convert_csum++;
  240. }
  241. static inline void inet_dec_convert_csum(struct sock *sk)
  242. {
  243. if (inet_sk(sk)->convert_csum > 0)
  244. inet_sk(sk)->convert_csum--;
  245. }
  246. static inline bool inet_get_convert_csum(struct sock *sk)
  247. {
  248. return !!inet_sk(sk)->convert_csum;
  249. }
  250. #endif /* _INET_SOCK_H */