chiark / gitweb /
Patch up zero-knowledge property by passing an encrypted log with a
[tripe] / tun-unet.c
1 /* -*-c-*-
2  *
3  * $Id: tun-unet.c,v 1.4 2001/02/19 19:10:45 mdw Exp $
4  *
5  * Tunnel interface based on Linux Usernet
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-unet.c,v $
32  * Revision 1.4  2001/02/19 19:10:45  mdw
33  * Set unet devices to be point-to-point.
34  *
35  * Revision 1.3  2001/02/05 19:55:00  mdw
36  * Guard against inappropriate compilation.
37  *
38  * Revision 1.2  2001/02/04 17:10:58  mdw
39  * Make file descriptors be nonblocking and close-on-exec.
40  *
41  * Revision 1.1  2001/02/03 20:26:37  mdw
42  * Initial checkin.
43  *
44  */
45
46 /*----- Header files ------------------------------------------------------*/
47
48 #include "tripe.h"
49
50 #include <sys/ioctl.h>
51 #include <net/if.h>
52 #include <unet.h>
53
54 /*----- Main code ---------------------------------------------------------*/
55
56 #if TUN_TYPE != TUN_UNET
57 #  error "Tunnel type mismatch: fix the Makefile"
58 #endif
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 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("tunnel read failed (%s): %s", tun_ifname(t), strerror(errno));
80     return;
81   }
82   IF_TRACING(T_TUNNEL, {
83     trace(T_TUNNEL, "tunnel: packet arrived");
84     trace_block(T_PACKET, "tunnel: packet contents", buf_i, n);
85   })
86   buf_init(&b, buf_i, n);
87   p_tun(t->p, &b);
88 }
89
90 /* --- @tun_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 void tun_init(void)
101 {
102   return;
103 }
104
105 /* --- @tun_create@ --- *
106  *
107  * Arguments:   @tunnel *t@ = pointer to tunnel block
108  *              @peer *p@ = pointer to peer block
109  *
110  * Returns:     Zero if it worked, nonzero on failure.
111  *
112  * Use:         Initializes a new tunnel.
113  */
114
115 int tun_create(tunnel *t, peer *p)
116 {
117   int fd;
118   int f;
119
120   if ((fd = open("/dev/unet", O_RDWR)) < 0) {
121     a_warn("open `/dev/unet' failed: %s", strerror(errno));
122     return (-1);
123   }
124   fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
125   if ((f = ioctl(fd, UNIOCGIFFLAGS)) < 0 ||
126       ioctl(fd, UNIOCSIFFLAGS, f | IFF_POINTOPOINT)) {
127     a_warn("couldn't set point-to-point flag: %s", strerror(errno));
128     close(fd);
129     return (-1);
130   }
131   t->p = p;
132   sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
133   sel_addfile(&t->f);
134   T( trace(T_TUNNEL, "tunnel: attached interface %s to peer `%s'",
135            tun_ifname(t), p_name(p)); )
136   return (0);
137 }
138
139 /* --- @tun_ifname@ --- *
140  *
141  * Arguments:   @tunnel *t@ = pointer to tunnel block
142  *
143  * Returns:     A pointer to the tunnel's interface name.
144  */
145
146 const char *tun_ifname(tunnel *t)
147 {
148   static char b[UNET_NAMEMAX];
149   struct unet_info uni;
150   if (ioctl(t->f.fd, UNIOCGINFO, &uni)) {
151     a_warn("ioctl(UNIOCGINFO) failed: %s", strerror(errno));
152     return ("<error>");
153   }
154   if (strlen(uni.uni_ifname) + 1 > sizeof(b)) {
155     a_warn("interface name too long!");
156     return ("<error>");
157   }
158   strcpy(b, uni.uni_ifname);
159   return (b);
160 }
161
162 /* --- @tun_inject@ --- *
163  *
164  * Arguments:   @tunnel *t@ = pointer to tunnel block
165  *              @buf *b@ = buffer to send
166  *
167  * Returns:     ---
168  *
169  * Use:         Injects a packet into the local network stack.
170  */
171
172 void tun_inject(tunnel *t, buf *b)
173 {
174   IF_TRACING(T_TUNNEL, {
175     trace(T_TUNNEL, "tunnel: inject decrypted packet");
176     trace_block(T_PACKET, "tunnel: packet contents", BBASE(b), BLEN(b));
177   })
178   write(t->f.fd, BBASE(b), BLEN(b));
179 }
180
181 /* --- @tun_destroy@ --- *
182  *
183  * Arguments:   @tunnel *t@ = pointer to tunnel block
184  *
185  * Returns:     ---
186  *
187  * Use:         Destroys a tunnel.
188  */
189
190 void tun_destroy(tunnel *t)
191 {
192   sel_rmfile(&t->f);
193   close(t->f.fd);
194 }
195
196 /*----- That's all, folks -------------------------------------------------*/