blob: 897bfaaa131378388b60cc2ae0fc85a482325881 [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/*
Xiaoping Fand44a5b42015-05-26 17:37:37 -070022 * IPv6 address structure
23 */
24struct sfe_ipv6_addr {
25 __be32 addr[4];
26};
27
28typedef union {
29 __be32 ip;
30 struct sfe_ipv6_addr ip6[1];
31} sfe_ip_addr_t;
32
Xiaoping Fan99cb4c12015-08-21 19:07:32 -070033typedef enum sfe_sync_reason {
34 SFE_SYNC_REASON_STATS, /* Sync is to synchronize stats */
35 SFE_SYNC_REASON_FLUSH, /* Sync is to flush a entry */
36 SFE_SYNC_REASON_DESTROY /* Sync is to destroy a entry(requested by connection manager) */
37} sfe_sync_reason_t;
38
Xiaoping Fand44a5b42015-05-26 17:37:37 -070039/*
40 * Structure used to sync connection stats/state back within the system.
41 *
42 * NOTE: The addresses here are NON-NAT addresses, i.e. the true endpoint addressing.
43 * 'src' is the creator of the connection.
44 */
45struct sfe_connection_sync {
46 struct net_device *src_dev;
47 struct net_device *dest_dev;
48 int is_v6; /* Is it for ipv6? */
49 int protocol; /* IP protocol number (IPPROTO_...) */
50 sfe_ip_addr_t src_ip; /* Non-NAT source address, i.e. the creator of the connection */
Xiaoping Fan99cb4c12015-08-21 19:07:32 -070051 sfe_ip_addr_t src_ip_xlate; /* NATed source address */
Xiaoping Fand44a5b42015-05-26 17:37:37 -070052 __be16 src_port; /* Non-NAT source port */
Xiaoping Fan99cb4c12015-08-21 19:07:32 -070053 __be16 src_port_xlate; /* NATed source port */
54 sfe_ip_addr_t dest_ip; /* Non-NAT destination address, i.e. to whom the connection was created */
55 sfe_ip_addr_t dest_ip_xlate; /* NATed destination address */
Xiaoping Fand44a5b42015-05-26 17:37:37 -070056 __be16 dest_port; /* Non-NAT destination port */
Xiaoping Fan99cb4c12015-08-21 19:07:32 -070057 __be16 dest_port_xlate; /* NATed destination port */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070058 u32 src_td_max_window;
59 u32 src_td_end;
60 u32 src_td_max_end;
61 u64 src_packet_count;
62 u64 src_byte_count;
63 u32 src_new_packet_count;
64 u32 src_new_byte_count;
65 u32 dest_td_max_window;
66 u32 dest_td_end;
67 u32 dest_td_max_end;
68 u64 dest_packet_count;
69 u64 dest_byte_count;
70 u32 dest_new_packet_count;
71 u32 dest_new_byte_count;
72 u32 reason; /* reason for stats sync message, i.e. destroy, flush, period sync */
73 u64 delta_jiffies; /* Time to be added to the current timeout to keep the connection alive */
Xiaoping Fand44a5b42015-05-26 17:37:37 -070074};
75
76/*
77 * connection mark structure
78 */
79struct sfe_connection_mark {
80 int protocol;
81 sfe_ip_addr_t src_ip;
82 sfe_ip_addr_t dest_ip;
83 __be16 src_port;
84 __be16 dest_port;
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070085 u32 mark;
Xiaoping Fand44a5b42015-05-26 17:37:37 -070086};
87
88/*
Xiaoping Fan6a1672f2016-08-17 19:58:12 -070089 * Expose the hook for the receive processing.
90 */
91extern int (*athrs_fast_nat_recv)(struct sk_buff *skb);
92
93/*
94 * Expose what should be a static flag in the TCP connection tracker.
95 */
96extern int nf_ct_tcp_no_window_check;
97
98/*
99 * This callback will be called in a timer
100 * at 100 times per second to sync stats back to
101 * Linux connection track.
102 *
103 * A RCU lock is taken to prevent this callback
104 * from unregistering.
Xiaoping Fand44a5b42015-05-26 17:37:37 -0700105 */
106typedef void (*sfe_sync_rule_callback_t)(struct sfe_connection_sync *);
107
108/*
109 * IPv4 APIs used by connection manager
110 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700111int sfe_ipv4_recv(struct net_device *dev, struct sk_buff *skb);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530112int sfe_ipv4_create_rule(struct sfe_ipv4_rule_create_msg *msg);
113void sfe_ipv4_destroy_rule(struct sfe_ipv4_rule_destroy_msg *msg);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700114void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev);
115void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t callback);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530116void sfe_ipv4_update_rule(struct sfe_ipv4_rule_create_msg *msg);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700117void sfe_ipv4_mark_rule(struct sfe_connection_mark *mark);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700118
119#ifdef SFE_SUPPORT_IPV6
120/*
121 * IPv6 APIs used by connection manager
122 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700123int sfe_ipv6_recv(struct net_device *dev, struct sk_buff *skb);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530124int sfe_ipv6_create_rule(struct sfe_ipv6_rule_create_msg *msg);
125void sfe_ipv6_destroy_rule(struct sfe_ipv6_rule_destroy_msg *msg);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700126void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev);
127void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t callback);
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530128void sfe_ipv6_update_rule(struct sfe_ipv6_rule_create_msg *msg);
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700129void sfe_ipv6_mark_rule(struct sfe_connection_mark *mark);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700130#else
131static inline int sfe_ipv6_recv(struct net_device *dev, struct sk_buff *skb)
132{
133 return 0;
134}
135
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530136static inline int sfe_ipv6_create_rule(struct sfe_ipv6_rule_create_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700137{
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700138 return 0;
Xiaoping Fan978b3772015-05-27 14:15:18 -0700139}
140
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530141static inline void sfe_ipv6_destroy_rule(struct sfe_ipv6_rule_destroy_msg *msg);
Xiaoping Fan978b3772015-05-27 14:15:18 -0700142{
143 return;
144}
145
146static inline void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev)
147{
148 return;
149}
150
151static inline void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t callback)
152{
153 return;
154}
155
Ratheesh Kannoth89302a72021-10-20 08:10:37 +0530156static inline void sfe_ipv6_update_rule(struct sfe_ipv6_rule_create_msg *msg)
Xiaoping Fan978b3772015-05-27 14:15:18 -0700157{
158 return;
159}
160
161static inline void sfe_ipv6_mark_rule(struct sfe_connection_mark *mark)
162{
163 return;
164}
165#endif
Xiaoping Fan9b6bb332016-04-05 19:21:26 -0700166
167/*
168 * sfe_ipv6_addr_equal()
169 * compare ipv6 address
170 *
171 * return: 1, equal; 0, no equal
172 */
173static inline int sfe_ipv6_addr_equal(struct sfe_ipv6_addr *a,
174 struct sfe_ipv6_addr *b)
175{
176 return a->addr[0] == b->addr[0] &&
177 a->addr[1] == b->addr[1] &&
178 a->addr[2] == b->addr[2] &&
179 a->addr[3] == b->addr[3];
180}
181
182/*
183 * sfe_ipv4_addr_equal()
184 * compare ipv4 address
185 *
186 * return: 1, equal; 0, no equal
187 */
Xiaoping Fan6a1672f2016-08-17 19:58:12 -0700188#define sfe_ipv4_addr_equal(a, b) ((u32)(a) == (u32)(b))
Xiaoping Fan9b6bb332016-04-05 19:21:26 -0700189
190/*
191 * sfe_addr_equal()
192 * compare ipv4 or ipv6 address
193 *
194 * return: 1, equal; 0, no equal
195 */
196static inline int sfe_addr_equal(sfe_ip_addr_t *a,
197 sfe_ip_addr_t *b, int is_v4)
198{
199 return is_v4 ? sfe_ipv4_addr_equal(a->ip, b->ip) : sfe_ipv6_addr_equal(a->ip6, b->ip6);
200}
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +0530201
202int sfe_init_if(void);
203void sfe_exit_if(void);