Commit efe69c1b authored by Masahide NAKAMURA's avatar Masahide NAKAMURA Committed by Stephen Hemminger

ip: xfrm: Fix flush message.

Fix xfrm state or policy flush message.
And minor updates are included:
o Use static buffer to show unknown value as string.
o Show policy type (ptype) only when kernel specified it.
o Clean-up xfrm_monitor.
Signed-off-by: default avatarMasahide NAKAMURA <nakam@linux-ipv6.org>
Signed-off-by: default avatarStephen Hemminger <shemminger@linux-foundation.org>
parent c1fa2253
...@@ -114,6 +114,7 @@ struct typeent { ...@@ -114,6 +114,7 @@ struct typeent {
static const struct typeent xfrmproto_types[]= { static const struct typeent xfrmproto_types[]= {
{ "esp", IPPROTO_ESP }, { "ah", IPPROTO_AH }, { "comp", IPPROTO_COMP }, { "esp", IPPROTO_ESP }, { "ah", IPPROTO_AH }, { "comp", IPPROTO_COMP },
{ "route2", IPPROTO_ROUTING }, { "hao", IPPROTO_DSTOPTS }, { "route2", IPPROTO_ROUTING }, { "hao", IPPROTO_DSTOPTS },
{ "ipsec-any", IPSEC_PROTO_ANY },
{ NULL, -1 } { NULL, -1 }
}; };
...@@ -135,6 +136,7 @@ int xfrm_xfrmproto_getbyname(char *name) ...@@ -135,6 +136,7 @@ int xfrm_xfrmproto_getbyname(char *name)
const char *strxf_xfrmproto(__u8 proto) const char *strxf_xfrmproto(__u8 proto)
{ {
static char str[16];
int i; int i;
for (i = 0; ; i++) { for (i = 0; ; i++) {
...@@ -146,7 +148,8 @@ const char *strxf_xfrmproto(__u8 proto) ...@@ -146,7 +148,8 @@ const char *strxf_xfrmproto(__u8 proto)
return t->t_name; return t->t_name;
} }
return NULL; sprintf(str, "%u", proto);
return str;
} }
static const struct typeent algo_types[]= { static const struct typeent algo_types[]= {
...@@ -172,6 +175,7 @@ int xfrm_algotype_getbyname(char *name) ...@@ -172,6 +175,7 @@ int xfrm_algotype_getbyname(char *name)
const char *strxf_algotype(int type) const char *strxf_algotype(int type)
{ {
static char str[32];
int i; int i;
for (i = 0; ; i++) { for (i = 0; ; i++) {
...@@ -183,7 +187,8 @@ const char *strxf_algotype(int type) ...@@ -183,7 +187,8 @@ const char *strxf_algotype(int type)
return t->t_name; return t->t_name;
} }
return NULL; sprintf(str, "%d", type);
return str;
} }
const char *strxf_mask8(__u8 mask) const char *strxf_mask8(__u8 mask)
...@@ -251,6 +256,25 @@ const char *strxf_proto(__u8 proto) ...@@ -251,6 +256,25 @@ const char *strxf_proto(__u8 proto)
return p; return p;
} }
const char *strxf_ptype(__u8 ptype)
{
static char str[16];
switch (ptype) {
case XFRM_POLICY_TYPE_MAIN:
strcpy(str, "main");
break;
case XFRM_POLICY_TYPE_SUB:
strcpy(str, "sub");
break;
default:
sprintf(str, "%u", ptype);
break;
}
return str;
}
void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id, void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id,
__u8 mode, __u32 reqid, __u16 family, int force_spi, __u8 mode, __u32 reqid, __u16 family, int force_spi,
FILE *fp, const char *prefix, const char *title) FILE *fp, const char *prefix, const char *title)
...@@ -776,7 +800,6 @@ void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo, ...@@ -776,7 +800,6 @@ void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo,
const char *title) const char *title)
{ {
char buf[STRBUF_SIZE]; char buf[STRBUF_SIZE];
__u8 ptype = XFRM_POLICY_TYPE_MAIN;
memset(buf, '\0', sizeof(buf)); memset(buf, '\0', sizeof(buf));
...@@ -821,31 +844,18 @@ void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo, ...@@ -821,31 +844,18 @@ void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo,
fprintf(fp, "index %u ", xpinfo->index); fprintf(fp, "index %u ", xpinfo->index);
fprintf(fp, "priority %u ", xpinfo->priority); fprintf(fp, "priority %u ", xpinfo->priority);
fprintf(fp, "ptype ");
if (tb[XFRMA_POLICY_TYPE]) { if (tb[XFRMA_POLICY_TYPE]) {
struct xfrm_userpolicy_type *upt; struct xfrm_userpolicy_type *upt;
fprintf(fp, "ptype ");
if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
fprintf(fp, "(ERROR truncated)"); fprintf(fp, "(ERROR truncated)");
upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]); upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
ptype = upt->type; fprintf(fp, "%s ", strxf_ptype(upt->type));
} }
switch (ptype) {
case XFRM_POLICY_TYPE_MAIN:
fprintf(fp, "main");
break;
case XFRM_POLICY_TYPE_SUB:
fprintf(fp, "sub");
break;
default:
fprintf(fp, "%u", ptype);
break;
}
fprintf(fp, " ");
if (show_stats > 0) if (show_stats > 0)
fprintf(fp, "share %s ", strxf_share(xpinfo->share)); fprintf(fp, "share %s ", strxf_share(xpinfo->share));
......
...@@ -127,6 +127,7 @@ const char *strxf_mask8(__u8 mask); ...@@ -127,6 +127,7 @@ const char *strxf_mask8(__u8 mask);
const char *strxf_mask32(__u32 mask); const char *strxf_mask32(__u32 mask);
const char *strxf_share(__u8 share); const char *strxf_share(__u8 share);
const char *strxf_proto(__u8 proto); const char *strxf_proto(__u8 proto);
const char *strxf_ptype(__u8 ptype);
void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id, void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id,
__u8 mode, __u32 reqid, __u16 family, int force_spi, __u8 mode, __u32 reqid, __u16 family, int force_spi,
FILE *fp, const char *prefix, const char *title); FILE *fp, const char *prefix, const char *title);
......
...@@ -50,12 +50,6 @@ static int xfrm_acquire_print(const struct sockaddr_nl *who, ...@@ -50,12 +50,6 @@ static int xfrm_acquire_print(const struct sockaddr_nl *who,
struct rtattr * tb[XFRMA_MAX+1]; struct rtattr * tb[XFRMA_MAX+1];
__u16 family; __u16 family;
if (n->nlmsg_type != XFRM_MSG_ACQUIRE) {
fprintf(stderr, "Not an acquire: %08x %08x %08x\n",
n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
return 0;
}
len -= NLMSG_LENGTH(sizeof(*xacq)); len -= NLMSG_LENGTH(sizeof(*xacq));
if (len < 0) { if (len < 0) {
fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
...@@ -108,6 +102,74 @@ static int xfrm_acquire_print(const struct sockaddr_nl *who, ...@@ -108,6 +102,74 @@ static int xfrm_acquire_print(const struct sockaddr_nl *who,
return 0; return 0;
} }
static int xfrm_state_flush_print(const struct sockaddr_nl *who,
struct nlmsghdr *n, void *arg)
{
FILE *fp = (FILE*)arg;
struct xfrm_usersa_flush *xsf = NLMSG_DATA(n);
int len = n->nlmsg_len;
const char *str;
len -= NLMSG_SPACE(sizeof(*xsf));
if (len < 0) {
fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
return -1;
}
fprintf(fp, "Flushed state ");
str = strxf_xfrmproto(xsf->proto);
if (str)
fprintf(fp, "proto %s", str);
else
fprintf(fp, "proto %u", xsf->proto);
fprintf(fp, "%s", _SL_);
if (oneline)
fprintf(fp, "\n");
fflush(fp);
return 0;
}
static int xfrm_policy_flush_print(const struct sockaddr_nl *who,
struct nlmsghdr *n, void *arg)
{
struct rtattr * tb[XFRMA_MAX+1];
FILE *fp = (FILE*)arg;
int len = n->nlmsg_len;
len -= NLMSG_SPACE(0);
if (len < 0) {
fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
return -1;
}
fprintf(fp, "Flushed policy ");
parse_rtattr(tb, XFRMA_MAX, NLMSG_DATA(n), len);
if (tb[XFRMA_POLICY_TYPE]) {
struct xfrm_userpolicy_type *upt;
fprintf(fp, "ptype ");
if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
fprintf(fp, "(ERROR truncated)");
upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
fprintf(fp, "%s ", strxf_ptype(upt->type));
}
fprintf(fp, "%s", _SL_);
if (oneline)
fprintf(fp, "\n");
fflush(fp);
return 0;
}
static int xfrm_report_print(const struct sockaddr_nl *who, static int xfrm_report_print(const struct sockaddr_nl *who,
struct nlmsghdr *n, void *arg) struct nlmsghdr *n, void *arg)
{ {
...@@ -117,12 +179,6 @@ static int xfrm_report_print(const struct sockaddr_nl *who, ...@@ -117,12 +179,6 @@ static int xfrm_report_print(const struct sockaddr_nl *who,
struct rtattr * tb[XFRMA_MAX+1]; struct rtattr * tb[XFRMA_MAX+1];
__u16 family; __u16 family;
if (n->nlmsg_type != XFRM_MSG_REPORT) {
fprintf(stderr, "Not a report: %08x %08x %08x\n",
n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
return 0;
}
len -= NLMSG_LENGTH(sizeof(*xrep)); len -= NLMSG_LENGTH(sizeof(*xrep));
if (len < 0) { if (len < 0) {
fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
...@@ -201,42 +257,38 @@ static int xfrm_accept_msg(const struct sockaddr_nl *who, ...@@ -201,42 +257,38 @@ static int xfrm_accept_msg(const struct sockaddr_nl *who,
if (timestamp) if (timestamp)
print_timestamp(fp); print_timestamp(fp);
if (n->nlmsg_type == XFRM_MSG_NEWSA || switch (n->nlmsg_type) {
n->nlmsg_type == XFRM_MSG_DELSA || case XFRM_MSG_NEWSA:
n->nlmsg_type == XFRM_MSG_UPDSA || case XFRM_MSG_DELSA:
n->nlmsg_type == XFRM_MSG_EXPIRE) { case XFRM_MSG_UPDSA:
case XFRM_MSG_EXPIRE:
xfrm_state_print(who, n, arg); xfrm_state_print(who, n, arg);
return 0; return 0;
} case XFRM_MSG_NEWPOLICY:
if (n->nlmsg_type == XFRM_MSG_NEWPOLICY || case XFRM_MSG_DELPOLICY:
n->nlmsg_type == XFRM_MSG_DELPOLICY || case XFRM_MSG_UPDPOLICY:
n->nlmsg_type == XFRM_MSG_UPDPOLICY || case XFRM_MSG_POLEXPIRE:
n->nlmsg_type == XFRM_MSG_POLEXPIRE) {
xfrm_policy_print(who, n, arg); xfrm_policy_print(who, n, arg);
return 0; return 0;
} case XFRM_MSG_ACQUIRE:
if (n->nlmsg_type == XFRM_MSG_ACQUIRE) {
xfrm_acquire_print(who, n, arg); xfrm_acquire_print(who, n, arg);
return 0; return 0;
} case XFRM_MSG_FLUSHSA:
if (n->nlmsg_type == XFRM_MSG_FLUSHSA) { xfrm_state_flush_print(who, n, arg);
/* XXX: Todo: show proto in xfrm_usersa_flush */
fprintf(fp, "Flushed state\n");
return 0; return 0;
} case XFRM_MSG_FLUSHPOLICY:
if (n->nlmsg_type == XFRM_MSG_FLUSHPOLICY) { xfrm_policy_flush_print(who, n, arg);
fprintf(fp, "Flushed policy\n");
return 0; return 0;
} case XFRM_MSG_REPORT:
if (n->nlmsg_type == XFRM_MSG_REPORT) {
xfrm_report_print(who, n, arg); xfrm_report_print(who, n, arg);
return 0; return 0;
} case XFRM_MSG_NEWAE:
if (n->nlmsg_type == XFRM_MSG_NEWAE) {
xfrm_ae_print(who, n, arg); xfrm_ae_print(who, n, arg);
return 0; return 0;
default:
break;
} }
if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP && if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
n->nlmsg_type != NLMSG_DONE) { n->nlmsg_type != NLMSG_DONE) {
fprintf(fp, "Unknown message: %08d 0x%08x 0x%08x\n", fprintf(fp, "Unknown message: %08d 0x%08x 0x%08x\n",
......
...@@ -1053,7 +1053,6 @@ static int xfrm_state_flush(int argc, char **argv) ...@@ -1053,7 +1053,6 @@ static int xfrm_state_flush(int argc, char **argv)
if (show_stats > 1) if (show_stats > 1)
fprintf(stderr, "Flush state proto=%s\n", fprintf(stderr, "Flush state proto=%s\n",
(req.xsf.proto == IPSEC_PROTO_ANY) ? "any" :
strxf_xfrmproto(req.xsf.proto)); strxf_xfrmproto(req.xsf.proto));
if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment