2 * BIRD -- Router Advertisement
5 * Can be freely distributed and used under the terms of the GNU GPL.
13 * DOC: Router Advertisements
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 * The protocol does not import or export any routes.
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.
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.
33 * 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.
40 * Supported standards:
41 * - RFC 4861 - main RA standard
42 * - RFC 6106 - DNS extensions (RDDNS, DNSSL)
48 struct radv_iface
*ifa
= tm
->data
;
49 struct proto_radv
*ra
= ifa
->ra
;
51 RADV_TRACE(D_EVENTS
, "Timer fired on %s", ifa
->iface
->name
);
57 unsigned after
= ifa
->cf
->min_ra_int
;
58 after
+= random() % (ifa
->cf
->max_ra_int
- ifa
->cf
->min_ra_int
+ 1);
64 after
= MIN(after
, MAX_INITIAL_RTR_ADVERT_INTERVAL
);
66 tm_start(ifa
->timer
, after
);
69 static char* ev_name
[] = { NULL
, "Init", "Change", "RS" };
72 radv_iface_notify(struct radv_iface
*ifa
, int event
)
74 struct proto_radv
*ra
= ifa
->ra
;
79 RADV_TRACE(D_EVENTS
, "Event %s on %s", ev_name
[event
], ifa
->iface
->name
);
86 ifa
->initial
= MAX_INITIAL_RTR_ADVERTISEMENTS
;
94 unsigned delta
= now
- ifa
->last
;
97 if (delta
< ifa
->cf
->min_delay
)
98 after
= ifa
->cf
->min_delay
- delta
;
100 tm_start(ifa
->timer
, after
);
104 radv_iface_notify_all(struct proto_radv
*ra
, int event
)
106 struct radv_iface
*ifa
;
108 WALK_LIST(ifa
, ra
->iface_list
)
109 radv_iface_notify(ifa
, event
);
113 static struct radv_iface
*
114 radv_iface_find(struct proto_radv
*ra
, struct iface
*what
)
116 struct radv_iface
*ifa
;
118 WALK_LIST(ifa
, ra
->iface_list
)
119 if (ifa
->iface
== what
)
126 radv_iface_add(struct object_lock
*lock
)
128 struct radv_iface
*ifa
= lock
->data
;
129 struct proto_radv
*ra
= ifa
->ra
;
131 if (! radv_sk_open(ifa
))
133 log(L_ERR
"%s: Socket open failed on interface %s", ra
->p
.name
, ifa
->iface
->name
);
137 radv_iface_notify(ifa
, RA_EV_INIT
);
140 static inline struct ifa
*
141 find_lladdr(struct iface
*iface
)
144 WALK_LIST(a
, iface
->addrs
)
145 if (a
->scope
== SCOPE_LINK
)
152 radv_iface_new(struct proto_radv
*ra
, struct iface
*iface
, struct radv_iface_config
*cf
)
154 pool
*pool
= ra
->p
.pool
;
155 struct radv_iface
*ifa
;
157 RADV_TRACE(D_EVENTS
, "Adding interface %s", iface
->name
);
159 ifa
= mb_allocz(pool
, sizeof(struct radv_iface
));
164 add_tail(&ra
->iface_list
, NODE ifa
);
166 ifa
->addr
= find_lladdr(iface
);
169 log(L_ERR
"%s: Cannot find link-locad addr on interface %s", ra
->p
.name
, iface
->name
);
173 timer
*tm
= tm_new(pool
);
174 tm
->hook
= radv_timer
;
180 struct object_lock
*lock
= olock_new(pool
);
181 lock
->addr
= IPA_NONE
;
182 lock
->type
= OBJLOCK_IP
;
183 lock
->port
= ICMPV6_PROTO
;
186 lock
->hook
= radv_iface_add
;
193 radv_iface_remove(struct radv_iface
*ifa
)
195 struct proto_radv
*ra
= ifa
->ra
;
196 RADV_TRACE(D_EVENTS
, "Removing interface %s", ifa
->iface
->name
);
208 radv_if_notify(struct proto
*p
, unsigned flags
, struct iface
*iface
)
210 struct proto_radv
*ra
= (struct proto_radv
*) p
;
211 struct radv_config
*cf
= (struct radv_config
*) (p
->cf
);
213 if (iface
->flags
& IF_IGNORE
)
216 if (flags
& IF_CHANGE_UP
)
218 struct radv_iface_config
*ic
= (struct radv_iface_config
*)
219 iface_patt_find(&cf
->patt_list
, iface
, NULL
);
222 radv_iface_new(ra
, iface
, ic
);
227 struct radv_iface
*ifa
= radv_iface_find(ra
, iface
);
231 if (flags
& IF_CHANGE_DOWN
)
233 radv_iface_remove(ifa
);
237 if ((flags
& IF_CHANGE_LINK
) && (iface
->flags
& IF_LINK_UP
))
238 radv_iface_notify(ifa
, RA_EV_INIT
);
242 radv_ifa_notify(struct proto
*p
, unsigned flags
, struct ifa
*a
)
244 struct proto_radv
*ra
= (struct proto_radv
*) p
;
246 if (a
->flags
& IA_SECONDARY
)
249 if (a
->scope
<= SCOPE_LINK
)
252 struct radv_iface
*ifa
= radv_iface_find(ra
, a
->iface
);
255 radv_iface_notify(ifa
, RA_EV_CHANGE
);
258 static inline int radv_net_match_trigger(struct radv_config
*cf
, net
*n
)
260 return cf
->trigger_valid
&&
261 (n
->n
.pxlen
== cf
->trigger_pxlen
) &&
262 ipa_equal(n
->n
.prefix
, cf
->trigger_prefix
);
266 radv_import_control(struct proto
*p
, rte
**new, ea_list
**attrs UNUSED
, struct linpool
*pool UNUSED
)
268 // struct proto_radv *ra = (struct proto_radv *) p;
269 struct radv_config
*cf
= (struct radv_config
*) (p
->cf
);
271 if (radv_net_match_trigger(cf
, (*new)->net
))
278 radv_rt_notify(struct proto
*p
, rtable
*tbl UNUSED
, net
*n
, rte
*new, rte
*old UNUSED
, ea_list
*attrs UNUSED
)
280 struct proto_radv
*ra
= (struct proto_radv
*) p
;
281 struct radv_config
*cf
= (struct radv_config
*) (p
->cf
);
283 if (radv_net_match_trigger(cf
, n
))
285 u8 old_active
= ra
->active
;
288 if (ra
->active
== old_active
)
292 RADV_TRACE(D_EVENTS
, "Triggered");
294 RADV_TRACE(D_EVENTS
, "Suppressed");
296 radv_iface_notify_all(ra
, RA_EV_CHANGE
);
301 radv_check_active(struct proto_radv
*ra
)
303 struct radv_config
*cf
= (struct radv_config
*) (ra
->p
.cf
);
305 if (! cf
->trigger_valid
)
308 return rt_examine(ra
->p
.table
, cf
->trigger_prefix
, cf
->trigger_pxlen
,
309 &(ra
->p
), ra
->p
.cf
->out_filter
);
312 static struct proto
*
313 radv_init(struct proto_config
*c
)
315 struct proto
*p
= proto_new(c
, sizeof(struct proto_radv
));
317 p
->accept_ra_types
= RA_OPTIMAL
;
318 p
->import_control
= radv_import_control
;
319 p
->rt_notify
= radv_rt_notify
;
320 p
->if_notify
= radv_if_notify
;
321 p
->ifa_notify
= radv_ifa_notify
;
326 radv_start(struct proto
*p
)
328 struct proto_radv
*ra
= (struct proto_radv
*) p
;
329 struct radv_config
*cf
= (struct radv_config
*) (p
->cf
);
331 init_list(&(ra
->iface_list
));
332 ra
->active
= !cf
->trigger_valid
;
338 radv_iface_shutdown(struct radv_iface
*ifa
)
341 radv_send_ra(ifa
, 1);
345 radv_shutdown(struct proto
*p
)
347 struct proto_radv
*ra
= (struct proto_radv
*) p
;
349 struct radv_iface
*ifa
;
350 WALK_LIST(ifa
, ra
->iface_list
)
351 radv_iface_shutdown(ifa
);
357 radv_reconfigure(struct proto
*p
, struct proto_config
*c
)
359 struct proto_radv
*ra
= (struct proto_radv
*) p
;
360 // struct radv_config *old = (struct radv_config *) (p->cf);
361 struct radv_config
*new = (struct radv_config
*) c
;
364 * The question is why there is a reconfigure function for RAdv if
365 * it has almost none internal state so restarting the protocol
366 * would probably suffice. One small reason is that restarting the
367 * protocol would lead to sending a RA with Router Lifetime 0
368 * causing nodes to temporary remove their default routes.
371 p
->cf
= c
; /* radv_check_active() requires proper p->cf */
372 ra
->active
= radv_check_active(ra
);
375 WALK_LIST(iface
, iface_list
)
377 struct radv_iface
*ifa
= radv_iface_find(ra
, iface
);
378 struct radv_iface_config
*ic
= (struct radv_iface_config
*)
379 iface_patt_find(&new->patt_list
, iface
, NULL
);
385 /* We cheat here - always notify the change even if there isn't
386 any. That would leads just to a few unnecessary RAs. */
387 radv_iface_notify(ifa
, RA_EV_CHANGE
);
392 radv_iface_shutdown(ifa
);
393 radv_iface_remove(ifa
);
397 radv_iface_new(ra
, iface
, ic
);
404 radv_copy_config(struct proto_config
*dest
, struct proto_config
*src
)
406 struct radv_config
*d
= (struct radv_config
*) dest
;
407 struct radv_config
*s
= (struct radv_config
*) src
;
409 /* We clean up patt_list, ifaces are non-sharable */
410 init_list(&d
->patt_list
);
412 /* We copy pref_list, shallow copy suffices */
413 cfg_copy_list(&d
->pref_list
, &s
->pref_list
, sizeof(struct radv_prefix_config
));
417 radv_get_status(struct proto
*p
, byte
*buf
)
419 struct proto_radv
*ra
= (struct proto_radv
*) p
;
422 strcpy(buf
, "Suppressed");
425 struct protocol proto_radv
= {
427 .template = "radv%d",
430 .shutdown
= radv_shutdown
,
431 .reconfigure
= radv_reconfigure
,
432 .copy_config
= radv_copy_config
,
433 .get_status
= radv_get_status