xref: /linux/include/net/mctp.h (revision 8be4d31cb8aaeea27bde4b7ddb26e28a89062ebf)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Management Component Transport Protocol (MCTP)
4  *
5  * Copyright (c) 2021 Code Construct
6  * Copyright (c) 2021 Google
7  */
8 
9 #ifndef __NET_MCTP_H
10 #define __NET_MCTP_H
11 
12 #include <linux/bits.h>
13 #include <linux/mctp.h>
14 #include <linux/netdevice.h>
15 #include <net/net_namespace.h>
16 #include <net/sock.h>
17 
18 /* MCTP packet definitions */
19 struct mctp_hdr {
20 	u8	ver;
21 	u8	dest;
22 	u8	src;
23 	u8	flags_seq_tag;
24 };
25 
26 #define MCTP_VER_MIN	1
27 #define MCTP_VER_MAX	1
28 
29 /* Definitions for flags_seq_tag field */
30 #define MCTP_HDR_FLAG_SOM	BIT(7)
31 #define MCTP_HDR_FLAG_EOM	BIT(6)
32 #define MCTP_HDR_FLAG_TO	BIT(3)
33 #define MCTP_HDR_FLAGS		GENMASK(5, 3)
34 #define MCTP_HDR_SEQ_SHIFT	4
35 #define MCTP_HDR_SEQ_MASK	GENMASK(1, 0)
36 #define MCTP_HDR_TAG_SHIFT	0
37 #define MCTP_HDR_TAG_MASK	GENMASK(2, 0)
38 
39 #define MCTP_INITIAL_DEFAULT_NET	1
40 
mctp_address_unicast(mctp_eid_t eid)41 static inline bool mctp_address_unicast(mctp_eid_t eid)
42 {
43 	return eid >= 8 && eid < 255;
44 }
45 
mctp_address_broadcast(mctp_eid_t eid)46 static inline bool mctp_address_broadcast(mctp_eid_t eid)
47 {
48 	return eid == 255;
49 }
50 
mctp_address_null(mctp_eid_t eid)51 static inline bool mctp_address_null(mctp_eid_t eid)
52 {
53 	return eid == 0;
54 }
55 
mctp_address_matches(mctp_eid_t match,mctp_eid_t eid)56 static inline bool mctp_address_matches(mctp_eid_t match, mctp_eid_t eid)
57 {
58 	return match == eid || match == MCTP_ADDR_ANY;
59 }
60 
mctp_hdr(struct sk_buff * skb)61 static inline struct mctp_hdr *mctp_hdr(struct sk_buff *skb)
62 {
63 	return (struct mctp_hdr *)skb_network_header(skb);
64 }
65 
66 /* socket implementation */
67 struct mctp_sock {
68 	struct sock	sk;
69 
70 	/* bind() params */
71 	unsigned int	bind_net;
72 	mctp_eid_t	bind_local_addr;
73 	mctp_eid_t	bind_peer_addr;
74 	unsigned int	bind_peer_net;
75 	bool		bind_peer_set;
76 	__u8		bind_type;
77 
78 	/* sendmsg()/recvmsg() uses struct sockaddr_mctp_ext */
79 	bool		addr_ext;
80 
81 	/* list of mctp_sk_key, for incoming tag lookup. updates protected
82 	 * by sk->net->keys_lock
83 	 */
84 	struct hlist_head keys;
85 
86 	/* mechanism for expiring allocated keys; will release an allocated
87 	 * tag, and any netdev state for a request/response pairing
88 	 */
89 	struct timer_list key_expiry;
90 };
91 
92 /* Key for matching incoming packets to sockets or reassembly contexts.
93  * Packets are matched on (peer EID, local EID, tag).
94  *
95  * Lifetime / locking requirements:
96  *
97  *  - individual key data (ie, the struct itself) is protected by key->lock;
98  *    changes must be made with that lock held.
99  *
100  *  - the lookup fields: peer_addr, local_addr and tag are set before the
101  *    key is added to lookup lists, and never updated.
102  *
103  *  - A ref to the key must be held (throuh key->refs) if a pointer to the
104  *    key is to be accessed after key->lock is released.
105  *
106  *  - a mctp_sk_key contains a reference to a struct sock; this is valid
107  *    for the life of the key. On sock destruction (through unhash), the key is
108  *    removed from lists (see below), and marked invalid.
109  *
110  * - these mctp_sk_keys appear on two lists:
111  *     1) the struct mctp_sock->keys list
112  *     2) the struct netns_mctp->keys list
113  *
114  *   presences on these lists requires a (single) refcount to be held; both
115  *   lists are updated as a single operation.
116  *
117  *   Updates and lookups in either list are performed under the
118  *   netns_mctp->keys lock. Lookup functions will need to lock the key and
119  *   take a reference before unlocking the keys_lock. Consequently, the list's
120  *   keys_lock *cannot* be acquired with the individual key->lock held.
121  *
122  * - a key may have a sk_buff attached as part of an in-progress message
123  *   reassembly (->reasm_head). The reasm data is protected by the individual
124  *   key->lock.
125  *
126  * - there are two destruction paths for a mctp_sk_key:
127  *
128  *    - through socket unhash (see mctp_sk_unhash). This performs the list
129  *      removal under keys_lock.
130  *
131  *    - where a key is established to receive a reply message: after receiving
132  *      the (complete) reply, or during reassembly errors. Here, we clean up
133  *      the reassembly context (marking reasm_dead, to prevent another from
134  *      starting), and remove the socket from the netns & socket lists.
135  *
136  *    - through an expiry timeout, on a per-socket timer
137  */
138 struct mctp_sk_key {
139 	unsigned int	net;
140 	mctp_eid_t	peer_addr;
141 	mctp_eid_t	local_addr; /* MCTP_ADDR_ANY for local owned tags */
142 	__u8		tag; /* incoming tag match; invert TO for local */
143 
144 	/* we hold a ref to sk when set */
145 	struct sock	*sk;
146 
147 	/* routing lookup list */
148 	struct hlist_node hlist;
149 
150 	/* per-socket list */
151 	struct hlist_node sklist;
152 
153 	/* lock protects against concurrent updates to the reassembly and
154 	 * expiry data below.
155 	 */
156 	spinlock_t	lock;
157 
158 	/* Keys are referenced during the output path, which may sleep */
159 	refcount_t	refs;
160 
161 	/* incoming fragment reassembly context */
162 	struct sk_buff	*reasm_head;
163 	struct sk_buff	**reasm_tailp;
164 	bool		reasm_dead;
165 	u8		last_seq;
166 
167 	/* key validity */
168 	bool		valid;
169 
170 	/* expiry timeout; valid (above) cleared on expiry */
171 	unsigned long	expiry;
172 
173 	/* free to use for device flow state tracking. Initialised to
174 	 * zero on initial key creation
175 	 */
176 	unsigned long	dev_flow_state;
177 	struct mctp_dev	*dev;
178 
179 	/* a tag allocated with SIOCMCTPALLOCTAG ioctl will not expire
180 	 * automatically on timeout or response, instead SIOCMCTPDROPTAG
181 	 * is used.
182 	 */
183 	bool		manual_alloc;
184 };
185 
186 struct mctp_skb_cb {
187 	unsigned int	magic;
188 	unsigned int	net;
189 	/* fields below provide extended addressing for ingress to recvmsg() */
190 	int		ifindex;
191 	unsigned char	halen;
192 	unsigned char	haddr[MAX_ADDR_LEN];
193 };
194 
195 /* skb control-block accessors with a little extra debugging for initial
196  * development.
197  *
198  * TODO: remove checks & mctp_skb_cb->magic; replace callers of __mctp_cb
199  * with mctp_cb().
200  *
201  * __mctp_cb() is only for the initial ingress code; we should see ->magic set
202  * at all times after this.
203  */
__mctp_cb(struct sk_buff * skb)204 static inline struct mctp_skb_cb *__mctp_cb(struct sk_buff *skb)
205 {
206 	struct mctp_skb_cb *cb = (void *)skb->cb;
207 
208 	cb->magic = 0x4d435450;
209 	return cb;
210 }
211 
mctp_cb(struct sk_buff * skb)212 static inline struct mctp_skb_cb *mctp_cb(struct sk_buff *skb)
213 {
214 	struct mctp_skb_cb *cb = (void *)skb->cb;
215 
216 	BUILD_BUG_ON(sizeof(struct mctp_skb_cb) > sizeof(skb->cb));
217 	WARN_ON(cb->magic != 0x4d435450);
218 	return cb;
219 }
220 
221 /* If CONFIG_MCTP_FLOWS, we may add one of these as a SKB extension,
222  * indicating the flow to the device driver.
223  */
224 struct mctp_flow {
225 	struct mctp_sk_key *key;
226 };
227 
228 struct mctp_dst;
229 
230 /* Route definition.
231  *
232  * These are held in the pernet->mctp.routes list, with RCU protection for
233  * removed routes. We hold a reference to the netdev; routes need to be
234  * dropped on NETDEV_UNREGISTER events.
235  *
236  * Updates to the route table are performed under rtnl; all reads under RCU,
237  * so routes cannot be referenced over a RCU grace period.
238  */
239 struct mctp_route {
240 	mctp_eid_t		min, max;
241 
242 	unsigned char		type;
243 
244 	unsigned int		mtu;
245 
246 	enum {
247 		MCTP_ROUTE_DIRECT,
248 		MCTP_ROUTE_GATEWAY,
249 	} dst_type;
250 	union {
251 		struct mctp_dev	*dev;
252 		struct mctp_fq_addr gateway;
253 	};
254 
255 	int			(*output)(struct mctp_dst *dst,
256 					  struct sk_buff *skb);
257 
258 	struct list_head	list;
259 	refcount_t		refs;
260 	struct rcu_head		rcu;
261 };
262 
263 /* Route lookup result: dst. Represents the results of a routing decision,
264  * but is only held over the individual routing operation.
265  *
266  * Will typically be stored on the caller stack, and must be released after
267  * usage.
268  */
269 struct mctp_dst {
270 	struct mctp_dev *dev;
271 	unsigned int mtu;
272 	mctp_eid_t nexthop;
273 
274 	/* set for direct addressing */
275 	unsigned char halen;
276 	unsigned char haddr[MAX_ADDR_LEN];
277 
278 	int (*output)(struct mctp_dst *dst, struct sk_buff *skb);
279 };
280 
281 int mctp_dst_from_extaddr(struct mctp_dst *dst, struct net *net, int ifindex,
282 			  unsigned char halen, const unsigned char *haddr);
283 
284 /* route interfaces */
285 int mctp_route_lookup(struct net *net, unsigned int dnet,
286 		      mctp_eid_t daddr, struct mctp_dst *dst);
287 
288 void mctp_dst_release(struct mctp_dst *dst);
289 
290 /* always takes ownership of skb */
291 int mctp_local_output(struct sock *sk, struct mctp_dst *dst,
292 		      struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag);
293 
294 void mctp_key_unref(struct mctp_sk_key *key);
295 struct mctp_sk_key *mctp_alloc_local_tag(struct mctp_sock *msk,
296 					 unsigned int netid,
297 					 mctp_eid_t local, mctp_eid_t peer,
298 					 bool manual, u8 *tagp);
299 
300 /* routing <--> device interface */
301 unsigned int mctp_default_net(struct net *net);
302 int mctp_default_net_set(struct net *net, unsigned int index);
303 int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr);
304 int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr);
305 void mctp_route_remove_dev(struct mctp_dev *mdev);
306 
307 /* neighbour definitions */
308 enum mctp_neigh_source {
309 	MCTP_NEIGH_STATIC,
310 	MCTP_NEIGH_DISCOVER,
311 };
312 
313 struct mctp_neigh {
314 	struct mctp_dev		*dev;
315 	mctp_eid_t		eid;
316 	enum mctp_neigh_source	source;
317 
318 	unsigned char		ha[MAX_ADDR_LEN];
319 
320 	struct list_head	list;
321 	struct rcu_head		rcu;
322 };
323 
324 int mctp_neigh_init(void);
325 void mctp_neigh_exit(void);
326 
327 // ret_hwaddr may be NULL, otherwise must have space for MAX_ADDR_LEN
328 int mctp_neigh_lookup(struct mctp_dev *dev, mctp_eid_t eid,
329 		      void *ret_hwaddr);
330 void mctp_neigh_remove_dev(struct mctp_dev *mdev);
331 
332 int mctp_routes_init(void);
333 void mctp_routes_exit(void);
334 
335 int mctp_device_init(void);
336 void mctp_device_exit(void);
337 
338 /* MCTP IDs and Codes from DMTF specification
339  * "DSP0239 Management Component Transport Protocol (MCTP) IDs and Codes"
340  * https://www.dmtf.org/sites/default/files/standards/documents/DSP0239_1.11.1.pdf
341  */
342 enum mctp_phys_binding {
343 	MCTP_PHYS_BINDING_UNSPEC	= 0x00,
344 	MCTP_PHYS_BINDING_SMBUS		= 0x01,
345 	MCTP_PHYS_BINDING_PCIE_VDM	= 0x02,
346 	MCTP_PHYS_BINDING_USB		= 0x03,
347 	MCTP_PHYS_BINDING_KCS		= 0x04,
348 	MCTP_PHYS_BINDING_SERIAL	= 0x05,
349 	MCTP_PHYS_BINDING_I3C		= 0x06,
350 	MCTP_PHYS_BINDING_MMBI		= 0x07,
351 	MCTP_PHYS_BINDING_PCC		= 0x08,
352 	MCTP_PHYS_BINDING_UCIE		= 0x09,
353 	MCTP_PHYS_BINDING_VENDOR	= 0xFF,
354 };
355 
356 #endif /* __NET_MCTP_H */
357