1 /*
2  * The olsr.org Optimized Link-State Routing daemon (olsrd)
3  *
4  * (c) by the OLSR project
5  *
6  * See our Git repository to find out who worked on this file
7  * and thus is a copyright holder on it.
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * * Redistributions of source code must retain the above copyright
16  *   notice, this list of conditions and the following disclaimer.
17  * * Redistributions in binary form must reproduce the above copyright
18  *   notice, this list of conditions and the following disclaimer in
19  *   the documentation and/or other materials provided with the
20  *   distribution.
21  * * Neither the name of olsr.org, olsrd nor the names of its
22  *   contributors may be used to endorse or promote products derived
23  *   from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  *
38  * Visit http://www.olsr.org for more information.
39  *
40  * If you find this software useful feel free to make a donation
41  * to the project. For more information see the website or contact
42  * the copyright holders.
43  *
44  */
45 
46 /*
47  *Values and packet formats as proposed in RFC3626 and misc. values and
48  *data structures used by the olsr.org OLSR daemon.
49  */
50 
51 #ifndef _PROTOCOLS_OLSR_H
52 #define	_PROTOCOLS_OLSR_H
53 
54 struct olsr;
55 
56 #include "olsr_types.h"
57 #include "olsr_cfg.h"
58 #include "compiler.h"
59 
60 #include <string.h>
61 
62 #define OLSR_HEADERSIZE (sizeof(uint16_t) + sizeof(uint16_t))
63 
64 #define OLSR_MSGHDRSZ_IPV4 12
65 #define OLSR_MSGHDRSZ_IPV6 24
66 
67 /*
68  *Emission Intervals
69  */
70 
71 #define HELLO_INTERVAL        2
72 #define REFRESH_INTERVAL      2
73 #define TC_INTERVAL           5
74 #define MID_INTERVAL          TC_INTERVAL
75 #define HNA_INTERVAL          TC_INTERVAL
76 
77 /* Emission Jitter */
78 #define HELLO_JITTER         25 /* percent */
79 #define HNA_JITTER           25 /* percent */
80 #define MID_JITTER           25 /* percent */
81 #define TC_JITTER            25 /* percent */
82 
83 /*
84  * Default Holding Time (for large scale community networks)
85  */
86 
87 #define NEIGHB_HOLD_TIME      10 * REFRESH_INTERVAL
88 #define TOP_HOLD_TIME         60 * TC_INTERVAL
89 #define DUP_HOLD_TIME         30
90 #define MID_HOLD_TIME         60 * MID_INTERVAL
91 #define HNA_HOLD_TIME         60 * HNA_INTERVAL
92 
93 /*
94  *Message Types
95  */
96 
97 #define HELLO_MESSAGE         1
98 #define TC_MESSAGE            2
99 #define MID_MESSAGE           3
100 #define HNA_MESSAGE           4
101 #define MAX_MESSAGE           4
102 
103 /*
104  *Link Types
105  */
106 
107 #define UNSPEC_LINK           0
108 #define ASYM_LINK             1
109 #define SYM_LINK              2
110 #define LOST_LINK             3
111 #define HIDE_LINK             4
112 #define MAX_LINK              4
113 
114 #define HELLO_LINK_ORDER_ARRAY { UNSPEC_LINK, LOST_LINK, ASYM_LINK, SYM_LINK }
115 
linkTypeToString(int type)116 static INLINE const char * linkTypeToString(int type) {
117   switch (type) {
118     case ASYM_LINK:
119       return "ASYMMETRIC";
120 
121     case SYM_LINK:
122       return "SYMMETRIC";
123 
124     case LOST_LINK:
125       return "LOST";
126 
127     case HIDE_LINK:
128       return "HIDE";
129 
130     case UNSPEC_LINK:
131     default:
132       return "UNSPECIFIED";
133   }
134 }
135 
136 /*
137  *Neighbor Types
138  */
139 
140 #define NOT_NEIGH             0
141 #define SYM_NEIGH             1
142 #define MPR_NEIGH             2
143 #define MAX_NEIGH             2
144 
145 /*
146  *Neighbor status
147  */
148 
149 #define NOT_SYM               0
150 #define SYM                   1
151 
152 /*
153  *Link Hysteresis
154  */
155 
156 #define HYST_THRESHOLD_HIGH   0.8
157 #define HYST_THRESHOLD_LOW    0.3
158 #define HYST_SCALING          0.5
159 
160 /*
161  *Willingness
162  */
163 
164 #define WILL_NEVER            0
165 #define WILL_LOW              1
166 #define WILL_DEFAULT          3
167 #define WILL_HIGH             6
168 #define WILL_ALWAYS           7
169 
170 /*
171  *Redundancy defaults
172  */
173 #define TC_REDUNDANCY         2
174 #define MPR_COVERAGE          7
175 
176 /*
177  *Misc. Constants
178  */
179 #define MAXJITTER             HELLO_INTERVAL / 4
180 #define MAX_TTL               0xff
181 
182 /*
183  *Sequence numbering
184  */
185 
186 /* Seqnos are 16 bit values */
187 
188 #define MAXVALUE 0xFFFF
189 
190 /* Macro for checking seqnos "wraparound" */
191 #define SEQNO_GREATER_THAN(s1, s2)                \
192         (((s1 > s2) && (s1 - s2 <= (MAXVALUE/2))) \
193      || ((s2 > s1) && (s2 - s1 > (MAXVALUE/2))))
194 
195 /*
196  * Macros for creating and extracting the neighbor
197  * and link type information from 8bit link_code
198  * data as passed in HELLO messages
199  */
200 
201 #define CREATE_LINK_CODE(status, link) (link | (status<<2))
202 
203 #define EXTRACT_STATUS(link_code) ((link_code & 0xC)>>2)
204 
205 #define EXTRACT_LINK(link_code) (link_code & 0x3)
206 
207 /***********************************************
208  *           OLSR packet definitions           *
209  ***********************************************/
210 
211 /*
212  *The HELLO message
213  */
214 
215 /*
216  *Hello info
217  */
218 struct hellinfo {
219   uint8_t link_code;
220   uint8_t reserved;
221   uint16_t size;
222   uint32_t neigh_addr[1];              /* neighbor IP address(es) */
223 } __attribute__ ((packed));
224 
225 struct hellomsg {
226   uint16_t reserved;
227   uint8_t htime;
228   uint8_t willingness;
229   struct hellinfo hell_info[1];
230 } __attribute__ ((packed));
231 
232 /*
233  *IPv6
234  */
235 
236 struct hellinfo6 {
237   uint8_t link_code;
238   uint8_t reserved;
239   uint16_t size;
240   struct in6_addr neigh_addr[1];       /* neighbor IP address(es) */
241 } __attribute__ ((packed));
242 
243 struct hellomsg6 {
244   uint16_t reserved;
245   uint8_t htime;
246   uint8_t willingness;
247   struct hellinfo6 hell_info[1];
248 } __attribute__ ((packed));
249 
250 /*
251  * Topology Control packet
252  */
253 
254 struct neigh_info {
255   uint32_t addr;
256 } __attribute__ ((packed));
257 
258 struct olsr_tcmsg {
259   uint16_t ansn;
260   uint16_t reserved;
261   struct neigh_info neigh[1];
262 } __attribute__ ((packed));
263 
264 /*
265  *IPv6
266  */
267 
268 struct neigh_info6 {
269   struct in6_addr addr;
270 } __attribute__ ((packed));
271 
272 struct olsr_tcmsg6 {
273   uint16_t ansn;
274   uint16_t reserved;
275   struct neigh_info6 neigh[1];
276 } __attribute__ ((packed));
277 
278 /*
279  *Multiple Interface Declaration message
280  */
281 
282 /*
283  * Defined as s struct for further expansion
284  * For example: do we want to tell what type of interface
285  * is associated whit each address?
286  */
287 struct midaddr {
288   uint32_t addr;
289 } __attribute__ ((packed));
290 
291 struct midmsg {
292   struct midaddr mid_addr[1];
293 } __attribute__ ((packed));
294 
295 /*
296  *IPv6
297  */
298 struct midaddr6 {
299   struct in6_addr addr;
300 } __attribute__ ((packed));
301 
302 struct midmsg6 {
303   struct midaddr6 mid_addr[1];
304 } __attribute__ ((packed));
305 
306 /*
307  * Host and Network Association message
308  */
309 struct hnapair {
310   uint32_t addr;
311   uint32_t netmask;
312 } __attribute__ ((packed));
313 
314 struct hnamsg {
315   struct hnapair hna_net[1];
316 } __attribute__ ((packed));
317 
318 /*
319  *IPv6
320  */
321 
322 struct hnapair6 {
323   struct in6_addr addr;
324   struct in6_addr netmask;
325 } __attribute__ ((packed));
326 
327 struct hnamsg6 {
328   struct hnapair6 hna_net[1];
329 } __attribute__ ((packed));
330 
331 /*
332  * OLSR message (several can exist in one OLSR packet)
333  */
334 
335 struct olsrmsg {
336   uint8_t olsr_msgtype;
337   uint8_t olsr_vtime;
338   uint16_t olsr_msgsize;
339   uint32_t originator;
340   uint8_t ttl;
341   uint8_t hopcnt;
342   uint16_t seqno;
343 
344   union {
345     struct hellomsg hello;
346     struct olsr_tcmsg tc;
347     struct hnamsg hna;
348     struct midmsg mid;
349   } message;
350 
351 } __attribute__ ((packed));
352 
353 /*
354  *IPv6
355  */
356 
357 struct olsrmsg6 {
358   uint8_t olsr_msgtype;
359   uint8_t olsr_vtime;
360   uint16_t olsr_msgsize;
361   struct in6_addr originator;
362   uint8_t ttl;
363   uint8_t hopcnt;
364   uint16_t seqno;
365 
366   union {
367     struct hellomsg6 hello;
368     struct olsr_tcmsg6 tc;
369     struct hnamsg6 hna;
370     struct midmsg6 mid;
371   } message;
372 
373 } __attribute__ ((packed));
374 
375 /*
376  * Generic OLSR packet
377  */
378 
379 struct olsr {
380   uint16_t olsr_packlen;               /* packet length */
381   uint16_t olsr_seqno;
382   struct olsrmsg olsr_msg[1];          /* variable messages */
383 } __attribute__ ((packed));
384 
385 struct olsr6 {
386   uint16_t olsr_packlen;               /* packet length */
387   uint16_t olsr_seqno;
388   struct olsrmsg6 olsr_msg[1];         /* variable messages */
389 } __attribute__ ((packed));
390 
391 /* IPv4 <-> IPv6 compability */
392 
393 union olsr_message {
394   struct olsrmsg v4;
395   struct olsrmsg6 v6;
396 } __attribute__ ((packed));
397 
398 union olsr_packet {
399   struct olsr v4;
400   struct olsr6 v6;
401 } __attribute__ ((packed));
402 
403 #endif /* _PROTOCOLS_OLSR_H */
404 
405 /*
406  * Local Variables:
407  * c-basic-offset: 2
408  * indent-tabs-mode: nil
409  * End:
410  */
411