]> git.decadent.org.uk Git - ap-utils.git/blob - ap-gl/bridge.c
Imported Upstream version 1.5~pre3~a
[ap-utils.git] / ap-gl / bridge.c
1 /*
2  *      bridge.c from Access Point SNMP Utils for Linux
3  *
4  * Copyright (c) 2002 Roman Festchook <roma at polesye dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License Version 2 from
8  * June 1991 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include "ap-utils.h"
25
26 #define IPADDR _("[I] IP: ")
27 #define NETMASK _("[N] Netmask: ")
28 #define GATEWAY _("[G] Gateway: ")
29 #define IP_FILTER _("[F] Filter non-IP traffic: ")
30 #define PR_PORT _("[P] Primary port: ")
31 #define SB_ATTMAC _("Attached station MAC: ")
32 #define DHCP _("[D] DHCP client: ")
33 #define OPER _("[O] Operational mode: ")
34 #define REMOTE_MAC _("[M] Preferred BSSID (remote MAC addr.): ")
35 //#define CF_PORT _("[C] Configuration-enabled port(s): ")
36 #define TRAP_PORT _("[T] Trap-sending port: ")
37 #define FW_BCAST _("[R] Forward broadcast traffic: ")
38 //#define SB_BCAST _("[B] Isolate wireless clients (broadcast traffic): ")
39 #define SB_UCAST _("[U] Isolate wireless clients: ")
40 #define HELP _("INGFPDOMSCTRBU - set; W - write conf; Q - quit to menu")
41
42 extern short ap_type;
43
44
45 void bridging()
46 {
47
48     char sysTrapSwitch[] =
49         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x01, 0x03,
50         0x00
51     };
52
53     char operIPAddress[] =
54         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x02, 0x01,
55         0x00
56     };
57     char operIPMask[] =
58         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x02, 0x02,
59         0x00
60     };
61     char operEthernetAddress[] =
62         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x02, 0x03,
63         0x00
64     };
65     char operGateway[] =
66         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x02, 0x04,
67         0x00
68     };
69     char operDHCP[] =
70         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x02, 0x05,
71         0x00
72     };
73     char PrimaryPort[] =
74         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x02, 0x06,
75         0x00
76     };
77 /*    char ConfigPort[] =
78         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0xE0, 0x3E, 0x01, 0x01, 0x02, 0x07,
79         0x00
80     };
81 */
82
83 char *bridge_modes[6] = {
84     _("Wireless Bridge Point to MultiPoint"),
85     _("Access Point"),
86     _("Access Point client"),
87     _("Wireless Bridge Point to Point"),
88     _("Repeater"),
89     _("unknown")
90 };
91
92     char TrapPort[] =
93         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0xE0, 0x3E, 0x01, 0x01, 0x02, 0x08,
94         0x00
95     };
96
97     char IPFilter[] =
98         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x03, 0x01,
99         0x00
100     };
101
102     char ForwarbBcast[] =
103         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x03, 0x02,
104         0x00
105     };
106     char SendBackBcast[] =
107         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x03, 0x03,
108         0x00
109     };
110     char SendBackUnicast[] =
111         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x03, 0x04,
112         0x00
113     };
114
115     char bridgeOperationalMode[] =
116         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x04, 0x01,
117         0x00
118     };
119     char bridgeRemoteBridgeBSSID[] =
120         { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x04, 0x02,
121         0x00
122     };
123
124     extern WINDOW *main_sub;
125     varbind varbinds[15];
126     struct in_addr ip, mask, gw;
127     unsigned char message[1024], filter, primary_port, dhcp, RemoteBSSID[6],
128         bridge_mode, traps, fw_bcast, sb_ucast;
129     char m_filter = 0, m_bridge_mode = 0, m_primary_port = 0, m_traps = 0,
130         m_dhcp = 0, m_ip = 0, m_mask = 0, m_gw = 0, m_remote_bssid = 0,
131         /*m_config_port = 0, */m_trap_port = 0, m_fw_bcast = 0, /*m_sb_bcast = 0,*/
132         m_sb_ucast = 0;
133     char *pr_ports[2] = {
134         _("Ethernet"),
135         _("Wireless")
136     } /*, *cf_trap_ports[3] = {
137         _("Ethernet"),
138         _("Wireless"),
139         _("Both")
140     }*/;
141
142     int i;
143     unsigned int trap_port = 0;
144
145     /* Determine the advanced MIB subtype of the device from its MAC address. */
146     varbinds[0].type = NULL_VALUE;
147     varbinds[0].oid = operEthernetAddress;
148     varbinds[0].len_oid = sizeof(operEthernetAddress);
149     varbinds[0].len_val = 0;
150
151     print_help(WAIT_RET);
152 /*    if (snmp(varbinds, 1, GET) <= 0) {
153         print_helperr(ERR_RET);
154         goto exit;
155     }
156 */
157     for (i = 1; i < 15; i++) {
158         varbinds[i].type = NULL_VALUE;
159         varbinds[i].len_val = 0;
160         varbinds[i].len_oid = sizeof(sysTrapSwitch);
161     }
162
163     varbinds[1].oid = IPFilter;
164     varbinds[2].oid = PrimaryPort;
165     varbinds[3].oid = operDHCP;
166     varbinds[4].oid = operIPAddress;
167     varbinds[5].oid = operIPMask;
168     varbinds[6].oid = operGateway;
169     varbinds[7].oid = bridgeOperationalMode;
170     varbinds[8].oid = sysTrapSwitch;
171     varbinds[9].oid = bridgeRemoteBridgeBSSID;
172     varbinds[10].oid = ForwarbBcast;
173     varbinds[11].oid = SendBackUnicast;
174     varbinds[12].oid = SendBackBcast;
175
176     if (snmp(varbinds, 13, GET) < 13) {
177         print_helperr(ERR_RET);
178         goto exit;
179     }
180
181     print_top(NULL, _("Bridging"));
182
183     sb_ucast = *(varbinds[11].value);
184 //    sb_ucast = varbinds[11].len_val;
185     sprintf(message, "%s%s", SB_UCAST, (sb_ucast == 2) ? ON : OFF);
186     mvwaddstr(main_sub, 10, 0, message);
187
188
189
190     sprintf(message, "%s%02X%02X%02X%02X%02X%02X", MAC,
191             varbinds[0].value[0] & 0xFF,
192             varbinds[0].value[1] & 0xFF,
193             varbinds[0].value[2] & 0xFF,
194             varbinds[0].value[3] & 0xFF,
195             varbinds[0].value[4] & 0xFF,
196             varbinds[0].value[5] & 0xFF);
197     mvwaddstr(main_sub, 0, 0, message);
198
199     filter = *(varbinds[1].value);
200     sprintf(message, "%s%s", IP_FILTER, (filter == 1) ? ON : OFF);
201     mvwaddstr(main_sub, 3, 0, message);
202
203     primary_port = *(varbinds[2].value);
204         if (primary_port < 1 || primary_port > 2) {
205             primary_port = 1;
206         }
207         sprintf(message, "%s%s", PR_PORT, pr_ports[primary_port - 1]);
208     mvwaddstr(main_sub, 4, 0, message);
209
210     dhcp = *(varbinds[3].value);
211     sprintf(message, "%s%s", DHCP, (dhcp == 1) ? ON : OFF);
212     mvwaddstr(main_sub, 5, 0, message);
213
214     memcpy(&ip.s_addr, varbinds[4].value, 4);
215     sprintf(message, "%s%s", IPADDR, inet_ntoa(ip));
216     mvwaddstr(main_sub, 1, 0, message);
217
218     memcpy(&mask.s_addr, varbinds[5].value, 4);
219     sprintf(message, "%s%s", NETMASK, inet_ntoa(mask));
220     mvwaddstr(main_sub, 1, 24, message);
221
222     memcpy(&gw.s_addr, varbinds[6].value, 4);
223     sprintf(message, "%s%s", GATEWAY, inet_ntoa(gw));
224     mvwaddstr(main_sub, 2, 0, message);
225
226     memcpy(RemoteBSSID, varbinds[9].value, 6);
227
228     if ((bridge_mode = *(varbinds[7].value)) != 2) {
229         sprintf(message, "%s%02X%02X%02X%02X%02X%02X", REMOTE_MAC,
230                 *(RemoteBSSID + 0) & 0xFF, *(RemoteBSSID + 1) & 0xFF,
231                 *(RemoteBSSID + 2) & 0xFF, *(RemoteBSSID + 3) & 0xFF,
232                 *(RemoteBSSID + 4) & 0xFF, *(RemoteBSSID + 5) & 0xFF);
233         mvwaddstr(main_sub, 7, 0, message);
234     }
235     if (bridge_mode > 5)
236         bridge_mode  = 6;
237     sprintf(message, "%s%s", OPER, bridge_modes[bridge_mode - 1]);
238     mvwaddstr(main_sub, 6, 0, message);
239
240     traps = *(varbinds[8].value);
241     sprintf(message, "%s%s", TRAPS, (traps == 1) ? ON : OFF);
242     mvwaddstr(main_sub, 8, 0, message);
243
244     fw_bcast = *(varbinds[10].value);
245     sprintf(message, "%s%s", FW_BCAST, (fw_bcast == 1) ? ON : OFF);
246     mvwaddstr(main_sub, 9, 0, message);
247
248     wrefresh(main_sub);
249     noecho();
250
251     print_help(HELP);
252     while (1) {
253         switch (getch()) {
254         case 'I':
255         case 'i':
256             get_ip(&ip, 1, strlen(IPADDR), HELP);
257             m_ip = 1;
258             continue;
259         case 'N':
260         case 'n':
261             get_mask(&mask, 1, strlen(IPADDR) + 16 + strlen(NETMASK), HELP);
262             m_mask = 1;
263             continue;
264         case 'G':
265         case 'g':
266             get_ip(&gw, 2, strlen(GATEWAY), HELP);
267             m_gw = 1;
268             continue;
269         case 'F':
270         case 'f':
271             filter = on_off(3, strlen(IP_FILTER));
272             clear_main_new(3, 4);
273             print_menusel(3, 0, IP_FILTER, (filter == 1) ? ON : OFF);
274             m_filter = 1;
275             continue;
276         case 'P':
277         case 'p':
278                 primary_port = menu_choose(4, strlen(PR_PORT), pr_ports, 2) + 1;
279                 clear_main_new(4, 5);
280                 print_menusel(4, 0, PR_PORT, pr_ports[primary_port - 1]);
281                 m_primary_port = 1;
282             continue;
283         case 'D':
284         case 'd':
285             dhcp = on_off(5, strlen(DHCP));
286             clear_main_new(5, 6);
287             print_menusel(5, 0, DHCP, (dhcp == 1) ? ON : OFF);
288             m_dhcp = 1;
289             continue;
290         case 'O':
291         case 'o':
292             bridge_mode = menu_choose(6, strlen(OPER), bridge_modes, 5) + 1;
293             clear_main_new(6, 8);
294             print_menusel(6, 0, OPER, bridge_modes[bridge_mode - 1]);
295             if (bridge_mode != 2) {
296                 sprintf(message, "%02X%02X%02X%02X%02X%02X",
297                         *(RemoteBSSID + 0) & 0xFF,
298                         *(RemoteBSSID + 1) & 0xFF,
299                         *(RemoteBSSID + 2) & 0xFF,
300                         *(RemoteBSSID + 3) & 0xFF,
301                         *(RemoteBSSID + 4) & 0xFF,
302                         *(RemoteBSSID + 5) & 0xFF);
303                 print_menusel(7, 0, REMOTE_MAC, message);
304             }
305             m_bridge_mode = 1;
306             continue;
307         case 'M':
308         case 'm':
309             if (bridge_mode == 2)
310                 continue;
311             get_mac(RemoteBSSID, 7, strlen(REMOTE_MAC));
312             m_remote_bssid = 1;
313             continue;
314         case 'S':
315         case 's':
316             traps = on_off(8, strlen(TRAPS));
317             clear_main_new(8, 9);
318             print_menusel(8, 0, TRAPS, (traps == 1) ? ON : OFF);
319             m_traps = 1;
320             continue;
321 /*      case 'C':
322         case 'c':
323                 config_port = menu_choose(9, strlen(CF_PORT), cf_trap_ports, 3);
324                 clear_main_new(9, 10);
325                 print_menusel(9, 0, CF_PORT, cf_trap_ports[config_port]);
326                 m_config_port = 1;
327             continue;
328 */      case 'R':
329         case 'r':
330                 fw_bcast = on_off(9, strlen(FW_BCAST));
331                 clear_main_new(9, 10);
332                 print_menusel(9, 0, FW_BCAST, (fw_bcast == 1) ? ON : OFF);
333                 m_fw_bcast = 1;
334             continue;
335 /*      case 'B':
336         case 'b':
337                 sb_bcast = on_off(12, strlen(SB_BCAST));
338                 clear_main_new(12, 13);
339                 print_menusel(12, 0, SB_BCAST, (sb_bcast == 1) ? ON : OFF);
340                 m_sb_bcast = 1;
341             continue;
342 */      case 'U':
343         case 'u':
344                 sb_ucast = on_off(10, strlen(SB_UCAST));
345                 sb_ucast = (sb_ucast == 2) ? 1 : 2;
346                 clear_main_new(10, 11);
347                 print_menusel(10, 0, SB_UCAST, (sb_ucast == 2) ? ON : OFF);
348                 m_sb_ucast = 1;
349             continue;
350         case 'w':
351         case 'W':
352             i = 0;
353             if (m_filter) {
354                 varbinds[i].oid = IPFilter;
355                 varbinds[i].len_oid = sizeof(IPFilter);
356                 varbinds[i].type = INT_VALUE;
357                 varbinds[i].value = (char *) &filter;
358                 varbinds[i].len_val = 1;
359                 i++;
360             }
361             if (m_primary_port) {
362                 varbinds[i].oid = PrimaryPort;
363                 varbinds[i].len_oid = sizeof(PrimaryPort);
364                 varbinds[i].type = INT_VALUE;
365                 varbinds[i].value = (char *) &primary_port;
366                 varbinds[i].len_val = 1;
367                 i++;
368             }
369             if (m_dhcp) {
370                 varbinds[i].oid = operDHCP;
371                 varbinds[i].len_oid = sizeof(operDHCP);
372                 varbinds[i].type = INT_VALUE;
373                 varbinds[i].value = (char *) &dhcp;
374                 varbinds[i].len_val = 1;
375                 i++;
376             }
377             if (m_ip) {
378                 varbinds[i].oid = operIPAddress;
379                 varbinds[i].len_oid = sizeof(operIPAddress);
380                 ip.s_addr = htonl(ip.s_addr);
381                 ip.s_addr = swap4(ip.s_addr);
382                 varbinds[i].value = (char *) &ip.s_addr;
383                 varbinds[i].len_val = 4;
384                 varbinds[i].type = INT_VALUE;
385                 i++;
386             }
387             if (m_mask) {
388                 varbinds[i].oid = operIPMask;
389                 varbinds[i].len_oid = sizeof(operIPMask);
390                 mask.s_addr = htonl(mask.s_addr);
391                 mask.s_addr = swap4(mask.s_addr);
392                 varbinds[i].value = (char *) &mask.s_addr;
393                 varbinds[i].len_val = 4;
394                 varbinds[i].type = INT_VALUE;
395                 i++;
396             }
397             if (m_gw) {
398                 varbinds[i].oid = operGateway;
399                 gw.s_addr = htonl(gw.s_addr);
400                 gw.s_addr = swap4(gw.s_addr);
401                 varbinds[i].len_oid = sizeof(operGateway);
402                 varbinds[i].value = (char *) &gw.s_addr;
403                 varbinds[i].len_val = 4;
404                 varbinds[i].type = INT_VALUE;
405                 i++;
406             }
407             if (m_traps) {
408                 varbinds[i].oid = sysTrapSwitch;
409                 varbinds[i].len_oid = sizeof(sysTrapSwitch);
410                 varbinds[i].value = &traps;
411                 varbinds[i].len_val = 1;
412                 varbinds[i].type = INT_VALUE;
413                 i++;
414             }
415             if (m_bridge_mode) {
416                 varbinds[i].oid = bridgeOperationalMode;
417                 varbinds[i].len_oid = sizeof(bridgeOperationalMode);
418                 varbinds[i].value = (char *) &bridge_mode;
419                 varbinds[i].len_val = 1;
420                 varbinds[i].type = INT_VALUE;
421                 i++;
422             }
423             if (m_remote_bssid) {
424                 varbinds[i].oid = bridgeRemoteBridgeBSSID;
425                 varbinds[i].len_oid = sizeof(bridgeRemoteBridgeBSSID);
426                 varbinds[i].value = RemoteBSSID;
427                 varbinds[i].len_val = 6;
428                 varbinds[i].type = STRING_VALUE;
429                 i++;
430             }
431 /*          if (m_config_port) {
432                 varbinds[i].oid = ConfigPort;
433                 varbinds[i].len_oid = sizeof(ConfigPort);
434                 varbinds[i].type = INT_VALUE;
435                 config_port +=1;
436                 varbinds[i].value = (char *) &config_port;
437                 varbinds[i].len_val = 1;
438                 i++;
439             }
440 */          if (m_fw_bcast) {
441                 varbinds[i].oid = ForwarbBcast;
442                 varbinds[i].len_oid = sizeof(ForwarbBcast);
443                 varbinds[i].type = INT_VALUE;
444                 varbinds[i].value = (char *) &fw_bcast;
445                 varbinds[i].len_val = 1;
446                 i++;
447             }
448             if (m_sb_ucast) {
449                 varbinds[i].oid = SendBackBcast;
450                 varbinds[i].len_oid = sizeof(SendBackBcast);
451                 varbinds[i].type = INT_VALUE;
452                 varbinds[i].value = (char *) &sb_ucast;
453                 varbinds[i].len_val = 1;
454                 i++;
455                 varbinds[i].oid = SendBackUnicast;
456                 varbinds[i].len_oid = sizeof(SendBackUnicast);
457                 varbinds[i].type = INT_VALUE;
458                 varbinds[i].value = (char *) &sb_ucast;
459                 varbinds[i].len_val = 1;
460                 i++;
461             }
462             if (m_trap_port) {
463                 int len_val;
464
465                 len_val = (trap_port > 0x7fff) ? 3 : (trap_port > 0x7f) ? 2 : 1;
466                 varbinds[i].oid = TrapPort;
467                 varbinds[i].len_oid = sizeof(TrapPort);
468                 varbinds[i].type = STRING_VALUE;
469                 varbinds[i].value = (char *) &trap_port;
470                 varbinds[i].len_val = len_val;
471                 i++;
472             }
473
474             print_help(WAIT_SET);
475             if (snmp(varbinds, i, SET) <= 0)
476                 print_helperr(ERR_SET);
477             else
478                 print_help(DONE_SET);
479             goto exit;
480         case 'Q':
481         case 'q':
482             goto quit;
483         }
484         continue;
485     }
486   exit:
487     getch();
488   quit:
489     print_top(NULL, NULL);
490     clear_main(0);
491 }
492