wg.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2022 Felix Fietkau <nbd@nbd.name>
  4. */
  5. #ifndef __UNETD_WG_H
  6. #define __UNETD_WG_H
  7. #define WG_KEY_LEN 32
  8. #define WG_KEY_LEN_HEX (WG_KEY_LEN * 2 + 1)
  9. enum wg_update_cmd {
  10. WG_PEER_CREATE,
  11. WG_PEER_UPDATE,
  12. WG_PEER_DELETE,
  13. };
  14. struct network;
  15. struct network_peer;
  16. union network_endpoint;
  17. struct wg_ops {
  18. const char *name;
  19. bool (*check)(struct network *net);
  20. int (*init)(struct network *net);
  21. void (*cleanup)(struct network *net);
  22. int (*init_local)(struct network *net, struct network_peer *peer);
  23. int (*peer_refresh)(struct network *net);
  24. int (*peer_update)(struct network *net, struct network_peer *peer,
  25. enum wg_update_cmd cmd);
  26. int (*peer_connect)(struct network *net, struct network_peer *peer,
  27. union network_endpoint *ep);
  28. };
  29. struct wg {
  30. const struct wg_ops *ops;
  31. };
  32. extern const struct wg_ops wg_user_ops;
  33. extern const struct wg_ops wg_linux_ops;
  34. int wg_init_network(struct network *net);
  35. void wg_cleanup_network(struct network *net);
  36. #define wg_init_local(net, ...) (net)->wg.ops->init_local(net, ##__VA_ARGS__)
  37. #define wg_peer_update(net, ...) (net)->wg.ops->peer_update(net, ##__VA_ARGS__)
  38. #define wg_peer_connect(net, ...) (net)->wg.ops->peer_connect(net, ##__VA_ARGS__)
  39. #define wg_peer_refresh(net) (net)->wg.ops->peer_refresh(net)
  40. /* internal */
  41. struct network_peer *wg_peer_update_start(struct network *net, const uint8_t *key);
  42. void wg_peer_update_done(struct network *net, struct network_peer *peer);
  43. void wg_peer_set_last_handshake(struct network *net, struct network_peer *peer,
  44. uint64_t now, uint64_t sec);
  45. void wg_peer_set_rx_bytes(struct network *net, struct network_peer *peer,
  46. uint64_t bytes);
  47. void wg_peer_set_endpoint(struct network *net, struct network_peer *peer,
  48. void *data, size_t len);
  49. #endif