mctp.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #ifndef __NET_MCTP_H
  9. #define __NET_MCTP_H
  10. #include <linux/bits.h>
  11. #include <linux/mctp.h>
  12. #include <linux/netdevice.h>
  13. #include <net/net_namespace.h>
  14. #include <net/sock.h>
  15. /* MCTP packet definitions */
  16. struct mctp_hdr {
  17. u8 ver;
  18. u8 dest;
  19. u8 src;
  20. u8 flags_seq_tag;
  21. };
  22. #define MCTP_VER_MIN 1
  23. #define MCTP_VER_MAX 1
  24. /* Definitions for flags_seq_tag field */
  25. #define MCTP_HDR_FLAG_SOM BIT(7)
  26. #define MCTP_HDR_FLAG_EOM BIT(6)
  27. #define MCTP_HDR_FLAG_TO BIT(3)
  28. #define MCTP_HDR_FLAGS GENMASK(5, 3)
  29. #define MCTP_HDR_SEQ_SHIFT 4
  30. #define MCTP_HDR_SEQ_MASK GENMASK(1, 0)
  31. #define MCTP_HDR_TAG_SHIFT 0
  32. #define MCTP_HDR_TAG_MASK GENMASK(2, 0)
  33. #define MCTP_INITIAL_DEFAULT_NET 1
  34. static inline bool mctp_address_unicast(mctp_eid_t eid)
  35. {
  36. return eid >= 8 && eid < 255;
  37. }
  38. static inline bool mctp_address_broadcast(mctp_eid_t eid)
  39. {
  40. return eid == 255;
  41. }
  42. static inline bool mctp_address_null(mctp_eid_t eid)
  43. {
  44. return eid == 0;
  45. }
  46. static inline bool mctp_address_matches(mctp_eid_t match, mctp_eid_t eid)
  47. {
  48. return match == eid || match == MCTP_ADDR_ANY;
  49. }
  50. static inline struct mctp_hdr *mctp_hdr(struct sk_buff *skb)
  51. {
  52. return (struct mctp_hdr *)skb_network_header(skb);
  53. }
  54. /* socket implementation */
  55. struct mctp_sock {
  56. struct sock sk;
  57. /* bind() params */
  58. unsigned int bind_net;
  59. mctp_eid_t bind_local_addr;
  60. mctp_eid_t bind_peer_addr;
  61. unsigned int bind_peer_net;
  62. bool bind_peer_set;
  63. __u8 bind_type;
  64. /* sendmsg()/recvmsg() uses struct sockaddr_mctp_ext */
  65. bool addr_ext;
  66. /* list of mctp_sk_key, for incoming tag lookup. updates protected
  67. * by sk->net->keys_lock
  68. */
  69. struct hlist_head keys;
  70. /* mechanism for expiring allocated keys; will release an allocated
  71. * tag, and any netdev state for a request/response pairing
  72. */
  73. struct timer_list key_expiry;
  74. };
  75. /* Key for matching incoming packets to sockets or reassembly contexts.
  76. * Packets are matched on (peer EID, local EID, tag).
  77. *
  78. * Lifetime / locking requirements:
  79. *
  80. * - individual key data (ie, the struct itself) is protected by key->lock;
  81. * changes must be made with that lock held.
  82. *
  83. * - the lookup fields: peer_addr, local_addr and tag are set before the
  84. * key is added to lookup lists, and never updated.
  85. *
  86. * - A ref to the key must be held (throuh key->refs) if a pointer to the
  87. * key is to be accessed after key->lock is released.
  88. *
  89. * - a mctp_sk_key contains a reference to a struct sock; this is valid
  90. * for the life of the key. On sock destruction (through unhash), the key is
  91. * removed from lists (see below), and marked invalid.
  92. *
  93. * - these mctp_sk_keys appear on two lists:
  94. * 1) the struct mctp_sock->keys list
  95. * 2) the struct netns_mctp->keys list
  96. *
  97. * presences on these lists requires a (single) refcount to be held; both
  98. * lists are updated as a single operation.
  99. *
  100. * Updates and lookups in either list are performed under the
  101. * netns_mctp->keys lock. Lookup functions will need to lock the key and
  102. * take a reference before unlocking the keys_lock. Consequently, the list's
  103. * keys_lock *cannot* be acquired with the individual key->lock held.
  104. *
  105. * - a key may have a sk_buff attached as part of an in-progress message
  106. * reassembly (->reasm_head). The reasm data is protected by the individual
  107. * key->lock.
  108. *
  109. * - there are two destruction paths for a mctp_sk_key:
  110. *
  111. * - through socket unhash (see mctp_sk_unhash). This performs the list
  112. * removal under keys_lock.
  113. *
  114. * - where a key is established to receive a reply message: after receiving
  115. * the (complete) reply, or during reassembly errors. Here, we clean up
  116. * the reassembly context (marking reasm_dead, to prevent another from
  117. * starting), and remove the socket from the netns & socket lists.
  118. *
  119. * - through an expiry timeout, on a per-socket timer
  120. */
  121. struct mctp_sk_key {
  122. unsigned int net;
  123. mctp_eid_t peer_addr;
  124. mctp_eid_t local_addr; /* MCTP_ADDR_ANY for local owned tags */
  125. __u8 tag; /* incoming tag match; invert TO for local */
  126. /* we hold a ref to sk when set */
  127. struct sock *sk;
  128. /* routing lookup list */
  129. struct hlist_node hlist;
  130. /* per-socket list */
  131. struct hlist_node sklist;
  132. /* lock protects against concurrent updates to the reassembly and
  133. * expiry data below.
  134. */
  135. spinlock_t lock;
  136. /* Keys are referenced during the output path, which may sleep */
  137. refcount_t refs;
  138. /* incoming fragment reassembly context */
  139. struct sk_buff *reasm_head;
  140. struct sk_buff **reasm_tailp;
  141. bool reasm_dead;
  142. u8 last_seq;
  143. /* key validity */
  144. bool valid;
  145. /* expiry timeout; valid (above) cleared on expiry */
  146. unsigned long expiry;
  147. /* free to use for device flow state tracking. Initialised to
  148. * zero on initial key creation
  149. */
  150. unsigned long dev_flow_state;
  151. struct mctp_dev *dev;
  152. /* a tag allocated with SIOCMCTPALLOCTAG ioctl will not expire
  153. * automatically on timeout or response, instead SIOCMCTPDROPTAG
  154. * is used.
  155. */
  156. bool manual_alloc;
  157. };
  158. struct mctp_skb_cb {
  159. unsigned int magic;
  160. unsigned int net;
  161. /* fields below provide extended addressing for ingress to recvmsg() */
  162. int ifindex;
  163. unsigned char halen;
  164. unsigned char haddr[MAX_ADDR_LEN];
  165. };
  166. /* skb control-block accessors with a little extra debugging for initial
  167. * development.
  168. *
  169. * TODO: remove checks & mctp_skb_cb->magic; replace callers of __mctp_cb
  170. * with mctp_cb().
  171. *
  172. * __mctp_cb() is only for the initial ingress code; we should see ->magic set
  173. * at all times after this.
  174. */
  175. static inline struct mctp_skb_cb *__mctp_cb(struct sk_buff *skb)
  176. {
  177. struct mctp_skb_cb *cb = (void *)skb->cb;
  178. cb->magic = 0x4d435450;
  179. return cb;
  180. }
  181. static inline struct mctp_skb_cb *mctp_cb(struct sk_buff *skb)
  182. {
  183. struct mctp_skb_cb *cb = (void *)skb->cb;
  184. BUILD_BUG_ON(sizeof(struct mctp_skb_cb) > sizeof(skb->cb));
  185. WARN_ON(cb->magic != 0x4d435450);
  186. return cb;
  187. }
  188. /* If CONFIG_MCTP_FLOWS, we may add one of these as a SKB extension,
  189. * indicating the flow to the device driver.
  190. */
  191. struct mctp_flow {
  192. struct mctp_sk_key *key;
  193. };
  194. struct mctp_dst;
  195. /* Route definition.
  196. *
  197. * These are held in the pernet->mctp.routes list, with RCU protection for
  198. * removed routes. We hold a reference to the netdev; routes need to be
  199. * dropped on NETDEV_UNREGISTER events.
  200. *
  201. * Updates to the route table are performed under rtnl; all reads under RCU,
  202. * so routes cannot be referenced over a RCU grace period.
  203. */
  204. struct mctp_route {
  205. mctp_eid_t min, max;
  206. unsigned char type;
  207. unsigned int mtu;
  208. enum {
  209. MCTP_ROUTE_DIRECT,
  210. MCTP_ROUTE_GATEWAY,
  211. } dst_type;
  212. union {
  213. struct mctp_dev *dev;
  214. struct mctp_fq_addr gateway;
  215. };
  216. int (*output)(struct mctp_dst *dst,
  217. struct sk_buff *skb);
  218. struct list_head list;
  219. refcount_t refs;
  220. struct rcu_head rcu;
  221. };
  222. /* Route lookup result: dst. Represents the results of a routing decision,
  223. * but is only held over the individual routing operation.
  224. *
  225. * Will typically be stored on the caller stack, and must be released after
  226. * usage.
  227. */
  228. struct mctp_dst {
  229. struct mctp_dev *dev;
  230. unsigned int mtu;
  231. mctp_eid_t nexthop;
  232. /* set for direct addressing */
  233. unsigned char halen;
  234. unsigned char haddr[MAX_ADDR_LEN];
  235. int (*output)(struct mctp_dst *dst, struct sk_buff *skb);
  236. };
  237. int mctp_dst_from_extaddr(struct mctp_dst *dst, struct net *net, int ifindex,
  238. unsigned char halen, const unsigned char *haddr);
  239. /* route interfaces */
  240. int mctp_route_lookup(struct net *net, unsigned int dnet,
  241. mctp_eid_t daddr, struct mctp_dst *dst);
  242. void mctp_dst_release(struct mctp_dst *dst);
  243. /* always takes ownership of skb */
  244. int mctp_local_output(struct sock *sk, struct mctp_dst *dst,
  245. struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag);
  246. void mctp_key_unref(struct mctp_sk_key *key);
  247. struct mctp_sk_key *mctp_alloc_local_tag(struct mctp_sock *msk,
  248. unsigned int netid,
  249. mctp_eid_t local, mctp_eid_t peer,
  250. bool manual, u8 *tagp);
  251. /* routing <--> device interface */
  252. unsigned int mctp_default_net(struct net *net);
  253. int mctp_default_net_set(struct net *net, unsigned int index);
  254. int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr);
  255. int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr);
  256. void mctp_route_remove_dev(struct mctp_dev *mdev);
  257. /* neighbour definitions */
  258. enum mctp_neigh_source {
  259. MCTP_NEIGH_STATIC,
  260. MCTP_NEIGH_DISCOVER,
  261. };
  262. struct mctp_neigh {
  263. struct mctp_dev *dev;
  264. mctp_eid_t eid;
  265. enum mctp_neigh_source source;
  266. unsigned char ha[MAX_ADDR_LEN];
  267. struct list_head list;
  268. struct rcu_head rcu;
  269. };
  270. int mctp_neigh_init(void);
  271. void mctp_neigh_exit(void);
  272. // ret_hwaddr may be NULL, otherwise must have space for MAX_ADDR_LEN
  273. int mctp_neigh_lookup(struct mctp_dev *dev, mctp_eid_t eid,
  274. void *ret_hwaddr);
  275. void mctp_neigh_remove_dev(struct mctp_dev *mdev);
  276. int mctp_routes_init(void);
  277. void mctp_routes_exit(void);
  278. int mctp_device_init(void);
  279. void mctp_device_exit(void);
  280. /* MCTP IDs and Codes from DMTF specification
  281. * "DSP0239 Management Component Transport Protocol (MCTP) IDs and Codes"
  282. * https://www.dmtf.org/sites/default/files/standards/documents/DSP0239_1.11.1.pdf
  283. */
  284. enum mctp_phys_binding {
  285. MCTP_PHYS_BINDING_UNSPEC = 0x00,
  286. MCTP_PHYS_BINDING_SMBUS = 0x01,
  287. MCTP_PHYS_BINDING_PCIE_VDM = 0x02,
  288. MCTP_PHYS_BINDING_USB = 0x03,
  289. MCTP_PHYS_BINDING_KCS = 0x04,
  290. MCTP_PHYS_BINDING_SERIAL = 0x05,
  291. MCTP_PHYS_BINDING_I3C = 0x06,
  292. MCTP_PHYS_BINDING_MMBI = 0x07,
  293. MCTP_PHYS_BINDING_PCC = 0x08,
  294. MCTP_PHYS_BINDING_UCIE = 0x09,
  295. MCTP_PHYS_BINDING_VENDOR = 0xFF,
  296. };
  297. #endif /* __NET_MCTP_H */