3 * Tunnel interface for Linux-tun-shaped arrangements
5 * (c) 2003 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
32 /*----- Main code ---------------------------------------------------------*/
34 #if defined(TUN_LINUX) || defined(TUN_BSD) || defined(TUN_UNET)
37 const tunnel_ops *ops; /* Pointer to operations */
38 sel_file f; /* Selector for TUN/TAP device */
39 struct peer *p; /* Pointer to my peer */
44 * Arguments: @int fd@ = file descriptor to read
45 * @unsigned mode@ = what's happened
46 * @void *v@ = pointer to tunnel block
50 * Use: Reads data from the tunnel.
53 static void t_read(int fd, unsigned mode, void *v)
59 n = read(fd, buf_i, sizeof(buf_i));
61 a_warn("TUN", "%s", p_ifname(t->p), "%s", t->ops->name,
62 "read-error", "?ERRNO", A_END);
65 IF_TRACING(T_TUNNEL, {
66 trace(T_TUNNEL, "tun-%s: packet arrived", t->ops->name);
67 trace_block(T_PACKET, "tunnel: packet contents", buf_i, n);
69 buf_init(&b, buf_i, n);
79 * Use: Initializes the tunneling system. Maybe this will require
80 * opening file descriptors or something.
83 static void t_init(void) { return; }
85 /* --- @t_create@ --- *
87 * Arguments: @peer *p@ = pointer to peer block
88 * @int fd@ = file descriptor of tunnel device
89 * @char **ifn@ = where to put the interface name
91 * Returns: A tunnel block if it worked, or null on failure.
93 * Use: Initializes a new tunnel.
96 static tunnel *t_create(peer *p, int fd, char **ifn, const tunnel_ops *ops)
100 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
104 sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
109 /* --- @t_inject@ --- *
111 * Arguments: @tunnel *t@ = pointer to tunnel block
112 * @buf *b@ = buffer to send
116 * Use: Injects a packet into the local network stack.
119 static void t_inject(tunnel *t, buf *b)
121 IF_TRACING(T_TUNNEL, {
122 trace(T_TUNNEL, "tun-%s: inject decrypted packet", t->ops->name);
123 trace_block(T_PACKET, "tunnel: packet contents", BBASE(b), BLEN(b));
125 DISCARD(write(t->f.fd, BBASE(b), BLEN(b)));
128 /* --- @t_destroy@ --- *
130 * Arguments: @tunnel *t@ = pointer to tunnel block
134 * Use: Destroys a tunnel.
137 static void t_destroy(tunnel *t)
138 { sel_rmfile(&t->f); close(t->f.fd); DESTROY(t); }
140 #define DEFOPS(name) \
142 static tunnel *t_create_##name(peer *p, int fd, char **ifn); \
144 const tunnel_ops tun_##name = { \
145 #name, TUNF_PRIVOPEN, \
146 t_init, t_create_##name, 0, t_inject, t_destroy \
149 static tunnel *t_create_##name(peer *p, int fd, char **ifn) \
150 { return t_create(p, fd, ifn, &tun_##name); }
166 /*----- That's all, folks -------------------------------------------------*/