📄 ipxfrm.c
字号:
/* $USAGI: $ */
/*
* Copyright (C)2004 USAGI/WIDE Project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* based on ip.c, iproute.c
*/
/*
* Authors:
* Masahide NAKAMURA @USAGI
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include <netdb.h>
#include <net/if.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/xfrm.h>
#include "utils.h"
#include "xfrm.h"
#define STRBUF_SIZE (128)
#define STRBUF_CAT(buf, str) \
do { \
int rest = sizeof(buf) - 1 - strlen(buf); \
if (rest > 0) { \
int len = strlen(str); \
if (len > rest) \
len = rest; \
strncat(buf, str, len); \
buf[sizeof(buf) - 1] = '\0'; \
} \
} while(0);
struct xfrm_filter filter;
static void usage(void) __attribute__((noreturn));
static void usage(void)
{
fprintf(stderr,
"Usage: ip xfrm XFRM_OBJECT { COMMAND | help }\n"
"where XFRM_OBJECT := { state | policy | monitor }\n");
exit(-1);
}
/* This is based on utils.c(inet_addr_match) */
int xfrm_addr_match(xfrm_address_t *x1, xfrm_address_t *x2, int bits)
{
__u32 *a1 = (__u32 *)x1;
__u32 *a2 = (__u32 *)x2;
int words = bits >> 0x05;
bits &= 0x1f;
if (words)
if (memcmp(a1, a2, words << 2))
return -1;
if (bits) {
__u32 w1, w2;
__u32 mask;
w1 = a1[words];
w2 = a2[words];
mask = htonl((0xffffffff) << (0x20 - bits));
if ((w1 ^ w2) & mask)
return 1;
}
return 0;
}
int xfrm_xfrmproto_is_ipsec(__u8 proto)
{
return (proto == IPPROTO_ESP ||
proto == IPPROTO_AH ||
proto == IPPROTO_COMP);
}
int xfrm_xfrmproto_is_ro(__u8 proto)
{
return (proto == IPPROTO_ROUTING ||
proto == IPPROTO_DSTOPTS);
}
struct typeent {
const char *t_name;
int t_type;
};
static const struct typeent xfrmproto_types[]= {
{ "esp", IPPROTO_ESP }, { "ah", IPPROTO_AH }, { "comp", IPPROTO_COMP },
{ "route2", IPPROTO_ROUTING }, { "hao", IPPROTO_DSTOPTS },
{ NULL, -1 }
};
int xfrm_xfrmproto_getbyname(char *name)
{
int i;
for (i = 0; ; i++) {
const struct typeent *t = &xfrmproto_types[i];
if (!t->t_name || t->t_type == -1)
break;
if (strcmp(t->t_name, name) == 0)
return t->t_type;
}
return -1;
}
const char *strxf_xfrmproto(__u8 proto)
{
int i;
for (i = 0; ; i++) {
const struct typeent *t = &xfrmproto_types[i];
if (!t->t_name || t->t_type == -1)
break;
if (t->t_type == proto)
return t->t_name;
}
return NULL;
}
static const struct typeent algo_types[]= {
{ "enc", XFRMA_ALG_CRYPT }, { "auth", XFRMA_ALG_AUTH },
{ "comp", XFRMA_ALG_COMP }, { NULL, -1 }
};
int xfrm_algotype_getbyname(char *name)
{
int i;
for (i = 0; ; i++) {
const struct typeent *t = &algo_types[i];
if (!t->t_name || t->t_type == -1)
break;
if (strcmp(t->t_name, name) == 0)
return t->t_type;
}
return -1;
}
const char *strxf_algotype(int type)
{
int i;
for (i = 0; ; i++) {
const struct typeent *t = &algo_types[i];
if (!t->t_name || t->t_type == -1)
break;
if (t->t_type == type)
return t->t_name;
}
return NULL;
}
const char *strxf_mask8(__u8 mask)
{
static char str[16];
const int sn = sizeof(mask) * 8 - 1;
__u8 b;
int i = 0;
for (b = (1 << sn); b > 0; b >>= 1)
str[i++] = ((b & mask) ? '1' : '0');
str[i] = '\0';
return str;
}
const char *strxf_mask32(__u32 mask)
{
static char str[16];
sprintf(str, "%.8x", mask);
return str;
}
const char *strxf_share(__u8 share)
{
static char str[32];
switch (share) {
case XFRM_SHARE_ANY:
strcpy(str, "any");
break;
case XFRM_SHARE_SESSION:
strcpy(str, "session");
break;
case XFRM_SHARE_USER:
strcpy(str, "user");
break;
case XFRM_SHARE_UNIQUE:
strcpy(str, "unique");
break;
default:
sprintf(str, "%u", share);
break;
}
return str;
}
const char *strxf_proto(__u8 proto)
{
static char buf[32];
struct protoent *pp;
const char *p;
pp = getprotobynumber(proto);
if (pp)
p = pp->p_name;
else {
sprintf(buf, "%u", proto);
p = buf;
}
return p;
}
void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id,
__u8 mode, __u32 reqid, __u16 family, int force_spi,
FILE *fp, const char *prefix, const char *title)
{
char abuf[256];
if (title)
fprintf(fp, title);
memset(abuf, '\0', sizeof(abuf));
fprintf(fp, "src %s ", rt_addr_n2a(family, sizeof(*saddr),
saddr, abuf, sizeof(abuf)));
memset(abuf, '\0', sizeof(abuf));
fprintf(fp, "dst %s", rt_addr_n2a(family, sizeof(id->daddr),
&id->daddr, abuf, sizeof(abuf)));
fprintf(fp, "%s", _SL_);
if (prefix)
fprintf(fp, prefix);
fprintf(fp, "\t");
fprintf(fp, "proto %s ", strxf_xfrmproto(id->proto));
if (show_stats > 0 || force_spi || id->spi) {
__u32 spi = ntohl(id->spi);
fprintf(fp, "spi 0x%08x", spi);
if (show_stats > 0)
fprintf(fp, "(%u)", spi);
fprintf(fp, " ");
}
fprintf(fp, "reqid %u", reqid);
if (show_stats > 0)
fprintf(fp, "(0x%08x)", reqid);
fprintf(fp, " ");
fprintf(fp, "mode ");
switch (mode) {
case XFRM_MODE_TRANSPORT:
fprintf(fp, "transport");
break;
case XFRM_MODE_TUNNEL:
fprintf(fp, "tunnel");
break;
case XFRM_MODE_ROUTEOPTIMIZATION:
fprintf(fp, "ro");
break;
case XFRM_MODE_IN_TRIGGER:
fprintf(fp, "in_trigger");
break;
case XFRM_MODE_BEET:
fprintf(fp, "beet");
break;
default:
fprintf(fp, "%u", mode);
break;
}
fprintf(fp, "%s", _SL_);
}
static const char *strxf_limit(__u64 limit)
{
static char str[32];
if (limit == XFRM_INF)
strcpy(str, "(INF)");
else
sprintf(str, "%llu", (unsigned long long) limit);
return str;
}
void xfrm_stats_print(struct xfrm_stats *s, FILE *fp, const char *prefix)
{
if (prefix)
fprintf(fp, prefix);
fprintf(fp, "stats:");
fprintf(fp, "%s", _SL_);
if (prefix)
fprintf(fp, prefix);
fprintf(fp, " ");
fprintf(fp, "replay-window %u ", s->replay_window);
fprintf(fp, "replay %u ", s->replay);
fprintf(fp, "failed %u", s->integrity_failed);
fprintf(fp, "%s", _SL_);
}
static const char *strxf_time(__u64 time)
{
static char str[32];
if (time == 0)
strcpy(str, "-");
else {
time_t t;
struct tm *tp;
/* XXX: treat time in the same manner of kernel's
* net/xfrm/xfrm_{user,state}.c
*/
t = (long)time;
tp = localtime(&t);
strftime(str, sizeof(str), "%Y-%m-%d %T", tp);
}
return str;
}
void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg,
struct xfrm_lifetime_cur *cur,
FILE *fp, const char *prefix)
{
if (cfg) {
if (prefix)
fprintf(fp, prefix);
fprintf(fp, "lifetime config:");
fprintf(fp, "%s", _SL_);
if (prefix)
fprintf(fp, prefix);
fprintf(fp, " ");
fprintf(fp, "limit: ");
fprintf(fp, "soft ");
fprintf(fp, strxf_limit(cfg->soft_byte_limit));
fprintf(fp, "(bytes), hard ");
fprintf(fp, strxf_limit(cfg->hard_byte_limit));
fprintf(fp, "(bytes)");
fprintf(fp, "%s", _SL_);
if (prefix)
fprintf(fp, prefix);
fprintf(fp, " ");
fprintf(fp, "limit: ");
fprintf(fp, "soft ");
fprintf(fp, strxf_limit(cfg->soft_packet_limit));
fprintf(fp, "(packets), hard ");
fprintf(fp, strxf_limit(cfg->hard_packet_limit));
fprintf(fp, "(packets)");
fprintf(fp, "%s", _SL_);
if (prefix)
fprintf(fp, prefix);
fprintf(fp, " ");
fprintf(fp, "expire add: ");
fprintf(fp, "soft ");
fprintf(fp, "%llu", (unsigned long long) cfg->soft_add_expires_seconds);
fprintf(fp, "(sec), hard ");
fprintf(fp, "%llu", (unsigned long long) cfg->hard_add_expires_seconds);
fprintf(fp, "(sec)");
fprintf(fp, "%s", _SL_);
if (prefix)
fprintf(fp, prefix);
fprintf(fp, " ");
fprintf(fp, "expire use: ");
fprintf(fp, "soft ");
fprintf(fp, "%llu", (unsigned long long) cfg->soft_use_expires_seconds);
fprintf(fp, "(sec), hard ");
fprintf(fp, "%llu", (unsigned long long) cfg->hard_use_expires_seconds);
fprintf(fp, "(sec)");
fprintf(fp, "%s", _SL_);
}
if (cur) {
if (prefix)
fprintf(fp, prefix);
fprintf(fp, "lifetime current:");
fprintf(fp, "%s", _SL_);
if (prefix)
fprintf(fp, prefix);
fprintf(fp, " ");
fprintf(fp, "%llu(bytes), ", (unsigned long long) cur->bytes);
fprintf(fp, "%llu(packets)", (unsigned long long) cur->packets);
fprintf(fp, "%s", _SL_);
if (prefix)
fprintf(fp, prefix);
fprintf(fp, " ");
fprintf(fp, "add %s ", strxf_time(cur->add_time));
fprintf(fp, "use %s", strxf_time(cur->use_time));
fprintf(fp, "%s", _SL_);
}
}
void xfrm_selector_print(struct xfrm_selector *sel, __u16 family,
FILE *fp, const char *prefix)
{
char abuf[256];
__u16 f;
f = sel->family;
if (f == AF_UNSPEC)
f = family;
if (f == AF_UNSPEC)
f = preferred_family;
if (prefix)
fprintf(fp, prefix);
memset(abuf, '\0', sizeof(abuf));
fprintf(fp, "src %s/%u ", rt_addr_n2a(f, sizeof(sel->saddr),
&sel->saddr, abuf, sizeof(abuf)),
sel->prefixlen_s);
memset(abuf, '\0', sizeof(abuf));
fprintf(fp, "dst %s/%u ", rt_addr_n2a(f, sizeof(sel->daddr),
&sel->daddr, abuf, sizeof(abuf)),
sel->prefixlen_d);
if (sel->proto)
fprintf(fp, "proto %s ", strxf_proto(sel->proto));
switch (sel->proto) {
case IPPROTO_TCP:
case IPPROTO_UDP:
case IPPROTO_SCTP:
case IPPROTO_DCCP:
default: /* XXX */
if (sel->sport_mask)
fprintf(fp, "sport %u ", ntohs(sel->sport));
if (sel->dport_mask)
fprintf(fp, "dport %u ", ntohs(sel->dport));
break;
case IPPROTO_ICMP:
case IPPROTO_ICMPV6:
/* type/code is stored at sport/dport in selector */
if (sel->sport_mask)
fprintf(fp, "type %u ", ntohs(sel->sport));
if (sel->dport_mask)
fprintf(fp, "code %u ", ntohs(sel->dport));
break;
case IPPROTO_MH:
if (sel->sport_mask)
fprintf(fp, "type %u ", ntohs(sel->sport));
if (sel->dport_mask) {
if (show_stats > 0)
fprintf(fp, "(dport) 0x%.4x ", sel->dport);
}
break;
}
if (sel->ifindex > 0) {
char buf[IFNAMSIZ];
memset(buf, '\0', sizeof(buf));
if_indextoname(sel->ifindex, buf);
fprintf(fp, "dev %s ", buf);
}
if (show_stats > 0)
fprintf(fp, "uid %u", sel->user);
fprintf(fp, "%s", _SL_);
}
static void xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
FILE *fp, const char *prefix)
{
int keylen;
int i;
if (prefix)
fprintf(fp, prefix);
fprintf(fp, "%s ", strxf_algotype(type));
if (len < sizeof(*algo)) {
fprintf(fp, "(ERROR truncated)");
goto fin;
}
len -= sizeof(*algo);
fprintf(fp, "%s ", algo->alg_name);
keylen = algo->alg_key_len / 8;
if (len < keylen) {
fprintf(fp, "(ERROR truncated)");
goto fin;
}
fprintf(fp, "0x");
for (i = 0; i < keylen; i ++)
fprintf(fp, "%.2x", (unsigned char)algo->alg_key[i]);
if (show_stats > 0)
fprintf(fp, " (%d bits)", algo->alg_key_len);
fin:
fprintf(fp, "%s", _SL_);
}
static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int len,
__u16 family, FILE *fp, const char *prefix)
{
int ntmpls = len / sizeof(struct xfrm_user_tmpl);
int i;
if (ntmpls <= 0) {
if (prefix)
fprintf(fp, prefix);
fprintf(fp, "(ERROR \"tmpl\" truncated)");
fprintf(fp, "%s", _SL_);
return;
}
for (i = 0; i < ntmpls; i++) {
struct xfrm_user_tmpl *tmpl = &tmpls[i];
if (prefix)
fprintf(fp, prefix);
xfrm_id_info_print(&tmpl->saddr, &tmpl->id, tmpl->mode,
tmpl->reqid, family, 0, fp, prefix, "tmpl ");
if (show_stats > 0 || tmpl->optional) {
if (prefix)
fprintf(fp, prefix);
fprintf(fp, "\t");
switch (tmpl->optional) {
case 0:
if (show_stats > 0)
fprintf(fp, "level required ");
break;
case 1:
fprintf(fp, "level use ");
break;
default:
fprintf(fp, "level %u ", tmpl->optional);
break;
}
if (show_stats > 0)
fprintf(fp, "share %s ", strxf_share(tmpl->share));
fprintf(fp, "%s", _SL_);
}
if (show_stats > 0) {
if (prefix)
fprintf(fp, prefix);
fprintf(fp, "\t");
fprintf(fp, "%s-mask %s ",
strxf_algotype(XFRMA_ALG_CRYPT),
strxf_mask32(tmpl->ealgos));
fprintf(fp, "%s-mask %s ",
strxf_algotype(XFRMA_ALG_AUTH),
strxf_mask32(tmpl->aalgos));
fprintf(fp, "%s-mask %s",
strxf_algotype(XFRMA_ALG_COMP),
strxf_mask32(tmpl->calgos));
fprintf(fp, "%s", _SL_);
}
}
}
void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
FILE *fp, const char *prefix)
{
if (tb[XFRMA_ALG_AUTH]) {
struct rtattr *rta = tb[XFRMA_ALG_AUTH];
xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
XFRMA_ALG_AUTH, RTA_PAYLOAD(rta), fp, prefix);
}
if (tb[XFRMA_ALG_CRYPT]) {
struct rtattr *rta = tb[XFRMA_ALG_CRYPT];
xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
XFRMA_ALG_CRYPT, RTA_PAYLOAD(rta), fp, prefix);
}
if (tb[XFRMA_ALG_COMP]) {
struct rtattr *rta = tb[XFRMA_ALG_COMP];
xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
XFRMA_ALG_COMP, RTA_PAYLOAD(rta), fp, prefix);
}
if (tb[XFRMA_ENCAP]) {
struct xfrm_encap_tmpl *e;
char abuf[256];
if (prefix)
fprintf(fp, prefix);
fprintf(fp, "encap ");
if (RTA_PAYLOAD(tb[XFRMA_ENCAP]) < sizeof(*e)) {
fprintf(fp, "(ERROR truncated)");
fprintf(fp, "%s", _SL_);
return;
}
e = (struct xfrm_encap_tmpl *) RTA_DATA(tb[XFRMA_ENCAP]);
fprintf(fp, "type ");
switch (e->encap_type) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -