6lowpan.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  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. break;
  400. }
  401. pr_debug("uncompressed UDP ports: src = %d, dst = %d\n",
  402. uh->source, uh->dest);
  403. /* copy checksum */
  404. memcpy(&uh->check, &skb->data[0], 2);
  405. skb_pull(skb, 2);
  406. /*
  407. * UDP lenght needs to be infered from the lower layers
  408. * here, we obtain the hint from the remaining size of the
  409. * frame
  410. */
  411. uh->len = htons(skb->len + sizeof(struct udphdr));
  412. pr_debug("uncompressed UDP length: src = %d", uh->len);
  413. } else {
  414. pr_debug("ERROR: unsupported NH format\n");
  415. goto err;
  416. }
  417. return 0;
  418. err:
  419. return -EINVAL;
  420. }
  421. static int lowpan_header_create(struct sk_buff *skb,
  422. struct net_device *dev,
  423. unsigned short type, const void *_daddr,
  424. const void *_saddr, unsigned int len)
  425. {
  426. u8 tmp, iphc0, iphc1, *hc06_ptr;
  427. struct ipv6hdr *hdr;
  428. const u8 *saddr = _saddr;
  429. const u8 *daddr = _daddr;
  430. u8 head[100];
  431. struct ieee802154_addr sa, da;
  432. /* TODO:
  433. * if this package isn't ipv6 one, where should it be routed?
  434. */
  435. if (type != ETH_P_IPV6)
  436. return 0;
  437. hdr = ipv6_hdr(skb);
  438. hc06_ptr = head + 2;
  439. pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n"
  440. "\tnexthdr = 0x%02x\n\thop_lim = %d\n", hdr->version,
  441. ntohs(hdr->payload_len), hdr->nexthdr, hdr->hop_limit);
  442. lowpan_raw_dump_table(__func__, "raw skb network header dump",
  443. skb_network_header(skb), sizeof(struct ipv6hdr));
  444. if (!saddr)
  445. saddr = dev->dev_addr;
  446. lowpan_raw_dump_inline(__func__, "saddr", (unsigned char *)saddr, 8);
  447. /*
  448. * As we copy some bit-length fields, in the IPHC encoding bytes,
  449. * we sometimes use |=
  450. * If the field is 0, and the current bit value in memory is 1,
  451. * this does not work. We therefore reset the IPHC encoding here
  452. */
  453. iphc0 = LOWPAN_DISPATCH_IPHC;
  454. iphc1 = 0;
  455. /* TODO: context lookup */
  456. lowpan_raw_dump_inline(__func__, "daddr", (unsigned char *)daddr, 8);
  457. /*
  458. * Traffic class, flow label
  459. * If flow label is 0, compress it. If traffic class is 0, compress it
  460. * We have to process both in the same time as the offset of traffic
  461. * class depends on the presence of version and flow label
  462. */
  463. /* hc06 format of TC is ECN | DSCP , original one is DSCP | ECN */
  464. tmp = (hdr->priority << 4) | (hdr->flow_lbl[0] >> 4);
  465. tmp = ((tmp & 0x03) << 6) | (tmp >> 2);
  466. if (((hdr->flow_lbl[0] & 0x0F) == 0) &&
  467. (hdr->flow_lbl[1] == 0) && (hdr->flow_lbl[2] == 0)) {
  468. /* flow label can be compressed */
  469. iphc0 |= LOWPAN_IPHC_FL_C;
  470. if ((hdr->priority == 0) &&
  471. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  472. /* compress (elide) all */
  473. iphc0 |= LOWPAN_IPHC_TC_C;
  474. } else {
  475. /* compress only the flow label */
  476. *hc06_ptr = tmp;
  477. hc06_ptr += 1;
  478. }
  479. } else {
  480. /* Flow label cannot be compressed */
  481. if ((hdr->priority == 0) &&
  482. ((hdr->flow_lbl[0] & 0xF0) == 0)) {
  483. /* compress only traffic class */
  484. iphc0 |= LOWPAN_IPHC_TC_C;
  485. *hc06_ptr = (tmp & 0xc0) | (hdr->flow_lbl[0] & 0x0F);
  486. memcpy(hc06_ptr + 1, &hdr->flow_lbl[1], 2);
  487. hc06_ptr += 3;
  488. } else {
  489. /* compress nothing */
  490. memcpy(hc06_ptr, &hdr, 4);
  491. /* replace the top byte with new ECN | DSCP format */
  492. *hc06_ptr = tmp;
  493. hc06_ptr += 4;
  494. }
  495. }
  496. /* NOTE: payload length is always compressed */
  497. /* Next Header is compress if UDP */
  498. if (hdr->nexthdr == UIP_PROTO_UDP)
  499. iphc0 |= LOWPAN_IPHC_NH_C;
  500. if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
  501. *hc06_ptr = hdr->nexthdr;
  502. hc06_ptr += 1;
  503. }
  504. /*
  505. * Hop limit
  506. * if 1: compress, encoding is 01
  507. * if 64: compress, encoding is 10
  508. * if 255: compress, encoding is 11
  509. * else do not compress
  510. */
  511. switch (hdr->hop_limit) {
  512. case 1:
  513. iphc0 |= LOWPAN_IPHC_TTL_1;
  514. break;
  515. case 64:
  516. iphc0 |= LOWPAN_IPHC_TTL_64;
  517. break;
  518. case 255:
  519. iphc0 |= LOWPAN_IPHC_TTL_255;
  520. break;
  521. default:
  522. *hc06_ptr = hdr->hop_limit;
  523. hc06_ptr += 1;
  524. break;
  525. }
  526. /* source address compression */
  527. if (is_addr_unspecified(&hdr->saddr)) {
  528. pr_debug("source address is unspecified, setting SAC\n");
  529. iphc1 |= LOWPAN_IPHC_SAC;
  530. /* TODO: context lookup */
  531. } else if (is_addr_link_local(&hdr->saddr)) {
  532. pr_debug("source address is link-local\n");
  533. iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
  534. LOWPAN_IPHC_SAM_BIT, &hdr->saddr, saddr);
  535. } else {
  536. pr_debug("send the full source address\n");
  537. memcpy(hc06_ptr, &hdr->saddr.s6_addr16[0], 16);
  538. hc06_ptr += 16;
  539. }
  540. /* destination address compression */
  541. if (is_addr_mcast(&hdr->daddr)) {
  542. pr_debug("destination address is multicast: ");
  543. iphc1 |= LOWPAN_IPHC_M;
  544. if (lowpan_is_mcast_addr_compressable8(&hdr->daddr)) {
  545. pr_debug("compressed to 1 octet\n");
  546. iphc1 |= LOWPAN_IPHC_DAM_11;
  547. /* use last byte */
  548. *hc06_ptr = hdr->daddr.s6_addr[15];
  549. hc06_ptr += 1;
  550. } else if (lowpan_is_mcast_addr_compressable32(&hdr->daddr)) {
  551. pr_debug("compressed to 4 octets\n");
  552. iphc1 |= LOWPAN_IPHC_DAM_10;
  553. /* second byte + the last three */
  554. *hc06_ptr = hdr->daddr.s6_addr[1];
  555. memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[13], 3);
  556. hc06_ptr += 4;
  557. } else if (lowpan_is_mcast_addr_compressable48(&hdr->daddr)) {
  558. pr_debug("compressed to 6 octets\n");
  559. iphc1 |= LOWPAN_IPHC_DAM_01;
  560. /* second byte + the last five */
  561. *hc06_ptr = hdr->daddr.s6_addr[1];
  562. memcpy(hc06_ptr + 1, &hdr->daddr.s6_addr[11], 5);
  563. hc06_ptr += 6;
  564. } else {
  565. pr_debug("using full address\n");
  566. iphc1 |= LOWPAN_IPHC_DAM_00;
  567. memcpy(hc06_ptr, &hdr->daddr.s6_addr[0], 16);
  568. hc06_ptr += 16;
  569. }
  570. } else {
  571. /* TODO: context lookup */
  572. if (is_addr_link_local(&hdr->daddr)) {
  573. pr_debug("dest address is unicast and link-local\n");
  574. iphc1 |= lowpan_compress_addr_64(&hc06_ptr,
  575. LOWPAN_IPHC_DAM_BIT, &hdr->daddr, daddr);
  576. } else {
  577. pr_debug("dest address is unicast: using full one\n");
  578. memcpy(hc06_ptr, &hdr->daddr.s6_addr16[0], 16);
  579. hc06_ptr += 16;
  580. }
  581. }
  582. /* UDP header compression */
  583. if (hdr->nexthdr == UIP_PROTO_UDP)
  584. lowpan_compress_udp_header(&hc06_ptr, skb);
  585. head[0] = iphc0;
  586. head[1] = iphc1;
  587. skb_pull(skb, sizeof(struct ipv6hdr));
  588. memcpy(skb_push(skb, hc06_ptr - head), head, hc06_ptr - head);
  589. lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
  590. skb->len);
  591. /*
  592. * NOTE1: I'm still unsure about the fact that compression and WPAN
  593. * header are created here and not later in the xmit. So wait for
  594. * an opinion of net maintainers.
  595. */
  596. /*
  597. * NOTE2: to be absolutely correct, we must derive PANid information
  598. * from MAC subif of the 'dev' and 'real_dev' network devices, but
  599. * this isn't implemented in mainline yet, so currently we assign 0xff
  600. */
  601. {
  602. mac_cb(skb)->flags = IEEE802154_FC_TYPE_DATA;
  603. mac_cb(skb)->seq = ieee802154_mlme_ops(dev)->get_dsn(dev);
  604. /* prepare wpan address data */
  605. sa.addr_type = IEEE802154_ADDR_LONG;
  606. sa.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
  607. memcpy(&(sa.hwaddr), saddr, 8);
  608. /* intra-PAN communications */
  609. da.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev);
  610. /*
  611. * if the destination address is the broadcast address, use the
  612. * corresponding short address
  613. */
  614. if (lowpan_is_addr_broadcast(daddr)) {
  615. da.addr_type = IEEE802154_ADDR_SHORT;
  616. da.short_addr = IEEE802154_ADDR_BROADCAST;
  617. } else {
  618. da.addr_type = IEEE802154_ADDR_LONG;
  619. memcpy(&(da.hwaddr), daddr, IEEE802154_ADDR_LEN);
  620. /* request acknowledgment */
  621. mac_cb(skb)->flags |= MAC_CB_FLAG_ACKREQ;
  622. }
  623. return dev_hard_header(skb, lowpan_dev_info(dev)->real_dev,
  624. type, (void *)&da, (void *)&sa, skb->len);
  625. }
  626. }
  627. static int lowpan_give_skb_to_devices(struct sk_buff *skb)
  628. {
  629. struct lowpan_dev_record *entry;
  630. struct sk_buff *skb_cp;
  631. int stat = NET_RX_SUCCESS;
  632. rcu_read_lock();
  633. list_for_each_entry_rcu(entry, &lowpan_devices, list)
  634. if (lowpan_dev_info(entry->ldev)->real_dev == skb->dev) {
  635. skb_cp = skb_copy(skb, GFP_ATOMIC);
  636. if (!skb_cp) {
  637. stat = -ENOMEM;
  638. break;
  639. }
  640. skb_cp->dev = entry->ldev;
  641. stat = netif_rx(skb_cp);
  642. }
  643. rcu_read_unlock();
  644. return stat;
  645. }
  646. static int lowpan_skb_deliver(struct sk_buff *skb, struct ipv6hdr *hdr)
  647. {
  648. struct sk_buff *new;
  649. int stat = NET_RX_SUCCESS;
  650. new = skb_copy_expand(skb, sizeof(struct ipv6hdr), skb_tailroom(skb),
  651. GFP_ATOMIC);
  652. kfree_skb(skb);
  653. if (!new)
  654. return -ENOMEM;
  655. skb_push(new, sizeof(struct ipv6hdr));
  656. skb_reset_network_header(new);
  657. skb_copy_to_linear_data(new, hdr, sizeof(struct ipv6hdr));
  658. new->protocol = htons(ETH_P_IPV6);
  659. new->pkt_type = PACKET_HOST;
  660. stat = lowpan_give_skb_to_devices(new);
  661. kfree_skb(new);
  662. return stat;
  663. }
  664. static void lowpan_fragment_timer_expired(unsigned long entry_addr)
  665. {
  666. struct lowpan_fragment *entry = (struct lowpan_fragment *)entry_addr;
  667. pr_debug("timer expired for frame with tag %d\n", entry->tag);
  668. list_del(&entry->list);
  669. dev_kfree_skb(entry->skb);
  670. kfree(entry);
  671. }
  672. static struct lowpan_fragment *
  673. lowpan_alloc_new_frame(struct sk_buff *skb, u16 len, u16 tag)
  674. {
  675. struct lowpan_fragment *frame;
  676. frame = kzalloc(sizeof(struct lowpan_fragment),
  677. GFP_ATOMIC);
  678. if (!frame)
  679. goto frame_err;
  680. INIT_LIST_HEAD(&frame->list);
  681. frame->length = len;
  682. frame->tag = tag;
  683. /* allocate buffer for frame assembling */
  684. frame->skb = netdev_alloc_skb_ip_align(skb->dev, frame->length +
  685. sizeof(struct ipv6hdr));
  686. if (!frame->skb)
  687. goto skb_err;
  688. frame->skb->priority = skb->priority;
  689. frame->skb->dev = skb->dev;
  690. /* reserve headroom for uncompressed ipv6 header */
  691. skb_reserve(frame->skb, sizeof(struct ipv6hdr));
  692. skb_put(frame->skb, frame->length);
  693. /* copy the first control block to keep a
  694. * trace of the link-layer addresses in case
  695. * of a link-local compressed address
  696. */
  697. memcpy(frame->skb->cb, skb->cb, sizeof(skb->cb));
  698. init_timer(&frame->timer);
  699. /* time out is the same as for ipv6 - 60 sec */
  700. frame->timer.expires = jiffies + LOWPAN_FRAG_TIMEOUT;
  701. frame->timer.data = (unsigned long)frame;
  702. frame->timer.function = lowpan_fragment_timer_expired;
  703. add_timer(&frame->timer);
  704. list_add_tail(&frame->list, &lowpan_fragments);
  705. return frame;
  706. skb_err:
  707. kfree(frame);
  708. frame_err:
  709. return NULL;
  710. }
  711. static int
  712. lowpan_process_data(struct sk_buff *skb)
  713. {
  714. struct ipv6hdr hdr = {};
  715. u8 tmp, iphc0, iphc1, num_context = 0;
  716. const struct ieee802154_addr *_saddr, *_daddr;
  717. int err;
  718. lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
  719. skb->len);
  720. /* at least two bytes will be used for the encoding */
  721. if (skb->len < 2)
  722. goto drop;
  723. if (lowpan_fetch_skb_u8(skb, &iphc0))
  724. goto drop;
  725. /* fragments assembling */
  726. switch (iphc0 & LOWPAN_DISPATCH_MASK) {
  727. case LOWPAN_DISPATCH_FRAG1:
  728. case LOWPAN_DISPATCH_FRAGN:
  729. {
  730. struct lowpan_fragment *frame;
  731. /* slen stores the rightmost 8 bits of the 11 bits length */
  732. u8 slen, offset = 0;
  733. u16 len, tag;
  734. bool found = false;
  735. if (lowpan_fetch_skb_u8(skb, &slen) || /* frame length */
  736. lowpan_fetch_skb_u16(skb, &tag)) /* fragment tag */
  737. goto drop;
  738. /* adds the 3 MSB to the 8 LSB to retrieve the 11 bits length */
  739. len = ((iphc0 & 7) << 8) | slen;
  740. if ((iphc0 & LOWPAN_DISPATCH_MASK) == LOWPAN_DISPATCH_FRAG1) {
  741. pr_debug("%s received a FRAG1 packet (tag: %d, "
  742. "size of the entire IP packet: %d)",
  743. __func__, tag, len);
  744. } else { /* FRAGN */
  745. if (lowpan_fetch_skb_u8(skb, &offset))
  746. goto unlock_and_drop;
  747. pr_debug("%s received a FRAGN packet (tag: %d, "
  748. "size of the entire IP packet: %d, "
  749. "offset: %d)", __func__, tag, len, offset * 8);
  750. }
  751. /*
  752. * check if frame assembling with the same tag is
  753. * already in progress
  754. */
  755. spin_lock_bh(&flist_lock);
  756. list_for_each_entry(frame, &lowpan_fragments, list)
  757. if (frame->tag == tag) {
  758. found = true;
  759. break;
  760. }
  761. /* alloc new frame structure */
  762. if (!found) {
  763. pr_debug("%s first fragment received for tag %d, "
  764. "begin packet reassembly", __func__, tag);
  765. frame = lowpan_alloc_new_frame(skb, len, tag);
  766. if (!frame)
  767. goto unlock_and_drop;
  768. }
  769. /* if payload fits buffer, copy it */
  770. if (likely((offset * 8 + skb->len) <= frame->length))
  771. skb_copy_to_linear_data_offset(frame->skb, offset * 8,
  772. skb->data, skb->len);
  773. else
  774. goto unlock_and_drop;
  775. frame->bytes_rcv += skb->len;
  776. /* frame assembling complete */
  777. if ((frame->bytes_rcv == frame->length) &&
  778. frame->timer.expires > jiffies) {
  779. /* if timer haven't expired - first of all delete it */
  780. del_timer_sync(&frame->timer);
  781. list_del(&frame->list);
  782. spin_unlock_bh(&flist_lock);
  783. pr_debug("%s successfully reassembled fragment "
  784. "(tag %d)", __func__, tag);
  785. dev_kfree_skb(skb);
  786. skb = frame->skb;
  787. kfree(frame);
  788. if (lowpan_fetch_skb_u8(skb, &iphc0))
  789. goto drop;
  790. break;
  791. }
  792. spin_unlock_bh(&flist_lock);
  793. return kfree_skb(skb), 0;
  794. }
  795. default:
  796. break;
  797. }
  798. if (lowpan_fetch_skb_u8(skb, &iphc1))
  799. goto drop;
  800. _saddr = &mac_cb(skb)->sa;
  801. _daddr = &mac_cb(skb)->da;
  802. pr_debug("iphc0 = %02x, iphc1 = %02x\n", iphc0, iphc1);
  803. /* another if the CID flag is set */
  804. if (iphc1 & LOWPAN_IPHC_CID) {
  805. pr_debug("CID flag is set, increase header with one\n");
  806. if (lowpan_fetch_skb_u8(skb, &num_context))
  807. goto drop;
  808. }
  809. hdr.version = 6;
  810. /* Traffic Class and Flow Label */
  811. switch ((iphc0 & LOWPAN_IPHC_TF) >> 3) {
  812. /*
  813. * Traffic Class and FLow Label carried in-line
  814. * ECN + DSCP + 4-bit Pad + Flow Label (4 bytes)
  815. */
  816. case 0: /* 00b */
  817. if (lowpan_fetch_skb_u8(skb, &tmp))
  818. goto drop;
  819. memcpy(&hdr.flow_lbl, &skb->data[0], 3);
  820. skb_pull(skb, 3);
  821. hdr.priority = ((tmp >> 2) & 0x0f);
  822. hdr.flow_lbl[0] = ((tmp >> 2) & 0x30) | (tmp << 6) |
  823. (hdr.flow_lbl[0] & 0x0f);
  824. break;
  825. /*
  826. * Traffic class carried in-line
  827. * ECN + DSCP (1 byte), Flow Label is elided
  828. */
  829. case 1: /* 10b */
  830. if (lowpan_fetch_skb_u8(skb, &tmp))
  831. goto drop;
  832. hdr.priority = ((tmp >> 2) & 0x0f);
  833. hdr.flow_lbl[0] = ((tmp << 6) & 0xC0) | ((tmp >> 2) & 0x30);
  834. break;
  835. /*
  836. * Flow Label carried in-line
  837. * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
  838. */
  839. case 2: /* 01b */
  840. if (lowpan_fetch_skb_u8(skb, &tmp))
  841. goto drop;
  842. hdr.flow_lbl[0] = (skb->data[0] & 0x0F) | ((tmp >> 2) & 0x30);
  843. memcpy(&hdr.flow_lbl[1], &skb->data[0], 2);
  844. skb_pull(skb, 2);
  845. break;
  846. /* Traffic Class and Flow Label are elided */
  847. case 3: /* 11b */
  848. break;
  849. default:
  850. break;
  851. }
  852. /* Next Header */
  853. if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) {
  854. /* Next header is carried inline */
  855. if (lowpan_fetch_skb_u8(skb, &(hdr.nexthdr)))
  856. goto drop;
  857. pr_debug("NH flag is set, next header carried inline: %02x\n",
  858. hdr.nexthdr);
  859. }
  860. /* Hop Limit */
  861. if ((iphc0 & 0x03) != LOWPAN_IPHC_TTL_I)
  862. hdr.hop_limit = lowpan_ttl_values[iphc0 & 0x03];
  863. else {
  864. if (lowpan_fetch_skb_u8(skb, &(hdr.hop_limit)))
  865. goto drop;
  866. }
  867. /* Extract SAM to the tmp variable */
  868. tmp = ((iphc1 & LOWPAN_IPHC_SAM) >> LOWPAN_IPHC_SAM_BIT) & 0x03;
  869. if (iphc1 & LOWPAN_IPHC_SAC) {
  870. /* Source address context based uncompression */
  871. pr_debug("SAC bit is set. Handle context based source address.\n");
  872. err = lowpan_uncompress_context_based_src_addr(
  873. skb, &hdr.saddr, tmp);
  874. } else {
  875. /* Source address uncompression */
  876. pr_debug("source address stateless compression\n");
  877. err = lowpan_uncompress_addr(skb, &hdr.saddr, tmp, _saddr);
  878. }
  879. /* Check on error of previous branch */
  880. if (err)
  881. goto drop;
  882. /* Extract DAM to the tmp variable */
  883. tmp = ((iphc1 & LOWPAN_IPHC_DAM_11) >> LOWPAN_IPHC_DAM_BIT) & 0x03;
  884. /* check for Multicast Compression */
  885. if (iphc1 & LOWPAN_IPHC_M) {
  886. if (iphc1 & LOWPAN_IPHC_DAC) {
  887. pr_debug("dest: context-based mcast compression\n");
  888. /* TODO: implement this */
  889. } else {
  890. err = lowpan_uncompress_multicast_daddr(
  891. skb, &hdr.daddr, tmp);
  892. if (err)
  893. goto drop;
  894. }
  895. } else {
  896. pr_debug("dest: stateless compression\n");
  897. err = lowpan_uncompress_addr(skb, &hdr.daddr, tmp, _daddr);
  898. if (err)
  899. goto drop;
  900. }
  901. /* UDP data uncompression */
  902. if (iphc0 & LOWPAN_IPHC_NH_C) {
  903. struct udphdr uh;
  904. struct sk_buff *new;
  905. if (lowpan_uncompress_udp_header(skb, &uh))
  906. goto drop;
  907. /*
  908. * replace the compressed UDP head by the uncompressed UDP
  909. * header
  910. */
  911. new = skb_copy_expand(skb, sizeof(struct udphdr),
  912. skb_tailroom(skb), GFP_ATOMIC);
  913. kfree_skb(skb);
  914. if (!new)
  915. return -ENOMEM;
  916. skb = new;
  917. skb_push(skb, sizeof(struct udphdr));
  918. skb_reset_transport_header(skb);
  919. skb_copy_to_linear_data(skb, &uh, sizeof(struct udphdr));
  920. lowpan_raw_dump_table(__func__, "raw UDP header dump",
  921. (u8 *)&uh, sizeof(uh));
  922. hdr.nexthdr = UIP_PROTO_UDP;
  923. }
  924. /* Not fragmented package */
  925. hdr.payload_len = htons(skb->len);
  926. pr_debug("skb headroom size = %d, data length = %d\n",
  927. skb_headroom(skb), skb->len);
  928. pr_debug("IPv6 header dump:\n\tversion = %d\n\tlength = %d\n\t"
  929. "nexthdr = 0x%02x\n\thop_lim = %d\n", hdr.version,
  930. ntohs(hdr.payload_len), hdr.nexthdr, hdr.hop_limit);
  931. lowpan_raw_dump_table(__func__, "raw header dump", (u8 *)&hdr,
  932. sizeof(hdr));
  933. return lowpan_skb_deliver(skb, &hdr);
  934. unlock_and_drop:
  935. spin_unlock_bh(&flist_lock);
  936. drop:
  937. kfree_skb(skb);
  938. return -EINVAL;
  939. }
  940. static int lowpan_set_address(struct net_device *dev, void *p)
  941. {
  942. struct sockaddr *sa = p;
  943. if (netif_running(dev))
  944. return -EBUSY;
  945. /* TODO: validate addr */
  946. memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
  947. return 0;
  948. }
  949. static int lowpan_get_mac_header_length(struct sk_buff *skb)
  950. {
  951. /*
  952. * Currently long addressing mode is supported only, so the overall
  953. * header size is 21:
  954. * FC SeqNum DPAN DA SA Sec
  955. * 2 + 1 + 2 + 8 + 8 + 0 = 21
  956. */
  957. return 21;
  958. }
  959. static int
  960. lowpan_fragment_xmit(struct sk_buff *skb, u8 *head,
  961. int mlen, int plen, int offset, int type)
  962. {
  963. struct sk_buff *frag;
  964. int hlen, ret;
  965. hlen = (type == LOWPAN_DISPATCH_FRAG1) ?
  966. LOWPAN_FRAG1_HEAD_SIZE : LOWPAN_FRAGN_HEAD_SIZE;
  967. lowpan_raw_dump_inline(__func__, "6lowpan fragment header", head, hlen);
  968. frag = dev_alloc_skb(hlen + mlen + plen + IEEE802154_MFR_SIZE);
  969. if (!frag)
  970. return -ENOMEM;
  971. frag->priority = skb->priority;
  972. frag->dev = skb->dev;
  973. /* copy header, MFR and payload */
  974. memcpy(skb_put(frag, mlen), skb->data, mlen);
  975. memcpy(skb_put(frag, hlen), head, hlen);
  976. if (plen)
  977. skb_copy_from_linear_data_offset(skb, offset + mlen,
  978. skb_put(frag, plen), plen);
  979. lowpan_raw_dump_table(__func__, " raw fragment dump", frag->data,
  980. frag->len);
  981. ret = dev_queue_xmit(frag);
  982. return ret;
  983. }
  984. static int
  985. lowpan_skb_fragmentation(struct sk_buff *skb, struct net_device *dev)
  986. {
  987. int err, header_length, payload_length, tag, offset = 0;
  988. u8 head[5];
  989. header_length = lowpan_get_mac_header_length(skb);
  990. payload_length = skb->len - header_length;
  991. tag = lowpan_dev_info(dev)->fragment_tag++;
  992. /* first fragment header */
  993. head[0] = LOWPAN_DISPATCH_FRAG1 | ((payload_length >> 8) & 0x7);
  994. head[1] = payload_length & 0xff;
  995. head[2] = tag >> 8;
  996. head[3] = tag & 0xff;
  997. err = lowpan_fragment_xmit(skb, head, header_length, LOWPAN_FRAG_SIZE,
  998. 0, LOWPAN_DISPATCH_FRAG1);
  999. if (err) {
  1000. pr_debug("%s unable to send FRAG1 packet (tag: %d)",
  1001. __func__, tag);
  1002. goto exit;
  1003. }
  1004. offset = LOWPAN_FRAG_SIZE;
  1005. /* next fragment header */
  1006. head[0] &= ~LOWPAN_DISPATCH_FRAG1;
  1007. head[0] |= LOWPAN_DISPATCH_FRAGN;
  1008. while ((payload_length - offset > 0) && (err >= 0)) {
  1009. int len = LOWPAN_FRAG_SIZE;
  1010. head[4] = offset / 8;
  1011. if (payload_length - offset < len)
  1012. len = payload_length - offset;
  1013. err = lowpan_fragment_xmit(skb, head, header_length,
  1014. len, offset, LOWPAN_DISPATCH_FRAGN);
  1015. if (err) {
  1016. pr_debug("%s unable to send a subsequent FRAGN packet "
  1017. "(tag: %d, offset: %d", __func__, tag, offset);
  1018. goto exit;
  1019. }
  1020. offset += len;
  1021. }
  1022. exit:
  1023. return err;
  1024. }
  1025. static netdev_tx_t lowpan_xmit(struct sk_buff *skb, struct net_device *dev)
  1026. {
  1027. int err = -1;
  1028. pr_debug("package xmit\n");
  1029. skb->dev = lowpan_dev_info(dev)->real_dev;
  1030. if (skb->dev == NULL) {
  1031. pr_debug("ERROR: no real wpan device found\n");
  1032. goto error;
  1033. }
  1034. /* Send directly if less than the MTU minus the 2 checksum bytes. */
  1035. if (skb->len <= IEEE802154_MTU - IEEE802154_MFR_SIZE) {
  1036. err = dev_queue_xmit(skb);
  1037. goto out;
  1038. }
  1039. pr_debug("frame is too big, fragmentation is needed\n");
  1040. err = lowpan_skb_fragmentation(skb, dev);
  1041. error:
  1042. dev_kfree_skb(skb);
  1043. out:
  1044. if (err)
  1045. pr_debug("ERROR: xmit failed\n");
  1046. return (err < 0) ? NET_XMIT_DROP : err;
  1047. }
  1048. static struct wpan_phy *lowpan_get_phy(const struct net_device *dev)
  1049. {
  1050. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  1051. return ieee802154_mlme_ops(real_dev)->get_phy(real_dev);
  1052. }
  1053. static u16 lowpan_get_pan_id(const struct net_device *dev)
  1054. {
  1055. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  1056. return ieee802154_mlme_ops(real_dev)->get_pan_id(real_dev);
  1057. }
  1058. static u16 lowpan_get_short_addr(const struct net_device *dev)
  1059. {
  1060. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  1061. return ieee802154_mlme_ops(real_dev)->get_short_addr(real_dev);
  1062. }
  1063. static u8 lowpan_get_dsn(const struct net_device *dev)
  1064. {
  1065. struct net_device *real_dev = lowpan_dev_info(dev)->real_dev;
  1066. return ieee802154_mlme_ops(real_dev)->get_dsn(real_dev);
  1067. }
  1068. static struct header_ops lowpan_header_ops = {
  1069. .create = lowpan_header_create,
  1070. };
  1071. static const struct net_device_ops lowpan_netdev_ops = {
  1072. .ndo_start_xmit = lowpan_xmit,
  1073. .ndo_set_mac_address = lowpan_set_address,
  1074. };
  1075. static struct ieee802154_mlme_ops lowpan_mlme = {
  1076. .get_pan_id = lowpan_get_pan_id,
  1077. .get_phy = lowpan_get_phy,
  1078. .get_short_addr = lowpan_get_short_addr,
  1079. .get_dsn = lowpan_get_dsn,
  1080. };
  1081. static void lowpan_setup(struct net_device *dev)
  1082. {
  1083. dev->addr_len = IEEE802154_ADDR_LEN;
  1084. memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
  1085. dev->type = ARPHRD_IEEE802154;
  1086. /* Frame Control + Sequence Number + Address fields + Security Header */
  1087. dev->hard_header_len = 2 + 1 + 20 + 14;
  1088. dev->needed_tailroom = 2; /* FCS */
  1089. dev->mtu = 1281;
  1090. dev->tx_queue_len = 0;
  1091. dev->flags = IFF_BROADCAST | IFF_MULTICAST;
  1092. dev->watchdog_timeo = 0;
  1093. dev->netdev_ops = &lowpan_netdev_ops;
  1094. dev->header_ops = &lowpan_header_ops;
  1095. dev->ml_priv = &lowpan_mlme;
  1096. dev->destructor = free_netdev;
  1097. }
  1098. static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
  1099. {
  1100. if (tb[IFLA_ADDRESS]) {
  1101. if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
  1102. return -EINVAL;
  1103. }
  1104. return 0;
  1105. }
  1106. static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
  1107. struct packet_type *pt, struct net_device *orig_dev)
  1108. {
  1109. struct sk_buff *local_skb;
  1110. if (!netif_running(dev))
  1111. goto drop;
  1112. if (dev->type != ARPHRD_IEEE802154)
  1113. goto drop;
  1114. /* check that it's our buffer */
  1115. if (skb->data[0] == LOWPAN_DISPATCH_IPV6) {
  1116. /* Copy the packet so that the IPv6 header is
  1117. * properly aligned.
  1118. */
  1119. local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1,
  1120. skb_tailroom(skb), GFP_ATOMIC);
  1121. if (!local_skb)
  1122. goto drop;
  1123. local_skb->protocol = htons(ETH_P_IPV6);
  1124. local_skb->pkt_type = PACKET_HOST;
  1125. /* Pull off the 1-byte of 6lowpan header. */
  1126. skb_pull(local_skb, 1);
  1127. skb_reset_network_header(local_skb);
  1128. skb_set_transport_header(local_skb, sizeof(struct ipv6hdr));
  1129. lowpan_give_skb_to_devices(local_skb);
  1130. kfree_skb(local_skb);
  1131. kfree_skb(skb);
  1132. } else {
  1133. switch (skb->data[0] & 0xe0) {
  1134. case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
  1135. case LOWPAN_DISPATCH_FRAG1: /* first fragment header */
  1136. case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */
  1137. local_skb = skb_clone(skb, GFP_ATOMIC);
  1138. if (!local_skb)
  1139. goto drop;
  1140. lowpan_process_data(local_skb);
  1141. kfree_skb(skb);
  1142. break;
  1143. default:
  1144. break;
  1145. }
  1146. }
  1147. return NET_RX_SUCCESS;
  1148. drop:
  1149. kfree_skb(skb);
  1150. return NET_RX_DROP;
  1151. }
  1152. static int lowpan_newlink(struct net *src_net, struct net_device *dev,
  1153. struct nlattr *tb[], struct nlattr *data[])
  1154. {
  1155. struct net_device *real_dev;
  1156. struct lowpan_dev_record *entry;
  1157. pr_debug("adding new link\n");
  1158. if (!tb[IFLA_LINK])
  1159. return -EINVAL;
  1160. /* find and hold real wpan device */
  1161. real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  1162. if (!real_dev)
  1163. return -ENODEV;
  1164. lowpan_dev_info(dev)->real_dev = real_dev;
  1165. lowpan_dev_info(dev)->fragment_tag = 0;
  1166. mutex_init(&lowpan_dev_info(dev)->dev_list_mtx);
  1167. entry = kzalloc(sizeof(struct lowpan_dev_record), GFP_KERNEL);
  1168. if (!entry) {
  1169. dev_put(real_dev);
  1170. lowpan_dev_info(dev)->real_dev = NULL;
  1171. return -ENOMEM;
  1172. }
  1173. entry->ldev = dev;
  1174. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  1175. INIT_LIST_HEAD(&entry->list);
  1176. list_add_tail(&entry->list, &lowpan_devices);
  1177. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  1178. register_netdevice(dev);
  1179. return 0;
  1180. }
  1181. static void lowpan_dellink(struct net_device *dev, struct list_head *head)
  1182. {
  1183. struct lowpan_dev_info *lowpan_dev = lowpan_dev_info(dev);
  1184. struct net_device *real_dev = lowpan_dev->real_dev;
  1185. struct lowpan_dev_record *entry, *tmp;
  1186. ASSERT_RTNL();
  1187. mutex_lock(&lowpan_dev_info(dev)->dev_list_mtx);
  1188. list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
  1189. if (entry->ldev == dev) {
  1190. list_del(&entry->list);
  1191. kfree(entry);
  1192. }
  1193. }
  1194. mutex_unlock(&lowpan_dev_info(dev)->dev_list_mtx);
  1195. mutex_destroy(&lowpan_dev_info(dev)->dev_list_mtx);
  1196. unregister_netdevice_queue(dev, head);
  1197. dev_put(real_dev);
  1198. }
  1199. static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
  1200. .kind = "lowpan",
  1201. .priv_size = sizeof(struct lowpan_dev_info),
  1202. .setup = lowpan_setup,
  1203. .newlink = lowpan_newlink,
  1204. .dellink = lowpan_dellink,
  1205. .validate = lowpan_validate,
  1206. };
  1207. static inline int __init lowpan_netlink_init(void)
  1208. {
  1209. return rtnl_link_register(&lowpan_link_ops);
  1210. }
  1211. static inline void lowpan_netlink_fini(void)
  1212. {
  1213. rtnl_link_unregister(&lowpan_link_ops);
  1214. }
  1215. static int lowpan_device_event(struct notifier_block *unused,
  1216. unsigned long event, void *ptr)
  1217. {
  1218. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1219. LIST_HEAD(del_list);
  1220. struct lowpan_dev_record *entry, *tmp;
  1221. if (dev->type != ARPHRD_IEEE802154)
  1222. goto out;
  1223. if (event == NETDEV_UNREGISTER) {
  1224. list_for_each_entry_safe(entry, tmp, &lowpan_devices, list) {
  1225. if (lowpan_dev_info(entry->ldev)->real_dev == dev)
  1226. lowpan_dellink(entry->ldev, &del_list);
  1227. }
  1228. unregister_netdevice_many(&del_list);
  1229. }
  1230. out:
  1231. return NOTIFY_DONE;
  1232. }
  1233. static struct notifier_block lowpan_dev_notifier = {
  1234. .notifier_call = lowpan_device_event,
  1235. };
  1236. static struct packet_type lowpan_packet_type = {
  1237. .type = __constant_htons(ETH_P_IEEE802154),
  1238. .func = lowpan_rcv,
  1239. };
  1240. static int __init lowpan_init_module(void)
  1241. {
  1242. int err = 0;
  1243. err = lowpan_netlink_init();
  1244. if (err < 0)
  1245. goto out;
  1246. dev_add_pack(&lowpan_packet_type);
  1247. err = register_netdevice_notifier(&lowpan_dev_notifier);
  1248. if (err < 0) {
  1249. dev_remove_pack(&lowpan_packet_type);
  1250. lowpan_netlink_fini();
  1251. }
  1252. out:
  1253. return err;
  1254. }
  1255. static void __exit lowpan_cleanup_module(void)
  1256. {
  1257. struct lowpan_fragment *frame, *tframe;
  1258. lowpan_netlink_fini();
  1259. dev_remove_pack(&lowpan_packet_type);
  1260. unregister_netdevice_notifier(&lowpan_dev_notifier);
  1261. /* Now 6lowpan packet_type is removed, so no new fragments are
  1262. * expected on RX, therefore that's the time to clean incomplete
  1263. * fragments.
  1264. */
  1265. spin_lock_bh(&flist_lock);
  1266. list_for_each_entry_safe(frame, tframe, &lowpan_fragments, list) {
  1267. del_timer_sync(&frame->timer);
  1268. list_del(&frame->list);
  1269. dev_kfree_skb(frame->skb);
  1270. kfree(frame);
  1271. }
  1272. spin_unlock_bh(&flist_lock);
  1273. }
  1274. module_init(lowpan_init_module);
  1275. module_exit(lowpan_cleanup_module);
  1276. MODULE_LICENSE("GPL");
  1277. MODULE_ALIAS_RTNL_LINK("lowpan");