Home | History | Annotate | Line # | Download | only in virtual_ip_router
rumprouter.c revision 1.2
      1  1.2  pooka /*	$NetBSD: rumprouter.c,v 1.2 2010/03/29 11:01:16 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*
      4  1.1  pooka  * Copyright (c) 2008 Antti Kantee.  All Rights Reserved.
      5  1.1  pooka  *
      6  1.2  pooka  * Development of this software was supported by the
      7  1.1  pooka  * Finnish Cultural Foundation.
      8  1.1  pooka  *
      9  1.1  pooka  * Redistribution and use in source and binary forms, with or without
     10  1.1  pooka  * modification, are permitted provided that the following conditions
     11  1.1  pooka  * are met:
     12  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     13  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     14  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     17  1.1  pooka  *
     18  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     19  1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.1  pooka  * SUCH DAMAGE.
     29  1.1  pooka  */
     30  1.1  pooka 
     31  1.1  pooka #include <sys/types.h>
     32  1.1  pooka #include <sys/socket.h>
     33  1.1  pooka #include <sys/time.h>
     34  1.1  pooka 
     35  1.1  pooka #include <arpa/inet.h>
     36  1.1  pooka 
     37  1.1  pooka #include <netinet/in.h>
     38  1.1  pooka #include <net/route.h>
     39  1.1  pooka 
     40  1.1  pooka #include <rump/rump.h>
     41  1.1  pooka #include <rump/rump_syscalls.h>
     42  1.1  pooka 
     43  1.1  pooka #include <err.h>
     44  1.1  pooka #include <errno.h>
     45  1.1  pooka #include <stdio.h>
     46  1.1  pooka #include <stdlib.h>
     47  1.1  pooka #include <string.h>
     48  1.1  pooka #include <unistd.h>
     49  1.1  pooka 
     50  1.1  pooka #include <sys/sockio.h>
     51  1.1  pooka #include <arpa/inet.h>
     52  1.1  pooka #include <net/if.h>
     53  1.1  pooka #include <net/route.h>
     54  1.1  pooka 
     55  1.1  pooka #undef DEBUG
     56  1.1  pooka 
     57  1.1  pooka #ifdef DEBUG
     58  1.1  pooka #define DP	if (1) printf
     59  1.1  pooka #else
     60  1.1  pooka #define DP	if (0) printf
     61  1.1  pooka #endif
     62  1.1  pooka 
     63  1.1  pooka static void
     64  1.1  pooka configure_interface(const char *ifname, const char *addr, const char *mask,
     65  1.1  pooka 	const char *bcast)
     66  1.1  pooka {
     67  1.1  pooka 	struct ifaliasreq ia;
     68  1.1  pooka 	struct sockaddr_in *sin;
     69  1.1  pooka 	int s, rv;
     70  1.1  pooka 
     71  1.1  pooka 	DP("Entering %s\n", __FUNCTION__);
     72  1.1  pooka 
     73  1.1  pooka 	DP("Create an interface(%s)\n", ifname);
     74  1.1  pooka 	s = atoi(ifname + strlen(ifname) - 1);		/* XXX FIXME XXX */
     75  1.1  pooka 	if ((s = rump_pub_virtif_create(s)) != 0) {
     76  1.1  pooka 		err(1, "rump_pub_virtif_create(%d)", s);
     77  1.1  pooka 	}
     78  1.1  pooka 
     79  1.1  pooka 	DP("Get a socket for configuring the interface\n");
     80  1.1  pooka 	if ((s = rump_sys_socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
     81  1.1  pooka 		err(1, "rump_sys_socket");
     82  1.1  pooka 	}
     83  1.1  pooka 
     84  1.1  pooka 	/* Address */
     85  1.1  pooka 	memset(&ia, 0, sizeof(ia));
     86  1.1  pooka 	strcpy(ia.ifra_name, ifname);
     87  1.1  pooka 	sin = (struct sockaddr_in *)&ia.ifra_addr;
     88  1.1  pooka 	sin->sin_family = AF_INET;
     89  1.1  pooka 	sin->sin_len = sizeof(struct sockaddr_in);
     90  1.1  pooka 	sin->sin_addr.s_addr = inet_addr(addr);
     91  1.1  pooka 
     92  1.1  pooka 	/* Netmask */
     93  1.1  pooka 	sin = (struct sockaddr_in *)&ia.ifra_mask;
     94  1.1  pooka 	sin->sin_family = AF_INET;
     95  1.1  pooka 	sin->sin_len = sizeof(struct sockaddr_in);
     96  1.1  pooka 	sin->sin_addr.s_addr = inet_addr(mask);
     97  1.1  pooka 
     98  1.1  pooka 	/* Broadcast address */
     99  1.1  pooka 	sin = (struct sockaddr_in *)&ia.ifra_broadaddr;
    100  1.1  pooka 	sin->sin_family = AF_INET;
    101  1.1  pooka 	sin->sin_len = sizeof(struct sockaddr_in);
    102  1.1  pooka 	sin->sin_addr.s_addr = inet_addr(bcast);
    103  1.1  pooka 
    104  1.1  pooka 	DP("Set the addresses\n");
    105  1.1  pooka 	rv = rump_sys_ioctl(s, SIOCAIFADDR, &ia);
    106  1.1  pooka 	if (rv) {
    107  1.1  pooka 		err(1, "SIOCAIFADDR");
    108  1.1  pooka 	}
    109  1.1  pooka 	rump_sys_close(s);
    110  1.1  pooka 	DP("Done with %s\n", __FUNCTION__);
    111  1.1  pooka }
    112  1.1  pooka 
    113  1.1  pooka static void
    114  1.1  pooka configure_routing(const char *dst, const char *mask, const char *gw)
    115  1.1  pooka {
    116  1.1  pooka 	size_t len;
    117  1.1  pooka 	struct {
    118  1.1  pooka 		struct rt_msghdr m_rtm;
    119  1.1  pooka 		uint8_t m_space;
    120  1.1  pooka 	} m_rtmsg;
    121  1.1  pooka #define rtm m_rtmsg.m_rtm
    122  1.1  pooka 	uint8_t *bp = &m_rtmsg.m_space;
    123  1.1  pooka 	struct sockaddr_in sinstore;
    124  1.1  pooka 	int s, rv;
    125  1.1  pooka 
    126  1.1  pooka 	DP("Entering %s\n", __FUNCTION__);
    127  1.1  pooka 
    128  1.1  pooka 	DP("Open a routing socket\n");
    129  1.1  pooka 	s = rump_sys_socket(PF_INET, SOCK_DGRAM, 0);
    130  1.1  pooka 	if (s == -1) {
    131  1.1  pooka 		err(1, "rump_sys_socket");
    132  1.1  pooka 	}
    133  1.1  pooka 
    134  1.1  pooka 	memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    135  1.1  pooka 	rtm.rtm_type = RTM_ADD;
    136  1.1  pooka 	rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
    137  1.1  pooka 	rtm.rtm_version = RTM_VERSION;
    138  1.1  pooka 	rtm.rtm_seq = 2;
    139  1.1  pooka 	rtm.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK;
    140  1.1  pooka 
    141  1.1  pooka 	/* dst */
    142  1.1  pooka 	memset(&sinstore, 0, sizeof(sinstore));
    143  1.1  pooka 	sinstore.sin_family = AF_INET;
    144  1.1  pooka 	sinstore.sin_len = sizeof(sinstore);
    145  1.1  pooka 	sinstore.sin_addr.s_addr = inet_addr(dst);
    146  1.1  pooka 	memcpy(bp, &sinstore, sizeof(sinstore));
    147  1.1  pooka 	bp += sizeof(sinstore);
    148  1.1  pooka 
    149  1.1  pooka 	/* gw */
    150  1.1  pooka 	memset(&sinstore, 0, sizeof(sinstore));
    151  1.1  pooka 	sinstore.sin_family = AF_INET;
    152  1.1  pooka 	sinstore.sin_len = sizeof(sinstore);
    153  1.1  pooka 	sinstore.sin_addr.s_addr = inet_addr(gw);
    154  1.1  pooka 	memcpy(bp, &sinstore, sizeof(sinstore));
    155  1.1  pooka 	bp += sizeof(sinstore);
    156  1.1  pooka 
    157  1.1  pooka 	/* netmask */
    158  1.1  pooka 	memset(&sinstore, 0, sizeof(sinstore));
    159  1.1  pooka 	sinstore.sin_family = AF_INET;
    160  1.1  pooka 	sinstore.sin_len = sizeof(sinstore);
    161  1.1  pooka 	sinstore.sin_addr.s_addr = inet_addr(mask);
    162  1.1  pooka 	memcpy(bp, &sinstore, sizeof(sinstore));
    163  1.1  pooka 	bp += sizeof(sinstore);
    164  1.1  pooka 
    165  1.1  pooka 	len = bp - (uint8_t *)&m_rtmsg;
    166  1.1  pooka 	rtm.rtm_msglen = len;
    167  1.1  pooka 
    168  1.1  pooka 	DP("Set the route\n");
    169  1.1  pooka 	rv = rump_sys_write(s, &m_rtmsg, len);
    170  1.1  pooka 	if (rv != (int)len) {
    171  1.1  pooka 		err(1, "rump_sys_write");
    172  1.1  pooka 	}
    173  1.1  pooka 	rump_sys_close(s);
    174  1.1  pooka 	DP("Done with %s\n", __FUNCTION__);
    175  1.1  pooka }
    176  1.1  pooka 
    177  1.1  pooka static void
    178  1.1  pooka usage(const char *argv0)
    179  1.1  pooka {
    180  1.1  pooka 	printf("Usage: %s if1 if2 [route]\n", argv0);
    181  1.1  pooka 	printf("\n");
    182  1.1  pooka 	printf("where both \"if1\" and \"if2\" are\n");
    183  1.1  pooka 	printf("\n");
    184  1.1  pooka 	printf("ifname address netmask broadcast\n");
    185  1.1  pooka 	printf("\n");
    186  1.1  pooka 	printf("and \"route\" is an optional default route\n");
    187  1.1  pooka 	exit(1);
    188  1.1  pooka }
    189  1.1  pooka 
    190  1.1  pooka int
    191  1.1  pooka main(int argc, char *argv[])
    192  1.1  pooka {
    193  1.1  pooka 	if (argc < 9 || argc > 10) {
    194  1.1  pooka 		usage(argv[0]);
    195  1.1  pooka 	}
    196  1.1  pooka 
    197  1.1  pooka 	rump_init();
    198  1.1  pooka 	configure_interface(argv[1], argv[2], argv[3], argv[4]);
    199  1.1  pooka 	configure_interface(argv[5], argv[6], argv[7], argv[8]);
    200  1.1  pooka 	if (argc == 10) {
    201  1.1  pooka 		configure_routing("192.168.3.0", "255.255.255.0", "192.168.1.2");
    202  1.1  pooka 	}
    203  1.1  pooka 	printf("Press Ctrl+C to quit...");
    204  1.1  pooka 	pause();
    205  1.1  pooka 
    206  1.1  pooka 	return 0;
    207  1.1  pooka }
    208