Skip to content

Commit 889b7da

Browse files
jk-ozlabsdavem330
authored andcommitted
mctp: Add initial routing framework
Add a simple routing table, and a couple of route output handlers, and the mctp packet_type & handler. Includes changes from Matt Johnston <matt@codeconstruct.com.au>. Signed-off-by: Jeremy Kerr <jk@codeconstruct.com.au> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 583be98 commit 889b7da

File tree

8 files changed

+441
-1
lines changed

8 files changed

+441
-1
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11040,6 +11040,7 @@ S: Maintained
1104011040
F: drivers/net/mctp/
1104111041
F: include/net/mctp.h
1104211042
F: include/net/mctpdevice.h
11043+
F: include/net/netns/mctp.h
1104311044
F: net/mctp/
1104411045

1104511046
MAN-PAGES: MANUAL PAGES FOR LINUX -- Sections 2, 3, 4, 5, and 7

include/net/mctp.h

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <linux/bits.h>
1313
#include <linux/mctp.h>
14+
#include <net/net_namespace.h>
1415

1516
/* MCTP packet definitions */
1617
struct mctp_hdr {
@@ -33,6 +34,8 @@ struct mctp_hdr {
3334
#define MCTP_HDR_TAG_SHIFT 0
3435
#define MCTP_HDR_TAG_MASK GENMASK(2, 0)
3536

37+
#define MCTP_HEADER_MAXLEN 4
38+
3639
static inline bool mctp_address_ok(mctp_eid_t eid)
3740
{
3841
return eid >= 8 && eid < 255;
@@ -43,6 +46,78 @@ static inline struct mctp_hdr *mctp_hdr(struct sk_buff *skb)
4346
return (struct mctp_hdr *)skb_network_header(skb);
4447
}
4548

49+
struct mctp_skb_cb {
50+
unsigned int magic;
51+
unsigned int net;
52+
mctp_eid_t src;
53+
};
54+
55+
/* skb control-block accessors with a little extra debugging for initial
56+
* development.
57+
*
58+
* TODO: remove checks & mctp_skb_cb->magic; replace callers of __mctp_cb
59+
* with mctp_cb().
60+
*
61+
* __mctp_cb() is only for the initial ingress code; we should see ->magic set
62+
* at all times after this.
63+
*/
64+
static inline struct mctp_skb_cb *__mctp_cb(struct sk_buff *skb)
65+
{
66+
struct mctp_skb_cb *cb = (void *)skb->cb;
67+
68+
cb->magic = 0x4d435450;
69+
return cb;
70+
}
71+
72+
static inline struct mctp_skb_cb *mctp_cb(struct sk_buff *skb)
73+
{
74+
struct mctp_skb_cb *cb = (void *)skb->cb;
75+
76+
WARN_ON(cb->magic != 0x4d435450);
77+
return (void *)(skb->cb);
78+
}
79+
80+
/* Route definition.
81+
*
82+
* These are held in the pernet->mctp.routes list, with RCU protection for
83+
* removed routes. We hold a reference to the netdev; routes need to be
84+
* dropped on NETDEV_UNREGISTER events.
85+
*
86+
* Updates to the route table are performed under rtnl; all reads under RCU,
87+
* so routes cannot be referenced over a RCU grace period. Specifically: A
88+
* caller cannot block between mctp_route_lookup and passing the route to
89+
* mctp_do_route.
90+
*/
91+
struct mctp_route {
92+
mctp_eid_t min, max;
93+
94+
struct mctp_dev *dev;
95+
unsigned int mtu;
96+
int (*output)(struct mctp_route *route,
97+
struct sk_buff *skb);
98+
99+
struct list_head list;
100+
refcount_t refs;
101+
struct rcu_head rcu;
102+
};
103+
104+
/* route interfaces */
105+
struct mctp_route *mctp_route_lookup(struct net *net, unsigned int dnet,
106+
mctp_eid_t daddr);
107+
108+
int mctp_do_route(struct mctp_route *rt, struct sk_buff *skb);
109+
110+
int mctp_local_output(struct sock *sk, struct mctp_route *rt,
111+
struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag);
112+
113+
/* routing <--> device interface */
114+
int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr);
115+
int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr);
116+
void mctp_route_remove_dev(struct mctp_dev *mdev);
117+
118+
int mctp_routes_init(void);
119+
void mctp_routes_exit(void);
120+
46121
void mctp_device_init(void);
47122
void mctp_device_exit(void);
48123

include/net/net_namespace.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <net/netns/xdp.h>
3535
#include <net/netns/smc.h>
3636
#include <net/netns/bpf.h>
37+
#include <net/netns/mctp.h>
3738
#include <linux/ns_common.h>
3839
#include <linux/idr.h>
3940
#include <linux/skbuff.h>
@@ -167,6 +168,9 @@ struct net {
167168
#ifdef CONFIG_XDP_SOCKETS
168169
struct netns_xdp xdp;
169170
#endif
171+
#if IS_ENABLED(CONFIG_MCTP)
172+
struct netns_mctp mctp;
173+
#endif
170174
#if IS_ENABLED(CONFIG_CRYPTO_USER)
171175
struct sock *crypto_nlsk;
172176
#endif

include/net/netns/mctp.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/*
3+
* MCTP per-net structures
4+
*/
5+
6+
#ifndef __NETNS_MCTP_H__
7+
#define __NETNS_MCTP_H__
8+
9+
#include <linux/types.h>
10+
11+
struct netns_mctp {
12+
/* Only updated under RTNL, entries freed via RCU */
13+
struct list_head routes;
14+
};
15+
16+
#endif /* __NETNS_MCTP_H__ */

net/mctp/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# SPDX-License-Identifier: GPL-2.0
22
obj-$(CONFIG_MCTP) += mctp.o
3-
mctp-objs := af_mctp.o device.o
3+
mctp-objs := af_mctp.o device.o route.o

net/mctp/af_mctp.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,16 @@ static __init int mctp_init(void)
157157
if (rc)
158158
goto err_unreg_sock;
159159

160+
rc = mctp_routes_init();
161+
if (rc)
162+
goto err_unreg_proto;
163+
160164
mctp_device_init();
161165

162166
return 0;
163167

168+
err_unreg_proto:
169+
proto_unregister(&mctp_proto);
164170
err_unreg_sock:
165171
sock_unregister(PF_MCTP);
166172

@@ -170,6 +176,7 @@ static __init int mctp_init(void)
170176
static __exit void mctp_exit(void)
171177
{
172178
mctp_device_exit();
179+
mctp_routes_exit();
173180
proto_unregister(&mctp_proto);
174181
sock_unregister(PF_MCTP);
175182
}

net/mctp/device.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ static int mctp_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
197197

198198
kfree(tmp_addrs);
199199

200+
mctp_route_add_local(mdev, addr->s_addr);
201+
200202
return 0;
201203
}
202204

@@ -240,6 +242,11 @@ static int mctp_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
240242
if (!pos)
241243
return -ENOENT;
242244

245+
rc = mctp_route_remove_local(mdev, addr->s_addr);
246+
// we can ignore -ENOENT in the case a route was already removed
247+
if (rc < 0 && rc != -ENOENT)
248+
return rc;
249+
243250
spin_lock_irqsave(&mdev->addrs_lock, flags);
244251
memmove(pos, pos + 1, mdev->num_addrs - 1 - (pos - mdev->addrs));
245252
mdev->num_addrs--;
@@ -334,6 +341,7 @@ static void mctp_unregister(struct net_device *dev)
334341

335342
RCU_INIT_POINTER(mdev->dev->mctp_ptr, NULL);
336343

344+
mctp_route_remove_dev(mdev);
337345
kfree(mdev->addrs);
338346

339347
mctp_dev_destroy(mdev);

0 commit comments

Comments
 (0)