200b5d9325a65fecf3c9d1a831b533ee10079bb2
3 * Tunnel interface for 4.4BSD-derived systems
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
33 /*----- Main code ---------------------------------------------------------*/
38 const tunnel_ops
*ops
; /* Pointer to operations */
39 sel_file f
; /* Selector for tunnel device */
40 struct peer
*p
; /* Pointer to my peer */
45 * Arguments: @int fd@ = file descriptor to read
46 * @unsigned mode@ = what's happened
47 * @void *v@ = pointer to tunnel block
51 * Use: Reads data from the tunnel.
54 static void t_read(int fd
, unsigned mode
, void *v
)
60 n
= read(fd
, buf_i
, sizeof(buf_i
));
62 a_warn("TUN", "%s", p_ifname(t
->p
), "read-error", "?ERRNO", A_END
);
65 IF_TRACING(T_TUNNEL
, {
66 trace(T_TUNNEL
, "tun-bsd: packet arrived");
67 trace_block(T_PACKET
, "tun-bsd: packet contents", buf_i
, n
);
69 buf_init(&b
, buf_i
, n
);
79 * Use: Initializes the tunneling system. Maybe this will require
80 * opening file descriptors or something.
83 static void t_init(void) { return; }
87 * Arguments: @char **ifn@ = where to put the interface name
89 * Returns: A file descriptor, or @-1@ on failure.
91 * Use: Opens a tunnel device. This will run with root privileges
92 * even if the rest of the server has dropped them.
95 static int t_open(char **ifn
)
103 sprintf(buf
, "/dev/tun%u", n
);
104 if ((fd
= open(buf
, O_RDWR
)) >= 0)
108 T( trace(T_TUNNEL
, "tunnel device %u busy: skipping", n
); )
111 a_warn("TUN", "-", "bsd", "no-tunnel-devices", A_END
);
114 a_warn("TUN", "-", "open-error", "%s", buf
, "?ERRNO", A_END
);
122 /* --- @t_create@ --- *
124 * Arguments: @peer *p@ = pointer to peer block
125 * @int fd@ = file descriptor of tunnel device
126 * @char **ifn@ = where to put the interface name
128 * Returns: A tunnel block if it worked, or null on failure.
130 * Use: Initializes a new tunnel.
133 static tunnel
*t_create(peer
*p
, int fd
, char **ifn
)
137 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
141 sel_initfile(&sel
, &t
->f
, fd
, SEL_READ
, t_read
, t
);
146 /* --- @t_inject@ --- *
148 * Arguments: @tunnel *t@ = pointer to tunnel block
149 * @buf *b@ = buffer to send
153 * Use: Injects a packet into the local network stack.
156 static void t_inject(tunnel
*t
, buf
*b
)
158 IF_TRACING(T_TUNNEL
, {
159 trace(T_TUNNEL
, "tun-bsd: inject decrypted packet");
160 trace_block(T_PACKET
, "tun-bsd: packet contents", BBASE(b
), BLEN(b
));
162 write(t
->f
.fd
, BBASE(b
), BLEN(b
));
165 /* --- @t_destroy@ --- *
167 * Arguments: @tunnel *t@ = pointer to tunnel block
171 * Use: Destroys a tunnel.
174 static void t_destroy(tunnel
*t
)
175 { sel_rmfile(&t
->f
); close(t
->f
.fd
); DESTROY(t
); }
177 const tunnel_ops tun_bsd
= {
189 /*----- That's all, folks -------------------------------------------------*/