2 * Copyright 2007-2012 Siemens AG
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <linux/netdevice.h>
25 #include <linux/module.h>
26 #include <linux/if_arp.h>
28 #include <net/rtnetlink.h>
29 #include <linux/nl802154.h>
30 #include <net/af_ieee802154.h>
31 #include <net/mac802154.h>
32 #include <net/ieee802154_netdev.h>
33 #include <net/ieee802154.h>
34 #include <net/wpan-phy.h>
36 #include "mac802154.h"
38 static inline int mac802154_fetch_skb_u8(struct sk_buff *skb, u8 *val)
40 if (unlikely(!pskb_may_pull(skb, 1)))
49 static inline int mac802154_fetch_skb_u16(struct sk_buff *skb, u16 *val)
51 if (unlikely(!pskb_may_pull(skb, 2)))
54 *val = skb->data[0] | (skb->data[1] << 8);
60 static inline void mac802154_haddr_copy_swap(u8 *dest, const u8 *src)
63 for (i = 0; i < IEEE802154_ADDR_LEN; i++)
64 dest[IEEE802154_ADDR_LEN - i - 1] = src[i];
68 mac802154_wpan_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
70 struct mac802154_sub_if_data *priv = netdev_priv(dev);
71 struct sockaddr_ieee802154 *sa =
72 (struct sockaddr_ieee802154 *)&ifr->ifr_addr;
73 int err = -ENOIOCTLCMD;
75 spin_lock_bh(&priv->mib_lock);
79 if (priv->pan_id == IEEE802154_PANID_BROADCAST ||
80 priv->short_addr == IEEE802154_ADDR_BROADCAST) {
85 sa->family = AF_IEEE802154;
86 sa->addr.addr_type = IEEE802154_ADDR_SHORT;
87 sa->addr.pan_id = priv->pan_id;
88 sa->addr.short_addr = priv->short_addr;
94 "Using DEBUGing ioctl SIOCSIFADDR isn't recommened!\n");
95 if (sa->family != AF_IEEE802154 ||
96 sa->addr.addr_type != IEEE802154_ADDR_SHORT ||
97 sa->addr.pan_id == IEEE802154_PANID_BROADCAST ||
98 sa->addr.short_addr == IEEE802154_ADDR_BROADCAST ||
99 sa->addr.short_addr == IEEE802154_ADDR_UNDEF) {
104 priv->pan_id = sa->addr.pan_id;
105 priv->short_addr = sa->addr.short_addr;
111 spin_unlock_bh(&priv->mib_lock);
115 static int mac802154_wpan_mac_addr(struct net_device *dev, void *p)
117 struct sockaddr *addr = p;
119 if (netif_running(dev))
122 /* FIXME: validate addr */
123 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
124 mac802154_dev_set_ieee_addr(dev);
128 static int mac802154_header_create(struct sk_buff *skb,
129 struct net_device *dev,
135 const struct ieee802154_addr *saddr = _saddr;
136 const struct ieee802154_addr *daddr = _daddr;
137 struct ieee802154_addr dev_addr;
138 struct mac802154_sub_if_data *priv = netdev_priv(dev);
140 u8 head[MAC802154_FRAME_HARD_HEADER_LEN];
146 head[pos++] = mac_cb(skb)->seq; /* DSN/BSN */
147 fc = mac_cb_type(skb);
150 spin_lock_bh(&priv->mib_lock);
152 if (priv->short_addr == IEEE802154_ADDR_BROADCAST ||
153 priv->short_addr == IEEE802154_ADDR_UNDEF ||
154 priv->pan_id == IEEE802154_PANID_BROADCAST) {
155 dev_addr.addr_type = IEEE802154_ADDR_LONG;
156 memcpy(dev_addr.hwaddr, dev->dev_addr,
157 IEEE802154_ADDR_LEN);
159 dev_addr.addr_type = IEEE802154_ADDR_SHORT;
160 dev_addr.short_addr = priv->short_addr;
163 dev_addr.pan_id = priv->pan_id;
166 spin_unlock_bh(&priv->mib_lock);
169 if (daddr->addr_type != IEEE802154_ADDR_NONE) {
170 fc |= (daddr->addr_type << IEEE802154_FC_DAMODE_SHIFT);
172 head[pos++] = daddr->pan_id & 0xff;
173 head[pos++] = daddr->pan_id >> 8;
175 if (daddr->addr_type == IEEE802154_ADDR_SHORT) {
176 head[pos++] = daddr->short_addr & 0xff;
177 head[pos++] = daddr->short_addr >> 8;
179 mac802154_haddr_copy_swap(head + pos, daddr->hwaddr);
180 pos += IEEE802154_ADDR_LEN;
184 if (saddr->addr_type != IEEE802154_ADDR_NONE) {
185 fc |= (saddr->addr_type << IEEE802154_FC_SAMODE_SHIFT);
187 if ((saddr->pan_id == daddr->pan_id) &&
188 (saddr->pan_id != IEEE802154_PANID_BROADCAST)) {
189 /* PANID compression/intra PAN */
190 fc |= IEEE802154_FC_INTRA_PAN;
192 head[pos++] = saddr->pan_id & 0xff;
193 head[pos++] = saddr->pan_id >> 8;
196 if (saddr->addr_type == IEEE802154_ADDR_SHORT) {
197 head[pos++] = saddr->short_addr & 0xff;
198 head[pos++] = saddr->short_addr >> 8;
200 mac802154_haddr_copy_swap(head + pos, saddr->hwaddr);
201 pos += IEEE802154_ADDR_LEN;
208 memcpy(skb_push(skb, pos), head, pos);
214 mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
216 const u8 *hdr = skb_mac_header(skb);
217 const u8 *tail = skb_tail_pointer(skb);
218 struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr;
225 fc = hdr[0] | (hdr[1] << 8);
229 da_type = IEEE802154_FC_DAMODE(fc);
230 addr->addr_type = IEEE802154_FC_SAMODE(fc);
233 case IEEE802154_ADDR_NONE:
234 if (fc & IEEE802154_FC_INTRA_PAN)
237 case IEEE802154_ADDR_LONG:
238 if (fc & IEEE802154_FC_INTRA_PAN) {
241 addr->pan_id = hdr[0] | (hdr[1] << 8);
245 if (hdr + IEEE802154_ADDR_LEN > tail)
248 hdr += IEEE802154_ADDR_LEN;
250 case IEEE802154_ADDR_SHORT:
251 if (fc & IEEE802154_FC_INTRA_PAN) {
254 addr->pan_id = hdr[0] | (hdr[1] << 8);
268 switch (addr->addr_type) {
269 case IEEE802154_ADDR_NONE:
271 case IEEE802154_ADDR_LONG:
272 if (!(fc & IEEE802154_FC_INTRA_PAN)) {
275 addr->pan_id = hdr[0] | (hdr[1] << 8);
279 if (hdr + IEEE802154_ADDR_LEN > tail)
282 mac802154_haddr_copy_swap(addr->hwaddr, hdr);
283 hdr += IEEE802154_ADDR_LEN;
285 case IEEE802154_ADDR_SHORT:
286 if (!(fc & IEEE802154_FC_INTRA_PAN)) {
289 addr->pan_id = hdr[0] | (hdr[1] << 8);
296 addr->short_addr = hdr[0] | (hdr[1] << 8);
303 return sizeof(struct ieee802154_addr);
306 pr_debug("malformed packet\n");
311 mac802154_wpan_xmit(struct sk_buff *skb, struct net_device *dev)
313 struct mac802154_sub_if_data *priv;
316 priv = netdev_priv(dev);
318 spin_lock_bh(&priv->mib_lock);
321 spin_unlock_bh(&priv->mib_lock);
323 if (chan == MAC802154_CHAN_NONE ||
324 page >= WPAN_NUM_PAGES ||
325 chan >= WPAN_NUM_CHANNELS) {
330 skb->skb_iif = dev->ifindex;
331 dev->stats.tx_packets++;
332 dev->stats.tx_bytes += skb->len;
334 return mac802154_tx(priv->hw, skb, page, chan);
337 static struct header_ops mac802154_header_ops = {
338 .create = mac802154_header_create,
339 .parse = mac802154_header_parse,
342 static const struct net_device_ops mac802154_wpan_ops = {
343 .ndo_open = mac802154_slave_open,
344 .ndo_stop = mac802154_slave_close,
345 .ndo_start_xmit = mac802154_wpan_xmit,
346 .ndo_do_ioctl = mac802154_wpan_ioctl,
347 .ndo_set_mac_address = mac802154_wpan_mac_addr,
350 void mac802154_wpan_setup(struct net_device *dev)
352 struct mac802154_sub_if_data *priv;
354 dev->addr_len = IEEE802154_ADDR_LEN;
355 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
357 dev->hard_header_len = MAC802154_FRAME_HARD_HEADER_LEN;
358 dev->header_ops = &mac802154_header_ops;
359 dev->needed_tailroom = 2; /* FCS */
360 dev->mtu = IEEE802154_MTU;
361 dev->tx_queue_len = 10;
362 dev->type = ARPHRD_IEEE802154;
363 dev->flags = IFF_NOARP | IFF_BROADCAST;
364 dev->watchdog_timeo = 0;
366 dev->destructor = free_netdev;
367 dev->netdev_ops = &mac802154_wpan_ops;
368 dev->ml_priv = &mac802154_mlme_wpan;
370 priv = netdev_priv(dev);
371 priv->type = IEEE802154_DEV_WPAN;
373 priv->chan = MAC802154_CHAN_NONE;
376 spin_lock_init(&priv->mib_lock);
378 get_random_bytes(&priv->bsn, 1);
379 get_random_bytes(&priv->dsn, 1);
381 priv->pan_id = IEEE802154_PANID_BROADCAST;
382 priv->short_addr = IEEE802154_ADDR_BROADCAST;
385 static int mac802154_process_data(struct net_device *dev, struct sk_buff *skb)
387 return netif_rx_ni(skb);
391 mac802154_subif_frame(struct mac802154_sub_if_data *sdata, struct sk_buff *skb)
393 pr_debug("getting packet via slave interface %s\n", sdata->dev->name);
395 spin_lock_bh(&sdata->mib_lock);
397 switch (mac_cb(skb)->da.addr_type) {
398 case IEEE802154_ADDR_NONE:
399 if (mac_cb(skb)->sa.addr_type != IEEE802154_ADDR_NONE)
400 /* FIXME: check if we are PAN coordinator */
401 skb->pkt_type = PACKET_OTHERHOST;
403 /* ACK comes with both addresses empty */
404 skb->pkt_type = PACKET_HOST;
406 case IEEE802154_ADDR_LONG:
407 if (mac_cb(skb)->da.pan_id != sdata->pan_id &&
408 mac_cb(skb)->da.pan_id != IEEE802154_PANID_BROADCAST)
409 skb->pkt_type = PACKET_OTHERHOST;
410 else if (!memcmp(mac_cb(skb)->da.hwaddr, sdata->dev->dev_addr,
411 IEEE802154_ADDR_LEN))
412 skb->pkt_type = PACKET_HOST;
414 skb->pkt_type = PACKET_OTHERHOST;
416 case IEEE802154_ADDR_SHORT:
417 if (mac_cb(skb)->da.pan_id != sdata->pan_id &&
418 mac_cb(skb)->da.pan_id != IEEE802154_PANID_BROADCAST)
419 skb->pkt_type = PACKET_OTHERHOST;
420 else if (mac_cb(skb)->da.short_addr == sdata->short_addr)
421 skb->pkt_type = PACKET_HOST;
422 else if (mac_cb(skb)->da.short_addr ==
423 IEEE802154_ADDR_BROADCAST)
424 skb->pkt_type = PACKET_BROADCAST;
426 skb->pkt_type = PACKET_OTHERHOST;
432 spin_unlock_bh(&sdata->mib_lock);
434 skb->dev = sdata->dev;
436 sdata->dev->stats.rx_packets++;
437 sdata->dev->stats.rx_bytes += skb->len;
439 switch (mac_cb_type(skb)) {
440 case IEEE802154_FC_TYPE_DATA:
441 return mac802154_process_data(sdata->dev, skb);
443 pr_warning("ieee802154: bad frame received (type = %d)\n",
450 static int mac802154_parse_frame_start(struct sk_buff *skb)
452 u8 *head = skb->data;
455 if (mac802154_fetch_skb_u16(skb, &fc) ||
456 mac802154_fetch_skb_u8(skb, &(mac_cb(skb)->seq)))
459 pr_debug("fc: %04x dsn: %02x\n", fc, head[2]);
461 mac_cb(skb)->flags = IEEE802154_FC_TYPE(fc);
462 mac_cb(skb)->sa.addr_type = IEEE802154_FC_SAMODE(fc);
463 mac_cb(skb)->da.addr_type = IEEE802154_FC_DAMODE(fc);
465 if (fc & IEEE802154_FC_INTRA_PAN)
466 mac_cb(skb)->flags |= MAC_CB_FLAG_INTRAPAN;
468 if (mac_cb(skb)->da.addr_type != IEEE802154_ADDR_NONE) {
469 if (mac802154_fetch_skb_u16(skb, &(mac_cb(skb)->da.pan_id)))
472 /* source PAN id compression */
473 if (mac_cb_is_intrapan(skb))
474 mac_cb(skb)->sa.pan_id = mac_cb(skb)->da.pan_id;
476 pr_debug("dest PAN addr: %04x\n", mac_cb(skb)->da.pan_id);
478 if (mac_cb(skb)->da.addr_type == IEEE802154_ADDR_SHORT) {
479 u16 *da = &(mac_cb(skb)->da.short_addr);
481 if (mac802154_fetch_skb_u16(skb, da))
484 pr_debug("destination address is short: %04x\n",
485 mac_cb(skb)->da.short_addr);
487 if (!pskb_may_pull(skb, IEEE802154_ADDR_LEN))
490 mac802154_haddr_copy_swap(mac_cb(skb)->da.hwaddr,
492 skb_pull(skb, IEEE802154_ADDR_LEN);
494 pr_debug("destination address is hardware\n");
498 if (mac_cb(skb)->sa.addr_type != IEEE802154_ADDR_NONE) {
499 /* non PAN-compression, fetch source address id */
500 if (!(mac_cb_is_intrapan(skb))) {
501 u16 *sa_pan = &(mac_cb(skb)->sa.pan_id);
503 if (mac802154_fetch_skb_u16(skb, sa_pan))
507 pr_debug("source PAN addr: %04x\n", mac_cb(skb)->da.pan_id);
509 if (mac_cb(skb)->sa.addr_type == IEEE802154_ADDR_SHORT) {
510 u16 *sa = &(mac_cb(skb)->sa.short_addr);
512 if (mac802154_fetch_skb_u16(skb, sa))
515 pr_debug("source address is short: %04x\n",
516 mac_cb(skb)->sa.short_addr);
518 if (!pskb_may_pull(skb, IEEE802154_ADDR_LEN))
521 mac802154_haddr_copy_swap(mac_cb(skb)->sa.hwaddr,
523 skb_pull(skb, IEEE802154_ADDR_LEN);
525 pr_debug("source address is hardware\n");
534 void mac802154_wpans_rx(struct mac802154_priv *priv, struct sk_buff *skb)
537 struct sk_buff *sskb;
538 struct mac802154_sub_if_data *sdata;
540 ret = mac802154_parse_frame_start(skb);
542 pr_debug("got invalid frame\n");
547 list_for_each_entry_rcu(sdata, &priv->slaves, list) {
548 if (sdata->type != IEEE802154_DEV_WPAN)
551 sskb = skb_clone(skb, GFP_ATOMIC);
553 mac802154_subif_frame(sdata, sskb);