]>
Commit | Line | Data |
---|---|---|
a94ebc39 JS |
1 | /* |
2 | * Copyright (c) 2015 Nicira, Inc. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of version 2 of the GNU General Public | |
6 | * License as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | */ | |
13 | ||
14 | #include <linux/kconfig.h> | |
15 | #include <linux/version.h> | |
16 | ||
8063e095 | 17 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
a94ebc39 JS |
18 | |
19 | #include <linux/module.h> | |
20 | #include <linux/openvswitch.h> | |
f8f97cdc JR |
21 | #include <linux/tcp.h> |
22 | #include <linux/udp.h> | |
23 | #include <linux/sctp.h> | |
cb2a5486 | 24 | #include <linux/static_key.h> |
a94ebc39 | 25 | #include <net/ip.h> |
cb2a5486 | 26 | #include <net/genetlink.h> |
a94ebc39 | 27 | #include <net/netfilter/nf_conntrack_core.h> |
cb2a5486 | 28 | #include <net/netfilter/nf_conntrack_count.h> |
11251c17 | 29 | #include <net/netfilter/nf_conntrack_helper.h> |
038e34ab | 30 | #include <net/netfilter/nf_conntrack_labels.h> |
f8f97cdc | 31 | #include <net/netfilter/nf_conntrack_seqadj.h> |
a94ebc39 JS |
32 | #include <net/netfilter/nf_conntrack_zones.h> |
33 | #include <net/netfilter/ipv6/nf_defrag_ipv6.h> | |
34 | ||
f8f97cdc JR |
35 | #ifdef CONFIG_NF_NAT_NEEDED |
36 | #include <linux/netfilter/nf_nat.h> | |
37 | #include <net/netfilter/nf_nat_core.h> | |
38 | #include <net/netfilter/nf_nat_l3proto.h> | |
39 | #endif | |
40 | ||
a94ebc39 JS |
41 | #include "datapath.h" |
42 | #include "conntrack.h" | |
43 | #include "flow.h" | |
44 | #include "flow_netlink.h" | |
86c2eb45 | 45 | #include "gso.h" |
a94ebc39 | 46 | |
fdec3c17 YHW |
47 | #ifndef HAVE_NF_NAT_RANGE2 |
48 | #define nf_nat_range2 nf_nat_range | |
49 | #endif | |
50 | ||
a94ebc39 | 51 | struct ovs_ct_len_tbl { |
f8f97cdc JR |
52 | int maxlen; |
53 | int minlen; | |
a94ebc39 JS |
54 | }; |
55 | ||
372ce973 JS |
56 | /* Metadata mark for masked write to conntrack mark */ |
57 | struct md_mark { | |
58 | u32 value; | |
59 | u32 mask; | |
60 | }; | |
61 | ||
038e34ab | 62 | /* Metadata label for masked write to conntrack label. */ |
c05e2094 JS |
63 | struct md_labels { |
64 | struct ovs_key_ct_labels value; | |
65 | struct ovs_key_ct_labels mask; | |
038e34ab JS |
66 | }; |
67 | ||
f8f97cdc JR |
68 | enum ovs_ct_nat { |
69 | OVS_CT_NAT = 1 << 0, /* NAT for committed connections only. */ | |
70 | OVS_CT_SRC_NAT = 1 << 1, /* Source NAT for NEW connections. */ | |
71 | OVS_CT_DST_NAT = 1 << 2, /* Destination NAT for NEW connections. */ | |
72 | }; | |
73 | ||
a94ebc39 JS |
74 | /* Conntrack action context for execution. */ |
75 | struct ovs_conntrack_info { | |
11251c17 | 76 | struct nf_conntrack_helper *helper; |
a94ebc39 JS |
77 | struct nf_conntrack_zone zone; |
78 | struct nf_conn *ct; | |
c05e2094 | 79 | u8 commit : 1; |
f8f97cdc | 80 | u8 nat : 3; /* enum ovs_ct_nat */ |
9f1de150 | 81 | u8 random_fully_compat : 1; /* bool */ |
b80e259f | 82 | u8 force : 1; |
8e83854c | 83 | u8 have_eventmask : 1; |
a94ebc39 | 84 | u16 family; |
8e83854c | 85 | u32 eventmask; /* Mask of 1 << IPCT_*. */ |
372ce973 | 86 | struct md_mark mark; |
c05e2094 | 87 | struct md_labels labels; |
f8f97cdc | 88 | #ifdef CONFIG_NF_NAT_NEEDED |
fdec3c17 | 89 | struct nf_nat_range2 range; /* Only present for SRC NAT and DST NAT. */ |
f8f97cdc | 90 | #endif |
a94ebc39 JS |
91 | }; |
92 | ||
cb2a5486 YHW |
93 | #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) |
94 | #define OVS_CT_LIMIT_UNLIMITED 0 | |
95 | #define OVS_CT_LIMIT_DEFAULT OVS_CT_LIMIT_UNLIMITED | |
96 | #define CT_LIMIT_HASH_BUCKETS 512 | |
97 | static DEFINE_STATIC_KEY_FALSE(ovs_ct_limit_enabled); | |
98 | ||
99 | struct ovs_ct_limit { | |
100 | /* Elements in ovs_ct_limit_info->limits hash table */ | |
101 | struct hlist_node hlist_node; | |
102 | struct rcu_head rcu; | |
103 | u16 zone; | |
104 | u32 limit; | |
105 | }; | |
106 | ||
107 | struct ovs_ct_limit_info { | |
108 | u32 default_limit; | |
109 | struct hlist_head *limits; | |
110 | struct nf_conncount_data *data; | |
111 | }; | |
112 | ||
113 | static const struct nla_policy ct_limit_policy[OVS_CT_LIMIT_ATTR_MAX + 1] = { | |
114 | [OVS_CT_LIMIT_ATTR_ZONE_LIMIT] = { .type = NLA_NESTED, }, | |
115 | }; | |
116 | #endif | |
117 | ||
7162cae4 JR |
118 | static bool labels_nonzero(const struct ovs_key_ct_labels *labels); |
119 | ||
11251c17 JS |
120 | static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info); |
121 | ||
a94ebc39 JS |
122 | static u16 key_to_nfproto(const struct sw_flow_key *key) |
123 | { | |
124 | switch (ntohs(key->eth.type)) { | |
125 | case ETH_P_IP: | |
126 | return NFPROTO_IPV4; | |
127 | case ETH_P_IPV6: | |
128 | return NFPROTO_IPV6; | |
129 | default: | |
130 | return NFPROTO_UNSPEC; | |
131 | } | |
132 | } | |
133 | ||
134 | /* Map SKB connection state into the values used by flow definition. */ | |
135 | static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo) | |
136 | { | |
137 | u8 ct_state = OVS_CS_F_TRACKED; | |
138 | ||
139 | switch (ctinfo) { | |
140 | case IP_CT_ESTABLISHED_REPLY: | |
141 | case IP_CT_RELATED_REPLY: | |
a94ebc39 JS |
142 | ct_state |= OVS_CS_F_REPLY_DIR; |
143 | break; | |
144 | default: | |
145 | break; | |
146 | } | |
147 | ||
148 | switch (ctinfo) { | |
149 | case IP_CT_ESTABLISHED: | |
150 | case IP_CT_ESTABLISHED_REPLY: | |
151 | ct_state |= OVS_CS_F_ESTABLISHED; | |
152 | break; | |
153 | case IP_CT_RELATED: | |
154 | case IP_CT_RELATED_REPLY: | |
155 | ct_state |= OVS_CS_F_RELATED; | |
156 | break; | |
157 | case IP_CT_NEW: | |
a94ebc39 JS |
158 | ct_state |= OVS_CS_F_NEW; |
159 | break; | |
160 | default: | |
161 | break; | |
162 | } | |
163 | ||
164 | return ct_state; | |
165 | } | |
166 | ||
c05e2094 JS |
167 | static u32 ovs_ct_get_mark(const struct nf_conn *ct) |
168 | { | |
169 | #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) | |
170 | return ct ? ct->mark : 0; | |
171 | #else | |
172 | return 0; | |
173 | #endif | |
174 | } | |
175 | ||
237f9413 JR |
176 | /* Guard against conntrack labels max size shrinking below 128 bits. */ |
177 | #if NF_CT_LABELS_MAX_SIZE < 16 | |
178 | #error NF_CT_LABELS_MAX_SIZE must be at least 16 bytes | |
179 | #endif | |
180 | ||
c05e2094 JS |
181 | static void ovs_ct_get_labels(const struct nf_conn *ct, |
182 | struct ovs_key_ct_labels *labels) | |
038e34ab JS |
183 | { |
184 | struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL; | |
185 | ||
237f9413 JR |
186 | if (cl) |
187 | memcpy(labels, cl->bits, OVS_CT_LABELS_LEN); | |
188 | else | |
c05e2094 | 189 | memset(labels, 0, OVS_CT_LABELS_LEN); |
038e34ab JS |
190 | } |
191 | ||
c30b4cea JR |
192 | static void __ovs_ct_update_key_orig_tp(struct sw_flow_key *key, |
193 | const struct nf_conntrack_tuple *orig, | |
194 | u8 icmp_proto) | |
195 | { | |
1963a885 | 196 | key->ct_orig_proto = orig->dst.protonum; |
c30b4cea JR |
197 | if (orig->dst.protonum == icmp_proto) { |
198 | key->ct.orig_tp.src = htons(orig->dst.u.icmp.type); | |
199 | key->ct.orig_tp.dst = htons(orig->dst.u.icmp.code); | |
200 | } else { | |
201 | key->ct.orig_tp.src = orig->src.u.all; | |
202 | key->ct.orig_tp.dst = orig->dst.u.all; | |
203 | } | |
204 | } | |
205 | ||
a94ebc39 | 206 | static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state, |
372ce973 JS |
207 | const struct nf_conntrack_zone *zone, |
208 | const struct nf_conn *ct) | |
a94ebc39 | 209 | { |
1963a885 JR |
210 | key->ct_state = state; |
211 | key->ct_zone = zone->id; | |
c05e2094 JS |
212 | key->ct.mark = ovs_ct_get_mark(ct); |
213 | ovs_ct_get_labels(ct, &key->ct.labels); | |
c30b4cea JR |
214 | |
215 | if (ct) { | |
216 | const struct nf_conntrack_tuple *orig; | |
217 | ||
218 | /* Use the master if we have one. */ | |
219 | if (ct->master) | |
220 | ct = ct->master; | |
221 | orig = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple; | |
222 | ||
223 | /* IP version must match with the master connection. */ | |
224 | if (key->eth.type == htons(ETH_P_IP) && | |
225 | nf_ct_l3num(ct) == NFPROTO_IPV4) { | |
226 | key->ipv4.ct_orig.src = orig->src.u3.ip; | |
227 | key->ipv4.ct_orig.dst = orig->dst.u3.ip; | |
228 | __ovs_ct_update_key_orig_tp(key, orig, IPPROTO_ICMP); | |
229 | return; | |
230 | } else if (key->eth.type == htons(ETH_P_IPV6) && | |
231 | !sw_flow_key_is_nd(key) && | |
232 | nf_ct_l3num(ct) == NFPROTO_IPV6) { | |
233 | key->ipv6.ct_orig.src = orig->src.u3.in6; | |
234 | key->ipv6.ct_orig.dst = orig->dst.u3.in6; | |
235 | __ovs_ct_update_key_orig_tp(key, orig, NEXTHDR_ICMP); | |
236 | return; | |
237 | } | |
238 | } | |
1963a885 | 239 | /* Clear 'ct_orig_proto' to mark the non-existence of conntrack |
c30b4cea JR |
240 | * original direction key fields. |
241 | */ | |
1963a885 | 242 | key->ct_orig_proto = 0; |
a94ebc39 JS |
243 | } |
244 | ||
e3c42eb8 | 245 | /* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has |
f8f97cdc JR |
246 | * previously sent the packet to conntrack via the ct action. If |
247 | * 'keep_nat_flags' is true, the existing NAT flags retained, else they are | |
248 | * initialized from the connection status. | |
a94ebc39 JS |
249 | */ |
250 | static void ovs_ct_update_key(const struct sk_buff *skb, | |
f23593a1 | 251 | const struct ovs_conntrack_info *info, |
f8f97cdc JR |
252 | struct sw_flow_key *key, bool post_ct, |
253 | bool keep_nat_flags) | |
a94ebc39 JS |
254 | { |
255 | const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt; | |
256 | enum ip_conntrack_info ctinfo; | |
257 | struct nf_conn *ct; | |
258 | u8 state = 0; | |
259 | ||
260 | ct = nf_ct_get(skb, &ctinfo); | |
261 | if (ct) { | |
262 | state = ovs_ct_get_state(ctinfo); | |
b0f251cd | 263 | /* All unconfirmed entries are NEW connections. */ |
c05e2094 JS |
264 | if (!nf_ct_is_confirmed(ct)) |
265 | state |= OVS_CS_F_NEW; | |
b0f251cd JR |
266 | /* OVS persists the related flag for the duration of the |
267 | * connection. | |
268 | */ | |
a94ebc39 JS |
269 | if (ct->master) |
270 | state |= OVS_CS_F_RELATED; | |
f8f97cdc | 271 | if (keep_nat_flags) { |
1963a885 | 272 | state |= key->ct_state & OVS_CS_F_NAT_MASK; |
f8f97cdc JR |
273 | } else { |
274 | if (ct->status & IPS_SRC_NAT) | |
275 | state |= OVS_CS_F_SRC_NAT; | |
276 | if (ct->status & IPS_DST_NAT) | |
277 | state |= OVS_CS_F_DST_NAT; | |
278 | } | |
a94ebc39 JS |
279 | zone = nf_ct_zone(ct); |
280 | } else if (post_ct) { | |
281 | state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID; | |
f23593a1 JS |
282 | if (info) |
283 | zone = &info->zone; | |
a94ebc39 | 284 | } |
372ce973 | 285 | __ovs_ct_update_key(key, state, zone, ct); |
a94ebc39 JS |
286 | } |
287 | ||
b0f251cd JR |
288 | /* This is called to initialize CT key fields possibly coming in from the local |
289 | * stack. | |
290 | */ | |
a94ebc39 JS |
291 | void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key) |
292 | { | |
f8f97cdc | 293 | ovs_ct_update_key(skb, NULL, key, false, false); |
a94ebc39 JS |
294 | } |
295 | ||
c30b4cea JR |
296 | #define IN6_ADDR_INITIALIZER(ADDR) \ |
297 | { (ADDR).s6_addr32[0], (ADDR).s6_addr32[1], \ | |
298 | (ADDR).s6_addr32[2], (ADDR).s6_addr32[3] } | |
299 | ||
300 | int ovs_ct_put_key(const struct sw_flow_key *swkey, | |
301 | const struct sw_flow_key *output, struct sk_buff *skb) | |
a94ebc39 | 302 | { |
1963a885 | 303 | if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, output->ct_state)) |
a94ebc39 JS |
304 | return -EMSGSIZE; |
305 | ||
306 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && | |
1963a885 | 307 | nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, output->ct_zone)) |
a94ebc39 JS |
308 | return -EMSGSIZE; |
309 | ||
372ce973 | 310 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && |
c30b4cea | 311 | nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, output->ct.mark)) |
372ce973 JS |
312 | return -EMSGSIZE; |
313 | ||
c05e2094 | 314 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && |
c30b4cea JR |
315 | nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(output->ct.labels), |
316 | &output->ct.labels)) | |
038e34ab JS |
317 | return -EMSGSIZE; |
318 | ||
1963a885 | 319 | if (swkey->ct_orig_proto) { |
c30b4cea JR |
320 | if (swkey->eth.type == htons(ETH_P_IP)) { |
321 | struct ovs_key_ct_tuple_ipv4 orig = { | |
322 | output->ipv4.ct_orig.src, | |
323 | output->ipv4.ct_orig.dst, | |
324 | output->ct.orig_tp.src, | |
325 | output->ct.orig_tp.dst, | |
1963a885 | 326 | output->ct_orig_proto, |
c30b4cea JR |
327 | }; |
328 | if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4, | |
329 | sizeof(orig), &orig)) | |
330 | return -EMSGSIZE; | |
331 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { | |
332 | struct ovs_key_ct_tuple_ipv6 orig = { | |
333 | IN6_ADDR_INITIALIZER(output->ipv6.ct_orig.src), | |
334 | IN6_ADDR_INITIALIZER(output->ipv6.ct_orig.dst), | |
335 | output->ct.orig_tp.src, | |
336 | output->ct.orig_tp.dst, | |
1963a885 | 337 | output->ct_orig_proto, |
c30b4cea JR |
338 | }; |
339 | if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6, | |
340 | sizeof(orig), &orig)) | |
341 | return -EMSGSIZE; | |
342 | } | |
343 | } | |
344 | ||
372ce973 JS |
345 | return 0; |
346 | } | |
347 | ||
efd40994 | 348 | static int ovs_ct_set_mark(struct nf_conn *ct, struct sw_flow_key *key, |
372ce973 JS |
349 | u32 ct_mark, u32 mask) |
350 | { | |
c05e2094 | 351 | #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) |
372ce973 JS |
352 | u32 new_mark; |
353 | ||
372ce973 JS |
354 | new_mark = ct_mark | (ct->mark & ~(mask)); |
355 | if (ct->mark != new_mark) { | |
356 | ct->mark = new_mark; | |
1b22e621 JR |
357 | if (nf_ct_is_confirmed(ct)) |
358 | nf_conntrack_event_cache(IPCT_MARK, ct); | |
372ce973 JS |
359 | key->ct.mark = new_mark; |
360 | } | |
361 | ||
a94ebc39 | 362 | return 0; |
c05e2094 JS |
363 | #else |
364 | return -ENOTSUPP; | |
365 | #endif | |
a94ebc39 JS |
366 | } |
367 | ||
efd40994 | 368 | static struct nf_conn_labels *ovs_ct_get_conn_labels(struct nf_conn *ct) |
038e34ab | 369 | { |
038e34ab | 370 | struct nf_conn_labels *cl; |
038e34ab JS |
371 | |
372 | cl = nf_ct_labels_find(ct); | |
373 | if (!cl) { | |
374 | nf_ct_labels_ext_add(ct); | |
375 | cl = nf_ct_labels_find(ct); | |
376 | } | |
9f04eabe | 377 | |
efd40994 JR |
378 | return cl; |
379 | } | |
380 | ||
381 | /* Initialize labels for a new, yet to be committed conntrack entry. Note that | |
382 | * since the new connection is not yet confirmed, and thus no-one else has | |
383 | * access to it's labels, we simply write them over. | |
384 | */ | |
385 | static int ovs_ct_init_labels(struct nf_conn *ct, struct sw_flow_key *key, | |
386 | const struct ovs_key_ct_labels *labels, | |
387 | const struct ovs_key_ct_labels *mask) | |
388 | { | |
7162cae4 JR |
389 | struct nf_conn_labels *cl, *master_cl; |
390 | bool have_mask = labels_nonzero(mask); | |
391 | ||
392 | /* Inherit master's labels to the related connection? */ | |
393 | master_cl = ct->master ? nf_ct_labels_find(ct->master) : NULL; | |
394 | ||
395 | if (!master_cl && !have_mask) | |
396 | return 0; /* Nothing to do. */ | |
efd40994 JR |
397 | |
398 | cl = ovs_ct_get_conn_labels(ct); | |
399 | if (!cl) | |
038e34ab JS |
400 | return -ENOSPC; |
401 | ||
28033773 JR |
402 | /* Inherit the master's labels, if any. Must use memcpy for backport |
403 | * as struct assignment only copies the length field in older | |
404 | * kernels. | |
405 | */ | |
7162cae4 | 406 | if (master_cl) |
28033773 | 407 | memcpy(cl->bits, master_cl->bits, OVS_CT_LABELS_LEN); |
7162cae4 JR |
408 | |
409 | if (have_mask) { | |
410 | u32 *dst = (u32 *)cl->bits; | |
411 | int i; | |
412 | ||
413 | for (i = 0; i < OVS_CT_LABELS_LEN_32; i++) | |
414 | dst[i] = (dst[i] & ~mask->ct_labels_32[i]) | | |
415 | (labels->ct_labels_32[i] | |
416 | & mask->ct_labels_32[i]); | |
417 | } | |
1b22e621 | 418 | |
efd40994 | 419 | /* Labels are included in the IPCTNL_MSG_CT_NEW event only if the |
100f4e14 | 420 | * IPCT_LABEL bit is set in the event cache. |
efd40994 JR |
421 | */ |
422 | nf_conntrack_event_cache(IPCT_LABEL, ct); | |
1b22e621 | 423 | |
efd40994 JR |
424 | memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN); |
425 | ||
426 | return 0; | |
427 | } | |
428 | ||
429 | static int ovs_ct_set_labels(struct nf_conn *ct, struct sw_flow_key *key, | |
430 | const struct ovs_key_ct_labels *labels, | |
431 | const struct ovs_key_ct_labels *mask) | |
432 | { | |
433 | struct nf_conn_labels *cl; | |
434 | int err; | |
435 | ||
436 | cl = ovs_ct_get_conn_labels(ct); | |
437 | if (!cl) | |
438 | return -ENOSPC; | |
439 | ||
440 | err = nf_connlabels_replace(ct, labels->ct_labels_32, | |
441 | mask->ct_labels_32, | |
442 | OVS_CT_LABELS_LEN_32); | |
443 | if (err) | |
444 | return err; | |
445 | ||
446 | memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN); | |
038e34ab | 447 | |
038e34ab JS |
448 | return 0; |
449 | } | |
450 | ||
11251c17 JS |
451 | /* 'skb' should already be pulled to nh_ofs. */ |
452 | static int ovs_ct_helper(struct sk_buff *skb, u16 proto) | |
453 | { | |
454 | const struct nf_conntrack_helper *helper; | |
455 | const struct nf_conn_help *help; | |
456 | enum ip_conntrack_info ctinfo; | |
457 | unsigned int protoff; | |
458 | struct nf_conn *ct; | |
4cc85f28 | 459 | u8 nexthdr; |
f8f97cdc | 460 | int err; |
11251c17 | 461 | |
3d47fa46 JH |
462 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0) |
463 | bool dst_set = false; | |
464 | struct rtable rt = { .rt_flags = 0 }; | |
465 | #endif | |
466 | ||
11251c17 JS |
467 | ct = nf_ct_get(skb, &ctinfo); |
468 | if (!ct || ctinfo == IP_CT_RELATED_REPLY) | |
469 | return NF_ACCEPT; | |
470 | ||
471 | help = nfct_help(ct); | |
472 | if (!help) | |
473 | return NF_ACCEPT; | |
474 | ||
475 | helper = rcu_dereference(help->helper); | |
476 | if (!helper) | |
477 | return NF_ACCEPT; | |
478 | ||
479 | switch (proto) { | |
480 | case NFPROTO_IPV4: | |
481 | protoff = ip_hdrlen(skb); | |
482 | break; | |
483 | case NFPROTO_IPV6: { | |
11251c17 | 484 | __be16 frag_off; |
c05e2094 | 485 | int ofs; |
11251c17 | 486 | |
4cc85f28 | 487 | nexthdr = ipv6_hdr(skb)->nexthdr; |
c05e2094 JS |
488 | ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr, |
489 | &frag_off); | |
490 | if (ofs < 0 || (frag_off & htons(~0x7)) != 0) { | |
11251c17 JS |
491 | pr_debug("proto header not found\n"); |
492 | return NF_ACCEPT; | |
493 | } | |
c05e2094 | 494 | protoff = ofs; |
11251c17 JS |
495 | break; |
496 | } | |
497 | default: | |
498 | WARN_ONCE(1, "helper invoked on non-IP family!"); | |
499 | return NF_DROP; | |
500 | } | |
501 | ||
4cc85f28 JR |
502 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0) |
503 | /* Linux 4.5 and older depend on skb_dst being set when recalculating | |
504 | * checksums after NAT helper has mangled TCP or UDP packet payload. | |
3d47fa46 JH |
505 | * skb_dst is cast to a rtable struct and the flags examined. |
506 | * Forcing these flags to have RTCF_LOCAL not set ensures checksum mod | |
507 | * is carried out in the same way as kernel versions > 4.5 | |
4cc85f28 | 508 | */ |
3d47fa46 JH |
509 | if (ct->status & IPS_NAT_MASK && skb->ip_summed != CHECKSUM_PARTIAL |
510 | && !skb_dst(skb)) { | |
511 | dst_set = true; | |
512 | skb_dst_set(skb, &rt.dst); | |
4cc85f28 JR |
513 | } |
514 | #endif | |
f8f97cdc JR |
515 | err = helper->help(skb, protoff, ct, ctinfo); |
516 | if (err != NF_ACCEPT) | |
517 | return err; | |
518 | ||
3d47fa46 JH |
519 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0) |
520 | if (dst_set) | |
521 | skb_dst_set(skb, NULL); | |
522 | #endif | |
523 | ||
f8f97cdc JR |
524 | /* Adjust seqs after helper. This is needed due to some helpers (e.g., |
525 | * FTP with NAT) adusting the TCP payload size when mangling IP | |
526 | * addresses and/or port numbers in the text-based control connection. | |
527 | */ | |
528 | if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) && | |
529 | !nf_ct_seq_adjust(skb, ct, ctinfo, protoff)) | |
530 | return NF_DROP; | |
531 | return NF_ACCEPT; | |
11251c17 JS |
532 | } |
533 | ||
c05e2094 JS |
534 | /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero |
535 | * value if 'skb' is freed. | |
536 | */ | |
a94ebc39 JS |
537 | static int handle_fragments(struct net *net, struct sw_flow_key *key, |
538 | u16 zone, struct sk_buff *skb) | |
539 | { | |
86c2eb45 | 540 | struct ovs_gso_cb ovs_cb = *OVS_GSO_CB(skb); |
2e602ea3 | 541 | int err; |
a94ebc39 | 542 | |
a94ebc39 JS |
543 | if (key->eth.type == htons(ETH_P_IP)) { |
544 | enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone; | |
a94ebc39 JS |
545 | |
546 | memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); | |
39c0ff22 | 547 | err = ip_defrag(net, skb, user); |
a94ebc39 JS |
548 | if (err) |
549 | return err; | |
550 | ||
86c2eb45 | 551 | ovs_cb.dp_cb.mru = IPCB(skb)->frag_max_size; |
a94ebc39 | 552 | #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) |
c05e2094 | 553 | } else if (key->eth.type == htons(ETH_P_IPV6)) { |
a94ebc39 | 554 | enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone; |
a94ebc39 JS |
555 | |
556 | memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm)); | |
2e602ea3 | 557 | err = nf_ct_frag6_gather(net, skb, user); |
6b4fe5a9 DDP |
558 | if (err) { |
559 | if (err != -EINPROGRESS) | |
560 | kfree_skb(skb); | |
2e602ea3 | 561 | return err; |
6b4fe5a9 | 562 | } |
a94ebc39 | 563 | |
2e602ea3 | 564 | key->ip.proto = ipv6_hdr(skb)->nexthdr; |
86c2eb45 | 565 | ovs_cb.dp_cb.mru = IP6CB(skb)->frag_max_size; |
a94ebc39 JS |
566 | #endif /* IP frag support */ |
567 | } else { | |
c05e2094 | 568 | kfree_skb(skb); |
a94ebc39 JS |
569 | return -EPFNOSUPPORT; |
570 | } | |
571 | ||
572 | key->ip.frag = OVS_FRAG_TYPE_NONE; | |
573 | skb_clear_hash(skb); | |
574 | skb->ignore_df = 1; | |
86c2eb45 | 575 | *OVS_GSO_CB(skb) = ovs_cb; |
a94ebc39 JS |
576 | |
577 | return 0; | |
578 | } | |
579 | ||
580 | static struct nf_conntrack_expect * | |
581 | ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone, | |
582 | u16 proto, const struct sk_buff *skb) | |
583 | { | |
584 | struct nf_conntrack_tuple tuple; | |
3618f466 | 585 | struct nf_conntrack_expect *exp; |
a94ebc39 | 586 | |
fa67f8e0 | 587 | if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple)) |
a94ebc39 | 588 | return NULL; |
3618f466 JR |
589 | |
590 | exp = __nf_ct_expect_find(net, zone, &tuple); | |
591 | if (exp) { | |
592 | struct nf_conntrack_tuple_hash *h; | |
593 | ||
594 | /* Delete existing conntrack entry, if it clashes with the | |
595 | * expectation. This can happen since conntrack ALGs do not | |
596 | * check for clashes between (new) expectations and existing | |
597 | * conntrack entries. nf_conntrack_in() will check the | |
598 | * expectations only if a conntrack entry can not be found, | |
599 | * which can lead to OVS finding the expectation (here) in the | |
600 | * init direction, but which will not be removed by the | |
601 | * nf_conntrack_in() call, if a matching conntrack entry is | |
602 | * found instead. In this case all init direction packets | |
603 | * would be reported as new related packets, while reply | |
604 | * direction packets would be reported as un-related | |
605 | * established packets. | |
606 | */ | |
607 | h = nf_conntrack_find_get(net, zone, &tuple); | |
608 | if (h) { | |
609 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); | |
610 | ||
611 | nf_ct_delete(ct, 0, 0); | |
612 | nf_conntrack_put(&ct->ct_general); | |
613 | } | |
614 | } | |
615 | ||
616 | return exp; | |
a94ebc39 JS |
617 | } |
618 | ||
3dd9e118 JR |
619 | /* This replicates logic from nf_conntrack_core.c that is not exported. */ |
620 | static enum ip_conntrack_info | |
621 | ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h) | |
622 | { | |
623 | const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); | |
624 | ||
625 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) | |
626 | return IP_CT_ESTABLISHED_REPLY; | |
627 | /* Once we've had two way comms, always ESTABLISHED. */ | |
628 | if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) | |
629 | return IP_CT_ESTABLISHED; | |
630 | if (test_bit(IPS_EXPECTED_BIT, &ct->status)) | |
631 | return IP_CT_RELATED; | |
632 | return IP_CT_NEW; | |
633 | } | |
634 | ||
635 | /* Find an existing connection which this packet belongs to without | |
636 | * re-attributing statistics or modifying the connection state. This allows an | |
e3c42eb8 | 637 | * skb->_nfct lost due to an upcall to be recovered during actions execution. |
3dd9e118 JR |
638 | * |
639 | * Must be called with rcu_read_lock. | |
640 | * | |
e3c42eb8 JR |
641 | * On success, populates skb->_nfct and returns the connection. Returns NULL |
642 | * if there is no existing entry. | |
3dd9e118 JR |
643 | */ |
644 | static struct nf_conn * | |
645 | ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone, | |
c0324e37 | 646 | u8 l3num, struct sk_buff *skb, bool natted) |
3dd9e118 | 647 | { |
b81164d9 FW |
648 | const struct nf_conntrack_l3proto *l3proto; |
649 | const struct nf_conntrack_l4proto *l4proto; | |
3dd9e118 JR |
650 | struct nf_conntrack_tuple tuple; |
651 | struct nf_conntrack_tuple_hash *h; | |
3dd9e118 JR |
652 | struct nf_conn *ct; |
653 | unsigned int dataoff; | |
654 | u8 protonum; | |
655 | ||
656 | l3proto = __nf_ct_l3proto_find(l3num); | |
3dd9e118 JR |
657 | if (l3proto->get_l4proto(skb, skb_network_offset(skb), &dataoff, |
658 | &protonum) <= 0) { | |
659 | pr_debug("ovs_ct_find_existing: Can't get protonum\n"); | |
660 | return NULL; | |
661 | } | |
662 | l4proto = __nf_ct_l4proto_find(l3num, protonum); | |
3dd9e118 JR |
663 | if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num, |
664 | protonum, net, &tuple, l3proto, l4proto)) { | |
665 | pr_debug("ovs_ct_find_existing: Can't get tuple\n"); | |
666 | return NULL; | |
667 | } | |
668 | ||
c0324e37 JR |
669 | /* Must invert the tuple if skb has been transformed by NAT. */ |
670 | if (natted) { | |
671 | struct nf_conntrack_tuple inverse; | |
672 | ||
673 | if (!nf_ct_invert_tuple(&inverse, &tuple, l3proto, l4proto)) { | |
674 | pr_debug("ovs_ct_find_existing: Inversion failed!\n"); | |
675 | return NULL; | |
676 | } | |
677 | tuple = inverse; | |
678 | } | |
679 | ||
3dd9e118 JR |
680 | /* look for tuple match */ |
681 | h = nf_conntrack_find_get(net, zone, &tuple); | |
682 | if (!h) | |
683 | return NULL; /* Not found. */ | |
684 | ||
685 | ct = nf_ct_tuplehash_to_ctrack(h); | |
686 | ||
c0324e37 JR |
687 | /* Inverted packet tuple matches the reverse direction conntrack tuple, |
688 | * select the other tuplehash to get the right 'ctinfo' bits for this | |
689 | * packet. | |
690 | */ | |
691 | if (natted) | |
692 | h = &ct->tuplehash[!h->tuple.dst.dir]; | |
693 | ||
dfa791b2 | 694 | nf_ct_set(skb, ct, ovs_ct_get_info(h)); |
3dd9e118 JR |
695 | return ct; |
696 | } | |
697 | ||
4e7b535d GR |
698 | static |
699 | struct nf_conn *ovs_ct_executed(struct net *net, | |
700 | const struct sw_flow_key *key, | |
701 | const struct ovs_conntrack_info *info, | |
702 | struct sk_buff *skb, | |
703 | bool *ct_executed) | |
704 | { | |
705 | struct nf_conn *ct = NULL; | |
706 | ||
707 | /* If no ct, check if we have evidence that an existing conntrack entry | |
708 | * might be found for this skb. This happens when we lose a skb->_nfct | |
709 | * due to an upcall, or if the direction is being forced. If the | |
710 | * connection was not confirmed, it is not cached and needs to be run | |
711 | * through conntrack again. | |
712 | */ | |
713 | *ct_executed = (key->ct_state & OVS_CS_F_TRACKED) && | |
714 | !(key->ct_state & OVS_CS_F_INVALID) && | |
715 | (key->ct_zone == info->zone.id); | |
716 | ||
717 | if (*ct_executed || (!key->ct_state && info->force)) { | |
718 | ct = ovs_ct_find_existing(net, &info->zone, info->family, skb, | |
719 | !!(key->ct_state & | |
720 | OVS_CS_F_NAT_MASK)); | |
721 | } | |
722 | ||
723 | return ct; | |
724 | } | |
725 | ||
e3c42eb8 | 726 | /* Determine whether skb->_nfct is equal to the result of conntrack lookup. */ |
3dd9e118 JR |
727 | static bool skb_nfct_cached(struct net *net, |
728 | const struct sw_flow_key *key, | |
729 | const struct ovs_conntrack_info *info, | |
730 | struct sk_buff *skb) | |
a94ebc39 JS |
731 | { |
732 | enum ip_conntrack_info ctinfo; | |
733 | struct nf_conn *ct; | |
4e7b535d | 734 | bool ct_executed = true; |
a94ebc39 JS |
735 | |
736 | ct = nf_ct_get(skb, &ctinfo); | |
737 | if (!ct) | |
4e7b535d GR |
738 | ct = ovs_ct_executed(net, key, info, skb, &ct_executed); |
739 | ||
740 | if (ct) | |
741 | nf_ct_get(skb, &ctinfo); | |
742 | else | |
a94ebc39 | 743 | return false; |
4e7b535d | 744 | |
a94ebc39 JS |
745 | if (!net_eq(net, read_pnet(&ct->ct_net))) |
746 | return false; | |
747 | if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct))) | |
748 | return false; | |
11251c17 JS |
749 | if (info->helper) { |
750 | struct nf_conn_help *help; | |
751 | ||
752 | help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER); | |
753 | if (help && rcu_access_pointer(help->helper) != info->helper) | |
754 | return false; | |
755 | } | |
b80e259f JR |
756 | /* Force conntrack entry direction to the current packet? */ |
757 | if (info->force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) { | |
758 | /* Delete the conntrack entry if confirmed, else just release | |
759 | * the reference. | |
760 | */ | |
761 | if (nf_ct_is_confirmed(ct)) | |
762 | nf_ct_delete(ct, 0, 0); | |
ceb0a8a9 JR |
763 | |
764 | nf_conntrack_put(&ct->ct_general); | |
b80e259f JR |
765 | nf_ct_set(skb, NULL, 0); |
766 | return false; | |
767 | } | |
a94ebc39 | 768 | |
4e7b535d | 769 | return ct_executed; |
a94ebc39 JS |
770 | } |
771 | ||
f8f97cdc JR |
772 | #ifdef CONFIG_NF_NAT_NEEDED |
773 | /* Modelled after nf_nat_ipv[46]_fn(). | |
774 | * range is only used for new, uninitialized NAT state. | |
775 | * Returns either NF_ACCEPT or NF_DROP. | |
776 | */ | |
777 | static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct, | |
778 | enum ip_conntrack_info ctinfo, | |
fdec3c17 | 779 | const struct nf_nat_range2 *range, |
f8f97cdc JR |
780 | enum nf_nat_manip_type maniptype) |
781 | { | |
782 | int hooknum, nh_off, err = NF_ACCEPT; | |
783 | ||
784 | nh_off = skb_network_offset(skb); | |
073c7b86 | 785 | skb_pull_rcsum(skb, nh_off); |
f8f97cdc JR |
786 | |
787 | /* See HOOK2MANIP(). */ | |
788 | if (maniptype == NF_NAT_MANIP_SRC) | |
789 | hooknum = NF_INET_LOCAL_IN; /* Source NAT */ | |
790 | else | |
791 | hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */ | |
792 | ||
793 | switch (ctinfo) { | |
794 | case IP_CT_RELATED: | |
795 | case IP_CT_RELATED_REPLY: | |
90b01477 AB |
796 | if (IS_ENABLED(CONFIG_NF_NAT_IPV4) && |
797 | skb->protocol == htons(ETH_P_IP) && | |
f8f97cdc JR |
798 | ip_hdr(skb)->protocol == IPPROTO_ICMP) { |
799 | if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo, | |
800 | hooknum)) | |
801 | err = NF_DROP; | |
802 | goto push; | |
90b01477 AB |
803 | } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) && |
804 | skb->protocol == htons(ETH_P_IPV6)) { | |
f8f97cdc JR |
805 | __be16 frag_off; |
806 | u8 nexthdr = ipv6_hdr(skb)->nexthdr; | |
807 | int hdrlen = ipv6_skip_exthdr(skb, | |
808 | sizeof(struct ipv6hdr), | |
809 | &nexthdr, &frag_off); | |
810 | ||
811 | if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) { | |
812 | if (!nf_nat_icmpv6_reply_translation(skb, ct, | |
813 | ctinfo, | |
814 | hooknum, | |
815 | hdrlen)) | |
816 | err = NF_DROP; | |
817 | goto push; | |
818 | } | |
f8f97cdc JR |
819 | } |
820 | /* Non-ICMP, fall thru to initialize if needed. */ | |
821 | case IP_CT_NEW: | |
822 | /* Seen it before? This can happen for loopback, retrans, | |
823 | * or local packets. | |
824 | */ | |
825 | if (!nf_nat_initialized(ct, maniptype)) { | |
826 | /* Initialize according to the NAT action. */ | |
827 | err = (range && range->flags & NF_NAT_RANGE_MAP_IPS) | |
828 | /* Action is set up to establish a new | |
829 | * mapping. | |
830 | */ | |
831 | ? nf_nat_setup_info(ct, range, maniptype) | |
832 | : nf_nat_alloc_null_binding(ct, hooknum); | |
833 | if (err != NF_ACCEPT) | |
834 | goto push; | |
835 | } | |
836 | break; | |
837 | ||
838 | case IP_CT_ESTABLISHED: | |
839 | case IP_CT_ESTABLISHED_REPLY: | |
840 | break; | |
841 | ||
842 | default: | |
843 | err = NF_DROP; | |
844 | goto push; | |
845 | } | |
846 | ||
847 | err = nf_nat_packet(ct, ctinfo, hooknum, skb); | |
848 | push: | |
849 | skb_push(skb, nh_off); | |
073c7b86 | 850 | skb_postpush_rcsum(skb, skb->data, nh_off); |
f8f97cdc JR |
851 | |
852 | return err; | |
853 | } | |
854 | ||
855 | static void ovs_nat_update_key(struct sw_flow_key *key, | |
856 | const struct sk_buff *skb, | |
857 | enum nf_nat_manip_type maniptype) | |
858 | { | |
859 | if (maniptype == NF_NAT_MANIP_SRC) { | |
860 | __be16 src; | |
861 | ||
1963a885 | 862 | key->ct_state |= OVS_CS_F_SRC_NAT; |
f8f97cdc JR |
863 | if (key->eth.type == htons(ETH_P_IP)) |
864 | key->ipv4.addr.src = ip_hdr(skb)->saddr; | |
865 | else if (key->eth.type == htons(ETH_P_IPV6)) | |
866 | memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr, | |
867 | sizeof(key->ipv6.addr.src)); | |
868 | else | |
869 | return; | |
870 | ||
871 | if (key->ip.proto == IPPROTO_UDP) | |
872 | src = udp_hdr(skb)->source; | |
873 | else if (key->ip.proto == IPPROTO_TCP) | |
874 | src = tcp_hdr(skb)->source; | |
875 | else if (key->ip.proto == IPPROTO_SCTP) | |
876 | src = sctp_hdr(skb)->source; | |
877 | else | |
878 | return; | |
879 | ||
880 | key->tp.src = src; | |
881 | } else { | |
882 | __be16 dst; | |
883 | ||
1963a885 | 884 | key->ct_state |= OVS_CS_F_DST_NAT; |
f8f97cdc JR |
885 | if (key->eth.type == htons(ETH_P_IP)) |
886 | key->ipv4.addr.dst = ip_hdr(skb)->daddr; | |
887 | else if (key->eth.type == htons(ETH_P_IPV6)) | |
888 | memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr, | |
889 | sizeof(key->ipv6.addr.dst)); | |
890 | else | |
891 | return; | |
892 | ||
893 | if (key->ip.proto == IPPROTO_UDP) | |
894 | dst = udp_hdr(skb)->dest; | |
895 | else if (key->ip.proto == IPPROTO_TCP) | |
896 | dst = tcp_hdr(skb)->dest; | |
897 | else if (key->ip.proto == IPPROTO_SCTP) | |
898 | dst = sctp_hdr(skb)->dest; | |
899 | else | |
900 | return; | |
901 | ||
902 | key->tp.dst = dst; | |
903 | } | |
904 | } | |
905 | ||
906 | /* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */ | |
907 | static int ovs_ct_nat(struct net *net, struct sw_flow_key *key, | |
908 | const struct ovs_conntrack_info *info, | |
909 | struct sk_buff *skb, struct nf_conn *ct, | |
910 | enum ip_conntrack_info ctinfo) | |
911 | { | |
912 | enum nf_nat_manip_type maniptype; | |
913 | int err; | |
914 | ||
56ec5593 | 915 | #ifdef HAVE_NF_CT_IS_UNTRACKED |
f8f97cdc JR |
916 | if (nf_ct_is_untracked(ct)) { |
917 | /* A NAT action may only be performed on tracked packets. */ | |
918 | return NF_ACCEPT; | |
919 | } | |
56ec5593 | 920 | #endif /* HAVE_NF_CT_IS_UNTRACKED */ |
f8f97cdc JR |
921 | |
922 | /* Add NAT extension if not confirmed yet. */ | |
923 | if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct)) | |
924 | return NF_ACCEPT; /* Can't NAT. */ | |
925 | ||
926 | /* Determine NAT type. | |
927 | * Check if the NAT type can be deduced from the tracked connection. | |
d2e8b514 JR |
928 | * Make sure new expected connections (IP_CT_RELATED) are NATted only |
929 | * when committing. | |
f8f97cdc JR |
930 | */ |
931 | if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW && | |
932 | ct->status & IPS_NAT_MASK && | |
d2e8b514 | 933 | (ctinfo != IP_CT_RELATED || info->commit)) { |
f8f97cdc JR |
934 | /* NAT an established or related connection like before. */ |
935 | if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) | |
936 | /* This is the REPLY direction for a connection | |
937 | * for which NAT was applied in the forward | |
938 | * direction. Do the reverse NAT. | |
939 | */ | |
940 | maniptype = ct->status & IPS_SRC_NAT | |
941 | ? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC; | |
942 | else | |
943 | maniptype = ct->status & IPS_SRC_NAT | |
944 | ? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST; | |
945 | } else if (info->nat & OVS_CT_SRC_NAT) { | |
946 | maniptype = NF_NAT_MANIP_SRC; | |
947 | } else if (info->nat & OVS_CT_DST_NAT) { | |
948 | maniptype = NF_NAT_MANIP_DST; | |
949 | } else { | |
950 | return NF_ACCEPT; /* Connection is not NATed. */ | |
951 | } | |
952 | err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype); | |
953 | ||
954 | /* Mark NAT done if successful and update the flow key. */ | |
955 | if (err == NF_ACCEPT) | |
956 | ovs_nat_update_key(key, skb, maniptype); | |
957 | ||
958 | return err; | |
959 | } | |
960 | #else /* !CONFIG_NF_NAT_NEEDED */ | |
961 | static int ovs_ct_nat(struct net *net, struct sw_flow_key *key, | |
962 | const struct ovs_conntrack_info *info, | |
963 | struct sk_buff *skb, struct nf_conn *ct, | |
964 | enum ip_conntrack_info ctinfo) | |
965 | { | |
966 | return NF_ACCEPT; | |
967 | } | |
968 | #endif | |
969 | ||
b0f251cd | 970 | /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if |
a04a5794 JR |
971 | * not done already. Update key with new CT state after passing the packet |
972 | * through conntrack. | |
e3c42eb8 | 973 | * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be |
b0f251cd JR |
974 | * set to NULL and 0 will be returned. |
975 | */ | |
c05e2094 | 976 | static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key, |
a94ebc39 JS |
977 | const struct ovs_conntrack_info *info, |
978 | struct sk_buff *skb) | |
979 | { | |
980 | /* If we are recirculating packets to match on conntrack fields and | |
981 | * committing with a separate conntrack action, then we don't need to | |
982 | * actually run the packet through conntrack twice unless it's for a | |
983 | * different zone. | |
984 | */ | |
b21d237e JR |
985 | bool cached = skb_nfct_cached(net, key, info, skb); |
986 | enum ip_conntrack_info ctinfo; | |
987 | struct nf_conn *ct; | |
988 | ||
989 | if (!cached) { | |
4fdec898 FW |
990 | struct nf_hook_state state = { |
991 | .hook = NF_INET_PRE_ROUTING, | |
992 | .pf = info->family, | |
993 | .net = net, | |
994 | }; | |
a94ebc39 | 995 | struct nf_conn *tmpl = info->ct; |
9bf67b92 | 996 | int err; |
a94ebc39 JS |
997 | |
998 | /* Associate skb with specified zone. */ | |
999 | if (tmpl) { | |
d3c313c1 FW |
1000 | if (skb_nfct(skb)) |
1001 | nf_conntrack_put(skb_nfct(skb)); | |
a94ebc39 | 1002 | nf_conntrack_get(&tmpl->ct_general); |
dfa791b2 | 1003 | nf_ct_set(skb, tmpl, IP_CT_NEW); |
a94ebc39 JS |
1004 | } |
1005 | ||
4fdec898 | 1006 | err = nf_conntrack_in(skb, &state); |
9bf67b92 | 1007 | if (err != NF_ACCEPT) |
a94ebc39 | 1008 | return -ENOENT; |
11251c17 | 1009 | |
f8f97cdc JR |
1010 | /* Clear CT state NAT flags to mark that we have not yet done |
1011 | * NAT after the nf_conntrack_in() call. We can actually clear | |
1012 | * the whole state, as it will be re-initialized below. | |
1013 | */ | |
1963a885 | 1014 | key->ct_state = 0; |
f8f97cdc JR |
1015 | |
1016 | /* Update the key, but keep the NAT flags. */ | |
1017 | ovs_ct_update_key(skb, info, key, true, true); | |
b21d237e | 1018 | } |
a04a5794 | 1019 | |
b21d237e | 1020 | ct = nf_ct_get(skb, &ctinfo); |
f8f97cdc JR |
1021 | if (ct) { |
1022 | /* Packets starting a new connection must be NATted before the | |
1023 | * helper, so that the helper knows about the NAT. We enforce | |
1024 | * this by delaying both NAT and helper calls for unconfirmed | |
1025 | * connections until the committing CT action. For later | |
1026 | * packets NAT and Helper may be called in either order. | |
1027 | * | |
1028 | * NAT will be done only if the CT action has NAT, and only | |
1029 | * once per packet (per zone), as guarded by the NAT bits in | |
1963a885 | 1030 | * the key->ct_state. |
f8f97cdc | 1031 | */ |
1963a885 | 1032 | if (info->nat && !(key->ct_state & OVS_CS_F_NAT_MASK) && |
f8f97cdc JR |
1033 | (nf_ct_is_confirmed(ct) || info->commit) && |
1034 | ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) { | |
1035 | return -EINVAL; | |
1036 | } | |
1037 | ||
b87a5aac JS |
1038 | /* Userspace may decide to perform a ct lookup without a helper |
1039 | * specified followed by a (recirculate and) commit with one. | |
1040 | * Therefore, for unconfirmed connections which we will commit, | |
1041 | * we need to attach the helper here. | |
1042 | */ | |
1043 | if (!nf_ct_is_confirmed(ct) && info->commit && | |
1044 | info->helper && !nfct_help(ct)) { | |
1045 | int err = __nf_ct_try_assign_helper(ct, info->ct, | |
1046 | GFP_ATOMIC); | |
1047 | if (err) | |
1048 | return err; | |
1049 | } | |
1050 | ||
f8f97cdc JR |
1051 | /* Call the helper only if: |
1052 | * - nf_conntrack_in() was executed above ("!cached") for a | |
1053 | * confirmed connection, or | |
1054 | * - When committing an unconfirmed connection. | |
1055 | */ | |
1056 | if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) && | |
1057 | ovs_ct_helper(skb, info->family) != NF_ACCEPT) { | |
1058 | return -EINVAL; | |
1059 | } | |
a94ebc39 JS |
1060 | } |
1061 | ||
1062 | return 0; | |
1063 | } | |
1064 | ||
1065 | /* Lookup connection and read fields into key. */ | |
1066 | static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key, | |
1067 | const struct ovs_conntrack_info *info, | |
1068 | struct sk_buff *skb) | |
1069 | { | |
1070 | struct nf_conntrack_expect *exp; | |
1071 | ||
b0f251cd JR |
1072 | /* If we pass an expected packet through nf_conntrack_in() the |
1073 | * expectation is typically removed, but the packet could still be | |
1074 | * lost in upcall processing. To prevent this from happening we | |
1075 | * perform an explicit expectation lookup. Expected connections are | |
1076 | * always new, and will be passed through conntrack only when they are | |
1077 | * committed, as it is OK to remove the expectation at that time. | |
1078 | */ | |
a94ebc39 JS |
1079 | exp = ovs_ct_expect_find(net, &info->zone, info->family, skb); |
1080 | if (exp) { | |
1081 | u8 state; | |
1082 | ||
f8f97cdc JR |
1083 | /* NOTE: New connections are NATted and Helped only when |
1084 | * committed, so we are not calling into NAT here. | |
1085 | */ | |
a94ebc39 | 1086 | state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED; |
372ce973 | 1087 | __ovs_ct_update_key(key, state, &info->zone, exp->master); |
f325530e PS |
1088 | } else { |
1089 | struct nf_conn *ct; | |
1090 | int err; | |
1091 | ||
1092 | err = __ovs_ct_lookup(net, key, info, skb); | |
1093 | if (err) | |
1094 | return err; | |
1095 | ||
d3c313c1 | 1096 | ct = (struct nf_conn *)skb_nfct(skb); |
f325530e PS |
1097 | if (ct) |
1098 | nf_ct_deliver_cached_events(ct); | |
1099 | } | |
a94ebc39 JS |
1100 | |
1101 | return 0; | |
1102 | } | |
1103 | ||
c05e2094 | 1104 | static bool labels_nonzero(const struct ovs_key_ct_labels *labels) |
038e34ab JS |
1105 | { |
1106 | size_t i; | |
1107 | ||
83495bd9 JR |
1108 | for (i = 0; i < OVS_CT_LABELS_LEN_32; i++) |
1109 | if (labels->ct_labels_32[i]) | |
038e34ab JS |
1110 | return true; |
1111 | ||
1112 | return false; | |
1113 | } | |
1114 | ||
cb2a5486 YHW |
1115 | #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) |
1116 | static struct hlist_head *ct_limit_hash_bucket( | |
1117 | const struct ovs_ct_limit_info *info, u16 zone) | |
1118 | { | |
1119 | return &info->limits[zone & (CT_LIMIT_HASH_BUCKETS - 1)]; | |
1120 | } | |
1121 | ||
1122 | /* Call with ovs_mutex */ | |
1123 | static void ct_limit_set(const struct ovs_ct_limit_info *info, | |
1124 | struct ovs_ct_limit *new_ct_limit) | |
1125 | { | |
1126 | struct ovs_ct_limit *ct_limit; | |
1127 | struct hlist_head *head; | |
1128 | ||
1129 | head = ct_limit_hash_bucket(info, new_ct_limit->zone); | |
1130 | hlist_for_each_entry_rcu(ct_limit, head, hlist_node) { | |
1131 | if (ct_limit->zone == new_ct_limit->zone) { | |
1132 | hlist_replace_rcu(&ct_limit->hlist_node, | |
1133 | &new_ct_limit->hlist_node); | |
1134 | kfree_rcu(ct_limit, rcu); | |
1135 | return; | |
1136 | } | |
1137 | } | |
1138 | ||
1139 | hlist_add_head_rcu(&new_ct_limit->hlist_node, head); | |
1140 | } | |
1141 | ||
1142 | /* Call with ovs_mutex */ | |
1143 | static void ct_limit_del(const struct ovs_ct_limit_info *info, u16 zone) | |
1144 | { | |
1145 | struct ovs_ct_limit *ct_limit; | |
1146 | struct hlist_head *head; | |
1147 | struct hlist_node *n; | |
1148 | ||
1149 | head = ct_limit_hash_bucket(info, zone); | |
1150 | hlist_for_each_entry_safe(ct_limit, n, head, hlist_node) { | |
1151 | if (ct_limit->zone == zone) { | |
1152 | hlist_del_rcu(&ct_limit->hlist_node); | |
1153 | kfree_rcu(ct_limit, rcu); | |
1154 | return; | |
1155 | } | |
1156 | } | |
1157 | } | |
1158 | ||
1159 | /* Call with RCU read lock */ | |
1160 | static u32 ct_limit_get(const struct ovs_ct_limit_info *info, u16 zone) | |
1161 | { | |
1162 | struct ovs_ct_limit *ct_limit; | |
1163 | struct hlist_head *head; | |
1164 | ||
1165 | head = ct_limit_hash_bucket(info, zone); | |
1166 | hlist_for_each_entry_rcu(ct_limit, head, hlist_node) { | |
1167 | if (ct_limit->zone == zone) | |
1168 | return ct_limit->limit; | |
1169 | } | |
1170 | ||
1171 | return info->default_limit; | |
1172 | } | |
1173 | ||
1174 | static int ovs_ct_check_limit(struct net *net, | |
1175 | const struct ovs_conntrack_info *info, | |
1176 | const struct nf_conntrack_tuple *tuple) | |
1177 | { | |
1178 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); | |
1179 | const struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info; | |
1180 | u32 per_zone_limit, connections; | |
1181 | u32 conncount_key; | |
1182 | ||
1183 | conncount_key = info->zone.id; | |
1184 | ||
1185 | per_zone_limit = ct_limit_get(ct_limit_info, info->zone.id); | |
1186 | if (per_zone_limit == OVS_CT_LIMIT_UNLIMITED) | |
1187 | return 0; | |
1188 | ||
1189 | connections = nf_conncount_count(net, ct_limit_info->data, | |
1190 | &conncount_key, tuple, &info->zone); | |
1191 | if (connections > per_zone_limit) | |
1192 | return -ENOMEM; | |
1193 | ||
1194 | return 0; | |
1195 | } | |
1196 | #endif | |
1197 | ||
39a6542b JR |
1198 | /* Lookup connection and confirm if unconfirmed. */ |
1199 | static int ovs_ct_commit(struct net *net, struct sw_flow_key *key, | |
1200 | const struct ovs_conntrack_info *info, | |
1201 | struct sk_buff *skb) | |
1202 | { | |
efd40994 JR |
1203 | enum ip_conntrack_info ctinfo; |
1204 | struct nf_conn *ct; | |
39a6542b JR |
1205 | int err; |
1206 | ||
1207 | err = __ovs_ct_lookup(net, key, info, skb); | |
1208 | if (err) | |
1209 | return err; | |
1210 | ||
efd40994 JR |
1211 | /* The connection could be invalid, in which case this is a no-op.*/ |
1212 | ct = nf_ct_get(skb, &ctinfo); | |
1213 | if (!ct) | |
1214 | return 0; | |
1215 | ||
cb2a5486 YHW |
1216 | #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) |
1217 | if (static_branch_unlikely(&ovs_ct_limit_enabled)) { | |
1218 | if (!nf_ct_is_confirmed(ct)) { | |
1219 | err = ovs_ct_check_limit(net, info, | |
1220 | &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); | |
1221 | if (err) { | |
1222 | net_warn_ratelimited("openvswitch: zone: %u " | |
56913238 | 1223 | "exceeds conntrack limit\n", |
cb2a5486 YHW |
1224 | info->zone.id); |
1225 | return err; | |
1226 | } | |
1227 | } | |
1228 | } | |
1229 | #endif | |
1230 | ||
8e83854c JR |
1231 | /* Set the conntrack event mask if given. NEW and DELETE events have |
1232 | * their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener | |
1233 | * typically would receive many kinds of updates. Setting the event | |
1234 | * mask allows those events to be filtered. The set event mask will | |
1235 | * remain in effect for the lifetime of the connection unless changed | |
1236 | * by a further CT action with both the commit flag and the eventmask | |
1237 | * option. */ | |
1238 | if (info->have_eventmask) { | |
1239 | struct nf_conntrack_ecache *cache = nf_ct_ecache_find(ct); | |
1240 | ||
1241 | if (cache) | |
1242 | cache->ctmask = info->eventmask; | |
1243 | } | |
1244 | ||
39a6542b JR |
1245 | /* Apply changes before confirming the connection so that the initial |
1246 | * conntrack NEW netlink event carries the values given in the CT | |
1247 | * action. | |
1248 | */ | |
1249 | if (info->mark.mask) { | |
efd40994 | 1250 | err = ovs_ct_set_mark(ct, key, info->mark.value, |
39a6542b JR |
1251 | info->mark.mask); |
1252 | if (err) | |
1253 | return err; | |
1254 | } | |
7162cae4 JR |
1255 | if (!nf_ct_is_confirmed(ct)) { |
1256 | err = ovs_ct_init_labels(ct, key, &info->labels.value, | |
1257 | &info->labels.mask); | |
1258 | if (err) | |
1259 | return err; | |
1260 | } else if (labels_nonzero(&info->labels.mask)) { | |
1261 | err = ovs_ct_set_labels(ct, key, &info->labels.value, | |
1262 | &info->labels.mask); | |
39a6542b JR |
1263 | if (err) |
1264 | return err; | |
1265 | } | |
1266 | /* This will take care of sending queued events even if the connection | |
1267 | * is already confirmed. | |
1268 | */ | |
1269 | if (nf_conntrack_confirm(skb) != NF_ACCEPT) | |
1270 | return -EINVAL; | |
1271 | ||
1272 | return 0; | |
1273 | } | |
1274 | ||
c21adacd ES |
1275 | /* Trim the skb to the length specified by the IP/IPv6 header, |
1276 | * removing any trailing lower-layer padding. This prepares the skb | |
1277 | * for higher-layer processing that assumes skb->len excludes padding | |
1278 | * (such as nf_ip_checksum). The caller needs to pull the skb to the | |
1279 | * network header, and ensure ip_hdr/ipv6_hdr points to valid data. | |
1280 | */ | |
1281 | static int ovs_skb_network_trim(struct sk_buff *skb) | |
1282 | { | |
1283 | unsigned int len; | |
1284 | int err; | |
1285 | ||
1286 | switch (skb->protocol) { | |
1287 | case htons(ETH_P_IP): | |
1288 | len = ntohs(ip_hdr(skb)->tot_len); | |
1289 | break; | |
1290 | case htons(ETH_P_IPV6): | |
1291 | len = sizeof(struct ipv6hdr) | |
1292 | + ntohs(ipv6_hdr(skb)->payload_len); | |
1293 | break; | |
1294 | default: | |
1295 | len = skb->len; | |
1296 | } | |
1297 | ||
1298 | err = pskb_trim_rcsum(skb, len); | |
1299 | if (err) | |
1300 | kfree_skb(skb); | |
1301 | ||
1302 | return err; | |
1303 | } | |
1304 | ||
c05e2094 JS |
1305 | /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero |
1306 | * value if 'skb' is freed. | |
1307 | */ | |
a94ebc39 JS |
1308 | int ovs_ct_execute(struct net *net, struct sk_buff *skb, |
1309 | struct sw_flow_key *key, | |
1310 | const struct ovs_conntrack_info *info) | |
1311 | { | |
1312 | int nh_ofs; | |
1313 | int err; | |
1314 | ||
1315 | /* The conntrack module expects to be working at L3. */ | |
1316 | nh_ofs = skb_network_offset(skb); | |
073c7b86 | 1317 | skb_pull_rcsum(skb, nh_ofs); |
a94ebc39 | 1318 | |
c21adacd ES |
1319 | err = ovs_skb_network_trim(skb); |
1320 | if (err) | |
1321 | return err; | |
1322 | ||
a94ebc39 JS |
1323 | if (key->ip.frag != OVS_FRAG_TYPE_NONE) { |
1324 | err = handle_fragments(net, key, info->zone.id, skb); | |
1325 | if (err) | |
1326 | return err; | |
1327 | } | |
1328 | ||
c05e2094 | 1329 | if (info->commit) |
39a6542b | 1330 | err = ovs_ct_commit(net, key, info, skb); |
a94ebc39 JS |
1331 | else |
1332 | err = ovs_ct_lookup(net, key, info, skb); | |
1333 | ||
1334 | skb_push(skb, nh_ofs); | |
073c7b86 | 1335 | skb_postpush_rcsum(skb, skb->data, nh_ofs); |
c05e2094 JS |
1336 | if (err) |
1337 | kfree_skb(skb); | |
a94ebc39 JS |
1338 | return err; |
1339 | } | |
1340 | ||
0cdfdddd EG |
1341 | int ovs_ct_clear(struct sk_buff *skb, struct sw_flow_key *key) |
1342 | { | |
1343 | if (skb_nfct(skb)) { | |
1344 | nf_conntrack_put(skb_nfct(skb)); | |
1345 | #ifdef HAVE_IP_CT_UNTRACKED | |
1346 | nf_ct_set(skb, NULL, IP_CT_UNTRACKED); | |
1347 | #else | |
1348 | nf_ct_set(skb, NULL, 0); | |
1349 | #endif | |
1350 | ovs_ct_fill_key(skb, key); | |
1351 | } | |
1352 | ||
1353 | return 0; | |
1354 | } | |
1355 | ||
11251c17 JS |
1356 | static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name, |
1357 | const struct sw_flow_key *key, bool log) | |
1358 | { | |
1359 | struct nf_conntrack_helper *helper; | |
1360 | struct nf_conn_help *help; | |
1361 | ||
1362 | helper = nf_conntrack_helper_try_module_get(name, info->family, | |
1363 | key->ip.proto); | |
1364 | if (!helper) { | |
1365 | OVS_NLERR(log, "Unknown helper \"%s\"", name); | |
1366 | return -EINVAL; | |
1367 | } | |
1368 | ||
1369 | help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL); | |
1370 | if (!help) { | |
ac8e3c6d | 1371 | nf_conntrack_helper_put(helper); |
11251c17 JS |
1372 | return -ENOMEM; |
1373 | } | |
1374 | ||
1375 | rcu_assign_pointer(help->helper, helper); | |
1376 | info->helper = helper; | |
15d536e7 FL |
1377 | |
1378 | if (info->nat) | |
1379 | request_module("ip_nat_%s", name); | |
1380 | ||
11251c17 JS |
1381 | return 0; |
1382 | } | |
1383 | ||
f8f97cdc JR |
1384 | #ifdef CONFIG_NF_NAT_NEEDED |
1385 | static int parse_nat(const struct nlattr *attr, | |
1386 | struct ovs_conntrack_info *info, bool log) | |
1387 | { | |
1388 | struct nlattr *a; | |
1389 | int rem; | |
1390 | bool have_ip_max = false; | |
1391 | bool have_proto_max = false; | |
1392 | bool ip_vers = (info->family == NFPROTO_IPV6); | |
1393 | ||
1394 | nla_for_each_nested(a, attr, rem) { | |
1395 | static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = { | |
1396 | [OVS_NAT_ATTR_SRC] = {0, 0}, | |
1397 | [OVS_NAT_ATTR_DST] = {0, 0}, | |
1398 | [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr), | |
1399 | sizeof(struct in6_addr)}, | |
1400 | [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr), | |
1401 | sizeof(struct in6_addr)}, | |
1402 | [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)}, | |
1403 | [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)}, | |
1404 | [OVS_NAT_ATTR_PERSISTENT] = {0, 0}, | |
1405 | [OVS_NAT_ATTR_PROTO_HASH] = {0, 0}, | |
1406 | [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0}, | |
1407 | }; | |
1408 | int type = nla_type(a); | |
1409 | ||
1410 | if (type > OVS_NAT_ATTR_MAX) { | |
72ef85ed | 1411 | OVS_NLERR(log, "Unknown NAT attribute (type=%d, max=%d)", |
f8f97cdc JR |
1412 | type, OVS_NAT_ATTR_MAX); |
1413 | return -EINVAL; | |
1414 | } | |
1415 | ||
1416 | if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) { | |
72ef85ed | 1417 | OVS_NLERR(log, "NAT attribute type %d has unexpected length (%d != %d)", |
f8f97cdc JR |
1418 | type, nla_len(a), |
1419 | ovs_nat_attr_lens[type][ip_vers]); | |
1420 | return -EINVAL; | |
1421 | } | |
1422 | ||
1423 | switch (type) { | |
1424 | case OVS_NAT_ATTR_SRC: | |
1425 | case OVS_NAT_ATTR_DST: | |
1426 | if (info->nat) { | |
72ef85ed | 1427 | OVS_NLERR(log, "Only one type of NAT may be specified"); |
f8f97cdc JR |
1428 | return -ERANGE; |
1429 | } | |
1430 | info->nat |= OVS_CT_NAT; | |
1431 | info->nat |= ((type == OVS_NAT_ATTR_SRC) | |
1432 | ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT); | |
1433 | break; | |
1434 | ||
1435 | case OVS_NAT_ATTR_IP_MIN: | |
70e71d27 HY |
1436 | nla_memcpy(&info->range.min_addr, a, |
1437 | sizeof(info->range.min_addr)); | |
f8f97cdc JR |
1438 | info->range.flags |= NF_NAT_RANGE_MAP_IPS; |
1439 | break; | |
1440 | ||
1441 | case OVS_NAT_ATTR_IP_MAX: | |
1442 | have_ip_max = true; | |
1443 | nla_memcpy(&info->range.max_addr, a, | |
1444 | sizeof(info->range.max_addr)); | |
1445 | info->range.flags |= NF_NAT_RANGE_MAP_IPS; | |
1446 | break; | |
1447 | ||
1448 | case OVS_NAT_ATTR_PROTO_MIN: | |
1449 | info->range.min_proto.all = htons(nla_get_u16(a)); | |
1450 | info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED; | |
1451 | break; | |
1452 | ||
1453 | case OVS_NAT_ATTR_PROTO_MAX: | |
1454 | have_proto_max = true; | |
1455 | info->range.max_proto.all = htons(nla_get_u16(a)); | |
1456 | info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED; | |
1457 | break; | |
1458 | ||
1459 | case OVS_NAT_ATTR_PERSISTENT: | |
1460 | info->range.flags |= NF_NAT_RANGE_PERSISTENT; | |
1461 | break; | |
1462 | ||
1463 | case OVS_NAT_ATTR_PROTO_HASH: | |
1464 | info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM; | |
1465 | break; | |
1466 | ||
1467 | case OVS_NAT_ATTR_PROTO_RANDOM: | |
9f1de150 | 1468 | #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY |
f8f97cdc | 1469 | info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY; |
9f1de150 JR |
1470 | #else |
1471 | info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM; | |
1472 | info->random_fully_compat = true; | |
1473 | #endif | |
f8f97cdc JR |
1474 | break; |
1475 | ||
1476 | default: | |
72ef85ed | 1477 | OVS_NLERR(log, "Unknown nat attribute (%d)", type); |
f8f97cdc JR |
1478 | return -EINVAL; |
1479 | } | |
1480 | } | |
1481 | ||
1482 | if (rem > 0) { | |
72ef85ed | 1483 | OVS_NLERR(log, "NAT attribute has %d unknown bytes", rem); |
f8f97cdc JR |
1484 | return -EINVAL; |
1485 | } | |
1486 | if (!info->nat) { | |
1487 | /* Do not allow flags if no type is given. */ | |
1488 | if (info->range.flags) { | |
1489 | OVS_NLERR(log, | |
e7c57cc3 | 1490 | "NAT flags may be given only when NAT range (SRC or DST) is also specified." |
f8f97cdc JR |
1491 | ); |
1492 | return -EINVAL; | |
1493 | } | |
1494 | info->nat = OVS_CT_NAT; /* NAT existing connections. */ | |
1495 | } else if (!info->commit) { | |
1496 | OVS_NLERR(log, | |
e7c57cc3 | 1497 | "NAT attributes may be specified only when CT COMMIT flag is also specified." |
f8f97cdc JR |
1498 | ); |
1499 | return -EINVAL; | |
1500 | } | |
1501 | /* Allow missing IP_MAX. */ | |
1502 | if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) { | |
1503 | memcpy(&info->range.max_addr, &info->range.min_addr, | |
1504 | sizeof(info->range.max_addr)); | |
1505 | } | |
1506 | /* Allow missing PROTO_MAX. */ | |
1507 | if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED && | |
1508 | !have_proto_max) { | |
1509 | info->range.max_proto.all = info->range.min_proto.all; | |
1510 | } | |
1511 | return 0; | |
1512 | } | |
1513 | #endif | |
1514 | ||
a94ebc39 | 1515 | static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = { |
c05e2094 | 1516 | [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 }, |
b80e259f | 1517 | [OVS_CT_ATTR_FORCE_COMMIT] = { .minlen = 0, .maxlen = 0 }, |
a94ebc39 JS |
1518 | [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16), |
1519 | .maxlen = sizeof(u16) }, | |
372ce973 JS |
1520 | [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark), |
1521 | .maxlen = sizeof(struct md_mark) }, | |
c05e2094 JS |
1522 | [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels), |
1523 | .maxlen = sizeof(struct md_labels) }, | |
11251c17 | 1524 | [OVS_CT_ATTR_HELPER] = { .minlen = 1, |
f8f97cdc JR |
1525 | .maxlen = NF_CT_HELPER_NAME_LEN }, |
1526 | #ifdef CONFIG_NF_NAT_NEEDED | |
1527 | /* NAT length is checked when parsing the nested attributes. */ | |
1528 | [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX }, | |
1529 | #endif | |
8e83854c JR |
1530 | [OVS_CT_ATTR_EVENTMASK] = { .minlen = sizeof(u32), |
1531 | .maxlen = sizeof(u32) }, | |
a94ebc39 JS |
1532 | }; |
1533 | ||
1534 | static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info, | |
11251c17 | 1535 | const char **helper, bool log) |
a94ebc39 JS |
1536 | { |
1537 | struct nlattr *a; | |
1538 | int rem; | |
1539 | ||
1540 | nla_for_each_nested(a, attr, rem) { | |
1541 | int type = nla_type(a); | |
fb296f42 GR |
1542 | int maxlen; |
1543 | int minlen; | |
a94ebc39 JS |
1544 | |
1545 | if (type > OVS_CT_ATTR_MAX) { | |
1546 | OVS_NLERR(log, | |
1547 | "Unknown conntrack attr (type=%d, max=%d)", | |
1548 | type, OVS_CT_ATTR_MAX); | |
1549 | return -EINVAL; | |
1550 | } | |
fb296f42 GR |
1551 | |
1552 | maxlen = ovs_ct_attr_lens[type].maxlen; | |
1553 | minlen = ovs_ct_attr_lens[type].minlen; | |
a94ebc39 JS |
1554 | if (nla_len(a) < minlen || nla_len(a) > maxlen) { |
1555 | OVS_NLERR(log, | |
1556 | "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)", | |
1557 | type, nla_len(a), maxlen); | |
1558 | return -EINVAL; | |
1559 | } | |
1560 | ||
1561 | switch (type) { | |
b80e259f JR |
1562 | case OVS_CT_ATTR_FORCE_COMMIT: |
1563 | info->force = true; | |
1564 | /* fall through. */ | |
c05e2094 JS |
1565 | case OVS_CT_ATTR_COMMIT: |
1566 | info->commit = true; | |
a94ebc39 JS |
1567 | break; |
1568 | #ifdef CONFIG_NF_CONNTRACK_ZONES | |
1569 | case OVS_CT_ATTR_ZONE: | |
1570 | info->zone.id = nla_get_u16(a); | |
1571 | break; | |
372ce973 JS |
1572 | #endif |
1573 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
1574 | case OVS_CT_ATTR_MARK: { | |
1575 | struct md_mark *mark = nla_data(a); | |
1576 | ||
c05e2094 JS |
1577 | if (!mark->mask) { |
1578 | OVS_NLERR(log, "ct_mark mask cannot be 0"); | |
1579 | return -EINVAL; | |
1580 | } | |
372ce973 JS |
1581 | info->mark = *mark; |
1582 | break; | |
1583 | } | |
038e34ab JS |
1584 | #endif |
1585 | #ifdef CONFIG_NF_CONNTRACK_LABELS | |
c05e2094 JS |
1586 | case OVS_CT_ATTR_LABELS: { |
1587 | struct md_labels *labels = nla_data(a); | |
038e34ab | 1588 | |
c05e2094 JS |
1589 | if (!labels_nonzero(&labels->mask)) { |
1590 | OVS_NLERR(log, "ct_labels mask cannot be 0"); | |
1591 | return -EINVAL; | |
1592 | } | |
1593 | info->labels = *labels; | |
038e34ab JS |
1594 | break; |
1595 | } | |
a94ebc39 | 1596 | #endif |
11251c17 JS |
1597 | case OVS_CT_ATTR_HELPER: |
1598 | *helper = nla_data(a); | |
1599 | if (!memchr(*helper, '\0', nla_len(a))) { | |
1600 | OVS_NLERR(log, "Invalid conntrack helper"); | |
1601 | return -EINVAL; | |
1602 | } | |
1603 | break; | |
f8f97cdc JR |
1604 | #ifdef CONFIG_NF_NAT_NEEDED |
1605 | case OVS_CT_ATTR_NAT: { | |
1606 | int err = parse_nat(a, info, log); | |
1607 | ||
1608 | if (err) | |
1609 | return err; | |
1610 | break; | |
1611 | } | |
1612 | #endif | |
8e83854c JR |
1613 | case OVS_CT_ATTR_EVENTMASK: |
1614 | info->have_eventmask = true; | |
1615 | info->eventmask = nla_get_u32(a); | |
1616 | break; | |
1617 | ||
a94ebc39 JS |
1618 | default: |
1619 | OVS_NLERR(log, "Unknown conntrack attr (%d)", | |
1620 | type); | |
1621 | return -EINVAL; | |
1622 | } | |
1623 | } | |
1624 | ||
39a6542b JR |
1625 | #ifdef CONFIG_NF_CONNTRACK_MARK |
1626 | if (!info->commit && info->mark.mask) { | |
1627 | OVS_NLERR(log, | |
1628 | "Setting conntrack mark requires 'commit' flag."); | |
1629 | return -EINVAL; | |
1630 | } | |
1631 | #endif | |
1632 | #ifdef CONFIG_NF_CONNTRACK_LABELS | |
1633 | if (!info->commit && labels_nonzero(&info->labels.mask)) { | |
1634 | OVS_NLERR(log, | |
1635 | "Setting conntrack labels requires 'commit' flag."); | |
1636 | return -EINVAL; | |
1637 | } | |
1638 | #endif | |
a94ebc39 JS |
1639 | if (rem > 0) { |
1640 | OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem); | |
1641 | return -EINVAL; | |
1642 | } | |
1643 | ||
1644 | return 0; | |
1645 | } | |
1646 | ||
038e34ab | 1647 | bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr) |
a94ebc39 JS |
1648 | { |
1649 | if (attr == OVS_KEY_ATTR_CT_STATE) | |
1650 | return true; | |
1651 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && | |
1652 | attr == OVS_KEY_ATTR_CT_ZONE) | |
1653 | return true; | |
372ce973 JS |
1654 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && |
1655 | attr == OVS_KEY_ATTR_CT_MARK) | |
1656 | return true; | |
038e34ab | 1657 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && |
c05e2094 | 1658 | attr == OVS_KEY_ATTR_CT_LABELS) { |
038e34ab JS |
1659 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
1660 | ||
1661 | return ovs_net->xt_label; | |
1662 | } | |
a94ebc39 JS |
1663 | |
1664 | return false; | |
1665 | } | |
1666 | ||
1667 | int ovs_ct_copy_action(struct net *net, const struct nlattr *attr, | |
1668 | const struct sw_flow_key *key, | |
1669 | struct sw_flow_actions **sfa, bool log) | |
1670 | { | |
1671 | struct ovs_conntrack_info ct_info; | |
11251c17 | 1672 | const char *helper = NULL; |
a94ebc39 JS |
1673 | u16 family; |
1674 | int err; | |
1675 | ||
1676 | family = key_to_nfproto(key); | |
1677 | if (family == NFPROTO_UNSPEC) { | |
1678 | OVS_NLERR(log, "ct family unspecified"); | |
1679 | return -EINVAL; | |
1680 | } | |
1681 | ||
1682 | memset(&ct_info, 0, sizeof(ct_info)); | |
1683 | ct_info.family = family; | |
1684 | ||
1685 | nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID, | |
1686 | NF_CT_DEFAULT_ZONE_DIR, 0); | |
1687 | ||
11251c17 | 1688 | err = parse_ct(attr, &ct_info, &helper, log); |
a94ebc39 JS |
1689 | if (err) |
1690 | return err; | |
1691 | ||
1692 | /* Set up template for tracking connections in specific zones. */ | |
1693 | ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL); | |
1694 | if (!ct_info.ct) { | |
1695 | OVS_NLERR(log, "Failed to allocate conntrack template"); | |
1696 | return -ENOMEM; | |
1697 | } | |
11251c17 JS |
1698 | if (helper) { |
1699 | err = ovs_ct_add_helper(&ct_info, helper, key, log); | |
1700 | if (err) | |
1701 | goto err_free_ct; | |
1702 | } | |
a94ebc39 JS |
1703 | |
1704 | err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info, | |
1705 | sizeof(ct_info), log); | |
1706 | if (err) | |
1707 | goto err_free_ct; | |
1708 | ||
50aa6e68 FL |
1709 | __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status); |
1710 | nf_conntrack_get(&ct_info.ct->ct_general); | |
a94ebc39 JS |
1711 | return 0; |
1712 | err_free_ct: | |
11251c17 | 1713 | __ovs_ct_free_action(&ct_info); |
a94ebc39 JS |
1714 | return err; |
1715 | } | |
1716 | ||
f8f97cdc JR |
1717 | #ifdef CONFIG_NF_NAT_NEEDED |
1718 | static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info, | |
1719 | struct sk_buff *skb) | |
1720 | { | |
1721 | struct nlattr *start; | |
1722 | ||
1723 | start = nla_nest_start(skb, OVS_CT_ATTR_NAT); | |
1724 | if (!start) | |
1725 | return false; | |
1726 | ||
1727 | if (info->nat & OVS_CT_SRC_NAT) { | |
1728 | if (nla_put_flag(skb, OVS_NAT_ATTR_SRC)) | |
1729 | return false; | |
1730 | } else if (info->nat & OVS_CT_DST_NAT) { | |
1731 | if (nla_put_flag(skb, OVS_NAT_ATTR_DST)) | |
1732 | return false; | |
1733 | } else { | |
1734 | goto out; | |
1735 | } | |
1736 | ||
1737 | if (info->range.flags & NF_NAT_RANGE_MAP_IPS) { | |
90b01477 AB |
1738 | if (IS_ENABLED(CONFIG_NF_NAT_IPV4) && |
1739 | info->family == NFPROTO_IPV4) { | |
f8f97cdc JR |
1740 | if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN, |
1741 | info->range.min_addr.ip) || | |
1742 | (info->range.max_addr.ip | |
1743 | != info->range.min_addr.ip && | |
1744 | (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX, | |
1745 | info->range.max_addr.ip)))) | |
1746 | return false; | |
90b01477 AB |
1747 | } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) && |
1748 | info->family == NFPROTO_IPV6) { | |
f8f97cdc JR |
1749 | if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN, |
1750 | &info->range.min_addr.in6) || | |
1751 | (memcmp(&info->range.max_addr.in6, | |
1752 | &info->range.min_addr.in6, | |
1753 | sizeof(info->range.max_addr.in6)) && | |
1754 | (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX, | |
1755 | &info->range.max_addr.in6)))) | |
1756 | return false; | |
f8f97cdc JR |
1757 | } else { |
1758 | return false; | |
1759 | } | |
1760 | } | |
1761 | if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED && | |
1762 | (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN, | |
1763 | ntohs(info->range.min_proto.all)) || | |
1764 | (info->range.max_proto.all != info->range.min_proto.all && | |
1765 | nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX, | |
1766 | ntohs(info->range.max_proto.all))))) | |
1767 | return false; | |
1768 | ||
1769 | if (info->range.flags & NF_NAT_RANGE_PERSISTENT && | |
1770 | nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT)) | |
1771 | return false; | |
1772 | if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM && | |
9f1de150 JR |
1773 | nla_put_flag(skb, info->random_fully_compat |
1774 | ? OVS_NAT_ATTR_PROTO_RANDOM | |
1775 | : OVS_NAT_ATTR_PROTO_HASH)) | |
f8f97cdc | 1776 | return false; |
9f1de150 | 1777 | #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY |
f8f97cdc JR |
1778 | if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY && |
1779 | nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM)) | |
1780 | return false; | |
9f1de150 | 1781 | #endif |
f8f97cdc JR |
1782 | out: |
1783 | nla_nest_end(skb, start); | |
1784 | ||
1785 | return true; | |
1786 | } | |
1787 | #endif | |
1788 | ||
a94ebc39 JS |
1789 | int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info, |
1790 | struct sk_buff *skb) | |
1791 | { | |
1792 | struct nlattr *start; | |
1793 | ||
1794 | start = nla_nest_start(skb, OVS_ACTION_ATTR_CT); | |
1795 | if (!start) | |
1796 | return -EMSGSIZE; | |
1797 | ||
b80e259f JR |
1798 | if (ct_info->commit && nla_put_flag(skb, ct_info->force |
1799 | ? OVS_CT_ATTR_FORCE_COMMIT | |
1800 | : OVS_CT_ATTR_COMMIT)) | |
a94ebc39 JS |
1801 | return -EMSGSIZE; |
1802 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && | |
1803 | nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id)) | |
1804 | return -EMSGSIZE; | |
c05e2094 | 1805 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask && |
372ce973 JS |
1806 | nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark), |
1807 | &ct_info->mark)) | |
1808 | return -EMSGSIZE; | |
038e34ab | 1809 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && |
c05e2094 JS |
1810 | labels_nonzero(&ct_info->labels.mask) && |
1811 | nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels), | |
1812 | &ct_info->labels)) | |
038e34ab | 1813 | return -EMSGSIZE; |
11251c17 JS |
1814 | if (ct_info->helper) { |
1815 | if (nla_put_string(skb, OVS_CT_ATTR_HELPER, | |
1816 | ct_info->helper->name)) | |
1817 | return -EMSGSIZE; | |
1818 | } | |
8e83854c JR |
1819 | if (ct_info->have_eventmask && |
1820 | nla_put_u32(skb, OVS_CT_ATTR_EVENTMASK, ct_info->eventmask)) | |
1821 | return -EMSGSIZE; | |
1822 | ||
f8f97cdc JR |
1823 | #ifdef CONFIG_NF_NAT_NEEDED |
1824 | if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb)) | |
1825 | return -EMSGSIZE; | |
1826 | #endif | |
a94ebc39 JS |
1827 | nla_nest_end(skb, start); |
1828 | ||
1829 | return 0; | |
1830 | } | |
1831 | ||
1832 | void ovs_ct_free_action(const struct nlattr *a) | |
1833 | { | |
1834 | struct ovs_conntrack_info *ct_info = nla_data(a); | |
1835 | ||
11251c17 JS |
1836 | __ovs_ct_free_action(ct_info); |
1837 | } | |
1838 | ||
1839 | static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info) | |
1840 | { | |
1841 | if (ct_info->helper) | |
ac8e3c6d | 1842 | nf_conntrack_helper_put(ct_info->helper); |
a94ebc39 JS |
1843 | if (ct_info->ct) |
1844 | nf_ct_tmpl_free(ct_info->ct); | |
1845 | } | |
1846 | ||
cb2a5486 YHW |
1847 | #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) |
1848 | static int ovs_ct_limit_init(struct net *net, struct ovs_net *ovs_net) | |
1849 | { | |
1850 | int i, err; | |
1851 | ||
1852 | ovs_net->ct_limit_info = kmalloc(sizeof(*ovs_net->ct_limit_info), | |
1853 | GFP_KERNEL); | |
1854 | if (!ovs_net->ct_limit_info) | |
1855 | return -ENOMEM; | |
1856 | ||
1857 | ovs_net->ct_limit_info->default_limit = OVS_CT_LIMIT_DEFAULT; | |
1858 | ovs_net->ct_limit_info->limits = | |
1859 | kmalloc_array(CT_LIMIT_HASH_BUCKETS, sizeof(struct hlist_head), | |
1860 | GFP_KERNEL); | |
1861 | if (!ovs_net->ct_limit_info->limits) { | |
1862 | kfree(ovs_net->ct_limit_info); | |
1863 | return -ENOMEM; | |
1864 | } | |
1865 | ||
1866 | for (i = 0; i < CT_LIMIT_HASH_BUCKETS; i++) | |
1867 | INIT_HLIST_HEAD(&ovs_net->ct_limit_info->limits[i]); | |
1868 | ||
1869 | ovs_net->ct_limit_info->data = | |
1870 | nf_conncount_init(net, NFPROTO_INET, sizeof(u32)); | |
1871 | ||
1872 | if (IS_ERR(ovs_net->ct_limit_info->data)) { | |
1873 | err = PTR_ERR(ovs_net->ct_limit_info->data); | |
1874 | kfree(ovs_net->ct_limit_info->limits); | |
1875 | kfree(ovs_net->ct_limit_info); | |
1876 | pr_err("openvswitch: failed to init nf_conncount %d\n", err); | |
1877 | return err; | |
1878 | } | |
1879 | return 0; | |
1880 | } | |
1881 | ||
1882 | static void ovs_ct_limit_exit(struct net *net, struct ovs_net *ovs_net) | |
1883 | { | |
1884 | const struct ovs_ct_limit_info *info = ovs_net->ct_limit_info; | |
1885 | int i; | |
1886 | ||
1887 | nf_conncount_destroy(net, NFPROTO_INET, info->data); | |
1888 | for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) { | |
1889 | struct hlist_head *head = &info->limits[i]; | |
1890 | struct ovs_ct_limit *ct_limit; | |
1891 | ||
1892 | hlist_for_each_entry_rcu(ct_limit, head, hlist_node) | |
1893 | kfree_rcu(ct_limit, rcu); | |
1894 | } | |
1895 | kfree(ovs_net->ct_limit_info->limits); | |
1896 | kfree(ovs_net->ct_limit_info); | |
1897 | } | |
1898 | ||
1899 | static struct sk_buff * | |
1900 | ovs_ct_limit_cmd_reply_start(struct genl_info *info, u8 cmd, | |
1901 | struct ovs_header **ovs_reply_header) | |
1902 | { | |
1903 | struct ovs_header *ovs_header = info->userhdr; | |
1904 | struct sk_buff *skb; | |
1905 | ||
1906 | skb = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
1907 | if (!skb) | |
1908 | return ERR_PTR(-ENOMEM); | |
1909 | ||
1910 | *ovs_reply_header = genlmsg_put(skb, info->snd_portid, | |
1911 | info->snd_seq, | |
1912 | &dp_ct_limit_genl_family, 0, cmd); | |
1913 | ||
1914 | if (!*ovs_reply_header) { | |
1915 | nlmsg_free(skb); | |
1916 | return ERR_PTR(-EMSGSIZE); | |
1917 | } | |
1918 | (*ovs_reply_header)->dp_ifindex = ovs_header->dp_ifindex; | |
1919 | ||
1920 | return skb; | |
1921 | } | |
1922 | ||
1923 | static bool check_zone_id(int zone_id, u16 *pzone) | |
1924 | { | |
1925 | if (zone_id >= 0 && zone_id <= 65535) { | |
1926 | *pzone = (u16)zone_id; | |
1927 | return true; | |
1928 | } | |
1929 | return false; | |
1930 | } | |
1931 | ||
1932 | static int ovs_ct_limit_set_zone_limit(struct nlattr *nla_zone_limit, | |
1933 | struct ovs_ct_limit_info *info) | |
1934 | { | |
1935 | struct ovs_zone_limit *zone_limit; | |
1936 | int rem; | |
1937 | u16 zone; | |
1938 | ||
1939 | rem = NLA_ALIGN(nla_len(nla_zone_limit)); | |
1940 | zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit); | |
1941 | ||
1942 | while (rem >= sizeof(*zone_limit)) { | |
1943 | if (unlikely(zone_limit->zone_id == | |
1944 | OVS_ZONE_LIMIT_DEFAULT_ZONE)) { | |
1945 | ovs_lock(); | |
1946 | info->default_limit = zone_limit->limit; | |
1947 | ovs_unlock(); | |
1948 | } else if (unlikely(!check_zone_id( | |
1949 | zone_limit->zone_id, &zone))) { | |
1950 | OVS_NLERR(true, "zone id is out of range"); | |
1951 | } else { | |
1952 | struct ovs_ct_limit *ct_limit; | |
1953 | ||
1954 | ct_limit = kmalloc(sizeof(*ct_limit), GFP_KERNEL); | |
1955 | if (!ct_limit) | |
1956 | return -ENOMEM; | |
1957 | ||
1958 | ct_limit->zone = zone; | |
1959 | ct_limit->limit = zone_limit->limit; | |
1960 | ||
1961 | ovs_lock(); | |
1962 | ct_limit_set(info, ct_limit); | |
1963 | ovs_unlock(); | |
1964 | } | |
1965 | rem -= NLA_ALIGN(sizeof(*zone_limit)); | |
1966 | zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit + | |
1967 | NLA_ALIGN(sizeof(*zone_limit))); | |
1968 | } | |
1969 | ||
1970 | if (rem) | |
1971 | OVS_NLERR(true, "set zone limit has %d unknown bytes", rem); | |
1972 | ||
1973 | return 0; | |
1974 | } | |
1975 | ||
1976 | static int ovs_ct_limit_del_zone_limit(struct nlattr *nla_zone_limit, | |
1977 | struct ovs_ct_limit_info *info) | |
1978 | { | |
1979 | struct ovs_zone_limit *zone_limit; | |
1980 | int rem; | |
1981 | u16 zone; | |
1982 | ||
1983 | rem = NLA_ALIGN(nla_len(nla_zone_limit)); | |
1984 | zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit); | |
1985 | ||
1986 | while (rem >= sizeof(*zone_limit)) { | |
1987 | if (unlikely(zone_limit->zone_id == | |
1988 | OVS_ZONE_LIMIT_DEFAULT_ZONE)) { | |
1989 | ovs_lock(); | |
1990 | info->default_limit = OVS_CT_LIMIT_DEFAULT; | |
1991 | ovs_unlock(); | |
1992 | } else if (unlikely(!check_zone_id( | |
1993 | zone_limit->zone_id, &zone))) { | |
1994 | OVS_NLERR(true, "zone id is out of range"); | |
1995 | } else { | |
1996 | ovs_lock(); | |
1997 | ct_limit_del(info, zone); | |
1998 | ovs_unlock(); | |
1999 | } | |
2000 | rem -= NLA_ALIGN(sizeof(*zone_limit)); | |
2001 | zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit + | |
2002 | NLA_ALIGN(sizeof(*zone_limit))); | |
2003 | } | |
2004 | ||
2005 | if (rem) | |
2006 | OVS_NLERR(true, "del zone limit has %d unknown bytes", rem); | |
2007 | ||
2008 | return 0; | |
2009 | } | |
2010 | ||
2011 | static int ovs_ct_limit_get_default_limit(struct ovs_ct_limit_info *info, | |
2012 | struct sk_buff *reply) | |
2013 | { | |
2014 | struct ovs_zone_limit zone_limit; | |
2015 | int err; | |
2016 | ||
2017 | zone_limit.zone_id = OVS_ZONE_LIMIT_DEFAULT_ZONE; | |
2018 | zone_limit.limit = info->default_limit; | |
2019 | err = nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit); | |
2020 | if (err) | |
2021 | return err; | |
2022 | ||
2023 | return 0; | |
2024 | } | |
2025 | ||
2026 | static int __ovs_ct_limit_get_zone_limit(struct net *net, | |
2027 | struct nf_conncount_data *data, | |
2028 | u16 zone_id, u32 limit, | |
2029 | struct sk_buff *reply) | |
2030 | { | |
2031 | struct nf_conntrack_zone ct_zone; | |
2032 | struct ovs_zone_limit zone_limit; | |
2033 | u32 conncount_key = zone_id; | |
2034 | ||
2035 | zone_limit.zone_id = zone_id; | |
2036 | zone_limit.limit = limit; | |
2037 | nf_ct_zone_init(&ct_zone, zone_id, NF_CT_DEFAULT_ZONE_DIR, 0); | |
2038 | ||
2039 | zone_limit.count = nf_conncount_count(net, data, &conncount_key, NULL, | |
2040 | &ct_zone); | |
2041 | return nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit); | |
2042 | } | |
2043 | ||
2044 | static int ovs_ct_limit_get_zone_limit(struct net *net, | |
2045 | struct nlattr *nla_zone_limit, | |
2046 | struct ovs_ct_limit_info *info, | |
2047 | struct sk_buff *reply) | |
2048 | { | |
2049 | struct ovs_zone_limit *zone_limit; | |
2050 | int rem, err; | |
2051 | u32 limit; | |
2052 | u16 zone; | |
2053 | ||
2054 | rem = NLA_ALIGN(nla_len(nla_zone_limit)); | |
2055 | zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit); | |
2056 | ||
2057 | while (rem >= sizeof(*zone_limit)) { | |
2058 | if (unlikely(zone_limit->zone_id == | |
2059 | OVS_ZONE_LIMIT_DEFAULT_ZONE)) { | |
2060 | err = ovs_ct_limit_get_default_limit(info, reply); | |
2061 | if (err) | |
2062 | return err; | |
2063 | } else if (unlikely(!check_zone_id(zone_limit->zone_id, | |
2064 | &zone))) { | |
2065 | OVS_NLERR(true, "zone id is out of range"); | |
2066 | } else { | |
2067 | rcu_read_lock(); | |
2068 | limit = ct_limit_get(info, zone); | |
2069 | rcu_read_unlock(); | |
2070 | ||
2071 | err = __ovs_ct_limit_get_zone_limit( | |
2072 | net, info->data, zone, limit, reply); | |
2073 | if (err) | |
2074 | return err; | |
2075 | } | |
2076 | rem -= NLA_ALIGN(sizeof(*zone_limit)); | |
2077 | zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit + | |
2078 | NLA_ALIGN(sizeof(*zone_limit))); | |
2079 | } | |
2080 | ||
2081 | if (rem) | |
2082 | OVS_NLERR(true, "get zone limit has %d unknown bytes", rem); | |
2083 | ||
2084 | return 0; | |
2085 | } | |
2086 | ||
2087 | static int ovs_ct_limit_get_all_zone_limit(struct net *net, | |
2088 | struct ovs_ct_limit_info *info, | |
2089 | struct sk_buff *reply) | |
2090 | { | |
2091 | struct ovs_ct_limit *ct_limit; | |
2092 | struct hlist_head *head; | |
2093 | int i, err = 0; | |
2094 | ||
2095 | err = ovs_ct_limit_get_default_limit(info, reply); | |
2096 | if (err) | |
2097 | return err; | |
2098 | ||
2099 | rcu_read_lock(); | |
2100 | for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) { | |
2101 | head = &info->limits[i]; | |
2102 | hlist_for_each_entry_rcu(ct_limit, head, hlist_node) { | |
2103 | err = __ovs_ct_limit_get_zone_limit(net, info->data, | |
2104 | ct_limit->zone, ct_limit->limit, reply); | |
2105 | if (err) | |
2106 | goto exit_err; | |
2107 | } | |
2108 | } | |
2109 | ||
2110 | exit_err: | |
2111 | rcu_read_unlock(); | |
2112 | return err; | |
2113 | } | |
2114 | ||
2115 | static int ovs_ct_limit_cmd_set(struct sk_buff *skb, struct genl_info *info) | |
2116 | { | |
2117 | struct nlattr **a = info->attrs; | |
2118 | struct sk_buff *reply; | |
2119 | struct ovs_header *ovs_reply_header; | |
2120 | struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id); | |
2121 | struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info; | |
2122 | int err; | |
2123 | ||
2124 | reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_SET, | |
2125 | &ovs_reply_header); | |
2126 | if (IS_ERR(reply)) | |
2127 | return PTR_ERR(reply); | |
2128 | ||
2129 | if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) { | |
2130 | err = -EINVAL; | |
2131 | goto exit_err; | |
2132 | } | |
2133 | ||
2134 | err = ovs_ct_limit_set_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT], | |
2135 | ct_limit_info); | |
2136 | if (err) | |
2137 | goto exit_err; | |
2138 | ||
2139 | static_branch_enable(&ovs_ct_limit_enabled); | |
2140 | ||
2141 | genlmsg_end(reply, ovs_reply_header); | |
2142 | return genlmsg_reply(reply, info); | |
2143 | ||
2144 | exit_err: | |
2145 | nlmsg_free(reply); | |
2146 | return err; | |
2147 | } | |
2148 | ||
2149 | static int ovs_ct_limit_cmd_del(struct sk_buff *skb, struct genl_info *info) | |
2150 | { | |
2151 | struct nlattr **a = info->attrs; | |
2152 | struct sk_buff *reply; | |
2153 | struct ovs_header *ovs_reply_header; | |
2154 | struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id); | |
2155 | struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info; | |
2156 | int err; | |
2157 | ||
2158 | reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_DEL, | |
2159 | &ovs_reply_header); | |
2160 | if (IS_ERR(reply)) | |
2161 | return PTR_ERR(reply); | |
2162 | ||
2163 | if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) { | |
2164 | err = -EINVAL; | |
2165 | goto exit_err; | |
2166 | } | |
2167 | ||
2168 | err = ovs_ct_limit_del_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT], | |
2169 | ct_limit_info); | |
2170 | if (err) | |
2171 | goto exit_err; | |
2172 | ||
2173 | genlmsg_end(reply, ovs_reply_header); | |
2174 | return genlmsg_reply(reply, info); | |
2175 | ||
2176 | exit_err: | |
2177 | nlmsg_free(reply); | |
2178 | return err; | |
2179 | } | |
2180 | ||
2181 | static int ovs_ct_limit_cmd_get(struct sk_buff *skb, struct genl_info *info) | |
2182 | { | |
2183 | struct nlattr **a = info->attrs; | |
2184 | struct nlattr *nla_reply; | |
2185 | struct sk_buff *reply; | |
2186 | struct ovs_header *ovs_reply_header; | |
2187 | struct net *net = sock_net(skb->sk); | |
2188 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); | |
2189 | struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info; | |
2190 | int err; | |
2191 | ||
2192 | reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_GET, | |
2193 | &ovs_reply_header); | |
2194 | if (IS_ERR(reply)) | |
2195 | return PTR_ERR(reply); | |
2196 | ||
2197 | nla_reply = nla_nest_start(reply, OVS_CT_LIMIT_ATTR_ZONE_LIMIT); | |
2198 | ||
2199 | if (a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) { | |
2200 | err = ovs_ct_limit_get_zone_limit( | |
2201 | net, a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT], ct_limit_info, | |
2202 | reply); | |
2203 | if (err) | |
2204 | goto exit_err; | |
2205 | } else { | |
2206 | err = ovs_ct_limit_get_all_zone_limit(net, ct_limit_info, | |
2207 | reply); | |
2208 | if (err) | |
2209 | goto exit_err; | |
2210 | } | |
2211 | ||
2212 | nla_nest_end(reply, nla_reply); | |
2213 | genlmsg_end(reply, ovs_reply_header); | |
2214 | return genlmsg_reply(reply, info); | |
2215 | ||
2216 | exit_err: | |
2217 | nlmsg_free(reply); | |
2218 | return err; | |
2219 | } | |
2220 | ||
2221 | static struct genl_ops ct_limit_genl_ops[] = { | |
2222 | { .cmd = OVS_CT_LIMIT_CMD_SET, | |
2223 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN | |
2224 | * privilege. */ | |
2225 | .policy = ct_limit_policy, | |
2226 | .doit = ovs_ct_limit_cmd_set, | |
2227 | }, | |
2228 | { .cmd = OVS_CT_LIMIT_CMD_DEL, | |
2229 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN | |
2230 | * privilege. */ | |
2231 | .policy = ct_limit_policy, | |
2232 | .doit = ovs_ct_limit_cmd_del, | |
2233 | }, | |
2234 | { .cmd = OVS_CT_LIMIT_CMD_GET, | |
2235 | .flags = 0, /* OK for unprivileged users. */ | |
2236 | .policy = ct_limit_policy, | |
2237 | .doit = ovs_ct_limit_cmd_get, | |
2238 | }, | |
2239 | }; | |
2240 | ||
2241 | static const struct genl_multicast_group ovs_ct_limit_multicast_group = { | |
2242 | .name = OVS_CT_LIMIT_MCGROUP, | |
2243 | }; | |
2244 | ||
2245 | struct genl_family dp_ct_limit_genl_family __ro_after_init = { | |
2246 | .hdrsize = sizeof(struct ovs_header), | |
2247 | .name = OVS_CT_LIMIT_FAMILY, | |
2248 | .version = OVS_CT_LIMIT_VERSION, | |
2249 | .maxattr = OVS_CT_LIMIT_ATTR_MAX, | |
2250 | .netnsok = true, | |
2251 | .parallel_ops = true, | |
2252 | .ops = ct_limit_genl_ops, | |
2253 | .n_ops = ARRAY_SIZE(ct_limit_genl_ops), | |
2254 | .mcgrps = &ovs_ct_limit_multicast_group, | |
2255 | .n_mcgrps = 1, | |
2256 | .module = THIS_MODULE, | |
2257 | }; | |
2258 | #endif | |
2259 | ||
2260 | int ovs_ct_init(struct net *net) | |
038e34ab | 2261 | { |
c05e2094 | 2262 | unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE; |
038e34ab JS |
2263 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
2264 | ||
7f2ab8cd | 2265 | if (nf_connlabels_get(net, n_bits - 1)) { |
038e34ab JS |
2266 | ovs_net->xt_label = false; |
2267 | OVS_NLERR(true, "Failed to set connlabel length"); | |
2268 | } else { | |
2269 | ovs_net->xt_label = true; | |
2270 | } | |
cb2a5486 YHW |
2271 | |
2272 | #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) | |
2273 | return ovs_ct_limit_init(net, ovs_net); | |
2274 | #else | |
2275 | return 0; | |
2276 | #endif | |
038e34ab JS |
2277 | } |
2278 | ||
2279 | void ovs_ct_exit(struct net *net) | |
2280 | { | |
2281 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); | |
2282 | ||
cb2a5486 YHW |
2283 | #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) |
2284 | ovs_ct_limit_exit(net, ovs_net); | |
2285 | #endif | |
2286 | ||
038e34ab JS |
2287 | if (ovs_net->xt_label) |
2288 | nf_connlabels_put(net); | |
2289 | } | |
2290 | ||
8063e095 | 2291 | #endif /* CONFIG_NF_CONNTRACK */ |