chiark / gitweb /
doc: Fix distribution of generated manpages.
[tripe] / server / tun-linux.c
1 /* -*-c-*-
2  *
3  * $Id$
4  *
5  * Tunnel interface based on Linux TUN/TAP driver
6  *
7  * (c) 2003 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 #ifdef TUN_LINUX
36 #  include <sys/ioctl.h>
37 #  include <linux/if.h>
38 #  include <linux/if_tun.h>
39 #endif
40
41 /*----- Main code ---------------------------------------------------------*/
42
43 #ifdef TUN_LINUX
44
45 struct tunnel {
46   const tunnel_ops *ops;                /* Pointer to operations */
47   sel_file f;                           /* Selector for TUN/TAP device */
48   struct peer *p;                       /* Pointer to my peer */
49 };  
50
51 /* --- @t_read@ --- *
52  *
53  * Arguments:   @int fd@ = file descriptor to read
54  *              @unsigned mode@ = what's happened
55  *              @void *v@ = pointer to tunnel block
56  *
57  * Returns:     ---
58  *
59  * Use:         Reads data from the tunnel.
60  */
61
62 static void t_read(int fd, unsigned mode, void *v)
63 {
64   tunnel *t = v;
65   ssize_t n;
66   buf b;
67
68   n = read(fd, buf_i, sizeof(buf_i));
69   if (n < 0) {
70     a_warn("TUN", "%s", p_ifname(t->p), "read-error", "?ERRNO", A_END);
71     return;
72   }
73   IF_TRACING(T_TUNNEL, {
74     trace(T_TUNNEL, "tun-linux: packet arrived");
75     trace_block(T_PACKET, "tun-linux: packet contents", buf_i, n);
76   })
77   buf_init(&b, buf_i, n);
78   p_tun(t->p, &b);
79 }
80
81 /* --- @t_init@ --- *
82  *
83  * Arguments:   ---
84  *
85  * Returns:     ---
86  *
87  * Use:         Initializes the tunneling system.  Maybe this will require
88  *              opening file descriptors or something.
89  */
90
91 static void t_init(void) { return; }
92
93 /* --- @t_create@ --- *
94  *
95  * Arguments:   @peer *p@ = pointer to peer block
96  *              @char **ifn@ = where to put the interface name
97  *
98  * Returns:     A tunnel block if it worked, or null on failure.
99  *
100  * Use:         Initializes a new tunnel.
101  */
102
103 static tunnel *t_create(peer *p, char **ifn)
104 {
105   int fd;
106   int f;
107   struct ifreq iff;
108   tunnel *t;
109
110   if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
111     a_warn("TUN", "-", "linux",
112             "open-error", "/dev/net/tun", "?ERRNO",
113             A_END);
114     return (0);
115   }
116   fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
117   memset(&iff, 0, sizeof(iff));
118   iff.ifr_name[0] = 0;
119   iff.ifr_flags = IFF_TUN | IFF_NO_PI;
120   if ((f = ioctl(fd, TUNSETIFF, &iff)) < 0) {
121     a_warn("TUN", "-", "linux", "config-error", "?ERRNO", A_END);
122     close(fd);
123     return (0);
124   }
125   t = CREATE(tunnel);
126   t->ops = &tun_linux;
127   t->p = p;
128   sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
129   sel_addfile(&t->f);
130   iff.ifr_name[IFNAMSIZ - 1] = 0;
131   *ifn = xstrdup(iff.ifr_name);
132   T( trace(T_TUNNEL, "tun-linux: 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-linux: inject decrypted packet");
151     trace_block(T_PACKET, "tunnel: 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_linux = {
173   "linux",
174   t_init,
175   t_create,
176   0,
177   t_inject,
178   t_destroy
179 };
180
181 #endif
182
183 /*----- That's all, folks -------------------------------------------------*/