]> git.proxmox.com Git - mirror_ovs.git/blob - datapath-windows/ovsext/Conntrack.h
datapath-windows: Fix static analysis warnings in OvsGetTcpPayloadLength
[mirror_ovs.git] / datapath-windows / ovsext / Conntrack.h
1 /*
2 * Copyright (c) 2015, 2016 VMware, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef __OVS_CONNTRACK_H_
18 #define __OVS_CONNTRACK_H_ 1
19
20 #include "precomp.h"
21 #include "Actions.h"
22 #include "Debug.h"
23 #include "Flow.h"
24 #include "Actions.h"
25 #include <stddef.h>
26
27 #ifdef OVS_DBG_MOD
28 #undef OVS_DBG_MOD
29 #endif
30 #define OVS_DBG_MOD OVS_DBG_CONTRK
31
32 struct ct_addr {
33 union {
34 ovs_be32 ipv4;
35 struct in6_addr ipv6;
36 uint32_t ipv4_aligned;
37 struct in6_addr ipv6_aligned;
38 };
39 };
40
41 struct ct_endpoint {
42 struct ct_addr addr;
43 union {
44 struct {
45 ovs_be16 port;
46 uint16 pad_port;
47 };
48 struct {
49 ovs_be16 icmp_id;
50 uint8_t icmp_type;
51 uint8_t icmp_code;
52 };
53 };
54 };
55
56 typedef enum CT_UPDATE_RES {
57 CT_UPDATE_INVALID,
58 CT_UPDATE_VALID,
59 CT_UPDATE_NEW,
60 } CT_UPDATE_RES;
61
62 /* Metadata mark for masked write to conntrack mark */
63 typedef struct MD_MARK {
64 UINT32 value;
65 UINT32 mask;
66 } MD_MARK;
67
68 /* Metadata label for masked write to conntrack label. */
69 typedef struct MD_LABELS {
70 struct ovs_key_ct_labels value;
71 struct ovs_key_ct_labels mask;
72 } MD_LABELS;
73
74 typedef enum _NAT_ACTION {
75 NAT_ACTION_NONE = 0,
76 NAT_ACTION_REVERSE = 1 << 0,
77 NAT_ACTION_SRC = 1 << 1,
78 NAT_ACTION_SRC_PORT = 1 << 2,
79 NAT_ACTION_DST = 1 << 3,
80 NAT_ACTION_DST_PORT = 1 << 4,
81 } NAT_ACTION;
82
83 typedef struct _OVS_CT_KEY {
84 struct ct_endpoint src;
85 struct ct_endpoint dst;
86 UINT16 dl_type;
87 UINT8 nw_proto;
88 UINT16 zone;
89 UINT64 packetCount;
90 UINT64 byteCount;
91 } OVS_CT_KEY, *POVS_CT_KEY;
92
93 typedef struct _NAT_ACTION_INFO {
94 struct ct_addr minAddr;
95 struct ct_addr maxAddr;
96 uint16_t minPort;
97 uint16_t maxPort;
98 uint16_t natAction;
99 } NAT_ACTION_INFO, *PNAT_ACTION_INFO;
100
101 typedef struct OVS_CT_ENTRY {
102 OVS_CT_KEY key;
103 OVS_CT_KEY rev_key;
104 UINT64 expiration;
105 LIST_ENTRY link;
106 UINT32 mark;
107 UINT64 timestampStart;
108 struct ovs_key_ct_labels labels;
109 NAT_ACTION_INFO natInfo;
110 PVOID parent; /* Points to main connection */
111 } OVS_CT_ENTRY, *POVS_CT_ENTRY;
112
113 typedef struct OVS_CT_REL_ENTRY {
114 OVS_CT_KEY key;
115 POVS_CT_ENTRY parent;
116 UINT64 expiration;
117 LIST_ENTRY link;
118 } OVS_CT_REL_ENTRY, *POVS_CT_REL_ENTRY;
119
120 typedef struct _OVS_CT_THREAD_CTX {
121 KEVENT event;
122 PVOID threadObject;
123 UINT32 exit;
124 } OVS_CT_THREAD_CTX, *POVS_CT_THREAD_CTX;
125
126 typedef struct OvsConntrackKeyLookupCtx {
127 OVS_CT_KEY key;
128 POVS_CT_ENTRY entry;
129 UINT32 hash;
130 BOOLEAN reply;
131 BOOLEAN related;
132 } OvsConntrackKeyLookupCtx;
133
134 #define CT_MAX_ENTRIES 1 << 21
135 #define CT_HASH_TABLE_SIZE ((UINT32)1 << 10)
136 #define CT_HASH_TABLE_MASK (CT_HASH_TABLE_SIZE - 1)
137 #define CT_INTERVAL_SEC 10000000LL //1s
138 #define CT_ENTRY_TIMEOUT (2 * 60 * CT_INTERVAL_SEC) // 2m
139 #define CT_CLEANUP_INTERVAL (2 * 60 * CT_INTERVAL_SEC) // 2m
140
141
142 /* Given POINTER, the address of the given MEMBER in a STRUCT object, returns
143 the STRUCT object. */
144 #define CONTAINER_OF(POINTER, STRUCT, MEMBER) \
145 ((STRUCT *) (void *) ((char *) (POINTER) - \
146 offsetof (STRUCT, MEMBER)))
147
148 static __inline void
149 OvsConntrackUpdateExpiration(OVS_CT_ENTRY *ctEntry,
150 long long now,
151 long long interval)
152 {
153 ctEntry->expiration = now + interval;
154 }
155
156 static __inline UINT32
157 OvsGetTcpPayloadLength(PNET_BUFFER_LIST nbl)
158 {
159 IPHdr *ipHdr;
160 TCPHdr *tcp;
161 char *ipBuf[sizeof(EthHdr) + sizeof(IPHdr) + sizeof(TCPHdr)];
162
163 ipHdr = NdisGetDataBuffer(NET_BUFFER_LIST_FIRST_NB(nbl), sizeof *ipBuf,
164 (PVOID)&ipBuf, 1 /*no align*/, 0);
165 if (ipHdr == NULL) {
166 return 0;
167 }
168
169 ipHdr = (IPHdr *)((PCHAR)ipHdr + sizeof(EthHdr));
170 tcp = (TCPHdr *)((PCHAR)ipHdr + ipHdr->ihl * 4);
171
172 return (ntohs(ipHdr->tot_len) - (ipHdr->ihl * 4) - (TCP_HDR_LEN(tcp)));
173 }
174
175 VOID OvsCleanupConntrack(VOID);
176 NTSTATUS OvsInitConntrack(POVS_SWITCH_CONTEXT context);
177
178 NDIS_STATUS OvsExecuteConntrackAction(OvsForwardingContext *fwdCtx,
179 OvsFlowKey *key,
180 const PNL_ATTR a);
181 BOOLEAN OvsConntrackValidateTcpPacket(const TCPHdr *tcp);
182 BOOLEAN OvsConntrackValidateIcmpPacket(const ICMPHdr *icmp);
183 OVS_CT_ENTRY * OvsConntrackCreateTcpEntry(const TCPHdr *tcp,
184 PNET_BUFFER_LIST nbl,
185 UINT64 now);
186 NDIS_STATUS OvsCtMapTcpProtoInfoToNl(PNL_BUFFER nlBuf,
187 OVS_CT_ENTRY *conn_);
188 OVS_CT_ENTRY * OvsConntrackCreateOtherEntry(UINT64 now);
189 OVS_CT_ENTRY * OvsConntrackCreateIcmpEntry(UINT64 now);
190 enum CT_UPDATE_RES OvsConntrackUpdateTcpEntry(OVS_CT_ENTRY* conn_,
191 const TCPHdr *tcp,
192 PNET_BUFFER_LIST nbl,
193 BOOLEAN reply,
194 UINT64 now);
195 enum CT_UPDATE_RES OvsConntrackUpdateOtherEntry(OVS_CT_ENTRY *conn_,
196 BOOLEAN reply,
197 UINT64 now);
198 enum CT_UPDATE_RES OvsConntrackUpdateIcmpEntry(OVS_CT_ENTRY* conn_,
199 BOOLEAN reply,
200 UINT64 now);
201 NTSTATUS OvsCreateNlMsgFromCtEntry(POVS_CT_ENTRY entry,
202 PVOID outBuffer,
203 UINT32 outBufLen,
204 UINT8 eventType,
205 UINT32 nlmsgSeq,
206 UINT32 nlmsgPid,
207 UINT8 nfGenVersion,
208 UINT32 dpIfIndex);
209
210 /* Tracking related connections */
211 NTSTATUS OvsInitCtRelated(POVS_SWITCH_CONTEXT context);
212 VOID OvsCleanupCtRelated(VOID);
213 NDIS_STATUS OvsCtRelatedEntryCreate(UINT8 ipProto,
214 UINT16 dl_type,
215 UINT32 serverIp,
216 UINT32 clientIp,
217 UINT16 serverPort,
218 UINT16 clientPort,
219 UINT64 currentTime,
220 POVS_CT_ENTRY parent);
221 POVS_CT_ENTRY OvsCtRelatedLookup(OVS_CT_KEY key, UINT64 currentTime);
222
223 NDIS_STATUS OvsCtHandleFtp(PNET_BUFFER_LIST curNbl,
224 OvsFlowKey *key,
225 OVS_PACKET_HDR_INFO *layers,
226 UINT64 currentTime,
227 POVS_CT_ENTRY entry,
228 BOOLEAN request);
229
230 UINT32 OvsHashCtKey(const OVS_CT_KEY *key);
231 BOOLEAN OvsCtKeyAreSame(OVS_CT_KEY ctxKey, OVS_CT_KEY entryKey);
232 POVS_CT_ENTRY OvsCtLookup(OvsConntrackKeyLookupCtx *ctx);
233
234
235 #endif /* __OVS_CONNTRACK_H_ */