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