packet.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /* Copyright (C) 2007-2015 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, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef _NET_BATMAN_ADV_PACKET_H_
  18. #define _NET_BATMAN_ADV_PACKET_H_
  19. #include <asm/byteorder.h>
  20. #include <linux/types.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. /**
  88. * enum batadv_mcast_flags - flags for multicast capabilities and settings
  89. * @BATADV_MCAST_WANT_ALL_UNSNOOPABLES: we want all packets destined for
  90. * 224.0.0.0/24 or ff02::1
  91. * @BATADV_MCAST_WANT_ALL_IPV4: we want all IPv4 multicast packets
  92. * @BATADV_MCAST_WANT_ALL_IPV6: we want all IPv6 multicast packets
  93. */
  94. enum batadv_mcast_flags {
  95. BATADV_MCAST_WANT_ALL_UNSNOOPABLES = BIT(0),
  96. BATADV_MCAST_WANT_ALL_IPV4 = BIT(1),
  97. BATADV_MCAST_WANT_ALL_IPV6 = BIT(2),
  98. };
  99. /* tt data subtypes */
  100. #define BATADV_TT_DATA_TYPE_MASK 0x0F
  101. /**
  102. * enum batadv_tt_data_flags - flags for tt data tvlv
  103. * @BATADV_TT_OGM_DIFF: TT diff propagated through OGM
  104. * @BATADV_TT_REQUEST: TT request message
  105. * @BATADV_TT_RESPONSE: TT response message
  106. * @BATADV_TT_FULL_TABLE: contains full table to replace existing table
  107. */
  108. enum batadv_tt_data_flags {
  109. BATADV_TT_OGM_DIFF = BIT(0),
  110. BATADV_TT_REQUEST = BIT(1),
  111. BATADV_TT_RESPONSE = BIT(2),
  112. BATADV_TT_FULL_TABLE = BIT(4),
  113. };
  114. /**
  115. * enum batadv_tt_client_flags - TT client specific flags
  116. * @BATADV_TT_CLIENT_DEL: the client has to be deleted from the table
  117. * @BATADV_TT_CLIENT_ROAM: the client roamed to/from another node and the new
  118. * update telling its new real location has not been received/sent yet
  119. * @BATADV_TT_CLIENT_WIFI: this client is connected through a wifi interface.
  120. * This information is used by the "AP Isolation" feature
  121. * @BATADV_TT_CLIENT_ISOLA: this client is considered "isolated". This
  122. * information is used by the Extended Isolation feature
  123. * @BATADV_TT_CLIENT_NOPURGE: this client should never be removed from the table
  124. * @BATADV_TT_CLIENT_NEW: this client has been added to the local table but has
  125. * not been announced yet
  126. * @BATADV_TT_CLIENT_PENDING: this client is marked for removal but it is kept
  127. * in the table for one more originator interval for consistency purposes
  128. * @BATADV_TT_CLIENT_TEMP: this global client has been detected to be part of
  129. * the network but no nnode has already announced it
  130. *
  131. * Bits from 0 to 7 are called _remote flags_ because they are sent on the wire.
  132. * Bits from 8 to 15 are called _local flags_ because they are used for local
  133. * computations only.
  134. *
  135. * Bits from 4 to 7 - a subset of remote flags - are ensured to be in sync with
  136. * the other nodes in the network. To achieve this goal these flags are included
  137. * in the TT CRC computation.
  138. */
  139. enum batadv_tt_client_flags {
  140. BATADV_TT_CLIENT_DEL = BIT(0),
  141. BATADV_TT_CLIENT_ROAM = BIT(1),
  142. BATADV_TT_CLIENT_WIFI = BIT(4),
  143. BATADV_TT_CLIENT_ISOLA = BIT(5),
  144. BATADV_TT_CLIENT_NOPURGE = BIT(8),
  145. BATADV_TT_CLIENT_NEW = BIT(9),
  146. BATADV_TT_CLIENT_PENDING = BIT(10),
  147. BATADV_TT_CLIENT_TEMP = BIT(11),
  148. };
  149. /**
  150. * batadv_vlan_flags - flags for the four MSB of any vlan ID field
  151. * @BATADV_VLAN_HAS_TAG: whether the field contains a valid vlan tag or not
  152. */
  153. enum batadv_vlan_flags {
  154. BATADV_VLAN_HAS_TAG = BIT(15),
  155. };
  156. /* claim frame types for the bridge loop avoidance */
  157. enum batadv_bla_claimframe {
  158. BATADV_CLAIM_TYPE_CLAIM = 0x00,
  159. BATADV_CLAIM_TYPE_UNCLAIM = 0x01,
  160. BATADV_CLAIM_TYPE_ANNOUNCE = 0x02,
  161. BATADV_CLAIM_TYPE_REQUEST = 0x03,
  162. };
  163. /**
  164. * enum batadv_tvlv_type - tvlv type definitions
  165. * @BATADV_TVLV_GW: gateway tvlv
  166. * @BATADV_TVLV_DAT: distributed arp table tvlv
  167. * @BATADV_TVLV_NC: network coding tvlv
  168. * @BATADV_TVLV_TT: translation table tvlv
  169. * @BATADV_TVLV_ROAM: roaming advertisement tvlv
  170. * @BATADV_TVLV_MCAST: multicast capability tvlv
  171. */
  172. enum batadv_tvlv_type {
  173. BATADV_TVLV_GW = 0x01,
  174. BATADV_TVLV_DAT = 0x02,
  175. BATADV_TVLV_NC = 0x03,
  176. BATADV_TVLV_TT = 0x04,
  177. BATADV_TVLV_ROAM = 0x05,
  178. BATADV_TVLV_MCAST = 0x06,
  179. };
  180. #pragma pack(2)
  181. /* the destination hardware field in the ARP frame is used to
  182. * transport the claim type and the group id
  183. */
  184. struct batadv_bla_claim_dst {
  185. u8 magic[3]; /* FF:43:05 */
  186. u8 type; /* bla_claimframe */
  187. __be16 group; /* group id */
  188. };
  189. #pragma pack()
  190. /**
  191. * struct batadv_ogm_packet - ogm (routing protocol) packet
  192. * @packet_type: batman-adv packet type, part of the general header
  193. * @version: batman-adv protocol version, part of the genereal header
  194. * @ttl: time to live for this packet, part of the genereal header
  195. * @flags: contains routing relevant flags - see enum batadv_iv_flags
  196. * @tvlv_len: length of tvlv data following the ogm header
  197. */
  198. struct batadv_ogm_packet {
  199. u8 packet_type;
  200. u8 version;
  201. u8 ttl;
  202. u8 flags;
  203. __be32 seqno;
  204. u8 orig[ETH_ALEN];
  205. u8 prev_sender[ETH_ALEN];
  206. u8 reserved;
  207. u8 tq;
  208. __be16 tvlv_len;
  209. /* __packed is not needed as the struct size is divisible by 4,
  210. * and the largest data type in this struct has a size of 4.
  211. */
  212. };
  213. #define BATADV_OGM_HLEN sizeof(struct batadv_ogm_packet)
  214. /**
  215. * batadv_icmp_header - common members among all the ICMP packets
  216. * @packet_type: batman-adv packet type, part of the general header
  217. * @version: batman-adv protocol version, part of the genereal header
  218. * @ttl: time to live for this packet, part of the genereal header
  219. * @msg_type: ICMP packet type
  220. * @dst: address of the destination node
  221. * @orig: address of the source node
  222. * @uid: local ICMP socket identifier
  223. * @align: not used - useful for alignment purposes only
  224. *
  225. * This structure is used for ICMP packets parsing only and it is never sent
  226. * over the wire. The alignment field at the end is there to ensure that
  227. * members are padded the same way as they are in real packets.
  228. */
  229. struct batadv_icmp_header {
  230. u8 packet_type;
  231. u8 version;
  232. u8 ttl;
  233. u8 msg_type; /* see ICMP message types above */
  234. u8 dst[ETH_ALEN];
  235. u8 orig[ETH_ALEN];
  236. u8 uid;
  237. u8 align[3];
  238. };
  239. /**
  240. * batadv_icmp_packet - ICMP packet
  241. * @packet_type: batman-adv packet type, part of the general header
  242. * @version: batman-adv protocol version, part of the genereal header
  243. * @ttl: time to live for this packet, part of the genereal header
  244. * @msg_type: ICMP packet type
  245. * @dst: address of the destination node
  246. * @orig: address of the source node
  247. * @uid: local ICMP socket identifier
  248. * @reserved: not used - useful for alignment
  249. * @seqno: ICMP sequence number
  250. */
  251. struct batadv_icmp_packet {
  252. u8 packet_type;
  253. u8 version;
  254. u8 ttl;
  255. u8 msg_type; /* see ICMP message types above */
  256. u8 dst[ETH_ALEN];
  257. u8 orig[ETH_ALEN];
  258. u8 uid;
  259. u8 reserved;
  260. __be16 seqno;
  261. };
  262. #define BATADV_RR_LEN 16
  263. /**
  264. * batadv_icmp_packet_rr - ICMP RouteRecord packet
  265. * @packet_type: batman-adv packet type, part of the general header
  266. * @version: batman-adv protocol version, part of the genereal header
  267. * @ttl: time to live for this packet, part of the genereal header
  268. * @msg_type: ICMP packet type
  269. * @dst: address of the destination node
  270. * @orig: address of the source node
  271. * @uid: local ICMP socket identifier
  272. * @rr_cur: number of entries the rr array
  273. * @seqno: ICMP sequence number
  274. * @rr: route record array
  275. */
  276. struct batadv_icmp_packet_rr {
  277. u8 packet_type;
  278. u8 version;
  279. u8 ttl;
  280. u8 msg_type; /* see ICMP message types above */
  281. u8 dst[ETH_ALEN];
  282. u8 orig[ETH_ALEN];
  283. u8 uid;
  284. u8 rr_cur;
  285. __be16 seqno;
  286. u8 rr[BATADV_RR_LEN][ETH_ALEN];
  287. };
  288. #define BATADV_ICMP_MAX_PACKET_SIZE sizeof(struct batadv_icmp_packet_rr)
  289. /* All packet headers in front of an ethernet header have to be completely
  290. * divisible by 2 but not by 4 to make the payload after the ethernet
  291. * header again 4 bytes boundary aligned.
  292. *
  293. * A packing of 2 is necessary to avoid extra padding at the end of the struct
  294. * caused by a structure member which is larger than two bytes. Otherwise
  295. * the structure would not fulfill the previously mentioned rule to avoid the
  296. * misalignment of the payload after the ethernet header. It may also lead to
  297. * leakage of information when the padding it not initialized before sending.
  298. */
  299. #pragma pack(2)
  300. /**
  301. * struct batadv_unicast_packet - unicast packet for network payload
  302. * @packet_type: batman-adv packet type, part of the general header
  303. * @version: batman-adv protocol version, part of the genereal header
  304. * @ttl: time to live for this packet, part of the genereal header
  305. * @ttvn: translation table version number
  306. * @dest: originator destination of the unicast packet
  307. */
  308. struct batadv_unicast_packet {
  309. u8 packet_type;
  310. u8 version;
  311. u8 ttl;
  312. u8 ttvn; /* destination translation table version number */
  313. u8 dest[ETH_ALEN];
  314. /* "4 bytes boundary + 2 bytes" long to make the payload after the
  315. * following ethernet header again 4 bytes boundary aligned
  316. */
  317. };
  318. /**
  319. * struct batadv_unicast_4addr_packet - extended unicast packet
  320. * @u: common unicast packet header
  321. * @src: address of the source
  322. * @subtype: packet subtype
  323. */
  324. struct batadv_unicast_4addr_packet {
  325. struct batadv_unicast_packet u;
  326. u8 src[ETH_ALEN];
  327. u8 subtype;
  328. u8 reserved;
  329. /* "4 bytes boundary + 2 bytes" long to make the payload after the
  330. * following ethernet header again 4 bytes boundary aligned
  331. */
  332. };
  333. /**
  334. * struct batadv_frag_packet - fragmented packet
  335. * @packet_type: batman-adv packet type, part of the general header
  336. * @version: batman-adv protocol version, part of the genereal header
  337. * @ttl: time to live for this packet, part of the genereal header
  338. * @dest: final destination used when routing fragments
  339. * @orig: originator of the fragment used when merging the packet
  340. * @no: fragment number within this sequence
  341. * @reserved: reserved byte for alignment
  342. * @seqno: sequence identification
  343. * @total_size: size of the merged packet
  344. */
  345. struct batadv_frag_packet {
  346. u8 packet_type;
  347. u8 version; /* batman version field */
  348. u8 ttl;
  349. #if defined(__BIG_ENDIAN_BITFIELD)
  350. u8 no:4;
  351. u8 reserved:4;
  352. #elif defined(__LITTLE_ENDIAN_BITFIELD)
  353. u8 reserved:4;
  354. u8 no:4;
  355. #else
  356. #error "unknown bitfield endianness"
  357. #endif
  358. u8 dest[ETH_ALEN];
  359. u8 orig[ETH_ALEN];
  360. __be16 seqno;
  361. __be16 total_size;
  362. };
  363. /**
  364. * struct batadv_bcast_packet - broadcast packet for network payload
  365. * @packet_type: batman-adv packet type, part of the general header
  366. * @version: batman-adv protocol version, part of the genereal header
  367. * @ttl: time to live for this packet, part of the genereal header
  368. * @reserved: reserved byte for alignment
  369. * @seqno: sequence identification
  370. * @orig: originator of the broadcast packet
  371. */
  372. struct batadv_bcast_packet {
  373. u8 packet_type;
  374. u8 version; /* batman version field */
  375. u8 ttl;
  376. u8 reserved;
  377. __be32 seqno;
  378. u8 orig[ETH_ALEN];
  379. /* "4 bytes boundary + 2 bytes" long to make the payload after the
  380. * following ethernet header again 4 bytes boundary aligned
  381. */
  382. };
  383. /**
  384. * struct batadv_coded_packet - network coded packet
  385. * @packet_type: batman-adv packet type, part of the general header
  386. * @version: batman-adv protocol version, part of the genereal header
  387. * @ttl: time to live for this packet, part of the genereal header
  388. * @reserved: Align following fields to 2-byte boundaries
  389. * @first_source: original source of first included packet
  390. * @first_orig_dest: original destinal of first included packet
  391. * @first_crc: checksum of first included packet
  392. * @first_ttvn: tt-version number of first included packet
  393. * @second_ttl: ttl of second packet
  394. * @second_dest: second receiver of this coded packet
  395. * @second_source: original source of second included packet
  396. * @second_orig_dest: original destination of second included packet
  397. * @second_crc: checksum of second included packet
  398. * @second_ttvn: tt version number of second included packet
  399. * @coded_len: length of network coded part of the payload
  400. */
  401. struct batadv_coded_packet {
  402. u8 packet_type;
  403. u8 version; /* batman version field */
  404. u8 ttl;
  405. u8 first_ttvn;
  406. /* u8 first_dest[ETH_ALEN]; - saved in mac header destination */
  407. u8 first_source[ETH_ALEN];
  408. u8 first_orig_dest[ETH_ALEN];
  409. __be32 first_crc;
  410. u8 second_ttl;
  411. u8 second_ttvn;
  412. u8 second_dest[ETH_ALEN];
  413. u8 second_source[ETH_ALEN];
  414. u8 second_orig_dest[ETH_ALEN];
  415. __be32 second_crc;
  416. __be16 coded_len;
  417. };
  418. #pragma pack()
  419. /**
  420. * struct batadv_unicast_tvlv - generic unicast packet with tvlv payload
  421. * @packet_type: batman-adv packet type, part of the general header
  422. * @version: batman-adv protocol version, part of the genereal header
  423. * @ttl: time to live for this packet, part of the genereal header
  424. * @reserved: reserved field (for packet alignment)
  425. * @src: address of the source
  426. * @dst: address of the destination
  427. * @tvlv_len: length of tvlv data following the unicast tvlv header
  428. * @align: 2 bytes to align the header to a 4 byte boundary
  429. */
  430. struct batadv_unicast_tvlv_packet {
  431. u8 packet_type;
  432. u8 version; /* batman version field */
  433. u8 ttl;
  434. u8 reserved;
  435. u8 dst[ETH_ALEN];
  436. u8 src[ETH_ALEN];
  437. __be16 tvlv_len;
  438. u16 align;
  439. };
  440. /**
  441. * struct batadv_tvlv_hdr - base tvlv header struct
  442. * @type: tvlv container type (see batadv_tvlv_type)
  443. * @version: tvlv container version
  444. * @len: tvlv container length
  445. */
  446. struct batadv_tvlv_hdr {
  447. u8 type;
  448. u8 version;
  449. __be16 len;
  450. };
  451. /**
  452. * struct batadv_tvlv_gateway_data - gateway data propagated through gw tvlv
  453. * container
  454. * @bandwidth_down: advertised uplink download bandwidth
  455. * @bandwidth_up: advertised uplink upload bandwidth
  456. */
  457. struct batadv_tvlv_gateway_data {
  458. __be32 bandwidth_down;
  459. __be32 bandwidth_up;
  460. };
  461. /**
  462. * struct batadv_tvlv_tt_data - tt data propagated through the tt tvlv container
  463. * @flags: translation table flags (see batadv_tt_data_flags)
  464. * @ttvn: translation table version number
  465. * @vlan_num: number of announced VLANs. In the TVLV this struct is followed by
  466. * one batadv_tvlv_tt_vlan_data object per announced vlan
  467. */
  468. struct batadv_tvlv_tt_data {
  469. u8 flags;
  470. u8 ttvn;
  471. __be16 num_vlan;
  472. };
  473. /**
  474. * struct batadv_tvlv_tt_vlan_data - vlan specific tt data propagated through
  475. * the tt tvlv container
  476. * @crc: crc32 checksum of the entries belonging to this vlan
  477. * @vid: vlan identifier
  478. * @reserved: unused, useful for alignment purposes
  479. */
  480. struct batadv_tvlv_tt_vlan_data {
  481. __be32 crc;
  482. __be16 vid;
  483. u16 reserved;
  484. };
  485. /**
  486. * struct batadv_tvlv_tt_change - translation table diff data
  487. * @flags: status indicators concerning the non-mesh client (see
  488. * batadv_tt_client_flags)
  489. * @reserved: reserved field - useful for alignment purposes only
  490. * @addr: mac address of non-mesh client that triggered this tt change
  491. * @vid: VLAN identifier
  492. */
  493. struct batadv_tvlv_tt_change {
  494. u8 flags;
  495. u8 reserved[3];
  496. u8 addr[ETH_ALEN];
  497. __be16 vid;
  498. };
  499. /**
  500. * struct batadv_tvlv_roam_adv - roaming advertisement
  501. * @client: mac address of roaming client
  502. * @vid: VLAN identifier
  503. */
  504. struct batadv_tvlv_roam_adv {
  505. u8 client[ETH_ALEN];
  506. __be16 vid;
  507. };
  508. /**
  509. * struct batadv_tvlv_mcast_data - payload of a multicast tvlv
  510. * @flags: multicast flags announced by the orig node
  511. * @reserved: reserved field
  512. */
  513. struct batadv_tvlv_mcast_data {
  514. u8 flags;
  515. u8 reserved[3];
  516. };
  517. #endif /* _NET_BATMAN_ADV_PACKET_H_ */