]> git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_dlpi.c
Merge pull request #12791 from taspelund/loc_rib_json_fix
[mirror_frr.git] / isisd / isis_dlpi.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * IS-IS Rout(e)ing protocol - isis_dlpi.c
4 *
5 * Copyright (C) 2001,2002 Sampo Saaristo
6 * Tampere University of Technology
7 * Institute of Communications Engineering
8 */
9
10 #include <zebra.h>
11 #if ISIS_METHOD == ISIS_METHOD_DLPI
12 #include <net/if.h>
13 #include <netinet/if_ether.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <stropts.h>
18 #include <poll.h>
19 #include <sys/dlpi.h>
20 #include <sys/pfmod.h>
21
22 #include "log.h"
23 #include "network.h"
24 #include "stream.h"
25 #include "if.h"
26 #include "lib_errors.h"
27
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"
34
35 #include "privs.h"
36
37 static t_uscalar_t dlpi_ctl[1024]; /* DLPI control messages */
38
39 /*
40 * Table 9 - Architectural constants for use with ISO 8802 subnetworks
41 * ISO 10589 - 8.4.8
42 */
43
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];
48
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 */
55 #ifdef _BIG_ENDIAN
56 0xFF00,
57 #else
58 0x00FF,
59 #endif
60 ENF_PUSHLIT | ENF_CAND, /* Test for expected value */
61 #ifdef _BIG_ENDIAN
62 0x0300
63 #else
64 0x0003
65 #endif
66 };
67
68 /*
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.
73 */
74
75 static int dlpisend(int fd, const void *cbuf, size_t cbuflen, const void *dbuf,
76 size_t dbuflen, int flags)
77 {
78 const struct strbuf *ctlptr = NULL;
79 const struct strbuf *dataptr = NULL;
80 struct strbuf ctlbuf, databuf;
81 int rv;
82
83 if (cbuf != NULL) {
84 memset(&ctlbuf, 0, sizeof(ctlbuf));
85 ctlbuf.len = cbuflen;
86 ctlbuf.buf = (void *)cbuf;
87 ctlptr = &ctlbuf;
88 }
89
90 if (dbuf != NULL) {
91 memset(&databuf, 0, sizeof(databuf));
92 databuf.len = dbuflen;
93 databuf.buf = (void *)dbuf;
94 dataptr = &databuf;
95 }
96
97 /* We assume this doesn't happen often and isn't operationally
98 * significant */
99 rv = putmsg(fd, ctlptr, dataptr, flags);
100 if (rv == -1 && dbuf == NULL) {
101 /*
102 * For actual PDU transmission - recognizable buf dbuf != NULL,
103 * the error is passed upwards and should not be printed here.
104 */
105 zlog_debug("%s: putmsg: %s", __func__, safe_strerror(errno));
106 }
107 return rv;
108 }
109
110 static ssize_t dlpirctl(int fd)
111 {
112 struct pollfd fds[1];
113 struct strbuf ctlbuf, databuf;
114 int flags, retv;
115
116 do {
117 /* Poll is used here in case the device doesn't speak DLPI
118 * correctly */
119 memset(fds, 0, sizeof(fds));
120 fds[0].fd = fd;
121 fds[0].events = POLLIN | POLLPRI;
122 if (poll(fds, 1, 1000) <= 0)
123 return -1;
124
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;
131 flags = 0;
132 retv = getmsg(fd, &ctlbuf, &databuf, &flags);
133
134 if (retv < 0)
135 return -1;
136 } while (ctlbuf.len == 0);
137
138 if (!(retv & MORECTL)) {
139 while (retv & MOREDATA) {
140 flags = 0;
141 retv = getmsg(fd, NULL, &databuf, &flags);
142 }
143 return ctlbuf.len;
144 }
145
146 while (retv & MORECTL) {
147 flags = 0;
148 retv = getmsg(fd, &ctlbuf, &databuf, &flags);
149 }
150 return -1;
151 }
152
153 static int dlpiok(int fd, t_uscalar_t oprim)
154 {
155 int retv;
156 dl_ok_ack_t *doa = (dl_ok_ack_t *)dlpi_ctl;
157
158 retv = dlpirctl(fd);
159 if (retv < (ssize_t)DL_OK_ACK_SIZE || doa->dl_primitive != DL_OK_ACK
160 || doa->dl_correct_primitive != oprim) {
161 return -1;
162 } else {
163 return 0;
164 }
165 }
166
167 static int dlpiinfo(int fd)
168 {
169 dl_info_req_t dir;
170 ssize_t retv;
171
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);
176 retv = dlpirctl(fd);
177 if (retv < (ssize_t)DL_INFO_ACK_SIZE || dlpi_ctl[0] != DL_INFO_ACK)
178 return -1;
179 else
180 return retv;
181 }
182
183 static int dlpiopen(const char *devpath, ssize_t *acklen)
184 {
185 int fd, flags;
186
187 fd = open(devpath, O_RDWR | O_NONBLOCK | O_NOCTTY);
188 if (fd == -1)
189 return -1;
190
191 /* All that we want is for the open itself to be non-blocking, not I/O.
192 */
193 flags = fcntl(fd, F_GETFL, 0);
194 if (flags != -1)
195 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
196
197 /* After opening, ask for information */
198 if ((*acklen = dlpiinfo(fd)) == -1) {
199 close(fd);
200 return -1;
201 }
202
203 return fd;
204 }
205
206 static int dlpiattach(int fd, int unit)
207 {
208 dl_attach_req_t dar;
209
210 memset(&dar, 0, sizeof(dar));
211 dar.dl_primitive = DL_ATTACH_REQ;
212 dar.dl_ppa = unit;
213 dlpisend(fd, &dar, sizeof(dar), NULL, 0, 0);
214 return dlpiok(fd, dar.dl_primitive);
215 }
216
217 static int dlpibind(int fd)
218 {
219 dl_bind_req_t dbr;
220 int retv;
221 dl_bind_ack_t *dba = (dl_bind_ack_t *)dlpi_ctl;
222
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);
227
228 retv = dlpirctl(fd);
229 if (retv < (ssize_t)DL_BIND_ACK_SIZE
230 || dba->dl_primitive != DL_BIND_ACK)
231 return -1;
232 else
233 return 0;
234 }
235
236 static int dlpimcast(int fd, const uint8_t *mcaddr)
237 {
238 struct {
239 dl_enabmulti_req_t der;
240 uint8_t addr[ETHERADDRL];
241 } dler;
242
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);
250 }
251
252 static int dlpiaddr(int fd, uint8_t *addr)
253 {
254 dl_phys_addr_req_t dpar;
255 dl_phys_addr_ack_t *dpaa = (dl_phys_addr_ack_t *)dlpi_ctl;
256 int retv;
257
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);
262
263 retv = dlpirctl(fd);
264 if (retv < (ssize_t)DL_PHYS_ADDR_ACK_SIZE
265 || dpaa->dl_primitive != DL_PHYS_ADDR_ACK)
266 return -1;
267
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)
271 return -1;
272
273 bcopy((char *)dpaa + dpaa->dl_addr_offset, addr, ETHERADDRL);
274 return 0;
275 }
276
277 static int open_dlpi_dev(struct isis_circuit *circuit)
278 {
279 int fd = -1, unit, retval;
280 char devpath[MAXPATHLEN];
281 dl_info_ack_t *dia = (dl_info_ack_t *)dlpi_ctl;
282 ssize_t acklen;
283
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);
288 return ISIS_WARNING;
289 }
290
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);
296 }
297
298 /* Now try as an ordinary Style 1 node */
299 if (fd == -1) {
300 (void)snprintf(devpath, sizeof(devpath), "/dev/%s",
301 circuit->interface->name);
302 unit = -1;
303 fd = dlpiopen(devpath, &acklen);
304 }
305
306 /* If that fails, try again as Style 2 */
307 if (fd == -1) {
308 char *cp;
309
310 cp = devpath + strlen(devpath);
311 while (--cp >= devpath && isdigit(*cp))
312 ;
313 unit = strtol(cp, NULL, 0);
314 *cp = '\0';
315 fd = dlpiopen(devpath, &acklen);
316
317 /* If that too fails, then the device really doesn't exist */
318 if (fd == -1) {
319 zlog_warn("%s: unknown interface %s", __func__,
320 circuit->interface->name);
321 return ISIS_WARNING;
322 }
323
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);
328 close(fd);
329 return ISIS_WARNING;
330 }
331
332 /* If it succeeds, then we need to attach to the unit specified
333 */
334 dlpiattach(fd, unit);
335
336 /* Reget the information, as it may be different per node */
337 if ((acklen = dlpiinfo(fd)) == -1) {
338 close(fd);
339 return ISIS_WARNING;
340 }
341 } else {
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);
346 close(fd);
347 return ISIS_WARNING;
348 }
349 }
350
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);
358 close(fd);
359 return ISIS_WARNING;
360 }
361 switch (dia->dl_mac_type) {
362 case DL_CSMACD:
363 case DL_ETHER:
364 case DL_100VG:
365 case DL_100VGTPR:
366 case DL_ETH_CSMA:
367 case DL_100BT:
368 break;
369 default:
370 zlog_warn("%s: unexpected mac type on %s: %lld", __func__,
371 circuit->interface->name,
372 (long long)dia->dl_mac_type);
373 close(fd);
374 return ISIS_WARNING;
375 }
376
377 circuit->sap_length = dia->dl_sap_length;
378
379 /*
380 * The local hardware address is something that should be provided by
381 * way of
382 * sockaddr_dl for the interface, but isn't on Solaris. We set it here
383 * based
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.)
386 *
387 * Unfortunately, GLD is broken and doesn't provide the address after
388 * attach,
389 * so we need to be careful and use DL_PHYS_ADDR_REQ instead.
390 */
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);
394 close(fd);
395 return ISIS_WARNING;
396 }
397
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);
402 close(fd);
403 return ISIS_WARNING;
404 }
405
406 /*
407 * Join to multicast groups according to
408 * 8.4.2 - Broadcast subnetwork IIH PDUs
409 */
410 retval = 0;
411 retval |= dlpimcast(fd, ALL_L1_ISS);
412 retval |= dlpimcast(fd, ALL_ISS);
413 retval |= dlpimcast(fd, ALL_L2_ISS);
414
415 if (retval != 0) {
416 zlog_warn("%s: unable to join multicast on %s", __func__,
417 circuit->interface->name);
418 close(fd);
419 return ISIS_WARNING;
420 }
421
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;
426
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;
432 sioc.ic_timout = 5;
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);
438 }
439
440 circuit->fd = fd;
441
442 return ISIS_OK;
443 }
444
445 /*
446 * Create the socket and set the tx/rx funcs
447 */
448 int isis_sock_init(struct isis_circuit *circuit)
449 {
450 int retval = ISIS_OK;
451
452 frr_with_privs(&isisd_privs) {
453
454 retval = open_dlpi_dev(circuit);
455
456 if (retval != ISIS_OK) {
457 zlog_warn("%s: could not initialize the socket",
458 __func__);
459 break;
460 }
461
462 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
463 circuit->tx = isis_send_pdu_bcast;
464 circuit->rx = isis_recv_pdu_bcast;
465 } else {
466 zlog_warn("%s: unknown circuit type", __func__);
467 retval = ISIS_WARNING;
468 break;
469 }
470 }
471
472 return retval;
473 }
474
475 int isis_recv_pdu_bcast(struct isis_circuit *circuit, uint8_t *ssnpa)
476 {
477 struct pollfd fds[1];
478 struct strbuf ctlbuf, databuf;
479 int flags, retv;
480 dl_unitdata_ind_t *dui = (dl_unitdata_ind_t *)dlpi_ctl;
481
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)
486 return ISIS_WARNING;
487
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;
494 flags = 0;
495 retv = getmsg(circuit->fd, &ctlbuf, &databuf, &flags);
496
497 if (retv < 0) {
498 zlog_warn("%s: getmsg failed: %s", __func__,
499 safe_strerror(errno));
500 return ISIS_WARNING;
501 }
502
503 if (retv & (MORECTL | MOREDATA)) {
504 while (retv & (MORECTL | MOREDATA)) {
505 flags = 0;
506 retv = getmsg(circuit->fd, &ctlbuf, &databuf, &flags);
507 }
508 return ISIS_WARNING;
509 }
510
511 if (ctlbuf.len < (ssize_t)DL_UNITDATA_IND_SIZE
512 || dui->dl_primitive != DL_UNITDATA_IND)
513 return ISIS_WARNING;
514
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)
519 return ISIS_WARNING;
520
521 memcpy(ssnpa,
522 (char *)dui + dui->dl_src_addr_offset
523 + (circuit->sap_length > 0 ? circuit->sap_length : 0),
524 ETHERADDRL);
525
526 if (databuf.len < LLC_LEN || sock_buff[0] != ISO_SAP
527 || sock_buff[1] != ISO_SAP || sock_buff[2] != 3)
528 return ISIS_WARNING;
529
530 stream_write(circuit->rcv_stream, sock_buff + LLC_LEN,
531 databuf.len - LLC_LEN);
532 stream_set_getp(circuit->rcv_stream, 0);
533
534 return ISIS_OK;
535 }
536
537 int isis_send_pdu_bcast(struct isis_circuit *circuit, int level)
538 {
539 dl_unitdata_req_t *dur = (dl_unitdata_req_t *)dlpi_ctl;
540 char *dstaddr;
541 unsigned short *dstsap;
542 int buflen;
543 int rv;
544
545 buflen = stream_get_endp(circuit->snd_stream) + LLC_LEN;
546 if ((size_t)buflen > sizeof(sock_buff)) {
547 zlog_warn(
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);
551 return ISIS_WARNING;
552 }
553
554 stream_set_getp(circuit->snd_stream, 0);
555
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);
560
561 dstaddr = (char *)(dur + 1);
562 if (circuit->sap_length < 0) {
563 dstsap = (unsigned short *)(dstaddr + ETHERADDRL);
564 } else {
565 dstsap = (unsigned short *)dstaddr;
566 dstaddr += circuit->sap_length;
567 }
568 if (level == 1)
569 memcpy(dstaddr, ALL_L1_ISS, ETHERADDRL);
570 else
571 memcpy(dstaddr, ALL_L2_ISS, ETHERADDRL);
572 /* Note: DLPI SAP values are in host byte order */
573 *dstsap = buflen;
574
575 sock_buff[0] = ISO_SAP;
576 sock_buff[1] = ISO_SAP;
577 sock_buff[2] = 0x03;
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);
582 if (rv < 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))
586 return ISIS_WARNING;
587 return ISIS_ERROR;
588 }
589
590 return ISIS_OK;
591 }
592
593 #endif /* ISIS_METHOD == ISIS_METHOD_DLPI */