blob: 0ed7ad1c6866ced027061774bca49e6d088ab67b [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysinger7031f622006-05-08 03:20:50 +00002/*
Denys Vlasenko385b4562010-03-26 10:09:34 +01003 * DHCP server client/server socket creation
Mike Frysinger7031f622006-05-08 03:20:50 +00004 *
5 * udhcp client/server
6 * Copyright (C) 1999 Matthew Ramsay <matthewr@moreton.com.au>
7 * Chris Trew <ctrew@moreton.com.au>
8 *
9 * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
Mike Frysinger7031f622006-05-08 03:20:50 +000025#include <net/if.h>
Denis Vlasenko83e5d6f2006-12-18 21:49:06 +000026#if (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1) || defined _NEWLIB_VERSION
Denys Vlasenko385b4562010-03-26 10:09:34 +010027# include <netpacket/packet.h>
28# include <net/ethernet.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000029#else
Denys Vlasenko385b4562010-03-26 10:09:34 +010030# include <asm/types.h>
31# include <linux/if_packet.h>
32# include <linux/if_ether.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000033#endif
34
35#include "common.h"
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000036
Denys Vlasenko26918dd2009-06-16 12:04:23 +020037int FAST_FUNC udhcp_read_interface(const char *interface, int *ifindex, uint32_t *nip, uint8_t *mac)
Mike Frysinger7031f622006-05-08 03:20:50 +000038{
39 int fd;
40 struct ifreq ifr;
41 struct sockaddr_in *our_ip;
42
Denis Vlasenkoc82b5102007-07-01 17:05:57 +000043 memset(&ifr, 0, sizeof(ifr));
Denis Vlasenko80edead2007-08-02 22:31:05 +000044 fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW);
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000045
46 ifr.ifr_addr.sa_family = AF_INET;
Denis Vlasenko360d9662008-12-02 18:18:50 +000047 strncpy_IFNAMSIZ(ifr.ifr_name, interface);
Denys Vlasenko990a6172009-06-16 10:23:55 +020048 if (nip) {
Denis Vlasenko49a128a2007-07-17 21:42:59 +000049 if (ioctl_or_perror(fd, SIOCGIFADDR, &ifr,
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000050 "is interface %s up and configured?", interface)
51 ) {
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000052 close(fd);
53 return -1;
54 }
55 our_ip = (struct sockaddr_in *) &ifr.ifr_addr;
Denys Vlasenko990a6172009-06-16 10:23:55 +020056 *nip = our_ip->sin_addr.s_addr;
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020057 log1("IP %s", inet_ntoa(our_ip->sin_addr));
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000058 }
59
Denis Vlasenko736230e2006-11-20 19:40:36 +000060 if (ifindex) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000061 if (ioctl_or_warn(fd, SIOCGIFINDEX, &ifr) != 0) {
Denis Vlasenko736230e2006-11-20 19:40:36 +000062 close(fd);
63 return -1;
64 }
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020065 log1("Adapter index %d", ifr.ifr_ifindex);
Denis Vlasenko736230e2006-11-20 19:40:36 +000066 *ifindex = ifr.ifr_ifindex;
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000067 }
68
Denys Vlasenko26918dd2009-06-16 12:04:23 +020069 if (mac) {
Denis Vlasenkofb79a2e2007-07-14 22:07:14 +000070 if (ioctl_or_warn(fd, SIOCGIFHWADDR, &ifr) != 0) {
Denis Vlasenko736230e2006-11-20 19:40:36 +000071 close(fd);
72 return -1;
73 }
Denys Vlasenko26918dd2009-06-16 12:04:23 +020074 memcpy(mac, ifr.ifr_hwaddr.sa_data, 6);
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020075 log1("MAC %02x:%02x:%02x:%02x:%02x:%02x",
Denys Vlasenko26918dd2009-06-16 12:04:23 +020076 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Denis Vlasenko5a3395b2006-11-18 19:51:32 +000077 }
78
Denis Vlasenko80edead2007-08-02 22:31:05 +000079 close(fd);
Mike Frysinger7031f622006-05-08 03:20:50 +000080 return 0;
81}
82
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +000083/* 1. None of the callers expects it to ever fail */
84/* 2. ip was always INADDR_ANY */
Denis Vlasenkof1980f62008-09-26 09:34:59 +000085int FAST_FUNC udhcp_listen_socket(/*uint32_t ip,*/ int port, const char *inf)
Mike Frysinger7031f622006-05-08 03:20:50 +000086{
Mike Frysinger7031f622006-05-08 03:20:50 +000087 int fd;
88 struct sockaddr_in addr;
Mike Frysinger7031f622006-05-08 03:20:50 +000089
Denys Vlasenkoac906fa2009-06-17 11:54:52 +020090 log1("Opening listen socket on *:%d %s", port, inf);
Bernhard Reutner-Fischer28fbd692006-12-19 16:31:09 +000091 fd = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
Mike Frysinger7031f622006-05-08 03:20:50 +000092
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +000093 setsockopt_reuseaddr(fd);
94 if (setsockopt_broadcast(fd) == -1)
95 bb_perror_msg_and_die("SO_BROADCAST");
96
Denis Vlasenko7ff85c52008-10-27 12:56:58 +000097 /* NB: bug 1032 says this doesn't work on ethernet aliases (ethN:M) */
Denis Vlasenkoe5373852008-12-10 11:12:16 +000098 if (setsockopt_bindtodevice(fd, inf))
99 xfunc_die(); /* warning is already printed */
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000100
Mike Frysinger7031f622006-05-08 03:20:50 +0000101 memset(&addr, 0, sizeof(addr));
102 addr.sin_family = AF_INET;
103 addr.sin_port = htons(port);
Denis Vlasenkoa27a11b2007-08-18 14:16:39 +0000104 /* addr.sin_addr.s_addr = ip; - all-zeros is INADDR_ANY */
105 xbind(fd, (struct sockaddr *)&addr, sizeof(addr));
Mike Frysinger7031f622006-05-08 03:20:50 +0000106
107 return fd;
108}