]>
Commit | Line | Data |
---|---|---|
2c8e2e9a JK |
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> | |
583be982 | 13 | #include <linux/mctp.h> |
889b7da2 | 14 | #include <net/net_namespace.h> |
833ef3b9 | 15 | #include <net/sock.h> |
2c8e2e9a JK |
16 | |
17 | /* MCTP packet definitions */ | |
18 | struct mctp_hdr { | |
19 | u8 ver; | |
20 | u8 dest; | |
21 | u8 src; | |
22 | u8 flags_seq_tag; | |
23 | }; | |
24 | ||
25 | #define MCTP_VER_MIN 1 | |
26 | #define MCTP_VER_MAX 1 | |
27 | ||
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) | |
37 | ||
889b7da2 JK |
38 | #define MCTP_HEADER_MAXLEN 4 |
39 | ||
583be982 JK |
40 | static inline bool mctp_address_ok(mctp_eid_t eid) |
41 | { | |
42 | return eid >= 8 && eid < 255; | |
43 | } | |
44 | ||
45 | static inline struct mctp_hdr *mctp_hdr(struct sk_buff *skb) | |
46 | { | |
47 | return (struct mctp_hdr *)skb_network_header(skb); | |
48 | } | |
49 | ||
833ef3b9 JK |
50 | /* socket implementation */ |
51 | struct mctp_sock { | |
52 | struct sock sk; | |
53 | ||
54 | /* bind() params */ | |
55 | int bind_net; | |
56 | mctp_eid_t bind_addr; | |
57 | __u8 bind_type; | |
58 | ||
59 | /* list of mctp_sk_key, for incoming tag lookup. updates protected | |
60 | * by sk->net->keys_lock | |
61 | */ | |
62 | struct hlist_head keys; | |
63 | }; | |
64 | ||
65 | /* Key for matching incoming packets to sockets or reassembly contexts. | |
66 | * Packets are matched on (src,dest,tag). | |
67 | * | |
68 | * Lifetime requirements: | |
69 | * | |
70 | * - keys are free()ed via RCU | |
71 | * | |
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 | |
75 | * grace period. | |
76 | * | |
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. | |
79 | * | |
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 | |
83 | * | |
84 | * updates to either list are performed under the netns_mctp->keys | |
85 | * lock. | |
86 | * | |
87 | * - there is a single destruction path for a mctp_sk_key - through socket | |
88 | * unhash (see mctp_sk_unhash). This performs the list removal under | |
89 | * keys_lock. | |
90 | */ | |
91 | struct mctp_sk_key { | |
92 | mctp_eid_t peer_addr; | |
93 | mctp_eid_t local_addr; | |
94 | __u8 tag; /* incoming tag match; invert TO for local */ | |
95 | ||
96 | /* we hold a ref to sk when set */ | |
97 | struct sock *sk; | |
98 | ||
99 | /* routing lookup list */ | |
100 | struct hlist_node hlist; | |
101 | ||
102 | /* per-socket list */ | |
103 | struct hlist_node sklist; | |
104 | ||
105 | struct rcu_head rcu; | |
106 | }; | |
107 | ||
889b7da2 JK |
108 | struct mctp_skb_cb { |
109 | unsigned int magic; | |
110 | unsigned int net; | |
111 | mctp_eid_t src; | |
112 | }; | |
113 | ||
114 | /* skb control-block accessors with a little extra debugging for initial | |
115 | * development. | |
116 | * | |
117 | * TODO: remove checks & mctp_skb_cb->magic; replace callers of __mctp_cb | |
118 | * with mctp_cb(). | |
119 | * | |
120 | * __mctp_cb() is only for the initial ingress code; we should see ->magic set | |
121 | * at all times after this. | |
122 | */ | |
123 | static inline struct mctp_skb_cb *__mctp_cb(struct sk_buff *skb) | |
124 | { | |
125 | struct mctp_skb_cb *cb = (void *)skb->cb; | |
126 | ||
127 | cb->magic = 0x4d435450; | |
128 | return cb; | |
129 | } | |
130 | ||
131 | static inline struct mctp_skb_cb *mctp_cb(struct sk_buff *skb) | |
132 | { | |
133 | struct mctp_skb_cb *cb = (void *)skb->cb; | |
134 | ||
135 | WARN_ON(cb->magic != 0x4d435450); | |
136 | return (void *)(skb->cb); | |
137 | } | |
138 | ||
139 | /* Route definition. | |
140 | * | |
141 | * These are held in the pernet->mctp.routes list, with RCU protection for | |
142 | * removed routes. We hold a reference to the netdev; routes need to be | |
143 | * dropped on NETDEV_UNREGISTER events. | |
144 | * | |
145 | * Updates to the route table are performed under rtnl; all reads under RCU, | |
146 | * so routes cannot be referenced over a RCU grace period. Specifically: A | |
147 | * caller cannot block between mctp_route_lookup and passing the route to | |
148 | * mctp_do_route. | |
149 | */ | |
150 | struct mctp_route { | |
151 | mctp_eid_t min, max; | |
152 | ||
153 | struct mctp_dev *dev; | |
154 | unsigned int mtu; | |
155 | int (*output)(struct mctp_route *route, | |
156 | struct sk_buff *skb); | |
157 | ||
158 | struct list_head list; | |
159 | refcount_t refs; | |
160 | struct rcu_head rcu; | |
161 | }; | |
162 | ||
163 | /* route interfaces */ | |
164 | struct mctp_route *mctp_route_lookup(struct net *net, unsigned int dnet, | |
165 | mctp_eid_t daddr); | |
166 | ||
167 | int mctp_do_route(struct mctp_route *rt, struct sk_buff *skb); | |
168 | ||
169 | int mctp_local_output(struct sock *sk, struct mctp_route *rt, | |
170 | struct sk_buff *skb, mctp_eid_t daddr, u8 req_tag); | |
171 | ||
172 | /* routing <--> device interface */ | |
06d2f4c5 MJ |
173 | unsigned int mctp_default_net(struct net *net); |
174 | int mctp_default_net_set(struct net *net, unsigned int index); | |
889b7da2 JK |
175 | int mctp_route_add_local(struct mctp_dev *mdev, mctp_eid_t addr); |
176 | int mctp_route_remove_local(struct mctp_dev *mdev, mctp_eid_t addr); | |
177 | void mctp_route_remove_dev(struct mctp_dev *mdev); | |
178 | ||
4d8b9319 MJ |
179 | /* neighbour definitions */ |
180 | enum mctp_neigh_source { | |
181 | MCTP_NEIGH_STATIC, | |
182 | MCTP_NEIGH_DISCOVER, | |
183 | }; | |
184 | ||
185 | struct mctp_neigh { | |
186 | struct mctp_dev *dev; | |
187 | mctp_eid_t eid; | |
188 | enum mctp_neigh_source source; | |
189 | ||
190 | unsigned char ha[MAX_ADDR_LEN]; | |
191 | ||
192 | struct list_head list; | |
193 | struct rcu_head rcu; | |
194 | }; | |
195 | ||
196 | int mctp_neigh_init(void); | |
197 | void mctp_neigh_exit(void); | |
198 | ||
199 | // ret_hwaddr may be NULL, otherwise must have space for MAX_ADDR_LEN | |
200 | int mctp_neigh_lookup(struct mctp_dev *dev, mctp_eid_t eid, | |
201 | void *ret_hwaddr); | |
202 | void mctp_neigh_remove_dev(struct mctp_dev *mdev); | |
203 | ||
889b7da2 JK |
204 | int mctp_routes_init(void); |
205 | void mctp_routes_exit(void); | |
206 | ||
583be982 JK |
207 | void mctp_device_init(void); |
208 | void mctp_device_exit(void); | |
209 | ||
2c8e2e9a | 210 | #endif /* __NET_MCTP_H */ |