4 STROK="remote IP address"
6 GCOM="/usr/bin/"${GCOMP}
8 PPPD_PIDFILE="/var/run/umts_pppd.pid"
9 DESTS_FILE="/tmp/umts_dest_file"
10 FILE_TEMP_NID="/tmp/umts_temp_nid"
11 FILE_UMTS_INT="/tmp/umts_dev"
12 LOCK_DIR="/var/run/umts_lock"
13 FILE_LOCK_SLIVER="/var/run/umts_locking_sliver"
16 CHAT_SCRIPTS="/etc/chatscripts"
17 UMTS_CONF="/etc/umts.conf"
21 function get_umts_dev(){
22 echo "/dev/umts_modem"
28 if [ -e $LOCK_DIR ]; then
32 #if ! grep "umts_table" /etc/iproute2/rt_tables > /dev/null 2>&1; then
33 # echo "20 umts_table" >> /etc/iproute2/rt_tables
41 function get_temp_nid(){
46 #called when the connection is started
50 #DESTS_FILE contains added destinations
53 set_temp_nid $sliver_nid
56 cat $LOGF | grep "local"
61 #called when the connection is terminated
66 unset_routes $sliver $ppp_addr
68 for i in `cat $DESTS_FILE`; do
69 del_destination $i $sliver > /dev/null 2>&1
72 rm $DESTS_FILE >/dev/null 2>&1
75 function start_umts(){
77 local sliver_nid=`get_nid $sliver`
79 if ! grep "umts_table" /etc/iproute2/rt_tables > /dev/null 2>&1; then
80 echo "20 umts_table" >> /etc/iproute2/rt_tables
83 if ! lock $sliver; then
88 echo "Already connected"
92 echo "Starting GCOM..."
93 if ! $GCOM -d `get_umts_dev`; then
100 if [ -e $UMTS_CONF ]; then
104 if ! [[ $APN ]]; then
107 if ! [[ $NUM ]]; then
112 exec /usr/sbin/pppd nodetach `get_umts_dev` 460800 \
114 connect "/usr/sbin/chat -v \
117 ABORT '\nNO ANSWER\r' \
118 ABORT '\nRINGING\r\n\r\nRINGING\r' \
119 '' ATZ OK 'ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0' OK \
120 'AT+CGDCONT=1,\"IP\",\"$APN\"' OK \
121 ATD$NUM CONNECT ''" > $LOGF &
124 #exec /usr/sbin/pppd nodetach `get_umts_dev` 460800 \
126 # connect "/usr/sbin/chat -f ${CHAT_SCRIPTS}/vodaphone-it" > $LOGF &
128 echo $! > $PPPD_PIDFILE
130 sleep 5s; #waiting for the interface to establish the link
131 if grep "$STROK" $LOGF >/dev/null 2>&1; then
132 conn_on $sliver $sliver_nid
136 if grep "$STROK" $LOGF >/dev/null 2>&1; then
137 conn_on $sliver $sliver_nid
146 function stop_umts(){
148 local sliver_nid=`get_nid $sliver`
151 if ! [ -e $PPPD_PIDFILE ]; then
156 ppp_addr=`get_ppp_address`
158 if ! check_who_locked $sliver; then
162 PID=`cat $PPPD_PIDFILE`;
164 if [ -d /proc/$PID ] && grep $PPPD /proc/$PID/cmdline >/dev/null 2>&1; then
165 kill $PID; touch $DESTS_FILE
173 conn_off $sliver $ppp_addr
174 cat $LOGF | grep "time"
182 function status_umts(){
185 if ! [ -e $PPPD_PIDFILE ]; then return 1; fi
186 PID=`cat $PPPD_PIDFILE`
188 if [ -d /proc/$PID ] && grep $PPPD /proc/$PID/cmdline >/dev/null 2>&1; then
195 #add the ppp interface to the slice - not used at the moment
196 function add_interface(){
197 local nid=`get_nid $1`
198 $NADDRESS --add --nid $nid --ip $PPP_INT >>$LOGFILE 2>&1;
201 #remove the ppp interface from the slice - not used at the moment
202 function del_interface(){
203 local nid=`get_nid $1`
204 #$NADDRESS --remove --nid $NID --ip $PPP_INT >>$LOGFILE 2>&1;
207 #get slice network id
212 function set_routes(){
214 local sliver_nid=`get_nid $sliver`
215 local temp_nid=`get_temp_nid`
216 local ppp_addr=`get_ppp_address`
218 #Asks VNET+ to tag all the packets with the respective sliver id
219 iptables -t mangle -A OUTPUT -j MARK --copy-xid 0x00
221 #Changes the netfilter mark of packets to trigger rerouting.
222 #We need to change the mark because the kernel triggers the rerouting process only
223 #if it sees that the netfilter mark has been altered in the mangle iptables chain
224 iptables -t mangle -A OUTPUT -m mark --mark $sliver_nid -j MARK --set-mark $temp_nid
226 #Adds an SNAT rule to set the source IP address of packets that are about to go out through the UMTS
227 #interface. The kernel sets the source address of packets when the first routing process happens
228 #so, without this rule, packets would have the source ip address set on the first routing process of the ethernet interface
229 iptables -t nat -A POSTROUTING -o $PPP_INT -j SNAT --to-source $ppp_addr
231 #Restores the original netfilter mark for planetflow
232 iptables -t mangle -I POSTROUTING 1 -m mark --mark $temp_nid -j MARK --set-mark $sliver_nid
234 #Forbids other slices to use the UMTS interface
235 iptables -t mangle -I POSTROUTING 2 -o $PPP_INT -m mark ! --mark $sliver_nid -j DROP
237 #Enables the explicit bind to the UMTS interface. Applications launched by the user in the slice that is using the UMTS and that
238 #bind to the UMTS interface will have packets going out through the UMTS interface
239 ip route flush table umts_table >/dev/null 2>&1
240 ip rule add from $ppp_addr fwmark $temp_nid table umts_table >/dev/null 2>&1
241 ip route add default dev $PPP_INT src $ppp_addr table umts_table >/dev/null 2>&1
242 ip route flush cache >/dev/null 2>&1
245 function unset_routes(){
248 local sliver_nid=`get_nid $sliver`
249 local temp_nid=`get_temp_nid`
252 #removing the rules for changing the mark and the rules for SNAT
253 iptables -t mangle -D OUTPUT -j MARK --copy-xid 0x00
254 iptables -t mangle -D OUTPUT -m mark --mark $sliver_nid -j MARK --set-mark $temp_nid
255 iptables -t nat -D POSTROUTING -o $PPP_INT -j SNAT --to-source $ppp_addr
256 iptables -t mangle -D POSTROUTING -m mark --mark $temp_nid -j MARK --set-mark $sliver_nid
258 iptables -t mangle -D POSTROUTING -o $PPP_INT -m mark ! --mark $sliver_nid -j DROP
260 #disable the explicit bind to the ppp interface
261 ip rule del from $ppp_addr fwmark $temp_nid table umts_table >/dev/null 2>&1
262 ip route del default dev $PPP_INT src $ppp_addr table umts_table >/dev/null 2>&1
263 ip route flush cache >/dev/null 2>&1
267 function add_destination(){
270 local sliver_nid=`get_nid $sliver`
271 local temp_nid=`get_temp_nid`
273 if [[ ! $dest ]]; then return 1; fi
275 if ! status_umts; then
279 if ! check_who_locked $sliver; then
283 if ip rule add to "$dest" fwmark $temp_nid table umts_table >/dev/null 2>&1; then
284 echo "$dest" >> $DESTS_FILE
289 ip route flush cache >/dev/null 2>&1
292 function del_destination(){
295 local sliver_nid=`get_nid $sliver`
296 local temp_nid=`get_temp_nid`
298 if [[ ! $dest ]]; then return 1; fi
300 if ! check_who_locked $sliver; then
304 if ip rule del to "$dest" fwmark $temp_nid table umts_table >/dev/null 2>&1; then
309 ip route flush cache >/dev/null 2>&1
313 function get_ppp_address(){
314 ifconfig $PPP_INT | grep inet\ addr | cut -d ":" -f 2 | cut -d " " -f 1
317 function set_temp_nid(){
319 local temp_nid=$((0x20000+$sliver_nid))
320 echo $temp_nid > $FILE_TEMP_NID
324 function check_who_locked(){
326 local sliver_nid=`get_nid $sliver`
329 if [ -e $LOCK_DIR ]; then
330 if [[ `cat $FILE_LOCK_SLIVER` != $sliver_nid ]]; then
331 echo "Interface in use by another slice.";
342 function kill_gcom(){
346 if ! ps -C $GCOMP >/dev/null 2>&1; then
347 echo "$GCOMP terminated."
349 echo "$GCOMP still alive. Try kill -9"
354 if ! ps -C $GCOMP >/dev/null 2>&1; then
355 echo "$GCOMP terminated."
357 echo "Couldn't stop $GCOMP. Please contact administrators for assistance."
364 local sliver_nid=`get_nid $sliver`
367 if [ -e $LOCK_DIR ]; then
368 if ! check_who_locked $sliver; then
372 if mkdir $LOCK_DIR >/dev/null 2>&1; then
373 echo "$sliver_nid" > $FILE_LOCK_SLIVER
384 local sliver_nid=`get_nid $sliver`
387 if ! [ -e $LOCK_DIR ]; then
390 if ! check_who_locked $sliver; then
393 if ! rmdir $LOCK_DIR; then
405 # checks ip addresses
406 function valid_dotted_quad(){
416 ""|*[!0-9]*) return 1; break ;; ## Segment empty or non-numeric char
417 *) [ $seg -gt 255 ] && return 2 ;;
421 return 3 ## Not 4 segments