]>
git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_bpf.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IS-IS Rout(e)ing protocol - isis_bpf.c
5 * Copyright (C) 2001,2002 Sampo Saaristo
6 * Tampere University of Technology
7 * Institute of Communications Engineering
11 #if ISIS_METHOD == ISIS_METHOD_BPF
13 #include <netinet/if_ether.h>
15 #include <sys/ioctl.h>
22 #include "lib_errors.h"
24 #include "isisd/isis_constants.h"
25 #include "isisd/isis_common.h"
26 #include "isisd/isis_circuit.h"
27 #include "isisd/isis_flags.h"
28 #include "isisd/isisd.h"
29 #include "isisd/isis_constants.h"
30 #include "isisd/isis_circuit.h"
31 #include "isisd/isis_network.h"
32 #include "isisd/isis_pdu.h"
36 struct bpf_insn llcfilter
[] = {
37 BPF_STMT(BPF_LD
+ BPF_B
+ BPF_ABS
,
38 ETHER_HDR_LEN
), /* check first byte */
39 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, ISO_SAP
, 0, 5),
40 BPF_STMT(BPF_LD
+ BPF_B
+ BPF_ABS
, ETHER_HDR_LEN
+ 1),
41 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, ISO_SAP
, 0,
42 3), /* check second byte */
43 BPF_STMT(BPF_LD
+ BPF_B
+ BPF_ABS
, ETHER_HDR_LEN
+ 2),
44 BPF_JUMP(BPF_JMP
+ BPF_JEQ
+ BPF_K
, 0x03, 0, 1), /* check third byte */
45 BPF_STMT(BPF_RET
+ BPF_K
, (unsigned int)-1),
46 BPF_STMT(BPF_RET
+ BPF_K
, 0)};
47 unsigned int readblen
= 0;
48 uint8_t *readbuff
= NULL
;
51 * Table 9 - Architectural constants for use with ISO 8802 subnetworks
55 static const uint8_t ALL_L1_ISS
[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x14};
56 static const uint8_t ALL_L2_ISS
[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x15};
57 static char sock_buff
[16384];
59 static int open_bpf_dev(struct isis_circuit
*circuit
)
64 unsigned int blen
, immediate
;
68 struct timeval timeout
;
69 struct bpf_program bpf_prog
;
72 (void)snprintf(bpfdev
, sizeof(bpfdev
), "/dev/bpf%d", i
++);
73 fd
= open(bpfdev
, O_RDWR
);
74 } while (fd
< 0 && errno
== EBUSY
);
77 zlog_warn("open_bpf_dev(): failed to create bpf socket: %s",
78 safe_strerror(errno
));
82 zlog_debug("Opened BPF device %s", bpfdev
);
84 memcpy(ifr
.ifr_name
, circuit
->interface
->name
, sizeof(ifr
.ifr_name
));
85 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
86 zlog_warn("open_bpf_dev(): failed to bind to interface: %s",
87 safe_strerror(errno
));
91 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&blen
) < 0) {
92 zlog_warn("failed to get BPF buffer len");
93 blen
= circuit
->interface
->mtu
;
99 readbuff
= malloc(blen
);
101 zlog_debug("BPF buffer len = %u", blen
);
103 /* BPF(4): reads return immediately upon packet reception.
104 * Otherwise, a read will block until either the kernel
105 * buffer becomes full or a timeout occurs.
108 if (ioctl(fd
, BIOCIMMEDIATE
, (caddr_t
)&immediate
) < 0) {
109 zlog_warn("failed to set BPF dev to immediate mode");
114 * We want to see only incoming packets
117 if (ioctl(fd
, BIOCSSEESENT
, (caddr_t
)&seesent
) < 0) {
118 zlog_warn("failed to set BPF dev to incoming only mode");
125 if (ioctl(fd
, BIOCPROMISC
) < 0) {
126 zlog_warn("failed to set BPF dev to promiscuous mode");
130 * If the buffer length is smaller than our mtu, lets try to increase it
132 if (blen
< circuit
->interface
->mtu
) {
133 if (ioctl(fd
, BIOCSBLEN
, &circuit
->interface
->mtu
) < 0) {
134 zlog_warn("failed to set BPF buffer len (%u to %u)",
135 blen
, circuit
->interface
->mtu
);
140 * Set a timeout parameter - hope this helps select()
142 timeout
.tv_sec
= 600;
144 if (ioctl(fd
, BIOCSRTIMEOUT
, (caddr_t
)&timeout
) < 0) {
145 zlog_warn("failed to set BPF device timeout");
151 memset(&bpf_prog
, 0, sizeof(bpf_prog
));
153 bpf_prog
.bf_insns
= &(llcfilter
[0]);
154 if (ioctl(fd
, BIOCSETF
, (caddr_t
)&bpf_prog
) < 0) {
155 zlog_warn("%s: failed to install filter: %s", __func__
,
156 safe_strerror(errno
));
168 * Create the socket and set the tx/rx funcs
170 int isis_sock_init(struct isis_circuit
*circuit
)
172 int retval
= ISIS_OK
;
174 frr_with_privs(&isisd_privs
) {
176 retval
= open_bpf_dev(circuit
);
178 if (retval
!= ISIS_OK
) {
179 zlog_warn("%s: could not initialize the socket",
184 if (if_is_broadcast(circuit
->interface
)) {
185 circuit
->tx
= isis_send_pdu_bcast
;
186 circuit
->rx
= isis_recv_pdu_bcast
;
188 zlog_warn("%s: unknown circuit type", __func__
);
189 retval
= ISIS_WARNING
;
197 int isis_recv_pdu_bcast(struct isis_circuit
*circuit
, uint8_t *ssnpa
)
199 int bytesread
= 0, bytestoread
= 0, offset
, one
= 1;
201 struct bpf_hdr
*bpf_hdr
;
203 assert(circuit
->fd
> 0);
205 if (ioctl(circuit
->fd
, FIONREAD
, (caddr_t
)&bytestoread
) < 0) {
206 zlog_warn("ioctl() FIONREAD failed: %s", safe_strerror(errno
));
210 bytesread
= read(circuit
->fd
, readbuff
, readblen
);
213 zlog_warn("%s: read() failed: %s", __func__
,
214 safe_strerror(errno
));
222 while (buff_ptr
< readbuff
+ bytesread
) {
223 bpf_hdr
= (struct bpf_hdr
*) buff_ptr
;
224 assert(bpf_hdr
->bh_caplen
== bpf_hdr
->bh_datalen
);
225 offset
= bpf_hdr
->bh_hdrlen
+ LLC_LEN
+ ETHER_HDR_LEN
;
227 /* then we lose the BPF, LLC and ethernet headers */
228 stream_write(circuit
->rcv_stream
, buff_ptr
+ offset
,
229 bpf_hdr
->bh_caplen
- LLC_LEN
- ETHER_HDR_LEN
);
230 stream_set_getp(circuit
->rcv_stream
, 0);
232 memcpy(ssnpa
, buff_ptr
+ bpf_hdr
->bh_hdrlen
+ ETHER_ADDR_LEN
,
235 isis_handle_pdu(circuit
, ssnpa
);
236 stream_reset(circuit
->rcv_stream
);
237 buff_ptr
+= BPF_WORDALIGN(bpf_hdr
->bh_hdrlen
+
238 bpf_hdr
->bh_datalen
);
242 if (ioctl(circuit
->fd
, BIOCFLUSH
, &one
) < 0)
243 zlog_warn("Flushing failed: %s", safe_strerror(errno
));
248 int isis_send_pdu_bcast(struct isis_circuit
*circuit
, int level
)
250 struct ether_header
*eth
;
254 buflen
= stream_get_endp(circuit
->snd_stream
) + LLC_LEN
+ ETHER_HDR_LEN
;
255 if (buflen
> sizeof(sock_buff
)) {
257 "%s: sock_buff size %zu is less than output pdu size %zu on circuit %s",
258 __func__
, sizeof(sock_buff
), buflen
,
259 circuit
->interface
->name
);
263 stream_set_getp(circuit
->snd_stream
, 0);
266 * First the eth header
268 eth
= (struct ether_header
*)sock_buff
;
270 memcpy(eth
->ether_dhost
, ALL_L1_ISS
, ETH_ALEN
);
272 memcpy(eth
->ether_dhost
, ALL_L2_ISS
, ETH_ALEN
);
273 memcpy(eth
->ether_shost
, circuit
->u
.bc
.snpa
, ETH_ALEN
);
274 size_t frame_size
= stream_get_endp(circuit
->snd_stream
) + LLC_LEN
;
275 eth
->ether_type
= htons(isis_ethertype(frame_size
));
280 sock_buff
[ETHER_HDR_LEN
] = ISO_SAP
;
281 sock_buff
[ETHER_HDR_LEN
+ 1] = ISO_SAP
;
282 sock_buff
[ETHER_HDR_LEN
+ 2] = 0x03;
284 /* then we copy the data */
285 memcpy(sock_buff
+ (LLC_LEN
+ ETHER_HDR_LEN
), circuit
->snd_stream
->data
,
286 stream_get_endp(circuit
->snd_stream
));
288 /* now we can send this */
289 written
= write(circuit
->fd
, sock_buff
, buflen
);
291 zlog_warn("IS-IS bpf: could not transmit packet on %s: %s",
292 circuit
->interface
->name
, safe_strerror(errno
));
293 if (ERRNO_IO_RETRY(errno
))
301 #endif /* ISIS_METHOD == ISIS_METHOD_BPF */