minivmac4ios/Mini vMac/mnvm_core/BPFILTER.h

1 line
9.1 KiB
C
Raw Normal View History

/* BPFILTER.h Copyright (C) 2012 Michael Fort, Paul C. Pratt You can redistribute this file and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. You should have received a copy of the license along with this file; see the file COPYING. This file 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 license for more details. */ /* Berkeley Packet Filter for localtalk emulation */ /* BPF and devices */ static unsigned char device_address[6] = { 0 }; static unsigned int device_buffer_size = 0; static int fd = -1; /* BPF file descriptor */ static struct bpf_version bpf_version; static struct bpf_program bpf_program; static struct bpf_insn insns[] = { /* Program for BPF to filter out non-LTOE packets */ BPF_STMT(BPF_LD + BPF_H + BPF_ABS, 12), BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, 0x809B, 0, 1), BPF_STMT(BPF_RET + BPF_K, 65535), BPF_STMT(BPF_RET + BPF_K, 0), }; GLOBALVAR ui3p LT_TxBuffer = NULL; /* Transmit state */ GLOBALVAR ui4r LT_TxBuffSz = 0; /* Transmit buffer that is reused from tx packet to tx packet. The 's' byte represents the source mac address (ours) and we don't have to initialize it because the MAC device will automatically fill it in for us. The four 'p' bytes represent the process number of this Mini vMac application. It helps differentiate packets between two applications running on the same machine. It is not used at this time. There is a small chance two applications could get the same LLAP/SDLC address to start with and would not work correctly (1 in 254). The 'S' bytes are the size of the LLAP packet since it can be smaller than the minimum sized Ethernet frame. The process number is replaced at initialization. The size is updated just before sending to BPF. All LLAP data is inserted starting at byte 20. */ static unsigned char tx_buffer[20 + LT_TxBfMxSz] = "\xFF\xFF\xFF\xFF\xFF\xFFssssss\x80\x9BppppSS"; /* Receive state */ GLOBALVAR ui3p LT_RxBuffer = NULL; /* When data pending, this is used */ GLOBALVAR ui5r LT_RxBuffSz = 0; /* When data pending, this is used */ /* Macro used by only the get_sockaddrs function for readability. */ #define ROUNDUP(a, size) \ (((a) & ((size) - 1)) ? (1 + ((a) | ((size) - 1))) : (a)) /* Utility function needed for walking the addresses of the kernel route lookup */ LOCALPROC get_sockaddrs(int addrs, struct sockaddr* sa, struct sockaddr** rti_info) { int loop; int incr; for (loop = 0; loop < RTAX_MAX; loop++) { if (addrs & (1 << loop)) { rti_info[loop] = sa; incr = sa->sa_len ? ROUNDUP(sa->sa_len, sizeof(uint32_t)) : sizeof(uint32_t); sa = (struct sockaddr*)((unsigned long int)sa + incr); } else { rti_info[loop] = NULL; } } } /* This ugly function does a lot of steps to prepare the BPF for our use. 1. Find the ethernet interface that the default route uses. 2. Determine the maximum number of BPF devices can exist. 3. Search for a usable BPF device and open it. 4. Set the BPF device to use our ethernet interface. 5. Get the proper buffer size to use with the BPF device. 6. Set some useful modes of operation on the BPF device. 7. Install a program on the device that filters out non-LTOE packets. */ LOCALFUNC int get_ethernet(void) { int result; int size; struct rt_msghdr* message; struct sockaddr_in* addrs; struct sockaddr* sa_list[RTAX_MAX]; int loop; char filename[64]; struct ifreq ifreq; int enable = 1; struct kinfo_proc kp; size_t len = sizeof(kp); int max = 4; char device[32]; /* Get a socket to routed for IPv4 */ fd = socket(PF_ROUTE, SOCK_RAW, AF_INET); if (fd == -1) { return falseblnr; } /* Allocate a message */ size = sizeof(struct rt_msghdr) + 16 * sizeof(struct sockaddr_in); message = (struct rt_msghdr*)malloc(size); if (! message) { close(fd); return falseblnr; } memset(message, 0, size); addrs = (struct sockaddr_in*)(message + 1); /* Fill in the re