Commit | Line | Data |
---|---|---|
1cd829c8 | 1 | /* |
2 | * Copyright (C) 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 | * | |
1cd829c8 | 13 | * Written by: |
14 | * Pavel Smolenskiy <pavel.smolenskiy@gmail.com> | |
15 | * Maxim Gorbachyov <maxim.gorbachev@siemens.com> | |
16 | * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> | |
17 | * Alexander Smirnov <alex.bluesman.smirnov@gmail.com> | |
18 | */ | |
19 | ||
20 | #include <linux/kernel.h> | |
21 | #include <linux/module.h> | |
1cd829c8 | 22 | #include <linux/netdevice.h> |
23 | #include <linux/crc-ccitt.h> | |
b7889497 | 24 | #include <asm/unaligned.h> |
1cd829c8 | 25 | |
26 | #include <net/mac802154.h> | |
27 | #include <net/ieee802154_netdev.h> | |
2a9820c9 AA |
28 | #include <net/rtnetlink.h> |
29 | #include <linux/nl802154.h> | |
1cd829c8 | 30 | |
0f1556bc | 31 | #include "ieee802154_i.h" |
1cd829c8 | 32 | |
08c511a7 | 33 | static int ieee802154_deliver_skb(struct sk_buff *skb) |
2a9820c9 | 34 | { |
75a46f0e | 35 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
702dcf99 AA |
36 | skb->protocol = htons(ETH_P_IEEE802154); |
37 | ||
2a9820c9 AA |
38 | return netif_receive_skb(skb); |
39 | } | |
40 | ||
41 | static int | |
be9d215f AA |
42 | ieee802154_subif_frame(struct ieee802154_sub_if_data *sdata, |
43 | struct sk_buff *skb, const struct ieee802154_hdr *hdr) | |
2a9820c9 | 44 | { |
863e88f2 | 45 | struct wpan_dev *wpan_dev = &sdata->wpan_dev; |
2a9820c9 AA |
46 | __le16 span, sshort; |
47 | int rc; | |
48 | ||
49 | pr_debug("getting packet via slave interface %s\n", sdata->dev->name); | |
50 | ||
51 | spin_lock_bh(&sdata->mib_lock); | |
52 | ||
863e88f2 AA |
53 | span = wpan_dev->pan_id; |
54 | sshort = wpan_dev->short_addr; | |
2a9820c9 AA |
55 | |
56 | switch (mac_cb(skb)->dest.mode) { | |
57 | case IEEE802154_ADDR_NONE: | |
58 | if (mac_cb(skb)->dest.mode != IEEE802154_ADDR_NONE) | |
59 | /* FIXME: check if we are PAN coordinator */ | |
60 | skb->pkt_type = PACKET_OTHERHOST; | |
61 | else | |
62 | /* ACK comes with both addresses empty */ | |
63 | skb->pkt_type = PACKET_HOST; | |
64 | break; | |
65 | case IEEE802154_ADDR_LONG: | |
66 | if (mac_cb(skb)->dest.pan_id != span && | |
67 | mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST)) | |
68 | skb->pkt_type = PACKET_OTHERHOST; | |
863e88f2 | 69 | else if (mac_cb(skb)->dest.extended_addr == wpan_dev->extended_addr) |
2a9820c9 AA |
70 | skb->pkt_type = PACKET_HOST; |
71 | else | |
72 | skb->pkt_type = PACKET_OTHERHOST; | |
73 | break; | |
74 | case IEEE802154_ADDR_SHORT: | |
75 | if (mac_cb(skb)->dest.pan_id != span && | |
76 | mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST)) | |
77 | skb->pkt_type = PACKET_OTHERHOST; | |
78 | else if (mac_cb(skb)->dest.short_addr == sshort) | |
79 | skb->pkt_type = PACKET_HOST; | |
80 | else if (mac_cb(skb)->dest.short_addr == | |
81 | cpu_to_le16(IEEE802154_ADDR_BROADCAST)) | |
82 | skb->pkt_type = PACKET_BROADCAST; | |
83 | else | |
84 | skb->pkt_type = PACKET_OTHERHOST; | |
85 | break; | |
86 | default: | |
87 | spin_unlock_bh(&sdata->mib_lock); | |
88 | pr_debug("invalid dest mode\n"); | |
89 | kfree_skb(skb); | |
90 | return NET_RX_DROP; | |
91 | } | |
92 | ||
93 | spin_unlock_bh(&sdata->mib_lock); | |
94 | ||
95 | skb->dev = sdata->dev; | |
96 | ||
97 | rc = mac802154_llsec_decrypt(&sdata->sec, skb); | |
98 | if (rc) { | |
99 | pr_debug("decryption failed: %i\n", rc); | |
100 | goto fail; | |
101 | } | |
102 | ||
103 | sdata->dev->stats.rx_packets++; | |
104 | sdata->dev->stats.rx_bytes += skb->len; | |
105 | ||
106 | switch (mac_cb(skb)->type) { | |
107 | case IEEE802154_FC_TYPE_DATA: | |
08c511a7 | 108 | return ieee802154_deliver_skb(skb); |
2a9820c9 AA |
109 | default: |
110 | pr_warn("ieee802154: bad frame received (type = %d)\n", | |
111 | mac_cb(skb)->type); | |
112 | goto fail; | |
113 | } | |
114 | ||
115 | fail: | |
116 | kfree_skb(skb); | |
117 | return NET_RX_DROP; | |
118 | } | |
119 | ||
be9d215f AA |
120 | static void |
121 | ieee802154_print_addr(const char *name, const struct ieee802154_addr *addr) | |
2a9820c9 AA |
122 | { |
123 | if (addr->mode == IEEE802154_ADDR_NONE) | |
124 | pr_debug("%s not present\n", name); | |
125 | ||
126 | pr_debug("%s PAN ID: %04x\n", name, le16_to_cpu(addr->pan_id)); | |
127 | if (addr->mode == IEEE802154_ADDR_SHORT) { | |
128 | pr_debug("%s is short: %04x\n", name, | |
129 | le16_to_cpu(addr->short_addr)); | |
130 | } else { | |
131 | u64 hw = swab64((__force u64)addr->extended_addr); | |
132 | ||
133 | pr_debug("%s is hardware: %8phC\n", name, &hw); | |
134 | } | |
135 | } | |
136 | ||
be9d215f AA |
137 | static int |
138 | ieee802154_parse_frame_start(struct sk_buff *skb, struct ieee802154_hdr *hdr) | |
2a9820c9 AA |
139 | { |
140 | int hlen; | |
141 | struct ieee802154_mac_cb *cb = mac_cb_init(skb); | |
142 | ||
9cf215d0 AA |
143 | skb_reset_mac_header(skb); |
144 | ||
2a9820c9 AA |
145 | hlen = ieee802154_hdr_pull(skb, hdr); |
146 | if (hlen < 0) | |
147 | return -EINVAL; | |
148 | ||
149 | skb->mac_len = hlen; | |
150 | ||
151 | pr_debug("fc: %04x dsn: %02x\n", le16_to_cpup((__le16 *)&hdr->fc), | |
152 | hdr->seq); | |
153 | ||
154 | cb->type = hdr->fc.type; | |
155 | cb->ackreq = hdr->fc.ack_request; | |
156 | cb->secen = hdr->fc.security_enabled; | |
157 | ||
be9d215f AA |
158 | ieee802154_print_addr("destination", &hdr->dest); |
159 | ieee802154_print_addr("source", &hdr->source); | |
2a9820c9 AA |
160 | |
161 | cb->source = hdr->source; | |
162 | cb->dest = hdr->dest; | |
163 | ||
164 | if (hdr->fc.security_enabled) { | |
165 | u64 key; | |
166 | ||
167 | pr_debug("seclevel %i\n", hdr->sec.level); | |
168 | ||
169 | switch (hdr->sec.key_id_mode) { | |
170 | case IEEE802154_SCF_KEY_IMPLICIT: | |
171 | pr_debug("implicit key\n"); | |
172 | break; | |
173 | ||
174 | case IEEE802154_SCF_KEY_INDEX: | |
175 | pr_debug("key %02x\n", hdr->sec.key_id); | |
176 | break; | |
177 | ||
178 | case IEEE802154_SCF_KEY_SHORT_INDEX: | |
179 | pr_debug("key %04x:%04x %02x\n", | |
180 | le32_to_cpu(hdr->sec.short_src) >> 16, | |
181 | le32_to_cpu(hdr->sec.short_src) & 0xffff, | |
182 | hdr->sec.key_id); | |
183 | break; | |
184 | ||
185 | case IEEE802154_SCF_KEY_HW_INDEX: | |
186 | key = swab64((__force u64)hdr->sec.extended_src); | |
187 | pr_debug("key source %8phC %02x\n", &key, | |
188 | hdr->sec.key_id); | |
189 | break; | |
190 | } | |
191 | } | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
196 | static void | |
be9d215f AA |
197 | __ieee802154_rx_handle_packet(struct ieee802154_local *local, |
198 | struct sk_buff *skb) | |
2a9820c9 AA |
199 | { |
200 | int ret; | |
201 | struct ieee802154_sub_if_data *sdata; | |
202 | struct ieee802154_hdr hdr; | |
203 | ||
be9d215f | 204 | ret = ieee802154_parse_frame_start(skb, &hdr); |
2a9820c9 AA |
205 | if (ret) { |
206 | pr_debug("got invalid frame\n"); | |
207 | kfree_skb(skb); | |
208 | return; | |
209 | } | |
210 | ||
2a9820c9 | 211 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
7c118c1a | 212 | if (sdata->vif.type != IEEE802154_DEV_WPAN || |
2a9820c9 AA |
213 | !netif_running(sdata->dev)) |
214 | continue; | |
215 | ||
be9d215f | 216 | ieee802154_subif_frame(sdata, skb, &hdr); |
2a9820c9 AA |
217 | skb = NULL; |
218 | break; | |
219 | } | |
2a9820c9 AA |
220 | |
221 | if (skb) | |
222 | kfree_skb(skb); | |
223 | } | |
224 | ||
4ca18be5 | 225 | static void |
be9d215f | 226 | ieee802154_monitors_rx(struct ieee802154_local *local, struct sk_buff *skb) |
2a9820c9 AA |
227 | { |
228 | struct sk_buff *skb2; | |
229 | struct ieee802154_sub_if_data *sdata; | |
2a9820c9 | 230 | |
9cf215d0 | 231 | skb_reset_mac_header(skb); |
75a46f0e | 232 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
c9ca6401 | 233 | skb->pkt_type = PACKET_OTHERHOST; |
702dcf99 AA |
234 | skb->protocol = htons(ETH_P_IEEE802154); |
235 | ||
2a9820c9 | 236 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
7c118c1a | 237 | if (sdata->vif.type != IEEE802154_DEV_MONITOR) |
20b48120 AA |
238 | continue; |
239 | ||
240 | if (!ieee802154_sdata_running(sdata)) | |
2a9820c9 AA |
241 | continue; |
242 | ||
243 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
05f7de67 AA |
244 | if (skb2) { |
245 | skb2->dev = sdata->dev; | |
246 | ieee802154_deliver_skb(skb2); | |
2a9820c9 | 247 | |
05f7de67 AA |
248 | sdata->dev->stats.rx_packets++; |
249 | sdata->dev->stats.rx_bytes += skb->len; | |
250 | } | |
2a9820c9 | 251 | } |
2a9820c9 AA |
252 | } |
253 | ||
469100d6 | 254 | void ieee802154_rx(struct ieee802154_hw *hw, struct sk_buff *skb) |
1cd829c8 | 255 | { |
60741361 | 256 | struct ieee802154_local *local = hw_to_local(hw); |
ec718f3d | 257 | u16 crc; |
1cd829c8 | 258 | |
469100d6 AA |
259 | WARN_ON_ONCE(softirq_count() == 0); |
260 | ||
b7889497 AA |
261 | /* TODO: When a transceiver omits the checksum here, we |
262 | * add an own calculated one. This is currently an ugly | |
263 | * solution because the monitor needs a crc here. | |
264 | */ | |
265 | if (local->hw.flags & IEEE802154_HW_RX_OMIT_CKSUM) { | |
ec718f3d | 266 | crc = crc_ccitt(0, skb->data, skb->len); |
b7889497 | 267 | put_unaligned_le16(crc, skb_put(skb, 2)); |
1cd829c8 | 268 | } |
269 | ||
e176b681 AA |
270 | rcu_read_lock(); |
271 | ||
be9d215f | 272 | ieee802154_monitors_rx(local, skb); |
2d3b5b0a | 273 | |
ec718f3d AA |
274 | /* Check if transceiver doesn't validate the checksum. |
275 | * If not we validate the checksum here. | |
276 | */ | |
277 | if (local->hw.flags & IEEE802154_HW_RX_DROP_BAD_CKSUM) { | |
278 | crc = crc_ccitt(0, skb->data, skb->len); | |
279 | if (crc) { | |
280 | rcu_read_unlock(); | |
281 | kfree_skb(skb); | |
282 | return; | |
283 | } | |
284 | } | |
b7889497 AA |
285 | /* remove crc */ |
286 | skb_trim(skb, skb->len - 2); | |
e176b681 | 287 | |
b7889497 | 288 | __ieee802154_rx_handle_packet(local, skb); |
2d3b5b0a | 289 | |
b7889497 | 290 | rcu_read_unlock(); |
1cd829c8 | 291 | } |
c5c47e67 | 292 | EXPORT_SYMBOL(ieee802154_rx); |
1cd829c8 | 293 | |
294 | void | |
5a504397 | 295 | ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb, u8 lqi) |
1cd829c8 | 296 | { |
60741361 | 297 | struct ieee802154_local *local = hw_to_local(hw); |
1cd829c8 | 298 | |
c5c47e67 AA |
299 | mac_cb(skb)->lqi = lqi; |
300 | skb->pkt_type = IEEE802154_RX_MSG; | |
301 | skb_queue_tail(&local->skb_queue, skb); | |
302 | tasklet_schedule(&local->tasklet); | |
1cd829c8 | 303 | } |
304 | EXPORT_SYMBOL(ieee802154_rx_irqsafe); |