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