veth.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 <net/ethernet.h>
  22. #ifdef linux
  23. #include <netinet/ether.h>
  24. #endif
  25. #include "netifd.h"
  26. #include "device.h"
  27. #include "interface.h"
  28. #include "system.h"
  29. enum {
  30. VETH_ATTR_MACADDR,
  31. VETH_ATTR_PEER_NAME,
  32. VETH_ATTR_PEER_MACADDR,
  33. __VETH_ATTR_MAX
  34. };
  35. static const struct blobmsg_policy veth_attrs[__VETH_ATTR_MAX] = {
  36. [VETH_ATTR_MACADDR] = { "macaddr", BLOBMSG_TYPE_STRING },
  37. [VETH_ATTR_PEER_NAME] = { "peer_name", BLOBMSG_TYPE_STRING },
  38. [VETH_ATTR_PEER_MACADDR] = { "peer_macaddr", BLOBMSG_TYPE_STRING },
  39. };
  40. static const struct uci_blob_param_list veth_attr_list = {
  41. .n_params = __VETH_ATTR_MAX,
  42. .params = veth_attrs,
  43. .n_next = 1,
  44. .next = { &device_attr_list },
  45. };
  46. struct veth {
  47. struct device dev;
  48. device_state_cb set_state;
  49. struct blob_attr *config_data;
  50. struct veth_config config;
  51. };
  52. static int
  53. veth_set_down(struct veth *veth)
  54. {
  55. veth->set_state(&veth->dev, false);
  56. system_veth_del(&veth->dev);
  57. return 0;
  58. }
  59. static int
  60. veth_set_up(struct veth *veth)
  61. {
  62. int ret;
  63. ret = system_veth_add(&veth->dev, &veth->config);
  64. if (ret < 0)
  65. return ret;
  66. ret = veth->set_state(&veth->dev, true);
  67. if (ret)
  68. goto delete;
  69. return 0;
  70. delete:
  71. system_veth_del(&veth->dev);
  72. return ret;
  73. }
  74. static int
  75. veth_set_state(struct device *dev, bool up)
  76. {
  77. struct veth *veth;
  78. D(SYSTEM, "veth_set_state(%s, %u)\n", dev->ifname, up);
  79. veth = container_of(dev, struct veth, dev);
  80. if (up)
  81. return veth_set_up(veth);
  82. else
  83. return veth_set_down(veth);
  84. }
  85. static void
  86. veth_free(struct device *dev)
  87. {
  88. struct veth *veth;
  89. veth = container_of(dev, struct veth, dev);
  90. free(veth->config_data);
  91. free(veth);
  92. }
  93. static void
  94. veth_dump_info(struct device *dev, struct blob_buf *b)
  95. {
  96. struct veth *veth;
  97. veth = container_of(dev, struct veth, dev);
  98. if (veth->config.flags & VETH_OPT_PEER_NAME)
  99. blobmsg_add_string(b, "peer", veth->config.peer_name);
  100. system_if_dump_info(dev, b);
  101. }
  102. static void
  103. veth_config_init(struct device *dev)
  104. {
  105. device_set_present(dev, true);
  106. }
  107. static void
  108. veth_apply_settings(struct veth *veth, struct blob_attr **tb)
  109. {
  110. struct veth_config *cfg = &veth->config;
  111. struct blob_attr *cur;
  112. struct ether_addr *ea;
  113. cfg->flags = 0;
  114. if ((cur = tb[VETH_ATTR_MACADDR]))
  115. {
  116. ea = ether_aton(blobmsg_data(cur));
  117. if (ea) {
  118. memcpy(cfg->macaddr, ea, 6);
  119. cfg->flags |= VETH_OPT_MACADDR;
  120. }
  121. }
  122. if ((cur = tb[VETH_ATTR_PEER_NAME]))
  123. {
  124. strncpy(cfg->peer_name, blobmsg_get_string(cur), sizeof(cfg->peer_name)-1);
  125. cfg->flags |= VETH_OPT_PEER_NAME;
  126. }
  127. if ((cur = tb[VETH_ATTR_PEER_MACADDR]))
  128. {
  129. ea = ether_aton(blobmsg_data(cur));
  130. if (ea) {
  131. memcpy(cfg->peer_macaddr, ea, 6);
  132. cfg->flags |= VETH_OPT_PEER_MACADDR;
  133. }
  134. }
  135. }
  136. static enum dev_change_type
  137. veth_reload(struct device *dev, struct blob_attr *attr)
  138. {
  139. struct blob_attr *tb_dev[__DEV_ATTR_MAX];
  140. struct blob_attr *tb_mv[__VETH_ATTR_MAX];
  141. enum dev_change_type ret = DEV_CONFIG_APPLIED;
  142. struct veth *veth;
  143. veth = container_of(dev, struct veth, dev);
  144. attr = blob_memdup(attr);
  145. blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
  146. blob_data(attr), blob_len(attr));
  147. blobmsg_parse(veth_attrs, __VETH_ATTR_MAX, tb_mv,
  148. blob_data(attr), blob_len(attr));
  149. device_init_settings(dev, tb_dev);
  150. veth_apply_settings(veth, tb_mv);
  151. if (veth->config_data) {
  152. struct blob_attr *otb_dev[__DEV_ATTR_MAX];
  153. struct blob_attr *otb_mv[__VETH_ATTR_MAX];
  154. blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
  155. blob_data(veth->config_data), blob_len(veth->config_data));
  156. if (uci_blob_diff(tb_dev, otb_dev, &device_attr_list, NULL))
  157. ret = DEV_CONFIG_RESTART;
  158. blobmsg_parse(veth_attrs, __VETH_ATTR_MAX, otb_mv,
  159. blob_data(veth->config_data), blob_len(veth->config_data));
  160. if (uci_blob_diff(tb_mv, otb_mv, &veth_attr_list, NULL))
  161. ret = DEV_CONFIG_RESTART;
  162. veth_config_init(dev);
  163. }
  164. free(veth->config_data);
  165. veth->config_data = attr;
  166. return ret;
  167. }
  168. static struct device *
  169. veth_create(const char *name, struct device_type *devtype,
  170. struct blob_attr *attr)
  171. {
  172. struct veth *veth;
  173. struct device *dev = NULL;
  174. veth = calloc(1, sizeof(*veth));
  175. if (!veth)
  176. return NULL;
  177. dev = &veth->dev;
  178. if (device_init(dev, devtype, name) < 0) {
  179. device_cleanup(dev);
  180. free(veth);
  181. return NULL;
  182. }
  183. dev->config_pending = true;
  184. veth->set_state = dev->set_state;
  185. dev->set_state = veth_set_state;
  186. dev->hotplug_ops = NULL;
  187. veth_reload(dev, attr);
  188. return dev;
  189. }
  190. static struct device_type veth_device_type = {
  191. .name = "veth",
  192. .config_params = &veth_attr_list,
  193. .create = veth_create,
  194. .config_init = veth_config_init,
  195. .reload = veth_reload,
  196. .free = veth_free,
  197. .dump_info = veth_dump_info,
  198. };
  199. static void __init veth_device_type_init(void)
  200. {
  201. device_type_add(&veth_device_type);
  202. }