summaryrefslogtreecommitdiffstats
path: root/isisd/isis_pfpacket.c
blob: af69fac1cd9a0177c37be17d4af7ac51fd224a1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * IS-IS Rout(e)ing protocol - isis_pfpacket.c
 *
 * Copyright (C) 2001,2002    Sampo Saaristo
 *                            Tampere University of Technology
 *                            Institute of Communications Engineering
 */

#include <zebra.h>
#if ISIS_METHOD == ISIS_METHOD_PFPACKET
#include <net/ethernet.h> /* the L2 protocols */
#include <netpacket/packet.h>

#include <linux/filter.h>

#include "log.h"
#include "network.h"
#include "stream.h"
#include "if.h"
#include "lib_errors.h"
#include "vrf.h"

#include "isisd/isis_constants.h"
#include "isisd/isis_common.h"
#include "isisd/isis_circuit.h"
#include "isisd/isis_flags.h"
#include "isisd/isisd.h"
#include "isisd/isis_constants.h"
#include "isisd/isis_circuit.h"
#include "isisd/isis_network.h"

#include "privs.h"

/* tcpdump -i eth0 'isis' -dd */
static const struct sock_filter isisfilter[] = {
	/* NB: we're in SOCK_DGRAM, so src/dst mac + length are stripped
	 * off! */
	/* The following BPF filter accepts IS-IS over LLC and IS-IS over
	 * ethertype 0x00fe.
	 * BPF assembly:
	 * l0: ldh [0]
	 * l1: jeq #0xfefe, l2, l4
	 * l2: ldb [3]
	 * l3: jmp l7
	 * l4: ldh proto
	 * l5: jeq #0x00fe, l6, l9
	 * l6: ldb [0]
	 * l7: jeq #0x83, l8, l9
	 * l8: ret #0x40000
	 * l9: ret #0 */
	{0x28, 0, 0, 0000000000}, {0x15, 0, 2, 0x0000fefe},
	{0x30, 0, 0, 0x00000003}, {0x05, 0, 0, 0x00000003},
	{0x28, 0, 0, 0xfffff000}, {0x15, 0, 3, 0x000000fe},
	{0x30, 0, 0, 0000000000}, {0x15, 0, 1, 0x00000083},
	{0x06, 0, 0, 0x00040000}, {0x06, 0, 0, 0000000000},
};

static const struct sock_fprog bpf = {
	.len = array_size(isisfilter),
	.filter = (struct sock_filter *)isisfilter,
};

/*
 * Table 9 - Architectural constants for use with ISO 8802 subnetworks
 * ISO 10589 - 8.4.8
 */

static const uint8_t ALL_L1_ISS[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x14};
static const uint8_t ALL_L2_ISS[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x15};
static const uint8_t ALL_ISS[6] = {0x09, 0x00, 0x2B, 0x00, 0x00, 0x05};
static const uint8_t ALL_ESS[6] = {0x09, 0x00, 0x2B, 0x00, 0x00, 0x04};

static uint8_t discard_buff[8192];

/*
 * if level is 0 we are joining p2p multicast
 * FIXME: and the p2p multicast being ???
 */
static int isis_multicast_join(int fd, int registerto, int if_num)
{
	struct packet_mreq mreq;

	memset(&mreq, 0, sizeof(mreq));
	mreq.mr_ifindex = if_num;
	if (registerto) {
		mreq.mr_type = PACKET_MR_MULTICAST;
		mreq.mr_alen = ETH_ALEN;
		if (registerto == 1)
			memcpy(&mreq.mr_address, ALL_L1_ISS, ETH_ALEN);
		else if (registerto == 2)
			memcpy(&mreq.mr_address, ALL_L2_ISS, ETH_ALEN);
		else if (registerto == 3)
			memcpy(&mreq.mr_address, ALL_ISS, ETH_ALEN);
		else
			memcpy(&mreq.mr_address, ALL_ESS, ETH_ALEN);

	} else {
		mreq.mr_type = PACKET_MR_ALLMULTI;
	}
#ifdef EXTREME_DEBUG
	if (IS_DEBUG_EVENTS)
		zlog_debug(
			"%s: fd=%d, reg_to=%d, if_num=%d, address = %02x:%02x:%02x:%02x:%02x:%02x",
			__func__, fd, registerto, if_num, mreq.mr_address[0],
			mreq.mr_address[1], mreq.mr_address[2],
			mreq.mr_address[3], mreq.mr_address[4],
			mreq.mr_address[5]);
#endif /* EXTREME_DEBUG */
	if (setsockopt(fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq,
		       sizeof(struct packet_mreq))) {
		zlog_warn("%s: setsockopt(): %s", __func__,
			  safe_strerror(errno));
		return ISIS_WARNING;
	}

	return ISIS_OK;
}

static int open_packet_socket(struct isis_circuit *circuit)
{
	struct sockaddr_ll s_addr;
	int fd, retval = ISIS_OK;
	struct vrf *vrf = NULL;

	vrf = circuit->interface->vrf;

	fd = vrf_socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL), vrf->vrf_id,
			vrf->name);

	if (fd < 0) {
		zlog_warn("%s: socket() failed %s", __func__,
			  safe_strerror(errno));
		return ISIS_WARNING;
	}

	if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf))) {
		zlog_warn("%s: SO_ATTACH_FILTER failed: %s", __func__,
			  safe_strerror(errno));
	}

	/*
	 * Bind to the physical interface
	 */
	memset(&s_addr, 0, sizeof(s_addr));
	s_addr.sll_family = AF_PACKET;
	s_addr.sll_protocol = htons(ETH_P_ALL);
	s_addr.sll_ifindex = circuit->interface->ifindex;

	if (bind(fd, (struct sockaddr *)(&s_addr), sizeof(struct sockaddr_ll))
	    < 0) {
		zlog_warn("%s: bind() failed: %s", __func__,
			  safe_strerror(errno));
		close(fd);
		return ISIS_WARNING;
	}

	circuit->fd = fd;

	if (if_is_broadcast(circuit->interface)) {
		/*
		 * Join to multicast groups
		 * according to
		 * 8.4.2 - Broadcast subnetwork IIH PDUs
		 * FIXME: is there a case only one will fail??
		 */
		/* joining ALL_L1_ISS */
		retval |= isis_multicast_join(circuit->fd, 1,
					      circuit->interface->ifindex);
		/* joining ALL_L2_ISS */
		retval |= isis_multicast_join(circuit->fd, 2,
					      circuit->interface->ifindex);
		/* joining ALL_ISS (used in RFC 5309 p2p-over-lan as well) */
		retval |= isis_multicast_join(circuit->fd, 3,
					      circuit->interface->ifindex);
	} else {
		retval = isis_multicast_join(circuit->fd, 0,
					     circuit->interface->ifindex);
	}

	return retval;
}

/*
 * Create the socket and set the tx/rx funcs
 */
int isis_sock_init(struct isis_circuit *circuit)
{
	int retval = ISIS_OK;

	frr_with_privs(&isisd_privs) {

		retval = open_packet_socket(circuit);

		if (retval != ISIS_OK) {
			zlog_warn("%s: could not initialize the socket",
				  __func__);
			break;
		}

	/* Assign Rx and Tx callbacks are based on real if type */
		if (if_is_broadcast(circuit->interface)) {
			circuit->tx = isis_send_pdu_bcast;
			circuit->rx = isis_recv_pdu_bcast;
		} else if (if_is_pointopoint(circuit->interface)) {
			circuit->tx = isis_send_pdu_p2p;
			circuit->rx = isis_recv_pdu_p2p;
		} else {
			zlog_warn("%s: unknown circuit type", __func__);
			retval = ISIS_WARNING;
			break;
		}
	}

	return retval;
}

static inline int llc_check(uint8_t *llc)
{
	if (*llc != ISO_SAP || *(llc + 1) != ISO_SAP || *(llc + 2) != 3)
		return 0;

	return 1;
}

int isis_recv_pdu_bcast(struct isis_circuit *circuit, uint8_t *ssnpa)
{
	int bytesread, addr_len;
	struct sockaddr_ll s_addr;
	uint8_t llc[LLC_LEN];

	addr_len = sizeof(s_addr);

	memset(&s_addr, 0, sizeof(s_addr));

	bytesread =
		recvfrom(circuit->fd, (void *)&llc, LLC_LEN, MSG_PEEK,
			 (struct sockaddr *)&s_addr, (socklen_t *)&addr_len);

	if ((bytesread < 0)
	    || (s_addr.sll_ifindex != (int)circuit->interface->ifindex)) {
		if (bytesread < 0) {
			zlog_warn(
				"%s: ifname %s, fd %d, bytesread %d, recvfrom(): %s",
				__func__, circuit->interface->name, circuit->fd,
				bytesread, safe_strerror(errno));
		}
		if (s_addr.sll_ifindex != (int)circuit->interface->ifindex) {
			zlog_warn(
				"packet is received on multiple interfaces: socket interface %d, circuit interface %d, packet type %u",
				s_addr.sll_ifindex, circuit->interface->ifindex,
				s_addr.sll_pkttype);
		}

		/* get rid of the packet */
		bytesread = recvfrom(circuit->fd, discard_buff,
				     sizeof(discard_buff), MSG_DONTWAIT,
				     (struct sockaddr *)&s_addr,
				     (socklen_t *)&addr_len);

		if (bytesread < 0)
			zlog_warn("%s: recvfrom() failed", __func__);

		return ISIS_WARNING;
	}
	/*
	 * Filtering by llc field, discard packets sent by this host (other
	 * circuit)
	 */
	if (!llc_check(llc) || s_addr.sll_pkttype == PACKET_OUTGOING) {
		/*  Read the packet into discard buff */
		bytesread = recvfrom(circuit->fd, discard_buff,
				     sizeof(discard_buff), MSG_DONTWAIT,
				     (struct sockaddr *)&s_addr,
				     (socklen_t *)&addr_len);
		if (bytesread < 0)
			zlog_warn("%s: recvfrom() failed", __func__);
		return ISIS_WARNING;
	}

	/* Ensure that we have enough space for a pdu padded to fill the mtu */
	unsigned int max_size =
		circuit->interface->mtu > circuit->interface->mtu6
			? circuit->interface->mtu
			: circuit->interface->mtu6;
	uint8_t temp_buff[max_size];
	bytesread =
		recvfrom(circuit->fd, temp_buff, max_size, MSG_DONTWAIT,
			 (struct sockaddr *)&s_addr, (socklen_t *)&addr_len);
	if (bytesread < 0) {
		zlog_warn("%s: recvfrom() failed", __func__);
		return ISIS_WARNING;
	}
	/* then we lose the LLC */
	stream_write(circuit->rcv_stream, temp_buff + LLC_LEN,
		     bytesread - LLC_LEN);
	memcpy(ssnpa, &s_addr.sll_addr, s_addr.sll_halen);

	return ISIS_OK;
}

int isis_recv_pdu_p2p(struct isis_circuit *circuit, uint8_t *ssnpa)
{
	int bytesread, addr_len;
	struct sockaddr_ll s_addr;

	memset(&s_addr, 0, sizeof(s_addr));
	addr_len = sizeof(s_addr);

	/* we can read directly to the stream */
	(void)stream_recvfrom(
		circuit->rcv_stream, circuit->fd, circuit->interface->mtu, 0,
		(struct sockaddr *)&s_addr, (socklen_t *)&addr_len);

	if (s_addr.sll_pkttype == PACKET_OUTGOING) {
		/*  Read the packet into discard buff */
		bytesread = recvfrom(circuit->fd, discard_buff,
				     sizeof(discard_buff), MSG_DONTWAIT,
				     (struct sockaddr *)&s_addr,
				     (socklen_t *)&addr_len);
		if (bytesread < 0)
			zlog_warn("%s: recvfrom() failed", __func__);
		return ISIS_WARNING;
	}

	/* If we don't have protocol type 0x00FE which is
	 * ISO over GRE we exit with pain :)
	 */
	if (ntohs(s_addr.sll_protocol) != 0x00FE) {
		zlog_warn("%s: protocol mismatch(): %X", __func__,
			  ntohs(s_addr.sll_protocol));
		return ISIS_WARNING;
	}

	memcpy(ssnpa, &s_addr.sll_addr, s_addr.sll_halen);

	return ISIS_OK;
}

int isis_send_pdu_bcast(struct isis_circuit *circuit, int level)
{
	struct msghdr msg;
	struct iovec iov[2];
	char temp_buff[LLC_LEN];

	/* we need to do the LLC in here because of P2P circuits, which will
	 * not need it
	 */
	struct sockaddr_ll sa;

	stream_set_getp(circuit->snd_stream, 0);
	memset(&sa, 0, sizeof(sa));
	sa.sll_family = AF_PACKET;

	size_t frame_size = stream_get_endp(circuit->snd_stream) + LLC_LEN;
	sa.sll_protocol = htons(isis_ethertype(frame_size));
	sa.sll_ifindex = circuit->interface->ifindex;
	sa.sll_halen = ETH_ALEN;
	/* RFC5309 section 4.1 recommends ALL_ISS */
	if (circuit->circ_type == CIRCUIT_T_P2P)
		memcpy(&sa.sll_addr, ALL_ISS, ETH_ALEN);
	else if (level == 1)
		memcpy(&sa.sll_addr, ALL_L1_ISS, ETH_ALEN);
	else
		memcpy(&sa.sll_addr, ALL_L2_ISS, ETH_ALEN);

	/* on a broadcast circuit */
	/* first we put the LLC in */
	temp_buff[0] = 0xFE;
	temp_buff[1] = 0xFE;
	temp_buff[2] = 0x03;

	memset(&msg, 0, sizeof(msg));
	msg.msg_name = &sa;
	msg.msg_namelen = sizeof(struct sockaddr_ll);
	msg.msg_iov = iov;
	msg.msg_iovlen = 2;
	iov[0].iov_base = temp_buff;
	iov[0].iov_len = LLC_LEN;
	iov[1].iov_base = circuit->snd_stream->data;
	iov[1].iov_len = stream_get_endp(circuit->snd_stream);

	if (sendmsg(circuit->fd, &msg, 0) < 0) {
		zlog_warn("IS-IS pfpacket: could not transmit packet on %s: %s",
			  circuit->interface->name, safe_strerror(errno));
		if (ERRNO_IO_RETRY(errno))
			return ISIS_WARNING;
		return ISIS_ERROR;
	}
	return ISIS_OK;
}

int isis_send_pdu_p2p(struct isis_circuit *circuit, int level)
{
	struct sockaddr_ll sa;
	ssize_t rv;

	stream_set_getp(circuit->snd_stream, 0);
	memset(&sa, 0, sizeof(sa));
	sa.sll_family = AF_PACKET;
	sa.sll_ifindex = circuit->interface->ifindex;
	sa.sll_halen = ETH_ALEN;
	if (level == 1)
		memcpy(&sa.sll_addr, ALL_L1_ISS, ETH_ALEN);
	else
		memcpy(&sa.sll_addr, ALL_L2_ISS, ETH_ALEN);


	/* lets try correcting the protocol */
	sa.sll_protocol = htons(0x00FE);
	rv = sendto(circuit->fd, circuit->snd_stream->data,
		    stream_get_endp(circuit->snd_stream), 0,
		    (struct sockaddr *)&sa, sizeof(struct sockaddr_ll));
	if (rv < 0) {
		zlog_warn("IS-IS pfpacket: could not transmit packet on %s: %s",
			  circuit->interface->name, safe_strerror(errno));
		if (ERRNO_IO_RETRY(errno))
			return ISIS_WARNING;
		return ISIS_ERROR;
	}
	return ISIS_OK;
}

#endif /* ISIS_METHOD == ISIS_METHOD_PFPACKET */