packet.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. /* tt data subtypes */
  88. #define BATADV_TT_DATA_TYPE_MASK 0x0F
  89. /**
  90. * enum batadv_tt_data_flags - flags for tt data tvlv
  91. * @BATADV_TT_OGM_DIFF: TT diff propagated through OGM
  92. * @BATADV_TT_REQUEST: TT request message
  93. * @BATADV_TT_RESPONSE: TT response message
  94. * @BATADV_TT_FULL_TABLE: contains full table to replace existing table
  95. */
  96. enum batadv_tt_data_flags {
  97. BATADV_TT_OGM_DIFF = BIT(0),
  98. BATADV_TT_REQUEST = BIT(1),
  99. BATADV_TT_RESPONSE = BIT(2),
  100. BATADV_TT_FULL_TABLE = BIT(4),
  101. };
  102. /* BATADV_TT_CLIENT flags.
  103. * Flags from BIT(0) to BIT(7) are sent on the wire, while flags from BIT(8) to
  104. * BIT(15) are used for local computation only
  105. */
  106. enum batadv_tt_client_flags {
  107. BATADV_TT_CLIENT_DEL = BIT(0),
  108. BATADV_TT_CLIENT_ROAM = BIT(1),
  109. BATADV_TT_CLIENT_WIFI = BIT(2),
  110. BATADV_TT_CLIENT_NOPURGE = BIT(8),
  111. BATADV_TT_CLIENT_NEW = BIT(9),
  112. BATADV_TT_CLIENT_PENDING = BIT(10),
  113. BATADV_TT_CLIENT_TEMP = BIT(11),
  114. };
  115. /* claim frame types for the bridge loop avoidance */
  116. enum batadv_bla_claimframe {
  117. BATADV_CLAIM_TYPE_CLAIM = 0x00,
  118. BATADV_CLAIM_TYPE_UNCLAIM = 0x01,
  119. BATADV_CLAIM_TYPE_ANNOUNCE = 0x02,
  120. BATADV_CLAIM_TYPE_REQUEST = 0x03,
  121. };
  122. /**
  123. * enum batadv_tvlv_type - tvlv type definitions
  124. * @BATADV_TVLV_GW: gateway tvlv
  125. * @BATADV_TVLV_DAT: distributed arp table tvlv
  126. * @BATADV_TVLV_NC: network coding tvlv
  127. * @BATADV_TVLV_TT: translation table tvlv
  128. * @BATADV_TVLV_ROAM: roaming advertisement tvlv
  129. */
  130. enum batadv_tvlv_type {
  131. BATADV_TVLV_GW = 0x01,
  132. BATADV_TVLV_DAT = 0x02,
  133. BATADV_TVLV_NC = 0x03,
  134. BATADV_TVLV_TT = 0x04,
  135. BATADV_TVLV_ROAM = 0x05,
  136. };
  137. /* the destination hardware field in the ARP frame is used to
  138. * transport the claim type and the group id
  139. */
  140. struct batadv_bla_claim_dst {
  141. uint8_t magic[3]; /* FF:43:05 */
  142. uint8_t type; /* bla_claimframe */
  143. __be16 group; /* group id */
  144. };
  145. struct batadv_header {
  146. uint8_t packet_type;
  147. uint8_t version; /* batman version field */
  148. uint8_t ttl;
  149. /* the parent struct has to add a byte after the header to make
  150. * everything 4 bytes aligned again
  151. */
  152. };
  153. /**
  154. * struct batadv_ogm_packet - ogm (routing protocol) packet
  155. * @header: common batman packet header
  156. * @flags: contains routing relevant flags - see enum batadv_iv_flags
  157. * @tvlv_len: length of tvlv data following the ogm header
  158. */
  159. struct batadv_ogm_packet {
  160. struct batadv_header header;
  161. uint8_t flags;
  162. __be32 seqno;
  163. uint8_t orig[ETH_ALEN];
  164. uint8_t prev_sender[ETH_ALEN];
  165. uint8_t reserved;
  166. uint8_t tq;
  167. __be16 tvlv_len;
  168. /* __packed is not needed as the struct size is divisible by 4,
  169. * and the largest data type in this struct has a size of 4.
  170. */
  171. };
  172. #define BATADV_OGM_HLEN sizeof(struct batadv_ogm_packet)
  173. /**
  174. * batadv_icmp_header - common ICMP header
  175. * @header: common batman header
  176. * @msg_type: ICMP packet type
  177. * @dst: address of the destination node
  178. * @orig: address of the source node
  179. * @uid: local ICMP socket identifier
  180. */
  181. struct batadv_icmp_header {
  182. struct batadv_header header;
  183. uint8_t msg_type; /* see ICMP message types above */
  184. uint8_t dst[ETH_ALEN];
  185. uint8_t orig[ETH_ALEN];
  186. uint8_t uid;
  187. };
  188. /**
  189. * batadv_icmp_packet - ICMP packet
  190. * @icmph: common ICMP header
  191. * @reserved: not used - useful for alignment
  192. * @seqno: ICMP sequence number
  193. */
  194. struct batadv_icmp_packet {
  195. struct batadv_icmp_header icmph;
  196. uint8_t reserved;
  197. __be16 seqno;
  198. };
  199. #define BATADV_RR_LEN 16
  200. /**
  201. * batadv_icmp_packet_rr - ICMP RouteRecord packet
  202. * @icmph: common ICMP header
  203. * @rr_cur: number of entries the rr array
  204. * @seqno: ICMP sequence number
  205. * @rr: route record array
  206. */
  207. struct batadv_icmp_packet_rr {
  208. struct batadv_icmp_header icmph;
  209. uint8_t rr_cur;
  210. __be16 seqno;
  211. uint8_t rr[BATADV_RR_LEN][ETH_ALEN];
  212. };
  213. /* All packet headers in front of an ethernet header have to be completely
  214. * divisible by 2 but not by 4 to make the payload after the ethernet
  215. * header again 4 bytes boundary aligned.
  216. *
  217. * A packing of 2 is necessary to avoid extra padding at the end of the struct
  218. * caused by a structure member which is larger than two bytes. Otherwise
  219. * the structure would not fulfill the previously mentioned rule to avoid the
  220. * misalignment of the payload after the ethernet header. It may also lead to
  221. * leakage of information when the padding it not initialized before sending.
  222. */
  223. #pragma pack(2)
  224. struct batadv_unicast_packet {
  225. struct batadv_header header;
  226. uint8_t ttvn; /* destination translation table version number */
  227. uint8_t dest[ETH_ALEN];
  228. /* "4 bytes boundary + 2 bytes" long to make the payload after the
  229. * following ethernet header again 4 bytes boundary aligned
  230. */
  231. };
  232. /**
  233. * struct batadv_unicast_4addr_packet - extended unicast packet
  234. * @u: common unicast packet header
  235. * @src: address of the source
  236. * @subtype: packet subtype
  237. */
  238. struct batadv_unicast_4addr_packet {
  239. struct batadv_unicast_packet u;
  240. uint8_t src[ETH_ALEN];
  241. uint8_t subtype;
  242. uint8_t reserved;
  243. /* "4 bytes boundary + 2 bytes" long to make the payload after the
  244. * following ethernet header again 4 bytes boundary aligned
  245. */
  246. };
  247. /**
  248. * struct batadv_frag_packet - fragmented packet
  249. * @header: common batman packet header with type, compatversion, and ttl
  250. * @dest: final destination used when routing fragments
  251. * @orig: originator of the fragment used when merging the packet
  252. * @no: fragment number within this sequence
  253. * @reserved: reserved byte for alignment
  254. * @seqno: sequence identification
  255. * @total_size: size of the merged packet
  256. */
  257. struct batadv_frag_packet {
  258. struct batadv_header header;
  259. #if defined(__BIG_ENDIAN_BITFIELD)
  260. uint8_t no:4;
  261. uint8_t reserved:4;
  262. #elif defined(__LITTLE_ENDIAN_BITFIELD)
  263. uint8_t reserved:4;
  264. uint8_t no:4;
  265. #else
  266. #error "unknown bitfield endianess"
  267. #endif
  268. uint8_t dest[ETH_ALEN];
  269. uint8_t orig[ETH_ALEN];
  270. __be16 seqno;
  271. __be16 total_size;
  272. };
  273. struct batadv_bcast_packet {
  274. struct batadv_header header;
  275. uint8_t reserved;
  276. __be32 seqno;
  277. uint8_t orig[ETH_ALEN];
  278. /* "4 bytes boundary + 2 bytes" long to make the payload after the
  279. * following ethernet header again 4 bytes boundary aligned
  280. */
  281. };
  282. #pragma pack()
  283. /**
  284. * struct batadv_coded_packet - network coded packet
  285. * @header: common batman packet header and ttl of first included packet
  286. * @reserved: Align following fields to 2-byte boundaries
  287. * @first_source: original source of first included packet
  288. * @first_orig_dest: original destinal of first included packet
  289. * @first_crc: checksum of first included packet
  290. * @first_ttvn: tt-version number of first included packet
  291. * @second_ttl: ttl of second packet
  292. * @second_dest: second receiver of this coded packet
  293. * @second_source: original source of second included packet
  294. * @second_orig_dest: original destination of second included packet
  295. * @second_crc: checksum of second included packet
  296. * @second_ttvn: tt version number of second included packet
  297. * @coded_len: length of network coded part of the payload
  298. */
  299. struct batadv_coded_packet {
  300. struct batadv_header header;
  301. uint8_t first_ttvn;
  302. /* uint8_t first_dest[ETH_ALEN]; - saved in mac header destination */
  303. uint8_t first_source[ETH_ALEN];
  304. uint8_t first_orig_dest[ETH_ALEN];
  305. __be32 first_crc;
  306. uint8_t second_ttl;
  307. uint8_t second_ttvn;
  308. uint8_t second_dest[ETH_ALEN];
  309. uint8_t second_source[ETH_ALEN];
  310. uint8_t second_orig_dest[ETH_ALEN];
  311. __be32 second_crc;
  312. __be16 coded_len;
  313. };
  314. /**
  315. * struct batadv_unicast_tvlv - generic unicast packet with tvlv payload
  316. * @header: common batman packet header
  317. * @reserved: reserved field (for packet alignment)
  318. * @src: address of the source
  319. * @dst: address of the destination
  320. * @tvlv_len: length of tvlv data following the unicast tvlv header
  321. * @align: 2 bytes to align the header to a 4 byte boundry
  322. */
  323. struct batadv_unicast_tvlv_packet {
  324. struct batadv_header header;
  325. uint8_t reserved;
  326. uint8_t dst[ETH_ALEN];
  327. uint8_t src[ETH_ALEN];
  328. __be16 tvlv_len;
  329. uint16_t align;
  330. };
  331. /**
  332. * struct batadv_tvlv_hdr - base tvlv header struct
  333. * @type: tvlv container type (see batadv_tvlv_type)
  334. * @version: tvlv container version
  335. * @len: tvlv container length
  336. */
  337. struct batadv_tvlv_hdr {
  338. uint8_t type;
  339. uint8_t version;
  340. __be16 len;
  341. };
  342. /**
  343. * struct batadv_tvlv_gateway_data - gateway data propagated through gw tvlv
  344. * container
  345. * @bandwidth_down: advertised uplink download bandwidth
  346. * @bandwidth_up: advertised uplink upload bandwidth
  347. */
  348. struct batadv_tvlv_gateway_data {
  349. __be32 bandwidth_down;
  350. __be32 bandwidth_up;
  351. };
  352. /**
  353. * struct batadv_tvlv_tt_data - tt data propagated through the tt tvlv container
  354. * @flags: translation table flags (see batadv_tt_data_flags)
  355. * @ttvn: translation table version number
  356. * @reserved: field reserved for future use
  357. * @crc: crc32 checksum of the local translation table
  358. */
  359. struct batadv_tvlv_tt_data {
  360. uint8_t flags;
  361. uint8_t ttvn;
  362. uint16_t reserved;
  363. __be32 crc;
  364. };
  365. /**
  366. * struct batadv_tvlv_tt_change - translation table diff data
  367. * @flags: status indicators concerning the non-mesh client (see
  368. * batadv_tt_client_flags)
  369. * @reserved: reserved field
  370. * @addr: mac address of non-mesh client that triggered this tt change
  371. */
  372. struct batadv_tvlv_tt_change {
  373. uint8_t flags;
  374. uint8_t reserved;
  375. uint8_t addr[ETH_ALEN];
  376. };
  377. /**
  378. * struct batadv_tvlv_roam_adv - roaming advertisement
  379. * @client: mac address of roaming client
  380. * @reserved: field reserved for future use
  381. */
  382. struct batadv_tvlv_roam_adv {
  383. uint8_t client[ETH_ALEN];
  384. uint16_t reserved;
  385. };
  386. #endif /* _NET_BATMAN_ADV_PACKET_H_ */