xref: /linux/include/net/mctp.h (revision f3956ebb3bf06ab2266ad5ee2214aed46405810c)
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 <net/net_namespace.h>
15 #include <net/sock.h>
16 
17 /* MCTP packet definitions */
18 struct mctp_hdr {
19 	u8	ver;
20 	u8	dest;
21 	u8	src;
22 	u8	flags_seq_tag;
23 };
24 
25 #define MCTP_VER_MIN	1
26 #define MCTP_VER_MAX	1
27 
28 /* Definitions for flags_seq_tag field */
29 #define MCTP_HDR_FLAG_SOM	BIT(7)
30 #define MCTP_HDR_FLAG_EOM	BIT(6)
31 #define MCTP_HDR_FLAG_TO	BIT(3)
32 #define MCTP_HDR_FLAGS		GENMASK(5, 3)
33 #define MCTP_HDR_SEQ_SHIFT	4
34 #define MCTP_HDR_SEQ_MASK	GENMASK(1, 0)
35 #define MCTP_HDR_TAG_SHIFT	0
36 #define MCTP_HDR_TAG_MASK	GENMASK(2, 0)
37 
38 #define MCTP_HEADER_MAXLEN	4
39 
40 #define MCTP_INITIAL_DEFAULT_NET	1
41 
42 static inline bool mctp_address_ok(mctp_eid_t eid)
43 {
44 	return eid >= 8 && eid < 255;
45 }
46 
47 static inline struct mctp_hdr *mctp_hdr(struct sk_buff *skb)
48 {
49 	return (struct mctp_hdr *)skb_network_header(skb);
50 }
51 
52 /* socket implementation */
53 struct mctp_sock {
54 	struct sock	sk;
55 
56 	/* bind() params */
57 	int		bind_net;
58 	mctp_eid_t	bind_addr;
59 	__u8		bind_type;
60 
61 	/* list of mctp_sk_key, for incoming tag lookup. updates protected
62 	 * by sk->net->keys_lock
63 	 */
64 	struct hlist_head keys;
65 
66 	/* mechanism for expiring allocated keys; will release an allocated
67 	 * tag, and any netdev state for a request/response pairing
68 	 */
69 	struct timer_list key_expiry;
70 };
71 
72 /* Key for matching incoming packets to sockets or reassembly contexts.
73  * Packets are matched on (src,dest,tag).
74  *
75  * Lifetime / locking requirements:
76  *
77  *  - individual key data (ie, the struct itself) is protected by key->lock;
78  *    changes must be made with that lock held.
79  *
80  *  - the lookup fields: peer_addr, local_addr and tag are set before the
81  *    key is added to lookup lists, and never updated.
82  *
83  *  - A ref to the key must be held (throuh key->refs) if a pointer to the
84  *    key is to be accessed after key->lock is released.
85  *
86  *  - a mctp_sk_key contains a reference to a struct sock; this is valid
87  *    for the life of the key. On sock destruction (through unhash), the key is
88  *    removed from lists (see below), and marked invalid.
89  *
90  * - these mctp_sk_keys appear on two lists:
91  *     1) the struct mctp_sock->keys list
92  *     2) the struct netns_mctp->keys list
93  *
94  *   presences on these lists requires a (single) refcount to be held; both
95  *   lists are updated as a single operation.
96  *
97  *   Updates and lookups in either list are performed under the
98  *   netns_mctp->keys lock. Lookup functions will need to lock the key and
99  *   take a reference before unlocking the keys_lock. Consequently, the list's
100  *   keys_lock *cannot* be acquired with the individual key->lock held.
101  *
102  * - a key may have a sk_buff attached as part of an in-progress message
103  *   reassembly (->reasm_head). The reasm data is protected by the individual
104  *   key->lock.
105  *
106  * - there are two destruction paths for a mctp_sk_key:
107  *
108  *    - through socket unhash (see mctp_sk_unhash). This performs the list
109  *      removal under keys_lock.
110  *
111  *    - where a key is established to receive a reply message: after receiving
112  *      the (complete) reply, or during reassembly errors. Here, we clean up
113  *      the reassembly context (marking reasm_dead, to prevent another from
114  *      starting), and remove the socket from the netns & socket lists.
115  *
116  *    - through an expiry timeout, on a per-socket timer
117  */
118 struct mctp_sk_key {
119 	mctp_eid_t	peer_addr;
120 	mctp_eid_t	local_addr;
121 	__u8		tag; /* incoming tag match; invert TO for local */
122 
123 	/* we hold a ref to sk when set */
124 	struct sock	*sk;
125 
126 	/* routing lookup list */
127 	struct hlist_node hlist;
128 
129 	/* per-socket list */
130 	struct hlist_node sklist;
131 
132 	/* lock protects against concurrent updates to the reassembly and
133 	 * expiry data below.
134 	 */
135 	spinlock_t	lock;
136 
137 	/* Keys are referenced during the output path, which may sleep */
138 	refcount_t	refs;
139 
140 	/* incoming fragment reassembly context */
141 	struct sk_buff	*reasm_head;
142 	struct sk_buff	**reasm_tailp;
143 	bool		reasm_dead;
144 	u8		last_seq;
145 
146 	/* key validity */
147 	bool		valid;
148 
149 	/* expiry timeout; valid (above) cleared on expiry */
150 	unsigned long	expiry;
151 };
152 
153 struct mctp_skb_cb {
154 	unsigned int	magic;
155 	unsigned int	net;
156 	mctp_eid_t	src;
157 };
158 
159 /* skb control-block accessors with a little extra debugging for initial
160  * development.
161  *
162  * TODO: remove checks & mctp_skb_cb->magic; replace callers of __mctp_cb
163  * with mctp_cb().
164  *
165  * __mctp_cb() is only for the initial ingress code; we should see ->magic set
166  * at all times after this.
167  */
168 static inline struct mctp_skb_cb *__mctp_cb(struct sk_buff *skb)
169 {
170 	struct mctp_skb_cb *cb = (void *)skb->cb;
171 
172 	cb->magic = 0x4d435450;
173 	return cb;
174 }
175 
176 static inline struct mctp_skb_cb *mctp_cb(struct sk_buff *skb)
177 {
178 	struct mctp_skb_cb *cb = (void *)skb->cb;
179 
180 	WARN_ON(cb->magic != 0x4d435450);
181 	return (void *)(skb->cb);
182 }
183 
184 /* Route definition.
185  *
186  * These are held in the pernet->mctp.routes list, with RCU protection for
187  * removed routes. We hold a reference to the netdev; routes need to be
188  * dropped on NETDEV_UNREGISTER events.
189  *
190  * Updates to the route table are performed under rtnl; all reads under RCU,
191  * so routes cannot be referenced over a RCU grace period. Specifically: A
192  * caller cannot block between mctp_route_lookup and passing the route to
193  * mctp_do_route.
194  */
195 struct mctp_route {
196 	mctp_eid_t		min, max;
197 
198 	struct mctp_dev		*dev;
199 	unsigned int		mtu;
200 	unsigned char		type;
201 	int			(*output)(struct mctp_route *route,
202 					  struct sk_buff *skb);
203 
204 	struct list_head	list;
205 	refcount_t		refs;
206 	struct rcu_head		rcu;
207 };
208 
209 /* route interfaces */
210 struct mctp_route *mctp_route_lookup(struct net *net, unsigned int dnet,
211 				     mctp_eid_t daddr);
212 
213 int mctp_do_route(struct mctp_route *rt, struct sk_buff *skb);
214 
215 int mctp_local_output(struct sock *sk, struct mctp_route *rt,
216 		      struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag);
217 
218 void mctp_key_unref(struct mctp_sk_key *key);
219 
220 /* routing <--> device interface */
221 unsigned int mctp_default_net(struct net *net);
222 int mctp_default_net_set(struct net *net, unsigned int index);
223 int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr);
224 int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr);
225 void mctp_route_remove_dev(struct mctp_dev *mdev);
226 
227 /* neighbour definitions */
228 enum mctp_neigh_source {
229 	MCTP_NEIGH_STATIC,
230 	MCTP_NEIGH_DISCOVER,
231 };
232 
233 struct mctp_neigh {
234 	struct mctp_dev		*dev;
235 	mctp_eid_t		eid;
236 	enum mctp_neigh_source	source;
237 
238 	unsigned char		ha[MAX_ADDR_LEN];
239 
240 	struct list_head	list;
241 	struct rcu_head		rcu;
242 };
243 
244 int mctp_neigh_init(void);
245 void mctp_neigh_exit(void);
246 
247 // ret_hwaddr may be NULL, otherwise must have space for MAX_ADDR_LEN
248 int mctp_neigh_lookup(struct mctp_dev *dev, mctp_eid_t eid,
249 		      void *ret_hwaddr);
250 void mctp_neigh_remove_dev(struct mctp_dev *mdev);
251 
252 int mctp_routes_init(void);
253 void mctp_routes_exit(void);
254 
255 void mctp_device_init(void);
256 void mctp_device_exit(void);
257 
258 #endif /* __NET_MCTP_H */
259