[patch-team] update wing to 20110329 - Signed off by Roberto Riggio <roberto.riggio...
[openwrt/svn-archive/archive.git] / net / wing / files / lib / network / wing.sh
1
2 scan_wing() {
3 config_set "$1" device "wing-$1"
4 }
5
6 coldplug_interface_wing() {
7 setup_interface_wing "wing-$1" "$1"
8 }
9
10 stop_interface_wing() {
11 local config="$1"
12 local iface="wing-$config"
13 env -i ACTION="ifdown" INTERFACE="$config" DEVICE="$iface" PROTO=wing /sbin/hotplug-call "iface" &
14 [ -f "/var/run/$iface.pid" ] && {
15 kill -9 $(cat /var/run/$iface.pid)
16 rm /var/run/$iface.pid
17 }
18 }
19
20 setup_interface_wing() {
21
22 local iface="$1"
23 local config="$2"
24
25 local hwmodes=""
26 local freqs=""
27 local ifnames=""
28 local hwaddrs=""
29
30 config_load wireless
31 config_foreach wing_list_interfaces wifi-iface
32
33 # start click router
34 if [ "$hwmodes" = "" -o "$freqs" = "" -o "$ifnames" = "" -o "$hwaddrs" = "" ]; then
35 logger -t "$config" "No raw interfaces available. Exiting."
36 exit 1
37 fi
38
39 local profile rc ls metric prefix period tau debug
40
41 config_get profile $config profile "bulk"
42 config_get rc $config rc "minstrel"
43 config_get ls $config ls "fcfs"
44 config_get metric $config metric "wcett"
45 config_get prefix $config prefix "6"
46 config_get period $config period "10000"
47 config_get tau $config tau "100000"
48 config_get_bool debug $config debug "false"
49
50 local hwaddr=$(echo $hwaddrs | sed 's/ .*//');
51 local ipaddr=$(printf "$prefix.%d.%d.%d" $(echo $hwaddr | awk -F: '{printf "0x%s 0x%s 0x%s",$4,$5,$6}'))
52 local netmask=255.0.0.0
53
54 if ! wing_template_available "profile" "$profile"; then
55 logger -t "$config" "Unable to configure router. Exiting."
56 exit 1
57 fi
58
59 if ! wing_template_available "rc" "$rc"; then
60 logger -t "$config" "Unable to configure rate control. Exiting."
61 exit 1
62 fi
63
64 if ! wing_template_available "ls" "$ls"; then
65 logger -t "$config" "Unable to configure link scheduler. Exiting."
66 exit 1
67 fi
68
69 if [ "$profile" = "" -o "$rc" = "" ]; then
70 logger -t "$config" "Unable to generate template. Exiting."
71 exit 1
72 fi
73
74 [ "$debug" == 0 ] && dbg="" || dbg="-d"
75
76 /usr/bin/click_config -p $profile -r $rc -s $ls -l $metric \
77 -m "$hwmodes" -c "$freqs" -n "$ifnames" -a "$hwaddrs" $dbg \
78 | sed -e "s/__XR_IFNAME__/$iface/g" \
79 | sed -e "s/__XR_IP__/$ipaddr/g" \
80 | sed -e "s/__XR_NM__/$netmask/g" \
81 | sed -e "s/__XR_PERIOD__/$period/g" \
82 | sed -e "s/__XR_TAU__/$tau/g" > /tmp/$iface.click
83
84 /usr/bin/click-align /tmp/$iface.click > /tmp/$iface-aligned.click 2>/var/log/$iface.log
85 [ ! -c /dev/net/tun ] && {
86 mkdir -p /dev/net/
87 mknod /dev/net/tun c 10 200
88 if [ ! -c /dev/net/tun ]; then
89 logger -t "$config" "Device not available (/dev/net/tun). Exiting."
90 exit 1
91 fi
92 }
93
94 # creating the tun interface below will trigger a net subsystem event
95 # prevent it from touching iface by disabling .auto here
96 uci_set_state network "$config" auto 0
97
98 (/usr/bin/click /tmp/$iface-aligned.click >> /var/log/$iface.log 2>&1 &) &
99 sleep 2
100 ps | grep /usr/bin/click | grep -q -v grep || {
101 logger -t "$config" "Unable to start click. Exiting."
102 exit 1
103 }
104
105 ps | grep /usr/bin/click | grep -v grep | awk '{print $1}' > /var/run/$iface.pid
106
107 ifconfig "$iface" "$ipaddr" netmask "$netmask"
108 uci_set_state network $config ipaddr "$ipaddr"
109 uci_set_state network $config netmask "$netmask"
110
111 env -i ACTION="ifup" INTERFACE="$config" DEVICE="$iface" PROTO=wing /sbin/hotplug-call "iface" &
112
113 }
114
115 wing_template_available() { # prefix, template, default
116 local template="/etc/wing/$1.$2.click"
117 [ ! -f $template ] && {
118 return 1
119 }
120 return 0
121 }
122
123 wing_list_interfaces() {
124 local channel freq hwmode hwaddr ifname mode
125 config_get mode $1 mode
126 config_get_bool up $1 up
127 [ "$up" = "1" -a "$mode" = "monitor" ] || return 0
128 config_get ifname $1 ifname
129 config_get device $1 device
130 config_get hwmode $device hwmode "11bg"
131 config_get channel $device channel "0"
132 [ "$channel" = "0" -o "$channel" = "auto" ] && {
133 logger -t "$device" "Channel not specified. Ignoring."
134 return 0
135 }
136 freq=$(iwlist $ifname freq | sed -n "s/^.*Channel 0*$channel : \([0-9.]*\).*/\1/p" | awk '{print $1*1000}')
137 hwaddr=$(/sbin/ifconfig $ifname 2>&1 | sed -n 's/^.*HWaddr \([0-9A-Za-z\-]*\).*/\1/p' | sed -e 's/\-/:/g' | cut -c1-17)
138 freqs=${freqs:+"$freqs "}$freq
139 hwmodes=${hwmodes:+"$hwmodes "}$hwmode
140 hwaddrs=${hwaddrs:+"$hwaddrs "}$hwaddr
141 ifnames=${ifnames:+"$ifnames "}$ifname
142 /sbin/ifconfig $ifname mtu 1900
143 /sbin/ifconfig $ifname txqueuelen 5
144 /sbin/ifconfig $ifname up
145 }
146