[packages] wing: don't use symlink to /lib/functions.sh
[packages.git] / net / wing / files / lib / netifd / proto / wing.sh
1 #!/bin/sh
2
3 . /lib/functions.sh
4 . ../netifd-proto.sh
5 init_proto "$@"
6
7 proto_wing_init_config() {
8         no_device=1
9         available=1
10         proto_config_add_string "ipaddr"
11         proto_config_add_string "netmask"
12 }
13
14 proto_wing_teardown() {
15         local config="$1"
16         local link="wing-$config"
17         [ -f "/var/run/$link.pid" ] && {
18                 kill -9 $(cat /var/run/$link.pid)
19                 rm /var/run/$link.pid
20         }
21         env -i ACTION="ifdown" INTERFACE="$config" DEVICE="$link" PROTO=wing /sbin/hotplug-call "link" &
22 }
23
24 proto_wing_setup() {
25
26         local iface="$2"
27         local config="$1"
28         local link="wing-$config"
29
30         local hwmodes=""
31         local freqs=""
32         local ifnames=""
33         local hwaddrs=""
34
35         # temporary hack waiting for a way to delay wing interfaces until the
36         # wifi sub-system has been brought up
37         sleep 15
38
39         config_load wireless
40         config_foreach wing_list_interfaces wifi-iface
41
42         # start click router
43         if [ "$hwmodes" = "" -o "$freqs" = "" -o "$ifnames" = "" -o "$hwaddrs" = "" ]; then
44                 logger -t "$config" "No raw interfaces available. Exiting."
45                 exit 1
46         fi
47
48         local profile rc ls metric prefix period tau debug
49
50         config_load network
51         config_get profile $config profile "bulk"
52         config_get rc $config rc "minstrel"
53         config_get ls $config ls "fcfs"
54         config_get metric $config metric "wcett"
55         config_get prefix $config prefix "6"
56         config_get period $config period "10000"
57         config_get tau $config tau "100000"
58         config_get_bool debug $config debug "false"
59
60         local hwaddr=$(echo $hwaddrs | sed 's/ .*//');
61         local ipaddr=$(printf "$prefix.%d.%d.%d" $(echo $hwaddr | awk -F: '{printf "0x%s 0x%s 0x%s",$4,$5,$6}'))
62         local bcast="$prefix.255.255.255"
63         local netmask=255.0.0.0
64
65         if ! wing_template_available "profile" "$profile"; then
66                 logger -t "$config" "Unable to configure router. Exiting."
67                 exit 1
68         fi
69
70         if ! wing_template_available "rc" "$rc"; then
71                 logger -t "$config" "Unable to configure rate control. Exiting."
72                 exit 1
73         fi
74
75         if ! wing_template_available "ls" "$ls"; then
76                 logger -t "$config" "Unable to configure link scheduler. Exiting."
77                 exit 1
78         fi
79
80         if [ "$profile" = "" -o "$rc" = "" ]; then
81                 logger -t "$config" "Unable to generate template. Exiting."
82                 exit 1
83         fi
84
85         [ "$debug" == 0 ] && dbg="" || dbg="-d"
86
87         export CLICK_BACKTRACE=1
88
89         /usr/bin/click_config -p $profile -r $rc -s $ls -l $metric \
90                 -m "$hwmodes" -c "$freqs" -n "$ifnames" -a "$hwaddrs" $dbg \
91                 | sed -e "s/__XR_IFNAME__/$link/g" \
92                 | sed -e "s/__XR_IP__/$ipaddr/g" \
93                 | sed -e "s/__XR_BCAST__/$bcast/g" \
94                 | sed -e "s/__XR_NM__/$netmask/g" \
95                 | sed -e "s/__XR_PERIOD__/$period/g" \
96                 | sed -e "s/__XR_TAU__/$tau/g" > /tmp/$link.click
97
98         /usr/bin/click-align /tmp/$link.click > /tmp/$link-aligned.click 2>/var/log/$link.log
99         [ ! -c /dev/net/tun ] && {
100                 mkdir -p /dev/net/
101                 mknod /dev/net/tun c 10 200
102                 if [ ! -c /dev/net/tun ]; then
103                         logger -t "$config" "Device not available (/dev/net/tun). Exiting."
104                         exit 1
105                 fi
106         }
107
108         (/usr/bin/click /tmp/$link-aligned.click >> /var/log/$link.log 2>&1 &) &
109         sleep 2
110         ps | grep /usr/bin/click | grep -q -v grep || {
111                 logger -t "$config" "Unable to start click. Exiting."
112                 exit 1
113         }
114
115         ps | grep /usr/bin/click | grep -v grep | awk '{print $1}' > /var/run/$link.pid
116
117         uci_set_state network $config ifname "$iface"
118         uci_set_state network $config ipaddr "$ipaddr"
119         uci_set_state network $config netmask "$netmask"
120         uci_set_state network $config gateway "0.0.0.0"
121
122         env -i ACTION="ifup" INTERFACE="$config" DEVICE="$link" PROTO=wing /sbin/hotplug-call "link" &
123
124         proto_init_update "$link" 1
125         proto_add_ipv4_address "$ipaddr" "$netmask"
126         proto_add_ipv4_route "$prefix.0.0.0" "255.0.0.0" "$iface"
127
128         route -n | grep -q '^0.0.0.0' || {
129                 proto_add_ipv4_route "0.0.0.0" "0" "$iface"
130         }
131         
132         proto_send_update "$config"
133
134 }
135
136 wing_template_available() { # prefix, template, default
137         local template="/etc/wing/$1.$2.click"
138         [ ! -f $template ] && {
139                 return 1
140         }
141         return 0
142 }
143
144 wing_list_interfaces() {
145         local channel freq hwmode hwaddr ifname mode
146         config_get mode $1 mode
147         config_get_bool up $1 up
148         [ "$up" = "1" -a "$mode" = "monitor" ] || return 0
149         config_get ifname $1 ifname
150         config_get device $1 device
151         config_get hwmode $device hwmode "11bg"
152         config_get channel $device channel "0"
153         [ "$channel" = "0" -o "$channel" = "auto" ] && {
154                 logger -t "$device" "Channel not specified. Ignoring."
155                 return 0
156         }
157         freq=$(iwlist $ifname freq | sed -n "s/^.*Channel 0*$channel : \([0-9.]*\).*/\1/p" | awk '{print $1*1000}')
158         hwaddr=$(/sbin/ifconfig $ifname 2>&1 | sed -n 's/^.*HWaddr \([0-9A-Za-z\-]*\).*/\1/p' | sed -e 's/\-/:/g' | cut -c1-17)
159         freqs=${freqs:+"$freqs "}$freq
160         hwmodes=${hwmodes:+"$hwmodes "}$hwmode
161         hwaddrs=${hwaddrs:+"$hwaddrs "}$hwaddr
162         ifnames=${ifnames:+"$ifnames "}$ifname
163         /sbin/ifconfig $ifname mtu 1900
164         /sbin/ifconfig $ifname txqueuelen 5
165         /sbin/ifconfig $ifname up
166 }
167
168 add_protocol wing
169