Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 1 | /* |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame^] | 2 | * sfe.h |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 3 | * Shortcut forwarding engine. |
| 4 | * |
Ratheesh Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame^] | 5 | * 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 Fan | a42c68b | 2015-08-07 18:00:39 -0700 | [diff] [blame] | 12 | * 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 Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame^] | 17 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * connection flags. |
| 23 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 24 | #define SFE_CREATE_FLAG_NO_SEQ_CHECK BIT(0) |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 25 | /* Indicates that we should not check sequence numbers */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 26 | #define SFE_CREATE_FLAG_REMARK_PRIORITY BIT(1) |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 27 | /* Indicates that we should remark priority of skb */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 28 | #define SFE_CREATE_FLAG_REMARK_DSCP BIT(2) |
Xiaoping Fan | e1963d4 | 2015-08-25 17:06:19 -0700 | [diff] [blame] | 29 | /* Indicates that we should remark DSCP of packet */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * IPv6 address structure |
| 33 | */ |
| 34 | struct sfe_ipv6_addr { |
| 35 | __be32 addr[4]; |
| 36 | }; |
| 37 | |
| 38 | typedef union { |
| 39 | __be32 ip; |
| 40 | struct sfe_ipv6_addr ip6[1]; |
| 41 | } sfe_ip_addr_t; |
| 42 | |
| 43 | /* |
| 44 | * connection creation structure. |
| 45 | */ |
| 46 | struct sfe_connection_create { |
| 47 | int protocol; |
| 48 | struct net_device *src_dev; |
| 49 | struct net_device *dest_dev; |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 50 | u32 flags; |
| 51 | u32 src_mtu; |
| 52 | u32 dest_mtu; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 53 | 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 Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 61 | 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 Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 74 | #ifdef CONFIG_XFRM |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 75 | u32 original_accel; |
| 76 | u32 reply_accel; |
Zhi Chen | 8748eb3 | 2015-06-18 12:58:48 -0700 | [diff] [blame] | 77 | #endif |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 78 | u32 src_priority; |
| 79 | u32 dest_priority; |
| 80 | u32 src_dscp; |
| 81 | u32 dest_dscp; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | /* |
| 85 | * connection destruction structure. |
| 86 | */ |
| 87 | struct 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 Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 95 | typedef 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 Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 101 | /* |
| 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 | */ |
| 107 | struct 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 Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 113 | sfe_ip_addr_t src_ip_xlate; /* NATed source address */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 114 | __be16 src_port; /* Non-NAT source port */ |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 115 | __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 Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 118 | __be16 dest_port; /* Non-NAT destination port */ |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 119 | __be16 dest_port_xlate; /* NATed destination port */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 120 | 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 Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | /* |
| 139 | * connection mark structure |
| 140 | */ |
| 141 | struct 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 Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 147 | u32 mark; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | /* |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 151 | * Expose the hook for the receive processing. |
| 152 | */ |
| 153 | extern 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 | */ |
| 158 | extern 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 Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 167 | */ |
| 168 | typedef void (*sfe_sync_rule_callback_t)(struct sfe_connection_sync *); |
| 169 | |
| 170 | /* |
| 171 | * IPv4 APIs used by connection manager |
| 172 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 173 | int sfe_ipv4_recv(struct net_device *dev, struct sk_buff *skb); |
| 174 | int sfe_ipv4_create_rule(struct sfe_connection_create *sic); |
| 175 | void sfe_ipv4_destroy_rule(struct sfe_connection_destroy *sid); |
| 176 | void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev); |
| 177 | void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t callback); |
| 178 | void sfe_ipv4_update_rule(struct sfe_connection_create *sic); |
| 179 | void sfe_ipv4_mark_rule(struct sfe_connection_mark *mark); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 180 | |
| 181 | #ifdef SFE_SUPPORT_IPV6 |
| 182 | /* |
| 183 | * IPv6 APIs used by connection manager |
| 184 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 185 | int sfe_ipv6_recv(struct net_device *dev, struct sk_buff *skb); |
| 186 | int sfe_ipv6_create_rule(struct sfe_connection_create *sic); |
| 187 | void sfe_ipv6_destroy_rule(struct sfe_connection_destroy *sid); |
| 188 | void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev); |
| 189 | void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t callback); |
| 190 | void sfe_ipv6_update_rule(struct sfe_connection_create *sic); |
| 191 | void sfe_ipv6_mark_rule(struct sfe_connection_mark *mark); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 192 | #else |
| 193 | static inline int sfe_ipv6_recv(struct net_device *dev, struct sk_buff *skb) |
| 194 | { |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static inline int sfe_ipv6_create_rule(struct sfe_connection_create *sic) |
| 199 | { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 200 | return 0; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | static inline void sfe_ipv6_destroy_rule(struct sfe_connection_destroy *sid) |
| 204 | { |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | static inline void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev) |
| 209 | { |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | static inline void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t callback) |
| 214 | { |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | static inline void sfe_ipv6_update_rule(struct sfe_connection_create *sic) |
| 219 | { |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | static inline void sfe_ipv6_mark_rule(struct sfe_connection_mark *mark) |
| 224 | { |
| 225 | return; |
| 226 | } |
| 227 | #endif |
Xiaoping Fan | 9b6bb33 | 2016-04-05 19:21:26 -0700 | [diff] [blame] | 228 | |
| 229 | /* |
| 230 | * sfe_ipv6_addr_equal() |
| 231 | * compare ipv6 address |
| 232 | * |
| 233 | * return: 1, equal; 0, no equal |
| 234 | */ |
| 235 | static 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 Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 250 | #define sfe_ipv4_addr_equal(a, b) ((u32)(a) == (u32)(b)) |
Xiaoping Fan | 9b6bb33 | 2016-04-05 19:21:26 -0700 | [diff] [blame] | 251 | |
| 252 | /* |
| 253 | * sfe_addr_equal() |
| 254 | * compare ipv4 or ipv6 address |
| 255 | * |
| 256 | * return: 1, equal; 0, no equal |
| 257 | */ |
| 258 | static 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 Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame^] | 263 | |
| 264 | int sfe_init_if(void); |
| 265 | void sfe_exit_if(void); |