This repository has been archived by the owner on Jun 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
iouyap.c
1658 lines (1370 loc) · 42.4 KB
/
iouyap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of iouyap, a program to bridge IOU with
* network interfaces.
*
* Copyright (C) 2013, 2014 James E. Carpenter
*
* iouyap is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iouyap is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <arpa/inet.h>
#include <netpacket/packet.h>
#if HAVE_NETINET_IF_ETHER_H && !HAVE_LINUX_IF_TUN_H
#include <net/ethernet.h>
#endif
#include <netdb.h>
#include <net/if_arp.h>
#include <linux/rtnetlink.h>
#include <linux/netlink.h>
#include <sys/time.h>
#include <limits.h>
#include <sys/file.h>
#include <ctype.h>
#ifdef USE_SYSTEM_INIPARSER
#include <iniparser.h>
#else
#include "iniparser/iniparser.h"
#endif
#include "iouyap.h"
#include "netmap.h"
#include "config.h"
extern char *program_invocation_short_name;
extern char *program_invocation_name;
int yap_appl_id = -1;
unsigned int yap_verbose = LOG_BASIC;
dictionary *yap_config = NULL;
foreign_port_t *port_table = NULL;
static int
set_socket_directory (char *dir)
{
return snprintf (dir, UNIX_PATH_MAX, "%s%u", NETIO_DIR_PREFIX, getuid ());
}
static int
set_socket_filename (char *name, unsigned int appl_id)
{
char dir[UNIX_PATH_MAX];
set_socket_directory (dir);
return snprintf (name, UNIX_PATH_MAX, "%s/%u", dir, appl_id);
}
static pid_t
get_socket_lock (const char *name)
{
char semaphore[FILENAME_MAX];
int fd;
struct flock fl;
int e;
if (strlen (name) < 1)
{
errno = EINVAL;
return -1;
}
snprintf (semaphore, sizeof semaphore, "%s.lck", name);
if ((fd = open (semaphore, O_RDONLY)) < 0)
return -1;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
fl.l_pid = getpid ();
if (fcntl (fd, F_GETLK, &fl) < 0)
{
e = errno;
close (fd);
errno = e;
return -1;
}
close (fd);
return fl.l_pid;
}
static int
unlock_socket (int fd, const char *name)
{
char semaphore[FILENAME_MAX];
int result;
int e;
if (fd < 0)
{
errno = EINVAL;
return -1;
}
snprintf (semaphore, sizeof semaphore, "%s.lck", name);
/* Unlinking before close avoids a race condition where we
* could accidentally delete the next lock file.
*/
result = unlink (semaphore);
e = errno;
close (fd);
errno = e;
return result;
}
static int
lock_socket (const char *name)
{
char semaphore[FILENAME_MAX];
int fd;
char pid[12];
int pid_len;
struct flock fl;
int e;
if (strlen (name) < 1)
{
errno = EINVAL;
return -1;
}
snprintf (semaphore, sizeof semaphore, "%s.lck", name);
// Either find a lock-file or create a new one
if ((fd = open (semaphore, O_WRONLY)) < 0 && errno == ENOENT)
fd = open (semaphore, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR);
if (fd < 0)
return -1;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
if (fcntl (fd, F_SETLK, &fl) < 0)
{
close (fd);
errno = EADDRINUSE;
return -1;
}
// We have the lock. Wipe out the file and put our PID in it.
ftruncate (fd, 0);
pid_len = snprintf (pid, sizeof(pid), "%ld\n", (long) getpid ());
if (write (fd, pid, pid_len) == -1)
{
e = errno;
// Something is wrong. Roll back.
unlock_socket (fd, name);
errno = e;
return -1;
}
return fd;
}
/* getbits: get n bits from position p
* (liberated from K&R2, p. 49)
*/
static unsigned int
getbits (unsigned int x, int p, int n)
{
return (x >> (p + 1 - n)) & ~(~0 << n);
}
static unsigned int
setbits (unsigned int x, int p, int n, unsigned int y)
{
return ((x & ~(~(~0 << n) << (p + 1 - n)))
| ((y & ~(~0 << n)) << (p + 1 - n)));
}
appl_id_t
unpack_ids (unsigned short appl_id)
{
appl_id_t i;
i.id = getbits(appl_id, IOU_IDS_ID, IOU_IDS_ID_LEN);
i.subid = getbits(appl_id, IOU_IDS_SUBID, IOU_IDS_SUBID_LEN);
i.subid_present = getbits(appl_id, IOU_IDS_SUBID_PRESENT,
IOU_IDS_SUBID_PRESENT_LEN);
return i;
}
port_t
unpack_port (unsigned char port)
{
port_t p;
p.unit = getbits(port, IOU_PORT_UNIT, IOU_PORT_UNIT_LEN);
p.bay = getbits(port, IOU_PORT_BAY, IOU_PORT_BAY_LEN);
return p;
}
unsigned short
pack_ids (appl_id_t appl_id)
{
unsigned short i = 0;
i = setbits (i, IOU_IDS_ID, IOU_IDS_ID_LEN, appl_id.id);
i = setbits (i, IOU_IDS_SUBID, IOU_IDS_SUBID_LEN, appl_id.subid);
i = setbits (i, IOU_IDS_SUBID_PRESENT, IOU_IDS_SUBID_PRESENT_LEN,
appl_id.subid_present);
return i;
}
unsigned char
pack_port (port_t port)
{
unsigned char p = 0;
p = setbits (p, IOU_PORT_BAY, IOU_PORT_BAY_LEN, port.bay);
p = setbits (p, IOU_PORT_UNIT, IOU_PORT_UNIT_LEN, port.unit);
return p;
}
void
parse_iou_hdr (iou_hdr_t *iou_hdr, unsigned char *iou_hdr_buf)
{
unsigned short dst_node;
unsigned short src_node;
unsigned char dst_port;
unsigned char src_port;
/* destination node */
dst_node = ntohs (*(unsigned short *) &iou_hdr_buf[IOU_DST_IDS]);
iou_hdr->dst_node.ids = unpack_ids (dst_node);
/* source node */
src_node = ntohs (*(unsigned short *) &iou_hdr_buf[IOU_SRC_IDS]);
iou_hdr->src_node.ids = unpack_ids (src_node);
/* destination port */
dst_port = iou_hdr_buf[IOU_DST_PORT];
iou_hdr->dst_node.port = unpack_port (dst_port);
/* source port */
src_port = iou_hdr_buf[IOU_SRC_PORT];
iou_hdr->src_node.port = unpack_port (src_port);
/* message type & channel */
iou_hdr->msg_type = iou_hdr_buf[IOU_MSG_TYPE];
iou_hdr->channel = iou_hdr_buf[IOU_CHANNEL];
}
void
build_iou_hdr (unsigned char *buf, iou_hdr_t iou_hdr)
{
appl_id_t dst_ids = iou_hdr.dst_node.ids;
appl_id_t src_ids = iou_hdr.src_node.ids;
port_t dst_port = iou_hdr.dst_node.port;
port_t src_port = iou_hdr.src_node.port;
unsigned short n;
/* destination id */
n = htons (pack_ids (dst_ids));
memcpy(&buf[IOU_DST_IDS], (unsigned char *)&n, IOU_IDS_SIZE);
/* source id */
n = htons (pack_ids (src_ids));
memcpy(&buf[IOU_SRC_IDS], (unsigned char *)&n, IOU_IDS_SIZE);
/* destination port */
buf[IOU_DST_PORT] = pack_port (dst_port);
/* source port */
buf[IOU_SRC_PORT] = pack_port (src_port);
/* message type */
buf[IOU_MSG_TYPE] = iou_hdr.msg_type;
/* channel number */
buf[IOU_CHANNEL] = iou_hdr.channel;
}
static int
get_iou_udp_port (int port)
{
return ini_getint_default_def ("base_port", BASE_PORT) + port;
}
static int
write_pcap_header (int fd, int snaplen, int linktype)
{
struct pcap_file_header header;
header.magic = 0xa1b2c3d4;
header.version_major = 2;
header.version_minor = 4;
header.thiszone = 0;
header.sigfigs = 0;
header.snaplen = snaplen;
header.linktype = linktype;
if (write (fd, &header, sizeof header) != sizeof header)
return -1;
return 0;
}
static int
write_pcap_frame (int fd, const unsigned char *packet, size_t len,
size_t caplen, int linktype)
{
size_t count;
struct pcap_pkthdr pcap_header;
size_t hdr_len = sizeof pcap_header;
unsigned char buf[MAX_MTU + hdr_len];
struct timeval ts;
if (caplen > MAX_MTU)
return -1;
gettimeofday (&ts, 0);
pcap_header.tv_sec = ts.tv_sec;
pcap_header.tv_usec = ts.tv_usec;
if (len < caplen)
caplen = len;
pcap_header.caplen = caplen;
pcap_header.len = len;
memcpy (buf, &pcap_header, hdr_len);
memcpy (&buf[hdr_len], packet, caplen);
count = caplen + hdr_len;
if (write (fd, &buf, count) != count)
return -1;
return 0;
}
static void *
foreign_listener (void *arg)
{
foreign_port_t *port = arg;
int segment_size;
ssize_t bytes_received, bytes_sent;
iou_node_t *nodes;
unsigned char buf[IOU_HDR_SIZE + MAX_MTU]; // TODO: may already include HDR
int i, j;
/* segment size, minus us */
segment_size = port->segment->size - 1;
nodes = port->nodes;
if (yap_verbose >= LOG_EXTENDED)
log_fmt ("foreign listener for %d:%d started (sfd=%d)\n",
yap_appl_id, port->iou_port, port->sfd);
for (;;)
{
/* Put received bytes after the (absent) IOU header */
bytes_received = read (port->sfd, &buf[IOU_HDR_SIZE], MAX_MTU);
if (bytes_received <= 0)
{
/* When tunneling, because our sends are asynchronous, we
* can get errors here from ICMP packets for UDP packets we
* sent earlier.
*/
switch (errno)
{
case ECONNREFUSED:
if (yap_verbose >= LOG_NOISY)
log_error ("read");
continue;
default:
log_error ("read");
goto shutdown;
}
}
if (yap_verbose >= LOG_CRAZY)
debug_log_fmt ("received %zd bytes (sfd=%d)\n",
bytes_received, port->sfd);
if (port->span_sfd != NO_FD)
write (port->span_sfd, &buf[IOU_HDR_SIZE], bytes_received);
if (port->pcap_fd != NO_FD)
{
write_pcap_frame (port->pcap_fd, &buf[IOU_HDR_SIZE],
bytes_received, port->pcap_caplen,
port->pcap_linktype);
}
/* Add the length of the IOU header we'll be sending */
bytes_received += IOU_HDR_SIZE;
/* Send the packet to the IOU node(s) in our segment. For each
* node, we copy the pre-calculated IOU header into the
* beginning of the buffer before sending.
*/
for (i = 0; i < segment_size; i++)
{
memcpy (buf, &nodes[i].header, sizeof nodes[i].header);
bytes_sent = sendto (nodes[i].sfd, buf, bytes_received,
0, &nodes[i].sa, nodes[i].sa_len);
/* Make sure everything went out. Certain errors, like a
* socket that hasn't been created yet, should be ignored.
* Others we can consider fatal and so we remove the node
* from the IOU segment.
*/
if (bytes_sent != bytes_received)
{
if (bytes_sent != -1) /* no error, shouldn't happen */
{
log_fmt ("sendto() only sent %zd of %zd bytes!"
" (sfd=%d)\n", bytes_sent,
bytes_received, port->sfd);
continue;
}
switch (errno)
{
case ENOENT: /* Socket file doesn't exist */
case ECONNREFUSED:
if (yap_verbose >= LOG_NOISY)
log_error ("sendto");
continue;
default:
log_error ("sendto");
break;
}
/* Remove the offending node */
segment_size--;
for (j = i; j < segment_size; j++)
nodes[j] = nodes[j + 1];
i--; /* redo (now with the next node) */
log_msg ("offending node removed");
if (segment_size == 0)
{
log_msg ("no nodes left!");
goto shutdown;
}
}
}
}
shutdown:
log_msg ("Thread shutting down because of errors");
return NULL;
}
static void *
iou_listener (void *arg)
{
int sfd = *(int *) arg;
ssize_t bytes_received, bytes_sent;
unsigned char buf[IOU_HDR_SIZE + MAX_MTU]; // TODO: may already include HDR
unsigned int port;
#ifdef DEBUG
iou_hdr_t iou_hdr;
#endif
if (yap_verbose >= LOG_EXTENDED)
log_fmt ("IOU listener for ID %d started (sfd=%d)\n", yap_appl_id, sfd);
for (;;)
{
/* This receives from an IOU instance */
bytes_received = read (sfd, buf, IOU_HDR_SIZE + MAX_MTU);
if (bytes_received <= 0)
{
log_error ("read");
break;
}
#ifdef DEBUG
parse_iou_hdr (&iou_hdr, buf);
if (iou_hdr.dst_node.ids.id != yap_appl_id
|| iou_hdr.dst_node.ids.subid_present == 1)
{
log_msg ("Packet is not for us!");
continue;
}
#endif
/* Get the port number we were addressed as */
port = buf[IOU_DST_PORT];
if (yap_verbose >= LOG_CRAZY)
debug_log_fmt ("received %zd bytes for port %d (sfd=%d)\n",
bytes_received, port, sfd);
if (bytes_received <= IOU_HDR_SIZE)
continue;
/* Send on the packet, minus the IOU header */
bytes_received -= IOU_HDR_SIZE;
if (port_table[port].span_sfd != NO_FD)
write (port_table[port].span_sfd, &buf[IOU_HDR_SIZE],
bytes_received);
if (port_table[port].pcap_fd != NO_FD)
{
write_pcap_frame (port_table[port].pcap_fd, &buf[IOU_HDR_SIZE],
bytes_received, port_table[port].pcap_caplen,
port_table[port].pcap_linktype);
}
bytes_sent = write (port_table[port].sfd, &buf[IOU_HDR_SIZE],
bytes_received);
if (bytes_sent != bytes_received)
{
if (bytes_sent != -1) /* no error, shouldn't happen */
{
log_fmt ("write() only sent %zd of %zd bytes! (sfd=%d)\n",
bytes_sent, bytes_received, sfd);
continue;
}
switch (errno)
{
// case ENOENT:
// if (yap_verbose >= LOG_NOISY)
// log_msg ("ENOENT");
// continue;
case EBADF: /* Bad file descriptor */
/* This error is normal if no foreign port is configured */
if (port_table[port].sfd == NO_FD)
{
if (yap_verbose >= LOG_NOISY)
log_fmt ("Discarding packet from port %d. "
"No foreign port configured.\n", port);
continue;
}
break;
case ECONNREFUSED:
if (yap_verbose >= LOG_NOISY)
log_msg ("ECONNREFUSED");
continue;
case ENOTCONN:
if (yap_verbose >= LOG_NOISY)
log_msg ("ENOTCONN");
/* Try to (re-)connect */
if (!connect (port_table[port].sfd, &port_table[port].sa,
port_table[port].sa_len))
continue;
/* We couldn't connect. Some connect errors are okay.
We'll just try again later. */
switch (errno)
{
case ENOENT:
case ECONNREFUSED:
continue;
default:
log_error ("connect");
goto shutdown;
}
break;
default:
break;
}
log_error ("Couldn't write to foreign port");
goto shutdown;
}
}
shutdown:
log_msg ("Thread shutting down because of errors");
return NULL;
}
static void
open_eth_dev (foreign_port_t * port, char *dev)
{
int sfd;
struct ifreq ifr;
struct sockaddr_ll sa;
struct packet_mreq mreq;
if (strlen (dev) >= IFNAMSIZ)
die ("too big");
if (yap_verbose >= LOG_EXTENDED)
log_fmt ("Trying to open %s\n", dev);
/* open socket */
sfd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL));
if (sfd == -1)
fatal_error ("socket");
/* get interface number */
memset (&ifr, 0, sizeof ifr);
strcpy (ifr.ifr_name, dev);
if (ioctl (sfd, SIOCGIFINDEX, &ifr) == -1)
fatal_error ("ioctl");
if (yap_verbose >= LOG_NOISY)
log_fmt ("ifr_name=%s, ifr_ifindex=%d\n", ifr.ifr_name, ifr.ifr_ifindex);
/* bind */
memset (&sa, 0, sizeof sa);
sa.sll_family = AF_PACKET;
sa.sll_ifindex = ifr.ifr_ifindex;
if (bind (sfd, (struct sockaddr *) &sa, sizeof sa) == -1)
fatal_error ("bind");
/* set promiscuous mode */
memset (&mreq, 0, sizeof mreq);
mreq.mr_ifindex = ifr.ifr_ifindex;
mreq.mr_type = PACKET_MR_PROMISC;
if (setsockopt (sfd, SOL_PACKET, PACKET_ADD_MEMBERSHIP,
&mreq, sizeof mreq) == -1)
fatal_error ("setsockopt");
if (yap_verbose >= LOG_BASIC)
log_fmt ("%s opened (sfd=%d)\n", ifr.ifr_name, sfd);
port->sfd = sfd;
}
static void
open_tap_dev (foreign_port_t * port, char *dev)
{
struct ifreq ifr;
int sfd;
if (strlen (dev) >= IFNAMSIZ)
die ("too big");
if (yap_verbose >= LOG_EXTENDED)
log_fmt ("Trying to open %s\n", dev);
if ((sfd = open (TAP_DEV, O_RDWR)) < 0)
fatal_error ("open");
memset (&ifr, 0, sizeof ifr);
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
if (*dev)
strcpy (ifr.ifr_name, dev);
if (ioctl (sfd, TUNSETIFF, (void *) &ifr) < 0)
fatal_error ("ioctl");
strcpy (dev, ifr.ifr_name);
if (yap_verbose >= LOG_BASIC)
log_fmt ("%s opened (sfd=%d)\n", ifr.ifr_name, sfd);
port->sfd = sfd;
}
static void
open_span_dev (foreign_port_t * port, char *dev)
{
struct ifreq ifr;
int sfd;
int sock;
struct {
struct nlmsghdr nh;
struct ifinfomsg ifinfo;
char attrbuf[512];
} req;
struct rtattr *rta;
unsigned int mtu = 68; // too small for IPv6, which is good here
int rtnetlink_sk;
if (strlen (dev) >= IFNAMSIZ)
die ("too big");
if (yap_verbose >= LOG_EXTENDED)
log_fmt ("Trying to open %s\n", dev);
if ((sfd = open (TAP_DEV, O_WRONLY)) < 0)
fatal_error ("open");
memset (&ifr, 0, sizeof ifr);
ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_MULTI_QUEUE;
if (*dev)
strcpy (ifr.ifr_name, dev);
if (ioctl (sfd, TUNSETIFF, (void *) &ifr) < 0)
fatal_error ("TUNSETIFF ioctl");
if (yap_verbose >= LOG_BASIC)
log_fmt ("%s opened (sfd=%d)\n", ifr.ifr_name, sfd);
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (ioctl (sock, SIOCGIFINDEX, (void *) &ifr) < 0)
fatal_error ("SIOCGIFINDEX ioctl");
rtnetlink_sk = socket (AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
memset (&req, 0, sizeof req);
req.nh.nlmsg_len = NLMSG_LENGTH (sizeof req.ifinfo);
req.nh.nlmsg_flags = NLM_F_REQUEST;
req.nh.nlmsg_type = RTM_SETLINK;
req.ifinfo.ifi_family = AF_UNSPEC;
req.ifinfo.ifi_index = ifr.ifr_ifindex;
req.ifinfo.ifi_change = 0xffffffff;
req.ifinfo.ifi_flags = IFF_UP | IFF_PROMISC | IFF_NOARP;
rta = (struct rtattr *)(((char *) &req) + NLMSG_ALIGN (req.nh.nlmsg_len));
rta->rta_type = IFLA_MTU;
rta->rta_len = RTA_LENGTH (sizeof (unsigned int));
req.nh.nlmsg_len = NLMSG_ALIGN (req.nh.nlmsg_len) + RTA_LENGTH (sizeof mtu);
memcpy (RTA_DATA (rta), &mtu, sizeof mtu);
send (rtnetlink_sk, &req, req.nh.nlmsg_len, 0);
close (rtnetlink_sk);
port->span_sfd = sfd;
}
static void
open_pcap_file (foreign_port_t * port, char *file, int no_hdr, int overwrite)
{
int fd;
if ((fd = open (file, O_CREAT | O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR)) < 0)
fatal_error ("open");
if( getuid() != geteuid() )
if( fchown(fd, getuid (), -1) )
fatal_error ("fchown");
/* If we can get an exclusive lock (without blocking) then check to see
* if the file is empty. Write a pcap header if it is.
*/
if (!no_hdr || overwrite)
{
if (flock (fd, LOCK_EX | LOCK_NB) == 0)
{
if (overwrite)
ftruncate (fd, 0);
if (!no_hdr && lseek (fd, 0, SEEK_END) == 0)
{
if (write_pcap_header (fd, port->pcap_caplen,
port->pcap_linktype) < 0)
{
fatal_error ("write_pcap_header");
}
}
}
else if (errno == EWOULDBLOCK)
{
if (overwrite)
log_fmt ("Can't overwrite %s. Somebody has a lock.\n", file);
}
else
{
close (fd);
fatal_error ("flock");
}
}
/* For normal operation we hold a shared lock */
if (flock (fd, LOCK_SH) < 0)
{
close (fd);
fatal_error ("flock");
}
port->pcap_fd = fd;
}
static void
open_pcap_pipe (foreign_port_t * port, char *file, int no_hdr)
{
int fd;
int new_pipe = 0;
/* Create the named pipe. Whoever creates it has to clean up. */
if (mkfifo (file, S_IRUSR | S_IWUSR) == 0)
{
port->pcap_fifo = file;
new_pipe = 1;
}
else if (errno != EEXIST)
{
fatal_error ("mkfifo");
}
// O_RDWR allows us to open the pipe without any readers attached
if ((fd = open (file, O_RDWR)) < 0)
{
fatal_error ("open");
}
/* If we created the pipe then we should normally be the one to write the
* header.
*/
if (!no_hdr && new_pipe)
{
if (flock (fd, LOCK_EX | LOCK_NB) == 0)
{
if (write_pcap_header (fd, port->pcap_caplen,
port->pcap_linktype) < 0)
{
fatal_error ("write_pcap_header");
}
}
else if (errno == EWOULDBLOCK)
{
log_msg ("Couldn't write pcap header to pipe we created! Locked.");
}
else
{
close (fd);
fatal_error ("flock");
}
}
/* For normal operation we hold a shared lock */
if (flock (fd, LOCK_SH) < 0)
{
close (fd);
fatal_error ("flock");
}
port->pcap_fd = fd;
}
static void
open_tunnel_uds (foreign_port_t * port, char *socks)
{
char *local_sock;
char *remote_sock;
struct sockaddr_un sock_addr;
// TODO: validate!
local_sock = strtok (socks, ":");
remote_sock = strtok (NULL, ":");
if (yap_verbose >= LOG_EXTENDED)
{
log_fmt ("binding to %s\n", local_sock);
log_fmt ("connecting to %s\n", remote_sock);
}
if ((port->sfd = socket (AF_UNIX, SOCK_DGRAM, 0)) < 0)
fatal_error ("socket");
/* bind */
memset (&sock_addr, 0, sizeof sock_addr);
sock_addr.sun_family = AF_UNIX;
strcpy (sock_addr.sun_path, local_sock);
unlink (sock_addr.sun_path);
if (bind (port->sfd, (struct sockaddr *) &sock_addr, sizeof sock_addr))
fatal_error ("bind");
/* save it so we can clean it up later */
port->socket_fname = local_sock;
/* connect */
port->sa_len = sizeof port->sa_un;
memset (&port->sa_un, 0, port->sa_len);
port->sa_un.sun_family = AF_UNIX;
strcpy (port->sa_un.sun_path, remote_sock);
/* We'll *try* to connect now. */
if (connect (port->sfd, &port->sa, port->sa_len) == -1)
{
/* Some connect errors are okay. We'll just try again later. */
switch (errno)
{
case ENOENT:
case ECONNREFUSED:
break;
default:
fatal_error ("connect");
}
}
}
static void
open_tunnel_udp (foreign_port_t * port, char *ports)
{
char *local_port;
char *remote_host;
char *remote_port;
int sfd;
struct addrinfo hints;
struct addrinfo *result, *rp;
int yes = 1;
// TODO: validate data
local_port = strtok (ports, ":");
remote_host = strtok (NULL, ":");
remote_port = strtok (NULL, ":");
if (yap_verbose >= LOG_EXTENDED)
{
log_fmt ("binding to UDP port %s\n", local_port);
log_fmt ("connecting to UDP %s:%s\n", remote_host, remote_port);
}
/* bind */
memset (&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
hints.ai_protocol = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
// TODO: allow binding to a specific IP address
if (getaddrinfo (NULL, local_port, &hints, &result) != 0)
fatal_error ("getaddrinfo");
for (rp = result; rp != NULL; rp = rp->ai_next)
{
sfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sfd == -1)
continue;
setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes);
if (bind (sfd, rp->ai_addr, rp->ai_addrlen) == 0)
break;
close (sfd);
}
if (rp == NULL)
die ("Could not bind");
freeaddrinfo (result);
/* connect */
memset (&hints, 0, sizeof hints);