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="/tmp/umts_lock_d"
13 FILE_LOCK_SLIVER="/tmp/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 echo $! > $PPPD_PIDFILE
126 sleep 5s; #waiting for the interface to establish the link
127 if grep "$STROK" $LOGF >/dev/null 2>&1; then
128 conn_on $sliver $sliver_nid
132 if grep "$STROK" $LOGF >/dev/null 2>&1; then
133 conn_on $sliver $sliver_nid
142 function stop_umts(){
144 local sliver_nid=`get_nid $sliver`
147 if ! [ -e $PPPD_PIDFILE ]; then
152 ppp_addr=`get_ppp_address`
154 if ! check_who_locked $sliver; then
158 PID=`cat $PPPD_PIDFILE`;
160 if [ -d /proc/$PID ] && grep $PPPD /proc/$PID/cmdline >/dev/null 2>&1; then
161 kill $PID; touch $DESTS_FILE
169 conn_off $sliver $ppp_addr
170 cat $LOGF | grep "time"
178 function status_umts(){
181 if ! [ -e $PPPD_PIDFILE ]; then return 1; fi
182 PID=`cat $PPPD_PIDFILE`
184 if [ -d /proc/$PID ] && grep $PPPD /proc/$PID/cmdline >/dev/null 2>&1; then
191 #add the ppp interface to the slice - not used at the moment
192 function add_interface(){
193 local nid=`get_nid $1`
194 $NADDRESS --add --nid $nid --ip $PPP_INT >>$LOGFILE 2>&1;
197 #remove the ppp interface from the slice - not used at the moment
198 function del_interface(){
199 local nid=`get_nid $1`
200 #$NADDRESS --remove --nid $NID --ip $PPP_INT >>$LOGFILE 2>&1;
203 #get slice network id
208 function set_routes(){
210 local sliver_nid=`get_nid $sliver`
211 local temp_nid=`get_temp_nid`
212 local ppp_addr=`get_ppp_address`
214 #Asks VNET+ to tag all the packets with the respective sliver id
215 iptables -t mangle -A OUTPUT -j MARK --copy-xid 0x00
217 #Changes the netfilter mark of packets to trigger rerouting.
218 #We need to change the mark because the kernel triggers the rerouting process only
219 #if it sees that the netfilter mark has been altered in the mangle iptables chain
220 iptables -t mangle -A OUTPUT -m mark --mark $sliver_nid -j MARK --set-mark $temp_nid
222 #Adds an SNAT rule to set the source IP address of packets that are about to go out through the UMTS
223 #interface. The kernel sets the source address of packets when the first routing process happens
224 #so, without this rule, packets would have the source ip address set on the first routing process of the ethernet interface
225 iptables -t nat -A POSTROUTING -o $PPP_INT -j SNAT --to-source $ppp_addr
227 #Restores the original netfilter mark for planetflow
228 iptables -t mangle -I POSTROUTING 1 -m mark --mark $temp_nid -j MARK --set-mark $sliver_nid
230 #Forbids other slices to use the UMTS interface
231 iptables -t mangle -I POSTROUTING 2 -o $PPP_INT -m mark ! --mark $sliver_nid -j DROP
233 #Enables the explicit bind to the UMTS interface. Applications launched by the user in the slice that is using the UMTS and that
234 #bind to the UMTS interface will have packets going out through the UMTS interface
235 ip route flush table umts_table >/dev/null 2>&1
236 ip rule add from $ppp_addr fwmark $temp_nid table umts_table >/dev/null 2>&1
237 ip route add default dev $PPP_INT src $ppp_addr table umts_table >/dev/null 2>&1
238 ip route flush cache >/dev/null 2>&1
241 function unset_routes(){
244 local sliver_nid=`get_nid $sliver`
245 local temp_nid=`get_temp_nid`
248 #removing the rules for changing the mark and the rules for SNAT
249 iptables -t mangle -D OUTPUT -j MARK --copy-xid 0x00
250 iptables -t mangle -D OUTPUT -m mark --mark $sliver_nid -j MARK --set-mark $temp_nid
251 iptables -t nat -D POSTROUTING -o $PPP_INT -j SNAT --to-source $ppp_addr
252 iptables -t mangle -D POSTROUTING -m mark --mark $temp_nid -j MARK --set-mark $sliver_nid
254 iptables -t mangle -D POSTROUTING -o $PPP_INT -m mark ! --mark $sliver_nid -j DROP
256 #disable the explicit bind to the ppp interface
257 ip rule del from $ppp_addr fwmark $temp_nid table umts_table >/dev/null 2>&1
258 ip route del default dev $PPP_INT src $ppp_addr table umts_table >/dev/null 2>&1
259 ip route flush cache >/dev/null 2>&1
263 function add_destination(){
266 local sliver_nid=`get_nid $sliver`
267 local temp_nid=`get_temp_nid`
269 if [[ ! $dest ]]; then return 1; fi
271 if ! status_umts; then
275 if ! check_who_locked $sliver; then
279 if ip rule add to "$dest" fwmark $temp_nid table umts_table >/dev/null 2>&1; then
280 echo "$dest" >> $DESTS_FILE
285 ip route flush cache >/dev/null 2>&1
288 function del_destination(){
291 local sliver_nid=`get_nid $sliver`
292 local temp_nid=`get_temp_nid`
294 if [[ ! $dest ]]; then return 1; fi
296 if ! check_who_locked $sliver; then
300 if ip rule del to "$dest" fwmark $temp_nid table umts_table >/dev/null 2>&1; then
305 ip route flush cache >/dev/null 2>&1
309 function get_ppp_address(){
310 ifconfig $PPP_INT | grep inet\ addr | cut -d ":" -f 2 | cut -d " " -f 1
313 function set_temp_nid(){
315 local temp_nid=$((0x20000+$sliver_nid))
316 echo $temp_nid > $FILE_TEMP_NID
320 function check_who_locked(){
322 local sliver_nid=`get_nid $sliver`
325 if [ -e $LOCK_DIR ]; then
326 if [[ `cat $FILE_LOCK_SLIVER` != $sliver_nid ]]; then
327 echo "Interface in use by another slice.";
338 function kill_gcom(){
342 if ! ps -C $GCOMP >/dev/null 2>&1; then
343 echo "$GCOMP terminated."
345 echo "$GCOMP still alive. Try kill -9"
350 if ! ps -C $GCOMP >/dev/null 2>&1; then
351 echo "$GCOMP terminated."
353 echo "Couldn't stop $GCOMP. Please contact administrators for assistance."
360 local sliver_nid=`get_nid $sliver`
363 if [ -e $LOCK_DIR ]; then
364 if ! check_who_locked $sliver; then
368 if mkdir $LOCK_DIR >/dev/null 2>&1; then
369 echo "$sliver_nid" > $FILE_LOCK_SLIVER
380 local sliver_nid=`get_nid $sliver`
383 if ! [ -e $LOCK_DIR ]; then
386 if ! check_who_locked $sliver; then
389 if ! rmdir $LOCK_DIR; then
401 # checks ip addresses
402 function valid_dotted_quad(){
412 ""|*[!0-9]*) return 1; break ;; ## Segment empty or non-numeric char
413 *) [ $seg -gt 255 ] && return 2 ;;
417 return 3 ## Not 4 segments