frame-xchg: Try to call a handler only once per frame
PATCH 1/6 frame-xchg: Try to call a handler only once per frame
Try to better deduplicate the frame watches. Until now we'd check if we'd already registered a given frame body prefix with the kernel, or a matching more general prefix (shorter). Now also try to check if we have already have a watch with the same callback pointer and user_data value, and:
* an identical or shorter (more general) prefix, in that case ignore the new watch completely.
* a longer (more specific) prefix, in that case forget the existing watch.
The use case for this is when we have a single callback for multiple watches and multiple frame types, and inside that callback we're looking at the frame body again and matching it to frame types. In that case we don't want that function to be called multiple times for one frame event.
--- src/frame-xchg.c | 70 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 64 insertions(+), 6 deletions(-)
src/frame-xchg.c +64 -6
@@ -280,6 +280,60 @@ static void frame_watch_register_cb(struct l_genl_msg *msg, void *user_data) L_PTR_TO_UINT(user_data), l_genl_msg_get_error(msg)); } +struct frame_duplicate_info { +uint64_t wdev_id; +uint16_t frame_type; +const uint8_t *prefix; +size_t prefix_len; +frame_watch_cb_t handler; +void *user_data; +bool duplicate :1 ; +bool registered :1 ; +}; + +static bool frame_watch_check_duplicate (void *data,void *user_data) +{ +struct watchlist_item *super = data; +struct frame_watch *watch = + l_container_of(super, struct frame_watch, super); +struct frame_duplicate_info *info = user_data; +int common_len = info->prefix_len < watch->prefix_len ? + info->prefix_len : watch->prefix_len; + +if (info->wdev_id != watch->wdev_id || + info->frame_type != watch->frame_type || + (common_len && +memcmp (info->prefix, watch->prefix, common_len))) +/* No match */ +return false; + +if (info->prefix_len >= watch->prefix_len) +/* +* A matching shorter prefix is already registered with +* the kernel, no need to register the new prefix. +*/ + info->registered = true; + +if (info->handler != watch->super.notify || + info->user_data != watch->super.notify_data) +return false; + +/* +* If we already have a watch with the exact same callback and +* user_data and a matching prefix (longer or shorter), drop +* either the existing watch, or the new watch, so as to preserve +* the set of frames that trigger the callback but avoid +* calling back twice with the same user_data. +*/ +if (info->prefix_len >= watch->prefix_len) { + info->duplicate = true; +return false; + } + +/* Drop the existing watch as a duplicate of the new one */ +return true; +} + bool frame_watch_add(uint64_t wdev_id, uint32_t group_id, uint16_t frame_type, const uint8_t *prefix, size_t prefix_len, frame_watch_cb_t handler, void *user_data,
@@ -288,15 +342,19 @@ bool frame_watch_add(uint64_t wdev_id, uint32_t group_id, uint16_t frame_type,struct watch_group *group =frame_watch_group_get (wdev_id, group_id);struct frame_watch *watch;struct l_genl_msg *msg; -struct frame_prefix_info info = { frame_type, prefix, prefix_len, wdev_id }; -bool registered; +struct frame_duplicate_info info = { + wdev_id, frame_type, prefix, prefix_len, + handler, user_data, false, false + };if (!group)return false; - registered =l_queue_find (group->watches.items, - frame_watch_match_prefix, - &info); +l_queue_foreach_remove (group->watches.items, + frame_watch_check_duplicate, &info); + +if (info.duplicate) +return true; watch = l_new(struct frame_watch, 1); watch->frame_type = frame_type;
@@ -307,7 +365,7 @@ bool frame_watch_add(uint64_t wdev_id, uint32_t group_id, uint16_t frame_type,watchlist_link (&group->watches, &watch->super, handler, user_data, destroy); -if (registered) +if (info.registered)return true; msg =l_genl_msg_new_sized (NL80211_CMD_REGISTER_FRAME ,32 + prefix_len);
Signature · 1 line
2.20.1
PATCH 2/6 frame-xchg: Add a frame exchange API
Add a little state machine and a related API, to simplify sending out a frame, receiving the Ack / No-ack status and (if acked) waiting for a response frame from the target device, one of a list of possible frame prefixes. The nl80211 API for this makes it complicated enough that this new API seems to be justified, on top of that there's a quirk when using the brcmfmac driver where the nl80211 response (containing the operation's cookie), the Tx Status event and the response Frame event are received from nl80211 in reverse order (not seen with other drivers so far), further complicating what should be a pretty simple task.
--- src/frame-xchg.c | 556 +++++++++++++++++++++++++++++++++++++++++++++++ src/frame-xchg.h | 15 ++ 2 files changed, 571 insertions(+)
src/frame-xchg.c +556 -0
@@ -25,6 +25,7 @@#endif #include <errno.h> +#include <stdarg.h> #include <ell/ell.h>
@@ -79,6 +80,40 @@ struct wdev_info {static struct l_queue *wdevs; +struct frame_xchg_data { +uint64_t wdev_id; +uint32_t freq; +struct mmpdu_header *tx_mpdu; +size_t tx_mpdu_len; +bool tx_acked; +uint64_t cookie; +bool have_cookie; +bool early_status; +struct { +struct mmpdu_header *mpdu; +const void *body; +size_t body_len; +int rssi; + } early_frame; +struct l_timeout *timeout; +struct l_queue *rx_watches; +frame_xchg_cb_t cb; +void *user_data; +uint32_t group_id; +unsigned int retry_cnt; +unsigned int retry_interval; +unsigned int resp_timeout; +bool in_frame_cb :1 ; +}; + +struct frame_xchg_watch_data { +struct frame_xchg_prefix *prefix; +frame_xchg_resp_cb_t cb; +}; + +static struct l_queue *frame_xchgs; +static struct l_genl_family *nl80211; +struct frame_prefix_info {uint16_t frame_type;const uint8_t *body;
@@ -451,6 +486,514 @@ bool frame_watch_wdev_remove(uint64_t wdev_id) &wdev_id) > 0; } +struct frame_watch_handler_check_info { +frame_watch_cb_t handler; +void *user_data; +}; + +static bool frame_watch_item_remove_by_handler (void *data,void *user_data) +{ +struct frame_watch *watch = data; +struct frame_watch_handler_check_info *info = user_data; + +if (watch->super.notify != info->handler || + watch->super.notify_data != info->user_data) +return false; + +if (watch->super.destroy) + watch->super.destroy (watch->super.notify_data); + +frame_watch_free (&watch->super); +return true; +} + +/* +* Note this one doesn't interact with the kernel watches, only forgets our +* struct frame_watch instances. +* +* Also note empty groups are not automatically destroyed because right now +* this is not desired in frame_xchg_reset -- the only user of this function. +*/ +static bool frame_watch_remove_by_handler (uint64_t wdev_id,uint32_t group_id, +frame_watch_cb_t handler, +void *user_data) +{ +struct watch_group_match_info group_info = + { wdev_id, group_id }; +struct frame_watch_handler_check_info handler_info = + { handler, user_data }; +struct watch_group *group =l_queue_find (watch_groups, + frame_watch_group_match, + &group_info); + +if (!group) +return false; + +return l_queue_foreach_remove (group->watches.items, + frame_watch_item_remove_by_handler, + &handler_info) >0 ; +} + +static void frame_xchg_tx_retry (struct frame_xchg_data *fx); +static void frame_xchg_resp_cb (const struct mmpdu_header *mpdu, +const void *body,size_t body_len, +int rssi,void *user_data); + +static void frame_xchg_wait_cancel (struct frame_xchg_data *fx) +{ +struct l_genl_msg *msg; + +if (!fx->have_cookie) +return ; + +l_debug ("" ); + + msg =l_genl_msg_new_sized (NL80211_CMD_FRAME_WAIT_CANCEL ,32 ); +l_genl_msg_append_attr (msg,NL80211_ATTR_WDEV ,8 , &fx->wdev_id); +l_genl_msg_append_attr (msg,NL80211_ATTR_COOKIE ,8 , &fx->cookie); +l_genl_family_send (nl80211, msg,NULL ,NULL ,NULL ); + + fx->have_cookie = false; +} + +static void frame_xchg_reset (struct frame_xchg_data *fx) +{ + fx->in_frame_cb = false; + +frame_xchg_wait_cancel (fx); + +if (fx->timeout) +l_timeout_remove (fx->timeout); + +l_free (fx->early_frame.mpdu); + fx->early_frame.mpdu =NULL ; +l_queue_destroy (fx->rx_watches, l_free); + fx->rx_watches =NULL ; +l_free (fx->tx_mpdu); + fx->tx_mpdu =NULL ; +frame_watch_remove_by_handler (fx->wdev_id, fx->group_id, + frame_xchg_resp_cb, fx); +} + +static void frame_xchg_destroy (struct frame_xchg_data *fx,int err) +{ +if (fx->cb) + fx->cb (err, fx->user_data); + +frame_xchg_reset (fx); +l_free (fx); +} + +static void frame_xchg_cancel (void *user_data) +{ +struct frame_xchg_data *fx = user_data; + +frame_xchg_destroy (fx, -ECANCELED ); +} + +static void frame_xchg_done (struct frame_xchg_data *fx,int err) +{ +l_queue_remove (frame_xchgs, fx); +frame_xchg_destroy (fx, err); +} + +static void frame_xchg_timeout_destroy (void *user_data) +{ +struct frame_xchg_data *fx = user_data; + + fx->timeout =NULL ; +} + +static void frame_xchg_timeout_cb (struct l_timeout *timeout, +void *user_data) +{ +struct frame_xchg_data *fx = user_data; + +l_timeout_remove (fx->timeout); +frame_xchg_tx_retry (fx); +} + +static void frame_xchg_resp_timeout_cb (struct l_timeout *timeout, +void *user_data) +{ +struct frame_xchg_data *fx = user_data; + +frame_xchg_done (fx,0 ); +} + +static void frame_xchg_tx_status (struct frame_xchg_data *fx,bool acked) +{ +if (!acked) { +frame_xchg_wait_cancel (fx); + +if (!fx->retry_interval || fx->retry_cnt >=15 ) { +if (!fx->resp_timeout) + fx->have_cookie = false; + +l_error ("Frame tx retry limit reached" ); +frame_xchg_done (fx, -ECOMM ); +return ; + } + +l_free (fx->early_frame.mpdu); + fx->early_frame.mpdu =NULL ; + fx->timeout =l_timeout_create_ms (fx->retry_interval, + frame_xchg_timeout_cb, fx, + frame_xchg_timeout_destroy); +return ; + } + +if (!fx->resp_timeout) { +/* No listen period to cancel */ + fx->have_cookie = false; +frame_xchg_done (fx,0 ); +return ; + } + + fx->tx_acked = true; + +/* Process frames received early for strange drivers */ +if (fx->early_frame.mpdu) { +/* The command is now over so no need to cancel it */ + fx->have_cookie = false; + +l_debug ("Processing an early frame" ); +frame_xchg_resp_cb (fx->early_frame.mpdu, fx->early_frame.body, + fx->early_frame.body_len, + fx->early_frame.rssi, fx); + +frame_xchg_done (fx,0 ); +return ; + } + +/* Txed frame ACKed, listen for response frames */ + fx->timeout =l_timeout_create_ms (fx->resp_timeout, + frame_xchg_resp_timeout_cb, fx, + frame_xchg_timeout_destroy); +} + +static void frame_xchg_tx_cb (struct l_genl_msg *msg,void *user_data) +{ +struct frame_xchg_data *fx = user_data; +int error =l_genl_msg_get_error (msg); +uint64_t cookie; +bool early_status; + +l_debug ("err %i" , -error); + +if (error <0 ) { +if (error == -EBUSY ) { + fx->timeout =l_timeout_create_ms (fx->retry_interval, + frame_xchg_timeout_cb, fx, + frame_xchg_timeout_destroy); +return ; + } + +l_error ("Frame tx failed: %s (%i)" ,strerror (-error), -error); + goto error; + } + +if (L_WARN_ON (nl80211_parse_attrs (msg,NL80211_ATTR_COOKIE , &cookie, +NL80211_ATTR_UNSPEC ) <0 )) { + error = -EINVAL ; + goto error; + } + + early_status = fx->early_status && cookie == fx->cookie; + fx->tx_acked = early_status && fx->tx_acked; + fx->have_cookie = true; + fx->cookie = cookie; + +if (early_status) +frame_xchg_tx_status (fx, fx->tx_acked); + +return ; +error: +frame_xchg_done (fx, error); +} + +static void frame_xchg_tx_retry (struct frame_xchg_data *fx) +{ +struct l_genl_msg *msg; +uint32_t cmd_id; +uint32_t duration = fx->resp_timeout; + +/* +* TODO: in Station, AP, P2P-Client, GO or Ad-Hoc modes if we're +* transmitting the frame on the BSS's operating channel we can skip +* NL80211_ATTR_DURATION and we should still receive the frames +* without potentially interfering with other operations. +* +* TODO: we may want to react to NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL +* in the group socket's unicast handler. +*/ + + msg =l_genl_msg_new_sized (NL80211_CMD_FRAME ,128 + fx->tx_mpdu_len); +l_genl_msg_append_attr (msg,NL80211_ATTR_WDEV ,8 , &fx->wdev_id); +l_genl_msg_append_attr (msg,NL80211_ATTR_WIPHY_FREQ ,4 , &fx->freq); +l_genl_msg_append_attr (msg,NL80211_ATTR_OFFCHANNEL_TX_OK ,0 ,NULL ); +l_genl_msg_append_attr (msg,NL80211_ATTR_TX_NO_CCK_RATE ,0 ,NULL ); +l_genl_msg_append_attr (msg,NL80211_ATTR_FRAME , + fx->tx_mpdu_len, fx->tx_mpdu); + +if (duration) +l_genl_msg_append_attr (msg,NL80211_ATTR_DURATION ,4 , + &duration); + + cmd_id =l_genl_family_send (nl80211, msg, frame_xchg_tx_cb, fx,NULL ); +if (!cmd_id) { +l_error ("Error sending frame" ); +l_genl_msg_unref (msg); +frame_xchg_done (fx, -EIO ); +return ; + } + + fx->tx_acked = false; + fx->have_cookie = false; + fx->early_status = false; + fx->retry_cnt++; +} + +static void frame_xchg_resp_cb (const struct mmpdu_header *mpdu, +const void *body,size_t body_len, +int rssi,void *user_data) +{ +struct frame_xchg_data *fx = user_data; +const struct l_queue_entry *entry; +size_t hdr_len; + +l_debug ("" ); + +if (memcmp (mpdu->address_1, fx->tx_mpdu->address_2,6 )) +return ; + +if (memcmp (mpdu->address_2, fx->tx_mpdu->address_1,6 )) +return ; + +/* +* Is the received frame's BSSID same as the transmitted frame's +* BSSID, may have to be moved to the user callback if there are +* usages where this is false. Some drivers (brcmfmac) can't +* report the BSSID so check for all-zeros too. +*/ +if (memcmp (mpdu->address_3, fx->tx_mpdu->address_3,6 ) && + !util_mem_is_zero (mpdu->address_3,6 )) +return ; + +for (entry =l_queue_get_entries (fx->rx_watches); + entry; entry = entry->next) { +struct frame_xchg_watch_data *watch = entry->data; +bool done; + +if (body_len < watch->prefix->len || +memcmp (body, watch->prefix->data, + watch->prefix->len)) +continue ; + +if (!fx->tx_acked) + goto early_frame; + + fx->in_frame_cb = true; + done = watch->cb (mpdu, body, body_len, rssi, fx->user_data); + +/* +* If the callback has started a new frame exchange it will +* have reset and taken over the state variables and we need +* to just exit without touching anything. +*/ +if (!fx->in_frame_cb) +return ; + + fx->in_frame_cb = false; + +if (done) { + fx->cb =NULL ; +frame_xchg_done (fx,0 ); +return ; + } + } + +return ; + +early_frame: +/* +* Work around the strange order of events seen with the brcmfmac +* driver where we receive the response frames before the frame +* Tx status, which in turn is receive before the Tx callback with +* the operation cookie... rather then the reverse. +* Save the response frame to be processed in the Tx done callback. +*/ +if (fx->early_frame.mpdu) +return ; + + hdr_len = (const uint8_t *) body - (const uint8_t *) mpdu; + fx->early_frame.mpdu =l_memdup (mpdu, body_len + hdr_len); + fx->early_frame.body = (const uint8_t *) fx->early_frame.mpdu + hdr_len; + fx->early_frame.body_len = body_len; + fx->early_frame.rssi = rssi; +} + +static bool frame_xchg_match (const void *a,const void *b) +{ +const struct frame_xchg_data *fx = a; +const uint64_t *wdev_id = b; + +return fx->wdev_id == *wdev_id; +} + +/* +* Send an action frame described by @frame. If @retry_interval is +* non-zero and we receive no ACK from @peer to any of the retransmissions +* done in the kernel (at a high rate), retry after @retry_interval +* milliseconds from the time the kernel gave up. If no ACK is received +* after all the retransmissions, call @cb with a non-zero error number. +* Otherwise, if @resp_timeout is non-zero, remain on the same channel +* and report any response frames from the frame's destination address +* that match provided prefixes, to the corresponding callbacks. Do so +* for @resp_timeout milliseconds from the ACK receival or until a frame +* callback returns @true. Call @cb when @resp_timeout runs out and +* no frame callback returned @true, or immediately after the ACK if +* @resp_timeout was 0. @frame is an iovec array terminated by an iovec +* struct with NULL-iov_base. +*/ +void frame_xchg_startv (uint64_t wdev_id,struct iovec *frame,uint32_t freq, +unsigned int retry_interval,unsigned int resp_timeout, +uint32_t group_id,frame_xchg_cb_t cb,void *user_data, +va_list resp_args) +{ +struct frame_xchg_data *fx; +size_t frame_len; +struct iovec *iov; +uint8_t *ptr; +struct mmpdu_header *mpdu; + +for (frame_len =0 , iov = frame; iov->iov_base; iov++) + frame_len += iov->iov_len; + +if (frame_len <sizeof (*mpdu)) { +l_error ("Frame too short" ); +cb (-EMSGSIZE , user_data); +return ; + } + + fx =l_queue_find (frame_xchgs, frame_xchg_match, &wdev_id); + +if (fx) { +/* +* If a frame callback calls us assume it's the end of +* that earlier frame exchange and the start of a new one. +*/ +if (fx->in_frame_cb) +frame_xchg_reset (fx); +else { +l_error ("Frame exchange in progress" ); +cb (-EBUSY , user_data); +return ; + } + }else { + fx = l_new(struct frame_xchg_data, 1); + +if (!frame_xchgs) + frame_xchgs =l_queue_new (); + +l_queue_push_tail (frame_xchgs, fx); + } + + fx->wdev_id = wdev_id; + fx->freq = freq; + fx->retry_interval = retry_interval; + fx->resp_timeout = resp_timeout; + fx->cb = cb; + fx->user_data = user_data; + fx->group_id = group_id; + + fx->tx_mpdu =l_malloc (frame_len); + fx->tx_mpdu_len = frame_len; + ptr = (uint8_t *) fx->tx_mpdu; + +for (iov = frame; iov->iov_base; ptr += iov->iov_len, iov++) +memcpy (ptr, iov->iov_base, iov->iov_len); + +/* +* Subscribe to the response frames now instead of in the ACK +* callback to save ourselves race condition considerations. +*/ +while (1 ) { +struct frame_xchg_prefix *prefix; +struct frame_xchg_watch_data *watch; + + prefix = va_arg(resp_args, struct frame_xchg_prefix *); +if (!prefix) +break ; + + watch = l_new(struct frame_xchg_watch_data, 1); + watch->prefix = prefix; + watch->cb = va_arg(resp_args, void *); +frame_watch_add (wdev_id, group_id,0x00d0 , + prefix->data, prefix->len, + frame_xchg_resp_cb, fx,NULL ); + +if (!fx->rx_watches) + fx->rx_watches =l_queue_new (); + +l_queue_push_tail (fx->rx_watches, watch); + } + + fx->retry_cnt =0 ; +frame_xchg_tx_retry (fx); +} + +void frame_xchg_stop (uint64_t wdev_id) +{ +struct frame_xchg_data *fx = +l_queue_remove_if (frame_xchgs, frame_xchg_match, &wdev_id); + +if (!fx) +return ; + +frame_xchg_reset (fx); +l_free (fx); +} + +static void frame_xchg_mlme_notify (struct l_genl_msg *msg,void *user_data) +{ +uint64_t wdev_id; +struct frame_xchg_data *fx; +uint64_t cookie; +bool ack; +uint8_t cmd =l_genl_msg_get_command (msg); + +switch (cmd) { +case NL80211_CMD_FRAME_TX_STATUS : +if (nl80211_parse_attrs (msg,NL80211_ATTR_WDEV , &wdev_id, +NL80211_ATTR_COOKIE , &cookie, +NL80211_ATTR_ACK , &ack, +NL80211_ATTR_UNSPEC ) <0 ) +return ; + +l_debug ("Received %s" , ack ?"an ACK" :"no ACK" ); + + fx =l_queue_find (frame_xchgs, frame_xchg_match, &wdev_id); +if (!fx) +return ; + +if (fx->have_cookie && cookie == fx->cookie && !fx->tx_acked) +frame_xchg_tx_status (fx, ack); +else if (!fx->have_cookie && !fx->tx_acked) { +/* +* Save the information about the frame's ACK status +* to be processed in frame_xchg_tx_cb if we were +* called before it (happens on brcmfmac). +*/ + fx->tx_acked = ack; + fx->cookie = cookie; + fx->early_status = true; + } + +break ; + } +} +static bool frame_xchg_wdev_match (const void *a,const void *b) {const struct wdev_info *wdev = a;
@@ -525,8 +1068,17 @@ static int frame_xchg_init(void)return -EIO ; } +if (!l_genl_family_register (default_group->nl80211,"mlme" , + frame_xchg_mlme_notify,NULL ,NULL )) { +l_error ("Registering for MLME notification failed" ); +frame_watch_group_destroy (default_group); + default_group =NULL ; +return -EIO ; + } + watch_groups =l_queue_new ();l_queue_push_tail (watch_groups, default_group); + nl80211 = default_group->nl80211;return 0 ; }
@@ -535,9 +1087,13 @@ static void frame_xchg_exit(void) {l_queue_destroy (watch_groups, frame_watch_group_destroy); watch_groups =NULL ; + nl80211 =NULL ;l_queue_destroy (wdevs, l_free); wdevs =NULL ; + +l_queue_destroy (frame_xchgs, frame_xchg_cancel); + frame_xchgs =NULL ; }IWD_MODULE (frame_xchg, frame_xchg_init, frame_xchg_exit);
src/frame-xchg.h +15 -0
@@ -25,11 +25,26 @@ struct mmpdu_header;typedef void (*frame_watch_cb_t )(const struct mmpdu_header *frame,const void *body,size_t body_len,int rssi,void *user_data); +typedef bool (*frame_xchg_resp_cb_t )(const struct mmpdu_header *frame, +const void *body,size_t body_len, +int rssi,void *user_data); +typedef void (*frame_xchg_cb_t )(int err,void *user_data);typedef void (*frame_xchg_destroy_func_t )(void *user_data); +struct frame_xchg_prefix { +const uint8_t *data; +size_t len; +}; +bool frame_watch_add (uint64_t wdev_id,uint32_t group,uint16_t frame_type,const uint8_t *prefix,size_t prefix_len,frame_watch_cb_t handler,void *user_data,frame_xchg_destroy_func_t destroy);bool frame_watch_group_remove (uint64_t wdev_id,uint32_t group);bool frame_watch_wdev_remove (uint64_t wdev_id); + +void frame_xchg_startv (uint64_t wdev_id,struct iovec *frame,uint32_t freq, +unsigned int retry_interval,unsigned int resp_timeout, +uint32_t group_id,frame_xchg_cb_t cb,void *user_data, +va_list resp_args); +void frame_xchg_stop (uint64_t wdev_id);
Signature · 1 line
2.20.1
PATCH 3/6 frame-xchg: Don't use l_genl for additional nl80211 sockets
In reply to Andrew ZaborowskiFor nl80211 sockets other than our main l_genl object use socket io directly, to avoid creating many instances of l_genl. The only reason we use multiple sockets is to work around an nl80211 design quirk that requires closing the socket to unregister management frame watches. Normally there should not be a need to create multiple sockets in a program.
--- src/frame-xchg.c | 270 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 226 insertions(+), 44 deletions(-)
src/frame-xchg.c +226 -44
@@ -26,6 +26,8 @@#include <errno.h> #include <stdarg.h> +#include <sys/socket.h> +#include <linux/genetlink.h> #include <ell/ell.h>
@@ -57,8 +59,10 @@ struct watch_group {uint32_t id;uint64_t wdev_id;uint32_t unicast_watch_id; -struct l_genl *genl; -struct l_genl_family *nl80211; +struct l_io *io; +uint32_t nl_pid; +uint32_t nl_seq; +struct l_queue *write_queue;struct watchlist watches; };
@@ -113,6 +117,7 @@ struct frame_xchg_watch_data {static struct l_queue *frame_xchgs;static struct l_genl_family *nl80211; +static uint32_t nl80211_id;struct frame_prefix_info {uint16_t frame_type;
@@ -226,15 +231,12 @@ static void frame_watch_group_destroy(void *data)struct watch_group *group = data;if (group->unicast_watch_id) -l_genl_remove_unicast_watch (group->genl, +l_genl_remove_unicast_watch (iwd_get_genl (), group->unicast_watch_id); -if (group->genl) -l_genl_unref (group->genl); - -if (group->nl80211) -l_genl_family_free (group->nl80211); - +l_io_destroy (group->io); +l_queue_destroy (group->write_queue, + (l_queue_destroy_func_t ) l_genl_msg_unref);watchlist_destroy (&group->watches);l_free (group); }
@@ -252,40 +254,194 @@ static const struct watchlist_ops frame_watch_ops = { .item_free = frame_watch_free, }; +static bool frame_watch_group_io_write (struct l_io *io,void *user_data) +{ +struct watch_group *group = user_data; +struct l_genl_msg *msg; +const void *msg_data; +size_t msg_size; +ssize_t bytes_written; + + msg =l_queue_pop_head (group->write_queue); +if (!msg) +return false; + + msg_data =l_genl_msg_to_data (msg, nl80211_id,NLM_F_REQUEST , + ++group->nl_seq, group->nl_pid, + &msg_size); + bytes_written =send (l_io_get_fd (group->io), msg_data, msg_size,0 ); +l_genl_msg_unref (msg); + +if (bytes_written <0 ) { +l_error ("Frame watch group socket write error: %s (%i)" , +strerror (errno), errno); + +l_queue_push_head (group->write_queue, msg); +return false; + } + +return !l_queue_isempty (group->write_queue); +} + +static bool frame_watch_group_io_read (struct l_io *io,void *user_data) +{ +struct watch_group *group = user_data; +struct cmsghdr *cmsg; +struct msghdr msg; +struct iovec iov; +unsigned char buf[8192 ]; +unsigned char control[32 ]; +ssize_t bytes_read; +struct nlmsghdr *nlmsg; +size_t nlmsg_len; +uint32_t nlmsg_group =0 ; + +memset (&iov,0 ,sizeof (iov)); + iov.iov_base = buf; + iov.iov_len =sizeof (buf); + +memset (&msg,0 ,sizeof (msg)); + msg.msg_iov = &iov; + msg.msg_iovlen =1 ; + msg.msg_control = control; + msg.msg_controllen =sizeof (control); + + bytes_read =recvmsg (l_io_get_fd (group->io), &msg,0 ); +if (bytes_read <0 ) { +if (errno !=EAGAIN && errno !=EINTR ) { +l_error ("Frame watch group socket read error: %s (%i)" , +strerror (errno), errno); +return false; + } + +return true; + } + + nlmsg_len = bytes_read; + +for (cmsg =CMSG_FIRSTHDR (&msg); cmsg !=NULL ; + cmsg =CMSG_NXTHDR (&msg, cmsg)) { +struct nl_pktinfo pktinfo; + +if (cmsg->cmsg_level !=SOL_NETLINK ) +continue ; + +if (cmsg->cmsg_type !=NETLINK_PKTINFO ) +continue ; + +memcpy (&pktinfo,CMSG_DATA (cmsg),sizeof (pktinfo)); + + nlmsg_group = pktinfo.group; + } + +if (nlmsg_group)/* Ignore multicast */ +return true; + +for (nlmsg = iov.iov_base;NLMSG_OK (nlmsg, nlmsg_len); + nlmsg =NLMSG_NEXT (nlmsg, nlmsg_len)) { +struct l_genl_msg *genl_msg; + +if (nlmsg->nlmsg_type != nl80211_id)/* Ignore other families */ +continue ; + +if (nlmsg->nlmsg_seq)/* Ignore responses */ +continue ; + + genl_msg =l_genl_msg_new_from_data ((void *) nlmsg, + nlmsg->nlmsg_len); +if (!genl_msg) +continue ; + +frame_watch_unicast_notify (genl_msg, group); +l_genl_msg_unref (genl_msg); + } + +return true; +} + +static void frame_watch_group_io_destroy (void *user_data) +{ +struct watch_group *group = user_data; + + group->io =NULL ; + +if (l_queue_remove (watch_groups, group)) { +l_error ("Frame watch group socket closed" ); + +frame_watch_group_destroy (group); + } +} +static struct watch_group *frame_watch_group_new (uint64_t wdev_id,uint32_t id) { struct watch_group *group = l_new(struct watch_group, 1); +struct sockaddr_nl addr; +socklen_t addrlen =sizeof (addr); +int fd =-1 , pktinfo =1 ; group->id = id; group->wdev_id = wdev_id;watchlist_init (&group->watches, &frame_watch_ops); -if (id ==0 ) - group->genl =l_genl_ref (iwd_get_genl ()); -else { - group->genl =l_genl_new (); -if (!group->genl) - goto err; - } - - group->unicast_watch_id =l_genl_add_unicast_watch (group->genl, +if (id ==0 ) { + group->unicast_watch_id =l_genl_add_unicast_watch ( +iwd_get_genl (),NL80211_GENL_NAME , frame_watch_unicast_notify, group,NULL ); -if (!group->unicast_watch_id) { -l_error ("Registering for unicast notification failed" ); - goto err; +if (unlikely (!group->unicast_watch_id)) { +l_error ("Registering for unicast notification failed" ); + goto err; + } + +return group; } - group->nl80211 =l_genl_family_new (group->genl,NL80211_GENL_NAME ); -if (!group->nl80211) { -l_error ("Failed to obtain nl80211" ); +/* +* If we need to add groups using genl sockets other than our main +* genl socket (iwd_get_genl()), use raw io instead of l_genl. This +* way we don't have to query the genl families -- we already know +* nl80211 is present -- and the handling is simpler, and l_genl, +* while it would work, is not meant to have multiple instances +* per program. +*/ + fd =socket (PF_NETLINK , +SOCK_DGRAM |SOCK_CLOEXEC |SOCK_NONBLOCK , +NETLINK_GENERIC ); +if (unlikely (fd <0 )) goto err; - } + +memset (&addr,0 ,sizeof (addr)); + addr.nl_family =AF_NETLINK ; + addr.nl_pid =0 ; + +if (unlikely (bind (fd, (struct sockaddr *) &addr,sizeof (addr)) <0 )) + goto err; + +if (unlikely (getsockname (fd, (struct sockaddr *) &addr, &addrlen) <0 )) + goto err; + + group->nl_pid = addr.nl_pid; + +if (unlikely (setsockopt (fd,SOL_NETLINK ,NETLINK_PKTINFO , + &pktinfo,sizeof (pktinfo)) <0 )) + goto err; + + group->io =l_io_new (fd); +if (unlikely (!group->io)) + goto err; + +l_io_set_close_on_destroy (group->io, true); +l_io_set_read_handler (group->io, frame_watch_group_io_read, group, + frame_watch_group_io_destroy); + group->write_queue =l_queue_new ();return group; err: +if (fd >-1 ) +close (fd); +frame_watch_group_destroy (group);return NULL ; }
@@ -410,8 +566,14 @@ bool frame_watch_add(uint64_t wdev_id, uint32_t group_id, uint16_t frame_type,l_genl_msg_append_attr (msg,NL80211_ATTR_FRAME_MATCH , prefix_len, prefix); -l_genl_family_send (group->nl80211, msg, frame_watch_register_cb, -L_UINT_TO_PTR (frame_type),NULL ); +if (group->id ==0 ) +l_genl_family_send (nl80211, msg, frame_watch_register_cb, +L_UINT_TO_PTR (frame_type),NULL ); +else { +l_queue_push_tail (group->write_queue, msg); +l_io_set_write_handler (group->io, frame_watch_group_io_write, + group,NULL ); + }return true; }
@@ -1054,46 +1216,66 @@ static void frame_xchg_config_notify(struct l_genl_msg *msg, void *user_data)static int frame_xchg_init (void ) { -struct watch_group *default_group =frame_watch_group_new (0 ,0 ); +struct watch_group *default_group; +const struct l_genl_family_info *info; -if (!default_group) + nl80211 =l_genl_family_new (iwd_get_genl (),NL80211_GENL_NAME ); +if (!nl80211) { +l_error ("Failed to obtain nl80211" );return -EIO ; + } -if (!l_genl_family_register (default_group->nl80211,"config" , - frame_xchg_config_notify, + info =l_genl_family_get_info (nl80211); + nl80211_id =l_genl_family_info_get_id (info); + + default_group =frame_watch_group_new (0 ,0 ); +if (!default_group) + goto error; + +if (!l_genl_family_register (nl80211,"config" , frame_xchg_config_notify,NULL ,NULL )) {l_error ("Registering for config notifications failed" ); -frame_watch_group_destroy (default_group); - default_group =NULL ; -return -EIO ; + goto error; } -if (!l_genl_family_register (default_group->nl80211,"mlme" , - frame_xchg_mlme_notify,NULL ,NULL )) { +if (!l_genl_family_register (nl80211,"mlme" , frame_xchg_mlme_notify, +NULL ,NULL )) {l_error ("Registering for MLME notification failed" ); -frame_watch_group_destroy (default_group); - default_group =NULL ; -return -EIO ; + goto error; } watch_groups =l_queue_new ();l_queue_push_tail (watch_groups, default_group); - nl80211 = default_group->nl80211;return 0 ; + +error: +if (nl80211) { +l_genl_family_free (nl80211); + nl80211 =NULL ; + } + +if (default_group) +frame_watch_group_destroy (default_group); + +return -EIO ; }static void frame_xchg_exit (void ) { -l_queue_destroy (watch_groups, frame_watch_group_destroy); +struct l_queue *groups = watch_groups; + +l_queue_destroy (frame_xchgs, frame_xchg_cancel); + frame_xchgs =NULL ; + watch_groups =NULL ; +l_queue_destroy (groups, frame_watch_group_destroy); + +l_genl_family_free (nl80211); nl80211 =NULL ;l_queue_destroy (wdevs, l_free); wdevs =NULL ; - -l_queue_destroy (frame_xchgs, frame_xchg_cancel); - frame_xchgs =NULL ; }IWD_MODULE (frame_xchg, frame_xchg_init, frame_xchg_exit);
Signature · 1 line
2.20.1
PATCH 4/6 ap: React to NL80211_CMD_STOP_AP events
In reply to Andrew ZaborowskiThese events will tell use when our AP gets stopped without our request, for example due to suspend/resume.
--- src/ap.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)
src/ap.c +29 -0
@@ -57,6 +57,7 @@ struct ap_state {struct l_uintset *rates;uint8_t pmk[32 ];uint32_t start_stop_cmd_id; +uint32_t mlme_watch;uint8_t gtk[CRYPTO_MAX_GTK_LEN ];uint8_t gtk_index;
@@ -1393,6 +1394,26 @@ static struct l_genl_msg *ap_build_cmd_start_ap(struct ap_state *ap)return cmd; } +static void ap_mlme_notify (struct l_genl_msg *msg,void *user_data) +{ +struct ap_state *ap = user_data; +uint32_t ifindex; + +if (nl80211_parse_attrs (msg,NL80211_ATTR_IFINDEX , &ifindex, +NL80211_ATTR_UNSPEC ) <0 || + ifindex !=netdev_get_ifindex (ap->netdev)) +return ; + +switch (l_genl_msg_get_command (msg)) { +case NL80211_CMD_STOP_AP : +if (ap->start_stop_cmd_id) +break ; + +ap_reset (ap); +break ; + } +} +static int ap_start (struct ap_state *ap,const char *ssid,const char *psk,struct l_dbus_message *message) {
@@ -1448,6 +1469,11 @@ static int ap_start(struct ap_state *ap, const char *ssid, const char *psk, NULL, 0, ap_deauth_cb, ap, NULL)) goto error; + ap->mlme_watch =l_genl_family_register (ap->nl80211,"mlme" , + ap_mlme_notify, ap,NULL ); +if (!ap->mlme_watch) +l_error ("Registering for MLME notification failed" ); + cmd =ap_build_cmd_start_ap (ap);if (!cmd) goto error;
@@ -1514,6 +1540,9 @@ static int ap_stop(struct ap_state *ap, struct l_dbus_message *message)if (ap->start_stop_cmd_id)l_genl_family_cancel (ap->nl80211, ap->start_stop_cmd_id); +if (ap->mlme_watch) +l_genl_family_unregister (ap->nl80211, ap->mlme_watch); + ap->start_stop_cmd_id =l_genl_family_send (ap->nl80211, cmd, ap_stop_cb, ap,NULL );if (!ap->start_stop_cmd_id) {
Signature · 1 line
2.20.1
PATCH 5/6 test: Update paths in list-devices
In reply to Andrew Zaborowski--- test/list-devices | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
test/list-devices +2 -1
@@ -21,7 +21,8 @@ for path in objects: node = node.children[subpath] node.interfaces.update(objects[path]) -for path, phy in tree.children.items(): +root = tree.children['/net'].children['/net/connman'].children['/net/connman/iwd'] +for path, phy in root.children.items(): if 'net.connman.iwd.Adapter' not in phy.interfaces: continue
Signature · 1 line
2.20.1
PATCH 6/6 test: Handle missing agent calls in simple-agent
In reply to Andrew ZaborowskiHandle RequestPrivateKeyPassphrase and RequestUserNameAndPassword
--- test/simple-agent | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
test/simple-agent +35 -0
@@ -35,6 +35,41 @@ class Agent(dbus.service.Object): print("returning (%s)" % (passphrase)) return passphrase + @dbus.service.method("net.connman.iwd.Agent", + in_signature='o', + out_signature='s') + def RequestPrivateKeyPassphrase(self, path): + print("RequestPrivateKeyPassphrase (%s)" % (path)) + + print("Service credentials requested, type cancel to cancel") + passphrase = input('Answer: ') + + if not passphrase or passphrase == 'cancel': + raise Canceled("canceled") + + print("returning (%s)" % (passphrase)) + return passphrase + + @dbus.service.method("net.connman.iwd.Agent", + in_signature='o', + out_signature='ss') + def RequestUserNameAndPassword(self, path): + print("RequestPrivateKeyPassphrase (%s)" % (path)) + + print("Service credentials requested, type cancel to cancel") + user = input('User name: ') + + if not user or user == 'cancel': + raise Canceled("canceled") + + passwd = input('User password: ') + + if not passwd or passwd == 'cancel': + raise Canceled("canceled") + + print("returning (%s, %s)" % (user, passwd)) + return (user, passwd) + @dbus.service.method("net.connman.iwd.Agent", in_signature='s', out_signature='') def Cancel(self, reason):
Signature · 1 line
2.20.1
Hi Andrew,
Quoted 12 lines · Andrew Zaborowski
Try to better deduplicate the frame watches. Until now we'd check if we'd already registered a given frame body prefix with the kernel, or a matching more general prefix (shorter). Now also try to check if we have already have a watch with the same callback pointer and
user_datavalue, and:* an identical or shorter (more general) prefix, in that case ignore the new watch completely.
* a longer (more specific) prefix, in that case forget the existing watch.
Quoted 9 lines
The use case for this is when we have a single callback for multiple watches and multiple frame types, and inside that callback we're looking at the frame body again and matching it to frame types. In that case we don't want that function to be called multiple times for one frame event.
--- src/frame-xchg.c | 70 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 64 insertions(+), 6 deletions(-)
All applied, thanks.
Regards,
-Denis