^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0-only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Stream Parser
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2016 Tom Herbert <tom@herbertland.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #ifndef __NET_STRPARSER_H_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define __NET_STRPARSER_H_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define STRP_STATS_ADD(stat, count) ((stat) += (count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define STRP_STATS_INCR(stat) ((stat)++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct strp_stats {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) unsigned long long msgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) unsigned long long bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) unsigned int mem_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned int need_more_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned int msg_too_big;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned int msg_timeouts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) unsigned int bad_hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct strp_aggr_stats {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned long long msgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) unsigned long long bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) unsigned int mem_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) unsigned int need_more_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned int msg_too_big;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned int msg_timeouts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) unsigned int bad_hdr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) unsigned int aborts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) unsigned int interrupted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) unsigned int unrecov_intr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct strparser;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* Callbacks are called with lock held for the attached socket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct strp_callbacks {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int (*parse_msg)(struct strparser *strp, struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) void (*rcv_msg)(struct strparser *strp, struct sk_buff *skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int (*read_sock_done)(struct strparser *strp, int err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) void (*abort_parser)(struct strparser *strp, int err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) void (*lock)(struct strparser *strp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) void (*unlock)(struct strparser *strp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct strp_msg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int full_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct _strp_msg {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /* Internal cb structure. struct strp_msg must be first for passing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * to upper layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct strp_msg strp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int accum_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct sk_skb_cb {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define SK_SKB_CB_PRIV_LEN 20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) unsigned char data[SK_SKB_CB_PRIV_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct _strp_msg strp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static inline struct strp_msg *strp_msg(struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return (struct strp_msg *)((void *)skb->cb +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) offsetof(struct sk_skb_cb, strp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /* Structure for an attached lower socket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct strparser {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) u32 stopped : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) u32 paused : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) u32 aborted : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) u32 interrupted : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) u32 unrecov_intr : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct sk_buff **skb_nextp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct sk_buff *skb_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) unsigned int need_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct delayed_work msg_timer_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct strp_stats stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct strp_callbacks cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* Must be called with lock held for attached socket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static inline void strp_pause(struct strparser *strp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) strp->paused = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* May be called without holding lock for attached socket */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) void strp_unpause(struct strparser *strp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /* Must be called with process lock held (lock_sock) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) void __strp_unpause(struct strparser *strp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static inline void save_strp_stats(struct strparser *strp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) struct strp_aggr_stats *agg_stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /* Save psock statistics in the mux when psock is being unattached. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat += \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) strp->stats._stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) SAVE_PSOCK_STATS(msgs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) SAVE_PSOCK_STATS(bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) SAVE_PSOCK_STATS(mem_fail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) SAVE_PSOCK_STATS(need_more_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) SAVE_PSOCK_STATS(msg_too_big);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) SAVE_PSOCK_STATS(msg_timeouts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) SAVE_PSOCK_STATS(bad_hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #undef SAVE_PSOCK_STATS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (strp->aborted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) agg_stats->aborts++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (strp->interrupted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) agg_stats->interrupted++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (strp->unrecov_intr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) agg_stats->unrecov_intr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static inline void aggregate_strp_stats(struct strp_aggr_stats *stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct strp_aggr_stats *agg_stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #define SAVE_PSOCK_STATS(_stat) (agg_stats->_stat += stats->_stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) SAVE_PSOCK_STATS(msgs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) SAVE_PSOCK_STATS(bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) SAVE_PSOCK_STATS(mem_fail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) SAVE_PSOCK_STATS(need_more_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) SAVE_PSOCK_STATS(msg_too_big);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) SAVE_PSOCK_STATS(msg_timeouts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) SAVE_PSOCK_STATS(bad_hdr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) SAVE_PSOCK_STATS(aborts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) SAVE_PSOCK_STATS(interrupted);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) SAVE_PSOCK_STATS(unrecov_intr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) #undef SAVE_PSOCK_STATS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) void strp_done(struct strparser *strp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) void strp_stop(struct strparser *strp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) void strp_check_rcv(struct strparser *strp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) int strp_init(struct strparser *strp, struct sock *sk,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) const struct strp_callbacks *cb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) void strp_data_ready(struct strparser *strp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) int strp_process(struct strparser *strp, struct sk_buff *orig_skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) unsigned int orig_offset, size_t orig_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) size_t max_msg_size, long timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) #endif /* __NET_STRPARSER_H_ */