Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 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> |
| 16 | #include <net/ip.h> |
| 17 | #include <net/netfilter/nf_conntrack_core.h> |
Joe Stringer | cae3a26 | 2015-08-26 11:31:53 -0700 | [diff] [blame] | 18 | #include <net/netfilter/nf_conntrack_helper.h> |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 19 | #include <net/netfilter/nf_conntrack_labels.h> |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 20 | #include <net/netfilter/nf_conntrack_zones.h> |
| 21 | #include <net/netfilter/ipv6/nf_defrag_ipv6.h> |
| 22 | |
| 23 | #include "datapath.h" |
| 24 | #include "conntrack.h" |
| 25 | #include "flow.h" |
| 26 | #include "flow_netlink.h" |
| 27 | |
| 28 | struct ovs_ct_len_tbl { |
| 29 | size_t maxlen; |
| 30 | size_t minlen; |
| 31 | }; |
| 32 | |
Joe Stringer | 182e304 | 2015-08-26 11:31:49 -0700 | [diff] [blame] | 33 | /* Metadata mark for masked write to conntrack mark */ |
| 34 | struct md_mark { |
| 35 | u32 value; |
| 36 | u32 mask; |
| 37 | }; |
| 38 | |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 39 | /* Metadata label for masked write to conntrack label. */ |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 40 | struct md_labels { |
| 41 | struct ovs_key_ct_labels value; |
| 42 | struct ovs_key_ct_labels mask; |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 45 | /* Conntrack action context for execution. */ |
| 46 | struct ovs_conntrack_info { |
Joe Stringer | cae3a26 | 2015-08-26 11:31:53 -0700 | [diff] [blame] | 47 | struct nf_conntrack_helper *helper; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 48 | struct nf_conntrack_zone zone; |
| 49 | struct nf_conn *ct; |
Joe Stringer | ab38a7b | 2015-10-06 11:00:01 -0700 | [diff] [blame] | 50 | u8 commit : 1; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 51 | u16 family; |
Joe Stringer | 182e304 | 2015-08-26 11:31:49 -0700 | [diff] [blame] | 52 | struct md_mark mark; |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 53 | struct md_labels labels; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
Joe Stringer | 2f3ab9f | 2015-12-09 14:07:39 -0800 | [diff] [blame] | 56 | static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info); |
| 57 | |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 58 | static u16 key_to_nfproto(const struct sw_flow_key *key) |
| 59 | { |
| 60 | switch (ntohs(key->eth.type)) { |
| 61 | case ETH_P_IP: |
| 62 | return NFPROTO_IPV4; |
| 63 | case ETH_P_IPV6: |
| 64 | return NFPROTO_IPV6; |
| 65 | default: |
| 66 | return NFPROTO_UNSPEC; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /* Map SKB connection state into the values used by flow definition. */ |
| 71 | static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo) |
| 72 | { |
| 73 | u8 ct_state = OVS_CS_F_TRACKED; |
| 74 | |
| 75 | switch (ctinfo) { |
| 76 | case IP_CT_ESTABLISHED_REPLY: |
| 77 | case IP_CT_RELATED_REPLY: |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 78 | ct_state |= OVS_CS_F_REPLY_DIR; |
| 79 | break; |
| 80 | default: |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | switch (ctinfo) { |
| 85 | case IP_CT_ESTABLISHED: |
| 86 | case IP_CT_ESTABLISHED_REPLY: |
| 87 | ct_state |= OVS_CS_F_ESTABLISHED; |
| 88 | break; |
| 89 | case IP_CT_RELATED: |
| 90 | case IP_CT_RELATED_REPLY: |
| 91 | ct_state |= OVS_CS_F_RELATED; |
| 92 | break; |
| 93 | case IP_CT_NEW: |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 94 | ct_state |= OVS_CS_F_NEW; |
| 95 | break; |
| 96 | default: |
| 97 | break; |
| 98 | } |
| 99 | |
| 100 | return ct_state; |
| 101 | } |
| 102 | |
Joe Stringer | 0d5cdef | 2015-08-28 19:22:11 -0700 | [diff] [blame] | 103 | static u32 ovs_ct_get_mark(const struct nf_conn *ct) |
| 104 | { |
| 105 | #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) |
| 106 | return ct ? ct->mark : 0; |
| 107 | #else |
| 108 | return 0; |
| 109 | #endif |
| 110 | } |
| 111 | |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 112 | static void ovs_ct_get_labels(const struct nf_conn *ct, |
| 113 | struct ovs_key_ct_labels *labels) |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 114 | { |
| 115 | struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL; |
| 116 | |
| 117 | if (cl) { |
| 118 | size_t len = cl->words * sizeof(long); |
| 119 | |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 120 | if (len > OVS_CT_LABELS_LEN) |
| 121 | len = OVS_CT_LABELS_LEN; |
| 122 | else if (len < OVS_CT_LABELS_LEN) |
| 123 | memset(labels, 0, OVS_CT_LABELS_LEN); |
| 124 | memcpy(labels, cl->bits, len); |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 125 | } else { |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 126 | memset(labels, 0, OVS_CT_LABELS_LEN); |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 130 | static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state, |
Joe Stringer | 182e304 | 2015-08-26 11:31:49 -0700 | [diff] [blame] | 131 | const struct nf_conntrack_zone *zone, |
| 132 | const struct nf_conn *ct) |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 133 | { |
| 134 | key->ct.state = state; |
| 135 | key->ct.zone = zone->id; |
Joe Stringer | 0d5cdef | 2015-08-28 19:22:11 -0700 | [diff] [blame] | 136 | key->ct.mark = ovs_ct_get_mark(ct); |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 137 | ovs_ct_get_labels(ct, &key->ct.labels); |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* Update 'key' based on skb->nfct. If 'post_ct' is true, then OVS has |
| 141 | * previously sent the packet to conntrack via the ct action. |
| 142 | */ |
| 143 | static void ovs_ct_update_key(const struct sk_buff *skb, |
Joe Stringer | d110986 | 2015-12-09 14:07:40 -0800 | [diff] [blame] | 144 | const struct ovs_conntrack_info *info, |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 145 | struct sw_flow_key *key, bool post_ct) |
| 146 | { |
| 147 | const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt; |
| 148 | enum ip_conntrack_info ctinfo; |
| 149 | struct nf_conn *ct; |
| 150 | u8 state = 0; |
| 151 | |
| 152 | ct = nf_ct_get(skb, &ctinfo); |
| 153 | if (ct) { |
| 154 | state = ovs_ct_get_state(ctinfo); |
Jarno Rajahalme | 9f13ded | 2016-03-10 10:54:18 -0800 | [diff] [blame] | 155 | /* All unconfirmed entries are NEW connections. */ |
Joe Stringer | 4f0909e | 2015-10-19 19:18:59 -0700 | [diff] [blame] | 156 | if (!nf_ct_is_confirmed(ct)) |
| 157 | state |= OVS_CS_F_NEW; |
Jarno Rajahalme | 9f13ded | 2016-03-10 10:54:18 -0800 | [diff] [blame] | 158 | /* OVS persists the related flag for the duration of the |
| 159 | * connection. |
| 160 | */ |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 161 | if (ct->master) |
| 162 | state |= OVS_CS_F_RELATED; |
| 163 | zone = nf_ct_zone(ct); |
| 164 | } else if (post_ct) { |
| 165 | state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID; |
Joe Stringer | d110986 | 2015-12-09 14:07:40 -0800 | [diff] [blame] | 166 | if (info) |
| 167 | zone = &info->zone; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 168 | } |
Joe Stringer | 182e304 | 2015-08-26 11:31:49 -0700 | [diff] [blame] | 169 | __ovs_ct_update_key(key, state, zone, ct); |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Jarno Rajahalme | 9f13ded | 2016-03-10 10:54:18 -0800 | [diff] [blame] | 172 | /* This is called to initialize CT key fields possibly coming in from the local |
| 173 | * stack. |
| 174 | */ |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 175 | void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key) |
| 176 | { |
Joe Stringer | d110986 | 2015-12-09 14:07:40 -0800 | [diff] [blame] | 177 | ovs_ct_update_key(skb, NULL, key, false); |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | int ovs_ct_put_key(const struct sw_flow_key *key, struct sk_buff *skb) |
| 181 | { |
Joe Stringer | fbccce5 | 2015-10-06 11:00:00 -0700 | [diff] [blame] | 182 | if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, key->ct.state)) |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 183 | return -EMSGSIZE; |
| 184 | |
| 185 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && |
| 186 | nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, key->ct.zone)) |
| 187 | return -EMSGSIZE; |
| 188 | |
Joe Stringer | 182e304 | 2015-08-26 11:31:49 -0700 | [diff] [blame] | 189 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && |
| 190 | nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, key->ct.mark)) |
| 191 | return -EMSGSIZE; |
| 192 | |
Valentin Rothberg | 9723e6a | 2015-08-28 10:39:56 +0200 | [diff] [blame] | 193 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 194 | nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(key->ct.labels), |
| 195 | &key->ct.labels)) |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 196 | return -EMSGSIZE; |
| 197 | |
Joe Stringer | 182e304 | 2015-08-26 11:31:49 -0700 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static int ovs_ct_set_mark(struct sk_buff *skb, struct sw_flow_key *key, |
| 202 | u32 ct_mark, u32 mask) |
| 203 | { |
Joe Stringer | 0d5cdef | 2015-08-28 19:22:11 -0700 | [diff] [blame] | 204 | #if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) |
Joe Stringer | 182e304 | 2015-08-26 11:31:49 -0700 | [diff] [blame] | 205 | enum ip_conntrack_info ctinfo; |
| 206 | struct nf_conn *ct; |
| 207 | u32 new_mark; |
| 208 | |
Joe Stringer | 182e304 | 2015-08-26 11:31:49 -0700 | [diff] [blame] | 209 | /* The connection could be invalid, in which case set_mark is no-op. */ |
| 210 | ct = nf_ct_get(skb, &ctinfo); |
| 211 | if (!ct) |
| 212 | return 0; |
| 213 | |
| 214 | new_mark = ct_mark | (ct->mark & ~(mask)); |
| 215 | if (ct->mark != new_mark) { |
| 216 | ct->mark = new_mark; |
| 217 | nf_conntrack_event_cache(IPCT_MARK, ct); |
| 218 | key->ct.mark = new_mark; |
| 219 | } |
| 220 | |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 221 | return 0; |
Joe Stringer | 0d5cdef | 2015-08-28 19:22:11 -0700 | [diff] [blame] | 222 | #else |
| 223 | return -ENOTSUPP; |
| 224 | #endif |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 227 | static int ovs_ct_set_labels(struct sk_buff *skb, struct sw_flow_key *key, |
| 228 | const struct ovs_key_ct_labels *labels, |
| 229 | const struct ovs_key_ct_labels *mask) |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 230 | { |
| 231 | enum ip_conntrack_info ctinfo; |
| 232 | struct nf_conn_labels *cl; |
| 233 | struct nf_conn *ct; |
| 234 | int err; |
| 235 | |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 236 | /* The connection could be invalid, in which case set_label is no-op.*/ |
| 237 | ct = nf_ct_get(skb, &ctinfo); |
| 238 | if (!ct) |
| 239 | return 0; |
| 240 | |
| 241 | cl = nf_ct_labels_find(ct); |
| 242 | if (!cl) { |
| 243 | nf_ct_labels_ext_add(ct); |
| 244 | cl = nf_ct_labels_find(ct); |
| 245 | } |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 246 | if (!cl || cl->words * sizeof(long) < OVS_CT_LABELS_LEN) |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 247 | return -ENOSPC; |
| 248 | |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 249 | err = nf_connlabels_replace(ct, (u32 *)labels, (u32 *)mask, |
| 250 | OVS_CT_LABELS_LEN / sizeof(u32)); |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 251 | if (err) |
| 252 | return err; |
| 253 | |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 254 | ovs_ct_get_labels(ct, &key->ct.labels); |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 255 | return 0; |
| 256 | } |
| 257 | |
Joe Stringer | cae3a26 | 2015-08-26 11:31:53 -0700 | [diff] [blame] | 258 | /* 'skb' should already be pulled to nh_ofs. */ |
| 259 | static int ovs_ct_helper(struct sk_buff *skb, u16 proto) |
| 260 | { |
| 261 | const struct nf_conntrack_helper *helper; |
| 262 | const struct nf_conn_help *help; |
| 263 | enum ip_conntrack_info ctinfo; |
| 264 | unsigned int protoff; |
| 265 | struct nf_conn *ct; |
| 266 | |
| 267 | ct = nf_ct_get(skb, &ctinfo); |
| 268 | if (!ct || ctinfo == IP_CT_RELATED_REPLY) |
| 269 | return NF_ACCEPT; |
| 270 | |
| 271 | help = nfct_help(ct); |
| 272 | if (!help) |
| 273 | return NF_ACCEPT; |
| 274 | |
| 275 | helper = rcu_dereference(help->helper); |
| 276 | if (!helper) |
| 277 | return NF_ACCEPT; |
| 278 | |
| 279 | switch (proto) { |
| 280 | case NFPROTO_IPV4: |
| 281 | protoff = ip_hdrlen(skb); |
| 282 | break; |
| 283 | case NFPROTO_IPV6: { |
| 284 | u8 nexthdr = ipv6_hdr(skb)->nexthdr; |
| 285 | __be16 frag_off; |
Joe Stringer | cc57060 | 2015-09-14 11:14:50 -0700 | [diff] [blame] | 286 | int ofs; |
Joe Stringer | cae3a26 | 2015-08-26 11:31:53 -0700 | [diff] [blame] | 287 | |
Joe Stringer | cc57060 | 2015-09-14 11:14:50 -0700 | [diff] [blame] | 288 | ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr, |
| 289 | &frag_off); |
| 290 | if (ofs < 0 || (frag_off & htons(~0x7)) != 0) { |
Joe Stringer | cae3a26 | 2015-08-26 11:31:53 -0700 | [diff] [blame] | 291 | pr_debug("proto header not found\n"); |
| 292 | return NF_ACCEPT; |
| 293 | } |
Joe Stringer | cc57060 | 2015-09-14 11:14:50 -0700 | [diff] [blame] | 294 | protoff = ofs; |
Joe Stringer | cae3a26 | 2015-08-26 11:31:53 -0700 | [diff] [blame] | 295 | break; |
| 296 | } |
| 297 | default: |
| 298 | WARN_ONCE(1, "helper invoked on non-IP family!"); |
| 299 | return NF_DROP; |
| 300 | } |
| 301 | |
| 302 | return helper->help(skb, protoff, ct, ctinfo); |
| 303 | } |
| 304 | |
Joe Stringer | 74c1661 | 2015-10-25 20:21:48 -0700 | [diff] [blame] | 305 | /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero |
| 306 | * value if 'skb' is freed. |
| 307 | */ |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 308 | static int handle_fragments(struct net *net, struct sw_flow_key *key, |
| 309 | u16 zone, struct sk_buff *skb) |
| 310 | { |
| 311 | struct ovs_skb_cb ovs_cb = *OVS_CB(skb); |
Florian Westphal | daaa7d6 | 2015-11-18 23:32:40 +0100 | [diff] [blame] | 312 | int err; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 313 | |
| 314 | if (key->eth.type == htons(ETH_P_IP)) { |
| 315 | enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 316 | |
| 317 | memset(IPCB(skb), 0, sizeof(struct inet_skb_parm)); |
Eric W. Biederman | 19bcf9f | 2015-10-09 13:44:54 -0500 | [diff] [blame] | 318 | err = ip_defrag(net, skb, user); |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 319 | if (err) |
| 320 | return err; |
| 321 | |
| 322 | ovs_cb.mru = IPCB(skb)->frag_max_size; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 323 | #if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) |
Joe Stringer | 74c1661 | 2015-10-25 20:21:48 -0700 | [diff] [blame] | 324 | } else if (key->eth.type == htons(ETH_P_IPV6)) { |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 325 | enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 326 | |
| 327 | memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm)); |
Florian Westphal | daaa7d6 | 2015-11-18 23:32:40 +0100 | [diff] [blame] | 328 | err = nf_ct_frag6_gather(net, skb, user); |
| 329 | if (err) |
| 330 | return err; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 331 | |
Florian Westphal | daaa7d6 | 2015-11-18 23:32:40 +0100 | [diff] [blame] | 332 | key->ip.proto = ipv6_hdr(skb)->nexthdr; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 333 | ovs_cb.mru = IP6CB(skb)->frag_max_size; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 334 | #endif |
| 335 | } else { |
Joe Stringer | 74c1661 | 2015-10-25 20:21:48 -0700 | [diff] [blame] | 336 | kfree_skb(skb); |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 337 | return -EPFNOSUPPORT; |
| 338 | } |
| 339 | |
| 340 | key->ip.frag = OVS_FRAG_TYPE_NONE; |
| 341 | skb_clear_hash(skb); |
| 342 | skb->ignore_df = 1; |
| 343 | *OVS_CB(skb) = ovs_cb; |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | static struct nf_conntrack_expect * |
| 349 | ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone, |
| 350 | u16 proto, const struct sk_buff *skb) |
| 351 | { |
| 352 | struct nf_conntrack_tuple tuple; |
| 353 | |
Eric W. Biederman | a31f1ad | 2015-09-18 14:33:04 -0500 | [diff] [blame] | 354 | if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple)) |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 355 | return NULL; |
| 356 | return __nf_ct_expect_find(net, zone, &tuple); |
| 357 | } |
| 358 | |
Jarno Rajahalme | 289f225 | 2016-03-10 10:54:20 -0800 | [diff] [blame] | 359 | /* This replicates logic from nf_conntrack_core.c that is not exported. */ |
| 360 | static enum ip_conntrack_info |
| 361 | ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h) |
| 362 | { |
| 363 | const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); |
| 364 | |
| 365 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) |
| 366 | return IP_CT_ESTABLISHED_REPLY; |
| 367 | /* Once we've had two way comms, always ESTABLISHED. */ |
| 368 | if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) |
| 369 | return IP_CT_ESTABLISHED; |
| 370 | if (test_bit(IPS_EXPECTED_BIT, &ct->status)) |
| 371 | return IP_CT_RELATED; |
| 372 | return IP_CT_NEW; |
| 373 | } |
| 374 | |
| 375 | /* Find an existing connection which this packet belongs to without |
| 376 | * re-attributing statistics or modifying the connection state. This allows an |
| 377 | * skb->nfct lost due to an upcall to be recovered during actions execution. |
| 378 | * |
| 379 | * Must be called with rcu_read_lock. |
| 380 | * |
| 381 | * On success, populates skb->nfct and skb->nfctinfo, and returns the |
| 382 | * connection. Returns NULL if there is no existing entry. |
| 383 | */ |
| 384 | static struct nf_conn * |
| 385 | ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone, |
| 386 | u8 l3num, struct sk_buff *skb) |
| 387 | { |
| 388 | struct nf_conntrack_l3proto *l3proto; |
| 389 | struct nf_conntrack_l4proto *l4proto; |
| 390 | struct nf_conntrack_tuple tuple; |
| 391 | struct nf_conntrack_tuple_hash *h; |
| 392 | enum ip_conntrack_info ctinfo; |
| 393 | struct nf_conn *ct; |
| 394 | unsigned int dataoff; |
| 395 | u8 protonum; |
| 396 | |
| 397 | l3proto = __nf_ct_l3proto_find(l3num); |
| 398 | if (!l3proto) { |
| 399 | pr_debug("ovs_ct_find_existing: Can't get l3proto\n"); |
| 400 | return NULL; |
| 401 | } |
| 402 | if (l3proto->get_l4proto(skb, skb_network_offset(skb), &dataoff, |
| 403 | &protonum) <= 0) { |
| 404 | pr_debug("ovs_ct_find_existing: Can't get protonum\n"); |
| 405 | return NULL; |
| 406 | } |
| 407 | l4proto = __nf_ct_l4proto_find(l3num, protonum); |
| 408 | if (!l4proto) { |
| 409 | pr_debug("ovs_ct_find_existing: Can't get l4proto\n"); |
| 410 | return NULL; |
| 411 | } |
| 412 | if (!nf_ct_get_tuple(skb, skb_network_offset(skb), dataoff, l3num, |
| 413 | protonum, net, &tuple, l3proto, l4proto)) { |
| 414 | pr_debug("ovs_ct_find_existing: Can't get tuple\n"); |
| 415 | return NULL; |
| 416 | } |
| 417 | |
| 418 | /* look for tuple match */ |
| 419 | h = nf_conntrack_find_get(net, zone, &tuple); |
| 420 | if (!h) |
| 421 | return NULL; /* Not found. */ |
| 422 | |
| 423 | ct = nf_ct_tuplehash_to_ctrack(h); |
| 424 | |
| 425 | ctinfo = ovs_ct_get_info(h); |
| 426 | if (ctinfo == IP_CT_NEW) { |
| 427 | /* This should not happen. */ |
| 428 | WARN_ONCE(1, "ovs_ct_find_existing: new packet for %p\n", ct); |
| 429 | } |
| 430 | skb->nfct = &ct->ct_general; |
| 431 | skb->nfctinfo = ctinfo; |
| 432 | return ct; |
| 433 | } |
| 434 | |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 435 | /* Determine whether skb->nfct is equal to the result of conntrack lookup. */ |
Jarno Rajahalme | 289f225 | 2016-03-10 10:54:20 -0800 | [diff] [blame] | 436 | static bool skb_nfct_cached(struct net *net, |
| 437 | const struct sw_flow_key *key, |
| 438 | const struct ovs_conntrack_info *info, |
| 439 | struct sk_buff *skb) |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 440 | { |
| 441 | enum ip_conntrack_info ctinfo; |
| 442 | struct nf_conn *ct; |
| 443 | |
| 444 | ct = nf_ct_get(skb, &ctinfo); |
Jarno Rajahalme | 289f225 | 2016-03-10 10:54:20 -0800 | [diff] [blame] | 445 | /* If no ct, check if we have evidence that an existing conntrack entry |
| 446 | * might be found for this skb. This happens when we lose a skb->nfct |
| 447 | * due to an upcall. If the connection was not confirmed, it is not |
| 448 | * cached and needs to be run through conntrack again. |
| 449 | */ |
| 450 | if (!ct && key->ct.state & OVS_CS_F_TRACKED && |
| 451 | !(key->ct.state & OVS_CS_F_INVALID) && |
| 452 | key->ct.zone == info->zone.id) |
| 453 | ct = ovs_ct_find_existing(net, &info->zone, info->family, skb); |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 454 | if (!ct) |
| 455 | return false; |
| 456 | if (!net_eq(net, read_pnet(&ct->ct_net))) |
| 457 | return false; |
| 458 | if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct))) |
| 459 | return false; |
Joe Stringer | cae3a26 | 2015-08-26 11:31:53 -0700 | [diff] [blame] | 460 | if (info->helper) { |
| 461 | struct nf_conn_help *help; |
| 462 | |
| 463 | help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER); |
| 464 | if (help && rcu_access_pointer(help->helper) != info->helper) |
| 465 | return false; |
| 466 | } |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 467 | |
| 468 | return true; |
| 469 | } |
| 470 | |
Jarno Rajahalme | 9f13ded | 2016-03-10 10:54:18 -0800 | [diff] [blame] | 471 | /* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if |
Jarno Rajahalme | 394e910 | 2016-03-10 10:54:19 -0800 | [diff] [blame] | 472 | * not done already. Update key with new CT state after passing the packet |
| 473 | * through conntrack. |
Jarno Rajahalme | 9f13ded | 2016-03-10 10:54:18 -0800 | [diff] [blame] | 474 | * Note that if the packet is deemed invalid by conntrack, skb->nfct will be |
| 475 | * set to NULL and 0 will be returned. |
| 476 | */ |
Joe Stringer | 4f0909e | 2015-10-19 19:18:59 -0700 | [diff] [blame] | 477 | static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key, |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 478 | const struct ovs_conntrack_info *info, |
| 479 | struct sk_buff *skb) |
| 480 | { |
| 481 | /* If we are recirculating packets to match on conntrack fields and |
| 482 | * committing with a separate conntrack action, then we don't need to |
| 483 | * actually run the packet through conntrack twice unless it's for a |
| 484 | * different zone. |
| 485 | */ |
Jarno Rajahalme | 289f225 | 2016-03-10 10:54:20 -0800 | [diff] [blame] | 486 | if (!skb_nfct_cached(net, key, info, skb)) { |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 487 | struct nf_conn *tmpl = info->ct; |
Jarno Rajahalme | 5b6b929 | 2016-03-10 10:54:21 -0800 | [diff] [blame^] | 488 | int err; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 489 | |
| 490 | /* Associate skb with specified zone. */ |
| 491 | if (tmpl) { |
| 492 | if (skb->nfct) |
| 493 | nf_conntrack_put(skb->nfct); |
| 494 | nf_conntrack_get(&tmpl->ct_general); |
| 495 | skb->nfct = &tmpl->ct_general; |
| 496 | skb->nfctinfo = IP_CT_NEW; |
| 497 | } |
| 498 | |
Jarno Rajahalme | 5b6b929 | 2016-03-10 10:54:21 -0800 | [diff] [blame^] | 499 | /* Repeat if requested, see nf_iterate(). */ |
| 500 | do { |
| 501 | err = nf_conntrack_in(net, info->family, |
| 502 | NF_INET_PRE_ROUTING, skb); |
| 503 | } while (err == NF_REPEAT); |
| 504 | |
| 505 | if (err != NF_ACCEPT) |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 506 | return -ENOENT; |
Joe Stringer | cae3a26 | 2015-08-26 11:31:53 -0700 | [diff] [blame] | 507 | |
Jarno Rajahalme | 394e910 | 2016-03-10 10:54:19 -0800 | [diff] [blame] | 508 | ovs_ct_update_key(skb, info, key, true); |
| 509 | |
Joe Stringer | cae3a26 | 2015-08-26 11:31:53 -0700 | [diff] [blame] | 510 | if (ovs_ct_helper(skb, info->family) != NF_ACCEPT) { |
| 511 | WARN_ONCE(1, "helper rejected packet"); |
| 512 | return -EINVAL; |
| 513 | } |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | /* Lookup connection and read fields into key. */ |
| 520 | static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key, |
| 521 | const struct ovs_conntrack_info *info, |
| 522 | struct sk_buff *skb) |
| 523 | { |
| 524 | struct nf_conntrack_expect *exp; |
| 525 | |
Jarno Rajahalme | 9f13ded | 2016-03-10 10:54:18 -0800 | [diff] [blame] | 526 | /* If we pass an expected packet through nf_conntrack_in() the |
| 527 | * expectation is typically removed, but the packet could still be |
| 528 | * lost in upcall processing. To prevent this from happening we |
| 529 | * perform an explicit expectation lookup. Expected connections are |
| 530 | * always new, and will be passed through conntrack only when they are |
| 531 | * committed, as it is OK to remove the expectation at that time. |
| 532 | */ |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 533 | exp = ovs_ct_expect_find(net, &info->zone, info->family, skb); |
| 534 | if (exp) { |
| 535 | u8 state; |
| 536 | |
| 537 | state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED; |
Joe Stringer | 182e304 | 2015-08-26 11:31:49 -0700 | [diff] [blame] | 538 | __ovs_ct_update_key(key, state, &info->zone, exp->master); |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 539 | } else { |
| 540 | int err; |
| 541 | |
| 542 | err = __ovs_ct_lookup(net, key, info, skb); |
| 543 | if (err) |
| 544 | return err; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | /* Lookup connection and confirm if unconfirmed. */ |
| 551 | static int ovs_ct_commit(struct net *net, struct sw_flow_key *key, |
| 552 | const struct ovs_conntrack_info *info, |
| 553 | struct sk_buff *skb) |
| 554 | { |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 555 | int err; |
| 556 | |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 557 | err = __ovs_ct_lookup(net, key, info, skb); |
| 558 | if (err) |
| 559 | return err; |
Jarno Rajahalme | 9f13ded | 2016-03-10 10:54:18 -0800 | [diff] [blame] | 560 | /* This is a no-op if the connection has already been confirmed. */ |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 561 | if (nf_conntrack_confirm(skb) != NF_ACCEPT) |
| 562 | return -EINVAL; |
| 563 | |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 564 | return 0; |
| 565 | } |
| 566 | |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 567 | static bool labels_nonzero(const struct ovs_key_ct_labels *labels) |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 568 | { |
| 569 | size_t i; |
| 570 | |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 571 | for (i = 0; i < sizeof(*labels); i++) |
| 572 | if (labels->ct_labels[i]) |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 573 | return true; |
| 574 | |
| 575 | return false; |
| 576 | } |
| 577 | |
Joe Stringer | 74c1661 | 2015-10-25 20:21:48 -0700 | [diff] [blame] | 578 | /* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero |
| 579 | * value if 'skb' is freed. |
| 580 | */ |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 581 | int ovs_ct_execute(struct net *net, struct sk_buff *skb, |
| 582 | struct sw_flow_key *key, |
| 583 | const struct ovs_conntrack_info *info) |
| 584 | { |
| 585 | int nh_ofs; |
| 586 | int err; |
| 587 | |
| 588 | /* The conntrack module expects to be working at L3. */ |
| 589 | nh_ofs = skb_network_offset(skb); |
| 590 | skb_pull(skb, nh_ofs); |
| 591 | |
| 592 | if (key->ip.frag != OVS_FRAG_TYPE_NONE) { |
| 593 | err = handle_fragments(net, key, info->zone.id, skb); |
| 594 | if (err) |
| 595 | return err; |
| 596 | } |
| 597 | |
Joe Stringer | ab38a7b | 2015-10-06 11:00:01 -0700 | [diff] [blame] | 598 | if (info->commit) |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 599 | err = ovs_ct_commit(net, key, info, skb); |
| 600 | else |
| 601 | err = ovs_ct_lookup(net, key, info, skb); |
Joe Stringer | 182e304 | 2015-08-26 11:31:49 -0700 | [diff] [blame] | 602 | if (err) |
| 603 | goto err; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 604 | |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 605 | if (info->mark.mask) { |
Joe Stringer | 182e304 | 2015-08-26 11:31:49 -0700 | [diff] [blame] | 606 | err = ovs_ct_set_mark(skb, key, info->mark.value, |
| 607 | info->mark.mask); |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 608 | if (err) |
| 609 | goto err; |
| 610 | } |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 611 | if (labels_nonzero(&info->labels.mask)) |
| 612 | err = ovs_ct_set_labels(skb, key, &info->labels.value, |
| 613 | &info->labels.mask); |
Joe Stringer | 182e304 | 2015-08-26 11:31:49 -0700 | [diff] [blame] | 614 | err: |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 615 | skb_push(skb, nh_ofs); |
Joe Stringer | 74c1661 | 2015-10-25 20:21:48 -0700 | [diff] [blame] | 616 | if (err) |
| 617 | kfree_skb(skb); |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 618 | return err; |
| 619 | } |
| 620 | |
Joe Stringer | cae3a26 | 2015-08-26 11:31:53 -0700 | [diff] [blame] | 621 | static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name, |
| 622 | const struct sw_flow_key *key, bool log) |
| 623 | { |
| 624 | struct nf_conntrack_helper *helper; |
| 625 | struct nf_conn_help *help; |
| 626 | |
| 627 | helper = nf_conntrack_helper_try_module_get(name, info->family, |
| 628 | key->ip.proto); |
| 629 | if (!helper) { |
| 630 | OVS_NLERR(log, "Unknown helper \"%s\"", name); |
| 631 | return -EINVAL; |
| 632 | } |
| 633 | |
| 634 | help = nf_ct_helper_ext_add(info->ct, helper, GFP_KERNEL); |
| 635 | if (!help) { |
| 636 | module_put(helper->me); |
| 637 | return -ENOMEM; |
| 638 | } |
| 639 | |
| 640 | rcu_assign_pointer(help->helper, helper); |
| 641 | info->helper = helper; |
| 642 | return 0; |
| 643 | } |
| 644 | |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 645 | static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = { |
Joe Stringer | ab38a7b | 2015-10-06 11:00:01 -0700 | [diff] [blame] | 646 | [OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 }, |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 647 | [OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16), |
| 648 | .maxlen = sizeof(u16) }, |
Joe Stringer | 182e304 | 2015-08-26 11:31:49 -0700 | [diff] [blame] | 649 | [OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark), |
| 650 | .maxlen = sizeof(struct md_mark) }, |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 651 | [OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels), |
| 652 | .maxlen = sizeof(struct md_labels) }, |
Joe Stringer | cae3a26 | 2015-08-26 11:31:53 -0700 | [diff] [blame] | 653 | [OVS_CT_ATTR_HELPER] = { .minlen = 1, |
| 654 | .maxlen = NF_CT_HELPER_NAME_LEN } |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 655 | }; |
| 656 | |
| 657 | static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info, |
Joe Stringer | cae3a26 | 2015-08-26 11:31:53 -0700 | [diff] [blame] | 658 | const char **helper, bool log) |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 659 | { |
| 660 | struct nlattr *a; |
| 661 | int rem; |
| 662 | |
| 663 | nla_for_each_nested(a, attr, rem) { |
| 664 | int type = nla_type(a); |
| 665 | int maxlen = ovs_ct_attr_lens[type].maxlen; |
| 666 | int minlen = ovs_ct_attr_lens[type].minlen; |
| 667 | |
| 668 | if (type > OVS_CT_ATTR_MAX) { |
| 669 | OVS_NLERR(log, |
| 670 | "Unknown conntrack attr (type=%d, max=%d)", |
| 671 | type, OVS_CT_ATTR_MAX); |
| 672 | return -EINVAL; |
| 673 | } |
| 674 | if (nla_len(a) < minlen || nla_len(a) > maxlen) { |
| 675 | OVS_NLERR(log, |
| 676 | "Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)", |
| 677 | type, nla_len(a), maxlen); |
| 678 | return -EINVAL; |
| 679 | } |
| 680 | |
| 681 | switch (type) { |
Joe Stringer | ab38a7b | 2015-10-06 11:00:01 -0700 | [diff] [blame] | 682 | case OVS_CT_ATTR_COMMIT: |
| 683 | info->commit = true; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 684 | break; |
| 685 | #ifdef CONFIG_NF_CONNTRACK_ZONES |
| 686 | case OVS_CT_ATTR_ZONE: |
| 687 | info->zone.id = nla_get_u16(a); |
| 688 | break; |
| 689 | #endif |
Joe Stringer | 182e304 | 2015-08-26 11:31:49 -0700 | [diff] [blame] | 690 | #ifdef CONFIG_NF_CONNTRACK_MARK |
| 691 | case OVS_CT_ATTR_MARK: { |
| 692 | struct md_mark *mark = nla_data(a); |
| 693 | |
Joe Stringer | e754ec6 | 2015-10-19 19:19:00 -0700 | [diff] [blame] | 694 | if (!mark->mask) { |
| 695 | OVS_NLERR(log, "ct_mark mask cannot be 0"); |
| 696 | return -EINVAL; |
| 697 | } |
Joe Stringer | 182e304 | 2015-08-26 11:31:49 -0700 | [diff] [blame] | 698 | info->mark = *mark; |
| 699 | break; |
| 700 | } |
| 701 | #endif |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 702 | #ifdef CONFIG_NF_CONNTRACK_LABELS |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 703 | case OVS_CT_ATTR_LABELS: { |
| 704 | struct md_labels *labels = nla_data(a); |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 705 | |
Joe Stringer | e754ec6 | 2015-10-19 19:19:00 -0700 | [diff] [blame] | 706 | if (!labels_nonzero(&labels->mask)) { |
| 707 | OVS_NLERR(log, "ct_labels mask cannot be 0"); |
| 708 | return -EINVAL; |
| 709 | } |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 710 | info->labels = *labels; |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 711 | break; |
| 712 | } |
| 713 | #endif |
Joe Stringer | cae3a26 | 2015-08-26 11:31:53 -0700 | [diff] [blame] | 714 | case OVS_CT_ATTR_HELPER: |
| 715 | *helper = nla_data(a); |
| 716 | if (!memchr(*helper, '\0', nla_len(a))) { |
| 717 | OVS_NLERR(log, "Invalid conntrack helper"); |
| 718 | return -EINVAL; |
| 719 | } |
| 720 | break; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 721 | default: |
| 722 | OVS_NLERR(log, "Unknown conntrack attr (%d)", |
| 723 | type); |
| 724 | return -EINVAL; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | if (rem > 0) { |
| 729 | OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem); |
| 730 | return -EINVAL; |
| 731 | } |
| 732 | |
| 733 | return 0; |
| 734 | } |
| 735 | |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 736 | bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr) |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 737 | { |
| 738 | if (attr == OVS_KEY_ATTR_CT_STATE) |
| 739 | return true; |
| 740 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && |
| 741 | attr == OVS_KEY_ATTR_CT_ZONE) |
| 742 | return true; |
Joe Stringer | 182e304 | 2015-08-26 11:31:49 -0700 | [diff] [blame] | 743 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && |
| 744 | attr == OVS_KEY_ATTR_CT_MARK) |
| 745 | return true; |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 746 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 747 | attr == OVS_KEY_ATTR_CT_LABELS) { |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 748 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
| 749 | |
| 750 | return ovs_net->xt_label; |
| 751 | } |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 752 | |
| 753 | return false; |
| 754 | } |
| 755 | |
| 756 | int ovs_ct_copy_action(struct net *net, const struct nlattr *attr, |
| 757 | const struct sw_flow_key *key, |
| 758 | struct sw_flow_actions **sfa, bool log) |
| 759 | { |
| 760 | struct ovs_conntrack_info ct_info; |
Joe Stringer | cae3a26 | 2015-08-26 11:31:53 -0700 | [diff] [blame] | 761 | const char *helper = NULL; |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 762 | u16 family; |
| 763 | int err; |
| 764 | |
| 765 | family = key_to_nfproto(key); |
| 766 | if (family == NFPROTO_UNSPEC) { |
| 767 | OVS_NLERR(log, "ct family unspecified"); |
| 768 | return -EINVAL; |
| 769 | } |
| 770 | |
| 771 | memset(&ct_info, 0, sizeof(ct_info)); |
| 772 | ct_info.family = family; |
| 773 | |
| 774 | nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID, |
| 775 | NF_CT_DEFAULT_ZONE_DIR, 0); |
| 776 | |
Joe Stringer | cae3a26 | 2015-08-26 11:31:53 -0700 | [diff] [blame] | 777 | err = parse_ct(attr, &ct_info, &helper, log); |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 778 | if (err) |
| 779 | return err; |
| 780 | |
| 781 | /* Set up template for tracking connections in specific zones. */ |
| 782 | ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL); |
| 783 | if (!ct_info.ct) { |
| 784 | OVS_NLERR(log, "Failed to allocate conntrack template"); |
| 785 | return -ENOMEM; |
| 786 | } |
Joe Stringer | 90c7afc96 | 2015-12-23 14:39:27 -0800 | [diff] [blame] | 787 | |
| 788 | __set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status); |
| 789 | nf_conntrack_get(&ct_info.ct->ct_general); |
| 790 | |
Joe Stringer | cae3a26 | 2015-08-26 11:31:53 -0700 | [diff] [blame] | 791 | if (helper) { |
| 792 | err = ovs_ct_add_helper(&ct_info, helper, key, log); |
| 793 | if (err) |
| 794 | goto err_free_ct; |
| 795 | } |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 796 | |
| 797 | err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info, |
| 798 | sizeof(ct_info), log); |
| 799 | if (err) |
| 800 | goto err_free_ct; |
| 801 | |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 802 | return 0; |
| 803 | err_free_ct: |
Joe Stringer | 2f3ab9f | 2015-12-09 14:07:39 -0800 | [diff] [blame] | 804 | __ovs_ct_free_action(&ct_info); |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 805 | return err; |
| 806 | } |
| 807 | |
| 808 | int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info, |
| 809 | struct sk_buff *skb) |
| 810 | { |
| 811 | struct nlattr *start; |
| 812 | |
| 813 | start = nla_nest_start(skb, OVS_ACTION_ATTR_CT); |
| 814 | if (!start) |
| 815 | return -EMSGSIZE; |
| 816 | |
Joe Stringer | ab38a7b | 2015-10-06 11:00:01 -0700 | [diff] [blame] | 817 | if (ct_info->commit && nla_put_flag(skb, OVS_CT_ATTR_COMMIT)) |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 818 | return -EMSGSIZE; |
| 819 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && |
| 820 | nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id)) |
| 821 | return -EMSGSIZE; |
Joe Stringer | e754ec6 | 2015-10-19 19:19:00 -0700 | [diff] [blame] | 822 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask && |
Joe Stringer | 182e304 | 2015-08-26 11:31:49 -0700 | [diff] [blame] | 823 | nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark), |
| 824 | &ct_info->mark)) |
| 825 | return -EMSGSIZE; |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 826 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && |
Joe Stringer | e754ec6 | 2015-10-19 19:19:00 -0700 | [diff] [blame] | 827 | labels_nonzero(&ct_info->labels.mask) && |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 828 | nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels), |
| 829 | &ct_info->labels)) |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 830 | return -EMSGSIZE; |
Joe Stringer | cae3a26 | 2015-08-26 11:31:53 -0700 | [diff] [blame] | 831 | if (ct_info->helper) { |
| 832 | if (nla_put_string(skb, OVS_CT_ATTR_HELPER, |
| 833 | ct_info->helper->name)) |
| 834 | return -EMSGSIZE; |
| 835 | } |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 836 | |
| 837 | nla_nest_end(skb, start); |
| 838 | |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | void ovs_ct_free_action(const struct nlattr *a) |
| 843 | { |
| 844 | struct ovs_conntrack_info *ct_info = nla_data(a); |
| 845 | |
Joe Stringer | 2f3ab9f | 2015-12-09 14:07:39 -0800 | [diff] [blame] | 846 | __ovs_ct_free_action(ct_info); |
| 847 | } |
| 848 | |
| 849 | static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info) |
| 850 | { |
Joe Stringer | cae3a26 | 2015-08-26 11:31:53 -0700 | [diff] [blame] | 851 | if (ct_info->helper) |
| 852 | module_put(ct_info->helper->me); |
Joe Stringer | 7f8a436 | 2015-08-26 11:31:48 -0700 | [diff] [blame] | 853 | if (ct_info->ct) |
| 854 | nf_ct_put(ct_info->ct); |
| 855 | } |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 856 | |
| 857 | void ovs_ct_init(struct net *net) |
| 858 | { |
Joe Stringer | 33db412 | 2015-10-01 15:00:37 -0700 | [diff] [blame] | 859 | unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE; |
Joe Stringer | c2ac667 | 2015-08-26 11:31:52 -0700 | [diff] [blame] | 860 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
| 861 | |
| 862 | if (nf_connlabels_get(net, n_bits)) { |
| 863 | ovs_net->xt_label = false; |
| 864 | OVS_NLERR(true, "Failed to set connlabel length"); |
| 865 | } else { |
| 866 | ovs_net->xt_label = true; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | void ovs_ct_exit(struct net *net) |
| 871 | { |
| 872 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
| 873 | |
| 874 | if (ovs_net->xt_label) |
| 875 | nf_connlabels_put(net); |
| 876 | } |