blob: db4383bed2d31fce6420cb5ac8cfe8eb42130ac9 [file] [log] [blame]
Paul Jakma8bc98052007-08-08 11:19:27 +00001/*
2 * IS-IS Rout(e)ing protocol - isis_dlpi.c
3 *
4 * Copyright (C) 2001,2002 Sampo Saaristo
5 * Tampere University of Technology
6 * Institute of Communications Engineering
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public Licenseas published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23#include <zebra.h>
24#include <net/if.h>
25#include <netinet/if_ether.h>
26#include <sys/types.h>
27#include <unistd.h>
28#include <fcntl.h>
29#include <stropts.h>
30#include <poll.h>
31#include <sys/dlpi.h>
32#include <sys/pfmod.h>
33
34#include "log.h"
35#include "stream.h"
36#include "if.h"
37
38#include "isisd/dict.h"
39#include "isisd/include-netbsd/iso.h"
40#include "isisd/isis_constants.h"
41#include "isisd/isis_common.h"
42#include "isisd/isis_circuit.h"
43#include "isisd/isis_flags.h"
44#include "isisd/isisd.h"
45#include "isisd/isis_constants.h"
46#include "isisd/isis_circuit.h"
47#include "isisd/isis_network.h"
48
49#include "privs.h"
50
51extern struct zebra_privs_t isisd_privs;
52
53static t_uscalar_t dlpi_ctl[1024]; /* DLPI control messages */
54
55/*
56 * Table 9 - Architectural constants for use with ISO 8802 subnetworks
57 * ISO 10589 - 8.4.8
58 */
59
60u_char ALL_L1_ISS[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x14 };
61u_char ALL_L2_ISS[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x15 };
62u_char ALL_ISS[6] = { 0x09, 0x00, 0x2B, 0x00, 0x00, 0x05 };
63u_char ALL_ESS[6] = { 0x09, 0x00, 0x2B, 0x00, 0x00, 0x04 };
64
65static u_char sock_buff[8192];
66
67static u_short pf_filter[] =
68{
69 ENF_PUSHWORD + 0, /* Get the SSAP/DSAP values */
70 ENF_PUSHLIT | ENF_CAND, /* Check them */
71 ISO_SAP | (ISO_SAP << 8),
72 ENF_PUSHWORD + 1, /* Get the control value */
73 ENF_PUSHLIT | ENF_AND, /* Isolate it */
74#ifdef _BIG_ENDIAN
75 0xFF00,
76#else
77 0x00FF,
78#endif
79 ENF_PUSHLIT | ENF_CAND, /* Test for expected value */
80#ifdef _BIG_ENDIAN
81 0x0300
82#else
83 0x0003
84#endif
85};
86
87/*
88 * We would like to use something like libdlpi here, but that's not present on
89 * all versions of Solaris or on any non-Solaris system, so it's nowhere near
90 * as portable as we'd like. Thus, we use the standards-conformant DLPI
91 * interfaces plus the (optional; not needed) Solaris packet filter module.
92 */
93
94static void
95dlpisend (int fd, const void *cbuf, size_t cbuflen,
96 const void *dbuf, size_t dbuflen, int flags)
97{
98 const struct strbuf *ctlptr = NULL;
99 const struct strbuf *dataptr = NULL;
100 struct strbuf ctlbuf, databuf;
101
102 if (cbuf != NULL)
103 {
104 memset (&ctlbuf, 0, sizeof (ctlbuf));
105 ctlbuf.len = cbuflen;
106 ctlbuf.buf = (void *)cbuf;
107 ctlptr = &ctlbuf;
108 }
109
110 if (dbuf != NULL)
111 {
112 memset (&databuf, 0, sizeof (databuf));
113 databuf.len = dbuflen;
114 databuf.buf = (void *)dbuf;
115 dataptr = &databuf;
116 }
117
118 /* We assume this doesn't happen often and isn't operationally significant */
119 if (putmsg (fd, ctlptr, dataptr, flags) == -1)
120 zlog_debug ("%s: putmsg: %s", __func__, safe_strerror (errno));
121}
122
123static ssize_t
124dlpirctl (int fd)
125{
126 struct pollfd fds[1];
127 struct strbuf ctlbuf, databuf;
128 int flags, retv;
129
130 do
131 {
132 /* Poll is used here in case the device doesn't speak DLPI correctly */
133 memset (fds, 0, sizeof (fds));
134 fds[0].fd = fd;
135 fds[0].events = POLLIN | POLLPRI;
136 if (poll (fds, 1, 1000) <= 0)
137 return -1;
138
139 memset (&ctlbuf, 0, sizeof (ctlbuf));
140 memset (&databuf, 0, sizeof (databuf));
141 ctlbuf.maxlen = sizeof (dlpi_ctl);
142 ctlbuf.buf = (void *)dlpi_ctl;
143 databuf.maxlen = sizeof (sock_buff);
144 databuf.buf = (void *)sock_buff;
145 flags = 0;
146 retv = getmsg (fd, &ctlbuf, &databuf, &flags);
147
148 if (retv < 0)
149 return -1;
150 }
151 while (ctlbuf.len == 0);
152
153 if (!(retv & MORECTL))
154 {
155 while (retv & MOREDATA)
156 {
157 flags = 0;
158 retv = getmsg (fd, NULL, &databuf, &flags);
159 }
160 return ctlbuf.len;
161 }
162
163 while (retv & MORECTL)
164 {
165 flags = 0;
166 retv = getmsg (fd, &ctlbuf, &databuf, &flags);
167 }
168 return -1;
169}
170
171static int
172dlpiok (int fd, t_uscalar_t oprim)
173{
174 int retv;
175 dl_ok_ack_t *doa = (dl_ok_ack_t *)dlpi_ctl;
176
177 retv = dlpirctl (fd);
178 if (retv < DL_OK_ACK_SIZE || doa->dl_primitive != DL_OK_ACK ||
179 doa->dl_correct_primitive != oprim)
180 {
181 return -1;
182 }
183 else
184 {
185 return 0;
186 }
187}
188
189static int
190dlpiinfo (int fd)
191{
192 dl_info_req_t dir;
193 ssize_t retv;
194
195 memset (&dir, 0, sizeof (dir));
196 dir.dl_primitive = DL_INFO_REQ;
197 /* Info_req uses M_PCPROTO. */
198 dlpisend (fd, &dir, sizeof (dir), NULL, 0, RS_HIPRI);
199 retv = dlpirctl (fd);
200 if (retv < DL_INFO_ACK_SIZE || dlpi_ctl[0] != DL_INFO_ACK)
201 return -1;
202 else
203 return retv;
204}
205
206static int
207dlpiopen (const char *devpath, ssize_t *acklen)
208{
209 int fd, flags;
210
211 fd = open (devpath, O_RDWR | O_NONBLOCK | O_NOCTTY);
212 if (fd == -1)
213 return -1;
214
215 /* All that we want is for the open itself to be non-blocking, not I/O. */
216 flags = fcntl (fd, F_GETFL, 0);
217 if (flags != -1)
218 fcntl (fd, F_SETFL, flags & ~O_NONBLOCK);
219
220 /* After opening, ask for information */
221 if ((*acklen = dlpiinfo (fd)) == -1)
222 {
223 close (fd);
224 return -1;
225 }
226
227 return fd;
228}
229
230static int
231dlpiattach (int fd, int unit)
232{
233 dl_attach_req_t dar;
234
235 memset (&dar, 0, sizeof (dar));
236 dar.dl_primitive = DL_ATTACH_REQ;
237 dar.dl_ppa = unit;
238 dlpisend (fd, &dar, sizeof (dar), NULL, 0, 0);
239 return dlpiok (fd, dar.dl_primitive);
240}
241
242static int
243dlpibind (int fd)
244{
245 dl_bind_req_t dbr;
246 int retv;
247 dl_bind_ack_t *dba = (dl_bind_ack_t *)dlpi_ctl;
248
249 memset (&dbr, 0, sizeof (dbr));
250 dbr.dl_primitive = DL_BIND_REQ;
251 dbr.dl_service_mode = DL_CLDLS;
252 dlpisend (fd, &dbr, sizeof (dbr), NULL, 0, 0);
253
254 retv = dlpirctl (fd);
255 if (retv < DL_BIND_ACK_SIZE || dba->dl_primitive != DL_BIND_ACK)
256 return -1;
257 else
258 return 0;
259}
260
261static int
262dlpimcast (int fd, const u_char *mcaddr)
263{
264 struct {
265 dl_enabmulti_req_t der;
266 u_char addr[ETHERADDRL];
267 } dler;
268
269 memset (&dler, 0, sizeof (dler));
270 dler.der.dl_primitive = DL_ENABMULTI_REQ;
271 dler.der.dl_addr_length = sizeof (dler.addr);
272 dler.der.dl_addr_offset = dler.addr - (u_char *)&dler;
273 memcpy (dler.addr, mcaddr, sizeof (dler.addr));
274 dlpisend (fd, &dler, sizeof (dler), NULL, 0, 0);
275 return dlpiok (fd, dler.der.dl_primitive);
276}
277
278static int
279dlpiaddr (int fd, u_char *addr)
280{
281 dl_phys_addr_req_t dpar;
282 dl_phys_addr_ack_t *dpaa = (dl_phys_addr_ack_t *)dlpi_ctl;
283 int retv;
284
285 memset (&dpar, 0, sizeof (dpar));
286 dpar.dl_primitive = DL_PHYS_ADDR_REQ;
287 dpar.dl_addr_type = DL_CURR_PHYS_ADDR;
288 dlpisend (fd, &dpar, sizeof (dpar), NULL, 0, 0);
289
290 retv = dlpirctl (fd);
291 if (retv < DL_PHYS_ADDR_ACK_SIZE || dpaa->dl_primitive != DL_PHYS_ADDR_ACK)
292 return -1;
293
294 if (dpaa->dl_addr_offset < DL_PHYS_ADDR_ACK_SIZE ||
295 dpaa->dl_addr_length != ETHERADDRL ||
296 dpaa->dl_addr_offset + dpaa->dl_addr_length > retv)
297 return -1;
298
299 bcopy((char *)dpaa + dpaa->dl_addr_offset, addr, ETHERADDRL);
300 return 0;
301}
302
303static int
304open_dlpi_dev (struct isis_circuit *circuit)
305{
306 int fd, unit, retval;
307 char devpath[MAXPATHLEN];
308 dl_info_ack_t *dia = (dl_info_ack_t *)dlpi_ctl;
309 ssize_t acklen;
310
311 /* Only broadcast-type are supported at the moment */
312 if (circuit->circ_type != CIRCUIT_T_BROADCAST)
313 {
314 zlog_warn ("%s: non-broadcast interface %s", __func__,
315 circuit->interface->name);
316 return ISIS_WARNING;
317 }
318
319 /* Try first as Style 1 */
320 (void) snprintf(devpath, sizeof (devpath), "/dev/%s",
321 circuit->interface->name);
322 unit = -1;
323 fd = dlpiopen (devpath, &acklen);
324
325 /* If that fails, try again as Style 2 */
326 if (fd == -1)
327 {
328 char *cp;
329
330 cp = devpath + strlen (devpath);
331 while (--cp >= devpath && isdigit(*cp))
332 ;
333 unit = strtol(cp, NULL, 0);
334 *cp = '\0';
335 fd = dlpiopen (devpath, &acklen);
336
337 /* If that too fails, then the device really doesn't exist */
338 if (fd == -1)
339 {
340 zlog_warn ("%s: unknown interface %s", __func__,
341 circuit->interface->name);
342 return ISIS_WARNING;
343 }
344
345 /* Double check the DLPI style */
346 if (dia->dl_provider_style != DL_STYLE2)
347 {
348 zlog_warn ("open_dlpi_dev(): interface %s: %s is not style 2",
349 circuit->interface->name, devpath);
350 close (fd);
351 return ISIS_WARNING;
352 }
353
354 /* If it succeeds, then we need to attach to the unit specified */
355 dlpiattach (fd, unit);
356
357 /* Reget the information, as it may be different per node */
358 if ((acklen = dlpiinfo (fd)) == -1)
359 {
360 close (fd);
361 return ISIS_WARNING;
362 }
363 }
364 else
365 {
366 /* Double check the DLPI style */
367 if (dia->dl_provider_style != DL_STYLE1)
368 {
369 zlog_warn ("open_dlpi_dev(): interface %s: %s is not style 1",
370 circuit->interface->name, devpath);
371 close (fd);
372 return ISIS_WARNING;
373 }
374 }
375
376 /* Check that the interface we've got is the kind we expect */
377 if ((dia->dl_sap_length != 2 && dia->dl_sap_length != -2) ||
378 dia->dl_service_mode != DL_CLDLS || dia->dl_addr_length != ETHERADDRL + 2 ||
379 dia->dl_brdcst_addr_length != ETHERADDRL)
380 {
381 zlog_warn ("%s: unsupported interface type for %s", __func__,
382 circuit->interface->name);
383 close (fd);
384 return ISIS_WARNING;
385 }
386 switch (dia->dl_mac_type)
387 {
388 case DL_CSMACD:
389 case DL_ETHER:
390 case DL_100VG:
391 case DL_100VGTPR:
392 case DL_ETH_CSMA:
393 case DL_100BT:
394 break;
395 default:
396 zlog_warn ("%s: unexpected mac type on %s: %d", __func__,
397 circuit->interface->name, dia->dl_mac_type);
398 close (fd);
399 return ISIS_WARNING;
400 }
401
402 circuit->sap_length = dia->dl_sap_length;
403
404 /*
405 * The local hardware address is something that should be provided by way of
406 * sockaddr_dl for the interface, but isn't on Solaris. We set it here based
407 * on DLPI's reported address to avoid roto-tilling the world.
408 * (Note that isis_circuit_if_add on Solaris doesn't set the snpa.)
409 *
410 * Unfortunately, GLD is broken and doesn't provide the address after attach,
411 * so we need to be careful and use DL_PHYS_ADDR_REQ instead.
412 */
413 if (dlpiaddr (fd, circuit->u.bc.snpa) == -1)
414 {
415 zlog_warn ("open_dlpi_dev(): interface %s: unable to get MAC address",
416 circuit->interface->name);
417 close (fd);
418 return ISIS_WARNING;
419 }
420
421 /* Now bind to SAP 0. This gives us 802-type traffic. */
422 if (dlpibind (fd) == -1)
423 {
424 zlog_warn ("%s: cannot bind SAP 0 on %s", __func__,
425 circuit->interface->name);
426 close (fd);
427 return ISIS_WARNING;
428 }
429
430 /*
431 * Join to multicast groups according to
432 * 8.4.2 - Broadcast subnetwork IIH PDUs
433 */
434 retval = 0;
435 if (circuit->circuit_is_type & IS_LEVEL_1)
436 {
437 retval |= dlpimcast (fd, ALL_L1_ISS);
438 retval |= dlpimcast (fd, ALL_ISS);
439 }
440 if (circuit->circuit_is_type & IS_LEVEL_2)
441 retval |= dlpimcast (fd, ALL_L2_ISS);
442
443 if (retval != 0)
444 {
445 zlog_warn ("%s: unable to join multicast on %s", __func__,
446 circuit->interface->name);
447 close (fd);
448 return ISIS_WARNING;
449 }
450
451 /* Push on the packet filter to avoid stray 802 packets */
452 if (ioctl (fd, I_PUSH, "pfmod") == 0)
453 {
454 struct packetfilt pfil;
455
456 pfil.Pf_Priority = 0;
457 pfil.Pf_FilterLen = sizeof (pf_filter) / sizeof (u_short);
458 memcpy (pfil.Pf_Filter, pf_filter, sizeof (pf_filter));
459 ioctl (fd, PFIOCSETF, &pfil);
460 }
461
462 circuit->fd = fd;
463
464 return ISIS_OK;
465}
466
467/*
468 * Create the socket and set the tx/rx funcs
469 */
470int
471isis_sock_init (struct isis_circuit *circuit)
472{
473 int retval = ISIS_OK;
474
475 if (isisd_privs.change (ZPRIVS_RAISE))
476 zlog_err ("%s: could not raise privs, %s", __func__, safe_strerror (errno));
477
478 retval = open_dlpi_dev (circuit);
479
480 if (retval != ISIS_OK)
481 {
482 zlog_warn ("%s: could not initialize the socket", __func__);
483 goto end;
484 }
485
486 if (circuit->circ_type == CIRCUIT_T_BROADCAST)
487 {
488 circuit->tx = isis_send_pdu_bcast;
489 circuit->rx = isis_recv_pdu_bcast;
490 }
491 else
492 {
493 zlog_warn ("isis_sock_init(): unknown circuit type");
494 retval = ISIS_WARNING;
495 goto end;
496 }
497
498end:
499 if (isisd_privs.change (ZPRIVS_LOWER))
500 zlog_err ("%s: could not lower privs, %s", __func__, safe_strerror (errno));
501
502 return retval;
503}
504
505int
506isis_recv_pdu_bcast (struct isis_circuit *circuit, u_char * ssnpa)
507{
508 struct pollfd fds[1];
509 struct strbuf ctlbuf, databuf;
510 int flags, retv;
511 dl_unitdata_ind_t *dui = (dl_unitdata_ind_t *)dlpi_ctl;
512
513 memset (fds, 0, sizeof (fds));
514 fds[0].fd = circuit->fd;
515 fds[0].events = POLLIN | POLLPRI;
516 if (poll (fds, 1, 0) <= 0)
517 return ISIS_WARNING;
518
519 memset (&ctlbuf, 0, sizeof (ctlbuf));
520 memset (&databuf, 0, sizeof (databuf));
521 ctlbuf.maxlen = sizeof (dlpi_ctl);
522 ctlbuf.buf = (void *)dlpi_ctl;
523 databuf.maxlen = sizeof (sock_buff);
524 databuf.buf = (void *)sock_buff;
525 flags = 0;
526 retv = getmsg (circuit->fd, &ctlbuf, &databuf, &flags);
527
528 if (retv < 0)
529 {
530 zlog_warn ("isis_recv_pdu_bcast: getmsg failed: %s",
531 safe_strerror (errno));
532 return ISIS_WARNING;
533 }
534
535 if (retv & (MORECTL | MOREDATA))
536 {
537 while (retv & (MORECTL | MOREDATA))
538 {
539 flags = 0;
540 retv = getmsg (circuit->fd, &ctlbuf, &databuf, &flags);
541 }
542 return ISIS_WARNING;
543 }
544
545 if (ctlbuf.len < DL_UNITDATA_IND_SIZE ||
546 dui->dl_primitive != DL_UNITDATA_IND)
547 return ISIS_WARNING;
548
549 if (dui->dl_src_addr_length != ETHERADDRL + 2 ||
550 dui->dl_src_addr_offset < DL_UNITDATA_IND_SIZE ||
551 dui->dl_src_addr_offset + dui->dl_src_addr_length > ctlbuf.len)
552 return ISIS_WARNING;
553
554 memcpy (ssnpa, (char *)dui + dui->dl_src_addr_offset +
555 (circuit->sap_length > 0 ? circuit->sap_length : 0), ETHERADDRL);
556
557 if (databuf.len < LLC_LEN || sock_buff[0] != ISO_SAP ||
558 sock_buff[1] != ISO_SAP || sock_buff[2] != 3)
559 return ISIS_WARNING;
560
561 stream_write (circuit->rcv_stream, sock_buff + LLC_LEN,
562 databuf.len - LLC_LEN);
563 stream_set_getp (circuit->rcv_stream, 0);
564
565 return ISIS_OK;
566}
567
568int
569isis_send_pdu_bcast (struct isis_circuit *circuit, int level)
570{
571 dl_unitdata_req_t *dur = (dl_unitdata_req_t *)dlpi_ctl;
572 char *dstaddr;
573 u_short *dstsap;
574
575 stream_set_getp (circuit->snd_stream, 0);
576
577 memset (dur, 0, sizeof (*dur));
578 dur->dl_primitive = DL_UNITDATA_REQ;
579 dur->dl_dest_addr_length = ETHERADDRL + 2;
580 dur->dl_dest_addr_offset = sizeof (*dur);
581
582 dstaddr = (char *)(dur + 1);
583 if (circuit->sap_length < 0)
584 {
585 dstsap = (u_short *)(dstaddr + ETHERADDRL);
586 }
587 else
588 {
589 dstsap = (u_short *)dstaddr;
590 dstaddr += circuit->sap_length;
591 }
592 if (level == 1)
593 memcpy (dstaddr, ALL_L1_ISS, ETHERADDRL);
594 else
595 memcpy (dstaddr, ALL_L2_ISS, ETHERADDRL);
596 /* Note: DLPI SAP values are in host byte order */
597 *dstsap = stream_get_endp (circuit->snd_stream) + LLC_LEN;
598
599 sock_buff[0] = ISO_SAP;
600 sock_buff[1] = ISO_SAP;
601 sock_buff[2] = 0x03;
602 memcpy (sock_buff + LLC_LEN, circuit->snd_stream->data,
603 stream_get_endp (circuit->snd_stream));
604 dlpisend (circuit->fd, dur, sizeof (*dur) + dur->dl_dest_addr_length,
605 sock_buff, stream_get_endp (circuit->snd_stream) + LLC_LEN, 0);
606 return ISIS_OK;
607}