]>
Commit | Line | Data |
---|---|---|
ef2486f5 | 1 | /* |
2 | * Copyright 2007-2012 Siemens AG | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 | |
6 | * as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
ef2486f5 | 13 | * Written by: |
14 | * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> | |
15 | * Sergey Lapin <slapin@ossfans.org> | |
16 | * Maxim Gorbachyov <maxim.gorbachev@siemens.com> | |
17 | * Alexander Smirnov <alex.bluesman.smirnov@gmail.com> | |
18 | */ | |
19 | ||
20 | #include <linux/if_arp.h> | |
21 | ||
22 | #include <net/mac802154.h> | |
b70ab2e8 | 23 | #include <net/ieee802154_netdev.h> |
ef2486f5 | 24 | #include <net/wpan-phy.h> |
25 | ||
0f1556bc | 26 | #include "ieee802154_i.h" |
ef2486f5 | 27 | |
66b69d4d | 28 | struct phy_chan_notify_work { |
29 | struct work_struct work; | |
30 | struct net_device *dev; | |
31 | }; | |
32 | ||
ef2486f5 | 33 | struct hw_addr_filt_notify_work { |
34 | struct work_struct work; | |
35 | struct net_device *dev; | |
36 | unsigned long changed; | |
37 | }; | |
38 | ||
42884042 | 39 | static struct mac802154_priv *mac802154_slave_get_priv(struct net_device *dev) |
ef2486f5 | 40 | { |
41 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
42 | ||
43 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
44 | ||
45 | return priv->hw; | |
46 | } | |
47 | ||
48 | static void hw_addr_notify(struct work_struct *work) | |
49 | { | |
50 | struct hw_addr_filt_notify_work *nw = container_of(work, | |
51 | struct hw_addr_filt_notify_work, work); | |
52 | struct mac802154_priv *hw = mac802154_slave_get_priv(nw->dev); | |
53 | int res; | |
54 | ||
55 | res = hw->ops->set_hw_addr_filt(&hw->hw, | |
56 | &hw->hw.hw_filt, | |
57 | nw->changed); | |
58 | if (res) | |
59 | pr_debug("failed changed mask %lx\n", nw->changed); | |
60 | ||
61 | kfree(nw); | |
ef2486f5 | 62 | } |
63 | ||
64 | static void set_hw_addr_filt(struct net_device *dev, unsigned long changed) | |
65 | { | |
66 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
67 | struct hw_addr_filt_notify_work *work; | |
68 | ||
69 | work = kzalloc(sizeof(*work), GFP_ATOMIC); | |
70 | if (!work) | |
71 | return; | |
72 | ||
73 | INIT_WORK(&work->work, hw_addr_notify); | |
74 | work->dev = dev; | |
75 | work->changed = changed; | |
76 | queue_work(priv->hw->dev_workqueue, &work->work); | |
ef2486f5 | 77 | } |
78 | ||
b70ab2e8 | 79 | void mac802154_dev_set_short_addr(struct net_device *dev, __le16 val) |
48e44d50 | 80 | { |
81 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
82 | ||
83 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
84 | ||
85 | spin_lock_bh(&priv->mib_lock); | |
86 | priv->short_addr = val; | |
87 | spin_unlock_bh(&priv->mib_lock); | |
88 | ||
89 | if ((priv->hw->ops->set_hw_addr_filt) && | |
90 | (priv->hw->hw.hw_filt.short_addr != priv->short_addr)) { | |
91 | priv->hw->hw.hw_filt.short_addr = priv->short_addr; | |
57205c14 | 92 | set_hw_addr_filt(dev, IEEE802154_AFILT_SADDR_CHANGED); |
48e44d50 | 93 | } |
94 | } | |
95 | ||
b70ab2e8 | 96 | __le16 mac802154_dev_get_short_addr(const struct net_device *dev) |
e885a47a | 97 | { |
98 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
b70ab2e8 | 99 | __le16 ret; |
e885a47a | 100 | |
101 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
102 | ||
103 | spin_lock_bh(&priv->mib_lock); | |
104 | ret = priv->short_addr; | |
105 | spin_unlock_bh(&priv->mib_lock); | |
106 | ||
107 | return ret; | |
108 | } | |
109 | ||
ef2486f5 | 110 | void mac802154_dev_set_ieee_addr(struct net_device *dev) |
111 | { | |
112 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
113 | struct mac802154_priv *mac = priv->hw; | |
114 | ||
e6278d92 | 115 | priv->extended_addr = ieee802154_devaddr_from_raw(dev->dev_addr); |
b70ab2e8 | 116 | |
e6278d92 PB |
117 | if (mac->ops->set_hw_addr_filt && |
118 | mac->hw.hw_filt.ieee_addr != priv->extended_addr) { | |
119 | mac->hw.hw_filt.ieee_addr = priv->extended_addr; | |
57205c14 | 120 | set_hw_addr_filt(dev, IEEE802154_AFILT_IEEEADDR_CHANGED); |
ef2486f5 | 121 | } |
122 | } | |
dcbe4f93 | 123 | |
b70ab2e8 | 124 | __le16 mac802154_dev_get_pan_id(const struct net_device *dev) |
dcbe4f93 | 125 | { |
126 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
b70ab2e8 | 127 | __le16 ret; |
dcbe4f93 | 128 | |
129 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
130 | ||
131 | spin_lock_bh(&priv->mib_lock); | |
132 | ret = priv->pan_id; | |
133 | spin_unlock_bh(&priv->mib_lock); | |
134 | ||
135 | return ret; | |
136 | } | |
137 | ||
b70ab2e8 | 138 | void mac802154_dev_set_pan_id(struct net_device *dev, __le16 val) |
dcbe4f93 | 139 | { |
140 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
141 | ||
142 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
143 | ||
144 | spin_lock_bh(&priv->mib_lock); | |
145 | priv->pan_id = val; | |
146 | spin_unlock_bh(&priv->mib_lock); | |
147 | ||
148 | if ((priv->hw->ops->set_hw_addr_filt) && | |
149 | (priv->hw->hw.hw_filt.pan_id != priv->pan_id)) { | |
150 | priv->hw->hw.hw_filt.pan_id = priv->pan_id; | |
57205c14 | 151 | set_hw_addr_filt(dev, IEEE802154_AFILT_PANID_CHANGED); |
dcbe4f93 | 152 | } |
153 | } | |
66b69d4d | 154 | |
0483546a TC |
155 | u8 mac802154_dev_get_dsn(const struct net_device *dev) |
156 | { | |
157 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
158 | ||
159 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
160 | ||
161 | return priv->dsn++; | |
162 | } | |
163 | ||
66b69d4d | 164 | static void phy_chan_notify(struct work_struct *work) |
165 | { | |
166 | struct phy_chan_notify_work *nw = container_of(work, | |
167 | struct phy_chan_notify_work, work); | |
168 | struct mac802154_priv *hw = mac802154_slave_get_priv(nw->dev); | |
169 | struct mac802154_sub_if_data *priv = netdev_priv(nw->dev); | |
170 | int res; | |
171 | ||
9f7f78b4 | 172 | mutex_lock(&priv->hw->phy->pib_lock); |
66b69d4d | 173 | res = hw->ops->set_channel(&hw->hw, priv->page, priv->chan); |
4710d806 | 174 | if (res) { |
66b69d4d | 175 | pr_debug("set_channel failed\n"); |
4710d806 | 176 | } else { |
9f7f78b4 AO |
177 | priv->hw->phy->current_channel = priv->chan; |
178 | priv->hw->phy->current_page = priv->page; | |
179 | } | |
180 | mutex_unlock(&priv->hw->phy->pib_lock); | |
66b69d4d | 181 | |
182 | kfree(nw); | |
183 | } | |
184 | ||
185 | void mac802154_dev_set_page_channel(struct net_device *dev, u8 page, u8 chan) | |
186 | { | |
187 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
188 | struct phy_chan_notify_work *work; | |
189 | ||
190 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
191 | ||
192 | spin_lock_bh(&priv->mib_lock); | |
193 | priv->page = page; | |
194 | priv->chan = chan; | |
195 | spin_unlock_bh(&priv->mib_lock); | |
196 | ||
9f7f78b4 | 197 | mutex_lock(&priv->hw->phy->pib_lock); |
66b69d4d | 198 | if (priv->hw->phy->current_channel != priv->chan || |
199 | priv->hw->phy->current_page != priv->page) { | |
9f7f78b4 AO |
200 | mutex_unlock(&priv->hw->phy->pib_lock); |
201 | ||
66b69d4d | 202 | work = kzalloc(sizeof(*work), GFP_ATOMIC); |
203 | if (!work) | |
204 | return; | |
205 | ||
206 | INIT_WORK(&work->work, phy_chan_notify); | |
207 | work->dev = dev; | |
208 | queue_work(priv->hw->dev_workqueue, &work->work); | |
4710d806 | 209 | } else { |
9f7f78b4 | 210 | mutex_unlock(&priv->hw->phy->pib_lock); |
4710d806 | 211 | } |
66b69d4d | 212 | } |
29e02374 PB |
213 | |
214 | ||
215 | int mac802154_get_params(struct net_device *dev, | |
216 | struct ieee802154_llsec_params *params) | |
217 | { | |
218 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
219 | int res; | |
220 | ||
221 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
222 | ||
223 | mutex_lock(&priv->sec_mtx); | |
224 | res = mac802154_llsec_get_params(&priv->sec, params); | |
225 | mutex_unlock(&priv->sec_mtx); | |
226 | ||
227 | return res; | |
228 | } | |
229 | ||
230 | int mac802154_set_params(struct net_device *dev, | |
231 | const struct ieee802154_llsec_params *params, | |
232 | int changed) | |
233 | { | |
234 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
235 | int res; | |
236 | ||
237 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
238 | ||
239 | mutex_lock(&priv->sec_mtx); | |
240 | res = mac802154_llsec_set_params(&priv->sec, params, changed); | |
241 | mutex_unlock(&priv->sec_mtx); | |
242 | ||
243 | return res; | |
244 | } | |
245 | ||
246 | ||
247 | int mac802154_add_key(struct net_device *dev, | |
248 | const struct ieee802154_llsec_key_id *id, | |
249 | const struct ieee802154_llsec_key *key) | |
250 | { | |
251 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
252 | int res; | |
253 | ||
254 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
255 | ||
256 | mutex_lock(&priv->sec_mtx); | |
257 | res = mac802154_llsec_key_add(&priv->sec, id, key); | |
258 | mutex_unlock(&priv->sec_mtx); | |
259 | ||
260 | return res; | |
261 | } | |
262 | ||
263 | int mac802154_del_key(struct net_device *dev, | |
264 | const struct ieee802154_llsec_key_id *id) | |
265 | { | |
266 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
267 | int res; | |
268 | ||
269 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
270 | ||
271 | mutex_lock(&priv->sec_mtx); | |
272 | res = mac802154_llsec_key_del(&priv->sec, id); | |
273 | mutex_unlock(&priv->sec_mtx); | |
274 | ||
275 | return res; | |
276 | } | |
277 | ||
278 | ||
279 | int mac802154_add_dev(struct net_device *dev, | |
280 | const struct ieee802154_llsec_device *llsec_dev) | |
281 | { | |
282 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
283 | int res; | |
284 | ||
285 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
286 | ||
287 | mutex_lock(&priv->sec_mtx); | |
288 | res = mac802154_llsec_dev_add(&priv->sec, llsec_dev); | |
289 | mutex_unlock(&priv->sec_mtx); | |
290 | ||
291 | return res; | |
292 | } | |
293 | ||
294 | int mac802154_del_dev(struct net_device *dev, __le64 dev_addr) | |
295 | { | |
296 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
297 | int res; | |
298 | ||
299 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
300 | ||
301 | mutex_lock(&priv->sec_mtx); | |
302 | res = mac802154_llsec_dev_del(&priv->sec, dev_addr); | |
303 | mutex_unlock(&priv->sec_mtx); | |
304 | ||
305 | return res; | |
306 | } | |
307 | ||
308 | ||
309 | int mac802154_add_devkey(struct net_device *dev, | |
310 | __le64 device_addr, | |
311 | const struct ieee802154_llsec_device_key *key) | |
312 | { | |
313 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
314 | int res; | |
315 | ||
316 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
317 | ||
318 | mutex_lock(&priv->sec_mtx); | |
319 | res = mac802154_llsec_devkey_add(&priv->sec, device_addr, key); | |
320 | mutex_unlock(&priv->sec_mtx); | |
321 | ||
322 | return res; | |
323 | } | |
324 | ||
325 | int mac802154_del_devkey(struct net_device *dev, | |
326 | __le64 device_addr, | |
327 | const struct ieee802154_llsec_device_key *key) | |
328 | { | |
329 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
330 | int res; | |
331 | ||
332 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
333 | ||
334 | mutex_lock(&priv->sec_mtx); | |
335 | res = mac802154_llsec_devkey_del(&priv->sec, device_addr, key); | |
336 | mutex_unlock(&priv->sec_mtx); | |
337 | ||
338 | return res; | |
339 | } | |
340 | ||
341 | ||
342 | int mac802154_add_seclevel(struct net_device *dev, | |
343 | const struct ieee802154_llsec_seclevel *sl) | |
344 | { | |
345 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
346 | int res; | |
347 | ||
348 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
349 | ||
350 | mutex_lock(&priv->sec_mtx); | |
351 | res = mac802154_llsec_seclevel_add(&priv->sec, sl); | |
352 | mutex_unlock(&priv->sec_mtx); | |
353 | ||
354 | return res; | |
355 | } | |
356 | ||
357 | int mac802154_del_seclevel(struct net_device *dev, | |
358 | const struct ieee802154_llsec_seclevel *sl) | |
359 | { | |
360 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
361 | int res; | |
362 | ||
363 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
364 | ||
365 | mutex_lock(&priv->sec_mtx); | |
366 | res = mac802154_llsec_seclevel_del(&priv->sec, sl); | |
367 | mutex_unlock(&priv->sec_mtx); | |
368 | ||
369 | return res; | |
370 | } | |
371 | ||
372 | ||
373 | void mac802154_lock_table(struct net_device *dev) | |
374 | { | |
375 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
376 | ||
377 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
378 | ||
379 | mutex_lock(&priv->sec_mtx); | |
380 | } | |
381 | ||
382 | void mac802154_get_table(struct net_device *dev, | |
383 | struct ieee802154_llsec_table **t) | |
384 | { | |
385 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
386 | ||
387 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
388 | ||
389 | *t = &priv->sec.table; | |
390 | } | |
391 | ||
392 | void mac802154_unlock_table(struct net_device *dev) | |
393 | { | |
394 | struct mac802154_sub_if_data *priv = netdev_priv(dev); | |
395 | ||
396 | BUG_ON(dev->type != ARPHRD_IEEE802154); | |
397 | ||
398 | mutex_unlock(&priv->sec_mtx); | |
399 | } |