batman-adv: Add SPDX license identifier above copyright header
[sfrench/cifs-2.6.git] / net / batman-adv / packet.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (C) 2007-2017  B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner, Simon Wunderlich
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifndef _NET_BATMAN_ADV_PACKET_H_
20 #define _NET_BATMAN_ADV_PACKET_H_
21
22 #include <asm/byteorder.h>
23 #include <linux/types.h>
24
25 #define batadv_tp_is_error(n) ((u8)(n) > 127 ? 1 : 0)
26
27 /**
28  * enum batadv_packettype - types for batman-adv encapsulated packets
29  * @BATADV_IV_OGM: originator messages for B.A.T.M.A.N. IV
30  * @BATADV_BCAST: broadcast packets carrying broadcast payload
31  * @BATADV_CODED: network coded packets
32  * @BATADV_ELP: echo location packets for B.A.T.M.A.N. V
33  * @BATADV_OGM2: originator messages for B.A.T.M.A.N. V
34  *
35  * @BATADV_UNICAST: unicast packets carrying unicast payload traffic
36  * @BATADV_UNICAST_FRAG: unicast packets carrying a fragment of the original
37  *     payload packet
38  * @BATADV_UNICAST_4ADDR: unicast packet including the originator address of
39  *     the sender
40  * @BATADV_ICMP: unicast packet like IP ICMP used for ping or traceroute
41  * @BATADV_UNICAST_TVLV: unicast packet carrying TVLV containers
42  */
43 enum batadv_packettype {
44         /* 0x00 - 0x3f: local packets or special rules for handling */
45         BATADV_IV_OGM           = 0x00,
46         BATADV_BCAST            = 0x01,
47         BATADV_CODED            = 0x02,
48         BATADV_ELP              = 0x03,
49         BATADV_OGM2             = 0x04,
50         /* 0x40 - 0x7f: unicast */
51 #define BATADV_UNICAST_MIN     0x40
52         BATADV_UNICAST          = 0x40,
53         BATADV_UNICAST_FRAG     = 0x41,
54         BATADV_UNICAST_4ADDR    = 0x42,
55         BATADV_ICMP             = 0x43,
56         BATADV_UNICAST_TVLV     = 0x44,
57 #define BATADV_UNICAST_MAX     0x7f
58         /* 0x80 - 0xff: reserved */
59 };
60
61 /**
62  * enum batadv_subtype - packet subtype for unicast4addr
63  * @BATADV_P_DATA: user payload
64  * @BATADV_P_DAT_DHT_GET: DHT request message
65  * @BATADV_P_DAT_DHT_PUT: DHT store message
66  * @BATADV_P_DAT_CACHE_REPLY: ARP reply generated by DAT
67  */
68 enum batadv_subtype {
69         BATADV_P_DATA                   = 0x01,
70         BATADV_P_DAT_DHT_GET            = 0x02,
71         BATADV_P_DAT_DHT_PUT            = 0x03,
72         BATADV_P_DAT_CACHE_REPLY        = 0x04,
73 };
74
75 /* this file is included by batctl which needs these defines */
76 #define BATADV_COMPAT_VERSION 15
77
78 /**
79  * enum batadv_iv_flags - flags used in B.A.T.M.A.N. IV OGM packets
80  * @BATADV_NOT_BEST_NEXT_HOP: flag is set when ogm packet is forwarded and was
81  *     previously received from someone else than the best neighbor.
82  * @BATADV_PRIMARIES_FIRST_HOP: flag unused.
83  * @BATADV_DIRECTLINK: flag is for the first hop or if rebroadcasted from a
84  *     one hop neighbor on the interface where it was originally received.
85  */
86 enum batadv_iv_flags {
87         BATADV_NOT_BEST_NEXT_HOP   = BIT(0),
88         BATADV_PRIMARIES_FIRST_HOP = BIT(1),
89         BATADV_DIRECTLINK          = BIT(2),
90 };
91
92 /* ICMP message types */
93 enum batadv_icmp_packettype {
94         BATADV_ECHO_REPLY              = 0,
95         BATADV_DESTINATION_UNREACHABLE = 3,
96         BATADV_ECHO_REQUEST            = 8,
97         BATADV_TTL_EXCEEDED            = 11,
98         BATADV_PARAMETER_PROBLEM       = 12,
99         BATADV_TP                      = 15,
100 };
101
102 /**
103  * enum batadv_mcast_flags - flags for multicast capabilities and settings
104  * @BATADV_MCAST_WANT_ALL_UNSNOOPABLES: we want all packets destined for
105  *  224.0.0.0/24 or ff02::1
106  * @BATADV_MCAST_WANT_ALL_IPV4: we want all IPv4 multicast packets
107  * @BATADV_MCAST_WANT_ALL_IPV6: we want all IPv6 multicast packets
108  */
109 enum batadv_mcast_flags {
110         BATADV_MCAST_WANT_ALL_UNSNOOPABLES      = BIT(0),
111         BATADV_MCAST_WANT_ALL_IPV4              = BIT(1),
112         BATADV_MCAST_WANT_ALL_IPV6              = BIT(2),
113 };
114
115 /* tt data subtypes */
116 #define BATADV_TT_DATA_TYPE_MASK 0x0F
117
118 /**
119  * enum batadv_tt_data_flags - flags for tt data tvlv
120  * @BATADV_TT_OGM_DIFF: TT diff propagated through OGM
121  * @BATADV_TT_REQUEST: TT request message
122  * @BATADV_TT_RESPONSE: TT response message
123  * @BATADV_TT_FULL_TABLE: contains full table to replace existing table
124  */
125 enum batadv_tt_data_flags {
126         BATADV_TT_OGM_DIFF   = BIT(0),
127         BATADV_TT_REQUEST    = BIT(1),
128         BATADV_TT_RESPONSE   = BIT(2),
129         BATADV_TT_FULL_TABLE = BIT(4),
130 };
131
132 /**
133  * enum batadv_vlan_flags - flags for the four MSB of any vlan ID field
134  * @BATADV_VLAN_HAS_TAG: whether the field contains a valid vlan tag or not
135  */
136 enum batadv_vlan_flags {
137         BATADV_VLAN_HAS_TAG     = BIT(15),
138 };
139
140 /* claim frame types for the bridge loop avoidance */
141 enum batadv_bla_claimframe {
142         BATADV_CLAIM_TYPE_CLAIM         = 0x00,
143         BATADV_CLAIM_TYPE_UNCLAIM       = 0x01,
144         BATADV_CLAIM_TYPE_ANNOUNCE      = 0x02,
145         BATADV_CLAIM_TYPE_REQUEST       = 0x03,
146         BATADV_CLAIM_TYPE_LOOPDETECT    = 0x04,
147 };
148
149 /**
150  * enum batadv_tvlv_type - tvlv type definitions
151  * @BATADV_TVLV_GW: gateway tvlv
152  * @BATADV_TVLV_DAT: distributed arp table tvlv
153  * @BATADV_TVLV_NC: network coding tvlv
154  * @BATADV_TVLV_TT: translation table tvlv
155  * @BATADV_TVLV_ROAM: roaming advertisement tvlv
156  * @BATADV_TVLV_MCAST: multicast capability tvlv
157  */
158 enum batadv_tvlv_type {
159         BATADV_TVLV_GW          = 0x01,
160         BATADV_TVLV_DAT         = 0x02,
161         BATADV_TVLV_NC          = 0x03,
162         BATADV_TVLV_TT          = 0x04,
163         BATADV_TVLV_ROAM        = 0x05,
164         BATADV_TVLV_MCAST       = 0x06,
165 };
166
167 #pragma pack(2)
168 /* the destination hardware field in the ARP frame is used to
169  * transport the claim type and the group id
170  */
171 struct batadv_bla_claim_dst {
172         u8     magic[3];        /* FF:43:05 */
173         u8     type;            /* bla_claimframe */
174         __be16 group;           /* group id */
175 };
176
177 #pragma pack()
178
179 /**
180  * struct batadv_ogm_packet - ogm (routing protocol) packet
181  * @packet_type: batman-adv packet type, part of the general header
182  * @version: batman-adv protocol version, part of the genereal header
183  * @ttl: time to live for this packet, part of the genereal header
184  * @flags: contains routing relevant flags - see enum batadv_iv_flags
185  * @seqno: sequence identification
186  * @orig: address of the source node
187  * @prev_sender: address of the previous sender
188  * @reserved: reserved byte for alignment
189  * @tq: transmission quality
190  * @tvlv_len: length of tvlv data following the ogm header
191  */
192 struct batadv_ogm_packet {
193         u8     packet_type;
194         u8     version;
195         u8     ttl;
196         u8     flags;
197         __be32 seqno;
198         u8     orig[ETH_ALEN];
199         u8     prev_sender[ETH_ALEN];
200         u8     reserved;
201         u8     tq;
202         __be16 tvlv_len;
203         /* __packed is not needed as the struct size is divisible by 4,
204          * and the largest data type in this struct has a size of 4.
205          */
206 };
207
208 #define BATADV_OGM_HLEN sizeof(struct batadv_ogm_packet)
209
210 /**
211  * struct batadv_ogm2_packet - ogm2 (routing protocol) packet
212  * @packet_type: batman-adv packet type, part of the general header
213  * @version: batman-adv protocol version, part of the general header
214  * @ttl: time to live for this packet, part of the general header
215  * @flags: reseved for routing relevant flags - currently always 0
216  * @seqno: sequence number
217  * @orig: originator mac address
218  * @tvlv_len: length of the appended tvlv buffer (in bytes)
219  * @throughput: the currently flooded path throughput
220  */
221 struct batadv_ogm2_packet {
222         u8     packet_type;
223         u8     version;
224         u8     ttl;
225         u8     flags;
226         __be32 seqno;
227         u8     orig[ETH_ALEN];
228         __be16 tvlv_len;
229         __be32 throughput;
230         /* __packed is not needed as the struct size is divisible by 4,
231          * and the largest data type in this struct has a size of 4.
232          */
233 };
234
235 #define BATADV_OGM2_HLEN sizeof(struct batadv_ogm2_packet)
236
237 /**
238  * struct batadv_elp_packet - elp (neighbor discovery) packet
239  * @packet_type: batman-adv packet type, part of the general header
240  * @version: batman-adv protocol version, part of the genereal header
241  * @orig: originator mac address
242  * @seqno: sequence number
243  * @elp_interval: currently used ELP sending interval in ms
244  */
245 struct batadv_elp_packet {
246         u8     packet_type;
247         u8     version;
248         u8     orig[ETH_ALEN];
249         __be32 seqno;
250         __be32 elp_interval;
251 };
252
253 #define BATADV_ELP_HLEN sizeof(struct batadv_elp_packet)
254
255 /**
256  * struct batadv_icmp_header - common members among all the ICMP packets
257  * @packet_type: batman-adv packet type, part of the general header
258  * @version: batman-adv protocol version, part of the genereal header
259  * @ttl: time to live for this packet, part of the genereal header
260  * @msg_type: ICMP packet type
261  * @dst: address of the destination node
262  * @orig: address of the source node
263  * @uid: local ICMP socket identifier
264  * @align: not used - useful for alignment purposes only
265  *
266  * This structure is used for ICMP packets parsing only and it is never sent
267  * over the wire. The alignment field at the end is there to ensure that
268  * members are padded the same way as they are in real packets.
269  */
270 struct batadv_icmp_header {
271         u8 packet_type;
272         u8 version;
273         u8 ttl;
274         u8 msg_type; /* see ICMP message types above */
275         u8 dst[ETH_ALEN];
276         u8 orig[ETH_ALEN];
277         u8 uid;
278         u8 align[3];
279 };
280
281 /**
282  * struct batadv_icmp_packet - ICMP packet
283  * @packet_type: batman-adv packet type, part of the general header
284  * @version: batman-adv protocol version, part of the genereal header
285  * @ttl: time to live for this packet, part of the genereal header
286  * @msg_type: ICMP packet type
287  * @dst: address of the destination node
288  * @orig: address of the source node
289  * @uid: local ICMP socket identifier
290  * @reserved: not used - useful for alignment
291  * @seqno: ICMP sequence number
292  */
293 struct batadv_icmp_packet {
294         u8     packet_type;
295         u8     version;
296         u8     ttl;
297         u8     msg_type; /* see ICMP message types above */
298         u8     dst[ETH_ALEN];
299         u8     orig[ETH_ALEN];
300         u8     uid;
301         u8     reserved;
302         __be16 seqno;
303 };
304
305 /**
306  * struct batadv_icmp_tp_packet - ICMP TP Meter packet
307  * @packet_type: batman-adv packet type, part of the general header
308  * @version: batman-adv protocol version, part of the genereal header
309  * @ttl: time to live for this packet, part of the genereal header
310  * @msg_type: ICMP packet type
311  * @dst: address of the destination node
312  * @orig: address of the source node
313  * @uid: local ICMP socket identifier
314  * @subtype: TP packet subtype (see batadv_icmp_tp_subtype)
315  * @session: TP session identifier
316  * @seqno: the TP sequence number
317  * @timestamp: time when the packet has been sent. This value is filled in a
318  *  TP_MSG and echoed back in the next TP_ACK so that the sender can compute the
319  *  RTT. Since it is read only by the host which wrote it, there is no need to
320  *  store it using network order
321  */
322 struct batadv_icmp_tp_packet {
323         u8  packet_type;
324         u8  version;
325         u8  ttl;
326         u8  msg_type; /* see ICMP message types above */
327         u8  dst[ETH_ALEN];
328         u8  orig[ETH_ALEN];
329         u8  uid;
330         u8  subtype;
331         u8  session[2];
332         __be32 seqno;
333         __be32 timestamp;
334 };
335
336 /**
337  * enum batadv_icmp_tp_subtype - ICMP TP Meter packet subtypes
338  * @BATADV_TP_MSG: Msg from sender to receiver
339  * @BATADV_TP_ACK: acknowledgment from receiver to sender
340  */
341 enum batadv_icmp_tp_subtype {
342         BATADV_TP_MSG   = 0,
343         BATADV_TP_ACK,
344 };
345
346 #define BATADV_RR_LEN 16
347
348 /**
349  * struct batadv_icmp_packet_rr - ICMP RouteRecord packet
350  * @packet_type: batman-adv packet type, part of the general header
351  * @version: batman-adv protocol version, part of the genereal header
352  * @ttl: time to live for this packet, part of the genereal header
353  * @msg_type: ICMP packet type
354  * @dst: address of the destination node
355  * @orig: address of the source node
356  * @uid: local ICMP socket identifier
357  * @rr_cur: number of entries the rr array
358  * @seqno: ICMP sequence number
359  * @rr: route record array
360  */
361 struct batadv_icmp_packet_rr {
362         u8     packet_type;
363         u8     version;
364         u8     ttl;
365         u8     msg_type; /* see ICMP message types above */
366         u8     dst[ETH_ALEN];
367         u8     orig[ETH_ALEN];
368         u8     uid;
369         u8     rr_cur;
370         __be16 seqno;
371         u8     rr[BATADV_RR_LEN][ETH_ALEN];
372 };
373
374 #define BATADV_ICMP_MAX_PACKET_SIZE     sizeof(struct batadv_icmp_packet_rr)
375
376 /* All packet headers in front of an ethernet header have to be completely
377  * divisible by 2 but not by 4 to make the payload after the ethernet
378  * header again 4 bytes boundary aligned.
379  *
380  * A packing of 2 is necessary to avoid extra padding at the end of the struct
381  * caused by a structure member which is larger than two bytes. Otherwise
382  * the structure would not fulfill the previously mentioned rule to avoid the
383  * misalignment of the payload after the ethernet header. It may also lead to
384  * leakage of information when the padding it not initialized before sending.
385  */
386 #pragma pack(2)
387
388 /**
389  * struct batadv_unicast_packet - unicast packet for network payload
390  * @packet_type: batman-adv packet type, part of the general header
391  * @version: batman-adv protocol version, part of the genereal header
392  * @ttl: time to live for this packet, part of the genereal header
393  * @ttvn: translation table version number
394  * @dest: originator destination of the unicast packet
395  */
396 struct batadv_unicast_packet {
397         u8 packet_type;
398         u8 version;
399         u8 ttl;
400         u8 ttvn; /* destination translation table version number */
401         u8 dest[ETH_ALEN];
402         /* "4 bytes boundary + 2 bytes" long to make the payload after the
403          * following ethernet header again 4 bytes boundary aligned
404          */
405 };
406
407 /**
408  * struct batadv_unicast_4addr_packet - extended unicast packet
409  * @u: common unicast packet header
410  * @src: address of the source
411  * @subtype: packet subtype
412  * @reserved: reserved byte for alignment
413  */
414 struct batadv_unicast_4addr_packet {
415         struct batadv_unicast_packet u;
416         u8 src[ETH_ALEN];
417         u8 subtype;
418         u8 reserved;
419         /* "4 bytes boundary + 2 bytes" long to make the payload after the
420          * following ethernet header again 4 bytes boundary aligned
421          */
422 };
423
424 /**
425  * struct batadv_frag_packet - fragmented packet
426  * @packet_type: batman-adv packet type, part of the general header
427  * @version: batman-adv protocol version, part of the genereal header
428  * @ttl: time to live for this packet, part of the genereal header
429  * @dest: final destination used when routing fragments
430  * @orig: originator of the fragment used when merging the packet
431  * @no: fragment number within this sequence
432  * @priority: priority of frame, from ToS IP precedence or 802.1p
433  * @reserved: reserved byte for alignment
434  * @seqno: sequence identification
435  * @total_size: size of the merged packet
436  */
437 struct batadv_frag_packet {
438         u8     packet_type;
439         u8     version;  /* batman version field */
440         u8     ttl;
441 #if defined(__BIG_ENDIAN_BITFIELD)
442         u8     no:4;
443         u8     priority:3;
444         u8     reserved:1;
445 #elif defined(__LITTLE_ENDIAN_BITFIELD)
446         u8     reserved:1;
447         u8     priority:3;
448         u8     no:4;
449 #else
450 #error "unknown bitfield endianness"
451 #endif
452         u8     dest[ETH_ALEN];
453         u8     orig[ETH_ALEN];
454         __be16 seqno;
455         __be16 total_size;
456 };
457
458 /**
459  * struct batadv_bcast_packet - broadcast packet for network payload
460  * @packet_type: batman-adv packet type, part of the general header
461  * @version: batman-adv protocol version, part of the genereal header
462  * @ttl: time to live for this packet, part of the genereal header
463  * @reserved: reserved byte for alignment
464  * @seqno: sequence identification
465  * @orig: originator of the broadcast packet
466  */
467 struct batadv_bcast_packet {
468         u8     packet_type;
469         u8     version;  /* batman version field */
470         u8     ttl;
471         u8     reserved;
472         __be32 seqno;
473         u8     orig[ETH_ALEN];
474         /* "4 bytes boundary + 2 bytes" long to make the payload after the
475          * following ethernet header again 4 bytes boundary aligned
476          */
477 };
478
479 /**
480  * struct batadv_coded_packet - network coded packet
481  * @packet_type: batman-adv packet type, part of the general header
482  * @version: batman-adv protocol version, part of the genereal header
483  * @ttl: time to live for this packet, part of the genereal header
484  * @first_source: original source of first included packet
485  * @first_orig_dest: original destinal of first included packet
486  * @first_crc: checksum of first included packet
487  * @first_ttvn: tt-version number of first included packet
488  * @second_ttl: ttl of second packet
489  * @second_dest: second receiver of this coded packet
490  * @second_source: original source of second included packet
491  * @second_orig_dest: original destination of second included packet
492  * @second_crc: checksum of second included packet
493  * @second_ttvn: tt version number of second included packet
494  * @coded_len: length of network coded part of the payload
495  */
496 struct batadv_coded_packet {
497         u8     packet_type;
498         u8     version;  /* batman version field */
499         u8     ttl;
500         u8     first_ttvn;
501         /* u8  first_dest[ETH_ALEN]; - saved in mac header destination */
502         u8     first_source[ETH_ALEN];
503         u8     first_orig_dest[ETH_ALEN];
504         __be32 first_crc;
505         u8     second_ttl;
506         u8     second_ttvn;
507         u8     second_dest[ETH_ALEN];
508         u8     second_source[ETH_ALEN];
509         u8     second_orig_dest[ETH_ALEN];
510         __be32 second_crc;
511         __be16 coded_len;
512 };
513
514 #pragma pack()
515
516 /**
517  * struct batadv_unicast_tvlv_packet - generic unicast packet with tvlv payload
518  * @packet_type: batman-adv packet type, part of the general header
519  * @version: batman-adv protocol version, part of the genereal header
520  * @ttl: time to live for this packet, part of the genereal header
521  * @reserved: reserved field (for packet alignment)
522  * @src: address of the source
523  * @dst: address of the destination
524  * @tvlv_len: length of tvlv data following the unicast tvlv header
525  * @align: 2 bytes to align the header to a 4 byte boundary
526  */
527 struct batadv_unicast_tvlv_packet {
528         u8     packet_type;
529         u8     version;  /* batman version field */
530         u8     ttl;
531         u8     reserved;
532         u8     dst[ETH_ALEN];
533         u8     src[ETH_ALEN];
534         __be16 tvlv_len;
535         u16    align;
536 };
537
538 /**
539  * struct batadv_tvlv_hdr - base tvlv header struct
540  * @type: tvlv container type (see batadv_tvlv_type)
541  * @version: tvlv container version
542  * @len: tvlv container length
543  */
544 struct batadv_tvlv_hdr {
545         u8     type;
546         u8     version;
547         __be16 len;
548 };
549
550 /**
551  * struct batadv_tvlv_gateway_data - gateway data propagated through gw tvlv
552  *  container
553  * @bandwidth_down: advertised uplink download bandwidth
554  * @bandwidth_up: advertised uplink upload bandwidth
555  */
556 struct batadv_tvlv_gateway_data {
557         __be32 bandwidth_down;
558         __be32 bandwidth_up;
559 };
560
561 /**
562  * struct batadv_tvlv_tt_data - tt data propagated through the tt tvlv container
563  * @flags: translation table flags (see batadv_tt_data_flags)
564  * @ttvn: translation table version number
565  * @num_vlan: number of announced VLANs. In the TVLV this struct is followed by
566  *  one batadv_tvlv_tt_vlan_data object per announced vlan
567  */
568 struct batadv_tvlv_tt_data {
569         u8     flags;
570         u8     ttvn;
571         __be16 num_vlan;
572 };
573
574 /**
575  * struct batadv_tvlv_tt_vlan_data - vlan specific tt data propagated through
576  *  the tt tvlv container
577  * @crc: crc32 checksum of the entries belonging to this vlan
578  * @vid: vlan identifier
579  * @reserved: unused, useful for alignment purposes
580  */
581 struct batadv_tvlv_tt_vlan_data {
582         __be32 crc;
583         __be16 vid;
584         u16    reserved;
585 };
586
587 /**
588  * struct batadv_tvlv_tt_change - translation table diff data
589  * @flags: status indicators concerning the non-mesh client (see
590  *  batadv_tt_client_flags)
591  * @reserved: reserved field - useful for alignment purposes only
592  * @addr: mac address of non-mesh client that triggered this tt change
593  * @vid: VLAN identifier
594  */
595 struct batadv_tvlv_tt_change {
596         u8     flags;
597         u8     reserved[3];
598         u8     addr[ETH_ALEN];
599         __be16 vid;
600 };
601
602 /**
603  * struct batadv_tvlv_roam_adv - roaming advertisement
604  * @client: mac address of roaming client
605  * @vid: VLAN identifier
606  */
607 struct batadv_tvlv_roam_adv {
608         u8     client[ETH_ALEN];
609         __be16 vid;
610 };
611
612 /**
613  * struct batadv_tvlv_mcast_data - payload of a multicast tvlv
614  * @flags: multicast flags announced by the orig node
615  * @reserved: reserved field
616  */
617 struct batadv_tvlv_mcast_data {
618         u8 flags;
619         u8 reserved[3];
620 };
621
622 #endif /* _NET_BATMAN_ADV_PACKET_H_ */