c8b023c2a8771dd7a549f878bb7de20bdea62d6d
[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 #include <linux/neighbour.h>
31
32 #include <stdint.h>
33 #include <stdbool.h>
34
35 #include "uloop.h"
36 #include "list.h"
37
38 #define DEBUG
39 #ifdef DEBUG
40 #define DPRINTF(level, ...) if (debug >= level) fprintf(stderr, __VA_ARGS__);
41 #else
42 #define DPRINTF(...) do {} while(0)
43 #endif
44
45 #ifndef __packed
46 #define __packed __attribute__((packed))
47 #endif
48
49 #define __uc(c) ((unsigned char *)(c))
50
51 #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
52 #define MAC_BUF(_c) __uc(_c)[0], __uc(_c)[1], __uc(_c)[2], __uc(_c)[3], __uc(_c)[4], __uc(_c)[5]
53
54 #define IP_FMT  "%d.%d.%d.%d"
55 #define IP_BUF(_c) __uc(_c)[0], __uc(_c)[1], __uc(_c)[2], __uc(_c)[3]
56
57 #define DUMMY_IP ((uint8_t *) "\x01\x01\x01\x01")
58
59 #define DHCP_FLAG_BROADCAST     (1 << 15)
60
61 struct relayd_interface {
62         struct list_head list;
63         struct uloop_fd fd;
64         struct uloop_fd bcast_fd;
65         struct sockaddr_ll sll;
66         struct sockaddr_ll bcast_sll;
67         char ifname[IFNAMSIZ];
68         struct list_head hosts;
69         uint8_t src_ip[4];
70         bool managed;
71         int rt_table;
72 };
73
74 struct relayd_host {
75         struct list_head list;
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 arp_packet {
84         struct ether_header eth;
85         struct ether_arp arp;
86 } __packed;
87
88 struct ip_packet {
89         struct ether_header eth;
90         struct iphdr iph;
91 } __packed;
92
93 struct dhcp_header {
94         uint8_t op, htype, hlen, hops;
95         uint32_t xit;
96         uint16_t secs, flags;
97         struct in_addr ciaddr, yiaddr, siaddr, giaddr;
98         unsigned char chaddr[16];
99         unsigned char sname[64];
100         unsigned char file[128];
101 } __packed;
102
103 struct rtnl_req {
104         struct nlmsghdr nl;
105         struct rtmsg rt;
106 } __packed;
107
108 extern struct list_head interfaces;
109 extern int debug;
110 extern int route_table;
111
112 void rtnl_route_set(struct relayd_host *host, bool add);
113
114 static inline void relayd_add_route(struct relayd_host *host)
115 {
116         rtnl_route_set(host, true);
117 }
118
119 static inline void relayd_del_route(struct relayd_host *host)
120 {
121         rtnl_route_set(host, false);
122 }
123
124 void relayd_add_interface_routes(struct relayd_interface *rif);
125 void relayd_del_interface_routes(struct relayd_interface *rif);
126
127 int relayd_rtnl_init(void);
128 void relayd_rtnl_done(void);
129
130 struct relayd_host *relayd_refresh_host(struct relayd_interface *rif, const uint8_t *lladdr, const uint8_t *ipaddr);
131
132 #endif