blob: a9ecd40ffdfdcd9f3f2ab4890db8c6f770fd0316 [file] [log] [blame]
Paul Jakma8bc98052007-08-08 11:19:27 +00001/*
2 * IS-IS Rout(e)ing protocol - isis_pfpacket.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>
David Lamparter745bf052010-02-02 21:49:35 +010024#if ISIS_METHOD == ISIS_METHOD_PFPACKET
Paul Jakma8bc98052007-08-08 11:19:27 +000025#include <net/ethernet.h> /* the L2 protocols */
26#include <netpacket/packet.h>
27
28#include "log.h"
29#include "stream.h"
30#include "if.h"
31
32#include "isisd/dict.h"
33#include "isisd/include-netbsd/iso.h"
34#include "isisd/isis_constants.h"
35#include "isisd/isis_common.h"
36#include "isisd/isis_circuit.h"
37#include "isisd/isis_flags.h"
38#include "isisd/isisd.h"
39#include "isisd/isis_constants.h"
40#include "isisd/isis_circuit.h"
41#include "isisd/isis_network.h"
42
43#include "privs.h"
44
45extern struct zebra_privs_t isisd_privs;
46
47/*
48 * Table 9 - Architectural constants for use with ISO 8802 subnetworks
49 * ISO 10589 - 8.4.8
50 */
51
52u_char ALL_L1_ISS[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x14 };
53u_char ALL_L2_ISS[6] = { 0x01, 0x80, 0xC2, 0x00, 0x00, 0x15 };
54u_char ALL_ISS[6] = { 0x09, 0x00, 0x2B, 0x00, 0x00, 0x05 };
55u_char ALL_ESS[6] = { 0x09, 0x00, 0x2B, 0x00, 0x00, 0x04 };
56
David Lamparter21401f32015-03-03 08:55:26 +010057static uint8_t discard_buff[8192];
58static uint8_t sock_buff[8192];
Paul Jakma8bc98052007-08-08 11:19:27 +000059
60/*
61 * if level is 0 we are joining p2p multicast
62 * FIXME: and the p2p multicast being ???
63 */
64static int
65isis_multicast_join (int fd, int registerto, int if_num)
66{
67 struct packet_mreq mreq;
68
69 memset (&mreq, 0, sizeof (mreq));
70 mreq.mr_ifindex = if_num;
71 if (registerto)
72 {
73 mreq.mr_type = PACKET_MR_MULTICAST;
74 mreq.mr_alen = ETH_ALEN;
75 if (registerto == 1)
76 memcpy (&mreq.mr_address, ALL_L1_ISS, ETH_ALEN);
77 else if (registerto == 2)
78 memcpy (&mreq.mr_address, ALL_L2_ISS, ETH_ALEN);
79 else if (registerto == 3)
80 memcpy (&mreq.mr_address, ALL_ISS, ETH_ALEN);
81 else
82 memcpy (&mreq.mr_address, ALL_ESS, ETH_ALEN);
83
84 }
85 else
86 {
87 mreq.mr_type = PACKET_MR_ALLMULTI;
88 }
89#ifdef EXTREME_DEBUG
90 zlog_debug ("isis_multicast_join(): fd=%d, reg_to=%d, if_num=%d, "
91 "address = %02x:%02x:%02x:%02x:%02x:%02x",
92 fd, registerto, if_num, mreq.mr_address[0], mreq.mr_address[1],
93 mreq.mr_address[2], mreq.mr_address[3], mreq.mr_address[4],
94 mreq.mr_address[5]);
95#endif /* EXTREME_DEBUG */
96 if (setsockopt (fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq,
97 sizeof (struct packet_mreq)))
98 {
99 zlog_warn ("isis_multicast_join(): setsockopt(): %s", safe_strerror (errno));
100 return ISIS_WARNING;
101 }
102
103 return ISIS_OK;
104}
105
106static int
107open_packet_socket (struct isis_circuit *circuit)
108{
109 struct sockaddr_ll s_addr;
110 int fd, retval = ISIS_OK;
111
112 fd = socket (PF_PACKET, SOCK_DGRAM, htons (ETH_P_ALL));
113 if (fd < 0)
114 {
115 zlog_warn ("open_packet_socket(): socket() failed %s",
116 safe_strerror (errno));
117 return ISIS_WARNING;
118 }
119
120 /*
121 * Bind to the physical interface
122 */
123 memset (&s_addr, 0, sizeof (struct sockaddr_ll));
124 s_addr.sll_family = AF_PACKET;
125 s_addr.sll_protocol = htons (ETH_P_ALL);
126 s_addr.sll_ifindex = circuit->interface->ifindex;
127
128 if (bind (fd, (struct sockaddr *) (&s_addr),
129 sizeof (struct sockaddr_ll)) < 0)
130 {
131 zlog_warn ("open_packet_socket(): bind() failed: %s", safe_strerror (errno));
David Lampartere8aca322012-11-27 01:10:30 +0000132 close (fd);
Paul Jakma8bc98052007-08-08 11:19:27 +0000133 return ISIS_WARNING;
134 }
135
136 circuit->fd = fd;
137
Josh Bailey3f045a02012-03-24 08:35:20 -0700138 if (if_is_broadcast (circuit->interface))
Paul Jakma8bc98052007-08-08 11:19:27 +0000139 {
140 /*
141 * Join to multicast groups
142 * according to
143 * 8.4.2 - Broadcast subnetwork IIH PDUs
144 * FIXME: is there a case only one will fail??
145 */
David Lamparter318c8042012-11-27 01:10:29 +0000146 /* joining ALL_L1_ISS */
147 retval |= isis_multicast_join (circuit->fd, 1,
Josh Bailey3f045a02012-03-24 08:35:20 -0700148 circuit->interface->ifindex);
David Lamparter318c8042012-11-27 01:10:29 +0000149 /* joining ALL_L2_ISS */
150 retval |= isis_multicast_join (circuit->fd, 2,
Josh Bailey3f045a02012-03-24 08:35:20 -0700151 circuit->interface->ifindex);
152 /* joining ALL_ISS (used in RFC 5309 p2p-over-lan as well) */
David Lamparter318c8042012-11-27 01:10:29 +0000153 retval |= isis_multicast_join (circuit->fd, 3,
Josh Bailey3f045a02012-03-24 08:35:20 -0700154 circuit->interface->ifindex);
Paul Jakma8bc98052007-08-08 11:19:27 +0000155 }
156 else
157 {
158 retval =
Josh Bailey3f045a02012-03-24 08:35:20 -0700159 isis_multicast_join (circuit->fd, 0, circuit->interface->ifindex);
Paul Jakma8bc98052007-08-08 11:19:27 +0000160 }
161
162 return retval;
163}
164
165/*
166 * Create the socket and set the tx/rx funcs
167 */
168int
169isis_sock_init (struct isis_circuit *circuit)
170{
171 int retval = ISIS_OK;
172
173 if (isisd_privs.change (ZPRIVS_RAISE))
174 zlog_err ("%s: could not raise privs, %s", __func__, safe_strerror (errno));
175
176 retval = open_packet_socket (circuit);
177
178 if (retval != ISIS_OK)
179 {
180 zlog_warn ("%s: could not initialize the socket", __func__);
181 goto end;
182 }
183
Josh Bailey3f045a02012-03-24 08:35:20 -0700184 /* Assign Rx and Tx callbacks are based on real if type */
185 if (if_is_broadcast (circuit->interface))
Paul Jakma8bc98052007-08-08 11:19:27 +0000186 {
187 circuit->tx = isis_send_pdu_bcast;
188 circuit->rx = isis_recv_pdu_bcast;
189 }
Josh Bailey3f045a02012-03-24 08:35:20 -0700190 else if (if_is_pointopoint (circuit->interface))
Paul Jakma8bc98052007-08-08 11:19:27 +0000191 {
192 circuit->tx = isis_send_pdu_p2p;
193 circuit->rx = isis_recv_pdu_p2p;
194 }
195 else
196 {
197 zlog_warn ("isis_sock_init(): unknown circuit type");
198 retval = ISIS_WARNING;
199 goto end;
200 }
201
202end:
203 if (isisd_privs.change (ZPRIVS_LOWER))
204 zlog_err ("%s: could not lower privs, %s", __func__, safe_strerror (errno));
205
206 return retval;
207}
208
209static inline int
210llc_check (u_char * llc)
211{
212 if (*llc != ISO_SAP || *(llc + 1) != ISO_SAP || *(llc + 2) != 3)
213 return 0;
214
215 return 1;
216}
217
218int
219isis_recv_pdu_bcast (struct isis_circuit *circuit, u_char * ssnpa)
220{
221 int bytesread, addr_len;
222 struct sockaddr_ll s_addr;
223 u_char llc[LLC_LEN];
224
225 addr_len = sizeof (s_addr);
226
227 memset (&s_addr, 0, sizeof (struct sockaddr_ll));
228
229 bytesread = recvfrom (circuit->fd, (void *) &llc,
230 LLC_LEN, MSG_PEEK,
231 (struct sockaddr *) &s_addr, (socklen_t *) &addr_len);
232
Amritha Nambiaree162612015-10-13 22:08:46 -0700233 if ((bytesread < 0) || (s_addr.sll_ifindex != (int)circuit->interface->ifindex))
Paul Jakma8bc98052007-08-08 11:19:27 +0000234 {
Amritha Nambiaree162612015-10-13 22:08:46 -0700235 if (bytesread < 0)
236 {
237 zlog_warn ("isis_recv_packet_bcast(): ifname %s, fd %d, "
238 "bytesread %d, recvfrom(): %s",
239 circuit->interface->name, circuit->fd, bytesread,
240 safe_strerror (errno));
241 }
242 if (s_addr.sll_ifindex != (int)circuit->interface->ifindex)
243 {
244 zlog_warn("packet is received on multiple interfaces: "
245 "socket interface %d, circuit interface %d, "
246 "packet type %u",
247 s_addr.sll_ifindex, circuit->interface->ifindex,
248 s_addr.sll_pkttype);
249 }
250
Paul Jakma8bc98052007-08-08 11:19:27 +0000251 /* get rid of the packet */
Josh Bailey3f045a02012-03-24 08:35:20 -0700252 bytesread = recvfrom (circuit->fd, discard_buff, sizeof (discard_buff),
253 MSG_DONTWAIT, (struct sockaddr *) &s_addr,
254 (socklen_t *) &addr_len);
Paul Jakma8bc98052007-08-08 11:19:27 +0000255 return ISIS_WARNING;
256 }
257 /*
258 * Filtering by llc field, discard packets sent by this host (other circuit)
259 */
260 if (!llc_check (llc) || s_addr.sll_pkttype == PACKET_OUTGOING)
261 {
262 /* Read the packet into discard buff */
Josh Bailey3f045a02012-03-24 08:35:20 -0700263 bytesread = recvfrom (circuit->fd, discard_buff, sizeof (discard_buff),
264 MSG_DONTWAIT, (struct sockaddr *) &s_addr,
265 (socklen_t *) &addr_len);
Paul Jakma8bc98052007-08-08 11:19:27 +0000266 if (bytesread < 0)
Josh Bailey3f045a02012-03-24 08:35:20 -0700267 zlog_warn ("isis_recv_pdu_bcast(): recvfrom() failed");
Paul Jakma8bc98052007-08-08 11:19:27 +0000268 return ISIS_WARNING;
269 }
270
271 /* on lan we have to read to the static buff first */
Josh Bailey3f045a02012-03-24 08:35:20 -0700272 bytesread = recvfrom (circuit->fd, sock_buff, sizeof (sock_buff), MSG_DONTWAIT,
Paul Jakma8bc98052007-08-08 11:19:27 +0000273 (struct sockaddr *) &s_addr, (socklen_t *) &addr_len);
Josh Bailey3f045a02012-03-24 08:35:20 -0700274 if (bytesread < 0)
275 {
276 zlog_warn ("isis_recv_pdu_bcast(): recvfrom() failed");
277 return ISIS_WARNING;
278 }
Paul Jakma8bc98052007-08-08 11:19:27 +0000279
280 /* then we lose the LLC */
281 stream_write (circuit->rcv_stream, sock_buff + LLC_LEN, bytesread - LLC_LEN);
282
283 memcpy (ssnpa, &s_addr.sll_addr, s_addr.sll_halen);
284
285 return ISIS_OK;
286}
287
288int
289isis_recv_pdu_p2p (struct isis_circuit *circuit, u_char * ssnpa)
290{
291 int bytesread, addr_len;
292 struct sockaddr_ll s_addr;
293
294 memset (&s_addr, 0, sizeof (struct sockaddr_ll));
295 addr_len = sizeof (s_addr);
296
297 /* we can read directly to the stream */
298 bytesread = stream_recvfrom (circuit->rcv_stream, circuit->fd,
299 circuit->interface->mtu, 0,
300 (struct sockaddr *) &s_addr,
301 (socklen_t *) &addr_len);
302
303 if (s_addr.sll_pkttype == PACKET_OUTGOING)
304 {
305 /* Read the packet into discard buff */
Josh Bailey3f045a02012-03-24 08:35:20 -0700306 bytesread = recvfrom (circuit->fd, discard_buff, sizeof (discard_buff),
307 MSG_DONTWAIT, (struct sockaddr *) &s_addr,
308 (socklen_t *) &addr_len);
Paul Jakma8bc98052007-08-08 11:19:27 +0000309 if (bytesread < 0)
Josh Bailey3f045a02012-03-24 08:35:20 -0700310 zlog_warn ("isis_recv_pdu_p2p(): recvfrom() failed");
Paul Jakma8bc98052007-08-08 11:19:27 +0000311 return ISIS_WARNING;
312 }
313
314 /* If we don't have protocol type 0x00FE which is
315 * ISO over GRE we exit with pain :)
316 */
317 if (ntohs (s_addr.sll_protocol) != 0x00FE)
318 {
319 zlog_warn ("isis_recv_pdu_p2p(): protocol mismatch(): %X",
320 ntohs (s_addr.sll_protocol));
321 return ISIS_WARNING;
322 }
323
324 memcpy (ssnpa, &s_addr.sll_addr, s_addr.sll_halen);
325
326 return ISIS_OK;
327}
328
329int
330isis_send_pdu_bcast (struct isis_circuit *circuit, int level)
331{
Josh Bailey3f045a02012-03-24 08:35:20 -0700332 struct msghdr msg;
333 struct iovec iov[2];
334
Paul Jakma8bc98052007-08-08 11:19:27 +0000335 /* we need to do the LLC in here because of P2P circuits, which will
336 * not need it
337 */
Paul Jakma8bc98052007-08-08 11:19:27 +0000338 struct sockaddr_ll sa;
339
340 stream_set_getp (circuit->snd_stream, 0);
341 memset (&sa, 0, sizeof (struct sockaddr_ll));
342 sa.sll_family = AF_PACKET;
343 sa.sll_protocol = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN);
344 sa.sll_ifindex = circuit->interface->ifindex;
345 sa.sll_halen = ETH_ALEN;
Josh Bailey3f045a02012-03-24 08:35:20 -0700346 /* RFC5309 section 4.1 recommends ALL_ISS */
347 if (circuit->circ_type == CIRCUIT_T_P2P)
348 memcpy (&sa.sll_addr, ALL_ISS, ETH_ALEN);
349 else if (level == 1)
Paul Jakma8bc98052007-08-08 11:19:27 +0000350 memcpy (&sa.sll_addr, ALL_L1_ISS, ETH_ALEN);
351 else
352 memcpy (&sa.sll_addr, ALL_L2_ISS, ETH_ALEN);
353
354 /* on a broadcast circuit */
355 /* first we put the LLC in */
356 sock_buff[0] = 0xFE;
357 sock_buff[1] = 0xFE;
358 sock_buff[2] = 0x03;
359
Josh Bailey3f045a02012-03-24 08:35:20 -0700360 memset (&msg, 0, sizeof (msg));
361 msg.msg_name = &sa;
362 msg.msg_namelen = sizeof (struct sockaddr_ll);
363 msg.msg_iov = iov;
364 msg.msg_iovlen = 2;
365 iov[0].iov_base = sock_buff;
366 iov[0].iov_len = LLC_LEN;
367 iov[1].iov_base = circuit->snd_stream->data;
368 iov[1].iov_len = stream_get_endp (circuit->snd_stream);
Paul Jakma8bc98052007-08-08 11:19:27 +0000369
David Lamparterf50ee932015-03-04 07:13:38 +0100370 sendmsg (circuit->fd, &msg, 0);
Paul Jakma8bc98052007-08-08 11:19:27 +0000371
372 return ISIS_OK;
373}
374
375int
376isis_send_pdu_p2p (struct isis_circuit *circuit, int level)
377{
Paul Jakma8bc98052007-08-08 11:19:27 +0000378 struct sockaddr_ll sa;
379
380 stream_set_getp (circuit->snd_stream, 0);
381 memset (&sa, 0, sizeof (struct sockaddr_ll));
382 sa.sll_family = AF_PACKET;
383 sa.sll_protocol = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN);
384 sa.sll_ifindex = circuit->interface->ifindex;
385 sa.sll_halen = ETH_ALEN;
386 if (level == 1)
387 memcpy (&sa.sll_addr, ALL_L1_ISS, ETH_ALEN);
388 else
389 memcpy (&sa.sll_addr, ALL_L2_ISS, ETH_ALEN);
390
391
392 /* lets try correcting the protocol */
393 sa.sll_protocol = htons (0x00FE);
David Lamparterf50ee932015-03-04 07:13:38 +0100394 sendto (circuit->fd, circuit->snd_stream->data,
Paul Jakma8bc98052007-08-08 11:19:27 +0000395 stream_get_endp (circuit->snd_stream), 0,
396 (struct sockaddr *) &sa,
397 sizeof (struct sockaddr_ll));
398
399 return ISIS_OK;
400}
David Lamparter745bf052010-02-02 21:49:35 +0100401
402#endif /* ISIS_METHOD == ISIS_METHOD_PFPACKET */