xref: /netbsd-src/usr.bin/rump_dhcpclient/net.h (revision e963960681e924e34192f4aace9206aa70d13d8d)
1*e9639606Spooka /*
2*e9639606Spooka  * dhcpcd - DHCP client daemon
3*e9639606Spooka  * Copyright (c) 2006-2010 Roy Marples <roy@marples.name>
4*e9639606Spooka  * All rights reserved
5*e9639606Spooka 
6*e9639606Spooka  * Redistribution and use in source and binary forms, with or without
7*e9639606Spooka  * modification, are permitted provided that the following conditions
8*e9639606Spooka  * are met:
9*e9639606Spooka  * 1. Redistributions of source code must retain the above copyright
10*e9639606Spooka  *    notice, this list of conditions and the following disclaimer.
11*e9639606Spooka  * 2. Redistributions in binary form must reproduce the above copyright
12*e9639606Spooka  *    notice, this list of conditions and the following disclaimer in the
13*e9639606Spooka  *    documentation and/or other materials provided with the distribution.
14*e9639606Spooka  *
15*e9639606Spooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*e9639606Spooka  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*e9639606Spooka  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*e9639606Spooka  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*e9639606Spooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*e9639606Spooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*e9639606Spooka  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*e9639606Spooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*e9639606Spooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*e9639606Spooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*e9639606Spooka  * SUCH DAMAGE.
26*e9639606Spooka  */
27*e9639606Spooka 
28*e9639606Spooka #ifndef INTERFACE_H
29*e9639606Spooka #define INTERFACE_H
30*e9639606Spooka 
31*e9639606Spooka #include <sys/types.h>
32*e9639606Spooka #include <sys/param.h>
33*e9639606Spooka #include <sys/socket.h>
34*e9639606Spooka 
35*e9639606Spooka #include <net/if.h>
36*e9639606Spooka #include <netinet/in.h>
37*e9639606Spooka #include <netinet/if_ether.h>
38*e9639606Spooka 
39*e9639606Spooka #include <limits.h>
40*e9639606Spooka 
41*e9639606Spooka #include "dhcp.h"
42*e9639606Spooka #include "dhcpcd.h"
43*e9639606Spooka 
44*e9639606Spooka #ifndef DUID_LEN
45*e9639606Spooka #  define DUID_LEN			128 + 2
46*e9639606Spooka #endif
47*e9639606Spooka 
48*e9639606Spooka #define EUI64_ADDR_LEN			8
49*e9639606Spooka #define INFINIBAND_ADDR_LEN		20
50*e9639606Spooka 
51*e9639606Spooka /* Linux 2.4 doesn't define this */
52*e9639606Spooka #ifndef ARPHRD_IEEE1394
53*e9639606Spooka #  define ARPHRD_IEEE1394		24
54*e9639606Spooka #endif
55*e9639606Spooka 
56*e9639606Spooka /* The BSD's don't define this yet */
57*e9639606Spooka #ifndef ARPHRD_INFINIBAND
58*e9639606Spooka #  define ARPHRD_INFINIBAND		32
59*e9639606Spooka #endif
60*e9639606Spooka 
61*e9639606Spooka 
62*e9639606Spooka /* Work out if we have a private address or not
63*e9639606Spooka  * 10/8
64*e9639606Spooka  * 172.16/12
65*e9639606Spooka  * 192.168/16
66*e9639606Spooka  */
67*e9639606Spooka #ifndef IN_PRIVATE
68*e9639606Spooka # define IN_PRIVATE(addr) (((addr & IN_CLASSA_NET) == 0x0a000000) ||	      \
69*e9639606Spooka 	    ((addr & 0xfff00000)    == 0xac100000) ||			      \
70*e9639606Spooka 	    ((addr & IN_CLASSB_NET) == 0xc0a80000))
71*e9639606Spooka #endif
72*e9639606Spooka 
73*e9639606Spooka #define LINKLOCAL_ADDR	0xa9fe0000
74*e9639606Spooka #define LINKLOCAL_MASK	IN_CLASSB_NET
75*e9639606Spooka #define LINKLOCAL_BRDC	(LINKLOCAL_ADDR | ~LINKLOCAL_MASK)
76*e9639606Spooka 
77*e9639606Spooka #ifndef IN_LINKLOCAL
78*e9639606Spooka # define IN_LINKLOCAL(addr) ((addr & IN_CLASSB_NET) == LINKLOCAL_ADDR)
79*e9639606Spooka #endif
80*e9639606Spooka 
81*e9639606Spooka struct rt {
82*e9639606Spooka 	struct in_addr dest;
83*e9639606Spooka 	struct in_addr net;
84*e9639606Spooka 	struct in_addr gate;
85*e9639606Spooka 	const struct interface *iface;
86*e9639606Spooka 	struct rt *next;
87*e9639606Spooka };
88*e9639606Spooka 
89*e9639606Spooka extern int socket_afnet;
90*e9639606Spooka 
91*e9639606Spooka uint32_t get_netmask(uint32_t);
92*e9639606Spooka char *hwaddr_ntoa(const unsigned char *, size_t);
93*e9639606Spooka size_t hwaddr_aton(unsigned char *, const char *);
94*e9639606Spooka 
95*e9639606Spooka int getifssid(const char *, char *);
96*e9639606Spooka struct interface *init_interface(const char *);
97*e9639606Spooka struct interface *discover_interfaces(int, char * const *);
98*e9639606Spooka void free_interface(struct interface *);
99*e9639606Spooka int do_mtu(const char *, short int);
100*e9639606Spooka #define get_mtu(iface) do_mtu(iface, 0)
101*e9639606Spooka #define set_mtu(iface, mtu) do_mtu(iface, mtu)
102*e9639606Spooka 
103*e9639606Spooka int inet_ntocidr(struct in_addr);
104*e9639606Spooka int inet_cidrtoaddr(int, struct in_addr *);
105*e9639606Spooka 
106*e9639606Spooka int up_interface(struct interface *);
107*e9639606Spooka int if_conf(struct interface *);
108*e9639606Spooka int if_init(struct interface *);
109*e9639606Spooka 
110*e9639606Spooka int do_address(const char *,
111*e9639606Spooka     struct in_addr *, struct in_addr *, struct in_addr *, int);
112*e9639606Spooka int if_address(const struct interface *,
113*e9639606Spooka     const struct in_addr *, const struct in_addr *,
114*e9639606Spooka     const struct in_addr *, int);
115*e9639606Spooka #define add_address(iface, addr, net, brd)				      \
116*e9639606Spooka 	if_address(iface, addr, net, brd, 1)
117*e9639606Spooka #define del_address(iface, addr, net)					      \
118*e9639606Spooka 	if_address(iface, addr, net, NULL, -1)
119*e9639606Spooka #define has_address(iface, addr, net)					      \
120*e9639606Spooka 	do_address(iface, addr, net, NULL, 0)
121*e9639606Spooka #define get_address(iface, addr, net, dst)				      \
122*e9639606Spooka 	do_address(iface, addr, net, dst, 1)
123*e9639606Spooka 
124*e9639606Spooka int if_route(const struct interface *, const struct in_addr *,
125*e9639606Spooka     const struct in_addr *, const struct in_addr *, int, int);
126*e9639606Spooka #define add_route(iface, dest, mask, gate, metric)			      \
127*e9639606Spooka 	if_route(iface, dest, mask, gate, metric, 1)
128*e9639606Spooka #define change_route(iface, dest, mask, gate, metric)			      \
129*e9639606Spooka 	if_route(iface, dest, mask, gate, metric, 0)
130*e9639606Spooka #define del_route(iface, dest, mask, gate, metric)			      \
131*e9639606Spooka 	if_route(iface, dest, mask, gate, metric, -1)
132*e9639606Spooka #define del_src_route(iface, dest, mask, gate, metric)			      \
133*e9639606Spooka 	if_route(iface, dest, mask, gate, metric, -2)
134*e9639606Spooka void free_routes(struct rt *);
135*e9639606Spooka 
136*e9639606Spooka int open_udp_socket(struct interface *);
137*e9639606Spooka extern const size_t udp_dhcp_len;
138*e9639606Spooka ssize_t make_udp_packet(uint8_t **, const uint8_t *, size_t,
139*e9639606Spooka     struct in_addr, struct in_addr);
140*e9639606Spooka ssize_t get_udp_data(const uint8_t **, const uint8_t *);
141*e9639606Spooka int valid_udp_packet(const uint8_t *, size_t, struct in_addr *);
142*e9639606Spooka 
143*e9639606Spooka int open_socket(struct interface *, int);
144*e9639606Spooka ssize_t send_packet(const struct interface *, struct in_addr,
145*e9639606Spooka     const uint8_t *, ssize_t);
146*e9639606Spooka ssize_t send_raw_packet(const struct interface *, int,
147*e9639606Spooka     const void *, ssize_t);
148*e9639606Spooka ssize_t get_raw_packet(struct interface *, int, void *, ssize_t);
149*e9639606Spooka 
150*e9639606Spooka int init_sockets(void);
151*e9639606Spooka int open_link_socket(void);
152*e9639606Spooka int manage_link(int);
153*e9639606Spooka int carrier_status(struct interface *);
154*e9639606Spooka 
155*e9639606Spooka int get_hwaddr(struct interface *);
156*e9639606Spooka #endif
157