]>
git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_pfpacket.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IS-IS Rout(e)ing protocol - isis_pfpacket.c
5 * Copyright (C) 2001,2002 Sampo Saaristo
6 * Tampere University of Technology
7 * Institute of Communications Engineering
11 #if ISIS_METHOD == ISIS_METHOD_PFPACKET
12 #include <net/ethernet.h> /* the L2 protocols */
13 #include <netpacket/packet.h>
15 #include <linux/filter.h>
21 #include "lib_errors.h"
24 #include "isisd/isis_constants.h"
25 #include "isisd/isis_common.h"
26 #include "isisd/isis_circuit.h"
27 #include "isisd/isis_flags.h"
28 #include "isisd/isisd.h"
29 #include "isisd/isis_constants.h"
30 #include "isisd/isis_circuit.h"
31 #include "isisd/isis_network.h"
35 /* tcpdump -i eth0 'isis' -dd */
36 static const struct sock_filter isisfilter
[] = {
37 /* NB: we're in SOCK_DGRAM, so src/dst mac + length are stripped
39 * (OTOH it's a bit more lower-layer agnostic and might work
41 /* { 0x28, 0, 0, 0x0000000c - 14 }, */
42 /* { 0x25, 5, 0, 0x000005dc }, */
43 {0x28, 0, 0, 0x0000000e - 14}, {0x15, 0, 3, 0x0000fefe},
44 {0x30, 0, 0, 0x00000011 - 14}, {0x15, 0, 1, 0x00000083},
45 {0x6, 0, 0, 0x00040000}, {0x6, 0, 0, 0x00000000},
48 static const struct sock_fprog bpf
= {
49 .len
= array_size(isisfilter
),
50 .filter
= (struct sock_filter
*)isisfilter
,
54 * Table 9 - Architectural constants for use with ISO 8802 subnetworks
58 static const uint8_t ALL_L1_ISS
[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x14};
59 static const uint8_t ALL_L2_ISS
[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x15};
60 static const uint8_t ALL_ISS
[6] = {0x09, 0x00, 0x2B, 0x00, 0x00, 0x05};
61 static const uint8_t ALL_ESS
[6] = {0x09, 0x00, 0x2B, 0x00, 0x00, 0x04};
63 static uint8_t discard_buff
[8192];
66 * if level is 0 we are joining p2p multicast
67 * FIXME: and the p2p multicast being ???
69 static int isis_multicast_join(int fd
, int registerto
, int if_num
)
71 struct packet_mreq mreq
;
73 memset(&mreq
, 0, sizeof(mreq
));
74 mreq
.mr_ifindex
= if_num
;
76 mreq
.mr_type
= PACKET_MR_MULTICAST
;
77 mreq
.mr_alen
= ETH_ALEN
;
79 memcpy(&mreq
.mr_address
, ALL_L1_ISS
, ETH_ALEN
);
80 else if (registerto
== 2)
81 memcpy(&mreq
.mr_address
, ALL_L2_ISS
, ETH_ALEN
);
82 else if (registerto
== 3)
83 memcpy(&mreq
.mr_address
, ALL_ISS
, ETH_ALEN
);
85 memcpy(&mreq
.mr_address
, ALL_ESS
, ETH_ALEN
);
88 mreq
.mr_type
= PACKET_MR_ALLMULTI
;
93 "%s: fd=%d, reg_to=%d, if_num=%d, address = %02x:%02x:%02x:%02x:%02x:%02x",
94 __func__
, fd
, registerto
, if_num
, mreq
.mr_address
[0],
95 mreq
.mr_address
[1], mreq
.mr_address
[2],
96 mreq
.mr_address
[3], mreq
.mr_address
[4],
98 #endif /* EXTREME_DEBUG */
99 if (setsockopt(fd
, SOL_PACKET
, PACKET_ADD_MEMBERSHIP
, &mreq
,
100 sizeof(struct packet_mreq
))) {
101 zlog_warn("%s: setsockopt(): %s", __func__
,
102 safe_strerror(errno
));
109 static int open_packet_socket(struct isis_circuit
*circuit
)
111 struct sockaddr_ll s_addr
;
112 int fd
, retval
= ISIS_OK
;
113 struct vrf
*vrf
= NULL
;
115 vrf
= circuit
->interface
->vrf
;
117 fd
= vrf_socket(PF_PACKET
, SOCK_DGRAM
, htons(ETH_P_ALL
), vrf
->vrf_id
,
121 zlog_warn("%s: socket() failed %s", __func__
,
122 safe_strerror(errno
));
126 if (setsockopt(fd
, SOL_SOCKET
, SO_ATTACH_FILTER
, &bpf
, sizeof(bpf
))) {
127 zlog_warn("%s: SO_ATTACH_FILTER failed: %s", __func__
,
128 safe_strerror(errno
));
132 * Bind to the physical interface
134 memset(&s_addr
, 0, sizeof(s_addr
));
135 s_addr
.sll_family
= AF_PACKET
;
136 s_addr
.sll_protocol
= htons(ETH_P_ALL
);
137 s_addr
.sll_ifindex
= circuit
->interface
->ifindex
;
139 if (bind(fd
, (struct sockaddr
*)(&s_addr
), sizeof(struct sockaddr_ll
))
141 zlog_warn("%s: bind() failed: %s", __func__
,
142 safe_strerror(errno
));
149 if (if_is_broadcast(circuit
->interface
)) {
151 * Join to multicast groups
153 * 8.4.2 - Broadcast subnetwork IIH PDUs
154 * FIXME: is there a case only one will fail??
156 /* joining ALL_L1_ISS */
157 retval
|= isis_multicast_join(circuit
->fd
, 1,
158 circuit
->interface
->ifindex
);
159 /* joining ALL_L2_ISS */
160 retval
|= isis_multicast_join(circuit
->fd
, 2,
161 circuit
->interface
->ifindex
);
162 /* joining ALL_ISS (used in RFC 5309 p2p-over-lan as well) */
163 retval
|= isis_multicast_join(circuit
->fd
, 3,
164 circuit
->interface
->ifindex
);
166 retval
= isis_multicast_join(circuit
->fd
, 0,
167 circuit
->interface
->ifindex
);
174 * Create the socket and set the tx/rx funcs
176 int isis_sock_init(struct isis_circuit
*circuit
)
178 int retval
= ISIS_OK
;
180 frr_with_privs(&isisd_privs
) {
182 retval
= open_packet_socket(circuit
);
184 if (retval
!= ISIS_OK
) {
185 zlog_warn("%s: could not initialize the socket",
190 /* Assign Rx and Tx callbacks are based on real if type */
191 if (if_is_broadcast(circuit
->interface
)) {
192 circuit
->tx
= isis_send_pdu_bcast
;
193 circuit
->rx
= isis_recv_pdu_bcast
;
194 } else if (if_is_pointopoint(circuit
->interface
)) {
195 circuit
->tx
= isis_send_pdu_p2p
;
196 circuit
->rx
= isis_recv_pdu_p2p
;
198 zlog_warn("%s: unknown circuit type", __func__
);
199 retval
= ISIS_WARNING
;
207 static inline int llc_check(uint8_t *llc
)
209 if (*llc
!= ISO_SAP
|| *(llc
+ 1) != ISO_SAP
|| *(llc
+ 2) != 3)
215 int isis_recv_pdu_bcast(struct isis_circuit
*circuit
, uint8_t *ssnpa
)
217 int bytesread
, addr_len
;
218 struct sockaddr_ll s_addr
;
219 uint8_t llc
[LLC_LEN
];
221 addr_len
= sizeof(s_addr
);
223 memset(&s_addr
, 0, sizeof(s_addr
));
226 recvfrom(circuit
->fd
, (void *)&llc
, LLC_LEN
, MSG_PEEK
,
227 (struct sockaddr
*)&s_addr
, (socklen_t
*)&addr_len
);
230 || (s_addr
.sll_ifindex
!= (int)circuit
->interface
->ifindex
)) {
233 "%s: ifname %s, fd %d, bytesread %d, recvfrom(): %s",
234 __func__
, circuit
->interface
->name
, circuit
->fd
,
235 bytesread
, safe_strerror(errno
));
237 if (s_addr
.sll_ifindex
!= (int)circuit
->interface
->ifindex
) {
239 "packet is received on multiple interfaces: socket interface %d, circuit interface %d, packet type %u",
240 s_addr
.sll_ifindex
, circuit
->interface
->ifindex
,
244 /* get rid of the packet */
245 bytesread
= recvfrom(circuit
->fd
, discard_buff
,
246 sizeof(discard_buff
), MSG_DONTWAIT
,
247 (struct sockaddr
*)&s_addr
,
248 (socklen_t
*)&addr_len
);
251 zlog_warn("%s: recvfrom() failed", __func__
);
256 * Filtering by llc field, discard packets sent by this host (other
259 if (!llc_check(llc
) || s_addr
.sll_pkttype
== PACKET_OUTGOING
) {
260 /* Read the packet into discard buff */
261 bytesread
= recvfrom(circuit
->fd
, discard_buff
,
262 sizeof(discard_buff
), MSG_DONTWAIT
,
263 (struct sockaddr
*)&s_addr
,
264 (socklen_t
*)&addr_len
);
266 zlog_warn("%s: recvfrom() failed", __func__
);
270 /* Ensure that we have enough space for a pdu padded to fill the mtu */
271 unsigned int max_size
=
272 circuit
->interface
->mtu
> circuit
->interface
->mtu6
273 ? circuit
->interface
->mtu
274 : circuit
->interface
->mtu6
;
275 uint8_t temp_buff
[max_size
];
277 recvfrom(circuit
->fd
, temp_buff
, max_size
, MSG_DONTWAIT
,
278 (struct sockaddr
*)&s_addr
, (socklen_t
*)&addr_len
);
280 zlog_warn("%s: recvfrom() failed", __func__
);
283 /* then we lose the LLC */
284 stream_write(circuit
->rcv_stream
, temp_buff
+ LLC_LEN
,
285 bytesread
- LLC_LEN
);
286 memcpy(ssnpa
, &s_addr
.sll_addr
, s_addr
.sll_halen
);
291 int isis_recv_pdu_p2p(struct isis_circuit
*circuit
, uint8_t *ssnpa
)
293 int bytesread
, addr_len
;
294 struct sockaddr_ll s_addr
;
296 memset(&s_addr
, 0, sizeof(s_addr
));
297 addr_len
= sizeof(s_addr
);
299 /* we can read directly to the stream */
300 (void)stream_recvfrom(
301 circuit
->rcv_stream
, circuit
->fd
, circuit
->interface
->mtu
, 0,
302 (struct sockaddr
*)&s_addr
, (socklen_t
*)&addr_len
);
304 if (s_addr
.sll_pkttype
== PACKET_OUTGOING
) {
305 /* Read the packet into discard buff */
306 bytesread
= recvfrom(circuit
->fd
, discard_buff
,
307 sizeof(discard_buff
), MSG_DONTWAIT
,
308 (struct sockaddr
*)&s_addr
,
309 (socklen_t
*)&addr_len
);
311 zlog_warn("%s: recvfrom() failed", __func__
);
315 /* If we don't have protocol type 0x00FE which is
316 * ISO over GRE we exit with pain :)
318 if (ntohs(s_addr
.sll_protocol
) != 0x00FE) {
319 zlog_warn("%s: protocol mismatch(): %X", __func__
,
320 ntohs(s_addr
.sll_protocol
));
324 memcpy(ssnpa
, &s_addr
.sll_addr
, s_addr
.sll_halen
);
329 int isis_send_pdu_bcast(struct isis_circuit
*circuit
, int level
)
333 char temp_buff
[LLC_LEN
];
335 /* we need to do the LLC in here because of P2P circuits, which will
338 struct sockaddr_ll sa
;
340 stream_set_getp(circuit
->snd_stream
, 0);
341 memset(&sa
, 0, sizeof(sa
));
342 sa
.sll_family
= AF_PACKET
;
344 size_t frame_size
= stream_get_endp(circuit
->snd_stream
) + LLC_LEN
;
345 sa
.sll_protocol
= htons(isis_ethertype(frame_size
));
346 sa
.sll_ifindex
= circuit
->interface
->ifindex
;
347 sa
.sll_halen
= ETH_ALEN
;
348 /* RFC5309 section 4.1 recommends ALL_ISS */
349 if (circuit
->circ_type
== CIRCUIT_T_P2P
)
350 memcpy(&sa
.sll_addr
, ALL_ISS
, ETH_ALEN
);
352 memcpy(&sa
.sll_addr
, ALL_L1_ISS
, ETH_ALEN
);
354 memcpy(&sa
.sll_addr
, ALL_L2_ISS
, ETH_ALEN
);
356 /* on a broadcast circuit */
357 /* first we put the LLC in */
362 memset(&msg
, 0, sizeof(msg
));
364 msg
.msg_namelen
= sizeof(struct sockaddr_ll
);
367 iov
[0].iov_base
= temp_buff
;
368 iov
[0].iov_len
= LLC_LEN
;
369 iov
[1].iov_base
= circuit
->snd_stream
->data
;
370 iov
[1].iov_len
= stream_get_endp(circuit
->snd_stream
);
372 if (sendmsg(circuit
->fd
, &msg
, 0) < 0) {
373 zlog_warn("IS-IS pfpacket: could not transmit packet on %s: %s",
374 circuit
->interface
->name
, safe_strerror(errno
));
375 if (ERRNO_IO_RETRY(errno
))
382 int isis_send_pdu_p2p(struct isis_circuit
*circuit
, int level
)
384 struct sockaddr_ll sa
;
387 stream_set_getp(circuit
->snd_stream
, 0);
388 memset(&sa
, 0, sizeof(sa
));
389 sa
.sll_family
= AF_PACKET
;
390 sa
.sll_ifindex
= circuit
->interface
->ifindex
;
391 sa
.sll_halen
= ETH_ALEN
;
393 memcpy(&sa
.sll_addr
, ALL_L1_ISS
, ETH_ALEN
);
395 memcpy(&sa
.sll_addr
, ALL_L2_ISS
, ETH_ALEN
);
398 /* lets try correcting the protocol */
399 sa
.sll_protocol
= htons(0x00FE);
400 rv
= sendto(circuit
->fd
, circuit
->snd_stream
->data
,
401 stream_get_endp(circuit
->snd_stream
), 0,
402 (struct sockaddr
*)&sa
, sizeof(struct sockaddr_ll
));
404 zlog_warn("IS-IS pfpacket: could not transmit packet on %s: %s",
405 circuit
->interface
->name
, safe_strerror(errno
));
406 if (ERRNO_IO_RETRY(errno
))
413 #endif /* ISIS_METHOD == ISIS_METHOD_PFPACKET */