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; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #if ISIS_METHOD == ISIS_METHOD_DLPI
26 #include <netinet/if_ether.h>
27 #include <sys/types.h>
33 #include <sys/pfmod.h>
39 #include "isisd/dict.h"
40 #include "isisd/include-netbsd/iso.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
;
103 memset (&ctlbuf
, 0, sizeof (ctlbuf
));
104 ctlbuf
.len
= cbuflen
;
105 ctlbuf
.buf
= (void *)cbuf
;
111 memset (&databuf
, 0, sizeof (databuf
));
112 databuf
.len
= dbuflen
;
113 databuf
.buf
= (void *)dbuf
;
117 /* We assume this doesn't happen often and isn't operationally significant */
118 if (putmsg (fd
, ctlptr
, dataptr
, flags
) == -1)
119 zlog_debug ("%s: putmsg: %s", __func__
, safe_strerror (errno
));
125 struct pollfd fds
[1];
126 struct strbuf ctlbuf
, databuf
;
131 /* Poll is used here in case the device doesn't speak DLPI correctly */
132 memset (fds
, 0, sizeof (fds
));
134 fds
[0].events
= POLLIN
| POLLPRI
;
135 if (poll (fds
, 1, 1000) <= 0)
138 memset (&ctlbuf
, 0, sizeof (ctlbuf
));
139 memset (&databuf
, 0, sizeof (databuf
));
140 ctlbuf
.maxlen
= sizeof (dlpi_ctl
);
141 ctlbuf
.buf
= (void *)dlpi_ctl
;
142 databuf
.maxlen
= sizeof (sock_buff
);
143 databuf
.buf
= (void *)sock_buff
;
145 retv
= getmsg (fd
, &ctlbuf
, &databuf
, &flags
);
150 while (ctlbuf
.len
== 0);
152 if (!(retv
& MORECTL
))
154 while (retv
& MOREDATA
)
157 retv
= getmsg (fd
, NULL
, &databuf
, &flags
);
162 while (retv
& MORECTL
)
165 retv
= getmsg (fd
, &ctlbuf
, &databuf
, &flags
);
171 dlpiok (int fd
, t_uscalar_t oprim
)
174 dl_ok_ack_t
*doa
= (dl_ok_ack_t
*)dlpi_ctl
;
176 retv
= dlpirctl (fd
);
177 if (retv
< DL_OK_ACK_SIZE
|| doa
->dl_primitive
!= DL_OK_ACK
||
178 doa
->dl_correct_primitive
!= oprim
)
194 memset (&dir
, 0, sizeof (dir
));
195 dir
.dl_primitive
= DL_INFO_REQ
;
196 /* Info_req uses M_PCPROTO. */
197 dlpisend (fd
, &dir
, sizeof (dir
), NULL
, 0, RS_HIPRI
);
198 retv
= dlpirctl (fd
);
199 if (retv
< DL_INFO_ACK_SIZE
|| dlpi_ctl
[0] != DL_INFO_ACK
)
206 dlpiopen (const char *devpath
, ssize_t
*acklen
)
210 fd
= open (devpath
, O_RDWR
| O_NONBLOCK
| O_NOCTTY
);
214 /* All that we want is for the open itself to be non-blocking, not I/O. */
215 flags
= fcntl (fd
, F_GETFL
, 0);
217 fcntl (fd
, F_SETFL
, flags
& ~O_NONBLOCK
);
219 /* After opening, ask for information */
220 if ((*acklen
= dlpiinfo (fd
)) == -1)
230 dlpiattach (int fd
, int unit
)
234 memset (&dar
, 0, sizeof (dar
));
235 dar
.dl_primitive
= DL_ATTACH_REQ
;
237 dlpisend (fd
, &dar
, sizeof (dar
), NULL
, 0, 0);
238 return dlpiok (fd
, dar
.dl_primitive
);
246 dl_bind_ack_t
*dba
= (dl_bind_ack_t
*)dlpi_ctl
;
248 memset (&dbr
, 0, sizeof (dbr
));
249 dbr
.dl_primitive
= DL_BIND_REQ
;
250 dbr
.dl_service_mode
= DL_CLDLS
;
251 dlpisend (fd
, &dbr
, sizeof (dbr
), NULL
, 0, 0);
253 retv
= dlpirctl (fd
);
254 if (retv
< DL_BIND_ACK_SIZE
|| dba
->dl_primitive
!= DL_BIND_ACK
)
261 dlpimcast (int fd
, const u_char
*mcaddr
)
264 dl_enabmulti_req_t der
;
265 u_char addr
[ETHERADDRL
];
268 memset (&dler
, 0, sizeof (dler
));
269 dler
.der
.dl_primitive
= DL_ENABMULTI_REQ
;
270 dler
.der
.dl_addr_length
= sizeof (dler
.addr
);
271 dler
.der
.dl_addr_offset
= dler
.addr
- (u_char
*)&dler
;
272 memcpy (dler
.addr
, mcaddr
, sizeof (dler
.addr
));
273 dlpisend (fd
, &dler
, sizeof (dler
), NULL
, 0, 0);
274 return dlpiok (fd
, dler
.der
.dl_primitive
);
278 dlpiaddr (int fd
, u_char
*addr
)
280 dl_phys_addr_req_t dpar
;
281 dl_phys_addr_ack_t
*dpaa
= (dl_phys_addr_ack_t
*)dlpi_ctl
;
284 memset (&dpar
, 0, sizeof (dpar
));
285 dpar
.dl_primitive
= DL_PHYS_ADDR_REQ
;
286 dpar
.dl_addr_type
= DL_CURR_PHYS_ADDR
;
287 dlpisend (fd
, &dpar
, sizeof (dpar
), NULL
, 0, 0);
289 retv
= dlpirctl (fd
);
290 if (retv
< DL_PHYS_ADDR_ACK_SIZE
|| dpaa
->dl_primitive
!= DL_PHYS_ADDR_ACK
)
293 if (dpaa
->dl_addr_offset
< DL_PHYS_ADDR_ACK_SIZE
||
294 dpaa
->dl_addr_length
!= ETHERADDRL
||
295 dpaa
->dl_addr_offset
+ dpaa
->dl_addr_length
> retv
)
298 bcopy((char *)dpaa
+ dpaa
->dl_addr_offset
, addr
, ETHERADDRL
);
303 open_dlpi_dev (struct isis_circuit
*circuit
)
305 int fd
, unit
, retval
;
306 char devpath
[MAXPATHLEN
];
307 dl_info_ack_t
*dia
= (dl_info_ack_t
*)dlpi_ctl
;
310 /* Only broadcast-type are supported at the moment */
311 if (circuit
->circ_type
!= CIRCUIT_T_BROADCAST
)
313 zlog_warn ("%s: non-broadcast interface %s", __func__
,
314 circuit
->interface
->name
);
318 /* Try the vanity node first, if permitted */
319 if (getenv("DLPI_DEVONLY") == NULL
)
321 (void) snprintf (devpath
, sizeof(devpath
), "/dev/net/%s",
322 circuit
->interface
->name
);
323 fd
= dlpiopen (devpath
, &acklen
);
326 /* Now try as an ordinary Style 1 node */
329 (void) snprintf (devpath
, sizeof (devpath
), "/dev/%s",
330 circuit
->interface
->name
);
332 fd
= dlpiopen (devpath
, &acklen
);
335 /* If that fails, try again as Style 2 */
340 cp
= devpath
+ strlen (devpath
);
341 while (--cp
>= devpath
&& isdigit(*cp
))
343 unit
= strtol(cp
, NULL
, 0);
345 fd
= dlpiopen (devpath
, &acklen
);
347 /* If that too fails, then the device really doesn't exist */
350 zlog_warn ("%s: unknown interface %s", __func__
,
351 circuit
->interface
->name
);
355 /* Double check the DLPI style */
356 if (dia
->dl_provider_style
!= DL_STYLE2
)
358 zlog_warn ("open_dlpi_dev(): interface %s: %s is not style 2",
359 circuit
->interface
->name
, devpath
);
364 /* If it succeeds, then we need to attach to the unit specified */
365 dlpiattach (fd
, unit
);
367 /* Reget the information, as it may be different per node */
368 if ((acklen
= dlpiinfo (fd
)) == -1)
376 /* Double check the DLPI style */
377 if (dia
->dl_provider_style
!= DL_STYLE1
)
379 zlog_warn ("open_dlpi_dev(): interface %s: %s is not style 1",
380 circuit
->interface
->name
, devpath
);
386 /* Check that the interface we've got is the kind we expect */
387 if ((dia
->dl_sap_length
!= 2 && dia
->dl_sap_length
!= -2) ||
388 dia
->dl_service_mode
!= DL_CLDLS
|| dia
->dl_addr_length
!= ETHERADDRL
+ 2 ||
389 dia
->dl_brdcst_addr_length
!= ETHERADDRL
)
391 zlog_warn ("%s: unsupported interface type for %s", __func__
,
392 circuit
->interface
->name
);
396 switch (dia
->dl_mac_type
)
406 zlog_warn ("%s: unexpected mac type on %s: %d", __func__
,
407 circuit
->interface
->name
, dia
->dl_mac_type
);
412 circuit
->sap_length
= dia
->dl_sap_length
;
415 * The local hardware address is something that should be provided by way of
416 * sockaddr_dl for the interface, but isn't on Solaris. We set it here based
417 * on DLPI's reported address to avoid roto-tilling the world.
418 * (Note that isis_circuit_if_add on Solaris doesn't set the snpa.)
420 * Unfortunately, GLD is broken and doesn't provide the address after attach,
421 * so we need to be careful and use DL_PHYS_ADDR_REQ instead.
423 if (dlpiaddr (fd
, circuit
->u
.bc
.snpa
) == -1)
425 zlog_warn ("open_dlpi_dev(): interface %s: unable to get MAC address",
426 circuit
->interface
->name
);
431 /* Now bind to SAP 0. This gives us 802-type traffic. */
432 if (dlpibind (fd
) == -1)
434 zlog_warn ("%s: cannot bind SAP 0 on %s", __func__
,
435 circuit
->interface
->name
);
441 * Join to multicast groups according to
442 * 8.4.2 - Broadcast subnetwork IIH PDUs
445 retval
|= dlpimcast (fd
, ALL_L1_ISS
);
446 retval
|= dlpimcast (fd
, ALL_ISS
);
447 retval
|= dlpimcast (fd
, ALL_L2_ISS
);
451 zlog_warn ("%s: unable to join multicast on %s", __func__
,
452 circuit
->interface
->name
);
457 /* Push on the packet filter to avoid stray 802 packets */
458 if (ioctl (fd
, I_PUSH
, "pfmod") == 0)
460 struct packetfilt pfil
;
461 struct strioctl sioc
;
463 pfil
.Pf_Priority
= 0;
464 pfil
.Pf_FilterLen
= sizeof (pf_filter
) / sizeof (u_short
);
465 memcpy (pfil
.Pf_Filter
, pf_filter
, sizeof (pf_filter
));
466 /* pfmod does not support transparent ioctls */
467 sioc
.ic_cmd
= PFIOCSETF
;
469 sioc
.ic_len
= sizeof (struct packetfilt
);
470 sioc
.ic_dp
= (char *)&pfil
;
471 if (ioctl (fd
, I_STR
, &sioc
) == -1)
472 zlog_warn("%s: could not perform PF_IOCSETF on %s",
473 __func__
, circuit
->interface
->name
);
482 * Create the socket and set the tx/rx funcs
485 isis_sock_init (struct isis_circuit
*circuit
)
487 int retval
= ISIS_OK
;
489 if (isisd_privs
.change (ZPRIVS_RAISE
))
490 zlog_err ("%s: could not raise privs, %s", __func__
, safe_strerror (errno
));
492 retval
= open_dlpi_dev (circuit
);
494 if (retval
!= ISIS_OK
)
496 zlog_warn ("%s: could not initialize the socket", __func__
);
500 if (circuit
->circ_type
== CIRCUIT_T_BROADCAST
)
502 circuit
->tx
= isis_send_pdu_bcast
;
503 circuit
->rx
= isis_recv_pdu_bcast
;
507 zlog_warn ("isis_sock_init(): unknown circuit type");
508 retval
= ISIS_WARNING
;
513 if (isisd_privs
.change (ZPRIVS_LOWER
))
514 zlog_err ("%s: could not lower privs, %s", __func__
, safe_strerror (errno
));
520 isis_recv_pdu_bcast (struct isis_circuit
*circuit
, u_char
* ssnpa
)
522 struct pollfd fds
[1];
523 struct strbuf ctlbuf
, databuf
;
525 dl_unitdata_ind_t
*dui
= (dl_unitdata_ind_t
*)dlpi_ctl
;
527 memset (fds
, 0, sizeof (fds
));
528 fds
[0].fd
= circuit
->fd
;
529 fds
[0].events
= POLLIN
| POLLPRI
;
530 if (poll (fds
, 1, 0) <= 0)
533 memset (&ctlbuf
, 0, sizeof (ctlbuf
));
534 memset (&databuf
, 0, sizeof (databuf
));
535 ctlbuf
.maxlen
= sizeof (dlpi_ctl
);
536 ctlbuf
.buf
= (void *)dlpi_ctl
;
537 databuf
.maxlen
= sizeof (sock_buff
);
538 databuf
.buf
= (void *)sock_buff
;
540 retv
= getmsg (circuit
->fd
, &ctlbuf
, &databuf
, &flags
);
544 zlog_warn ("isis_recv_pdu_bcast: getmsg failed: %s",
545 safe_strerror (errno
));
549 if (retv
& (MORECTL
| MOREDATA
))
551 while (retv
& (MORECTL
| MOREDATA
))
554 retv
= getmsg (circuit
->fd
, &ctlbuf
, &databuf
, &flags
);
559 if (ctlbuf
.len
< DL_UNITDATA_IND_SIZE
||
560 dui
->dl_primitive
!= DL_UNITDATA_IND
)
563 if (dui
->dl_src_addr_length
!= ETHERADDRL
+ 2 ||
564 dui
->dl_src_addr_offset
< DL_UNITDATA_IND_SIZE
||
565 dui
->dl_src_addr_offset
+ dui
->dl_src_addr_length
> ctlbuf
.len
)
568 memcpy (ssnpa
, (char *)dui
+ dui
->dl_src_addr_offset
+
569 (circuit
->sap_length
> 0 ? circuit
->sap_length
: 0), ETHERADDRL
);
571 if (databuf
.len
< LLC_LEN
|| sock_buff
[0] != ISO_SAP
||
572 sock_buff
[1] != ISO_SAP
|| sock_buff
[2] != 3)
575 stream_write (circuit
->rcv_stream
, sock_buff
+ LLC_LEN
,
576 databuf
.len
- LLC_LEN
);
577 stream_set_getp (circuit
->rcv_stream
, 0);
583 isis_send_pdu_bcast (struct isis_circuit
*circuit
, int level
)
585 dl_unitdata_req_t
*dur
= (dl_unitdata_req_t
*)dlpi_ctl
;
590 buflen
= stream_get_endp (circuit
->snd_stream
) + LLC_LEN
;
591 if (buflen
> sizeof (sock_buff
))
593 zlog_warn ("isis_send_pdu_bcast: sock_buff size %lu is less than "
594 "output pdu size %d on circuit %s",
595 sizeof (sock_buff
), buflen
, circuit
->interface
->name
);
599 stream_set_getp (circuit
->snd_stream
, 0);
601 memset (dur
, 0, sizeof (*dur
));
602 dur
->dl_primitive
= DL_UNITDATA_REQ
;
603 dur
->dl_dest_addr_length
= ETHERADDRL
+ 2;
604 dur
->dl_dest_addr_offset
= sizeof (*dur
);
606 dstaddr
= (char *)(dur
+ 1);
607 if (circuit
->sap_length
< 0)
609 dstsap
= (u_short
*)(dstaddr
+ ETHERADDRL
);
613 dstsap
= (u_short
*)dstaddr
;
614 dstaddr
+= circuit
->sap_length
;
617 memcpy (dstaddr
, ALL_L1_ISS
, ETHERADDRL
);
619 memcpy (dstaddr
, ALL_L2_ISS
, ETHERADDRL
);
620 /* Note: DLPI SAP values are in host byte order */
623 sock_buff
[0] = ISO_SAP
;
624 sock_buff
[1] = ISO_SAP
;
626 memcpy (sock_buff
+ LLC_LEN
, circuit
->snd_stream
->data
,
627 stream_get_endp (circuit
->snd_stream
));
628 dlpisend (circuit
->fd
, dur
, sizeof (*dur
) + dur
->dl_dest_addr_length
,
629 sock_buff
, buflen
, 0);
633 #endif /* ISIS_METHOD == ISIS_METHOD_DLPI */