6lowpan.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. /*
  2. * Copyright 2011, Siemens AG
  3. * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  4. */
  5. /*
  6. * Based on patches from Jon Smirl <jonsmirl@gmail.com>
  7. * Copyright (c) 2011 Jon Smirl <jonsmirl@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. /* Jon's code is based on 6lowpan implementation for Contiki which is:
  23. * Copyright (c) 2008, Swedish Institute of Computer Science.
  24. * All rights reserved.
  25. *
  26. * Redistribution and use in source and binary forms, with or without
  27. * modification, are permitted provided that the following conditions
  28. * are met:
  29. * 1. Redistributions of source code must retain the above copyright
  30. * notice, this list of conditions and the following disclaimer.
  31. * 2. Redistributions in binary form must reproduce the above copyright
  32. * notice, this list of conditions and the following disclaimer in the
  33. * documentation and/or other materials provided with the distribution.
  34. * 3. Neither the name of the Institute nor the names of its contributors
  35. * may be used to endorse or promote products derived from this software
  36. * without specific prior written permission.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  39. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  41. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  42. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  43. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  44. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  46. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  47. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  48. * SUCH DAMAGE.
  49. */
  50. #include <linux/bitops.h>
  51. #include <linux/if_arp.h>
  52. #include <linux/module.h>
  53. #include <linux/moduleparam.h>
  54. #include <linux/netdevice.h>
  55. #include <linux/etherdevice.h>
  56. #include <net/af_ieee802154.h>
  57. #include <net/ieee802154.h>
  58. #include <net/ieee802154_netdev.h>
  59. #include <net/ipv6.h>
  60. #include "6lowpan.h"
  61. /* TTL uncompression values */
  62. static const u8 lowpan_ttl_values[] = {0, 1, 64, 255};
  63. static LIST_HEAD(lowpan_devices);
  64. /*
  65. * Uncompression of linklocal:
  66. * 0 -> 16 bytes from packet
  67. * 1 -> 2 bytes from prefix - bunch of zeroes and 8 from packet
  68. * 2 -> 2 bytes from prefix - zeroes + 2 from packet
  69. * 3 -> 2 bytes from prefix - infer 8 bytes from lladdr
  70. *
  71. * NOTE: => the uncompress function does change 0xf to 0x10
  72. * NOTE: 0x00 => no-autoconfig => unspecified
  73. */
  74. static const u8 lowpan_unc_llconf[] = {0x0f, 0x28, 0x22, 0x20};
  75. /*
  76. * Uncompression of ctx-based:
  77. * 0 -> 0 bits from packet [unspecified / reserved]
  78. * 1 -> 8 bytes from prefix - bunch of zeroes and 8 from packet
  79. * 2 -> 8 bytes from prefix - zeroes + 2 from packet
  80. * 3 -> 8 bytes from prefix - infer 8 bytes from lladdr
  81. */
  82. static const u8 lowpan_unc_ctxconf[] = {0x00, 0x88, 0x82, 0x80};
  83. /*
  84. * Uncompression of ctx-base
  85. * 0 -> 0 bits from packet
  86. * 1 -> 2 bytes from prefix - bunch of zeroes 5 from packet
  87. * 2 -> 2 bytes from prefix - zeroes + 3 from packet
  88. * 3 -> 2 bytes from prefix - infer 1 bytes from lladdr
  89. */
  90. static const u8 lowpan_unc_mxconf[] = {0x0f, 0x25, 0x23, 0x21};
  91. /* Link local prefix */
  92. static const u8 lowpan_llprefix[] = {0xfe, 0x80};
  93. /* private device info */
  94. struct lowpan_dev_info {
  95. struct net_device *real_dev; /* real WPAN device ptr */
  96. struct mutex dev_list_mtx; /* mutex for list ops */
  97. };
  98. struct lowpan_dev_record {
  99. struct net_device *ldev;
  100. struct list_head list;
  101. };
  102. struct lowpan_fragment {
  103. struct sk_buff *skb; /* skb to be assembled */
  104. spinlock_t lock; /* concurency lock */
  105. u16 length; /* length to be assemled */
  106. u32 bytes_rcv; /* bytes received */
  107. u16 tag; /* current fragment tag */
  108. struct timer_list timer; /* assembling timer */
  109. struct list_head list; /* fragments list */
  110. };
  111. static unsigned short fragment_tag;
  112. static LIST_HEAD(lowpan_fragments);
  113. spinlock_t flist_lock;
  114. static inline struct
  115. lowpan_dev_info *lowpan_dev_info(const struct net_device *dev)
  116. {
  117. return netdev_priv(dev);
  118. }
  119. static inline void lowpan_address_flip(u8 *src, u8 *dest)
  120. {
  121. int i;
  122. for (i = 0; i < IEEE802154_ADDR_LEN; i++)
  123. (dest)[IEEE802154_ADDR_LEN - i - 1] = (src)[i];
  124. }
  125. /* list of all 6lowpan devices, uses for package delivering */
  126. /* print data in line */
  127. static inline void lowpan_raw_dump_inline(const char *caller, char *msg,
  128. unsigned char *buf, int len)
  129. {
  130. #ifdef DEBUG
  131. if (msg)
  132. pr_debug("(%s) %s: ", caller, msg);
  133. print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_NONE,
  134. 16, 1, buf, len, false);
  135. #endif /* DEBUG */
  136. }
  137. /*
  138. * print data in a table format:
  139. *
  140. * addr: xx xx xx xx xx xx
  141. * addr: xx xx xx xx xx xx
  142. * ...
  143. */
  144. static inline void lowpan_raw_dump_table(const char *caller, char *msg,
  145. unsigned char *buf, int len)
  146. {
  147. #ifdef DEBUG
  148. if (msg)
  149. pr_debug("(%s) %s:\n", caller, msg);
  150. print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET,
  151. 16, 1, buf, len, false);
  152. #endif /* DEBUG */
  153. }
  154. static u8
  155. lowpan_compress_addr_64(u8 **hc06_ptr, u8 shift, const struct in6_addr *ipaddr,
  156. const unsigned char *lladdr)
  157. {
  158. u8 val = 0;
  159. if (is_addr_mac_addr_based(ipaddr, lladdr))
  160. val = 3; /* 0-bits */
  161. else if (lowpan_is_iid_16_bit_compressable(ipaddr)) {
  162. /* compress IID to 16 bits xxxx::XXXX */
  163. memcpy(*hc06_ptr, &ipaddr->s6_addr16[7], 2);
  164. *hc06_ptr += 2;
  165. val = 2; /* 16-bits */
  166. } else {
  167. /* do not compress IID => xxxx::IID */
  168. memcpy(*hc06_ptr, &ipaddr->s6_addr16[4], 8);
  169. *hc06_ptr += 8;
  170. val = 1; /* 64-bits */
  171. }
  172. return rol8(val, shift);
  173. }
  174. static void
  175. lowpan_uip_ds6_set_addr_iid(struct in6_addr *ipaddr, unsigned char *lladdr)
  176. {
  177. memcpy(&ipaddr->s6_addr[8], lladdr, IEEE802154_ALEN);
  178. /* second bit-flip (Universe/Local) is done according RFC2464 */
  179. ipaddr->s6_addr[8] ^= 0x02;
  180. }
  181. /*
  182. * Uncompress addresses based on a prefix and a postfix with zeroes in
  183. * between. If the postfix is zero in length it will use the link address
  184. * to configure the IP address (autoconf style).
  185. * pref_post_count takes a byte where the first nibble specify prefix count
  186. * and the second postfix count (NOTE: 15/0xf => 16 bytes copy).
  187. */
  188. static int
  189. lowpan_uncompress_addr(struct sk_buff *skb, struct in6_addr *ipaddr,
  190. u8 const *prefix, u8 pref_post_count, unsigned char *lladdr)
  191. {
  192. u8 prefcount = pref_post_count >> 4;
  193. u8 postcount = pref_post_count & 0x0f;
  194. /* full nibble 15 => 16 */
  195. prefcount = (prefcount == 15 ? 16 : prefcount);
  196. postcount = (postcount == 15 ? 16 : postcount);
  197. if (lladdr)
  198. lowpan_raw_dump_inline(__func__, "linklocal address",
  199. lladdr, IEEE802154_ALEN);
  200. if (prefcount > 0)
  201. memcpy(ipaddr, prefix, prefcount);
  202. if (prefcount + postcount < 16)
  203. memset(&ipaddr->s6_addr[prefcount], 0,
  204. 16 - (prefcount + postcount));
  205. if (postcount > 0) {
  206. memcpy(&ipaddr->s6_addr[16 - postcount], skb->data, postcount);
  207. skb_pull(skb, postcount);
  208. } else if (prefcount > 0) {
  209. if (lladdr == NULL)
  210. return -EINVAL;
  211. /* no IID based configuration if no prefix and no data */
  212. lowpan_uip_ds6_set_addr_iid(ipaddr, lladdr);
  213. }
  214. pr_debug("(%s): uncompressing %d + %d => ", __func__, prefcount,
  215. postcount);
  216. lowpan_raw_dump_inline(NULL, NULL, ipaddr->s6_addr, 16);
  217. return 0;
  218. }
  219. static void
  220. lowpan_compress_udp_header(u8 **hc06_ptr, struct sk_buff *skb)
  221. {
  222. struct udphdr *uh = udp_hdr(skb);
  223. pr_debug("(%s): UDP header compression\n", __func__);
  224. if (((uh->source & LOWPAN_NHC_UDP_4BIT_MASK) ==
  225. LOWPAN_NHC_UDP_4BIT_PORT) &&
  226. ((uh->dest & LOWPAN_NHC_UDP_4BIT_MASK) ==
  227. LOWPAN_NHC_UDP_4BIT_PORT)) {
  228. pr_debug("(%s): both ports compression to 4 bits\n", __func__);
  229. **hc06_ptr = LOWPAN_NHC_UDP_CS_P_11;
  230. **(hc06_ptr + 1) = /* subtraction is faster */
  231. (u8)((uh->dest - LOWPAN_NHC_UDP_4BIT_PORT) +
  232. ((uh->source & LOWPAN_NHC_UDP_4BIT_PORT) << 4));
  233. *hc06_ptr += 2;
  234. } else if ((uh->dest & LOWPAN_NHC_UDP_8BIT_MASK) ==
  235. LOWPAN_NHC_UDP_8BIT_PORT) {
  236. pr_debug("(%s): remove 8 bits of dest\n", __func__);
  237. **hc06_ptr = LOWPAN_NHC_UDP_CS_P_01;
  238. memcpy(*hc06_ptr + 1, &uh->source, 2);
  239. **(hc06_ptr + 3) = (u8)(uh->dest - LOWPAN_NHC_UDP_8BIT_PORT);
  240. *hc06_ptr += 4;
  241. } else if ((uh->source & LOWPAN_NHC_UDP_8BIT_MASK) ==
  242. LOWPAN_NHC_UDP_8BIT_PORT) {
  243. pr_debug("(%s): remove 8 bits of source\n", __func__);
  244. **hc06_ptr = LOWPAN_NHC_UDP_CS_P_10;
  245. memcpy(*hc06_ptr + 1, &uh->dest, 2);
  246. **(hc06_ptr + 3) = (u8)(uh->source - LOWPAN_NHC_UDP_8BIT_PORT);
  247. *hc06_ptr += 4;
  248. } else {
  249. pr_debug("(%s): can't compress header\n", __func__);
  250. **hc06_ptr = LOWPAN_NHC_UDP_CS_P_00;
  251. memcpy(*hc06_ptr + 1, &uh->source, 2);
  252. memcpy(*hc06_ptr + 3, &uh->dest, 2);
  253. *hc06_ptr += 5;
  254. }
  255. /* checksum is always inline */
  256. memcpy(*hc06_ptr, &uh->check, 2);
  257. *hc06_ptr += 2;
  258. }
  259. static u8 lowpan_fetch_skb_u8(struct sk_buff *skb)
  260. {
  261. u8 ret;
  262. ret = skb->data[0];
  263. skb_pull(skb, 1);
  264. return ret;
  265. }
  266. static u16 lowpan_fetch_skb_u16(struct sk_buff *skb)
  267. {
  268. u16 ret;
  269. BUG_ON(!pskb_may_pull(skb, 2));
  270. ret = skb->data[0] | (skb->data[1] << 8);
  271. skb_pull(skb, 2);
  272. return ret;
  273. }
  274. static int
  275. lowpan_uncompress_udp_header(struct sk_buff *skb)
  276. {
  277. struct udphdr *uh = udp_hdr(skb);
  278. u8 tmp;
  279. tmp = lowpan_fetch_skb_u8(skb);
  280. if ((tmp & LOWPAN_NHC_UDP_MASK) == LOWPAN_NHC_UDP_ID) {
  281. pr_debug("(%s): UDP header uncompression\n", __func__);
  282. switch (tmp & LOWPAN_NHC_UDP_CS_P_11) {
  283. case LOWPAN_NHC_UDP_CS_P_00:
  284. memcpy(&uh->source, &skb->data[0], 2);
  285. memcpy(&uh->dest, &skb->data[2], 2);
  286. skb_pull(skb, 4);
  287. break;
  288. case LOWPAN_NHC_UDP_CS_P_01:
  289. memcpy(&uh->source, &skb->data[0], 2);
  290. uh->dest =
  291. skb->data[2] + LOWPAN_NHC_UDP_8BIT_PORT;
  292. skb_pull(skb, 3);
  293. break;
  294. case LOWPAN_NHC_UDP_CS_P_10:
  295. uh->source = skb->data[0] + LOWPAN_NHC_UDP_8BIT_PORT;
  296. memcpy(&uh->dest, &skb->data[1], 2);
  297. skb_pull(skb, 3);
  298. break;
  299. case LOWPAN_NHC_UDP_CS_P_11:
  300. uh->source =
  301. LOWPAN_NHC_UDP_4BIT_PORT + (skb->data[0] >> 4);
  302. uh->dest =
  303. LOWPAN_NHC_UDP_4BIT_PORT + (skb->data[0] & 0x0f);
  304. skb_pull(skb, 1);
  305. break;
  306. default:
  307. pr_debug("(%s) ERROR: unknown UDP format\n", __func__);
  308. goto err;
  309. break;
  310. }
  311. pr_debug("(%s): uncompressed UDP ports: src = %d, dst = %d\n",
  312. __func__, uh->source, uh->dest);
  313. /* copy checksum */
  314. memcpy(&uh->check, &skb->data[0], 2);
  315. skb_pull(skb, 2);
  316. } else {
  317. pr_debug("(%s): ERROR: unsupported NH format\n", __func__);
  318. goto err;
  319. }
  320. return 0;
  321. err:
  322. return -EINVAL;
  323. }
  324. static int lowpan_header_create(struct sk_buff *skb,
  325. struct net_device *dev,
  326. unsigned short type, const void *_daddr,
  327. const void *_saddr, unsigned int len)
  328. {
  329. u8 tmp, iphc0, iphc1, *hc06_ptr;
  330. struct ipv6hdr *hdr;
  331. const u8 *saddr = _saddr;
  332. const u8 *daddr = _daddr;
  333. u8 *head;
  334. struct ieee802154_addr sa, da;
  335. if (type != ETH_P_IPV6)
  336. return 0;
  337. /* TODO:
  338. * if this package isn't ipv6 one, where should it be routed?
  339. */
  340. head = kzalloc(100, GFP_KERNEL);
  341. if (head == NULL)
  342. return -ENOMEM;
  343. hdr = ipv6_hdr(skb);
  344. hc06_ptr = head + 2;
  345. pr_debug("(%s): IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
  346. "\tnexthdr = 0x%02x\n\thop_lim = %d\n", __func__,
  347. hdr->version, ntohs(hdr->payload_len), hdr->nexthdr,
  348. hdr->hop_limit);
  349. lowpan_raw_dump_table(__func__, "raw skb network header dump",
  350. skb_network_header(skb), sizeof(struct ipv6hdr));
  351. if (!saddr)
  352. saddr = dev->dev_addr;
  353. lowpan_raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8);
  354. /*
  355. * As we copy some bit-length fields, in the IPHC encoding bytes,
  356. * we sometimes use |=
  357. * If the field is 0, and the current bit value in memory is 1,
  358. * this does not work. We therefore reset the IPHC encoding here
  359. */
  360. iphc0 = LOWPAN_DISPATCH_IPHC;
  361. iphc1 = 0;
  362. /* TODO: context lookup */
  363. lowpan_raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8);
  364. /*
  365. * Traffic class, flow label
  366. * If flow label is 0, compress it. If traffic class is 0, compress it
  367. * We have to process both in the same time as the offset of traffic
  368. * class depends on the presence of version and flow label
  369. */
  370. /* hc06 format of TC is ECN | DSCP , original one is DSCP | ECN */
  371. tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
  372. tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
  373. if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
  374. (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
  375. /* flow label can be compressed */
  376. iphc0 |= LOWPAN_IPHC_FL_C;
  377. if ((hdr->priority == 0) &&
  378. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  379. /* compress (elide) all */
  380. iphc0 |= LOWPAN_IPHC_TC_C;
  381. } else {
  382. /* compress only the flow label */
  383. *hc06_ptr = tmp;
  384. hc06_ptr += 1;
  385. }
  386. } else {
  387. /* Flow label cannot be compressed */
  388. if ((hdr->priority == 0) &&
  389. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  390. /* compress only traffic class */
  391. iphc0 |= LOWPAN_IPHC_TC_C;
  392. *hc06_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
  393. memcpy(hc06_ptr + 1, &hdr->flow_lbl[1], 2);
  394. hc06_ptr += 3;
  395. } else {
  396. /* compress nothing */
  397. memcpy(hc06_ptr, &hdr, 4);
  398. /* replace the top byte with new ECN | DSCP format */
  399. *hc06_ptr = tmp;
  400. hc06_ptr += 4;
  401. }
  402. }
  403. /* NOTE: payload length is always compressed */
  404. /* Next Header is compress if UDP */
  405. if (hdr->nexthdr == UIP_PROTO_UDP)
  406. iphc0 |= LOWPAN_IPHC_NH_C;
  407. if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
  408. *hc06_ptr = hdr->nexthdr;
  409. hc06_ptr += 1;
  410. }
  411. /*
  412. * Hop limit
  413. * if 1: compress, encoding is 01
  414. * if 64: compress, encoding is 10
  415. * if 255: compress, encoding is 11
  416. * else do not compress
  417. */
  418. switch (hdr->hop_limit) {
  419. case 1:
  420. iphc0 |= LOWPAN_IPHC_TTL_1;
  421. break;
  422. case 64:
  423. iphc0 |= LOWPAN_IPHC_TTL_64;
  424. break;
  425. case 255:
  426. iphc0 |= LOWPAN_IPHC_TTL_255;
  427. break;
  428. default:
  429. *hc06_ptr = hdr->hop_limit;
  430. break;
  431. }
  432. /* source address compression */
  433. if (is_addr_unspecified(&hdr->saddr)) {
  434. pr_debug("(%s): source address is unspecified, setting SAC\n",
  435. __func__);
  436. iphc1 |= LOWPAN_IPHC_SAC;
  437. /* TODO: context lookup */
  438. } else if (is_addr_link_local(&hdr->saddr)) {
  439. pr_debug("(%s): source address is link-local\n", __func__);
  440. iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
  441. LOWPAN_IPHC_SAM_BIT, &hdr->saddr, saddr);
  442. } else {
  443. pr_debug("(%s): send the full source address\n", __func__);
  444. memcpy(hc06_ptr, &hdr->saddr.s6_addr16[0], 16);
  445. hc06_ptr += 16;
  446. }
  447. /* destination address compression */
  448. if (is_addr_mcast(&hdr->daddr)) {
  449. pr_debug("(%s): destination address is multicast", __func__);
  450. iphc1 |= LOWPAN_IPHC_M;
  451. if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
  452. pr_debug("compressed to 1 octet\n");
  453. iphc1 |= LOWPAN_IPHC_DAM_11;
  454. /* use last byte */
  455. *hc06_ptr = hdr->daddr.s6_addr[15];
  456. hc06_ptr += 1;
  457. } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
  458. pr_debug("compressed to 4 octets\n");
  459. iphc1 |= LOWPAN_IPHC_DAM_10;
  460. /* second byte + the last three */
  461. *hc06_ptr = hdr->daddr.s6_addr[1];
  462. memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[13], 3);
  463. hc06_ptr += 4;
  464. } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
  465. pr_debug("compressed to 6 octets\n");
  466. iphc1 |= LOWPAN_IPHC_DAM_01;
  467. /* second byte + the last five */
  468. *hc06_ptr = hdr->daddr.s6_addr[1];
  469. memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[11], 5);
  470. hc06_ptr += 6;
  471. } else {
  472. pr_debug("using full address\n");
  473. iphc1 |= LOWPAN_IPHC_DAM_00;
  474. memcpy(hc06_ptr, &hdr->daddr.s6_addr[0], 16);
  475. hc06_ptr += 16;
  476. }
  477. } else {
  478. pr_debug("(%s): destination address is unicast: ", __func__);
  479. /* TODO: context lookup */
  480. if (is_addr_link_local(&hdr->daddr)) {
  481. pr_debug("destination address is link-local\n");
  482. iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
  483. LOWPAN_IPHC_DAM_BIT, &hdr->daddr, daddr);
  484. } else {
  485. pr_debug("using full address\n");
  486. memcpy(hc06_ptr, &hdr->daddr.s6_addr16[0], 16);
  487. hc06_ptr += 16;
  488. }
  489. }
  490. /* UDP header compression */
  491. if (hdr->nexthdr == UIP_PROTO_UDP)
  492. lowpan_compress_udp_header(&hc06_ptr, skb);
  493. head[0] = iphc0;
  494. head[1] = iphc1;
  495. skb_pull(skb, sizeof(struct ipv6hdr));
  496. memcpy(skb_push(skb, hc06_ptr - head), head, hc06_ptr - head);
  497. kfree(head);
  498. lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
  499. skb->len);
  500. /*
  501. * NOTE1: I'm still unsure about the fact that compression and WPAN
  502. * header are created here and not later in the xmit. So wait for
  503. * an opinion of net maintainers.
  504. */
  505. /*
  506. * NOTE2: to be absolutely correct, we must derive PANid information
  507. * from MAC subif of the 'dev' and 'real_dev' network devices, but
  508. * this isn't implemented in mainline yet, so currently we assign 0xff
  509. */
  510. {
  511. /* prepare wpan address data */
  512. sa.addr_type = IEEE802154_ADDR_LONG;
  513. sa.pan_id = 0xff;
  514. da.addr_type = IEEE802154_ADDR_LONG;
  515. da.pan_id = 0xff;
  516. memcpy(&(da.hwaddr), daddr, 8);
  517. memcpy(&(sa.hwaddr), saddr, 8);
  518. mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA;
  519. return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
  520. type, (void *)&da, (void *)&sa, skb->len);
  521. }
  522. }
  523. static int lowpan_skb_deliver(struct sk_buff *skb, struct ipv6hdr *hdr)
  524. {
  525. struct sk_buff *new;
  526. struct lowpan_dev_record *entry;
  527. int stat = NET_RX_SUCCESS;
  528. new = skb_copy_expand(skb, sizeof(struct ipv6hdr), skb_tailroom(skb),
  529. GFP_ATOMIC);
  530. kfree_skb(skb);
  531. if (!new)
  532. return -ENOMEM;
  533. skb_push(new, sizeof(struct ipv6hdr));
  534. skb_reset_network_header(new);
  535. skb_copy_to_linear_data(new, hdr, sizeof(struct ipv6hdr));
  536. new->protocol = htons(ETH_P_IPV6);
  537. new->pkt_type = PACKET_HOST;
  538. rcu_read_lock();
  539. list_for_each_entry_rcu(entry, &lowpan_devices, list)
  540. if (lowpan_dev_info(entry->ldev)->real_dev == new->dev) {
  541. skb = skb_copy(new, GFP_ATOMIC);
  542. if (!skb) {
  543. stat = -ENOMEM;
  544. break;
  545. }
  546. skb->dev = entry->ldev;
  547. stat = netif_rx(skb);
  548. }
  549. rcu_read_unlock();
  550. kfree_skb(new);
  551. return stat;
  552. }
  553. static void lowpan_fragment_timer_expired(unsigned long entry_addr)
  554. {
  555. struct lowpan_fragment *entry = (struct lowpan_fragment *)entry_addr;
  556. pr_debug("%s: timer expired for frame with tag %d\n", __func__,
  557. entry->tag);
  558. spin_lock(&flist_lock);
  559. list_del(&entry->list);
  560. spin_unlock(&flist_lock);
  561. dev_kfree_skb(entry->skb);
  562. kfree(entry);
  563. }
  564. static int
  565. lowpan_process_data(struct sk_buff *skb)
  566. {
  567. struct ipv6hdr hdr;
  568. u8 tmp, iphc0, iphc1, num_context = 0;
  569. u8 *_saddr, *_daddr;
  570. int err;
  571. lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
  572. skb->len);
  573. /* at least two bytes will be used for the encoding */
  574. if (skb->len < 2)
  575. goto drop;
  576. iphc0 = lowpan_fetch_skb_u8(skb);
  577. /* fragments assembling */
  578. switch (iphc0 & LOWPAN_DISPATCH_MASK) {
  579. case LOWPAN_DISPATCH_FRAG1:
  580. case LOWPAN_DISPATCH_FRAGN:
  581. {
  582. struct lowpan_fragment *frame;
  583. u8 len, offset;
  584. u16 tag;
  585. bool found = false;
  586. len = lowpan_fetch_skb_u8(skb); /* frame length */
  587. tag = lowpan_fetch_skb_u16(skb);
  588. /*
  589. * check if frame assembling with the same tag is
  590. * already in progress
  591. */
  592. spin_lock(&flist_lock);
  593. list_for_each_entry(frame, &lowpan_fragments, list)
  594. if (frame->tag == tag) {
  595. found = true;
  596. break;
  597. }
  598. /* alloc new frame structure */
  599. if (!found) {
  600. frame = kzalloc(sizeof(struct lowpan_fragment),
  601. GFP_ATOMIC);
  602. if (!frame)
  603. goto unlock_and_drop;
  604. INIT_LIST_HEAD(&frame->list);
  605. frame->length = (iphc0 & 7) | (len << 3);
  606. frame->tag = tag;
  607. /* allocate buffer for frame assembling */
  608. frame->skb = alloc_skb(frame->length +
  609. sizeof(struct ipv6hdr), GFP_ATOMIC);
  610. if (!frame->skb) {
  611. kfree(frame);
  612. goto unlock_and_drop;
  613. }
  614. frame->skb->priority = skb->priority;
  615. frame->skb->dev = skb->dev;
  616. /* reserve headroom for uncompressed ipv6 header */
  617. skb_reserve(frame->skb, sizeof(struct ipv6hdr));
  618. skb_put(frame->skb, frame->length);
  619. init_timer(&frame->timer);
  620. /* time out is the same as for ipv6 - 60 sec */
  621. frame->timer.expires = jiffies + LOWPAN_FRAG_TIMEOUT;
  622. frame->timer.data = (unsigned long)frame;
  623. frame->timer.function = lowpan_fragment_timer_expired;
  624. add_timer(&frame->timer);
  625. list_add_tail(&frame->list, &lowpan_fragments);
  626. }
  627. if ((iphc0 & LOWPAN_DISPATCH_MASK) == LOWPAN_DISPATCH_FRAG1)
  628. goto unlock_and_drop;
  629. offset = lowpan_fetch_skb_u8(skb); /* fetch offset */
  630. /* if payload fits buffer, copy it */
  631. if (likely((offset * 8 + skb->len) <= frame->length))
  632. skb_copy_to_linear_data_offset(frame->skb, offset * 8,
  633. skb->data, skb->len);
  634. else
  635. goto unlock_and_drop;
  636. frame->bytes_rcv += skb->len;
  637. /* frame assembling complete */
  638. if ((frame->bytes_rcv == frame->length) &&
  639. frame->timer.expires > jiffies) {
  640. /* if timer haven't expired - first of all delete it */
  641. del_timer(&frame->timer);
  642. list_del(&frame->list);
  643. spin_unlock(&flist_lock);
  644. dev_kfree_skb(skb);
  645. skb = frame->skb;
  646. kfree(frame);
  647. iphc0 = lowpan_fetch_skb_u8(skb);
  648. break;
  649. }
  650. spin_unlock(&flist_lock);
  651. return kfree_skb(skb), 0;
  652. }
  653. default:
  654. break;
  655. }
  656. iphc1 = lowpan_fetch_skb_u8(skb);
  657. _saddr = mac_cb(skb)->sa.hwaddr;
  658. _daddr = mac_cb(skb)->da.hwaddr;
  659. pr_debug("(%s): iphc0 = %02x, iphc1 = %02x\n", __func__, iphc0, iphc1);
  660. /* another if the CID flag is set */
  661. if (iphc1 & LOWPAN_IPHC_CID) {
  662. pr_debug("(%s): CID flag is set, increase header with one\n",
  663. __func__);
  664. if (!skb->len)
  665. goto drop;
  666. num_context = lowpan_fetch_skb_u8(skb);
  667. }
  668. hdr.version = 6;
  669. /* Traffic Class and Flow Label */
  670. switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
  671. /*
  672. * Traffic Class and FLow Label carried in-line
  673. * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
  674. */
  675. case 0: /* 00b */
  676. if (!skb->len)
  677. goto drop;
  678. tmp = lowpan_fetch_skb_u8(skb);
  679. memcpy(&hdr.flow_lbl, &skb->data[0], 3);
  680. skb_pull(skb, 3);
  681. hdr.priority = ((tmp >> 2) & 0x0f);
  682. hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
  683. (hdr.flow_lbl[0] & 0x0f);
  684. break;
  685. /*
  686. * Traffic class carried in-line
  687. * ECN + DSCP (1 byte), Flow Label is elided
  688. */
  689. case 2: /* 10b */
  690. if (!skb->len)
  691. goto drop;
  692. tmp = lowpan_fetch_skb_u8(skb);
  693. hdr.priority = ((tmp >> 2) & 0x0f);
  694. hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
  695. hdr.flow_lbl[1] = 0;
  696. hdr.flow_lbl[2] = 0;
  697. break;
  698. /*
  699. * Flow Label carried in-line
  700. * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
  701. */
  702. case 1: /* 01b */
  703. if (!skb->len)
  704. goto drop;
  705. tmp = lowpan_fetch_skb_u8(skb);
  706. hdr.flow_lbl[0] = (skb->data[0] & 0x0F) | ((tmp >> 2) & 0x30);
  707. memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
  708. skb_pull(skb, 2);
  709. break;
  710. /* Traffic Class and Flow Label are elided */
  711. case 3: /* 11b */
  712. hdr.priority = 0;
  713. hdr.flow_lbl[0] = 0;
  714. hdr.flow_lbl[1] = 0;
  715. hdr.flow_lbl[2] = 0;
  716. break;
  717. default:
  718. break;
  719. }
  720. /* Next Header */
  721. if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
  722. /* Next header is carried inline */
  723. if (!skb->len)
  724. goto drop;
  725. hdr.nexthdr = lowpan_fetch_skb_u8(skb);
  726. pr_debug("(%s): NH flag is set, next header is carried "
  727. "inline: %02x\n", __func__, hdr.nexthdr);
  728. }
  729. /* Hop Limit */
  730. if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I)
  731. hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
  732. else {
  733. if (!skb->len)
  734. goto drop;
  735. hdr.hop_limit = lowpan_fetch_skb_u8(skb);
  736. }
  737. /* Extract SAM to the tmp variable */
  738. tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
  739. /* Source address uncompression */
  740. pr_debug("(%s): source address stateless compression\n", __func__);
  741. err = lowpan_uncompress_addr(skb, &hdr.saddr, lowpan_llprefix,
  742. lowpan_unc_llconf[tmp], skb->data);
  743. if (err)
  744. goto drop;
  745. /* Extract DAM to the tmp variable */
  746. tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
  747. /* check for Multicast Compression */
  748. if (iphc1 & LOWPAN_IPHC_M) {
  749. if (iphc1 & LOWPAN_IPHC_DAC) {
  750. pr_debug("(%s): destination address context-based "
  751. "multicast compression\n", __func__);
  752. /* TODO: implement this */
  753. } else {
  754. u8 prefix[] = {0xff, 0x02};
  755. pr_debug("(%s): destination address non-context-based"
  756. " multicast compression\n", __func__);
  757. if (0 < tmp && tmp < 3) {
  758. if (!skb->len)
  759. goto drop;
  760. else
  761. prefix[1] = lowpan_fetch_skb_u8(skb);
  762. }
  763. err = lowpan_uncompress_addr(skb, &hdr.daddr, prefix,
  764. lowpan_unc_mxconf[tmp], NULL);
  765. if (err)
  766. goto drop;
  767. }
  768. } else {
  769. pr_debug("(%s): destination address stateless compression\n",
  770. __func__);
  771. err = lowpan_uncompress_addr(skb, &hdr.daddr, lowpan_llprefix,
  772. lowpan_unc_llconf[tmp], skb->data);
  773. if (err)
  774. goto drop;
  775. }
  776. /* UDP data uncompression */
  777. if (iphc0 & LOWPAN_IPHC_NH_C)
  778. if (lowpan_uncompress_udp_header(skb))
  779. goto drop;
  780. /* Not fragmented package */
  781. hdr.payload_len = htons(skb->len);
  782. pr_debug("(%s): skb headroom size = %d, data length = %d\n", __func__,
  783. skb_headroom(skb), skb->len);
  784. pr_debug("(%s): IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
  785. "nexthdr = 0x%02x\n\thop_lim = %d\n", __func__, hdr.version,
  786. ntohs(hdr.payload_len), hdr.nexthdr, hdr.hop_limit);
  787. lowpan_raw_dump_table(__func__, "raw header dump", (u8 *)&hdr,
  788. sizeof(hdr));
  789. return lowpan_skb_deliver(skb, &hdr);
  790. unlock_and_drop:
  791. spin_unlock(&flist_lock);
  792. drop:
  793. kfree_skb(skb);
  794. return -EINVAL;
  795. }
  796. static int lowpan_get_mac_header_length(struct sk_buff *skb)
  797. {
  798. /*
  799. * Currently long addressing mode is supported only, so the overall
  800. * header size is 21:
  801. * FC SeqNum DPAN DA SA Sec
  802. * 2 + 1 + 2 + 8 + 8 + 0 = 21
  803. */
  804. return 21;
  805. }
  806. static int
  807. lowpan_fragment_xmit(struct sk_buff *skb, u8 *head,
  808. int mlen, int plen, int offset)
  809. {
  810. struct sk_buff *frag;
  811. int hlen, ret;
  812. /* if payload length is zero, therefore it's a first fragment */
  813. hlen = (plen == 0 ? LOWPAN_FRAG1_HEAD_SIZE : LOWPAN_FRAGN_HEAD_SIZE);
  814. lowpan_raw_dump_inline(__func__, "6lowpan fragment header", head, hlen);
  815. frag = dev_alloc_skb(hlen + mlen + plen + IEEE802154_MFR_SIZE);
  816. if (!frag)
  817. return -ENOMEM;
  818. frag->priority = skb->priority;
  819. frag->dev = skb->dev;
  820. /* copy header, MFR and payload */
  821. memcpy(skb_put(frag, mlen), skb->data, mlen);
  822. memcpy(skb_put(frag, hlen), head, hlen);
  823. if (plen)
  824. skb_copy_from_linear_data_offset(skb, offset + mlen,
  825. skb_put(frag, plen), plen);
  826. lowpan_raw_dump_table(__func__, " raw fragment dump", frag->data,
  827. frag->len);
  828. ret = dev_queue_xmit(frag);
  829. return ret;
  830. }
  831. static int
  832. lowpan_skb_fragmentation(struct sk_buff *skb)
  833. {
  834. int err, header_length, payload_length, tag, offset = 0;
  835. u8 head[5];
  836. header_length = lowpan_get_mac_header_length(skb);
  837. payload_length = skb->len - header_length;
  838. tag = fragment_tag++;
  839. /* first fragment header */
  840. head[0] = LOWPAN_DISPATCH_FRAG1 | (payload_length & 0x7);
  841. head[1] = (payload_length >> 3) & 0xff;
  842. head[2] = tag & 0xff;
  843. head[3] = tag >> 8;
  844. err = lowpan_fragment_xmit(skb, head, header_length, 0, 0);
  845. /* next fragment header */
  846. head[0] &= ~LOWPAN_DISPATCH_FRAG1;
  847. head[0] |= LOWPAN_DISPATCH_FRAGN;
  848. while ((payload_length - offset > 0) && (err >= 0)) {
  849. int len = LOWPAN_FRAG_SIZE;
  850. head[4] = offset / 8;
  851. if (payload_length - offset < len)
  852. len = payload_length - offset;
  853. err = lowpan_fragment_xmit(skb, head, header_length,
  854. len, offset);
  855. offset += len;
  856. }
  857. return err;
  858. }
  859. static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
  860. {
  861. int err = -1;
  862. pr_debug("(%s): package xmit\n", __func__);
  863. skb->dev = lowpan_dev_info(dev)->real_dev;
  864. if (skb->dev == NULL) {
  865. pr_debug("(%s) ERROR: no real wpan device found\n", __func__);
  866. goto error;
  867. }
  868. if (skb->len <= IEEE802154_MTU) {
  869. err = dev_queue_xmit(skb);
  870. goto out;
  871. }
  872. pr_debug("(%s): frame is too big, fragmentation is needed\n",
  873. __func__);
  874. err = lowpan_skb_fragmentation(skb);
  875. error:
  876. dev_kfree_skb(skb);
  877. out:
  878. if (err < 0)
  879. pr_debug("(%s): ERROR: xmit failed\n", __func__);
  880. return (err < 0 ? NETDEV_TX_BUSY : NETDEV_TX_OK);
  881. }
  882. static void lowpan_dev_free(struct net_device *dev)
  883. {
  884. dev_put(lowpan_dev_info(dev)->real_dev);
  885. free_netdev(dev);
  886. }
  887. static struct wpan_phy *lowpan_get_phy(const struct net_device *dev)
  888. {
  889. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  890. return ieee802154_mlme_ops(real_dev)->get_phy(real_dev);
  891. }
  892. static u16 lowpan_get_pan_id(const struct net_device *dev)
  893. {
  894. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  895. return ieee802154_mlme_ops(real_dev)->get_pan_id(real_dev);
  896. }
  897. static u16 lowpan_get_short_addr(const struct net_device *dev)
  898. {
  899. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  900. return ieee802154_mlme_ops(real_dev)->get_short_addr(real_dev);
  901. }
  902. static struct header_ops lowpan_header_ops = {
  903. .create = lowpan_header_create,
  904. };
  905. static const struct net_device_ops lowpan_netdev_ops = {
  906. .ndo_start_xmit = lowpan_xmit,
  907. .ndo_set_mac_address = eth_mac_addr,
  908. };
  909. static struct ieee802154_mlme_ops lowpan_mlme = {
  910. .get_pan_id = lowpan_get_pan_id,
  911. .get_phy = lowpan_get_phy,
  912. .get_short_addr = lowpan_get_short_addr,
  913. };
  914. static void lowpan_setup(struct net_device *dev)
  915. {
  916. pr_debug("(%s)\n", __func__);
  917. dev->addr_len = IEEE802154_ADDR_LEN;
  918. memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
  919. dev->type = ARPHRD_IEEE802154;
  920. /* Frame Control + Sequence Number + Address fields + Security Header */
  921. dev->hard_header_len = 2 + 1 + 20 + 14;
  922. dev->needed_tailroom = 2; /* FCS */
  923. dev->mtu = 1281;
  924. dev->tx_queue_len = 0;
  925. dev->flags = IFF_BROADCAST | IFF_MULTICAST;
  926. dev->watchdog_timeo = 0;
  927. dev->netdev_ops = &lowpan_netdev_ops;
  928. dev->header_ops = &lowpan_header_ops;
  929. dev->ml_priv = &lowpan_mlme;
  930. dev->destructor = lowpan_dev_free;
  931. }
  932. static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
  933. {
  934. pr_debug("(%s)\n", __func__);
  935. if (tb[IFLA_ADDRESS]) {
  936. if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
  937. return -EINVAL;
  938. }
  939. return 0;
  940. }
  941. static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
  942. struct packet_type *pt, struct net_device *orig_dev)
  943. {
  944. if (!netif_running(dev))
  945. goto drop;
  946. if (dev->type != ARPHRD_IEEE802154)
  947. goto drop;
  948. /* check that it's our buffer */
  949. switch (skb->data[0] & 0xe0) {
  950. case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
  951. case LOWPAN_DISPATCH_FRAG1: /* first fragment header */
  952. case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */
  953. lowpan_process_data(skb);
  954. break;
  955. default:
  956. break;
  957. }
  958. return NET_RX_SUCCESS;
  959. drop:
  960. kfree_skb(skb);
  961. return NET_RX_DROP;
  962. }
  963. static int lowpan_newlink(struct net *src_net, struct net_device *dev,
  964. struct nlattr *tb[], struct nlattr *data[])
  965. {
  966. struct net_device *real_dev;
  967. struct lowpan_dev_record *entry;
  968. pr_debug("(%s)\n", __func__);
  969. if (!tb[IFLA_LINK])
  970. return -EINVAL;
  971. /* find and hold real wpan device */
  972. real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  973. if (!real_dev)
  974. return -ENODEV;
  975. lowpan_dev_info(dev)->real_dev = real_dev;
  976. mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
  977. entry = kzalloc(sizeof(struct lowpan_dev_record), GFP_KERNEL);
  978. if (!entry) {
  979. dev_put(real_dev);
  980. lowpan_dev_info(dev)->real_dev = NULL;
  981. return -ENOMEM;
  982. }
  983. entry->ldev = dev;
  984. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  985. INIT_LIST_HEAD(&entry->list);
  986. list_add_tail(&entry->list, &lowpan_devices);
  987. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  988. spin_lock_init(&flist_lock);
  989. register_netdevice(dev);
  990. return 0;
  991. }
  992. static void lowpan_dellink(struct net_device *dev, struct list_head *head)
  993. {
  994. struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
  995. struct net_device *real_dev = lowpan_dev->real_dev;
  996. struct lowpan_dev_record *entry, *tmp;
  997. struct lowpan_fragment *frame, *tframe;
  998. ASSERT_RTNL();
  999. spin_lock(&flist_lock);
  1000. list_for_each_entry_safe(frame, tframe, &lowpan_fragments, list) {
  1001. del_timer(&frame->timer);
  1002. list_del(&frame->list);
  1003. dev_kfree_skb(frame->skb);
  1004. kfree(frame);
  1005. }
  1006. spin_unlock(&flist_lock);
  1007. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  1008. list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
  1009. if (entry->ldev == dev) {
  1010. list_del(&entry->list);
  1011. kfree(entry);
  1012. }
  1013. }
  1014. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  1015. mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
  1016. unregister_netdevice_queue(dev, head);
  1017. dev_put(real_dev);
  1018. }
  1019. static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
  1020. .kind = "lowpan",
  1021. .priv_size = sizeof(struct lowpan_dev_info),
  1022. .setup = lowpan_setup,
  1023. .newlink = lowpan_newlink,
  1024. .dellink = lowpan_dellink,
  1025. .validate = lowpan_validate,
  1026. };
  1027. static inline int __init lowpan_netlink_init(void)
  1028. {
  1029. return rtnl_link_register(&lowpan_link_ops);
  1030. }
  1031. static inline void __init lowpan_netlink_fini(void)
  1032. {
  1033. rtnl_link_unregister(&lowpan_link_ops);
  1034. }
  1035. static struct packet_type lowpan_packet_type = {
  1036. .type = __constant_htons(ETH_P_IEEE802154),
  1037. .func = lowpan_rcv,
  1038. };
  1039. static int __init lowpan_init_module(void)
  1040. {
  1041. int err = 0;
  1042. pr_debug("(%s)\n", __func__);
  1043. err = lowpan_netlink_init();
  1044. if (err < 0)
  1045. goto out;
  1046. dev_add_pack(&lowpan_packet_type);
  1047. out:
  1048. return err;
  1049. }
  1050. static void __exit lowpan_cleanup_module(void)
  1051. {
  1052. pr_debug("(%s)\n", __func__);
  1053. lowpan_netlink_fini();
  1054. dev_remove_pack(&lowpan_packet_type);
  1055. }
  1056. module_init(lowpan_init_module);
  1057. module_exit(lowpan_cleanup_module);
  1058. MODULE_LICENSE("GPL");
  1059. MODULE_ALIAS_RTNL_LINK("lowpan");