"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 2 | /* |
Denys Vlasenko | 385b456 | 2010-03-26 10:09:34 +0100 | [diff] [blame] | 3 | * DHCP server client/server socket creation |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 4 | * |
| 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 Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 25 | #include "common.h" |
Dan Fandrich | f533ec8 | 2011-06-10 05:17:59 +0200 | [diff] [blame] | 26 | #include <net/if.h> |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 27 | |
Denys Vlasenko | 26918dd | 2009-06-16 12:04:23 +0200 | [diff] [blame] | 28 | int FAST_FUNC udhcp_read_interface(const char *interface, int *ifindex, uint32_t *nip, uint8_t *mac) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 29 | { |
Denys Vlasenko | dd169e8 | 2011-01-20 12:13:23 +0100 | [diff] [blame] | 30 | /* char buffer instead of bona-fide struct avoids aliasing warning */ |
| 31 | char ifr_buf[sizeof(struct ifreq)]; |
| 32 | struct ifreq *const ifr = (void *)ifr_buf; |
| 33 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 34 | int fd; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 35 | struct sockaddr_in *our_ip; |
| 36 | |
Denys Vlasenko | dd169e8 | 2011-01-20 12:13:23 +0100 | [diff] [blame] | 37 | memset(ifr, 0, sizeof(*ifr)); |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 38 | fd = xsocket(AF_INET, SOCK_RAW, IPPROTO_RAW); |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 39 | |
Denys Vlasenko | dd169e8 | 2011-01-20 12:13:23 +0100 | [diff] [blame] | 40 | ifr->ifr_addr.sa_family = AF_INET; |
| 41 | strncpy_IFNAMSIZ(ifr->ifr_name, interface); |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 42 | if (nip) { |
Denys Vlasenko | dd169e8 | 2011-01-20 12:13:23 +0100 | [diff] [blame] | 43 | if (ioctl_or_perror(fd, SIOCGIFADDR, ifr, |
Denis Vlasenko | fb79a2e | 2007-07-14 22:07:14 +0000 | [diff] [blame] | 44 | "is interface %s up and configured?", interface) |
| 45 | ) { |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 46 | close(fd); |
| 47 | return -1; |
| 48 | } |
Denys Vlasenko | dd169e8 | 2011-01-20 12:13:23 +0100 | [diff] [blame] | 49 | our_ip = (struct sockaddr_in *) &ifr->ifr_addr; |
Denys Vlasenko | 990a617 | 2009-06-16 10:23:55 +0200 | [diff] [blame] | 50 | *nip = our_ip->sin_addr.s_addr; |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 51 | log1("IP %s", inet_ntoa(our_ip->sin_addr)); |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 54 | if (ifindex) { |
Denys Vlasenko | dd169e8 | 2011-01-20 12:13:23 +0100 | [diff] [blame] | 55 | if (ioctl_or_warn(fd, SIOCGIFINDEX, ifr) != 0) { |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 56 | close(fd); |
| 57 | return -1; |
| 58 | } |
Denys Vlasenko | b72f1ef | 2017-07-24 12:06:53 +0200 | [diff] [blame^] | 59 | log2("ifindex %d", ifr->ifr_ifindex); |
Denys Vlasenko | dd169e8 | 2011-01-20 12:13:23 +0100 | [diff] [blame] | 60 | *ifindex = ifr->ifr_ifindex; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Denys Vlasenko | 26918dd | 2009-06-16 12:04:23 +0200 | [diff] [blame] | 63 | if (mac) { |
Denys Vlasenko | dd169e8 | 2011-01-20 12:13:23 +0100 | [diff] [blame] | 64 | if (ioctl_or_warn(fd, SIOCGIFHWADDR, ifr) != 0) { |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 65 | close(fd); |
| 66 | return -1; |
| 67 | } |
Denys Vlasenko | dd169e8 | 2011-01-20 12:13:23 +0100 | [diff] [blame] | 68 | memcpy(mac, ifr->ifr_hwaddr.sa_data, 6); |
Denys Vlasenko | ac906fa | 2009-06-17 11:54:52 +0200 | [diff] [blame] | 69 | log1("MAC %02x:%02x:%02x:%02x:%02x:%02x", |
Denys Vlasenko | 26918dd | 2009-06-16 12:04:23 +0200 | [diff] [blame] | 70 | mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Denis Vlasenko | 80edead | 2007-08-02 22:31:05 +0000 | [diff] [blame] | 73 | close(fd); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 74 | return 0; |
| 75 | } |
| 76 | |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 77 | /* 1. None of the callers expects it to ever fail */ |
| 78 | /* 2. ip was always INADDR_ANY */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 79 | int FAST_FUNC udhcp_listen_socket(/*uint32_t ip,*/ int port, const char *inf) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 80 | { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 81 | int fd; |
| 82 | struct sockaddr_in addr; |
Denys Vlasenko | 6567edd | 2012-08-10 00:27:22 +0200 | [diff] [blame] | 83 | char *colon; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 84 | |
Denys Vlasenko | 8f2e99c | 2016-03-30 18:41:23 +0200 | [diff] [blame] | 85 | log1("opening listen socket on *:%d %s", port, inf); |
Bernhard Reutner-Fischer | 28fbd69 | 2006-12-19 16:31:09 +0000 | [diff] [blame] | 86 | fd = xsocket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 87 | |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 88 | setsockopt_reuseaddr(fd); |
| 89 | if (setsockopt_broadcast(fd) == -1) |
| 90 | bb_perror_msg_and_die("SO_BROADCAST"); |
| 91 | |
Denys Vlasenko | 6567edd | 2012-08-10 00:27:22 +0200 | [diff] [blame] | 92 | /* SO_BINDTODEVICE doesn't work on ethernet aliases (ethN:M) */ |
| 93 | colon = strrchr(inf, ':'); |
| 94 | if (colon) |
| 95 | *colon = '\0'; |
| 96 | |
Denis Vlasenko | e537385 | 2008-12-10 11:12:16 +0000 | [diff] [blame] | 97 | if (setsockopt_bindtodevice(fd, inf)) |
| 98 | xfunc_die(); /* warning is already printed */ |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 99 | |
Denys Vlasenko | 6567edd | 2012-08-10 00:27:22 +0200 | [diff] [blame] | 100 | if (colon) |
| 101 | *colon = ':'; |
| 102 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 103 | memset(&addr, 0, sizeof(addr)); |
| 104 | addr.sin_family = AF_INET; |
| 105 | addr.sin_port = htons(port); |
Denis Vlasenko | a27a11b | 2007-08-18 14:16:39 +0000 | [diff] [blame] | 106 | /* addr.sin_addr.s_addr = ip; - all-zeros is INADDR_ANY */ |
| 107 | xbind(fd, (struct sockaddr *)&addr, sizeof(addr)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 108 | |
| 109 | return fd; |
| 110 | } |