swconfig: swlib.c: Fix another memleak
[lede.git] / package / network / config / swconfig / src / swlib.c
1 /*
2  * swlib.c: Switch configuration API (user space part)
3  *
4  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * version 2.1 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but 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
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <inttypes.h>
20 #include <errno.h>
21 #include <stdint.h>
22 #include <getopt.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <linux/switch.h>
26 #include "swlib.h"
27 #include <netlink/netlink.h>
28 #include <netlink/genl/genl.h>
29 #include <netlink/genl/family.h>
30
31 //#define DEBUG 1
32 #ifdef DEBUG
33 #define DPRINTF(fmt, ...) fprintf(stderr, "%s(%d): " fmt, __func__, __LINE__, ##__VA_ARGS__)
34 #else
35 #define DPRINTF(fmt, ...) do {} while (0)
36 #endif
37
38 static struct nl_sock *handle;
39 static struct nl_cache *cache;
40 static struct genl_family *family;
41 static struct nlattr *tb[SWITCH_ATTR_MAX + 1];
42 static int refcount = 0;
43
44 static struct nla_policy port_policy[SWITCH_ATTR_MAX] = {
45         [SWITCH_PORT_ID] = { .type = NLA_U32 },
46         [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
47 };
48
49 static struct nla_policy portmap_policy[SWITCH_PORTMAP_MAX] = {
50         [SWITCH_PORTMAP_SEGMENT] = { .type = NLA_STRING },
51         [SWITCH_PORTMAP_VIRT] = { .type = NLA_U32 },
52 };
53
54 static inline void *
55 swlib_alloc(size_t size)
56 {
57         void *ptr;
58
59         ptr = malloc(size);
60         if (!ptr)
61                 goto done;
62         memset(ptr, 0, size);
63
64 done:
65         return ptr;
66 }
67
68 static int
69 wait_handler(struct nl_msg *msg, void *arg)
70 {
71         int *finished = arg;
72
73         *finished = 1;
74         return NL_STOP;
75 }
76
77 /* helper function for performing netlink requests */
78 static int
79 swlib_call(int cmd, int (*call)(struct nl_msg *, void *),
80                 int (*data)(struct nl_msg *, void *), void *arg)
81 {
82         struct nl_msg *msg;
83         struct nl_cb *cb = NULL;
84         int finished;
85         int flags = 0;
86         int err;
87
88         msg = nlmsg_alloc();
89         if (!msg) {
90                 fprintf(stderr, "Out of memory!\n");
91                 exit(1);
92         }
93
94         if (!data)
95                 flags |= NLM_F_DUMP;
96
97         genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, genl_family_get_id(family), 0, flags, cmd, 0);
98         if (data) {
99                 if (data(msg, arg) < 0)
100                         goto nla_put_failure;
101         }
102
103         cb = nl_cb_alloc(NL_CB_CUSTOM);
104         if (!cb) {
105                 fprintf(stderr, "nl_cb_alloc failed.\n");
106                 exit(1);
107         }
108
109         err = nl_send_auto_complete(handle, msg);
110         if (err < 0) {
111                 fprintf(stderr, "nl_send_auto_complete failed: %d\n", err);
112                 goto out;
113         }
114
115         finished = 0;
116
117         if (call)
118                 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, call, arg);
119
120         if (data)
121                 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, wait_handler, &finished);
122         else
123                 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, wait_handler, &finished);
124
125         err = nl_recvmsgs(handle, cb);
126         if (err < 0) {
127                 goto out;
128         }
129
130         if (!finished)
131                 err = nl_wait_for_ack(handle);
132
133 out:
134         if (cb)
135                 nl_cb_put(cb);
136 nla_put_failure:
137         nlmsg_free(msg);
138         return err;
139 }
140
141 static int
142 send_attr(struct nl_msg *msg, void *arg)
143 {
144         struct switch_val *val = arg;
145         struct switch_attr *attr = val->attr;
146
147         NLA_PUT_U32(msg, SWITCH_ATTR_ID, attr->dev->id);
148         NLA_PUT_U32(msg, SWITCH_ATTR_OP_ID, attr->id);
149         switch(attr->atype) {
150         case SWLIB_ATTR_GROUP_PORT:
151                 NLA_PUT_U32(msg, SWITCH_ATTR_OP_PORT, val->port_vlan);
152                 break;
153         case SWLIB_ATTR_GROUP_VLAN:
154                 NLA_PUT_U32(msg, SWITCH_ATTR_OP_VLAN, val->port_vlan);
155                 break;
156         default:
157                 break;
158         }
159
160         return 0;
161
162 nla_put_failure:
163         return -1;
164 }
165
166 static int
167 store_port_val(struct nl_msg *msg, struct nlattr *nla, struct switch_val *val)
168 {
169         struct nlattr *p;
170         int ports = val->attr->dev->ports;
171         int err = 0;
172         int remaining;
173
174         if (!val->value.ports)
175                 val->value.ports = malloc(sizeof(struct switch_port) * ports);
176
177         nla_for_each_nested(p, nla, remaining) {
178                 struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
179                 struct switch_port *port;
180
181                 if (val->len >= ports)
182                         break;
183
184                 err = nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, p, port_policy);
185                 if (err < 0)
186                         goto out;
187
188                 if (!tb[SWITCH_PORT_ID])
189                         continue;
190
191                 port = &val->value.ports[val->len];
192                 port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
193                 port->flags = 0;
194                 if (tb[SWITCH_PORT_FLAG_TAGGED])
195                         port->flags |= SWLIB_PORT_FLAG_TAGGED;
196
197                 val->len++;
198         }
199
200 out:
201         return err;
202 }
203
204 static int
205 store_val(struct nl_msg *msg, void *arg)
206 {
207         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
208         struct switch_val *val = arg;
209
210         if (!val)
211                 goto error;
212
213         if (nla_parse(tb, SWITCH_ATTR_MAX - 1, genlmsg_attrdata(gnlh, 0),
214                         genlmsg_attrlen(gnlh, 0), NULL) < 0) {
215                 goto error;
216         }
217
218         if (tb[SWITCH_ATTR_OP_VALUE_INT])
219                 val->value.i = nla_get_u32(tb[SWITCH_ATTR_OP_VALUE_INT]);
220         else if (tb[SWITCH_ATTR_OP_VALUE_STR])
221                 val->value.s = strdup(nla_get_string(tb[SWITCH_ATTR_OP_VALUE_STR]));
222         else if (tb[SWITCH_ATTR_OP_VALUE_PORTS])
223                 val->err = store_port_val(msg, tb[SWITCH_ATTR_OP_VALUE_PORTS], val);
224
225         val->err = 0;
226         return 0;
227
228 error:
229         return NL_SKIP;
230 }
231
232 int
233 swlib_get_attr(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
234 {
235         int cmd;
236         int err;
237
238         switch(attr->atype) {
239         case SWLIB_ATTR_GROUP_GLOBAL:
240                 cmd = SWITCH_CMD_GET_GLOBAL;
241                 break;
242         case SWLIB_ATTR_GROUP_PORT:
243                 cmd = SWITCH_CMD_GET_PORT;
244                 break;
245         case SWLIB_ATTR_GROUP_VLAN:
246                 cmd = SWITCH_CMD_GET_VLAN;
247                 break;
248         default:
249                 return -EINVAL;
250         }
251
252         memset(&val->value, 0, sizeof(val->value));
253         val->len = 0;
254         val->attr = attr;
255         val->err = -EINVAL;
256         err = swlib_call(cmd, store_val, send_attr, val);
257         if (!err)
258                 err = val->err;
259
260         return err;
261 }
262
263 static int
264 send_attr_ports(struct nl_msg *msg, struct switch_val *val)
265 {
266         struct nlattr *n;
267         int i;
268
269         /* TODO implement multipart? */
270         if (val->len == 0)
271                 goto done;
272         n = nla_nest_start(msg, SWITCH_ATTR_OP_VALUE_PORTS);
273         if (!n)
274                 goto nla_put_failure;
275         for (i = 0; i < val->len; i++) {
276                 struct switch_port *port = &val->value.ports[i];
277                 struct nlattr *np;
278
279                 np = nla_nest_start(msg, SWITCH_ATTR_PORT);
280                 if (!np)
281                         goto nla_put_failure;
282
283                 NLA_PUT_U32(msg, SWITCH_PORT_ID, port->id);
284                 if (port->flags & SWLIB_PORT_FLAG_TAGGED)
285                         NLA_PUT_FLAG(msg, SWITCH_PORT_FLAG_TAGGED);
286
287                 nla_nest_end(msg, np);
288         }
289         nla_nest_end(msg, n);
290 done:
291         return 0;
292
293 nla_put_failure:
294         return -1;
295 }
296
297 static int
298 send_attr_val(struct nl_msg *msg, void *arg)
299 {
300         struct switch_val *val = arg;
301         struct switch_attr *attr = val->attr;
302
303         if (send_attr(msg, arg))
304                 goto nla_put_failure;
305
306         switch(attr->type) {
307         case SWITCH_TYPE_NOVAL:
308                 break;
309         case SWITCH_TYPE_INT:
310                 NLA_PUT_U32(msg, SWITCH_ATTR_OP_VALUE_INT, val->value.i);
311                 break;
312         case SWITCH_TYPE_STRING:
313                 if (!val->value.s)
314                         goto nla_put_failure;
315                 NLA_PUT_STRING(msg, SWITCH_ATTR_OP_VALUE_STR, val->value.s);
316                 break;
317         case SWITCH_TYPE_PORTS:
318                 if (send_attr_ports(msg, val) < 0)
319                         goto nla_put_failure;
320                 break;
321         default:
322                 goto nla_put_failure;
323         }
324         return 0;
325
326 nla_put_failure:
327         return -1;
328 }
329
330 int
331 swlib_set_attr(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val)
332 {
333         int cmd;
334
335         switch(attr->atype) {
336         case SWLIB_ATTR_GROUP_GLOBAL:
337                 cmd = SWITCH_CMD_SET_GLOBAL;
338                 break;
339         case SWLIB_ATTR_GROUP_PORT:
340                 cmd = SWITCH_CMD_SET_PORT;
341                 break;
342         case SWLIB_ATTR_GROUP_VLAN:
343                 cmd = SWITCH_CMD_SET_VLAN;
344                 break;
345         default:
346                 return -EINVAL;
347         }
348
349         val->attr = attr;
350         return swlib_call(cmd, NULL, send_attr_val, val);
351 }
352
353 int swlib_set_attr_string(struct switch_dev *dev, struct switch_attr *a, int port_vlan, const char *str)
354 {
355         struct switch_port *ports;
356         struct switch_val val;
357         char *ptr;
358
359         memset(&val, 0, sizeof(val));
360         val.port_vlan = port_vlan;
361         switch(a->type) {
362         case SWITCH_TYPE_INT:
363                 val.value.i = atoi(str);
364                 break;
365         case SWITCH_TYPE_STRING:
366                 val.value.s = (char *)str;
367                 break;
368         case SWITCH_TYPE_PORTS:
369                 ports = alloca(sizeof(struct switch_port) * dev->ports);
370                 memset(ports, 0, sizeof(struct switch_port) * dev->ports);
371                 val.len = 0;
372                 ptr = (char *)str;
373                 while(ptr && *ptr)
374                 {
375                         while(*ptr && isspace(*ptr))
376                                 ptr++;
377
378                         if (!*ptr)
379                                 break;
380
381                         if (!isdigit(*ptr))
382                                 return -1;
383
384                         if (val.len >= dev->ports)
385                                 return -1;
386
387                         ports[val.len].flags = 0;
388                         ports[val.len].id = strtoul(ptr, &ptr, 10);
389                         while(*ptr && !isspace(*ptr)) {
390                                 if (*ptr == 't')
391                                         ports[val.len].flags |= SWLIB_PORT_FLAG_TAGGED;
392                                 else
393                                         return -1;
394
395                                 ptr++;
396                         }
397                         if (*ptr)
398                                 ptr++;
399                         val.len++;
400                 }
401                 val.value.ports = ports;
402                 break;
403         case SWITCH_TYPE_NOVAL:
404                 if (str && !strcmp(str, "0"))
405                         return 0;
406
407                 break;
408         default:
409                 return -1;
410         }
411         return swlib_set_attr(dev, a, &val);
412 }
413
414
415 struct attrlist_arg {
416         int id;
417         int atype;
418         struct switch_dev *dev;
419         struct switch_attr *prev;
420         struct switch_attr **head;
421 };
422
423 static int
424 add_id(struct nl_msg *msg, void *arg)
425 {
426         struct attrlist_arg *l = arg;
427
428         NLA_PUT_U32(msg, SWITCH_ATTR_ID, l->id);
429
430         return 0;
431 nla_put_failure:
432         return -1;
433 }
434
435 static int
436 add_attr(struct nl_msg *msg, void *ptr)
437 {
438         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
439         struct attrlist_arg *arg = ptr;
440         struct switch_attr *new;
441
442         if (nla_parse(tb, SWITCH_ATTR_MAX - 1, genlmsg_attrdata(gnlh, 0),
443                         genlmsg_attrlen(gnlh, 0), NULL) < 0)
444                 goto done;
445
446         new = swlib_alloc(sizeof(struct switch_attr));
447         if (!new)
448                 goto done;
449
450         new->dev = arg->dev;
451         new->atype = arg->atype;
452         if (arg->prev) {
453                 arg->prev->next = new;
454         } else {
455                 arg->prev = *arg->head;
456         }
457         *arg->head = new;
458         arg->head = &new->next;
459
460         if (tb[SWITCH_ATTR_OP_ID])
461                 new->id = nla_get_u32(tb[SWITCH_ATTR_OP_ID]);
462         if (tb[SWITCH_ATTR_OP_TYPE])
463                 new->type = nla_get_u32(tb[SWITCH_ATTR_OP_TYPE]);
464         if (tb[SWITCH_ATTR_OP_NAME])
465                 new->name = strdup(nla_get_string(tb[SWITCH_ATTR_OP_NAME]));
466         if (tb[SWITCH_ATTR_OP_DESCRIPTION])
467                 new->description = strdup(nla_get_string(tb[SWITCH_ATTR_OP_DESCRIPTION]));
468
469 done:
470         return NL_SKIP;
471 }
472
473 int
474 swlib_scan(struct switch_dev *dev)
475 {
476         struct attrlist_arg arg;
477
478         if (dev->ops || dev->port_ops || dev->vlan_ops)
479                 return 0;
480
481         arg.atype = SWLIB_ATTR_GROUP_GLOBAL;
482         arg.dev = dev;
483         arg.id = dev->id;
484         arg.prev = NULL;
485         arg.head = &dev->ops;
486         swlib_call(SWITCH_CMD_LIST_GLOBAL, add_attr, add_id, &arg);
487
488         arg.atype = SWLIB_ATTR_GROUP_PORT;
489         arg.prev = NULL;
490         arg.head = &dev->port_ops;
491         swlib_call(SWITCH_CMD_LIST_PORT, add_attr, add_id, &arg);
492
493         arg.atype = SWLIB_ATTR_GROUP_VLAN;
494         arg.prev = NULL;
495         arg.head = &dev->vlan_ops;
496         swlib_call(SWITCH_CMD_LIST_VLAN, add_attr, add_id, &arg);
497
498         return 0;
499 }
500
501 struct switch_attr *swlib_lookup_attr(struct switch_dev *dev,
502                 enum swlib_attr_group atype, const char *name)
503 {
504         struct switch_attr *head;
505
506         if (!name || !dev)
507                 return NULL;
508
509         switch(atype) {
510         case SWLIB_ATTR_GROUP_GLOBAL:
511                 head = dev->ops;
512                 break;
513         case SWLIB_ATTR_GROUP_PORT:
514                 head = dev->port_ops;
515                 break;
516         case SWLIB_ATTR_GROUP_VLAN:
517                 head = dev->vlan_ops;
518                 break;
519         }
520         while(head) {
521                 if (!strcmp(name, head->name))
522                         return head;
523                 head = head->next;
524         }
525
526         return NULL;
527 }
528
529 static void
530 swlib_priv_free(void)
531 {
532         if (family)
533                 nl_object_put((struct nl_object*)family);
534         if (cache)
535                 nl_cache_free(cache);
536         if (handle)
537                 nl_socket_free(handle);
538         family = NULL;
539         handle = NULL;
540         cache = NULL;
541 }
542
543 static int
544 swlib_priv_init(void)
545 {
546         int ret;
547
548         handle = nl_socket_alloc();
549         if (!handle) {
550                 DPRINTF("Failed to create handle\n");
551                 goto err;
552         }
553
554         if (genl_connect(handle)) {
555                 DPRINTF("Failed to connect to generic netlink\n");
556                 goto err;
557         }
558
559         ret = genl_ctrl_alloc_cache(handle, &cache);
560         if (ret < 0) {
561                 DPRINTF("Failed to allocate netlink cache\n");
562                 goto err;
563         }
564
565         family = genl_ctrl_search_by_name(cache, "switch");
566         if (!family) {
567                 DPRINTF("Switch API not present\n");
568                 goto err;
569         }
570         return 0;
571
572 err:
573         swlib_priv_free();
574         return -EINVAL;
575 }
576
577 struct swlib_scan_arg {
578         const char *name;
579         struct switch_dev *head;
580         struct switch_dev *ptr;
581 };
582
583 static int
584 add_port_map(struct switch_dev *dev, struct nlattr *nla)
585 {
586         struct nlattr *p;
587         int err = 0, idx = 0;
588         int remaining;
589
590         dev->maps = malloc(sizeof(struct switch_portmap) * dev->ports);
591         if (!dev->maps)
592                 return -1;
593         memset(dev->maps, 0, sizeof(struct switch_portmap) * dev->ports);
594
595         nla_for_each_nested(p, nla, remaining) {
596                 struct nlattr *tb[SWITCH_PORTMAP_MAX+1];
597
598                 if (idx >= dev->ports)
599                         continue;
600
601                 err = nla_parse_nested(tb, SWITCH_PORTMAP_MAX, p, portmap_policy);
602                 if (err < 0)
603                         continue;
604
605
606                 if (tb[SWITCH_PORTMAP_SEGMENT] && tb[SWITCH_PORTMAP_VIRT]) {
607                         dev->maps[idx].segment = strdup(nla_get_string(tb[SWITCH_PORTMAP_SEGMENT]));
608                         dev->maps[idx].virt = nla_get_u32(tb[SWITCH_PORTMAP_VIRT]);
609                 }
610                 idx++;
611         }
612
613 out:
614         return err;
615 }
616
617
618 static int
619 add_switch(struct nl_msg *msg, void *arg)
620 {
621         struct swlib_scan_arg *sa = arg;
622         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
623         struct switch_dev *dev;
624         const char *name;
625         const char *alias;
626
627         if (nla_parse(tb, SWITCH_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
628                 goto done;
629
630         if (!tb[SWITCH_ATTR_DEV_NAME])
631                 goto done;
632
633         name = nla_get_string(tb[SWITCH_ATTR_DEV_NAME]);
634         alias = nla_get_string(tb[SWITCH_ATTR_ALIAS]);
635
636         if (sa->name && (strcmp(name, sa->name) != 0) && (strcmp(alias, sa->name) != 0))
637                 goto done;
638
639         dev = swlib_alloc(sizeof(struct switch_dev));
640         if (!dev)
641                 goto done;
642
643         strncpy(dev->dev_name, name, IFNAMSIZ - 1);
644         dev->alias = strdup(alias);
645         if (tb[SWITCH_ATTR_ID])
646                 dev->id = nla_get_u32(tb[SWITCH_ATTR_ID]);
647         if (tb[SWITCH_ATTR_NAME])
648                 dev->name = strdup(nla_get_string(tb[SWITCH_ATTR_NAME]));
649         if (tb[SWITCH_ATTR_PORTS])
650                 dev->ports = nla_get_u32(tb[SWITCH_ATTR_PORTS]);
651         if (tb[SWITCH_ATTR_VLANS])
652                 dev->vlans = nla_get_u32(tb[SWITCH_ATTR_VLANS]);
653         if (tb[SWITCH_ATTR_CPU_PORT])
654                 dev->cpu_port = nla_get_u32(tb[SWITCH_ATTR_CPU_PORT]);
655         if (tb[SWITCH_ATTR_PORTMAP])
656                 add_port_map(dev, tb[SWITCH_ATTR_PORTMAP]);
657
658         if (!sa->head) {
659                 sa->head = dev;
660                 sa->ptr = dev;
661         } else {
662                 sa->ptr->next = dev;
663                 sa->ptr = dev;
664         }
665
666         refcount++;
667 done:
668         return NL_SKIP;
669 }
670
671 static int
672 list_switch(struct nl_msg *msg, void *arg)
673 {
674         struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
675
676         if (nla_parse(tb, SWITCH_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0)
677                 goto done;
678
679         if (!tb[SWITCH_ATTR_DEV_NAME] || !tb[SWITCH_ATTR_NAME])
680                 goto done;
681
682         printf("Found: %s - %s\n", nla_get_string(tb[SWITCH_ATTR_DEV_NAME]),
683                 nla_get_string(tb[SWITCH_ATTR_ALIAS]));
684
685 done:
686         return NL_SKIP;
687 }
688
689 void
690 swlib_list(void)
691 {
692         if (swlib_priv_init() < 0)
693                 return;
694         swlib_call(SWITCH_CMD_GET_SWITCH, list_switch, NULL, NULL);
695         swlib_priv_free();
696 }
697
698 void
699 swlib_print_portmap(struct switch_dev *dev, char *segment)
700 {
701         int i;
702
703         if (segment) {
704                 if (!strcmp(segment, "cpu")) {
705                         printf("%d ", dev->cpu_port);
706                 } else if (!strcmp(segment, "disabled")) {
707                         for (i = 0; i < dev->ports; i++)
708                                 if (!dev->maps[i].segment)
709                                         printf("%d ", i);
710                 } else for (i = 0; i < dev->ports; i++) {
711                         if (dev->maps[i].segment && !strcmp(dev->maps[i].segment, segment))
712                                 printf("%d ", i);
713                 }
714         } else {
715                 printf("%s - %s\n", dev->dev_name, dev->name);
716                 for (i = 0; i < dev->ports; i++)
717                         if (i == dev->cpu_port)
718                                 printf("port%d:\tcpu\n", i);
719                         else if (dev->maps[i].segment)
720                                 printf("port%d:\t%s.%d\n", i, dev->maps[i].segment, dev->maps[i].virt);
721                         else
722                                 printf("port%d:\tdisabled\n", i);
723         }
724 }
725
726 struct switch_dev *
727 swlib_connect(const char *name)
728 {
729         struct swlib_scan_arg arg;
730
731         if (!refcount) {
732                 if (swlib_priv_init() < 0)
733                         return NULL;
734         };
735
736         arg.head = NULL;
737         arg.ptr = NULL;
738         arg.name = name;
739         swlib_call(SWITCH_CMD_GET_SWITCH, add_switch, NULL, &arg);
740
741         if (!refcount)
742                 swlib_priv_free();
743
744         return arg.head;
745 }
746
747 static void
748 swlib_free_attributes(struct switch_attr **head)
749 {
750         struct switch_attr *a = *head;
751         struct switch_attr *next;
752
753         while (a) {
754                 next = a->next;
755                 free(a->name);
756                 free(a->description);
757                 free(a);
758                 a = next;
759         }
760         *head = NULL;
761 }
762
763 static void
764 swlib_free_port_map(struct switch_dev *dev)
765 {
766         int i;
767
768         if (!dev || !dev->maps)
769                 return;
770
771         for (i = 0; i < dev->ports; i++)
772                 free(dev->maps[i].segment);
773         free(dev->maps);
774 }
775
776 void
777 swlib_free(struct switch_dev *dev)
778 {
779         swlib_free_attributes(&dev->ops);
780         swlib_free_attributes(&dev->port_ops);
781         swlib_free_attributes(&dev->vlan_ops);
782         swlib_free_port_map(dev);
783         free(dev->name);
784         free(dev->alias);
785         free(dev);
786
787         if (--refcount == 0)
788                 swlib_priv_free();
789 }
790
791 void
792 swlib_free_all(struct switch_dev *dev)
793 {
794         struct switch_dev *p;
795
796         while (dev) {
797                 p = dev->next;
798                 swlib_free(dev);
799                 dev = p;
800         }
801 }