cd24aa6feacd2f0199a8da8d2e8d6a2c8bd80307
2 * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
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.
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.
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.
21 #include <arpa/inet.h>
23 #include <net/ethernet.h>
24 #include <netinet/if_ether.h>
25 #include <netinet/ip.h>
26 #include <netinet/udp.h>
28 #include <linux/if_packet.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/neighbour.h>
40 #define DPRINTF(level, ...) if (debug >= level) fprintf(stderr, __VA_ARGS__);
42 #define DPRINTF(...) do {} while(0)
46 #define __packed __attribute__((packed))
49 #define __uc(c) ((unsigned char *)(c))
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]
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]
57 #define DUMMY_IP ((uint8_t *) "\x01\x01\x01\x01")
59 #define DHCP_FLAG_BROADCAST (1 << 15)
61 struct relayd_interface
{
62 struct list_head list
;
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
;
75 struct list_head list
;
76 struct list_head routes
;
77 struct relayd_interface
*rif
;
78 uint8_t lladdr
[ETH_ALEN
];
80 struct uloop_timeout timeout
;
85 struct list_head list
;
91 struct ether_header eth
;
100 extern struct list_head interfaces
;
102 extern int route_table
;
103 extern uint8_t local_addr
[4];
104 extern int local_route_table
;
106 void rtnl_route_set(struct relayd_host
*host
, struct relayd_route
*route
, bool add
);
108 static inline void relayd_add_route(struct relayd_host
*host
, struct relayd_route
*route
)
110 rtnl_route_set(host
, route
, true);
113 static inline void relayd_del_route(struct relayd_host
*host
, struct relayd_route
*route
)
115 rtnl_route_set(host
, route
, false);
118 void relayd_add_interface_routes(struct relayd_interface
*rif
);
119 void relayd_del_interface_routes(struct relayd_interface
*rif
);
121 int relayd_rtnl_init(void);
122 void relayd_rtnl_done(void);
124 struct relayd_host
*relayd_refresh_host(struct relayd_interface
*rif
,
125 const uint8_t *lladdr
,
126 const uint8_t *ipaddr
);
127 void relayd_add_host_route(struct relayd_host
*host
, const uint8_t *ipaddr
, uint8_t mask
);
128 void relayd_add_pending_route(const uint8_t *gateway
, const uint8_t *dest
, uint8_t mask
, int timeout
);
130 void relayd_forward_bcast_packet(struct relayd_interface
*from_rif
, void *packet
, int len
);
131 bool relayd_handle_dhcp_packet(struct relayd_interface
*rif
, void *data
, int len
, bool forward
);