3 * Copyright (C) 2000 Robert Olsson.
4 * Swedish University of Agricultural Sciences
6 * This file is part of GNU Zebra.
8 * GNU Zebra is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 * This work includes work with the following copywrite:
26 * Copyright (C) 1997, 2000 Kunihiro Ishiguro
31 * Thanks to Jens Laas at Swedish University of Agricultural Sciences
32 * for reviewing and tests.
40 #include "sockunion.h"
45 #include "zebra_memory.h"
48 #include "connected.h"
54 #include "lib_errors.h"
56 #include "zebra/interface.h"
57 #include "zebra/rtadv.h"
58 #include "zebra/rib.h"
59 #include "zebra/zserv.h"
60 #include "zebra/redistribute.h"
61 #include "zebra/irdp.h"
62 #include <netinet/ip_icmp.h>
66 #include "sockunion.h"
71 extern struct zebra_privs_t zserv_privs
;
73 struct thread
*t_irdp_raw
;
75 /* Timer interval of irdp. */
76 int irdp_timer_interval
= IRDP_DEFAULT_INTERVAL
;
78 int irdp_sock_init(void)
84 frr_elevate_privs(&zserv_privs
) {
86 sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_ICMP
);
92 zlog_warn("IRDP: can't create irdp socket %s",
93 safe_strerror(save_errno
));
98 ret
= setsockopt(sock
, IPPROTO_IP
, IP_TTL
, (void *)&i
, sizeof(i
));
100 zlog_warn("IRDP: can't do irdp sockopt %s",
101 safe_strerror(errno
));
106 ret
= setsockopt_ifindex(AF_INET
, sock
, 1);
108 zlog_warn("IRDP: can't do irdp sockopt %s",
109 safe_strerror(errno
));
115 thread_add_read(zebrad
.master
, irdp_read_raw
, NULL
, sock
, &t_irdp_raw
);
121 static int get_pref(struct irdp_interface
*irdp
, struct prefix
*p
)
123 struct listnode
*node
;
126 /* Use default preference or use the override pref */
128 if (irdp
->AdvPrefList
== NULL
)
129 return irdp
->Preference
;
131 for (ALL_LIST_ELEMENTS_RO(irdp
->AdvPrefList
, node
, adv
))
132 if (p
->u
.prefix4
.s_addr
== adv
->ip
.s_addr
)
135 return irdp
->Preference
;
138 /* Make ICMP Router Advertisement Message. */
139 static int make_advertisement_packet(struct interface
*ifp
, struct prefix
*p
,
142 struct zebra_if
*zi
= ifp
->info
;
143 struct irdp_interface
*irdp
= zi
->irdp
;
148 pref
= get_pref(irdp
, p
);
150 stream_putc(s
, ICMP_ROUTERADVERT
); /* Type. */
151 stream_putc(s
, 0); /* Code. */
152 stream_putw(s
, 0); /* Checksum. */
153 stream_putc(s
, 1); /* Num address. */
154 stream_putc(s
, 2); /* Address Entry Size. */
156 if (irdp
->flags
& IF_SHUTDOWN
)
159 stream_putw(s
, irdp
->Lifetime
);
161 stream_putl(s
, htonl(p
->u
.prefix4
.s_addr
)); /* Router address. */
162 stream_putl(s
, pref
);
164 /* in_cksum return network byte order value */
166 checksum
= in_cksum(s
->data
, size
);
167 stream_putw_at(s
, 2, htons(checksum
));
172 static void irdp_send(struct interface
*ifp
, struct prefix
*p
, struct stream
*s
)
174 struct zebra_if
*zi
= ifp
->info
;
175 struct irdp_interface
*irdp
= zi
->irdp
;
176 char buf
[PREFIX_STRLEN
];
182 if (!(ifp
->flags
& IFF_UP
))
185 if (irdp
->flags
& IF_BROADCAST
)
186 dst
= INADDR_BROADCAST
;
188 dst
= htonl(INADDR_ALLHOSTS_GROUP
);
190 if (irdp
->flags
& IF_DEBUG_MESSAGES
)
191 zlog_debug("IRDP: TX Advert on %s %s Holdtime=%d Preference=%d",
192 ifp
->name
, prefix2str(p
, buf
, sizeof buf
),
193 irdp
->flags
& IF_SHUTDOWN
? 0 : irdp
->Lifetime
,
196 send_packet(ifp
, s
, dst
, p
, ttl
);
199 static void irdp_advertisement(struct interface
*ifp
, struct prefix
*p
)
203 make_advertisement_packet(ifp
, p
, s
);
204 irdp_send(ifp
, p
, s
);
208 int irdp_send_thread(struct thread
*t_advert
)
211 struct interface
*ifp
= THREAD_ARG(t_advert
);
212 struct zebra_if
*zi
= ifp
->info
;
213 struct irdp_interface
*irdp
= zi
->irdp
;
215 struct listnode
*node
, *nnode
;
216 struct connected
*ifc
;
221 irdp
->flags
&= ~IF_SOLICIT
;
224 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, ifc
)) {
227 if (p
->family
!= AF_INET
)
230 irdp_advertisement(ifp
, p
);
234 tmp
= irdp
->MaxAdvertInterval
- irdp
->MinAdvertInterval
;
235 timer
= random() % (tmp
+ 1);
236 timer
= irdp
->MinAdvertInterval
+ timer
;
238 if (irdp
->irdp_sent
< MAX_INITIAL_ADVERTISEMENTS
239 && timer
> MAX_INITIAL_ADVERT_INTERVAL
)
240 timer
= MAX_INITIAL_ADVERT_INTERVAL
;
242 if (irdp
->flags
& IF_DEBUG_MISC
)
243 zlog_debug("IRDP: New timer for %s set to %u\n", ifp
->name
,
246 irdp
->t_advertise
= NULL
;
247 thread_add_timer(zebrad
.master
, irdp_send_thread
, ifp
, timer
,
252 void irdp_advert_off(struct interface
*ifp
)
254 struct zebra_if
*zi
= ifp
->info
;
255 struct irdp_interface
*irdp
= zi
->irdp
;
256 struct listnode
*node
, *nnode
;
258 struct connected
*ifc
;
264 if (irdp
->t_advertise
)
265 thread_cancel(irdp
->t_advertise
);
266 irdp
->t_advertise
= NULL
;
269 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, ifc
)) {
272 /* Output some packets with Lifetime 0
273 we should add a wait...
276 for (i
= 0; i
< IRDP_LAST_ADVERT_MESSAGES
; i
++) {
278 irdp_advertisement(ifp
, p
);
284 void process_solicit(struct interface
*ifp
)
286 struct zebra_if
*zi
= ifp
->info
;
287 struct irdp_interface
*irdp
= zi
->irdp
;
293 /* When SOLICIT is active we reject further incoming solicits
294 this keeps down the answering rate so we don't have think
295 about DoS attacks here. */
297 if (irdp
->flags
& IF_SOLICIT
)
300 irdp
->flags
|= IF_SOLICIT
;
301 if (irdp
->t_advertise
)
302 thread_cancel(irdp
->t_advertise
);
303 irdp
->t_advertise
= NULL
;
305 timer
= (random() % MAX_RESPONSE_DELAY
) + 1;
307 irdp
->t_advertise
= NULL
;
308 thread_add_timer(zebrad
.master
, irdp_send_thread
, ifp
, timer
,
312 static int irdp_finish(void)
315 struct interface
*ifp
;
317 struct irdp_interface
*irdp
;
319 zlog_info("IRDP: Received shutdown notification.");
321 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
322 FOR_ALL_INTERFACES (vrf
, ifp
) {
331 if (irdp
->flags
& IF_ACTIVE
) {
332 irdp
->flags
|= IF_SHUTDOWN
;
333 irdp_advert_off(ifp
);
339 static int irdp_init(struct thread_master
*master
)
343 hook_register(frr_early_fini
, irdp_finish
);
347 static int irdp_module_init(void)
349 hook_register(frr_late_init
, irdp_init
);
353 FRR_MODULE_SETUP(.name
= "zebra_irdp", .version
= FRR_VERSION
,
354 .description
= "zebra IRDP module", .init
= irdp_module_init
, )