jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * IS-IS Rout(e)ing protocol - isis_network.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 <sys/types.h> |
| 24 | #include <sys/socket.h> |
| 25 | #include <errno.h> |
| 26 | #include <zebra.h> |
hasso | 37da8c0 | 2004-05-19 11:38:40 +0000 | [diff] [blame] | 27 | #ifdef GNU_LINUX |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 28 | #include <net/ethernet.h> /* the L2 protocols */ |
hasso | 37da8c0 | 2004-05-19 11:38:40 +0000 | [diff] [blame] | 29 | #else |
| 30 | #include <net/if.h> |
| 31 | #include <netinet/if_ether.h> |
| 32 | #endif |
| 33 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 34 | #include "log.h" |
| 35 | #include "stream.h" |
| 36 | #include "if.h" |
| 37 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 38 | #include "isisd/dict.h" |
| 39 | #include "isisd/include-netbsd/iso.h" |
| 40 | #include "isisd/isis_constants.h" |
| 41 | #include "isisd/isis_common.h" |
| 42 | #include "isisd/isis_circuit.h" |
| 43 | #include "isisd/isis_flags.h" |
| 44 | #include "isisd/isisd.h" |
| 45 | #include "isisd/isis_constants.h" |
| 46 | #include "isisd/isis_circuit.h" |
| 47 | #include "isisd/isis_network.h" |
| 48 | |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 49 | #include "privs.h" |
| 50 | |
| 51 | extern struct zebra_privs_t isisd_privs; |
| 52 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 53 | /* |
| 54 | * On linux we can use the packet(7) sockets, in other OSs we have to do with |
| 55 | * Berkley Packet Filter (BPF). Please tell me if you can think of a better |
| 56 | * way... |
| 57 | */ |
| 58 | #ifdef GNU_LINUX |
| 59 | #include <netpacket/packet.h> |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 60 | #else |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 61 | #include <sys/time.h> |
| 62 | #include <sys/ioctl.h> |
| 63 | #include <net/bpf.h> |
| 64 | struct bpf_insn llcfilter[] = { |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 65 | BPF_STMT (BPF_LD + BPF_B + BPF_ABS, ETHER_HDR_LEN), /* check first byte */ |
| 66 | BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ISO_SAP, 0, 5), |
| 67 | BPF_STMT (BPF_LD + BPF_B + BPF_ABS, ETHER_HDR_LEN + 1), |
| 68 | BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ISO_SAP, 0, 3), /* check second byte */ |
| 69 | BPF_STMT (BPF_LD + BPF_B + BPF_ABS, ETHER_HDR_LEN + 2), |
| 70 | BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, 0x03, 0, 1), /* check third byte */ |
| 71 | BPF_STMT (BPF_RET + BPF_K, (u_int) - 1), |
| 72 | BPF_STMT (BPF_RET + BPF_K, 0) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 73 | }; |
| 74 | int readblen = 0; |
| 75 | u_char *readbuff = NULL; |
| 76 | #endif /* GNU_LINUX */ |
| 77 | |
| 78 | /* |
| 79 | * Table 9 - Architectural constans for use with ISO 8802 subnetworks |
| 80 | * ISO 10589 - 8.4.8 |
| 81 | */ |
| 82 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 83 | u_char ALL_L1_ISS[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x14 }; |
| 84 | u_char ALL_L2_ISS[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x15 }; |
| 85 | u_char ALL_ISS[6] = { 0x09, 0x00, 0x2B, 0x00, 0x00, 0x05 }; |
| 86 | u_char ALL_ESS[6] = { 0x09, 0x00, 0x2B, 0x00, 0x00, 0x04 }; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 87 | |
| 88 | #ifdef GNU_LINUX |
| 89 | static char discard_buff[8192]; |
| 90 | #endif |
| 91 | static char sock_buff[8192]; |
| 92 | |
| 93 | /* |
| 94 | * if level is 0 we are joining p2p multicast |
| 95 | * FIXME: and the p2p multicast being ??? |
| 96 | */ |
| 97 | #ifdef GNU_LINUX |
hasso | 9236588 | 2005-01-18 13:53:33 +0000 | [diff] [blame] | 98 | static int |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 99 | isis_multicast_join (int fd, int registerto, int if_num) |
| 100 | { |
| 101 | struct packet_mreq mreq; |
| 102 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 103 | memset (&mreq, 0, sizeof (mreq)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 104 | mreq.mr_ifindex = if_num; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 105 | if (registerto) |
| 106 | { |
| 107 | mreq.mr_type = PACKET_MR_MULTICAST; |
| 108 | mreq.mr_alen = ETH_ALEN; |
| 109 | if (registerto == 1) |
| 110 | memcpy (&mreq.mr_address, ALL_L1_ISS, ETH_ALEN); |
| 111 | else if (registerto == 2) |
| 112 | memcpy (&mreq.mr_address, ALL_L2_ISS, ETH_ALEN); |
| 113 | else if (registerto == 3) |
| 114 | memcpy (&mreq.mr_address, ALL_ISS, ETH_ALEN); |
| 115 | else |
| 116 | memcpy (&mreq.mr_address, ALL_ESS, ETH_ALEN); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 117 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 118 | } |
| 119 | else |
| 120 | { |
| 121 | mreq.mr_type = PACKET_MR_ALLMULTI; |
| 122 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 123 | #ifdef EXTREME_DEBUG |
hasso | 529d65b | 2004-12-24 00:14:50 +0000 | [diff] [blame] | 124 | zlog_debug ("isis_multicast_join(): fd=%d, reg_to=%d, if_num=%d, " |
| 125 | "address = %02x:%02x:%02x:%02x:%02x:%02x", |
| 126 | fd, registerto, if_num, mreq.mr_address[0], mreq.mr_address[1], |
| 127 | mreq.mr_address[2], mreq.mr_address[3], mreq.mr_address[4], |
| 128 | mreq.mr_address[5]); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 129 | #endif /* EXTREME_DEBUG */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 130 | if (setsockopt (fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, |
| 131 | sizeof (struct packet_mreq))) |
| 132 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 133 | zlog_warn ("isis_multicast_join(): setsockopt(): %s", safe_strerror (errno)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 134 | return ISIS_WARNING; |
| 135 | } |
| 136 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 137 | return ISIS_OK; |
| 138 | } |
| 139 | |
hasso | 9236588 | 2005-01-18 13:53:33 +0000 | [diff] [blame] | 140 | static int |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 141 | open_packet_socket (struct isis_circuit *circuit) |
| 142 | { |
| 143 | struct sockaddr_ll s_addr; |
| 144 | int fd, retval = ISIS_OK; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 145 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 146 | fd = socket (PF_PACKET, SOCK_DGRAM, htons (ETH_P_ALL)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 147 | if (fd < 0) |
| 148 | { |
| 149 | zlog_warn ("open_packet_socket(): socket() failed %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 150 | safe_strerror (errno)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 151 | return ISIS_WARNING; |
| 152 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 153 | |
| 154 | /* |
| 155 | * Bind to the physical interface |
| 156 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 157 | memset (&s_addr, 0, sizeof (struct sockaddr_ll)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 158 | s_addr.sll_family = AF_PACKET; |
| 159 | s_addr.sll_protocol = htons (ETH_P_ALL); |
| 160 | s_addr.sll_ifindex = circuit->interface->ifindex; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 161 | |
| 162 | if (bind (fd, (struct sockaddr *) (&s_addr), |
| 163 | sizeof (struct sockaddr_ll)) < 0) |
| 164 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 165 | zlog_warn ("open_packet_socket(): bind() failed: %s", safe_strerror (errno)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 166 | return ISIS_WARNING; |
| 167 | } |
| 168 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 169 | circuit->fd = fd; |
| 170 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 171 | if (circuit->circ_type == CIRCUIT_T_BROADCAST) |
| 172 | { |
| 173 | /* |
| 174 | * Join to multicast groups |
| 175 | * according to |
| 176 | * 8.4.2 - Broadcast subnetwork IIH PDUs |
| 177 | * FIXME: is there a case only one will fail?? |
| 178 | */ |
| 179 | if (circuit->circuit_is_type & IS_LEVEL_1) |
| 180 | { |
| 181 | /* joining ALL_L1_ISS */ |
| 182 | retval = isis_multicast_join (circuit->fd, 1, |
| 183 | circuit->interface->ifindex); |
| 184 | /* joining ALL_ISS */ |
| 185 | retval = isis_multicast_join (circuit->fd, 3, |
| 186 | circuit->interface->ifindex); |
| 187 | } |
| 188 | if (circuit->circuit_is_type & IS_LEVEL_2) |
| 189 | /* joining ALL_L2_ISS */ |
| 190 | retval = isis_multicast_join (circuit->fd, 2, |
| 191 | circuit->interface->ifindex); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 192 | } |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 193 | else |
| 194 | { |
| 195 | retval = |
| 196 | isis_multicast_join (circuit->fd, 0, circuit->interface->ifindex); |
| 197 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 198 | |
| 199 | return retval; |
| 200 | } |
| 201 | |
| 202 | #else |
| 203 | |
hasso | 9236588 | 2005-01-18 13:53:33 +0000 | [diff] [blame] | 204 | static int |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 205 | open_bpf_dev (struct isis_circuit *circuit) |
| 206 | { |
| 207 | int i = 0, fd; |
| 208 | char bpfdev[128]; |
| 209 | struct ifreq ifr; |
| 210 | u_int16_t blen; |
| 211 | int true = 1, false = 0; |
| 212 | struct timeval timeout; |
| 213 | struct bpf_program bpf_prog; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 214 | |
| 215 | do |
| 216 | { |
| 217 | (void) snprintf (bpfdev, sizeof (bpfdev), "/dev/bpf%d", i++); |
| 218 | fd = open (bpfdev, O_RDWR); |
| 219 | } |
| 220 | while (fd < 0 && errno == EBUSY); |
| 221 | |
| 222 | if (fd < 0) |
| 223 | { |
| 224 | zlog_warn ("open_bpf_dev(): failed to create bpf socket: %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 225 | safe_strerror (errno)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 226 | return ISIS_WARNING; |
| 227 | } |
| 228 | |
hasso | 529d65b | 2004-12-24 00:14:50 +0000 | [diff] [blame] | 229 | zlog_debug ("Opened BPF device %s", bpfdev); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 230 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 231 | memcpy (ifr.ifr_name, circuit->interface->name, sizeof (ifr.ifr_name)); |
| 232 | if (ioctl (fd, BIOCSETIF, (caddr_t) & ifr) < 0) |
| 233 | { |
| 234 | zlog_warn ("open_bpf_dev(): failed to bind to interface: %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 235 | safe_strerror (errno)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 236 | return ISIS_WARNING; |
| 237 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 238 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 239 | if (ioctl (fd, BIOCGBLEN, (caddr_t) & blen) < 0) |
| 240 | { |
| 241 | zlog_warn ("failed to get BPF buffer len"); |
| 242 | blen = circuit->interface->mtu; |
| 243 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 244 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 245 | readblen = blen; |
| 246 | |
| 247 | if (readbuff == NULL) |
| 248 | readbuff = malloc (blen); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 249 | |
hasso | 529d65b | 2004-12-24 00:14:50 +0000 | [diff] [blame] | 250 | zlog_debug ("BPF buffer len = %u", blen); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 251 | |
| 252 | /* BPF(4): reads return immediately upon packet reception. |
| 253 | * Otherwise, a read will block until either the kernel |
| 254 | * buffer becomes full or a timeout occurs. |
| 255 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 256 | if (ioctl (fd, BIOCIMMEDIATE, (caddr_t) & true) < 0) |
| 257 | { |
| 258 | zlog_warn ("failed to set BPF dev to immediate mode"); |
| 259 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 260 | |
hasso | 37da8c0 | 2004-05-19 11:38:40 +0000 | [diff] [blame] | 261 | #ifdef BIOCSSEESENT |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 262 | /* |
| 263 | * We want to see only incoming packets |
| 264 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 265 | if (ioctl (fd, BIOCSSEESENT, (caddr_t) & false) < 0) |
| 266 | { |
| 267 | zlog_warn ("failed to set BPF dev to incoming only mode"); |
| 268 | } |
hasso | 37da8c0 | 2004-05-19 11:38:40 +0000 | [diff] [blame] | 269 | #endif |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 270 | |
| 271 | /* |
| 272 | * ...but all of them |
| 273 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 274 | if (ioctl (fd, BIOCPROMISC, (caddr_t) & true) < 0) |
| 275 | { |
| 276 | zlog_warn ("failed to set BPF dev to promiscuous mode"); |
| 277 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 278 | |
| 279 | /* |
| 280 | * If the buffer length is smaller than our mtu, lets try to increase it |
| 281 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 282 | if (blen < circuit->interface->mtu) |
| 283 | { |
| 284 | if (ioctl (fd, BIOCSBLEN, &circuit->interface->mtu) < 0) |
| 285 | { |
| 286 | zlog_warn ("failed to set BPF buffer len (%u to %u)", blen, |
| 287 | circuit->interface->mtu); |
| 288 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 289 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 290 | |
| 291 | /* |
| 292 | * Set a timeout parameter - hope this helps select() |
| 293 | */ |
| 294 | timeout.tv_sec = 600; |
| 295 | timeout.tv_usec = 0; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 296 | if (ioctl (fd, BIOCSRTIMEOUT, (caddr_t) & timeout) < 0) |
| 297 | { |
| 298 | zlog_warn ("failed to set BPF device timeout"); |
| 299 | } |
| 300 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 301 | /* |
| 302 | * And set the filter |
| 303 | */ |
| 304 | memset (&bpf_prog, 0, sizeof (struct bpf_program)); |
| 305 | bpf_prog.bf_len = 8; |
| 306 | bpf_prog.bf_insns = &(llcfilter[0]); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 307 | if (ioctl (fd, BIOCSETF, (caddr_t) & bpf_prog) < 0) |
| 308 | { |
| 309 | zlog_warn ("open_bpf_dev(): failed to install filter: %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 310 | safe_strerror (errno)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 311 | return ISIS_WARNING; |
| 312 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 313 | |
| 314 | assert (fd > 0); |
| 315 | |
| 316 | circuit->fd = fd; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 317 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 318 | return ISIS_OK; |
| 319 | } |
| 320 | |
| 321 | #endif /* GNU_LINUX */ |
| 322 | |
| 323 | /* |
| 324 | * Create the socket and set the tx/rx funcs |
| 325 | */ |
| 326 | int |
| 327 | isis_sock_init (struct isis_circuit *circuit) |
| 328 | { |
| 329 | int retval = ISIS_OK; |
| 330 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 331 | if (isisd_privs.change (ZPRIVS_RAISE)) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 332 | zlog_err ("%s: could not raise privs, %s", __func__, safe_strerror (errno)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 333 | |
| 334 | #ifdef GNU_LINUX |
| 335 | retval = open_packet_socket (circuit); |
| 336 | #else |
| 337 | retval = open_bpf_dev (circuit); |
| 338 | #endif |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 339 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 340 | if (retval != ISIS_OK) |
| 341 | { |
| 342 | zlog_warn ("%s: could not initialize the socket", __func__); |
| 343 | goto end; |
| 344 | } |
| 345 | |
| 346 | if (circuit->circ_type == CIRCUIT_T_BROADCAST) |
| 347 | { |
| 348 | circuit->tx = isis_send_pdu_bcast; |
| 349 | circuit->rx = isis_recv_pdu_bcast; |
| 350 | } |
| 351 | else if (circuit->circ_type == CIRCUIT_T_P2P) |
| 352 | { |
| 353 | circuit->tx = isis_send_pdu_p2p; |
| 354 | circuit->rx = isis_recv_pdu_p2p; |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | zlog_warn ("isis_sock_init(): unknown circuit type"); |
| 359 | retval = ISIS_WARNING; |
| 360 | goto end; |
| 361 | } |
| 362 | |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 363 | end: |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 364 | if (isisd_privs.change (ZPRIVS_LOWER)) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 365 | zlog_err ("%s: could not lower privs, %s", __func__, safe_strerror (errno)); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 366 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 367 | return retval; |
| 368 | } |
| 369 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 370 | static inline int |
| 371 | llc_check (u_char * llc) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 372 | { |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 373 | if (*llc != ISO_SAP || *(llc + 1) != ISO_SAP || *(llc + 2) != 3) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 374 | return 0; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 375 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 376 | return 1; |
| 377 | } |
| 378 | |
| 379 | #ifdef GNU_LINUX |
| 380 | int |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 381 | isis_recv_pdu_bcast (struct isis_circuit *circuit, u_char * ssnpa) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 382 | { |
| 383 | int bytesread, addr_len; |
| 384 | struct sockaddr_ll s_addr; |
| 385 | u_char llc[LLC_LEN]; |
| 386 | |
| 387 | addr_len = sizeof (s_addr); |
| 388 | |
| 389 | memset (&s_addr, 0, sizeof (struct sockaddr_ll)); |
| 390 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 391 | bytesread = recvfrom (circuit->fd, (void *) &llc, |
| 392 | LLC_LEN, MSG_PEEK, |
hasso | f7c43dc | 2004-09-26 16:24:14 +0000 | [diff] [blame] | 393 | (struct sockaddr *) &s_addr, (socklen_t *) &addr_len); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 394 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 395 | if (bytesread < 0) |
| 396 | { |
| 397 | zlog_warn ("isis_recv_packet_bcast(): fd %d, recvfrom (): %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 398 | circuit->fd, safe_strerror (errno)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 399 | zlog_warn ("circuit is %s", circuit->interface->name); |
| 400 | zlog_warn ("circuit fd %d", circuit->fd); |
| 401 | zlog_warn ("bytesread %d", bytesread); |
| 402 | /* get rid of the packet */ |
| 403 | bytesread = read (circuit->fd, discard_buff, sizeof (discard_buff)); |
| 404 | return ISIS_WARNING; |
| 405 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 406 | /* |
| 407 | * Filtering by llc field, discard packets sent by this host (other circuit) |
| 408 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 409 | if (!llc_check (llc) || s_addr.sll_pkttype == PACKET_OUTGOING) |
| 410 | { |
| 411 | /* Read the packet into discard buff */ |
| 412 | bytesread = read (circuit->fd, discard_buff, sizeof (discard_buff)); |
| 413 | if (bytesread < 0) |
| 414 | zlog_warn ("isis_recv_pdu_bcast(): read() failed"); |
| 415 | return ISIS_WARNING; |
| 416 | } |
| 417 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 418 | /* on lan we have to read to the static buff first */ |
| 419 | bytesread = recvfrom (circuit->fd, sock_buff, circuit->interface->mtu, 0, |
hasso | f7c43dc | 2004-09-26 16:24:14 +0000 | [diff] [blame] | 420 | (struct sockaddr *) &s_addr, (socklen_t *) &addr_len); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 421 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 422 | /* then we lose the LLC */ |
paul | 15935e9 | 2005-05-03 09:27:23 +0000 | [diff] [blame] | 423 | stream_write (circuit->rcv_stream, sock_buff + LLC_LEN, bytesread - LLC_LEN); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 424 | |
| 425 | memcpy (ssnpa, &s_addr.sll_addr, s_addr.sll_halen); |
| 426 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 427 | return ISIS_OK; |
| 428 | } |
| 429 | |
| 430 | int |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 431 | isis_recv_pdu_p2p (struct isis_circuit *circuit, u_char * ssnpa) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 432 | { |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 433 | int bytesread, addr_len; |
| 434 | struct sockaddr_ll s_addr; |
| 435 | |
| 436 | memset (&s_addr, 0, sizeof (struct sockaddr_ll)); |
| 437 | addr_len = sizeof (s_addr); |
| 438 | |
| 439 | /* we can read directly to the stream */ |
paul | 15935e9 | 2005-05-03 09:27:23 +0000 | [diff] [blame] | 440 | bytesread = stream_recvfrom (circuit->rcv_stream, circuit->fd, |
| 441 | circuit->interface->mtu, 0, |
| 442 | (struct sockaddr *) &s_addr, |
| 443 | (socklen_t *) &addr_len); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 444 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 445 | if (s_addr.sll_pkttype == PACKET_OUTGOING) |
| 446 | { |
| 447 | /* Read the packet into discard buff */ |
| 448 | bytesread = read (circuit->fd, discard_buff, sizeof (discard_buff)); |
| 449 | if (bytesread < 0) |
| 450 | zlog_warn ("isis_recv_pdu_p2p(): read() failed"); |
| 451 | return ISIS_WARNING; |
| 452 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 453 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 454 | /* If we don't have protocol type 0x00FE which is |
| 455 | * ISO over GRE we exit with pain :) |
| 456 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 457 | if (ntohs (s_addr.sll_protocol) != 0x00FE) |
| 458 | { |
| 459 | zlog_warn ("isis_recv_pdu_p2p(): protocol mismatch(): %X", |
| 460 | ntohs (s_addr.sll_protocol)); |
| 461 | return ISIS_WARNING; |
| 462 | } |
| 463 | |
| 464 | memcpy (ssnpa, &s_addr.sll_addr, s_addr.sll_halen); |
| 465 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 466 | return ISIS_OK; |
| 467 | } |
| 468 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 469 | int |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 470 | isis_send_pdu_bcast (struct isis_circuit *circuit, int level) |
| 471 | { |
| 472 | /* we need to do the LLC in here because of P2P circuits, which will |
| 473 | * not need it |
| 474 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 475 | int written = 1; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 476 | struct sockaddr_ll sa; |
| 477 | |
| 478 | stream_set_getp (circuit->snd_stream, 0); |
| 479 | memset (&sa, 0, sizeof (struct sockaddr_ll)); |
| 480 | sa.sll_family = AF_PACKET; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 481 | sa.sll_protocol = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 482 | sa.sll_ifindex = circuit->interface->ifindex; |
| 483 | sa.sll_halen = ETH_ALEN; |
| 484 | if (level == 1) |
| 485 | memcpy (&sa.sll_addr, ALL_L1_ISS, ETH_ALEN); |
| 486 | else |
| 487 | memcpy (&sa.sll_addr, ALL_L2_ISS, ETH_ALEN); |
| 488 | |
| 489 | /* on a broadcast circuit */ |
| 490 | /* first we put the LLC in */ |
| 491 | sock_buff[0] = 0xFE; |
| 492 | sock_buff[1] = 0xFE; |
| 493 | sock_buff[2] = 0x03; |
| 494 | |
| 495 | /* then we copy the data */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 496 | memcpy (sock_buff + LLC_LEN, circuit->snd_stream->data, |
| 497 | stream_get_endp (circuit->snd_stream)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 498 | |
| 499 | /* now we can send this */ |
| 500 | written = sendto (circuit->fd, sock_buff, |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 501 | stream_get_endp(circuit->snd_stream) + LLC_LEN, 0, |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 502 | (struct sockaddr *) &sa, sizeof (struct sockaddr_ll)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 503 | |
| 504 | return ISIS_OK; |
| 505 | } |
| 506 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 507 | int |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 508 | isis_send_pdu_p2p (struct isis_circuit *circuit, int level) |
| 509 | { |
| 510 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 511 | int written = 1; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 512 | struct sockaddr_ll sa; |
| 513 | |
| 514 | stream_set_getp (circuit->snd_stream, 0); |
| 515 | memset (&sa, 0, sizeof (struct sockaddr_ll)); |
| 516 | sa.sll_family = AF_PACKET; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 517 | sa.sll_protocol = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 518 | sa.sll_ifindex = circuit->interface->ifindex; |
| 519 | sa.sll_halen = ETH_ALEN; |
| 520 | if (level == 1) |
| 521 | memcpy (&sa.sll_addr, ALL_L1_ISS, ETH_ALEN); |
| 522 | else |
| 523 | memcpy (&sa.sll_addr, ALL_L2_ISS, ETH_ALEN); |
| 524 | |
| 525 | |
| 526 | /* lets try correcting the protocol */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 527 | sa.sll_protocol = htons (0x00FE); |
| 528 | written = sendto (circuit->fd, circuit->snd_stream->data, |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 529 | stream_get_endp (circuit->snd_stream), 0, |
| 530 | (struct sockaddr *) &sa, |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 531 | sizeof (struct sockaddr_ll)); |
| 532 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 533 | return ISIS_OK; |
| 534 | } |
| 535 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 536 | #else |
| 537 | |
| 538 | int |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 539 | isis_recv_pdu_bcast (struct isis_circuit *circuit, u_char * ssnpa) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 540 | { |
| 541 | int bytesread = 0, bytestoread, offset, one = 1; |
| 542 | struct bpf_hdr *bpf_hdr; |
| 543 | |
| 544 | assert (circuit->fd > 0); |
| 545 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 546 | if (ioctl (circuit->fd, FIONREAD, (caddr_t) & bytestoread) < 0) |
| 547 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 548 | zlog_warn ("ioctl() FIONREAD failed: %s", safe_strerror (errno)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 549 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 550 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 551 | if (bytestoread) |
| 552 | { |
| 553 | bytesread = read (circuit->fd, readbuff, readblen); |
| 554 | } |
| 555 | if (bytesread < 0) |
| 556 | { |
| 557 | zlog_warn ("isis_recv_pdu_bcast(): read() failed: %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 558 | safe_strerror (errno)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 559 | return ISIS_WARNING; |
| 560 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 561 | |
| 562 | if (bytesread == 0) |
| 563 | return ISIS_WARNING; |
| 564 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 565 | bpf_hdr = (struct bpf_hdr *) readbuff; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 566 | |
| 567 | assert (bpf_hdr->bh_caplen == bpf_hdr->bh_datalen); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 568 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 569 | offset = bpf_hdr->bh_hdrlen + LLC_LEN + ETHER_HDR_LEN; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 570 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 571 | /* then we lose the BPF, LLC and ethernet headers */ |
paul | 15935e9 | 2005-05-03 09:27:23 +0000 | [diff] [blame] | 572 | stream_write (circuit->rcv_stream, readbuff + offset, |
| 573 | bpf_hdr->bh_caplen - LLC_LEN - ETHER_HDR_LEN); |
| 574 | stream_set_getp (circuit->rcv_stream, 0); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 575 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 576 | memcpy (ssnpa, readbuff + bpf_hdr->bh_hdrlen + ETHER_ADDR_LEN, |
| 577 | ETHER_ADDR_LEN); |
| 578 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 579 | if (ioctl (circuit->fd, BIOCFLUSH, &one) < 0) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 580 | zlog_warn ("Flushing failed: %s", safe_strerror (errno)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 581 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 582 | return ISIS_OK; |
| 583 | } |
| 584 | |
| 585 | int |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 586 | isis_recv_pdu_p2p (struct isis_circuit *circuit, u_char * ssnpa) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 587 | { |
| 588 | int bytesread; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 589 | |
paul | 15935e9 | 2005-05-03 09:27:23 +0000 | [diff] [blame] | 590 | bytesread = stream_read (circuit->rcv_stream, circuit->fd, |
| 591 | circuit->interface->mtu); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 592 | |
| 593 | if (bytesread < 0) |
| 594 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 595 | zlog_warn ("isis_recv_pdu_p2p(): read () failed: %s", safe_strerror (errno)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 596 | return ISIS_WARNING; |
| 597 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 598 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 599 | return ISIS_OK; |
| 600 | } |
| 601 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 602 | int |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 603 | isis_send_pdu_bcast (struct isis_circuit *circuit, int level) |
| 604 | { |
| 605 | struct ether_header *eth; |
| 606 | int written; |
| 607 | |
| 608 | stream_set_getp (circuit->snd_stream, 0); |
| 609 | |
| 610 | /* |
| 611 | * First the eth header |
| 612 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 613 | eth = (struct ether_header *) sock_buff; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 614 | if (level == 1) |
| 615 | memcpy (eth->ether_dhost, ALL_L1_ISS, ETHER_ADDR_LEN); |
| 616 | else |
| 617 | memcpy (eth->ether_dhost, ALL_L2_ISS, ETHER_ADDR_LEN); |
| 618 | memcpy (eth->ether_shost, circuit->u.bc.snpa, ETHER_ADDR_LEN); |
| 619 | eth->ether_type = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN); |
| 620 | |
| 621 | /* |
| 622 | * Then the LLC |
| 623 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 624 | sock_buff[ETHER_HDR_LEN] = ISO_SAP; |
| 625 | sock_buff[ETHER_HDR_LEN + 1] = ISO_SAP; |
| 626 | sock_buff[ETHER_HDR_LEN + 2] = 0x03; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 627 | |
| 628 | /* then we copy the data */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 629 | memcpy (sock_buff + (LLC_LEN + ETHER_HDR_LEN), circuit->snd_stream->data, |
| 630 | stream_get_endp (circuit->snd_stream)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 631 | |
| 632 | /* now we can send this */ |
| 633 | written = write (circuit->fd, sock_buff, |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 634 | stream_get_endp (circuit->snd_stream) |
| 635 | + LLC_LEN + ETHER_HDR_LEN); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 636 | |
| 637 | return ISIS_OK; |
| 638 | } |
| 639 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 640 | int |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 641 | isis_send_pdu_p2p (struct isis_circuit *circuit, int level) |
| 642 | { |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 643 | return ISIS_OK; |
| 644 | } |
| 645 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 646 | #endif /* GNU_LINUX */ |