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> | |
16 | #include <net/ip.h> | |
17 | #include <net/netfilter/nf_conntrack_core.h> | |
cae3a262 | 18 | #include <net/netfilter/nf_conntrack_helper.h> |
c2ac6673 | 19 | #include <net/netfilter/nf_conntrack_labels.h> |
7f8a436e JS |
20 | #include <net/netfilter/nf_conntrack_zones.h> |
21 | #include <net/netfilter/ipv6/nf_defrag_ipv6.h> | |
22 | ||
23 | #include "datapath.h" | |
24 | #include "conntrack.h" | |
25 | #include "flow.h" | |
26 | #include "flow_netlink.h" | |
27 | ||
28 | struct ovs_ct_len_tbl { | |
29 | size_t maxlen; | |
30 | size_t minlen; | |
31 | }; | |
32 | ||
182e3042 JS |
33 | /* Metadata mark for masked write to conntrack mark */ |
34 | struct md_mark { | |
35 | u32 value; | |
36 | u32 mask; | |
37 | }; | |
38 | ||
c2ac6673 | 39 | /* Metadata label for masked write to conntrack label. */ |
33db4125 JS |
40 | struct md_labels { |
41 | struct ovs_key_ct_labels value; | |
42 | struct ovs_key_ct_labels mask; | |
c2ac6673 JS |
43 | }; |
44 | ||
7f8a436e JS |
45 | /* Conntrack action context for execution. */ |
46 | struct ovs_conntrack_info { | |
cae3a262 | 47 | struct nf_conntrack_helper *helper; |
7f8a436e JS |
48 | struct nf_conntrack_zone zone; |
49 | struct nf_conn *ct; | |
ab38a7b5 | 50 | u8 commit : 1; |
7f8a436e | 51 | u16 family; |
182e3042 | 52 | struct md_mark mark; |
33db4125 | 53 | struct md_labels labels; |
7f8a436e JS |
54 | }; |
55 | ||
2f3ab9f9 JS |
56 | static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info); |
57 | ||
7f8a436e JS |
58 | static u16 key_to_nfproto(const struct sw_flow_key *key) |
59 | { | |
60 | switch (ntohs(key->eth.type)) { | |
61 | case ETH_P_IP: | |
62 | return NFPROTO_IPV4; | |
63 | case ETH_P_IPV6: | |
64 | return NFPROTO_IPV6; | |
65 | default: | |
66 | return NFPROTO_UNSPEC; | |
67 | } | |
68 | } | |
69 | ||
70 | /* Map SKB connection state into the values used by flow definition. */ | |
71 | static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo) | |
72 | { | |
73 | u8 ct_state = OVS_CS_F_TRACKED; | |
74 | ||
75 | switch (ctinfo) { | |
76 | case IP_CT_ESTABLISHED_REPLY: | |
77 | case IP_CT_RELATED_REPLY: | |
7f8a436e JS |
78 | ct_state |= OVS_CS_F_REPLY_DIR; |
79 | break; | |
80 | default: | |
81 | break; | |
82 | } | |
83 | ||
84 | switch (ctinfo) { | |
85 | case IP_CT_ESTABLISHED: | |
86 | case IP_CT_ESTABLISHED_REPLY: | |
87 | ct_state |= OVS_CS_F_ESTABLISHED; | |
88 | break; | |
89 | case IP_CT_RELATED: | |
90 | case IP_CT_RELATED_REPLY: | |
91 | ct_state |= OVS_CS_F_RELATED; | |
92 | break; | |
93 | case IP_CT_NEW: | |
7f8a436e JS |
94 | ct_state |= OVS_CS_F_NEW; |
95 | break; | |
96 | default: | |
97 | break; | |
98 | } | |
99 | ||
100 | return ct_state; | |
101 | } | |
102 | ||
0d5cdef8 JS |
103 | static u32 ovs_ct_get_mark(const struct nf_conn *ct) |
104 | { | |
105 | #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) | |
106 | return ct ? ct->mark : 0; | |
107 | #else | |
108 | return 0; | |
109 | #endif | |
110 | } | |
111 | ||
33db4125 JS |
112 | static void ovs_ct_get_labels(const struct nf_conn *ct, |
113 | struct ovs_key_ct_labels *labels) | |
c2ac6673 JS |
114 | { |
115 | struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL; | |
116 | ||
117 | if (cl) { | |
118 | size_t len = cl->words * sizeof(long); | |
119 | ||
33db4125 JS |
120 | if (len > OVS_CT_LABELS_LEN) |
121 | len = OVS_CT_LABELS_LEN; | |
122 | else if (len < OVS_CT_LABELS_LEN) | |
123 | memset(labels, 0, OVS_CT_LABELS_LEN); | |
124 | memcpy(labels, cl->bits, len); | |
c2ac6673 | 125 | } else { |
33db4125 | 126 | memset(labels, 0, OVS_CT_LABELS_LEN); |
c2ac6673 JS |
127 | } |
128 | } | |
129 | ||
7f8a436e | 130 | static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state, |
182e3042 JS |
131 | const struct nf_conntrack_zone *zone, |
132 | const struct nf_conn *ct) | |
7f8a436e JS |
133 | { |
134 | key->ct.state = state; | |
135 | key->ct.zone = zone->id; | |
0d5cdef8 | 136 | key->ct.mark = ovs_ct_get_mark(ct); |
33db4125 | 137 | ovs_ct_get_labels(ct, &key->ct.labels); |
7f8a436e JS |
138 | } |
139 | ||
140 | /* Update 'key' based on skb->nfct. If 'post_ct' is true, then OVS has | |
141 | * previously sent the packet to conntrack via the ct action. | |
142 | */ | |
143 | static void ovs_ct_update_key(const struct sk_buff *skb, | |
d110986c | 144 | const struct ovs_conntrack_info *info, |
7f8a436e JS |
145 | struct sw_flow_key *key, bool post_ct) |
146 | { | |
147 | const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt; | |
148 | enum ip_conntrack_info ctinfo; | |
149 | struct nf_conn *ct; | |
150 | u8 state = 0; | |
151 | ||
152 | ct = nf_ct_get(skb, &ctinfo); | |
153 | if (ct) { | |
154 | state = ovs_ct_get_state(ctinfo); | |
9f13ded8 | 155 | /* All unconfirmed entries are NEW connections. */ |
4f0909ee JS |
156 | if (!nf_ct_is_confirmed(ct)) |
157 | state |= OVS_CS_F_NEW; | |
9f13ded8 JR |
158 | /* OVS persists the related flag for the duration of the |
159 | * connection. | |
160 | */ | |
7f8a436e JS |
161 | if (ct->master) |
162 | state |= OVS_CS_F_RELATED; | |
163 | zone = nf_ct_zone(ct); | |
164 | } else if (post_ct) { | |
165 | state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID; | |
d110986c JS |
166 | if (info) |
167 | zone = &info->zone; | |
7f8a436e | 168 | } |
182e3042 | 169 | __ovs_ct_update_key(key, state, zone, ct); |
7f8a436e JS |
170 | } |
171 | ||
9f13ded8 JR |
172 | /* This is called to initialize CT key fields possibly coming in from the local |
173 | * stack. | |
174 | */ | |
7f8a436e JS |
175 | void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key) |
176 | { | |
d110986c | 177 | ovs_ct_update_key(skb, NULL, key, false); |
7f8a436e JS |
178 | } |
179 | ||
180 | int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb) | |
181 | { | |
fbccce59 | 182 | if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state)) |
7f8a436e JS |
183 | return -EMSGSIZE; |
184 | ||
185 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && | |
186 | nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone)) | |
187 | return -EMSGSIZE; | |
188 | ||
182e3042 JS |
189 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && |
190 | nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark)) | |
191 | return -EMSGSIZE; | |
192 | ||
9723e6ab | 193 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && |
33db4125 JS |
194 | nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(key->ct.labels), |
195 | &key->ct.labels)) | |
c2ac6673 JS |
196 | return -EMSGSIZE; |
197 | ||
182e3042 JS |
198 | return 0; |
199 | } | |
200 | ||
201 | static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key, | |
202 | u32 ct_mark, u32 mask) | |
203 | { | |
0d5cdef8 | 204 | #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) |
182e3042 JS |
205 | enum ip_conntrack_info ctinfo; |
206 | struct nf_conn *ct; | |
207 | u32 new_mark; | |
208 | ||
182e3042 JS |
209 | /* The connection could be invalid, in which case set_mark is no-op. */ |
210 | ct = nf_ct_get(skb, &ctinfo); | |
211 | if (!ct) | |
212 | return 0; | |
213 | ||
214 | new_mark = ct_mark | (ct->mark & ~(mask)); | |
215 | if (ct->mark != new_mark) { | |
216 | ct->mark = new_mark; | |
217 | nf_conntrack_event_cache(IPCT_MARK, ct); | |
218 | key->ct.mark = new_mark; | |
219 | } | |
220 | ||
7f8a436e | 221 | return 0; |
0d5cdef8 JS |
222 | #else |
223 | return -ENOTSUPP; | |
224 | #endif | |
7f8a436e JS |
225 | } |
226 | ||
33db4125 JS |
227 | static int ovs_ct_set_labels(struct sk_buff *skb, struct sw_flow_key *key, |
228 | const struct ovs_key_ct_labels *labels, | |
229 | const struct ovs_key_ct_labels *mask) | |
c2ac6673 JS |
230 | { |
231 | enum ip_conntrack_info ctinfo; | |
232 | struct nf_conn_labels *cl; | |
233 | struct nf_conn *ct; | |
234 | int err; | |
235 | ||
c2ac6673 JS |
236 | /* The connection could be invalid, in which case set_label is no-op.*/ |
237 | ct = nf_ct_get(skb, &ctinfo); | |
238 | if (!ct) | |
239 | return 0; | |
240 | ||
241 | cl = nf_ct_labels_find(ct); | |
242 | if (!cl) { | |
243 | nf_ct_labels_ext_add(ct); | |
244 | cl = nf_ct_labels_find(ct); | |
245 | } | |
33db4125 | 246 | if (!cl || cl->words * sizeof(long) < OVS_CT_LABELS_LEN) |
c2ac6673 JS |
247 | return -ENOSPC; |
248 | ||
33db4125 JS |
249 | err = nf_connlabels_replace(ct, (u32 *)labels, (u32 *)mask, |
250 | OVS_CT_LABELS_LEN / sizeof(u32)); | |
c2ac6673 JS |
251 | if (err) |
252 | return err; | |
253 | ||
33db4125 | 254 | ovs_ct_get_labels(ct, &key->ct.labels); |
c2ac6673 JS |
255 | return 0; |
256 | } | |
257 | ||
cae3a262 JS |
258 | /* 'skb' should already be pulled to nh_ofs. */ |
259 | static int ovs_ct_helper(struct sk_buff *skb, u16 proto) | |
260 | { | |
261 | const struct nf_conntrack_helper *helper; | |
262 | const struct nf_conn_help *help; | |
263 | enum ip_conntrack_info ctinfo; | |
264 | unsigned int protoff; | |
265 | struct nf_conn *ct; | |
266 | ||
267 | ct = nf_ct_get(skb, &ctinfo); | |
268 | if (!ct || ctinfo == IP_CT_RELATED_REPLY) | |
269 | return NF_ACCEPT; | |
270 | ||
271 | help = nfct_help(ct); | |
272 | if (!help) | |
273 | return NF_ACCEPT; | |
274 | ||
275 | helper = rcu_dereference(help->helper); | |
276 | if (!helper) | |
277 | return NF_ACCEPT; | |
278 | ||
279 | switch (proto) { | |
280 | case NFPROTO_IPV4: | |
281 | protoff = ip_hdrlen(skb); | |
282 | break; | |
283 | case NFPROTO_IPV6: { | |
284 | u8 nexthdr = ipv6_hdr(skb)->nexthdr; | |
285 | __be16 frag_off; | |
cc570605 | 286 | int ofs; |
cae3a262 | 287 | |
cc570605 JS |
288 | ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr, |
289 | &frag_off); | |
290 | if (ofs < 0 || (frag_off & htons(~0x7)) != 0) { | |
cae3a262 JS |
291 | pr_debug("proto header not found\n"); |
292 | return NF_ACCEPT; | |
293 | } | |
cc570605 | 294 | protoff = ofs; |
cae3a262 JS |
295 | break; |
296 | } | |
297 | default: | |
298 | WARN_ONCE(1, "helper invoked on non-IP family!"); | |
299 | return NF_DROP; | |
300 | } | |
301 | ||
302 | return helper->help(skb, protoff, ct, ctinfo); | |
303 | } | |
304 | ||
74c16618 JS |
305 | /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero |
306 | * value if 'skb' is freed. | |
307 | */ | |
7f8a436e JS |
308 | static int handle_fragments(struct net *net, struct sw_flow_key *key, |
309 | u16 zone, struct sk_buff *skb) | |
310 | { | |
311 | struct ovs_skb_cb ovs_cb = *OVS_CB(skb); | |
daaa7d64 | 312 | int err; |
7f8a436e JS |
313 | |
314 | if (key->eth.type == htons(ETH_P_IP)) { | |
315 | enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone; | |
7f8a436e JS |
316 | |
317 | memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); | |
19bcf9f2 | 318 | err = ip_defrag(net, skb, user); |
7f8a436e JS |
319 | if (err) |
320 | return err; | |
321 | ||
322 | ovs_cb.mru = IPCB(skb)->frag_max_size; | |
7f8a436e | 323 | #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) |
74c16618 | 324 | } else if (key->eth.type == htons(ETH_P_IPV6)) { |
7f8a436e | 325 | enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone; |
7f8a436e JS |
326 | |
327 | memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm)); | |
daaa7d64 FW |
328 | err = nf_ct_frag6_gather(net, skb, user); |
329 | if (err) | |
330 | return err; | |
7f8a436e | 331 | |
daaa7d64 | 332 | key->ip.proto = ipv6_hdr(skb)->nexthdr; |
7f8a436e | 333 | ovs_cb.mru = IP6CB(skb)->frag_max_size; |
7f8a436e JS |
334 | #endif |
335 | } else { | |
74c16618 | 336 | kfree_skb(skb); |
7f8a436e JS |
337 | return -EPFNOSUPPORT; |
338 | } | |
339 | ||
340 | key->ip.frag = OVS_FRAG_TYPE_NONE; | |
341 | skb_clear_hash(skb); | |
342 | skb->ignore_df = 1; | |
343 | *OVS_CB(skb) = ovs_cb; | |
344 | ||
345 | return 0; | |
346 | } | |
347 | ||
348 | static struct nf_conntrack_expect * | |
349 | ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone, | |
350 | u16 proto, const struct sk_buff *skb) | |
351 | { | |
352 | struct nf_conntrack_tuple tuple; | |
353 | ||
a31f1adc | 354 | if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple)) |
7f8a436e JS |
355 | return NULL; |
356 | return __nf_ct_expect_find(net, zone, &tuple); | |
357 | } | |
358 | ||
359 | /* Determine whether skb->nfct is equal to the result of conntrack lookup. */ | |
360 | static bool skb_nfct_cached(const struct net *net, const struct sk_buff *skb, | |
361 | const struct ovs_conntrack_info *info) | |
362 | { | |
363 | enum ip_conntrack_info ctinfo; | |
364 | struct nf_conn *ct; | |
365 | ||
366 | ct = nf_ct_get(skb, &ctinfo); | |
367 | if (!ct) | |
368 | return false; | |
369 | if (!net_eq(net, read_pnet(&ct->ct_net))) | |
370 | return false; | |
371 | if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct))) | |
372 | return false; | |
cae3a262 JS |
373 | if (info->helper) { |
374 | struct nf_conn_help *help; | |
375 | ||
376 | help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER); | |
377 | if (help && rcu_access_pointer(help->helper) != info->helper) | |
378 | return false; | |
379 | } | |
7f8a436e JS |
380 | |
381 | return true; | |
382 | } | |
383 | ||
9f13ded8 JR |
384 | /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if |
385 | * not done already. Update key with new CT state. | |
386 | * Note that if the packet is deemed invalid by conntrack, skb->nfct will be | |
387 | * set to NULL and 0 will be returned. | |
388 | */ | |
4f0909ee | 389 | static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key, |
7f8a436e JS |
390 | const struct ovs_conntrack_info *info, |
391 | struct sk_buff *skb) | |
392 | { | |
393 | /* If we are recirculating packets to match on conntrack fields and | |
394 | * committing with a separate conntrack action, then we don't need to | |
395 | * actually run the packet through conntrack twice unless it's for a | |
396 | * different zone. | |
397 | */ | |
398 | if (!skb_nfct_cached(net, skb, info)) { | |
399 | struct nf_conn *tmpl = info->ct; | |
400 | ||
401 | /* Associate skb with specified zone. */ | |
402 | if (tmpl) { | |
403 | if (skb->nfct) | |
404 | nf_conntrack_put(skb->nfct); | |
405 | nf_conntrack_get(&tmpl->ct_general); | |
406 | skb->nfct = &tmpl->ct_general; | |
407 | skb->nfctinfo = IP_CT_NEW; | |
408 | } | |
409 | ||
410 | if (nf_conntrack_in(net, info->family, NF_INET_PRE_ROUTING, | |
411 | skb) != NF_ACCEPT) | |
412 | return -ENOENT; | |
cae3a262 JS |
413 | |
414 | if (ovs_ct_helper(skb, info->family) != NF_ACCEPT) { | |
415 | WARN_ONCE(1, "helper rejected packet"); | |
416 | return -EINVAL; | |
417 | } | |
7f8a436e JS |
418 | } |
419 | ||
d110986c | 420 | ovs_ct_update_key(skb, info, key, true); |
4f0909ee | 421 | |
7f8a436e JS |
422 | return 0; |
423 | } | |
424 | ||
425 | /* Lookup connection and read fields into key. */ | |
426 | static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key, | |
427 | const struct ovs_conntrack_info *info, | |
428 | struct sk_buff *skb) | |
429 | { | |
430 | struct nf_conntrack_expect *exp; | |
431 | ||
9f13ded8 JR |
432 | /* If we pass an expected packet through nf_conntrack_in() the |
433 | * expectation is typically removed, but the packet could still be | |
434 | * lost in upcall processing. To prevent this from happening we | |
435 | * perform an explicit expectation lookup. Expected connections are | |
436 | * always new, and will be passed through conntrack only when they are | |
437 | * committed, as it is OK to remove the expectation at that time. | |
438 | */ | |
7f8a436e JS |
439 | exp = ovs_ct_expect_find(net, &info->zone, info->family, skb); |
440 | if (exp) { | |
441 | u8 state; | |
442 | ||
443 | state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED; | |
182e3042 | 444 | __ovs_ct_update_key(key, state, &info->zone, exp->master); |
7f8a436e JS |
445 | } else { |
446 | int err; | |
447 | ||
448 | err = __ovs_ct_lookup(net, key, info, skb); | |
449 | if (err) | |
450 | return err; | |
7f8a436e JS |
451 | } |
452 | ||
453 | return 0; | |
454 | } | |
455 | ||
456 | /* Lookup connection and confirm if unconfirmed. */ | |
457 | static int ovs_ct_commit(struct net *net, struct sw_flow_key *key, | |
458 | const struct ovs_conntrack_info *info, | |
459 | struct sk_buff *skb) | |
460 | { | |
461 | u8 state; | |
462 | int err; | |
463 | ||
464 | state = key->ct.state; | |
465 | if (key->ct.zone == info->zone.id && | |
466 | ((state & OVS_CS_F_TRACKED) && !(state & OVS_CS_F_NEW))) { | |
467 | /* Previous lookup has shown that this connection is already | |
468 | * tracked and committed. Skip committing. | |
469 | */ | |
470 | return 0; | |
471 | } | |
472 | ||
473 | err = __ovs_ct_lookup(net, key, info, skb); | |
474 | if (err) | |
475 | return err; | |
9f13ded8 | 476 | /* This is a no-op if the connection has already been confirmed. */ |
7f8a436e JS |
477 | if (nf_conntrack_confirm(skb) != NF_ACCEPT) |
478 | return -EINVAL; | |
479 | ||
7f8a436e JS |
480 | return 0; |
481 | } | |
482 | ||
33db4125 | 483 | static bool labels_nonzero(const struct ovs_key_ct_labels *labels) |
c2ac6673 JS |
484 | { |
485 | size_t i; | |
486 | ||
33db4125 JS |
487 | for (i = 0; i < sizeof(*labels); i++) |
488 | if (labels->ct_labels[i]) | |
c2ac6673 JS |
489 | return true; |
490 | ||
491 | return false; | |
492 | } | |
493 | ||
74c16618 JS |
494 | /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero |
495 | * value if 'skb' is freed. | |
496 | */ | |
7f8a436e JS |
497 | int ovs_ct_execute(struct net *net, struct sk_buff *skb, |
498 | struct sw_flow_key *key, | |
499 | const struct ovs_conntrack_info *info) | |
500 | { | |
501 | int nh_ofs; | |
502 | int err; | |
503 | ||
504 | /* The conntrack module expects to be working at L3. */ | |
505 | nh_ofs = skb_network_offset(skb); | |
506 | skb_pull(skb, nh_ofs); | |
507 | ||
508 | if (key->ip.frag != OVS_FRAG_TYPE_NONE) { | |
509 | err = handle_fragments(net, key, info->zone.id, skb); | |
510 | if (err) | |
511 | return err; | |
512 | } | |
513 | ||
ab38a7b5 | 514 | if (info->commit) |
7f8a436e JS |
515 | err = ovs_ct_commit(net, key, info, skb); |
516 | else | |
517 | err = ovs_ct_lookup(net, key, info, skb); | |
182e3042 JS |
518 | if (err) |
519 | goto err; | |
7f8a436e | 520 | |
c2ac6673 | 521 | if (info->mark.mask) { |
182e3042 JS |
522 | err = ovs_ct_set_mark(skb, key, info->mark.value, |
523 | info->mark.mask); | |
c2ac6673 JS |
524 | if (err) |
525 | goto err; | |
526 | } | |
33db4125 JS |
527 | if (labels_nonzero(&info->labels.mask)) |
528 | err = ovs_ct_set_labels(skb, key, &info->labels.value, | |
529 | &info->labels.mask); | |
182e3042 | 530 | err: |
7f8a436e | 531 | skb_push(skb, nh_ofs); |
74c16618 JS |
532 | if (err) |
533 | kfree_skb(skb); | |
7f8a436e JS |
534 | return err; |
535 | } | |
536 | ||
cae3a262 JS |
537 | static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name, |
538 | const struct sw_flow_key *key, bool log) | |
539 | { | |
540 | struct nf_conntrack_helper *helper; | |
541 | struct nf_conn_help *help; | |
542 | ||
543 | helper = nf_conntrack_helper_try_module_get(name, info->family, | |
544 | key->ip.proto); | |
545 | if (!helper) { | |
546 | OVS_NLERR(log, "Unknown helper \"%s\"", name); | |
547 | return -EINVAL; | |
548 | } | |
549 | ||
550 | help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL); | |
551 | if (!help) { | |
552 | module_put(helper->me); | |
553 | return -ENOMEM; | |
554 | } | |
555 | ||
556 | rcu_assign_pointer(help->helper, helper); | |
557 | info->helper = helper; | |
558 | return 0; | |
559 | } | |
560 | ||
7f8a436e | 561 | static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = { |
ab38a7b5 | 562 | [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 }, |
7f8a436e JS |
563 | [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16), |
564 | .maxlen = sizeof(u16) }, | |
182e3042 JS |
565 | [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark), |
566 | .maxlen = sizeof(struct md_mark) }, | |
33db4125 JS |
567 | [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels), |
568 | .maxlen = sizeof(struct md_labels) }, | |
cae3a262 JS |
569 | [OVS_CT_ATTR_HELPER] = { .minlen = 1, |
570 | .maxlen = NF_CT_HELPER_NAME_LEN } | |
7f8a436e JS |
571 | }; |
572 | ||
573 | static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info, | |
cae3a262 | 574 | const char **helper, bool log) |
7f8a436e JS |
575 | { |
576 | struct nlattr *a; | |
577 | int rem; | |
578 | ||
579 | nla_for_each_nested(a, attr, rem) { | |
580 | int type = nla_type(a); | |
581 | int maxlen = ovs_ct_attr_lens[type].maxlen; | |
582 | int minlen = ovs_ct_attr_lens[type].minlen; | |
583 | ||
584 | if (type > OVS_CT_ATTR_MAX) { | |
585 | OVS_NLERR(log, | |
586 | "Unknown conntrack attr (type=%d, max=%d)", | |
587 | type, OVS_CT_ATTR_MAX); | |
588 | return -EINVAL; | |
589 | } | |
590 | if (nla_len(a) < minlen || nla_len(a) > maxlen) { | |
591 | OVS_NLERR(log, | |
592 | "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)", | |
593 | type, nla_len(a), maxlen); | |
594 | return -EINVAL; | |
595 | } | |
596 | ||
597 | switch (type) { | |
ab38a7b5 JS |
598 | case OVS_CT_ATTR_COMMIT: |
599 | info->commit = true; | |
7f8a436e JS |
600 | break; |
601 | #ifdef CONFIG_NF_CONNTRACK_ZONES | |
602 | case OVS_CT_ATTR_ZONE: | |
603 | info->zone.id = nla_get_u16(a); | |
604 | break; | |
182e3042 JS |
605 | #endif |
606 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
607 | case OVS_CT_ATTR_MARK: { | |
608 | struct md_mark *mark = nla_data(a); | |
609 | ||
e754ec69 JS |
610 | if (!mark->mask) { |
611 | OVS_NLERR(log, "ct_mark mask cannot be 0"); | |
612 | return -EINVAL; | |
613 | } | |
182e3042 JS |
614 | info->mark = *mark; |
615 | break; | |
616 | } | |
c2ac6673 JS |
617 | #endif |
618 | #ifdef CONFIG_NF_CONNTRACK_LABELS | |
33db4125 JS |
619 | case OVS_CT_ATTR_LABELS: { |
620 | struct md_labels *labels = nla_data(a); | |
c2ac6673 | 621 | |
e754ec69 JS |
622 | if (!labels_nonzero(&labels->mask)) { |
623 | OVS_NLERR(log, "ct_labels mask cannot be 0"); | |
624 | return -EINVAL; | |
625 | } | |
33db4125 | 626 | info->labels = *labels; |
c2ac6673 JS |
627 | break; |
628 | } | |
7f8a436e | 629 | #endif |
cae3a262 JS |
630 | case OVS_CT_ATTR_HELPER: |
631 | *helper = nla_data(a); | |
632 | if (!memchr(*helper, '\0', nla_len(a))) { | |
633 | OVS_NLERR(log, "Invalid conntrack helper"); | |
634 | return -EINVAL; | |
635 | } | |
636 | break; | |
7f8a436e JS |
637 | default: |
638 | OVS_NLERR(log, "Unknown conntrack attr (%d)", | |
639 | type); | |
640 | return -EINVAL; | |
641 | } | |
642 | } | |
643 | ||
644 | if (rem > 0) { | |
645 | OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem); | |
646 | return -EINVAL; | |
647 | } | |
648 | ||
649 | return 0; | |
650 | } | |
651 | ||
c2ac6673 | 652 | bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr) |
7f8a436e JS |
653 | { |
654 | if (attr == OVS_KEY_ATTR_CT_STATE) | |
655 | return true; | |
656 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && | |
657 | attr == OVS_KEY_ATTR_CT_ZONE) | |
658 | return true; | |
182e3042 JS |
659 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && |
660 | attr == OVS_KEY_ATTR_CT_MARK) | |
661 | return true; | |
c2ac6673 | 662 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && |
33db4125 | 663 | attr == OVS_KEY_ATTR_CT_LABELS) { |
c2ac6673 JS |
664 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
665 | ||
666 | return ovs_net->xt_label; | |
667 | } | |
7f8a436e JS |
668 | |
669 | return false; | |
670 | } | |
671 | ||
672 | int ovs_ct_copy_action(struct net *net, const struct nlattr *attr, | |
673 | const struct sw_flow_key *key, | |
674 | struct sw_flow_actions **sfa, bool log) | |
675 | { | |
676 | struct ovs_conntrack_info ct_info; | |
cae3a262 | 677 | const char *helper = NULL; |
7f8a436e JS |
678 | u16 family; |
679 | int err; | |
680 | ||
681 | family = key_to_nfproto(key); | |
682 | if (family == NFPROTO_UNSPEC) { | |
683 | OVS_NLERR(log, "ct family unspecified"); | |
684 | return -EINVAL; | |
685 | } | |
686 | ||
687 | memset(&ct_info, 0, sizeof(ct_info)); | |
688 | ct_info.family = family; | |
689 | ||
690 | nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID, | |
691 | NF_CT_DEFAULT_ZONE_DIR, 0); | |
692 | ||
cae3a262 | 693 | err = parse_ct(attr, &ct_info, &helper, log); |
7f8a436e JS |
694 | if (err) |
695 | return err; | |
696 | ||
697 | /* Set up template for tracking connections in specific zones. */ | |
698 | ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL); | |
699 | if (!ct_info.ct) { | |
700 | OVS_NLERR(log, "Failed to allocate conntrack template"); | |
701 | return -ENOMEM; | |
702 | } | |
90c7afc9 JS |
703 | |
704 | __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status); | |
705 | nf_conntrack_get(&ct_info.ct->ct_general); | |
706 | ||
cae3a262 JS |
707 | if (helper) { |
708 | err = ovs_ct_add_helper(&ct_info, helper, key, log); | |
709 | if (err) | |
710 | goto err_free_ct; | |
711 | } | |
7f8a436e JS |
712 | |
713 | err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info, | |
714 | sizeof(ct_info), log); | |
715 | if (err) | |
716 | goto err_free_ct; | |
717 | ||
7f8a436e JS |
718 | return 0; |
719 | err_free_ct: | |
2f3ab9f9 | 720 | __ovs_ct_free_action(&ct_info); |
7f8a436e JS |
721 | return err; |
722 | } | |
723 | ||
724 | int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info, | |
725 | struct sk_buff *skb) | |
726 | { | |
727 | struct nlattr *start; | |
728 | ||
729 | start = nla_nest_start(skb, OVS_ACTION_ATTR_CT); | |
730 | if (!start) | |
731 | return -EMSGSIZE; | |
732 | ||
ab38a7b5 | 733 | if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT)) |
7f8a436e JS |
734 | return -EMSGSIZE; |
735 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && | |
736 | nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id)) | |
737 | return -EMSGSIZE; | |
e754ec69 | 738 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask && |
182e3042 JS |
739 | nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark), |
740 | &ct_info->mark)) | |
741 | return -EMSGSIZE; | |
c2ac6673 | 742 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && |
e754ec69 | 743 | labels_nonzero(&ct_info->labels.mask) && |
33db4125 JS |
744 | nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels), |
745 | &ct_info->labels)) | |
c2ac6673 | 746 | return -EMSGSIZE; |
cae3a262 JS |
747 | if (ct_info->helper) { |
748 | if (nla_put_string(skb, OVS_CT_ATTR_HELPER, | |
749 | ct_info->helper->name)) | |
750 | return -EMSGSIZE; | |
751 | } | |
7f8a436e JS |
752 | |
753 | nla_nest_end(skb, start); | |
754 | ||
755 | return 0; | |
756 | } | |
757 | ||
758 | void ovs_ct_free_action(const struct nlattr *a) | |
759 | { | |
760 | struct ovs_conntrack_info *ct_info = nla_data(a); | |
761 | ||
2f3ab9f9 JS |
762 | __ovs_ct_free_action(ct_info); |
763 | } | |
764 | ||
765 | static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info) | |
766 | { | |
cae3a262 JS |
767 | if (ct_info->helper) |
768 | module_put(ct_info->helper->me); | |
7f8a436e JS |
769 | if (ct_info->ct) |
770 | nf_ct_put(ct_info->ct); | |
771 | } | |
c2ac6673 JS |
772 | |
773 | void ovs_ct_init(struct net *net) | |
774 | { | |
33db4125 | 775 | unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE; |
c2ac6673 JS |
776 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
777 | ||
778 | if (nf_connlabels_get(net, n_bits)) { | |
779 | ovs_net->xt_label = false; | |
780 | OVS_NLERR(true, "Failed to set connlabel length"); | |
781 | } else { | |
782 | ovs_net->xt_label = true; | |
783 | } | |
784 | } | |
785 | ||
786 | void ovs_ct_exit(struct net *net) | |
787 | { | |
788 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); | |
789 | ||
790 | if (ovs_net->xt_label) | |
791 | nf_connlabels_put(net); | |
792 | } |