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