blob: d22f3dd8a645b913dfca6018238a9caa745426d1 [file] [log] [blame]
jardineb5d44e2003-12-23 08:09:43 +00001/*
2 * IS-IS Rout(e)ing protocol - isis_network.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 <sys/types.h>
24#include <sys/socket.h>
25#include <errno.h>
26#include <zebra.h>
27#include <net/ethernet.h> /* the L2 protocols */
28
29#include "log.h"
30#include "stream.h"
31#include "if.h"
32
33
34#include "isisd/dict.h"
35#include "isisd/include-netbsd/iso.h"
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/*
46 * On linux we can use the packet(7) sockets, in other OSs we have to do with
47 * Berkley Packet Filter (BPF). Please tell me if you can think of a better
48 * way...
49 */
50#ifdef GNU_LINUX
51#include <netpacket/packet.h>
52#else
53#include <sys/time.h>
54#include <sys/ioctl.h>
55#include <net/bpf.h>
56struct bpf_insn llcfilter[] = {
57 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, ETHER_HDR_LEN), /* check first byte */
58 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ISO_SAP, 0, 5),
59 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, ETHER_HDR_LEN+1),
60 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ISO_SAP, 0, 3), /* check second byte */
61 BPF_STMT(BPF_LD+BPF_B+BPF_ABS, ETHER_HDR_LEN+2),
62 BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, 0x03, 0, 1), /* check third byte */
63 BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
64 BPF_STMT(BPF_RET+BPF_K, 0)
65};
66int readblen = 0;
67u_char *readbuff = NULL;
68#endif /* GNU_LINUX */
69
70/*
71 * Table 9 - Architectural constans for use with ISO 8802 subnetworks
72 * ISO 10589 - 8.4.8
73 */
74
75u_char ALL_L1_ISS[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x14};
76u_char ALL_L2_ISS[6] = {0x01, 0x80, 0xC2, 0x00, 0x00, 0x15};
77u_char ALL_ISS[6] = {0x09, 0x00, 0x2B, 0x00, 0x00, 0x05};
78u_char ALL_ESS[6] = {0x09, 0x00, 0x2B, 0x00, 0x00, 0x04};
79
80#ifdef GNU_LINUX
81static char discard_buff[8192];
82#endif
83static char sock_buff[8192];
84
85/*
86 * if level is 0 we are joining p2p multicast
87 * FIXME: and the p2p multicast being ???
88 */
89#ifdef GNU_LINUX
90int
91isis_multicast_join (int fd, int registerto, int if_num)
92{
93 struct packet_mreq mreq;
94
95 memset(&mreq, 0, sizeof(mreq));
96 mreq.mr_ifindex = if_num;
97 if (registerto) {
98 mreq.mr_type = PACKET_MR_MULTICAST;
99 mreq.mr_alen = ETH_ALEN;
100 if (registerto == 1)
101 memcpy (&mreq.mr_address, ALL_L1_ISS, ETH_ALEN);
102 else if (registerto == 2)
103 memcpy (&mreq.mr_address, ALL_L2_ISS, ETH_ALEN);
104 else if (registerto == 3)
105 memcpy (&mreq.mr_address, ALL_ISS, ETH_ALEN);
106 else
107 memcpy (&mreq.mr_address, ALL_ESS, ETH_ALEN);
108
109 } else {
110 mreq.mr_type = PACKET_MR_ALLMULTI;
111 }
112#ifdef EXTREME_DEBUG
113 zlog_info ("isis_multicast_join(): fd=%d, reg_to=%d, if_num=%d, "
114 "address = %02x:%02x:%02x:%02x:%02x:%02x",
115 fd, registerto, if_num, mreq.mr_address[0], mreq.mr_address[1],
116 mreq.mr_address[2], mreq.mr_address[3],mreq.mr_address[4],
117 mreq.mr_address[5]);
118#endif /* EXTREME_DEBUG */
119 if (setsockopt (fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq,
120 sizeof (struct packet_mreq))) {
121 zlog_warn ("isis_multicast_join(): setsockopt(): %s", strerror (errno));
122 return ISIS_WARNING;
123 }
124
125 return ISIS_OK;
126}
127
128int
129open_packet_socket (struct isis_circuit *circuit)
130{
131 struct sockaddr_ll s_addr;
132 int fd, retval = ISIS_OK;
133
134 fd = socket (PF_PACKET, SOCK_DGRAM, htons (ETH_P_ALL));
135 if (fd < 0) {
136 zlog_warn ("open_packet_socket(): socket() failed %s", strerror (errno));
137 return ISIS_WARNING;
138 }
139
140 /*
141 * Bind to the physical interface
142 */
143 memset(&s_addr, 0, sizeof (struct sockaddr_ll));
144 s_addr.sll_family = AF_PACKET;
145 s_addr.sll_protocol = htons (ETH_P_ALL);
146 s_addr.sll_ifindex = circuit->interface->ifindex;
147
148 if (bind (fd, (struct sockaddr*) (&s_addr),
149 sizeof(struct sockaddr_ll)) < 0) {
150 zlog_warn ("open_packet_socket(): bind() failed: %s", strerror(errno));
151 return ISIS_WARNING;
152 }
153
154 circuit->fd = fd;
155
156 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
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 if (circuit->circuit_is_type & IS_LEVEL_1) {
164 /* joining ALL_L1_ISS */
165 retval = isis_multicast_join (circuit->fd, 1,
166 circuit->interface->ifindex);
167 /* joining ALL_ISS */
168 retval = isis_multicast_join (circuit->fd, 3,
169 circuit->interface->ifindex);
170 }
171 if (circuit->circuit_is_type & IS_LEVEL_2)
172 /* joining ALL_L2_ISS */
173 retval = isis_multicast_join (circuit->fd, 2,
174 circuit->interface->ifindex);
175 } else {
176 retval = isis_multicast_join (circuit->fd, 0, circuit->interface->ifindex);
177 }
178
179 return retval;
180}
181
182#else
183
184int
185open_bpf_dev (struct isis_circuit *circuit)
186{
187 int i = 0, fd;
188 char bpfdev[128];
189 struct ifreq ifr;
190 u_int16_t blen;
191 int true = 1, false = 0;
192 struct timeval timeout;
193 struct bpf_program bpf_prog;
194
195 do {
196 (void)snprintf(bpfdev, sizeof(bpfdev), "/dev/bpf%d", i++);
197 fd = open(bpfdev, O_RDWR);
198 } while (fd < 0 && errno == EBUSY);
199
200 if (fd < 0) {
201 zlog_warn ("open_bpf_dev(): failed to create bpf socket: %s",
202 strerror (errno));
203 return ISIS_WARNING;
204 }
205
206 zlog_info ("Opened BPF device %s", bpfdev);
207
208 memcpy (ifr.ifr_name, circuit->interface->name, sizeof(ifr.ifr_name));
209 if (ioctl (fd, BIOCSETIF, (caddr_t)&ifr) < 0 ) {
210 zlog_warn ("open_bpf_dev(): failed to bind to interface: %s",
211 strerror (errno));
212 return ISIS_WARNING;
213 }
214
215
216 if (ioctl (fd, BIOCGBLEN, (caddr_t)&blen) < 0) {
217 zlog_warn ("failed to get BPF buffer len");
218 blen = circuit->interface->mtu;
219 }
220
221 readblen = blen;
222
223 if (readbuff == NULL)
224 readbuff = malloc (blen);
225
226 zlog_info ("BPF buffer len = %u", blen);
227
228 /* BPF(4): reads return immediately upon packet reception.
229 * Otherwise, a read will block until either the kernel
230 * buffer becomes full or a timeout occurs.
231 */
232 if (ioctl (fd, BIOCIMMEDIATE, (caddr_t)&true) < 0) {
233 zlog_warn ("failed to set BPF dev to immediate mode");
234 }
235
236 /*
237 * We want to see only incoming packets
238 */
239 if (ioctl (fd, BIOCSSEESENT, (caddr_t)&false) < 0) {
240 zlog_warn ("failed to set BPF dev to incoming only mode");
241 }
242
243 /*
244 * ...but all of them
245 */
246 if (ioctl (fd, BIOCPROMISC, (caddr_t)&true) < 0) {
247 zlog_warn ("failed to set BPF dev to promiscuous mode");
248 }
249
250
251 /*
252 * If the buffer length is smaller than our mtu, lets try to increase it
253 */
254 if (blen < circuit->interface->mtu) {
255 if (ioctl (fd, BIOCSBLEN, &circuit->interface->mtu) < 0) {
256 zlog_warn ("failed to set BPF buffer len (%u to %u)", blen,
257 circuit->interface->mtu);
258 }
259 }
260
261 /*
262 * Set a timeout parameter - hope this helps select()
263 */
264 timeout.tv_sec = 600;
265 timeout.tv_usec = 0;
266 if (ioctl (fd, BIOCSRTIMEOUT, (caddr_t)&timeout) < 0) {
267 zlog_warn ("failed to set BPF device timeout");
268 }
269
270 /*
271 * And set the filter
272 */
273 memset (&bpf_prog, 0, sizeof (struct bpf_program));
274 bpf_prog.bf_len = 8;
275 bpf_prog.bf_insns = &(llcfilter[0]);
276 if (ioctl (fd, BIOCSETF, (caddr_t)&bpf_prog) < 0) {
277 zlog_warn ("open_bpf_dev(): failed to install filter: %s",
278 strerror (errno));
279 return ISIS_WARNING;
280 }
281
282
283 assert (fd > 0);
284
285 circuit->fd = fd;
286
287 return ISIS_OK;
288}
289
290#endif /* GNU_LINUX */
291
292/*
293 * Create the socket and set the tx/rx funcs
294 */
295int
296isis_sock_init (struct isis_circuit *circuit)
297{
298 int retval = ISIS_OK;
299
300
301#ifdef GNU_LINUX
302 retval = open_packet_socket (circuit);
303#else
304 retval = open_bpf_dev (circuit);
305#endif
306
307 if (retval == ISIS_OK) {
308 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
309 circuit->tx = isis_send_pdu_bcast;
310 circuit->rx = isis_recv_pdu_bcast;
311 }
312 else if (circuit->circ_type == CIRCUIT_T_P2P) {
313 circuit->tx = isis_send_pdu_p2p;
314 circuit->rx = isis_recv_pdu_p2p;
315 }
316 else {
317 zlog_warn ("isis_sock_init(): unknown circuit type");
318 retval = ISIS_WARNING;
319 }
320 }
321
322 return retval;
323}
324
325
326static inline int
327llc_check (u_char *llc)
328{
329
330 if(*llc != ISO_SAP || *(llc + 1) != ISO_SAP || *(llc +2) != 3)
331 return 0;
332
333 return 1;
334}
335
336#ifdef GNU_LINUX
337int
338isis_recv_pdu_bcast (struct isis_circuit *circuit, u_char *ssnpa)
339{
340 int bytesread, addr_len;
341 struct sockaddr_ll s_addr;
342 u_char llc[LLC_LEN];
343
344 addr_len = sizeof (s_addr);
345
346 memset (&s_addr, 0, sizeof (struct sockaddr_ll));
347
348 bytesread = recvfrom (circuit->fd, (void *)&llc,
349 LLC_LEN, MSG_PEEK,
350 (struct sockaddr *)&s_addr, &addr_len);
351
352 if (bytesread < 0) {
353 zlog_warn ("isis_recv_packet_bcast(): fd %d, recvfrom (): %s",
354 circuit->fd, strerror (errno));
355 zlog_warn ("circuit is %s", circuit->interface->name);
356 zlog_warn ("circuit fd %d", circuit->fd);
357 zlog_warn ("bytesread %d", bytesread);
358 /* get rid of the packet */
359 bytesread = read (circuit->fd, discard_buff, sizeof (discard_buff));
360 return ISIS_WARNING;
361 }
362 /*
363 * Filtering by llc field, discard packets sent by this host (other circuit)
364 */
365 if (!llc_check (llc) || s_addr.sll_pkttype == PACKET_OUTGOING) {
366 /* Read the packet into discard buff */
367 bytesread = read (circuit->fd, discard_buff, sizeof (discard_buff));
368 if (bytesread < 0)
369 zlog_warn ("isis_recv_pdu_bcast(): read() failed");
370 return ISIS_WARNING;
371 }
372
373 /* on lan we have to read to the static buff first */
374 bytesread = recvfrom (circuit->fd, sock_buff, circuit->interface->mtu, 0,
375 (struct sockaddr *)&s_addr, &addr_len);
376
377 /* then we lose the LLC */
378 memcpy (STREAM_DATA (circuit->rcv_stream),
379 sock_buff + LLC_LEN, bytesread - LLC_LEN);
380 circuit->rcv_stream->putp = bytesread - LLC_LEN;
381 circuit->rcv_stream->endp = bytesread - LLC_LEN;
382
383 memcpy (ssnpa, &s_addr.sll_addr, s_addr.sll_halen);
384
385 return ISIS_OK;
386}
387
388int
389isis_recv_pdu_p2p (struct isis_circuit *circuit, u_char *ssnpa)
390{
391
392 int bytesread, addr_len;
393 struct sockaddr_ll s_addr;
394
395 memset (&s_addr, 0, sizeof (struct sockaddr_ll));
396 addr_len = sizeof (s_addr);
397
398 /* we can read directly to the stream */
399 bytesread = recvfrom (circuit->fd, STREAM_DATA (circuit->rcv_stream),
400 circuit->interface->mtu, 0,
401 (struct sockaddr *)&s_addr, &addr_len);
402
403 if(s_addr.sll_pkttype == PACKET_OUTGOING) {
404 /* Read the packet into discard buff */
405 bytesread = read (circuit->fd, discard_buff, sizeof (discard_buff));
406 if (bytesread < 0)
407 zlog_warn ("isis_recv_pdu_p2p(): read() failed");
408 return ISIS_WARNING;
409 }
410
411 circuit->rcv_stream->putp = bytesread;
412 circuit->rcv_stream->endp = bytesread;
413
414 /* If we don't have protocol type 0x00FE which is
415 * ISO over GRE we exit with pain :)
416 */
417 if (ntohs(s_addr.sll_protocol) != 0x00FE) {
418 zlog_warn ("isis_recv_pdu_p2p(): protocol mismatch(): %X",
419 ntohs(s_addr.sll_protocol));
420 return ISIS_WARNING;
421 }
422
423 memcpy (ssnpa, &s_addr.sll_addr, s_addr.sll_halen);
424
425 return ISIS_OK;
426}
427
428
429
430int
431isis_send_pdu_bcast (struct isis_circuit *circuit, int level)
432{
433 /* we need to do the LLC in here because of P2P circuits, which will
434 * not need it
435 */
436 int written = 1;
437 struct sockaddr_ll sa;
438
439 stream_set_getp (circuit->snd_stream, 0);
440 memset (&sa, 0, sizeof (struct sockaddr_ll));
441 sa.sll_family = AF_PACKET;
442 sa.sll_protocol = htons (stream_get_endp(circuit->snd_stream)+LLC_LEN);
443 sa.sll_ifindex = circuit->interface->ifindex;
444 sa.sll_halen = ETH_ALEN;
445 if (level == 1)
446 memcpy (&sa.sll_addr, ALL_L1_ISS, ETH_ALEN);
447 else
448 memcpy (&sa.sll_addr, ALL_L2_ISS, ETH_ALEN);
449
450 /* on a broadcast circuit */
451 /* first we put the LLC in */
452 sock_buff[0] = 0xFE;
453 sock_buff[1] = 0xFE;
454 sock_buff[2] = 0x03;
455
456 /* then we copy the data */
457 memcpy (sock_buff + LLC_LEN, circuit->snd_stream->data,
458 stream_get_endp (circuit->snd_stream));
459
460 /* now we can send this */
461 written = sendto (circuit->fd, sock_buff,
462 circuit->snd_stream->putp + LLC_LEN, 0,
463 (struct sockaddr *)&sa, sizeof (struct sockaddr_ll));
464
465
466 return ISIS_OK;
467}
468
469int
470isis_send_pdu_p2p (struct isis_circuit *circuit, int level)
471{
472
473 int written = 1;
474 struct sockaddr_ll sa;
475
476 stream_set_getp (circuit->snd_stream, 0);
477 memset (&sa, 0, sizeof (struct sockaddr_ll));
478 sa.sll_family = AF_PACKET;
479 sa.sll_protocol = htons (stream_get_endp(circuit->snd_stream)+LLC_LEN);
480 sa.sll_ifindex = circuit->interface->ifindex;
481 sa.sll_halen = ETH_ALEN;
482 if (level == 1)
483 memcpy (&sa.sll_addr, ALL_L1_ISS, ETH_ALEN);
484 else
485 memcpy (&sa.sll_addr, ALL_L2_ISS, ETH_ALEN);
486
487
488 /* lets try correcting the protocol */
489 sa.sll_protocol = htons(0x00FE);
490 written = sendto (circuit->fd, circuit->snd_stream->data,
491 circuit->snd_stream->putp, 0, (struct sockaddr *)&sa,
492 sizeof (struct sockaddr_ll));
493
494 return ISIS_OK;
495}
496
497
498#else
499
500int
501isis_recv_pdu_bcast (struct isis_circuit *circuit, u_char *ssnpa)
502{
503 int bytesread = 0, bytestoread, offset, one = 1;
504 struct bpf_hdr *bpf_hdr;
505
506 assert (circuit->fd > 0);
507
508 if (ioctl (circuit->fd, FIONREAD, (caddr_t)&bytestoread) < 0 ) {
509 zlog_warn ("ioctl() FIONREAD failed: %s", strerror (errno));
510 }
511
512 if (bytestoread) {
513 bytesread = read (circuit->fd, readbuff, readblen);
514 }
515 if (bytesread < 0) {
516 zlog_warn ("isis_recv_pdu_bcast(): read() failed: %s", strerror (errno));
517 return ISIS_WARNING;
518 }
519
520 if (bytesread == 0)
521 return ISIS_WARNING;
522
523 bpf_hdr = (struct bpf_hdr*)readbuff;
524
525 assert (bpf_hdr->bh_caplen == bpf_hdr->bh_datalen);
526
527 offset = bpf_hdr->bh_hdrlen + LLC_LEN + ETHER_HDR_LEN;
528
529 /* then we lose the BPF, LLC and ethernet headers */
530 memcpy (STREAM_DATA (circuit->rcv_stream),
531 readbuff + offset,
532 bpf_hdr->bh_caplen - LLC_LEN - ETHER_HDR_LEN);
533
534 circuit->rcv_stream->putp = bpf_hdr->bh_caplen - LLC_LEN - ETHER_HDR_LEN;
535 circuit->rcv_stream->endp = bpf_hdr->bh_caplen - LLC_LEN - ETHER_HDR_LEN;
536 circuit->rcv_stream->getp = 0;
537
538 memcpy (ssnpa, readbuff + bpf_hdr->bh_hdrlen + ETHER_ADDR_LEN,
539 ETHER_ADDR_LEN);
540
541 if (ioctl (circuit->fd, BIOCFLUSH, &one) < 0)
542 zlog_warn ("Flushing failed: %s", strerror (errno));
543
544 return ISIS_OK;
545}
546
547int
548isis_recv_pdu_p2p (struct isis_circuit *circuit, u_char *ssnpa)
549{
550 int bytesread;
551
552 bytesread = read (circuit->fd, STREAM_DATA(circuit->rcv_stream),
553 circuit->interface->mtu);
554
555 if (bytesread < 0) {
556 zlog_warn ("isis_recv_pdu_p2p(): read () failed: %s", strerror (errno));
557 return ISIS_WARNING;
558 }
559
560 circuit->rcv_stream->putp = bytesread;
561 circuit->rcv_stream->endp = bytesread;
562
563 return ISIS_OK;
564}
565
566
567
568int
569isis_send_pdu_bcast (struct isis_circuit *circuit, int level)
570{
571 struct ether_header *eth;
572 int written;
573
574 stream_set_getp (circuit->snd_stream, 0);
575
576 /*
577 * First the eth header
578 */
579 eth = (struct ether_header *)sock_buff;
580 if (level == 1)
581 memcpy (eth->ether_dhost, ALL_L1_ISS, ETHER_ADDR_LEN);
582 else
583 memcpy (eth->ether_dhost, ALL_L2_ISS, ETHER_ADDR_LEN);
584 memcpy (eth->ether_shost, circuit->u.bc.snpa, ETHER_ADDR_LEN);
585 eth->ether_type = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN);
586
587 /*
588 * Then the LLC
589 */
590 sock_buff[ETHER_HDR_LEN] = ISO_SAP;
591 sock_buff[ETHER_HDR_LEN + 1] = ISO_SAP;
592 sock_buff[ETHER_HDR_LEN + 2] = 0x03;
593
594 /* then we copy the data */
595 memcpy (sock_buff + (LLC_LEN + ETHER_HDR_LEN), circuit->snd_stream->data,
596 stream_get_endp (circuit->snd_stream));
597
598 /* now we can send this */
599 written = write (circuit->fd, sock_buff,
600 circuit->snd_stream->putp + LLC_LEN + ETHER_HDR_LEN);
601
602
603 return ISIS_OK;
604}
605
606int
607isis_send_pdu_p2p (struct isis_circuit *circuit, int level)
608{
609
610
611 return ISIS_OK;
612}
613
614
615
616
617#endif /* GNU_LINUX */
618
619
620
621
622