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