6lowpan.c 33 KB

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