4 STROK="remote IP address"
7 PPPD_PIDFILE="/var/run/umts_pppd.pid"
8 DESTS_FILE="/tmp/umts_dest_file"
9 FILE_TEMP_NID="/tmp/umts_temp_nid"
10 FILE_UMTS_INT="/tmp/umts_dev"
11 LOCK_DIR="/var/run/umts_lock"
12 FILE_LOCK_SLIVER="/var/run/umts_locking_sliver"
17 function set_umts_dev(){
19 echo $umts_dev > $FILE_UMTS_INT
22 function get_umts_dev(){
23 if [ -e $FILE_UMTS_INT ]; then
28 function unset_umts_dev(){
36 if [ -f /usr/bin/gcom ]; then
38 elif [ -f /usr/bin/comgt ]; then
41 echo "I couldn't find gcom"
47 if [ -e $LOCK_DIR ]; then
51 for i in /dev/umts_modem /dev/umts_modem1; do
52 echo "Testing if the umts interface is present on the device $i..."
62 if ! [ $found == 0 ]; then
63 echo "I couldn't find the umts device. Make a symlink from it to /dev/umts_modem."
67 if ! grep "umts_table" /etc/iproute2/rt_tables > /dev/null 2>&1; then
68 echo "20 umts_table" >> /etc/iproute2/rt_tables
76 function get_temp_nid(){
81 #called when the connection is started
85 #DESTS_FILE contains added destinations
88 set_temp_nid $sliver_nid
92 cat $LOGF | grep "local"
97 #called when the connection is terminated
102 unset_routes $sliver $ppp_addr
104 for i in `cat $DESTS_FILE`; do
105 del_destination $i $sliver > /dev/null 2>&1
108 rm $DESTS_FILE >/dev/null 2>&1
111 function start_umts(){
113 local sliver_nid=`get_nid $sliver`
115 if ! lock $sliver; then
120 echo "Already connected"
124 if [[ `get_umts_dev` == "" ]]; then
125 if [[ init_umts != 0 ]]; then
130 if ! [ -e `get_umts_dev` ]; then
131 echo "Umts interface is not present"
137 exec /usr/sbin/pppd nodetach `get_umts_dev` 460800 \
139 connect "/usr/sbin/chat -v \
142 ABORT '\nNO ANSWER\r' \
143 ABORT '\nRINGING\r\n\r\nRINGING\r' \
144 '' ATZ OK 'ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0' OK \
145 ATD*99***1# CONNECT ''" > $LOGF &
147 echo $! > $PPPD_PIDFILE
149 sleep 5s; #waiting for the interface to establish the link
150 if grep "$STROK" $LOGF >/dev/null 2>&1; then
151 conn_on $sliver $sliver_nid
155 if grep "$STROK" $LOGF >/dev/null 2>&1; then
156 conn_on $sliver $sliver_nid
167 function stop_umts(){
169 local sliver_nid=`get_nid $sliver`
172 if ! [ -e $PPPD_PIDFILE ]; then
177 ppp_addr=`get_ppp_address`
179 if ! check_who_locked $sliver; then
183 PID=`cat $PPPD_PIDFILE`;
185 if [ -d /proc/$PID ] && grep $PPPD /proc/$PID/cmdline >/dev/null 2>&1; then
186 kill $PID; touch $DESTS_FILE
194 conn_off $sliver $ppp_addr
195 cat $LOGF | grep "time"
203 function status_umts(){
206 if ! [ -e $PPPD_PIDFILE ]; then return 1; fi
208 PID=`cat $PPPD_PIDFILE`
210 if [ -d /proc/$PID ] && grep $PPPD /proc/$PID/cmdline >/dev/null 2>&1; then
217 #add the ppp interface to the slice - not used at the moment
218 function add_interface(){
219 local nid=`get_nid $1`
220 $NADDRESS --add --nid $nid --ip $PPP_INT >>$LOGFILE 2>&1;
223 #remove the ppp interface from the slice - not used at the moment
224 function del_interface(){
225 local nid=`get_nid $1`
226 #$NADDRESS --remove --nid $NID --ip $PPP_INT >>$LOGFILE 2>&1;
229 #get slice network id
234 function set_routes(){
236 local sliver_nid=`get_nid $sliver`
237 local temp_nid=`get_temp_nid`
238 local ppp_addr=`get_ppp_address`
240 #remarking of packets, to trigger rerouting, and SNAT
241 iptables -t mangle -A OUTPUT -j MARK --copy-xid 0x00
242 iptables -t mangle -A OUTPUT -m mark --mark $sliver_nid -j MARK --set-mark $temp_nid
243 iptables -t nat -A POSTROUTING -o $PPP_INT -j SNAT --to-source $ppp_addr
245 #restores the original mark
246 iptables -t mangle -I POSTROUTING 1 -m mark --mark $temp_nid -j MARK --set-mark $sliver_nid
248 #forbids other slices to use the UMTS interface
249 iptables -t mangle -I POSTROUTING 2 -o $PPP_INT -m mark ! --mark $sliver_nid -j DROP
251 #enables the explicit bind to the ppp interface
252 ip route flush table umts_table >/dev/null 2>&1
253 ip rule add from $ppp_addr fwmark $temp_nid table umts_table >/dev/null 2>&1
254 ip route add default dev $PPP_INT src $ppp_addr table umts_table >/dev/null 2>&1
255 ip route flush cache >/dev/null 2>&1
258 function unset_routes(){
261 local sliver_nid=`get_nid $sliver`
262 local temp_nid=`get_temp_nid`
265 #remarking and SNAT removed
266 iptables -t mangle -D OUTPUT -j MARK --copy-xid 0x00
267 iptables -t mangle -D OUTPUT -m mark --mark $sliver_nid -j MARK --set-mark $temp_nid
268 iptables -t nat -D POSTROUTING -o $PPP_INT -j SNAT --to-source $ppp_addr
269 iptables -t mangle -D POSTROUTING -m mark --mark $temp_nid -j MARK --set-mark $sliver_nid
271 iptables -t mangle -D POSTROUTING -o $PPP_INT -m mark ! --mark $sliver_nid -j DROP
273 #disable the explicit bind to the ppp interface
274 ip rule del from $ppp_addr fwmark $temp_nid table umts_table >/dev/null 2>&1
275 #ip route del default dev $PPP_INT src $ppp_addr table umts_table >/dev/null 2>&1
276 ip route flush cache >/dev/null 2>&1
280 function add_destination(){
283 local sliver_nid=`get_nid $sliver`
284 local temp_nid=`get_temp_nid`
286 if [[ ! $dest ]]; then return 1; fi
288 if ! status_umts; then
292 if ! check_who_locked $sliver; then
296 if ip rule add to "$dest" fwmark $temp_nid table umts_table >/dev/null 2>&1; then
297 echo "$dest" >> $DESTS_FILE
302 ip route flush cache >/dev/null 2>&1
305 function del_destination(){
308 local sliver_nid=`get_nid $sliver`
309 local temp_nid=`get_temp_nid`
311 if [[ ! $dest ]]; then return 1; fi
313 if ! check_who_locked $sliver; then
317 if ip rule del to "$dest" fwmark $temp_nid table umts_table >/dev/null 2>&1; then
322 ip route flush cache >/dev/null 2>&1
326 function get_ppp_address(){
327 ifconfig $PPP_INT | grep inet\ addr | cut -d ":" -f 2 | cut -d " " -f 1
330 function set_temp_nid(){
332 local temp_nid=$((0x20000+$sliver_nid))
335 #while grep :$temp_nid: /etc/passwd; do
336 # temp_nid=$((temp_nid+1))
338 # if [[ $k == 1000 ]]; then
339 # logger "Fatal error: I couldn't find a temp_nid"
344 echo $temp_nid > $FILE_TEMP_NID
349 function check_who_locked(){
351 local sliver_nid=`get_nid $sliver`
354 if [ -e $LOCK_DIR ]; then
355 if [[ `cat $FILE_LOCK_SLIVER` != $sliver_nid ]]; then
356 echo "Interface in use by another slice.";
369 local sliver_nid=`get_nid $sliver`
372 if [ -e $LOCK_DIR ]; then
373 if ! check_who_locked $sliver; then
377 if mkdir $LOCK_DIR >/dev/null 2>&1; then
378 echo "$sliver_nid" > $FILE_LOCK_SLIVER
389 local sliver_nid=`get_nid $sliver`
392 if ! [ -e $LOCK_DIR ]; then
395 if ! check_who_locked $sliver; then
398 if ! rmdir $LOCK_DIR; then
410 # checks ip addresses
411 function valid_dotted_quad(){
421 ""|*[!0-9]*) return 1; break ;; ## Segment empty or non-numeric char
422 *) [ $seg -gt 255 ] && return 2 ;;
426 return 3 ## Not 4 segments