Debian Codegraph @

hdlcutil/hdrvcomm.c

raw symbols SCIP
1 /*****************************************************************************/
2
3 /*
4 * hdrvcomm.c -- HDLC driver communications.
5 *
6 * Copyright (C) 1996-1998 Thomas Sailer ([email protected])
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 *
23 * Please note that the GPL allows you to use the driver, NOT the radio.
24 * In order to use the radio, you need a license from the communications
25 * authority of your country.
26 *
27 *
28 * History:
29 * 0.1 10.05.97 Started
30 * 0.2 14.04.98 Tried to implement AF_PACKET
31 */
32
33 /*****************************************************************************/
34
35 #include <stdio.h>
36 #include <string.h>
37 #include <signal.h>
38 #include <errno.h>
39 #include <sys/ioctl.h>
40 #include <sys/types.h>
41 #include <sys/ipc.h>
42 #include <sys/msg.h>
43 #include <endian.h>
44 #include <netinet/in.h>
45 #include <fcntl.h>
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <net/if.h>
49 #include "hdrvcomm.h"
50 #include "usersmdiag.h"
51 #include <net/if_arp.h>
52
53 #include <net/ethernet.h>
54
55 #include <linux/if_packet.h>
56 #ifndef SOL_PACKET
57 #define SOL_PACKET 263
58 #endif
59
60 /* ---------------------------------------------------------------------- */
61
62 static const char *if_name = "bcsf0";
63 static char *prg_name;
64 static int fd = -1;
65 static struct ifreq ifr_h;
66 static int promisc;
67 static int afpacket = 1;
68 static int msqid = -1;
69
70 /* ---------------------------------------------------------------------- */
71
72 static void terminate(void)
73 {
74 if (ioctl(fd, SIOCSIFFLAGS, &ifr_h) < 0) {
75 perror("ioctl (SIOCSIFFLAGS)");
76 exit(1);
77 }
78 exit(0);
79 }
80
81 /* ---------------------------------------------------------------------- */
82
83 static void terminate_sig(int signal)
84 {
85 printf("signal %i caught\n", signal);
86 terminate();
87 }
88
89 /* ---------------------------------------------------------------------- */
90
91 int hdrvc_recvpacket(char *pkt, int maxlen)
92 {
93 struct ifreq ifr_new;
94 struct sockaddr_ll from;
95 socklen_t from_len = sizeof(from);
96
97 if (!promisc) {
98 if (afpacket) {
99 struct sockaddr_ll sll;
100 struct packet_mreq mr;
101
102 memset(&sll, 0, sizeof(sll));
103 sll.sll_family = AF_PACKET;
104 sll.sll_ifindex = ifr_h.ifr_ifindex;
105 sll.sll_protocol = htons(ETH_P_AX25);
106 if (bind(fd, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
107 fprintf(stderr,
108 "%s: Error %s (%i) bind failed\n",
109 prg_name, strerror(errno), errno);
110 exit(-2);
111 }
112 memset(&mr, 0, sizeof(mr));
113 mr.mr_ifindex = sll.sll_ifindex;
114 mr.mr_type = PACKET_MR_PROMISC;
115 if (setsockopt(fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, (char *)&mr, sizeof(mr)) < 0) {
116 fprintf(stderr,
117 "%s: Error %s (%i) setsockopt SOL_PACKET, PACKET_ADD_MEMBERSHIP failed\n",
118 prg_name, strerror(errno), errno);
119 exit(-2);
120 }
121 } else {
122 struct sockaddr sa;
123
124 strcpy(sa.sa_data, if_name);
125 sa.sa_family = AF_INET;
126 if (bind(fd, &sa, sizeof(sa)) < 0) {
127 fprintf(stderr,
128 "%s: Error %s (%i) bind failed\n",
129 prg_name, strerror(errno), errno);
130 exit(-2);
131 }
132 ifr_new = ifr_h;
133 ifr_new.ifr_flags |= IFF_PROMISC;
134 if (ioctl(fd, SIOCSIFFLAGS, &ifr_new) < 0) {
135 perror("ioctl (SIOCSIFFLAGS)");
136 exit(1);
137 }
138 signal(SIGTERM, terminate_sig);
139 signal(SIGQUIT, terminate_sig);
140 if (atexit((void (*)(void))terminate)) {
141 perror("atexit");
142 terminate();
143 }
144 }
145 promisc = 1;
146 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
147 }
148 if (!pkt || maxlen < 2)
149 return 0;
150 return recvfrom(fd, pkt, maxlen, 0, (struct sockaddr *)&from, &from_len);
151 return -1;
152 }
153
154 /* ---------------------------------------------------------------------- */
155
156 int hdrvc_getfd(void)
157 {
158 return fd;
159 }
160
161 /* ---------------------------------------------------------------------- */
162
163 const char *hdrvc_ifname(void)
164 {
165 return if_name;
166 }
167
168 /* ---------------------------------------------------------------------- */
169
170 void hdrvc_args(int *argc, char *argv[], const char *def_if)
171 {
172 int ac, i;
173
174 if (def_if)
175 if_name = def_if;
176 if (!argc || !argv || (ac = *argc) < 1)
177 return;
178 prg_name = argv[0];
179 for (i = 1; i < ac-1; i++) {
180 if (!strcmp(argv[i], "-i")) {
181 if_name = argv[i+1];
182 ac -= 2;
183 if (i < ac)
184 memmove(argv+i, argv+i+2, (ac-i) * sizeof(void *));
185 i--;
186 }
187 }
188 *argc = ac;
189 }
190
191 /* ---------------------------------------------------------------------- */
192
193 void hdrvc_init(void)
194 {
195 key_t k;
196 static struct ifreq ifr;
197
198 strcpy(ifr_h.ifr_name, if_name);
199 /* first try to use AF_PACKET */
200 if ((fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_AX25))) < 0
201 || ioctl(fd, SIOCGIFINDEX, &ifr_h) < 0) {
202 if (fd >= 0)
203 close(fd);
204 afpacket = 0;
205 fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_AX25));
206 if (fd < 0) {
207 fprintf(stderr, "%s: Error %s (%i), cannot open %s\n",
208 prg_name,
209 strerror(errno), errno, if_name);
210 exit(-1);
211 }
212 if (ioctl(fd, SIOCGIFFLAGS, &ifr_h) < 0) {
213 fprintf(stderr, "%s: Error %s (%i), cannot ioctl SIOCGIFFLAGS %s\n",
214 prg_name,
215 strerror(errno), errno, if_name);
216 exit(-1);
217 }
218 }
219 strcpy(ifr.ifr_name, if_name);
220 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0 ) {
221 fprintf(stderr, "%s: Error %s (%i), cannot ioctl SIOCGIFHWADDR %s\n",
222 prg_name,
223 strerror(errno), errno, if_name);
224 exit(-1);
225 }
226 if (ifr.ifr_hwaddr.sa_family != ARPHRD_AX25) {
227 fprintf(stderr, "%s: Error, interface %s not AX25 (%i)\n",
228 prg_name,
229 if_name, ifr.ifr_hwaddr.sa_family);
230 exit(-1);
231 }
232 return;
233 k = ftok(if_name, USERSM_KEY_PROJ);
234 if (k == (key_t)-1) {
235 fprintf(stderr, "%s: Error %s (%i), cannot ftok on %s\n", prg_name,
236 strerror(errno), errno, if_name);
237 exit(-1);
238 }
239 msqid = msgget(k, 0700);
240 if (msqid < 0) {
241 fprintf(stderr, "%s: Error %s (%i), cannot msgget %d\n", prg_name,
242 strerror(errno), errno, k);
243 exit(-1);
244 }
245 }
246
247 /* ---------------------------------------------------------------------- */
248
249 static void hdrvc_sendmsg(struct usersmmsg *msg, int len)
250 {
251 if (msgsnd(msqid, (struct msgbuf *)msg, len+sizeof(msg->hdr)-sizeof(long), 0) < 0) {
252 perror("msgsnd");
253 exit(1);
254 }
255 }
256
257 static int hdrvc_recvmsg(struct usersmmsg *msg, int maxlen, long type)
258 {
259 int len;
260
261 len = msgrcv(msqid, (struct msgbuf *)msg, maxlen - sizeof(long), type,
262 MSG_NOERROR);
263 if (len < 0) {
264 perror("msgrcv");
265 exit(1);
266 }
267 #if 0
268 for (;;) {
269 if ((len = msgrcv(msqid, (struct msgbuf *)msg, maxlen-sizeof(long), type, IPC_NOWAIT|MSG_NOERROR)) >= 0)
270 return len+sizeof(long);
271 if (errno != ENOMSG) {
272 perror("msgrcv");
273 exit(1);
274 }
275 usleep(250000);
276 }
277 #endif
278 return len+sizeof(long);
279 }
280
281 /* ---------------------------------------------------------------------- */
282
283
284 int hdrvc_hdlcdrv_ioctl(int cmd, struct hdlcdrv_ioctl *par)
285 {
286 struct ifreq ifr = ifr_h;
287
288 errno = EINVAL;
289 return -1;
290 ifr.ifr_data = (caddr_t)par;
291 par->cmd = cmd;
292 return ioctl(fd, SIOCDEVPRIVATE, &ifr);
293 }
294
295 /* ---------------------------------------------------------------------- */
296
297 int hdrvc_sm_ioctl(int cmd, struct sm_ioctl *par)
298 {
299 struct ifreq ifr = ifr_h;
300
301 errno = EINVAL;
302 return -1;
303 ifr.ifr_data = (caddr_t)par;
304 par->cmd = cmd;
305 return ioctl(fd, SIOCDEVPRIVATE, &ifr);
306 }
307
308 /* ---------------------------------------------------------------------- */
309
310 int hdrvc_baycom_ioctl(int cmd, struct baycom_ioctl *par)
311 {
312 struct ifreq ifr = ifr_h;
313
314 errno = EINVAL;
315 return -1;
316 ifr.ifr_data = (caddr_t)par;
317 par->cmd = cmd;
318 return ioctl(fd, SIOCDEVPRIVATE, &ifr);
319 }
320
321 /* ---------------------------------------------------------------------- */
322
323 unsigned int hdrvc_get_ifflags(void)
324 {
325 struct ifreq ifr;
326
327 memcpy(&ifr, &ifr_h, sizeof(ifr));
328 if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
329 perror("ioctl: SIOCGIFFLAGS");
330 exit(-1);
331 }
332 return ifr.ifr_flags;
333 return IFF_UP | IFF_RUNNING;
334 }
335
336 /* ---------------------------------------------------------------------- */
337
338 int hdrvc_diag(struct sm_diag_data *diag)
339 {
340 struct sm_ioctl smi;
341 int ret;
342 struct usersmmsg msg;
343
344 if (!diag) {
345 errno = EINVAL;
346 return -1;
347 }
348 memcpy(&smi.data.diag, diag, sizeof(smi.data.diag));
349 ret = hdrvc_sm_ioctl(SMCTL_DIAGNOSE, &smi);
350 memcpy(diag, &smi.data.diag, sizeof(smi.data.diag));
351 return ret;
352 msg.hdr.type = USERSM_CMD_REQ_DIAG;
353 msg.hdr.channel = 0;
354 msg.data.diag.mode = diag->mode;
355 msg.data.diag.flags = diag->flags;
356 msg.data.diag.samplesperbit = diag->samplesperbit;
357 msg.data.diag.datalen = diag->datalen;
358 hdrvc_sendmsg(&msg, sizeof(msg.data.diag));
359 ret = hdrvc_recvmsg(&msg, sizeof(msg), USERSM_CMD_ACK_DIAG);
360 if (ret < 0)
361 return ret;
362 if (ret < sizeof(msg.data.diag) || ret < sizeof(msg.data.diag)+msg.data.diag.datalen*sizeof(short)) {
363 errno = EIO;
364 return -1;
365 }
366 diag->mode = msg.data.diag.mode;
367 diag->flags = msg.data.diag.flags;
368 diag->samplesperbit = msg.data.diag.samplesperbit;
369 diag->datalen = msg.data.diag.datalen;
370 memcpy(diag->data, msg.data.diag_out.samples, msg.data.diag.datalen*sizeof(short));
371 return 0;
372 }
373
374
375 /* ---------------------------------------------------------------------- */
376
377 int hdrvc_get_samples(void)
378 {
379 int ret;
380 struct hdlcdrv_ioctl bi;
381
382 ret = hdrvc_hdlcdrv_ioctl(HDLCDRVCTL_GETSAMPLES, &bi);
383 if (ret < 0)
384 return ret;
385 return bi.data.bits & 0xff;
386 errno = EAGAIN;
387 return -1;
388 }
389
390 /* ---------------------------------------------------------------------- */
391
392 int hdrvc_get_bits(void)
393 {
394 int ret;
395 struct hdlcdrv_ioctl bi;
396
397 ret = hdrvc_hdlcdrv_ioctl(HDLCDRVCTL_GETBITS, &bi);
398 if (ret < 0)
399 return ret;
400 return bi.data.bits & 0xff;
401 errno = EAGAIN;
402 return -1;
403 }
404
405 /* ---------------------------------------------------------------------- */
406
407 int hdrvc_get_channel_access_param(struct hdrvc_channel_params *par)
408 {
409 struct hdlcdrv_ioctl hi;
410 int ret;
411 struct usersmmsg msg;
412
413 ret = hdrvc_hdlcdrv_ioctl(HDLCDRVCTL_GETCHANNELPAR, &hi);
414 if (ret < 0)
415 return ret;
416 if (!par) {
417 errno = EINVAL;
418 return -1;
419 }
420 par->tx_delay = hi.data.cp.tx_delay;
421 par->tx_tail = hi.data.cp.tx_tail;
422 par->slottime = hi.data.cp.slottime;
423 par->ppersist = hi.data.cp.ppersist;
424 par->fulldup = hi.data.cp.fulldup;
425 return 0;
426 msg.hdr.type = USERSM_CMD_REQ_CHACCESS_PAR;
427 msg.hdr.channel = 0;
428 hdrvc_sendmsg(&msg, 0);
429 ret = hdrvc_recvmsg(&msg, sizeof(msg), USERSM_CMD_ACK_CHACCESS_PAR);
430 if (ret < 0)
431 return ret;
432 if (ret < sizeof(msg.data.cp)) {
433 errno = EIO;
434 return -1;
435 }
436 par->tx_delay = msg.data.cp.tx_delay;
437 par->tx_tail = msg.data.cp.tx_tail;
438 par->slottime = msg.data.cp.slottime;
439 par->ppersist = msg.data.cp.ppersist;
440 par->fulldup = msg.data.cp.fulldup;
441 return 0;
442 }
443
444 /* ---------------------------------------------------------------------- */
445
446 int hdrvc_set_channel_access_param(struct hdrvc_channel_params par)
447 {
448 int ret;
449 struct usersmmsg msg;
450
451 struct hdlcdrv_ioctl hi;
452
453 hi.data.cp.tx_delay = par.tx_delay;
454 hi.data.cp.tx_tail = par.tx_tail;
455 hi.data.cp.slottime = par.slottime;
456 hi.data.cp.ppersist = par.ppersist;
457 hi.data.cp.fulldup = par.fulldup;
458 ret = hdrvc_hdlcdrv_ioctl(HDLCDRVCTL_SETCHANNELPAR, &hi);
459 if (ret < 0)
460 return ret;
461 return 0;
462 msg.hdr.type = USERSM_CMD_SET_CHACCESS_PAR;
463 msg.hdr.channel = 0;
464 msg.data.cp.tx_delay = par.tx_delay;
465 msg.data.cp.tx_tail = par.tx_tail;
466 msg.data.cp.slottime = par.slottime;
467 msg.data.cp.ppersist = par.ppersist;
468 msg.data.cp.fulldup = par.fulldup;
469 hdrvc_sendmsg(&msg, sizeof(msg.data.cp));
470 return 0;
471 }
472
473 /* ---------------------------------------------------------------------- */
474
475 int hdrvc_calibrate(int calib)
476 {
477 struct hdlcdrv_ioctl bhi;
478 struct usersmmsg msg;
479
480 bhi.data.calibrate = calib;
481 return hdrvc_hdlcdrv_ioctl(HDLCDRVCTL_CALIBRATE, &bhi);
482 msg.hdr.type = USERSM_CMD_CALIBRATE;
483 msg.hdr.channel = 0;
484 msg.data.calib = calib;
485 hdrvc_sendmsg(&msg, sizeof(msg.data.calib));
486 return 0;
487 }
488
489 /* ---------------------------------------------------------------------- */
490
491 int hdrvc_get_channel_state(struct hdrvc_channel_state *st)
492 {
493 struct hdlcdrv_ioctl bhi;
494 int ret;
495 struct usersmmsg msg;
496
497 if (!st) {
498 errno = EINVAL;
499 return -1;
500 }
501 ret = hdrvc_hdlcdrv_ioctl(HDLCDRVCTL_GETSTAT, &bhi);
502 if (ret >= 0) {
503 st->ptt = bhi.data.cs.ptt;
504 st->dcd = bhi.data.cs.dcd;
505 st->ptt_keyed = bhi.data.cs.ptt_keyed;
506 st->tx_packets = bhi.data.cs.tx_packets;
507 st->tx_errors = bhi.data.cs.tx_errors;
508 st->rx_packets = bhi.data.cs.rx_packets;
509 st->rx_errors = bhi.data.cs.rx_errors;
510 }
511 return ret;
512 msg.hdr.type = USERSM_CMD_REQ_CHANNELSTATE;
513 msg.hdr.channel = 0;
514 hdrvc_sendmsg(&msg, 0);
515 ret = hdrvc_recvmsg(&msg, sizeof(msg), USERSM_CMD_ACK_CHANNELSTATE);
516 if (ret < 0)
517 return ret;
518 if (ret < sizeof(msg.data.cs)) {
519 errno = EIO;
520 return -1;
521 }
522 st->ptt = msg.data.cs.ptt;
523 st->dcd = msg.data.cs.dcd;
524 st->ptt_keyed = msg.data.cs.ptt_keyed;
525 st->tx_packets = msg.data.cs.tx_packets;
526 st->tx_errors = msg.data.cs.tx_errors;
527 st->rx_packets = msg.data.cs.rx_packets;
528 st->rx_errors = msg.data.cs.rx_errors;
529 return 0;
530 }
531
532 /* ---------------------------------------------------------------------- */
533
534 int hdrvc_diag2(unsigned int mode, unsigned int flags, short *data,
535 unsigned int maxdatalen, unsigned int *samplesperbit)
536 {
537 int ret;
538 struct usersmmsg msg;
539 static unsigned int modeconvusersm[4] = {
540 USERSM_DIAGMODE_OFF, USERSM_DIAGMODE_INPUT, USERSM_DIAGMODE_DEMOD,
541 USERSM_DIAGMODE_CONSTELLATION
542 };
543
544 if (mode > HDRVC_DIAGMODE_CONSTELLATION) {
545 errno = EINVAL;
546 return -1;
547 }
548 struct sm_ioctl smi;
549 static unsigned int modeconvsm[4] = {
550 SM_DIAGMODE_OFF, SM_DIAGMODE_INPUT, SM_DIAGMODE_DEMOD,
551 SM_DIAGMODE_CONSTELLATION
552 };
553
554 smi.data.diag.mode = modeconvsm[mode];
555 smi.data.diag.flags = (flags & HDRVC_DIAGFLAG_DCDGATE) ? SM_DIAGFLAG_DCDGATE : 0;
556 smi.data.diag.samplesperbit = 0;
557 smi.data.diag.datalen = maxdatalen;
558 smi.data.diag.data = data;
559 ret = hdrvc_sm_ioctl(SMCTL_DIAGNOSE, &smi);
560 if (ret < 0)
561 return ret;
562 if (samplesperbit)
563 *samplesperbit = smi.data.diag.samplesperbit;
564 if (smi.data.diag.mode != modeconvsm[mode] || !(smi.data.diag.flags & SM_DIAGFLAG_VALID))
565 return 0;
566 return smi.data.diag.datalen;
567 msg.hdr.type = USERSM_CMD_REQ_DIAG;
568 msg.hdr.channel = 0;
569 msg.data.diag.mode = modeconvusersm[mode];
570 msg.data.diag.flags = (flags & HDRVC_DIAGFLAG_DCDGATE) ? USERSM_DIAGFLAG_DCDGATE : 0;
571 msg.data.diag.samplesperbit = 0;
572 msg.data.diag.datalen = maxdatalen;
573 hdrvc_sendmsg(&msg, sizeof(msg.data.diag));
574 ret = hdrvc_recvmsg(&msg, sizeof(msg), USERSM_CMD_ACK_DIAG);
575 if (ret < 0)
576 return ret;
577 if (ret < sizeof(msg.data.diag) || ret < sizeof(msg.data.diag)+msg.data.diag.datalen*sizeof(short)) {
578 errno = EIO;
579 return -1;
580 }
581 if (samplesperbit)
582 *samplesperbit = msg.data.diag.samplesperbit;
583 if (msg.data.diag.mode != modeconvusersm[mode] || !(msg.data.diag.flags & USERSM_DIAGFLAG_VALID))
584 return 0;
585 if (!data || msg.data.diag.datalen <= 0)
586 return 0;
587 memcpy(data, msg.data.diag_out.samples, msg.data.diag.datalen*sizeof(short));
588 return msg.data.diag.datalen;
589 }
590
591 /* ---------------------------------------------------------------------- */
592
593 int hdrvc_get_driver_name(char *buf, int bufsz)
594 {
595 int ret;
596 struct usersmmsg msg;
597
598 struct hdlcdrv_ioctl bhi;
599
600 ret = hdrvc_hdlcdrv_ioctl(HDLCDRVCTL_DRIVERNAME, &bhi);
601 if (ret < 0)
602 return ret;
603 strncpy(buf, bhi.data.modename, bufsz);
604 return 0;
605 msg.hdr.type = USERSM_CMD_REQ_DRVNAME;
606 msg.hdr.channel = 0;
607 hdrvc_sendmsg(&msg, 0);
608 ret = hdrvc_recvmsg(&msg, sizeof(msg), USERSM_CMD_ACK_DRVNAME);
609 if (ret < 0)
610 return ret;
611 if (ret < 1) {
612 errno = EIO;
613 return -1;
614 }
615 if (bufsz < ret)
616 ret = bufsz;
617 strncpy(buf, msg.data.by, ret);
618 buf[ret-1] = 0;
619 return 0;
620 }
621
622 /* ---------------------------------------------------------------------- */
623
624 int hdrvc_get_mode_name(char *buf, int bufsz)
625 {
626 int ret;
627 struct usersmmsg msg;
628
629 struct hdlcdrv_ioctl bhi;
630
631 ret = hdrvc_hdlcdrv_ioctl(HDLCDRVCTL_GETMODE, &bhi);
632 if (ret < 0)
633 return ret;
634 strncpy(buf, bhi.data.modename, bufsz);
635 return 0;
636 msg.hdr.type = USERSM_CMD_REQ_DRVMODE;
637 msg.hdr.channel = 0;
638 hdrvc_sendmsg(&msg, 0);
639 ret = hdrvc_recvmsg(&msg, sizeof(msg), USERSM_CMD_ACK_DRVMODE);
640 if (ret < 0)
641 return ret;
642 if (ret < 1) {
643 errno = EIO;
644 return -1;
645 }
646 if (bufsz < ret)
647 ret = bufsz;
648 strncpy(buf, msg.data.by, ret);
649 buf[ret-1] = 0;
650 return 0;
651 }
652
653 /* ---------------------------------------------------------------------- */