Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 900e4d633a
Fetching contributors…

Cannot retrieve contributors at this time

file 206 lines (170 sloc) 5.955 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
%% Copyright (c) 2012, 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(sut).
-include("sut.hrl").
-behaviour(gen_server).

-export([
        destroy/1
    ]).

-export([start/1, start_link/1]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
        terminate/2, code_change/3]).

-record(state, {
    ifname = <<"sut-ipv6">>,
    serverv4,
    clientv4,
    clientv6,

    filter_out = fun(_Packet, _State) -> ok end,
    filter_in = fun(_Packet, _State) -> ok end,

    error_out = fun(ok) -> ok; (Error) -> Error end,
    error_in = fun(ok) -> ok; (Error) -> Error end,

    s,
    fd,
    dev
    }).

%%--------------------------------------------------------------------
%%% Exports
%%--------------------------------------------------------------------
destroy(Ref) when is_pid(Ref) ->
    gen_server:call(Ref, destroy).

start(Opt) when is_list(Opt) ->
    gen_server:start(?MODULE, [options(Opt)], []).

start_link(Opt) when is_list(Opt) ->
    gen_server:start_link(?MODULE, [options(Opt)], []).


%%--------------------------------------------------------------------
%%% Callbacks
%%--------------------------------------------------------------------
init([#state{serverv4 = Server,
            clientv4 = Client4,
            clientv6 = Client6,
            ifname = Ifname} = State]) ->
    process_flag(trap_exit, true),

    {ok, FD} = procket:open(0, [
                {protocol, ?IPPROTO_IPV6},
                {type, raw},
                {family, inet}
                ]),

    {ok, Socket} = gen_udp:open(0, [
                binary,
                {fd, FD}
                ]),

    {ok, Dev} = tuncer:create(Ifname, [
                tun,
                no_pi,
                {active, true}
                ]),

    ok = tuncer:up(Dev, Client6),

    {ok, State#state{
            fd = FD,
            s = Socket,
            dev = Dev,
            serverv4 = aton(Server),
            clientv4 = aton(Client4),
            clientv6 = aton(Client6)
            }}.



handle_call(destroy, _From, State) ->
    {stop, normal, ok, State}.

handle_cast(_Msg, State) ->
    {noreply, State}.

%%
%% IPv6 encapsuplated packet read from socket
%%
handle_info({udp, Socket, {SA1,SA2,SA3,SA4}, 0,
         <<4:4, HL:4, _ToS:8, _Len:16, _Id:16, 0:1, _DF:1, _MF:1,
           _Off:13, _TTL:8, ?IPPROTO_IPV6:8, _Sum:16,
           SA1:8, SA2:8, SA3:8, SA4:8,
           DA1:8, DA2:8, DA3:8, DA4:8,
           Data/binary>>}, #state{
                s = Socket,
                clientv4 = {DA1,DA2,DA3,DA4},
                serverv4 = {SA1,SA2,SA3,SA4},
                dev = Dev
                } = State) ->

            Opt = case (HL-5)*4 of
                N when N > 0 -> N;
                _ -> 0
            end,
            <<_:Opt/bits, Payload/bits>> = Data,

            spawn(sut_fw, in, [Dev, Payload, copy(State)]),
            {noreply, State};

% Invalid packet
handle_info({udp, _Socket, Src, 0, Pkt}, State) ->
            error_logger:info_report([
                    {error, invalid_packet},
                    {source_address, Src},
                    {packet, Pkt}
                    ]),
            {noreply, State};

% Data from the tun device
handle_info({tuntap, Dev, Data}, #state{
                dev = Dev,
                s = Socket
                } = State) ->
    spawn(sut_fw, out, [Socket, Data, copy(State)]),
    {noreply, State};

% WTF?
handle_info(Info, State) ->
    error_logger:error_report([wtf, Info]),
    {noreply, State}.

terminate(_Reason, #state{fd = FD}) ->
    procket:close(FD),
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.


%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
aton(Address) when is_list(Address) ->
    {ok, N} = inet_parse:address(Address),
    N;
aton(Address) when is_tuple(Address) ->
    Address.

options(Opt) ->
    Fun = ?PROPLIST_TO_RECORD(state),
    Fun(Opt).

copy(#state{
        serverv4 = Serverv4,
        clientv4 = Clientv4,
        clientv6 = Clientv6,

        filter_out = Fout,
        filter_in = Fin,

        error_out = Eout,
        error_in = Ein
    }) ->
    #sut_state{
        serverv4 = Serverv4,
        clientv4 = Clientv4,
        clientv6 = Clientv6,

        filter_out = Fout,
        filter_in = Fin,

        error_out = Eout,
        error_in = Ein
        }.
Something went wrong with that request. Please try again.