xref: /freebsd/lib/lib80211/lib80211_ioctl.c (revision 6b13d60bf49ee40626d7e3a5d5a80519f0067307)
1 /*
2  * Copyright 2001 The Aerospace Corporation.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of The Aerospace Corporation may not be used to endorse or
13  *    promote products derived from this software.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AEROSPACE CORPORATION ``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 AEROSPACE CORPORATION 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  * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
30  * All rights reserved.
31  *
32  * This code is derived from software contributed to The NetBSD Foundation
33  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
34  * NASA Ames Research Center.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
46  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
47  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
49  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55  * POSSIBILITY OF SUCH DAMAGE.
56  */
57 
58 #include <sys/param.h>
59 #include <sys/ioctl.h>
60 #include <sys/socket.h>
61 #include <sys/sysctl.h>
62 #include <sys/time.h>
63 
64 #include <net/ethernet.h>
65 #include <net/if.h>
66 #include <net/if_dl.h>
67 #include <net/if_types.h>
68 #include <net/if_media.h>
69 #include <net/route.h>
70 
71 #include <net80211/ieee80211_ioctl.h>
72 #include <net80211/ieee80211_freebsd.h>
73 #include <net80211/ieee80211_superg.h>
74 #include <net80211/ieee80211_tdma.h>
75 #include <net80211/ieee80211_mesh.h>
76 
77 #include <assert.h>
78 #include <ctype.h>
79 #include <err.h>
80 #include <errno.h>
81 #include <fcntl.h>
82 #include <inttypes.h>
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <string.h>
86 #include <unistd.h>
87 #include <stdarg.h>
88 #include <stddef.h>		/* NB: for offsetof */
89 
90 #include "lib80211_ioctl.h"
91 
92 /*
93  * These implement basic net80211 accessor methods to wrap the IOCTL
94  * calls.
95  */
96 
97 int
98 lib80211_get80211(int s, const char *name, int type, void *data, int len)
99 {
100 	struct ieee80211req ireq;
101 
102 	(void) memset(&ireq, 0, sizeof(ireq));
103 	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
104 	ireq.i_type = type;
105 	ireq.i_data = data;
106 	ireq.i_len = len;
107 	return ioctl(s, SIOCG80211, &ireq);
108 }
109 
110 int
111 lib80211_get80211len(int s, const char *name, int type, void *data, int len, int *plen)
112 {
113 	struct ieee80211req ireq;
114 
115 	(void) memset(&ireq, 0, sizeof(ireq));
116 	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
117 	ireq.i_type = type;
118 	ireq.i_len = len;
119 	assert(ireq.i_len == len);	/* NB: check for 16-bit truncation */
120 	ireq.i_data = data;
121 	if (ioctl(s, SIOCG80211, &ireq) < 0)
122 		return -1;
123 	*plen = ireq.i_len;
124 	return 0;
125 }
126 
127 int
128 lib80211_get80211val(int s, const char *name, int type, int *val)
129 {
130 	struct ieee80211req ireq;
131 
132 	(void) memset(&ireq, 0, sizeof(ireq));
133 	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
134 	ireq.i_type = type;
135 	if (ioctl(s, SIOCG80211, &ireq) < 0)
136 		return -1;
137 	*val = ireq.i_val;
138 	return 0;
139 }
140 
141 int
142 lib80211_set80211(int s, const char *name, int type, int val, int len, void *data)
143 {
144 	struct ieee80211req	ireq;
145 
146 	(void) memset(&ireq, 0, sizeof(ireq));
147 	(void) strncpy(ireq.i_name, name, sizeof(ireq.i_name));
148 	ireq.i_type = type;
149 	ireq.i_val = val;
150 	ireq.i_len = len;
151 	assert(ireq.i_len == len);	/* NB: check for 16-bit truncation */
152 	ireq.i_data = data;
153 	if (ioctl(s, SIOCS80211, &ireq) < 0)
154 		return (-1);
155 	return (0);
156 }
157 
158