]>
Commit | Line | Data |
---|---|---|
a489b168 | 1 | /* |
bd5e81a0 | 2 | * Copyright (c) 2015, 2016, 2017 Nicira, Inc. |
a489b168 DDP |
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 | #include <config.h> | |
bd5e81a0 | 18 | #include <ctype.h> |
a489b168 | 19 | #include <errno.h> |
ff6aa424 | 20 | #include <sys/types.h> |
a489b168 DDP |
21 | #include <netinet/in.h> |
22 | #include <netinet/icmp6.h> | |
bd5e81a0 | 23 | #include <string.h> |
a489b168 DDP |
24 | |
25 | #include "bitmap.h" | |
bd5e81a0 | 26 | #include "conntrack.h" |
a489b168 DDP |
27 | #include "conntrack-private.h" |
28 | #include "coverage.h" | |
29 | #include "csum.h" | |
4d4e68ed | 30 | #include "ct-dpif.h" |
a489b168 DDP |
31 | #include "dp-packet.h" |
32 | #include "flow.h" | |
33 | #include "netdev.h" | |
34 | #include "odp-netlink.h" | |
35 | #include "openvswitch/hmap.h" | |
36 | #include "openvswitch/vlog.h" | |
37 | #include "ovs-rcu.h" | |
e6ef6cc6 | 38 | #include "ovs-thread.h" |
fd016ae3 | 39 | #include "openvswitch/poll-loop.h" |
a489b168 DDP |
40 | #include "random.h" |
41 | #include "timeval.h" | |
42 | ||
43 | VLOG_DEFINE_THIS_MODULE(conntrack); | |
44 | ||
45 | COVERAGE_DEFINE(conntrack_full); | |
e6ef6cc6 | 46 | COVERAGE_DEFINE(conntrack_long_cleanup); |
a489b168 DDP |
47 | |
48 | struct conn_lookup_ctx { | |
49 | struct conn_key key; | |
50 | struct conn *conn; | |
51 | uint32_t hash; | |
52 | bool reply; | |
dbb597d3 | 53 | bool icmp_related; |
a489b168 DDP |
54 | }; |
55 | ||
bd5e81a0 DB |
56 | enum ftp_ctl_pkt { |
57 | /* Control packets with address and/or port specifiers. */ | |
58 | CT_FTP_CTL_INTEREST, | |
59 | /* Control packets without address and/or port specifiers. */ | |
60 | CT_FTP_CTL_OTHER, | |
61 | CT_FTP_CTL_INVALID, | |
62 | }; | |
63 | ||
64 | enum ct_alg_mode { | |
65 | CT_FTP_MODE_ACTIVE, | |
66 | CT_FTP_MODE_PASSIVE, | |
7be77cb0 | 67 | CT_TFTP_MODE, |
bd5e81a0 DB |
68 | }; |
69 | ||
94e71143 DB |
70 | enum ct_alg_ctl_type { |
71 | CT_ALG_CTL_NONE, | |
72 | CT_ALG_CTL_FTP, | |
73 | CT_ALG_CTL_TFTP, | |
be38342d DB |
74 | /* SIP is not enabled through Openflow and presently only used as |
75 | * an example of an alg that allows a wildcard src ip. */ | |
76 | CT_ALG_CTL_SIP, | |
94e71143 DB |
77 | }; |
78 | ||
a489b168 | 79 | static bool conn_key_extract(struct conntrack *, struct dp_packet *, |
66e4ad8a DDP |
80 | ovs_be16 dl_type, struct conn_lookup_ctx *, |
81 | uint16_t zone); | |
a489b168 DDP |
82 | static uint32_t conn_key_hash(const struct conn_key *, uint32_t basis); |
83 | static void conn_key_reverse(struct conn_key *); | |
84 | static void conn_key_lookup(struct conntrack_bucket *ctb, | |
85 | struct conn_lookup_ctx *ctx, | |
86 | long long now); | |
87 | static bool valid_new(struct dp_packet *pkt, struct conn_key *); | |
e6ef6cc6 DDP |
88 | static struct conn *new_conn(struct conntrack_bucket *, struct dp_packet *pkt, |
89 | struct conn_key *, long long now); | |
a489b168 | 90 | static void delete_conn(struct conn *); |
e6ef6cc6 DDP |
91 | static enum ct_update_res conn_update(struct conn *, |
92 | struct conntrack_bucket *ctb, | |
93 | struct dp_packet *, bool reply, | |
94 | long long now); | |
a489b168 DDP |
95 | static bool conn_expired(struct conn *, long long now); |
96 | static void set_mark(struct dp_packet *, struct conn *, | |
97 | uint32_t val, uint32_t mask); | |
98 | static void set_label(struct dp_packet *, struct conn *, | |
99 | const struct ovs_key_ct_labels *val, | |
100 | const struct ovs_key_ct_labels *mask); | |
e6ef6cc6 | 101 | static void *clean_thread_main(void *f_); |
a489b168 | 102 | |
286de272 DB |
103 | static struct nat_conn_key_node * |
104 | nat_conn_keys_lookup(struct hmap *nat_conn_keys, | |
105 | const struct conn_key *key, | |
106 | uint32_t basis); | |
107 | ||
80cee116 DB |
108 | static bool |
109 | nat_conn_keys_insert(struct hmap *nat_conn_keys, | |
110 | const struct conn *nat_conn, | |
111 | uint32_t hash_basis); | |
112 | ||
286de272 DB |
113 | static void |
114 | nat_conn_keys_remove(struct hmap *nat_conn_keys, | |
115 | const struct conn_key *key, | |
116 | uint32_t basis); | |
117 | ||
118 | static bool | |
119 | nat_select_range_tuple(struct conntrack *ct, const struct conn *conn, | |
120 | struct conn *nat_conn); | |
121 | ||
122 | static uint8_t | |
123 | reverse_icmp_type(uint8_t type); | |
124 | static uint8_t | |
125 | reverse_icmp6_type(uint8_t type); | |
126 | static inline bool | |
127 | extract_l3_ipv4(struct conn_key *key, const void *data, size_t size, | |
128 | const char **new_data, bool validate_checksum); | |
129 | static inline bool | |
130 | extract_l3_ipv6(struct conn_key *key, const void *data, size_t size, | |
131 | const char **new_data); | |
132 | ||
bd5e81a0 | 133 | static struct alg_exp_node * |
be38342d DB |
134 | expectation_lookup(struct hmap *alg_expectations, const struct conn_key *key, |
135 | uint32_t basis, bool src_ip_wc); | |
bd5e81a0 DB |
136 | |
137 | static int | |
138 | repl_ftp_v4_addr(struct dp_packet *pkt, ovs_be32 v4_addr_rep, | |
139 | char *ftp_data_v4_start, | |
140 | size_t addr_offset_from_ftp_data_start); | |
141 | ||
142 | static enum ftp_ctl_pkt | |
143 | process_ftp_ctl_v4(struct conntrack *ct, | |
144 | struct dp_packet *pkt, | |
145 | const struct conn *conn_for_expectation, | |
4417ca3d | 146 | ovs_be32 *v4_addr_rep, |
bd5e81a0 DB |
147 | char **ftp_data_v4_start, |
148 | size_t *addr_offset_from_ftp_data_start); | |
149 | ||
150 | static enum ftp_ctl_pkt | |
151 | detect_ftp_ctl_type(const struct conn_lookup_ctx *ctx, | |
152 | struct dp_packet *pkt); | |
153 | ||
4417ca3d DB |
154 | static void |
155 | expectation_clean(struct conntrack *ct, const struct conn_key *master_key, | |
156 | uint32_t basis); | |
157 | ||
94e71143 DB |
158 | static struct ct_l4_proto *l4_protos[] = { |
159 | [IPPROTO_TCP] = &ct_proto_tcp, | |
160 | [IPPROTO_UDP] = &ct_proto_other, | |
161 | [IPPROTO_ICMP] = &ct_proto_icmp4, | |
162 | [IPPROTO_ICMPV6] = &ct_proto_icmp6, | |
163 | }; | |
164 | ||
bd5e81a0 DB |
165 | static void |
166 | handle_ftp_ctl(struct conntrack *ct, const struct conn_lookup_ctx *ctx, | |
167 | struct dp_packet *pkt, | |
168 | const struct conn *conn_for_expectation, | |
169 | long long now, enum ftp_ctl_pkt ftp_ctl, bool nat); | |
170 | ||
7be77cb0 DB |
171 | static void |
172 | handle_tftp_ctl(struct conntrack *ct, | |
94e71143 | 173 | const struct conn_lookup_ctx *ctx OVS_UNUSED, |
be38342d | 174 | struct dp_packet *pkt, |
7be77cb0 | 175 | const struct conn *conn_for_expectation, |
4417ca3d DB |
176 | long long now OVS_UNUSED, |
177 | enum ftp_ctl_pkt ftp_ctl OVS_UNUSED, bool nat OVS_UNUSED); | |
94e71143 DB |
178 | |
179 | typedef void (*alg_helper)(struct conntrack *ct, | |
180 | const struct conn_lookup_ctx *ctx, | |
181 | struct dp_packet *pkt, | |
182 | const struct conn *conn_for_expectation, | |
183 | long long now, enum ftp_ctl_pkt ftp_ctl, | |
184 | bool nat); | |
185 | ||
186 | static alg_helper alg_helpers[] = { | |
187 | [CT_ALG_CTL_NONE] = NULL, | |
188 | [CT_ALG_CTL_FTP] = handle_ftp_ctl, | |
189 | [CT_ALG_CTL_TFTP] = handle_tftp_ctl, | |
a489b168 DDP |
190 | }; |
191 | ||
192 | long long ct_timeout_val[] = { | |
193 | #define CT_TIMEOUT(NAME, VAL) [CT_TM_##NAME] = VAL, | |
194 | CT_TIMEOUTS | |
195 | #undef CT_TIMEOUT | |
196 | }; | |
197 | ||
bd5e81a0 DB |
198 | /* The maximum TCP or UDP port number. */ |
199 | #define CT_MAX_L4_PORT 65535 | |
bd5e81a0 DB |
200 | /* String buffer used for parsing FTP string messages. |
201 | * This is sized about twice what is needed to leave some | |
202 | * margin of error. */ | |
203 | #define LARGEST_FTP_MSG_OF_INTEREST 128 | |
204 | /* FTP port string used in active mode. */ | |
205 | #define FTP_PORT_CMD "PORT" | |
206 | /* FTP pasv string used in passive mode. */ | |
207 | #define FTP_PASV_REPLY_CODE "227" | |
208 | /* Maximum decimal digits for port in FTP command. | |
209 | * The port is represented as two 3 digit numbers with the | |
210 | * high part a multiple of 256. */ | |
211 | #define MAX_FTP_PORT_DGTS 3 | |
212 | ||
213 | /* FTP extension EPRT string used for active mode. */ | |
214 | #define FTP_EPRT_CMD "EPRT" | |
215 | /* FTP extension EPSV string used for passive mode. */ | |
216 | #define FTP_EPSV_REPLY "EXTENDED PASSIVE" | |
217 | /* Maximum decimal digits for port in FTP extended command. */ | |
218 | #define MAX_EXT_FTP_PORT_DGTS 5 | |
219 | /* FTP extended command code for IPv6. */ | |
220 | #define FTP_AF_V6 '2' | |
221 | /* Used to indicate a wildcard L4 source port number for ALGs. | |
222 | * This is used for port numbers that we cannot predict in | |
223 | * expectations. */ | |
224 | #define ALG_WC_SRC_PORT 0 | |
225 | ||
a489b168 | 226 | /* If the total number of connections goes above this value, no new connections |
286de272 | 227 | * are accepted; this is for CT_CONN_TYPE_DEFAULT connections. */ |
a489b168 DDP |
228 | #define DEFAULT_N_CONN_LIMIT 3000000 |
229 | ||
5ed7a0b4 DB |
230 | /* Does a member by member comparison of two conn_keys; this |
231 | * function must be kept in sync with struct conn_key; returns 0 | |
232 | * if the keys are equal or 1 if the keys are not equal. */ | |
233 | static int | |
234 | conn_key_cmp(const struct conn_key *key1, const struct conn_key *key2) | |
235 | { | |
236 | if (!memcmp(&key1->src.addr, &key2->src.addr, sizeof key1->src.addr) && | |
237 | !memcmp(&key1->dst.addr, &key2->dst.addr, sizeof key1->dst.addr) && | |
238 | (key1->src.icmp_id == key2->src.icmp_id) && | |
239 | (key1->src.icmp_type == key2->src.icmp_type) && | |
240 | (key1->src.icmp_code == key2->src.icmp_code) && | |
241 | (key1->dst.icmp_id == key2->dst.icmp_id) && | |
242 | (key1->dst.icmp_type == key2->dst.icmp_type) && | |
243 | (key1->dst.icmp_code == key2->dst.icmp_code) && | |
244 | (key1->dl_type == key2->dl_type) && | |
245 | (key1->zone == key2->zone) && | |
246 | (key1->nw_proto == key2->nw_proto)) { | |
247 | ||
248 | return 0; | |
249 | } | |
250 | return 1; | |
251 | } | |
252 | ||
d8682ee5 | 253 | static void |
dec0dbbc DB |
254 | ct_print_conn_info(const struct conn *c, const char *log_msg, |
255 | enum vlog_level vll, bool force, bool rl_on) | |
66f400f5 DB |
256 | { |
257 | #define CT_VLOG(RL_ON, LEVEL, ...) \ | |
258 | do { \ | |
259 | if (RL_ON) { \ | |
260 | static struct vlog_rate_limit rl_ = VLOG_RATE_LIMIT_INIT(5, 5); \ | |
261 | vlog_rate_limit(&this_module, LEVEL, &rl_, __VA_ARGS__); \ | |
262 | } else { \ | |
263 | vlog(&this_module, LEVEL, __VA_ARGS__); \ | |
264 | } \ | |
265 | } while (0) | |
266 | ||
267 | if (OVS_UNLIKELY(force || vlog_is_enabled(&this_module, vll))) { | |
268 | if (c->key.dl_type == htons(ETH_TYPE_IP)) { | |
269 | CT_VLOG(rl_on, vll, "%s: src ip "IP_FMT" dst ip "IP_FMT" rev src " | |
270 | "ip "IP_FMT" rev dst ip "IP_FMT" src/dst ports " | |
271 | "%"PRIu16"/%"PRIu16" rev src/dst ports " | |
272 | "%"PRIu16"/%"PRIu16" zone/rev zone " | |
273 | "%"PRIu16"/%"PRIu16" nw_proto/rev nw_proto " | |
274 | "%"PRIu8"/%"PRIu8, log_msg, | |
275 | IP_ARGS(c->key.src.addr.ipv4_aligned), | |
276 | IP_ARGS(c->key.dst.addr.ipv4_aligned), | |
277 | IP_ARGS(c->rev_key.src.addr.ipv4_aligned), | |
278 | IP_ARGS(c->rev_key.dst.addr.ipv4_aligned), | |
279 | ntohs(c->key.src.port), ntohs(c->key.dst.port), | |
280 | ntohs(c->rev_key.src.port), ntohs(c->rev_key.dst.port), | |
281 | c->key.zone, c->rev_key.zone, c->key.nw_proto, | |
282 | c->rev_key.nw_proto); | |
283 | } else { | |
284 | char ip6_s[INET6_ADDRSTRLEN]; | |
285 | inet_ntop(AF_INET6, &c->key.src.addr.ipv6, ip6_s, sizeof ip6_s); | |
286 | char ip6_d[INET6_ADDRSTRLEN]; | |
287 | inet_ntop(AF_INET6, &c->key.dst.addr.ipv6, ip6_d, sizeof ip6_d); | |
288 | char ip6_rs[INET6_ADDRSTRLEN]; | |
289 | inet_ntop(AF_INET6, &c->rev_key.src.addr.ipv6, ip6_rs, | |
290 | sizeof ip6_rs); | |
291 | char ip6_rd[INET6_ADDRSTRLEN]; | |
292 | inet_ntop(AF_INET6, &c->rev_key.dst.addr.ipv6, ip6_rd, | |
293 | sizeof ip6_rd); | |
294 | ||
295 | CT_VLOG(rl_on, vll, "%s: src ip %s dst ip %s rev src ip %s" | |
296 | " rev dst ip %s src/dst ports %"PRIu16"/%"PRIu16 | |
297 | " rev src/dst ports %"PRIu16"/%"PRIu16" zone/rev zone " | |
298 | "%"PRIu16"/%"PRIu16" nw_proto/rev nw_proto " | |
299 | "%"PRIu8"/%"PRIu8, log_msg, ip6_s, ip6_d, ip6_rs, | |
300 | ip6_rd, ntohs(c->key.src.port), ntohs(c->key.dst.port), | |
301 | ntohs(c->rev_key.src.port), ntohs(c->rev_key.dst.port), | |
302 | c->key.zone, c->rev_key.zone, c->key.nw_proto, | |
303 | c->rev_key.nw_proto); | |
304 | } | |
305 | } | |
306 | } | |
307 | ||
a489b168 DDP |
308 | /* Initializes the connection tracker 'ct'. The caller is responsible for |
309 | * calling 'conntrack_destroy()', when the instance is not needed anymore */ | |
310 | void | |
311 | conntrack_init(struct conntrack *ct) | |
312 | { | |
e6ef6cc6 | 313 | long long now = time_msec(); |
a489b168 | 314 | |
8b934ced DB |
315 | ct_rwlock_init(&ct->resources_lock); |
316 | ct_rwlock_wrlock(&ct->resources_lock); | |
286de272 | 317 | hmap_init(&ct->nat_conn_keys); |
bd5e81a0 | 318 | hmap_init(&ct->alg_expectations); |
4417ca3d | 319 | hindex_init(&ct->alg_expectation_refs); |
bd5e81a0 | 320 | ovs_list_init(&ct->alg_exp_list); |
8b934ced | 321 | ct_rwlock_unlock(&ct->resources_lock); |
286de272 | 322 | |
dec0dbbc | 323 | for (unsigned i = 0; i < CONNTRACK_BUCKETS; i++) { |
a489b168 DDP |
324 | struct conntrack_bucket *ctb = &ct->buckets[i]; |
325 | ||
326 | ct_lock_init(&ctb->lock); | |
327 | ct_lock_lock(&ctb->lock); | |
328 | hmap_init(&ctb->connections); | |
dec0dbbc | 329 | for (unsigned j = 0; j < ARRAY_SIZE(ctb->exp_lists); j++) { |
e6ef6cc6 DDP |
330 | ovs_list_init(&ctb->exp_lists[j]); |
331 | } | |
a489b168 | 332 | ct_lock_unlock(&ctb->lock); |
e6ef6cc6 DDP |
333 | ovs_mutex_init(&ctb->cleanup_mutex); |
334 | ovs_mutex_lock(&ctb->cleanup_mutex); | |
335 | ctb->next_cleanup = now + CT_TM_MIN; | |
336 | ovs_mutex_unlock(&ctb->cleanup_mutex); | |
a489b168 DDP |
337 | } |
338 | ct->hash_basis = random_uint32(); | |
339 | atomic_count_init(&ct->n_conn, 0); | |
340 | atomic_init(&ct->n_conn_limit, DEFAULT_N_CONN_LIMIT); | |
e6ef6cc6 DDP |
341 | latch_init(&ct->clean_thread_exit); |
342 | ct->clean_thread = ovs_thread_create("ct_clean", clean_thread_main, ct); | |
a489b168 DDP |
343 | } |
344 | ||
345 | /* Destroys the connection tracker 'ct' and frees all the allocated memory. */ | |
346 | void | |
347 | conntrack_destroy(struct conntrack *ct) | |
348 | { | |
e6ef6cc6 DDP |
349 | latch_set(&ct->clean_thread_exit); |
350 | pthread_join(ct->clean_thread, NULL); | |
351 | latch_destroy(&ct->clean_thread_exit); | |
dec0dbbc | 352 | for (unsigned i = 0; i < CONNTRACK_BUCKETS; i++) { |
a489b168 DDP |
353 | struct conntrack_bucket *ctb = &ct->buckets[i]; |
354 | struct conn *conn; | |
355 | ||
e6ef6cc6 | 356 | ovs_mutex_destroy(&ctb->cleanup_mutex); |
a489b168 | 357 | ct_lock_lock(&ctb->lock); |
bd5e81a0 | 358 | HMAP_FOR_EACH_POP (conn, node, &ctb->connections) { |
286de272 DB |
359 | if (conn->conn_type == CT_CONN_TYPE_DEFAULT) { |
360 | atomic_count_dec(&ct->n_conn); | |
361 | } | |
a489b168 DDP |
362 | delete_conn(conn); |
363 | } | |
364 | hmap_destroy(&ctb->connections); | |
365 | ct_lock_unlock(&ctb->lock); | |
366 | ct_lock_destroy(&ctb->lock); | |
367 | } | |
8b934ced | 368 | ct_rwlock_wrlock(&ct->resources_lock); |
286de272 DB |
369 | struct nat_conn_key_node *nat_conn_key_node; |
370 | HMAP_FOR_EACH_POP (nat_conn_key_node, node, &ct->nat_conn_keys) { | |
371 | free(nat_conn_key_node); | |
372 | } | |
373 | hmap_destroy(&ct->nat_conn_keys); | |
bd5e81a0 DB |
374 | |
375 | struct alg_exp_node *alg_exp_node; | |
376 | HMAP_FOR_EACH_POP (alg_exp_node, node, &ct->alg_expectations) { | |
377 | free(alg_exp_node); | |
378 | } | |
4417ca3d | 379 | |
bd5e81a0 DB |
380 | ovs_list_poison(&ct->alg_exp_list); |
381 | hmap_destroy(&ct->alg_expectations); | |
4417ca3d | 382 | hindex_destroy(&ct->alg_expectation_refs); |
8b934ced DB |
383 | ct_rwlock_unlock(&ct->resources_lock); |
384 | ct_rwlock_destroy(&ct->resources_lock); | |
a489b168 DDP |
385 | } |
386 | \f | |
387 | static unsigned hash_to_bucket(uint32_t hash) | |
388 | { | |
389 | /* Extracts the most significant bits in hash. The least significant bits | |
390 | * are already used internally by the hmap implementation. */ | |
391 | BUILD_ASSERT(CONNTRACK_BUCKETS_SHIFT < 32 && CONNTRACK_BUCKETS_SHIFT >= 1); | |
392 | ||
393 | return (hash >> (32 - CONNTRACK_BUCKETS_SHIFT)) % CONNTRACK_BUCKETS; | |
394 | } | |
395 | ||
396 | static void | |
286de272 | 397 | write_ct_md(struct dp_packet *pkt, uint16_t zone, const struct conn *conn, |
bd5e81a0 | 398 | const struct conn_key *key, const struct alg_exp_node *alg_exp) |
a489b168 | 399 | { |
286de272 | 400 | pkt->md.ct_state |= CS_TRACKED; |
a489b168 | 401 | pkt->md.ct_zone = zone; |
daf4d3c1 JR |
402 | pkt->md.ct_mark = conn ? conn->mark : 0; |
403 | pkt->md.ct_label = conn ? conn->label : OVS_U128_ZERO; | |
404 | ||
405 | /* Use the original direction tuple if we have it. */ | |
406 | if (conn) { | |
bd5e81a0 DB |
407 | if (conn->alg_related) { |
408 | key = &conn->master_key; | |
409 | } else { | |
410 | key = &conn->key; | |
411 | } | |
412 | } else if (alg_exp) { | |
413 | pkt->md.ct_mark = alg_exp->master_mark; | |
414 | pkt->md.ct_label = alg_exp->master_label; | |
415 | key = &alg_exp->master_key; | |
daf4d3c1 | 416 | } |
dec0dbbc | 417 | |
daf4d3c1 | 418 | pkt->md.ct_orig_tuple_ipv6 = false; |
dec0dbbc | 419 | |
daf4d3c1 JR |
420 | if (key) { |
421 | if (key->dl_type == htons(ETH_TYPE_IP)) { | |
422 | pkt->md.ct_orig_tuple.ipv4 = (struct ovs_key_ct_tuple_ipv4) { | |
423 | key->src.addr.ipv4_aligned, | |
424 | key->dst.addr.ipv4_aligned, | |
425 | key->nw_proto != IPPROTO_ICMP | |
426 | ? key->src.port : htons(key->src.icmp_type), | |
427 | key->nw_proto != IPPROTO_ICMP | |
428 | ? key->dst.port : htons(key->src.icmp_code), | |
429 | key->nw_proto, | |
430 | }; | |
286de272 | 431 | } else { |
daf4d3c1 JR |
432 | pkt->md.ct_orig_tuple_ipv6 = true; |
433 | pkt->md.ct_orig_tuple.ipv6 = (struct ovs_key_ct_tuple_ipv6) { | |
434 | key->src.addr.ipv6_aligned, | |
435 | key->dst.addr.ipv6_aligned, | |
436 | key->nw_proto != IPPROTO_ICMPV6 | |
437 | ? key->src.port : htons(key->src.icmp_type), | |
438 | key->nw_proto != IPPROTO_ICMPV6 | |
439 | ? key->dst.port : htons(key->src.icmp_code), | |
440 | key->nw_proto, | |
441 | }; | |
442 | } | |
443 | } else { | |
444 | memset(&pkt->md.ct_orig_tuple, 0, sizeof pkt->md.ct_orig_tuple); | |
445 | } | |
bd5e81a0 DB |
446 | } |
447 | ||
448 | static uint8_t | |
449 | get_ip_proto(const struct dp_packet *pkt) | |
450 | { | |
451 | uint8_t ip_proto; | |
452 | struct eth_header *l2 = dp_packet_eth(pkt); | |
453 | if (l2->eth_type == htons(ETH_TYPE_IPV6)) { | |
454 | struct ovs_16aligned_ip6_hdr *nh6 = dp_packet_l3(pkt); | |
455 | ip_proto = nh6->ip6_ctlun.ip6_un1.ip6_un1_nxt; | |
456 | } else { | |
457 | struct ip_header *l3_hdr = dp_packet_l3(pkt); | |
458 | ip_proto = l3_hdr->ip_proto; | |
459 | } | |
286de272 | 460 | |
bd5e81a0 DB |
461 | return ip_proto; |
462 | } | |
463 | ||
464 | static bool | |
94e71143 | 465 | is_ftp_ctl(const enum ct_alg_ctl_type ct_alg_ctl) |
bd5e81a0 | 466 | { |
94e71143 | 467 | return ct_alg_ctl == CT_ALG_CTL_FTP; |
bd5e81a0 DB |
468 | } |
469 | ||
94e71143 | 470 | static enum ct_alg_ctl_type |
bd7d93f8 DB |
471 | get_alg_ctl_type(const struct dp_packet *pkt, ovs_be16 tp_src, ovs_be16 tp_dst, |
472 | const char *helper) | |
7be77cb0 | 473 | { |
94e71143 DB |
474 | /* CT_IPPORT_FTP/TFTP is used because IPPORT_FTP/TFTP in not defined |
475 | * in OSX, at least in in.h. Since these values will never change, remove | |
7be77cb0 | 476 | * the external dependency. */ |
94e71143 DB |
477 | enum { CT_IPPORT_FTP = 21 }; |
478 | enum { CT_IPPORT_TFTP = 69 }; | |
bd7d93f8 DB |
479 | uint8_t ip_proto = get_ip_proto(pkt); |
480 | struct udp_header *uh = dp_packet_l4(pkt); | |
481 | struct tcp_header *th = dp_packet_l4(pkt); | |
482 | ovs_be16 ftp_src_port = htons(CT_IPPORT_FTP); | |
483 | ovs_be16 ftp_dst_port = htons(CT_IPPORT_FTP); | |
484 | ovs_be16 tftp_dst_port = htons(CT_IPPORT_TFTP); | |
485 | ||
486 | if (OVS_UNLIKELY(tp_dst)) { | |
487 | if (helper && !strncmp(helper, "ftp", strlen("ftp"))) { | |
488 | ftp_dst_port = tp_dst; | |
489 | } else if (helper && !strncmp(helper, "tftp", strlen("tftp"))) { | |
490 | tftp_dst_port = tp_dst; | |
491 | } | |
492 | } else if (OVS_UNLIKELY(tp_src)) { | |
493 | if (helper && !strncmp(helper, "ftp", strlen("ftp"))) { | |
494 | ftp_src_port = tp_src; | |
495 | } | |
496 | } | |
7be77cb0 | 497 | |
bd7d93f8 | 498 | if (ip_proto == IPPROTO_UDP && uh->udp_dst == tftp_dst_port) { |
94e71143 DB |
499 | return CT_ALG_CTL_TFTP; |
500 | } else if (ip_proto == IPPROTO_TCP && | |
bd7d93f8 | 501 | (th->tcp_src == ftp_src_port || th->tcp_dst == ftp_dst_port)) { |
94e71143 DB |
502 | return CT_ALG_CTL_FTP; |
503 | } | |
504 | return CT_ALG_CTL_NONE; | |
505 | } | |
506 | ||
be38342d DB |
507 | static bool |
508 | alg_src_ip_wc(enum ct_alg_ctl_type alg_ctl_type) | |
509 | { | |
510 | if (alg_ctl_type == CT_ALG_CTL_SIP) { | |
511 | return true; | |
512 | } | |
513 | return false; | |
514 | } | |
515 | ||
94e71143 DB |
516 | static void |
517 | handle_alg_ctl(struct conntrack *ct, const struct conn_lookup_ctx *ctx, | |
518 | struct dp_packet *pkt, enum ct_alg_ctl_type ct_alg_ctl, | |
519 | const struct conn *conn, long long now, bool nat, | |
520 | const struct conn *conn_for_expectation) | |
521 | { | |
522 | /* ALG control packet handling with expectation creation. */ | |
3a2a425b | 523 | if (OVS_UNLIKELY(alg_helpers[ct_alg_ctl] && conn && conn->alg)) { |
94e71143 DB |
524 | alg_helpers[ct_alg_ctl](ct, ctx, pkt, conn_for_expectation, now, |
525 | CT_FTP_CTL_INTEREST, nat); | |
526 | } | |
7be77cb0 DB |
527 | } |
528 | ||
286de272 DB |
529 | static void |
530 | pat_packet(struct dp_packet *pkt, const struct conn *conn) | |
531 | { | |
532 | if (conn->nat_info->nat_action & NAT_ACTION_SRC) { | |
533 | if (conn->key.nw_proto == IPPROTO_TCP) { | |
534 | struct tcp_header *th = dp_packet_l4(pkt); | |
535 | packet_set_tcp_port(pkt, conn->rev_key.dst.port, th->tcp_dst); | |
536 | } else if (conn->key.nw_proto == IPPROTO_UDP) { | |
537 | struct udp_header *uh = dp_packet_l4(pkt); | |
538 | packet_set_udp_port(pkt, conn->rev_key.dst.port, uh->udp_dst); | |
539 | } | |
540 | } else if (conn->nat_info->nat_action & NAT_ACTION_DST) { | |
541 | if (conn->key.nw_proto == IPPROTO_TCP) { | |
542 | struct tcp_header *th = dp_packet_l4(pkt); | |
543 | packet_set_tcp_port(pkt, th->tcp_src, conn->rev_key.src.port); | |
544 | } else if (conn->key.nw_proto == IPPROTO_UDP) { | |
545 | struct udp_header *uh = dp_packet_l4(pkt); | |
546 | packet_set_udp_port(pkt, uh->udp_src, conn->rev_key.src.port); | |
547 | } | |
548 | } | |
549 | } | |
550 | ||
551 | static void | |
552 | nat_packet(struct dp_packet *pkt, const struct conn *conn, bool related) | |
553 | { | |
554 | if (conn->nat_info->nat_action & NAT_ACTION_SRC) { | |
555 | pkt->md.ct_state |= CS_SRC_NAT; | |
556 | if (conn->key.dl_type == htons(ETH_TYPE_IP)) { | |
557 | struct ip_header *nh = dp_packet_l3(pkt); | |
558 | packet_set_ipv4_addr(pkt, &nh->ip_src, | |
559 | conn->rev_key.dst.addr.ipv4_aligned); | |
560 | } else { | |
561 | struct ovs_16aligned_ip6_hdr *nh6 = dp_packet_l3(pkt); | |
562 | packet_set_ipv6_addr(pkt, conn->key.nw_proto, | |
563 | nh6->ip6_src.be32, | |
564 | &conn->rev_key.dst.addr.ipv6_aligned, | |
565 | true); | |
566 | } | |
567 | if (!related) { | |
568 | pat_packet(pkt, conn); | |
569 | } | |
570 | } else if (conn->nat_info->nat_action & NAT_ACTION_DST) { | |
571 | pkt->md.ct_state |= CS_DST_NAT; | |
572 | if (conn->key.dl_type == htons(ETH_TYPE_IP)) { | |
573 | struct ip_header *nh = dp_packet_l3(pkt); | |
574 | packet_set_ipv4_addr(pkt, &nh->ip_dst, | |
575 | conn->rev_key.src.addr.ipv4_aligned); | |
576 | } else { | |
577 | struct ovs_16aligned_ip6_hdr *nh6 = dp_packet_l3(pkt); | |
578 | packet_set_ipv6_addr(pkt, conn->key.nw_proto, | |
579 | nh6->ip6_dst.be32, | |
580 | &conn->rev_key.src.addr.ipv6_aligned, | |
581 | true); | |
582 | } | |
583 | if (!related) { | |
584 | pat_packet(pkt, conn); | |
585 | } | |
586 | } | |
587 | } | |
588 | ||
589 | static void | |
590 | un_pat_packet(struct dp_packet *pkt, const struct conn *conn) | |
591 | { | |
592 | if (conn->nat_info->nat_action & NAT_ACTION_SRC) { | |
593 | if (conn->key.nw_proto == IPPROTO_TCP) { | |
594 | struct tcp_header *th = dp_packet_l4(pkt); | |
595 | packet_set_tcp_port(pkt, th->tcp_src, conn->key.src.port); | |
596 | } else if (conn->key.nw_proto == IPPROTO_UDP) { | |
597 | struct udp_header *uh = dp_packet_l4(pkt); | |
598 | packet_set_udp_port(pkt, uh->udp_src, conn->key.src.port); | |
599 | } | |
600 | } else if (conn->nat_info->nat_action & NAT_ACTION_DST) { | |
601 | if (conn->key.nw_proto == IPPROTO_TCP) { | |
602 | struct tcp_header *th = dp_packet_l4(pkt); | |
603 | packet_set_tcp_port(pkt, conn->key.dst.port, th->tcp_dst); | |
604 | } else if (conn->key.nw_proto == IPPROTO_UDP) { | |
605 | struct udp_header *uh = dp_packet_l4(pkt); | |
606 | packet_set_udp_port(pkt, conn->key.dst.port, uh->udp_dst); | |
607 | } | |
608 | } | |
609 | } | |
610 | ||
edd1bef4 DB |
611 | static void |
612 | reverse_pat_packet(struct dp_packet *pkt, const struct conn *conn) | |
613 | { | |
614 | if (conn->nat_info->nat_action & NAT_ACTION_SRC) { | |
615 | if (conn->key.nw_proto == IPPROTO_TCP) { | |
616 | struct tcp_header *th_in = dp_packet_l4(pkt); | |
617 | packet_set_tcp_port(pkt, conn->key.src.port, | |
618 | th_in->tcp_dst); | |
619 | } else if (conn->key.nw_proto == IPPROTO_UDP) { | |
620 | struct udp_header *uh_in = dp_packet_l4(pkt); | |
621 | packet_set_udp_port(pkt, conn->key.src.port, | |
622 | uh_in->udp_dst); | |
623 | } | |
624 | } else if (conn->nat_info->nat_action & NAT_ACTION_DST) { | |
625 | if (conn->key.nw_proto == IPPROTO_TCP) { | |
626 | struct tcp_header *th_in = dp_packet_l4(pkt); | |
627 | packet_set_tcp_port(pkt, th_in->tcp_src, | |
628 | conn->key.dst.port); | |
629 | } else if (conn->key.nw_proto == IPPROTO_UDP) { | |
630 | struct udp_header *uh_in = dp_packet_l4(pkt); | |
631 | packet_set_udp_port(pkt, uh_in->udp_src, | |
632 | conn->key.dst.port); | |
633 | } | |
634 | } | |
635 | } | |
636 | ||
637 | static void | |
638 | reverse_nat_packet(struct dp_packet *pkt, const struct conn *conn) | |
639 | { | |
640 | char *tail = dp_packet_tail(pkt); | |
641 | char pad = dp_packet_l2_pad_size(pkt); | |
642 | struct conn_key inner_key; | |
643 | const char *inner_l4 = NULL; | |
644 | uint16_t orig_l3_ofs = pkt->l3_ofs; | |
645 | uint16_t orig_l4_ofs = pkt->l4_ofs; | |
646 | ||
647 | if (conn->key.dl_type == htons(ETH_TYPE_IP)) { | |
648 | struct ip_header *nh = dp_packet_l3(pkt); | |
649 | struct icmp_header *icmp = dp_packet_l4(pkt); | |
650 | struct ip_header *inner_l3 = (struct ip_header *) (icmp + 1); | |
bd5e81a0 DB |
651 | extract_l3_ipv4(&inner_key, inner_l3, tail - ((char *)inner_l3) - pad, |
652 | &inner_l4, false); | |
edd1bef4 DB |
653 | pkt->l3_ofs += (char *) inner_l3 - (char *) nh; |
654 | pkt->l4_ofs += inner_l4 - (char *) icmp; | |
655 | ||
656 | if (conn->nat_info->nat_action & NAT_ACTION_SRC) { | |
657 | packet_set_ipv4_addr(pkt, &inner_l3->ip_src, | |
658 | conn->key.src.addr.ipv4_aligned); | |
659 | } else if (conn->nat_info->nat_action & NAT_ACTION_DST) { | |
660 | packet_set_ipv4_addr(pkt, &inner_l3->ip_dst, | |
661 | conn->key.dst.addr.ipv4_aligned); | |
662 | } | |
dec0dbbc | 663 | |
edd1bef4 DB |
664 | reverse_pat_packet(pkt, conn); |
665 | icmp->icmp_csum = 0; | |
666 | icmp->icmp_csum = csum(icmp, tail - (char *) icmp - pad); | |
667 | } else { | |
668 | struct ovs_16aligned_ip6_hdr *nh6 = dp_packet_l3(pkt); | |
669 | struct icmp6_error_header *icmp6 = dp_packet_l4(pkt); | |
670 | struct ovs_16aligned_ip6_hdr *inner_l3_6 = | |
671 | (struct ovs_16aligned_ip6_hdr *) (icmp6 + 1); | |
672 | extract_l3_ipv6(&inner_key, inner_l3_6, | |
673 | tail - ((char *)inner_l3_6) - pad, | |
674 | &inner_l4); | |
675 | pkt->l3_ofs += (char *) inner_l3_6 - (char *) nh6; | |
676 | pkt->l4_ofs += inner_l4 - (char *) icmp6; | |
677 | ||
678 | if (conn->nat_info->nat_action & NAT_ACTION_SRC) { | |
679 | packet_set_ipv6_addr(pkt, conn->key.nw_proto, | |
680 | inner_l3_6->ip6_src.be32, | |
681 | &conn->key.src.addr.ipv6_aligned, | |
682 | true); | |
683 | } else if (conn->nat_info->nat_action & NAT_ACTION_DST) { | |
684 | packet_set_ipv6_addr(pkt, conn->key.nw_proto, | |
685 | inner_l3_6->ip6_dst.be32, | |
686 | &conn->key.dst.addr.ipv6_aligned, | |
687 | true); | |
688 | } | |
689 | reverse_pat_packet(pkt, conn); | |
690 | uint32_t icmp6_csum = packet_csum_pseudoheader6(nh6); | |
691 | icmp6->icmp6_base.icmp6_cksum = 0; | |
692 | icmp6->icmp6_base.icmp6_cksum = csum_finish( | |
693 | csum_continue(icmp6_csum, icmp6, tail - (char *) icmp6 - pad)); | |
694 | } | |
695 | pkt->l3_ofs = orig_l3_ofs; | |
696 | pkt->l4_ofs = orig_l4_ofs; | |
697 | } | |
698 | ||
286de272 DB |
699 | static void |
700 | un_nat_packet(struct dp_packet *pkt, const struct conn *conn, | |
701 | bool related) | |
702 | { | |
703 | if (conn->nat_info->nat_action & NAT_ACTION_SRC) { | |
704 | pkt->md.ct_state |= CS_DST_NAT; | |
705 | if (conn->key.dl_type == htons(ETH_TYPE_IP)) { | |
706 | struct ip_header *nh = dp_packet_l3(pkt); | |
707 | packet_set_ipv4_addr(pkt, &nh->ip_dst, | |
708 | conn->key.src.addr.ipv4_aligned); | |
709 | } else { | |
710 | struct ovs_16aligned_ip6_hdr *nh6 = dp_packet_l3(pkt); | |
711 | packet_set_ipv6_addr(pkt, conn->key.nw_proto, | |
712 | nh6->ip6_dst.be32, | |
713 | &conn->key.src.addr.ipv6_aligned, true); | |
714 | } | |
edd1bef4 DB |
715 | |
716 | if (OVS_UNLIKELY(related)) { | |
717 | reverse_nat_packet(pkt, conn); | |
718 | } else { | |
286de272 DB |
719 | un_pat_packet(pkt, conn); |
720 | } | |
721 | } else if (conn->nat_info->nat_action & NAT_ACTION_DST) { | |
722 | pkt->md.ct_state |= CS_SRC_NAT; | |
723 | if (conn->key.dl_type == htons(ETH_TYPE_IP)) { | |
724 | struct ip_header *nh = dp_packet_l3(pkt); | |
725 | packet_set_ipv4_addr(pkt, &nh->ip_src, | |
726 | conn->key.dst.addr.ipv4_aligned); | |
727 | } else { | |
728 | struct ovs_16aligned_ip6_hdr *nh6 = dp_packet_l3(pkt); | |
729 | packet_set_ipv6_addr(pkt, conn->key.nw_proto, | |
730 | nh6->ip6_src.be32, | |
731 | &conn->key.dst.addr.ipv6_aligned, true); | |
732 | } | |
edd1bef4 DB |
733 | |
734 | if (OVS_UNLIKELY(related)) { | |
735 | reverse_nat_packet(pkt, conn); | |
736 | } else { | |
286de272 DB |
737 | un_pat_packet(pkt, conn); |
738 | } | |
739 | } | |
740 | } | |
741 | ||
742 | /* Typical usage of this helper is in non per-packet code; | |
743 | * this is because the bucket lock needs to be held for lookup | |
744 | * and a hash would have already been needed. Hence, this function | |
745 | * is just intended for code clarity. */ | |
746 | static struct conn * | |
bd5e81a0 | 747 | conn_lookup(struct conntrack *ct, const struct conn_key *key, long long now) |
286de272 DB |
748 | { |
749 | struct conn_lookup_ctx ctx; | |
750 | ctx.conn = NULL; | |
751 | ctx.key = *key; | |
752 | ctx.hash = conn_key_hash(key, ct->hash_basis); | |
753 | unsigned bucket = hash_to_bucket(ctx.hash); | |
754 | conn_key_lookup(&ct->buckets[bucket], &ctx, now); | |
755 | return ctx.conn; | |
756 | } | |
757 | ||
bd5e81a0 DB |
758 | static void |
759 | conn_seq_skew_set(struct conntrack *ct, const struct conn_key *key, | |
760 | long long now, int seq_skew, bool seq_skew_dir) | |
761 | { | |
dec0dbbc | 762 | unsigned bucket = hash_to_bucket(conn_key_hash(key, ct->hash_basis)); |
bd5e81a0 DB |
763 | ct_lock_lock(&ct->buckets[bucket].lock); |
764 | struct conn *conn = conn_lookup(ct, key, now); | |
765 | if (conn && seq_skew) { | |
766 | conn->seq_skew = seq_skew; | |
767 | conn->seq_skew_dir = seq_skew_dir; | |
768 | } | |
769 | ct_lock_unlock(&ct->buckets[bucket].lock); | |
770 | } | |
771 | ||
286de272 DB |
772 | static void |
773 | nat_clean(struct conntrack *ct, struct conn *conn, | |
774 | struct conntrack_bucket *ctb) | |
775 | OVS_REQUIRES(ctb->lock) | |
776 | { | |
8b934ced | 777 | ct_rwlock_wrlock(&ct->resources_lock); |
286de272 | 778 | nat_conn_keys_remove(&ct->nat_conn_keys, &conn->rev_key, ct->hash_basis); |
8b934ced | 779 | ct_rwlock_unlock(&ct->resources_lock); |
286de272 | 780 | ct_lock_unlock(&ctb->lock); |
dec0dbbc DB |
781 | unsigned bucket_rev_conn = |
782 | hash_to_bucket(conn_key_hash(&conn->rev_key, ct->hash_basis)); | |
286de272 | 783 | ct_lock_lock(&ct->buckets[bucket_rev_conn].lock); |
8b934ced | 784 | ct_rwlock_wrlock(&ct->resources_lock); |
dec0dbbc | 785 | long long now = time_msec(); |
286de272 | 786 | struct conn *rev_conn = conn_lookup(ct, &conn->rev_key, now); |
286de272 DB |
787 | struct nat_conn_key_node *nat_conn_key_node = |
788 | nat_conn_keys_lookup(&ct->nat_conn_keys, &conn->rev_key, | |
789 | ct->hash_basis); | |
790 | ||
791 | /* In the unlikely event, rev conn was recreated, then skip | |
792 | * rev_conn cleanup. */ | |
793 | if (rev_conn && (!nat_conn_key_node || | |
5ed7a0b4 DB |
794 | conn_key_cmp(&nat_conn_key_node->value, |
795 | &rev_conn->rev_key))) { | |
286de272 DB |
796 | hmap_remove(&ct->buckets[bucket_rev_conn].connections, |
797 | &rev_conn->node); | |
798 | free(rev_conn); | |
799 | } | |
286de272 | 800 | |
dec0dbbc | 801 | delete_conn(conn); |
8b934ced | 802 | ct_rwlock_unlock(&ct->resources_lock); |
286de272 DB |
803 | ct_lock_unlock(&ct->buckets[bucket_rev_conn].lock); |
804 | ct_lock_lock(&ctb->lock); | |
805 | } | |
806 | ||
807 | static void | |
808 | conn_clean(struct conntrack *ct, struct conn *conn, | |
809 | struct conntrack_bucket *ctb) | |
810 | OVS_REQUIRES(ctb->lock) | |
811 | { | |
4417ca3d DB |
812 | if (conn->alg) { |
813 | expectation_clean(ct, &conn->key, ct->hash_basis); | |
814 | } | |
286de272 DB |
815 | ovs_list_remove(&conn->exp_node); |
816 | hmap_remove(&ctb->connections, &conn->node); | |
817 | atomic_count_dec(&ct->n_conn); | |
818 | if (conn->nat_info) { | |
819 | nat_clean(ct, conn, ctb); | |
820 | } else { | |
821 | delete_conn(conn); | |
822 | } | |
a489b168 DDP |
823 | } |
824 | ||
3a2a425b DB |
825 | static bool |
826 | ct_verify_helper(const char *helper, enum ct_alg_ctl_type ct_alg_ctl) | |
827 | { | |
828 | if (ct_alg_ctl == CT_ALG_CTL_NONE) { | |
829 | return true; | |
830 | } else if (helper) { | |
831 | if ((ct_alg_ctl == CT_ALG_CTL_FTP) && | |
832 | !strncmp(helper, "ftp", strlen("ftp"))) { | |
833 | return true; | |
834 | } else if ((ct_alg_ctl == CT_ALG_CTL_TFTP) && | |
835 | !strncmp(helper, "tftp", strlen("tftp"))) { | |
836 | return true; | |
837 | } else { | |
838 | return false; | |
839 | } | |
840 | } else { | |
841 | return false; | |
842 | } | |
843 | } | |
844 | ||
ac6abe5f | 845 | /* This function is called with the bucket lock held. */ |
a489b168 DDP |
846 | static struct conn * |
847 | conn_not_found(struct conntrack *ct, struct dp_packet *pkt, | |
286de272 DB |
848 | struct conn_lookup_ctx *ctx, bool commit, long long now, |
849 | const struct nat_action_info_t *nat_action_info, | |
bd5e81a0 DB |
850 | struct conn *conn_for_un_nat_copy, |
851 | const char *helper, | |
3a2a425b DB |
852 | const struct alg_exp_node *alg_exp, |
853 | enum ct_alg_ctl_type ct_alg_ctl) | |
a489b168 | 854 | { |
a489b168 DDP |
855 | struct conn *nc = NULL; |
856 | ||
857 | if (!valid_new(pkt, &ctx->key)) { | |
286de272 | 858 | pkt->md.ct_state = CS_INVALID; |
a489b168 DDP |
859 | return nc; |
860 | } | |
dec0dbbc | 861 | |
286de272 | 862 | pkt->md.ct_state = CS_NEW; |
dec0dbbc | 863 | |
bd5e81a0 DB |
864 | if (alg_exp) { |
865 | pkt->md.ct_state |= CS_RELATED; | |
866 | } | |
a489b168 DDP |
867 | |
868 | if (commit) { | |
869 | unsigned int n_conn_limit; | |
a489b168 DDP |
870 | atomic_read_relaxed(&ct->n_conn_limit, &n_conn_limit); |
871 | ||
872 | if (atomic_count_get(&ct->n_conn) >= n_conn_limit) { | |
873 | COVERAGE_INC(conntrack_full); | |
874 | return nc; | |
875 | } | |
876 | ||
dec0dbbc | 877 | unsigned bucket = hash_to_bucket(ctx->hash); |
e6ef6cc6 | 878 | nc = new_conn(&ct->buckets[bucket], pkt, &ctx->key, now); |
286de272 DB |
879 | ctx->conn = nc; |
880 | nc->rev_key = nc->key; | |
881 | conn_key_reverse(&nc->rev_key); | |
a489b168 | 882 | |
3a2a425b DB |
883 | if (ct_verify_helper(helper, ct_alg_ctl)) { |
884 | nc->alg = nullable_xstrdup(helper); | |
bd5e81a0 DB |
885 | } |
886 | ||
887 | if (alg_exp) { | |
888 | nc->alg_related = true; | |
889 | nc->mark = alg_exp->master_mark; | |
890 | nc->label = alg_exp->master_label; | |
891 | nc->master_key = alg_exp->master_key; | |
892 | } | |
893 | ||
286de272 DB |
894 | if (nat_action_info) { |
895 | nc->nat_info = xmemdup(nat_action_info, sizeof *nc->nat_info); | |
a489b168 | 896 | |
bd5e81a0 | 897 | if (alg_exp) { |
be38342d | 898 | if (alg_exp->nat_rpl_dst) { |
bd5e81a0 DB |
899 | nc->rev_key.dst.addr = alg_exp->alg_nat_repl_addr; |
900 | nc->nat_info->nat_action = NAT_ACTION_SRC; | |
901 | } else { | |
902 | nc->rev_key.src.addr = alg_exp->alg_nat_repl_addr; | |
903 | nc->nat_info->nat_action = NAT_ACTION_DST; | |
904 | } | |
905 | *conn_for_un_nat_copy = *nc; | |
d8682ee5 DB |
906 | ct_rwlock_wrlock(&ct->resources_lock); |
907 | bool new_insert = nat_conn_keys_insert(&ct->nat_conn_keys, | |
908 | conn_for_un_nat_copy, | |
909 | ct->hash_basis); | |
910 | ct_rwlock_unlock(&ct->resources_lock); | |
911 | if (!new_insert) { | |
912 | char *log_msg = xasprintf("Pre-existing alg " | |
913 | "nat_conn_key"); | |
914 | ct_print_conn_info(conn_for_un_nat_copy, log_msg, VLL_INFO, | |
915 | true, false); | |
916 | free(log_msg); | |
917 | } | |
bd5e81a0 DB |
918 | } else { |
919 | *conn_for_un_nat_copy = *nc; | |
920 | ct_rwlock_wrlock(&ct->resources_lock); | |
dec0dbbc DB |
921 | bool nat_res = nat_select_range_tuple(ct, nc, |
922 | conn_for_un_nat_copy); | |
286de272 | 923 | |
bd5e81a0 DB |
924 | if (!nat_res) { |
925 | goto nat_res_exhaustion; | |
926 | } | |
286de272 | 927 | |
bd5e81a0 DB |
928 | /* Update nc with nat adjustments made to |
929 | * conn_for_un_nat_copy by nat_select_range_tuple(). */ | |
286de272 | 930 | *nc = *conn_for_un_nat_copy; |
bd5e81a0 | 931 | ct_rwlock_unlock(&ct->resources_lock); |
286de272 | 932 | } |
bd5e81a0 DB |
933 | conn_for_un_nat_copy->conn_type = CT_CONN_TYPE_UN_NAT; |
934 | conn_for_un_nat_copy->nat_info = NULL; | |
935 | conn_for_un_nat_copy->alg = NULL; | |
dbb597d3 | 936 | nat_packet(pkt, nc, ctx->icmp_related); |
286de272 | 937 | } |
a489b168 DDP |
938 | hmap_insert(&ct->buckets[bucket].connections, &nc->node, ctx->hash); |
939 | atomic_count_inc(&ct->n_conn); | |
940 | } | |
bd5e81a0 | 941 | |
a489b168 | 942 | return nc; |
bd5e81a0 DB |
943 | |
944 | /* This would be a user error or a DOS attack. | |
945 | * A user error is prevented by allocating enough | |
946 | * combinations of NAT addresses when combined with | |
947 | * ephemeral ports. A DOS attack should be protected | |
948 | * against with firewall rules or a separate firewall. | |
949 | * Also using zone partitioning can limit DoS impact. */ | |
950 | nat_res_exhaustion: | |
d8c5a93b | 951 | ovs_list_remove(&nc->exp_node); |
bd5e81a0 DB |
952 | delete_conn(nc); |
953 | /* conn_for_un_nat_copy is a local variable in process_one; this | |
954 | * memset() serves to document that conn_for_un_nat_copy is from | |
955 | * this point on unused. */ | |
956 | memset(conn_for_un_nat_copy, 0, sizeof *conn_for_un_nat_copy); | |
957 | ct_rwlock_unlock(&ct->resources_lock); | |
958 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5); | |
959 | VLOG_WARN_RL(&rl, "Unable to NAT due to tuple space exhaustion - " | |
960 | "if DoS attack, use firewalling and/or zone partitioning."); | |
961 | return NULL; | |
a489b168 DDP |
962 | } |
963 | ||
286de272 DB |
964 | static bool |
965 | conn_update_state(struct conntrack *ct, struct dp_packet *pkt, | |
966 | struct conn_lookup_ctx *ctx, struct conn **conn, | |
967 | long long now, unsigned bucket) | |
968 | OVS_REQUIRES(ct->buckets[bucket].lock) | |
969 | { | |
970 | bool create_new_conn = false; | |
971 | ||
dbb597d3 | 972 | if (ctx->icmp_related) { |
286de272 DB |
973 | pkt->md.ct_state |= CS_RELATED; |
974 | if (ctx->reply) { | |
975 | pkt->md.ct_state |= CS_REPLY_DIR; | |
976 | } | |
977 | } else { | |
bd5e81a0 DB |
978 | if ((*conn)->alg_related) { |
979 | pkt->md.ct_state |= CS_RELATED; | |
980 | } | |
dec0dbbc | 981 | |
286de272 DB |
982 | enum ct_update_res res = conn_update(*conn, &ct->buckets[bucket], |
983 | pkt, ctx->reply, now); | |
984 | ||
985 | switch (res) { | |
986 | case CT_UPDATE_VALID: | |
987 | pkt->md.ct_state |= CS_ESTABLISHED; | |
988 | pkt->md.ct_state &= ~CS_NEW; | |
989 | if (ctx->reply) { | |
990 | pkt->md.ct_state |= CS_REPLY_DIR; | |
991 | } | |
992 | break; | |
993 | case CT_UPDATE_INVALID: | |
994 | pkt->md.ct_state = CS_INVALID; | |
995 | break; | |
996 | case CT_UPDATE_NEW: | |
997 | conn_clean(ct, *conn, &ct->buckets[bucket]); | |
998 | create_new_conn = true; | |
999 | break; | |
1000 | default: | |
1001 | OVS_NOT_REACHED(); | |
1002 | } | |
1003 | } | |
1004 | return create_new_conn; | |
1005 | } | |
1006 | ||
1007 | static void | |
1008 | create_un_nat_conn(struct conntrack *ct, struct conn *conn_for_un_nat_copy, | |
bd5e81a0 | 1009 | long long now, bool alg_un_nat) |
286de272 DB |
1010 | { |
1011 | struct conn *nc = xmemdup(conn_for_un_nat_copy, sizeof *nc); | |
1012 | nc->key = conn_for_un_nat_copy->rev_key; | |
1013 | nc->rev_key = conn_for_un_nat_copy->key; | |
1014 | uint32_t un_nat_hash = conn_key_hash(&nc->key, ct->hash_basis); | |
1015 | unsigned un_nat_conn_bucket = hash_to_bucket(un_nat_hash); | |
1016 | ct_lock_lock(&ct->buckets[un_nat_conn_bucket].lock); | |
286de272 DB |
1017 | struct conn *rev_conn = conn_lookup(ct, &nc->key, now); |
1018 | ||
bd5e81a0 | 1019 | if (alg_un_nat) { |
d8682ee5 DB |
1020 | if (!rev_conn) { |
1021 | hmap_insert(&ct->buckets[un_nat_conn_bucket].connections, | |
1022 | &nc->node, un_nat_hash); | |
1023 | } else { | |
1024 | char *log_msg = xasprintf("Unusual condition for un_nat conn " | |
1025 | "create for alg: rev_conn %p", rev_conn); | |
1026 | ct_print_conn_info(nc, log_msg, VLL_INFO, true, false); | |
1027 | free(log_msg); | |
1028 | free(nc); | |
1029 | } | |
286de272 | 1030 | } else { |
bd5e81a0 DB |
1031 | ct_rwlock_rdlock(&ct->resources_lock); |
1032 | ||
1033 | struct nat_conn_key_node *nat_conn_key_node = | |
1034 | nat_conn_keys_lookup(&ct->nat_conn_keys, &nc->key, ct->hash_basis); | |
1035 | if (nat_conn_key_node && !conn_key_cmp(&nat_conn_key_node->value, | |
1036 | &nc->rev_key) && !rev_conn) { | |
bd5e81a0 DB |
1037 | hmap_insert(&ct->buckets[un_nat_conn_bucket].connections, |
1038 | &nc->node, un_nat_hash); | |
1039 | } else { | |
d8682ee5 DB |
1040 | char *log_msg = xasprintf("Unusual condition for un_nat conn " |
1041 | "create: nat_conn_key_node/rev_conn " | |
1042 | "%p/%p", nat_conn_key_node, rev_conn); | |
1043 | ct_print_conn_info(nc, log_msg, VLL_INFO, true, false); | |
1044 | free(log_msg); | |
bd5e81a0 DB |
1045 | free(nc); |
1046 | } | |
1047 | ct_rwlock_unlock(&ct->resources_lock); | |
286de272 | 1048 | } |
286de272 DB |
1049 | ct_lock_unlock(&ct->buckets[un_nat_conn_bucket].lock); |
1050 | } | |
1051 | ||
1052 | static void | |
1053 | handle_nat(struct dp_packet *pkt, struct conn *conn, | |
1054 | uint16_t zone, bool reply, bool related) | |
1055 | { | |
1056 | if (conn->nat_info && | |
1057 | (!(pkt->md.ct_state & (CS_SRC_NAT | CS_DST_NAT)) || | |
1058 | (pkt->md.ct_state & (CS_SRC_NAT | CS_DST_NAT) && | |
1059 | zone != pkt->md.ct_zone))) { | |
bd5e81a0 | 1060 | |
286de272 DB |
1061 | if (pkt->md.ct_state & (CS_SRC_NAT | CS_DST_NAT)) { |
1062 | pkt->md.ct_state &= ~(CS_SRC_NAT | CS_DST_NAT); | |
1063 | } | |
1064 | if (reply) { | |
1065 | un_nat_packet(pkt, conn, related); | |
1066 | } else { | |
1067 | nat_packet(pkt, conn, related); | |
1068 | } | |
1069 | } | |
1070 | } | |
1071 | ||
f8016041 DB |
1072 | static bool |
1073 | check_orig_tuple(struct conntrack *ct, struct dp_packet *pkt, | |
1074 | struct conn_lookup_ctx *ctx_in, long long now, | |
1075 | unsigned *bucket, struct conn **conn, | |
1076 | const struct nat_action_info_t *nat_action_info) | |
1077 | OVS_REQUIRES(ct->buckets[*bucket].lock) | |
1078 | { | |
1079 | if ((ctx_in->key.dl_type == htons(ETH_TYPE_IP) && | |
1080 | !pkt->md.ct_orig_tuple.ipv4.ipv4_proto) || | |
1081 | (ctx_in->key.dl_type == htons(ETH_TYPE_IPV6) && | |
1082 | !pkt->md.ct_orig_tuple.ipv6.ipv6_proto) || | |
1083 | !(pkt->md.ct_state & (CS_SRC_NAT | CS_DST_NAT)) || | |
1084 | nat_action_info) { | |
1085 | return false; | |
1086 | } | |
1087 | ||
1088 | ct_lock_unlock(&ct->buckets[*bucket].lock); | |
1089 | struct conn_lookup_ctx ctx; | |
1090 | memset(&ctx, 0 , sizeof ctx); | |
1091 | ctx.conn = NULL; | |
1092 | ||
1093 | if (ctx_in->key.dl_type == htons(ETH_TYPE_IP)) { | |
1094 | ctx.key.src.addr.ipv4_aligned = pkt->md.ct_orig_tuple.ipv4.ipv4_src; | |
1095 | ctx.key.dst.addr.ipv4_aligned = pkt->md.ct_orig_tuple.ipv4.ipv4_dst; | |
1096 | ||
1097 | if (ctx_in->key.nw_proto == IPPROTO_ICMP) { | |
1098 | ctx.key.src.icmp_id = ctx_in->key.src.icmp_id; | |
1099 | ctx.key.dst.icmp_id = ctx_in->key.dst.icmp_id; | |
1100 | uint16_t src_port = ntohs(pkt->md.ct_orig_tuple.ipv4.src_port); | |
1101 | ctx.key.src.icmp_type = (uint8_t) src_port; | |
1102 | ctx.key.dst.icmp_type = reverse_icmp_type(ctx.key.src.icmp_type); | |
1103 | } else { | |
1104 | ctx.key.src.port = pkt->md.ct_orig_tuple.ipv4.src_port; | |
1105 | ctx.key.dst.port = pkt->md.ct_orig_tuple.ipv4.dst_port; | |
1106 | } | |
1107 | ctx.key.nw_proto = pkt->md.ct_orig_tuple.ipv4.ipv4_proto; | |
1108 | } else { | |
1109 | ctx.key.src.addr.ipv6_aligned = pkt->md.ct_orig_tuple.ipv6.ipv6_src; | |
1110 | ctx.key.dst.addr.ipv6_aligned = pkt->md.ct_orig_tuple.ipv6.ipv6_dst; | |
1111 | ||
1112 | if (ctx_in->key.nw_proto == IPPROTO_ICMPV6) { | |
1113 | ctx.key.src.icmp_id = ctx_in->key.src.icmp_id; | |
1114 | ctx.key.dst.icmp_id = ctx_in->key.dst.icmp_id; | |
1115 | uint16_t src_port = ntohs(pkt->md.ct_orig_tuple.ipv6.src_port); | |
1116 | ctx.key.src.icmp_type = (uint8_t) src_port; | |
1117 | ctx.key.dst.icmp_type = reverse_icmp6_type(ctx.key.src.icmp_type); | |
1118 | } else { | |
1119 | ctx.key.src.port = pkt->md.ct_orig_tuple.ipv6.src_port; | |
1120 | ctx.key.dst.port = pkt->md.ct_orig_tuple.ipv6.dst_port; | |
1121 | } | |
1122 | ctx.key.nw_proto = pkt->md.ct_orig_tuple.ipv6.ipv6_proto; | |
1123 | } | |
1124 | ||
1125 | ctx.key.dl_type = ctx_in->key.dl_type; | |
1126 | ctx.key.zone = pkt->md.ct_zone; | |
f8016041 DB |
1127 | ctx.hash = conn_key_hash(&ctx.key, ct->hash_basis); |
1128 | *bucket = hash_to_bucket(ctx.hash); | |
1129 | ct_lock_lock(&ct->buckets[*bucket].lock); | |
1130 | conn_key_lookup(&ct->buckets[*bucket], &ctx, now); | |
1131 | *conn = ctx.conn; | |
f8016041 DB |
1132 | return *conn ? true : false; |
1133 | } | |
1134 | ||
bd5e81a0 DB |
1135 | static bool |
1136 | is_un_nat_conn_valid(const struct conn *un_nat_conn) | |
1137 | { | |
1138 | return un_nat_conn->conn_type == CT_CONN_TYPE_UN_NAT; | |
1139 | } | |
1140 | ||
94e71143 DB |
1141 | static bool |
1142 | conn_update_state_alg(struct conntrack *ct, struct dp_packet *pkt, | |
1143 | struct conn_lookup_ctx *ctx, struct conn *conn, | |
1144 | const struct nat_action_info_t *nat_action_info, | |
1145 | enum ct_alg_ctl_type ct_alg_ctl, long long now, | |
1146 | unsigned bucket, bool *create_new_conn) | |
1147 | OVS_REQUIRES(ct->buckets[bucket].lock) | |
1148 | { | |
1149 | if (is_ftp_ctl(ct_alg_ctl)) { | |
1150 | /* Keep sequence tracking in sync with the source of the | |
1151 | * sequence skew. */ | |
1152 | if (ctx->reply != conn->seq_skew_dir) { | |
1153 | handle_ftp_ctl(ct, ctx, pkt, conn, now, CT_FTP_CTL_OTHER, | |
1154 | !!nat_action_info); | |
1155 | *create_new_conn = conn_update_state(ct, pkt, ctx, &conn, now, | |
1156 | bucket); | |
1157 | } else { | |
1158 | *create_new_conn = conn_update_state(ct, pkt, ctx, &conn, now, | |
1159 | bucket); | |
1160 | handle_ftp_ctl(ct, ctx, pkt, conn, now, CT_FTP_CTL_OTHER, | |
1161 | !!nat_action_info); | |
1162 | } | |
1163 | return true; | |
1164 | } | |
1165 | return false; | |
1166 | } | |
1167 | ||
286de272 | 1168 | static void |
a489b168 DDP |
1169 | process_one(struct conntrack *ct, struct dp_packet *pkt, |
1170 | struct conn_lookup_ctx *ctx, uint16_t zone, | |
286de272 DB |
1171 | bool force, bool commit, long long now, const uint32_t *setmark, |
1172 | const struct ovs_key_ct_labels *setlabel, | |
bd5e81a0 | 1173 | const struct nat_action_info_t *nat_action_info, |
bd7d93f8 | 1174 | ovs_be16 tp_src, ovs_be16 tp_dst, const char *helper) |
a489b168 | 1175 | { |
286de272 | 1176 | struct conn *conn; |
a489b168 | 1177 | unsigned bucket = hash_to_bucket(ctx->hash); |
286de272 DB |
1178 | ct_lock_lock(&ct->buckets[bucket].lock); |
1179 | conn_key_lookup(&ct->buckets[bucket], ctx, now); | |
1180 | conn = ctx->conn; | |
a489b168 | 1181 | |
a76a37ef JR |
1182 | /* Delete found entry if in wrong direction. 'force' implies commit. */ |
1183 | if (conn && force && ctx->reply) { | |
286de272 | 1184 | conn_clean(ct, conn, &ct->buckets[bucket]); |
a76a37ef JR |
1185 | conn = NULL; |
1186 | } | |
1187 | ||
286de272 DB |
1188 | if (OVS_LIKELY(conn)) { |
1189 | if (conn->conn_type == CT_CONN_TYPE_UN_NAT) { | |
a489b168 | 1190 | |
286de272 | 1191 | ctx->reply = true; |
a489b168 | 1192 | |
286de272 DB |
1193 | struct conn_lookup_ctx ctx2; |
1194 | ctx2.conn = NULL; | |
1195 | ctx2.key = conn->rev_key; | |
1196 | ctx2.hash = conn_key_hash(&conn->rev_key, ct->hash_basis); | |
1197 | ||
1198 | ct_lock_unlock(&ct->buckets[bucket].lock); | |
1199 | bucket = hash_to_bucket(ctx2.hash); | |
1200 | ||
1201 | ct_lock_lock(&ct->buckets[bucket].lock); | |
1202 | conn_key_lookup(&ct->buckets[bucket], &ctx2, now); | |
1203 | ||
1204 | if (ctx2.conn) { | |
1205 | conn = ctx2.conn; | |
1206 | } else { | |
1207 | /* It is a race condition where conn has timed out and removed | |
1208 | * between unlock of the rev_conn and lock of the forward conn; | |
1209 | * nothing to do. */ | |
1210 | pkt->md.ct_state |= CS_TRACKED | CS_INVALID; | |
1211 | ct_lock_unlock(&ct->buckets[bucket].lock); | |
1212 | return; | |
a489b168 DDP |
1213 | } |
1214 | } | |
286de272 DB |
1215 | } |
1216 | ||
1217 | bool create_new_conn = false; | |
1218 | struct conn conn_for_un_nat_copy; | |
1219 | conn_for_un_nat_copy.conn_type = CT_CONN_TYPE_DEFAULT; | |
94e71143 | 1220 | |
bd7d93f8 DB |
1221 | enum ct_alg_ctl_type ct_alg_ctl = get_alg_ctl_type(pkt, tp_src, tp_dst, |
1222 | helper); | |
bd5e81a0 | 1223 | |
286de272 | 1224 | if (OVS_LIKELY(conn)) { |
94e71143 DB |
1225 | if (OVS_LIKELY(!conn_update_state_alg(ct, pkt, ctx, conn, |
1226 | nat_action_info, | |
1227 | ct_alg_ctl, now, bucket, | |
1228 | &create_new_conn))) { | |
bd5e81a0 DB |
1229 | create_new_conn = conn_update_state(ct, pkt, ctx, &conn, now, |
1230 | bucket); | |
1231 | } | |
286de272 | 1232 | if (nat_action_info && !create_new_conn) { |
dbb597d3 | 1233 | handle_nat(pkt, conn, zone, ctx->reply, ctx->icmp_related); |
286de272 | 1234 | } |
bd5e81a0 | 1235 | |
dec0dbbc | 1236 | } else if (check_orig_tuple(ct, pkt, ctx, now, &bucket, &conn, |
bd5e81a0 | 1237 | nat_action_info)) { |
dec0dbbc | 1238 | create_new_conn = conn_update_state(ct, pkt, ctx, &conn, now, bucket); |
a489b168 | 1239 | } else { |
dbb597d3 | 1240 | if (ctx->icmp_related) { |
bd5e81a0 DB |
1241 | /* An icmp related conn should always be found; no new |
1242 | connection is created based on an icmp related packet. */ | |
286de272 | 1243 | pkt->md.ct_state = CS_INVALID; |
5c2e106b | 1244 | } else { |
286de272 | 1245 | create_new_conn = true; |
5c2e106b | 1246 | } |
a489b168 DDP |
1247 | } |
1248 | ||
bd5e81a0 | 1249 | const struct alg_exp_node *alg_exp = NULL; |
dec0dbbc | 1250 | |
286de272 | 1251 | if (OVS_UNLIKELY(create_new_conn)) { |
bd5e81a0 DB |
1252 | struct alg_exp_node alg_exp_entry; |
1253 | ||
1254 | ct_rwlock_rdlock(&ct->resources_lock); | |
1255 | alg_exp = expectation_lookup(&ct->alg_expectations, &ctx->key, | |
be38342d DB |
1256 | ct->hash_basis, |
1257 | alg_src_ip_wc(ct_alg_ctl)); | |
bd5e81a0 DB |
1258 | if (alg_exp) { |
1259 | alg_exp_entry = *alg_exp; | |
1260 | alg_exp = &alg_exp_entry; | |
1261 | } | |
1262 | ct_rwlock_unlock(&ct->resources_lock); | |
1263 | ||
286de272 | 1264 | conn = conn_not_found(ct, pkt, ctx, commit, now, nat_action_info, |
3a2a425b DB |
1265 | &conn_for_un_nat_copy, helper, alg_exp, |
1266 | ct_alg_ctl); | |
286de272 DB |
1267 | } |
1268 | ||
bd5e81a0 DB |
1269 | write_ct_md(pkt, zone, conn, &ctx->key, alg_exp); |
1270 | ||
286de272 DB |
1271 | if (conn && setmark) { |
1272 | set_mark(pkt, conn, setmark[0], setmark[1]); | |
1273 | } | |
a489b168 | 1274 | |
286de272 DB |
1275 | if (conn && setlabel) { |
1276 | set_label(pkt, conn, &setlabel[0], &setlabel[1]); | |
1277 | } | |
1278 | ||
bd5e81a0 | 1279 | struct conn conn_for_expectation; |
94e71143 | 1280 | if (OVS_UNLIKELY((ct_alg_ctl != CT_ALG_CTL_NONE) && conn)) { |
bd5e81a0 DB |
1281 | conn_for_expectation = *conn; |
1282 | } | |
1283 | ||
286de272 DB |
1284 | ct_lock_unlock(&ct->buckets[bucket].lock); |
1285 | ||
bd5e81a0 DB |
1286 | if (is_un_nat_conn_valid(&conn_for_un_nat_copy)) { |
1287 | create_un_nat_conn(ct, &conn_for_un_nat_copy, now, !!alg_exp); | |
1288 | } | |
1289 | ||
94e71143 DB |
1290 | handle_alg_ctl(ct, ctx, pkt, ct_alg_ctl, conn, now, !!nat_action_info, |
1291 | &conn_for_expectation); | |
a489b168 DDP |
1292 | } |
1293 | ||
1294 | /* Sends the packets in '*pkt_batch' through the connection tracker 'ct'. All | |
1295 | * the packets should have the same 'dl_type' (IPv4 or IPv6) and should have | |
1296 | * the l3 and and l4 offset properly set. | |
1297 | * | |
1298 | * If 'commit' is true, the packets are allowed to create new entries in the | |
1299 | * connection tables. 'setmark', if not NULL, should point to a two | |
1300 | * elements array containing a value and a mask to set the connection mark. | |
1301 | * 'setlabel' behaves similarly for the connection label.*/ | |
1302 | int | |
1303 | conntrack_execute(struct conntrack *ct, struct dp_packet_batch *pkt_batch, | |
a76a37ef | 1304 | ovs_be16 dl_type, bool force, bool commit, uint16_t zone, |
66e4ad8a | 1305 | const uint32_t *setmark, |
a489b168 | 1306 | const struct ovs_key_ct_labels *setlabel, |
bd7d93f8 | 1307 | ovs_be16 tp_src, ovs_be16 tp_dst, const char *helper, |
94053e66 FA |
1308 | const struct nat_action_info_t *nat_action_info, |
1309 | long long now) | |
a489b168 | 1310 | { |
bd5e81a0 | 1311 | |
43495c45 | 1312 | struct dp_packet *packet; |
61ce32b9 | 1313 | struct conn_lookup_ctx ctx; |
a489b168 | 1314 | |
43495c45 BB |
1315 | DP_PACKET_BATCH_FOR_EACH (packet, pkt_batch) { |
1316 | if (!conn_key_extract(ct, packet, dl_type, &ctx, zone)) { | |
1317 | packet->md.ct_state = CS_INVALID; | |
1318 | write_ct_md(packet, zone, NULL, NULL, NULL); | |
a489b168 DDP |
1319 | continue; |
1320 | } | |
94e71143 | 1321 | process_one(ct, packet, &ctx, zone, force, commit, now, setmark, |
bd7d93f8 | 1322 | setlabel, nat_action_info, tp_src, tp_dst, helper); |
a489b168 DDP |
1323 | } |
1324 | ||
1325 | return 0; | |
1326 | } | |
1327 | ||
1328 | static void | |
1329 | set_mark(struct dp_packet *pkt, struct conn *conn, uint32_t val, uint32_t mask) | |
1330 | { | |
bd5e81a0 DB |
1331 | if (conn->alg_related) { |
1332 | pkt->md.ct_mark = conn->mark; | |
1333 | } else { | |
1334 | pkt->md.ct_mark = val | (pkt->md.ct_mark & ~(mask)); | |
1335 | conn->mark = pkt->md.ct_mark; | |
1336 | } | |
a489b168 DDP |
1337 | } |
1338 | ||
1339 | static void | |
1340 | set_label(struct dp_packet *pkt, struct conn *conn, | |
1341 | const struct ovs_key_ct_labels *val, | |
1342 | const struct ovs_key_ct_labels *mask) | |
1343 | { | |
bd5e81a0 DB |
1344 | if (conn->alg_related) { |
1345 | pkt->md.ct_label = conn->label; | |
1346 | } else { | |
1347 | ovs_u128 v, m; | |
a489b168 | 1348 | |
bd5e81a0 DB |
1349 | memcpy(&v, val, sizeof v); |
1350 | memcpy(&m, mask, sizeof m); | |
a489b168 | 1351 | |
bd5e81a0 | 1352 | pkt->md.ct_label.u64.lo = v.u64.lo |
a489b168 | 1353 | | (pkt->md.ct_label.u64.lo & ~(m.u64.lo)); |
bd5e81a0 | 1354 | pkt->md.ct_label.u64.hi = v.u64.hi |
a489b168 | 1355 | | (pkt->md.ct_label.u64.hi & ~(m.u64.hi)); |
bd5e81a0 DB |
1356 | conn->label = pkt->md.ct_label; |
1357 | } | |
a489b168 | 1358 | } |
286de272 | 1359 | |
a489b168 | 1360 | \f |
e6ef6cc6 DDP |
1361 | /* Delete the expired connections from 'ctb', up to 'limit'. Returns the |
1362 | * earliest expiration time among the remaining connections in 'ctb'. Returns | |
1363 | * LLONG_MAX if 'ctb' is empty. The return value might be smaller than 'now', | |
1364 | * if 'limit' is reached */ | |
1365 | static long long | |
bd5e81a0 DB |
1366 | sweep_bucket(struct conntrack *ct, struct conntrack_bucket *ctb, |
1367 | long long now, size_t limit) | |
e6ef6cc6 DDP |
1368 | OVS_REQUIRES(ctb->lock) |
1369 | { | |
1370 | struct conn *conn, *next; | |
1371 | long long min_expiration = LLONG_MAX; | |
e6ef6cc6 DDP |
1372 | size_t count = 0; |
1373 | ||
dec0dbbc | 1374 | for (unsigned i = 0; i < N_CT_TM; i++) { |
e6ef6cc6 | 1375 | LIST_FOR_EACH_SAFE (conn, next, exp_node, &ctb->exp_lists[i]) { |
286de272 DB |
1376 | if (conn->conn_type == CT_CONN_TYPE_DEFAULT) { |
1377 | if (!conn_expired(conn, now) || count >= limit) { | |
1378 | min_expiration = MIN(min_expiration, conn->expiration); | |
1379 | if (count >= limit) { | |
1380 | /* Do not check other lists. */ | |
1381 | COVERAGE_INC(conntrack_long_cleanup); | |
1382 | return min_expiration; | |
1383 | } | |
1384 | break; | |
e6ef6cc6 | 1385 | } |
286de272 DB |
1386 | conn_clean(ct, conn, ctb); |
1387 | count++; | |
e6ef6cc6 | 1388 | } |
e6ef6cc6 DDP |
1389 | } |
1390 | } | |
e6ef6cc6 DDP |
1391 | return min_expiration; |
1392 | } | |
1393 | ||
1394 | /* Cleans up old connection entries from 'ct'. Returns the time when the | |
1395 | * next expiration might happen. The return value might be smaller than | |
1396 | * 'now', meaning that an internal limit has been reached, and some expired | |
1397 | * connections have not been deleted. */ | |
1398 | static long long | |
1399 | conntrack_clean(struct conntrack *ct, long long now) | |
1400 | { | |
1401 | long long next_wakeup = now + CT_TM_MIN; | |
1402 | unsigned int n_conn_limit; | |
1403 | size_t clean_count = 0; | |
e6ef6cc6 DDP |
1404 | |
1405 | atomic_read_relaxed(&ct->n_conn_limit, &n_conn_limit); | |
1406 | ||
dec0dbbc | 1407 | for (unsigned i = 0; i < CONNTRACK_BUCKETS; i++) { |
e6ef6cc6 DDP |
1408 | struct conntrack_bucket *ctb = &ct->buckets[i]; |
1409 | size_t prev_count; | |
1410 | long long min_exp; | |
1411 | ||
1412 | ovs_mutex_lock(&ctb->cleanup_mutex); | |
1413 | if (ctb->next_cleanup > now) { | |
1414 | goto next_bucket; | |
1415 | } | |
1416 | ||
1417 | ct_lock_lock(&ctb->lock); | |
1418 | prev_count = hmap_count(&ctb->connections); | |
1419 | /* If the connections are well distributed among buckets, we want to | |
1420 | * limit to 10% of the global limit equally split among buckets. If | |
1421 | * the bucket is busier than the others, we limit to 10% of its | |
1422 | * current size. */ | |
1423 | min_exp = sweep_bucket(ct, ctb, now, | |
1424 | MAX(prev_count/10, n_conn_limit/(CONNTRACK_BUCKETS*10))); | |
1425 | clean_count += prev_count - hmap_count(&ctb->connections); | |
1426 | ||
1427 | if (min_exp > now) { | |
1428 | /* We call hmap_shrink() only if sweep_bucket() managed to delete | |
1429 | * every expired connection. */ | |
1430 | hmap_shrink(&ctb->connections); | |
1431 | } | |
1432 | ||
1433 | ct_lock_unlock(&ctb->lock); | |
1434 | ||
1435 | ctb->next_cleanup = MIN(min_exp, now + CT_TM_MIN); | |
1436 | ||
1437 | next_bucket: | |
1438 | next_wakeup = MIN(next_wakeup, ctb->next_cleanup); | |
1439 | ovs_mutex_unlock(&ctb->cleanup_mutex); | |
1440 | } | |
1441 | ||
1442 | VLOG_DBG("conntrack cleanup %"PRIuSIZE" entries in %lld msec", | |
1443 | clean_count, time_msec() - now); | |
1444 | ||
1445 | return next_wakeup; | |
1446 | } | |
1447 | ||
1448 | /* Cleanup: | |
e6ef6cc6 DDP |
1449 | * |
1450 | * We must call conntrack_clean() periodically. conntrack_clean() return | |
1451 | * value gives an hint on when the next cleanup must be done (either because | |
1452 | * there is an actual connection that expires, or because a new connection | |
1453 | * might be created with the minimum timeout). | |
1454 | * | |
1455 | * The logic below has two goals: | |
1456 | * | |
6c54734e DDP |
1457 | * - We want to reduce the number of wakeups and batch connection cleanup |
1458 | * when the load is not very high. CT_CLEAN_INTERVAL ensures that if we | |
1459 | * are coping with the current cleanup tasks, then we wait at least | |
1460 | * 5 seconds to do further cleanup. | |
e6ef6cc6 | 1461 | * |
6c54734e DDP |
1462 | * - We don't want to keep the buckets locked too long, as we might prevent |
1463 | * traffic from flowing. CT_CLEAN_MIN_INTERVAL ensures that if cleanup is | |
1464 | * behind, there is at least some 200ms blocks of time when buckets will be | |
1465 | * left alone, so the datapath can operate unhindered. | |
e6ef6cc6 DDP |
1466 | */ |
1467 | #define CT_CLEAN_INTERVAL 5000 /* 5 seconds */ | |
1468 | #define CT_CLEAN_MIN_INTERVAL 200 /* 0.2 seconds */ | |
1469 | ||
1470 | static void * | |
1471 | clean_thread_main(void *f_) | |
1472 | { | |
1473 | struct conntrack *ct = f_; | |
1474 | ||
1475 | while (!latch_is_set(&ct->clean_thread_exit)) { | |
1476 | long long next_wake; | |
1477 | long long now = time_msec(); | |
e6ef6cc6 DDP |
1478 | next_wake = conntrack_clean(ct, now); |
1479 | ||
1480 | if (next_wake < now) { | |
1481 | poll_timer_wait_until(now + CT_CLEAN_MIN_INTERVAL); | |
1482 | } else { | |
1483 | poll_timer_wait_until(MAX(next_wake, now + CT_CLEAN_INTERVAL)); | |
1484 | } | |
1485 | latch_wait(&ct->clean_thread_exit); | |
1486 | poll_block(); | |
1487 | } | |
1488 | ||
1489 | return NULL; | |
1490 | } | |
1491 | \f | |
a489b168 DDP |
1492 | /* Key extraction */ |
1493 | ||
1494 | /* The function stores a pointer to the first byte after the header in | |
1495 | * '*new_data', if 'new_data' is not NULL. If it is NULL, the caller is | |
1496 | * not interested in the header's tail, meaning that the header has | |
1497 | * already been parsed (e.g. by flow_extract): we take this as a hint to | |
1498 | * save a few checks. If 'validate_checksum' is true, the function returns | |
1499 | * false if the IPv4 checksum is invalid. */ | |
1500 | static inline bool | |
1501 | extract_l3_ipv4(struct conn_key *key, const void *data, size_t size, | |
1502 | const char **new_data, bool validate_checksum) | |
1503 | { | |
a489b168 DDP |
1504 | if (new_data) { |
1505 | if (OVS_UNLIKELY(size < IP_HEADER_LEN)) { | |
1506 | return false; | |
1507 | } | |
1508 | } | |
1509 | ||
dec0dbbc DB |
1510 | const struct ip_header *ip = data; |
1511 | size_t ip_len = IP_IHL(ip->ip_ihl_ver) * 4; | |
a489b168 DDP |
1512 | |
1513 | if (new_data) { | |
1514 | if (OVS_UNLIKELY(ip_len < IP_HEADER_LEN)) { | |
1515 | return false; | |
1516 | } | |
1517 | if (OVS_UNLIKELY(size < ip_len)) { | |
1518 | return false; | |
1519 | } | |
1520 | ||
c8b1ad49 DB |
1521 | if (IP_IS_FRAGMENT(ip->ip_frag_off)) { |
1522 | return false; | |
1523 | } | |
a489b168 | 1524 | |
c8b1ad49 | 1525 | *new_data = (char *) data + ip_len; |
a489b168 DDP |
1526 | } |
1527 | ||
1528 | if (validate_checksum && csum(data, ip_len) != 0) { | |
1529 | return false; | |
1530 | } | |
1531 | ||
1532 | key->src.addr.ipv4 = ip->ip_src; | |
1533 | key->dst.addr.ipv4 = ip->ip_dst; | |
1534 | key->nw_proto = ip->ip_proto; | |
1535 | ||
1536 | return true; | |
1537 | } | |
1538 | ||
1539 | /* The function stores a pointer to the first byte after the header in | |
1540 | * '*new_data', if 'new_data' is not NULL. If it is NULL, the caller is | |
1541 | * not interested in the header's tail, meaning that the header has | |
1542 | * already been parsed (e.g. by flow_extract): we take this as a hint to | |
1543 | * save a few checks. */ | |
1544 | static inline bool | |
1545 | extract_l3_ipv6(struct conn_key *key, const void *data, size_t size, | |
1546 | const char **new_data) | |
1547 | { | |
1548 | const struct ovs_16aligned_ip6_hdr *ip6 = data; | |
286de272 | 1549 | |
a489b168 DDP |
1550 | if (new_data) { |
1551 | if (OVS_UNLIKELY(size < sizeof *ip6)) { | |
1552 | return false; | |
1553 | } | |
1554 | } | |
1555 | ||
1556 | data = ip6 + 1; | |
1557 | size -= sizeof *ip6; | |
dec0dbbc DB |
1558 | uint8_t nw_proto = ip6->ip6_nxt; |
1559 | uint8_t nw_frag = 0; | |
a489b168 DDP |
1560 | |
1561 | if (!parse_ipv6_ext_hdrs(&data, &size, &nw_proto, &nw_frag)) { | |
1562 | return false; | |
1563 | } | |
1564 | ||
a489b168 DDP |
1565 | if (nw_frag) { |
1566 | return false; | |
1567 | } | |
1568 | ||
c8b1ad49 DB |
1569 | if (new_data) { |
1570 | *new_data = data; | |
1571 | } | |
1572 | ||
a489b168 DDP |
1573 | key->src.addr.ipv6 = ip6->ip6_src; |
1574 | key->dst.addr.ipv6 = ip6->ip6_dst; | |
1575 | key->nw_proto = nw_proto; | |
1576 | ||
1577 | return true; | |
1578 | } | |
1579 | ||
1580 | static inline bool | |
1581 | checksum_valid(const struct conn_key *key, const void *data, size_t size, | |
1582 | const void *l3) | |
1583 | { | |
1584 | uint32_t csum = 0; | |
1585 | ||
1586 | if (key->dl_type == htons(ETH_TYPE_IP)) { | |
1587 | csum = packet_csum_pseudoheader(l3); | |
1588 | } else if (key->dl_type == htons(ETH_TYPE_IPV6)) { | |
1589 | csum = packet_csum_pseudoheader6(l3); | |
1590 | } else { | |
1591 | return false; | |
1592 | } | |
1593 | ||
1594 | csum = csum_continue(csum, data, size); | |
1595 | ||
1596 | return csum_finish(csum) == 0; | |
1597 | } | |
1598 | ||
1599 | static inline bool | |
1600 | check_l4_tcp(const struct conn_key *key, const void *data, size_t size, | |
324459a3 | 1601 | const void *l3, bool validate_checksum) |
a489b168 DDP |
1602 | { |
1603 | const struct tcp_header *tcp = data; | |
40225b0c BP |
1604 | if (size < sizeof *tcp) { |
1605 | return false; | |
1606 | } | |
a489b168 | 1607 | |
40225b0c | 1608 | size_t tcp_len = TCP_OFFSET(tcp->tcp_ctl) * 4; |
a489b168 DDP |
1609 | if (OVS_UNLIKELY(tcp_len < TCP_HEADER_LEN || tcp_len > size)) { |
1610 | return false; | |
1611 | } | |
1612 | ||
324459a3 | 1613 | return validate_checksum ? checksum_valid(key, data, size, l3) : true; |
a489b168 DDP |
1614 | } |
1615 | ||
1616 | static inline bool | |
1617 | check_l4_udp(const struct conn_key *key, const void *data, size_t size, | |
324459a3 | 1618 | const void *l3, bool validate_checksum) |
a489b168 DDP |
1619 | { |
1620 | const struct udp_header *udp = data; | |
40225b0c BP |
1621 | if (size < sizeof *udp) { |
1622 | return false; | |
1623 | } | |
a489b168 | 1624 | |
40225b0c | 1625 | size_t udp_len = ntohs(udp->udp_len); |
a489b168 DDP |
1626 | if (OVS_UNLIKELY(udp_len < UDP_HEADER_LEN || udp_len > size)) { |
1627 | return false; | |
1628 | } | |
1629 | ||
1630 | /* Validation must be skipped if checksum is 0 on IPv4 packets */ | |
1631 | return (udp->udp_csum == 0 && key->dl_type == htons(ETH_TYPE_IP)) | |
324459a3 | 1632 | || (validate_checksum ? checksum_valid(key, data, size, l3) : true); |
a489b168 DDP |
1633 | } |
1634 | ||
1635 | static inline bool | |
324459a3 | 1636 | check_l4_icmp(const void *data, size_t size, bool validate_checksum) |
a489b168 | 1637 | { |
324459a3 | 1638 | return validate_checksum ? csum(data, size) == 0 : true; |
a489b168 DDP |
1639 | } |
1640 | ||
1641 | static inline bool | |
1642 | check_l4_icmp6(const struct conn_key *key, const void *data, size_t size, | |
324459a3 | 1643 | const void *l3, bool validate_checksum) |
a489b168 | 1644 | { |
324459a3 | 1645 | return validate_checksum ? checksum_valid(key, data, size, l3) : true; |
a489b168 DDP |
1646 | } |
1647 | ||
1648 | static inline bool | |
1649 | extract_l4_tcp(struct conn_key *key, const void *data, size_t size) | |
1650 | { | |
a489b168 DDP |
1651 | if (OVS_UNLIKELY(size < TCP_HEADER_LEN)) { |
1652 | return false; | |
1653 | } | |
1654 | ||
dec0dbbc | 1655 | const struct tcp_header *tcp = data; |
a489b168 DDP |
1656 | key->src.port = tcp->tcp_src; |
1657 | key->dst.port = tcp->tcp_dst; | |
1658 | ||
1659 | /* Port 0 is invalid */ | |
1660 | return key->src.port && key->dst.port; | |
1661 | } | |
1662 | ||
1663 | static inline bool | |
1664 | extract_l4_udp(struct conn_key *key, const void *data, size_t size) | |
1665 | { | |
a489b168 DDP |
1666 | if (OVS_UNLIKELY(size < UDP_HEADER_LEN)) { |
1667 | return false; | |
1668 | } | |
1669 | ||
dec0dbbc | 1670 | const struct udp_header *udp = data; |
a489b168 DDP |
1671 | key->src.port = udp->udp_src; |
1672 | key->dst.port = udp->udp_dst; | |
1673 | ||
1674 | /* Port 0 is invalid */ | |
1675 | return key->src.port && key->dst.port; | |
1676 | } | |
1677 | ||
1678 | static inline bool extract_l4(struct conn_key *key, const void *data, | |
324459a3 SC |
1679 | size_t size, bool *related, const void *l3, |
1680 | bool validate_checksum); | |
a489b168 | 1681 | |
b269a122 DDP |
1682 | static uint8_t |
1683 | reverse_icmp_type(uint8_t type) | |
1684 | { | |
1685 | switch (type) { | |
1686 | case ICMP4_ECHO_REQUEST: | |
1687 | return ICMP4_ECHO_REPLY; | |
1688 | case ICMP4_ECHO_REPLY: | |
1689 | return ICMP4_ECHO_REQUEST; | |
1690 | ||
1691 | case ICMP4_TIMESTAMP: | |
1692 | return ICMP4_TIMESTAMPREPLY; | |
1693 | case ICMP4_TIMESTAMPREPLY: | |
1694 | return ICMP4_TIMESTAMP; | |
1695 | ||
1696 | case ICMP4_INFOREQUEST: | |
1697 | return ICMP4_INFOREPLY; | |
1698 | case ICMP4_INFOREPLY: | |
1699 | return ICMP4_INFOREQUEST; | |
1700 | default: | |
1701 | OVS_NOT_REACHED(); | |
1702 | } | |
1703 | } | |
1704 | ||
a489b168 DDP |
1705 | /* If 'related' is not NULL and the function is processing an ICMP |
1706 | * error packet, extract the l3 and l4 fields from the nested header | |
1707 | * instead and set *related to true. If 'related' is NULL we're | |
1708 | * already processing a nested header and no such recursion is | |
1709 | * possible */ | |
1710 | static inline int | |
1711 | extract_l4_icmp(struct conn_key *key, const void *data, size_t size, | |
1712 | bool *related) | |
1713 | { | |
a489b168 DDP |
1714 | if (OVS_UNLIKELY(size < ICMP_HEADER_LEN)) { |
1715 | return false; | |
1716 | } | |
1717 | ||
dec0dbbc DB |
1718 | const struct icmp_header *icmp = data; |
1719 | ||
a489b168 DDP |
1720 | switch (icmp->icmp_type) { |
1721 | case ICMP4_ECHO_REQUEST: | |
1722 | case ICMP4_ECHO_REPLY: | |
1723 | case ICMP4_TIMESTAMP: | |
1724 | case ICMP4_TIMESTAMPREPLY: | |
1725 | case ICMP4_INFOREQUEST: | |
1726 | case ICMP4_INFOREPLY: | |
b269a122 DDP |
1727 | if (icmp->icmp_code != 0) { |
1728 | return false; | |
1729 | } | |
a489b168 | 1730 | /* Separate ICMP connection: identified using id */ |
b269a122 DDP |
1731 | key->src.icmp_id = key->dst.icmp_id = icmp->icmp_fields.echo.id; |
1732 | key->src.icmp_type = icmp->icmp_type; | |
1733 | key->dst.icmp_type = reverse_icmp_type(icmp->icmp_type); | |
a489b168 DDP |
1734 | break; |
1735 | case ICMP4_DST_UNREACH: | |
1736 | case ICMP4_TIME_EXCEEDED: | |
1737 | case ICMP4_PARAM_PROB: | |
1738 | case ICMP4_SOURCEQUENCH: | |
1739 | case ICMP4_REDIRECT: { | |
1740 | /* ICMP packet part of another connection. We should | |
1741 | * extract the key from embedded packet header */ | |
1742 | struct conn_key inner_key; | |
1743 | const char *l3 = (const char *) (icmp + 1); | |
1744 | const char *tail = (const char *) data + size; | |
1745 | const char *l4; | |
a489b168 DDP |
1746 | |
1747 | if (!related) { | |
1748 | return false; | |
1749 | } | |
1750 | ||
1751 | memset(&inner_key, 0, sizeof inner_key); | |
1752 | inner_key.dl_type = htons(ETH_TYPE_IP); | |
dec0dbbc | 1753 | bool ok = extract_l3_ipv4(&inner_key, l3, tail - l3, &l4, false); |
a489b168 DDP |
1754 | if (!ok) { |
1755 | return false; | |
1756 | } | |
1757 | ||
a81da080 | 1758 | if (inner_key.src.addr.ipv4_aligned != key->dst.addr.ipv4_aligned) { |
a489b168 DDP |
1759 | return false; |
1760 | } | |
1761 | ||
1762 | key->src = inner_key.src; | |
1763 | key->dst = inner_key.dst; | |
1764 | key->nw_proto = inner_key.nw_proto; | |
1765 | ||
324459a3 | 1766 | ok = extract_l4(key, l4, tail - l4, NULL, l3, false); |
a489b168 DDP |
1767 | if (ok) { |
1768 | conn_key_reverse(key); | |
1769 | *related = true; | |
1770 | } | |
1771 | return ok; | |
1772 | } | |
1773 | default: | |
1774 | return false; | |
1775 | } | |
1776 | ||
1777 | return true; | |
1778 | } | |
1779 | ||
b269a122 DDP |
1780 | static uint8_t |
1781 | reverse_icmp6_type(uint8_t type) | |
1782 | { | |
1783 | switch (type) { | |
1784 | case ICMP6_ECHO_REQUEST: | |
1785 | return ICMP6_ECHO_REPLY; | |
1786 | case ICMP6_ECHO_REPLY: | |
1787 | return ICMP6_ECHO_REQUEST; | |
1788 | default: | |
1789 | OVS_NOT_REACHED(); | |
1790 | } | |
1791 | } | |
1792 | ||
a489b168 DDP |
1793 | /* If 'related' is not NULL and the function is processing an ICMP |
1794 | * error packet, extract the l3 and l4 fields from the nested header | |
1795 | * instead and set *related to true. If 'related' is NULL we're | |
1796 | * already processing a nested header and no such recursion is | |
1797 | * possible */ | |
1798 | static inline bool | |
1799 | extract_l4_icmp6(struct conn_key *key, const void *data, size_t size, | |
1800 | bool *related) | |
1801 | { | |
1802 | const struct icmp6_header *icmp6 = data; | |
1803 | ||
1804 | /* All the messages that we support need at least 4 bytes after | |
1805 | * the header */ | |
1806 | if (size < sizeof *icmp6 + 4) { | |
1807 | return false; | |
1808 | } | |
1809 | ||
1810 | switch (icmp6->icmp6_type) { | |
1811 | case ICMP6_ECHO_REQUEST: | |
1812 | case ICMP6_ECHO_REPLY: | |
b269a122 DDP |
1813 | if (icmp6->icmp6_code != 0) { |
1814 | return false; | |
1815 | } | |
a489b168 | 1816 | /* Separate ICMP connection: identified using id */ |
b269a122 DDP |
1817 | key->src.icmp_id = key->dst.icmp_id = *(ovs_be16 *) (icmp6 + 1); |
1818 | key->src.icmp_type = icmp6->icmp6_type; | |
1819 | key->dst.icmp_type = reverse_icmp6_type(icmp6->icmp6_type); | |
a489b168 DDP |
1820 | break; |
1821 | case ICMP6_DST_UNREACH: | |
1822 | case ICMP6_PACKET_TOO_BIG: | |
1823 | case ICMP6_TIME_EXCEEDED: | |
1824 | case ICMP6_PARAM_PROB: { | |
1825 | /* ICMP packet part of another connection. We should | |
1826 | * extract the key from embedded packet header */ | |
1827 | struct conn_key inner_key; | |
1828 | const char *l3 = (const char *) icmp6 + 8; | |
1829 | const char *tail = (const char *) data + size; | |
1830 | const char *l4 = NULL; | |
a489b168 DDP |
1831 | |
1832 | if (!related) { | |
1833 | return false; | |
1834 | } | |
1835 | ||
1836 | memset(&inner_key, 0, sizeof inner_key); | |
1837 | inner_key.dl_type = htons(ETH_TYPE_IPV6); | |
dec0dbbc | 1838 | bool ok = extract_l3_ipv6(&inner_key, l3, tail - l3, &l4); |
a489b168 DDP |
1839 | if (!ok) { |
1840 | return false; | |
1841 | } | |
1842 | ||
1843 | /* pf doesn't do this, but it seems a good idea */ | |
1844 | if (!ipv6_addr_equals(&inner_key.src.addr.ipv6_aligned, | |
a81da080 | 1845 | &key->dst.addr.ipv6_aligned)) { |
a489b168 DDP |
1846 | return false; |
1847 | } | |
1848 | ||
1849 | key->src = inner_key.src; | |
1850 | key->dst = inner_key.dst; | |
1851 | key->nw_proto = inner_key.nw_proto; | |
1852 | ||
324459a3 | 1853 | ok = extract_l4(key, l4, tail - l4, NULL, l3, false); |
a489b168 DDP |
1854 | if (ok) { |
1855 | conn_key_reverse(key); | |
1856 | *related = true; | |
1857 | } | |
1858 | return ok; | |
1859 | } | |
1860 | default: | |
1861 | return false; | |
1862 | } | |
1863 | ||
1864 | return true; | |
1865 | } | |
1866 | ||
1867 | /* Extract l4 fields into 'key', which must already contain valid l3 | |
1868 | * members. | |
1869 | * | |
1870 | * If 'related' is not NULL and an ICMP error packet is being | |
1871 | * processed, the function will extract the key from the packet nested | |
1401f6de | 1872 | * in the ICMP payload and set '*related' to true. |
a489b168 DDP |
1873 | * |
1874 | * If 'related' is NULL, it means that we're already parsing a header nested | |
1875 | * in an ICMP error. In this case, we skip checksum and length validation. */ | |
1876 | static inline bool | |
1877 | extract_l4(struct conn_key *key, const void *data, size_t size, bool *related, | |
324459a3 | 1878 | const void *l3, bool validate_checksum) |
a489b168 DDP |
1879 | { |
1880 | if (key->nw_proto == IPPROTO_TCP) { | |
324459a3 SC |
1881 | return (!related || check_l4_tcp(key, data, size, l3, |
1882 | validate_checksum)) && extract_l4_tcp(key, data, size); | |
a489b168 | 1883 | } else if (key->nw_proto == IPPROTO_UDP) { |
324459a3 SC |
1884 | return (!related || check_l4_udp(key, data, size, l3, |
1885 | validate_checksum)) && extract_l4_udp(key, data, size); | |
a489b168 DDP |
1886 | } else if (key->dl_type == htons(ETH_TYPE_IP) |
1887 | && key->nw_proto == IPPROTO_ICMP) { | |
324459a3 | 1888 | return (!related || check_l4_icmp(data, size, validate_checksum)) |
a489b168 DDP |
1889 | && extract_l4_icmp(key, data, size, related); |
1890 | } else if (key->dl_type == htons(ETH_TYPE_IPV6) | |
1891 | && key->nw_proto == IPPROTO_ICMPV6) { | |
324459a3 SC |
1892 | return (!related || check_l4_icmp6(key, data, size, l3, |
1893 | validate_checksum)) && extract_l4_icmp6(key, data, size, | |
1894 | related); | |
a489b168 DDP |
1895 | } else { |
1896 | return false; | |
1897 | } | |
1898 | } | |
1899 | ||
1900 | static bool | |
66e4ad8a | 1901 | conn_key_extract(struct conntrack *ct, struct dp_packet *pkt, ovs_be16 dl_type, |
a489b168 DDP |
1902 | struct conn_lookup_ctx *ctx, uint16_t zone) |
1903 | { | |
2482b0b0 | 1904 | const struct eth_header *l2 = dp_packet_eth(pkt); |
a489b168 DDP |
1905 | const struct ip_header *l3 = dp_packet_l3(pkt); |
1906 | const char *l4 = dp_packet_l4(pkt); | |
a489b168 DDP |
1907 | |
1908 | memset(ctx, 0, sizeof *ctx); | |
1909 | ||
1910 | if (!l2 || !l3 || !l4) { | |
1911 | return false; | |
1912 | } | |
1913 | ||
1914 | ctx->key.zone = zone; | |
1915 | ||
1916 | /* XXX In this function we parse the packet (again, it has already | |
1917 | * gone through miniflow_extract()) for two reasons: | |
1918 | * | |
1919 | * 1) To extract the l3 addresses and l4 ports. | |
1920 | * We already have the l3 and l4 headers' pointers. Extracting | |
1921 | * the l3 addresses and the l4 ports is really cheap, since they | |
1922 | * can be found at fixed locations. | |
66e4ad8a DDP |
1923 | * 2) To extract the l4 type. |
1924 | * Extracting the l4 types, for IPv6 can be quite expensive, because | |
1925 | * it's not at a fixed location. | |
a489b168 DDP |
1926 | * |
1927 | * Here's a way to avoid (2) with the help of the datapath. | |
66e4ad8a | 1928 | * The datapath doesn't keep the packet's extracted flow[1], so |
a489b168 | 1929 | * using that is not an option. We could use the packet's matching |
66e4ad8a DDP |
1930 | * megaflow, but we have to make sure that the l4 type (nw_proto) |
1931 | * is unwildcarded. This means either: | |
a489b168 | 1932 | * |
66e4ad8a DDP |
1933 | * a) dpif-netdev unwildcards the l4 type when a new flow is installed |
1934 | * if the actions contains ct(). | |
a489b168 | 1935 | * |
66e4ad8a DDP |
1936 | * b) ofproto-dpif-xlate unwildcards the l4 type when translating a ct() |
1937 | * action. This is already done in different actions, but it's | |
1938 | * unnecessary for the kernel. | |
a489b168 DDP |
1939 | * |
1940 | * --- | |
66e4ad8a | 1941 | * [1] The reasons for this are that keeping the flow increases |
a489b168 DDP |
1942 | * (slightly) the cache footprint and increases computation |
1943 | * time as we move the packet around. Most importantly, the flow | |
1944 | * should be updated by the actions and this can be slow, as | |
1945 | * we use a sparse representation (miniflow). | |
1946 | * | |
1947 | */ | |
dec0dbbc DB |
1948 | const char *tail = dp_packet_tail(pkt); |
1949 | bool ok; | |
66e4ad8a | 1950 | ctx->key.dl_type = dl_type; |
dec0dbbc | 1951 | |
a489b168 | 1952 | if (ctx->key.dl_type == htons(ETH_TYPE_IP)) { |
dec0dbbc | 1953 | bool hwol_bad_l3_csum = dp_packet_ip_checksum_bad(pkt); |
324459a3 SC |
1954 | if (hwol_bad_l3_csum) { |
1955 | ok = false; | |
1956 | } else { | |
dec0dbbc | 1957 | bool hwol_good_l3_csum = dp_packet_ip_checksum_valid(pkt); |
324459a3 SC |
1958 | /* Validate the checksum only when hwol is not supported. */ |
1959 | ok = extract_l3_ipv4(&ctx->key, l3, tail - (char *) l3, NULL, | |
1960 | !hwol_good_l3_csum); | |
1961 | } | |
a489b168 DDP |
1962 | } else if (ctx->key.dl_type == htons(ETH_TYPE_IPV6)) { |
1963 | ok = extract_l3_ipv6(&ctx->key, l3, tail - (char *) l3, NULL); | |
1964 | } else { | |
1965 | ok = false; | |
1966 | } | |
1967 | ||
1968 | if (ok) { | |
324459a3 SC |
1969 | bool hwol_bad_l4_csum = dp_packet_l4_checksum_bad(pkt); |
1970 | if (!hwol_bad_l4_csum) { | |
1971 | bool hwol_good_l4_csum = dp_packet_l4_checksum_valid(pkt); | |
1972 | /* Validate the checksum only when hwol is not supported. */ | |
1973 | if (extract_l4(&ctx->key, l4, tail - l4, &ctx->icmp_related, l3, | |
1974 | !hwol_good_l4_csum)) { | |
1975 | ctx->hash = conn_key_hash(&ctx->key, ct->hash_basis); | |
1976 | return true; | |
1977 | } | |
a489b168 DDP |
1978 | } |
1979 | } | |
1980 | ||
1981 | return false; | |
1982 | } | |
92edd073 DB |
1983 | |
1984 | static uint32_t | |
1985 | ct_addr_hash_add(uint32_t hash, const struct ct_addr *addr) | |
1986 | { | |
1987 | BUILD_ASSERT_DECL(sizeof *addr % 4 == 0); | |
1988 | return hash_add_bytes32(hash, (const uint32_t *) addr, sizeof *addr); | |
1989 | } | |
1990 | ||
1991 | static uint32_t | |
1992 | ct_endpoint_hash_add(uint32_t hash, const struct ct_endpoint *ep) | |
1993 | { | |
1994 | BUILD_ASSERT_DECL(sizeof *ep % 4 == 0); | |
1995 | return hash_add_bytes32(hash, (const uint32_t *) ep, sizeof *ep); | |
1996 | } | |
a489b168 DDP |
1997 | \f |
1998 | /* Symmetric */ | |
1999 | static uint32_t | |
2000 | conn_key_hash(const struct conn_key *key, uint32_t basis) | |
2001 | { | |
2002 | uint32_t hsrc, hdst, hash; | |
a489b168 | 2003 | hsrc = hdst = basis; |
6b1d4625 DB |
2004 | hsrc = ct_endpoint_hash_add(hsrc, &key->src); |
2005 | hdst = ct_endpoint_hash_add(hdst, &key->dst); | |
a489b168 DDP |
2006 | |
2007 | /* Even if source and destination are swapped the hash will be the same. */ | |
2008 | hash = hsrc ^ hdst; | |
2009 | ||
2010 | /* Hash the rest of the key(L3 and L4 types and zone). */ | |
853cca3f | 2011 | hash = hash_words((uint32_t *) (&key->dst + 1), |
a489b168 DDP |
2012 | (uint32_t *) (key + 1) - (uint32_t *) (&key->dst + 1), |
2013 | hash); | |
2014 | ||
6b1d4625 | 2015 | return hash_finish(hash, 0); |
a489b168 DDP |
2016 | } |
2017 | ||
2018 | static void | |
2019 | conn_key_reverse(struct conn_key *key) | |
2020 | { | |
dec0dbbc | 2021 | struct ct_endpoint tmp = key->src; |
a489b168 DDP |
2022 | key->src = key->dst; |
2023 | key->dst = tmp; | |
2024 | } | |
2025 | ||
286de272 DB |
2026 | static uint32_t |
2027 | nat_ipv6_addrs_delta(struct in6_addr *ipv6_aligned_min, | |
2028 | struct in6_addr *ipv6_aligned_max) | |
2029 | { | |
2030 | uint8_t *ipv6_min_hi = &ipv6_aligned_min->s6_addr[0]; | |
2031 | uint8_t *ipv6_min_lo = &ipv6_aligned_min->s6_addr[0] + sizeof(uint64_t); | |
2032 | uint8_t *ipv6_max_hi = &ipv6_aligned_max->s6_addr[0]; | |
2033 | uint8_t *ipv6_max_lo = &ipv6_aligned_max->s6_addr[0] + sizeof(uint64_t); | |
2034 | ||
2035 | ovs_be64 addr6_64_min_hi; | |
2036 | ovs_be64 addr6_64_min_lo; | |
2037 | memcpy(&addr6_64_min_hi, ipv6_min_hi, sizeof addr6_64_min_hi); | |
2038 | memcpy(&addr6_64_min_lo, ipv6_min_lo, sizeof addr6_64_min_lo); | |
2039 | ||
2040 | ovs_be64 addr6_64_max_hi; | |
2041 | ovs_be64 addr6_64_max_lo; | |
2042 | memcpy(&addr6_64_max_hi, ipv6_max_hi, sizeof addr6_64_max_hi); | |
2043 | memcpy(&addr6_64_max_lo, ipv6_max_lo, sizeof addr6_64_max_lo); | |
2044 | ||
2045 | uint64_t diff; | |
dec0dbbc | 2046 | |
286de272 DB |
2047 | if (addr6_64_min_hi == addr6_64_max_hi && |
2048 | ntohll(addr6_64_min_lo) <= ntohll(addr6_64_max_lo)) { | |
2049 | diff = ntohll(addr6_64_max_lo) - ntohll(addr6_64_min_lo); | |
2050 | } else if (ntohll(addr6_64_min_hi) + 1 == ntohll(addr6_64_max_hi) && | |
2051 | ntohll(addr6_64_min_lo) > ntohll(addr6_64_max_lo)) { | |
2052 | diff = UINT64_MAX - (ntohll(addr6_64_min_lo) - | |
2053 | ntohll(addr6_64_max_lo) - 1); | |
2054 | } else { | |
2055 | /* Limit address delta supported to 32 bits or 4 billion approximately. | |
2056 | * Possibly, this should be visible to the user through a datapath | |
2057 | * support check, however the practical impact is probably nil. */ | |
2058 | diff = 0xfffffffe; | |
2059 | } | |
dec0dbbc | 2060 | |
286de272 DB |
2061 | if (diff > 0xfffffffe) { |
2062 | diff = 0xfffffffe; | |
2063 | } | |
2064 | return diff; | |
2065 | } | |
2066 | ||
2067 | /* This function must be used in tandem with nat_ipv6_addrs_delta(), which | |
2068 | * restricts the input parameters. */ | |
a489b168 | 2069 | static void |
286de272 DB |
2070 | nat_ipv6_addr_increment(struct in6_addr *ipv6_aligned, uint32_t increment) |
2071 | { | |
2072 | uint8_t *ipv6_hi = &ipv6_aligned->s6_addr[0]; | |
2073 | uint8_t *ipv6_lo = &ipv6_aligned->s6_addr[0] + sizeof(ovs_be64); | |
2074 | ovs_be64 addr6_64_hi; | |
2075 | ovs_be64 addr6_64_lo; | |
2076 | memcpy(&addr6_64_hi, ipv6_hi, sizeof addr6_64_hi); | |
2077 | memcpy(&addr6_64_lo, ipv6_lo, sizeof addr6_64_lo); | |
2078 | ||
2079 | if (UINT64_MAX - increment >= ntohll(addr6_64_lo)) { | |
2080 | addr6_64_lo = htonll(increment + ntohll(addr6_64_lo)); | |
2081 | } else if (addr6_64_hi != OVS_BE64_MAX) { | |
2082 | addr6_64_hi = htonll(1 + ntohll(addr6_64_hi)); | |
2083 | addr6_64_lo = htonll(increment - (UINT64_MAX - | |
2084 | ntohll(addr6_64_lo) + 1)); | |
2085 | } else { | |
2086 | OVS_NOT_REACHED(); | |
2087 | } | |
2088 | ||
2089 | memcpy(ipv6_hi, &addr6_64_hi, sizeof addr6_64_hi); | |
2090 | memcpy(ipv6_lo, &addr6_64_lo, sizeof addr6_64_lo); | |
2091 | ||
2092 | return; | |
2093 | } | |
2094 | ||
2095 | static uint32_t | |
2096 | nat_range_hash(const struct conn *conn, uint32_t basis) | |
2097 | { | |
2098 | uint32_t hash = basis; | |
286de272 | 2099 | |
92edd073 DB |
2100 | hash = ct_addr_hash_add(hash, &conn->nat_info->min_addr); |
2101 | hash = ct_addr_hash_add(hash, &conn->nat_info->max_addr); | |
2102 | hash = hash_add(hash, | |
2103 | (conn->nat_info->max_port << 16) | |
2104 | | conn->nat_info->min_port); | |
92edd073 DB |
2105 | hash = ct_endpoint_hash_add(hash, &conn->key.src); |
2106 | hash = ct_endpoint_hash_add(hash, &conn->key.dst); | |
286de272 DB |
2107 | hash = hash_add(hash, (OVS_FORCE uint32_t) conn->key.dl_type); |
2108 | hash = hash_add(hash, conn->key.nw_proto); | |
2109 | hash = hash_add(hash, conn->key.zone); | |
92edd073 DB |
2110 | |
2111 | /* The purpose of the second parameter is to distinguish hashes of data of | |
2112 | * different length; our data always has the same length so there is no | |
2113 | * value in counting. */ | |
2114 | return hash_finish(hash, 0); | |
286de272 DB |
2115 | } |
2116 | ||
2117 | static bool | |
2118 | nat_select_range_tuple(struct conntrack *ct, const struct conn *conn, | |
2119 | struct conn *nat_conn) | |
2120 | { | |
bd5e81a0 DB |
2121 | enum { MIN_NAT_EPHEMERAL_PORT = 1024, |
2122 | MAX_NAT_EPHEMERAL_PORT = 65535 }; | |
286de272 DB |
2123 | |
2124 | uint16_t min_port; | |
2125 | uint16_t max_port; | |
2126 | uint16_t first_port; | |
286de272 DB |
2127 | uint32_t hash = nat_range_hash(conn, ct->hash_basis); |
2128 | ||
2129 | if ((conn->nat_info->nat_action & NAT_ACTION_SRC) && | |
2130 | (!(conn->nat_info->nat_action & NAT_ACTION_SRC_PORT))) { | |
2131 | min_port = ntohs(conn->key.src.port); | |
2132 | max_port = ntohs(conn->key.src.port); | |
2133 | first_port = min_port; | |
2134 | } else if ((conn->nat_info->nat_action & NAT_ACTION_DST) && | |
2135 | (!(conn->nat_info->nat_action & NAT_ACTION_DST_PORT))) { | |
2136 | min_port = ntohs(conn->key.dst.port); | |
2137 | max_port = ntohs(conn->key.dst.port); | |
2138 | first_port = min_port; | |
2139 | } else { | |
2140 | uint16_t deltap = conn->nat_info->max_port - conn->nat_info->min_port; | |
2141 | uint32_t port_index = hash % (deltap + 1); | |
2142 | first_port = conn->nat_info->min_port + port_index; | |
2143 | min_port = conn->nat_info->min_port; | |
2144 | max_port = conn->nat_info->max_port; | |
2145 | } | |
2146 | ||
2147 | uint32_t deltaa = 0; | |
2148 | uint32_t address_index; | |
2149 | struct ct_addr ct_addr; | |
2150 | memset(&ct_addr, 0, sizeof ct_addr); | |
2151 | struct ct_addr max_ct_addr; | |
2152 | memset(&max_ct_addr, 0, sizeof max_ct_addr); | |
2153 | max_ct_addr = conn->nat_info->max_addr; | |
2154 | ||
2155 | if (conn->key.dl_type == htons(ETH_TYPE_IP)) { | |
2156 | deltaa = ntohl(conn->nat_info->max_addr.ipv4_aligned) - | |
2157 | ntohl(conn->nat_info->min_addr.ipv4_aligned); | |
2158 | address_index = hash % (deltaa + 1); | |
2159 | ct_addr.ipv4_aligned = htonl( | |
2160 | ntohl(conn->nat_info->min_addr.ipv4_aligned) + address_index); | |
2161 | } else { | |
2162 | deltaa = nat_ipv6_addrs_delta(&conn->nat_info->min_addr.ipv6_aligned, | |
2163 | &conn->nat_info->max_addr.ipv6_aligned); | |
2164 | /* deltaa must be within 32 bits for full hash coverage. A 64 or | |
2165 | * 128 bit hash is unnecessary and hence not used here. Most code | |
2166 | * is kept common with V4; nat_ipv6_addrs_delta() will do the | |
2167 | * enforcement via max_ct_addr. */ | |
2168 | max_ct_addr = conn->nat_info->min_addr; | |
2169 | nat_ipv6_addr_increment(&max_ct_addr.ipv6_aligned, deltaa); | |
286de272 DB |
2170 | address_index = hash % (deltaa + 1); |
2171 | ct_addr.ipv6_aligned = conn->nat_info->min_addr.ipv6_aligned; | |
2172 | nat_ipv6_addr_increment(&ct_addr.ipv6_aligned, address_index); | |
2173 | } | |
2174 | ||
2175 | uint16_t port = first_port; | |
2176 | bool all_ports_tried = false; | |
2177 | bool original_ports_tried = false; | |
2178 | struct ct_addr first_addr = ct_addr; | |
286de272 DB |
2179 | |
2180 | while (true) { | |
2181 | if (conn->nat_info->nat_action & NAT_ACTION_SRC) { | |
2182 | nat_conn->rev_key.dst.addr = ct_addr; | |
2183 | } else { | |
2184 | nat_conn->rev_key.src.addr = ct_addr; | |
2185 | } | |
2186 | ||
2187 | if ((conn->key.nw_proto == IPPROTO_ICMP) || | |
2188 | (conn->key.nw_proto == IPPROTO_ICMPV6)) { | |
2189 | all_ports_tried = true; | |
2190 | } else if (conn->nat_info->nat_action & NAT_ACTION_SRC) { | |
2191 | nat_conn->rev_key.dst.port = htons(port); | |
2192 | } else { | |
2193 | nat_conn->rev_key.src.port = htons(port); | |
2194 | } | |
2195 | ||
80cee116 DB |
2196 | bool new_insert = nat_conn_keys_insert(&ct->nat_conn_keys, nat_conn, |
2197 | ct->hash_basis); | |
2198 | if (new_insert) { | |
286de272 DB |
2199 | return true; |
2200 | } else if (!all_ports_tried) { | |
2201 | if (min_port == max_port) { | |
2202 | all_ports_tried = true; | |
2203 | } else if (port == max_port) { | |
2204 | port = min_port; | |
2205 | } else { | |
2206 | port++; | |
2207 | } | |
2208 | if (port == first_port) { | |
2209 | all_ports_tried = true; | |
2210 | } | |
2211 | } else { | |
2212 | if (memcmp(&ct_addr, &max_ct_addr, sizeof ct_addr)) { | |
2213 | if (conn->key.dl_type == htons(ETH_TYPE_IP)) { | |
2214 | ct_addr.ipv4_aligned = htonl( | |
2215 | ntohl(ct_addr.ipv4_aligned) + 1); | |
2216 | } else { | |
2217 | nat_ipv6_addr_increment(&ct_addr.ipv6_aligned, 1); | |
2218 | } | |
2219 | } else { | |
2220 | ct_addr = conn->nat_info->min_addr; | |
2221 | } | |
2222 | if (!memcmp(&ct_addr, &first_addr, sizeof ct_addr)) { | |
2223 | if (!original_ports_tried) { | |
2224 | original_ports_tried = true; | |
2225 | ct_addr = conn->nat_info->min_addr; | |
2226 | min_port = MIN_NAT_EPHEMERAL_PORT; | |
2227 | max_port = MAX_NAT_EPHEMERAL_PORT; | |
2228 | } else { | |
2229 | break; | |
2230 | } | |
2231 | } | |
2232 | first_port = min_port; | |
2233 | port = first_port; | |
2234 | all_ports_tried = false; | |
2235 | } | |
2236 | } | |
2237 | return false; | |
2238 | } | |
2239 | ||
ac6abe5f | 2240 | /* This function must be called with the ct->resources lock taken. */ |
286de272 DB |
2241 | static struct nat_conn_key_node * |
2242 | nat_conn_keys_lookup(struct hmap *nat_conn_keys, | |
2243 | const struct conn_key *key, | |
2244 | uint32_t basis) | |
2245 | { | |
2246 | struct nat_conn_key_node *nat_conn_key_node; | |
286de272 | 2247 | |
dec0dbbc DB |
2248 | HMAP_FOR_EACH_WITH_HASH (nat_conn_key_node, node, |
2249 | conn_key_hash(key, basis), nat_conn_keys) { | |
5ed7a0b4 | 2250 | if (!conn_key_cmp(&nat_conn_key_node->key, key)) { |
286de272 DB |
2251 | return nat_conn_key_node; |
2252 | } | |
2253 | } | |
2254 | return NULL; | |
2255 | } | |
2256 | ||
80cee116 DB |
2257 | /* This function must be called with the ct->resources lock taken. */ |
2258 | static bool | |
2259 | nat_conn_keys_insert(struct hmap *nat_conn_keys, const struct conn *nat_conn, | |
2260 | uint32_t basis) | |
2261 | { | |
2262 | struct nat_conn_key_node *nat_conn_key_node = | |
2263 | nat_conn_keys_lookup(nat_conn_keys, &nat_conn->rev_key, basis); | |
2264 | ||
2265 | if (!nat_conn_key_node) { | |
2266 | struct nat_conn_key_node *nat_conn_key = xzalloc(sizeof *nat_conn_key); | |
2267 | nat_conn_key->key = nat_conn->rev_key; | |
2268 | nat_conn_key->value = nat_conn->key; | |
dec0dbbc DB |
2269 | hmap_insert(nat_conn_keys, &nat_conn_key->node, |
2270 | conn_key_hash(&nat_conn_key->key, basis)); | |
80cee116 DB |
2271 | return true; |
2272 | } | |
2273 | return false; | |
2274 | } | |
2275 | ||
ac6abe5f | 2276 | /* This function must be called with the ct->resources write lock taken. */ |
286de272 | 2277 | static void |
bd5e81a0 DB |
2278 | nat_conn_keys_remove(struct hmap *nat_conn_keys, |
2279 | const struct conn_key *key, | |
286de272 DB |
2280 | uint32_t basis) |
2281 | { | |
2282 | struct nat_conn_key_node *nat_conn_key_node; | |
286de272 | 2283 | |
dec0dbbc DB |
2284 | HMAP_FOR_EACH_WITH_HASH (nat_conn_key_node, node, |
2285 | conn_key_hash(key, basis), nat_conn_keys) { | |
5ed7a0b4 | 2286 | if (!conn_key_cmp(&nat_conn_key_node->key, key)) { |
286de272 DB |
2287 | hmap_remove(nat_conn_keys, &nat_conn_key_node->node); |
2288 | free(nat_conn_key_node); | |
2289 | return; | |
2290 | } | |
2291 | } | |
2292 | } | |
2293 | ||
2294 | static void | |
2295 | conn_key_lookup(struct conntrack_bucket *ctb, struct conn_lookup_ctx *ctx, | |
a489b168 | 2296 | long long now) |
ac6abe5f | 2297 | OVS_REQUIRES(ctb->lock) |
a489b168 DDP |
2298 | { |
2299 | uint32_t hash = ctx->hash; | |
2300 | struct conn *conn; | |
2301 | ||
2302 | ctx->conn = NULL; | |
2303 | ||
2304 | HMAP_FOR_EACH_WITH_HASH (conn, node, hash, &ctb->connections) { | |
5ed7a0b4 | 2305 | if (!conn_key_cmp(&conn->key, &ctx->key) |
a489b168 DDP |
2306 | && !conn_expired(conn, now)) { |
2307 | ctx->conn = conn; | |
2308 | ctx->reply = false; | |
2309 | break; | |
2310 | } | |
5ed7a0b4 | 2311 | if (!conn_key_cmp(&conn->rev_key, &ctx->key) |
a489b168 DDP |
2312 | && !conn_expired(conn, now)) { |
2313 | ctx->conn = conn; | |
2314 | ctx->reply = true; | |
2315 | break; | |
2316 | } | |
2317 | } | |
2318 | } | |
2319 | ||
2320 | static enum ct_update_res | |
e6ef6cc6 DDP |
2321 | conn_update(struct conn *conn, struct conntrack_bucket *ctb, |
2322 | struct dp_packet *pkt, bool reply, long long now) | |
a489b168 | 2323 | { |
e6ef6cc6 DDP |
2324 | return l4_protos[conn->key.nw_proto]->conn_update(conn, ctb, pkt, |
2325 | reply, now); | |
a489b168 DDP |
2326 | } |
2327 | ||
2328 | static bool | |
2329 | conn_expired(struct conn *conn, long long now) | |
2330 | { | |
286de272 DB |
2331 | if (conn->conn_type == CT_CONN_TYPE_DEFAULT) { |
2332 | return now >= conn->expiration; | |
2333 | } | |
2334 | return false; | |
a489b168 DDP |
2335 | } |
2336 | ||
2337 | static bool | |
2338 | valid_new(struct dp_packet *pkt, struct conn_key *key) | |
2339 | { | |
2340 | return l4_protos[key->nw_proto]->valid_new(pkt); | |
2341 | } | |
2342 | ||
2343 | static struct conn * | |
e6ef6cc6 DDP |
2344 | new_conn(struct conntrack_bucket *ctb, struct dp_packet *pkt, |
2345 | struct conn_key *key, long long now) | |
a489b168 | 2346 | { |
dec0dbbc | 2347 | struct conn *newconn = l4_protos[key->nw_proto]->new_conn(ctb, pkt, now); |
a489b168 DDP |
2348 | if (newconn) { |
2349 | newconn->key = *key; | |
2350 | } | |
2351 | ||
2352 | return newconn; | |
2353 | } | |
2354 | ||
2355 | static void | |
2356 | delete_conn(struct conn *conn) | |
2357 | { | |
286de272 | 2358 | free(conn->nat_info); |
bd5e81a0 | 2359 | free(conn->alg); |
a489b168 DDP |
2360 | free(conn); |
2361 | } | |
4d4e68ed DDP |
2362 | \f |
2363 | static void | |
2364 | ct_endpoint_to_ct_dpif_inet_addr(const struct ct_addr *a, | |
2365 | union ct_dpif_inet_addr *b, | |
2366 | ovs_be16 dl_type) | |
2367 | { | |
2368 | if (dl_type == htons(ETH_TYPE_IP)) { | |
2369 | b->ip = a->ipv4_aligned; | |
2370 | } else if (dl_type == htons(ETH_TYPE_IPV6)){ | |
2371 | b->in6 = a->ipv6_aligned; | |
2372 | } | |
2373 | } | |
2374 | ||
2375 | static void | |
2376 | conn_key_to_tuple(const struct conn_key *key, struct ct_dpif_tuple *tuple) | |
2377 | { | |
2378 | if (key->dl_type == htons(ETH_TYPE_IP)) { | |
2379 | tuple->l3_type = AF_INET; | |
2380 | } else if (key->dl_type == htons(ETH_TYPE_IPV6)) { | |
2381 | tuple->l3_type = AF_INET6; | |
2382 | } | |
2383 | tuple->ip_proto = key->nw_proto; | |
2384 | ct_endpoint_to_ct_dpif_inet_addr(&key->src.addr, &tuple->src, | |
2385 | key->dl_type); | |
2386 | ct_endpoint_to_ct_dpif_inet_addr(&key->dst.addr, &tuple->dst, | |
2387 | key->dl_type); | |
2388 | ||
2389 | if (key->nw_proto == IPPROTO_ICMP || key->nw_proto == IPPROTO_ICMPV6) { | |
b269a122 DDP |
2390 | tuple->icmp_id = key->src.icmp_id; |
2391 | tuple->icmp_type = key->src.icmp_type; | |
2392 | tuple->icmp_code = key->src.icmp_code; | |
4d4e68ed DDP |
2393 | } else { |
2394 | tuple->src_port = key->src.port; | |
2395 | tuple->dst_port = key->dst.port; | |
2396 | } | |
2397 | } | |
2398 | ||
2399 | static void | |
2400 | conn_to_ct_dpif_entry(const struct conn *conn, struct ct_dpif_entry *entry, | |
ded30c74 | 2401 | long long now, int bkt) |
4d4e68ed | 2402 | { |
4d4e68ed DDP |
2403 | memset(entry, 0, sizeof *entry); |
2404 | conn_key_to_tuple(&conn->key, &entry->tuple_orig); | |
2405 | conn_key_to_tuple(&conn->rev_key, &entry->tuple_reply); | |
2406 | ||
2407 | entry->zone = conn->key.zone; | |
2408 | entry->mark = conn->mark; | |
2409 | ||
286de272 | 2410 | memcpy(&entry->labels, &conn->label, sizeof entry->labels); |
4d4e68ed DDP |
2411 | /* Not implemented yet */ |
2412 | entry->timestamp.start = 0; | |
2413 | entry->timestamp.stop = 0; | |
2414 | ||
dec0dbbc | 2415 | long long expiration = conn->expiration - now; |
4d4e68ed DDP |
2416 | entry->timeout = (expiration > 0) ? expiration / 1000 : 0; |
2417 | ||
dec0dbbc | 2418 | struct ct_l4_proto *class = l4_protos[conn->key.nw_proto]; |
4d4e68ed DDP |
2419 | if (class->conn_get_protoinfo) { |
2420 | class->conn_get_protoinfo(conn, &entry->protoinfo); | |
2421 | } | |
bd5e81a0 | 2422 | |
ded30c74 | 2423 | entry->bkt = bkt; |
bd5e81a0 DB |
2424 | |
2425 | if (conn->alg) { | |
2426 | /* Caller is responsible for freeing. */ | |
2427 | entry->helper.name = xstrdup(conn->alg); | |
2428 | } | |
4d4e68ed DDP |
2429 | } |
2430 | ||
2431 | int | |
2432 | conntrack_dump_start(struct conntrack *ct, struct conntrack_dump *dump, | |
ded30c74 | 2433 | const uint16_t *pzone, int *ptot_bkts) |
4d4e68ed DDP |
2434 | { |
2435 | memset(dump, 0, sizeof(*dump)); | |
dec0dbbc | 2436 | |
4d4e68ed DDP |
2437 | if (pzone) { |
2438 | dump->zone = *pzone; | |
2439 | dump->filter_zone = true; | |
2440 | } | |
4d4e68ed | 2441 | |
dec0dbbc | 2442 | dump->ct = ct; |
ded30c74 | 2443 | *ptot_bkts = CONNTRACK_BUCKETS; |
4d4e68ed DDP |
2444 | return 0; |
2445 | } | |
2446 | ||
2447 | int | |
2448 | conntrack_dump_next(struct conntrack_dump *dump, struct ct_dpif_entry *entry) | |
2449 | { | |
2450 | struct conntrack *ct = dump->ct; | |
2451 | long long now = time_msec(); | |
2452 | ||
2453 | while (dump->bucket < CONNTRACK_BUCKETS) { | |
2454 | struct hmap_node *node; | |
2455 | ||
2456 | ct_lock_lock(&ct->buckets[dump->bucket].lock); | |
2457 | for (;;) { | |
2458 | struct conn *conn; | |
2459 | ||
2460 | node = hmap_at_position(&ct->buckets[dump->bucket].connections, | |
2461 | &dump->bucket_pos); | |
2462 | if (!node) { | |
2463 | break; | |
2464 | } | |
2465 | INIT_CONTAINER(conn, node, node); | |
286de272 DB |
2466 | if ((!dump->filter_zone || conn->key.zone == dump->zone) && |
2467 | (conn->conn_type != CT_CONN_TYPE_UN_NAT)) { | |
ded30c74 | 2468 | conn_to_ct_dpif_entry(conn, entry, now, dump->bucket); |
4d4e68ed DDP |
2469 | break; |
2470 | } | |
2471 | /* Else continue, until we find an entry in the appropriate zone | |
2472 | * or the bucket has been scanned completely. */ | |
2473 | } | |
2474 | ct_lock_unlock(&ct->buckets[dump->bucket].lock); | |
2475 | ||
2476 | if (!node) { | |
2477 | memset(&dump->bucket_pos, 0, sizeof dump->bucket_pos); | |
2478 | dump->bucket++; | |
2479 | } else { | |
2480 | return 0; | |
2481 | } | |
2482 | } | |
2483 | return EOF; | |
2484 | } | |
2485 | ||
2486 | int | |
2487 | conntrack_dump_done(struct conntrack_dump *dump OVS_UNUSED) | |
2488 | { | |
2489 | return 0; | |
2490 | } | |
5d9cbb4c DDP |
2491 | |
2492 | int | |
2493 | conntrack_flush(struct conntrack *ct, const uint16_t *zone) | |
2494 | { | |
dec0dbbc | 2495 | for (unsigned i = 0; i < CONNTRACK_BUCKETS; i++) { |
5d9cbb4c DDP |
2496 | struct conn *conn, *next; |
2497 | ||
2498 | ct_lock_lock(&ct->buckets[i].lock); | |
bd5e81a0 | 2499 | HMAP_FOR_EACH_SAFE (conn, next, node, &ct->buckets[i].connections) { |
286de272 DB |
2500 | if ((!zone || *zone == conn->key.zone) && |
2501 | (conn->conn_type == CT_CONN_TYPE_DEFAULT)) { | |
2502 | conn_clean(ct, conn, &ct->buckets[i]); | |
5d9cbb4c DDP |
2503 | } |
2504 | } | |
2505 | ct_lock_unlock(&ct->buckets[i].lock); | |
2506 | } | |
bd5e81a0 | 2507 | |
5d9cbb4c DDP |
2508 | return 0; |
2509 | } | |
bd5e81a0 | 2510 | |
c92339ad DB |
2511 | int |
2512 | conntrack_set_maxconns(struct conntrack *ct, uint32_t maxconns) | |
2513 | { | |
2514 | atomic_store_relaxed(&ct->n_conn_limit, maxconns); | |
2515 | return 0; | |
2516 | } | |
2517 | ||
2518 | int | |
2519 | conntrack_get_maxconns(struct conntrack *ct, uint32_t *maxconns) | |
2520 | { | |
2521 | atomic_read_relaxed(&ct->n_conn_limit, maxconns); | |
2522 | return 0; | |
2523 | } | |
2524 | ||
875075b3 DB |
2525 | int |
2526 | conntrack_get_nconns(struct conntrack *ct, uint32_t *nconns) | |
2527 | { | |
2528 | *nconns = atomic_count_get(&ct->n_conn); | |
2529 | return 0; | |
2530 | } | |
2531 | ||
bd5e81a0 DB |
2532 | /* This function must be called with the ct->resources read lock taken. */ |
2533 | static struct alg_exp_node * | |
be38342d DB |
2534 | expectation_lookup(struct hmap *alg_expectations, const struct conn_key *key, |
2535 | uint32_t basis, bool src_ip_wc) | |
bd5e81a0 DB |
2536 | { |
2537 | struct conn_key check_key = *key; | |
2538 | check_key.src.port = ALG_WC_SRC_PORT; | |
dec0dbbc | 2539 | |
be38342d DB |
2540 | if (src_ip_wc) { |
2541 | memset(&check_key.src.addr, 0, sizeof check_key.src.addr); | |
2542 | } | |
dec0dbbc | 2543 | |
bd5e81a0 DB |
2544 | struct alg_exp_node *alg_exp_node; |
2545 | ||
bd5e81a0 | 2546 | HMAP_FOR_EACH_WITH_HASH (alg_exp_node, node, |
dec0dbbc | 2547 | conn_key_hash(&check_key, basis), |
bd5e81a0 DB |
2548 | alg_expectations) { |
2549 | if (!conn_key_cmp(&alg_exp_node->key, &check_key)) { | |
2550 | return alg_exp_node; | |
2551 | } | |
2552 | } | |
2553 | return NULL; | |
2554 | } | |
2555 | ||
4417ca3d DB |
2556 | /* This function must be called with the ct->resources write lock taken. */ |
2557 | static void | |
2558 | expectation_remove(struct hmap *alg_expectations, | |
2559 | const struct conn_key *key, uint32_t basis) | |
2560 | { | |
2561 | struct alg_exp_node *alg_exp_node; | |
2562 | ||
2563 | HMAP_FOR_EACH_WITH_HASH (alg_exp_node, node, conn_key_hash(key, basis), | |
2564 | alg_expectations) { | |
2565 | if (!conn_key_cmp(&alg_exp_node->key, key)) { | |
2566 | hmap_remove(alg_expectations, &alg_exp_node->node); | |
2567 | break; | |
2568 | } | |
2569 | } | |
2570 | } | |
2571 | ||
2572 | /* This function must be called with the ct->resources read lock taken. */ | |
2573 | static struct alg_exp_node * | |
2574 | expectation_ref_lookup_unique(const struct hindex *alg_expectation_refs, | |
2575 | const struct conn_key *master_key, | |
2576 | const struct conn_key *alg_exp_key, | |
2577 | uint32_t basis) | |
2578 | { | |
2579 | struct alg_exp_node *alg_exp_node; | |
2580 | ||
2581 | HINDEX_FOR_EACH_WITH_HASH (alg_exp_node, node_ref, | |
2582 | conn_key_hash(master_key, basis), | |
2583 | alg_expectation_refs) { | |
2584 | if (!conn_key_cmp(&alg_exp_node->master_key, master_key) && | |
2585 | !conn_key_cmp(&alg_exp_node->key, alg_exp_key)) { | |
2586 | return alg_exp_node; | |
2587 | } | |
2588 | } | |
2589 | return NULL; | |
2590 | } | |
2591 | ||
2592 | /* This function must be called with the ct->resources write lock taken. */ | |
2593 | static void | |
2594 | expectation_ref_create(struct hindex *alg_expectation_refs, | |
2595 | struct alg_exp_node *alg_exp_node, | |
2596 | uint32_t basis) | |
2597 | { | |
2598 | if (!expectation_ref_lookup_unique(alg_expectation_refs, | |
2599 | &alg_exp_node->master_key, | |
2600 | &alg_exp_node->key, basis)) { | |
2601 | hindex_insert(alg_expectation_refs, &alg_exp_node->node_ref, | |
2602 | conn_key_hash(&alg_exp_node->master_key, basis)); | |
2603 | } | |
2604 | } | |
2605 | ||
2606 | static void | |
2607 | expectation_clean(struct conntrack *ct, const struct conn_key *master_key, | |
2608 | uint32_t basis) | |
2609 | { | |
2610 | ct_rwlock_wrlock(&ct->resources_lock); | |
2611 | ||
2612 | struct alg_exp_node *node, *next; | |
2613 | HINDEX_FOR_EACH_WITH_HASH_SAFE (node, next, node_ref, | |
2614 | conn_key_hash(master_key, basis), | |
2615 | &ct->alg_expectation_refs) { | |
2616 | if (!conn_key_cmp(&node->master_key, master_key)) { | |
2617 | expectation_remove(&ct->alg_expectations, &node->key, basis); | |
2618 | hindex_remove(&ct->alg_expectation_refs, &node->node_ref); | |
2619 | free(node); | |
2620 | } | |
2621 | } | |
2622 | ||
2623 | ct_rwlock_unlock(&ct->resources_lock); | |
2624 | } | |
2625 | ||
bd5e81a0 | 2626 | static void |
be38342d DB |
2627 | expectation_create(struct conntrack *ct, ovs_be16 dst_port, |
2628 | const struct conn *master_conn, bool reply, bool src_ip_wc, | |
2629 | bool skip_nat) | |
bd5e81a0 DB |
2630 | { |
2631 | struct ct_addr src_addr; | |
2632 | struct ct_addr dst_addr; | |
2633 | struct ct_addr alg_nat_repl_addr; | |
be38342d | 2634 | struct alg_exp_node *alg_exp_node = xzalloc(sizeof *alg_exp_node); |
bd5e81a0 | 2635 | |
be38342d | 2636 | if (reply) { |
bd5e81a0 DB |
2637 | src_addr = master_conn->key.src.addr; |
2638 | dst_addr = master_conn->key.dst.addr; | |
be38342d DB |
2639 | if (skip_nat) { |
2640 | alg_nat_repl_addr = dst_addr; | |
2641 | } else { | |
2642 | alg_nat_repl_addr = master_conn->rev_key.dst.addr; | |
2643 | } | |
2644 | alg_exp_node->nat_rpl_dst = true; | |
2645 | } else { | |
2646 | src_addr = master_conn->rev_key.src.addr; | |
2647 | dst_addr = master_conn->rev_key.dst.addr; | |
2648 | if (skip_nat) { | |
2649 | alg_nat_repl_addr = src_addr; | |
2650 | } else { | |
2651 | alg_nat_repl_addr = master_conn->key.src.addr; | |
2652 | } | |
2653 | alg_exp_node->nat_rpl_dst = false; | |
2654 | } | |
2655 | if (src_ip_wc) { | |
2656 | memset(&src_addr, 0, sizeof src_addr); | |
bd5e81a0 DB |
2657 | } |
2658 | ||
bd5e81a0 DB |
2659 | alg_exp_node->key.dl_type = master_conn->key.dl_type; |
2660 | alg_exp_node->key.nw_proto = master_conn->key.nw_proto; | |
2661 | alg_exp_node->key.zone = master_conn->key.zone; | |
2662 | alg_exp_node->key.src.addr = src_addr; | |
2663 | alg_exp_node->key.dst.addr = dst_addr; | |
2664 | alg_exp_node->key.src.port = ALG_WC_SRC_PORT; | |
2665 | alg_exp_node->key.dst.port = dst_port; | |
2666 | alg_exp_node->master_mark = master_conn->mark; | |
2667 | alg_exp_node->master_label = master_conn->label; | |
2668 | alg_exp_node->master_key = master_conn->key; | |
bd5e81a0 DB |
2669 | /* Take the write lock here because it is almost 100% |
2670 | * likely that the lookup will fail and | |
2671 | * expectation_create() will be called below. */ | |
2672 | ct_rwlock_wrlock(&ct->resources_lock); | |
2673 | struct alg_exp_node *alg_exp = expectation_lookup( | |
be38342d | 2674 | &ct->alg_expectations, &alg_exp_node->key, ct->hash_basis, src_ip_wc); |
bd5e81a0 DB |
2675 | if (alg_exp) { |
2676 | free(alg_exp_node); | |
2677 | ct_rwlock_unlock(&ct->resources_lock); | |
2678 | return; | |
2679 | } | |
2680 | ||
2681 | alg_exp_node->alg_nat_repl_addr = alg_nat_repl_addr; | |
4417ca3d | 2682 | hmap_insert(&ct->alg_expectations, &alg_exp_node->node, |
dec0dbbc | 2683 | conn_key_hash(&alg_exp_node->key, ct->hash_basis)); |
4417ca3d DB |
2684 | expectation_ref_create(&ct->alg_expectation_refs, alg_exp_node, |
2685 | ct->hash_basis); | |
bd5e81a0 DB |
2686 | ct_rwlock_unlock(&ct->resources_lock); |
2687 | } | |
2688 | ||
2689 | static uint8_t | |
2690 | get_v4_byte_be(ovs_be32 v4_addr, uint8_t index) | |
2691 | { | |
2692 | uint8_t *byte_ptr = (OVS_FORCE uint8_t *) &v4_addr; | |
2693 | return byte_ptr[index]; | |
2694 | } | |
2695 | ||
2696 | static void | |
2697 | replace_substring(char *substr, uint8_t substr_size, | |
2698 | uint8_t total_size, char *rep_str, | |
2699 | uint8_t rep_str_size) | |
2700 | { | |
2701 | memmove(substr + rep_str_size, substr + substr_size, | |
2702 | total_size - substr_size); | |
2703 | memcpy(substr, rep_str, rep_str_size); | |
2704 | } | |
2705 | ||
2706 | /* Replace IPV4 address in FTP message with NATed address. */ | |
2707 | static int | |
2708 | repl_ftp_v4_addr(struct dp_packet *pkt, ovs_be32 v4_addr_rep, | |
2709 | char *ftp_data_start, | |
2710 | size_t addr_offset_from_ftp_data_start) | |
2711 | { | |
2712 | enum { MAX_FTP_V4_NAT_DELTA = 8 }; | |
2713 | ||
2714 | /* Do conservative check for pathological MTU usage. */ | |
2715 | uint32_t orig_used_size = dp_packet_size(pkt); | |
2716 | uint16_t allocated_size = dp_packet_get_allocated(pkt); | |
2717 | if (orig_used_size + MAX_FTP_V4_NAT_DELTA > allocated_size) { | |
2718 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5); | |
2719 | VLOG_WARN_RL(&rl, "Unsupported effective MTU %u used with FTP", | |
2720 | allocated_size); | |
2721 | return 0; | |
2722 | } | |
2723 | ||
2724 | size_t remain_size = tcp_payload_length(pkt) - | |
2725 | addr_offset_from_ftp_data_start; | |
bd5e81a0 DB |
2726 | int overall_delta = 0; |
2727 | char *byte_str = ftp_data_start + addr_offset_from_ftp_data_start; | |
2728 | ||
2729 | /* Replace the existing IPv4 address by the new one. */ | |
2730 | for (uint8_t i = 0; i < 4; i++) { | |
2731 | /* Find the end of the string for this octet. */ | |
2732 | char *next_delim = memchr(byte_str, ',', 4); | |
2733 | ovs_assert(next_delim); | |
2734 | int substr_size = next_delim - byte_str; | |
2735 | remain_size -= substr_size; | |
2736 | ||
2737 | /* Compose the new string for this octet, and replace it. */ | |
2738 | char rep_str[4]; | |
2739 | uint8_t rep_byte = get_v4_byte_be(v4_addr_rep, i); | |
2740 | int replace_size = sprintf(rep_str, "%d", rep_byte); | |
2741 | replace_substring(byte_str, substr_size, remain_size, | |
2742 | rep_str, replace_size); | |
2743 | overall_delta += replace_size - substr_size; | |
2744 | ||
2745 | /* Advance past the octet and the following comma. */ | |
2746 | byte_str += replace_size + 1; | |
2747 | } | |
2748 | ||
2749 | dp_packet_set_size(pkt, orig_used_size + overall_delta); | |
2750 | return overall_delta; | |
2751 | } | |
2752 | ||
2753 | static char * | |
2754 | skip_non_digits(char *str) | |
2755 | { | |
2756 | while (!isdigit(*str) && *str != 0) { | |
2757 | str++; | |
2758 | } | |
2759 | return str; | |
2760 | } | |
2761 | ||
2762 | static char * | |
2763 | terminate_number_str(char *str, uint8_t max_digits) | |
2764 | { | |
2765 | uint8_t digits_found = 0; | |
2766 | while (isdigit(*str) && digits_found <= max_digits) { | |
2767 | str++; | |
2768 | digits_found++; | |
2769 | } | |
2770 | ||
2771 | *str = 0; | |
2772 | return str; | |
2773 | } | |
2774 | ||
2775 | ||
2776 | static void | |
2777 | get_ftp_ctl_msg(struct dp_packet *pkt, char *ftp_msg) | |
2778 | { | |
2779 | struct tcp_header *th = dp_packet_l4(pkt); | |
2780 | char *tcp_hdr = (char *) th; | |
2781 | uint32_t tcp_payload_len = tcp_payload_length(pkt); | |
2782 | size_t tcp_payload_of_interest = MIN(tcp_payload_len, | |
2783 | LARGEST_FTP_MSG_OF_INTEREST); | |
2784 | size_t tcp_hdr_len = TCP_OFFSET(th->tcp_ctl) * 4; | |
2785 | ||
2786 | ovs_strlcpy(ftp_msg, tcp_hdr + tcp_hdr_len, | |
2787 | tcp_payload_of_interest); | |
2788 | } | |
2789 | ||
2790 | static enum ftp_ctl_pkt | |
2791 | detect_ftp_ctl_type(const struct conn_lookup_ctx *ctx, | |
2792 | struct dp_packet *pkt) | |
2793 | { | |
bd5e81a0 DB |
2794 | char ftp_msg[LARGEST_FTP_MSG_OF_INTEREST + 1] = {0}; |
2795 | get_ftp_ctl_msg(pkt, ftp_msg); | |
dec0dbbc | 2796 | |
bd5e81a0 DB |
2797 | if (ctx->key.dl_type == htons(ETH_TYPE_IPV6)) { |
2798 | if (strncasecmp(ftp_msg, FTP_EPRT_CMD, strlen(FTP_EPRT_CMD)) && | |
2799 | !strcasestr(ftp_msg, FTP_EPSV_REPLY)) { | |
2800 | return CT_FTP_CTL_OTHER; | |
2801 | } | |
2802 | } else { | |
2803 | if (strncasecmp(ftp_msg, FTP_PORT_CMD, strlen(FTP_PORT_CMD)) && | |
2804 | strncasecmp(ftp_msg, FTP_PASV_REPLY_CODE, | |
2805 | strlen(FTP_PASV_REPLY_CODE))) { | |
2806 | return CT_FTP_CTL_OTHER; | |
2807 | } | |
2808 | } | |
2809 | ||
2810 | return CT_FTP_CTL_INTEREST; | |
2811 | } | |
2812 | ||
2813 | static enum ftp_ctl_pkt | |
2814 | process_ftp_ctl_v4(struct conntrack *ct, | |
2815 | struct dp_packet *pkt, | |
2816 | const struct conn *conn_for_expectation, | |
4417ca3d | 2817 | ovs_be32 *v4_addr_rep, |
bd5e81a0 DB |
2818 | char **ftp_data_v4_start, |
2819 | size_t *addr_offset_from_ftp_data_start) | |
2820 | { | |
2821 | struct tcp_header *th = dp_packet_l4(pkt); | |
2822 | size_t tcp_hdr_len = TCP_OFFSET(th->tcp_ctl) * 4; | |
2823 | char *tcp_hdr = (char *) th; | |
2824 | *ftp_data_v4_start = tcp_hdr + tcp_hdr_len; | |
2825 | char ftp_msg[LARGEST_FTP_MSG_OF_INTEREST + 1] = {0}; | |
2826 | get_ftp_ctl_msg(pkt, ftp_msg); | |
bd5e81a0 DB |
2827 | char *ftp = ftp_msg; |
2828 | enum ct_alg_mode mode; | |
dec0dbbc | 2829 | |
23bea975 | 2830 | if (!strncasecmp(ftp, FTP_PORT_CMD, strlen(FTP_PORT_CMD))) { |
bd5e81a0 DB |
2831 | ftp = ftp_msg + strlen(FTP_PORT_CMD); |
2832 | mode = CT_FTP_MODE_ACTIVE; | |
2833 | } else { | |
2834 | ftp = ftp_msg + strlen(FTP_PASV_REPLY_CODE); | |
2835 | mode = CT_FTP_MODE_PASSIVE; | |
2836 | } | |
2837 | ||
2838 | /* Find first space. */ | |
2839 | ftp = strchr(ftp, ' '); | |
2840 | if (!ftp) { | |
2841 | return CT_FTP_CTL_INVALID; | |
2842 | } | |
2843 | ||
2844 | /* Find the first digit, after space. */ | |
2845 | ftp = skip_non_digits(ftp); | |
2846 | if (*ftp == 0) { | |
2847 | return CT_FTP_CTL_INVALID; | |
2848 | } | |
2849 | ||
2850 | char *ip_addr_start = ftp; | |
2851 | *addr_offset_from_ftp_data_start = ip_addr_start - ftp_msg; | |
bd5e81a0 | 2852 | |
dec0dbbc | 2853 | uint8_t comma_count = 0; |
bd5e81a0 DB |
2854 | while (comma_count < 4 && *ftp) { |
2855 | if (*ftp == ',') { | |
2856 | comma_count++; | |
2857 | if (comma_count == 4) { | |
2858 | *ftp = 0; | |
2859 | } else { | |
2860 | *ftp = '.'; | |
2861 | } | |
2862 | } | |
2863 | ftp++; | |
2864 | } | |
2865 | if (comma_count != 4) { | |
2866 | return CT_FTP_CTL_INVALID; | |
2867 | } | |
2868 | ||
2869 | struct in_addr ip_addr; | |
2870 | int rc2 = inet_pton(AF_INET, ip_addr_start, &ip_addr); | |
2871 | if (rc2 != 1) { | |
2872 | return CT_FTP_CTL_INVALID; | |
2873 | } | |
2874 | ||
2875 | char *save_ftp = ftp; | |
2876 | ftp = terminate_number_str(ftp, MAX_FTP_PORT_DGTS); | |
2877 | if (!ftp) { | |
2878 | return CT_FTP_CTL_INVALID; | |
2879 | } | |
2880 | int value; | |
2881 | if (!str_to_int(save_ftp, 10, &value)) { | |
2882 | return CT_FTP_CTL_INVALID; | |
2883 | } | |
2884 | ||
2885 | /* This is derived from the L4 port maximum is 65535. */ | |
2886 | if (value > 255) { | |
2887 | return CT_FTP_CTL_INVALID; | |
2888 | } | |
2889 | ||
2890 | uint16_t port_hs = value; | |
2891 | port_hs <<= 8; | |
2892 | ||
2893 | /* Skip over comma. */ | |
2894 | ftp++; | |
2895 | save_ftp = ftp; | |
2896 | bool digit_found = false; | |
2897 | while (isdigit(*ftp)) { | |
2898 | ftp++; | |
2899 | digit_found = true; | |
2900 | } | |
2901 | if (!digit_found) { | |
2902 | return CT_FTP_CTL_INVALID; | |
2903 | } | |
2904 | *ftp = 0; | |
2905 | if (!str_to_int(save_ftp, 10, &value)) { | |
2906 | return CT_FTP_CTL_INVALID; | |
2907 | } | |
2908 | ||
2909 | if (value > 255) { | |
2910 | return CT_FTP_CTL_INVALID; | |
2911 | } | |
2912 | ||
2913 | uint16_t port_lo_hs = value; | |
2914 | if (65535 - port_hs < port_lo_hs) { | |
2915 | return CT_FTP_CTL_INVALID; | |
2916 | } | |
dec0dbbc | 2917 | |
bd5e81a0 DB |
2918 | port_hs |= port_lo_hs; |
2919 | ovs_be16 port = htons(port_hs); | |
2920 | ovs_be32 conn_ipv4_addr; | |
2921 | ||
2922 | switch (mode) { | |
2923 | case CT_FTP_MODE_ACTIVE: | |
2924 | *v4_addr_rep = conn_for_expectation->rev_key.dst.addr.ipv4_aligned; | |
2925 | conn_ipv4_addr = conn_for_expectation->key.src.addr.ipv4_aligned; | |
2926 | break; | |
2927 | case CT_FTP_MODE_PASSIVE: | |
2928 | *v4_addr_rep = conn_for_expectation->key.dst.addr.ipv4_aligned; | |
2929 | conn_ipv4_addr = conn_for_expectation->rev_key.src.addr.ipv4_aligned; | |
2930 | break; | |
7be77cb0 | 2931 | case CT_TFTP_MODE: |
bd5e81a0 DB |
2932 | default: |
2933 | OVS_NOT_REACHED(); | |
2934 | } | |
2935 | ||
2936 | ovs_be32 ftp_ipv4_addr; | |
2937 | ftp_ipv4_addr = ip_addr.s_addr; | |
2938 | /* Although most servers will block this exploit, there may be some | |
2939 | * less well managed. */ | |
2940 | if (ftp_ipv4_addr != conn_ipv4_addr && ftp_ipv4_addr != *v4_addr_rep) { | |
2941 | return CT_FTP_CTL_INVALID; | |
2942 | } | |
2943 | ||
be38342d DB |
2944 | expectation_create(ct, port, conn_for_expectation, |
2945 | !!(pkt->md.ct_state & CS_REPLY_DIR), false, false); | |
bd5e81a0 DB |
2946 | return CT_FTP_CTL_INTEREST; |
2947 | } | |
2948 | ||
2949 | static char * | |
2950 | skip_ipv6_digits(char *str) | |
2951 | { | |
2952 | while (isxdigit(*str) || *str == ':' || *str == '.') { | |
2953 | str++; | |
2954 | } | |
2955 | return str; | |
2956 | } | |
2957 | ||
2958 | static enum ftp_ctl_pkt | |
2959 | process_ftp_ctl_v6(struct conntrack *ct, | |
2960 | struct dp_packet *pkt, | |
2961 | const struct conn *conn_for_expectation, | |
bd5e81a0 DB |
2962 | struct ct_addr *v6_addr_rep, |
2963 | char **ftp_data_start, | |
2964 | size_t *addr_offset_from_ftp_data_start, | |
2965 | size_t *addr_size, enum ct_alg_mode *mode) | |
2966 | { | |
2967 | struct tcp_header *th = dp_packet_l4(pkt); | |
2968 | size_t tcp_hdr_len = TCP_OFFSET(th->tcp_ctl) * 4; | |
2969 | char *tcp_hdr = (char *) th; | |
2970 | char ftp_msg[LARGEST_FTP_MSG_OF_INTEREST + 1] = {0}; | |
bd5e81a0 DB |
2971 | get_ftp_ctl_msg(pkt, ftp_msg); |
2972 | *ftp_data_start = tcp_hdr + tcp_hdr_len; | |
bd5e81a0 DB |
2973 | char *ftp = ftp_msg; |
2974 | struct in6_addr ip6_addr; | |
dec0dbbc | 2975 | |
23bea975 | 2976 | if (!strncasecmp(ftp, FTP_EPRT_CMD, strlen(FTP_EPRT_CMD))) { |
bd5e81a0 DB |
2977 | ftp = ftp_msg + strlen(FTP_EPRT_CMD); |
2978 | ftp = skip_non_digits(ftp); | |
2979 | if (*ftp != FTP_AF_V6 || isdigit(ftp[1])) { | |
2980 | return CT_FTP_CTL_INVALID; | |
2981 | } | |
2982 | /* Jump over delimiter. */ | |
2983 | ftp += 2; | |
2984 | ||
bd5e81a0 | 2985 | memset(&ip6_addr, 0, sizeof ip6_addr); |
dec0dbbc | 2986 | char *ip_addr_start = ftp; |
bd5e81a0 DB |
2987 | *addr_offset_from_ftp_data_start = ip_addr_start - ftp_msg; |
2988 | ftp = skip_ipv6_digits(ftp); | |
2989 | *ftp = 0; | |
2990 | *addr_size = ftp - ip_addr_start; | |
2991 | int rc2 = inet_pton(AF_INET6, ip_addr_start, &ip6_addr); | |
2992 | if (rc2 != 1) { | |
2993 | return CT_FTP_CTL_INVALID; | |
2994 | } | |
2995 | ftp++; | |
2996 | *mode = CT_FTP_MODE_ACTIVE; | |
2997 | } else { | |
2998 | ftp = ftp_msg + strcspn(ftp_msg, "("); | |
2999 | ftp = skip_non_digits(ftp); | |
3000 | if (!isdigit(*ftp)) { | |
3001 | return CT_FTP_CTL_INVALID; | |
3002 | } | |
3003 | ||
3004 | /* Not used for passive mode. */ | |
3005 | *addr_offset_from_ftp_data_start = 0; | |
3006 | *addr_size = 0; | |
3007 | ||
3008 | *mode = CT_FTP_MODE_PASSIVE; | |
3009 | } | |
3010 | ||
3011 | char *save_ftp = ftp; | |
3012 | ftp = terminate_number_str(ftp, MAX_EXT_FTP_PORT_DGTS); | |
3013 | if (!ftp) { | |
3014 | return CT_FTP_CTL_INVALID; | |
3015 | } | |
dec0dbbc | 3016 | |
bd5e81a0 DB |
3017 | int value; |
3018 | if (!str_to_int(save_ftp, 10, &value)) { | |
3019 | return CT_FTP_CTL_INVALID; | |
3020 | } | |
3021 | if (value > CT_MAX_L4_PORT) { | |
3022 | return CT_FTP_CTL_INVALID; | |
3023 | } | |
3024 | ||
3025 | uint16_t port_hs = value; | |
3026 | ovs_be16 port = htons(port_hs); | |
3027 | ||
3028 | switch (*mode) { | |
3029 | case CT_FTP_MODE_ACTIVE: | |
3030 | *v6_addr_rep = conn_for_expectation->rev_key.dst.addr; | |
3031 | /* Although most servers will block this exploit, there may be some | |
3032 | * less well managed. */ | |
3033 | if (memcmp(&ip6_addr, &v6_addr_rep->ipv6_aligned, sizeof ip6_addr) && | |
3034 | memcmp(&ip6_addr, &conn_for_expectation->key.src.addr.ipv6_aligned, | |
3035 | sizeof ip6_addr)) { | |
3036 | return CT_FTP_CTL_INVALID; | |
3037 | } | |
3038 | break; | |
3039 | case CT_FTP_MODE_PASSIVE: | |
3040 | *v6_addr_rep = conn_for_expectation->key.dst.addr; | |
3041 | break; | |
7be77cb0 | 3042 | case CT_TFTP_MODE: |
bd5e81a0 DB |
3043 | default: |
3044 | OVS_NOT_REACHED(); | |
3045 | } | |
3046 | ||
be38342d DB |
3047 | expectation_create(ct, port, conn_for_expectation, |
3048 | !!(pkt->md.ct_state & CS_REPLY_DIR), false, false); | |
bd5e81a0 DB |
3049 | return CT_FTP_CTL_INTEREST; |
3050 | } | |
3051 | ||
3052 | static int | |
3053 | repl_ftp_v6_addr(struct dp_packet *pkt, struct ct_addr v6_addr_rep, | |
3054 | char *ftp_data_start, | |
3055 | size_t addr_offset_from_ftp_data_start, | |
3056 | size_t addr_size, enum ct_alg_mode mode) | |
3057 | { | |
3058 | /* This is slightly bigger than really possible. */ | |
3059 | enum { MAX_FTP_V6_NAT_DELTA = 45 }; | |
3060 | ||
3061 | if (mode == CT_FTP_MODE_PASSIVE) { | |
3062 | return 0; | |
3063 | } | |
3064 | ||
3065 | /* Do conservative check for pathological MTU usage. */ | |
3066 | uint32_t orig_used_size = dp_packet_size(pkt); | |
3067 | uint16_t allocated_size = dp_packet_get_allocated(pkt); | |
3068 | if (orig_used_size + MAX_FTP_V6_NAT_DELTA > allocated_size) { | |
3069 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5); | |
3070 | VLOG_WARN_RL(&rl, "Unsupported effective MTU %u used with FTP", | |
3071 | allocated_size); | |
3072 | return 0; | |
3073 | } | |
3074 | ||
3075 | const char *rc; | |
3076 | char v6_addr_str[IPV6_SCAN_LEN] = {0}; | |
3077 | rc = inet_ntop(AF_INET6, &v6_addr_rep.ipv6_aligned, v6_addr_str, | |
3078 | IPV6_SCAN_LEN - 1); | |
3079 | ovs_assert(rc != NULL); | |
3080 | ||
3081 | size_t replace_addr_size = strlen(v6_addr_str); | |
3082 | ||
3083 | size_t remain_size = tcp_payload_length(pkt) - | |
3084 | addr_offset_from_ftp_data_start; | |
3085 | ||
3086 | char *pkt_addr_str = ftp_data_start + addr_offset_from_ftp_data_start; | |
3087 | replace_substring(pkt_addr_str, addr_size, remain_size, | |
3088 | v6_addr_str, replace_addr_size); | |
3089 | ||
3090 | int overall_delta = (int) replace_addr_size - (int) addr_size; | |
3091 | ||
3092 | dp_packet_set_size(pkt, orig_used_size + overall_delta); | |
3093 | return overall_delta; | |
3094 | } | |
3095 | ||
3096 | static void | |
3097 | handle_ftp_ctl(struct conntrack *ct, const struct conn_lookup_ctx *ctx, | |
3098 | struct dp_packet *pkt, | |
3099 | const struct conn *conn_for_expectation, | |
3100 | long long now, enum ftp_ctl_pkt ftp_ctl, bool nat) | |
3101 | { | |
3102 | struct ip_header *l3_hdr = dp_packet_l3(pkt); | |
3103 | ovs_be32 v4_addr_rep = 0; | |
3104 | struct ct_addr v6_addr_rep; | |
3105 | size_t addr_offset_from_ftp_data_start; | |
3106 | size_t addr_size = 0; | |
3107 | char *ftp_data_start; | |
3108 | bool do_seq_skew_adj = true; | |
3109 | enum ct_alg_mode mode = CT_FTP_MODE_ACTIVE; | |
3110 | ||
3111 | if (detect_ftp_ctl_type(ctx, pkt) != ftp_ctl) { | |
3112 | return; | |
3113 | } | |
3114 | ||
3115 | if (!nat || !conn_for_expectation->seq_skew) { | |
3116 | do_seq_skew_adj = false; | |
3117 | } | |
3118 | ||
3119 | struct ovs_16aligned_ip6_hdr *nh6 = dp_packet_l3(pkt); | |
3120 | int64_t seq_skew = 0; | |
dec0dbbc | 3121 | |
bd5e81a0 DB |
3122 | if (ftp_ctl == CT_FTP_CTL_OTHER) { |
3123 | seq_skew = conn_for_expectation->seq_skew; | |
bd5e81a0 DB |
3124 | } else if (ftp_ctl == CT_FTP_CTL_INTEREST) { |
3125 | enum ftp_ctl_pkt rc; | |
3126 | if (ctx->key.dl_type == htons(ETH_TYPE_IPV6)) { | |
3127 | rc = process_ftp_ctl_v6(ct, pkt, conn_for_expectation, | |
4417ca3d | 3128 | &v6_addr_rep, &ftp_data_start, |
bd5e81a0 DB |
3129 | &addr_offset_from_ftp_data_start, |
3130 | &addr_size, &mode); | |
3131 | } else { | |
3132 | rc = process_ftp_ctl_v4(ct, pkt, conn_for_expectation, | |
4417ca3d | 3133 | &v4_addr_rep, &ftp_data_start, |
bd5e81a0 DB |
3134 | &addr_offset_from_ftp_data_start); |
3135 | } | |
3136 | if (rc == CT_FTP_CTL_INVALID) { | |
3137 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5); | |
3138 | VLOG_WARN_RL(&rl, "Invalid FTP control packet format"); | |
3139 | pkt->md.ct_state |= CS_TRACKED | CS_INVALID; | |
3140 | return; | |
3141 | } else if (rc == CT_FTP_CTL_INTEREST) { | |
3142 | uint16_t ip_len; | |
dec0dbbc | 3143 | |
bd5e81a0 DB |
3144 | if (ctx->key.dl_type == htons(ETH_TYPE_IPV6)) { |
3145 | seq_skew = repl_ftp_v6_addr(pkt, v6_addr_rep, ftp_data_start, | |
3146 | addr_offset_from_ftp_data_start, | |
3147 | addr_size, mode); | |
bd5e81a0 DB |
3148 | if (seq_skew) { |
3149 | ip_len = ntohs(nh6->ip6_ctlun.ip6_un1.ip6_un1_plen); | |
3150 | ip_len += seq_skew; | |
3151 | nh6->ip6_ctlun.ip6_un1.ip6_un1_plen = htons(ip_len); | |
3152 | conn_seq_skew_set(ct, &conn_for_expectation->key, now, | |
23bea975 | 3153 | seq_skew, ctx->reply); |
bd5e81a0 DB |
3154 | } |
3155 | } else { | |
3156 | seq_skew = repl_ftp_v4_addr(pkt, v4_addr_rep, ftp_data_start, | |
3157 | addr_offset_from_ftp_data_start); | |
bd5e81a0 DB |
3158 | ip_len = ntohs(l3_hdr->ip_tot_len); |
3159 | if (seq_skew) { | |
3160 | ip_len += seq_skew; | |
3161 | l3_hdr->ip_csum = recalc_csum16(l3_hdr->ip_csum, | |
3162 | l3_hdr->ip_tot_len, htons(ip_len)); | |
3163 | l3_hdr->ip_tot_len = htons(ip_len); | |
3164 | conn_seq_skew_set(ct, &conn_for_expectation->key, now, | |
23bea975 | 3165 | seq_skew, ctx->reply); |
bd5e81a0 DB |
3166 | } |
3167 | } | |
3168 | } else { | |
3169 | OVS_NOT_REACHED(); | |
3170 | } | |
3171 | } else { | |
3172 | OVS_NOT_REACHED(); | |
3173 | } | |
3174 | ||
3175 | struct tcp_header *th = dp_packet_l4(pkt); | |
dec0dbbc | 3176 | |
bd5e81a0 DB |
3177 | if (do_seq_skew_adj && seq_skew != 0) { |
3178 | if (ctx->reply != conn_for_expectation->seq_skew_dir) { | |
3179 | ||
3180 | uint32_t tcp_ack = ntohl(get_16aligned_be32(&th->tcp_ack)); | |
3181 | ||
3182 | if ((seq_skew > 0) && (tcp_ack < seq_skew)) { | |
3183 | /* Should not be possible; will be marked invalid. */ | |
3184 | tcp_ack = 0; | |
3185 | } else if ((seq_skew < 0) && (UINT32_MAX - tcp_ack < -seq_skew)) { | |
3186 | tcp_ack = (-seq_skew) - (UINT32_MAX - tcp_ack); | |
3187 | } else { | |
3188 | tcp_ack -= seq_skew; | |
3189 | } | |
3190 | ovs_be32 new_tcp_ack = htonl(tcp_ack); | |
3191 | put_16aligned_be32(&th->tcp_ack, new_tcp_ack); | |
3192 | } else { | |
3193 | uint32_t tcp_seq = ntohl(get_16aligned_be32(&th->tcp_seq)); | |
3194 | if ((seq_skew > 0) && (UINT32_MAX - tcp_seq < seq_skew)) { | |
3195 | tcp_seq = seq_skew - (UINT32_MAX - tcp_seq); | |
3196 | } else if ((seq_skew < 0) && (tcp_seq < -seq_skew)) { | |
3197 | /* Should not be possible; will be marked invalid. */ | |
3198 | tcp_seq = 0; | |
3199 | } else { | |
3200 | tcp_seq += seq_skew; | |
3201 | } | |
3202 | ovs_be32 new_tcp_seq = htonl(tcp_seq); | |
3203 | put_16aligned_be32(&th->tcp_seq, new_tcp_seq); | |
3204 | } | |
3205 | } | |
3206 | ||
bd5e81a0 DB |
3207 | th->tcp_csum = 0; |
3208 | uint32_t tcp_csum; | |
3209 | if (ctx->key.dl_type == htons(ETH_TYPE_IPV6)) { | |
3210 | tcp_csum = packet_csum_pseudoheader6(nh6); | |
3211 | } else { | |
3212 | tcp_csum = packet_csum_pseudoheader(l3_hdr); | |
3213 | } | |
dec0dbbc DB |
3214 | const char *tail = dp_packet_tail(pkt); |
3215 | uint8_t pad = dp_packet_l2_pad_size(pkt); | |
bd5e81a0 DB |
3216 | th->tcp_csum = csum_finish( |
3217 | csum_continue(tcp_csum, th, tail - (char *) th - pad)); | |
3218 | return; | |
3219 | } | |
7be77cb0 DB |
3220 | |
3221 | static void | |
3222 | handle_tftp_ctl(struct conntrack *ct, | |
94e71143 | 3223 | const struct conn_lookup_ctx *ctx OVS_UNUSED, |
be38342d | 3224 | struct dp_packet *pkt, |
7be77cb0 | 3225 | const struct conn *conn_for_expectation, |
4417ca3d DB |
3226 | long long now OVS_UNUSED, |
3227 | enum ftp_ctl_pkt ftp_ctl OVS_UNUSED, bool nat OVS_UNUSED) | |
7be77cb0 | 3228 | { |
be38342d DB |
3229 | expectation_create(ct, conn_for_expectation->key.src.port, |
3230 | conn_for_expectation, | |
3231 | !!(pkt->md.ct_state & CS_REPLY_DIR), false, false); | |
7be77cb0 DB |
3232 | return; |
3233 | } |