Skip to content

Commit

Permalink
decode interface type if message-type 'newlink' or 'dellink'
Browse files Browse the repository at this point in the history
  • Loading branch information
Helge Sychla committed Jan 4, 2011
1 parent 660bce1 commit 28e7d13
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 12 deletions.
69 changes: 69 additions & 0 deletions include/netlink.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,72 @@
-define(NETLINK_GENERIC, 16).
-define(NETLINK_SCSITRANSPORT, 18).
-define(NETLINK_ECRYPTFS, 19).

%% ifi_type
%% taken from /usr/include/linux/if_arp.h
%% ARP protocol HARDWARE identifiers.
-define(ARPHRD_NETROM, 0). %% from KA9Q: NET/ROM pseudo
-define(ARPHRD_ETHER , 1). %% Ethernet 10Mbps
-define(ARPHRD_EETHER, 2). %% Experimental Ethernet
-define(ARPHRD_AX25, 3). %% AX.25 Level 2
-define(ARPHRD_PRONET, 4). %% PROnet token ring
-define(ARPHRD_CHAOS, 5). %% Chaosnet
-define(ARPHRD_IEEE802,6). %% IEEE 802.2 Ethernet/TR/TB
-define(ARPHRD_ARCNET, 7). %% ARCnet
-define(ARPHRD_APPLETLK, 8). %% APPLEtalk
-define(ARPHRD_DLCI, 15). %% Frame Relay DLCI
-define(ARPHRD_ATM, 19). %% ATM
-define(ARPHRD_METRICOM, 23). %% Metricom STRIP (new IANA id)
-define(ARPHRD_IEEE1394, 24). %% IEEE 1394 IPv4 - RFC 2734
-define(ARPHRD_EUI64, 27). %% EUI-64
-define(ARPHRD_INFINIBAND, 32). %% InfiniBand
%% Dummy types for non ARP hardware
-define(ARPHRD_SLIP, 256).
-define(ARPHRD_CSLIP, 257).
-define(ARPHRD_SLIP6, 258).
-define(ARPHRD_CSLIP6, 259).
-define(ARPHRD_RSRVD, 260). %% Notional KISS type
-define(ARPHRD_ADAPT, 264).
-define(ARPHRD_ROSE, 270).
-define(ARPHRD_X25, 271). %% CCITT X.25
-define(ARPHRD_HWX25, 272). %% Boards with X.25 in firmware
-define(ARPHRD_CAN, 280). %% Controller Area Network
-define(ARPHRD_PPP, 512).
-define(ARPHRD_CISCO, 513). %% Cisco HDLC
-define(ARPHRD_HDLC, 513).
-define(ARPHRD_LAPB, 516). %% LAPB
-define(ARPHRD_DDCMP, 517). %% Digitals DDCMP protocol
-define(ARPHRD_RAWHDLC, 518). %% Raw HDLC
-define(ARPHRD_TUNNEL, 768). %% IPIP tunnel
-define(ARPHRD_TUNNEL6, 769). %% IP6IP6 tunnel
-define(ARPHRD_FRAD, 770). %% Frame Relay Access Device
-define(ARPHRD_SKIP, 771). %% SKIP vif
-define(ARPHRD_LOOPBACK, 772). %% Loopback device
-define(ARPHRD_LOCALTLK, 773). %% Localtalk device
-define(ARPHRD_FDDI, 774). %% Fiber Distributed Data Interface
-define(ARPHRD_BIF, 775). %% AP1000 BIF
-define(ARPHRD_SIT, 776). %% sit0 device - IPv6-in-IPv4
-define(ARPHRD_IPDDP, 777). %% IP over DDP tunneller
-define(ARPHRD_IPGRE, 778). %% GRE over IP
-define(ARPHRD_PIMREG, 779). %% PIMSM register interface
-define(ARPHRD_HIPPI, 780). %% High Performance Parallel Interface
-define(ARPHRD_ASH, 781). %% Nexus 64Mbps Ash
-define(ARPHRD_ECONET, 782). %% Acorn Econet
-define(ARPHRD_IRDA, 783). %% Linux-IrDA
%% ARP works differently on different FC media .. so
-define(ARPHRD_FCPP, 784). %% Point to point fibrechannel
-define(ARPHRD_FCAL, 785). %% Fibrechannel arbitrated loop
-define(ARPHRD_FCPL, 786). %% Fibrechannel public loop
-define(ARPHRD_FCFABRIC, 787). %% Fibrechannel fabric
%% 787->799 reserved for fibrechannel media types
-define(ARPHRD_IEEE802_TR, 800). %% Magic type ident for TR
-define(ARPHRD_IEEE80211, 801). %% IEEE 802.11
-define(ARPHRD_IEEE80211_PRISM, 802). %% IEEE 802.11 + Prism2 header
-define(ARPHRD_IEEE80211_RADIOTAP, 803). %% IEEE 802.11 + radiotap header
-define(ARPHRD_IEEE802154, 804).
-define(ARPHRD_PHONET, 820). %% PhoNet media type
-define(ARPHRD_PHONET_PIPE, 821). %% PhoNet pipe header
-define(ARPHRD_CAIF, 822). %% CAIF media type

-define(ARPHRD_VOID, 65535). %% Void type, nothing is known
-define(ARPHRD_NONE, 65534). %% zero header length
86 changes: 74 additions & 12 deletions src/netlink.erl
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,68 @@ dec_nfnl_subsys(SubSys) when is_integer(SubSys) -> SubSys.
-define(RTM_GETDCB, 78).
-define(RTM_SETDCB, 79).

dec_ifi_type(?ARPHRD_NETROM) -> arphrd_netrom;
dec_ifi_type(?ARPHRD_ETHER ) -> arphrd_ether ;
dec_ifi_type(?ARPHRD_EETHER) -> arphrd_eether;
dec_ifi_type(?ARPHRD_AX25) -> arphrd_ax25;
dec_ifi_type(?ARPHRD_PRONET) -> arphrd_pronet;
dec_ifi_type(?ARPHRD_CHAOS) -> arphrd_chaos;
dec_ifi_type(?ARPHRD_IEEE802) -> arphrd_ieee802;
dec_ifi_type(?ARPHRD_ARCNET) -> arphrd_arcnet;
dec_ifi_type(?ARPHRD_APPLETLK) -> arphrd_appletlk;
dec_ifi_type(?ARPHRD_DLCI) -> arphrd_dlci;
dec_ifi_type(?ARPHRD_ATM) -> arphrd_atm;
dec_ifi_type(?ARPHRD_METRICOM) -> arphrd_metricom;
dec_ifi_type(?ARPHRD_IEEE1394) -> arphrd_ieee1394;
dec_ifi_type(?ARPHRD_EUI64) -> arphrd_eui64;
dec_ifi_type(?ARPHRD_INFINIBAND) -> arphrd_infiniband;
dec_ifi_type(?ARPHRD_SLIP) -> arphrd_slip;
dec_ifi_type(?ARPHRD_CSLIP) -> arphrd_cslip;
dec_ifi_type(?ARPHRD_SLIP6) -> arphrd_slip6;
dec_ifi_type(?ARPHRD_CSLIP6) -> arphrd_cslip6;
dec_ifi_type(?ARPHRD_RSRVD) -> arphrd_rsrvd;
dec_ifi_type(?ARPHRD_ADAPT) -> arphrd_adapt;
dec_ifi_type(?ARPHRD_ROSE) -> arphrd_rose;
dec_ifi_type(?ARPHRD_X25) -> arphrd_x25;
dec_ifi_type(?ARPHRD_HWX25) -> arphrd_hwx25;
dec_ifi_type(?ARPHRD_CAN) -> arphrd_can;
dec_ifi_type(?ARPHRD_PPP) -> arphrd_ppp;
dec_ifi_type(?ARPHRD_CISCO) -> arphrd_cisco;
dec_ifi_type(?ARPHRD_HDLC) -> arphrd_hdlc;
dec_ifi_type(?ARPHRD_LAPB) -> arphrd_lapb;
dec_ifi_type(?ARPHRD_DDCMP) -> arphrd_ddcmp;
dec_ifi_type(?ARPHRD_RAWHDLC) -> arphrd_rawhdlc;
dec_ifi_type(?ARPHRD_TUNNEL) -> arphrd_tunnel;
dec_ifi_type(?ARPHRD_TUNNEL6) -> arphrd_tunnel6;
dec_ifi_type(?ARPHRD_FRAD) -> arphrd_frad;
dec_ifi_type(?ARPHRD_SKIP) -> arphrd_skip;
dec_ifi_type(?ARPHRD_LOOPBACK) -> arphrd_loopback;
dec_ifi_type(?ARPHRD_LOCALTLK) -> arphrd_localtlk;
dec_ifi_type(?ARPHRD_FDDI) -> arphrd_fddi;
dec_ifi_type(?ARPHRD_BIF) -> arphrd_bif;
dec_ifi_type(?ARPHRD_SIT) -> arphrd_sit;
dec_ifi_type(?ARPHRD_IPDDP) -> arphrd_ipddp;
dec_ifi_type(?ARPHRD_IPGRE) -> arphrd_ipgre;
dec_ifi_type(?ARPHRD_PIMREG) -> arphrd_pimreg;
dec_ifi_type(?ARPHRD_HIPPI) -> arphrd_hippi;
dec_ifi_type(?ARPHRD_ASH) -> arphrd_ash;
dec_ifi_type(?ARPHRD_ECONET) -> arphrd_econet;
dec_ifi_type(?ARPHRD_IRDA) -> arphrd_irda;
dec_ifi_type(?ARPHRD_FCPP) -> arphrd_fcpp;
dec_ifi_type(?ARPHRD_FCAL) -> arphrd_fcal;
dec_ifi_type(?ARPHRD_FCPL) -> arphrd_fcpl;
dec_ifi_type(?ARPHRD_FCFABRIC) -> arphrd_fcfabric;
dec_ifi_type(?ARPHRD_IEEE802_TR) -> arphrd_ieee802_tr;
dec_ifi_type(?ARPHRD_IEEE80211) -> arphrd_ieee80211;
dec_ifi_type(?ARPHRD_IEEE80211_PRISM) -> arphrd_ieee80211_prism;
dec_ifi_type(?ARPHRD_IEEE80211_RADIOTAP) -> arphrd_ieee80211_radiotap;
dec_ifi_type(?ARPHRD_IEEE802154) -> arphrd_ieee802154;
dec_ifi_type(?ARPHRD_PHONET) -> arphrd_phonet;
dec_ifi_type(?ARPHRD_PHONET_PIPE) -> arphrd_phonet_pipe;
dec_ifi_type(?ARPHRD_CAIF) -> arphrd_caif;
dec_ifi_type(?ARPHRD_VOID) -> arphrd_void;
dec_ifi_type(?ARPHRD_NONE) -> arphrd_none.

create_table() ->
ets:new(?TAB, [named_table, public]).

Expand Down Expand Up @@ -591,7 +653,7 @@ dec_flags(Type, Flag) ->

dec_iff_flags(Flag) ->
dec_flags(iff_flags, Flag).

nl_dec_nl_attr(_Family, Type, Attr, flag, false, << Flag:8 >>) ->
{Attr, dec_flags(Type, Flag)};
nl_dec_nl_attr(_Family, Type, Attr, flag, false, << Flag:16 >>) ->
Expand Down Expand Up @@ -675,8 +737,8 @@ nl_dec_nla(Family, Type0, << Len:16/native-integer, NlaType:16/native-integer, R
<< Data:PLen/bytes, _Pad:Padding/bytes, NewRest/binary >> = Rest,

{NewAttr, DType} = dec_netlink(Type0, NlaType band 16#7FFF),
{Nested, DType1} = case DType of

{Nested, DType1} = case DType of
{nested, T} -> {true, T};
T -> { (NlaType band 16#8000) /= 0, T }
end,
Expand All @@ -698,25 +760,25 @@ nl_dec_payload({ctnetlink} = Type, _MsgType, << Family:8, Version:8, ResId:16/na
Fam = gen_socket:family(Family),
{ Fam, Version, ResId, nl_dec_nla(Fam, Type, Data) };

nl_dec_payload({rtnetlink}, MsgType, << Family:8, _Pad1:8, _Pad2:16, IfIndex:32/native-signed-integer, State:16/native-integer, Flags:8, NdmType:8, Data/binary >>)
nl_dec_payload({rtnetlink}, MsgType, << Family:8, _Pad1:8, _Pad2:16, IfIndex:32/native-signed-integer, State:16/native-integer, Flags:8, NdmType:8, Data/binary >>)
when MsgType == newneigh; MsgType == delneigh ->
Fam = gen_socket:family(Family),
{ Fam, IfIndex, State, Flags, NdmType, nl_dec_nla(Fam, {rtnetlink,neigh}, Data) };

nl_dec_payload({rtnetlink}, MsgType, << Family:8, DstLen:8, SrcLen:8, Tos:8, Table:8, Protocol:8, Scope:8, RtmType:8, Flags:32/native-integer, Data/binary >>)
nl_dec_payload({rtnetlink}, MsgType, << Family:8, DstLen:8, SrcLen:8, Tos:8, Table:8, Protocol:8, Scope:8, RtmType:8, Flags:32/native-integer, Data/binary >>)
when MsgType == newroute; MsgType == delroute ->
Fam = gen_socket:family(Family),
{ Fam, DstLen, SrcLen, Tos, Table, gen_socket:protocol(Protocol), Scope, RtmType, Flags, nl_dec_nla(Fam, {rtnetlink,route}, Data) };

nl_dec_payload({rtnetlink}, MsgType, << Family:8, PrefixLen:8, Flags:8, Scope:8, Index:32/native-integer, Data/binary >>)
nl_dec_payload({rtnetlink}, MsgType, << Family:8, PrefixLen:8, Flags:8, Scope:8, Index:32/native-integer, Data/binary >>)
when MsgType == newaddr; MsgType == deladdr ->
Fam = gen_socket:family(Family),
{ Fam, PrefixLen, Flags, Scope, Index, nl_dec_nla(Fam, {rtnetlink,addr}, Data) };

nl_dec_payload({rtnetlink}, MsgType, << Family:8, _Pad:8, Type:16/native-integer, Index:32/native-integer, Flags:32/native-integer, Change:32/native-integer, Data/binary >>)
nl_dec_payload({rtnetlink}, MsgType, << Family:8, _Pad:8, Type:16/native-integer, Index:32/native-integer, Flags:32/native-integer, Change:32/native-integer, Data/binary >>)
when MsgType == newlink; MsgType == dellink ->
Fam = gen_socket:family(Family),
{ Fam, Type, Index, dec_iff_flags(Flags), dec_iff_flags(Change), nl_dec_nla(Fam, {rtnetlink,link}, Data) };
{ Fam, dec_ifi_type(Type), Index, dec_iff_flags(Flags), dec_iff_flags(Change), nl_dec_nla(Fam, {rtnetlink,link}, Data) };

nl_dec_payload({rtnetlink}, MsgType, << Family:8, _Pad1:8, _Pad2:16, IfIndex:32/native-signed-integer, PfxType:8, PfxLen:8, Flags:8, _Pad3:8, Data/binary >>)
when MsgType == newprefix; MsgType == delprefix ->
Expand All @@ -731,7 +793,7 @@ nlmsg_ok(DataLen, MsgLen) ->

nl_ct_dec(<< _IpHdr:5/bytes, Len:32/native-integer, Type:16/native-integer, Flags:16/native-integer, Seq:32/native-integer, Pid:32/native-integer, Data/binary >> = Msg) ->
case nlmsg_ok(size(Msg) - 5, Len) of
true ->
true ->
SubSys = dec_nfnl_subsys(Type bsr 8),
MsgType = Type band 16#00FF,
{ SubSys, MsgType, Flags, Seq, Pid, nl_dec_payload({SubSys}, MsgType, Data) };
Expand All @@ -747,9 +809,9 @@ nl_rt_dec(Msg) ->

nl_rt_dec(<< Len:32/native-integer, Type:16/native-integer, Flags:16/native-integer, Seq:32/native-integer, Pid:32/native-integer, Data/binary >> = Msg, Acc) ->
{DecodedMsg, Next} = case nlmsg_ok(size(Msg), Len) of
true ->
true ->
PayLoadLen = Len - 16,
<< PayLoad:PayLoadLen/bytes, NextMsg/binary >> = Data,
<< PayLoad:PayLoadLen/bytes, NextMsg/binary >> = Data,
MsgType = dec_rtm_msgtype(Type),
{{ rtnetlink, MsgType, dec_flags(nlm_flags, Flags), Seq, Pid, nl_dec_payload({rtnetlink}, MsgType, PayLoad) }, NextMsg};
_ ->
Expand All @@ -769,7 +831,7 @@ enc_flags(Type, [F|T], Ret) ->
enc_flags(_Type, [], Ret) ->
Ret.

enc_nlmsghdr_flags(Type, Flags) when
enc_nlmsghdr_flags(Type, Flags) when
Type == getlink; Type == getaddr; Type == getroute; Type == getneigh;
Type == getrule; Type == getqdisc; Type == gettclass; Type == gettfilter;
Type == getaction; Type == getmulticast; Type == getanycast; Type == getneightbl;
Expand Down

0 comments on commit 28e7d13

Please sign in to comment.