1 /*
2  *	BIRD -- 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 #undef LOCAL_DEBUG
10 
11 #include "nest/bird.h"
12 #include "nest/protocol.h"
13 #include "lib/resource.h"
14 #include "lib/lists.h"
15 #include "lib/event.h"
16 #include "lib/timer.h"
17 #include "lib/string.h"
18 #include "conf/conf.h"
19 #include "nest/route.h"
20 #include "nest/iface.h"
21 #include "nest/cli.h"
22 #include "filter/filter.h"
23 #include "filter/f-inst.h"
24 
25 pool *proto_pool;
26 list  proto_list;
27 
28 static list protocol_list;
29 struct protocol *class_to_protocol[PROTOCOL__MAX];
30 
31 #define CD(c, msg, args...) ({ if (c->debug & D_STATES) log(L_TRACE "%s.%s: " msg, c->proto->name, c->name ?: "?", ## args); })
32 #define PD(p, msg, args...) ({ if (p->debug & D_STATES) log(L_TRACE "%s: " msg, p->name, ## args); })
33 
34 static timer *proto_shutdown_timer;
35 static timer *gr_wait_timer;
36 
37 #define GRS_NONE	0
38 #define GRS_INIT	1
39 #define GRS_ACTIVE	2
40 #define GRS_DONE	3
41 
42 static int graceful_restart_state;
43 static u32 graceful_restart_locks;
44 
45 static char *p_states[] = { "DOWN", "START", "UP", "STOP" };
46 static char *c_states[] = { "DOWN", "START", "UP", "FLUSHING" };
47 static char *e_states[] = { "DOWN", "FEEDING", "READY" };
48 
49 extern struct protocol proto_unix_iface;
50 
51 static void channel_request_reload(struct channel *c);
52 static void proto_shutdown_loop(timer *);
53 static void proto_rethink_goal(struct proto *p);
54 static char *proto_state_name(struct proto *p);
55 static void channel_verify_limits(struct channel *c);
56 static inline void channel_reset_limit(struct channel_limit *l);
57 
58 
proto_is_done(struct proto * p)59 static inline int proto_is_done(struct proto *p)
60 { return (p->proto_state == PS_DOWN) && (p->active_channels == 0); }
61 
channel_is_active(struct channel * c)62 static inline int channel_is_active(struct channel *c)
63 { return (c->channel_state == CS_START) || (c->channel_state == CS_UP); }
64 
channel_reloadable(struct channel * c)65 static inline int channel_reloadable(struct channel *c)
66 { return c->proto->reload_routes && c->reloadable; }
67 
68 static inline void
channel_log_state_change(struct channel * c)69 channel_log_state_change(struct channel *c)
70 {
71   if (c->export_state)
72     CD(c, "State changed to %s/%s", c_states[c->channel_state], e_states[c->export_state]);
73   else
74     CD(c, "State changed to %s", c_states[c->channel_state]);
75 }
76 
77 static void
proto_log_state_change(struct proto * p)78 proto_log_state_change(struct proto *p)
79 {
80   if (p->debug & D_STATES)
81   {
82     char *name = proto_state_name(p);
83     if (name != p->last_state_name_announced)
84     {
85       p->last_state_name_announced = name;
86       PD(p, "State changed to %s", proto_state_name(p));
87     }
88   }
89   else
90     p->last_state_name_announced = NULL;
91 }
92 
93 
94 struct channel_config *
proto_cf_find_channel(struct proto_config * pc,uint net_type)95 proto_cf_find_channel(struct proto_config *pc, uint net_type)
96 {
97   struct channel_config *cc;
98 
99   WALK_LIST(cc, pc->channels)
100     if (cc->net_type == net_type)
101       return cc;
102 
103   return NULL;
104 }
105 
106 /**
107  * proto_find_channel_by_table - find channel connected to a routing table
108  * @p: protocol instance
109  * @t: routing table
110  *
111  * Returns pointer to channel or NULL
112  */
113 struct channel *
proto_find_channel_by_table(struct proto * p,struct rtable * t)114 proto_find_channel_by_table(struct proto *p, struct rtable *t)
115 {
116   struct channel *c;
117 
118   WALK_LIST(c, p->channels)
119     if (c->table == t)
120       return c;
121 
122   return NULL;
123 }
124 
125 /**
126  * proto_find_channel_by_name - find channel by its name
127  * @p: protocol instance
128  * @n: channel name
129  *
130  * Returns pointer to channel or NULL
131  */
132 struct channel *
proto_find_channel_by_name(struct proto * p,const char * n)133 proto_find_channel_by_name(struct proto *p, const char *n)
134 {
135   struct channel *c;
136 
137   WALK_LIST(c, p->channels)
138     if (!strcmp(c->name, n))
139       return c;
140 
141   return NULL;
142 }
143 
144 /**
145  * proto_add_channel - connect protocol to a routing table
146  * @p: protocol instance
147  * @cf: channel configuration
148  *
149  * This function creates a channel between the protocol instance @p and the
150  * routing table specified in the configuration @cf, making the protocol hear
151  * all changes in the table and allowing the protocol to update routes in the
152  * table.
153  *
154  * The channel is linked in the protocol channel list and when active also in
155  * the table channel list. Channels are allocated from the global resource pool
156  * (@proto_pool) and they are automatically freed when the protocol is removed.
157  */
158 
159 struct channel *
proto_add_channel(struct proto * p,struct channel_config * cf)160 proto_add_channel(struct proto *p, struct channel_config *cf)
161 {
162   struct channel *c = mb_allocz(proto_pool, cf->channel->channel_size);
163 
164   c->name = cf->name;
165   c->channel = cf->channel;
166   c->proto = p;
167   c->table = cf->table->table;
168 
169   c->in_filter = cf->in_filter;
170   c->out_filter = cf->out_filter;
171   c->rx_limit = cf->rx_limit;
172   c->in_limit = cf->in_limit;
173   c->out_limit = cf->out_limit;
174 
175   c->net_type = cf->net_type;
176   c->ra_mode = cf->ra_mode;
177   c->preference = cf->preference;
178   c->debug = cf->debug;
179   c->merge_limit = cf->merge_limit;
180   c->in_keep_filtered = cf->in_keep_filtered;
181   c->rpki_reload = cf->rpki_reload;
182 
183   c->channel_state = CS_DOWN;
184   c->export_state = ES_DOWN;
185   c->last_state_change = current_time();
186   c->reloadable = 1;
187 
188   init_list(&c->roa_subscriptions);
189 
190   CALL(c->channel->init, c, cf);
191 
192   add_tail(&p->channels, &c->n);
193 
194   CD(c, "Connected to table %s", c->table->name);
195 
196   return c;
197 }
198 
199 void
proto_remove_channel(struct proto * p UNUSED,struct channel * c)200 proto_remove_channel(struct proto *p UNUSED, struct channel *c)
201 {
202   ASSERT(c->channel_state == CS_DOWN);
203 
204   CD(c, "Removed", c->name);
205 
206   rem_node(&c->n);
207   mb_free(c);
208 }
209 
210 
211 static void
proto_start_channels(struct proto * p)212 proto_start_channels(struct proto *p)
213 {
214   struct channel *c;
215   WALK_LIST(c, p->channels)
216     if (!c->disabled)
217       channel_set_state(c, CS_UP);
218 }
219 
220 static void
proto_pause_channels(struct proto * p)221 proto_pause_channels(struct proto *p)
222 {
223   struct channel *c;
224   WALK_LIST(c, p->channels)
225     if (!c->disabled && channel_is_active(c))
226       channel_set_state(c, CS_START);
227 }
228 
229 static void
proto_stop_channels(struct proto * p)230 proto_stop_channels(struct proto *p)
231 {
232   struct channel *c;
233   WALK_LIST(c, p->channels)
234     if (!c->disabled && channel_is_active(c))
235       channel_set_state(c, CS_FLUSHING);
236 }
237 
238 static void
proto_remove_channels(struct proto * p)239 proto_remove_channels(struct proto *p)
240 {
241   struct channel *c;
242   WALK_LIST_FIRST(c, p->channels)
243     proto_remove_channel(p, c);
244 }
245 
246 static void
channel_schedule_feed(struct channel * c,int initial)247 channel_schedule_feed(struct channel *c, int initial)
248 {
249   // DBG("%s: Scheduling meal\n", p->name);
250   ASSERT(c->channel_state == CS_UP);
251 
252   c->export_state = ES_FEEDING;
253   c->refeeding = !initial;
254 
255   ev_schedule_work(c->feed_event);
256 }
257 
258 static void
channel_feed_loop(void * ptr)259 channel_feed_loop(void *ptr)
260 {
261   struct channel *c = ptr;
262 
263   if (c->export_state != ES_FEEDING)
264     return;
265 
266   /* Start feeding */
267   if (!c->feed_active)
268   {
269     if (c->proto->feed_begin)
270       c->proto->feed_begin(c, !c->refeeding);
271 
272     c->refeed_pending = 0;
273   }
274 
275   // DBG("Feeding protocol %s continued\n", p->name);
276   if (!rt_feed_channel(c))
277   {
278     ev_schedule_work(c->feed_event);
279     return;
280   }
281 
282   /* Reset export limit if the feed ended with acceptable number of exported routes */
283   struct channel_limit *l = &c->out_limit;
284   if (c->refeeding &&
285       (l->state == PLS_BLOCKED) &&
286       (c->refeed_count <= l->limit) &&
287       (c->stats.exp_routes <= l->limit))
288   {
289     log(L_INFO "Protocol %s resets route export limit (%u)", c->proto->name, l->limit);
290     channel_reset_limit(&c->out_limit);
291 
292     /* Continue in feed - it will process routing table again from beginning */
293     c->refeed_count = 0;
294     ev_schedule_work(c->feed_event);
295     return;
296   }
297 
298   // DBG("Feeding protocol %s finished\n", p->name);
299   c->export_state = ES_READY;
300   channel_log_state_change(c);
301 
302   if (c->proto->feed_end)
303     c->proto->feed_end(c);
304 
305   /* Restart feeding */
306   if (c->refeed_pending)
307     channel_request_feeding(c);
308 }
309 
310 
311 static void
channel_roa_in_changed(struct rt_subscription * s)312 channel_roa_in_changed(struct rt_subscription *s)
313 {
314   struct channel *c = s->data;
315   int active = c->reload_event && ev_active(c->reload_event);
316 
317   CD(c, "Reload triggered by RPKI change%s", active ? " - already active" : "");
318 
319   if (!active)
320     channel_request_reload(c);
321   else
322     c->reload_pending = 1;
323 }
324 
325 static void
channel_roa_out_changed(struct rt_subscription * s)326 channel_roa_out_changed(struct rt_subscription *s)
327 {
328   struct channel *c = s->data;
329   int active = (c->export_state == ES_FEEDING);
330 
331   CD(c, "Feeding triggered by RPKI change%s", active ? " - already active" : "");
332 
333   if (!active)
334     channel_request_feeding(c);
335   else
336     c->refeed_pending = 1;
337 }
338 
339 /* Temporary code, subscriptions should be changed to resources */
340 struct roa_subscription {
341   struct rt_subscription s;
342   node roa_node;
343 };
344 
345 static int
channel_roa_is_subscribed(struct channel * c,rtable * tab,int dir)346 channel_roa_is_subscribed(struct channel *c, rtable *tab, int dir)
347 {
348   void (*hook)(struct rt_subscription *) =
349     dir ? channel_roa_in_changed : channel_roa_out_changed;
350 
351   struct roa_subscription *s;
352   node *n;
353 
354   WALK_LIST2(s, n, c->roa_subscriptions, roa_node)
355     if ((s->s.tab == tab) && (s->s.hook == hook))
356       return 1;
357 
358   return 0;
359 }
360 
361 
362 static void
channel_roa_subscribe(struct channel * c,rtable * tab,int dir)363 channel_roa_subscribe(struct channel *c, rtable *tab, int dir)
364 {
365   if (channel_roa_is_subscribed(c, tab, dir))
366     return;
367 
368   struct roa_subscription *s = mb_allocz(c->proto->pool, sizeof(struct roa_subscription));
369 
370   s->s.hook = dir ? channel_roa_in_changed : channel_roa_out_changed;
371   s->s.data = c;
372   rt_subscribe(tab, &s->s);
373 
374   add_tail(&c->roa_subscriptions, &s->roa_node);
375 }
376 
377 static void
channel_roa_unsubscribe(struct roa_subscription * s)378 channel_roa_unsubscribe(struct roa_subscription *s)
379 {
380   rt_unsubscribe(&s->s);
381   rem_node(&s->roa_node);
382   mb_free(s);
383 }
384 
385 static void
channel_roa_subscribe_filter(struct channel * c,int dir)386 channel_roa_subscribe_filter(struct channel *c, int dir)
387 {
388   const struct filter *f = dir ? c->in_filter : c->out_filter;
389   struct rtable *tab;
390   int valid = 1, found = 0;
391 
392   if ((f == FILTER_ACCEPT) || (f == FILTER_REJECT))
393     return;
394 
395   /* No automatic reload for non-reloadable channels */
396   if (dir && !channel_reloadable(c))
397     valid = 0;
398 
399 #ifdef CONFIG_BGP
400   /* No automatic reload for BGP channels without in_table / out_table */
401   if (c->channel == &channel_bgp)
402     valid = dir ? !!c->in_table : !!c->out_table;
403 #endif
404 
405   struct filter_iterator fit;
406   FILTER_ITERATE_INIT(&fit, f, c->proto->pool);
407 
408   FILTER_ITERATE(&fit, fi)
409   {
410     switch (fi->fi_code)
411     {
412     case FI_ROA_CHECK_IMPLICIT:
413       tab = fi->i_FI_ROA_CHECK_IMPLICIT.rtc->table;
414       if (valid) channel_roa_subscribe(c, tab, dir);
415       found = 1;
416       break;
417 
418     case FI_ROA_CHECK_EXPLICIT:
419       tab = fi->i_FI_ROA_CHECK_EXPLICIT.rtc->table;
420       if (valid) channel_roa_subscribe(c, tab, dir);
421       found = 1;
422       break;
423 
424     default:
425       break;
426     }
427   }
428   FILTER_ITERATE_END;
429 
430   FILTER_ITERATE_CLEANUP(&fit);
431 
432   if (!valid && found)
433     log(L_WARN "%s.%s: Automatic RPKI reload not active for %s",
434 	c->proto->name, c->name ?: "?", dir ? "import" : "export");
435 }
436 
437 static void
channel_roa_unsubscribe_all(struct channel * c)438 channel_roa_unsubscribe_all(struct channel *c)
439 {
440   struct roa_subscription *s;
441   node *n, *x;
442 
443   WALK_LIST2_DELSAFE(s, n, x, c->roa_subscriptions, roa_node)
444     channel_roa_unsubscribe(s);
445 }
446 
447 static void
channel_start_export(struct channel * c)448 channel_start_export(struct channel *c)
449 {
450   ASSERT(c->channel_state == CS_UP);
451   ASSERT(c->export_state == ES_DOWN);
452 
453   channel_schedule_feed(c, 1);	/* Sets ES_FEEDING */
454 }
455 
456 static void
channel_stop_export(struct channel * c)457 channel_stop_export(struct channel *c)
458 {
459   /* Need to abort feeding */
460   if (c->export_state == ES_FEEDING)
461     rt_feed_channel_abort(c);
462 
463   c->export_state = ES_DOWN;
464   c->stats.exp_routes = 0;
465   bmap_reset(&c->export_map, 1024);
466 }
467 
468 
469 /* Called by protocol for reload from in_table */
470 void
channel_schedule_reload(struct channel * c)471 channel_schedule_reload(struct channel *c)
472 {
473   ASSERT(c->channel_state == CS_UP);
474 
475   rt_reload_channel_abort(c);
476   ev_schedule_work(c->reload_event);
477 }
478 
479 static void
channel_reload_loop(void * ptr)480 channel_reload_loop(void *ptr)
481 {
482   struct channel *c = ptr;
483 
484   /* Start reload */
485   if (!c->reload_active)
486     c->reload_pending = 0;
487 
488   if (!rt_reload_channel(c))
489   {
490     ev_schedule_work(c->reload_event);
491     return;
492   }
493 
494   /* Restart reload */
495   if (c->reload_pending)
496     channel_request_reload(c);
497 }
498 
499 static void
channel_reset_import(struct channel * c)500 channel_reset_import(struct channel *c)
501 {
502   /* Need to abort feeding */
503   ev_postpone(c->reload_event);
504   rt_reload_channel_abort(c);
505 
506   rt_prune_sync(c->in_table, 1);
507 }
508 
509 static void
channel_reset_export(struct channel * c)510 channel_reset_export(struct channel *c)
511 {
512   /* Just free the routes */
513   rt_prune_sync(c->out_table, 1);
514 }
515 
516 /* Called by protocol to activate in_table */
517 void
channel_setup_in_table(struct channel * c)518 channel_setup_in_table(struct channel *c)
519 {
520   struct rtable_config *cf = mb_allocz(c->proto->pool, sizeof(struct rtable_config));
521   cf->name = "import";
522   cf->addr_type = c->net_type;
523 
524   c->in_table = mb_allocz(c->proto->pool, sizeof(struct rtable));
525   rt_setup(c->proto->pool, c->in_table, cf);
526 
527   c->reload_event = ev_new_init(c->proto->pool, channel_reload_loop, c);
528 }
529 
530 /* Called by protocol to activate out_table */
531 void
channel_setup_out_table(struct channel * c)532 channel_setup_out_table(struct channel *c)
533 {
534   struct rtable_config *cf = mb_allocz(c->proto->pool, sizeof(struct rtable_config));
535   cf->name = "export";
536   cf->addr_type = c->net_type;
537 
538   c->out_table = mb_allocz(c->proto->pool, sizeof(struct rtable));
539   rt_setup(c->proto->pool, c->out_table, cf);
540 }
541 
542 
543 static void
channel_do_start(struct channel * c)544 channel_do_start(struct channel *c)
545 {
546   rt_lock_table(c->table);
547   add_tail(&c->table->channels, &c->table_node);
548   c->proto->active_channels++;
549 
550   c->feed_event = ev_new_init(c->proto->pool, channel_feed_loop, c);
551 
552   bmap_init(&c->export_map, c->proto->pool, 1024);
553   memset(&c->stats, 0, sizeof(struct proto_stats));
554 
555   channel_reset_limit(&c->rx_limit);
556   channel_reset_limit(&c->in_limit);
557   channel_reset_limit(&c->out_limit);
558 
559   CALL(c->channel->start, c);
560 }
561 
562 static void
channel_do_up(struct channel * c)563 channel_do_up(struct channel *c)
564 {
565   /* Register RPKI/ROA subscriptions */
566   if (c->rpki_reload)
567   {
568     channel_roa_subscribe_filter(c, 1);
569     channel_roa_subscribe_filter(c, 0);
570   }
571 }
572 
573 static void
channel_do_flush(struct channel * c)574 channel_do_flush(struct channel *c)
575 {
576   rt_schedule_prune(c->table);
577 
578   c->gr_wait = 0;
579   if (c->gr_lock)
580     channel_graceful_restart_unlock(c);
581 
582   CALL(c->channel->shutdown, c);
583 
584   /* This have to be done in here, as channel pool is freed before channel_do_down() */
585   bmap_free(&c->export_map);
586   c->in_table = NULL;
587   c->reload_event = NULL;
588   c->out_table = NULL;
589 
590   channel_roa_unsubscribe_all(c);
591 }
592 
593 static void
channel_do_down(struct channel * c)594 channel_do_down(struct channel *c)
595 {
596   ASSERT(!c->feed_active && !c->reload_active);
597 
598   rem_node(&c->table_node);
599   rt_unlock_table(c->table);
600   c->proto->active_channels--;
601 
602   if ((c->stats.imp_routes + c->stats.filt_routes) != 0)
603     log(L_ERR "%s: Channel %s is down but still has some routes", c->proto->name, c->name);
604 
605   // bmap_free(&c->export_map);
606   memset(&c->stats, 0, sizeof(struct proto_stats));
607 
608   c->in_table = NULL;
609   c->reload_event = NULL;
610   c->out_table = NULL;
611 
612   CALL(c->channel->cleanup, c);
613 
614   /* Schedule protocol shutddown */
615   if (proto_is_done(c->proto))
616     ev_schedule(c->proto->event);
617 }
618 
619 void
channel_set_state(struct channel * c,uint state)620 channel_set_state(struct channel *c, uint state)
621 {
622   uint cs = c->channel_state;
623   uint es = c->export_state;
624 
625   DBG("%s reporting channel %s state transition %s -> %s\n", c->proto->name, c->name, c_states[cs], c_states[state]);
626   if (state == cs)
627     return;
628 
629   c->channel_state = state;
630   c->last_state_change = current_time();
631 
632   switch (state)
633   {
634   case CS_START:
635     ASSERT(cs == CS_DOWN || cs == CS_UP);
636 
637     if (cs == CS_DOWN)
638       channel_do_start(c);
639 
640     if (es != ES_DOWN)
641       channel_stop_export(c);
642 
643     if (c->in_table && (cs == CS_UP))
644       channel_reset_import(c);
645 
646     if (c->out_table && (cs == CS_UP))
647       channel_reset_export(c);
648 
649     break;
650 
651   case CS_UP:
652     ASSERT(cs == CS_DOWN || cs == CS_START);
653 
654     if (cs == CS_DOWN)
655       channel_do_start(c);
656 
657     if (!c->gr_wait && c->proto->rt_notify)
658       channel_start_export(c);
659 
660     channel_do_up(c);
661     break;
662 
663   case CS_FLUSHING:
664     ASSERT(cs == CS_START || cs == CS_UP);
665 
666     if (es != ES_DOWN)
667       channel_stop_export(c);
668 
669     if (c->in_table && (cs == CS_UP))
670       channel_reset_import(c);
671 
672     if (c->out_table && (cs == CS_UP))
673       channel_reset_export(c);
674 
675     channel_do_flush(c);
676     break;
677 
678   case CS_DOWN:
679     ASSERT(cs == CS_FLUSHING);
680 
681     channel_do_down(c);
682     break;
683 
684   default:
685     ASSERT(0);
686   }
687 
688   channel_log_state_change(c);
689 }
690 
691 /**
692  * channel_request_feeding - request feeding routes to the channel
693  * @c: given channel
694  *
695  * Sometimes it is needed to send again all routes to the channel. This is
696  * called feeding and can be requested by this function. This would cause
697  * channel export state transition to ES_FEEDING (during feeding) and when
698  * completed, it will switch back to ES_READY. This function can be called
699  * even when feeding is already running, in that case it is restarted.
700  */
701 void
channel_request_feeding(struct channel * c)702 channel_request_feeding(struct channel *c)
703 {
704   ASSERT(c->channel_state == CS_UP);
705 
706   CD(c, "Feeding requested");
707 
708   /* Do nothing if we are still waiting for feeding */
709   if (c->export_state == ES_DOWN)
710     return;
711 
712   /* If we are already feeding, we want to restart it */
713   if (c->export_state == ES_FEEDING)
714   {
715     /* Unless feeding is in initial state */
716     if (!c->feed_active)
717 	return;
718 
719     rt_feed_channel_abort(c);
720   }
721 
722   /* Track number of exported routes during refeed */
723   c->refeed_count = 0;
724 
725   channel_schedule_feed(c, 0);	/* Sets ES_FEEDING */
726   channel_log_state_change(c);
727 }
728 
729 static void
channel_request_reload(struct channel * c)730 channel_request_reload(struct channel *c)
731 {
732   ASSERT(c->channel_state == CS_UP);
733   ASSERT(channel_reloadable(c));
734 
735   CD(c, "Reload requested");
736 
737   c->proto->reload_routes(c);
738 
739   /*
740    * Should this be done before reload_routes() hook?
741    * Perhaps, but routes are updated asynchronously.
742    */
743   channel_reset_limit(&c->rx_limit);
744   channel_reset_limit(&c->in_limit);
745 }
746 
747 const struct channel_class channel_basic = {
748   .channel_size = sizeof(struct channel),
749   .config_size = sizeof(struct channel_config)
750 };
751 
752 void *
channel_config_new(const struct channel_class * cc,const char * name,uint net_type,struct proto_config * proto)753 channel_config_new(const struct channel_class *cc, const char *name, uint net_type, struct proto_config *proto)
754 {
755   struct channel_config *cf = NULL;
756   struct rtable_config *tab = NULL;
757 
758   if (net_type)
759   {
760     if (!net_val_match(net_type, proto->protocol->channel_mask))
761       cf_error("Unsupported channel type");
762 
763     if (proto->net_type && (net_type != proto->net_type))
764       cf_error("Different channel type");
765 
766     tab = new_config->def_tables[net_type];
767   }
768 
769   if (!cc)
770     cc = &channel_basic;
771 
772   cf = cfg_allocz(cc->config_size);
773   cf->name = name;
774   cf->channel = cc;
775   cf->parent = proto;
776   cf->table = tab;
777   cf->out_filter = FILTER_REJECT;
778 
779   cf->net_type = net_type;
780   cf->ra_mode = RA_OPTIMAL;
781   cf->preference = proto->protocol->preference;
782   cf->debug = new_config->channel_default_debug;
783   cf->rpki_reload = 1;
784 
785   add_tail(&proto->channels, &cf->n);
786 
787   return cf;
788 }
789 
790 void *
channel_config_get(const struct channel_class * cc,const char * name,uint net_type,struct proto_config * proto)791 channel_config_get(const struct channel_class *cc, const char *name, uint net_type, struct proto_config *proto)
792 {
793   struct channel_config *cf;
794 
795   /* We are using name as token, so no strcmp() */
796   WALK_LIST(cf, proto->channels)
797     if (cf->name == name)
798     {
799       /* Allow to redefine channel only if inherited from template */
800       if (cf->parent == proto)
801 	cf_error("Multiple %s channels", name);
802 
803       cf->parent = proto;
804       return cf;
805     }
806 
807   return channel_config_new(cc, name, net_type, proto);
808 }
809 
810 struct channel_config *
channel_copy_config(struct channel_config * src,struct proto_config * proto)811 channel_copy_config(struct channel_config *src, struct proto_config *proto)
812 {
813   struct channel_config *dst = cfg_alloc(src->channel->config_size);
814 
815   memcpy(dst, src, src->channel->config_size);
816   memset(&dst->n, 0, sizeof(node));
817   add_tail(&proto->channels, &dst->n);
818   CALL(src->channel->copy_config, dst, src);
819 
820   return dst;
821 }
822 
823 
824 static int reconfigure_type;  /* Hack to propagate type info to channel_reconfigure() */
825 
826 int
channel_reconfigure(struct channel * c,struct channel_config * cf)827 channel_reconfigure(struct channel *c, struct channel_config *cf)
828 {
829   /* FIXME: better handle these changes, also handle in_keep_filtered */
830   if ((c->table != cf->table->table) || (cf->ra_mode && (c->ra_mode != cf->ra_mode)))
831     return 0;
832 
833   /* Note that filter_same() requires arguments in (new, old) order */
834   int import_changed = !filter_same(cf->in_filter, c->in_filter);
835   int export_changed = !filter_same(cf->out_filter, c->out_filter);
836   int rpki_reload_changed = (cf->rpki_reload != c->rpki_reload);
837 
838   if (c->preference != cf->preference)
839     import_changed = 1;
840 
841   if (c->merge_limit != cf->merge_limit)
842     export_changed = 1;
843 
844   /* Reconfigure channel fields */
845   c->in_filter = cf->in_filter;
846   c->out_filter = cf->out_filter;
847   c->rx_limit = cf->rx_limit;
848   c->in_limit = cf->in_limit;
849   c->out_limit = cf->out_limit;
850 
851   // c->ra_mode = cf->ra_mode;
852   c->merge_limit = cf->merge_limit;
853   c->preference = cf->preference;
854   c->debug = cf->debug;
855   c->in_keep_filtered = cf->in_keep_filtered;
856   c->rpki_reload = cf->rpki_reload;
857 
858   channel_verify_limits(c);
859 
860   /* Execute channel-specific reconfigure hook */
861   if (c->channel->reconfigure && !c->channel->reconfigure(c, cf, &import_changed, &export_changed))
862     return 0;
863 
864   /* If the channel is not open, it has no routes and we cannot reload it anyways */
865   if (c->channel_state != CS_UP)
866     goto done;
867 
868   /* Update RPKI/ROA subscriptions */
869   if (import_changed || export_changed || rpki_reload_changed)
870   {
871     channel_roa_unsubscribe_all(c);
872 
873     if (c->rpki_reload)
874     {
875       channel_roa_subscribe_filter(c, 1);
876       channel_roa_subscribe_filter(c, 0);
877     }
878   }
879 
880   if (reconfigure_type == RECONFIG_SOFT)
881   {
882     if (import_changed)
883       log(L_INFO "Channel %s.%s changed import", c->proto->name, c->name);
884 
885     if (export_changed)
886       log(L_INFO "Channel %s.%s changed export", c->proto->name, c->name);
887 
888     goto done;
889   }
890 
891   /* Route reload may be not supported */
892   if (import_changed && !channel_reloadable(c))
893     return 0;
894 
895   if (import_changed || export_changed)
896     log(L_INFO "Reloading channel %s.%s", c->proto->name, c->name);
897 
898   if (import_changed)
899     channel_request_reload(c);
900 
901   if (export_changed)
902     channel_request_feeding(c);
903 
904 done:
905   CD(c, "Reconfigured");
906   return 1;
907 }
908 
909 
910 int
proto_configure_channel(struct proto * p,struct channel ** pc,struct channel_config * cf)911 proto_configure_channel(struct proto *p, struct channel **pc, struct channel_config *cf)
912 {
913   struct channel *c = *pc;
914 
915   if (!c && cf)
916   {
917     /* We could add the channel, but currently it would just stay in down state
918        until protocol is restarted, so it is better to force restart anyways. */
919     if (p->proto_state != PS_DOWN)
920     {
921       log(L_INFO "Cannot add channel %s.%s", p->name, cf->name);
922       return 0;
923     }
924 
925     *pc = proto_add_channel(p, cf);
926   }
927   else if (c && !cf)
928   {
929     if (c->channel_state != CS_DOWN)
930     {
931       log(L_INFO "Cannot remove channel %s.%s", c->proto->name, c->name);
932       return 0;
933     }
934 
935     proto_remove_channel(p, c);
936     *pc = NULL;
937   }
938   else if (c && cf)
939   {
940     if (!channel_reconfigure(c, cf))
941     {
942       log(L_INFO "Cannot reconfigure channel %s.%s", c->proto->name, c->name);
943       return 0;
944     }
945   }
946 
947   return 1;
948 }
949 
950 
951 static void
proto_event(void * ptr)952 proto_event(void *ptr)
953 {
954   struct proto *p = ptr;
955 
956   if (p->do_start)
957   {
958     if_feed_baby(p);
959     p->do_start = 0;
960   }
961 
962   if (p->do_stop)
963   {
964     if (p->proto == &proto_unix_iface)
965       if_flush_ifaces(p);
966     p->do_stop = 0;
967   }
968 
969   if (proto_is_done(p))
970   {
971     if (p->proto->cleanup)
972       p->proto->cleanup(p);
973 
974     p->active = 0;
975     proto_log_state_change(p);
976     proto_rethink_goal(p);
977   }
978 }
979 
980 
981 /**
982  * proto_new - create a new protocol instance
983  * @c: protocol configuration
984  *
985  * When a new configuration has been read in, the core code starts
986  * initializing all the protocol instances configured by calling their
987  * init() hooks with the corresponding instance configuration. The initialization
988  * code of the protocol is expected to create a new instance according to the
989  * configuration by calling this function and then modifying the default settings
990  * to values wanted by the protocol.
991  */
992 void *
proto_new(struct proto_config * cf)993 proto_new(struct proto_config *cf)
994 {
995   struct proto *p = mb_allocz(proto_pool, cf->protocol->proto_size);
996 
997   p->cf = cf;
998   p->debug = cf->debug;
999   p->mrtdump = cf->mrtdump;
1000   p->name = cf->name;
1001   p->proto = cf->protocol;
1002   p->net_type = cf->net_type;
1003   p->disabled = cf->disabled;
1004   p->hash_key = random_u32();
1005   cf->proto = p;
1006 
1007   init_list(&p->channels);
1008 
1009   return p;
1010 }
1011 
1012 static struct proto *
proto_init(struct proto_config * c,node * n)1013 proto_init(struct proto_config *c, node *n)
1014 {
1015   struct protocol *pr = c->protocol;
1016   struct proto *p = pr->init(c);
1017 
1018   p->proto_state = PS_DOWN;
1019   p->last_state_change = current_time();
1020   p->vrf = c->vrf;
1021   p->vrf_set = c->vrf_set;
1022   insert_node(&p->n, n);
1023 
1024   p->event = ev_new_init(proto_pool, proto_event, p);
1025 
1026   PD(p, "Initializing%s", p->disabled ? " [disabled]" : "");
1027 
1028   return p;
1029 }
1030 
1031 static void
proto_start(struct proto * p)1032 proto_start(struct proto *p)
1033 {
1034   /* Here we cannot use p->cf->name since it won't survive reconfiguration */
1035   p->pool = rp_new(proto_pool, p->proto->name);
1036 
1037   if (graceful_restart_state == GRS_INIT)
1038     p->gr_recovery = 1;
1039 }
1040 
1041 
1042 /**
1043  * proto_config_new - create a new protocol configuration
1044  * @pr: protocol the configuration will belong to
1045  * @class: SYM_PROTO or SYM_TEMPLATE
1046  *
1047  * Whenever the configuration file says that a new instance
1048  * of a routing protocol should be created, the parser calls
1049  * proto_config_new() to create a configuration entry for this
1050  * instance (a structure staring with the &proto_config header
1051  * containing all the generic items followed by protocol-specific
1052  * ones). Also, the configuration entry gets added to the list
1053  * of protocol instances kept in the configuration.
1054  *
1055  * The function is also used to create protocol templates (when class
1056  * SYM_TEMPLATE is specified), the only difference is that templates
1057  * are not added to the list of protocol instances and therefore not
1058  * initialized during protos_commit()).
1059  */
1060 void *
proto_config_new(struct protocol * pr,int class)1061 proto_config_new(struct protocol *pr, int class)
1062 {
1063   struct proto_config *cf = cfg_allocz(pr->config_size);
1064 
1065   if (class == SYM_PROTO)
1066     add_tail(&new_config->protos, &cf->n);
1067 
1068   cf->global = new_config;
1069   cf->protocol = pr;
1070   cf->name = pr->name;
1071   cf->class = class;
1072   cf->debug = new_config->proto_default_debug;
1073   cf->mrtdump = new_config->proto_default_mrtdump;
1074 
1075   init_list(&cf->channels);
1076 
1077   return cf;
1078 }
1079 
1080 
1081 /**
1082  * proto_copy_config - copy a protocol configuration
1083  * @dest: destination protocol configuration
1084  * @src: source protocol configuration
1085  *
1086  * Whenever a new instance of a routing protocol is created from the
1087  * template, proto_copy_config() is called to copy a content of
1088  * the source protocol configuration to the new protocol configuration.
1089  * Name, class and a node in protos list of @dest are kept intact.
1090  * copy_config() protocol hook is used to copy protocol-specific data.
1091  */
1092 void
proto_copy_config(struct proto_config * dest,struct proto_config * src)1093 proto_copy_config(struct proto_config *dest, struct proto_config *src)
1094 {
1095   struct channel_config *cc;
1096   node old_node;
1097   int old_class;
1098   const char *old_name;
1099 
1100   if (dest->protocol != src->protocol)
1101     cf_error("Can't copy configuration from a different protocol type");
1102 
1103   if (dest->protocol->copy_config == NULL)
1104     cf_error("Inheriting configuration for %s is not supported", src->protocol->name);
1105 
1106   DBG("Copying configuration from %s to %s\n", src->name, dest->name);
1107 
1108   /*
1109    * Copy struct proto_config here. Keep original node, class and name.
1110    * protocol-specific config copy is handled by protocol copy_config() hook
1111    */
1112 
1113   old_node = dest->n;
1114   old_class = dest->class;
1115   old_name = dest->name;
1116 
1117   memcpy(dest, src, src->protocol->config_size);
1118 
1119   dest->n = old_node;
1120   dest->class = old_class;
1121   dest->name = old_name;
1122   init_list(&dest->channels);
1123 
1124   WALK_LIST(cc, src->channels)
1125     channel_copy_config(cc, dest);
1126 
1127   /* FIXME: allow for undefined copy_config */
1128   dest->protocol->copy_config(dest, src);
1129 }
1130 
1131 void
proto_clone_config(struct symbol * sym,struct proto_config * parent)1132 proto_clone_config(struct symbol *sym, struct proto_config *parent)
1133 {
1134   struct proto_config *cf = proto_config_new(parent->protocol, SYM_PROTO);
1135   proto_copy_config(cf, parent);
1136   cf->name = sym->name;
1137   cf->proto = NULL;
1138   cf->parent = parent;
1139 
1140   sym->class = cf->class;
1141   sym->proto = cf;
1142 }
1143 
1144 static void
proto_undef_clone(struct symbol * sym,struct proto_config * cf)1145 proto_undef_clone(struct symbol *sym, struct proto_config *cf)
1146 {
1147   rem_node(&cf->n);
1148 
1149   sym->class = SYM_VOID;
1150   sym->proto = NULL;
1151 }
1152 
1153 /**
1154  * protos_preconfig - pre-configuration processing
1155  * @c: new configuration
1156  *
1157  * This function calls the preconfig() hooks of all routing
1158  * protocols available to prepare them for reading of the new
1159  * configuration.
1160  */
1161 void
protos_preconfig(struct config * c)1162 protos_preconfig(struct config *c)
1163 {
1164   struct protocol *p;
1165 
1166   init_list(&c->protos);
1167   DBG("Protocol preconfig:");
1168   WALK_LIST(p, protocol_list)
1169   {
1170     DBG(" %s", p->name);
1171     p->name_counter = 0;
1172     if (p->preconfig)
1173       p->preconfig(p, c);
1174   }
1175   DBG("\n");
1176 }
1177 
1178 static int
proto_reconfigure(struct proto * p,struct proto_config * oc,struct proto_config * nc,int type)1179 proto_reconfigure(struct proto *p, struct proto_config *oc, struct proto_config *nc, int type)
1180 {
1181   /* If the protocol is DOWN, we just restart it */
1182   if (p->proto_state == PS_DOWN)
1183     return 0;
1184 
1185   /* If there is a too big change in core attributes, ... */
1186   if ((nc->protocol != oc->protocol) ||
1187       (nc->net_type != oc->net_type) ||
1188       (nc->disabled != p->disabled) ||
1189       (nc->vrf != oc->vrf) ||
1190       (nc->vrf_set != oc->vrf_set))
1191     return 0;
1192 
1193   p->name = nc->name;
1194   p->debug = nc->debug;
1195   p->mrtdump = nc->mrtdump;
1196   reconfigure_type = type;
1197 
1198   /* Execute protocol specific reconfigure hook */
1199   if (!p->proto->reconfigure || !p->proto->reconfigure(p, nc))
1200     return 0;
1201 
1202   DBG("\t%s: same\n", oc->name);
1203   PD(p, "Reconfigured");
1204   p->cf = nc;
1205 
1206   return 1;
1207 }
1208 
1209 /**
1210  * protos_commit - commit new protocol configuration
1211  * @new: new configuration
1212  * @old: old configuration or %NULL if it's boot time config
1213  * @force_reconfig: force restart of all protocols (used for example
1214  * when the router ID changes)
1215  * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
1216  *
1217  * Scan differences between @old and @new configuration and adjust all
1218  * protocol instances to conform to the new configuration.
1219  *
1220  * When a protocol exists in the new configuration, but it doesn't in the
1221  * original one, it's immediately started. When a collision with the other
1222  * running protocol would arise, the new protocol will be temporarily stopped
1223  * by the locking mechanism.
1224  *
1225  * When a protocol exists in the old configuration, but it doesn't in the
1226  * new one, it's shut down and deleted after the shutdown completes.
1227  *
1228  * When a protocol exists in both configurations, the core decides
1229  * whether it's possible to reconfigure it dynamically - it checks all
1230  * the core properties of the protocol (changes in filters are ignored
1231  * if type is RECONFIG_SOFT) and if they match, it asks the
1232  * reconfigure() hook of the protocol to see if the protocol is able
1233  * to switch to the new configuration.  If it isn't possible, the
1234  * protocol is shut down and a new instance is started with the new
1235  * configuration after the shutdown is completed.
1236  */
1237 void
protos_commit(struct config * new,struct config * old,int force_reconfig,int type)1238 protos_commit(struct config *new, struct config *old, int force_reconfig, int type)
1239 {
1240   struct proto_config *oc, *nc;
1241   struct symbol *sym;
1242   struct proto *p;
1243   node *n;
1244 
1245 
1246   DBG("protos_commit:\n");
1247   if (old)
1248   {
1249     WALK_LIST(oc, old->protos)
1250     {
1251       p = oc->proto;
1252       sym = cf_find_symbol(new, oc->name);
1253 
1254       /* Handle dynamic protocols */
1255       if (!sym && oc->parent && !new->shutdown)
1256       {
1257 	struct symbol *parsym = cf_find_symbol(new, oc->parent->name);
1258 	if (parsym && parsym->class == SYM_PROTO)
1259 	{
1260 	  /* This is hack, we would like to share config, but we need to copy it now */
1261 	  new_config = new;
1262 	  cfg_mem = new->mem;
1263 	  conf_this_scope = new->root_scope;
1264 	  sym = cf_get_symbol(oc->name);
1265 	  proto_clone_config(sym, parsym->proto);
1266 	  new_config = NULL;
1267 	  cfg_mem = NULL;
1268 	}
1269       }
1270 
1271       if (sym && sym->class == SYM_PROTO && !new->shutdown)
1272       {
1273 	/* Found match, let's check if we can smoothly switch to new configuration */
1274 	/* No need to check description */
1275 	nc = sym->proto;
1276 	nc->proto = p;
1277 
1278 	/* We will try to reconfigure protocol p */
1279 	if (! force_reconfig && proto_reconfigure(p, oc, nc, type))
1280 	  continue;
1281 
1282 	if (nc->parent)
1283 	{
1284 	  proto_undef_clone(sym, nc);
1285 	  goto remove;
1286 	}
1287 
1288 	/* Unsuccessful, we will restart it */
1289 	if (!p->disabled && !nc->disabled)
1290 	  log(L_INFO "Restarting protocol %s", p->name);
1291 	else if (p->disabled && !nc->disabled)
1292 	  log(L_INFO "Enabling protocol %s", p->name);
1293 	else if (!p->disabled && nc->disabled)
1294 	  log(L_INFO "Disabling protocol %s", p->name);
1295 
1296 	p->down_code = nc->disabled ? PDC_CF_DISABLE : PDC_CF_RESTART;
1297 	p->cf_new = nc;
1298       }
1299       else if (!new->shutdown)
1300       {
1301       remove:
1302 	log(L_INFO "Removing protocol %s", p->name);
1303 	p->down_code = PDC_CF_REMOVE;
1304 	p->cf_new = NULL;
1305       }
1306       else if (new->gr_down)
1307       {
1308 	p->down_code = PDC_CMD_GR_DOWN;
1309 	p->cf_new = NULL;
1310       }
1311       else /* global shutdown */
1312       {
1313 	p->down_code = PDC_CMD_SHUTDOWN;
1314 	p->cf_new = NULL;
1315       }
1316 
1317       p->reconfiguring = 1;
1318       config_add_obstacle(old);
1319       proto_rethink_goal(p);
1320     }
1321   }
1322 
1323   struct proto *first_dev_proto = NULL;
1324 
1325   n = NODE &(proto_list.head);
1326   WALK_LIST(nc, new->protos)
1327     if (!nc->proto)
1328     {
1329       /* Not a first-time configuration */
1330       if (old)
1331 	log(L_INFO "Adding protocol %s", nc->name);
1332 
1333       p = proto_init(nc, n);
1334       n = NODE p;
1335 
1336       if (p->proto == &proto_unix_iface)
1337 	first_dev_proto = p;
1338     }
1339     else
1340       n = NODE nc->proto;
1341 
1342   DBG("Protocol start\n");
1343 
1344   /* Start device protocol first */
1345   if (first_dev_proto)
1346     proto_rethink_goal(first_dev_proto);
1347 
1348   /* Determine router ID for the first time - it has to be here and not in
1349      global_commit() because it is postponed after start of device protocol */
1350   if (!config->router_id)
1351   {
1352     config->router_id = if_choose_router_id(config->router_id_from, 0);
1353     if (!config->router_id)
1354       die("Cannot determine router ID, please configure it manually");
1355   }
1356 
1357   /* Start all new protocols */
1358   WALK_LIST_DELSAFE(p, n, proto_list)
1359     proto_rethink_goal(p);
1360 }
1361 
1362 static void
proto_rethink_goal(struct proto * p)1363 proto_rethink_goal(struct proto *p)
1364 {
1365   struct protocol *q;
1366   byte goal;
1367 
1368   if (p->reconfiguring && !p->active)
1369   {
1370     struct proto_config *nc = p->cf_new;
1371     node *n = p->n.prev;
1372     DBG("%s has shut down for reconfiguration\n", p->name);
1373     p->cf->proto = NULL;
1374     config_del_obstacle(p->cf->global);
1375     proto_remove_channels(p);
1376     rem_node(&p->n);
1377     rfree(p->event);
1378     mb_free(p->message);
1379     mb_free(p);
1380     if (!nc)
1381       return;
1382     p = proto_init(nc, n);
1383   }
1384 
1385   /* Determine what state we want to reach */
1386   if (p->disabled || p->reconfiguring)
1387     goal = PS_DOWN;
1388   else
1389     goal = PS_UP;
1390 
1391   q = p->proto;
1392   if (goal == PS_UP)
1393   {
1394     if (!p->active)
1395     {
1396       /* Going up */
1397       DBG("Kicking %s up\n", p->name);
1398       PD(p, "Starting");
1399       proto_start(p);
1400       proto_notify_state(p, (q->start ? q->start(p) : PS_UP));
1401     }
1402   }
1403   else
1404   {
1405     if (p->proto_state == PS_START || p->proto_state == PS_UP)
1406     {
1407       /* Going down */
1408       DBG("Kicking %s down\n", p->name);
1409       PD(p, "Shutting down");
1410       proto_notify_state(p, (q->shutdown ? q->shutdown(p) : PS_DOWN));
1411     }
1412   }
1413 }
1414 
1415 struct proto *
proto_spawn(struct proto_config * cf,uint disabled)1416 proto_spawn(struct proto_config *cf, uint disabled)
1417 {
1418   struct proto *p = proto_init(cf, TAIL(proto_list));
1419   p->disabled = disabled;
1420   proto_rethink_goal(p);
1421   return p;
1422 }
1423 
1424 
1425 /**
1426  * DOC: Graceful restart recovery
1427  *
1428  * Graceful restart of a router is a process when the routing plane (e.g. BIRD)
1429  * restarts but both the forwarding plane (e.g kernel routing table) and routing
1430  * neighbors keep proper routes, and therefore uninterrupted packet forwarding
1431  * is maintained.
1432  *
1433  * BIRD implements graceful restart recovery by deferring export of routes to
1434  * protocols until routing tables are refilled with the expected content. After
1435  * start, protocols generate routes as usual, but routes are not propagated to
1436  * them, until protocols report that they generated all routes. After that,
1437  * graceful restart recovery is finished and the export (and the initial feed)
1438  * to protocols is enabled.
1439  *
1440  * When graceful restart recovery need is detected during initialization, then
1441  * enabled protocols are marked with @gr_recovery flag before start. Such
1442  * protocols then decide how to proceed with graceful restart, participation is
1443  * voluntary. Protocols could lock the recovery for each channel by function
1444  * channel_graceful_restart_lock() (state stored in @gr_lock flag), which means
1445  * that they want to postpone the end of the recovery until they converge and
1446  * then unlock it. They also could set @gr_wait before advancing to %PS_UP,
1447  * which means that the core should defer route export to that channel until
1448  * the end of the recovery. This should be done by protocols that expect their
1449  * neigbors to keep the proper routes (kernel table, BGP sessions with BGP
1450  * graceful restart capability).
1451  *
1452  * The graceful restart recovery is finished when either all graceful restart
1453  * locks are unlocked or when graceful restart wait timer fires.
1454  *
1455  */
1456 
1457 static void graceful_restart_done(timer *t);
1458 
1459 /**
1460  * graceful_restart_recovery - request initial graceful restart recovery
1461  *
1462  * Called by the platform initialization code if the need for recovery
1463  * after graceful restart is detected during boot. Have to be called
1464  * before protos_commit().
1465  */
1466 void
graceful_restart_recovery(void)1467 graceful_restart_recovery(void)
1468 {
1469   graceful_restart_state = GRS_INIT;
1470 }
1471 
1472 /**
1473  * graceful_restart_init - initialize graceful restart
1474  *
1475  * When graceful restart recovery was requested, the function starts an active
1476  * phase of the recovery and initializes graceful restart wait timer. The
1477  * function have to be called after protos_commit().
1478  */
1479 void
graceful_restart_init(void)1480 graceful_restart_init(void)
1481 {
1482   if (!graceful_restart_state)
1483     return;
1484 
1485   log(L_INFO "Graceful restart started");
1486 
1487   if (!graceful_restart_locks)
1488   {
1489     graceful_restart_done(NULL);
1490     return;
1491   }
1492 
1493   graceful_restart_state = GRS_ACTIVE;
1494   gr_wait_timer = tm_new_init(proto_pool, graceful_restart_done, NULL, 0, 0);
1495   tm_start(gr_wait_timer, config->gr_wait S);
1496 }
1497 
1498 /**
1499  * graceful_restart_done - finalize graceful restart
1500  * @t: unused
1501  *
1502  * When there are no locks on graceful restart, the functions finalizes the
1503  * graceful restart recovery. Protocols postponing route export until the end of
1504  * the recovery are awakened and the export to them is enabled. All other
1505  * related state is cleared. The function is also called when the graceful
1506  * restart wait timer fires (but there are still some locks).
1507  */
1508 static void
graceful_restart_done(timer * t UNUSED)1509 graceful_restart_done(timer *t UNUSED)
1510 {
1511   log(L_INFO "Graceful restart done");
1512   graceful_restart_state = GRS_DONE;
1513 
1514   struct proto *p;
1515   WALK_LIST(p, proto_list)
1516   {
1517     if (!p->gr_recovery)
1518       continue;
1519 
1520     struct channel *c;
1521     WALK_LIST(c, p->channels)
1522     {
1523       /* Resume postponed export of routes */
1524       if ((c->channel_state == CS_UP) && c->gr_wait && c->proto->rt_notify)
1525 	channel_start_export(c);
1526 
1527       /* Cleanup */
1528       c->gr_wait = 0;
1529       c->gr_lock = 0;
1530     }
1531 
1532     p->gr_recovery = 0;
1533   }
1534 
1535   graceful_restart_locks = 0;
1536 }
1537 
1538 void
graceful_restart_show_status(void)1539 graceful_restart_show_status(void)
1540 {
1541   if (graceful_restart_state != GRS_ACTIVE)
1542     return;
1543 
1544   cli_msg(-24, "Graceful restart recovery in progress");
1545   cli_msg(-24, "  Waiting for %d channels to recover", graceful_restart_locks);
1546   cli_msg(-24, "  Wait timer is %t/%u", tm_remains(gr_wait_timer), config->gr_wait);
1547 }
1548 
1549 /**
1550  * channel_graceful_restart_lock - lock graceful restart by channel
1551  * @p: channel instance
1552  *
1553  * This function allows a protocol to postpone the end of graceful restart
1554  * recovery until it converges. The lock is removed when the protocol calls
1555  * channel_graceful_restart_unlock() or when the channel is closed.
1556  *
1557  * The function have to be called during the initial phase of graceful restart
1558  * recovery and only for protocols that are part of graceful restart (i.e. their
1559  * @gr_recovery is set), which means it should be called from protocol start
1560  * hooks.
1561  */
1562 void
channel_graceful_restart_lock(struct channel * c)1563 channel_graceful_restart_lock(struct channel *c)
1564 {
1565   ASSERT(graceful_restart_state == GRS_INIT);
1566   ASSERT(c->proto->gr_recovery);
1567 
1568   if (c->gr_lock)
1569     return;
1570 
1571   c->gr_lock = 1;
1572   graceful_restart_locks++;
1573 }
1574 
1575 /**
1576  * channel_graceful_restart_unlock - unlock graceful restart by channel
1577  * @p: channel instance
1578  *
1579  * This function unlocks a lock from channel_graceful_restart_lock(). It is also
1580  * automatically called when the lock holding protocol went down.
1581  */
1582 void
channel_graceful_restart_unlock(struct channel * c)1583 channel_graceful_restart_unlock(struct channel *c)
1584 {
1585   if (!c->gr_lock)
1586     return;
1587 
1588   c->gr_lock = 0;
1589   graceful_restart_locks--;
1590 
1591   if ((graceful_restart_state == GRS_ACTIVE) && !graceful_restart_locks)
1592     tm_start(gr_wait_timer, 0);
1593 }
1594 
1595 
1596 
1597 /**
1598  * protos_dump_all - dump status of all protocols
1599  *
1600  * This function dumps status of all existing protocol instances to the
1601  * debug output. It involves printing of general status information
1602  * such as protocol states, its position on the protocol lists
1603  * and also calling of a dump() hook of the protocol to print
1604  * the internals.
1605  */
1606 void
protos_dump_all(void)1607 protos_dump_all(void)
1608 {
1609   debug("Protocols:\n");
1610 
1611   struct proto *p;
1612   WALK_LIST(p, proto_list)
1613   {
1614     debug("  protocol %s state %s\n", p->name, p_states[p->proto_state]);
1615 
1616     struct channel *c;
1617     WALK_LIST(c, p->channels)
1618     {
1619       debug("\tTABLE %s\n", c->table->name);
1620       if (c->in_filter)
1621 	debug("\tInput filter: %s\n", filter_name(c->in_filter));
1622       if (c->out_filter)
1623 	debug("\tOutput filter: %s\n", filter_name(c->out_filter));
1624     }
1625 
1626     if (p->proto->dump && (p->proto_state != PS_DOWN))
1627       p->proto->dump(p);
1628   }
1629 }
1630 
1631 /**
1632  * proto_build - make a single protocol available
1633  * @p: the protocol
1634  *
1635  * After the platform specific initialization code uses protos_build()
1636  * to add all the standard protocols, it should call proto_build() for
1637  * all platform specific protocols to inform the core that they exist.
1638  */
1639 void
proto_build(struct protocol * p)1640 proto_build(struct protocol *p)
1641 {
1642   add_tail(&protocol_list, &p->n);
1643   ASSERT(p->class);
1644   ASSERT(!class_to_protocol[p->class]);
1645   class_to_protocol[p->class] = p;
1646 }
1647 
1648 /* FIXME: convert this call to some protocol hook */
1649 extern void bfd_init_all(void);
1650 
1651 /**
1652  * protos_build - build a protocol list
1653  *
1654  * This function is called during BIRD startup to insert
1655  * all standard protocols to the global protocol list. Insertion
1656  * of platform specific protocols (such as the kernel syncer)
1657  * is in the domain of competence of the platform dependent
1658  * startup code.
1659  */
1660 void
protos_build(void)1661 protos_build(void)
1662 {
1663   init_list(&proto_list);
1664   init_list(&protocol_list);
1665 
1666   proto_build(&proto_device);
1667 #ifdef CONFIG_RADV
1668   proto_build(&proto_radv);
1669 #endif
1670 #ifdef CONFIG_RIP
1671   proto_build(&proto_rip);
1672 #endif
1673 #ifdef CONFIG_STATIC
1674   proto_build(&proto_static);
1675 #endif
1676 #ifdef CONFIG_MRT
1677   proto_build(&proto_mrt);
1678 #endif
1679 #ifdef CONFIG_OSPF
1680   proto_build(&proto_ospf);
1681 #endif
1682 #ifdef CONFIG_PIPE
1683   proto_build(&proto_pipe);
1684 #endif
1685 #ifdef CONFIG_BGP
1686   proto_build(&proto_bgp);
1687 #endif
1688 #ifdef CONFIG_BFD
1689   proto_build(&proto_bfd);
1690   bfd_init_all();
1691 #endif
1692 #ifdef CONFIG_BABEL
1693   proto_build(&proto_babel);
1694 #endif
1695 #ifdef CONFIG_RPKI
1696   proto_build(&proto_rpki);
1697 #endif
1698 #ifdef CONFIG_PERF
1699   proto_build(&proto_perf);
1700 #endif
1701 
1702   proto_pool = rp_new(&root_pool, "Protocols");
1703   proto_shutdown_timer = tm_new(proto_pool);
1704   proto_shutdown_timer->hook = proto_shutdown_loop;
1705 }
1706 
1707 
1708 /* Temporary hack to propagate restart to BGP */
1709 int proto_restart;
1710 
1711 static void
proto_shutdown_loop(timer * t UNUSED)1712 proto_shutdown_loop(timer *t UNUSED)
1713 {
1714   struct proto *p, *p_next;
1715 
1716   WALK_LIST_DELSAFE(p, p_next, proto_list)
1717     if (p->down_sched)
1718     {
1719       proto_restart = (p->down_sched == PDS_RESTART);
1720 
1721       p->disabled = 1;
1722       proto_rethink_goal(p);
1723       if (proto_restart)
1724       {
1725 	p->disabled = 0;
1726 	proto_rethink_goal(p);
1727       }
1728     }
1729 }
1730 
1731 static inline void
proto_schedule_down(struct proto * p,byte restart,byte code)1732 proto_schedule_down(struct proto *p, byte restart, byte code)
1733 {
1734   /* Does not work for other states (even PS_START) */
1735   ASSERT(p->proto_state == PS_UP);
1736 
1737   /* Scheduled restart may change to shutdown, but not otherwise */
1738   if (p->down_sched == PDS_DISABLE)
1739     return;
1740 
1741   p->down_sched = restart ? PDS_RESTART : PDS_DISABLE;
1742   p->down_code = code;
1743   tm_start_max(proto_shutdown_timer, restart ? 250 MS : 0);
1744 }
1745 
1746 /**
1747  * proto_set_message - set administrative message to protocol
1748  * @p: protocol
1749  * @msg: message
1750  * @len: message length (-1 for NULL-terminated string)
1751  *
1752  * The function sets administrative message (string) related to protocol state
1753  * change. It is called by the nest code for manual enable/disable/restart
1754  * commands all routes to the protocol, and by protocol-specific code when the
1755  * protocol state change is initiated by the protocol. Using NULL message clears
1756  * the last message. The message string may be either NULL-terminated or with an
1757  * explicit length.
1758  */
1759 void
proto_set_message(struct proto * p,char * msg,int len)1760 proto_set_message(struct proto *p, char *msg, int len)
1761 {
1762   mb_free(p->message);
1763   p->message = NULL;
1764 
1765   if (!msg || !len)
1766     return;
1767 
1768   if (len < 0)
1769     len = strlen(msg);
1770 
1771   if (!len)
1772     return;
1773 
1774   p->message = mb_alloc(proto_pool, len + 1);
1775   memcpy(p->message, msg, len);
1776   p->message[len] = 0;
1777 }
1778 
1779 
1780 static const char *
channel_limit_name(struct channel_limit * l)1781 channel_limit_name(struct channel_limit *l)
1782 {
1783   const char *actions[] = {
1784     [PLA_WARN] = "warn",
1785     [PLA_BLOCK] = "block",
1786     [PLA_RESTART] = "restart",
1787     [PLA_DISABLE] = "disable",
1788   };
1789 
1790   return actions[l->action];
1791 }
1792 
1793 /**
1794  * channel_notify_limit: notify about limit hit and take appropriate action
1795  * @c: channel
1796  * @l: limit being hit
1797  * @dir: limit direction (PLD_*)
1798  * @rt_count: the number of routes
1799  *
1800  * The function is called by the route processing core when limit @l
1801  * is breached. It activates the limit and tooks appropriate action
1802  * according to @l->action.
1803  */
1804 void
channel_notify_limit(struct channel * c,struct channel_limit * l,int dir,u32 rt_count)1805 channel_notify_limit(struct channel *c, struct channel_limit *l, int dir, u32 rt_count)
1806 {
1807   const char *dir_name[PLD_MAX] = { "receive", "import" , "export" };
1808   const byte dir_down[PLD_MAX] = { PDC_RX_LIMIT_HIT, PDC_IN_LIMIT_HIT, PDC_OUT_LIMIT_HIT };
1809   struct proto *p = c->proto;
1810 
1811   if (l->state == PLS_BLOCKED)
1812     return;
1813 
1814   /* For warning action, we want the log message every time we hit the limit */
1815   if (!l->state || ((l->action == PLA_WARN) && (rt_count == l->limit)))
1816     log(L_WARN "Protocol %s hits route %s limit (%d), action: %s",
1817 	p->name, dir_name[dir], l->limit, channel_limit_name(l));
1818 
1819   switch (l->action)
1820   {
1821   case PLA_WARN:
1822     l->state = PLS_ACTIVE;
1823     break;
1824 
1825   case PLA_BLOCK:
1826     l->state = PLS_BLOCKED;
1827     break;
1828 
1829   case PLA_RESTART:
1830   case PLA_DISABLE:
1831     l->state = PLS_BLOCKED;
1832     if (p->proto_state == PS_UP)
1833       proto_schedule_down(p, l->action == PLA_RESTART, dir_down[dir]);
1834     break;
1835   }
1836 }
1837 
1838 static void
channel_verify_limits(struct channel * c)1839 channel_verify_limits(struct channel *c)
1840 {
1841   struct channel_limit *l;
1842   u32 all_routes = c->stats.imp_routes + c->stats.filt_routes;
1843 
1844   l = &c->rx_limit;
1845   if (l->action && (all_routes > l->limit))
1846     channel_notify_limit(c, l, PLD_RX, all_routes);
1847 
1848   l = &c->in_limit;
1849   if (l->action && (c->stats.imp_routes > l->limit))
1850     channel_notify_limit(c, l, PLD_IN, c->stats.imp_routes);
1851 
1852   l = &c->out_limit;
1853   if (l->action && (c->stats.exp_routes > l->limit))
1854     channel_notify_limit(c, l, PLD_OUT, c->stats.exp_routes);
1855 }
1856 
1857 static inline void
channel_reset_limit(struct channel_limit * l)1858 channel_reset_limit(struct channel_limit *l)
1859 {
1860   if (l->action)
1861     l->state = PLS_INITIAL;
1862 }
1863 
1864 static inline void
proto_do_start(struct proto * p)1865 proto_do_start(struct proto *p)
1866 {
1867   p->active = 1;
1868   p->do_start = 1;
1869   ev_schedule(p->event);
1870 }
1871 
1872 static void
proto_do_up(struct proto * p)1873 proto_do_up(struct proto *p)
1874 {
1875   if (!p->main_source)
1876   {
1877     p->main_source = rt_get_source(p, 0);
1878     rt_lock_source(p->main_source);
1879   }
1880 
1881   proto_start_channels(p);
1882 }
1883 
1884 static inline void
proto_do_pause(struct proto * p)1885 proto_do_pause(struct proto *p)
1886 {
1887   proto_pause_channels(p);
1888 }
1889 
1890 static void
proto_do_stop(struct proto * p)1891 proto_do_stop(struct proto *p)
1892 {
1893   p->down_sched = 0;
1894   p->gr_recovery = 0;
1895 
1896   p->do_stop = 1;
1897   ev_schedule(p->event);
1898 
1899   if (p->main_source)
1900   {
1901     rt_unlock_source(p->main_source);
1902     p->main_source = NULL;
1903   }
1904 
1905   proto_stop_channels(p);
1906 }
1907 
1908 static void
proto_do_down(struct proto * p)1909 proto_do_down(struct proto *p)
1910 {
1911   p->down_code = 0;
1912   neigh_prune();
1913   rfree(p->pool);
1914   p->pool = NULL;
1915 
1916   /* Shutdown is finished in the protocol event */
1917   if (proto_is_done(p))
1918     ev_schedule(p->event);
1919 }
1920 
1921 
1922 
1923 /**
1924  * proto_notify_state - notify core about protocol state change
1925  * @p: protocol the state of which has changed
1926  * @ps: the new status
1927  *
1928  * Whenever a state of a protocol changes due to some event internal
1929  * to the protocol (i.e., not inside a start() or shutdown() hook),
1930  * it should immediately notify the core about the change by calling
1931  * proto_notify_state() which will write the new state to the &proto
1932  * structure and take all the actions necessary to adapt to the new
1933  * state. State change to PS_DOWN immediately frees resources of protocol
1934  * and might execute start callback of protocol; therefore,
1935  * it should be used at tail positions of protocol callbacks.
1936  */
1937 void
proto_notify_state(struct proto * p,uint state)1938 proto_notify_state(struct proto *p, uint state)
1939 {
1940   uint ps = p->proto_state;
1941 
1942   DBG("%s reporting state transition %s -> %s\n", p->name, p_states[ps], p_states[state]);
1943   if (state == ps)
1944     return;
1945 
1946   p->proto_state = state;
1947   p->last_state_change = current_time();
1948 
1949   switch (state)
1950   {
1951   case PS_START:
1952     ASSERT(ps == PS_DOWN || ps == PS_UP);
1953 
1954     if (ps == PS_DOWN)
1955       proto_do_start(p);
1956     else
1957       proto_do_pause(p);
1958     break;
1959 
1960   case PS_UP:
1961     ASSERT(ps == PS_DOWN || ps == PS_START);
1962 
1963     if (ps == PS_DOWN)
1964       proto_do_start(p);
1965 
1966     proto_do_up(p);
1967     break;
1968 
1969   case PS_STOP:
1970     ASSERT(ps == PS_START || ps == PS_UP);
1971 
1972     proto_do_stop(p);
1973     break;
1974 
1975   case PS_DOWN:
1976     if (ps != PS_STOP)
1977       proto_do_stop(p);
1978 
1979     proto_do_down(p);
1980     break;
1981 
1982   default:
1983     bug("%s: Invalid state %d", p->name, ps);
1984   }
1985 
1986   proto_log_state_change(p);
1987 }
1988 
1989 /*
1990  *  CLI Commands
1991  */
1992 
1993 static char *
proto_state_name(struct proto * p)1994 proto_state_name(struct proto *p)
1995 {
1996   switch (p->proto_state)
1997   {
1998   case PS_DOWN:		return p->active ? "flush" : "down";
1999   case PS_START:	return "start";
2000   case PS_UP:		return "up";
2001   case PS_STOP:		return "stop";
2002   default:		return "???";
2003   }
2004 }
2005 
2006 static void
channel_show_stats(struct channel * c)2007 channel_show_stats(struct channel *c)
2008 {
2009   struct proto_stats *s = &c->stats;
2010 
2011   if (c->in_keep_filtered)
2012     cli_msg(-1006, "    Routes:         %u imported, %u filtered, %u exported, %u preferred",
2013 	    s->imp_routes, s->filt_routes, s->exp_routes, s->pref_routes);
2014   else
2015     cli_msg(-1006, "    Routes:         %u imported, %u exported, %u preferred",
2016 	    s->imp_routes, s->exp_routes, s->pref_routes);
2017 
2018   cli_msg(-1006, "    Route change stats:     received   rejected   filtered    ignored   accepted");
2019   cli_msg(-1006, "      Import updates:     %10u %10u %10u %10u %10u",
2020 	  s->imp_updates_received, s->imp_updates_invalid,
2021 	  s->imp_updates_filtered, s->imp_updates_ignored,
2022 	  s->imp_updates_accepted);
2023   cli_msg(-1006, "      Import withdraws:   %10u %10u        --- %10u %10u",
2024 	  s->imp_withdraws_received, s->imp_withdraws_invalid,
2025 	  s->imp_withdraws_ignored, s->imp_withdraws_accepted);
2026   cli_msg(-1006, "      Export updates:     %10u %10u %10u        --- %10u",
2027 	  s->exp_updates_received, s->exp_updates_rejected,
2028 	  s->exp_updates_filtered, s->exp_updates_accepted);
2029   cli_msg(-1006, "      Export withdraws:   %10u        ---        ---        --- %10u",
2030 	  s->exp_withdraws_received, s->exp_withdraws_accepted);
2031 }
2032 
2033 void
channel_show_limit(struct channel_limit * l,const char * dsc)2034 channel_show_limit(struct channel_limit *l, const char *dsc)
2035 {
2036   if (!l->action)
2037     return;
2038 
2039   cli_msg(-1006, "    %-16s%d%s", dsc, l->limit, l->state ? " [HIT]" : "");
2040   cli_msg(-1006, "      Action:       %s", channel_limit_name(l));
2041 }
2042 
2043 void
channel_show_info(struct channel * c)2044 channel_show_info(struct channel *c)
2045 {
2046   cli_msg(-1006, "  Channel %s", c->name);
2047   cli_msg(-1006, "    State:          %s", c_states[c->channel_state]);
2048   cli_msg(-1006, "    Table:          %s", c->table->name);
2049   cli_msg(-1006, "    Preference:     %d", c->preference);
2050   cli_msg(-1006, "    Input filter:   %s", filter_name(c->in_filter));
2051   cli_msg(-1006, "    Output filter:  %s", filter_name(c->out_filter));
2052 
2053   if (graceful_restart_state == GRS_ACTIVE)
2054     cli_msg(-1006, "    GR recovery:   %s%s",
2055 	    c->gr_lock ? " pending" : "",
2056 	    c->gr_wait ? " waiting" : "");
2057 
2058   channel_show_limit(&c->rx_limit, "Receive limit:");
2059   channel_show_limit(&c->in_limit, "Import limit:");
2060   channel_show_limit(&c->out_limit, "Export limit:");
2061 
2062   if (c->channel_state != CS_DOWN)
2063     channel_show_stats(c);
2064 }
2065 
2066 void
channel_cmd_debug(struct channel * c,uint mask)2067 channel_cmd_debug(struct channel *c, uint mask)
2068 {
2069   if (cli_access_restricted())
2070     return;
2071 
2072   c->debug = mask;
2073   cli_msg(0, "");
2074 }
2075 
2076 void
proto_cmd_show(struct proto * p,uintptr_t verbose,int cnt)2077 proto_cmd_show(struct proto *p, uintptr_t verbose, int cnt)
2078 {
2079   byte buf[256], tbuf[TM_DATETIME_BUFFER_SIZE];
2080 
2081   /* First protocol - show header */
2082   if (!cnt)
2083     cli_msg(-2002, "%-10s %-10s %-10s %-6s %-12s  %s",
2084 	    "Name", "Proto", "Table", "State", "Since", "Info");
2085 
2086   buf[0] = 0;
2087   if (p->proto->get_status)
2088     p->proto->get_status(p, buf);
2089   tm_format_time(tbuf, &config->tf_proto, p->last_state_change);
2090   cli_msg(-1002, "%-10s %-10s %-10s %-6s %-12s  %s",
2091 	  p->name,
2092 	  p->proto->name,
2093 	  p->main_channel ? p->main_channel->table->name : "---",
2094 	  proto_state_name(p),
2095 	  tbuf,
2096 	  buf);
2097 
2098   if (verbose)
2099   {
2100     if (p->cf->dsc)
2101       cli_msg(-1006, "  Description:    %s", p->cf->dsc);
2102     if (p->message)
2103       cli_msg(-1006, "  Message:        %s", p->message);
2104     if (p->cf->router_id)
2105       cli_msg(-1006, "  Router ID:      %R", p->cf->router_id);
2106     if (p->vrf_set)
2107       cli_msg(-1006, "  VRF:            %s", p->vrf ? p->vrf->name : "default");
2108 
2109     if (p->proto->show_proto_info)
2110       p->proto->show_proto_info(p);
2111     else
2112     {
2113       struct channel *c;
2114       WALK_LIST(c, p->channels)
2115 	channel_show_info(c);
2116     }
2117 
2118     cli_msg(-1006, "");
2119   }
2120 }
2121 
2122 void
proto_cmd_disable(struct proto * p,uintptr_t arg,int cnt UNUSED)2123 proto_cmd_disable(struct proto *p, uintptr_t arg, int cnt UNUSED)
2124 {
2125   if (p->disabled)
2126   {
2127     cli_msg(-8, "%s: already disabled", p->name);
2128     return;
2129   }
2130 
2131   log(L_INFO "Disabling protocol %s", p->name);
2132   p->disabled = 1;
2133   p->down_code = PDC_CMD_DISABLE;
2134   proto_set_message(p, (char *) arg, -1);
2135   proto_rethink_goal(p);
2136   cli_msg(-9, "%s: disabled", p->name);
2137 }
2138 
2139 void
proto_cmd_enable(struct proto * p,uintptr_t arg,int cnt UNUSED)2140 proto_cmd_enable(struct proto *p, uintptr_t arg, int cnt UNUSED)
2141 {
2142   if (!p->disabled)
2143   {
2144     cli_msg(-10, "%s: already enabled", p->name);
2145     return;
2146   }
2147 
2148   log(L_INFO "Enabling protocol %s", p->name);
2149   p->disabled = 0;
2150   proto_set_message(p, (char *) arg, -1);
2151   proto_rethink_goal(p);
2152   cli_msg(-11, "%s: enabled", p->name);
2153 }
2154 
2155 void
proto_cmd_restart(struct proto * p,uintptr_t arg,int cnt UNUSED)2156 proto_cmd_restart(struct proto *p, uintptr_t arg, int cnt UNUSED)
2157 {
2158   if (p->disabled)
2159   {
2160     cli_msg(-8, "%s: already disabled", p->name);
2161     return;
2162   }
2163 
2164   log(L_INFO "Restarting protocol %s", p->name);
2165   p->disabled = 1;
2166   p->down_code = PDC_CMD_RESTART;
2167   proto_set_message(p, (char *) arg, -1);
2168   proto_rethink_goal(p);
2169   p->disabled = 0;
2170   proto_rethink_goal(p);
2171   cli_msg(-12, "%s: restarted", p->name);
2172 }
2173 
2174 void
proto_cmd_reload(struct proto * p,uintptr_t dir,int cnt UNUSED)2175 proto_cmd_reload(struct proto *p, uintptr_t dir, int cnt UNUSED)
2176 {
2177   struct channel *c;
2178 
2179   if (p->disabled)
2180   {
2181     cli_msg(-8, "%s: already disabled", p->name);
2182     return;
2183   }
2184 
2185   /* If the protocol in not UP, it has no routes */
2186   if (p->proto_state != PS_UP)
2187     return;
2188 
2189   /* All channels must support reload */
2190   if (dir != CMD_RELOAD_OUT)
2191     WALK_LIST(c, p->channels)
2192       if ((c->channel_state == CS_UP) && !channel_reloadable(c))
2193       {
2194 	cli_msg(-8006, "%s: reload failed", p->name);
2195 	return;
2196       }
2197 
2198   log(L_INFO "Reloading protocol %s", p->name);
2199 
2200   /* re-importing routes */
2201   if (dir != CMD_RELOAD_OUT)
2202     WALK_LIST(c, p->channels)
2203       if (c->channel_state == CS_UP)
2204 	channel_request_reload(c);
2205 
2206   /* re-exporting routes */
2207   if (dir != CMD_RELOAD_IN)
2208     WALK_LIST(c, p->channels)
2209       if (c->channel_state == CS_UP)
2210 	channel_request_feeding(c);
2211 
2212   cli_msg(-15, "%s: reloading", p->name);
2213 }
2214 
2215 extern void pipe_update_debug(struct proto *P);
2216 
2217 void
proto_cmd_debug(struct proto * p,uintptr_t mask,int cnt UNUSED)2218 proto_cmd_debug(struct proto *p, uintptr_t mask, int cnt UNUSED)
2219 {
2220   p->debug = mask;
2221 
2222 #ifdef CONFIG_PIPE
2223   if (p->proto == &proto_pipe)
2224     pipe_update_debug(p);
2225 #endif
2226 }
2227 
2228 void
proto_cmd_mrtdump(struct proto * p,uintptr_t mask,int cnt UNUSED)2229 proto_cmd_mrtdump(struct proto *p, uintptr_t mask, int cnt UNUSED)
2230 {
2231   p->mrtdump = mask;
2232 }
2233 
2234 static void
proto_apply_cmd_symbol(const struct symbol * s,void (* cmd)(struct proto *,uintptr_t,int),uintptr_t arg)2235 proto_apply_cmd_symbol(const struct symbol *s, void (* cmd)(struct proto *, uintptr_t, int), uintptr_t arg)
2236 {
2237   if (s->class != SYM_PROTO)
2238   {
2239     cli_msg(9002, "%s is not a protocol", s->name);
2240     return;
2241   }
2242 
2243   cmd(s->proto->proto, arg, 0);
2244   cli_msg(0, "");
2245 }
2246 
2247 static void
proto_apply_cmd_patt(const char * patt,void (* cmd)(struct proto *,uintptr_t,int),uintptr_t arg)2248 proto_apply_cmd_patt(const char *patt, void (* cmd)(struct proto *, uintptr_t, int), uintptr_t arg)
2249 {
2250   struct proto *p;
2251   int cnt = 0;
2252 
2253   WALK_LIST(p, proto_list)
2254     if (!patt || patmatch(patt, p->name))
2255       cmd(p, arg, cnt++);
2256 
2257   if (!cnt)
2258     cli_msg(8003, "No protocols match");
2259   else
2260     cli_msg(0, "");
2261 }
2262 
2263 void
proto_apply_cmd(struct proto_spec ps,void (* cmd)(struct proto *,uintptr_t,int),int restricted,uintptr_t arg)2264 proto_apply_cmd(struct proto_spec ps, void (* cmd)(struct proto *, uintptr_t, int),
2265 		int restricted, uintptr_t arg)
2266 {
2267   if (restricted && cli_access_restricted())
2268     return;
2269 
2270   if (ps.patt)
2271     proto_apply_cmd_patt(ps.ptr, cmd, arg);
2272   else
2273     proto_apply_cmd_symbol(ps.ptr, cmd, arg);
2274 }
2275 
2276 struct proto *
proto_get_named(struct symbol * sym,struct protocol * pr)2277 proto_get_named(struct symbol *sym, struct protocol *pr)
2278 {
2279   struct proto *p, *q;
2280 
2281   if (sym)
2282   {
2283     if (sym->class != SYM_PROTO)
2284       cf_error("%s: Not a protocol", sym->name);
2285 
2286     p = sym->proto->proto;
2287     if (!p || p->proto != pr)
2288       cf_error("%s: Not a %s protocol", sym->name, pr->name);
2289   }
2290   else
2291   {
2292     p = NULL;
2293     WALK_LIST(q, proto_list)
2294       if ((q->proto == pr) && (q->proto_state != PS_DOWN))
2295       {
2296 	if (p)
2297 	  cf_error("There are multiple %s protocols running", pr->name);
2298 	p = q;
2299       }
2300     if (!p)
2301       cf_error("There is no %s protocol running", pr->name);
2302   }
2303 
2304   return p;
2305 }
2306 
2307 struct proto *
proto_iterate_named(struct symbol * sym,struct protocol * proto,struct proto * old)2308 proto_iterate_named(struct symbol *sym, struct protocol *proto, struct proto *old)
2309 {
2310   if (sym)
2311   {
2312     /* Just the first pass */
2313     if (old)
2314     {
2315       cli_msg(0, "");
2316       return NULL;
2317     }
2318 
2319     if (sym->class != SYM_PROTO)
2320       cf_error("%s: Not a protocol", sym->name);
2321 
2322     struct proto *p = sym->proto->proto;
2323     if (!p || (p->proto != proto))
2324       cf_error("%s: Not a %s protocol", sym->name, proto->name);
2325 
2326     return p;
2327   }
2328   else
2329   {
2330     for (struct proto *p = !old ? HEAD(proto_list) : NODE_NEXT(old);
2331 	 NODE_VALID(p);
2332 	 p = NODE_NEXT(p))
2333     {
2334       if ((p->proto == proto) && (p->proto_state != PS_DOWN))
2335       {
2336 	cli_separator(this_cli);
2337 	return p;
2338       }
2339     }
2340 
2341     /* Not found anything during first pass */
2342     if (!old)
2343       cf_error("There is no %s protocol running", proto->name);
2344 
2345     /* No more items */
2346     cli_msg(0, "");
2347     return NULL;
2348   }
2349 }
2350