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
19 * along with GNU Zebra; see the file COPYING. If not, write to the Free
20 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 * This work includes work with the following copywrite:
27 * Copyright (C) 1997, 2000 Kunihiro Ishiguro
32 * Thanks to Jens Låås at Swedish University of Agricultural Sciences
33 * for reviewing and tests.
43 #include "sockunion.h"
50 #include "connected.h"
55 #include "zebra/interface.h"
56 #include "zebra/rtadv.h"
57 #include "zebra/rib.h"
58 #include "zebra/zserv.h"
59 #include "zebra/redistribute.h"
60 #include "zebra/irdp.h"
61 #include <netinet/ip_icmp.h>
64 #include "sockunion.h"
69 extern struct zebra_privs_t zserv_privs
;
71 /* Master of threads. */
72 extern struct zebra_t zebrad
;
73 struct thread
*t_irdp_raw
;
75 /* Timer interval of irdp. */
76 int irdp_timer_interval
= IRDP_DEFAULT_INTERVAL
;
78 int irdp_read_raw(struct thread
*r
);
79 int in_cksum (void *ptr
, int nbytes
);
81 void send_packet(struct interface
*ifp
,
90 inet_2a(u_int32_t a
, char *b
)
92 sprintf(b
, "%u.%u.%u.%u",
101 irdp_sock_init (void)
105 if ( zserv_privs
.change (ZPRIVS_RAISE
) )
106 zlog_err ("irdp_sock_init: could not raise privs, %s",
109 irdp_sock
= socket (AF_INET
, SOCK_RAW
, IPPROTO_ICMP
);
111 if ( zserv_privs
.change (ZPRIVS_LOWER
) )
112 zlog_err ("irdp_sock_init: could not lower privs, %s",
116 zlog_warn ("IRDP: can't create irdp socket %s", strerror(errno
));
121 ret
= setsockopt (irdp_sock
, IPPROTO_IP
, IP_TTL
,
122 (void *) &i
, sizeof (i
));
124 zlog_warn ("IRDP: can't do irdp sockopt %s", strerror(errno
));
128 ret
= setsockopt_pktinfo (AF_INET
, irdp_sock
, 1);
130 zlog_warn ("IRDP: can't do irdp sockopt %s", strerror(errno
));
134 t_irdp_raw
= thread_add_read (zebrad
.master
, irdp_read_raw
, NULL
, irdp_sock
);
140 int get_pref(struct irdp_interface
*irdp
, struct prefix
*p
)
142 struct listnode
*node
;
145 /* Use default preference or use the override pref */
147 if( irdp
->AdvPrefList
== NULL
)
148 return irdp
->Preference
;
150 LIST_LOOP (irdp
->AdvPrefList
, adv
, node
)
151 if( p
->u
.prefix4
.s_addr
== adv
->ip
.s_addr
)
154 return irdp
->Preference
;
157 /* Make ICMP Router Advertisement Message. */
158 int make_advertisement_packet (struct interface
*ifp
,
162 struct zebra_if
*zi
=ifp
->info
;
163 struct irdp_interface
*irdp
=&zi
->irdp
;
168 pref
= get_pref(irdp
, p
);
170 stream_putc (s
, ICMP_ROUTERADVERT
); /* Type. */
171 stream_putc (s
, 0); /* Code. */
172 stream_putw (s
, 0); /* Checksum. */
173 stream_putc (s
, 1); /* Num address. */
174 stream_putc (s
, 2); /* Address Entry Size. */
176 if(irdp
->flags
& IF_SHUTDOWN
)
179 stream_putw (s
, irdp
->Lifetime
);
181 stream_putl (s
, htonl(p
->u
.prefix4
.s_addr
)); /* Router address. */
182 stream_putl (s
, pref
);
184 /* in_cksum return network byte order value */
186 checksum
= in_cksum (s
->data
, size
);
187 stream_putw_at (s
, 2, htons(checksum
));
192 void irdp_send(struct interface
*ifp
,
196 struct zebra_if
*zi
=ifp
->info
;
197 struct irdp_interface
*irdp
=&zi
->irdp
;
201 if (! (ifp
->flags
& IFF_UP
)) return;
203 if (irdp
->flags
& IF_BROADCAST
)
204 dst
=INADDR_BROADCAST
;
206 dst
= htonl(INADDR_ALLHOSTS_GROUP
);
208 if(irdp
->flags
& IF_DEBUG_MESSAGES
)
209 zlog_warn("IRDP: TX Advert on %s %s/%d Holdtime=%d Preference=%d",
211 inet_ntoa(p
->u
.prefix4
),
213 irdp
->flags
& IF_SHUTDOWN
? 0 : irdp
->Lifetime
,
216 send_packet (ifp
, s
, dst
, p
, ttl
);
219 void irdp_advertisement (struct interface
*ifp
,
223 s
= stream_new (128);
224 make_advertisement_packet (ifp
, p
, s
);
225 irdp_send(ifp
, p
, s
);
228 int irdp_send_thread(struct thread
*t_advert
)
230 u_int32_t timer
, tmp
;
231 struct interface
*ifp
= THREAD_ARG (t_advert
);
232 struct zebra_if
*zi
=ifp
->info
;
233 struct irdp_interface
*irdp
=&zi
->irdp
;
235 struct listnode
*node
;
236 struct connected
*ifc
;
238 irdp
->flags
&= ~IF_SOLICIT
;
241 LIST_LOOP (ifp
->connected
, ifc
, node
)
244 irdp_advertisement(ifp
, p
);
248 tmp
= irdp
->MaxAdvertInterval
-irdp
->MinAdvertInterval
;
249 timer
= (random () % tmp
) + 1;
250 timer
= irdp
->MinAdvertInterval
+ timer
;
252 if(irdp
->irdp_sent
< MAX_INITIAL_ADVERTISEMENTS
&&
253 timer
> MAX_INITIAL_ADVERT_INTERVAL
)
254 timer
= MAX_INITIAL_ADVERT_INTERVAL
;
256 if(irdp
->flags
& IF_DEBUG_MISC
)
257 zlog_warn("IRDP: New timer for %s set to %u\n", ifp
->name
, timer
);
259 irdp
->t_advertise
= thread_add_timer(zebrad
.master
, irdp_send_thread
, ifp
, timer
);
263 void irdp_advert_off(struct interface
*ifp
)
265 struct zebra_if
*zi
=ifp
->info
;
266 struct irdp_interface
*irdp
=&zi
->irdp
;
267 struct listnode
*node
;
269 struct connected
*ifc
;
272 if(irdp
->t_advertise
) thread_cancel(irdp
->t_advertise
);
273 irdp
->t_advertise
= NULL
;
276 LIST_LOOP (ifp
->connected
, ifc
, node
)
280 /* Output some packets with Lifetime 0
281 we should add a wait...
284 for(i
=0; i
< IRDP_LAST_ADVERT_MESSAGES
; i
++)
287 irdp_advertisement(ifp
, p
);
293 void process_solicit (struct interface
*ifp
)
295 struct zebra_if
*zi
=ifp
->info
;
296 struct irdp_interface
*irdp
=&zi
->irdp
;
299 /* When SOLICIT is active we reject further incoming solicits
300 this keeps down the answering rate so we don't have think
301 about DoS attacks here. */
303 if( irdp
->flags
& IF_SOLICIT
) return;
305 irdp
->flags
|= IF_SOLICIT
;
306 if(irdp
->t_advertise
) thread_cancel(irdp
->t_advertise
);
307 irdp
->t_advertise
= NULL
;
309 timer
= (random () % MAX_RESPONSE_DELAY
) + 1;
311 irdp
->t_advertise
= thread_add_timer(zebrad
.master
,
320 struct listnode
*node
;
321 struct interface
*ifp
;
323 struct irdp_interface
*irdp
;
325 zlog_warn("IRDP: Received shutdown notification.");
327 for (node
= listhead (iflist
); node
; node
= nextnode (node
))
338 if (irdp
->flags
& IF_ACTIVE
)
340 irdp
->flags
|= IF_SHUTDOWN
;
341 irdp_advert_off(ifp
);
352 #endif /* HAVE_IRDP */