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