Commit e7b83f2f authored by Paolo Abeni's avatar Paolo Abeni

Merge branch 'mctp-core-protocol-updates-minor-fixes-tests'

Jeremy Kerr says:

====================
MCTP core protocol updates, minor fixes & tests

This series implements some procotol improvements for AF_MCTP,
particularly for systems with multiple MCTP networks defined. For those,
we need to add the network ID to the tag lookups, which then suggests an
updated version of the tag allocate / drop ioctl to allow the net ID to
be specified there too.

The ioctl change affects uabi, so might warrant some extra attention.

There are also a couple of new kunit tests for multiple-net
configurations.

We have a fix for populating the flow data when fragmenting, and a
testcase for that too.

Of course, any queries/comments/etc., please let me know!
====================

Link: https://lore.kernel.org/r/cover.1708335994.git.jk@codeconstruct.com.auSigned-off-by: default avatarPaolo Abeni <pabeni@redhat.com>
parents 6d5c3656 d192eaf5
......@@ -87,7 +87,7 @@ struct mctp_sock {
};
/* Key for matching incoming packets to sockets or reassembly contexts.
* Packets are matched on (src,dest,tag).
* Packets are matched on (peer EID, local EID, tag).
*
* Lifetime / locking requirements:
*
......@@ -133,6 +133,7 @@ struct mctp_sock {
* - through an expiry timeout, on a per-socket timer
*/
struct mctp_sk_key {
unsigned int net;
mctp_eid_t peer_addr;
mctp_eid_t local_addr; /* MCTP_ADDR_ANY for local owned tags */
__u8 tag; /* incoming tag match; invert TO for local */
......@@ -254,7 +255,8 @@ int mctp_local_output(struct sock *sk, struct mctp_route *rt,
void mctp_key_unref(struct mctp_sk_key *key);
struct mctp_sk_key *mctp_alloc_local_tag(struct mctp_sock *msk,
mctp_eid_t daddr, mctp_eid_t saddr,
unsigned int netid,
mctp_eid_t local, mctp_eid_t peer,
bool manual, u8 *tagp);
/* routing <--> device interface */
......
......@@ -50,7 +50,14 @@ struct sockaddr_mctp_ext {
#define SIOCMCTPALLOCTAG (SIOCPROTOPRIVATE + 0)
#define SIOCMCTPDROPTAG (SIOCPROTOPRIVATE + 1)
#define SIOCMCTPALLOCTAG2 (SIOCPROTOPRIVATE + 2)
#define SIOCMCTPDROPTAG2 (SIOCPROTOPRIVATE + 3)
/* Deprecated: use mctp_ioc_tag_ctl2 / TAG2 ioctls instead, which defines the
* MCTP network ID as part of the allocated tag. Using this assumes the default
* net ID for allocated tags, which may not give correct behaviour on system
* with multiple networks configured.
*/
struct mctp_ioc_tag_ctl {
mctp_eid_t peer_addr;
......@@ -65,4 +72,29 @@ struct mctp_ioc_tag_ctl {
__u16 flags;
};
struct mctp_ioc_tag_ctl2 {
/* Peer details: network ID, peer EID, local EID. All set by the
* caller.
*
* Local EID must be MCTP_ADDR_NULL or MCTP_ADDR_ANY in current
* kernels.
*/
unsigned int net;
mctp_eid_t peer_addr;
mctp_eid_t local_addr;
/* Set by caller, but no flags defined currently. Must be 0 */
__u16 flags;
/* For SIOCMCTPALLOCTAG2: must be passed as zero, kernel will
* populate with the allocated tag value. Returned tag value will
* always have TO and PREALLOC set.
*
* For SIOCMCTPDROPTAG2: userspace provides tag value to drop, from
* a prior SIOCMCTPALLOCTAG2 call (and so must have TO and PREALLOC set).
*/
__u8 tag;
};
#endif /* __UAPI_MCTP_H */
......@@ -6849,6 +6849,14 @@ static struct skb_ext *skb_ext_maybe_cow(struct skb_ext *old,
for (i = 0; i < sp->len; i++)
xfrm_state_hold(sp->xvec[i]);
}
#endif
#ifdef CONFIG_MCTP_FLOWS
if (old_active & (1 << SKB_EXT_MCTP)) {
struct mctp_flow *flow = skb_ext_get_ptr(old, SKB_EXT_MCTP);
if (flow->key)
refcount_inc(&flow->key->refs);
}
#endif
__skb_ext_put(old);
return new;
......
......@@ -14,6 +14,7 @@ menuconfig MCTP
config MCTP_TEST
bool "MCTP core tests" if !KUNIT_ALL_TESTS
select MCTP_FLOWS
depends on MCTP=y && KUNIT=y
default KUNIT_ALL_TESTS
......
......@@ -350,30 +350,102 @@ static int mctp_getsockopt(struct socket *sock, int level, int optname,
return -EINVAL;
}
static int mctp_ioctl_alloctag(struct mctp_sock *msk, unsigned long arg)
/* helpers for reading/writing the tag ioc, handling compatibility across the
* two versions, and some basic API error checking
*/
static int mctp_ioctl_tag_copy_from_user(unsigned long arg,
struct mctp_ioc_tag_ctl2 *ctl,
bool tagv2)
{
struct mctp_ioc_tag_ctl ctl_compat;
unsigned long size;
void *ptr;
int rc;
if (tagv2) {
size = sizeof(*ctl);
ptr = ctl;
} else {
size = sizeof(ctl_compat);
ptr = &ctl_compat;
}
rc = copy_from_user(ptr, (void __user *)arg, size);
if (rc)
return -EFAULT;
if (!tagv2) {
/* compat, using defaults for new fields */
ctl->net = MCTP_INITIAL_DEFAULT_NET;
ctl->peer_addr = ctl_compat.peer_addr;
ctl->local_addr = MCTP_ADDR_ANY;
ctl->flags = ctl_compat.flags;
ctl->tag = ctl_compat.tag;
}
if (ctl->flags)
return -EINVAL;
if (ctl->local_addr != MCTP_ADDR_ANY &&
ctl->local_addr != MCTP_ADDR_NULL)
return -EINVAL;
return 0;
}
static int mctp_ioctl_tag_copy_to_user(unsigned long arg,
struct mctp_ioc_tag_ctl2 *ctl,
bool tagv2)
{
struct mctp_ioc_tag_ctl ctl_compat;
unsigned long size;
void *ptr;
int rc;
if (tagv2) {
ptr = ctl;
size = sizeof(*ctl);
} else {
ctl_compat.peer_addr = ctl->peer_addr;
ctl_compat.tag = ctl->tag;
ctl_compat.flags = ctl->flags;
ptr = &ctl_compat;
size = sizeof(ctl_compat);
}
rc = copy_to_user((void __user *)arg, ptr, size);
if (rc)
return -EFAULT;
return 0;
}
static int mctp_ioctl_alloctag(struct mctp_sock *msk, bool tagv2,
unsigned long arg)
{
struct net *net = sock_net(&msk->sk);
struct mctp_sk_key *key = NULL;
struct mctp_ioc_tag_ctl ctl;
struct mctp_ioc_tag_ctl2 ctl;
unsigned long flags;
u8 tag;
int rc;
if (copy_from_user(&ctl, (void __user *)arg, sizeof(ctl)))
return -EFAULT;
rc = mctp_ioctl_tag_copy_from_user(arg, &ctl, tagv2);
if (rc)
return rc;
if (ctl.tag)
return -EINVAL;
if (ctl.flags)
return -EINVAL;
key = mctp_alloc_local_tag(msk, ctl.peer_addr, MCTP_ADDR_ANY,
true, &tag);
key = mctp_alloc_local_tag(msk, ctl.net, MCTP_ADDR_ANY,
ctl.peer_addr, true, &tag);
if (IS_ERR(key))
return PTR_ERR(key);
ctl.tag = tag | MCTP_TAG_OWNER | MCTP_TAG_PREALLOC;
if (copy_to_user((void __user *)arg, &ctl, sizeof(ctl))) {
rc = mctp_ioctl_tag_copy_to_user(arg, &ctl, tagv2);
if (rc) {
unsigned long fl2;
/* Unwind our key allocation: the keys list lock needs to be
* taken before the individual key locks, and we need a valid
......@@ -385,28 +457,27 @@ static int mctp_ioctl_alloctag(struct mctp_sock *msk, unsigned long arg)
__mctp_key_remove(key, net, fl2, MCTP_TRACE_KEY_DROPPED);
mctp_key_unref(key);
spin_unlock_irqrestore(&net->mctp.keys_lock, flags);
return -EFAULT;
return rc;
}
mctp_key_unref(key);
return 0;
}
static int mctp_ioctl_droptag(struct mctp_sock *msk, unsigned long arg)
static int mctp_ioctl_droptag(struct mctp_sock *msk, bool tagv2,
unsigned long arg)
{
struct net *net = sock_net(&msk->sk);
struct mctp_ioc_tag_ctl ctl;
struct mctp_ioc_tag_ctl2 ctl;
unsigned long flags, fl2;
struct mctp_sk_key *key;
struct hlist_node *tmp;
int rc;
u8 tag;
if (copy_from_user(&ctl, (void __user *)arg, sizeof(ctl)))
return -EFAULT;
if (ctl.flags)
return -EINVAL;
rc = mctp_ioctl_tag_copy_from_user(arg, &ctl, tagv2);
if (rc)
return rc;
/* Must be a local tag, TO set, preallocated */
if ((ctl.tag & ~MCTP_TAG_MASK) != (MCTP_TAG_OWNER | MCTP_TAG_PREALLOC))
......@@ -422,6 +493,7 @@ static int mctp_ioctl_droptag(struct mctp_sock *msk, unsigned long arg)
*/
spin_lock_irqsave(&key->lock, fl2);
if (key->manual_alloc &&
ctl.net == key->net &&
ctl.peer_addr == key->peer_addr &&
tag == key->tag) {
__mctp_key_remove(key, net, fl2,
......@@ -439,12 +511,17 @@ static int mctp_ioctl_droptag(struct mctp_sock *msk, unsigned long arg)
static int mctp_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
{
struct mctp_sock *msk = container_of(sock->sk, struct mctp_sock, sk);
bool tagv2 = false;
switch (cmd) {
case SIOCMCTPALLOCTAG2:
case SIOCMCTPALLOCTAG:
return mctp_ioctl_alloctag(msk, arg);
tagv2 = cmd == SIOCMCTPALLOCTAG2;
return mctp_ioctl_alloctag(msk, tagv2, arg);
case SIOCMCTPDROPTAG:
return mctp_ioctl_droptag(msk, arg);
case SIOCMCTPDROPTAG2:
tagv2 = cmd == SIOCMCTPDROPTAG2;
return mctp_ioctl_droptag(msk, tagv2, arg);
}
return -EINVAL;
......
......@@ -73,13 +73,50 @@ static struct mctp_sock *mctp_lookup_bind(struct net *net, struct sk_buff *skb)
return NULL;
}
static bool mctp_key_match(struct mctp_sk_key *key, mctp_eid_t local,
mctp_eid_t peer, u8 tag)
/* A note on the key allocations.
*
* struct net->mctp.keys contains our set of currently-allocated keys for
* MCTP tag management. The lookup tuple for these is the peer EID,
* local EID and MCTP tag.
*
* In some cases, the peer EID may be MCTP_EID_ANY: for example, when a
* broadcast message is sent, we may receive responses from any peer EID.
* Because the broadcast dest address is equivalent to ANY, we create
* a key with (local = local-eid, peer = ANY). This allows a match on the
* incoming broadcast responses from any peer.
*
* We perform lookups when packets are received, and when tags are allocated
* in two scenarios:
*
* - when a packet is sent, with a locally-owned tag: we need to find an
* unused tag value for the (local, peer) EID pair.
*
* - when a tag is manually allocated: we need to find an unused tag value
* for the peer EID, but don't have a specific local EID at that stage.
*
* in the latter case, on successful allocation, we end up with a tag with
* (local = ANY, peer = peer-eid).
*
* So, the key set allows both a local EID of ANY, as well as a peer EID of
* ANY in the lookup tuple. Both may be ANY if we prealloc for a broadcast.
* The matching (in mctp_key_match()) during lookup allows the match value to
* be ANY in either the dest or source addresses.
*
* When allocating (+ inserting) a tag, we need to check for conflicts amongst
* the existing tag set. This requires macthing either exactly on the local
* and peer addresses, or either being ANY.
*/
static bool mctp_key_match(struct mctp_sk_key *key, unsigned int net,
mctp_eid_t local, mctp_eid_t peer, u8 tag)
{
if (key->net != net)
return false;
if (!mctp_address_matches(key->local_addr, local))
return false;
if (key->peer_addr != peer)
if (!mctp_address_matches(key->peer_addr, peer))
return false;
if (key->tag != tag)
......@@ -92,7 +129,7 @@ static bool mctp_key_match(struct mctp_sk_key *key, mctp_eid_t local,
* key exists.
*/
static struct mctp_sk_key *mctp_lookup_key(struct net *net, struct sk_buff *skb,
mctp_eid_t peer,
unsigned int netid, mctp_eid_t peer,
unsigned long *irqflags)
__acquires(&key->lock)
{
......@@ -108,7 +145,7 @@ static struct mctp_sk_key *mctp_lookup_key(struct net *net, struct sk_buff *skb,
spin_lock_irqsave(&net->mctp.keys_lock, flags);
hlist_for_each_entry(key, &net->mctp.keys, hlist) {
if (!mctp_key_match(key, mh->dest, peer, tag))
if (!mctp_key_match(key, netid, mh->dest, peer, tag))
continue;
spin_lock(&key->lock);
......@@ -131,6 +168,7 @@ static struct mctp_sk_key *mctp_lookup_key(struct net *net, struct sk_buff *skb,
}
static struct mctp_sk_key *mctp_key_alloc(struct mctp_sock *msk,
unsigned int net,
mctp_eid_t local, mctp_eid_t peer,
u8 tag, gfp_t gfp)
{
......@@ -140,6 +178,7 @@ static struct mctp_sk_key *mctp_key_alloc(struct mctp_sock *msk,
if (!key)
return NULL;
key->net = net;
key->peer_addr = peer;
key->local_addr = local;
key->tag = tag;
......@@ -185,8 +224,8 @@ static int mctp_key_add(struct mctp_sk_key *key, struct mctp_sock *msk)
}
hlist_for_each_entry(tmp, &net->mctp.keys, hlist) {
if (mctp_key_match(tmp, key->local_addr, key->peer_addr,
key->tag)) {
if (mctp_key_match(tmp, key->net, key->local_addr,
key->peer_addr, key->tag)) {
spin_lock(&tmp->lock);
if (tmp->valid)
rc = -EEXIST;
......@@ -327,6 +366,7 @@ static int mctp_route_input(struct mctp_route *route, struct sk_buff *skb)
struct net *net = dev_net(skb->dev);
struct mctp_sock *msk;
struct mctp_hdr *mh;
unsigned int netid;
unsigned long f;
u8 tag, flags;
int rc;
......@@ -345,6 +385,7 @@ static int mctp_route_input(struct mctp_route *route, struct sk_buff *skb)
/* grab header, advance data ptr */
mh = mctp_hdr(skb);
netid = mctp_cb(skb)->net;
skb_pull(skb, sizeof(struct mctp_hdr));
if (mh->ver != 1)
......@@ -358,7 +399,7 @@ static int mctp_route_input(struct mctp_route *route, struct sk_buff *skb)
/* lookup socket / reasm context, exactly matching (src,dest,tag).
* we hold a ref on the key, and key->lock held.
*/
key = mctp_lookup_key(net, skb, mh->src, &f);
key = mctp_lookup_key(net, skb, netid, mh->src, &f);
if (flags & MCTP_HDR_FLAG_SOM) {
if (key) {
......@@ -368,8 +409,12 @@ static int mctp_route_input(struct mctp_route *route, struct sk_buff *skb)
* key lookup to find the socket, but don't use this
* key for reassembly - we'll create a more specific
* one for future packets if required (ie, !EOM).
*
* this lookup requires key->peer to be MCTP_ADDR_ANY,
* it doesn't match just any key->peer.
*/
any_key = mctp_lookup_key(net, skb, MCTP_ADDR_ANY, &f);
any_key = mctp_lookup_key(net, skb, netid,
MCTP_ADDR_ANY, &f);
if (any_key) {
msk = container_of(any_key->sk,
struct mctp_sock, sk);
......@@ -406,7 +451,7 @@ static int mctp_route_input(struct mctp_route *route, struct sk_buff *skb)
* packets for this message
*/
if (!key) {
key = mctp_key_alloc(msk, mh->dest, mh->src,
key = mctp_key_alloc(msk, netid, mh->dest, mh->src,
tag, GFP_ATOMIC);
if (!key) {
rc = -ENOMEM;
......@@ -596,11 +641,12 @@ static void mctp_reserve_tag(struct net *net, struct mctp_sk_key *key,
refcount_inc(&key->refs);
}
/* Allocate a locally-owned tag value for (saddr, daddr), and reserve
/* Allocate a locally-owned tag value for (local, peer), and reserve
* it for the socket msk
*/
struct mctp_sk_key *mctp_alloc_local_tag(struct mctp_sock *msk,
mctp_eid_t daddr, mctp_eid_t saddr,
unsigned int netid,
mctp_eid_t local, mctp_eid_t peer,
bool manual, u8 *tagp)
{
struct net *net = sock_net(&msk->sk);
......@@ -610,11 +656,11 @@ struct mctp_sk_key *mctp_alloc_local_tag(struct mctp_sock *msk,
u8 tagbits;
/* for NULL destination EIDs, we may get a response from any peer */
if (daddr == MCTP_ADDR_NULL)
daddr = MCTP_ADDR_ANY;
if (peer == MCTP_ADDR_NULL)
peer = MCTP_ADDR_ANY;
/* be optimistic, alloc now */
key = mctp_key_alloc(msk, saddr, daddr, 0, GFP_KERNEL);
key = mctp_key_alloc(msk, netid, local, peer, 0, GFP_KERNEL);
if (!key)
return ERR_PTR(-ENOMEM);
......@@ -631,12 +677,24 @@ struct mctp_sk_key *mctp_alloc_local_tag(struct mctp_sock *msk,
* lock held, they don't change over the lifetime of the key.
*/
/* tags are net-specific */
if (tmp->net != netid)
continue;
/* if we don't own the tag, it can't conflict */
if (tmp->tag & MCTP_HDR_FLAG_TO)
continue;
if (!(mctp_address_matches(tmp->peer_addr, daddr) &&
mctp_address_matches(tmp->local_addr, saddr)))
/* Since we're avoiding conflicting entries, match peer and
* local addresses, including with a wildcard on ANY. See
* 'A note on key allocations' for background.
*/
if (peer != MCTP_ADDR_ANY &&
!mctp_address_matches(tmp->peer_addr, peer))
continue;
if (local != MCTP_ADDR_ANY &&
!mctp_address_matches(tmp->local_addr, local))
continue;
spin_lock(&tmp->lock);
......@@ -671,6 +729,7 @@ struct mctp_sk_key *mctp_alloc_local_tag(struct mctp_sock *msk,
}
static struct mctp_sk_key *mctp_lookup_prealloc_tag(struct mctp_sock *msk,
unsigned int netid,
mctp_eid_t daddr,
u8 req_tag, u8 *tagp)
{
......@@ -685,6 +744,9 @@ static struct mctp_sk_key *mctp_lookup_prealloc_tag(struct mctp_sock *msk,
spin_lock_irqsave(&mns->keys_lock, flags);
hlist_for_each_entry(tmp, &mns->keys, hlist) {
if (tmp->net != netid)
continue;
if (tmp->tag != req_tag)
continue;
......@@ -843,6 +905,9 @@ static int mctp_do_fragment_route(struct mctp_route *rt, struct sk_buff *skb,
/* copy message payload */
skb_copy_bits(skb, pos, skb_transport_header(skb2), size);
/* we need to copy the extensions, for MCTP flow data */
skb_ext_copy(skb2, skb);
/* do route */
rc = rt->output(rt, skb2);
if (rc)
......@@ -865,6 +930,7 @@ int mctp_local_output(struct sock *sk, struct mctp_route *rt,
struct mctp_sk_key *key;
struct mctp_hdr *hdr;
unsigned long flags;
unsigned int netid;
unsigned int mtu;
mctp_eid_t saddr;
bool ext_rt;
......@@ -915,16 +981,17 @@ int mctp_local_output(struct sock *sk, struct mctp_route *rt,
rc = 0;
}
spin_unlock_irqrestore(&rt->dev->addrs_lock, flags);
netid = READ_ONCE(rt->dev->net);
if (rc)
goto out_release;
if (req_tag & MCTP_TAG_OWNER) {
if (req_tag & MCTP_TAG_PREALLOC)
key = mctp_lookup_prealloc_tag(msk, daddr,
key = mctp_lookup_prealloc_tag(msk, netid, daddr,
req_tag, &tag);
else
key = mctp_alloc_local_tag(msk, daddr, saddr,
key = mctp_alloc_local_tag(msk, netid, saddr, daddr,
false, &tag);
if (IS_ERR(key)) {
......
This diff is collapsed.
......@@ -4,6 +4,7 @@
#include <linux/mctp.h>
#include <linux/if_arp.h>
#include <net/mctp.h>
#include <net/mctpdevice.h>
#include <net/pkt_sched.h>
......@@ -54,6 +55,7 @@ struct mctp_test_dev *mctp_test_create_dev(void)
rcu_read_lock();
dev->mdev = __mctp_dev_get(ndev);
dev->mdev->net = mctp_default_net(dev_net(ndev));
rcu_read_unlock();
return dev;
......
......@@ -23,6 +23,7 @@ CONFIG_USB4=y
CONFIG_NET=y
CONFIG_MCTP=y
CONFIG_MCTP_FLOWS=y
CONFIG_INET=y
CONFIG_MPTCP=y
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment