]> git.proxmox.com Git - mirror_frr.git/blame - isisd/isis_pfpacket.c
zebra, lib: fix the ZEBRA_INTERFACE_VRF_UPDATE zapi message
[mirror_frr.git] / isisd / isis_pfpacket.c
CommitLineData
8bc98059
PJ
1/*
2 * IS-IS Rout(e)ing protocol - isis_pfpacket.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_PFPACKET
d62a17ae 25#include <net/ethernet.h> /* the L2 protocols */
8bc98059
PJ
26#include <netpacket/packet.h>
27
4fa80053
DL
28#include <linux/filter.h>
29
8bc98059 30#include "log.h"
cfd1f27b 31#include "network.h"
8bc98059
PJ
32#include "stream.h"
33#include "if.h"
38937bd5 34#include "lib_errors.h"
8bc98059
PJ
35
36#include "isisd/dict.h"
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"
45
46#include "privs.h"
47
4fa80053 48/* tcpdump -i eth0 'isis' -dd */
996c9314
LB
49static struct sock_filter isisfilter[] = {
50 /* NB: we're in SOCK_DGRAM, so src/dst mac + length are stripped
51 * off!
52 * (OTOH it's a bit more lower-layer agnostic and might work
53 * over GRE?) */
54 /* { 0x28, 0, 0, 0x0000000c - 14 }, */
55 /* { 0x25, 5, 0, 0x000005dc }, */
56 {0x28, 0, 0, 0x0000000e - 14}, {0x15, 0, 3, 0x0000fefe},
57 {0x30, 0, 0, 0x00000011 - 14}, {0x15, 0, 1, 0x00000083},
58 {0x6, 0, 0, 0x00040000}, {0x6, 0, 0, 0x00000000},
4fa80053
DL
59};
60
61static struct sock_fprog bpf = {
62 .len = array_size(isisfilter),
63 .filter = isisfilter,
64};
65
8bc98059
PJ
66/*
67 * Table 9 - Architectural constants for use with ISO 8802 subnetworks
68 * ISO 10589 - 8.4.8
69 */
70
d7c0a89a
QY
71uint8_t ALL_L1_ISS[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x14};
72uint8_t ALL_L2_ISS[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x15};
73uint8_t ALL_ISS[6] = {0x09, 0x00, 0x2B, 0x00, 0x00, 0x05};
74uint8_t ALL_ESS[6] = {0x09, 0x00, 0x2B, 0x00, 0x00, 0x04};
8bc98059 75
f2bce9a5
DL
76static uint8_t discard_buff[8192];
77static uint8_t sock_buff[8192];
8bc98059
PJ
78
79/*
80 * if level is 0 we are joining p2p multicast
81 * FIXME: and the p2p multicast being ???
82 */
d62a17ae 83static int isis_multicast_join(int fd, int registerto, int if_num)
8bc98059 84{
d62a17ae 85 struct packet_mreq mreq;
86
87 memset(&mreq, 0, sizeof(mreq));
88 mreq.mr_ifindex = if_num;
89 if (registerto) {
90 mreq.mr_type = PACKET_MR_MULTICAST;
91 mreq.mr_alen = ETH_ALEN;
92 if (registerto == 1)
93 memcpy(&mreq.mr_address, ALL_L1_ISS, ETH_ALEN);
94 else if (registerto == 2)
95 memcpy(&mreq.mr_address, ALL_L2_ISS, ETH_ALEN);
96 else if (registerto == 3)
97 memcpy(&mreq.mr_address, ALL_ISS, ETH_ALEN);
98 else
99 memcpy(&mreq.mr_address, ALL_ESS, ETH_ALEN);
100
101 } else {
102 mreq.mr_type = PACKET_MR_ALLMULTI;
103 }
8bc98059 104#ifdef EXTREME_DEBUG
d62a17ae 105 zlog_debug(
106 "isis_multicast_join(): fd=%d, reg_to=%d, if_num=%d, "
107 "address = %02x:%02x:%02x:%02x:%02x:%02x",
108 fd, registerto, if_num, mreq.mr_address[0], mreq.mr_address[1],
109 mreq.mr_address[2], mreq.mr_address[3], mreq.mr_address[4],
110 mreq.mr_address[5]);
8bc98059 111#endif /* EXTREME_DEBUG */
d62a17ae 112 if (setsockopt(fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq,
113 sizeof(struct packet_mreq))) {
114 zlog_warn("isis_multicast_join(): setsockopt(): %s",
115 safe_strerror(errno));
116 return ISIS_WARNING;
117 }
118
119 return ISIS_OK;
8bc98059
PJ
120}
121
d62a17ae 122static int open_packet_socket(struct isis_circuit *circuit)
8bc98059 123{
d62a17ae 124 struct sockaddr_ll s_addr;
125 int fd, retval = ISIS_OK;
126
127 fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
128 if (fd < 0) {
129 zlog_warn("open_packet_socket(): socket() failed %s",
130 safe_strerror(errno));
131 return ISIS_WARNING;
132 }
133
134 if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf))) {
135 zlog_warn("open_packet_socket(): SO_ATTACH_FILTER failed: %s",
136 safe_strerror(errno));
137 }
138
139 /*
140 * Bind to the physical interface
141 */
142 memset(&s_addr, 0, sizeof(struct sockaddr_ll));
143 s_addr.sll_family = AF_PACKET;
144 s_addr.sll_protocol = htons(ETH_P_ALL);
145 s_addr.sll_ifindex = circuit->interface->ifindex;
146
147 if (bind(fd, (struct sockaddr *)(&s_addr), sizeof(struct sockaddr_ll))
148 < 0) {
149 zlog_warn("open_packet_socket(): bind() failed: %s",
150 safe_strerror(errno));
151 close(fd);
152 return ISIS_WARNING;
153 }
154
155 circuit->fd = fd;
156
157 if (if_is_broadcast(circuit->interface)) {
158 /*
159 * Join to multicast groups
160 * according to
161 * 8.4.2 - Broadcast subnetwork IIH PDUs
162 * FIXME: is there a case only one will fail??
163 */
164 /* joining ALL_L1_ISS */
165 retval |= isis_multicast_join(circuit->fd, 1,
166 circuit->interface->ifindex);
167 /* joining ALL_L2_ISS */
168 retval |= isis_multicast_join(circuit->fd, 2,
169 circuit->interface->ifindex);
170 /* joining ALL_ISS (used in RFC 5309 p2p-over-lan as well) */
171 retval |= isis_multicast_join(circuit->fd, 3,
172 circuit->interface->ifindex);
173 } else {
174 retval = isis_multicast_join(circuit->fd, 0,
175 circuit->interface->ifindex);
176 }
177
178 return retval;
8bc98059
PJ
179}
180
181/*
182 * Create the socket and set the tx/rx funcs
183 */
d62a17ae 184int isis_sock_init(struct isis_circuit *circuit)
8bc98059 185{
d62a17ae 186 int retval = ISIS_OK;
187
01b9e3fd 188 frr_elevate_privs(&isisd_privs) {
d62a17ae 189
01b9e3fd 190 retval = open_packet_socket(circuit);
d62a17ae 191
01b9e3fd 192 if (retval != ISIS_OK) {
633fc9b1
DL
193 zlog_warn("%s: could not initialize the socket",
194 __func__);
01b9e3fd
DL
195 break;
196 }
d62a17ae 197
198 /* Assign Rx and Tx callbacks are based on real if type */
01b9e3fd
DL
199 if (if_is_broadcast(circuit->interface)) {
200 circuit->tx = isis_send_pdu_bcast;
201 circuit->rx = isis_recv_pdu_bcast;
202 } else if (if_is_pointopoint(circuit->interface)) {
203 circuit->tx = isis_send_pdu_p2p;
204 circuit->rx = isis_recv_pdu_p2p;
205 } else {
206 zlog_warn("isis_sock_init(): unknown circuit type");
207 retval = ISIS_WARNING;
208 break;
209 }
633fc9b1 210 }
8bc98059 211
d62a17ae 212 return retval;
8bc98059
PJ
213}
214
d7c0a89a 215static inline int llc_check(uint8_t *llc)
8bc98059 216{
d62a17ae 217 if (*llc != ISO_SAP || *(llc + 1) != ISO_SAP || *(llc + 2) != 3)
218 return 0;
8bc98059 219
d62a17ae 220 return 1;
8bc98059
PJ
221}
222
d7c0a89a 223int isis_recv_pdu_bcast(struct isis_circuit *circuit, uint8_t *ssnpa)
8bc98059 224{
d62a17ae 225 int bytesread, addr_len;
226 struct sockaddr_ll s_addr;
d7c0a89a 227 uint8_t llc[LLC_LEN];
d62a17ae 228
229 addr_len = sizeof(s_addr);
230
231 memset(&s_addr, 0, sizeof(struct sockaddr_ll));
232
233 bytesread =
234 recvfrom(circuit->fd, (void *)&llc, LLC_LEN, MSG_PEEK,
235 (struct sockaddr *)&s_addr, (socklen_t *)&addr_len);
236
237 if ((bytesread < 0)
238 || (s_addr.sll_ifindex != (int)circuit->interface->ifindex)) {
239 if (bytesread < 0) {
240 zlog_warn(
241 "isis_recv_packet_bcast(): ifname %s, fd %d, "
242 "bytesread %d, recvfrom(): %s",
243 circuit->interface->name, circuit->fd,
244 bytesread, safe_strerror(errno));
245 }
246 if (s_addr.sll_ifindex != (int)circuit->interface->ifindex) {
247 zlog_warn(
248 "packet is received on multiple interfaces: "
249 "socket interface %d, circuit interface %d, "
250 "packet type %u",
251 s_addr.sll_ifindex, circuit->interface->ifindex,
252 s_addr.sll_pkttype);
253 }
254
255 /* get rid of the packet */
256 bytesread = recvfrom(circuit->fd, discard_buff,
257 sizeof(discard_buff), MSG_DONTWAIT,
258 (struct sockaddr *)&s_addr,
259 (socklen_t *)&addr_len);
260
261 if (bytesread < 0)
262 zlog_warn("isis_recv_pdu_bcast(): recvfrom() failed");
263
264 return ISIS_WARNING;
265 }
266 /*
267 * Filtering by llc field, discard packets sent by this host (other
268 * circuit)
269 */
270 if (!llc_check(llc) || s_addr.sll_pkttype == PACKET_OUTGOING) {
271 /* Read the packet into discard buff */
272 bytesread = recvfrom(circuit->fd, discard_buff,
273 sizeof(discard_buff), MSG_DONTWAIT,
274 (struct sockaddr *)&s_addr,
275 (socklen_t *)&addr_len);
276 if (bytesread < 0)
277 zlog_warn("isis_recv_pdu_bcast(): recvfrom() failed");
278 return ISIS_WARNING;
279 }
280
281 /* on lan we have to read to the static buff first */
282 bytesread = recvfrom(circuit->fd, sock_buff, sizeof(sock_buff),
283 MSG_DONTWAIT, (struct sockaddr *)&s_addr,
284 (socklen_t *)&addr_len);
285 if (bytesread < 0) {
286 zlog_warn("isis_recv_pdu_bcast(): recvfrom() failed");
287 return ISIS_WARNING;
288 }
289
290 /* then we lose the LLC */
291 stream_write(circuit->rcv_stream, sock_buff + LLC_LEN,
292 bytesread - LLC_LEN);
293
294 memcpy(ssnpa, &s_addr.sll_addr, s_addr.sll_halen);
295
296 return ISIS_OK;
8bc98059
PJ
297}
298
d7c0a89a 299int isis_recv_pdu_p2p(struct isis_circuit *circuit, uint8_t *ssnpa)
8bc98059 300{
d62a17ae 301 int bytesread, addr_len;
302 struct sockaddr_ll s_addr;
303
304 memset(&s_addr, 0, sizeof(struct sockaddr_ll));
305 addr_len = sizeof(s_addr);
306
307 /* we can read directly to the stream */
aa979109
A
308 (void)stream_recvfrom(
309 circuit->rcv_stream, circuit->fd, circuit->interface->mtu, 0,
310 (struct sockaddr *)&s_addr, (socklen_t *)&addr_len);
d62a17ae 311
312 if (s_addr.sll_pkttype == PACKET_OUTGOING) {
313 /* Read the packet into discard buff */
314 bytesread = recvfrom(circuit->fd, discard_buff,
315 sizeof(discard_buff), MSG_DONTWAIT,
316 (struct sockaddr *)&s_addr,
317 (socklen_t *)&addr_len);
318 if (bytesread < 0)
319 zlog_warn("isis_recv_pdu_p2p(): recvfrom() failed");
320 return ISIS_WARNING;
321 }
322
323 /* If we don't have protocol type 0x00FE which is
324 * ISO over GRE we exit with pain :)
325 */
326 if (ntohs(s_addr.sll_protocol) != 0x00FE) {
327 zlog_warn("isis_recv_pdu_p2p(): protocol mismatch(): %X",
328 ntohs(s_addr.sll_protocol));
329 return ISIS_WARNING;
330 }
331
332 memcpy(ssnpa, &s_addr.sll_addr, s_addr.sll_halen);
333
334 return ISIS_OK;
8bc98059
PJ
335}
336
d62a17ae 337int isis_send_pdu_bcast(struct isis_circuit *circuit, int level)
8bc98059 338{
d62a17ae 339 struct msghdr msg;
340 struct iovec iov[2];
341
342 /* we need to do the LLC in here because of P2P circuits, which will
343 * not need it
344 */
345 struct sockaddr_ll sa;
346
347 stream_set_getp(circuit->snd_stream, 0);
348 memset(&sa, 0, sizeof(struct sockaddr_ll));
349 sa.sll_family = AF_PACKET;
350
351 size_t frame_size = stream_get_endp(circuit->snd_stream) + LLC_LEN;
352 sa.sll_protocol = htons(isis_ethertype(frame_size));
353 sa.sll_ifindex = circuit->interface->ifindex;
354 sa.sll_halen = ETH_ALEN;
355 /* RFC5309 section 4.1 recommends ALL_ISS */
356 if (circuit->circ_type == CIRCUIT_T_P2P)
357 memcpy(&sa.sll_addr, ALL_ISS, ETH_ALEN);
358 else if (level == 1)
359 memcpy(&sa.sll_addr, ALL_L1_ISS, ETH_ALEN);
360 else
361 memcpy(&sa.sll_addr, ALL_L2_ISS, ETH_ALEN);
362
363 /* on a broadcast circuit */
364 /* first we put the LLC in */
365 sock_buff[0] = 0xFE;
366 sock_buff[1] = 0xFE;
367 sock_buff[2] = 0x03;
368
369 memset(&msg, 0, sizeof(msg));
370 msg.msg_name = &sa;
371 msg.msg_namelen = sizeof(struct sockaddr_ll);
372 msg.msg_iov = iov;
373 msg.msg_iovlen = 2;
374 iov[0].iov_base = sock_buff;
375 iov[0].iov_len = LLC_LEN;
376 iov[1].iov_base = circuit->snd_stream->data;
377 iov[1].iov_len = stream_get_endp(circuit->snd_stream);
378
379 if (sendmsg(circuit->fd, &msg, 0) < 0) {
380 zlog_warn("IS-IS pfpacket: could not transmit packet on %s: %s",
381 circuit->interface->name, safe_strerror(errno));
382 if (ERRNO_IO_RETRY(errno))
383 return ISIS_WARNING;
384 return ISIS_ERROR;
385 }
386 return ISIS_OK;
8bc98059
PJ
387}
388
d62a17ae 389int isis_send_pdu_p2p(struct isis_circuit *circuit, int level)
8bc98059 390{
d62a17ae 391 struct sockaddr_ll sa;
392 ssize_t rv;
393
394 stream_set_getp(circuit->snd_stream, 0);
395 memset(&sa, 0, sizeof(struct sockaddr_ll));
396 sa.sll_family = AF_PACKET;
397 sa.sll_ifindex = circuit->interface->ifindex;
398 sa.sll_halen = ETH_ALEN;
399 if (level == 1)
400 memcpy(&sa.sll_addr, ALL_L1_ISS, ETH_ALEN);
401 else
402 memcpy(&sa.sll_addr, ALL_L2_ISS, ETH_ALEN);
403
404
405 /* lets try correcting the protocol */
406 sa.sll_protocol = htons(0x00FE);
407 rv = sendto(circuit->fd, circuit->snd_stream->data,
408 stream_get_endp(circuit->snd_stream), 0,
409 (struct sockaddr *)&sa, sizeof(struct sockaddr_ll));
410 if (rv < 0) {
411 zlog_warn("IS-IS pfpacket: could not transmit packet on %s: %s",
412 circuit->interface->name, safe_strerror(errno));
413 if (ERRNO_IO_RETRY(errno))
414 return ISIS_WARNING;
415 return ISIS_ERROR;
416 }
417 return ISIS_OK;
8bc98059 418}
745bf05f
DL
419
420#endif /* ISIS_METHOD == ISIS_METHOD_PFPACKET */