blob: 1cf5ac09edcbcdee6bf8461a06836f4ae219036d [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +00002/*
3 * Copyright 2007-2012 Siemens AG
4 *
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +00005 * Written by:
6 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
7 * Sergey Lapin <slapin@ossfans.org>
8 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
9 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
10 */
11
12#include <linux/netdevice.h>
13#include <linux/module.h>
14#include <linux/if_arp.h>
Alexander Aring4ca24ac2014-10-25 09:41:04 +020015#include <linux/ieee802154.h>
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000016
Alexander Aring944742a2014-11-17 08:20:49 +010017#include <net/nl802154.h>
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000018#include <net/mac802154.h>
19#include <net/ieee802154_netdev.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020020#include <net/cfg802154.h>
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000021
Alexander Aring0f1556b2014-10-25 09:41:00 +020022#include "ieee802154_i.h"
Alexander Aring59cb3002014-10-28 18:21:21 +010023#include "driver-ops.h"
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000024
Alexander Aringd77b4852015-06-21 16:45:20 +020025int mac802154_wpan_update_llsec(struct net_device *dev)
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +020026{
Alexander Aring59d19cd2014-10-25 17:16:40 +020027 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +020028 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev);
Alexander Aring863e88f2014-11-09 08:36:45 +010029 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +020030 int rc = 0;
31
32 if (ops->llsec) {
33 struct ieee802154_llsec_params params;
34 int changed = 0;
35
Alexander Aring863e88f2014-11-09 08:36:45 +010036 params.pan_id = wpan_dev->pan_id;
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +020037 changed |= IEEE802154_LLSEC_PARAM_PAN_ID;
38
Alexander Aring863e88f2014-11-09 08:36:45 +010039 params.hwaddr = wpan_dev->extended_addr;
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +020040 changed |= IEEE802154_LLSEC_PARAM_HWADDR;
41
42 rc = ops->llsec->set_params(dev, &params, changed);
43 }
44
45 return rc;
46}
47
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000048static int
49mac802154_wpan_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
50{
Alexander Aring59d19cd2014-10-25 17:16:40 +020051 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Alexander Aring863e88f2014-11-09 08:36:45 +010052 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000053 struct sockaddr_ieee802154 *sa =
54 (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
55 int err = -ENOIOCTLCMD;
56
Lennert Buytenheke34fd872015-05-26 15:06:10 +030057 if (cmd != SIOCGIFADDR && cmd != SIOCSIFADDR)
58 return err;
59
Alexander Aring4a669f72015-05-22 17:43:51 +020060 rtnl_lock();
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000061
62 switch (cmd) {
63 case SIOCGIFADDR:
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010064 {
65 u16 pan_id, short_addr;
66
Alexander Aring863e88f2014-11-09 08:36:45 +010067 pan_id = le16_to_cpu(wpan_dev->pan_id);
68 short_addr = le16_to_cpu(wpan_dev->short_addr);
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010069 if (pan_id == IEEE802154_PANID_BROADCAST ||
70 short_addr == IEEE802154_ADDR_BROADCAST) {
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000071 err = -EADDRNOTAVAIL;
72 break;
73 }
74
75 sa->family = AF_IEEE802154;
76 sa->addr.addr_type = IEEE802154_ADDR_SHORT;
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010077 sa->addr.pan_id = pan_id;
78 sa->addr.short_addr = short_addr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000079
80 err = 0;
81 break;
Phoebe Buckheisterb70ab2e2014-03-14 21:23:59 +010082 }
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000083 case SIOCSIFADDR:
Alexander Aringf7cb96f2014-11-09 08:36:59 +010084 if (netif_running(dev)) {
Alexander Aring4a669f72015-05-22 17:43:51 +020085 rtnl_unlock();
Alexander Aringf7cb96f2014-11-09 08:36:59 +010086 return -EBUSY;
87 }
88
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000089 dev_warn(&dev->dev,
Masanari Iida9b134942014-08-06 22:31:28 +090090 "Using DEBUGing ioctl SIOCSIFADDR isn't recommended!\n");
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +000091 if (sa->family != AF_IEEE802154 ||
92 sa->addr.addr_type != IEEE802154_ADDR_SHORT ||
93 sa->addr.pan_id == IEEE802154_PANID_BROADCAST ||
94 sa->addr.short_addr == IEEE802154_ADDR_BROADCAST ||
95 sa->addr.short_addr == IEEE802154_ADDR_UNDEF) {
96 err = -EINVAL;
97 break;
98 }
99
Alexander Aring863e88f2014-11-09 08:36:45 +0100100 wpan_dev->pan_id = cpu_to_le16(sa->addr.pan_id);
101 wpan_dev->short_addr = cpu_to_le16(sa->addr.short_addr);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000102
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +0200103 err = mac802154_wpan_update_llsec(dev);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000104 break;
105 }
106
Alexander Aring4a669f72015-05-22 17:43:51 +0200107 rtnl_unlock();
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000108 return err;
109}
110
111static int mac802154_wpan_mac_addr(struct net_device *dev, void *p)
112{
Alexander Aring776e59d2014-11-02 04:18:41 +0100113 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000114 struct sockaddr *addr = p;
Alexander Aringea7053c2014-11-02 04:18:46 +0100115 __le64 extended_addr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000116
117 if (netif_running(dev))
118 return -EBUSY;
119
Alexander Aring09095fd2015-08-10 21:15:54 +0200120 /* lowpan need to be down for update
121 * SLAAC address after ifup
122 */
123 if (sdata->wpan_dev.lowpan_dev) {
124 if (netif_running(sdata->wpan_dev.lowpan_dev))
125 return -EBUSY;
126 }
127
Alexander Aring705cbbb2014-11-05 20:51:24 +0100128 ieee802154_be64_to_le64(&extended_addr, addr->sa_data);
Lennert Buytenhekdaf4e2c2015-05-28 15:38:43 +0300129 if (!ieee802154_is_valid_extended_unicast_addr(extended_addr))
Alexander Aringea7053c2014-11-02 04:18:46 +0100130 return -EINVAL;
131
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000132 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
Alexander Aring863e88f2014-11-09 08:36:45 +0100133 sdata->wpan_dev.extended_addr = extended_addr;
Alexander Aring776e59d2014-11-02 04:18:41 +0100134
Alexander Aring09095fd2015-08-10 21:15:54 +0200135 /* update lowpan interface mac address when
136 * wpan mac has been changed
137 */
138 if (sdata->wpan_dev.lowpan_dev)
139 memcpy(sdata->wpan_dev.lowpan_dev->dev_addr, dev->dev_addr,
140 dev->addr_len);
141
Phoebe Buckheister9b0bb4a2014-05-16 17:46:43 +0200142 return mac802154_wpan_update_llsec(dev);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000143}
144
Alexander Aring95c0aa12015-06-17 15:35:17 +0200145static int ieee802154_setup_hw(struct ieee802154_sub_if_data *sdata)
146{
147 struct ieee802154_local *local = sdata->local;
148 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
149 int ret;
150
151 if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) {
152 ret = drv_set_promiscuous_mode(local,
153 wpan_dev->promiscuous_mode);
154 if (ret < 0)
155 return ret;
156 }
157
158 if (local->hw.flags & IEEE802154_HW_AFILT) {
159 ret = drv_set_pan_id(local, wpan_dev->pan_id);
160 if (ret < 0)
161 return ret;
162
163 ret = drv_set_extended_addr(local, wpan_dev->extended_addr);
164 if (ret < 0)
165 return ret;
166
167 ret = drv_set_short_addr(local, wpan_dev->short_addr);
168 if (ret < 0)
169 return ret;
170 }
171
172 if (local->hw.flags & IEEE802154_HW_LBT) {
173 ret = drv_set_lbt_mode(local, wpan_dev->lbt);
174 if (ret < 0)
175 return ret;
176 }
177
178 if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) {
179 ret = drv_set_csma_params(local, wpan_dev->min_be,
180 wpan_dev->max_be,
181 wpan_dev->csma_retries);
182 if (ret < 0)
183 return ret;
184 }
185
186 if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) {
187 ret = drv_set_max_frame_retries(local, wpan_dev->frame_retries);
188 if (ret < 0)
189 return ret;
190 }
191
192 return 0;
193}
194
Alexander Aring19ec6902014-10-28 18:21:17 +0100195static int mac802154_slave_open(struct net_device *dev)
196{
197 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Alexander Aring19ec6902014-10-28 18:21:17 +0100198 struct ieee802154_local *local = sdata->local;
Alexander Aring5c698e82015-06-17 15:35:18 +0200199 int res;
Alexander Aring19ec6902014-10-28 18:21:17 +0100200
201 ASSERT_RTNL();
202
Alexander Aring0ea3da62014-10-28 18:21:24 +0100203 set_bit(SDATA_STATE_RUNNING, &sdata->state);
Alexander Aring19ec6902014-10-28 18:21:17 +0100204
Alexander Aring74457642014-10-28 18:21:31 +0100205 if (!local->open_count) {
Alexander Aring95c0aa12015-06-17 15:35:17 +0200206 res = ieee802154_setup_hw(sdata);
207 if (res)
208 goto err;
209
Alexander Aring59cb3002014-10-28 18:21:21 +0100210 res = drv_start(local);
Alexander Aring19ec6902014-10-28 18:21:17 +0100211 if (res)
212 goto err;
213 }
214
Alexander Aring74457642014-10-28 18:21:31 +0100215 local->open_count++;
Alexander Aring19ec6902014-10-28 18:21:17 +0100216 netif_start_queue(dev);
217 return 0;
218err:
Alexander Aring0ea3da62014-10-28 18:21:24 +0100219 /* might already be clear but that doesn't matter */
220 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
Alexander Aring19ec6902014-10-28 18:21:17 +0100221
222 return res;
223}
224
Alexander Aringbd37a782014-12-19 23:45:58 +0100225static int
226ieee802154_check_mac_settings(struct ieee802154_local *local,
227 struct wpan_dev *wpan_dev,
228 struct wpan_dev *nwpan_dev)
229{
230 ASSERT_RTNL();
231
232 if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) {
233 if (wpan_dev->promiscuous_mode != nwpan_dev->promiscuous_mode)
234 return -EBUSY;
235 }
236
237 if (local->hw.flags & IEEE802154_HW_AFILT) {
Alexander Aring8bf95382015-03-26 12:46:28 +0100238 if (wpan_dev->pan_id != nwpan_dev->pan_id ||
239 wpan_dev->short_addr != nwpan_dev->short_addr ||
240 wpan_dev->extended_addr != nwpan_dev->extended_addr)
Alexander Aringbd37a782014-12-19 23:45:58 +0100241 return -EBUSY;
242 }
243
244 if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) {
Alexander Aring8bf95382015-03-26 12:46:28 +0100245 if (wpan_dev->min_be != nwpan_dev->min_be ||
246 wpan_dev->max_be != nwpan_dev->max_be ||
247 wpan_dev->csma_retries != nwpan_dev->csma_retries)
Alexander Aringbd37a782014-12-19 23:45:58 +0100248 return -EBUSY;
249 }
250
251 if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) {
252 if (wpan_dev->frame_retries != nwpan_dev->frame_retries)
253 return -EBUSY;
254 }
255
256 if (local->hw.flags & IEEE802154_HW_LBT) {
257 if (wpan_dev->lbt != nwpan_dev->lbt)
258 return -EBUSY;
259 }
260
261 return 0;
262}
263
264static int
265ieee802154_check_concurrent_iface(struct ieee802154_sub_if_data *sdata,
266 enum nl802154_iftype iftype)
267{
268 struct ieee802154_local *local = sdata->local;
269 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
270 struct ieee802154_sub_if_data *nsdata;
271
272 /* we hold the RTNL here so can safely walk the list */
273 list_for_each_entry(nsdata, &local->interfaces, list) {
274 if (nsdata != sdata && ieee802154_sdata_running(nsdata)) {
275 int ret;
276
Alexander Aringcd1c5662014-12-19 23:45:59 +0100277 /* TODO currently we don't support multiple node types
278 * we need to run skb_clone at rx path. Check if there
279 * exist really an use case if we need to support
280 * multiple node types at the same time.
281 */
Alexander Aringed659632015-06-06 17:30:46 +0200282 if (wpan_dev->iftype == NL802154_IFTYPE_NODE &&
283 nsdata->wpan_dev.iftype == NL802154_IFTYPE_NODE)
Alexander Aringcd1c5662014-12-19 23:45:59 +0100284 return -EBUSY;
285
Alexander Aringbd37a782014-12-19 23:45:58 +0100286 /* check all phy mac sublayer settings are the same.
287 * We have only one phy, different values makes trouble.
288 */
289 ret = ieee802154_check_mac_settings(local, wpan_dev,
290 &nsdata->wpan_dev);
291 if (ret < 0)
292 return ret;
293 }
294 }
295
296 return 0;
297}
298
Phoebe Buckheister6ef00232014-05-14 17:43:11 +0200299static int mac802154_wpan_open(struct net_device *dev)
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200300{
301 int rc;
Alexander Aring59d19cd2014-10-25 17:16:40 +0200302 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Alexander Aring863e88f2014-11-09 08:36:45 +0100303 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200304
Alexander Aringed659632015-06-06 17:30:46 +0200305 rc = ieee802154_check_concurrent_iface(sdata, wpan_dev->iftype);
Alexander Aringbd37a782014-12-19 23:45:58 +0100306 if (rc < 0)
307 return rc;
308
Alexander Aring95c0aa12015-06-17 15:35:17 +0200309 return mac802154_slave_open(dev);
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200310}
311
Alexander Aring19ec6902014-10-28 18:21:17 +0100312static int mac802154_slave_close(struct net_device *dev)
313{
314 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
315 struct ieee802154_local *local = sdata->local;
316
317 ASSERT_RTNL();
318
319 netif_stop_queue(dev);
Alexander Aring74457642014-10-28 18:21:31 +0100320 local->open_count--;
Alexander Aring19ec6902014-10-28 18:21:17 +0100321
Alexander Aring0ea3da62014-10-28 18:21:24 +0100322 clear_bit(SDATA_STATE_RUNNING, &sdata->state);
Alexander Aring19ec6902014-10-28 18:21:17 +0100323
Alexander Aringc4227c82015-06-24 11:36:34 +0200324 if (!local->open_count)
325 ieee802154_stop_device(local);
Alexander Aring19ec6902014-10-28 18:21:17 +0100326
327 return 0;
328}
329
Alexander Aring036562f2014-10-25 17:16:36 +0200330static int mac802154_set_header_security(struct ieee802154_sub_if_data *sdata,
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200331 struct ieee802154_hdr *hdr,
332 const struct ieee802154_mac_cb *cb)
333{
334 struct ieee802154_llsec_params params;
335 u8 level;
336
Alexander Aring036562f2014-10-25 17:16:36 +0200337 mac802154_llsec_get_params(&sdata->sec, &params);
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200338
339 if (!params.enabled && cb->secen_override && cb->secen)
340 return -EINVAL;
341 if (!params.enabled ||
342 (cb->secen_override && !cb->secen) ||
343 !params.out_level)
344 return 0;
345 if (cb->seclevel_override && !cb->seclevel)
346 return -EINVAL;
347
348 level = cb->seclevel_override ? cb->seclevel : params.out_level;
349
350 hdr->fc.security_enabled = 1;
351 hdr->sec.level = level;
352 hdr->sec.key_id_mode = params.out_key.mode;
353 if (params.out_key.mode == IEEE802154_SCF_KEY_SHORT_INDEX)
354 hdr->sec.short_src = params.out_key.short_source;
355 else if (params.out_key.mode == IEEE802154_SCF_KEY_HW_INDEX)
356 hdr->sec.extended_src = params.out_key.extended_source;
357 hdr->sec.key_id = params.out_key.id;
358
359 return 0;
360}
361
Alexander Aring838b83d2015-09-18 11:30:42 +0200362static int ieee802154_header_create(struct sk_buff *skb,
363 struct net_device *dev,
364 const struct ieee802154_addr *daddr,
365 const struct ieee802154_addr *saddr,
366 unsigned len)
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000367{
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100368 struct ieee802154_hdr hdr;
Alexander Aring59d19cd2014-10-25 17:16:40 +0200369 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Alexander Aring863e88f2014-11-09 08:36:45 +0100370 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
Phoebe Buckheister32edc402014-05-14 17:43:08 +0200371 struct ieee802154_mac_cb *cb = mac_cb(skb);
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100372 int hlen;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000373
374 if (!daddr)
375 return -EINVAL;
376
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100377 memset(&hdr.fc, 0, sizeof(hdr.fc));
Phoebe Buckheister32edc402014-05-14 17:43:08 +0200378 hdr.fc.type = cb->type;
379 hdr.fc.security_enabled = cb->secen;
380 hdr.fc.ack_request = cb->ackreq;
Alexander Aring344f8c12015-05-22 17:43:53 +0200381 hdr.seq = atomic_inc_return(&dev->ieee802154_ptr->dsn) & 0xFF;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000382
Alexander Aring036562f2014-10-25 17:16:36 +0200383 if (mac802154_set_header_security(sdata, &hdr, cb) < 0)
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200384 return -EINVAL;
385
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000386 if (!saddr) {
Alexander Aring863e88f2014-11-09 08:36:45 +0100387 if (wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) ||
388 wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) ||
389 wpan_dev->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) {
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100390 hdr.source.mode = IEEE802154_ADDR_LONG;
Alexander Aring863e88f2014-11-09 08:36:45 +0100391 hdr.source.extended_addr = wpan_dev->extended_addr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000392 } else {
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100393 hdr.source.mode = IEEE802154_ADDR_SHORT;
Alexander Aring863e88f2014-11-09 08:36:45 +0100394 hdr.source.short_addr = wpan_dev->short_addr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000395 }
396
Alexander Aring863e88f2014-11-09 08:36:45 +0100397 hdr.source.pan_id = wpan_dev->pan_id;
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100398 } else {
399 hdr.source = *(const struct ieee802154_addr *)saddr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000400 }
401
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100402 hdr.dest = *(const struct ieee802154_addr *)daddr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000403
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100404 hlen = ieee802154_hdr_push(skb, &hdr);
405 if (hlen < 0)
406 return -EINVAL;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000407
Alexander Aring3e691622013-10-30 09:18:22 +0100408 skb_reset_mac_header(skb);
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100409 skb->mac_len = hlen;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000410
Phoebe Buckheister8c842962014-05-14 17:43:07 +0200411 if (len > ieee802154_max_payload(&hdr))
Phoebe Buckheisterd1d73582014-03-14 21:24:04 +0100412 return -EMSGSIZE;
413
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100414 return hlen;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000415}
416
Alexander Aring838b83d2015-09-18 11:30:42 +0200417static const struct wpan_dev_header_ops ieee802154_header_ops = {
418 .create = ieee802154_header_create,
419};
420
421/* This header create functionality assumes a 8 byte array for
422 * source and destination pointer at maximum. To adapt this for
423 * the 802.15.4 dataframe header we use extended address handling
424 * here only and intra pan connection. fc fields are mostly fallback
425 * handling. For provide dev_hard_header for dgram sockets.
426 */
427static int mac802154_header_create(struct sk_buff *skb,
428 struct net_device *dev,
429 unsigned short type,
430 const void *daddr,
431 const void *saddr,
432 unsigned len)
433{
434 struct ieee802154_hdr hdr;
435 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
436 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
437 struct ieee802154_mac_cb cb = { };
438 int hlen;
439
440 if (!daddr)
441 return -EINVAL;
442
443 memset(&hdr.fc, 0, sizeof(hdr.fc));
444 hdr.fc.type = IEEE802154_FC_TYPE_DATA;
445 hdr.fc.ack_request = wpan_dev->ackreq;
446 hdr.seq = atomic_inc_return(&dev->ieee802154_ptr->dsn) & 0xFF;
447
448 /* TODO currently a workaround to give zero cb block to set
449 * security parameters defaults according MIB.
450 */
451 if (mac802154_set_header_security(sdata, &hdr, &cb) < 0)
452 return -EINVAL;
453
454 hdr.dest.pan_id = wpan_dev->pan_id;
455 hdr.dest.mode = IEEE802154_ADDR_LONG;
Alexander Aringc2d5ecf2015-09-24 09:37:11 +0200456 ieee802154_be64_to_le64(&hdr.dest.extended_addr, daddr);
Alexander Aring838b83d2015-09-18 11:30:42 +0200457
458 hdr.source.pan_id = hdr.dest.pan_id;
459 hdr.source.mode = IEEE802154_ADDR_LONG;
460
461 if (!saddr)
462 hdr.source.extended_addr = wpan_dev->extended_addr;
463 else
Alexander Aringc2d5ecf2015-09-24 09:37:11 +0200464 ieee802154_be64_to_le64(&hdr.source.extended_addr, saddr);
Alexander Aring838b83d2015-09-18 11:30:42 +0200465
466 hlen = ieee802154_hdr_push(skb, &hdr);
467 if (hlen < 0)
468 return -EINVAL;
469
470 skb_reset_mac_header(skb);
471 skb->mac_len = hlen;
472
473 if (len > ieee802154_max_payload(&hdr))
474 return -EMSGSIZE;
475
476 return hlen;
477}
478
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000479static int
480mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
481{
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100482 struct ieee802154_hdr hdr;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000483
Phoebe Buckheistere6278d92014-03-14 21:24:01 +0100484 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
485 pr_debug("malformed packet\n");
486 return 0;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000487 }
488
Alexander Aring838b83d2015-09-18 11:30:42 +0200489 if (hdr.source.mode == IEEE802154_ADDR_LONG) {
Alexander Aringc2d5ecf2015-09-24 09:37:11 +0200490 ieee802154_le64_to_be64(haddr, &hdr.source.extended_addr);
Alexander Aring838b83d2015-09-18 11:30:42 +0200491 return IEEE802154_EXTENDED_ADDR_LEN;
492 }
493
494 return 0;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000495}
496
Alexander Aring838b83d2015-09-18 11:30:42 +0200497static const struct header_ops mac802154_header_ops = {
498 .create = mac802154_header_create,
499 .parse = mac802154_header_parse,
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000500};
501
502static const struct net_device_ops mac802154_wpan_ops = {
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200503 .ndo_open = mac802154_wpan_open,
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000504 .ndo_stop = mac802154_slave_close,
Alexander Aringe5e584f2014-10-26 09:37:13 +0100505 .ndo_start_xmit = ieee802154_subif_start_xmit,
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000506 .ndo_do_ioctl = mac802154_wpan_ioctl,
507 .ndo_set_mac_address = mac802154_wpan_mac_addr,
508};
509
Alexander Aringb9ff77e2014-10-28 18:21:16 +0100510static const struct net_device_ops mac802154_monitor_ops = {
Alexander Aring38130c32014-10-29 21:34:44 +0100511 .ndo_open = mac802154_wpan_open,
Alexander Aringb9ff77e2014-10-28 18:21:16 +0100512 .ndo_stop = mac802154_slave_close,
513 .ndo_start_xmit = ieee802154_monitor_start_xmit,
514};
515
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200516static void mac802154_wpan_free(struct net_device *dev)
517{
Alexander Aring59d19cd2014-10-25 17:16:40 +0200518 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200519
Alexander Aring036562f2014-10-25 17:16:36 +0200520 mac802154_llsec_destroy(&sdata->sec);
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200521}
522
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100523static void ieee802154_if_setup(struct net_device *dev)
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000524{
Alexander Aringe57a8942014-11-05 20:51:28 +0100525 dev->addr_len = IEEE802154_EXTENDED_ADDR_LEN;
526 memset(dev->broadcast, 0xff, IEEE802154_EXTENDED_ADDR_LEN);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000527
Alexander Aring87a93e42015-09-18 11:30:43 +0200528 /* Let hard_header_len set to IEEE802154_MIN_HEADER_LEN. AF_PACKET
529 * will not send frames without any payload, but ack frames
530 * has no payload, so substract one that we can send a 3 bytes
531 * frame. The xmit callback assumes at least a hard header where two
532 * bytes fc and sequence field are set.
533 */
534 dev->hard_header_len = IEEE802154_MIN_HEADER_LEN - 1;
535 /* The auth_tag header is for security and places in private payload
536 * room of mac frame which stucks between payload and FCS field.
537 */
538 dev->needed_tailroom = IEEE802154_MAX_AUTH_TAG_LEN +
539 IEEE802154_FCS_LEN;
Alexander Aringb40988c2015-09-28 12:36:26 +0200540 /* The mtu size is the payload without mac header in this case.
541 * We have a dynamic length header with a minimum header length
542 * which is hard_header_len. In this case we let mtu to the size
543 * of maximum payload which is IEEE802154_MTU - IEEE802154_FCS_LEN -
544 * hard_header_len. The FCS which is set by hardware or ndo_start_xmit
545 * and the minimum mac header which can be evaluated inside driver
546 * layer. The rest of mac header will be part of payload if greater
547 * than hard_header_len.
548 */
549 dev->mtu = IEEE802154_MTU - IEEE802154_FCS_LEN -
550 dev->hard_header_len;
Alan Otte937f5832013-04-03 04:00:57 +0000551 dev->tx_queue_len = 300;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000552 dev->flags = IFF_NOARP | IFF_BROADCAST;
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100553}
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000554
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100555static int
Alexander Aring944742a2014-11-17 08:20:49 +0100556ieee802154_setup_sdata(struct ieee802154_sub_if_data *sdata,
557 enum nl802154_iftype type)
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100558{
Alexander Aring863e88f2014-11-09 08:36:45 +0100559 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
Alexander Aringd77b4852015-06-21 16:45:20 +0200560 int ret;
Alexander Aring344f8c12015-05-22 17:43:53 +0200561 u8 tmp;
Alexander Aring863e88f2014-11-09 08:36:45 +0100562
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100563 /* set some type-dependent values */
Alexander Aring190ac1c2014-11-09 08:36:49 +0100564 sdata->wpan_dev.iftype = type;
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000565
Alexander Aring344f8c12015-05-22 17:43:53 +0200566 get_random_bytes(&tmp, sizeof(tmp));
567 atomic_set(&wpan_dev->bsn, tmp);
568 get_random_bytes(&tmp, sizeof(tmp));
569 atomic_set(&wpan_dev->dsn, tmp);
alex.bluesman.smirnov@gmail.com32bad7e2012-06-25 23:24:48 +0000570
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200571 /* defaults per 802.15.4-2011 */
Alexander Aring5fb3f022014-11-09 08:36:46 +0100572 wpan_dev->min_be = 3;
573 wpan_dev->max_be = 5;
574 wpan_dev->csma_retries = 4;
Alexander Aring89c7d782015-08-10 21:15:56 +0200575 wpan_dev->frame_retries = 3;
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200576
Alexander Aring863e88f2014-11-09 08:36:45 +0100577 wpan_dev->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST);
578 wpan_dev->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST);
Phoebe Buckheisterf30be4d2014-05-16 17:46:40 +0200579
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100580 switch (type) {
Alexander Aring944742a2014-11-17 08:20:49 +0100581 case NL802154_IFTYPE_NODE:
Alexander Aringb03c9cc2014-11-09 08:36:56 +0100582 ieee802154_be64_to_le64(&wpan_dev->extended_addr,
583 sdata->dev->dev_addr);
584
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100585 sdata->dev->header_ops = &mac802154_header_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400586 sdata->dev->needs_free_netdev = true;
587 sdata->dev->priv_destructor = mac802154_wpan_free;
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100588 sdata->dev->netdev_ops = &mac802154_wpan_ops;
589 sdata->dev->ml_priv = &mac802154_mlme_wpan;
Alexander Aring863e88f2014-11-09 08:36:45 +0100590 wpan_dev->promiscuous_mode = false;
Alexander Aring838b83d2015-09-18 11:30:42 +0200591 wpan_dev->header_ops = &ieee802154_header_ops;
Alexander Aring38130c32014-10-29 21:34:44 +0100592
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100593 mutex_init(&sdata->sec_mtx);
Alexander Aringb9ff77e2014-10-28 18:21:16 +0100594
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100595 mac802154_llsec_init(&sdata->sec);
Alexander Aringd77b4852015-06-21 16:45:20 +0200596 ret = mac802154_wpan_update_llsec(sdata->dev);
597 if (ret < 0)
598 return ret;
599
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100600 break;
Alexander Aring944742a2014-11-17 08:20:49 +0100601 case NL802154_IFTYPE_MONITOR:
David S. Millercf124db2017-05-08 12:52:56 -0400602 sdata->dev->needs_free_netdev = true;
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100603 sdata->dev->netdev_ops = &mac802154_monitor_ops;
Alexander Aring863e88f2014-11-09 08:36:45 +0100604 wpan_dev->promiscuous_mode = true;
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100605 break;
606 default:
607 BUG();
608 }
Alexander Aring986a8ab2014-11-05 20:51:15 +0100609
610 return 0;
611}
612
613struct net_device *
614ieee802154_if_add(struct ieee802154_local *local, const char *name,
Varka Bhadram5b4a10392015-04-30 17:44:57 +0200615 unsigned char name_assign_type, enum nl802154_iftype type,
616 __le64 extended_addr)
Alexander Aring986a8ab2014-11-05 20:51:15 +0100617{
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100618 struct net_device *ndev = NULL;
619 struct ieee802154_sub_if_data *sdata = NULL;
620 int ret = -ENOMEM;
Alexander Aring986a8ab2014-11-05 20:51:15 +0100621
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100622 ASSERT_RTNL();
623
Alexander Aringed659632015-06-06 17:30:46 +0200624 ndev = alloc_netdev(sizeof(*sdata), name,
Varka Bhadram5b4a10392015-04-30 17:44:57 +0200625 name_assign_type, ieee802154_if_setup);
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100626 if (!ndev)
627 return ERR_PTR(-ENOMEM);
628
Alexander Aring87a93e42015-09-18 11:30:43 +0200629 ndev->needed_headroom = local->hw.extra_tx_headroom +
630 IEEE802154_MAX_HEADER_LEN;
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100631
632 ret = dev_alloc_name(ndev, ndev->name);
633 if (ret < 0)
Alexander Aring986a8ab2014-11-05 20:51:15 +0100634 goto err;
635
Alexander Aring0e575472014-11-17 08:20:52 +0100636 ieee802154_le64_to_be64(ndev->perm_addr,
637 &local->hw.phy->perm_extended_addr);
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100638 switch (type) {
Alexander Aring944742a2014-11-17 08:20:49 +0100639 case NL802154_IFTYPE_NODE:
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100640 ndev->type = ARPHRD_IEEE802154;
Lennert Buytenhekdaf4e2c2015-05-28 15:38:43 +0300641 if (ieee802154_is_valid_extended_unicast_addr(extended_addr))
Alexander Aring0e575472014-11-17 08:20:52 +0100642 ieee802154_le64_to_be64(ndev->dev_addr, &extended_addr);
643 else
644 memcpy(ndev->dev_addr, ndev->perm_addr,
645 IEEE802154_EXTENDED_ADDR_LEN);
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100646 break;
Alexander Aring944742a2014-11-17 08:20:49 +0100647 case NL802154_IFTYPE_MONITOR:
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100648 ndev->type = ARPHRD_IEEE802154_MONITOR;
649 break;
650 default:
651 ret = -EINVAL;
652 goto err;
653 }
Alexander Aring986a8ab2014-11-05 20:51:15 +0100654
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100655 /* TODO check this */
656 SET_NETDEV_DEV(ndev, &local->phy->dev);
Alexander Aringabbcc342016-07-24 16:12:24 +0200657 dev_net_set(ndev, wpan_phy_net(local->hw.phy));
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100658 sdata = netdev_priv(ndev);
659 ndev->ieee802154_ptr = &sdata->wpan_dev;
660 memcpy(sdata->name, ndev->name, IFNAMSIZ);
661 sdata->dev = ndev;
662 sdata->wpan_dev.wpan_phy = local->hw.phy;
663 sdata->local = local;
Alexander Aring986a8ab2014-11-05 20:51:15 +0100664
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100665 /* setup type-dependent data */
666 ret = ieee802154_setup_sdata(sdata, type);
667 if (ret)
668 goto err;
669
Varka Bhadram473f3762014-11-25 16:34:43 +0530670 ret = register_netdevice(ndev);
671 if (ret < 0)
672 goto err;
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100673
674 mutex_lock(&local->iflist_mtx);
675 list_add_tail_rcu(&sdata->list, &local->interfaces);
676 mutex_unlock(&local->iflist_mtx);
677
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100678 return ndev;
679
Alexander Aring986a8ab2014-11-05 20:51:15 +0100680err:
Alexander Aringd5ae67b2014-11-05 20:51:17 +0100681 free_netdev(ndev);
682 return ERR_PTR(ret);
Alexander Aring986a8ab2014-11-05 20:51:15 +0100683}
684
Alexander Aringb210b182014-11-05 20:51:14 +0100685void ieee802154_if_remove(struct ieee802154_sub_if_data *sdata)
686{
687 ASSERT_RTNL();
688
689 mutex_lock(&sdata->local->iflist_mtx);
690 list_del_rcu(&sdata->list);
691 mutex_unlock(&sdata->local->iflist_mtx);
692
693 synchronize_rcu();
694 unregister_netdevice(sdata->dev);
695}
Alexander Aring592dfbfc2014-11-12 03:36:48 +0100696
697void ieee802154_remove_interfaces(struct ieee802154_local *local)
698{
Alexander Aringd14e1c72014-11-12 03:36:49 +0100699 struct ieee802154_sub_if_data *sdata, *tmp;
Alexander Aring592dfbfc2014-11-12 03:36:48 +0100700
Alexander Aring2789e622014-11-12 03:36:50 +0100701 mutex_lock(&local->iflist_mtx);
Alexander Aringd14e1c72014-11-12 03:36:49 +0100702 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
Alexander Aring592dfbfc2014-11-12 03:36:48 +0100703 list_del(&sdata->list);
Alexander Aring592dfbfc2014-11-12 03:36:48 +0100704
705 unregister_netdevice(sdata->dev);
706 }
Alexander Aring2789e622014-11-12 03:36:50 +0100707 mutex_unlock(&local->iflist_mtx);
Alexander Aring592dfbfc2014-11-12 03:36:48 +0100708}
Alexander Aringbe4fd8e2014-11-12 03:36:53 +0100709
710static int netdev_notify(struct notifier_block *nb,
711 unsigned long state, void *ptr)
712{
713 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
714 struct ieee802154_sub_if_data *sdata;
715
716 if (state != NETDEV_CHANGENAME)
717 return NOTIFY_DONE;
718
719 if (!dev->ieee802154_ptr || !dev->ieee802154_ptr->wpan_phy)
720 return NOTIFY_DONE;
721
722 if (dev->ieee802154_ptr->wpan_phy->privid != mac802154_wpan_phy_privid)
723 return NOTIFY_DONE;
724
725 sdata = IEEE802154_DEV_TO_SUB_IF(dev);
726 memcpy(sdata->name, dev->name, IFNAMSIZ);
727
728 return NOTIFY_OK;
729}
730
731static struct notifier_block mac802154_netdev_notifier = {
732 .notifier_call = netdev_notify,
733};
734
735int ieee802154_iface_init(void)
736{
737 return register_netdevice_notifier(&mac802154_netdev_notifier);
738}
739
740void ieee802154_iface_exit(void)
741{
742 unregister_netdevice_notifier(&mac802154_netdev_notifier);
743}