blob: 8c3602db369da295db384831dae5942cb901b143 [file] [log] [blame]
Paul Jakma8bc98052007-08-08 11:19:27 +00001/*
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; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23#include <zebra.h>
24#include <net/if.h>
25#include <netinet/if_ether.h>
26#include <sys/time.h>
27#include <sys/ioctl.h>
28#include <net/bpf.h>
29
30#include "log.h"
31#include "stream.h"
32#include "if.h"
33
34#include "isisd/dict.h"
35#include "isisd/include-netbsd/iso.h"
36#include "isisd/isis_constants.h"
37#include "isisd/isis_common.h"
38#include "isisd/isis_circuit.h"
39#include "isisd/isis_flags.h"
40#include "isisd/isisd.h"
41#include "isisd/isis_constants.h"
42#include "isisd/isis_circuit.h"
43#include "isisd/isis_network.h"
44
45#include "privs.h"
46
47extern struct zebra_privs_t isisd_privs;
48
49struct bpf_insn llcfilter[] = {
50 BPF_STMT (BPF_LD + BPF_B + BPF_ABS, 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, 3), /* check second byte */
54 BPF_STMT (BPF_LD + BPF_B + BPF_ABS, ETHER_HDR_LEN + 2),
55 BPF_JUMP (BPF_JMP + BPF_JEQ + BPF_K, 0x03, 0, 1), /* check third byte */
56 BPF_STMT (BPF_RET + BPF_K, (u_int) - 1),
57 BPF_STMT (BPF_RET + BPF_K, 0)
58};
David Ward362573e2009-12-03 20:44:35 +030059u_int readblen = 0;
Paul Jakma8bc98052007-08-08 11:19:27 +000060u_char *readbuff = NULL;
61
62/*
63 * Table 9 - Architectural constants for use with ISO 8802 subnetworks
64 * ISO 10589 - 8.4.8
65 */
66
67u_char ALL_L1_ISS[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x14 };
68u_char ALL_L2_ISS[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x15 };
69u_char ALL_ISS[6] = { 0x09, 0x00, 0x2B, 0x00, 0x00, 0x05 };
70u_char ALL_ESS[6] = { 0x09, 0x00, 0x2B, 0x00, 0x00, 0x04 };
71
72static char sock_buff[8192];
73
74static int
75open_bpf_dev (struct isis_circuit *circuit)
76{
77 int i = 0, fd;
78 char bpfdev[128];
79 struct ifreq ifr;
David Ward362573e2009-12-03 20:44:35 +030080 u_int blen, immediate, seesent;
Paul Jakma8bc98052007-08-08 11:19:27 +000081 struct timeval timeout;
82 struct bpf_program bpf_prog;
83
84 do
85 {
86 (void) snprintf (bpfdev, sizeof (bpfdev), "/dev/bpf%d", i++);
87 fd = open (bpfdev, O_RDWR);
88 }
89 while (fd < 0 && errno == EBUSY);
90
91 if (fd < 0)
92 {
93 zlog_warn ("open_bpf_dev(): failed to create bpf socket: %s",
94 safe_strerror (errno));
95 return ISIS_WARNING;
96 }
97
98 zlog_debug ("Opened BPF device %s", bpfdev);
99
100 memcpy (ifr.ifr_name, circuit->interface->name, sizeof (ifr.ifr_name));
101 if (ioctl (fd, BIOCSETIF, (caddr_t) & ifr) < 0)
102 {
103 zlog_warn ("open_bpf_dev(): failed to bind to interface: %s",
104 safe_strerror (errno));
105 return ISIS_WARNING;
106 }
107
108 if (ioctl (fd, BIOCGBLEN, (caddr_t) & blen) < 0)
109 {
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 */
David Ward362573e2009-12-03 20:44:35 +0300125 immediate = 1;
126 if (ioctl (fd, BIOCIMMEDIATE, (caddr_t) & immediate) < 0)
Paul Jakma8bc98052007-08-08 11:19:27 +0000127 {
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 */
David Ward362573e2009-12-03 20:44:35 +0300135 seesent = 0;
136 if (ioctl (fd, BIOCSSEESENT, (caddr_t) & seesent) < 0)
Paul Jakma8bc98052007-08-08 11:19:27 +0000137 {
138 zlog_warn ("failed to set BPF dev to incoming only mode");
139 }
140#endif
141
142 /*
143 * ...but all of them
144 */
David Ward362573e2009-12-03 20:44:35 +0300145 if (ioctl (fd, BIOCPROMISC) < 0)
Paul Jakma8bc98052007-08-08 11:19:27 +0000146 {
147 zlog_warn ("failed to set BPF dev to promiscuous mode");
148 }
149
150 /*
151 * If the buffer length is smaller than our mtu, lets try to increase it
152 */
153 if (blen < circuit->interface->mtu)
154 {
155 if (ioctl (fd, BIOCSBLEN, &circuit->interface->mtu) < 0)
156 {
157 zlog_warn ("failed to set BPF buffer len (%u to %u)", blen,
158 circuit->interface->mtu);
159 }
160 }
161
162 /*
163 * Set a timeout parameter - hope this helps select()
164 */
165 timeout.tv_sec = 600;
166 timeout.tv_usec = 0;
167 if (ioctl (fd, BIOCSRTIMEOUT, (caddr_t) & timeout) < 0)
168 {
169 zlog_warn ("failed to set BPF device timeout");
170 }
171
172 /*
173 * And set the filter
174 */
175 memset (&bpf_prog, 0, sizeof (struct bpf_program));
176 bpf_prog.bf_len = 8;
177 bpf_prog.bf_insns = &(llcfilter[0]);
178 if (ioctl (fd, BIOCSETF, (caddr_t) & bpf_prog) < 0)
179 {
180 zlog_warn ("open_bpf_dev(): failed to install filter: %s",
181 safe_strerror (errno));
182 return ISIS_WARNING;
183 }
184
185 assert (fd > 0);
186
187 circuit->fd = fd;
188
189 return ISIS_OK;
190}
191
192/*
193 * Create the socket and set the tx/rx funcs
194 */
195int
196isis_sock_init (struct isis_circuit *circuit)
197{
198 int retval = ISIS_OK;
199
200 if (isisd_privs.change (ZPRIVS_RAISE))
201 zlog_err ("%s: could not raise privs, %s", __func__, safe_strerror (errno));
202
203 retval = open_bpf_dev (circuit);
204
205 if (retval != ISIS_OK)
206 {
207 zlog_warn ("%s: could not initialize the socket", __func__);
208 goto end;
209 }
210
211 if (circuit->circ_type == CIRCUIT_T_BROADCAST)
212 {
213 circuit->tx = isis_send_pdu_bcast;
214 circuit->rx = isis_recv_pdu_bcast;
215 }
216 else if (circuit->circ_type == CIRCUIT_T_P2P)
217 {
218 circuit->tx = isis_send_pdu_p2p;
219 circuit->rx = isis_recv_pdu_p2p;
220 }
221 else
222 {
223 zlog_warn ("isis_sock_init(): unknown circuit type");
224 retval = ISIS_WARNING;
225 goto end;
226 }
227
228end:
229 if (isisd_privs.change (ZPRIVS_LOWER))
230 zlog_err ("%s: could not lower privs, %s", __func__, safe_strerror (errno));
231
232 return retval;
233}
234
235int
236isis_recv_pdu_bcast (struct isis_circuit *circuit, u_char * ssnpa)
237{
238 int bytesread = 0, bytestoread, offset, one = 1;
239 struct bpf_hdr *bpf_hdr;
240
241 assert (circuit->fd > 0);
242
243 if (ioctl (circuit->fd, FIONREAD, (caddr_t) & bytestoread) < 0)
244 {
245 zlog_warn ("ioctl() FIONREAD failed: %s", safe_strerror (errno));
246 }
247
248 if (bytestoread)
249 {
250 bytesread = read (circuit->fd, readbuff, readblen);
251 }
252 if (bytesread < 0)
253 {
254 zlog_warn ("isis_recv_pdu_bcast(): read() failed: %s",
255 safe_strerror (errno));
256 return ISIS_WARNING;
257 }
258
259 if (bytesread == 0)
260 return ISIS_WARNING;
261
262 bpf_hdr = (struct bpf_hdr *) readbuff;
263
264 assert (bpf_hdr->bh_caplen == bpf_hdr->bh_datalen);
265
266 offset = bpf_hdr->bh_hdrlen + LLC_LEN + ETHER_HDR_LEN;
267
268 /* then we lose the BPF, LLC and ethernet headers */
269 stream_write (circuit->rcv_stream, readbuff + offset,
270 bpf_hdr->bh_caplen - LLC_LEN - ETHER_HDR_LEN);
271 stream_set_getp (circuit->rcv_stream, 0);
272
273 memcpy (ssnpa, readbuff + bpf_hdr->bh_hdrlen + ETHER_ADDR_LEN,
274 ETHER_ADDR_LEN);
275
276 if (ioctl (circuit->fd, BIOCFLUSH, &one) < 0)
277 zlog_warn ("Flushing failed: %s", safe_strerror (errno));
278
279 return ISIS_OK;
280}
281
282int
283isis_recv_pdu_p2p (struct isis_circuit *circuit, u_char * ssnpa)
284{
285 int bytesread;
286
287 bytesread = stream_read (circuit->rcv_stream, circuit->fd,
288 circuit->interface->mtu);
289
290 if (bytesread < 0)
291 {
292 zlog_warn ("isis_recv_pdu_p2p(): read () failed: %s", safe_strerror (errno));
293 return ISIS_WARNING;
294 }
295
296 return ISIS_OK;
297}
298
299int
300isis_send_pdu_bcast (struct isis_circuit *circuit, int level)
301{
302 struct ether_header *eth;
303 int written;
304
305 stream_set_getp (circuit->snd_stream, 0);
306
307 /*
308 * First the eth header
309 */
310 eth = (struct ether_header *) sock_buff;
311 if (level == 1)
312 memcpy (eth->ether_dhost, ALL_L1_ISS, ETHER_ADDR_LEN);
313 else
314 memcpy (eth->ether_dhost, ALL_L2_ISS, ETHER_ADDR_LEN);
315 memcpy (eth->ether_shost, circuit->u.bc.snpa, ETHER_ADDR_LEN);
316 eth->ether_type = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN);
317
318 /*
319 * Then the LLC
320 */
321 sock_buff[ETHER_HDR_LEN] = ISO_SAP;
322 sock_buff[ETHER_HDR_LEN + 1] = ISO_SAP;
323 sock_buff[ETHER_HDR_LEN + 2] = 0x03;
324
325 /* then we copy the data */
326 memcpy (sock_buff + (LLC_LEN + ETHER_HDR_LEN), circuit->snd_stream->data,
327 stream_get_endp (circuit->snd_stream));
328
329 /* now we can send this */
330 written = write (circuit->fd, sock_buff,
331 stream_get_endp (circuit->snd_stream)
332 + LLC_LEN + ETHER_HDR_LEN);
333
334 return ISIS_OK;
335}
336
337int
338isis_send_pdu_p2p (struct isis_circuit *circuit, int level)
339{
340 return ISIS_OK;
341}