Home | History | Annotate | Download | only in dhcpcd
      1 /*
      2  * dhcpcd - DHCP client daemon
      3  * Copyright 2006-2008 Roy Marples <roy (at) marples.name>
      4  * All rights reserved
      5 
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #ifndef INTERFACE_H
     29 #define INTERFACE_H
     30 
     31 #include <sys/types.h>
     32 #include <sys/param.h>
     33 #include <sys/socket.h>
     34 
     35 #include <net/if.h>
     36 #include <netinet/in.h>
     37 #include <netinet/if_ether.h>
     38 
     39 #include <limits.h>
     40 
     41 #include "config.h"
     42 
     43 #ifndef DUID_LEN
     44 #  define DUID_LEN			128 + 2
     45 #endif
     46 
     47 #define EUI64_ADDR_LEN			8
     48 #define INFINIBAND_ADDR_LEN		20
     49 
     50 /* Linux 2.4 doesn't define this */
     51 #ifndef ARPHRD_IEEE1394
     52 #  define ARPHRD_IEEE1394		24
     53 #endif
     54 
     55 /* The BSD's don't define this yet */
     56 #ifndef ARPHRD_INFINIBAND
     57 #  define ARPHRD_INFINIBAND		32
     58 #endif
     59 
     60 #define HWADDR_LEN			20
     61 
     62 /* Work out if we have a private address or not
     63  * 10/8
     64  * 172.16/12
     65  * 192.168/16
     66  */
     67 #ifndef IN_PRIVATE
     68 # define IN_PRIVATE(addr) (((addr & IN_CLASSA_NET) == 0x0a000000) || \
     69 			   ((addr & 0xfff00000)    == 0xac100000) || \
     70 			   ((addr & IN_CLASSB_NET) == 0xc0a80000))
     71 #endif
     72 
     73 #define LINKLOCAL_ADDR	0xa9fe0000
     74 #define LINKLOCAL_MASK	IN_CLASSB_NET
     75 #define LINKLOCAL_BRDC	(LINKLOCAL_ADDR | ~LINKLOCAL_MASK)
     76 
     77 #ifndef IN_LINKLOCAL
     78 # define IN_LINKLOCAL(addr) ((addr & IN_CLASSB_NET) == LINKLOCAL_ADDR)
     79 #endif
     80 
     81 /* There is an argument that this should be converted to an STAIL using
     82  * queue(3). However, that isn't readily available on all libc's that
     83  * dhcpcd works on. The only benefit of STAILQ over this is the ability to
     84  * quickly loop backwards through the list - currently we reverse the list
     85  * and then move through it forwards. This isn't that much of a big deal
     86  * though as the norm is to just have one default route, and an IPV4LL route.
     87  * You can (and do) get more routes in the DHCP message, but not enough to
     88  * really warrant a change to STAIL queue for performance reasons. */
     89 struct rt {
     90 	struct in_addr dest;
     91 	struct in_addr net;
     92 	struct in_addr gate;
     93 	struct rt *next;
     94 };
     95 
     96 struct interface
     97 {
     98 	char name[IF_NAMESIZE];
     99 	sa_family_t family;
    100 	unsigned char hwaddr[HWADDR_LEN];
    101 	size_t hwlen;
    102 	int arpable;
    103 
    104 	int raw_fd;
    105 	int udp_fd;
    106 	int arp_fd;
    107 	int link_fd;
    108 	size_t buffer_size, buffer_len, buffer_pos;
    109 	unsigned char *buffer;
    110 
    111 	struct in_addr addr;
    112 	struct in_addr net;
    113 	struct rt *routes;
    114 
    115 	char leasefile[PATH_MAX];
    116 	time_t start_uptime;
    117 
    118 	unsigned char *clientid;
    119 };
    120 
    121 uint32_t get_netmask(uint32_t);
    122 char *hwaddr_ntoa(const unsigned char *, size_t);
    123 size_t hwaddr_aton(unsigned char *, const char *);
    124 
    125 struct interface *read_interface(const char *, int);
    126 int do_mtu(const char *, short int);
    127 #define get_mtu(iface) do_mtu(iface, 0)
    128 #define set_mtu(iface, mtu) do_mtu(iface, mtu)
    129 
    130 int inet_ntocidr(struct in_addr);
    131 int inet_cidrtoaddr(int, struct in_addr *);
    132 
    133 int up_interface(const char *);
    134 int do_interface(const char *, unsigned char *, size_t *,
    135 		 struct in_addr *, struct in_addr *, int);
    136 int if_address(const char *, const struct in_addr *, const struct in_addr *,
    137 	       const struct in_addr *, int);
    138 #define add_address(ifname, addr, net, brd) \
    139 	if_address(ifname, addr, net, brd, 1)
    140 #define del_address(ifname, addr, net) \
    141 	if_address(ifname, addr, net, NULL, -1)
    142 #define has_address(ifname, addr, net) \
    143 	do_interface(ifname, NULL, NULL, addr, net, 0)
    144 #define get_address(ifname, addr, net) \
    145 	do_interface(ifname, NULL, NULL, addr, net, 1)
    146 
    147 int if_route(const struct interface *,
    148 	     const struct in_addr *,const struct in_addr *,
    149 	     const struct in_addr *, int, int);
    150 #define add_route(iface, dest, mask, gate, metric) \
    151 	if_route(iface, dest, mask, gate, metric, 1)
    152 #define change_route(iface, dest, mask, gate, metric) \
    153 	if_route(iface, dest, mask, gate, metric, 0)
    154 #define del_route(iface, dest, mask, gate, metric) \
    155 	if_route(iface, dest, mask, gate, metric, -1)
    156 void free_routes(struct rt *);
    157 
    158 int open_udp_socket(struct interface *);
    159 const size_t udp_dhcp_len;
    160 ssize_t make_udp_packet(uint8_t **, const uint8_t *, size_t,
    161 			struct in_addr, struct in_addr);
    162 ssize_t get_udp_data(const uint8_t **, const uint8_t *);
    163 int valid_udp_packet(const uint8_t *, size_t);
    164 
    165 int open_socket(struct interface *, int);
    166 ssize_t send_packet(const struct interface *, struct in_addr,
    167 		    const uint8_t *, ssize_t);
    168 ssize_t send_raw_packet(const struct interface *, int,
    169 			const void *, ssize_t);
    170 ssize_t get_raw_packet(struct interface *, int, void *, ssize_t);
    171 
    172 int send_arp(const struct interface *, int, in_addr_t, in_addr_t);
    173 
    174 int open_link_socket(struct interface *);
    175 int link_changed(struct interface *);
    176 int carrier_status(const char *);
    177 #endif
    178