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