blob: 9ae893057dd761e4e82f563b9f24b4c59e6254bc [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
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +020033static int mac802154_wpan_update_llsec(struct net_device *dev)
34{
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
Alexander Aringf7cb96f2014-11-09 08:36:59 +010065 ASSERT_RTNL();
66
Alexander Aring036562f2014-10-25 17:16:36 +020067 spin_lock_bh(&sdata->mib_lock);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000068
69 switch (cmd) {
70 case SIOCGIFADDR:
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010071 {
72 u16 pan_id, short_addr;
73
Alexander Aring863e88f2014-11-09 08:36:45 +010074 pan_id = le16_to_cpu(wpan_dev->pan_id);
75 short_addr = le16_to_cpu(wpan_dev->short_addr);
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010076 if (pan_id == IEEE802154_PANID_BROADCAST ||
77 short_addr == IEEE802154_ADDR_BROADCAST) {
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000078 err = -EADDRNOTAVAIL;
79 break;
80 }
81
82 sa->family = AF_IEEE802154;
83 sa->addr.addr_type = IEEE802154_ADDR_SHORT;
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010084 sa->addr.pan_id = pan_id;
85 sa->addr.short_addr = short_addr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000086
87 err = 0;
88 break;
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010089 }
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000090 case SIOCSIFADDR:
Alexander Aringf7cb96f2014-11-09 08:36:59 +010091 if (netif_running(dev)) {
92 spin_unlock_bh(&sdata->mib_lock);
93 return -EBUSY;
94 }
95
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000096 dev_warn(&dev->dev,
Masanari Iida9b134942014-08-06 22:31:28 +090097 "Using DEBUGing ioctl SIOCSIFADDR isn't recommended!\n");
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000098 if (sa->family != AF_IEEE802154 ||
99 sa->addr.addr_type != IEEE802154_ADDR_SHORT ||
100 sa->addr.pan_id == IEEE802154_PANID_BROADCAST ||
101 sa->addr.short_addr == IEEE802154_ADDR_BROADCAST ||
102 sa->addr.short_addr == IEEE802154_ADDR_UNDEF) {
103 err = -EINVAL;
104 break;
105 }
106
Alexander Aring863e88f2014-11-09 08:36:45 +0100107 wpan_dev->pan_id = cpu_to_le16(sa->addr.pan_id);
108 wpan_dev->short_addr = cpu_to_le16(sa->addr.short_addr);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000109
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +0200110 err = mac802154_wpan_update_llsec(dev);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000111 break;
112 }
113
Alexander Aring036562f2014-10-25 17:16:36 +0200114 spin_unlock_bh(&sdata->mib_lock);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000115 return err;
116}
117
118static int mac802154_wpan_mac_addr(struct net_device *dev, void *p)
119{
Alexander Aring776e59d2014-11-02 04:18:41 +0100120 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000121 struct sockaddr *addr = p;
Alexander Aringea7053c2014-11-02 04:18:46 +0100122 __le64 extended_addr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000123
124 if (netif_running(dev))
125 return -EBUSY;
126
Alexander Aring705cbbb2014-11-05 20:51:24 +0100127 ieee802154_be64_to_le64(&extended_addr, addr->sa_data);
Alexander Aringea7053c2014-11-02 04:18:46 +0100128 if (!ieee802154_is_valid_extended_addr(extended_addr))
129 return -EINVAL;
130
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000131 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
Alexander Aring863e88f2014-11-09 08:36:45 +0100132 sdata->wpan_dev.extended_addr = extended_addr;
Alexander Aring776e59d2014-11-02 04:18:41 +0100133
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +0200134 return mac802154_wpan_update_llsec(dev);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000135}
136
Alexander Aring19ec6902014-10-28 18:21:17 +0100137static int mac802154_slave_open(struct net_device *dev)
138{
139 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
140 struct ieee802154_sub_if_data *subif;
141 struct ieee802154_local *local = sdata->local;
142 int res = 0;
143
144 ASSERT_RTNL();
145
Alexander Aring944742a2014-11-17 08:20:49 +0100146 if (sdata->vif.type == NL802154_IFTYPE_NODE) {
Alexander Aring19ec6902014-10-28 18:21:17 +0100147 mutex_lock(&sdata->local->iflist_mtx);
148 list_for_each_entry(subif, &sdata->local->interfaces, list) {
Alexander Aring7c118c12014-11-05 20:51:20 +0100149 if (subif != sdata &&
150 subif->vif.type == sdata->vif.type &&
Alexander Aring0ea3da62014-10-28 18:21:24 +0100151 ieee802154_sdata_running(subif)) {
Alexander Aring19ec6902014-10-28 18:21:17 +0100152 mutex_unlock(&sdata->local->iflist_mtx);
153 return -EBUSY;
154 }
155 }
156 mutex_unlock(&sdata->local->iflist_mtx);
157 }
158
Alexander Aring0ea3da62014-10-28 18:21:24 +0100159 set_bit(SDATA_STATE_RUNNING, &sdata->state);
Alexander Aring19ec6902014-10-28 18:21:17 +0100160
Alexander Aring74457642014-10-28 18:21:31 +0100161 if (!local->open_count) {
Alexander Aring59cb3002014-10-28 18:21:21 +0100162 res = drv_start(local);
Alexander Aring19ec6902014-10-28 18:21:17 +0100163 WARN_ON(res);
164 if (res)
165 goto err;
166 }
167
Alexander Aring74457642014-10-28 18:21:31 +0100168 local->open_count++;
Alexander Aring19ec6902014-10-28 18:21:17 +0100169 netif_start_queue(dev);
170 return 0;
171err:
Alexander Aring0ea3da62014-10-28 18:21:24 +0100172 /* might already be clear but that doesn't matter */
173 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
Alexander Aring19ec6902014-10-28 18:21:17 +0100174
175 return res;
176}
177
Phoebe Buckheister6ef00232014-05-14 17:43:11 +0200178static int mac802154_wpan_open(struct net_device *dev)
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200179{
180 int rc;
Alexander Aring59d19cd2014-10-25 17:16:40 +0200181 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Alexander Aring59cb3002014-10-28 18:21:21 +0100182 struct ieee802154_local *local = sdata->local;
Alexander Aring863e88f2014-11-09 08:36:45 +0100183 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
Alexander Aring04e850f2014-10-25 17:16:37 +0200184 struct wpan_phy *phy = sdata->local->phy;
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200185
186 rc = mac802154_slave_open(dev);
187 if (rc < 0)
188 return rc;
189
190 mutex_lock(&phy->pib_lock);
191
Alexander Aring38130c32014-10-29 21:34:44 +0100192 if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) {
Alexander Aring863e88f2014-11-09 08:36:45 +0100193 rc = drv_set_promiscuous_mode(local,
194 wpan_dev->promiscuous_mode);
Alexander Aring38130c32014-10-29 21:34:44 +0100195 if (rc < 0)
196 goto out;
197 }
198
Alexander Aring776e59d2014-11-02 04:18:41 +0100199 if (local->hw.flags & IEEE802154_HW_AFILT) {
Alexander Aring863e88f2014-11-09 08:36:45 +0100200 rc = drv_set_pan_id(local, wpan_dev->pan_id);
Alexander Aring50c79072014-11-02 04:18:43 +0100201 if (rc < 0)
202 goto out;
203
Alexander Aring863e88f2014-11-09 08:36:45 +0100204 rc = drv_set_extended_addr(local, wpan_dev->extended_addr);
Alexander Aring776e59d2014-11-02 04:18:41 +0100205 if (rc < 0)
206 goto out;
Alexander Aring78b4bad162014-11-02 04:18:42 +0100207
Alexander Aring863e88f2014-11-09 08:36:45 +0100208 rc = drv_set_short_addr(local, wpan_dev->short_addr);
Alexander Aring78b4bad162014-11-02 04:18:42 +0100209 if (rc < 0)
210 goto out;
Alexander Aring776e59d2014-11-02 04:18:41 +0100211 }
212
Alexander Aringa543c592014-10-28 18:21:23 +0100213 if (local->hw.flags & IEEE802154_HW_LBT) {
Alexander Aring5fb3f022014-11-09 08:36:46 +0100214 rc = drv_set_lbt_mode(local, wpan_dev->lbt);
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200215 if (rc < 0)
216 goto out;
217 }
218
Alexander Aringa543c592014-10-28 18:21:23 +0100219 if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) {
Alexander Aring5fb3f022014-11-09 08:36:46 +0100220 rc = drv_set_csma_params(local, wpan_dev->min_be,
221 wpan_dev->max_be,
222 wpan_dev->csma_retries);
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200223 if (rc < 0)
224 goto out;
225 }
226
Alexander Aringa543c592014-10-28 18:21:23 +0100227 if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) {
Alexander Aring5fb3f022014-11-09 08:36:46 +0100228 rc = drv_set_max_frame_retries(local, wpan_dev->frame_retries);
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200229 if (rc < 0)
230 goto out;
231 }
232
233 mutex_unlock(&phy->pib_lock);
234 return 0;
235
236out:
237 mutex_unlock(&phy->pib_lock);
238 return rc;
239}
240
Alexander Aring19ec6902014-10-28 18:21:17 +0100241static int mac802154_slave_close(struct net_device *dev)
242{
243 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
244 struct ieee802154_local *local = sdata->local;
245
246 ASSERT_RTNL();
247
Alexander Aring61f2dcb2014-11-12 19:51:56 +0100248 hrtimer_cancel(&local->ifs_timer);
249
Alexander Aring19ec6902014-10-28 18:21:17 +0100250 netif_stop_queue(dev);
Alexander Aring74457642014-10-28 18:21:31 +0100251 local->open_count--;
Alexander Aring19ec6902014-10-28 18:21:17 +0100252
Alexander Aring0ea3da62014-10-28 18:21:24 +0100253 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
Alexander Aring19ec6902014-10-28 18:21:17 +0100254
Alexander Aring74457642014-10-28 18:21:31 +0100255 if (!local->open_count)
Alexander Aring59cb3002014-10-28 18:21:21 +0100256 drv_stop(local);
Alexander Aring19ec6902014-10-28 18:21:17 +0100257
258 return 0;
259}
260
Alexander Aring036562f2014-10-25 17:16:36 +0200261static int mac802154_set_header_security(struct ieee802154_sub_if_data *sdata,
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200262 struct ieee802154_hdr *hdr,
263 const struct ieee802154_mac_cb *cb)
264{
265 struct ieee802154_llsec_params params;
266 u8 level;
267
Alexander Aring036562f2014-10-25 17:16:36 +0200268 mac802154_llsec_get_params(&sdata->sec, &params);
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200269
270 if (!params.enabled && cb->secen_override && cb->secen)
271 return -EINVAL;
272 if (!params.enabled ||
273 (cb->secen_override && !cb->secen) ||
274 !params.out_level)
275 return 0;
276 if (cb->seclevel_override && !cb->seclevel)
277 return -EINVAL;
278
279 level = cb->seclevel_override ? cb->seclevel : params.out_level;
280
281 hdr->fc.security_enabled = 1;
282 hdr->sec.level = level;
283 hdr->sec.key_id_mode = params.out_key.mode;
284 if (params.out_key.mode == IEEE802154_SCF_KEY_SHORT_INDEX)
285 hdr->sec.short_src = params.out_key.short_source;
286 else if (params.out_key.mode == IEEE802154_SCF_KEY_HW_INDEX)
287 hdr->sec.extended_src = params.out_key.extended_source;
288 hdr->sec.key_id = params.out_key.id;
289
290 return 0;
291}
292
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000293static int mac802154_header_create(struct sk_buff *skb,
294 struct net_device *dev,
295 unsigned short type,
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100296 const void *daddr,
297 const void *saddr,
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000298 unsigned len)
299{
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100300 struct ieee802154_hdr hdr;
Alexander Aring59d19cd2014-10-25 17:16:40 +0200301 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Alexander Aring863e88f2014-11-09 08:36:45 +0100302 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
Phoebe Buckheister32edc402014-05-14 17:43:08 +0200303 struct ieee802154_mac_cb *cb = mac_cb(skb);
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100304 int hlen;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000305
306 if (!daddr)
307 return -EINVAL;
308
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100309 memset(&hdr.fc, 0, sizeof(hdr.fc));
Phoebe Buckheister32edc402014-05-14 17:43:08 +0200310 hdr.fc.type = cb->type;
311 hdr.fc.security_enabled = cb->secen;
312 hdr.fc.ack_request = cb->ackreq;
313 hdr.seq = ieee802154_mlme_ops(dev)->get_dsn(dev);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000314
Alexander Aring036562f2014-10-25 17:16:36 +0200315 if (mac802154_set_header_security(sdata, &hdr, cb) < 0)
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200316 return -EINVAL;
317
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000318 if (!saddr) {
Alexander Aring036562f2014-10-25 17:16:36 +0200319 spin_lock_bh(&sdata->mib_lock);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000320
Alexander Aring863e88f2014-11-09 08:36:45 +0100321 if (wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) ||
322 wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
323 wpan_dev->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100324 hdr.source.mode = IEEE802154_ADDR_LONG;
Alexander Aring863e88f2014-11-09 08:36:45 +0100325 hdr.source.extended_addr = wpan_dev->extended_addr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000326 } else {
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100327 hdr.source.mode = IEEE802154_ADDR_SHORT;
Alexander Aring863e88f2014-11-09 08:36:45 +0100328 hdr.source.short_addr = wpan_dev->short_addr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000329 }
330
Alexander Aring863e88f2014-11-09 08:36:45 +0100331 hdr.source.pan_id = wpan_dev->pan_id;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000332
Alexander Aring036562f2014-10-25 17:16:36 +0200333 spin_unlock_bh(&sdata->mib_lock);
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100334 } else {
335 hdr.source = *(const struct ieee802154_addr *)saddr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000336 }
337
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100338 hdr.dest = *(const struct ieee802154_addr *)daddr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000339
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100340 hlen = ieee802154_hdr_push(skb, &hdr);
341 if (hlen < 0)
342 return -EINVAL;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000343
Alexander Aring3e691622013-10-30 09:18:22 +0100344 skb_reset_mac_header(skb);
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100345 skb->mac_len = hlen;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000346
Phoebe Buckheister8c842962014-05-14 17:43:07 +0200347 if (len > ieee802154_max_payload(&hdr))
Phoebe Buckheisterd1d73582014-03-14 21:24:04 +0100348 return -EMSGSIZE;
349
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100350 return hlen;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000351}
352
353static int
354mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
355{
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100356 struct ieee802154_hdr hdr;
357 struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000358
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100359 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
360 pr_debug("malformed packet\n");
361 return 0;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000362 }
363
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100364 *addr = hdr.source;
365 return sizeof(*addr);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000366}
367
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000368static struct header_ops mac802154_header_ops = {
369 .create = mac802154_header_create,
370 .parse = mac802154_header_parse,
371};
372
373static const struct net_device_ops mac802154_wpan_ops = {
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200374 .ndo_open = mac802154_wpan_open,
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000375 .ndo_stop = mac802154_slave_close,
Alexander Aringe5e584f2014-10-26 09:37:13 +0100376 .ndo_start_xmit = ieee802154_subif_start_xmit,
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000377 .ndo_do_ioctl = mac802154_wpan_ioctl,
378 .ndo_set_mac_address = mac802154_wpan_mac_addr,
379};
380
Alexander Aringb9ff77e2014-10-28 18:21:16 +0100381static const struct net_device_ops mac802154_monitor_ops = {
Alexander Aring38130c32014-10-29 21:34:44 +0100382 .ndo_open = mac802154_wpan_open,
Alexander Aringb9ff77e2014-10-28 18:21:16 +0100383 .ndo_stop = mac802154_slave_close,
384 .ndo_start_xmit = ieee802154_monitor_start_xmit,
385};
386
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200387static void mac802154_wpan_free(struct net_device *dev)
388{
Alexander Aring59d19cd2014-10-25 17:16:40 +0200389 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200390
Alexander Aring036562f2014-10-25 17:16:36 +0200391 mac802154_llsec_destroy(&sdata->sec);
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200392
393 free_netdev(dev);
394}
395
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100396static void ieee802154_if_setup(struct net_device *dev)
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000397{
Alexander Aringe57a8942014-11-05 20:51:28 +0100398 dev->addr_len = IEEE802154_EXTENDED_ADDR_LEN;
399 memset(dev->broadcast, 0xff, IEEE802154_EXTENDED_ADDR_LEN);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000400
401 dev->hard_header_len = MAC802154_FRAME_HARD_HEADER_LEN;
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200402 dev->needed_tailroom = 2 + 16; /* FCS + MIC */
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000403 dev->mtu = IEEE802154_MTU;
Alan Otte937f5832013-04-03 04:00:57 +0000404 dev->tx_queue_len = 300;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000405 dev->flags = IFF_NOARP | IFF_BROADCAST;
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100406}
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000407
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100408static int
Alexander Aring944742a2014-11-17 08:20:49 +0100409ieee802154_setup_sdata(struct ieee802154_sub_if_data *sdata,
410 enum nl802154_iftype type)
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100411{
Alexander Aring863e88f2014-11-09 08:36:45 +0100412 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
413
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100414 /* set some type-dependent values */
Alexander Aring7c118c12014-11-05 20:51:20 +0100415 sdata->vif.type = type;
Alexander Aring190ac1c2014-11-09 08:36:49 +0100416 sdata->wpan_dev.iftype = type;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000417
Alexander Aring863e88f2014-11-09 08:36:45 +0100418 get_random_bytes(&wpan_dev->bsn, 1);
419 get_random_bytes(&wpan_dev->dsn, 1);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000420
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200421 /* defaults per 802.15.4-2011 */
Alexander Aring5fb3f022014-11-09 08:36:46 +0100422 wpan_dev->min_be = 3;
423 wpan_dev->max_be = 5;
424 wpan_dev->csma_retries = 4;
Alexander Aring036562f2014-10-25 17:16:36 +0200425 /* for compatibility, actual default is 3 */
Alexander Aring5fb3f022014-11-09 08:36:46 +0100426 wpan_dev->frame_retries = -1;
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200427
Alexander Aring863e88f2014-11-09 08:36:45 +0100428 wpan_dev->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
429 wpan_dev->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200430
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100431 switch (type) {
Alexander Aring944742a2014-11-17 08:20:49 +0100432 case NL802154_IFTYPE_NODE:
Alexander Aringb03c9cc2014-11-09 08:36:56 +0100433 ieee802154_be64_to_le64(&wpan_dev->extended_addr,
434 sdata->dev->dev_addr);
435
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100436 sdata->dev->header_ops = &mac802154_header_ops;
437 sdata->dev->destructor = mac802154_wpan_free;
438 sdata->dev->netdev_ops = &mac802154_wpan_ops;
439 sdata->dev->ml_priv = &mac802154_mlme_wpan;
Alexander Aring863e88f2014-11-09 08:36:45 +0100440 wpan_dev->promiscuous_mode = false;
Alexander Aring38130c32014-10-29 21:34:44 +0100441
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100442 spin_lock_init(&sdata->mib_lock);
443 mutex_init(&sdata->sec_mtx);
Alexander Aringb9ff77e2014-10-28 18:21:16 +0100444
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100445 mac802154_llsec_init(&sdata->sec);
446 break;
Alexander Aring944742a2014-11-17 08:20:49 +0100447 case NL802154_IFTYPE_MONITOR:
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100448 sdata->dev->destructor = free_netdev;
449 sdata->dev->netdev_ops = &mac802154_monitor_ops;
Alexander Aring863e88f2014-11-09 08:36:45 +0100450 wpan_dev->promiscuous_mode = true;
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100451 break;
452 default:
453 BUG();
454 }
Alexander Aring986a8ab2014-11-05 20:51:15 +0100455
456 return 0;
457}
458
459struct net_device *
460ieee802154_if_add(struct ieee802154_local *local, const char *name,
Alexander Aring0e575472014-11-17 08:20:52 +0100461 enum nl802154_iftype type, __le64 extended_addr)
Alexander Aring986a8ab2014-11-05 20:51:15 +0100462{
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100463 struct net_device *ndev = NULL;
464 struct ieee802154_sub_if_data *sdata = NULL;
465 int ret = -ENOMEM;
Alexander Aring986a8ab2014-11-05 20:51:15 +0100466
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100467 ASSERT_RTNL();
468
Alexander Aring7c118c12014-11-05 20:51:20 +0100469 ndev = alloc_netdev(sizeof(*sdata) + local->hw.vif_data_size, name,
470 NET_NAME_UNKNOWN, ieee802154_if_setup);
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100471 if (!ndev)
472 return ERR_PTR(-ENOMEM);
473
474 ndev->needed_headroom = local->hw.extra_tx_headroom;
475
476 ret = dev_alloc_name(ndev, ndev->name);
477 if (ret < 0)
Alexander Aring986a8ab2014-11-05 20:51:15 +0100478 goto err;
479
Alexander Aring0e575472014-11-17 08:20:52 +0100480 ieee802154_le64_to_be64(ndev->perm_addr,
481 &local->hw.phy->perm_extended_addr);
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100482 switch (type) {
Alexander Aring944742a2014-11-17 08:20:49 +0100483 case NL802154_IFTYPE_NODE:
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100484 ndev->type = ARPHRD_IEEE802154;
Alexander Aring0e575472014-11-17 08:20:52 +0100485 if (ieee802154_is_valid_extended_addr(extended_addr))
486 ieee802154_le64_to_be64(ndev->dev_addr, &extended_addr);
487 else
488 memcpy(ndev->dev_addr, ndev->perm_addr,
489 IEEE802154_EXTENDED_ADDR_LEN);
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100490 break;
Alexander Aring944742a2014-11-17 08:20:49 +0100491 case NL802154_IFTYPE_MONITOR:
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100492 ndev->type = ARPHRD_IEEE802154_MONITOR;
493 break;
494 default:
495 ret = -EINVAL;
496 goto err;
497 }
Alexander Aring986a8ab2014-11-05 20:51:15 +0100498
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100499 /* TODO check this */
500 SET_NETDEV_DEV(ndev, &local->phy->dev);
501 sdata = netdev_priv(ndev);
502 ndev->ieee802154_ptr = &sdata->wpan_dev;
503 memcpy(sdata->name, ndev->name, IFNAMSIZ);
504 sdata->dev = ndev;
505 sdata->wpan_dev.wpan_phy = local->hw.phy;
506 sdata->local = local;
Alexander Aring986a8ab2014-11-05 20:51:15 +0100507
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100508 /* setup type-dependent data */
509 ret = ieee802154_setup_sdata(sdata, type);
510 if (ret)
511 goto err;
512
Varka Bhadram473f3762014-11-25 16:34:43 +0530513 ret = register_netdevice(ndev);
514 if (ret < 0)
515 goto err;
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100516
517 mutex_lock(&local->iflist_mtx);
518 list_add_tail_rcu(&sdata->list, &local->interfaces);
519 mutex_unlock(&local->iflist_mtx);
520
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100521 return ndev;
522
Alexander Aring986a8ab2014-11-05 20:51:15 +0100523err:
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100524 free_netdev(ndev);
525 return ERR_PTR(ret);
Alexander Aring986a8ab2014-11-05 20:51:15 +0100526}
527
Alexander Aringb210b182014-11-05 20:51:14 +0100528void ieee802154_if_remove(struct ieee802154_sub_if_data *sdata)
529{
530 ASSERT_RTNL();
531
532 mutex_lock(&sdata->local->iflist_mtx);
533 list_del_rcu(&sdata->list);
534 mutex_unlock(&sdata->local->iflist_mtx);
535
536 synchronize_rcu();
537 unregister_netdevice(sdata->dev);
538}
Alexander Aring592dfbfc2014-11-12 03:36:48 +0100539
540void ieee802154_remove_interfaces(struct ieee802154_local *local)
541{
Alexander Aringd14e1c72014-11-12 03:36:49 +0100542 struct ieee802154_sub_if_data *sdata, *tmp;
Alexander Aring592dfbfc2014-11-12 03:36:48 +0100543
Alexander Aring2789e622014-11-12 03:36:50 +0100544 mutex_lock(&local->iflist_mtx);
Alexander Aringd14e1c72014-11-12 03:36:49 +0100545 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
Alexander Aring592dfbfc2014-11-12 03:36:48 +0100546 list_del(&sdata->list);
Alexander Aring592dfbfc2014-11-12 03:36:48 +0100547
548 unregister_netdevice(sdata->dev);
549 }
Alexander Aring2789e622014-11-12 03:36:50 +0100550 mutex_unlock(&local->iflist_mtx);
Alexander Aring592dfbfc2014-11-12 03:36:48 +0100551}
Alexander Aringbe4fd8e2014-11-12 03:36:53 +0100552
553static int netdev_notify(struct notifier_block *nb,
554 unsigned long state, void *ptr)
555{
556 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
557 struct ieee802154_sub_if_data *sdata;
558
559 if (state != NETDEV_CHANGENAME)
560 return NOTIFY_DONE;
561
562 if (!dev->ieee802154_ptr || !dev->ieee802154_ptr->wpan_phy)
563 return NOTIFY_DONE;
564
565 if (dev->ieee802154_ptr->wpan_phy->privid != mac802154_wpan_phy_privid)
566 return NOTIFY_DONE;
567
568 sdata = IEEE802154_DEV_TO_SUB_IF(dev);
569 memcpy(sdata->name, dev->name, IFNAMSIZ);
570
571 return NOTIFY_OK;
572}
573
574static struct notifier_block mac802154_netdev_notifier = {
575 .notifier_call = netdev_notify,
576};
577
578int ieee802154_iface_init(void)
579{
580 return register_netdevice_notifier(&mac802154_netdev_notifier);
581}
582
583void ieee802154_iface_exit(void)
584{
585 unregister_netdevice_notifier(&mac802154_netdev_notifier);
586}