vlan.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include "netifd.h"
  18. #include "system.h"
  19. struct vlan_device {
  20. struct device dev;
  21. struct device_user dep;
  22. device_state_cb set_state;
  23. int id;
  24. };
  25. static void free_vlan_if(struct device *iface)
  26. {
  27. struct vlan_device *vldev;
  28. vldev = container_of(iface, struct vlan_device, dev);
  29. device_remove_user(&vldev->dep);
  30. device_cleanup(&vldev->dev);
  31. free(vldev);
  32. }
  33. static int vlan_set_device_state(struct device *dev, bool up)
  34. {
  35. struct vlan_device *vldev;
  36. int ret = 0;
  37. vldev = container_of(dev, struct vlan_device, dev);
  38. if (!up) {
  39. vldev->set_state(dev, false);
  40. system_vlan_del(dev);
  41. device_release(&vldev->dep);
  42. return 0;
  43. }
  44. ret = device_claim(&vldev->dep);
  45. if (ret < 0)
  46. return ret;
  47. system_vlan_add(vldev->dep.dev, vldev->id);
  48. ret = vldev->set_state(dev, true);
  49. if (ret)
  50. device_release(&vldev->dep);
  51. return ret;
  52. }
  53. static int vlan_dev_set_name(struct vlan_device *vldev, struct device *dev)
  54. {
  55. char *name;
  56. name = alloca(strlen(dev->ifname) + sizeof(".2147483647\0"));
  57. vldev->dev.hidden = dev->hidden;
  58. sprintf(name, "%s.%d", dev->ifname, vldev->id);
  59. return device_set_ifname(&vldev->dev, name);
  60. }
  61. static void vlan_dev_cb(struct device_user *dep, enum device_event ev)
  62. {
  63. struct vlan_device *vldev;
  64. vldev = container_of(dep, struct vlan_device, dep);
  65. switch(ev) {
  66. case DEV_EVENT_ADD:
  67. device_set_present(&vldev->dev, true);
  68. break;
  69. case DEV_EVENT_REMOVE:
  70. device_set_present(&vldev->dev, false);
  71. break;
  72. case DEV_EVENT_UPDATE_IFNAME:
  73. if (vlan_dev_set_name(vldev, dep->dev) < 0)
  74. free_vlan_if(&vldev->dev);
  75. break;
  76. case DEV_EVENT_TOPO_CHANGE:
  77. /* Propagate topo changes */
  78. device_broadcast_event(&vldev->dev, DEV_EVENT_TOPO_CHANGE);
  79. break;
  80. default:
  81. break;
  82. }
  83. }
  84. static struct device *get_vlan_device(struct device *dev, int id, bool create)
  85. {
  86. static struct device_type vlan_type = {
  87. .name = "VLAN",
  88. .config_params = &device_attr_list,
  89. .free = free_vlan_if,
  90. };
  91. struct vlan_device *vldev;
  92. struct device_user *dep;
  93. /* look for an existing interface before creating a new one */
  94. list_for_each_entry(dep, &dev->users.list, list.list) {
  95. if (dep->cb != vlan_dev_cb)
  96. continue;
  97. vldev = container_of(dep, struct vlan_device, dep);
  98. if (vldev->id != id)
  99. continue;
  100. return &vldev->dev;
  101. }
  102. if (!create)
  103. return NULL;
  104. D(DEVICE, "Create vlan device '%s.%d'\n", dev->ifname, id);
  105. vldev = calloc(1, sizeof(*vldev));
  106. if (!vldev)
  107. return NULL;
  108. vldev->id = id;
  109. if (device_init(&vldev->dev, &vlan_type, NULL) < 0)
  110. goto error;
  111. if (vlan_dev_set_name(vldev, dev) < 0)
  112. goto error;
  113. vldev->dev.default_config = true;
  114. vldev->set_state = vldev->dev.set_state;
  115. vldev->dev.set_state = vlan_set_device_state;
  116. vldev->dep.cb = vlan_dev_cb;
  117. device_add_user(&vldev->dep, dev);
  118. return &vldev->dev;
  119. error:
  120. device_cleanup(&vldev->dev);
  121. free(vldev);
  122. return NULL;
  123. }
  124. static char *split_vlan(char *s)
  125. {
  126. s = strchr(s, '.');
  127. if (!s)
  128. goto out;
  129. *s = 0;
  130. s++;
  131. out:
  132. return s;
  133. }
  134. struct device *get_vlan_device_chain(const char *ifname, bool create)
  135. {
  136. struct device *dev = NULL;
  137. char *buf, *s, *next, *err = NULL;
  138. int id;
  139. buf = strdup(ifname);
  140. if (!buf)
  141. return NULL;
  142. s = split_vlan(buf);
  143. dev = device_get(buf, create);
  144. if (!dev)
  145. goto error;
  146. do {
  147. next = split_vlan(s);
  148. id = strtoul(s, &err, 10);
  149. if (err && *err)
  150. goto error;
  151. dev = get_vlan_device(dev, id, create);
  152. if (!dev)
  153. goto error;
  154. s = next;
  155. if (!s)
  156. goto out;
  157. } while (1);
  158. error:
  159. dev = NULL;
  160. out:
  161. free(buf);
  162. return dev;
  163. }