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 | /* |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 22 | * IPv6 address structure |
| 23 | */ |
| 24 | struct sfe_ipv6_addr { |
| 25 | __be32 addr[4]; |
| 26 | }; |
| 27 | |
| 28 | typedef union { |
| 29 | __be32 ip; |
| 30 | struct sfe_ipv6_addr ip6[1]; |
| 31 | } sfe_ip_addr_t; |
| 32 | |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 33 | typedef 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 Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 39 | /* |
| 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 | */ |
| 45 | struct 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 Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 51 | sfe_ip_addr_t src_ip_xlate; /* NATed source address */ |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 52 | __be16 src_port; /* Non-NAT source port */ |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 53 | __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 Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 56 | __be16 dest_port; /* Non-NAT destination port */ |
Xiaoping Fan | 99cb4c1 | 2015-08-21 19:07:32 -0700 | [diff] [blame] | 57 | __be16 dest_port_xlate; /* NATed destination port */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 58 | 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 Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /* |
| 77 | * connection mark structure |
| 78 | */ |
| 79 | struct 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 Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 85 | u32 mark; |
Xiaoping Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | /* |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 89 | * Expose the hook for the receive processing. |
| 90 | */ |
| 91 | extern 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 | */ |
| 96 | extern 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 Fan | d44a5b4 | 2015-05-26 17:37:37 -0700 | [diff] [blame] | 105 | */ |
| 106 | typedef void (*sfe_sync_rule_callback_t)(struct sfe_connection_sync *); |
| 107 | |
| 108 | /* |
| 109 | * IPv4 APIs used by connection manager |
| 110 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 111 | int sfe_ipv4_recv(struct net_device *dev, struct sk_buff *skb); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame^] | 112 | int sfe_ipv4_create_rule(struct sfe_ipv4_rule_create_msg *msg); |
| 113 | void sfe_ipv4_destroy_rule(struct sfe_ipv4_rule_destroy_msg *msg); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 114 | void sfe_ipv4_destroy_all_rules_for_dev(struct net_device *dev); |
| 115 | void sfe_ipv4_register_sync_rule_callback(sfe_sync_rule_callback_t callback); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame^] | 116 | void sfe_ipv4_update_rule(struct sfe_ipv4_rule_create_msg *msg); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 117 | void sfe_ipv4_mark_rule(struct sfe_connection_mark *mark); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 118 | |
| 119 | #ifdef SFE_SUPPORT_IPV6 |
| 120 | /* |
| 121 | * IPv6 APIs used by connection manager |
| 122 | */ |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 123 | int sfe_ipv6_recv(struct net_device *dev, struct sk_buff *skb); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame^] | 124 | int sfe_ipv6_create_rule(struct sfe_ipv6_rule_create_msg *msg); |
| 125 | void sfe_ipv6_destroy_rule(struct sfe_ipv6_rule_destroy_msg *msg); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 126 | void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev); |
| 127 | void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t callback); |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame^] | 128 | void sfe_ipv6_update_rule(struct sfe_ipv6_rule_create_msg *msg); |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 129 | void sfe_ipv6_mark_rule(struct sfe_connection_mark *mark); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 130 | #else |
| 131 | static inline int sfe_ipv6_recv(struct net_device *dev, struct sk_buff *skb) |
| 132 | { |
| 133 | return 0; |
| 134 | } |
| 135 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame^] | 136 | static inline int sfe_ipv6_create_rule(struct sfe_ipv6_rule_create_msg *msg) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 137 | { |
Xiaoping Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 138 | return 0; |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame^] | 141 | static inline void sfe_ipv6_destroy_rule(struct sfe_ipv6_rule_destroy_msg *msg); |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 142 | { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | static inline void sfe_ipv6_destroy_all_rules_for_dev(struct net_device *dev) |
| 147 | { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | static inline void sfe_ipv6_register_sync_rule_callback(sfe_sync_rule_callback_t callback) |
| 152 | { |
| 153 | return; |
| 154 | } |
| 155 | |
Ratheesh Kannoth | 89302a7 | 2021-10-20 08:10:37 +0530 | [diff] [blame^] | 156 | static inline void sfe_ipv6_update_rule(struct sfe_ipv6_rule_create_msg *msg) |
Xiaoping Fan | 978b377 | 2015-05-27 14:15:18 -0700 | [diff] [blame] | 157 | { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | static inline void sfe_ipv6_mark_rule(struct sfe_connection_mark *mark) |
| 162 | { |
| 163 | return; |
| 164 | } |
| 165 | #endif |
Xiaoping Fan | 9b6bb33 | 2016-04-05 19:21:26 -0700 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * sfe_ipv6_addr_equal() |
| 169 | * compare ipv6 address |
| 170 | * |
| 171 | * return: 1, equal; 0, no equal |
| 172 | */ |
| 173 | static 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 Fan | 6a1672f | 2016-08-17 19:58:12 -0700 | [diff] [blame] | 188 | #define sfe_ipv4_addr_equal(a, b) ((u32)(a) == (u32)(b)) |
Xiaoping Fan | 9b6bb33 | 2016-04-05 19:21:26 -0700 | [diff] [blame] | 189 | |
| 190 | /* |
| 191 | * sfe_addr_equal() |
| 192 | * compare ipv4 or ipv6 address |
| 193 | * |
| 194 | * return: 1, equal; 0, no equal |
| 195 | */ |
| 196 | static 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 Kannoth | 24fb1db | 2021-10-20 07:28:06 +0530 | [diff] [blame] | 201 | |
| 202 | int sfe_init_if(void); |
| 203 | void sfe_exit_if(void); |