]> git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_dlpi.c
Merge branch 'stable/3.0'
[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
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"
47
48 #include "privs.h"
49
50 extern struct zebra_privs_t isisd_privs;
51
52 static t_uscalar_t dlpi_ctl[1024]; /* DLPI control messages */
53
54 /*
55 * Table 9 - Architectural constants for use with ISO 8802 subnetworks
56 * ISO 10589 - 8.4.8
57 */
58
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 };
63
64 static u_char sock_buff[8192];
65
66 static u_short pf_filter[] =
67 {
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 */
73 #ifdef _BIG_ENDIAN
74 0xFF00,
75 #else
76 0x00FF,
77 #endif
78 ENF_PUSHLIT | ENF_CAND, /* Test for expected value */
79 #ifdef _BIG_ENDIAN
80 0x0300
81 #else
82 0x0003
83 #endif
84 };
85
86 /*
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.
91 */
92
93 static int
94 dlpisend (int fd, const void *cbuf, size_t cbuflen,
95 const void *dbuf, size_t dbuflen, int flags)
96 {
97 const struct strbuf *ctlptr = NULL;
98 const struct strbuf *dataptr = NULL;
99 struct strbuf ctlbuf, databuf;
100 int rv;
101
102 if (cbuf != NULL)
103 {
104 memset (&ctlbuf, 0, sizeof (ctlbuf));
105 ctlbuf.len = cbuflen;
106 ctlbuf.buf = (void *)cbuf;
107 ctlptr = &ctlbuf;
108 }
109
110 if (dbuf != NULL)
111 {
112 memset (&databuf, 0, sizeof (databuf));
113 databuf.len = dbuflen;
114 databuf.buf = (void *)dbuf;
115 dataptr = &databuf;
116 }
117
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)
121 {
122 /*
123 * For actual PDU transmission - recognizable buf dbuf != NULL,
124 * the error is passed upwards and should not be printed here.
125 */
126 zlog_debug ("%s: putmsg: %s", __func__, safe_strerror (errno));
127 }
128 return rv;
129 }
130
131 static ssize_t
132 dlpirctl (int fd)
133 {
134 struct pollfd fds[1];
135 struct strbuf ctlbuf, databuf;
136 int flags, retv;
137
138 do
139 {
140 /* Poll is used here in case the device doesn't speak DLPI correctly */
141 memset (fds, 0, sizeof (fds));
142 fds[0].fd = fd;
143 fds[0].events = POLLIN | POLLPRI;
144 if (poll (fds, 1, 1000) <= 0)
145 return -1;
146
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;
153 flags = 0;
154 retv = getmsg (fd, &ctlbuf, &databuf, &flags);
155
156 if (retv < 0)
157 return -1;
158 }
159 while (ctlbuf.len == 0);
160
161 if (!(retv & MORECTL))
162 {
163 while (retv & MOREDATA)
164 {
165 flags = 0;
166 retv = getmsg (fd, NULL, &databuf, &flags);
167 }
168 return ctlbuf.len;
169 }
170
171 while (retv & MORECTL)
172 {
173 flags = 0;
174 retv = getmsg (fd, &ctlbuf, &databuf, &flags);
175 }
176 return -1;
177 }
178
179 static int
180 dlpiok (int fd, t_uscalar_t oprim)
181 {
182 int retv;
183 dl_ok_ack_t *doa = (dl_ok_ack_t *)dlpi_ctl;
184
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)
188 {
189 return -1;
190 }
191 else
192 {
193 return 0;
194 }
195 }
196
197 static int
198 dlpiinfo (int fd)
199 {
200 dl_info_req_t dir;
201 ssize_t retv;
202
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)
209 return -1;
210 else
211 return retv;
212 }
213
214 static int
215 dlpiopen (const char *devpath, ssize_t *acklen)
216 {
217 int fd, flags;
218
219 fd = open (devpath, O_RDWR | O_NONBLOCK | O_NOCTTY);
220 if (fd == -1)
221 return -1;
222
223 /* All that we want is for the open itself to be non-blocking, not I/O. */
224 flags = fcntl (fd, F_GETFL, 0);
225 if (flags != -1)
226 fcntl (fd, F_SETFL, flags & ~O_NONBLOCK);
227
228 /* After opening, ask for information */
229 if ((*acklen = dlpiinfo (fd)) == -1)
230 {
231 close (fd);
232 return -1;
233 }
234
235 return fd;
236 }
237
238 static int
239 dlpiattach (int fd, int unit)
240 {
241 dl_attach_req_t dar;
242
243 memset (&dar, 0, sizeof (dar));
244 dar.dl_primitive = DL_ATTACH_REQ;
245 dar.dl_ppa = unit;
246 dlpisend (fd, &dar, sizeof (dar), NULL, 0, 0);
247 return dlpiok (fd, dar.dl_primitive);
248 }
249
250 static int
251 dlpibind (int fd)
252 {
253 dl_bind_req_t dbr;
254 int retv;
255 dl_bind_ack_t *dba = (dl_bind_ack_t *)dlpi_ctl;
256
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);
261
262 retv = dlpirctl (fd);
263 if (retv < (ssize_t)DL_BIND_ACK_SIZE || dba->dl_primitive != DL_BIND_ACK)
264 return -1;
265 else
266 return 0;
267 }
268
269 static int
270 dlpimcast (int fd, const u_char *mcaddr)
271 {
272 struct {
273 dl_enabmulti_req_t der;
274 u_char addr[ETHERADDRL];
275 } dler;
276
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);
284 }
285
286 static int
287 dlpiaddr (int fd, u_char *addr)
288 {
289 dl_phys_addr_req_t dpar;
290 dl_phys_addr_ack_t *dpaa = (dl_phys_addr_ack_t *)dlpi_ctl;
291 int retv;
292
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);
297
298 retv = dlpirctl (fd);
299 if (retv < (ssize_t)DL_PHYS_ADDR_ACK_SIZE
300 || dpaa->dl_primitive != DL_PHYS_ADDR_ACK)
301 return -1;
302
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)
306 return -1;
307
308 bcopy((char *)dpaa + dpaa->dl_addr_offset, addr, ETHERADDRL);
309 return 0;
310 }
311
312 static int
313 open_dlpi_dev (struct isis_circuit *circuit)
314 {
315 int fd = -1, unit, retval;
316 char devpath[MAXPATHLEN];
317 dl_info_ack_t *dia = (dl_info_ack_t *)dlpi_ctl;
318 ssize_t acklen;
319
320 /* Only broadcast-type are supported at the moment */
321 if (circuit->circ_type != CIRCUIT_T_BROADCAST)
322 {
323 zlog_warn ("%s: non-broadcast interface %s", __func__,
324 circuit->interface->name);
325 return ISIS_WARNING;
326 }
327
328 /* Try the vanity node first, if permitted */
329 if (getenv("DLPI_DEVONLY") == NULL)
330 {
331 (void) snprintf (devpath, sizeof(devpath), "/dev/net/%s",
332 circuit->interface->name);
333 fd = dlpiopen (devpath, &acklen);
334 }
335
336 /* Now try as an ordinary Style 1 node */
337 if (fd == -1)
338 {
339 (void) snprintf (devpath, sizeof (devpath), "/dev/%s",
340 circuit->interface->name);
341 unit = -1;
342 fd = dlpiopen (devpath, &acklen);
343 }
344
345 /* If that fails, try again as Style 2 */
346 if (fd == -1)
347 {
348 char *cp;
349
350 cp = devpath + strlen (devpath);
351 while (--cp >= devpath && isdigit(*cp))
352 ;
353 unit = strtol(cp, NULL, 0);
354 *cp = '\0';
355 fd = dlpiopen (devpath, &acklen);
356
357 /* If that too fails, then the device really doesn't exist */
358 if (fd == -1)
359 {
360 zlog_warn ("%s: unknown interface %s", __func__,
361 circuit->interface->name);
362 return ISIS_WARNING;
363 }
364
365 /* Double check the DLPI style */
366 if (dia->dl_provider_style != DL_STYLE2)
367 {
368 zlog_warn ("open_dlpi_dev(): interface %s: %s is not style 2",
369 circuit->interface->name, devpath);
370 close (fd);
371 return ISIS_WARNING;
372 }
373
374 /* If it succeeds, then we need to attach to the unit specified */
375 dlpiattach (fd, unit);
376
377 /* Reget the information, as it may be different per node */
378 if ((acklen = dlpiinfo (fd)) == -1)
379 {
380 close (fd);
381 return ISIS_WARNING;
382 }
383 }
384 else
385 {
386 /* Double check the DLPI style */
387 if (dia->dl_provider_style != DL_STYLE1)
388 {
389 zlog_warn ("open_dlpi_dev(): interface %s: %s is not style 1",
390 circuit->interface->name, devpath);
391 close (fd);
392 return ISIS_WARNING;
393 }
394 }
395
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)
400 {
401 zlog_warn ("%s: unsupported interface type for %s", __func__,
402 circuit->interface->name);
403 close (fd);
404 return ISIS_WARNING;
405 }
406 switch (dia->dl_mac_type)
407 {
408 case DL_CSMACD:
409 case DL_ETHER:
410 case DL_100VG:
411 case DL_100VGTPR:
412 case DL_ETH_CSMA:
413 case DL_100BT:
414 break;
415 default:
416 zlog_warn ("%s: unexpected mac type on %s: %lld", __func__,
417 circuit->interface->name, (long long)dia->dl_mac_type);
418 close (fd);
419 return ISIS_WARNING;
420 }
421
422 circuit->sap_length = dia->dl_sap_length;
423
424 /*
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.)
429 *
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.
432 */
433 if (dlpiaddr (fd, circuit->u.bc.snpa) == -1)
434 {
435 zlog_warn ("open_dlpi_dev(): interface %s: unable to get MAC address",
436 circuit->interface->name);
437 close (fd);
438 return ISIS_WARNING;
439 }
440
441 /* Now bind to SAP 0. This gives us 802-type traffic. */
442 if (dlpibind (fd) == -1)
443 {
444 zlog_warn ("%s: cannot bind SAP 0 on %s", __func__,
445 circuit->interface->name);
446 close (fd);
447 return ISIS_WARNING;
448 }
449
450 /*
451 * Join to multicast groups according to
452 * 8.4.2 - Broadcast subnetwork IIH PDUs
453 */
454 retval = 0;
455 retval |= dlpimcast (fd, ALL_L1_ISS);
456 retval |= dlpimcast (fd, ALL_ISS);
457 retval |= dlpimcast (fd, ALL_L2_ISS);
458
459 if (retval != 0)
460 {
461 zlog_warn ("%s: unable to join multicast on %s", __func__,
462 circuit->interface->name);
463 close (fd);
464 return ISIS_WARNING;
465 }
466
467 /* Push on the packet filter to avoid stray 802 packets */
468 if (ioctl (fd, I_PUSH, "pfmod") == 0)
469 {
470 struct packetfilt pfil;
471 struct strioctl sioc;
472
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;
478 sioc.ic_timout = 5;
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);
484 }
485
486 circuit->fd = fd;
487
488 return ISIS_OK;
489 }
490
491 /*
492 * Create the socket and set the tx/rx funcs
493 */
494 int
495 isis_sock_init (struct isis_circuit *circuit)
496 {
497 int retval = ISIS_OK;
498
499 if (isisd_privs.change (ZPRIVS_RAISE))
500 zlog_err ("%s: could not raise privs, %s", __func__, safe_strerror (errno));
501
502 retval = open_dlpi_dev (circuit);
503
504 if (retval != ISIS_OK)
505 {
506 zlog_warn ("%s: could not initialize the socket", __func__);
507 goto end;
508 }
509
510 if (circuit->circ_type == CIRCUIT_T_BROADCAST)
511 {
512 circuit->tx = isis_send_pdu_bcast;
513 circuit->rx = isis_recv_pdu_bcast;
514 }
515 else
516 {
517 zlog_warn ("isis_sock_init(): unknown circuit type");
518 retval = ISIS_WARNING;
519 goto end;
520 }
521
522 end:
523 if (isisd_privs.change (ZPRIVS_LOWER))
524 zlog_err ("%s: could not lower privs, %s", __func__, safe_strerror (errno));
525
526 return retval;
527 }
528
529 int
530 isis_recv_pdu_bcast (struct isis_circuit *circuit, u_char * ssnpa)
531 {
532 struct pollfd fds[1];
533 struct strbuf ctlbuf, databuf;
534 int flags, retv;
535 dl_unitdata_ind_t *dui = (dl_unitdata_ind_t *)dlpi_ctl;
536
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)
541 return ISIS_WARNING;
542
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;
549 flags = 0;
550 retv = getmsg (circuit->fd, &ctlbuf, &databuf, &flags);
551
552 if (retv < 0)
553 {
554 zlog_warn ("isis_recv_pdu_bcast: getmsg failed: %s",
555 safe_strerror (errno));
556 return ISIS_WARNING;
557 }
558
559 if (retv & (MORECTL | MOREDATA))
560 {
561 while (retv & (MORECTL | MOREDATA))
562 {
563 flags = 0;
564 retv = getmsg (circuit->fd, &ctlbuf, &databuf, &flags);
565 }
566 return ISIS_WARNING;
567 }
568
569 if (ctlbuf.len < (ssize_t)DL_UNITDATA_IND_SIZE ||
570 dui->dl_primitive != DL_UNITDATA_IND)
571 return ISIS_WARNING;
572
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)
576 return ISIS_WARNING;
577
578 memcpy (ssnpa, (char *)dui + dui->dl_src_addr_offset +
579 (circuit->sap_length > 0 ? circuit->sap_length : 0), ETHERADDRL);
580
581 if (databuf.len < LLC_LEN || sock_buff[0] != ISO_SAP ||
582 sock_buff[1] != ISO_SAP || sock_buff[2] != 3)
583 return ISIS_WARNING;
584
585 stream_write (circuit->rcv_stream, sock_buff + LLC_LEN,
586 databuf.len - LLC_LEN);
587 stream_set_getp (circuit->rcv_stream, 0);
588
589 return ISIS_OK;
590 }
591
592 int
593 isis_send_pdu_bcast (struct isis_circuit *circuit, int level)
594 {
595 dl_unitdata_req_t *dur = (dl_unitdata_req_t *)dlpi_ctl;
596 char *dstaddr;
597 u_short *dstsap;
598 int buflen;
599 int rv;
600
601 buflen = stream_get_endp (circuit->snd_stream) + LLC_LEN;
602 if ((size_t)buflen > sizeof (sock_buff))
603 {
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);
607 return ISIS_WARNING;
608 }
609
610 stream_set_getp (circuit->snd_stream, 0);
611
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);
616
617 dstaddr = (char *)(dur + 1);
618 if (circuit->sap_length < 0)
619 {
620 dstsap = (u_short *)(dstaddr + ETHERADDRL);
621 }
622 else
623 {
624 dstsap = (u_short *)dstaddr;
625 dstaddr += circuit->sap_length;
626 }
627 if (level == 1)
628 memcpy (dstaddr, ALL_L1_ISS, ETHERADDRL);
629 else
630 memcpy (dstaddr, ALL_L2_ISS, ETHERADDRL);
631 /* Note: DLPI SAP values are in host byte order */
632 *dstsap = buflen;
633
634 sock_buff[0] = ISO_SAP;
635 sock_buff[1] = ISO_SAP;
636 sock_buff[2] = 0x03;
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);
641 if (rv < 0)
642 {
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))
646 return ISIS_WARNING;
647 return ISIS_ERROR;
648 }
649
650 return ISIS_OK;
651 }
652
653 #endif /* ISIS_METHOD == ISIS_METHOD_DLPI */