5 * Tunnel interface based on Linux Usernet
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
29 /*----- Header files ------------------------------------------------------*/
36 # include <sys/ioctl.h>
41 /*----- Main code ---------------------------------------------------------*/
46 const tunnel_ops *ops; /* Pointer to operations */
47 sel_file f; /* Selector for Usernet device */
48 struct peer *p; /* Pointer to my peer */
53 * Arguments: @int fd@ = file descriptor to read
54 * @unsigned mode@ = what's happened
55 * @void *v@ = pointer to tunnel block
59 * Use: Reads data from the tunnel.
62 static void t_read(int fd, unsigned mode, void *v)
68 n = read(fd, buf_i, sizeof(buf_i));
70 a_warn("TUN", "%s", p_ifname(t->p), "read-error", "?ERRNO", A_END);
73 IF_TRACING(T_TUNNEL, {
74 trace(T_TUNNEL, "tun-unet: packet arrived");
75 trace_block(T_PACKET, "tun-unet: packet contents", buf_i, n);
77 buf_init(&b, buf_i, n);
87 * Use: Initializes the tunneling system. Maybe this will require
88 * opening file descriptors or something.
91 static void t_init(void) { return; }
93 /* --- @t_create@ --- *
95 * Arguments: @tunnel *t@ = pointer to tunnel block
96 * @peer *p@ = pointer to peer block
97 * @char *ifn@ = where to put the interface name
99 * Returns: A tunnel block if it worked, or null on failure.
101 * Use: Initializes a new tunnel.
104 static tunnel *t_create(peer *p, char **ifn)
109 struct unet_info uni;
111 if ((fd = open("/dev/unet", O_RDWR)) < 0) {
112 a_warn("TUN", "-", "unet", "open-error", "/dev/unet", "?ERRNO", A_END);
115 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
116 if ((f = ioctl(fd, UNIOCGIFFLAGS)) < 0 ||
117 ioctl(fd, UNIOCSIFFLAGS, f | IFF_POINTOPOINT)) {
118 a_warn("TUN", "-", "unet", "config-error", "?ERRNO", A_END);
125 sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
128 if (ioctl(t->f.fd, UNIOCGINFO, &uni)) {
129 a_warn("TUN", "-", "unet", "getinfo-error", "?ERRNO", A_END);
132 *ifn = xstrdup(uni.uni_ifname);
133 T( trace(T_TUNNEL, "tun-unet: attached interface %s to peer `%s'",
138 /* --- @t_inject@ --- *
140 * Arguments: @tunnel *t@ = pointer to tunnel block
141 * @buf *b@ = buffer to send
145 * Use: Injects a packet into the local network stack.
148 static void t_inject(tunnel *t, buf *b)
150 IF_TRACING(T_TUNNEL, {
151 trace(T_TUNNEL, "tun-unet: inject decrypted packet");
152 trace_block(T_PACKET, "tun-unet: packet contents", BBASE(b), BLEN(b));
154 write(t->f.fd, BBASE(b), BLEN(b));
157 /* --- @t_destroy@ --- *
159 * Arguments: @tunnel *t@ = pointer to tunnel block
163 * Use: Destroys a tunnel.
166 static void t_destroy(tunnel *t)
173 const tunnel_ops tun_unet = {
184 /*----- That's all, folks -------------------------------------------------*/