Skip to contentSkip to conversation

Add Management Component Transport Protocol support

PATCH Applied Jeremy Kerr 4 participants 23 messages Jul 29, 2021 – Oct 16, 2021

Subsystems (6)
lore
Share & tools
lore

Jeremy Kerr

author maintainer
#0 lore raw

v4 cover Add Management Component Transport Protocol support

This series adds core MCTP support to the kernel. From the Kconfig description:

Management Component Transport Protocol (MCTP) is an in-system protocol for communicating between management controllers and their managed devices (peripherals, host processors, etc.). The protocol is defined by DMTF specification DSP0236.

This option enables core MCTP support. For communicating with other devices, you'll want to enable a driver for a specific hardware channel.

This implementation allows a sockets-based API for sending and receiving MCTP messages via sendmsg/recvmsg on SOCK_DGRAM sockets. Kernel stack control is all via netlink, using existing RTM_* messages. The userspace ABI change is fairly small; just the necessary AF_/ETH_P_/ARPHDR_ constants, a new sockaddr, and a new netlink attribute.

For MAINTAINERS, I've just included netdev@ as the list entry. I'm happy to alter this based on preferences here - an alternative would be the OpenBMC list (the main user of the MCTP interface), or we can create a new list entirely.

We have a couple of interface drivers almost ready to go at the moment, but those can wait until the core code has some review.

This is v4 of the series; v1 and v2 were both RFC.

selinux folks: CCing 01/15 due to the new PF_MCTP protocol family.

linux-doc folks: CCing 15/15 for the new MCTP overview document.

Review, comments, questions etc. are most welcome.

Cheers,

Jeremy

v2:
- change to match spec terminology: controller -> component
- require specific capabilities for bind() & sendmsg()
- add address and tag defintions to uapi
- add selinux AF_MCTP table definitions
- remove strict cflags; warnings are present in common headers

v3:
- require caps for MCTP bind() & send()
- comment typo fixes
- switch to an array for local EIDs
- fix addrinfo dump iteration & error path
- add RTM_DELADDR
- remove GENMASK() and BIT() from uapi

v4:
- drop tun patch; that can be submitted separately
- keep nipa happy: add maintainer CCs, including doc and selinux
- net-next rebase
- Include AF_MCTP in af_family_slock_keys and pf_family_names
- Introduce MODULE_ definitions earlier
- upstream change: set_link_af no longer called with RTNL held
- add kdoc for net_device.mctp_ptr
- don't inline mctp_rt_match_eid
- require rtm_type == RTN_UNICAST in route management handlers
- remove unused RTAX policy table
- fix mctp_sock->keys rcu annotations
- fix spurious rcu_read_unlock in route input

Jeremy Kerr (10):
mctp: Add MCTP base
mctp: Add base socket/protocol definitions
mctp: Add base packet definitions
mctp: Add sockaddr_mctp to uapi
mctp: Add initial driver infrastructure
mctp: Add device handling and netlink interface
mctp: Add initial routing framework
mctp: Populate socket implementation
mctp: Implement message fragmentation & reassembly
mctp: Add MCTP overview document

Matt Johnston (6):
mctp: Add netlink route management
mctp: Add neighbour implementation
mctp: Add neighbour netlink interface
mctp: Add dest neighbour lladdr to route output
mctp: Allow per-netns default networks
mctp: Allow MCTP on tun devices

 Documentation/networking/index.rst  |    1 +
 Documentation/networking/mctp.rst   |  213 ++++++
 MAINTAINERS                         |   12 +
 drivers/net/Kconfig                 |    2 +
 drivers/net/Makefile                |    1 +
 drivers/net/mctp/Kconfig            |    8 +
 drivers/net/mctp/Makefile           |    0
 include/linux/netdevice.h           |    3 +
 include/linux/socket.h              |    6 +-
 include/net/mctp.h                  |  235 ++++++
 include/net/mctpdevice.h            |   36 +
 include/net/net_namespace.h         |    4 +
 include/net/netns/mctp.h            |   36 +
 include/uapi/linux/if_arp.h         |    1 +
 include/uapi/linux/if_ether.h       |    3 +
 include/uapi/linux/if_link.h        |   10 +
 include/uapi/linux/mctp.h           |   36 +
 net/Kconfig                         |    1 +
 net/Makefile                        |    1 +
 net/mctp/Kconfig                    |   13 +
 net/mctp/Makefile                   |    3 +
 net/mctp/af_mctp.c                  |  396 ++++++++++
 net/mctp/device.c                   |  427 +++++++++++
 net/mctp/neigh.c                    |  342 +++++++++
 net/mctp/route.c                    | 1095 +++++++++++++++++++++++++++
 security/selinux/hooks.c            |    4 +-
 security/selinux/include/classmap.h |    4 +-
 27 files changed, 2890 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/networking/mctp.rst
 create mode 100644 drivers/net/mctp/Kconfig
 create mode 100644 drivers/net/mctp/Makefile
 create mode 100644 include/net/mctp.h
 create mode 100644 include/net/mctpdevice.h
 create mode 100644 include/net/netns/mctp.h
 create mode 100644 include/uapi/linux/mctp.h
 create mode 100644 net/mctp/Kconfig
 create mode 100644 net/mctp/Makefile
 create mode 100644 net/mctp/af_mctp.c
 create mode 100644 net/mctp/device.c
 create mode 100644 net/mctp/neigh.c
 create mode 100644 net/mctp/route.c
Signature · 136 lines
2.30.2



Jeremy Kerr (10):
  mctp: Add MCTP base
  mctp: Add base socket/protocol definitions
  mctp: Add base packet definitions
  mctp: Add sockaddr_mctp to uapi
  mctp: Add initial driver infrastructure
  mctp: Add device handling and netlink interface
  mctp: Add initial routing framework
  mctp: Populate socket implementation
  mctp: Implement message fragmentation & reassembly
  mctp: Add MCTP overview document

Matt Johnston (5):
  mctp: Add netlink route management
  mctp: Add neighbour implementation
  mctp: Add neighbour netlink interface
  mctp: Add dest neighbour lladdr to route output
  mctp: Allow per-netns default networks

 Documentation/networking/index.rst  |    1 +
 Documentation/networking/mctp.rst   |  213 ++++++
 MAINTAINERS                         |   12 +
 drivers/net/Kconfig                 |    2 +
 drivers/net/Makefile                |    1 +
 drivers/net/mctp/Kconfig            |    8 +
 drivers/net/mctp/Makefile           |    0
 include/linux/netdevice.h           |    4 +
 include/linux/socket.h              |    6 +-
 include/net/mctp.h                  |  231 ++++++
 include/net/mctpdevice.h            |   36 +
 include/net/net_namespace.h         |    4 +
 include/net/netns/mctp.h            |   36 +
 include/uapi/linux/if_arp.h         |    1 +
 include/uapi/linux/if_ether.h       |    3 +
 include/uapi/linux/if_link.h        |   10 +
 include/uapi/linux/mctp.h           |   36 +
 net/Kconfig                         |    1 +
 net/Makefile                        |    1 +
 net/core/sock.c                     |    1 +
 net/mctp/Kconfig                    |   13 +
 net/mctp/Makefile                   |    3 +
 net/mctp/af_mctp.c                  |  396 ++++++++++
 net/mctp/device.c                   |  423 +++++++++++
 net/mctp/neigh.c                    |  342 +++++++++
 net/mctp/route.c                    | 1099 +++++++++++++++++++++++++++
 net/socket.c                        |    1 +
 security/selinux/hooks.c            |    4 +-
 security/selinux/include/classmap.h |    4 +-
 29 files changed, 2889 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/networking/mctp.rst
 create mode 100644 drivers/net/mctp/Kconfig
 create mode 100644 drivers/net/mctp/Makefile
 create mode 100644 include/net/mctp.h
 create mode 100644 include/net/mctpdevice.h
 create mode 100644 include/net/netns/mctp.h
 create mode 100644 include/uapi/linux/mctp.h
 create mode 100644 net/mctp/Kconfig
 create mode 100644 net/mctp/Makefile
 create mode 100644 net/mctp/af_mctp.c
 create mode 100644 net/mctp/device.c
 create mode 100644 net/mctp/neigh.c
 create mode 100644 net/mctp/route.c

-- 
2.30.2


Jeremy Kerr (10):
  mctp: Add MCTP base
  mctp: Add base socket/protocol definitions
  mctp: Add base packet definitions
  mctp: Add sockaddr_mctp to uapi
  mctp: Add initial driver infrastructure
  mctp: Add device handling and netlink interface
  mctp: Add initial routing framework
  mctp: Populate socket implementation
  mctp: Implement message fragmentation & reassembly
  mctp: Add MCTP overview document

Matt Johnston (5):
  mctp: Add netlink route management
  mctp: Add neighbour implementation
  mctp: Add neighbour netlink interface
  mctp: Add dest neighbour lladdr to route output
  mctp: Allow per-netns default networks

 Documentation/networking/index.rst  |    1 +
 Documentation/networking/mctp.rst   |  213 ++++++
 MAINTAINERS                         |   12 +
 drivers/net/Kconfig                 |    2 +
 drivers/net/Makefile                |    1 +
 drivers/net/mctp/Kconfig            |    8 +
 drivers/net/mctp/Makefile           |    0
 include/linux/netdevice.h           |    4 +
 include/linux/socket.h              |    6 +-
 include/net/mctp.h                  |  231 ++++++
 include/net/mctpdevice.h            |   36 +
 include/net/net_namespace.h         |    4 +
 include/net/netns/mctp.h            |   36 +
 include/uapi/linux/if_arp.h         |    1 +
 include/uapi/linux/if_ether.h       |    3 +
 include/uapi/linux/if_link.h        |   10 +
 include/uapi/linux/mctp.h           |   36 +
 net/Kconfig                         |    1 +
 net/Makefile                        |    1 +
 net/core/sock.c                     |    1 +
 net/mctp/Kconfig                    |   13 +
 net/mctp/Makefile                   |    3 +
 net/mctp/af_mctp.c                  |  396 ++++++++++
 net/mctp/device.c                   |  423 +++++++++++
 net/mctp/neigh.c                    |  342 +++++++++
 net/mctp/route.c                    | 1099 +++++++++++++++++++++++++++
 net/socket.c                        |    1 +
 security/selinux/hooks.c            |    4 +-
 security/selinux/include/classmap.h |    4 +-
 29 files changed, 2889 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/networking/mctp.rst
 create mode 100644 drivers/net/mctp/Kconfig
 create mode 100644 drivers/net/mctp/Makefile
 create mode 100644 include/net/mctp.h
 create mode 100644 include/net/mctpdevice.h
 create mode 100644 include/net/netns/mctp.h
 create mode 100644 include/uapi/linux/mctp.h
 create mode 100644 net/mctp/Kconfig
 create mode 100644 net/mctp/Makefile
 create mode 100644 net/mctp/af_mctp.c
 create mode 100644 net/mctp/device.c
 create mode 100644 net/mctp/neigh.c
 create mode 100644 net/mctp/route.c

-- 
2.30.2
author maintainer
#1 lore raw

PATCH v4 1/15 mctp: Add MCTP base

Add basic Kconfig, an initial (empty) af_mctp source object, and {AF,PF}_MCTP definitions, and the required definitions for a new protocol type.

---
v2:
- Add Linux-syscall-note to uapi header
- Add selinux defs for new AF_MCTP
- Controller -> component
- Don't use strict cflags; warnings are present in #includes
v4:
- Include AF_MCTP in af_family_slock_keys and pf_family_names
- Introduce MODULE_ definitions earlier

---
 MAINTAINERS                         |  7 +++++++
 include/linux/socket.h              |  6 +++++-
 include/uapi/linux/mctp.h           | 15 +++++++++++++++
 net/Kconfig                         |  1 +
 net/Makefile                        |  1 +
 net/core/sock.c                     |  1 +
 net/mctp/Kconfig                    | 13 +++++++++++++
 net/mctp/Makefile                   |  3 +++
 net/mctp/af_mctp.c                  | 13 +++++++++++++
 net/socket.c                        |  1 +
 security/selinux/hooks.c            |  4 +++-
 security/selinux/include/classmap.h |  4 +++-
 12 files changed, 66 insertions(+), 3 deletions(-)
 create mode 100644 include/uapi/linux/mctp.h
 create mode 100644 net/mctp/Kconfig
 create mode 100644 net/mctp/Makefile
 create mode 100644 net/mctp/af_mctp.c
MAINTAINERS +7 -0
index 4c32a9c532b7..22a1ff9afd9d 100644
@@ -11032,6 +11032,13 @@ F:	drivers/mailbox/arm_mhuv2.c
 F:	include/linux/mailbox/arm_mhuv2_message.h
 F:	Documentation/devicetree/bindings/mailbox/arm,mhuv2.yaml
 
+MANAGEMENT COMPONENT TRANSPORT PROTOCOL (MCTP)
+M:	Jeremy Kerr <[email protected]>
+M:	Matt Johnston <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	net/mctp/
+
 MAN-PAGES: MANUAL PAGES FOR LINUX -- Sections 2, 3, 4, 5, and 7
 M:	Michael Kerrisk <[email protected]>
 L:	[email protected]
include/linux/socket.h +5 -1
index 0d8e3dcb7f88..fd9ce51582d8 100644
@@ -223,8 +223,11 @@ struct ucred {
 				 * reuses AF_INET address family
 				 */
 #define AF_XDP		44	/* XDP sockets			*/
+#define AF_MCTP		45	/* Management component
+				 * transport protocol
+				 */
 
-#define AF_MAX		45	/* For now.. */
+#define AF_MAX		46	/* For now.. */
 
 /* Protocol families, same as address families. */
 #define PF_UNSPEC	AF_UNSPEC
@@ -274,6 +277,7 @@ struct ucred {
 #define PF_QIPCRTR	AF_QIPCRTR
 #define PF_SMC		AF_SMC
 #define PF_XDP		AF_XDP
+#define PF_MCTP		AF_MCTP
 #define PF_MAX		AF_MAX
 
 /* Maximum queue length specifiable by listen.  */
include/uapi/linux/mctp.h new file +15 -0
new file mode 100644
index 000000000000..2640a589c14c
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * Management Component Transport Protocol (MCTP)
+ *
+ * Copyright (c) 2021 Code Construct
+ * Copyright (c) 2021 Google
+ */
+
+#ifndef __UAPI_MCTP_H
+#define __UAPI_MCTP_H
+
+struct sockaddr_mctp {
+};
+
+#endif /* __UAPI_MCTP_H */
net/Kconfig +1 -0
index c7392c449b25..fb13460c6dab 100644
@@ -363,6 +363,7 @@ source "net/bluetooth/Kconfig"
 source "net/rxrpc/Kconfig"
 source "net/kcm/Kconfig"
 source "net/strparser/Kconfig"
+source "net/mctp/Kconfig"
 
 config FIB_RULES
 	bool
net/Makefile +1 -0
index 9ca9572188fe..fbfeb8a0bb37 100644
@@ -78,3 +78,4 @@ obj-$(CONFIG_QRTR)		+= qrtr/
 obj-$(CONFIG_NET_NCSI)		+= ncsi/
 obj-$(CONFIG_XDP_SOCKETS)	+= xdp/
 obj-$(CONFIG_MPTCP)		+= mptcp/
+obj-$(CONFIG_MCTP)		+= mctp/
net/core/sock.c +1 -0
index a3eea6e0b30a..9671c32e6ef5 100644
@@ -226,6 +226,7 @@ static struct lock_class_key af_family_kern_slock_keys[AF_MAX];
   x "AF_IEEE802154",	x "AF_CAIF"	,	x "AF_ALG"      , \
   x "AF_NFC"   ,	x "AF_VSOCK"    ,	x "AF_KCM"      , \
   x "AF_QIPCRTR",	x "AF_SMC"	,	x "AF_XDP"	, \
+  x "AF_MCTP"  , \
   x "AF_MAX"
 
 static const char *const af_family_key_strings[AF_MAX+1] = {
net/mctp/Kconfig new file +13 -0
new file mode 100644
index 000000000000..2cdf3d0a28c9
@@ -0,0 +1,13 @@
+
+menuconfig MCTP
+	depends on NET
+	tristate "MCTP core protocol support"
+	help
+	  Management Component Transport Protocol (MCTP) is an in-system
+	  protocol for communicating between management controllers and
+	  their managed devices (peripherals, host processors, etc.). The
+	  protocol is defined by DMTF specification DSP0236.
+
+	  This option enables core MCTP support. For communicating with other
+	  devices, you'll want to enable a driver for a specific hardware
+	  channel.
net/mctp/Makefile new file +3 -0
new file mode 100644
index 000000000000..7c056b1b7939
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_MCTP) += mctp.o
+mctp-objs := af_mctp.o
net/mctp/af_mctp.c new file +13 -0
new file mode 100644
index 000000000000..8f9c77e97357
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Management Component Transport Protocol (MCTP)
+ *
+ * Copyright (c) 2021 Code Construct
+ * Copyright (c) 2021 Google
+ */
+
+#include <linux/module.h>
+
+MODULE_DESCRIPTION("MCTP core");
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Jeremy Kerr <[email protected]>");
net/socket.c +1 -0
index 42665bd99ea4..3c10504e46d9 100644
@@ -212,6 +212,7 @@ static const char * const pf_family_names[] = {
 	[PF_QIPCRTR]	= "PF_QIPCRTR",
 	[PF_SMC]	= "PF_SMC",
 	[PF_XDP]	= "PF_XDP",
+	[PF_MCTP]	= "PF_MCTP",
 };
 
 /*
security/selinux/hooks.c +3 -1
index b0032c42333e..2143f590e3d6 100644
@@ -1330,7 +1330,9 @@ static inline u16 socket_type_to_security_class(int family, int type, int protoc
 			return SECCLASS_SMC_SOCKET;
 		case PF_XDP:
 			return SECCLASS_XDP_SOCKET;
-#if PF_MAX > 45
+		case PF_MCTP:
+			return SECCLASS_MCTP_SOCKET;
+#if PF_MAX > 46
 #error New address family defined, please update this function.
 #endif
 		}
security/selinux/include/classmap.h +3 -1
index 62d19bccf3de..084757ff4390 100644
@@ -246,6 +246,8 @@ struct security_class_mapping secclass_map[] = {
 	    NULL } },
 	{ "xdp_socket",
 	  { COMMON_SOCK_PERMS, NULL } },
+	{ "mctp_socket",
+	  { COMMON_SOCK_PERMS, NULL } },
 	{ "perf_event",
 	  { "open", "cpu", "kernel", "tracepoint", "read", "write", NULL } },
 	{ "lockdown",
@@ -255,6 +257,6 @@ struct security_class_mapping secclass_map[] = {
 	{ NULL }
   };
 
-#if PF_MAX > 45
+#if PF_MAX > 46
 #error New address family defined, please update secclass_map.
 #endif
Signature · 1 line
2.30.2
author maintainer
#2 lore raw

PATCH v4 2/15 mctp: Add base socket/protocol definitions

In reply to Jeremy Kerr

Add an empty socket implementation, plus initialisation/destruction handlers.

---
v2:
- Controller -> component

---
 net/mctp/af_mctp.c | 163 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 163 insertions(+)
net/mctp/af_mctp.c +163 -0
index 8f9c77e97357..b3aeca6486e3 100644
@@ -6,8 +6,171 @@
  * Copyright (c) 2021 Google
  */
 
+#include <linux/net.h>
+#include <linux/mctp.h>
 #include <linux/module.h>
+#include <linux/socket.h>
+
+#include <net/sock.h>
+
+struct mctp_sock {
+	struct sock	sk;
+};
+
+static int mctp_release(struct socket *sock)
+{
+	struct sock *sk = sock->sk;
+
+	if (sk) {
+		sock->sk = NULL;
+		sk->sk_prot->close(sk, 0);
+	}
+
+	return 0;
+}
+
+static int mctp_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
+{
+	return 0;
+}
+
+static int mctp_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
+{
+	return 0;
+}
+
+static int mctp_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
+			int flags)
+{
+	return 0;
+}
+
+static int mctp_setsockopt(struct socket *sock, int level, int optname,
+			   sockptr_t optval, unsigned int optlen)
+{
+	return -EINVAL;
+}
+
+static int mctp_getsockopt(struct socket *sock, int level, int optname,
+			   char __user *optval, int __user *optlen)
+{
+	return -EINVAL;
+}
+
+static const struct proto_ops mctp_dgram_ops = {
+	.family		= PF_MCTP,
+	.release	= mctp_release,
+	.bind		= mctp_bind,
+	.connect	= sock_no_connect,
+	.socketpair	= sock_no_socketpair,
+	.accept		= sock_no_accept,
+	.getname	= sock_no_getname,
+	.poll		= datagram_poll,
+	.ioctl		= sock_no_ioctl,
+	.gettstamp	= sock_gettstamp,
+	.listen		= sock_no_listen,
+	.shutdown	= sock_no_shutdown,
+	.setsockopt	= mctp_setsockopt,
+	.getsockopt	= mctp_getsockopt,
+	.sendmsg	= mctp_sendmsg,
+	.recvmsg	= mctp_recvmsg,
+	.mmap		= sock_no_mmap,
+	.sendpage	= sock_no_sendpage,
+};
+
+static void mctp_sk_close(struct sock *sk, long timeout)
+{
+	sk_common_release(sk);
+}
+
+static struct proto mctp_proto = {
+	.name		= "MCTP",
+	.owner		= THIS_MODULE,
+	.obj_size	= sizeof(struct mctp_sock),
+	.close		= mctp_sk_close,
+};
+
+static int mctp_pf_create(struct net *net, struct socket *sock,
+			  int protocol, int kern)
+{
+	const struct proto_ops *ops;
+	struct proto *proto;
+	struct sock *sk;
+	int rc;
+
+	if (protocol)
+		return -EPROTONOSUPPORT;
+
+	/* only datagram sockets are supported */
+	if (sock->type != SOCK_DGRAM)
+		return -ESOCKTNOSUPPORT;
+
+	proto = &mctp_proto;
+	ops = &mctp_dgram_ops;
+
+	sock->state = SS_UNCONNECTED;
+	sock->ops = ops;
+
+	sk = sk_alloc(net, PF_MCTP, GFP_KERNEL, proto, kern);
+	if (!sk)
+		return -ENOMEM;
+
+	sock_init_data(sock, sk);
+
+	rc = 0;
+	if (sk->sk_prot->init)
+		rc = sk->sk_prot->init(sk);
+
+	if (rc)
+		goto err_sk_put;
+
+	return 0;
+
+err_sk_put:
+	sock_orphan(sk);
+	sock_put(sk);
+	return rc;
+}
+
+static struct net_proto_family mctp_pf = {
+	.family = PF_MCTP,
+	.create = mctp_pf_create,
+	.owner = THIS_MODULE,
+};
+
+static __init int mctp_init(void)
+{
+	int rc;
+
+	pr_info("mctp: management component transport protocol core\n");
+
+	rc = sock_register(&mctp_pf);
+	if (rc)
+		return rc;
+
+	rc = proto_register(&mctp_proto, 0);
+	if (rc)
+		goto err_unreg_sock;
+
+	return 0;
+
+err_unreg_sock:
+	sock_unregister(PF_MCTP);
+
+	return rc;
+}
+
+static __exit void mctp_exit(void)
+{
+	proto_unregister(&mctp_proto);
+	sock_unregister(PF_MCTP);
+}
+
+module_init(mctp_init);
+module_exit(mctp_exit);
 
 MODULE_DESCRIPTION("MCTP core");
 MODULE_LICENSE("GPL v2");
 MODULE_AUTHOR("Jeremy Kerr <[email protected]>");
+
+MODULE_ALIAS_NETPROTO(PF_MCTP);
Signature · 1 line
2.30.2
author maintainer
#3 lore raw

PATCH v4 3/15 mctp: Add base packet definitions

In reply to Jeremy Kerr

Simple packet header format as defined by DMTF DSP0236.

---
v2:
- Controller -> component

---
 MAINTAINERS        |  1 +
 include/net/mctp.h | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)
 create mode 100644 include/net/mctp.h
MAINTAINERS +1 -0
index 22a1ff9afd9d..770b986d10f0 100644
@@ -11037,6 +11037,7 @@ M:	Jeremy Kerr <[email protected]>
 M:	Matt Johnston <[email protected]>
 L:	[email protected]
 S:	Maintained
+F:	include/net/mctp.h
 F:	net/mctp/
 
 MAN-PAGES: MANUAL PAGES FOR LINUX -- Sections 2, 3, 4, 5, and 7
include/net/mctp.h new file +35 -0
new file mode 100644
index 000000000000..4c01e083be45
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Management Component Transport Protocol (MCTP)
+ *
+ * Copyright (c) 2021 Code Construct
+ * Copyright (c) 2021 Google
+ */
+
+#ifndef __NET_MCTP_H
+#define __NET_MCTP_H
+
+#include <linux/bits.h>
+
+/* MCTP packet definitions */
+struct mctp_hdr {
+	u8	ver;
+	u8	dest;
+	u8	src;
+	u8	flags_seq_tag;
+};
+
+#define MCTP_VER_MIN	1
+#define MCTP_VER_MAX	1
+
+/* Definitions for flags_seq_tag field */
+#define MCTP_HDR_FLAG_SOM	BIT(7)
+#define MCTP_HDR_FLAG_EOM	BIT(6)
+#define MCTP_HDR_FLAG_TO	BIT(3)
+#define MCTP_HDR_FLAGS		GENMASK(5, 3)
+#define MCTP_HDR_SEQ_SHIFT	4
+#define MCTP_HDR_SEQ_MASK	GENMASK(1, 0)
+#define MCTP_HDR_TAG_SHIFT	0
+#define MCTP_HDR_TAG_MASK	GENMASK(2, 0)
+
+#endif /* __NET_MCTP_H */
Signature · 1 line
2.30.2
author maintainer
#4 lore raw

PATCH v4 4/15 mctp: Add sockaddr_mctp to uapi

In reply to Jeremy Kerr

This change introduces the user-visible MCTP header, containing the protocol-specific addressing definitions.

---
v2:
- include MCTP_ADDR_NULL and MCTP_TAG_* definitions
v3:
- don't use GENMASK/BIT in uapi

---
 include/uapi/linux/mctp.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
include/uapi/linux/mctp.h +21 -0
index 2640a589c14c..52b54d13f385 100644
@@ -9,7 +9,28 @@
 #ifndef __UAPI_MCTP_H
 #define __UAPI_MCTP_H
 
+#include <linux/types.h>
+
+typedef __u8			mctp_eid_t;
+
+struct mctp_addr {
+	mctp_eid_t		s_addr;
+};
+
 struct sockaddr_mctp {
+	unsigned short int	smctp_family;
+	int			smctp_network;
+	struct mctp_addr	smctp_addr;
+	__u8			smctp_type;
+	__u8			smctp_tag;
 };
 
+#define MCTP_NET_ANY		0x0
+
+#define MCTP_ADDR_NULL		0x00
+#define MCTP_ADDR_ANY		0xff
+
+#define MCTP_TAG_MASK		0x07
+#define MCTP_TAG_OWNER		0x08
+
 #endif /* __UAPI_MCTP_H */
Signature · 1 line
2.30.2
#5 lore raw

Re: [PATCH net-next v4 04/15] mctp: Add sockaddr_mctp to uapi

Quoted 2 lines · Jeremy Kerr

This change introduces the user-visible MCTP header, containing the protocol-specific addressing definitions.

[...]

Quoted 6 lines
include/uapi/linux/mctp.h +0 -0
@@ -9,7 +9,28 @@
 #ifndef __UAPI_MCTP_H
 #define __UAPI_MCTP_H
 

include/uapi/linux/mctp.h line 21

Quoted 10 lines
include/uapi/linux/mctp.h +9 -0
+#include <linux/types.h>
+
+typedef __u8			mctp_eid_t;
+
+struct mctp_addr {
+	mctp_eid_t		s_addr;
+};
+
 struct sockaddr_mctp {
+	unsigned short int	smctp_family;

This gap makes the size of struct sockaddr_mctp 2 bytes less at least on m68k, are you fine with that?

Quoted 5 lines
include/uapi/linux/mctp.h +4 -0
+	int			smctp_network;
+	struct mctp_addr	smctp_addr;
+	__u8			smctp_type;
+	__u8			smctp_tag;
 };
author maintainer
#6 lore raw

Re: [PATCH net-next v4 04/15] mctp: Add sockaddr_mctp to uapi

Hi Eugene,

Thanks for taking a look at these!

Quoted 11 lines
Quoted 8 lines
include/uapi/linux/mctp.h +6 -0
+typedef __u8                   mctp_eid_t;
+
+struct mctp_addr {
+       mctp_eid_t              s_addr;
+};
+

 struct sockaddr_mctp {
+       unsigned short int      smctp_family;

This gap makes the size of struct sockaddr_mctp 2 bytes less at least on m68k, are you fine with that?

Yep, that's OK from the protocol implementation side; this layout better matches the "hierarchy" of the MCTP addressing. If we go for optimal packing, the order of the members makes somewhat less sense. We could add padding members, but I'm not sure that's worth it...

I noticed a few other protocol implementations doing similar things, so assume it isn't an issue - it's all arch-specific ABI anyway, right?

Quoted 5 lines
Quoted 5 lines

+       int                     smctp_network;
+       struct mctp_addr        smctp_addr;
+       __u8                    smctp_type;
+       __u8                    smctp_tag;
 };

Cheers,

Jeremy

#7 lore raw

Re: [PATCH net-next v4 04/15] mctp: Add sockaddr_mctp to uapi

In reply to Eugene Syromiatnikov
Quoted 12 lines · Eugene Syromiatnikov
Quoted 2 lines · Jeremy Kerr

This change introduces the user-visible MCTP header, containing the protocol-specific addressing definitions.

[...]

Quoted 6 lines
include/uapi/linux/mctp.h +0 -0
@@ -9,7 +9,28 @@
 #ifndef __UAPI_MCTP_H
 #define __UAPI_MCTP_H
 
Quoted 18 lines

include/uapi/linux/mctp.h line 21

Quoted 10 lines
include/uapi/linux/mctp.h +9 -0
+#include <linux/types.h>
+
+typedef __u8                 mctp_eid_t;
+
+struct mctp_addr {
+     mctp_eid_t              s_addr;
+};
+
 struct sockaddr_mctp {
+     unsigned short int      smctp_family;

This gap makes the size of struct sockaddr_mctp 2 bytes less at least on m68k, are you fine with that?

Quoted 4 lines
include/uapi/linux/mctp.h +4 -0
+     int                     smctp_network;
+     struct mctp_addr        smctp_addr;
+     __u8                    smctp_type;
+     __u8                    smctp_tag;

And it may be wise to add 1 byte of explicit padding here? Or is there a good reason not to do so?

Quoted 1 line

include/uapi/linux/mctp.h line 26

Quoted 1 line
include/uapi/linux/mctp.h +0 -0
 };

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#8 lore raw

Re: [PATCH net-next v4 04/15] mctp: Add sockaddr_mctp to uapi

In reply to Jeremy Kerr

include/uapi/linux/mctp.h line 22

Quoted 3 lines · Jeremy Kerr

struct sockaddr_mctp {

include/uapi/linux/mctp.h +2 -0
+	unsigned short int	smctp_family;
+	int			smctp_network;

struct mctp_skb_cb.net (as well as struct mctp_dev.net) are unsigned, is it intentional that this field (along with struct mctp_sock.bind_net) differs in signedness?

author maintainer
#9 lore raw

Re: [PATCH net-next v4 04/15] mctp: Add sockaddr_mctp to uapi

Hi Eugene,

Quoted 8 lines · Jeremy Kerr
Quoted 3 lines

 struct sockaddr_mctp {
+       unsigned short int      smctp_family;
+       int                     smctp_network;

struct mctp_skb_cb.net (as well as struct mctp_dev.net) are unsigned, is it intentional that this field (along with struct mctp_sock.bind_net) differs in signedness?

No, that's not intentional - I'll submit a patch to unify those.

Thanks for the review.

Cheers,

Jeremy

author maintainer
#10 lore raw

PATCH v4 5/15 mctp: Add initial driver infrastructure

In reply to Jeremy Kerr

Add an empty drivers/net/mctp/, for future interface drivers.

---
 MAINTAINERS                 | 1 +
 drivers/net/Kconfig         | 2 ++
 drivers/net/Makefile        | 1 +
 drivers/net/mctp/Kconfig    | 8 ++++++++
 drivers/net/mctp/Makefile   | 0
 include/uapi/linux/if_arp.h | 1 +
 6 files changed, 13 insertions(+)
 create mode 100644 drivers/net/mctp/Kconfig
 create mode 100644 drivers/net/mctp/Makefile
MAINTAINERS +1 -0
index 770b986d10f0..e95eb3b00cd2 100644
@@ -11037,6 +11037,7 @@ M:	Jeremy Kerr <[email protected]>
 M:	Matt Johnston <[email protected]>
 L:	[email protected]
 S:	Maintained
+F:	drivers/net/mctp/
 F:	include/net/mctp.h
 F:	net/mctp/
 
drivers/net/Kconfig +2 -0
index 6977f8248df7..56213a8a1ec5 100644
@@ -483,6 +483,8 @@ config NET_SB1000
 
 source "drivers/net/phy/Kconfig"
 
+source "drivers/net/mctp/Kconfig"
+
 source "drivers/net/mdio/Kconfig"
 
 source "drivers/net/pcs/Kconfig"
drivers/net/Makefile +1 -0
index 7ffd2d03efaf..a48a664605a3 100644
@@ -69,6 +69,7 @@ obj-$(CONFIG_WAN) += wan/
 obj-$(CONFIG_WLAN) += wireless/
 obj-$(CONFIG_IEEE802154) += ieee802154/
 obj-$(CONFIG_WWAN) += wwan/
+obj-$(CONFIG_MCTP) += mctp/
 
 obj-$(CONFIG_VMXNET3) += vmxnet3/
 obj-$(CONFIG_XEN_NETDEV_FRONTEND) += xen-netfront.o
drivers/net/mctp/Kconfig new file +8 -0
new file mode 100644
index 000000000000..d8f966cedc89
@@ -0,0 +1,8 @@
+
+if MCTP
+
+menu "MCTP Device Drivers"
+
+endmenu
+
+endif
drivers/net/mctp/Makefile new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
include/uapi/linux/if_arp.h +1 -0
index c3cc5a9e5eaf..4783af9fe520 100644
@@ -54,6 +54,7 @@
 #define ARPHRD_X25	271		/* CCITT X.25			*/
 #define ARPHRD_HWX25	272		/* Boards with X.25 in firmware	*/
 #define ARPHRD_CAN	280		/* Controller Area Network      */
+#define ARPHRD_MCTP	290
 #define ARPHRD_PPP	512
 #define ARPHRD_CISCO	513		/* Cisco HDLC	 		*/
 #define ARPHRD_HDLC	ARPHRD_CISCO
Signature · 1 line
2.30.2
author maintainer
#11 lore raw

PATCH v4 6/15 mctp: Add device handling and netlink interface

In reply to Jeremy Kerr

This change adds the infrastructure for managing MCTP netdevices; we add a pointer to the AF_MCTP-specific data to struct netdevice, and hook up the rtnetlink operations for adding and removing addresses.

Includes changes from Matt Johnston <[email protected]>.

---
v2:
- controller -> component
v3:
- fix addrinfo dump iteration & error path
- fix non-negative ERR_PTR
- use an array for local EIDs
- remove unnecessary RTM_GETLINK unregister
- add RTM_DELADDR
v4:
- upstream change: set_link_af no longer called with RTNL held
- add kdoc for net_device.mctp_ptr

---
 MAINTAINERS                   |   1 +
 include/linux/netdevice.h     |   4 +
 include/net/mctp.h            |  14 ++
 include/net/mctpdevice.h      |  35 +++
 include/uapi/linux/if_ether.h |   3 +
 include/uapi/linux/if_link.h  |  10 +
 include/uapi/linux/mctp.h     |   1 +
 net/mctp/Makefile             |   2 +-
 net/mctp/af_mctp.c            |   8 +
 net/mctp/device.c             | 414 ++++++++++++++++++++++++++++++++++
 10 files changed, 491 insertions(+), 1 deletion(-)
 create mode 100644 include/net/mctpdevice.h
 create mode 100644 net/mctp/device.c
MAINTAINERS +1 -0
index e95eb3b00cd2..c2943c227ee6 100644
@@ -11039,6 +11039,7 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/net/mctp/
 F:	include/net/mctp.h
+F:	include/net/mctpdevice.h
 F:	net/mctp/
 
 MAN-PAGES: MANUAL PAGES FOR LINUX -- Sections 2, 3, 4, 5, and 7
include/linux/netdevice.h +4 -0
index 226bbee06730..d63a94ecbf3b 100644
@@ -1823,6 +1823,7 @@ enum netdev_ml_priv_type {
  *	@ieee802154_ptr: IEEE 802.15.4 low-rate Wireless Personal Area Network
  *			 device struct
  *	@mpls_ptr:	mpls_dev struct pointer
+ *	@mctp_ptr:	MCTP specific data
  *
  *	@dev_addr:	Hw address (before bcast,
  *			because most packets are unicast)
@@ -2110,6 +2111,9 @@ struct net_device {
 #if IS_ENABLED(CONFIG_MPLS_ROUTING)
 	struct mpls_dev __rcu	*mpls_ptr;
 #endif
+#if IS_ENABLED(CONFIG_MCTP)
+	struct mctp_dev __rcu	*mctp_ptr;
+#endif
 
 /*
  * Cache lines mostly used on receive path (including eth_type_trans())
include/net/mctp.h +14 -0
index 4c01e083be45..61452e03aa85 100644
@@ -10,6 +10,7 @@
 #define __NET_MCTP_H
 
 #include <linux/bits.h>
+#include <linux/mctp.h>
 
 /* MCTP packet definitions */
 struct mctp_hdr {
@@ -32,4 +33,17 @@ struct mctp_hdr {
 #define MCTP_HDR_TAG_SHIFT	0
 #define MCTP_HDR_TAG_MASK	GENMASK(2, 0)
 
+static inline bool mctp_address_ok(mctp_eid_t eid)
+{
+	return eid >= 8 && eid < 255;
+}
+
+static inline struct mctp_hdr *mctp_hdr(struct sk_buff *skb)
+{
+	return (struct mctp_hdr *)skb_network_header(skb);
+}
+
+void mctp_device_init(void);
+void mctp_device_exit(void);
+
 #endif /* __NET_MCTP_H */
include/net/mctpdevice.h new file +35 -0
new file mode 100644
index 000000000000..71a11012fac7
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Management Component Transport Protocol (MCTP) - device
+ * definitions.
+ *
+ * Copyright (c) 2021 Code Construct
+ * Copyright (c) 2021 Google
+ */
+
+#ifndef __NET_MCTPDEVICE_H
+#define __NET_MCTPDEVICE_H
+
+#include <linux/list.h>
+#include <linux/types.h>
+#include <linux/refcount.h>
+
+struct mctp_dev {
+	struct net_device	*dev;
+
+	unsigned int		net;
+
+	/* Only modified under RTNL. Reads have addrs_lock held */
+	u8			*addrs;
+	size_t			num_addrs;
+	spinlock_t		addrs_lock;
+
+	struct rcu_head		rcu;
+};
+
+#define MCTP_INITIAL_DEFAULT_NET	1
+
+struct mctp_dev *mctp_dev_get_rtnl(const struct net_device *dev);
+struct mctp_dev *__mctp_dev_get(const struct net_device *dev);
+
+#endif /* __NET_MCTPDEVICE_H */
include/uapi/linux/if_ether.h +3 -0
index a0b637911d3c..5f589c7a8382 100644
@@ -151,6 +151,9 @@
 #define ETH_P_MAP	0x00F9		/* Qualcomm multiplexing and
 					 * aggregation protocol
 					 */
+#define ETH_P_MCTP	0x00FA		/* Management component transport
+					 * protocol packets
+					 */
 
 /*
  *	This is an Ethernet frame header.
include/uapi/linux/if_link.h +10 -0
index 4882e81514b6..49b22afab78f 100644
@@ -1260,4 +1260,14 @@ struct ifla_rmnet_flags {
 	__u32	mask;
 };
 
+/* MCTP section */
+
+enum {
+	IFLA_MCTP_UNSPEC,
+	IFLA_MCTP_NET,
+	__IFLA_MCTP_MAX,
+};
+
+#define IFLA_MCTP_MAX (__IFLA_MCTP_MAX - 1)
+
 #endif /* _UAPI_LINUX_IF_LINK_H */
include/uapi/linux/mctp.h +1 -0
index 52b54d13f385..a9d8edb3402b 100644
@@ -26,6 +26,7 @@ struct sockaddr_mctp {
 };
 
 #define MCTP_NET_ANY		0x0
+#define MCTP_NET_DEFAULT	0x0
 
 #define MCTP_ADDR_NULL		0x00
 #define MCTP_ADDR_ANY		0xff
net/mctp/Makefile +1 -1
index 7c056b1b7939..2ea98c27b262 100644
@@ -1,3 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_MCTP) += mctp.o
-mctp-objs := af_mctp.o
+mctp-objs := af_mctp.o device.o
net/mctp/af_mctp.c +8 -0
index b3aeca6486e3..401b4fa141a5 100644
@@ -6,13 +6,18 @@
  * Copyright (c) 2021 Google
  */
 
+#include <linux/if_arp.h>
 #include <linux/net.h>
 #include <linux/mctp.h>
 #include <linux/module.h>
 #include <linux/socket.h>
 
+#include <net/mctp.h>
+#include <net/mctpdevice.h>
 #include <net/sock.h>
 
+/* socket implementation */
+
 struct mctp_sock {
 	struct sock	sk;
 };
@@ -152,6 +157,8 @@ static __init int mctp_init(void)
 	if (rc)
 		goto err_unreg_sock;
 
+	mctp_device_init();
+
 	return 0;
 
 err_unreg_sock:
@@ -162,6 +169,7 @@ static __init int mctp_init(void)
 
 static __exit void mctp_exit(void)
 {
+	mctp_device_exit();
 	proto_unregister(&mctp_proto);
 	sock_unregister(PF_MCTP);
 }
net/mctp/device.c new file +414 -0

414 lines not on this page · raw message

Signature · 1 line
2.30.2
author maintainer
#12 lore raw

PATCH v4 7/15 mctp: Add initial routing framework

In reply to Jeremy Kerr

Add a simple routing table, and a couple of route output handlers, and the mctp packet_type & handler.

Includes changes from Matt Johnston <[email protected]>.

---
v2:
- Controller -> component
v3:
- use an array for local EIDs
v4:
- don't inline mctp_rt_match_eid

---
 MAINTAINERS                 |   1 +
 include/net/mctp.h          |  75 ++++++++
 include/net/net_namespace.h |   4 +
 include/net/netns/mctp.h    |  16 ++
 net/mctp/Makefile           |   2 +-
 net/mctp/af_mctp.c          |   7 +
 net/mctp/device.c           |   8 +
 net/mctp/route.c            | 329 ++++++++++++++++++++++++++++++++++++
 8 files changed, 441 insertions(+), 1 deletion(-)
 create mode 100644 include/net/netns/mctp.h
 create mode 100644 net/mctp/route.c
MAINTAINERS +1 -0
index c2943c227ee6..4ca73465e690 100644
@@ -11040,6 +11040,7 @@ S:	Maintained
 F:	drivers/net/mctp/
 F:	include/net/mctp.h
 F:	include/net/mctpdevice.h
+F:	include/net/netns/mctp.h
 F:	net/mctp/
 
 MAN-PAGES: MANUAL PAGES FOR LINUX -- Sections 2, 3, 4, 5, and 7
include/net/mctp.h +75 -0

93 lines not on this page · raw message

include/net/net_namespace.h +4 -0
index 12cf6d7ea62c..cc54750dd3db 100644
@@ -34,6 +34,7 @@
 #include <net/netns/xdp.h>
 #include <net/netns/smc.h>
 #include <net/netns/bpf.h>
+#include <net/netns/mctp.h>
 #include <linux/ns_common.h>
 #include <linux/idr.h>
 #include <linux/skbuff.h>
@@ -167,6 +168,9 @@ struct net {
 #ifdef CONFIG_XDP_SOCKETS
 	struct netns_xdp	xdp;
 #endif
+#if IS_ENABLED(CONFIG_MCTP)
+	struct netns_mctp	mctp;
+#endif
 #if IS_ENABLED(CONFIG_CRYPTO_USER)
 	struct sock		*crypto_nlsk;
 #endif
include/net/netns/mctp.h new file +16 -0
new file mode 100644
index 000000000000..508459b08a59
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * MCTP per-net structures
+ */
+
+#ifndef __NETNS_MCTP_H__
+#define __NETNS_MCTP_H__
+
+#include <linux/types.h>
+
+struct netns_mctp {
+	/* Only updated under RTNL, entries freed via RCU */
+	struct list_head routes;
+};
+
+#endif /* __NETNS_MCTP_H__ */
net/mctp/Makefile +1 -1
index 2ea98c27b262..b1a330e9d82a 100644
@@ -1,3 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_MCTP) += mctp.o
-mctp-objs := af_mctp.o device.o
+mctp-objs := af_mctp.o device.o route.o
net/mctp/af_mctp.c +7 -0
index 401b4fa141a5..8085f5912101 100644
@@ -157,10 +157,16 @@ static __init int mctp_init(void)
 	if (rc)
 		goto err_unreg_sock;
 
+	rc = mctp_routes_init();
+	if (rc)
+		goto err_unreg_proto;
+
 	mctp_device_init();
 
 	return 0;
 
+err_unreg_proto:
+	proto_unregister(&mctp_proto);
 err_unreg_sock:
 	sock_unregister(PF_MCTP);
 
@@ -170,6 +176,7 @@ static __init int mctp_init(void)
 static __exit void mctp_exit(void)
 {
 	mctp_device_exit();
+	mctp_routes_exit();
 	proto_unregister(&mctp_proto);
 	sock_unregister(PF_MCTP);
 }
net/mctp/device.c +8 -0
index 877abe5312cd..5f1b18c17351 100644
@@ -197,6 +197,8 @@ static int mctp_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
 
 	kfree(tmp_addrs);
 
+	mctp_route_add_local(mdev, addr->s_addr);
+
 	return 0;
 }
 
@@ -240,6 +242,11 @@ static int mctp_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
 	if (!pos)
 		return -ENOENT;
 
+	rc = mctp_route_remove_local(mdev, addr->s_addr);
+	// we can ignore -ENOENT in the case a route was already removed
+	if (rc < 0 && rc != -ENOENT)
+		return rc;
+
 	spin_lock_irqsave(&mdev->addrs_lock, flags);
 	memmove(pos, pos + 1, mdev->num_addrs - 1 - (pos - mdev->addrs));
 	mdev->num_addrs--;
@@ -334,6 +341,7 @@ static void mctp_unregister(struct net_device *dev)
 
 	RCU_INIT_POINTER(mdev->dev->mctp_ptr, NULL);
 
+	mctp_route_remove_dev(mdev);
 	kfree(mdev->addrs);
 
 	mctp_dev_destroy(mdev);
net/mctp/route.c new file +329 -0

329 lines not on this page · raw message

Signature · 1 line
2.30.2
author maintainer
#13 lore raw

PATCH v4 8/15 mctp: Add netlink route management

In reply to Jeremy Kerr

From: Matt Johnston <[email protected]>

This change adds RTM_GETROUTE, RTM_NEWROUTE & RTM_DELROUTE handlers, allowing management of the MCTP route table.

Includes changes from Jeremy Kerr <[email protected]>.

---
v4:
- require rtm_type == RTN_UNICAST in route management handlers
- remove unused RTAX policy table

---
 include/net/mctp.h |   2 +
 net/mctp/route.c   | 258 +++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 251 insertions(+), 9 deletions(-)
include/net/mctp.h +2 -0
index 4094bec5e5db..bc36e37e8198 100644
@@ -111,6 +111,8 @@ int mctp_local_output(struct sock *sk, struct mctp_route *rt,
 		      struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag);
 
 /* routing <--> device interface */
+unsigned int mctp_default_net(struct net *net);
+int mctp_default_net_set(struct net *net, unsigned int index);
 int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr);
 int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr);
 void mctp_route_remove_dev(struct mctp_dev *mdev);
net/mctp/route.c +249 -9

322 lines not on this page · raw message

Signature · 1 line
2.30.2
author maintainer
#14 lore raw

PATCH v4 9/15 mctp: Add neighbour implementation

In reply to Jeremy Kerr

From: Matt Johnston <[email protected]>

Add an initial neighbour table implementation, to be used in the route output path.

---
v2:
- controller -> component

---
 include/net/mctp.h       |  25 +++++++
 include/net/mctpdevice.h |   1 +
 include/net/netns/mctp.h |   4 ++
 net/mctp/Makefile        |   2 +-
 net/mctp/af_mctp.c       |   5 ++
 net/mctp/device.c        |   1 +
 net/mctp/neigh.c         | 141 +++++++++++++++++++++++++++++++++++++++
 7 files changed, 178 insertions(+), 1 deletion(-)
 create mode 100644 net/mctp/neigh.c
include/net/mctp.h +25 -0
index bc36e37e8198..53f035c8b59c 100644
@@ -117,6 +117,31 @@ int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr);
 int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr);
 void mctp_route_remove_dev(struct mctp_dev *mdev);
 
+/* neighbour definitions */
+enum mctp_neigh_source {
+	MCTP_NEIGH_STATIC,
+	MCTP_NEIGH_DISCOVER,
+};
+
+struct mctp_neigh {
+	struct mctp_dev		*dev;
+	mctp_eid_t		eid;
+	enum mctp_neigh_source	source;
+
+	unsigned char		ha[MAX_ADDR_LEN];
+
+	struct list_head	list;
+	struct rcu_head		rcu;
+};
+
+int mctp_neigh_init(void);
+void mctp_neigh_exit(void);
+
+// ret_hwaddr may be NULL, otherwise must have space for MAX_ADDR_LEN
+int mctp_neigh_lookup(struct mctp_dev *dev, mctp_eid_t eid,
+		      void *ret_hwaddr);
+void mctp_neigh_remove_dev(struct mctp_dev *mdev);
+
 int mctp_routes_init(void);
 void mctp_routes_exit(void);
 
include/net/mctpdevice.h +1 -0
index 71a11012fac7..57e773ff08bb 100644
@@ -31,5 +31,6 @@ struct mctp_dev {
 
 struct mctp_dev *mctp_dev_get_rtnl(const struct net_device *dev);
 struct mctp_dev *__mctp_dev_get(const struct net_device *dev);
+struct mctp_dev *mctp_dev_get_rtnl(const struct net_device *dev);
 
 #endif /* __NET_MCTPDEVICE_H */
include/net/netns/mctp.h +4 -0
index 508459b08a59..2f5ebeeb320e 100644
@@ -11,6 +11,10 @@
 struct netns_mctp {
 	/* Only updated under RTNL, entries freed via RCU */
 	struct list_head routes;
+
+	/* neighbour table */
+	struct mutex neigh_lock;
+	struct list_head neighbours;
 };
 
 #endif /* __NETNS_MCTP_H__ */
net/mctp/Makefile +1 -1
index b1a330e9d82a..0171333384d7 100644
@@ -1,3 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_MCTP) += mctp.o
-mctp-objs := af_mctp.o device.o route.o
+mctp-objs := af_mctp.o device.o route.o neigh.o
net/mctp/af_mctp.c +5 -0
index 8085f5912101..58701e6b282c 100644
@@ -161,6 +161,10 @@ static __init int mctp_init(void)
 	if (rc)
 		goto err_unreg_proto;
 
+	rc = mctp_neigh_init();
+	if (rc)
+		goto err_unreg_proto;
+
 	mctp_device_init();
 
 	return 0;
@@ -176,6 +180,7 @@ static __init int mctp_init(void)
 static __exit void mctp_exit(void)
 {
 	mctp_device_exit();
+	mctp_neigh_exit();
 	mctp_routes_exit();
 	proto_unregister(&mctp_proto);
 	sock_unregister(PF_MCTP);
net/mctp/device.c +1 -0
index 5f1b18c17351..aa049590acda 100644
@@ -342,6 +342,7 @@ static void mctp_unregister(struct net_device *dev)
 	RCU_INIT_POINTER(mdev->dev->mctp_ptr, NULL);
 
 	mctp_route_remove_dev(mdev);
+	mctp_neigh_remove_dev(mdev);
 	kfree(mdev->addrs);
 
 	mctp_dev_destroy(mdev);
net/mctp/neigh.c new file +141 -0

141 lines not on this page · raw message

Signature · 1 line
2.30.2
author maintainer
#15 lore raw

PATCH v4 10/15 mctp: Add neighbour netlink interface

In reply to Jeremy Kerr

From: Matt Johnston <[email protected]>

This change adds the netlink interfaces for manipulating the MCTP neighbour table.

---
 net/mctp/neigh.c | 207 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 204 insertions(+), 3 deletions(-)
net/mctp/neigh.c +204 -3
index 8603f0c45a8f..90ed2f02d1fb 100644
@@ -21,9 +21,9 @@
 #include <net/netlink.h>
 #include <net/sock.h>
 
-static int __always_unused mctp_neigh_add(struct mctp_dev *mdev, mctp_eid_t eid,
-					  enum mctp_neigh_source source,
-					  size_t lladdr_len, const void *lladdr)
+static int mctp_neigh_add(struct mctp_dev *mdev, mctp_eid_t eid,
+			  enum mctp_neigh_source source,
+			  size_t lladdr_len, const void *lladdr)
 {
 	struct net *net = dev_net(mdev->dev);
 	struct mctp_neigh *neigh;
@@ -85,6 +85,196 @@ void mctp_neigh_remove_dev(struct mctp_dev *mdev)
 	mutex_unlock(&net->mctp.neigh_lock);
 }
 
+// TODO: add a "source" flag so netlink can only delete static neighbours?
+static int mctp_neigh_remove(struct mctp_dev *mdev, mctp_eid_t eid)
+{
+	struct net *net = dev_net(mdev->dev);
+	struct mctp_neigh *neigh, *tmp;
+	bool dropped = false;
+
+	mutex_lock(&net->mctp.neigh_lock);
+	list_for_each_entry_safe(neigh, tmp, &net->mctp.neighbours, list) {
+		if (neigh->dev == mdev && neigh->eid == eid) {
+			list_del_rcu(&neigh->list);
+			/* TODO: immediate RTM_DELNEIGH */
+			call_rcu(&neigh->rcu, __mctp_neigh_free);
+			dropped = true;
+		}
+	}
+
+	mutex_unlock(&net->mctp.neigh_lock);
+	return dropped ? 0 : -ENOENT;
+}
+
+static const struct nla_policy nd_mctp_policy[NDA_MAX + 1] = {
+	[NDA_DST]		= { .type = NLA_U8 },
+	[NDA_LLADDR]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
+};
+
+static int mctp_rtm_newneigh(struct sk_buff *skb, struct nlmsghdr *nlh,
+			     struct netlink_ext_ack *extack)
+{
+	struct net *net = sock_net(skb->sk);
+	struct net_device *dev;
+	struct mctp_dev *mdev;
+	struct ndmsg *ndm;
+	struct nlattr *tb[NDA_MAX + 1];
+	int rc;
+	mctp_eid_t eid;
+	void *lladdr;
+	int lladdr_len;
+
+	rc = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, nd_mctp_policy,
+			 extack);
+	if (rc < 0) {
+		NL_SET_ERR_MSG(extack, "lladdr too large?");
+		return rc;
+	}
+
+	if (!tb[NDA_DST]) {
+		NL_SET_ERR_MSG(extack, "Neighbour EID must be specified");
+		return -EINVAL;
+	}
+
+	if (!tb[NDA_LLADDR]) {
+		NL_SET_ERR_MSG(extack, "Neighbour lladdr must be specified");
+		return -EINVAL;
+	}
+
+	eid = nla_get_u8(tb[NDA_DST]);
+	if (!mctp_address_ok(eid)) {
+		NL_SET_ERR_MSG(extack, "Invalid neighbour EID");
+		return -EINVAL;
+	}
+
+	lladdr = nla_data(tb[NDA_LLADDR]);
+	lladdr_len = nla_len(tb[NDA_LLADDR]);
+
+	ndm = nlmsg_data(nlh);
+
+	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
+	if (!dev)
+		return -ENODEV;
+
+	mdev = mctp_dev_get_rtnl(dev);
+	if (!mdev)
+		return -ENODEV;
+
+	if (lladdr_len != dev->addr_len) {
+		NL_SET_ERR_MSG(extack, "Wrong lladdr length");
+		return -EINVAL;
+	}
+
+	return mctp_neigh_add(mdev, eid, MCTP_NEIGH_STATIC,
+			lladdr_len, lladdr);
+}
+
+static int mctp_rtm_delneigh(struct sk_buff *skb, struct nlmsghdr *nlh,
+			     struct netlink_ext_ack *extack)
+{
+	struct net *net = sock_net(skb->sk);
+	struct nlattr *tb[NDA_MAX + 1];
+	struct net_device *dev;
+	struct mctp_dev *mdev;
+	struct ndmsg *ndm;
+	int rc;
+	mctp_eid_t eid;
+
+	rc = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, nd_mctp_policy,
+			 extack);
+	if (rc < 0) {
+		NL_SET_ERR_MSG(extack, "incorrect format");
+		return rc;
+	}
+
+	if (!tb[NDA_DST]) {
+		NL_SET_ERR_MSG(extack, "Neighbour EID must be specified");
+		return -EINVAL;
+	}
+	eid = nla_get_u8(tb[NDA_DST]);
+
+	ndm = nlmsg_data(nlh);
+	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
+	if (!dev)
+		return -ENODEV;
+
+	mdev = mctp_dev_get_rtnl(dev);
+	if (!mdev)
+		return -ENODEV;
+
+	return mctp_neigh_remove(mdev, eid);
+}
+
+static int mctp_fill_neigh(struct sk_buff *skb, u32 portid, u32 seq, int event,
+			   unsigned int flags, struct mctp_neigh *neigh)
+{
+	struct net_device *dev = neigh->dev->dev;
+	struct nlmsghdr *nlh;
+	struct ndmsg *hdr;
+
+	nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
+	if (!nlh)
+		return -EMSGSIZE;
+
+	hdr = nlmsg_data(nlh);
+	hdr->ndm_family = AF_MCTP;
+	hdr->ndm_ifindex = dev->ifindex;
+	hdr->ndm_state = 0; // TODO other state bits?
+	if (neigh->source == MCTP_NEIGH_STATIC)
+		hdr->ndm_state |= NUD_PERMANENT;
+	hdr->ndm_flags = 0;
+	hdr->ndm_type = RTN_UNICAST; // TODO: is loopback RTN_LOCAL?
+
+	if (nla_put_u8(skb, NDA_DST, neigh->eid))
+		goto cancel;
+
+	if (nla_put(skb, NDA_LLADDR, dev->addr_len, neigh->ha))
+		goto cancel;
+
+	nlmsg_end(skb, nlh);
+
+	return 0;
+cancel:
+	nlmsg_cancel(skb, nlh);
+	return -EMSGSIZE;
+}
+
+static int mctp_rtm_getneigh(struct sk_buff *skb, struct netlink_callback *cb)
+{
+	struct net *net = sock_net(skb->sk);
+	int rc, idx, req_ifindex;
+	struct mctp_neigh *neigh;
+	struct ndmsg *ndmsg;
+	struct {
+		int idx;
+	} *cbctx = (void *)cb->ctx;
+
+	ndmsg = nlmsg_data(cb->nlh);
+	req_ifindex = ndmsg->ndm_ifindex;
+
+	idx = 0;
+	rcu_read_lock();
+	list_for_each_entry_rcu(neigh, &net->mctp.neighbours, list) {
+		if (idx < cbctx->idx)
+			goto cont;
+
+		rc = 0;
+		if (req_ifindex == 0 || req_ifindex == neigh->dev->dev->ifindex)
+			rc = mctp_fill_neigh(skb, NETLINK_CB(cb->skb).portid,
+					     cb->nlh->nlmsg_seq,
+					     RTM_NEWNEIGH, NLM_F_MULTI, neigh);
+
+		if (rc)
+			break;
+cont:
+		idx++;
+	}
+	rcu_read_unlock();
+
+	cbctx->idx = idx;
+	return skb->len;
+}
+
 int mctp_neigh_lookup(struct mctp_dev *mdev, mctp_eid_t eid, void *ret_hwaddr)
 {
 	struct net *net = dev_net(mdev->dev);
@@ -111,6 +301,7 @@ static int __net_init mctp_neigh_net_init(struct net *net)
 	struct netns_mctp *ns = &net->mctp;
 
 	INIT_LIST_HEAD(&ns->neighbours);
+	mutex_init(&ns->neigh_lock);
 	return 0;
 }
 
@@ -132,10 +323,20 @@ static struct pernet_operations mctp_net_ops = {
 
 int __init mctp_neigh_init(void)
 {
+	rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_NEWNEIGH,
+			     mctp_rtm_newneigh, NULL, 0);
+	rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_DELNEIGH,
+			     mctp_rtm_delneigh, NULL, 0);
+	rtnl_register_module(THIS_MODULE, PF_MCTP, RTM_GETNEIGH,
+			     NULL, mctp_rtm_getneigh, 0);
+
 	return register_pernet_subsys(&mctp_net_ops);
 }
 
 void __exit mctp_neigh_exit(void)
 {
 	unregister_pernet_subsys(&mctp_net_ops);
+	rtnl_unregister(PF_MCTP, RTM_GETNEIGH);
+	rtnl_unregister(PF_MCTP, RTM_DELNEIGH);
+	rtnl_unregister(PF_MCTP, RTM_NEWNEIGH);
 }
Signature · 1 line
2.30.2
author maintainer
#16 lore raw

PATCH v4 11/15 mctp: Populate socket implementation

In reply to Jeremy Kerr

Start filling-out the socket syscalls: bind, sendmsg & recvmsg.

This requires an input route implementation, so we add to mctp_route_input, allowing lookups on binds & message tags. This just handles single-packet messages at present, we will add fragmentation in a future change.

---
v2:
- require CAP_NET_BIND_SERVICE for bind(), CAP_NET_RAW for TX.
- be strict about sendmsg() tag bits
v3:
- fix comment typos
v4:
- fix mctp_sock->keys rcu annotations

---
 include/net/mctp.h       |  59 ++++++++++
 include/net/netns/mctp.h |  13 +++
 net/mctp/af_mctp.c       | 203 +++++++++++++++++++++++++++++++++--
 net/mctp/route.c         | 226 ++++++++++++++++++++++++++++++++++++++-
 4 files changed, 491 insertions(+), 10 deletions(-)
include/net/mctp.h +59 -0
index 53f035c8b59c..f2d98f6993c0 100644
@@ -12,6 +12,7 @@
 #include <linux/bits.h>
 #include <linux/mctp.h>
 #include <net/net_namespace.h>
+#include <net/sock.h>
 
 /* MCTP packet definitions */
 struct mctp_hdr {
@@ -46,6 +47,64 @@ static inline struct mctp_hdr *mctp_hdr(struct sk_buff *skb)
 	return (struct mctp_hdr *)skb_network_header(skb);
 }
 
+/* socket implementation */
+struct mctp_sock {
+	struct sock	sk;
+
+	/* bind() params */
+	int		bind_net;
+	mctp_eid_t	bind_addr;
+	__u8		bind_type;
+
+	/* list of mctp_sk_key, for incoming tag lookup. updates protected
+	 * by sk->net->keys_lock
+	 */
+	struct hlist_head keys;
+};
+
+/* Key for matching incoming packets to sockets or reassembly contexts.
+ * Packets are matched on (src,dest,tag).
+ *
+ * Lifetime requirements:
+ *
+ *  - keys are free()ed via RCU
+ *
+ *  - a mctp_sk_key contains a reference to a struct sock; this is valid
+ *    for the life of the key. On sock destruction (through unhash), the key is
+ *    removed from lists (see below), and will not be observable after a RCU
+ *    grace period.
+ *
+ *    any RX occurring within that grace period may still queue to the socket,
+ *    but will hit the SOCK_DEAD case before the socket is freed.
+ *
+ * - these mctp_sk_keys appear on two lists:
+ *     1) the struct mctp_sock->keys list
+ *     2) the struct netns_mctp->keys list
+ *
+ *        updates to either list are performed under the netns_mctp->keys
+ *        lock.
+ *
+ * - there is a single destruction path for a mctp_sk_key - through socket
+ *   unhash (see mctp_sk_unhash). This performs the list removal under
+ *   keys_lock.
+ */
+struct mctp_sk_key {
+	mctp_eid_t	peer_addr;
+	mctp_eid_t	local_addr;
+	__u8		tag; /* incoming tag match; invert TO for local */
+
+	/* we hold a ref to sk when set */
+	struct sock	*sk;
+
+	/* routing lookup list */
+	struct hlist_node hlist;
+
+	/* per-socket list */
+	struct hlist_node sklist;
+
+	struct rcu_head	rcu;
+};
+
 struct mctp_skb_cb {
 	unsigned int	magic;
 	unsigned int	net;
include/net/netns/mctp.h +13 -0
index 2f5ebeeb320e..14ae6d37e52a 100644
@@ -12,6 +12,19 @@ struct netns_mctp {
 	/* Only updated under RTNL, entries freed via RCU */
 	struct list_head routes;
 
+	/* Bound sockets: list of sockets bound by type.
+	 * This list is updated from non-atomic contexts (under bind_lock),
+	 * and read (under rcu) in packet rx
+	 */
+	struct mutex bind_lock;
+	struct hlist_head binds;
+
+	/* tag allocations. This list is read and updated from atomic contexts,
+	 * but elements are free()ed after a RCU grace-period
+	 */
+	spinlock_t keys_lock;
+	struct hlist_head keys;
+
 	/* neighbour table */
 	struct mutex neigh_lock;
 	struct list_head neighbours;
net/mctp/af_mctp.c +196 -7

246 lines not on this page · raw message

net/mctp/route.c +223 -3

270 lines not on this page · raw message

Signature · 1 line
2.30.2
author maintainer
#17 lore raw

PATCH v4 12/15 mctp: Implement message fragmentation & reassembly

In reply to Jeremy Kerr

This change implements MCTP fragmentation (based on route & device MTU), and corresponding reassembly.

The MCTP specification only allows for fragmentation on the originating message endpoint, and reassembly on the destination endpoint - intermediate nodes do not need to reassemble/refragment. Consequently, we only fragment in the local transmit path, and reassemble locally-bound packets. Messages are required to be in-order, so we simply cancel reassembly on out-of-order or missing packets.

In the fragmentation path, we just break up the message into MTU-sized fragments; the skb structure is a simple copy for now, which we can later improve with a shared data implementation.

For reassembly, we keep track of incoming message fragments using the existing tag infrastructure, allocating a key on the (src,dest,tag) tuple, and reassembles matching fragments into a skb->frag_list.

---
v2:
- limit max reassembly size
v3:
- fix comment typos
v4
- fix spurious rcu_read_unlock in route input

---
 include/net/mctp.h |  25 ++-
 net/mctp/af_mctp.c |   8 +
 net/mctp/route.c   | 372 ++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 361 insertions(+), 44 deletions(-)
include/net/mctp.h +22 -3
index f2d98f6993c0..0a460ba185b8 100644
@@ -84,9 +84,21 @@ struct mctp_sock {
  *        updates to either list are performed under the netns_mctp->keys
  *        lock.
  *
- * - there is a single destruction path for a mctp_sk_key - through socket
- *   unhash (see mctp_sk_unhash). This performs the list removal under
- *   keys_lock.
+ * - a key may have a sk_buff attached as part of an in-progress message
+ *   reassembly (->reasm_head). The reassembly context is protected by
+ *   reasm_lock, which may be acquired with the keys lock (above) held, if
+ *   necessary. Consequently, keys lock *cannot* be acquired with the
+ *   reasm_lock held.
+ *
+ * - there are two destruction paths for a mctp_sk_key:
+ *
+ *    - through socket unhash (see mctp_sk_unhash). This performs the list
+ *      removal under keys_lock.
+ *
+ *    - where a key is established to receive a reply message: after receiving
+ *      the (complete) reply, or during reassembly errors. Here, we clean up
+ *      the reassembly context (marking reasm_dead, to prevent another from
+ *      starting), and remove the socket from the netns & socket lists.
  */
 struct mctp_sk_key {
 	mctp_eid_t	peer_addr;
@@ -102,6 +114,13 @@ struct mctp_sk_key {
 	/* per-socket list */
 	struct hlist_node sklist;
 
+	/* incoming fragment reassembly context */
+	spinlock_t	reasm_lock;
+	struct sk_buff	*reasm_head;
+	struct sk_buff	**reasm_tailp;
+	bool		reasm_dead;
+	u8		last_seq;
+
 	struct rcu_head	rcu;
 };
 
net/mctp/af_mctp.c +8 -0
index 52bd7f2b78db..9ca836df19d0 100644
@@ -263,6 +263,14 @@ static void mctp_sk_unhash(struct sock *sk)
 	hlist_for_each_entry_safe(key, tmp, &msk->keys, sklist) {
 		hlist_del_rcu(&key->sklist);
 		hlist_del_rcu(&key->hlist);
+
+		spin_lock(&key->reasm_lock);
+		if (key->reasm_head)
+			kfree_skb(key->reasm_head);
+		key->reasm_head = NULL;
+		key->reasm_dead = true;
+		spin_unlock(&key->reasm_lock);
+
 		kfree_rcu(key, rcu);
 	}
 	spin_unlock_irqrestore(&net->mctp.keys_lock, flags);
net/mctp/route.c +331 -41

458 lines not on this page · raw message

Signature · 1 line
2.30.2
author maintainer
#18 lore raw

PATCH v4 13/15 mctp: Add dest neighbour lladdr to route output

In reply to Jeremy Kerr

From: Matt Johnston <[email protected]>

Now that we have a neighbour implementation, hook it up to the output path to set the dest hardware address for outgoing packets.

---
 net/mctp/route.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
net/mctp/route.c +8 -2
index 160220e6f241..38f0a7278520 100644
@@ -376,6 +376,9 @@ static unsigned int mctp_route_mtu(struct mctp_route *rt)
 
 static int mctp_route_output(struct mctp_route *route, struct sk_buff *skb)
 {
+	struct mctp_hdr *hdr = mctp_hdr(skb);
+	char daddr_buf[MAX_ADDR_LEN];
+	char *daddr = NULL;
 	unsigned int mtu;
 	int rc;
 
@@ -387,9 +390,12 @@ static int mctp_route_output(struct mctp_route *route, struct sk_buff *skb)
 		return -EMSGSIZE;
 	}
 
-	/* TODO: daddr (from rt->neigh), saddr (from device?)  */
+	/* If lookup fails let the device handle daddr==NULL */
+	if (mctp_neigh_lookup(route->dev, hdr->dest, daddr_buf) == 0)
+		daddr = daddr_buf;
+
 	rc = dev_hard_header(skb, skb->dev, ntohs(skb->protocol),
-			     NULL, NULL, skb->len);
+			     daddr, skb->dev->dev_addr, skb->len);
 	if (rc) {
 		kfree_skb(skb);
 		return -EHOSTUNREACH;
Signature · 1 line
2.30.2
author maintainer
#19 lore raw

PATCH v4 14/15 mctp: Allow per-netns default networks

In reply to Jeremy Kerr

From: Matt Johnston <[email protected]>

Currently we have a compile-time default network (MCTP_INITIAL_DEFAULT_NET). This change introduces a default_net field on the net namespace, allowing future configuration for new interfaces.

---
 include/net/mctp.h        |  2 ++
 include/net/netns/mctp.h  |  3 +++
 include/uapi/linux/mctp.h |  1 -
 net/mctp/af_mctp.c        |  3 +++
 net/mctp/device.c         |  2 +-
 net/mctp/route.c          | 14 ++++++++++++++
 6 files changed, 23 insertions(+), 2 deletions(-)
include/net/mctp.h +2 -0
index 0a460ba185b8..54bbe042c973 100644
@@ -37,6 +37,8 @@ struct mctp_hdr {
 
 #define MCTP_HEADER_MAXLEN	4
 
+#define MCTP_INITIAL_DEFAULT_NET	1
+
 static inline bool mctp_address_ok(mctp_eid_t eid)
 {
 	return eid >= 8 && eid < 255;
include/net/netns/mctp.h +3 -0
index 14ae6d37e52a..acedef12a35e 100644
@@ -25,6 +25,9 @@ struct netns_mctp {
 	spinlock_t keys_lock;
 	struct hlist_head keys;
 
+	/* MCTP network */
+	unsigned int default_net;
+
 	/* neighbour table */
 	struct mutex neigh_lock;
 	struct list_head neighbours;
include/uapi/linux/mctp.h +0 -1
index a9d8edb3402b..52b54d13f385 100644
@@ -26,7 +26,6 @@ struct sockaddr_mctp {
 };
 
 #define MCTP_NET_ANY		0x0
-#define MCTP_NET_DEFAULT	0x0
 
 #define MCTP_ADDR_NULL		0x00
 #define MCTP_ADDR_ANY		0xff
net/mctp/af_mctp.c +3 -0
index 9ca836df19d0..84f722d31fd7 100644
@@ -94,6 +94,9 @@ static int mctp_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
 	if (!capable(CAP_NET_RAW))
 		return -EACCES;
 
+	if (addr->smctp_network == MCTP_NET_ANY)
+		addr->smctp_network = mctp_default_net(sock_net(sk));
+
 	rt = mctp_route_lookup(sock_net(sk), addr->smctp_network,
 			       addr->smctp_addr.s_addr);
 	if (!rt)
net/mctp/device.c +1 -1
index aa049590acda..b9f38e765f61 100644
@@ -267,7 +267,7 @@ static struct mctp_dev *mctp_add_dev(struct net_device *dev)
 
 	spin_lock_init(&mdev->addrs_lock);
 
-	mdev->net = MCTP_INITIAL_DEFAULT_NET;
+	mdev->net = mctp_default_net(dev_net(dev));
 
 	/* associate to net_device */
 	rcu_assign_pointer(dev->mctp_ptr, mdev);
net/mctp/route.c +14 -0
index 38f0a7278520..b3101375c8e7 100644
@@ -433,6 +433,19 @@ static struct mctp_route *mctp_route_alloc(void)
 	return rt;
 }
 
+unsigned int mctp_default_net(struct net *net)
+{
+	return READ_ONCE(net->mctp.default_net);
+}
+
+int mctp_default_net_set(struct net *net, unsigned int index)
+{
+	if (index == 0)
+		return -EINVAL;
+	WRITE_ONCE(net->mctp.default_net, index);
+	return 0;
+}
+
 /* tag management */
 static void mctp_reserve_tag(struct net *net, struct mctp_sk_key *key,
 			     struct mctp_sock *msk)
@@ -1045,6 +1058,7 @@ static int __net_init mctp_routes_net_init(struct net *net)
 	mutex_init(&ns->bind_lock);
 	INIT_HLIST_HEAD(&ns->keys);
 	spin_lock_init(&ns->keys_lock);
+	WARN_ON(mctp_default_net_set(net, MCTP_INITIAL_DEFAULT_NET));
 	return 0;
 }
 
Signature · 1 line
2.30.2
author maintainer
#20 lore raw

PATCH v4 15/15 mctp: Add MCTP overview document

In reply to Jeremy Kerr

This change adds a brief document about the sockets API provided for sending and receiving MCTP messages from userspace.

This is roughly based on the OpenBMC design document, at:

https://github.com/openbmc/docs/blob/master/designs/mctp/mctp-kernel.md

---
v2:
- Controller -> component

---
 Documentation/networking/index.rst |   1 +
 Documentation/networking/mctp.rst  | 213 +++++++++++++++++++++++++++++
 MAINTAINERS                        |   1 +
 3 files changed, 215 insertions(+)
 create mode 100644 Documentation/networking/mctp.rst
Documentation/networking/index.rst +1 -0
index a91a2739f8ed..58bc8cd367c6 100644
@@ -69,6 +69,7 @@ Contents:
    l2tp
    lapb-module
    mac80211-injection
+   mctp
    mpls-sysctl
    mptcp-sysctl
    multiqueue
Documentation/networking/mctp.rst new file +213 -0

213 lines not on this page · raw message

MAINTAINERS +1 -0
index 4ca73465e690..73beb91891ee 100644
@@ -11037,6 +11037,7 @@ M:	Jeremy Kerr <[email protected]>
 M:	Matt Johnston <[email protected]>
 L:	[email protected]
 S:	Maintained
+F:	Documentation/networking/mctp.rst
 F:	drivers/net/mctp/
 F:	include/net/mctp.h
 F:	include/net/mctpdevice.h
Signature · 1 line
2.30.2
#21 lore raw

Re: [PATCH net-next v4 15/15] mctp: Add MCTP overview document

Quoted 2 lines · Jeremy Kerr

This change adds a brief document about the sockets API provided for sending and receiving MCTP messages from userspace.

[...]

Quoted 1 line
Documentation/networking/mctp.rst +0 -0

[...]

Quoted 14 lines

+``bind()`` : set local socket address
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Documentation/networking/mctp.rst +12 -0
+
+Sockets that receive incoming request packets will bind to a local address,
+using the ``bind()`` syscall.
+
+.. code-block:: C
+
+    struct sockaddr_mctp addr;
+
+    addr.smctp_family = AF_MCTP;
+    addr.smctp_network = MCTP_NET_ANY;
+    addr.smctp_addr.s_addr = MCTP_ADDR_ANY;
+    addr.smctp_type = MCTP_TYPE_PLDM;

[...]

Quoted 8 lines

+``sendto()``, ``sendmsg()``, ``send()`` : transmit an MCTP message +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Documentation/networking/mctp.rst +6 -0
+
+An MCTP message is transmitted using one of the ``sendto()``, ``sendmsg()`` or
+``send()`` syscalls. Using ``sendto()`` as the primary example:
+
+.. code-block:: C
+

Documentation/networking/mctp.rst line 140

Quoted 10 lines
Documentation/networking/mctp.rst +10 -0
+    struct sockaddr_mctp addr;
+    char buf[14];
+    ssize_t len;
+
+    /* set message destination */
+    addr.smctp_family = AF_MCTP;
+    addr.smctp_network = 0;
+    addr.smctp_addr.s_addr = 8;
+    addr.smctp_tag = MCTP_TAG_OWNER;
+    addr.smctp_type = MCTP_TYPE_ECHO;

While MCTP_TYPE_PLDM and MCTP_TYPE_ECHO are mentioned
in the documentation, their definition is currently missing in the UAPI header (or anywhere in the source tree at all). Is that expected?

#22 lore raw
In reply to Jeremy Kerr
This series was applied to netdev/net-next.git (refs/heads/master):

Hello:

This series was applied to netdev/net-next.git (refs/heads/master):

Quoted 9 lines · On Thu, 29 Jul 2021 10:20:38 +0800 you wrote:

This series adds core MCTP support to the kernel. From the Kconfig description:

Management Component Transport Protocol (MCTP) is an in-system protocol for communicating between management controllers and their managed devices (peripherals, host processors, etc.). The protocol is defined by DMTF specification DSP0236.

[...]

Here is the summary with links:
- [net-next,v4,01/15] mctp: Add MCTP base

    https://git.kernel.org/netdev/net-next/c/bc49d8169aa7
  - [net-next,v4,02/15] mctp: Add base socket/protocol definitions
    https://git.kernel.org/netdev/net-next/c/8f601a1e4f8c
  - [net-next,v4,03/15] mctp: Add base packet definitions
    https://git.kernel.org/netdev/net-next/c/2c8e2e9aec79
  - [net-next,v4,04/15] mctp: Add sockaddr_mctp to uapi
    https://git.kernel.org/netdev/net-next/c/60fc63981693
  - [net-next,v4,05/15] mctp: Add initial driver infrastructure
    https://git.kernel.org/netdev/net-next/c/4b2e69305cbb
  - [net-next,v4,06/15] mctp: Add device handling and netlink interface
    https://git.kernel.org/netdev/net-next/c/583be982d934
  - [net-next,v4,07/15] mctp: Add initial routing framework
    https://git.kernel.org/netdev/net-next/c/889b7da23abf
  - [net-next,v4,08/15] mctp: Add netlink route management
    https://git.kernel.org/netdev/net-next/c/06d2f4c583a7
  - [net-next,v4,09/15] mctp: Add neighbour implementation
    https://git.kernel.org/netdev/net-next/c/4d8b9319282a
  - [net-next,v4,10/15] mctp: Add neighbour netlink interface
    https://git.kernel.org/netdev/net-next/c/831119f88781
  - [net-next,v4,11/15] mctp: Populate socket implementation
    https://git.kernel.org/netdev/net-next/c/833ef3b91de6
  - [net-next,v4,12/15] mctp: Implement message fragmentation & reassembly
    https://git.kernel.org/netdev/net-next/c/4a992bbd3650
  - [net-next,v4,13/15] mctp: Add dest neighbour lladdr to route output
    https://git.kernel.org/netdev/net-next/c/26ab3fcaf235
  - [net-next,v4,14/15] mctp: Allow per-netns default networks
    https://git.kernel.org/netdev/net-next/c/03f2bbc4ee57
  - [net-next,v4,15/15] mctp: Add MCTP overview document
    https://git.kernel.org/netdev/net-next/c/6a2d98b18900

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html