chiark / gitweb /
Don't try to run bogus startup scripts.
[tripe] / tun-bsd.c
1 /* -*-c-*-
2  *
3  * $Id: tun-bsd.c,v 1.1 2001/02/05 19:48:18 mdw Exp $
4  *
5  * Tunnel interface for 4.4BSD-derived systems
6  *
7  * (c) 2001 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of Trivial IP Encryption (TrIPE).
13  *
14  * TrIPE is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  * 
19  * TrIPE is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with TrIPE; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 /*----- Revision history --------------------------------------------------* 
30  *
31  * $Log: tun-bsd.c,v $
32  * Revision 1.1  2001/02/05 19:48:18  mdw
33  * Initial support for BSD tunnel devices.
34  *
35  */
36
37 /*----- Header files ------------------------------------------------------*/
38
39 #include "tripe.h"
40
41 /*----- Main code ---------------------------------------------------------*/
42
43 #if TUN_TYPE != TUN_BSD
44 #  error "Tunnel type mismatch: fix the Makefile"
45 #endif
46
47 /* --- @t_read@ --- *
48  *
49  * Arguments:   @int fd@ = file descriptor to read
50  *              @unsigned mode@ = what's happened
51  *              @void *v@ = pointer to tunnel block
52  *
53  * Returns:     ---
54  *
55  * Use:         Reads data from the tunnel.
56  */
57
58 void t_read(int fd, unsigned mode, void *v)
59 {
60   tunnel *t = v;
61   ssize_t n;
62   buf b;
63
64   n = read(fd, buf_i, sizeof(buf_i));
65   if (n < 0) {
66     a_warn("tunnel read failed (%s): %s", tun_ifname(t), strerror(errno));
67     return;
68   }
69   IF_TRACING(T_TUNNEL, {
70     trace(T_TUNNEL, "tunnel: packet arrived");
71     trace_block(T_PACKET, "tunnel: packet contents", buf_i, n);
72   })
73   buf_init(&b, buf_i, n);
74   p_tun(t->p, &b);
75 }
76
77 /* --- @tun_init@ --- *
78  *
79  * Arguments:   ---
80  *
81  * Returns:     ---
82  *
83  * Use:         Initializes the tunneling system.  Maybe this will require
84  *              opening file descriptors or something.
85  */
86
87 void tun_init(void)
88 {
89   return;
90 }
91
92 /* --- @tun_create@ --- *
93  *
94  * Arguments:   @tunnel *t@ = pointer to tunnel block
95  *              @peer *p@ = pointer to peer block
96  *
97  * Returns:     Zero if it worked, nonzero on failure.
98  *
99  * Use:         Initializes a new tunnel.
100  */
101
102 int tun_create(tunnel *t, peer *p)
103 {
104   int fd;
105   unsigned n;
106   char buf[16];
107
108   n = 0;
109   for (;;) {
110     sprintf(buf, "/dev/tun%u", n);
111     if ((fd = open("/dev/unet", O_RDWR)) >= 0)
112       break;
113     switch (errno) {
114       case EBUSY:
115         T( trace(T_TUNNEL, "tunnel device %u busy: skipping", n); )
116         break;
117       case ENOENT:
118         a_warn("no suitable tunnel devices found");
119         return (-1);
120       default:
121         a_warn("error opening `%s': %s (skipping)", buf, strerror(errno));
122         break;
123     }
124     n++;
125   }
126
127   fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
128   t->p = p;
129   t->n = n;
130   sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
131   sel_addfile(&t->f);
132   T( trace(T_TUNNEL, "tunnel: attached interface %s to peer `%s'",
133            tun_ifname(t), p_name(p)); )
134   return (0);
135 }
136
137 /* --- @tun_ifname@ --- *
138  *
139  * Arguments:   @tunnel *t@ = pointer to tunnel block
140  *
141  * Returns:     A pointer to the tunnel's interface name.
142  */
143
144 const char *tun_ifname(tunnel *t)
145 {
146   static char buf[8];
147   sprintf(buf, "tun%u", t->n);
148   return (buf);
149 }
150
151 /* --- @tun_inject@ --- *
152  *
153  * Arguments:   @tunnel *t@ = pointer to tunnel block
154  *              @buf *b@ = buffer to send
155  *
156  * Returns:     ---
157  *
158  * Use:         Injects a packet into the local network stack.
159  */
160
161 void tun_inject(tunnel *t, buf *b)
162 {
163   IF_TRACING(T_TUNNEL, {
164     trace(T_TUNNEL, "tunnel: inject decrypted packet");
165     trace_block(T_PACKET, "tunnel: packet contents", BBASE(b), BLEN(b));
166   })
167   write(t->f.fd, BBASE(b), BLEN(b));
168 }
169
170 /* --- @tun_destroy@ --- *
171  *
172  * Arguments:   @tunnel *t@ = pointer to tunnel block
173  *
174  * Returns:     ---
175  *
176  * Use:         Destroys a tunnel.
177  */
178
179 void tun_destroy(tunnel *t)
180 {
181   sel_rmfile(&t->f);
182   close(t->f.fd);
183 }
184
185 /*----- That's all, folks -------------------------------------------------*/