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 |
| 98 | int |
| 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 | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 140 | 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 | |
| 204 | int |
| 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 */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 423 | memcpy (STREAM_DATA (circuit->rcv_stream), |
| 424 | sock_buff + LLC_LEN, bytesread - LLC_LEN); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 425 | circuit->rcv_stream->putp = bytesread - LLC_LEN; |
| 426 | circuit->rcv_stream->endp = bytesread - LLC_LEN; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 427 | |
| 428 | memcpy (ssnpa, &s_addr.sll_addr, s_addr.sll_halen); |
| 429 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 430 | return ISIS_OK; |
| 431 | } |
| 432 | |
| 433 | int |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 434 | isis_recv_pdu_p2p (struct isis_circuit *circuit, u_char * ssnpa) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 435 | { |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 436 | int bytesread, addr_len; |
| 437 | struct sockaddr_ll s_addr; |
| 438 | |
| 439 | memset (&s_addr, 0, sizeof (struct sockaddr_ll)); |
| 440 | addr_len = sizeof (s_addr); |
| 441 | |
| 442 | /* we can read directly to the stream */ |
| 443 | bytesread = recvfrom (circuit->fd, STREAM_DATA (circuit->rcv_stream), |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 444 | circuit->interface->mtu, 0, |
hasso | f7c43dc | 2004-09-26 16:24:14 +0000 | [diff] [blame] | 445 | (struct sockaddr *) &s_addr, (socklen_t *) &addr_len); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 446 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 447 | if (s_addr.sll_pkttype == PACKET_OUTGOING) |
| 448 | { |
| 449 | /* Read the packet into discard buff */ |
| 450 | bytesread = read (circuit->fd, discard_buff, sizeof (discard_buff)); |
| 451 | if (bytesread < 0) |
| 452 | zlog_warn ("isis_recv_pdu_p2p(): read() failed"); |
| 453 | return ISIS_WARNING; |
| 454 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 455 | |
| 456 | circuit->rcv_stream->putp = bytesread; |
| 457 | circuit->rcv_stream->endp = bytesread; |
| 458 | |
| 459 | /* If we don't have protocol type 0x00FE which is |
| 460 | * ISO over GRE we exit with pain :) |
| 461 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 462 | if (ntohs (s_addr.sll_protocol) != 0x00FE) |
| 463 | { |
| 464 | zlog_warn ("isis_recv_pdu_p2p(): protocol mismatch(): %X", |
| 465 | ntohs (s_addr.sll_protocol)); |
| 466 | return ISIS_WARNING; |
| 467 | } |
| 468 | |
| 469 | memcpy (ssnpa, &s_addr.sll_addr, s_addr.sll_halen); |
| 470 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 471 | return ISIS_OK; |
| 472 | } |
| 473 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 474 | int |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 475 | isis_send_pdu_bcast (struct isis_circuit *circuit, int level) |
| 476 | { |
| 477 | /* we need to do the LLC in here because of P2P circuits, which will |
| 478 | * not need it |
| 479 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 480 | int written = 1; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 481 | struct sockaddr_ll sa; |
| 482 | |
| 483 | stream_set_getp (circuit->snd_stream, 0); |
| 484 | memset (&sa, 0, sizeof (struct sockaddr_ll)); |
| 485 | sa.sll_family = AF_PACKET; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 486 | sa.sll_protocol = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 487 | sa.sll_ifindex = circuit->interface->ifindex; |
| 488 | sa.sll_halen = ETH_ALEN; |
| 489 | if (level == 1) |
| 490 | memcpy (&sa.sll_addr, ALL_L1_ISS, ETH_ALEN); |
| 491 | else |
| 492 | memcpy (&sa.sll_addr, ALL_L2_ISS, ETH_ALEN); |
| 493 | |
| 494 | /* on a broadcast circuit */ |
| 495 | /* first we put the LLC in */ |
| 496 | sock_buff[0] = 0xFE; |
| 497 | sock_buff[1] = 0xFE; |
| 498 | sock_buff[2] = 0x03; |
| 499 | |
| 500 | /* then we copy the data */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 501 | memcpy (sock_buff + LLC_LEN, circuit->snd_stream->data, |
| 502 | stream_get_endp (circuit->snd_stream)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 503 | |
| 504 | /* now we can send this */ |
| 505 | written = sendto (circuit->fd, sock_buff, |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 506 | circuit->snd_stream->putp + LLC_LEN, 0, |
| 507 | (struct sockaddr *) &sa, sizeof (struct sockaddr_ll)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 508 | |
| 509 | return ISIS_OK; |
| 510 | } |
| 511 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 512 | int |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 513 | isis_send_pdu_p2p (struct isis_circuit *circuit, int level) |
| 514 | { |
| 515 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 516 | int written = 1; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 517 | struct sockaddr_ll sa; |
| 518 | |
| 519 | stream_set_getp (circuit->snd_stream, 0); |
| 520 | memset (&sa, 0, sizeof (struct sockaddr_ll)); |
| 521 | sa.sll_family = AF_PACKET; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 522 | sa.sll_protocol = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 523 | sa.sll_ifindex = circuit->interface->ifindex; |
| 524 | sa.sll_halen = ETH_ALEN; |
| 525 | if (level == 1) |
| 526 | memcpy (&sa.sll_addr, ALL_L1_ISS, ETH_ALEN); |
| 527 | else |
| 528 | memcpy (&sa.sll_addr, ALL_L2_ISS, ETH_ALEN); |
| 529 | |
| 530 | |
| 531 | /* lets try correcting the protocol */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 532 | sa.sll_protocol = htons (0x00FE); |
| 533 | written = sendto (circuit->fd, circuit->snd_stream->data, |
| 534 | circuit->snd_stream->putp, 0, (struct sockaddr *) &sa, |
| 535 | sizeof (struct sockaddr_ll)); |
| 536 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 537 | return ISIS_OK; |
| 538 | } |
| 539 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 540 | #else |
| 541 | |
| 542 | int |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 543 | isis_recv_pdu_bcast (struct isis_circuit *circuit, u_char * ssnpa) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 544 | { |
| 545 | int bytesread = 0, bytestoread, offset, one = 1; |
| 546 | struct bpf_hdr *bpf_hdr; |
| 547 | |
| 548 | assert (circuit->fd > 0); |
| 549 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 550 | if (ioctl (circuit->fd, FIONREAD, (caddr_t) & bytestoread) < 0) |
| 551 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 552 | zlog_warn ("ioctl() FIONREAD failed: %s", safe_strerror (errno)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 553 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 554 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 555 | if (bytestoread) |
| 556 | { |
| 557 | bytesread = read (circuit->fd, readbuff, readblen); |
| 558 | } |
| 559 | if (bytesread < 0) |
| 560 | { |
| 561 | zlog_warn ("isis_recv_pdu_bcast(): read() failed: %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 562 | safe_strerror (errno)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 563 | return ISIS_WARNING; |
| 564 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 565 | |
| 566 | if (bytesread == 0) |
| 567 | return ISIS_WARNING; |
| 568 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 569 | bpf_hdr = (struct bpf_hdr *) readbuff; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 570 | |
| 571 | assert (bpf_hdr->bh_caplen == bpf_hdr->bh_datalen); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 572 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 573 | offset = bpf_hdr->bh_hdrlen + LLC_LEN + ETHER_HDR_LEN; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 574 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 575 | /* then we lose the BPF, LLC and ethernet headers */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 576 | memcpy (STREAM_DATA (circuit->rcv_stream), |
| 577 | readbuff + offset, bpf_hdr->bh_caplen - LLC_LEN - ETHER_HDR_LEN); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 578 | |
| 579 | circuit->rcv_stream->putp = bpf_hdr->bh_caplen - LLC_LEN - ETHER_HDR_LEN; |
| 580 | circuit->rcv_stream->endp = bpf_hdr->bh_caplen - LLC_LEN - ETHER_HDR_LEN; |
| 581 | circuit->rcv_stream->getp = 0; |
| 582 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 583 | memcpy (ssnpa, readbuff + bpf_hdr->bh_hdrlen + ETHER_ADDR_LEN, |
| 584 | ETHER_ADDR_LEN); |
| 585 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 586 | if (ioctl (circuit->fd, BIOCFLUSH, &one) < 0) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 587 | zlog_warn ("Flushing failed: %s", safe_strerror (errno)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 588 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 589 | return ISIS_OK; |
| 590 | } |
| 591 | |
| 592 | int |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 593 | isis_recv_pdu_p2p (struct isis_circuit *circuit, u_char * ssnpa) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 594 | { |
| 595 | int bytesread; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 596 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 597 | bytesread = read (circuit->fd, STREAM_DATA (circuit->rcv_stream), |
| 598 | circuit->interface->mtu); |
| 599 | |
| 600 | if (bytesread < 0) |
| 601 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 602 | zlog_warn ("isis_recv_pdu_p2p(): read () failed: %s", safe_strerror (errno)); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 603 | return ISIS_WARNING; |
| 604 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 605 | |
| 606 | circuit->rcv_stream->putp = bytesread; |
| 607 | circuit->rcv_stream->endp = bytesread; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 608 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 609 | return ISIS_OK; |
| 610 | } |
| 611 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 612 | int |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 613 | isis_send_pdu_bcast (struct isis_circuit *circuit, int level) |
| 614 | { |
| 615 | struct ether_header *eth; |
| 616 | int written; |
| 617 | |
| 618 | stream_set_getp (circuit->snd_stream, 0); |
| 619 | |
| 620 | /* |
| 621 | * First the eth header |
| 622 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 623 | eth = (struct ether_header *) sock_buff; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 624 | if (level == 1) |
| 625 | memcpy (eth->ether_dhost, ALL_L1_ISS, ETHER_ADDR_LEN); |
| 626 | else |
| 627 | memcpy (eth->ether_dhost, ALL_L2_ISS, ETHER_ADDR_LEN); |
| 628 | memcpy (eth->ether_shost, circuit->u.bc.snpa, ETHER_ADDR_LEN); |
| 629 | eth->ether_type = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN); |
| 630 | |
| 631 | /* |
| 632 | * Then the LLC |
| 633 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 634 | sock_buff[ETHER_HDR_LEN] = ISO_SAP; |
| 635 | sock_buff[ETHER_HDR_LEN + 1] = ISO_SAP; |
| 636 | sock_buff[ETHER_HDR_LEN + 2] = 0x03; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 637 | |
| 638 | /* then we copy the data */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 639 | memcpy (sock_buff + (LLC_LEN + ETHER_HDR_LEN), circuit->snd_stream->data, |
| 640 | stream_get_endp (circuit->snd_stream)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 641 | |
| 642 | /* now we can send this */ |
| 643 | written = write (circuit->fd, sock_buff, |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 644 | circuit->snd_stream->putp + LLC_LEN + ETHER_HDR_LEN); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 645 | |
| 646 | return ISIS_OK; |
| 647 | } |
| 648 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 649 | int |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 650 | isis_send_pdu_p2p (struct isis_circuit *circuit, int level) |
| 651 | { |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 652 | return ISIS_OK; |
| 653 | } |
| 654 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 655 | #endif /* GNU_LINUX */ |