1/* SPDX-License-Identifier: BSD-2-Clause */
2/*
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2023 Roy Marples <roy@marples.name>
5 * All rights reserved
6
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#ifndef INTERFACE_H
30#define INTERFACE_H
31
32#include <net/if.h>
33#include <net/route.h>		/* for RTM_ADD et all */
34#include <netinet/in.h>
35#ifdef BSD
36#include <netinet/in_var.h>	/* for IN_IFF_TENTATIVE et all */
37#endif
38
39#include <ifaddrs.h>
40
41/* If the interface does not support carrier status (ie PPP),
42 * dhcpcd can poll it for the relevant flags periodically */
43#define IF_POLL_UP	100	/* milliseconds */
44
45/*
46 * Systems which handle 1 address per alias.
47 * Currenly this is just Solaris.
48 * While Linux can do aliased addresses, it is only useful for their
49 * legacy ifconfig(8) tool which cannot display >1 IPv4 address
50 * (it can display many IPv6 addresses which makes the limitation odd).
51 * Linux has ip(8) which is a more feature rich tool, without the above
52 * restriction.
53 */
54#ifndef ALIAS_ADDR
55#  ifdef __sun
56#    define ALIAS_ADDR
57#  endif
58#endif
59
60#include "config.h"
61
62/* POSIX defines ioctl request as an int, which Solaris and musl use.
63 * Everyone else use an unsigned long, which happens to be the bigger one
64 * so we use that in our on wire API. */
65#ifdef IOCTL_REQUEST_TYPE
66typedef IOCTL_REQUEST_TYPE	ioctl_request_t;
67#else
68typedef unsigned long		ioctl_request_t;
69#endif
70
71#include "dhcpcd.h"
72#include "ipv4.h"
73#include "ipv6.h"
74#include "route.h"
75
76#define EUI64_ADDR_LEN			8
77#define INFINIBAND_ADDR_LEN		20
78
79/* Linux 2.4 doesn't define this */
80#ifndef ARPHRD_IEEE1394
81#  define ARPHRD_IEEE1394		24
82#endif
83
84/* The BSD's don't define this yet */
85#ifndef ARPHRD_INFINIBAND
86#  define ARPHRD_INFINIBAND		32
87#endif
88
89/* Maximum frame length.
90 * Support jumbo frames and some extra. */
91#define	FRAMEHDRLEN_MAX			14	/* only ethernet support */
92#define	FRAMELEN_MAX			(FRAMEHDRLEN_MAX + 9216)
93
94#define UDPLEN_MAX			64 * 1024
95
96/* Work out if we have a private address or not
97 * 10/8
98 * 172.16/12
99 * 192.168/16
100 */
101#ifndef IN_PRIVATE
102# define IN_PRIVATE(addr) (((addr & IN_CLASSA_NET) == 0x0a000000) ||	      \
103	    ((addr & 0xfff00000)    == 0xac100000) ||			      \
104	    ((addr & IN_CLASSB_NET) == 0xc0a80000))
105#endif
106
107#ifndef CLLADDR
108#ifdef AF_LINK
109#  define CLLADDR(sdl) (const void *)((sdl)->sdl_data + (sdl)->sdl_nlen)
110#endif
111#endif
112
113#ifdef __sun
114/* Solaris stupidly defines this for compat with BSD
115 * but then ignores it. */
116#undef RTF_CLONING
117
118/* This interface is busted on DilOS at least.
119 * It used to work, but lukily Solaris can fall back to
120 * IP_PKTINFO. */
121#undef IP_RECVIF
122#endif
123
124/* Private structures specific to an OS */
125#ifdef BSD
126struct priv {
127#ifdef INET6
128	int pf_inet6_fd;
129#endif
130#if defined(SIOCALIFADDR) && defined(IFLR_ACTIVE) /*NetBSD */
131	int pf_link_fd;
132#endif
133};
134#endif
135#ifdef __linux__
136struct priv {
137	int route_fd;
138	int generic_fd;
139	uint32_t route_pid;
140};
141#endif
142#ifdef __sun
143struct priv {
144#ifdef INET6
145	int pf_inet6_fd;
146#endif
147};
148#endif
149
150#ifdef __sun
151/* Solaris getifaddrs is very un-suitable for dhcpcd.
152 * See if-sun.c for details why. */
153struct ifaddrs;
154int if_getifaddrs(struct ifaddrs **);
155#define	getifaddrs	if_getifaddrs
156int if_getsubnet(struct dhcpcd_ctx *, const char *, int, void *, size_t);
157#endif
158
159int if_ioctl(struct dhcpcd_ctx *, ioctl_request_t, void *, size_t);
160#ifdef HAVE_PLEDGE
161#define	pioctl(ctx, req, data, len) if_ioctl((ctx), (req), (data), (len))
162#else
163#define	pioctl(ctx, req, data, len) ioctl((ctx)->pf_inet_fd, (req),(data),(len))
164#endif
165int if_getflags(struct interface *);
166int if_setflag(struct interface *, short, short);
167#define if_up(ifp) if_setflag((ifp), (IFF_UP | IFF_RUNNING), 0)
168#define if_down(ifp) if_setflag((ifp), 0, IFF_UP);
169bool if_is_link_up(const struct interface *);
170bool if_valid_hwaddr(const uint8_t *, size_t);
171struct if_head *if_discover(struct dhcpcd_ctx *, struct ifaddrs **,
172    int, char * const *);
173void if_freeifaddrs(struct dhcpcd_ctx *ctx, struct ifaddrs **);
174void if_markaddrsstale(struct if_head *);
175void if_learnaddrs(struct dhcpcd_ctx *, struct if_head *, struct ifaddrs **);
176void if_deletestaleaddrs(struct if_head *);
177struct interface *if_find(struct if_head *, const char *);
178struct interface *if_findindex(struct if_head *, unsigned int);
179struct interface *if_loopback(struct dhcpcd_ctx *);
180void if_free(struct interface *);
181int if_domtu(const struct interface *, short int);
182#define if_getmtu(ifp) if_domtu((ifp), 0)
183#define if_setmtu(ifp, mtu) if_domtu((ifp), (mtu))
184int if_carrier(struct interface *, const void *);
185bool if_roaming(struct interface *);
186
187#ifdef ALIAS_ADDR
188int if_makealias(char *, size_t, const char *, int);
189#endif
190
191int if_mtu_os(const struct interface *);
192
193/*
194 * Helper to decode an interface name of bge0:1 to
195 * devname = bge0, drvname = bge0, ppa = 0, lun = 1.
196 * If ppa or lun are invalid they are set to -1.
197 */
198struct if_spec {
199	char ifname[IF_NAMESIZE];
200	char devname[IF_NAMESIZE];
201	char drvname[IF_NAMESIZE];
202	int ppa;
203	int vlid;
204	int lun;
205};
206int if_nametospec(const char *, struct if_spec *);
207
208/* The below functions are provided by if-KERNEL.c */
209int os_init(void);
210int if_conf(struct interface *);
211int if_init(struct interface *);
212int if_getssid(struct interface *);
213int if_ignoregroup(int, const char *);
214bool if_ignore(struct dhcpcd_ctx *, const char *);
215int if_vimaster(struct dhcpcd_ctx *ctx, const char *);
216unsigned short if_vlanid(const struct interface *);
217char * if_getnetworknamespace(char *, size_t);
218int if_opensockets(struct dhcpcd_ctx *);
219int if_opensockets_os(struct dhcpcd_ctx *);
220void if_closesockets(struct dhcpcd_ctx *);
221void if_closesockets_os(struct dhcpcd_ctx *);
222int if_handlelink(struct dhcpcd_ctx *);
223int if_randomisemac(struct interface *);
224int if_setmac(struct interface *ifp, void *, uint8_t);
225
226/* dhcpcd uses the same routing flags as BSD.
227 * If the platform doesn't use these flags,
228 * map them in the platform interace file. */
229#ifndef RTM_ADD
230#define RTM_ADD		0x1	/* Add Route */
231#define RTM_DELETE	0x2	/* Delete Route */
232#define RTM_CHANGE	0x3	/* Change Metrics or flags */
233#define RTM_GET		0x4	/* Report Metrics */
234#endif
235
236/* Define SOCK_CLOEXEC and SOCK_NONBLOCK for systems that lack it.
237 * xsocket() in if.c will map them to fctnl FD_CLOEXEC and O_NONBLOCK. */
238#ifdef SOCK_CLOEXEC
239# define HAVE_SOCK_CLOEXEC
240#else
241# define SOCK_CLOEXEC	0x10000000
242#endif
243#ifdef SOCK_NONBLOCK
244# define HAVE_SOCK_NONBLOCK
245#else
246# define SOCK_NONBLOCK	0x20000000
247#endif
248#ifndef SOCK_CXNB
249#define	SOCK_CXNB	SOCK_CLOEXEC | SOCK_NONBLOCK
250#endif
251int xsocket(int, int, int);
252int xsocketpair(int, int, int, int[2]);
253
254int if_route(unsigned char, const struct rt *rt);
255int if_initrt(struct dhcpcd_ctx *, rb_tree_t *, int);
256
257int if_missfilter(struct interface *, struct sockaddr *);
258int if_missfilter_apply(struct dhcpcd_ctx *);
259
260#ifdef INET
261int if_address(unsigned char, const struct ipv4_addr *);
262int if_addrflags(const struct interface *, const struct in_addr *,
263    const char *);
264
265#endif
266
267#ifdef INET6
268void if_disable_rtadv(void);
269void if_setup_inet6(const struct interface *);
270int ip6_forwarding(const char *ifname);
271
272struct ra;
273struct ipv6_addr;
274
275int if_applyra(const struct ra *);
276int if_address6(unsigned char, const struct ipv6_addr *);
277int if_addrflags6(const struct interface *, const struct in6_addr *,
278    const char *);
279int if_getlifetime6(struct ipv6_addr *);
280
281#else
282#define if_checkipv6(a, b, c) (-1)
283#endif
284
285int if_machinearch(char *, size_t);
286struct interface *if_findifpfromcmsg(struct dhcpcd_ctx *,
287    struct msghdr *, int *);
288
289#ifdef __linux__
290int if_linksocket(struct sockaddr_nl *, int, int);
291int if_getnetlink(struct dhcpcd_ctx *, struct iovec *, int, int,
292    int (*)(struct dhcpcd_ctx *, void *, struct nlmsghdr *), void *);
293#endif
294#endif
295