6lowpan.c 33 KB

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