WPS UPnP: Throttle WLANEvent notifications to 5 per second
[libeap.git] / src / wps / wps_upnp.c
index 01c2dee..d2b8315 100644 (file)
 #define MAX_SUBSCRIPTIONS 4    /* how many subscribing clients we handle */
 #define MAX_ADDR_PER_SUBSCRIPTION 8
 
+/* Maximum number of Probe Request events per second */
+#define MAX_EVENTS_PER_SEC 5
 
 /* Write the current date/time per RFC */
 void format_date(struct wpabuf *buf)
@@ -474,12 +476,38 @@ static void upnp_wps_device_send_event(struct upnp_wps_device_sm *sm)
                "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                "<e:propertyset xmlns:e=\"urn:schemas-upnp-org:event-1-0\">\n";
        const char *format_tail = "</e:propertyset>\n";
+       struct os_time now;
 
        if (dl_list_empty(&sm->subscriptions)) {
                /* optimize */
                return;
        }
 
+       if (os_get_time(&now) == 0) {
+               if (now.sec != sm->last_event_sec) {
+                       sm->last_event_sec = now.sec;
+                       sm->num_events_in_sec = 1;
+               } else {
+                       sm->num_events_in_sec++;
+                       /*
+                        * In theory, this should apply to all WLANEvent
+                        * notifications, but EAP messages are of much higher
+                        * priority and Probe Request notifications should not
+                        * be allowed to drop EAP messages, so only throttle
+                        * Probe Request notifications.
+                        */
+                       if (sm->num_events_in_sec > MAX_EVENTS_PER_SEC &&
+                           sm->wlanevent_type ==
+                           UPNP_WPS_WLANEVENT_TYPE_PROBE) {
+                               wpa_printf(MSG_DEBUG, "WPS UPnP: Throttle "
+                                          "event notifications (%u seen "
+                                          "during one second)",
+                                          sm->num_events_in_sec);
+                               return;
+                       }
+               }
+       }
+
        /* Determine buffer size needed first */
        buf_size += os_strlen(format_head);
        buf_size += 50 + 2 * os_strlen("WLANEvent");
@@ -499,7 +527,8 @@ static void upnp_wps_device_send_event(struct upnp_wps_device_sm *sm)
 
        dl_list_for_each_safe(s, tmp, &sm->subscriptions, struct subscription,
                              list) {
-               if (event_add(s, buf)) {
+               if (event_add(s, buf, sm->wlanevent_type ==
+                             UPNP_WPS_WLANEVENT_TYPE_PROBE) == 1) {
                        wpa_printf(MSG_INFO, "WPS UPnP: Dropping "
                                   "subscriber %p due to event backlog", s);
                        dl_list_del(&s->list);
@@ -608,6 +637,7 @@ static int subscription_first_event(struct subscription *s)
                "<e:propertyset xmlns:e=\"urn:schemas-upnp-org:event-1-0\">\n";
        const char *tail = "</e:propertyset>\n";
        char txt[10];
+       int ret;
 
        if (s->sm->wlanevent == NULL) {
                /*
@@ -639,7 +669,7 @@ static int subscription_first_event(struct subscription *s)
        }
        buf = wpabuf_alloc(500 + os_strlen(wlan_event));
        if (buf == NULL)
-               return 1;
+               return -1;
 
        wpabuf_put_str(buf, head);
        wpabuf_put_property(buf, "STAStatus", "1");
@@ -649,9 +679,10 @@ static int subscription_first_event(struct subscription *s)
                wpabuf_put_property(buf, "WLANEvent", wlan_event);
        wpabuf_put_str(buf, tail);
 
-       if (event_add(s, buf)) {
+       ret = event_add(s, buf, 0);
+       if (ret) {
                wpabuf_free(buf);
-               return 1;
+               return ret;
        }
        wpabuf_free(buf);
 
@@ -796,6 +827,7 @@ int upnp_wps_device_send_wlan_event(struct upnp_wps_device_sm *sm,
 
        os_free(sm->wlanevent);
        sm->wlanevent = val;
+       sm->wlanevent_type = ev_type;
        upnp_wps_device_send_event(sm);
 
        ret = 0;