]> git.sur5r.net Git - ngadmin/blob - cli/src/com_vlan.c
Merge remote-tracking branch 'upstream/master'
[ngadmin] / cli / src / 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                 "0 - disabled\n"
343                 "1 - basic port based\n"
344                 "2 - advanced port based\n"
345                 "3 - basic 802.1Q\n"
346                 "4 - advanced 802.1Q\n"
347                 );
348                 return 0;
349         }
350         
351         if (ngadmin_getCurrentSwitch(nga) == NULL) {
352                 printf("must be logged\n");
353                 return 1;
354         }
355         
356         mode = strtoul(argv[0], NULL, 0);
357         if (mode < VLAN_DISABLED || mode > VLAN_DOT_ADV) {
358                 printf("mode out of range\n");
359                 return 1;
360         }
361         
362         i = ngadmin_setVLANType(nga, mode);
363         printErrCode(i);
364         
365         
366         return 0;
367 }
368
369
370 int do_vlan_mode_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
371 {
372         int i, t, ret = 0;
373         
374         
375         if (argc > 0) {
376                 printf("this command takes no argument\n");
377                 ret = 1;
378                 goto end;
379         }
380         
381         if (ngadmin_getCurrentSwitch(nga) == NULL) {
382                 printf("must be logged\n");
383                 ret = 1;
384                 goto end;
385         }
386         
387         i = ngadmin_getVLANType(nga, &t);
388         if (i != ERR_OK) {
389                 printErrCode(i);
390                 ret = 1;
391                 goto end;
392         }
393         
394         printf("VLAN type: %s\n", safeStr(getVlanTypeStr(t)));
395         
396 end:
397         
398         return ret;
399 }
400
401
402 int do_vlan_pvid_set (int argc, const char **argv, struct ngadmin *nga)
403 {
404         const struct swi_attr *sa;
405         unsigned char port;
406         unsigned short vlan;
407         int i;
408         
409         
410         if (argc != 2) {
411                 printf("usage: vlan pvid set <port> <vlan>\n");
412                 return 1;
413         }
414         
415         sa = ngadmin_getCurrentSwitch(nga);
416         if (sa == NULL) {
417                 printf("must be logged\n");
418                 return 1;
419         }
420         
421         port = strtoul(argv[0], NULL, 0);
422         vlan = strtoul(argv[1], NULL, 0);
423         
424         if (port < 1 || port > sa->ports) {
425                 printf("port out of range\n");
426                 return 1;
427         }
428         
429         if (vlan < VLAN_MIN || vlan > VLAN_DOT_MAX) {
430                 printf("vlan out of range\n");
431                 return 1;
432         }
433         
434         i = ngadmin_setPVID(nga, port, vlan);
435         printErrCode(i);
436         
437         
438         return 0;
439 }
440
441
442 int do_vlan_pvid_show (int argc, const char **argv UNUSED, struct ngadmin *nga)
443 {
444         unsigned short *ports = NULL;
445         const struct swi_attr *sa;
446         int i, ret = 0;
447         
448         
449         if (argc > 0) {
450                 printf("this command takes no argument\n");
451                 ret = 1;
452                 goto end;
453         }
454         
455         sa = ngadmin_getCurrentSwitch(nga);
456         if (sa == NULL) {
457                 printf("must be logged\n");
458                 ret = 1;
459                 goto end;
460         }
461         
462         ports = malloc(sa->ports * sizeof(unsigned short));
463         i = ngadmin_getAllPVID(nga, ports);
464         if (i != ERR_OK) {
465                 printErrCode(i);
466                 ret = 1;
467                 goto end;
468         }
469         
470         printf("Port\t");
471         for (i = 1; i <= sa->ports; i++)
472                 printf("%i\t", i);
473         putchar('\n');
474         
475         printf("VLAN\t");
476         for (i = 0; i < sa->ports; i++)
477                 printf("%u\t", ports[i]);
478         putchar('\n');
479         
480 end:
481         free(ports);
482         
483         return ret;
484 }
485
486