summaryrefslogtreecommitdiffstats
path: root/zebra/rib.h
blob: a05bd8c1960ca94ea43e3f799bc131df9d496205 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
 * Routing Information Base header
 * Copyright (C) 1997 Kunihiro Ishiguro
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * GNU Zebra is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Zebra; see the file COPYING.  If not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.  
 */

#ifndef _ZEBRA_RIB_H
#define _ZEBRA_RIB_H

#include "prefix.h"
#include "table.h"
#include "queue.h"
#include "nexthop.h"

#define DISTANCE_INFINITY  255
#define ZEBRA_KERNEL_TABLE_MAX 252 /* support for no more than this rt tables */

struct rib
{
  /* Link list. */
  struct rib *next;
  struct rib *prev;
  
  /* Nexthop structure */
  struct nexthop *nexthop;
  
  /* Refrence count. */
  unsigned long refcnt;
  
  /* Uptime. */
  time_t uptime;

  /* Type fo this route. */
  int type;

  /* Source protocol instance */
  u_short instance;

  /* Which routing table */
  uint32_t table;

  /* Metric */
  u_int32_t metric;

  /* Distance. */
  u_char distance;

  /* Tag */
  u_short tag;

  /* Flags of this route.
   * This flag's definition is in lib/zebra.h ZEBRA_FLAG_* and is exposed
   * to clients via Zserv
   */
  u_char flags;

  /* RIB internal status */
  u_char status;
#define RIB_ENTRY_REMOVED	   0x1
  /* to simplify NHT logic when NHs change, instead of doing a NH by NH cmp */
#define RIB_ENTRY_NEXTHOPS_CHANGED 0x2

  /* Nexthop information. */
  u_char nexthop_num;
  u_char nexthop_active_num;
  u_char nexthop_fib_num;
};

/* meta-queue structure:
 * sub-queue 0: connected, kernel
 * sub-queue 1: static
 * sub-queue 2: RIP, RIPng, OSPF, OSPF6, IS-IS
 * sub-queue 3: iBGP, eBGP
 * sub-queue 4: any other origin (if any)
 */
#define MQ_SIZE 5
struct meta_queue
{
  struct list *subq[MQ_SIZE];
  u_int32_t size; /* sum of lengths of all subqueues */
};

/*
 * Structure that represents a single destination (prefix).
 */
typedef struct rib_dest_t_
{

  /*
   * Back pointer to the route node for this destination. This helps
   * us get to the prefix that this structure is for.
   */
  struct route_node *rnode;

  /*
   * Doubly-linked list of routes for this prefix.
   */
  struct rib *routes;

  /*
   * Flags, see below.
   */
  u_int32_t flags;

  /*
   * Linkage to put dest on the FPM processing queue.
   */
  TAILQ_ENTRY(rib_dest_t_) fpm_q_entries;

} rib_dest_t;

#define RIB_ROUTE_QUEUED(x)	(1 << (x))

/*
 * The maximum qindex that can be used.
 */
#define ZEBRA_MAX_QINDEX        (MQ_SIZE - 1)

/*
 * This flag indicates that a given prefix has been 'advertised' to
 * the FPM to be installed in the forwarding plane.
 */
#define RIB_DEST_SENT_TO_FPM   (1 << (ZEBRA_MAX_QINDEX + 1))

/*
 * This flag is set when we need to send an update to the FPM about a
 * dest.
 */
#define RIB_DEST_UPDATE_FPM    (1 << (ZEBRA_MAX_QINDEX + 2))

/*
 * Macro to iterate over each route for a destination (prefix).
 */
#define RIB_DEST_FOREACH_ROUTE(dest, rib)				\
  for ((rib) = (dest) ? (dest)->routes : NULL; (rib); (rib) = (rib)->next)

/*
 * Same as above, but allows the current node to be unlinked.
 */
#define RIB_DEST_FOREACH_ROUTE_SAFE(dest, rib, next)	\
  for ((rib) = (dest) ? (dest)->routes : NULL;		\
       (rib) && ((next) = (rib)->next, 1);		\
       (rib) = (next))

#define RNODE_FOREACH_RIB(rn, rib)				\
  RIB_DEST_FOREACH_ROUTE (rib_dest_from_rnode (rn), rib)

#define RNODE_FOREACH_RIB_SAFE(rn, rib, next)				\
  RIB_DEST_FOREACH_ROUTE_SAFE (rib_dest_from_rnode (rn), rib, next)

/* Static route information. */
struct static_ipv4
{
  /* For linked list. */
  struct static_ipv4 *prev;
  struct static_ipv4 *next;

  /* Administrative distance. */
  u_char distance;

  /* Tag */
  u_short tag;

  /* Flag for this static route's type. */
  u_char type;
#define STATIC_IPV4_GATEWAY     1
#define STATIC_IPV4_IFNAME      2
#define STATIC_IPV4_BLACKHOLE   3

  /* Nexthop value. */
  union 
  {
    struct in_addr ipv4;
    char *ifname;
  } gate;

  /* bit flags */
  u_char flags;
/*
 see ZEBRA_FLAG_REJECT
     ZEBRA_FLAG_BLACKHOLE
 */
};

#ifdef HAVE_IPV6
/* Static route information. */
struct static_ipv6
{
  /* For linked list. */
  struct static_ipv6 *prev;
  struct static_ipv6 *next;

  /* Administrative distance. */
  u_char distance;

  /* Tag */
  u_short tag;

  /* Flag for this static route's type. */
  u_char type;
#define STATIC_IPV6_GATEWAY          1
#define STATIC_IPV6_GATEWAY_IFNAME   2
#define STATIC_IPV6_IFNAME           3

  /* Nexthop value. */
  struct in6_addr ipv6;
  char *ifname;

  /* bit flags */
  u_char flags;
/*
 see ZEBRA_FLAG_REJECT
     ZEBRA_FLAG_BLACKHOLE
 */
};
#endif /* HAVE_IPV6 */

/* The following for loop allows to iterate over the nexthop
 * structure of routes.
 *
 * We have to maintain quite a bit of state:
 *
 * nexthop:   The pointer to the current nexthop, either in the
 *            top-level chain or in the resolved chain of ni.
 * tnexthop:  The pointer to the current nexthop in the top-level
 *            nexthop chain.
 * recursing: Information if nh currently is in the top-level chain
 *            (0) or in a resolved chain (1).
 *
 * Initialization: Set `nexthop' and `tnexthop' to the head of the
 * top-level chain. As nexthop is in the top level chain, set recursing
 * to 0.
 *
 * Iteration check: Check that the `nexthop' pointer is not NULL.
 *
 * Iteration step: This is the tricky part. Check if `nexthop' has
 * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' is in
 * the top level chain and has at least one nexthop attached to
 * `nexthop->resolved'. As we want to descend into `nexthop->resolved',
 * set `recursing' to 1 and set `nexthop' to `nexthop->resolved'.
 * `tnexthop' is left alone in that case so we can remember which nexthop
 * in the top level chain we are currently handling.
 *
 * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
 * current chain. If we are recursing, `nexthop' will be set to
 * `nexthop->next' and `tnexthop' will be left alone. If we are not
 * recursing, both `tnexthop' and `nexthop' will be set to `nexthop->next'
 * as we are progressing in the top level chain.
 *   If we encounter `nexthop->next == NULL', we will clear the `recursing'
 * flag as we arived either at the end of the resolved chain or at the end
 * of the top level chain. In both cases, we set `tnexthop' and `nexthop'
 * to `tnexthop->next', progressing to the next position in the top-level
 * chain and possibly to its end marked by NULL.
 */
#define ALL_NEXTHOPS_RO(head, nexthop, tnexthop, recursing) \
  (tnexthop) = (nexthop) = (head), (recursing) = 0; \
  (nexthop); \
  (nexthop) = CHECK_FLAG((nexthop)->flags, NEXTHOP_FLAG_RECURSIVE) \
    ? (((recursing) = 1), (nexthop)->resolved) \
    : ((nexthop)->next ? ((recursing) ? (nexthop)->next \
                                      : ((tnexthop) = (nexthop)->next)) \
                       : (((recursing) = 0),((tnexthop) = (tnexthop)->next)))

/* Routing table instance.  */
struct vrf
{
  /* Identifier.  This is same as routing table vector index.  */
  u_int32_t id;

  /* Routing table name.  */
  char *name;

  /* Description.  */
  char *desc;

  /* FIB identifier.  */
  u_char fib_id;

  /* Routing table.  */
  struct route_table *table[AFI_MAX][SAFI_MAX];

  /* Static route configuration.  */
  struct route_table *stable[AFI_MAX][SAFI_MAX];

  /* Recursive Nexthop table */
  struct route_table *rnh_table[AFI_MAX];

  /* Import check table (used mostly by BGP */
  struct route_table *import_check_table[AFI_MAX];

  /* Routing tables off of main table for redistribute table */
  struct route_table *other_table[AFI_MAX][ZEBRA_KERNEL_TABLE_MAX];
};

/*
 * rib_table_info_t
 *
 * Structure that is hung off of a route_table that holds information about
 * the table.
 */
typedef struct rib_table_info_t_
{

  /*
   * Back pointer to vrf.
   */
  struct vrf *vrf;
  afi_t afi;
  safi_t safi;

} rib_table_info_t;

typedef enum
{
  RIB_TABLES_ITER_S_INIT,
  RIB_TABLES_ITER_S_ITERATING,
  RIB_TABLES_ITER_S_DONE
} rib_tables_iter_state_t;

/*
 * Structure that holds state for iterating over all tables in the
 * Routing Information Base.
 */
typedef struct rib_tables_iter_t_
{
  uint32_t vrf_id;
  int afi_safi_ix;

  rib_tables_iter_state_t state;
} rib_tables_iter_t;

extern struct nexthop *nexthop_ifindex_add (struct rib *, unsigned int);
extern struct nexthop *nexthop_ifname_add (struct rib *, char *);
extern struct nexthop *nexthop_blackhole_add (struct rib *);
extern struct nexthop *nexthop_ipv4_add (struct rib *, struct in_addr *,
					 struct in_addr *);
extern struct nexthop * nexthop_ipv4_ifindex_ol_add (struct rib *, const struct in_addr *,
						     const struct in_addr *, const unsigned);
extern struct nexthop *nexthop_ipv4_ifindex_add (struct rib *,
                                                 struct in_addr *,
                                                 struct in_addr *,
                                                 unsigned int);
extern void nexthop_free (struct nexthop *nexthop, struct route_node *);
extern void nexthops_free (struct nexthop *nexthop, struct route_node *);
extern void nexthop_add (struct rib *rib, struct nexthop *nexthop);
extern void copy_nexthops (struct rib *rib, struct nexthop *nh);

extern int nexthop_has_fib_child(struct nexthop *);
extern void rib_lookup_and_dump (struct prefix_ipv4 *);
extern void rib_lookup_and_pushup (struct prefix_ipv4 *);
#define rib_dump(prefix ,rib) _rib_dump(__func__, prefix, rib)
extern void _rib_dump (const char *,
		       union prefix46constptr, const struct rib *);
extern int rib_lookup_ipv4_route (struct prefix_ipv4 *, union sockunion *);
#define ZEBRA_RIB_LOOKUP_ERROR -1
#define ZEBRA_RIB_FOUND_EXACT 0
#define ZEBRA_RIB_FOUND_NOGATE 1
#define ZEBRA_RIB_FOUND_CONNECTED 2
#define ZEBRA_RIB_NOTFOUND 3

#ifdef HAVE_IPV6
extern struct nexthop *nexthop_ipv6_add (struct rib *, struct in6_addr *);
extern struct nexthop *nexthop_ipv6_ifindex_add (struct rib *rib,
		        struct in6_addr *ipv6, unsigned int ifindex);
extern struct nexthop *nexthop_ipv6_ifname_add (struct rib *rib,
						struct in6_addr *ipv6,
						char *ifname);
extern int
rib_bogus_ipv6 (int type, struct prefix_ipv6 *p,
                struct in6_addr *gate, unsigned int ifindex, int table);
#endif /* HAVE_IPV6 */

extern struct vrf *vrf_lookup (u_int32_t);
extern struct route_table *vrf_table (afi_t afi, safi_t safi, u_int32_t id);
extern struct route_table *vrf_static_table (afi_t afi, safi_t safi, u_int32_t id);
extern struct route_table *vrf_other_route_table (afi_t afi, u_int32_t table_id,
						  u_int32_t vrf_id);
extern int is_zebra_valid_kernel_table(u_int32_t table_id);
extern int is_zebra_main_routing_table(u_int32_t table_id);

/* NOTE:
 * All rib_add_ipv[46]* functions will not just add prefix into RIB, but
 * also implicitly withdraw equal prefix of same type. */
extern int rib_add_ipv4 (int type, u_short instance, int flags, struct prefix_ipv4 *p,
			 struct in_addr *gate, struct in_addr *src,
			 unsigned int ifindex, u_int32_t vrf_id,
			 u_int32_t, u_char, safi_t);

extern int rib_add_ipv4_multipath (struct prefix_ipv4 *, struct rib *, safi_t);

extern int rib_delete_ipv4 (int type, u_short instance, int flags, struct prefix_ipv4 *p,
		            struct in_addr *gate, unsigned int ifindex, 
		            u_int32_t, safi_t safi);

extern struct rib *rib_match_ipv4 (struct in_addr);

extern struct rib *rib_lookup_ipv4 (struct prefix_ipv4 *);

extern void rib_update (void);
extern void rib_weed_tables (void);
extern void rib_sweep_route (void);
extern void rib_close (void);
extern void rib_init (void);
extern unsigned long rib_score_proto (u_char proto, u_short instance);
struct zebra_t;
extern void rib_queue_add (struct zebra_t *zebra, struct route_node *rn);


extern int
static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
                 u_char flags, u_short tag, u_char distance, u_int32_t vrf_id);

extern int
static_delete_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
		    u_short tag, u_char distance, u_int32_t vrf_id);

#ifdef HAVE_IPV6
extern int
rib_add_ipv6 (int type, u_short instance, int flags, struct prefix_ipv6 *p,
	      struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id,
	      u_int32_t metric, u_char distance, safi_t safi);

extern int
rib_delete_ipv6 (int type, u_short instance, int flags, struct prefix_ipv6 *p,
		 struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id, safi_t safi);

extern struct rib *rib_lookup_ipv6 (struct in6_addr *);

extern struct rib *rib_match_ipv6 (struct in6_addr *);

extern struct route_table *rib_table_ipv6;

extern int
static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
		 const char *ifname, u_char flags, u_short tag,
                 u_char distance, u_int32_t vrf_id);

extern int
rib_add_ipv6_multipath (struct prefix_ipv6 *, struct rib *, safi_t,
                        unsigned long);

extern int
static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
		    const char *ifname, u_short tag, u_char distance,
                    u_int32_t vrf_id);

#endif /* HAVE_IPV6 */

extern int rib_gc_dest (struct route_node *rn);
extern struct route_table *rib_tables_iter_next (rib_tables_iter_t *iter);

/*
 * Inline functions.
 */

/*
 * rib_table_info
 */
static inline rib_table_info_t *
rib_table_info (struct route_table *table)
{
  return (rib_table_info_t *) table->info;
}

/*
 * rib_dest_from_rnode
 */
static inline rib_dest_t *
rib_dest_from_rnode (struct route_node *rn)
{
  return (rib_dest_t *) rn->info;
}

/*
 * rnode_to_ribs
 *
 * Returns a pointer to the list of routes corresponding to the given
 * route_node.
 */
static inline struct rib *
rnode_to_ribs (struct route_node *rn)
{
  rib_dest_t *dest;

  dest = rib_dest_from_rnode (rn);
  if (!dest)
    return NULL;

  return dest->routes;
}

/*
 * rib_dest_prefix
 */
static inline struct prefix *
rib_dest_prefix (rib_dest_t *dest)
{
  return &dest->rnode->p;
}

/*
 * rib_dest_af
 *
 * Returns the address family that the destination is for.
 */
static inline u_char
rib_dest_af (rib_dest_t *dest)
{
  return dest->rnode->p.family;
}

/*
 * rib_dest_table
 */
static inline struct route_table *
rib_dest_table (rib_dest_t *dest)
{
  return dest->rnode->table;
}

/*
 * rib_dest_vrf
 */
static inline struct vrf *
rib_dest_vrf (rib_dest_t *dest)
{
  return rib_table_info (rib_dest_table (dest))->vrf;
}

/*
 * rib_tables_iter_init
 */
static inline void
rib_tables_iter_init (rib_tables_iter_t *iter)

{
  memset (iter, 0, sizeof (*iter));
  iter->state = RIB_TABLES_ITER_S_INIT;
}

/*
 * rib_tables_iter_started
 *
 * Returns TRUE if this iterator has started iterating over the set of
 * tables.
 */
static inline int
rib_tables_iter_started (rib_tables_iter_t *iter)
{
  return iter->state != RIB_TABLES_ITER_S_INIT;
}

/*
 * rib_tables_iter_cleanup
 */
static inline void
rib_tables_iter_cleanup (rib_tables_iter_t *iter)
{
  iter->state = RIB_TABLES_ITER_S_DONE;
}

#endif /*_ZEBRA_RIB_H */