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