]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - ip/xfrm_monitor.c
Merge branch 'iproute2-master' into iproute2-next
[mirror_iproute2.git] / ip / xfrm_monitor.c
index ebccb71c688ef0099f1bee36b2f86b29af30e6e8..76905ed3f1e14c56ae338e0d73998ac99c763389 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <netinet/in.h>
-#include <linux/xfrm.h>
 
 #include "utils.h"
 #include "xfrm.h"
 #include "ip_common.h"
 
 static void usage(void) __attribute__((noreturn));
-int listen_all_nsid;
+static int listen_all_nsid;
 
 static void usage(void)
 {
@@ -44,13 +43,12 @@ static void usage(void)
        exit(-1);
 }
 
-static int xfrm_acquire_print(const struct sockaddr_nl *who,
-                             struct nlmsghdr *n, void *arg)
+static int xfrm_acquire_print(struct nlmsghdr *n, void *arg)
 {
-       FILE *fp = (FILE*)arg;
+       FILE *fp = (FILE *)arg;
        struct xfrm_user_acquire *xacq = NLMSG_DATA(n);
        int len = n->nlmsg_len;
-       struct rtattr * tb[XFRMA_MAX+1];
+       struct rtattr *tb[XFRMA_MAX+1];
        __u16 family;
 
        len -= NLMSG_LENGTH(sizeof(*xacq));
@@ -72,6 +70,7 @@ static int xfrm_acquire_print(const struct sockaddr_nl *who,
        fprintf(fp, "proto %s ", strxf_xfrmproto(xacq->id.proto));
        if (show_stats > 0 || xacq->id.spi) {
                __u32 spi = ntohl(xacq->id.spi);
+
                fprintf(fp, "spi 0x%08x", spi);
                if (show_stats > 0)
                        fprintf(fp, "(%u)", spi);
@@ -105,10 +104,9 @@ static int xfrm_acquire_print(const struct sockaddr_nl *who,
        return 0;
 }
 
-static int xfrm_state_flush_print(const struct sockaddr_nl *who,
-                                 struct nlmsghdr *n, void *arg)
+static int xfrm_state_flush_print(struct nlmsghdr *n, void *arg)
 {
-       FILE *fp = (FILE*)arg;
+       FILE *fp = (FILE *)arg;
        struct xfrm_usersa_flush *xsf = NLMSG_DATA(n);
        int len = n->nlmsg_len;
        const char *str;
@@ -135,11 +133,10 @@ static int xfrm_state_flush_print(const struct sockaddr_nl *who,
        return 0;
 }
 
-static int xfrm_policy_flush_print(const struct sockaddr_nl *who,
-                                  struct nlmsghdr *n, void *arg)
+static int xfrm_policy_flush_print(struct nlmsghdr *n, void *arg)
 {
-       struct rtattr * tb[XFRMA_MAX+1];
-       FILE *fp = (FILE*)arg;
+       struct rtattr *tb[XFRMA_MAX+1];
+       FILE *fp = (FILE *)arg;
        int len = n->nlmsg_len;
 
        len -= NLMSG_SPACE(0);
@@ -160,7 +157,7 @@ static int xfrm_policy_flush_print(const struct sockaddr_nl *who,
                if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt))
                        fprintf(fp, "(ERROR truncated)");
 
-               upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]);
+               upt = RTA_DATA(tb[XFRMA_POLICY_TYPE]);
                fprintf(fp, "%s ", strxf_ptype(upt->type));
        }
 
@@ -173,13 +170,12 @@ static int xfrm_policy_flush_print(const struct sockaddr_nl *who,
        return 0;
 }
 
-static int xfrm_report_print(const struct sockaddr_nl *who,
-                            struct nlmsghdr *n, void *arg)
+static int xfrm_report_print(struct nlmsghdr *n, void *arg)
 {
-       FILE *fp = (FILE*)arg;
+       FILE *fp = (FILE *)arg;
        struct xfrm_user_report *xrep = NLMSG_DATA(n);
        int len = n->nlmsg_len;
-       struct rtattr * tb[XFRMA_MAX+1];
+       struct rtattr *tb[XFRMA_MAX+1];
        __u16 family;
 
        len -= NLMSG_LENGTH(sizeof(*xrep));
@@ -211,7 +207,8 @@ static int xfrm_report_print(const struct sockaddr_nl *who,
 
 static void xfrm_ae_flags_print(__u32 flags, void *arg)
 {
-       FILE *fp = (FILE*)arg;
+       FILE *fp = (FILE *)arg;
+
        fprintf(fp, " (0x%x) ", flags);
        if (!flags)
                return;
@@ -226,12 +223,8 @@ static void xfrm_ae_flags_print(__u32 flags, void *arg)
 
 static void xfrm_usersa_print(const struct xfrm_usersa_id *sa_id, __u32 reqid, FILE *fp)
 {
-       char buf[256];
-
-       buf[0] = 0;
        fprintf(fp, "dst %s ",
-               rt_addr_n2a(sa_id->family, sizeof(sa_id->daddr), &sa_id->daddr,
-                           buf, sizeof(buf)));
+               rt_addr_n2a(sa_id->family, sizeof(sa_id->daddr), &sa_id->daddr));
 
        fprintf(fp, " reqid 0x%x", reqid);
 
@@ -239,20 +232,16 @@ static void xfrm_usersa_print(const struct xfrm_usersa_id *sa_id, __u32 reqid, F
        fprintf(fp, " SPI 0x%x", ntohl(sa_id->spi));
 }
 
-static int xfrm_ae_print(const struct sockaddr_nl *who,
-                            struct nlmsghdr *n, void *arg)
+static int xfrm_ae_print(struct nlmsghdr *n, void *arg)
 {
-       FILE *fp = (FILE*)arg;
+       FILE *fp = (FILE *)arg;
        struct xfrm_aevent_id *id = NLMSG_DATA(n);
-       char abuf[256];
 
        fprintf(fp, "Async event ");
        xfrm_ae_flags_print(id->flags, arg);
-       fprintf(fp,"\n\t");
-       memset(abuf, '\0', sizeof(abuf));
+       fprintf(fp, "\n\t");
        fprintf(fp, "src %s ", rt_addr_n2a(id->sa_id.family,
-                                          sizeof(id->saddr), &id->saddr,
-                                          abuf, sizeof(abuf)));
+                                          sizeof(id->saddr), &id->saddr));
 
        xfrm_usersa_print(&id->sa_id, id->reqid, fp);
 
@@ -264,16 +253,12 @@ static int xfrm_ae_print(const struct sockaddr_nl *who,
 
 static void xfrm_print_addr(FILE *fp, int family, xfrm_address_t *a)
 {
-       char buf[256];
-
-       buf[0] = 0;
-       fprintf(fp, "%s", rt_addr_n2a(family, sizeof(*a), a, buf, sizeof(buf)));
+       fprintf(fp, "%s", rt_addr_n2a(family, sizeof(*a), a));
 }
 
-static int xfrm_mapping_print(const struct sockaddr_nl *who,
-                            struct nlmsghdr *n, void *arg)
+static int xfrm_mapping_print(struct nlmsghdr *n, void *arg)
 {
-       FILE *fp = (FILE*)arg;
+       FILE *fp = (FILE *)arg;
        struct xfrm_user_mapping *map = NLMSG_DATA(n);
 
        fprintf(fp, "Mapping change ");
@@ -290,11 +275,10 @@ static int xfrm_mapping_print(const struct sockaddr_nl *who,
        return 0;
 }
 
-static int xfrm_accept_msg(const struct sockaddr_nl *who,
-                          struct rtnl_ctrl_data *ctrl,
+static int xfrm_accept_msg(struct rtnl_ctrl_data *ctrl,
                           struct nlmsghdr *n, void *arg)
 {
-       FILE *fp = (FILE*)arg;
+       FILE *fp = (FILE *)arg;
 
        if (timestamp)
                print_timestamp(fp);
@@ -311,31 +295,31 @@ static int xfrm_accept_msg(const struct sockaddr_nl *who,
        case XFRM_MSG_DELSA:
        case XFRM_MSG_UPDSA:
        case XFRM_MSG_EXPIRE:
-               xfrm_state_print(who, n, arg);
+               xfrm_state_print(n, arg);
                return 0;
        case XFRM_MSG_NEWPOLICY:
        case XFRM_MSG_DELPOLICY:
        case XFRM_MSG_UPDPOLICY:
        case XFRM_MSG_POLEXPIRE:
-               xfrm_policy_print(who, n, arg);
+               xfrm_policy_print(n, arg);
                return 0;
        case XFRM_MSG_ACQUIRE:
-               xfrm_acquire_print(who, n, arg);
+               xfrm_acquire_print(n, arg);
                return 0;
        case XFRM_MSG_FLUSHSA:
-               xfrm_state_flush_print(who, n, arg);
+               xfrm_state_flush_print(n, arg);
                return 0;
        case XFRM_MSG_FLUSHPOLICY:
-               xfrm_policy_flush_print(who, n, arg);
+               xfrm_policy_flush_print(n, arg);
                return 0;
        case XFRM_MSG_REPORT:
-               xfrm_report_print(who, n, arg);
+               xfrm_report_print(n, arg);
                return 0;
        case XFRM_MSG_NEWAE:
-               xfrm_ae_print(who, n, arg);
+               xfrm_ae_print(n, arg);
                return 0;
        case XFRM_MSG_MAPPING:
-               xfrm_mapping_print(who, n, arg);
+               xfrm_mapping_print(n, arg);
                return 0;
        default:
                break;
@@ -354,13 +338,13 @@ extern struct rtnl_handle rth;
 int do_xfrm_monitor(int argc, char **argv)
 {
        char *file = NULL;
-       unsigned groups = ~((unsigned)0); /* XXX */
-       int lacquire=0;
-       int lexpire=0;
-       int laevent=0;
-       int lpolicy=0;
-       int lsa=0;
-       int lreport=0;
+       unsigned int groups = ~((unsigned)0); /* XXX */
+       int lacquire = 0;
+       int lexpire = 0;
+       int laevent = 0;
+       int lpolicy = 0;
+       int lsa = 0;
+       int lreport = 0;
 
        rtnl_close(&rth);
 
@@ -368,29 +352,31 @@ int do_xfrm_monitor(int argc, char **argv)
                if (matches(*argv, "file") == 0) {
                        NEXT_ARG();
                        file = *argv;
+               } else if (strcmp(*argv, "all") == 0) {
+                       /* fall out */
                } else if (matches(*argv, "all-nsid") == 0) {
                        listen_all_nsid = 1;
                } else if (matches(*argv, "acquire") == 0) {
-                       lacquire=1;
+                       lacquire = 1;
                        groups = 0;
                } else if (matches(*argv, "expire") == 0) {
-                       lexpire=1;
+                       lexpire = 1;
                        groups = 0;
                } else if (matches(*argv, "SA") == 0) {
-                       lsa=1;
+                       lsa = 1;
                        groups = 0;
                } else if (matches(*argv, "aevent") == 0) {
-                       laevent=1;
+                       laevent = 1;
                        groups = 0;
                } else if (matches(*argv, "policy") == 0) {
-                       lpolicy=1;
+                       lpolicy = 1;
                        groups = 0;
                } else if (matches(*argv, "report") == 0) {
-                       lreport=1;
+                       lreport = 1;
                        groups = 0;
                } else if (matches(*argv, "help") == 0) {
                        usage();
-               } else if (strcmp(*argv, "all")) {
+               } else {
                        fprintf(stderr, "Argument \"%s\" is unknown, try \"ip xfrm monitor help\".\n", *argv);
                        exit(-1);
                }
@@ -412,12 +398,16 @@ int do_xfrm_monitor(int argc, char **argv)
 
        if (file) {
                FILE *fp;
+               int err;
+
                fp = fopen(file, "r");
                if (fp == NULL) {
                        perror("Cannot fopen");
                        exit(-1);
                }
-               return rtnl_from_file(fp, xfrm_accept_msg, (void*)stdout);
+               err = rtnl_from_file(fp, xfrm_accept_msg, stdout);
+               fclose(fp);
+               return err;
        }
 
        if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0)
@@ -425,7 +415,7 @@ int do_xfrm_monitor(int argc, char **argv)
        if (listen_all_nsid && rtnl_listen_all_nsid(&rth) < 0)
                exit(1);
 
-       if (rtnl_listen(&rth, xfrm_accept_msg, (void*)stdout) < 0)
+       if (rtnl_listen(&rth, xfrm_accept_msg, (void *)stdout) < 0)
                exit(2);
 
        return 0;