veth.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  4. * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
  5. * Copyright (C) 2017 Matthias Schiffer <mschiffer@universe-factory.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2
  9. * as published by the Free Software Foundation
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <assert.h>
  20. #include <errno.h>
  21. #include "netifd.h"
  22. #include "device.h"
  23. #include "interface.h"
  24. #include "system.h"
  25. enum {
  26. VETH_ATTR_MACADDR,
  27. VETH_ATTR_PEER_NAME,
  28. VETH_ATTR_PEER_MACADDR,
  29. __VETH_ATTR_MAX
  30. };
  31. static const struct blobmsg_policy veth_attrs[__VETH_ATTR_MAX] = {
  32. [VETH_ATTR_MACADDR] = { "macaddr", BLOBMSG_TYPE_STRING },
  33. [VETH_ATTR_PEER_NAME] = { "peer_name", BLOBMSG_TYPE_STRING },
  34. [VETH_ATTR_PEER_MACADDR] = { "peer_macaddr", BLOBMSG_TYPE_STRING },
  35. };
  36. static const struct uci_blob_param_list veth_attr_list = {
  37. .n_params = __VETH_ATTR_MAX,
  38. .params = veth_attrs,
  39. .n_next = 1,
  40. .next = { &device_attr_list },
  41. };
  42. struct veth {
  43. struct device dev;
  44. device_state_cb set_state;
  45. struct blob_attr *config_data;
  46. struct veth_config config;
  47. };
  48. static int
  49. veth_set_down(struct veth *veth)
  50. {
  51. veth->set_state(&veth->dev, false);
  52. system_veth_del(&veth->dev);
  53. return 0;
  54. }
  55. static int
  56. veth_set_up(struct veth *veth)
  57. {
  58. int ret;
  59. ret = system_veth_add(&veth->dev, &veth->config);
  60. if (ret < 0)
  61. return ret;
  62. ret = veth->set_state(&veth->dev, true);
  63. if (ret)
  64. goto delete;
  65. return 0;
  66. delete:
  67. system_veth_del(&veth->dev);
  68. return ret;
  69. }
  70. static int
  71. veth_set_state(struct device *dev, bool up)
  72. {
  73. struct veth *veth;
  74. D(SYSTEM, "veth_set_state(%s, %u)\n", dev->ifname, up);
  75. veth = container_of(dev, struct veth, dev);
  76. if (up)
  77. return veth_set_up(veth);
  78. else
  79. return veth_set_down(veth);
  80. }
  81. static void
  82. veth_free(struct device *dev)
  83. {
  84. struct veth *veth;
  85. veth = container_of(dev, struct veth, dev);
  86. free(veth->config_data);
  87. free(veth);
  88. }
  89. static void
  90. veth_dump_info(struct device *dev, struct blob_buf *b)
  91. {
  92. struct veth *veth;
  93. veth = container_of(dev, struct veth, dev);
  94. if (veth->config.flags & VETH_OPT_PEER_NAME)
  95. blobmsg_add_string(b, "peer", veth->config.peer_name);
  96. system_if_dump_info(dev, b);
  97. }
  98. static void
  99. veth_config_init(struct device *dev)
  100. {
  101. device_set_present(dev, true);
  102. }
  103. static void
  104. veth_apply_settings(struct veth *veth, struct blob_attr **tb)
  105. {
  106. struct veth_config *cfg = &veth->config;
  107. struct blob_attr *cur;
  108. struct ether_addr *ea;
  109. cfg->flags = 0;
  110. if ((cur = tb[VETH_ATTR_MACADDR]))
  111. {
  112. ea = ether_aton(blobmsg_data(cur));
  113. if (ea) {
  114. memcpy(cfg->macaddr, ea, 6);
  115. cfg->flags |= VETH_OPT_MACADDR;
  116. }
  117. }
  118. if ((cur = tb[VETH_ATTR_PEER_NAME]))
  119. {
  120. strncpy(cfg->peer_name, blobmsg_get_string(cur), sizeof(cfg->peer_name)-1);
  121. cfg->flags |= VETH_OPT_PEER_NAME;
  122. }
  123. if ((cur = tb[VETH_ATTR_PEER_MACADDR]))
  124. {
  125. ea = ether_aton(blobmsg_data(cur));
  126. if (ea) {
  127. memcpy(cfg->peer_macaddr, ea, 6);
  128. cfg->flags |= VETH_OPT_PEER_MACADDR;
  129. }
  130. }
  131. }
  132. static enum dev_change_type
  133. veth_reload(struct device *dev, struct blob_attr *attr)
  134. {
  135. struct blob_attr *tb_dev[__DEV_ATTR_MAX];
  136. struct blob_attr *tb_mv[__VETH_ATTR_MAX];
  137. enum dev_change_type ret = DEV_CONFIG_APPLIED;
  138. struct veth *veth;
  139. veth = container_of(dev, struct veth, dev);
  140. attr = blob_memdup(attr);
  141. blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
  142. blob_data(attr), blob_len(attr));
  143. blobmsg_parse(veth_attrs, __VETH_ATTR_MAX, tb_mv,
  144. blob_data(attr), blob_len(attr));
  145. device_init_settings(dev, tb_dev);
  146. veth_apply_settings(veth, tb_mv);
  147. if (veth->config_data) {
  148. struct blob_attr *otb_dev[__DEV_ATTR_MAX];
  149. struct blob_attr *otb_mv[__VETH_ATTR_MAX];
  150. blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
  151. blob_data(veth->config_data), blob_len(veth->config_data));
  152. if (uci_blob_diff(tb_dev, otb_dev, &device_attr_list, NULL))
  153. ret = DEV_CONFIG_RESTART;
  154. blobmsg_parse(veth_attrs, __VETH_ATTR_MAX, otb_mv,
  155. blob_data(veth->config_data), blob_len(veth->config_data));
  156. if (uci_blob_diff(tb_mv, otb_mv, &veth_attr_list, NULL))
  157. ret = DEV_CONFIG_RESTART;
  158. veth_config_init(dev);
  159. }
  160. free(veth->config_data);
  161. veth->config_data = attr;
  162. return ret;
  163. }
  164. static struct device *
  165. veth_create(const char *name, struct device_type *devtype,
  166. struct blob_attr *attr)
  167. {
  168. struct veth *veth;
  169. struct device *dev = NULL;
  170. veth = calloc(1, sizeof(*veth));
  171. if (!veth)
  172. return NULL;
  173. dev = &veth->dev;
  174. if (device_init(dev, devtype, name) < 0) {
  175. device_cleanup(dev);
  176. free(veth);
  177. return NULL;
  178. }
  179. dev->config_pending = true;
  180. veth->set_state = dev->set_state;
  181. dev->set_state = veth_set_state;
  182. dev->hotplug_ops = NULL;
  183. veth_reload(dev, attr);
  184. return dev;
  185. }
  186. static struct device_type veth_device_type = {
  187. .name = "veth",
  188. .config_params = &veth_attr_list,
  189. .create = veth_create,
  190. .config_init = veth_config_init,
  191. .reload = veth_reload,
  192. .free = veth_free,
  193. .dump_info = veth_dump_info,
  194. };
  195. static void __init veth_device_type_init(void)
  196. {
  197. device_type_add(&veth_device_type);
  198. }