]> git.sur5r.net Git - ngadmin/blob - cli/com_vlan.c
Cli: allow VLAN 0 in port based config
[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 < VLAN_MIN || vlan > VLAN_DOT_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_port_set (int argc, const char **argv, struct ngadmin *nga)
58 {
59         unsigned char vlan, port, *ports = NULL;
60         const struct swi_attr *sa;
61         int i, k = 0, ret = 0;
62         
63         
64         if (argc < 2) {
65                 printf("usage: vlan port set [all <vlan>] [<port1> <vlan>] [<port2> <vlan>] [...]\n");
66                 ret = 1;
67                 goto end;
68         }
69         
70         sa = ngadmin_getCurrentSwitch(nga);
71         if (sa == NULL) {
72                 printf("must be logged\n");
73                 ret = 1;
74                 goto end;
75         }
76         
77         ports = malloc(sa->ports * sizeof(unsigned char));
78
79         /* read defaults */
80         vlan = 0;
81         if (strcmp(argv[k], "all") == 0) {
82                 k++;
83                 vlan = strtoul(argv[k++], NULL, 0);
84                 /* VLAN 0 is allowed and means no change */
85                 if (vlan > VLAN_PORT_MAX) {
86                         printf("vlan out of range\n");
87                         ret = 1;
88                         goto end;
89                 }
90         }
91         
92         /* apply defaults */
93         memset(ports, vlan, sa->ports);
94         
95         /* read and apply port specifics */
96         while (k < argc - 1) {
97                 /* read port */
98                 port = strtoul(argv[k++], NULL, 0);
99                 if (port < 1 || port > sa->ports) {
100                         printf("port out of range\n");
101                         ret = 1;
102                         goto end;
103                 }
104                 
105                 /* read vlan */
106                 vlan = strtoul(argv[k++], NULL, 0);
107                 /* VLAN 0 is allowed and means no change */
108                 if (vlan > VLAN_PORT_MAX) {
109                         printf("vlan out of range\n");
110                         ret = 1;
111                         goto end;
112                 }
113                 
114                 ports[port - 1] = vlan;
115         }
116         
117         /* set conf */
118         i = ngadmin_setVLANPortConf(nga, ports);
119         printErrCode(i);
120         
121 end:
122         free(ports);
123         
124         return ret;
125 }
126
127
128 int do_vlan_port_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
129 {
130         unsigned char *ports = NULL;
131         const struct swi_attr *sa;
132         int i, ret = 0;
133         
134         
135         if (argc > 0) {
136                 printf("this command takes no argument\n");
137                 ret = 1;
138                 goto end;
139         }
140         
141         sa = ngadmin_getCurrentSwitch(nga);
142         if (sa == NULL) {
143                 printf("must be logged\n");
144                 ret = 1;
145                 goto end;
146         }
147         
148         ports = malloc(sa->ports * sizeof(unsigned char));
149         
150         /* request all VLANs config */
151         i = ngadmin_getVLANPortConf(nga, ports);
152         
153         if (i != ERR_OK) {
154                 printErrCode(i);
155                 ret = 1;
156                 goto end;
157         }
158         
159         printf("Ports configuration: \n");
160         printf("Port\t");
161         for (i = 1; i <= sa->ports; i++)
162                 printf("%i\t", i);
163         putchar('\n');
164         
165         /* show all VLANs */
166         printf("VLAN\t");
167         for (i = 0; i < sa->ports; i++)
168                 printf("%u\t", ports[i]);
169         putchar('\n');
170         
171 end:
172         free(ports);
173         
174         return ret;
175 }
176
177
178 int do_vlan_8021q_set (int argc, const char **argv, struct ngadmin *nga)
179 {
180         unsigned char *ports = NULL, p, def = VLAN_UNSPEC;
181         const struct swi_attr *sa;
182         unsigned short vlan;
183         int i, k = 0, ret = 0;
184         
185         
186         if (argc == 0) {
187                 printf("usage: vlan 802.1q set <vlan> [all unspec|no|untagged|tagged] [<port1> unspec|no|untagged|tagged ...]\n");
188                 ret = 1;
189                 goto end;
190         }
191         
192         sa = ngadmin_getCurrentSwitch(nga);
193         if (sa == NULL) {
194                 printf("must be logged\n");
195                 ret = 1;
196                 goto end;
197         }
198         
199         /* read vlan */
200         vlan = strtoul(argv[k++], NULL, 0);
201         
202         if (vlan < VLAN_MIN || vlan > VLAN_DOT_MAX) {
203                 printf("vlan out of range\n");
204                 ret = 1;
205                 goto end;
206         }
207         
208         /* read defaults */
209         if (k < argc - 1 && strcasecmp(argv[k], "all") == 0) {
210                 k++;
211                 if (strcasecmp(argv[k], "tagged") == 0) {
212                         def = VLAN_TAGGED;
213                 } else if (strcasecmp(argv[k], "untagged") == 0) {
214                         def = VLAN_UNTAGGED;
215                 } else if (strcasecmp(argv[k], "no") == 0) {
216                         def = VLAN_NO;
217                 } else if (strcasecmp(argv[k], "unspec") == 0) {
218                         def = VLAN_UNSPEC;
219                 } else {
220                         printf("incorrect type\n");
221                         ret = 1;
222                         goto end;
223                 }
224                 k++;
225         }
226         
227         ports = malloc(sa->ports * sizeof(unsigned char));
228         
229         /* apply defaults */
230         memset(ports, def, sa->ports);
231         
232         /* read and apply port specifics */
233         while (k < argc - 1) {
234                 p = strtoul(argv[k++], NULL, 0) - 1;
235                 if (p >= sa->ports) {
236                         printf("port out of range\n");
237                         ret = 1;
238                         goto end;
239                 }
240                 if (strcasecmp(argv[k], "tagged") ==0) {
241                         ports[p] = VLAN_TAGGED;
242                 } else if (strcasecmp(argv[k], "untagged") == 0) {
243                         ports[p] = VLAN_UNTAGGED;
244                 } else if (strcasecmp(argv[k], "no") == 0) {
245                         ports[p] = VLAN_NO;
246                 } else if (strcasecmp(argv[k], "unspec") == 0) {
247                         ports[p] = VLAN_UNSPEC;
248                 } else {
249                         printf("incorrect type\n");
250                         ret = 1;
251                         goto end;
252                 }
253                 k++;
254         }
255         
256         /* set conf */
257         i = ngadmin_setVLANDotConf(nga, vlan, ports);
258         printErrCode(i);
259         
260 end:
261         free(ports);
262         
263         return ret;
264 }
265
266
267 int do_vlan_8021q_show (int argc, const char **argv, struct ngadmin *nga)
268 {
269         unsigned short vl = 0, *vlans = NULL;
270         unsigned char *ports = NULL;
271         const struct swi_attr *sa;
272         int i, j, n = 16, ret = 0;
273         
274         
275         sa = ngadmin_getCurrentSwitch(nga);
276         if (sa == NULL) {
277                 printf("must be logged\n");
278                 ret = 1;
279                 goto end;
280         }
281         
282         if (argc > 0)
283                 vl = strtoul(argv[0], NULL, 0);
284         
285         ports = malloc(sa->ports * n * sizeof(unsigned char));
286         
287         if (vl == 0) {
288                 /* request all VLANs config */
289                 vlans = malloc(n * sizeof(unsigned short));
290                 ports = malloc(sa->ports * n * sizeof(unsigned char));
291                 i = ngadmin_getVLANDotAllConf(nga, vlans, ports, &n);
292         } else {
293                 /* request single VLAN config */
294                 ports = malloc(sa->ports * sizeof(unsigned char));
295                 i = ngadmin_getVLANDotConf(nga, vl, ports);
296         }
297         
298         if (i != ERR_OK) {
299                 printErrCode(i);
300                 ret = 1;
301                 goto end;
302         }
303         
304         printf("Ports configuration: \n");
305         printf("VLAN\t");
306         for (i = 1; i <= sa->ports; i++)
307                 printf("%i\t", i);
308         putchar('\n');
309         
310         if (vl == 0) {
311                 /* show all VLANs */
312                 for (i = 0; i < n; i++) {
313                         printf("%u\t", vlans[i]);
314                         for (j = 0; j < sa->ports; j++)
315                                 printf("%c\t", vlan_char(ports[i * sa->ports + j]));
316                         putchar('\n');
317                 }
318         } else {
319                 /* show single VLAN config */
320                 printf("%u\t", vl);
321                 for (j = 0; j < sa->ports; j++)
322                         printf("%c\t", vlan_char(ports[j]));
323                 putchar('\n');
324         }
325         
326 end:
327         free(vlans);
328         free(ports);
329         
330         return ret;
331 }
332
333
334 int do_vlan_mode_set (int argc, const char **argv, struct ngadmin *nga)
335 {
336         int mode, i;
337         
338         
339         if (argc == 0) {
340                 printf(
341                 "usage: vlan mode set <mode>\n"
342                 "1 - basic port based\n"
343                 "2 - advanced port based\n"
344                 "3 - basic 802.1Q\n"
345                 "4 - advanced 802.1Q\n"
346                 );
347                 return 0;
348         }
349         
350         if (ngadmin_getCurrentSwitch(nga) == NULL) {
351                 printf("must be logged\n");
352                 return 1;
353         }
354         
355         mode = strtoul(argv[0], NULL, 0);
356         if (mode < 1 || mode > 4) {
357                 printf("mode out of range\n");
358                 return 1;
359         }
360         
361         i = ngadmin_setVLANType(nga, mode);
362         printErrCode(i);
363         
364         
365         return 0;
366 }
367
368
369 int do_vlan_mode_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
370 {
371         int i, t, ret = 0;
372         
373         
374         if (argc > 0) {
375                 printf("this command takes no argument\n");
376                 ret = 1;
377                 goto end;
378         }
379         
380         if (ngadmin_getCurrentSwitch(nga) == NULL) {
381                 printf("must be logged\n");
382                 ret = 1;
383                 goto end;
384         }
385         
386         i = ngadmin_getVLANType(nga, &t);
387         if (i != ERR_OK) {
388                 printErrCode(i);
389                 ret = 1;
390                 goto end;
391         }
392         
393         printf("VLAN type: ");
394         switch (t) {
395         
396         case VLAN_DISABLED:
397                 printf("disabled\n");
398                 break;
399         
400         case VLAN_PORT_BASIC:
401                 printf("port basic\n");
402                 break;
403         
404         case VLAN_PORT_ADV:
405                 printf("port advanced\n");
406                 break;
407         
408         case VLAN_DOT_BASIC:
409                 printf("802.1Q basic\n");
410                 break;
411         
412         case VLAN_DOT_ADV:
413                 printf("802.1Q advanced\n");
414                 break;
415         
416         default:
417                 printf("unknown (%i)\n", t);
418         }
419         
420 end:
421         
422         return ret;
423 }
424
425
426 int do_vlan_pvid_set (int argc, const char **argv, struct ngadmin *nga)
427 {
428         const struct swi_attr *sa;
429         unsigned char port;
430         unsigned short vlan;
431         int i;
432         
433         
434         if (argc != 2) {
435                 printf("usage: vlan pvid set <port> <vlan>\n");
436                 return 1;
437         }
438         
439         sa = ngadmin_getCurrentSwitch(nga);
440         if (sa == NULL) {
441                 printf("must be logged\n");
442                 return 1;
443         }
444         
445         port = strtoul(argv[0], NULL, 0);
446         vlan = strtoul(argv[1], NULL, 0);
447         
448         if (port < 1 || port > sa->ports) {
449                 printf("port out of range\n");
450                 return 1;
451         }
452         
453         if (vlan < VLAN_MIN || vlan > VLAN_DOT_MAX) {
454                 printf("vlan out of range\n");
455                 return 1;
456         }
457         
458         i = ngadmin_setPVID(nga, port, vlan);
459         printErrCode(i);
460         
461         
462         return 0;
463 }
464
465
466 int do_vlan_pvid_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
467 {
468         unsigned short *ports = NULL;
469         const struct swi_attr *sa;
470         int i, ret = 0;
471         
472         
473         if (argc > 0) {
474                 printf("this command takes no argument\n");
475                 ret = 1;
476                 goto end;
477         }
478         
479         sa = ngadmin_getCurrentSwitch(nga);
480         if (sa == NULL) {
481                 printf("must be logged\n");
482                 ret = 1;
483                 goto end;
484         }
485         
486         ports = malloc(sa->ports * sizeof(unsigned short));
487         i = ngadmin_getAllPVID(nga, ports);
488         if (i != ERR_OK) {
489                 printErrCode(i);
490                 ret = 1;
491                 goto end;
492         }
493         
494         printf("Port\t");
495         for (i = 1; i <= sa->ports; i++)
496                 printf("%i\t", i);
497         putchar('\n');
498         
499         printf("VLAN\t");
500         for (i = 0; i < sa->ports; i++)
501                 printf("%u\t", ports[i]);
502         putchar('\n');
503         
504 end:
505         free(ports);
506         
507         return ret;
508 }
509
510