1 /*
2  *	BIRD Internet Routing Daemon -- Protocols
3  *
4  *	(c) 1998--2000 Martin Mares <mj@ucw.cz>
5  *
6  *	Can be freely distributed and used under the terms of the GNU GPL.
7  */
8 
9 #ifndef _BIRD_PROTOCOL_H_
10 #define _BIRD_PROTOCOL_H_
11 
12 #include "lib/lists.h"
13 #include "lib/resource.h"
14 #include "lib/event.h"
15 #include "nest/route.h"
16 #include "conf/conf.h"
17 
18 struct iface;
19 struct ifa;
20 struct rtable;
21 struct rte;
22 struct neighbor;
23 struct rta;
24 struct network;
25 struct proto_config;
26 struct channel_limit;
27 struct channel_config;
28 struct config;
29 struct proto;
30 struct channel;
31 struct ea_list;
32 struct eattr;
33 struct symbol;
34 
35 
36 /*
37  *	Routing Protocol
38  */
39 
40 enum protocol_class {
41   PROTOCOL_NONE,
42   PROTOCOL_BABEL,
43   PROTOCOL_BFD,
44   PROTOCOL_BGP,
45   PROTOCOL_DEVICE,
46   PROTOCOL_DIRECT,
47   PROTOCOL_KERNEL,
48   PROTOCOL_OSPF,
49   PROTOCOL_MRT,
50   PROTOCOL_PERF,
51   PROTOCOL_PIPE,
52   PROTOCOL_RADV,
53   PROTOCOL_RIP,
54   PROTOCOL_RPKI,
55   PROTOCOL_STATIC,
56   PROTOCOL__MAX
57 };
58 
59 extern struct protocol *class_to_protocol[PROTOCOL__MAX];
60 
61 struct protocol {
62   node n;
63   char *name;
64   char *template;			/* Template for automatic generation of names */
65   int name_counter;			/* Counter for automatic name generation */
66   enum protocol_class class;		/* Machine readable protocol class */
67   uint preference;			/* Default protocol preference */
68   uint channel_mask;			/* Mask of accepted channel types (NB_*) */
69   uint proto_size;			/* Size of protocol data structure */
70   uint config_size;			/* Size of protocol config data structure */
71 
72   void (*preconfig)(struct protocol *, struct config *);	/* Just before configuring */
73   void (*postconfig)(struct proto_config *);			/* After configuring each instance */
74   struct proto * (*init)(struct proto_config *);		/* Create new instance */
75   int (*reconfigure)(struct proto *, struct proto_config *);	/* Try to reconfigure instance, returns success */
76   void (*dump)(struct proto *);			/* Debugging dump */
77   void (*dump_attrs)(struct rte *);		/* Dump protocol-dependent attributes */
78   int (*start)(struct proto *);			/* Start the instance */
79   int (*shutdown)(struct proto *);		/* Stop the instance */
80   void (*cleanup)(struct proto *);		/* Called after shutdown when protocol became hungry/down */
81   void (*get_status)(struct proto *, byte *buf); /* Get instance status (for `show protocols' command) */
82   void (*get_route_info)(struct rte *, byte *buf); /* Get route information (for `show route' command) */
83   int (*get_attr)(const struct eattr *, byte *buf, int buflen);	/* ASCIIfy dynamic attribute (returns GA_*) */
84   void (*show_proto_info)(struct proto *);	/* Show protocol info (for `show protocols all' command) */
85   void (*copy_config)(struct proto_config *, struct proto_config *);	/* Copy config from given protocol instance */
86 };
87 
88 void protos_build(void);
89 void proto_build(struct protocol *);
90 void protos_preconfig(struct config *);
91 void protos_commit(struct config *new, struct config *old, int force_restart, int type);
92 struct proto * proto_spawn(struct proto_config *cf, uint disabled);
93 void protos_dump_all(void);
94 
95 #define GA_UNKNOWN	0		/* Attribute not recognized */
96 #define GA_NAME		1		/* Result = name */
97 #define GA_FULL		2		/* Result = both name and value */
98 
99 /*
100  *	Known protocols
101  */
102 
103 extern struct protocol
104   proto_device, proto_radv, proto_rip, proto_static, proto_mrt,
105   proto_ospf, proto_perf,
106   proto_pipe, proto_bgp, proto_bfd, proto_babel, proto_rpki;
107 
108 /*
109  *	Routing Protocol Instance
110  */
111 
112 struct proto_config {
113   node n;
114   struct config *global;		/* Global configuration data */
115   struct protocol *protocol;		/* Protocol */
116   struct proto *proto;			/* Instance we've created */
117   struct proto_config *parent;		/* Parent proto_config for dynamic protocols */
118   const char *name;
119   const char *dsc;
120   int class;				/* SYM_PROTO or SYM_TEMPLATE */
121   u8 net_type;				/* Protocol network type (NET_*), 0 for undefined */
122   u8 disabled;				/* Protocol enabled/disabled by default */
123   u8 vrf_set;				/* Related VRF instance (below) is defined */
124   u32 debug, mrtdump;			/* Debugging bitfields, both use D_* constants */
125   u32 router_id;			/* Protocol specific router ID */
126 
127   list channels;			/* List of channel configs (struct channel_config) */
128   struct iface *vrf;			/* Related VRF instance, NULL if global */
129 
130   /* Check proto_reconfigure() and proto_copy_config() after changing struct proto_config */
131 
132   /* Protocol-specific data follow... */
133 };
134 
135 /* Protocol statistics */
136 struct proto_stats {
137   /* Import - from protocol to core */
138   u32 imp_routes;		/* Number of routes successfully imported to the (adjacent) routing table */
139   u32 filt_routes;		/* Number of routes rejected in import filter but kept in the routing table */
140   u32 pref_routes;		/* Number of routes selected as best in the (adjacent) routing table */
141   u32 imp_updates_received;	/* Number of route updates received */
142   u32 imp_updates_invalid;	/* Number of route updates rejected as invalid */
143   u32 imp_updates_filtered;	/* Number of route updates rejected by filters */
144   u32 imp_updates_ignored;	/* Number of route updates rejected as already in route table */
145   u32 imp_updates_accepted;	/* Number of route updates accepted and imported */
146   u32 imp_withdraws_received;	/* Number of route withdraws received */
147   u32 imp_withdraws_invalid;	/* Number of route withdraws rejected as invalid */
148   u32 imp_withdraws_ignored;	/* Number of route withdraws rejected as already not in route table */
149   u32 imp_withdraws_accepted;	/* Number of route withdraws accepted and processed */
150 
151   /* Export - from core to protocol */
152   u32 exp_routes;		/* Number of routes successfully exported to the protocol */
153   u32 exp_updates_received;	/* Number of route updates received */
154   u32 exp_updates_rejected;	/* Number of route updates rejected by protocol */
155   u32 exp_updates_filtered;	/* Number of route updates rejected by filters */
156   u32 exp_updates_accepted;	/* Number of route updates accepted and exported */
157   u32 exp_withdraws_received;	/* Number of route withdraws received */
158   u32 exp_withdraws_accepted;	/* Number of route withdraws accepted and processed */
159 };
160 
161 struct proto {
162   node n;				/* Node in global proto_list */
163   struct protocol *proto;		/* Protocol */
164   struct proto_config *cf;		/* Configuration data */
165   struct proto_config *cf_new;		/* Configuration we want to switch to after shutdown (NULL=delete) */
166   pool *pool;				/* Pool containing local objects */
167   event *event;				/* Protocol event */
168 
169   list channels;			/* List of channels to rtables (struct channel) */
170   struct channel *main_channel;		/* Primary channel */
171   struct rte_src *main_source;		/* Primary route source */
172   struct iface *vrf;			/* Related VRF instance, NULL if global */
173 
174   const char *name;				/* Name of this instance (== cf->name) */
175   u32 debug;				/* Debugging flags */
176   u32 mrtdump;				/* MRTDump flags */
177   uint active_channels;			/* Number of active channels */
178   byte net_type;			/* Protocol network type (NET_*), 0 for undefined */
179   byte disabled;			/* Manually disabled */
180   byte vrf_set;				/* Related VRF instance (above) is defined */
181   byte proto_state;			/* Protocol state machine (PS_*, see below) */
182   byte active;				/* From PS_START to cleanup after PS_STOP */
183   byte do_start;			/* Start actions are scheduled */
184   byte do_stop;				/* Stop actions are scheduled */
185   byte reconfiguring;			/* We're shutting down due to reconfiguration */
186   byte gr_recovery;			/* Protocol should participate in graceful restart recovery */
187   byte down_sched;			/* Shutdown is scheduled for later (PDS_*) */
188   byte down_code;			/* Reason for shutdown (PDC_* codes) */
189   u32 hash_key;				/* Random key used for hashing of neighbors */
190   btime last_state_change;		/* Time of last state transition */
191   char *last_state_name_announced;	/* Last state name we've announced to the user */
192   char *message;			/* State-change message, allocated from proto_pool */
193 
194   /*
195    *	General protocol hooks:
196    *
197    *	   if_notify	Notify protocol about interface state changes.
198    *	   ifa_notify	Notify protocol about interface address changes.
199    *	   rt_notify	Notify protocol about routing table updates.
200    *	   neigh_notify	Notify protocol about neighbor cache events.
201    *	   make_tmp_attrs  Add attributes to rta from from private attrs stored in rte. The route and rta MUST NOT be cached.
202    *	   store_tmp_attrs Store private attrs back to rte and undef added attributes. The route and rta MUST NOT be cached.
203    *	   preexport  Called as the first step of the route exporting process.
204    *			It can construct a new rte, add private attributes and
205    *			decide whether the route shall be exported: 1=yes, -1=no,
206    *			0=process it through the export filter set by the user.
207    *	   reload_routes   Request channel to reload all its routes to the core
208    *			(using rte_update()). Returns: 0=reload cannot be done,
209    *			1= reload is scheduled and will happen (asynchronously).
210    *	   feed_begin	Notify channel about beginning of route feeding.
211    *	   feed_end	Notify channel about finish of route feeding.
212    */
213 
214   void (*if_notify)(struct proto *, unsigned flags, struct iface *i);
215   void (*ifa_notify)(struct proto *, unsigned flags, struct ifa *a);
216   void (*rt_notify)(struct proto *, struct channel *, struct network *net, struct rte *new, struct rte *old);
217   void (*neigh_notify)(struct neighbor *neigh);
218   void (*make_tmp_attrs)(struct rte *rt, struct linpool *pool);
219   void (*store_tmp_attrs)(struct rte *rt, struct linpool *pool);
220   int (*preexport)(struct proto *, struct rte **rt, struct linpool *pool);
221   void (*reload_routes)(struct channel *);
222   void (*feed_begin)(struct channel *, int initial);
223   void (*feed_end)(struct channel *);
224 
225   /*
226    *	Routing entry hooks (called only for routes belonging to this protocol):
227    *
228    *	   rte_recalculate Called at the beginning of the best route selection
229    *	   rte_better	Compare two rte's and decide which one is better (1=first, 0=second).
230    *       rte_same	Compare two rte's and decide whether they are identical (1=yes, 0=no).
231    *       rte_mergable	Compare two rte's and decide whether they could be merged (1=yes, 0=no).
232    *	   rte_insert	Called whenever a rte is inserted to a routing table.
233    *	   rte_remove	Called whenever a rte is removed from the routing table.
234    */
235 
236   int (*rte_recalculate)(struct rtable *, struct network *, struct rte *, struct rte *, struct rte *);
237   int (*rte_better)(struct rte *, struct rte *);
238   int (*rte_same)(struct rte *, struct rte *);
239   int (*rte_mergable)(struct rte *, struct rte *);
240   struct rte * (*rte_modify)(struct rte *, struct linpool *);
241   void (*rte_insert)(struct network *, struct rte *);
242   void (*rte_remove)(struct network *, struct rte *);
243 
244   /* Hic sunt protocol-specific data */
245 };
246 
247 struct proto_spec {
248   const void *ptr;
249   int patt;
250 };
251 
252 
253 #define PDS_DISABLE		1	/* Proto disable scheduled */
254 #define PDS_RESTART		2	/* Proto restart scheduled */
255 
256 #define PDC_CF_REMOVE		0x01	/* Removed in new config */
257 #define PDC_CF_DISABLE		0x02	/* Disabled in new config */
258 #define PDC_CF_RESTART		0x03	/* Restart due to reconfiguration */
259 #define PDC_CMD_DISABLE		0x11	/* Result of disable command */
260 #define PDC_CMD_RESTART		0x12	/* Result of restart command */
261 #define PDC_CMD_SHUTDOWN	0x13	/* Result of global shutdown */
262 #define PDC_CMD_GR_DOWN		0x14	/* Result of global graceful restart */
263 #define PDC_RX_LIMIT_HIT	0x21	/* Route receive limit reached */
264 #define PDC_IN_LIMIT_HIT	0x22	/* Route import limit reached */
265 #define PDC_OUT_LIMIT_HIT	0x23	/* Route export limit reached */
266 
267 
268 void *proto_new(struct proto_config *);
269 void *proto_config_new(struct protocol *, int class);
270 void proto_copy_config(struct proto_config *dest, struct proto_config *src);
271 void proto_clone_config(struct symbol *sym, struct proto_config *parent);
272 void proto_set_message(struct proto *p, char *msg, int len);
273 
274 void graceful_restart_recovery(void);
275 void graceful_restart_init(void);
276 void graceful_restart_show_status(void);
277 void channel_graceful_restart_lock(struct channel *c);
278 void channel_graceful_restart_unlock(struct channel *c);
279 
280 #define DEFAULT_GR_WAIT	240
281 
282 void channel_show_limit(struct channel_limit *l, const char *dsc);
283 void channel_show_info(struct channel *c);
284 void channel_cmd_debug(struct channel *c, uint mask);
285 
286 void proto_cmd_show(struct proto *, uintptr_t, int);
287 void proto_cmd_disable(struct proto *, uintptr_t, int);
288 void proto_cmd_enable(struct proto *, uintptr_t, int);
289 void proto_cmd_restart(struct proto *, uintptr_t, int);
290 void proto_cmd_reload(struct proto *, uintptr_t, int);
291 void proto_cmd_debug(struct proto *, uintptr_t, int);
292 void proto_cmd_mrtdump(struct proto *, uintptr_t, int);
293 
294 void proto_apply_cmd(struct proto_spec ps, void (* cmd)(struct proto *, uintptr_t, int), int restricted, uintptr_t arg);
295 struct proto *proto_get_named(struct symbol *, struct protocol *);
296 struct proto *proto_iterate_named(struct symbol *sym, struct protocol *proto, struct proto *old);
297 
298 #define PROTO_WALK_CMD(sym,pr,p) for(struct proto *p = NULL; p = proto_iterate_named(sym, pr, p); )
299 
300 
301 #define CMD_RELOAD	0
302 #define CMD_RELOAD_IN	1
303 #define CMD_RELOAD_OUT	2
304 
305 static inline u32
proto_get_router_id(struct proto_config * pc)306 proto_get_router_id(struct proto_config *pc)
307 {
308   return pc->router_id ? pc->router_id : pc->global->router_id;
309 }
310 
311 
312 extern pool *proto_pool;
313 extern list proto_list;
314 
315 /*
316  *  Each protocol instance runs two different state machines:
317  *
318  *  [P] The protocol machine: (implemented inside protocol)
319  *
320  *		DOWN    ---->    START
321  *		  ^		   |
322  *		  |		   V
323  *		STOP    <----     UP
324  *
325  *	States:	DOWN	Protocol is down and it's waiting for the core
326  *			requesting protocol start.
327  *		START	Protocol is waiting for connection with the rest
328  *			of the network and it's not willing to accept
329  *			packets. When it connects, it goes to UP state.
330  *		UP	Protocol is up and running. When the network
331  *			connection breaks down or the core requests
332  *			protocol to be terminated, it goes to STOP state.
333  *		STOP	Protocol is disconnecting from the network.
334  *			After it disconnects, it returns to DOWN state.
335  *
336  *	In:	start()	Called in DOWN state to request protocol startup.
337  *			Returns new state: either UP or START (in this
338  *			case, the protocol will notify the core when it
339  *			finally comes UP).
340  *		stop()	Called in START, UP or STOP state to request
341  *			protocol shutdown. Returns new state: either
342  *			DOWN or STOP (in this case, the protocol will
343  *			notify the core when it finally comes DOWN).
344  *
345  *	Out:	proto_notify_state() -- called by protocol instance when
346  *			it does any state transition not covered by
347  *			return values of start() and stop(). This includes
348  *			START->UP (delayed protocol startup), UP->STOP
349  *			(spontaneous shutdown) and STOP->DOWN (delayed
350  *			shutdown).
351  */
352 
353 #define PS_DOWN 0
354 #define PS_START 1
355 #define PS_UP 2
356 #define PS_STOP 3
357 
358 void proto_notify_state(struct proto *p, unsigned state);
359 
360 /*
361  *  [F] The feeder machine: (implemented in core routines)
362  *
363  *		HUNGRY    ---->   FEEDING
364  *		 ^		     |
365  *		 |		     V
366  *		FLUSHING  <----   HAPPY
367  *
368  *	States:	HUNGRY	Protocol either administratively down (i.e.,
369  *			disabled by the user) or temporarily down
370  *			(i.e., [P] is not UP)
371  *		FEEDING	The protocol came up and we're feeding it
372  *			initial routes. [P] is UP.
373  *		HAPPY	The protocol is up and it's receiving normal
374  *			routing updates. [P] is UP.
375  *		FLUSHING The protocol is down and we're removing its
376  *			routes from the table. [P] is STOP or DOWN.
377  *
378  *	Normal lifecycle of a protocol looks like:
379  *
380  *		HUNGRY/DOWN --> HUNGRY/START --> HUNGRY/UP -->
381  *		FEEDING/UP --> HAPPY/UP --> FLUSHING/STOP|DOWN -->
382  *		HUNGRY/STOP|DOWN --> HUNGRY/DOWN
383  *
384  *	Sometimes, protocol might switch from HAPPY/UP to FEEDING/UP
385  *	if it wants to refeed the routes (for example BGP does so
386  *	as a result of received ROUTE-REFRESH request).
387  */
388 
389 
390 
391 /*
392  *	Debugging flags
393  */
394 
395 #define D_STATES 1		/* [core] State transitions */
396 #define D_ROUTES 2		/* [core] Routes passed by the filters */
397 #define D_FILTERS 4		/* [core] Routes rejected by the filters */
398 #define D_IFACES 8		/* [core] Interface events */
399 #define D_EVENTS 16		/* Protocol events */
400 #define D_PACKETS 32		/* Packets sent/received */
401 
402 #ifndef PARSER
403 #define TRACE(flags, msg, args...) \
404   do { if (p->p.debug & flags) log(L_TRACE "%s: " msg, p->p.name , ## args ); } while(0)
405 #endif
406 
407 
408 /*
409  *	MRTDump flags
410  */
411 
412 #define MD_STATES	1		/* Protocol state changes (BGP4MP_MESSAGE_AS4) */
413 #define MD_MESSAGES	2		/* Protocol packets (BGP4MP_MESSAGE_AS4) */
414 
415 /*
416  *	Known unique protocol instances as referenced by config routines
417  */
418 
419 extern struct proto_config *cf_dev_proto;
420 
421 
422 /*
423  * Protocol limits
424  */
425 
426 #define PLD_RX		0	/* Receive limit */
427 #define PLD_IN		1	/* Import limit */
428 #define PLD_OUT		2	/* Export limit */
429 #define PLD_MAX		3
430 
431 #define PLA_NONE	0	/* No limit */
432 #define PLA_WARN	1	/* Issue log warning */
433 #define PLA_BLOCK	2	/* Block new routes */
434 #define PLA_RESTART	4	/* Force protocol restart */
435 #define PLA_DISABLE	5	/* Shutdown and disable protocol */
436 
437 #define PLS_INITIAL	0	/* Initial limit state after protocol start */
438 #define PLS_ACTIVE	1	/* Limit was hit */
439 #define PLS_BLOCKED	2	/* Limit is active and blocking new routes */
440 
441 struct channel_limit {
442   u32 limit;			/* Maximum number of prefixes */
443   u8 action;			/* Action to take (PLA_*) */
444   u8 state;			/* State of limit (PLS_*) */
445 };
446 
447 void channel_notify_limit(struct channel *c, struct channel_limit *l, int dir, u32 rt_count);
448 
449 
450 /*
451  *	Channels
452  */
453 
454 struct channel_class {
455   uint channel_size;			/* Size of channel data structure */
456   uint config_size;			/* Size of channel config data structure */
457 
458   void (*init)(struct channel *, struct channel_config *);	/* Create new instance */
459   int (*reconfigure)(struct channel *, struct channel_config *, int *import_changed, int *export_changed);	/* Try to reconfigure instance, returns success */
460   int (*start)(struct channel *);	/* Start the instance */
461   void (*shutdown)(struct channel *);	/* Stop the instance */
462   void (*cleanup)(struct channel *);	/* Channel finished flush */
463 
464   void (*copy_config)(struct channel_config *, struct channel_config *); /* Copy config from given channel instance */
465 #if 0
466   XXXX;
467   void (*preconfig)(struct protocol *, struct config *);	/* Just before configuring */
468   void (*postconfig)(struct proto_config *);			/* After configuring each instance */
469 
470 
471   void (*dump)(struct proto *);			/* Debugging dump */
472   void (*dump_attrs)(struct rte *);		/* Dump protocol-dependent attributes */
473 
474   void (*get_status)(struct proto *, byte *buf); /* Get instance status (for `show protocols' command) */
475   void (*get_route_info)(struct rte *, byte *buf); /* Get route information (for `show route' command) */
476   int (*get_attr)(struct eattr *, byte *buf, int buflen);	/* ASCIIfy dynamic attribute (returns GA_*) */
477   void (*show_proto_info)(struct proto *);	/* Show protocol info (for `show protocols all' command) */
478 
479 #endif
480 };
481 
482 extern struct channel_class channel_bgp;
483 
484 struct channel_config {
485   node n;
486   const char *name;
487   const struct channel_class *channel;
488 
489   struct proto_config *parent;		/* Where channel is defined (proto or template) */
490   struct rtable_config *table;		/* Table we're attached to */
491   const struct filter *in_filter, *out_filter; /* Attached filters */
492   struct channel_limit rx_limit;	/* Limit for receiving routes from protocol
493 					   (relevant when in_keep_filtered is active) */
494   struct channel_limit in_limit;	/* Limit for importing routes from protocol */
495   struct channel_limit out_limit;	/* Limit for exporting routes to protocol */
496 
497   u8 net_type;				/* Routing table network type (NET_*), 0 for undefined */
498   u8 ra_mode;				/* Mode of received route advertisements (RA_*) */
499   u16 preference;			/* Default route preference */
500   u32 debug;				/* Debugging flags (D_*) */
501   u8 merge_limit;			/* Maximal number of nexthops for RA_MERGED */
502   u8 in_keep_filtered;			/* Routes rejected in import filter are kept */
503   u8 rpki_reload;			/* RPKI changes trigger channel reload */
504 };
505 
506 struct channel {
507   node n;				/* Node in proto->channels */
508   node table_node;			/* Node in table->channels */
509 
510   const char *name;			/* Channel name (may be NULL) */
511   const struct channel_class *channel;
512   struct proto *proto;
513 
514   struct rtable *table;
515   const struct filter *in_filter;	/* Input filter */
516   const struct filter *out_filter;	/* Output filter */
517   struct bmap export_map;		/* Keeps track which routes passed export filter */
518   struct channel_limit rx_limit;	/* Receive limit (for in_keep_filtered) */
519   struct channel_limit in_limit;	/* Input limit */
520   struct channel_limit out_limit;	/* Output limit */
521 
522   struct event *feed_event;		/* Event responsible for feeding */
523   struct fib_iterator feed_fit;		/* Routing table iterator used during feeding */
524   struct proto_stats stats;		/* Per-channel protocol statistics */
525   u32 refeed_count;			/* Number of routes exported during refeed regardless of out_limit */
526 
527   u8 net_type;				/* Routing table network type (NET_*), 0 for undefined */
528   u8 ra_mode;				/* Mode of received route advertisements (RA_*) */
529   u16 preference;			/* Default route preference */
530   u32 debug;				/* Debugging flags (D_*) */
531   u8 merge_limit;			/* Maximal number of nexthops for RA_MERGED */
532   u8 in_keep_filtered;			/* Routes rejected in import filter are kept */
533   u8 disabled;
534   u8 stale;				/* Used in reconfiguration */
535 
536   u8 channel_state;
537   u8 export_state;			/* Route export state (ES_*, see below) */
538   u8 feed_active;
539   u8 flush_active;
540   u8 refeeding;				/* We are refeeding (valid only if export_state == ES_FEEDING) */
541   u8 reloadable;			/* Hook reload_routes() is allowed on the channel */
542   u8 gr_lock;				/* Graceful restart mechanism should wait for this channel */
543   u8 gr_wait;				/* Route export to channel is postponed until graceful restart */
544 
545   btime last_state_change;		/* Time of last state transition */
546 
547   struct rtable *in_table;		/* Internal table for received routes */
548   struct event *reload_event;		/* Event responsible for reloading from in_table */
549   struct fib_iterator reload_fit;	/* FIB iterator in in_table used during reloading */
550   struct rte *reload_next_rte;		/* Route iterator in in_table used during reloading */
551   u8 reload_active;			/* Iterator reload_fit is linked */
552 
553   u8 reload_pending;			/* Reloading and another reload is scheduled */
554   u8 refeed_pending;			/* Refeeding and another refeed is scheduled */
555   u8 rpki_reload;			/* RPKI changes trigger channel reload */
556 
557   struct rtable *out_table;		/* Internal table for exported routes */
558 
559   list roa_subscriptions;		/* List of active ROA table subscriptions based on filters roa_check() */
560 };
561 
562 
563 /*
564  * Channel states
565  *
566  * CS_DOWN - The initial and the final state of a channel. There is no route
567  * exchange between the protocol and the table. Channel is not counted as
568  * active. Channel keeps a ptr to the table, but do not lock the table and is
569  * not linked in the table. Generally, new closed channels are created in
570  * protocols' init() hooks. The protocol is expected to explicitly activate its
571  * channels (by calling channel_init() or channel_open()).
572  *
573  * CS_START - The channel as a connection between the protocol and the table is
574  * initialized (counted as active by the protocol, linked in the table and keeps
575  * the table locked), but there is no current route exchange. There still may be
576  * routes associated with the channel in the routing table if the channel falls
577  * to CS_START from CS_UP. Generally, channels are initialized in protocols'
578  * start() hooks when going to PS_START.
579  *
580  * CS_UP - The channel is initialized and the route exchange is allowed. Note
581  * that even in CS_UP state, route export may still be down (ES_DOWN) by the
582  * core decision (e.g. waiting for table convergence after graceful restart).
583  * I.e., the protocol decides to open the channel but the core decides to start
584  * route export. Route import (caused by rte_update() from the protocol) is not
585  * restricted by that and is on volition of the protocol. Generally, channels
586  * are opened in protocols' start() hooks when going to PS_UP.
587  *
588  * CS_FLUSHING - The transitional state between initialized channel and closed
589  * channel. The channel is still initialized, but no route exchange is allowed.
590  * Instead, the associated table is running flush loop to remove routes imported
591  * through the channel. After that, the channel changes state to CS_DOWN and
592  * is detached from the table (the table is unlocked and the channel is unlinked
593  * from it). Unlike other states, the CS_FLUSHING state is not explicitly
594  * entered or left by the protocol. A protocol may request to close a channel
595  * (by calling channel_close()), which causes the channel to change state to
596  * CS_FLUSHING and later to CS_DOWN. Also note that channels are closed
597  * automatically by the core when the protocol is going down.
598  *
599  * Allowed transitions:
600  *
601  * CS_DOWN	-> CS_START / CS_UP
602  * CS_START	-> CS_UP / CS_FLUSHING
603  * CS_UP	-> CS_START / CS_FLUSHING
604  * CS_FLUSHING	-> CS_DOWN (automatic)
605  */
606 
607 #define CS_DOWN		0
608 #define CS_START	1
609 #define CS_UP		2
610 #define CS_FLUSHING	3
611 
612 #define ES_DOWN		0
613 #define ES_FEEDING	1
614 #define ES_READY	2
615 
616 
617 struct channel_config *proto_cf_find_channel(struct proto_config *p, uint net_type);
proto_cf_main_channel(struct proto_config * pc)618 static inline struct channel_config *proto_cf_main_channel(struct proto_config *pc)
619 { struct channel_config *cc = HEAD(pc->channels); return NODE_VALID(cc) ? cc : NULL; }
620 
621 struct channel *proto_find_channel_by_table(struct proto *p, struct rtable *t);
622 struct channel *proto_find_channel_by_name(struct proto *p, const char *n);
623 struct channel *proto_add_channel(struct proto *p, struct channel_config *cf);
624 int proto_configure_channel(struct proto *p, struct channel **c, struct channel_config *cf);
625 
626 void channel_set_state(struct channel *c, uint state);
627 void channel_setup_in_table(struct channel *c);
628 void channel_setup_out_table(struct channel *c);
629 void channel_schedule_reload(struct channel *c);
630 
channel_init(struct channel * c)631 static inline void channel_init(struct channel *c) { channel_set_state(c, CS_START); }
channel_open(struct channel * c)632 static inline void channel_open(struct channel *c) { channel_set_state(c, CS_UP); }
channel_close(struct channel * c)633 static inline void channel_close(struct channel *c) { channel_set_state(c, CS_FLUSHING); }
634 
635 void channel_request_feeding(struct channel *c);
636 void *channel_config_new(const struct channel_class *cc, const char *name, uint net_type, struct proto_config *proto);
637 void *channel_config_get(const struct channel_class *cc, const char *name, uint net_type, struct proto_config *proto);
638 int channel_reconfigure(struct channel *c, struct channel_config *cf);
639 
640 
641 /* Moved from route.h to avoid dependency conflicts */
rte_update(struct proto * p,const net_addr * n,rte * new)642 static inline void rte_update(struct proto *p, const net_addr *n, rte *new) { rte_update2(p->main_channel, n, new, p->main_source); }
643 
644 static inline void
rte_update3(struct channel * c,const net_addr * n,rte * new,struct rte_src * src)645 rte_update3(struct channel *c, const net_addr *n, rte *new, struct rte_src *src)
646 {
647   if (c->in_table && !rte_update_in(c, n, new, src))
648     return;
649 
650   rte_update2(c, n, new, src);
651 }
652 
653 
654 #endif
655