Skip to content

Commit 838b83d

Browse files
alexaringholtmann
authored andcommitted
ieee802154: introduce wpan_dev_header_ops
The current header_ops callback structure of net device are used mostly from 802.15.4 upper-layers. Because this callback structure is a very generic one, which is also used by e.g. DGRAM AF_PACKET sockets, we can't make this callback structure 802.15.4 specific which is currently is. I saw the smallest "constraint" for calling this callback with dev_hard_header/dev_parse_header by AF_PACKET which assign a 8 byte array for address void pointers. Currently 802.15.4 specific protocols like af802154 and 6LoWPAN will assign the "struct ieee802154_addr" as these parameters which is greater than 8 bytes. The current callback implementation for header_ops.create assumes always a complete "struct ieee802154_addr" which AF_PACKET can't never handled and is greater than 8 bytes. For that reason we introduce now a "generic" create/parse header_ops callback which allows handling with intra-pan extended addresses only. This allows a small use-case with AF_PACKET to send "somehow" a valid dataframe over DGRAM. To keeping the current dev_hard_header behaviour we introduce a similar callback structure "wpan_dev_header_ops" which contains 802.15.4 specific upper-layer header creation functionality, which can be called by wpan_dev_hard_header. Signed-off-by: Alexander Aring <alex.aring@gmail.com> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
1 parent a1da67b commit 838b83d

5 files changed

Lines changed: 118 additions & 27 deletions

File tree

‎include/net/cfg802154.h‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,26 @@ struct wpan_phy {
167167
char priv[0] __aligned(NETDEV_ALIGN);
168168
};
169169

170+
struct ieee802154_addr {
171+
u8 mode;
172+
__le16 pan_id;
173+
union {
174+
__le16 short_addr;
175+
__le64 extended_addr;
176+
};
177+
};
178+
179+
struct wpan_dev_header_ops {
180+
/* TODO create callback currently assumes ieee802154_mac_cb inside
181+
* skb->cb. This should be changed to give these information as
182+
* parameter.
183+
*/
184+
int (*create)(struct sk_buff *skb, struct net_device *dev,
185+
const struct ieee802154_addr *daddr,
186+
const struct ieee802154_addr *saddr,
187+
unsigned int len);
188+
};
189+
170190
struct wpan_dev {
171191
struct wpan_phy *wpan_phy;
172192
int iftype;
@@ -175,6 +195,8 @@ struct wpan_dev {
175195
struct list_head list;
176196
struct net_device *netdev;
177197

198+
const struct wpan_dev_header_ops *header_ops;
199+
178200
/* lowpan interface, set when the wpan_dev belongs to one lowpan_dev */
179201
struct net_device *lowpan_dev;
180202

@@ -205,6 +227,17 @@ struct wpan_dev {
205227

206228
#define to_phy(_dev) container_of(_dev, struct wpan_phy, dev)
207229

230+
static inline int
231+
wpan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
232+
const struct ieee802154_addr *daddr,
233+
const struct ieee802154_addr *saddr,
234+
unsigned int len)
235+
{
236+
struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
237+
238+
return wpan_dev->header_ops->create(skb, dev, daddr, saddr, len);
239+
}
240+
208241
struct wpan_phy *
209242
wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size);
210243
static inline void wpan_phy_set_dev(struct wpan_phy *phy, struct device *dev)

‎include/net/ieee802154_netdev.h‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@ struct ieee802154_sechdr {
5050
};
5151
};
5252

53-
struct ieee802154_addr {
54-
u8 mode;
55-
__le16 pan_id;
56-
union {
57-
__le16 short_addr;
58-
__le64 extended_addr;
59-
};
60-
};
61-
6253
struct ieee802154_hdr_fc {
6354
#if defined(__LITTLE_ENDIAN_BITFIELD)
6455
u16 type:3,

‎net/ieee802154/6lowpan/tx.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ lowpan_alloc_frag(struct sk_buff *skb, int size,
8787
skb_reset_network_header(frag);
8888
*mac_cb(frag) = *mac_cb(skb);
8989

90-
rc = dev_hard_header(frag, wdev, 0, &master_hdr->dest,
91-
&master_hdr->source, size);
90+
rc = wpan_dev_hard_header(frag, wdev, &master_hdr->dest,
91+
&master_hdr->source, size);
9292
if (rc < 0) {
9393
kfree_skb(frag);
9494
return ERR_PTR(rc);
@@ -228,8 +228,8 @@ static int lowpan_header(struct sk_buff *skb, struct net_device *ldev,
228228
cb->ackreq = wpan_dev->ackreq;
229229
}
230230

231-
return dev_hard_header(skb, lowpan_dev_info(ldev)->wdev, ETH_P_IPV6,
232-
(void *)&da, (void *)&sa, 0);
231+
return wpan_dev_hard_header(skb, lowpan_dev_info(ldev)->wdev, &da, &sa,
232+
0);
233233
}
234234

235235
netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *ldev)

‎net/ieee802154/socket.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,8 @@ static int dgram_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
676676
cb->seclevel = ro->seclevel;
677677
cb->seclevel_override = ro->seclevel_override;
678678

679-
err = dev_hard_header(skb, dev, ETH_P_IEEE802154, &dst_addr,
680-
ro->bound ? &ro->src_addr : NULL, size);
679+
err = wpan_dev_hard_header(skb, dev, &dst_addr,
680+
ro->bound ? &ro->src_addr : NULL, size);
681681
if (err < 0)
682682
goto out_skb;
683683

‎net/mac802154/iface.c‎

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,11 @@ static int mac802154_set_header_security(struct ieee802154_sub_if_data *sdata,
367367
return 0;
368368
}
369369

370-
static int mac802154_header_create(struct sk_buff *skb,
371-
struct net_device *dev,
372-
unsigned short type,
373-
const void *daddr,
374-
const void *saddr,
375-
unsigned len)
370+
static int ieee802154_header_create(struct sk_buff *skb,
371+
struct net_device *dev,
372+
const struct ieee802154_addr *daddr,
373+
const struct ieee802154_addr *saddr,
374+
unsigned len)
376375
{
377376
struct ieee802154_hdr hdr;
378377
struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
@@ -423,24 +422,91 @@ static int mac802154_header_create(struct sk_buff *skb,
423422
return hlen;
424423
}
425424

425+
static const struct wpan_dev_header_ops ieee802154_header_ops = {
426+
.create = ieee802154_header_create,
427+
};
428+
429+
/* This header create functionality assumes a 8 byte array for
430+
* source and destination pointer at maximum. To adapt this for
431+
* the 802.15.4 dataframe header we use extended address handling
432+
* here only and intra pan connection. fc fields are mostly fallback
433+
* handling. For provide dev_hard_header for dgram sockets.
434+
*/
435+
static int mac802154_header_create(struct sk_buff *skb,
436+
struct net_device *dev,
437+
unsigned short type,
438+
const void *daddr,
439+
const void *saddr,
440+
unsigned len)
441+
{
442+
struct ieee802154_hdr hdr;
443+
struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
444+
struct wpan_dev *wpan_dev = &sdata->wpan_dev;
445+
struct ieee802154_mac_cb cb = { };
446+
int hlen;
447+
448+
if (!daddr)
449+
return -EINVAL;
450+
451+
memset(&hdr.fc, 0, sizeof(hdr.fc));
452+
hdr.fc.type = IEEE802154_FC_TYPE_DATA;
453+
hdr.fc.ack_request = wpan_dev->ackreq;
454+
hdr.seq = atomic_inc_return(&dev->ieee802154_ptr->dsn) & 0xFF;
455+
456+
/* TODO currently a workaround to give zero cb block to set
457+
* security parameters defaults according MIB.
458+
*/
459+
if (mac802154_set_header_security(sdata, &hdr, &cb) < 0)
460+
return -EINVAL;
461+
462+
hdr.dest.pan_id = wpan_dev->pan_id;
463+
hdr.dest.mode = IEEE802154_ADDR_LONG;
464+
memcpy(&hdr.dest.extended_addr, daddr, IEEE802154_EXTENDED_ADDR_LEN);
465+
466+
hdr.source.pan_id = hdr.dest.pan_id;
467+
hdr.source.mode = IEEE802154_ADDR_LONG;
468+
469+
if (!saddr)
470+
hdr.source.extended_addr = wpan_dev->extended_addr;
471+
else
472+
memcpy(&hdr.source.extended_addr, saddr,
473+
IEEE802154_EXTENDED_ADDR_LEN);
474+
475+
hlen = ieee802154_hdr_push(skb, &hdr);
476+
if (hlen < 0)
477+
return -EINVAL;
478+
479+
skb_reset_mac_header(skb);
480+
skb->mac_len = hlen;
481+
482+
if (len > ieee802154_max_payload(&hdr))
483+
return -EMSGSIZE;
484+
485+
return hlen;
486+
}
487+
426488
static int
427489
mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr)
428490
{
429491
struct ieee802154_hdr hdr;
430-
struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr;
431492

432493
if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) {
433494
pr_debug("malformed packet\n");
434495
return 0;
435496
}
436497

437-
*addr = hdr.source;
438-
return sizeof(*addr);
498+
if (hdr.source.mode == IEEE802154_ADDR_LONG) {
499+
memcpy(haddr, &hdr.source.extended_addr,
500+
IEEE802154_EXTENDED_ADDR_LEN);
501+
return IEEE802154_EXTENDED_ADDR_LEN;
502+
}
503+
504+
return 0;
439505
}
440506

441-
static struct header_ops mac802154_header_ops = {
442-
.create = mac802154_header_create,
443-
.parse = mac802154_header_parse,
507+
static const struct header_ops mac802154_header_ops = {
508+
.create = mac802154_header_create,
509+
.parse = mac802154_header_parse,
444510
};
445511

446512
static const struct net_device_ops mac802154_wpan_ops = {
@@ -513,6 +579,7 @@ ieee802154_setup_sdata(struct ieee802154_sub_if_data *sdata,
513579
sdata->dev->netdev_ops = &mac802154_wpan_ops;
514580
sdata->dev->ml_priv = &mac802154_mlme_wpan;
515581
wpan_dev->promiscuous_mode = false;
582+
wpan_dev->header_ops = &ieee802154_header_ops;
516583

517584
mutex_init(&sdata->sec_mtx);
518585

0 commit comments

Comments
 (0)