Re: [PATCH iproute2 net-next 1/3] ip: add ip sr command to control SR-IPv6 internal structures

2017-04-14 Thread Stephen Hemminger
On Fri, 14 Apr 2017 14:36:21 +0200
David Lebrun  wrote:

> +static struct {
> + int cmd;
Why not unsigned? you only assign positive values
> + struct in6_addr addr;
> + __u32 keyid;
> + char *pass;
Why not const char *? or do you free the value on exit?
> + __u8 alg_id;
> +} opts;
> +


[PATCH iproute2 net-next 1/3] ip: add ip sr command to control SR-IPv6 internal structures

2017-04-14 Thread David Lebrun
This patch adds commands to support the tunnel source properties
("ip sr tunsrc") and the HMAC key -> secret, algorithm binding
("ip sr hmac").

Signed-off-by: David Lebrun 
---
 ip/Makefile|   2 +-
 ip/ip.c|   3 +-
 ip/ip_common.h |   1 +
 ip/ipseg6.c| 238 +
 4 files changed, 242 insertions(+), 2 deletions(-)
 create mode 100644 ip/ipseg6.c

diff --git a/ip/Makefile b/ip/Makefile
index 035d42c..e08c170 100644
--- a/ip/Makefile
+++ b/ip/Makefile
@@ -9,7 +9,7 @@ IPOBJ=ip.o ipaddress.o ipaddrlabel.o iproute.o iprule.o 
ipnetns.o \
 link_iptnl.o link_gre6.o iplink_bond.o iplink_bond_slave.o iplink_hsr.o \
 iplink_bridge.o iplink_bridge_slave.o ipfou.o iplink_ipvlan.o \
 iplink_geneve.o iplink_vrf.o iproute_lwtunnel.o ipmacsec.o ipila.o \
-ipvrf.o iplink_xstats.o
+ipvrf.o iplink_xstats.o ipseg6.o
 
 RTMONOBJ=rtmon.o
 
diff --git a/ip/ip.c b/ip/ip.c
index 07050b0..7c14a8e 100644
--- a/ip/ip.c
+++ b/ip/ip.c
@@ -52,7 +52,7 @@ static void usage(void)
 "where  OBJECT := { link | address | addrlabel | route | rule | neigh | ntable 
|\n"
 "   tunnel | tuntap | maddress | mroute | mrule | monitor | 
xfrm |\n"
 "   netns | l2tp | fou | macsec | tcp_metrics | token | 
netconf | ila |\n"
-"   vrf }\n"
+"   vrf | sr }\n"
 "   OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[esolve] |\n"
 "-h[uman-readable] | -iec |\n"
 "-f[amily] { inet | inet6 | ipx | dnet | mpls | bridge | 
link } |\n"
@@ -101,6 +101,7 @@ static const struct cmd {
{ "netns",  do_netns },
{ "netconf",do_ipnetconf },
{ "vrf",do_ipvrf},
+   { "sr", do_seg6 },
{ "help",   do_help },
{ 0 }
 };
diff --git a/ip/ip_common.h b/ip/ip_common.h
index 5a39623..202fc39 100644
--- a/ip/ip_common.h
+++ b/ip/ip_common.h
@@ -60,6 +60,7 @@ int do_iptoken(int argc, char **argv);
 int do_ipvrf(int argc, char **argv);
 void vrf_reset(void);
 int netns_identify_pid(const char *pidstr, char *name, int len);
+int do_seg6(int argc, char **argv);
 
 int iplink_get(unsigned int flags, char *name, __u32 filt_mask);
 int iplink_ifla_xstats(int argc, char **argv);
diff --git a/ip/ipseg6.c b/ip/ipseg6.c
new file mode 100644
index 000..0d4130e
--- /dev/null
+++ b/ip/ipseg6.c
@@ -0,0 +1,238 @@
+/*
+ * seg6.c "ip sr/seg6"
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   version 2 as published by the Free Software Foundation;
+ *
+ * Author: David Lebrun 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+
+#include "utils.h"
+#include "ip_common.h"
+#include "libgenl.h"
+
+#define HMAC_KEY_PROMPT "Enter secret for HMAC key ID (blank to delete): "
+
+static void usage(void)
+{
+   fprintf(stderr, "Usage: ip sr { COMMAND | help }\n");
+   fprintf(stderr, "  ip sr hmac show\n");
+   fprintf(stderr, "  ip sr hmac set KEYID ALGO\n");
+   fprintf(stderr, "  ip sr tunsrc show\n");
+   fprintf(stderr, "  ip sr tunsrc set ADDRESS\n");
+   fprintf(stderr, "where  ALGO := { sha1 | sha256 }\n");
+   exit(-1);
+}
+
+static struct rtnl_handle grth = { .fd = -1 };
+static int genl_family = -1;
+
+#define SEG6_REQUEST(_req, _bufsiz, _cmd, _flags) \
+   GENL_REQUEST(_req, _bufsiz, genl_family, 0, \
+   SEG6_GENL_VERSION, _cmd, _flags)
+
+static struct {
+   int cmd;
+   struct in6_addr addr;
+   __u32 keyid;
+   char *pass;
+   __u8 alg_id;
+} opts;
+
+static int process_msg(const struct sockaddr_nl *who, struct nlmsghdr *n,
+  void *arg)
+{
+   struct rtattr *attrs[SEG6_ATTR_MAX + 1];
+   struct genlmsghdr *ghdr;
+   FILE *fp = (FILE *)arg;
+   int len = n->nlmsg_len;
+
+   if (n->nlmsg_type != genl_family)
+   return -1;
+
+   len -= NLMSG_LENGTH(GENL_HDRLEN);
+   if (len < 0)
+   return -1;
+
+   ghdr = NLMSG_DATA(n);
+
+   parse_rtattr(attrs, SEG6_ATTR_MAX, (void *)ghdr + GENL_HDRLEN, len);
+
+   switch (ghdr->cmd) {
+   case SEG6_CMD_DUMPHMAC:
+   {
+   char secret[64];
+   char *algstr;
+   __u8 slen = rta_getattr_u8(attrs[SEG6_ATTR_SECRETLEN]);
+   __u8 alg_id = rta_getattr_u8(attrs[SEG6_ATTR_ALGID]);
+
+   memset(secret, 0, 64);
+
+   if (slen > 63) {
+   fprintf(stderr, "HMAC secret length %d > 63, "
+   "truncated\n", slen);
+   slen = 63;
+   }
+   memcpy(secret, RTA_DATA(attrs[SEG6_ATTR_SECRET]), slen);
+
+   switch (alg_id) {