blob: 6669da7446f2936607d3ac3df228db42150f2467 [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
25#include <net/rtnetlink.h>
26#include <linux/nl802154.h>
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000027#include <net/mac802154.h>
28#include <net/ieee802154_netdev.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020029#include <net/cfg802154.h>
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000030
Alexander Aring0f1556b2014-10-25 09:41:00 +020031#include "ieee802154_i.h"
Alexander Aring59cb3002014-10-28 18:21:21 +010032#include "driver-ops.h"
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000033
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +020034static int mac802154_wpan_update_llsec(struct net_device *dev)
35{
Alexander Aring59d19cd2014-10-25 17:16:40 +020036 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +020037 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
Alexander Aring863e88f2014-11-09 08:36:45 +010038 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +020039 int rc = 0;
40
41 if (ops->llsec) {
42 struct ieee802154_llsec_params params;
43 int changed = 0;
44
Alexander Aring863e88f2014-11-09 08:36:45 +010045 params.pan_id = wpan_dev->pan_id;
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +020046 changed |= IEEE802154_LLSEC_PARAM_PAN_ID;
47
Alexander Aring863e88f2014-11-09 08:36:45 +010048 params.hwaddr = wpan_dev->extended_addr;
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +020049 changed |= IEEE802154_LLSEC_PARAM_HWADDR;
50
51 rc = ops->llsec->set_params(dev, &params, changed);
52 }
53
54 return rc;
55}
56
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000057static int
58mac802154_wpan_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
59{
Alexander Aring59d19cd2014-10-25 17:16:40 +020060 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Alexander Aring863e88f2014-11-09 08:36:45 +010061 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000062 struct sockaddr_ieee802154 *sa =
63 (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
64 int err = -ENOIOCTLCMD;
65
Alexander Aring036562f2014-10-25 17:16:36 +020066 spin_lock_bh(&sdata->mib_lock);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000067
68 switch (cmd) {
69 case SIOCGIFADDR:
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010070 {
71 u16 pan_id, short_addr;
72
Alexander Aring863e88f2014-11-09 08:36:45 +010073 pan_id = le16_to_cpu(wpan_dev->pan_id);
74 short_addr = le16_to_cpu(wpan_dev->short_addr);
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010075 if (pan_id == IEEE802154_PANID_BROADCAST ||
76 short_addr == IEEE802154_ADDR_BROADCAST) {
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000077 err = -EADDRNOTAVAIL;
78 break;
79 }
80
81 sa->family = AF_IEEE802154;
82 sa->addr.addr_type = IEEE802154_ADDR_SHORT;
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010083 sa->addr.pan_id = pan_id;
84 sa->addr.short_addr = short_addr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000085
86 err = 0;
87 break;
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010088 }
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000089 case SIOCSIFADDR:
90 dev_warn(&dev->dev,
Masanari Iida9b134942014-08-06 22:31:28 +090091 "Using DEBUGing ioctl SIOCSIFADDR isn't recommended!\n");
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000092 if (sa->family != AF_IEEE802154 ||
93 sa->addr.addr_type != IEEE802154_ADDR_SHORT ||
94 sa->addr.pan_id == IEEE802154_PANID_BROADCAST ||
95 sa->addr.short_addr == IEEE802154_ADDR_BROADCAST ||
96 sa->addr.short_addr == IEEE802154_ADDR_UNDEF) {
97 err = -EINVAL;
98 break;
99 }
100
Alexander Aring863e88f2014-11-09 08:36:45 +0100101 wpan_dev->pan_id = cpu_to_le16(sa->addr.pan_id);
102 wpan_dev->short_addr = cpu_to_le16(sa->addr.short_addr);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000103
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +0200104 err = mac802154_wpan_update_llsec(dev);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000105 break;
106 }
107
Alexander Aring036562f2014-10-25 17:16:36 +0200108 spin_unlock_bh(&sdata->mib_lock);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000109 return err;
110}
111
112static int mac802154_wpan_mac_addr(struct net_device *dev, void *p)
113{
Alexander Aring776e59d2014-11-02 04:18:41 +0100114 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000115 struct sockaddr *addr = p;
Alexander Aringea7053c2014-11-02 04:18:46 +0100116 __le64 extended_addr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000117
118 if (netif_running(dev))
119 return -EBUSY;
120
Alexander Aring705cbbb2014-11-05 20:51:24 +0100121 ieee802154_be64_to_le64(&extended_addr, addr->sa_data);
Alexander Aringea7053c2014-11-02 04:18:46 +0100122 if (!ieee802154_is_valid_extended_addr(extended_addr))
123 return -EINVAL;
124
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000125 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
Alexander Aring863e88f2014-11-09 08:36:45 +0100126 sdata->wpan_dev.extended_addr = extended_addr;
Alexander Aring776e59d2014-11-02 04:18:41 +0100127
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +0200128 return mac802154_wpan_update_llsec(dev);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000129}
130
Alexander Aring19ec6902014-10-28 18:21:17 +0100131static int mac802154_slave_open(struct net_device *dev)
132{
133 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
134 struct ieee802154_sub_if_data *subif;
135 struct ieee802154_local *local = sdata->local;
136 int res = 0;
137
138 ASSERT_RTNL();
139
Alexander Aring7c118c12014-11-05 20:51:20 +0100140 if (sdata->vif.type == IEEE802154_DEV_WPAN) {
Alexander Aring19ec6902014-10-28 18:21:17 +0100141 mutex_lock(&sdata->local->iflist_mtx);
142 list_for_each_entry(subif, &sdata->local->interfaces, list) {
Alexander Aring7c118c12014-11-05 20:51:20 +0100143 if (subif != sdata &&
144 subif->vif.type == sdata->vif.type &&
Alexander Aring0ea3da62014-10-28 18:21:24 +0100145 ieee802154_sdata_running(subif)) {
Alexander Aring19ec6902014-10-28 18:21:17 +0100146 mutex_unlock(&sdata->local->iflist_mtx);
147 return -EBUSY;
148 }
149 }
150 mutex_unlock(&sdata->local->iflist_mtx);
151 }
152
Alexander Aring0ea3da62014-10-28 18:21:24 +0100153 set_bit(SDATA_STATE_RUNNING, &sdata->state);
Alexander Aring19ec6902014-10-28 18:21:17 +0100154
Alexander Aring74457642014-10-28 18:21:31 +0100155 if (!local->open_count) {
Alexander Aring59cb3002014-10-28 18:21:21 +0100156 res = drv_start(local);
Alexander Aring19ec6902014-10-28 18:21:17 +0100157 WARN_ON(res);
158 if (res)
159 goto err;
160 }
161
Alexander Aring74457642014-10-28 18:21:31 +0100162 local->open_count++;
Alexander Aring19ec6902014-10-28 18:21:17 +0100163 netif_start_queue(dev);
164 return 0;
165err:
Alexander Aring0ea3da62014-10-28 18:21:24 +0100166 /* might already be clear but that doesn't matter */
167 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
Alexander Aring19ec6902014-10-28 18:21:17 +0100168
169 return res;
170}
171
Phoebe Buckheister6ef00232014-05-14 17:43:11 +0200172static int mac802154_wpan_open(struct net_device *dev)
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200173{
174 int rc;
Alexander Aring59d19cd2014-10-25 17:16:40 +0200175 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Alexander Aring59cb3002014-10-28 18:21:21 +0100176 struct ieee802154_local *local = sdata->local;
Alexander Aring863e88f2014-11-09 08:36:45 +0100177 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
Alexander Aring04e850f2014-10-25 17:16:37 +0200178 struct wpan_phy *phy = sdata->local->phy;
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200179
180 rc = mac802154_slave_open(dev);
181 if (rc < 0)
182 return rc;
183
184 mutex_lock(&phy->pib_lock);
185
Alexander Aring38130c32014-10-29 21:34:44 +0100186 if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) {
Alexander Aring863e88f2014-11-09 08:36:45 +0100187 rc = drv_set_promiscuous_mode(local,
188 wpan_dev->promiscuous_mode);
Alexander Aring38130c32014-10-29 21:34:44 +0100189 if (rc < 0)
190 goto out;
191 }
192
Alexander Aring776e59d2014-11-02 04:18:41 +0100193 if (local->hw.flags & IEEE802154_HW_AFILT) {
Alexander Aring863e88f2014-11-09 08:36:45 +0100194 rc = drv_set_pan_id(local, wpan_dev->pan_id);
Alexander Aring50c79072014-11-02 04:18:43 +0100195 if (rc < 0)
196 goto out;
197
Alexander Aring863e88f2014-11-09 08:36:45 +0100198 rc = drv_set_extended_addr(local, wpan_dev->extended_addr);
Alexander Aring776e59d2014-11-02 04:18:41 +0100199 if (rc < 0)
200 goto out;
Alexander Aring78b4bad162014-11-02 04:18:42 +0100201
Alexander Aring863e88f2014-11-09 08:36:45 +0100202 rc = drv_set_short_addr(local, wpan_dev->short_addr);
Alexander Aring78b4bad162014-11-02 04:18:42 +0100203 if (rc < 0)
204 goto out;
Alexander Aring776e59d2014-11-02 04:18:41 +0100205 }
206
Alexander Aringa543c592014-10-28 18:21:23 +0100207 if (local->hw.flags & IEEE802154_HW_LBT) {
Alexander Aring59cb3002014-10-28 18:21:21 +0100208 rc = drv_set_lbt_mode(local, sdata->mac_params.lbt);
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200209 if (rc < 0)
210 goto out;
211 }
212
Alexander Aringa543c592014-10-28 18:21:23 +0100213 if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) {
Alexander Aring59cb3002014-10-28 18:21:21 +0100214 rc = drv_set_csma_params(local, sdata->mac_params.min_be,
215 sdata->mac_params.max_be,
216 sdata->mac_params.csma_retries);
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200217 if (rc < 0)
218 goto out;
219 }
220
Alexander Aringa543c592014-10-28 18:21:23 +0100221 if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) {
Alexander Aring59cb3002014-10-28 18:21:21 +0100222 rc = drv_set_max_frame_retries(local,
223 sdata->mac_params.frame_retries);
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200224 if (rc < 0)
225 goto out;
226 }
227
228 mutex_unlock(&phy->pib_lock);
229 return 0;
230
231out:
232 mutex_unlock(&phy->pib_lock);
233 return rc;
234}
235
Alexander Aring19ec6902014-10-28 18:21:17 +0100236static int mac802154_slave_close(struct net_device *dev)
237{
238 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
239 struct ieee802154_local *local = sdata->local;
240
241 ASSERT_RTNL();
242
243 netif_stop_queue(dev);
Alexander Aring74457642014-10-28 18:21:31 +0100244 local->open_count--;
Alexander Aring19ec6902014-10-28 18:21:17 +0100245
Alexander Aring0ea3da62014-10-28 18:21:24 +0100246 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
Alexander Aring19ec6902014-10-28 18:21:17 +0100247
Alexander Aring74457642014-10-28 18:21:31 +0100248 if (!local->open_count)
Alexander Aring59cb3002014-10-28 18:21:21 +0100249 drv_stop(local);
Alexander Aring19ec6902014-10-28 18:21:17 +0100250
251 return 0;
252}
253
Alexander Aring036562f2014-10-25 17:16:36 +0200254static int mac802154_set_header_security(struct ieee802154_sub_if_data *sdata,
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200255 struct ieee802154_hdr *hdr,
256 const struct ieee802154_mac_cb *cb)
257{
258 struct ieee802154_llsec_params params;
259 u8 level;
260
Alexander Aring036562f2014-10-25 17:16:36 +0200261 mac802154_llsec_get_params(&sdata->sec, &params);
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200262
263 if (!params.enabled && cb->secen_override && cb->secen)
264 return -EINVAL;
265 if (!params.enabled ||
266 (cb->secen_override && !cb->secen) ||
267 !params.out_level)
268 return 0;
269 if (cb->seclevel_override && !cb->seclevel)
270 return -EINVAL;
271
272 level = cb->seclevel_override ? cb->seclevel : params.out_level;
273
274 hdr->fc.security_enabled = 1;
275 hdr->sec.level = level;
276 hdr->sec.key_id_mode = params.out_key.mode;
277 if (params.out_key.mode == IEEE802154_SCF_KEY_SHORT_INDEX)
278 hdr->sec.short_src = params.out_key.short_source;
279 else if (params.out_key.mode == IEEE802154_SCF_KEY_HW_INDEX)
280 hdr->sec.extended_src = params.out_key.extended_source;
281 hdr->sec.key_id = params.out_key.id;
282
283 return 0;
284}
285
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000286static int mac802154_header_create(struct sk_buff *skb,
287 struct net_device *dev,
288 unsigned short type,
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100289 const void *daddr,
290 const void *saddr,
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000291 unsigned len)
292{
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100293 struct ieee802154_hdr hdr;
Alexander Aring59d19cd2014-10-25 17:16:40 +0200294 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Alexander Aring863e88f2014-11-09 08:36:45 +0100295 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
Phoebe Buckheister32edc402014-05-14 17:43:08 +0200296 struct ieee802154_mac_cb *cb = mac_cb(skb);
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100297 int hlen;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000298
299 if (!daddr)
300 return -EINVAL;
301
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100302 memset(&hdr.fc, 0, sizeof(hdr.fc));
Phoebe Buckheister32edc402014-05-14 17:43:08 +0200303 hdr.fc.type = cb->type;
304 hdr.fc.security_enabled = cb->secen;
305 hdr.fc.ack_request = cb->ackreq;
306 hdr.seq = ieee802154_mlme_ops(dev)->get_dsn(dev);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000307
Alexander Aring036562f2014-10-25 17:16:36 +0200308 if (mac802154_set_header_security(sdata, &hdr, cb) < 0)
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200309 return -EINVAL;
310
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000311 if (!saddr) {
Alexander Aring036562f2014-10-25 17:16:36 +0200312 spin_lock_bh(&sdata->mib_lock);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000313
Alexander Aring863e88f2014-11-09 08:36:45 +0100314 if (wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) ||
315 wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
316 wpan_dev->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100317 hdr.source.mode = IEEE802154_ADDR_LONG;
Alexander Aring863e88f2014-11-09 08:36:45 +0100318 hdr.source.extended_addr = wpan_dev->extended_addr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000319 } else {
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100320 hdr.source.mode = IEEE802154_ADDR_SHORT;
Alexander Aring863e88f2014-11-09 08:36:45 +0100321 hdr.source.short_addr = wpan_dev->short_addr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000322 }
323
Alexander Aring863e88f2014-11-09 08:36:45 +0100324 hdr.source.pan_id = wpan_dev->pan_id;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000325
Alexander Aring036562f2014-10-25 17:16:36 +0200326 spin_unlock_bh(&sdata->mib_lock);
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100327 } else {
328 hdr.source = *(const struct ieee802154_addr *)saddr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000329 }
330
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100331 hdr.dest = *(const struct ieee802154_addr *)daddr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000332
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100333 hlen = ieee802154_hdr_push(skb, &hdr);
334 if (hlen < 0)
335 return -EINVAL;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000336
Alexander Aring3e691622013-10-30 09:18:22 +0100337 skb_reset_mac_header(skb);
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100338 skb->mac_len = hlen;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000339
Phoebe Buckheister8c842962014-05-14 17:43:07 +0200340 if (len > ieee802154_max_payload(&hdr))
Phoebe Buckheisterd1d73582014-03-14 21:24:04 +0100341 return -EMSGSIZE;
342
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100343 return hlen;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000344}
345
346static int
347mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
348{
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100349 struct ieee802154_hdr hdr;
350 struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000351
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100352 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
353 pr_debug("malformed packet\n");
354 return 0;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000355 }
356
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100357 *addr = hdr.source;
358 return sizeof(*addr);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000359}
360
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000361static struct header_ops mac802154_header_ops = {
362 .create = mac802154_header_create,
363 .parse = mac802154_header_parse,
364};
365
366static const struct net_device_ops mac802154_wpan_ops = {
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200367 .ndo_open = mac802154_wpan_open,
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000368 .ndo_stop = mac802154_slave_close,
Alexander Aringe5e584f2014-10-26 09:37:13 +0100369 .ndo_start_xmit = ieee802154_subif_start_xmit,
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000370 .ndo_do_ioctl = mac802154_wpan_ioctl,
371 .ndo_set_mac_address = mac802154_wpan_mac_addr,
372};
373
Alexander Aringb9ff77e2014-10-28 18:21:16 +0100374static const struct net_device_ops mac802154_monitor_ops = {
Alexander Aring38130c32014-10-29 21:34:44 +0100375 .ndo_open = mac802154_wpan_open,
Alexander Aringb9ff77e2014-10-28 18:21:16 +0100376 .ndo_stop = mac802154_slave_close,
377 .ndo_start_xmit = ieee802154_monitor_start_xmit,
378};
379
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200380static void mac802154_wpan_free(struct net_device *dev)
381{
Alexander Aring59d19cd2014-10-25 17:16:40 +0200382 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200383
Alexander Aring036562f2014-10-25 17:16:36 +0200384 mac802154_llsec_destroy(&sdata->sec);
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200385
386 free_netdev(dev);
387}
388
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100389static void ieee802154_if_setup(struct net_device *dev)
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000390{
Alexander Aringe57a8942014-11-05 20:51:28 +0100391 dev->addr_len = IEEE802154_EXTENDED_ADDR_LEN;
392 memset(dev->broadcast, 0xff, IEEE802154_EXTENDED_ADDR_LEN);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000393
394 dev->hard_header_len = MAC802154_FRAME_HARD_HEADER_LEN;
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200395 dev->needed_tailroom = 2 + 16; /* FCS + MIC */
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000396 dev->mtu = IEEE802154_MTU;
Alan Otte937f5832013-04-03 04:00:57 +0000397 dev->tx_queue_len = 300;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000398 dev->flags = IFF_NOARP | IFF_BROADCAST;
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100399}
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000400
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100401static int
402ieee802154_setup_sdata(struct ieee802154_sub_if_data *sdata, int type)
403{
Alexander Aring863e88f2014-11-09 08:36:45 +0100404 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
405
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100406 /* set some type-dependent values */
Alexander Aring7c118c12014-11-05 20:51:20 +0100407 sdata->vif.type = type;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000408
Alexander Aring863e88f2014-11-09 08:36:45 +0100409 get_random_bytes(&wpan_dev->bsn, 1);
410 get_random_bytes(&wpan_dev->dsn, 1);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000411
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200412 /* defaults per 802.15.4-2011 */
Alexander Aring036562f2014-10-25 17:16:36 +0200413 sdata->mac_params.min_be = 3;
414 sdata->mac_params.max_be = 5;
415 sdata->mac_params.csma_retries = 4;
416 /* for compatibility, actual default is 3 */
417 sdata->mac_params.frame_retries = -1;
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200418
Alexander Aring863e88f2014-11-09 08:36:45 +0100419 ieee802154_be64_to_le64(&wpan_dev->extended_addr, sdata->dev->dev_addr);
420 wpan_dev->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
421 wpan_dev->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200422
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100423 switch (type) {
424 case IEEE802154_DEV_WPAN:
425 sdata->dev->header_ops = &mac802154_header_ops;
426 sdata->dev->destructor = mac802154_wpan_free;
427 sdata->dev->netdev_ops = &mac802154_wpan_ops;
428 sdata->dev->ml_priv = &mac802154_mlme_wpan;
Alexander Aring863e88f2014-11-09 08:36:45 +0100429 wpan_dev->promiscuous_mode = false;
Alexander Aring38130c32014-10-29 21:34:44 +0100430
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100431 spin_lock_init(&sdata->mib_lock);
432 mutex_init(&sdata->sec_mtx);
Alexander Aringb9ff77e2014-10-28 18:21:16 +0100433
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100434 mac802154_llsec_init(&sdata->sec);
435 break;
436 case IEEE802154_DEV_MONITOR:
437 sdata->dev->destructor = free_netdev;
438 sdata->dev->netdev_ops = &mac802154_monitor_ops;
Alexander Aring863e88f2014-11-09 08:36:45 +0100439 wpan_dev->promiscuous_mode = true;
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100440 break;
441 default:
442 BUG();
443 }
Alexander Aring986a8ab2014-11-05 20:51:15 +0100444
445 return 0;
446}
447
448struct net_device *
449ieee802154_if_add(struct ieee802154_local *local, const char *name,
450 struct wpan_dev **new_wpan_dev, int type)
451{
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100452 struct net_device *ndev = NULL;
453 struct ieee802154_sub_if_data *sdata = NULL;
454 int ret = -ENOMEM;
Alexander Aring986a8ab2014-11-05 20:51:15 +0100455
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100456 ASSERT_RTNL();
457
Alexander Aring7c118c12014-11-05 20:51:20 +0100458 ndev = alloc_netdev(sizeof(*sdata) + local->hw.vif_data_size, name,
459 NET_NAME_UNKNOWN, ieee802154_if_setup);
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100460 if (!ndev)
461 return ERR_PTR(-ENOMEM);
462
463 ndev->needed_headroom = local->hw.extra_tx_headroom;
464
465 ret = dev_alloc_name(ndev, ndev->name);
466 if (ret < 0)
Alexander Aring986a8ab2014-11-05 20:51:15 +0100467 goto err;
468
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100469 switch (type) {
470 case IEEE802154_DEV_WPAN:
471 ndev->type = ARPHRD_IEEE802154;
472 break;
473 case IEEE802154_DEV_MONITOR:
474 ndev->type = ARPHRD_IEEE802154_MONITOR;
475 break;
476 default:
477 ret = -EINVAL;
478 goto err;
479 }
Alexander Aring986a8ab2014-11-05 20:51:15 +0100480
Alexander Aringdee56d142014-11-05 20:51:25 +0100481 ieee802154_le64_to_be64(ndev->perm_addr,
482 &local->hw.phy->perm_extended_addr);
483 memcpy(ndev->dev_addr, ndev->perm_addr, IEEE802154_EXTENDED_ADDR_LEN);
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100484 /* TODO check this */
485 SET_NETDEV_DEV(ndev, &local->phy->dev);
486 sdata = netdev_priv(ndev);
487 ndev->ieee802154_ptr = &sdata->wpan_dev;
488 memcpy(sdata->name, ndev->name, IFNAMSIZ);
489 sdata->dev = ndev;
490 sdata->wpan_dev.wpan_phy = local->hw.phy;
491 sdata->local = local;
Alexander Aring986a8ab2014-11-05 20:51:15 +0100492
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100493 /* setup type-dependent data */
494 ret = ieee802154_setup_sdata(sdata, type);
495 if (ret)
496 goto err;
497
498 if (ndev) {
499 ret = register_netdevice(ndev);
500 if (ret < 0)
501 goto err;
502 }
503
504 mutex_lock(&local->iflist_mtx);
505 list_add_tail_rcu(&sdata->list, &local->interfaces);
506 mutex_unlock(&local->iflist_mtx);
507
508 if (new_wpan_dev)
509 *new_wpan_dev = &sdata->wpan_dev;
510
511 return ndev;
512
Alexander Aring986a8ab2014-11-05 20:51:15 +0100513err:
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100514 free_netdev(ndev);
515 return ERR_PTR(ret);
Alexander Aring986a8ab2014-11-05 20:51:15 +0100516}
517
Alexander Aringb210b182014-11-05 20:51:14 +0100518void ieee802154_if_remove(struct ieee802154_sub_if_data *sdata)
519{
520 ASSERT_RTNL();
521
522 mutex_lock(&sdata->local->iflist_mtx);
523 list_del_rcu(&sdata->list);
524 mutex_unlock(&sdata->local->iflist_mtx);
525
526 synchronize_rcu();
527 unregister_netdevice(sdata->dev);
528}