1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
/*
* uqmid -- implement a daemon based on the uqmi idea
*
* Copyright (C) 2023-2024 Alexander Couzens <lynxis@fe80.eu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA.
*/
/** Interface with the linux kernel module */
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <libgen.h>
#include <linux/limits.h>
#include <talloc.h>
#include <sys/ioctl.h>
#include <linux/if_ether.h>
#include <linux/if.h>
#include <linux/sockios.h>
#include "wwan.h"
#include "logging.h"
#include "modem.h"
static int get_real_device(const char *cdc_device_path, char *real_path, size_t real_path_size)
{
ssize_t ret = readlink(cdc_device_path, real_path, real_path_size - 1);
if (ret == -1) {
strncpy(real_path, cdc_device_path, real_path_size - 1);
} else if (ret == real_path_size - 1) {
real_path[real_path_size - 1] = 0;
}
return 0;
}
static int get_real_device_name(const char *cdc_device_path, char *real_name, size_t real_name_size)
{
char real_device[PATH_MAX];
char *base;
get_real_device(cdc_device_path, real_device, PATH_MAX);
base = basename(real_device);
strncpy(real_name, base, real_name_size - 1);
return 0;
}
static int _get_wwan_device(const char *sysfs_path,
char *wwan_device, size_t wwan_device_size)
{
DIR *dirfd;
int found = 0;
struct dirent *e;
dirfd = opendir(sysfs_path);
if (!dirfd) {
return -1;
}
while ((e = readdir(dirfd)) != NULL)
{
if (!strncmp(e->d_name, ".", strlen(e->d_name)) ||
!strncmp(e->d_name, "..", strlen(e->d_name)))
continue;
if (found == 0)
strncpy(wwan_device, e->d_name, wwan_device_size - 1);
found++;
}
closedir(dirfd);
return found;
}
/**
*
* @param modem
* @param wwan_device result if found
* @param wwan_device_size
* @return 0 on success
*/
int wwan_refresh_device(struct modem *modem)
{
char sysfs_path[PATH_MAX];
char cdcname[128];
char wwan_device[17];
int ret;
get_real_device_name(modem->device, cdcname, sizeof(cdcname));
TALLOC_FREE(modem->wwan.dev);
/*
* usbmisc >= 3.6
* usb < 3.6
*/
snprintf(&sysfs_path[0], sizeof(sysfs_path)-1, "/sys/class/%s/%s/device/net/", "usbmisc", cdcname);
ret = _get_wwan_device(sysfs_path, wwan_device, sizeof(wwan_device));
if (ret >= 1) {
snprintf(sysfs_path, sizeof(sysfs_path)-1, "/sys/class/%s/%s/device/net/%s", "usbmisc", cdcname, wwan_device);
modem->wwan.dev = talloc_strdup(modem, wwan_device);
modem->wwan.sysfs = talloc_strdup(modem, sysfs_path);
modem->subsystem_name = "usbmisc";
return 0;
}
snprintf(sysfs_path, sizeof(sysfs_path)-1, "/sys/class/%s/%s/device/net/", "usb", wwan_device);
ret = _get_wwan_device(sysfs_path, wwan_device, sizeof(wwan_device));
if (ret >= 1) {
snprintf(sysfs_path, sizeof(sysfs_path)-1, "/sys/class/%s/%s/device/net/%s", "usbmisc", cdcname, wwan_device);
modem->wwan.dev = talloc_strdup(modem, wwan_device);
modem->wwan.sysfs = talloc_strdup(modem, sysfs_path);
modem->subsystem_name = "usb";
return 0;
}
return -1;
}
/* read_uint_from_file from fstools under GPLv2 */
static int
read_char_from_file(const char *dirname, const char *filename, char *achar)
{
FILE *f;
char fname[PATH_MAX];
int ret = -1;
snprintf(fname, sizeof(fname), "%s/%s", dirname, filename);
f = fopen(fname, "r");
if (!f)
return ret;
if (fread(achar, 1, 1, f) == 1)
ret = 0;
fclose(f);
return ret;
}
static int
write_char_to_file(const char *dirname, const char *filename, const char *achar)
{
FILE *f;
char fname[PATH_MAX];
int ret = -1;
snprintf(fname, sizeof(fname), "%s/%s", dirname, filename);
f = fopen(fname, "w");
if (!f)
return ret;
if (fwrite(achar, 1, 1, f) == 1)
ret = 0;
fclose(f);
return ret;
}
/* use ioctl until uqmid has netlink support */
int wwan_set_mtu(const char *netif, unsigned int mtu)
{
int sock, rc;
struct ifreq req;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
return sock;
memset(&req, 0, sizeof req);
strncpy(req.ifr_name, netif, sizeof(req.ifr_name));
rc = ioctl(sock, SIOCGIFMTU, &req);
if (rc < 0) {
close(sock);
return rc;
}
if (req.ifr_mtu == mtu) {
close(sock);
return 0;
}
req.ifr_mtu = mtu;
rc = ioctl(sock, SIOCSIFMTU, &req);
close(sock);
return rc;
}
/* use ioctl until uqmid has netlink support */
int wwan_ifupdown(const char *netif, bool up)
{
int sock, rc;
struct ifreq req;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0)
return sock;
memset(&req, 0, sizeof req);
strncpy(req.ifr_name, netif, sizeof(req.ifr_name));
rc = ioctl(sock, SIOCGIFFLAGS, &req);
if (rc < 0) {
close(sock);
return rc;
}
if ((req.ifr_flags & IFF_UP) == up) {
close(sock);
return 0;
}
if (up)
req.ifr_flags |= IFF_UP;
else
req.ifr_flags &= ~IFF_UP;
rc = ioctl(sock, SIOCSIFFLAGS, &req);
close(sock);
return rc;
}
int wwan_read_configuration(const char *sysfs_path, struct wwan_conf *config)
{
char tmp = 0;
int ret;
char qmi_path[PATH_MAX];
snprintf(qmi_path, sizeof(qmi_path) - 1, "%s/qmi", sysfs_path);
ret = read_char_from_file(qmi_path, "raw_ip", &tmp);
if (ret)
return -ENOENT;
config->raw_ip = (tmp == 'Y');
ret = read_char_from_file(qmi_path, "pass_through", &tmp);
if (ret)
return -ENOENT;
config->pass_through = (tmp == 'Y');
return 0;
}
/**
*
* @param sysfs_path path to the network sysfs path
* @param config
* @return 0 on success
*/
int
wwan_set_configuration(const char *sysfs_path, const struct wwan_conf *config)
{
struct wwan_conf old = { 0 };
int ret;
char yes = 'Y';
char no = 'N';
char qmi_path[PATH_MAX];
snprintf(qmi_path, sizeof(qmi_path) - 1, "%s/qmi", sysfs_path);
ret = wwan_read_configuration(sysfs_path, &old);
if (ret) {
return -ENOENT;
}
if (config->raw_ip != old.raw_ip) {
ret = write_char_to_file(qmi_path, "raw_ip", config->raw_ip ? &yes : &no);
if (ret) {
return -EINVAL;
}
}
if (config->pass_through != old.pass_through) {
ret = write_char_to_file(qmi_path, "pass_through", config->pass_through ? &yes : &no);
if (ret) {
return -EINVAL;
}
}
return 0;
}
|