2 * Copyright 2007-2012 Siemens AG
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.
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.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
19 * Sergey Lapin <slapin@ossfans.org>
20 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
21 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
24 #include <linux/if_arp.h>
26 #include <net/mac802154.h>
27 #include <net/ieee802154_netdev.h>
28 #include <net/wpan-phy.h>
30 #include "mac802154.h"
32 struct phy_chan_notify_work
{
33 struct work_struct work
;
34 struct net_device
*dev
;
37 struct hw_addr_filt_notify_work
{
38 struct work_struct work
;
39 struct net_device
*dev
;
40 unsigned long changed
;
43 static struct mac802154_priv
*mac802154_slave_get_priv(struct net_device
*dev
)
45 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
47 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
52 static void hw_addr_notify(struct work_struct
*work
)
54 struct hw_addr_filt_notify_work
*nw
= container_of(work
,
55 struct hw_addr_filt_notify_work
, work
);
56 struct mac802154_priv
*hw
= mac802154_slave_get_priv(nw
->dev
);
59 res
= hw
->ops
->set_hw_addr_filt(&hw
->hw
,
63 pr_debug("failed changed mask %lx\n", nw
->changed
);
68 static void set_hw_addr_filt(struct net_device
*dev
, unsigned long changed
)
70 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
71 struct hw_addr_filt_notify_work
*work
;
73 work
= kzalloc(sizeof(*work
), GFP_ATOMIC
);
77 INIT_WORK(&work
->work
, hw_addr_notify
);
79 work
->changed
= changed
;
80 queue_work(priv
->hw
->dev_workqueue
, &work
->work
);
83 void mac802154_dev_set_short_addr(struct net_device
*dev
, __le16 val
)
85 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
87 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
89 spin_lock_bh(&priv
->mib_lock
);
90 priv
->short_addr
= val
;
91 spin_unlock_bh(&priv
->mib_lock
);
93 if ((priv
->hw
->ops
->set_hw_addr_filt
) &&
94 (priv
->hw
->hw
.hw_filt
.short_addr
!= priv
->short_addr
)) {
95 priv
->hw
->hw
.hw_filt
.short_addr
= priv
->short_addr
;
96 set_hw_addr_filt(dev
, IEEE802515_AFILT_SADDR_CHANGED
);
100 __le16
mac802154_dev_get_short_addr(const struct net_device
*dev
)
102 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
105 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
107 spin_lock_bh(&priv
->mib_lock
);
108 ret
= priv
->short_addr
;
109 spin_unlock_bh(&priv
->mib_lock
);
114 void mac802154_dev_set_ieee_addr(struct net_device
*dev
)
116 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
117 struct mac802154_priv
*mac
= priv
->hw
;
119 priv
->extended_addr
= ieee802154_devaddr_from_raw(dev
->dev_addr
);
121 if (mac
->ops
->set_hw_addr_filt
&&
122 mac
->hw
.hw_filt
.ieee_addr
!= priv
->extended_addr
) {
123 mac
->hw
.hw_filt
.ieee_addr
= priv
->extended_addr
;
124 set_hw_addr_filt(dev
, IEEE802515_AFILT_IEEEADDR_CHANGED
);
128 __le16
mac802154_dev_get_pan_id(const struct net_device
*dev
)
130 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
133 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
135 spin_lock_bh(&priv
->mib_lock
);
137 spin_unlock_bh(&priv
->mib_lock
);
142 void mac802154_dev_set_pan_id(struct net_device
*dev
, __le16 val
)
144 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
146 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
148 spin_lock_bh(&priv
->mib_lock
);
150 spin_unlock_bh(&priv
->mib_lock
);
152 if ((priv
->hw
->ops
->set_hw_addr_filt
) &&
153 (priv
->hw
->hw
.hw_filt
.pan_id
!= priv
->pan_id
)) {
154 priv
->hw
->hw
.hw_filt
.pan_id
= priv
->pan_id
;
155 set_hw_addr_filt(dev
, IEEE802515_AFILT_PANID_CHANGED
);
159 u8
mac802154_dev_get_dsn(const struct net_device
*dev
)
161 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
163 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
168 static void phy_chan_notify(struct work_struct
*work
)
170 struct phy_chan_notify_work
*nw
= container_of(work
,
171 struct phy_chan_notify_work
, work
);
172 struct mac802154_priv
*hw
= mac802154_slave_get_priv(nw
->dev
);
173 struct mac802154_sub_if_data
*priv
= netdev_priv(nw
->dev
);
176 mutex_lock(&priv
->hw
->phy
->pib_lock
);
177 res
= hw
->ops
->set_channel(&hw
->hw
, priv
->page
, priv
->chan
);
179 pr_debug("set_channel failed\n");
181 priv
->hw
->phy
->current_channel
= priv
->chan
;
182 priv
->hw
->phy
->current_page
= priv
->page
;
184 mutex_unlock(&priv
->hw
->phy
->pib_lock
);
189 void mac802154_dev_set_page_channel(struct net_device
*dev
, u8 page
, u8 chan
)
191 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
192 struct phy_chan_notify_work
*work
;
194 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
196 spin_lock_bh(&priv
->mib_lock
);
199 spin_unlock_bh(&priv
->mib_lock
);
201 mutex_lock(&priv
->hw
->phy
->pib_lock
);
202 if (priv
->hw
->phy
->current_channel
!= priv
->chan
||
203 priv
->hw
->phy
->current_page
!= priv
->page
) {
204 mutex_unlock(&priv
->hw
->phy
->pib_lock
);
206 work
= kzalloc(sizeof(*work
), GFP_ATOMIC
);
210 INIT_WORK(&work
->work
, phy_chan_notify
);
212 queue_work(priv
->hw
->dev_workqueue
, &work
->work
);
214 mutex_unlock(&priv
->hw
->phy
->pib_lock
);
218 int mac802154_get_params(struct net_device
*dev
,
219 struct ieee802154_llsec_params
*params
)
221 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
224 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
226 mutex_lock(&priv
->sec_mtx
);
227 res
= mac802154_llsec_get_params(&priv
->sec
, params
);
228 mutex_unlock(&priv
->sec_mtx
);
233 int mac802154_set_params(struct net_device
*dev
,
234 const struct ieee802154_llsec_params
*params
,
237 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
240 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
242 mutex_lock(&priv
->sec_mtx
);
243 res
= mac802154_llsec_set_params(&priv
->sec
, params
, changed
);
244 mutex_unlock(&priv
->sec_mtx
);
250 int mac802154_add_key(struct net_device
*dev
,
251 const struct ieee802154_llsec_key_id
*id
,
252 const struct ieee802154_llsec_key
*key
)
254 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
257 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
259 mutex_lock(&priv
->sec_mtx
);
260 res
= mac802154_llsec_key_add(&priv
->sec
, id
, key
);
261 mutex_unlock(&priv
->sec_mtx
);
266 int mac802154_del_key(struct net_device
*dev
,
267 const struct ieee802154_llsec_key_id
*id
)
269 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
272 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
274 mutex_lock(&priv
->sec_mtx
);
275 res
= mac802154_llsec_key_del(&priv
->sec
, id
);
276 mutex_unlock(&priv
->sec_mtx
);
282 int mac802154_add_dev(struct net_device
*dev
,
283 const struct ieee802154_llsec_device
*llsec_dev
)
285 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
288 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
290 mutex_lock(&priv
->sec_mtx
);
291 res
= mac802154_llsec_dev_add(&priv
->sec
, llsec_dev
);
292 mutex_unlock(&priv
->sec_mtx
);
297 int mac802154_del_dev(struct net_device
*dev
, __le64 dev_addr
)
299 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
302 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
304 mutex_lock(&priv
->sec_mtx
);
305 res
= mac802154_llsec_dev_del(&priv
->sec
, dev_addr
);
306 mutex_unlock(&priv
->sec_mtx
);
312 int mac802154_add_devkey(struct net_device
*dev
,
314 const struct ieee802154_llsec_device_key
*key
)
316 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
319 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
321 mutex_lock(&priv
->sec_mtx
);
322 res
= mac802154_llsec_devkey_add(&priv
->sec
, device_addr
, key
);
323 mutex_unlock(&priv
->sec_mtx
);
328 int mac802154_del_devkey(struct net_device
*dev
,
330 const struct ieee802154_llsec_device_key
*key
)
332 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
335 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
337 mutex_lock(&priv
->sec_mtx
);
338 res
= mac802154_llsec_devkey_del(&priv
->sec
, device_addr
, key
);
339 mutex_unlock(&priv
->sec_mtx
);
345 int mac802154_add_seclevel(struct net_device
*dev
,
346 const struct ieee802154_llsec_seclevel
*sl
)
348 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
351 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
353 mutex_lock(&priv
->sec_mtx
);
354 res
= mac802154_llsec_seclevel_add(&priv
->sec
, sl
);
355 mutex_unlock(&priv
->sec_mtx
);
360 int mac802154_del_seclevel(struct net_device
*dev
,
361 const struct ieee802154_llsec_seclevel
*sl
)
363 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
366 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
368 mutex_lock(&priv
->sec_mtx
);
369 res
= mac802154_llsec_seclevel_del(&priv
->sec
, sl
);
370 mutex_unlock(&priv
->sec_mtx
);
376 void mac802154_lock_table(struct net_device
*dev
)
378 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
380 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
382 mutex_lock(&priv
->sec_mtx
);
385 void mac802154_get_table(struct net_device
*dev
,
386 struct ieee802154_llsec_table
**t
)
388 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
390 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
392 *t
= &priv
->sec
.table
;
395 void mac802154_unlock_table(struct net_device
*dev
)
397 struct mac802154_sub_if_data
*priv
= netdev_priv(dev
);
399 BUG_ON(dev
->type
!= ARPHRD_IEEE802154
);
401 mutex_unlock(&priv
->sec_mtx
);