1 /*
2  * dhcpcd - DHCP client daemon
3  * Copyright (c) 2009-2015 Roy Marples <roy@marples.name>
4  * All rights reserved
5 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * THIS IS A NASTY HACK THAT SHOULD NEVER HAVE HAPPENED
30  * Basically we cannot include linux/if.h and net/if.h because
31  * they have conflicting structures.
32  * Sadly, linux/wireless.h includes linux/if.h all the time.
33  * Some kernel-header installs fix this and some do not.
34  * This file solely exists for those who do not.
35  *
36  * We *could* include wireless.h as that is designed for userspace,
37  * but that then depends on the correct version of wireless-tools being
38  * installed which isn't always the case.
39  */
40 
41 #include <sys/ioctl.h>
42 #include <sys/socket.h>
43 
44 #include <linux/types.h>
45 #include <linux/rtnetlink.h>
46 /* Support older kernels */
47 #ifdef IFLA_WIRELESS
48 # include <linux/if.h>
49 # include <linux/wireless.h>
50 #else
51 # define IFLA_WIRELESS (IFLA_MASTER + 1)
52 #endif
53 
54 #include <string.h>
55 #include <unistd.h>
56 
57 #include "config.h"
58 
59 /* We can't include if.h or dhcpcd.h because
60  * they would pull in net/if.h, which defeats the purpose of this hack. */
61 #define IF_SSIDSIZE 33
62 int if_getssid_wext(const char *ifname, uint8_t *ssid);
63 
64 int
if_getssid_wext(const char * ifname,uint8_t * ssid)65 if_getssid_wext(const char *ifname, uint8_t *ssid)
66 {
67 #ifdef SIOCGIWESSID
68 	int s, retval;
69 	struct iwreq iwr;
70 
71 	if ((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
72 		return -1;
73 	memset(&iwr, 0, sizeof(iwr));
74 	strlcpy(iwr.ifr_name, ifname, sizeof(iwr.ifr_name));
75 	iwr.u.essid.pointer = ssid;
76 	iwr.u.essid.length = IF_SSIDSIZE - 1;
77 
78 	if (ioctl(s, SIOCGIWESSID, &iwr) == 0) {
79 		retval = iwr.u.essid.length;
80 		ssid[retval] = '\0';
81 	} else
82 		retval = -1;
83 	close(s);
84 	return retval;
85 #else
86 	/* Stop gcc warning about unused parameters */
87 	ifname = ssid;
88 	return -1;
89 #endif
90 }
91