diff mbox series

[v2,2/2] bootpc: add dhcp-like configuration ability

Message ID 20230526175506.1071354-3-colin.foster@in-advantage.com
State New
Headers show
Series package: bootpc: add bootpc and bootp interface configuration | expand

Commit Message

Colin Foster May 26, 2023, 5:55 p.m. UTC
bootpc only works on interfaces that are up. If BOOTP is desired to get
an IP address to a running kernel, bootpc won't work.

This scenario was brought up as a bug in Debian forums [1] in 2007. The
response was to run the attached script to bring up the network without
an IP address. Sure enough, it still works!

Implement this bootpc script to allow IP configuration by way of
"iface eth0 inet bootp" in /etc/network/interfaces.

[1] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=436443

Signed-off-by: Colin Foster <colin.foster@in-advantage.com>
---

v2:
  * check-package fixups
  * Ignore shellcheck issues in the bootpc_dhcp_script
  * Fix max line lengths
  * Fix stale OVERLAY reference
  * Note: pre-up and post-up were suggested instead of doing things in
    the bootpc_dhcp_script. If the script could be removed entirely, I
    feel that'd be the best course, but it seemingly needs to be there
    to apply the settings. Keeping things in the script leads to less
    user configuration, so that was chosen.

---
 .checkpackageignore               |  1 +
 package/bootpc/Config.in          | 16 ++++++
 package/bootpc/bootpc.mk          | 22 +++++++-
 package/bootpc/bootpc_dhcp_script | 86 +++++++++++++++++++++++++++++++
 4 files changed, 123 insertions(+), 2 deletions(-)
 create mode 100644 package/bootpc/bootpc_dhcp_script
diff mbox series

Patch

diff --git a/.checkpackageignore b/.checkpackageignore
index 947f4606aa..15cfc6868b 100644
--- a/.checkpackageignore
+++ b/.checkpackageignore
@@ -243,6 +243,7 @@  package/bmx7/0002-Fix-linking-error.patch Upstream
 package/bmx7/0003-Reorder-includes-to-avoid-ethhdr-collision.patch Upstream
 package/boinc/S99boinc-client Indent Shellcheck Variables
 package/bootgen/0001-Fix-build-on-machines-with-modern-flex.patch Upstream
+package/bootpc/bootpc_dhcp_script Shellcheck
 package/botan/0001-Add-superh-alias-needed-by-Debian.patch Upstream
 package/botan/0002-src-build-data-arch-superh.txt-add-sh4-eb-aeb.patch Upstream
 package/brickd/S70brickd Indent Shellcheck Variables
diff --git a/package/bootpc/Config.in b/package/bootpc/Config.in
index 59c6a6e739..1a77181c35 100644
--- a/package/bootpc/Config.in
+++ b/package/bootpc/Config.in
@@ -2,3 +2,19 @@  config BR2_PACKAGE_BOOTPC
 	bool "bootpc"
 	help
 	  Application to perform BOOTP
+
+if BR2_PACKAGE_BOOTPC
+
+config BR2_PACKAGE_BOOTPC_FULL_CONFIG
+	bool "bootpc interface configuration script"
+	help
+	  BOOTP can be used to fully configure a network interface in a
+	  similar manner to DHCP. bootpc requires the network interface
+	  to be configured before this can happen.
+
+	  Replace the bootpc binary with a script that allows an
+	  unconfigured network interface to work as a BOOTP client. The
+	  setting can be applied with "iface eth0 inet bootp" in
+	  /etc/network/interfaces.
+
+endif
diff --git a/package/bootpc/bootpc.mk b/package/bootpc/bootpc.mk
index 200ed24872..e3d41350d9 100644
--- a/package/bootpc/bootpc.mk
+++ b/package/bootpc/bootpc.mk
@@ -10,12 +10,30 @@  BOOTPC_SITE_METHOD = git
 BOOTPC_LICENSE = GPL-2.0-or-later
 BOOTPC_LICENSE_FILES = LICENSE NOTICE
 
+ifeq ($(BR2_PACKAGE_BOOTPC_FULL_CONFIG),y)
+BOOTPC_TARGET_BIN_NAME = bootpc-bin
+else
+BOOTPC_TARGET_BIN_NAME = bootpc
+endif
+
 define BOOTPC_BUILD_CMDS
-	$(TARGET_MAKE_ENV) $(TARGET_CONFIGURE_OPTS) $(MAKE) -C $(@D) all
+	$(TARGET_MAKE_ENV) $(TARGET_CONFIGURE_OPTS) $(MAKE) \
+		-C $(@D) \
+		PROG=$(BOOTPC_TARGET_BIN_NAME) \
+		all
+endef
+
+ifeq ($(BR2_PACKAGE_BOOTPC_FULL_CONFIG),y)
+define BOOTPC_INSTALL_CONFIG_SCRIPT
+	$(INSTALL) -m 0755 \
+		package/bootpc/bootpc_dhcp_script \
+		$(TARGET_DIR)/sbin/bootpc
 endef
+endif
 
 define BOOTPC_INSTALL_TARGET_CMDS
-	$(INSTALL) -D -m 0755 $(@D)/bootpc $(TARGET_DIR)/sbin
+	$(INSTALL) -D -m 0755 $(@D)/$(BOOTPC_TARGET_BIN_NAME) $(TARGET_DIR)/sbin
+	$(BOOTPC_INSTALL_CONFIG_SCRIPT)
 endef
 
 $(eval $(generic-package))
diff --git a/package/bootpc/bootpc_dhcp_script b/package/bootpc/bootpc_dhcp_script
new file mode 100644
index 0000000000..239ef5ec03
--- /dev/null
+++ b/package/bootpc/bootpc_dhcp_script
@@ -0,0 +1,86 @@ 
+#!/bin/sh
+#
+# script to initialize a network interface using bootp
+#
+# Copyright (C) 2008 by Fuji Xerox Co., Ltd. All rights reserved.
+#
+# 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; either version 2 of the License, or
+# (at your option) any later version.
+#
+# 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.
+#
+# written by Kohei Tanaka <Kohei.Tanaka@fujixerox.co.jp>
+
+INTERFACE=eth0
+
+# load configuration file
+[ -f /etc/default/bootpc ] && . /etc/default/bootpc
+
+# to get the interface looping over arguments
+while [ $# -ne 0 ]
+do
+	case $1 in
+	--dev)	INTERFACE=$2
+		break;;
+	*)	shift
+		continue;;
+	esac
+done
+
+# up the interface for sending broadcast packet
+ifconfig $INTERFACE up 0.0.0.0
+route add default dev $INTERFACE
+
+TMPFILE=`mktemp /tmp/bootpc.XXXXXX`
+
+/sbin/bootpc-bin --dev $INTERFACE --returniffail --serverbcast > $TMPFILE
+RET=$?
+route del default
+ifconfig $INTERFACE down
+if [ $RET -ne 0 ]
+then
+	rm -rf $TMPFILE
+	exit 1
+fi
+
+. $TMPFILE
+rm -rf $TMPFILE
+
+[ -z "$IPADDR" ] && exit 1
+
+OPT_NETMASK=""
+OPT_BROADCAST=""
+if [ -n "$NETMASK" ]
+then
+	OPT_NETMASK="netmask $NETMASK"
+fi
+if [ -n "$BROADCAST" ]
+then
+	OPT_BROADCAST="broadcast $BROADCAST"
+fi
+ifconfig $INTERFACE $IPADDR $OPT_NETMASK $OPT_BROADCAST
+
+# already added a route to the network.  just add default gateway.
+for i in $GATEWAYS
+do
+	route add default gw $i
+done
+
+[ x"$SETDNS" == xno ] && exit 0
+if [ -n "$DOMAIN" ] || [ -n "$DNSSRVS" ]
+then
+	mv -f /etc/resolv.conf /etc/resolv.conf.old
+	if [ -n "$DOMAIN" ]
+	then
+		echo "search $DOMAIN" >> /etc/resolv.conf
+	fi
+	for i in $DNSSRVS
+	do
+		echo "nameserver $i" >> /etc/resolv.conf
+	done
+fi