From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 26/27] commands: Add ip_route_add command
Date: Fri, 1 Dec 2017 12:22:55 +0100 [thread overview]
Message-ID: <20171201112256.20196-27-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20171201112256.20196-1-s.hauer@pengutronix.de>
The ip_route_get command is used to retrieve the network
device used to reach a given IP address. This is useful
for informational purposes and also for the network boot
which wants to get the linux.bootargs parameter of the
network device used for nfsroot.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
commands/Kconfig | 10 ++++++
commands/Makefile | 1 +
commands/ip-route-get.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 107 insertions(+)
create mode 100644 commands/ip-route-get.c
diff --git a/commands/Kconfig b/commands/Kconfig
index ae2dc4b094..eee4b6aee8 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -1200,6 +1200,16 @@ config CMD_TFTP
Options:
-p push to TFTP server
+config CMD_IP_ROUTE_GET
+ tristate
+ prompt "ip-route-get"
+ default y
+ help
+ The ip-route-get command is used to retrieve the network interface
+ which is used to reach the specified IP address. Information can
+ be shown on the command line or alternatively a variable is set to
+ the result.
+
# end Network commands
endmenu
diff --git a/commands/Makefile b/commands/Makefile
index 582175f631..eb4796389e 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -122,3 +122,4 @@ obj-$(CONFIG_CMD_SPD_DECODE) += spd_decode.o
obj-$(CONFIG_CMD_MMC_EXTCSD) += mmc_extcsd.o
obj-$(CONFIG_CMD_NAND_BITFLIP) += nand-bitflip.o
obj-$(CONFIG_CMD_SEED) += seed.o
+obj-$(CONFIG_CMD_IP_ROUTE_GET) += ip-route-get.o
diff --git a/commands/ip-route-get.c b/commands/ip-route-get.c
new file mode 100644
index 0000000000..d393218188
--- /dev/null
+++ b/commands/ip-route-get.c
@@ -0,0 +1,96 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; version 2.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ */
+
+#include <command.h>
+#include <common.h>
+#include <getopt.h>
+#include <complete.h>
+#include <environment.h>
+#include <net.h>
+
+static int do_ip_route_get(int argc, char *argv[])
+{
+ struct eth_device *edev;
+ IPaddr_t ip, gw = 0;
+ const char *variable = NULL;
+ int opt, ret;
+ bool bootarg = false;
+
+ while ((opt = getopt(argc, argv, "b")) > 0) {
+ switch (opt) {
+ case 'b':
+ bootarg = true;
+ break;
+ default:
+ return COMMAND_ERROR_USAGE;
+ }
+ }
+
+ if (argc == optind)
+ return COMMAND_ERROR_USAGE;
+
+ if (argc == optind + 2)
+ variable = argv[optind + 1];
+
+ ret = string_to_ip(argv[optind], &ip);
+ if (ret) {
+ printf("Cannot convert %s into a IP address: %s\n",
+ argv[1], strerror(-ret));
+ return 1;
+ }
+
+ edev = net_route(ip);
+ if (!edev) {
+ gw = net_get_gateway();
+ if (gw)
+ edev = net_route(gw);
+ }
+
+ if (!edev) {
+ printf("IP %pI4 is not reachable\n", &ip);
+ return 1;
+ }
+
+ if (variable) {
+ if (bootarg)
+ setenv(variable, edev->bootarg);
+ else
+ setenv(variable, edev->devname);
+ return 0;
+ }
+
+ if (bootarg) {
+ printf("%s\n", edev->bootarg);
+ } else {
+ if (gw)
+ printf("%pI4 via %pI4 dev %s\n", &ip, &gw,
+ edev->devname);
+ else
+ printf("%pI4 dev %s\n", &ip, edev->devname);
+ }
+
+ return 0;
+}
+
+BAREBOX_CMD_HELP_START(ip_route_get)
+BAREBOX_CMD_HELP_TEXT("get ethernet device used to reach given IP address")
+BAREBOX_CMD_HELP_TEXT("")
+BAREBOX_CMD_HELP_TEXT("Options:")
+BAREBOX_CMD_HELP_OPT("-b", "Instead of ethernet device, show linux bootargs for that device")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(ip_route_get)
+ .cmd = do_ip_route_get,
+ BAREBOX_CMD_DESC("get ethernet device name for an IP address")
+ BAREBOX_CMD_OPTS("[-b] <IP> [variable]")
+ BAREBOX_CMD_GROUP(CMD_GRP_MISC)
+ BAREBOX_CMD_COMPLETE(empty_complete)
+BAREBOX_CMD_END
--
2.11.0
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2017-12-01 11:23 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-12-01 11:22 [PATCH v2] Networking updates Sascha Hauer
2017-12-01 11:22 ` [PATCH 01/27] detect command: Drop -e option Sascha Hauer
2017-12-01 11:22 ` [PATCH 02/27] driver: Add device_detect_all() function Sascha Hauer
2017-12-01 11:22 ` [PATCH 03/27] nvvar: when setting a nvvar to NULL just free the content Sascha Hauer
2017-12-01 11:22 ` [PATCH 04/27] net: use pr_* functions for messages Sascha Hauer
2017-12-01 11:22 ` [PATCH 05/27] net: Add and use IP_BROADCAST Sascha Hauer
2017-12-01 11:22 ` [PATCH 06/27] net: Make domainname and nameserver globalvars Sascha Hauer
2017-12-01 11:22 ` [PATCH 07/27] net: Add functions to get/set nameserver and domainname Sascha Hauer
2017-12-01 11:22 ` [PATCH 08/27] net: introduce global.net.server Sascha Hauer
2017-12-01 11:22 ` [PATCH 09/27] net: dhcp: Do not overwrite serverip if it is valid Sascha Hauer
2017-12-01 11:22 ` [PATCH 10/27] net: Use a single gateway Sascha Hauer
2017-12-01 11:22 ` [PATCH 11/27] net: allow udp connections on specified network device Sascha Hauer
2017-12-01 11:22 ` [PATCH 12/27] net: dhcp: Allow to specify " Sascha Hauer
2017-12-01 11:22 ` [PATCH 13/27] net: dhcp: avoid unnecessary casts Sascha Hauer
2017-12-01 11:22 ` [PATCH 14/27] net: dhcp: Coding style fixes Sascha Hauer
2017-12-01 11:22 ` [PATCH 15/27] net: dhcp: rework Sascha Hauer
2017-12-08 21:56 ` Oleksij Rempel
2017-12-01 11:22 ` [PATCH 16/27] net: Pick network device based on IP settings Sascha Hauer
2017-12-01 11:22 ` [PATCH 17/27] net: remove "current" network device Sascha Hauer
2017-12-01 11:22 ` [PATCH 18/27] net: ifup: Factor out a eth_discover function Sascha Hauer
2017-12-01 11:22 ` [PATCH 19/27] ifup: Use dhcp C API rather than running command Sascha Hauer
2017-12-01 11:22 ` [PATCH 20/27] net: Provide new way to configure network devices Sascha Hauer
2017-12-01 11:22 ` [PATCH 21/27] net: update network docs Sascha Hauer
2017-12-01 11:22 ` [PATCH 22/27] net: environment: remove ethx setup files Sascha Hauer
2017-12-01 11:22 ` [PATCH 23/27] net: environment: update automounts Sascha Hauer
2017-12-01 11:22 ` [PATCH 24/27] defaultenv: Add README for new network config Sascha Hauer
2017-12-01 11:22 ` [PATCH 25/27] net: Add linuxdevname property Sascha Hauer
2017-12-01 11:22 ` Sascha Hauer [this message]
2017-12-01 11:22 ` [PATCH 27/27] defaultenv-2: set bootargs correctly for network boot Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20171201112256.20196-27-s.hauer@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox