]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/net/mctp.h
Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[mirror_ubuntu-jammy-kernel.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>
889b7da2 14#include <net/net_namespace.h>
833ef3b9 15#include <net/sock.h>
2c8e2e9a
JK
16
17/* MCTP packet definitions */
18struct 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
889b7da2
JK
38#define MCTP_HEADER_MAXLEN 4
39
03f2bbc4
MJ
40#define MCTP_INITIAL_DEFAULT_NET 1
41
583be982
JK
42static inline bool mctp_address_ok(mctp_eid_t eid)
43{
44 return eid >= 8 && eid < 255;
45}
46
47static inline struct mctp_hdr *mctp_hdr(struct sk_buff *skb)
48{
49 return (struct mctp_hdr *)skb_network_header(skb);
50}
51
833ef3b9
JK
52/* socket implementation */
53struct 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
67/* Key for matching incoming packets to sockets or reassembly contexts.
68 * Packets are matched on (src,dest,tag).
69 *
70 * Lifetime requirements:
71 *
72 * - keys are free()ed via RCU
73 *
74 * - a mctp_sk_key contains a reference to a struct sock; this is valid
75 * for the life of the key. On sock destruction (through unhash), the key is
76 * removed from lists (see below), and will not be observable after a RCU
77 * grace period.
78 *
79 * any RX occurring within that grace period may still queue to the socket,
80 * but will hit the SOCK_DEAD case before the socket is freed.
81 *
82 * - these mctp_sk_keys appear on two lists:
83 * 1) the struct mctp_sock->keys list
84 * 2) the struct netns_mctp->keys list
85 *
86 * updates to either list are performed under the netns_mctp->keys
87 * lock.
88 *
4a992bbd
JK
89 * - a key may have a sk_buff attached as part of an in-progress message
90 * reassembly (->reasm_head). The reassembly context is protected by
91 * reasm_lock, which may be acquired with the keys lock (above) held, if
92 * necessary. Consequently, keys lock *cannot* be acquired with the
93 * reasm_lock held.
94 *
95 * - there are two destruction paths for a mctp_sk_key:
96 *
97 * - through socket unhash (see mctp_sk_unhash). This performs the list
98 * removal under keys_lock.
99 *
100 * - where a key is established to receive a reply message: after receiving
101 * the (complete) reply, or during reassembly errors. Here, we clean up
102 * the reassembly context (marking reasm_dead, to prevent another from
103 * starting), and remove the socket from the netns & socket lists.
833ef3b9
JK
104 */
105struct mctp_sk_key {
106 mctp_eid_t peer_addr;
107 mctp_eid_t local_addr;
108 __u8 tag; /* incoming tag match; invert TO for local */
109
110 /* we hold a ref to sk when set */
111 struct sock *sk;
112
113 /* routing lookup list */
114 struct hlist_node hlist;
115
116 /* per-socket list */
117 struct hlist_node sklist;
118
4a992bbd
JK
119 /* incoming fragment reassembly context */
120 spinlock_t reasm_lock;
121 struct sk_buff *reasm_head;
122 struct sk_buff **reasm_tailp;
123 bool reasm_dead;
124 u8 last_seq;
125
833ef3b9
JK
126 struct rcu_head rcu;
127};
128
889b7da2
JK
129struct mctp_skb_cb {
130 unsigned int magic;
131 unsigned int net;
132 mctp_eid_t src;
133};
134
135/* skb control-block accessors with a little extra debugging for initial
136 * development.
137 *
138 * TODO: remove checks & mctp_skb_cb->magic; replace callers of __mctp_cb
139 * with mctp_cb().
140 *
141 * __mctp_cb() is only for the initial ingress code; we should see ->magic set
142 * at all times after this.
143 */
144static inline struct mctp_skb_cb *__mctp_cb(struct sk_buff *skb)
145{
146 struct mctp_skb_cb *cb = (void *)skb->cb;
147
148 cb->magic = 0x4d435450;
149 return cb;
150}
151
152static inline struct mctp_skb_cb *mctp_cb(struct sk_buff *skb)
153{
154 struct mctp_skb_cb *cb = (void *)skb->cb;
155
156 WARN_ON(cb->magic != 0x4d435450);
157 return (void *)(skb->cb);
158}
159
160/* Route definition.
161 *
162 * These are held in the pernet->mctp.routes list, with RCU protection for
163 * removed routes. We hold a reference to the netdev; routes need to be
164 * dropped on NETDEV_UNREGISTER events.
165 *
166 * Updates to the route table are performed under rtnl; all reads under RCU,
167 * so routes cannot be referenced over a RCU grace period. Specifically: A
168 * caller cannot block between mctp_route_lookup and passing the route to
169 * mctp_do_route.
170 */
171struct mctp_route {
172 mctp_eid_t min, max;
173
174 struct mctp_dev *dev;
175 unsigned int mtu;
83f0a0b7 176 unsigned char type;
889b7da2
JK
177 int (*output)(struct mctp_route *route,
178 struct sk_buff *skb);
179
180 struct list_head list;
181 refcount_t refs;
182 struct rcu_head rcu;
183};
184
185/* route interfaces */
186struct mctp_route *mctp_route_lookup(struct net *net, unsigned int dnet,
187 mctp_eid_t daddr);
188
189int mctp_do_route(struct mctp_route *rt, struct sk_buff *skb);
190
191int mctp_local_output(struct sock *sk, struct mctp_route *rt,
192 struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag);
193
194/* routing <--> device interface */
06d2f4c5
MJ
195unsigned int mctp_default_net(struct net *net);
196int mctp_default_net_set(struct net *net, unsigned int index);
889b7da2
JK
197int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr);
198int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr);
199void mctp_route_remove_dev(struct mctp_dev *mdev);
200
4d8b9319
MJ
201/* neighbour definitions */
202enum mctp_neigh_source {
203 MCTP_NEIGH_STATIC,
204 MCTP_NEIGH_DISCOVER,
205};
206
207struct mctp_neigh {
208 struct mctp_dev *dev;
209 mctp_eid_t eid;
210 enum mctp_neigh_source source;
211
212 unsigned char ha[MAX_ADDR_LEN];
213
214 struct list_head list;
215 struct rcu_head rcu;
216};
217
218int mctp_neigh_init(void);
219void mctp_neigh_exit(void);
220
221// ret_hwaddr may be NULL, otherwise must have space for MAX_ADDR_LEN
222int mctp_neigh_lookup(struct mctp_dev *dev, mctp_eid_t eid,
223 void *ret_hwaddr);
224void mctp_neigh_remove_dev(struct mctp_dev *mdev);
225
889b7da2
JK
226int mctp_routes_init(void);
227void mctp_routes_exit(void);
228
583be982
JK
229void mctp_device_init(void);
230void mctp_device_exit(void);
231
2c8e2e9a 232#endif /* __NET_MCTP_H */