]>
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 static const uint8_t ALL_L1_ISS
[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x14};
69 static const uint8_t ALL_L2_ISS
[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x15};
71 /* missing support for P2P-over-LAN / ES-IS on BSD */
72 static const uint8_t ALL_ISS
[6] = {0x09, 0x00, 0x2B, 0x00, 0x00, 0x05};
73 static const uint8_t ALL_ESS
[6] = {0x09, 0x00, 0x2B, 0x00, 0x00, 0x04};
76 static char sock_buff
[8192];
78 static int open_bpf_dev(struct isis_circuit
*circuit
)
83 unsigned int blen
, immediate
;
87 struct timeval timeout
;
88 struct bpf_program bpf_prog
;
91 (void)snprintf(bpfdev
, sizeof(bpfdev
), "/dev/bpf%d", i
++);
92 fd
= open(bpfdev
, O_RDWR
);
93 } while (fd
< 0 && errno
== EBUSY
);
96 zlog_warn("open_bpf_dev(): failed to create bpf socket: %s",
97 safe_strerror(errno
));
101 zlog_debug("Opened BPF device %s", bpfdev
);
103 memcpy(ifr
.ifr_name
, circuit
->interface
->name
, sizeof(ifr
.ifr_name
));
104 if (ioctl(fd
, BIOCSETIF
, (caddr_t
)&ifr
) < 0) {
105 zlog_warn("open_bpf_dev(): failed to bind to interface: %s",
106 safe_strerror(errno
));
110 if (ioctl(fd
, BIOCGBLEN
, (caddr_t
)&blen
) < 0) {
111 zlog_warn("failed to get BPF buffer len");
112 blen
= circuit
->interface
->mtu
;
117 if (readbuff
== NULL
)
118 readbuff
= malloc(blen
);
120 zlog_debug("BPF buffer len = %u", blen
);
122 /* BPF(4): reads return immediately upon packet reception.
123 * Otherwise, a read will block until either the kernel
124 * buffer becomes full or a timeout occurs.
127 if (ioctl(fd
, BIOCIMMEDIATE
, (caddr_t
)&immediate
) < 0) {
128 zlog_warn("failed to set BPF dev to immediate mode");
133 * We want to see only incoming packets
136 if (ioctl(fd
, BIOCSSEESENT
, (caddr_t
)&seesent
) < 0) {
137 zlog_warn("failed to set BPF dev to incoming only mode");
144 if (ioctl(fd
, BIOCPROMISC
) < 0) {
145 zlog_warn("failed to set BPF dev to promiscuous mode");
149 * If the buffer length is smaller than our mtu, lets try to increase it
151 if (blen
< circuit
->interface
->mtu
) {
152 if (ioctl(fd
, BIOCSBLEN
, &circuit
->interface
->mtu
) < 0) {
153 zlog_warn("failed to set BPF buffer len (%u to %u)",
154 blen
, circuit
->interface
->mtu
);
159 * Set a timeout parameter - hope this helps select()
161 timeout
.tv_sec
= 600;
163 if (ioctl(fd
, BIOCSRTIMEOUT
, (caddr_t
)&timeout
) < 0) {
164 zlog_warn("failed to set BPF device timeout");
170 memset(&bpf_prog
, 0, sizeof(struct bpf_program
));
172 bpf_prog
.bf_insns
= &(llcfilter
[0]);
173 if (ioctl(fd
, BIOCSETF
, (caddr_t
)&bpf_prog
) < 0) {
174 zlog_warn("open_bpf_dev(): failed to install filter: %s",
175 safe_strerror(errno
));
187 * Create the socket and set the tx/rx funcs
189 int isis_sock_init(struct isis_circuit
*circuit
)
191 int retval
= ISIS_OK
;
193 frr_with_privs(&isisd_privs
) {
195 retval
= open_bpf_dev(circuit
);
197 if (retval
!= ISIS_OK
) {
198 zlog_warn("%s: could not initialize the socket",
203 if (if_is_broadcast(circuit
->interface
)) {
204 circuit
->tx
= isis_send_pdu_bcast
;
205 circuit
->rx
= isis_recv_pdu_bcast
;
207 zlog_warn("isis_sock_init(): unknown circuit type");
208 retval
= ISIS_WARNING
;
216 int isis_recv_pdu_bcast(struct isis_circuit
*circuit
, uint8_t *ssnpa
)
218 int bytesread
= 0, bytestoread
, offset
, one
= 1;
220 struct bpf_hdr
*bpf_hdr
;
222 assert(circuit
->fd
> 0);
224 if (ioctl(circuit
->fd
, FIONREAD
, (caddr_t
)&bytestoread
) < 0) {
225 zlog_warn("ioctl() FIONREAD failed: %s", safe_strerror(errno
));
229 bytesread
= read(circuit
->fd
, readbuff
, readblen
);
232 zlog_warn("isis_recv_pdu_bcast(): read() failed: %s",
233 safe_strerror(errno
));
241 while (buff_ptr
< readbuff
+ bytesread
) {
242 bpf_hdr
= (struct bpf_hdr
*) buff_ptr
;
243 assert(bpf_hdr
->bh_caplen
== bpf_hdr
->bh_datalen
);
244 offset
= bpf_hdr
->bh_hdrlen
+ LLC_LEN
+ ETHER_HDR_LEN
;
246 /* then we lose the BPF, LLC and ethernet headers */
247 stream_write(circuit
->rcv_stream
, buff_ptr
+ offset
,
248 bpf_hdr
->bh_caplen
- LLC_LEN
- ETHER_HDR_LEN
);
249 stream_set_getp(circuit
->rcv_stream
, 0);
251 memcpy(ssnpa
, buff_ptr
+ bpf_hdr
->bh_hdrlen
+ ETHER_ADDR_LEN
,
254 isis_handle_pdu(circuit
, ssnpa
);
255 stream_reset(circuit
->rcv_stream
);
256 buff_ptr
+= BPF_WORDALIGN(bpf_hdr
->bh_hdrlen
+
257 bpf_hdr
->bh_datalen
);
261 if (ioctl(circuit
->fd
, BIOCFLUSH
, &one
) < 0)
262 zlog_warn("Flushing failed: %s", safe_strerror(errno
));
267 int isis_send_pdu_bcast(struct isis_circuit
*circuit
, int level
)
269 struct ether_header
*eth
;
273 buflen
= stream_get_endp(circuit
->snd_stream
) + LLC_LEN
+ ETHER_HDR_LEN
;
274 if (buflen
> sizeof(sock_buff
)) {
276 "isis_send_pdu_bcast: sock_buff size %zu is less than "
277 "output pdu size %zu on circuit %s",
278 sizeof(sock_buff
), buflen
, circuit
->interface
->name
);
282 stream_set_getp(circuit
->snd_stream
, 0);
285 * First the eth header
287 eth
= (struct ether_header
*)sock_buff
;
289 memcpy(eth
->ether_dhost
, ALL_L1_ISS
, ETH_ALEN
);
291 memcpy(eth
->ether_dhost
, ALL_L2_ISS
, ETH_ALEN
);
292 memcpy(eth
->ether_shost
, circuit
->u
.bc
.snpa
, ETH_ALEN
);
293 size_t frame_size
= stream_get_endp(circuit
->snd_stream
) + LLC_LEN
;
294 eth
->ether_type
= htons(isis_ethertype(frame_size
));
299 sock_buff
[ETHER_HDR_LEN
] = ISO_SAP
;
300 sock_buff
[ETHER_HDR_LEN
+ 1] = ISO_SAP
;
301 sock_buff
[ETHER_HDR_LEN
+ 2] = 0x03;
303 /* then we copy the data */
304 memcpy(sock_buff
+ (LLC_LEN
+ ETHER_HDR_LEN
), circuit
->snd_stream
->data
,
305 stream_get_endp(circuit
->snd_stream
));
307 /* now we can send this */
308 written
= write(circuit
->fd
, sock_buff
, buflen
);
310 zlog_warn("IS-IS bpf: could not transmit packet on %s: %s",
311 circuit
->interface
->name
, safe_strerror(errno
));
312 if (ERRNO_IO_RETRY(errno
))
320 #endif /* ISIS_METHOD == ISIS_METHOD_BPF */