62 lines
1.9 KiB
Bash
Executable File
62 lines
1.9 KiB
Bash
Executable File
#!/bin/sh
|
|
# Handle USB networking and tethering with unudhcpd and NetworkManager
|
|
# without unbinding the USB gadget UDC on link up/down events.
|
|
|
|
con_uuid="83bd1823-feca-4c2b-9205-4b83dc792e1f"
|
|
|
|
[ -f /usr/share/misc/source_deviceinfo ] && . /usr/share/misc/source_deviceinfo
|
|
[ -e /etc/unudhcpd.conf ] && . /etc/unudhcpd.conf
|
|
|
|
usb_network_function="${deviceinfo_usb_network_function:-ncm.usb0}"
|
|
usb_network_function_fallback="rndis.usb0"
|
|
disable_dhcpd="${deviceinfo_disable_dhcpd:-false}"
|
|
host_ip="${unudhcpd_host_ip:-172.16.42.1}"
|
|
client_ip="${unudhcpd_client_ip:-172.16.42.2}"
|
|
|
|
CONFIGFS="/sys/kernel/config/usb_gadget"
|
|
if ! [ -d "$CONFIGFS" ]; then
|
|
mount -t configfs configfs /sys/kernel/config 2>/dev/null || true
|
|
fi
|
|
|
|
ensure_gadget_bound() {
|
|
udc_dev="$(ls /sys/class/udc 2>/dev/null | head -n 1)"
|
|
[ -n "$udc_dev" ] || return 0
|
|
if [ -d "$CONFIGFS/g1" ]; then
|
|
cur_udc="$(cat "$CONFIGFS/g1/UDC" 2>/dev/null | tr -d '[:space:]')"
|
|
if [ -z "$cur_udc" ]; then
|
|
echo "$udc_dev" > "$CONFIGFS/g1/UDC" 2>/dev/null || true
|
|
fi
|
|
fi
|
|
}
|
|
|
|
ensure_gadget_bound
|
|
|
|
interface="$(
|
|
cat "$CONFIGFS/g1/functions/$usb_network_function/ifname" 2>/dev/null ||
|
|
cat "$CONFIGFS/g1/functions/$usb_network_function_fallback/ifname" 2>/dev/null ||
|
|
echo "usb0"
|
|
)"
|
|
|
|
if [ -n "$DEVICE_IFACE" ] && [ "$DEVICE_IFACE" != "$interface" ]; then
|
|
exit 0
|
|
fi
|
|
|
|
ensure_usb_dhcp() {
|
|
ip link set "$interface" up 2>/dev/null || true
|
|
if ! ip -4 addr show dev "$interface" 2>/dev/null | grep -q "$host_ip"; then
|
|
ip addr add "$host_ip/16" dev "$interface" 2>/dev/null || true
|
|
fi
|
|
if [ "$disable_dhcpd" != "true" ] && ! pidof unudhcpd >/dev/null 2>&1; then
|
|
(unudhcpd -i "$interface" -s "$host_ip" -c "$client_ip" >/dev/null 2>&1) &
|
|
logger -t nm-tethering "unudhcpd started on $interface ($host_ip -> $client_ip)"
|
|
fi
|
|
}
|
|
|
|
case "${NM_DISPATCHER_ACTION:-up}" in
|
|
up|down|reapply)
|
|
ensure_gadget_bound
|
|
ensure_usb_dhcp
|
|
;;
|
|
esac
|
|
exit 0
|