blob: 9e31950e2ca1a6a1d1a789d58e5879cbbc5acc5a [file] [log] [blame]
hassoca776982004-06-12 14:33:05 +00001/*
2 *
3 * Copyright (C) 2000 Robert Olsson.
4 * Swedish University of Agricultural Sciences
5 *
6 * This file is part of GNU Zebra.
7 *
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
11 * later version.
12 *
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.
17 *
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
21 * 02111-1307, USA.
22 */
23
24/*
25 * This work includes work with the following copywrite:
26 *
27 * Copyright (C) 1997, 2000 Kunihiro Ishiguro
28 *
29 */
30
31/*
32 * Thanks to Jens Låås at Swedish University of Agricultural Sciences
33 * for reviewing and tests.
34 */
35
36
37#include <zebra.h>
38
39#ifdef HAVE_IRDP
40
41#include "if.h"
42#include "vty.h"
43#include "sockunion.h"
44#include "prefix.h"
45#include "command.h"
46#include "memory.h"
47#include "stream.h"
48#include "ioctl.h"
49#include "connected.h"
50#include "log.h"
51#include "zclient.h"
52#include "thread.h"
hasso25dac852004-07-13 03:06:51 +000053#include "privs.h"
hassoca776982004-06-12 14:33:05 +000054#include "zebra/interface.h"
55#include "zebra/rtadv.h"
56#include "zebra/rib.h"
57#include "zebra/zserv.h"
58#include "zebra/redistribute.h"
59#include "zebra/irdp.h"
60#include <netinet/ip_icmp.h>
61
62#include "if.h"
63#include "sockunion.h"
64#include "log.h"
65
66/* GLOBAL VARS */
67
hasso25dac852004-07-13 03:06:51 +000068extern struct zebra_privs_t zserv_privs;
69
hassoca776982004-06-12 14:33:05 +000070/* Master of threads. */
71extern struct zebra_t zebrad;
72struct thread *t_irdp_raw;
73
74/* Timer interval of irdp. */
75int irdp_timer_interval = IRDP_DEFAULT_INTERVAL;
76
77int irdp_read_raw(struct thread *r);
78int in_cksum (void *ptr, int nbytes);
79extern int irdp_sock;
80void send_packet(struct interface *ifp,
81 struct stream *s,
82 u_int32_t dst,
83 struct prefix *p,
84 u_int32_t ttl);
85
86void irdp_if_init ();
87
88char *
89inet_2a(u_int32_t a, char *b)
90{
91 sprintf(b, "%u.%u.%u.%u",
92 (a ) & 0xFF,
93 (a>> 8) & 0xFF,
94 (a>>16) & 0xFF,
95 (a>>24) & 0xFF);
96 return b;
97}
98
99int
100irdp_sock_init (void)
101{
102 int ret, i;
103
hasso25dac852004-07-13 03:06:51 +0000104 if ( zserv_privs.change (ZPRIVS_RAISE) )
105 zlog_err ("irdp_sock_init: could not raise privs, %s",
106 strerror (errno) );
107
hassoca776982004-06-12 14:33:05 +0000108 irdp_sock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
hasso25dac852004-07-13 03:06:51 +0000109
110 if ( zserv_privs.change (ZPRIVS_LOWER) )
111 zlog_err ("irdp_sock_init: could not lower privs, %s",
112 strerror (errno) );
113
hassoca776982004-06-12 14:33:05 +0000114 if (irdp_sock < 0) {
115 zlog_warn ("IRDP: can't create irdp socket %s", strerror(errno));
116 return irdp_sock;
117 };
118
119 i = 1;
120 ret = setsockopt (irdp_sock, IPPROTO_IP, IP_TTL,
121 (void *) &i, sizeof (i));
122 if (ret < 0) {
123 zlog_warn ("IRDP: can't do irdp sockopt %s", strerror(errno));
124 return ret;
125 };
126
paul0de1cde2004-08-19 04:45:33 +0000127 ret = setsockopt_pktinfo (AF_INET, irdp_sock, 1);
hassoca776982004-06-12 14:33:05 +0000128 if (ret < 0) {
129 zlog_warn ("IRDP: can't do irdp sockopt %s", strerror(errno));
130 return ret;
131 };
132
133 t_irdp_raw = thread_add_read (zebrad.master, irdp_read_raw, NULL, irdp_sock);
134
135 return irdp_sock;
136}
137
138
139int get_pref(struct irdp_interface *irdp, struct prefix *p)
140{
paul0c0f9112004-09-24 08:24:42 +0000141 struct listnode *node;
hassoca776982004-06-12 14:33:05 +0000142 struct Adv *adv;
143
144 /* Use default preference or use the override pref */
145
paul0c0f9112004-09-24 08:24:42 +0000146 if( irdp->AdvPrefList == NULL )
147 return irdp->Preference;
hassoca776982004-06-12 14:33:05 +0000148
paul0c0f9112004-09-24 08:24:42 +0000149 LIST_LOOP (irdp->AdvPrefList, adv, node)
hassoca776982004-06-12 14:33:05 +0000150 if( p->u.prefix4.s_addr == adv->ip.s_addr )
151 return adv->pref;
paul0c0f9112004-09-24 08:24:42 +0000152
hassoca776982004-06-12 14:33:05 +0000153 return irdp->Preference;
154}
155
156/* Make ICMP Router Advertisement Message. */
157int make_advertisement_packet (struct interface *ifp,
158 struct prefix *p,
159 struct stream *s)
160{
161 struct zebra_if *zi=ifp->info;
162 struct irdp_interface *irdp=&zi->irdp;
163 int size;
164 int pref;
165 u_int16_t checksum;
166
167 pref = get_pref(irdp, p);
168
169 stream_putc (s, ICMP_ROUTERADVERT); /* Type. */
170 stream_putc (s, 0); /* Code. */
171 stream_putw (s, 0); /* Checksum. */
172 stream_putc (s, 1); /* Num address. */
173 stream_putc (s, 2); /* Address Entry Size. */
174
175 if(irdp->flags & IF_SHUTDOWN)
176 stream_putw (s, 0);
177 else
178 stream_putw (s, irdp->Lifetime);
179
180 stream_putl (s, htonl(p->u.prefix4.s_addr)); /* Router address. */
181 stream_putl (s, pref);
182
183 /* in_cksum return network byte order value */
184 size = 16;
185 checksum = in_cksum (s->data, size);
186 stream_putw_at (s, 2, htons(checksum));
187
188 return size;
189}
190
191void irdp_send(struct interface *ifp,
192 struct prefix *p,
193 struct stream *s)
194{
195 struct zebra_if *zi=ifp->info;
196 struct irdp_interface *irdp=&zi->irdp;
197 u_int32_t dst;
198 u_int32_t ttl=1;
199
200 if (! (ifp->flags & IFF_UP)) return;
201
202 if (irdp->flags & IF_BROADCAST)
203 dst =INADDR_BROADCAST ;
204 else
205 dst = htonl(INADDR_ALLHOSTS_GROUP);
206
207 if(irdp->flags & IF_DEBUG_MESSAGES)
208 zlog_warn("IRDP: TX Advert on %s %s/%d Holdtime=%d Preference=%d",
209 ifp->name,
210 inet_ntoa(p->u.prefix4),
211 p->prefixlen,
212 irdp->flags & IF_SHUTDOWN? 0 : irdp->Lifetime,
213 get_pref(irdp, p));
214
215 send_packet (ifp, s, dst, p, ttl);
216}
217
218void irdp_advertisement (struct interface *ifp,
219 struct prefix *p)
220{
221 struct stream *s;
222 s = stream_new (128);
223 make_advertisement_packet (ifp, p, s);
224 irdp_send(ifp, p, s);
225}
226
227int irdp_send_thread(struct thread *t_advert)
228{
229 u_int32_t timer, tmp;
230 struct interface *ifp = THREAD_ARG (t_advert);
231 struct zebra_if *zi=ifp->info;
232 struct irdp_interface *irdp=&zi->irdp;
233 struct prefix *p;
paul0c0f9112004-09-24 08:24:42 +0000234 struct listnode *node;
hassoca776982004-06-12 14:33:05 +0000235 struct connected *ifc;
236
237 irdp->flags &= ~IF_SOLICIT;
238
239 if(ifp->connected)
paul0c0f9112004-09-24 08:24:42 +0000240 LIST_LOOP (ifp->connected, ifc, node)
241 {
242 p = ifc->address;
243 irdp_advertisement(ifp, p);
244 irdp->irdp_sent++;
245 }
hassoca776982004-06-12 14:33:05 +0000246
247 tmp = irdp->MaxAdvertInterval-irdp->MinAdvertInterval;
248 timer = (random () % tmp ) + 1;
249 timer = irdp->MinAdvertInterval + timer;
250
251 if(irdp->irdp_sent < MAX_INITIAL_ADVERTISEMENTS &&
252 timer > MAX_INITIAL_ADVERT_INTERVAL )
253 timer= MAX_INITIAL_ADVERT_INTERVAL;
254
255 if(irdp->flags & IF_DEBUG_MISC)
256 zlog_warn("IRDP: New timer for %s set to %u\n", ifp->name, timer);
257
258 irdp->t_advertise = thread_add_timer(zebrad.master, irdp_send_thread, ifp, timer);
259 return 0;
260}
261
262void irdp_advert_off(struct interface *ifp)
263{
264 struct zebra_if *zi=ifp->info;
265 struct irdp_interface *irdp=&zi->irdp;
paul0c0f9112004-09-24 08:24:42 +0000266 struct listnode *node;
hassoca776982004-06-12 14:33:05 +0000267 int i;
268 struct connected *ifc;
269 struct prefix *p;
270
271 if(irdp->t_advertise) thread_cancel(irdp->t_advertise);
272 irdp->t_advertise = NULL;
273
274 if(ifp->connected)
paul0c0f9112004-09-24 08:24:42 +0000275 LIST_LOOP (ifp->connected, ifc, node)
276 {
277 p = ifc->address;
hassoca776982004-06-12 14:33:05 +0000278
paul0c0f9112004-09-24 08:24:42 +0000279 /* Output some packets with Lifetime 0
280 we should add a wait...
281 */
hassoca776982004-06-12 14:33:05 +0000282
paul0c0f9112004-09-24 08:24:42 +0000283 for(i=0; i< IRDP_LAST_ADVERT_MESSAGES; i++)
284 {
285 irdp->irdp_sent++;
286 irdp_advertisement(ifp, p);
287 }
hassoca776982004-06-12 14:33:05 +0000288 }
hassoca776982004-06-12 14:33:05 +0000289}
290
291
292void process_solicit (struct interface *ifp)
293{
294 struct zebra_if *zi=ifp->info;
295 struct irdp_interface *irdp=&zi->irdp;
296 u_int32_t timer;
297
298 /* When SOLICIT is active we reject further incoming solicits
299 this keeps down the answering rate so we don't have think
300 about DoS attacks here. */
301
302 if( irdp->flags & IF_SOLICIT) return;
303
304 irdp->flags |= IF_SOLICIT;
305 if(irdp->t_advertise) thread_cancel(irdp->t_advertise);
306 irdp->t_advertise = NULL;
307
308 timer = (random () % MAX_RESPONSE_DELAY) + 1;
309
310 irdp->t_advertise = thread_add_timer(zebrad.master,
311 irdp_send_thread,
312 ifp,
313 timer);
314}
315
316void irdp_finish()
317{
318
paul0c0f9112004-09-24 08:24:42 +0000319 struct listnode *node;
hassoca776982004-06-12 14:33:05 +0000320 struct interface *ifp;
321 struct zebra_if *zi;
322 struct irdp_interface *irdp;
323
324 zlog_warn("IRDP: Received shutdown notification.");
325
326 for (node = listhead (iflist); node; node = nextnode (node))
327 {
328 ifp = getdata(node);
paul0c0f9112004-09-24 08:24:42 +0000329 zi = ifp->info;
330
331 if (!zi)
332 continue;
hassoca776982004-06-12 14:33:05 +0000333 irdp = &zi->irdp;
paul0c0f9112004-09-24 08:24:42 +0000334 if (!irdp)
335 continue;
hassoca776982004-06-12 14:33:05 +0000336
paul0c0f9112004-09-24 08:24:42 +0000337 if (irdp->flags & IF_ACTIVE )
338 {
339 irdp->flags |= IF_SHUTDOWN;
340 irdp_advert_off(ifp);
341 }
hassoca776982004-06-12 14:33:05 +0000342 }
343}
344
345void irdp_init()
346{
347 irdp_sock_init();
348 irdp_if_init ();
349}
350
hassoca776982004-06-12 14:33:05 +0000351#endif /* HAVE_IRDP */
352
353
354
355