c0f73da12a6e79ee6a97116e99ec7d1136b98da2
[platform/upstream/dracut.git] / modules.d / 40network / net-lib.sh
1 #!/bin/sh
2
3 get_ip() {
4     local iface="$1" ip=""
5     ip=$(ip -o -f inet addr show $iface)
6     ip=${ip%%/*}
7     ip=${ip##* }
8 }
9
10 iface_for_remote_addr() {
11     set -- $(ip -o route get to $1)
12     echo $5
13 }
14
15 iface_for_mac() {
16     local interface="" mac="$(echo $1 | tr '[:upper:]' '[:lower:]')"
17     for interface in /sys/class/net/*; do
18         if [ $(cat $interface/address) = "$mac" ]; then
19             echo ${interface##*/}
20         fi
21     done
22 }
23
24 iface_has_link() {
25     local interface="$1" flags=""
26     [ -n "$interface" ] || return 2
27     interface="/sys/class/net/$interface"
28     [ -d "$interface" ] || return 2
29     flags=$(cat $interface/flags)
30     echo $(($flags|0x41)) > $interface/flags # 0x41: IFF_UP|IFF_RUNNING
31     [ "$(cat $interface/carrier)" = 1 ] || return 1
32     # XXX Do we need to reset the flags here? anaconda never bothered..
33 }
34
35 all_ifaces_up() {
36     local iface="" IFACES=""
37     [ -e "/tmp/net.ifaces" ] && read IFACES < /tmp/net.ifaces
38     for iface in $IFACES; do
39         [ -e /tmp/net.$iface.up ] || return 1
40     done
41 }
42
43 get_netroot_ip() {
44     local prefix="" server="" rest=""
45     splitsep "$1" ":" prefix server rest
46     case $server in
47         [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*) echo "$server"; return 0 ;;
48     esac
49     return 1
50 }
51
52 ip_is_local() {
53     strstr "$(ip route get $1 2>/dev/null)" " via "
54 }
55
56 ifdown() {
57     local netif="$1"
58     # ip down/flush ensures that routing info goes away as well
59     ip link set $netif down
60     ip addr flush dev $netif
61     echo "#empty" > /etc/resolv.conf
62     rm -f /tmp/net.$netif.did-setup
63     # TODO: send "offline" uevent?
64 }
65
66 setup_net() {
67     local netif="$1" f="" gw_ip="" netroot_ip="" iface="" IFACES=""
68     [ -e /tmp/net.$netif.up ] || return 1
69     [ -e /tmp/net.$netif.did-setup ] && return
70     [ -e "/tmp/net.ifaces" ] && read IFACES < /tmp/net.ifaces
71     [ -z "$IFACES" ] && IFACES="$netif"
72     for iface in $IFACES ; do
73         . /tmp/net.$iface.up
74     done
75     # run the scripts written by ifup
76     [ -e /tmp/net.$netif.gw ]            && . /tmp/net.$netif.gw
77     [ -e /tmp/net.$netif.hostname ]      && . /tmp/net.$netif.hostname
78     [ -e /tmp/net.$netif.override ]      && . /tmp/net.$netif.override
79     [ -e /tmp/dhclient.$netif.dhcpopts ] && . /tmp/dhclient.$netif.dhcpopts
80     # set up resolv.conf
81     [ -e /tmp/net.$netif.resolv.conf ] && \
82         cp -f /tmp/net.$netif.resolv.conf /etc/resolv.conf
83
84     # Handle STP Timeout: arping the default gateway.
85     # (or the root server, if a) it's local or b) there's no gateway.)
86     # Note: This assumes that if no router is present the
87     # root server is on the same subnet.
88
89     # Get DHCP-provided router IP, or the cmdline-provided "gw=" argument
90     [ -n "$new_routers" ] && gw_ip=${new_routers%%,*}
91     [ -n "$gw" ] && gw_ip=$gw
92
93     # Get the "netroot" IP (if there's an IP address in there)
94     netroot_ip=$(get_netroot_ip $netroot)
95
96     # try netroot if it's local (or there's no gateway)
97     if ip_is_local $netroot_ip || [ -z "$gw_ip" ]; then
98         dest="$netroot_ip"
99     else
100         dest="$gw_ip"
101     fi
102     if [ -n "$dest" ] && ! arping -q -f -w 60 -I $netif $dest ; then
103         info "Resolving $dest via ARP on $netif failed"
104     fi
105     > /tmp/net.$netif.did-setup
106 }
107
108 save_netinfo() {
109     local netif="$1" IFACES="" f="" i=""
110     [ -e /tmp/net.ifaces ] && read IFACES < /tmp/net.ifaces
111     # Add $netif to the front of IFACES (if it's not there already).
112     set -- "$netif"
113     for i in $IFACES; do [ "$i" != "$netif" ] && set -- "$@" "$i"; done
114     IFACES="$*"
115     for i in $IFACES; do
116         for f in /tmp/dhclient.$i.*; do
117             [ -f $f ] && cp -f $f /tmp/net.${f#/tmp/dhclient.}
118         done
119     done
120     echo $IFACES > /tmp/.net.ifaces.new
121     mv /tmp/.net.ifaces.new /tmp/net.ifaces
122 }
123
124 set_ifname() {
125     local name="$1" mac="$2" num=0 n=""
126     # if it's already set, return the existing name
127     for n in $(getargs ifname=); do
128         strstr "$n" "$mac" && echo ${n%%:*} && return
129     done
130     # otherwise, pick a new name and use that
131     while [ -e /sys/class/$name$num ]; do num=$(($num+1)); done
132     echo "ifname=$name$num:$mac" >> /etc/cmdline.d/45-ifname.conf
133     echo "$name$num"
134 }
135
136 ibft_to_cmdline() {
137     local iface="" mac="" dev=""
138     local dhcp="" ip="" gw="" mask="" hostname=""
139     modprobe -q iscsi_ibft
140     (
141         for iface in /sys/firmware/ibft/ethernet*; do
142             [ -e ${iface}/mac ] || continue
143             mac=$(read a < ${iface}/mac; echo $a)
144             [ -z "$ifname_mac" ] && continue
145             dev=$(set_ifname ibft $ifname_mac)
146             dhcp=$(read a < ${iface}/dhcp; echo $a)
147             if [ -n "$dhcp" ]; then
148                 echo "ip=$dev:dhcp"
149             else
150                 ip=$(read a < ${iface}/ip-addr; echo $a)
151                 gw=$(read a < ${iface}/gateway; echo $a)
152                 mask=$(read a < ${iface}/subnet-mask; echo $a)
153                 hostname=$(read a < ${iface}/hostname; echo $a)
154                 echo "ip=$ip::$gw:$mask:$hostname:$dev:none"
155             fi
156         done
157     ) >> /etc/cmdline.d/40-ibft.conf
158     # reread cmdline
159     unset CMDLINE
160 }
161
162 parse_iscsi_root()
163 {
164     local v
165     v=${1#iscsi:}
166
167 # extract authentication info
168     case "$v" in
169         *@*:*:*:*:*)
170             authinfo=${v%%@*}
171             v=${v#*@}
172     # allow empty authinfo to allow having an @ in iscsi_target_name like this:
173     # netroot=iscsi:@192.168.1.100::3260::iqn.2009-01.com.example:testdi@sk
174             if [ -n "$authinfo" ]; then
175                 OLDIFS="$IFS"
176                 IFS=:
177                 set $authinfo
178                 IFS="$OLDIFS"
179                 if [ $# -gt 4 ]; then
180                     warn "Wrong authentication info in iscsi: parameter!"
181                     return 1
182                 fi
183                 iscsi_username=$1
184                 iscsi_password=$2
185                 if [ $# -gt 2 ]; then
186                     iscsi_in_username=$3
187                     iscsi_in_password=$4
188                 fi
189             fi
190             ;;
191     esac
192
193 # extract target ip
194     case "$v" in
195         [[]*[]]:*)
196             iscsi_target_ip=${v#[[]}
197                 iscsi_target_ip=${iscsi_target_ip%%[]]*}
198             v=${v#[[]$iscsi_target_ip[]]:}
199             ;;
200         *)
201             iscsi_target_ip=${v%%[:]*}
202             v=${v#$iscsi_target_ip:}
203             ;;
204     esac
205
206 # extract target name
207     case "$v" in
208         *:iqn.*)
209             iscsi_target_name=iqn.${v##*:iqn.}
210             v=${v%:iqn.*}:
211             ;;
212         *:eui.*)
213             iscsi_target_name=iqn.${v##*:eui.}
214             v=${v%:iqn.*}:
215             ;;
216         *:naa.*)
217             iscsi_target_name=iqn.${v##*:naa.}
218             v=${v%:iqn.*}:
219             ;;
220         *)
221             warn "Invalid iscii target name, should begin with 'iqn.' or 'eui.' or 'naa.'"
222             return 1
223             ;;
224     esac
225
226 # parse the rest
227     OLDIFS="$IFS"
228     IFS=:
229     set $v
230     IFS="$OLDIFS"
231
232     iscsi_protocol=$1; shift # ignored
233     iscsi_target_port=$1; shift
234     if [ $# -eq 3 ]; then
235         iscsi_iface_name=$1; shift
236     fi
237     if [ $# -eq 2 ]; then
238         iscsi_netdev_name=$1; shift
239     fi
240     iscsi_lun=$1; shift
241     if [ $# -ne 0 ]; then
242         warn "Invalid parameter in iscsi: parameter!"
243         return 1
244     fi
245 }
246
247 ip_to_var() {
248     local v=${1}:
249     local i
250     set --
251     while [ -n "$v" ]; do
252         if [ "${v#\[*:*:*\]:}" != "$v" ]; then
253             # handle IPv6 address
254             i="${v%%\]:*}"
255             i="${i##\[}"
256             set -- "$@" "$i"
257             v=${v#\[$i\]:}
258         else
259             set -- "$@" "${v%%:*}"
260             v=${v#*:}
261         fi
262     done
263
264     unset ip srv gw mask hostname dev autoconf macaddr mtu
265     case $# in
266         0)  autoconf="error" ;;
267         1)  autoconf=$1 ;;
268         2)  dev=$1; autoconf=$2 ;;
269         3)  dev=$1; autoconf=$2; mtu=$3 ;;
270         4)  dev=$1; autoconf=$2; mtu=$3; macaddr=$4 ;;
271         *)  ip=$1; srv=$2; gw=$3; mask=$4; hostname=$5; dev=$6; autoconf=$7; mtu=$8; macaddr=$9 ;;
272     esac
273 }