Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * socket.c -- DHCP server client/server socket creation |
| 3 | * |
| 4 | * udhcp client/server |
| 5 | * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au> |
| 6 | * Chris Trew <ctrew@moreton.com.au> |
| 7 | * |
| 8 | * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 | */ |
| 24 | |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/socket.h> |
| 27 | #include <sys/ioctl.h> |
| 28 | #include <netinet/in.h> |
| 29 | #include <unistd.h> |
| 30 | #include <string.h> |
| 31 | #include <arpa/inet.h> |
| 32 | #include <net/if.h> |
| 33 | #include <errno.h> |
| 34 | #include <features.h> |
| 35 | #if __GLIBC__ >=2 && __GLIBC_MINOR >= 1 |
| 36 | #include <netpacket/packet.h> |
| 37 | #include <net/ethernet.h> |
| 38 | #else |
| 39 | #include <asm/types.h> |
| 40 | #include <linux/if_packet.h> |
| 41 | #include <linux/if_ether.h> |
| 42 | #endif |
| 43 | |
Russ Dill | 76729b8 | 2003-12-16 20:44:15 +0000 | [diff] [blame^] | 44 | #include "socket.h" |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 45 | #include "common.h" |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 46 | |
| 47 | int read_interface(char *interface, int *ifindex, u_int32_t *addr, unsigned char *arp) |
| 48 | { |
| 49 | int fd; |
| 50 | struct ifreq ifr; |
| 51 | struct sockaddr_in *our_ip; |
| 52 | |
| 53 | memset(&ifr, 0, sizeof(struct ifreq)); |
| 54 | if((fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) >= 0) { |
| 55 | ifr.ifr_addr.sa_family = AF_INET; |
| 56 | strcpy(ifr.ifr_name, interface); |
| 57 | |
| 58 | if (addr) { |
| 59 | if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) { |
| 60 | our_ip = (struct sockaddr_in *) &ifr.ifr_addr; |
| 61 | *addr = our_ip->sin_addr.s_addr; |
| 62 | DEBUG(LOG_INFO, "%s (our ip) = %s", ifr.ifr_name, inet_ntoa(our_ip->sin_addr)); |
| 63 | } else { |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 64 | LOG(LOG_ERR, "SIOCGIFADDR failed, is the interface up and configured?: %m"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 65 | return -1; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (ioctl(fd, SIOCGIFINDEX, &ifr) == 0) { |
| 70 | DEBUG(LOG_INFO, "adapter index %d", ifr.ifr_ifindex); |
| 71 | *ifindex = ifr.ifr_ifindex; |
| 72 | } else { |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 73 | LOG(LOG_ERR, "SIOCGIFINDEX failed!: %m"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 74 | return -1; |
| 75 | } |
| 76 | if (ioctl(fd, SIOCGIFHWADDR, &ifr) == 0) { |
| 77 | memcpy(arp, ifr.ifr_hwaddr.sa_data, 6); |
| 78 | DEBUG(LOG_INFO, "adapter hardware address %02x:%02x:%02x:%02x:%02x:%02x", |
| 79 | arp[0], arp[1], arp[2], arp[3], arp[4], arp[5]); |
| 80 | } else { |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 81 | LOG(LOG_ERR, "SIOCGIFHWADDR failed!: %m"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 82 | return -1; |
| 83 | } |
| 84 | } else { |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 85 | LOG(LOG_ERR, "socket failed!: %m"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 86 | return -1; |
| 87 | } |
| 88 | close(fd); |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | int listen_socket(unsigned int ip, int port, char *inf) |
| 94 | { |
| 95 | struct ifreq interface; |
| 96 | int fd; |
| 97 | struct sockaddr_in addr; |
| 98 | int n = 1; |
| 99 | |
Glenn L McGrath | 8ce8f9b | 2003-08-29 15:19:44 +0000 | [diff] [blame] | 100 | DEBUG(LOG_INFO, "Opening listen socket on 0x%08x:%d %s", ip, port, inf); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 101 | if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 102 | DEBUG(LOG_ERR, "socket call failed: %m"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | memset(&addr, 0, sizeof(addr)); |
| 107 | addr.sin_family = AF_INET; |
| 108 | addr.sin_port = htons(port); |
| 109 | addr.sin_addr.s_addr = ip; |
| 110 | |
| 111 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) { |
| 112 | close(fd); |
| 113 | return -1; |
| 114 | } |
| 115 | if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *) &n, sizeof(n)) == -1) { |
| 116 | close(fd); |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | strncpy(interface.ifr_ifrn.ifrn_name, inf, IFNAMSIZ); |
| 121 | if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,(char *)&interface, sizeof(interface)) < 0) { |
| 122 | close(fd); |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | if (bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) == -1) { |
| 127 | close(fd); |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | return fd; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | int raw_socket(int ifindex) |
| 136 | { |
| 137 | int fd; |
| 138 | struct sockaddr_ll sock; |
| 139 | |
Glenn L McGrath | 8ce8f9b | 2003-08-29 15:19:44 +0000 | [diff] [blame] | 140 | DEBUG(LOG_INFO, "Opening raw socket on ifindex %d", ifindex); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 141 | if ((fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP))) < 0) { |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 142 | DEBUG(LOG_ERR, "socket call failed: %m"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 143 | return -1; |
| 144 | } |
Eric Andersen | c16686c | 2003-11-14 03:00:04 +0000 | [diff] [blame] | 145 | |
| 146 | while (fd >= 0 && fd < 3) fd = dup(fd); /* don't let daemon close fds on us */ |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 147 | |
| 148 | sock.sll_family = AF_PACKET; |
| 149 | sock.sll_protocol = htons(ETH_P_IP); |
| 150 | sock.sll_ifindex = ifindex; |
| 151 | if (bind(fd, (struct sockaddr *) &sock, sizeof(sock)) < 0) { |
Glenn L McGrath | 2483343 | 2003-06-10 17:22:49 +0000 | [diff] [blame] | 152 | DEBUG(LOG_ERR, "bind call failed: %m"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 153 | close(fd); |
| 154 | return -1; |
| 155 | } |
| 156 | |
| 157 | return fd; |
| 158 | } |