xref: /openbmc/ipmitool/contrib/bmclanconf (revision c18ec02f)
1#!/bin/bash
2#
3# Copyright (c) 2003 Sun Microsystems, Inc.  All Rights Reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8#
9# Redistribution of source code must retain the above copyright
10# notice, this list of conditions and the following disclaimer.
11#
12# Redistribution in binary form must reproduce the above copyright
13# notice, this list of conditions and the following disclaimer in the
14# documentation and/or other materials provided with the distribution.
15#
16# Neither the name of Sun Microsystems, Inc. or the names of
17# contributors may be used to endorse or promote products derived
18# from this software without specific prior written permission.
19#
20# This software is provided "AS IS," without a warranty of any kind.
21# ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
22# INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
23# PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
24# SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE
25# FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
26# OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.  IN NO EVENT WILL
27# SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA,
28# OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
29# PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
30# LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
31# EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32
33
34# This script is designed for Sun Fire LX50, V60x and V65x systems.
35# It may work with other setups as long as you use the correct channel
36# and interface settings.  It can be used on the V20z as long as you
37# specify network settings on the command line.
38#
39# If the wrong channel is used you could lose network access to the
40# box because your BMC will be sending out bad Gratuitous ARP packets
41# with the wrong MAC address.  You will need to use a console or
42# override your local ARP cache with the correct addr.
43
44usage ()
45{
46    echo "
47usage: $0 -c <chan> -i <interface> [options]
48       $0 -c <chan> [-v|w|x|y|z address] [options]
49
50    -c channel     Which BMC channel to configure [-c help for more info]
51    -i interface   Auto-configure BMC based on interface network settings
52    -v address     Use specified IP address
53    -w address     Use specified Netmask address
54    -x address     Use specified MAC address
55    -y address     Use specified Gateway IP address
56    -z address     Use specified Gateway MAC address
57    -m interface   Which IPMI interface to use [default linux=open solaris=lipmi]
58    -p password    Set BMC channel password
59    -s string      Set SNMP community string
60    -a interval    Set Gratuitous ARP interval, off=0, default=4
61    -r             Reset BMC channel, zero all network settings, disable
62    -d             Debug mode, does not make any changes
63"
64    exit 0;
65}
66
67channel_usage ()
68{
69    echo "
70 Sun Fire V60x and V65x - Linux 2.4
71   eth0 = channel 6, top
72   eth1 = channel 7, bottom
73
74 Sun Fire V60x and V65x - Linux 2.6
75   eth0 = channel 7, bottom
76   eth1 = channel 6, top
77
78 Sun Fire V60x and V65x - Solaris x86
79   e1000g0 = channel 7, bottom
80   e1000g1 = channel 6, top
81
82 Sun LX50 - Linux
83   eth0 = channel 7, bottom
84   eth1 = channel 6, top
85
86 Sun LX50 - Solaris x86
87   le0 = channel 7, bottom
88   le1 = channel 6, top
89
90 Sun Fire V20z (-i option does not apply)
91   channel 1
92"
93    exit 0
94}
95
96hex_to_ip ()
97{
98    test $# -ge 1 || return;
99    local HEX B1 B2 B3 B4;
100    HEX=`echo $1 | tr '[:lower:]' '[:upper:]'`;
101    H1=`echo $HEX | cut -c1-2`
102    H2=`echo $HEX | cut -c3-4`
103    H3=`echo $HEX | cut -c5-6`
104    H4=`echo $HEX | cut -c7-8`
105    B1=`echo 10 o 16 i $H1 p | dc`
106    B2=`echo 10 o 16 i $H2 p | dc`
107    B3=`echo 10 o 16 i $H3 p | dc`
108    B4=`echo 10 o 16 i $H4 p | dc`
109    echo "$B1.$B2.$B3.$B4"
110}
111
112ipmitool_lan_set ()
113{
114    [ $# -lt 1 ] && return
115    local PARAM=$1
116    local VALUE=
117    [ $# -ge 2 ] && VALUE=$2
118
119    if [ $DEBUG -gt 0 ]; then
120	echo "Setting LAN parameter $PARAM $VALUE"
121	echo "$IPMITOOL -I $IPMIINTF lan set $CHANNEL $PARAM $VALUE"
122	return
123    fi
124
125    $IPMITOOL -I $IPMIINTF lan set $CHANNEL $PARAM $VALUE
126}
127
128ipmitool_lan_reset ()
129{
130    ipmitool_lan_set "ipsrc" "static"
131    ipmitool_lan_set "ipaddr" "0.0.0.0"
132    ipmitool_lan_set "netmask" "0.0.0.0"
133    ipmitool_lan_set "macaddr" "00:00:00:00:00:00"
134    ipmitool_lan_set "defgw ipaddr" "0.0.0.0"
135    ipmitool_lan_set "defgw macaddr" "00:00:00:00:00:00"
136    ipmitool_lan_set "password"
137    ipmitool_lan_set "snmp" "public"
138    ipmitool_lan_set "arp generate" "off"
139    ipmitool_lan_set "access" "off"
140    exit 0
141}
142
143DEBUG=0
144LINUX=0
145SOLARIS=0
146CHANNEL=0
147IFACE=
148PASSWORD=
149SNMP=
150GRATARP=8
151PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin
152
153case `uname -s` in
154    Linux)
155	IPMIINTF=open
156	IPMITOOL=ipmitool
157	PING=ping
158	IFCONFIG=ifconfig
159	ARP=arp
160	ROUTE=route
161	;;
162    SunOS)
163	IPMIINTF=lipmi
164	IPMITOOL=ipmiadm
165	PING=ping
166	IFCONFIG=ifconfig
167	ARP=arp
168	ROUTE=route
169	;;
170    *)
171	echo "Invalid OS!"
172	exit 1
173esac
174
175while getopts "dhri:c:m:p:s:a:v:w:x:y:z:" OPTION ; do
176    case "$OPTION" in
177	i) IFACE=$OPTARG ;;
178	c) CHANNEL=$OPTARG ; test X$CHANNEL = Xhelp && channel_usage ;;
179	m) IPMIINTF=$OPTARG ;;
180	p) PASSWORD=$OPTARG ;;
181	s) SNMP=$OPTARG ;;
182	a) GRATARP=$OPTARG ;;
183	d) DEBUG=1 ;;
184	v) IP_ADDRESS=$OPTARG ;;
185	w) IP_NETMASK=$OPTARG ;;
186	x) MAC_ADDRESS=$OPTARG ;;
187	y) GATEWAY_IP=$OPTARG ;;
188	z) GATEWAY_MAC=$OPTARG ;;
189	r) ipmitool_lan_reset ;;
190	h) usage ;;
191	*) echo "Ignoring invalid option : -$OPTARG" ;;
192    esac
193done
194
195if [ ! -x `which $IPMITOOL` ]; then
196    echo "Error: unable to find $IPMITOOL"
197    exit 1
198fi
199
200if [ $CHANNEL -eq 0 ]; then
201    echo
202    echo "Error: you must specify a channel with -c"
203    echo
204    exit 1
205fi
206
207if [ "$IFACE" ]; then
208    if ! $IFCONFIG $IFACE 2>/dev/null | grep "inet " >/dev/null 2>&1 ; then
209	echo
210	echo "Error: unable to find interface $IFACE"
211	echo
212	exit 1
213    fi
214    echo "Auto-configuring $IFACE (channel $CHANNEL)"
215fi
216
217case `uname -s` in
218    SunOS)
219	if [ X$IFACE != X ]; then
220	    if [ X$IP_ADDRESS = X ]; then
221		IP_ADDRESS=`$IFCONFIG $IFACE | grep inet | awk '{print $2}'`
222	    fi
223	    if [ X$IP_NETMASK = X ]; then
224		HEX_NETMASK=`$IFCONFIG $IFACE | grep netmask | awk '{print $4}'`
225		IP_NETMASK=`hex_to_ip $HEX_NETMASK`
226	    fi
227	    if [ X$MAC_ADDRESS = X ]; then
228		MAC_ADDRESS=`$IFCONFIG $IFACE | grep ether | awk '{print $2}'`
229	    fi
230	    if [ X$GATEWAY_IP = X ]; then
231		GATEWAY_IP=`$ROUTE -n get default | grep gateway: | awk '{print $2}'`
232	    fi
233	    if [ X$GATEWAY_MAC = X ]; then
234		$PING -i $IFACE $GATEWAY_IP 1 >/dev/null 2>&1
235		GATEWAY_MAC=`$ARP $GATEWAY_IP | awk '{print $4}'`
236		if [ X$GATEWAY_MAC = Xno ]; then
237		    GATEWAY_MAC=
238		fi
239	    fi
240	fi
241	;;
242
243    Linux)
244	if [ X$IFACE != X ]; then
245	    if [ X$IP_ADDRESS = X ]; then
246		IP_ADDRESS=`$IFCONFIG $IFACE | grep "inet addr" | awk -F"[:[:space:]]+" '{print $4}'`
247	    fi
248	    if [ X$IP_NETMASK = X ]; then
249		IP_NETMASK=`$IFCONFIG $IFACE | grep Bcast | awk -F"[:[:space:]]+" '{print $8}'`
250	    fi
251	    if [ X$MAC_ADDRESS = X ]; then
252		MAC_ADDRESS=`$IFCONFIG $IFACE | grep HWaddr | awk '{print $5}'`
253	    fi
254	    if [ X$GATEWAY_IP = X ]; then
255		GATEWAY_IP=`$ROUTE -n | awk '/^0.0.0.0/ {print $2}'`
256	    fi
257	    if [ X$GATEWAY_MAC = X ]; then
258		$PING -q -c1 $GATEWAY_IP >/dev/null 2>&1
259		GATEWAY_MAC=`$ARP -an | grep "$GATEWAY_IP[^0-9]" | awk '{print $4}'`
260	    fi
261	fi
262	;;
263esac
264
265if [ X$IP_ADDRESS != X ]; then
266    ipmitool_lan_set "ipsrc" "static"
267    ipmitool_lan_set "ipaddr" "$IP_ADDRESS"
268fi
269
270if [ X$IP_NETMASK != X ]; then
271    ipmitool_lan_set "netmask" "$IP_NETMASK"
272fi
273
274if [ X$MAC_ADDRESS != X ]; then
275    ipmitool_lan_set "macaddr" "$MAC_ADDRESS"
276fi
277
278if [ X$GATEWAY_IP != X ]; then
279    ipmitool_lan_set "defgw ipaddr" "$GATEWAY_IP"
280fi
281
282if [ X$GATEWAY_MAC != X ]; then
283    ipmitool_lan_set "defgw macaddr" "$GATEWAY_MAC"
284fi
285
286if [ X$PASSWORD != X ]; then
287    ipmitool_lan_set "password" "$PASSWORD"
288fi
289
290if [ X$SNMP != X ]; then
291    ipmitool_lan_set "snmp" "$SNMP"
292fi
293
294if [ "$GRATARP" -ne 0 ]; then
295    ipmitool_lan_set "arp generate" "on"
296    ipmitool_lan_set "arp interval" "$GRATARP"
297else
298    ipmitool_lan_set "arp generate" "off"
299fi
300
301echo "Setting channel authentication capabilities"
302ipmitool_lan_set "auth callback,user,operator,admin" "md2,md5"
303
304echo "Enabling channel $CHANNEL"
305ipmitool_lan_set "access" "on"
306ipmitool_lan_set "user"
307
308exit 0
309