-
Notifications
You must be signed in to change notification settings - Fork 0
/
relayd.h
152 lines (124 loc) · 4.68 KB
/
relayd.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (C) 2022 Synaptics Incorporated. All rights reserved
/*
* Copyright (C) 2010 Felix Fietkau <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License v2 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
*/
// INFORMATION CONTAINED IN THIS DOCUMENT IS PROVIDED "AS-IS,” AND SYNAPTICS
// EXPRESSLY DISCLAIMS ALL EXPRESS AND IMPLIED WARRANTIES, INCLUDING ANY IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND ANY
// WARRANTIES OF NON-INFRINGEMENT OF ANY INTELLECTUAL PROPERTY RIGHTS. IN NO
// EVENT SHALL SYNAPTICS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// PUNITIVE, OR CONSEQUENTIAL DAMAGES ARISING OUT OF OR IN CONNECTION WITH THE
// USE OF THE INFORMATION CONTAINED IN THIS DOCUMENT, HOWEVER CAUSED AND BASED
// ON ANY THEORY OF LIABILITY, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, AND EVEN IF SYNAPTICS WAS ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE. IF A TRIBUNAL OF COMPETENT JURISDICTION DOES NOT PERMIT THE
// DISCLAIMER OF DIRECT DAMAGES OR ANY OTHER DAMAGES, SYNAPTICS’ TOTAL
// CUMULATIVE LIABILITY TO ANY PARTY SHALL NOT EXCEED ONE HUNDRED U.S. DOLLARS.
#ifndef __RELAYD_H
#define __RELAYD_H
#include <arpa/inet.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <netinet/if_ether.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <linux/if_packet.h>
#include <linux/rtnetlink.h>
#include <stdint.h>
#include <stdbool.h>
#include <libubox/uloop.h>
#include <libubox/list.h>
#include <pcap.h>
#define DEBUG
#ifdef DEBUG
#define DPRINTF(level, ...) if (debug >= level) fprintf(stderr, __VA_ARGS__);
#else
#define DPRINTF(...) do {} while(0)
#endif
#ifndef __packed
#define __packed __attribute__((packed))
#endif
#define __uc(c) ((unsigned char *)(c))
#define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
#define MAC_BUF(_c) __uc(_c)[0], __uc(_c)[1], __uc(_c)[2], __uc(_c)[3], __uc(_c)[4], __uc(_c)[5]
#define IP_FMT "%d.%d.%d.%d"
#define IP_BUF(_c) __uc(_c)[0], __uc(_c)[1], __uc(_c)[2], __uc(_c)[3]
#define DUMMY_IP ((uint8_t *) "\x01\x01\x01\x01")
#define DHCP_FLAG_BROADCAST (1 << 15)
struct relayd_interface {
struct list_head list;
struct uloop_fd fd;
struct uloop_fd bcast_fd;
struct uloop_fd dhcp_unicast_sniffer_fd;
struct sockaddr_ll sll;
struct sockaddr_ll bcast_sll;
pcap_t *pcap_handle;
char ifname[IFNAMSIZ];
struct list_head hosts;
uint8_t src_ip[4];
bool managed;
int rt_table;
};
struct relayd_host {
struct list_head list;
struct list_head routes;
struct relayd_interface *rif;
uint8_t lladdr[ETH_ALEN];
uint8_t ipaddr[4];
struct uloop_timeout timeout;
int cleanup_pending;
};
struct relayd_route {
struct list_head list;
uint8_t dest[4];
uint8_t mask;
};
struct arp_packet {
struct ether_header eth;
struct ether_arp arp;
} __packed;
struct rtnl_req {
struct nlmsghdr nl;
struct rtmsg rt;
} __packed;
extern struct list_head interfaces;
extern int debug;
extern int route_table;
extern uint8_t local_addr[4];
extern int local_route_table;
void rtnl_route_set(struct relayd_host *host, struct relayd_route *route, bool add);
static inline void relayd_add_route(struct relayd_host *host, struct relayd_route *route)
{
rtnl_route_set(host, route, true);
}
static inline void relayd_del_route(struct relayd_host *host, struct relayd_route *route)
{
rtnl_route_set(host, route, false);
}
void relayd_add_interface_routes(struct relayd_interface *rif);
void relayd_del_interface_routes(struct relayd_interface *rif);
int relayd_rtnl_init(void);
void relayd_rtnl_done(void);
struct relayd_host *relayd_refresh_host(struct relayd_interface *rif,
const uint8_t *lladdr,
const uint8_t *ipaddr);
void relayd_add_host_route(struct relayd_host *host, const uint8_t *ipaddr, uint8_t mask);
void relayd_add_pending_route(const uint8_t *gateway, const uint8_t *dest, uint8_t mask, int timeout);
void relayd_forward_bcast_packet(struct relayd_interface *from_rif, void *packet, int len);
bool relayd_handle_dhcp_packet(struct relayd_interface *rif, void *data, int len, bool forward, bool parse);
bool relayd_handle_unicast_dhcp_packet(struct relayd_interface *rif, void *data, int len, bool forward, bool parse);
#endif