| 1 |
/* |
| 2 |
* this is a port of wampes ethertap with my extension for bpqether |
| 3 |
* |
| 4 |
* (c) 20020630 Thomas Osterried dl9sau |
| 5 |
* License: GPL |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <errno.h> |
| 9 |
#include <fcntl.h> |
| 10 |
#include <limits.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <string.h> |
| 13 |
#include <syslog.h> |
| 14 |
#include <unistd.h> |
| 15 |
#include <sys/ioctl.h> |
| 16 |
#include <sys/socket.h> |
| 17 |
#include <sys/stat.h> |
| 18 |
#include <sys/types.h> |
| 19 |
|
| 20 |
#ifdef linux |
| 21 |
|
| 22 |
#include <linux/version.h> |
| 23 |
#ifndef KERNEL_VERSION |
| 24 |
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c)) |
| 25 |
#endif |
| 26 |
#ifndef LINUX_VERSION_CODE |
| 27 |
#define LINUX_VERSION_CODE KERNEL_VERSION(2,4,0)-1 |
| 28 |
#endif |
| 29 |
|
| 30 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0) |
| 31 |
#define TRY_TUNTAP 1 |
| 32 |
#include <net/if.h> |
| 33 |
#include <linux/if_tun.h> |
| 34 |
#else |
| 35 |
#include <linux/if.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#endif /* linux */ |
| 39 |
|
| 40 |
#include "ax25ipd.h" |
| 41 |
|
| 42 |
#define ETAP_MTU_MIN 256 |
| 43 |
#define ETAP_MTU 1024 |
| 44 |
#define ETAP_MTU_MAX 1500 |
| 45 |
#ifndef MAX_FRAME |
| 46 |
#define MAX_FRAME 2048 |
| 47 |
#endif |
| 48 |
|
| 49 |
#define ETHERTAP_HEADER_LEN_TUN 18 |
| 50 |
#define ETHERTAP_HEADER_LEN_ETHERTAP 16 |
| 51 |
#define ETHERTAP_HEADER_LEN_MAX ETHERTAP_HEADER_LEN_TUN |
| 52 |
|
| 53 |
struct ethertap_packet { |
| 54 |
char ethernet_header[18]; |
| 55 |
char data[MAX_FRAME]; |
| 56 |
}; |
| 57 |
|
| 58 |
static unsigned char hwaddr_remote[6]; |
| 59 |
|
| 60 |
static int ethertap_header_len; |
| 61 |
|
| 62 |
/*---------------------------------------------------------------------------*/ |
| 63 |
|
| 64 |
int send_bpq(unsigned char *buf, int l) |
| 65 |
{ |
| 66 |
struct ethertap_packet ethertap_packet; |
| 67 |
void *addr = ðertap_packet; |
| 68 |
int offset = ETHERTAP_HEADER_LEN_MAX - ethertap_header_len; |
| 69 |
|
| 70 |
static const unsigned char ethernet_header[18] = { |
| 71 |
0x00, 0x00, 0x00, 0x02, /* ??? ??? ETH_P_AX25 (16bit) */ |
| 72 |
0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, /* Destination address (kernel et |
| 73 |
hertap module) */ |
| 74 |
0xfe, 0xfe, 0x00, 0x00, 0x00, 0x00, /* Source address (WAMPES etherta |
| 75 |
p module) */ |
| 76 |
0x08, 0xff /* Protocol (bpqether) */ |
| 77 |
}; |
| 78 |
|
| 79 |
if (l <= 0) |
| 80 |
return -1; |
| 81 |
|
| 82 |
if (l > (sizeof(ethertap_packet.data) - 2)) |
| 83 |
l = sizeof(ethertap_packet.data); |
| 84 |
memcpy(ethertap_packet.data + 2, buf, l); |
| 85 |
memcpy(ethertap_packet.ethernet_header, (const char *) ethernet_header, sizeof(ethernet_header)); |
| 86 |
memcpy(ethertap_packet.ethernet_header + 4, hwaddr_remote, 6); |
| 87 |
memcpy(ethertap_packet.ethernet_header + 4 + 6 + 2, hwaddr_remote +2, 6 -2); |
| 88 |
ethertap_packet.data[0] = (l + 5) % 256; |
| 89 |
ethertap_packet.data[1] = (l + 5) / 256; |
| 90 |
l += 2; |
| 91 |
|
| 92 |
/*send_tty(addr + offset, l + sizeof(ethertap_packet.ethernet_header) - offset);*/ |
| 93 |
write(ttyfd, addr + offset, l + sizeof(ethertap_packet.ethernet_header) - offset); |
| 94 |
return l; |
| 95 |
} |
| 96 |
|
| 97 |
/*---------------------------------------------------------------------------*/ |
| 98 |
|
| 99 |
int receive_bpq(unsigned char *buf, int l) |
| 100 |
{ |
| 101 |
if ((l -= ethertap_header_len) <= 0 || |
| 102 |
(buf[ethertap_header_len-2] & 0xff) != 0x08 || |
| 103 |
(buf[ethertap_header_len-1] & 0xff) != 0xff) { |
| 104 |
/* not a bpqether packet. |
| 105 |
* drop silently - ethernet.ax25 is not the only protocol spoken |
| 106 |
* on ethernet ;) |
| 107 |
*/ |
| 108 |
return -1; |
| 109 |
} |
| 110 |
l -= 2; |
| 111 |
if (l <= 0 && buf[ethertap_header_len] + buf[ethertap_header_len] * 256 - 5 != l) { |
| 112 |
/* length error in bpqether packet */ |
| 113 |
return 0; |
| 114 |
} |
| 115 |
|
| 116 |
from_kiss(buf + ethertap_header_len + 2, l); |
| 117 |
return l; |
| 118 |
} |
| 119 |
|
| 120 |
/*---------------------------------------------------------------------------*/ |
| 121 |
/* TUN/TAP support for linux. ethertap is obsolete */ |
| 122 |
|
| 123 |
#ifdef TRY_TUNTAP |
| 124 |
static int tun_alloc(char *dev) |
| 125 |
{ |
| 126 |
struct ifreq ifr; |
| 127 |
int fd, err; |
| 128 |
|
| 129 |
fd = open("/dev/net/tun", O_RDWR); |
| 130 |
if (fd < 0) |
| 131 |
return -1; |
| 132 |
|
| 133 |
memset(&ifr, 0, sizeof(ifr)); |
| 134 |
|
| 135 |
/* Flags: IFF_TUN - TUN device (no Ethernet headers) |
| 136 |
* IFF_TAP - TAP device |
| 137 |
* |
| 138 |
* IFF_NO_PI - Do not provide packet information |
| 139 |
*/ |
| 140 |
ifr.ifr_flags = IFF_TAP; |
| 141 |
if (*dev) { |
| 142 |
/* |
| 143 |
* This error check convinces GCC the following strncpy |
| 144 |
* won't overflow its destination. |
| 145 |
* Sadly the rest of the code is such spaghetty that I |
| 146 |
* can't convince myself this is not possible either. |
| 147 |
*/ |
| 148 |
if (strlen(dev) >= IFNAMSIZ) { |
| 149 |
close(fd); |
| 150 |
fprintf(stderr, "Network device name \"%s\" exceeds system limit %d / IFNAMSIZ.\n", dev, IFNAMSIZ); |
| 151 |
|
| 152 |
return -1; |
| 153 |
} |
| 154 |
strcpy(ifr.ifr_name, dev); |
| 155 |
} |
| 156 |
|
| 157 |
err = ioctl(fd, TUNSETIFF, (void *)&ifr); |
| 158 |
if (err < 0) { |
| 159 |
close(fd); |
| 160 |
return err; |
| 161 |
} |
| 162 |
strcpy(dev, ifr.ifr_name); |
| 163 |
|
| 164 |
/* persist mode */ |
| 165 |
#if 0 |
| 166 |
if (ioctl(fd, TUNSETPERSIST, 1) < 0) |
| 167 |
perror("TUNSETPERSIST"); |
| 168 |
#endif |
| 169 |
|
| 170 |
/* don't checksum */ |
| 171 |
#if 0 |
| 172 |
if (ioctl(fd, TUNSETNOCSUM, 1) < 0) |
| 173 |
perror("TUNSETNOCSUM"); |
| 174 |
#endif |
| 175 |
|
| 176 |
return fd; |
| 177 |
} |
| 178 |
#endif |
| 179 |
|
| 180 |
/*---------------------------------------------------------------------------*/ |
| 181 |
|
| 182 |
int open_ethertap(char *ifname) |
| 183 |
{ |
| 184 |
|
| 185 |
int fd; |
| 186 |
char devname[PATH_MAX]; |
| 187 |
struct ifreq ifr; |
| 188 |
struct stat statbuf; |
| 189 |
int tuntap = 0; |
| 190 |
int mtu = 0; |
| 191 |
int skfd; |
| 192 |
|
| 193 |
strcpy(devname, "/dev/"); |
| 194 |
strncpy(devname + 5, ifname, sizeof(devname) - 5 -1); |
| 195 |
devname[sizeof(devname) -1] = 0; |
| 196 |
|
| 197 |
/* tuntap == 0: original ethertap. works on a real character device */ |
| 198 |
if (!stat(devname, &statbuf)) { |
| 199 |
fd = open(devname, O_RDWR); |
| 200 |
if (fd < 0) { |
| 201 |
LOGL2("%s: %s\n", devname, strerror(errno)); |
| 202 |
return -1; |
| 203 |
} |
| 204 |
#ifdef TRY_TUNTAP |
| 205 |
} else { |
| 206 |
strcpy(devname, ifname); |
| 207 |
fd = tun_alloc(devname); |
| 208 |
if (fd < 0) { |
| 209 |
LOGL2("%s: %s\n", devname, strerror(errno)); |
| 210 |
return -1; |
| 211 |
} |
| 212 |
ifname = devname; |
| 213 |
tuntap = 1; |
| 214 |
#endif |
| 215 |
} |
| 216 |
ethertap_header_len = (tuntap ? ETHERTAP_HEADER_LEN_TUN : ETHERTAP_HEADER_LEN_ETHERTAP); |
| 217 |
|
| 218 |
skfd = socket(AF_INET, SOCK_DGRAM, 0); |
| 219 |
if (skfd < 0) { |
| 220 |
perror("socket()"); |
| 221 |
close(fd); |
| 222 |
return -1; |
| 223 |
} |
| 224 |
|
| 225 |
memset(&ifr, 0, sizeof(ifr)); |
| 226 |
|
| 227 |
strncpy(ifr.ifr_name, ifname, IFNAMSIZ); |
| 228 |
ifr.ifr_name[IFNAMSIZ-1] = 0; |
| 229 |
if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) |
| 230 |
perror("SIOCGIFFLAGS"); |
| 231 |
|
| 232 |
ifr.ifr_flags |= (IFF_UP | IFF_NOARP); |
| 233 |
if (ioctl(skfd, SIOCSIFFLAGS, &ifr) < 0) |
| 234 |
perror("SIOCSIFFLAGS"); |
| 235 |
|
| 236 |
strncpy(ifr.ifr_name, ifname, IFNAMSIZ); |
| 237 |
ifr.ifr_name[IFNAMSIZ-1] = 0; |
| 238 |
if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0) { |
| 239 |
perror("SIOCGIFHWADDR"); |
| 240 |
hwaddr_remote[0] = 0xfe; |
| 241 |
hwaddr_remote[1] = 0xfd; |
| 242 |
hwaddr_remote[2] = 0x0; |
| 243 |
hwaddr_remote[3] = 0x0; |
| 244 |
hwaddr_remote[4] = 0x0; |
| 245 |
hwaddr_remote[5] = 0x0; |
| 246 |
} else |
| 247 |
memcpy(hwaddr_remote, ifr.ifr_hwaddr.sa_data, 6); |
| 248 |
|
| 249 |
if (ttyspeed > ETAP_MTU_MIN && ttyspeed != 9600) { |
| 250 |
mtu = ttyspeed; |
| 251 |
} else { |
| 252 |
strncpy(ifr.ifr_name, ifname, IFNAMSIZ); |
| 253 |
ifr.ifr_name[IFNAMSIZ-1] = 0; |
| 254 |
strcpy(ifr.ifr_name, ifname); |
| 255 |
if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0) |
| 256 |
perror("SIOCGIFMTU"); |
| 257 |
else |
| 258 |
mtu = ifr.ifr_mtu; |
| 259 |
} |
| 260 |
if (mtu < ETAP_MTU_MIN || mtu > ETAP_MTU_MAX) |
| 261 |
mtu = ETAP_MTU; |
| 262 |
strncpy(ifr.ifr_name, ifname, IFNAMSIZ); |
| 263 |
ifr.ifr_name[IFNAMSIZ-1] = 0; |
| 264 |
ifr.ifr_mtu = mtu; |
| 265 |
if (ioctl(skfd, SIOCSIFMTU, &ifr) < 0) |
| 266 |
perror("SIOSGIFMTU"); |
| 267 |
|
| 268 |
close(skfd); |
| 269 |
|
| 270 |
return fd; |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
/*---------------------------------------------------------------------------*/ |
| 275 |
|
| 276 |
int set_bpq_dev_call_and_up(char *ifname) |
| 277 |
{ |
| 278 |
FILE * fp; |
| 279 |
char buf[1024]; |
| 280 |
char bpq_name[IFNAMSIZ]; |
| 281 |
char dev_name[IFNAMSIZ]; |
| 282 |
struct ifreq ifr; |
| 283 |
int drop; |
| 284 |
int skfd; |
| 285 |
int err = -1; |
| 286 |
|
| 287 |
/* |
| 288 |
* 1. find correspondent bpqether device in /proc (i.e. bpq0 for tap0) |
| 289 |
* 2. set the ax25 call of bpq0 (in this example) |
| 290 |
* 3. ifup bpq0 |
| 291 |
*/ |
| 292 |
|
| 293 |
if (!(fp = fopen("/proc/net/bpqether", "r"))) { |
| 294 |
perror("/proc/net/bpqether"); |
| 295 |
return -1; |
| 296 |
} |
| 297 |
|
| 298 |
/* |
| 299 |
* look up bpqether devce in /proc: |
| 300 |
* dev ether destination accept from |
| 301 |
* bpq0 tap0 FF:FF:FF:FF:FF:FF * |
| 302 |
*/ |
| 303 |
|
| 304 |
drop = 1; |
| 305 |
*bpq_name = *dev_name = 0; |
| 306 |
while (fgets(buf, sizeof(buf), fp)) { |
| 307 |
if (drop) { |
| 308 |
/* header line */ |
| 309 |
drop = 0; |
| 310 |
continue; |
| 311 |
} |
| 312 |
sscanf(buf, "%s %s ", bpq_name, dev_name); |
| 313 |
if (!strcmp(dev_name, ifname)) |
| 314 |
break; |
| 315 |
*bpq_name = *dev_name = 0; |
| 316 |
} |
| 317 |
fclose(fp); |
| 318 |
if (!*bpq_name) { |
| 319 |
LOGL2("Could not find bpqether device for %s in " |
| 320 |
"/proc/net/bpqether: %s", ifr.ifr_name, strerror(errno)); |
| 321 |
return -1; |
| 322 |
} |
| 323 |
|
| 324 |
LOGL1("found bpq device %s for %s\n", bpq_name, dev_name); |
| 325 |
|
| 326 |
skfd = socket(AF_INET, SOCK_DGRAM, 0); |
| 327 |
if (skfd < 0) { |
| 328 |
perror("socket()"); |
| 329 |
return -1; |
| 330 |
} |
| 331 |
memset(&ifr, 0, sizeof(ifr)); |
| 332 |
memcpy(ifr.ifr_name, bpq_name, IFNAMSIZ); |
| 333 |
ifr.ifr_name[IFNAMSIZ-1] = 0; |
| 334 |
|
| 335 |
ifr.ifr_hwaddr.sa_family = PF_AX25; |
| 336 |
memcpy(ifr.ifr_hwaddr.sa_data, mycallsign, 7); |
| 337 |
|
| 338 |
if (ioctl(skfd, SIOCSIFHWADDR, &ifr) < 0) { |
| 339 |
perror("SIOCSIFHWADDR"); |
| 340 |
close(skfd); |
| 341 |
return -1; |
| 342 |
} |
| 343 |
strncpy(ifr.ifr_name, bpq_name, IFNAMSIZ); |
| 344 |
ifr.ifr_name[IFNAMSIZ-1] = 0; |
| 345 |
ifr.ifr_mtu = 256; |
| 346 |
if (ioctl(skfd, SIOCSIFMTU, &ifr) < 0) |
| 347 |
perror("SIOSGIFMTU"); |
| 348 |
|
| 349 |
strncpy(ifr.ifr_name, bpq_name, IFNAMSIZ); |
| 350 |
ifr.ifr_name[IFNAMSIZ-1] = 0; |
| 351 |
if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) { |
| 352 |
perror("SIOCGIFFLAGS"); |
| 353 |
ifr.ifr_flags = 0; |
| 354 |
} |
| 355 |
|
| 356 |
ifr.ifr_flags |= (IFF_UP); |
| 357 |
if (ioctl(skfd, SIOCSIFFLAGS, &ifr) < 0) { |
| 358 |
perror("SIOCSIFFLAGS"); |
| 359 |
err = -1; |
| 360 |
} |
| 361 |
|
| 362 |
close(skfd); |
| 363 |
|
| 364 |
return err; |
| 365 |
} |