add initial support for handling wireless devices via scripts
authorFelix Fietkau <nbd@openwrt.org>
Mon, 21 Oct 2013 18:15:31 +0000 (20:15 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Fri, 29 Nov 2013 13:52:01 +0000 (14:52 +0100)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
CMakeLists.txt
config.c
config/wireless [new file with mode: 0644]
examples/wireless/mac80211.sh [new file with mode: 0755]
netifd.h
scripts/netifd-wireless.sh [new file with mode: 0644]
ubus.c
ubus.h
wireless.c [new file with mode: 0644]
wireless.h [new file with mode: 0644]

index 632d1b9480b734bee420c92c8938164a862fd402..65da3cf90e23e64aeea2c1da3748920ac103ccb4 100644 (file)
@@ -15,7 +15,7 @@ SET(SOURCES
        interface.c interface-ip.c interface-event.c
        iprule.c proto.c proto-static.c proto-shell.c
        config.c device.c bridge.c vlan.c alias.c
-       macvlan.c ubus.c)
+       macvlan.c ubus.c wireless.c)
 
 
 find_library(json NAMES json-c json)
index 49bae15dfe21599d6dc6058f2598760b429f1196..bb0cd05eec8f5805227c88ddc1ba6e7b218146f3 100644 (file)
--- a/config.c
+++ b/config.c
 #include "interface-ip.h"
 #include "iprule.h"
 #include "proto.h"
+#include "wireless.h"
 #include "config.h"
 
 bool config_init = false;
 
 static struct uci_context *uci_ctx;
 static struct uci_package *uci_network;
+static struct uci_package *uci_wireless;
 static struct blob_buf b;
 
 static int
@@ -278,6 +280,85 @@ config_init_globals(void)
        interface_ip_set_ula_prefix(ula_prefix);
 }
 
+static void
+config_parse_wireless_device(struct uci_section *s)
+{
+       struct wireless_driver *drv;
+       const char *driver_name;
+
+       driver_name = uci_lookup_option_string(uci_ctx, s, "type");
+       if (!driver_name)
+               return;
+
+       drv = avl_find_element(&wireless_drivers, driver_name, drv, node);
+       if (!drv)
+               return;
+
+       blob_buf_init(&b, 0);
+       uci_to_blob(&b, s, drv->device.config);
+       wireless_device_create(drv, s->e.name, b.head);
+}
+
+static void
+config_parse_wireless_interface(struct wireless_device *wdev, struct uci_section *s)
+{
+       blob_buf_init(&b, 0);
+       uci_to_blob(&b, s, wdev->drv->interface.config);
+       wireless_interface_create(wdev, b.head);
+}
+
+static void
+config_init_wireless(void)
+{
+       struct wireless_device *wdev;
+       struct uci_element *e;
+       const char *dev_name;
+
+       if (!uci_wireless) {
+               DPRINTF("No wireless configuration found\n");
+               return;
+       }
+
+       vlist_update(&wireless_devices);
+
+       uci_foreach_element(&uci_wireless->sections, e) {
+               struct uci_section *s = uci_to_section(e);
+               if (strcmp(s->type, "wifi-device") != 0)
+                       continue;
+
+               config_parse_wireless_device(s);
+       }
+
+       vlist_flush(&wireless_devices);
+
+       vlist_for_each_element(&wireless_devices, wdev, node) {
+               wdev->vif_idx = 0;
+               vlist_update(&wdev->interfaces);
+       }
+
+       uci_foreach_element(&uci_wireless->sections, e) {
+               struct uci_section *s = uci_to_section(e);
+
+               if (strcmp(s->type, "wifi-iface") != 0)
+                       continue;
+
+               dev_name = uci_lookup_option_string(uci_ctx, s, "device");
+               if (!dev_name)
+                       continue;
+
+               wdev = vlist_find(&wireless_devices, dev_name, wdev, node);
+               if (!wdev) {
+                       DPRINTF("device %s not found!\n", dev_name);
+                       continue;
+               }
+
+               config_parse_wireless_interface(wdev, s);
+       }
+
+       vlist_for_each_element(&wireless_devices, wdev, node)
+               vlist_flush(&wdev->interfaces);
+}
+
 void
 config_init_all(void)
 {
@@ -287,6 +368,8 @@ config_init_all(void)
                return;
        }
 
+       uci_wireless = config_init_package("wireless");
+
        vlist_update(&interfaces);
        config_init = true;
        device_lock();
@@ -297,6 +380,7 @@ config_init_all(void)
        config_init_routes();
        config_init_rules();
        config_init_globals();
+       config_init_wireless();
 
        config_init = false;
        device_unlock();
@@ -307,4 +391,5 @@ config_init_all(void)
        device_free_unused(NULL);
        interface_refresh_assignments(false);
        interface_start_pending();
+       wireless_start_pending();
 }
diff --git a/config/wireless b/config/wireless
new file mode 100644 (file)
index 0000000..5a14619
--- /dev/null
@@ -0,0 +1,20 @@
+config wifi-device  radio0
+       option type     mac80211
+       option channel  11
+       option hwmode   11ng
+       option path     'platform/ar933x_wmac'
+       option htmode   HT20
+       list ht_capab   SHORT-GI-20
+       list ht_capab   SHORT-GI-40
+       list ht_capab   RX-STBC1
+       list ht_capab   DSSS_CCK-40
+       # REMOVE THIS LINE TO ENABLE WIFI:
+       # option disabled 1
+
+config wifi-iface
+       option device   radio0
+       option network  lan
+       option mode     ap
+       option ssid     OpenWrt
+       option encryption none
+
diff --git a/examples/wireless/mac80211.sh b/examples/wireless/mac80211.sh
new file mode 100755 (executable)
index 0000000..aa663bb
--- /dev/null
@@ -0,0 +1,63 @@
+#!/bin/sh
+NETIFD_MAIN_DIR=../../scripts
+. $NETIFD_MAIN_DIR/netifd-wireless.sh
+
+init_wireless_driver "$@"
+
+drv_mac80211_init_device_config() {
+       # identifiers
+       config_add_string macaddr
+       config_add_string path
+       config_add_string phy
+
+       # config
+       config_add_int channel
+       config_add_string hwmode
+       config_add_array ht_capab
+
+       config_add_int chanbw
+}
+
+drv_mac80211_init_iface_config() {
+       config_add_string macaddr
+
+       config_add_boolean wds
+       config_add_int maxassoc
+       config_add_int dtim_period
+
+       config_add_int max_listen_int
+
+       config_add_boolean hidden
+       config_add_boolean wmm
+}
+
+setup_vif() {
+       local name="$1"
+
+       json_select config
+       json_get_var ssid ssid
+       json_select ..
+
+       wireless_add_vif "$name" "dummy-$ssid"
+       /bin/sleep 10 &
+       wireless_add_process "$!" /bin/sleep 1
+}
+
+
+drv_mac80211_setup() {
+       echo "mac80211 setup: $1"
+       json_dump
+       for_each_interface "sta ap adhoc" setup_vif
+       wireless_set_data phy=phy0
+       wireless_set_up
+}
+
+drv_mac80211_teardown() {
+       json_select data
+       json_get_var phy phy
+       json_select ..
+       echo "mac80211 teardown: $1 ($phy)"
+       json_dump
+}
+
+add_driver mac80211
index 1f64555d46f638712394f9049a6b810334d12169..75e997c13233f15c7b9522860d5397de48c8fa71 100644 (file)
--- a/netifd.h
+++ b/netifd.h
@@ -54,6 +54,7 @@ enum {
        DEBUG_SYSTEM    = 0,
        DEBUG_DEVICE    = 1,
        DEBUG_INTERFACE = 2,
+       DEBUG_WIRELESS  = 3,
 };
 
 #ifdef DEBUG
diff --git a/scripts/netifd-wireless.sh b/scripts/netifd-wireless.sh
new file mode 100644 (file)
index 0000000..67ba38b
--- /dev/null
@@ -0,0 +1,321 @@
+NETIFD_MAIN_DIR="${NETIFD_MAIN_DIR:-/lib/netifd}"
+
+. /usr/share/libubox/jshn.sh
+. $NETIFD_MAIN_DIR/utils.sh
+
+CMD_UP=0
+CMD_SET_DATA=1
+CMD_PROCESS_ADD=2
+CMD_PROCESS_KILL_ALL=3
+CMD_SET_RETRY=4
+
+add_driver() {
+       return
+}
+
+wireless_setup_vif_failed() {
+       local error="$1"
+}
+
+wireless_setup_failed() {
+       local error="$1"
+
+       wireless_set_retry 0
+}
+
+prepare_key_wep() {
+       local key="$1"
+       local hex=1
+
+       echo -n "$key" | grep -qE "[^a-fA-F0-9]" && hex=0
+       [ "${#key}" -eq 10 -a $hex -eq 1 ] || \
+       [ "${#key}" -eq 26 -a $hex -eq 1 ] || {
+               [ "${key:0:2}" = "s:" ] && key="${key#s:}"
+               key="$(echo -n "$key" | hexdump -ve '1/1 "%02x" ""')"
+       }
+       echo "$key"
+}
+
+_wdev_prepare_channel() {
+       json_get_vars channel hwmode
+
+       auto_channel=0
+       enable_ht=0
+       htmode=
+       hwmode="${hwmode##11}"
+       hwmode_n="${hwmode##n}"
+
+       case "$channel" in
+               0|auto)
+                       channel=0
+                       auto_channel=1
+               ;;
+               [0-9]+) ;;
+               *)
+                       wireless_setup_failed "INVALID_CHANNEL"
+               ;;
+       esac
+
+       [[ "$hwmode_n" = "$hwmode" ]] && {
+               enable_ht=1
+               hwmode="$hwmode_n"
+
+               json_get_vars htmode
+               case "$htmode" in
+                       HT20|HT40+|HT40-);;
+                       *) htmode= ;;
+               esac
+       }
+
+       case "$hwmode" in
+               a|b|g) ;;
+               *)
+                       if [ "$channel" -gt 14 ]; then
+                               hwmode=a
+                       else
+                               hwmode=g
+                       fi
+               ;;
+       esac
+}
+
+_wdev_handler() {
+       json_load "$data"
+
+       json_select config
+       _wdev_prepare_channel
+       json_select ..
+
+       eval "drv_$1_$2 \"$interface\""
+}
+
+_wdev_msg_call() {
+       local old_cb
+
+       json_set_namespace wdev old_cb
+       "$@"
+       json_set_namespace $old_cb
+}
+
+_wdev_wrapper() {
+       while [ -n "$1" ]; do
+               eval "$1() { _wdev_msg_call _$1 \"\$@\"; }"
+               shift
+       done
+}
+
+_wdev_notify_init() {
+       local command="$1"
+       local interface="$2"
+
+       json_init
+       json_add_int "command" "$command"
+       json_add_string "device" "$__netifd_device"
+       [ -n "$interface" ] && json_add_string "interface" "$interface"
+       json_add_object "data"
+}
+
+_wdev_notify() {
+       local options="$1"
+
+       json_close_object
+       ubus $options call network.wireless notify "$(json_dump)"
+}
+
+_wdev_add_variables() {
+       while [ -n "$1" ]; do
+               local var="${1%%=*}"
+               local val="$1"
+               shift
+               [[ "$var" = "$val" ]] && continue
+               val="${val#*=}"
+               json_add_string "$var" "$val"
+       done
+}
+
+_wireless_add_vif() {
+       local name="$1"; shift
+       local ifname="$1"; shift
+
+       _wdev_notify_init $CMD_SET_DATA "$name"
+       json_add_string "ifname" "$ifname"
+       _wdev_add_variables "$@"
+       _wdev_notify
+}
+
+_wireless_set_up() {
+       _wdev_notify_init $CMD_UP
+       _wdev_notify
+}
+
+_wireless_set_data() {
+       _wdev_notify_init $CMD_SET_DATA
+       _wdev_add_variables "$@"
+       _wdev_notify
+}
+
+_wireless_add_process() {
+       _wdev_notify_init $CMD_PROCESS_ADD
+       json_add_int pid "$1"
+       json_add_string exe "$2"
+       [ -n "$3" ] && json_add_boolean required 1
+       _wdev_notify
+}
+
+_wireless_process_kill_all() {
+       _wdev_notify_init $CMD_PROCESS_KILL_ALL
+       [ -n "$1" ] && json_add_int signal "$1"
+       _wdev_notify
+}
+
+_wireless_set_retry() {
+       _wdev_notify_init $CMD_SET_RETRY
+       json_add_int retry "$1"
+       _wdev_notify
+}
+
+_wdev_wrapper \
+       wireless_add_vif \
+       wireless_set_up \
+       wireless_set_data \
+       wireless_add_process \
+       wireless_process_kill_all \
+       wireless_set_retry \
+
+wireless_vif_parse_encryption() {
+       json_get_vars encryption
+       set_default encryption none
+
+       auth_mode_open=1
+       auth_mode_shared=0
+       auth_type=none
+       wpa_pairwise=CCMP
+       case "$encryption" in
+               *tkip+aes|*tkip+ccmp|*aes+tkip|*ccmp+tkip) wpa_pairwise="CCMP TKIP";;
+               *aes|*ccmp) wpa_pairwise="CCMP";;
+               *tkip) wpa_pairwise="TKIP";;
+       esac
+
+       # 802.11n requires CCMP for WPA
+       [ "$enable_ht:$wpa_pairwise" = "1:TKIP" ] && wpa_pairwise="CCMP TKIP"
+
+       # Examples:
+       # psk-mixed/tkip    => WPA1+2 PSK, TKIP
+       # wpa-psk2/tkip+aes => WPA2 PSK, CCMP+TKIP
+       # wpa2/tkip+aes     => WPA2 RADIUS, CCMP+TKIP
+
+       case "$encryption" in
+               wpa2*|*psk2*)
+                       wpa=2
+               ;;
+               *mixed*)
+                       wpa=3
+               ;;
+               wpa*|*psk*)
+                       wpa=1
+               ;;
+               *)
+                       wpa=0
+                       wpa_pairwise=
+               ;;
+       esac
+
+       case "$encryption" in
+               *psk*)
+                       auth_type=psk
+               ;;
+               *wpa*|*8021x*)
+                       auth_type=eap
+               ;;
+               *wep*)
+                       auth_type=wep
+                       case "$encryption" in
+                               *shared*)
+                                       auth_mode_open=0
+                                       auth_mode_shared=1
+                               ;;
+                               *mixed*)
+                                       auth_mode_shared=1
+                               ;;
+                       esac
+               ;;
+       esac
+}
+
+_get_vif_vars() {
+       # internal use
+       json_get_var _w_type mode
+
+       # for drivers
+       json_get_var network_bridge bridge
+}
+
+for_each_interface() {
+       local _w_types="$1"; shift
+       local _w_ifaces _w_iface
+       local _w_type
+       local _w_found
+
+       json_get_keys _w_ifaces interfaces
+       json_select interfaces
+       for _w_iface in $_w_ifaces; do
+               json_select "$_w_iface"
+               if [ -n "$_w_types" ]; then
+                       json_select config
+                       _get_vif_vars
+                       json_select ..
+                       _w_types=" $_w_types "
+                       [[ "${_w_types%$_w_type*}" = "$_w_types" ]] && {
+                               json_select ..
+                               continue
+                       }
+               fi
+               "$@" "$_w_iface"
+               json_select ..
+       done
+       json_select ..
+}
+
+_wdev_common_device_config() {
+       config_add_string channel hwmode
+}
+
+_wdev_common_iface_config() {
+       config_add_string mode ssid encryption key
+}
+
+init_wireless_driver() {
+       name="$1"; shift
+       cmd="$1"; shift
+
+       case "$cmd" in
+               dump)
+                       add_driver() {
+                               json_init
+                               json_add_string name "$1"
+
+                               json_add_array device
+                               _wdev_common_device_config
+                               eval "drv_$1_init_device_config"
+                               json_close_array
+
+                               json_add_array iface
+                               _wdev_common_iface_config
+                               eval "drv_$1_init_iface_config"
+                               json_close_array
+
+                               json_dump
+                       }
+               ;;
+               setup|teardown)
+                       interface="$1"; shift
+                       data="$1"; shift
+                       export __netifd_device="$interface"
+
+                       add_driver() {
+                               [[ "$name" == "$1" ]] || return 0
+                               _wdev_handler "$1" "$cmd"
+                       }
+               ;;
+       esac
+}
diff --git a/ubus.c b/ubus.c
index 340a8e417110b2cabc4b388d7da1dad1d54009e9..d8c6a4fe411c334a6ddf6d92ac8ca368d3d641a3 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -22,8 +22,9 @@
 #include "proto.h"
 #include "ubus.h"
 #include "system.h"
+#include "wireless.h"
 
-static struct ubus_context *ctx = NULL;
+struct ubus_context *ubus_ctx = NULL;
 static struct blob_buf b;
 static const char *ubus_path;
 
@@ -319,8 +320,8 @@ static struct ubus_object dev_object = {
 static void
 netifd_ubus_add_fd(void)
 {
-       ubus_add_uloop(ctx);
-       system_fd_set_cloexec(ctx->sock.fd);
+       ubus_add_uloop(ubus_ctx);
+       system_fd_set_cloexec(ubus_ctx->sock.fd);
 }
 
 static void
@@ -331,13 +332,13 @@ netifd_ubus_reconnect_timer(struct uloop_timeout *timeout)
        };
        int t = 2;
 
-       if (ubus_reconnect(ctx, ubus_path) != 0) {
+       if (ubus_reconnect(ubus_ctx, ubus_path) != 0) {
                DPRINTF("failed to reconnect, trying again in %d seconds\n", t);
                uloop_timeout_set(&retry, t * 1000);
                return;
        }
 
-       DPRINTF("reconnected to ubus, new id: %08x\n", ctx->local_id);
+       DPRINTF("reconnected to ubus, new id: %08x\n", ubus_ctx->local_id);
        netifd_ubus_add_fd();
 }
 
@@ -885,7 +886,7 @@ static struct ubus_object iface_object = {
 
 static void netifd_add_object(struct ubus_object *obj)
 {
-       int ret = ubus_add_object(ctx, obj);
+       int ret = ubus_add_object(ubus_ctx, obj);
 
        if (ret != 0)
                fprintf(stderr, "Failed to publish object '%s': %s\n", obj->name, ubus_strerror(ret));
@@ -946,22 +947,138 @@ static void netifd_add_iface_object(void)
        netifd_add_object(&iface_object);
 }
 
+static struct wireless_device *
+get_wdev(struct blob_attr *msg, int *ret)
+{
+       struct blobmsg_policy wdev_policy = {
+               .name = "device",
+               .type = BLOBMSG_TYPE_STRING,
+       };
+       struct blob_attr *dev_attr;
+       struct wireless_device *wdev = NULL;
+
+
+       blobmsg_parse(&wdev_policy, 1, &dev_attr, blob_data(msg), blob_len(msg));
+       if (!dev_attr) {
+               *ret = UBUS_STATUS_INVALID_ARGUMENT;
+               return NULL;
+       }
+
+       wdev = vlist_find(&wireless_devices, blobmsg_data(dev_attr), wdev, node);
+       if (!wdev) {
+               *ret = UBUS_STATUS_NOT_FOUND;
+               return NULL;
+       }
+
+       *ret = 0;
+       return wdev;
+}
+
+static int
+netifd_handle_wdev_up(struct ubus_context *ctx, struct ubus_object *obj,
+                     struct ubus_request_data *req, const char *method,
+                     struct blob_attr *msg)
+{
+       struct wireless_device *wdev;
+       int ret;
+
+       wdev = get_wdev(msg, &ret);
+       if (!wdev)
+               return ret;
+
+       wireless_device_set_up(wdev);
+       return 0;
+}
+
+static int
+netifd_handle_wdev_down(struct ubus_context *ctx, struct ubus_object *obj,
+                       struct ubus_request_data *req, const char *method,
+                       struct blob_attr *msg)
+{
+       struct wireless_device *wdev;
+       int ret;
+
+       wdev = get_wdev(msg, &ret);
+       if (!wdev)
+               return ret;
+
+       wireless_device_set_down(wdev);
+       return 0;
+}
+
+static int
+netifd_handle_wdev_status(struct ubus_context *ctx, struct ubus_object *obj,
+                         struct ubus_request_data *req, const char *method,
+                         struct blob_attr *msg)
+{
+       struct wireless_device *wdev;
+       int ret;
+
+       wdev = get_wdev(msg, &ret);
+       if (ret == UBUS_STATUS_NOT_FOUND)
+               return ret;
+
+       blob_buf_init(&b, 0);
+       if (wdev) {
+               wireless_device_status(wdev, &b);
+       } else {
+               vlist_for_each_element(&wireless_devices, wdev, node)
+                       wireless_device_status(wdev, &b);
+       }
+       ubus_send_reply(ctx, req, b.head);
+       return 0;
+}
+
+static int
+netifd_handle_wdev_notify(struct ubus_context *ctx, struct ubus_object *obj,
+                         struct ubus_request_data *req, const char *method,
+                         struct blob_attr *msg)
+{
+       struct wireless_device *wdev;
+       int ret;
+
+       wdev = get_wdev(msg, &ret);
+       if (!wdev)
+               return ret;
+
+       return wireless_device_notify(wdev, msg, req);
+}
+
+static struct ubus_method wireless_object_methods[] = {
+       { .name = "up", .handler = netifd_handle_wdev_up },
+       { .name = "down", .handler = netifd_handle_wdev_down },
+       { .name = "status", .handler = netifd_handle_wdev_status },
+       { .name = "notify", .handler = netifd_handle_wdev_notify },
+};
+
+static struct ubus_object_type wireless_object_type =
+       UBUS_OBJECT_TYPE("netifd_iface", wireless_object_methods);
+
+
+static struct ubus_object wireless_object = {
+       .name = "network.wireless",
+       .type = &wireless_object_type,
+       .methods = wireless_object_methods,
+       .n_methods = ARRAY_SIZE(wireless_object_methods),
+};
+
 int
 netifd_ubus_init(const char *path)
 {
        uloop_init();
        ubus_path = path;
 
-       ctx = ubus_connect(path);
-       if (!ctx)
+       ubus_ctx = ubus_connect(path);
+       if (!ubus_ctx)
                return -EIO;
 
-       DPRINTF("connected as %08x\n", ctx->local_id);
-       ctx->connection_lost = netifd_ubus_connection_lost;
+       DPRINTF("connected as %08x\n", ubus_ctx->local_id);
+       ubus_ctx->connection_lost = netifd_ubus_connection_lost;
        netifd_ubus_add_fd();
 
        netifd_add_object(&main_object);
        netifd_add_object(&dev_object);
+       netifd_add_object(&wireless_object);
        netifd_add_iface_object();
 
        return 0;
@@ -970,7 +1087,7 @@ netifd_ubus_init(const char *path)
 void
 netifd_ubus_done(void)
 {
-       ubus_free(ctx);
+       ubus_free(ubus_ctx);
 }
 
 void
@@ -979,7 +1096,7 @@ netifd_ubus_interface_event(struct interface *iface, bool up)
        blob_buf_init(&b, 0);
        blobmsg_add_string(&b, "action", up ? "ifup" : "ifdown");
        blobmsg_add_string(&b, "interface", iface->name);
-       ubus_send_event(ctx, "network.interface", b.head);
+       ubus_send_event(ubus_ctx, "network.interface", b.head);
 }
 
 void
@@ -989,8 +1106,8 @@ netifd_ubus_interface_notify(struct interface *iface, bool up)
        blob_buf_init(&b, 0);
        blobmsg_add_string(&b, "interface", iface->name);
        netifd_dump_status(iface);
-       ubus_notify(ctx, &iface_object, event, b.head, -1);
-       ubus_notify(ctx, &iface->ubus, event, b.head, -1);
+       ubus_notify(ubus_ctx, &iface_object, event, b.head, -1);
+       ubus_notify(ubus_ctx, &iface->ubus, event, b.head, -1);
 }
 
 void
@@ -1006,7 +1123,7 @@ netifd_ubus_add_interface(struct interface *iface)
        obj->type = &iface_object_type;
        obj->methods = iface_object_methods;
        obj->n_methods = ARRAY_SIZE(iface_object_methods);
-       if (ubus_add_object(ctx, &iface->ubus)) {
+       if (ubus_add_object(ubus_ctx, &iface->ubus)) {
                DPRINTF("failed to publish ubus object for interface '%s'\n", iface->name);
                free(name);
                obj->name = NULL;
@@ -1019,6 +1136,6 @@ netifd_ubus_remove_interface(struct interface *iface)
        if (!iface->ubus.name)
                return;
 
-       ubus_remove_object(ctx, &iface->ubus);
+       ubus_remove_object(ubus_ctx, &iface->ubus);
        free((void *) iface->ubus.name);
 }
diff --git a/ubus.h b/ubus.h
index 553faef99764dd6175da6fb33e268225c37a3fa5..541962258bb186429d5cf6386b363779e9938c24 100644 (file)
--- a/ubus.h
+++ b/ubus.h
@@ -14,6 +14,8 @@
 #ifndef __NETIFD_UBUS_H
 #define __NETIFD_UBUS_H
 
+extern struct ubus_context *ubus_ctx;
+
 int netifd_ubus_init(const char *path);
 void netifd_ubus_done(void);
 void netifd_ubus_add_interface(struct interface *iface);
diff --git a/wireless.c b/wireless.c
new file mode 100644 (file)
index 0000000..52d0eae
--- /dev/null
@@ -0,0 +1,863 @@
+/*
+ * netifd - network interface daemon
+ * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+#include <signal.h>
+#include "netifd.h"
+#include "wireless.h"
+#include "handler.h"
+#include "ubus.h"
+
+#define WIRELESS_SETUP_RETRY   3
+
+struct vlist_tree wireless_devices;
+struct avl_tree wireless_drivers;
+static struct blob_buf b;
+static int drv_fd;
+
+static const struct blobmsg_policy wdev_policy =
+       { .name = "disabled", .type = BLOBMSG_TYPE_BOOL };
+
+static const struct uci_blob_param_list wdev_param = {
+       .n_params = 1,
+       .params = &wdev_policy,
+};
+
+enum {
+       VIF_ATTR_DISABLED,
+       VIF_ATTR_NETWORK,
+       __VIF_ATTR_MAX,
+};
+
+static const struct blobmsg_policy vif_policy[__VIF_ATTR_MAX] = {
+       [VIF_ATTR_DISABLED] = { .name = "disabled", .type = BLOBMSG_TYPE_BOOL },
+       [VIF_ATTR_NETWORK] = { .name = "network", .type = BLOBMSG_TYPE_STRING },
+};
+
+static const struct uci_blob_param_list vif_param = {
+       .n_params = ARRAY_SIZE(vif_policy),
+       .params = vif_policy,
+};
+
+static void
+put_container(struct blob_buf *buf, struct blob_attr *attr, const char *name)
+{
+       void *c = blobmsg_open_table(&b, name);
+       blob_put_raw(&b, blob_data(attr), blob_len(attr));
+       blobmsg_close_table(&b, c);
+}
+
+static void
+vif_config_add_bridge(struct blob_buf *buf, const char *network, bool prepare)
+{
+       struct interface *iface;
+       struct device *dev;
+
+       if (!network)
+               return;
+
+       iface = vlist_find(&interfaces, network, iface, node);
+       if (!iface)
+               return;
+
+       dev = iface->main_dev.dev;
+       if (!dev)
+               return;
+
+       if (dev->type != &bridge_device_type)
+               return;
+
+       blobmsg_add_string(buf, "bridge", dev->ifname);
+}
+
+static void
+prepare_config(struct wireless_device *wdev, struct blob_buf *buf, bool up)
+{
+       struct wireless_interface *vif;
+       void *l, *i;
+
+       blob_buf_init(&b, 0);
+       put_container(&b, wdev->config, "config");
+       if (wdev->data)
+               blobmsg_add_blob(&b, wdev->data);
+
+       l = blobmsg_open_table(&b, "interfaces");
+       vlist_for_each_element(&wdev->interfaces, vif, node) {
+               i = blobmsg_open_table(&b, vif->name);
+               vif_config_add_bridge(&b, vif->network, up);
+               put_container(&b, vif->config, "config");
+               if (vif->data)
+                       blobmsg_add_blob(&b, vif->data);
+               blobmsg_close_table(&b, i);
+       }
+       blobmsg_close_table(&b, l);
+}
+
+static bool
+wireless_process_check(struct wireless_process *proc)
+{
+       return check_pid_path(proc->pid, proc->exe);
+}
+
+static void
+wireless_complete_kill_request(struct wireless_device *wdev)
+{
+       if (!wdev->kill_request)
+               return;
+
+       ubus_complete_deferred_request(ubus_ctx, wdev->kill_request, 0);
+       free(wdev->kill_request);
+       wdev->kill_request = NULL;
+}
+
+static void
+wireless_process_free(struct wireless_device *wdev, struct wireless_process *proc)
+{
+       list_del(&proc->list);
+       free(proc);
+
+       if (list_empty(&wdev->script_proc))
+               wireless_complete_kill_request(wdev);
+}
+
+static void
+wireless_close_script_proc_fd(struct wireless_device *wdev)
+{
+       if (wdev->script_proc_fd.fd < 0)
+               return;
+
+       uloop_fd_delete(&wdev->script_proc_fd);
+       close(wdev->script_proc_fd.fd);
+       wdev->script_proc_fd.fd = -1;
+}
+
+static void
+wireless_process_kill_all(struct wireless_device *wdev, int signal, bool free)
+{
+       struct wireless_process *proc, *tmp;
+
+       list_for_each_entry_safe(proc, tmp, &wdev->script_proc, list) {
+               bool check = wireless_process_check(proc);
+
+               if (check)
+                       kill(proc->pid, signal);
+
+               if (free || !check)
+                       wireless_process_free(wdev, proc);
+       }
+
+       if (free)
+               wireless_close_script_proc_fd(wdev);
+}
+
+static void
+wireless_device_free_state(struct wireless_device *wdev)
+{
+       struct wireless_interface *vif;
+
+       wireless_complete_kill_request(wdev);
+       free(wdev->data);
+       wdev->data = NULL;
+       vlist_for_each_element(&wdev->interfaces, vif, node) {
+               free(vif->data);
+               vif->data = NULL;
+               vif->ifname = NULL;
+       }
+}
+
+static void wireless_interface_handle_link(struct wireless_interface *vif, bool up)
+{
+       struct interface *iface;
+
+       if (!vif->network || !vif->ifname)
+               return;
+
+       iface = vlist_find(&interfaces, vif->network, iface, node);
+       if (!iface)
+               return;
+
+       interface_handle_link(iface, vif->ifname, up);
+}
+
+static void
+wireless_device_setup_cancel(struct wireless_device *wdev)
+{
+       if (wdev->cancel)
+               return;
+
+       wdev->cancel = true;
+       uloop_timeout_set(&wdev->timeout, 10 * 1000);
+}
+
+static void
+wireless_device_run_handler(struct wireless_device *wdev, bool up)
+{
+       const char *action = up ? "setup" : "teardown";
+       const char *argv[6];
+       char *config;
+       int i = 0;
+       int fds[2] = { -1, -1 };
+
+       prepare_config(wdev, &b, up);
+       config = blobmsg_format_json(b.head, true);
+
+       argv[i++] = wdev->drv->script;
+       argv[i++] = wdev->drv->name;
+       argv[i++] = action;
+       argv[i++] = wdev->name;
+       argv[i++] = config;
+       argv[i] = NULL;
+
+       if (up && pipe(fds) == 0) {
+               wdev->script_proc_fd.fd = fds[0];
+               uloop_fd_add(&wdev->script_proc_fd,
+                            ULOOP_READ | ULOOP_EDGE_TRIGGER);
+       }
+
+       netifd_start_process(argv, NULL, &wdev->script_task);
+
+       if (fds[1] >= 0)
+               close(fds[1]);
+
+       free(config);
+}
+
+static void
+__wireless_device_set_up(struct wireless_device *wdev)
+{
+       if (wdev->state != IFS_DOWN || config_init)
+               return;
+
+       wdev->state = IFS_SETUP;
+       wireless_device_run_handler(wdev, true);
+}
+
+static void
+wireless_device_mark_down(struct wireless_device *wdev)
+{
+       struct wireless_interface *vif;
+
+       vlist_for_each_element(&wdev->interfaces, vif, node)
+               wireless_interface_handle_link(vif, false);
+
+       wireless_process_kill_all(wdev, SIGTERM, true);
+
+       wdev->state = IFS_DOWN;
+       wireless_device_free_state(wdev);
+
+       if (wdev->autostart)
+               __wireless_device_set_up(wdev);
+}
+
+static void
+wireless_device_mark_up(struct wireless_device *wdev)
+{
+       struct wireless_interface *vif;
+
+       wdev->state = IFS_UP;
+       vlist_for_each_element(&wdev->interfaces, vif, node)
+               wireless_interface_handle_link(vif, true);
+}
+
+static void
+wireless_device_setup_timeout(struct uloop_timeout *timeout)
+{
+       struct wireless_device *wdev = container_of(timeout, struct wireless_device, timeout);
+
+       netifd_kill_process(&wdev->script_task);
+       wdev->script_task.cb(&wdev->script_task, -1);
+       wireless_device_mark_down(wdev);
+}
+
+void
+wireless_device_set_up(struct wireless_device *wdev)
+{
+       wdev->retry = WIRELESS_SETUP_RETRY;
+       wdev->autostart = true;
+       __wireless_device_set_up(wdev);
+}
+
+static void
+__wireless_device_set_down(struct wireless_device *wdev)
+{
+       if (wdev->state == IFS_TEARDOWN || wdev->state == IFS_DOWN)
+               return;
+
+       if (wdev->script_task.uloop.pending) {
+               wireless_device_setup_cancel(wdev);
+               return;
+       }
+
+       wdev->state = IFS_TEARDOWN;
+       wireless_device_run_handler(wdev, false);
+}
+
+static void
+wireless_device_retry_setup(struct wireless_device *wdev)
+{
+       if (--wdev->retry < 0)
+               wdev->autostart = false;
+
+       __wireless_device_set_down(wdev);
+}
+
+static void
+wireless_device_script_task_cb(struct netifd_process *proc, int ret)
+{
+       struct wireless_device *wdev = container_of(proc, struct wireless_device, script_task);
+
+       switch (wdev->state) {
+       case IFS_SETUP:
+               wireless_device_retry_setup(wdev);
+               break;
+       case IFS_TEARDOWN:
+               wireless_device_mark_down(wdev);
+               break;
+       default:
+               break;
+       }
+}
+
+void
+wireless_device_set_down(struct wireless_device *wdev)
+{
+       wdev->autostart = false;
+       __wireless_device_set_down(wdev);
+}
+
+static void
+wireless_device_free(struct wireless_device *wdev)
+{
+    vlist_flush_all(&wdev->interfaces);
+    free(wdev->config);
+    free(wdev);
+}
+
+static void
+wdev_handle_config_change(struct wireless_device *wdev)
+{
+       switch(wdev->config_state) {
+       case IFC_NORMAL:
+               break;
+       case IFC_RELOAD:
+               if (wdev->autostart)
+                       __wireless_device_set_up(wdev);
+               break;
+       case IFC_REMOVE:
+               wireless_device_free(wdev);
+               break;
+       }
+}
+
+static void
+wdev_set_config_state(struct wireless_device *wdev, enum interface_config_state s)
+{
+       enum interface_config_state old_state = wdev->config_state;
+
+       wdev->config_state = s;
+       if (old_state != IFC_NORMAL)
+               return;
+
+       if (wdev->state == IFS_DOWN)
+               wdev_handle_config_change(wdev);
+       else
+               __wireless_device_set_down(wdev);
+}
+
+static void
+wdev_change_config(struct wireless_device *wdev, struct wireless_device *wd_new)
+{
+       struct blob_attr *new_config = wd_new->config;
+
+       free(wd_new);
+
+       if (blob_attr_equal(wdev->config, new_config))
+               return;
+
+       D(WIRELESS, "Update configuration of wireless device '%s'\n", wdev->name);
+       free(wdev->config);
+       wdev->config = blob_memdup(new_config);
+       wdev_set_config_state(wdev, IFC_RELOAD);
+}
+
+static void
+wdev_create(struct wireless_device *wdev)
+{
+       wdev->retry = WIRELESS_SETUP_RETRY;
+       wdev->config = blob_memdup(wdev->config);
+}
+
+static void
+wdev_update(struct vlist_tree *tree, struct vlist_node *node_new,
+           struct vlist_node *node_old)
+{
+       struct wireless_device *wd_old = container_of(node_old, struct wireless_device, node);
+       struct wireless_device *wd_new = container_of(node_new, struct wireless_device, node);
+
+       if (wd_old && wd_new) {
+               wdev_change_config(wd_old, wd_new);
+       } else if (wd_old) {
+               D(WIRELESS, "Delete wireless device '%s'\n", wd_old->name);
+               wdev_set_config_state(wd_old, IFC_REMOVE);
+       } else if (wd_new) {
+               D(WIRELESS, "Create wireless device '%s'\n", wd_new->name);
+               wdev_create(wd_new);
+       }
+}
+
+static void
+wireless_add_handler(const char *script, const char *name, json_object *obj)
+{
+       struct wireless_driver *drv;
+       char *name_str, *script_str;
+       json_object *dev_config_obj, *iface_config_obj;
+       struct uci_blob_param_list *dev_config, *iface_config;
+
+       dev_config_obj = json_get_field(obj, "device", json_type_array);
+       iface_config_obj = json_get_field(obj, "iface", json_type_array);
+
+       if (!dev_config_obj || !iface_config_obj)
+               return;
+
+       drv = calloc_a(sizeof(*drv),
+               &name_str, strlen(name) + 1,
+               &script_str, strlen(script) + 1,
+               &dev_config, sizeof(*dev_config) + sizeof(void *),
+               &iface_config, sizeof(*iface_config) + sizeof(void *));
+
+       drv->name = strcpy(name_str, name);
+       drv->script = strcpy(script_str, script);
+
+       dev_config->n_next = 1;
+       dev_config->next[0] = &wdev_param;
+       drv->device.config = dev_config;
+
+       iface_config->n_next = 1;
+       iface_config->next[0] = &vif_param;
+       drv->interface.config = iface_config;
+
+       drv->device.buf = netifd_handler_parse_config(drv->device.config, dev_config_obj);
+       drv->interface.buf = netifd_handler_parse_config(drv->interface.config, iface_config_obj);
+
+       drv->node.key = drv->name;
+       avl_insert(&wireless_drivers, &drv->node);
+       D(WIRELESS, "Add handler for script %s: %s\n", script, name);
+}
+
+static void __init
+wireless_init_list(void)
+{
+       vlist_init(&wireless_devices, avl_strcmp, wdev_update);
+       wireless_devices.keep_old = true;
+       wireless_devices.no_delete = true;
+
+       avl_init(&wireless_drivers, avl_strcmp, false, NULL);
+       drv_fd = netifd_open_subdir("wireless");
+       if (drv_fd < 0)
+               return;
+
+       netifd_init_script_handlers(drv_fd, wireless_add_handler);
+}
+
+static void
+wireless_interface_init_config(struct wireless_interface *vif)
+{
+       struct blob_attr *tb[__VIF_ATTR_MAX];
+       struct blob_attr *cur;
+
+       vif->network = NULL;
+       blobmsg_parse(vif_policy, __VIF_ATTR_MAX, tb, blob_data(vif->config), blob_len(vif->config));
+
+       if ((cur = tb[VIF_ATTR_NETWORK]))
+               vif->network = blobmsg_data(cur);
+}
+
+static void
+vif_update(struct vlist_tree *tree, struct vlist_node *node_new,
+          struct vlist_node *node_old)
+{
+       struct wireless_interface *vif_old = container_of(node_old, struct wireless_interface, node);
+       struct wireless_interface *vif_new = container_of(node_new, struct wireless_interface, node);
+       struct wireless_device *wdev;
+
+       if (vif_old)
+               wdev = vif_old->wdev;
+       else
+               wdev = vif_new->wdev;
+
+       if (vif_old && vif_new) {
+               if (blob_attr_equal(vif_old->config, vif_new->config)) {
+                       free(vif_new);
+                       return;
+               }
+
+               D(WIRELESS, "Update wireless interface %s on device %s\n", vif_new->name, wdev->name);
+               free(vif_old->config);
+               vif_old->config = blob_memdup(vif_new->config);
+               wireless_interface_init_config(vif_old);
+               free(vif_new);
+       } else if (vif_new) {
+               D(WIRELESS, "Create new wireless interface %s on device %s\n", vif_new->name, wdev->name);
+               vif_new->config = blob_memdup(vif_new->config);
+               wireless_interface_init_config(vif_new);
+       } else if (vif_old) {
+               D(WIRELESS, "Delete wireless interface %s on device %s\n", vif_old->name, wdev->name);
+               free(vif_old->config);
+               free(vif_old);
+       }
+
+       wdev_set_config_state(wdev, IFC_RELOAD);
+}
+
+static void
+wireless_proc_poll_fd(struct uloop_fd *fd, unsigned int events)
+{
+       struct wireless_device *wdev = container_of(fd, struct wireless_device, script_proc_fd);
+       char buf[128];
+
+       while (1) {
+               int b = read(fd->fd, buf, sizeof(buf));
+               if (b < 0) {
+                       if (errno == EINTR)
+                               continue;
+
+                       if (errno == EAGAIN)
+                               return;
+
+                       goto done;
+               }
+
+               if (!b)
+                       goto done;
+       }
+
+done:
+       uloop_timeout_set(&wdev->script_check, 0);
+       wireless_close_script_proc_fd(wdev);
+}
+
+static void
+wireless_device_check_script_tasks(struct uloop_timeout *timeout)
+{
+       struct wireless_device *wdev = container_of(timeout, struct wireless_device, script_check);
+       struct wireless_process *proc, *tmp;
+       bool restart = false;
+
+       list_for_each_entry_safe(proc, tmp, &wdev->script_proc, list) {
+               if (wireless_process_check(proc))
+                       continue;
+
+               if (proc->required)
+                       restart = true;
+
+               wireless_process_free(wdev, proc);
+       }
+
+       if (restart)
+               wireless_device_retry_setup(wdev);
+       else
+               uloop_timeout_set(&wdev->script_check, 1000);
+}
+
+void
+wireless_device_create(struct wireless_driver *drv, const char *name, struct blob_attr *data)
+{
+       struct wireless_device *wdev;
+       char *name_buf;
+       struct blob_attr *disabled;
+
+       blobmsg_parse(&wdev_policy, 1, &disabled, blob_data(data), blob_len(data));
+       if (disabled && blobmsg_get_bool(disabled))
+               return;
+
+       wdev = calloc_a(sizeof(*wdev), &name_buf, strlen(name) + 1);
+       wdev->drv = drv;
+       wdev->state = IFS_DOWN;
+       wdev->config_state = IFC_NORMAL;
+       wdev->name = strcpy(name_buf, name);
+       wdev->config = data;
+       wdev->config_autostart = true;
+       wdev->autostart = wdev->config_autostart;
+       INIT_LIST_HEAD(&wdev->script_proc);
+       vlist_init(&wdev->interfaces, avl_strcmp, vif_update);
+       wdev->interfaces.keep_old = true;
+       wdev->interfaces.no_delete = true;
+       vlist_add(&wireless_devices, &wdev->node, wdev->name);
+
+       wdev->timeout.cb = wireless_device_setup_timeout;
+       wdev->script_task.cb = wireless_device_script_task_cb;
+       wdev->script_task.dir_fd = drv_fd;
+       wdev->script_task.log_prefix = wdev->name;
+
+       wdev->script_proc_fd.fd = -1;
+       wdev->script_proc_fd.cb = wireless_proc_poll_fd;
+
+       wdev->script_check.cb = wireless_device_check_script_tasks;
+}
+
+void wireless_interface_create(struct wireless_device *wdev, struct blob_attr *data)
+{
+       struct wireless_interface *vif;
+       struct blob_attr *tb[__VIF_ATTR_MAX];
+       struct blob_attr *cur;
+       char *name_buf;
+       char name[8];
+
+       blobmsg_parse(vif_policy, __VIF_ATTR_MAX, tb, blob_data(data), blob_len(data));
+
+       cur = tb[VIF_ATTR_DISABLED];
+       if (cur && blobmsg_get_bool(cur))
+               return;
+
+       sprintf(name, "%d", wdev->vif_idx++);
+
+       vif = calloc_a(sizeof(*vif), &name_buf, strlen(name) + 1);
+       vif->name = strcpy(name_buf, name);
+       vif->wdev = wdev;
+       vif->config = data;
+       vlist_add(&wdev->interfaces, &vif->node, vif->name);
+}
+
+static void
+wireless_interface_status(struct wireless_interface *iface, struct blob_buf *b)
+{
+       void *i;
+
+       i = blobmsg_open_table(b, iface->name);
+       if (iface->data)
+               blob_put_raw(b, blob_data(iface->data), blob_len(iface->data));
+       blobmsg_close_table(b, i);
+}
+
+void
+wireless_device_status(struct wireless_device *wdev, struct blob_buf *b)
+{
+       struct wireless_interface *iface;
+       void *c, *i;
+
+       c = blobmsg_open_table(b, wdev->name);
+       blobmsg_add_u8(b, "up", wdev->state == IFS_UP);
+       blobmsg_add_u8(b, "pending", wdev->state == IFS_SETUP || wdev->state == IFS_TEARDOWN);
+       blobmsg_add_u8(b, "autostart", wdev->autostart);
+       i = blobmsg_open_table(b, "interfaces");
+       vlist_for_each_element(&wdev->interfaces, iface, node)
+               wireless_interface_status(iface, b);
+       blobmsg_close_table(b, i);
+       blobmsg_close_table(b, c);
+}
+
+static void
+wireless_interface_set_data(struct wireless_interface *vif)
+{
+       enum {
+               VIF_DATA_IFNAME,
+               __VIF_DATA_MAX,
+       };
+       static const struct blobmsg_policy data_policy[__VIF_DATA_MAX] = {
+               [VIF_DATA_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
+       };
+       struct blob_attr *tb[__VIF_DATA_MAX];
+       struct blob_attr *cur;
+
+       blobmsg_parse(data_policy, __VIF_DATA_MAX, tb,
+                     blobmsg_data(vif->data), blobmsg_data_len(vif->data));
+
+       if ((cur = tb[VIF_DATA_IFNAME]))
+               vif->ifname = blobmsg_data(cur);
+}
+
+static int
+wireless_device_add_process(struct wireless_device *wdev, struct blob_attr *data)
+{
+       enum {
+               PROC_ATTR_PID,
+               PROC_ATTR_EXE,
+               PROC_ATTR_REQUIRED,
+               __PROC_ATTR_MAX
+       };
+       static const struct blobmsg_policy proc_policy[__PROC_ATTR_MAX] = {
+               [PROC_ATTR_PID] = { .name = "pid", .type = BLOBMSG_TYPE_INT32 },
+               [PROC_ATTR_EXE] = { .name = "exe", .type = BLOBMSG_TYPE_STRING },
+               [PROC_ATTR_REQUIRED] = { .name = "required", .type = BLOBMSG_TYPE_BOOL },
+       };
+       struct blob_attr *tb[__PROC_ATTR_MAX];
+       struct wireless_process *proc;
+       char *name;
+
+       if (!data)
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       blobmsg_parse(proc_policy, __PROC_ATTR_MAX, tb, blobmsg_data(data), blobmsg_data_len(data));
+       if (!tb[PROC_ATTR_PID] || !tb[PROC_ATTR_EXE])
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       proc = calloc_a(sizeof(*proc),
+               &name, strlen(blobmsg_data(tb[PROC_ATTR_EXE])) + 1);
+
+       proc->pid = blobmsg_get_u32(tb[PROC_ATTR_PID]);
+       proc->exe = strcpy(name, blobmsg_data(tb[PROC_ATTR_EXE]));
+
+       if (tb[PROC_ATTR_REQUIRED])
+               proc->required = blobmsg_get_bool(tb[PROC_ATTR_REQUIRED]);
+
+       list_add(&proc->list, &wdev->script_proc);
+       uloop_timeout_set(&wdev->script_check, 0);
+
+       return 0;
+}
+
+static int
+wireless_device_process_kill_all(struct wireless_device *wdev, struct blob_attr *data,
+                                struct ubus_request_data *req)
+{
+       enum {
+               KILL_ATTR_SIGNAL,
+               KILL_ATTR_IMMEDIATE,
+               __KILL_ATTR_MAX
+       };
+       static const struct blobmsg_policy kill_policy[__KILL_ATTR_MAX] = {
+               [KILL_ATTR_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 },
+               [KILL_ATTR_IMMEDIATE] = { .name = "immediate", .type = BLOBMSG_TYPE_BOOL },
+       };
+       struct blob_attr *tb[__KILL_ATTR_MAX];
+       struct blob_attr *cur;
+       bool immediate = false;
+       int signal = SIGTERM;
+
+       blobmsg_parse(kill_policy, __KILL_ATTR_MAX, tb, blobmsg_data(data), blobmsg_data_len(data));
+
+       if ((cur = tb[KILL_ATTR_SIGNAL]))
+               signal = blobmsg_get_u32(cur);
+
+       if ((cur = tb[KILL_ATTR_IMMEDIATE]))
+               immediate = blobmsg_get_u32(cur);
+
+       if (wdev->state != IFS_TEARDOWN || wdev->kill_request)
+               return UBUS_STATUS_PERMISSION_DENIED;
+
+       wireless_process_kill_all(wdev, signal, immediate);
+
+       if (list_empty(&wdev->script_proc))
+               return 0;
+
+       wdev->kill_request = calloc(1, sizeof(*wdev->kill_request));
+       ubus_defer_request(ubus_ctx, req, wdev->kill_request);
+
+       return 0;
+}
+
+static int
+wireless_device_set_retry(struct wireless_device *wdev, struct blob_attr *data)
+{
+       static const struct blobmsg_policy retry_policy = {
+               .name = "retry", .type = BLOBMSG_TYPE_INT32
+       };
+       struct blob_attr *val;
+
+       blobmsg_parse(&retry_policy, 1, &val, blobmsg_data(data), blobmsg_data_len(data));
+       if (!val)
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       wdev->retry = blobmsg_get_u32(val);
+       return 0;
+}
+
+enum {
+       NOTIFY_CMD_UP = 0,
+       NOTIFY_CMD_SET_DATA = 1,
+       NOTIFY_CMD_PROCESS_ADD = 2,
+       NOTIFY_CMD_PROCESS_KILL_ALL = 3,
+       NOTIFY_CMD_SET_RETRY = 4,
+};
+
+int
+wireless_device_notify(struct wireless_device *wdev, struct blob_attr *data,
+                      struct ubus_request_data *req)
+{
+       enum {
+               NOTIFY_ATTR_COMMAND,
+               NOTIFY_ATTR_VIF,
+               NOTIFY_ATTR_DATA,
+               __NOTIFY_MAX,
+       };
+       static const struct blobmsg_policy notify_policy[__NOTIFY_MAX] = {
+               [NOTIFY_ATTR_COMMAND] = { .name = "command", .type = BLOBMSG_TYPE_INT32 },
+               [NOTIFY_ATTR_VIF] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
+               [NOTIFY_ATTR_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
+       };
+       struct wireless_interface *vif = NULL;
+       struct blob_attr *tb[__NOTIFY_MAX];
+       struct blob_attr *cur, **pdata;
+
+       blobmsg_parse(notify_policy, __NOTIFY_MAX, tb, blob_data(data), blob_len(data));
+
+       if (!tb[NOTIFY_ATTR_COMMAND])
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       if ((cur = tb[NOTIFY_ATTR_VIF]) != NULL) {
+               vif = vlist_find(&wdev->interfaces, blobmsg_data(cur), vif, node);
+               if (!vif)
+                       return UBUS_STATUS_NOT_FOUND;
+       }
+
+       cur = tb[NOTIFY_ATTR_DATA];
+       if (!cur)
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       switch (blobmsg_get_u32(tb[NOTIFY_ATTR_COMMAND])) {
+       case NOTIFY_CMD_UP:
+               if (vif)
+                       return UBUS_STATUS_INVALID_ARGUMENT;
+
+               if (wdev->state != IFS_SETUP)
+                       return UBUS_STATUS_PERMISSION_DENIED;
+
+               if (wdev->cancel)
+                       return 0;
+
+               wireless_device_mark_up(wdev);
+               break;
+       case NOTIFY_CMD_SET_DATA:
+               if (vif)
+                       pdata = &vif->data;
+               else
+                       pdata = &wdev->data;
+
+               if (*pdata)
+                       return UBUS_STATUS_INVALID_ARGUMENT;
+
+               *pdata = blob_memdup(cur);
+               if (vif)
+                       wireless_interface_set_data(vif);
+               break;
+       case NOTIFY_CMD_PROCESS_ADD:
+               return wireless_device_add_process(wdev, cur);
+       case NOTIFY_CMD_PROCESS_KILL_ALL:
+               return wireless_device_process_kill_all(wdev, cur, req);
+       case NOTIFY_CMD_SET_RETRY:
+               return wireless_device_set_retry(wdev, cur);
+       default:
+               return UBUS_STATUS_INVALID_ARGUMENT;
+       }
+
+       return 0;
+}
+
+void
+wireless_start_pending(void)
+{
+       struct wireless_device *wdev;
+
+       vlist_for_each_element(&wireless_devices, wdev, node)
+               if (wdev->autostart)
+                       __wireless_device_set_up(wdev);
+}
diff --git a/wireless.h b/wireless.h
new file mode 100644 (file)
index 0000000..c6609e7
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * netifd - network interface daemon
+ * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+#ifndef __NETIFD_WIRELESS_H
+#define __NETIFD_WIRELESS_H
+
+#include <libubox/utils.h>
+#include "interface.h"
+
+struct vlist_tree wireless_devices;
+struct avl_tree wireless_drivers;
+
+struct wireless_driver {
+       struct avl_node node;
+
+       const char *name;
+       const char *script;
+
+       struct {
+               char *buf;
+               struct uci_blob_param_list *config;
+       } device, interface;
+};
+
+struct wireless_device {
+       struct vlist_node node;
+
+       struct wireless_driver *drv;
+       struct vlist_tree interfaces;
+       char *name;
+
+       struct netifd_process script_task;
+       struct uloop_timeout timeout;
+       struct uloop_timeout poll;
+
+       struct list_head script_proc;
+       struct uloop_fd script_proc_fd;
+       struct uloop_timeout script_check;
+
+       struct ubus_request_data *kill_request;
+
+       struct blob_attr *config;
+       struct blob_attr *data;
+
+       bool config_autostart;
+       bool autostart;
+
+       enum interface_state state;
+       enum interface_config_state config_state;
+       bool cancel;
+       int retry;
+
+       int vif_idx;
+};
+
+struct wireless_interface {
+       struct vlist_node node;
+       char *name;
+
+       struct wireless_device *wdev;
+
+       struct blob_attr *config;
+       struct blob_attr *data;
+
+       const char *ifname;
+       const char *network;
+};
+
+struct wireless_process {
+       struct list_head list;
+
+       const char *exe;
+       int pid;
+
+       bool required;
+};
+
+void wireless_device_create(struct wireless_driver *drv, const char *name, struct blob_attr *data);
+void wireless_device_set_up(struct wireless_device *wdev);
+void wireless_device_set_down(struct wireless_device *wdev);
+void wireless_device_status(struct wireless_device *wdev, struct blob_buf *b);
+void wireless_interface_create(struct wireless_device *wdev, struct blob_attr *data);
+int wireless_device_notify(struct wireless_device *wdev, struct blob_attr *data,
+                          struct ubus_request_data *req);
+
+void wireless_start_pending(void);
+
+#endif