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