* 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
+ * along with this program; if not, see <http://www.gnu.org/licenses>.
*/
/*
* based on ip.c, iproute.c
* Masahide NAKAMURA @USAGI
*/
+#include <alloca.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
static void usage(void)
{
fprintf(stderr,
- "Usage: ip xfrm XFRM_OBJECT { COMMAND | help }\n"
- "where XFRM_OBJECT := { state | policy | monitor }\n");
+ "Usage: ip xfrm XFRM-OBJECT { COMMAND | help }\n"
+ "where XFRM-OBJECT := state | policy | monitor\n");
exit(-1);
}
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 },
{ NULL, -1 }
};
for (i = 0; ; i++) {
const struct typeent *t = &xfrmproto_types[i];
+
if (!t->t_name || t->t_type == -1)
break;
const char *strxf_xfrmproto(__u8 proto)
{
+ static char str[16];
int i;
for (i = 0; ; i++) {
const struct typeent *t = &xfrmproto_types[i];
+
if (!t->t_name || t->t_type == -1)
break;
return t->t_name;
}
- return NULL;
+ sprintf(str, "%u", 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 }, { NULL, -1 }
+ { "comp", XFRMA_ALG_COMP }, { "aead", XFRMA_ALG_AEAD },
+ { "auth-trunc", XFRMA_ALG_AUTH_TRUNC },
+ { NULL, -1 }
};
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;
const char *strxf_algotype(int type)
{
+ static char str[32];
int i;
for (i = 0; ; i++) {
const struct typeent *t = &algo_types[i];
+
if (!t->t_name || t->t_type == -1)
break;
return t->t_name;
}
- return NULL;
+ sprintf(str, "%d", 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;
return str;
}
-const char *strxf_share(__u8 share)
+static const char *strxf_share(__u8 share)
{
static char str[32];
return p;
}
-void xfrm_id_info_print(xfrm_address_t *saddr, struct xfrm_id *id,
+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;
+}
+
+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)
- 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)));
+ fputs(title, fp);
+
+ 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)
- fprintf(fp, prefix);
+ fputs(prefix, fp);
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);
static const char *strxf_limit(__u64 limit)
{
static char str[32];
+
if (limit == XFRM_INF)
strcpy(str, "(INF)");
else
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)
- fprintf(fp, prefix);
- fprintf(fp, "stats:");
- fprintf(fp, "%s", _SL_);
+ fputs(prefix, fp);
+ fprintf(fp, "stats:%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_);
+ fputs(prefix, fp);
+ fprintf(fp, " replay-window %u replay %u failed %u%s",
+ s->replay_window, s->replay, s->integrity_failed, _SL_);
}
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)
- fprintf(fp, prefix);
- fprintf(fp, "lifetime config:");
- fprintf(fp, "%s", _SL_);
+ fputs(prefix, fp);
+ fprintf(fp, "lifetime config:%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_);
+ fputs(prefix, fp);
+ fprintf(fp, " limit: soft %s(bytes),",
+ strxf_limit(cfg->soft_byte_limit));
+ fprintf(fp, " hard %s(bytes)%s",
+ strxf_limit(cfg->hard_byte_limit), _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_);
+ fputs(prefix, fp);
+ fprintf(fp, " limit: soft %s(packets),",
+ strxf_limit(cfg->soft_packet_limit));
+ fprintf(fp, " hard %s(packets)%s",
+ strxf_limit(cfg->hard_packet_limit), _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_);
+ fputs(prefix, fp);
+ fprintf(fp, " expire add: soft %llu(sec), hard %llu(sec)%s",
+ (unsigned long long) cfg->soft_add_expires_seconds,
+ (unsigned long long) cfg->hard_add_expires_seconds,
+ _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_);
+ fputs(prefix, fp);
+ fprintf(fp, " expire use: soft %llu(sec), hard %llu(sec)%s",
+ (unsigned long long) cfg->soft_use_expires_seconds,
+ (unsigned long long) cfg->hard_use_expires_seconds,
+ _SL_);
}
if (cur) {
if (prefix)
- fprintf(fp, prefix);
- fprintf(fp, "lifetime current:");
- fprintf(fp, "%s", _SL_);
+ fputs(prefix, fp);
+ fprintf(fp, "lifetime current:%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_);
+ fputs(prefix, fp);
+ fprintf(fp, " %llu(bytes), %llu(packets)%s",
+ (unsigned long long) cur->bytes,
+ (unsigned long long) cur->packets,
+ _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_);
+ fputs(prefix, fp);
+ fprintf(fp, " add %s ", strxf_time(cur->add_time));
+ fprintf(fp, "use %s%s", strxf_time(cur->use_time), _SL_);
}
}
void xfrm_selector_print(struct xfrm_selector *sel, __u16 family,
FILE *fp, const char *prefix)
{
- char abuf[256];
__u16 f;
f = sel->family;
f = preferred_family;
if (prefix)
- fprintf(fp, prefix);
+ fputs(prefix, fp);
- memset(abuf, '\0', sizeof(abuf));
- fprintf(fp, "src %s/%u ", rt_addr_n2a(f, sizeof(sel->saddr),
- &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, sizeof(sel->daddr),
- &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)
if (sel->dport_mask)
fprintf(fp, "code %u ", ntohs(sel->dport));
break;
+ case IPPROTO_GRE:
+ if (sel->sport_mask || sel->dport_mask)
+ fprintf(fp, "key %u ",
+ (((__u32)ntohs(sel->sport)) << 16) +
+ ntohs(sel->dport));
+ break;
case IPPROTO_MH:
if (sel->sport_mask)
fprintf(fp, "type %u ", ntohs(sel->sport));
fprintf(fp, "%s", _SL_);
}
-static void xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
- FILE *fp, const char *prefix)
+static void __xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
+ FILE *fp, const char *prefix, int newline)
{
int keylen;
int i;
if (prefix)
- fprintf(fp, prefix);
+ fputs(prefix, fp);
fprintf(fp, "%s ", strxf_algotype(type));
goto fin;
}
- fprintf(fp, "0x");
- for (i = 0; i < keylen; i ++)
- fprintf(fp, "%.2x", (unsigned char)algo->alg_key[i]);
+ if (keylen > 0) {
+ 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);
+ if (show_stats > 0)
+ fprintf(fp, " (%d bits)", algo->alg_key_len);
+ }
fin:
+ if (newline)
+ fprintf(fp, "%s", _SL_);
+}
+
+static inline void xfrm_algo_print(struct xfrm_algo *algo, int type, int len,
+ FILE *fp, const char *prefix)
+{
+ return __xfrm_algo_print(algo, type, len, fp, prefix, 1);
+}
+
+static void xfrm_aead_print(struct xfrm_algo_aead *algo, int len,
+ FILE *fp, const char *prefix)
+{
+ struct xfrm_algo *base_algo = alloca(sizeof(*base_algo) + algo->alg_key_len / 8);
+
+ memcpy(base_algo->alg_name, algo->alg_name, sizeof(base_algo->alg_name));
+ 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);
+
+ fprintf(fp, " %d", algo->alg_icv_len);
+
+ fprintf(fp, "%s", _SL_);
+}
+
+static void xfrm_auth_trunc_print(struct xfrm_algo_auth *algo, int len,
+ FILE *fp, const char *prefix)
+{
+ struct xfrm_algo *base_algo = alloca(sizeof(*base_algo) + algo->alg_key_len / 8);
+
+ memcpy(base_algo->alg_name, algo->alg_name, sizeof(base_algo->alg_name));
+ 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);
+
+ fprintf(fp, " %d", algo->alg_trunc_len);
+
fprintf(fp, "%s", _SL_);
}
static void xfrm_tmpl_print(struct xfrm_user_tmpl *tmpls, int len,
- __u16 family, FILE *fp, const char *prefix)
+ FILE *fp, const char *prefix)
{
int ntmpls = len / sizeof(struct xfrm_user_tmpl);
int i;
if (ntmpls <= 0) {
if (prefix)
- fprintf(fp, prefix);
+ fputs(prefix, fp);
fprintf(fp, "(ERROR \"tmpl\" truncated)");
fprintf(fp, "%s", _SL_);
return;
struct xfrm_user_tmpl *tmpl = &tmpls[i];
if (prefix)
- fprintf(fp, prefix);
+ fputs(prefix, fp);
xfrm_id_info_print(&tmpl->saddr, &tmpl->id, tmpl->mode,
- tmpl->reqid, family, 0, fp, prefix, "tmpl ");
+ tmpl->reqid, tmpl->family, 0, fp, prefix, "tmpl ");
if (show_stats > 0 || tmpl->optional) {
if (prefix)
- fprintf(fp, prefix);
+ fputs(prefix, fp);
fprintf(fp, "\t");
switch (tmpl->optional) {
case 0:
if (show_stats > 0) {
if (prefix)
- fprintf(fp, prefix);
+ fputs(prefix, fp);
fprintf(fp, "\t");
fprintf(fp, "%s-mask %s ",
strxf_algotype(XFRMA_ALG_CRYPT),
}
}
+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;
+ char **argv = *argvp;
+
+ NEXT_ARG();
+ if (get_u32(&mark->v, *argv, 0)) {
+ invarg("MARK value is invalid\n", *argv);
+ }
+ if (argc > 1)
+ NEXT_ARG();
+ else { /* last entry on parse line */
+ mark->m = 0xffffffff;
+ goto done;
+ }
+
+ if (strcmp(*argv, "mask") == 0) {
+ NEXT_ARG();
+ if (get_u32(&mark->m, *argv, 0)) {
+ invarg("MASK value is invalid\n", *argv);
+ }
+ } else {
+ mark->m = 0xffffffff;
+ PREV_ARG();
+ }
+
+done:
+ *argcp = argc;
+ *argvp = argv;
+
+ return 0;
+}
+
void xfrm_xfrma_print(struct rtattr *tb[], __u16 family,
FILE *fp, const char *prefix)
{
- if (tb[XFRMA_ALG_AUTH]) {
+ if (tb[XFRMA_MARK]) {
+ struct rtattr *rta = tb[XFRMA_MARK];
+ 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),
+
+ xfrm_algo_print(RTA_DATA(rta),
XFRMA_ALG_AUTH, RTA_PAYLOAD(rta), fp, prefix);
}
+ if (tb[XFRMA_ALG_AUTH_TRUNC]) {
+ struct rtattr *rta = tb[XFRMA_ALG_AUTH_TRUNC];
+
+ xfrm_auth_trunc_print(RTA_DATA(rta),
+ RTA_PAYLOAD(rta), fp, prefix);
+ }
+
+ if (tb[XFRMA_ALG_AEAD]) {
+ struct rtattr *rta = tb[XFRMA_ALG_AEAD];
+
+ xfrm_aead_print(RTA_DATA(rta),
+ 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),
+
+ xfrm_algo_print(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),
+
+ xfrm_algo_print(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);
+ fputs(prefix, fp);
fprintf(fp, "encap ");
if (RTA_PAYLOAD(tb[XFRMA_ENCAP]) < sizeof(*e)) {
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) {
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, sizeof(e->encap_oa),
- &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),
- RTA_PAYLOAD(rta), family, fp, prefix);
+
+ 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)
- fprintf(fp, 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, sizeof(*coa), coa,
- abuf, sizeof(abuf)));
+ rt_addr_n2a(family, sizeof(*coa), coa));
fprintf(fp, "%s", _SL_);
}
__u64 lastused;
if (prefix)
- fprintf(fp, prefix);
+ fputs(prefix, fp);
fprintf(fp, "lastused ");
if (RTA_PAYLOAD(tb[XFRMA_LASTUSED]) < sizeof(lastused)) {
return;
}
- lastused = *(__u64 *)RTA_DATA(tb[XFRMA_LASTUSED]);
+ lastused = rta_getattr_u64(tb[XFRMA_LASTUSED]);
fprintf(fp, "%s", strxf_time(lastused));
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);
}
struct rtattr *tb[], FILE *fp, const char *prefix,
const char *title)
{
- 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));
- fprintf(fp, buf);
+ fputs(buf, fp);
fprintf(fp, "replay-window %u ", xsinfo->replay_window);
if (show_stats > 0)
fprintf(fp, "seq 0x%08u ", xsinfo->seq);
XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_DECAP_DSCP, "decap-dscp");
XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_NOPMTUDISC, "nopmtudisc");
XFRM_FLAG_PRINT(fp, flags, XFRM_STATE_WILDRECV, "wildrecv");
+ 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 = rta_getattr_u32(tb[XFRMA_SA_EXTRA_FLAGS]);
+
+ fprintf(fp, "extra_flag ");
+ XFRM_FLAG_PRINT(fp, extra_flags,
+ XFRM_SA_XFLAG_DONT_ENCAP_DSCP,
+ "dont-encap-dscp");
+ if (extra_flags)
+ fprintf(fp, "%x", extra_flags);
+ }
if (show_stats > 0)
fprintf(fp, " (0x%s)", strxf_mask8(xsinfo->flags));
fprintf(fp, "%s", _SL_);
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);
}
xfrm_lifetime_print(&xsinfo->lft, &xsinfo->curlft, fp, buf);
xfrm_stats_print(&xsinfo->stats, fp, buf);
}
+
+ if (tb[XFRMA_SEC_CTX]) {
+ struct xfrm_user_sec_ctx *sctx;
+
+ fprintf(fp, "\tsecurity context ");
+
+ if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx))
+ fprintf(fp, "(ERROR truncated)");
+
+ sctx = RTA_DATA(tb[XFRMA_SEC_CTX]);
+
+ fprintf(fp, "%s %s", (char *)(sctx + 1), _SL_);
+ }
+
}
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];
- __u8 ptype = XFRM_POLICY_TYPE_MAIN;
-
- memset(buf, '\0', sizeof(buf));
+ char buf[STRBUF_SIZE] = {};
xfrm_selector_print(&xpinfo->sel, preferred_family, fp, title);
+ if (tb[XFRMA_SEC_CTX]) {
+ struct xfrm_user_sec_ctx *sctx;
+
+ fprintf(fp, "\tsecurity context ");
+
+ if (RTA_PAYLOAD(tb[XFRMA_SEC_CTX]) < sizeof(*sctx))
+ fprintf(fp, "(ERROR truncated)");
+
+ 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) {
+ xpinfo->dir -= XFRM_POLICY_MAX;
+ fprintf(fp, "socket ");
+ } else
+ fprintf(fp, "dir ");
- fprintf(fp, buf);
- fprintf(fp, "dir ");
switch (xpinfo->dir) {
case XFRM_POLICY_IN:
fprintf(fp, "in");
fprintf(fp, "index %u ", xpinfo->index);
fprintf(fp, "priority %u ", xpinfo->priority);
- fprintf(fp, "ptype ");
-
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]);
- 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;
+ upt = RTA_DATA(tb[XFRMA_POLICY_TYPE]);
+ fprintf(fp, "%s ", strxf_ptype(upt->type));
}
- fprintf(fp, " ");
if (show_stats > 0)
fprintf(fp, "share %s ", strxf_share(xpinfo->share));
fprintf(fp, "flag ");
XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_LOCALOK, "localok");
+ XFRM_FLAG_PRINT(fp, flags, XFRM_POLICY_ICMP, "icmp");
if (flags)
fprintf(fp, "%x", flags);
}
{
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) {
get_prefix(&src, *argv, preferred_family);
if (src.family == AF_UNSPEC)
- invarg("\"src\" address family is AF_UNSPEC", *argv);
+ invarg("value after \"src\" has an unrecognized address family", *argv);
if (family)
*family = src.family;
get_prefix(&dst, *argv, preferred_family);
if (dst.family == AF_UNSPEC)
- invarg("\"dst\" address family is AF_UNSPEC", *argv);
+ invarg("value after \"dst\" has an unrecognized address family", *argv);
if (family)
*family = dst.family;
ret = xfrm_xfrmproto_getbyname(*argv);
if (ret < 0)
- invarg("\"XFRM_PROTO\" is invalid", *argv);
+ invarg("XFRM-PROTO value is invalid", *argv);
id->proto = (__u8)ret;
filter.id_proto_mask = XFRM_FILTER_MASK_FULL;
} else if (strcmp(*argv, "spi") == 0) {
- __u32 spi;
-
NEXT_ARG();
- if (get_u32(&spi, *argv, 0))
- invarg("\"SPI\" is invalid", *argv);
-
- spi = htonl(spi);
- id->spi = spi;
+ if (get_be32(&id->spi, *argv, 0))
+ invarg("SPI value is invalid", *argv);
filter.id_spi_mask = XFRM_FILTER_MASK_FULL;
}
if (src.family && dst.family && (src.family != dst.family))
- invarg("the same address family is required between \"src\" and \"dst\"", *argv);
+ invarg("the same address family is required between values after \"src\" and \"dst\"", *argv);
+
+ 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));
+ 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));
+ exit(1);
+ }
+ }
if (loose == 0 && id->proto == 0)
- missarg("XFRM_PROTO");
+ missarg("XFRM-PROTO");
if (argc == *argcp)
missarg("ID");
else if (matches(*argv, "beet") == 0)
*mode = XFRM_MODE_BEET;
else
- invarg("\"MODE\" is invalid", *argv);
+ invarg("MODE value is invalid", *argv);
*argcp = argc;
*argvp = argv;
else if (strcmp(*argv, "espinudp") == 0)
*type = 2;
else
- invarg("\"ENCAP-TYPE\" is invalid", *argv);
+ invarg("ENCAP-TYPE value is invalid", *argv);
*argcp = argc;
*argvp = argv;
char **argv = *argvp;
if (get_u32(reqid, *argv, 0))
- invarg("\"REQID\" is invalid", *argv);
+ invarg("REQID value is invalid", *argv);
*argcp = argc;
*argvp = argv;
char *dportp = NULL;
char *typep = NULL;
char *codep = NULL;
+ char *grekey = NULL;
while (1) {
if (strcmp(*argv, "proto") == 0) {
upspec = 0;
else {
struct protoent *pp;
+
pp = getprotobyname(*argv);
if (pp)
upspec = pp->p_proto;
else {
if (get_u8(&upspec, *argv, 0))
- invarg("\"PROTO\" is invalid", *argv);
+ invarg("PROTO value is invalid", *argv);
}
}
sel->proto = upspec;
NEXT_ARG();
- if (get_u16(&sel->sport, *argv, 0))
- invarg("\"PORT\" is invalid", *argv);
- sel->sport = htons(sel->sport);
+ if (get_be16(&sel->sport, *argv, 0))
+ invarg("value after \"sport\" is invalid", *argv);
if (sel->sport)
sel->sport_mask = ~((__u16)0);
NEXT_ARG();
- if (get_u16(&sel->dport, *argv, 0))
- invarg("\"PORT\" is invalid", *argv);
- sel->dport = htons(sel->dport);
+ if (get_be16(&sel->dport, *argv, 0))
+ invarg("value after \"dport\" is invalid", *argv);
if (sel->dport)
sel->dport_mask = ~((__u16)0);
if (get_u16(&sel->sport, *argv, 0) ||
(sel->sport & ~((__u16)0xff)))
- invarg("\"type\" value is invalid", *argv);
+ invarg("value after \"type\" is invalid", *argv);
sel->sport = htons(sel->sport);
sel->sport_mask = ~((__u16)0);
if (get_u16(&sel->dport, *argv, 0) ||
(sel->dport & ~((__u16)0xff)))
- invarg("\"code\" value is invalid", *argv);
+ invarg("value after \"code\" is invalid", *argv);
sel->dport = htons(sel->dport);
sel->dport_mask = ~((__u16)0);
filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
+ } else if (strcmp(*argv, "key") == 0) {
+ unsigned int uval;
+
+ grekey = *argv;
+
+ NEXT_ARG();
+
+ if (strchr(*argv, '.'))
+ uval = htonl(get_addr32(*argv));
+ else {
+ if (get_unsigned(&uval, *argv, 0) < 0) {
+ fprintf(stderr, "value after \"key\" is invalid\n");
+ exit(-1);
+ }
+ }
+
+ sel->sport = htons(uval >> 16);
+ sel->dport = htons(uval & 0xffff);
+ sel->sport_mask = ~((__u16)0);
+ sel->dport_mask = ~((__u16)0);
+
+ filter.upspec_dport_mask = XFRM_FILTER_MASK_FULL;
+
} else {
PREV_ARG(); /* back track */
break;
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=%s\n", strxf_proto(sel->proto));
+ fprintf(stderr, "\"sport\" and \"dport\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto));
exit(1);
}
}
case IPPROTO_MH:
break;
default:
- fprintf(stderr, "\"type\" and \"code\" are invalid with proto=%s\n", strxf_proto(sel->proto));
+ fprintf(stderr, "\"type\" and \"code\" are invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto));
+ exit(1);
+ }
+ }
+ if (grekey) {
+ switch (sel->proto) {
+ case IPPROTO_GRE:
+ break;
+ default:
+ fprintf(stderr, "\"key\" is invalid with PROTO value \"%s\"\n", strxf_proto(sel->proto));
exit(1);
}
}
{
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();
get_prefix(&src, *argv, preferred_family);
if (src.family == AF_UNSPEC)
- invarg("\"src\" address family is AF_UNSPEC", *argv);
+ invarg("value after \"src\" has an unrecognized address family", *argv);
sel->family = src.family;
memcpy(&sel->saddr, &src.data, sizeof(sel->saddr));
get_prefix(&dst, *argv, preferred_family);
if (dst.family == AF_UNSPEC)
- invarg("\"dst\" address family is AF_UNSPEC", *argv);
+ invarg("value after \"dst\" has an unrecognized address family", *argv);
sel->family = dst.family;
memcpy(&sel->daddr, &dst.data, sizeof(sel->daddr));
else {
ifindex = ll_name_to_index(*argv);
if (ifindex <= 0)
- invarg("\"DEV\" is invalid", *argv);
+ invarg("DEV value is invalid", *argv);
}
sel->ifindex = ifindex;
}
if (src.family && dst.family && (src.family != dst.family))
- invarg("the same address family is required between \"src\" and \"dst\"", *argv);
+ invarg("the same address family is required between values after \"src\" and \"dst\"", *argv);
if (argc == *argcp)
missarg("SELECTOR");
NEXT_ARG();
ret = get_u64(&lft->soft_add_expires_seconds, *argv, 0);
if (ret)
- invarg("\"time-soft\" value is invalid", *argv);
+ invarg("value after \"time-soft\" is invalid", *argv);
} else if (strcmp(*argv, "time-hard") == 0) {
NEXT_ARG();
ret = get_u64(&lft->hard_add_expires_seconds, *argv, 0);
if (ret)
- invarg("\"time-hard\" value is invalid", *argv);
+ invarg("value after \"time-hard\" is invalid", *argv);
} else if (strcmp(*argv, "time-use-soft") == 0) {
NEXT_ARG();
ret = get_u64(&lft->soft_use_expires_seconds, *argv, 0);
if (ret)
- invarg("\"time-use-soft\" value is invalid", *argv);
+ invarg("value after \"time-use-soft\" is invalid", *argv);
} else if (strcmp(*argv, "time-use-hard") == 0) {
NEXT_ARG();
ret = get_u64(&lft->hard_use_expires_seconds, *argv, 0);
if (ret)
- invarg("\"time-use-hard\" value is invalid", *argv);
+ invarg("value after \"time-use-hard\" is invalid", *argv);
} else if (strcmp(*argv, "byte-soft") == 0) {
NEXT_ARG();
ret = get_u64(&lft->soft_byte_limit, *argv, 0);
if (ret)
- invarg("\"byte-soft\" value is invalid", *argv);
+ invarg("value after \"byte-soft\" is invalid", *argv);
} else if (strcmp(*argv, "byte-hard") == 0) {
NEXT_ARG();
ret = get_u64(&lft->hard_byte_limit, *argv, 0);
if (ret)
- invarg("\"byte-hard\" value is invalid", *argv);
+ invarg("value after \"byte-hard\" is invalid", *argv);
} else if (strcmp(*argv, "packet-soft") == 0) {
NEXT_ARG();
ret = get_u64(&lft->soft_packet_limit, *argv, 0);
if (ret)
- invarg("\"packet-soft\" value is invalid", *argv);
+ invarg("value after \"packet-soft\" is invalid", *argv);
} else if (strcmp(*argv, "packet-hard") == 0) {
NEXT_ARG();
ret = get_u64(&lft->hard_packet_limit, *argv, 0);
if (ret)
- invarg("\"packet-hard\" value is invalid", *argv);
+ invarg("value after \"packet-hard\" is invalid", *argv);
} else
- invarg("\"LIMIT\" is invalid", *argv);
+ invarg("LIMIT value is invalid", *argv);
*argcp = argc;
*argvp = argv;