Statistics
| Branch: | Revision:

iof-bird-daemon / proto / radv / radv.c @ 153f02da

History | View | Annotate | Download (10.5 KB)

1 93e868c7 Ondrej Zajicek
/*
2
 *        BIRD -- Router Advertisement
3
 *
4
 *
5
 *        Can be freely distributed and used under the terms of the GNU GPL.
6
 */
7
8
9
#include <stdlib.h>
10
#include "radv.h"
11
12
/**
13
 * DOC: Router Advertisements
14
 *
15
 * The RAdv protocol is implemented in two files: |radv.c| containing
16
 * the interface with BIRD core and the protocol logic and |packets.c|
17
 * handling low level protocol stuff (RX, TX and packet formats).
18 1ec52253 Ondrej Zajicek
 * The protocol does not export any routes.
19 93e868c7 Ondrej Zajicek
 *
20
 * The RAdv is structured in the usual way - for each handled interface
21
 * there is a structure &radv_iface that contains a state related to
22
 * that interface together with its resources (a socket, a timer).
23
 * There is also a prepared RA stored in a TX buffer of the socket
24
 * associated with an iface. These iface structures are created
25
 * and removed according to iface events from BIRD core handled by
26
 * radv_if_notify() callback.
27
 *
28
 * The main logic of RAdv consists of two functions:
29
 * radv_iface_notify(), which processes asynchronous events (specified
30
 * by RA_EV_* codes), and radv_timer(), which triggers sending RAs and
31
 * computes the next timeout.
32 fc06fb62 Ondrej Zajicek
 *
33 36da2857 Ondrej Zajicek
 * The RAdv protocol could receive routes (through
34
 * radv_import_control() and radv_rt_notify()), but only the
35
 * configured trigger route is tracked (in &active var).  When a radv
36
 * protocol is reconfigured, the connected routing table is examined
37
 * (in radv_check_active()) to have proper &active value in case of
38
 * the specified trigger prefix was changed.
39
 *
40 fc06fb62 Ondrej Zajicek
 * Supported standards:
41
 * - RFC 4861 - main RA standard
42
 * - RFC 6106 - DNS extensions (RDDNS, DNSSL)
43 75148289 Ondrej Zajicek
 * - RFC 4191 (partial) - Default Router Preference
44 93e868c7 Ondrej Zajicek
 */
45
46
static void
47
radv_timer(timer *tm)
48
{
49
  struct radv_iface *ifa = tm->data;
50 5a41eed2 Michal 'vorner' Vaner
  struct radv_proto *p = ifa->ra;
51 93e868c7 Ondrej Zajicek
52
  RADV_TRACE(D_EVENTS, "Timer fired on %s", ifa->iface->name);
53
54
  radv_send_ra(ifa, 0);
55
56
  /* Update timer */
57
  ifa->last = now;
58
  unsigned after = ifa->cf->min_ra_int;
59
  after += random() % (ifa->cf->max_ra_int - ifa->cf->min_ra_int + 1);
60
61
  if (ifa->initial)
62
    ifa->initial--;
63
64
  if (ifa->initial)
65
    after = MIN(after, MAX_INITIAL_RTR_ADVERT_INTERVAL);
66
67
  tm_start(ifa->timer, after);
68
}
69
70
static char* ev_name[] = { NULL, "Init", "Change", "RS" };
71
72
void
73
radv_iface_notify(struct radv_iface *ifa, int event)
74
{
75 5a41eed2 Michal 'vorner' Vaner
  struct radv_proto *p = ifa->ra;
76 93e868c7 Ondrej Zajicek
77
  if (!ifa->sk)
78
    return;
79
80
  RADV_TRACE(D_EVENTS, "Event %s on %s", ev_name[event], ifa->iface->name);
81
82
  switch (event)
83
  {
84
  case RA_EV_CHANGE:
85
    ifa->plen = 0;
86
  case RA_EV_INIT:
87
    ifa->initial = MAX_INITIAL_RTR_ADVERTISEMENTS;
88
    break;
89
90
  case RA_EV_RS:
91
    break;
92
  }
93
94
  /* Update timer */
95
  unsigned delta = now - ifa->last;
96
  unsigned after = 0;
97
98
  if (delta < ifa->cf->min_delay)
99
    after = ifa->cf->min_delay - delta;
100
101
  tm_start(ifa->timer, after);
102
}
103
104 36da2857 Ondrej Zajicek
static void
105 5a41eed2 Michal 'vorner' Vaner
radv_iface_notify_all(struct radv_proto *p, int event)
106 36da2857 Ondrej Zajicek
{
107
  struct radv_iface *ifa;
108
109 5a41eed2 Michal 'vorner' Vaner
  WALK_LIST(ifa, p->iface_list)
110 36da2857 Ondrej Zajicek
    radv_iface_notify(ifa, event);
111
}
112
113
114 93e868c7 Ondrej Zajicek
static struct radv_iface *
115 5a41eed2 Michal 'vorner' Vaner
radv_iface_find(struct radv_proto *p, struct iface *what)
116 93e868c7 Ondrej Zajicek
{
117
  struct radv_iface *ifa;
118
119 5a41eed2 Michal 'vorner' Vaner
  WALK_LIST(ifa, p->iface_list)
120 93e868c7 Ondrej Zajicek
    if (ifa->iface == what)
121
      return ifa;
122
123
  return NULL;
124
}
125
126
static void
127
radv_iface_add(struct object_lock *lock)
128
{
129
  struct radv_iface *ifa = lock->data;
130 5a41eed2 Michal 'vorner' Vaner
  struct radv_proto *p = ifa->ra;
131 93e868c7 Ondrej Zajicek
132
  if (! radv_sk_open(ifa))
133
  {
134 5a41eed2 Michal 'vorner' Vaner
    log(L_ERR "%s: Socket open failed on interface %s", p->p.name, ifa->iface->name);
135 93e868c7 Ondrej Zajicek
    return;
136
  }
137
138
  radv_iface_notify(ifa, RA_EV_INIT);
139
}
140
141
static void
142 5a41eed2 Michal 'vorner' Vaner
radv_iface_new(struct radv_proto *p, struct iface *iface, struct radv_iface_config *cf)
143 93e868c7 Ondrej Zajicek
{
144 5a41eed2 Michal 'vorner' Vaner
  pool *pool = p->p.pool;
145 93e868c7 Ondrej Zajicek
  struct radv_iface *ifa;
146
147
  RADV_TRACE(D_EVENTS, "Adding interface %s", iface->name);
148
149
  ifa = mb_allocz(pool, sizeof(struct radv_iface));
150 5a41eed2 Michal 'vorner' Vaner
  ifa->ra = p;
151 93e868c7 Ondrej Zajicek
  ifa->cf = cf;
152
  ifa->iface = iface;
153 153f02da Ondrej Zajicek (work)
  ifa->addr = iface->llv6;
154 93e868c7 Ondrej Zajicek
155 5a41eed2 Michal 'vorner' Vaner
  add_tail(&p->iface_list, NODE ifa);
156 93e868c7 Ondrej Zajicek
157
  timer *tm = tm_new(pool);
158
  tm->hook = radv_timer;
159
  tm->data = ifa;
160
  tm->randomize = 0;
161
  tm->recurrent = 0;
162
  ifa->timer = tm;
163
164
  struct object_lock *lock = olock_new(pool);
165
  lock->addr = IPA_NONE;
166
  lock->type = OBJLOCK_IP;
167
  lock->port = ICMPV6_PROTO;
168
  lock->iface = iface;
169
  lock->data = ifa;
170
  lock->hook = radv_iface_add;
171
  ifa->lock = lock;
172
173
  olock_acquire(lock);
174
}
175
176
static void
177
radv_iface_remove(struct radv_iface *ifa)
178
{
179 5a41eed2 Michal 'vorner' Vaner
  struct radv_proto *p = ifa->ra;
180 93e868c7 Ondrej Zajicek
  RADV_TRACE(D_EVENTS, "Removing interface %s", ifa->iface->name);
181
182
  rem_node(NODE ifa);
183
184
  rfree(ifa->sk);
185
  rfree(ifa->timer);
186
  rfree(ifa->lock);
187
188
  mb_free(ifa);
189
}
190
191
static void
192 5a41eed2 Michal 'vorner' Vaner
radv_if_notify(struct proto *P, unsigned flags, struct iface *iface)
193 2bbc3083 Ondrej Zajicek
{
194 5a41eed2 Michal 'vorner' Vaner
  struct radv_proto *p = (struct radv_proto *) P;
195
  struct radv_config *cf = (struct radv_config *) (P->cf);
196 93e868c7 Ondrej Zajicek
197
  if (iface->flags & IF_IGNORE)
198
    return;
199
200
  if (flags & IF_CHANGE_UP)
201
  {
202 153f02da Ondrej Zajicek (work)
    struct radv_iface_config *ic = (void *) iface_patt_find(&cf->patt_list, iface, NULL);
203
204
    /* Ignore non-multicast ifaces */
205
    if (!(iface->flags & IF_MULTICAST))
206
      return;
207
208
    /* Ignore ifaces without link-local address */
209
    if (!iface->llv6)
210
      return;
211 93e868c7 Ondrej Zajicek
212
    if (ic)
213 5a41eed2 Michal 'vorner' Vaner
      radv_iface_new(p, iface, ic);
214 93e868c7 Ondrej Zajicek
215
    return;
216
  }
217
218 5a41eed2 Michal 'vorner' Vaner
  struct radv_iface *ifa = radv_iface_find(p, iface);
219 93e868c7 Ondrej Zajicek
  if (!ifa)
220
    return;
221
222
  if (flags & IF_CHANGE_DOWN)
223
  {
224
    radv_iface_remove(ifa);
225
    return;
226
  }
227
228
  if ((flags & IF_CHANGE_LINK) && (iface->flags & IF_LINK_UP))
229
    radv_iface_notify(ifa, RA_EV_INIT);
230
}
231
232
static void
233 5a41eed2 Michal 'vorner' Vaner
radv_ifa_notify(struct proto *P, unsigned flags UNUSED, struct ifa *a)
234 93e868c7 Ondrej Zajicek
{
235 5a41eed2 Michal 'vorner' Vaner
  struct radv_proto *p = (struct radv_proto *) P;
236 93e868c7 Ondrej Zajicek
237
  if (a->flags & IA_SECONDARY)
238
    return;
239
240
  if (a->scope <= SCOPE_LINK)
241
    return;
242
243 5a41eed2 Michal 'vorner' Vaner
  struct radv_iface *ifa = radv_iface_find(p, a->iface);
244 93e868c7 Ondrej Zajicek
245
  if (ifa)
246
    radv_iface_notify(ifa, RA_EV_CHANGE);
247
}
248
249 f4a60a9b Ondrej Zajicek (work)
static inline int
250
radv_trigger_valid(struct radv_config *cf)
251 36da2857 Ondrej Zajicek
{
252 f4a60a9b Ondrej Zajicek (work)
  return cf->trigger.type != 0;
253
}
254
255
static inline int
256
radv_net_match_trigger(struct radv_config *cf, net *n)
257
{
258
  return radv_trigger_valid(cf) && net_equal(n->n.addr, &cf->trigger);
259 36da2857 Ondrej Zajicek
}
260
261
int
262 5a41eed2 Michal 'vorner' Vaner
radv_import_control(struct proto *P, rte **new, ea_list **attrs UNUSED, struct linpool *pool UNUSED)
263 36da2857 Ondrej Zajicek
{
264 5a41eed2 Michal 'vorner' Vaner
  // struct radv_proto *p = (struct radv_proto *) P;
265
  struct radv_config *cf = (struct radv_config *) (P->cf);
266 36da2857 Ondrej Zajicek
267
  if (radv_net_match_trigger(cf, (*new)->net))
268
    return RIC_PROCESS;
269
270
  return RIC_DROP;
271
}
272
273
static void
274 5a41eed2 Michal 'vorner' Vaner
radv_rt_notify(struct proto *P, struct channel *ch UNUSED, net *n, rte *new, rte *old UNUSED, ea_list *attrs UNUSED)
275 36da2857 Ondrej Zajicek
{
276 5a41eed2 Michal 'vorner' Vaner
  struct radv_proto *p = (struct radv_proto *) P;
277
  struct radv_config *cf = (struct radv_config *) (P->cf);
278 36da2857 Ondrej Zajicek
279
  if (radv_net_match_trigger(cf, n))
280
  {
281 5a41eed2 Michal 'vorner' Vaner
    u8 old_active = p->active;
282
    p->active = !!new;
283 36da2857 Ondrej Zajicek
284 5a41eed2 Michal 'vorner' Vaner
    if (p->active == old_active)
285 36da2857 Ondrej Zajicek
      return;
286
287 5a41eed2 Michal 'vorner' Vaner
    if (p->active)
288 36da2857 Ondrej Zajicek
      RADV_TRACE(D_EVENTS, "Triggered");
289
    else
290
      RADV_TRACE(D_EVENTS, "Suppressed");
291
292 5a41eed2 Michal 'vorner' Vaner
    radv_iface_notify_all(p, RA_EV_CHANGE);
293 36da2857 Ondrej Zajicek
  }
294
}
295
296
static int
297 5a41eed2 Michal 'vorner' Vaner
radv_check_active(struct radv_proto *p)
298 36da2857 Ondrej Zajicek
{
299 5a41eed2 Michal 'vorner' Vaner
  struct radv_config *cf = (struct radv_config *) (p->p.cf);
300 36da2857 Ondrej Zajicek
301 f4a60a9b Ondrej Zajicek (work)
  if (!radv_trigger_valid(cf))
302 36da2857 Ondrej Zajicek
    return 1;
303
304 5a41eed2 Michal 'vorner' Vaner
  struct channel *c = p->p.main_channel;
305
  return rt_examine(c->table, &cf->trigger, &p->p, c->out_filter);
306 f4a60a9b Ondrej Zajicek (work)
}
307
308
static void
309
radv_postconfig(struct proto_config *CF)
310
{
311
  // struct radv_config *cf = (void *) CF;
312
313
  /* Define default channel */
314
  if (EMPTY_LIST(CF->channels))
315
    channel_config_new(NULL, NET_IP6, CF);
316 36da2857 Ondrej Zajicek
}
317
318 93e868c7 Ondrej Zajicek
static struct proto *
319 f4a60a9b Ondrej Zajicek (work)
radv_init(struct proto_config *CF)
320 93e868c7 Ondrej Zajicek
{
321 5a41eed2 Michal 'vorner' Vaner
  struct proto *P = proto_new(CF);
322 f4a60a9b Ondrej Zajicek (work)
323 5a41eed2 Michal 'vorner' Vaner
  P->main_channel = proto_add_channel(P, proto_cf_main_channel(CF));
324 93e868c7 Ondrej Zajicek
325 5a41eed2 Michal 'vorner' Vaner
  P->import_control = radv_import_control;
326
  P->rt_notify = radv_rt_notify;
327
  P->if_notify = radv_if_notify;
328
  P->ifa_notify = radv_ifa_notify;
329 f4a60a9b Ondrej Zajicek (work)
330 5a41eed2 Michal 'vorner' Vaner
  return P;
331 93e868c7 Ondrej Zajicek
}
332
333
static int
334 5a41eed2 Michal 'vorner' Vaner
radv_start(struct proto *P)
335 93e868c7 Ondrej Zajicek
{
336 5a41eed2 Michal 'vorner' Vaner
  struct radv_proto *p = (struct radv_proto *) P;
337
  struct radv_config *cf = (struct radv_config *) (P->cf);
338 93e868c7 Ondrej Zajicek
339 5a41eed2 Michal 'vorner' Vaner
  init_list(&(p->iface_list));
340
  p->active = !radv_trigger_valid(cf);
341 93e868c7 Ondrej Zajicek
342
  return PS_UP;
343
}
344
345
static inline void
346
radv_iface_shutdown(struct radv_iface *ifa)
347
{
348
  if (ifa->sk)
349
    radv_send_ra(ifa, 1);
350
}
351
352
static int
353 5a41eed2 Michal 'vorner' Vaner
radv_shutdown(struct proto *P)
354 93e868c7 Ondrej Zajicek
{
355 5a41eed2 Michal 'vorner' Vaner
  struct radv_proto *p = (struct radv_proto *) P;
356 93e868c7 Ondrej Zajicek
357
  struct radv_iface *ifa;
358 5a41eed2 Michal 'vorner' Vaner
  WALK_LIST(ifa, p->iface_list)
359 93e868c7 Ondrej Zajicek
    radv_iface_shutdown(ifa);
360
361
  return PS_DOWN;
362
}
363
364
static int
365 5a41eed2 Michal 'vorner' Vaner
radv_reconfigure(struct proto *P, struct proto_config *CF)
366 93e868c7 Ondrej Zajicek
{
367 5a41eed2 Michal 'vorner' Vaner
  struct radv_proto *p = (struct radv_proto *) P;
368 93e868c7 Ondrej Zajicek
  // struct radv_config *old = (struct radv_config *) (p->cf);
369 f4a60a9b Ondrej Zajicek (work)
  struct radv_config *new = (struct radv_config *) CF;
370 93e868c7 Ondrej Zajicek
371 2bbc3083 Ondrej Zajicek
  /*
372 93e868c7 Ondrej Zajicek
   * The question is why there is a reconfigure function for RAdv if
373
   * it has almost none internal state so restarting the protocol
374
   * would probably suffice. One small reason is that restarting the
375
   * protocol would lead to sending a RA with Router Lifetime 0
376
   * causing nodes to temporary remove their default routes.
377
   */
378
379 5a41eed2 Michal 'vorner' Vaner
  if (!proto_configure_channel(P, &P->main_channel, proto_cf_main_channel(CF)))
380 f4a60a9b Ondrej Zajicek (work)
    return 0;
381
382 69f73992 Ondrej Zajicek (work)
  P->cf = CF; /* radv_check_active() requires proper P->cf */
383 5a41eed2 Michal 'vorner' Vaner
  p->active = radv_check_active(p);
384 36da2857 Ondrej Zajicek
385 93e868c7 Ondrej Zajicek
  struct iface *iface;
386
  WALK_LIST(iface, iface_list)
387
  {
388 153f02da Ondrej Zajicek (work)
    if (!(iface->flags & IF_UP))
389
      continue;
390
391
    /* Ignore non-multicast ifaces */
392
    if (!(iface->flags & IF_MULTICAST))
393
      continue;
394
395
    /* Ignore ifaces without link-local address */
396
    if (!iface->llv6)
397
      continue;
398
399 5a41eed2 Michal 'vorner' Vaner
    struct radv_iface *ifa = radv_iface_find(p, iface);
400 93e868c7 Ondrej Zajicek
    struct radv_iface_config *ic = (struct radv_iface_config *)
401
      iface_patt_find(&new->patt_list, iface, NULL);
402
403
    if (ifa && ic)
404
    {
405
      ifa->cf = ic;
406
407
      /* We cheat here - always notify the change even if there isn't
408
         any. That would leads just to a few unnecessary RAs. */
409
      radv_iface_notify(ifa, RA_EV_CHANGE);
410
    }
411
412
    if (ifa && !ic)
413
    {
414
      radv_iface_shutdown(ifa);
415
      radv_iface_remove(ifa);
416
    }
417
418
    if (!ifa && ic)
419 5a41eed2 Michal 'vorner' Vaner
      radv_iface_new(p, iface, ic);
420 93e868c7 Ondrej Zajicek
  }
421
422
  return 1;
423
}
424
425 a7f23f58 Ondrej Zajicek
static void
426
radv_copy_config(struct proto_config *dest, struct proto_config *src)
427
{
428
  struct radv_config *d = (struct radv_config *) dest;
429
  struct radv_config *s = (struct radv_config *) src;
430
431
  /* We clean up patt_list, ifaces are non-sharable */
432
  init_list(&d->patt_list);
433
434
  /* We copy pref_list, shallow copy suffices */
435 2779d50a Ondrej Zajicek
  cfg_copy_list(&d->pref_list, &s->pref_list, sizeof(struct radv_prefix_config));
436 a7f23f58 Ondrej Zajicek
}
437
438 36da2857 Ondrej Zajicek
static void
439 5a41eed2 Michal 'vorner' Vaner
radv_get_status(struct proto *P, byte *buf)
440 36da2857 Ondrej Zajicek
{
441 5a41eed2 Michal 'vorner' Vaner
  struct radv_proto *p = (struct radv_proto *) P;
442 36da2857 Ondrej Zajicek
443 5a41eed2 Michal 'vorner' Vaner
  if (!p->active)
444 36da2857 Ondrej Zajicek
    strcpy(buf, "Suppressed");
445
}
446 93e868c7 Ondrej Zajicek
447
struct protocol proto_radv = {
448
  .name =                "RAdv",
449
  .template =                "radv%d",
450 f4a60a9b Ondrej Zajicek (work)
  .channel_mask =        NB_IP6,
451 5a41eed2 Michal 'vorner' Vaner
  .proto_size =                sizeof(struct radv_proto),
452 2bbc3083 Ondrej Zajicek
  .config_size =        sizeof(struct radv_config),
453 f4a60a9b Ondrej Zajicek (work)
  .postconfig =                radv_postconfig,
454 93e868c7 Ondrej Zajicek
  .init =                radv_init,
455
  .start =                radv_start,
456
  .shutdown =                radv_shutdown,
457 a7f23f58 Ondrej Zajicek
  .reconfigure =        radv_reconfigure,
458 36da2857 Ondrej Zajicek
  .copy_config =        radv_copy_config,
459
  .get_status =                radv_get_status
460 93e868c7 Ondrej Zajicek
};