blob: 080863bda5c8732d14562107a223ff402752078a [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
98int
99isis_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
124 zlog_info ("isis_multicast_join(): fd=%d, reg_to=%d, if_num=%d, "
hassof390d2c2004-09-10 20:48:21 +0000125 "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 {
133 zlog_warn ("isis_multicast_join(): setsockopt(): %s", strerror (errno));
134 return ISIS_WARNING;
135 }
136
jardineb5d44e2003-12-23 08:09:43 +0000137 return ISIS_OK;
138}
139
hassof390d2c2004-09-10 20:48:21 +0000140int
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",
150 strerror (errno));
151 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 {
165 zlog_warn ("open_packet_socket(): bind() failed: %s", strerror (errno));
166 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
204int
205open_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",
225 strerror (errno));
226 return ISIS_WARNING;
227 }
228
jardineb5d44e2003-12-23 08:09:43 +0000229 zlog_info ("Opened BPF device %s", bpfdev);
230
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",
235 strerror (errno));
236 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
jardineb5d44e2003-12-23 08:09:43 +0000250 zlog_info ("BPF buffer len = %u", blen);
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 */
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",
310 strerror (errno));
311 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))
332 zlog_err ("%s: could not raise privs, %s", __func__, 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))
365 zlog_err ("%s: could not lower privs, %s", __func__, 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,
393 (struct sockaddr *) &s_addr, &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",
398 circuit->fd, strerror (errno));
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 }
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,
hassof390d2c2004-09-10 20:48:21 +0000420 (struct sockaddr *) &s_addr, &addr_len);
421
jardineb5d44e2003-12-23 08:09:43 +0000422 /* then we lose the LLC */
hassof390d2c2004-09-10 20:48:21 +0000423 memcpy (STREAM_DATA (circuit->rcv_stream),
424 sock_buff + LLC_LEN, bytesread - LLC_LEN);
jardineb5d44e2003-12-23 08:09:43 +0000425 circuit->rcv_stream->putp = bytesread - LLC_LEN;
426 circuit->rcv_stream->endp = bytesread - LLC_LEN;
hassof390d2c2004-09-10 20:48:21 +0000427
428 memcpy (ssnpa, &s_addr.sll_addr, s_addr.sll_halen);
429
jardineb5d44e2003-12-23 08:09:43 +0000430 return ISIS_OK;
431}
432
433int
hassof390d2c2004-09-10 20:48:21 +0000434isis_recv_pdu_p2p (struct isis_circuit *circuit, u_char * ssnpa)
jardineb5d44e2003-12-23 08:09:43 +0000435{
jardineb5d44e2003-12-23 08:09:43 +0000436 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),
hassof390d2c2004-09-10 20:48:21 +0000444 circuit->interface->mtu, 0,
445 (struct sockaddr *) &s_addr, &addr_len);
jardineb5d44e2003-12-23 08:09:43 +0000446
hassof390d2c2004-09-10 20:48:21 +0000447 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 }
jardineb5d44e2003-12-23 08:09:43 +0000455
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 */
hassof390d2c2004-09-10 20:48:21 +0000462 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
jardineb5d44e2003-12-23 08:09:43 +0000471 return ISIS_OK;
472}
473
hassof390d2c2004-09-10 20:48:21 +0000474int
jardineb5d44e2003-12-23 08:09:43 +0000475isis_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 */
hassof390d2c2004-09-10 20:48:21 +0000480 int written = 1;
jardineb5d44e2003-12-23 08:09:43 +0000481 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;
hassof390d2c2004-09-10 20:48:21 +0000486 sa.sll_protocol = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN);
jardineb5d44e2003-12-23 08:09:43 +0000487 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 */
hassof390d2c2004-09-10 20:48:21 +0000501 memcpy (sock_buff + LLC_LEN, circuit->snd_stream->data,
502 stream_get_endp (circuit->snd_stream));
jardineb5d44e2003-12-23 08:09:43 +0000503
504 /* now we can send this */
505 written = sendto (circuit->fd, sock_buff,
hassof390d2c2004-09-10 20:48:21 +0000506 circuit->snd_stream->putp + LLC_LEN, 0,
507 (struct sockaddr *) &sa, sizeof (struct sockaddr_ll));
jardineb5d44e2003-12-23 08:09:43 +0000508
509 return ISIS_OK;
510}
511
hassof390d2c2004-09-10 20:48:21 +0000512int
jardineb5d44e2003-12-23 08:09:43 +0000513isis_send_pdu_p2p (struct isis_circuit *circuit, int level)
514{
515
hassof390d2c2004-09-10 20:48:21 +0000516 int written = 1;
jardineb5d44e2003-12-23 08:09:43 +0000517 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;
hassof390d2c2004-09-10 20:48:21 +0000522 sa.sll_protocol = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN);
jardineb5d44e2003-12-23 08:09:43 +0000523 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 */
hassof390d2c2004-09-10 20:48:21 +0000532 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
jardineb5d44e2003-12-23 08:09:43 +0000537 return ISIS_OK;
538}
539
jardineb5d44e2003-12-23 08:09:43 +0000540#else
541
542int
hassof390d2c2004-09-10 20:48:21 +0000543isis_recv_pdu_bcast (struct isis_circuit *circuit, u_char * ssnpa)
jardineb5d44e2003-12-23 08:09:43 +0000544{
545 int bytesread = 0, bytestoread, offset, one = 1;
546 struct bpf_hdr *bpf_hdr;
547
548 assert (circuit->fd > 0);
549
hassof390d2c2004-09-10 20:48:21 +0000550 if (ioctl (circuit->fd, FIONREAD, (caddr_t) & bytestoread) < 0)
551 {
552 zlog_warn ("ioctl() FIONREAD failed: %s", strerror (errno));
553 }
jardineb5d44e2003-12-23 08:09:43 +0000554
hassof390d2c2004-09-10 20:48:21 +0000555 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",
562 strerror (errno));
563 return ISIS_WARNING;
564 }
jardineb5d44e2003-12-23 08:09:43 +0000565
566 if (bytesread == 0)
567 return ISIS_WARNING;
568
hassof390d2c2004-09-10 20:48:21 +0000569 bpf_hdr = (struct bpf_hdr *) readbuff;
jardineb5d44e2003-12-23 08:09:43 +0000570
571 assert (bpf_hdr->bh_caplen == bpf_hdr->bh_datalen);
hassof390d2c2004-09-10 20:48:21 +0000572
jardineb5d44e2003-12-23 08:09:43 +0000573 offset = bpf_hdr->bh_hdrlen + LLC_LEN + ETHER_HDR_LEN;
hassof390d2c2004-09-10 20:48:21 +0000574
jardineb5d44e2003-12-23 08:09:43 +0000575 /* then we lose the BPF, LLC and ethernet headers */
hassof390d2c2004-09-10 20:48:21 +0000576 memcpy (STREAM_DATA (circuit->rcv_stream),
577 readbuff + offset, bpf_hdr->bh_caplen - LLC_LEN - ETHER_HDR_LEN);
jardineb5d44e2003-12-23 08:09:43 +0000578
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
hassof390d2c2004-09-10 20:48:21 +0000583 memcpy (ssnpa, readbuff + bpf_hdr->bh_hdrlen + ETHER_ADDR_LEN,
584 ETHER_ADDR_LEN);
585
jardineb5d44e2003-12-23 08:09:43 +0000586 if (ioctl (circuit->fd, BIOCFLUSH, &one) < 0)
587 zlog_warn ("Flushing failed: %s", strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000588
jardineb5d44e2003-12-23 08:09:43 +0000589 return ISIS_OK;
590}
591
592int
hassof390d2c2004-09-10 20:48:21 +0000593isis_recv_pdu_p2p (struct isis_circuit *circuit, u_char * ssnpa)
jardineb5d44e2003-12-23 08:09:43 +0000594{
595 int bytesread;
jardineb5d44e2003-12-23 08:09:43 +0000596
hassof390d2c2004-09-10 20:48:21 +0000597 bytesread = read (circuit->fd, STREAM_DATA (circuit->rcv_stream),
598 circuit->interface->mtu);
599
600 if (bytesread < 0)
601 {
602 zlog_warn ("isis_recv_pdu_p2p(): read () failed: %s", strerror (errno));
603 return ISIS_WARNING;
604 }
jardineb5d44e2003-12-23 08:09:43 +0000605
606 circuit->rcv_stream->putp = bytesread;
607 circuit->rcv_stream->endp = bytesread;
hassof390d2c2004-09-10 20:48:21 +0000608
jardineb5d44e2003-12-23 08:09:43 +0000609 return ISIS_OK;
610}
611
hassof390d2c2004-09-10 20:48:21 +0000612int
jardineb5d44e2003-12-23 08:09:43 +0000613isis_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 */
hassof390d2c2004-09-10 20:48:21 +0000623 eth = (struct ether_header *) sock_buff;
jardineb5d44e2003-12-23 08:09:43 +0000624 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 */
hassof390d2c2004-09-10 20:48:21 +0000634 sock_buff[ETHER_HDR_LEN] = ISO_SAP;
635 sock_buff[ETHER_HDR_LEN + 1] = ISO_SAP;
636 sock_buff[ETHER_HDR_LEN + 2] = 0x03;
jardineb5d44e2003-12-23 08:09:43 +0000637
638 /* then we copy the data */
hassof390d2c2004-09-10 20:48:21 +0000639 memcpy (sock_buff + (LLC_LEN + ETHER_HDR_LEN), circuit->snd_stream->data,
640 stream_get_endp (circuit->snd_stream));
jardineb5d44e2003-12-23 08:09:43 +0000641
642 /* now we can send this */
643 written = write (circuit->fd, sock_buff,
hassof390d2c2004-09-10 20:48:21 +0000644 circuit->snd_stream->putp + LLC_LEN + ETHER_HDR_LEN);
jardineb5d44e2003-12-23 08:09:43 +0000645
646 return ISIS_OK;
647}
648
hassof390d2c2004-09-10 20:48:21 +0000649int
jardineb5d44e2003-12-23 08:09:43 +0000650isis_send_pdu_p2p (struct isis_circuit *circuit, int level)
651{
jardineb5d44e2003-12-23 08:09:43 +0000652 return ISIS_OK;
653}
654
jardineb5d44e2003-12-23 08:09:43 +0000655#endif /* GNU_LINUX */