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