]>
git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_dlpi.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IS-IS Rout(e)ing protocol - isis_dlpi.c
5 * Copyright (C) 2001,2002 Sampo Saaristo
6 * Tampere University of Technology
7 * Institute of Communications Engineering
11 #if ISIS_METHOD == ISIS_METHOD_DLPI
13 #include <netinet/if_ether.h>
14 #include <sys/types.h>
20 #include <sys/pfmod.h>
26 #include "lib_errors.h"
28 #include "isisd/isis_constants.h"
29 #include "isisd/isis_common.h"
30 #include "isisd/isis_circuit.h"
31 #include "isisd/isis_flags.h"
32 #include "isisd/isisd.h"
33 #include "isisd/isis_network.h"
37 static t_uscalar_t dlpi_ctl
[1024]; /* DLPI control messages */
40 * Table 9 - Architectural constants for use with ISO 8802 subnetworks
44 static const uint8_t ALL_L1_ISS
[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x14};
45 static const uint8_t ALL_L2_ISS
[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x15};
46 static const uint8_t ALL_ISS
[6] = {0x09, 0x00, 0x2B, 0x00, 0x00, 0x05};
47 static uint8_t sock_buff
[16384];
49 static unsigned short pf_filter
[] = {
50 ENF_PUSHWORD
+ 0, /* Get the SSAP/DSAP values */
51 ENF_PUSHLIT
| ENF_CAND
, /* Check them */
52 ISO_SAP
| (ISO_SAP
<< 8),
53 ENF_PUSHWORD
+ 1, /* Get the control value */
54 ENF_PUSHLIT
| ENF_AND
, /* Isolate it */
60 ENF_PUSHLIT
| ENF_CAND
, /* Test for expected value */
69 * We would like to use something like libdlpi here, but that's not present on
70 * all versions of Solaris or on any non-Solaris system, so it's nowhere near
71 * as portable as we'd like. Thus, we use the standards-conformant DLPI
72 * interfaces plus the (optional; not needed) Solaris packet filter module.
75 static int dlpisend(int fd
, const void *cbuf
, size_t cbuflen
, const void *dbuf
,
76 size_t dbuflen
, int flags
)
78 const struct strbuf
*ctlptr
= NULL
;
79 const struct strbuf
*dataptr
= NULL
;
80 struct strbuf ctlbuf
, databuf
;
84 memset(&ctlbuf
, 0, sizeof(ctlbuf
));
86 ctlbuf
.buf
= (void *)cbuf
;
91 memset(&databuf
, 0, sizeof(databuf
));
92 databuf
.len
= dbuflen
;
93 databuf
.buf
= (void *)dbuf
;
97 /* We assume this doesn't happen often and isn't operationally
99 rv
= putmsg(fd
, ctlptr
, dataptr
, flags
);
100 if (rv
== -1 && dbuf
== NULL
) {
102 * For actual PDU transmission - recognizable buf dbuf != NULL,
103 * the error is passed upwards and should not be printed here.
105 zlog_debug("%s: putmsg: %s", __func__
, safe_strerror(errno
));
110 static ssize_t
dlpirctl(int fd
)
112 struct pollfd fds
[1];
113 struct strbuf ctlbuf
, databuf
;
117 /* Poll is used here in case the device doesn't speak DLPI
119 memset(fds
, 0, sizeof(fds
));
121 fds
[0].events
= POLLIN
| POLLPRI
;
122 if (poll(fds
, 1, 1000) <= 0)
125 memset(&ctlbuf
, 0, sizeof(ctlbuf
));
126 memset(&databuf
, 0, sizeof(databuf
));
127 ctlbuf
.maxlen
= sizeof(dlpi_ctl
);
128 ctlbuf
.buf
= (void *)dlpi_ctl
;
129 databuf
.maxlen
= sizeof(sock_buff
);
130 databuf
.buf
= (void *)sock_buff
;
132 retv
= getmsg(fd
, &ctlbuf
, &databuf
, &flags
);
136 } while (ctlbuf
.len
== 0);
138 if (!(retv
& MORECTL
)) {
139 while (retv
& MOREDATA
) {
141 retv
= getmsg(fd
, NULL
, &databuf
, &flags
);
146 while (retv
& MORECTL
) {
148 retv
= getmsg(fd
, &ctlbuf
, &databuf
, &flags
);
153 static int dlpiok(int fd
, t_uscalar_t oprim
)
156 dl_ok_ack_t
*doa
= (dl_ok_ack_t
*)dlpi_ctl
;
159 if (retv
< (ssize_t
)DL_OK_ACK_SIZE
|| doa
->dl_primitive
!= DL_OK_ACK
160 || doa
->dl_correct_primitive
!= oprim
) {
167 static int dlpiinfo(int fd
)
172 memset(&dir
, 0, sizeof(dir
));
173 dir
.dl_primitive
= DL_INFO_REQ
;
174 /* Info_req uses M_PCPROTO. */
175 dlpisend(fd
, &dir
, sizeof(dir
), NULL
, 0, RS_HIPRI
);
177 if (retv
< (ssize_t
)DL_INFO_ACK_SIZE
|| dlpi_ctl
[0] != DL_INFO_ACK
)
183 static int dlpiopen(const char *devpath
, ssize_t
*acklen
)
187 fd
= open(devpath
, O_RDWR
| O_NONBLOCK
| O_NOCTTY
);
191 /* All that we want is for the open itself to be non-blocking, not I/O.
193 flags
= fcntl(fd
, F_GETFL
, 0);
195 fcntl(fd
, F_SETFL
, flags
& ~O_NONBLOCK
);
197 /* After opening, ask for information */
198 if ((*acklen
= dlpiinfo(fd
)) == -1) {
206 static int dlpiattach(int fd
, int unit
)
210 memset(&dar
, 0, sizeof(dar
));
211 dar
.dl_primitive
= DL_ATTACH_REQ
;
213 dlpisend(fd
, &dar
, sizeof(dar
), NULL
, 0, 0);
214 return dlpiok(fd
, dar
.dl_primitive
);
217 static int dlpibind(int fd
)
221 dl_bind_ack_t
*dba
= (dl_bind_ack_t
*)dlpi_ctl
;
223 memset(&dbr
, 0, sizeof(dbr
));
224 dbr
.dl_primitive
= DL_BIND_REQ
;
225 dbr
.dl_service_mode
= DL_CLDLS
;
226 dlpisend(fd
, &dbr
, sizeof(dbr
), NULL
, 0, 0);
229 if (retv
< (ssize_t
)DL_BIND_ACK_SIZE
230 || dba
->dl_primitive
!= DL_BIND_ACK
)
236 static int dlpimcast(int fd
, const uint8_t *mcaddr
)
239 dl_enabmulti_req_t der
;
240 uint8_t addr
[ETHERADDRL
];
243 memset(&dler
, 0, sizeof(dler
));
244 dler
.der
.dl_primitive
= DL_ENABMULTI_REQ
;
245 dler
.der
.dl_addr_length
= sizeof(dler
.addr
);
246 dler
.der
.dl_addr_offset
= dler
.addr
- (uint8_t *)&dler
;
247 memcpy(dler
.addr
, mcaddr
, sizeof(dler
.addr
));
248 dlpisend(fd
, &dler
, sizeof(dler
), NULL
, 0, 0);
249 return dlpiok(fd
, dler
.der
.dl_primitive
);
252 static int dlpiaddr(int fd
, uint8_t *addr
)
254 dl_phys_addr_req_t dpar
;
255 dl_phys_addr_ack_t
*dpaa
= (dl_phys_addr_ack_t
*)dlpi_ctl
;
258 memset(&dpar
, 0, sizeof(dpar
));
259 dpar
.dl_primitive
= DL_PHYS_ADDR_REQ
;
260 dpar
.dl_addr_type
= DL_CURR_PHYS_ADDR
;
261 dlpisend(fd
, &dpar
, sizeof(dpar
), NULL
, 0, 0);
264 if (retv
< (ssize_t
)DL_PHYS_ADDR_ACK_SIZE
265 || dpaa
->dl_primitive
!= DL_PHYS_ADDR_ACK
)
268 if (dpaa
->dl_addr_offset
< DL_PHYS_ADDR_ACK_SIZE
269 || dpaa
->dl_addr_length
!= ETHERADDRL
270 || dpaa
->dl_addr_offset
+ dpaa
->dl_addr_length
> (size_t)retv
)
273 bcopy((char *)dpaa
+ dpaa
->dl_addr_offset
, addr
, ETHERADDRL
);
277 static int open_dlpi_dev(struct isis_circuit
*circuit
)
279 int fd
= -1, unit
, retval
;
280 char devpath
[MAXPATHLEN
];
281 dl_info_ack_t
*dia
= (dl_info_ack_t
*)dlpi_ctl
;
284 /* Only broadcast-type are supported at the moment */
285 if (circuit
->circ_type
!= CIRCUIT_T_BROADCAST
) {
286 zlog_warn("%s: non-broadcast interface %s", __func__
,
287 circuit
->interface
->name
);
291 /* Try the vanity node first, if permitted */
292 if (getenv("DLPI_DEVONLY") == NULL
) {
293 (void)snprintf(devpath
, sizeof(devpath
), "/dev/net/%s",
294 circuit
->interface
->name
);
295 fd
= dlpiopen(devpath
, &acklen
);
298 /* Now try as an ordinary Style 1 node */
300 (void)snprintf(devpath
, sizeof(devpath
), "/dev/%s",
301 circuit
->interface
->name
);
303 fd
= dlpiopen(devpath
, &acklen
);
306 /* If that fails, try again as Style 2 */
310 cp
= devpath
+ strlen(devpath
);
311 while (--cp
>= devpath
&& isdigit(*cp
))
313 unit
= strtol(cp
, NULL
, 0);
315 fd
= dlpiopen(devpath
, &acklen
);
317 /* If that too fails, then the device really doesn't exist */
319 zlog_warn("%s: unknown interface %s", __func__
,
320 circuit
->interface
->name
);
324 /* Double check the DLPI style */
325 if (dia
->dl_provider_style
!= DL_STYLE2
) {
326 zlog_warn("%s: interface %s: %s is not style 2",
327 __func__
, circuit
->interface
->name
, devpath
);
332 /* If it succeeds, then we need to attach to the unit specified
334 dlpiattach(fd
, unit
);
336 /* Reget the information, as it may be different per node */
337 if ((acklen
= dlpiinfo(fd
)) == -1) {
342 /* Double check the DLPI style */
343 if (dia
->dl_provider_style
!= DL_STYLE1
) {
344 zlog_warn("%s: interface %s: %s is not style 1",
345 __func__
, circuit
->interface
->name
, devpath
);
351 /* Check that the interface we've got is the kind we expect */
352 if ((dia
->dl_sap_length
!= 2 && dia
->dl_sap_length
!= -2)
353 || dia
->dl_service_mode
!= DL_CLDLS
354 || dia
->dl_addr_length
!= ETHERADDRL
+ 2
355 || dia
->dl_brdcst_addr_length
!= ETHERADDRL
) {
356 zlog_warn("%s: unsupported interface type for %s", __func__
,
357 circuit
->interface
->name
);
361 switch (dia
->dl_mac_type
) {
370 zlog_warn("%s: unexpected mac type on %s: %lld", __func__
,
371 circuit
->interface
->name
,
372 (long long)dia
->dl_mac_type
);
377 circuit
->sap_length
= dia
->dl_sap_length
;
380 * The local hardware address is something that should be provided by
382 * sockaddr_dl for the interface, but isn't on Solaris. We set it here
384 * on DLPI's reported address to avoid roto-tilling the world.
385 * (Note that isis_circuit_if_add on Solaris doesn't set the snpa.)
387 * Unfortunately, GLD is broken and doesn't provide the address after
389 * so we need to be careful and use DL_PHYS_ADDR_REQ instead.
391 if (dlpiaddr(fd
, circuit
->u
.bc
.snpa
) == -1) {
392 zlog_warn("%s: interface %s: unable to get MAC address",
393 __func__
, circuit
->interface
->name
);
398 /* Now bind to SAP 0. This gives us 802-type traffic. */
399 if (dlpibind(fd
) == -1) {
400 zlog_warn("%s: cannot bind SAP 0 on %s", __func__
,
401 circuit
->interface
->name
);
407 * Join to multicast groups according to
408 * 8.4.2 - Broadcast subnetwork IIH PDUs
411 retval
|= dlpimcast(fd
, ALL_L1_ISS
);
412 retval
|= dlpimcast(fd
, ALL_ISS
);
413 retval
|= dlpimcast(fd
, ALL_L2_ISS
);
416 zlog_warn("%s: unable to join multicast on %s", __func__
,
417 circuit
->interface
->name
);
422 /* Push on the packet filter to avoid stray 802 packets */
423 if (ioctl(fd
, I_PUSH
, "pfmod") == 0) {
424 struct packetfilt pfil
;
425 struct strioctl sioc
;
427 pfil
.Pf_Priority
= 0;
428 pfil
.Pf_FilterLen
= array_size(pf_filter
);
429 memcpy(pfil
.Pf_Filter
, pf_filter
, sizeof(pf_filter
));
430 /* pfmod does not support transparent ioctls */
431 sioc
.ic_cmd
= PFIOCSETF
;
433 sioc
.ic_len
= sizeof(struct packetfilt
);
434 sioc
.ic_dp
= (char *)&pfil
;
435 if (ioctl(fd
, I_STR
, &sioc
) == -1)
436 zlog_warn("%s: could not perform PF_IOCSETF on %s",
437 __func__
, circuit
->interface
->name
);
446 * Create the socket and set the tx/rx funcs
448 int isis_sock_init(struct isis_circuit
*circuit
)
450 int retval
= ISIS_OK
;
452 frr_with_privs(&isisd_privs
) {
454 retval
= open_dlpi_dev(circuit
);
456 if (retval
!= ISIS_OK
) {
457 zlog_warn("%s: could not initialize the socket",
462 if (circuit
->circ_type
== CIRCUIT_T_BROADCAST
) {
463 circuit
->tx
= isis_send_pdu_bcast
;
464 circuit
->rx
= isis_recv_pdu_bcast
;
466 zlog_warn("%s: unknown circuit type", __func__
);
467 retval
= ISIS_WARNING
;
475 int isis_recv_pdu_bcast(struct isis_circuit
*circuit
, uint8_t *ssnpa
)
477 struct pollfd fds
[1];
478 struct strbuf ctlbuf
, databuf
;
480 dl_unitdata_ind_t
*dui
= (dl_unitdata_ind_t
*)dlpi_ctl
;
482 memset(fds
, 0, sizeof(fds
));
483 fds
[0].fd
= circuit
->fd
;
484 fds
[0].events
= POLLIN
| POLLPRI
;
485 if (poll(fds
, 1, 0) <= 0)
488 memset(&ctlbuf
, 0, sizeof(ctlbuf
));
489 memset(&databuf
, 0, sizeof(databuf
));
490 ctlbuf
.maxlen
= sizeof(dlpi_ctl
);
491 ctlbuf
.buf
= (void *)dlpi_ctl
;
492 databuf
.maxlen
= sizeof(sock_buff
);
493 databuf
.buf
= (void *)sock_buff
;
495 retv
= getmsg(circuit
->fd
, &ctlbuf
, &databuf
, &flags
);
498 zlog_warn("%s: getmsg failed: %s", __func__
,
499 safe_strerror(errno
));
503 if (retv
& (MORECTL
| MOREDATA
)) {
504 while (retv
& (MORECTL
| MOREDATA
)) {
506 retv
= getmsg(circuit
->fd
, &ctlbuf
, &databuf
, &flags
);
511 if (ctlbuf
.len
< (ssize_t
)DL_UNITDATA_IND_SIZE
512 || dui
->dl_primitive
!= DL_UNITDATA_IND
)
515 if (dui
->dl_src_addr_length
!= ETHERADDRL
+ 2
516 || dui
->dl_src_addr_offset
< DL_UNITDATA_IND_SIZE
517 || dui
->dl_src_addr_offset
+ dui
->dl_src_addr_length
518 > (size_t)ctlbuf
.len
)
522 (char *)dui
+ dui
->dl_src_addr_offset
523 + (circuit
->sap_length
> 0 ? circuit
->sap_length
: 0),
526 if (databuf
.len
< LLC_LEN
|| sock_buff
[0] != ISO_SAP
527 || sock_buff
[1] != ISO_SAP
|| sock_buff
[2] != 3)
530 stream_write(circuit
->rcv_stream
, sock_buff
+ LLC_LEN
,
531 databuf
.len
- LLC_LEN
);
532 stream_set_getp(circuit
->rcv_stream
, 0);
537 int isis_send_pdu_bcast(struct isis_circuit
*circuit
, int level
)
539 dl_unitdata_req_t
*dur
= (dl_unitdata_req_t
*)dlpi_ctl
;
541 unsigned short *dstsap
;
545 buflen
= stream_get_endp(circuit
->snd_stream
) + LLC_LEN
;
546 if ((size_t)buflen
> sizeof(sock_buff
)) {
548 "%s: sock_buff size %zu is less than output pdu size %d on circuit %s",
549 __func__
, sizeof(sock_buff
), buflen
,
550 circuit
->interface
->name
);
554 stream_set_getp(circuit
->snd_stream
, 0);
556 memset(dur
, 0, sizeof(*dur
));
557 dur
->dl_primitive
= DL_UNITDATA_REQ
;
558 dur
->dl_dest_addr_length
= ETHERADDRL
+ 2;
559 dur
->dl_dest_addr_offset
= sizeof(*dur
);
561 dstaddr
= (char *)(dur
+ 1);
562 if (circuit
->sap_length
< 0) {
563 dstsap
= (unsigned short *)(dstaddr
+ ETHERADDRL
);
565 dstsap
= (unsigned short *)dstaddr
;
566 dstaddr
+= circuit
->sap_length
;
569 memcpy(dstaddr
, ALL_L1_ISS
, ETHERADDRL
);
571 memcpy(dstaddr
, ALL_L2_ISS
, ETHERADDRL
);
572 /* Note: DLPI SAP values are in host byte order */
575 sock_buff
[0] = ISO_SAP
;
576 sock_buff
[1] = ISO_SAP
;
578 memcpy(sock_buff
+ LLC_LEN
, circuit
->snd_stream
->data
,
579 stream_get_endp(circuit
->snd_stream
));
580 rv
= dlpisend(circuit
->fd
, dur
, sizeof(*dur
) + dur
->dl_dest_addr_length
,
581 sock_buff
, buflen
, 0);
583 zlog_warn("IS-IS dlpi: could not transmit packet on %s: %s",
584 circuit
->interface
->name
, safe_strerror(errno
));
585 if (ERRNO_IO_RETRY(errno
))
593 #endif /* ISIS_METHOD == ISIS_METHOD_DLPI */