Page Menu
Home
ClusterLabs Projects
Search
Configure Global Search
Log In
Files
F3151631
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
86 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/heartbeat/IPaddr2 b/heartbeat/IPaddr2
index 2d2ba2ce2..5fd8dba11 100755
--- a/heartbeat/IPaddr2
+++ b/heartbeat/IPaddr2
@@ -1,1174 +1,1238 @@
#!/bin/sh
#
# $Id: IPaddr2.in,v 1.24 2006/08/09 13:01:54 lars Exp $
#
# OCF Resource Agent compliant IPaddr2 script.
#
# Based on work by Tuomo Soini, ported to the OCF RA API by Lars
# Marowsky-Brée. Implements Cluster Alias IP functionality too.
#
# Cluster Alias IP cleanup, fixes and testing by Michael Schwartzkopff
#
#
# Copyright (c) 2003 Tuomo Soini
# Copyright (c) 2004-2006 SUSE LINUX AG, Lars Marowsky-Brée
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like. Any license provided herein, whether implied or
# otherwise, applies only to this software file. Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#
#
# TODO:
# - There ought to be an ocf_run_cmd function which does all logging,
# timeout handling etc for us
# - Make this the standard IP address agent on Linux; the other
# platforms simply should ignore the additional parameters OR can use
# the legacy heartbeat resource script...
# - Check LVS <-> clusterip incompatibilities.
#
# OCF parameters are as below
# OCF_RESKEY_ip
# OCF_RESKEY_broadcast
# OCF_RESKEY_nic
# OCF_RESKEY_cidr_netmask
# OCF_RESKEY_iflabel
# OCF_RESKEY_mac
# OCF_RESKEY_clusterip_hash
# OCF_RESKEY_arp_interval
# OCF_RESKEY_arp_count
# OCF_RESKEY_arp_bg
-# OCF_RESKEY_arp_mac
# OCF_RESKEY_preferred_lft
#
# OCF_RESKEY_CRM_meta_clone
# OCF_RESKEY_CRM_meta_clone_max
#######################################################################
# Initialization:
: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs
. ${OCF_FUNCTIONS_DIR}/findif.sh
# Defaults
OCF_RESKEY_lvs_support_default=false
OCF_RESKEY_lvs_ipv6_addrlabel_default=false
OCF_RESKEY_lvs_ipv6_addrlabel_value_default=99
OCF_RESKEY_clusterip_hash_default="sourceip-sourceport"
OCF_RESKEY_unique_clone_address_default=false
OCF_RESKEY_arp_interval_default=200
OCF_RESKEY_arp_count_default=5
OCF_RESKEY_arp_count_refresh_default=0
OCF_RESKEY_arp_bg_default=true
-OCF_RESKEY_arp_mac_default="ffffffffffff"
OCF_RESKEY_run_arping_default=false
OCF_RESKEY_preferred_lft_default="forever"
: ${OCF_RESKEY_lvs_support=${OCF_RESKEY_lvs_support_default}}
: ${OCF_RESKEY_lvs_ipv6_addrlabel=${OCF_RESKEY_lvs_ipv6_addrlabel_default}}
: ${OCF_RESKEY_lvs_ipv6_addrlabel_value=${OCF_RESKEY_lvs_ipv6_addrlabel_value_default}}
: ${OCF_RESKEY_clusterip_hash=${OCF_RESKEY_clusterip_hash_default}}
: ${OCF_RESKEY_unique_clone_address=${OCF_RESKEY_unique_clone_address_default}}
: ${OCF_RESKEY_arp_interval=${OCF_RESKEY_arp_interval_default}}
: ${OCF_RESKEY_arp_count=${OCF_RESKEY_arp_count_default}}
: ${OCF_RESKEY_arp_count_refresh=${OCF_RESKEY_arp_count_refresh_default}}
: ${OCF_RESKEY_arp_bg=${OCF_RESKEY_arp_bg_default}}
-: ${OCF_RESKEY_arp_mac=${OCF_RESKEY_arp_mac_default}}
: ${OCF_RESKEY_run_arping=${OCF_RESKEY_run_arping_default}}
: ${OCF_RESKEY_preferred_lft=${OCF_RESKEY_preferred_lft_default}}
#######################################################################
SENDARP=$HA_BIN/send_arp
SENDUA=$HA_BIN/send_ua
FINDIF=findif
VLDIR=$HA_RSCTMP
SENDARPPIDDIR=$HA_RSCTMP
CIP_lockfile=$HA_RSCTMP/IPaddr2-CIP-${OCF_RESKEY_ip}
#######################################################################
meta_data() {
cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="IPaddr2">
<version>1.0</version>
<longdesc lang="en">
This Linux-specific resource manages IP alias IP addresses.
It can add an IP alias, or remove one.
In addition, it can implement Cluster Alias IP functionality
if invoked as a clone resource.
If used as a clone, you should explicitly set clone-node-max >= 2,
and/or clone-max < number of nodes. In case of node failure,
clone instances need to be re-allocated on surviving nodes.
This would not be possible if there is already an instance on those nodes,
and clone-node-max=1 (which is the default).
</longdesc>
<shortdesc lang="en">Manages virtual IPv4 and IPv6 addresses (Linux specific version)</shortdesc>
<parameters>
<parameter name="ip" unique="1" required="1">
<longdesc lang="en">
The IPv4 (dotted quad notation) or IPv6 address (colon hexadecimal notation)
example IPv4 "192.168.1.1".
example IPv6 "2001:db8:DC28:0:0:FC57:D4C8:1FFF".
</longdesc>
<shortdesc lang="en">IPv4 or IPv6 address</shortdesc>
<content type="string" default="" />
</parameter>
<parameter name="nic" unique="0">
<longdesc lang="en">
The base network interface on which the IP address will be brought
online.
If left empty, the script will try and determine this from the
routing table.
Do NOT specify an alias interface in the form eth0:1 or anything here;
rather, specify the base interface only.
If you want a label, see the iflabel parameter.
Prerequisite:
There must be at least one static IP address, which is not managed by
the cluster, assigned to the network interface.
If you can not assign any static IP address on the interface,
modify this kernel parameter:
sysctl -w net.ipv4.conf.all.promote_secondaries=1 # (or per device)
</longdesc>
<shortdesc lang="en">Network interface</shortdesc>
<content type="string"/>
</parameter>
<parameter name="cidr_netmask">
<longdesc lang="en">
The netmask for the interface in CIDR format
(e.g., 24 and not 255.255.255.0)
If unspecified, the script will also try to determine this from the
routing table.
</longdesc>
<shortdesc lang="en">CIDR netmask</shortdesc>
<content type="string" default=""/>
</parameter>
<parameter name="broadcast">
<longdesc lang="en">
Broadcast address associated with the IP. It is possible to use the
special symbols '+' and '-' instead of the broadcast address. In this
case, the broadcast address is derived by setting/resetting the host
bits of the interface prefix.
</longdesc>
<shortdesc lang="en">Broadcast address</shortdesc>
<content type="string" default=""/>
</parameter>
<parameter name="iflabel">
<longdesc lang="en">
You can specify an additional label for your IP address here.
This label is appended to your interface name.
The kernel allows alphanumeric labels up to a maximum length of 15
characters including the interface name and colon (e.g. eth0:foobar1234)
A label can be specified in nic parameter but it is deprecated.
If a label is specified in nic name, this parameter has no effect.
</longdesc>
<shortdesc lang="en">Interface label</shortdesc>
<content type="string" default=""/>
</parameter>
<parameter name="lvs_support">
<longdesc lang="en">
Enable support for LVS Direct Routing configurations. In case a IP
address is stopped, only move it to the loopback device to allow the
local node to continue to service requests, but no longer advertise it
on the network.
Notes for IPv6:
It is not necessary to enable this option on IPv6.
Instead, enable 'lvs_ipv6_addrlabel' option for LVS-DR usage on IPv6.
</longdesc>
<shortdesc lang="en">Enable support for LVS DR</shortdesc>
<content type="boolean" default="${OCF_RESKEY_lvs_support_default}"/>
</parameter>
<parameter name="lvs_ipv6_addrlabel">
<longdesc lang="en">
Enable adding IPv6 address label so IPv6 traffic originating from
the address's interface does not use this address as the source.
This is necessary for LVS-DR health checks to realservers to work. Without it,
the most recently added IPv6 address (probably the address added by IPaddr2)
will be used as the source address for IPv6 traffic from that interface and
since that address exists on loopback on the realservers, the realserver
response to pings/connections will never leave its loopback.
See RFC3484 for the detail of the source address selection.
See also 'lvs_ipv6_addrlabel_value' parameter.
</longdesc>
<shortdesc lang="en">Enable adding IPv6 address label.</shortdesc>
<content type="boolean" default="${OCF_RESKEY_lvs_ipv6_addrlabel_default}"/>
</parameter>
<parameter name="lvs_ipv6_addrlabel_value">
<longdesc lang="en">
Specify IPv6 address label value used when 'lvs_ipv6_addrlabel' is enabled.
The value should be an unused label in the policy table
which is shown by 'ip addrlabel list' command.
You would rarely need to change this parameter.
</longdesc>
<shortdesc lang="en">IPv6 address label value.</shortdesc>
<content type="integer" default="${OCF_RESKEY_lvs_ipv6_addrlabel_value_default}"/>
</parameter>
<parameter name="mac">
<longdesc lang="en">
Set the interface MAC address explicitly. Currently only used in case of
the Cluster IP Alias. Leave empty to chose automatically.
</longdesc>
<shortdesc lang="en">Cluster IP MAC address</shortdesc>
<content type="string" default=""/>
</parameter>
<parameter name="clusterip_hash">
<longdesc lang="en">
Specify the hashing algorithm used for the Cluster IP functionality.
</longdesc>
<shortdesc lang="en">Cluster IP hashing function</shortdesc>
<content type="string" default="${OCF_RESKEY_clusterip_hash_default}"/>
</parameter>
<parameter name="unique_clone_address">
<longdesc lang="en">
If true, add the clone ID to the supplied value of IP to create
a unique address to manage
</longdesc>
<shortdesc lang="en">Create a unique address for cloned instances</shortdesc>
<content type="boolean" default="${OCF_RESKEY_unique_clone_address_default}"/>
</parameter>
<parameter name="arp_interval">
<longdesc lang="en">
Specify the interval between unsolicited ARP packets in milliseconds.
+
+This parameter is deprecated and used for the backward compatibility only.
+It is effective only for the send_arp binary which is built with libnet,
+and send_ua for IPv6. It has no effect for other arp_sender.
</longdesc>
-<shortdesc lang="en">ARP packet interval in ms</shortdesc>
+<shortdesc lang="en">ARP packet interval in ms (deprecated)</shortdesc>
<content type="integer" default="${OCF_RESKEY_arp_interval_default}"/>
</parameter>
<parameter name="arp_count">
<longdesc lang="en">
Number of unsolicited ARP packets to send at resource initialization.
</longdesc>
<shortdesc lang="en">ARP packet count sent during initialization</shortdesc>
<content type="integer" default="${OCF_RESKEY_arp_count_default}"/>
</parameter>
<parameter name="arp_count_refresh">
<longdesc lang="en">
Number of unsolicited ARP packets to send during resource monitoring. Doing
so helps mitigate issues of stuck ARP caches resulting from split-brain
situations.
</longdesc>
<shortdesc lang="en">ARP packet count sent during monitoring</shortdesc>
<content type="integer" default="${OCF_RESKEY_arp_count_refresh_default}"/>
</parameter>
<parameter name="arp_bg">
<longdesc lang="en">
Whether or not to send the ARP packets in the background.
</longdesc>
<shortdesc lang="en">ARP from background</shortdesc>
<content type="string" default="${OCF_RESKEY_arp_bg_default}"/>
</parameter>
-<parameter name="arp_mac">
+<parameter name="arp_sender">
<longdesc lang="en">
-MAC address to send the ARP packets to.
-
-You really shouldn't be touching this.
-
+The program to send ARP packets with on start. Available options are:
+ - send_arp: default
+ - ipoibarping: default for infiniband interfaces if ipoibarping is available
+ - iputils_arping: use arping in iputils package
+ - libnet_arping: use another variant of arping based on libnet
</longdesc>
-<shortdesc lang="en">ARP MAC</shortdesc>
-<content type="string" default="${OCF_RESKEY_arp_mac_default}"/>
+<shortdesc lang="en">ARP sender</shortdesc>
+<content type="string" default=""/>
</parameter>
-<parameter name="arp_sender">
+<parameter name="send_arp_opts">
<longdesc lang="en">
-The program to send ARP packets with on start. For infiniband
-interfaces, default is ipoibarping. If ipoibarping is not
-available, set this to send_arp.
+Extra options to pass to the arp_sender program.
+Available options are vary depending on which arp_sender is used.
+
+A typical use case is specifying '-A' for iputils_arping to use
+ARP REPLY instead of ARP REQUEST as Gratuitous ARPs.
</longdesc>
-<shortdesc lang="en">ARP sender</shortdesc>
+<shortdesc lang="en">Options for ARP sender</shortdesc>
<content type="string" default=""/>
</parameter>
<parameter name="flush_routes">
<longdesc lang="en">
Flush the routing table on stop. This is for
applications which use the cluster IP address
and which run on the same physical host that the
IP address lives on. The Linux kernel may force that
application to take a shortcut to the local loopback
interface, instead of the interface the address
is really bound to. Under those circumstances, an
application may, somewhat unexpectedly, continue
to use connections for some time even after the
IP address is deconfigured. Set this parameter in
order to immediately disable said shortcut when the
IP address goes away.
</longdesc>
<shortdesc lang="en">Flush kernel routing table on stop</shortdesc>
<content type="boolean" default="false"/>
</parameter>
<parameter name="run_arping">
<longdesc lang="en">
Whether or not to run arping for IPv4 collision detection check.
</longdesc>
<shortdesc lang="en">Run arping for IPv4 collision detection check</shortdesc>
<content type="string" default="${OCF_RESKEY_run_arping_default}"/>
</parameter>
<parameter name="preferred_lft">
<longdesc lang="en">
For IPv6, set the preferred lifetime of the IP address.
This can be used to ensure that the created IP address will not
be used as a source address for routing.
Expects a value as specified in section 5.5.4 of RFC 4862.
</longdesc>
<shortdesc lang="en">IPv6 preferred lifetime</shortdesc>
<content type="string" default="${OCF_RESKEY_preferred_lft_default}"/>
</parameter>
</parameters>
<actions>
<action name="start" timeout="20s" />
<action name="stop" timeout="20s" />
<action name="status" depth="0" timeout="20s" interval="10s" />
<action name="monitor" depth="0" timeout="20s" interval="10s" />
<action name="meta-data" timeout="5s" />
<action name="validate-all" timeout="20s" />
</actions>
</resource-agent>
END
exit $OCF_SUCCESS
}
ip_init() {
local rc
if [ X`uname -s` != "XLinux" ]; then
ocf_exit_reason "IPaddr2 only supported Linux."
exit $OCF_ERR_INSTALLED
fi
if [ X"$OCF_RESKEY_ip" = "X" ] && [ "$__OCF_ACTION" != "stop" ]; then
ocf_exit_reason "IP address (the ip parameter) is mandatory"
exit $OCF_ERR_CONFIGURED
fi
if
case $__OCF_ACTION in
start|stop) ocf_is_root;;
*) true;;
esac
then
: YAY!
else
ocf_exit_reason "You must be root for $__OCF_ACTION operation."
exit $OCF_ERR_PERM
fi
BASEIP="$OCF_RESKEY_ip"
BRDCAST="$OCF_RESKEY_broadcast"
NIC="$OCF_RESKEY_nic"
# Note: We had a version out there for a while which used
# netmask instead of cidr_netmask. Don't remove this aliasing code!
if
[ ! -z "$OCF_RESKEY_netmask" -a -z "$OCF_RESKEY_cidr_netmask" ]
then
OCF_RESKEY_cidr_netmask=$OCF_RESKEY_netmask
export OCF_RESKEY_cidr_netmask
fi
NETMASK="$OCF_RESKEY_cidr_netmask"
IFLABEL="$OCF_RESKEY_iflabel"
IF_MAC="$OCF_RESKEY_mac"
IP_INC_GLOBAL=${OCF_RESKEY_CRM_meta_clone_max:-1}
IP_INC_NO=`expr ${OCF_RESKEY_CRM_meta_clone:-0} + 1`
if ocf_is_true ${OCF_RESKEY_lvs_support} && [ $IP_INC_GLOBAL -gt 1 ]; then
ocf_exit_reason "LVS and load sharing do not go together well"
exit $OCF_ERR_CONFIGURED
fi
if ocf_is_decimal "$IP_INC_GLOBAL" && [ $IP_INC_GLOBAL -gt 0 ]; then
:
else
ocf_exit_reason "Invalid meta-attribute clone_max [$IP_INC_GLOBAL], should be positive integer"
exit $OCF_ERR_CONFIGURED
fi
echo $OCF_RESKEY_ip | grep -qs ":"
if [ $? -ne 0 ];then
FAMILY=inet
if ocf_is_true $OCF_RESKEY_lvs_ipv6_addrlabel ;then
ocf_exit_reason "IPv4 does not support lvs_ipv6_addrlabel"
exit $OCF_ERR_CONFIGURED
fi
else
FAMILY=inet6
if ocf_is_true $OCF_RESKEY_lvs_support ;then
ocf_exit_reason "The IPv6 does not support lvs_support"
exit $OCF_ERR_CONFIGURED
fi
if ocf_is_true $OCF_RESKEY_lvs_ipv6_addrlabel ;then
if ocf_is_decimal "$OCF_RESKEY_lvs_ipv6_addrlabel_value" && [ $OCF_RESKEY_lvs_ipv6_addrlabel_value -ge 0 ]; then
:
else
ocf_exit_reason "Invalid lvs_ipv6_addrlabel_value [$OCF_RESKEY_lvs_ipv6_addrlabel_value], should be positive integer"
exit $OCF_ERR_CONFIGURED
fi
fi
fi
# support nic:iflabel format in nic parameter
case $NIC in
*:*)
IFLABEL=`echo $NIC | sed 's/[^:]*://'`
NIC=`echo $NIC | sed 's/:.*//'`
# only the base name should be passed to findif
OCF_RESKEY_nic=$NIC
;;
esac
# $FINDIF takes its parameters from the environment
#
NICINFO=`$FINDIF`
rc=$?
if
[ $rc -eq 0 ]
then
NICINFO=`echo "$NICINFO" | sed -e 's/netmask\ //;s/broadcast\ //'`
NIC=`echo "$NICINFO" | cut -d" " -f1`
NETMASK=`echo "$NICINFO" | cut -d" " -f2`
BRDCAST=`echo "$NICINFO" | cut -d" " -f3`
else
# findif couldn't find the interface
if ocf_is_probe; then
ocf_log info "[$FINDIF] failed"
exit $OCF_NOT_RUNNING
elif [ "$__OCF_ACTION" = stop ]; then
ocf_log warn "[$FINDIF] failed"
exit $OCF_SUCCESS
else
ocf_exit_reason "[$FINDIF] failed"
exit $rc
fi
fi
SENDARPPIDFILE="$SENDARPPIDDIR/send_arp-$OCF_RESKEY_ip"
if [ -n "$IFLABEL" ]; then
IFLABEL=${NIC}:${IFLABEL}
if [ ${#IFLABEL} -gt 15 ]; then
ocf_exit_reason "Interface label [$IFLABEL] exceeds maximum character limit of 15"
exit $OCF_ERR_CONFIGURED
fi
fi
if [ "$IP_INC_GLOBAL" -gt 1 ] && ! ocf_is_true "$OCF_RESKEY_unique_clone_address"; then
IP_CIP="yes"
IP_CIP_HASH="${OCF_RESKEY_clusterip_hash}"
if [ -z "$IF_MAC" ]; then
# Choose a MAC
# 1. Concatenate some input together
# 2. This doesn't need to be a cryptographically
# secure hash.
# 3. Drop everything after the first 6 octets (12 chars)
# 4. Delimit the octets with ':'
# 5. Make sure the first octet is odd,
# so the result is a multicast MAC
IF_MAC=`echo $OCF_RESKEY_ip $NETMASK $BRDCAST | \
md5sum | \
sed -e 's#\(............\).*#\1#' \
-e 's#..#&:#g; s#:$##' \
-e 's#^\(.\)[02468aAcCeE]#\11#'`
fi
IP_CIP_FILE="/proc/net/ipt_CLUSTERIP/$OCF_RESKEY_ip"
fi
}
#
# Find out which interfaces serve the given IP address and netmask.
# The arguments are an IP address and a netmask.
# Its output are interface names devided by spaces (e.g., "eth0 eth1").
#
find_interface() {
local ipaddr="$1"
local netmask="$2"
#
# List interfaces but exclude FreeS/WAN ipsecN virtual interfaces
#
local iface="`$IP2UTIL -o -f $FAMILY addr show \
| grep "\ $ipaddr/$netmask" \
| cut -d ' ' -f2 \
| grep -v '^ipsec[0-9][0-9]*$'`"
echo "$iface"
return 0
}
#
# Delete an interface
#
delete_interface () {
ipaddr="$1"
iface="$2"
netmask="$3"
CMD="$IP2UTIL -f $FAMILY addr delete $ipaddr/$netmask dev $iface"
ocf_run $CMD || return $OCF_ERR_GENERIC
if ocf_is_true $OCF_RESKEY_flush_routes; then
ocf_run $IP2UTIL route flush cache
fi
if [ "$FAMILY" = "inet6" ] && ocf_is_true $OCF_RESKEY_lvs_ipv6_addrlabel ;then
delete_ipv6_addrlabel $ipaddr
fi
return $OCF_SUCCESS
}
#
# Add an interface
#
add_interface () {
local cmd msg ipaddr netmask broadcast iface label
ipaddr="$1"
netmask="$2"
broadcast="$3"
iface="$4"
label="$5"
if [ "$FAMILY" = "inet" ] && ocf_is_true $OCF_RESKEY_run_arping &&
check_binary arping; then
arping -q -c 2 -w 3 -D -I $iface $ipaddr
if [ $? = 1 ]; then
ocf_log err "IPv4 address collision $ipaddr [DAD]"
return $OCF_ERR_CONFIGURED
fi
fi
if [ "$FAMILY" = "inet6" ] && ocf_is_true $OCF_RESKEY_lvs_ipv6_addrlabel ;then
add_ipv6_addrlabel $ipaddr
fi
cmd="$IP2UTIL -f $FAMILY addr add $ipaddr/$netmask dev $iface"
msg="Adding $FAMILY address $ipaddr/$netmask to device $iface"
if [ "$broadcast" != "none" ]; then
cmd="$IP2UTIL -f $FAMILY addr add $ipaddr/$netmask brd $broadcast dev $iface"
msg="Adding $FAMILY address $ipaddr/$netmask with broadcast address $broadcast to device $iface"
fi
if [ ! -z "$label" ]; then
cmd="$cmd label $label"
msg="${msg} (with label $label)"
fi
if [ "$FAMILY" = "inet6" ] ;then
cmd="$cmd preferred_lft $OCF_RESKEY_preferred_lft"
msg="${msg} (with preferred_lft $OCF_RESKEY_preferred_lft)"
fi
ocf_log info "$msg"
ocf_run $cmd || return $OCF_ERR_GENERIC
msg="Bringing device $iface up"
cmd="$IP2UTIL link set $iface up"
ocf_log info "$msg"
ocf_run $cmd || return $OCF_ERR_GENERIC
return $OCF_SUCCESS
}
#
# Delete a route
#
delete_route () {
prefix="$1"
iface="$2"
CMD="$IP2UTIL route delete $prefix dev $iface"
ocf_log info "$CMD"
$CMD
return $?
}
# On Linux systems the (hidden) loopback interface may
# conflict with the requested IP address. If so, this
# unoriginal code will remove the offending loopback address
# and save it in VLDIR so it can be added back in later
# when the IPaddr is released.
#
# TODO: This is very ugly and should be controlled by an additional
# instance parameter. Or even: multi-state, with the IP only being
# "active" on the master!?
#
remove_conflicting_loopback() {
ipaddr="$1"
netmask="$2"
broadcast="$3"
ifname="$4"
ocf_log info "Removing conflicting loopback $ifname."
if
echo "$ipaddr $netmask $broadcast $ifname" > "$VLDIR/$ipaddr"
then
: Saved loopback information in $VLDIR/$ipaddr
else
ocf_log err "Could not save conflicting loopback $ifname." \
"it will not be restored."
fi
delete_interface "$ipaddr" "$ifname" "$netmask"
# Forcibly remove the route (if it exists) to the loopback.
delete_route "$ipaddr" "$ifname"
}
#
# On Linux systems the (hidden) loopback interface may
# need to be restored if it has been taken down previously
# by remove_conflicting_loopback()
#
restore_loopback() {
ipaddr="$1"
if [ -s "$VLDIR/$ipaddr" ]; then
ifinfo=`cat "$VLDIR/$ipaddr"`
ocf_log info "Restoring loopback IP Address " \
"$ifinfo."
add_interface $ifinfo
rm -f "$VLDIR/$ipaddr"
fi
}
add_ipv6_addrlabel() {
local cmd ipaddr value
ipaddr="$1"
value="$OCF_RESKEY_lvs_ipv6_addrlabel_value"
cmd="$IP2UTIL addrlabel add prefix $ipaddr label $value"
ocf_log info "Adding IPv6 address label prefix $ipaddr label $value"
ocf_run $cmd || ocf_log warn "$cmd failed."
}
delete_ipv6_addrlabel() {
local cmd ipaddr value
ipaddr="$1"
value="$OCF_RESKEY_lvs_ipv6_addrlabel_value"
cmd="$IP2UTIL addrlabel del prefix $ipaddr label $value"
ocf_run $cmd # an error can be ignored
}
is_infiniband() {
$IP2UTIL link show $NIC | grep link/infiniband >/dev/null
}
-#
-# Run send_arp to note peers about new mac address
-#
-run_send_arp() {
- if [ "x$IP_CIP" = "xyes" ] ; then
- if [ x = "x$IF_MAC" ] ; then
- MY_MAC=auto
+log_arp_sender() {
+ local cmdline
+ local output
+ local rc
+ cmdline="$@"
+
+ output=$($cmdline 2>&1)
+ rc=$?
+ if [ $rc -ne 0 ] && \
+ [ "$ARP_SENDER" != "libnet_arping" ] ; then
+ # libnet_arping always return an error as no answers
+ ocf_log err "Could not send gratuitous arps: rc=$rc"
+ fi
+ ocf_log $LOGLEVEL "$output"
+}
+
+# wrapper function to manage PID file to run arping in background
+run_with_pidfile() {
+ local cmdline
+ local pid
+ local rc
+
+ cmdline="$@"
+
+ $cmdline &
+ pid=$!
+ echo "$pid" > $SENDARPPIDFILE
+ wait $pid
+ rc=$?
+ rm -f $SENDARPPIDFILE
+ return $rc
+}
+
+build_arp_sender_cmd() {
+ case "$ARP_SENDER" in
+ send_arp)
+ if [ "x$IP_CIP" = "xyes" ] ; then
+ if [ x = "x$IF_MAC" ] ; then
+ MY_MAC=auto
+ else
+ # send_arp.linux should return without doing anything in this case
+ MY_MAC=`echo ${IF_MAC} | sed -e 's/://g'`
+ fi
else
- MY_MAC=`echo ${IF_MAC} | sed -e 's/://g'`
+ MY_MAC=auto
fi
- else
- MY_MAC=auto
- fi
+
+ ARGS="$OCF_RESKEY_send_arp_opts -i $OCF_RESKEY_arp_interval -r $ARP_COUNT -p $SENDARPPIDFILE $NIC $OCF_RESKEY_ip $MY_MAC not_used not_used"
+ ARP_SENDER_CMD="$SENDARP $ARGS"
+ ;;
+ iputils_arping)
+ ARGS="$OCF_RESKEY_send_arp_opts -U -c $ARP_COUNT -I $NIC $OCF_RESKEY_ip"
+ ARP_SENDER_CMD="run_with_pidfile arping $ARGS"
+ ;;
+ libnet_arping)
+ ARGS="$OCF_RESKEY_send_arp_opts -U -c $ARP_COUNT -i $NIC -S $OCF_RESKEY_ip $OCF_RESKEY_ip"
+ ARP_SENDER_CMD="run_with_pidfile arping $ARGS"
+ ;;
+ ipoibarping)
+ ARGS="-q -c $ARP_COUNT -U -I $NIC $OCF_RESKEY_ip"
+ ARP_SENDER_CMD="ipoibarping $ARGS"
+ ;;
+ *)
+ # should not occur
+ ocf_exit_reason "unrecognized arp_sender value: $ARP_SENDER"
+ exit $OCF_ERR_GENERIC
+ ;;
+ esac
+}
+
+#
+# Send Unsolicited ARPs to update neighbor's ARP cache
+#
+run_arp_sender() {
if [ "x$1" = "xrefresh" ] ; then
ARP_COUNT=$OCF_RESKEY_arp_count_refresh
LOGLEVEL=debug
else
ARP_COUNT=$OCF_RESKEY_arp_count
LOGLEVEL=info
fi
- if [ $ARP_COUNT -ne 0 ] ; then
- ARGS="-i $OCF_RESKEY_arp_interval -r $ARP_COUNT -p $SENDARPPIDFILE $NIC $OCF_RESKEY_ip $MY_MAC not_used not_used"
- ocf_log $LOGLEVEL "$SENDARP $ARGS"
- if ocf_is_true $OCF_RESKEY_arp_bg; then
- ($SENDARP $ARGS || ocf_log err "Could not send gratuitous arps")& >&2
- else
- $SENDARP $ARGS || ocf_log err "Could not send gratuitous arps"
- fi
+ if [ $ARP_COUNT -eq 0 ] ; then
+ return
+ fi
+
+ # do not need to send Gratuitous ARPs in the Cluster IP configuration
+ # except send_arp.libnet binary to retain the old behavior
+ if [ "x$IP_CIP" = "xyes" ] && \
+ [ "x$ARP_SENDER" != "xsend_arp" ] ; then
+ ocf_log info "Gratuitous ARPs are not sent in the Cluster IP configuration"
+ return
+ fi
+
+ # prepare arguments for each arp sender program
+ # $ARP_SENDER_CMD should be set
+ build_arp_sender_cmd
+
+ ocf_log $LOGLEVEL "$ARP_SENDER_CMD"
+
+ if ocf_is_true $OCF_RESKEY_arp_bg; then
+ log_arp_sender $ARP_SENDER_CMD &
+ else
+ log_arp_sender $ARP_SENDER_CMD
fi
}
+
#
# Run send_ua to note send ICMPv6 Unsolicited Neighbor Advertisements.
#
run_send_ua() {
local i
# Duplicate Address Detection [DAD]
# Kernel will flag the IP as 'tentative' until it ensured that
# there is no duplicates.
# If there is, it will flag it as 'dadfailed'
for i in $(seq 1 10); do
ipstatus=$($IP2UTIL -o -f $FAMILY addr show dev $NIC to $OCF_RESKEY_ip/$NETMASK)
case "$ipstatus" in
*dadfailed*)
ocf_log err "IPv6 address collision $OCF_RESKEY_ip [DAD]"
$IP2UTIL -f $FAMILY addr del dev $NIC $OCF_RESKEY_ip/$NETMASK
if [ $? -ne 0 ]; then
ocf_log err "Could not delete IPv6 address"
fi
return $OCF_ERR_GENERIC
;;
*tentative*)
if [ $i -eq 10 ]; then
ocf_log warn "IPv6 address : DAD is still in tentative"
fi
;;
*)
break
;;
esac
sleep 1
done
# Now the address should be usable
ARGS="-i $OCF_RESKEY_arp_interval -c $OCF_RESKEY_arp_count $OCF_RESKEY_ip $NETMASK $NIC"
ocf_log info "$SENDUA $ARGS"
$SENDUA $ARGS || ocf_log err "Could not send ICMPv6 Unsolicited Neighbor Advertisements."
}
-#
-# Run ipoibarping to note peers about new Infiniband address
-#
-run_send_ib_arp() {
- if [ "x$1" = "xrefresh" ] ; then
- ARP_COUNT=$OCF_RESKEY_arp_count_refresh
- LOGLEVEL=debug
- else
- ARP_COUNT=$OCF_RESKEY_arp_count
- LOGLEVEL=info
- fi
- if [ $ARP_COUNT -ne 0 ] ; then
- ARGS="-q -c $ARP_COUNT -U -I $NIC $OCF_RESKEY_ip"
- ocf_log $LOGLEVEL "ipoibarping $ARGS"
- if ocf_is_true $OCF_RESKEY_arp_bg; then
- (ipoibarping $ARGS || ocf_log err "Could not send gratuitous arps")& >&2
- else
- ipoibarping $ARGS || ocf_log err "Could not send gratuitous arps"
- fi
- fi
-}
-
# Do we already serve this IP address on the given $NIC?
#
# returns:
# ok = served (for CIP: + hash bucket)
# partial = served and no hash bucket (CIP only)
# partial2 = served and no CIP iptables rule
# no = nothing
#
ip_served() {
if [ -z "$NIC" ]; then # no nic found or specified
echo "no"
return 0
fi
cur_nic="`find_interface $OCF_RESKEY_ip $NETMASK`"
if [ -z "$cur_nic" ]; then
echo "no"
return 0
fi
if [ -z "$IP_CIP" ]; then
for i in $cur_nic; do
# only mark as served when on the same interfaces as $NIC
[ "$i" = "$NIC" ] || continue
echo "ok"
return 0
done
# There used to be logic here to pretend "not served",
# if ${OCF_RESKEY_lvs_support} was enabled, and the IP was
# found active on "lo*" only. With lvs_support on, you should
# have NIC != lo, so thats already filtered
# by the continue above.
echo "no"
return 0
fi
# Special handling for the CIP:
if [ ! -e $IP_CIP_FILE ]; then
echo "partial2"
return 0
fi
if egrep -q "(^|,)${IP_INC_NO}(,|$)" $IP_CIP_FILE ; then
echo "ok"
return 0
else
echo "partial"
return 0
fi
exit $OCF_ERR_GENERIC
}
#######################################################################
ip_usage() {
cat <<END
usage: $0 {start|stop|status|monitor|validate-all|meta-data}
Expects to have a fully populated OCF RA-compliant environment set.
END
}
ip_start() {
if [ -z "$NIC" ]; then
ocf_exit_reason "No nic found or specified"
exit $OCF_ERR_CONFIGURED
fi
if [ -n "$IP_CIP" ]; then
# Cluster IPs need special processing when the first bucket
# is added to the node... take a lock to make sure only one
# process executes that code
ocf_take_lock $CIP_lockfile
ocf_release_lock_on_exit $CIP_lockfile
fi
#
# Do we already service this IP address on $NIC?
#
local ip_status=`ip_served`
if [ "$ip_status" = "ok" ]; then
exit $OCF_SUCCESS
fi
if [ -n "$IP_CIP" ] && ([ $ip_status = "no" ] || [ $ip_status = "partial2" ]); then
$MODPROBE ip_conntrack
$IPTABLES -I INPUT -d $OCF_RESKEY_ip -i $NIC -j CLUSTERIP \
--new \
--clustermac $IF_MAC \
--total-nodes $IP_INC_GLOBAL \
--local-node $IP_INC_NO \
--hashmode $IP_CIP_HASH
if [ $? -ne 0 ]; then
ocf_exit_reason "iptables failed"
exit $OCF_ERR_GENERIC
fi
fi
if [ -n "$IP_CIP" ] && [ $ip_status = "partial" ]; then
echo "+$IP_INC_NO" >$IP_CIP_FILE
fi
if [ "$ip_status" = "no" ]; then
if ocf_is_true ${OCF_RESKEY_lvs_support}; then
for i in `find_interface $OCF_RESKEY_ip 32`; do
case $i in
lo*)
remove_conflicting_loopback $OCF_RESKEY_ip 32 255.255.255.255 lo
;;
esac
done
fi
add_interface $OCF_RESKEY_ip $NETMASK ${BRDCAST:-none} $NIC $IFLABEL
rc=$?
if [ $rc -ne $OCF_SUCCESS ]; then
ocf_exit_reason "Failed to add $OCF_RESKEY_ip"
exit $rc
fi
fi
case $NIC in
lo*)
: no need to run send_arp on loopback
;;
*)
if [ $FAMILY = "inet" ];then
- $ARP_SEND_FUN
+ run_arp_sender
else
if [ -x $SENDUA ]; then
run_send_ua
if [ $? -ne 0 ]; then
ocf_exit_reason "run_send_ua failed."
exit $OCF_ERR_GENERIC
fi
fi
fi
;;
esac
exit $OCF_SUCCESS
}
ip_stop() {
local ip_del_if="yes"
if [ -n "$IP_CIP" ]; then
# Cluster IPs need special processing when the last bucket
# is removed from the node... take a lock to make sure only one
# process executes that code
ocf_take_lock $CIP_lockfile
ocf_release_lock_on_exit $CIP_lockfile
fi
if [ -f "$SENDARPPIDFILE" ] ; then
kill `cat "$SENDARPPIDFILE"`
if [ $? -ne 0 ]; then
ocf_log warn "Could not kill previously running send_arp for $OCF_RESKEY_ip"
else
ocf_log info "killed previously running send_arp for $OCF_RESKEY_ip"
- rm -f "$SENDARPPIDFILE"
fi
+ rm -f "$SENDARPPIDFILE"
fi
local ip_status=`ip_served`
ocf_log info "IP status = $ip_status, IP_CIP=$IP_CIP"
if [ $ip_status = "no" ]; then
: Requested interface not in use
exit $OCF_SUCCESS
fi
if [ -n "$IP_CIP" ] && [ $ip_status != "partial2" ]; then
if [ $ip_status = "partial" ]; then
exit $OCF_SUCCESS
fi
echo "-$IP_INC_NO" >$IP_CIP_FILE
if [ "x$(cat $IP_CIP_FILE)" = "x" ]; then
ocf_log info $OCF_RESKEY_ip, $IP_CIP_HASH
i=1
while [ $i -le $IP_INC_GLOBAL ]; do
ocf_log info $i
$IPTABLES -D INPUT -d $OCF_RESKEY_ip -i $NIC -j CLUSTERIP \
--new \
--clustermac $IF_MAC \
--total-nodes $IP_INC_GLOBAL \
--local-node $i \
--hashmode $IP_CIP_HASH
i=`expr $i + 1`
done
else
ip_del_if="no"
fi
fi
if [ "$ip_del_if" = "yes" ]; then
delete_interface $OCF_RESKEY_ip $NIC $NETMASK
if [ $? -ne 0 ]; then
ocf_exit_reason "Unable to remove IP [${OCF_RESKEY_ip} from interface [ $NIC ]"
exit $OCF_ERR_GENERIC
fi
if ocf_is_true ${OCF_RESKEY_lvs_support}; then
restore_loopback "$OCF_RESKEY_ip"
fi
fi
exit $OCF_SUCCESS
}
ip_monitor() {
# TODO: Implement more elaborate monitoring like checking for
# interface health maybe via a daemon like FailSafe etc...
local ip_status=`ip_served`
case $ip_status in
ok)
- $ARP_SEND_FUN refresh
+ run_arp_sender refresh
return $OCF_SUCCESS
;;
partial|no|partial2)
exit $OCF_NOT_RUNNING
;;
*)
# Errors on this interface?
return $OCF_ERR_GENERIC
;;
esac
}
# make sure that we have something to send ARPs with
set_send_arp_program() {
- ARP_SEND_FUN=run_send_arp
+ ARP_SENDER=send_arp
if [ -n "$OCF_RESKEY_arp_sender" ]; then
case "$OCF_RESKEY_arp_sender" in
send_arp)
check_binary $SENDARP
;;
+ iputils_arping)
+ check_binary arping
+ ;;
+ libnet_arping)
+ check_binary arping
+ ;;
ipoibarping)
check_binary ipoibarping
- ARP_SEND_FUN=run_send_ib_arp
;;
*)
ocf_exit_reason "unrecognized arp_sender value: $OCF_RESKEY_arp_sender"
exit $OCF_ERR_CONFIGURED
;;
esac
+ ARP_SENDER="$OCF_RESKEY_arp_sender"
else
if is_infiniband; then
- ARP_SEND_FUN=run_send_ib_arp
+ ARP_SENDER=ipoibarping
if ! have_binary ipoibarping; then
[ "$__OCF_ACTION" = start ] &&
ocf_log warn "using send_arp for infiniband because ipoibarping is not available (set arp_sender to \"send_arp\" to suppress this message)"
check_binary $SENDARP
- ARP_SEND_FUN=run_send_arp
+ ARP_SENDER=send_arp
fi
fi
fi
}
ip_validate() {
check_binary $IP2UTIL
IP_CIP=
ip_init
set_send_arp_program
if [ -n "$IP_CIP" ]; then
check_binary $IPTABLES
check_binary $MODPROBE
fi
# $BASEIP, $NETMASK, $NIC , $IP_INC_GLOBAL, and $BRDCAST have been checked within ip_init,
# do not bother here.
if ocf_is_true "$OCF_RESKEY_unique_clone_address" &&
! ocf_is_true "$OCF_RESKEY_CRM_meta_globally_unique"; then
ocf_exit_reason "unique_clone_address makes sense only with meta globally_unique set"
exit $OCF_ERR_CONFIGURED
fi
if ocf_is_decimal "$OCF_RESKEY_arp_interval" && [ $OCF_RESKEY_arp_interval -gt 0 ]; then
:
else
ocf_exit_reason "Invalid OCF_RESKEY_arp_interval [$OCF_RESKEY_arp_interval]"
exit $OCF_ERR_CONFIGURED
fi
if ocf_is_decimal "$OCF_RESKEY_arp_count" && [ $OCF_RESKEY_arp_count -gt 0 ]; then
:
else
ocf_exit_reason "Invalid OCF_RESKEY_arp_count [$OCF_RESKEY_arp_count]"
exit $OCF_ERR_CONFIGURED
fi
if [ -z "$OCF_RESKEY_preferred_lft" ]; then
ocf_exit_reason "Empty value is invalid for OCF_RESKEY_preferred_lft"
exit $OCF_ERR_CONFIGURED
fi
if [ -n "$IP_CIP" ]; then
local valid=1
case $IP_CIP_HASH in
sourceip|sourceip-sourceport|sourceip-sourceport-destport)
;;
*)
ocf_exit_reason "Invalid OCF_RESKEY_clusterip_hash [$IP_CIP_HASH]"
exit $OCF_ERR_CONFIGURED
;;
esac
if ocf_is_true ${OCF_RESKEY_lvs_support}; then
ocf_exit_reason "LVS and load sharing not advised to try"
exit $OCF_ERR_CONFIGURED
fi
case $IF_MAC in
[0-9a-zA-Z][13579bBdDfF][!0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][!0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][!0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][!0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z][!0-9a-zA-Z][0-9a-zA-Z][0-9a-zA-Z])
;;
*)
valid=0
;;
esac
if [ $valid -eq 0 ]; then
ocf_exit_reason "Invalid IF_MAC [$IF_MAC]"
exit $OCF_ERR_CONFIGURED
fi
fi
}
if ocf_is_true "$OCF_RESKEY_unique_clone_address"; then
prefix=`echo $OCF_RESKEY_ip | awk -F. '{print $1"."$2"."$3}'`
suffix=`echo $OCF_RESKEY_ip | awk -F. '{print $4}'`
suffix=`expr ${OCF_RESKEY_CRM_meta_clone:-0} + $suffix`
OCF_RESKEY_ip="$prefix.$suffix"
fi
case $__OCF_ACTION in
meta-data) meta_data
;;
usage|help) ip_usage
exit $OCF_SUCCESS
;;
esac
ip_validate
case $__OCF_ACTION in
start) ip_start
;;
stop) ip_stop
;;
status) ip_status=`ip_served`
if [ $ip_status = "ok" ]; then
echo "running"
exit $OCF_SUCCESS
else
echo "stopped"
exit $OCF_NOT_RUNNING
fi
;;
monitor) ip_monitor
;;
validate-all) ;;
*) ip_usage
exit $OCF_ERR_UNIMPLEMENTED
;;
esac
# vi:sw=4:ts=8:
diff --git a/tools/send_arp.libnet.c b/tools/send_arp.libnet.c
index 12fe7f1c7..7fdfb06aa 100644
--- a/tools/send_arp.libnet.c
+++ b/tools/send_arp.libnet.c
@@ -1,758 +1,758 @@
/*
* send_arp
*
* This program sends out one ARP packet with source/target IP and Ethernet
* hardware addresses suuplied by the user. It uses the libnet libary from
* Packet Factory (http://www.packetfactory.net/libnet/ ). It has been tested
* on Linux, FreeBSD, and on Solaris.
*
* This inspired by the sample application supplied by Packet Factory.
* Matt Soffen
* Copyright (C) 2001 Matt Soffen <matt@soffen.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* Needs to be defined before any other includes, otherwise some system
* headers do not behave as expected! Major black magic... */
#undef _GNU_SOURCE /* in case it was defined on the command line */
#define _GNU_SOURCE
#include <config.h>
#include <sys/param.h>
#define USE_GNU
#if defined(ANSI_ONLY) && !defined(inline)
# define inline /* nothing */
#endif
#include <limits.h>
#include <libnet.h>
#include <libgen.h>
#include <clplumbing/timers.h>
#include <clplumbing/cl_signal.h>
#include <clplumbing/cl_log.h>
#ifdef HAVE_LIBNET_1_0_API
# define LTYPE struct libnet_link_int
static u_char *mk_packet(u_int32_t ip, u_char *device, u_char *macaddr, u_char *broadcast, u_char *netmask, u_short arptype);
static int send_arp(struct libnet_link_int *l, u_char *device, u_char *buf);
#endif
#ifdef HAVE_LIBNET_1_1_API
# define LTYPE libnet_t
static libnet_t *mk_packet(libnet_t* lntag, u_int32_t ip, u_char *device, u_char macaddr[6], u_char *broadcast, u_char *netmask, u_short arptype);
int send_arp(libnet_t* lntag);
#endif
#define PIDDIR HA_VARRUNDIR "/" PACKAGE
#define PIDFILE_BASE PIDDIR "/send_arp-"
static char print_usage[]={
"send_arp: sends out custom ARP packet.\n"
" usage: send_arp [-i repeatinterval-ms] [-r repeatcount] [-p pidfile] \\\n"
" device src_ip_addr src_hw_addr broadcast_ip_addr netmask\n"
"\n"
" where:\n"
" repeatinterval-ms: timing, in milliseconds of sending arp packets\n"
" For each ARP announcement requested, a pair of ARP packets is sent,\n"
-" an ARP request, and an ARP reply. This is becuse some systems\n"
+" an ARP request, and an ARP reply. This is because some systems\n"
" ignore one or the other, and this combination gives the greatest\n"
" chance of success.\n"
"\n"
" Each time an ARP is sent, if another ARP will be sent then\n"
" the code sleeps for half of repeatinterval-ms.\n"
"\n"
" repeatcount: how many pairs of ARP packets to send.\n"
" See above for why pairs are sent\n"
"\n"
" pidfile: pid file to use\n"
"\n"
-" device: netowrk interace to use\n"
+" device: network interface to use\n"
"\n"
" src_ip_addr: source ip address\n"
"\n"
" src_hw_addr: source hardware address.\n"
" If \"auto\" then the address of device\n"
"\n"
" broadcast_ip_addr: ignored\n"
"\n"
" netmask: ignored\n"
};
static const char * SENDARPNAME = "send_arp";
static void convert_macaddr (u_char *macaddr, u_char enet_src[6]);
static int get_hw_addr(char *device, u_char mac[6]);
int write_pid_file(const char *pidfilename);
int create_pid_directory(const char *piddirectory);
#define AUTO_MAC_ADDR "auto"
#ifndef LIBNET_ERRBUF_SIZE
# define LIBNET_ERRBUF_SIZE 256
#endif
/*
* For use logd, should keep identical with the same const variables defined
* in heartbeat.h.
*/
#define ENV_PREFIX "HA_"
#define KEY_LOGDAEMON "use_logd"
static void
byebye(int nsig)
{
(void)nsig;
/* Avoid an "error exit" log message if we're killed */
exit(0);
}
int
main(int argc, char *argv[])
{
int c = -1;
char errbuf[LIBNET_ERRBUF_SIZE];
char* device;
char* ipaddr;
char* macaddr;
char* broadcast;
char* netmask;
u_int32_t ip;
u_char src_mac[6];
int repeatcount = 1;
int j;
long msinterval = 1000;
int flag;
char pidfilenamebuf[64];
char *pidfilename = NULL;
#ifdef HAVE_LIBNET_1_0_API
LTYPE* l;
u_char *request, *reply;
#elif defined(HAVE_LIBNET_1_1_API)
LTYPE *request, *reply;
#endif
CL_SIGNAL(SIGTERM, byebye);
CL_SIGINTERRUPT(SIGTERM, 1);
cl_log_set_entity(SENDARPNAME);
cl_log_enable_stderr(TRUE);
cl_log_set_facility(LOG_USER);
cl_inherit_logging_environment(0);
while ((flag = getopt(argc, argv, "i:r:p:")) != EOF) {
switch(flag) {
case 'i': msinterval= atol(optarg);
break;
case 'r': repeatcount= atoi(optarg);
break;
case 'p': pidfilename= optarg;
break;
default: fprintf(stderr, "%s\n\n", print_usage);
return 1;
break;
}
}
if (argc-optind != 5) {
fprintf(stderr, "%s\n\n", print_usage);
return 1;
}
/*
* argv[optind+1] DEVICE dc0,eth0:0,hme0:0,
* argv[optind+2] IP 192.168.195.186
* argv[optind+3] MAC ADDR 00a0cc34a878
* argv[optind+4] BROADCAST 192.168.195.186
* argv[optind+5] NETMASK ffffffffffff
*/
device = argv[optind];
ipaddr = argv[optind+1];
macaddr = argv[optind+2];
broadcast = argv[optind+3];
netmask = argv[optind+4];
if (!pidfilename) {
if (snprintf(pidfilenamebuf, sizeof(pidfilenamebuf), "%s%s",
PIDFILE_BASE, ipaddr) >=
(int)sizeof(pidfilenamebuf)) {
cl_log(LOG_INFO, "Pid file truncated");
return EXIT_FAILURE;
}
pidfilename = pidfilenamebuf;
}
if(write_pid_file(pidfilename) < 0) {
return EXIT_FAILURE;
}
if (!strcasecmp(macaddr, AUTO_MAC_ADDR)) {
if (get_hw_addr(device, src_mac) < 0) {
cl_log(LOG_ERR, "Cannot find mac address for %s",
device);
unlink(pidfilename);
return EXIT_FAILURE;
}
}
else {
convert_macaddr((unsigned char *)macaddr, src_mac);
}
/*
* We need to send both a broadcast ARP request as well as the ARP response we
* were already sending. All the interesting research work for this fix was
* done by Masaki Hasegawa <masaki-h@pp.iij4u.or.jp> and his colleagues.
*/
#if defined(HAVE_LIBNET_1_0_API)
#ifdef ON_DARWIN
if ((ip = libnet_name_resolve((unsigned char*)ipaddr, 1)) == -1UL) {
#else
if ((ip = libnet_name_resolve(ipaddr, 1)) == -1UL) {
#endif
cl_log(LOG_ERR, "Cannot resolve IP address [%s]", ipaddr);
unlink(pidfilename);
return EXIT_FAILURE;
}
l = libnet_open_link_interface(device, errbuf);
if (!l) {
cl_log(LOG_ERR, "libnet_open_link_interface on %s: %s"
, device, errbuf);
unlink(pidfilename);
return EXIT_FAILURE;
}
request = mk_packet(ip, (unsigned char*)device, src_mac
, (unsigned char*)broadcast, (unsigned char*)netmask
, ARPOP_REQUEST);
reply = mk_packet(ip, (unsigned char*)device, src_mac
, (unsigned char *)broadcast
, (unsigned char *)netmask, ARPOP_REPLY);
if (!request || !reply) {
cl_log(LOG_ERR, "could not create packets");
unlink(pidfilename);
return EXIT_FAILURE;
}
for (j=0; j < repeatcount; ++j) {
c = send_arp(l, (unsigned char*)device, request);
if (c < 0) {
break;
}
mssleep(msinterval / 2);
c = send_arp(l, (unsigned char*)device, reply);
if (c < 0) {
break;
}
if (j != repeatcount-1) {
mssleep(msinterval / 2);
}
}
#elif defined(HAVE_LIBNET_1_1_API)
if ((request=libnet_init(LIBNET_LINK, device, errbuf)) == NULL) {
cl_log(LOG_ERR, "libnet_init failure on %s: %s", device, errbuf);
unlink(pidfilename);
return EXIT_FAILURE;
}
if ((reply=libnet_init(LIBNET_LINK, device, errbuf)) == NULL) {
cl_log(LOG_ERR, "libnet_init failure on %s: %s", device, errbuf);
unlink(pidfilename);
return EXIT_FAILURE;
}
if ((signed)(ip = libnet_name2addr4(request, ipaddr, 1)) == -1) {
cl_log(LOG_ERR, "Cannot resolve IP address [%s]", ipaddr);
unlink(pidfilename);
return EXIT_FAILURE;
}
request = mk_packet(request, ip, (unsigned char*)device, src_mac
, (unsigned char*)broadcast, (unsigned char*)netmask
, ARPOP_REQUEST);
reply = mk_packet(reply, ip, (unsigned char*)device, src_mac
, (unsigned char *)broadcast
, (unsigned char *)netmask, ARPOP_REPLY);
if (!request || !reply) {
cl_log(LOG_ERR, "could not create packets");
unlink(pidfilename);
return EXIT_FAILURE;
}
for (j=0; j < repeatcount; ++j) {
c = send_arp(request);
if (c < 0) {
break;
}
mssleep(msinterval / 2);
c = send_arp(reply);
if (c < 0) {
break;
}
if (j != repeatcount-1) {
mssleep(msinterval / 2);
}
}
#else
# error "Must have LIBNET API version defined."
#endif
unlink(pidfilename);
return c < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
void
convert_macaddr (u_char *macaddr, u_char enet_src[6])
{
int i, pos;
u_char bits[3];
pos = 0;
for (i = 0; i < 6; i++) {
/* Inserted to allow old-style MAC addresses */
if (*macaddr == ':') {
pos++;
}
bits[0] = macaddr[pos++];
bits[1] = macaddr[pos++];
bits[2] = '\0';
enet_src[i] = strtol((const char *)bits, (char **)NULL, 16);
}
}
#ifdef HAVE_LIBNET_1_0_API
int
get_hw_addr(char *device, u_char mac[6])
{
struct ether_addr *mac_address;
struct libnet_link_int *network;
char err_buf[LIBNET_ERRBUF_SIZE];
network = libnet_open_link_interface(device, err_buf);
if (!network) {
fprintf(stderr, "libnet_open_link_interface: %s\n", err_buf);
return -1;
}
mac_address = libnet_get_hwaddr(network, device, err_buf);
if (!mac_address) {
fprintf(stderr, "libnet_get_hwaddr: %s\n", err_buf);
return -1;
}
memcpy(mac, mac_address->ether_addr_octet, 6);
return 0;
}
#endif
#ifdef HAVE_LIBNET_1_1_API
int
get_hw_addr(char *device, u_char mac[6])
{
struct libnet_ether_addr *mac_address;
libnet_t *ln;
char err_buf[LIBNET_ERRBUF_SIZE];
ln = libnet_init(LIBNET_LINK, device, err_buf);
if (!ln) {
fprintf(stderr, "libnet_open_link_interface: %s\n", err_buf);
return -1;
}
mac_address = libnet_get_hwaddr(ln);
if (!mac_address) {
fprintf(stderr, "libnet_get_hwaddr: %s\n", err_buf);
return -1;
}
memcpy(mac, mac_address->ether_addr_octet, 6);
return 0;
}
#endif
/*
* Notes on send_arp() behaviour. Horms, 15th June 2004
*
* 1. Target Hardware Address
* (In the ARP portion of the packet)
*
* a) ARP Reply
*
* Set to the MAC address we want associated with the VIP,
* as per RFC2002 (4.6).
*
* Previously set to ff:ff:ff:ff:ff:ff
*
* b) ARP Request
*
* Set to 00:00:00:00:00:00. According to RFC2002 (4.6)
* this value is not used in an ARP request, so the value should
* not matter. However, I observed that typically (always?) this value
* is set to 00:00:00:00:00:00. It seems harmless enough to follow
* this trend.
*
* Previously set to ff:ff:ff:ff:ff:ff
*
* 2. Source Hardware Address
* (Ethernet Header, not in the ARP portion of the packet)
*
* Set to the MAC address of the interface that the packet is being
* sent to. Actually, due to the way that send_arp is called this would
* usually (always?) be the case anyway. Although this value should not
* really matter, it seems sensible to set the source address to where
* the packet is really coming from. The other obvious choice would be
* the MAC address that is being associated for the VIP. Which was the
* previous values. Again, these are typically the same thing.
*
* Previously set to MAC address being associated with the VIP
*/
#ifdef HAVE_LIBNET_1_0_API
u_char *
mk_packet(u_int32_t ip, u_char *device, u_char *macaddr, u_char *broadcast, u_char *netmask, u_short arptype)
{
u_char *buf;
u_char *target_mac;
u_char device_mac[6];
u_char bcast_mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
u_char zero_mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
if (libnet_init_packet(LIBNET_ARP_H + LIBNET_ETH_H, &buf) == -1) {
cl_log(LOG_ERR, "libnet_init_packet memory:");
return NULL;
}
/* Convert ASCII Mac Address to 6 Hex Digits. */
/* Ethernet header */
if (get_hw_addr((char*)device, device_mac) < 0) {
cl_log(LOG_ERR, "Cannot find mac address for %s",
device);
return NULL;
}
if (libnet_build_ethernet(bcast_mac, device_mac, ETHERTYPE_ARP, NULL, 0
, buf) == -1) {
cl_log(LOG_ERR, "libnet_build_ethernet failed:");
libnet_destroy_packet(&buf);
return NULL;
}
if (arptype == ARPOP_REQUEST) {
target_mac = zero_mac;
}
else if (arptype == ARPOP_REPLY) {
target_mac = macaddr;
}
else {
cl_log(LOG_ERR, "unknown arptype");
return NULL;
}
/*
* ARP header
*/
if (libnet_build_arp(ARPHRD_ETHER, /* Hardware address type */
ETHERTYPE_IP, /* Protocol address type */
6, /* Hardware address length */
4, /* Protocol address length */
arptype, /* ARP operation */
macaddr, /* Source hardware addr */
(u_char *)&ip, /* Target hardware addr */
target_mac, /* Destination hw addr */
(u_char *)&ip, /* Target protocol address */
NULL, /* Payload */
0, /* Payload length */
buf + LIBNET_ETH_H) == -1) {
cl_log(LOG_ERR, "libnet_build_arp failed:");
libnet_destroy_packet(&buf);
return NULL;
}
return buf;
}
#endif /* HAVE_LIBNET_1_0_API */
#ifdef HAVE_LIBNET_1_1_API
libnet_t*
mk_packet(libnet_t* lntag, u_int32_t ip, u_char *device, u_char macaddr[6], u_char *broadcast, u_char *netmask, u_short arptype)
{
u_char *target_mac;
u_char device_mac[6];
u_char bcast_mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
u_char zero_mac[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
if (arptype == ARPOP_REQUEST) {
target_mac = zero_mac;
}
else if (arptype == ARPOP_REPLY) {
target_mac = macaddr;
}
else {
cl_log(LOG_ERR, "unkonwn arptype:");
return NULL;
}
/*
* ARP header
*/
if (libnet_build_arp(ARPHRD_ETHER, /* hardware address type */
ETHERTYPE_IP, /* protocol address type */
6, /* Hardware address length */
4, /* protocol address length */
arptype, /* ARP operation type */
macaddr, /* sender Hardware address */
(u_int8_t *)&ip, /* sender protocol address */
target_mac, /* target hardware address */
(u_int8_t *)&ip, /* target protocol address */
NULL, /* Payload */
0, /* Length of payload */
lntag, /* libnet context pointer */
0 /* packet id */
) == -1 ) {
cl_log(LOG_ERR, "libnet_build_arp failed:");
return NULL;
}
/* Ethernet header */
if (get_hw_addr((char *)device, device_mac) < 0) {
cl_log(LOG_ERR, "Cannot find mac address for %s",
device);
return NULL;
}
if (libnet_build_ethernet(bcast_mac, device_mac, ETHERTYPE_ARP, NULL, 0
, lntag, 0) == -1 ) {
cl_log(LOG_ERR, "libnet_build_ethernet failed:");
return NULL;
}
return lntag;
}
#endif /* HAVE_LIBNET_1_1_API */
#ifdef HAVE_LIBNET_1_0_API
int
send_arp(struct libnet_link_int *l, u_char *device, u_char *buf)
{
int n;
n = libnet_write_link_layer(l, (char*)device, buf, LIBNET_ARP_H + LIBNET_ETH_H);
if (n == -1) {
cl_log(LOG_ERR, "libnet_write_link_layer failed");
}
return (n);
}
#endif /* HAVE_LIBNET_1_0_API */
#ifdef HAVE_LIBNET_1_1_API
int
send_arp(libnet_t* lntag)
{
int n;
n = libnet_write(lntag);
if (n == -1) {
cl_log(LOG_ERR, "libnet_write failed");
}
return (n);
}
#endif /* HAVE_LIBNET_1_1_API */
int
create_pid_directory(const char *pidfilename)
{
int status;
struct stat stat_buf;
char *pidfilename_cpy;
char *dir;
pidfilename_cpy = strdup(pidfilename);
if (!pidfilename_cpy) {
cl_log(LOG_INFO, "Memory allocation failure: %s\n",
strerror(errno));
return -1;
}
dir = dirname(pidfilename_cpy);
status = stat(dir, &stat_buf);
if (status < 0 && errno != ENOENT && errno != ENOTDIR) {
cl_log(LOG_INFO, "Could not stat pid-file directory "
"[%s]: %s", dir, strerror(errno));
free(pidfilename_cpy);
return -1;
}
if (status >= 0) {
if (S_ISDIR(stat_buf.st_mode)) {
return 0;
}
cl_log(LOG_INFO, "Pid-File directory exists but is "
"not a directory [%s]", dir);
free(pidfilename_cpy);
return -1;
}
if (mkdir(dir, S_IRUSR|S_IWUSR|S_IXUSR | S_IRGRP|S_IXGRP) < 0) {
/* Did someone else make it while we were trying ? */
if (errno == EEXIST && stat(dir, &stat_buf) >= 0
&& S_ISDIR(stat_buf.st_mode)) {
return 0;
}
cl_log(LOG_INFO, "Could not create pid-file directory "
"[%s]: %s", dir, strerror(errno));
free(pidfilename_cpy);
return -1;
}
free(pidfilename_cpy);
return 0;
}
int
write_pid_file(const char *pidfilename)
{
int pidfilefd;
char pidbuf[11];
unsigned long pid;
ssize_t bytes;
if (*pidfilename != '/') {
cl_log(LOG_INFO, "Invalid pid-file name, must begin with a "
"'/' [%s]\n", pidfilename);
return -1;
}
if (create_pid_directory(pidfilename) < 0) {
return -1;
}
while (1) {
pidfilefd = open(pidfilename, O_CREAT|O_EXCL|O_RDWR,
S_IRUSR|S_IWUSR);
if (pidfilefd < 0) {
if (errno != EEXIST) { /* Old PID file */
cl_log(LOG_INFO, "Could not open pid-file "
"[%s]: %s", pidfilename,
strerror(errno));
return -1;
}
}
else {
break;
}
pidfilefd = open(pidfilename, O_RDONLY, S_IRUSR|S_IWUSR);
if (pidfilefd < 0) {
cl_log(LOG_INFO, "Could not open pid-file "
"[%s]: %s", pidfilename,
strerror(errno));
return -1;
}
while (1) {
bytes = read(pidfilefd, pidbuf, sizeof(pidbuf)-1);
if (bytes < 0) {
if (errno == EINTR) {
continue;
}
cl_log(LOG_INFO, "Could not read pid-file "
"[%s]: %s", pidfilename,
strerror(errno));
return -1;
}
pidbuf[bytes] = '\0';
break;
}
if(unlink(pidfilename) < 0) {
cl_log(LOG_INFO, "Could not delete pid-file "
"[%s]: %s", pidfilename,
strerror(errno));
return -1;
}
if (!bytes) {
cl_log(LOG_INFO, "Invalid pid in pid-file "
"[%s]: %s", pidfilename,
strerror(errno));
return -1;
}
close(pidfilefd);
pid = strtoul(pidbuf, NULL, 10);
if (pid == ULONG_MAX && errno == ERANGE) {
cl_log(LOG_INFO, "Invalid pid in pid-file "
"[%s]: %s", pidfilename,
strerror(errno));
return -1;
}
if (kill(pid, SIGKILL) < 0 && errno != ESRCH) {
cl_log(LOG_INFO, "Error killing old proccess [%lu] "
"from pid-file [%s]: %s", pid,
pidfilename, strerror(errno));
return -1;
}
cl_log(LOG_INFO, "Killed old send_arp process [%lu]\n",
pid);
}
if (snprintf(pidbuf, sizeof(pidbuf), "%u"
, getpid()) >= (int)sizeof(pidbuf)) {
cl_log(LOG_INFO, "Pid too long for buffer [%u]", getpid());
return -1;
}
while (1) {
bytes = write(pidfilefd, pidbuf, strlen(pidbuf));
if (bytes != (ssize_t)strlen(pidbuf)) {
if (bytes < 0 && errno == EINTR) {
continue;
}
cl_log(LOG_INFO, "Could not write pid-file "
"[%s]: %s", pidfilename,
strerror(errno));
return -1;
}
break;
}
close(pidfilefd);
return 0;
}
diff --git a/tools/send_arp.linux.c b/tools/send_arp.linux.c
index 477100a0d..2aa9b5d44 100644
--- a/tools/send_arp.linux.c
+++ b/tools/send_arp.linux.c
@@ -1,1293 +1,1336 @@
/*
* arping.c
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
* YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
*/
/* Andrew Beekhof, Lars Ellenberg:
* Based on arping from iputils,
* adapted to the command line conventions established by the libnet based
* send_arp tool as used by the IPaddr and IPaddr2 resource agents.
* The libnet based send_arp, and its command line argument convention,
* was first added to the heartbeat project by Matt Soffen.
*
* Latest "resync" with iputils as of:
* git://git.linux-ipv6.org/gitroot/iputils.git
* 511f8356e22615479c3cc16bca64d72d204f6df3
* Fri Jul 24 10:48:47 2015
* To get various bugfixes and support for infiniband and other link layer
* addresses which do not fit into plain "sockaddr_ll", and broadcast addresses
* that may be different from memset(,0xff,).
*/
#include <stdlib.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <linux/sockios.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/signal.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <net/if_arp.h>
#include <sys/uio.h>
#ifdef CAPABILITIES
#include <sys/prctl.h>
#include <sys/capability.h>
#endif
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef USE_SYSFS
#include <sysfs/libsysfs.h>
struct sysfs_devattr_values;
#endif
#ifndef WITHOUT_IFADDRS
#include <ifaddrs.h>
#endif
#ifdef USE_IDN
#include <idna.h>
#include <locale.h>
#endif
static char SNAPSHOT[] = "s20121221";
static void usage(void) __attribute__((noreturn));
#ifndef DEFAULT_DEVICE
#define DEFAULT_DEVICE "eth0"
#endif
#ifdef DEFAULT_DEVICE
# define DEFAULT_DEVICE_STR DEFAULT_DEVICE
#else
# define DEFAULT_DEVICE NULL
#endif
struct device {
const char *name;
int ifindex;
#ifndef WITHOUT_IFADDRS
struct ifaddrs *ifa;
#endif
#ifdef USE_SYSFS
struct sysfs_devattr_values *sysfs;
#endif
};
int quit_on_reply=0;
struct device device = {
.name = DEFAULT_DEVICE,
};
char *source;
struct in_addr src, dst;
char *target;
int dad, unsolicited, advert;
int quiet;
int count=-1;
int timeout;
int unicasting;
int s;
int broadcast_only;
struct sockaddr_storage me;
struct sockaddr_storage he;
struct timeval start, last;
int sent, brd_sent;
int received, brd_recv, req_recv;
#ifndef CAPABILITIES
static uid_t euid;
#endif
#define MS_TDIFF(tv1,tv2) ( ((tv1).tv_sec-(tv2).tv_sec)*1000 + \
((tv1).tv_usec-(tv2).tv_usec)/1000 )
#define OFFSET_OF(name,ele) ((size_t)&((name *)0)->ele)
static socklen_t sll_len(size_t halen)
{
socklen_t len = OFFSET_OF(struct sockaddr_ll, sll_addr) + halen;
if (len < sizeof(struct sockaddr_ll))
len = sizeof(struct sockaddr_ll);
return len;
}
#define SLL_LEN(hln) sll_len(hln)
+#if 1 /* hb_mode: always print hb_mode usage in this binary */
+static char print_usage[]={
+"send_arp: sends out custom ARP packet.\n"
+" usage: send_arp [-i repeatinterval-ms] [-r repeatcount] [-p pidfile] \\\n"
+" device src_ip_addr src_hw_addr broadcast_ip_addr netmask\n"
+"\n"
+" where:\n"
+" repeatinterval-ms: ignored\n"
+"\n"
+" repeatcount: how many ARP packets to send.\n"
+"\n"
+" pidfile: pid file to use\n"
+"\n"
+" device: network interface to use\n"
+"\n"
+" src_ip_addr: source ip address\n"
+"\n"
+" src_hw_addr: only \"auto\" is supported.\n"
+" If other specified, it will exit without sending any ARP packets.\n"
+"\n"
+" broadcast_ip_addr: ignored\n"
+"\n"
+" netmask: ignored\n"
+"\n"
+" Notes: Other options of iputils-arping may be accepted but it's not\n"
+" intended to be supported in this binary.\n"
+"\n"
+};
+
+void usage(void)
+{
+ fprintf(stderr, "%s\n", print_usage);
+ exit(2);
+}
+
+#else /* hb_mode */
+
void usage(void)
{
fprintf(stderr,
"Usage: arping [-fqbDUAV] [-c count] [-w timeout] [-I device] [-s source] destination\n"
" -f : quit on first reply\n"
" -q : be quiet\n"
" -b : keep broadcasting, don't go unicast\n"
" -D : duplicate address detection mode\n"
" -U : Unsolicited ARP mode, update your neighbours\n"
" -A : ARP answer mode, update your neighbours\n"
" -V : print version and exit\n"
" -c count : how many packets to send\n"
" -w timeout : how long to wait for a reply\n"
" -I device : which ethernet device to use"
#ifdef DEFAULT_DEVICE_STR
" (" DEFAULT_DEVICE_STR ")"
#endif
"\n"
" -s source : source ip address\n"
" destination : ask for what ip address\n"
);
exit(2);
}
+#endif /* hb_mode */
static void set_signal(int signo, void (*handler)(void))
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = (void (*)(int))handler;
sa.sa_flags = SA_RESTART;
sigaction(signo, &sa, NULL);
}
#ifdef CAPABILITIES
static const cap_value_t caps[] = { CAP_NET_RAW, };
static cap_flag_value_t cap_raw = CAP_CLEAR;
#endif
static void limit_capabilities(void)
{
#ifdef CAPABILITIES
cap_t cap_p;
cap_p = cap_get_proc();
if (!cap_p) {
perror("arping: cap_get_proc");
exit(-1);
}
cap_get_flag(cap_p, CAP_NET_RAW, CAP_PERMITTED, &cap_raw);
if (cap_raw != CAP_CLEAR) {
if (cap_clear(cap_p) < 0) {
perror("arping: cap_clear");
exit(-1);
}
cap_set_flag(cap_p, CAP_PERMITTED, 1, caps, CAP_SET);
if (cap_set_proc(cap_p) < 0) {
perror("arping: cap_set_proc");
if (errno != EPERM)
exit(-1);
}
}
if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
perror("arping: prctl");
exit(-1);
}
if (setuid(getuid()) < 0) {
perror("arping: setuid");
exit(-1);
}
if (prctl(PR_SET_KEEPCAPS, 0) < 0) {
perror("arping: prctl");
exit(-1);
}
cap_free(cap_p);
#else
euid = geteuid();
#endif
}
static int modify_capability_raw(int on)
{
#ifdef CAPABILITIES
cap_t cap_p;
if (cap_raw != CAP_SET)
return on ? -1 : 0;
cap_p = cap_get_proc();
if (!cap_p) {
perror("arping: cap_get_proc");
return -1;
}
cap_set_flag(cap_p, CAP_EFFECTIVE, 1, caps, on ? CAP_SET : CAP_CLEAR);
if (cap_set_proc(cap_p) < 0) {
perror("arping: cap_set_proc");
return -1;
}
cap_free(cap_p);
#else
if (setuid(on ? euid : getuid())) {
perror("arping: setuid");
return -1;
}
#endif
return 0;
}
static int enable_capability_raw(void)
{
return modify_capability_raw(1);
}
static int disable_capability_raw(void)
{
return modify_capability_raw(0);
}
static void drop_capabilities(void)
{
#ifdef CAPABILITIES
cap_t cap_p = cap_init();
if (!cap_p) {
perror("arping: cap_init");
exit(-1);
}
if (cap_set_proc(cap_p) < 0) {
perror("arping: cap_set_proc");
exit(-1);
}
cap_free(cap_p);
#else
if (setuid(getuid()) < 0) {
perror("arping: setuid");
exit(-1);
}
#endif
}
static int send_pack(int s, struct in_addr src, struct in_addr dst,
struct sockaddr_ll *ME, struct sockaddr_ll *HE)
{
int err;
struct timeval now;
unsigned char buf[256];
struct arphdr *ah = (struct arphdr*)buf;
unsigned char *p = (unsigned char *)(ah+1);
ah->ar_hrd = htons(ME->sll_hatype);
if (ah->ar_hrd == htons(ARPHRD_FDDI))
ah->ar_hrd = htons(ARPHRD_ETHER);
ah->ar_pro = htons(ETH_P_IP);
ah->ar_hln = ME->sll_halen;
ah->ar_pln = 4;
ah->ar_op = advert ? htons(ARPOP_REPLY) : htons(ARPOP_REQUEST);
memcpy(p, &ME->sll_addr, ah->ar_hln);
p+=ME->sll_halen;
memcpy(p, &src, 4);
p+=4;
if (advert)
memcpy(p, &ME->sll_addr, ah->ar_hln);
else
memcpy(p, &HE->sll_addr, ah->ar_hln);
p+=ah->ar_hln;
memcpy(p, &dst, 4);
p+=4;
gettimeofday(&now, NULL);
err = sendto(s, buf, p-buf, 0, (struct sockaddr*)HE, SLL_LEN(ah->ar_hln));
if (err == p-buf) {
last = now;
sent++;
if (!unicasting)
brd_sent++;
}
return err;
}
static void finish(void)
{
if (!quiet) {
printf("Sent %d probes (%d broadcast(s))\n", sent, brd_sent);
printf("Received %d response(s)", received);
if (brd_recv || req_recv) {
printf(" (");
if (req_recv)
printf("%d request(s)", req_recv);
if (brd_recv)
printf("%s%d broadcast(s)",
req_recv ? ", " : "",
brd_recv);
printf(")");
}
printf("\n");
fflush(stdout);
}
fflush(stdout);
if (dad)
exit(!!received);
if (unsolicited)
exit(0);
exit(!received);
}
static void catcher(void)
{
struct timeval tv, tv_s, tv_o;
gettimeofday(&tv, NULL);
if (start.tv_sec==0)
start = tv;
timersub(&tv, &start, &tv_s);
tv_o.tv_sec = timeout;
tv_o.tv_usec = 500 * 1000;
if (count-- == 0 || (timeout && timercmp(&tv_s, &tv_o, >)))
finish();
timersub(&tv, &last, &tv_s);
tv_o.tv_sec = 0;
if (last.tv_sec==0 || timercmp(&tv_s, &tv_o, >)) {
send_pack(s, src, dst,
(struct sockaddr_ll *)&me, (struct sockaddr_ll *)&he);
if (count == 0 && unsolicited)
finish();
}
alarm(1);
}
static void print_hex(unsigned char *p, int len)
{
int i;
for (i=0; i<len; i++) {
printf("%02X", p[i]);
if (i != len-1)
printf(":");
}
}
static int recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
{
struct timeval tv;
struct arphdr *ah = (struct arphdr*)buf;
unsigned char *p = (unsigned char *)(ah+1);
struct in_addr src_ip, dst_ip;
gettimeofday(&tv, NULL);
/* Filter out wild packets */
if (FROM->sll_pkttype != PACKET_HOST &&
FROM->sll_pkttype != PACKET_BROADCAST &&
FROM->sll_pkttype != PACKET_MULTICAST)
return 0;
/* Only these types are recognised */
if (ah->ar_op != htons(ARPOP_REQUEST) &&
ah->ar_op != htons(ARPOP_REPLY))
return 0;
/* ARPHRD check and this darned FDDI hack here :-( */
if (ah->ar_hrd != htons(FROM->sll_hatype) &&
(FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
return 0;
/* Protocol must be IP. */
if (ah->ar_pro != htons(ETH_P_IP))
return 0;
if (ah->ar_pln != 4)
return 0;
if (ah->ar_hln != ((struct sockaddr_ll *)&me)->sll_halen)
return 0;
if (len < sizeof(*ah) + 2*(4 + ah->ar_hln))
return 0;
memcpy(&src_ip, p+ah->ar_hln, 4);
memcpy(&dst_ip, p+ah->ar_hln+4+ah->ar_hln, 4);
if (!dad) {
if (src_ip.s_addr != dst.s_addr)
return 0;
if (src.s_addr != dst_ip.s_addr)
return 0;
if (memcmp(p+ah->ar_hln+4, ((struct sockaddr_ll *)&me)->sll_addr, ah->ar_hln))
return 0;
} else {
/* DAD packet was:
src_ip = 0 (or some src)
src_hw = ME
dst_ip = tested address
dst_hw = <unspec>
We fail, if receive request/reply with:
src_ip = tested_address
src_hw != ME
if src_ip in request was not zero, check
also that it matches to dst_ip, otherwise
dst_ip/dst_hw do not matter.
*/
if (src_ip.s_addr != dst.s_addr)
return 0;
if (memcmp(p, ((struct sockaddr_ll *)&me)->sll_addr, ((struct sockaddr_ll *)&me)->sll_halen) == 0)
return 0;
if (src.s_addr && src.s_addr != dst_ip.s_addr)
return 0;
}
if (!quiet) {
int s_printed = 0;
printf("%s ", FROM->sll_pkttype==PACKET_HOST ? "Unicast" : "Broadcast");
printf("%s from ", ah->ar_op == htons(ARPOP_REPLY) ? "reply" : "request");
printf("%s [", inet_ntoa(src_ip));
print_hex(p, ah->ar_hln);
printf("] ");
if (dst_ip.s_addr != src.s_addr) {
printf("for %s ", inet_ntoa(dst_ip));
s_printed = 1;
}
if (memcmp(p+ah->ar_hln+4, ((struct sockaddr_ll *)&me)->sll_addr, ah->ar_hln)) {
if (!s_printed)
printf("for ");
printf("[");
print_hex(p+ah->ar_hln+4, ah->ar_hln);
printf("]");
}
if (last.tv_sec) {
long usecs = (tv.tv_sec-last.tv_sec) * 1000000 +
tv.tv_usec-last.tv_usec;
long msecs = (usecs+500)/1000;
usecs -= msecs*1000 - 500;
printf(" %ld.%03ldms\n", msecs, usecs);
} else {
printf(" UNSOLICITED?\n");
}
fflush(stdout);
}
received++;
if (FROM->sll_pkttype != PACKET_HOST)
brd_recv++;
if (ah->ar_op == htons(ARPOP_REQUEST))
req_recv++;
if (quit_on_reply || (count == 0 && received == sent))
finish();
if(!broadcast_only) {
memcpy(((struct sockaddr_ll *)&he)->sll_addr, p, ((struct sockaddr_ll *)&me)->sll_halen);
unicasting=1;
}
return 1;
}
#ifdef USE_SYSFS
union sysfs_devattr_value {
unsigned long ulong;
void *ptr;
};
enum {
SYSFS_DEVATTR_IFINDEX,
SYSFS_DEVATTR_FLAGS,
SYSFS_DEVATTR_ADDR_LEN,
#if 0
SYSFS_DEVATTR_TYPE,
SYSFS_DEVATTR_ADDRESS,
#endif
SYSFS_DEVATTR_BROADCAST,
SYSFS_DEVATTR_NUM
};
struct sysfs_devattr_values
{
char *ifname;
union sysfs_devattr_value value[SYSFS_DEVATTR_NUM];
};
static int sysfs_devattr_ulong_dec(char *ptr, struct sysfs_devattr_values *v, unsigned idx);
static int sysfs_devattr_ulong_hex(char *ptr, struct sysfs_devattr_values *v, unsigned idx);
static int sysfs_devattr_macaddr(char *ptr, struct sysfs_devattr_values *v, unsigned idx);
struct sysfs_devattrs {
const char *name;
int (*handler)(char *ptr, struct sysfs_devattr_values *v, unsigned int idx);
int free;
} sysfs_devattrs[SYSFS_DEVATTR_NUM] = {
[SYSFS_DEVATTR_IFINDEX] = {
.name = "ifindex",
.handler = sysfs_devattr_ulong_dec,
},
[SYSFS_DEVATTR_ADDR_LEN] = {
.name = "addr_len",
.handler = sysfs_devattr_ulong_dec,
},
[SYSFS_DEVATTR_FLAGS] = {
.name = "flags",
.handler = sysfs_devattr_ulong_hex,
},
#if 0
[SYSFS_DEVATTR_TYPE] = {
.name = "type",
.handler = sysfs_devattr_ulong_dec,
},
[SYSFS_DEVATTR_ADDRESS] = {
.name = "address",
.handler = sysfs_devattr_macaddr,
.free = 1,
},
#endif
[SYSFS_DEVATTR_BROADCAST] = {
.name = "broadcast",
.handler = sysfs_devattr_macaddr,
.free = 1,
},
};
#endif
static void byebye(int nsig)
{
/* Avoid an "error exit" log message if we're killed */
nsig = 0;
exit(nsig);
}
/*
* find_device()
*
* This function checks 1) if the device (if given) is okay for ARP,
* or 2) find fist appropriate device on the system.
*
* Return value:
* >0 : Succeeded, and appropriate device not found.
* device.ifindex remains 0.
* 0 : Succeeded, and approptiate device found.
* device.ifindex is set.
* <0 : Failed. Support not found, or other
* : system error. Try other method.
*
* If an appropriate device found, it is recorded inside the
* "device" variable for later reference.
*
* We have several implementations for this.
* by_ifaddrs(): requires getifaddr() in glibc, and rtnetlink in
* kernel. default and recommended for recent systems.
* by_sysfs(): requires libsysfs , and sysfs in kernel.
* by_ioctl(): unable to list devices without ipv4 address; this
* means, you need to supply the device name for
* DAD purpose.
*/
/* Common check for ifa->ifa_flags */
static int check_ifflags(unsigned int ifflags, int fatal)
{
if (!(ifflags & IFF_UP)) {
if (fatal) {
if (!quiet)
printf("Interface \"%s\" is down\n", device.name);
exit(2);
}
return -1;
}
if (ifflags & (IFF_NOARP | IFF_LOOPBACK)) {
if (fatal) {
if (!quiet)
printf("Interface \"%s\" is not ARPable\n", device.name);
exit(dad ? 0 : 2);
}
return -1;
}
return 0;
}
static int find_device_by_ifaddrs(void)
{
#ifndef WITHOUT_IFADDRS
int rc;
struct ifaddrs *ifa0, *ifa;
int count = 0;
rc = getifaddrs(&ifa0);
if (rc) {
perror("getifaddrs");
return -1;
}
for (ifa = ifa0; ifa; ifa = ifa->ifa_next) {
if (!ifa->ifa_addr)
continue;
if (ifa->ifa_addr->sa_family != AF_PACKET)
continue;
if (device.name && ifa->ifa_name && strcmp(ifa->ifa_name, device.name))
continue;
if (check_ifflags(ifa->ifa_flags, device.name != NULL) < 0)
continue;
if (!((struct sockaddr_ll *)ifa->ifa_addr)->sll_halen)
continue;
if (!ifa->ifa_broadaddr)
continue;
device.ifa = ifa;
if (count++)
break;
}
if (count == 1 && device.ifa) {
device.ifindex = if_nametoindex(device.ifa->ifa_name);
if (!device.ifindex) {
perror("arping: if_nametoindex");
freeifaddrs(ifa0);
return -1;
}
device.name = device.ifa->ifa_name;
return 0;
}
return 1;
#else
return -1;
#endif
}
#ifdef USE_SYSFS
static void sysfs_devattr_values_init(struct sysfs_devattr_values *v, int do_free)
{
int i;
if (do_free) {
free(v->ifname);
for (i = 0; i < SYSFS_DEVATTR_NUM; i++) {
if (sysfs_devattrs[i].free)
free(v->value[i].ptr);
}
}
memset(v, 0, sizeof(*v));
}
static int sysfs_devattr_ulong(char *ptr, struct sysfs_devattr_values *v, unsigned int idx,
unsigned int base)
{
unsigned long *p;
char *ep;
if (!ptr || !v)
return -1;
p = &v->value[idx].ulong;
errno = 0;
*p = strtoul(ptr, &ep, base);
if ((*ptr && isspace(*ptr & 0xff)) || errno || (*ep != '\0' && *ep != '\n'))
goto out;
return 0;
out:
return -1;
}
static int sysfs_devattr_ulong_dec(char *ptr, struct sysfs_devattr_values *v, unsigned int idx)
{
int rc = sysfs_devattr_ulong(ptr, v, idx, 10);
return rc;
}
static int sysfs_devattr_ulong_hex(char *ptr, struct sysfs_devattr_values *v, unsigned int idx)
{
int rc = sysfs_devattr_ulong(ptr, v, idx, 16);
return rc;
}
static int sysfs_devattr_macaddr(char *ptr, struct sysfs_devattr_values *v, unsigned int idx)
{
unsigned char *m;
int i;
unsigned int addrlen;
if (!ptr || !v)
return -1;
addrlen = v->value[SYSFS_DEVATTR_ADDR_LEN].ulong;
m = malloc(addrlen);
for (i = 0; i < addrlen; i++) {
if (i && *(ptr + i * 3 - 1) != ':')
goto out;
if (sscanf(ptr + i * 3, "%02hhx", &m[i]) != 1)
goto out;
}
v->value[idx].ptr = m;
return 0;
out:
free(m);
return -1;
}
#endif
static int find_device_by_sysfs(void)
{
int rc = -1;
#ifdef USE_SYSFS
struct sysfs_class *cls_net;
struct dlist *dev_list;
struct sysfs_class_device *dev;
struct sysfs_attribute *dev_attr;
struct sysfs_devattr_values sysfs_devattr_values;
int count = 0;
if (!device.sysfs) {
device.sysfs = malloc(sizeof(*device.sysfs));
sysfs_devattr_values_init(device.sysfs, 0);
}
cls_net = sysfs_open_class("net");
if (!cls_net) {
perror("sysfs_open_class");
return -1;
}
dev_list = sysfs_get_class_devices(cls_net);
if (!dev_list) {
perror("sysfs_get_class_devices");
goto out;
}
sysfs_devattr_values_init(&sysfs_devattr_values, 0);
dlist_for_each_data(dev_list, dev, struct sysfs_class_device) {
int i;
int rc = -1;
if (device.name && strcmp(dev->name, device.name))
goto do_next;
sysfs_devattr_values_init(&sysfs_devattr_values, 1);
for (i = 0; i < SYSFS_DEVATTR_NUM; i++) {
dev_attr = sysfs_get_classdev_attr(dev, sysfs_devattrs[i].name);
if (!dev_attr) {
perror("sysfs_get_classdev_attr");
rc = -1;
break;
}
if (sysfs_read_attribute(dev_attr)) {
perror("sysfs_read_attribute");
rc = -1;
break;
}
rc = sysfs_devattrs[i].handler(dev_attr->value, &sysfs_devattr_values, i);
if (rc < 0)
break;
}
if (rc < 0)
goto do_next;
if (check_ifflags(sysfs_devattr_values.value[SYSFS_DEVATTR_FLAGS].ulong,
device.name != NULL) < 0)
goto do_next;
if (!sysfs_devattr_values.value[SYSFS_DEVATTR_ADDR_LEN].ulong)
goto do_next;
if (device.sysfs->value[SYSFS_DEVATTR_IFINDEX].ulong) {
if (device.sysfs->value[SYSFS_DEVATTR_FLAGS].ulong & IFF_RUNNING)
goto do_next;
}
sysfs_devattr_values.ifname = strdup(dev->name);
if (!sysfs_devattr_values.ifname) {
perror("malloc");
goto out;
}
sysfs_devattr_values_init(device.sysfs, 1);
memcpy(device.sysfs, &sysfs_devattr_values, sizeof(*device.sysfs));
sysfs_devattr_values_init(&sysfs_devattr_values, 0);
if (count++)
break;
continue;
do_next:
sysfs_devattr_values_init(&sysfs_devattr_values, 1);
}
if (count == 1) {
device.ifindex = device.sysfs->value[SYSFS_DEVATTR_IFINDEX].ulong;
device.name = device.sysfs->ifname;
}
rc = !device.ifindex;
out:
sysfs_close_class(cls_net);
#endif
return rc;
}
static int check_device_by_ioctl(int s, struct ifreq *ifr)
{
if (ioctl(s, SIOCGIFFLAGS, ifr) < 0) {
perror("ioctl(SIOCGIFINDEX");
return -1;
}
if (check_ifflags(ifr->ifr_flags, device.name != NULL) < 0)
return 1;
if (ioctl(s, SIOCGIFINDEX, ifr) < 0) {
perror("ioctl(SIOCGIFINDEX");
return -1;
}
return 0;
}
static int find_device_by_ioctl(void)
{
int s;
struct ifreq *ifr0, *ifr, *ifr_end;
size_t ifrsize = sizeof(*ifr);
struct ifconf ifc;
static struct ifreq ifrbuf;
int count = 0;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0) {
perror("socket");
return -1;
}
memset(&ifrbuf, 0, sizeof(ifrbuf));
if (device.name) {
strncpy(ifrbuf.ifr_name, device.name, sizeof(ifrbuf.ifr_name) - 1);
if (check_device_by_ioctl(s, &ifrbuf))
goto out;
count++;
} else {
do {
int rc;
ifr0 = malloc(ifrsize);
if (!ifr0) {
perror("malloc");
goto out;
}
ifc.ifc_buf = (char *)ifr0;
ifc.ifc_len = ifrsize;
rc = ioctl(s, SIOCGIFCONF, &ifc);
if (rc < 0) {
perror("ioctl(SIOCFIFCONF");
goto out;
}
if (ifc.ifc_len + sizeof(*ifr0) + sizeof(struct sockaddr_storage) - sizeof(struct sockaddr) <= ifrsize)
break;
ifrsize *= 2;
free(ifr0);
ifr0 = NULL;
} while(ifrsize < INT_MAX / 2);
if (!ifr0) {
fprintf(stderr, "arping: too many interfaces!?\n");
goto out;
}
ifr_end = (struct ifreq *)(((char *)ifr0) + ifc.ifc_len - sizeof(*ifr0));
for (ifr = ifr0; ifr <= ifr_end; ifr++) {
if (check_device_by_ioctl(s, &ifrbuf))
continue;
memcpy(&ifrbuf.ifr_name, ifr->ifr_name, sizeof(ifrbuf.ifr_name));
if (count++)
break;
}
}
close(s);
if (count == 1) {
device.ifindex = ifrbuf.ifr_ifindex;
device.name = ifrbuf.ifr_name;
}
return !device.ifindex;
out:
close(s);
return -1;
}
static int find_device(void)
{
int rc;
rc = find_device_by_ifaddrs();
if (rc >= 0)
goto out;
rc = find_device_by_sysfs();
if (rc >= 0)
goto out;
rc = find_device_by_ioctl();
out:
return rc;
}
/*
* set_device_broadcast()
*
* This fills the device "broadcast address"
* based on information found by find_device() funcion.
*/
static int set_device_broadcast_ifaddrs_one(struct device *device, unsigned char *ba, size_t balen, int fatal)
{
#ifndef WITHOUT_IFADDRS
struct ifaddrs *ifa;
struct sockaddr_ll *sll;
if (!device)
return -1;
ifa = device->ifa;
if (!ifa)
return -1;
sll = (struct sockaddr_ll *)ifa->ifa_broadaddr;
if (sll->sll_halen != balen) {
if (fatal) {
if (!quiet)
printf("Address length does not match...\n");
exit(2);
}
return -1;
}
memcpy(ba, sll->sll_addr, sll->sll_halen);
return 0;
#else
return -1;
#endif
}
static int set_device_broadcast_sysfs(struct device *device, unsigned char *ba, size_t balen)
{
#ifdef USE_SYSFS
struct sysfs_devattr_values *v;
if (!device)
return -1;
v = device->sysfs;
if (!v)
return -1;
if (v->value[SYSFS_DEVATTR_ADDR_LEN].ulong != balen)
return -1;
memcpy(ba, v->value[SYSFS_DEVATTR_BROADCAST].ptr, balen);
return 0;
#else
return -1;
#endif
}
static int set_device_broadcast_fallback(struct device *device, unsigned char *ba, size_t balen)
{
if (!quiet)
fprintf(stderr, "WARNING: using default broadcast address.\n");
memset(ba, -1, balen);
return 0;
}
static void set_device_broadcast(struct device *dev, unsigned char *ba, size_t balen)
{
if (!set_device_broadcast_ifaddrs_one(dev, ba, balen, 0))
return;
if (!set_device_broadcast_sysfs(dev, ba, balen))
return;
set_device_broadcast_fallback(dev, ba, balen);
}
int
main(int argc, char **argv)
{
int socket_errno;
int ch;
int hb_mode = 0;
signal(SIGTERM, byebye);
signal(SIGPIPE, byebye);
limit_capabilities();
#ifdef USE_IDN
setlocale(LC_ALL, "");
#endif
enable_capability_raw();
s = socket(PF_PACKET, SOCK_DGRAM, 0);
socket_errno = errno;
disable_capability_raw();
while ((ch = getopt(argc, argv, "h?bfDUAqc:w:s:I:Vr:i:p:")) != EOF) {
switch(ch) {
case 'b':
broadcast_only=1;
break;
case 'D':
dad++;
quit_on_reply=1;
break;
case 'U':
unsolicited++;
break;
case 'A':
advert++;
unsolicited++;
break;
case 'q':
quiet++;
break;
case 'r': /* send_arp.libnet compatibility option */
hb_mode = 1;
/* fall-through */
case 'c':
count = atoi(optarg);
break;
case 'w':
timeout = atoi(optarg);
break;
case 'I':
device.name = optarg;
break;
case 'f':
quit_on_reply=1;
break;
case 's':
source = optarg;
break;
case 'V':
printf("send_arp utility, based on arping from iputils-%s\n", SNAPSHOT);
exit(0);
case 'p':
case 'i':
hb_mode = 1;
/* send_arp.libnet compatibility options, ignore */
break;
case 'h':
case '?':
default:
usage();
}
}
if(hb_mode) {
/* send_arp.libnet compatibility mode */
if (argc - optind != 5) {
usage();
return 1;
}
/*
* argv[optind+1] DEVICE dc0,eth0:0,hme0:0,
* argv[optind+2] IP 192.168.195.186
* argv[optind+3] MAC ADDR 00a0cc34a878
* argv[optind+4] BROADCAST 192.168.195.186
* argv[optind+5] NETMASK ffffffffffff
*/
unsolicited = 1;
device.name = argv[optind];
target = argv[optind+1];
+ if (strcmp(argv[optind+2], "auto")) {
+ fprintf(stderr, "send_arp.linux: Gratuitous ARPs are not sent in the Cluster IP configuration\n");
+ /* return success to suppress an error log by the RA */
+ exit(0);
+ }
} else {
argc -= optind;
argv += optind;
if (argc != 1)
usage();
target = *argv;
}
if (device.name && !*device.name)
device.name = NULL;
if (s < 0) {
errno = socket_errno;
perror("arping: socket");
exit(2);
}
if (find_device() < 0)
exit(2);
if (!device.ifindex) {
if (device.name) {
fprintf(stderr, "arping: Device %s not available.\n", device.name);
exit(2);
}
fprintf(stderr, "arping: device (option -I) is required.\n");
usage();
}
if (inet_aton(target, &dst) != 1) {
struct hostent *hp;
char *idn = target;
#ifdef USE_IDN
int rc;
rc = idna_to_ascii_lz(target, &idn, 0);
if (rc != IDNA_SUCCESS) {
fprintf(stderr, "arping: IDN encoding failed: %s\n", idna_strerror(rc));
exit(2);
}
#endif
hp = gethostbyname2(idn, AF_INET);
if (!hp) {
fprintf(stderr, "arping: unknown host %s\n", target);
exit(2);
}
#ifdef USE_IDN
free(idn);
#endif
memcpy(&dst, hp->h_addr, 4);
}
if (source && inet_aton(source, &src) != 1) {
fprintf(stderr, "arping: invalid source %s\n", source);
exit(2);
}
if (!dad && unsolicited && src.s_addr == 0)
src = dst;
if (!dad || src.s_addr) {
struct sockaddr_in saddr;
int probe_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (probe_fd < 0) {
perror("socket");
exit(2);
}
if (device.name) {
enable_capability_raw();
if (setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device.name, strlen(device.name)+1) == -1)
perror("WARNING: interface is ignored");
disable_capability_raw();
}
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
if (src.s_addr) {
saddr.sin_addr = src;
if (bind(probe_fd, (struct sockaddr*)&saddr, sizeof(saddr)) == -1) {
perror("bind");
exit(2);
}
} else if (!dad) {
int on = 1;
socklen_t alen = sizeof(saddr);
saddr.sin_port = htons(1025);
saddr.sin_addr = dst;
if (setsockopt(probe_fd, SOL_SOCKET, SO_DONTROUTE, (char*)&on, sizeof(on)) == -1)
perror("WARNING: setsockopt(SO_DONTROUTE)");
if (connect(probe_fd, (struct sockaddr*)&saddr, sizeof(saddr)) == -1) {
perror("connect");
exit(2);
}
if (getsockname(probe_fd, (struct sockaddr*)&saddr, &alen) == -1) {
perror("getsockname");
exit(2);
}
src = saddr.sin_addr;
}
close(probe_fd);
};
((struct sockaddr_ll *)&me)->sll_family = AF_PACKET;
((struct sockaddr_ll *)&me)->sll_ifindex = device.ifindex;
((struct sockaddr_ll *)&me)->sll_protocol = htons(ETH_P_ARP);
if (bind(s, (struct sockaddr*)&me, sizeof(me)) == -1) {
perror("bind");
exit(2);
}
if (1) {
socklen_t alen = sizeof(me);
if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) {
perror("getsockname");
exit(2);
}
}
if (((struct sockaddr_ll *)&me)->sll_halen == 0) {
if (!quiet)
printf("Interface \"%s\" is not ARPable (no ll address)\n", device.name);
exit(dad?0:2);
}
he = me;
set_device_broadcast(&device, ((struct sockaddr_ll *)&he)->sll_addr,
((struct sockaddr_ll *)&he)->sll_halen);
if (!quiet) {
printf("ARPING %s ", inet_ntoa(dst));
printf("from %s %s\n", inet_ntoa(src), device.name ? : "");
}
if (!src.s_addr && !dad) {
fprintf(stderr, "arping: no source address in not-DAD mode\n");
exit(2);
}
drop_capabilities();
set_signal(SIGINT, finish);
set_signal(SIGALRM, catcher);
catcher();
while(1) {
sigset_t sset, osset;
unsigned char packet[4096];
struct sockaddr_storage from;
socklen_t alen = sizeof(from);
int cc;
if ((cc = recvfrom(s, packet, sizeof(packet), 0,
(struct sockaddr *)&from, &alen)) < 0) {
perror("arping: recvfrom");
continue;
}
sigemptyset(&sset);
sigaddset(&sset, SIGALRM);
sigaddset(&sset, SIGINT);
sigprocmask(SIG_BLOCK, &sset, &osset);
recv_pack(packet, cc, (struct sockaddr_ll *)&from);
sigprocmask(SIG_SETMASK, &osset, NULL);
}
}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Feb 24, 7:48 AM (1 d, 1 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1458346
Default Alt Text
(86 KB)
Attached To
Mode
rR Resource Agents
Attached
Detach File
Event Timeline
Log In to Comment