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 #define MCTP_INITIAL_DEFAULT_NET 1
42 static inline bool mctp_address_ok(mctp_eid_t eid)
44 return eid >= 8 && eid < 255;
47 static inline struct mctp_hdr *mctp_hdr(struct sk_buff *skb)
49 return (struct mctp_hdr *)skb_network_header(skb);
52 /* socket implementation */
57 unsigned int bind_net;
61 /* list of mctp_sk_key, for incoming tag lookup. updates protected
62 * by sk->net->keys_lock
64 struct hlist_head keys;
67 /* Key for matching incoming packets to sockets or reassembly contexts.
68 * Packets are matched on (src,dest,tag).
70 * Lifetime requirements:
72 * - keys are free()ed via RCU
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
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.
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
86 * updates to either list are performed under the netns_mctp->keys
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
95 * - there are two destruction paths for a mctp_sk_key:
97 * - through socket unhash (see mctp_sk_unhash). This performs the list
98 * removal under keys_lock.
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.
106 mctp_eid_t peer_addr;
107 mctp_eid_t local_addr;
108 __u8 tag; /* incoming tag match; invert TO for local */
110 /* we hold a ref to sk when set */
113 /* routing lookup list */
114 struct hlist_node hlist;
116 /* per-socket list */
117 struct hlist_node sklist;
119 /* incoming fragment reassembly context */
120 spinlock_t reasm_lock;
121 struct sk_buff *reasm_head;
122 struct sk_buff **reasm_tailp;
135 /* skb control-block accessors with a little extra debugging for initial
138 * TODO: remove checks & mctp_skb_cb->magic; replace callers of __mctp_cb
141 * __mctp_cb() is only for the initial ingress code; we should see ->magic set
142 * at all times after this.
144 static inline struct mctp_skb_cb *__mctp_cb(struct sk_buff *skb)
146 struct mctp_skb_cb *cb = (void *)skb->cb;
148 cb->magic = 0x4d435450;
152 static inline struct mctp_skb_cb *mctp_cb(struct sk_buff *skb)
154 struct mctp_skb_cb *cb = (void *)skb->cb;
156 WARN_ON(cb->magic != 0x4d435450);
157 return (void *)(skb->cb);
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.
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
174 struct mctp_dev *dev;
177 int (*output)(struct mctp_route *route,
178 struct sk_buff *skb);
180 struct list_head list;
185 /* route interfaces */
186 struct mctp_route *mctp_route_lookup(struct net *net, unsigned int dnet,
189 int mctp_do_route(struct mctp_route *rt, struct sk_buff *skb);
191 int mctp_local_output(struct sock *sk, struct mctp_route *rt,
192 struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag);
194 /* routing <--> device interface */
195 unsigned int mctp_default_net(struct net *net);
196 int mctp_default_net_set(struct net *net, unsigned int index);
197 int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr);
198 int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr);
199 void mctp_route_remove_dev(struct mctp_dev *mdev);
201 /* neighbour definitions */
202 enum mctp_neigh_source {
208 struct mctp_dev *dev;
210 enum mctp_neigh_source source;
212 unsigned char ha[MAX_ADDR_LEN];
214 struct list_head list;
218 int mctp_neigh_init(void);
219 void mctp_neigh_exit(void);
221 // ret_hwaddr may be NULL, otherwise must have space for MAX_ADDR_LEN
222 int mctp_neigh_lookup(struct mctp_dev *dev, mctp_eid_t eid,
224 void mctp_neigh_remove_dev(struct mctp_dev *mdev);
226 int mctp_routes_init(void);
227 void mctp_routes_exit(void);
229 void mctp_device_init(void);
230 void mctp_device_exit(void);
232 #endif /* __NET_MCTP_H */