]>
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> | |
4f7e690e | 24 | #include <linux/static_key.h> |
a94ebc39 | 25 | #include <net/ip.h> |
4f7e690e | 26 | #include <net/genetlink.h> |
a94ebc39 | 27 | #include <net/netfilter/nf_conntrack_core.h> |
4f7e690e | 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 | |
bb9518d9 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 |
bb9518d9 | 89 | struct nf_nat_range2 range; /* Only present for SRC NAT and DST NAT. */ |
f8f97cdc | 90 | #endif |
a94ebc39 JS |
91 | }; |
92 | ||
4f7e690e 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, | |
bb9518d9 | 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) { | |
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 | ||
a6d28f7c PNA |
1001 | err = nf_conntrack_in(net, info->family, |
1002 | NF_INET_PRE_ROUTING, skb); | |
9bf67b92 | 1003 | if (err != NF_ACCEPT) |
a94ebc39 | 1004 | return -ENOENT; |
11251c17 | 1005 | |
f8f97cdc JR |
1006 | /* Clear CT state NAT flags to mark that we have not yet done |
1007 | * NAT after the nf_conntrack_in() call. We can actually clear | |
1008 | * the whole state, as it will be re-initialized below. | |
1009 | */ | |
1963a885 | 1010 | key->ct_state = 0; |
f8f97cdc JR |
1011 | |
1012 | /* Update the key, but keep the NAT flags. */ | |
1013 | ovs_ct_update_key(skb, info, key, true, true); | |
b21d237e | 1014 | } |
a04a5794 | 1015 | |
b21d237e | 1016 | ct = nf_ct_get(skb, &ctinfo); |
f8f97cdc JR |
1017 | if (ct) { |
1018 | /* Packets starting a new connection must be NATted before the | |
1019 | * helper, so that the helper knows about the NAT. We enforce | |
1020 | * this by delaying both NAT and helper calls for unconfirmed | |
1021 | * connections until the committing CT action. For later | |
1022 | * packets NAT and Helper may be called in either order. | |
1023 | * | |
1024 | * NAT will be done only if the CT action has NAT, and only | |
1025 | * once per packet (per zone), as guarded by the NAT bits in | |
1963a885 | 1026 | * the key->ct_state. |
f8f97cdc | 1027 | */ |
1963a885 | 1028 | if (info->nat && !(key->ct_state & OVS_CS_F_NAT_MASK) && |
f8f97cdc JR |
1029 | (nf_ct_is_confirmed(ct) || info->commit) && |
1030 | ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) { | |
1031 | return -EINVAL; | |
1032 | } | |
1033 | ||
b87a5aac JS |
1034 | /* Userspace may decide to perform a ct lookup without a helper |
1035 | * specified followed by a (recirculate and) commit with one. | |
1036 | * Therefore, for unconfirmed connections which we will commit, | |
1037 | * we need to attach the helper here. | |
1038 | */ | |
1039 | if (!nf_ct_is_confirmed(ct) && info->commit && | |
1040 | info->helper && !nfct_help(ct)) { | |
1041 | int err = __nf_ct_try_assign_helper(ct, info->ct, | |
1042 | GFP_ATOMIC); | |
1043 | if (err) | |
1044 | return err; | |
1045 | } | |
1046 | ||
f8f97cdc JR |
1047 | /* Call the helper only if: |
1048 | * - nf_conntrack_in() was executed above ("!cached") for a | |
1049 | * confirmed connection, or | |
1050 | * - When committing an unconfirmed connection. | |
1051 | */ | |
1052 | if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) && | |
1053 | ovs_ct_helper(skb, info->family) != NF_ACCEPT) { | |
1054 | return -EINVAL; | |
1055 | } | |
a94ebc39 JS |
1056 | } |
1057 | ||
1058 | return 0; | |
1059 | } | |
1060 | ||
1061 | /* Lookup connection and read fields into key. */ | |
1062 | static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key, | |
1063 | const struct ovs_conntrack_info *info, | |
1064 | struct sk_buff *skb) | |
1065 | { | |
1066 | struct nf_conntrack_expect *exp; | |
1067 | ||
b0f251cd JR |
1068 | /* If we pass an expected packet through nf_conntrack_in() the |
1069 | * expectation is typically removed, but the packet could still be | |
1070 | * lost in upcall processing. To prevent this from happening we | |
1071 | * perform an explicit expectation lookup. Expected connections are | |
1072 | * always new, and will be passed through conntrack only when they are | |
1073 | * committed, as it is OK to remove the expectation at that time. | |
1074 | */ | |
a94ebc39 JS |
1075 | exp = ovs_ct_expect_find(net, &info->zone, info->family, skb); |
1076 | if (exp) { | |
1077 | u8 state; | |
1078 | ||
f8f97cdc JR |
1079 | /* NOTE: New connections are NATted and Helped only when |
1080 | * committed, so we are not calling into NAT here. | |
1081 | */ | |
a94ebc39 | 1082 | state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED; |
372ce973 | 1083 | __ovs_ct_update_key(key, state, &info->zone, exp->master); |
f325530e PS |
1084 | } else { |
1085 | struct nf_conn *ct; | |
1086 | int err; | |
1087 | ||
1088 | err = __ovs_ct_lookup(net, key, info, skb); | |
1089 | if (err) | |
1090 | return err; | |
1091 | ||
d3c313c1 | 1092 | ct = (struct nf_conn *)skb_nfct(skb); |
f325530e PS |
1093 | if (ct) |
1094 | nf_ct_deliver_cached_events(ct); | |
1095 | } | |
a94ebc39 JS |
1096 | |
1097 | return 0; | |
1098 | } | |
1099 | ||
c05e2094 | 1100 | static bool labels_nonzero(const struct ovs_key_ct_labels *labels) |
038e34ab JS |
1101 | { |
1102 | size_t i; | |
1103 | ||
83495bd9 JR |
1104 | for (i = 0; i < OVS_CT_LABELS_LEN_32; i++) |
1105 | if (labels->ct_labels_32[i]) | |
038e34ab JS |
1106 | return true; |
1107 | ||
1108 | return false; | |
1109 | } | |
1110 | ||
4f7e690e YHW |
1111 | #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) |
1112 | static struct hlist_head *ct_limit_hash_bucket( | |
1113 | const struct ovs_ct_limit_info *info, u16 zone) | |
1114 | { | |
1115 | return &info->limits[zone & (CT_LIMIT_HASH_BUCKETS - 1)]; | |
1116 | } | |
1117 | ||
1118 | /* Call with ovs_mutex */ | |
1119 | static void ct_limit_set(const struct ovs_ct_limit_info *info, | |
1120 | struct ovs_ct_limit *new_ct_limit) | |
1121 | { | |
1122 | struct ovs_ct_limit *ct_limit; | |
1123 | struct hlist_head *head; | |
1124 | ||
1125 | head = ct_limit_hash_bucket(info, new_ct_limit->zone); | |
1126 | hlist_for_each_entry_rcu(ct_limit, head, hlist_node) { | |
1127 | if (ct_limit->zone == new_ct_limit->zone) { | |
1128 | hlist_replace_rcu(&ct_limit->hlist_node, | |
1129 | &new_ct_limit->hlist_node); | |
1130 | kfree_rcu(ct_limit, rcu); | |
1131 | return; | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | hlist_add_head_rcu(&new_ct_limit->hlist_node, head); | |
1136 | } | |
1137 | ||
1138 | /* Call with ovs_mutex */ | |
1139 | static void ct_limit_del(const struct ovs_ct_limit_info *info, u16 zone) | |
1140 | { | |
1141 | struct ovs_ct_limit *ct_limit; | |
1142 | struct hlist_head *head; | |
1143 | struct hlist_node *n; | |
1144 | ||
1145 | head = ct_limit_hash_bucket(info, zone); | |
1146 | hlist_for_each_entry_safe(ct_limit, n, head, hlist_node) { | |
1147 | if (ct_limit->zone == zone) { | |
1148 | hlist_del_rcu(&ct_limit->hlist_node); | |
1149 | kfree_rcu(ct_limit, rcu); | |
1150 | return; | |
1151 | } | |
1152 | } | |
1153 | } | |
1154 | ||
1155 | /* Call with RCU read lock */ | |
1156 | static u32 ct_limit_get(const struct ovs_ct_limit_info *info, u16 zone) | |
1157 | { | |
1158 | struct ovs_ct_limit *ct_limit; | |
1159 | struct hlist_head *head; | |
1160 | ||
1161 | head = ct_limit_hash_bucket(info, zone); | |
1162 | hlist_for_each_entry_rcu(ct_limit, head, hlist_node) { | |
1163 | if (ct_limit->zone == zone) | |
1164 | return ct_limit->limit; | |
1165 | } | |
1166 | ||
1167 | return info->default_limit; | |
1168 | } | |
1169 | ||
1170 | static int ovs_ct_check_limit(struct net *net, | |
1171 | const struct ovs_conntrack_info *info, | |
1172 | const struct nf_conntrack_tuple *tuple) | |
1173 | { | |
1174 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); | |
1175 | const struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info; | |
1176 | u32 per_zone_limit, connections; | |
1177 | u32 conncount_key; | |
1178 | ||
1179 | conncount_key = info->zone.id; | |
1180 | ||
1181 | per_zone_limit = ct_limit_get(ct_limit_info, info->zone.id); | |
1182 | if (per_zone_limit == OVS_CT_LIMIT_UNLIMITED) | |
1183 | return 0; | |
1184 | ||
1185 | connections = nf_conncount_count(net, ct_limit_info->data, | |
1186 | &conncount_key, tuple, &info->zone); | |
1187 | if (connections > per_zone_limit) | |
1188 | return -ENOMEM; | |
1189 | ||
1190 | return 0; | |
1191 | } | |
1192 | #endif | |
1193 | ||
39a6542b JR |
1194 | /* Lookup connection and confirm if unconfirmed. */ |
1195 | static int ovs_ct_commit(struct net *net, struct sw_flow_key *key, | |
1196 | const struct ovs_conntrack_info *info, | |
1197 | struct sk_buff *skb) | |
1198 | { | |
efd40994 JR |
1199 | enum ip_conntrack_info ctinfo; |
1200 | struct nf_conn *ct; | |
39a6542b JR |
1201 | int err; |
1202 | ||
1203 | err = __ovs_ct_lookup(net, key, info, skb); | |
1204 | if (err) | |
1205 | return err; | |
1206 | ||
efd40994 JR |
1207 | /* The connection could be invalid, in which case this is a no-op.*/ |
1208 | ct = nf_ct_get(skb, &ctinfo); | |
1209 | if (!ct) | |
1210 | return 0; | |
1211 | ||
4f7e690e YHW |
1212 | #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) |
1213 | if (static_branch_unlikely(&ovs_ct_limit_enabled)) { | |
1214 | if (!nf_ct_is_confirmed(ct)) { | |
1215 | err = ovs_ct_check_limit(net, info, | |
1216 | &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); | |
1217 | if (err) { | |
1218 | net_warn_ratelimited("openvswitch: zone: %u " | |
1219 | "execeeds conntrack limit\n", | |
1220 | info->zone.id); | |
1221 | return err; | |
1222 | } | |
1223 | } | |
1224 | } | |
1225 | #endif | |
1226 | ||
8e83854c JR |
1227 | /* Set the conntrack event mask if given. NEW and DELETE events have |
1228 | * their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener | |
1229 | * typically would receive many kinds of updates. Setting the event | |
1230 | * mask allows those events to be filtered. The set event mask will | |
1231 | * remain in effect for the lifetime of the connection unless changed | |
1232 | * by a further CT action with both the commit flag and the eventmask | |
1233 | * option. */ | |
1234 | if (info->have_eventmask) { | |
1235 | struct nf_conntrack_ecache *cache = nf_ct_ecache_find(ct); | |
1236 | ||
1237 | if (cache) | |
1238 | cache->ctmask = info->eventmask; | |
1239 | } | |
1240 | ||
39a6542b JR |
1241 | /* Apply changes before confirming the connection so that the initial |
1242 | * conntrack NEW netlink event carries the values given in the CT | |
1243 | * action. | |
1244 | */ | |
1245 | if (info->mark.mask) { | |
efd40994 | 1246 | err = ovs_ct_set_mark(ct, key, info->mark.value, |
39a6542b JR |
1247 | info->mark.mask); |
1248 | if (err) | |
1249 | return err; | |
1250 | } | |
7162cae4 JR |
1251 | if (!nf_ct_is_confirmed(ct)) { |
1252 | err = ovs_ct_init_labels(ct, key, &info->labels.value, | |
1253 | &info->labels.mask); | |
1254 | if (err) | |
1255 | return err; | |
1256 | } else if (labels_nonzero(&info->labels.mask)) { | |
1257 | err = ovs_ct_set_labels(ct, key, &info->labels.value, | |
1258 | &info->labels.mask); | |
39a6542b JR |
1259 | if (err) |
1260 | return err; | |
1261 | } | |
1262 | /* This will take care of sending queued events even if the connection | |
1263 | * is already confirmed. | |
1264 | */ | |
1265 | if (nf_conntrack_confirm(skb) != NF_ACCEPT) | |
1266 | return -EINVAL; | |
1267 | ||
1268 | return 0; | |
1269 | } | |
1270 | ||
c21adacd ES |
1271 | /* Trim the skb to the length specified by the IP/IPv6 header, |
1272 | * removing any trailing lower-layer padding. This prepares the skb | |
1273 | * for higher-layer processing that assumes skb->len excludes padding | |
1274 | * (such as nf_ip_checksum). The caller needs to pull the skb to the | |
1275 | * network header, and ensure ip_hdr/ipv6_hdr points to valid data. | |
1276 | */ | |
1277 | static int ovs_skb_network_trim(struct sk_buff *skb) | |
1278 | { | |
1279 | unsigned int len; | |
1280 | int err; | |
1281 | ||
1282 | switch (skb->protocol) { | |
1283 | case htons(ETH_P_IP): | |
1284 | len = ntohs(ip_hdr(skb)->tot_len); | |
1285 | break; | |
1286 | case htons(ETH_P_IPV6): | |
1287 | len = sizeof(struct ipv6hdr) | |
1288 | + ntohs(ipv6_hdr(skb)->payload_len); | |
1289 | break; | |
1290 | default: | |
1291 | len = skb->len; | |
1292 | } | |
1293 | ||
1294 | err = pskb_trim_rcsum(skb, len); | |
1295 | if (err) | |
1296 | kfree_skb(skb); | |
1297 | ||
1298 | return err; | |
1299 | } | |
1300 | ||
c05e2094 JS |
1301 | /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero |
1302 | * value if 'skb' is freed. | |
1303 | */ | |
a94ebc39 JS |
1304 | int ovs_ct_execute(struct net *net, struct sk_buff *skb, |
1305 | struct sw_flow_key *key, | |
1306 | const struct ovs_conntrack_info *info) | |
1307 | { | |
1308 | int nh_ofs; | |
1309 | int err; | |
1310 | ||
1311 | /* The conntrack module expects to be working at L3. */ | |
1312 | nh_ofs = skb_network_offset(skb); | |
073c7b86 | 1313 | skb_pull_rcsum(skb, nh_ofs); |
a94ebc39 | 1314 | |
c21adacd ES |
1315 | err = ovs_skb_network_trim(skb); |
1316 | if (err) | |
1317 | return err; | |
1318 | ||
a94ebc39 JS |
1319 | if (key->ip.frag != OVS_FRAG_TYPE_NONE) { |
1320 | err = handle_fragments(net, key, info->zone.id, skb); | |
1321 | if (err) | |
1322 | return err; | |
1323 | } | |
1324 | ||
c05e2094 | 1325 | if (info->commit) |
39a6542b | 1326 | err = ovs_ct_commit(net, key, info, skb); |
a94ebc39 JS |
1327 | else |
1328 | err = ovs_ct_lookup(net, key, info, skb); | |
1329 | ||
1330 | skb_push(skb, nh_ofs); | |
073c7b86 | 1331 | skb_postpush_rcsum(skb, skb->data, nh_ofs); |
c05e2094 JS |
1332 | if (err) |
1333 | kfree_skb(skb); | |
a94ebc39 JS |
1334 | return err; |
1335 | } | |
1336 | ||
0cdfdddd EG |
1337 | int ovs_ct_clear(struct sk_buff *skb, struct sw_flow_key *key) |
1338 | { | |
1339 | if (skb_nfct(skb)) { | |
1340 | nf_conntrack_put(skb_nfct(skb)); | |
1341 | #ifdef HAVE_IP_CT_UNTRACKED | |
1342 | nf_ct_set(skb, NULL, IP_CT_UNTRACKED); | |
1343 | #else | |
1344 | nf_ct_set(skb, NULL, 0); | |
1345 | #endif | |
1346 | ovs_ct_fill_key(skb, key); | |
1347 | } | |
1348 | ||
1349 | return 0; | |
1350 | } | |
1351 | ||
11251c17 JS |
1352 | static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name, |
1353 | const struct sw_flow_key *key, bool log) | |
1354 | { | |
1355 | struct nf_conntrack_helper *helper; | |
1356 | struct nf_conn_help *help; | |
1357 | ||
1358 | helper = nf_conntrack_helper_try_module_get(name, info->family, | |
1359 | key->ip.proto); | |
1360 | if (!helper) { | |
1361 | OVS_NLERR(log, "Unknown helper \"%s\"", name); | |
1362 | return -EINVAL; | |
1363 | } | |
1364 | ||
1365 | help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL); | |
1366 | if (!help) { | |
ac8e3c6d | 1367 | nf_conntrack_helper_put(helper); |
11251c17 JS |
1368 | return -ENOMEM; |
1369 | } | |
1370 | ||
1371 | rcu_assign_pointer(help->helper, helper); | |
1372 | info->helper = helper; | |
1373 | return 0; | |
1374 | } | |
1375 | ||
f8f97cdc JR |
1376 | #ifdef CONFIG_NF_NAT_NEEDED |
1377 | static int parse_nat(const struct nlattr *attr, | |
1378 | struct ovs_conntrack_info *info, bool log) | |
1379 | { | |
1380 | struct nlattr *a; | |
1381 | int rem; | |
1382 | bool have_ip_max = false; | |
1383 | bool have_proto_max = false; | |
1384 | bool ip_vers = (info->family == NFPROTO_IPV6); | |
1385 | ||
1386 | nla_for_each_nested(a, attr, rem) { | |
1387 | static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = { | |
1388 | [OVS_NAT_ATTR_SRC] = {0, 0}, | |
1389 | [OVS_NAT_ATTR_DST] = {0, 0}, | |
1390 | [OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr), | |
1391 | sizeof(struct in6_addr)}, | |
1392 | [OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr), | |
1393 | sizeof(struct in6_addr)}, | |
1394 | [OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)}, | |
1395 | [OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)}, | |
1396 | [OVS_NAT_ATTR_PERSISTENT] = {0, 0}, | |
1397 | [OVS_NAT_ATTR_PROTO_HASH] = {0, 0}, | |
1398 | [OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0}, | |
1399 | }; | |
1400 | int type = nla_type(a); | |
1401 | ||
1402 | if (type > OVS_NAT_ATTR_MAX) { | |
72ef85ed | 1403 | OVS_NLERR(log, "Unknown NAT attribute (type=%d, max=%d)", |
f8f97cdc JR |
1404 | type, OVS_NAT_ATTR_MAX); |
1405 | return -EINVAL; | |
1406 | } | |
1407 | ||
1408 | if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) { | |
72ef85ed | 1409 | OVS_NLERR(log, "NAT attribute type %d has unexpected length (%d != %d)", |
f8f97cdc JR |
1410 | type, nla_len(a), |
1411 | ovs_nat_attr_lens[type][ip_vers]); | |
1412 | return -EINVAL; | |
1413 | } | |
1414 | ||
1415 | switch (type) { | |
1416 | case OVS_NAT_ATTR_SRC: | |
1417 | case OVS_NAT_ATTR_DST: | |
1418 | if (info->nat) { | |
72ef85ed | 1419 | OVS_NLERR(log, "Only one type of NAT may be specified"); |
f8f97cdc JR |
1420 | return -ERANGE; |
1421 | } | |
1422 | info->nat |= OVS_CT_NAT; | |
1423 | info->nat |= ((type == OVS_NAT_ATTR_SRC) | |
1424 | ? OVS_CT_SRC_NAT : OVS_CT_DST_NAT); | |
1425 | break; | |
1426 | ||
1427 | case OVS_NAT_ATTR_IP_MIN: | |
70e71d27 HY |
1428 | nla_memcpy(&info->range.min_addr, a, |
1429 | sizeof(info->range.min_addr)); | |
f8f97cdc JR |
1430 | info->range.flags |= NF_NAT_RANGE_MAP_IPS; |
1431 | break; | |
1432 | ||
1433 | case OVS_NAT_ATTR_IP_MAX: | |
1434 | have_ip_max = true; | |
1435 | nla_memcpy(&info->range.max_addr, a, | |
1436 | sizeof(info->range.max_addr)); | |
1437 | info->range.flags |= NF_NAT_RANGE_MAP_IPS; | |
1438 | break; | |
1439 | ||
1440 | case OVS_NAT_ATTR_PROTO_MIN: | |
1441 | info->range.min_proto.all = htons(nla_get_u16(a)); | |
1442 | info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED; | |
1443 | break; | |
1444 | ||
1445 | case OVS_NAT_ATTR_PROTO_MAX: | |
1446 | have_proto_max = true; | |
1447 | info->range.max_proto.all = htons(nla_get_u16(a)); | |
1448 | info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED; | |
1449 | break; | |
1450 | ||
1451 | case OVS_NAT_ATTR_PERSISTENT: | |
1452 | info->range.flags |= NF_NAT_RANGE_PERSISTENT; | |
1453 | break; | |
1454 | ||
1455 | case OVS_NAT_ATTR_PROTO_HASH: | |
1456 | info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM; | |
1457 | break; | |
1458 | ||
1459 | case OVS_NAT_ATTR_PROTO_RANDOM: | |
9f1de150 | 1460 | #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY |
f8f97cdc | 1461 | info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY; |
9f1de150 JR |
1462 | #else |
1463 | info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM; | |
1464 | info->random_fully_compat = true; | |
1465 | #endif | |
f8f97cdc JR |
1466 | break; |
1467 | ||
1468 | default: | |
72ef85ed | 1469 | OVS_NLERR(log, "Unknown nat attribute (%d)", type); |
f8f97cdc JR |
1470 | return -EINVAL; |
1471 | } | |
1472 | } | |
1473 | ||
1474 | if (rem > 0) { | |
72ef85ed | 1475 | OVS_NLERR(log, "NAT attribute has %d unknown bytes", rem); |
f8f97cdc JR |
1476 | return -EINVAL; |
1477 | } | |
1478 | if (!info->nat) { | |
1479 | /* Do not allow flags if no type is given. */ | |
1480 | if (info->range.flags) { | |
1481 | OVS_NLERR(log, | |
e7c57cc3 | 1482 | "NAT flags may be given only when NAT range (SRC or DST) is also specified." |
f8f97cdc JR |
1483 | ); |
1484 | return -EINVAL; | |
1485 | } | |
1486 | info->nat = OVS_CT_NAT; /* NAT existing connections. */ | |
1487 | } else if (!info->commit) { | |
1488 | OVS_NLERR(log, | |
e7c57cc3 | 1489 | "NAT attributes may be specified only when CT COMMIT flag is also specified." |
f8f97cdc JR |
1490 | ); |
1491 | return -EINVAL; | |
1492 | } | |
1493 | /* Allow missing IP_MAX. */ | |
1494 | if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) { | |
1495 | memcpy(&info->range.max_addr, &info->range.min_addr, | |
1496 | sizeof(info->range.max_addr)); | |
1497 | } | |
1498 | /* Allow missing PROTO_MAX. */ | |
1499 | if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED && | |
1500 | !have_proto_max) { | |
1501 | info->range.max_proto.all = info->range.min_proto.all; | |
1502 | } | |
1503 | return 0; | |
1504 | } | |
1505 | #endif | |
1506 | ||
a94ebc39 | 1507 | static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = { |
c05e2094 | 1508 | [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 }, |
b80e259f | 1509 | [OVS_CT_ATTR_FORCE_COMMIT] = { .minlen = 0, .maxlen = 0 }, |
a94ebc39 JS |
1510 | [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16), |
1511 | .maxlen = sizeof(u16) }, | |
372ce973 JS |
1512 | [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark), |
1513 | .maxlen = sizeof(struct md_mark) }, | |
c05e2094 JS |
1514 | [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels), |
1515 | .maxlen = sizeof(struct md_labels) }, | |
11251c17 | 1516 | [OVS_CT_ATTR_HELPER] = { .minlen = 1, |
f8f97cdc JR |
1517 | .maxlen = NF_CT_HELPER_NAME_LEN }, |
1518 | #ifdef CONFIG_NF_NAT_NEEDED | |
1519 | /* NAT length is checked when parsing the nested attributes. */ | |
1520 | [OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX }, | |
1521 | #endif | |
8e83854c JR |
1522 | [OVS_CT_ATTR_EVENTMASK] = { .minlen = sizeof(u32), |
1523 | .maxlen = sizeof(u32) }, | |
a94ebc39 JS |
1524 | }; |
1525 | ||
1526 | static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info, | |
11251c17 | 1527 | const char **helper, bool log) |
a94ebc39 JS |
1528 | { |
1529 | struct nlattr *a; | |
1530 | int rem; | |
1531 | ||
1532 | nla_for_each_nested(a, attr, rem) { | |
1533 | int type = nla_type(a); | |
fb296f42 GR |
1534 | int maxlen; |
1535 | int minlen; | |
a94ebc39 JS |
1536 | |
1537 | if (type > OVS_CT_ATTR_MAX) { | |
1538 | OVS_NLERR(log, | |
1539 | "Unknown conntrack attr (type=%d, max=%d)", | |
1540 | type, OVS_CT_ATTR_MAX); | |
1541 | return -EINVAL; | |
1542 | } | |
fb296f42 GR |
1543 | |
1544 | maxlen = ovs_ct_attr_lens[type].maxlen; | |
1545 | minlen = ovs_ct_attr_lens[type].minlen; | |
a94ebc39 JS |
1546 | if (nla_len(a) < minlen || nla_len(a) > maxlen) { |
1547 | OVS_NLERR(log, | |
1548 | "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)", | |
1549 | type, nla_len(a), maxlen); | |
1550 | return -EINVAL; | |
1551 | } | |
1552 | ||
1553 | switch (type) { | |
b80e259f JR |
1554 | case OVS_CT_ATTR_FORCE_COMMIT: |
1555 | info->force = true; | |
1556 | /* fall through. */ | |
c05e2094 JS |
1557 | case OVS_CT_ATTR_COMMIT: |
1558 | info->commit = true; | |
a94ebc39 JS |
1559 | break; |
1560 | #ifdef CONFIG_NF_CONNTRACK_ZONES | |
1561 | case OVS_CT_ATTR_ZONE: | |
1562 | info->zone.id = nla_get_u16(a); | |
1563 | break; | |
372ce973 JS |
1564 | #endif |
1565 | #ifdef CONFIG_NF_CONNTRACK_MARK | |
1566 | case OVS_CT_ATTR_MARK: { | |
1567 | struct md_mark *mark = nla_data(a); | |
1568 | ||
c05e2094 JS |
1569 | if (!mark->mask) { |
1570 | OVS_NLERR(log, "ct_mark mask cannot be 0"); | |
1571 | return -EINVAL; | |
1572 | } | |
372ce973 JS |
1573 | info->mark = *mark; |
1574 | break; | |
1575 | } | |
038e34ab JS |
1576 | #endif |
1577 | #ifdef CONFIG_NF_CONNTRACK_LABELS | |
c05e2094 JS |
1578 | case OVS_CT_ATTR_LABELS: { |
1579 | struct md_labels *labels = nla_data(a); | |
038e34ab | 1580 | |
c05e2094 JS |
1581 | if (!labels_nonzero(&labels->mask)) { |
1582 | OVS_NLERR(log, "ct_labels mask cannot be 0"); | |
1583 | return -EINVAL; | |
1584 | } | |
1585 | info->labels = *labels; | |
038e34ab JS |
1586 | break; |
1587 | } | |
a94ebc39 | 1588 | #endif |
11251c17 JS |
1589 | case OVS_CT_ATTR_HELPER: |
1590 | *helper = nla_data(a); | |
1591 | if (!memchr(*helper, '\0', nla_len(a))) { | |
1592 | OVS_NLERR(log, "Invalid conntrack helper"); | |
1593 | return -EINVAL; | |
1594 | } | |
1595 | break; | |
f8f97cdc JR |
1596 | #ifdef CONFIG_NF_NAT_NEEDED |
1597 | case OVS_CT_ATTR_NAT: { | |
1598 | int err = parse_nat(a, info, log); | |
1599 | ||
1600 | if (err) | |
1601 | return err; | |
1602 | break; | |
1603 | } | |
1604 | #endif | |
8e83854c JR |
1605 | case OVS_CT_ATTR_EVENTMASK: |
1606 | info->have_eventmask = true; | |
1607 | info->eventmask = nla_get_u32(a); | |
1608 | break; | |
1609 | ||
a94ebc39 JS |
1610 | default: |
1611 | OVS_NLERR(log, "Unknown conntrack attr (%d)", | |
1612 | type); | |
1613 | return -EINVAL; | |
1614 | } | |
1615 | } | |
1616 | ||
39a6542b JR |
1617 | #ifdef CONFIG_NF_CONNTRACK_MARK |
1618 | if (!info->commit && info->mark.mask) { | |
1619 | OVS_NLERR(log, | |
1620 | "Setting conntrack mark requires 'commit' flag."); | |
1621 | return -EINVAL; | |
1622 | } | |
1623 | #endif | |
1624 | #ifdef CONFIG_NF_CONNTRACK_LABELS | |
1625 | if (!info->commit && labels_nonzero(&info->labels.mask)) { | |
1626 | OVS_NLERR(log, | |
1627 | "Setting conntrack labels requires 'commit' flag."); | |
1628 | return -EINVAL; | |
1629 | } | |
1630 | #endif | |
a94ebc39 JS |
1631 | if (rem > 0) { |
1632 | OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem); | |
1633 | return -EINVAL; | |
1634 | } | |
1635 | ||
1636 | return 0; | |
1637 | } | |
1638 | ||
038e34ab | 1639 | bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr) |
a94ebc39 JS |
1640 | { |
1641 | if (attr == OVS_KEY_ATTR_CT_STATE) | |
1642 | return true; | |
1643 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && | |
1644 | attr == OVS_KEY_ATTR_CT_ZONE) | |
1645 | return true; | |
372ce973 JS |
1646 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && |
1647 | attr == OVS_KEY_ATTR_CT_MARK) | |
1648 | return true; | |
038e34ab | 1649 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && |
c05e2094 | 1650 | attr == OVS_KEY_ATTR_CT_LABELS) { |
038e34ab JS |
1651 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
1652 | ||
1653 | return ovs_net->xt_label; | |
1654 | } | |
a94ebc39 JS |
1655 | |
1656 | return false; | |
1657 | } | |
1658 | ||
1659 | int ovs_ct_copy_action(struct net *net, const struct nlattr *attr, | |
1660 | const struct sw_flow_key *key, | |
1661 | struct sw_flow_actions **sfa, bool log) | |
1662 | { | |
1663 | struct ovs_conntrack_info ct_info; | |
11251c17 | 1664 | const char *helper = NULL; |
a94ebc39 JS |
1665 | u16 family; |
1666 | int err; | |
1667 | ||
1668 | family = key_to_nfproto(key); | |
1669 | if (family == NFPROTO_UNSPEC) { | |
1670 | OVS_NLERR(log, "ct family unspecified"); | |
1671 | return -EINVAL; | |
1672 | } | |
1673 | ||
1674 | memset(&ct_info, 0, sizeof(ct_info)); | |
1675 | ct_info.family = family; | |
1676 | ||
1677 | nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID, | |
1678 | NF_CT_DEFAULT_ZONE_DIR, 0); | |
1679 | ||
11251c17 | 1680 | err = parse_ct(attr, &ct_info, &helper, log); |
a94ebc39 JS |
1681 | if (err) |
1682 | return err; | |
1683 | ||
1684 | /* Set up template for tracking connections in specific zones. */ | |
1685 | ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL); | |
1686 | if (!ct_info.ct) { | |
1687 | OVS_NLERR(log, "Failed to allocate conntrack template"); | |
1688 | return -ENOMEM; | |
1689 | } | |
a3a68d63 JS |
1690 | |
1691 | __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status); | |
1692 | nf_conntrack_get(&ct_info.ct->ct_general); | |
1693 | ||
11251c17 JS |
1694 | if (helper) { |
1695 | err = ovs_ct_add_helper(&ct_info, helper, key, log); | |
1696 | if (err) | |
1697 | goto err_free_ct; | |
1698 | } | |
a94ebc39 JS |
1699 | |
1700 | err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info, | |
1701 | sizeof(ct_info), log); | |
1702 | if (err) | |
1703 | goto err_free_ct; | |
1704 | ||
a94ebc39 JS |
1705 | return 0; |
1706 | err_free_ct: | |
11251c17 | 1707 | __ovs_ct_free_action(&ct_info); |
a94ebc39 JS |
1708 | return err; |
1709 | } | |
1710 | ||
f8f97cdc JR |
1711 | #ifdef CONFIG_NF_NAT_NEEDED |
1712 | static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info, | |
1713 | struct sk_buff *skb) | |
1714 | { | |
1715 | struct nlattr *start; | |
1716 | ||
1717 | start = nla_nest_start(skb, OVS_CT_ATTR_NAT); | |
1718 | if (!start) | |
1719 | return false; | |
1720 | ||
1721 | if (info->nat & OVS_CT_SRC_NAT) { | |
1722 | if (nla_put_flag(skb, OVS_NAT_ATTR_SRC)) | |
1723 | return false; | |
1724 | } else if (info->nat & OVS_CT_DST_NAT) { | |
1725 | if (nla_put_flag(skb, OVS_NAT_ATTR_DST)) | |
1726 | return false; | |
1727 | } else { | |
1728 | goto out; | |
1729 | } | |
1730 | ||
1731 | if (info->range.flags & NF_NAT_RANGE_MAP_IPS) { | |
90b01477 AB |
1732 | if (IS_ENABLED(CONFIG_NF_NAT_IPV4) && |
1733 | info->family == NFPROTO_IPV4) { | |
f8f97cdc JR |
1734 | if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN, |
1735 | info->range.min_addr.ip) || | |
1736 | (info->range.max_addr.ip | |
1737 | != info->range.min_addr.ip && | |
1738 | (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX, | |
1739 | info->range.max_addr.ip)))) | |
1740 | return false; | |
90b01477 AB |
1741 | } else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) && |
1742 | info->family == NFPROTO_IPV6) { | |
f8f97cdc JR |
1743 | if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN, |
1744 | &info->range.min_addr.in6) || | |
1745 | (memcmp(&info->range.max_addr.in6, | |
1746 | &info->range.min_addr.in6, | |
1747 | sizeof(info->range.max_addr.in6)) && | |
1748 | (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX, | |
1749 | &info->range.max_addr.in6)))) | |
1750 | return false; | |
f8f97cdc JR |
1751 | } else { |
1752 | return false; | |
1753 | } | |
1754 | } | |
1755 | if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED && | |
1756 | (nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN, | |
1757 | ntohs(info->range.min_proto.all)) || | |
1758 | (info->range.max_proto.all != info->range.min_proto.all && | |
1759 | nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX, | |
1760 | ntohs(info->range.max_proto.all))))) | |
1761 | return false; | |
1762 | ||
1763 | if (info->range.flags & NF_NAT_RANGE_PERSISTENT && | |
1764 | nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT)) | |
1765 | return false; | |
1766 | if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM && | |
9f1de150 JR |
1767 | nla_put_flag(skb, info->random_fully_compat |
1768 | ? OVS_NAT_ATTR_PROTO_RANDOM | |
1769 | : OVS_NAT_ATTR_PROTO_HASH)) | |
f8f97cdc | 1770 | return false; |
9f1de150 | 1771 | #ifdef NF_NAT_RANGE_PROTO_RANDOM_FULLY |
f8f97cdc JR |
1772 | if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY && |
1773 | nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM)) | |
1774 | return false; | |
9f1de150 | 1775 | #endif |
f8f97cdc JR |
1776 | out: |
1777 | nla_nest_end(skb, start); | |
1778 | ||
1779 | return true; | |
1780 | } | |
1781 | #endif | |
1782 | ||
a94ebc39 JS |
1783 | int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info, |
1784 | struct sk_buff *skb) | |
1785 | { | |
1786 | struct nlattr *start; | |
1787 | ||
1788 | start = nla_nest_start(skb, OVS_ACTION_ATTR_CT); | |
1789 | if (!start) | |
1790 | return -EMSGSIZE; | |
1791 | ||
b80e259f JR |
1792 | if (ct_info->commit && nla_put_flag(skb, ct_info->force |
1793 | ? OVS_CT_ATTR_FORCE_COMMIT | |
1794 | : OVS_CT_ATTR_COMMIT)) | |
a94ebc39 JS |
1795 | return -EMSGSIZE; |
1796 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && | |
1797 | nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id)) | |
1798 | return -EMSGSIZE; | |
c05e2094 | 1799 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask && |
372ce973 JS |
1800 | nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark), |
1801 | &ct_info->mark)) | |
1802 | return -EMSGSIZE; | |
038e34ab | 1803 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && |
c05e2094 JS |
1804 | labels_nonzero(&ct_info->labels.mask) && |
1805 | nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels), | |
1806 | &ct_info->labels)) | |
038e34ab | 1807 | return -EMSGSIZE; |
11251c17 JS |
1808 | if (ct_info->helper) { |
1809 | if (nla_put_string(skb, OVS_CT_ATTR_HELPER, | |
1810 | ct_info->helper->name)) | |
1811 | return -EMSGSIZE; | |
1812 | } | |
8e83854c JR |
1813 | if (ct_info->have_eventmask && |
1814 | nla_put_u32(skb, OVS_CT_ATTR_EVENTMASK, ct_info->eventmask)) | |
1815 | return -EMSGSIZE; | |
1816 | ||
f8f97cdc JR |
1817 | #ifdef CONFIG_NF_NAT_NEEDED |
1818 | if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb)) | |
1819 | return -EMSGSIZE; | |
1820 | #endif | |
a94ebc39 JS |
1821 | nla_nest_end(skb, start); |
1822 | ||
1823 | return 0; | |
1824 | } | |
1825 | ||
1826 | void ovs_ct_free_action(const struct nlattr *a) | |
1827 | { | |
1828 | struct ovs_conntrack_info *ct_info = nla_data(a); | |
1829 | ||
11251c17 JS |
1830 | __ovs_ct_free_action(ct_info); |
1831 | } | |
1832 | ||
1833 | static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info) | |
1834 | { | |
1835 | if (ct_info->helper) | |
ac8e3c6d | 1836 | nf_conntrack_helper_put(ct_info->helper); |
a94ebc39 JS |
1837 | if (ct_info->ct) |
1838 | nf_ct_tmpl_free(ct_info->ct); | |
1839 | } | |
1840 | ||
4f7e690e YHW |
1841 | #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) |
1842 | static int ovs_ct_limit_init(struct net *net, struct ovs_net *ovs_net) | |
1843 | { | |
1844 | int i, err; | |
1845 | ||
1846 | ovs_net->ct_limit_info = kmalloc(sizeof(*ovs_net->ct_limit_info), | |
1847 | GFP_KERNEL); | |
1848 | if (!ovs_net->ct_limit_info) | |
1849 | return -ENOMEM; | |
1850 | ||
1851 | ovs_net->ct_limit_info->default_limit = OVS_CT_LIMIT_DEFAULT; | |
1852 | ovs_net->ct_limit_info->limits = | |
1853 | kmalloc_array(CT_LIMIT_HASH_BUCKETS, sizeof(struct hlist_head), | |
1854 | GFP_KERNEL); | |
1855 | if (!ovs_net->ct_limit_info->limits) { | |
1856 | kfree(ovs_net->ct_limit_info); | |
1857 | return -ENOMEM; | |
1858 | } | |
1859 | ||
1860 | for (i = 0; i < CT_LIMIT_HASH_BUCKETS; i++) | |
1861 | INIT_HLIST_HEAD(&ovs_net->ct_limit_info->limits[i]); | |
1862 | ||
1863 | ovs_net->ct_limit_info->data = | |
1864 | nf_conncount_init(net, NFPROTO_INET, sizeof(u32)); | |
1865 | ||
1866 | if (IS_ERR(ovs_net->ct_limit_info->data)) { | |
1867 | err = PTR_ERR(ovs_net->ct_limit_info->data); | |
1868 | kfree(ovs_net->ct_limit_info->limits); | |
1869 | kfree(ovs_net->ct_limit_info); | |
1870 | pr_err("openvswitch: failed to init nf_conncount %d\n", err); | |
1871 | return err; | |
1872 | } | |
1873 | return 0; | |
1874 | } | |
1875 | ||
1876 | static void ovs_ct_limit_exit(struct net *net, struct ovs_net *ovs_net) | |
1877 | { | |
1878 | const struct ovs_ct_limit_info *info = ovs_net->ct_limit_info; | |
1879 | int i; | |
1880 | ||
1881 | nf_conncount_destroy(net, NFPROTO_INET, info->data); | |
1882 | for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) { | |
1883 | struct hlist_head *head = &info->limits[i]; | |
1884 | struct ovs_ct_limit *ct_limit; | |
1885 | ||
1886 | hlist_for_each_entry_rcu(ct_limit, head, hlist_node) | |
1887 | kfree_rcu(ct_limit, rcu); | |
1888 | } | |
1889 | kfree(ovs_net->ct_limit_info->limits); | |
1890 | kfree(ovs_net->ct_limit_info); | |
1891 | } | |
1892 | ||
1893 | static struct sk_buff * | |
1894 | ovs_ct_limit_cmd_reply_start(struct genl_info *info, u8 cmd, | |
1895 | struct ovs_header **ovs_reply_header) | |
1896 | { | |
1897 | struct ovs_header *ovs_header = info->userhdr; | |
1898 | struct sk_buff *skb; | |
1899 | ||
1900 | skb = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
1901 | if (!skb) | |
1902 | return ERR_PTR(-ENOMEM); | |
1903 | ||
1904 | *ovs_reply_header = genlmsg_put(skb, info->snd_portid, | |
1905 | info->snd_seq, | |
1906 | &dp_ct_limit_genl_family, 0, cmd); | |
1907 | ||
1908 | if (!*ovs_reply_header) { | |
1909 | nlmsg_free(skb); | |
1910 | return ERR_PTR(-EMSGSIZE); | |
1911 | } | |
1912 | (*ovs_reply_header)->dp_ifindex = ovs_header->dp_ifindex; | |
1913 | ||
1914 | return skb; | |
1915 | } | |
1916 | ||
1917 | static bool check_zone_id(int zone_id, u16 *pzone) | |
1918 | { | |
1919 | if (zone_id >= 0 && zone_id <= 65535) { | |
1920 | *pzone = (u16)zone_id; | |
1921 | return true; | |
1922 | } | |
1923 | return false; | |
1924 | } | |
1925 | ||
1926 | static int ovs_ct_limit_set_zone_limit(struct nlattr *nla_zone_limit, | |
1927 | struct ovs_ct_limit_info *info) | |
1928 | { | |
1929 | struct ovs_zone_limit *zone_limit; | |
1930 | int rem; | |
1931 | u16 zone; | |
1932 | ||
1933 | rem = NLA_ALIGN(nla_len(nla_zone_limit)); | |
1934 | zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit); | |
1935 | ||
1936 | while (rem >= sizeof(*zone_limit)) { | |
1937 | if (unlikely(zone_limit->zone_id == | |
1938 | OVS_ZONE_LIMIT_DEFAULT_ZONE)) { | |
1939 | ovs_lock(); | |
1940 | info->default_limit = zone_limit->limit; | |
1941 | ovs_unlock(); | |
1942 | } else if (unlikely(!check_zone_id( | |
1943 | zone_limit->zone_id, &zone))) { | |
1944 | OVS_NLERR(true, "zone id is out of range"); | |
1945 | } else { | |
1946 | struct ovs_ct_limit *ct_limit; | |
1947 | ||
1948 | ct_limit = kmalloc(sizeof(*ct_limit), GFP_KERNEL); | |
1949 | if (!ct_limit) | |
1950 | return -ENOMEM; | |
1951 | ||
1952 | ct_limit->zone = zone; | |
1953 | ct_limit->limit = zone_limit->limit; | |
1954 | ||
1955 | ovs_lock(); | |
1956 | ct_limit_set(info, ct_limit); | |
1957 | ovs_unlock(); | |
1958 | } | |
1959 | rem -= NLA_ALIGN(sizeof(*zone_limit)); | |
1960 | zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit + | |
1961 | NLA_ALIGN(sizeof(*zone_limit))); | |
1962 | } | |
1963 | ||
1964 | if (rem) | |
1965 | OVS_NLERR(true, "set zone limit has %d unknown bytes", rem); | |
1966 | ||
1967 | return 0; | |
1968 | } | |
1969 | ||
1970 | static int ovs_ct_limit_del_zone_limit(struct nlattr *nla_zone_limit, | |
1971 | struct ovs_ct_limit_info *info) | |
1972 | { | |
1973 | struct ovs_zone_limit *zone_limit; | |
1974 | int rem; | |
1975 | u16 zone; | |
1976 | ||
1977 | rem = NLA_ALIGN(nla_len(nla_zone_limit)); | |
1978 | zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit); | |
1979 | ||
1980 | while (rem >= sizeof(*zone_limit)) { | |
1981 | if (unlikely(zone_limit->zone_id == | |
1982 | OVS_ZONE_LIMIT_DEFAULT_ZONE)) { | |
1983 | ovs_lock(); | |
1984 | info->default_limit = OVS_CT_LIMIT_DEFAULT; | |
1985 | ovs_unlock(); | |
1986 | } else if (unlikely(!check_zone_id( | |
1987 | zone_limit->zone_id, &zone))) { | |
1988 | OVS_NLERR(true, "zone id is out of range"); | |
1989 | } else { | |
1990 | ovs_lock(); | |
1991 | ct_limit_del(info, zone); | |
1992 | ovs_unlock(); | |
1993 | } | |
1994 | rem -= NLA_ALIGN(sizeof(*zone_limit)); | |
1995 | zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit + | |
1996 | NLA_ALIGN(sizeof(*zone_limit))); | |
1997 | } | |
1998 | ||
1999 | if (rem) | |
2000 | OVS_NLERR(true, "del zone limit has %d unknown bytes", rem); | |
2001 | ||
2002 | return 0; | |
2003 | } | |
2004 | ||
2005 | static int ovs_ct_limit_get_default_limit(struct ovs_ct_limit_info *info, | |
2006 | struct sk_buff *reply) | |
2007 | { | |
2008 | struct ovs_zone_limit zone_limit; | |
2009 | int err; | |
2010 | ||
2011 | zone_limit.zone_id = OVS_ZONE_LIMIT_DEFAULT_ZONE; | |
2012 | zone_limit.limit = info->default_limit; | |
2013 | err = nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit); | |
2014 | if (err) | |
2015 | return err; | |
2016 | ||
2017 | return 0; | |
2018 | } | |
2019 | ||
2020 | static int __ovs_ct_limit_get_zone_limit(struct net *net, | |
2021 | struct nf_conncount_data *data, | |
2022 | u16 zone_id, u32 limit, | |
2023 | struct sk_buff *reply) | |
2024 | { | |
2025 | struct nf_conntrack_zone ct_zone; | |
2026 | struct ovs_zone_limit zone_limit; | |
2027 | u32 conncount_key = zone_id; | |
2028 | ||
2029 | zone_limit.zone_id = zone_id; | |
2030 | zone_limit.limit = limit; | |
2031 | nf_ct_zone_init(&ct_zone, zone_id, NF_CT_DEFAULT_ZONE_DIR, 0); | |
2032 | ||
2033 | zone_limit.count = nf_conncount_count(net, data, &conncount_key, NULL, | |
2034 | &ct_zone); | |
2035 | return nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit); | |
2036 | } | |
2037 | ||
2038 | static int ovs_ct_limit_get_zone_limit(struct net *net, | |
2039 | struct nlattr *nla_zone_limit, | |
2040 | struct ovs_ct_limit_info *info, | |
2041 | struct sk_buff *reply) | |
2042 | { | |
2043 | struct ovs_zone_limit *zone_limit; | |
2044 | int rem, err; | |
2045 | u32 limit; | |
2046 | u16 zone; | |
2047 | ||
2048 | rem = NLA_ALIGN(nla_len(nla_zone_limit)); | |
2049 | zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit); | |
2050 | ||
2051 | while (rem >= sizeof(*zone_limit)) { | |
2052 | if (unlikely(zone_limit->zone_id == | |
2053 | OVS_ZONE_LIMIT_DEFAULT_ZONE)) { | |
2054 | err = ovs_ct_limit_get_default_limit(info, reply); | |
2055 | if (err) | |
2056 | return err; | |
2057 | } else if (unlikely(!check_zone_id(zone_limit->zone_id, | |
2058 | &zone))) { | |
2059 | OVS_NLERR(true, "zone id is out of range"); | |
2060 | } else { | |
2061 | rcu_read_lock(); | |
2062 | limit = ct_limit_get(info, zone); | |
2063 | rcu_read_unlock(); | |
2064 | ||
2065 | err = __ovs_ct_limit_get_zone_limit( | |
2066 | net, info->data, zone, limit, reply); | |
2067 | if (err) | |
2068 | return err; | |
2069 | } | |
2070 | rem -= NLA_ALIGN(sizeof(*zone_limit)); | |
2071 | zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit + | |
2072 | NLA_ALIGN(sizeof(*zone_limit))); | |
2073 | } | |
2074 | ||
2075 | if (rem) | |
2076 | OVS_NLERR(true, "get zone limit has %d unknown bytes", rem); | |
2077 | ||
2078 | return 0; | |
2079 | } | |
2080 | ||
2081 | static int ovs_ct_limit_get_all_zone_limit(struct net *net, | |
2082 | struct ovs_ct_limit_info *info, | |
2083 | struct sk_buff *reply) | |
2084 | { | |
2085 | struct ovs_ct_limit *ct_limit; | |
2086 | struct hlist_head *head; | |
2087 | int i, err = 0; | |
2088 | ||
2089 | err = ovs_ct_limit_get_default_limit(info, reply); | |
2090 | if (err) | |
2091 | return err; | |
2092 | ||
2093 | rcu_read_lock(); | |
2094 | for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) { | |
2095 | head = &info->limits[i]; | |
2096 | hlist_for_each_entry_rcu(ct_limit, head, hlist_node) { | |
2097 | err = __ovs_ct_limit_get_zone_limit(net, info->data, | |
2098 | ct_limit->zone, ct_limit->limit, reply); | |
2099 | if (err) | |
2100 | goto exit_err; | |
2101 | } | |
2102 | } | |
2103 | ||
2104 | exit_err: | |
2105 | rcu_read_unlock(); | |
2106 | return err; | |
2107 | } | |
2108 | ||
2109 | static int ovs_ct_limit_cmd_set(struct sk_buff *skb, struct genl_info *info) | |
2110 | { | |
2111 | struct nlattr **a = info->attrs; | |
2112 | struct sk_buff *reply; | |
2113 | struct ovs_header *ovs_reply_header; | |
2114 | struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id); | |
2115 | struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info; | |
2116 | int err; | |
2117 | ||
2118 | reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_SET, | |
2119 | &ovs_reply_header); | |
2120 | if (IS_ERR(reply)) | |
2121 | return PTR_ERR(reply); | |
2122 | ||
2123 | if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) { | |
2124 | err = -EINVAL; | |
2125 | goto exit_err; | |
2126 | } | |
2127 | ||
2128 | err = ovs_ct_limit_set_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT], | |
2129 | ct_limit_info); | |
2130 | if (err) | |
2131 | goto exit_err; | |
2132 | ||
2133 | static_branch_enable(&ovs_ct_limit_enabled); | |
2134 | ||
2135 | genlmsg_end(reply, ovs_reply_header); | |
2136 | return genlmsg_reply(reply, info); | |
2137 | ||
2138 | exit_err: | |
2139 | nlmsg_free(reply); | |
2140 | return err; | |
2141 | } | |
2142 | ||
2143 | static int ovs_ct_limit_cmd_del(struct sk_buff *skb, struct genl_info *info) | |
2144 | { | |
2145 | struct nlattr **a = info->attrs; | |
2146 | struct sk_buff *reply; | |
2147 | struct ovs_header *ovs_reply_header; | |
2148 | struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id); | |
2149 | struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info; | |
2150 | int err; | |
2151 | ||
2152 | reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_DEL, | |
2153 | &ovs_reply_header); | |
2154 | if (IS_ERR(reply)) | |
2155 | return PTR_ERR(reply); | |
2156 | ||
2157 | if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) { | |
2158 | err = -EINVAL; | |
2159 | goto exit_err; | |
2160 | } | |
2161 | ||
2162 | err = ovs_ct_limit_del_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT], | |
2163 | ct_limit_info); | |
2164 | if (err) | |
2165 | goto exit_err; | |
2166 | ||
2167 | genlmsg_end(reply, ovs_reply_header); | |
2168 | return genlmsg_reply(reply, info); | |
2169 | ||
2170 | exit_err: | |
2171 | nlmsg_free(reply); | |
2172 | return err; | |
2173 | } | |
2174 | ||
2175 | static int ovs_ct_limit_cmd_get(struct sk_buff *skb, struct genl_info *info) | |
2176 | { | |
2177 | struct nlattr **a = info->attrs; | |
2178 | struct nlattr *nla_reply; | |
2179 | struct sk_buff *reply; | |
2180 | struct ovs_header *ovs_reply_header; | |
2181 | struct net *net = sock_net(skb->sk); | |
2182 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); | |
2183 | struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info; | |
2184 | int err; | |
2185 | ||
2186 | reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_GET, | |
2187 | &ovs_reply_header); | |
2188 | if (IS_ERR(reply)) | |
2189 | return PTR_ERR(reply); | |
2190 | ||
2191 | nla_reply = nla_nest_start(reply, OVS_CT_LIMIT_ATTR_ZONE_LIMIT); | |
2192 | ||
2193 | if (a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) { | |
2194 | err = ovs_ct_limit_get_zone_limit( | |
2195 | net, a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT], ct_limit_info, | |
2196 | reply); | |
2197 | if (err) | |
2198 | goto exit_err; | |
2199 | } else { | |
2200 | err = ovs_ct_limit_get_all_zone_limit(net, ct_limit_info, | |
2201 | reply); | |
2202 | if (err) | |
2203 | goto exit_err; | |
2204 | } | |
2205 | ||
2206 | nla_nest_end(reply, nla_reply); | |
2207 | genlmsg_end(reply, ovs_reply_header); | |
2208 | return genlmsg_reply(reply, info); | |
2209 | ||
2210 | exit_err: | |
2211 | nlmsg_free(reply); | |
2212 | return err; | |
2213 | } | |
2214 | ||
2215 | static struct genl_ops ct_limit_genl_ops[] = { | |
2216 | { .cmd = OVS_CT_LIMIT_CMD_SET, | |
2217 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN | |
2218 | * privilege. */ | |
2219 | .policy = ct_limit_policy, | |
2220 | .doit = ovs_ct_limit_cmd_set, | |
2221 | }, | |
2222 | { .cmd = OVS_CT_LIMIT_CMD_DEL, | |
2223 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN | |
2224 | * privilege. */ | |
2225 | .policy = ct_limit_policy, | |
2226 | .doit = ovs_ct_limit_cmd_del, | |
2227 | }, | |
2228 | { .cmd = OVS_CT_LIMIT_CMD_GET, | |
2229 | .flags = 0, /* OK for unprivileged users. */ | |
2230 | .policy = ct_limit_policy, | |
2231 | .doit = ovs_ct_limit_cmd_get, | |
2232 | }, | |
2233 | }; | |
2234 | ||
2235 | static const struct genl_multicast_group ovs_ct_limit_multicast_group = { | |
2236 | .name = OVS_CT_LIMIT_MCGROUP, | |
2237 | }; | |
2238 | ||
2239 | struct genl_family dp_ct_limit_genl_family __ro_after_init = { | |
2240 | .hdrsize = sizeof(struct ovs_header), | |
2241 | .name = OVS_CT_LIMIT_FAMILY, | |
2242 | .version = OVS_CT_LIMIT_VERSION, | |
2243 | .maxattr = OVS_CT_LIMIT_ATTR_MAX, | |
2244 | .netnsok = true, | |
2245 | .parallel_ops = true, | |
2246 | .ops = ct_limit_genl_ops, | |
2247 | .n_ops = ARRAY_SIZE(ct_limit_genl_ops), | |
2248 | .mcgrps = &ovs_ct_limit_multicast_group, | |
2249 | .n_mcgrps = 1, | |
2250 | .module = THIS_MODULE, | |
2251 | }; | |
2252 | #endif | |
2253 | ||
2254 | int ovs_ct_init(struct net *net) | |
038e34ab | 2255 | { |
c05e2094 | 2256 | unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE; |
038e34ab JS |
2257 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
2258 | ||
7f2ab8cd | 2259 | if (nf_connlabels_get(net, n_bits - 1)) { |
038e34ab JS |
2260 | ovs_net->xt_label = false; |
2261 | OVS_NLERR(true, "Failed to set connlabel length"); | |
2262 | } else { | |
2263 | ovs_net->xt_label = true; | |
2264 | } | |
4f7e690e YHW |
2265 | |
2266 | #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) | |
2267 | return ovs_ct_limit_init(net, ovs_net); | |
2268 | #else | |
2269 | return 0; | |
2270 | #endif | |
038e34ab JS |
2271 | } |
2272 | ||
2273 | void ovs_ct_exit(struct net *net) | |
2274 | { | |
2275 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); | |
2276 | ||
4f7e690e YHW |
2277 | #if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) |
2278 | ovs_ct_limit_exit(net, ovs_net); | |
2279 | #endif | |
2280 | ||
038e34ab JS |
2281 | if (ovs_net->xt_label) |
2282 | nf_connlabels_put(net); | |
2283 | } | |
2284 | ||
8063e095 | 2285 | #endif /* CONFIG_NF_CONNTRACK */ |