-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
tunslip.c
1113 lines (968 loc) · 25.7 KB
/
tunslip.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
/*
* Copyright (c) 2001, Adam Dunkels.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This file is part of the uIP TCP/IP stack.
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <err.h>
int ssystem(const char *fmt, ...)
__attribute__((__format__ (__printf__, 1, 2)));
void write_to_serial(int outfd, void *inbuf, int len);
//#define PROGRESS(s) fprintf(stderr, s)
#define PROGRESS(s) do { } while (0)
struct ip {
u_int8_t ip_vhl; /* version and header length */
#define IP_V4 0x40
#define IP_V 0xf0
#define IP_HL 0x0f
u_int8_t ip_tos; /* type of service */
u_int16_t ip_len; /* total length */
u_int16_t ip_id; /* identification */
u_int16_t ip_off; /* fragment offset field */
#define IP_RF 0x8000 /* reserved fragment flag */
#define IP_DF 0x4000 /* dont fragment flag */
#define IP_MF 0x2000 /* more fragments flag */
#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
u_int8_t ip_ttl; /* time to live */
u_int8_t ip_p; /* protocol */
u_int16_t ip_sum; /* checksum */
u_int32_t ip_src, ip_dst; /* source and dest address */
u_int16_t uh_sport; /* source port */
u_int16_t uh_dport; /* destination port */
u_int16_t uh_ulen; /* udp length */
u_int16_t uh_sum; /* udp checksum */
};
int check_ip(const struct ip *ip, unsigned ip_len);
u_int16_t ip4sum(u_int16_t sum, const void *_p, u_int16_t len);
struct dhcp_msg {
u_int8_t op, htype, hlen, hops;
u_int8_t xid[4];
u_int16_t secs, flags;
u_int8_t ciaddr[4];
u_int8_t yiaddr[4];
u_int8_t siaddr[4];
u_int8_t giaddr[4];
u_int8_t chaddr[16];
#define DHCP_BASE_LEN (4*7 + 16)
u_int8_t sname[64];
u_int8_t file[128];
#define DHCP_HOLE_LEN (64 + 128)
#define DHCP_MSG_LEN (DHCP_BASE_LEN + DHCP_HOLE_LEN)
u_int8_t options[312];
};
struct dhcp_light_msg {
u_int8_t op, htype, hlen, hops;
u_int8_t xid[4];
u_int16_t secs, flags;
u_int8_t ciaddr[4];
u_int8_t yiaddr[4];
u_int8_t siaddr[4];
u_int8_t giaddr[4];
u_int8_t chaddr[16];
#define DHCP_LIGHT_MSG_LEN (4*7 + 16)
u_int8_t options[312];
};
#define DHCP_OPTION_SUBNET_MASK 1
#define DHCP_OPTION_ROUTER 3
#define DHCP_OPTION_DNS_SERVER 6
#define DHCP_OPTION_REQ_IPADDR 50
#define DHCP_OPTION_LEASE_TIME 51
#define DHCP_OPTION_MSG_TYPE 53
#define DHCP_OPTION_SERVER_ID 54
#define DHCP_OPTION_REQ_LIST 55
#define DHCP_OPTION_AGENT 82
#define DHCP_OPTION_SUBNET_SELECTION 118
#define DHCP_OPTION_END 255
/* DHCP_OPTION_AGENT, Relay Agent Information option subtypes: */
#define RAI_CIRCUIT_ID 1
#define RAI_REMOTE_ID 2
#define RAI_AGENT_ID 3
#define RAI_SUBNET_SELECTION 5
#define DHCPDISCOVER 1
#define DHCPOFFER 2
#define DHCPREQUEST 3
#define DHCPDECLINE 4
#define DHCPACK 5
#define DHCPNAK 6
#define DHCPRELEASE 7
#define BOOTP_BROADCAST 0x8000
#define BOOTPS 67
#define BOOTPC 68
#define BOOTREQUEST 1
#define BOOTREPLY 2
in_addr_t giaddr;
in_addr_t netaddr;
in_addr_t circuit_addr;
char tundev[1024] = { "tun0" };
struct sockaddr_in dhaddr;
int dhsock = -1;
void
relay_dhcp_to_server(struct ip *ip, int len)
{
struct dhcp_light_msg *inm;
struct dhcp_msg m;
int n;
u_int8_t *optptr;
inm = (void*)(((u_int8_t*)ip) + 20 + 8); /* Skip over IP&UDP headers. */
if (inm->op != BOOTREQUEST) {
return;
}
inm->flags = ntohs(BOOTP_BROADCAST);
memcpy(&m, inm, DHCP_BASE_LEN);
memset(&m.sname, 0x0, DHCP_HOLE_LEN);
memcpy(&m.options, &inm->options, len - 20 - 8 - DHCP_BASE_LEN);
n = (len - 20 - 8) + DHCP_HOLE_LEN; /* +HOLE -IP&UDP headers. */
/*
* Ideally we would like to use the Relay Agent information option
* (RFC3046) together with the Link Selection sub-option (RFC3527)
* to ensure that addresses are allocated for this
* subnet. Unfortunately ISC-DHCPD does not currently implement
* RFC3527 and some other mechanism must be used. For this reason
* this implementation in addition uses the DHCP option for subnet
* selection (RFC3011) which is really not intended to be used by
* relays.
*
* Find DHCP_OPTION_END and add the new option here.
*/
optptr = &m.options[n - DHCP_BASE_LEN - DHCP_HOLE_LEN - 1];
{
*optptr++ = DHCP_OPTION_SUBNET_SELECTION; /* RFC3011 */
*optptr++ = 4;
memcpy(optptr, &netaddr, 4); optptr += 4;
n += 4 + 2;
}
{
*optptr++ = DHCP_OPTION_AGENT; /* RFC3046 */
*optptr++ = 18; /* Sum of all suboptions below! */
*optptr++ = RAI_SUBNET_SELECTION; /* RFC3527 */
*optptr++ = 4;
memcpy(optptr, &netaddr, 4); optptr += 4;
*optptr++ = RAI_CIRCUIT_ID;
*optptr++ = 4;
memcpy(optptr, &circuit_addr, 4); optptr += 4;
*optptr++ = RAI_AGENT_ID;
*optptr++ = 4;
memcpy(optptr, &giaddr, 4); optptr += 4;
n += 18 + 2; /* Sum of all suboptions + 2! */
}
/* And finally put back the END. */
*optptr++ = DHCP_OPTION_END;
m.hops++;
memcpy(m.giaddr, &giaddr, sizeof(m.giaddr));
if (n != sendto(dhsock, &m, n, 0x0/*flags*/,
(struct sockaddr *)&dhaddr, sizeof(dhaddr)))
err(1, "sendto relay failed");
}
static u_int16_t ip_id;
void
relay_dhcp_to_client(int slipfd)
{
struct dhcp_msg inm;
struct {
struct ip ip;
struct dhcp_light_msg m;
} pkt;
int n, optlen, ip_len, udp_len;
u_int8_t *p, *t, *end;
u_int16_t sum;
u_int8_t op, msg_type = 0;
struct in_addr yiaddr;
memset(&inm.options, 0x0, sizeof(inm.options));
n = recv(dhsock, &inm, sizeof(inm), 0x0/*flags*/);
if (inm.op != BOOTREPLY) {
return;
}
memcpy(&yiaddr, inm.yiaddr, sizeof(inm.yiaddr));
memcpy(&pkt.m, &inm, DHCP_BASE_LEN);
pkt.m.hops++;
memset(pkt.m.giaddr, 0x0, sizeof(pkt.m.giaddr));
/*
* Copy options we would like to send to client.
*/
memcpy(pkt.m.options, inm.options, 4); /* Magic cookie */
end = &inm.op + n;
p = inm.options + 4; /* Magic cookie */
t = pkt.m.options + 4; /* Magic cookie */
while (p < end) {
op = p[0];
switch (op) {
case DHCP_OPTION_END:
goto done;
case DHCP_OPTION_MSG_TYPE:
msg_type = p[2];
case DHCP_OPTION_SUBNET_MASK:
case DHCP_OPTION_ROUTER:
case DHCP_OPTION_LEASE_TIME:
case DHCP_OPTION_SERVER_ID: /* Copy these options */
memcpy(t, p, p[1] + 2);
t += p[1] + 2;
p += p[1] + 2;
break;
case DHCP_OPTION_DNS_SERVER: /* Only copy first server */
*t++ = p[0];
*t++ = 4;
memcpy(t, p + 2, 4); t += 4;
p += p[1] + 2;
break;
default: /* Ignore these options */
/* printf("option type %d len %d\n", op, p[1]); */
p += p[1] + 2;
continue;
}
}
done:
if (op == DHCP_OPTION_END) {
*t++ = op; *p++;
}
optlen = t - pkt.m.options;
ip_len = 20 + 8 + DHCP_BASE_LEN + optlen;
udp_len = 8 + DHCP_BASE_LEN + optlen;
pkt.ip.ip_vhl = 0x45; /* IPv4 and hdrlen=5*4 */
pkt.ip.ip_tos = 0;
pkt.ip.ip_len = htons(ip_len);
pkt.ip.ip_id = htons(ip_id++);
pkt.ip.ip_off = 0;
pkt.ip.ip_ttl = 64;
pkt.ip.ip_p = 17; /* proto UDP */
pkt.ip.ip_sum = 0;
pkt.ip.ip_src = giaddr;
if (inm.flags & htons(BOOTP_BROADCAST)) /* check bcast bit */
pkt.ip.ip_dst = 0xffffffff; /* 255.255.255.255 */
else
pkt.ip.ip_dst = yiaddr.s_addr;
pkt.ip.uh_sport = htons(BOOTPS);
pkt.ip.uh_dport = htons(BOOTPC);
pkt.ip.uh_ulen = htons(udp_len);
pkt.ip.uh_sum = 0;
pkt.ip.ip_sum = ~htons(ip4sum(0, &pkt.ip, 20));
sum = 17 + udp_len;
sum = ip4sum(sum, &pkt.ip.ip_src, 8);
sum = ip4sum(sum, &pkt.ip.uh_sport, udp_len);
if (sum != 0xffff)
pkt.ip.uh_sum = ~htons(sum);
else
pkt.ip.uh_sum = 0xffff;
write_to_serial(slipfd, &pkt, ip_len);
if (msg_type == DHCPACK) {
printf("DHCPACK %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x IP %s\n",
pkt.m.chaddr[0], pkt.m.chaddr[1], pkt.m.chaddr[2], pkt.m.chaddr[3],
pkt.m.chaddr[4], pkt.m.chaddr[5], pkt.m.chaddr[6], pkt.m.chaddr[7],
inet_ntoa(yiaddr));
/* ssystem("arp -s %s auto pub only", inet_ntoa(yiaddr)); */
}
}
/*
* Internet checksum in host byte order.
*/
u_int16_t
ip4sum(u_int16_t sum, const void *_p, u_int16_t len)
{
u_int16_t t;
const u_int8_t *p = _p;
const u_int8_t *end = p + len;
while(p < (end-1)) {
t = (p[0] << 8) + p[1];
sum += t;
if (sum < t) sum++;
p += 2;
}
if(p < end) {
t = (p[0] << 8) + 0;
sum += t;
if (sum < t) sum++;
}
return sum;
}
int
check_ip(const struct ip *ip, unsigned ip_len)
{
u_int16_t sum, ip_hl;
/* Check IP version and length. */
if((ip->ip_vhl & IP_V) != IP_V4)
return -1;
if(ntohs(ip->ip_len) > ip_len)
return -2;
if(ntohs(ip->ip_len) < ip_len)
return -3;
/* Check IP header. */
ip_hl = 4*(ip->ip_vhl & IP_HL);
sum = ip4sum(0, ip, ip_hl);
if(sum != 0xffff && sum != 0x0)
return -4;
if(ip->ip_p == 6 || ip->ip_p == 17) { /* Check TCP or UDP header. */
u_int16_t tcp_len = ip_len - ip_hl;
/* Sum pseudoheader. */
sum = ip->ip_p + tcp_len; /* proto and len, no carry */
sum = ip4sum(sum, &ip->ip_src, 8); /* src and dst */
/* Sum TCP/UDP header and data. */
sum = ip4sum(sum, (u_int8_t*)ip + ip_hl, tcp_len);
/* Failed checksum test? */
if (sum != 0xffff && sum != 0x0) {
if (ip->ip_p == 6) { /* TCP == 6 */
return -5;
} else { /* UDP */
/* Deal with disabled UDP checksums. */
if (ip->uh_sum != 0)
return -6;
}
}
} else if (ip->ip_p == 1) { /* ICMP */
u_int16_t icmp_len = ip_len - ip_hl;
sum = ip4sum(0, (u_int8_t*)ip + ip_hl, icmp_len);
if(sum != 0xffff && sum != 0x0)
return -7;
}
return 0;
}
int
is_sensible_string(const unsigned char *s, int len)
{
int i;
for(i = 1; i < len; i++) {
if(s[i] == 0 || s[i] == '\r' || s[i] == '\n' || s[i] == '\t') {
continue;
} else if(s[i] < ' ' || '~' < s[i]) {
return 0;
}
}
return 1;
}
int
ssystem(const char *fmt, ...) __attribute__((__format__ (__printf__, 1, 2)));
int
ssystem(const char *fmt, ...)
{
char cmd[128];
va_list ap;
va_start(ap, fmt);
vsnprintf(cmd, sizeof(cmd), fmt, ap);
va_end(ap);
printf("%s\n", cmd);
fflush(stdout);
return system(cmd);
}
#define SLIP_END 0300
#define SLIP_ESC 0333
#define SLIP_ESC_END 0334
#define SLIP_ESC_ESC 0335
/*
* Read from serial, when we have a packet write it to tun. No output
* buffering, input buffered by stdio.
*/
void
serial_to_tun(FILE *inslip, int outfd)
{
static union {
unsigned char inbuf[2000];
struct ip iphdr;
} uip;
static int inbufptr = 0;
int ret;
unsigned char c;
#ifdef linux
ret = fread(&c, 1, 1, inslip);
if(ret == -1 || ret == 0) err(1, "serial_to_tun: read");
goto after_fread;
#endif
read_more:
if(inbufptr >= sizeof(uip.inbuf)) {
inbufptr = 0;
}
ret = fread(&c, 1, 1, inslip);
#ifdef linux
after_fread:
#endif
if(ret == -1) {
err(1, "serial_to_tun: read");
}
if(ret == 0) {
clearerr(inslip);
return;
fprintf(stderr, "serial_to_tun: EOF\n");
exit(1);
}
/* fprintf(stderr, ".");*/
switch(c) {
case SLIP_END:
if(inbufptr > 0) {
/*
* Sanity checks.
*/
#define DEBUG_LINE_MARKER '\r'
int ecode;
ecode = check_ip(&uip.iphdr, inbufptr);
if(ecode < 0 && inbufptr == 8 && strncmp(uip.inbuf, "=IPA", 4) == 0) {
static struct in_addr ipa;
inbufptr = 0;
if(memcmp(&ipa, &uip.inbuf[4], sizeof(ipa)) == 0) {
break;
}
/* New address. */
if(ipa.s_addr != 0) {
#ifdef linux
ssystem("route delete -net %s netmask %s dev %s",
inet_ntoa(ipa), "255.255.255.255", tundev);
#else
ssystem("route delete -net %s -netmask %s -interface %s",
inet_ntoa(ipa), "255.255.255.255", tundev);
#endif
}
memcpy(&ipa, &uip.inbuf[4], sizeof(ipa));
if(ipa.s_addr != 0) {
#ifdef linux
ssystem("route add -net %s netmask %s dev %s",
inet_ntoa(ipa), "255.255.255.255", tundev);
#else
ssystem("route add -net %s -netmask %s -interface %s",
inet_ntoa(ipa), "255.255.255.255", tundev);
#endif
}
break;
} else if(ecode < 0) {
/*
* If sensible ASCII string, print it as debug info!
*/
if(uip.inbuf[0] == DEBUG_LINE_MARKER) {
fwrite(uip.inbuf + 1, inbufptr - 1, 1, stderr);
} else if(is_sensible_string(uip.inbuf, inbufptr)) {
fwrite(uip.inbuf, inbufptr, 1, stderr);
} else {
fprintf(stderr,
"serial_to_tun: drop packet len=%d ecode=%d\n",
inbufptr, ecode);
}
inbufptr = 0;
break;
}
PROGRESS("s");
if(dhsock != -1) {
struct ip *ip = (void *)uip.inbuf;
if(ip->ip_p == 17 && ip->ip_dst == 0xffffffff /* UDP and broadcast */
&& ip->uh_sport == ntohs(BOOTPC) && ip->uh_dport == ntohs(BOOTPS)) {
relay_dhcp_to_server(ip, inbufptr);
inbufptr = 0;
}
}
if(write(outfd, uip.inbuf, inbufptr) != inbufptr) {
err(1, "serial_to_tun: write");
}
inbufptr = 0;
}
break;
case SLIP_ESC:
if(fread(&c, 1, 1, inslip) != 1) {
clearerr(inslip);
/* Put ESC back and give up! */
ungetc(SLIP_ESC, inslip);
return;
}
switch(c) {
case SLIP_ESC_END:
c = SLIP_END;
break;
case SLIP_ESC_ESC:
c = SLIP_ESC;
break;
}
/* FALLTHROUGH */
default:
uip.inbuf[inbufptr++] = c;
break;
}
goto read_more;
}
unsigned char slip_buf[2000];
int slip_end, slip_begin;
void
slip_send(int fd, unsigned char c)
{
if (slip_end >= sizeof(slip_buf))
err(1, "slip_send overflow");
slip_buf[slip_end] = c;
slip_end++;
}
int
slip_empty()
{
return slip_end == 0;
}
void
slip_flushbuf(int fd)
{
int n;
if (slip_empty())
return;
n = write(fd, slip_buf + slip_begin, (slip_end - slip_begin));
if(n == -1 && errno != EAGAIN) {
err(1, "slip_flushbuf write failed");
} else if(n == -1) {
PROGRESS("Q"); /* Outqueueis full! */
} else {
slip_begin += n;
if(slip_begin == slip_end) {
slip_begin = slip_end = 0;
}
}
}
void
write_to_serial(int outfd, void *inbuf, int len)
{
u_int8_t *p = inbuf;
int i, ecode;
struct ip *iphdr = inbuf;
/*
* Sanity checks.
*/
ecode = check_ip(inbuf, len);
if(ecode < 0) {
fprintf(stderr, "tun_to_serial: drop packet %d\n", ecode);
return;
}
if(iphdr->ip_id == 0 && iphdr->ip_off & IP_DF) {
uint16_t nid = htons(ip_id++);
iphdr->ip_id = nid;
nid = ~nid; /* negate */
iphdr->ip_sum += nid; /* add */
if(iphdr->ip_sum < nid) { /* 1-complement overflow? */
iphdr->ip_sum++;
}
ecode = check_ip(inbuf, len);
if(ecode < 0) {
fprintf(stderr, "tun_to_serial: drop packet %d\n", ecode);
return;
}
}
/* It would be ``nice'' to send a SLIP_END here but it's not
* really necessary.
*/
/* slip_send(outfd, SLIP_END); */
for(i = 0; i < len; i++) {
switch(p[i]) {
case SLIP_END:
slip_send(outfd, SLIP_ESC);
slip_send(outfd, SLIP_ESC_END);
break;
case SLIP_ESC:
slip_send(outfd, SLIP_ESC);
slip_send(outfd, SLIP_ESC_ESC);
break;
default:
slip_send(outfd, p[i]);
break;
}
}
slip_send(outfd, SLIP_END);
PROGRESS("t");
}
/*
* Read from tun, write to slip.
*/
void
tun_to_serial(int infd, int outfd)
{
static union {
unsigned char inbuf[2000];
struct ip iphdr;
} uip;
int size;
if((size = read(infd, uip.inbuf, 2000)) == -1) err(1, "tun_to_serial: read");
write_to_serial(outfd, uip.inbuf, size);
}
#ifndef BAUDRATE
#define BAUDRATE B115200
#endif
speed_t b_rate = BAUDRATE;
void
stty_telos(int fd)
{
struct termios tty;
speed_t speed = b_rate;
int i;
if(tcflush(fd, TCIOFLUSH) == -1) err(1, "tcflush");
if(tcgetattr(fd, &tty) == -1) err(1, "tcgetattr");
cfmakeraw(&tty);
/* Nonblocking read. */
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 0;
tty.c_cflag &= ~CRTSCTS;
tty.c_cflag &= ~HUPCL;
tty.c_cflag &= ~CLOCAL;
cfsetispeed(&tty, speed);
cfsetospeed(&tty, speed);
if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) err(1, "tcsetattr");
#if 1
/* Nonblocking read and write. */
/* if(fcntl(fd, F_SETFL, O_NONBLOCK) == -1) err(1, "fcntl"); */
tty.c_cflag |= CLOCAL;
if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) err(1, "tcsetattr");
i = TIOCM_DTR;
if(ioctl(fd, TIOCMBIS, &i) == -1) err(1, "ioctl");
#endif
usleep(10*1000); /* Wait for hardware 10ms. */
/* Flush input and output buffers. */
if(tcflush(fd, TCIOFLUSH) == -1) err(1, "tcflush");
}
int
devopen(const char *dev, int flags)
{
char t[1024];
strcpy(t, "/dev/");
strcat(t, dev);
return open(t, flags);
}
#ifdef linux
#include <linux/if.h>
#include <linux/if_tun.h>
int
tun_alloc(char *dev)
{
struct ifreq ifr;
int fd, err;
if( (fd = open("/dev/net/tun", O_RDWR)) < 0 )
return -1;
memset(&ifr, 0, sizeof(ifr));
/* Flags: IFF_TUN - TUN device (no Ethernet headers)
* IFF_TAP - TAP device
*
* IFF_NO_PI - Do not provide packet information
*/
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
if(*dev != 0)
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
if((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
close(fd);
return err;
}
strcpy(dev, ifr.ifr_name);
return fd;
}
#else
int
tun_alloc(char *dev)
{
return devopen(dev, O_RDWR);
}
#endif
const char *ipaddr;
const char *netmask;
void
cleanup(void)
{
ssystem("ifconfig %s down", tundev);
#ifndef linux
ssystem("sysctl -w net.inet.ip.forwarding=0");
#endif
/* ssystem("arp -d %s", ipaddr); */
ssystem("netstat -nr"
" | awk '{ if ($2 == \"%s\") print \"route delete -net \"$1; }'"
" | sh",
tundev);
}
void
sigcleanup(int signo)
{
fprintf(stderr, "signal %d\n", signo);
exit(0); /* exit(0) will call cleanup() */
}
static int got_sigalarm;
void
sigalarm(int signo)
{
got_sigalarm = 1;
return;
}
void
sigalarm_reset()
{
#ifdef linux
#define TIMEOUT (997*1000)
#else
#define TIMEOUT (2451*1000)
#endif
ualarm(TIMEOUT, TIMEOUT);
got_sigalarm = 0;
}
void
ifconf(const char *tundev, const char *ipaddr, const char *netmask)
{
struct in_addr netname;
netname.s_addr = inet_addr(ipaddr) & inet_addr(netmask);
#ifdef linux
ssystem("ifconfig %s inet `hostname` up", tundev);
if(strcmp(ipaddr, "0.0.0.0") != 0) {
ssystem("route add -net %s netmask %s dev %s",
inet_ntoa(netname), netmask, tundev);
}
#else
ssystem("ifconfig %s inet `hostname` %s up", tundev, ipaddr);
if(strcmp(ipaddr, "0.0.0.0") != 0) {
ssystem("route add -net %s -netmask %s -interface %s",
inet_ntoa(netname), netmask, tundev);
}
ssystem("sysctl -w net.inet.ip.forwarding=1");
#endif /* !linux */
ssystem("ifconfig %s\n", tundev);
}
int
main(int argc, char **argv)
{
int c;
int tunfd, slipfd, maxfd;
int ret;
fd_set rset, wset;
FILE *inslip;
const char *siodev = NULL;
const char *dhcp_server = NULL;
u_int16_t myport = BOOTPS, dhport = BOOTPS;
int baudrate = -2;
ip_id = getpid() * time(NULL);
setvbuf(stdout, NULL, _IOLBF, 0); /* Line buffered output. */
while((c = getopt(argc, argv, "B:D:hs:t:")) != -1) {
switch (c) {
case 'B':
baudrate = atoi(optarg);
break;
case 'D':
dhcp_server = optarg;
break;
case 's':
if(strncmp("/dev/", optarg, 5) == 0) {
siodev = optarg + 5;
} else {
siodev = optarg;
}
break;
case 't':
if(strncmp("/dev/", optarg, 5) == 0) {
strcpy(tundev, optarg + 5);
} else {
strcpy(tundev, optarg);
}
break;
case '?':
case 'h':
default:
err(1, "usage: tunslip [-B baudrate] [-s siodev] [-t tundev] [-D dhcp-server] ipaddress netmask [dhcp-server]");
break;
}
}
argc -= (optind - 1);
argv += (optind - 1);
if(argc != 3 && argc != 4) {
err(1, "usage: tunslip [-s siodev] [-t tundev] [-D dhcp-server] ipaddress netmask [dhcp-server]");
}
ipaddr = argv[1];
netmask = argv[2];
circuit_addr = inet_addr(ipaddr);
netaddr = inet_addr(ipaddr) & inet_addr(netmask);
switch(baudrate) {
case -2:
break; /* Use default. */
case 9600:
b_rate = B9600;
break;
case 19200:
b_rate = B19200;
break;
case 38400:
b_rate = B38400;
break;
case 57600:
b_rate = B57600;
break;
case 115200:
b_rate = B115200;
break;
default:
err(1, "unknown baudrate %d", baudrate);
break;
}
/*
* Set up DHCP relay agent socket and find the address of this relay
* agent.
*/
if(argc == 4) {
dhcp_server = argv[3];
}
if(dhcp_server != NULL) {
struct sockaddr_in myaddr;
socklen_t len;
in_addr_t a;
if(strchr(dhcp_server, ':') != NULL) {
dhport = atoi(strchr(dhcp_server, ':') + 1);
myport = dhport + 1;
*strchr(dhcp_server, ':') = '\0';
}
a = inet_addr(dhcp_server);
if(a == -1) {
err(1, "illegal dhcp-server address");
}
#ifndef linux
dhaddr.sin_len = sizeof(dhaddr);
#endif
dhaddr.sin_family = AF_INET;
dhaddr.sin_port = htons(dhport);
dhaddr.sin_addr.s_addr = a;
dhsock = socket(AF_INET, SOCK_DGRAM, 0);
if(dhsock < 0) {
err (1, "socket");
}
memset(&myaddr, 0x0, sizeof(myaddr));
#ifndef linux
myaddr.sin_len = sizeof(myaddr);
#endif
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = INADDR_ANY;
myaddr.sin_port = htons(myport);
if(bind(dhsock, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
err(1, "bind dhcp-relay");
}
if(connect(dhsock, (struct sockaddr *)&dhaddr, sizeof(dhaddr)) < 0) {
err(1, "connect to dhcp-server");
}
len = sizeof(myaddr);
if(getsockname(dhsock, (struct sockaddr *)&myaddr, &len) < 0) {
err(1, "getsockname dhsock");
}
giaddr = myaddr.sin_addr.s_addr;