xref: /freebsd/sbin/umbctl/umbctl.c (revision 0f1bf1c22a0c97e84a4db19197a75952487aa20b)
1*0f1bf1c2SAdrian Chadd /*-
2*0f1bf1c2SAdrian Chadd  * SPDX-License-Identifier: BSD-3-Clause
3*0f1bf1c2SAdrian Chadd  *
4*0f1bf1c2SAdrian Chadd  * Original copyright (c) 2018 Pierre Pronchery <khorben@defora.org> (for the
5*0f1bf1c2SAdrian Chadd  * NetBSD Project)
6*0f1bf1c2SAdrian Chadd  *
7*0f1bf1c2SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
8*0f1bf1c2SAdrian Chadd  * modification, are permitted provided that the following conditions
9*0f1bf1c2SAdrian Chadd  * are met:
10*0f1bf1c2SAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
11*0f1bf1c2SAdrian Chadd  *    notice, this list of conditions and the following disclaimer.
12*0f1bf1c2SAdrian Chadd  * 2. Redistributions in binary form must reproduce the above copyright
13*0f1bf1c2SAdrian Chadd  *    notice, this list of conditions and the following disclaimer in the
14*0f1bf1c2SAdrian Chadd  *    documentation and/or other materials provided with the distribution.
15*0f1bf1c2SAdrian Chadd  *
16*0f1bf1c2SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
17*0f1bf1c2SAdrian Chadd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*0f1bf1c2SAdrian Chadd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*0f1bf1c2SAdrian Chadd  * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*0f1bf1c2SAdrian Chadd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21*0f1bf1c2SAdrian Chadd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22*0f1bf1c2SAdrian Chadd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23*0f1bf1c2SAdrian Chadd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*0f1bf1c2SAdrian Chadd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25*0f1bf1c2SAdrian Chadd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*0f1bf1c2SAdrian Chadd  *
27*0f1bf1c2SAdrian Chadd  * Copyright (c) 2022 ADISTA SAS (FreeBSD updates)
28*0f1bf1c2SAdrian Chadd  *
29*0f1bf1c2SAdrian Chadd  * Updates for FreeBSD by Pierre Pronchery <pierre@defora.net>
30*0f1bf1c2SAdrian Chadd  *
31*0f1bf1c2SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
32*0f1bf1c2SAdrian Chadd  * modification, are permitted provided that the following conditions are met:
33*0f1bf1c2SAdrian Chadd  *
34*0f1bf1c2SAdrian Chadd  * - Redistributions of source code must retain the above copyright notice,
35*0f1bf1c2SAdrian Chadd  *   this list of conditions and the following disclaimer.
36*0f1bf1c2SAdrian Chadd  * - Redistributions in binary form must reproduce the above copyright notice,
37*0f1bf1c2SAdrian Chadd  *   this list of conditions and the following disclaimer in the documentation
38*0f1bf1c2SAdrian Chadd  *   and/or other materials provided with the distribution.
39*0f1bf1c2SAdrian Chadd  * - Neither the name of the copyright holder nor the names of its contributors
40*0f1bf1c2SAdrian Chadd  *   may be used to endorse or promote products derived from this software
41*0f1bf1c2SAdrian Chadd  *   without specific prior written permission.
42*0f1bf1c2SAdrian Chadd  *
43*0f1bf1c2SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
44*0f1bf1c2SAdrian Chadd  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45*0f1bf1c2SAdrian Chadd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46*0f1bf1c2SAdrian Chadd  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
47*0f1bf1c2SAdrian Chadd  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48*0f1bf1c2SAdrian Chadd  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49*0f1bf1c2SAdrian Chadd  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50*0f1bf1c2SAdrian Chadd  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
51*0f1bf1c2SAdrian Chadd  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52*0f1bf1c2SAdrian Chadd  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
53*0f1bf1c2SAdrian Chadd  * POSSIBILITY OF SUCH DAMAGE.
54*0f1bf1c2SAdrian Chadd  *
55*0f1bf1c2SAdrian Chadd  * $NetBSD: umbctl.c,v 1.4 2020/05/13 21:44:30 khorben Exp $
56*0f1bf1c2SAdrian Chadd  */
57*0f1bf1c2SAdrian Chadd 
58*0f1bf1c2SAdrian Chadd #include <sys/endian.h>
59*0f1bf1c2SAdrian Chadd #include <sys/ioctl.h>
60*0f1bf1c2SAdrian Chadd #include <sys/socket.h>
61*0f1bf1c2SAdrian Chadd #include <sys/sockio.h>
62*0f1bf1c2SAdrian Chadd 
63*0f1bf1c2SAdrian Chadd #include <net/if.h>
64*0f1bf1c2SAdrian Chadd #include <netinet/in.h>
65*0f1bf1c2SAdrian Chadd #include <arpa/inet.h>
66*0f1bf1c2SAdrian Chadd 
67*0f1bf1c2SAdrian Chadd #include <ctype.h>
68*0f1bf1c2SAdrian Chadd #include <errno.h>
69*0f1bf1c2SAdrian Chadd #include <inttypes.h>
70*0f1bf1c2SAdrian Chadd #include <stdarg.h>
71*0f1bf1c2SAdrian Chadd #include <stdio.h>
72*0f1bf1c2SAdrian Chadd #include <string.h>
73*0f1bf1c2SAdrian Chadd #include <strings.h>
74*0f1bf1c2SAdrian Chadd #include <unistd.h>
75*0f1bf1c2SAdrian Chadd 
76*0f1bf1c2SAdrian Chadd #include "mbim.h"
77*0f1bf1c2SAdrian Chadd #include "if_umbreg.h"
78*0f1bf1c2SAdrian Chadd 
79*0f1bf1c2SAdrian Chadd /* constants */
80*0f1bf1c2SAdrian Chadd static const struct umb_valdescr _umb_actstate[] =
81*0f1bf1c2SAdrian Chadd 	MBIM_ACTIVATION_STATE_DESCRIPTIONS;
82*0f1bf1c2SAdrian Chadd 
83*0f1bf1c2SAdrian Chadd static const struct umb_valdescr _umb_simstate[] =
84*0f1bf1c2SAdrian Chadd 	MBIM_SIMSTATE_DESCRIPTIONS;
85*0f1bf1c2SAdrian Chadd 
86*0f1bf1c2SAdrian Chadd static const struct umb_valdescr _umb_regstate[] =
87*0f1bf1c2SAdrian Chadd 	MBIM_REGSTATE_DESCRIPTIONS;
88*0f1bf1c2SAdrian Chadd 
89*0f1bf1c2SAdrian Chadd static const struct umb_valdescr _umb_pktstate[] =
90*0f1bf1c2SAdrian Chadd 	MBIM_PKTSRV_STATE_DESCRIPTIONS;
91*0f1bf1c2SAdrian Chadd 
92*0f1bf1c2SAdrian Chadd static const struct umb_valdescr _umb_dataclass[] =
93*0f1bf1c2SAdrian Chadd 	MBIM_DATACLASS_DESCRIPTIONS;
94*0f1bf1c2SAdrian Chadd 
95*0f1bf1c2SAdrian Chadd static const struct umb_valdescr _umb_state[] =
96*0f1bf1c2SAdrian Chadd 	UMB_INTERNAL_STATE_DESCRIPTIONS;
97*0f1bf1c2SAdrian Chadd 
98*0f1bf1c2SAdrian Chadd static const struct umb_valdescr _umb_pin_state[] =
99*0f1bf1c2SAdrian Chadd {
100*0f1bf1c2SAdrian Chadd 	{ UMB_PIN_REQUIRED, "PIN required"},
101*0f1bf1c2SAdrian Chadd 	{ UMB_PIN_UNLOCKED, "PIN unlocked"},
102*0f1bf1c2SAdrian Chadd 	{ UMB_PUK_REQUIRED, "PUK required"},
103*0f1bf1c2SAdrian Chadd 	{ 0, NULL }
104*0f1bf1c2SAdrian Chadd };
105*0f1bf1c2SAdrian Chadd 
106*0f1bf1c2SAdrian Chadd static const struct umb_valdescr _umb_regmode[] =
107*0f1bf1c2SAdrian Chadd {
108*0f1bf1c2SAdrian Chadd 	{ MBIM_REGMODE_UNKNOWN, "unknown" },
109*0f1bf1c2SAdrian Chadd 	{ MBIM_REGMODE_AUTOMATIC, "automatic" },
110*0f1bf1c2SAdrian Chadd 	{ MBIM_REGMODE_MANUAL, "manual" },
111*0f1bf1c2SAdrian Chadd 	{ 0, NULL }
112*0f1bf1c2SAdrian Chadd };
113*0f1bf1c2SAdrian Chadd 
114*0f1bf1c2SAdrian Chadd static const struct umb_valdescr _umb_ber[] =
115*0f1bf1c2SAdrian Chadd {
116*0f1bf1c2SAdrian Chadd 	{ UMB_BER_EXCELLENT, "excellent" },
117*0f1bf1c2SAdrian Chadd 	{ UMB_BER_VERYGOOD, "very good" },
118*0f1bf1c2SAdrian Chadd 	{ UMB_BER_GOOD, "good" },
119*0f1bf1c2SAdrian Chadd 	{ UMB_BER_OK, "ok" },
120*0f1bf1c2SAdrian Chadd 	{ UMB_BER_MEDIUM, "medium" },
121*0f1bf1c2SAdrian Chadd 	{ UMB_BER_BAD, "bad" },
122*0f1bf1c2SAdrian Chadd 	{ UMB_BER_VERYBAD, "very bad" },
123*0f1bf1c2SAdrian Chadd 	{ UMB_BER_EXTREMELYBAD, "extremely bad" },
124*0f1bf1c2SAdrian Chadd 	{ 0, NULL }
125*0f1bf1c2SAdrian Chadd };
126*0f1bf1c2SAdrian Chadd 
127*0f1bf1c2SAdrian Chadd 
128*0f1bf1c2SAdrian Chadd /* prototypes */
129*0f1bf1c2SAdrian Chadd static int _char_to_utf16(const char * in, uint16_t * out, size_t outlen);
130*0f1bf1c2SAdrian Chadd static int _error(int ret, char const * format, ...);
131*0f1bf1c2SAdrian Chadd static int _umbctl(char const * ifname, int verbose, int argc, char * argv[]);
132*0f1bf1c2SAdrian Chadd static int _umbctl_file(char const * ifname, char const * filename,
133*0f1bf1c2SAdrian Chadd 		int verbose);
134*0f1bf1c2SAdrian Chadd static void _umbctl_info(char const * ifname, struct umb_info * umbi);
135*0f1bf1c2SAdrian Chadd static int _umbctl_ioctl(char const * ifname, int fd, unsigned long request,
136*0f1bf1c2SAdrian Chadd 		struct ifreq * ifr);
137*0f1bf1c2SAdrian Chadd static int _umbctl_set(char const * ifname, struct umb_parameter * umbp,
138*0f1bf1c2SAdrian Chadd 		int argc, char * argv[]);
139*0f1bf1c2SAdrian Chadd static int _umbctl_socket(void);
140*0f1bf1c2SAdrian Chadd static int _usage(void);
141*0f1bf1c2SAdrian Chadd static void _utf16_to_char(uint16_t * in, int inlen, char * out, size_t outlen);
142*0f1bf1c2SAdrian Chadd 
143*0f1bf1c2SAdrian Chadd 
144*0f1bf1c2SAdrian Chadd /* functions */
145*0f1bf1c2SAdrian Chadd /* char_to_utf16 */
146*0f1bf1c2SAdrian Chadd /* this function is from OpenBSD's ifconfig(8) */
_char_to_utf16(const char * in,uint16_t * out,size_t outlen)147*0f1bf1c2SAdrian Chadd static int _char_to_utf16(const char * in, uint16_t * out, size_t outlen)
148*0f1bf1c2SAdrian Chadd {
149*0f1bf1c2SAdrian Chadd 	int	n = 0;
150*0f1bf1c2SAdrian Chadd 	uint16_t c;
151*0f1bf1c2SAdrian Chadd 
152*0f1bf1c2SAdrian Chadd 	for (;;) {
153*0f1bf1c2SAdrian Chadd 		c = *in++;
154*0f1bf1c2SAdrian Chadd 
155*0f1bf1c2SAdrian Chadd 		if (c == '\0') {
156*0f1bf1c2SAdrian Chadd 			/*
157*0f1bf1c2SAdrian Chadd 			 * NUL termination is not required, but zero out the
158*0f1bf1c2SAdrian Chadd 			 * residual buffer
159*0f1bf1c2SAdrian Chadd 			 */
160*0f1bf1c2SAdrian Chadd 			memset(out, 0, outlen);
161*0f1bf1c2SAdrian Chadd 			return n;
162*0f1bf1c2SAdrian Chadd 		}
163*0f1bf1c2SAdrian Chadd 		if (outlen < sizeof(*out))
164*0f1bf1c2SAdrian Chadd 			return -1;
165*0f1bf1c2SAdrian Chadd 
166*0f1bf1c2SAdrian Chadd 		*out++ = htole16(c);
167*0f1bf1c2SAdrian Chadd 		n += sizeof(*out);
168*0f1bf1c2SAdrian Chadd 		outlen -= sizeof(*out);
169*0f1bf1c2SAdrian Chadd 	}
170*0f1bf1c2SAdrian Chadd }
171*0f1bf1c2SAdrian Chadd 
172*0f1bf1c2SAdrian Chadd 
173*0f1bf1c2SAdrian Chadd /* error */
_error(int ret,char const * format,...)174*0f1bf1c2SAdrian Chadd static int _error(int ret, char const * format, ...)
175*0f1bf1c2SAdrian Chadd {
176*0f1bf1c2SAdrian Chadd 	va_list ap;
177*0f1bf1c2SAdrian Chadd 
178*0f1bf1c2SAdrian Chadd 	fputs("umbctl: ", stderr);
179*0f1bf1c2SAdrian Chadd 	va_start(ap, format);
180*0f1bf1c2SAdrian Chadd 	vfprintf(stderr, format, ap);
181*0f1bf1c2SAdrian Chadd 	va_end(ap);
182*0f1bf1c2SAdrian Chadd 	fputs("\n", stderr);
183*0f1bf1c2SAdrian Chadd 	return ret;
184*0f1bf1c2SAdrian Chadd }
185*0f1bf1c2SAdrian Chadd 
186*0f1bf1c2SAdrian Chadd 
187*0f1bf1c2SAdrian Chadd /* umbctl */
_umbctl(char const * ifname,int verbose,int argc,char * argv[])188*0f1bf1c2SAdrian Chadd static int _umbctl(char const * ifname, int verbose, int argc, char * argv[])
189*0f1bf1c2SAdrian Chadd {
190*0f1bf1c2SAdrian Chadd 	int fd;
191*0f1bf1c2SAdrian Chadd 	struct ifreq ifr;
192*0f1bf1c2SAdrian Chadd 	struct umb_info umbi;
193*0f1bf1c2SAdrian Chadd 	struct umb_parameter umbp;
194*0f1bf1c2SAdrian Chadd 
195*0f1bf1c2SAdrian Chadd 	if((fd = _umbctl_socket()) < 0)
196*0f1bf1c2SAdrian Chadd 		return 2;
197*0f1bf1c2SAdrian Chadd 	memset(&ifr, 0, sizeof(ifr));
198*0f1bf1c2SAdrian Chadd 	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
199*0f1bf1c2SAdrian Chadd 	if(argc != 0)
200*0f1bf1c2SAdrian Chadd 	{
201*0f1bf1c2SAdrian Chadd 		memset(&umbp, 0, sizeof(umbp));
202*0f1bf1c2SAdrian Chadd 		ifr.ifr_data = (caddr_t)&umbp;
203*0f1bf1c2SAdrian Chadd 		if(_umbctl_ioctl(ifname, fd, SIOCGUMBPARAM, &ifr) != 0
204*0f1bf1c2SAdrian Chadd 				|| _umbctl_set(ifname, &umbp, argc, argv) != 0
205*0f1bf1c2SAdrian Chadd 				|| _umbctl_ioctl(ifname, fd, SIOCSUMBPARAM,
206*0f1bf1c2SAdrian Chadd 					&ifr) != 0)
207*0f1bf1c2SAdrian Chadd 		{
208*0f1bf1c2SAdrian Chadd 			close(fd);
209*0f1bf1c2SAdrian Chadd 			return 2;
210*0f1bf1c2SAdrian Chadd 		}
211*0f1bf1c2SAdrian Chadd 	}
212*0f1bf1c2SAdrian Chadd 	if(argc == 0 || verbose > 0)
213*0f1bf1c2SAdrian Chadd 	{
214*0f1bf1c2SAdrian Chadd 		ifr.ifr_data = (caddr_t)&umbi;
215*0f1bf1c2SAdrian Chadd 		if(_umbctl_ioctl(ifname, fd, SIOCGUMBINFO, &ifr) != 0)
216*0f1bf1c2SAdrian Chadd 		{
217*0f1bf1c2SAdrian Chadd 			close(fd);
218*0f1bf1c2SAdrian Chadd 			return 3;
219*0f1bf1c2SAdrian Chadd 		}
220*0f1bf1c2SAdrian Chadd 		_umbctl_info(ifname, &umbi);
221*0f1bf1c2SAdrian Chadd 	}
222*0f1bf1c2SAdrian Chadd 	if(close(fd) != 0)
223*0f1bf1c2SAdrian Chadd 		return _error(2, "%s: %s", ifname, strerror(errno));
224*0f1bf1c2SAdrian Chadd 	return 0;
225*0f1bf1c2SAdrian Chadd }
226*0f1bf1c2SAdrian Chadd 
227*0f1bf1c2SAdrian Chadd 
228*0f1bf1c2SAdrian Chadd /* umbctl_file */
229*0f1bf1c2SAdrian Chadd static int _file_parse(char const * ifname, struct umb_parameter * umbp,
230*0f1bf1c2SAdrian Chadd 		char const * filename);
231*0f1bf1c2SAdrian Chadd 
_umbctl_file(char const * ifname,char const * filename,int verbose)232*0f1bf1c2SAdrian Chadd static int _umbctl_file(char const * ifname, char const * filename, int verbose)
233*0f1bf1c2SAdrian Chadd {
234*0f1bf1c2SAdrian Chadd 	int fd;
235*0f1bf1c2SAdrian Chadd 	struct ifreq ifr;
236*0f1bf1c2SAdrian Chadd 	struct umb_parameter umbp;
237*0f1bf1c2SAdrian Chadd 	struct umb_info umbi;
238*0f1bf1c2SAdrian Chadd 
239*0f1bf1c2SAdrian Chadd 	if((fd = _umbctl_socket()) < 0)
240*0f1bf1c2SAdrian Chadd 		return 2;
241*0f1bf1c2SAdrian Chadd 	memset(&ifr, 0, sizeof(ifr));
242*0f1bf1c2SAdrian Chadd 	strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
243*0f1bf1c2SAdrian Chadd 	ifr.ifr_data = (caddr_t)&umbp;
244*0f1bf1c2SAdrian Chadd 	memset(&umbp, 0, sizeof(umbp));
245*0f1bf1c2SAdrian Chadd 	if(_umbctl_ioctl(ifname, fd, SIOCGUMBPARAM, &ifr) != 0
246*0f1bf1c2SAdrian Chadd 			|| _file_parse(ifname, &umbp, filename) != 0
247*0f1bf1c2SAdrian Chadd 			|| _umbctl_ioctl(ifname, fd, SIOCSUMBPARAM, &ifr) != 0)
248*0f1bf1c2SAdrian Chadd 	{
249*0f1bf1c2SAdrian Chadd 		close(fd);
250*0f1bf1c2SAdrian Chadd 		return 2;
251*0f1bf1c2SAdrian Chadd 	}
252*0f1bf1c2SAdrian Chadd 	if(verbose > 0)
253*0f1bf1c2SAdrian Chadd 	{
254*0f1bf1c2SAdrian Chadd 		ifr.ifr_data = (caddr_t)&umbi;
255*0f1bf1c2SAdrian Chadd 		if(_umbctl_ioctl(ifname, fd, SIOCGUMBINFO, &ifr) != 0)
256*0f1bf1c2SAdrian Chadd 		{
257*0f1bf1c2SAdrian Chadd 			close(fd);
258*0f1bf1c2SAdrian Chadd 			return 3;
259*0f1bf1c2SAdrian Chadd 		}
260*0f1bf1c2SAdrian Chadd 		_umbctl_info(ifname, &umbi);
261*0f1bf1c2SAdrian Chadd 	}
262*0f1bf1c2SAdrian Chadd 	if(close(fd) != 0)
263*0f1bf1c2SAdrian Chadd 		return _error(2, "%s: %s", ifname, strerror(errno));
264*0f1bf1c2SAdrian Chadd 	return 0;
265*0f1bf1c2SAdrian Chadd }
266*0f1bf1c2SAdrian Chadd 
_file_parse(char const * ifname,struct umb_parameter * umbp,char const * filename)267*0f1bf1c2SAdrian Chadd static int _file_parse(char const * ifname, struct umb_parameter * umbp,
268*0f1bf1c2SAdrian Chadd 		char const * filename)
269*0f1bf1c2SAdrian Chadd {
270*0f1bf1c2SAdrian Chadd 	int ret = 0;
271*0f1bf1c2SAdrian Chadd 	FILE * fp;
272*0f1bf1c2SAdrian Chadd 	char buf[512];
273*0f1bf1c2SAdrian Chadd 	size_t len;
274*0f1bf1c2SAdrian Chadd 	int i;
275*0f1bf1c2SAdrian Chadd 	int eof;
276*0f1bf1c2SAdrian Chadd 	char * tokens[3] = { buf, NULL, NULL };
277*0f1bf1c2SAdrian Chadd 	char * p;
278*0f1bf1c2SAdrian Chadd 
279*0f1bf1c2SAdrian Chadd 	if((fp = fopen(filename, "r")) == NULL)
280*0f1bf1c2SAdrian Chadd 		return _error(2, "%s: %s", filename, strerror(errno));
281*0f1bf1c2SAdrian Chadd 	while(fgets(buf, sizeof(buf), fp) != NULL)
282*0f1bf1c2SAdrian Chadd 	{
283*0f1bf1c2SAdrian Chadd 		if(buf[0] == '#')
284*0f1bf1c2SAdrian Chadd 			continue;
285*0f1bf1c2SAdrian Chadd 		buf[sizeof(buf) - 1] = '\0';
286*0f1bf1c2SAdrian Chadd 		if((len = strlen(buf)) > 0)
287*0f1bf1c2SAdrian Chadd 		{
288*0f1bf1c2SAdrian Chadd 			if(buf[len - 1] != '\n')
289*0f1bf1c2SAdrian Chadd 			{
290*0f1bf1c2SAdrian Chadd 				ret = _error(2, "%s: %s", filename,
291*0f1bf1c2SAdrian Chadd 						"Line too long");
292*0f1bf1c2SAdrian Chadd 				while((i = fgetc(fp)) != EOF && i != '\n');
293*0f1bf1c2SAdrian Chadd 				continue;
294*0f1bf1c2SAdrian Chadd 			}
295*0f1bf1c2SAdrian Chadd 			buf[len - 1] = '\0';
296*0f1bf1c2SAdrian Chadd 		}
297*0f1bf1c2SAdrian Chadd 		if((p = strchr(buf, '=')) != NULL)
298*0f1bf1c2SAdrian Chadd 		{
299*0f1bf1c2SAdrian Chadd 			tokens[1] = p + 1;
300*0f1bf1c2SAdrian Chadd 			*p = '\0';
301*0f1bf1c2SAdrian Chadd 		} else
302*0f1bf1c2SAdrian Chadd 			tokens[1] = NULL;
303*0f1bf1c2SAdrian Chadd 		ret |= _umbctl_set(ifname, umbp, (p != NULL) ? 2 : 1, tokens)
304*0f1bf1c2SAdrian Chadd 			? 2 : 0;
305*0f1bf1c2SAdrian Chadd 	}
306*0f1bf1c2SAdrian Chadd 	eof = feof(fp);
307*0f1bf1c2SAdrian Chadd 	if(fclose(fp) != 0 || !eof)
308*0f1bf1c2SAdrian Chadd 		return _error(2, "%s: %s", filename, strerror(errno));
309*0f1bf1c2SAdrian Chadd 	return ret;
310*0f1bf1c2SAdrian Chadd }
311*0f1bf1c2SAdrian Chadd 
312*0f1bf1c2SAdrian Chadd 
313*0f1bf1c2SAdrian Chadd /* umbctl_info */
_umbctl_info(char const * ifname,struct umb_info * umbi)314*0f1bf1c2SAdrian Chadd static void _umbctl_info(char const * ifname, struct umb_info * umbi)
315*0f1bf1c2SAdrian Chadd {
316*0f1bf1c2SAdrian Chadd 	char provider[UMB_PROVIDERNAME_MAXLEN + 1];
317*0f1bf1c2SAdrian Chadd 	char pn[UMB_PHONENR_MAXLEN + 1];
318*0f1bf1c2SAdrian Chadd 	char roaming[UMB_ROAMINGTEXT_MAXLEN + 1];
319*0f1bf1c2SAdrian Chadd 	char apn[UMB_APN_MAXLEN + 1];
320*0f1bf1c2SAdrian Chadd 	char fwinfo[UMB_FWINFO_MAXLEN + 1];
321*0f1bf1c2SAdrian Chadd 	char hwinfo[UMB_HWINFO_MAXLEN + 1];
322*0f1bf1c2SAdrian Chadd 
323*0f1bf1c2SAdrian Chadd 	_utf16_to_char(umbi->provider, UMB_PROVIDERNAME_MAXLEN,
324*0f1bf1c2SAdrian Chadd 			provider, sizeof(provider));
325*0f1bf1c2SAdrian Chadd 	_utf16_to_char(umbi->pn, UMB_PHONENR_MAXLEN, pn, sizeof(pn));
326*0f1bf1c2SAdrian Chadd 	_utf16_to_char(umbi->roamingtxt, UMB_ROAMINGTEXT_MAXLEN,
327*0f1bf1c2SAdrian Chadd 			roaming, sizeof(roaming));
328*0f1bf1c2SAdrian Chadd 	_utf16_to_char(umbi->apn, UMB_APN_MAXLEN, apn, sizeof(apn));
329*0f1bf1c2SAdrian Chadd 	_utf16_to_char(umbi->fwinfo, UMB_FWINFO_MAXLEN, fwinfo, sizeof(fwinfo));
330*0f1bf1c2SAdrian Chadd 	_utf16_to_char(umbi->hwinfo, UMB_HWINFO_MAXLEN, hwinfo, sizeof(hwinfo));
331*0f1bf1c2SAdrian Chadd 	printf("%s: state %s, mode %s, registration %s\n"
332*0f1bf1c2SAdrian Chadd 			"\tprovider \"%s\", dataclass %s, signal %s\n"
333*0f1bf1c2SAdrian Chadd 			"\tphone number \"%s\", roaming \"%s\" (%s)\n"
334*0f1bf1c2SAdrian Chadd 			"\tAPN \"%s\", TX %" PRIu64 ", RX %" PRIu64 "\n"
335*0f1bf1c2SAdrian Chadd 			"\tfirmware \"%s\", hardware \"%s\"\n",
336*0f1bf1c2SAdrian Chadd 			ifname, umb_val2descr(_umb_state, umbi->state),
337*0f1bf1c2SAdrian Chadd 			umb_val2descr(_umb_regmode, umbi->regmode),
338*0f1bf1c2SAdrian Chadd 			umb_val2descr(_umb_regstate, umbi->regstate), provider,
339*0f1bf1c2SAdrian Chadd 			umb_val2descr(_umb_dataclass, umbi->cellclass),
340*0f1bf1c2SAdrian Chadd 			umb_val2descr(_umb_ber, umbi->ber), pn, roaming,
341*0f1bf1c2SAdrian Chadd 			umbi->enable_roaming ? "allowed" : "denied",
342*0f1bf1c2SAdrian Chadd 			apn, umbi->uplink_speed, umbi->downlink_speed,
343*0f1bf1c2SAdrian Chadd 			fwinfo, hwinfo);
344*0f1bf1c2SAdrian Chadd }
345*0f1bf1c2SAdrian Chadd 
346*0f1bf1c2SAdrian Chadd 
347*0f1bf1c2SAdrian Chadd /* umbctl_ioctl */
_umbctl_ioctl(char const * ifname,int fd,unsigned long request,struct ifreq * ifr)348*0f1bf1c2SAdrian Chadd static int _umbctl_ioctl(char const * ifname, int fd, unsigned long request,
349*0f1bf1c2SAdrian Chadd 		struct ifreq * ifr)
350*0f1bf1c2SAdrian Chadd {
351*0f1bf1c2SAdrian Chadd 	if(ioctl(fd, request, ifr) != 0)
352*0f1bf1c2SAdrian Chadd 		return _error(-1, "%s: %s", ifname, strerror(errno));
353*0f1bf1c2SAdrian Chadd 	return 0;
354*0f1bf1c2SAdrian Chadd }
355*0f1bf1c2SAdrian Chadd 
356*0f1bf1c2SAdrian Chadd 
357*0f1bf1c2SAdrian Chadd /* umbctl_set */
358*0f1bf1c2SAdrian Chadd /* callbacks */
359*0f1bf1c2SAdrian Chadd static int _set_apn(char const *, struct umb_parameter *, char const *);
360*0f1bf1c2SAdrian Chadd static int _set_username(char const *, struct umb_parameter *, char const *);
361*0f1bf1c2SAdrian Chadd static int _set_password(char const *, struct umb_parameter *, char const *);
362*0f1bf1c2SAdrian Chadd static int _set_pin(char const *, struct umb_parameter *, char const *);
363*0f1bf1c2SAdrian Chadd static int _set_puk(char const *, struct umb_parameter *, char const *);
364*0f1bf1c2SAdrian Chadd static int _set_roaming_allow(char const *, struct umb_parameter *,
365*0f1bf1c2SAdrian Chadd 		char const *);
366*0f1bf1c2SAdrian Chadd static int _set_roaming_deny(char const *, struct umb_parameter *,
367*0f1bf1c2SAdrian Chadd 		char const *);
368*0f1bf1c2SAdrian Chadd 
_umbctl_set(char const * ifname,struct umb_parameter * umbp,int argc,char * argv[])369*0f1bf1c2SAdrian Chadd static int _umbctl_set(char const * ifname, struct umb_parameter * umbp,
370*0f1bf1c2SAdrian Chadd 		int argc, char * argv[])
371*0f1bf1c2SAdrian Chadd {
372*0f1bf1c2SAdrian Chadd 	struct
373*0f1bf1c2SAdrian Chadd 	{
374*0f1bf1c2SAdrian Chadd 		char const * name;
375*0f1bf1c2SAdrian Chadd 		int (*callback)(char const *,
376*0f1bf1c2SAdrian Chadd 				struct umb_parameter *, char const *);
377*0f1bf1c2SAdrian Chadd 		int parameter;
378*0f1bf1c2SAdrian Chadd 	} callbacks[] =
379*0f1bf1c2SAdrian Chadd 	{
380*0f1bf1c2SAdrian Chadd 		{ "apn", _set_apn, 1 },
381*0f1bf1c2SAdrian Chadd 		{ "username", _set_username, 1 },
382*0f1bf1c2SAdrian Chadd 		{ "password", _set_password, 1 },
383*0f1bf1c2SAdrian Chadd 		{ "pin", _set_pin, 1 },
384*0f1bf1c2SAdrian Chadd 		{ "puk", _set_puk, 1 },
385*0f1bf1c2SAdrian Chadd 		{ "roaming", _set_roaming_allow, 0 },
386*0f1bf1c2SAdrian Chadd 		{ "-roaming", _set_roaming_deny, 0 },
387*0f1bf1c2SAdrian Chadd 	};
388*0f1bf1c2SAdrian Chadd 	int i;
389*0f1bf1c2SAdrian Chadd 	size_t j;
390*0f1bf1c2SAdrian Chadd 
391*0f1bf1c2SAdrian Chadd 	for(i = 0; i < argc; i++)
392*0f1bf1c2SAdrian Chadd 	{
393*0f1bf1c2SAdrian Chadd 		for(j = 0; j < sizeof(callbacks) / sizeof(*callbacks); j++)
394*0f1bf1c2SAdrian Chadd 			if(strcmp(argv[i], callbacks[j].name) == 0)
395*0f1bf1c2SAdrian Chadd 			{
396*0f1bf1c2SAdrian Chadd 				if(callbacks[j].parameter && i + 1 == argc)
397*0f1bf1c2SAdrian Chadd 					return _error(-1, "%s: Incomplete"
398*0f1bf1c2SAdrian Chadd 							" parameter", argv[i]);
399*0f1bf1c2SAdrian Chadd 				if(callbacks[j].callback(ifname, umbp,
400*0f1bf1c2SAdrian Chadd 							callbacks[j].parameter
401*0f1bf1c2SAdrian Chadd 							? argv[i + 1] : NULL))
402*0f1bf1c2SAdrian Chadd 					return -1;
403*0f1bf1c2SAdrian Chadd 				if(callbacks[j].parameter)
404*0f1bf1c2SAdrian Chadd 					i++;
405*0f1bf1c2SAdrian Chadd 				break;
406*0f1bf1c2SAdrian Chadd 			}
407*0f1bf1c2SAdrian Chadd 		if(j == sizeof(callbacks) / sizeof(*callbacks))
408*0f1bf1c2SAdrian Chadd 			return _error(-1, "%s: Unknown parameter", argv[i]);
409*0f1bf1c2SAdrian Chadd 	}
410*0f1bf1c2SAdrian Chadd 	return 0;
411*0f1bf1c2SAdrian Chadd }
412*0f1bf1c2SAdrian Chadd 
_set_apn(char const * ifname,struct umb_parameter * umbp,char const * apn)413*0f1bf1c2SAdrian Chadd static int _set_apn(char const * ifname, struct umb_parameter * umbp,
414*0f1bf1c2SAdrian Chadd 		char const * apn)
415*0f1bf1c2SAdrian Chadd {
416*0f1bf1c2SAdrian Chadd 	umbp->apnlen = _char_to_utf16(apn, umbp->apn, sizeof(umbp->apn));
417*0f1bf1c2SAdrian Chadd 	if(umbp->apnlen < 0 || (size_t)umbp->apnlen > sizeof(umbp->apn))
418*0f1bf1c2SAdrian Chadd 		return _error(-1, "%s: %s", ifname, "APN too long");
419*0f1bf1c2SAdrian Chadd 	return 0;
420*0f1bf1c2SAdrian Chadd }
421*0f1bf1c2SAdrian Chadd 
_set_username(char const * ifname,struct umb_parameter * umbp,char const * username)422*0f1bf1c2SAdrian Chadd static int _set_username(char const * ifname, struct umb_parameter * umbp,
423*0f1bf1c2SAdrian Chadd 		char const * username)
424*0f1bf1c2SAdrian Chadd {
425*0f1bf1c2SAdrian Chadd 	umbp->usernamelen = _char_to_utf16(username, umbp->username,
426*0f1bf1c2SAdrian Chadd 			sizeof(umbp->username));
427*0f1bf1c2SAdrian Chadd 	if(umbp->usernamelen < 0
428*0f1bf1c2SAdrian Chadd 			|| (size_t)umbp->usernamelen > sizeof(umbp->username))
429*0f1bf1c2SAdrian Chadd 		return _error(-1, "%s: %s", ifname, "Username too long");
430*0f1bf1c2SAdrian Chadd 	return 0;
431*0f1bf1c2SAdrian Chadd }
432*0f1bf1c2SAdrian Chadd 
_set_password(char const * ifname,struct umb_parameter * umbp,char const * password)433*0f1bf1c2SAdrian Chadd static int _set_password(char const * ifname, struct umb_parameter * umbp,
434*0f1bf1c2SAdrian Chadd 		char const * password)
435*0f1bf1c2SAdrian Chadd {
436*0f1bf1c2SAdrian Chadd 	umbp->passwordlen = _char_to_utf16(password, umbp->password,
437*0f1bf1c2SAdrian Chadd 			sizeof(umbp->password));
438*0f1bf1c2SAdrian Chadd 	if(umbp->passwordlen < 0
439*0f1bf1c2SAdrian Chadd 			|| (size_t)umbp->passwordlen > sizeof(umbp->password))
440*0f1bf1c2SAdrian Chadd 		return _error(-1, "%s: %s", ifname, "Password too long");
441*0f1bf1c2SAdrian Chadd 	return 0;
442*0f1bf1c2SAdrian Chadd }
443*0f1bf1c2SAdrian Chadd 
_set_pin(char const * ifname,struct umb_parameter * umbp,char const * pin)444*0f1bf1c2SAdrian Chadd static int _set_pin(char const * ifname, struct umb_parameter * umbp,
445*0f1bf1c2SAdrian Chadd 		char const * pin)
446*0f1bf1c2SAdrian Chadd {
447*0f1bf1c2SAdrian Chadd 	umbp->is_puk = 0;
448*0f1bf1c2SAdrian Chadd 	umbp->op = MBIM_PIN_OP_ENTER;
449*0f1bf1c2SAdrian Chadd 	umbp->pinlen = _char_to_utf16(pin, umbp->pin, sizeof(umbp->pin));
450*0f1bf1c2SAdrian Chadd 	if(umbp->pinlen < 0 || (size_t)umbp->pinlen
451*0f1bf1c2SAdrian Chadd 			> sizeof(umbp->pin))
452*0f1bf1c2SAdrian Chadd 		return _error(-1, "%s: %s", ifname, "PIN code too long");
453*0f1bf1c2SAdrian Chadd 	return 0;
454*0f1bf1c2SAdrian Chadd }
455*0f1bf1c2SAdrian Chadd 
_set_puk(char const * ifname,struct umb_parameter * umbp,char const * puk)456*0f1bf1c2SAdrian Chadd static int _set_puk(char const * ifname, struct umb_parameter * umbp,
457*0f1bf1c2SAdrian Chadd 		char const * puk)
458*0f1bf1c2SAdrian Chadd {
459*0f1bf1c2SAdrian Chadd 	umbp->is_puk = 1;
460*0f1bf1c2SAdrian Chadd 	umbp->op = MBIM_PIN_OP_ENTER;
461*0f1bf1c2SAdrian Chadd 	umbp->pinlen = _char_to_utf16(puk, umbp->pin, sizeof(umbp->pin));
462*0f1bf1c2SAdrian Chadd 	if(umbp->pinlen < 0 || (size_t)umbp->pinlen > sizeof(umbp->pin))
463*0f1bf1c2SAdrian Chadd 		return _error(-1, "%s: %s", ifname, "PUK code too long");
464*0f1bf1c2SAdrian Chadd 	return 0;
465*0f1bf1c2SAdrian Chadd }
466*0f1bf1c2SAdrian Chadd 
_set_roaming_allow(char const * ifname,struct umb_parameter * umbp,char const * unused)467*0f1bf1c2SAdrian Chadd static int _set_roaming_allow(char const * ifname, struct umb_parameter * umbp,
468*0f1bf1c2SAdrian Chadd 		char const * unused)
469*0f1bf1c2SAdrian Chadd {
470*0f1bf1c2SAdrian Chadd 	(void) ifname;
471*0f1bf1c2SAdrian Chadd 	(void) unused;
472*0f1bf1c2SAdrian Chadd 
473*0f1bf1c2SAdrian Chadd 	umbp->roaming = 1;
474*0f1bf1c2SAdrian Chadd 	return 0;
475*0f1bf1c2SAdrian Chadd }
476*0f1bf1c2SAdrian Chadd 
_set_roaming_deny(char const * ifname,struct umb_parameter * umbp,char const * unused)477*0f1bf1c2SAdrian Chadd static int _set_roaming_deny(char const * ifname, struct umb_parameter * umbp,
478*0f1bf1c2SAdrian Chadd 		char const * unused)
479*0f1bf1c2SAdrian Chadd {
480*0f1bf1c2SAdrian Chadd 	(void) ifname;
481*0f1bf1c2SAdrian Chadd 	(void) unused;
482*0f1bf1c2SAdrian Chadd 
483*0f1bf1c2SAdrian Chadd 	umbp->roaming = 0;
484*0f1bf1c2SAdrian Chadd 	return 0;
485*0f1bf1c2SAdrian Chadd }
486*0f1bf1c2SAdrian Chadd 
487*0f1bf1c2SAdrian Chadd 
488*0f1bf1c2SAdrian Chadd /* umbctl_socket */
_umbctl_socket(void)489*0f1bf1c2SAdrian Chadd static int _umbctl_socket(void)
490*0f1bf1c2SAdrian Chadd {
491*0f1bf1c2SAdrian Chadd 	int fd;
492*0f1bf1c2SAdrian Chadd 
493*0f1bf1c2SAdrian Chadd 	if((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
494*0f1bf1c2SAdrian Chadd 		return _error(-1, "socket: %s", strerror(errno));
495*0f1bf1c2SAdrian Chadd 	return fd;
496*0f1bf1c2SAdrian Chadd }
497*0f1bf1c2SAdrian Chadd 
498*0f1bf1c2SAdrian Chadd 
499*0f1bf1c2SAdrian Chadd /* usage */
_usage(void)500*0f1bf1c2SAdrian Chadd static int _usage(void)
501*0f1bf1c2SAdrian Chadd {
502*0f1bf1c2SAdrian Chadd 	fputs("Usage: umbctl [-v] ifname [parameter [value]] [...]\n"
503*0f1bf1c2SAdrian Chadd "       umbctl -f config-file ifname\n",
504*0f1bf1c2SAdrian Chadd 			stderr);
505*0f1bf1c2SAdrian Chadd 	return 1;
506*0f1bf1c2SAdrian Chadd }
507*0f1bf1c2SAdrian Chadd 
508*0f1bf1c2SAdrian Chadd 
509*0f1bf1c2SAdrian Chadd /* utf16_to_char */
_utf16_to_char(uint16_t * in,int inlen,char * out,size_t outlen)510*0f1bf1c2SAdrian Chadd static void _utf16_to_char(uint16_t * in, int inlen, char * out, size_t outlen)
511*0f1bf1c2SAdrian Chadd {
512*0f1bf1c2SAdrian Chadd 	uint16_t c;
513*0f1bf1c2SAdrian Chadd 
514*0f1bf1c2SAdrian Chadd 	while (outlen > 0) {
515*0f1bf1c2SAdrian Chadd 		c = inlen > 0 ? htole16(*in) : 0;
516*0f1bf1c2SAdrian Chadd 		if (c == 0 || --outlen == 0) {
517*0f1bf1c2SAdrian Chadd 			/* always NUL terminate result */
518*0f1bf1c2SAdrian Chadd 			*out = '\0';
519*0f1bf1c2SAdrian Chadd 			break;
520*0f1bf1c2SAdrian Chadd 		}
521*0f1bf1c2SAdrian Chadd 		*out++ = isascii(c) ? (char)c : '?';
522*0f1bf1c2SAdrian Chadd 		in++;
523*0f1bf1c2SAdrian Chadd 		inlen--;
524*0f1bf1c2SAdrian Chadd 	}
525*0f1bf1c2SAdrian Chadd }
526*0f1bf1c2SAdrian Chadd 
527*0f1bf1c2SAdrian Chadd 
528*0f1bf1c2SAdrian Chadd /* main */
main(int argc,char * argv[])529*0f1bf1c2SAdrian Chadd int main(int argc, char * argv[])
530*0f1bf1c2SAdrian Chadd {
531*0f1bf1c2SAdrian Chadd 	int o;
532*0f1bf1c2SAdrian Chadd 	char const * filename = NULL;
533*0f1bf1c2SAdrian Chadd 	int verbose = 0;
534*0f1bf1c2SAdrian Chadd 
535*0f1bf1c2SAdrian Chadd 	while((o = getopt(argc, argv, "f:gv")) != -1)
536*0f1bf1c2SAdrian Chadd 		switch(o)
537*0f1bf1c2SAdrian Chadd 		{
538*0f1bf1c2SAdrian Chadd 			case 'f':
539*0f1bf1c2SAdrian Chadd 				filename = optarg;
540*0f1bf1c2SAdrian Chadd 				break;
541*0f1bf1c2SAdrian Chadd 			case 'v':
542*0f1bf1c2SAdrian Chadd 				verbose++;
543*0f1bf1c2SAdrian Chadd 				break;
544*0f1bf1c2SAdrian Chadd 			default:
545*0f1bf1c2SAdrian Chadd 				return _usage();
546*0f1bf1c2SAdrian Chadd 		}
547*0f1bf1c2SAdrian Chadd 	if(optind == argc)
548*0f1bf1c2SAdrian Chadd 		return _usage();
549*0f1bf1c2SAdrian Chadd 	if(filename != NULL)
550*0f1bf1c2SAdrian Chadd 	{
551*0f1bf1c2SAdrian Chadd 		if(optind + 1 != argc)
552*0f1bf1c2SAdrian Chadd 			return _usage();
553*0f1bf1c2SAdrian Chadd 		return _umbctl_file(argv[optind], filename, verbose);
554*0f1bf1c2SAdrian Chadd 	}
555*0f1bf1c2SAdrian Chadd 	return _umbctl(argv[optind], verbose, argc - optind - 1,
556*0f1bf1c2SAdrian Chadd 			&argv[optind + 1]);
557*0f1bf1c2SAdrian Chadd }
558