blob: 0db2a54da5fa13170b8a2f7d9788c776b7ce3889 [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"
hassoc9e52be2004-09-26 16:09:34 +000044#include "sockopt.h"
hassoca776982004-06-12 14:33:05 +000045#include "prefix.h"
46#include "command.h"
47#include "memory.h"
48#include "stream.h"
49#include "ioctl.h"
50#include "connected.h"
51#include "log.h"
52#include "zclient.h"
53#include "thread.h"
hasso25dac852004-07-13 03:06:51 +000054#include "privs.h"
hassoca776982004-06-12 14:33:05 +000055#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>
62
63#include "if.h"
64#include "sockunion.h"
65#include "log.h"
66
67/* GLOBAL VARS */
68
hasso25dac852004-07-13 03:06:51 +000069extern struct zebra_privs_t zserv_privs;
70
hassoca776982004-06-12 14:33:05 +000071/* Master of threads. */
72extern struct zebra_t zebrad;
73struct thread *t_irdp_raw;
74
75/* Timer interval of irdp. */
76int irdp_timer_interval = IRDP_DEFAULT_INTERVAL;
77
78int irdp_read_raw(struct thread *r);
79int in_cksum (void *ptr, int nbytes);
80extern int irdp_sock;
81void send_packet(struct interface *ifp,
82 struct stream *s,
83 u_int32_t dst,
84 struct prefix *p,
85 u_int32_t ttl);
86
87void irdp_if_init ();
88
89char *
90inet_2a(u_int32_t a, char *b)
91{
92 sprintf(b, "%u.%u.%u.%u",
93 (a ) & 0xFF,
94 (a>> 8) & 0xFF,
95 (a>>16) & 0xFF,
96 (a>>24) & 0xFF);
97 return b;
98}
99
100int
101irdp_sock_init (void)
102{
103 int ret, i;
ajs4460e7a2005-01-29 17:07:40 +0000104 int save_errno;
hassoca776982004-06-12 14:33:05 +0000105
hasso25dac852004-07-13 03:06:51 +0000106 if ( zserv_privs.change (ZPRIVS_RAISE) )
107 zlog_err ("irdp_sock_init: could not raise privs, %s",
ajs6099b3b2004-11-20 02:06:59 +0000108 safe_strerror (errno) );
hasso25dac852004-07-13 03:06:51 +0000109
hassoca776982004-06-12 14:33:05 +0000110 irdp_sock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
ajs4460e7a2005-01-29 17:07:40 +0000111 save_errno = errno;
hasso25dac852004-07-13 03:06:51 +0000112
113 if ( zserv_privs.change (ZPRIVS_LOWER) )
114 zlog_err ("irdp_sock_init: could not lower privs, %s",
ajs6099b3b2004-11-20 02:06:59 +0000115 safe_strerror (errno) );
hasso25dac852004-07-13 03:06:51 +0000116
hassoca776982004-06-12 14:33:05 +0000117 if (irdp_sock < 0) {
ajs4460e7a2005-01-29 17:07:40 +0000118 zlog_warn ("IRDP: can't create irdp socket %s", safe_strerror(save_errno));
hassoca776982004-06-12 14:33:05 +0000119 return irdp_sock;
120 };
121
122 i = 1;
123 ret = setsockopt (irdp_sock, IPPROTO_IP, IP_TTL,
124 (void *) &i, sizeof (i));
125 if (ret < 0) {
ajs6099b3b2004-11-20 02:06:59 +0000126 zlog_warn ("IRDP: can't do irdp sockopt %s", safe_strerror(errno));
hassoca776982004-06-12 14:33:05 +0000127 return ret;
128 };
129
paul06f953f2004-10-22 17:00:38 +0000130 ret = setsockopt_ifindex (AF_INET, irdp_sock, 1);
hassoca776982004-06-12 14:33:05 +0000131 if (ret < 0) {
ajs6099b3b2004-11-20 02:06:59 +0000132 zlog_warn ("IRDP: can't do irdp sockopt %s", safe_strerror(errno));
hassoca776982004-06-12 14:33:05 +0000133 return ret;
134 };
135
136 t_irdp_raw = thread_add_read (zebrad.master, irdp_read_raw, NULL, irdp_sock);
137
138 return irdp_sock;
139}
140
141
142int get_pref(struct irdp_interface *irdp, struct prefix *p)
143{
paul0c0f9112004-09-24 08:24:42 +0000144 struct listnode *node;
hassoca776982004-06-12 14:33:05 +0000145 struct Adv *adv;
146
147 /* Use default preference or use the override pref */
148
paul0c0f9112004-09-24 08:24:42 +0000149 if( irdp->AdvPrefList == NULL )
150 return irdp->Preference;
hassoca776982004-06-12 14:33:05 +0000151
paul0c0f9112004-09-24 08:24:42 +0000152 LIST_LOOP (irdp->AdvPrefList, adv, node)
hassoca776982004-06-12 14:33:05 +0000153 if( p->u.prefix4.s_addr == adv->ip.s_addr )
154 return adv->pref;
paul0c0f9112004-09-24 08:24:42 +0000155
hassoca776982004-06-12 14:33:05 +0000156 return irdp->Preference;
157}
158
159/* Make ICMP Router Advertisement Message. */
160int make_advertisement_packet (struct interface *ifp,
161 struct prefix *p,
162 struct stream *s)
163{
164 struct zebra_if *zi=ifp->info;
165 struct irdp_interface *irdp=&zi->irdp;
166 int size;
167 int pref;
168 u_int16_t checksum;
169
170 pref = get_pref(irdp, p);
171
172 stream_putc (s, ICMP_ROUTERADVERT); /* Type. */
173 stream_putc (s, 0); /* Code. */
174 stream_putw (s, 0); /* Checksum. */
175 stream_putc (s, 1); /* Num address. */
176 stream_putc (s, 2); /* Address Entry Size. */
177
178 if(irdp->flags & IF_SHUTDOWN)
179 stream_putw (s, 0);
180 else
181 stream_putw (s, irdp->Lifetime);
182
183 stream_putl (s, htonl(p->u.prefix4.s_addr)); /* Router address. */
184 stream_putl (s, pref);
185
186 /* in_cksum return network byte order value */
187 size = 16;
188 checksum = in_cksum (s->data, size);
189 stream_putw_at (s, 2, htons(checksum));
190
191 return size;
192}
193
194void irdp_send(struct interface *ifp,
195 struct prefix *p,
196 struct stream *s)
197{
198 struct zebra_if *zi=ifp->info;
199 struct irdp_interface *irdp=&zi->irdp;
200 u_int32_t dst;
201 u_int32_t ttl=1;
202
203 if (! (ifp->flags & IFF_UP)) return;
204
205 if (irdp->flags & IF_BROADCAST)
206 dst =INADDR_BROADCAST ;
207 else
208 dst = htonl(INADDR_ALLHOSTS_GROUP);
209
210 if(irdp->flags & IF_DEBUG_MESSAGES)
ajsb6178002004-12-07 21:12:56 +0000211 zlog_debug("IRDP: TX Advert on %s %s/%d Holdtime=%d Preference=%d",
hassoca776982004-06-12 14:33:05 +0000212 ifp->name,
213 inet_ntoa(p->u.prefix4),
214 p->prefixlen,
215 irdp->flags & IF_SHUTDOWN? 0 : irdp->Lifetime,
216 get_pref(irdp, p));
217
218 send_packet (ifp, s, dst, p, ttl);
219}
220
221void irdp_advertisement (struct interface *ifp,
222 struct prefix *p)
223{
224 struct stream *s;
225 s = stream_new (128);
226 make_advertisement_packet (ifp, p, s);
227 irdp_send(ifp, p, s);
228}
229
230int irdp_send_thread(struct thread *t_advert)
231{
232 u_int32_t timer, tmp;
233 struct interface *ifp = THREAD_ARG (t_advert);
234 struct zebra_if *zi=ifp->info;
235 struct irdp_interface *irdp=&zi->irdp;
236 struct prefix *p;
paul0c0f9112004-09-24 08:24:42 +0000237 struct listnode *node;
hassoca776982004-06-12 14:33:05 +0000238 struct connected *ifc;
239
240 irdp->flags &= ~IF_SOLICIT;
241
242 if(ifp->connected)
paul0c0f9112004-09-24 08:24:42 +0000243 LIST_LOOP (ifp->connected, ifc, node)
244 {
245 p = ifc->address;
246 irdp_advertisement(ifp, p);
247 irdp->irdp_sent++;
248 }
hassoca776982004-06-12 14:33:05 +0000249
250 tmp = irdp->MaxAdvertInterval-irdp->MinAdvertInterval;
251 timer = (random () % tmp ) + 1;
252 timer = irdp->MinAdvertInterval + timer;
253
254 if(irdp->irdp_sent < MAX_INITIAL_ADVERTISEMENTS &&
255 timer > MAX_INITIAL_ADVERT_INTERVAL )
256 timer= MAX_INITIAL_ADVERT_INTERVAL;
257
258 if(irdp->flags & IF_DEBUG_MISC)
ajsb6178002004-12-07 21:12:56 +0000259 zlog_debug("IRDP: New timer for %s set to %u\n", ifp->name, timer);
hassoca776982004-06-12 14:33:05 +0000260
261 irdp->t_advertise = thread_add_timer(zebrad.master, irdp_send_thread, ifp, timer);
262 return 0;
263}
264
265void irdp_advert_off(struct interface *ifp)
266{
267 struct zebra_if *zi=ifp->info;
268 struct irdp_interface *irdp=&zi->irdp;
paul0c0f9112004-09-24 08:24:42 +0000269 struct listnode *node;
hassoca776982004-06-12 14:33:05 +0000270 int i;
271 struct connected *ifc;
272 struct prefix *p;
273
274 if(irdp->t_advertise) thread_cancel(irdp->t_advertise);
275 irdp->t_advertise = NULL;
276
277 if(ifp->connected)
paul0c0f9112004-09-24 08:24:42 +0000278 LIST_LOOP (ifp->connected, ifc, node)
279 {
280 p = ifc->address;
hassoca776982004-06-12 14:33:05 +0000281
paul0c0f9112004-09-24 08:24:42 +0000282 /* Output some packets with Lifetime 0
283 we should add a wait...
284 */
hassoca776982004-06-12 14:33:05 +0000285
paul0c0f9112004-09-24 08:24:42 +0000286 for(i=0; i< IRDP_LAST_ADVERT_MESSAGES; i++)
287 {
288 irdp->irdp_sent++;
289 irdp_advertisement(ifp, p);
290 }
hassoca776982004-06-12 14:33:05 +0000291 }
hassoca776982004-06-12 14:33:05 +0000292}
293
294
295void process_solicit (struct interface *ifp)
296{
297 struct zebra_if *zi=ifp->info;
298 struct irdp_interface *irdp=&zi->irdp;
299 u_int32_t timer;
300
301 /* When SOLICIT is active we reject further incoming solicits
302 this keeps down the answering rate so we don't have think
303 about DoS attacks here. */
304
305 if( irdp->flags & IF_SOLICIT) return;
306
307 irdp->flags |= IF_SOLICIT;
308 if(irdp->t_advertise) thread_cancel(irdp->t_advertise);
309 irdp->t_advertise = NULL;
310
311 timer = (random () % MAX_RESPONSE_DELAY) + 1;
312
313 irdp->t_advertise = thread_add_timer(zebrad.master,
314 irdp_send_thread,
315 ifp,
316 timer);
317}
318
319void irdp_finish()
320{
321
paul0c0f9112004-09-24 08:24:42 +0000322 struct listnode *node;
hassoca776982004-06-12 14:33:05 +0000323 struct interface *ifp;
324 struct zebra_if *zi;
325 struct irdp_interface *irdp;
326
ajs274a4a42004-12-07 15:39:31 +0000327 zlog_info("IRDP: Received shutdown notification.");
hassoca776982004-06-12 14:33:05 +0000328
329 for (node = listhead (iflist); node; node = nextnode (node))
330 {
331 ifp = getdata(node);
paul0c0f9112004-09-24 08:24:42 +0000332 zi = ifp->info;
333
334 if (!zi)
335 continue;
hassoca776982004-06-12 14:33:05 +0000336 irdp = &zi->irdp;
paul0c0f9112004-09-24 08:24:42 +0000337 if (!irdp)
338 continue;
hassoca776982004-06-12 14:33:05 +0000339
paul0c0f9112004-09-24 08:24:42 +0000340 if (irdp->flags & IF_ACTIVE )
341 {
342 irdp->flags |= IF_SHUTDOWN;
343 irdp_advert_off(ifp);
344 }
hassoca776982004-06-12 14:33:05 +0000345 }
346}
347
348void irdp_init()
349{
350 irdp_sock_init();
351 irdp_if_init ();
352}
353
hassoca776982004-06-12 14:33:05 +0000354#endif /* HAVE_IRDP */
355
356
357
358