Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support tuntap interfaces

  • Loading branch information...
commit 0cd4e788f5f209deb38e9583689a1e8d6a50c385 1 parent bf791cd
@msantos authored
View
35 include/ioctl.hrl
@@ -0,0 +1,35 @@
+%% Copyright (c) 2011, Michael Santos <michael.santos@gmail.com>
+%% All rights reserved.
+%%
+%% Redistribution and use in source and binary forms, with or without
+%% modification, are permitted provided that the following conditions
+%% are met:
+%%
+%% Redistributions of source code must retain the above copyright
+%% notice, this list of conditions and the following disclaimer.
+%%
+%% Redistributions in binary form must reproduce the above copyright
+%% notice, this list of conditions and the following disclaimer in the
+%% documentation and/or other materials provided with the distribution.
+%%
+%% Neither the name of the author nor the names of its contributors
+%% may be used to endorse or promote products derived from this software
+%% without specific prior written permission.
+%%
+%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+%% POSSIBILITY OF SUCH DAMAGE.
+
+-define(IO(Type,NR), procket_ioctl:io(Type,NR)).
+-define(IOR(Type,NR,Size), procket_ioctl:ior(Type,NR,Size)).
+-define(IOW(Type,NR,Size), procket_ioctl:iow(Type,NR,Size)).
+-define(IOWR(Type,NR,Size), procket_ioctl:iowr(Type,NR,Size)).
View
56 include/tuntap.hrl
@@ -0,0 +1,56 @@
+%% Copyright (c) 2011, Michael Santos <michael.santos@gmail.com>
+%% All rights reserved.
+%%
+%% Redistribution and use in source and binary forms, with or without
+%% modification, are permitted provided that the following conditions
+%% are met:
+%%
+%% Redistributions of source code must retain the above copyright
+%% notice, this list of conditions and the following disclaimer.
+%%
+%% Redistributions in binary form must reproduce the above copyright
+%% notice, this list of conditions and the following disclaimer in the
+%% documentation and/or other materials provided with the distribution.
+%%
+%% Neither the name of the author nor the names of its contributors
+%% may be used to endorse or promote products derived from this software
+%% without specific prior written permission.
+%%
+%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+%% POSSIBILITY OF SUCH DAMAGE.
+
+-define(SIZEOF_INT, 4).
+-define(SIZEOF_UNSIGNED_INT, ?SIZEOF_INT).
+
+% Ioctl defines
+-define(TUNSETNOCSUM, ?IOW($T, 200, ?SIZEOF_INT)).
+-define(TUNSETDEBUG, ?IOW($T, 201, ?SIZEOF_INT)).
+-define(TUNSETIFF, ?IOW($T, 202, ?SIZEOF_INT)).
+-define(TUNSETPERSIST, ?IOW($T, 203, ?SIZEOF_INT)).
+-define(TUNSETOWNER, ?IOW($T, 204, ?SIZEOF_INT)).
+-define(TUNSETLINK, ?IOW($T, 205, ?SIZEOF_INT)).
+-define(TUNSETGROUP, ?IOW($T, 206, ?SIZEOF_INT)).
+-define(TUNGETFEATURES, ?IOR($T, 207, ?SIZEOF_UNSIGNED_INT)).
+-define(TUNSETOFFLOAD, ?IOW($T, 208, ?SIZEOF_UNSIGNED_INT)).
+-define(TUNSETTXFILTER, ?IOW($T, 209, ?SIZEOF_UNSIGNED_INT)).
+-define(TUNGETIFF, ?IOR($T, 210, ?SIZEOF_UNSIGNED_INT)).
+-define(TUNGETSNDBUF, ?IOR($T, 211, ?SIZEOF_INT)).
+-define(TUNSETSNDBUF, ?IOW($T, 212, ?SIZEOF_INT)).
+
+% TUNSETIFF ifr flags
+-define(IFF_TUN, 16#0001).
+-define(IFF_TAP, 16#0002).
+-define(IFF_NO_PI, 16#1000).
+-define(IFF_ONE_QUEUE, 16#2000).
+-define(IFF_VNET_HDR, 16#4000).
+-define(IFF_TUN_EXCL, 16#8000).
View
93 src/procket_ioctl.erl
@@ -0,0 +1,93 @@
+%% Copyright (c) 2011, Michael Santos <michael.santos@gmail.com>
+%% All rights reserved.
+%%
+%% Redistribution and use in source and binary forms, with or without
+%% modification, are permitted provided that the following conditions
+%% are met:
+%%
+%% Redistributions of source code must retain the above copyright
+%% notice, this list of conditions and the following disclaimer.
+%%
+%% Redistributions in binary form must reproduce the above copyright
+%% notice, this list of conditions and the following disclaimer in the
+%% documentation and/or other materials provided with the distribution.
+%%
+%% Neither the name of the author nor the names of its contributors
+%% may be used to endorse or promote products derived from this software
+%% without specific prior written permission.
+%%
+%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+%% POSSIBILITY OF SUCH DAMAGE.
+-module(procket_ioctl).
+
+-export([
+ in/0, out/0, void/0,
+ in/1, out/1, void/1,
+ ioc/4,
+ io/2, ior/3,
+ iow/3, iowr/3
+ ]).
+
+
+%% BSD: <<IN:1, OUT:1, VOID:1, Length:13, Group:8, Command:8>>
+%%
+%% IOC_OUT = 0x40000000
+%% IOC_IN = 0x80000000
+%%
+%% #define _IOR(g,n,t) _IOC(IOC_OUT, (g), (n), sizeof(t))
+%% #define _IOW(g,n,t) _IOC(IOC_IN, (g), (n), sizeof(t))
+
+%% Linux: <<OUT:1, IN:1, Length:14, Group:8, Command:8>>
+%%
+%% IOC_OUT = 0x80000000
+%% IOC_IN = 0x40000000
+%%
+%% #define _IOR(g,n,t) _IOC(_IOC_READ,(g),(n),(t))
+%% #define _IOW(g,n,t) _IOC(_IOC_WRITE,(g),(n),(t))
+
+in() -> in(os()).
+in(bsd) -> 16#80000000;
+in(linux) -> 16#40000000.
+
+out() -> out(os()).
+out(bsd) -> 16#40000000;
+out(linux) -> 16#80000000.
+
+void() -> void(os()).
+void(bsd) -> 16#20000000;
+void(linux) -> 0.
+
+ioc(Inout,Group,Num,Len) ->
+ (Len bsl 16) bor Inout bor (Group bsl 8) bor Num.
+
+io(Type,NR) ->
+ ioc(void(), Type, NR, 0).
+
+ior(Type,NR,Size) ->
+ ioc(out(), Type, NR, Size).
+
+iow(Type,NR,Size) ->
+ ioc(in(), Type, NR, Size).
+
+iowr(Type,NR,Size) ->
+ ioc(in() bor out(), Type, NR, Size).
+
+
+os() ->
+ case os:type() of
+ {unix, linux} -> linux;
+ {unix,BSD} when BSD == darwin;
+ BSD == openbsd;
+ BSD == netbsd;
+ BSD == freebsd -> bsd
+ end.
View
93 src/tuntap.erl
@@ -0,0 +1,93 @@
+%% Copyright (c) 2011, Michael Santos <michael.santos@gmail.com>
+%% All rights reserved.
+%%
+%% Redistribution and use in source and binary forms, with or without
+%% modification, are permitted provided that the following conditions
+%% are met:
+%%
+%% Redistributions of source code must retain the above copyright
+%% notice, this list of conditions and the following disclaimer.
+%%
+%% Redistributions in binary form must reproduce the above copyright
+%% notice, this list of conditions and the following disclaimer in the
+%% documentation and/or other materials provided with the distribution.
+%%
+%% Neither the name of the author nor the names of its contributors
+%% may be used to endorse or promote products derived from this software
+%% without specific prior written permission.
+%%
+%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+%% POSSIBILITY OF SUCH DAMAGE.
+-module(tuntap).
+
+-include("ioctl.hrl").
+-include("tuntap.hrl").
+
+-export([
+ create/0, create/1, create/2,
+ persist/2, destroy/1,
+ owner/2, group/2
+ ]).
+
+
+create() ->
+ create(<<0:(15*8)>>, [tap, no_pi]).
+create(Ifname) ->
+ create(Ifname, [tap, no_pi]).
+
+create(Ifname, Opt) when is_list(Ifname) ->
+ create(list_to_binary(Ifname), Opt);
+create(Ifname, Opt) when byte_size(Ifname) < 16, is_list(Opt) ->
+ Flags = lists:foldl(fun(N, F) -> F bor flag(N) end, 0, Opt),
+
+ {ok, FD} = procket:dev("net/tun"),
+ {ok, Dev} = procket:ioctl(FD, ?TUNSETIFF,
+ <<Ifname/binary, 0:((15*8) - (byte_size(Ifname)*8)), 0:8, % ifrn_name[IFNAMSIZ]: interface name
+ Flags:2/native-signed-integer-unit:8, % ifru_flags
+ 0:(14*8)>>),
+ {ok, FD, hd(binary:split(Dev, <<0>>))}.
+
+persist(FD, Bool) when is_integer(FD) ->
+ ioctl(FD, ?TUNSETPERSIST, bool(Bool)).
+
+destroy(FD) when is_integer(FD) ->
+ persist(FD, false).
+
+owner(FD, Owner) when is_integer(FD), is_integer(Owner) ->
+ ioctl(FD, ?TUNSETOWNER, int_to_bin(Owner)).
+
+group(FD, Group) when is_integer(FD), is_integer(Group) ->
+ ioctl(FD, ?TUNSETGROUP, int_to_bin(Group)).
+
+
+%%-------------------------------------------------------------------------
+%%% Internal functions
+%%-------------------------------------------------------------------------
+ioctl(FD, Request, Opt) ->
+ case procket:ioctl(FD, Request, Opt) of
+ {ok, _} -> ok;
+ Error -> Error
+ end.
+
+flag(tun) -> ?IFF_TUN;
+flag(tap) -> ?IFF_TAP;
+flag(no_pi) -> ?IFF_NO_PI;
+flag(one_queue) -> ?IFF_ONE_QUEUE;
+flag(vnet_hdr) -> ?IFF_VNET_HDR;
+flag(tun_excl) -> ?IFF_TUN_EXCL.
+
+int_to_bin(Int) ->
+ <<Int:4/native-integer-unsigned-unit:8>>.
+
+bool(true) -> <<1:4/native-integer-unsigned-unit:8>>;
+bool(false) -> <<0:4/native-integer-unsigned-unit:8>>.
Please sign in to comment.
Something went wrong with that request. Please try again.