skmsg.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
  3. #ifndef _LINUX_SKMSG_H
  4. #define _LINUX_SKMSG_H
  5. #include <linux/bpf.h>
  6. #include <linux/filter.h>
  7. #include <linux/scatterlist.h>
  8. #include <linux/skbuff.h>
  9. #include <net/sock.h>
  10. #include <net/tcp.h>
  11. #include <net/strparser.h>
  12. #define MAX_MSG_FRAGS MAX_SKB_FRAGS
  13. enum __sk_action {
  14. __SK_DROP = 0,
  15. __SK_PASS,
  16. __SK_REDIRECT,
  17. __SK_NONE,
  18. };
  19. struct sk_msg_sg {
  20. u32 start;
  21. u32 curr;
  22. u32 end;
  23. u32 size;
  24. u32 copybreak;
  25. bool copy[MAX_MSG_FRAGS];
  26. /* The extra element is used for chaining the front and sections when
  27. * the list becomes partitioned (e.g. end < start). The crypto APIs
  28. * require the chaining.
  29. */
  30. struct scatterlist data[MAX_MSG_FRAGS + 1];
  31. };
  32. struct sk_msg {
  33. struct sk_msg_sg sg;
  34. void *data;
  35. void *data_end;
  36. u32 apply_bytes;
  37. u32 cork_bytes;
  38. u32 flags;
  39. struct sk_buff *skb;
  40. struct sock *sk_redir;
  41. struct sock *sk;
  42. struct list_head list;
  43. };
  44. struct sk_psock_progs {
  45. struct bpf_prog *msg_parser;
  46. struct bpf_prog *skb_parser;
  47. struct bpf_prog *skb_verdict;
  48. };
  49. enum sk_psock_state_bits {
  50. SK_PSOCK_TX_ENABLED,
  51. };
  52. struct sk_psock_link {
  53. struct list_head list;
  54. struct bpf_map *map;
  55. void *link_raw;
  56. };
  57. struct sk_psock_parser {
  58. struct strparser strp;
  59. bool enabled;
  60. void (*saved_data_ready)(struct sock *sk);
  61. };
  62. struct sk_psock_work_state {
  63. struct sk_buff *skb;
  64. u32 len;
  65. u32 off;
  66. };
  67. struct sk_psock {
  68. struct sock *sk;
  69. struct sock *sk_redir;
  70. u32 apply_bytes;
  71. u32 cork_bytes;
  72. u32 eval;
  73. struct sk_msg *cork;
  74. struct sk_psock_progs progs;
  75. struct sk_psock_parser parser;
  76. struct sk_buff_head ingress_skb;
  77. struct list_head ingress_msg;
  78. unsigned long state;
  79. struct list_head link;
  80. spinlock_t link_lock;
  81. refcount_t refcnt;
  82. void (*saved_unhash)(struct sock *sk);
  83. void (*saved_close)(struct sock *sk, long timeout);
  84. void (*saved_write_space)(struct sock *sk);
  85. struct proto *sk_proto;
  86. struct sk_psock_work_state work_state;
  87. struct work_struct work;
  88. union {
  89. struct rcu_head rcu;
  90. struct work_struct gc;
  91. };
  92. };
  93. int sk_msg_alloc(struct sock *sk, struct sk_msg *msg, int len,
  94. int elem_first_coalesce);
  95. int sk_msg_clone(struct sock *sk, struct sk_msg *dst, struct sk_msg *src,
  96. u32 off, u32 len);
  97. void sk_msg_trim(struct sock *sk, struct sk_msg *msg, int len);
  98. int sk_msg_free(struct sock *sk, struct sk_msg *msg);
  99. int sk_msg_free_nocharge(struct sock *sk, struct sk_msg *msg);
  100. void sk_msg_free_partial(struct sock *sk, struct sk_msg *msg, u32 bytes);
  101. void sk_msg_free_partial_nocharge(struct sock *sk, struct sk_msg *msg,
  102. u32 bytes);
  103. void sk_msg_return(struct sock *sk, struct sk_msg *msg, int bytes);
  104. void sk_msg_return_zero(struct sock *sk, struct sk_msg *msg, int bytes);
  105. int sk_msg_zerocopy_from_iter(struct sock *sk, struct iov_iter *from,
  106. struct sk_msg *msg, u32 bytes);
  107. int sk_msg_memcopy_from_iter(struct sock *sk, struct iov_iter *from,
  108. struct sk_msg *msg, u32 bytes);
  109. static inline void sk_msg_check_to_free(struct sk_msg *msg, u32 i, u32 bytes)
  110. {
  111. WARN_ON(i == msg->sg.end && bytes);
  112. }
  113. static inline void sk_msg_apply_bytes(struct sk_psock *psock, u32 bytes)
  114. {
  115. if (psock->apply_bytes) {
  116. if (psock->apply_bytes < bytes)
  117. psock->apply_bytes = 0;
  118. else
  119. psock->apply_bytes -= bytes;
  120. }
  121. }
  122. #define sk_msg_iter_var_prev(var) \
  123. do { \
  124. if (var == 0) \
  125. var = MAX_MSG_FRAGS - 1; \
  126. else \
  127. var--; \
  128. } while (0)
  129. #define sk_msg_iter_var_next(var) \
  130. do { \
  131. var++; \
  132. if (var == MAX_MSG_FRAGS) \
  133. var = 0; \
  134. } while (0)
  135. #define sk_msg_iter_prev(msg, which) \
  136. sk_msg_iter_var_prev(msg->sg.which)
  137. #define sk_msg_iter_next(msg, which) \
  138. sk_msg_iter_var_next(msg->sg.which)
  139. static inline void sk_msg_clear_meta(struct sk_msg *msg)
  140. {
  141. memset(&msg->sg, 0, offsetofend(struct sk_msg_sg, copy));
  142. }
  143. static inline void sk_msg_init(struct sk_msg *msg)
  144. {
  145. BUILD_BUG_ON(ARRAY_SIZE(msg->sg.data) - 1 != MAX_MSG_FRAGS);
  146. memset(msg, 0, sizeof(*msg));
  147. sg_init_marker(msg->sg.data, MAX_MSG_FRAGS);
  148. }
  149. static inline void sk_msg_xfer(struct sk_msg *dst, struct sk_msg *src,
  150. int which, u32 size)
  151. {
  152. dst->sg.data[which] = src->sg.data[which];
  153. dst->sg.data[which].length = size;
  154. dst->sg.size += size;
  155. src->sg.data[which].length -= size;
  156. src->sg.data[which].offset += size;
  157. }
  158. static inline void sk_msg_xfer_full(struct sk_msg *dst, struct sk_msg *src)
  159. {
  160. memcpy(dst, src, sizeof(*src));
  161. sk_msg_init(src);
  162. }
  163. static inline bool sk_msg_full(const struct sk_msg *msg)
  164. {
  165. return (msg->sg.end == msg->sg.start) && msg->sg.size;
  166. }
  167. static inline u32 sk_msg_elem_used(const struct sk_msg *msg)
  168. {
  169. if (sk_msg_full(msg))
  170. return MAX_MSG_FRAGS;
  171. return msg->sg.end >= msg->sg.start ?
  172. msg->sg.end - msg->sg.start :
  173. msg->sg.end + (MAX_MSG_FRAGS - msg->sg.start);
  174. }
  175. static inline struct scatterlist *sk_msg_elem(struct sk_msg *msg, int which)
  176. {
  177. return &msg->sg.data[which];
  178. }
  179. static inline struct scatterlist sk_msg_elem_cpy(struct sk_msg *msg, int which)
  180. {
  181. return msg->sg.data[which];
  182. }
  183. static inline struct page *sk_msg_page(struct sk_msg *msg, int which)
  184. {
  185. return sg_page(sk_msg_elem(msg, which));
  186. }
  187. static inline bool sk_msg_to_ingress(const struct sk_msg *msg)
  188. {
  189. return msg->flags & BPF_F_INGRESS;
  190. }
  191. static inline void sk_msg_compute_data_pointers(struct sk_msg *msg)
  192. {
  193. struct scatterlist *sge = sk_msg_elem(msg, msg->sg.start);
  194. if (msg->sg.copy[msg->sg.start]) {
  195. msg->data = NULL;
  196. msg->data_end = NULL;
  197. } else {
  198. msg->data = sg_virt(sge);
  199. msg->data_end = msg->data + sge->length;
  200. }
  201. }
  202. static inline void sk_msg_page_add(struct sk_msg *msg, struct page *page,
  203. u32 len, u32 offset)
  204. {
  205. struct scatterlist *sge;
  206. get_page(page);
  207. sge = sk_msg_elem(msg, msg->sg.end);
  208. sg_set_page(sge, page, len, offset);
  209. sg_unmark_end(sge);
  210. msg->sg.copy[msg->sg.end] = true;
  211. msg->sg.size += len;
  212. sk_msg_iter_next(msg, end);
  213. }
  214. static inline void sk_msg_sg_copy(struct sk_msg *msg, u32 i, bool copy_state)
  215. {
  216. do {
  217. msg->sg.copy[i] = copy_state;
  218. sk_msg_iter_var_next(i);
  219. if (i == msg->sg.end)
  220. break;
  221. } while (1);
  222. }
  223. static inline void sk_msg_sg_copy_set(struct sk_msg *msg, u32 start)
  224. {
  225. sk_msg_sg_copy(msg, start, true);
  226. }
  227. static inline void sk_msg_sg_copy_clear(struct sk_msg *msg, u32 start)
  228. {
  229. sk_msg_sg_copy(msg, start, false);
  230. }
  231. static inline struct sk_psock *sk_psock(const struct sock *sk)
  232. {
  233. return rcu_dereference_sk_user_data(sk);
  234. }
  235. static inline void sk_psock_queue_msg(struct sk_psock *psock,
  236. struct sk_msg *msg)
  237. {
  238. list_add_tail(&msg->list, &psock->ingress_msg);
  239. }
  240. static inline bool sk_psock_queue_empty(const struct sk_psock *psock)
  241. {
  242. return psock ? list_empty(&psock->ingress_msg) : true;
  243. }
  244. static inline void sk_psock_report_error(struct sk_psock *psock, int err)
  245. {
  246. struct sock *sk = psock->sk;
  247. sk->sk_err = err;
  248. sk->sk_error_report(sk);
  249. }
  250. struct sk_psock *sk_psock_init(struct sock *sk, int node);
  251. int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock);
  252. void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock);
  253. void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock);
  254. int sk_psock_msg_verdict(struct sock *sk, struct sk_psock *psock,
  255. struct sk_msg *msg);
  256. static inline struct sk_psock_link *sk_psock_init_link(void)
  257. {
  258. return kzalloc(sizeof(struct sk_psock_link),
  259. GFP_ATOMIC | __GFP_NOWARN);
  260. }
  261. static inline void sk_psock_free_link(struct sk_psock_link *link)
  262. {
  263. kfree(link);
  264. }
  265. struct sk_psock_link *sk_psock_link_pop(struct sk_psock *psock);
  266. #if defined(CONFIG_BPF_STREAM_PARSER)
  267. void sk_psock_unlink(struct sock *sk, struct sk_psock_link *link);
  268. #else
  269. static inline void sk_psock_unlink(struct sock *sk,
  270. struct sk_psock_link *link)
  271. {
  272. }
  273. #endif
  274. void __sk_psock_purge_ingress_msg(struct sk_psock *psock);
  275. static inline void sk_psock_cork_free(struct sk_psock *psock)
  276. {
  277. if (psock->cork) {
  278. sk_msg_free(psock->sk, psock->cork);
  279. kfree(psock->cork);
  280. psock->cork = NULL;
  281. }
  282. }
  283. static inline void sk_psock_update_proto(struct sock *sk,
  284. struct sk_psock *psock,
  285. struct proto *ops)
  286. {
  287. psock->saved_unhash = sk->sk_prot->unhash;
  288. psock->saved_close = sk->sk_prot->close;
  289. psock->saved_write_space = sk->sk_write_space;
  290. psock->sk_proto = sk->sk_prot;
  291. sk->sk_prot = ops;
  292. }
  293. static inline void sk_psock_restore_proto(struct sock *sk,
  294. struct sk_psock *psock)
  295. {
  296. if (psock->sk_proto) {
  297. sk->sk_prot = psock->sk_proto;
  298. psock->sk_proto = NULL;
  299. }
  300. }
  301. static inline void sk_psock_set_state(struct sk_psock *psock,
  302. enum sk_psock_state_bits bit)
  303. {
  304. set_bit(bit, &psock->state);
  305. }
  306. static inline void sk_psock_clear_state(struct sk_psock *psock,
  307. enum sk_psock_state_bits bit)
  308. {
  309. clear_bit(bit, &psock->state);
  310. }
  311. static inline bool sk_psock_test_state(const struct sk_psock *psock,
  312. enum sk_psock_state_bits bit)
  313. {
  314. return test_bit(bit, &psock->state);
  315. }
  316. static inline struct sk_psock *sk_psock_get_checked(struct sock *sk)
  317. {
  318. struct sk_psock *psock;
  319. rcu_read_lock();
  320. psock = sk_psock(sk);
  321. if (psock) {
  322. if (sk->sk_prot->recvmsg != tcp_bpf_recvmsg) {
  323. psock = ERR_PTR(-EBUSY);
  324. goto out;
  325. }
  326. if (!refcount_inc_not_zero(&psock->refcnt))
  327. psock = ERR_PTR(-EBUSY);
  328. }
  329. out:
  330. rcu_read_unlock();
  331. return psock;
  332. }
  333. static inline struct sk_psock *sk_psock_get(struct sock *sk)
  334. {
  335. struct sk_psock *psock;
  336. rcu_read_lock();
  337. psock = sk_psock(sk);
  338. if (psock && !refcount_inc_not_zero(&psock->refcnt))
  339. psock = NULL;
  340. rcu_read_unlock();
  341. return psock;
  342. }
  343. void sk_psock_stop(struct sock *sk, struct sk_psock *psock);
  344. void sk_psock_destroy(struct rcu_head *rcu);
  345. void sk_psock_drop(struct sock *sk, struct sk_psock *psock);
  346. static inline void sk_psock_put(struct sock *sk, struct sk_psock *psock)
  347. {
  348. if (refcount_dec_and_test(&psock->refcnt))
  349. sk_psock_drop(sk, psock);
  350. }
  351. static inline void psock_set_prog(struct bpf_prog **pprog,
  352. struct bpf_prog *prog)
  353. {
  354. prog = xchg(pprog, prog);
  355. if (prog)
  356. bpf_prog_put(prog);
  357. }
  358. static inline void psock_progs_drop(struct sk_psock_progs *progs)
  359. {
  360. psock_set_prog(&progs->msg_parser, NULL);
  361. psock_set_prog(&progs->skb_parser, NULL);
  362. psock_set_prog(&progs->skb_verdict, NULL);
  363. }
  364. #endif /* _LINUX_SKMSG_H */