Jeremy Kerr | 2c8e2e9 | 2021-07-29 10:20:41 +0800 | [diff] [blame] | 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> |
Jeremy Kerr | 583be98 | 2021-07-29 10:20:44 +0800 | [diff] [blame] | 13 | #include <linux/mctp.h> |
Jeremy Kerr | 99ce45d | 2021-10-26 09:57:28 +0800 | [diff] [blame] | 14 | #include <linux/netdevice.h> |
Jeremy Kerr | 889b7da | 2021-07-29 10:20:45 +0800 | [diff] [blame] | 15 | #include <net/net_namespace.h> |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 16 | #include <net/sock.h> |
Jeremy Kerr | 2c8e2e9 | 2021-07-29 10:20:41 +0800 | [diff] [blame] | 17 | |
| 18 | /* MCTP packet definitions */ |
| 19 | struct mctp_hdr { |
| 20 | u8 ver; |
| 21 | u8 dest; |
| 22 | u8 src; |
| 23 | u8 flags_seq_tag; |
| 24 | }; |
| 25 | |
| 26 | #define MCTP_VER_MIN 1 |
| 27 | #define MCTP_VER_MAX 1 |
| 28 | |
| 29 | /* Definitions for flags_seq_tag field */ |
| 30 | #define MCTP_HDR_FLAG_SOM BIT(7) |
| 31 | #define MCTP_HDR_FLAG_EOM BIT(6) |
| 32 | #define MCTP_HDR_FLAG_TO BIT(3) |
| 33 | #define MCTP_HDR_FLAGS GENMASK(5, 3) |
| 34 | #define MCTP_HDR_SEQ_SHIFT 4 |
| 35 | #define MCTP_HDR_SEQ_MASK GENMASK(1, 0) |
| 36 | #define MCTP_HDR_TAG_SHIFT 0 |
| 37 | #define MCTP_HDR_TAG_MASK GENMASK(2, 0) |
| 38 | |
Jeremy Kerr | 889b7da | 2021-07-29 10:20:45 +0800 | [diff] [blame] | 39 | #define MCTP_HEADER_MAXLEN 4 |
| 40 | |
Matt Johnston | 03f2bbc | 2021-07-29 10:20:52 +0800 | [diff] [blame] | 41 | #define MCTP_INITIAL_DEFAULT_NET 1 |
| 42 | |
Jeremy Kerr | 583be98 | 2021-07-29 10:20:44 +0800 | [diff] [blame] | 43 | static inline bool mctp_address_ok(mctp_eid_t eid) |
| 44 | { |
| 45 | return eid >= 8 && eid < 255; |
| 46 | } |
| 47 | |
| 48 | static inline struct mctp_hdr *mctp_hdr(struct sk_buff *skb) |
| 49 | { |
| 50 | return (struct mctp_hdr *)skb_network_header(skb); |
| 51 | } |
| 52 | |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 53 | /* socket implementation */ |
| 54 | struct mctp_sock { |
| 55 | struct sock sk; |
| 56 | |
| 57 | /* bind() params */ |
Jeremy Kerr | b416beb | 2021-10-18 11:29:34 +0800 | [diff] [blame] | 58 | unsigned int bind_net; |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 59 | mctp_eid_t bind_addr; |
| 60 | __u8 bind_type; |
| 61 | |
Jeremy Kerr | 99ce45d | 2021-10-26 09:57:28 +0800 | [diff] [blame] | 62 | /* sendmsg()/recvmsg() uses struct sockaddr_mctp_ext */ |
| 63 | bool addr_ext; |
| 64 | |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 65 | /* list of mctp_sk_key, for incoming tag lookup. updates protected |
| 66 | * by sk->net->keys_lock |
| 67 | */ |
| 68 | struct hlist_head keys; |
Jeremy Kerr | 7b14e15 | 2021-09-29 15:26:09 +0800 | [diff] [blame] | 69 | |
| 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; |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /* Key for matching incoming packets to sockets or reassembly contexts. |
| 77 | * Packets are matched on (src,dest,tag). |
| 78 | * |
Jeremy Kerr | 73c6184 | 2021-09-29 15:26:07 +0800 | [diff] [blame] | 79 | * Lifetime / locking requirements: |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 80 | * |
Jeremy Kerr | 73c6184 | 2021-09-29 15:26:07 +0800 | [diff] [blame] | 81 | * - individual key data (ie, the struct itself) is protected by key->lock; |
| 82 | * changes must be made with that lock held. |
| 83 | * |
| 84 | * - the lookup fields: peer_addr, local_addr and tag are set before the |
| 85 | * key is added to lookup lists, and never updated. |
| 86 | * |
| 87 | * - A ref to the key must be held (throuh key->refs) if a pointer to the |
| 88 | * key is to be accessed after key->lock is released. |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 89 | * |
| 90 | * - a mctp_sk_key contains a reference to a struct sock; this is valid |
| 91 | * for the life of the key. On sock destruction (through unhash), the key is |
Jeremy Kerr | 73c6184 | 2021-09-29 15:26:07 +0800 | [diff] [blame] | 92 | * removed from lists (see below), and marked invalid. |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 93 | * |
| 94 | * - these mctp_sk_keys appear on two lists: |
| 95 | * 1) the struct mctp_sock->keys list |
| 96 | * 2) the struct netns_mctp->keys list |
| 97 | * |
Jeremy Kerr | 73c6184 | 2021-09-29 15:26:07 +0800 | [diff] [blame] | 98 | * presences on these lists requires a (single) refcount to be held; both |
| 99 | * lists are updated as a single operation. |
| 100 | * |
| 101 | * Updates and lookups in either list are performed under the |
| 102 | * netns_mctp->keys lock. Lookup functions will need to lock the key and |
| 103 | * take a reference before unlocking the keys_lock. Consequently, the list's |
| 104 | * keys_lock *cannot* be acquired with the individual key->lock held. |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 105 | * |
Jeremy Kerr | 4a992bb | 2021-07-29 10:20:50 +0800 | [diff] [blame] | 106 | * - a key may have a sk_buff attached as part of an in-progress message |
Jeremy Kerr | 73c6184 | 2021-09-29 15:26:07 +0800 | [diff] [blame] | 107 | * reassembly (->reasm_head). The reasm data is protected by the individual |
| 108 | * key->lock. |
Jeremy Kerr | 4a992bb | 2021-07-29 10:20:50 +0800 | [diff] [blame] | 109 | * |
| 110 | * - there are two destruction paths for a mctp_sk_key: |
| 111 | * |
| 112 | * - through socket unhash (see mctp_sk_unhash). This performs the list |
| 113 | * removal under keys_lock. |
| 114 | * |
| 115 | * - where a key is established to receive a reply message: after receiving |
| 116 | * the (complete) reply, or during reassembly errors. Here, we clean up |
| 117 | * the reassembly context (marking reasm_dead, to prevent another from |
| 118 | * starting), and remove the socket from the netns & socket lists. |
Jeremy Kerr | 7b14e15 | 2021-09-29 15:26:09 +0800 | [diff] [blame] | 119 | * |
| 120 | * - through an expiry timeout, on a per-socket timer |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 121 | */ |
| 122 | struct mctp_sk_key { |
| 123 | mctp_eid_t peer_addr; |
| 124 | mctp_eid_t local_addr; |
| 125 | __u8 tag; /* incoming tag match; invert TO for local */ |
| 126 | |
| 127 | /* we hold a ref to sk when set */ |
| 128 | struct sock *sk; |
| 129 | |
| 130 | /* routing lookup list */ |
| 131 | struct hlist_node hlist; |
| 132 | |
| 133 | /* per-socket list */ |
| 134 | struct hlist_node sklist; |
| 135 | |
Jeremy Kerr | 73c6184 | 2021-09-29 15:26:07 +0800 | [diff] [blame] | 136 | /* lock protects against concurrent updates to the reassembly and |
| 137 | * expiry data below. |
| 138 | */ |
| 139 | spinlock_t lock; |
| 140 | |
| 141 | /* Keys are referenced during the output path, which may sleep */ |
| 142 | refcount_t refs; |
| 143 | |
Jeremy Kerr | 4a992bb | 2021-07-29 10:20:50 +0800 | [diff] [blame] | 144 | /* incoming fragment reassembly context */ |
Jeremy Kerr | 4a992bb | 2021-07-29 10:20:50 +0800 | [diff] [blame] | 145 | struct sk_buff *reasm_head; |
| 146 | struct sk_buff **reasm_tailp; |
| 147 | bool reasm_dead; |
| 148 | u8 last_seq; |
| 149 | |
Jeremy Kerr | 73c6184 | 2021-09-29 15:26:07 +0800 | [diff] [blame] | 150 | /* key validity */ |
| 151 | bool valid; |
Jeremy Kerr | 7b14e15 | 2021-09-29 15:26:09 +0800 | [diff] [blame] | 152 | |
| 153 | /* expiry timeout; valid (above) cleared on expiry */ |
| 154 | unsigned long expiry; |
Jeremy Kerr | 67737c4 | 2021-10-29 11:01:45 +0800 | [diff] [blame] | 155 | |
| 156 | /* free to use for device flow state tracking. Initialised to |
| 157 | * zero on initial key creation |
| 158 | */ |
| 159 | unsigned long dev_flow_state; |
| 160 | struct mctp_dev *dev; |
Jeremy Kerr | 833ef3b | 2021-07-29 10:20:49 +0800 | [diff] [blame] | 161 | }; |
| 162 | |
Jeremy Kerr | 889b7da | 2021-07-29 10:20:45 +0800 | [diff] [blame] | 163 | struct mctp_skb_cb { |
| 164 | unsigned int magic; |
| 165 | unsigned int net; |
Jeremy Kerr | 99ce45d | 2021-10-26 09:57:28 +0800 | [diff] [blame] | 166 | int ifindex; /* extended/direct addressing if set */ |
Jeremy Kerr | 889b7da | 2021-07-29 10:20:45 +0800 | [diff] [blame] | 167 | mctp_eid_t src; |
Jeremy Kerr | 99ce45d | 2021-10-26 09:57:28 +0800 | [diff] [blame] | 168 | unsigned char halen; |
| 169 | unsigned char haddr[MAX_ADDR_LEN]; |
Jeremy Kerr | 889b7da | 2021-07-29 10:20:45 +0800 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | /* skb control-block accessors with a little extra debugging for initial |
| 173 | * development. |
| 174 | * |
| 175 | * TODO: remove checks & mctp_skb_cb->magic; replace callers of __mctp_cb |
| 176 | * with mctp_cb(). |
| 177 | * |
| 178 | * __mctp_cb() is only for the initial ingress code; we should see ->magic set |
| 179 | * at all times after this. |
| 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 | |
| 185 | cb->magic = 0x4d435450; |
| 186 | return cb; |
| 187 | } |
| 188 | |
| 189 | static inline struct mctp_skb_cb *mctp_cb(struct sk_buff *skb) |
| 190 | { |
| 191 | struct mctp_skb_cb *cb = (void *)skb->cb; |
| 192 | |
Jeremy Kerr | 99ce45d | 2021-10-26 09:57:28 +0800 | [diff] [blame] | 193 | BUILD_BUG_ON(sizeof(struct mctp_skb_cb) > sizeof(skb->cb)); |
Jeremy Kerr | 889b7da | 2021-07-29 10:20:45 +0800 | [diff] [blame] | 194 | WARN_ON(cb->magic != 0x4d435450); |
| 195 | return (void *)(skb->cb); |
| 196 | } |
| 197 | |
Jeremy Kerr | 78476d3 | 2021-10-29 11:01:44 +0800 | [diff] [blame] | 198 | /* If CONFIG_MCTP_FLOWS, we may add one of these as a SKB extension, |
| 199 | * indicating the flow to the device driver. |
| 200 | */ |
| 201 | struct mctp_flow { |
| 202 | struct mctp_sk_key *key; |
| 203 | }; |
| 204 | |
Jeremy Kerr | 889b7da | 2021-07-29 10:20:45 +0800 | [diff] [blame] | 205 | /* Route definition. |
| 206 | * |
| 207 | * These are held in the pernet->mctp.routes list, with RCU protection for |
| 208 | * removed routes. We hold a reference to the netdev; routes need to be |
| 209 | * dropped on NETDEV_UNREGISTER events. |
| 210 | * |
| 211 | * Updates to the route table are performed under rtnl; all reads under RCU, |
| 212 | * so routes cannot be referenced over a RCU grace period. Specifically: A |
Jeremy Kerr | 99ce45d | 2021-10-26 09:57:28 +0800 | [diff] [blame] | 213 | * caller cannot block between mctp_route_lookup and mctp_route_release() |
Jeremy Kerr | 889b7da | 2021-07-29 10:20:45 +0800 | [diff] [blame] | 214 | */ |
| 215 | struct mctp_route { |
| 216 | mctp_eid_t min, max; |
| 217 | |
| 218 | struct mctp_dev *dev; |
| 219 | unsigned int mtu; |
Jeremy Kerr | 83f0a0b | 2021-08-10 10:38:34 +0800 | [diff] [blame] | 220 | unsigned char type; |
Jeremy Kerr | 889b7da | 2021-07-29 10:20:45 +0800 | [diff] [blame] | 221 | int (*output)(struct mctp_route *route, |
| 222 | struct sk_buff *skb); |
| 223 | |
| 224 | struct list_head list; |
| 225 | refcount_t refs; |
| 226 | struct rcu_head rcu; |
| 227 | }; |
| 228 | |
| 229 | /* route interfaces */ |
| 230 | struct mctp_route *mctp_route_lookup(struct net *net, unsigned int dnet, |
| 231 | mctp_eid_t daddr); |
| 232 | |
Jeremy Kerr | 889b7da | 2021-07-29 10:20:45 +0800 | [diff] [blame] | 233 | int mctp_local_output(struct sock *sk, struct mctp_route *rt, |
| 234 | struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag); |
| 235 | |
Jeremy Kerr | 73c6184 | 2021-09-29 15:26:07 +0800 | [diff] [blame] | 236 | void mctp_key_unref(struct mctp_sk_key *key); |
| 237 | |
Jeremy Kerr | 889b7da | 2021-07-29 10:20:45 +0800 | [diff] [blame] | 238 | /* routing <--> device interface */ |
Matt Johnston | 06d2f4c | 2021-07-29 10:20:46 +0800 | [diff] [blame] | 239 | unsigned int mctp_default_net(struct net *net); |
| 240 | int mctp_default_net_set(struct net *net, unsigned int index); |
Jeremy Kerr | 889b7da | 2021-07-29 10:20:45 +0800 | [diff] [blame] | 241 | int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr); |
| 242 | int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr); |
| 243 | void mctp_route_remove_dev(struct mctp_dev *mdev); |
| 244 | |
Matt Johnston | 4d8b931 | 2021-07-29 10:20:47 +0800 | [diff] [blame] | 245 | /* neighbour definitions */ |
| 246 | enum mctp_neigh_source { |
| 247 | MCTP_NEIGH_STATIC, |
| 248 | MCTP_NEIGH_DISCOVER, |
| 249 | }; |
| 250 | |
| 251 | struct mctp_neigh { |
| 252 | struct mctp_dev *dev; |
| 253 | mctp_eid_t eid; |
| 254 | enum mctp_neigh_source source; |
| 255 | |
| 256 | unsigned char ha[MAX_ADDR_LEN]; |
| 257 | |
| 258 | struct list_head list; |
| 259 | struct rcu_head rcu; |
| 260 | }; |
| 261 | |
| 262 | int mctp_neigh_init(void); |
| 263 | void mctp_neigh_exit(void); |
| 264 | |
| 265 | // ret_hwaddr may be NULL, otherwise must have space for MAX_ADDR_LEN |
| 266 | int mctp_neigh_lookup(struct mctp_dev *dev, mctp_eid_t eid, |
| 267 | void *ret_hwaddr); |
| 268 | void mctp_neigh_remove_dev(struct mctp_dev *mdev); |
| 269 | |
Jeremy Kerr | 889b7da | 2021-07-29 10:20:45 +0800 | [diff] [blame] | 270 | int mctp_routes_init(void); |
| 271 | void mctp_routes_exit(void); |
| 272 | |
Jeremy Kerr | 583be98 | 2021-07-29 10:20:44 +0800 | [diff] [blame] | 273 | void mctp_device_init(void); |
| 274 | void mctp_device_exit(void); |
| 275 | |
Jeremy Kerr | 2c8e2e9 | 2021-07-29 10:20:41 +0800 | [diff] [blame] | 276 | #endif /* __NET_MCTP_H */ |