chiark / gitweb /
Build: Explicitly link against mLib or catacomb.
[tripe] / server / tun-unet.c
1 /* -*-c-*-
2  *
3  * Tunnel interface based on Linux Usernet
4  *
5  * (c) 2001 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Trivial IP Encryption (TrIPE).
11  *
12  * TrIPE is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * TrIPE is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with TrIPE; if not, write to the Free Software Foundation,
24  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 /*----- Header files ------------------------------------------------------*/
28
29 #define TUN_INTERNALS
30
31 #include "tripe.h"
32
33 #ifdef TUN_UNET
34 #  include <sys/ioctl.h>
35 #  include <net/if.h>
36 #  include <unet.h>
37 #endif
38
39 /*----- Main code ---------------------------------------------------------*/
40
41 #ifdef TUN_UNET
42
43 struct tunnel {
44   const tunnel_ops *ops;                /* Pointer to operations */
45   sel_file f;                           /* Selector for Usernet device */
46   struct peer *p;                       /* Pointer to my peer */
47 };
48
49 /* --- @t_read@ --- *
50  *
51  * Arguments:   @int fd@ = file descriptor to read
52  *              @unsigned mode@ = what's happened
53  *              @void *v@ = pointer to tunnel block
54  *
55  * Returns:     ---
56  *
57  * Use:         Reads data from the tunnel.
58  */
59
60 static void t_read(int fd, unsigned mode, void *v)
61 {
62   tunnel *t = v;
63   ssize_t n;
64   buf b;
65
66   n = read(fd, buf_i, sizeof(buf_i));
67   if (n < 0) {
68     a_warn("TUN", "%s", p_ifname(t->p), "read-error", "?ERRNO", A_END);
69     return;
70   }
71   IF_TRACING(T_TUNNEL, {
72     trace(T_TUNNEL, "tun-unet: packet arrived");
73     trace_block(T_PACKET, "tun-unet: packet contents", buf_i, n);
74   })
75   buf_init(&b, buf_i, n);
76   p_tun(t->p, &b);
77 }
78
79 /* --- @t_init@ --- *
80  *
81  * Arguments:   ---
82  *
83  * Returns:     ---
84  *
85  * Use:         Initializes the tunneling system.  Maybe this will require
86  *              opening file descriptors or something.
87  */
88
89 static void t_init(void) { return; }
90
91 /* --- @t_create@ --- *
92  *
93  * Arguments:   @tunnel *t@ = pointer to tunnel block
94  *              @peer *p@ = pointer to peer block
95  *              @char *ifn@ = where to put the interface name
96  *
97  * Returns:     A tunnel block if it worked, or null on failure.
98  *
99  * Use:         Initializes a new tunnel.
100  */
101
102 static tunnel *t_create(peer *p, char **ifn)
103 {
104   int fd;
105   tunnel *t;
106   int f;
107   struct unet_info uni;
108
109   if ((fd = open("/dev/unet", O_RDWR)) < 0) {
110     a_warn("TUN", "-", "unet", "open-error", "/dev/unet", "?ERRNO", A_END);
111     return (0);
112   }
113   fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
114   if ((f = ioctl(fd, UNIOCGIFFLAGS)) < 0 ||
115       ioctl(fd, UNIOCSIFFLAGS, f | IFF_POINTOPOINT)) {
116     a_warn("TUN", "-", "unet", "config-error", "?ERRNO", A_END);
117     close(fd);
118     return (0);
119   }
120   t = CREATE(tunnel);
121   t->ops = &tun_unet;
122   t->p = p;
123   sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
124   sel_addfile(&t->f);
125
126   if (ioctl(t->f.fd, UNIOCGINFO, &uni)) {
127     a_warn("TUN", "-", "unet", "getinfo-error", "?ERRNO", A_END);
128     return (0);
129   }
130   *ifn = xstrdup(uni.uni_ifname);
131   T( trace(T_TUNNEL, "tun-unet: attached interface %s to peer `%s'",
132            *ifn, p_name(p)); )
133   return (t);
134 }
135
136 /* --- @t_inject@ --- *
137  *
138  * Arguments:   @tunnel *t@ = pointer to tunnel block
139  *              @buf *b@ = buffer to send
140  *
141  * Returns:     ---
142  *
143  * Use:         Injects a packet into the local network stack.
144  */
145
146 static void t_inject(tunnel *t, buf *b)
147 {
148   IF_TRACING(T_TUNNEL, {
149     trace(T_TUNNEL, "tun-unet: inject decrypted packet");
150     trace_block(T_PACKET, "tun-unet: packet contents", BBASE(b), BLEN(b));
151   })
152   write(t->f.fd, BBASE(b), BLEN(b));
153 }
154
155 /* --- @t_destroy@ --- *
156  *
157  * Arguments:   @tunnel *t@ = pointer to tunnel block
158  *
159  * Returns:     ---
160  *
161  * Use:         Destroys a tunnel.
162  */
163
164 static void t_destroy(tunnel *t)
165   { sel_rmfile(&t->f); close(t->f.fd); DESTROY(t); }
166
167 const tunnel_ops tun_unet = {
168   "unet",
169   t_init,
170   t_create,
171   0,
172   t_inject,
173   t_destroy
174 };
175
176 #endif
177
178 /*----- That's all, folks -------------------------------------------------*/