1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2017 - 2018 Covalent IO, Inc. http://covalent.io */
8 #include <linux/filter.h>
9 #include <linux/scatterlist.h>
10 #include <linux/skbuff.h>
14 #include <net/strparser.h>
16 #define MAX_MSG_FRAGS MAX_SKB_FRAGS
31 bool copy[MAX_MSG_FRAGS];
32 /* The extra element is used for chaining the front and sections when
33 * the list becomes partitioned (e.g. end < start). The crypto APIs
34 * require the chaining.
36 struct scatterlist data[MAX_MSG_FRAGS + 1];
47 struct sock *sk_redir;
49 struct list_head list;
52 struct sk_psock_progs {
53 struct bpf_prog *msg_parser;
54 struct bpf_prog *skb_parser;
55 struct bpf_prog *skb_verdict;
58 enum sk_psock_state_bits {
62 struct sk_psock_link {
63 struct list_head list;
68 struct sk_psock_parser {
69 struct strparser strp;
71 void (*saved_data_ready)(struct sock *sk);
74 struct sk_psock_work_state {
82 struct sock *sk_redir;
87 struct sk_psock_progs progs;
88 struct sk_psock_parser parser;
89 struct sk_buff_head ingress_skb;
90 struct list_head ingress_msg;
92 struct list_head link;
95 void (*saved_unhash)(struct sock *sk);
96 void (*saved_close)(struct sock *sk, long timeout);
97 void (*saved_write_space)(struct sock *sk);
98 struct proto *sk_proto;
99 struct sk_psock_work_state work_state;
100 struct work_struct work;
103 struct work_struct gc;
107 int sk_msg_alloc(struct sock *sk, struct sk_msg *msg, int len,
108 int elem_first_coalesce);
109 int sk_msg_clone(struct sock *sk, struct sk_msg *dst, struct sk_msg *src,
111 void sk_msg_trim(struct sock *sk, struct sk_msg *msg, int len);
112 int sk_msg_free(struct sock *sk, struct sk_msg *msg);
113 int sk_msg_free_nocharge(struct sock *sk, struct sk_msg *msg);
114 void sk_msg_free_partial(struct sock *sk, struct sk_msg *msg, u32 bytes);
115 void sk_msg_free_partial_nocharge(struct sock *sk, struct sk_msg *msg,
118 void sk_msg_return(struct sock *sk, struct sk_msg *msg, int bytes);
119 void sk_msg_return_zero(struct sock *sk, struct sk_msg *msg, int bytes);
121 int sk_msg_zerocopy_from_iter(struct sock *sk, struct iov_iter *from,
122 struct sk_msg *msg, u32 bytes);
123 int sk_msg_memcopy_from_iter(struct sock *sk, struct iov_iter *from,
124 struct sk_msg *msg, u32 bytes);
126 static inline void sk_msg_check_to_free(struct sk_msg *msg, u32 i, u32 bytes)
128 WARN_ON(i == msg->sg.end && bytes);
131 static inline void sk_msg_apply_bytes(struct sk_psock *psock, u32 bytes)
133 if (psock->apply_bytes) {
134 if (psock->apply_bytes < bytes)
135 psock->apply_bytes = 0;
137 psock->apply_bytes -= bytes;
141 #define sk_msg_iter_var_prev(var) \
144 var = MAX_MSG_FRAGS - 1; \
149 #define sk_msg_iter_var_next(var) \
152 if (var == MAX_MSG_FRAGS) \
156 #define sk_msg_iter_prev(msg, which) \
157 sk_msg_iter_var_prev(msg->sg.which)
159 #define sk_msg_iter_next(msg, which) \
160 sk_msg_iter_var_next(msg->sg.which)
162 static inline void sk_msg_clear_meta(struct sk_msg *msg)
164 memset(&msg->sg, 0, offsetofend(struct sk_msg_sg, copy));
167 static inline void sk_msg_init(struct sk_msg *msg)
169 BUILD_BUG_ON(ARRAY_SIZE(msg->sg.data) - 1 != MAX_MSG_FRAGS);
170 memset(msg, 0, sizeof(*msg));
171 sg_init_marker(msg->sg.data, MAX_MSG_FRAGS);
174 static inline void sk_msg_xfer(struct sk_msg *dst, struct sk_msg *src,
177 dst->sg.data[which] = src->sg.data[which];
178 dst->sg.data[which].length = size;
179 dst->sg.size += size;
180 src->sg.data[which].length -= size;
181 src->sg.data[which].offset += size;
184 static inline void sk_msg_xfer_full(struct sk_msg *dst, struct sk_msg *src)
186 memcpy(dst, src, sizeof(*src));
190 static inline bool sk_msg_full(const struct sk_msg *msg)
192 return (msg->sg.end == msg->sg.start) && msg->sg.size;
195 static inline u32 sk_msg_elem_used(const struct sk_msg *msg)
197 if (sk_msg_full(msg))
198 return MAX_MSG_FRAGS;
200 return msg->sg.end >= msg->sg.start ?
201 msg->sg.end - msg->sg.start :
202 msg->sg.end + (MAX_MSG_FRAGS - msg->sg.start);
205 static inline struct scatterlist *sk_msg_elem(struct sk_msg *msg, int which)
207 return &msg->sg.data[which];
210 static inline struct scatterlist sk_msg_elem_cpy(struct sk_msg *msg, int which)
212 return msg->sg.data[which];
215 static inline struct page *sk_msg_page(struct sk_msg *msg, int which)
217 return sg_page(sk_msg_elem(msg, which));
220 static inline bool sk_msg_to_ingress(const struct sk_msg *msg)
222 return msg->flags & BPF_F_INGRESS;
225 static inline void sk_msg_compute_data_pointers(struct sk_msg *msg)
227 struct scatterlist *sge = sk_msg_elem(msg, msg->sg.start);
229 if (msg->sg.copy[msg->sg.start]) {
231 msg->data_end = NULL;
233 msg->data = sg_virt(sge);
234 msg->data_end = msg->data + sge->length;
238 static inline void sk_msg_page_add(struct sk_msg *msg, struct page *page,
241 struct scatterlist *sge;
244 sge = sk_msg_elem(msg, msg->sg.end);
245 sg_set_page(sge, page, len, offset);
248 msg->sg.copy[msg->sg.end] = true;
250 sk_msg_iter_next(msg, end);
253 static inline void sk_msg_sg_copy(struct sk_msg *msg, u32 i, bool copy_state)
256 msg->sg.copy[i] = copy_state;
257 sk_msg_iter_var_next(i);
258 if (i == msg->sg.end)
263 static inline void sk_msg_sg_copy_set(struct sk_msg *msg, u32 start)
265 sk_msg_sg_copy(msg, start, true);
268 static inline void sk_msg_sg_copy_clear(struct sk_msg *msg, u32 start)
270 sk_msg_sg_copy(msg, start, false);
273 static inline struct sk_psock *sk_psock(const struct sock *sk)
275 return rcu_dereference_sk_user_data(sk);
278 static inline void sk_psock_queue_msg(struct sk_psock *psock,
281 list_add_tail(&msg->list, &psock->ingress_msg);
284 static inline bool sk_psock_queue_empty(const struct sk_psock *psock)
286 return psock ? list_empty(&psock->ingress_msg) : true;
289 static inline void sk_psock_report_error(struct sk_psock *psock, int err)
291 struct sock *sk = psock->sk;
294 sk->sk_error_report(sk);
297 struct sk_psock *sk_psock_init(struct sock *sk, int node);
299 int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock);
300 void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock);
301 void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock);
303 int sk_psock_msg_verdict(struct sock *sk, struct sk_psock *psock,
306 static inline struct sk_psock_link *sk_psock_init_link(void)
308 return kzalloc(sizeof(struct sk_psock_link),
309 GFP_ATOMIC | __GFP_NOWARN);
312 static inline void sk_psock_free_link(struct sk_psock_link *link)
317 struct sk_psock_link *sk_psock_link_pop(struct sk_psock *psock);
318 #if defined(CONFIG_BPF_STREAM_PARSER)
319 void sk_psock_unlink(struct sock *sk, struct sk_psock_link *link);
321 static inline void sk_psock_unlink(struct sock *sk,
322 struct sk_psock_link *link)
327 void __sk_psock_purge_ingress_msg(struct sk_psock *psock);
329 static inline void sk_psock_cork_free(struct sk_psock *psock)
332 sk_msg_free(psock->sk, psock->cork);
338 static inline void sk_psock_update_proto(struct sock *sk,
339 struct sk_psock *psock,
342 psock->saved_unhash = sk->sk_prot->unhash;
343 psock->saved_close = sk->sk_prot->close;
344 psock->saved_write_space = sk->sk_write_space;
346 psock->sk_proto = sk->sk_prot;
350 static inline void sk_psock_restore_proto(struct sock *sk,
351 struct sk_psock *psock)
353 if (psock->sk_proto) {
354 sk->sk_prot = psock->sk_proto;
355 psock->sk_proto = NULL;
359 static inline void sk_psock_set_state(struct sk_psock *psock,
360 enum sk_psock_state_bits bit)
362 set_bit(bit, &psock->state);
365 static inline void sk_psock_clear_state(struct sk_psock *psock,
366 enum sk_psock_state_bits bit)
368 clear_bit(bit, &psock->state);
371 static inline bool sk_psock_test_state(const struct sk_psock *psock,
372 enum sk_psock_state_bits bit)
374 return test_bit(bit, &psock->state);
377 static inline struct sk_psock *sk_psock_get_checked(struct sock *sk)
379 struct sk_psock *psock;
382 psock = sk_psock(sk);
384 if (sk->sk_prot->recvmsg != tcp_bpf_recvmsg) {
385 psock = ERR_PTR(-EBUSY);
389 if (!refcount_inc_not_zero(&psock->refcnt))
390 psock = ERR_PTR(-EBUSY);
397 static inline struct sk_psock *sk_psock_get(struct sock *sk)
399 struct sk_psock *psock;
402 psock = sk_psock(sk);
403 if (psock && !refcount_inc_not_zero(&psock->refcnt))
409 void sk_psock_stop(struct sock *sk, struct sk_psock *psock);
410 void sk_psock_destroy(struct rcu_head *rcu);
411 void sk_psock_drop(struct sock *sk, struct sk_psock *psock);
413 static inline void sk_psock_put(struct sock *sk, struct sk_psock *psock)
415 if (refcount_dec_and_test(&psock->refcnt))
416 sk_psock_drop(sk, psock);
419 static inline void psock_set_prog(struct bpf_prog **pprog,
420 struct bpf_prog *prog)
422 prog = xchg(pprog, prog);
427 static inline void psock_progs_drop(struct sk_psock_progs *progs)
429 psock_set_prog(&progs->msg_parser, NULL);
430 psock_set_prog(&progs->skb_parser, NULL);
431 psock_set_prog(&progs->skb_verdict, NULL);
434 #endif /* _LINUX_SKMSG_H */