blob: 466a91778b1a18a4b5dab2694ab99c96cdf4cebd [file] [log] [blame]
jardineb5d44e2003-12-23 08:09:43 +00001/*
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>
hasso37da8c02004-05-19 11:38:40 +000027#ifdef GNU_LINUX
hassof390d2c2004-09-10 20:48:21 +000028#include <net/ethernet.h> /* the L2 protocols */
hasso37da8c02004-05-19 11:38:40 +000029#else
30#include <net/if.h>
31#include <netinet/if_ether.h>
32#endif
33
jardineb5d44e2003-12-23 08:09:43 +000034#include "log.h"
35#include "stream.h"
36#include "if.h"
37
jardineb5d44e2003-12-23 08:09:43 +000038#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
jardin9e867fe2003-12-23 08:56:18 +000049#include "privs.h"
50
51extern struct zebra_privs_t isisd_privs;
52
jardineb5d44e2003-12-23 08:09:43 +000053/*
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>
hassof390d2c2004-09-10 20:48:21 +000060#else
jardineb5d44e2003-12-23 08:09:43 +000061#include <sys/time.h>
62#include <sys/ioctl.h>
63#include <net/bpf.h>
64struct bpf_insn llcfilter[] = {
hassof390d2c2004-09-10 20:48:21 +000065 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)
jardineb5d44e2003-12-23 08:09:43 +000073};
74int readblen = 0;
75u_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
hassof390d2c2004-09-10 20:48:21 +000083u_char ALL_L1_ISS[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x14 };
84u_char ALL_L2_ISS[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x15 };
85u_char ALL_ISS[6] = { 0x09, 0x00, 0x2B, 0x00, 0x00, 0x05 };
86u_char ALL_ESS[6] = { 0x09, 0x00, 0x2B, 0x00, 0x00, 0x04 };
jardineb5d44e2003-12-23 08:09:43 +000087
88#ifdef GNU_LINUX
89static char discard_buff[8192];
90#endif
91static 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
hasso92365882005-01-18 13:53:33 +000098static int
jardineb5d44e2003-12-23 08:09:43 +000099isis_multicast_join (int fd, int registerto, int if_num)
100{
101 struct packet_mreq mreq;
102
hassof390d2c2004-09-10 20:48:21 +0000103 memset (&mreq, 0, sizeof (mreq));
jardineb5d44e2003-12-23 08:09:43 +0000104 mreq.mr_ifindex = if_num;
hassof390d2c2004-09-10 20:48:21 +0000105 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);
jardineb5d44e2003-12-23 08:09:43 +0000117
hassof390d2c2004-09-10 20:48:21 +0000118 }
119 else
120 {
121 mreq.mr_type = PACKET_MR_ALLMULTI;
122 }
jardineb5d44e2003-12-23 08:09:43 +0000123#ifdef EXTREME_DEBUG
hasso529d65b2004-12-24 00:14:50 +0000124 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]);
jardineb5d44e2003-12-23 08:09:43 +0000129#endif /* EXTREME_DEBUG */
hassof390d2c2004-09-10 20:48:21 +0000130 if (setsockopt (fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq,
131 sizeof (struct packet_mreq)))
132 {
ajs6099b3b2004-11-20 02:06:59 +0000133 zlog_warn ("isis_multicast_join(): setsockopt(): %s", safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000134 return ISIS_WARNING;
135 }
136
jardineb5d44e2003-12-23 08:09:43 +0000137 return ISIS_OK;
138}
139
hasso92365882005-01-18 13:53:33 +0000140static int
jardineb5d44e2003-12-23 08:09:43 +0000141open_packet_socket (struct isis_circuit *circuit)
142{
143 struct sockaddr_ll s_addr;
144 int fd, retval = ISIS_OK;
hassof390d2c2004-09-10 20:48:21 +0000145
jardineb5d44e2003-12-23 08:09:43 +0000146 fd = socket (PF_PACKET, SOCK_DGRAM, htons (ETH_P_ALL));
hassof390d2c2004-09-10 20:48:21 +0000147 if (fd < 0)
148 {
149 zlog_warn ("open_packet_socket(): socket() failed %s",
ajs6099b3b2004-11-20 02:06:59 +0000150 safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000151 return ISIS_WARNING;
152 }
jardineb5d44e2003-12-23 08:09:43 +0000153
154 /*
155 * Bind to the physical interface
156 */
hassof390d2c2004-09-10 20:48:21 +0000157 memset (&s_addr, 0, sizeof (struct sockaddr_ll));
jardineb5d44e2003-12-23 08:09:43 +0000158 s_addr.sll_family = AF_PACKET;
159 s_addr.sll_protocol = htons (ETH_P_ALL);
160 s_addr.sll_ifindex = circuit->interface->ifindex;
hassof390d2c2004-09-10 20:48:21 +0000161
162 if (bind (fd, (struct sockaddr *) (&s_addr),
163 sizeof (struct sockaddr_ll)) < 0)
164 {
ajs6099b3b2004-11-20 02:06:59 +0000165 zlog_warn ("open_packet_socket(): bind() failed: %s", safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000166 return ISIS_WARNING;
167 }
168
jardineb5d44e2003-12-23 08:09:43 +0000169 circuit->fd = fd;
170
hassof390d2c2004-09-10 20:48:21 +0000171 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);
jardineb5d44e2003-12-23 08:09:43 +0000192 }
hassof390d2c2004-09-10 20:48:21 +0000193 else
194 {
195 retval =
196 isis_multicast_join (circuit->fd, 0, circuit->interface->ifindex);
197 }
jardineb5d44e2003-12-23 08:09:43 +0000198
199 return retval;
200}
201
202#else
203
hasso92365882005-01-18 13:53:33 +0000204static int
jardineb5d44e2003-12-23 08:09:43 +0000205open_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;
hassof390d2c2004-09-10 20:48:21 +0000214
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",
ajs6099b3b2004-11-20 02:06:59 +0000225 safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000226 return ISIS_WARNING;
227 }
228
hasso529d65b2004-12-24 00:14:50 +0000229 zlog_debug ("Opened BPF device %s", bpfdev);
jardineb5d44e2003-12-23 08:09:43 +0000230
hassof390d2c2004-09-10 20:48:21 +0000231 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",
ajs6099b3b2004-11-20 02:06:59 +0000235 safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000236 return ISIS_WARNING;
237 }
jardineb5d44e2003-12-23 08:09:43 +0000238
hassof390d2c2004-09-10 20:48:21 +0000239 if (ioctl (fd, BIOCGBLEN, (caddr_t) & blen) < 0)
240 {
241 zlog_warn ("failed to get BPF buffer len");
242 blen = circuit->interface->mtu;
243 }
jardineb5d44e2003-12-23 08:09:43 +0000244
jardineb5d44e2003-12-23 08:09:43 +0000245 readblen = blen;
246
247 if (readbuff == NULL)
248 readbuff = malloc (blen);
hassof390d2c2004-09-10 20:48:21 +0000249
hasso529d65b2004-12-24 00:14:50 +0000250 zlog_debug ("BPF buffer len = %u", blen);
jardineb5d44e2003-12-23 08:09:43 +0000251
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 */
hassof390d2c2004-09-10 20:48:21 +0000256 if (ioctl (fd, BIOCIMMEDIATE, (caddr_t) & true) < 0)
257 {
258 zlog_warn ("failed to set BPF dev to immediate mode");
259 }
jardineb5d44e2003-12-23 08:09:43 +0000260
hasso37da8c02004-05-19 11:38:40 +0000261#ifdef BIOCSSEESENT
jardineb5d44e2003-12-23 08:09:43 +0000262 /*
263 * We want to see only incoming packets
264 */
hassof390d2c2004-09-10 20:48:21 +0000265 if (ioctl (fd, BIOCSSEESENT, (caddr_t) & false) < 0)
266 {
267 zlog_warn ("failed to set BPF dev to incoming only mode");
268 }
hasso37da8c02004-05-19 11:38:40 +0000269#endif
jardineb5d44e2003-12-23 08:09:43 +0000270
271 /*
272 * ...but all of them
273 */
hassof390d2c2004-09-10 20:48:21 +0000274 if (ioctl (fd, BIOCPROMISC, (caddr_t) & true) < 0)
275 {
276 zlog_warn ("failed to set BPF dev to promiscuous mode");
277 }
jardineb5d44e2003-12-23 08:09:43 +0000278
279 /*
280 * If the buffer length is smaller than our mtu, lets try to increase it
281 */
hassof390d2c2004-09-10 20:48:21 +0000282 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 }
jardineb5d44e2003-12-23 08:09:43 +0000289 }
jardineb5d44e2003-12-23 08:09:43 +0000290
291 /*
292 * Set a timeout parameter - hope this helps select()
293 */
294 timeout.tv_sec = 600;
295 timeout.tv_usec = 0;
hassof390d2c2004-09-10 20:48:21 +0000296 if (ioctl (fd, BIOCSRTIMEOUT, (caddr_t) & timeout) < 0)
297 {
298 zlog_warn ("failed to set BPF device timeout");
299 }
300
jardineb5d44e2003-12-23 08:09:43 +0000301 /*
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]);
hassof390d2c2004-09-10 20:48:21 +0000307 if (ioctl (fd, BIOCSETF, (caddr_t) & bpf_prog) < 0)
308 {
309 zlog_warn ("open_bpf_dev(): failed to install filter: %s",
ajs6099b3b2004-11-20 02:06:59 +0000310 safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000311 return ISIS_WARNING;
312 }
jardineb5d44e2003-12-23 08:09:43 +0000313
314 assert (fd > 0);
315
316 circuit->fd = fd;
hassof390d2c2004-09-10 20:48:21 +0000317
jardineb5d44e2003-12-23 08:09:43 +0000318 return ISIS_OK;
319}
320
321#endif /* GNU_LINUX */
322
323/*
324 * Create the socket and set the tx/rx funcs
325 */
326int
327isis_sock_init (struct isis_circuit *circuit)
328{
329 int retval = ISIS_OK;
330
hassof390d2c2004-09-10 20:48:21 +0000331 if (isisd_privs.change (ZPRIVS_RAISE))
ajs6099b3b2004-11-20 02:06:59 +0000332 zlog_err ("%s: could not raise privs, %s", __func__, safe_strerror (errno));
jardineb5d44e2003-12-23 08:09:43 +0000333
334#ifdef GNU_LINUX
335 retval = open_packet_socket (circuit);
336#else
337 retval = open_bpf_dev (circuit);
338#endif
jardin9e867fe2003-12-23 08:56:18 +0000339
hassof390d2c2004-09-10 20:48:21 +0000340 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
jardin9e867fe2003-12-23 08:56:18 +0000363end:
hassof390d2c2004-09-10 20:48:21 +0000364 if (isisd_privs.change (ZPRIVS_LOWER))
ajs6099b3b2004-11-20 02:06:59 +0000365 zlog_err ("%s: could not lower privs, %s", __func__, safe_strerror (errno));
jardin9e867fe2003-12-23 08:56:18 +0000366
jardineb5d44e2003-12-23 08:09:43 +0000367 return retval;
368}
369
hassof390d2c2004-09-10 20:48:21 +0000370static inline int
371llc_check (u_char * llc)
jardineb5d44e2003-12-23 08:09:43 +0000372{
hassof390d2c2004-09-10 20:48:21 +0000373 if (*llc != ISO_SAP || *(llc + 1) != ISO_SAP || *(llc + 2) != 3)
jardineb5d44e2003-12-23 08:09:43 +0000374 return 0;
hassof390d2c2004-09-10 20:48:21 +0000375
jardineb5d44e2003-12-23 08:09:43 +0000376 return 1;
377}
378
379#ifdef GNU_LINUX
380int
hassof390d2c2004-09-10 20:48:21 +0000381isis_recv_pdu_bcast (struct isis_circuit *circuit, u_char * ssnpa)
jardineb5d44e2003-12-23 08:09:43 +0000382{
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
hassof390d2c2004-09-10 20:48:21 +0000391 bytesread = recvfrom (circuit->fd, (void *) &llc,
392 LLC_LEN, MSG_PEEK,
hassof7c43dc2004-09-26 16:24:14 +0000393 (struct sockaddr *) &s_addr, (socklen_t *) &addr_len);
jardineb5d44e2003-12-23 08:09:43 +0000394
hassof390d2c2004-09-10 20:48:21 +0000395 if (bytesread < 0)
396 {
397 zlog_warn ("isis_recv_packet_bcast(): fd %d, recvfrom (): %s",
ajs6099b3b2004-11-20 02:06:59 +0000398 circuit->fd, safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000399 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 }
jardineb5d44e2003-12-23 08:09:43 +0000406 /*
407 * Filtering by llc field, discard packets sent by this host (other circuit)
408 */
hassof390d2c2004-09-10 20:48:21 +0000409 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
jardineb5d44e2003-12-23 08:09:43 +0000418 /* on lan we have to read to the static buff first */
419 bytesread = recvfrom (circuit->fd, sock_buff, circuit->interface->mtu, 0,
hassof7c43dc2004-09-26 16:24:14 +0000420 (struct sockaddr *) &s_addr, (socklen_t *) &addr_len);
hassof390d2c2004-09-10 20:48:21 +0000421
jardineb5d44e2003-12-23 08:09:43 +0000422 /* then we lose the LLC */
paul15935e92005-05-03 09:27:23 +0000423 stream_write (circuit->rcv_stream, sock_buff + LLC_LEN, bytesread - LLC_LEN);
hassof390d2c2004-09-10 20:48:21 +0000424
425 memcpy (ssnpa, &s_addr.sll_addr, s_addr.sll_halen);
426
jardineb5d44e2003-12-23 08:09:43 +0000427 return ISIS_OK;
428}
429
430int
hassof390d2c2004-09-10 20:48:21 +0000431isis_recv_pdu_p2p (struct isis_circuit *circuit, u_char * ssnpa)
jardineb5d44e2003-12-23 08:09:43 +0000432{
jardineb5d44e2003-12-23 08:09:43 +0000433 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 */
paul15935e92005-05-03 09:27:23 +0000440 bytesread = stream_recvfrom (circuit->rcv_stream, circuit->fd,
441 circuit->interface->mtu, 0,
442 (struct sockaddr *) &s_addr,
443 (socklen_t *) &addr_len);
jardineb5d44e2003-12-23 08:09:43 +0000444
hassof390d2c2004-09-10 20:48:21 +0000445 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 }
jardineb5d44e2003-12-23 08:09:43 +0000453
jardineb5d44e2003-12-23 08:09:43 +0000454 /* If we don't have protocol type 0x00FE which is
455 * ISO over GRE we exit with pain :)
456 */
hassof390d2c2004-09-10 20:48:21 +0000457 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
jardineb5d44e2003-12-23 08:09:43 +0000466 return ISIS_OK;
467}
468
hassof390d2c2004-09-10 20:48:21 +0000469int
jardineb5d44e2003-12-23 08:09:43 +0000470isis_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 */
hassof390d2c2004-09-10 20:48:21 +0000475 int written = 1;
jardineb5d44e2003-12-23 08:09:43 +0000476 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;
hassof390d2c2004-09-10 20:48:21 +0000481 sa.sll_protocol = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN);
jardineb5d44e2003-12-23 08:09:43 +0000482 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 */
hassof390d2c2004-09-10 20:48:21 +0000496 memcpy (sock_buff + LLC_LEN, circuit->snd_stream->data,
497 stream_get_endp (circuit->snd_stream));
jardineb5d44e2003-12-23 08:09:43 +0000498
499 /* now we can send this */
500 written = sendto (circuit->fd, sock_buff,
paul9985f832005-02-09 15:51:56 +0000501 stream_get_endp(circuit->snd_stream) + LLC_LEN, 0,
hassof390d2c2004-09-10 20:48:21 +0000502 (struct sockaddr *) &sa, sizeof (struct sockaddr_ll));
jardineb5d44e2003-12-23 08:09:43 +0000503
504 return ISIS_OK;
505}
506
hassof390d2c2004-09-10 20:48:21 +0000507int
jardineb5d44e2003-12-23 08:09:43 +0000508isis_send_pdu_p2p (struct isis_circuit *circuit, int level)
509{
510
hassof390d2c2004-09-10 20:48:21 +0000511 int written = 1;
jardineb5d44e2003-12-23 08:09:43 +0000512 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;
hassof390d2c2004-09-10 20:48:21 +0000517 sa.sll_protocol = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN);
jardineb5d44e2003-12-23 08:09:43 +0000518 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 */
hassof390d2c2004-09-10 20:48:21 +0000527 sa.sll_protocol = htons (0x00FE);
528 written = sendto (circuit->fd, circuit->snd_stream->data,
paul9985f832005-02-09 15:51:56 +0000529 stream_get_endp (circuit->snd_stream), 0,
530 (struct sockaddr *) &sa,
hassof390d2c2004-09-10 20:48:21 +0000531 sizeof (struct sockaddr_ll));
532
jardineb5d44e2003-12-23 08:09:43 +0000533 return ISIS_OK;
534}
535
jardineb5d44e2003-12-23 08:09:43 +0000536#else
537
538int
hassof390d2c2004-09-10 20:48:21 +0000539isis_recv_pdu_bcast (struct isis_circuit *circuit, u_char * ssnpa)
jardineb5d44e2003-12-23 08:09:43 +0000540{
541 int bytesread = 0, bytestoread, offset, one = 1;
542 struct bpf_hdr *bpf_hdr;
543
544 assert (circuit->fd > 0);
545
hassof390d2c2004-09-10 20:48:21 +0000546 if (ioctl (circuit->fd, FIONREAD, (caddr_t) & bytestoread) < 0)
547 {
ajs6099b3b2004-11-20 02:06:59 +0000548 zlog_warn ("ioctl() FIONREAD failed: %s", safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000549 }
jardineb5d44e2003-12-23 08:09:43 +0000550
hassof390d2c2004-09-10 20:48:21 +0000551 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",
ajs6099b3b2004-11-20 02:06:59 +0000558 safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000559 return ISIS_WARNING;
560 }
jardineb5d44e2003-12-23 08:09:43 +0000561
562 if (bytesread == 0)
563 return ISIS_WARNING;
564
hassof390d2c2004-09-10 20:48:21 +0000565 bpf_hdr = (struct bpf_hdr *) readbuff;
jardineb5d44e2003-12-23 08:09:43 +0000566
567 assert (bpf_hdr->bh_caplen == bpf_hdr->bh_datalen);
hassof390d2c2004-09-10 20:48:21 +0000568
jardineb5d44e2003-12-23 08:09:43 +0000569 offset = bpf_hdr->bh_hdrlen + LLC_LEN + ETHER_HDR_LEN;
hassof390d2c2004-09-10 20:48:21 +0000570
jardineb5d44e2003-12-23 08:09:43 +0000571 /* then we lose the BPF, LLC and ethernet headers */
paul15935e92005-05-03 09:27:23 +0000572 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);
jardineb5d44e2003-12-23 08:09:43 +0000575
hassof390d2c2004-09-10 20:48:21 +0000576 memcpy (ssnpa, readbuff + bpf_hdr->bh_hdrlen + ETHER_ADDR_LEN,
577 ETHER_ADDR_LEN);
578
jardineb5d44e2003-12-23 08:09:43 +0000579 if (ioctl (circuit->fd, BIOCFLUSH, &one) < 0)
ajs6099b3b2004-11-20 02:06:59 +0000580 zlog_warn ("Flushing failed: %s", safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000581
jardineb5d44e2003-12-23 08:09:43 +0000582 return ISIS_OK;
583}
584
585int
hassof390d2c2004-09-10 20:48:21 +0000586isis_recv_pdu_p2p (struct isis_circuit *circuit, u_char * ssnpa)
jardineb5d44e2003-12-23 08:09:43 +0000587{
588 int bytesread;
jardineb5d44e2003-12-23 08:09:43 +0000589
paul15935e92005-05-03 09:27:23 +0000590 bytesread = stream_read (circuit->rcv_stream, circuit->fd,
591 circuit->interface->mtu);
hassof390d2c2004-09-10 20:48:21 +0000592
593 if (bytesread < 0)
594 {
ajs6099b3b2004-11-20 02:06:59 +0000595 zlog_warn ("isis_recv_pdu_p2p(): read () failed: %s", safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000596 return ISIS_WARNING;
597 }
jardineb5d44e2003-12-23 08:09:43 +0000598
jardineb5d44e2003-12-23 08:09:43 +0000599 return ISIS_OK;
600}
601
hassof390d2c2004-09-10 20:48:21 +0000602int
jardineb5d44e2003-12-23 08:09:43 +0000603isis_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 */
hassof390d2c2004-09-10 20:48:21 +0000613 eth = (struct ether_header *) sock_buff;
jardineb5d44e2003-12-23 08:09:43 +0000614 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 */
hassof390d2c2004-09-10 20:48:21 +0000624 sock_buff[ETHER_HDR_LEN] = ISO_SAP;
625 sock_buff[ETHER_HDR_LEN + 1] = ISO_SAP;
626 sock_buff[ETHER_HDR_LEN + 2] = 0x03;
jardineb5d44e2003-12-23 08:09:43 +0000627
628 /* then we copy the data */
hassof390d2c2004-09-10 20:48:21 +0000629 memcpy (sock_buff + (LLC_LEN + ETHER_HDR_LEN), circuit->snd_stream->data,
630 stream_get_endp (circuit->snd_stream));
jardineb5d44e2003-12-23 08:09:43 +0000631
632 /* now we can send this */
633 written = write (circuit->fd, sock_buff,
paul9985f832005-02-09 15:51:56 +0000634 stream_get_endp (circuit->snd_stream)
635 + LLC_LEN + ETHER_HDR_LEN);
jardineb5d44e2003-12-23 08:09:43 +0000636
637 return ISIS_OK;
638}
639
hassof390d2c2004-09-10 20:48:21 +0000640int
jardineb5d44e2003-12-23 08:09:43 +0000641isis_send_pdu_p2p (struct isis_circuit *circuit, int level)
642{
jardineb5d44e2003-12-23 08:09:43 +0000643 return ISIS_OK;
644}
645
jardineb5d44e2003-12-23 08:09:43 +0000646#endif /* GNU_LINUX */