vlan.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. static struct blob_buf b;
  20. struct vlan_device {
  21. struct device dev;
  22. struct device_user dep;
  23. device_state_cb set_state;
  24. int id;
  25. };
  26. static void free_vlan_if(struct device *iface)
  27. {
  28. struct vlan_device *vldev;
  29. vldev = container_of(iface, struct vlan_device, dev);
  30. device_remove_user(&vldev->dep);
  31. device_cleanup(&vldev->dev);
  32. free(vldev);
  33. }
  34. static int
  35. vlan_hotplug_add(struct device *dev, struct device *member, struct blob_attr *vlan)
  36. {
  37. struct vlan_device *vldev = container_of(dev, struct vlan_device, dev);
  38. void *a;
  39. dev = vldev->dep.dev;
  40. if (!dev || !dev->hotplug_ops)
  41. return UBUS_STATUS_NOT_SUPPORTED;
  42. blob_buf_init(&b, 0);
  43. a = blobmsg_open_array(&b, "vlans");
  44. blobmsg_printf(&b, NULL, "%d", vldev->id);
  45. blobmsg_close_array(&b, a);
  46. return dev->hotplug_ops->add(dev, member, blobmsg_data(b.head));
  47. }
  48. static int
  49. vlan_hotplug_del(struct device *dev, struct device *member)
  50. {
  51. struct vlan_device *vldev = container_of(dev, struct vlan_device, dev);
  52. dev = vldev->dep.dev;
  53. if (!dev || !dev->hotplug_ops)
  54. return UBUS_STATUS_NOT_SUPPORTED;
  55. return dev->hotplug_ops->del(dev, member);
  56. }
  57. static int
  58. vlan_hotplug_prepare(struct device *dev, struct device **bridge_dev)
  59. {
  60. struct vlan_device *vldev = container_of(dev, struct vlan_device, dev);
  61. dev = vldev->dep.dev;
  62. if (!dev || !dev->hotplug_ops)
  63. return UBUS_STATUS_NOT_SUPPORTED;
  64. return dev->hotplug_ops->prepare(dev, bridge_dev);
  65. }
  66. static void vlan_hotplug_check(struct vlan_device *vldev, struct device *dev)
  67. {
  68. static const struct device_hotplug_ops hotplug_ops = {
  69. .prepare = vlan_hotplug_prepare,
  70. .add = vlan_hotplug_add,
  71. .del = vlan_hotplug_del
  72. };
  73. if (!dev || !dev->hotplug_ops || avl_is_empty(&dev->vlans.avl)) {
  74. vldev->dev.hotplug_ops = NULL;
  75. return;
  76. }
  77. vldev->dev.hotplug_ops = &hotplug_ops;
  78. }
  79. static int vlan_set_device_state(struct device *dev, bool up)
  80. {
  81. struct vlan_device *vldev;
  82. int ret = 0;
  83. vldev = container_of(dev, struct vlan_device, dev);
  84. if (!up) {
  85. vldev->set_state(dev, false);
  86. system_vlan_del(dev);
  87. device_release(&vldev->dep);
  88. return 0;
  89. }
  90. ret = device_claim(&vldev->dep);
  91. if (ret < 0)
  92. return ret;
  93. system_vlan_add(vldev->dep.dev, vldev->id);
  94. ret = vldev->set_state(dev, true);
  95. if (ret)
  96. device_release(&vldev->dep);
  97. return ret;
  98. }
  99. static void vlan_dev_cb(struct device_user *dep, enum device_event ev)
  100. {
  101. char name[IFNAMSIZ + 1];
  102. struct vlan_device *vldev;
  103. vldev = container_of(dep, struct vlan_device, dep);
  104. switch(ev) {
  105. case DEV_EVENT_ADD:
  106. device_set_present(&vldev->dev, true);
  107. break;
  108. case DEV_EVENT_REMOVE:
  109. device_set_present(&vldev->dev, false);
  110. break;
  111. case DEV_EVENT_UPDATE_IFNAME:
  112. vlan_hotplug_check(vldev, dep->dev);
  113. vldev->dev.hidden = dep->dev->hidden;
  114. if (snprintf(name, sizeof(name), "%s.%d", dep->dev->ifname,
  115. vldev->id) >= sizeof(name) - 1 ||
  116. device_set_ifname(&vldev->dev, name))
  117. free_vlan_if(&vldev->dev);
  118. break;
  119. case DEV_EVENT_TOPO_CHANGE:
  120. /* Propagate topo changes */
  121. device_broadcast_event(&vldev->dev, DEV_EVENT_TOPO_CHANGE);
  122. break;
  123. default:
  124. break;
  125. }
  126. }
  127. static void
  128. vlan_config_init(struct device *dev)
  129. {
  130. struct vlan_device *vldev;
  131. vldev = container_of(dev, struct vlan_device, dev);
  132. vlan_hotplug_check(vldev, vldev->dep.dev);
  133. }
  134. static struct device *get_vlan_device(struct device *dev, char *id_str, bool create)
  135. {
  136. static struct device_type vlan_type = {
  137. .name = "VLAN",
  138. .config_params = &device_attr_list,
  139. .config_init = vlan_config_init,
  140. .free = free_vlan_if,
  141. };
  142. struct vlan_device *vldev;
  143. struct device_user *dep;
  144. char name[IFNAMSIZ + 1];
  145. char *err = NULL;
  146. int id, *alias_id;
  147. id = strtoul(id_str, &err, 10);
  148. if (err && *err) {
  149. alias_id = kvlist_get(&dev->vlan_aliases, id_str);
  150. if (!alias_id)
  151. return NULL;
  152. id = *alias_id;
  153. }
  154. /* look for an existing interface before creating a new one */
  155. list_for_each_entry(dep, &dev->users.list, list.list) {
  156. if (dep->cb != vlan_dev_cb)
  157. continue;
  158. vldev = container_of(dep, struct vlan_device, dep);
  159. if (vldev->id != id)
  160. continue;
  161. return &vldev->dev;
  162. }
  163. if (!create)
  164. return NULL;
  165. if (snprintf(name, sizeof(name), "%s.%d", dev->ifname, id) >= sizeof(name) - 1)
  166. return NULL;
  167. D(DEVICE, "Create vlan device '%s'\n", name);
  168. vldev = calloc(1, sizeof(*vldev));
  169. if (!vldev)
  170. return NULL;
  171. vldev->id = id;
  172. vldev->dev.hidden = dev->hidden;
  173. strcpy(vldev->dev.ifname, name);
  174. if (device_init(&vldev->dev, &vlan_type, NULL) < 0)
  175. goto error;
  176. vldev->dev.default_config = true;
  177. vldev->dev.config_pending = true;
  178. vldev->set_state = vldev->dev.set_state;
  179. vldev->dev.set_state = vlan_set_device_state;
  180. vldev->dep.cb = vlan_dev_cb;
  181. vlan_hotplug_check(vldev, vldev->dep.dev);
  182. device_add_user(&vldev->dep, dev);
  183. return &vldev->dev;
  184. error:
  185. device_cleanup(&vldev->dev);
  186. free(vldev);
  187. return NULL;
  188. }
  189. static char *split_vlan(char *s)
  190. {
  191. s = strchr(s, '.');
  192. if (!s)
  193. goto out;
  194. *s = 0;
  195. s++;
  196. out:
  197. return s;
  198. }
  199. struct device *get_vlan_device_chain(const char *ifname, bool create)
  200. {
  201. struct device *dev = NULL;
  202. char *buf, *s, *next;
  203. buf = strdup(ifname);
  204. if (!buf)
  205. return NULL;
  206. s = split_vlan(buf);
  207. dev = device_get(buf, create);
  208. if (!dev)
  209. goto error;
  210. do {
  211. next = split_vlan(s);
  212. dev = get_vlan_device(dev, s, create);
  213. if (!dev)
  214. goto error;
  215. s = next;
  216. if (!s)
  217. goto out;
  218. } while (1);
  219. error:
  220. dev = NULL;
  221. out:
  222. free(buf);
  223. return dev;
  224. }