]> git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_bpf.c
Merge pull request #1921 from donaldsharp/pim_stuff
[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
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"
45
46 #include "privs.h"
47
48 struct bpf_insn llcfilter[] = {
49 BPF_STMT(BPF_LD + BPF_B + BPF_ABS,
50 ETHER_HDR_LEN), /* check first byte */
51 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ISO_SAP, 0, 5),
52 BPF_STMT(BPF_LD + BPF_B + BPF_ABS, ETHER_HDR_LEN + 1),
53 BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ISO_SAP, 0,
54 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, (unsigned int)-1),
58 BPF_STMT(BPF_RET + BPF_K, 0)};
59 unsigned int readblen = 0;
60 uint8_t *readbuff = NULL;
61
62 /*
63 * Table 9 - Architectural constants for use with ISO 8802 subnetworks
64 * ISO 10589 - 8.4.8
65 */
66
67 uint8_t ALL_L1_ISS[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x14};
68 uint8_t ALL_L2_ISS[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x15};
69 uint8_t ALL_ISS[6] = {0x09, 0x00, 0x2B, 0x00, 0x00, 0x05};
70 uint8_t ALL_ESS[6] = {0x09, 0x00, 0x2B, 0x00, 0x00, 0x04};
71
72 static char sock_buff[8192];
73
74 static int open_bpf_dev(struct isis_circuit *circuit)
75 {
76 int i = 0, fd;
77 char bpfdev[128];
78 struct ifreq ifr;
79 unsigned int blen, immediate;
80 #ifdef BIOCSSEESENT
81 unsigned int seesent;
82 #endif
83 struct timeval timeout;
84 struct bpf_program bpf_prog;
85
86 do {
87 (void)snprintf(bpfdev, sizeof(bpfdev), "/dev/bpf%d", i++);
88 fd = open(bpfdev, O_RDWR);
89 } while (fd < 0 && errno == EBUSY);
90
91 if (fd < 0) {
92 zlog_warn("open_bpf_dev(): failed to create bpf socket: %s",
93 safe_strerror(errno));
94 return ISIS_WARNING;
95 }
96
97 zlog_debug("Opened BPF device %s", bpfdev);
98
99 memcpy(ifr.ifr_name, circuit->interface->name, sizeof(ifr.ifr_name));
100 if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) {
101 zlog_warn("open_bpf_dev(): failed to bind to interface: %s",
102 safe_strerror(errno));
103 return ISIS_WARNING;
104 }
105
106 if (ioctl(fd, BIOCGBLEN, (caddr_t)&blen) < 0) {
107 zlog_warn("failed to get BPF buffer len");
108 blen = circuit->interface->mtu;
109 }
110
111 readblen = blen;
112
113 if (readbuff == NULL)
114 readbuff = malloc(blen);
115
116 zlog_debug("BPF buffer len = %u", blen);
117
118 /* BPF(4): reads return immediately upon packet reception.
119 * Otherwise, a read will block until either the kernel
120 * buffer becomes full or a timeout occurs.
121 */
122 immediate = 1;
123 if (ioctl(fd, BIOCIMMEDIATE, (caddr_t)&immediate) < 0) {
124 zlog_warn("failed to set BPF dev to immediate mode");
125 }
126
127 #ifdef BIOCSSEESENT
128 /*
129 * We want to see only incoming packets
130 */
131 seesent = 0;
132 if (ioctl(fd, BIOCSSEESENT, (caddr_t)&seesent) < 0) {
133 zlog_warn("failed to set BPF dev to incoming only mode");
134 }
135 #endif
136
137 /*
138 * ...but all of them
139 */
140 if (ioctl(fd, BIOCPROMISC) < 0) {
141 zlog_warn("failed to set BPF dev to promiscuous mode");
142 }
143
144 /*
145 * If the buffer length is smaller than our mtu, lets try to increase it
146 */
147 if (blen < circuit->interface->mtu) {
148 if (ioctl(fd, BIOCSBLEN, &circuit->interface->mtu) < 0) {
149 zlog_warn("failed to set BPF buffer len (%u to %u)",
150 blen, circuit->interface->mtu);
151 }
152 }
153
154 /*
155 * Set a timeout parameter - hope this helps select()
156 */
157 timeout.tv_sec = 600;
158 timeout.tv_usec = 0;
159 if (ioctl(fd, BIOCSRTIMEOUT, (caddr_t)&timeout) < 0) {
160 zlog_warn("failed to set BPF device timeout");
161 }
162
163 /*
164 * And set the filter
165 */
166 memset(&bpf_prog, 0, sizeof(struct bpf_program));
167 bpf_prog.bf_len = 8;
168 bpf_prog.bf_insns = &(llcfilter[0]);
169 if (ioctl(fd, BIOCSETF, (caddr_t)&bpf_prog) < 0) {
170 zlog_warn("open_bpf_dev(): failed to install filter: %s",
171 safe_strerror(errno));
172 return ISIS_WARNING;
173 }
174
175 assert(fd > 0);
176
177 circuit->fd = fd;
178
179 return ISIS_OK;
180 }
181
182 /*
183 * Create the socket and set the tx/rx funcs
184 */
185 int isis_sock_init(struct isis_circuit *circuit)
186 {
187 int retval = ISIS_OK;
188
189 if (isisd_privs.change(ZPRIVS_RAISE))
190 zlog_err("%s: could not raise privs, %s", __func__,
191 safe_strerror(errno));
192
193 retval = open_bpf_dev(circuit);
194
195 if (retval != ISIS_OK) {
196 zlog_warn("%s: could not initialize the socket", __func__);
197 goto end;
198 }
199
200 if (if_is_broadcast(circuit->interface)) {
201 circuit->tx = isis_send_pdu_bcast;
202 circuit->rx = isis_recv_pdu_bcast;
203 } else {
204 zlog_warn("isis_sock_init(): unknown circuit type");
205 retval = ISIS_WARNING;
206 goto end;
207 }
208
209 end:
210 if (isisd_privs.change(ZPRIVS_LOWER))
211 zlog_err("%s: could not lower privs, %s", __func__,
212 safe_strerror(errno));
213
214 return retval;
215 }
216
217 int isis_recv_pdu_bcast(struct isis_circuit *circuit, uint8_t *ssnpa)
218 {
219 int bytesread = 0, bytestoread, offset, one = 1, err = ISIS_OK;
220 uint8_t *buff_ptr;
221 struct bpf_hdr *bpf_hdr;
222
223 assert(circuit->fd > 0);
224
225 if (ioctl(circuit->fd, FIONREAD, (caddr_t)&bytestoread) < 0) {
226 zlog_warn("ioctl() FIONREAD failed: %s", safe_strerror(errno));
227 }
228
229 if (bytestoread) {
230 bytesread = read(circuit->fd, readbuff, readblen);
231 }
232 if (bytesread < 0) {
233 zlog_warn("isis_recv_pdu_bcast(): read() failed: %s",
234 safe_strerror(errno));
235 return ISIS_WARNING;
236 }
237
238 if (bytesread == 0)
239 return ISIS_WARNING;
240
241 buff_ptr = readbuff;
242 while (buff_ptr < readbuff + bytesread) {
243 bpf_hdr = (struct bpf_hdr *) buff_ptr;
244 assert(bpf_hdr->bh_caplen == bpf_hdr->bh_datalen);
245 offset = bpf_hdr->bh_hdrlen + LLC_LEN + ETHER_HDR_LEN;
246
247 /* then we lose the BPF, LLC and ethernet headers */
248 stream_write(circuit->rcv_stream, buff_ptr + offset,
249 bpf_hdr->bh_caplen - LLC_LEN - ETHER_HDR_LEN);
250 stream_set_getp(circuit->rcv_stream, 0);
251
252 memcpy(ssnpa, buff_ptr + bpf_hdr->bh_hdrlen + ETHER_ADDR_LEN,
253 ETHER_ADDR_LEN);
254
255 err = isis_handle_pdu(circuit, ssnpa);
256 stream_reset(circuit->rcv_stream);
257 buff_ptr += BPF_WORDALIGN(bpf_hdr->bh_hdrlen +
258 bpf_hdr->bh_datalen);
259 }
260
261
262 if (ioctl(circuit->fd, BIOCFLUSH, &one) < 0)
263 zlog_warn("Flushing failed: %s", safe_strerror(errno));
264
265 return ISIS_OK;
266 }
267
268 int isis_send_pdu_bcast(struct isis_circuit *circuit, int level)
269 {
270 struct ether_header *eth;
271 ssize_t written;
272 size_t buflen;
273
274 buflen = stream_get_endp(circuit->snd_stream) + LLC_LEN + ETHER_HDR_LEN;
275 if (buflen > sizeof(sock_buff)) {
276 zlog_warn(
277 "isis_send_pdu_bcast: sock_buff size %zu is less than "
278 "output pdu size %zu on circuit %s",
279 sizeof(sock_buff), buflen, circuit->interface->name);
280 return ISIS_WARNING;
281 }
282
283 stream_set_getp(circuit->snd_stream, 0);
284
285 /*
286 * First the eth header
287 */
288 eth = (struct ether_header *)sock_buff;
289 if (level == 1)
290 memcpy(eth->ether_dhost, ALL_L1_ISS, ETH_ALEN);
291 else
292 memcpy(eth->ether_dhost, ALL_L2_ISS, ETH_ALEN);
293 memcpy(eth->ether_shost, circuit->u.bc.snpa, ETH_ALEN);
294 size_t frame_size = stream_get_endp(circuit->snd_stream) + LLC_LEN;
295 eth->ether_type = htons(isis_ethertype(frame_size));
296
297 /*
298 * Then the LLC
299 */
300 sock_buff[ETHER_HDR_LEN] = ISO_SAP;
301 sock_buff[ETHER_HDR_LEN + 1] = ISO_SAP;
302 sock_buff[ETHER_HDR_LEN + 2] = 0x03;
303
304 /* then we copy the data */
305 memcpy(sock_buff + (LLC_LEN + ETHER_HDR_LEN), circuit->snd_stream->data,
306 stream_get_endp(circuit->snd_stream));
307
308 /* now we can send this */
309 written = write(circuit->fd, sock_buff, buflen);
310 if (written < 0) {
311 zlog_warn("IS-IS bpf: could not transmit packet on %s: %s",
312 circuit->interface->name, safe_strerror(errno));
313 if (ERRNO_IO_RETRY(errno))
314 return ISIS_WARNING;
315 return ISIS_ERROR;
316 }
317
318 return ISIS_OK;
319 }
320
321 #endif /* ISIS_METHOD == ISIS_METHOD_BPF */