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 "zebra/zebra_errors.h"
63 #include <netinet/ip_icmp.h>
67 #include "sockunion.h"
72 extern struct zebra_privs_t zserv_privs
;
74 struct thread
*t_irdp_raw
;
76 /* Timer interval of irdp. */
77 int irdp_timer_interval
= IRDP_DEFAULT_INTERVAL
;
79 int irdp_sock_init(void)
85 frr_elevate_privs(&zserv_privs
) {
87 sock
= socket(AF_INET
, SOCK_RAW
, IPPROTO_ICMP
);
93 flog_err_sys(EC_LIB_SOCKET
, "IRDP: can't create irdp socket %s",
94 safe_strerror(save_errno
));
99 ret
= setsockopt(sock
, IPPROTO_IP
, IP_TTL
, (void *)&i
, sizeof(i
));
101 flog_err_sys(EC_LIB_SOCKET
, "IRDP: can't do irdp sockopt %s",
102 safe_strerror(errno
));
107 ret
= setsockopt_ifindex(AF_INET
, sock
, 1);
109 flog_err_sys(EC_LIB_SOCKET
, "IRDP: can't do irdp sockopt %s",
110 safe_strerror(errno
));
116 thread_add_read(zebrad
.master
, irdp_read_raw
, NULL
, sock
, &t_irdp_raw
);
122 static int get_pref(struct irdp_interface
*irdp
, struct prefix
*p
)
124 struct listnode
*node
;
127 /* Use default preference or use the override pref */
129 if (irdp
->AdvPrefList
== NULL
)
130 return irdp
->Preference
;
132 for (ALL_LIST_ELEMENTS_RO(irdp
->AdvPrefList
, node
, adv
))
133 if (p
->u
.prefix4
.s_addr
== adv
->ip
.s_addr
)
136 return irdp
->Preference
;
139 /* Make ICMP Router Advertisement Message. */
140 static int make_advertisement_packet(struct interface
*ifp
, struct prefix
*p
,
143 struct zebra_if
*zi
= ifp
->info
;
144 struct irdp_interface
*irdp
= zi
->irdp
;
149 pref
= get_pref(irdp
, p
);
151 stream_putc(s
, ICMP_ROUTERADVERT
); /* Type. */
152 stream_putc(s
, 0); /* Code. */
153 stream_putw(s
, 0); /* Checksum. */
154 stream_putc(s
, 1); /* Num address. */
155 stream_putc(s
, 2); /* Address Entry Size. */
157 if (irdp
->flags
& IF_SHUTDOWN
)
160 stream_putw(s
, irdp
->Lifetime
);
162 stream_putl(s
, htonl(p
->u
.prefix4
.s_addr
)); /* Router address. */
163 stream_putl(s
, pref
);
165 /* in_cksum return network byte order value */
167 checksum
= in_cksum(s
->data
, size
);
168 stream_putw_at(s
, 2, htons(checksum
));
173 static void irdp_send(struct interface
*ifp
, struct prefix
*p
, struct stream
*s
)
175 struct zebra_if
*zi
= ifp
->info
;
176 struct irdp_interface
*irdp
= zi
->irdp
;
177 char buf
[PREFIX_STRLEN
];
183 if (!(ifp
->flags
& IFF_UP
))
186 if (irdp
->flags
& IF_BROADCAST
)
187 dst
= INADDR_BROADCAST
;
189 dst
= htonl(INADDR_ALLHOSTS_GROUP
);
191 if (irdp
->flags
& IF_DEBUG_MESSAGES
)
192 zlog_debug("IRDP: TX Advert on %s %s Holdtime=%d Preference=%d",
193 ifp
->name
, prefix2str(p
, buf
, sizeof buf
),
194 irdp
->flags
& IF_SHUTDOWN
? 0 : irdp
->Lifetime
,
197 send_packet(ifp
, s
, dst
, p
, ttl
);
200 static void irdp_advertisement(struct interface
*ifp
, struct prefix
*p
)
204 make_advertisement_packet(ifp
, p
, s
);
205 irdp_send(ifp
, p
, s
);
209 int irdp_send_thread(struct thread
*t_advert
)
212 struct interface
*ifp
= THREAD_ARG(t_advert
);
213 struct zebra_if
*zi
= ifp
->info
;
214 struct irdp_interface
*irdp
= zi
->irdp
;
216 struct listnode
*node
, *nnode
;
217 struct connected
*ifc
;
222 irdp
->flags
&= ~IF_SOLICIT
;
225 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, ifc
)) {
228 if (p
->family
!= AF_INET
)
231 irdp_advertisement(ifp
, p
);
235 tmp
= irdp
->MaxAdvertInterval
- irdp
->MinAdvertInterval
;
236 timer
= random() % (tmp
+ 1);
237 timer
= irdp
->MinAdvertInterval
+ timer
;
239 if (irdp
->irdp_sent
< MAX_INITIAL_ADVERTISEMENTS
240 && timer
> MAX_INITIAL_ADVERT_INTERVAL
)
241 timer
= MAX_INITIAL_ADVERT_INTERVAL
;
243 if (irdp
->flags
& IF_DEBUG_MISC
)
244 zlog_debug("IRDP: New timer for %s set to %u\n", ifp
->name
,
247 irdp
->t_advertise
= NULL
;
248 thread_add_timer(zebrad
.master
, irdp_send_thread
, ifp
, timer
,
253 void irdp_advert_off(struct interface
*ifp
)
255 struct zebra_if
*zi
= ifp
->info
;
256 struct irdp_interface
*irdp
= zi
->irdp
;
257 struct listnode
*node
, *nnode
;
259 struct connected
*ifc
;
265 if (irdp
->t_advertise
)
266 thread_cancel(irdp
->t_advertise
);
267 irdp
->t_advertise
= NULL
;
270 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nnode
, ifc
)) {
273 /* Output some packets with Lifetime 0
274 we should add a wait...
277 for (i
= 0; i
< IRDP_LAST_ADVERT_MESSAGES
; i
++) {
279 irdp_advertisement(ifp
, p
);
285 void process_solicit(struct interface
*ifp
)
287 struct zebra_if
*zi
= ifp
->info
;
288 struct irdp_interface
*irdp
= zi
->irdp
;
294 /* When SOLICIT is active we reject further incoming solicits
295 this keeps down the answering rate so we don't have think
296 about DoS attacks here. */
298 if (irdp
->flags
& IF_SOLICIT
)
301 irdp
->flags
|= IF_SOLICIT
;
302 if (irdp
->t_advertise
)
303 thread_cancel(irdp
->t_advertise
);
304 irdp
->t_advertise
= NULL
;
306 timer
= (random() % MAX_RESPONSE_DELAY
) + 1;
308 irdp
->t_advertise
= NULL
;
309 thread_add_timer(zebrad
.master
, irdp_send_thread
, ifp
, timer
,
313 static int irdp_finish(void)
316 struct interface
*ifp
;
318 struct irdp_interface
*irdp
;
320 zlog_info("IRDP: Received shutdown notification.");
322 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
323 FOR_ALL_INTERFACES (vrf
, ifp
) {
332 if (irdp
->flags
& IF_ACTIVE
) {
333 irdp
->flags
|= IF_SHUTDOWN
;
334 irdp_advert_off(ifp
);
340 static int irdp_init(struct thread_master
*master
)
344 hook_register(frr_early_fini
, irdp_finish
);
348 static int irdp_module_init(void)
350 hook_register(frr_late_init
, irdp_init
);
354 FRR_MODULE_SETUP(.name
= "zebra_irdp", .version
= FRR_VERSION
,
355 .description
= "zebra IRDP module", .init
= irdp_module_init
, )