xref: /openbmc/linux/include/net/mctp.h (revision 2dd6532e)
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 
41 static inline bool mctp_address_unicast(mctp_eid_t eid)
42 {
43 	return eid >= 8 && eid < 255;
44 }
45 
46 static inline bool mctp_address_broadcast(mctp_eid_t eid)
47 {
48 	return eid == 255;
49 }
50 
51 static inline bool mctp_address_null(mctp_eid_t eid)
52 {
53 	return eid == 0;
54 }
55 
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 
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_addr;
73 	__u8		bind_type;
74 
75 	/* sendmsg()/recvmsg() uses struct sockaddr_mctp_ext */
76 	bool		addr_ext;
77 
78 	/* list of mctp_sk_key, for incoming tag lookup. updates protected
79 	 * by sk->net->keys_lock
80 	 */
81 	struct hlist_head keys;
82 
83 	/* mechanism for expiring allocated keys; will release an allocated
84 	 * tag, and any netdev state for a request/response pairing
85 	 */
86 	struct timer_list key_expiry;
87 };
88 
89 /* Key for matching incoming packets to sockets or reassembly contexts.
90  * Packets are matched on (src,dest,tag).
91  *
92  * Lifetime / locking requirements:
93  *
94  *  - individual key data (ie, the struct itself) is protected by key->lock;
95  *    changes must be made with that lock held.
96  *
97  *  - the lookup fields: peer_addr, local_addr and tag are set before the
98  *    key is added to lookup lists, and never updated.
99  *
100  *  - A ref to the key must be held (throuh key->refs) if a pointer to the
101  *    key is to be accessed after key->lock is released.
102  *
103  *  - a mctp_sk_key contains a reference to a struct sock; this is valid
104  *    for the life of the key. On sock destruction (through unhash), the key is
105  *    removed from lists (see below), and marked invalid.
106  *
107  * - these mctp_sk_keys appear on two lists:
108  *     1) the struct mctp_sock->keys list
109  *     2) the struct netns_mctp->keys list
110  *
111  *   presences on these lists requires a (single) refcount to be held; both
112  *   lists are updated as a single operation.
113  *
114  *   Updates and lookups in either list are performed under the
115  *   netns_mctp->keys lock. Lookup functions will need to lock the key and
116  *   take a reference before unlocking the keys_lock. Consequently, the list's
117  *   keys_lock *cannot* be acquired with the individual key->lock held.
118  *
119  * - a key may have a sk_buff attached as part of an in-progress message
120  *   reassembly (->reasm_head). The reasm data is protected by the individual
121  *   key->lock.
122  *
123  * - there are two destruction paths for a mctp_sk_key:
124  *
125  *    - through socket unhash (see mctp_sk_unhash). This performs the list
126  *      removal under keys_lock.
127  *
128  *    - where a key is established to receive a reply message: after receiving
129  *      the (complete) reply, or during reassembly errors. Here, we clean up
130  *      the reassembly context (marking reasm_dead, to prevent another from
131  *      starting), and remove the socket from the netns & socket lists.
132  *
133  *    - through an expiry timeout, on a per-socket timer
134  */
135 struct mctp_sk_key {
136 	mctp_eid_t	peer_addr;
137 	mctp_eid_t	local_addr; /* MCTP_ADDR_ANY for local owned tags */
138 	__u8		tag; /* incoming tag match; invert TO for local */
139 
140 	/* we hold a ref to sk when set */
141 	struct sock	*sk;
142 
143 	/* routing lookup list */
144 	struct hlist_node hlist;
145 
146 	/* per-socket list */
147 	struct hlist_node sklist;
148 
149 	/* lock protects against concurrent updates to the reassembly and
150 	 * expiry data below.
151 	 */
152 	spinlock_t	lock;
153 
154 	/* Keys are referenced during the output path, which may sleep */
155 	refcount_t	refs;
156 
157 	/* incoming fragment reassembly context */
158 	struct sk_buff	*reasm_head;
159 	struct sk_buff	**reasm_tailp;
160 	bool		reasm_dead;
161 	u8		last_seq;
162 
163 	/* key validity */
164 	bool		valid;
165 
166 	/* expiry timeout; valid (above) cleared on expiry */
167 	unsigned long	expiry;
168 
169 	/* free to use for device flow state tracking. Initialised to
170 	 * zero on initial key creation
171 	 */
172 	unsigned long	dev_flow_state;
173 	struct mctp_dev	*dev;
174 
175 	/* a tag allocated with SIOCMCTPALLOCTAG ioctl will not expire
176 	 * automatically on timeout or response, instead SIOCMCTPDROPTAG
177 	 * is used.
178 	 */
179 	bool		manual_alloc;
180 };
181 
182 struct mctp_skb_cb {
183 	unsigned int	magic;
184 	unsigned int	net;
185 	int		ifindex; /* extended/direct addressing if set */
186 	mctp_eid_t	src;
187 	unsigned char	halen;
188 	unsigned char	haddr[MAX_ADDR_LEN];
189 };
190 
191 /* skb control-block accessors with a little extra debugging for initial
192  * development.
193  *
194  * TODO: remove checks & mctp_skb_cb->magic; replace callers of __mctp_cb
195  * with mctp_cb().
196  *
197  * __mctp_cb() is only for the initial ingress code; we should see ->magic set
198  * at all times after this.
199  */
200 static inline struct mctp_skb_cb *__mctp_cb(struct sk_buff *skb)
201 {
202 	struct mctp_skb_cb *cb = (void *)skb->cb;
203 
204 	cb->magic = 0x4d435450;
205 	return cb;
206 }
207 
208 static inline struct mctp_skb_cb *mctp_cb(struct sk_buff *skb)
209 {
210 	struct mctp_skb_cb *cb = (void *)skb->cb;
211 
212 	BUILD_BUG_ON(sizeof(struct mctp_skb_cb) > sizeof(skb->cb));
213 	WARN_ON(cb->magic != 0x4d435450);
214 	return (void *)(skb->cb);
215 }
216 
217 /* If CONFIG_MCTP_FLOWS, we may add one of these as a SKB extension,
218  * indicating the flow to the device driver.
219  */
220 struct mctp_flow {
221 	struct mctp_sk_key *key;
222 };
223 
224 /* Route definition.
225  *
226  * These are held in the pernet->mctp.routes list, with RCU protection for
227  * removed routes. We hold a reference to the netdev; routes need to be
228  * dropped on NETDEV_UNREGISTER events.
229  *
230  * Updates to the route table are performed under rtnl; all reads under RCU,
231  * so routes cannot be referenced over a RCU grace period. Specifically: A
232  * caller cannot block between mctp_route_lookup and mctp_route_release()
233  */
234 struct mctp_route {
235 	mctp_eid_t		min, max;
236 
237 	struct mctp_dev		*dev;
238 	unsigned int		mtu;
239 	unsigned char		type;
240 	int			(*output)(struct mctp_route *route,
241 					  struct sk_buff *skb);
242 
243 	struct list_head	list;
244 	refcount_t		refs;
245 	struct rcu_head		rcu;
246 };
247 
248 /* route interfaces */
249 struct mctp_route *mctp_route_lookup(struct net *net, unsigned int dnet,
250 				     mctp_eid_t daddr);
251 
252 int mctp_local_output(struct sock *sk, struct mctp_route *rt,
253 		      struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag);
254 
255 void mctp_key_unref(struct mctp_sk_key *key);
256 struct mctp_sk_key *mctp_alloc_local_tag(struct mctp_sock *msk,
257 					 mctp_eid_t daddr, mctp_eid_t saddr,
258 					 bool manual, u8 *tagp);
259 
260 /* routing <--> device interface */
261 unsigned int mctp_default_net(struct net *net);
262 int mctp_default_net_set(struct net *net, unsigned int index);
263 int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr);
264 int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr);
265 void mctp_route_remove_dev(struct mctp_dev *mdev);
266 
267 /* neighbour definitions */
268 enum mctp_neigh_source {
269 	MCTP_NEIGH_STATIC,
270 	MCTP_NEIGH_DISCOVER,
271 };
272 
273 struct mctp_neigh {
274 	struct mctp_dev		*dev;
275 	mctp_eid_t		eid;
276 	enum mctp_neigh_source	source;
277 
278 	unsigned char		ha[MAX_ADDR_LEN];
279 
280 	struct list_head	list;
281 	struct rcu_head		rcu;
282 };
283 
284 int mctp_neigh_init(void);
285 void mctp_neigh_exit(void);
286 
287 // ret_hwaddr may be NULL, otherwise must have space for MAX_ADDR_LEN
288 int mctp_neigh_lookup(struct mctp_dev *dev, mctp_eid_t eid,
289 		      void *ret_hwaddr);
290 void mctp_neigh_remove_dev(struct mctp_dev *mdev);
291 
292 int mctp_routes_init(void);
293 void mctp_routes_exit(void);
294 
295 void mctp_device_init(void);
296 void mctp_device_exit(void);
297 
298 #endif /* __NET_MCTP_H */
299