blob: 2e901f1a3fc8b416ffabc4f313a5b027d53cfbd7 [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 */
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->endp = bytesread - LLC_LEN;
hassof390d2c2004-09-10 20:48:21 +0000426
427 memcpy (ssnpa, &s_addr.sll_addr, s_addr.sll_halen);
428
jardineb5d44e2003-12-23 08:09:43 +0000429 return ISIS_OK;
430}
431
432int
hassof390d2c2004-09-10 20:48:21 +0000433isis_recv_pdu_p2p (struct isis_circuit *circuit, u_char * ssnpa)
jardineb5d44e2003-12-23 08:09:43 +0000434{
jardineb5d44e2003-12-23 08:09:43 +0000435 int bytesread, addr_len;
436 struct sockaddr_ll s_addr;
437
438 memset (&s_addr, 0, sizeof (struct sockaddr_ll));
439 addr_len = sizeof (s_addr);
440
441 /* we can read directly to the stream */
442 bytesread = recvfrom (circuit->fd, STREAM_DATA (circuit->rcv_stream),
hassof390d2c2004-09-10 20:48:21 +0000443 circuit->interface->mtu, 0,
hassof7c43dc2004-09-26 16:24:14 +0000444 (struct sockaddr *) &s_addr, (socklen_t *) &addr_len);
jardineb5d44e2003-12-23 08:09:43 +0000445
hassof390d2c2004-09-10 20:48:21 +0000446 if (s_addr.sll_pkttype == PACKET_OUTGOING)
447 {
448 /* Read the packet into discard buff */
449 bytesread = read (circuit->fd, discard_buff, sizeof (discard_buff));
450 if (bytesread < 0)
451 zlog_warn ("isis_recv_pdu_p2p(): read() failed");
452 return ISIS_WARNING;
453 }
jardineb5d44e2003-12-23 08:09:43 +0000454
jardineb5d44e2003-12-23 08:09:43 +0000455 circuit->rcv_stream->endp = bytesread;
456
457 /* If we don't have protocol type 0x00FE which is
458 * ISO over GRE we exit with pain :)
459 */
hassof390d2c2004-09-10 20:48:21 +0000460 if (ntohs (s_addr.sll_protocol) != 0x00FE)
461 {
462 zlog_warn ("isis_recv_pdu_p2p(): protocol mismatch(): %X",
463 ntohs (s_addr.sll_protocol));
464 return ISIS_WARNING;
465 }
466
467 memcpy (ssnpa, &s_addr.sll_addr, s_addr.sll_halen);
468
jardineb5d44e2003-12-23 08:09:43 +0000469 return ISIS_OK;
470}
471
hassof390d2c2004-09-10 20:48:21 +0000472int
jardineb5d44e2003-12-23 08:09:43 +0000473isis_send_pdu_bcast (struct isis_circuit *circuit, int level)
474{
475 /* we need to do the LLC in here because of P2P circuits, which will
476 * not need it
477 */
hassof390d2c2004-09-10 20:48:21 +0000478 int written = 1;
jardineb5d44e2003-12-23 08:09:43 +0000479 struct sockaddr_ll sa;
480
481 stream_set_getp (circuit->snd_stream, 0);
482 memset (&sa, 0, sizeof (struct sockaddr_ll));
483 sa.sll_family = AF_PACKET;
hassof390d2c2004-09-10 20:48:21 +0000484 sa.sll_protocol = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN);
jardineb5d44e2003-12-23 08:09:43 +0000485 sa.sll_ifindex = circuit->interface->ifindex;
486 sa.sll_halen = ETH_ALEN;
487 if (level == 1)
488 memcpy (&sa.sll_addr, ALL_L1_ISS, ETH_ALEN);
489 else
490 memcpy (&sa.sll_addr, ALL_L2_ISS, ETH_ALEN);
491
492 /* on a broadcast circuit */
493 /* first we put the LLC in */
494 sock_buff[0] = 0xFE;
495 sock_buff[1] = 0xFE;
496 sock_buff[2] = 0x03;
497
498 /* then we copy the data */
hassof390d2c2004-09-10 20:48:21 +0000499 memcpy (sock_buff + LLC_LEN, circuit->snd_stream->data,
500 stream_get_endp (circuit->snd_stream));
jardineb5d44e2003-12-23 08:09:43 +0000501
502 /* now we can send this */
503 written = sendto (circuit->fd, sock_buff,
paul9985f832005-02-09 15:51:56 +0000504 stream_get_endp(circuit->snd_stream) + LLC_LEN, 0,
hassof390d2c2004-09-10 20:48:21 +0000505 (struct sockaddr *) &sa, sizeof (struct sockaddr_ll));
jardineb5d44e2003-12-23 08:09:43 +0000506
507 return ISIS_OK;
508}
509
hassof390d2c2004-09-10 20:48:21 +0000510int
jardineb5d44e2003-12-23 08:09:43 +0000511isis_send_pdu_p2p (struct isis_circuit *circuit, int level)
512{
513
hassof390d2c2004-09-10 20:48:21 +0000514 int written = 1;
jardineb5d44e2003-12-23 08:09:43 +0000515 struct sockaddr_ll sa;
516
517 stream_set_getp (circuit->snd_stream, 0);
518 memset (&sa, 0, sizeof (struct sockaddr_ll));
519 sa.sll_family = AF_PACKET;
hassof390d2c2004-09-10 20:48:21 +0000520 sa.sll_protocol = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN);
jardineb5d44e2003-12-23 08:09:43 +0000521 sa.sll_ifindex = circuit->interface->ifindex;
522 sa.sll_halen = ETH_ALEN;
523 if (level == 1)
524 memcpy (&sa.sll_addr, ALL_L1_ISS, ETH_ALEN);
525 else
526 memcpy (&sa.sll_addr, ALL_L2_ISS, ETH_ALEN);
527
528
529 /* lets try correcting the protocol */
hassof390d2c2004-09-10 20:48:21 +0000530 sa.sll_protocol = htons (0x00FE);
531 written = sendto (circuit->fd, circuit->snd_stream->data,
paul9985f832005-02-09 15:51:56 +0000532 stream_get_endp (circuit->snd_stream), 0,
533 (struct sockaddr *) &sa,
hassof390d2c2004-09-10 20:48:21 +0000534 sizeof (struct sockaddr_ll));
535
jardineb5d44e2003-12-23 08:09:43 +0000536 return ISIS_OK;
537}
538
jardineb5d44e2003-12-23 08:09:43 +0000539#else
540
541int
hassof390d2c2004-09-10 20:48:21 +0000542isis_recv_pdu_bcast (struct isis_circuit *circuit, u_char * ssnpa)
jardineb5d44e2003-12-23 08:09:43 +0000543{
544 int bytesread = 0, bytestoread, offset, one = 1;
545 struct bpf_hdr *bpf_hdr;
546
547 assert (circuit->fd > 0);
548
hassof390d2c2004-09-10 20:48:21 +0000549 if (ioctl (circuit->fd, FIONREAD, (caddr_t) & bytestoread) < 0)
550 {
ajs6099b3b2004-11-20 02:06:59 +0000551 zlog_warn ("ioctl() FIONREAD failed: %s", safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000552 }
jardineb5d44e2003-12-23 08:09:43 +0000553
hassof390d2c2004-09-10 20:48:21 +0000554 if (bytestoread)
555 {
556 bytesread = read (circuit->fd, readbuff, readblen);
557 }
558 if (bytesread < 0)
559 {
560 zlog_warn ("isis_recv_pdu_bcast(): read() failed: %s",
ajs6099b3b2004-11-20 02:06:59 +0000561 safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000562 return ISIS_WARNING;
563 }
jardineb5d44e2003-12-23 08:09:43 +0000564
565 if (bytesread == 0)
566 return ISIS_WARNING;
567
hassof390d2c2004-09-10 20:48:21 +0000568 bpf_hdr = (struct bpf_hdr *) readbuff;
jardineb5d44e2003-12-23 08:09:43 +0000569
570 assert (bpf_hdr->bh_caplen == bpf_hdr->bh_datalen);
hassof390d2c2004-09-10 20:48:21 +0000571
jardineb5d44e2003-12-23 08:09:43 +0000572 offset = bpf_hdr->bh_hdrlen + LLC_LEN + ETHER_HDR_LEN;
hassof390d2c2004-09-10 20:48:21 +0000573
jardineb5d44e2003-12-23 08:09:43 +0000574 /* then we lose the BPF, LLC and ethernet headers */
hassof390d2c2004-09-10 20:48:21 +0000575 memcpy (STREAM_DATA (circuit->rcv_stream),
576 readbuff + offset, bpf_hdr->bh_caplen - LLC_LEN - ETHER_HDR_LEN);
jardineb5d44e2003-12-23 08:09:43 +0000577
jardineb5d44e2003-12-23 08:09:43 +0000578 circuit->rcv_stream->endp = bpf_hdr->bh_caplen - LLC_LEN - ETHER_HDR_LEN;
579 circuit->rcv_stream->getp = 0;
580
hassof390d2c2004-09-10 20:48:21 +0000581 memcpy (ssnpa, readbuff + bpf_hdr->bh_hdrlen + ETHER_ADDR_LEN,
582 ETHER_ADDR_LEN);
583
jardineb5d44e2003-12-23 08:09:43 +0000584 if (ioctl (circuit->fd, BIOCFLUSH, &one) < 0)
ajs6099b3b2004-11-20 02:06:59 +0000585 zlog_warn ("Flushing failed: %s", safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000586
jardineb5d44e2003-12-23 08:09:43 +0000587 return ISIS_OK;
588}
589
590int
hassof390d2c2004-09-10 20:48:21 +0000591isis_recv_pdu_p2p (struct isis_circuit *circuit, u_char * ssnpa)
jardineb5d44e2003-12-23 08:09:43 +0000592{
593 int bytesread;
jardineb5d44e2003-12-23 08:09:43 +0000594
hassof390d2c2004-09-10 20:48:21 +0000595 bytesread = read (circuit->fd, STREAM_DATA (circuit->rcv_stream),
596 circuit->interface->mtu);
597
598 if (bytesread < 0)
599 {
ajs6099b3b2004-11-20 02:06:59 +0000600 zlog_warn ("isis_recv_pdu_p2p(): read () failed: %s", safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000601 return ISIS_WARNING;
602 }
jardineb5d44e2003-12-23 08:09:43 +0000603
jardineb5d44e2003-12-23 08:09:43 +0000604 circuit->rcv_stream->endp = bytesread;
hassof390d2c2004-09-10 20:48:21 +0000605
jardineb5d44e2003-12-23 08:09:43 +0000606 return ISIS_OK;
607}
608
hassof390d2c2004-09-10 20:48:21 +0000609int
jardineb5d44e2003-12-23 08:09:43 +0000610isis_send_pdu_bcast (struct isis_circuit *circuit, int level)
611{
612 struct ether_header *eth;
613 int written;
614
615 stream_set_getp (circuit->snd_stream, 0);
616
617 /*
618 * First the eth header
619 */
hassof390d2c2004-09-10 20:48:21 +0000620 eth = (struct ether_header *) sock_buff;
jardineb5d44e2003-12-23 08:09:43 +0000621 if (level == 1)
622 memcpy (eth->ether_dhost, ALL_L1_ISS, ETHER_ADDR_LEN);
623 else
624 memcpy (eth->ether_dhost, ALL_L2_ISS, ETHER_ADDR_LEN);
625 memcpy (eth->ether_shost, circuit->u.bc.snpa, ETHER_ADDR_LEN);
626 eth->ether_type = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN);
627
628 /*
629 * Then the LLC
630 */
hassof390d2c2004-09-10 20:48:21 +0000631 sock_buff[ETHER_HDR_LEN] = ISO_SAP;
632 sock_buff[ETHER_HDR_LEN + 1] = ISO_SAP;
633 sock_buff[ETHER_HDR_LEN + 2] = 0x03;
jardineb5d44e2003-12-23 08:09:43 +0000634
635 /* then we copy the data */
hassof390d2c2004-09-10 20:48:21 +0000636 memcpy (sock_buff + (LLC_LEN + ETHER_HDR_LEN), circuit->snd_stream->data,
637 stream_get_endp (circuit->snd_stream));
jardineb5d44e2003-12-23 08:09:43 +0000638
639 /* now we can send this */
640 written = write (circuit->fd, sock_buff,
paul9985f832005-02-09 15:51:56 +0000641 stream_get_endp (circuit->snd_stream)
642 + LLC_LEN + ETHER_HDR_LEN);
jardineb5d44e2003-12-23 08:09:43 +0000643
644 return ISIS_OK;
645}
646
hassof390d2c2004-09-10 20:48:21 +0000647int
jardineb5d44e2003-12-23 08:09:43 +0000648isis_send_pdu_p2p (struct isis_circuit *circuit, int level)
649{
jardineb5d44e2003-12-23 08:09:43 +0000650 return ISIS_OK;
651}
652
jardineb5d44e2003-12-23 08:09:43 +0000653#endif /* GNU_LINUX */