Home | History | Annotate | Line # | Download | only in rump_dhcpclient
if-bsd.c revision 1.2
      1  1.1  pooka /*
      2  1.1  pooka  * dhcpcd - DHCP client daemon
      3  1.1  pooka  * Copyright (c) 2006-2010 Roy Marples <roy (at) marples.name>
      4  1.1  pooka  * All rights reserved
      5  1.1  pooka 
      6  1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7  1.1  pooka  * modification, are permitted provided that the following conditions
      8  1.1  pooka  * are met:
      9  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14  1.1  pooka  *
     15  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16  1.1  pooka  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  1.1  pooka  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  1.1  pooka  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  1.1  pooka  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  pooka  * SUCH DAMAGE.
     26  1.1  pooka  */
     27  1.1  pooka 
     28  1.1  pooka #include <sys/ioctl.h>
     29  1.1  pooka #include <sys/param.h>
     30  1.1  pooka #include <sys/socket.h>
     31  1.1  pooka #include <sys/stat.h>
     32  1.1  pooka #include <sys/sysctl.h>
     33  1.1  pooka #include <sys/types.h>
     34  1.1  pooka 
     35  1.1  pooka #include <arpa/inet.h>
     36  1.1  pooka #include <net/if.h>
     37  1.1  pooka #include <net/if_dl.h>
     38  1.1  pooka #include <net/route.h>
     39  1.1  pooka #include <netinet/in.h>
     40  1.1  pooka #ifdef __DragonFly__
     41  1.1  pooka #  include <netproto/802_11/ieee80211_ioctl.h>
     42  1.1  pooka #elif __APPLE__
     43  1.1  pooka   /* FIXME: Add apple includes so we can work out SSID */
     44  1.1  pooka #else
     45  1.1  pooka #  include <net80211/ieee80211_ioctl.h>
     46  1.1  pooka #endif
     47  1.1  pooka 
     48  1.1  pooka #include <errno.h>
     49  1.1  pooka #include <fnmatch.h>
     50  1.1  pooka #include <stddef.h>
     51  1.1  pooka #include <stdio.h>
     52  1.1  pooka #include <stdlib.h>
     53  1.1  pooka #include <string.h>
     54  1.1  pooka #include <unistd.h>
     55  1.1  pooka 
     56  1.1  pooka #include <rump/rump.h>
     57  1.1  pooka #include <rump/rump_syscalls.h>
     58  1.1  pooka 
     59  1.1  pooka #include "common.h"
     60  1.1  pooka #include "configure.h"
     61  1.1  pooka #include "dhcp.h"
     62  1.1  pooka #include "if-options.h"
     63  1.1  pooka #include "net.h"
     64  1.1  pooka 
     65  1.1  pooka /* FIXME: Why do we need to check for sa_family 255 */
     66  1.1  pooka #define COPYOUT(sin, sa)						      \
     67  1.1  pooka 	sin.s_addr = ((sa) != NULL) ?					      \
     68  1.1  pooka 	    (((struct sockaddr_in *)(void *)sa)->sin_addr).s_addr : 0
     69  1.1  pooka 
     70  1.1  pooka static int r_fd = -1;
     71  1.1  pooka 
     72  1.1  pooka int
     73  1.1  pooka if_init(_unused struct interface *iface)
     74  1.1  pooka {
     75  1.1  pooka 	/* BSD promotes secondary address by default */
     76  1.1  pooka 	return 0;
     77  1.1  pooka }
     78  1.1  pooka 
     79  1.1  pooka int
     80  1.1  pooka if_conf(_unused struct interface *iface)
     81  1.1  pooka {
     82  1.1  pooka 	/* No extra checks needed on BSD */
     83  1.1  pooka 	return 0;
     84  1.1  pooka }
     85  1.1  pooka 
     86  1.1  pooka int
     87  1.1  pooka init_sockets(void)
     88  1.1  pooka {
     89  1.1  pooka 
     90  1.1  pooka 	if ((socket_afnet = rump_sys_socket(AF_INET, SOCK_DGRAM, 0)) == -1)
     91  1.1  pooka 		return -1;
     92  1.1  pooka 	if ((r_fd = rump_sys_socket(PF_ROUTE, SOCK_RAW, 0)) == -1)
     93  1.1  pooka 		return -1;
     94  1.1  pooka 	return 0;
     95  1.1  pooka }
     96  1.1  pooka 
     97  1.1  pooka int
     98  1.1  pooka getifssid(const char *ifname, char *ssid)
     99  1.1  pooka {
    100  1.1  pooka 	int retval = -1;
    101  1.1  pooka #if defined(SIOCG80211NWID)
    102  1.1  pooka 	struct ifreq ifr;
    103  1.1  pooka 	struct ieee80211_nwid nwid;
    104  1.1  pooka #elif defined(IEEE80211_IOC_SSID)
    105  1.1  pooka 	struct ieee80211req ireq;
    106  1.1  pooka 	char nwid[IEEE80211_NWID_LEN + 1];
    107  1.1  pooka #endif
    108  1.1  pooka 
    109  1.1  pooka #if defined(SIOCG80211NWID) /* NetBSD */
    110  1.1  pooka 	memset(&ifr, 0, sizeof(ifr));
    111  1.1  pooka 	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
    112  1.1  pooka 	memset(&nwid, 0, sizeof(nwid));
    113  1.1  pooka 	ifr.ifr_data = (void *)&nwid;
    114  1.1  pooka 	if (rump_sys_ioctl(socket_afnet, SIOCG80211NWID, &ifr) == 0) {
    115  1.1  pooka 		retval = nwid.i_len;
    116  1.1  pooka 		memcpy(ssid, nwid.i_nwid, nwid.i_len);
    117  1.1  pooka 		ssid[nwid.i_len] = '\0';
    118  1.1  pooka 	}
    119  1.1  pooka #elif defined(IEEE80211_IOC_SSID) /* FreeBSD */
    120  1.1  pooka 	memset(&ireq, 0, sizeof(ireq));
    121  1.1  pooka 	strlcpy(ireq.i_name, ifname, sizeof(ireq.i_name));
    122  1.1  pooka 	ireq.i_type = IEEE80211_IOC_SSID;
    123  1.1  pooka 	ireq.i_val = -1;
    124  1.1  pooka 	ireq.i_data = &nwid;
    125  1.1  pooka 	if (rump_sys_ioctl(socket_afnet, SIOCG80211, &ireq) == 0) {
    126  1.1  pooka 		retval = ireq.i_len;
    127  1.1  pooka 		memcpy(ssid, nwid, ireq.i_len);
    128  1.1  pooka 		ssid[ireq.i_len] = '\0';
    129  1.1  pooka 	}
    130  1.1  pooka #endif
    131  1.1  pooka 	return retval;
    132  1.1  pooka }
    133  1.1  pooka 
    134  1.1  pooka int
    135  1.1  pooka if_address(const struct interface *iface, const struct in_addr *address,
    136  1.1  pooka     const struct in_addr *netmask, const struct in_addr *broadcast,
    137  1.1  pooka     int action)
    138  1.1  pooka {
    139  1.1  pooka 	int retval;
    140  1.1  pooka 	struct ifaliasreq ifa;
    141  1.1  pooka 	union {
    142  1.1  pooka 		struct sockaddr *sa;
    143  1.1  pooka 		struct sockaddr_in *sin;
    144  1.1  pooka 	} _s;
    145  1.1  pooka 
    146  1.1  pooka 	memset(&ifa, 0, sizeof(ifa));
    147  1.1  pooka 	strlcpy(ifa.ifra_name, iface->name, sizeof(ifa.ifra_name));
    148  1.1  pooka 
    149  1.1  pooka #define ADDADDR(_var, _addr) {						      \
    150  1.1  pooka 		_s.sa = &_var;						      \
    151  1.1  pooka 		_s.sin->sin_family = AF_INET;				      \
    152  1.1  pooka 		_s.sin->sin_len = sizeof(*_s.sin);			      \
    153  1.1  pooka 		memcpy(&_s.sin->sin_addr, _addr, sizeof(_s.sin->sin_addr));   \
    154  1.1  pooka 	}
    155  1.1  pooka 
    156  1.1  pooka 	ADDADDR(ifa.ifra_addr, address);
    157  1.1  pooka 	ADDADDR(ifa.ifra_mask, netmask);
    158  1.1  pooka 	if (action >= 0 && broadcast) {
    159  1.1  pooka 		ADDADDR(ifa.ifra_broadaddr, broadcast);
    160  1.1  pooka 	}
    161  1.1  pooka #undef ADDADDR
    162  1.1  pooka 
    163  1.1  pooka 	if (action < 0)
    164  1.1  pooka 		retval = rump_sys_ioctl(socket_afnet, SIOCDIFADDR, &ifa);
    165  1.1  pooka 	else
    166  1.1  pooka 		retval = rump_sys_ioctl(socket_afnet, SIOCAIFADDR, &ifa);
    167  1.1  pooka 	return retval;
    168  1.1  pooka }
    169  1.1  pooka 
    170  1.1  pooka /* ARGSUSED4 */
    171  1.1  pooka int
    172  1.1  pooka if_route(const struct interface *iface, const struct in_addr *dest,
    173  1.1  pooka     const struct in_addr *net, const struct in_addr *gate,
    174  1.1  pooka     _unused int metric, int action)
    175  1.1  pooka {
    176  1.1  pooka 	union sockunion {
    177  1.1  pooka 		struct sockaddr sa;
    178  1.1  pooka 		struct sockaddr_in sin;
    179  1.1  pooka #ifdef INET6
    180  1.1  pooka 		struct sockaddr_in6 sin6;
    181  1.1  pooka #endif
    182  1.1  pooka 		struct sockaddr_dl sdl;
    183  1.1  pooka 		struct sockaddr_storage ss;
    184  1.1  pooka 	} su;
    185  1.1  pooka 	struct rtm
    186  1.1  pooka 	{
    187  1.1  pooka 		struct rt_msghdr hdr;
    188  1.1  pooka 		char buffer[sizeof(su) * 4];
    189  1.1  pooka 	} rtm;
    190  1.1  pooka 	char *bp = rtm.buffer, *p;
    191  1.1  pooka 	size_t l;
    192  1.1  pooka 	int retval = 0;
    193  1.1  pooka 
    194  1.1  pooka #define ADDSU(_su) {							      \
    195  1.2    roy 		l = RT_ROUNDUP(_su.sa.sa_len);				      \
    196  1.1  pooka 		memcpy(bp, &(_su), l);					      \
    197  1.1  pooka 		bp += l;						      \
    198  1.1  pooka 	}
    199  1.1  pooka #define ADDADDR(_a) {							      \
    200  1.1  pooka 		memset (&su, 0, sizeof(su));				      \
    201  1.1  pooka 		su.sin.sin_family = AF_INET;				      \
    202  1.1  pooka 		su.sin.sin_len = sizeof(su.sin);			      \
    203  1.1  pooka 		memcpy (&su.sin.sin_addr, _a, sizeof(su.sin.sin_addr));	      \
    204  1.1  pooka 		ADDSU(su);						      \
    205  1.1  pooka 	}
    206  1.1  pooka 
    207  1.1  pooka 	memset(&rtm, 0, sizeof(rtm));
    208  1.1  pooka 	rtm.hdr.rtm_version = RTM_VERSION;
    209  1.1  pooka 	rtm.hdr.rtm_seq = 1;
    210  1.1  pooka 	if (action == 0)
    211  1.1  pooka 		rtm.hdr.rtm_type = RTM_CHANGE;
    212  1.1  pooka 	else if (action > 0)
    213  1.1  pooka 		rtm.hdr.rtm_type = RTM_ADD;
    214  1.1  pooka 	else
    215  1.1  pooka 		rtm.hdr.rtm_type = RTM_DELETE;
    216  1.1  pooka 	rtm.hdr.rtm_flags = RTF_UP;
    217  1.1  pooka 	/* None interface subnet routes are static. */
    218  1.1  pooka 	if (gate->s_addr != INADDR_ANY ||
    219  1.1  pooka 	    net->s_addr != iface->net.s_addr ||
    220  1.1  pooka 	    dest->s_addr != (iface->addr.s_addr & iface->net.s_addr))
    221  1.1  pooka 		rtm.hdr.rtm_flags |= RTF_STATIC;
    222  1.1  pooka 	rtm.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY;
    223  1.1  pooka 	if (dest->s_addr == gate->s_addr && net->s_addr == INADDR_BROADCAST)
    224  1.1  pooka 		rtm.hdr.rtm_flags |= RTF_HOST;
    225  1.1  pooka 	else {
    226  1.1  pooka 		rtm.hdr.rtm_addrs |= RTA_NETMASK;
    227  1.1  pooka 		if (rtm.hdr.rtm_flags & RTF_STATIC)
    228  1.1  pooka 			rtm.hdr.rtm_flags |= RTF_GATEWAY;
    229  1.1  pooka 		if (action >= 0)
    230  1.1  pooka 			rtm.hdr.rtm_addrs |= RTA_IFA;
    231  1.1  pooka 	}
    232  1.1  pooka 
    233  1.1  pooka 	ADDADDR(dest);
    234  1.1  pooka 	if (rtm.hdr.rtm_flags & RTF_HOST ||
    235  1.1  pooka 	    !(rtm.hdr.rtm_flags & RTF_STATIC))
    236  1.1  pooka 	{
    237  1.1  pooka 		/* Make us a link layer socket for the host gateway */
    238  1.1  pooka 		memset(&su, 0, sizeof(su));
    239  1.1  pooka 		su.sdl.sdl_len = sizeof(struct sockaddr_dl);
    240  1.1  pooka 		link_addr(iface->name, &su.sdl);
    241  1.1  pooka 		ADDSU(su);
    242  1.1  pooka 	} else
    243  1.1  pooka 		ADDADDR(gate);
    244  1.1  pooka 
    245  1.1  pooka 	if (rtm.hdr.rtm_addrs & RTA_NETMASK) {
    246  1.1  pooka 		/* Ensure that netmask is set correctly */
    247  1.1  pooka 		memset(&su, 0, sizeof(su));
    248  1.1  pooka 		su.sin.sin_family = AF_INET;
    249  1.1  pooka 		su.sin.sin_len = sizeof(su.sin);
    250  1.1  pooka 		memcpy(&su.sin.sin_addr, &net->s_addr, sizeof(su.sin.sin_addr));
    251  1.1  pooka 		p = su.sa.sa_len + (char *)&su;
    252  1.1  pooka 		for (su.sa.sa_len = 0; p > (char *)&su;)
    253  1.1  pooka 			if (*--p != 0) {
    254  1.1  pooka 				su.sa.sa_len = 1 + p - (char *)&su;
    255  1.1  pooka 				break;
    256  1.1  pooka 			}
    257  1.1  pooka 		ADDSU(su);
    258  1.1  pooka 	}
    259  1.1  pooka 
    260  1.1  pooka 	if (rtm.hdr.rtm_addrs & RTA_IFA)
    261  1.1  pooka 		ADDADDR(&iface->addr);
    262  1.1  pooka 
    263  1.1  pooka 	rtm.hdr.rtm_msglen = l = bp - (char *)&rtm;
    264  1.1  pooka 	if (rump_sys_write(r_fd, &rtm, l) == -1)
    265  1.1  pooka 		retval = -1;
    266  1.1  pooka 	return retval;
    267  1.1  pooka }
    268