Skip to contentSkip to conversation

mctp: Implement extended addressing

PATCH Superseded Jeremy Kerr 3 participants 4 messages Oct 25, 2021 – Oct 26, 2021

Subsystems (4)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Jeremy Kerr

author maintainer
#0 lore raw

PATCH v5 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:

---
v5:
- rebase to net-next, where we've added explicit padding to struct sockaddr_mctp. Consequently, pad sockaddr_mctp_ext too, and unify types.

v4:
- move SOL_MCTP to linux/sockets.h

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/linux/socket.h    |  1 +
 include/net/mctp.h        | 16 +++++--
 include/uapi/linux/mctp.h | 11 +++++
 net/mctp/af_mctp.c        | 86 ++++++++++++++++++++++++++++++----
 net/mctp/route.c          | 98 +++++++++++++++++++++++++++++----------
 5 files changed, 172 insertions(+), 40 deletions(-)
include/linux/socket.h +1 -0
index 7612d760b6a9..8ef26d89ef49 100644
@@ -365,6 +365,7 @@ struct ucred {
 #define SOL_TLS		282
 #define SOL_XDP		283
 #define SOL_MPTCP	284
+#define SOL_MCTP	285
 
 /* IPX options */
 #define IPX_TYPE	1
include/net/mctp.h +11 -5
index 2a83443bdfac..6cd0789bc947 100644
@@ -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 +11 -0
index 6acd4ccafbf7..07b0318716fc 100644
@@ -11,6 +11,7 @@
 
 #include <linux/types.h>
 #include <linux/socket.h>
+#include <linux/netdevice.h>
 
 typedef __u8			mctp_eid_t;
 
@@ -28,6 +29,14 @@ struct sockaddr_mctp {
 	__u8			__smctp_pad1;
 };
 
+struct sockaddr_mctp_ext {
+	struct sockaddr_mctp	smctp_base;
+	int			smctp_ifindex;
+	__u8			smctp_halen;
+	__u8			__smctp_pad0[3];
+	__u8			smctp_haddr[MAX_ADDR_LEN];
+};
+
 #define MCTP_NET_ANY		0x0
 
 #define MCTP_ADDR_NULL		0x00
@@ -36,4 +45,6 @@ struct sockaddr_mctp {
 #define MCTP_TAG_MASK		0x07
 #define MCTP_TAG_OWNER		0x08
 
+#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
maintainer
#1 lore raw

From: Jeremy Kerr <[email protected]>
Date: Mon, 25 Oct 2021 11:27:57 +0800

include/net/mctp.h struct mctp_sk_key { · line 164

Quoted 12 lines
include/net/mctp.h +4 -1
@@ -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[];
 };
 

putting a variably sized type in the skb control blocxk is not a good idea. Overruns will be silent, nothing in the typing protects you from udsing more space han exists in skb->cb.

Plrease find another way, thank you.

#2 lore raw
Quoted 19 lines · David Miller

From: Jeremy Kerr <[email protected]>
Date: Mon, 25 Oct 2021 11:27:57 +0800

include/net/mctp.h struct mctp_sk_key { · line 164

Quoted 12 lines
include/net/mctp.h +4 -1
@@ -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[];
 };
 

putting a variably sized type in the skb control blocxk is not a good idea. Overruns will be silent, nothing in the typing protects you from udsing more space han exists in skb->cb.

Quoted 1 line

Plrease find another way, thank you.

haddr[MAX_ADDR_LEN]? It is defined to 32 bytes.

author maintainer
#3 lore raw
In reply to David Miller

Hi David,

Quoted 3 lines

putting a variably sized type in the skb control blocxk is not a good idea. Overruns will be silent, nothing in the typing protects you from udsing more space han exists in skb->cb.

There is a check using MCTP_SKB_CB_HADDR_MAXLEN, but setting the size of the struct member typing would allow for better enforcement, yes.

Quoted 1 line

Plrease find another way, thank you.

OK, we can use MAX_ADDR_LEN here; we currently have enough space in the cb for the maximum possible addressing sizes (and can check that through a BUILD_BUG_ON too).

v6 coming right up.

Cheers,

Jeremy