Add Management Component Transport Protocol support
Subsystems (7)
Jeremy Kerr
maintainerv3 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 v3 of the series, but v1 and v2 were both RFC.
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:
- 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
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 · 1 line
2.30.2
Jeremy Kerr
maintainerPATCH v3 1/16 mctp: Add MCTP base
Add basic Kconfig, an initial (empty) af_mctp source object, and {AF,PF}_MCTP definitions, and the required selinux definitions.
---
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
--- MAINTAINERS | 7 +++++++ include/linux/socket.h | 6 +++++- include/uapi/linux/mctp.h | 15 +++++++++++++++ net/Kconfig | 1 + net/Makefile | 1 + net/mctp/Kconfig | 13 +++++++++++++ net/mctp/Makefile | 3 +++ net/mctp/af_mctp.c | 7 +++++++ security/selinux/hooks.c | 4 +++- security/selinux/include/classmap.h | 4 +++- 10 files changed, 58 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
@@ -11030,6 +11030,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
@@ -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
@@ -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
@@ -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
@@ -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/mctp/Kconfig new file +13 -0
@@ -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
@@ -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 +7 -0
@@ -0,0 +1,7 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Management Component Transport Protocol (MCTP) +* +* Copyright (c) 2021 Code Construct +* Copyright (c) 2021 Google +*/
security/selinux/hooks.c +3 -1
@@ -1330,7 +1330,9 @@ static inline u16 socket_type_to_security_class(int family, int type, int protocreturn 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
@@ -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
Re: [PATCH net-next v3 01/16] mctp: Add MCTP base
Hi Jeremy,
CC kbuild
Quoted 4 lines · Jeremy Kerr
Add basic
Kconfig, an initial (empty)af_mctpsource object, and {AF,PF}_MCTP definitions, and the required selinux definitions.Signed-off-by Jeremy Kerr <[email protected]>
Thanks for your patch, which is now commit bc49d8169aa72295 ("mctp: Add MCTP base") in net-next.
security/selinux/hooks.c static inline u16 socket_type_to_security_class(int family, int type, int protoc · line 1334
Quoted 9 lines
security/selinux/hooks.c +2
-1@@ -1330,7 +1330,9 @@ static inline u16 socket_type_to_security_class(int family, int type, int protocreturn SECCLASS_SMC_SOCKET ;case PF_XDP :return SECCLASS_XDP_SOCKET ; -#if PF_MAX >45 +case PF_MCTP : +return SECCLASS_MCTP_SOCKET ;
When building an allmodconfig kernel, I got:
security/selinux/hooks.c: In function 'socket_type_to_security_class':security/selinux/hooks.c:1334:32: error: 'SECCLASS_MCTP_SOCKET' undeclared (first use in this function); did you mean 'SECCLASS_SCTP_SOCKET'?
1334 | return SECCLASS_MCTP_SOCKET;
| ^~~~~~~~~~~~~~~~~~~~
| SECCLASS_SCTP_SOCKETQuoted 9 lines
security/selinux/hooks.c +1
-0+#if PF_MAX >46 #error New address family defined, please update this function.#endif }security/selinux/include/classmap.h +0
-0
security/selinux/include/classmap.h struct security_class_mapping secclass_map[] = { · line 253
Quoted 8 lines
security/selinux/include/classmap.h +2
-0@@ -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",
The needed definition should be auto-generated from the above file, but there seems to be an issue with the dependencies, as the file was not regenerated.
Manually removing security/selinux/flask.h in the build dir fixed the issue.
I'm building in a separate build directory, using make -j 12.
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
Jeremy Kerr
maintainerRe: [PATCH net-next v3 01/16] mctp: Add MCTP base
Hi Geert,
Thanks for the testing!
Quoted 1 line
When building an allmodconfig kernel, I got:
[...]
I don't see this on a clean allmodconfig build, nor when building the previous commit then the MCTP commit with something like:
git checkout bc49d81^
make O=obj.allmodconfig allmodconfig
make O=obj.allmodconfig -j16
git checkout bc49d81
make O=obj.allmodconfig -j16
- but it seems like it might be up to the ordering of a parallel build.
From your description, it does sound like it's not regenerating flask.h; the kbuild rules would seem to have a classmap.h -> flask.h dependency:
$(addprefix $(obj)/,$(selinux-y)): $(obj)/flask.h
quiet_cmd_flask = GEN $(obj)/flask.h $(obj)/av_permissions.h
cmd_flask = scripts/selinux/genheaders/genheaders $(obj)/flask.h $(obj)/av_permissions.h
targets += flask.h av_permissions.h
$(obj)/flask.h: $(src)/include/classmap.h FORCE
$(call if_changed,flask)however, classmap.h is #include-ed as part of the genheaders binary build, rather than read at runtime; maybe $(obj)/flask.h should depend on the genheaders binary, rather than $(src)/include/classmap.h ?
If you can reproduce, can you compare the ctimes with:
stat scripts/selinux/genheaders/genheaders security/selinux/flask.h
in your object dir?
Cheers,
Jeremy
Re: [PATCH net-next v3 01/16] mctp: Add MCTP base
Hi Jeremy,
Quoted 31 lines · Jeremy Kerr
Quoted 1 line
When building an allmodconfig kernel, I got:
[...]
I don't see this on a clean allmodconfig build, nor when building the previous commit then the MCTP commit with something like:
git checkout bc49d81^
make O=obj.allmodconfig allmodconfig
make O=obj.allmodconfig -j16
git checkout bc49d81
make O=obj.allmodconfig -j16- but it seems like it might be up to the ordering of a parallel build.
From your description, it does sound like it's not regenerating
flask.h; the kbuild rules would seem to have aclassmap.h->flask.hdependency:$(addprefix $(obj)/,$(selinux-y)): $(obj)/flask.h quiet_cmd_flask = GEN $(obj)/flask.h $(obj)/av_permissions.h cmd_flask = scripts/selinux/genheaders/genheaders $(obj)/flask.h $(obj)/av_permissions.h targets += flask.h av_permissions.h $(obj)/flask.h: $(src)/include/classmap.h FORCE $(call if_changed,flask)however,
classmap.his #include-ed as part of the genheaders binary build, rather than read at runtime; maybe $(obj)/flask.h should depend on the genheaders binary, rather than $(src)/include/classmap.h ?
Quoted 5 lines
If you can reproduce, can you compare the ctimes with:
stat
scripts/selinux/genheaders/genheaderssecurity/selinux/flask.hin your object dir?
Unfortunately I can't seem to reproduce this anymore. Goodbye, Heisenbug!
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
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
Re: [PATCH net-next v3 01/16] mctp: Add MCTP base
In reply to Jeremy KerrQuoted 31 lines · On Thu, 12 Aug 2021 19:15:24 +0800 Jeremy Kerr <[email protected]> wrote:
Hi Geert,
Thanks for the testing!
Quoted 1 line
When building an allmodconfig kernel, I got:
[...]
I don't see this on a clean allmodconfig build, nor when building the previous commit then the MCTP commit with something like:
git checkout bc49d81^
make O=obj.allmodconfig allmodconfig
make O=obj.allmodconfig -j16
git checkout bc49d81
make O=obj.allmodconfig -j16- but it seems like it might be up to the ordering of a parallel build.
Quoted 1 line
From your description, it does sound like it's not regenerating
flask.h;the kbuild rules would seem to have a
classmap.h->flask.hdependency:$(addprefix $(obj)/,$(selinux-y)): $(obj)/flask.h quiet_cmd_flask = GEN $(obj)/flask.h $(obj)/av_permissions.h cmd_flask = scripts/selinux/genheaders/genheaders $(obj)/flask.h $(obj)/av_permissions.h targets += flask.h av_permissions.h $(obj)/flask.h: $(src)/include/classmap.h FORCE $(call if_changed,flask)
Quoted 7 lines
however,
classmap.his #include-ed as part of the genheaders binary build, rather than read at runtime; maybe $(obj)/flask.h should depend on the genheaders binary, rather than $(src)/include/classmap.h ?If you can reproduce, can you compare the ctimes with:
stat
scripts/selinux/genheaders/genheaderssecurity/selinux/flask.h
I just hit the exact same issue. I build with O=../build/ and by removing security/selinux/flask.h and av_permission.h, it built fine afterward. Appears to be a dependency issue.
-- Steve
Quoted 8 lines
in your object dir?
Cheers,
Jeremy
Jeremy Kerr
maintainerPATCH v3 2/16 mctp: Add base socket/protocol definitions
In reply to Jeremy KerrAdd an empty socket implementation, plus initialisation/destruction handlers.
---
v2:
- Controller -> component
--- net/mctp/af_mctp.c | 169 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+)
net/mctp/af_mctp.c +169 -0
@@ -5,3 +5,172 @@* Copyright (c) 2021 Code Construct * 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
Jeremy Kerr
maintainerPATCH v3 3/16 mctp: Add base packet definitions
In reply to Jeremy KerrSimple 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
@@ -11035,6 +11035,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
@@ -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
Jeremy Kerr
maintainerPATCH v3 4/16 mctp: Add sockaddr_mctp to uapi
In reply to Jeremy KerrThis 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
@@ -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
Jeremy Kerr
maintainerPATCH v3 5/16 mctp: Add initial driver infrastructure
In reply to Jeremy KerrAdd 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
@@ -11035,6 +11035,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
@@ -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
@@ -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
@@ -0,0 +1,8 @@ + +if MCTP + +menu "MCTP Device Drivers" + +endmenu + +endif
drivers/net/mctp/Makefile new file +0 -0
include/uapi/linux/if_arp.h +1 -0
@@ -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
Jeremy Kerr
maintainerPATCH v3 6/16 mctp: Add device handling and netlink interface
In reply to Jeremy KerrThis 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
--- MAINTAINERS | 1 + include/linux/netdevice.h | 3 + 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 | 415 ++++++++++++++++++++++++++++++++++ 10 files changed, 491 insertions(+), 1 deletion(-) create mode 100644 include/net/mctpdevice.h create mode 100644 net/mctp/device.c
MAINTAINERS +1 -0
@@ -11037,6 +11037,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 +3 -0
@@ -2092,6 +2092,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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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 +415 -0
415 lines not on this page · raw message
Signature · 1 line
2.30.2
Jeremy Kerr
maintainerPATCH v3 7/16 mctp: Add initial routing framework
In reply to Jeremy KerrAdd 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
--- 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
@@ -11038,6 +11038,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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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--;
@@ -335,6 +342,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
Jeremy Kerr
maintainerPATCH v3 8/16 mctp: Add netlink route management
In reply to Jeremy KerrFrom: 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]>.
--- include/net/mctp.h | 4 + net/mctp/route.c | 252 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 247 insertions(+), 9 deletions(-)
include/net/mctp.h +4 -0
@@ -111,6 +111,10 @@ 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 */ +int mctp_route_add (struct mctp_dev *mdev,mctp_eid_t daddr_start, +unsigned int daddr_extent,unsigned int mtu,bool is_local); +int mctp_route_remove (struct mctp_dev *mdev,mctp_eid_t daddr_start, +unsigned int daddr_extent);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 +243 -9
316 lines not on this page · raw message
Signature · 1 line
2.30.2
Jeremy Kerr
maintainerPATCH v3 9/16 mctp: Add neighbour implementation
In reply to Jeremy KerrFrom: 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
@@ -119,6 +119,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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -343,6 +343,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
Jeremy Kerr
maintainerPATCH v3 10/16 mctp: Add neighbour netlink interface
In reply to Jeremy KerrFrom: 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
@@ -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
Jeremy Kerr
maintainerPATCH v3 11/16 mctp: Populate socket implementation
In reply to Jeremy KerrStart 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
--- 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
@@ -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 __rcu 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
@@ -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
Jeremy Kerr
maintainerPATCH v3 12/16 mctp: Implement message fragmentation & reassembly
In reply to Jeremy KerrThis 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
--- include/net/mctp.h | 25 ++- net/mctp/af_mctp.c | 8 + net/mctp/route.c | 371 ++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 360 insertions(+), 44 deletions(-)
include/net/mctp.h +22 -3
@@ -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
@@ -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 +330 -41
457 lines not on this page · raw message
Signature · 1 line
2.30.2
Jeremy Kerr
maintainerPATCH v3 13/16 mctp: Add dest neighbour lladdr to route output
In reply to Jeremy KerrFrom: 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
@@ -375,6 +375,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;
@@ -386,9 +389,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
Jeremy Kerr
maintainerPATCH v3 14/16 mctp: Allow per-netns default networks
In reply to Jeremy KerrFrom: 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 | 4 ++++ 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, 25 insertions(+), 2 deletions(-)
include/net/mctp.h +4 -0
@@ -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 ;
@@ -189,6 +191,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 (struct mctp_dev *mdev,mctp_eid_t daddr_start,unsigned int daddr_extent,unsigned int mtu,bool is_local); int mctp_route_remove(struct mctp_dev *mdev, mctp_eid_t daddr_start,
include/net/netns/mctp.h +3 -0
@@ -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
@@ -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
@@ -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
@@ -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
@@ -432,6 +432,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)
@@ -1038,6 +1051,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
Jeremy Kerr
maintainerPATCH v3 15/16 mctp: Allow MCTP on tun devices
In reply to Jeremy KerrFrom: Matt Johnston <[email protected]>
Allowing TUN is useful for testing, to route packets to userspace or to tunnel between machines.
--- net/mctp/device.c | 7 +++++-- net/mctp/route.c | 13 ++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-)
net/mctp/device.c +5 -2
@@ -357,9 +357,12 @@ static int mctp_register(struct net_device *dev)if (rtnl_dereference (dev->mctp_ptr))return 0 ; -/* only register specific types; MCTP-specific and loopback for now */ -if (dev->type !=ARPHRD_MCTP && dev->type !=ARPHRD_LOOPBACK ) +/* only register specific types (inc. NONE for TUN devices) */ +if (!(dev->type ==ARPHRD_MCTP || + dev->type ==ARPHRD_LOOPBACK || + dev->type ==ARPHRD_NONE )) {return 0 ; + } mdev =mctp_add_dev (dev);if (IS_ERR (mdev))
net/mctp/route.c +8 -5
@@ -806,13 +806,18 @@ static int mctp_pkttype_receive(struct sk_buff *skb, struct net_device *dev, struct net_device *orig_dev) {struct net *net =dev_net (dev); +struct mctp_dev *mdev;struct mctp_skb_cb *cb;struct mctp_route *rt;struct mctp_hdr *mh; -/* basic non-data sanity checks */ -if (dev->type !=ARPHRD_MCTP ) +rcu_read_lock (); + mdev =__mctp_dev_get (dev); +rcu_read_unlock (); +if (!mdev) { +/* basic non-data sanity checks */ goto err_drop; + }if (!pskb_may_pull (skb,sizeof (struct mctp_hdr ))) goto err_drop;
@@ -826,9 +831,7 @@ static int mctp_pkttype_receive(struct sk_buff *skb, struct net_device *dev, goto err_drop; cb =__mctp_cb (skb); -rcu_read_lock (); - cb->net =READ_ONCE (__mctp_dev_get (dev)->net); -rcu_read_unlock (); + cb->net =READ_ONCE (mdev->net); rt =mctp_route_lookup (net, cb->net, mh->dest);if (!rt)
Signature · 1 line
2.30.2
Jeremy Kerr
maintainerPATCH v3 16/16 mctp: Add MCTP overview document
In reply to Jeremy KerrThis 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
@@ -68,6 +68,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
@@ -11035,6 +11035,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