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
17 #define NR_MSG_FRAG_IDS (MAX_MSG_FRAGS + 1)
32 bool copy[MAX_MSG_FRAGS];
33 /* The extra two elements:
34 * 1) used for chaining the front and sections when the list becomes
35 * partitioned (e.g. end < start). The crypto APIs require the
37 * 2) to chain tailer SG entries after the message.
39 struct scatterlist data[MAX_MSG_FRAGS + 2];
42 /* UAPI in filter.c depends on struct sk_msg_sg being first element. */
51 struct sock *sk_redir;
53 struct list_head list;
56 struct sk_psock_progs {
57 struct bpf_prog *msg_parser;
58 struct bpf_prog *skb_parser;
59 struct bpf_prog *skb_verdict;
62 enum sk_psock_state_bits {
66 struct sk_psock_link {
67 struct list_head list;
72 struct sk_psock_parser {
73 struct strparser strp;
75 void (*saved_data_ready)(struct sock *sk);
78 struct sk_psock_work_state {
86 struct sock *sk_redir;
91 struct sk_psock_progs progs;
92 struct sk_psock_parser parser;
93 struct sk_buff_head ingress_skb;
94 struct list_head ingress_msg;
96 struct list_head link;
99 void (*saved_unhash)(struct sock *sk);
100 void (*saved_close)(struct sock *sk, long timeout);
101 void (*saved_write_space)(struct sock *sk);
102 struct proto *sk_proto;
103 struct sk_psock_work_state work_state;
104 struct work_struct work;
107 struct work_struct gc;
111 int sk_msg_alloc(struct sock *sk, struct sk_msg *msg, int len,
112 int elem_first_coalesce);
113 int sk_msg_clone(struct sock *sk, struct sk_msg *dst, struct sk_msg *src,
115 void sk_msg_trim(struct sock *sk, struct sk_msg *msg, int len);
116 int sk_msg_free(struct sock *sk, struct sk_msg *msg);
117 int sk_msg_free_nocharge(struct sock *sk, struct sk_msg *msg);
118 void sk_msg_free_partial(struct sock *sk, struct sk_msg *msg, u32 bytes);
119 void sk_msg_free_partial_nocharge(struct sock *sk, struct sk_msg *msg,
122 void sk_msg_return(struct sock *sk, struct sk_msg *msg, int bytes);
123 void sk_msg_return_zero(struct sock *sk, struct sk_msg *msg, int bytes);
125 int sk_msg_zerocopy_from_iter(struct sock *sk, struct iov_iter *from,
126 struct sk_msg *msg, u32 bytes);
127 int sk_msg_memcopy_from_iter(struct sock *sk, struct iov_iter *from,
128 struct sk_msg *msg, u32 bytes);
130 static inline void sk_msg_check_to_free(struct sk_msg *msg, u32 i, u32 bytes)
132 WARN_ON(i == msg->sg.end && bytes);
135 static inline void sk_msg_apply_bytes(struct sk_psock *psock, u32 bytes)
137 if (psock->apply_bytes) {
138 if (psock->apply_bytes < bytes)
139 psock->apply_bytes = 0;
141 psock->apply_bytes -= bytes;
145 static inline u32 sk_msg_iter_dist(u32 start, u32 end)
147 return end >= start ? end - start : end + (NR_MSG_FRAG_IDS - start);
150 #define sk_msg_iter_var_prev(var) \
153 var = NR_MSG_FRAG_IDS - 1; \
158 #define sk_msg_iter_var_next(var) \
161 if (var == NR_MSG_FRAG_IDS) \
165 #define sk_msg_iter_prev(msg, which) \
166 sk_msg_iter_var_prev(msg->sg.which)
168 #define sk_msg_iter_next(msg, which) \
169 sk_msg_iter_var_next(msg->sg.which)
171 static inline void sk_msg_clear_meta(struct sk_msg *msg)
173 memset(&msg->sg, 0, offsetofend(struct sk_msg_sg, copy));
176 static inline void sk_msg_init(struct sk_msg *msg)
178 BUILD_BUG_ON(ARRAY_SIZE(msg->sg.data) - 1 != NR_MSG_FRAG_IDS);
179 memset(msg, 0, sizeof(*msg));
180 sg_init_marker(msg->sg.data, NR_MSG_FRAG_IDS);
183 static inline void sk_msg_xfer(struct sk_msg *dst, struct sk_msg *src,
186 dst->sg.data[which] = src->sg.data[which];
187 dst->sg.data[which].length = size;
188 dst->sg.size += size;
189 src->sg.size -= size;
190 src->sg.data[which].length -= size;
191 src->sg.data[which].offset += size;
194 static inline void sk_msg_xfer_full(struct sk_msg *dst, struct sk_msg *src)
196 memcpy(dst, src, sizeof(*src));
200 static inline bool sk_msg_full(const struct sk_msg *msg)
202 return sk_msg_iter_dist(msg->sg.start, msg->sg.end) == MAX_MSG_FRAGS;
205 static inline u32 sk_msg_elem_used(const struct sk_msg *msg)
207 return sk_msg_iter_dist(msg->sg.start, msg->sg.end);
210 static inline struct scatterlist *sk_msg_elem(struct sk_msg *msg, int which)
212 return &msg->sg.data[which];
215 static inline struct scatterlist sk_msg_elem_cpy(struct sk_msg *msg, int which)
217 return msg->sg.data[which];
220 static inline struct page *sk_msg_page(struct sk_msg *msg, int which)
222 return sg_page(sk_msg_elem(msg, which));
225 static inline bool sk_msg_to_ingress(const struct sk_msg *msg)
227 return msg->flags & BPF_F_INGRESS;
230 static inline void sk_msg_compute_data_pointers(struct sk_msg *msg)
232 struct scatterlist *sge = sk_msg_elem(msg, msg->sg.start);
234 if (msg->sg.copy[msg->sg.start]) {
236 msg->data_end = NULL;
238 msg->data = sg_virt(sge);
239 msg->data_end = msg->data + sge->length;
243 static inline void sk_msg_page_add(struct sk_msg *msg, struct page *page,
246 struct scatterlist *sge;
249 sge = sk_msg_elem(msg, msg->sg.end);
250 sg_set_page(sge, page, len, offset);
253 msg->sg.copy[msg->sg.end] = true;
255 sk_msg_iter_next(msg, end);
258 static inline void sk_msg_sg_copy(struct sk_msg *msg, u32 i, bool copy_state)
261 msg->sg.copy[i] = copy_state;
262 sk_msg_iter_var_next(i);
263 if (i == msg->sg.end)
268 static inline void sk_msg_sg_copy_set(struct sk_msg *msg, u32 start)
270 sk_msg_sg_copy(msg, start, true);
273 static inline void sk_msg_sg_copy_clear(struct sk_msg *msg, u32 start)
275 sk_msg_sg_copy(msg, start, false);
278 static inline struct sk_psock *sk_psock(const struct sock *sk)
280 return rcu_dereference_sk_user_data(sk);
283 static inline void sk_psock_queue_msg(struct sk_psock *psock,
286 list_add_tail(&msg->list, &psock->ingress_msg);
289 static inline bool sk_psock_queue_empty(const struct sk_psock *psock)
291 return psock ? list_empty(&psock->ingress_msg) : true;
294 static inline void sk_psock_report_error(struct sk_psock *psock, int err)
296 struct sock *sk = psock->sk;
299 sk->sk_error_report(sk);
302 struct sk_psock *sk_psock_init(struct sock *sk, int node);
304 int sk_psock_init_strp(struct sock *sk, struct sk_psock *psock);
305 void sk_psock_start_strp(struct sock *sk, struct sk_psock *psock);
306 void sk_psock_stop_strp(struct sock *sk, struct sk_psock *psock);
308 int sk_psock_msg_verdict(struct sock *sk, struct sk_psock *psock,
311 static inline struct sk_psock_link *sk_psock_init_link(void)
313 return kzalloc(sizeof(struct sk_psock_link),
314 GFP_ATOMIC | __GFP_NOWARN);
317 static inline void sk_psock_free_link(struct sk_psock_link *link)
322 struct sk_psock_link *sk_psock_link_pop(struct sk_psock *psock);
323 #if defined(CONFIG_BPF_STREAM_PARSER)
324 void sk_psock_unlink(struct sock *sk, struct sk_psock_link *link);
326 static inline void sk_psock_unlink(struct sock *sk,
327 struct sk_psock_link *link)
332 void __sk_psock_purge_ingress_msg(struct sk_psock *psock);
334 static inline void sk_psock_cork_free(struct sk_psock *psock)
337 sk_msg_free(psock->sk, psock->cork);
343 static inline void sk_psock_update_proto(struct sock *sk,
344 struct sk_psock *psock,
347 psock->saved_unhash = sk->sk_prot->unhash;
348 psock->saved_close = sk->sk_prot->close;
349 psock->saved_write_space = sk->sk_write_space;
351 psock->sk_proto = sk->sk_prot;
355 static inline void sk_psock_restore_proto(struct sock *sk,
356 struct sk_psock *psock)
358 if (psock->sk_proto) {
359 struct inet_connection_sock *icsk = inet_csk(sk);
360 bool has_ulp = !!icsk->icsk_ulp_data;
363 /* TLS does not have an unhash proto in SW cases, but we need
364 * to ensure we stop using the sock_map unhash routine because
365 * the associated psock is being removed. So use the original
368 WRITE_ONCE(sk->sk_prot->unhash, psock->saved_unhash);
369 tcp_update_ulp(sk, psock->sk_proto,
370 psock->saved_write_space);
372 sk->sk_prot = psock->sk_proto;
373 sk->sk_write_space = psock->saved_write_space;
375 psock->sk_proto = NULL;
377 sk->sk_write_space = psock->saved_write_space;
381 static inline void sk_psock_set_state(struct sk_psock *psock,
382 enum sk_psock_state_bits bit)
384 set_bit(bit, &psock->state);
387 static inline void sk_psock_clear_state(struct sk_psock *psock,
388 enum sk_psock_state_bits bit)
390 clear_bit(bit, &psock->state);
393 static inline bool sk_psock_test_state(const struct sk_psock *psock,
394 enum sk_psock_state_bits bit)
396 return test_bit(bit, &psock->state);
399 static inline struct sk_psock *sk_psock_get_checked(struct sock *sk)
401 struct sk_psock *psock;
404 psock = sk_psock(sk);
406 if (sk->sk_prot->recvmsg != tcp_bpf_recvmsg) {
407 psock = ERR_PTR(-EBUSY);
411 if (!refcount_inc_not_zero(&psock->refcnt))
412 psock = ERR_PTR(-EBUSY);
419 static inline struct sk_psock *sk_psock_get(struct sock *sk)
421 struct sk_psock *psock;
424 psock = sk_psock(sk);
425 if (psock && !refcount_inc_not_zero(&psock->refcnt))
431 void sk_psock_stop(struct sock *sk, struct sk_psock *psock);
432 void sk_psock_destroy(struct rcu_head *rcu);
433 void sk_psock_drop(struct sock *sk, struct sk_psock *psock);
435 static inline void sk_psock_put(struct sock *sk, struct sk_psock *psock)
437 if (refcount_dec_and_test(&psock->refcnt))
438 sk_psock_drop(sk, psock);
441 static inline void sk_psock_data_ready(struct sock *sk, struct sk_psock *psock)
443 if (psock->parser.enabled)
444 psock->parser.saved_data_ready(sk);
446 sk->sk_data_ready(sk);
449 static inline void psock_set_prog(struct bpf_prog **pprog,
450 struct bpf_prog *prog)
452 prog = xchg(pprog, prog);
457 static inline int psock_replace_prog(struct bpf_prog **pprog,
458 struct bpf_prog *prog,
459 struct bpf_prog *old)
461 if (cmpxchg(pprog, old, prog) != old)
470 static inline void psock_progs_drop(struct sk_psock_progs *progs)
472 psock_set_prog(&progs->msg_parser, NULL);
473 psock_set_prog(&progs->skb_parser, NULL);
474 psock_set_prog(&progs->skb_verdict, NULL);
477 int sk_psock_tls_strp_read(struct sk_psock *psock, struct sk_buff *skb);
479 static inline bool sk_psock_strp_enabled(struct sk_psock *psock)
483 return psock->parser.enabled;
485 #endif /* _LINUX_SKMSG_H */