/* Map SKB connection state into the values used by flow definition. */ static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
{
u8 ct_state = OVS_CS_F_TRACKED;
switch (ctinfo) { case IP_CT_ESTABLISHED_REPLY: case IP_CT_RELATED_REPLY:
ct_state |= OVS_CS_F_REPLY_DIR; break; default: break;
}
switch (ctinfo) { case IP_CT_ESTABLISHED: case IP_CT_ESTABLISHED_REPLY:
ct_state |= OVS_CS_F_ESTABLISHED; break; case IP_CT_RELATED: case IP_CT_RELATED_REPLY:
ct_state |= OVS_CS_F_RELATED; break; case IP_CT_NEW:
ct_state |= OVS_CS_F_NEW; break; default: break;
}
/* Guard against conntrack labels max size shrinking below 128 bits. */ #if NF_CT_LABELS_MAX_SIZE < 16 #error NF_CT_LABELS_MAX_SIZE must be at least 16 bytes #endif
/* Use the master if we have one. */ if (ct->master)
ct = ct->master;
orig = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
/* IP version must match with the master connection. */ if (key->eth.type == htons(ETH_P_IP) &&
nf_ct_l3num(ct) == NFPROTO_IPV4) {
key->ipv4.ct_orig.src = orig->src.u3.ip;
key->ipv4.ct_orig.dst = orig->dst.u3.ip;
__ovs_ct_update_key_orig_tp(key, orig, IPPROTO_ICMP); return;
} elseif (key->eth.type == htons(ETH_P_IPV6) &&
!sw_flow_key_is_nd(key) &&
nf_ct_l3num(ct) == NFPROTO_IPV6) {
key->ipv6.ct_orig.src = orig->src.u3.in6;
key->ipv6.ct_orig.dst = orig->dst.u3.in6;
__ovs_ct_update_key_orig_tp(key, orig, NEXTHDR_ICMP); return;
}
} /* Clear 'ct_orig_proto' to mark the non-existence of conntrack * original direction key fields.
*/
key->ct_orig_proto = 0;
}
/* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has * previously sent the packet to conntrack via the ct action. If * 'keep_nat_flags' is true, the existing NAT flags retained, else they are * initialized from the connection status.
*/ staticvoid ovs_ct_update_key(conststruct sk_buff *skb, conststruct ovs_conntrack_info *info, struct sw_flow_key *key, bool post_ct, bool keep_nat_flags)
{ conststruct nf_conntrack_zone *zone = &nf_ct_zone_dflt; enum ip_conntrack_info ctinfo; struct nf_conn *ct;
u8 state = 0;
ct = nf_ct_get(skb, &ctinfo); if (ct) {
state = ovs_ct_get_state(ctinfo); /* All unconfirmed entries are NEW connections. */ if (!nf_ct_is_confirmed(ct))
state |= OVS_CS_F_NEW; /* OVS persists the related flag for the duration of the * connection.
*/ if (ct->master)
state |= OVS_CS_F_RELATED; if (keep_nat_flags) {
state |= key->ct_state & OVS_CS_F_NAT_MASK;
} else { if (ct->status & IPS_SRC_NAT)
state |= OVS_CS_F_SRC_NAT; if (ct->status & IPS_DST_NAT)
state |= OVS_CS_F_DST_NAT;
}
zone = nf_ct_zone(ct);
} elseif (post_ct) {
state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID; if (info)
zone = &info->zone;
}
__ovs_ct_update_key(key, state, zone, ct);
}
/* This is called to initialize CT key fields possibly coming in from the local * stack.
*/ void ovs_ct_fill_key(conststruct sk_buff *skb, struct sw_flow_key *key, bool post_ct)
{
ovs_ct_update_key(skb, NULL, key, post_ct, false);
}
int ovs_ct_put_key(conststruct sw_flow_key *swkey, conststruct sw_flow_key *output, struct sk_buff *skb)
{ if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, output->ct_state)) return -EMSGSIZE;
if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, output->ct_zone)) return -EMSGSIZE;
if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, output->ct.mark)) return -EMSGSIZE;
if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(output->ct.labels),
&output->ct.labels)) return -EMSGSIZE;
if (swkey->ct_orig_proto) { if (swkey->eth.type == htons(ETH_P_IP)) { struct ovs_key_ct_tuple_ipv4 orig;
/* Initialize labels for a new, yet to be committed conntrack entry. Note that * since the new connection is not yet confirmed, and thus no-one else has * access to it's labels, we simply write them over.
*/ staticint ovs_ct_init_labels(struct nf_conn *ct, struct sw_flow_key *key, conststruct ovs_key_ct_labels *labels, conststruct ovs_key_ct_labels *mask)
{ struct nf_conn_labels *cl, *master_cl; bool have_mask = labels_nonzero(mask);
/* Inherit master's labels to the related connection? */
master_cl = ct->master ? nf_ct_labels_find(ct->master) : NULL;
if (!master_cl && !have_mask) return 0; /* Nothing to do. */
cl = ovs_ct_get_conn_labels(ct); if (!cl) return -ENOSPC;
/* Inherit the master's labels, if any. */ if (master_cl)
*cl = *master_cl;
if (have_mask) {
u32 *dst = (u32 *)cl->bits; int i;
for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
dst[i] = (dst[i] & ~mask->ct_labels_32[i]) |
(labels->ct_labels_32[i]
& mask->ct_labels_32[i]);
}
/* Labels are included in the IPCTNL_MSG_CT_NEW event only if the * IPCT_LABEL bit is set in the event cache.
*/
nf_conntrack_event_cache(IPCT_LABEL, ct);
/* The key extracted from the fragment that completed this datagram * likely didn't have an L4 header, so regenerate it.
*/
ovs_flow_key_update_l3l4(skb, key);
key->ip.frag = OVS_FRAG_TYPE_NONE;
*OVS_CB(skb) = ovs_cb;
return 0;
}
/* This replicates logic from nf_conntrack_core.c that is not exported. */ staticenum ip_conntrack_info
ovs_ct_get_info(conststruct nf_conntrack_tuple_hash *h)
{ conststruct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) return IP_CT_ESTABLISHED_REPLY; /* Once we've had two way comms, always ESTABLISHED. */ if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) return IP_CT_ESTABLISHED; if (test_bit(IPS_EXPECTED_BIT, &ct->status)) return IP_CT_RELATED; return IP_CT_NEW;
}
/* Find an existing connection which this packet belongs to without * re-attributing statistics or modifying the connection state. This allows an * skb->_nfct lost due to an upcall to be recovered during actions execution. * * Must be called with rcu_read_lock. * * On success, populates skb->_nfct and returns the connection. Returns NULL * if there is no existing entry.
*/ staticstruct nf_conn *
ovs_ct_find_existing(struct net *net, conststruct nf_conntrack_zone *zone,
u8 l3num, struct sk_buff *skb, bool natted)
{ struct nf_conntrack_tuple tuple; struct nf_conntrack_tuple_hash *h; struct nf_conn *ct;
if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), l3num,
net, &tuple)) {
pr_debug("ovs_ct_find_existing: Can't get tuple\n"); return NULL;
}
/* Must invert the tuple if skb has been transformed by NAT. */ if (natted) { struct nf_conntrack_tuple inverse;
/* look for tuple match */
h = nf_conntrack_find_get(net, zone, &tuple); if (!h) return NULL; /* Not found. */
ct = nf_ct_tuplehash_to_ctrack(h);
/* Inverted packet tuple matches the reverse direction conntrack tuple, * select the other tuplehash to get the right 'ctinfo' bits for this * packet.
*/ if (natted)
h = &ct->tuplehash[!h->tuple.dst.dir];
/* If no ct, check if we have evidence that an existing conntrack entry * might be found for this skb. This happens when we lose a skb->_nfct * due to an upcall, or if the direction is being forced. If the * connection was not confirmed, it is not cached and needs to be run * through conntrack again.
*/
*ct_executed = (key->ct_state & OVS_CS_F_TRACKED) &&
!(key->ct_state & OVS_CS_F_INVALID) &&
(key->ct_zone == info->zone.id);
if (ct)
nf_ct_get(skb, &ctinfo); else returnfalse;
if (!net_eq(net, read_pnet(&ct->ct_net))) returnfalse; if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct))) returnfalse; if (info->helper) { struct nf_conn_help *help;
help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER); if (help && rcu_access_pointer(help->helper) != info->helper) returnfalse;
} if (info->nf_ct_timeout) { struct nf_conn_timeout *timeout_ext;
timeout_ext = nf_ct_timeout_find(ct); if (!timeout_ext || info->nf_ct_timeout !=
rcu_dereference(timeout_ext->timeout)) returnfalse;
} /* Force conntrack entry direction to the current packet? */ if (info->force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) { /* Delete the conntrack entry if confirmed, else just release * the reference.
*/ if (nf_ct_is_confirmed(ct))
nf_ct_delete(ct, 0, 0);
staticint verdict_to_errno(unsignedint verdict)
{ switch (verdict & NF_VERDICT_MASK) { case NF_ACCEPT: return 0; case NF_DROP: return -EINVAL; case NF_STOLEN: return -EINPROGRESS; default: break;
}
return -EINVAL;
}
/* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if * not done already. Update key with new CT state after passing the packet * through conntrack. * Note that if the packet is deemed invalid by conntrack, skb->_nfct will be * set to NULL and 0 will be returned.
*/ staticint __ovs_ct_lookup(struct net *net, struct sw_flow_key *key, conststruct ovs_conntrack_info *info, struct sk_buff *skb)
{ /* If we are recirculating packets to match on conntrack fields and * committing with a separate conntrack action, then we don't need to * actually run the packet through conntrack twice unless it's for a * different zone.
*/ bool cached = skb_nfct_cached(net, key, info, skb); enum ip_conntrack_info ctinfo; struct nf_conn *ct;
if (!cached) { struct nf_hook_state state = {
.hook = NF_INET_PRE_ROUTING,
.pf = info->family,
.net = net,
}; struct nf_conn *tmpl = info->ct; int err;
/* Associate skb with specified zone. */ if (tmpl) {
ct = nf_ct_get(skb, &ctinfo);
nf_ct_put(ct);
nf_conntrack_get(&tmpl->ct_general);
nf_ct_set(skb, tmpl, IP_CT_NEW);
}
err = nf_conntrack_in(skb, &state); if (err != NF_ACCEPT) return verdict_to_errno(err);
/* Clear CT state NAT flags to mark that we have not yet done * NAT after the nf_conntrack_in() call. We can actually clear * the whole state, as it will be re-initialized below.
*/
key->ct_state = 0;
/* Update the key, but keep the NAT flags. */
ovs_ct_update_key(skb, info, key, true, true);
}
/* Packets starting a new connection must be NATted before the * helper, so that the helper knows about the NAT. We enforce * this by delaying both NAT and helper calls for unconfirmed * connections until the committing CT action. For later * packets NAT and Helper may be called in either order. * * NAT will be done only if the CT action has NAT, and only * once per packet (per zone), as guarded by the NAT bits in * the key->ct_state.
*/ if (info->nat && !(key->ct_state & OVS_CS_F_NAT_MASK) &&
(nf_ct_is_confirmed(ct) || info->commit)) { int err = ovs_ct_nat(net, key, info, skb, ct, ctinfo);
err = verdict_to_errno(err); if (err) return err;
}
/* Userspace may decide to perform a ct lookup without a helper * specified followed by a (recirculate and) commit with one, * or attach a helper in a later commit. Therefore, for * connections which we will commit, we may need to attach * the helper here.
*/ if (!nf_ct_is_confirmed(ct) && info->commit &&
info->helper && !nfct_help(ct)) { int err = __nf_ct_try_assign_helper(ct, info->ct,
GFP_ATOMIC); if (err) return err;
add_helper = true;
/* helper installed, add seqadj if NAT is required */ if (info->nat && !nfct_seqadj(ct)) { if (!nfct_seqadj_ext_add(ct)) return -EINVAL;
}
}
/* Call the helper only if: * - nf_conntrack_in() was executed above ("!cached") or a * helper was just attached ("add_helper") for a confirmed * connection, or * - When committing an unconfirmed connection.
*/ if ((nf_ct_is_confirmed(ct) ? !cached || add_helper :
info->commit)) { int err = nf_ct_helper(skb, ct, ctinfo, info->family);
err = verdict_to_errno(err); if (err) return err;
}
if (nf_ct_protonum(ct) == IPPROTO_TCP &&
nf_ct_is_confirmed(ct) && nf_conntrack_tcp_established(ct)) { /* Be liberal for tcp packets so that out-of-window * packets are not marked invalid.
*/
nf_ct_set_tcp_be_liberal(ct);
}
nf_conn_act_ct_ext_fill(skb, ct, ctinfo);
}
return 0;
}
/* Lookup connection and read fields into key. */ staticint ovs_ct_lookup(struct net *net, struct sw_flow_key *key, conststruct ovs_conntrack_info *info, struct sk_buff *skb)
{ struct nf_conn *ct; int err;
err = __ovs_ct_lookup(net, key, info, skb); if (err) return err;
ct = (struct nf_conn *)skb_nfct(skb); if (ct)
nf_ct_deliver_cached_events(ct);
/* Lookup connection and confirm if unconfirmed. */ staticint ovs_ct_commit(struct net *net, struct sw_flow_key *key, conststruct ovs_conntrack_info *info, struct sk_buff *skb)
{ enum ip_conntrack_info ctinfo; struct nf_conn *ct; int err;
err = __ovs_ct_lookup(net, key, info, skb); if (err) return err;
/* The connection could be invalid, in which case this is a no-op.*/
ct = nf_ct_get(skb, &ctinfo); if (!ct) return 0;
#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT) if (static_branch_unlikely(&ovs_ct_limit_enabled)) { if (!nf_ct_is_confirmed(ct)) {
err = ovs_ct_check_limit(net, info,
&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); if (err) {
net_warn_ratelimited("openvswitch: zone: %u " "exceeds conntrack limit\n",
info->zone.id); return err;
}
}
} #endif
/* Set the conntrack event mask if given. NEW and DELETE events have * their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener * typically would receive many kinds of updates. Setting the event * mask allows those events to be filtered. The set event mask will * remain in effect for the lifetime of the connection unless changed * by a further CT action with both the commit flag and the eventmask
* option. */ if (info->have_eventmask) { struct nf_conntrack_ecache *cache = nf_ct_ecache_find(ct);
if (cache)
cache->ctmask = info->eventmask;
}
/* Apply changes before confirming the connection so that the initial * conntrack NEW netlink event carries the values given in the CT * action.
*/ if (info->mark.mask) {
err = ovs_ct_set_mark(ct, key, info->mark.value,
info->mark.mask); if (err) return err;
} if (!nf_ct_is_confirmed(ct)) {
err = ovs_ct_init_labels(ct, key, &info->labels.value,
&info->labels.mask); if (err) return err;
nf_conn_act_ct_ext_add(skb, ct, ctinfo);
} elseif (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
labels_nonzero(&info->labels.mask)) {
err = ovs_ct_set_labels(ct, key, &info->labels.value,
&info->labels.mask); if (err) return err;
} /* This will take care of sending queued events even if the connection * is already confirmed.
*/
err = nf_conntrack_confirm(skb);
return verdict_to_errno(err);
}
/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero * value if 'skb' is freed.
*/ int ovs_ct_execute(struct net *net, struct sk_buff *skb, struct sw_flow_key *key, conststruct ovs_conntrack_info *info)
{ int nh_ofs; int err;
/* The conntrack module expects to be working at L3. */
nh_ofs = skb_network_offset(skb);
skb_pull_rcsum(skb, nh_ofs);
if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
OVS_NLERR(log, "NAT attribute type %d has unexpected length (%d != %d)",
type, nla_len(a),
ovs_nat_attr_lens[type][ip_vers]); return -EINVAL;
}
switch (type) { case OVS_NAT_ATTR_SRC: case OVS_NAT_ATTR_DST: if (info->nat) {
OVS_NLERR(log, "Only one type of NAT may be specified"); return -ERANGE;
}
info->nat |= OVS_CT_NAT;
info->nat |= ((type == OVS_NAT_ATTR_SRC)
? OVS_CT_SRC_NAT : OVS_CT_DST_NAT); break;
case OVS_NAT_ATTR_IP_MIN:
nla_memcpy(&info->range.min_addr, a, sizeof(info->range.min_addr));
info->range.flags |= NF_NAT_RANGE_MAP_IPS; break;
case OVS_NAT_ATTR_IP_MAX:
have_ip_max = true;
nla_memcpy(&info->range.max_addr, a, sizeof(info->range.max_addr));
info->range.flags |= NF_NAT_RANGE_MAP_IPS; break;
case OVS_NAT_ATTR_PROTO_MIN:
info->range.min_proto.all = htons(nla_get_u16(a));
info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED; break;
if (rem > 0) {
OVS_NLERR(log, "NAT attribute has %d unknown bytes", rem); return -EINVAL;
} if (!info->nat) { /* Do not allow flags if no type is given. */ if (info->range.flags) {
OVS_NLERR(log, "NAT flags may be given only when NAT range (SRC or DST) is also specified."
); return -EINVAL;
}
info->nat = OVS_CT_NAT; /* NAT existing connections. */
} elseif (!info->commit) {
OVS_NLERR(log, "NAT attributes may be specified only when CT COMMIT flag is also specified."
); return -EINVAL;
} /* Allow missing IP_MAX. */ if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
memcpy(&info->range.max_addr, &info->range.min_addr, sizeof(info->range.max_addr));
} /* Allow missing PROTO_MAX. */ if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
!have_proto_max) {
info->range.max_proto.all = info->range.min_proto.all;
} return 0;
} #endif
err = parse_ct(attr, &ct_info, &helper, log); if (err) return err;
/* Set up template for tracking connections in specific zones. */
ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL); if (!ct_info.ct) {
OVS_NLERR(log, "Failed to allocate conntrack template"); return -ENOMEM;
}
if (ct_info.timeout[0]) { if (nf_ct_set_timeout(net, ct_info.ct, family, key->ip.proto,
ct_info.timeout))
OVS_NLERR(log, "Failed to associated timeout policy '%s'",
ct_info.timeout); else
ct_info.nf_ct_timeout = rcu_dereference(
nf_ct_timeout_find(ct_info.ct)->timeout);
}
if (helper) {
err = nf_ct_add_helper(ct_info.ct, helper, ct_info.family,
key->ip.proto, ct_info.nat, &ct_info.helper); if (err) {
OVS_NLERR(log, "Failed to add %s helper %d", helper, err); goto err_free_ct;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.