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