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