1 # Copyright (C) 2006 OpenWrt.org
2 # Copyright (C) 2010 Vertical Communications
10 if [ "$vid" = "$netdev" ]; then
14 grep -q "$netdev" /proc/net/dev || return
16 if [ -n "$vid" ]; then
17 ip link add link $netdev name $1 type vlan id $vid
20 ip link set dev $netdev up
21 if [ -n "$vid" ]; then
24 ip -4 address add $pi_ip/$pi_netmask broadcast $pi_broadcast dev $1
27 preinit_config_switch() {
28 local role roles ports device enable reset
36 json_get_vars enable reset
38 if [ "$reset" -eq "1" ]; then
39 swconfig dev $name set reset
41 swconfig dev $name set enable_vlan $enable
43 if json_is_a roles array; then
44 json_get_keys roles roles
47 for role in $roles; do
49 json_get_vars ports device
52 if [ "$device" = "$lan_if" ]; then
53 swconfig dev $name vlan $role set ports "$ports"
60 swconfig dev $name set apply
66 preinit_config_port() {
72 [ -d "/sys/devices/$path/net" ] || return
73 original="$(ls "/sys/devices/$path/net" | head -1)"
75 [ "$netdev" = "$original" ] && return
77 ip link set "$original" name "$netdev"
80 preinit_config_board() {
81 /bin/board_detect /tmp/board.json
83 [ -f "/tmp/board.json" ] || return
85 . /usr/share/libubox/jshn.sh
88 json_load "$(cat /tmp/board.json)"
90 # Find the current highest eth*
91 max_eth=$(grep -o '^ *eth[0-9]*:' /proc/net/dev | tr -dc '[0-9]\n' | sort -n | tail -1)
92 # Find and move netdevs using eth*s we are configuring
93 json_get_keys keys "network_device"
94 for netdev in $keys; do
95 json_select "network_device"
97 json_get_vars path path
98 next_eth="$(echo "$netdev" | grep 'eth[0-9]*' | tr -dc '[0-9]')"
99 [ "$next_eth" -gt "$max_eth" ] && max_eth=$next_eth
100 if [ -n "$path" -a -h "/sys/class/net/$netdev" ]; then
101 ip link set "$netdev" down
102 ip link set "$netdev" name eth$((++max_eth))
108 # Move interfaces by path to their netdev name
109 json_get_keys keys "network_device"
110 for netdev in $keys; do
111 json_select "network_device"
112 json_select "$netdev"
113 json_get_vars path path
114 [ -n "$path" ] && preinit_config_port "$netdev" "$path"
122 json_get_values ports ports
126 [ -n "$device" -o -n "$ports" ] || return
128 # swconfig uses $device and DSA uses ports
133 # only use the first one
136 if [ -x /sbin/swconfig ]; then
137 # configure the switch, if present
139 json_get_keys keys switch
141 preinit_config_switch $key $ifname
146 # trim any vlan modifiers like :t
152 preinit_ip_config $pi_ifname
156 [ "$pi_preinit_no_failsafe" = "y" ] && return
158 # if the preinit interface isn't specified and ifname is set in
159 # preinit.arch use that interface
160 if [ -z "$pi_ifname" ]; then
164 if [ -n "$pi_ifname" ]; then
165 preinit_ip_config $pi_ifname
166 elif [ -d "/etc/board.d/" ]; then
170 preinit_net_echo "Doing OpenWrt Preinit\n"
173 preinit_ip_deconfig() {
174 [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && {
177 netdev=${pi_ifname%\.*}
180 if [ "$vid" = "$netdev" ]; then
184 ip -4 address flush dev $pi_ifname
185 ip link set dev $netdev down
187 if [ -n "$vid" ]; then
188 ip link delete $pi_ifname
194 [ -n "$pi_ifname" ] && grep -q "$pi_ifname" /proc/net/dev && {
196 [ "$pi_preinit_net_messages" = "y" ] || {
197 [ "$pi_failsafe_net_message" = "true" ] &&
198 [ "$pi_preinit_no_failsafe_netmsg" != "y" ]
200 } && netmsg $pi_broadcast "$1"
204 pi_indicate_preinit() {
208 boot_hook_add preinit_main preinit_ip
209 boot_hook_add preinit_main pi_indicate_preinit