packet.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner, Simon Wunderlich
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #ifndef _NET_BATMAN_ADV_PACKET_H_
  20. #define _NET_BATMAN_ADV_PACKET_H_
  21. /**
  22. * enum batadv_packettype - types for batman-adv encapsulated packets
  23. * @BATADV_IV_OGM: originator messages for B.A.T.M.A.N. IV
  24. * @BATADV_BCAST: broadcast packets carrying broadcast payload
  25. * @BATADV_CODED: network coded packets
  26. *
  27. * @BATADV_UNICAST: unicast packets carrying unicast payload traffic
  28. * @BATADV_UNICAST_FRAG: unicast packets carrying a fragment of the original
  29. * payload packet
  30. * @BATADV_UNICAST_4ADDR: unicast packet including the originator address of
  31. * the sender
  32. * @BATADV_ICMP: unicast packet like IP ICMP used for ping or traceroute
  33. * @BATADV_UNICAST_TVLV: unicast packet carrying TVLV containers
  34. */
  35. enum batadv_packettype {
  36. /* 0x00 - 0x3f: local packets or special rules for handling */
  37. BATADV_IV_OGM = 0x00,
  38. BATADV_BCAST = 0x01,
  39. BATADV_CODED = 0x02,
  40. /* 0x40 - 0x7f: unicast */
  41. #define BATADV_UNICAST_MIN 0x40
  42. BATADV_UNICAST = 0x40,
  43. BATADV_UNICAST_FRAG = 0x41,
  44. BATADV_UNICAST_4ADDR = 0x42,
  45. BATADV_ICMP = 0x43,
  46. BATADV_UNICAST_TVLV = 0x44,
  47. #define BATADV_UNICAST_MAX 0x7f
  48. /* 0x80 - 0xff: reserved */
  49. };
  50. /**
  51. * enum batadv_subtype - packet subtype for unicast4addr
  52. * @BATADV_P_DATA: user payload
  53. * @BATADV_P_DAT_DHT_GET: DHT request message
  54. * @BATADV_P_DAT_DHT_PUT: DHT store message
  55. * @BATADV_P_DAT_CACHE_REPLY: ARP reply generated by DAT
  56. */
  57. enum batadv_subtype {
  58. BATADV_P_DATA = 0x01,
  59. BATADV_P_DAT_DHT_GET = 0x02,
  60. BATADV_P_DAT_DHT_PUT = 0x03,
  61. BATADV_P_DAT_CACHE_REPLY = 0x04,
  62. };
  63. /* this file is included by batctl which needs these defines */
  64. #define BATADV_COMPAT_VERSION 15
  65. /**
  66. * enum batadv_iv_flags - flags used in B.A.T.M.A.N. IV OGM packets
  67. * @BATADV_NOT_BEST_NEXT_HOP: flag is set when ogm packet is forwarded and was
  68. * previously received from someone else than the best neighbor.
  69. * @BATADV_PRIMARIES_FIRST_HOP: flag is set when the primary interface address
  70. * is used, and the packet travels its first hop.
  71. * @BATADV_DIRECTLINK: flag is for the first hop or if rebroadcasted from a
  72. * one hop neighbor on the interface where it was originally received.
  73. */
  74. enum batadv_iv_flags {
  75. BATADV_NOT_BEST_NEXT_HOP = BIT(0),
  76. BATADV_PRIMARIES_FIRST_HOP = BIT(1),
  77. BATADV_DIRECTLINK = BIT(2),
  78. };
  79. /* ICMP message types */
  80. enum batadv_icmp_packettype {
  81. BATADV_ECHO_REPLY = 0,
  82. BATADV_DESTINATION_UNREACHABLE = 3,
  83. BATADV_ECHO_REQUEST = 8,
  84. BATADV_TTL_EXCEEDED = 11,
  85. BATADV_PARAMETER_PROBLEM = 12,
  86. };
  87. /* fragmentation defines */
  88. enum batadv_unicast_frag_flags {
  89. BATADV_UNI_FRAG_HEAD = BIT(0),
  90. BATADV_UNI_FRAG_LARGETAIL = BIT(1),
  91. };
  92. /* tt data subtypes */
  93. #define BATADV_TT_DATA_TYPE_MASK 0x0F
  94. /**
  95. * enum batadv_tt_data_flags - flags for tt data tvlv
  96. * @BATADV_TT_OGM_DIFF: TT diff propagated through OGM
  97. * @BATADV_TT_REQUEST: TT request message
  98. * @BATADV_TT_RESPONSE: TT response message
  99. * @BATADV_TT_FULL_TABLE: contains full table to replace existing table
  100. */
  101. enum batadv_tt_data_flags {
  102. BATADV_TT_OGM_DIFF = BIT(0),
  103. BATADV_TT_REQUEST = BIT(1),
  104. BATADV_TT_RESPONSE = BIT(2),
  105. BATADV_TT_FULL_TABLE = BIT(4),
  106. };
  107. /* BATADV_TT_CLIENT flags.
  108. * Flags from BIT(0) to BIT(7) are sent on the wire, while flags from BIT(8) to
  109. * BIT(15) are used for local computation only
  110. */
  111. enum batadv_tt_client_flags {
  112. BATADV_TT_CLIENT_DEL = BIT(0),
  113. BATADV_TT_CLIENT_ROAM = BIT(1),
  114. BATADV_TT_CLIENT_WIFI = BIT(2),
  115. BATADV_TT_CLIENT_NOPURGE = BIT(8),
  116. BATADV_TT_CLIENT_NEW = BIT(9),
  117. BATADV_TT_CLIENT_PENDING = BIT(10),
  118. BATADV_TT_CLIENT_TEMP = BIT(11),
  119. };
  120. /* claim frame types for the bridge loop avoidance */
  121. enum batadv_bla_claimframe {
  122. BATADV_CLAIM_TYPE_CLAIM = 0x00,
  123. BATADV_CLAIM_TYPE_UNCLAIM = 0x01,
  124. BATADV_CLAIM_TYPE_ANNOUNCE = 0x02,
  125. BATADV_CLAIM_TYPE_REQUEST = 0x03,
  126. };
  127. /**
  128. * enum batadv_tvlv_type - tvlv type definitions
  129. * @BATADV_TVLV_GW: gateway tvlv
  130. * @BATADV_TVLV_DAT: distributed arp table tvlv
  131. * @BATADV_TVLV_NC: network coding tvlv
  132. * @BATADV_TVLV_TT: translation table tvlv
  133. * @BATADV_TVLV_ROAM: roaming advertisement tvlv
  134. */
  135. enum batadv_tvlv_type {
  136. BATADV_TVLV_GW = 0x01,
  137. BATADV_TVLV_DAT = 0x02,
  138. BATADV_TVLV_NC = 0x03,
  139. BATADV_TVLV_TT = 0x04,
  140. BATADV_TVLV_ROAM = 0x05,
  141. };
  142. /* the destination hardware field in the ARP frame is used to
  143. * transport the claim type and the group id
  144. */
  145. struct batadv_bla_claim_dst {
  146. uint8_t magic[3]; /* FF:43:05 */
  147. uint8_t type; /* bla_claimframe */
  148. __be16 group; /* group id */
  149. };
  150. struct batadv_header {
  151. uint8_t packet_type;
  152. uint8_t version; /* batman version field */
  153. uint8_t ttl;
  154. /* the parent struct has to add a byte after the header to make
  155. * everything 4 bytes aligned again
  156. */
  157. };
  158. /**
  159. * struct batadv_ogm_packet - ogm (routing protocol) packet
  160. * @header: common batman packet header
  161. * @flags: contains routing relevant flags - see enum batadv_iv_flags
  162. * @tvlv_len: length of tvlv data following the ogm header
  163. */
  164. struct batadv_ogm_packet {
  165. struct batadv_header header;
  166. uint8_t flags;
  167. __be32 seqno;
  168. uint8_t orig[ETH_ALEN];
  169. uint8_t prev_sender[ETH_ALEN];
  170. uint8_t reserved;
  171. uint8_t tq;
  172. __be16 tvlv_len;
  173. /* __packed is not needed as the struct size is divisible by 4,
  174. * and the largest data type in this struct has a size of 4.
  175. */
  176. };
  177. #define BATADV_OGM_HLEN sizeof(struct batadv_ogm_packet)
  178. struct batadv_icmp_packet {
  179. struct batadv_header header;
  180. uint8_t msg_type; /* see ICMP message types above */
  181. uint8_t dst[ETH_ALEN];
  182. uint8_t orig[ETH_ALEN];
  183. __be16 seqno;
  184. uint8_t uid;
  185. uint8_t reserved;
  186. };
  187. #define BATADV_RR_LEN 16
  188. /* icmp_packet_rr must start with all fields from imcp_packet
  189. * as this is assumed by code that handles ICMP packets
  190. */
  191. struct batadv_icmp_packet_rr {
  192. struct batadv_header header;
  193. uint8_t msg_type; /* see ICMP message types above */
  194. uint8_t dst[ETH_ALEN];
  195. uint8_t orig[ETH_ALEN];
  196. __be16 seqno;
  197. uint8_t uid;
  198. uint8_t rr_cur;
  199. uint8_t rr[BATADV_RR_LEN][ETH_ALEN];
  200. };
  201. /* All packet headers in front of an ethernet header have to be completely
  202. * divisible by 2 but not by 4 to make the payload after the ethernet
  203. * header again 4 bytes boundary aligned.
  204. *
  205. * A packing of 2 is necessary to avoid extra padding at the end of the struct
  206. * caused by a structure member which is larger than two bytes. Otherwise
  207. * the structure would not fulfill the previously mentioned rule to avoid the
  208. * misalignment of the payload after the ethernet header. It may also lead to
  209. * leakage of information when the padding it not initialized before sending.
  210. */
  211. #pragma pack(2)
  212. struct batadv_unicast_packet {
  213. struct batadv_header header;
  214. uint8_t ttvn; /* destination translation table version number */
  215. uint8_t dest[ETH_ALEN];
  216. /* "4 bytes boundary + 2 bytes" long to make the payload after the
  217. * following ethernet header again 4 bytes boundary aligned
  218. */
  219. };
  220. /**
  221. * struct batadv_unicast_4addr_packet - extended unicast packet
  222. * @u: common unicast packet header
  223. * @src: address of the source
  224. * @subtype: packet subtype
  225. */
  226. struct batadv_unicast_4addr_packet {
  227. struct batadv_unicast_packet u;
  228. uint8_t src[ETH_ALEN];
  229. uint8_t subtype;
  230. uint8_t reserved;
  231. /* "4 bytes boundary + 2 bytes" long to make the payload after the
  232. * following ethernet header again 4 bytes boundary aligned
  233. */
  234. };
  235. struct batadv_unicast_frag_packet {
  236. struct batadv_header header;
  237. uint8_t ttvn; /* destination translation table version number */
  238. uint8_t dest[ETH_ALEN];
  239. uint8_t flags;
  240. uint8_t align;
  241. uint8_t orig[ETH_ALEN];
  242. __be16 seqno;
  243. } __packed;
  244. struct batadv_bcast_packet {
  245. struct batadv_header header;
  246. uint8_t reserved;
  247. __be32 seqno;
  248. uint8_t orig[ETH_ALEN];
  249. /* "4 bytes boundary + 2 bytes" long to make the payload after the
  250. * following ethernet header again 4 bytes boundary aligned
  251. */
  252. };
  253. #pragma pack()
  254. /**
  255. * struct batadv_coded_packet - network coded packet
  256. * @header: common batman packet header and ttl of first included packet
  257. * @reserved: Align following fields to 2-byte boundaries
  258. * @first_source: original source of first included packet
  259. * @first_orig_dest: original destinal of first included packet
  260. * @first_crc: checksum of first included packet
  261. * @first_ttvn: tt-version number of first included packet
  262. * @second_ttl: ttl of second packet
  263. * @second_dest: second receiver of this coded packet
  264. * @second_source: original source of second included packet
  265. * @second_orig_dest: original destination of second included packet
  266. * @second_crc: checksum of second included packet
  267. * @second_ttvn: tt version number of second included packet
  268. * @coded_len: length of network coded part of the payload
  269. */
  270. struct batadv_coded_packet {
  271. struct batadv_header header;
  272. uint8_t first_ttvn;
  273. /* uint8_t first_dest[ETH_ALEN]; - saved in mac header destination */
  274. uint8_t first_source[ETH_ALEN];
  275. uint8_t first_orig_dest[ETH_ALEN];
  276. __be32 first_crc;
  277. uint8_t second_ttl;
  278. uint8_t second_ttvn;
  279. uint8_t second_dest[ETH_ALEN];
  280. uint8_t second_source[ETH_ALEN];
  281. uint8_t second_orig_dest[ETH_ALEN];
  282. __be32 second_crc;
  283. __be16 coded_len;
  284. };
  285. /**
  286. * struct batadv_unicast_tvlv - generic unicast packet with tvlv payload
  287. * @header: common batman packet header
  288. * @reserved: reserved field (for packet alignment)
  289. * @src: address of the source
  290. * @dst: address of the destination
  291. * @tvlv_len: length of tvlv data following the unicast tvlv header
  292. * @align: 2 bytes to align the header to a 4 byte boundry
  293. */
  294. struct batadv_unicast_tvlv_packet {
  295. struct batadv_header header;
  296. uint8_t reserved;
  297. uint8_t dst[ETH_ALEN];
  298. uint8_t src[ETH_ALEN];
  299. __be16 tvlv_len;
  300. uint16_t align;
  301. };
  302. /**
  303. * struct batadv_tvlv_hdr - base tvlv header struct
  304. * @type: tvlv container type (see batadv_tvlv_type)
  305. * @version: tvlv container version
  306. * @len: tvlv container length
  307. */
  308. struct batadv_tvlv_hdr {
  309. uint8_t type;
  310. uint8_t version;
  311. __be16 len;
  312. };
  313. /**
  314. * struct batadv_tvlv_gateway_data - gateway data propagated through gw tvlv
  315. * container
  316. * @bandwidth_down: advertised uplink download bandwidth
  317. * @bandwidth_up: advertised uplink upload bandwidth
  318. */
  319. struct batadv_tvlv_gateway_data {
  320. __be32 bandwidth_down;
  321. __be32 bandwidth_up;
  322. };
  323. /**
  324. * struct batadv_tvlv_tt_data - tt data propagated through the tt tvlv container
  325. * @flags: translation table flags (see batadv_tt_data_flags)
  326. * @ttvn: translation table version number
  327. * @reserved: field reserved for future use
  328. * @crc: crc32 checksum of the local translation table
  329. */
  330. struct batadv_tvlv_tt_data {
  331. uint8_t flags;
  332. uint8_t ttvn;
  333. uint16_t reserved;
  334. __be32 crc;
  335. };
  336. /**
  337. * struct batadv_tvlv_tt_change - translation table diff data
  338. * @flags: status indicators concerning the non-mesh client (see
  339. * batadv_tt_client_flags)
  340. * @reserved: reserved field
  341. * @addr: mac address of non-mesh client that triggered this tt change
  342. */
  343. struct batadv_tvlv_tt_change {
  344. uint8_t flags;
  345. uint8_t reserved;
  346. uint8_t addr[ETH_ALEN];
  347. };
  348. /**
  349. * struct batadv_tvlv_roam_adv - roaming advertisement
  350. * @client: mac address of roaming client
  351. * @reserved: field reserved for future use
  352. */
  353. struct batadv_tvlv_roam_adv {
  354. uint8_t client[ETH_ALEN];
  355. uint16_t reserved;
  356. };
  357. #endif /* _NET_BATMAN_ADV_PACKET_H_ */