Commit | Line | Data |
---|---|---|
37075862 | 1 | /* -*-c-*- |
2 | * | |
cd1f3310 | 3 | * Tunnel interface for Linux-tun-shaped arrangements |
37075862 | 4 | * |
5 | * (c) 2003 Straylight/Edgeware | |
6 | */ | |
7 | ||
e04c2d50 | 8 | /*----- Licensing notice --------------------------------------------------* |
37075862 | 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. | |
e04c2d50 | 16 | * |
37075862 | 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. | |
e04c2d50 | 21 | * |
37075862 | 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 | ||
37075862 | 27 | /*----- Header files ------------------------------------------------------*/ |
28 | ||
42da2a58 | 29 | #define TUN_INTERNALS |
30 | ||
37075862 | 31 | #include "tripe.h" |
32 | ||
37075862 | 33 | /*----- Main code ---------------------------------------------------------*/ |
34 | ||
cd1f3310 | 35 | #if defined(TUN_LINUX) || defined(TUN_BSD) || defined(TUN_UNET) |
42da2a58 | 36 | |
37 | struct tunnel { | |
38 | const tunnel_ops *ops; /* Pointer to operations */ | |
39 | sel_file f; /* Selector for TUN/TAP device */ | |
40 | struct peer *p; /* Pointer to my peer */ | |
e04c2d50 | 41 | }; |
37075862 | 42 | |
43 | /* --- @t_read@ --- * | |
44 | * | |
45 | * Arguments: @int fd@ = file descriptor to read | |
46 | * @unsigned mode@ = what's happened | |
47 | * @void *v@ = pointer to tunnel block | |
48 | * | |
49 | * Returns: --- | |
50 | * | |
51 | * Use: Reads data from the tunnel. | |
52 | */ | |
53 | ||
54 | static void t_read(int fd, unsigned mode, void *v) | |
55 | { | |
56 | tunnel *t = v; | |
57 | ssize_t n; | |
58 | buf b; | |
59 | ||
60 | n = read(fd, buf_i, sizeof(buf_i)); | |
61 | if (n < 0) { | |
cd1f3310 | 62 | a_warn("TUN", "%s", p_ifname(t->p), "%s", t->ops->name, |
b476642f | 63 | "read-error", "?ERRNO", A_END); |
37075862 | 64 | return; |
65 | } | |
66 | IF_TRACING(T_TUNNEL, { | |
cd1f3310 MW |
67 | trace(T_TUNNEL, "tun-%s: packet arrived", t->ops->name); |
68 | trace_block(T_PACKET, "tunnel: packet contents", buf_i, n); | |
37075862 | 69 | }) |
70 | buf_init(&b, buf_i, n); | |
71 | p_tun(t->p, &b); | |
72 | } | |
73 | ||
42da2a58 | 74 | /* --- @t_init@ --- * |
37075862 | 75 | * |
76 | * Arguments: --- | |
77 | * | |
78 | * Returns: --- | |
79 | * | |
80 | * Use: Initializes the tunneling system. Maybe this will require | |
81 | * opening file descriptors or something. | |
82 | */ | |
83 | ||
42da2a58 | 84 | static void t_init(void) { return; } |
37075862 | 85 | |
eb5f3fea MW |
86 | /* --- @t_create@ --- * |
87 | * | |
88 | * Arguments: @peer *p@ = pointer to peer block | |
89 | * @int fd@ = file descriptor of tunnel device | |
90 | * @char **ifn@ = where to put the interface name | |
91 | * | |
92 | * Returns: A tunnel block if it worked, or null on failure. | |
93 | * | |
94 | * Use: Initializes a new tunnel. | |
95 | */ | |
96 | ||
cd1f3310 | 97 | static tunnel *t_create(peer *p, int fd, char **ifn, const tunnel_ops *ops) |
eb5f3fea MW |
98 | { |
99 | tunnel *t; | |
100 | ||
101 | fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); | |
42da2a58 | 102 | t = CREATE(tunnel); |
cd1f3310 | 103 | t->ops = ops; |
37075862 | 104 | t->p = p; |
105 | sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t); | |
106 | sel_addfile(&t->f); | |
42da2a58 | 107 | return (t); |
37075862 | 108 | } |
109 | ||
42da2a58 | 110 | /* --- @t_inject@ --- * |
37075862 | 111 | * |
112 | * Arguments: @tunnel *t@ = pointer to tunnel block | |
113 | * @buf *b@ = buffer to send | |
114 | * | |
115 | * Returns: --- | |
116 | * | |
117 | * Use: Injects a packet into the local network stack. | |
118 | */ | |
119 | ||
42da2a58 | 120 | static void t_inject(tunnel *t, buf *b) |
37075862 | 121 | { |
122 | IF_TRACING(T_TUNNEL, { | |
cd1f3310 | 123 | trace(T_TUNNEL, "tun-%s: inject decrypted packet", t->ops->name); |
37075862 | 124 | trace_block(T_PACKET, "tunnel: packet contents", BBASE(b), BLEN(b)); |
125 | }) | |
99735357 | 126 | DISCARD(write(t->f.fd, BBASE(b), BLEN(b))); |
37075862 | 127 | } |
128 | ||
42da2a58 | 129 | /* --- @t_destroy@ --- * |
37075862 | 130 | * |
131 | * Arguments: @tunnel *t@ = pointer to tunnel block | |
132 | * | |
133 | * Returns: --- | |
134 | * | |
135 | * Use: Destroys a tunnel. | |
136 | */ | |
137 | ||
42da2a58 | 138 | static void t_destroy(tunnel *t) |
6047fbac | 139 | { sel_rmfile(&t->f); close(t->f.fd); DESTROY(t); } |
37075862 | 140 | |
cd1f3310 MW |
141 | #define DEFOPS(name) \ |
142 | \ | |
143 | static tunnel *t_create_##name(peer *p, int fd, char **ifn); \ | |
144 | \ | |
145 | const tunnel_ops tun_##name = { \ | |
146 | #name, TUNF_PRIVOPEN, \ | |
147 | t_init, t_create_##name, 0, t_inject, t_destroy \ | |
148 | }; \ | |
149 | \ | |
150 | static tunnel *t_create_##name(peer *p, int fd, char **ifn) \ | |
151 | { return t_create(p, fd, ifn, &tun_##name); } | |
152 | ||
153 | #ifdef TUN_LINUX | |
154 | DEFOPS(linux) | |
155 | #endif | |
156 | ||
157 | #ifdef TUN_BSD | |
158 | DEFOPS(bsd) | |
159 | #endif | |
160 | ||
161 | #ifdef TUN_UNET | |
162 | DEFOPS(unet) | |
163 | #endif | |
42da2a58 | 164 | |
165 | #endif | |
166 | ||
37075862 | 167 | /*----- That's all, folks -------------------------------------------------*/ |