Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 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; if not, write to the Free Software Foundation, Inc., |
| 20 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <zebra.h> |
David Lamparter | 745bf05 | 2010-02-02 21:49:35 +0100 | [diff] [blame] | 24 | #if ISIS_METHOD == ISIS_METHOD_DLPI |
Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 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 "stream.h" |
| 37 | #include "if.h" |
| 38 | |
| 39 | #include "isisd/dict.h" |
| 40 | #include "isisd/include-netbsd/iso.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" |
Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 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 void |
| 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 | |
| 101 | if (cbuf != NULL) |
| 102 | { |
| 103 | memset (&ctlbuf, 0, sizeof (ctlbuf)); |
| 104 | ctlbuf.len = cbuflen; |
| 105 | ctlbuf.buf = (void *)cbuf; |
| 106 | ctlptr = &ctlbuf; |
| 107 | } |
| 108 | |
| 109 | if (dbuf != NULL) |
| 110 | { |
| 111 | memset (&databuf, 0, sizeof (databuf)); |
| 112 | databuf.len = dbuflen; |
| 113 | databuf.buf = (void *)dbuf; |
| 114 | dataptr = &databuf; |
| 115 | } |
| 116 | |
| 117 | /* We assume this doesn't happen often and isn't operationally significant */ |
| 118 | if (putmsg (fd, ctlptr, dataptr, flags) == -1) |
| 119 | zlog_debug ("%s: putmsg: %s", __func__, safe_strerror (errno)); |
| 120 | } |
| 121 | |
| 122 | static ssize_t |
| 123 | dlpirctl (int fd) |
| 124 | { |
| 125 | struct pollfd fds[1]; |
| 126 | struct strbuf ctlbuf, databuf; |
| 127 | int flags, retv; |
| 128 | |
| 129 | do |
| 130 | { |
| 131 | /* Poll is used here in case the device doesn't speak DLPI 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 | } |
| 150 | while (ctlbuf.len == 0); |
| 151 | |
| 152 | if (!(retv & MORECTL)) |
| 153 | { |
| 154 | while (retv & MOREDATA) |
| 155 | { |
| 156 | flags = 0; |
| 157 | retv = getmsg (fd, NULL, &databuf, &flags); |
| 158 | } |
| 159 | return ctlbuf.len; |
| 160 | } |
| 161 | |
| 162 | while (retv & MORECTL) |
| 163 | { |
| 164 | flags = 0; |
| 165 | retv = getmsg (fd, &ctlbuf, &databuf, &flags); |
| 166 | } |
| 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | static int |
| 171 | dlpiok (int fd, t_uscalar_t oprim) |
| 172 | { |
| 173 | int retv; |
| 174 | dl_ok_ack_t *doa = (dl_ok_ack_t *)dlpi_ctl; |
| 175 | |
| 176 | retv = dlpirctl (fd); |
David Lamparter | ba6cd58 | 2015-09-15 03:00:59 -0700 | [diff] [blame] | 177 | if (retv < (ssize_t)DL_OK_ACK_SIZE || doa->dl_primitive != DL_OK_ACK || |
Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 178 | doa->dl_correct_primitive != oprim) |
| 179 | { |
| 180 | return -1; |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | return 0; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | static int |
| 189 | dlpiinfo (int fd) |
| 190 | { |
| 191 | dl_info_req_t dir; |
| 192 | ssize_t retv; |
| 193 | |
| 194 | memset (&dir, 0, sizeof (dir)); |
| 195 | dir.dl_primitive = DL_INFO_REQ; |
| 196 | /* Info_req uses M_PCPROTO. */ |
| 197 | dlpisend (fd, &dir, sizeof (dir), NULL, 0, RS_HIPRI); |
| 198 | retv = dlpirctl (fd); |
David Lamparter | ba6cd58 | 2015-09-15 03:00:59 -0700 | [diff] [blame] | 199 | if (retv < (ssize_t)DL_INFO_ACK_SIZE || dlpi_ctl[0] != DL_INFO_ACK) |
Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 200 | return -1; |
| 201 | else |
| 202 | return retv; |
| 203 | } |
| 204 | |
| 205 | static int |
| 206 | dlpiopen (const char *devpath, ssize_t *acklen) |
| 207 | { |
| 208 | int fd, flags; |
| 209 | |
| 210 | fd = open (devpath, O_RDWR | O_NONBLOCK | O_NOCTTY); |
| 211 | if (fd == -1) |
| 212 | return -1; |
| 213 | |
| 214 | /* All that we want is for the open itself to be non-blocking, not I/O. */ |
| 215 | flags = fcntl (fd, F_GETFL, 0); |
| 216 | if (flags != -1) |
| 217 | fcntl (fd, F_SETFL, flags & ~O_NONBLOCK); |
| 218 | |
| 219 | /* After opening, ask for information */ |
| 220 | if ((*acklen = dlpiinfo (fd)) == -1) |
| 221 | { |
| 222 | close (fd); |
| 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | return fd; |
| 227 | } |
| 228 | |
| 229 | static int |
| 230 | dlpiattach (int fd, int unit) |
| 231 | { |
| 232 | dl_attach_req_t dar; |
| 233 | |
| 234 | memset (&dar, 0, sizeof (dar)); |
| 235 | dar.dl_primitive = DL_ATTACH_REQ; |
| 236 | dar.dl_ppa = unit; |
| 237 | dlpisend (fd, &dar, sizeof (dar), NULL, 0, 0); |
| 238 | return dlpiok (fd, dar.dl_primitive); |
| 239 | } |
| 240 | |
| 241 | static int |
| 242 | dlpibind (int fd) |
| 243 | { |
| 244 | dl_bind_req_t dbr; |
| 245 | int retv; |
| 246 | dl_bind_ack_t *dba = (dl_bind_ack_t *)dlpi_ctl; |
| 247 | |
| 248 | memset (&dbr, 0, sizeof (dbr)); |
| 249 | dbr.dl_primitive = DL_BIND_REQ; |
| 250 | dbr.dl_service_mode = DL_CLDLS; |
| 251 | dlpisend (fd, &dbr, sizeof (dbr), NULL, 0, 0); |
| 252 | |
| 253 | retv = dlpirctl (fd); |
David Lamparter | ba6cd58 | 2015-09-15 03:00:59 -0700 | [diff] [blame] | 254 | if (retv < (ssize_t)DL_BIND_ACK_SIZE || dba->dl_primitive != DL_BIND_ACK) |
Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 255 | return -1; |
| 256 | else |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int |
| 261 | dlpimcast (int fd, const u_char *mcaddr) |
| 262 | { |
| 263 | struct { |
| 264 | dl_enabmulti_req_t der; |
| 265 | u_char addr[ETHERADDRL]; |
| 266 | } dler; |
| 267 | |
| 268 | memset (&dler, 0, sizeof (dler)); |
| 269 | dler.der.dl_primitive = DL_ENABMULTI_REQ; |
| 270 | dler.der.dl_addr_length = sizeof (dler.addr); |
| 271 | dler.der.dl_addr_offset = dler.addr - (u_char *)&dler; |
| 272 | memcpy (dler.addr, mcaddr, sizeof (dler.addr)); |
| 273 | dlpisend (fd, &dler, sizeof (dler), NULL, 0, 0); |
| 274 | return dlpiok (fd, dler.der.dl_primitive); |
| 275 | } |
| 276 | |
| 277 | static int |
| 278 | dlpiaddr (int fd, u_char *addr) |
| 279 | { |
| 280 | dl_phys_addr_req_t dpar; |
| 281 | dl_phys_addr_ack_t *dpaa = (dl_phys_addr_ack_t *)dlpi_ctl; |
| 282 | int retv; |
| 283 | |
| 284 | memset (&dpar, 0, sizeof (dpar)); |
| 285 | dpar.dl_primitive = DL_PHYS_ADDR_REQ; |
| 286 | dpar.dl_addr_type = DL_CURR_PHYS_ADDR; |
| 287 | dlpisend (fd, &dpar, sizeof (dpar), NULL, 0, 0); |
| 288 | |
| 289 | retv = dlpirctl (fd); |
David Lamparter | ba6cd58 | 2015-09-15 03:00:59 -0700 | [diff] [blame] | 290 | if (retv < (ssize_t)DL_PHYS_ADDR_ACK_SIZE |
| 291 | || dpaa->dl_primitive != DL_PHYS_ADDR_ACK) |
Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 292 | return -1; |
| 293 | |
| 294 | if (dpaa->dl_addr_offset < DL_PHYS_ADDR_ACK_SIZE || |
| 295 | dpaa->dl_addr_length != ETHERADDRL || |
David Lamparter | ba6cd58 | 2015-09-15 03:00:59 -0700 | [diff] [blame] | 296 | dpaa->dl_addr_offset + dpaa->dl_addr_length > (size_t)retv) |
Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 297 | return -1; |
| 298 | |
| 299 | bcopy((char *)dpaa + dpaa->dl_addr_offset, addr, ETHERADDRL); |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static int |
| 304 | open_dlpi_dev (struct isis_circuit *circuit) |
| 305 | { |
David Lamparter | f90ce64 | 2015-09-15 20:58:29 -0700 | [diff] [blame] | 306 | int fd = -1, unit, retval; |
Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 307 | char devpath[MAXPATHLEN]; |
| 308 | dl_info_ack_t *dia = (dl_info_ack_t *)dlpi_ctl; |
| 309 | ssize_t acklen; |
| 310 | |
| 311 | /* Only broadcast-type are supported at the moment */ |
| 312 | if (circuit->circ_type != CIRCUIT_T_BROADCAST) |
| 313 | { |
| 314 | zlog_warn ("%s: non-broadcast interface %s", __func__, |
| 315 | circuit->interface->name); |
| 316 | return ISIS_WARNING; |
| 317 | } |
Jingjing Duan | 6995456 | 2008-08-13 19:29:47 +0100 | [diff] [blame] | 318 | |
| 319 | /* Try the vanity node first, if permitted */ |
| 320 | if (getenv("DLPI_DEVONLY") == NULL) |
| 321 | { |
| 322 | (void) snprintf (devpath, sizeof(devpath), "/dev/net/%s", |
| 323 | circuit->interface->name); |
| 324 | fd = dlpiopen (devpath, &acklen); |
| 325 | } |
| 326 | |
| 327 | /* Now try as an ordinary Style 1 node */ |
| 328 | if (fd == -1) |
| 329 | { |
| 330 | (void) snprintf (devpath, sizeof (devpath), "/dev/%s", |
| 331 | circuit->interface->name); |
| 332 | unit = -1; |
| 333 | fd = dlpiopen (devpath, &acklen); |
| 334 | } |
Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 335 | |
| 336 | /* If that fails, try again as Style 2 */ |
| 337 | if (fd == -1) |
| 338 | { |
| 339 | char *cp; |
| 340 | |
| 341 | cp = devpath + strlen (devpath); |
| 342 | while (--cp >= devpath && isdigit(*cp)) |
| 343 | ; |
| 344 | unit = strtol(cp, NULL, 0); |
| 345 | *cp = '\0'; |
| 346 | fd = dlpiopen (devpath, &acklen); |
| 347 | |
| 348 | /* If that too fails, then the device really doesn't exist */ |
| 349 | if (fd == -1) |
| 350 | { |
| 351 | zlog_warn ("%s: unknown interface %s", __func__, |
| 352 | circuit->interface->name); |
| 353 | return ISIS_WARNING; |
| 354 | } |
| 355 | |
| 356 | /* Double check the DLPI style */ |
| 357 | if (dia->dl_provider_style != DL_STYLE2) |
| 358 | { |
| 359 | zlog_warn ("open_dlpi_dev(): interface %s: %s is not style 2", |
| 360 | circuit->interface->name, devpath); |
| 361 | close (fd); |
| 362 | return ISIS_WARNING; |
| 363 | } |
| 364 | |
| 365 | /* If it succeeds, then we need to attach to the unit specified */ |
| 366 | dlpiattach (fd, unit); |
| 367 | |
| 368 | /* Reget the information, as it may be different per node */ |
| 369 | if ((acklen = dlpiinfo (fd)) == -1) |
| 370 | { |
| 371 | close (fd); |
| 372 | return ISIS_WARNING; |
| 373 | } |
| 374 | } |
| 375 | else |
| 376 | { |
| 377 | /* Double check the DLPI style */ |
| 378 | if (dia->dl_provider_style != DL_STYLE1) |
| 379 | { |
| 380 | zlog_warn ("open_dlpi_dev(): interface %s: %s is not style 1", |
| 381 | circuit->interface->name, devpath); |
| 382 | close (fd); |
| 383 | return ISIS_WARNING; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | /* Check that the interface we've got is the kind we expect */ |
| 388 | if ((dia->dl_sap_length != 2 && dia->dl_sap_length != -2) || |
| 389 | dia->dl_service_mode != DL_CLDLS || dia->dl_addr_length != ETHERADDRL + 2 || |
| 390 | dia->dl_brdcst_addr_length != ETHERADDRL) |
| 391 | { |
| 392 | zlog_warn ("%s: unsupported interface type for %s", __func__, |
| 393 | circuit->interface->name); |
| 394 | close (fd); |
| 395 | return ISIS_WARNING; |
| 396 | } |
| 397 | switch (dia->dl_mac_type) |
| 398 | { |
| 399 | case DL_CSMACD: |
| 400 | case DL_ETHER: |
| 401 | case DL_100VG: |
| 402 | case DL_100VGTPR: |
| 403 | case DL_ETH_CSMA: |
| 404 | case DL_100BT: |
| 405 | break; |
| 406 | default: |
David Lamparter | ba6cd58 | 2015-09-15 03:00:59 -0700 | [diff] [blame] | 407 | zlog_warn ("%s: unexpected mac type on %s: %lld", __func__, |
| 408 | circuit->interface->name, (long long)dia->dl_mac_type); |
Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 409 | close (fd); |
| 410 | return ISIS_WARNING; |
| 411 | } |
| 412 | |
| 413 | circuit->sap_length = dia->dl_sap_length; |
| 414 | |
| 415 | /* |
| 416 | * The local hardware address is something that should be provided by way of |
| 417 | * sockaddr_dl for the interface, but isn't on Solaris. We set it here based |
| 418 | * on DLPI's reported address to avoid roto-tilling the world. |
| 419 | * (Note that isis_circuit_if_add on Solaris doesn't set the snpa.) |
| 420 | * |
| 421 | * Unfortunately, GLD is broken and doesn't provide the address after attach, |
| 422 | * so we need to be careful and use DL_PHYS_ADDR_REQ instead. |
| 423 | */ |
| 424 | if (dlpiaddr (fd, circuit->u.bc.snpa) == -1) |
| 425 | { |
| 426 | zlog_warn ("open_dlpi_dev(): interface %s: unable to get MAC address", |
| 427 | circuit->interface->name); |
| 428 | close (fd); |
| 429 | return ISIS_WARNING; |
| 430 | } |
| 431 | |
| 432 | /* Now bind to SAP 0. This gives us 802-type traffic. */ |
| 433 | if (dlpibind (fd) == -1) |
| 434 | { |
| 435 | zlog_warn ("%s: cannot bind SAP 0 on %s", __func__, |
| 436 | circuit->interface->name); |
| 437 | close (fd); |
| 438 | return ISIS_WARNING; |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * Join to multicast groups according to |
| 443 | * 8.4.2 - Broadcast subnetwork IIH PDUs |
| 444 | */ |
| 445 | retval = 0; |
David Lamparter | 318c804 | 2012-11-27 01:10:29 +0000 | [diff] [blame] | 446 | retval |= dlpimcast (fd, ALL_L1_ISS); |
| 447 | retval |= dlpimcast (fd, ALL_ISS); |
| 448 | retval |= dlpimcast (fd, ALL_L2_ISS); |
Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 449 | |
| 450 | if (retval != 0) |
| 451 | { |
| 452 | zlog_warn ("%s: unable to join multicast on %s", __func__, |
| 453 | circuit->interface->name); |
| 454 | close (fd); |
| 455 | return ISIS_WARNING; |
| 456 | } |
| 457 | |
| 458 | /* Push on the packet filter to avoid stray 802 packets */ |
| 459 | if (ioctl (fd, I_PUSH, "pfmod") == 0) |
| 460 | { |
| 461 | struct packetfilt pfil; |
Jingjing Duan | 3e40282 | 2008-08-13 19:06:16 +0100 | [diff] [blame] | 462 | struct strioctl sioc; |
Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 463 | |
| 464 | pfil.Pf_Priority = 0; |
| 465 | pfil.Pf_FilterLen = sizeof (pf_filter) / sizeof (u_short); |
| 466 | memcpy (pfil.Pf_Filter, pf_filter, sizeof (pf_filter)); |
Jingjing Duan | 3e40282 | 2008-08-13 19:06:16 +0100 | [diff] [blame] | 467 | /* pfmod does not support transparent ioctls */ |
| 468 | sioc.ic_cmd = PFIOCSETF; |
| 469 | sioc.ic_timout = 5; |
| 470 | sioc.ic_len = sizeof (struct packetfilt); |
| 471 | sioc.ic_dp = (char *)&pfil; |
| 472 | if (ioctl (fd, I_STR, &sioc) == -1) |
| 473 | zlog_warn("%s: could not perform PF_IOCSETF on %s", |
| 474 | __func__, circuit->interface->name); |
Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | circuit->fd = fd; |
| 478 | |
| 479 | return ISIS_OK; |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * Create the socket and set the tx/rx funcs |
| 484 | */ |
| 485 | int |
| 486 | isis_sock_init (struct isis_circuit *circuit) |
| 487 | { |
| 488 | int retval = ISIS_OK; |
| 489 | |
| 490 | if (isisd_privs.change (ZPRIVS_RAISE)) |
| 491 | zlog_err ("%s: could not raise privs, %s", __func__, safe_strerror (errno)); |
| 492 | |
| 493 | retval = open_dlpi_dev (circuit); |
| 494 | |
| 495 | if (retval != ISIS_OK) |
| 496 | { |
| 497 | zlog_warn ("%s: could not initialize the socket", __func__); |
| 498 | goto end; |
| 499 | } |
| 500 | |
| 501 | if (circuit->circ_type == CIRCUIT_T_BROADCAST) |
| 502 | { |
| 503 | circuit->tx = isis_send_pdu_bcast; |
| 504 | circuit->rx = isis_recv_pdu_bcast; |
| 505 | } |
| 506 | else |
| 507 | { |
| 508 | zlog_warn ("isis_sock_init(): unknown circuit type"); |
| 509 | retval = ISIS_WARNING; |
| 510 | goto end; |
| 511 | } |
| 512 | |
| 513 | end: |
| 514 | if (isisd_privs.change (ZPRIVS_LOWER)) |
| 515 | zlog_err ("%s: could not lower privs, %s", __func__, safe_strerror (errno)); |
| 516 | |
| 517 | return retval; |
| 518 | } |
| 519 | |
| 520 | int |
| 521 | isis_recv_pdu_bcast (struct isis_circuit *circuit, u_char * ssnpa) |
| 522 | { |
| 523 | struct pollfd fds[1]; |
| 524 | struct strbuf ctlbuf, databuf; |
| 525 | int flags, retv; |
| 526 | dl_unitdata_ind_t *dui = (dl_unitdata_ind_t *)dlpi_ctl; |
| 527 | |
| 528 | memset (fds, 0, sizeof (fds)); |
| 529 | fds[0].fd = circuit->fd; |
| 530 | fds[0].events = POLLIN | POLLPRI; |
| 531 | if (poll (fds, 1, 0) <= 0) |
| 532 | return ISIS_WARNING; |
| 533 | |
| 534 | memset (&ctlbuf, 0, sizeof (ctlbuf)); |
| 535 | memset (&databuf, 0, sizeof (databuf)); |
| 536 | ctlbuf.maxlen = sizeof (dlpi_ctl); |
| 537 | ctlbuf.buf = (void *)dlpi_ctl; |
| 538 | databuf.maxlen = sizeof (sock_buff); |
| 539 | databuf.buf = (void *)sock_buff; |
| 540 | flags = 0; |
| 541 | retv = getmsg (circuit->fd, &ctlbuf, &databuf, &flags); |
| 542 | |
| 543 | if (retv < 0) |
| 544 | { |
| 545 | zlog_warn ("isis_recv_pdu_bcast: getmsg failed: %s", |
| 546 | safe_strerror (errno)); |
| 547 | return ISIS_WARNING; |
| 548 | } |
| 549 | |
| 550 | if (retv & (MORECTL | MOREDATA)) |
| 551 | { |
| 552 | while (retv & (MORECTL | MOREDATA)) |
| 553 | { |
| 554 | flags = 0; |
| 555 | retv = getmsg (circuit->fd, &ctlbuf, &databuf, &flags); |
| 556 | } |
| 557 | return ISIS_WARNING; |
| 558 | } |
| 559 | |
David Lamparter | ba6cd58 | 2015-09-15 03:00:59 -0700 | [diff] [blame] | 560 | if (ctlbuf.len < (ssize_t)DL_UNITDATA_IND_SIZE || |
Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 561 | dui->dl_primitive != DL_UNITDATA_IND) |
| 562 | return ISIS_WARNING; |
| 563 | |
| 564 | if (dui->dl_src_addr_length != ETHERADDRL + 2 || |
| 565 | dui->dl_src_addr_offset < DL_UNITDATA_IND_SIZE || |
David Lamparter | ba6cd58 | 2015-09-15 03:00:59 -0700 | [diff] [blame] | 566 | dui->dl_src_addr_offset + dui->dl_src_addr_length > (size_t)ctlbuf.len) |
Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 567 | return ISIS_WARNING; |
| 568 | |
| 569 | memcpy (ssnpa, (char *)dui + dui->dl_src_addr_offset + |
| 570 | (circuit->sap_length > 0 ? circuit->sap_length : 0), ETHERADDRL); |
| 571 | |
| 572 | if (databuf.len < LLC_LEN || sock_buff[0] != ISO_SAP || |
| 573 | sock_buff[1] != ISO_SAP || sock_buff[2] != 3) |
| 574 | return ISIS_WARNING; |
| 575 | |
| 576 | stream_write (circuit->rcv_stream, sock_buff + LLC_LEN, |
| 577 | databuf.len - LLC_LEN); |
| 578 | stream_set_getp (circuit->rcv_stream, 0); |
| 579 | |
| 580 | return ISIS_OK; |
| 581 | } |
| 582 | |
| 583 | int |
| 584 | isis_send_pdu_bcast (struct isis_circuit *circuit, int level) |
| 585 | { |
| 586 | dl_unitdata_req_t *dur = (dl_unitdata_req_t *)dlpi_ctl; |
| 587 | char *dstaddr; |
| 588 | u_short *dstsap; |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 589 | int buflen; |
| 590 | |
| 591 | buflen = stream_get_endp (circuit->snd_stream) + LLC_LEN; |
David Lamparter | ba6cd58 | 2015-09-15 03:00:59 -0700 | [diff] [blame] | 592 | if ((size_t)buflen > sizeof (sock_buff)) |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 593 | { |
David Lamparter | ba6cd58 | 2015-09-15 03:00:59 -0700 | [diff] [blame] | 594 | zlog_warn ("isis_send_pdu_bcast: sock_buff size %zu is less than " |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 595 | "output pdu size %d on circuit %s", |
| 596 | sizeof (sock_buff), buflen, circuit->interface->name); |
| 597 | return ISIS_WARNING; |
| 598 | } |
Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 599 | |
| 600 | stream_set_getp (circuit->snd_stream, 0); |
| 601 | |
| 602 | memset (dur, 0, sizeof (*dur)); |
| 603 | dur->dl_primitive = DL_UNITDATA_REQ; |
| 604 | dur->dl_dest_addr_length = ETHERADDRL + 2; |
| 605 | dur->dl_dest_addr_offset = sizeof (*dur); |
| 606 | |
| 607 | dstaddr = (char *)(dur + 1); |
| 608 | if (circuit->sap_length < 0) |
| 609 | { |
| 610 | dstsap = (u_short *)(dstaddr + ETHERADDRL); |
| 611 | } |
| 612 | else |
| 613 | { |
| 614 | dstsap = (u_short *)dstaddr; |
| 615 | dstaddr += circuit->sap_length; |
| 616 | } |
| 617 | if (level == 1) |
| 618 | memcpy (dstaddr, ALL_L1_ISS, ETHERADDRL); |
| 619 | else |
| 620 | memcpy (dstaddr, ALL_L2_ISS, ETHERADDRL); |
| 621 | /* Note: DLPI SAP values are in host byte order */ |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 622 | *dstsap = buflen; |
Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 623 | |
| 624 | sock_buff[0] = ISO_SAP; |
| 625 | sock_buff[1] = ISO_SAP; |
| 626 | sock_buff[2] = 0x03; |
| 627 | memcpy (sock_buff + LLC_LEN, circuit->snd_stream->data, |
| 628 | stream_get_endp (circuit->snd_stream)); |
| 629 | dlpisend (circuit->fd, dur, sizeof (*dur) + dur->dl_dest_addr_length, |
Josh Bailey | 3f045a0 | 2012-03-24 08:35:20 -0700 | [diff] [blame] | 630 | sock_buff, buflen, 0); |
Paul Jakma | 8bc9805 | 2007-08-08 11:19:27 +0000 | [diff] [blame] | 631 | return ISIS_OK; |
| 632 | } |
David Lamparter | 745bf05 | 2010-02-02 21:49:35 +0100 | [diff] [blame] | 633 | |
| 634 | #endif /* ISIS_METHOD == ISIS_METHOD_DLPI */ |