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