From patchwork Thu Aug 3 15:55:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Fortin X-Patchwork-Id: 797281 X-Patchwork-Delegate: shemminger@vyatta.com Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=cumulusnetworks.com header.i=@cumulusnetworks.com header.b="a0xIQ9xp"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3xNZRz5504z9s7C for ; Fri, 4 Aug 2017 01:56:47 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752010AbdHCP4C (ORCPT ); Thu, 3 Aug 2017 11:56:02 -0400 Received: from mail-wm0-f52.google.com ([74.125.82.52]:36193 "EHLO mail-wm0-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751940AbdHCPz6 (ORCPT ); Thu, 3 Aug 2017 11:55:58 -0400 Received: by mail-wm0-f52.google.com with SMTP id t201so19081072wmt.1 for ; Thu, 03 Aug 2017 08:55:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cumulusnetworks.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=S30d/LIIhCZIn8RtBc6lxfp1wxBmYczZL1tZ6GK8g+s=; b=a0xIQ9xpgFxoH1IgotpPSfTNsQGDGZTnzFI42ThZDEIweaDQzY91eIyKAxLXSSGff7 4EyFVgpEiQqmwItjXhF2iuUL5vRiCv2XjkulauV4mbr4ffDUVBbSUyjR9EVWycKnADyb 2/PzycsCSm6lNOyrjxQYbd2PYL+1bdrvHm5f4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=S30d/LIIhCZIn8RtBc6lxfp1wxBmYczZL1tZ6GK8g+s=; b=KbGZGnL3myf4zQ2EKLKYjTMnncvuGCSBvWSsuyCREmMxq3m8p2RWuaAOC5dQwK9YD5 k22RVRJyQzIznd/LKQ7AjPmUYlaEHg+3kd+k8xotFTWHwu2ZZendx0kP1xaZ0cdFj1i2 KvcBaHv2Ppd6uTQ/hFK8NXjJz3ZzufCL1R8Z1vc9tfm1vGmdHUusLXe+rp14HI4YtWpS eG9VaE9ksYRueeWJuBSWhpTFazcw1C16tlH7gXzDGyLhqFMrmVIpy6jEncaHpjH+YirM 5TQXzeggMYu4P5a1BE5kJ574FvJS70dr5M2vpMipp11BNwPutHR5k/WIuBA9S8ZCfqqt 44Sg== X-Gm-Message-State: AIVw112Lv9mD7eQpsp6z+b1JcghP1r3uxstNaccc6xq+gbGRAO2uevP9 w5bSamvIxI9QJMAAD7I= X-Received: by 10.28.213.75 with SMTP id m72mr1524552wmg.41.1501775756368; Thu, 03 Aug 2017 08:55:56 -0700 (PDT) Received: from localhost.localdomain (91-160-18-219.subs.proxad.net. [91.160.18.219]) by smtp.googlemail.com with ESMTPSA id d91sm2213309wma.23.2017.08.03.08.55.55 (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 03 Aug 2017 08:55:55 -0700 (PDT) From: Julien Fortin X-Google-Original-From: Julien Fortin To: netdev@vger.kernel.org Cc: roopa@cumulusnetworks.com, nikolay@cumulusnetworks.com, dsa@cumulusnetworks.com, Julien Fortin Subject: [PATCH 19/27] ip: ipmacsec.c: add json output support Date: Thu, 3 Aug 2017 17:55:07 +0200 Message-Id: <20170803155515.99226-20-julien@cumulusnetworks.com> X-Mailer: git-send-email 2.13.3 In-Reply-To: <20170803155515.99226-1-julien@cumulusnetworks.com> References: <20170803155515.99226-1-julien@cumulusnetworks.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Julien Fortin Schema { "sci": { "type": "string", "attr": "IFLA_MACSEC_SCI" }, "protect": { "type": "string", "attr": "IFLA_MACSEC_PROTECT" }, "cipher_suite": { "type": "string", "attr": "IFLA_MACSEC_CIPHER_SUITE" }, "icv_len": { "type": "uint", "attr": "IFLA_MACSEC_ICV_LEN" }, "encoding_sa": { "type": "uint", "attr": "IFLA_MACSEC_ENCODING_SA" }, "validation": { "type": "string", "attr": "IFLA_MACSEC_VALIDATION" }, "encrypt": { "type": "string", "attr": "IFLA_MACSEC_ENCRYPT" }, "inc_sci": { "type": "string", "attr": "IFLA_MACSEC_INC_SCI" }, "es": { "type": "string", "attr": "IFLA_MACSEC_ES" }, "scb": { "type": "string", "attr": "IFLA_MACSEC_SCB" }, "replay_protect": { "type": "string", "attr": "IFLA_MACSEC_REPLAY_PROTECT" }, "window": { "type": "int", "attr": "" } } Signed-off-by: Julien Fortin --- ip/ipmacsec.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 66 insertions(+), 18 deletions(-) diff --git a/ip/ipmacsec.c b/ip/ipmacsec.c index aa89a00f..ecc371a5 100644 --- a/ip/ipmacsec.c +++ b/ip/ipmacsec.c @@ -561,9 +561,14 @@ static int validate_secy_dump(struct rtattr **attrs) static void print_flag(FILE *f, struct rtattr *attrs[], const char *desc, int field) { - if (attrs[field]) - fprintf(f, "%s %s ", desc, - values_on_off[!!rta_getattr_u8(attrs[field])]); + if (attrs[field]) { + const char *v = values_on_off[!!rta_getattr_u8(attrs[field])]; + + if (is_json_context()) + print_string(PRINT_JSON, desc, NULL, v); + else + fprintf(f, "%s %s ", desc, v); + } } #define DEFAULT_CIPHER_NAME "GCM-AES-128" @@ -1017,8 +1022,16 @@ static void macsec_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) return; if (tb[IFLA_MACSEC_SCI]) { - fprintf(f, "sci %016llx ", - ntohll(rta_getattr_u64(tb[IFLA_MACSEC_SCI]))); + if (is_json_context()) { + SPRINT_BUF(b1); + + snprintf(b1, sizeof(b1), "%016llx", + ntohll(rta_getattr_u64(tb[IFLA_MACSEC_SCI]))); + print_string(PRINT_JSON, "sci", NULL, b1); + } else { + fprintf(f, "sci %016llx ", + ntohll(rta_getattr_u64(tb[IFLA_MACSEC_SCI]))); + } } print_flag(f, tb, "protect", IFLA_MACSEC_PROTECT); @@ -1026,35 +1039,70 @@ static void macsec_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) if (tb[IFLA_MACSEC_CIPHER_SUITE]) { __u64 csid = rta_getattr_u64(tb[IFLA_MACSEC_CIPHER_SUITE]); - fprintf(f, "cipher %s ", cs_id_to_name(csid)); + print_string(PRINT_ANY, + "cipher_suite", + "cipher %s ", + cs_id_to_name(csid)); } if (tb[IFLA_MACSEC_ICV_LEN]) { - fprintf(f, "icvlen %hhu ", - rta_getattr_u8(tb[IFLA_MACSEC_ICV_LEN])); + if (is_json_context()) { + char b2[4]; + + snprintf(b2, sizeof(b2), "%hhu", + rta_getattr_u8(tb[IFLA_MACSEC_ICV_LEN])); + print_uint(PRINT_JSON, "icv_len", NULL, atoi(b2)); + } else { + fprintf(f, "icvlen %hhu ", + rta_getattr_u8(tb[IFLA_MACSEC_ICV_LEN])); + } } if (tb[IFLA_MACSEC_ENCODING_SA]) { - fprintf(f, "encodingsa %hhu ", - rta_getattr_u8(tb[IFLA_MACSEC_ENCODING_SA])); + if (is_json_context()) { + char b2[4]; + + snprintf(b2, sizeof(b2), "%hhu", + rta_getattr_u8(tb[IFLA_MACSEC_ENCODING_SA])); + print_uint(PRINT_JSON, "encoding_sa", NULL, atoi(b2)); + } else { + fprintf(f, "encodingsa %hhu ", + rta_getattr_u8(tb[IFLA_MACSEC_ENCODING_SA])); + } } if (tb[IFLA_MACSEC_VALIDATION]) { __u8 val = rta_getattr_u8(tb[IFLA_MACSEC_VALIDATION]); - fprintf(f, "validate %s ", VALIDATE_STR[val]); + print_string(PRINT_ANY, + "validation", + "validate %s ", + VALIDATE_STR[val]); + } + + const char *inc_sci, *es, *replay; + + if (is_json_context()) { + inc_sci = "inc_sci"; + replay = "replay_protect"; + es = "es"; + } else { + inc_sci = "send_sci"; + es = "end_station"; + replay = "replay"; } print_flag(f, tb, "encrypt", IFLA_MACSEC_ENCRYPT); - print_flag(f, tb, "send_sci", IFLA_MACSEC_INC_SCI); - print_flag(f, tb, "end_station", IFLA_MACSEC_ES); + print_flag(f, tb, inc_sci, IFLA_MACSEC_INC_SCI); + print_flag(f, tb, es, IFLA_MACSEC_ES); print_flag(f, tb, "scb", IFLA_MACSEC_SCB); + print_flag(f, tb, replay, IFLA_MACSEC_REPLAY_PROTECT); - print_flag(f, tb, "replay", IFLA_MACSEC_REPLAY_PROTECT); - if (tb[IFLA_MACSEC_WINDOW]) { - fprintf(f, "window %d ", - rta_getattr_u32(tb[IFLA_MACSEC_WINDOW])); - } + if (tb[IFLA_MACSEC_WINDOW]) + print_int(PRINT_ANY, + "window", + "window %d ", + rta_getattr_u32(tb[IFLA_MACSEC_WINDOW])); } static bool check_txsc_flags(bool es, bool scb, bool sci)