6lowpan.c 39 KB

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