-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathusb-network.sh
More file actions
142 lines (118 loc) · 4.32 KB
/
usb-network.sh
File metadata and controls
142 lines (118 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/sh
# Made by Jack'lul <jacklul.github.io>
#
# Connect any USB networking gadget device to your LAN
#
# To be used with devices that can use USB Gadget mode.
# Raspberry Pi Zero will probably be the best for this.
# See https://github.com/jacklul/asuswrt-usb-raspberry-pi
#
#jas-update=usb-network.sh
#shellcheck shell=ash
#shellcheck disable=SC2155
#shellcheck source=./common.sh
readonly common_script="$(dirname "$0")/common.sh"
if [ -f "$common_script" ]; then . "$common_script"; else { echo "$common_script not found" >&2; exit 1; } fi
BRIDGE_INTERFACE="" # bridge interface to add into, by default LAN bridge interface
EXECUTE_COMMAND="" # execute a command each time status changes (receives arguments: $1 = action (add/remove), $2 = interface)
RUN_EVERY_MINUTE= # scan for new interfaces to add to bridge periodically (true/false), empty means false when hotplug-event and service-event script are available but otherwise true
load_script_config
require_bridge_interface() {
[ -z "$BRIDGE_INTERFACE" ] && BRIDGE_INTERFACE="$(nvram get lan_ifname)"
}
is_interface_up() {
[ ! -d "/sys/class/net/$1" ] && return 1
local _operstate="$(cat "/sys/class/net/$1/operstate")"
case "$_operstate" in
"up")
return 0
;;
"unknown")
[ "$(cat "/sys/class/net/$1/carrier")" = "1" ] && return 0
;;
esac
# All other states: down, notpresent, lowerlayerdown, testing, dormant
return 1
}
setup_inteface() {
[ -z "$2" ] && { echo "You must specify a network interface" >&2; exit 1; }
require_bridge_interface
lockfile lockwait
case "$1" in
"add")
[ ! -d "/sys/class/net/$2" ] && return
if ! is_interface_up "$2"; then
logecho "Bringing interface '$2' up..."
ifconfig "$2" up
fi
if ! brctl show "$BRIDGE_INTERFACE" | grep -Fq "$2" && brctl addif "$BRIDGE_INTERFACE" "$2"; then
logecho "Added interface '$2' to bridge '$BRIDGE_INTERFACE'" alert
fi
;;
"remove")
if brctl show "$BRIDGE_INTERFACE" | grep -Fq "$2" && brctl delif "$BRIDGE_INTERFACE" "$2"; then
logecho "Removed interface '$2' from bridge '$BRIDGE_INTERFACE'" alert
fi
if [ -d "/sys/class/net/$2" ] && is_interface_up "$2"; then
logecho "Taking interface '$2' down..."
ifconfig "$2" down
fi
;;
esac
# no extra condition needed, already handled outside this function
[ -n "$EXECUTE_COMMAND" ] && eval "$EXECUTE_COMMAND $1 $2"
lockfile unlock
}
setup_interfaces() {
local _interface
for _interface in /sys/class/net/usb*; do
[ -d "$_interface" ] && setup_inteface "$1" "$(basename "$_interface")"
done
}
case "$1" in
"run")
require_bridge_interface
bridge_members="$(brctl show "$BRIDGE_INTERFACE")"
for interface in /sys/class/net/usb*; do
[ ! -d "$interface" ] && continue
interface="$(basename "$interface")"
if ! echo "$bridge_members" | grep -Fq "$interface"; then
setup_inteface add "$interface"
fi
done
;;
"hotplug")
if [ "$SUBSYSTEM" = "net" ] && [ "$(echo "$DEVICENAME" | cut -c 1-3)" = "usb" ]; then
case "$ACTION" in
"add")
setup_inteface add "$DEVICENAME"
;;
"remove")
setup_inteface remove "$DEVICENAME"
;;
esac
fi
;;
"start")
setup_interfaces add
# Set value of empty RUN_EVERY_MINUTE depending on situation
execute_script_basename "service-event.sh" check && service_event_active=true
execute_script_basename "hotplug-event.sh" check && hotplug_event_active=true
[ -z "$RUN_EVERY_MINUTE" ] && { [ -z "$service_event_active" ] || [ -z "$hotplug_event_active" ] ; } && RUN_EVERY_MINUTE=true
if [ "$RUN_EVERY_MINUTE" = true ]; then
crontab_entry add "*/1 * * * * $script_path run"
fi
;;
"stop")
crontab_entry delete
setup_interfaces remove
;;
"restart")
sh "$script_path" stop
sh "$script_path" start
;;
*)
echo "Usage: $0 run|start|stop|restart"
exit 1
;;
esac