1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#ifndef __NET_CFG802154_H
18#define __NET_CFG802154_H
19
20#include <linux/ieee802154.h>
21#include <linux/netdevice.h>
22#include <linux/mutex.h>
23#include <linux/bug.h>
24
25#include <net/nl802154.h>
26
27struct wpan_phy;
28struct wpan_phy_cca;
29
30#ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
31struct ieee802154_llsec_device_key;
32struct ieee802154_llsec_seclevel;
33struct ieee802154_llsec_params;
34struct ieee802154_llsec_device;
35struct ieee802154_llsec_table;
36struct ieee802154_llsec_key_id;
37struct ieee802154_llsec_key;
38#endif
39
40struct cfg802154_ops {
41 struct net_device * (*add_virtual_intf_deprecated)(struct wpan_phy *wpan_phy,
42 const char *name,
43 int type);
44 void (*del_virtual_intf_deprecated)(struct wpan_phy *wpan_phy,
45 struct net_device *dev);
46 int (*suspend)(struct wpan_phy *wpan_phy);
47 int (*resume)(struct wpan_phy *wpan_phy);
48 int (*add_virtual_intf)(struct wpan_phy *wpan_phy,
49 const char *name,
50 enum nl802154_iftype type,
51 __le64 extended_addr);
52 int (*del_virtual_intf)(struct wpan_phy *wpan_phy,
53 struct wpan_dev *wpan_dev);
54 int (*set_channel)(struct wpan_phy *wpan_phy, u8 page, u8 channel);
55 int (*set_cca_mode)(struct wpan_phy *wpan_phy,
56 const struct wpan_phy_cca *cca);
57 int (*set_cca_ed_level)(struct wpan_phy *wpan_phy, s32 ed_level);
58 int (*set_tx_power)(struct wpan_phy *wpan_phy, s32 power);
59 int (*set_pan_id)(struct wpan_phy *wpan_phy,
60 struct wpan_dev *wpan_dev, __le16 pan_id);
61 int (*set_short_addr)(struct wpan_phy *wpan_phy,
62 struct wpan_dev *wpan_dev, __le16 short_addr);
63 int (*set_backoff_exponent)(struct wpan_phy *wpan_phy,
64 struct wpan_dev *wpan_dev, u8 min_be,
65 u8 max_be);
66 int (*set_max_csma_backoffs)(struct wpan_phy *wpan_phy,
67 struct wpan_dev *wpan_dev,
68 u8 max_csma_backoffs);
69 int (*set_max_frame_retries)(struct wpan_phy *wpan_phy,
70 struct wpan_dev *wpan_dev,
71 s8 max_frame_retries);
72 int (*set_lbt_mode)(struct wpan_phy *wpan_phy,
73 struct wpan_dev *wpan_dev, bool mode);
74 int (*set_ackreq_default)(struct wpan_phy *wpan_phy,
75 struct wpan_dev *wpan_dev, bool ackreq);
76#ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
77 void (*get_llsec_table)(struct wpan_phy *wpan_phy,
78 struct wpan_dev *wpan_dev,
79 struct ieee802154_llsec_table **table);
80 void (*lock_llsec_table)(struct wpan_phy *wpan_phy,
81 struct wpan_dev *wpan_dev);
82 void (*unlock_llsec_table)(struct wpan_phy *wpan_phy,
83 struct wpan_dev *wpan_dev);
84
85
86
87 int (*get_llsec_params)(struct wpan_phy *wpan_phy,
88 struct wpan_dev *wpan_dev,
89 struct ieee802154_llsec_params *params);
90 int (*set_llsec_params)(struct wpan_phy *wpan_phy,
91 struct wpan_dev *wpan_dev,
92 const struct ieee802154_llsec_params *params,
93 int changed);
94 int (*add_llsec_key)(struct wpan_phy *wpan_phy,
95 struct wpan_dev *wpan_dev,
96 const struct ieee802154_llsec_key_id *id,
97 const struct ieee802154_llsec_key *key);
98 int (*del_llsec_key)(struct wpan_phy *wpan_phy,
99 struct wpan_dev *wpan_dev,
100 const struct ieee802154_llsec_key_id *id);
101 int (*add_seclevel)(struct wpan_phy *wpan_phy,
102 struct wpan_dev *wpan_dev,
103 const struct ieee802154_llsec_seclevel *sl);
104 int (*del_seclevel)(struct wpan_phy *wpan_phy,
105 struct wpan_dev *wpan_dev,
106 const struct ieee802154_llsec_seclevel *sl);
107 int (*add_device)(struct wpan_phy *wpan_phy,
108 struct wpan_dev *wpan_dev,
109 const struct ieee802154_llsec_device *dev);
110 int (*del_device)(struct wpan_phy *wpan_phy,
111 struct wpan_dev *wpan_dev, __le64 extended_addr);
112 int (*add_devkey)(struct wpan_phy *wpan_phy,
113 struct wpan_dev *wpan_dev,
114 __le64 extended_addr,
115 const struct ieee802154_llsec_device_key *key);
116 int (*del_devkey)(struct wpan_phy *wpan_phy,
117 struct wpan_dev *wpan_dev,
118 __le64 extended_addr,
119 const struct ieee802154_llsec_device_key *key);
120#endif
121};
122
123static inline bool
124wpan_phy_supported_bool(bool b, enum nl802154_supported_bool_states st)
125{
126 switch (st) {
127 case NL802154_SUPPORTED_BOOL_TRUE:
128 return b;
129 case NL802154_SUPPORTED_BOOL_FALSE:
130 return !b;
131 case NL802154_SUPPORTED_BOOL_BOTH:
132 return true;
133 default:
134 WARN_ON(1);
135 }
136
137 return false;
138}
139
140struct wpan_phy_supported {
141 u32 channels[IEEE802154_MAX_PAGE + 1],
142 cca_modes, cca_opts, iftypes;
143 enum nl802154_supported_bool_states lbt;
144 u8 min_minbe, max_minbe, min_maxbe, max_maxbe,
145 min_csma_backoffs, max_csma_backoffs;
146 s8 min_frame_retries, max_frame_retries;
147 size_t tx_powers_size, cca_ed_levels_size;
148 const s32 *tx_powers, *cca_ed_levels;
149};
150
151struct wpan_phy_cca {
152 enum nl802154_cca_modes mode;
153 enum nl802154_cca_opts opt;
154};
155
156static inline bool
157wpan_phy_cca_cmp(const struct wpan_phy_cca *a, const struct wpan_phy_cca *b)
158{
159 if (a->mode != b->mode)
160 return false;
161
162 if (a->mode == NL802154_CCA_ENERGY_CARRIER)
163 return a->opt == b->opt;
164
165 return true;
166}
167
168
169
170
171
172
173
174
175
176enum wpan_phy_flags {
177 WPAN_PHY_FLAG_TXPOWER = BIT(1),
178 WPAN_PHY_FLAG_CCA_ED_LEVEL = BIT(2),
179 WPAN_PHY_FLAG_CCA_MODE = BIT(3),
180};
181
182struct wpan_phy {
183
184
185
186
187
188
189 const void *privid;
190
191 u32 flags;
192
193
194
195
196
197
198 u8 current_channel;
199 u8 current_page;
200 struct wpan_phy_supported supported;
201
202 s32 transmit_power;
203 struct wpan_phy_cca cca;
204
205 __le64 perm_extended_addr;
206
207
208 s32 cca_ed_level;
209
210
211
212
213 u8 symbol_duration;
214
215 u16 lifs_period;
216 u16 sifs_period;
217
218 struct device dev;
219
220
221 possible_net_t _net;
222
223 char priv[0] __aligned(NETDEV_ALIGN);
224};
225
226static inline struct net *wpan_phy_net(struct wpan_phy *wpan_phy)
227{
228 return read_pnet(&wpan_phy->_net);
229}
230
231static inline void wpan_phy_net_set(struct wpan_phy *wpan_phy, struct net *net)
232{
233 write_pnet(&wpan_phy->_net, net);
234}
235
236struct ieee802154_addr {
237 u8 mode;
238 __le16 pan_id;
239 union {
240 __le16 short_addr;
241 __le64 extended_addr;
242 };
243};
244
245struct ieee802154_llsec_key_id {
246 u8 mode;
247 u8 id;
248 union {
249 struct ieee802154_addr device_addr;
250 __le32 short_source;
251 __le64 extended_source;
252 };
253};
254
255#define IEEE802154_LLSEC_KEY_SIZE 16
256
257struct ieee802154_llsec_key {
258 u8 frame_types;
259 u32 cmd_frame_ids;
260
261 u8 key[IEEE802154_LLSEC_KEY_SIZE];
262};
263
264struct ieee802154_llsec_key_entry {
265 struct list_head list;
266
267 struct ieee802154_llsec_key_id id;
268 struct ieee802154_llsec_key *key;
269};
270
271struct ieee802154_llsec_params {
272 bool enabled;
273
274 __be32 frame_counter;
275 u8 out_level;
276 struct ieee802154_llsec_key_id out_key;
277
278 __le64 default_key_source;
279
280 __le16 pan_id;
281 __le64 hwaddr;
282 __le64 coord_hwaddr;
283 __le16 coord_shortaddr;
284};
285
286struct ieee802154_llsec_table {
287 struct list_head keys;
288 struct list_head devices;
289 struct list_head security_levels;
290};
291
292struct ieee802154_llsec_seclevel {
293 struct list_head list;
294
295 u8 frame_type;
296 u8 cmd_frame_id;
297 bool device_override;
298 u32 sec_levels;
299};
300
301struct ieee802154_llsec_device {
302 struct list_head list;
303
304 __le16 pan_id;
305 __le16 short_addr;
306 __le64 hwaddr;
307 u32 frame_counter;
308 bool seclevel_exempt;
309
310 u8 key_mode;
311 struct list_head keys;
312};
313
314struct ieee802154_llsec_device_key {
315 struct list_head list;
316
317 struct ieee802154_llsec_key_id key_id;
318 u32 frame_counter;
319};
320
321struct wpan_dev_header_ops {
322
323
324
325
326 int (*create)(struct sk_buff *skb, struct net_device *dev,
327 const struct ieee802154_addr *daddr,
328 const struct ieee802154_addr *saddr,
329 unsigned int len);
330};
331
332struct wpan_dev {
333 struct wpan_phy *wpan_phy;
334 int iftype;
335
336
337 struct list_head list;
338 struct net_device *netdev;
339
340 const struct wpan_dev_header_ops *header_ops;
341
342
343 struct net_device *lowpan_dev;
344
345 u32 identifier;
346
347
348 __le16 pan_id;
349 __le16 short_addr;
350 __le64 extended_addr;
351
352
353 atomic_t bsn;
354
355 atomic_t dsn;
356
357 u8 min_be;
358 u8 max_be;
359 u8 csma_retries;
360 s8 frame_retries;
361
362 bool lbt;
363
364 bool promiscuous_mode;
365
366
367 bool ackreq;
368};
369
370#define to_phy(_dev) container_of(_dev, struct wpan_phy, dev)
371
372static inline int
373wpan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
374 const struct ieee802154_addr *daddr,
375 const struct ieee802154_addr *saddr,
376 unsigned int len)
377{
378 struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
379
380 return wpan_dev->header_ops->create(skb, dev, daddr, saddr, len);
381}
382
383struct wpan_phy *
384wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size);
385static inline void wpan_phy_set_dev(struct wpan_phy *phy, struct device *dev)
386{
387 phy->dev.parent = dev;
388}
389
390int wpan_phy_register(struct wpan_phy *phy);
391void wpan_phy_unregister(struct wpan_phy *phy);
392void wpan_phy_free(struct wpan_phy *phy);
393
394int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data), void *data);
395
396static inline void *wpan_phy_priv(struct wpan_phy *phy)
397{
398 BUG_ON(!phy);
399 return &phy->priv;
400}
401
402struct wpan_phy *wpan_phy_find(const char *str);
403
404static inline void wpan_phy_put(struct wpan_phy *phy)
405{
406 put_device(&phy->dev);
407}
408
409static inline const char *wpan_phy_name(struct wpan_phy *phy)
410{
411 return dev_name(&phy->dev);
412}
413
414#endif
415