inet_sock.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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_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. __be32 nexthop;
  41. unsigned char optlen;
  42. unsigned char srr;
  43. unsigned char rr;
  44. unsigned char ts;
  45. unsigned char is_strictroute:1,
  46. srr_is_hit:1,
  47. is_changed:1,
  48. rr_needaddr:1,
  49. ts_needtime:1,
  50. ts_needaddr:1;
  51. unsigned char router_alert;
  52. unsigned char cipso;
  53. unsigned char __pad2;
  54. unsigned char __data[0];
  55. };
  56. struct ip_options_rcu {
  57. struct rcu_head rcu;
  58. struct ip_options opt;
  59. };
  60. struct ip_options_data {
  61. struct ip_options_rcu opt;
  62. char data[40];
  63. };
  64. struct inet_request_sock {
  65. struct request_sock req;
  66. #define ir_loc_addr req.__req_common.skc_rcv_saddr
  67. #define ir_rmt_addr req.__req_common.skc_daddr
  68. #define ir_num req.__req_common.skc_num
  69. #define ir_rmt_port req.__req_common.skc_dport
  70. #define ir_v6_rmt_addr req.__req_common.skc_v6_daddr
  71. #define ir_v6_loc_addr req.__req_common.skc_v6_rcv_saddr
  72. #define ir_iif req.__req_common.skc_bound_dev_if
  73. kmemcheck_bitfield_begin(flags);
  74. u16 snd_wscale : 4,
  75. rcv_wscale : 4,
  76. tstamp_ok : 1,
  77. sack_ok : 1,
  78. wscale_ok : 1,
  79. ecn_ok : 1,
  80. acked : 1,
  81. no_srccheck: 1;
  82. kmemcheck_bitfield_end(flags);
  83. struct ip_options_rcu *opt;
  84. struct sk_buff *pktopts;
  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. int length; /* Total length of all frames */
  96. struct dst_entry *dst;
  97. u8 tx_flags;
  98. __u8 ttl;
  99. __s16 tos;
  100. char priority;
  101. };
  102. struct inet_cork_full {
  103. struct inet_cork base;
  104. struct flowi fl;
  105. };
  106. struct ip_mc_socklist;
  107. struct ipv6_pinfo;
  108. struct rtable;
  109. /** struct inet_sock - representation of INET sockets
  110. *
  111. * @sk - ancestor class
  112. * @pinet6 - pointer to IPv6 control block
  113. * @inet_daddr - Foreign IPv4 addr
  114. * @inet_rcv_saddr - Bound local IPv4 addr
  115. * @inet_dport - Destination port
  116. * @inet_num - Local port
  117. * @inet_saddr - Sending source
  118. * @uc_ttl - Unicast TTL
  119. * @inet_sport - Source port
  120. * @inet_id - ID counter for DF pkts
  121. * @tos - TOS
  122. * @mc_ttl - Multicasting TTL
  123. * @is_icsk - is this an inet_connection_sock?
  124. * @uc_index - Unicast outgoing device index
  125. * @mc_index - Multicast device index
  126. * @mc_list - Group array
  127. * @cork - info to build ip hdr on each ip frag while socket is corked
  128. */
  129. struct inet_sock {
  130. /* sk and pinet6 has to be the first two members of inet_sock */
  131. struct sock sk;
  132. #if IS_ENABLED(CONFIG_IPV6)
  133. struct ipv6_pinfo *pinet6;
  134. #endif
  135. /* Socket demultiplex comparisons on incoming packets. */
  136. #define inet_daddr sk.__sk_common.skc_daddr
  137. #define inet_rcv_saddr sk.__sk_common.skc_rcv_saddr
  138. #define inet_dport sk.__sk_common.skc_dport
  139. #define inet_num sk.__sk_common.skc_num
  140. __be32 inet_saddr;
  141. __s16 uc_ttl;
  142. __u16 cmsg_flags;
  143. __be16 inet_sport;
  144. __u16 inet_id;
  145. struct ip_options_rcu __rcu *inet_opt;
  146. int rx_dst_ifindex;
  147. __u8 tos;
  148. __u8 min_ttl;
  149. __u8 mc_ttl;
  150. __u8 pmtudisc;
  151. __u8 recverr:1,
  152. is_icsk:1,
  153. freebind:1,
  154. hdrincl:1,
  155. mc_loop:1,
  156. transparent:1,
  157. mc_all:1,
  158. nodefrag:1;
  159. __u8 rcv_tos;
  160. int uc_index;
  161. int mc_index;
  162. __be32 mc_addr;
  163. struct ip_mc_socklist __rcu *mc_list;
  164. struct inet_cork_full cork;
  165. };
  166. #define IPCORK_OPT 1 /* ip-options has been held in ipcork.opt */
  167. #define IPCORK_ALLFRAG 2 /* always fragment (for ipv6 for now) */
  168. static inline struct inet_sock *inet_sk(const struct sock *sk)
  169. {
  170. return (struct inet_sock *)sk;
  171. }
  172. static inline void __inet_sk_copy_descendant(struct sock *sk_to,
  173. const struct sock *sk_from,
  174. const int ancestor_size)
  175. {
  176. memcpy(inet_sk(sk_to) + 1, inet_sk(sk_from) + 1,
  177. sk_from->sk_prot->obj_size - ancestor_size);
  178. }
  179. #if !(IS_ENABLED(CONFIG_IPV6))
  180. static inline void inet_sk_copy_descendant(struct sock *sk_to,
  181. const struct sock *sk_from)
  182. {
  183. __inet_sk_copy_descendant(sk_to, sk_from, sizeof(struct inet_sock));
  184. }
  185. #endif
  186. int inet_sk_rebuild_header(struct sock *sk);
  187. static inline unsigned int __inet_ehashfn(const __be32 laddr,
  188. const __u16 lport,
  189. const __be32 faddr,
  190. const __be16 fport,
  191. u32 initval)
  192. {
  193. return jhash_3words((__force __u32) laddr,
  194. (__force __u32) faddr,
  195. ((__u32) lport) << 16 | (__force __u32)fport,
  196. initval);
  197. }
  198. static inline struct request_sock *inet_reqsk_alloc(struct request_sock_ops *ops)
  199. {
  200. struct request_sock *req = reqsk_alloc(ops);
  201. struct inet_request_sock *ireq = inet_rsk(req);
  202. if (req != NULL) {
  203. kmemcheck_annotate_bitfield(ireq, flags);
  204. ireq->opt = NULL;
  205. }
  206. return req;
  207. }
  208. static inline __u8 inet_sk_flowi_flags(const struct sock *sk)
  209. {
  210. __u8 flags = 0;
  211. if (inet_sk(sk)->transparent || inet_sk(sk)->hdrincl)
  212. flags |= FLOWI_FLAG_ANYSRC;
  213. return flags;
  214. }
  215. #endif /* _INET_SOCK_H */