b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | # Copyright (C) 2006 OpenWrt.org |
| 2 | # Copyright (C) 2010 Vertical Communications |
| 3 | |
| 4 | preinit_ip_config() { |
| 5 | local netdev vid |
| 6 | |
| 7 | netdev=${1%\.*} |
| 8 | vid=${1#*\.} |
| 9 | |
| 10 | if [ "$vid" = "$netdev" ]; then |
| 11 | vid= |
| 12 | fi |
| 13 | |
| 14 | grep -q "$netdev" /proc/net/dev || return |
| 15 | |
| 16 | if [ -n "$vid" ]; then |
| 17 | ip link add link $netdev name $1 type vlan id $vid |
| 18 | fi |
| 19 | |
| 20 | ip link set dev $netdev up |
| 21 | if [ -n "$vid" ]; then |
| 22 | ip link set dev $1 up |
| 23 | fi |
| 24 | ip -4 address add $pi_ip/$pi_netmask broadcast $pi_broadcast dev $1 |
| 25 | } |
| 26 | |
| 27 | preinit_config_switch() { |
| 28 | local role roles ports device enable reset |
| 29 | |
| 30 | local name=$1 |
| 31 | local lan_if=$2 |
| 32 | |
| 33 | json_select switch |
| 34 | json_select $name |
| 35 | |
| 36 | json_get_vars enable reset |
| 37 | |
| 38 | if [ "$reset" -eq "1" ]; then |
| 39 | swconfig dev $name set reset |
| 40 | fi |
| 41 | swconfig dev $name set enable_vlan $enable |
| 42 | |
| 43 | if json_is_a roles array; then |
| 44 | json_get_keys roles roles |
| 45 | json_select roles |
| 46 | |
| 47 | for role in $roles; do |
| 48 | json_select "$role" |
| 49 | json_get_vars ports device |
| 50 | json_select .. |
| 51 | |
| 52 | if [ "$device" = "$lan_if" ]; then |
| 53 | swconfig dev $name vlan $role set ports "$ports" |
| 54 | fi |
| 55 | done |
| 56 | |
| 57 | json_select .. |
| 58 | fi |
| 59 | |
| 60 | swconfig dev $name set apply |
| 61 | |
| 62 | json_select .. |
| 63 | json_select .. |
| 64 | } |
| 65 | |
| 66 | preinit_config_port() { |
| 67 | local original |
| 68 | local dev_port |
| 69 | |
| 70 | local netdev="$1" |
| 71 | local path="$2" |
| 72 | local port="$3" |
| 73 | |
| 74 | [ -d "/sys/devices/$path/net" ] || return |
| 75 | |
| 76 | if [ -z "$port" ]; then |
| 77 | original="$(ls "/sys/devices/$path/net" | head -1)" |
| 78 | else |
| 79 | for device in /sys/devices/$path/net/*; do |
| 80 | dev_port="$(cat "$device/dev_port")" |
| 81 | if [ "$dev_port" = "$port" ]; then |
| 82 | original="${device##*/}" |
| 83 | break |
| 84 | fi |
| 85 | done |
| 86 | |
| 87 | [ -z "$original" ] && return |
| 88 | fi |
| 89 | |
| 90 | [ "$netdev" = "$original" ] && return |
| 91 | |
| 92 | ip link set "$original" name "$netdev" |
| 93 | } |
| 94 | |
| 95 | preinit_config_board() { |
| 96 | /bin/board_detect /tmp/board.json |
| 97 | |
| 98 | [ -f "/tmp/board.json" ] || return |
| 99 | |
| 100 | . /usr/share/libubox/jshn.sh |
| 101 | |
| 102 | json_init |
| 103 | json_load "$(cat /tmp/board.json)" |
| 104 | |
| 105 | # Find the current highest eth* |
| 106 | max_eth=$(grep -o '^ *eth[0-9]*:' /proc/net/dev | tr -dc '[0-9]\n' | sort -n | tail -1) |
| 107 | # Find and move netdevs using eth*s we are configuring |
| 108 | json_get_keys keys "network_device" |
| 109 | for netdev in $keys; do |
| 110 | json_select "network_device" |
| 111 | json_select "$netdev" |
| 112 | json_get_vars path path |
| 113 | if [ -n "$path" -a -h "/sys/class/net/$netdev" ]; then |
| 114 | ip link set "$netdev" down |
| 115 | ip link set "$netdev" name eth$((++max_eth)) |
| 116 | fi |
| 117 | json_select .. |
| 118 | json_select .. |
| 119 | done |
| 120 | |
| 121 | # Move interfaces by path to their netdev name |
| 122 | json_get_keys keys "network_device" |
| 123 | for netdev in $keys; do |
| 124 | json_select "network_device" |
| 125 | json_select "$netdev" |
| 126 | json_get_vars path path |
| 127 | json_get_vars port port |
| 128 | [ -n "$path" ] && preinit_config_port "$netdev" "$path" "$port" |
| 129 | json_select .. |
| 130 | json_select .. |
| 131 | done |
| 132 | |
| 133 | json_select network |
| 134 | json_select "lan" |
| 135 | json_get_vars device |
| 136 | json_get_values ports ports |
| 137 | json_select .. |
| 138 | json_select .. |
| 139 | |
| 140 | [ -n "$device" -o -n "$ports" ] || return |
| 141 | |
| 142 | # swconfig uses $device and DSA uses ports |
| 143 | [ -z "$ports" ] && { |
| 144 | ports="$device" |
| 145 | } |
| 146 | |
| 147 | # only use the first one |
| 148 | ifname=${ports%% *} |
| 149 | |
| 150 | if [ -x /sbin/swconfig ]; then |
| 151 | # configure the switch, if present |
| 152 | |
| 153 | json_get_keys keys switch |
| 154 | for key in $keys; do |
| 155 | preinit_config_switch $key $ifname |
| 156 | done |
| 157 | else |
| 158 | # trim any vlan ids |
| 159 | ifname=${ifname%\.*} |
| 160 | # trim any vlan modifiers like :t |
| 161 | ifname=${ifname%\:*} |
| 162 | fi |
| 163 | |
| 164 | pi_ifname=$ifname |
| 165 | |
| 166 | preinit_ip_config $pi_ifname |
| 167 | } |
| 168 | |
| 169 | preinit_ip() { |
| 170 | [ "$pi_preinit_no_failsafe" = "y" ] && return |
| 171 | |
| 172 | # if the preinit interface isn't specified and ifname is set in |
| 173 | # preinit.arch use that interface |
| 174 | if [ -z "$pi_ifname" ]; then |
| 175 | pi_ifname=$ifname |
| 176 | fi |
| 177 | |
| 178 | if [ -n "$pi_ifname" ]; then |
| 179 | preinit_ip_config $pi_ifname |
| 180 | elif [ -d "/etc/board.d/" ]; then |
| 181 | preinit_config_board |
| 182 | fi |
| 183 | |
| 184 | preinit_net_echo "Doing OpenWrt Preinit\n" |
| 185 | } |
| 186 | |
| 187 | preinit_ip_deconfig() { |
| 188 | [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && { |
| 189 | local netdev vid |
| 190 | |
| 191 | netdev=${pi_ifname%\.*} |
| 192 | vid=${pi_ifname#*\.} |
| 193 | |
| 194 | if [ "$vid" = "$netdev" ]; then |
| 195 | vid= |
| 196 | fi |
| 197 | |
| 198 | ip -4 address flush dev $pi_ifname |
| 199 | ip link set dev $netdev down |
| 200 | |
| 201 | if [ -n "$vid" ]; then |
| 202 | ip link delete $pi_ifname |
| 203 | fi |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | preinit_net_echo() { |
| 208 | [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && { |
| 209 | { |
| 210 | [ "$pi_preinit_net_messages" = "y" ] || { |
| 211 | [ "$pi_failsafe_net_message" = "true" ] && |
| 212 | [ "$pi_preinit_no_failsafe_netmsg" != "y" ] |
| 213 | } |
| 214 | } && netmsg $pi_broadcast "$1" |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | pi_indicate_preinit() { |
| 219 | set_state preinit |
| 220 | } |
| 221 | |
| 222 | boot_hook_add preinit_main preinit_ip |
| 223 | boot_hook_add preinit_main pi_indicate_preinit |