header_ops.c 6.5 KB

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