]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - include/net/mctp.h
1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Management Component Transport Protocol (MCTP)
5 * Copyright (c) 2021 Code Construct
6 * Copyright (c) 2021 Google
12 #include <linux/bits.h>
13 #include <linux/mctp.h>
14 #include <net/net_namespace.h>
17 /* MCTP packet definitions */
25 #define MCTP_VER_MIN 1
26 #define MCTP_VER_MAX 1
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)
38 #define MCTP_HEADER_MAXLEN 4
40 static inline bool mctp_address_ok(mctp_eid_t eid
)
42 return eid
>= 8 && eid
< 255;
45 static inline struct mctp_hdr
*mctp_hdr(struct sk_buff
*skb
)
47 return (struct mctp_hdr
*)skb_network_header(skb
);
50 /* socket implementation */
59 /* list of mctp_sk_key, for incoming tag lookup. updates protected
60 * by sk->net->keys_lock
62 struct hlist_head keys
;
65 /* Key for matching incoming packets to sockets or reassembly contexts.
66 * Packets are matched on (src,dest,tag).
68 * Lifetime requirements:
70 * - keys are free()ed via RCU
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
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.
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
84 * updates to either list are performed under the netns_mctp->keys
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
93 * - there are two destruction paths for a mctp_sk_key:
95 * - through socket unhash (see mctp_sk_unhash). This performs the list
96 * removal under keys_lock.
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.
104 mctp_eid_t peer_addr
;
105 mctp_eid_t local_addr
;
106 __u8 tag
; /* incoming tag match; invert TO for local */
108 /* we hold a ref to sk when set */
111 /* routing lookup list */
112 struct hlist_node hlist
;
114 /* per-socket list */
115 struct hlist_node sklist
;
117 /* incoming fragment reassembly context */
118 spinlock_t reasm_lock
;
119 struct sk_buff
*reasm_head
;
120 struct sk_buff
**reasm_tailp
;
133 /* skb control-block accessors with a little extra debugging for initial
136 * TODO: remove checks & mctp_skb_cb->magic; replace callers of __mctp_cb
139 * __mctp_cb() is only for the initial ingress code; we should see ->magic set
140 * at all times after this.
142 static inline struct mctp_skb_cb
*__mctp_cb(struct sk_buff
*skb
)
144 struct mctp_skb_cb
*cb
= (void *)skb
->cb
;
146 cb
->magic
= 0x4d435450;
150 static inline struct mctp_skb_cb
*mctp_cb(struct sk_buff
*skb
)
152 struct mctp_skb_cb
*cb
= (void *)skb
->cb
;
154 WARN_ON(cb
->magic
!= 0x4d435450);
155 return (void *)(skb
->cb
);
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.
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
172 struct mctp_dev
*dev
;
174 int (*output
)(struct mctp_route
*route
,
175 struct sk_buff
*skb
);
177 struct list_head list
;
182 /* route interfaces */
183 struct mctp_route
*mctp_route_lookup(struct net
*net
, unsigned int dnet
,
186 int mctp_do_route(struct mctp_route
*rt
, struct sk_buff
*skb
);
188 int mctp_local_output(struct sock
*sk
, struct mctp_route
*rt
,
189 struct sk_buff
*skb
, mctp_eid_t daddr
, u8 req_tag
);
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
);
198 /* neighbour definitions */
199 enum mctp_neigh_source
{
205 struct mctp_dev
*dev
;
207 enum mctp_neigh_source source
;
209 unsigned char ha
[MAX_ADDR_LEN
];
211 struct list_head list
;
215 int mctp_neigh_init(void);
216 void mctp_neigh_exit(void);
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
,
221 void mctp_neigh_remove_dev(struct mctp_dev
*mdev
);
223 int mctp_routes_init(void);
224 void mctp_routes_exit(void);
226 void mctp_device_init(void);
227 void mctp_device_exit(void);
229 #endif /* __NET_MCTP_H */