rtnetlink

Section: Environments, Tables, and Troff Macros (7)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

rtnetlink - Linux ルーティングソケット  

書式

#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <sys/socket.h>

rtnetlink_socket = socket(AF_NETLINK, int socket_type, NETLINK_ROUTE);
 

説明

rtnetlink はカーネルのルーティングテーブルを読んだり変更したり するためのものである。これはカーネルが内部のサブシステムと 通信するためにも用いられているが、それはここでは記述しない。 この man ページではユーザー空間のプログラムとの通信に関してのみ述べる。 ネットワーク経路・IP アドレス・リンクパラメーター・ 近傍設定 (neighbor setup)・キューイングルール (queueing dicipline)・ トラフィッククラス・パケットのクラス分類などが、すべて NETLINK_ROUTE ソケットを通して制御できる。 rtnetlink は netlink メッセージをベースにしている。詳細は netlink(7) を見ること。  

ルーティング属性

rtnetlink メッセージには、初期ヘッダーの後に付加的な属性を 持つものがある。

struct rtattr {
    unsigned short rta_len;    /* Length of option */
    unsigned short rta_type;   /* Type of option */
    /* Data follows */
};

これらの属性の操作は、 RTA_* マクロか libnetlink のみを使って行うべきである。 rtnetlink(3) を見よ。  

メッセージ

rtnetlink は (標準的な netlink メッセージに加えて) 以下のメッセージタイプから構成される。
RTM_NEWLINK
RTM_DELLINK
RTM_GETLINK
指定したネットワークインターフェースの情報を、生成・削除・取得する。 これらのメッセージは ifinfomsg 構造体と、それに続いていくつかの rtattr 構造体を伴う。
struct ifinfomsg {
    unsigned char  ifi_family; /* AF_UNSPEC */
    unsigned short ifi_type;   /* Device type */
    int            ifi_index;  /* Interface index */
    unsigned int   ifi_flags;  /* Device flags  */
    unsigned int   ifi_change; /* change mask */
};
ifi_flags contains the device flags, see netdevice(7); ifi_index is the unique interface index (since Linux 3.7, it is possible to feed a nonzero value with the RTM_NEWLINK message, thus creating a link with the given ifindex); ifi_change is a mask specifying which bits of ifi_flags should be applied to the device.
ルーティング属性
rta_type値の型説明

IFLA_UNSPEC-指定されていない
IFLA_ADDRESShardware addressインターフェース L2 アドレス
IFLA_BROADCASThardware addressL2 ブロードキャストアドレス
IFLA_IFNAMEasciiz stringデバイス名
IFLA_MTUunsigned intデバイスの MTU
IFLA_LINKintリンクタイプ
IFLA_QDISCasciiz stringキューイングのルール
IFLA_STATS 下記参照 インターフェースの統計
IFLA_PERM_ADDRESShardware address hardware address provided by device (since Linux 5.5)
IFLA_STATS の値の型は struct rtnl_link_stats (Linux 2.4 以前では struct net_device_stats) である。
RTM_NEWADDR
RTM_DELADDR
RTM_GETADDR
Add, remove, or receive information about an IP address associated with an interface. In Linux 2.2, an interface can carry multiple IP addresses, this replaces the alias device concept in Linux 2.0. In Linux 2.2, these messages support IPv4 and IPv6 addresses. They contain an ifaddrmsg structure, optionally followed by rtattr routing attributes.
struct ifaddrmsg {
    unsigned char ifa_family;    /* Address type */
    unsigned char ifa_prefixlen; /* Prefixlength of address */
    unsigned char ifa_flags;     /* Address flags */
    unsigned char ifa_scope;     /* Address scope */
    unsigned int  ifa_index;     /* Interface index */
};
ifa_family はアドレスファミリーのタイプである (現在は AF_INET または AF_INET6)。 ifa_prefixlen はアドレスのアドレスマスクの長さである (IPv4 のように、 そのファミリーで定義されている場合)。 ifa_scope はアドレスのスコープである。 ifa_index はアドレスが関連づけられているインターフェースの index である。 ifa_flags はフラグワードで、 二つめのアドレス (古い別名インターフェース) の場合は IFA_F_SECONDARY に、永続的なアドレスの場合は IFA_F_PERMANENT に適用される。ユーザーによってセットされるフラグと、 undocumented なフラグがある。
属性
rta_type値の型説明

IFA_UNSPEC-指定されていない
IFA_ADDRESSraw protocol addressインターフェースアドレス
IFA_LOCALraw protocol addressローカルアドレス
IFA_LABELasciiz stringインターフェースの名前
IFA_BROADCASTraw protocol addressブロードキャストアドレス
IFA_ANYCASTraw protocol addressanycast アドレス
IFA_CACHEINFOstruct ifa_cacheinfoアドレス情報
struct ifa_cacheinfo {
    __u32 ifa_prefered; // Preferred lifetime remaining, in seconds
    __u32 ifa_valid;    // Valid lifetime remaining, in seconds
    __u32 cstamp;       // Creation timestamp, in centiseconds
    __u32 tstamp;       // Update timestamp, in centiseconds
};
ifa_valid cannot be zero, and ifa_prefered cannot be greater than ifa_valid. A value of UINT32_MAX represents an infinite lifetime.
RTM_NEWROUTE
RTM_DELROUTE
RTM_GETROUTE
ネットワーク経路の情報を生成・削除・取得する。 これらのメッセージは rtmsg 構造体を伴う。そのあとにいくつかの rtattr 構造体を続けることもできる。 RTM_GETROUTE で rtm_dst_len と rtm_src_len に 0 をセットすると、 指定されたルーティングテーブルの全てのエントリーを所得する。 rtm_table と rtm_protocol 以外の他のフィールドに 0 を入れると、ワイルドカードを意味する。
struct rtmsg {
    unsigned char rtm_family;   /* Address family of route */
    unsigned char rtm_dst_len;  /* Length of destination */
    unsigned char rtm_src_len;  /* Length of source */
    unsigned char rtm_tos;      /* TOS filter */
    unsigned char rtm_table;    /* Routing table ID;
                                   see RTA_TABLE below */
    unsigned char rtm_protocol; /* Routing protocol; see below */
    unsigned char rtm_scope;    /* See below */
    unsigned char rtm_type;     /* See below */

    unsigned int  rtm_flags;
};
rtm_type経路のタイプ

RTN_UNSPEC未知の経路
RTN_UNICASTゲートウェイまたはダイレクトな経路
RTN_LOCALローカルインターフェースの経路
RTN_BROADCAST ローカルなブロードキャスト経路 (ブロードキャストとして送信される)
RTN_ANYCAST ローカルなブロードキャスト経路 (ユニキャストとして送信される)
RTN_MULTICASTマルチキャスト経路
RTN_BLACKHOLEパケットを捨てる経路
RTN_UNREACHABLE到達できない行き先
RTN_PROHIBITパケットを拒否する経路
RTN_THROW経路探索を別のテーブルで継続
RTN_NATネットワークアドレスの変換ルール
RTN_XRESOLVE 外部レゾルバを参照 (実装されていない)
rtm_protocol経路の情報源

RTPROT_UNSPEC不明
RTPROT_REDIRECT ICMP リダイレクトによる (現在は用いられない)
RTPROT_KERNELカーネルによる
RTPROT_BOOTブート時
RTPROT_STATIC管理者による
RTPROT_STATIC よりも大きな値はカーネルによって解釈されない。これは 単なるユーザーへの情報である。これらは経路情報の情報源を タグ付けしたり、複数のルーティングデーモンからの情報を 区別するために用いることができる。 既に割り当てられているルーティングデーモンの識別子については <linux/rtnetlink.h> を見よ。
rtm_scope は行き先への距離である。
RT_SCOPE_UNIVERSEグローバルな経路
RT_SCOPE_SITE ローカルな自律システムにおける内部経路
RT_SCOPE_LINKこのリンク上の経路
RT_SCOPE_HOSTローカルホスト上の経路
RT_SCOPE_NOWHERE行き先が存在しない
ユーザーは RT_SCOPE_UNIVERSE と RT_SCOPE_SITE の間の値を用いることができる。
rtm_flags は以下の意味を持つ:
RTM_F_NOTIFY 経路が変更されると、 rtnetlink を通してユーザーに通知が行く。
RTM_F_CLONED経路は他の経路によって複製された。
RTM_F_EQUALIZEマルチパスイコライザ (まだ実装されていない)
rtm_table ではルーティングテーブルを指定する。
RT_TABLE_UNSPEC指定されていないルーティングテーブル
RT_TABLE_DEFAULTデフォルトのテーブル
RT_TABLE_MAINメインのテーブル
RT_TABLE_LOCALローカルテーブル
ユーザーは RT_TABLE_UNSPEC と RT_TABLE_DEFAULT. の間の任意の値を用いることができる。
属性
rta_type  値の型  説明

RTA_UNSPEC  -  無視される
RTA_DST  protocol address  経路の行き先アドレス
RTA_SRC  protocol address  経路の発信元アドレス
RTA_IIF  int  入力インターフェースの index
RTA_OIF  int  出力インターフェースの index
RTA_GATEWAY  protocol address  経路のゲートウェイ
RTA_PRIORITY  int  経路の優先度
RTA_PREFSRC  protocol address  Preferred source address
RTA_METRICS     Route metrics
(see below).
RTA_MULTIPATH     Multipath nexthop data
(see below).
RTA_PROTOINFO    No longer used
RTA_FLOW  int  Route realm
RTA_CACHEINFO  struct rta_cacheinfo  (linux/rtnetlink.h 参照)
RTA_SESSION    No longer used
RTA_MP_ALGO    No longer used
RTA_TABLE  int   Routing table ID; if set,
rtm_table is ignored
RTA_MARK  int  
RTA_MFC_STATS  struct rta_mfc_stats  (linux/rtnetlink.h 参照)
RTA_VIA  struct rtvia   Gateway in different AF (see below)
RTA_NEWDST  protocol address   パケットの経路の行き先アドレスを変更する
RTA_PREF  char   RFC4191 IPv6 router preference (see below)
RTA_ENCAP_TYPE  short   Encapsulation type for
lwtunnels (下記参照)
RTA_ENCAP    Defined by RTA_ENCAP_TYPE
RTA_EXPIRES  int   Expire time for IPv6 routes (in seconds)
RTA_METRICS contains an array of struct rtattr with their corresponding attributes:
属性
rta_type値の型説明

RTAX_UNSPEC-指定されていない
RTAX_LOCK__u32 Bit field indicating which RTAX_* attributes are locked.
RTAX_MTU__u32 Maximum Transmission Unit for this route.
RTAX_WINDOW__u32 Maximum size of the receive window for this route.
RTAX_RTT__u32 Estimated round-trip time for this route.
RTAX_RTTVAR__u32 Estimated round-trip time variation for this route.
RTAX_SSTHRESH__u32 Slow start threshold to use for this route.
RTAX_CWND__u32 Maximum size of the congestion window for this route.
RTAX_ADVMSS__u32 Maximum Segment Size to advertise for this route.
RTAX_REORDERING__u32 Initial reordering level of packets for this route.
RTAX_HOPLIMIT__u32 Hop limit (TTL) to use for this route.
RTAX_INITCWND__u32 Initial congestion window to use for this route.
RTAX_FEATURES__u32 Features to enable for this route specifically.
RTAX_RTO_MIN__u32 Minimum Retransmission TimeOut to use for this route.
RTAX_INITRWND__u32 Initial size of the receive window for this route.
RTAX_QUICKACK__u32 Use quick ack for this route.
RTAX_CC_ALGOasciiz string Congestion Control algorithm to use for this route.
RTAX_FASTOPEN_NO_COOKIE__u32 Allow TCP Fast Open without cookie.
Metrics that are locked with RTAX_LOCK take precedence over the values normally used by the kernel (computed or assigned by a sysctl or setsockopt(2)). Therefore, some metrics, like RTAX_RTO_MIN, have no effect unless their bit is set in RTAX_LOCK.
RTA_MULTIPATH contains several packed instances of struct rtnexthop together with nested RTAs (RTA_GATEWAY):
struct rtnexthop {
    unsigned short rtnh_len;     /* Length of struct + length
                                    of RTAs */
    unsigned char  rtnh_flags;   /* Flags (see
                                    linux/rtnetlink.h) */
    unsigned char  rtnh_hops;    /* Nexthop priority */
    int            rtnh_ifindex; /* Interface index for this
                                    nexthop */
}
There exist a bunch of RTNH_* macros similar to RTA_* and NLHDR_* macros useful to handle these structures.
struct rtvia {
    unsigned short rtvia_family;
    unsigned char  rtvia_addr[0];
};
rtvia_addr is the address, rtvia_family is its family type.
RTA_PREF may contain values ICMPV6_ROUTER_PREF_LOW, ICMPV6_ROUTER_PREF_MEDIUM, and ICMPV6_ROUTER_PREF_HIGH defined incw <linux/icmpv6.h>.
RTA_ENCAP_TYPE may contain values LWTUNNEL_ENCAP_MPLS, LWTUNNEL_ENCAP_IP, LWTUNNEL_ENCAP_ILA, or LWTUNNEL_ENCAP_IP6 defined in <linux/lwtunnel.h>.
(これらの値を埋めること!)
RTM_NEWNEIGH
RTM_DELNEIGH
RTM_GETNEIGH
近傍テーブル (neighbor table) のエントリー (例えば ARP エントリー) の情報を追加・削除・取得する。 このメッセージは ndmsg 構造体を伴う。
struct ndmsg {
    unsigned char ndm_family;
    int           ndm_ifindex;  /* Interface index */
    __u16         ndm_state;    /* State */
    __u8          ndm_flags;    /* Flags */
    __u8          ndm_type;
};

struct nda_cacheinfo {
    __u32         ndm_confirmed;
    __u32         ndm_used;
    __u32         ndm_updated;
    __u32         ndm_refcnt;
};
ndm_state は以下の状態のビットマスクである:
NUD_INCOMPLETE現在レゾルブ中のキャッシュエントリー
NUD_REACHABLE動作確認済みのキャッシュエントリー
NUD_STALE期限切れのキャッシュエントリー
NUD_DELAYタイマー待ちのキャッシュエントリー
NUD_PROBE再確認中のキャッシュエントリー
NUD_FAILED不正なキャッシュエントリー
NUD_NOARP行き先キャッシュのないデバイス
NUD_PERMANENT静的なエントリー
有効な ndm_flags は以下の通り:
NTF_PROXYプロクシ arp エントリー
NTF_ROUTERIPv6 ルータ
rtattr 構造体は、 rta_type フィールドに応じてそれぞれ以下の意味を持つ:
NDA_UNSPEC未知のタイプ
NDA_DST近傍キャッシュネットワーク層の行き先アドレス
NDA_LLADDR近傍キャッシュリンク層のアドレス
NDA_CACHEINFOキャッシュの統計
rta_type フィールドが NDA_CACHEINFO の場合には、 struct nda_cacheinfo ヘッダーが続く。
RTM_NEWRULE
RTM_DELRULE
RTM_GETRULE
ルーティングルールを追加・削除・取得する。 struct rtmsg を伴う。
RTM_NEWQDISC
RTM_DELQDISC
RTM_GETQDISC
キューイングルールを追加・削除・取得する。 このメッセージは struct tcmsg を伴い、またそのあとに属性がいくつか続くこともある。
struct tcmsg {
    unsigned char    tcm_family;
    int              tcm_ifindex;   /* interface index */
    __u32            tcm_handle;    /* Qdisc handle */
    __u32            tcm_parent;    /* Parent qdisc */
    __u32            tcm_info;
};
属性
rta_type  値の型  説明

TCA_UNSPEC  -  指定されていない
TCA_KIND  asciiz string  キューイングルールの名前
TCA_OPTIONS  byte sequence  Qdisc 特有のオプションが続く
TCA_STATS  struct tc_stats  Qdisc の統計
TCA_XSTATS  qdisc-specific  モジュール特有の統計
TCA_RATE  struct tc_estimator  レート制限
さらに、 qdisc モジュール特有の様々な属性を指定できる。 詳細な情報は適切なインクルードファイルを見よ。
RTM_NEWTCLASS
RTM_DELTCLASS
RTM_GETTCLASS
トラフィッククラスを追加・削除・取得する。 これらのメッセージは、上述の struct tcmsg を伴う。
RTM_NEWTFILTER
RTM_DELTFILTER
RTM_GETTFILTER
トラフィックフィルターの情報を追加・削除・取得する。 これらのメッセージは、上述の struct tcmsg を伴う。
 

バージョン

rtnetlink は Linux 2.2 の新機能である。  

バグ

このマニュアルは完全ではない。  

関連項目

cmsg(3), rtnetlink(3), ip(7), netlink(7)


 

Index

名前
書式
説明
ルーティング属性
メッセージ
バージョン
バグ
関連項目

This document was created by man2html, using the manual pages.