relayd: put rule requests on stack
[project/relayd.git] / relayd.h
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
21 #include <arpa/inet.h>
22 #include <net/if.h>
23 #include <net/ethernet.h>
24 #include <netinet/if_ether.h>
25 #include <netinet/ip.h>
26 #include <netinet/udp.h>
27
28 #include <linux/if_packet.h>
29 #include <linux/rtnetlink.h>
30
31 #include <stdint.h>
32 #include <stdbool.h>
33
34 #include <libubox/uloop.h>
35 #include <libubox/list.h>
36
37 #define DEBUG
38 #ifdef DEBUG
39 #define DPRINTF(level, ...) if (debug >= level) fprintf(stderr, __VA_ARGS__);
40 #else
41 #define DPRINTF(...) do {} while(0)
42 #endif
43
44 #ifndef __packed
45 #define __packed __attribute__((packed))
46 #endif
47
48 #define __uc(c) ((unsigned char *)(c))
49
50 #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
51 #define MAC_BUF(_c) __uc(_c)[0], __uc(_c)[1], __uc(_c)[2], __uc(_c)[3], __uc(_c)[4], __uc(_c)[5]
52
53 #define IP_FMT "%d.%d.%d.%d"
54 #define IP_BUF(_c) __uc(_c)[0], __uc(_c)[1], __uc(_c)[2], __uc(_c)[3]
55
56 #define DUMMY_IP ((uint8_t *) "\x01\x01\x01\x01")
57
58 #define DHCP_FLAG_BROADCAST (1 << 15)
59
60 struct relayd_interface {
61 struct list_head list;
62 struct uloop_fd fd;
63 struct uloop_fd bcast_fd;
64 struct sockaddr_ll sll;
65 struct sockaddr_ll bcast_sll;
66 char ifname[IFNAMSIZ];
67 struct list_head hosts;
68 uint8_t src_ip[4];
69 bool managed;
70 int rt_table;
71 };
72
73 struct relayd_host {
74 struct list_head list;
75 struct list_head routes;
76 struct relayd_interface *rif;
77 uint8_t lladdr[ETH_ALEN];
78 uint8_t ipaddr[4];
79 struct uloop_timeout timeout;
80 int cleanup_pending;
81 };
82
83 struct relayd_route {
84 struct list_head list;
85 uint8_t dest[4];
86 uint8_t mask;
87 };
88
89 struct arp_packet {
90 struct ether_header eth;
91 struct ether_arp arp;
92 } __packed;
93
94 struct rtnl_req {
95 struct nlmsghdr nl;
96 struct rtmsg rt;
97 } __packed;
98
99 extern struct list_head interfaces;
100 extern int debug;
101 extern int route_table;
102 extern uint8_t local_addr[4];
103 extern int local_route_table;
104
105 void rtnl_route_set(struct relayd_host *host, struct relayd_route *route, bool add);
106
107 static inline void relayd_add_route(struct relayd_host *host, struct relayd_route *route)
108 {
109 rtnl_route_set(host, route, true);
110 }
111
112 static inline void relayd_del_route(struct relayd_host *host, struct relayd_route *route)
113 {
114 rtnl_route_set(host, route, false);
115 }
116
117 void relayd_add_interface_routes(struct relayd_interface *rif);
118 void relayd_del_interface_routes(struct relayd_interface *rif);
119
120 int relayd_rtnl_init(void);
121 void relayd_rtnl_done(void);
122
123 struct relayd_host *relayd_refresh_host(struct relayd_interface *rif,
124 const uint8_t *lladdr,
125 const uint8_t *ipaddr);
126 void relayd_add_host_route(struct relayd_host *host, const uint8_t *ipaddr, uint8_t mask);
127 void relayd_add_pending_route(const uint8_t *gateway, const uint8_t *dest, uint8_t mask, int timeout);
128
129 void relayd_forward_bcast_packet(struct relayd_interface *from_rif, void *packet, int len);
130 bool relayd_handle_dhcp_packet(struct relayd_interface *rif, void *data, int len, bool forward, bool parse);
131
132 #endif