mctp: Implement extended addressing
Jeremy Kerr
maintainerPATCH v3 1/1 mctp: Implement extended addressing
This change allows an extended address struct - struct sockaddr_mctp_ext
- to be passed to sendmsg/recvmsg. This allows userspace to specify output ifindex and physical address information (for sendmsg) or receive the input ifindex/physaddr for incoming messages (for recvmsg). This is typically used by userspace for MCTP address discovery and assignment operations.
The extended addressing facility is conditional on a new sockopt:MCTP_OPT_ADDR_EXT; userspace must explicitly enable addressing before the kernel will consume/populate the extended address data.
Includes a fix for an uninitialised var:
---
v3:
- avoid uninitialised rc if we hit the !rt->dev WARN.
v2:
- non-RFC
- learn to spell "typically"
RFC: this patch adds a bit of an new ABI, in that the struct sockaddr for MCTP has been extended, with extra addressing data being available for applications after setting a sockopt.
Using something like IP_PKTINFO might be suitable instead, but that requires sendmsg/recvmsg, and control message setup, whereas this is a a simpler interface, and also allows extended addressing info in sendto/recvfrom too. Comments most welcome!
--- include/net/mctp.h | 16 +++++-- include/uapi/linux/mctp.h | 13 ++++++ net/mctp/af_mctp.c | 86 ++++++++++++++++++++++++++++++---- net/mctp/route.c | 98 +++++++++++++++++++++++++++++---------- 4 files changed, 173 insertions(+), 40 deletions(-)
include/net/mctp.h +11 -5
@@ -58,6 +58,9 @@ struct mctp_sock {mctp_eid_t bind_addr;__u8 bind_type; +/* sendmsg()/recvmsg() uses struct sockaddr_mctp_ext */ +bool addr_ext; +/* list of mctp_sk_key, for incoming tag lookup. updates protected * by sk->net->keys_lock */
@@ -152,10 +155,16 @@ struct mctp_sk_key {struct mctp_skb_cb {unsigned int magic; -unsigned int net; +int net; +int ifindex;/* extended/direct addressing if set */ +unsigned char halen;mctp_eid_t src; +unsigned char haddr[]; }; +#define MCTP_SKB_CB_HADDR_MAXLEN (sizeof((((struct sk_buff *)(NULL))->cb)) \ + - offsetof(struct mctp_skb_cb, haddr)) +/* skb control-block accessors with a little extra debugging for initial * development. *
@@ -189,8 +198,7 @@ static inline struct mctp_skb_cb *mctp_cb(struct sk_buff *skb)* * Updates to the route table are performed under rtnl; all reads under RCU, * so routes cannot be referenced over a RCU grace period. Specifically: A -* caller cannot block between mctp_route_lookup and passing the route to -* mctp_do_route. +* caller cannot block between mctp_route_lookup and mctp_route_release() */ struct mctp_route {mctp_eid_t min, max;
@@ -210,8 +218,6 @@ struct mctp_route {struct mctp_route *mctp_route_lookup (struct net *net,unsigned int dnet,mctp_eid_t daddr); -int mctp_do_route (struct mctp_route *rt,struct sk_buff *skb); -int mctp_local_output (struct sock *sk,struct mctp_route *rt,struct sk_buff *skb,mctp_eid_t daddr,u8 req_tag);
include/uapi/linux/mctp.h +13 -0
@@ -10,6 +10,7 @@#define __UAPI_MCTP_H#include <linux/types.h> +#include <linux/netdevice.h> typedef __u8 mctp_eid_t ;
@@ -25,6 +26,13 @@ struct sockaddr_mctp {__u8 smctp_tag; }; +struct sockaddr_mctp_ext { +struct sockaddr_mctp smctp_base; +int smctp_ifindex; +unsigned char smctp_halen; +unsigned char smctp_haddr[MAX_ADDR_LEN ]; +}; +#define MCTP_NET_ANY 0x0#define MCTP_ADDR_NULL 0x00
@@ -33,4 +41,9 @@ struct sockaddr_mctp {#define MCTP_TAG_MASK 0x07#define MCTP_TAG_OWNER 0x08 +/* setsockopt(2) level & options */ +#define SOL_MCTP 0 + +#define MCTP_OPT_ADDR_EXT 1 +#endif /* __UAPI_MCTP_H */
net/mctp/af_mctp.c +76 -10
136 lines not on this page · raw message
net/mctp/route.c +73 -25
178 lines not on this page · raw message
Signature · 1 line
2.30.2
Quoted 10 lines · Jeremy Kerr
This change allows an extended address struct - struct
sockaddr_mctp_ext
- to be passed to sendmsg/recvmsg. This allows userspace to specify output ifindex and physical address information (for sendmsg) or receive the input ifindex/physaddr for incoming messages (for recvmsg). This is typically used by userspace for MCTP address discovery and assignment operations.The extended addressing facility is conditional on a new sockopt:
MCTP_OPT_ADDR_EXT; userspace must explicitly enable addressing before the kernel will consume/populate the extended address data.
[...]
include/uapi/linux/mctp.h struct sockaddr_mctp { · line 45
Quoted 2 lines
include/uapi/linux/mctp.h +2
-0+/* setsockopt(2) level & options */ +#define SOL_MCTP 0
Socket option levels tend to be globally unique and additionally defined in include/linux/socket.h (which led to them not being exposed to UAPI in many cases, but that is another, entirely avoiadable, problem), with Bluetooth socket option levels being most notable exception (and IEEE 802.15.4 and mISDN being less notable). So, unless there is existing code that relies on this socket level definition, it is probably worth re-defining it to 284 and put a copy of SOL_MCTP definition after SOL_XDP in include/linux/socket.h.
Jeremy Kerr
maintainerHi Eugene,
Quoted 3 lines
So, unless there is existing code that relies on this socket level definition, it is probably worth re-defining it to 284 and put a copy of
SOL_MCTPdefinition afterSOL_XDPininclude/linux/socket.h.
Nope, new ABI here, so we're fine to define it as we like.
I've sent a v4 with this moved to linux/socket.h, and dropped the uapi definition - as (like you've mentioned) there's not a lot of precedent for the copies under uapi.
Thanks for the review!
Cheers,
Jeremy