relayd.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License v2 as published by
  6. * the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  16. *
  17. */
  18. #ifndef __RELAYD_H
  19. #define __RELAYD_H
  20. #include <arpa/inet.h>
  21. #include <net/if.h>
  22. #include <net/ethernet.h>
  23. #include <netinet/if_ether.h>
  24. #include <netinet/ip.h>
  25. #include <netinet/udp.h>
  26. #include <linux/if_packet.h>
  27. #include <linux/rtnetlink.h>
  28. #include <stdint.h>
  29. #include <stdbool.h>
  30. #include <libubox/uloop.h>
  31. #include <libubox/list.h>
  32. #define DEBUG
  33. #ifdef DEBUG
  34. #define DPRINTF(level, ...) if (debug >= level) fprintf(stderr, __VA_ARGS__);
  35. #else
  36. #define DPRINTF(...) do {} while(0)
  37. #endif
  38. #ifndef __packed
  39. #define __packed __attribute__((packed))
  40. #endif
  41. #define __uc(c) ((unsigned char *)(c))
  42. #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
  43. #define MAC_BUF(_c) __uc(_c)[0], __uc(_c)[1], __uc(_c)[2], __uc(_c)[3], __uc(_c)[4], __uc(_c)[5]
  44. #define IP_FMT "%d.%d.%d.%d"
  45. #define IP_BUF(_c) __uc(_c)[0], __uc(_c)[1], __uc(_c)[2], __uc(_c)[3]
  46. #define DUMMY_IP ((uint8_t *) "\x01\x01\x01\x01")
  47. #define DHCP_FLAG_BROADCAST (1 << 15)
  48. struct relayd_interface {
  49. struct list_head list;
  50. struct uloop_fd fd;
  51. struct uloop_fd bcast_fd;
  52. struct sockaddr_ll sll;
  53. struct sockaddr_ll bcast_sll;
  54. char ifname[IFNAMSIZ];
  55. struct list_head hosts;
  56. uint8_t src_ip[4];
  57. bool managed;
  58. int rt_table;
  59. };
  60. struct relayd_host {
  61. struct list_head list;
  62. struct list_head routes;
  63. struct relayd_interface *rif;
  64. uint8_t lladdr[ETH_ALEN];
  65. uint8_t ipaddr[4];
  66. struct uloop_timeout timeout;
  67. int cleanup_pending;
  68. };
  69. struct relayd_route {
  70. struct list_head list;
  71. uint8_t dest[4];
  72. uint8_t mask;
  73. };
  74. struct arp_packet {
  75. struct ether_header eth;
  76. struct ether_arp arp;
  77. } __packed;
  78. struct rtnl_req {
  79. struct nlmsghdr nl;
  80. struct rtmsg rt;
  81. } __packed;
  82. extern struct list_head interfaces;
  83. extern int debug;
  84. extern int route_table;
  85. extern uint8_t local_addr[4];
  86. extern int local_route_table;
  87. void rtnl_route_set(struct relayd_host *host, struct relayd_route *route, bool add);
  88. static inline void relayd_add_route(struct relayd_host *host, struct relayd_route *route)
  89. {
  90. rtnl_route_set(host, route, true);
  91. }
  92. static inline void relayd_del_route(struct relayd_host *host, struct relayd_route *route)
  93. {
  94. rtnl_route_set(host, route, false);
  95. }
  96. void relayd_add_interface_routes(struct relayd_interface *rif);
  97. void relayd_del_interface_routes(struct relayd_interface *rif);
  98. int relayd_rtnl_init(void);
  99. void relayd_rtnl_done(void);
  100. struct relayd_host *relayd_refresh_host(struct relayd_interface *rif,
  101. const uint8_t *lladdr,
  102. const uint8_t *ipaddr);
  103. void relayd_add_host_route(struct relayd_host *host, const uint8_t *ipaddr, uint8_t mask);
  104. void relayd_add_pending_route(const uint8_t *gateway, const uint8_t *dest, uint8_t mask, int timeout);
  105. void relayd_forward_bcast_packet(struct relayd_interface *from_rif, void *packet, int len);
  106. bool relayd_handle_dhcp_packet(struct relayd_interface *rif, void *data, int len, bool forward, bool parse);
  107. #endif