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