vlan.c 6.0 KB

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