Commit | Line | Data |
---|---|---|
37075862 | 1 | /* -*-c-*- |
2 | * | |
37075862 | 3 | * Tunnel interface based on Linux TUN/TAP driver |
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 | ||
42da2a58 | 33 | #ifdef TUN_LINUX |
34 | # include <sys/ioctl.h> | |
35 | # include <linux/if.h> | |
36 | # include <linux/if_tun.h> | |
37 | #endif | |
37075862 | 38 | |
39 | /*----- Main code ---------------------------------------------------------*/ | |
40 | ||
42da2a58 | 41 | #ifdef TUN_LINUX |
42 | ||
43 | struct tunnel { | |
44 | const tunnel_ops *ops; /* Pointer to operations */ | |
45 | sel_file f; /* Selector for TUN/TAP device */ | |
46 | struct peer *p; /* Pointer to my peer */ | |
e04c2d50 | 47 | }; |
37075862 | 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) { | |
72917fe7 | 68 | a_warn("TUN", "%s", p_ifname(t->p), "read-error", "?ERRNO", A_END); |
37075862 | 69 | return; |
70 | } | |
71 | IF_TRACING(T_TUNNEL, { | |
060ca767 | 72 | trace(T_TUNNEL, "tun-linux: packet arrived"); |
73 | trace_block(T_PACKET, "tun-linux: packet contents", buf_i, n); | |
37075862 | 74 | }) |
75 | buf_init(&b, buf_i, n); | |
76 | p_tun(t->p, &b); | |
77 | } | |
78 | ||
42da2a58 | 79 | /* --- @t_init@ --- * |
37075862 | 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 | ||
42da2a58 | 89 | static void t_init(void) { return; } |
37075862 | 90 | |
42da2a58 | 91 | /* --- @t_create@ --- * |
37075862 | 92 | * |
42da2a58 | 93 | * Arguments: @peer *p@ = pointer to peer block |
72917fe7 | 94 | * @char **ifn@ = where to put the interface name |
37075862 | 95 | * |
42da2a58 | 96 | * Returns: A tunnel block if it worked, or null on failure. |
37075862 | 97 | * |
98 | * Use: Initializes a new tunnel. | |
99 | */ | |
100 | ||
72917fe7 | 101 | static tunnel *t_create(peer *p, char **ifn) |
37075862 | 102 | { |
103 | int fd; | |
104 | int f; | |
105 | struct ifreq iff; | |
42da2a58 | 106 | tunnel *t; |
37075862 | 107 | |
108 | if ((fd = open("/dev/net/tun", O_RDWR)) < 0) { | |
f43df819 MW |
109 | a_warn("TUN", "-", "linux", |
110 | "open-error", "/dev/net/tun", "?ERRNO", | |
111 | A_END); | |
42da2a58 | 112 | return (0); |
37075862 | 113 | } |
114 | fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); | |
060ca767 | 115 | memset(&iff, 0, sizeof(iff)); |
37075862 | 116 | iff.ifr_name[0] = 0; |
b9066fbb | 117 | iff.ifr_flags = IFF_TUN | IFF_NO_PI; |
37075862 | 118 | if ((f = ioctl(fd, TUNSETIFF, &iff)) < 0) { |
f43df819 | 119 | a_warn("TUN", "-", "linux", "config-error", "?ERRNO", A_END); |
37075862 | 120 | close(fd); |
42da2a58 | 121 | return (0); |
37075862 | 122 | } |
42da2a58 | 123 | t = CREATE(tunnel); |
124 | t->ops = &tun_linux; | |
37075862 | 125 | t->p = p; |
126 | sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t); | |
127 | sel_addfile(&t->f); | |
128 | iff.ifr_name[IFNAMSIZ - 1] = 0; | |
72917fe7 | 129 | *ifn = xstrdup(iff.ifr_name); |
060ca767 | 130 | T( trace(T_TUNNEL, "tun-linux: attached interface %s to peer `%s'", |
72917fe7 | 131 | *ifn, p_name(p)); ) |
42da2a58 | 132 | return (t); |
37075862 | 133 | } |
134 | ||
42da2a58 | 135 | /* --- @t_inject@ --- * |
37075862 | 136 | * |
137 | * Arguments: @tunnel *t@ = pointer to tunnel block | |
138 | * @buf *b@ = buffer to send | |
139 | * | |
140 | * Returns: --- | |
141 | * | |
142 | * Use: Injects a packet into the local network stack. | |
143 | */ | |
144 | ||
42da2a58 | 145 | static void t_inject(tunnel *t, buf *b) |
37075862 | 146 | { |
147 | IF_TRACING(T_TUNNEL, { | |
060ca767 | 148 | trace(T_TUNNEL, "tun-linux: inject decrypted packet"); |
37075862 | 149 | trace_block(T_PACKET, "tunnel: packet contents", BBASE(b), BLEN(b)); |
150 | }) | |
151 | write(t->f.fd, BBASE(b), BLEN(b)); | |
152 | } | |
153 | ||
42da2a58 | 154 | /* --- @t_destroy@ --- * |
37075862 | 155 | * |
156 | * Arguments: @tunnel *t@ = pointer to tunnel block | |
157 | * | |
158 | * Returns: --- | |
159 | * | |
160 | * Use: Destroys a tunnel. | |
161 | */ | |
162 | ||
42da2a58 | 163 | static void t_destroy(tunnel *t) |
6047fbac | 164 | { sel_rmfile(&t->f); close(t->f.fd); DESTROY(t); } |
37075862 | 165 | |
42da2a58 | 166 | const tunnel_ops tun_linux = { |
167 | "linux", | |
168 | t_init, | |
169 | t_create, | |
72917fe7 | 170 | 0, |
42da2a58 | 171 | t_inject, |
172 | t_destroy | |
173 | }; | |
174 | ||
175 | #endif | |
176 | ||
37075862 | 177 | /*----- That's all, folks -------------------------------------------------*/ |