[packages] Port the wing package to the new netifd infrastructure.
[packages.git] / net / wing / files / lib / netifd / proto / wing.sh
1 #!/bin/sh
2
3 . /etc/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_get profile $config profile "bulk"
51         config_get rc $config rc "minstrel"
52         config_get ls $config ls "fcfs"
53         config_get metric $config metric "wcett"
54         config_get prefix $config prefix "6"
55         config_get period $config period "10000"
56         config_get tau $config tau "100000"
57         config_get_bool debug $config debug "false"
58
59         local hwaddr=$(echo $hwaddrs | sed 's/ .*//');
60         local ipaddr=$(printf "$prefix.%d.%d.%d" $(echo $hwaddr | awk -F: '{printf "0x%s 0x%s 0x%s",$4,$5,$6}'))
61         local bcast="$prefix.255.255.255"
62         local netmask=255.0.0.0
63
64         if ! wing_template_available "profile" "$profile"; then
65                 logger -t "$config" "Unable to configure router. Exiting."
66                 exit 1
67         fi
68
69         if ! wing_template_available "rc" "$rc"; then
70                 logger -t "$config" "Unable to configure rate control. Exiting."
71                 exit 1
72         fi
73
74         if ! wing_template_available "ls" "$ls"; then
75                 logger -t "$config" "Unable to configure link scheduler. Exiting."
76                 exit 1
77         fi
78
79         if [ "$profile" = "" -o "$rc" = "" ]; then
80                 logger -t "$config" "Unable to generate template. Exiting."
81                 exit 1
82         fi
83
84         [ "$debug" == 0 ] && dbg="" || dbg="-d"
85
86         /usr/bin/click_config -p $profile -r $rc -s $ls -l $metric \
87                 -m "$hwmodes" -c "$freqs" -n "$ifnames" -a "$hwaddrs" $dbg \
88                 | sed -e "s/__XR_IFNAME__/$link/g" \
89                 | sed -e "s/__XR_IP__/$ipaddr/g" \
90                 | sed -e "s/__XR_BCAST__/$bcast/g" \
91                 | sed -e "s/__XR_NM__/$netmask/g" \
92                 | sed -e "s/__XR_PERIOD__/$period/g" \
93                 | sed -e "s/__XR_TAU__/$tau/g" > /tmp/$link.click
94
95         /usr/bin/click-align /tmp/$link.click > /tmp/$link-aligned.click 2>/var/log/$link.log
96         [ ! -c /dev/net/tun ] && {
97                 mkdir -p /dev/net/
98                 mknod /dev/net/tun c 10 200
99                 if [ ! -c /dev/net/tun ]; then
100                         logger -t "$config" "Device not available (/dev/net/tun). Exiting."
101                         exit 1
102                 fi
103         }
104
105         (/usr/bin/click /tmp/$link-aligned.click >> /var/log/$link.log 2>&1 &) &
106         sleep 2
107         ps | grep /usr/bin/click | grep -q -v grep || {
108                 logger -t "$config" "Unable to start click. Exiting."
109                 exit 1
110         }
111
112         ps | grep /usr/bin/click | grep -v grep | awk '{print $1}' > /var/run/$link.pid
113
114         env -i ACTION="ifup" INTERFACE="$config" DEVICE="$link" PROTO=wing /sbin/hotplug-call "link" &
115
116         proto_init_update "$link" 1
117         proto_add_ipv4_address "$ipaddr" "$netmasj"
118
119         route -n | grep -q '^0.0.0.0' || {
120                 proto_add_ipv4_route "0.0.0.0" 0
121         }
122         
123         proto_send_update "$config"
124
125 }
126
127 wing_template_available() { # prefix, template, default
128         local template="/etc/wing/$1.$2.click"
129         [ ! -f $template ] && {
130                 return 1
131         }
132         return 0
133 }
134
135 wing_list_interfaces() {
136         local channel freq hwmode hwaddr ifname mode
137         config_get mode $1 mode
138         config_get_bool up $1 up
139         [ "$up" = "1" -a "$mode" = "monitor" ] || return 0
140         config_get ifname $1 ifname
141         config_get device $1 device
142         config_get hwmode $device hwmode "11bg"
143         config_get channel $device channel "0"
144         [ "$channel" = "0" -o "$channel" = "auto" ] && {
145                 logger -t "$device" "Channel not specified. Ignoring."
146                 return 0
147         }
148         freq=$(iwlist $ifname freq | sed -n "s/^.*Channel 0*$channel : \([0-9.]*\).*/\1/p" | awk '{print $1*1000}')
149         hwaddr=$(/sbin/ifconfig $ifname 2>&1 | sed -n 's/^.*HWaddr \([0-9A-Za-z\-]*\).*/\1/p' | sed -e 's/\-/:/g' | cut -c1-17)
150         freqs=${freqs:+"$freqs "}$freq
151         hwmodes=${hwmodes:+"$hwmodes "}$hwmode
152         hwaddrs=${hwaddrs:+"$hwaddrs "}$hwaddr
153         ifnames=${ifnames:+"$ifnames "}$ifname
154         /sbin/ifconfig $ifname mtu 1900
155         /sbin/ifconfig $ifname txqueuelen 5
156         /sbin/ifconfig $ifname up
157 }
158
159 add_protocol wing
160