summaryrefslogtreecommitdiff
path: root/zebra/irdp.c
blob: 1b3bf23232c98c20ac255284e5932cde0f858fbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/* ICMP Router Discovery Messages
 * Copyright (C) 1997, 2000 Kunihiro Ishiguro
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * GNU Zebra is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Zebra; see the file COPYING.  If not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.  
 */

#include <zebra.h>

#include <netinet/ip_icmp.h>

#include "if.h"
#include "stream.h"
#include "memory.h"
#include "command.h"
#include "log.h"
#include "sockunion.h"
#include "sockopt.h"

#include "zebra/irdp.h"

/* Default does nothing. */
int irdp_mode = IRDP_NONE;

/* Timer interval of irdp. */
int irdp_timer_interval = IRDP_DEFAULT_INTERVAL;

/* Max solicitations */
int max_solicitations = MAX_SOLICITATIONS;

#define IRDP_SOLICIT_PACKET_SIZE 8

static struct irdp *irdp_head = NULL;

extern int in_cksum (void *ptr, int nbytes);

char *icmp_type_str[] = 
{
  "Echo Reply",
  "ICMP 1",
  "ICMP 2",
  "Dest Unreachable",
  "Source Quench",
  "Redirect",
  "ICMP 6",
  "ICMP 7",
  "Echo",
  "Router Advertise",
  "Router Solicitation",
  "Time Exceeded",
  "Parameter Problem",
  "Timestamp",
  "Timestamp Reply",
  "Info Request",
  "Info Reply",
  "Netmask Request",
  "Netmask Reply",
};

char *
icmp_type (int type)
{
  if (type < 0 || type >= (sizeof icmp_type_str / sizeof (char *))) {
    return "OUT-OF-RANGE";
  }
  return icmp_type_str [type];
}

/* */
void
irdp_add_interface ()
{
  ;
}

/* */
void
irdp_delete_interface ()
{

}

struct irdp *
irdp_route_new ()
{
  struct irdp *new = XMALLOC (0, sizeof (struct irdp));
  memset (new, 0, sizeof (struct irdp));
  return new;
}

void
irdp_route_free (struct irdp *route)
{
  XFREE (0, route);
}

void
route_delete ()
{

}

void
route_init ()
{
  
}

void
route_add (struct in_addr addr, unsigned long pref)
{
  struct irdp *new = irdp_route_new();
  
  new->prefix = addr;
  new->pref = pref;

  printf ("address %s\n", inet_ntoa (new->prefix));
  printf ("pref %ld\n", new->pref);
}

void
route_age (int time)
{
  struct irdp *p;

  for (p = irdp_head; p != NULL; p = p->next) {
    if (p->timer < time) {
      /* fire */
    } else {
      p->timer -= time;
    }
  }
}

#define FLAG_TEST(a)  ((ifp->flags & (a)) == (a))

void
send_multicast (struct interface *ifp, int sock, struct stream *s, int size)
{
  struct sockaddr_in sin;
  struct in_addr addr;
  int nbytes;
  struct connected *connected;
  listnode node;
  
  for (node = listhead (ifp->connected); node; nextnode (node))
    {
      connected = getdata (node);
    }

  if (setsockopt_multicast_ipv4 (sock, IP_MULTICAST_IF,
		  addr, 0, ifp->ifindex) < 0) 
    {
      perror ("setsockopt");
      exit (1);
    }

  sin.sin_addr.s_addr = htonl (INADDR_ALLRTRS_GROUP);
  sin.sin_family = AF_INET;

  nbytes = sendto (sock, s->data, size, 0,
		   (struct sockaddr *) &sin, sizeof (struct sockaddr));

  if (nbytes != size) 
    {
      perror ("sendto");
      exit (1);
    }
}

void
send_broadcast ()
{
  struct sockaddr_in sin;

  printf ("broadcast\n");
  inet_aton ("255.255.255.255", &sin.sin_addr);
}

void
irdp_send_solicit (int sock, struct stream *s, int size)
{
  struct interface *ifp;
  listnode node;

  for (node = listhead (iflist); node; nextnode (node))
    {
      ifp = getdata (node);
      if (FLAG_TEST (IFF_UP | IFF_MULTICAST)) 
	{
	  send_multicast (ifp, sock, s, size);
	}
      else if (FLAG_TEST (IFF_UP | IFF_BROADCAST)) 
	{
	  send_broadcast ();
	}
    }
}

int
ipv4_multicast_join (int sock, 
		     struct in_addr group, 
		     struct in_addr ifa,
		     unsigned int ifindex)
{
  int ret;

  ret = setsockopt_multicast_ipv4 (sock, IP_ADD_MEMBERSHIP, 
		    ifa, group.saddr, ifindex);

  if (ret < 0) 
    zlog (NULL, LOG_INFO, "can't setsockopt IP_ADD_MEMBERSHIP");

  return ret;
}

/* multicast packet recieve socket */
int
irdp_multicast_socket (int sock, struct in_addr group)
{
  struct interface *ifp;
  listnode node;
  struct in_addr addr;

  for (node = listhead (iflist); node; nextnode (node))
    {
      ifp = getdata (node);

      if ((ifp->flags & IFF_UP) && (ifp->flags & IFF_MULTICAST)) 
	{
	  ipv4_multicast_join (sock, group, addr, ifp->ifindex);
	}
    }
  return 0;
}

struct 
{
  u_char type;
  u_char code;
  u_int16_t checksum;
  u_char number;
  u_char entry;
  u_int16_t lifetime;
} radv;

void
irdp_set (int sock)
{
  struct in_addr irdp_group;

  switch (irdp_mode) 
    {
    case IRDP_HOST:
      irdp_group.s_addr = htonl (INADDR_ALLHOSTS_GROUP);
      break;
    case IRDP_ROUTER:
      irdp_group.s_addr = htonl (INADDR_ALLRTRS_GROUP);
      break;
    case IRDP_NONE:
    default:
      return;
    }
  irdp_multicast_socket (sock, irdp_group);
}

/* Make ICMP Router Solicitation Message. */
int
make_solicit_packet (struct stream *s)
{
  int size;
  int checksum;

  stream_putc (s, ICMP_ROUTERSOLICIT); /* Type. */
  stream_putc (s, 0);		/* Code. */
  stream_putw (s, 0);		/* Checksum. */
  stream_putl (s, 0);		/* Reserved. */

  /* in_cksum return network byte order value */
  size = IRDP_SOLICIT_PACKET_SIZE;
  checksum = in_cksum (s->data, size);
  stream_putw_at (s, checksum, 2);

  return IRDP_SOLICIT_PACKET_SIZE;
}

void
irdp_solicit (int sock)
{
  struct stream *s;

  s = stream_new (IRDP_SOLICIT_PACKET_SIZE);
  make_solicit_packet (s);
  irdp_send_solicit (sock, s, IRDP_SOLICIT_PACKET_SIZE);
}

#define ICMP_MINLEN 8

/* check validity of the packet */
int
irdp_valid_check (char *packet, size_t size, struct sockaddr_in *from)
{
  struct icmp *icmp;

  icmp = (struct icmp *) packet;

  if (in_cksum (packet, size)) {
    zlog_warn ("ICMP %s packet from %s: Bad checksum, silently ignored",
	       icmp_type (icmp->icmp_type),
	       inet_ntoa (from->sin_addr));
    return -1;
  }

  if (icmp->icmp_code != 0) {
    zlog_warn ("ICMP %s packet from %s: Bad ICMP type code, silently ignored",
	       icmp_type (icmp->icmp_type),
	       inet_ntoa (from->sin_addr));
    return -1;
  }

  if (size < ICMP_MINLEN) {
    zlog_warn ("ICMP %s packet from %s: IMCP message length is short",
	       icmp_type (icmp->icmp_type),
	       inet_ntoa (from->sin_addr));
    return -1;
  }
  return 0;
}

int
irdp_solicit_recv (struct stream *s, int size, struct sockaddr_in *from)
{
  if (irdp_valid_check (s->data, size, from)) {
    return 1;
  }
  return 0;
}

void
irdp_advert_recv (struct stream *s, int size, struct sockaddr_in *from)
{
  int i;
  struct in_addr addr;
  long pref;

  if (irdp_valid_check (s->data, size, from) < 0) {
    return;
  }

  radv.type = stream_getc (s);
  radv.code =  stream_getc (s);
  radv.checksum = stream_getw (s);
  radv.number = stream_getc (s);
  radv.entry = stream_getc (s);
  radv.lifetime = stream_getw (s);

  printf ("type : %s\n", icmp_type (radv.type));
  printf ("number: %d\n", radv.number);
  printf ("entry: %d\n", radv.entry);
  printf ("lifetime: %d\n", radv.entry);

  for (i = 0; i < radv.number; i++) 
    {
      addr.s_addr = stream_getl (s);
      pref = stream_getl (s);
      route_add (addr, ntohl (pref));
    }
  /* Packet size check is needed at here. */
}

void
irdp_packet_process (char *buf, int size, struct sockaddr_in *from)
{
  struct ip *ip;
  struct icmp *icmp;
  int hlen;
  struct stream *s = NULL;

  ip = (struct ip *)buf;
  hlen = ip->ip_hl << 2;

  if (size < hlen + ICMP_MINLEN)
    zlog_err ("ICMP relpy length is short\n");

  icmp = (struct icmp *)(buf + hlen);

  stream_forward (s, hlen);
  
  switch (icmp->icmp_type) 
    {
    case ICMP_ROUTERADVERT:
      irdp_advert_recv (s, size - hlen, from);
      break;
    case ICMP_ROUTERSOLICIT:
      irdp_solicit_recv (s, size - hlen, from);
      break;
    }
}

/* Make socket for ICMP Router Discovery. */
int
irdp_make_socket ()
{
  int sock;
  struct protoent *pent;

  if ((pent = getprotobyname ("icmp")) == NULL) {
    perror ("getprotobyname");
    exit (1);
  }

  if ((sock = socket (AF_INET, SOCK_RAW, pent->p_proto)) < 0) 
    {
      perror ("socket");
      exit (1);
    }

  return sock;
}

/* recv routine */
int
irdp_recv (int sock)
{
#define PACKET_BUF 4096
  int nbytes;
  struct sockaddr_in from;
  int fromlen;
  char buf[PACKET_BUF];

  fromlen = sizeof (from);
  nbytes = recvfrom (sock, (char *)buf, PACKET_BUF, 0,
		     (struct sockaddr *)&from, &fromlen);

  if (nbytes < 0) 
    {
      perror ("recvfrom");
      exit (1);
    }

  irdp_packet_process (buf, nbytes, &from);

  return 0;
}

/* irdp packet recv loop */
void
irdp_loop (int sock)
{
  while (1) 
    {
      irdp_recv (sock);
    }
}

DEFUN (ip_irdp,
       ip_irdp_cmd,
       "ip irdp",
       IP_STR
       "ICMP Router discovery on this interface\n")
{
  return CMD_SUCCESS;
}

DEFUN (ip_irdp_multicast,
       ip_irdp_multicast_cmd,
       "ip irdp multicast",
       IP_STR
       "ICMP Router discovery on this interface\n"
       "Send IRDP advertisement to the multicast address\n")
{
  return CMD_SUCCESS;
}

DEFUN (ip_irdp_holdtime,
       ip_irdp_holdtime_cmd,
       "ip irdp holdtime <0-9000>",
       IP_STR
       "ICMP Router discovery on this interface\n"
       "Set holdtime value\n"
       "Holdtime value in seconds. Default is 1800 seconds\n")
{
  return CMD_SUCCESS;
}

DEFUN (ip_irdp_maxadvertinterval,
       ip_irdp_maxadvertinterval_cmd,
       "ip irdp maxadvertinterval (0|<4-1800>)",
       IP_STR
       "ICMP Router discovery on this interface\n"
       "Set maximum time between advertisement\n"
       "Maximum advertisement interval in seconds\n")
{
  return CMD_SUCCESS;
}

DEFUN (ip_irdp_minadvertinterval,
       ip_irdp_minadvertinterval_cmd,
       "ip irdp minadvertinterval <3-1800>",
       IP_STR
       "ICMP Router discovery on this interface\n"
       "Set minimum time between advertisement\n"
       "Minimum advertisement interval in seconds\n")
{
  return CMD_SUCCESS;
}

DEFUN (ip_irdp_preference,
       ip_irdp_preference_cmd,
       /* "ip irdp preference <-2147483648-2147483647>", */
       "ip irdp preference <0-2147483647>",
       IP_STR
       "ICMP Router discovery on this interface\n"
       "Set default preference level for this interface\n"
       "Preference level\n")
{
  return CMD_SUCCESS;
}

#if 0
DEFUN (ip_irdp_address,
       ip_irdp_address_cmd,
       "ip irdp address A.B.C.D",
       IP_STR
       "ICMP Router discovery on this interface\n"
       "Specify IRDP address and preference to proxy-advertise\n"
       "Set IRDP address for proxy-advertise\n")
{
  return CMD_SUCCESS;
}
#endif /* 0 */

DEFUN (ip_irdp_address_preference,
       ip_irdp_address_preference_cmd,
       "ip irdp address A.B.C.D <0-2147483647>",
       IP_STR
       "ICMP Router discovery on this interface\n"
       "Specify IRDP address and preference to proxy-advertise\n"
       "Set IRDP address for proxy-advertise\n"
       "Preference level\n")
{
  return CMD_SUCCESS;
}

void
irdp_init ()
{
  install_element (INTERFACE_NODE, &ip_irdp_cmd);
  install_element (INTERFACE_NODE, &ip_irdp_multicast_cmd);
  install_element (INTERFACE_NODE, &ip_irdp_holdtime_cmd);
  install_element (INTERFACE_NODE, &ip_irdp_maxadvertinterval_cmd);
  install_element (INTERFACE_NODE, &ip_irdp_minadvertinterval_cmd);
  install_element (INTERFACE_NODE, &ip_irdp_preference_cmd);
  install_element (INTERFACE_NODE, &ip_irdp_address_preference_cmd);
}