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