410c8acf |
1 | /* -*-c-*- |
2 | * |
650a6624 |
3 | * $Id: tun-unet.c,v 1.2 2001/02/04 17:10:58 mdw Exp $ |
410c8acf |
4 | * |
5 | * Tunnel interface based on Linux Usernet |
6 | * |
7 | * (c) 2001 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-unet.c,v $ |
650a6624 |
32 | * Revision 1.2 2001/02/04 17:10:58 mdw |
33 | * Make file descriptors be nonblocking and close-on-exec. |
34 | * |
410c8acf |
35 | * Revision 1.1 2001/02/03 20:26:37 mdw |
36 | * Initial checkin. |
37 | * |
38 | */ |
39 | |
40 | /*----- Header files ------------------------------------------------------*/ |
41 | |
42 | #include "tripe.h" |
43 | |
44 | #include <sys/ioctl.h> |
45 | #include <unet.h> |
46 | |
47 | /*----- Main code ---------------------------------------------------------*/ |
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 | 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) { |
68 | a_warn("tunnel read failed (%s): %s", tun_ifname(t), strerror(errno)); |
69 | return; |
70 | } |
71 | IF_TRACING(T_TUNNEL, { |
72 | trace(T_TUNNEL, "tunnel: packet arrived"); |
73 | trace_block(T_PACKET, "tunnel: packet contents", buf_i, n); |
74 | }) |
75 | buf_init(&b, buf_i, n); |
76 | p_tun(t->p, &b); |
77 | } |
78 | |
79 | /* --- @tun_init@ --- * |
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 | |
89 | void tun_init(void) |
90 | { |
91 | return; |
92 | } |
93 | |
94 | /* --- @tun_create@ --- * |
95 | * |
96 | * Arguments: @tunnel *t@ = pointer to tunnel block |
97 | * @peer *p@ = pointer to peer block |
98 | * |
99 | * Returns: Zero if it worked, nonzero on failure. |
100 | * |
101 | * Use: Initializes a new tunnel. |
102 | */ |
103 | |
104 | int tun_create(tunnel *t, peer *p) |
105 | { |
106 | int fd; |
107 | |
108 | if ((fd = open("/dev/unet", O_RDWR)) < 0) { |
109 | a_warn("open `/dev/unet' failed: %s", strerror(errno)); |
110 | return (-1); |
111 | } |
650a6624 |
112 | fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); |
410c8acf |
113 | t->p = p; |
114 | sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t); |
115 | sel_addfile(&t->f); |
116 | T( trace(T_TUNNEL, "tunnel: attached interface %s to peer `%s'", |
117 | tun_ifname(t), p_name(p)); ) |
118 | return (0); |
119 | } |
120 | |
121 | /* --- @tun_ifname@ --- * |
122 | * |
123 | * Arguments: @tunnel *t@ = pointer to tunnel block |
124 | * |
125 | * Returns: A pointer to the tunnel's interface name. |
126 | */ |
127 | |
128 | const char *tun_ifname(tunnel *t) |
129 | { |
130 | static char b[UNET_NAMEMAX]; |
131 | struct unet_info uni; |
132 | if (ioctl(t->f.fd, UNIOCGINFO, &uni)) { |
133 | a_warn("ioctl(UNIOCGINFO) failed: %s", strerror(errno)); |
134 | return ("<error>"); |
135 | } |
136 | if (strlen(uni.uni_ifname) + 1 > sizeof(b)) { |
137 | a_warn("interface name too long!"); |
138 | return ("<error>"); |
139 | } |
140 | strcpy(b, uni.uni_ifname); |
141 | return (b); |
142 | } |
143 | |
144 | /* --- @tun_inject@ --- * |
145 | * |
146 | * Arguments: @tunnel *t@ = pointer to tunnel block |
147 | * @buf *b@ = buffer to send |
148 | * |
149 | * Returns: --- |
150 | * |
151 | * Use: Injects a packet into the local network stack. |
152 | */ |
153 | |
154 | void tun_inject(tunnel *t, buf *b) |
155 | { |
156 | IF_TRACING(T_TUNNEL, { |
157 | trace(T_TUNNEL, "tunnel: inject decrypted packet"); |
158 | trace_block(T_PACKET, "tunnel: packet contents", BBASE(b), BLEN(b)); |
159 | }) |
160 | write(t->f.fd, BBASE(b), BLEN(b)); |
161 | } |
162 | |
163 | /* --- @tun_destroy@ --- * |
164 | * |
165 | * Arguments: @tunnel *t@ = pointer to tunnel block |
166 | * |
167 | * Returns: --- |
168 | * |
169 | * Use: Destroys a tunnel. |
170 | */ |
171 | |
172 | void tun_destroy(tunnel *t) |
173 | { |
174 | sel_rmfile(&t->f); |
175 | close(t->f.fd); |
176 | } |
177 | |
178 | /*----- That's all, folks -------------------------------------------------*/ |