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