Statistics
| Branch: | Revision:

iof-bird-daemon / proto / ospf / ospf.h @ 29239ba2

History | View | Annotate | Download (27.5 KB)

1
/*
2
 *        BIRD -- OSPF
3
 *
4
 *        (c) 1999--2005 Ondrej Filip <feela@network.cz>
5
 *        (c) 2009--2014 Ondrej Zajicek <santiago@crfreenet.org>
6
 *        (c) 2009--2014 CZ.NIC z.s.p.o.
7
 *
8
 *        Can be freely distributed and used under the terms of the GNU GPL.
9
 */
10

    
11
#ifndef _BIRD_OSPF_H_
12
#define _BIRD_OSPF_H_
13

    
14
#include "nest/bird.h"
15

    
16
#include "lib/checksum.h"
17
#include "lib/ip.h"
18
#include "lib/lists.h"
19
#include "lib/slists.h"
20
#include "lib/socket.h"
21
#include "lib/timer.h"
22
#include "lib/resource.h"
23
#include "nest/protocol.h"
24
#include "nest/iface.h"
25
#include "nest/route.h"
26
#include "nest/cli.h"
27
#include "nest/locks.h"
28
#include "nest/bfd.h"
29
#include "conf/conf.h"
30
#include "lib/string.h"
31

    
32

    
33
#ifdef LOCAL_DEBUG
34
#define OSPF_FORCE_DEBUG 1
35
#else
36
#define OSPF_FORCE_DEBUG 0
37
#endif
38

    
39

    
40
#ifdef IPV6
41
#define OSPF_IS_V2 0
42
#else
43
#define OSPF_IS_V2 1
44
#endif
45

    
46
// FIXME: MAX_PREFIX_LENGTH
47

    
48
#define OSPF_TRACE(flags, msg, args...) \
49
  do { if ((p->p.debug & flags) || OSPF_FORCE_DEBUG) \
50
    log(L_TRACE "%s: " msg, p->p.name , ## args ); } while(0)
51

    
52
#define OSPF_PACKET(dumpfn, buffer, msg, args...) \
53
  do { if ((p->p.debug & D_PACKETS) || OSPF_FORCE_DEBUG)                \
54
    { log(L_TRACE "%s: " msg, p->p.name, ## args ); dumpfn(p, buffer); } } while(0)
55

    
56
#define LOG_PKT(msg, args...) \
57
  log_rl(&p->log_pkt_tbf, L_REMOTE "%s: " msg, p->p.name, args)
58

    
59
#define LOG_PKT_AUTH(msg, args...) \
60
  log_rl(&p->log_pkt_tbf, L_AUTH "%s: " msg, p->p.name, args)
61

    
62
#define LOG_PKT_WARN(msg, args...) \
63
  log_rl(&p->log_pkt_tbf, L_WARN "%s: " msg, p->p.name, args)
64

    
65
#define LOG_LSA1(msg, args...) \
66
  log_rl(&p->log_lsa_tbf, L_REMOTE "%s: " msg, p->p.name, args)
67

    
68
#define LOG_LSA2(msg, args...) \
69
  do { if (! p->log_lsa_tbf.mark) \
70
    log(L_REMOTE "%s: " msg, p->p.name, args); } while(0)
71

    
72

    
73
#define OSPF_PROTO 89
74

    
75
#define LSREFRESHTIME 1800        /* 30 minutes */
76
#define MINLSINTERVAL 5
77
#define MINLSARRIVAL 1
78
#define LSINFINITY 0xffffff
79

    
80
#define OSPF_DEFAULT_TICK 1
81
#define OSPF_DEFAULT_STUB_COST 1000
82
#define OSPF_DEFAULT_ECMP_LIMIT 16
83
#define OSPF_DEFAULT_TRANSINT 40
84

    
85
#define OSPF_MIN_PKT_SIZE 256
86
#define OSPF_MAX_PKT_SIZE 65535
87

    
88
#define OSPF_VLINK_ID_OFFSET 0x80000000
89

    
90

    
91
struct ospf_config
92
{
93
  struct proto_config c;
94
  uint tick;
95
  u8 ospf2;
96
  u8 rfc1583;
97
  u8 stub_router;
98
  u8 merge_external;
99
  u8 instance_id;
100
  u8 abr;
101
  u8 asbr;
102
  int ecmp;
103
  list area_list;                /* list of area configs (struct ospf_area_config) */
104
  list vlink_list;                /* list of configured vlinks (struct ospf_iface_patt) */
105
};
106

    
107
struct ospf_area_config
108
{
109
  node n;
110
  u32 areaid;
111
  u32 default_cost;                /* Cost of default route for stub areas
112
                                   (With possible LSA_EXT3_EBIT for NSSA areas) */
113
  u8 type;                        /* Area type (standard, stub, NSSA), represented
114
                                   by option flags (OPT_E, OPT_N) */
115
  u8 summary;                        /* Import summaries to this stub/NSSA area, valid for ABR */
116
  u8 default_nssa;                /* Generate default NSSA route for NSSA+summary area */
117
  u8 translator;                /* Translator role, for NSSA ABR */
118
  u32 transint;                        /* Translator stability interval */
119
  list patt_list;                /* List of iface configs (struct ospf_iface_patt) */
120
  list net_list;                /* List of aggregate networks for that area */
121
  list enet_list;                /* List of aggregate external (NSSA) networks */
122
  list stubnet_list;                /* List of stub networks added to Router LSA */
123
};
124

    
125
struct area_net_config
126
{
127
  node n;
128
  struct prefix px;
129
  u32 tag;
130
  u8 hidden;
131
};
132

    
133
struct area_net
134
{
135
  struct fib_node fn;
136
  u32 metric;                        /* With possible LSA_EXT3_EBIT for NSSA area nets */
137
  u32 tag;
138
  u8 hidden;
139
  u8 active;
140
};
141

    
142
struct ospf_stubnet_config
143
{
144
  node n;
145
  struct prefix px;
146
  u32 cost;
147
  u8 hidden;
148
  u8 summary;
149
};
150

    
151
struct nbma_node
152
{
153
  node n;
154
  ip_addr ip;
155
  byte eligible;
156
  byte found;
157
};
158

    
159
struct ospf_iface_patt
160
{
161
  struct iface_patt i;
162
  u32 type;
163
  u32 stub;
164
  u32 cost;
165
  u32 helloint;
166
  u32 rxmtint;
167
  u32 pollint;
168
  u32 waitint;
169
  u32 deadc;
170
  u32 deadint;
171
  u32 inftransdelay;
172
  list nbma_list;
173
  u32 priority;
174
  u32 voa;
175
  u32 vid;
176
  int tx_tos;
177
  int tx_priority;
178
  u16 tx_length;
179
  u16 rx_buffer;
180

    
181
#define OSPF_RXBUF_MINSIZE 256        /* Minimal allowed size */
182
  u8 instance_id;
183
  u8 autype;                        /* OSPF_AUTH_*, not really used in OSPFv3 */
184
  u8 strictnbma;
185
  u8 check_link;
186
  u8 ecmp_weight;
187
  u8 link_lsa_suppression;
188
  u8 real_bcast;                /* Not really used in OSPFv3 */
189
  u8 ptp_netmask;                /* bool + 2 for unspecified */
190
  u8 ttl_security;                /* bool + 2 for TX only */
191
  u8 bfd;
192
  u8 bsd_secondary;
193
  list *passwords;
194
};
195

    
196
/* Default values for interface parameters */
197
#define COST_D 10
198
#define RXMTINT_D 5
199
#define INFTRANSDELAY_D 1
200
#define PRIORITY_D 1
201
#define HELLOINT_D 10
202
#define POLLINT_D 20
203
#define DEADC_D 4
204
#define WAIT_DMH 4
205
  /* Value of Wait timer - not found it in RFC * - using 4*HELLO */
206

    
207

    
208

    
209
struct ospf_proto
210
{
211
  struct proto p;
212
  timer *disp_timer;                /* OSPF proto dispatcher */
213
  uint tick;
214
  struct top_graph *gr;                /* LSA graph */
215
  slist lsal;                        /* List of all LSA's */
216
  int calcrt;                        /* Routing table calculation scheduled?
217
                                   0=no, 1=normal, 2=forced reload */
218
  list iface_list;                /* List of OSPF interfaces (struct ospf_iface) */
219
  list area_list;                /* List of OSPF areas (struct ospf_area) */
220
  int areano;                        /* Number of area I belong to */
221
  int padj;                        /* Number of neighbors in Exchange or Loading state */
222
  struct fib rtf;                /* Routing table */
223
  byte ospf2;                        /* OSPF v2 or v3 */
224
  byte rfc1583;                        /* RFC1583 compatibility */
225
  byte stub_router;                /* Do not forward transit traffic */
226
  byte merge_external;                /* Should i merge external routes? */
227
  byte asbr;                        /* May i originate any ext/NSSA lsa? */
228
  byte ecmp;                        /* Maximal number of nexthops in ECMP route, or 0 */
229
  struct ospf_area *backbone;        /* If exists */
230
  event *flood_event;                /* Event for flooding LS updates */
231
  void *lsab;                        /* LSA buffer used when originating router LSAs */
232
  int lsab_size, lsab_used;
233
  linpool *nhpool;                /* Linpool used for next hops computed in SPF */
234
  sock *vlink_sk;                /* IP socket used for vlink TX */
235
  u32 router_id;
236
  u32 last_vlink_id;                /* Interface IDs for vlinks (starts at 0x80000000) */
237
  struct tbf log_pkt_tbf;        /* TBF for packet messages */
238
  struct tbf log_lsa_tbf;        /* TBF for LSA messages */
239
};
240

    
241
struct ospf_area
242
{
243
  node n;
244
  u32 areaid;
245
  struct ospf_area_config *ac;        /* Related area config */
246
  struct top_hash_entry *rt;        /* My own router LSA */
247
  struct top_hash_entry *pxr_lsa; /* Originated prefix LSA */
248
  list cand;                        /* List of candidates for RT calc. */
249
  struct fib net_fib;                /* Networks to advertise or not */
250
  struct fib enet_fib;                /* External networks for NSSAs */
251
  u32 options;                        /* Optional features */
252
  u8 update_rt_lsa;                /* Rt lsa origination scheduled? */
253
  u8 trcap;                        /* Transit capability? */
254
  u8 marked;                        /* Used in OSPF reconfigure */
255
  u8 translate;                        /* Translator state (TRANS_*), for NSSA ABR  */
256
  timer *translator_timer;        /* For NSSA translator switch */
257
  struct ospf_proto *po;
258
  struct fib rtr;                /* Routing tables for routers */
259
};
260

    
261

    
262

    
263
struct ospf_iface
264
{
265
  node n;
266
  struct iface *iface;                /* Nest's iface (NULL for vlinks) */
267
  struct ifa *addr;                /* IP prefix associated with that OSPF iface */
268
  struct ospf_area *oa;
269
  struct ospf_iface_patt *cf;
270
  char *ifname;                        /* Interface name (iface->name), new one for vlinks */
271

    
272
  pool *pool;
273
  sock *sk;                        /* IP socket */
274
  list neigh_list;                /* List of neighbors (struct ospf_neighbor) */
275
  u32 cost;                        /* Cost of iface */
276
  u32 waitint;                        /* number of sec before changing state from wait */
277
  u32 rxmtint;                        /* number of seconds between LSA retransmissions */
278
  u32 pollint;                        /* Poll interval */
279
  u32 deadint;                        /* after "deadint" missing hellos is router dead */
280
  u32 iface_id;                        /* Interface ID (iface->index or new value for vlinks) */
281
  u32 vid;                        /* ID of peer of virtual link */
282
  ip_addr vip;                        /* IP of peer of virtual link */
283
  struct ospf_iface *vifa;        /* OSPF iface which the vlink goes through */
284
  struct ospf_area *voa;        /* OSPF area which the vlink goes through */
285
  u16 inftransdelay;                /* The estimated number of seconds it takes to
286
                                   transmit a Link State Update Packet over this
287
                                   interface.  LSAs contained in the update */
288
  u16 helloint;                        /* number of seconds between hello sending */
289
  list *passwords;
290
  u32 csn;                      /* Last used crypt seq number */
291
  bird_clock_t csn_use;         /* Last time when packet with that CSN was sent */
292
  ip_addr all_routers;                /* Multicast (or broadcast) address for all routers */
293
  ip_addr des_routers;                /* Multicast (or NULL) address for designated routers */
294
  ip_addr drip;                        /* Designated router IP */
295
  ip_addr bdrip;                /* Backup DR IP */
296
  u32 drid;                        /* DR Router ID */
297
  u32 bdrid;                        /* BDR Router ID */
298
  s16 rt_pos_beg;                /* Position of iface in Router-LSA, begin, inclusive */
299
  s16 rt_pos_end;                /* Position of iface in Router-LSA, end, exclusive */
300
  s16 px_pos_beg;                /* Position of iface in Rt Prefix-LSA, begin, inclusive */
301
  s16 px_pos_end;                /* Position of iface in Rt Prefix-LSA, end, exclusive */
302
  u32 dr_iface_id;                /* if drid is valid, this is iface_id of DR (for connecting network) */
303
  u8 instance_id;                /* Used to differentiate between more OSPF
304
                                   instances on one interface */
305
  u8 autype;                        /* Authentication type (OSPF_AUTH_*) */
306
  u8 type;                        /* OSPF view of type (OSPF_IT_*) */
307
  u8 strictnbma;                /* Can I talk with unknown neighbors? */
308
  u8 stub;                        /* Inactive interface */
309
  u8 state;                        /* Interface state machine (OSPF_IS_*) */
310
  timer *wait_timer;                /* WAIT timer */
311
  timer *hello_timer;                /* HELLOINT timer */
312
  timer *poll_timer;                /* Poll Interval - for NBMA */
313

    
314
  struct top_hash_entry *link_lsa;        /* Originated link LSA */
315
  struct top_hash_entry *net_lsa;        /* Originated network LSA */
316
  struct top_hash_entry *pxn_lsa;        /* Originated prefix LSA */
317
  struct top_hash_entry **flood_queue;        /* LSAs queued for LSUPD */
318
  u8 update_link_lsa;
319
  u8 update_net_lsa;
320
  u16 flood_queue_used;                /* The current number of LSAs in flood_queue */
321
  u16 flood_queue_size;                /* The maximum number of LSAs in flood_queue */
322
  int fadj;                        /* Number of fully adjacent neighbors */
323
  list nbma_list;
324
  u8 priority;                        /* A router priority for DR election */
325
  u8 ioprob;
326
#define OSPF_I_OK 0                /* Everything OK */
327
#define OSPF_I_SK 1                /* Socket open failed */
328
#define OSPF_I_LL 2                /* Missing link-local address (OSPFv3) */
329
  u8 sk_dr;                        /* Socket is a member of designated routers group */
330
  u8 marked;                        /* Used in OSPF reconfigure, 2 for force restart */
331
  u16 rxbuf;                        /* Buffer size */
332
  u16 tx_length;                /* Soft TX packet length limit, usually MTU */
333
  u16 tx_hdrlen;                /* Expected packet header length, less than tx_length */
334
  u8 check_link;                /* Whether iface link change is used */
335
  u8 ecmp_weight;                /* Weight used for ECMP */
336
  u8 link_lsa_suppression;        /* Suppression of Link-LSA origination */
337
  u8 ptp_netmask;                /* Send real netmask for P2P */
338
  u8 check_ttl;                        /* Check incoming packets for TTL 255 */
339
  u8 bfd;                        /* Use BFD on iface */
340
};
341

    
342
struct ospf_neighbor
343
{
344
  node n;
345
  pool *pool;
346
  struct ospf_iface *ifa;
347
  u8 state;
348
  timer *inactim;                /* Inactivity timer */
349
  u8 imms;                        /* I, M, Master/slave received */
350
  u8 myimms;                        /* I, M Master/slave */
351
  u32 dds;                        /* DD Sequence number being sent */
352
  u32 ddr;                        /* last Dat Des packet received */
353

    
354
  u32 rid;                        /* Router ID */
355
  ip_addr ip;                        /* IP of it's interface */
356
  u8 priority;                        /* Priority */
357
  u8 adj;                        /* built adjacency? */
358
  u32 options;                        /* Options received */
359

    
360
  /* Entries dr and bdr store IP addresses in OSPFv2 and router IDs in
361
     OSPFv3, we use the same type to simplify handling */
362
  u32 dr;                        /* Neighbor's idea of DR */
363
  u32 bdr;                        /* Neighbor's idea of BDR */
364
  u32 iface_id;                        /* ID of Neighbour's iface connected to common network */
365

    
366
  /* Database summary list iterator, controls initial dbdes exchange.
367
   * Advances in the LSA list as dbdes packets are sent.
368
   */
369
  siterator dbsi;                /* iterator of po->lsal */
370

    
371
  /* Link state request list, controls initial LSA exchange.
372
   * Entries added when received in dbdes packets, removed as sent in lsreq packets.
373
   */
374
  slist lsrql;                        /* slist of struct top_hash_entry from n->lsrqh */
375
  struct top_graph *lsrqh;
376
  struct top_hash_entry *lsrqi;        /* Pointer to the first unsent node in lsrql */
377

    
378
  /* Link state retransmission list, controls LSA retransmission during flood.
379
   * Entries added as sent in lsupd packets, removed when received in lsack packets.
380
   * These entries hold ret_count in appropriate LSA entries.
381
   */
382
  slist lsrtl;                        /* slist of struct top_hash_entry from n->lsrth */
383
  struct top_graph *lsrth;
384
  timer *dbdes_timer;                /* DBDES exchange timer */
385
  timer *lsrq_timer;                /* LSA request timer */
386
  timer *lsrt_timer;                /* LSA retransmission timer */
387
  list ackl[2];
388
#define ACKL_DIRECT 0
389
#define ACKL_DELAY 1
390
  timer *ackd_timer;                /* Delayed ack timer */
391
  struct bfd_request *bfd_req;        /* BFD request, if BFD is used */
392
  void *ldd_buffer;                /* Last database description packet */
393
  u32 ldd_bsize;                /* Buffer size for ldd_buffer */
394
  u32 csn;                      /* Last received crypt seq number (for MD5) */
395
};
396

    
397

    
398
/* OSPF interface types */
399
#define OSPF_IT_BCAST        0
400
#define OSPF_IT_NBMA        1
401
#define OSPF_IT_PTP        2
402
#define OSPF_IT_PTMP        3
403
#define OSPF_IT_VLINK        4
404
#define OSPF_IT_UNDEF        5
405

    
406
/* OSPF interface states */
407
#define OSPF_IS_DOWN        0        /* Not active */
408
#define OSPF_IS_LOOP        1        /* Iface with no link */
409
#define OSPF_IS_WAITING        2        /* Waiting for Wait timer */
410
#define OSPF_IS_PTP        3        /* PTP operational */
411
#define OSPF_IS_DROTHER        4        /* I'm on BCAST or NBMA and I'm not DR */
412
#define OSPF_IS_BACKUP        5        /* I'm BDR */
413
#define OSPF_IS_DR        6        /* I'm DR */
414

    
415
/* Definitions for interface state machine */
416
#define ISM_UP                0        /* Interface Up */
417
#define ISM_WAITF        1        /* Wait timer fired */
418
#define ISM_BACKS        2        /* Backup seen */
419
#define ISM_NEICH        3        /* Neighbor change */
420
#define ISM_LOOP        4        /* Link down */
421
#define ISM_UNLOOP        5        /* Link up */
422
#define ISM_DOWN        6        /* Interface down */
423

    
424
/* OSPF authentication types */
425
#define OSPF_AUTH_NONE        0
426
#define OSPF_AUTH_SIMPLE 1
427
#define OSPF_AUTH_CRYPT        2
428

    
429

    
430
/* OSPF neighbor states */
431
#define NEIGHBOR_DOWN        0
432
#define NEIGHBOR_ATTEMPT 1
433
#define NEIGHBOR_INIT        2
434
#define NEIGHBOR_2WAY        3
435
#define NEIGHBOR_EXSTART 4
436
#define NEIGHBOR_EXCHANGE 5
437
#define NEIGHBOR_LOADING 6
438
#define NEIGHBOR_FULL        7
439

    
440
/* Definitions for neighbor state machine */
441
#define INM_HELLOREC        0        /* Hello Received */
442
#define INM_START        1        /* Neighbor start - for NBMA */
443
#define INM_2WAYREC        2        /* 2-Way received */
444
#define INM_NEGDONE        3        /* Negotiation done */
445
#define INM_EXDONE        4        /* Exchange done */
446
#define INM_BADLSREQ        5        /* Bad LS Request */
447
#define INM_LOADDONE        6        /* Load done */
448
#define INM_ADJOK        7        /* AdjOK? */
449
#define INM_SEQMIS        8        /* Sequence number mismatch */
450
#define INM_1WAYREC        9        /* 1-Way */
451
#define INM_KILLNBR        10        /* Kill Neighbor */
452
#define INM_INACTTIM        11        /* Inactivity timer */
453
#define INM_LLDOWN        12        /* Line down */
454

    
455
#define TRANS_OFF        0
456
#define TRANS_ON        1
457
#define TRANS_WAIT        2        /* Waiting before the end of translation */
458

    
459

    
460
/* Generic option flags */
461
#define OPT_V6                0x01        /* OSPFv3, LSA relevant for IPv6 routing calculation */
462
#define OPT_E                0x02        /* Related to AS-external LSAs */
463
#define OPT_MC                0x04        /* Related to MOSPF, not used and obsolete */
464
#define OPT_N                0x08        /* Related to NSSA */
465
#define OPT_P                0x08        /* OSPFv2, flags P and N share position, see NSSA RFC */
466
#define OPT_EA                0x10        /* OSPFv2, external attributes, not used and obsolete */
467
#define OPT_R                0x10        /* OSPFv3, originator is active router */
468
#define OPT_DC                0x20        /* Related to demand circuits, not used */
469

    
470
/* Router-LSA VEB flags are are stored together with links (OSPFv2) or options (OSPFv3) */
471
#define OPT_RT_B        (0x01 << 24)
472
#define OPT_RT_E        (0x02 << 24)
473
#define OPT_RT_V        (0x04 << 24)
474
#define OPT_RT_NT        (0x10 << 24)
475

    
476
/* Prefix flags, specific for OSPFv3 */
477
#define OPT_PX_NU        0x01
478
#define OPT_PX_LA        0x02
479
#define OPT_PX_P        0x08
480
#define OPT_PX_DN        0x10
481

    
482

    
483
struct ospf_packet
484
{
485
  u8 version;
486
  u8 type;
487
  u16 length;
488
  u32 routerid;
489
  u32 areaid;
490
  u16 checksum;
491
  u8 instance_id;                /* See RFC 6549 */
492
  u8 autype;                        /* Undefined for OSPFv3 */
493
};
494

    
495
struct ospf_auth_crypto
496
{
497
  u16 zero;
498
  u8 keyid;
499
  u8 len;
500
  u32 csn;
501
};
502

    
503
union ospf_auth
504
{
505
  u8 password[8];
506
  struct ospf_auth_crypto c32;
507
};
508

    
509
/* Packet types */
510
#define HELLO_P                1        /* Hello */
511
#define DBDES_P                2        /* Database description */
512
#define LSREQ_P                3        /* Link state request */
513
#define LSUPD_P                4        /* Link state update */
514
#define LSACK_P                5        /* Link state acknowledgement */
515

    
516

    
517
#define DBDES_I                4        /* Init bit */
518
#define DBDES_M                2        /* More bit */
519
#define DBDES_MS        1        /* Master/Slave bit */
520
#define DBDES_IMMS        (DBDES_I | DBDES_M | DBDES_MS)
521

    
522

    
523
#define LSA_T_RT        0x2001
524
#define LSA_T_NET        0x2002
525
#define LSA_T_SUM_NET        0x2003
526
#define LSA_T_SUM_RT        0x2004
527
#define LSA_T_EXT        0x4005
528
#define LSA_T_NSSA        0x2007
529
#define LSA_T_LINK        0x0008
530
#define LSA_T_PREFIX        0x2009
531

    
532
#define LSA_T_V2_MASK        0x00ff
533

    
534
#define LSA_UBIT        0x8000
535

    
536
#define LSA_SCOPE_LINK        0x0000
537
#define LSA_SCOPE_AREA        0x2000
538
#define LSA_SCOPE_AS        0x4000
539
#define LSA_SCOPE_RES        0x6000
540
#define LSA_SCOPE_MASK        0x6000
541
#define LSA_SCOPE(type)        ((type) & LSA_SCOPE_MASK)
542

    
543

    
544
#define LSA_MAXAGE        3600        /* 1 hour */
545
#define LSA_CHECKAGE        300        /* 5 minutes */
546
#define LSA_MAXAGEDIFF        900        /* 15 minutes */
547

    
548
#define LSA_ZEROSEQNO        ((s32) 0x80000000)
549
#define LSA_INITSEQNO        ((s32) 0x80000001)
550
#define LSA_MAXSEQNO        ((s32) 0x7fffffff)
551

    
552
#define LSA_METRIC_MASK  0x00FFFFFF
553
#define LSA_OPTIONS_MASK 0x00FFFFFF
554

    
555

    
556
#define LSART_PTP        1
557
#define LSART_NET        2
558
#define LSART_STUB        3
559
#define LSART_VLNK        4
560

    
561
#define LSA_RT2_LINKS        0x0000FFFF
562

    
563
#define LSA_SUM2_TOS        0xFF000000
564

    
565
#define LSA_EXT2_TOS        0x7F000000
566
#define LSA_EXT2_EBIT        0x80000000
567

    
568
#define LSA_EXT3_EBIT        0x4000000
569
#define LSA_EXT3_FBIT        0x2000000
570
#define LSA_EXT3_TBIT        0x1000000
571

    
572

    
573
struct ospf_lsa_header
574
{
575
  u16 age;                        /* LS Age */
576
  u16 type_raw;                        /* Type, mixed with options on OSPFv2 */
577

    
578
  u32 id;
579
  u32 rt;                        /* Advertising router */
580
  s32 sn;                        /* LS Sequence number */
581
  u16 checksum;
582
  u16 length;
583
};
584

    
585

    
586
/* In OSPFv2, options are embedded in higher half of type_raw */
587
static inline u8 lsa_get_options(struct ospf_lsa_header *lsa)
588
{ return lsa->type_raw >> 8; }
589

    
590
static inline void lsa_set_options(struct ospf_lsa_header *lsa, u16 options)
591
{ lsa->type_raw = (lsa->type_raw & 0xff) | (options << 8); }
592

    
593

    
594
struct ospf_lsa_rt
595
{
596
  u32 options;        /* VEB flags, mixed with link count for OSPFv2 and options for OSPFv3 */
597
};
598

    
599
struct ospf_lsa_rt2_link
600
{
601
  u32 id;
602
  u32 data;
603
#ifdef CPU_BIG_ENDIAN
604
  u8 type;
605
  u8 no_tos;
606
  u16 metric;
607
#else
608
  u16 metric;
609
  u8 no_tos;
610
  u8 type;
611
#endif
612
};
613

    
614
struct ospf_lsa_rt2_tos
615
{
616
#ifdef CPU_BIG_ENDIAN
617
  u8 tos;
618
  u8 padding;
619
  u16 metric;
620
#else
621
  u16 metric;
622
  u8 padding;
623
  u8 tos;
624
#endif
625
};
626

    
627
struct ospf_lsa_rt3_link
628
{
629
#ifdef CPU_BIG_ENDIAN
630
  u8 type;
631
  u8 padding;
632
  u16 metric;
633
#else
634
  u16 metric;
635
  u8 padding;
636
  u8 type;
637
#endif
638
  u32 lif;        /* Local interface ID */
639
  u32 nif;        /* Neighbor interface ID */
640
  u32 id;        /* Neighbor router ID */
641
};
642

    
643

    
644
struct ospf_lsa_net
645
{
646
  u32 optx;        /* Netmask for OSPFv2, options for OSPFv3 */
647
  u32 routers[];
648
};
649

    
650
struct ospf_lsa_sum2
651
{
652
  u32 netmask;
653
  u32 metric;
654
};
655

    
656
struct ospf_lsa_sum3_net
657
{
658
  u32 metric;
659
  u32 prefix[];
660
};
661

    
662
struct ospf_lsa_sum3_rt
663
{
664
  u32 options;
665
  u32 metric;
666
  u32 drid;
667
};
668

    
669
struct ospf_lsa_ext2
670
{
671
  u32 netmask;
672
  u32 metric;
673
  u32 fwaddr;
674
  u32 tag;
675
};
676

    
677
struct ospf_lsa_ext3
678
{
679
  u32 metric;
680
  u32 rest[];
681
};
682

    
683
struct ospf_lsa_ext_local
684
{
685
  ip_addr ip, fwaddr;
686
  int pxlen;
687
  u32 metric, ebit, fbit, tag, propagate;
688
  u8 pxopts;
689
};
690

    
691
struct ospf_lsa_link
692
{
693
  u32 options;
694
  ip6_addr lladdr;
695
  u32 pxcount;
696
  u32 rest[];
697
};
698

    
699
struct ospf_lsa_prefix
700
{
701
#ifdef CPU_BIG_ENDIAN
702
  u16 pxcount;
703
  u16 ref_type;
704
#else
705
  u16 ref_type;
706
  u16 pxcount;
707
#endif
708
  u32 ref_id;
709
  u32 ref_rt;
710
  u32 rest[];
711
};
712

    
713

    
714
static inline uint
715
lsa_net_count(struct ospf_lsa_header *lsa)
716
{
717
  return (lsa->length - sizeof(struct ospf_lsa_header) - sizeof(struct ospf_lsa_net))
718
    / sizeof(u32);
719
}
720

    
721
/* In ospf_area->rtr we store paths to routers, but we use RID (and not IP address)
722
   as index, so we need to encapsulate RID to IP address */
723

    
724
#define ipa_from_rid(x) ipa_from_u32(x)
725
#define ipa_to_rid(x) ipa_to_u32(x)
726

    
727
#define IPV6_PREFIX_SPACE(x) ((((x) + 63) / 32) * 4)
728
#define IPV6_PREFIX_WORDS(x) (((x) + 63) / 32)
729

    
730
/* FIXME: these four functions should be significantly redesigned w.r.t. integration,
731
   also should be named as ospf3_* instead of *_ipv6_* */
732

    
733
static inline u32 *
734
lsa_get_ipv6_prefix(u32 *buf, ip_addr *addr, int *pxlen, u8 *pxopts, u16 *rest)
735
{
736
  u8 pxl = (*buf >> 24);
737
  *pxopts = (*buf >> 16);
738
  *rest = *buf;
739
  *pxlen = pxl;
740
  buf++;
741

    
742
  *addr = IPA_NONE;
743

    
744
#ifdef IPV6
745
  if (pxl > 0)
746
    _I0(*addr) = *buf++;
747
  if (pxl > 32)
748
    _I1(*addr) = *buf++;
749
  if (pxl > 64)
750
    _I2(*addr) = *buf++;
751
  if (pxl > 96)
752
    _I3(*addr) = *buf++;
753

    
754
  /* Clean up remaining bits */
755
  if (pxl < 128)
756
    addr->addr[pxl / 32] &= u32_mkmask(pxl % 32);
757
#endif
758

    
759
  return buf;
760
}
761

    
762
static inline u32 *
763
lsa_get_ipv6_addr(u32 *buf, ip_addr *addr)
764
{
765
  *addr = *(ip_addr *) buf;
766
  return buf + 4;
767
}
768

    
769
static inline u32 *
770
put_ipv6_prefix(u32 *buf, ip_addr addr UNUSED4, u8 pxlen UNUSED4, u8 pxopts UNUSED4, u16 lh UNUSED4)
771
{
772
#ifdef IPV6
773
  *buf++ = ((pxlen << 24) | (pxopts << 16) | lh);
774

    
775
  if (pxlen > 0)
776
    *buf++ = _I0(addr);
777
  if (pxlen > 32)
778
    *buf++ = _I1(addr);
779
  if (pxlen > 64)
780
    *buf++ = _I2(addr);
781
  if (pxlen > 96)
782
    *buf++ = _I3(addr);
783
#endif
784
  return buf;
785
}
786

    
787
static inline u32 *
788
put_ipv6_addr(u32 *buf, ip_addr addr)
789
{
790
  *(ip_addr *) buf = addr;
791
  return buf + 4;
792
}
793

    
794

    
795
struct ospf_lsreq_header
796
{
797
  u32 type;
798
  u32 id;
799
  u32 rt;
800
};
801

    
802

    
803

    
804
#define SH_ROUTER_SELF 0xffffffff
805

    
806
struct lsadb_show_data {
807
  struct symbol *name;        /* Protocol to request data from */
808
  u16 type;                /* LSA Type, 0 -> all */
809
  u16 scope;                /* Scope, 0 -> all, hack to handle link scope as 1 */
810
  u32 area;                /* Specified for area scope */
811
  u32 lsid;                /* LSA ID, 0 -> all */
812
  u32 router;                /* Advertising router, 0 -> all */
813
};
814

    
815

    
816
#define EA_OSPF_METRIC1        EA_CODE(EAP_OSPF, 0)
817
#define EA_OSPF_METRIC2        EA_CODE(EAP_OSPF, 1)
818
#define EA_OSPF_TAG        EA_CODE(EAP_OSPF, 2)
819
#define EA_OSPF_ROUTER_ID EA_CODE(EAP_OSPF, 3)
820

    
821

    
822
/* ospf.c */
823
void ospf_schedule_rtcalc(struct ospf_proto *p);
824

    
825
static inline void ospf_notify_rt_lsa(struct ospf_area *oa)
826
{ oa->update_rt_lsa = 1; }
827

    
828
static inline void ospf_notify_net_lsa(struct ospf_iface *ifa)
829
{ ifa->update_net_lsa = 1; }
830

    
831
static inline void ospf_notify_link_lsa(struct ospf_iface *ifa)
832
{ ifa->update_link_lsa = 1; }
833

    
834

    
835
#define ospf_is_v2(X) OSPF_IS_V2
836
#define ospf_is_v3(X) (!OSPF_IS_V2)
837
/*
838
static inline int ospf_is_v2(struct ospf_proto *p)
839
{ return p->ospf2; }
840

841
static inline int ospf_is_v3(struct ospf_proto *p)
842
{ return ! p->ospf2; }
843
*/
844
static inline int ospf_get_version(struct ospf_proto *p UNUSED4 UNUSED6)
845
{ return ospf_is_v2(p) ? 2 : 3; }
846

    
847
struct ospf_area *ospf_find_area(struct ospf_proto *p, u32 aid);
848

    
849
static inline struct ospf_area *ospf_main_area(struct ospf_proto *p)
850
{ return (p->areano == 1) ? HEAD(p->area_list) : p->backbone; }
851

    
852
static inline int oa_is_stub(struct ospf_area *oa)
853
{ return (oa->options & (OPT_E | OPT_N)) == 0; }
854

    
855
static inline int oa_is_ext(struct ospf_area *oa)
856
{ return oa->options & OPT_E; }
857

    
858
static inline int oa_is_nssa(struct ospf_area *oa)
859
{ return oa->options & OPT_N; }
860

    
861
void ospf_sh_neigh(struct proto *P, char *iff);
862
void ospf_sh(struct proto *P);
863
void ospf_sh_iface(struct proto *P, char *iff);
864
void ospf_sh_state(struct proto *P, int verbose, int reachable);
865

    
866
void ospf_sh_lsadb(struct lsadb_show_data *ld);
867

    
868
/* iface.c */
869
void ospf_iface_chstate(struct ospf_iface *ifa, u8 state);
870
void ospf_iface_sm(struct ospf_iface *ifa, int event);
871
struct ospf_iface *ospf_iface_find(struct ospf_proto *p, struct iface *what);
872
void ospf_if_notify(struct proto *P, uint flags, struct iface *iface);
873
void ospf_ifa_notify2(struct proto *P, uint flags, struct ifa *a);
874
void ospf_ifa_notify3(struct proto *P, uint flags, struct ifa *a);
875
void ospf_iface_info(struct ospf_iface *ifa);
876
void ospf_iface_new(struct ospf_area *oa, struct ifa *addr, struct ospf_iface_patt *ip);
877
void ospf_iface_new_vlink(struct ospf_proto *p, struct ospf_iface_patt *ip);
878
void ospf_iface_remove(struct ospf_iface *ifa);
879
void ospf_iface_shutdown(struct ospf_iface *ifa);
880
int ospf_iface_assure_bufsize(struct ospf_iface *ifa, uint plen);
881
int ospf_iface_reconfigure(struct ospf_iface *ifa, struct ospf_iface_patt *new);
882
void ospf_reconfigure_ifaces(struct ospf_proto *p);
883
void ospf_open_vlink_sk(struct ospf_proto *p);
884
struct nbma_node *find_nbma_node_(list *nnl, ip_addr ip);
885

    
886
static inline struct nbma_node * find_nbma_node(struct ospf_iface *ifa, ip_addr ip)
887
{ return find_nbma_node_(&ifa->nbma_list, ip); }
888

    
889
/* neighbor.c */
890
struct ospf_neighbor *ospf_neighbor_new(struct ospf_iface *ifa);
891
void ospf_neigh_sm(struct ospf_neighbor *n, int event);
892
void ospf_dr_election(struct ospf_iface *ifa);
893
struct ospf_neighbor *find_neigh(struct ospf_iface *ifa, u32 rid);
894
struct ospf_neighbor *find_neigh_by_ip(struct ospf_iface *ifa, ip_addr ip);
895
void ospf_neigh_update_bfd(struct ospf_neighbor *n, int use_bfd);
896
void ospf_sh_neigh_info(struct ospf_neighbor *n);
897

    
898
/* packet.c */
899
void ospf_pkt_fill_hdr(struct ospf_iface *ifa, void *buf, u8 h_type);
900
int ospf_rx_hook(sock * sk, uint size);
901
// void ospf_tx_hook(sock * sk);
902
void ospf_err_hook(sock * sk, int err);
903
void ospf_verr_hook(sock *sk, int err);
904
void ospf_send_to(struct ospf_iface *ifa, ip_addr ip);
905
void ospf_send_to_agt(struct ospf_iface *ifa, u8 state);
906
void ospf_send_to_bdr(struct ospf_iface *ifa);
907

    
908
static inline uint ospf_pkt_maxsize(struct ospf_iface *ifa)
909
{ return ifa->tx_length - ifa->tx_hdrlen; }
910

    
911
static inline void ospf_send_to_all(struct ospf_iface *ifa)
912
{ ospf_send_to(ifa, ifa->all_routers); }
913

    
914
static inline void ospf_send_to_des(struct ospf_iface *ifa)
915
{
916
  if (ipa_nonzero(ifa->des_routers))
917
    ospf_send_to(ifa, ifa->des_routers);
918
  else
919
    ospf_send_to_bdr(ifa);
920
}
921

    
922
#ifndef PARSER
923
#define DROP(DSC,VAL) do { err_dsc = DSC; err_val = VAL; goto drop; } while(0)
924
#define DROP1(DSC) do { err_dsc = DSC; goto drop; } while(0)
925
#define SKIP(DSC) do { err_dsc = DSC; goto skip; } while(0)
926
#endif
927

    
928
static inline uint ospf_pkt_hdrlen(struct ospf_proto *p UNUSED4 UNUSED6)
929
{ return ospf_is_v2(p) ? (sizeof(struct ospf_packet) + sizeof(union ospf_auth)) : sizeof(struct ospf_packet); }
930

    
931
static inline void * ospf_tx_buffer(struct ospf_iface *ifa)
932
{ return ifa->sk->tbuf; }
933

    
934
/* hello.c */
935
#define OHS_HELLO    0
936
#define OHS_POLL     1
937
#define OHS_SHUTDOWN 2
938

    
939
void ospf_send_hello(struct ospf_iface *ifa, int kind, struct ospf_neighbor *dirn);
940
void ospf_receive_hello(struct ospf_packet *pkt, struct ospf_iface *ifa, struct ospf_neighbor *n, ip_addr faddr);
941

    
942
/* dbdes.c */
943
void ospf_send_dbdes(struct ospf_proto *p, struct ospf_neighbor *n);
944
void ospf_rxmt_dbdes(struct ospf_proto *p, struct ospf_neighbor *n);
945
void ospf_receive_dbdes(struct ospf_packet *pkt, struct ospf_iface *ifa, struct ospf_neighbor *n);
946

    
947
/* lsreq.c */
948
void ospf_send_lsreq(struct ospf_proto *p, struct ospf_neighbor *n);
949
void ospf_receive_lsreq(struct ospf_packet *pkt, struct ospf_iface *ifa, struct ospf_neighbor *n);
950

    
951
/* lsupd.c */
952
void ospf_dump_lsahdr(struct ospf_proto *p, struct ospf_lsa_header *lsa_n);
953
void ospf_dump_common(struct ospf_proto *p, struct ospf_packet *pkt);
954
void ospf_lsa_lsrt_down_(struct top_hash_entry *en, struct ospf_neighbor *n, struct top_hash_entry *ret);
955
void ospf_add_flushed_to_lsrt(struct ospf_proto *p, struct ospf_neighbor *n);
956
void ospf_flood_event(void *ptr);
957
int ospf_flood_lsa(struct ospf_proto *p, struct top_hash_entry *en, struct ospf_neighbor *from);
958
int ospf_send_lsupd(struct ospf_proto *p, struct top_hash_entry **lsa_list, uint lsa_count, struct ospf_neighbor *n);
959
void ospf_rxmt_lsupd(struct ospf_proto *p, struct ospf_neighbor *n);
960
void ospf_receive_lsupd(struct ospf_packet *pkt, struct ospf_iface *ifa, struct ospf_neighbor *n);
961

    
962
/* lsack.c */
963
void ospf_enqueue_lsack(struct ospf_neighbor *n, struct ospf_lsa_header *h_n, int queue);
964
void ospf_reset_lsack_queue(struct ospf_neighbor *n);
965
void ospf_send_lsack(struct ospf_proto *p, struct ospf_neighbor *n, int queue);
966
void ospf_receive_lsack(struct ospf_packet *pkt, struct ospf_iface *ifa, struct ospf_neighbor *n);
967

    
968

    
969
#include "proto/ospf/rt.h"
970
#include "proto/ospf/topology.h"
971
#include "proto/ospf/lsalib.h"
972

    
973
#endif /* _BIRD_OSPF_H_ */