Change RIME max time to fit in 16 bit timer
[contiki-2.x.git] / tools / tunslip.c
blob06a9992a1b5da9615265c716be9a5aad87ab5f0d
1 /*
2 * Copyright (c) 2001, Adam Dunkels.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior
15 * written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * This file is part of the uIP TCP/IP stack.
31 * $Id: tunslip.c,v 1.15 2008/02/24 21:13:36 adamdunkels Exp $
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdarg.h>
38 #include <string.h>
39 #include <time.h>
40 #include <sys/types.h>
42 #include <unistd.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <signal.h>
46 #include <termios.h>
47 #include <sys/ioctl.h>
49 #include <sys/socket.h>
50 #include <netinet/in.h>
51 #include <arpa/inet.h>
53 #include <err.h>
55 int ssystem(const char *fmt, ...)
56 __attribute__((__format__ (__printf__, 1, 2)));
57 void write_to_serial(int outfd, void *inbuf, int len);
59 //#define PROGRESS(s) fprintf(stderr, s)
60 #define PROGRESS(s) do { } while (0)
62 struct ip {
63 u_int8_t ip_vhl; /* version and header length */
64 #define IP_V4 0x40
65 #define IP_V 0xf0
66 #define IP_HL 0x0f
67 u_int8_t ip_tos; /* type of service */
68 u_int16_t ip_len; /* total length */
69 u_int16_t ip_id; /* identification */
70 u_int16_t ip_off; /* fragment offset field */
71 #define IP_RF 0x8000 /* reserved fragment flag */
72 #define IP_DF 0x4000 /* dont fragment flag */
73 #define IP_MF 0x2000 /* more fragments flag */
74 #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
75 u_int8_t ip_ttl; /* time to live */
76 u_int8_t ip_p; /* protocol */
77 u_int16_t ip_sum; /* checksum */
78 u_int32_t ip_src, ip_dst; /* source and dest address */
79 u_int16_t uh_sport; /* source port */
80 u_int16_t uh_dport; /* destination port */
81 u_int16_t uh_ulen; /* udp length */
82 u_int16_t uh_sum; /* udp checksum */
85 int check_ip(const struct ip *ip, unsigned ip_len);
86 u_int16_t ip4sum(u_int16_t sum, const void *_p, u_int16_t len);
88 struct dhcp_msg {
89 u_int8_t op, htype, hlen, hops;
90 u_int8_t xid[4];
91 u_int16_t secs, flags;
92 u_int8_t ciaddr[4];
93 u_int8_t yiaddr[4];
94 u_int8_t siaddr[4];
95 u_int8_t giaddr[4];
96 u_int8_t chaddr[16];
97 #define DHCP_BASE_LEN (4*7 + 16)
98 u_int8_t sname[64];
99 u_int8_t file[128];
100 #define DHCP_HOLE_LEN (64 + 128)
101 #define DHCP_MSG_LEN (DHCP_BASE_LEN + DHCP_HOLE_LEN)
102 u_int8_t options[312];
105 struct dhcp_light_msg {
106 u_int8_t op, htype, hlen, hops;
107 u_int8_t xid[4];
108 u_int16_t secs, flags;
109 u_int8_t ciaddr[4];
110 u_int8_t yiaddr[4];
111 u_int8_t siaddr[4];
112 u_int8_t giaddr[4];
113 u_int8_t chaddr[16];
114 #define DHCP_LIGHT_MSG_LEN (4*7 + 16)
115 u_int8_t options[312];
118 #define DHCP_OPTION_SUBNET_MASK 1
119 #define DHCP_OPTION_ROUTER 3
120 #define DHCP_OPTION_DNS_SERVER 6
121 #define DHCP_OPTION_REQ_IPADDR 50
122 #define DHCP_OPTION_LEASE_TIME 51
123 #define DHCP_OPTION_MSG_TYPE 53
124 #define DHCP_OPTION_SERVER_ID 54
125 #define DHCP_OPTION_REQ_LIST 55
126 #define DHCP_OPTION_AGENT 82
127 #define DHCP_OPTION_SUBNET_SELECTION 118
128 #define DHCP_OPTION_END 255
130 /* DHCP_OPTION_AGENT, Relay Agent Information option subtypes: */
131 #define RAI_CIRCUIT_ID 1
132 #define RAI_REMOTE_ID 2
133 #define RAI_AGENT_ID 3
134 #define RAI_SUBNET_SELECTION 5
136 #define DHCPDISCOVER 1
137 #define DHCPOFFER 2
138 #define DHCPREQUEST 3
139 #define DHCPDECLINE 4
140 #define DHCPACK 5
141 #define DHCPNAK 6
142 #define DHCPRELEASE 7
144 #define BOOTP_BROADCAST 0x8000
146 #define BOOTPS 67
147 #define BOOTPC 68
149 #define BOOTREQUEST 1
150 #define BOOTREPLY 2
152 in_addr_t giaddr;
153 in_addr_t netaddr;
154 in_addr_t circuit_addr;
156 char tundev[32] = { "tun0" };
158 struct sockaddr_in dhaddr;
159 int dhsock = -1;
161 void
162 relay_dhcp_to_server(struct ip *ip, int len)
164 struct dhcp_light_msg *inm;
165 struct dhcp_msg m;
166 int n;
167 u_int8_t *optptr;
169 inm = (void*)(((u_int8_t*)ip) + 20 + 8); /* Skip over IP&UDP headers. */
171 if (inm->op != BOOTREQUEST) {
172 return;
175 inm->flags = ntohs(BOOTP_BROADCAST);
177 memcpy(&m, inm, DHCP_BASE_LEN);
178 memset(&m.sname, 0x0, DHCP_HOLE_LEN);
179 memcpy(&m.options, &inm->options, len - 20 - 8 - DHCP_BASE_LEN);
180 n = (len - 20 - 8) + DHCP_HOLE_LEN; /* +HOLE -IP&UDP headers. */
183 * Ideally we would like to use the Relay Agent information option
184 * (RFC3046) together with the Link Selection sub-option (RFC3527)
185 * to ensure that addresses are allocated for this
186 * subnet. Unfortunately ISC-DHCPD does not currently implement
187 * RFC3527 and some other mechanism must be used. For this reason
188 * this implementation in addition uses the DHCP option for subnet
189 * selection (RFC3011) which is really not intended to be used by
190 * relays.
192 * Find DHCP_OPTION_END and add the new option here.
194 optptr = &m.options[n - DHCP_BASE_LEN - DHCP_HOLE_LEN - 1];
196 *optptr++ = DHCP_OPTION_SUBNET_SELECTION; /* RFC3011 */
197 *optptr++ = 4;
198 memcpy(optptr, &netaddr, 4); optptr += 4;
199 n += 4 + 2;
202 *optptr++ = DHCP_OPTION_AGENT; /* RFC3046 */
203 *optptr++ = 18; /* Sum of all suboptions below! */
205 *optptr++ = RAI_SUBNET_SELECTION; /* RFC3527 */
206 *optptr++ = 4;
207 memcpy(optptr, &netaddr, 4); optptr += 4;
208 *optptr++ = RAI_CIRCUIT_ID;
209 *optptr++ = 4;
210 memcpy(optptr, &circuit_addr, 4); optptr += 4;
211 *optptr++ = RAI_AGENT_ID;
212 *optptr++ = 4;
213 memcpy(optptr, &giaddr, 4); optptr += 4;
214 n += 18 + 2; /* Sum of all suboptions + 2! */
216 /* And finally put back the END. */
217 *optptr++ = DHCP_OPTION_END;
219 m.hops++;
220 memcpy(m.giaddr, &giaddr, sizeof(m.giaddr));
221 if (n != sendto(dhsock, &m, n, 0x0/*flags*/,
222 (struct sockaddr *)&dhaddr, sizeof(dhaddr)))
223 err(1, "sendto relay failed");
226 static u_int16_t ip_id;
228 void
229 relay_dhcp_to_client(int slipfd)
231 struct dhcp_msg inm;
232 struct {
233 struct ip ip;
234 struct dhcp_light_msg m;
235 } pkt;
236 int n, optlen, ip_len, udp_len;
237 u_int8_t *p, *t, *end;
238 u_int16_t sum;
239 u_int8_t op, msg_type = 0;
240 struct in_addr yiaddr;
242 memset(&inm.options, 0x0, sizeof(inm.options));
244 n = recv(dhsock, &inm, sizeof(inm), 0x0/*flags*/);
246 if (inm.op != BOOTREPLY) {
247 return;
250 memcpy(&yiaddr, inm.yiaddr, sizeof(inm.yiaddr));
251 memcpy(&pkt.m, &inm, DHCP_BASE_LEN);
252 pkt.m.hops++;
253 memset(pkt.m.giaddr, 0x0, sizeof(pkt.m.giaddr));
256 * Copy options we would like to send to client.
258 memcpy(pkt.m.options, inm.options, 4); /* Magic cookie */
260 end = &inm.op + n;
261 p = inm.options + 4; /* Magic cookie */
262 t = pkt.m.options + 4; /* Magic cookie */
263 while (p < end) {
264 op = p[0];
265 switch (op) {
266 case DHCP_OPTION_END:
267 goto done;
269 case DHCP_OPTION_MSG_TYPE:
270 msg_type = p[2];
271 case DHCP_OPTION_SUBNET_MASK:
272 case DHCP_OPTION_ROUTER:
273 case DHCP_OPTION_LEASE_TIME:
274 case DHCP_OPTION_SERVER_ID: /* Copy these options */
275 memcpy(t, p, p[1] + 2);
276 t += p[1] + 2;
277 p += p[1] + 2;
278 break;
280 case DHCP_OPTION_DNS_SERVER: /* Only copy first server */
281 *t++ = p[0];
282 *t++ = 4;
283 memcpy(t, p + 2, 4); t += 4;
284 p += p[1] + 2;
285 break;
287 default: /* Ignore these options */
288 /* printf("option type %d len %d\n", op, p[1]); */
289 p += p[1] + 2;
290 continue;
293 done:
294 if (op == DHCP_OPTION_END) {
295 *t++ = op; *p++;
298 optlen = t - pkt.m.options;
299 ip_len = 20 + 8 + DHCP_BASE_LEN + optlen;
300 udp_len = 8 + DHCP_BASE_LEN + optlen;
302 pkt.ip.ip_vhl = 0x45; /* IPv4 and hdrlen=5*4 */
303 pkt.ip.ip_tos = 0;
304 pkt.ip.ip_len = htons(ip_len);
305 pkt.ip.ip_id = htons(ip_id++);
306 pkt.ip.ip_off = 0;
307 pkt.ip.ip_ttl = 64;
308 pkt.ip.ip_p = 17; /* proto UDP */
309 pkt.ip.ip_sum = 0;
310 pkt.ip.ip_src = giaddr;
311 if (inm.flags & htons(BOOTP_BROADCAST)) /* check bcast bit */
312 pkt.ip.ip_dst = 0xffffffff; /* 255.255.255.255 */
313 else
314 pkt.ip.ip_dst = yiaddr.s_addr;
316 pkt.ip.uh_sport = htons(BOOTPS);
317 pkt.ip.uh_dport = htons(BOOTPC);
318 pkt.ip.uh_ulen = htons(udp_len);
319 pkt.ip.uh_sum = 0;
321 pkt.ip.ip_sum = ~htons(ip4sum(0, &pkt.ip, 20));
322 sum = 17 + udp_len;
323 sum = ip4sum(sum, &pkt.ip.ip_src, 8);
324 sum = ip4sum(sum, &pkt.ip.uh_sport, udp_len);
325 if (sum != 0xffff)
326 pkt.ip.uh_sum = ~htons(sum);
327 else
328 pkt.ip.uh_sum = 0xffff;
330 write_to_serial(slipfd, &pkt, ip_len);
331 if (msg_type == DHCPACK) {
332 printf("DHCPACK %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x IP %s\n",
333 pkt.m.chaddr[0], pkt.m.chaddr[1], pkt.m.chaddr[2], pkt.m.chaddr[3],
334 pkt.m.chaddr[4], pkt.m.chaddr[5], pkt.m.chaddr[6], pkt.m.chaddr[7],
335 inet_ntoa(yiaddr));
336 /* ssystem("arp -s %s auto pub only", inet_ntoa(yiaddr)); */
341 * Internet checksum in host byte order.
343 u_int16_t
344 ip4sum(u_int16_t sum, const void *_p, u_int16_t len)
346 u_int16_t t;
347 const u_int8_t *p = _p;
348 const u_int8_t *end = p + len;
350 while(p < (end-1)) {
351 t = (p[0] << 8) + p[1];
352 sum += t;
353 if (sum < t) sum++;
354 p += 2;
356 if(p < end) {
357 t = (p[0] << 8) + 0;
358 sum += t;
359 if (sum < t) sum++;
361 return sum;
365 check_ip(const struct ip *ip, unsigned ip_len)
367 u_int16_t sum, ip_hl;
369 /* Check IP version and length. */
370 if((ip->ip_vhl & IP_V) != IP_V4)
371 return -1;
373 if(ntohs(ip->ip_len) > ip_len)
374 return -2;
376 if(ntohs(ip->ip_len) < ip_len)
377 return -3;
379 /* Check IP header. */
380 ip_hl = 4*(ip->ip_vhl & IP_HL);
381 sum = ip4sum(0, ip, ip_hl);
382 if(sum != 0xffff && sum != 0x0)
383 return -4;
385 if(ip->ip_p == 6 || ip->ip_p == 17) { /* Check TCP or UDP header. */
386 u_int16_t tcp_len = ip_len - ip_hl;
388 /* Sum pseudoheader. */
389 sum = ip->ip_p + tcp_len; /* proto and len, no carry */
390 sum = ip4sum(sum, &ip->ip_src, 8); /* src and dst */
392 /* Sum TCP/UDP header and data. */
393 sum = ip4sum(sum, (u_int8_t*)ip + ip_hl, tcp_len);
395 /* Failed checksum test? */
396 if (sum != 0xffff && sum != 0x0) {
397 if (ip->ip_p == 6) { /* TCP == 6 */
398 return -5;
399 } else { /* UDP */
400 /* Deal with disabled UDP checksums. */
401 if (ip->uh_sum != 0)
402 return -6;
405 } else if (ip->ip_p == 1) { /* ICMP */
406 u_int16_t icmp_len = ip_len - ip_hl;
408 sum = ip4sum(0, (u_int8_t*)ip + ip_hl, icmp_len);
409 if(sum != 0xffff && sum != 0x0)
410 return -7;
412 return 0;
416 is_sensible_string(const unsigned char *s, int len)
418 int i;
419 for(i = 1; i < len; i++) {
420 if(s[i] == 0 || s[i] == '\r' || s[i] == '\n' || s[i] == '\t') {
421 continue;
422 } else if(s[i] < ' ' || '~' < s[i]) {
423 return 0;
426 return 1;
430 ssystem(const char *fmt, ...) __attribute__((__format__ (__printf__, 1, 2)));
433 ssystem(const char *fmt, ...)
435 char cmd[128];
436 va_list ap;
437 va_start(ap, fmt);
438 vsnprintf(cmd, sizeof(cmd), fmt, ap);
439 va_end(ap);
440 printf("%s\n", cmd);
441 fflush(stdout);
442 return system(cmd);
445 #define SLIP_END 0300
446 #define SLIP_ESC 0333
447 #define SLIP_ESC_END 0334
448 #define SLIP_ESC_ESC 0335
451 * Read from serial, when we have a packet write it to tun. No output
452 * buffering, input buffered by stdio.
454 void
455 serial_to_tun(FILE *inslip, int outfd)
457 static union {
458 unsigned char inbuf[2000];
459 struct ip iphdr;
460 } uip;
461 static int inbufptr = 0;
463 int ret;
464 unsigned char c;
466 #ifdef linux
467 ret = fread(&c, 1, 1, inslip);
468 if(ret == -1 || ret == 0) err(1, "serial_to_tun: read");
469 goto after_fread;
470 #endif
472 read_more:
473 if(inbufptr >= sizeof(uip.inbuf)) {
474 inbufptr = 0;
476 ret = fread(&c, 1, 1, inslip);
477 #ifdef linux
478 after_fread:
479 #endif
480 if(ret == -1) {
481 err(1, "serial_to_tun: read");
483 if(ret == 0) {
484 clearerr(inslip);
485 return;
486 fprintf(stderr, "serial_to_tun: EOF\n");
487 exit(1);
489 /* fprintf(stderr, ".");*/
490 switch(c) {
491 case SLIP_END:
492 if(inbufptr > 0) {
494 * Sanity checks.
496 #define DEBUG_LINE_MARKER '\r'
497 int ecode;
498 ecode = check_ip(&uip.iphdr, inbufptr);
499 if(ecode < 0 && inbufptr == 8 && strncmp(uip.inbuf, "=IPA", 4) == 0) {
500 static struct in_addr ipa;
502 inbufptr = 0;
503 if(memcmp(&ipa, &uip.inbuf[4], sizeof(ipa)) == 0) {
504 break;
507 /* New address. */
508 if(ipa.s_addr != 0) {
509 #ifdef linux
510 ssystem("route delete -net %s netmask %s dev %s",
511 inet_ntoa(ipa), "255.255.255.255", tundev);
512 #else
513 ssystem("route delete -net %s -netmask %s -interface %s",
514 inet_ntoa(ipa), "255.255.255.255", tundev);
515 #endif
518 memcpy(&ipa, &uip.inbuf[4], sizeof(ipa));
519 if(ipa.s_addr != 0) {
520 #ifdef linux
521 ssystem("route add -net %s netmask %s dev %s",
522 inet_ntoa(ipa), "255.255.255.255", tundev);
523 #else
524 ssystem("route add -net %s -netmask %s -interface %s",
525 inet_ntoa(ipa), "255.255.255.255", tundev);
526 #endif
528 break;
529 } else if(ecode < 0) {
531 * If sensible ASCII string, print it as debug info!
533 if(uip.inbuf[0] == DEBUG_LINE_MARKER) {
534 fwrite(uip.inbuf + 1, inbufptr - 1, 1, stderr);
535 } else if(is_sensible_string(uip.inbuf, inbufptr)) {
536 fwrite(uip.inbuf, inbufptr, 1, stderr);
537 } else {
538 fprintf(stderr,
539 "serial_to_tun: drop packet len=%d ecode=%d\n",
540 inbufptr, ecode);
542 inbufptr = 0;
543 break;
545 PROGRESS("s");
547 if(dhsock != -1) {
548 struct ip *ip = (void *)uip.inbuf;
549 if(ip->ip_p == 17 && ip->ip_dst == 0xffffffff /* UDP and broadcast */
550 && ip->uh_sport == ntohs(BOOTPC) && ip->uh_dport == ntohs(BOOTPS)) {
551 relay_dhcp_to_server(ip, inbufptr);
552 inbufptr = 0;
555 if(write(outfd, uip.inbuf, inbufptr) != inbufptr) {
556 err(1, "serial_to_tun: write");
558 inbufptr = 0;
560 break;
562 case SLIP_ESC:
563 if(fread(&c, 1, 1, inslip) != 1) {
564 clearerr(inslip);
565 /* Put ESC back and give up! */
566 ungetc(SLIP_ESC, inslip);
567 return;
570 switch(c) {
571 case SLIP_ESC_END:
572 c = SLIP_END;
573 break;
574 case SLIP_ESC_ESC:
575 c = SLIP_ESC;
576 break;
578 /* FALLTHROUGH */
579 default:
580 uip.inbuf[inbufptr++] = c;
581 break;
584 goto read_more;
587 unsigned char slip_buf[2000];
588 int slip_end, slip_begin;
590 void
591 slip_send(int fd, unsigned char c)
593 if (slip_end >= sizeof(slip_buf))
594 err(1, "slip_send overflow");
595 slip_buf[slip_end] = c;
596 slip_end++;
600 slip_empty()
602 return slip_end == 0;
605 void
606 slip_flushbuf(int fd)
608 int n;
610 if (slip_empty())
611 return;
613 n = write(fd, slip_buf + slip_begin, (slip_end - slip_begin));
615 if(n == -1 && errno != EAGAIN) {
616 err(1, "slip_flushbuf write failed");
617 } else if(n == -1) {
618 PROGRESS("Q"); /* Outqueueis full! */
619 } else {
620 slip_begin += n;
621 if(slip_begin == slip_end) {
622 slip_begin = slip_end = 0;
627 void
628 write_to_serial(int outfd, void *inbuf, int len)
630 u_int8_t *p = inbuf;
631 int i, ecode;
632 struct ip *iphdr = inbuf;
635 * Sanity checks.
637 ecode = check_ip(inbuf, len);
638 if(ecode < 0) {
639 fprintf(stderr, "tun_to_serial: drop packet %d\n", ecode);
640 return;
643 if(iphdr->ip_id == 0 && iphdr->ip_off & IP_DF) {
644 uint16_t nid = htons(ip_id++);
645 iphdr->ip_id = nid;
646 nid = ~nid; /* negate */
647 iphdr->ip_sum += nid; /* add */
648 if(iphdr->ip_sum < nid) { /* 1-complement overflow? */
649 iphdr->ip_sum++;
651 ecode = check_ip(inbuf, len);
652 if(ecode < 0) {
653 fprintf(stderr, "tun_to_serial: drop packet %d\n", ecode);
654 return;
658 /* It would be ``nice'' to send a SLIP_END here but it's not
659 * really necessary.
661 /* slip_send(outfd, SLIP_END); */
663 for(i = 0; i < len; i++) {
664 switch(p[i]) {
665 case SLIP_END:
666 slip_send(outfd, SLIP_ESC);
667 slip_send(outfd, SLIP_ESC_END);
668 break;
669 case SLIP_ESC:
670 slip_send(outfd, SLIP_ESC);
671 slip_send(outfd, SLIP_ESC_ESC);
672 break;
673 default:
674 slip_send(outfd, p[i]);
675 break;
679 slip_send(outfd, SLIP_END);
680 PROGRESS("t");
685 * Read from tun, write to slip.
687 void
688 tun_to_serial(int infd, int outfd)
690 static union {
691 unsigned char inbuf[2000];
692 struct ip iphdr;
693 } uip;
694 int size;
696 if((size = read(infd, uip.inbuf, 2000)) == -1) err(1, "tun_to_serial: read");
698 write_to_serial(outfd, uip.inbuf, size);
701 #ifndef BAUDRATE
702 #define BAUDRATE B115200
703 #endif
704 speed_t b_rate = BAUDRATE;
706 void
707 stty_telos(int fd)
709 struct termios tty;
710 speed_t speed = b_rate;
711 int i;
713 if(tcflush(fd, TCIOFLUSH) == -1) err(1, "tcflush");
715 if(tcgetattr(fd, &tty) == -1) err(1, "tcgetattr");
717 cfmakeraw(&tty);
719 /* Nonblocking read. */
720 tty.c_cc[VTIME] = 0;
721 tty.c_cc[VMIN] = 0;
722 tty.c_cflag &= ~CRTSCTS;
723 tty.c_cflag &= ~HUPCL;
724 tty.c_cflag &= ~CLOCAL;
726 cfsetispeed(&tty, speed);
727 cfsetospeed(&tty, speed);
729 if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) err(1, "tcsetattr");
731 #if 1
732 /* Nonblocking read and write. */
733 /* if(fcntl(fd, F_SETFL, O_NONBLOCK) == -1) err(1, "fcntl"); */
735 tty.c_cflag |= CLOCAL;
736 if(tcsetattr(fd, TCSAFLUSH, &tty) == -1) err(1, "tcsetattr");
738 i = TIOCM_DTR;
739 if(ioctl(fd, TIOCMBIS, &i) == -1) err(1, "ioctl");
740 #endif
742 usleep(10*1000); /* Wait for hardware 10ms. */
744 /* Flush input and output buffers. */
745 if(tcflush(fd, TCIOFLUSH) == -1) err(1, "tcflush");
749 devopen(const char *dev, int flags)
751 char t[32];
752 strcpy(t, "/dev/");
753 strcat(t, dev);
754 return open(t, flags);
757 #ifdef linux
758 #include <linux/if.h>
759 #include <linux/if_tun.h>
762 tun_alloc(char *dev)
764 struct ifreq ifr;
765 int fd, err;
767 if( (fd = open("/dev/net/tun", O_RDWR)) < 0 )
768 return -1;
770 memset(&ifr, 0, sizeof(ifr));
772 /* Flags: IFF_TUN - TUN device (no Ethernet headers)
773 * IFF_TAP - TAP device
775 * IFF_NO_PI - Do not provide packet information
777 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
778 if(*dev != 0)
779 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
781 if((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
782 close(fd);
783 return err;
785 strcpy(dev, ifr.ifr_name);
786 return fd;
788 #else
790 tun_alloc(char *dev)
792 return devopen(dev, O_RDWR);
794 #endif
796 const char *ipaddr;
797 const char *netmask;
799 void
800 cleanup(void)
802 ssystem("ifconfig %s down", tundev);
803 #ifndef linux
804 ssystem("sysctl -w net.inet.ip.forwarding=0");
805 #endif
806 /* ssystem("arp -d %s", ipaddr); */
807 ssystem("netstat -nr"
808 " | awk '{ if ($2 == \"%s\") print \"route delete -net \"$1; }'"
809 " | sh",
810 tundev);
813 void
814 sigcleanup(int signo)
816 fprintf(stderr, "signal %d\n", signo);
817 exit(0); /* exit(0) will call cleanup() */
820 static int got_sigalarm;
822 void
823 sigalarm(int signo)
825 got_sigalarm = 1;
826 return;
829 void
830 sigalarm_reset()
832 #ifdef linux
833 #define TIMEOUT (997*1000)
834 #else
835 #define TIMEOUT (2451*1000)
836 #endif
837 ualarm(TIMEOUT, TIMEOUT);
838 got_sigalarm = 0;
841 void
842 ifconf(const char *tundev, const char *ipaddr, const char *netmask)
844 struct in_addr netname;
845 netname.s_addr = inet_addr(ipaddr) & inet_addr(netmask);
847 #ifdef linux
848 ssystem("ifconfig %s inet `hostname` up", tundev);
849 if(strcmp(ipaddr, "0.0.0.0") != 0) {
850 ssystem("route add -net %s netmask %s dev %s",
851 inet_ntoa(netname), netmask, tundev);
853 #else
854 ssystem("ifconfig %s inet `hostname` %s up", tundev, ipaddr);
855 if(strcmp(ipaddr, "0.0.0.0") != 0) {
856 ssystem("route add -net %s -netmask %s -interface %s",
857 inet_ntoa(netname), netmask, tundev);
859 ssystem("sysctl -w net.inet.ip.forwarding=1");
860 #endif /* !linux */
862 ssystem("ifconfig %s\n", tundev);
866 main(int argc, char **argv)
868 int c;
869 int tunfd, slipfd, maxfd;
870 int ret;
871 fd_set rset, wset;
872 FILE *inslip;
873 const char *siodev = NULL;
874 const char *dhcp_server = NULL;
875 u_int16_t myport = BOOTPS, dhport = BOOTPS;
876 int baudrate = -2;
878 ip_id = getpid() * time(NULL);
880 setvbuf(stdout, NULL, _IOLBF, 0); /* Line buffered output. */
882 while((c = getopt(argc, argv, "B:D:hs:t:")) != -1) {
883 switch (c) {
884 case 'B':
885 baudrate = atoi(optarg);
886 break;
888 case 'D':
889 dhcp_server = optarg;
890 break;
892 case 's':
893 if(strncmp("/dev/", optarg, 5) == 0) {
894 siodev = optarg + 5;
895 } else {
896 siodev = optarg;
898 break;
900 case 't':
901 if(strncmp("/dev/", optarg, 5) == 0) {
902 strcpy(tundev, optarg + 5);
903 } else {
904 strcpy(tundev, optarg);
906 break;
908 case '?':
909 case 'h':
910 default:
911 err(1, "usage: tunslip [-B baudrate] [-s siodev] [-t tundev] [-D dhcp-server] ipaddress netmask [dhcp-server]");
912 break;
915 argc -= (optind - 1);
916 argv += (optind - 1);
918 if(argc != 3 && argc != 4) {
919 err(1, "usage: tunslip [-s siodev] [-t tundev] [-D dhcp-server] ipaddress netmask [dhcp-server]");
921 ipaddr = argv[1];
922 netmask = argv[2];
923 circuit_addr = inet_addr(ipaddr);
924 netaddr = inet_addr(ipaddr) & inet_addr(netmask);
926 switch(baudrate) {
927 case -2:
928 break; /* Use default. */
929 case 9600:
930 b_rate = B9600;
931 break;
932 case 19200:
933 b_rate = B19200;
934 break;
935 case 38400:
936 b_rate = B38400;
937 break;
938 case 57600:
939 b_rate = B57600;
940 break;
941 case 115200:
942 b_rate = B115200;
943 break;
944 default:
945 err(1, "unknown baudrate %d", baudrate);
946 break;
950 * Set up DHCP relay agent socket and find the address of this relay
951 * agent.
953 if(argc == 4) {
954 dhcp_server = argv[3];
956 if(dhcp_server != NULL) {
957 struct sockaddr_in myaddr;
958 socklen_t len;
959 in_addr_t a;
961 if(strchr(dhcp_server, ':') != NULL) {
962 dhport = atoi(strchr(dhcp_server, ':') + 1);
963 myport = dhport + 1;
964 *strchr(dhcp_server, ':') = '\0';
966 a = inet_addr(dhcp_server);
967 if(a == -1) {
968 err(1, "illegal dhcp-server address");
970 #ifndef linux
971 dhaddr.sin_len = sizeof(dhaddr);
972 #endif
973 dhaddr.sin_family = AF_INET;
974 dhaddr.sin_port = htons(dhport);
975 dhaddr.sin_addr.s_addr = a;
977 dhsock = socket(AF_INET, SOCK_DGRAM, 0);
978 if(dhsock < 0) {
979 err (1, "socket");
981 memset(&myaddr, 0x0, sizeof(myaddr));
982 #ifndef linux
983 myaddr.sin_len = sizeof(myaddr);
984 #endif
985 myaddr.sin_family = AF_INET;
986 myaddr.sin_addr.s_addr = INADDR_ANY;
987 myaddr.sin_port = htons(myport);
988 if(bind(dhsock, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
989 err(1, "bind dhcp-relay");
992 if(connect(dhsock, (struct sockaddr *)&dhaddr, sizeof(dhaddr)) < 0) {
993 err(1, "connect to dhcp-server");
996 len = sizeof(myaddr);
997 if(getsockname(dhsock, (struct sockaddr *)&myaddr, &len) < 0) {
998 err(1, "getsockname dhsock");
1001 giaddr = myaddr.sin_addr.s_addr;
1004 * Don't want connected socket.
1006 close(dhsock);
1007 dhsock = socket(AF_INET, SOCK_DGRAM, 0);
1008 if(dhsock < 0) {
1009 err (1, "socket");
1011 myaddr.sin_family = AF_INET;
1012 myaddr.sin_addr.s_addr = INADDR_ANY;
1013 myaddr.sin_port = htons(myport);
1014 if(bind(dhsock, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
1015 err(1, "bind dhcp-relay");
1017 fprintf(stderr, "DHCP server at %s:%d\n", dhcp_server, dhport);
1020 if(siodev != NULL) {
1021 slipfd = devopen(siodev, O_RDWR | O_NONBLOCK);
1022 if(slipfd == -1) {
1023 err(1, "can't open siodev ``/dev/%s''", siodev);
1025 } else {
1026 static const char *siodevs[] = {
1027 "ttyUSB0", "cuaU0", "ucom0" /* linux, fbsd6, fbsd5 */
1029 int i;
1030 for(i = 0; i < 3; i++) {
1031 siodev = siodevs[i];
1032 slipfd = devopen(siodev, O_RDWR | O_NONBLOCK);
1033 if (slipfd != -1)
1034 break;
1036 if(slipfd == -1) {
1037 err(1, "can't open siodev");
1040 fprintf(stderr, "slip started on ``/dev/%s''\n", siodev);
1041 stty_telos(slipfd);
1042 slip_send(slipfd, SLIP_END);
1043 inslip = fdopen(slipfd, "r");
1044 if(inslip == NULL) err(1, "main: fdopen");
1046 tunfd = tun_alloc(tundev);
1047 if(tunfd == -1) err(1, "main: open");
1048 fprintf(stderr, "opened device ``/dev/%s''\n", tundev);
1050 atexit(cleanup);
1051 signal(SIGHUP, sigcleanup);
1052 signal(SIGTERM, sigcleanup);
1053 signal(SIGINT, sigcleanup);
1054 signal(SIGALRM, sigalarm);
1055 ifconf(tundev, ipaddr, netmask);
1057 while(1) {
1058 maxfd = 0;
1059 FD_ZERO(&rset);
1060 FD_ZERO(&wset);
1062 if(got_sigalarm) {
1063 /* Send "?IPA". */
1064 slip_send(slipfd, '?');
1065 slip_send(slipfd, 'I');
1066 slip_send(slipfd, 'P');
1067 slip_send(slipfd, 'A');
1068 slip_send(slipfd, SLIP_END);
1069 got_sigalarm = 0;
1072 if(!slip_empty()) { /* Anything to flush? */
1073 FD_SET(slipfd, &wset);
1076 FD_SET(slipfd, &rset); /* Read from slip ASAP! */
1077 if(slipfd > maxfd) maxfd = slipfd;
1079 /* We only have one packet at a time queued for slip output. */
1080 if(slip_empty()) {
1081 FD_SET(tunfd, &rset);
1082 if(tunfd > maxfd) maxfd = tunfd;
1083 if(dhsock != -1) {
1084 FD_SET(dhsock, &rset);
1085 if(dhsock > maxfd) maxfd = dhsock;
1089 ret = select(maxfd + 1, &rset, &wset, NULL, NULL);
1090 if(ret == -1 && errno != EINTR) {
1091 err(1, "select");
1092 } else if(ret > 0) {
1093 if(FD_ISSET(slipfd, &rset)) {
1094 serial_to_tun(inslip, tunfd);
1097 if(FD_ISSET(slipfd, &wset)) {
1098 slip_flushbuf(slipfd);
1099 sigalarm_reset();
1102 if(slip_empty() && FD_ISSET(tunfd, &rset)) {
1103 tun_to_serial(tunfd, slipfd);
1104 slip_flushbuf(slipfd);
1105 sigalarm_reset();
1108 if(dhsock != -1 && slip_empty() && FD_ISSET(dhsock, &rset)) {
1109 relay_dhcp_to_client(slipfd);
1110 slip_flushbuf(slipfd);