2 * IS-IS Rout(e)ing protocol - isis_bpf.c
4 * Copyright (C) 2001,2002 Sampo Saaristo
5 * Tampere University of Technology
6 * Institute of Communications Engineering
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)
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
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.
24 #if ISIS_METHOD == ISIS_METHOD_BPF
26 #include <netinet/if_ether.h>
28 #include <sys/ioctl.h>
36 #include "isisd/dict.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"
48 extern struct zebra_privs_t isisd_privs
;
50 struct bpf_insn llcfilter
[] = {
51 BPF_STMT (BPF_LD
+ BPF_B
+ BPF_ABS
, ETHER_HDR_LEN
), /* check first byte */
52 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, ISO_SAP
, 0, 5),
53 BPF_STMT (BPF_LD
+ BPF_B
+ BPF_ABS
, ETHER_HDR_LEN
+ 1),
54 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, ISO_SAP
, 0, 3), /* check second byte */
55 BPF_STMT (BPF_LD
+ BPF_B
+ BPF_ABS
, ETHER_HDR_LEN
+ 2),
56 BPF_JUMP (BPF_JMP
+ BPF_JEQ
+ BPF_K
, 0x03, 0, 1), /* check third byte */
57 BPF_STMT (BPF_RET
+ BPF_K
, (u_int
) - 1),
58 BPF_STMT (BPF_RET
+ BPF_K
, 0)
61 u_char
*readbuff
= NULL
;
64 * Table 9 - Architectural constants for use with ISO 8802 subnetworks
68 u_char ALL_L1_ISS
[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x14 };
69 u_char ALL_L2_ISS
[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x15 };
70 u_char ALL_ISS
[6] = { 0x09, 0x00, 0x2B, 0x00, 0x00, 0x05 };
71 u_char ALL_ESS
[6] = { 0x09, 0x00, 0x2B, 0x00, 0x00, 0x04 };
73 static char sock_buff
[8192];
76 open_bpf_dev (struct isis_circuit
*circuit
)
81 u_int blen
, immediate
;
85 struct timeval timeout
;
86 struct bpf_program bpf_prog
;
90 (void) snprintf (bpfdev
, sizeof (bpfdev
), "/dev/bpf%d", i
++);
91 fd
= open (bpfdev
, O_RDWR
);
93 while (fd
< 0 && errno
== EBUSY
);
97 zlog_warn ("open_bpf_dev(): failed to create bpf socket: %s",
98 safe_strerror (errno
));
102 zlog_debug ("Opened BPF device %s", bpfdev
);
104 memcpy (ifr
.ifr_name
, circuit
->interface
->name
, sizeof (ifr
.ifr_name
));
105 if (ioctl (fd
, BIOCSETIF
, (caddr_t
) & ifr
) < 0)
107 zlog_warn ("open_bpf_dev(): failed to bind to interface: %s",
108 safe_strerror (errno
));
112 if (ioctl (fd
, BIOCGBLEN
, (caddr_t
) & blen
) < 0)
114 zlog_warn ("failed to get BPF buffer len");
115 blen
= circuit
->interface
->mtu
;
120 if (readbuff
== NULL
)
121 readbuff
= malloc (blen
);
123 zlog_debug ("BPF buffer len = %u", blen
);
125 /* BPF(4): reads return immediately upon packet reception.
126 * Otherwise, a read will block until either the kernel
127 * buffer becomes full or a timeout occurs.
130 if (ioctl (fd
, BIOCIMMEDIATE
, (caddr_t
) & immediate
) < 0)
132 zlog_warn ("failed to set BPF dev to immediate mode");
137 * We want to see only incoming packets
140 if (ioctl (fd
, BIOCSSEESENT
, (caddr_t
) & seesent
) < 0)
142 zlog_warn ("failed to set BPF dev to incoming only mode");
149 if (ioctl (fd
, BIOCPROMISC
) < 0)
151 zlog_warn ("failed to set BPF dev to promiscuous mode");
155 * If the buffer length is smaller than our mtu, lets try to increase it
157 if (blen
< circuit
->interface
->mtu
)
159 if (ioctl (fd
, BIOCSBLEN
, &circuit
->interface
->mtu
) < 0)
161 zlog_warn ("failed to set BPF buffer len (%u to %u)", blen
,
162 circuit
->interface
->mtu
);
167 * Set a timeout parameter - hope this helps select()
169 timeout
.tv_sec
= 600;
171 if (ioctl (fd
, BIOCSRTIMEOUT
, (caddr_t
) & timeout
) < 0)
173 zlog_warn ("failed to set BPF device timeout");
179 memset (&bpf_prog
, 0, sizeof (struct bpf_program
));
181 bpf_prog
.bf_insns
= &(llcfilter
[0]);
182 if (ioctl (fd
, BIOCSETF
, (caddr_t
) & bpf_prog
) < 0)
184 zlog_warn ("open_bpf_dev(): failed to install filter: %s",
185 safe_strerror (errno
));
197 * Create the socket and set the tx/rx funcs
200 isis_sock_init (struct isis_circuit
*circuit
)
202 int retval
= ISIS_OK
;
204 if (isisd_privs
.change (ZPRIVS_RAISE
))
205 zlog_err ("%s: could not raise privs, %s", __func__
, safe_strerror (errno
));
207 retval
= open_bpf_dev (circuit
);
209 if (retval
!= ISIS_OK
)
211 zlog_warn ("%s: could not initialize the socket", __func__
);
215 if (circuit
->circ_type
== CIRCUIT_T_BROADCAST
)
217 circuit
->tx
= isis_send_pdu_bcast
;
218 circuit
->rx
= isis_recv_pdu_bcast
;
220 else if (circuit
->circ_type
== CIRCUIT_T_P2P
)
222 circuit
->tx
= isis_send_pdu_p2p
;
223 circuit
->rx
= isis_recv_pdu_p2p
;
227 zlog_warn ("isis_sock_init(): unknown circuit type");
228 retval
= ISIS_WARNING
;
233 if (isisd_privs
.change (ZPRIVS_LOWER
))
234 zlog_err ("%s: could not lower privs, %s", __func__
, safe_strerror (errno
));
240 isis_recv_pdu_bcast (struct isis_circuit
*circuit
, u_char
* ssnpa
)
242 int bytesread
= 0, bytestoread
, offset
, one
= 1;
243 struct bpf_hdr
*bpf_hdr
;
245 assert (circuit
->fd
> 0);
247 if (ioctl (circuit
->fd
, FIONREAD
, (caddr_t
) & bytestoread
) < 0)
249 zlog_warn ("ioctl() FIONREAD failed: %s", safe_strerror (errno
));
254 bytesread
= read (circuit
->fd
, readbuff
, readblen
);
258 zlog_warn ("isis_recv_pdu_bcast(): read() failed: %s",
259 safe_strerror (errno
));
266 bpf_hdr
= (struct bpf_hdr
*) readbuff
;
268 assert (bpf_hdr
->bh_caplen
== bpf_hdr
->bh_datalen
);
270 offset
= bpf_hdr
->bh_hdrlen
+ LLC_LEN
+ ETHER_HDR_LEN
;
272 /* then we lose the BPF, LLC and ethernet headers */
273 stream_write (circuit
->rcv_stream
, readbuff
+ offset
,
274 bpf_hdr
->bh_caplen
- LLC_LEN
- ETHER_HDR_LEN
);
275 stream_set_getp (circuit
->rcv_stream
, 0);
277 memcpy (ssnpa
, readbuff
+ bpf_hdr
->bh_hdrlen
+ ETHER_ADDR_LEN
,
280 if (ioctl (circuit
->fd
, BIOCFLUSH
, &one
) < 0)
281 zlog_warn ("Flushing failed: %s", safe_strerror (errno
));
287 isis_recv_pdu_p2p (struct isis_circuit
*circuit
, u_char
* ssnpa
)
291 bytesread
= stream_read (circuit
->rcv_stream
, circuit
->fd
,
292 circuit
->interface
->mtu
);
296 zlog_warn ("isis_recv_pdu_p2p(): read () failed: %s", safe_strerror (errno
));
304 isis_send_pdu_bcast (struct isis_circuit
*circuit
, int level
)
306 struct ether_header
*eth
;
310 buflen
= stream_get_endp (circuit
->snd_stream
) + LLC_LEN
+ ETHER_HDR_LEN
;
311 if (buflen
> sizeof (sock_buff
))
313 zlog_warn ("isis_send_pdu_bcast: sock_buff size %zu is less than "
314 "output pdu size %zu on circuit %s",
315 sizeof (sock_buff
), buflen
, circuit
->interface
->name
);
319 stream_set_getp (circuit
->snd_stream
, 0);
322 * First the eth header
324 eth
= (struct ether_header
*) sock_buff
;
326 memcpy (eth
->ether_dhost
, ALL_L1_ISS
, ETHER_ADDR_LEN
);
328 memcpy (eth
->ether_dhost
, ALL_L2_ISS
, ETHER_ADDR_LEN
);
329 memcpy (eth
->ether_shost
, circuit
->u
.bc
.snpa
, ETHER_ADDR_LEN
);
330 eth
->ether_type
= htons (stream_get_endp (circuit
->snd_stream
) + LLC_LEN
);
335 sock_buff
[ETHER_HDR_LEN
] = ISO_SAP
;
336 sock_buff
[ETHER_HDR_LEN
+ 1] = ISO_SAP
;
337 sock_buff
[ETHER_HDR_LEN
+ 2] = 0x03;
339 /* then we copy the data */
340 memcpy (sock_buff
+ (LLC_LEN
+ ETHER_HDR_LEN
), circuit
->snd_stream
->data
,
341 stream_get_endp (circuit
->snd_stream
));
343 /* now we can send this */
344 written
= write (circuit
->fd
, sock_buff
, buflen
);
347 zlog_warn("IS-IS bpf: could not transmit packet on %s: %s",
348 circuit
->interface
->name
, safe_strerror(errno
));
349 if (ERRNO_IO_RETRY(errno
))
358 isis_send_pdu_p2p (struct isis_circuit
*circuit
, int level
)
363 #endif /* ISIS_METHOD == ISIS_METHOD_BPF */