summaryrefslogtreecommitdiffstats
path: root/isisd/isis_dlpi.c
blob: db4383bed2d31fce6420cb5ac8cfe8eb42130ac9 (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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
 * IS-IS Rout(e)ing protocol - isis_dlpi.c
 *
 * Copyright (C) 2001,2002    Sampo Saaristo
 *                            Tampere University of Technology      
 *                            Institute of Communications Engineering
 *
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public Licenseas published by the Free 
 * Software Foundation; either version 2 of the License, or (at your option) 
 * any later version.
 *
 * This program 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 this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <zebra.h>
#include <net/if.h>
#include <netinet/if_ether.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stropts.h>
#include <poll.h>
#include <sys/dlpi.h>
#include <sys/pfmod.h>

#include "log.h"
#include "stream.h"
#include "if.h"

#include "isisd/dict.h"
#include "isisd/include-netbsd/iso.h"
#include "isisd/isis_constants.h"
#include "isisd/isis_common.h"
#include "isisd/isis_circuit.h"
#include "isisd/isis_flags.h"
#include "isisd/isisd.h"
#include "isisd/isis_constants.h"
#include "isisd/isis_circuit.h"
#include "isisd/isis_network.h"

#include "privs.h"

extern struct zebra_privs_t isisd_privs;

static t_uscalar_t dlpi_ctl[1024];	/* DLPI control messages */

/*
 * Table 9 - Architectural constants for use with ISO 8802 subnetworks
 * ISO 10589 - 8.4.8
 */

u_char ALL_L1_ISS[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x14 };
u_char ALL_L2_ISS[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x15 };
u_char ALL_ISS[6] = { 0x09, 0x00, 0x2B, 0x00, 0x00, 0x05 };
u_char ALL_ESS[6] = { 0x09, 0x00, 0x2B, 0x00, 0x00, 0x04 };

static u_char sock_buff[8192];

static u_short pf_filter[] =
{
  ENF_PUSHWORD + 0,		/* Get the SSAP/DSAP values */
  ENF_PUSHLIT | ENF_CAND,	/* Check them */
  ISO_SAP | (ISO_SAP << 8),
  ENF_PUSHWORD + 1,		/* Get the control value */
  ENF_PUSHLIT | ENF_AND,	/* Isolate it */
#ifdef _BIG_ENDIAN
  0xFF00,
#else
  0x00FF,
#endif
  ENF_PUSHLIT | ENF_CAND,	/* Test for expected value */
#ifdef _BIG_ENDIAN
  0x0300
#else
  0x0003
#endif
};

/*
 * We would like to use something like libdlpi here, but that's not present on
 * all versions of Solaris or on any non-Solaris system, so it's nowhere near
 * as portable as we'd like.  Thus, we use the standards-conformant DLPI
 * interfaces plus the (optional; not needed) Solaris packet filter module.
 */

static void
dlpisend (int fd, const void *cbuf, size_t cbuflen,
  const void *dbuf, size_t dbuflen, int flags)
{
  const struct strbuf *ctlptr = NULL;
  const struct strbuf *dataptr = NULL;
  struct strbuf ctlbuf, databuf;

  if (cbuf != NULL)
    {
      memset (&ctlbuf, 0, sizeof (ctlbuf));
      ctlbuf.len = cbuflen;
      ctlbuf.buf = (void *)cbuf;
      ctlptr = &ctlbuf;
    }

  if (dbuf != NULL)
    {
      memset (&databuf, 0, sizeof (databuf));
      databuf.len = dbuflen;
      databuf.buf = (void *)dbuf;
      dataptr = &databuf;
    }

  /* We assume this doesn't happen often and isn't operationally significant */
  if (putmsg (fd, ctlptr, dataptr, flags) == -1)
    zlog_debug ("%s: putmsg: %s", __func__, safe_strerror (errno));
}

static ssize_t
dlpirctl (int fd)
{
  struct pollfd fds[1];
  struct strbuf ctlbuf, databuf;
  int flags, retv;

  do
    {
      /* Poll is used here in case the device doesn't speak DLPI correctly */
      memset (fds, 0, sizeof (fds));
      fds[0].fd = fd;
      fds[0].events = POLLIN | POLLPRI;
      if (poll (fds, 1, 1000) <= 0)
	return -1;

      memset (&ctlbuf, 0, sizeof (ctlbuf));
      memset (&databuf, 0, sizeof (databuf));
      ctlbuf.maxlen = sizeof (dlpi_ctl);
      ctlbuf.buf = (void *)dlpi_ctl;
      databuf.maxlen = sizeof (sock_buff);
      databuf.buf = (void *)sock_buff;
      flags = 0;
      retv = getmsg (fd, &ctlbuf, &databuf, &flags);

      if (retv < 0)
	return -1;
    }
  while (ctlbuf.len == 0);

  if (!(retv & MORECTL))
    {
      while (retv & MOREDATA)
	{
	  flags = 0;
	  retv = getmsg (fd, NULL, &databuf, &flags);
	}
      return ctlbuf.len;
    }

  while (retv & MORECTL)
    {
      flags = 0;
      retv = getmsg (fd, &ctlbuf, &databuf, &flags);
    }
  return -1;
}

static int
dlpiok (int fd, t_uscalar_t oprim)
{
  int retv;
  dl_ok_ack_t *doa = (dl_ok_ack_t *)dlpi_ctl;

  retv = dlpirctl (fd);
  if (retv < DL_OK_ACK_SIZE || doa->dl_primitive != DL_OK_ACK ||
    doa->dl_correct_primitive != oprim)
    {
      return -1;
    }
  else
    {
      return 0;
    }
}

static int
dlpiinfo (int fd)
{
  dl_info_req_t dir;
  ssize_t retv;

  memset (&dir, 0, sizeof (dir));
  dir.dl_primitive = DL_INFO_REQ;
  /* Info_req uses M_PCPROTO. */
  dlpisend (fd, &dir, sizeof (dir), NULL, 0, RS_HIPRI);
  retv = dlpirctl (fd);
  if (retv < DL_INFO_ACK_SIZE || dlpi_ctl[0] != DL_INFO_ACK)
    return -1;
  else
    return retv;
}

static int
dlpiopen (const char *devpath, ssize_t *acklen)
{
  int fd, flags;

  fd = open (devpath, O_RDWR | O_NONBLOCK | O_NOCTTY);
  if (fd == -1)
    return -1;

  /* All that we want is for the open itself to be non-blocking, not I/O. */
  flags = fcntl (fd, F_GETFL, 0);
  if (flags != -1)
    fcntl (fd, F_SETFL, flags & ~O_NONBLOCK);

  /* After opening, ask for information */
  if ((*acklen = dlpiinfo (fd)) == -1)
    {
      close (fd);
      return -1;
    }

  return fd;
}

static int
dlpiattach (int fd, int unit)
{
  dl_attach_req_t dar;

  memset (&dar, 0, sizeof (dar));
  dar.dl_primitive = DL_ATTACH_REQ;
  dar.dl_ppa = unit;
  dlpisend (fd, &dar, sizeof (dar), NULL, 0, 0);
  return dlpiok (fd, dar.dl_primitive);
}

static int
dlpibind (int fd)
{
  dl_bind_req_t dbr;
  int retv;
  dl_bind_ack_t *dba = (dl_bind_ack_t *)dlpi_ctl;

  memset (&dbr, 0, sizeof (dbr));
  dbr.dl_primitive = DL_BIND_REQ;
  dbr.dl_service_mode = DL_CLDLS;
  dlpisend (fd, &dbr, sizeof (dbr), NULL, 0, 0);

  retv = dlpirctl (fd);
  if (retv < DL_BIND_ACK_SIZE || dba->dl_primitive != DL_BIND_ACK)
    return -1;
  else
    return 0;
}

static int
dlpimcast (int fd, const u_char *mcaddr)
{
  struct {
    dl_enabmulti_req_t der;
    u_char addr[ETHERADDRL];
  } dler;

  memset (&dler, 0, sizeof (dler));
  dler.der.dl_primitive = DL_ENABMULTI_REQ;
  dler.der.dl_addr_length = sizeof (dler.addr);
  dler.der.dl_addr_offset = dler.addr - (u_char *)&dler;
  memcpy (dler.addr, mcaddr, sizeof (dler.addr));
  dlpisend (fd, &dler, sizeof (dler), NULL, 0, 0);
  return dlpiok (fd, dler.der.dl_primitive);
}

static int
dlpiaddr (int fd, u_char *addr)
{
  dl_phys_addr_req_t dpar;
  dl_phys_addr_ack_t *dpaa = (dl_phys_addr_ack_t *)dlpi_ctl;
  int retv;

  memset (&dpar, 0, sizeof (dpar));
  dpar.dl_primitive = DL_PHYS_ADDR_REQ;
  dpar.dl_addr_type = DL_CURR_PHYS_ADDR;
  dlpisend (fd, &dpar, sizeof (dpar), NULL, 0, 0);

  retv = dlpirctl (fd);
  if (retv < DL_PHYS_ADDR_ACK_SIZE || dpaa->dl_primitive != DL_PHYS_ADDR_ACK)
    return -1;

  if (dpaa->dl_addr_offset < DL_PHYS_ADDR_ACK_SIZE ||
    dpaa->dl_addr_length != ETHERADDRL ||
    dpaa->dl_addr_offset + dpaa->dl_addr_length > retv)
    return -1;

  bcopy((char *)dpaa + dpaa->dl_addr_offset, addr, ETHERADDRL);
  return 0;
}

static int
open_dlpi_dev (struct isis_circuit *circuit)
{
  int fd, unit, retval;
  char devpath[MAXPATHLEN];
  dl_info_ack_t *dia = (dl_info_ack_t *)dlpi_ctl;
  ssize_t acklen;

  /* Only broadcast-type are supported at the moment */
  if (circuit->circ_type != CIRCUIT_T_BROADCAST)
    {
      zlog_warn ("%s: non-broadcast interface %s", __func__,
	circuit->interface->name);
      return ISIS_WARNING;
    }

  /* Try first as Style 1 */
  (void) snprintf(devpath, sizeof (devpath), "/dev/%s",
    circuit->interface->name);
  unit = -1;
  fd = dlpiopen (devpath, &acklen);

  /* If that fails, try again as Style 2 */
  if (fd == -1)
    {
      char *cp;

      cp = devpath + strlen (devpath);
      while (--cp >= devpath && isdigit(*cp))
	;
      unit = strtol(cp, NULL, 0);
      *cp = '\0';
      fd = dlpiopen (devpath, &acklen);

      /* If that too fails, then the device really doesn't exist */
      if (fd == -1)
	{
	  zlog_warn ("%s: unknown interface %s", __func__,
	    circuit->interface->name);
	  return ISIS_WARNING;
	}

      /* Double check the DLPI style */
      if (dia->dl_provider_style != DL_STYLE2)
	{
	  zlog_warn ("open_dlpi_dev(): interface %s: %s is not style 2",
	    circuit->interface->name, devpath);
	  close (fd);
	  return ISIS_WARNING;
	}

      /* If it succeeds, then we need to attach to the unit specified */
      dlpiattach (fd, unit);

      /* Reget the information, as it may be different per node */
      if ((acklen = dlpiinfo (fd)) == -1)
	{
	  close (fd);
	  return ISIS_WARNING;
	}
    }
  else
    {
      /* Double check the DLPI style */
      if (dia->dl_provider_style != DL_STYLE1)
	{
	  zlog_warn ("open_dlpi_dev(): interface %s: %s is not style 1",
	    circuit->interface->name, devpath);
	  close (fd);
	  return ISIS_WARNING;
	}
    }

  /* Check that the interface we've got is the kind we expect */
  if ((dia->dl_sap_length != 2 && dia->dl_sap_length != -2) ||
    dia->dl_service_mode != DL_CLDLS || dia->dl_addr_length != ETHERADDRL + 2 ||
    dia->dl_brdcst_addr_length != ETHERADDRL)
    {
      zlog_warn ("%s: unsupported interface type for %s", __func__,
	circuit->interface->name);
      close (fd);
      return ISIS_WARNING;
    }
  switch (dia->dl_mac_type)
    {
    case DL_CSMACD:
    case DL_ETHER:
    case DL_100VG:
    case DL_100VGTPR:
    case DL_ETH_CSMA:
    case DL_100BT:
      break;
    default:
      zlog_warn ("%s: unexpected mac type on %s: %d", __func__,
	circuit->interface->name, dia->dl_mac_type);
      close (fd);
      return ISIS_WARNING;
    }

  circuit->sap_length = dia->dl_sap_length;

  /*
   * The local hardware address is something that should be provided by way of
   * sockaddr_dl for the interface, but isn't on Solaris.  We set it here based
   * on DLPI's reported address to avoid roto-tilling the world.
   * (Note that isis_circuit_if_add on Solaris doesn't set the snpa.)
   *
   * Unfortunately, GLD is broken and doesn't provide the address after attach,
   * so we need to be careful and use DL_PHYS_ADDR_REQ instead.
   */
  if (dlpiaddr (fd, circuit->u.bc.snpa) == -1)
    {
      zlog_warn ("open_dlpi_dev(): interface %s: unable to get MAC address",
	circuit->interface->name);
      close (fd);
      return ISIS_WARNING;
    }

  /* Now bind to SAP 0.  This gives us 802-type traffic. */
  if (dlpibind (fd) == -1)
    {
      zlog_warn ("%s: cannot bind SAP 0 on %s", __func__,
	circuit->interface->name);
      close (fd);
      return ISIS_WARNING;
    }

  /*
   * Join to multicast groups according to
   * 8.4.2 - Broadcast subnetwork IIH PDUs
   */
  retval = 0;
  if (circuit->circuit_is_type & IS_LEVEL_1)
    {
      retval |= dlpimcast (fd, ALL_L1_ISS);
      retval |= dlpimcast (fd, ALL_ISS);
    }
  if (circuit->circuit_is_type & IS_LEVEL_2)
    retval |= dlpimcast (fd, ALL_L2_ISS);

  if (retval != 0)
    {
      zlog_warn ("%s: unable to join multicast on %s", __func__,
	circuit->interface->name);
      close (fd);
      return ISIS_WARNING;
    }

  /* Push on the packet filter to avoid stray 802 packets */
  if (ioctl (fd, I_PUSH, "pfmod") == 0)
    {
      struct packetfilt pfil;

      pfil.Pf_Priority = 0;
      pfil.Pf_FilterLen = sizeof (pf_filter) / sizeof (u_short);
      memcpy (pfil.Pf_Filter, pf_filter, sizeof (pf_filter));
      ioctl (fd, PFIOCSETF, &pfil);
    }

  circuit->fd = fd;

  return ISIS_OK;
}

/*
 * Create the socket and set the tx/rx funcs
 */
int
isis_sock_init (struct isis_circuit *circuit)
{
  int retval = ISIS_OK;

  if (isisd_privs.change (ZPRIVS_RAISE))
    zlog_err ("%s: could not raise privs, %s", __func__, safe_strerror (errno));

  retval = open_dlpi_dev (circuit);

  if (retval != ISIS_OK)
    {
      zlog_warn ("%s: could not initialize the socket", __func__);
      goto end;
    }

  if (circuit->circ_type == CIRCUIT_T_BROADCAST)
    {
      circuit->tx = isis_send_pdu_bcast;
      circuit->rx = isis_recv_pdu_bcast;
    }
  else
    {
      zlog_warn ("isis_sock_init(): unknown circuit type");
      retval = ISIS_WARNING;
      goto end;
    }

end:
  if (isisd_privs.change (ZPRIVS_LOWER))
    zlog_err ("%s: could not lower privs, %s", __func__, safe_strerror (errno));

  return retval;
}

int
isis_recv_pdu_bcast (struct isis_circuit *circuit, u_char * ssnpa)
{
  struct pollfd fds[1];
  struct strbuf ctlbuf, databuf;
  int flags, retv;
  dl_unitdata_ind_t *dui = (dl_unitdata_ind_t *)dlpi_ctl;

  memset (fds, 0, sizeof (fds));
  fds[0].fd = circuit->fd;
  fds[0].events = POLLIN | POLLPRI;
  if (poll (fds, 1, 0) <= 0)
    return ISIS_WARNING;

  memset (&ctlbuf, 0, sizeof (ctlbuf));
  memset (&databuf, 0, sizeof (databuf));
  ctlbuf.maxlen = sizeof (dlpi_ctl);
  ctlbuf.buf = (void *)dlpi_ctl;
  databuf.maxlen = sizeof (sock_buff);
  databuf.buf = (void *)sock_buff;
  flags = 0;
  retv = getmsg (circuit->fd, &ctlbuf, &databuf, &flags);

  if (retv < 0)
    {
      zlog_warn ("isis_recv_pdu_bcast: getmsg failed: %s",
		 safe_strerror (errno));
      return ISIS_WARNING;
    }

  if (retv & (MORECTL | MOREDATA))
    {
      while (retv & (MORECTL | MOREDATA))
	{
	  flags = 0;
	  retv = getmsg (circuit->fd, &ctlbuf, &databuf, &flags);
	}
      return ISIS_WARNING;
    }

  if (ctlbuf.len < DL_UNITDATA_IND_SIZE ||
    dui->dl_primitive != DL_UNITDATA_IND)
    return ISIS_WARNING;

  if (dui->dl_src_addr_length != ETHERADDRL + 2 ||
    dui->dl_src_addr_offset < DL_UNITDATA_IND_SIZE ||
    dui->dl_src_addr_offset + dui->dl_src_addr_length > ctlbuf.len)
    return ISIS_WARNING;

  memcpy (ssnpa, (char *)dui + dui->dl_src_addr_offset +
    (circuit->sap_length > 0 ? circuit->sap_length : 0), ETHERADDRL);

  if (databuf.len < LLC_LEN || sock_buff[0] != ISO_SAP ||
    sock_buff[1] != ISO_SAP || sock_buff[2] != 3)
    return ISIS_WARNING;

  stream_write (circuit->rcv_stream, sock_buff + LLC_LEN,
                databuf.len - LLC_LEN);
  stream_set_getp (circuit->rcv_stream, 0);

  return ISIS_OK;
}

int
isis_send_pdu_bcast (struct isis_circuit *circuit, int level)
{
  dl_unitdata_req_t *dur = (dl_unitdata_req_t *)dlpi_ctl;
  char *dstaddr;
  u_short *dstsap;

  stream_set_getp (circuit->snd_stream, 0);

  memset (dur, 0, sizeof (*dur));
  dur->dl_primitive = DL_UNITDATA_REQ;
  dur->dl_dest_addr_length = ETHERADDRL + 2;
  dur->dl_dest_addr_offset = sizeof (*dur);

  dstaddr = (char *)(dur + 1);
  if (circuit->sap_length < 0)
    {
      dstsap = (u_short *)(dstaddr + ETHERADDRL);
    }
  else
    {
      dstsap = (u_short *)dstaddr;
      dstaddr += circuit->sap_length;
    }
  if (level == 1)
    memcpy (dstaddr, ALL_L1_ISS, ETHERADDRL);
  else
    memcpy (dstaddr, ALL_L2_ISS, ETHERADDRL);
  /* Note: DLPI SAP values are in host byte order */
  *dstsap = stream_get_endp (circuit->snd_stream) + LLC_LEN;

  sock_buff[0] = ISO_SAP;
  sock_buff[1] = ISO_SAP;
  sock_buff[2] = 0x03;
  memcpy (sock_buff + LLC_LEN, circuit->snd_stream->data,
	  stream_get_endp (circuit->snd_stream));
  dlpisend (circuit->fd, dur, sizeof (*dur) + dur->dl_dest_addr_length,
    sock_buff, stream_get_endp (circuit->snd_stream) + LLC_LEN, 0);
  return ISIS_OK;
}