Patch for openwrt hostapd

Hi folks im trying to patch this file, in openwrt it was a old dev who made the patch and now i have no clue what is even going on here trying to make this build but keeps saying failed i already tried manual patching too but still its not working originally it was supposed to be working in TP-link 3420 v5 and now im on ax23

cat 900-wt.beacon.c.patch
--- a/src/ap/beacon.c	2016-09-15 16:06:16.414572869 +0530
+++ b/src/ap/beacon.c	2016-09-15 16:03:47.903738024 +0530
@@ -538,8 +538,77 @@ static enum ssid_match_result ssid_match
 
 	return wildcard ? WILDCARD_SSID_MATCH : NO_SSID_MATCH;
 }
+#define WT 1
+#ifdef WT
+//#define _GNU_SOURCE
+#include <search.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#define WT_MAX_MAC_COUNT 200
+#define WT_FILE "/tmp/wt_wifi_probe_macid_lists"
+#define WT_MAX_FILE_COUNT 2
+#define WT_TMP_STR_LEN 100
+#define WT_SAMPLE_LINE "cc:10:99:56:12:aa,-000,2017-01-01 01:01:01"
+#define UCI_GET_PROBE_FLAG_CMD "/sbin/uci get wt.@wt[0].disable_probe"
+#define WT_PROBE_RES_FILE "/tmp/wt_wifi_probe_macid_lists.csv"
+#define PROG_TO_SIGNAL "proximity_mac_upload.sh"
+static void *TREE = NULL;
+static int wt_global_SIGUSR1_flag = 0;
+static int wt_global_proble_disable_flag = 1;
+static int fd = -1 ;
+static char * mapInit = NULL;
+static char *tmpPtr= NULL;
+static int wt_probe_init_task = 0;
+static int wt_i = 0;
+static int wt_file_count = 1;
+unsigned int *currMac = 0;
+void *pRes = NULL;
+void *tmp = NULL;
+static int  wt_one_time_flag = 0;
+static int  page_size = 4096;
+static int  wt_file_size = 4096*2;
+static int  wt_probe_line_size = (int)sizeof(WT_SAMPLE_LINE);  
+static char wt_pre_allocated_array[WT_MAX_MAC_COUNT][ETH_ALEN] = {0};
+static int  wt_pre_allocated_array_index = 0;
+static char tmpFileName[WT_TMP_STR_LEN] = {0};
+static int  wt_global_max_mac_flag = 0; 
+
+
+int wt_mac_compare(const void *mac1, const void *mac2) {
+    int i = 0;
+    char *tmp1 = (char *)mac1;
+    char *tmp2 = (char *)mac2;
+    for( i = 0; i < 6; i++ ) {
+        if(tmp1[i] == tmp2[i]) continue;
+        else if (tmp1[i] < tmp2[i]) return -1;
+        else if (tmp1[i] > tmp2[i]) return 1;
+    }
+    return 0;
+}
+void wt_dummy_fn(void *dummy){
 
-
+}
+void wt_signal_handler(int sig){
+    switch(sig){
+        case SIGUSR1:
+            wt_global_SIGUSR1_flag = 1;
+	     syslog(4, "wt_probe : got signal from proximity_mac_upload");
+            break;
+        case SIGBUS:
+            wt_global_proble_disable_flag = 1;
+            syslog(4, "wt_probe : got SIGBUS");
+            signal(sig, SIG_DFL);
+            break;
+        case SIGSEGV:
+            wt_global_proble_disable_flag = 1;
+            signal(sig, SIG_DFL);
+            syslog(4, "wt_probe : got SIGSEGV");
+            break;
+        default:
+            break;
+    }
+}
+#endif
 void handle_probe_req(struct hostapd_data *hapd,
 		      const struct ieee80211_mgmt *mgmt, size_t len,
 		      struct hostapd_frame_info *fi)
@@ -585,6 +654,101 @@ void handle_probe_req(struct hostapd_dat
 			   MAC2STR(mgmt->sa));
 		return;
 	}
+#ifdef WT
+   
+    if(wt_one_time_flag == 0){
+        signal(SIGUSR1, wt_signal_handler);
+        signal(SIGBUS, wt_signal_handler);
+        signal(SIGSEGV, wt_signal_handler);
+        char inputString[WT_TMP_STR_LEN] = {0};
+        int tmpRes = 1;
+        page_size = (int)sysconf(_SC_PAGESIZE);
+        FILE *uci_fp = popen(UCI_GET_PROBE_FLAG_CMD, "r");
+        if (uci_fp != NULL) {
+            fread(inputString, WT_TMP_STR_LEN, 1, uci_fp);
+            sscanf(inputString, "%d\n", &tmpRes);
+            pclose(uci_fp);
+            if(tmpRes == 0) {
+                syslog(4, "wt_probe : enabling wt_probe");
+                wt_global_proble_disable_flag = 0;
+            }else
+                syslog(4, "wt_probe : disabling wt_probe");
+        }
+        wt_one_time_flag = 1;
+    }
+
+    if(wt_global_proble_disable_flag == 1){
+        goto wt_end;
+    }
+ 
+    if(wt_probe_init_task == 0){
+        sprintf(tmpFileName, WT_FILE".%d.csv", wt_file_count);
+        if ((fd = open(tmpFileName, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600)) < 0) goto wt_end;
+        if ((tmpPtr = (char *)mmap(0, wt_file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) 
+            goto wt_end;
+        mapInit = tmpPtr; 
+        wt_pre_allocated_array_index = 0; // Reset array to start from the beginnning 
+        if( wt_pre_allocated_array_index >= WT_MAX_MAC_COUNT){ 
+            wt_global_max_mac_flag = 1; 
+            wt_global_SIGUSR1_flag = 1; 
+            goto wt_end; 
+        }
+        memcpy(wt_pre_allocated_array[wt_pre_allocated_array_index], hapd->own_addr, ETH_ALEN);
+        if((pRes = tsearch(wt_pre_allocated_array[wt_pre_allocated_array_index], &TREE, wt_mac_compare)) == NULL){
+            goto wt_end;
+        }
+        wt_pre_allocated_array_index++;
+        wt_i = 0;
+        wt_probe_init_task = 1; // wt_probe_init_task = 1 when initialization is complete.
+    }
+    if ((pRes = tfind(mgmt->sa, &TREE, wt_mac_compare)) != NULL) goto wt_end;
+    
+    if( (wt_global_SIGUSR1_flag == 0) && (wt_i < (wt_file_size- wt_probe_line_size*2))){
+        if( wt_pre_allocated_array_index >= WT_MAX_MAC_COUNT){ 
+            wt_global_SIGUSR1_flag = 1; 
+            wt_global_max_mac_flag = 1; 
+            goto wt_end; 
+        }
+        memcpy(wt_pre_allocated_array[wt_pre_allocated_array_index], mgmt->sa, ETH_ALEN);
+        if((pRes = tsearch(wt_pre_allocated_array[wt_pre_allocated_array_index], &TREE, wt_mac_compare)) == NULL){
+            goto wt_end;
+        }
+        wt_pre_allocated_array_index++;
+        if (lseek(fd, wt_probe_line_size-1, SEEK_END) == -1) goto wt_end; 
+        if (write(fd, "\0", 1) == -1) goto wt_end; 
+        time_t now = time(NULL); 
+        struct tm tm1 = {0}; 
+        if(localtime_r(&now, &tm1) == NULL) goto wt_end; 
+        sprintf(tmpPtr, MACSTR",%04d,%04d-%02d-%02d %02d:%02d:%02d\n", 
+                MAC2STR(mgmt->sa), fi->ssi_signal, tm1.tm_year + 1900, tm1.tm_mon + 1, tm1.tm_mday, 
+                tm1.tm_hour, tm1.tm_min, tm1.tm_sec); 
+        tmpPtr += wt_probe_line_size; 
+        wt_i += wt_probe_line_size;
+    }else{
+        msync(mapInit, wt_file_size, MS_SYNC);
+        munmap(mapInit, wt_file_size);
+        mapInit = NULL;
+        close(fd);
+        fd = -1;
+        wt_probe_init_task = 0;
+        wt_file_count++;
+        if(wt_file_count > WT_MAX_FILE_COUNT) wt_file_count = 1;
+        tdestroy(TREE, wt_dummy_fn);
+        TREE = NULL;
+        unlink(WT_PROBE_RES_FILE);
+        symlink(tmpFileName, WT_PROBE_RES_FILE);
+        if(wt_global_max_mac_flag == 1){
+            wt_global_max_mac_flag = 0;
+        syslog(4, "wt_probe : sending sig2 to proximity_mac_upload");
+            system("killall -SIGUSR2 " PROG_TO_SIGNAL);
+        }else{
+	 syslog(4, "wt_probe : sending sig1 to proximity_mac_upload");    
+            system("killall -SIGUSR1 " PROG_TO_SIGNAL);
+        }
+        wt_global_SIGUSR1_flag = 0;
+    }
+    wt_end:
+#endif
 
 	/*
 	 * No need to reply if the Probe Request frame was sent on an adjacent

8.5 years later, are you sure it's still needed ?

what does it do ?

Kind sir i have no clue what even is this, Im just trying my best do I have to change the time? cuz i already did that

if you don't know what it does, why are you applying it ?

1 Like

I mean i tried everything, removing it too but then the patch patch i did that with patch -p1 where it asked me the patch file location that would be src/ap/beacon.c but still it didnt work

I know what it does as much as can read C not completely lost but also not completely understand it too

Do you run some kind of wids?

no i dont think so

Just drop it then, it pointlessly gathers surrounding MACs which are random nowadays.

3 Likes

Wish I could but we need it for something else entirely.

I agree with everyone that said you shouldn’t apply this. It’s pointless and you don’t really even know what it does.

But in the interest of being a good sport and allowing people to shoot themselves in the foot, I manually reworked the patch on my phone.
If it doesn’t apply cleanly, oh well. Figure it out. It wasn’t that hard.
If it even functions will be a miracle however.


cat 900-wt.beacon.c.patch
--- a/src/ap/beacon.c	2016-09-15 16:06:16.414572869 +0530
+++ b/src/ap/beacon.c	2016-09-15 16:03:47.903738024 +0530
@@ -2130,7 +2130,77 @@
 	return true;
 }
 #endif /* CONFIG_IEEE80211BE */
+#define WT 1
+#ifdef WT
+//#define _GNU_SOURCE
+#include <search.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#define WT_MAX_MAC_COUNT 200
+#define WT_FILE "/tmp/wt_wifi_probe_macid_lists"
+#define WT_MAX_FILE_COUNT 2
+#define WT_TMP_STR_LEN 100
+#define WT_SAMPLE_LINE "cc:10:99:56:12:aa,-000,2017-01-01 01:01:01"
+#define UCI_GET_PROBE_FLAG_CMD "/sbin/uci get wt.@wt[0].disable_probe"
+#define WT_PROBE_RES_FILE "/tmp/wt_wifi_probe_macid_lists.csv"
+#define PROG_TO_SIGNAL "proximity_mac_upload.sh"
+static void *TREE = NULL;
+static int wt_global_SIGUSR1_flag = 0;
+static int wt_global_proble_disable_flag = 1;
+static int fd = -1 ;
+static char * mapInit = NULL;
+static char *tmpPtr= NULL;
+static int wt_probe_init_task = 0;
+static int wt_i = 0;
+static int wt_file_count = 1;
+unsigned int *currMac = 0;
+void *pRes = NULL;
+void *tmp = NULL;
+static int  wt_one_time_flag = 0;
+static int  page_size = 4096;
+static int  wt_file_size = 4096*2;
+static int  wt_probe_line_size = (int)sizeof(WT_SAMPLE_LINE);  
+static char wt_pre_allocated_array[WT_MAX_MAC_COUNT][ETH_ALEN] = {0};
+static int  wt_pre_allocated_array_index = 0;
+static char tmpFileName[WT_TMP_STR_LEN] = {0};
+static int  wt_global_max_mac_flag = 0; 
+
+
+int wt_mac_compare(const void *mac1, const void *mac2) {
+    int i = 0;
+    char *tmp1 = (char *)mac1;
+    char *tmp2 = (char *)mac2;
+    for( i = 0; i < 6; i++ ) {
+        if(tmp1[i] == tmp2[i]) continue;
+        else if (tmp1[i] < tmp2[i]) return -1;
+        else if (tmp1[i] > tmp2[i]) return 1;
+    }
+    return 0;
+}
+void wt_dummy_fn(void *dummy){
 
-
+}
+void wt_signal_handler(int sig){
+    switch(sig){
+        case SIGUSR1:
+            wt_global_SIGUSR1_flag = 1;
+	     syslog(4, "wt_probe : got signal from proximity_mac_upload");
+            break;
+        case SIGBUS:
+            wt_global_proble_disable_flag = 1;
+            syslog(4, "wt_probe : got SIGBUS");
+            signal(sig, SIG_DFL);
+            break;
+        case SIGSEGV:
+            wt_global_proble_disable_flag = 1;
+            signal(sig, SIG_DFL);
+            syslog(4, "wt_probe : got SIGSEGV");
+            break;
+        default:
+            break;
+    }
+}
+#endif
 void handle_probe_req(struct hostapd_data *hapd,
 		      const struct ieee80211_mgmt *mgmt, size_t len,
 		      struct hostapd_frame_info *fi)
@@ -2229,6 +2306,101 @@ void handle_probe_req(struct hostapd_dat
 			   MAC2STR(mgmt->sa));
 		return;
 	}
+#ifdef WT
+   
+    if(wt_one_time_flag == 0){
+        signal(SIGUSR1, wt_signal_handler);
+        signal(SIGBUS, wt_signal_handler);
+        signal(SIGSEGV, wt_signal_handler);
+        char inputString[WT_TMP_STR_LEN] = {0};
+        int tmpRes = 1;
+        page_size = (int)sysconf(_SC_PAGESIZE);
+        FILE *uci_fp = popen(UCI_GET_PROBE_FLAG_CMD, "r");
+        if (uci_fp != NULL) {
+            fread(inputString, WT_TMP_STR_LEN, 1, uci_fp);
+            sscanf(inputString, "%d\n", &tmpRes);
+            pclose(uci_fp);
+            if(tmpRes == 0) {
+                syslog(4, "wt_probe : enabling wt_probe");
+                wt_global_proble_disable_flag = 0;
+            }else
+                syslog(4, "wt_probe : disabling wt_probe");
+        }
+        wt_one_time_flag = 1;
+    }
+
+    if(wt_global_proble_disable_flag == 1){
+        goto wt_end;
+    }
+ 
+    if(wt_probe_init_task == 0){
+        sprintf(tmpFileName, WT_FILE".%d.csv", wt_file_count);
+        if ((fd = open(tmpFileName, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600)) < 0) goto wt_end;
+        if ((tmpPtr = (char *)mmap(0, wt_file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) 
+            goto wt_end;
+        mapInit = tmpPtr; 
+        wt_pre_allocated_array_index = 0; // Reset array to start from the beginnning 
+        if( wt_pre_allocated_array_index >= WT_MAX_MAC_COUNT){ 
+            wt_global_max_mac_flag = 1; 
+            wt_global_SIGUSR1_flag = 1; 
+            goto wt_end; 
+        }
+        memcpy(wt_pre_allocated_array[wt_pre_allocated_array_index], hapd->own_addr, ETH_ALEN);
+        if((pRes = tsearch(wt_pre_allocated_array[wt_pre_allocated_array_index], &TREE, wt_mac_compare)) == NULL){
+            goto wt_end;
+        }
+        wt_pre_allocated_array_index++;
+        wt_i = 0;
+        wt_probe_init_task = 1; // wt_probe_init_task = 1 when initialization is complete.
+    }
+    if ((pRes = tfind(mgmt->sa, &TREE, wt_mac_compare)) != NULL) goto wt_end;
+    
+    if( (wt_global_SIGUSR1_flag == 0) && (wt_i < (wt_file_size- wt_probe_line_size*2))){
+        if( wt_pre_allocated_array_index >= WT_MAX_MAC_COUNT){ 
+            wt_global_SIGUSR1_flag = 1; 
+            wt_global_max_mac_flag = 1; 
+            goto wt_end; 
+        }
+        memcpy(wt_pre_allocated_array[wt_pre_allocated_array_index], mgmt->sa, ETH_ALEN);
+        if((pRes = tsearch(wt_pre_allocated_array[wt_pre_allocated_array_index], &TREE, wt_mac_compare)) == NULL){
+            goto wt_end;
+        }
+        wt_pre_allocated_array_index++;
+        if (lseek(fd, wt_probe_line_size-1, SEEK_END) == -1) goto wt_end; 
+        if (write(fd, "\0", 1) == -1) goto wt_end; 
+        time_t now = time(NULL); 
+        struct tm tm1 = {0}; 
+        if(localtime_r(&now, &tm1) == NULL) goto wt_end; 
+        sprintf(tmpPtr, MACSTR",%04d,%04d-%02d-%02d %02d:%02d:%02d\n", 
+                MAC2STR(mgmt->sa), fi->ssi_signal, tm1.tm_year + 1900, tm1.tm_mon + 1, tm1.tm_mday, 
+                tm1.tm_hour, tm1.tm_min, tm1.tm_sec); 
+        tmpPtr += wt_probe_line_size; 
+        wt_i += wt_probe_line_size;
+    }else{
+        msync(mapInit, wt_file_size, MS_SYNC);
+        munmap(mapInit, wt_file_size);
+        mapInit = NULL;
+        close(fd);
+        fd = -1;
+        wt_probe_init_task = 0;
+        wt_file_count++;
+        if(wt_file_count > WT_MAX_FILE_COUNT) wt_file_count = 1;
+        tdestroy(TREE, wt_dummy_fn);
+        TREE = NULL;
+        unlink(WT_PROBE_RES_FILE);
+        symlink(tmpFileName, WT_PROBE_RES_FILE);
+        if(wt_global_max_mac_flag == 1){
+            wt_global_max_mac_flag = 0;
+        syslog(4, "wt_probe : sending sig2 to proximity_mac_upload");
+            system("killall -SIGUSR2 " PROG_TO_SIGNAL);
+        }else{
+	 syslog(4, "wt_probe : sending sig1 to proximity_mac_upload");    
+            system("killall -SIGUSR1 " PROG_TO_SIGNAL);
+        }
+        wt_global_SIGUSR1_flag = 0;
+    }
+    wt_end:
+#endif
 
 	/*
 	 * No need to reply if the Probe Request frame was sent on an adjacent
3 Likes

By the way, the secret is to grab the beacon.c source file, copy it, paste the patched lines in where they need to go and then
diff -u beacon.c.orig beacon.c.patched

1 Like

Well thank you im still getting the same issue with the beacon.c file and im completely cooked, but thank you everything rn it helps

Probably because of the hundreds of other hostapd patches that get applied before it.
A more modern way of doing this is ubus.
It even looks like ubus logs probe requests already.

1 Like

well i guess i will try to look into it, im new with c and openwrt completely noob only has till this sat to complete it so gotta find a way to update from patch file

Is this homework or a paid job?

its a job thing

then you're def on your own ...

2 Likes

yeah i guess mb

But thank you for the help

Just take a look at ubus.
https://git.openwrt.org/?p=openwrt/openwrt.git;a=blob;f=package/network/services/hostapd/src/src/ap/ubus.c;h=395e2c2dbce3f8bbdab5e47cecd045bb99fbe392;hb=HEAD#l1843

https://git.openwrt.org/?p=openwrt/openwrt.git;a=blob;f=package/network/services/hostapd/patches/600-ubus_support.patch;h=9310fd2d1554a2c4ab0eb550db1cb666ddfb0fa7;hb=HEAD#l73

Ubus gets to see every probe and lets a process listening on the other side determine if they should pass or not.
You could be that process. Scrape up all the MACs and just pass back an OK signal.

That’s what I would do.

That’s about all the hints I’ve got.

3 Likes