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