Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Management Component Transport Protocol (MCTP)
0004  *
0005  * Copyright (c) 2021 Code Construct
0006  * Copyright (c) 2021 Google
0007  */
0008 
0009 #ifndef __NET_MCTP_H
0010 #define __NET_MCTP_H
0011 
0012 #include <linux/bits.h>
0013 #include <linux/mctp.h>
0014 #include <linux/netdevice.h>
0015 #include <net/net_namespace.h>
0016 #include <net/sock.h>
0017 
0018 /* MCTP packet definitions */
0019 struct mctp_hdr {
0020     u8  ver;
0021     u8  dest;
0022     u8  src;
0023     u8  flags_seq_tag;
0024 };
0025 
0026 #define MCTP_VER_MIN    1
0027 #define MCTP_VER_MAX    1
0028 
0029 /* Definitions for flags_seq_tag field */
0030 #define MCTP_HDR_FLAG_SOM   BIT(7)
0031 #define MCTP_HDR_FLAG_EOM   BIT(6)
0032 #define MCTP_HDR_FLAG_TO    BIT(3)
0033 #define MCTP_HDR_FLAGS      GENMASK(5, 3)
0034 #define MCTP_HDR_SEQ_SHIFT  4
0035 #define MCTP_HDR_SEQ_MASK   GENMASK(1, 0)
0036 #define MCTP_HDR_TAG_SHIFT  0
0037 #define MCTP_HDR_TAG_MASK   GENMASK(2, 0)
0038 
0039 #define MCTP_INITIAL_DEFAULT_NET    1
0040 
0041 static inline bool mctp_address_unicast(mctp_eid_t eid)
0042 {
0043     return eid >= 8 && eid < 255;
0044 }
0045 
0046 static inline bool mctp_address_broadcast(mctp_eid_t eid)
0047 {
0048     return eid == 255;
0049 }
0050 
0051 static inline bool mctp_address_null(mctp_eid_t eid)
0052 {
0053     return eid == 0;
0054 }
0055 
0056 static inline bool mctp_address_matches(mctp_eid_t match, mctp_eid_t eid)
0057 {
0058     return match == eid || match == MCTP_ADDR_ANY;
0059 }
0060 
0061 static inline struct mctp_hdr *mctp_hdr(struct sk_buff *skb)
0062 {
0063     return (struct mctp_hdr *)skb_network_header(skb);
0064 }
0065 
0066 /* socket implementation */
0067 struct mctp_sock {
0068     struct sock sk;
0069 
0070     /* bind() params */
0071     unsigned int    bind_net;
0072     mctp_eid_t  bind_addr;
0073     __u8        bind_type;
0074 
0075     /* sendmsg()/recvmsg() uses struct sockaddr_mctp_ext */
0076     bool        addr_ext;
0077 
0078     /* list of mctp_sk_key, for incoming tag lookup. updates protected
0079      * by sk->net->keys_lock
0080      */
0081     struct hlist_head keys;
0082 
0083     /* mechanism for expiring allocated keys; will release an allocated
0084      * tag, and any netdev state for a request/response pairing
0085      */
0086     struct timer_list key_expiry;
0087 };
0088 
0089 /* Key for matching incoming packets to sockets or reassembly contexts.
0090  * Packets are matched on (src,dest,tag).
0091  *
0092  * Lifetime / locking requirements:
0093  *
0094  *  - individual key data (ie, the struct itself) is protected by key->lock;
0095  *    changes must be made with that lock held.
0096  *
0097  *  - the lookup fields: peer_addr, local_addr and tag are set before the
0098  *    key is added to lookup lists, and never updated.
0099  *
0100  *  - A ref to the key must be held (throuh key->refs) if a pointer to the
0101  *    key is to be accessed after key->lock is released.
0102  *
0103  *  - a mctp_sk_key contains a reference to a struct sock; this is valid
0104  *    for the life of the key. On sock destruction (through unhash), the key is
0105  *    removed from lists (see below), and marked invalid.
0106  *
0107  * - these mctp_sk_keys appear on two lists:
0108  *     1) the struct mctp_sock->keys list
0109  *     2) the struct netns_mctp->keys list
0110  *
0111  *   presences on these lists requires a (single) refcount to be held; both
0112  *   lists are updated as a single operation.
0113  *
0114  *   Updates and lookups in either list are performed under the
0115  *   netns_mctp->keys lock. Lookup functions will need to lock the key and
0116  *   take a reference before unlocking the keys_lock. Consequently, the list's
0117  *   keys_lock *cannot* be acquired with the individual key->lock held.
0118  *
0119  * - a key may have a sk_buff attached as part of an in-progress message
0120  *   reassembly (->reasm_head). The reasm data is protected by the individual
0121  *   key->lock.
0122  *
0123  * - there are two destruction paths for a mctp_sk_key:
0124  *
0125  *    - through socket unhash (see mctp_sk_unhash). This performs the list
0126  *      removal under keys_lock.
0127  *
0128  *    - where a key is established to receive a reply message: after receiving
0129  *      the (complete) reply, or during reassembly errors. Here, we clean up
0130  *      the reassembly context (marking reasm_dead, to prevent another from
0131  *      starting), and remove the socket from the netns & socket lists.
0132  *
0133  *    - through an expiry timeout, on a per-socket timer
0134  */
0135 struct mctp_sk_key {
0136     mctp_eid_t  peer_addr;
0137     mctp_eid_t  local_addr; /* MCTP_ADDR_ANY for local owned tags */
0138     __u8        tag; /* incoming tag match; invert TO for local */
0139 
0140     /* we hold a ref to sk when set */
0141     struct sock *sk;
0142 
0143     /* routing lookup list */
0144     struct hlist_node hlist;
0145 
0146     /* per-socket list */
0147     struct hlist_node sklist;
0148 
0149     /* lock protects against concurrent updates to the reassembly and
0150      * expiry data below.
0151      */
0152     spinlock_t  lock;
0153 
0154     /* Keys are referenced during the output path, which may sleep */
0155     refcount_t  refs;
0156 
0157     /* incoming fragment reassembly context */
0158     struct sk_buff  *reasm_head;
0159     struct sk_buff  **reasm_tailp;
0160     bool        reasm_dead;
0161     u8      last_seq;
0162 
0163     /* key validity */
0164     bool        valid;
0165 
0166     /* expiry timeout; valid (above) cleared on expiry */
0167     unsigned long   expiry;
0168 
0169     /* free to use for device flow state tracking. Initialised to
0170      * zero on initial key creation
0171      */
0172     unsigned long   dev_flow_state;
0173     struct mctp_dev *dev;
0174 
0175     /* a tag allocated with SIOCMCTPALLOCTAG ioctl will not expire
0176      * automatically on timeout or response, instead SIOCMCTPDROPTAG
0177      * is used.
0178      */
0179     bool        manual_alloc;
0180 };
0181 
0182 struct mctp_skb_cb {
0183     unsigned int    magic;
0184     unsigned int    net;
0185     int     ifindex; /* extended/direct addressing if set */
0186     mctp_eid_t  src;
0187     unsigned char   halen;
0188     unsigned char   haddr[MAX_ADDR_LEN];
0189 };
0190 
0191 /* skb control-block accessors with a little extra debugging for initial
0192  * development.
0193  *
0194  * TODO: remove checks & mctp_skb_cb->magic; replace callers of __mctp_cb
0195  * with mctp_cb().
0196  *
0197  * __mctp_cb() is only for the initial ingress code; we should see ->magic set
0198  * at all times after this.
0199  */
0200 static inline struct mctp_skb_cb *__mctp_cb(struct sk_buff *skb)
0201 {
0202     struct mctp_skb_cb *cb = (void *)skb->cb;
0203 
0204     cb->magic = 0x4d435450;
0205     return cb;
0206 }
0207 
0208 static inline struct mctp_skb_cb *mctp_cb(struct sk_buff *skb)
0209 {
0210     struct mctp_skb_cb *cb = (void *)skb->cb;
0211 
0212     BUILD_BUG_ON(sizeof(struct mctp_skb_cb) > sizeof(skb->cb));
0213     WARN_ON(cb->magic != 0x4d435450);
0214     return (void *)(skb->cb);
0215 }
0216 
0217 /* If CONFIG_MCTP_FLOWS, we may add one of these as a SKB extension,
0218  * indicating the flow to the device driver.
0219  */
0220 struct mctp_flow {
0221     struct mctp_sk_key *key;
0222 };
0223 
0224 /* Route definition.
0225  *
0226  * These are held in the pernet->mctp.routes list, with RCU protection for
0227  * removed routes. We hold a reference to the netdev; routes need to be
0228  * dropped on NETDEV_UNREGISTER events.
0229  *
0230  * Updates to the route table are performed under rtnl; all reads under RCU,
0231  * so routes cannot be referenced over a RCU grace period. Specifically: A
0232  * caller cannot block between mctp_route_lookup and mctp_route_release()
0233  */
0234 struct mctp_route {
0235     mctp_eid_t      min, max;
0236 
0237     struct mctp_dev     *dev;
0238     unsigned int        mtu;
0239     unsigned char       type;
0240     int         (*output)(struct mctp_route *route,
0241                       struct sk_buff *skb);
0242 
0243     struct list_head    list;
0244     refcount_t      refs;
0245     struct rcu_head     rcu;
0246 };
0247 
0248 /* route interfaces */
0249 struct mctp_route *mctp_route_lookup(struct net *net, unsigned int dnet,
0250                      mctp_eid_t daddr);
0251 
0252 int mctp_local_output(struct sock *sk, struct mctp_route *rt,
0253               struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag);
0254 
0255 void mctp_key_unref(struct mctp_sk_key *key);
0256 struct mctp_sk_key *mctp_alloc_local_tag(struct mctp_sock *msk,
0257                      mctp_eid_t daddr, mctp_eid_t saddr,
0258                      bool manual, u8 *tagp);
0259 
0260 /* routing <--> device interface */
0261 unsigned int mctp_default_net(struct net *net);
0262 int mctp_default_net_set(struct net *net, unsigned int index);
0263 int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr);
0264 int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr);
0265 void mctp_route_remove_dev(struct mctp_dev *mdev);
0266 
0267 /* neighbour definitions */
0268 enum mctp_neigh_source {
0269     MCTP_NEIGH_STATIC,
0270     MCTP_NEIGH_DISCOVER,
0271 };
0272 
0273 struct mctp_neigh {
0274     struct mctp_dev     *dev;
0275     mctp_eid_t      eid;
0276     enum mctp_neigh_source  source;
0277 
0278     unsigned char       ha[MAX_ADDR_LEN];
0279 
0280     struct list_head    list;
0281     struct rcu_head     rcu;
0282 };
0283 
0284 int mctp_neigh_init(void);
0285 void mctp_neigh_exit(void);
0286 
0287 // ret_hwaddr may be NULL, otherwise must have space for MAX_ADDR_LEN
0288 int mctp_neigh_lookup(struct mctp_dev *dev, mctp_eid_t eid,
0289               void *ret_hwaddr);
0290 void mctp_neigh_remove_dev(struct mctp_dev *mdev);
0291 
0292 int mctp_routes_init(void);
0293 void mctp_routes_exit(void);
0294 
0295 void mctp_device_init(void);
0296 void mctp_device_exit(void);
0297 
0298 #endif /* __NET_MCTP_H */