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