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