aboutsummaryrefslogtreecommitdiffstats
path: root/src/kissattach.c
blob: 74d4efb960a4fda83aee3414c7047afee5315aea (plain)
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
// Taken from ax25-tools 0.0.10-rc4 kissattach.c
// SPDX-License-Identifier: GPL-2.0-only
#include <stdio.h>
#define __USE_XOPEN
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <signal.h>
#include <ctype.h>
#include <netdb.h>
#include <syslog.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/ioctl.h>

#include <net/if.h>
#include <netax25/ax25.h>
#include <netrose/rose.h>

#include <netax25/daemon.h>
#include <netax25/axlib.h>
#include <netax25/ttyutils.h>


static int setifcall(int fd, char *name)
{
    char call[7];

    if (ax25_aton_entry(name, call) == -1)
        return FALSE;

    if (ioctl(fd, SIOCSIFHWADDR, call) != 0) {
        close(fd);
        perror("SIOCSIFHWADDR");
        return FALSE;
    }

    return TRUE;
}


static int startiface(char *dev, int mtu, int allow_broadcast)
{
    struct ifreq ifr;
    int fd;

    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        perror("socket");
        return FALSE;
    }

    strcpy(ifr.ifr_name, dev);

    ifr.ifr_mtu = mtu;

    if (ioctl(fd, SIOCSIFMTU, &ifr) < 0) {
        perror("SIOCSIFMTU");
        return FALSE;
    }

    if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
        perror("SIOCGIFFLAGS");
        return FALSE;
    }

    ifr.ifr_flags &= IFF_NOARP;
    ifr.ifr_flags |= IFF_UP;
    ifr.ifr_flags |= IFF_RUNNING;
    if (allow_broadcast)
        ifr.ifr_flags |= IFF_BROADCAST; /* samba broadcasts are a pain.. */
    else
        ifr.ifr_flags &= ~(IFF_BROADCAST); /* samba broadcasts are a pain.. */

    if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
        perror("SIOCSIFFLAGS");
        return FALSE;
    }

    close(fd);

    return TRUE;
}

int32_t kissattach(
        char *callsign,
        int32_t speed,
        int32_t mtu,
        char *kttyname,
        int32_t allow_broadcast)
{
    int  fd;
    int  disc = N_AX25;
    char dev[64];

    if ((fd = open(kttyname, O_RDONLY | O_NONBLOCK)) == -1) {
        perror("open");
        return 0;
    }

    if (speed != 0 && !tty_speed(fd, speed))
        return 0;

    if (ioctl(fd, TIOCSETD, &disc) == -1) {
        //Error setting line discipline
        perror("TIOCSETD");
        fprintf(stderr, "Are you sure you have enabled %s support in the kernel\n",
                disc == N_AX25 ? "MKISS" : "6PACK");
        fprintf(stderr, "or, if you made it a module, that the module is loaded?\n");
        return 0;
    }

    if (ioctl(fd, SIOCGIFNAME, dev) == -1) {
        perror("SIOCGIFNAME");
        return 0;
    }

    if (!setifcall(fd, callsign))
        return 0;

    /* Now set the encapsulation */
    int v = 4;
    if (ioctl(fd, SIOCSIFENCAP, &v) == -1) {
        perror("SIOCSIFENCAP");
        return 0;
    }

    /* ax25 ifaces should not really need to have an IP address assigned to */
    if (!startiface(dev, mtu, allow_broadcast))
        return 0;

    return 1;
}