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