]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - ip/ipxfrm.c
bridge: fdb: add support for src_vni option
[mirror_iproute2.git] / ip / ipxfrm.c
index f5f78ca6b96873ff9ce2ebd516123ead4fad1b7b..b153b863569bb813cd1b44a21a9a6b365ee254f2 100644 (file)
 #include <netdb.h>
 #include <linux/netlink.h>
 #include <linux/rtnetlink.h>
-#include <linux/xfrm.h>
 
 #include "utils.h"
 #include "xfrm.h"
 #include "ip_common.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;
 
@@ -112,7 +100,7 @@ struct typeent {
        int t_type;
 };
 
-static const struct typeent xfrmproto_types[]= {
+static const struct typeent xfrmproto_types[] = {
        { "esp", IPPROTO_ESP }, { "ah", IPPROTO_AH }, { "comp", IPPROTO_COMP },
        { "route2", IPPROTO_ROUTING }, { "hao", IPPROTO_DSTOPTS },
        { "ipsec-any", IPSEC_PROTO_ANY },
@@ -125,6 +113,7 @@ int xfrm_xfrmproto_getbyname(char *name)
 
        for (i = 0; ; i++) {
                const struct typeent *t = &xfrmproto_types[i];
+
                if (!t->t_name || t->t_type == -1)
                        break;
 
@@ -142,6 +131,7 @@ const char *strxf_xfrmproto(__u8 proto)
 
        for (i = 0; ; i++) {
                const struct typeent *t = &xfrmproto_types[i];
+
                if (!t->t_name || t->t_type == -1)
                        break;
 
@@ -153,7 +143,7 @@ const char *strxf_xfrmproto(__u8 proto)
        return str;
 }
 
-static const struct typeent algo_types[]= {
+static const struct typeent algo_types[] = {
        { "enc", XFRMA_ALG_CRYPT }, { "auth", XFRMA_ALG_AUTH },
        { "comp", XFRMA_ALG_COMP }, { "aead", XFRMA_ALG_AEAD },
        { "auth-trunc", XFRMA_ALG_AUTH_TRUNC },
@@ -166,6 +156,7 @@ int xfrm_algotype_getbyname(char *name)
 
        for (i = 0; ; i++) {
                const struct typeent *t = &algo_types[i];
+
                if (!t->t_name || t->t_type == -1)
                        break;
 
@@ -183,6 +174,7 @@ const char *strxf_algotype(int type)
 
        for (i = 0; ; i++) {
                const struct typeent *t = &algo_types[i];
+
                if (!t->t_name || t->t_type == -1)
                        break;
 
@@ -194,7 +186,7 @@ const char *strxf_algotype(int type)
        return str;
 }
 
-const char *strxf_mask8(__u8 mask)
+static const char *strxf_mask8(__u8 mask)
 {
        static char str[16];
        const int sn = sizeof(mask) * 8 - 1;
@@ -217,7 +209,7 @@ const char *strxf_mask32(__u32 mask)
        return str;
 }
 
-const char *strxf_share(__u8 share)
+static const char *strxf_share(__u8 share)
 {
        static char str[32];
 
@@ -278,21 +270,15 @@ const char *strxf_ptype(__u8 ptype)
        return str;
 }
 
-void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id,
+static 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)
                fputs(title, fp);
 
-       memset(abuf, '\0', sizeof(abuf));
-       fprintf(fp, "src %s ", rt_addr_n2a(family,
-                                          saddr, abuf, sizeof(abuf)));
-       memset(abuf, '\0', sizeof(abuf));
-       fprintf(fp, "dst %s", rt_addr_n2a(family,
-                                         &id->daddr, abuf, sizeof(abuf)));
+       fprintf(fp, "src %s ", rt_addr_n2a(family, sizeof(*saddr), saddr));
+       fprintf(fp, "dst %s", rt_addr_n2a(family, sizeof(id->daddr), &id->daddr));
        fprintf(fp, "%s", _SL_);
 
        if (prefix)
@@ -303,6 +289,7 @@ void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id,
 
        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);
@@ -341,6 +328,7 @@ void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id,
 static const char *strxf_limit(__u64 limit)
 {
        static char str[32];
+
        if (limit == XFRM_INF)
                strcpy(str, "(INF)");
        else
@@ -349,7 +337,8 @@ static const char *strxf_limit(__u64 limit)
        return str;
 }
 
-void xfrm_stats_print(struct xfrm_stats *s, FILE *fp, const char *prefix)
+static void xfrm_stats_print(struct xfrm_stats *s, FILE *fp,
+                            const char *prefix)
 {
        if (prefix)
                fputs(prefix, fp);
@@ -383,14 +372,14 @@ static const char *strxf_time(__u64 time)
        return str;
 }
 
-void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg,
+static void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg,
                         struct xfrm_lifetime_cur *cur,
                         FILE *fp, const char *prefix)
 {
        if (cfg) {
                if (prefix)
                        fputs(prefix, fp);
-               fprintf(fp, "lifetime config:%s",_SL_);
+               fprintf(fp, "lifetime config:%s", _SL_);
 
                if (prefix)
                        fputs(prefix, fp);
@@ -442,7 +431,6 @@ void xfrm_lifetime_print(struct xfrm_lifetime_cfg *cfg,
 void xfrm_selector_print(struct xfrm_selector *sel, __u16 family,
                         FILE *fp, const char *prefix)
 {
-       char abuf[256];
        __u16 f;
 
        f = sel->family;
@@ -454,12 +442,12 @@ void xfrm_selector_print(struct xfrm_selector *sel, __u16 family,
        if (prefix)
                fputs(prefix, fp);
 
-       memset(abuf, '\0', sizeof(abuf));
-       fprintf(fp, "src %s/%u ", rt_addr_n2a(f, &sel->saddr, abuf, sizeof(abuf)),
+       fprintf(fp, "src %s/%u ",
+               rt_addr_n2a(f, sizeof(sel->saddr), &sel->saddr),
                sel->prefixlen_s);
 
-       memset(abuf, '\0', sizeof(abuf));
-       fprintf(fp, "dst %s/%u ", rt_addr_n2a(f, &sel->daddr, abuf, sizeof(abuf)),
+       fprintf(fp, "dst %s/%u ",
+               rt_addr_n2a(f, sizeof(sel->daddr), &sel->daddr),
                sel->prefixlen_d);
 
        if (sel->proto)
@@ -509,7 +497,8 @@ void xfrm_selector_print(struct xfrm_selector *sel, __u16 family,
 }
 
 static void __xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
-                             FILE *fp, const char *prefix, int newline)
+                             FILE *fp, const char *prefix, int newline,
+                             bool nokeys)
 {
        int keylen;
        int i;
@@ -533,9 +522,11 @@ static void __xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
                goto fin;
        }
 
-       if (keylen > 0) {
+       if (nokeys)
+               fprintf(fp, "<<Keys hidden>>");
+       else if (keylen > 0) {
                fprintf(fp, "0x");
-               for (i = 0; i < keylen; i ++)
+               for (i = 0; i < keylen; i++)
                        fprintf(fp, "%.2x", (unsigned char)algo->alg_key[i]);
 
                if (show_stats > 0)
@@ -548,13 +539,13 @@ static void __xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
 }
 
 static inline void xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
-                                  FILE *fp, const char *prefix)
+                                  FILE *fp, const char *prefix, bool nokeys)
 {
-       return __xfrm_algo_print(algo, type, len, fp, prefix, 1);
+       return __xfrm_algo_print(algo, type, len, fp, prefix, 1, nokeys);
 }
 
 static void xfrm_aead_print(struct xfrm_algo_aead *algo, int len,
-                           FILE *fp, const char *prefix)
+                           FILE *fp, const char *prefix, bool nokeys)
 {
        struct xfrm_algo *base_algo = alloca(sizeof(*base_algo) + algo->alg_key_len / 8);
 
@@ -562,7 +553,8 @@ static void xfrm_aead_print(struct xfrm_algo_aead *algo, int len,
        base_algo->alg_key_len = algo->alg_key_len;
        memcpy(base_algo->alg_key, algo->alg_key, algo->alg_key_len / 8);
 
-       __xfrm_algo_print(base_algo, XFRMA_ALG_AEAD, len, fp, prefix, 0);
+       __xfrm_algo_print(base_algo, XFRMA_ALG_AEAD, len, fp, prefix, 0,
+                         nokeys);
 
        fprintf(fp, " %d", algo->alg_icv_len);
 
@@ -570,7 +562,7 @@ static void xfrm_aead_print(struct xfrm_algo_aead *algo, int len,
 }
 
 static void xfrm_auth_trunc_print(struct xfrm_algo_auth *algo, int len,
-                                 FILE *fp, const char *prefix)
+                                 FILE *fp, const char *prefix, bool nokeys)
 {
        struct xfrm_algo *base_algo = alloca(sizeof(*base_algo) + algo->alg_key_len / 8);
 
@@ -578,7 +570,8 @@ static void xfrm_auth_trunc_print(struct xfrm_algo_auth *algo, int len,
        base_algo->alg_key_len = algo->alg_key_len;
        memcpy(base_algo->alg_key, algo->alg_key, algo->alg_key_len / 8);
 
-       __xfrm_algo_print(base_algo, XFRMA_ALG_AUTH_TRUNC, len, fp, prefix, 0);
+       __xfrm_algo_print(base_algo, XFRMA_ALG_AUTH_TRUNC, len, fp, prefix, 0,
+                         nokeys);
 
        fprintf(fp, " %d", algo->alg_trunc_len);
 
@@ -650,6 +643,13 @@ static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int len,
        }
 }
 
+static void xfrm_output_mark_print(struct rtattr *tb[], FILE *fp)
+{
+       __u32 output_mark = rta_getattr_u32(tb[XFRMA_OUTPUT_MARK]);
+
+       fprintf(fp, "output-mark 0x%x", output_mark);
+}
+
 int xfrm_parse_mark(struct xfrm_mark *mark, int *argcp, char ***argvp)
 {
        int argc = *argcp;
@@ -684,47 +684,61 @@ done:
 }
 
 void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
-                     FILE *fp, const char *prefix)
+                     FILE *fp, const char *prefix, bool nokeys)
 {
        if (tb[XFRMA_MARK]) {
                struct rtattr *rta = tb[XFRMA_MARK];
-               struct xfrm_mark *m = (struct xfrm_mark *) RTA_DATA(rta);
-               fprintf(fp, "\tmark %d/0x%x\n", m->v, m->m);
+               struct xfrm_mark *m = RTA_DATA(rta);
+
+               fprintf(fp, "\tmark %#x/%#x ", m->v, m->m);
+
+               if (tb[XFRMA_OUTPUT_MARK])
+                       xfrm_output_mark_print(tb, fp);
+               fprintf(fp, "%s", _SL_);
+       } else if (tb[XFRMA_OUTPUT_MARK]) {
+               fprintf(fp, "\t");
+
+               xfrm_output_mark_print(tb, fp);
+               fprintf(fp, "%s", _SL_);
        }
 
        if (tb[XFRMA_ALG_AUTH] && !tb[XFRMA_ALG_AUTH_TRUNC]) {
                struct rtattr *rta = tb[XFRMA_ALG_AUTH];
-               xfrm_algo_print((struct xfrm_algo *) RTA_DATA(rta),
-                               XFRMA_ALG_AUTH, RTA_PAYLOAD(rta), fp, prefix);
+
+               xfrm_algo_print(RTA_DATA(rta), XFRMA_ALG_AUTH, RTA_PAYLOAD(rta),
+                               fp, prefix, nokeys);
        }
 
        if (tb[XFRMA_ALG_AUTH_TRUNC]) {
                struct rtattr *rta = tb[XFRMA_ALG_AUTH_TRUNC];
-               xfrm_auth_trunc_print((struct xfrm_algo_auth *) RTA_DATA(rta),
-                                     RTA_PAYLOAD(rta), fp, prefix);
+
+               xfrm_auth_trunc_print(RTA_DATA(rta), RTA_PAYLOAD(rta), fp,
+                                     prefix, nokeys);
        }
 
        if (tb[XFRMA_ALG_AEAD]) {
                struct rtattr *rta = tb[XFRMA_ALG_AEAD];
-               xfrm_aead_print((struct xfrm_algo_aead *)RTA_DATA(rta),
-                               RTA_PAYLOAD(rta), fp, prefix);
+
+               xfrm_aead_print(RTA_DATA(rta), RTA_PAYLOAD(rta), fp, prefix,
+                               nokeys);
        }
 
        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);
+
+               xfrm_algo_print(RTA_DATA(rta), XFRMA_ALG_CRYPT,
+                               RTA_PAYLOAD(rta), fp, prefix, nokeys);
        }
 
        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);
+
+               xfrm_algo_print(RTA_DATA(rta), XFRMA_ALG_COMP, RTA_PAYLOAD(rta),
+                               fp, prefix, nokeys);
        }
 
        if (tb[XFRMA_ENCAP]) {
                struct xfrm_encap_tmpl *e;
-               char abuf[256];
 
                if (prefix)
                        fputs(prefix, fp);
@@ -735,7 +749,7 @@ void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
                        fprintf(fp, "%s", _SL_);
                        return;
                }
-               e = (struct xfrm_encap_tmpl *) RTA_DATA(tb[XFRMA_ENCAP]);
+               e = RTA_DATA(tb[XFRMA_ENCAP]);
 
                fprintf(fp, "type ");
                switch (e->encap_type) {
@@ -752,38 +766,34 @@ void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
                fprintf(fp, "sport %u ", ntohs(e->encap_sport));
                fprintf(fp, "dport %u ", ntohs(e->encap_dport));
 
-               memset(abuf, '\0', sizeof(abuf));
                fprintf(fp, "addr %s",
-                       rt_addr_n2a(family, &e->encap_oa, abuf, sizeof(abuf)));
+                       rt_addr_n2a(family, sizeof(e->encap_oa), &e->encap_oa));
                fprintf(fp, "%s", _SL_);
        }
 
        if (tb[XFRMA_TMPL]) {
                struct rtattr *rta = tb[XFRMA_TMPL];
-               xfrm_tmpl_print((struct xfrm_user_tmpl *) RTA_DATA(rta),
+
+               xfrm_tmpl_print(RTA_DATA(rta),
                                RTA_PAYLOAD(rta), fp, prefix);
        }
 
        if (tb[XFRMA_COADDR]) {
-               char abuf[256];
-               xfrm_address_t *coa;
+               const xfrm_address_t *coa;
 
                if (prefix)
                        fputs(prefix, fp);
                fprintf(fp, "coa ");
 
-               coa = (xfrm_address_t *)RTA_DATA(tb[XFRMA_COADDR]);
-
+               coa = RTA_DATA(tb[XFRMA_COADDR]);
                if (RTA_PAYLOAD(tb[XFRMA_COADDR]) < sizeof(*coa)) {
                        fprintf(fp, "(ERROR truncated)");
                        fprintf(fp, "%s", _SL_);
                        return;
                }
 
-               memset(abuf, '\0', sizeof(abuf));
                fprintf(fp, "%s",
-                       rt_addr_n2a(family, coa,
-                                   abuf, sizeof(abuf)));
+                       rt_addr_n2a(family, sizeof(*coa), coa));
                fprintf(fp, "%s", _SL_);
        }
 
@@ -806,33 +816,104 @@ void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
                fprintf(fp, "%s", _SL_);
        }
 
+       if (tb[XFRMA_REPLAY_VAL]) {
+               struct xfrm_replay_state *replay;
+
+               if (prefix)
+                       fputs(prefix, fp);
+               fprintf(fp, "anti-replay context: ");
+
+               if (RTA_PAYLOAD(tb[XFRMA_REPLAY_VAL]) < sizeof(*replay)) {
+                       fprintf(fp, "(ERROR truncated)");
+                       fprintf(fp, "%s", _SL_);
+                       return;
+               }
+
+               replay = RTA_DATA(tb[XFRMA_REPLAY_VAL]);
+               fprintf(fp, "seq 0x%x, oseq 0x%x, bitmap 0x%08x",
+                       replay->seq, replay->oseq, replay->bitmap);
+               fprintf(fp, "%s", _SL_);
+       }
+
+       if (tb[XFRMA_REPLAY_ESN_VAL]) {
+               struct xfrm_replay_state_esn *replay;
+               unsigned int i, j;
+
+               if (prefix)
+                       fputs(prefix, fp);
+               fprintf(fp, "anti-replay esn context:");
+
+               if (RTA_PAYLOAD(tb[XFRMA_REPLAY_ESN_VAL]) < sizeof(*replay)) {
+                       fprintf(fp, "(ERROR truncated)");
+                       fprintf(fp, "%s", _SL_);
+                       return;
+               }
+               fprintf(fp, "%s", _SL_);
+
+               replay = RTA_DATA(tb[XFRMA_REPLAY_ESN_VAL]);
+               if (prefix)
+                       fputs(prefix, fp);
+               fprintf(fp, " seq-hi 0x%x, seq 0x%x, oseq-hi 0x%0x, oseq 0x%0x",
+                       replay->seq_hi, replay->seq, replay->oseq_hi,
+                       replay->oseq);
+               fprintf(fp, "%s", _SL_);
+               if (prefix)
+                       fputs(prefix, fp);
+               fprintf(fp, " replay_window %u, bitmap-length %u",
+                       replay->replay_window, replay->bmp_len);
+               for (i = replay->bmp_len, j = 0; i; i--) {
+                       if (j++ % 8 == 0) {
+                               fprintf(fp, "%s", _SL_);
+                               if (prefix)
+                                       fputs(prefix, fp);
+                               fprintf(fp, " ");
+                       }
+                       fprintf(fp, "%08x ", replay->bmp[i - 1]);
+               }
+               fprintf(fp, "%s", _SL_);
+       }
+       if (tb[XFRMA_OFFLOAD_DEV]) {
+               struct xfrm_user_offload *xuo;
+
+               if (prefix)
+                       fputs(prefix, fp);
+               fprintf(fp, "crypto offload parameters: ");
+
+               if (RTA_PAYLOAD(tb[XFRMA_OFFLOAD_DEV]) < sizeof(*xuo)) {
+                       fprintf(fp, "(ERROR truncated)");
+                       fprintf(fp, "%s", _SL_);
+                       return;
+               }
+
+               xuo = (struct xfrm_user_offload *)
+                       RTA_DATA(tb[XFRMA_OFFLOAD_DEV]);
+               fprintf(fp, "dev %s dir %s", ll_index_to_name(xuo->ifindex),
+                       (xuo->flags & XFRM_OFFLOAD_INBOUND) ? "in" : "out");
+               fprintf(fp, "%s", _SL_);
+       }
 }
 
 static int xfrm_selector_iszero(struct xfrm_selector *s)
 {
-       struct xfrm_selector s0;
-
-       memset(&s0, 0, sizeof(s0));
+       struct xfrm_selector s0 = {};
 
        return (memcmp(&s0, s, sizeof(s0)) == 0);
 }
 
 void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo,
                            struct rtattr *tb[], FILE *fp, const char *prefix,
-                           const char *title)
+                           const char *title, bool nokeys)
 {
-       char buf[STRBUF_SIZE];
+       char buf[STRBUF_SIZE] = {};
        int force_spi = xfrm_xfrmproto_is_ipsec(xsinfo->id.proto);
 
-       memset(buf, '\0', sizeof(buf));
-
        xfrm_id_info_print(&xsinfo->saddr, &xsinfo->id, xsinfo->mode,
                           xsinfo->reqid, xsinfo->family, force_spi, fp,
                           prefix, title);
 
        if (prefix)
-               STRBUF_CAT(buf, prefix);
-       STRBUF_CAT(buf, "\t");
+               strlcat(buf, prefix, sizeof(buf));
+       strlcat(buf, "\t", sizeof(buf));
 
        fputs(buf, fp);
        fprintf(fp, "replay-window %u ", xsinfo->replay_window);
@@ -849,11 +930,12 @@ void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo,
                XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ICMP, "icmp");
                XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_AF_UNSPEC, "af-unspec");
                XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ALIGN4, "align4");
+               XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_ESN, "esn");
                if (flags)
                        fprintf(fp, "%x", flags);
        }
        if (show_stats > 0 && tb[XFRMA_SA_EXTRA_FLAGS]) {
-               __u32 extra_flags = *(__u32 *)RTA_DATA(tb[XFRMA_SA_EXTRA_FLAGS]);
+               __u32 extra_flags = rta_getattr_u32(tb[XFRMA_SA_EXTRA_FLAGS]);
 
                fprintf(fp, "extra_flag ");
                XFRM_FLAG_PRINT(fp, extra_flags,
@@ -866,13 +948,13 @@ void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo,
                fprintf(fp, " (0x%s)", strxf_mask8(xsinfo->flags));
        fprintf(fp, "%s", _SL_);
 
-       xfrm_xfrma_print(tb, xsinfo->family, fp, buf);
+       xfrm_xfrma_print(tb, xsinfo->family, fp, buf, nokeys);
 
        if (!xfrm_selector_iszero(&xsinfo->sel)) {
                char sbuf[STRBUF_SIZE];
 
                memcpy(sbuf, buf, sizeof(sbuf));
-               STRBUF_CAT(sbuf, "sel ");
+               strlcat(sbuf, "sel ", sizeof(sbuf));
 
                xfrm_selector_print(&xsinfo->sel, xsinfo->family, fp, sbuf);
        }
@@ -890,7 +972,7 @@ void xfrm_state_info_print(struct xfrm_usersa_info *xsinfo,
                if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx))
                        fprintf(fp, "(ERROR truncated)");
 
-               sctx = (struct xfrm_user_sec_ctx *)RTA_DATA(tb[XFRMA_SEC_CTX]);
+               sctx = RTA_DATA(tb[XFRMA_SEC_CTX]);
 
                fprintf(fp, "%s %s", (char *)(sctx + 1), _SL_);
        }
@@ -901,9 +983,7 @@ void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo,
                            struct rtattr *tb[], FILE *fp, const char *prefix,
                            const char *title)
 {
-       char buf[STRBUF_SIZE];
-
-       memset(buf, '\0', sizeof(buf));
+       char buf[STRBUF_SIZE] = {};
 
        xfrm_selector_print(&xpinfo->sel, preferred_family, fp, title);
 
@@ -915,15 +995,15 @@ void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo,
                if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx))
                        fprintf(fp, "(ERROR truncated)");
 
-               sctx = (struct xfrm_user_sec_ctx *)RTA_DATA(tb[XFRMA_SEC_CTX]);
+               sctx = RTA_DATA(tb[XFRMA_SEC_CTX]);
 
                fprintf(fp, "%s ", (char *)(sctx + 1));
                fprintf(fp, "%s", _SL_);
        }
 
        if (prefix)
-               STRBUF_CAT(buf, prefix);
-       STRBUF_CAT(buf, "\t");
+               strlcat(buf, prefix, sizeof(buf));
+       strlcat(buf, "\t", sizeof(buf));
 
        fputs(buf, fp);
        if (xpinfo->dir >= XFRM_POLICY_MAX) {
@@ -973,7 +1053,7 @@ void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo,
                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));
        }
 
@@ -996,7 +1076,7 @@ void xfrm_policy_info_print(struct xfrm_userpolicy_info *xpinfo,
        if (show_stats > 0)
                xfrm_lifetime_print(&xpinfo->lft, &xpinfo->curlft, fp, buf);
 
-       xfrm_xfrma_print(tb, xpinfo->sel.family, fp, buf);
+       xfrm_xfrma_print(tb, xpinfo->sel.family, fp, buf, false);
 }
 
 int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
@@ -1004,11 +1084,8 @@ int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
 {
        int argc = *argcp;
        char **argv = *argvp;
-       inet_prefix dst;
-       inet_prefix src;
-
-       memset(&dst, 0, sizeof(dst));
-       memset(&src, 0, sizeof(src));
+       inet_prefix dst = {};
+       inet_prefix src = {};
 
        while (1) {
                if (strcmp(*argv, "src") == 0) {
@@ -1051,15 +1128,10 @@ int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
                        filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
 
                } else if (strcmp(*argv, "spi") == 0) {
-                       __u32 spi;
-
                        NEXT_ARG();
-                       if (get_u32(&spi, *argv, 0))
+                       if (get_be32(&id->spi, *argv, 0))
                                invarg("SPI value is invalid", *argv);
 
-                       spi = htonl(spi);
-                       id->spi = spi;
-
                        filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
 
                } else {
@@ -1078,11 +1150,11 @@ int xfrm_id_parse(xfrm_address_t *saddr, struct xfrm_id *id, __u16 *family,
        if (id->spi && id->proto) {
                if (xfrm_xfrmproto_is_ro(id->proto)) {
                        fprintf(stderr, "\"spi\" is invalid with XFRM-PROTO value \"%s\"\n",
-                               strxf_xfrmproto(id->proto));
+                               strxf_xfrmproto(id->proto));
                        exit(1);
                } else if (id->proto == IPPROTO_COMP && ntohl(id->spi) >= 0x10000) {
                        fprintf(stderr, "SPI value is too large with XFRM-PROTO value \"%s\"\n",
-                               strxf_xfrmproto(id->proto));
+                               strxf_xfrmproto(id->proto));
                        exit(1);
                }
        }
@@ -1176,6 +1248,7 @@ static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
                                upspec = 0;
                        else {
                                struct protoent *pp;
+
                                pp = getprotobyname(*argv);
                                if (pp)
                                        upspec = pp->p_proto;
@@ -1193,9 +1266,8 @@ static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
 
                        NEXT_ARG();
 
-                       if (get_u16(&sel->sport, *argv, 0))
+                       if (get_be16(&sel->sport, *argv, 0))
                                invarg("value after \"sport\" is invalid", *argv);
-                       sel->sport = htons(sel->sport);
                        if (sel->sport)
                                sel->sport_mask = ~((__u16)0);
 
@@ -1206,9 +1278,8 @@ static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
 
                        NEXT_ARG();
 
-                       if (get_u16(&sel->dport, *argv, 0))
+                       if (get_be16(&sel->dport, *argv, 0))
                                invarg("value after \"dport\" is invalid", *argv);
-                       sel->dport = htons(sel->dport);
                        if (sel->dport)
                                sel->dport_mask = ~((__u16)0);
 
@@ -1242,7 +1313,7 @@ static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
                        filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
 
                } else if (strcmp(*argv, "key") == 0) {
-                       unsigned uval;
+                       unsigned int uval;
 
                        grekey = *argv;
 
@@ -1251,7 +1322,7 @@ static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
                        if (strchr(*argv, '.'))
                                uval = htonl(get_addr32(*argv));
                        else {
-                               if (get_unsigned(&uval, *argv, 0)<0) {
+                               if (get_unsigned(&uval, *argv, 0) < 0) {
                                        fprintf(stderr, "value after \"key\" is invalid\n");
                                        exit(-1);
                                }
@@ -1281,6 +1352,7 @@ static int xfrm_selector_upspec_parse(struct xfrm_selector *sel,
                case IPPROTO_UDP:
                case IPPROTO_SCTP:
                case IPPROTO_DCCP:
+               case IPPROTO_IP: /* to allow shared SA for different protocols */
                        break;
                default:
                        fprintf(stderr, "\"sport\" and \"dport\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto));
@@ -1318,13 +1390,10 @@ int xfrm_selector_parse(struct xfrm_selector *sel, int *argcp, char ***argvp)
 {
        int argc = *argcp;
        char **argv = *argvp;
-       inet_prefix dst;
-       inet_prefix src;
+       inet_prefix dst = {};
+       inet_prefix src = {};
        char *upspecp = NULL;
 
-       memset(&dst, 0, sizeof(dst));
-       memset(&src, 0, sizeof(src));
-
        while (1) {
                if (strcmp(*argv, "src") == 0) {
                        NEXT_ARG();