chiark / gitweb /
Rename ethereal -> wireshark.
[tripe] / tun-bsd.c
1 /* -*-c-*-
2  *
3  * $Id$
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 /*----- Header files ------------------------------------------------------*/
30
31 #define TUN_INTERNALS
32
33 #include "tripe.h"
34
35 /*----- Main code ---------------------------------------------------------*/
36
37 #ifdef TUN_BSD
38
39 struct tunnel {
40   const tunnel_ops *ops;                /* Pointer to operations */
41   sel_file f;                           /* Selector for tunnel device */
42   struct peer *p;                       /* Pointer to my peer */
43   unsigned n;                           /* Number of my tunnel device */
44 };  
45
46 /* --- @t_ifname@ --- *
47  *
48  * Arguments:   @tunnel *t@ = pointer to tunnel block
49  *
50  * Returns:     A pointer to the tunnel's interface name.
51  */
52
53 static const char *t_ifname(tunnel *t)
54 {
55   static char buf[8];
56   sprintf(buf, "tun%u", t->n);
57   return (buf);
58 }
59
60 /* --- @t_read@ --- *
61  *
62  * Arguments:   @int fd@ = file descriptor to read
63  *              @unsigned mode@ = what's happened
64  *              @void *v@ = pointer to tunnel block
65  *
66  * Returns:     ---
67  *
68  * Use:         Reads data from the tunnel.
69  */
70
71 static void t_read(int fd, unsigned mode, void *v)
72 {
73   tunnel *t = v;
74   ssize_t n;
75   buf b;
76
77   n = read(fd, buf_i, sizeof(buf_i));
78   if (n < 0) {
79     a_warn("TUN", "%s", t_ifname(t), "read-error", "?ERRNO", A_END);
80     return;
81   }
82   IF_TRACING(T_TUNNEL, {
83     trace(T_TUNNEL, "tun-bsd: packet arrived");
84     trace_block(T_PACKET, "tun-bsd: packet contents", buf_i, n);
85   })
86   buf_init(&b, buf_i, n);
87   p_tun(t->p, &b);
88 }
89
90 /* --- @t_init@ --- *
91  *
92  * Arguments:   ---
93  *
94  * Returns:     ---
95  *
96  * Use:         Initializes the tunneling system.  Maybe this will require
97  *              opening file descriptors or something.
98  */
99
100 static void t_init(void) { return; }
101
102 /* --- @t_create@ --- *
103  *
104  * Arguments:   @peer *p@ = pointer to peer block
105  *
106  * Returns:     A tunnel block if it worked, or null on failure.
107  *
108  * Use:         Initializes a new tunnel.
109  */
110
111 static tunnel *t_create(peer *p)
112 {
113   int fd;
114   unsigned n;
115   tunnel *t;
116   char buf[16];
117
118   n = 0;
119   for (;;) {
120     sprintf(buf, "/dev/tun%u", n);
121     if ((fd = open(buf, O_RDWR)) >= 0)
122       break;
123     switch (errno) {
124       case EBUSY:
125         T( trace(T_TUNNEL, "tunnel device %u busy: skipping", n); )
126         break;
127       case ENOENT:
128         a_warn("TUN", "-", "bsd", "no-tunnel-devices", A_END);
129         return (0);
130       default:
131         a_warn("TUN", "-", "open-error", "%s", buf, "?ERRNO", A_END);
132         break;
133     }
134     n++;
135   }
136
137   t = CREATE(tunnel);
138   t->ops = &tun_bsd;
139   fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
140   t->p = p;
141   t->n = n;
142   sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
143   sel_addfile(&t->f);
144   T( trace(T_TUNNEL, "tun-bsd: attached interface %s to peer `%s'",
145            t_ifname(t), p_name(p)); )
146   return (t);
147 }
148
149 /* --- @t_inject@ --- *
150  *
151  * Arguments:   @tunnel *t@ = pointer to tunnel block
152  *              @buf *b@ = buffer to send
153  *
154  * Returns:     ---
155  *
156  * Use:         Injects a packet into the local network stack.
157  */
158
159 static void t_inject(tunnel *t, buf *b)
160 {
161   IF_TRACING(T_TUNNEL, {
162     trace(T_TUNNEL, "tun-bsd: inject decrypted packet");
163     trace_block(T_PACKET, "tun-bsd: packet contents", BBASE(b), BLEN(b));
164   })
165   write(t->f.fd, BBASE(b), BLEN(b));
166 }
167
168 /* --- @t_destroy@ --- *
169  *
170  * Arguments:   @tunnel *t@ = pointer to tunnel block
171  *
172  * Returns:     ---
173  *
174  * Use:         Destroys a tunnel.
175  */
176
177 static void t_destroy(tunnel *t)
178 {
179   sel_rmfile(&t->f);
180   close(t->f.fd);
181   DESTROY(t);
182 }
183
184 const tunnel_ops tun_bsd = {
185   "bsd",
186   t_init,
187   t_create,
188   t_ifname,
189   t_inject,
190   t_destroy
191 };
192
193 #endif
194
195 /*----- That's all, folks -------------------------------------------------*/