blob: 7f3e1cb5f11f4cb148f9b0c17b94db708ec49254 [file] [log] [blame]
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +05301/*
2 * sfe_ipv6.h
3 * Shortcut forwarding engine header file for IPv6.
4 *
5 * Copyright (c) 2015-2016, 2019-2020, 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 *
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
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 */
20
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053021#define CHAR_DEV_MSG_SIZE 768
22
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053023#define SFE_IPV6_DSCP_MASK 0xf03f
24#define SFE_IPV6_DSCP_SHIFT 2
25
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053026#define SFE_IPV6_FRAG_OFFSET 0xfff8
27
28/*
29 * generic IPv6 extension header
30 */
31struct sfe_ipv6_ext_hdr {
32 __u8 next_hdr;
33 __u8 hdr_len;
34 __u8 padding[6];
Ratheesh Kannoth741f7992021-10-20 07:39:52 +053035};
Ratheesh Kannoth24fb1db2021-10-20 07:28:06 +053036
37/*
38 * Specifies the lower bound on ACK numbers carried in the TCP header
39 */
40#define SFE_IPV6_TCP_MAX_ACK_WINDOW 65520
41
42/*
43 * IPv6 TCP connection match additional data.
44 */
45struct sfe_ipv6_tcp_connection_match {
46 u8 win_scale; /* Window scale */
47 u32 max_win; /* Maximum window size seen */
48 u32 end; /* Sequence number of the next byte to send (seq + segment length) */
49 u32 max_end; /* Sequence number of the last byte to ack */
50};
51
52/*
53 * Bit flags for IPv6 connection matching entry.
54 */
55#define SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_SRC (1<<0)
56 /* Perform source translation */
57#define SFE_IPV6_CONNECTION_MATCH_FLAG_XLATE_DEST (1<<1)
58 /* Perform destination translation */
59#define SFE_IPV6_CONNECTION_MATCH_FLAG_NO_SEQ_CHECK (1<<2)
60 /* Ignore TCP sequence numbers */
61#define SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_FAST_ETH_HDR (1<<3)
62 /* Fast Ethernet header write */
63#define SFE_IPV6_CONNECTION_MATCH_FLAG_WRITE_L2_HDR (1<<4)
64 /* Fast Ethernet header write */
65#define SFE_IPV6_CONNECTION_MATCH_FLAG_PRIORITY_REMARK (1<<5)
66 /* remark priority of SKB */
67#define SFE_IPV6_CONNECTION_MATCH_FLAG_DSCP_REMARK (1<<6)
68 /* remark DSCP of packet */
69
70/*
71 * IPv6 connection matching structure.
72 */
73struct sfe_ipv6_connection_match {
74 /*
75 * References to other objects.
76 */
77 struct sfe_ipv6_connection_match *next;
78 struct sfe_ipv6_connection_match *prev;
79 struct sfe_ipv6_connection *connection;
80 struct sfe_ipv6_connection_match *counter_match;
81 /* Matches the flow in the opposite direction as the one in connection */
82 struct sfe_ipv6_connection_match *active_next;
83 struct sfe_ipv6_connection_match *active_prev;
84 bool active; /* Flag to indicate if we're on the active list */
85
86 /*
87 * Characteristics that identify flows that match this rule.
88 */
89 struct net_device *match_dev; /* Network device */
90 u8 match_protocol; /* Protocol */
91 struct sfe_ipv6_addr match_src_ip[1]; /* Source IP address */
92 struct sfe_ipv6_addr match_dest_ip[1]; /* Destination IP address */
93 __be16 match_src_port; /* Source port/connection ident */
94 __be16 match_dest_port; /* Destination port/connection ident */
95
96 /*
97 * Control the operations of the match.
98 */
99 u32 flags; /* Bit flags */
100#ifdef CONFIG_NF_FLOW_COOKIE
101 u32 flow_cookie; /* used flow cookie, for debug */
102#endif
103#ifdef CONFIG_XFRM
104 u32 flow_accel; /* The flow accelerated or not */
105#endif
106
107 /*
108 * Connection state that we track once we match.
109 */
110 union { /* Protocol-specific state */
111 struct sfe_ipv6_tcp_connection_match tcp;
112 } protocol_state;
113 /*
114 * Stats recorded in a sync period. These stats will be added to
115 * rx_packet_count64/rx_byte_count64 after a sync period.
116 */
117 u32 rx_packet_count;
118 u32 rx_byte_count;
119
120 /*
121 * Packet translation information.
122 */
123 struct sfe_ipv6_addr xlate_src_ip[1]; /* Address after source translation */
124 __be16 xlate_src_port; /* Port/connection ident after source translation */
125 u16 xlate_src_csum_adjustment;
126 /* Transport layer checksum adjustment after source translation */
127 struct sfe_ipv6_addr xlate_dest_ip[1]; /* Address after destination translation */
128 __be16 xlate_dest_port; /* Port/connection ident after destination translation */
129 u16 xlate_dest_csum_adjustment;
130 /* Transport layer checksum adjustment after destination translation */
131
132 /*
133 * QoS information
134 */
135 u32 priority;
136 u32 dscp;
137
138 /*
139 * Packet transmit information.
140 */
141 struct net_device *xmit_dev; /* Network device on which to transmit */
142 unsigned short int xmit_dev_mtu;
143 /* Interface MTU */
144 u16 xmit_dest_mac[ETH_ALEN / 2];
145 /* Destination MAC address to use when forwarding */
146 u16 xmit_src_mac[ETH_ALEN / 2];
147 /* Source MAC address to use when forwarding */
148
149 /*
150 * Summary stats.
151 */
152 u64 rx_packet_count64;
153 u64 rx_byte_count64;
154};
155
156/*
157 * Per-connection data structure.
158 */
159struct sfe_ipv6_connection {
160 struct sfe_ipv6_connection *next;
161 /* Pointer to the next entry in a hash chain */
162 struct sfe_ipv6_connection *prev;
163 /* Pointer to the previous entry in a hash chain */
164 int protocol; /* IP protocol number */
165 struct sfe_ipv6_addr src_ip[1]; /* Src IP addr pre-translation */
166 struct sfe_ipv6_addr src_ip_xlate[1]; /* Src IP addr post-translation */
167 struct sfe_ipv6_addr dest_ip[1]; /* Dest IP addr pre-translation */
168 struct sfe_ipv6_addr dest_ip_xlate[1]; /* Dest IP addr post-translation */
169 __be16 src_port; /* Src port pre-translation */
170 __be16 src_port_xlate; /* Src port post-translation */
171 __be16 dest_port; /* Dest port pre-translation */
172 __be16 dest_port_xlate; /* Dest port post-translation */
173 struct sfe_ipv6_connection_match *original_match;
174 /* Original direction matching structure */
175 struct net_device *original_dev;
176 /* Original direction source device */
177 struct sfe_ipv6_connection_match *reply_match;
178 /* Reply direction matching structure */
179 struct net_device *reply_dev; /* Reply direction source device */
180 u64 last_sync_jiffies; /* Jiffies count for the last sync */
181 struct sfe_ipv6_connection *all_connections_next;
182 /* Pointer to the next entry in the list of all connections */
183 struct sfe_ipv6_connection *all_connections_prev;
184 /* Pointer to the previous entry in the list of all connections */
185 u32 mark; /* mark for outgoing packet */
186 u32 debug_read_seq; /* sequence number for debug dump */
187};
188
189/*
190 * IPv6 connections and hash table size information.
191 */
192#define SFE_IPV6_CONNECTION_HASH_SHIFT 12
193#define SFE_IPV6_CONNECTION_HASH_SIZE (1 << SFE_IPV6_CONNECTION_HASH_SHIFT)
194#define SFE_IPV6_CONNECTION_HASH_MASK (SFE_IPV6_CONNECTION_HASH_SIZE - 1)
195
196enum sfe_ipv6_exception_events {
197 SFE_IPV6_EXCEPTION_EVENT_UDP_HEADER_INCOMPLETE,
198 SFE_IPV6_EXCEPTION_EVENT_UDP_NO_CONNECTION,
199 SFE_IPV6_EXCEPTION_EVENT_UDP_IP_OPTIONS_OR_INITIAL_FRAGMENT,
200 SFE_IPV6_EXCEPTION_EVENT_UDP_SMALL_TTL,
201 SFE_IPV6_EXCEPTION_EVENT_UDP_NEEDS_FRAGMENTATION,
202 SFE_IPV6_EXCEPTION_EVENT_TCP_HEADER_INCOMPLETE,
203 SFE_IPV6_EXCEPTION_EVENT_TCP_NO_CONNECTION_SLOW_FLAGS,
204 SFE_IPV6_EXCEPTION_EVENT_TCP_NO_CONNECTION_FAST_FLAGS,
205 SFE_IPV6_EXCEPTION_EVENT_TCP_IP_OPTIONS_OR_INITIAL_FRAGMENT,
206 SFE_IPV6_EXCEPTION_EVENT_TCP_SMALL_TTL,
207 SFE_IPV6_EXCEPTION_EVENT_TCP_NEEDS_FRAGMENTATION,
208 SFE_IPV6_EXCEPTION_EVENT_TCP_FLAGS,
209 SFE_IPV6_EXCEPTION_EVENT_TCP_SEQ_EXCEEDS_RIGHT_EDGE,
210 SFE_IPV6_EXCEPTION_EVENT_TCP_SMALL_DATA_OFFS,
211 SFE_IPV6_EXCEPTION_EVENT_TCP_BAD_SACK,
212 SFE_IPV6_EXCEPTION_EVENT_TCP_BIG_DATA_OFFS,
213 SFE_IPV6_EXCEPTION_EVENT_TCP_SEQ_BEFORE_LEFT_EDGE,
214 SFE_IPV6_EXCEPTION_EVENT_TCP_ACK_EXCEEDS_RIGHT_EDGE,
215 SFE_IPV6_EXCEPTION_EVENT_TCP_ACK_BEFORE_LEFT_EDGE,
216 SFE_IPV6_EXCEPTION_EVENT_ICMP_HEADER_INCOMPLETE,
217 SFE_IPV6_EXCEPTION_EVENT_ICMP_UNHANDLED_TYPE,
218 SFE_IPV6_EXCEPTION_EVENT_ICMP_IPV6_HEADER_INCOMPLETE,
219 SFE_IPV6_EXCEPTION_EVENT_ICMP_IPV6_NON_V6,
220 SFE_IPV6_EXCEPTION_EVENT_ICMP_IPV6_IP_OPTIONS_INCOMPLETE,
221 SFE_IPV6_EXCEPTION_EVENT_ICMP_IPV6_UDP_HEADER_INCOMPLETE,
222 SFE_IPV6_EXCEPTION_EVENT_ICMP_IPV6_TCP_HEADER_INCOMPLETE,
223 SFE_IPV6_EXCEPTION_EVENT_ICMP_IPV6_UNHANDLED_PROTOCOL,
224 SFE_IPV6_EXCEPTION_EVENT_ICMP_NO_CONNECTION,
225 SFE_IPV6_EXCEPTION_EVENT_ICMP_FLUSHED_CONNECTION,
226 SFE_IPV6_EXCEPTION_EVENT_HEADER_INCOMPLETE,
227 SFE_IPV6_EXCEPTION_EVENT_BAD_TOTAL_LENGTH,
228 SFE_IPV6_EXCEPTION_EVENT_NON_V6,
229 SFE_IPV6_EXCEPTION_EVENT_NON_INITIAL_FRAGMENT,
230 SFE_IPV6_EXCEPTION_EVENT_DATAGRAM_INCOMPLETE,
231 SFE_IPV6_EXCEPTION_EVENT_IP_OPTIONS_INCOMPLETE,
232 SFE_IPV6_EXCEPTION_EVENT_UNHANDLED_PROTOCOL,
233 SFE_IPV6_EXCEPTION_EVENT_FLOW_COOKIE_ADD_FAIL,
234 SFE_IPV6_EXCEPTION_EVENT_LAST
235};
236
237/*
238 * Per-module structure.
239 */
240struct sfe_ipv6 {
241 spinlock_t lock; /* Lock for SMP correctness */
242 struct sfe_ipv6_connection_match *active_head;
243 /* Head of the list of recently active connections */
244 struct sfe_ipv6_connection_match *active_tail;
245 /* Tail of the list of recently active connections */
246 struct sfe_ipv6_connection *all_connections_head;
247 /* Head of the list of all connections */
248 struct sfe_ipv6_connection *all_connections_tail;
249 /* Tail of the list of all connections */
250 unsigned int num_connections; /* Number of connections */
251 struct timer_list timer; /* Timer used for periodic sync ops */
252 sfe_sync_rule_callback_t __rcu sync_rule_callback;
253 /* Callback function registered by a connection manager for stats syncing */
254 struct sfe_ipv6_connection *conn_hash[SFE_IPV6_CONNECTION_HASH_SIZE];
255 /* Connection hash table */
256 struct sfe_ipv6_connection_match *conn_match_hash[SFE_IPV6_CONNECTION_HASH_SIZE];
257 /* Connection match hash table */
258#ifdef CONFIG_NF_FLOW_COOKIE
259 struct sfe_ipv6_flow_cookie_entry sfe_flow_cookie_table[SFE_FLOW_COOKIE_SIZE];
260 /* flow cookie table*/
261 sfe_ipv6_flow_cookie_set_func_t flow_cookie_set_func;
262 /* function used to configure flow cookie in hardware*/
263 int flow_cookie_enable;
264 /* Enable/disable flow cookie at runtime */
265#endif
266
267 /*
268 * Stats recorded in a sync period. These stats will be added to
269 * connection_xxx64 after a sync period.
270 */
271 u32 connection_create_requests;
272 /* Number of IPv6 connection create requests */
273 u32 connection_create_collisions;
274 /* Number of IPv6 connection create requests that collided with existing hash table entries */
275 u32 connection_destroy_requests;
276 /* Number of IPv6 connection destroy requests */
277 u32 connection_destroy_misses;
278 /* Number of IPv6 connection destroy requests that missed our hash table */
279 u32 connection_match_hash_hits;
280 /* Number of IPv6 connection match hash hits */
281 u32 connection_match_hash_reorders;
282 /* Number of IPv6 connection match hash reorders */
283 u32 connection_flushes; /* Number of IPv6 connection flushes */
284 u32 packets_forwarded; /* Number of IPv6 packets forwarded */
285 u32 packets_not_forwarded; /* Number of IPv6 packets not forwarded */
286 u32 exception_events[SFE_IPV6_EXCEPTION_EVENT_LAST];
287
288 /*
289 * Summary statistics.
290 */
291 u64 connection_create_requests64;
292 /* Number of IPv6 connection create requests */
293 u64 connection_create_collisions64;
294 /* Number of IPv6 connection create requests that collided with existing hash table entries */
295 u64 connection_destroy_requests64;
296 /* Number of IPv6 connection destroy requests */
297 u64 connection_destroy_misses64;
298 /* Number of IPv6 connection destroy requests that missed our hash table */
299 u64 connection_match_hash_hits64;
300 /* Number of IPv6 connection match hash hits */
301 u64 connection_match_hash_reorders64;
302 /* Number of IPv6 connection match hash reorders */
303 u64 connection_flushes64; /* Number of IPv6 connection flushes */
304 u64 packets_forwarded64; /* Number of IPv6 packets forwarded */
305 u64 packets_not_forwarded64;
306 /* Number of IPv6 packets not forwarded */
307 u64 exception_events64[SFE_IPV6_EXCEPTION_EVENT_LAST];
308
309 /*
310 * Control state.
311 */
312 struct kobject *sys_sfe_ipv6; /* sysfs linkage */
313 int debug_dev; /* Major number of the debug char device */
314 u32 debug_read_seq; /* sequence number for debug dump */
315};
316
317/*
318 * Enumeration of the XML output.
319 */
320enum sfe_ipv6_debug_xml_states {
321 SFE_IPV6_DEBUG_XML_STATE_START,
322 SFE_IPV6_DEBUG_XML_STATE_CONNECTIONS_START,
323 SFE_IPV6_DEBUG_XML_STATE_CONNECTIONS_CONNECTION,
324 SFE_IPV6_DEBUG_XML_STATE_CONNECTIONS_END,
325 SFE_IPV6_DEBUG_XML_STATE_EXCEPTIONS_START,
326 SFE_IPV6_DEBUG_XML_STATE_EXCEPTIONS_EXCEPTION,
327 SFE_IPV6_DEBUG_XML_STATE_EXCEPTIONS_END,
328 SFE_IPV6_DEBUG_XML_STATE_STATS,
329 SFE_IPV6_DEBUG_XML_STATE_END,
330 SFE_IPV6_DEBUG_XML_STATE_DONE
331};
332
333/*
334 * XML write state.
335 */
336struct sfe_ipv6_debug_xml_write_state {
337 enum sfe_ipv6_debug_xml_states state;
338 /* XML output file state machine state */
339 int iter_exception; /* Next exception iterator */
340};
341
342typedef bool (*sfe_ipv6_debug_xml_write_method_t)(struct sfe_ipv6 *si, char *buffer, char *msg, size_t *length,
343 int *total_read, struct sfe_ipv6_debug_xml_write_state *ws);
344
345void sfe_ipv6_exit(void);
346int sfe_ipv6_init(void);