vlan.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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)
  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 void vlan_dev_set_name(struct vlan_device *vldev, struct device *dev)
  54. {
  55. vldev->dev.hidden = dev->hidden;
  56. snprintf(vldev->dev.ifname, IFNAMSIZ, "%s.%d", dev->ifname, vldev->id);
  57. }
  58. static void vlan_dev_cb(struct device_user *dep, enum device_event ev)
  59. {
  60. struct vlan_device *vldev;
  61. vldev = container_of(dep, struct vlan_device, dep);
  62. switch(ev) {
  63. case DEV_EVENT_ADD:
  64. device_set_present(&vldev->dev, true);
  65. break;
  66. case DEV_EVENT_REMOVE:
  67. device_set_present(&vldev->dev, false);
  68. break;
  69. case DEV_EVENT_UPDATE_IFNAME:
  70. vlan_dev_set_name(vldev, dep->dev);
  71. device_broadcast_event(&vldev->dev, ev);
  72. break;
  73. default:
  74. break;
  75. }
  76. }
  77. static struct device *get_vlan_device(struct device *dev, int id, bool create)
  78. {
  79. static const struct device_type vlan_type = {
  80. .name = "VLAN",
  81. .config_params = &device_attr_list,
  82. .free = free_vlan_if,
  83. };
  84. struct vlan_device *vldev;
  85. struct device_user *dep;
  86. /* look for an existing interface before creating a new one */
  87. list_for_each_entry(dep, &dev->users.list, list.list) {
  88. if (dep->cb != vlan_dev_cb)
  89. continue;
  90. vldev = container_of(dep, struct vlan_device, dep);
  91. if (vldev->id != id)
  92. continue;
  93. return &vldev->dev;
  94. }
  95. if (!create)
  96. return NULL;
  97. vldev = calloc(1, sizeof(*vldev));
  98. device_init_virtual(&vldev->dev, &vlan_type, NULL);
  99. vldev->dev.default_config = true;
  100. vldev->set_state = vldev->dev.set_state;
  101. vldev->dev.set_state = vlan_set_device_state;
  102. vldev->id = id;
  103. vlan_dev_set_name(vldev, dev);
  104. vldev->dep.cb = vlan_dev_cb;
  105. device_add_user(&vldev->dep, dev);
  106. return &vldev->dev;
  107. }
  108. static char *split_vlan(char *s)
  109. {
  110. s = strchr(s, '.');
  111. if (!s)
  112. goto out;
  113. *s = 0;
  114. s++;
  115. out:
  116. return s;
  117. }
  118. struct device *get_vlan_device_chain(const char *ifname, bool create)
  119. {
  120. struct device *dev = NULL;
  121. char *buf, *s, *next, *err = NULL;
  122. int id;
  123. buf = strdup(ifname);
  124. if (!buf)
  125. return NULL;
  126. s = split_vlan(buf);
  127. dev = device_get(buf, create);
  128. if (!dev)
  129. goto error;
  130. do {
  131. next = split_vlan(s);
  132. id = strtoul(s, &err, 10);
  133. if (err && *err)
  134. goto error;
  135. dev = get_vlan_device(dev, id, create);
  136. if (!dev)
  137. goto error;
  138. s = next;
  139. if (!s)
  140. goto out;
  141. } while (1);
  142. error:
  143. dev = NULL;
  144. out:
  145. free(buf);
  146. return dev;
  147. }