blob: f162b79c3cf1f6f6709768c3871e630794f8c368 [file] [log] [blame]
Xiaoping Fand44a5b42015-05-26 17:37:37 -07001/*
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05302 * sfe.h
Xiaoping Fand44a5b42015-05-26 17:37:37 -07003 * Shortcut forwarding engine.
4 *
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05305 * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
6 * Copyright (c) 2021 Qualcomm Innovation Center, Inc. All rights reserved.
7 *
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
Xiaoping Fana42c68b2015-08-07 18:00:39 -070012 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053017 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Xiaoping Fand44a5b42015-05-26 17:37:37 -070019 */
20
21/*
22 * connection flags.
23 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070024#define SFE_CREATE_FLAG_NO_SEQ_CHECK BIT(0)
Xiaoping Fand44a5b42015-05-26 17:37:37 -070025 /* Indicates that we should not check sequence numbers */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070026#define SFE_CREATE_FLAG_REMARK_PRIORITY BIT(1)
Xiaoping Fane1963d42015-08-25 17:06:19 -070027 /* Indicates that we should remark priority of skb */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070028#define SFE_CREATE_FLAG_REMARK_DSCP BIT(2)
Xiaoping Fane1963d42015-08-25 17:06:19 -070029 /* Indicates that we should remark DSCP of packet */
Xiaoping Fand44a5b42015-05-26 17:37:37 -070030
31/*
32 * IPv6 address structure
33 */
34struct sfe_ipv6_addr {
35 __be32 addr[4];
36};
37
38typedef union {
39 __be32 ip;
40 struct sfe_ipv6_addr ip6[1];
41} sfe_ip_addr_t;
42
43/*
44 * connection creation structure.
45 */
46struct sfe_connection_create {
47 int protocol;
48 struct net_device *src_dev;
49 struct net_device *dest_dev;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070050 u32 flags;
51 u32 src_mtu;
52 u32 dest_mtu;
Xiaoping Fand44a5b42015-05-26 17:37:37 -070053 sfe_ip_addr_t src_ip;
54 sfe_ip_addr_t src_ip_xlate;
55 sfe_ip_addr_t dest_ip;
56 sfe_ip_addr_t dest_ip_xlate;
57 __be16 src_port;
58 __be16 src_port_xlate;
59 __be16 dest_port;
60 __be16 dest_port_xlate;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070061 u8 src_mac[ETH_ALEN];
62 u8 src_mac_xlate[ETH_ALEN];
63 u8 dest_mac[ETH_ALEN];
64 u8 dest_mac_xlate[ETH_ALEN];
65 u8 src_td_window_scale;
66 u32 src_td_max_window;
67 u32 src_td_end;
68 u32 src_td_max_end;
69 u8 dest_td_window_scale;
70 u32 dest_td_max_window;
71 u32 dest_td_end;
72 u32 dest_td_max_end;
73 u32 mark;
Zhi Chen8748eb32015-06-18 12:58:48 -070074#ifdef CONFIG_XFRM
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070075 u32 original_accel;
76 u32 reply_accel;
Zhi Chen8748eb32015-06-18 12:58:48 -070077#endif
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070078 u32 src_priority;
79 u32 dest_priority;
80 u32 src_dscp;
81 u32 dest_dscp;
Xiaoping Fand44a5b42015-05-26 17:37:37 -070082};
83
84/*
85 * connection destruction structure.
86 */
87struct sfe_connection_destroy {
88 int protocol;
89 sfe_ip_addr_t src_ip;
90 sfe_ip_addr_t dest_ip;
91 __be16 src_port;
92 __be16 dest_port;
93};
94
Xiaoping Fan99cb4c12015-08-21 19:07:32 -070095typedef enum sfe_sync_reason {
96 SFE_SYNC_REASON_STATS, /* Sync is to synchronize stats */
97 SFE_SYNC_REASON_FLUSH, /* Sync is to flush a entry */
98 SFE_SYNC_REASON_DESTROY /* Sync is to destroy a entry(requested by connection manager) */
99} sfe_sync_reason_t;
100
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700101/*
102 * Structure used to sync connection stats/state back within the system.
103 *
104 * NOTE: The addresses here are NON-NAT addresses, i.e. the true endpoint addressing.
105 * 'src' is the creator of the connection.
106 */
107struct sfe_connection_sync {
108 struct net_device *src_dev;
109 struct net_device *dest_dev;
110 int is_v6; /* Is it for ipv6? */
111 int protocol; /* IP protocol number (IPPROTO_...) */
112 sfe_ip_addr_t src_ip; /* Non-NAT source address, i.e. the creator of the connection */
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700113 sfe_ip_addr_t src_ip_xlate; /* NATed source address */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700114 __be16 src_port; /* Non-NAT source port */
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700115 __be16 src_port_xlate; /* NATed source port */
116 sfe_ip_addr_t dest_ip; /* Non-NAT destination address, i.e. to whom the connection was created */
117 sfe_ip_addr_t dest_ip_xlate; /* NATed destination address */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700118 __be16 dest_port; /* Non-NAT destination port */
Xiaoping Fan99cb4c12015-08-21 19:07:32 -0700119 __be16 dest_port_xlate; /* NATed destination port */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700120 u32 src_td_max_window;
121 u32 src_td_end;
122 u32 src_td_max_end;
123 u64 src_packet_count;
124 u64 src_byte_count;
125 u32 src_new_packet_count;
126 u32 src_new_byte_count;
127 u32 dest_td_max_window;
128 u32 dest_td_end;
129 u32 dest_td_max_end;
130 u64 dest_packet_count;
131 u64 dest_byte_count;
132 u32 dest_new_packet_count;
133 u32 dest_new_byte_count;
134 u32 reason; /* reason for stats sync message, i.e. destroy, flush, period sync */
135 u64 delta_jiffies; /* Time to be added to the current timeout to keep the connection alive */
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700136};
137
138/*
139 * connection mark structure
140 */
141struct sfe_connection_mark {
142 int protocol;
143 sfe_ip_addr_t src_ip;
144 sfe_ip_addr_t dest_ip;
145 __be16 src_port;
146 __be16 dest_port;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700147 u32 mark;
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700148};
149
150/*
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700151 * Expose the hook for the receive processing.
152 */
153extern int (*athrs_fast_nat_recv)(struct sk_buff *skb);
154
155/*
156 * Expose what should be a static flag in the TCP connection tracker.
157 */
158extern int nf_ct_tcp_no_window_check;
159
160/*
161 * This callback will be called in a timer
162 * at 100 times per second to sync stats back to
163 * Linux connection track.
164 *
165 * A RCU lock is taken to prevent this callback
166 * from unregistering.
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700167 */
168typedef void (*sfe_sync_rule_callback_t)(struct sfe_connection_sync *);
169
170/*
171 * IPv4 APIs used by connection manager
172 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700173int sfe_ipv4_recv(struct net_device *dev, struct sk_buff *skb);
174int sfe_ipv4_create_rule(struct sfe_connection_create *sic);
175void sfe_ipv4_destroy_rule(struct sfe_connection_destroy *sid);
176void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev);
177void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t callback);
178void sfe_ipv4_update_rule(struct sfe_connection_create *sic);
179void sfe_ipv4_mark_rule(struct sfe_connection_mark *mark);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700180
181#ifdef SFE_SUPPORT_IPV6
182/*
183 * IPv6 APIs used by connection manager
184 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700185int sfe_ipv6_recv(struct net_device *dev, struct sk_buff *skb);
186int sfe_ipv6_create_rule(struct sfe_connection_create *sic);
187void sfe_ipv6_destroy_rule(struct sfe_connection_destroy *sid);
188void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev);
189void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t callback);
190void sfe_ipv6_update_rule(struct sfe_connection_create *sic);
191void sfe_ipv6_mark_rule(struct sfe_connection_mark *mark);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700192#else
193static inline int sfe_ipv6_recv(struct net_device *dev, struct sk_buff *skb)
194{
195 return 0;
196}
197
198static inline int sfe_ipv6_create_rule(struct sfe_connection_create *sic)
199{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700200 return 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700201}
202
203static inline void sfe_ipv6_destroy_rule(struct sfe_connection_destroy *sid)
204{
205 return;
206}
207
208static inline void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev)
209{
210 return;
211}
212
213static inline void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t callback)
214{
215 return;
216}
217
218static inline void sfe_ipv6_update_rule(struct sfe_connection_create *sic)
219{
220 return;
221}
222
223static inline void sfe_ipv6_mark_rule(struct sfe_connection_mark *mark)
224{
225 return;
226}
227#endif
Xiaoping Fan9b6bb332016-04-05 19:21:26 -0700228
229/*
230 * sfe_ipv6_addr_equal()
231 * compare ipv6 address
232 *
233 * return: 1, equal; 0, no equal
234 */
235static inline int sfe_ipv6_addr_equal(struct sfe_ipv6_addr *a,
236 struct sfe_ipv6_addr *b)
237{
238 return a->addr[0] == b->addr[0] &&
239 a->addr[1] == b->addr[1] &&
240 a->addr[2] == b->addr[2] &&
241 a->addr[3] == b->addr[3];
242}
243
244/*
245 * sfe_ipv4_addr_equal()
246 * compare ipv4 address
247 *
248 * return: 1, equal; 0, no equal
249 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700250#define sfe_ipv4_addr_equal(a, b) ((u32)(a) == (u32)(b))
Xiaoping Fan9b6bb332016-04-05 19:21:26 -0700251
252/*
253 * sfe_addr_equal()
254 * compare ipv4 or ipv6 address
255 *
256 * return: 1, equal; 0, no equal
257 */
258static inline int sfe_addr_equal(sfe_ip_addr_t *a,
259 sfe_ip_addr_t *b, int is_v4)
260{
261 return is_v4 ? sfe_ipv4_addr_equal(a->ip, b->ip) : sfe_ipv6_addr_equal(a->ip6, b->ip6);
262}
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +0530263
264int sfe_init_if(void);
265void sfe_exit_if(void);