]> git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_bpf.c
Merge pull request #5625 from qlyoung/fix-zapi-ipset-name-nullterm
[mirror_frr.git] / isisd / isis_bpf.c
1 /*
2 * IS-IS Rout(e)ing protocol - isis_bpf.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; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <zebra.h>
24 #if ISIS_METHOD == ISIS_METHOD_BPF
25 #include <net/if.h>
26 #include <netinet/if_ether.h>
27 #include <sys/time.h>
28 #include <sys/ioctl.h>
29 #include <net/bpf.h>
30
31 #include "log.h"
32 #include "network.h"
33 #include "stream.h"
34 #include "if.h"
35 #include "lib_errors.h"
36
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"
46
47 #include "privs.h"
48
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;
62
63 /*
64 * Table 9 - Architectural constants for use with ISO 8802 subnetworks
65 * ISO 10589 - 8.4.8
66 */
67
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};
70 #if 0
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};
74 #endif
75
76 static char sock_buff[8192];
77
78 static int open_bpf_dev(struct isis_circuit *circuit)
79 {
80 int i = 0, fd;
81 char bpfdev[128];
82 struct ifreq ifr;
83 unsigned int blen, immediate;
84 #ifdef BIOCSSEESENT
85 unsigned int seesent;
86 #endif
87 struct timeval timeout;
88 struct bpf_program bpf_prog;
89
90 do {
91 (void)snprintf(bpfdev, sizeof(bpfdev), "/dev/bpf%d", i++);
92 fd = open(bpfdev, O_RDWR);
93 } while (fd < 0 && errno == EBUSY);
94
95 if (fd < 0) {
96 zlog_warn("open_bpf_dev(): failed to create bpf socket: %s",
97 safe_strerror(errno));
98 return ISIS_WARNING;
99 }
100
101 zlog_debug("Opened BPF device %s", bpfdev);
102
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));
107 return ISIS_WARNING;
108 }
109
110 if (ioctl(fd, BIOCGBLEN, (caddr_t)&blen) < 0) {
111 zlog_warn("failed to get BPF buffer len");
112 blen = circuit->interface->mtu;
113 }
114
115 readblen = blen;
116
117 if (readbuff == NULL)
118 readbuff = malloc(blen);
119
120 zlog_debug("BPF buffer len = %u", blen);
121
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.
125 */
126 immediate = 1;
127 if (ioctl(fd, BIOCIMMEDIATE, (caddr_t)&immediate) < 0) {
128 zlog_warn("failed to set BPF dev to immediate mode");
129 }
130
131 #ifdef BIOCSSEESENT
132 /*
133 * We want to see only incoming packets
134 */
135 seesent = 0;
136 if (ioctl(fd, BIOCSSEESENT, (caddr_t)&seesent) < 0) {
137 zlog_warn("failed to set BPF dev to incoming only mode");
138 }
139 #endif
140
141 /*
142 * ...but all of them
143 */
144 if (ioctl(fd, BIOCPROMISC) < 0) {
145 zlog_warn("failed to set BPF dev to promiscuous mode");
146 }
147
148 /*
149 * If the buffer length is smaller than our mtu, lets try to increase it
150 */
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);
155 }
156 }
157
158 /*
159 * Set a timeout parameter - hope this helps select()
160 */
161 timeout.tv_sec = 600;
162 timeout.tv_usec = 0;
163 if (ioctl(fd, BIOCSRTIMEOUT, (caddr_t)&timeout) < 0) {
164 zlog_warn("failed to set BPF device timeout");
165 }
166
167 /*
168 * And set the filter
169 */
170 memset(&bpf_prog, 0, sizeof(struct bpf_program));
171 bpf_prog.bf_len = 8;
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));
176 return ISIS_WARNING;
177 }
178
179 assert(fd > 0);
180
181 circuit->fd = fd;
182
183 return ISIS_OK;
184 }
185
186 /*
187 * Create the socket and set the tx/rx funcs
188 */
189 int isis_sock_init(struct isis_circuit *circuit)
190 {
191 int retval = ISIS_OK;
192
193 frr_with_privs(&isisd_privs) {
194
195 retval = open_bpf_dev(circuit);
196
197 if (retval != ISIS_OK) {
198 zlog_warn("%s: could not initialize the socket",
199 __func__);
200 break;
201 }
202
203 if (if_is_broadcast(circuit->interface)) {
204 circuit->tx = isis_send_pdu_bcast;
205 circuit->rx = isis_recv_pdu_bcast;
206 } else {
207 zlog_warn("isis_sock_init(): unknown circuit type");
208 retval = ISIS_WARNING;
209 break;
210 }
211 }
212
213 return retval;
214 }
215
216 int isis_recv_pdu_bcast(struct isis_circuit *circuit, uint8_t *ssnpa)
217 {
218 int bytesread = 0, bytestoread, offset, one = 1;
219 uint8_t *buff_ptr;
220 struct bpf_hdr *bpf_hdr;
221
222 assert(circuit->fd > 0);
223
224 if (ioctl(circuit->fd, FIONREAD, (caddr_t)&bytestoread) < 0) {
225 zlog_warn("ioctl() FIONREAD failed: %s", safe_strerror(errno));
226 }
227
228 if (bytestoread) {
229 bytesread = read(circuit->fd, readbuff, readblen);
230 }
231 if (bytesread < 0) {
232 zlog_warn("isis_recv_pdu_bcast(): read() failed: %s",
233 safe_strerror(errno));
234 return ISIS_WARNING;
235 }
236
237 if (bytesread == 0)
238 return ISIS_WARNING;
239
240 buff_ptr = readbuff;
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;
245
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);
250
251 memcpy(ssnpa, buff_ptr + bpf_hdr->bh_hdrlen + ETHER_ADDR_LEN,
252 ETHER_ADDR_LEN);
253
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);
258 }
259
260
261 if (ioctl(circuit->fd, BIOCFLUSH, &one) < 0)
262 zlog_warn("Flushing failed: %s", safe_strerror(errno));
263
264 return ISIS_OK;
265 }
266
267 int isis_send_pdu_bcast(struct isis_circuit *circuit, int level)
268 {
269 struct ether_header *eth;
270 ssize_t written;
271 size_t buflen;
272
273 buflen = stream_get_endp(circuit->snd_stream) + LLC_LEN + ETHER_HDR_LEN;
274 if (buflen > sizeof(sock_buff)) {
275 zlog_warn(
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);
279 return ISIS_WARNING;
280 }
281
282 stream_set_getp(circuit->snd_stream, 0);
283
284 /*
285 * First the eth header
286 */
287 eth = (struct ether_header *)sock_buff;
288 if (level == 1)
289 memcpy(eth->ether_dhost, ALL_L1_ISS, ETH_ALEN);
290 else
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));
295
296 /*
297 * Then the LLC
298 */
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;
302
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));
306
307 /* now we can send this */
308 written = write(circuit->fd, sock_buff, buflen);
309 if (written < 0) {
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))
313 return ISIS_WARNING;
314 return ISIS_ERROR;
315 }
316
317 return ISIS_OK;
318 }
319
320 #endif /* ISIS_METHOD == ISIS_METHOD_BPF */