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