]>
Commit | Line | Data |
---|---|---|
a94ebc39 JS |
1 | /* |
2 | * Copyright (c) 2015 Nicira, Inc. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of version 2 of the GNU General Public | |
6 | * License as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | */ | |
13 | ||
14 | #include <linux/kconfig.h> | |
15 | #include <linux/version.h> | |
16 | ||
8063e095 | 17 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
a94ebc39 JS |
18 | |
19 | #include <linux/module.h> | |
20 | #include <linux/openvswitch.h> | |
f8f97cdc JR |
21 | #include <linux/tcp.h> |
22 | #include <linux/udp.h> | |
23 | #include <linux/sctp.h> | |
a94ebc39 JS |
24 | #include <net/ip.h> |
25 | #include <net/netfilter/nf_conntrack_core.h> | |
11251c17 | 26 | #include <net/netfilter/nf_conntrack_helper.h> |
038e34ab | 27 | #include <net/netfilter/nf_conntrack_labels.h> |
f8f97cdc | 28 | #include <net/netfilter/nf_conntrack_seqadj.h> |
a94ebc39 JS |
29 | #include <net/netfilter/nf_conntrack_zones.h> |
30 | #include <net/netfilter/ipv6/nf_defrag_ipv6.h> | |
31 | ||
f8f97cdc JR |
32 | #ifdef CONFIG_NF_NAT_NEEDED |
33 | #include <linux/netfilter/nf_nat.h> | |
34 | #include <net/netfilter/nf_nat_core.h> | |
35 | #include <net/netfilter/nf_nat_l3proto.h> | |
36 | #endif | |
37 | ||
a94ebc39 JS |
38 | #include "datapath.h" |
39 | #include "conntrack.h" | |
40 | #include "flow.h" | |
41 | #include "flow_netlink.h" | |
86c2eb45 | 42 | #include "gso.h" |
a94ebc39 JS |
43 | |
44 | struct ovs_ct_len_tbl { | |
f8f97cdc JR |
45 | int maxlen; |
46 | int minlen; | |
a94ebc39 JS |
47 | }; |
48 | ||
372ce973 JS |
49 | /* Metadata mark for masked write to conntrack mark */ |
50 | struct md_mark { | |
51 | u32 value; | |
52 | u32 mask; | |
53 | }; | |
54 | ||
038e34ab | 55 | /* Metadata label for masked write to conntrack label. */ |
c05e2094 JS |
56 | struct md_labels { |
57 | struct ovs_key_ct_labels value; | |
58 | struct ovs_key_ct_labels mask; | |
038e34ab JS |
59 | }; |
60 | ||
f8f97cdc JR |
61 | enum ovs_ct_nat { |
62 | OVS_CT_NAT = 1 << 0, /* NAT for committed connections only. */ | |
63 | OVS_CT_SRC_NAT = 1 << 1, /* Source NAT for NEW connections. */ | |
64 | OVS_CT_DST_NAT = 1 << 2, /* Destination NAT for NEW connections. */ | |
65 | }; | |
66 | ||
a94ebc39 JS |
67 | /* Conntrack action context for execution. */ |
68 | struct ovs_conntrack_info { | |
11251c17 | 69 | struct nf_conntrack_helper *helper; |
a94ebc39 JS |
70 | struct nf_conntrack_zone zone; |
71 | struct nf_conn *ct; | |
c05e2094 | 72 | u8 commit : 1; |
f8f97cdc | 73 | u8 nat : 3; /* enum ovs_ct_nat */ |
9f1de150 | 74 | u8 random_fully_compat : 1; /* bool */ |
a94ebc39 | 75 | u16 family; |
372ce973 | 76 | struct md_mark mark; |
c05e2094 | 77 | struct md_labels labels; |
f8f97cdc JR |
78 | #ifdef CONFIG_NF_NAT_NEEDED |
79 | struct nf_nat_range range; /* Only present for SRC NAT and DST NAT. */ | |
80 | #endif | |
a94ebc39 JS |
81 | }; |
82 | ||
11251c17 JS |
83 | static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info); |
84 | ||
a94ebc39 JS |
85 | static u16 key_to_nfproto(const struct sw_flow_key *key) |
86 | { | |
87 | switch (ntohs(key->eth.type)) { | |
88 | case ETH_P_IP: | |
89 | return NFPROTO_IPV4; | |
90 | case ETH_P_IPV6: | |
91 | return NFPROTO_IPV6; | |
92 | default: | |
93 | return NFPROTO_UNSPEC; | |
94 | } | |
95 | } | |
96 | ||
97 | /* Map SKB connection state into the values used by flow definition. */ | |
98 | static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo) | |
99 | { | |
100 | u8 ct_state = OVS_CS_F_TRACKED; | |
101 | ||
102 | switch (ctinfo) { | |
103 | case IP_CT_ESTABLISHED_REPLY: | |
104 | case IP_CT_RELATED_REPLY: | |
a94ebc39 JS |
105 | ct_state |= OVS_CS_F_REPLY_DIR; |
106 | break; | |
107 | default: | |
108 | break; | |
109 | } | |
110 | ||
111 | switch (ctinfo) { | |
112 | case IP_CT_ESTABLISHED: | |
113 | case IP_CT_ESTABLISHED_REPLY: | |
114 | ct_state |= OVS_CS_F_ESTABLISHED; | |
115 | break; | |
116 | case IP_CT_RELATED: | |
117 | case IP_CT_RELATED_REPLY: | |
118 | ct_state |= OVS_CS_F_RELATED; | |
119 | break; | |
120 | case IP_CT_NEW: | |
a94ebc39 JS |
121 | ct_state |= OVS_CS_F_NEW; |
122 | break; | |
123 | default: | |
124 | break; | |
125 | } | |
126 | ||
127 | return ct_state; | |
128 | } | |
129 | ||
c05e2094 JS |
130 | static u32 ovs_ct_get_mark(const struct nf_conn *ct) |
131 | { | |
132 | #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) | |
133 | return ct ? ct->mark : 0; | |
134 | #else | |
135 | return 0; | |
136 | #endif | |
137 | } | |
138 | ||
139 | static void ovs_ct_get_labels(const struct nf_conn *ct, | |
140 | struct ovs_key_ct_labels *labels) | |
038e34ab JS |
141 | { |
142 | struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL; | |
143 | ||
144 | if (cl) { | |
145 | size_t len = cl->words * sizeof(long); | |
146 | ||
c05e2094 JS |
147 | if (len > OVS_CT_LABELS_LEN) |
148 | len = OVS_CT_LABELS_LEN; | |
149 | else if (len < OVS_CT_LABELS_LEN) | |
150 | memset(labels, 0, OVS_CT_LABELS_LEN); | |
151 | memcpy(labels, cl->bits, len); | |
038e34ab | 152 | } else { |
c05e2094 | 153 | memset(labels, 0, OVS_CT_LABELS_LEN); |
038e34ab JS |
154 | } |
155 | } | |
156 | ||
a94ebc39 | 157 | static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state, |
372ce973 JS |
158 | const struct nf_conntrack_zone *zone, |
159 | const struct nf_conn *ct) | |
a94ebc39 JS |
160 | { |
161 | key->ct.state = state; | |
162 | key->ct.zone = zone->id; | |
c05e2094 JS |
163 | key->ct.mark = ovs_ct_get_mark(ct); |
164 | ovs_ct_get_labels(ct, &key->ct.labels); | |
a94ebc39 JS |
165 | } |
166 | ||
f8f97cdc JR |
167 | /* Update 'key' based on skb->nfct. If 'post_ct' is true, then OVS has |
168 | * previously sent the packet to conntrack via the ct action. If | |
169 | * 'keep_nat_flags' is true, the existing NAT flags retained, else they are | |
170 | * initialized from the connection status. | |
a94ebc39 JS |
171 | */ |
172 | static void ovs_ct_update_key(const struct sk_buff *skb, | |
f23593a1 | 173 | const struct ovs_conntrack_info *info, |
f8f97cdc JR |
174 | struct sw_flow_key *key, bool post_ct, |
175 | bool keep_nat_flags) | |
a94ebc39 JS |
176 | { |
177 | const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt; | |
178 | enum ip_conntrack_info ctinfo; | |
179 | struct nf_conn *ct; | |
180 | u8 state = 0; | |
181 | ||
182 | ct = nf_ct_get(skb, &ctinfo); | |
183 | if (ct) { | |
184 | state = ovs_ct_get_state(ctinfo); | |
b0f251cd | 185 | /* All unconfirmed entries are NEW connections. */ |
c05e2094 JS |
186 | if (!nf_ct_is_confirmed(ct)) |
187 | state |= OVS_CS_F_NEW; | |
b0f251cd JR |
188 | /* OVS persists the related flag for the duration of the |
189 | * connection. | |
190 | */ | |
a94ebc39 JS |
191 | if (ct->master) |
192 | state |= OVS_CS_F_RELATED; | |
f8f97cdc JR |
193 | if (keep_nat_flags) { |
194 | state |= key->ct.state & OVS_CS_F_NAT_MASK; | |
195 | } else { | |
196 | if (ct->status & IPS_SRC_NAT) | |
197 | state |= OVS_CS_F_SRC_NAT; | |
198 | if (ct->status & IPS_DST_NAT) | |
199 | state |= OVS_CS_F_DST_NAT; | |
200 | } | |
a94ebc39 JS |
201 | zone = nf_ct_zone(ct); |
202 | } else if (post_ct) { | |
203 | state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID; | |
f23593a1 JS |
204 | if (info) |
205 | zone = &info->zone; | |
a94ebc39 | 206 | } |
372ce973 | 207 | __ovs_ct_update_key(key, state, zone, ct); |
a94ebc39 JS |
208 | } |
209 | ||
b0f251cd JR |
210 | /* This is called to initialize CT key fields possibly coming in from the local |
211 | * stack. | |
212 | */ | |
a94ebc39 JS |
213 | void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key) |
214 | { | |
f8f97cdc | 215 | ovs_ct_update_key(skb, NULL, key, false, false); |
a94ebc39 JS |
216 | } |
217 | ||
218 | int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb) | |
219 | { | |
c05e2094 | 220 | if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state)) |
a94ebc39 JS |
221 | return -EMSGSIZE; |
222 | ||
223 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && | |
224 | nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone)) | |
225 | return -EMSGSIZE; | |
226 | ||
372ce973 JS |
227 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && |
228 | nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark)) | |
229 | return -EMSGSIZE; | |
230 | ||
c05e2094 JS |
231 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && |
232 | nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(key->ct.labels), | |
233 | &key->ct.labels)) | |
038e34ab JS |
234 | return -EMSGSIZE; |
235 | ||
372ce973 JS |
236 | return 0; |
237 | } | |
238 | ||
239 | static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key, | |
240 | u32 ct_mark, u32 mask) | |
241 | { | |
c05e2094 | 242 | #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) |
372ce973 JS |
243 | enum ip_conntrack_info ctinfo; |
244 | struct nf_conn *ct; | |
245 | u32 new_mark; | |
246 | ||
372ce973 JS |
247 | /* The connection could be invalid, in which case set_mark is no-op. */ |
248 | ct = nf_ct_get(skb, &ctinfo); | |
249 | if (!ct) | |
250 | return 0; | |
251 | ||
252 | new_mark = ct_mark | (ct->mark & ~(mask)); | |
253 | if (ct->mark != new_mark) { | |
254 | ct->mark = new_mark; | |
255 | nf_conntrack_event_cache(IPCT_MARK, ct); | |
256 | key->ct.mark = new_mark; | |
257 | } | |
258 | ||
a94ebc39 | 259 | return 0; |
c05e2094 JS |
260 | #else |
261 | return -ENOTSUPP; | |
262 | #endif | |
a94ebc39 JS |
263 | } |
264 | ||
c05e2094 JS |
265 | static int ovs_ct_set_labels(struct sk_buff *skb, struct sw_flow_key *key, |
266 | const struct ovs_key_ct_labels *labels, | |
267 | const struct ovs_key_ct_labels *mask) | |
038e34ab JS |
268 | { |
269 | enum ip_conntrack_info ctinfo; | |
270 | struct nf_conn_labels *cl; | |
271 | struct nf_conn *ct; | |
272 | int err; | |
273 | ||
038e34ab JS |
274 | /* The connection could be invalid, in which case set_label is no-op.*/ |
275 | ct = nf_ct_get(skb, &ctinfo); | |
276 | if (!ct) | |
277 | return 0; | |
278 | ||
279 | cl = nf_ct_labels_find(ct); | |
280 | if (!cl) { | |
281 | nf_ct_labels_ext_add(ct); | |
282 | cl = nf_ct_labels_find(ct); | |
283 | } | |
c05e2094 | 284 | if (!cl || cl->words * sizeof(long) < OVS_CT_LABELS_LEN) |
038e34ab JS |
285 | return -ENOSPC; |
286 | ||
c05e2094 JS |
287 | err = nf_connlabels_replace(ct, (u32 *)labels, (u32 *)mask, |
288 | OVS_CT_LABELS_LEN / sizeof(u32)); | |
038e34ab JS |
289 | if (err) |
290 | return err; | |
291 | ||
c05e2094 | 292 | ovs_ct_get_labels(ct, &key->ct.labels); |
038e34ab JS |
293 | return 0; |
294 | } | |
295 | ||
11251c17 JS |
296 | /* 'skb' should already be pulled to nh_ofs. */ |
297 | static int ovs_ct_helper(struct sk_buff *skb, u16 proto) | |
298 | { | |
299 | const struct nf_conntrack_helper *helper; | |
300 | const struct nf_conn_help *help; | |
301 | enum ip_conntrack_info ctinfo; | |
302 | unsigned int protoff; | |
303 | struct nf_conn *ct; | |
4cc85f28 | 304 | u8 nexthdr; |
f8f97cdc | 305 | int err; |
11251c17 JS |
306 | |
307 | ct = nf_ct_get(skb, &ctinfo); | |
308 | if (!ct || ctinfo == IP_CT_RELATED_REPLY) | |
309 | return NF_ACCEPT; | |
310 | ||
311 | help = nfct_help(ct); | |
312 | if (!help) | |
313 | return NF_ACCEPT; | |
314 | ||
315 | helper = rcu_dereference(help->helper); | |
316 | if (!helper) | |
317 | return NF_ACCEPT; | |
318 | ||
319 | switch (proto) { | |
320 | case NFPROTO_IPV4: | |
321 | protoff = ip_hdrlen(skb); | |
322 | break; | |
323 | case NFPROTO_IPV6: { | |
11251c17 | 324 | __be16 frag_off; |
c05e2094 | 325 | int ofs; |
11251c17 | 326 | |
4cc85f28 | 327 | nexthdr = ipv6_hdr(skb)->nexthdr; |
c05e2094 JS |
328 | ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr, |
329 | &frag_off); | |
330 | if (ofs < 0 || (frag_off & htons(~0x7)) != 0) { | |
11251c17 JS |
331 | pr_debug("proto header not found\n"); |
332 | return NF_ACCEPT; | |
333 | } | |
c05e2094 | 334 | protoff = ofs; |
11251c17 JS |
335 | break; |
336 | } | |
337 | default: | |
338 | WARN_ONCE(1, "helper invoked on non-IP family!"); | |
339 | return NF_DROP; | |
340 | } | |
341 | ||
4cc85f28 JR |
342 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0) |
343 | /* Linux 4.5 and older depend on skb_dst being set when recalculating | |
344 | * checksums after NAT helper has mangled TCP or UDP packet payload. | |
345 | * This dependency is avoided when skb is CHECKSUM_PARTIAL or when UDP | |
346 | * has no checksum. | |
347 | * | |
348 | * The dependency is not triggered when the main NAT code updates | |
349 | * checksums after translating the IP header (address, port), so this | |
350 | * fix only needs to be executed on packets that are both being NATted | |
351 | * and that have a helper assigned. | |
352 | */ | |
353 | if (ct->status & IPS_NAT_MASK && skb->ip_summed != CHECKSUM_PARTIAL) { | |
354 | u8 ipproto = (proto == NFPROTO_IPV4) | |
355 | ? ip_hdr(skb)->protocol : nexthdr; | |
356 | u16 offset = 0; | |
357 | ||
358 | switch (ipproto) { | |
359 | case IPPROTO_TCP: | |
360 | offset = offsetof(struct tcphdr, check); | |
361 | break; | |
362 | case IPPROTO_UDP: | |
363 | /* Skip if no csum. */ | |
364 | if (udp_hdr(skb)->check) | |
365 | offset = offsetof(struct udphdr, check); | |
366 | break; | |
367 | } | |
368 | if (offset) { | |
369 | if (unlikely(!pskb_may_pull(skb, protoff + offset + 2))) | |
370 | return NF_DROP; | |
371 | ||
372 | skb->csum_start = skb_headroom(skb) + protoff; | |
373 | skb->csum_offset = offset; | |
374 | skb->ip_summed = CHECKSUM_PARTIAL; | |
375 | } | |
376 | } | |
377 | #endif | |
f8f97cdc JR |
378 | err = helper->help(skb, protoff, ct, ctinfo); |
379 | if (err != NF_ACCEPT) | |
380 | return err; | |
381 | ||
382 | /* Adjust seqs after helper. This is needed due to some helpers (e.g., | |
383 | * FTP with NAT) adusting the TCP payload size when mangling IP | |
384 | * addresses and/or port numbers in the text-based control connection. | |
385 | */ | |
386 | if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) && | |
387 | !nf_ct_seq_adjust(skb, ct, ctinfo, protoff)) | |
388 | return NF_DROP; | |
389 | return NF_ACCEPT; | |
11251c17 JS |
390 | } |
391 | ||
c05e2094 JS |
392 | /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero |
393 | * value if 'skb' is freed. | |
394 | */ | |
a94ebc39 JS |
395 | static int handle_fragments(struct net *net, struct sw_flow_key *key, |
396 | u16 zone, struct sk_buff *skb) | |
397 | { | |
86c2eb45 | 398 | struct ovs_gso_cb ovs_cb = *OVS_GSO_CB(skb); |
2e602ea3 | 399 | int err; |
a94ebc39 | 400 | |
a94ebc39 JS |
401 | if (key->eth.type == htons(ETH_P_IP)) { |
402 | enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone; | |
a94ebc39 JS |
403 | |
404 | memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); | |
39c0ff22 | 405 | err = ip_defrag(net, skb, user); |
a94ebc39 JS |
406 | if (err) |
407 | return err; | |
408 | ||
86c2eb45 | 409 | ovs_cb.dp_cb.mru = IPCB(skb)->frag_max_size; |
a94ebc39 | 410 | #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) |
c05e2094 | 411 | } else if (key->eth.type == htons(ETH_P_IPV6)) { |
a94ebc39 | 412 | enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone; |
a94ebc39 | 413 | |
66ec6da8 | 414 | skb_orphan(skb); |
a94ebc39 | 415 | memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm)); |
2e602ea3 JS |
416 | err = nf_ct_frag6_gather(net, skb, user); |
417 | if (err) | |
418 | return err; | |
a94ebc39 | 419 | |
2e602ea3 | 420 | key->ip.proto = ipv6_hdr(skb)->nexthdr; |
86c2eb45 | 421 | ovs_cb.dp_cb.mru = IP6CB(skb)->frag_max_size; |
a94ebc39 JS |
422 | #endif /* IP frag support */ |
423 | } else { | |
c05e2094 | 424 | kfree_skb(skb); |
a94ebc39 JS |
425 | return -EPFNOSUPPORT; |
426 | } | |
427 | ||
428 | key->ip.frag = OVS_FRAG_TYPE_NONE; | |
429 | skb_clear_hash(skb); | |
430 | skb->ignore_df = 1; | |
86c2eb45 | 431 | *OVS_GSO_CB(skb) = ovs_cb; |
a94ebc39 JS |
432 | |
433 | return 0; | |
434 | } | |
435 | ||
436 | static struct nf_conntrack_expect * | |
437 | ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone, | |
438 | u16 proto, const struct sk_buff *skb) | |
439 | { | |
440 | struct nf_conntrack_tuple tuple; | |
441 | ||
fa67f8e0 | 442 | if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple)) |
a94ebc39 JS |
443 | return NULL; |
444 | return __nf_ct_expect_find(net, zone, &tuple); | |
445 | } | |
446 | ||
3dd9e118 JR |
447 | /* This replicates logic from nf_conntrack_core.c that is not exported. */ |
448 | static enum ip_conntrack_info | |
449 | ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h) | |
450 | { | |
451 | const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); | |
452 | ||
453 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) | |
454 | return IP_CT_ESTABLISHED_REPLY; | |
455 | /* Once we've had two way comms, always ESTABLISHED. */ | |
456 | if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) | |
457 | return IP_CT_ESTABLISHED; | |
458 | if (test_bit(IPS_EXPECTED_BIT, &ct->status)) | |
459 | return IP_CT_RELATED; | |
460 | return IP_CT_NEW; | |
461 | } | |
462 | ||
463 | /* Find an existing connection which this packet belongs to without | |
464 | * re-attributing statistics or modifying the connection state. This allows an | |
465 | * skb->nfct lost due to an upcall to be recovered during actions execution. | |
466 | * | |
467 | * Must be called with rcu_read_lock. | |
468 | * | |
469 | * On success, populates skb->nfct and skb->nfctinfo, and returns the | |
470 | * connection. Returns NULL if there is no existing entry. | |
471 | */ | |
472 | static struct nf_conn * | |
473 | ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone, | |
474 | u8 l3num, struct sk_buff *skb) | |
475 | { | |
476 | struct nf_conntrack_l3proto *l3proto; | |
477 | struct nf_conntrack_l4proto *l4proto; | |
478 | struct nf_conntrack_tuple tuple; | |
479 | struct nf_conntrack_tuple_hash *h; | |
480 | enum ip_conntrack_info ctinfo; | |
481 | struct nf_conn *ct; | |
482 | unsigned int dataoff; | |
483 | u8 protonum; | |
484 | ||
485 | l3proto = __nf_ct_l3proto_find(l3num); | |
3dd9e118 JR |
486 | if (l3proto->get_l4proto(skb, skb_network_offset(skb), &dataoff, |
487 | &protonum) <= 0) { | |
488 | pr_debug("ovs_ct_find_existing: Can't get protonum\n"); | |
489 | return NULL; | |
490 | } | |
491 | l4proto = __nf_ct_l4proto_find(l3num, protonum); | |
3dd9e118 JR |
492 | if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num, |
493 | protonum, net, &tuple, l3proto, l4proto)) { | |
494 | pr_debug("ovs_ct_find_existing: Can't get tuple\n"); | |
495 | return NULL; | |
496 | } | |
497 | ||
498 | /* look for tuple match */ | |
499 | h = nf_conntrack_find_get(net, zone, &tuple); | |
500 | if (!h) | |
501 | return NULL; /* Not found. */ | |
502 | ||
503 | ct = nf_ct_tuplehash_to_ctrack(h); | |
504 | ||
505 | ctinfo = ovs_ct_get_info(h); | |
506 | if (ctinfo == IP_CT_NEW) { | |
507 | /* This should not happen. */ | |
508 | WARN_ONCE(1, "ovs_ct_find_existing: new packet for %p\n", ct); | |
509 | } | |
510 | skb->nfct = &ct->ct_general; | |
511 | skb->nfctinfo = ctinfo; | |
512 | return ct; | |
513 | } | |
514 | ||
a94ebc39 | 515 | /* Determine whether skb->nfct is equal to the result of conntrack lookup. */ |
3dd9e118 JR |
516 | static bool skb_nfct_cached(struct net *net, |
517 | const struct sw_flow_key *key, | |
518 | const struct ovs_conntrack_info *info, | |
519 | struct sk_buff *skb) | |
a94ebc39 JS |
520 | { |
521 | enum ip_conntrack_info ctinfo; | |
522 | struct nf_conn *ct; | |
523 | ||
524 | ct = nf_ct_get(skb, &ctinfo); | |
3dd9e118 JR |
525 | /* If no ct, check if we have evidence that an existing conntrack entry |
526 | * might be found for this skb. This happens when we lose a skb->nfct | |
527 | * due to an upcall. If the connection was not confirmed, it is not | |
528 | * cached and needs to be run through conntrack again. | |
529 | */ | |
530 | if (!ct && key->ct.state & OVS_CS_F_TRACKED && | |
531 | !(key->ct.state & OVS_CS_F_INVALID) && | |
532 | key->ct.zone == info->zone.id) | |
533 | ct = ovs_ct_find_existing(net, &info->zone, info->family, skb); | |
a94ebc39 JS |
534 | if (!ct) |
535 | return false; | |
536 | if (!net_eq(net, read_pnet(&ct->ct_net))) | |
537 | return false; | |
538 | if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct))) | |
539 | return false; | |
11251c17 JS |
540 | if (info->helper) { |
541 | struct nf_conn_help *help; | |
542 | ||
543 | help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER); | |
544 | if (help && rcu_access_pointer(help->helper) != info->helper) | |
545 | return false; | |
546 | } | |
a94ebc39 JS |
547 | |
548 | return true; | |
549 | } | |
550 | ||
f8f97cdc JR |
551 | #ifdef CONFIG_NF_NAT_NEEDED |
552 | /* Modelled after nf_nat_ipv[46]_fn(). | |
553 | * range is only used for new, uninitialized NAT state. | |
554 | * Returns either NF_ACCEPT or NF_DROP. | |
555 | */ | |
556 | static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct, | |
557 | enum ip_conntrack_info ctinfo, | |
558 | const struct nf_nat_range *range, | |
559 | enum nf_nat_manip_type maniptype) | |
560 | { | |
561 | int hooknum, nh_off, err = NF_ACCEPT; | |
562 | ||
563 | nh_off = skb_network_offset(skb); | |
564 | skb_pull(skb, nh_off); | |
565 | ||
566 | /* See HOOK2MANIP(). */ | |
567 | if (maniptype == NF_NAT_MANIP_SRC) | |
568 | hooknum = NF_INET_LOCAL_IN; /* Source NAT */ | |
569 | else | |
570 | hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */ | |
571 | ||
572 | switch (ctinfo) { | |
573 | case IP_CT_RELATED: | |
574 | case IP_CT_RELATED_REPLY: | |
90b01477 AB |
575 | if (IS_ENABLED(CONFIG_NF_NAT_IPV4) && |
576 | skb->protocol == htons(ETH_P_IP) && | |
f8f97cdc JR |
577 | ip_hdr(skb)->protocol == IPPROTO_ICMP) { |
578 | if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo, | |
579 | hooknum)) | |
580 | err = NF_DROP; | |
581 | goto push; | |
90b01477 AB |
582 | } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) && |
583 | skb->protocol == htons(ETH_P_IPV6)) { | |
f8f97cdc JR |
584 | __be16 frag_off; |
585 | u8 nexthdr = ipv6_hdr(skb)->nexthdr; | |
586 | int hdrlen = ipv6_skip_exthdr(skb, | |
587 | sizeof(struct ipv6hdr), | |
588 | &nexthdr, &frag_off); | |
589 | ||
590 | if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) { | |
591 | if (!nf_nat_icmpv6_reply_translation(skb, ct, | |
592 | ctinfo, | |
593 | hooknum, | |
594 | hdrlen)) | |
595 | err = NF_DROP; | |
596 | goto push; | |
597 | } | |
f8f97cdc JR |
598 | } |
599 | /* Non-ICMP, fall thru to initialize if needed. */ | |
600 | case IP_CT_NEW: | |
601 | /* Seen it before? This can happen for loopback, retrans, | |
602 | * or local packets. | |
603 | */ | |
604 | if (!nf_nat_initialized(ct, maniptype)) { | |
605 | /* Initialize according to the NAT action. */ | |
606 | err = (range && range->flags & NF_NAT_RANGE_MAP_IPS) | |
607 | /* Action is set up to establish a new | |
608 | * mapping. | |
609 | */ | |
610 | ? nf_nat_setup_info(ct, range, maniptype) | |
611 | : nf_nat_alloc_null_binding(ct, hooknum); | |
612 | if (err != NF_ACCEPT) | |
613 | goto push; | |
614 | } | |
615 | break; | |
616 | ||
617 | case IP_CT_ESTABLISHED: | |
618 | case IP_CT_ESTABLISHED_REPLY: | |
619 | break; | |
620 | ||
621 | default: | |
622 | err = NF_DROP; | |
623 | goto push; | |
624 | } | |
625 | ||
626 | err = nf_nat_packet(ct, ctinfo, hooknum, skb); | |
627 | push: | |
628 | skb_push(skb, nh_off); | |
629 | ||
630 | return err; | |
631 | } | |
632 | ||
633 | static void ovs_nat_update_key(struct sw_flow_key *key, | |
634 | const struct sk_buff *skb, | |
635 | enum nf_nat_manip_type maniptype) | |
636 | { | |
637 | if (maniptype == NF_NAT_MANIP_SRC) { | |
638 | __be16 src; | |
639 | ||
640 | key->ct.state |= OVS_CS_F_SRC_NAT; | |
641 | if (key->eth.type == htons(ETH_P_IP)) | |
642 | key->ipv4.addr.src = ip_hdr(skb)->saddr; | |
643 | else if (key->eth.type == htons(ETH_P_IPV6)) | |
644 | memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr, | |
645 | sizeof(key->ipv6.addr.src)); | |
646 | else | |
647 | return; | |
648 | ||
649 | if (key->ip.proto == IPPROTO_UDP) | |
650 | src = udp_hdr(skb)->source; | |
651 | else if (key->ip.proto == IPPROTO_TCP) | |
652 | src = tcp_hdr(skb)->source; | |
653 | else if (key->ip.proto == IPPROTO_SCTP) | |
654 | src = sctp_hdr(skb)->source; | |
655 | else | |
656 | return; | |
657 | ||
658 | key->tp.src = src; | |
659 | } else { | |
660 | __be16 dst; | |
661 | ||
662 | key->ct.state |= OVS_CS_F_DST_NAT; | |
663 | if (key->eth.type == htons(ETH_P_IP)) | |
664 | key->ipv4.addr.dst = ip_hdr(skb)->daddr; | |
665 | else if (key->eth.type == htons(ETH_P_IPV6)) | |
666 | memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr, | |
667 | sizeof(key->ipv6.addr.dst)); | |
668 | else | |
669 | return; | |
670 | ||
671 | if (key->ip.proto == IPPROTO_UDP) | |
672 | dst = udp_hdr(skb)->dest; | |
673 | else if (key->ip.proto == IPPROTO_TCP) | |
674 | dst = tcp_hdr(skb)->dest; | |
675 | else if (key->ip.proto == IPPROTO_SCTP) | |
676 | dst = sctp_hdr(skb)->dest; | |
677 | else | |
678 | return; | |
679 | ||
680 | key->tp.dst = dst; | |
681 | } | |
682 | } | |
683 | ||
684 | /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */ | |
685 | static int ovs_ct_nat(struct net *net, struct sw_flow_key *key, | |
686 | const struct ovs_conntrack_info *info, | |
687 | struct sk_buff *skb, struct nf_conn *ct, | |
688 | enum ip_conntrack_info ctinfo) | |
689 | { | |
690 | enum nf_nat_manip_type maniptype; | |
691 | int err; | |
692 | ||
693 | if (nf_ct_is_untracked(ct)) { | |
694 | /* A NAT action may only be performed on tracked packets. */ | |
695 | return NF_ACCEPT; | |
696 | } | |
697 | ||
698 | /* Add NAT extension if not confirmed yet. */ | |
699 | if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct)) | |
700 | return NF_ACCEPT; /* Can't NAT. */ | |
701 | ||
702 | /* Determine NAT type. | |
703 | * Check if the NAT type can be deduced from the tracked connection. | |
d2e8b514 JR |
704 | * Make sure new expected connections (IP_CT_RELATED) are NATted only |
705 | * when committing. | |
f8f97cdc JR |
706 | */ |
707 | if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW && | |
708 | ct->status & IPS_NAT_MASK && | |
d2e8b514 | 709 | (ctinfo != IP_CT_RELATED || info->commit)) { |
f8f97cdc JR |
710 | /* NAT an established or related connection like before. */ |
711 | if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) | |
712 | /* This is the REPLY direction for a connection | |
713 | * for which NAT was applied in the forward | |
714 | * direction. Do the reverse NAT. | |
715 | */ | |
716 | maniptype = ct->status & IPS_SRC_NAT | |
717 | ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC; | |
718 | else | |
719 | maniptype = ct->status & IPS_SRC_NAT | |
720 | ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST; | |
721 | } else if (info->nat & OVS_CT_SRC_NAT) { | |
722 | maniptype = NF_NAT_MANIP_SRC; | |
723 | } else if (info->nat & OVS_CT_DST_NAT) { | |
724 | maniptype = NF_NAT_MANIP_DST; | |
725 | } else { | |
726 | return NF_ACCEPT; /* Connection is not NATed. */ | |
727 | } | |
728 | err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype); | |
729 | ||
730 | /* Mark NAT done if successful and update the flow key. */ | |
731 | if (err == NF_ACCEPT) | |
732 | ovs_nat_update_key(key, skb, maniptype); | |
733 | ||
734 | return err; | |
735 | } | |
736 | #else /* !CONFIG_NF_NAT_NEEDED */ | |
737 | static int ovs_ct_nat(struct net *net, struct sw_flow_key *key, | |
738 | const struct ovs_conntrack_info *info, | |
739 | struct sk_buff *skb, struct nf_conn *ct, | |
740 | enum ip_conntrack_info ctinfo) | |
741 | { | |
742 | return NF_ACCEPT; | |
743 | } | |
744 | #endif | |
745 | ||
b0f251cd | 746 | /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if |
a04a5794 JR |
747 | * not done already. Update key with new CT state after passing the packet |
748 | * through conntrack. | |
b0f251cd JR |
749 | * Note that if the packet is deemed invalid by conntrack, skb->nfct will be |
750 | * set to NULL and 0 will be returned. | |
751 | */ | |
c05e2094 | 752 | static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key, |
a94ebc39 JS |
753 | const struct ovs_conntrack_info *info, |
754 | struct sk_buff *skb) | |
755 | { | |
756 | /* If we are recirculating packets to match on conntrack fields and | |
757 | * committing with a separate conntrack action, then we don't need to | |
758 | * actually run the packet through conntrack twice unless it's for a | |
759 | * different zone. | |
760 | */ | |
b21d237e JR |
761 | bool cached = skb_nfct_cached(net, key, info, skb); |
762 | enum ip_conntrack_info ctinfo; | |
763 | struct nf_conn *ct; | |
764 | ||
765 | if (!cached) { | |
a94ebc39 | 766 | struct nf_conn *tmpl = info->ct; |
9bf67b92 | 767 | int err; |
a94ebc39 JS |
768 | |
769 | /* Associate skb with specified zone. */ | |
770 | if (tmpl) { | |
771 | if (skb->nfct) | |
772 | nf_conntrack_put(skb->nfct); | |
773 | nf_conntrack_get(&tmpl->ct_general); | |
774 | skb->nfct = &tmpl->ct_general; | |
775 | skb->nfctinfo = IP_CT_NEW; | |
776 | } | |
777 | ||
9bf67b92 JR |
778 | /* Repeat if requested, see nf_iterate(). */ |
779 | do { | |
780 | err = nf_conntrack_in(net, info->family, | |
781 | NF_INET_FORWARD, skb); | |
782 | } while (err == NF_REPEAT); | |
783 | ||
784 | if (err != NF_ACCEPT) | |
a94ebc39 | 785 | return -ENOENT; |
11251c17 | 786 | |
f8f97cdc JR |
787 | /* Clear CT state NAT flags to mark that we have not yet done |
788 | * NAT after the nf_conntrack_in() call. We can actually clear | |
789 | * the whole state, as it will be re-initialized below. | |
790 | */ | |
791 | key->ct.state = 0; | |
792 | ||
793 | /* Update the key, but keep the NAT flags. */ | |
794 | ovs_ct_update_key(skb, info, key, true, true); | |
b21d237e | 795 | } |
a04a5794 | 796 | |
b21d237e | 797 | ct = nf_ct_get(skb, &ctinfo); |
f8f97cdc JR |
798 | if (ct) { |
799 | /* Packets starting a new connection must be NATted before the | |
800 | * helper, so that the helper knows about the NAT. We enforce | |
801 | * this by delaying both NAT and helper calls for unconfirmed | |
802 | * connections until the committing CT action. For later | |
803 | * packets NAT and Helper may be called in either order. | |
804 | * | |
805 | * NAT will be done only if the CT action has NAT, and only | |
806 | * once per packet (per zone), as guarded by the NAT bits in | |
807 | * the key->ct.state. | |
808 | */ | |
809 | if (info->nat && !(key->ct.state & OVS_CS_F_NAT_MASK) && | |
810 | (nf_ct_is_confirmed(ct) || info->commit) && | |
811 | ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) { | |
812 | return -EINVAL; | |
813 | } | |
814 | ||
b87a5aac JS |
815 | /* Userspace may decide to perform a ct lookup without a helper |
816 | * specified followed by a (recirculate and) commit with one. | |
817 | * Therefore, for unconfirmed connections which we will commit, | |
818 | * we need to attach the helper here. | |
819 | */ | |
820 | if (!nf_ct_is_confirmed(ct) && info->commit && | |
821 | info->helper && !nfct_help(ct)) { | |
822 | int err = __nf_ct_try_assign_helper(ct, info->ct, | |
823 | GFP_ATOMIC); | |
824 | if (err) | |
825 | return err; | |
826 | } | |
827 | ||
f8f97cdc JR |
828 | /* Call the helper only if: |
829 | * - nf_conntrack_in() was executed above ("!cached") for a | |
830 | * confirmed connection, or | |
831 | * - When committing an unconfirmed connection. | |
832 | */ | |
833 | if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) && | |
834 | ovs_ct_helper(skb, info->family) != NF_ACCEPT) { | |
835 | return -EINVAL; | |
836 | } | |
a94ebc39 JS |
837 | } |
838 | ||
839 | return 0; | |
840 | } | |
841 | ||
842 | /* Lookup connection and read fields into key. */ | |
843 | static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key, | |
844 | const struct ovs_conntrack_info *info, | |
845 | struct sk_buff *skb) | |
846 | { | |
847 | struct nf_conntrack_expect *exp; | |
848 | ||
b0f251cd JR |
849 | /* If we pass an expected packet through nf_conntrack_in() the |
850 | * expectation is typically removed, but the packet could still be | |
851 | * lost in upcall processing. To prevent this from happening we | |
852 | * perform an explicit expectation lookup. Expected connections are | |
853 | * always new, and will be passed through conntrack only when they are | |
854 | * committed, as it is OK to remove the expectation at that time. | |
855 | */ | |
a94ebc39 JS |
856 | exp = ovs_ct_expect_find(net, &info->zone, info->family, skb); |
857 | if (exp) { | |
858 | u8 state; | |
859 | ||
f8f97cdc JR |
860 | /* NOTE: New connections are NATted and Helped only when |
861 | * committed, so we are not calling into NAT here. | |
862 | */ | |
a94ebc39 | 863 | state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED; |
372ce973 | 864 | __ovs_ct_update_key(key, state, &info->zone, exp->master); |
f8f97cdc JR |
865 | } else |
866 | return __ovs_ct_lookup(net, key, info, skb); | |
a94ebc39 JS |
867 | |
868 | return 0; | |
869 | } | |
870 | ||
c05e2094 | 871 | static bool labels_nonzero(const struct ovs_key_ct_labels *labels) |
038e34ab JS |
872 | { |
873 | size_t i; | |
874 | ||
c05e2094 JS |
875 | for (i = 0; i < sizeof(*labels); i++) |
876 | if (labels->ct_labels[i]) | |
038e34ab JS |
877 | return true; |
878 | ||
879 | return false; | |
880 | } | |
881 | ||
c05e2094 JS |
882 | /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero |
883 | * value if 'skb' is freed. | |
884 | */ | |
a94ebc39 JS |
885 | int ovs_ct_execute(struct net *net, struct sk_buff *skb, |
886 | struct sw_flow_key *key, | |
887 | const struct ovs_conntrack_info *info) | |
888 | { | |
889 | int nh_ofs; | |
890 | int err; | |
891 | ||
892 | /* The conntrack module expects to be working at L3. */ | |
893 | nh_ofs = skb_network_offset(skb); | |
894 | skb_pull(skb, nh_ofs); | |
895 | ||
896 | if (key->ip.frag != OVS_FRAG_TYPE_NONE) { | |
897 | err = handle_fragments(net, key, info->zone.id, skb); | |
898 | if (err) | |
899 | return err; | |
900 | } | |
901 | ||
c05e2094 | 902 | if (info->commit) |
086c1753 | 903 | err = __ovs_ct_lookup(net, key, info, skb); |
a94ebc39 JS |
904 | else |
905 | err = ovs_ct_lookup(net, key, info, skb); | |
372ce973 JS |
906 | if (err) |
907 | goto err; | |
a94ebc39 | 908 | |
086c1753 JR |
909 | /* Apply changes before confirming the connection so that the initial |
910 | * conntrack NEW netlink event carries the values given in the CT | |
911 | * action. | |
912 | */ | |
038e34ab | 913 | if (info->mark.mask) { |
372ce973 JS |
914 | err = ovs_ct_set_mark(skb, key, info->mark.value, |
915 | info->mark.mask); | |
038e34ab JS |
916 | if (err) |
917 | goto err; | |
918 | } | |
086c1753 | 919 | if (labels_nonzero(&info->labels.mask)) { |
c05e2094 JS |
920 | err = ovs_ct_set_labels(skb, key, &info->labels.value, |
921 | &info->labels.mask); | |
086c1753 JR |
922 | if (err) |
923 | goto err; | |
924 | } | |
925 | /* This will take care of sending queued events even if the connection | |
926 | * is already confirmed. | |
927 | */ | |
928 | if (info->commit && nf_conntrack_confirm(skb) != NF_ACCEPT) | |
929 | err = -EINVAL; | |
372ce973 | 930 | err: |
a94ebc39 | 931 | skb_push(skb, nh_ofs); |
c05e2094 JS |
932 | if (err) |
933 | kfree_skb(skb); | |
a94ebc39 JS |
934 | return err; |
935 | } | |
936 | ||
11251c17 JS |
937 | static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name, |
938 | const struct sw_flow_key *key, bool log) | |
939 | { | |
940 | struct nf_conntrack_helper *helper; | |
941 | struct nf_conn_help *help; | |
942 | ||
943 | helper = nf_conntrack_helper_try_module_get(name, info->family, | |
944 | key->ip.proto); | |
945 | if (!helper) { | |
946 | OVS_NLERR(log, "Unknown helper \"%s\"", name); | |
947 | return -EINVAL; | |
948 | } | |
949 | ||
950 | help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL); | |
951 | if (!help) { | |
952 | module_put(helper->me); | |
953 | return -ENOMEM; | |
954 | } | |
955 | ||
956 | rcu_assign_pointer(help->helper, helper); | |
957 | info->helper = helper; | |
958 | return 0; | |
959 | } | |
960 | ||
f8f97cdc JR |
961 | #ifdef CONFIG_NF_NAT_NEEDED |
962 | static int parse_nat(const struct nlattr *attr, | |
963 | struct ovs_conntrack_info *info, bool log) | |
964 | { | |
965 | struct nlattr *a; | |
966 | int rem; | |
967 | bool have_ip_max = false; | |
968 | bool have_proto_max = false; | |
969 | bool ip_vers = (info->family == NFPROTO_IPV6); | |
970 | ||
971 | nla_for_each_nested(a, attr, rem) { | |
972 | static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = { | |
973 | [OVS_NAT_ATTR_SRC] = {0, 0}, | |
974 | [OVS_NAT_ATTR_DST] = {0, 0}, | |
975 | [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr), | |
976 | sizeof(struct in6_addr)}, | |
977 | [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr), | |
978 | sizeof(struct in6_addr)}, | |
979 | [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)}, | |
980 | [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)}, | |
981 | [OVS_NAT_ATTR_PERSISTENT] = {0, 0}, | |
982 | [OVS_NAT_ATTR_PROTO_HASH] = {0, 0}, | |
983 | [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0}, | |
984 | }; | |
985 | int type = nla_type(a); | |
986 | ||
987 | if (type > OVS_NAT_ATTR_MAX) { | |
988 | OVS_NLERR(log, | |
989 | "Unknown NAT attribute (type=%d, max=%d).\n", | |
990 | type, OVS_NAT_ATTR_MAX); | |
991 | return -EINVAL; | |
992 | } | |
993 | ||
994 | if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) { | |
995 | OVS_NLERR(log, | |
996 | "NAT attribute type %d has unexpected length (%d != %d).\n", | |
997 | type, nla_len(a), | |
998 | ovs_nat_attr_lens[type][ip_vers]); | |
999 | return -EINVAL; | |
1000 | } | |
1001 | ||
1002 | switch (type) { | |
1003 | case OVS_NAT_ATTR_SRC: | |
1004 | case OVS_NAT_ATTR_DST: | |
1005 | if (info->nat) { | |
1006 | OVS_NLERR(log, | |
1007 | "Only one type of NAT may be specified.\n" | |
1008 | ); | |
1009 | return -ERANGE; | |
1010 | } | |
1011 | info->nat |= OVS_CT_NAT; | |
1012 | info->nat |= ((type == OVS_NAT_ATTR_SRC) | |
1013 | ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT); | |
1014 | break; | |
1015 | ||
1016 | case OVS_NAT_ATTR_IP_MIN: | |
70e71d27 HY |
1017 | nla_memcpy(&info->range.min_addr, a, |
1018 | sizeof(info->range.min_addr)); | |
f8f97cdc JR |
1019 | info->range.flags |= NF_NAT_RANGE_MAP_IPS; |
1020 | break; | |
1021 | ||
1022 | case OVS_NAT_ATTR_IP_MAX: | |
1023 | have_ip_max = true; | |
1024 | nla_memcpy(&info->range.max_addr, a, | |
1025 | sizeof(info->range.max_addr)); | |
1026 | info->range.flags |= NF_NAT_RANGE_MAP_IPS; | |
1027 | break; | |
1028 | ||
1029 | case OVS_NAT_ATTR_PROTO_MIN: | |
1030 | info->range.min_proto.all = htons(nla_get_u16(a)); | |
1031 | info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED; | |
1032 | break; | |
1033 | ||
1034 | case OVS_NAT_ATTR_PROTO_MAX: | |
1035 | have_proto_max = true; | |
1036 | info->range.max_proto.all = htons(nla_get_u16(a)); | |
1037 | info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED; | |
1038 | break; | |
1039 | ||
1040 | case OVS_NAT_ATTR_PERSISTENT: | |
1041 | info->range.flags |= NF_NAT_RANGE_PERSISTENT; | |
1042 | break; | |
1043 | ||
1044 | case OVS_NAT_ATTR_PROTO_HASH: | |
1045 | info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM; | |
1046 | break; | |
1047 | ||
1048 | case OVS_NAT_ATTR_PROTO_RANDOM: | |
9f1de150 | 1049 | #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY |
f8f97cdc | 1050 | info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY; |
9f1de150 JR |
1051 | #else |
1052 | info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM; | |
1053 | info->random_fully_compat = true; | |
1054 | #endif | |
f8f97cdc JR |
1055 | break; |
1056 | ||
1057 | default: | |
1058 | OVS_NLERR(log, "Unknown nat attribute (%d).\n", type); | |
1059 | return -EINVAL; | |
1060 | } | |
1061 | } | |
1062 | ||
1063 | if (rem > 0) { | |
1064 | OVS_NLERR(log, "NAT attribute has %d unknown bytes.\n", rem); | |
1065 | return -EINVAL; | |
1066 | } | |
1067 | if (!info->nat) { | |
1068 | /* Do not allow flags if no type is given. */ | |
1069 | if (info->range.flags) { | |
1070 | OVS_NLERR(log, | |
1071 | "NAT flags may be given only when NAT range (SRC or DST) is also specified.\n" | |
1072 | ); | |
1073 | return -EINVAL; | |
1074 | } | |
1075 | info->nat = OVS_CT_NAT; /* NAT existing connections. */ | |
1076 | } else if (!info->commit) { | |
1077 | OVS_NLERR(log, | |
1078 | "NAT attributes may be specified only when CT COMMIT flag is also specified.\n" | |
1079 | ); | |
1080 | return -EINVAL; | |
1081 | } | |
1082 | /* Allow missing IP_MAX. */ | |
1083 | if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) { | |
1084 | memcpy(&info->range.max_addr, &info->range.min_addr, | |
1085 | sizeof(info->range.max_addr)); | |
1086 | } | |
1087 | /* Allow missing PROTO_MAX. */ | |
1088 | if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED && | |
1089 | !have_proto_max) { | |
1090 | info->range.max_proto.all = info->range.min_proto.all; | |
1091 | } | |
1092 | return 0; | |
1093 | } | |
1094 | #endif | |
1095 | ||
a94ebc39 | 1096 | static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = { |
c05e2094 | 1097 | [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 }, |
a94ebc39 JS |
1098 | [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16), |
1099 | .maxlen = sizeof(u16) }, | |
372ce973 JS |
1100 | [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark), |
1101 | .maxlen = sizeof(struct md_mark) }, | |
c05e2094 JS |
1102 | [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels), |
1103 | .maxlen = sizeof(struct md_labels) }, | |
11251c17 | 1104 | [OVS_CT_ATTR_HELPER] = { .minlen = 1, |
f8f97cdc JR |
1105 | .maxlen = NF_CT_HELPER_NAME_LEN }, |
1106 | #ifdef CONFIG_NF_NAT_NEEDED | |
1107 | /* NAT length is checked when parsing the nested attributes. */ | |
1108 | [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX }, | |
1109 | #endif | |
a94ebc39 JS |
1110 | }; |
1111 | ||
1112 | static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info, | |
11251c17 | 1113 | const char **helper, bool log) |
a94ebc39 JS |
1114 | { |
1115 | struct nlattr *a; | |
1116 | int rem; | |
1117 | ||
1118 | nla_for_each_nested(a, attr, rem) { | |
1119 | int type = nla_type(a); | |
1120 | int maxlen = ovs_ct_attr_lens[type].maxlen; | |
1121 | int minlen = ovs_ct_attr_lens[type].minlen; | |
1122 | ||
1123 | if (type > OVS_CT_ATTR_MAX) { | |
1124 | OVS_NLERR(log, | |
1125 | "Unknown conntrack attr (type=%d, max=%d)", | |
1126 | type, OVS_CT_ATTR_MAX); | |
1127 | return -EINVAL; | |
1128 | } | |
1129 | if (nla_len(a) < minlen || nla_len(a) > maxlen) { | |
1130 | OVS_NLERR(log, | |
1131 | "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)", | |
1132 | type, nla_len(a), maxlen); | |
1133 | return -EINVAL; | |
1134 | } | |
1135 | ||
1136 | switch (type) { | |
c05e2094 JS |
1137 | case OVS_CT_ATTR_COMMIT: |
1138 | info->commit = true; | |
a94ebc39 JS |
1139 | break; |
1140 | #ifdef CONFIG_NF_CONNTRACK_ZONES | |
1141 | case OVS_CT_ATTR_ZONE: | |
1142 | info->zone.id = nla_get_u16(a); | |
1143 | break; | |
372ce973 JS |
1144 | #endif |
1145 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
1146 | case OVS_CT_ATTR_MARK: { | |
1147 | struct md_mark *mark = nla_data(a); | |
1148 | ||
c05e2094 JS |
1149 | if (!mark->mask) { |
1150 | OVS_NLERR(log, "ct_mark mask cannot be 0"); | |
1151 | return -EINVAL; | |
1152 | } | |
372ce973 JS |
1153 | info->mark = *mark; |
1154 | break; | |
1155 | } | |
038e34ab JS |
1156 | #endif |
1157 | #ifdef CONFIG_NF_CONNTRACK_LABELS | |
c05e2094 JS |
1158 | case OVS_CT_ATTR_LABELS: { |
1159 | struct md_labels *labels = nla_data(a); | |
038e34ab | 1160 | |
c05e2094 JS |
1161 | if (!labels_nonzero(&labels->mask)) { |
1162 | OVS_NLERR(log, "ct_labels mask cannot be 0"); | |
1163 | return -EINVAL; | |
1164 | } | |
1165 | info->labels = *labels; | |
038e34ab JS |
1166 | break; |
1167 | } | |
a94ebc39 | 1168 | #endif |
11251c17 JS |
1169 | case OVS_CT_ATTR_HELPER: |
1170 | *helper = nla_data(a); | |
1171 | if (!memchr(*helper, '\0', nla_len(a))) { | |
1172 | OVS_NLERR(log, "Invalid conntrack helper"); | |
1173 | return -EINVAL; | |
1174 | } | |
1175 | break; | |
f8f97cdc JR |
1176 | #ifdef CONFIG_NF_NAT_NEEDED |
1177 | case OVS_CT_ATTR_NAT: { | |
1178 | int err = parse_nat(a, info, log); | |
1179 | ||
1180 | if (err) | |
1181 | return err; | |
1182 | break; | |
1183 | } | |
1184 | #endif | |
a94ebc39 JS |
1185 | default: |
1186 | OVS_NLERR(log, "Unknown conntrack attr (%d)", | |
1187 | type); | |
1188 | return -EINVAL; | |
1189 | } | |
1190 | } | |
1191 | ||
1192 | if (rem > 0) { | |
1193 | OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem); | |
1194 | return -EINVAL; | |
1195 | } | |
1196 | ||
1197 | return 0; | |
1198 | } | |
1199 | ||
038e34ab | 1200 | bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr) |
a94ebc39 JS |
1201 | { |
1202 | if (attr == OVS_KEY_ATTR_CT_STATE) | |
1203 | return true; | |
1204 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && | |
1205 | attr == OVS_KEY_ATTR_CT_ZONE) | |
1206 | return true; | |
372ce973 JS |
1207 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && |
1208 | attr == OVS_KEY_ATTR_CT_MARK) | |
1209 | return true; | |
038e34ab | 1210 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && |
c05e2094 | 1211 | attr == OVS_KEY_ATTR_CT_LABELS) { |
038e34ab JS |
1212 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
1213 | ||
1214 | return ovs_net->xt_label; | |
1215 | } | |
a94ebc39 JS |
1216 | |
1217 | return false; | |
1218 | } | |
1219 | ||
1220 | int ovs_ct_copy_action(struct net *net, const struct nlattr *attr, | |
1221 | const struct sw_flow_key *key, | |
1222 | struct sw_flow_actions **sfa, bool log) | |
1223 | { | |
1224 | struct ovs_conntrack_info ct_info; | |
11251c17 | 1225 | const char *helper = NULL; |
a94ebc39 JS |
1226 | u16 family; |
1227 | int err; | |
1228 | ||
1229 | family = key_to_nfproto(key); | |
1230 | if (family == NFPROTO_UNSPEC) { | |
1231 | OVS_NLERR(log, "ct family unspecified"); | |
1232 | return -EINVAL; | |
1233 | } | |
1234 | ||
1235 | memset(&ct_info, 0, sizeof(ct_info)); | |
1236 | ct_info.family = family; | |
1237 | ||
1238 | nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID, | |
1239 | NF_CT_DEFAULT_ZONE_DIR, 0); | |
1240 | ||
11251c17 | 1241 | err = parse_ct(attr, &ct_info, &helper, log); |
a94ebc39 JS |
1242 | if (err) |
1243 | return err; | |
1244 | ||
1245 | /* Set up template for tracking connections in specific zones. */ | |
1246 | ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL); | |
1247 | if (!ct_info.ct) { | |
1248 | OVS_NLERR(log, "Failed to allocate conntrack template"); | |
1249 | return -ENOMEM; | |
1250 | } | |
a3a68d63 JS |
1251 | |
1252 | __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status); | |
1253 | nf_conntrack_get(&ct_info.ct->ct_general); | |
1254 | ||
11251c17 JS |
1255 | if (helper) { |
1256 | err = ovs_ct_add_helper(&ct_info, helper, key, log); | |
1257 | if (err) | |
1258 | goto err_free_ct; | |
1259 | } | |
a94ebc39 JS |
1260 | |
1261 | err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info, | |
1262 | sizeof(ct_info), log); | |
1263 | if (err) | |
1264 | goto err_free_ct; | |
1265 | ||
a94ebc39 JS |
1266 | return 0; |
1267 | err_free_ct: | |
11251c17 | 1268 | __ovs_ct_free_action(&ct_info); |
a94ebc39 JS |
1269 | return err; |
1270 | } | |
1271 | ||
f8f97cdc JR |
1272 | #ifdef CONFIG_NF_NAT_NEEDED |
1273 | static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info, | |
1274 | struct sk_buff *skb) | |
1275 | { | |
1276 | struct nlattr *start; | |
1277 | ||
1278 | start = nla_nest_start(skb, OVS_CT_ATTR_NAT); | |
1279 | if (!start) | |
1280 | return false; | |
1281 | ||
1282 | if (info->nat & OVS_CT_SRC_NAT) { | |
1283 | if (nla_put_flag(skb, OVS_NAT_ATTR_SRC)) | |
1284 | return false; | |
1285 | } else if (info->nat & OVS_CT_DST_NAT) { | |
1286 | if (nla_put_flag(skb, OVS_NAT_ATTR_DST)) | |
1287 | return false; | |
1288 | } else { | |
1289 | goto out; | |
1290 | } | |
1291 | ||
1292 | if (info->range.flags & NF_NAT_RANGE_MAP_IPS) { | |
90b01477 AB |
1293 | if (IS_ENABLED(CONFIG_NF_NAT_IPV4) && |
1294 | info->family == NFPROTO_IPV4) { | |
f8f97cdc JR |
1295 | if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN, |
1296 | info->range.min_addr.ip) || | |
1297 | (info->range.max_addr.ip | |
1298 | != info->range.min_addr.ip && | |
1299 | (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX, | |
1300 | info->range.max_addr.ip)))) | |
1301 | return false; | |
90b01477 AB |
1302 | } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) && |
1303 | info->family == NFPROTO_IPV6) { | |
f8f97cdc JR |
1304 | if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN, |
1305 | &info->range.min_addr.in6) || | |
1306 | (memcmp(&info->range.max_addr.in6, | |
1307 | &info->range.min_addr.in6, | |
1308 | sizeof(info->range.max_addr.in6)) && | |
1309 | (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX, | |
1310 | &info->range.max_addr.in6)))) | |
1311 | return false; | |
f8f97cdc JR |
1312 | } else { |
1313 | return false; | |
1314 | } | |
1315 | } | |
1316 | if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED && | |
1317 | (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN, | |
1318 | ntohs(info->range.min_proto.all)) || | |
1319 | (info->range.max_proto.all != info->range.min_proto.all && | |
1320 | nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX, | |
1321 | ntohs(info->range.max_proto.all))))) | |
1322 | return false; | |
1323 | ||
1324 | if (info->range.flags & NF_NAT_RANGE_PERSISTENT && | |
1325 | nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT)) | |
1326 | return false; | |
1327 | if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM && | |
9f1de150 JR |
1328 | nla_put_flag(skb, info->random_fully_compat |
1329 | ? OVS_NAT_ATTR_PROTO_RANDOM | |
1330 | : OVS_NAT_ATTR_PROTO_HASH)) | |
f8f97cdc | 1331 | return false; |
9f1de150 | 1332 | #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY |
f8f97cdc JR |
1333 | if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY && |
1334 | nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM)) | |
1335 | return false; | |
9f1de150 | 1336 | #endif |
f8f97cdc JR |
1337 | out: |
1338 | nla_nest_end(skb, start); | |
1339 | ||
1340 | return true; | |
1341 | } | |
1342 | #endif | |
1343 | ||
a94ebc39 JS |
1344 | int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info, |
1345 | struct sk_buff *skb) | |
1346 | { | |
1347 | struct nlattr *start; | |
1348 | ||
1349 | start = nla_nest_start(skb, OVS_ACTION_ATTR_CT); | |
1350 | if (!start) | |
1351 | return -EMSGSIZE; | |
1352 | ||
c05e2094 | 1353 | if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT)) |
a94ebc39 JS |
1354 | return -EMSGSIZE; |
1355 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && | |
1356 | nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id)) | |
1357 | return -EMSGSIZE; | |
c05e2094 | 1358 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask && |
372ce973 JS |
1359 | nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark), |
1360 | &ct_info->mark)) | |
1361 | return -EMSGSIZE; | |
038e34ab | 1362 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && |
c05e2094 JS |
1363 | labels_nonzero(&ct_info->labels.mask) && |
1364 | nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels), | |
1365 | &ct_info->labels)) | |
038e34ab | 1366 | return -EMSGSIZE; |
11251c17 JS |
1367 | if (ct_info->helper) { |
1368 | if (nla_put_string(skb, OVS_CT_ATTR_HELPER, | |
1369 | ct_info->helper->name)) | |
1370 | return -EMSGSIZE; | |
1371 | } | |
f8f97cdc JR |
1372 | #ifdef CONFIG_NF_NAT_NEEDED |
1373 | if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb)) | |
1374 | return -EMSGSIZE; | |
1375 | #endif | |
a94ebc39 JS |
1376 | nla_nest_end(skb, start); |
1377 | ||
1378 | return 0; | |
1379 | } | |
1380 | ||
1381 | void ovs_ct_free_action(const struct nlattr *a) | |
1382 | { | |
1383 | struct ovs_conntrack_info *ct_info = nla_data(a); | |
1384 | ||
11251c17 JS |
1385 | __ovs_ct_free_action(ct_info); |
1386 | } | |
1387 | ||
1388 | static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info) | |
1389 | { | |
1390 | if (ct_info->helper) | |
1391 | module_put(ct_info->helper->me); | |
a94ebc39 JS |
1392 | if (ct_info->ct) |
1393 | nf_ct_tmpl_free(ct_info->ct); | |
1394 | } | |
1395 | ||
038e34ab JS |
1396 | void ovs_ct_init(struct net *net) |
1397 | { | |
c05e2094 | 1398 | unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE; |
038e34ab JS |
1399 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
1400 | ||
7f2ab8cd | 1401 | if (nf_connlabels_get(net, n_bits - 1)) { |
038e34ab JS |
1402 | ovs_net->xt_label = false; |
1403 | OVS_NLERR(true, "Failed to set connlabel length"); | |
1404 | } else { | |
1405 | ovs_net->xt_label = true; | |
1406 | } | |
1407 | } | |
1408 | ||
1409 | void ovs_ct_exit(struct net *net) | |
1410 | { | |
1411 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); | |
1412 | ||
1413 | if (ovs_net->xt_label) | |
1414 | nf_connlabels_put(net); | |
1415 | } | |
1416 | ||
8063e095 | 1417 | #endif /* CONFIG_NF_CONNTRACK */ |