]> git.ipfire.org Git - thirdparty/bird.git/blob - proto/ospf/ospf.h
OSPF: Basic support for DN-bit handling (RFC 4576)
[thirdparty/bird.git] / proto / ospf / ospf.h
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/idm.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 #define OSPF_TRACE(flags, msg, args...) \
41 do { if ((p->p.debug & flags) || OSPF_FORCE_DEBUG) \
42 log(L_TRACE "%s: " msg, p->p.name , ## args ); } while(0)
43
44 #define OSPF_PACKET(dumpfn, buffer, msg, args...) \
45 do { if ((p->p.debug & D_PACKETS) || OSPF_FORCE_DEBUG) \
46 { log(L_TRACE "%s: " msg, p->p.name, ## args ); dumpfn(p, buffer); } } while(0)
47
48 #define LOG_PKT(msg, args...) \
49 log_rl(&p->log_pkt_tbf, L_REMOTE "%s: " msg, p->p.name, args)
50
51 #define LOG_PKT_AUTH(msg, args...) \
52 log_rl(&p->log_pkt_tbf, L_AUTH "%s: " msg, p->p.name, args)
53
54 #define LOG_PKT_WARN(msg, args...) \
55 log_rl(&p->log_pkt_tbf, L_WARN "%s: " msg, p->p.name, args)
56
57 #define LOG_LSA1(msg, args...) \
58 log_rl(&p->log_lsa_tbf, L_REMOTE "%s: " msg, p->p.name, args)
59
60 #define LOG_LSA2(msg, args...) \
61 do { if (! p->log_lsa_tbf.drop) \
62 log(L_REMOTE "%s: " msg, p->p.name, args); } while(0)
63
64
65 #define OSPF_PROTO 89
66
67 #define LSREFRESHTIME 1800 /* 30 minutes */
68 #define MINLSINTERVAL (5 S_)
69 #define MINLSARRIVAL (1 S_)
70 #define LSINFINITY 0xffffff
71
72 #define OSPF_PKT_TYPES 5 /* HELLO_P .. LSACK_P */
73 #define OSPF3_CRYPTO_ID 1 /* OSPFv3 Cryptographic Protocol ID */
74
75 #define OSPF_DEFAULT_TICK 1
76 #define OSPF_DEFAULT_STUB_COST 1000
77 #define OSPF_DEFAULT_ECMP_LIMIT 16
78 #define OSPF_DEFAULT_TRANSINT 40
79
80 #define OSPF_MIN_PKT_SIZE 256
81 #define OSPF_MAX_PKT_SIZE 65535
82
83 #define OSPF_VLINK_ID_OFFSET 0x80000000
84
85 struct ospf_config
86 {
87 struct proto_config c;
88 uint tick;
89 u8 ospf2;
90 u8 af_ext;
91 u8 af_mc;
92 u8 rfc1583;
93 u8 stub_router;
94 u8 merge_external;
95 u8 instance_id;
96 u8 instance_id_set;
97 u8 abr;
98 u8 asbr;
99 u8 vpn_pe;
100 int ecmp;
101 list area_list; /* list of area configs (struct ospf_area_config) */
102 list vlink_list; /* list of configured vlinks (struct ospf_iface_patt) */
103 };
104
105 struct ospf_area_config
106 {
107 node n;
108 u32 areaid;
109 u32 default_cost; /* Cost of default route for stub areas
110 (With possible LSA_EXT3_EBIT for NSSA areas) */
111 u8 type; /* Area type (standard, stub, NSSA), represented
112 by option flags (OPT_E, OPT_N) */
113 u8 summary; /* Import summaries to this stub/NSSA area, valid for ABR */
114 u8 default_nssa; /* Generate default NSSA route for NSSA+summary area */
115 u8 translator; /* Translator role, for NSSA ABR */
116 u32 transint; /* Translator stability interval */
117 list patt_list; /* List of iface configs (struct ospf_iface_patt) */
118 list net_list; /* List of aggregate networks for that area */
119 list enet_list; /* List of aggregate external (NSSA) networks */
120 list stubnet_list; /* List of stub networks added to Router LSA */
121 };
122
123 struct area_net_config
124 {
125 node n;
126 net_addr prefix;
127 u32 tag;
128 u8 hidden;
129 };
130
131 struct area_net
132 {
133 u32 metric; /* With possible LSA_EXT3_EBIT for NSSA area nets */
134 u32 tag;
135 u8 hidden;
136 u8 active;
137 struct fib_node fn;
138 };
139
140 struct ospf_stubnet_config
141 {
142 node n;
143 net_addr prefix;
144 u32 cost;
145 u8 hidden;
146 u8 summary;
147 };
148
149 struct nbma_node
150 {
151 node n;
152 ip_addr ip;
153 byte eligible;
154 byte found;
155 };
156
157 struct ospf_iface_patt
158 {
159 struct iface_patt i;
160 u32 type;
161 u32 stub;
162 u32 cost;
163 u32 helloint;
164 u32 rxmtint;
165 u32 pollint;
166 u32 waitint;
167 u32 deadc;
168 u32 deadint;
169 u32 inftransdelay;
170 list nbma_list;
171 u32 priority;
172 u32 voa;
173 u32 vid;
174 int tx_tos;
175 int tx_priority;
176 u16 tx_length;
177 u16 rx_buffer;
178 #define OSPF_RXBUF_MINSIZE 256 /* Minimal allowed size */
179 u8 instance_id;
180 u8 instance_id_set;
181 u8 autype; /* OSPF_AUTH_*, not really used in OSPFv3 */
182 u8 strictnbma;
183 u8 check_link;
184 u8 ecmp_weight;
185 u8 link_lsa_suppression;
186 u8 real_bcast; /* Not really used in OSPFv3 */
187 u8 ptp_netmask; /* bool + 2 for unspecified */
188 u8 ttl_security; /* bool + 2 for TX only */
189 u8 bfd;
190 list *passwords;
191 };
192
193 /* Default values for interface parameters */
194 #define COST_D 10
195 #define RXMTINT_D 5
196 #define INFTRANSDELAY_D 1
197 #define PRIORITY_D 1
198 #define HELLOINT_D 10
199 #define POLLINT_D 20
200 #define DEADC_D 4
201 #define WAIT_DMH 4
202 /* Value of Wait timer - not found it in RFC * - using 4*HELLO */
203
204
205
206 struct ospf_proto
207 {
208 struct proto p;
209 timer *disp_timer; /* OSPF proto dispatcher */
210 uint tick;
211 struct top_graph *gr; /* LSA graph */
212 slist lsal; /* List of all LSA's */
213 int calcrt; /* Routing table calculation scheduled?
214 0=no, 1=normal, 2=forced reload */
215 list iface_list; /* List of OSPF interfaces (struct ospf_iface) */
216 list area_list; /* List of OSPF areas (struct ospf_area) */
217 int areano; /* Number of area I belong to */
218 int padj; /* Number of neighbors in Exchange or Loading state */
219 struct fib rtf; /* Routing table */
220 struct idm idm; /* OSPFv3 LSA ID map */
221 u8 ospf2; /* OSPF v2 or v3 */
222 u8 af_ext; /* OSPFv3-AF extension */
223 u8 af_mc; /* OSPFv3-AF multicast */
224 u8 rfc1583; /* RFC1583 compatibility */
225 u8 stub_router; /* Do not forward transit traffic */
226 u8 merge_external; /* Should i merge external routes? */
227 u8 instance_id; /* Differentiate between more OSPF instances */
228 u8 asbr; /* May i originate any ext/NSSA lsa? */
229 u8 vpn_pe; /* Should we do VPN PE specific behavior (RFC 4577)? */
230 u8 ecmp; /* Maximal number of nexthops in ECMP route, or 0 */
231 u64 csn64; /* Last used cryptographic sequence number */
232 struct ospf_area *backbone; /* If exists */
233 event *flood_event; /* Event for flooding LS updates */
234 void *lsab; /* LSA buffer used when originating router LSAs */
235 int lsab_size, lsab_used;
236 linpool *nhpool; /* Linpool used for next hops computed in SPF */
237 sock *vlink_sk; /* IP socket used for vlink TX */
238 u32 router_id;
239 u32 last_vlink_id; /* Interface IDs for vlinks (starts at 0x80000000) */
240 struct tbf log_pkt_tbf; /* TBF for packet messages */
241 struct tbf log_lsa_tbf; /* TBF for LSA messages */
242 };
243
244 struct ospf_area
245 {
246 node n;
247 u32 areaid;
248 struct ospf_area_config *ac; /* Related area config */
249 struct top_hash_entry *rt; /* My own router LSA */
250 struct top_hash_entry *pxr_lsa; /* Originated prefix LSA */
251 list cand; /* List of candidates for RT calc. */
252 struct fib net_fib; /* Networks to advertise or not */
253 struct fib enet_fib; /* External networks for NSSAs */
254 u32 options; /* Optional features */
255 u8 update_rt_lsa; /* Rt lsa origination scheduled? */
256 u8 trcap; /* Transit capability? */
257 u8 marked; /* Used in OSPF reconfigure */
258 u8 translate; /* Translator state (TRANS_*), for NSSA ABR */
259 timer *translator_timer; /* For NSSA translator switch */
260 struct ospf_proto *po;
261 struct fib rtr; /* Routing tables for routers */
262 };
263
264 struct ospf_iface
265 {
266 node n;
267 struct iface *iface; /* Nest's iface (NULL for vlinks) */
268 struct ifa *addr; /* IP prefix associated with that OSPF iface */
269 struct ospf_area *oa;
270 struct ospf_iface_patt *cf;
271 char *ifname; /* Interface name (iface->name), new one for vlinks */
272
273 pool *pool;
274 sock *sk; /* IP socket */
275 list neigh_list; /* List of neighbors (struct ospf_neighbor) */
276 u32 cost; /* Cost of iface */
277 u32 waitint; /* Number of seconds before changing state from wait */
278 u32 rxmtint; /* Number of seconds between LSA retransmissions */
279 u32 pollint; /* Poll interval in seconds */
280 u32 deadint; /* After deadint seconds without hellos is router dead */
281 u32 iface_id; /* Interface ID (iface->index or new value for vlinks) */
282 u32 vid; /* ID of peer of virtual link */
283 ip_addr vip; /* IP of peer of virtual link */
284 struct ospf_iface *vifa; /* OSPF iface which the vlink goes through */
285 struct ospf_area *voa; /* OSPF area which the vlink goes through */
286 u16 inftransdelay; /* The estimated number of seconds it takes to
287 transmit a Link State Update Packet over this
288 interface. LSAs contained in the update */
289 u16 helloint; /* number of seconds between hello sending */
290 list *passwords;
291 u32 csn; /* Last used crypt seq number */
292 btime csn_use; /* Last time when packet with that CSN was sent */
293 ip_addr all_routers; /* Multicast (or broadcast) address for all routers */
294 ip_addr des_routers; /* Multicast (or NULL) address for designated routers */
295 ip_addr drip; /* Designated router IP */
296 ip_addr bdrip; /* Backup DR IP */
297 u32 drid; /* DR Router ID */
298 u32 bdrid; /* BDR Router ID */
299 s16 rt_pos_beg; /* Position of iface in Router-LSA, begin, inclusive */
300 s16 rt_pos_end; /* Position of iface in Router-LSA, end, exclusive */
301 s16 px_pos_beg; /* Position of iface in Rt Prefix-LSA, begin, inclusive */
302 s16 px_pos_end; /* Position of iface in Rt Prefix-LSA, end, exclusive */
303 u32 dr_iface_id; /* if drid is valid, this is iface_id of DR (for connecting network) */
304 u8 instance_id; /* Used to differentiate between more OSPF
305 instances on one interface */
306 u8 autype; /* Authentication type (OSPF_AUTH_*) */
307 u8 type; /* OSPF view of type (OSPF_IT_*) */
308 u8 strictnbma; /* Can I talk with unknown neighbors? */
309 u8 stub; /* Inactive interface */
310 u8 state; /* Interface state machine (OSPF_IS_*) */
311 timer *wait_timer; /* WAIT timer */
312 timer *hello_timer; /* HELLOINT timer */
313 timer *poll_timer; /* Poll Interval - for NBMA */
314
315 struct top_hash_entry *link_lsa; /* Originated link LSA */
316 struct top_hash_entry *net_lsa; /* Originated network LSA */
317 struct top_hash_entry *pxn_lsa; /* Originated prefix LSA */
318 struct top_hash_entry **flood_queue; /* LSAs queued for LSUPD */
319 u8 update_link_lsa;
320 u8 update_net_lsa;
321 u16 flood_queue_used; /* The current number of LSAs in flood_queue */
322 u16 flood_queue_size; /* The maximum number of LSAs in flood_queue */
323 int fadj; /* Number of fully adjacent neighbors */
324 list nbma_list;
325 u8 priority; /* A router priority for DR election */
326 u8 ioprob;
327 #define OSPF_I_OK 0 /* Everything OK */
328 #define OSPF_I_SK 1 /* Socket open failed */
329 #define OSPF_I_LL 2 /* Missing link-local address (OSPFv3) */
330 u8 sk_dr; /* Socket is a member of designated routers group */
331 u8 marked; /* Used in OSPF reconfigure, 2 for force restart */
332 u16 rxbuf; /* Buffer size */
333 u16 tx_length; /* Soft TX packet length limit, usually MTU */
334 u16 tx_hdrlen; /* Expected packet header length, less than tx_length */
335 u8 check_link; /* Whether iface link change is used */
336 u8 ecmp_weight; /* Weight used for ECMP */
337 u8 link_lsa_suppression; /* Suppression of Link-LSA origination */
338 u8 ptp_netmask; /* Send real netmask for P2P */
339 u8 check_ttl; /* Check incoming packets for TTL 255 */
340 u8 bfd; /* Use BFD on iface */
341 };
342
343 struct ospf_neighbor
344 {
345 node n;
346 pool *pool;
347 struct ospf_iface *ifa;
348 u8 state;
349 timer *inactim; /* Inactivity timer */
350 u8 imms; /* I, M, Master/slave received */
351 u8 myimms; /* I, M Master/slave */
352 u32 dds; /* DD Sequence number being sent */
353 u32 ddr; /* last Dat Des packet received */
354
355 u32 rid; /* Router ID */
356 ip_addr ip; /* IP of it's interface */
357 u8 priority; /* Priority */
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; /* OSPFv2: Last received crypt seq number */
395 u64 csn64[OSPF_PKT_TYPES]; /* OSPFv3: Last received CSN for each type of packet */
396 };
397
398
399 /* OSPF interface types */
400 #define OSPF_IT_BCAST 0
401 #define OSPF_IT_NBMA 1
402 #define OSPF_IT_PTP 2
403 #define OSPF_IT_PTMP 3
404 #define OSPF_IT_VLINK 4
405 #define OSPF_IT_UNDEF 5
406
407 /* OSPF interface states */
408 #define OSPF_IS_DOWN 0 /* Not active */
409 #define OSPF_IS_LOOP 1 /* Iface with no link */
410 #define OSPF_IS_WAITING 2 /* Waiting for Wait timer */
411 #define OSPF_IS_PTP 3 /* PTP operational */
412 #define OSPF_IS_DROTHER 4 /* I'm on BCAST or NBMA and I'm not DR */
413 #define OSPF_IS_BACKUP 5 /* I'm BDR */
414 #define OSPF_IS_DR 6 /* I'm DR */
415
416 /* Definitions for interface state machine */
417 #define ISM_UP 0 /* Interface Up */
418 #define ISM_WAITF 1 /* Wait timer fired */
419 #define ISM_BACKS 2 /* Backup seen */
420 #define ISM_NEICH 3 /* Neighbor change */
421 #define ISM_LOOP 4 /* Link down */
422 #define ISM_UNLOOP 5 /* Link up */
423 #define ISM_DOWN 6 /* Interface down */
424
425 /* OSPF authentication types */
426 #define OSPF_AUTH_NONE 0
427 #define OSPF_AUTH_SIMPLE 1
428 #define OSPF_AUTH_CRYPT 2
429
430 #define OSPF3_AUTH_HMAC 1 /* HMAC Cryptographic Authentication */
431
432 /* OSPF neighbor states */
433 #define NEIGHBOR_DOWN 0
434 #define NEIGHBOR_ATTEMPT 1
435 #define NEIGHBOR_INIT 2
436 #define NEIGHBOR_2WAY 3
437 #define NEIGHBOR_EXSTART 4
438 #define NEIGHBOR_EXCHANGE 5
439 #define NEIGHBOR_LOADING 6
440 #define NEIGHBOR_FULL 7
441
442 /* Definitions for neighbor state machine */
443 #define INM_HELLOREC 0 /* Hello Received */
444 #define INM_START 1 /* Neighbor start - for NBMA */
445 #define INM_2WAYREC 2 /* 2-Way received */
446 #define INM_NEGDONE 3 /* Negotiation done */
447 #define INM_EXDONE 4 /* Exchange done */
448 #define INM_BADLSREQ 5 /* Bad LS Request */
449 #define INM_LOADDONE 6 /* Load done */
450 #define INM_ADJOK 7 /* AdjOK? */
451 #define INM_SEQMIS 8 /* Sequence number mismatch */
452 #define INM_1WAYREC 9 /* 1-Way */
453 #define INM_KILLNBR 10 /* Kill Neighbor */
454 #define INM_INACTTIM 11 /* Inactivity timer */
455 #define INM_LLDOWN 12 /* Line down */
456
457 #define TRANS_OFF 0
458 #define TRANS_ON 1
459 #define TRANS_WAIT 2 /* Waiting before the end of translation */
460
461
462 /* Generic option flags */
463 #define OPT_V6 0x0001 /* OSPFv3, LSA relevant for IPv6 routing calculation */
464 #define OPT_E 0x0002 /* Related to AS-external LSAs */
465 #define OPT_MC 0x0004 /* Related to MOSPF, not used and obsolete */
466 #define OPT_N 0x0008 /* Related to NSSA */
467 #define OPT_P 0x0008 /* OSPFv2, flags P and N share position, see NSSA RFC */
468 #define OPT_L_V2 0x0010 /* OSPFv2, link-local signaling, not used */
469 #define OPT_R 0x0010 /* OSPFv3, originator is active router */
470 #define OPT_DC 0x0020 /* Related to demand circuits, not used */
471 #define OPT_O 0x0040 /* OSPFv2 Opaque LSA (RFC 5250) */
472 #define OPT_DN 0x0080 /* OSPFv2 VPN loop prevention (RFC 4576)*/
473 #define OPT_AF 0x0100 /* OSPFv3 Address Families (RFC 5838) */
474 #define OPT_L_V3 0x0200 /* OSPFv3, link-local signaling */
475 #define OPT_AT 0x0400 /* OSPFv3, authentication trailer */
476
477 /* Router-LSA VEB flags are are stored together with links (OSPFv2) or options (OSPFv3) */
478 #define OPT_RT_B (0x01 << 24)
479 #define OPT_RT_E (0x02 << 24)
480 #define OPT_RT_V (0x04 << 24)
481 #define OPT_RT_NT (0x10 << 24)
482
483 /* Prefix flags, specific for OSPFv3 */
484 #define OPT_PX_NU 0x01
485 #define OPT_PX_LA 0x02
486 #define OPT_PX_P 0x08
487 #define OPT_PX_DN 0x10
488
489
490 struct ospf_packet
491 {
492 u8 version;
493 u8 type;
494 u16 length;
495 u32 routerid;
496 u32 areaid;
497 u16 checksum;
498 u8 instance_id; /* See RFC 6549 */
499 u8 autype; /* Undefined for OSPFv3 */
500 };
501
502 struct ospf_lls
503 {
504 u16 checksum;
505 u16 length;
506 byte data[0];
507 };
508
509 struct ospf_auth_crypto
510 {
511 u16 zero;
512 u8 keyid;
513 u8 len;
514 u32 csn; /* Cryptographic sequence number (32-bit) */
515 };
516
517 union ospf_auth2
518 {
519 u8 password[8];
520 struct ospf_auth_crypto c32;
521 };
522
523 struct ospf_auth3
524 {
525 u16 type; /* Authentication type (OSPF3_AUTH_*) */
526 u16 length; /* Authentication trailer length (header + data) */
527 u16 reserved;
528 u16 sa_id; /* Security association identifier (key_id) */
529 u64 csn; /* Cryptographic sequence number (64-bit) */
530 byte data[0]; /* Authentication data */
531 };
532
533
534 /* Packet types */
535 #define HELLO_P 1 /* Hello */
536 #define DBDES_P 2 /* Database description */
537 #define LSREQ_P 3 /* Link state request */
538 #define LSUPD_P 4 /* Link state update */
539 #define LSACK_P 5 /* Link state acknowledgement */
540
541
542 #define DBDES_I 4 /* Init bit */
543 #define DBDES_M 2 /* More bit */
544 #define DBDES_MS 1 /* Master/Slave bit */
545 #define DBDES_IMMS (DBDES_I | DBDES_M | DBDES_MS)
546
547
548 /* OSPFv3 LSA Types / LSA Function Codes */
549 /* https://www.iana.org/assignments/ospfv3-parameters/ospfv3-parameters.xhtml#ospfv3-parameters-3 */
550 #define LSA_T_RT 0x2001
551 #define LSA_T_NET 0x2002
552 #define LSA_T_SUM_NET 0x2003
553 #define LSA_T_SUM_RT 0x2004
554 #define LSA_T_EXT 0x4005
555 #define LSA_T_NSSA 0x2007
556 #define LSA_T_LINK 0x0008
557 #define LSA_T_PREFIX 0x2009
558 #define LSA_T_RI_ 0x000C
559 #define LSA_T_RI_LINK 0x800C
560 #define LSA_T_RI_AREA 0xA00C
561 #define LSA_T_RI_AS 0xC00C
562 #define LSA_T_OPAQUE_ 0x1FFF
563 #define LSA_T_OPAQUE_LINK 0x9FFF
564 #define LSA_T_OPAQUE_AREA 0xBFFF
565 #define LSA_T_OPAQUE_AS 0xDFFF
566
567 #define LSA_T_V2_OPAQUE_ 0x0009
568 #define LSA_T_V2_MASK 0x00ff
569
570 /* OSPFv2 Opaque LSA Types */
571 /* https://www.iana.org/assignments/ospf-opaque-types/ospf-opaque-types.xhtml#ospf-opaque-types-2 */
572 #define LSA_OT_RI 0x04
573
574 #define LSA_FUNCTION_MASK 0x1FFF
575 #define LSA_FUNCTION(type) ((type) & LSA_FUNCTION_MASK)
576
577 #define LSA_UBIT 0x8000
578
579 #define LSA_SCOPE_LINK 0x0000
580 #define LSA_SCOPE_AREA 0x2000
581 #define LSA_SCOPE_AS 0x4000
582 #define LSA_SCOPE_RES 0x6000
583 #define LSA_SCOPE_MASK 0x6000
584 #define LSA_SCOPE(type) ((type) & LSA_SCOPE_MASK)
585 #define LSA_SCOPE_ORDER(type) (((type) >> 13) & 0x3)
586
587
588 #define LSA_MAXAGE 3600 /* 1 hour */
589 #define LSA_CHECKAGE 300 /* 5 minutes */
590 #define LSA_MAXAGEDIFF 900 /* 15 minutes */
591
592 #define LSA_ZEROSEQNO ((s32) 0x80000000)
593 #define LSA_INITSEQNO ((s32) 0x80000001)
594 #define LSA_MAXSEQNO ((s32) 0x7fffffff)
595
596 #define LSA_METRIC_MASK 0x00FFFFFF
597 #define LSA_OPTIONS_MASK 0x00FFFFFF
598
599
600 #define LSART_PTP 1
601 #define LSART_NET 2
602 #define LSART_STUB 3
603 #define LSART_VLNK 4
604
605 #define LSA_RT2_LINKS 0x0000FFFF
606
607 #define LSA_SUM2_TOS 0xFF000000
608
609 #define LSA_EXT2_TOS 0x7F000000
610 #define LSA_EXT2_EBIT 0x80000000
611
612 #define LSA_EXT3_EBIT 0x04000000
613 #define LSA_EXT3_FBIT 0x02000000
614 #define LSA_EXT3_TBIT 0x01000000
615
616 /* OSPF Router Information (RI) TLVs */
617 /* https://www.iana.org/assignments/ospf-parameters/ospf-parameters.xhtml#ri-tlv */
618 #define LSA_RI_RIC 1
619 #define LSA_RI_RFC 2
620
621 /* OSPF Router Informational Capability Bits */
622 /* https://www.iana.org/assignments/ospf-parameters/ospf-parameters.xhtml#router-informational-capability */
623 #define LSA_RIC_GR_CAPABLE 0
624 #define LSA_RIC_GR_HELPER 1
625 #define LSA_RIC_STUB_ROUTER 2
626
627
628 struct ospf_lsa_header
629 {
630 u16 age; /* LS Age */
631 u16 type_raw; /* Type, mixed with options on OSPFv2 */
632
633 u32 id;
634 u32 rt; /* Advertising router */
635 s32 sn; /* LS Sequence number */
636 u16 checksum;
637 u16 length;
638 };
639
640
641 /* In OSPFv2, options are embedded in higher half of type_raw */
642 static inline u8 lsa_get_options(struct ospf_lsa_header *lsa)
643 { return lsa->type_raw >> 8; }
644
645 static inline void lsa_set_options(struct ospf_lsa_header *lsa, u16 options)
646 { lsa->type_raw = (lsa->type_raw & 0xff) | (options << 8); }
647
648
649 struct ospf_lsa_rt
650 {
651 u32 options; /* VEB flags, mixed with link count for OSPFv2 and options for OSPFv3 */
652 };
653
654 struct ospf_lsa_rt2_link
655 {
656 u32 id;
657 u32 data;
658 #ifdef CPU_BIG_ENDIAN
659 u8 type;
660 u8 no_tos;
661 u16 metric;
662 #else
663 u16 metric;
664 u8 no_tos;
665 u8 type;
666 #endif
667 };
668
669 struct ospf_lsa_rt2_tos
670 {
671 #ifdef CPU_BIG_ENDIAN
672 u8 tos;
673 u8 padding;
674 u16 metric;
675 #else
676 u16 metric;
677 u8 padding;
678 u8 tos;
679 #endif
680 };
681
682 struct ospf_lsa_rt3_link
683 {
684 #ifdef CPU_BIG_ENDIAN
685 u8 type;
686 u8 padding;
687 u16 metric;
688 #else
689 u16 metric;
690 u8 padding;
691 u8 type;
692 #endif
693 u32 lif; /* Local interface ID */
694 u32 nif; /* Neighbor interface ID */
695 u32 id; /* Neighbor router ID */
696 };
697
698
699 struct ospf_lsa_net
700 {
701 u32 optx; /* Netmask for OSPFv2, options for OSPFv3 */
702 u32 routers[];
703 };
704
705 struct ospf_lsa_sum2
706 {
707 u32 netmask;
708 u32 metric;
709 };
710
711 struct ospf_lsa_sum3_net
712 {
713 u32 metric;
714 u32 prefix[];
715 };
716
717 struct ospf_lsa_sum3_rt
718 {
719 u32 options;
720 u32 metric;
721 u32 drid;
722 };
723
724 struct ospf_lsa_ext2
725 {
726 u32 netmask;
727 u32 metric;
728 u32 fwaddr;
729 u32 tag;
730 };
731
732 struct ospf_lsa_ext3
733 {
734 u32 metric;
735 u32 rest[];
736 };
737
738 struct ospf_lsa_ext_local
739 {
740 net_addr net;
741 ip_addr fwaddr;
742 u32 metric, ebit, fbit, tag, propagate, downwards;
743 u8 pxopts;
744 };
745
746 struct ospf_lsa_link
747 {
748 u32 options;
749 ip6_addr lladdr;
750 u32 pxcount;
751 u32 rest[];
752 };
753
754 struct ospf_lsa_prefix
755 {
756 #ifdef CPU_BIG_ENDIAN
757 u16 pxcount;
758 u16 ref_type;
759 #else
760 u16 ref_type;
761 u16 pxcount;
762 #endif
763 u32 ref_id;
764 u32 ref_rt;
765 u32 rest[];
766 };
767
768 struct ospf_tlv
769 {
770 #ifdef CPU_BIG_ENDIAN
771 u16 type;
772 u16 length;
773 #else
774 u16 length;
775 u16 type;
776 #endif
777 u32 data[];
778 };
779
780
781 static inline uint
782 lsa_net_count(struct ospf_lsa_header *lsa)
783 {
784 return (lsa->length - sizeof(struct ospf_lsa_header) - sizeof(struct ospf_lsa_net))
785 / sizeof(u32);
786 }
787
788 /* In ospf_area->rtr we store paths to routers, but we use RID (and not IP address)
789 as index, so we need to encapsulate RID to IP address */
790
791 #define net_from_rid(x) NET_ADDR_IP4(ip4_from_u32(x), IP4_MAX_PREFIX_LENGTH)
792 #define rid_from_net(x) ip4_to_u32(((net_addr_ip4 *) x)->prefix)
793
794 #define IPV6_PREFIX_SPACE(x) ((((x) + 63) / 32) * 4)
795 #define IPV6_PREFIX_WORDS(x) (((x) + 63) / 32)
796
797
798 static inline int
799 ospf_valid_prefix(net_addr *n)
800 {
801 /*
802 * In OSPFv2, prefix is stored as netmask; ip4_masklen() returns 255 for
803 * invalid one. But OSPFv3-AF may receive IPv4 net with 32 < pxlen < 128.
804 */
805 uint max = (n->type == NET_IP4) ? IP4_MAX_PREFIX_LENGTH : IP6_MAX_PREFIX_LENGTH;
806 return n->pxlen <= max;
807 }
808
809 /*
810 * In OSPFv3-AF (RFC 5835), IPv4 address is encoded by just placing it in the
811 * first 32 bits of IPv6 address and setting remaining bits to zero. Likewise
812 * for IPv4 prefix, where remaining bits do not matter. We use following
813 * functions to convert between IPv4 and IPv4-in-IPv6 representations:
814 */
815
816 static inline ip4_addr ospf3_6to4(ip6_addr a)
817 { return _MI4(_I0(a)); }
818
819 static inline ip6_addr ospf3_4to6(ip4_addr a)
820 { return _MI6(_I(a), 0, 0, 0); }
821
822
823 static inline u32 *
824 ospf3_get_prefix(u32 *buf, int af, net_addr *n, u8 *pxopts, u16 *rest)
825 {
826 ip6_addr px = IP6_NONE;
827 uint pxlen = (*buf >> 24);
828 *pxopts = (*buf >> 16) & 0xff;
829 if (rest) *rest = *buf & 0xffff;
830 buf++;
831
832 if (pxlen > 0)
833 _I0(px) = *buf++;
834 if (pxlen > 32)
835 _I1(px) = *buf++;
836 if (pxlen > 64)
837 _I2(px) = *buf++;
838 if (pxlen > 96)
839 _I3(px) = *buf++;
840
841 /* Clean up remaining bits */
842 if (pxlen < 128)
843 px.addr[pxlen / 32] &= u32_mkmask(pxlen % 32);
844
845 if (af == NET_IP4)
846 net_fill_ip4(n, ospf3_6to4(px), pxlen);
847 else
848 net_fill_ip6(n, px, pxlen);
849
850 return buf;
851 }
852
853 static inline u32 *
854 ospf3_put_prefix(u32 *buf, net_addr *n, u8 pxopts, u16 rest)
855 {
856 ip6_addr px = (n->type == NET_IP4) ? ospf3_4to6(net4_prefix(n)) : net6_prefix(n);
857 uint pxlen = n->pxlen;
858
859 *buf++ = ((pxlen << 24) | (pxopts << 16) | rest);
860
861 if (pxlen > 0)
862 *buf++ = _I0(px);
863 if (pxlen > 32)
864 *buf++ = _I1(px);
865 if (pxlen > 64)
866 *buf++ = _I2(px);
867 if (pxlen > 96)
868 *buf++ = _I3(px);
869
870 return buf;
871 }
872
873 static inline u32 *
874 ospf3_get_addr(u32 *buf, int af, ip_addr *addr)
875 {
876 ip6_addr a;
877 memcpy(&a, buf, 16);
878 *addr = (af == NET_IP4) ? ipa_from_ip4(ospf3_6to4(a)) : ipa_from_ip6(a);
879 return buf + 4;
880 }
881
882 static inline u32 *
883 ospf3_put_addr(u32 *buf, ip_addr addr)
884 {
885 ip6_addr a = ipa_is_ip4(addr) ? ospf3_4to6(ipa_to_ip4(addr)) : ipa_to_ip6(addr);
886 memcpy(buf, &a, 16);
887 return buf + 4;
888 }
889
890
891 struct ospf_lsreq_header
892 {
893 u32 type;
894 u32 id;
895 u32 rt;
896 };
897
898
899
900 #define SH_ROUTER_SELF 0xffffffff
901
902 struct lsadb_show_data {
903 struct symbol *name; /* Protocol to request data from */
904 u16 type; /* LSA Type, 0 -> all */
905 u16 scope; /* Scope, 0 -> all, hack to handle link scope as 1 */
906 u32 area; /* Specified for area scope */
907 u32 lsid; /* LSA ID, 0 -> all */
908 u32 router; /* Advertising router, 0 -> all */
909 };
910
911
912 #define EA_OSPF_METRIC1 EA_CODE(PROTOCOL_OSPF, 0)
913 #define EA_OSPF_METRIC2 EA_CODE(PROTOCOL_OSPF, 1)
914 #define EA_OSPF_TAG EA_CODE(PROTOCOL_OSPF, 2)
915 #define EA_OSPF_ROUTER_ID EA_CODE(PROTOCOL_OSPF, 3)
916
917
918 /* ospf.c */
919 void ospf_schedule_rtcalc(struct ospf_proto *p);
920
921 static inline void ospf_notify_rt_lsa(struct ospf_area *oa)
922 { oa->update_rt_lsa = 1; }
923
924 static inline void ospf_notify_net_lsa(struct ospf_iface *ifa)
925 { ifa->update_net_lsa = 1; }
926
927 static inline void ospf_notify_link_lsa(struct ospf_iface *ifa)
928 { ifa->update_link_lsa = 1; }
929
930 static inline int ospf_is_v2(struct ospf_proto *p)
931 { return p->ospf2; }
932
933 static inline int ospf_is_v3(struct ospf_proto *p)
934 { return ! p->ospf2; }
935
936 static inline int ospf_get_version(struct ospf_proto *p)
937 { return ospf_is_v2(p) ? 2 : 3; }
938
939 static inline int ospf_is_ip4(struct ospf_proto *p)
940 { return p->p.net_type == NET_IP4; }
941
942 static inline int ospf_is_ip6(struct ospf_proto *p)
943 { return p->p.net_type == NET_IP6; }
944
945 static inline int ospf_get_af(struct ospf_proto *p)
946 { return p->p.net_type; }
947
948 struct ospf_area *ospf_find_area(struct ospf_proto *p, u32 aid);
949
950 static inline struct ospf_area *ospf_main_area(struct ospf_proto *p)
951 { return (p->areano == 1) ? HEAD(p->area_list) : p->backbone; }
952
953 static inline int oa_is_stub(struct ospf_area *oa)
954 { return (oa->options & (OPT_E | OPT_N)) == 0; }
955
956 static inline int oa_is_ext(struct ospf_area *oa)
957 { return oa->options & OPT_E; }
958
959 static inline int oa_is_nssa(struct ospf_area *oa)
960 { return oa->options & OPT_N; }
961
962 void ospf_sh_neigh(struct proto *P, char *iff);
963 void ospf_sh(struct proto *P);
964 void ospf_sh_iface(struct proto *P, char *iff);
965 void ospf_sh_state(struct proto *P, int verbose, int reachable);
966
967 void ospf_sh_lsadb(struct lsadb_show_data *ld);
968
969 /* iface.c */
970 void ospf_iface_chstate(struct ospf_iface *ifa, u8 state);
971 void ospf_iface_sm(struct ospf_iface *ifa, int event);
972 struct ospf_iface *ospf_iface_find(struct ospf_proto *p, struct iface *what);
973 void ospf_if_notify(struct proto *P, uint flags, struct iface *iface);
974 void ospf_ifa_notify2(struct proto *P, uint flags, struct ifa *a);
975 void ospf_ifa_notify3(struct proto *P, uint flags, struct ifa *a);
976 void ospf_iface_info(struct ospf_iface *ifa);
977 void ospf_iface_new(struct ospf_area *oa, struct ifa *addr, struct ospf_iface_patt *ip);
978 void ospf_iface_new_vlink(struct ospf_proto *p, struct ospf_iface_patt *ip);
979 void ospf_iface_remove(struct ospf_iface *ifa);
980 void ospf_iface_shutdown(struct ospf_iface *ifa);
981 int ospf_iface_assure_bufsize(struct ospf_iface *ifa, uint plen);
982 int ospf_iface_reconfigure(struct ospf_iface *ifa, struct ospf_iface_patt *new);
983 void ospf_reconfigure_ifaces(struct ospf_proto *p);
984 void ospf_open_vlink_sk(struct ospf_proto *p);
985 struct nbma_node *find_nbma_node_(list *nnl, ip_addr ip);
986
987 static inline struct nbma_node * find_nbma_node(struct ospf_iface *ifa, ip_addr ip)
988 { return find_nbma_node_(&ifa->nbma_list, ip); }
989
990 /* neighbor.c */
991 struct ospf_neighbor *ospf_neighbor_new(struct ospf_iface *ifa);
992 void ospf_neigh_sm(struct ospf_neighbor *n, int event);
993 void ospf_dr_election(struct ospf_iface *ifa);
994 struct ospf_neighbor *find_neigh(struct ospf_iface *ifa, u32 rid);
995 struct ospf_neighbor *find_neigh_by_ip(struct ospf_iface *ifa, ip_addr ip);
996 void ospf_neigh_update_bfd(struct ospf_neighbor *n, int use_bfd);
997 void ospf_sh_neigh_info(struct ospf_neighbor *n);
998
999 /* packet.c */
1000 void ospf_pkt_fill_hdr(struct ospf_iface *ifa, void *buf, u8 h_type);
1001 int ospf_rx_hook(sock * sk, uint size);
1002 // void ospf_tx_hook(sock * sk);
1003 void ospf_err_hook(sock * sk, int err);
1004 void ospf_verr_hook(sock *sk, int err);
1005 void ospf_send_to(struct ospf_iface *ifa, ip_addr ip);
1006 void ospf_send_to_agt(struct ospf_iface *ifa, u8 state);
1007 void ospf_send_to_bdr(struct ospf_iface *ifa);
1008
1009 static inline uint ospf_pkt_maxsize(struct ospf_iface *ifa)
1010 { return ifa->tx_length - ifa->tx_hdrlen; }
1011
1012 static inline void ospf_send_to_all(struct ospf_iface *ifa)
1013 { ospf_send_to(ifa, ifa->all_routers); }
1014
1015 static inline void ospf_send_to_des(struct ospf_iface *ifa)
1016 {
1017 if (ipa_nonzero(ifa->des_routers))
1018 ospf_send_to(ifa, ifa->des_routers);
1019 else
1020 ospf_send_to_bdr(ifa);
1021 }
1022
1023 #ifndef PARSER
1024 #define DROP(DSC,VAL) do { err_dsc = DSC; err_val = VAL; goto drop; } while(0)
1025 #define DROP1(DSC) do { err_dsc = DSC; goto drop; } while(0)
1026 #define SKIP(DSC) do { err_dsc = DSC; goto skip; } while(0)
1027 #endif
1028
1029 static inline uint ospf_pkt_hdrlen(struct ospf_proto *p)
1030 { return ospf_is_v2(p) ? (sizeof(struct ospf_packet) + sizeof(union ospf_auth2)) : sizeof(struct ospf_packet); }
1031
1032 static inline void * ospf_tx_buffer(struct ospf_iface *ifa)
1033 { return ifa->sk->tbuf; }
1034
1035 /* hello.c */
1036 #define OHS_HELLO 0
1037 #define OHS_POLL 1
1038 #define OHS_SHUTDOWN 2
1039
1040 void ospf_send_hello(struct ospf_iface *ifa, int kind, struct ospf_neighbor *dirn);
1041 void ospf_receive_hello(struct ospf_packet *pkt, struct ospf_iface *ifa, struct ospf_neighbor *n, ip_addr faddr);
1042 uint ospf_hello3_options(struct ospf_packet *pkt);
1043
1044 /* dbdes.c */
1045 void ospf_send_dbdes(struct ospf_proto *p, struct ospf_neighbor *n);
1046 void ospf_rxmt_dbdes(struct ospf_proto *p, struct ospf_neighbor *n);
1047 void ospf_reset_ldd(struct ospf_proto *p, struct ospf_neighbor *n);
1048 void ospf_receive_dbdes(struct ospf_packet *pkt, struct ospf_iface *ifa, struct ospf_neighbor *n);
1049 uint ospf_dbdes3_options(struct ospf_packet *pkt);
1050
1051 /* lsreq.c */
1052 void ospf_send_lsreq(struct ospf_proto *p, struct ospf_neighbor *n);
1053 void ospf_receive_lsreq(struct ospf_packet *pkt, struct ospf_iface *ifa, struct ospf_neighbor *n);
1054
1055 /* lsupd.c */
1056 void ospf_dump_lsahdr(struct ospf_proto *p, struct ospf_lsa_header *lsa_n);
1057 void ospf_dump_common(struct ospf_proto *p, struct ospf_packet *pkt);
1058 void ospf_lsa_lsrt_down_(struct top_hash_entry *en, struct ospf_neighbor *n, struct top_hash_entry *ret);
1059 void ospf_add_flushed_to_lsrt(struct ospf_proto *p, struct ospf_neighbor *n);
1060 void ospf_flood_event(void *ptr);
1061 int ospf_flood_lsa(struct ospf_proto *p, struct top_hash_entry *en, struct ospf_neighbor *from);
1062 int ospf_send_lsupd(struct ospf_proto *p, struct top_hash_entry **lsa_list, uint lsa_count, struct ospf_neighbor *n);
1063 void ospf_rxmt_lsupd(struct ospf_proto *p, struct ospf_neighbor *n);
1064 void ospf_receive_lsupd(struct ospf_packet *pkt, struct ospf_iface *ifa, struct ospf_neighbor *n);
1065
1066 /* lsack.c */
1067 void ospf_enqueue_lsack(struct ospf_neighbor *n, struct ospf_lsa_header *h_n, int queue);
1068 void ospf_reset_lsack_queue(struct ospf_neighbor *n);
1069 void ospf_send_lsack(struct ospf_proto *p, struct ospf_neighbor *n, int queue);
1070 void ospf_receive_lsack(struct ospf_packet *pkt, struct ospf_iface *ifa, struct ospf_neighbor *n);
1071
1072
1073 #include "proto/ospf/rt.h"
1074 #include "proto/ospf/topology.h"
1075 #include "proto/ospf/lsalib.h"
1076
1077 #endif /* _BIRD_OSPF_H_ */