blob: 56459ecab8bff689b8fdae1de7ef8c0b05fdeb59 [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
jardineb5d44e2003-12-23 08:09:43 +000023#include <zebra.h>
hasso37da8c02004-05-19 11:38:40 +000024#ifdef GNU_LINUX
hassof390d2c2004-09-10 20:48:21 +000025#include <net/ethernet.h> /* the L2 protocols */
hasso37da8c02004-05-19 11:38:40 +000026#else
27#include <net/if.h>
28#include <netinet/if_ether.h>
29#endif
30
jardineb5d44e2003-12-23 08:09:43 +000031#include "log.h"
32#include "stream.h"
33#include "if.h"
34
jardineb5d44e2003-12-23 08:09:43 +000035#include "isisd/dict.h"
36#include "isisd/include-netbsd/iso.h"
37#include "isisd/isis_constants.h"
38#include "isisd/isis_common.h"
39#include "isisd/isis_circuit.h"
40#include "isisd/isis_flags.h"
41#include "isisd/isisd.h"
42#include "isisd/isis_constants.h"
43#include "isisd/isis_circuit.h"
44#include "isisd/isis_network.h"
45
jardin9e867fe2003-12-23 08:56:18 +000046#include "privs.h"
47
48extern struct zebra_privs_t isisd_privs;
49
jardineb5d44e2003-12-23 08:09:43 +000050/*
51 * On linux we can use the packet(7) sockets, in other OSs we have to do with
52 * Berkley Packet Filter (BPF). Please tell me if you can think of a better
53 * way...
54 */
55#ifdef GNU_LINUX
56#include <netpacket/packet.h>
hassof390d2c2004-09-10 20:48:21 +000057#else
jardineb5d44e2003-12-23 08:09:43 +000058#include <sys/time.h>
59#include <sys/ioctl.h>
60#include <net/bpf.h>
61struct bpf_insn llcfilter[] = {
hassof390d2c2004-09-10 20:48:21 +000062 BPF_STMT (BPF_LD + BPF_B + BPF_ABS, ETHER_HDR_LEN), /* check first byte */
63 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ISO_SAP, 0, 5),
64 BPF_STMT (BPF_LD + BPF_B + BPF_ABS, ETHER_HDR_LEN + 1),
65 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, ISO_SAP, 0, 3), /* check second byte */
66 BPF_STMT (BPF_LD + BPF_B + BPF_ABS, ETHER_HDR_LEN + 2),
67 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, 0x03, 0, 1), /* check third byte */
68 BPF_STMT (BPF_RET + BPF_K, (u_int) - 1),
69 BPF_STMT (BPF_RET + BPF_K, 0)
jardineb5d44e2003-12-23 08:09:43 +000070};
71int readblen = 0;
72u_char *readbuff = NULL;
73#endif /* GNU_LINUX */
74
75/*
76 * Table 9 - Architectural constans for use with ISO 8802 subnetworks
77 * ISO 10589 - 8.4.8
78 */
79
hassof390d2c2004-09-10 20:48:21 +000080u_char ALL_L1_ISS[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x14 };
81u_char ALL_L2_ISS[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x15 };
82u_char ALL_ISS[6] = { 0x09, 0x00, 0x2B, 0x00, 0x00, 0x05 };
83u_char ALL_ESS[6] = { 0x09, 0x00, 0x2B, 0x00, 0x00, 0x04 };
jardineb5d44e2003-12-23 08:09:43 +000084
85#ifdef GNU_LINUX
86static char discard_buff[8192];
87#endif
88static char sock_buff[8192];
89
90/*
91 * if level is 0 we are joining p2p multicast
92 * FIXME: and the p2p multicast being ???
93 */
94#ifdef GNU_LINUX
hasso92365882005-01-18 13:53:33 +000095static int
jardineb5d44e2003-12-23 08:09:43 +000096isis_multicast_join (int fd, int registerto, int if_num)
97{
98 struct packet_mreq mreq;
99
hassof390d2c2004-09-10 20:48:21 +0000100 memset (&mreq, 0, sizeof (mreq));
jardineb5d44e2003-12-23 08:09:43 +0000101 mreq.mr_ifindex = if_num;
hassof390d2c2004-09-10 20:48:21 +0000102 if (registerto)
103 {
104 mreq.mr_type = PACKET_MR_MULTICAST;
105 mreq.mr_alen = ETH_ALEN;
106 if (registerto == 1)
107 memcpy (&mreq.mr_address, ALL_L1_ISS, ETH_ALEN);
108 else if (registerto == 2)
109 memcpy (&mreq.mr_address, ALL_L2_ISS, ETH_ALEN);
110 else if (registerto == 3)
111 memcpy (&mreq.mr_address, ALL_ISS, ETH_ALEN);
112 else
113 memcpy (&mreq.mr_address, ALL_ESS, ETH_ALEN);
jardineb5d44e2003-12-23 08:09:43 +0000114
hassof390d2c2004-09-10 20:48:21 +0000115 }
116 else
117 {
118 mreq.mr_type = PACKET_MR_ALLMULTI;
119 }
jardineb5d44e2003-12-23 08:09:43 +0000120#ifdef EXTREME_DEBUG
hasso529d65b2004-12-24 00:14:50 +0000121 zlog_debug ("isis_multicast_join(): fd=%d, reg_to=%d, if_num=%d, "
122 "address = %02x:%02x:%02x:%02x:%02x:%02x",
123 fd, registerto, if_num, mreq.mr_address[0], mreq.mr_address[1],
124 mreq.mr_address[2], mreq.mr_address[3], mreq.mr_address[4],
125 mreq.mr_address[5]);
jardineb5d44e2003-12-23 08:09:43 +0000126#endif /* EXTREME_DEBUG */
hassof390d2c2004-09-10 20:48:21 +0000127 if (setsockopt (fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq,
128 sizeof (struct packet_mreq)))
129 {
ajs6099b3b2004-11-20 02:06:59 +0000130 zlog_warn ("isis_multicast_join(): setsockopt(): %s", safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000131 return ISIS_WARNING;
132 }
133
jardineb5d44e2003-12-23 08:09:43 +0000134 return ISIS_OK;
135}
136
hasso92365882005-01-18 13:53:33 +0000137static int
jardineb5d44e2003-12-23 08:09:43 +0000138open_packet_socket (struct isis_circuit *circuit)
139{
140 struct sockaddr_ll s_addr;
141 int fd, retval = ISIS_OK;
hassof390d2c2004-09-10 20:48:21 +0000142
jardineb5d44e2003-12-23 08:09:43 +0000143 fd = socket (PF_PACKET, SOCK_DGRAM, htons (ETH_P_ALL));
hassof390d2c2004-09-10 20:48:21 +0000144 if (fd < 0)
145 {
146 zlog_warn ("open_packet_socket(): socket() failed %s",
ajs6099b3b2004-11-20 02:06:59 +0000147 safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000148 return ISIS_WARNING;
149 }
jardineb5d44e2003-12-23 08:09:43 +0000150
151 /*
152 * Bind to the physical interface
153 */
hassof390d2c2004-09-10 20:48:21 +0000154 memset (&s_addr, 0, sizeof (struct sockaddr_ll));
jardineb5d44e2003-12-23 08:09:43 +0000155 s_addr.sll_family = AF_PACKET;
156 s_addr.sll_protocol = htons (ETH_P_ALL);
157 s_addr.sll_ifindex = circuit->interface->ifindex;
hassof390d2c2004-09-10 20:48:21 +0000158
159 if (bind (fd, (struct sockaddr *) (&s_addr),
160 sizeof (struct sockaddr_ll)) < 0)
161 {
ajs6099b3b2004-11-20 02:06:59 +0000162 zlog_warn ("open_packet_socket(): bind() failed: %s", safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000163 return ISIS_WARNING;
164 }
165
jardineb5d44e2003-12-23 08:09:43 +0000166 circuit->fd = fd;
167
hassof390d2c2004-09-10 20:48:21 +0000168 if (circuit->circ_type == CIRCUIT_T_BROADCAST)
169 {
170 /*
171 * Join to multicast groups
172 * according to
173 * 8.4.2 - Broadcast subnetwork IIH PDUs
174 * FIXME: is there a case only one will fail??
175 */
176 if (circuit->circuit_is_type & IS_LEVEL_1)
177 {
178 /* joining ALL_L1_ISS */
179 retval = isis_multicast_join (circuit->fd, 1,
180 circuit->interface->ifindex);
181 /* joining ALL_ISS */
182 retval = isis_multicast_join (circuit->fd, 3,
183 circuit->interface->ifindex);
184 }
185 if (circuit->circuit_is_type & IS_LEVEL_2)
186 /* joining ALL_L2_ISS */
187 retval = isis_multicast_join (circuit->fd, 2,
188 circuit->interface->ifindex);
jardineb5d44e2003-12-23 08:09:43 +0000189 }
hassof390d2c2004-09-10 20:48:21 +0000190 else
191 {
192 retval =
193 isis_multicast_join (circuit->fd, 0, circuit->interface->ifindex);
194 }
jardineb5d44e2003-12-23 08:09:43 +0000195
196 return retval;
197}
198
199#else
200
hasso92365882005-01-18 13:53:33 +0000201static int
jardineb5d44e2003-12-23 08:09:43 +0000202open_bpf_dev (struct isis_circuit *circuit)
203{
204 int i = 0, fd;
205 char bpfdev[128];
206 struct ifreq ifr;
207 u_int16_t blen;
208 int true = 1, false = 0;
209 struct timeval timeout;
210 struct bpf_program bpf_prog;
hassof390d2c2004-09-10 20:48:21 +0000211
212 do
213 {
214 (void) snprintf (bpfdev, sizeof (bpfdev), "/dev/bpf%d", i++);
215 fd = open (bpfdev, O_RDWR);
216 }
217 while (fd < 0 && errno == EBUSY);
218
219 if (fd < 0)
220 {
221 zlog_warn ("open_bpf_dev(): failed to create bpf socket: %s",
ajs6099b3b2004-11-20 02:06:59 +0000222 safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000223 return ISIS_WARNING;
224 }
225
hasso529d65b2004-12-24 00:14:50 +0000226 zlog_debug ("Opened BPF device %s", bpfdev);
jardineb5d44e2003-12-23 08:09:43 +0000227
hassof390d2c2004-09-10 20:48:21 +0000228 memcpy (ifr.ifr_name, circuit->interface->name, sizeof (ifr.ifr_name));
229 if (ioctl (fd, BIOCSETIF, (caddr_t) & ifr) < 0)
230 {
231 zlog_warn ("open_bpf_dev(): failed to bind to interface: %s",
ajs6099b3b2004-11-20 02:06:59 +0000232 safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000233 return ISIS_WARNING;
234 }
jardineb5d44e2003-12-23 08:09:43 +0000235
hassof390d2c2004-09-10 20:48:21 +0000236 if (ioctl (fd, BIOCGBLEN, (caddr_t) & blen) < 0)
237 {
238 zlog_warn ("failed to get BPF buffer len");
239 blen = circuit->interface->mtu;
240 }
jardineb5d44e2003-12-23 08:09:43 +0000241
jardineb5d44e2003-12-23 08:09:43 +0000242 readblen = blen;
243
244 if (readbuff == NULL)
245 readbuff = malloc (blen);
hassof390d2c2004-09-10 20:48:21 +0000246
hasso529d65b2004-12-24 00:14:50 +0000247 zlog_debug ("BPF buffer len = %u", blen);
jardineb5d44e2003-12-23 08:09:43 +0000248
249 /* BPF(4): reads return immediately upon packet reception.
250 * Otherwise, a read will block until either the kernel
251 * buffer becomes full or a timeout occurs.
252 */
hassof390d2c2004-09-10 20:48:21 +0000253 if (ioctl (fd, BIOCIMMEDIATE, (caddr_t) & true) < 0)
254 {
255 zlog_warn ("failed to set BPF dev to immediate mode");
256 }
jardineb5d44e2003-12-23 08:09:43 +0000257
hasso37da8c02004-05-19 11:38:40 +0000258#ifdef BIOCSSEESENT
jardineb5d44e2003-12-23 08:09:43 +0000259 /*
260 * We want to see only incoming packets
261 */
hassof390d2c2004-09-10 20:48:21 +0000262 if (ioctl (fd, BIOCSSEESENT, (caddr_t) & false) < 0)
263 {
264 zlog_warn ("failed to set BPF dev to incoming only mode");
265 }
hasso37da8c02004-05-19 11:38:40 +0000266#endif
jardineb5d44e2003-12-23 08:09:43 +0000267
268 /*
269 * ...but all of them
270 */
hassof390d2c2004-09-10 20:48:21 +0000271 if (ioctl (fd, BIOCPROMISC, (caddr_t) & true) < 0)
272 {
273 zlog_warn ("failed to set BPF dev to promiscuous mode");
274 }
jardineb5d44e2003-12-23 08:09:43 +0000275
276 /*
277 * If the buffer length is smaller than our mtu, lets try to increase it
278 */
hassof390d2c2004-09-10 20:48:21 +0000279 if (blen < circuit->interface->mtu)
280 {
281 if (ioctl (fd, BIOCSBLEN, &circuit->interface->mtu) < 0)
282 {
283 zlog_warn ("failed to set BPF buffer len (%u to %u)", blen,
284 circuit->interface->mtu);
285 }
jardineb5d44e2003-12-23 08:09:43 +0000286 }
jardineb5d44e2003-12-23 08:09:43 +0000287
288 /*
289 * Set a timeout parameter - hope this helps select()
290 */
291 timeout.tv_sec = 600;
292 timeout.tv_usec = 0;
hassof390d2c2004-09-10 20:48:21 +0000293 if (ioctl (fd, BIOCSRTIMEOUT, (caddr_t) & timeout) < 0)
294 {
295 zlog_warn ("failed to set BPF device timeout");
296 }
297
jardineb5d44e2003-12-23 08:09:43 +0000298 /*
299 * And set the filter
300 */
301 memset (&bpf_prog, 0, sizeof (struct bpf_program));
302 bpf_prog.bf_len = 8;
303 bpf_prog.bf_insns = &(llcfilter[0]);
hassof390d2c2004-09-10 20:48:21 +0000304 if (ioctl (fd, BIOCSETF, (caddr_t) & bpf_prog) < 0)
305 {
306 zlog_warn ("open_bpf_dev(): failed to install filter: %s",
ajs6099b3b2004-11-20 02:06:59 +0000307 safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000308 return ISIS_WARNING;
309 }
jardineb5d44e2003-12-23 08:09:43 +0000310
311 assert (fd > 0);
312
313 circuit->fd = fd;
hassof390d2c2004-09-10 20:48:21 +0000314
jardineb5d44e2003-12-23 08:09:43 +0000315 return ISIS_OK;
316}
317
318#endif /* GNU_LINUX */
319
320/*
321 * Create the socket and set the tx/rx funcs
322 */
323int
324isis_sock_init (struct isis_circuit *circuit)
325{
326 int retval = ISIS_OK;
327
hassof390d2c2004-09-10 20:48:21 +0000328 if (isisd_privs.change (ZPRIVS_RAISE))
ajs6099b3b2004-11-20 02:06:59 +0000329 zlog_err ("%s: could not raise privs, %s", __func__, safe_strerror (errno));
jardineb5d44e2003-12-23 08:09:43 +0000330
331#ifdef GNU_LINUX
332 retval = open_packet_socket (circuit);
333#else
334 retval = open_bpf_dev (circuit);
335#endif
jardin9e867fe2003-12-23 08:56:18 +0000336
hassof390d2c2004-09-10 20:48:21 +0000337 if (retval != ISIS_OK)
338 {
339 zlog_warn ("%s: could not initialize the socket", __func__);
340 goto end;
341 }
342
343 if (circuit->circ_type == CIRCUIT_T_BROADCAST)
344 {
345 circuit->tx = isis_send_pdu_bcast;
346 circuit->rx = isis_recv_pdu_bcast;
347 }
348 else if (circuit->circ_type == CIRCUIT_T_P2P)
349 {
350 circuit->tx = isis_send_pdu_p2p;
351 circuit->rx = isis_recv_pdu_p2p;
352 }
353 else
354 {
355 zlog_warn ("isis_sock_init(): unknown circuit type");
356 retval = ISIS_WARNING;
357 goto end;
358 }
359
jardin9e867fe2003-12-23 08:56:18 +0000360end:
hassof390d2c2004-09-10 20:48:21 +0000361 if (isisd_privs.change (ZPRIVS_LOWER))
ajs6099b3b2004-11-20 02:06:59 +0000362 zlog_err ("%s: could not lower privs, %s", __func__, safe_strerror (errno));
jardin9e867fe2003-12-23 08:56:18 +0000363
jardineb5d44e2003-12-23 08:09:43 +0000364 return retval;
365}
366
hassof390d2c2004-09-10 20:48:21 +0000367static inline int
368llc_check (u_char * llc)
jardineb5d44e2003-12-23 08:09:43 +0000369{
hassof390d2c2004-09-10 20:48:21 +0000370 if (*llc != ISO_SAP || *(llc + 1) != ISO_SAP || *(llc + 2) != 3)
jardineb5d44e2003-12-23 08:09:43 +0000371 return 0;
hassof390d2c2004-09-10 20:48:21 +0000372
jardineb5d44e2003-12-23 08:09:43 +0000373 return 1;
374}
375
376#ifdef GNU_LINUX
377int
hassof390d2c2004-09-10 20:48:21 +0000378isis_recv_pdu_bcast (struct isis_circuit *circuit, u_char * ssnpa)
jardineb5d44e2003-12-23 08:09:43 +0000379{
380 int bytesread, addr_len;
381 struct sockaddr_ll s_addr;
382 u_char llc[LLC_LEN];
383
384 addr_len = sizeof (s_addr);
385
386 memset (&s_addr, 0, sizeof (struct sockaddr_ll));
387
hassof390d2c2004-09-10 20:48:21 +0000388 bytesread = recvfrom (circuit->fd, (void *) &llc,
389 LLC_LEN, MSG_PEEK,
hassof7c43dc2004-09-26 16:24:14 +0000390 (struct sockaddr *) &s_addr, (socklen_t *) &addr_len);
jardineb5d44e2003-12-23 08:09:43 +0000391
hassof390d2c2004-09-10 20:48:21 +0000392 if (bytesread < 0)
393 {
394 zlog_warn ("isis_recv_packet_bcast(): fd %d, recvfrom (): %s",
ajs6099b3b2004-11-20 02:06:59 +0000395 circuit->fd, safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000396 zlog_warn ("circuit is %s", circuit->interface->name);
397 zlog_warn ("circuit fd %d", circuit->fd);
398 zlog_warn ("bytesread %d", bytesread);
399 /* get rid of the packet */
400 bytesread = read (circuit->fd, discard_buff, sizeof (discard_buff));
401 return ISIS_WARNING;
402 }
jardineb5d44e2003-12-23 08:09:43 +0000403 /*
404 * Filtering by llc field, discard packets sent by this host (other circuit)
405 */
hassof390d2c2004-09-10 20:48:21 +0000406 if (!llc_check (llc) || s_addr.sll_pkttype == PACKET_OUTGOING)
407 {
408 /* Read the packet into discard buff */
409 bytesread = read (circuit->fd, discard_buff, sizeof (discard_buff));
410 if (bytesread < 0)
411 zlog_warn ("isis_recv_pdu_bcast(): read() failed");
412 return ISIS_WARNING;
413 }
414
jardineb5d44e2003-12-23 08:09:43 +0000415 /* on lan we have to read to the static buff first */
416 bytesread = recvfrom (circuit->fd, sock_buff, circuit->interface->mtu, 0,
hassof7c43dc2004-09-26 16:24:14 +0000417 (struct sockaddr *) &s_addr, (socklen_t *) &addr_len);
hassof390d2c2004-09-10 20:48:21 +0000418
jardineb5d44e2003-12-23 08:09:43 +0000419 /* then we lose the LLC */
paul15935e92005-05-03 09:27:23 +0000420 stream_write (circuit->rcv_stream, sock_buff + LLC_LEN, bytesread - LLC_LEN);
hassof390d2c2004-09-10 20:48:21 +0000421
422 memcpy (ssnpa, &s_addr.sll_addr, s_addr.sll_halen);
423
jardineb5d44e2003-12-23 08:09:43 +0000424 return ISIS_OK;
425}
426
427int
hassof390d2c2004-09-10 20:48:21 +0000428isis_recv_pdu_p2p (struct isis_circuit *circuit, u_char * ssnpa)
jardineb5d44e2003-12-23 08:09:43 +0000429{
jardineb5d44e2003-12-23 08:09:43 +0000430 int bytesread, addr_len;
431 struct sockaddr_ll s_addr;
432
433 memset (&s_addr, 0, sizeof (struct sockaddr_ll));
434 addr_len = sizeof (s_addr);
435
436 /* we can read directly to the stream */
paul15935e92005-05-03 09:27:23 +0000437 bytesread = stream_recvfrom (circuit->rcv_stream, circuit->fd,
438 circuit->interface->mtu, 0,
439 (struct sockaddr *) &s_addr,
440 (socklen_t *) &addr_len);
jardineb5d44e2003-12-23 08:09:43 +0000441
hassof390d2c2004-09-10 20:48:21 +0000442 if (s_addr.sll_pkttype == PACKET_OUTGOING)
443 {
444 /* Read the packet into discard buff */
445 bytesread = read (circuit->fd, discard_buff, sizeof (discard_buff));
446 if (bytesread < 0)
447 zlog_warn ("isis_recv_pdu_p2p(): read() failed");
448 return ISIS_WARNING;
449 }
jardineb5d44e2003-12-23 08:09:43 +0000450
jardineb5d44e2003-12-23 08:09:43 +0000451 /* If we don't have protocol type 0x00FE which is
452 * ISO over GRE we exit with pain :)
453 */
hassof390d2c2004-09-10 20:48:21 +0000454 if (ntohs (s_addr.sll_protocol) != 0x00FE)
455 {
456 zlog_warn ("isis_recv_pdu_p2p(): protocol mismatch(): %X",
457 ntohs (s_addr.sll_protocol));
458 return ISIS_WARNING;
459 }
460
461 memcpy (ssnpa, &s_addr.sll_addr, s_addr.sll_halen);
462
jardineb5d44e2003-12-23 08:09:43 +0000463 return ISIS_OK;
464}
465
hassof390d2c2004-09-10 20:48:21 +0000466int
jardineb5d44e2003-12-23 08:09:43 +0000467isis_send_pdu_bcast (struct isis_circuit *circuit, int level)
468{
469 /* we need to do the LLC in here because of P2P circuits, which will
470 * not need it
471 */
hassof390d2c2004-09-10 20:48:21 +0000472 int written = 1;
jardineb5d44e2003-12-23 08:09:43 +0000473 struct sockaddr_ll sa;
474
475 stream_set_getp (circuit->snd_stream, 0);
476 memset (&sa, 0, sizeof (struct sockaddr_ll));
477 sa.sll_family = AF_PACKET;
hassof390d2c2004-09-10 20:48:21 +0000478 sa.sll_protocol = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN);
jardineb5d44e2003-12-23 08:09:43 +0000479 sa.sll_ifindex = circuit->interface->ifindex;
480 sa.sll_halen = ETH_ALEN;
481 if (level == 1)
482 memcpy (&sa.sll_addr, ALL_L1_ISS, ETH_ALEN);
483 else
484 memcpy (&sa.sll_addr, ALL_L2_ISS, ETH_ALEN);
485
486 /* on a broadcast circuit */
487 /* first we put the LLC in */
488 sock_buff[0] = 0xFE;
489 sock_buff[1] = 0xFE;
490 sock_buff[2] = 0x03;
491
492 /* then we copy the data */
hassof390d2c2004-09-10 20:48:21 +0000493 memcpy (sock_buff + LLC_LEN, circuit->snd_stream->data,
494 stream_get_endp (circuit->snd_stream));
jardineb5d44e2003-12-23 08:09:43 +0000495
496 /* now we can send this */
497 written = sendto (circuit->fd, sock_buff,
paul9985f832005-02-09 15:51:56 +0000498 stream_get_endp(circuit->snd_stream) + LLC_LEN, 0,
hassof390d2c2004-09-10 20:48:21 +0000499 (struct sockaddr *) &sa, sizeof (struct sockaddr_ll));
jardineb5d44e2003-12-23 08:09:43 +0000500
501 return ISIS_OK;
502}
503
hassof390d2c2004-09-10 20:48:21 +0000504int
jardineb5d44e2003-12-23 08:09:43 +0000505isis_send_pdu_p2p (struct isis_circuit *circuit, int level)
506{
507
hassof390d2c2004-09-10 20:48:21 +0000508 int written = 1;
jardineb5d44e2003-12-23 08:09:43 +0000509 struct sockaddr_ll sa;
510
511 stream_set_getp (circuit->snd_stream, 0);
512 memset (&sa, 0, sizeof (struct sockaddr_ll));
513 sa.sll_family = AF_PACKET;
hassof390d2c2004-09-10 20:48:21 +0000514 sa.sll_protocol = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN);
jardineb5d44e2003-12-23 08:09:43 +0000515 sa.sll_ifindex = circuit->interface->ifindex;
516 sa.sll_halen = ETH_ALEN;
517 if (level == 1)
518 memcpy (&sa.sll_addr, ALL_L1_ISS, ETH_ALEN);
519 else
520 memcpy (&sa.sll_addr, ALL_L2_ISS, ETH_ALEN);
521
522
523 /* lets try correcting the protocol */
hassof390d2c2004-09-10 20:48:21 +0000524 sa.sll_protocol = htons (0x00FE);
525 written = sendto (circuit->fd, circuit->snd_stream->data,
paul9985f832005-02-09 15:51:56 +0000526 stream_get_endp (circuit->snd_stream), 0,
527 (struct sockaddr *) &sa,
hassof390d2c2004-09-10 20:48:21 +0000528 sizeof (struct sockaddr_ll));
529
jardineb5d44e2003-12-23 08:09:43 +0000530 return ISIS_OK;
531}
532
jardineb5d44e2003-12-23 08:09:43 +0000533#else
534
535int
hassof390d2c2004-09-10 20:48:21 +0000536isis_recv_pdu_bcast (struct isis_circuit *circuit, u_char * ssnpa)
jardineb5d44e2003-12-23 08:09:43 +0000537{
538 int bytesread = 0, bytestoread, offset, one = 1;
539 struct bpf_hdr *bpf_hdr;
540
541 assert (circuit->fd > 0);
542
hassof390d2c2004-09-10 20:48:21 +0000543 if (ioctl (circuit->fd, FIONREAD, (caddr_t) & bytestoread) < 0)
544 {
ajs6099b3b2004-11-20 02:06:59 +0000545 zlog_warn ("ioctl() FIONREAD failed: %s", safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000546 }
jardineb5d44e2003-12-23 08:09:43 +0000547
hassof390d2c2004-09-10 20:48:21 +0000548 if (bytestoread)
549 {
550 bytesread = read (circuit->fd, readbuff, readblen);
551 }
552 if (bytesread < 0)
553 {
554 zlog_warn ("isis_recv_pdu_bcast(): read() failed: %s",
ajs6099b3b2004-11-20 02:06:59 +0000555 safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000556 return ISIS_WARNING;
557 }
jardineb5d44e2003-12-23 08:09:43 +0000558
559 if (bytesread == 0)
560 return ISIS_WARNING;
561
hassof390d2c2004-09-10 20:48:21 +0000562 bpf_hdr = (struct bpf_hdr *) readbuff;
jardineb5d44e2003-12-23 08:09:43 +0000563
564 assert (bpf_hdr->bh_caplen == bpf_hdr->bh_datalen);
hassof390d2c2004-09-10 20:48:21 +0000565
jardineb5d44e2003-12-23 08:09:43 +0000566 offset = bpf_hdr->bh_hdrlen + LLC_LEN + ETHER_HDR_LEN;
hassof390d2c2004-09-10 20:48:21 +0000567
jardineb5d44e2003-12-23 08:09:43 +0000568 /* then we lose the BPF, LLC and ethernet headers */
paul15935e92005-05-03 09:27:23 +0000569 stream_write (circuit->rcv_stream, readbuff + offset,
570 bpf_hdr->bh_caplen - LLC_LEN - ETHER_HDR_LEN);
571 stream_set_getp (circuit->rcv_stream, 0);
jardineb5d44e2003-12-23 08:09:43 +0000572
hassof390d2c2004-09-10 20:48:21 +0000573 memcpy (ssnpa, readbuff + bpf_hdr->bh_hdrlen + ETHER_ADDR_LEN,
574 ETHER_ADDR_LEN);
575
jardineb5d44e2003-12-23 08:09:43 +0000576 if (ioctl (circuit->fd, BIOCFLUSH, &one) < 0)
ajs6099b3b2004-11-20 02:06:59 +0000577 zlog_warn ("Flushing failed: %s", safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000578
jardineb5d44e2003-12-23 08:09:43 +0000579 return ISIS_OK;
580}
581
582int
hassof390d2c2004-09-10 20:48:21 +0000583isis_recv_pdu_p2p (struct isis_circuit *circuit, u_char * ssnpa)
jardineb5d44e2003-12-23 08:09:43 +0000584{
585 int bytesread;
jardineb5d44e2003-12-23 08:09:43 +0000586
paul15935e92005-05-03 09:27:23 +0000587 bytesread = stream_read (circuit->rcv_stream, circuit->fd,
588 circuit->interface->mtu);
hassof390d2c2004-09-10 20:48:21 +0000589
590 if (bytesread < 0)
591 {
ajs6099b3b2004-11-20 02:06:59 +0000592 zlog_warn ("isis_recv_pdu_p2p(): read () failed: %s", safe_strerror (errno));
hassof390d2c2004-09-10 20:48:21 +0000593 return ISIS_WARNING;
594 }
jardineb5d44e2003-12-23 08:09:43 +0000595
jardineb5d44e2003-12-23 08:09:43 +0000596 return ISIS_OK;
597}
598
hassof390d2c2004-09-10 20:48:21 +0000599int
jardineb5d44e2003-12-23 08:09:43 +0000600isis_send_pdu_bcast (struct isis_circuit *circuit, int level)
601{
602 struct ether_header *eth;
603 int written;
604
605 stream_set_getp (circuit->snd_stream, 0);
606
607 /*
608 * First the eth header
609 */
hassof390d2c2004-09-10 20:48:21 +0000610 eth = (struct ether_header *) sock_buff;
jardineb5d44e2003-12-23 08:09:43 +0000611 if (level == 1)
612 memcpy (eth->ether_dhost, ALL_L1_ISS, ETHER_ADDR_LEN);
613 else
614 memcpy (eth->ether_dhost, ALL_L2_ISS, ETHER_ADDR_LEN);
615 memcpy (eth->ether_shost, circuit->u.bc.snpa, ETHER_ADDR_LEN);
616 eth->ether_type = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN);
617
618 /*
619 * Then the LLC
620 */
hassof390d2c2004-09-10 20:48:21 +0000621 sock_buff[ETHER_HDR_LEN] = ISO_SAP;
622 sock_buff[ETHER_HDR_LEN + 1] = ISO_SAP;
623 sock_buff[ETHER_HDR_LEN + 2] = 0x03;
jardineb5d44e2003-12-23 08:09:43 +0000624
625 /* then we copy the data */
hassof390d2c2004-09-10 20:48:21 +0000626 memcpy (sock_buff + (LLC_LEN + ETHER_HDR_LEN), circuit->snd_stream->data,
627 stream_get_endp (circuit->snd_stream));
jardineb5d44e2003-12-23 08:09:43 +0000628
629 /* now we can send this */
630 written = write (circuit->fd, sock_buff,
paul9985f832005-02-09 15:51:56 +0000631 stream_get_endp (circuit->snd_stream)
632 + LLC_LEN + ETHER_HDR_LEN);
jardineb5d44e2003-12-23 08:09:43 +0000633
634 return ISIS_OK;
635}
636
hassof390d2c2004-09-10 20:48:21 +0000637int
jardineb5d44e2003-12-23 08:09:43 +0000638isis_send_pdu_p2p (struct isis_circuit *circuit, int level)
639{
jardineb5d44e2003-12-23 08:09:43 +0000640 return ISIS_OK;
641}
642
jardineb5d44e2003-12-23 08:09:43 +0000643#endif /* GNU_LINUX */