37075862 |
1 | /* -*-c-*- |
2 | * |
ef4a1ab7 |
3 | * $Id: tun-linux.c,v 1.2 2003/11/29 23:49:32 mdw Exp $ |
37075862 |
4 | * |
5 | * Tunnel interface based on Linux TUN/TAP driver |
6 | * |
7 | * (c) 2003 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-linux.c,v $ |
ef4a1ab7 |
32 | * Revision 1.2 2003/11/29 23:49:32 mdw |
33 | * Debianization. |
34 | * |
37075862 |
35 | * Revision 1.1 2003/04/06 10:25:17 mdw |
36 | * Support Linux TUN/TAP device. Fix some bugs. |
37 | * |
38 | */ |
39 | |
40 | /*----- Header files ------------------------------------------------------*/ |
41 | |
42 | #include "tripe.h" |
43 | |
44 | #include <sys/ioctl.h> |
45 | |
46 | /*----- Main code ---------------------------------------------------------*/ |
47 | |
48 | #if TUN_TYPE != TUN_LINUX |
49 | # error "Tunnel type mismatch: fix the Makefile" |
50 | #endif |
51 | |
52 | /* --- @t_read@ --- * |
53 | * |
54 | * Arguments: @int fd@ = file descriptor to read |
55 | * @unsigned mode@ = what's happened |
56 | * @void *v@ = pointer to tunnel block |
57 | * |
58 | * Returns: --- |
59 | * |
60 | * Use: Reads data from the tunnel. |
61 | */ |
62 | |
63 | static void t_read(int fd, unsigned mode, void *v) |
64 | { |
65 | tunnel *t = v; |
66 | ssize_t n; |
67 | buf b; |
68 | |
69 | n = read(fd, buf_i, sizeof(buf_i)); |
70 | if (n < 0) { |
71 | a_warn("tunnel read failed (%s): %s", t->ifn, strerror(errno)); |
72 | return; |
73 | } |
74 | IF_TRACING(T_TUNNEL, { |
75 | trace(T_TUNNEL, "tunnel: packet arrived"); |
76 | trace_block(T_PACKET, "tunnel: packet contents", buf_i, n); |
77 | }) |
78 | buf_init(&b, buf_i, n); |
79 | p_tun(t->p, &b); |
80 | } |
81 | |
82 | /* --- @tun_init@ --- * |
83 | * |
84 | * Arguments: --- |
85 | * |
86 | * Returns: --- |
87 | * |
88 | * Use: Initializes the tunneling system. Maybe this will require |
89 | * opening file descriptors or something. |
90 | */ |
91 | |
92 | void tun_init(void) |
93 | { |
94 | return; |
95 | } |
96 | |
97 | /* --- @tun_create@ --- * |
98 | * |
99 | * Arguments: @tunnel *t@ = pointer to tunnel block |
100 | * @peer *p@ = pointer to peer block |
101 | * |
102 | * Returns: Zero if it worked, nonzero on failure. |
103 | * |
104 | * Use: Initializes a new tunnel. |
105 | */ |
106 | |
107 | int tun_create(tunnel *t, peer *p) |
108 | { |
109 | int fd; |
110 | int f; |
111 | struct ifreq iff; |
112 | |
113 | if ((fd = open("/dev/net/tun", O_RDWR)) < 0) { |
114 | a_warn("open `/dev/net/tun' failed: %s", strerror(errno)); |
115 | return (-1); |
116 | } |
117 | fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); |
118 | iff.ifr_name[0] = 0; |
ef4a1ab7 |
119 | iff.ifr_flags = IFF_TUN; |
37075862 |
120 | if ((f = ioctl(fd, TUNSETIFF, &iff)) < 0) { |
121 | a_warn("couldn't set configure new TUN/TAP interface: %s", |
122 | strerror(errno)); |
123 | close(fd); |
124 | return (-1); |
125 | } |
126 | t->p = p; |
127 | sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t); |
128 | sel_addfile(&t->f); |
129 | iff.ifr_name[IFNAMSIZ - 1] = 0; |
130 | strcpy(t->ifn, iff.ifr_name); |
131 | T( trace(T_TUNNEL, "tunnel: attached interface %s to peer `%s'", |
132 | t->ifn, p_name(p)); ) |
133 | return (0); |
134 | } |
135 | |
136 | /* --- @tun_ifname@ --- * |
137 | * |
138 | * Arguments: @tunnel *t@ = pointer to tunnel block |
139 | * |
140 | * Returns: A pointer to the tunnel's interface name. |
141 | */ |
142 | |
143 | const char *tun_ifname(tunnel *t) |
144 | { |
145 | return (t->ifn); |
146 | } |
147 | |
148 | /* --- @tun_inject@ --- * |
149 | * |
150 | * Arguments: @tunnel *t@ = pointer to tunnel block |
151 | * @buf *b@ = buffer to send |
152 | * |
153 | * Returns: --- |
154 | * |
155 | * Use: Injects a packet into the local network stack. |
156 | */ |
157 | |
158 | void tun_inject(tunnel *t, buf *b) |
159 | { |
160 | IF_TRACING(T_TUNNEL, { |
161 | trace(T_TUNNEL, "tunnel: inject decrypted packet"); |
162 | trace_block(T_PACKET, "tunnel: packet contents", BBASE(b), BLEN(b)); |
163 | }) |
164 | write(t->f.fd, BBASE(b), BLEN(b)); |
165 | } |
166 | |
167 | /* --- @tun_destroy@ --- * |
168 | * |
169 | * Arguments: @tunnel *t@ = pointer to tunnel block |
170 | * |
171 | * Returns: --- |
172 | * |
173 | * Use: Destroys a tunnel. |
174 | */ |
175 | |
176 | void tun_destroy(tunnel *t) |
177 | { |
178 | sel_rmfile(&t->f); |
179 | close(t->f.fd); |
180 | } |
181 | |
182 | /*----- That's all, folks -------------------------------------------------*/ |