header_ops.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (C) 2014 Fraunhofer ITWM
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * Written by:
  14. * Phoebe Buckheister <phoebe.buckheister@itwm.fraunhofer.de>
  15. */
  16. #include <linux/ieee802154.h>
  17. #include <net/mac802154.h>
  18. #include <net/ieee802154_netdev.h>
  19. static int
  20. ieee802154_hdr_push_addr(u8 *buf, const struct ieee802154_addr *addr,
  21. bool omit_pan)
  22. {
  23. int pos = 0;
  24. if (addr->mode == IEEE802154_ADDR_NONE)
  25. return 0;
  26. if (!omit_pan) {
  27. memcpy(buf + pos, &addr->pan_id, 2);
  28. pos += 2;
  29. }
  30. switch (addr->mode) {
  31. case IEEE802154_ADDR_SHORT:
  32. memcpy(buf + pos, &addr->short_addr, 2);
  33. pos += 2;
  34. break;
  35. case IEEE802154_ADDR_LONG:
  36. memcpy(buf + pos, &addr->extended_addr, IEEE802154_ADDR_LEN);
  37. pos += IEEE802154_ADDR_LEN;
  38. break;
  39. default:
  40. return -EINVAL;
  41. }
  42. return pos;
  43. }
  44. static int
  45. ieee802154_hdr_push_sechdr(u8 *buf, const struct ieee802154_sechdr *hdr)
  46. {
  47. int pos = 5;
  48. memcpy(buf, hdr, 1);
  49. memcpy(buf + 1, &hdr->frame_counter, 4);
  50. switch (hdr->key_id_mode) {
  51. case IEEE802154_SCF_KEY_IMPLICIT:
  52. return pos;
  53. case IEEE802154_SCF_KEY_INDEX:
  54. break;
  55. case IEEE802154_SCF_KEY_SHORT_INDEX:
  56. memcpy(buf + pos, &hdr->short_src, 4);
  57. pos += 4;
  58. break;
  59. case IEEE802154_SCF_KEY_HW_INDEX:
  60. memcpy(buf + pos, &hdr->extended_src, IEEE802154_ADDR_LEN);
  61. pos += IEEE802154_ADDR_LEN;
  62. break;
  63. }
  64. buf[pos++] = hdr->key_id;
  65. return pos;
  66. }
  67. int
  68. ieee802154_hdr_push(struct sk_buff *skb, const struct ieee802154_hdr *hdr)
  69. {
  70. u8 buf[MAC802154_FRAME_HARD_HEADER_LEN];
  71. int pos = 2;
  72. int rc;
  73. struct ieee802154_hdr_fc fc = hdr->fc;
  74. buf[pos++] = hdr->seq;
  75. fc.dest_addr_mode = hdr->dest.mode;
  76. rc = ieee802154_hdr_push_addr(buf + pos, &hdr->dest, false);
  77. if (rc < 0)
  78. return -EINVAL;
  79. pos += rc;
  80. fc.source_addr_mode = hdr->source.mode;
  81. if (hdr->source.pan_id == hdr->dest.pan_id &&
  82. hdr->dest.mode != IEEE802154_ADDR_NONE)
  83. fc.intra_pan = true;
  84. rc = ieee802154_hdr_push_addr(buf + pos, &hdr->source, fc.intra_pan);
  85. if (rc < 0)
  86. return -EINVAL;
  87. pos += rc;
  88. if (fc.security_enabled) {
  89. fc.version = 1;
  90. rc = ieee802154_hdr_push_sechdr(buf + pos, &hdr->sec);
  91. if (rc < 0)
  92. return -EINVAL;
  93. pos += rc;
  94. }
  95. memcpy(buf, &fc, 2);
  96. memcpy(skb_push(skb, pos), buf, pos);
  97. return pos;
  98. }
  99. EXPORT_SYMBOL_GPL(ieee802154_hdr_push);
  100. static int
  101. ieee802154_hdr_get_addr(const u8 *buf, int mode, bool omit_pan,
  102. struct ieee802154_addr *addr)
  103. {
  104. int pos = 0;
  105. addr->mode = mode;
  106. if (mode == IEEE802154_ADDR_NONE)
  107. return 0;
  108. if (!omit_pan) {
  109. memcpy(&addr->pan_id, buf + pos, 2);
  110. pos += 2;
  111. }
  112. if (mode == IEEE802154_ADDR_SHORT) {
  113. memcpy(&addr->short_addr, buf + pos, 2);
  114. return pos + 2;
  115. } else {
  116. memcpy(&addr->extended_addr, buf + pos, IEEE802154_ADDR_LEN);
  117. return pos + IEEE802154_ADDR_LEN;
  118. }
  119. }
  120. static int ieee802154_hdr_addr_len(int mode, bool omit_pan)
  121. {
  122. int pan_len = omit_pan ? 0 : 2;
  123. switch (mode) {
  124. case IEEE802154_ADDR_NONE: return 0;
  125. case IEEE802154_ADDR_SHORT: return 2 + pan_len;
  126. case IEEE802154_ADDR_LONG: return IEEE802154_ADDR_LEN + pan_len;
  127. default: return -EINVAL;
  128. }
  129. }
  130. static int
  131. ieee802154_hdr_get_sechdr(const u8 *buf, struct ieee802154_sechdr *hdr)
  132. {
  133. int pos = 5;
  134. memcpy(hdr, buf, 1);
  135. memcpy(&hdr->frame_counter, buf + 1, 4);
  136. switch (hdr->key_id_mode) {
  137. case IEEE802154_SCF_KEY_IMPLICIT:
  138. return pos;
  139. case IEEE802154_SCF_KEY_INDEX:
  140. break;
  141. case IEEE802154_SCF_KEY_SHORT_INDEX:
  142. memcpy(&hdr->short_src, buf + pos, 4);
  143. pos += 4;
  144. break;
  145. case IEEE802154_SCF_KEY_HW_INDEX:
  146. memcpy(&hdr->extended_src, buf + pos, IEEE802154_ADDR_LEN);
  147. pos += IEEE802154_ADDR_LEN;
  148. break;
  149. }
  150. hdr->key_id = buf[pos++];
  151. return pos;
  152. }
  153. static int ieee802154_sechdr_lengths[4] = {
  154. [IEEE802154_SCF_KEY_IMPLICIT] = 5,
  155. [IEEE802154_SCF_KEY_INDEX] = 6,
  156. [IEEE802154_SCF_KEY_SHORT_INDEX] = 10,
  157. [IEEE802154_SCF_KEY_HW_INDEX] = 14,
  158. };
  159. static int ieee802154_hdr_sechdr_len(u8 sc)
  160. {
  161. return ieee802154_sechdr_lengths[IEEE802154_SCF_KEY_ID_MODE(sc)];
  162. }
  163. static int ieee802154_hdr_minlen(const struct ieee802154_hdr *hdr)
  164. {
  165. int dlen, slen;
  166. dlen = ieee802154_hdr_addr_len(hdr->fc.dest_addr_mode, false);
  167. slen = ieee802154_hdr_addr_len(hdr->fc.source_addr_mode,
  168. hdr->fc.intra_pan);
  169. if (slen < 0 || dlen < 0)
  170. return -EINVAL;
  171. return 3 + dlen + slen + hdr->fc.security_enabled;
  172. }
  173. static int
  174. ieee802154_hdr_get_addrs(const u8 *buf, struct ieee802154_hdr *hdr)
  175. {
  176. int pos = 0;
  177. pos += ieee802154_hdr_get_addr(buf + pos, hdr->fc.dest_addr_mode,
  178. false, &hdr->dest);
  179. pos += ieee802154_hdr_get_addr(buf + pos, hdr->fc.source_addr_mode,
  180. hdr->fc.intra_pan, &hdr->source);
  181. if (hdr->fc.intra_pan)
  182. hdr->source.pan_id = hdr->dest.pan_id;
  183. return pos;
  184. }
  185. int
  186. ieee802154_hdr_pull(struct sk_buff *skb, struct ieee802154_hdr *hdr)
  187. {
  188. int pos = 3, rc;
  189. if (!pskb_may_pull(skb, 3))
  190. return -EINVAL;
  191. memcpy(hdr, skb->data, 3);
  192. rc = ieee802154_hdr_minlen(hdr);
  193. if (rc < 0 || !pskb_may_pull(skb, rc))
  194. return -EINVAL;
  195. pos += ieee802154_hdr_get_addrs(skb->data + pos, hdr);
  196. if (hdr->fc.security_enabled) {
  197. int want = pos + ieee802154_hdr_sechdr_len(skb->data[pos]);
  198. if (!pskb_may_pull(skb, want))
  199. return -EINVAL;
  200. pos += ieee802154_hdr_get_sechdr(skb->data + pos, &hdr->sec);
  201. }
  202. skb_pull(skb, pos);
  203. return pos;
  204. }
  205. EXPORT_SYMBOL_GPL(ieee802154_hdr_pull);
  206. int
  207. ieee802154_hdr_peek_addrs(const struct sk_buff *skb, struct ieee802154_hdr *hdr)
  208. {
  209. const u8 *buf = skb_mac_header(skb);
  210. int pos = 3, rc;
  211. if (buf + 3 > skb_tail_pointer(skb))
  212. return -EINVAL;
  213. memcpy(hdr, buf, 3);
  214. rc = ieee802154_hdr_minlen(hdr);
  215. if (rc < 0 || buf + rc > skb_tail_pointer(skb))
  216. return -EINVAL;
  217. pos += ieee802154_hdr_get_addrs(buf + pos, hdr);
  218. return pos;
  219. }
  220. EXPORT_SYMBOL_GPL(ieee802154_hdr_peek_addrs);
  221. int
  222. ieee802154_hdr_peek(const struct sk_buff *skb, struct ieee802154_hdr *hdr)
  223. {
  224. const u8 *buf = skb_mac_header(skb);
  225. int pos;
  226. pos = ieee802154_hdr_peek_addrs(skb, hdr);
  227. if (pos < 0)
  228. return -EINVAL;
  229. if (hdr->fc.security_enabled) {
  230. u8 key_id_mode = IEEE802154_SCF_KEY_ID_MODE(*(buf + pos));
  231. int want = pos + ieee802154_sechdr_lengths[key_id_mode];
  232. if (buf + want > skb_tail_pointer(skb))
  233. return -EINVAL;
  234. pos += ieee802154_hdr_get_sechdr(buf + pos, &hdr->sec);
  235. }
  236. return pos;
  237. }
  238. EXPORT_SYMBOL_GPL(ieee802154_hdr_peek);
  239. int ieee802154_max_payload(const struct ieee802154_hdr *hdr)
  240. {
  241. int hlen = ieee802154_hdr_minlen(hdr);
  242. if (hdr->fc.security_enabled) {
  243. hlen += ieee802154_sechdr_lengths[hdr->sec.key_id_mode] - 1;
  244. hlen += ieee802154_sechdr_authtag_len(&hdr->sec);
  245. }
  246. return IEEE802154_MTU - hlen - IEEE802154_MFR_SIZE;
  247. }
  248. EXPORT_SYMBOL_GPL(ieee802154_max_payload);