1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
/*
* BIRD -- Router Advertisement
*
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#include <stdlib.h>
#include "radv.h"
/**
* DOC: Router Advertisements
*
* The RAdv protocol is implemented in two files: |radv.c| containing
* the interface with BIRD core and the protocol logic and |packets.c|
* handling low level protocol stuff (RX, TX and packet formats).
* The protocol does not import or export any routes.
*
* The RAdv is structured in the usual way - for each handled interface
* there is a structure &radv_iface that contains a state related to
* that interface together with its resources (a socket, a timer).
* There is also a prepared RA stored in a TX buffer of the socket
* associated with an iface. These iface structures are created
* and removed according to iface events from BIRD core handled by
* radv_if_notify() callback.
*
* The main logic of RAdv consists of two functions:
* radv_iface_notify(), which processes asynchronous events (specified
* by RA_EV_* codes), and radv_timer(), which triggers sending RAs and
* computes the next timeout.
*/
static void
radv_timer(timer *tm)
{
struct radv_iface *ifa = tm->data;
struct proto_radv *ra = ifa->ra;
RADV_TRACE(D_EVENTS, "Timer fired on %s", ifa->iface->name);
radv_send_ra(ifa, 0);
/* Update timer */
ifa->last = now;
unsigned after = ifa->cf->min_ra_int;
after += random() % (ifa->cf->max_ra_int - ifa->cf->min_ra_int + 1);
if (ifa->initial)
ifa->initial--;
if (ifa->initial)
after = MIN(after, MAX_INITIAL_RTR_ADVERT_INTERVAL);
tm_start(ifa->timer, after);
}
static char* ev_name[] = { NULL, "Init", "Change", "RS" };
void
radv_iface_notify(struct radv_iface *ifa, int event)
{
struct proto_radv *ra = ifa->ra;
if (!ifa->sk)
return;
RADV_TRACE(D_EVENTS, "Event %s on %s", ev_name[event], ifa->iface->name);
switch (event)
{
case RA_EV_CHANGE:
ifa->plen = 0;
case RA_EV_INIT:
ifa->initial = MAX_INITIAL_RTR_ADVERTISEMENTS;
break;
case RA_EV_RS:
break;
}
/* Update timer */
unsigned delta = now - ifa->last;
unsigned after = 0;
if (delta < ifa->cf->min_delay)
after = ifa->cf->min_delay - delta;
tm_start(ifa->timer, after);
}
static struct radv_iface *
radv_iface_find(struct proto_radv *ra, struct iface *what)
{
struct radv_iface *ifa;
WALK_LIST(ifa, ra->iface_list)
if (ifa->iface == what)
return ifa;
return NULL;
}
static void
radv_iface_add(struct object_lock *lock)
{
struct radv_iface *ifa = lock->data;
struct proto_radv *ra = ifa->ra;
if (! radv_sk_open(ifa))
{
log(L_ERR "%s: Socket open failed on interface %s", ra->p.name, ifa->iface->name);
return;
}
radv_iface_notify(ifa, RA_EV_INIT);
}
static inline struct ifa *
find_lladdr(struct iface *iface)
{
struct ifa *a;
WALK_LIST(a, iface->addrs)
if (a->scope == SCOPE_LINK)
return a;
return NULL;
}
static void
radv_iface_new(struct proto_radv *ra, struct iface *iface, struct radv_iface_config *cf)
{
pool *pool = ra->p.pool;
struct radv_iface *ifa;
RADV_TRACE(D_EVENTS, "Adding interface %s", iface->name);
ifa = mb_allocz(pool, sizeof(struct radv_iface));
ifa->ra = ra;
ifa->cf = cf;
ifa->iface = iface;
add_tail(&ra->iface_list, NODE ifa);
ifa->addr = find_lladdr(iface);
if (!ifa->addr)
{
log(L_ERR "%s: Cannot find link-locad addr on interface %s", ra->p.name, iface->name);
return;
}
timer *tm = tm_new(pool);
tm->hook = radv_timer;
tm->data = ifa;
tm->randomize = 0;
tm->recurrent = 0;
ifa->timer = tm;
struct object_lock *lock = olock_new(pool);
lock->addr = IPA_NONE;
lock->type = OBJLOCK_IP;
lock->port = ICMPV6_PROTO;
lock->iface = iface;
lock->data = ifa;
lock->hook = radv_iface_add;
ifa->lock = lock;
olock_acquire(lock);
}
static void
radv_iface_remove(struct radv_iface *ifa)
{
struct proto_radv *ra = ifa->ra;
RADV_TRACE(D_EVENTS, "Removing interface %s", ifa->iface->name);
rem_node(NODE ifa);
rfree(ifa->sk);
rfree(ifa->timer);
rfree(ifa->lock);
mb_free(ifa);
}
static void
radv_if_notify(struct proto *p, unsigned flags, struct iface *iface)
{
struct proto_radv *ra = (struct proto_radv *) p;
struct radv_config *cf = (struct radv_config *) (p->cf);
if (iface->flags & IF_IGNORE)
return;
if (flags & IF_CHANGE_UP)
{
struct radv_iface_config *ic = (struct radv_iface_config *)
iface_patt_find(&cf->patt_list, iface, NULL);
if (ic)
radv_iface_new(ra, iface, ic);
return;
}
struct radv_iface *ifa = radv_iface_find(ra, iface);
if (!ifa)
return;
if (flags & IF_CHANGE_DOWN)
{
radv_iface_remove(ifa);
return;
}
if ((flags & IF_CHANGE_LINK) && (iface->flags & IF_LINK_UP))
radv_iface_notify(ifa, RA_EV_INIT);
}
static void
radv_ifa_notify(struct proto *p, unsigned flags, struct ifa *a)
{
struct proto_radv *ra = (struct proto_radv *) p;
if (a->flags & IA_SECONDARY)
return;
if (a->scope <= SCOPE_LINK)
return;
struct radv_iface *ifa = radv_iface_find(ra, a->iface);
if (ifa)
radv_iface_notify(ifa, RA_EV_CHANGE);
}
static struct proto *
radv_init(struct proto_config *c)
{
struct proto *p = proto_new(c, sizeof(struct proto_radv));
p->if_notify = radv_if_notify;
p->ifa_notify = radv_ifa_notify;
return p;
}
static int
radv_start(struct proto *p)
{
struct proto_radv *ra = (struct proto_radv *) p;
// struct radv_config *cf = (struct radv_config *) (p->cf);
init_list(&(ra->iface_list));
return PS_UP;
}
static inline void
radv_iface_shutdown(struct radv_iface *ifa)
{
if (ifa->sk)
radv_send_ra(ifa, 1);
}
static int
radv_shutdown(struct proto *p)
{
struct proto_radv *ra = (struct proto_radv *) p;
struct radv_iface *ifa;
WALK_LIST(ifa, ra->iface_list)
radv_iface_shutdown(ifa);
return PS_DOWN;
}
static int
radv_reconfigure(struct proto *p, struct proto_config *c)
{
struct proto_radv *ra = (struct proto_radv *) p;
// struct radv_config *old = (struct radv_config *) (p->cf);
struct radv_config *new = (struct radv_config *) c;
/*
* The question is why there is a reconfigure function for RAdv if
* it has almost none internal state so restarting the protocol
* would probably suffice. One small reason is that restarting the
* protocol would lead to sending a RA with Router Lifetime 0
* causing nodes to temporary remove their default routes.
*/
struct iface *iface;
WALK_LIST(iface, iface_list)
{
struct radv_iface *ifa = radv_iface_find(ra, iface);
struct radv_iface_config *ic = (struct radv_iface_config *)
iface_patt_find(&new->patt_list, iface, NULL);
if (ifa && ic)
{
ifa->cf = ic;
/* We cheat here - always notify the change even if there isn't
any. That would leads just to a few unnecessary RAs. */
radv_iface_notify(ifa, RA_EV_CHANGE);
}
if (ifa && !ic)
{
radv_iface_shutdown(ifa);
radv_iface_remove(ifa);
}
if (!ifa && ic)
radv_iface_new(ra, iface, ic);
}
return 1;
}
static void
radv_copy_config(struct proto_config *dest, struct proto_config *src)
{
struct radv_config *d = (struct radv_config *) dest;
struct radv_config *s = (struct radv_config *) src;
/* We clean up patt_list, ifaces are non-sharable */
init_list(&d->patt_list);
/* We copy pref_list, shallow copy suffices */
cfg_copy_list(&d->pref_list, &s->pref_list, sizeof(struct radv_prefix_config));
}
struct protocol proto_radv = {
.name = "RAdv",
.template = "radv%d",
.init = radv_init,
.start = radv_start,
.shutdown = radv_shutdown,
.reconfigure = radv_reconfigure,
.copy_config = radv_copy_config
};
|