]> git.sur5r.net Git - ngadmin/blob - cli/com_vlan.c
1e2b573dd60e368fa5fa1c0ee03a8dfdc9a49ab0
[ngadmin] / cli / com_vlan.c
1
2 #include "commands.h"
3
4
5
6 static char vlan_char (int t)
7 {
8         switch (t) {
9         
10         case VLAN_TAGGED:
11                 return 'T';
12         
13         case VLAN_UNTAGGED:
14                 return 'U';
15         
16         case VLAN_NO:
17                 return ' ';
18         
19         default:
20                 return '?';
21         }
22 }
23
24
25 int do_vlan_8021q_del (int argc, const char **argv, struct ngadmin *nga)
26 {
27         const struct swi_attr *sa;
28         unsigned short vlan;
29         int i;
30         
31         
32         if (argc != 1) {
33                 printf("usage: vlan 8021q del <vlan>\n");
34                 return 1;
35         }
36         
37         sa = ngadmin_getCurrentSwitch(nga);
38         if (sa == NULL) {
39                 printf("must be logged\n");
40                 return 1;
41         }
42         
43         vlan=strtoul(argv[0], NULL, 0);
44         if (vlan < 1 || vlan > VLAN_MAX) {
45                 printf("vlan out of range\n");
46                 return 1;
47         }
48         
49         i = ngadmin_VLANDestroy(nga, vlan);
50         printErrCode(i);
51         
52         
53         return 0;
54 }
55
56
57 int do_vlan_8021q_set (int argc, const char **argv, struct ngadmin *nga)
58 {
59         unsigned char *ports = NULL, p, def = VLAN_UNSPEC;
60         const struct swi_attr *sa;
61         unsigned short vlan;
62         int i, k = 0, ret = 0;
63         
64         
65         if (argc == 0) {
66                 printf("usage: vlan 802.1q set <vlan> [all unspec|no|untagged|tagged] [<port1> unspec|no|untagged|tagged ...]\n");
67                 ret = 1;
68                 goto end;
69         }
70         
71         sa = ngadmin_getCurrentSwitch(nga);
72         if (sa == NULL) {
73                 printf("must be logged\n");
74                 ret = 1;
75                 goto end;
76         }
77         
78         /* read vlan */
79         vlan = strtoul(argv[k++], NULL, 0);
80         
81         if (vlan < 1 || vlan > VLAN_MAX) {
82                 printf("vlan out of range\n");
83                 ret = 1;
84                 goto end;
85         }
86         
87         /* read defaults */
88         if (k < argc - 1 && strcasecmp(argv[k], "all") == 0) {
89                 k++;
90                 if (strcasecmp(argv[k], "tagged") == 0) {
91                         def = VLAN_TAGGED;
92                 } else if (strcasecmp(argv[k], "untagged") == 0) {
93                         def = VLAN_UNTAGGED;
94                 } else if (strcasecmp(argv[k], "no") == 0) {
95                         def = VLAN_NO;
96                 } else if (strcasecmp(argv[k], "unspec") == 0) {
97                         def = VLAN_UNSPEC;
98                 } else {
99                         printf("incorrect type\n");
100                         ret = 1;
101                         goto end;
102                 }
103                 k++;
104         }
105         
106         ports = malloc(sa->ports * sizeof(unsigned char));
107         
108         /* apply defaults */
109         memset(ports, def, sa->ports);
110         
111         /* apply port specifics */
112         while (k < argc - 1) {
113                 p = strtoul(argv[k++], NULL, 0) - 1;
114                 if (p >= sa->ports) {
115                         printf("port out of range\n");
116                         ret = 1;
117                         goto end;
118                 }
119                 if (strcasecmp(argv[k], "tagged") ==0) {
120                         ports[p] = VLAN_TAGGED;
121                 } else if (strcasecmp(argv[k], "untagged") == 0) {
122                         ports[p] = VLAN_UNTAGGED;
123                 } else if (strcasecmp(argv[k], "no") == 0) {
124                         ports[p] = VLAN_NO;
125                 } else if (strcasecmp(argv[k], "unspec") == 0) {
126                         ports[p] = VLAN_UNSPEC;
127                 } else {
128                         printf("incorrect type\n");
129                         ret = 1;
130                         goto end;
131                 }
132                 k++;
133         }
134         
135         /* set conf */
136         i = ngadmin_setVLANDotConf(nga, vlan, ports);
137         printErrCode(i);
138         
139 end:
140         free(ports);
141         
142         return ret;
143 }
144
145
146 int do_vlan_8021q_show (int argc, const char **argv, struct ngadmin *nga)
147 {
148         unsigned short vl = 0, *vlans = NULL;
149         unsigned char *ports = NULL;
150         const struct swi_attr *sa;
151         int i, j, n = 16, ret = 0;
152         
153         
154         sa = ngadmin_getCurrentSwitch(nga);
155         if (sa == NULL) {
156                 printf("must be logged\n");
157                 ret = 1;
158                 goto end;
159         }
160         
161         if (argc > 0)
162                 vl = strtoul(argv[0], NULL, 0);
163         
164         ports = malloc(sa->ports * n * sizeof(unsigned char));
165         
166         if (vl == 0) {
167                 /* request all VLANs config */
168                 vlans = malloc(n * sizeof(unsigned short));
169                 ports = malloc(sa->ports * n * sizeof(unsigned char));
170                 i = ngadmin_getVLANDotAllConf(nga, vlans, ports, &n);
171         } else {
172                 /* request single VLAN config */
173                 ports = malloc(sa->ports * sizeof(unsigned char));
174                 i = ngadmin_getVLANDotConf(nga, vl, ports);
175         }
176         
177         if (i != ERR_OK) {
178                 printErrCode(i);
179                 ret = 1;
180                 goto end;
181         }
182         
183         printf("Ports configuration: \n");
184         printf("VLAN\t");
185         for (i = 1; i <= sa->ports; i++)
186                 printf("%i\t", i);
187         putchar('\n');
188         
189         if (vl == 0) {
190                 /* show all VLANs */
191                 for (i = 0; i < n; i++) {
192                         printf("%u\t", vlans[i]);
193                         for (j = 0; j < sa->ports; j++)
194                                 printf("%c\t", vlan_char(ports[i * sa->ports + j]));
195                         putchar('\n');
196                 }
197         } else {
198                 /* show single VLAN config */
199                 printf("%u\t", vl);
200                 for (j = 0; j < sa->ports; j++)
201                         printf("%c\t", vlan_char(ports[j]));
202                 putchar('\n');
203         }
204         
205 end:
206         free(vlans);
207         free(ports);
208         
209         return ret;
210 }
211
212
213 int do_vlan_mode_set (int argc, const char **argv, struct ngadmin *nga)
214 {
215         int mode, i;
216         
217         
218         if (argc == 0) {
219                 printf(
220                 "usage: vlan mode set <mode>\n"
221                 "1 - basic port based\n"
222                 "2 - advanced port based\n"
223                 "3 - basic 802.1Q\n"
224                 "4 - advanced 802.1Q\n"
225                 );
226                 return 0;
227         }
228         
229         if (ngadmin_getCurrentSwitch(nga) == NULL) {
230                 printf("must be logged\n");
231                 return 1;
232         }
233         
234         mode = strtoul(argv[0], NULL, 0);
235         if (mode < 1 || mode > 4) {
236                 printf("mode out of range\n");
237                 return 1;
238         }
239         
240         i = ngadmin_setVLANType(nga, mode);
241         printErrCode(i);
242         
243         
244         return 0;
245 }
246
247
248 int do_vlan_mode_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
249 {
250         int i, t, ret = 0;
251         
252         
253         if (argc > 0) {
254                 printf("this command takes no argument\n");
255                 ret = 1;
256                 goto end;
257         }
258         
259         if (ngadmin_getCurrentSwitch(nga) == NULL) {
260                 printf("must be logged\n");
261                 ret = 1;
262                 goto end;
263         }
264         
265         i = ngadmin_getVLANType(nga, &t);
266         if (i != ERR_OK) {
267                 printErrCode(i);
268                 ret = 1;
269                 goto end;
270         }
271         
272         printf("VLAN type: ");
273         switch (t) {
274         
275         case VLAN_DISABLED:
276                 printf("disabled\n");
277                 break;
278         
279         case VLAN_PORT_BASIC:
280                 printf("port basic\n");
281                 break;
282         
283         case VLAN_PORT_ADV:
284                 printf("port advanced\n");
285                 break;
286         
287         case VLAN_DOT_BASIC:
288                 printf("802.1Q basic\n");
289                 break;
290         
291         case VLAN_DOT_ADV:
292                 printf("802.1Q advanced\n");
293                 break;
294         
295         default:
296                 printf("unknown (%i)\n", t);
297         }
298         
299 end:
300         
301         return ret;
302 }
303
304
305 int do_vlan_pvid_set (int argc, const char **argv, struct ngadmin *nga)
306 {
307         const struct swi_attr *sa;
308         unsigned char port;
309         unsigned short vlan;
310         int i;
311         
312         
313         if (argc != 2) {
314                 printf("usage: vlan pvid set <port> <vlan>\n");
315                 return 1;
316         }
317         
318         sa = ngadmin_getCurrentSwitch(nga);
319         if (sa == NULL) {
320                 printf("must be logged\n");
321                 return 1;
322         }
323         
324         port = strtoul(argv[0], NULL, 0);
325         vlan = strtoul(argv[1], NULL, 0);
326         
327         if (port < 1 || port > sa->ports) {
328                 printf("port out of range\n");
329                 return 1;
330         }
331         
332         if (vlan < 1 || vlan > VLAN_MAX) {
333                 printf("vlan out of range\n");
334                 return 1;
335         }
336         
337         i = ngadmin_setPVID(nga, port, vlan);
338         printErrCode(i);
339         
340         
341         return 0;
342 }
343
344
345 int do_vlan_pvid_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
346 {
347         unsigned short *ports = NULL;
348         const struct swi_attr *sa;
349         int i, ret = 0;
350         
351         
352         if (argc > 0) {
353                 printf("this command takes no argument\n");
354                 ret = 1;
355                 goto end;
356         }
357         
358         sa = ngadmin_getCurrentSwitch(nga);
359         if (sa == NULL) {
360                 printf("must be logged\n");
361                 ret = 1;
362                 goto end;
363         }
364         
365         ports = malloc(sa->ports * sizeof(unsigned short));
366         i = ngadmin_getAllPVID(nga, ports);
367         if (i != ERR_OK) {
368                 printErrCode(i);
369                 ret = 1;
370                 goto end;
371         }
372         
373         printf("Port\t");
374         for (i = 1; i <= sa->ports; i++)
375                 printf("%i\t", i);
376         putchar('\n');
377         
378         printf("VLAN\t");
379         for (i = 0; i < sa->ports; i++)
380                 printf("%u\t", ports[i]);
381         putchar('\n');
382         
383 end:
384         free(ports);
385         
386         return ret;
387 }
388
389