2 * IS-IS Rout(e)ing protocol - isis_dlpi.c
4 * Copyright (C) 2001,2002 Sampo Saaristo
5 * Tampere University of Technology
6 * Institute of Communications Engineering
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)
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
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #if ISIS_METHOD == ISIS_METHOD_DLPI
26 #include <netinet/if_ether.h>
27 #include <sys/types.h>
33 #include <sys/pfmod.h>
40 #include "isisd/dict.h"
41 #include "isisd/isis_constants.h"
42 #include "isisd/isis_common.h"
43 #include "isisd/isis_circuit.h"
44 #include "isisd/isis_flags.h"
45 #include "isisd/isisd.h"
46 #include "isisd/isis_network.h"
50 extern struct zebra_privs_t isisd_privs
;
52 static t_uscalar_t dlpi_ctl
[1024]; /* DLPI control messages */
55 * Table 9 - Architectural constants for use with ISO 8802 subnetworks
59 u_char ALL_L1_ISS
[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x14 };
60 u_char ALL_L2_ISS
[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x15 };
61 u_char ALL_ISS
[6] = { 0x09, 0x00, 0x2B, 0x00, 0x00, 0x05 };
62 u_char ALL_ESS
[6] = { 0x09, 0x00, 0x2B, 0x00, 0x00, 0x04 };
64 static u_char sock_buff
[8192];
66 static u_short pf_filter
[] =
68 ENF_PUSHWORD
+ 0, /* Get the SSAP/DSAP values */
69 ENF_PUSHLIT
| ENF_CAND
, /* Check them */
70 ISO_SAP
| (ISO_SAP
<< 8),
71 ENF_PUSHWORD
+ 1, /* Get the control value */
72 ENF_PUSHLIT
| ENF_AND
, /* Isolate it */
78 ENF_PUSHLIT
| ENF_CAND
, /* Test for expected value */
87 * We would like to use something like libdlpi here, but that's not present on
88 * all versions of Solaris or on any non-Solaris system, so it's nowhere near
89 * as portable as we'd like. Thus, we use the standards-conformant DLPI
90 * interfaces plus the (optional; not needed) Solaris packet filter module.
94 dlpisend (int fd
, const void *cbuf
, size_t cbuflen
,
95 const void *dbuf
, size_t dbuflen
, int flags
)
97 const struct strbuf
*ctlptr
= NULL
;
98 const struct strbuf
*dataptr
= NULL
;
99 struct strbuf ctlbuf
, databuf
;
104 memset (&ctlbuf
, 0, sizeof (ctlbuf
));
105 ctlbuf
.len
= cbuflen
;
106 ctlbuf
.buf
= (void *)cbuf
;
112 memset (&databuf
, 0, sizeof (databuf
));
113 databuf
.len
= dbuflen
;
114 databuf
.buf
= (void *)dbuf
;
118 /* We assume this doesn't happen often and isn't operationally significant */
119 rv
= putmsg(fd
, ctlptr
, dataptr
, flags
);
120 if (rv
== -1 && dbuf
== NULL
)
123 * For actual PDU transmission - recognizable buf dbuf != NULL,
124 * the error is passed upwards and should not be printed here.
126 zlog_debug ("%s: putmsg: %s", __func__
, safe_strerror (errno
));
134 struct pollfd fds
[1];
135 struct strbuf ctlbuf
, databuf
;
140 /* Poll is used here in case the device doesn't speak DLPI correctly */
141 memset (fds
, 0, sizeof (fds
));
143 fds
[0].events
= POLLIN
| POLLPRI
;
144 if (poll (fds
, 1, 1000) <= 0)
147 memset (&ctlbuf
, 0, sizeof (ctlbuf
));
148 memset (&databuf
, 0, sizeof (databuf
));
149 ctlbuf
.maxlen
= sizeof (dlpi_ctl
);
150 ctlbuf
.buf
= (void *)dlpi_ctl
;
151 databuf
.maxlen
= sizeof (sock_buff
);
152 databuf
.buf
= (void *)sock_buff
;
154 retv
= getmsg (fd
, &ctlbuf
, &databuf
, &flags
);
159 while (ctlbuf
.len
== 0);
161 if (!(retv
& MORECTL
))
163 while (retv
& MOREDATA
)
166 retv
= getmsg (fd
, NULL
, &databuf
, &flags
);
171 while (retv
& MORECTL
)
174 retv
= getmsg (fd
, &ctlbuf
, &databuf
, &flags
);
180 dlpiok (int fd
, t_uscalar_t oprim
)
183 dl_ok_ack_t
*doa
= (dl_ok_ack_t
*)dlpi_ctl
;
185 retv
= dlpirctl (fd
);
186 if (retv
< (ssize_t
)DL_OK_ACK_SIZE
|| doa
->dl_primitive
!= DL_OK_ACK
||
187 doa
->dl_correct_primitive
!= oprim
)
203 memset (&dir
, 0, sizeof (dir
));
204 dir
.dl_primitive
= DL_INFO_REQ
;
205 /* Info_req uses M_PCPROTO. */
206 dlpisend (fd
, &dir
, sizeof (dir
), NULL
, 0, RS_HIPRI
);
207 retv
= dlpirctl (fd
);
208 if (retv
< (ssize_t
)DL_INFO_ACK_SIZE
|| dlpi_ctl
[0] != DL_INFO_ACK
)
215 dlpiopen (const char *devpath
, ssize_t
*acklen
)
219 fd
= open (devpath
, O_RDWR
| O_NONBLOCK
| O_NOCTTY
);
223 /* All that we want is for the open itself to be non-blocking, not I/O. */
224 flags
= fcntl (fd
, F_GETFL
, 0);
226 fcntl (fd
, F_SETFL
, flags
& ~O_NONBLOCK
);
228 /* After opening, ask for information */
229 if ((*acklen
= dlpiinfo (fd
)) == -1)
239 dlpiattach (int fd
, int unit
)
243 memset (&dar
, 0, sizeof (dar
));
244 dar
.dl_primitive
= DL_ATTACH_REQ
;
246 dlpisend (fd
, &dar
, sizeof (dar
), NULL
, 0, 0);
247 return dlpiok (fd
, dar
.dl_primitive
);
255 dl_bind_ack_t
*dba
= (dl_bind_ack_t
*)dlpi_ctl
;
257 memset (&dbr
, 0, sizeof (dbr
));
258 dbr
.dl_primitive
= DL_BIND_REQ
;
259 dbr
.dl_service_mode
= DL_CLDLS
;
260 dlpisend (fd
, &dbr
, sizeof (dbr
), NULL
, 0, 0);
262 retv
= dlpirctl (fd
);
263 if (retv
< (ssize_t
)DL_BIND_ACK_SIZE
|| dba
->dl_primitive
!= DL_BIND_ACK
)
270 dlpimcast (int fd
, const u_char
*mcaddr
)
273 dl_enabmulti_req_t der
;
274 u_char addr
[ETHERADDRL
];
277 memset (&dler
, 0, sizeof (dler
));
278 dler
.der
.dl_primitive
= DL_ENABMULTI_REQ
;
279 dler
.der
.dl_addr_length
= sizeof (dler
.addr
);
280 dler
.der
.dl_addr_offset
= dler
.addr
- (u_char
*)&dler
;
281 memcpy (dler
.addr
, mcaddr
, sizeof (dler
.addr
));
282 dlpisend (fd
, &dler
, sizeof (dler
), NULL
, 0, 0);
283 return dlpiok (fd
, dler
.der
.dl_primitive
);
287 dlpiaddr (int fd
, u_char
*addr
)
289 dl_phys_addr_req_t dpar
;
290 dl_phys_addr_ack_t
*dpaa
= (dl_phys_addr_ack_t
*)dlpi_ctl
;
293 memset (&dpar
, 0, sizeof (dpar
));
294 dpar
.dl_primitive
= DL_PHYS_ADDR_REQ
;
295 dpar
.dl_addr_type
= DL_CURR_PHYS_ADDR
;
296 dlpisend (fd
, &dpar
, sizeof (dpar
), NULL
, 0, 0);
298 retv
= dlpirctl (fd
);
299 if (retv
< (ssize_t
)DL_PHYS_ADDR_ACK_SIZE
300 || dpaa
->dl_primitive
!= DL_PHYS_ADDR_ACK
)
303 if (dpaa
->dl_addr_offset
< DL_PHYS_ADDR_ACK_SIZE
||
304 dpaa
->dl_addr_length
!= ETHERADDRL
||
305 dpaa
->dl_addr_offset
+ dpaa
->dl_addr_length
> (size_t)retv
)
308 bcopy((char *)dpaa
+ dpaa
->dl_addr_offset
, addr
, ETHERADDRL
);
313 open_dlpi_dev (struct isis_circuit
*circuit
)
315 int fd
= -1, unit
, retval
;
316 char devpath
[MAXPATHLEN
];
317 dl_info_ack_t
*dia
= (dl_info_ack_t
*)dlpi_ctl
;
320 /* Only broadcast-type are supported at the moment */
321 if (circuit
->circ_type
!= CIRCUIT_T_BROADCAST
)
323 zlog_warn ("%s: non-broadcast interface %s", __func__
,
324 circuit
->interface
->name
);
328 /* Try the vanity node first, if permitted */
329 if (getenv("DLPI_DEVONLY") == NULL
)
331 (void) snprintf (devpath
, sizeof(devpath
), "/dev/net/%s",
332 circuit
->interface
->name
);
333 fd
= dlpiopen (devpath
, &acklen
);
336 /* Now try as an ordinary Style 1 node */
339 (void) snprintf (devpath
, sizeof (devpath
), "/dev/%s",
340 circuit
->interface
->name
);
342 fd
= dlpiopen (devpath
, &acklen
);
345 /* If that fails, try again as Style 2 */
350 cp
= devpath
+ strlen (devpath
);
351 while (--cp
>= devpath
&& isdigit(*cp
))
353 unit
= strtol(cp
, NULL
, 0);
355 fd
= dlpiopen (devpath
, &acklen
);
357 /* If that too fails, then the device really doesn't exist */
360 zlog_warn ("%s: unknown interface %s", __func__
,
361 circuit
->interface
->name
);
365 /* Double check the DLPI style */
366 if (dia
->dl_provider_style
!= DL_STYLE2
)
368 zlog_warn ("open_dlpi_dev(): interface %s: %s is not style 2",
369 circuit
->interface
->name
, devpath
);
374 /* If it succeeds, then we need to attach to the unit specified */
375 dlpiattach (fd
, unit
);
377 /* Reget the information, as it may be different per node */
378 if ((acklen
= dlpiinfo (fd
)) == -1)
386 /* Double check the DLPI style */
387 if (dia
->dl_provider_style
!= DL_STYLE1
)
389 zlog_warn ("open_dlpi_dev(): interface %s: %s is not style 1",
390 circuit
->interface
->name
, devpath
);
396 /* Check that the interface we've got is the kind we expect */
397 if ((dia
->dl_sap_length
!= 2 && dia
->dl_sap_length
!= -2) ||
398 dia
->dl_service_mode
!= DL_CLDLS
|| dia
->dl_addr_length
!= ETHERADDRL
+ 2 ||
399 dia
->dl_brdcst_addr_length
!= ETHERADDRL
)
401 zlog_warn ("%s: unsupported interface type for %s", __func__
,
402 circuit
->interface
->name
);
406 switch (dia
->dl_mac_type
)
416 zlog_warn ("%s: unexpected mac type on %s: %lld", __func__
,
417 circuit
->interface
->name
, (long long)dia
->dl_mac_type
);
422 circuit
->sap_length
= dia
->dl_sap_length
;
425 * The local hardware address is something that should be provided by way of
426 * sockaddr_dl for the interface, but isn't on Solaris. We set it here based
427 * on DLPI's reported address to avoid roto-tilling the world.
428 * (Note that isis_circuit_if_add on Solaris doesn't set the snpa.)
430 * Unfortunately, GLD is broken and doesn't provide the address after attach,
431 * so we need to be careful and use DL_PHYS_ADDR_REQ instead.
433 if (dlpiaddr (fd
, circuit
->u
.bc
.snpa
) == -1)
435 zlog_warn ("open_dlpi_dev(): interface %s: unable to get MAC address",
436 circuit
->interface
->name
);
441 /* Now bind to SAP 0. This gives us 802-type traffic. */
442 if (dlpibind (fd
) == -1)
444 zlog_warn ("%s: cannot bind SAP 0 on %s", __func__
,
445 circuit
->interface
->name
);
451 * Join to multicast groups according to
452 * 8.4.2 - Broadcast subnetwork IIH PDUs
455 retval
|= dlpimcast (fd
, ALL_L1_ISS
);
456 retval
|= dlpimcast (fd
, ALL_ISS
);
457 retval
|= dlpimcast (fd
, ALL_L2_ISS
);
461 zlog_warn ("%s: unable to join multicast on %s", __func__
,
462 circuit
->interface
->name
);
467 /* Push on the packet filter to avoid stray 802 packets */
468 if (ioctl (fd
, I_PUSH
, "pfmod") == 0)
470 struct packetfilt pfil
;
471 struct strioctl sioc
;
473 pfil
.Pf_Priority
= 0;
474 pfil
.Pf_FilterLen
= sizeof (pf_filter
) / sizeof (u_short
);
475 memcpy (pfil
.Pf_Filter
, pf_filter
, sizeof (pf_filter
));
476 /* pfmod does not support transparent ioctls */
477 sioc
.ic_cmd
= PFIOCSETF
;
479 sioc
.ic_len
= sizeof (struct packetfilt
);
480 sioc
.ic_dp
= (char *)&pfil
;
481 if (ioctl (fd
, I_STR
, &sioc
) == -1)
482 zlog_warn("%s: could not perform PF_IOCSETF on %s",
483 __func__
, circuit
->interface
->name
);
492 * Create the socket and set the tx/rx funcs
495 isis_sock_init (struct isis_circuit
*circuit
)
497 int retval
= ISIS_OK
;
499 if (isisd_privs
.change (ZPRIVS_RAISE
))
500 zlog_err ("%s: could not raise privs, %s", __func__
, safe_strerror (errno
));
502 retval
= open_dlpi_dev (circuit
);
504 if (retval
!= ISIS_OK
)
506 zlog_warn ("%s: could not initialize the socket", __func__
);
510 if (circuit
->circ_type
== CIRCUIT_T_BROADCAST
)
512 circuit
->tx
= isis_send_pdu_bcast
;
513 circuit
->rx
= isis_recv_pdu_bcast
;
517 zlog_warn ("isis_sock_init(): unknown circuit type");
518 retval
= ISIS_WARNING
;
523 if (isisd_privs
.change (ZPRIVS_LOWER
))
524 zlog_err ("%s: could not lower privs, %s", __func__
, safe_strerror (errno
));
530 isis_recv_pdu_bcast (struct isis_circuit
*circuit
, u_char
* ssnpa
)
532 struct pollfd fds
[1];
533 struct strbuf ctlbuf
, databuf
;
535 dl_unitdata_ind_t
*dui
= (dl_unitdata_ind_t
*)dlpi_ctl
;
537 memset (fds
, 0, sizeof (fds
));
538 fds
[0].fd
= circuit
->fd
;
539 fds
[0].events
= POLLIN
| POLLPRI
;
540 if (poll (fds
, 1, 0) <= 0)
543 memset (&ctlbuf
, 0, sizeof (ctlbuf
));
544 memset (&databuf
, 0, sizeof (databuf
));
545 ctlbuf
.maxlen
= sizeof (dlpi_ctl
);
546 ctlbuf
.buf
= (void *)dlpi_ctl
;
547 databuf
.maxlen
= sizeof (sock_buff
);
548 databuf
.buf
= (void *)sock_buff
;
550 retv
= getmsg (circuit
->fd
, &ctlbuf
, &databuf
, &flags
);
554 zlog_warn ("isis_recv_pdu_bcast: getmsg failed: %s",
555 safe_strerror (errno
));
559 if (retv
& (MORECTL
| MOREDATA
))
561 while (retv
& (MORECTL
| MOREDATA
))
564 retv
= getmsg (circuit
->fd
, &ctlbuf
, &databuf
, &flags
);
569 if (ctlbuf
.len
< (ssize_t
)DL_UNITDATA_IND_SIZE
||
570 dui
->dl_primitive
!= DL_UNITDATA_IND
)
573 if (dui
->dl_src_addr_length
!= ETHERADDRL
+ 2 ||
574 dui
->dl_src_addr_offset
< DL_UNITDATA_IND_SIZE
||
575 dui
->dl_src_addr_offset
+ dui
->dl_src_addr_length
> (size_t)ctlbuf
.len
)
578 memcpy (ssnpa
, (char *)dui
+ dui
->dl_src_addr_offset
+
579 (circuit
->sap_length
> 0 ? circuit
->sap_length
: 0), ETHERADDRL
);
581 if (databuf
.len
< LLC_LEN
|| sock_buff
[0] != ISO_SAP
||
582 sock_buff
[1] != ISO_SAP
|| sock_buff
[2] != 3)
585 stream_write (circuit
->rcv_stream
, sock_buff
+ LLC_LEN
,
586 databuf
.len
- LLC_LEN
);
587 stream_set_getp (circuit
->rcv_stream
, 0);
593 isis_send_pdu_bcast (struct isis_circuit
*circuit
, int level
)
595 dl_unitdata_req_t
*dur
= (dl_unitdata_req_t
*)dlpi_ctl
;
601 buflen
= stream_get_endp (circuit
->snd_stream
) + LLC_LEN
;
602 if ((size_t)buflen
> sizeof (sock_buff
))
604 zlog_warn ("isis_send_pdu_bcast: sock_buff size %zu is less than "
605 "output pdu size %d on circuit %s",
606 sizeof (sock_buff
), buflen
, circuit
->interface
->name
);
610 stream_set_getp (circuit
->snd_stream
, 0);
612 memset (dur
, 0, sizeof (*dur
));
613 dur
->dl_primitive
= DL_UNITDATA_REQ
;
614 dur
->dl_dest_addr_length
= ETHERADDRL
+ 2;
615 dur
->dl_dest_addr_offset
= sizeof (*dur
);
617 dstaddr
= (char *)(dur
+ 1);
618 if (circuit
->sap_length
< 0)
620 dstsap
= (u_short
*)(dstaddr
+ ETHERADDRL
);
624 dstsap
= (u_short
*)dstaddr
;
625 dstaddr
+= circuit
->sap_length
;
628 memcpy (dstaddr
, ALL_L1_ISS
, ETHERADDRL
);
630 memcpy (dstaddr
, ALL_L2_ISS
, ETHERADDRL
);
631 /* Note: DLPI SAP values are in host byte order */
634 sock_buff
[0] = ISO_SAP
;
635 sock_buff
[1] = ISO_SAP
;
637 memcpy (sock_buff
+ LLC_LEN
, circuit
->snd_stream
->data
,
638 stream_get_endp (circuit
->snd_stream
));
639 rv
= dlpisend(circuit
->fd
, dur
, sizeof (*dur
) + dur
->dl_dest_addr_length
,
640 sock_buff
, buflen
, 0);
643 zlog_warn("IS-IS dlpi: could not transmit packet on %s: %s",
644 circuit
->interface
->name
, safe_strerror(errno
));
645 if (ERRNO_IO_RETRY(errno
))
653 #endif /* ISIS_METHOD == ISIS_METHOD_DLPI */