]>
git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_bpf.c
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; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #if ISIS_METHOD == ISIS_METHOD_BPF
26 #include <netinet/if_ether.h>
28 #include <sys/ioctl.h>
35 #include "lib_errors.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 #include "isisd/isis_pdu.h"
49 struct bpf_insn llcfilter
[] = {
50 BPF_STMT(BPF_LD
+ BPF_B
+ BPF_ABS
,
51 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,
55 3), /* check second byte */
56 BPF_STMT(BPF_LD
+ BPF_B
+ BPF_ABS
, ETHER_HDR_LEN
+ 2),
57 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, 0x03, 0, 1), /* check third byte */
58 BPF_STMT(BPF_RET
+ BPF_K
, (unsigned int)-1),
59 BPF_STMT(BPF_RET
+ BPF_K
, 0)};
60 unsigned int readblen
= 0;
61 uint8_t *readbuff
= NULL
;
64 * Table 9 - Architectural constants for use with ISO 8802 subnetworks
68 uint8_t ALL_L1_ISS
[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x14};
69 uint8_t ALL_L2_ISS
[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x15};
70 uint8_t ALL_ISS
[6] = {0x09, 0x00, 0x2B, 0x00, 0x00, 0x05};
71 uint8_t ALL_ESS
[6] = {0x09, 0x00, 0x2B, 0x00, 0x00, 0x04};
73 static char sock_buff
[8192];
75 static int open_bpf_dev(struct isis_circuit
*circuit
)
80 unsigned int blen
, immediate
;
84 struct timeval timeout
;
85 struct bpf_program bpf_prog
;
88 (void)snprintf(bpfdev
, sizeof(bpfdev
), "/dev/bpf%d", i
++);
89 fd
= open(bpfdev
, O_RDWR
);
90 } while (fd
< 0 && errno
== EBUSY
);
93 zlog_warn("open_bpf_dev(): failed to create bpf socket: %s",
94 safe_strerror(errno
));
98 zlog_debug("Opened BPF device %s", bpfdev
);
100 memcpy(ifr
.ifr_name
, circuit
->interface
->name
, sizeof(ifr
.ifr_name
));
101 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
102 zlog_warn("open_bpf_dev(): failed to bind to interface: %s",
103 safe_strerror(errno
));
107 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&blen
) < 0) {
108 zlog_warn("failed to get BPF buffer len");
109 blen
= circuit
->interface
->mtu
;
114 if (readbuff
== NULL
)
115 readbuff
= malloc(blen
);
117 zlog_debug("BPF buffer len = %u", blen
);
119 /* BPF(4): reads return immediately upon packet reception.
120 * Otherwise, a read will block until either the kernel
121 * buffer becomes full or a timeout occurs.
124 if (ioctl(fd
, BIOCIMMEDIATE
, (caddr_t
)&immediate
) < 0) {
125 zlog_warn("failed to set BPF dev to immediate mode");
130 * We want to see only incoming packets
133 if (ioctl(fd
, BIOCSSEESENT
, (caddr_t
)&seesent
) < 0) {
134 zlog_warn("failed to set BPF dev to incoming only mode");
141 if (ioctl(fd
, BIOCPROMISC
) < 0) {
142 zlog_warn("failed to set BPF dev to promiscuous mode");
146 * If the buffer length is smaller than our mtu, lets try to increase it
148 if (blen
< circuit
->interface
->mtu
) {
149 if (ioctl(fd
, BIOCSBLEN
, &circuit
->interface
->mtu
) < 0) {
150 zlog_warn("failed to set BPF buffer len (%u to %u)",
151 blen
, circuit
->interface
->mtu
);
156 * Set a timeout parameter - hope this helps select()
158 timeout
.tv_sec
= 600;
160 if (ioctl(fd
, BIOCSRTIMEOUT
, (caddr_t
)&timeout
) < 0) {
161 zlog_warn("failed to set BPF device timeout");
167 memset(&bpf_prog
, 0, sizeof(struct bpf_program
));
169 bpf_prog
.bf_insns
= &(llcfilter
[0]);
170 if (ioctl(fd
, BIOCSETF
, (caddr_t
)&bpf_prog
) < 0) {
171 zlog_warn("open_bpf_dev(): failed to install filter: %s",
172 safe_strerror(errno
));
184 * Create the socket and set the tx/rx funcs
186 int isis_sock_init(struct isis_circuit
*circuit
)
188 int retval
= ISIS_OK
;
190 frr_with_privs(&isisd_privs
) {
192 retval
= open_bpf_dev(circuit
);
194 if (retval
!= ISIS_OK
) {
195 zlog_warn("%s: could not initialize the socket",
200 if (if_is_broadcast(circuit
->interface
)) {
201 circuit
->tx
= isis_send_pdu_bcast
;
202 circuit
->rx
= isis_recv_pdu_bcast
;
204 zlog_warn("isis_sock_init(): unknown circuit type");
205 retval
= ISIS_WARNING
;
213 int isis_recv_pdu_bcast(struct isis_circuit
*circuit
, uint8_t *ssnpa
)
215 int bytesread
= 0, bytestoread
, offset
, one
= 1;
217 struct bpf_hdr
*bpf_hdr
;
219 assert(circuit
->fd
> 0);
221 if (ioctl(circuit
->fd
, FIONREAD
, (caddr_t
)&bytestoread
) < 0) {
222 zlog_warn("ioctl() FIONREAD failed: %s", safe_strerror(errno
));
226 bytesread
= read(circuit
->fd
, readbuff
, readblen
);
229 zlog_warn("isis_recv_pdu_bcast(): read() failed: %s",
230 safe_strerror(errno
));
238 while (buff_ptr
< readbuff
+ bytesread
) {
239 bpf_hdr
= (struct bpf_hdr
*) buff_ptr
;
240 assert(bpf_hdr
->bh_caplen
== bpf_hdr
->bh_datalen
);
241 offset
= bpf_hdr
->bh_hdrlen
+ LLC_LEN
+ ETHER_HDR_LEN
;
243 /* then we lose the BPF, LLC and ethernet headers */
244 stream_write(circuit
->rcv_stream
, buff_ptr
+ offset
,
245 bpf_hdr
->bh_caplen
- LLC_LEN
- ETHER_HDR_LEN
);
246 stream_set_getp(circuit
->rcv_stream
, 0);
248 memcpy(ssnpa
, buff_ptr
+ bpf_hdr
->bh_hdrlen
+ ETHER_ADDR_LEN
,
251 isis_handle_pdu(circuit
, ssnpa
);
252 stream_reset(circuit
->rcv_stream
);
253 buff_ptr
+= BPF_WORDALIGN(bpf_hdr
->bh_hdrlen
+
254 bpf_hdr
->bh_datalen
);
258 if (ioctl(circuit
->fd
, BIOCFLUSH
, &one
) < 0)
259 zlog_warn("Flushing failed: %s", safe_strerror(errno
));
264 int isis_send_pdu_bcast(struct isis_circuit
*circuit
, int level
)
266 struct ether_header
*eth
;
270 buflen
= stream_get_endp(circuit
->snd_stream
) + LLC_LEN
+ ETHER_HDR_LEN
;
271 if (buflen
> sizeof(sock_buff
)) {
273 "isis_send_pdu_bcast: sock_buff size %zu is less than "
274 "output pdu size %zu on circuit %s",
275 sizeof(sock_buff
), buflen
, circuit
->interface
->name
);
279 stream_set_getp(circuit
->snd_stream
, 0);
282 * First the eth header
284 eth
= (struct ether_header
*)sock_buff
;
286 memcpy(eth
->ether_dhost
, ALL_L1_ISS
, ETH_ALEN
);
288 memcpy(eth
->ether_dhost
, ALL_L2_ISS
, ETH_ALEN
);
289 memcpy(eth
->ether_shost
, circuit
->u
.bc
.snpa
, ETH_ALEN
);
290 size_t frame_size
= stream_get_endp(circuit
->snd_stream
) + LLC_LEN
;
291 eth
->ether_type
= htons(isis_ethertype(frame_size
));
296 sock_buff
[ETHER_HDR_LEN
] = ISO_SAP
;
297 sock_buff
[ETHER_HDR_LEN
+ 1] = ISO_SAP
;
298 sock_buff
[ETHER_HDR_LEN
+ 2] = 0x03;
300 /* then we copy the data */
301 memcpy(sock_buff
+ (LLC_LEN
+ ETHER_HDR_LEN
), circuit
->snd_stream
->data
,
302 stream_get_endp(circuit
->snd_stream
));
304 /* now we can send this */
305 written
= write(circuit
->fd
, sock_buff
, buflen
);
307 zlog_warn("IS-IS bpf: could not transmit packet on %s: %s",
308 circuit
->interface
->name
, safe_strerror(errno
));
309 if (ERRNO_IO_RETRY(errno
))
317 #endif /* ISIS_METHOD == ISIS_METHOD_BPF */