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