blob: ed26952f9e143407723dca1ef45d8400424aca3e [file] [log] [blame]
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +00001/*
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 *
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000013 * 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/netdevice.h>
21#include <linux/module.h>
22#include <linux/if_arp.h>
Alexander Aring4ca24ac2014-10-25 09:41:04 +020023#include <linux/ieee802154.h>
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000024
Alexander Aring944742a2014-11-17 08:20:49 +010025#include <net/nl802154.h>
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000026#include <net/mac802154.h>
27#include <net/ieee802154_netdev.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020028#include <net/cfg802154.h>
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000029
Alexander Aring0f1556b2014-10-25 09:41:00 +020030#include "ieee802154_i.h"
Alexander Aring59cb3002014-10-28 18:21:21 +010031#include "driver-ops.h"
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000032
Alexander Aringd77b4852015-06-21 16:45:20 +020033int mac802154_wpan_update_llsec(struct net_device *dev)
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +020034{
Alexander Aring59d19cd2014-10-25 17:16:40 +020035 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +020036 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
Alexander Aring863e88f2014-11-09 08:36:45 +010037 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +020038 int rc = 0;
39
40 if (ops->llsec) {
41 struct ieee802154_llsec_params params;
42 int changed = 0;
43
Alexander Aring863e88f2014-11-09 08:36:45 +010044 params.pan_id = wpan_dev->pan_id;
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +020045 changed |= IEEE802154_LLSEC_PARAM_PAN_ID;
46
Alexander Aring863e88f2014-11-09 08:36:45 +010047 params.hwaddr = wpan_dev->extended_addr;
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +020048 changed |= IEEE802154_LLSEC_PARAM_HWADDR;
49
50 rc = ops->llsec->set_params(dev, &params, changed);
51 }
52
53 return rc;
54}
55
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000056static int
57mac802154_wpan_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
58{
Alexander Aring59d19cd2014-10-25 17:16:40 +020059 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Alexander Aring863e88f2014-11-09 08:36:45 +010060 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000061 struct sockaddr_ieee802154 *sa =
62 (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
63 int err = -ENOIOCTLCMD;
64
Lennert Buytenheke34fd872015-05-26 15:06:10 +030065 if (cmd != SIOCGIFADDR && cmd != SIOCSIFADDR)
66 return err;
67
Alexander Aring4a669f72015-05-22 17:43:51 +020068 rtnl_lock();
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000069
70 switch (cmd) {
71 case SIOCGIFADDR:
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010072 {
73 u16 pan_id, short_addr;
74
Alexander Aring863e88f2014-11-09 08:36:45 +010075 pan_id = le16_to_cpu(wpan_dev->pan_id);
76 short_addr = le16_to_cpu(wpan_dev->short_addr);
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010077 if (pan_id == IEEE802154_PANID_BROADCAST ||
78 short_addr == IEEE802154_ADDR_BROADCAST) {
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000079 err = -EADDRNOTAVAIL;
80 break;
81 }
82
83 sa->family = AF_IEEE802154;
84 sa->addr.addr_type = IEEE802154_ADDR_SHORT;
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010085 sa->addr.pan_id = pan_id;
86 sa->addr.short_addr = short_addr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000087
88 err = 0;
89 break;
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010090 }
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000091 case SIOCSIFADDR:
Alexander Aringf7cb96f2014-11-09 08:36:59 +010092 if (netif_running(dev)) {
Alexander Aring4a669f72015-05-22 17:43:51 +020093 rtnl_unlock();
Alexander Aringf7cb96f2014-11-09 08:36:59 +010094 return -EBUSY;
95 }
96
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000097 dev_warn(&dev->dev,
Masanari Iida9b134942014-08-06 22:31:28 +090098 "Using DEBUGing ioctl SIOCSIFADDR isn't recommended!\n");
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000099 if (sa->family != AF_IEEE802154 ||
100 sa->addr.addr_type != IEEE802154_ADDR_SHORT ||
101 sa->addr.pan_id == IEEE802154_PANID_BROADCAST ||
102 sa->addr.short_addr == IEEE802154_ADDR_BROADCAST ||
103 sa->addr.short_addr == IEEE802154_ADDR_UNDEF) {
104 err = -EINVAL;
105 break;
106 }
107
Alexander Aring863e88f2014-11-09 08:36:45 +0100108 wpan_dev->pan_id = cpu_to_le16(sa->addr.pan_id);
109 wpan_dev->short_addr = cpu_to_le16(sa->addr.short_addr);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000110
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +0200111 err = mac802154_wpan_update_llsec(dev);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000112 break;
113 }
114
Alexander Aring4a669f72015-05-22 17:43:51 +0200115 rtnl_unlock();
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000116 return err;
117}
118
119static int mac802154_wpan_mac_addr(struct net_device *dev, void *p)
120{
Alexander Aring776e59d2014-11-02 04:18:41 +0100121 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000122 struct sockaddr *addr = p;
Alexander Aringea7053c2014-11-02 04:18:46 +0100123 __le64 extended_addr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000124
125 if (netif_running(dev))
126 return -EBUSY;
127
Alexander Aring09095fd2015-08-10 21:15:54 +0200128 /* lowpan need to be down for update
129 * SLAAC address after ifup
130 */
131 if (sdata->wpan_dev.lowpan_dev) {
132 if (netif_running(sdata->wpan_dev.lowpan_dev))
133 return -EBUSY;
134 }
135
Alexander Aring705cbbb2014-11-05 20:51:24 +0100136 ieee802154_be64_to_le64(&extended_addr, addr->sa_data);
Lennert Buytenhekdaf4e2c2015-05-28 15:38:43 +0300137 if (!ieee802154_is_valid_extended_unicast_addr(extended_addr))
Alexander Aringea7053c2014-11-02 04:18:46 +0100138 return -EINVAL;
139
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000140 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
Alexander Aring863e88f2014-11-09 08:36:45 +0100141 sdata->wpan_dev.extended_addr = extended_addr;
Alexander Aring776e59d2014-11-02 04:18:41 +0100142
Alexander Aring09095fd2015-08-10 21:15:54 +0200143 /* update lowpan interface mac address when
144 * wpan mac has been changed
145 */
146 if (sdata->wpan_dev.lowpan_dev)
147 memcpy(sdata->wpan_dev.lowpan_dev->dev_addr, dev->dev_addr,
148 dev->addr_len);
149
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +0200150 return mac802154_wpan_update_llsec(dev);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000151}
152
Alexander Aring95c0aa12015-06-17 15:35:17 +0200153static int ieee802154_setup_hw(struct ieee802154_sub_if_data *sdata)
154{
155 struct ieee802154_local *local = sdata->local;
156 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
157 int ret;
158
159 if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) {
160 ret = drv_set_promiscuous_mode(local,
161 wpan_dev->promiscuous_mode);
162 if (ret < 0)
163 return ret;
164 }
165
166 if (local->hw.flags & IEEE802154_HW_AFILT) {
167 ret = drv_set_pan_id(local, wpan_dev->pan_id);
168 if (ret < 0)
169 return ret;
170
171 ret = drv_set_extended_addr(local, wpan_dev->extended_addr);
172 if (ret < 0)
173 return ret;
174
175 ret = drv_set_short_addr(local, wpan_dev->short_addr);
176 if (ret < 0)
177 return ret;
178 }
179
180 if (local->hw.flags & IEEE802154_HW_LBT) {
181 ret = drv_set_lbt_mode(local, wpan_dev->lbt);
182 if (ret < 0)
183 return ret;
184 }
185
186 if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) {
187 ret = drv_set_csma_params(local, wpan_dev->min_be,
188 wpan_dev->max_be,
189 wpan_dev->csma_retries);
190 if (ret < 0)
191 return ret;
192 }
193
194 if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) {
195 ret = drv_set_max_frame_retries(local, wpan_dev->frame_retries);
196 if (ret < 0)
197 return ret;
198 }
199
200 return 0;
201}
202
Alexander Aring19ec6902014-10-28 18:21:17 +0100203static int mac802154_slave_open(struct net_device *dev)
204{
205 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Alexander Aring19ec6902014-10-28 18:21:17 +0100206 struct ieee802154_local *local = sdata->local;
Alexander Aring5c698e82015-06-17 15:35:18 +0200207 int res;
Alexander Aring19ec6902014-10-28 18:21:17 +0100208
209 ASSERT_RTNL();
210
Alexander Aring0ea3da62014-10-28 18:21:24 +0100211 set_bit(SDATA_STATE_RUNNING, &sdata->state);
Alexander Aring19ec6902014-10-28 18:21:17 +0100212
Alexander Aring74457642014-10-28 18:21:31 +0100213 if (!local->open_count) {
Alexander Aring95c0aa12015-06-17 15:35:17 +0200214 res = ieee802154_setup_hw(sdata);
215 if (res)
216 goto err;
217
Alexander Aring59cb3002014-10-28 18:21:21 +0100218 res = drv_start(local);
Alexander Aring19ec6902014-10-28 18:21:17 +0100219 if (res)
220 goto err;
221 }
222
Alexander Aring74457642014-10-28 18:21:31 +0100223 local->open_count++;
Alexander Aring19ec6902014-10-28 18:21:17 +0100224 netif_start_queue(dev);
225 return 0;
226err:
Alexander Aring0ea3da62014-10-28 18:21:24 +0100227 /* might already be clear but that doesn't matter */
228 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
Alexander Aring19ec6902014-10-28 18:21:17 +0100229
230 return res;
231}
232
Alexander Aringbd37a782014-12-19 23:45:58 +0100233static int
234ieee802154_check_mac_settings(struct ieee802154_local *local,
235 struct wpan_dev *wpan_dev,
236 struct wpan_dev *nwpan_dev)
237{
238 ASSERT_RTNL();
239
240 if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) {
241 if (wpan_dev->promiscuous_mode != nwpan_dev->promiscuous_mode)
242 return -EBUSY;
243 }
244
245 if (local->hw.flags & IEEE802154_HW_AFILT) {
Alexander Aring8bf95382015-03-26 12:46:28 +0100246 if (wpan_dev->pan_id != nwpan_dev->pan_id ||
247 wpan_dev->short_addr != nwpan_dev->short_addr ||
248 wpan_dev->extended_addr != nwpan_dev->extended_addr)
Alexander Aringbd37a782014-12-19 23:45:58 +0100249 return -EBUSY;
250 }
251
252 if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) {
Alexander Aring8bf95382015-03-26 12:46:28 +0100253 if (wpan_dev->min_be != nwpan_dev->min_be ||
254 wpan_dev->max_be != nwpan_dev->max_be ||
255 wpan_dev->csma_retries != nwpan_dev->csma_retries)
Alexander Aringbd37a782014-12-19 23:45:58 +0100256 return -EBUSY;
257 }
258
259 if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) {
260 if (wpan_dev->frame_retries != nwpan_dev->frame_retries)
261 return -EBUSY;
262 }
263
264 if (local->hw.flags & IEEE802154_HW_LBT) {
265 if (wpan_dev->lbt != nwpan_dev->lbt)
266 return -EBUSY;
267 }
268
269 return 0;
270}
271
272static int
273ieee802154_check_concurrent_iface(struct ieee802154_sub_if_data *sdata,
274 enum nl802154_iftype iftype)
275{
276 struct ieee802154_local *local = sdata->local;
277 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
278 struct ieee802154_sub_if_data *nsdata;
279
280 /* we hold the RTNL here so can safely walk the list */
281 list_for_each_entry(nsdata, &local->interfaces, list) {
282 if (nsdata != sdata && ieee802154_sdata_running(nsdata)) {
283 int ret;
284
Alexander Aringcd1c5662014-12-19 23:45:59 +0100285 /* TODO currently we don't support multiple node types
286 * we need to run skb_clone at rx path. Check if there
287 * exist really an use case if we need to support
288 * multiple node types at the same time.
289 */
Alexander Aringed659632015-06-06 17:30:46 +0200290 if (wpan_dev->iftype == NL802154_IFTYPE_NODE &&
291 nsdata->wpan_dev.iftype == NL802154_IFTYPE_NODE)
Alexander Aringcd1c5662014-12-19 23:45:59 +0100292 return -EBUSY;
293
Alexander Aringbd37a782014-12-19 23:45:58 +0100294 /* check all phy mac sublayer settings are the same.
295 * We have only one phy, different values makes trouble.
296 */
297 ret = ieee802154_check_mac_settings(local, wpan_dev,
298 &nsdata->wpan_dev);
299 if (ret < 0)
300 return ret;
301 }
302 }
303
304 return 0;
305}
306
Phoebe Buckheister6ef00232014-05-14 17:43:11 +0200307static int mac802154_wpan_open(struct net_device *dev)
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200308{
309 int rc;
Alexander Aring59d19cd2014-10-25 17:16:40 +0200310 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Alexander Aring863e88f2014-11-09 08:36:45 +0100311 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200312
Alexander Aringed659632015-06-06 17:30:46 +0200313 rc = ieee802154_check_concurrent_iface(sdata, wpan_dev->iftype);
Alexander Aringbd37a782014-12-19 23:45:58 +0100314 if (rc < 0)
315 return rc;
316
Alexander Aring95c0aa12015-06-17 15:35:17 +0200317 return mac802154_slave_open(dev);
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200318}
319
Alexander Aring19ec6902014-10-28 18:21:17 +0100320static int mac802154_slave_close(struct net_device *dev)
321{
322 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
323 struct ieee802154_local *local = sdata->local;
324
325 ASSERT_RTNL();
326
327 netif_stop_queue(dev);
Alexander Aring74457642014-10-28 18:21:31 +0100328 local->open_count--;
Alexander Aring19ec6902014-10-28 18:21:17 +0100329
Alexander Aring0ea3da62014-10-28 18:21:24 +0100330 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
Alexander Aring19ec6902014-10-28 18:21:17 +0100331
Alexander Aringc4227c82015-06-24 11:36:34 +0200332 if (!local->open_count)
333 ieee802154_stop_device(local);
Alexander Aring19ec6902014-10-28 18:21:17 +0100334
335 return 0;
336}
337
Alexander Aring036562f2014-10-25 17:16:36 +0200338static int mac802154_set_header_security(struct ieee802154_sub_if_data *sdata,
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200339 struct ieee802154_hdr *hdr,
340 const struct ieee802154_mac_cb *cb)
341{
342 struct ieee802154_llsec_params params;
343 u8 level;
344
Alexander Aring036562f2014-10-25 17:16:36 +0200345 mac802154_llsec_get_params(&sdata->sec, &params);
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200346
347 if (!params.enabled && cb->secen_override && cb->secen)
348 return -EINVAL;
349 if (!params.enabled ||
350 (cb->secen_override && !cb->secen) ||
351 !params.out_level)
352 return 0;
353 if (cb->seclevel_override && !cb->seclevel)
354 return -EINVAL;
355
356 level = cb->seclevel_override ? cb->seclevel : params.out_level;
357
358 hdr->fc.security_enabled = 1;
359 hdr->sec.level = level;
360 hdr->sec.key_id_mode = params.out_key.mode;
361 if (params.out_key.mode == IEEE802154_SCF_KEY_SHORT_INDEX)
362 hdr->sec.short_src = params.out_key.short_source;
363 else if (params.out_key.mode == IEEE802154_SCF_KEY_HW_INDEX)
364 hdr->sec.extended_src = params.out_key.extended_source;
365 hdr->sec.key_id = params.out_key.id;
366
367 return 0;
368}
369
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000370static int mac802154_header_create(struct sk_buff *skb,
371 struct net_device *dev,
372 unsigned short type,
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100373 const void *daddr,
374 const void *saddr,
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000375 unsigned len)
376{
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100377 struct ieee802154_hdr hdr;
Alexander Aring59d19cd2014-10-25 17:16:40 +0200378 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Alexander Aring863e88f2014-11-09 08:36:45 +0100379 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
Phoebe Buckheister32edc402014-05-14 17:43:08 +0200380 struct ieee802154_mac_cb *cb = mac_cb(skb);
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100381 int hlen;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000382
383 if (!daddr)
384 return -EINVAL;
385
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100386 memset(&hdr.fc, 0, sizeof(hdr.fc));
Phoebe Buckheister32edc402014-05-14 17:43:08 +0200387 hdr.fc.type = cb->type;
388 hdr.fc.security_enabled = cb->secen;
389 hdr.fc.ack_request = cb->ackreq;
Alexander Aring344f8c12015-05-22 17:43:53 +0200390 hdr.seq = atomic_inc_return(&dev->ieee802154_ptr->dsn) & 0xFF;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000391
Alexander Aring036562f2014-10-25 17:16:36 +0200392 if (mac802154_set_header_security(sdata, &hdr, cb) < 0)
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200393 return -EINVAL;
394
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000395 if (!saddr) {
Alexander Aring863e88f2014-11-09 08:36:45 +0100396 if (wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) ||
397 wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
398 wpan_dev->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100399 hdr.source.mode = IEEE802154_ADDR_LONG;
Alexander Aring863e88f2014-11-09 08:36:45 +0100400 hdr.source.extended_addr = wpan_dev->extended_addr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000401 } else {
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100402 hdr.source.mode = IEEE802154_ADDR_SHORT;
Alexander Aring863e88f2014-11-09 08:36:45 +0100403 hdr.source.short_addr = wpan_dev->short_addr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000404 }
405
Alexander Aring863e88f2014-11-09 08:36:45 +0100406 hdr.source.pan_id = wpan_dev->pan_id;
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100407 } else {
408 hdr.source = *(const struct ieee802154_addr *)saddr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000409 }
410
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100411 hdr.dest = *(const struct ieee802154_addr *)daddr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000412
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100413 hlen = ieee802154_hdr_push(skb, &hdr);
414 if (hlen < 0)
415 return -EINVAL;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000416
Alexander Aring3e691622013-10-30 09:18:22 +0100417 skb_reset_mac_header(skb);
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100418 skb->mac_len = hlen;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000419
Phoebe Buckheister8c842962014-05-14 17:43:07 +0200420 if (len > ieee802154_max_payload(&hdr))
Phoebe Buckheisterd1d73582014-03-14 21:24:04 +0100421 return -EMSGSIZE;
422
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100423 return hlen;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000424}
425
426static int
427mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
428{
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100429 struct ieee802154_hdr hdr;
430 struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000431
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100432 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
433 pr_debug("malformed packet\n");
434 return 0;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000435 }
436
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100437 *addr = hdr.source;
438 return sizeof(*addr);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000439}
440
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000441static struct header_ops mac802154_header_ops = {
442 .create = mac802154_header_create,
443 .parse = mac802154_header_parse,
444};
445
446static const struct net_device_ops mac802154_wpan_ops = {
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200447 .ndo_open = mac802154_wpan_open,
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000448 .ndo_stop = mac802154_slave_close,
Alexander Aringe5e584f2014-10-26 09:37:13 +0100449 .ndo_start_xmit = ieee802154_subif_start_xmit,
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000450 .ndo_do_ioctl = mac802154_wpan_ioctl,
451 .ndo_set_mac_address = mac802154_wpan_mac_addr,
452};
453
Alexander Aringb9ff77e2014-10-28 18:21:16 +0100454static const struct net_device_ops mac802154_monitor_ops = {
Alexander Aring38130c32014-10-29 21:34:44 +0100455 .ndo_open = mac802154_wpan_open,
Alexander Aringb9ff77e2014-10-28 18:21:16 +0100456 .ndo_stop = mac802154_slave_close,
457 .ndo_start_xmit = ieee802154_monitor_start_xmit,
458};
459
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200460static void mac802154_wpan_free(struct net_device *dev)
461{
Alexander Aring59d19cd2014-10-25 17:16:40 +0200462 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200463
Alexander Aring036562f2014-10-25 17:16:36 +0200464 mac802154_llsec_destroy(&sdata->sec);
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200465
466 free_netdev(dev);
467}
468
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100469static void ieee802154_if_setup(struct net_device *dev)
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000470{
Alexander Aringe57a8942014-11-05 20:51:28 +0100471 dev->addr_len = IEEE802154_EXTENDED_ADDR_LEN;
472 memset(dev->broadcast, 0xff, IEEE802154_EXTENDED_ADDR_LEN);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000473
474 dev->hard_header_len = MAC802154_FRAME_HARD_HEADER_LEN;
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200475 dev->needed_tailroom = 2 + 16; /* FCS + MIC */
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000476 dev->mtu = IEEE802154_MTU;
Alan Otte937f5832013-04-03 04:00:57 +0000477 dev->tx_queue_len = 300;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000478 dev->flags = IFF_NOARP | IFF_BROADCAST;
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100479}
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000480
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100481static int
Alexander Aring944742a2014-11-17 08:20:49 +0100482ieee802154_setup_sdata(struct ieee802154_sub_if_data *sdata,
483 enum nl802154_iftype type)
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100484{
Alexander Aring863e88f2014-11-09 08:36:45 +0100485 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
Alexander Aringd77b4852015-06-21 16:45:20 +0200486 int ret;
Alexander Aring344f8c12015-05-22 17:43:53 +0200487 u8 tmp;
Alexander Aring863e88f2014-11-09 08:36:45 +0100488
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100489 /* set some type-dependent values */
Alexander Aring190ac1c2014-11-09 08:36:49 +0100490 sdata->wpan_dev.iftype = type;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000491
Alexander Aring344f8c12015-05-22 17:43:53 +0200492 get_random_bytes(&tmp, sizeof(tmp));
493 atomic_set(&wpan_dev->bsn, tmp);
494 get_random_bytes(&tmp, sizeof(tmp));
495 atomic_set(&wpan_dev->dsn, tmp);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000496
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200497 /* defaults per 802.15.4-2011 */
Alexander Aring5fb3f022014-11-09 08:36:46 +0100498 wpan_dev->min_be = 3;
499 wpan_dev->max_be = 5;
500 wpan_dev->csma_retries = 4;
Alexander Aring89c7d782015-08-10 21:15:56 +0200501 wpan_dev->frame_retries = 3;
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200502
Alexander Aring863e88f2014-11-09 08:36:45 +0100503 wpan_dev->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
504 wpan_dev->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200505
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100506 switch (type) {
Alexander Aring944742a2014-11-17 08:20:49 +0100507 case NL802154_IFTYPE_NODE:
Alexander Aringb03c9cc2014-11-09 08:36:56 +0100508 ieee802154_be64_to_le64(&wpan_dev->extended_addr,
509 sdata->dev->dev_addr);
510
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100511 sdata->dev->header_ops = &mac802154_header_ops;
512 sdata->dev->destructor = mac802154_wpan_free;
513 sdata->dev->netdev_ops = &mac802154_wpan_ops;
514 sdata->dev->ml_priv = &mac802154_mlme_wpan;
Alexander Aring863e88f2014-11-09 08:36:45 +0100515 wpan_dev->promiscuous_mode = false;
Alexander Aring38130c32014-10-29 21:34:44 +0100516
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100517 mutex_init(&sdata->sec_mtx);
Alexander Aringb9ff77e2014-10-28 18:21:16 +0100518
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100519 mac802154_llsec_init(&sdata->sec);
Alexander Aringd77b4852015-06-21 16:45:20 +0200520 ret = mac802154_wpan_update_llsec(sdata->dev);
521 if (ret < 0)
522 return ret;
523
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100524 break;
Alexander Aring944742a2014-11-17 08:20:49 +0100525 case NL802154_IFTYPE_MONITOR:
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100526 sdata->dev->destructor = free_netdev;
527 sdata->dev->netdev_ops = &mac802154_monitor_ops;
Alexander Aring863e88f2014-11-09 08:36:45 +0100528 wpan_dev->promiscuous_mode = true;
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100529 break;
530 default:
531 BUG();
532 }
Alexander Aring986a8ab2014-11-05 20:51:15 +0100533
534 return 0;
535}
536
537struct net_device *
538ieee802154_if_add(struct ieee802154_local *local, const char *name,
Varka Bhadram5b4a10392015-04-30 17:44:57 +0200539 unsigned char name_assign_type, enum nl802154_iftype type,
540 __le64 extended_addr)
Alexander Aring986a8ab2014-11-05 20:51:15 +0100541{
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100542 struct net_device *ndev = NULL;
543 struct ieee802154_sub_if_data *sdata = NULL;
544 int ret = -ENOMEM;
Alexander Aring986a8ab2014-11-05 20:51:15 +0100545
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100546 ASSERT_RTNL();
547
Alexander Aringed659632015-06-06 17:30:46 +0200548 ndev = alloc_netdev(sizeof(*sdata), name,
Varka Bhadram5b4a10392015-04-30 17:44:57 +0200549 name_assign_type, ieee802154_if_setup);
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100550 if (!ndev)
551 return ERR_PTR(-ENOMEM);
552
553 ndev->needed_headroom = local->hw.extra_tx_headroom;
554
555 ret = dev_alloc_name(ndev, ndev->name);
556 if (ret < 0)
Alexander Aring986a8ab2014-11-05 20:51:15 +0100557 goto err;
558
Alexander Aring0e575472014-11-17 08:20:52 +0100559 ieee802154_le64_to_be64(ndev->perm_addr,
560 &local->hw.phy->perm_extended_addr);
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100561 switch (type) {
Alexander Aring944742a2014-11-17 08:20:49 +0100562 case NL802154_IFTYPE_NODE:
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100563 ndev->type = ARPHRD_IEEE802154;
Lennert Buytenhekdaf4e2c2015-05-28 15:38:43 +0300564 if (ieee802154_is_valid_extended_unicast_addr(extended_addr))
Alexander Aring0e575472014-11-17 08:20:52 +0100565 ieee802154_le64_to_be64(ndev->dev_addr, &extended_addr);
566 else
567 memcpy(ndev->dev_addr, ndev->perm_addr,
568 IEEE802154_EXTENDED_ADDR_LEN);
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100569 break;
Alexander Aring944742a2014-11-17 08:20:49 +0100570 case NL802154_IFTYPE_MONITOR:
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100571 ndev->type = ARPHRD_IEEE802154_MONITOR;
572 break;
573 default:
574 ret = -EINVAL;
575 goto err;
576 }
Alexander Aring986a8ab2014-11-05 20:51:15 +0100577
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100578 /* TODO check this */
579 SET_NETDEV_DEV(ndev, &local->phy->dev);
580 sdata = netdev_priv(ndev);
581 ndev->ieee802154_ptr = &sdata->wpan_dev;
582 memcpy(sdata->name, ndev->name, IFNAMSIZ);
583 sdata->dev = ndev;
584 sdata->wpan_dev.wpan_phy = local->hw.phy;
585 sdata->local = local;
Alexander Aring986a8ab2014-11-05 20:51:15 +0100586
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100587 /* setup type-dependent data */
588 ret = ieee802154_setup_sdata(sdata, type);
589 if (ret)
590 goto err;
591
Varka Bhadram473f3762014-11-25 16:34:43 +0530592 ret = register_netdevice(ndev);
593 if (ret < 0)
594 goto err;
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100595
596 mutex_lock(&local->iflist_mtx);
597 list_add_tail_rcu(&sdata->list, &local->interfaces);
598 mutex_unlock(&local->iflist_mtx);
599
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100600 return ndev;
601
Alexander Aring986a8ab2014-11-05 20:51:15 +0100602err:
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100603 free_netdev(ndev);
604 return ERR_PTR(ret);
Alexander Aring986a8ab2014-11-05 20:51:15 +0100605}
606
Alexander Aringb210b182014-11-05 20:51:14 +0100607void ieee802154_if_remove(struct ieee802154_sub_if_data *sdata)
608{
609 ASSERT_RTNL();
610
611 mutex_lock(&sdata->local->iflist_mtx);
612 list_del_rcu(&sdata->list);
613 mutex_unlock(&sdata->local->iflist_mtx);
614
615 synchronize_rcu();
616 unregister_netdevice(sdata->dev);
617}
Alexander Aring592dfbf2014-11-12 03:36:48 +0100618
619void ieee802154_remove_interfaces(struct ieee802154_local *local)
620{
Alexander Aringd14e1c72014-11-12 03:36:49 +0100621 struct ieee802154_sub_if_data *sdata, *tmp;
Alexander Aring592dfbf2014-11-12 03:36:48 +0100622
Alexander Aring2789e622014-11-12 03:36:50 +0100623 mutex_lock(&local->iflist_mtx);
Alexander Aringd14e1c72014-11-12 03:36:49 +0100624 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
Alexander Aring592dfbf2014-11-12 03:36:48 +0100625 list_del(&sdata->list);
Alexander Aring592dfbf2014-11-12 03:36:48 +0100626
627 unregister_netdevice(sdata->dev);
628 }
Alexander Aring2789e622014-11-12 03:36:50 +0100629 mutex_unlock(&local->iflist_mtx);
Alexander Aring592dfbf2014-11-12 03:36:48 +0100630}
Alexander Aringbe4fd8e2014-11-12 03:36:53 +0100631
632static int netdev_notify(struct notifier_block *nb,
633 unsigned long state, void *ptr)
634{
635 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
636 struct ieee802154_sub_if_data *sdata;
637
638 if (state != NETDEV_CHANGENAME)
639 return NOTIFY_DONE;
640
641 if (!dev->ieee802154_ptr || !dev->ieee802154_ptr->wpan_phy)
642 return NOTIFY_DONE;
643
644 if (dev->ieee802154_ptr->wpan_phy->privid != mac802154_wpan_phy_privid)
645 return NOTIFY_DONE;
646
647 sdata = IEEE802154_DEV_TO_SUB_IF(dev);
648 memcpy(sdata->name, dev->name, IFNAMSIZ);
649
650 return NOTIFY_OK;
651}
652
653static struct notifier_block mac802154_netdev_notifier = {
654 .notifier_call = netdev_notify,
655};
656
657int ieee802154_iface_init(void)
658{
659 return register_netdevice_notifier(&mac802154_netdev_notifier);
660}
661
662void ieee802154_iface_exit(void)
663{
664 unregister_netdevice_notifier(&mac802154_netdev_notifier);
665}