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