1 /*
2  *	BIRD -- UNIX Kernel Route Syncer
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_KRT_H_
10 #define _BIRD_KRT_H_
11 
12 struct config;
13 struct krt_config;
14 struct krt_proto;
15 struct kif_config;
16 struct kif_proto;
17 
18 #include "nest/iface.h"
19 #include "sysdep/config.h"
20 #include CONFIG_INCLUDE_KRTSYS_H
21 
22 #define KRT_DEFAULT_ECMP_LIMIT	16
23 
24 #define EA_KRT_SOURCE	EA_CODE(PROTOCOL_KERNEL, 0)
25 #define EA_KRT_METRIC	EA_CODE(PROTOCOL_KERNEL, 1)
26 
27 /* Whenever we recognize our own routes, we allow learing of foreign routes */
28 
29 #ifdef CONFIG_SELF_CONSCIOUS
30 #define KRT_ALLOW_LEARN
31 #endif
32 
33 /* krt.c */
34 
35 extern struct protocol proto_unix_kernel;
36 
37 struct krt_config {
38   struct proto_config c;
39   struct krt_params sys;	/* Sysdep params */
40   btime scan_time;		/* How often we re-scan routes */
41   int persist;			/* Keep routes when we exit */
42   int learn;			/* Learn routes from other sources */
43   int graceful_restart;		/* Regard graceful restart recovery */
44   int merge_paths;		/* Exported routes are merged for ECMP */
45 };
46 
47 struct krt_proto {
48   struct proto p;
49   struct krt_state sys;		/* Sysdep state */
50 
51 #ifdef KRT_ALLOW_LEARN
52   struct rtable krt_table;	/* Internal table of inherited routes */
53 #endif
54 
55 #ifndef CONFIG_ALL_TABLES_AT_ONCE
56   timer *scan_timer;
57 #endif
58 
59   struct bmap sync_map;		/* Keeps track which exported routes were successfully written to kernel */
60   struct bmap seen_map;		/* Routes seen during last periodic scan */
61   node krt_node;		/* Node in krt_proto_list */
62   byte af;			/* Kernel address family (AF_*) */
63   byte ready;			/* Initial feed has been finished */
64   byte initialized;		/* First scan has been finished */
65   byte reload;			/* Next scan is doing reload */
66 };
67 
68 extern pool *krt_pool;
69 
70 #define KRT_CF ((struct krt_config *)p->p.cf)
71 
72 #define KRT_TRACE(pr, fl, msg, args...) do {	\
73   DBG("KRT: " msg "\n" , ## args);		\
74   if (pr->p.debug & fl)				\
75     { log(L_TRACE "%s: " msg, pr->p.name , ## args); } } while(0)
76 
77 struct proto_config * kif_init_config(int class);
78 void kif_request_scan(void);
79 void krt_got_route(struct krt_proto *p, struct rte *e);
80 void krt_got_route_async(struct krt_proto *p, struct rte *e, int new);
81 
82 static inline int
krt_get_sync_error(struct krt_proto * p,struct rte * e)83 krt_get_sync_error(struct krt_proto *p, struct rte *e)
84 {
85   return (p->p.proto_state == PS_UP) &&
86     bmap_test(&p->p.main_channel->export_map, e->id) &&
87     !bmap_test(&p->sync_map, e->id);
88 }
89 
90 /* Values for rte->u.krt_sync.src */
91 #define KRT_SRC_UNKNOWN	-1	/* Nobody knows */
92 #define KRT_SRC_BIRD	 0	/* Our route (not passed in async mode) */
93 #define KRT_SRC_REDIRECT 1	/* Redirect route, delete it */
94 #define KRT_SRC_ALIEN	 2	/* Route installed by someone else */
95 #define KRT_SRC_KERNEL	 3	/* Kernel routes, are ignored by krt syncer */
96 
97 extern struct protocol proto_unix_iface;
98 
99 struct kif_config {
100   struct proto_config c;
101   struct kif_params sys;	/* Sysdep params */
102 
103   list iface_list;		/* List of iface configs (struct kif_iface_config) */
104   btime scan_time;		/* How often we re-scan interfaces */
105 };
106 
107 struct kif_iface_config {
108   struct iface_patt i;
109 
110   ip_addr pref_v4;
111   ip_addr pref_v6;
112   ip_addr pref_ll;
113 };
114 
115 struct kif_proto {
116   struct proto p;
117   struct kif_state sys;		/* Sysdep state */
118 };
119 
120 extern struct kif_proto *kif_proto;
121 
122 #define KIF_CF ((struct kif_config *)p->p.cf)
123 
124 struct kif_iface_config * kif_get_iface_config(struct iface *iface);
125 struct proto_config * krt_init_config(int class);
126 
127 
128 /* krt sysdep */
129 
130 void krt_sys_io_init(void);
131 void krt_sys_init(struct krt_proto *);
132 int krt_sys_start(struct krt_proto *);
133 void krt_sys_shutdown(struct krt_proto *);
134 int krt_sys_reconfigure(struct krt_proto *p UNUSED, struct krt_config *n, struct krt_config *o);
135 
136 void krt_sys_preconfig(struct config *);
137 void krt_sys_postconfig(struct krt_config *);
138 void krt_sys_init_config(struct krt_config *);
139 void krt_sys_copy_config(struct krt_config *, struct krt_config *);
140 
141 int  krt_capable(rte *e);
142 void krt_do_scan(struct krt_proto *);
143 void krt_replace_rte(struct krt_proto *p, net *n, rte *new, rte *old);
144 int krt_sys_get_attr(const eattr *a, byte *buf, int buflen);
145 
146 
147 /* kif sysdep */
148 
149 void kif_sys_init(struct kif_proto *);
150 void kif_sys_start(struct kif_proto *);
151 void kif_sys_shutdown(struct kif_proto *);
152 int kif_sys_reconfigure(struct kif_proto *, struct kif_config *, struct kif_config *);
153 
154 void kif_sys_init_config(struct kif_config *);
155 void kif_sys_copy_config(struct kif_config *, struct kif_config *);
156 
157 void kif_do_scan(struct kif_proto *);
158 
159 int kif_update_sysdep_addr(struct iface *i);
160 
161 #endif
162