1 /* 2 * Copyright (c) 2009, The Android Open Source Project 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in 12 * the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google, Inc. nor the names of its contributors 15 * may be used to endorse or promote products derived from this 16 * software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <stdio.h> 33 #include <string.h> 34 #include <errno.h> 35 #include <sys/ioctl.h> 36 #include <sys/types.h> 37 #include <sys/socket.h> 38 #include <netinet/in.h> 39 #include <arpa/inet.h> 40 #include <linux/route.h> 41 42 static inline int set_address(const char *address, struct sockaddr *sa) { 43 return inet_aton(address, &((struct sockaddr_in *)sa)->sin_addr); 44 } 45 46 /* current support the following routing entries */ 47 /* route add default dev wlan0 */ 48 /* route add default gw 192.168.1.1 dev wlan0 */ 49 /* route add -net 192.168.1.2 netmask 255.255.255.0 gw 192.168.1.1 */ 50 51 int route_main(int argc, char *argv[]) 52 { 53 struct rtentry rt = { 54 .rt_dst = {.sa_family = AF_INET}, 55 .rt_genmask = {.sa_family = AF_INET}, 56 .rt_gateway = {.sa_family = AF_INET}, 57 }; 58 59 errno = EINVAL; 60 if (argc > 2 && !strcmp(argv[1], "add")) { 61 if (!strcmp(argv[2], "default")) { 62 /* route add default dev wlan0 */ 63 if (argc > 4 && !strcmp(argv[3], "dev")) { 64 rt.rt_flags = RTF_UP; 65 rt.rt_dev = argv[4]; 66 errno = 0; 67 goto apply; 68 } 69 70 /* route add default gw 192.168.1.1 dev wlan0 */ 71 if (argc > 6 && !strcmp(argv[3], "gw") && !strcmp(argv[5], "dev")) { 72 rt.rt_flags = RTF_UP | RTF_GATEWAY; 73 rt.rt_dev = argv[6]; 74 if (set_address(argv[4], &rt.rt_gateway)) { 75 errno = 0; 76 } 77 goto apply; 78 } 79 } 80 81 /* route add -net 192.168.1.2 netmask 255.255.255.0 gw 192.168.1.1 */ 82 if (argc > 7 && !strcmp(argv[2], "-net") && 83 !strcmp(argv[4], "netmask")) { 84 if (!strcmp(argv[6], "gw")) { 85 rt.rt_flags = RTF_UP | RTF_GATEWAY; 86 if (set_address(argv[3], &rt.rt_dst) && 87 set_address(argv[5], &rt.rt_genmask) && 88 set_address(argv[7], &rt.rt_gateway)) { 89 errno = 0; 90 } 91 goto apply; 92 } else if (!strcmp(argv[6], "dev")) { 93 rt.rt_flags = RTF_UP; 94 rt.rt_dev = argv[7]; 95 if (set_address(argv[3], &rt.rt_dst) && 96 set_address(argv[5], &rt.rt_genmask)) { 97 errno = 0; 98 } 99 goto apply; 100 } 101 } 102 } 103 104 apply: 105 if (!errno) { 106 int s = socket(AF_INET, SOCK_DGRAM, 0); 107 if (s != -1 && (ioctl(s, SIOCADDRT, &rt) != -1 || errno == EEXIST)) { 108 return 0; 109 } 110 } 111 puts(strerror(errno)); 112 return errno; 113 } 114