bcrelay.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. // A broadcast packet repeater. This packet repeater (currently designed for
  2. // udp packets) will listen for broadcast packets.
  3. // When it receives the packets, it will then re-broadcast the packet.
  4. //
  5. // Written by TheyCallMeLuc(at)yahoo.com.au
  6. // I accept no responsiblity for the function of this program if you
  7. // choose to use it.
  8. // Modified for Poptop by Richard de Vroede <r.devroede@linvision.com>
  9. // Ditto on the no responsibility.
  10. //
  11. // Rewritten by Norbert van Bolhuis <norbert@vanbolhuis.demon.nl> bcrelay (v1.0+)
  12. // now supports/does:
  13. // 1) Relaying from PPP (VPN tunnel) interfaces, hereby creating a virtual
  14. // LAN (w.r.t. UDP broadcasts) for VPN clients and ethernet PCs
  15. // belonging/matching the subnet portion of the VPN IP addresses.
  16. // So now broadcasting to/from all systems of the VPN has been implemented.
  17. // Note that bcrelay v0.5 only relayed from LAN to VPN clients.
  18. // It doesn't matter whether the systems on the VPN are on the LAN of the
  19. // VPN server or have a VPN/PPTP connection (over the internet) to the VPN
  20. // server. Broadcasts will always be relayed to/from all given interfaces. And
  21. // as long as the subnet portion of the IP addresses of the systems on the VPN
  22. // matches, the VPN server will be able to route properly. This means all
  23. // networking applications/games that rely on a UDP broadcast from one or
  24. // more PPP (VPN tunnel) interfaces will now see eachother and work over
  25. // the VPN.
  26. // Note that it depends on the networking application/game and whoever
  27. // acts as application/game server/host who is sending (UPD) broadcasts
  28. // and who is listening.
  29. // 2) UDP broadcasts received on a PPP interface (VPN tunnel) sometimes
  30. // don't carry the VPN IP address which pptpd provisioned. I've seen
  31. // this happening on a WinXP SP1 box, especially when the application
  32. // responsible for the UDP broadcasts isn't aware of the PPP interface.
  33. // In this case it just uses the LAN IP src address for the IP src
  34. // address of the inner (GRE encapsulated) IP packet. This breaks
  35. // the "virtual LAN" and therefore bcrelay, as of this version, changes
  36. // the src IP address to the VPN IP address (which pptpd provisioned)
  37. // before relaying.
  38. // 3) To avoid a UDP broadcast loop, bcrelay changes the IP TTL and the
  39. // UDP checksum (to 1 and 0 respectively) of the UDP broadcasts it relays.
  40. // No relaying will be done for UDP broadcasts with IP TTL=1 and UDP
  41. // checksum=0. Could also (mis)use IP identification for this, but IP TTL
  42. // and UDP chksum combination is expected to work fine.
  43. // 4) bcrelay v0.5 forgot to update IP/UDP checksum when it changed the
  44. // dest. IP address (e.g. from 192.168.1.255 to 255.255.255.255).
  45. // Of course as of this version bcrelay always updates the IP/UDP
  46. // checksum since the IP TTL and src IP address will change also.
  47. // 5) Enhanced the (syslog) debugging capabilities. By default bcrelay will
  48. // show what it is doing. Bcrelay will syslog the IP interfaces it tries
  49. // to read/relay UDP broadcasts from/to. These interfaces are called
  50. // the 'active interfaces', bcrelay will syslog the initial set of active
  51. // interfaces and whenever the set changes. Currently there is no difference
  52. // between an incoming interface (given with -i) and an outgoing interface
  53. // (given with -o), so bcrelay won't show this attribute. Also, bcrelay will
  54. // syslog a successfully relayed UDP broadcast, including the UDP port numbers,
  55. // the incoming interface and the interface(s) to which it was successfully
  56. // relayed. The (new) -n option allows to suppress syslog tracing.
  57. // If -n is given, bcrelay shows/syslogs nothing, except fatal error
  58. // messages.
  59. //
  60. // This software is completely free. You can use and/or modify this
  61. // software to your hearts content. You are free to redistribute it as
  62. // long as it is accompanied with the source and my credit is included.
  63. #ifdef HAVE_CONFIG_H
  64. #include "config.h"
  65. #endif
  66. #ifdef __linux__
  67. #define _GNU_SOURCE 1 /* strdup() prototype, broken arpa/inet.h */
  68. #endif
  69. #ifdef __svr4__
  70. #define __EXTENSIONS__ 1 /* strdup() prototype */
  71. #endif
  72. #ifdef __sgi__
  73. #define _XOPEN_SOURCE 500 /* strdup() prototype */
  74. #endif
  75. #include <fcntl.h>
  76. #include <stdio.h>
  77. #include <stdlib.h>
  78. #include <netdb.h>
  79. #include <unistd.h>
  80. #include <string.h>
  81. #include <libgen.h>
  82. #include <time.h>
  83. #include <sys/time.h>
  84. #include <regex.h>
  85. #include <net/if.h>
  86. #include <sys/ioctl.h>
  87. #include <sys/socket.h>
  88. #include <sys/types.h>
  89. #include <netinet/in.h>
  90. #include <netpacket/packet.h>
  91. #include <net/ethernet.h>
  92. #include <netinet/ip.h>
  93. #include <netinet/udp.h>
  94. #include <netinet/tcp.h>
  95. #include <dirent.h>
  96. #include "defaults.h"
  97. #include "our_syslog.h"
  98. #include "our_getopt.h"
  99. //#define VERSION "1.0"
  100. /* uncomment if you compile this without poptop's configure script */
  101. //#define HAVE_FORK
  102. /*
  103. * Value-return macros to fields in the IP PDU header
  104. */
  105. #define IP_IPPDU_IHL(ippdu) (*(unsigned char *)(ippdu) & 0x0F)
  106. #define IP_IPPDU_PROTO(ippdu) (*((unsigned char *)(ippdu) + 9) & 0xFF)
  107. /*
  108. * Pointer macros to fields in the IP PDU header
  109. */
  110. #define IP_IPPDU_CHECKSUM_MSB_PTR(ippdu) ((unsigned char *)(ippdu) + 10 )
  111. #define IP_IPPDU_CHECKSUM_LSB_PTR(ippdu) ((unsigned char *)(ippdu) + 11 )
  112. /*
  113. * Pointer macros to fields in the UDP PDU header
  114. */
  115. #define IP_UDPPDU_CHECKSUM_MSB_PTR(udppdu) ((unsigned char *)(udppdu) + 6 )
  116. #define IP_UDPPDU_CHECKSUM_LSB_PTR(udppdu) ((unsigned char *)(udppdu) + 7 )
  117. #define MAXIF 255 // Maximum interfaces to use
  118. #define MAX_SELECT_WAIT 3 // Maximum time (in secs) to wait for input on the socket/interfaces
  119. // A time-out triggers the discovery of new interfaces.
  120. #define MAX_NODISCOVER_IFS 12 // Maximum time (in secs) to elaps before a discovery of new
  121. // interfaces is triggered. Only when a packet keeps coming in
  122. // (this prevents a select time-out) a variable initialized with
  123. // this #define becomes 0 and a rediscovery of the interfaces is
  124. // triggered.
  125. #define MAX_IFLOGTOSTR 16
  126. /* Local function prototypes */
  127. static void showusage(char *prog);
  128. static void showversion();
  129. #ifndef HAVE_DAEMON
  130. static void my_daemon(int argc, char **argv);
  131. #endif
  132. static void mainloop(int argc, char **argv);
  133. struct packet {
  134. struct iphdr ip;
  135. struct udphdr udp;
  136. char data[ETHERMTU];
  137. };
  138. /*
  139. * struct that keeps track of the interfaces of the system
  140. * selected upon usage by bcrelay (with -i and -o option).
  141. * An array of this struct is returned by discoverActiveInterfaces.
  142. * This array is reset (filled from scratch) each time
  143. * discoverActiveInterfaces is called.
  144. */
  145. struct iflist {
  146. //Fix 3mar2003
  147. //char index;
  148. int index;
  149. u_int32_t bcast;
  150. char ifname[16+1];
  151. unsigned long ifaddr;
  152. unsigned long ifdstaddr;
  153. unsigned long flags1;
  154. };
  155. #define IFLIST_FLAGS1_IF_IS_ETH 0x00000001
  156. #define IFLIST_FLAGS1_IF_IS_PPP 0x00000002
  157. #define IFLIST_FLAGS1_IF_IS_UNKNOWN 0x00000004
  158. #define IFLIST_FLAGS1_CHANGED_INNER_SADDR 0x00010000
  159. /*
  160. * struct that keeps track of the socket fd's for every interface
  161. * that is in use (and thus present in iflist).
  162. * Two permanent arrays of this struct are used, one for the
  163. * previous/old list and one for the current list.
  164. */
  165. struct ifsnr {
  166. int sock_nr;
  167. int ifindex;
  168. };
  169. static void copy_ifsnr(struct ifsnr *from, struct ifsnr *to);
  170. static int find_sock_nr(struct ifsnr *l, int ifidx);
  171. struct iflist *discoverActiveInterfaces(int s);
  172. void ip_update_checksum(unsigned char *ippdu);
  173. static char *IpProtToString( unsigned char prot );
  174. static char *iflistToString( struct iflist *ifp );
  175. static char *iflistLogIToString( struct iflist *ifp, int idx, struct ifsnr *ifnr );
  176. static char *iflistLogRToString( struct iflist *ifp, int idx, struct ifsnr *ifnr );
  177. static void bind_to_iface(int fd, int ifindex);
  178. /*
  179. * This global variable determines whether NVBCR_PRINTF actually
  180. * displays something. While developping v1.0, NVBCR_PRINTF were
  181. * printf and a lot of tracing/logging/debugging was done with these.
  182. * Of course, by default these 'info' messages have been turned off
  183. * now. Enable by setting variable to 1. Note that output will only
  184. * appear in non-daemon mode (see also NVBCR_PRINTF).
  185. */
  186. static int do_org_info_printfs = 0;
  187. static int vnologging = 0;
  188. static int vdaemon = 0;
  189. #define NVBCR_PRINTF( args ) \
  190. if ((vdaemon == 0) && (do_org_info_printfs == 1)) printf args
  191. static char empty[1] = "";
  192. static char interfaces[32];
  193. static char log_interfaces[MAX_IFLOGTOSTR*MAXIF];
  194. static char log_relayed[(MAX_IFLOGTOSTR-1)*MAXIF+81];
  195. static char *ipsec = empty;
  196. static void showusage(char *prog)
  197. {
  198. printf("\nBCrelay v%s\n\n", VERSION);
  199. printf("A broadcast packet repeater. This packet repeater (currently designed for udp packets) will listen\n");
  200. printf(" for broadcast packets. When it receives the packets, it will then re-broadcast the packet.\n\n");
  201. printf("Usage: %s [options], where options are:\n\n", prog);
  202. printf(" [-d] [--daemon] Run as daemon.\n");
  203. printf(" [-h] [--help] Displays this help message.\n");
  204. printf(" [-i] [--incoming <ifin>] Defines from which interface broadcasts will be relayed.\n");
  205. printf(" [-n] [--nolog] No logging/tracing to /var/log/messages.\n");
  206. printf(" [-o] [--outgoing <ifout>] Defines to which interface broadcasts will be relayed.\n");
  207. printf(" [-s] [--ipsec <arg>] Defines an ipsec tunnel to be relayed to.\n");
  208. printf(" Since ipsec tunnels terminate on the same interface, we need to define the broadcast\n");
  209. printf(" address of the other end-point of the tunnel. This is done as ipsec0:x.x.x.255\n");
  210. printf(" [-v] [--version] Displays the BCrelay version number.\n");
  211. printf("\nLog messages and debugging go to syslog as DAEMON.\n\n");
  212. printf("\nInterfaces can be specified as regexpressions, ie. ppp[0-9]+\n\n");
  213. }
  214. static void showversion()
  215. {
  216. printf("BCrelay v%s\n", VERSION);
  217. }
  218. #ifndef HAVE_DAEMON
  219. static void my_daemon(int argc, char **argv)
  220. {
  221. pid_t pid;
  222. #ifndef BCRELAY_BIN
  223. /* Need a smart way to locate the binary -rdv */
  224. #define BCRELAY_BIN argv[0]
  225. #endif
  226. #ifndef HAVE_FORK
  227. /* need to use vfork - eg, uClinux */
  228. char **new_argv;
  229. extern char **environ;
  230. int minusd=0;
  231. int i;
  232. int fdr;
  233. /* Strip -d option */
  234. new_argv = malloc((argc) * sizeof(char **));
  235. fdr = open("/dev/null", O_RDONLY);
  236. new_argv[0] = BCRELAY_BIN;
  237. for (i = 1; argv[i] != NULL; i++) {
  238. if (fdr != 0) { dup2(fdr, 0); close(fdr); }
  239. if ( (strcmp(argv[i],"-d")) == 0 ) {
  240. minusd=1;
  241. }
  242. if (minusd) {
  243. new_argv[i] = argv[i+1];
  244. } else {
  245. new_argv[i] = argv[i];
  246. }
  247. }
  248. syslog(LOG_DEBUG, "Option parse OK, re-execing as daemon");
  249. fflush(stderr);
  250. if ((pid = vfork()) == 0) {
  251. if (setsid() < 0) { /* shouldn't fail */
  252. syslog(LOG_ERR, "Setsid failed!");
  253. _exit(1);
  254. }
  255. chdir("/");
  256. umask(0);
  257. /* execve only returns on an error */
  258. execve(BCRELAY_BIN, new_argv, environ);
  259. exit(1);
  260. } else if (pid > 0) {
  261. syslog(LOG_DEBUG, "Success re-execing as daemon!");
  262. exit(0);
  263. } else {
  264. syslog(LOG_ERR, "Error vforking");
  265. exit(1);
  266. }
  267. #else
  268. pid=fork();
  269. if (pid<0) { syslog(LOG_ERR, "Error forking"); _exit(1); }
  270. if (pid>0) { syslog(LOG_DEBUG, "Parent exits"); _exit(0); }
  271. if (pid==0) { syslog(LOG_DEBUG, "Running as child"); }
  272. /* child (daemon) continues */
  273. if (setsid() < 0) { /* shouldn't fail */
  274. syslog(LOG_ERR, "Setsid failed!");
  275. _exit(1);
  276. }
  277. chdir("/");
  278. #endif
  279. }
  280. #endif
  281. int main(int argc, char **argv) {
  282. regex_t preg;
  283. /* command line options */
  284. int c;
  285. char *ifout = empty;
  286. char *ifin = empty;
  287. #ifndef BCRELAY
  288. fprintf(stderr,
  289. "bcrelay: pptpd was compiled without support for bcrelay, exiting.\n"
  290. " run configure --with-bcrelay, make, and install.\n");
  291. exit(1);
  292. #endif
  293. /* open a connection to the syslog daemon */
  294. openlog("bcrelay", LOG_PID, PPTP_FACILITY);
  295. while (1) {
  296. int option_index = 0;
  297. static struct option long_options[] =
  298. {
  299. {"nolog", 0, 0, 0},
  300. {"daemon", 0, 0, 0},
  301. {"help", 0, 0, 0},
  302. {"incoming", 1, 0, 0},
  303. {"outgoing", 1, 0, 0},
  304. {"ipsec", 1, 0, 0},
  305. {"version", 0, 0, 0},
  306. {0, 0, 0, 0}
  307. };
  308. c = getopt_long(argc, argv, "ndhi:o:s:v", long_options, &option_index);
  309. if (c == -1)
  310. break;
  311. /* convert long options to short form */
  312. if (c == 0)
  313. c = "ndhiosv"[option_index];
  314. switch (c) {
  315. case 'n':
  316. vnologging = 1;
  317. break;
  318. case 'd':
  319. vdaemon = 1;
  320. break;
  321. case 'h':
  322. showusage(argv[0]);
  323. return 0;
  324. case 'i':
  325. ifin = strdup(optarg);
  326. break;
  327. case 'o':
  328. ifout = strdup(optarg);
  329. break;
  330. case 's':
  331. ipsec = strdup(optarg);
  332. // Validate the ipsec parameters
  333. regcomp(&preg, "ipsec[0-9]+:[0-9]+.[0-9]+.[0-9]+.255", REG_EXTENDED);
  334. if (regexec(&preg, ipsec, 0, NULL, 0)) {
  335. syslog(LOG_INFO,"Bad syntax: %s", ipsec);
  336. fprintf(stderr, "\nBad syntax: %s\n", ipsec);
  337. showusage(argv[0]);
  338. return 0;
  339. } else {
  340. regfree(&preg);
  341. break;
  342. }
  343. case 'v':
  344. showversion();
  345. return 0;
  346. default:
  347. showusage(argv[0]);
  348. return 1;
  349. }
  350. }
  351. if (ifin == empty) {
  352. syslog(LOG_INFO,"Incoming interface required!");
  353. showusage(argv[0]);
  354. _exit(1);
  355. }
  356. if (ifout == empty && ipsec == empty) {
  357. syslog(LOG_INFO,"Listen-mode or outgoing or IPsec interface required!");
  358. showusage(argv[0]);
  359. _exit(1);
  360. } else {
  361. sprintf(interfaces,"%s|%s", ifin, ifout);
  362. }
  363. // If specified, become Daemon.
  364. if (vdaemon) {
  365. #if HAVE_DAEMON
  366. closelog();
  367. if (freopen("/dev/null", "r", stdin) == NULL) {
  368. syslog(LOG_ERR, "failed to reopen stdin");
  369. }
  370. /* set noclose, we want stdout/stderr still attached if we can */
  371. if (daemon(0, 1) == -1) {
  372. syslog_perror("daemon");
  373. }
  374. /* returns to child only */
  375. /* pid will have changed */
  376. openlog("bcrelay", LOG_PID, PPTP_FACILITY);
  377. #else /* !HAVE_DAEMON */
  378. my_daemon(argc, argv);
  379. /* returns to child if !HAVE_FORK
  380. * never returns if HAVE_FORK (re-execs without -d)
  381. */
  382. #endif
  383. } else {
  384. syslog(LOG_INFO, "Running as child\n");
  385. }
  386. mainloop(argc,argv);
  387. _exit(0);
  388. }
  389. static void mainloop(int argc, char **argv)
  390. {
  391. socklen_t salen = sizeof(struct sockaddr_ll);
  392. int i, s, rcg, j, no_discifs_cntr, ifs_change;
  393. int logstr_cntr;
  394. struct iflist *iflist = NULL; // Initialised after the 1st packet
  395. struct sockaddr_ll sa;
  396. struct packet *ipp_p;
  397. unsigned char *udppdu;
  398. fd_set sock_set;
  399. struct timeval time_2_wait;
  400. static struct ifsnr old_ifsnr[MAXIF+1]; // Old iflist to socket fd's mapping list
  401. static struct ifsnr cur_ifsnr[MAXIF+1]; // Current iflist to socket fd's mapping list
  402. unsigned char buf[1518];
  403. char *logstr = empty;
  404. no_discifs_cntr = MAX_NODISCOVER_IFS;
  405. ifs_change = 0;
  406. /*
  407. * Open general ethernet socket, only used to discover interfaces.
  408. */
  409. if ((s = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL))) < 0)
  410. syslog(LOG_INFO,"%s: Error creating socket", *argv);
  411. /*
  412. * Discover interfaces (initial set) and create a dedicated socket bound to the interface
  413. */
  414. memset(old_ifsnr, -1, sizeof(old_ifsnr));
  415. memset(cur_ifsnr, -1, sizeof(cur_ifsnr));
  416. iflist = discoverActiveInterfaces(s);
  417. for (i=0; iflist[i].index; ++i) {
  418. if ((cur_ifsnr[i].sock_nr = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL))) < 0) {
  419. syslog(LOG_ERR, "mainloop: Error, socket error! (rv=%d, errno=%d)", cur_ifsnr[i].sock_nr, errno);
  420. exit(1);
  421. }
  422. bind_to_iface(cur_ifsnr[i].sock_nr, iflist[i].index);
  423. cur_ifsnr[i].ifindex = iflist[i].index;
  424. }
  425. NVBCR_PRINTF(("Displaying INITIAL active interfaces..\n"));
  426. if (vnologging == 0) {
  427. logstr = log_interfaces;
  428. logstr_cntr = sprintf(logstr, "Initial active interfaces: ");
  429. logstr += logstr_cntr;
  430. }
  431. for (i = 0; iflist[i].index; i++)
  432. {
  433. NVBCR_PRINTF(("\t\tactive interface number: %d, if=(%s), sock_nr=%d\n", i, iflistToString(&(iflist[i])), cur_ifsnr[i].sock_nr));
  434. if (vnologging == 0) {
  435. logstr_cntr = sprintf(logstr, "%s ", iflistLogIToString(&(iflist[i]), i, &(cur_ifsnr[i])));
  436. logstr += logstr_cntr;
  437. }
  438. }
  439. if (vnologging == 0) syslog(LOG_INFO, "%s", log_interfaces);
  440. // Main loop
  441. while (1)
  442. {
  443. /*
  444. * Check all (interface) sockets for incoming packets
  445. */
  446. FD_ZERO(&sock_set);
  447. for (i=0; iflist[i].index; ++i)
  448. {
  449. if (cur_ifsnr[i].sock_nr >= 0) {
  450. FD_SET(cur_ifsnr[i].sock_nr, &sock_set);
  451. }
  452. }
  453. /*
  454. * Don't wait more than MAX_SELECT_WAIT seconds
  455. */
  456. time_2_wait.tv_sec = MAX_SELECT_WAIT;
  457. time_2_wait.tv_usec = 0L;
  458. /* select on sockets */
  459. rcg = select(MAXIF, &sock_set, (fd_set *) NULL,(fd_set *) NULL, &time_2_wait);
  460. if (rcg < 0)
  461. {
  462. syslog(LOG_ERR, "Error, select error! (rv=%d, errno=%d)", rcg, errno);
  463. exit(1);
  464. }
  465. if (rcg == 0)
  466. {
  467. /* TimeOut, rediscover interfaces */
  468. NVBCR_PRINTF(("Select timeout, rediscover interfaces\n"));
  469. copy_ifsnr(cur_ifsnr, old_ifsnr);
  470. memset(cur_ifsnr, -1, sizeof(cur_ifsnr));
  471. iflist = discoverActiveInterfaces(s);
  472. /*
  473. * Build new cur_ifsnr list.
  474. * Make iflist[i] correspond with cur_ifsnr[i], so iflist[i].index == cur_ifsnr[i].ifindex
  475. * The old list (old_ifsnr) is used to compare.
  476. */
  477. for (i=0; iflist[i].index; ++i) {
  478. /* check to see if it is a NEW interface */
  479. int fsnr = find_sock_nr(old_ifsnr, iflist[i].index);
  480. if (fsnr == -1) {
  481. /* found new interface, open dedicated socket and bind it to the interface */
  482. if ((cur_ifsnr[i].sock_nr = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL))) < 0) {
  483. syslog(LOG_ERR, "mainloop: Error, socket error! (rv=%d, errno=%d)", cur_ifsnr[i].sock_nr, errno);
  484. exit(1);
  485. }
  486. bind_to_iface(cur_ifsnr[i].sock_nr, iflist[i].index);
  487. ifs_change = 1;
  488. }
  489. else
  490. {
  491. /*
  492. * not a new interface, socket already openen, interface already
  493. * bound. Update cur_ifsnr.
  494. */
  495. cur_ifsnr[i].sock_nr = fsnr;
  496. }
  497. cur_ifsnr[i].ifindex = iflist[i].index;
  498. }
  499. /* Close disappeared interfaces */
  500. for (i=0; i<MAXIF; ++i)
  501. {
  502. if ((old_ifsnr[i].sock_nr != -1) && (old_ifsnr[i].ifindex != -1) &&
  503. (find_sock_nr(cur_ifsnr, old_ifsnr[i].ifindex) == -1)) {
  504. NVBCR_PRINTF(("Closing an interface (it disappeared), namely: (s_nr=%d, ifidx=%d)\n", old_ifsnr[i].sock_nr, old_ifsnr[i].ifindex));
  505. close(old_ifsnr[i].sock_nr);
  506. old_ifsnr[i].sock_nr = -1;
  507. old_ifsnr[i].ifindex = -1;
  508. ifs_change = 1;
  509. }
  510. }
  511. if (ifs_change == 1)
  512. {
  513. NVBCR_PRINTF(("Active interface set changed --> displaying current active interfaces..\n"));
  514. if (vnologging == 0) {
  515. logstr = log_interfaces;
  516. logstr_cntr = sprintf(logstr, "Active interface set changed to: ");
  517. logstr += logstr_cntr;
  518. }
  519. for (i = 0; iflist[i].index; i++)
  520. {
  521. NVBCR_PRINTF(("\t\tactive interface number: %d, if=(%s), sock_nr=%d\n", i, iflistToString(&(iflist[i])), cur_ifsnr[i].sock_nr));
  522. if (vnologging == 0) {
  523. logstr_cntr = sprintf(logstr, "%s ", iflistLogIToString(&(iflist[i]), i, &(cur_ifsnr[i])));
  524. logstr += logstr_cntr;
  525. }
  526. }
  527. if (vnologging == 0) syslog(LOG_INFO, "%s", log_interfaces);
  528. ifs_change = 0;
  529. }
  530. continue;
  531. }
  532. if (rcg > 0)
  533. {
  534. /* rcg interfaces have pending input */
  535. for (i=0; ((iflist[i].index != 0) && (rcg > 0)); ++i)
  536. {
  537. if ((cur_ifsnr[i].sock_nr != -1) && (FD_ISSET(cur_ifsnr[i].sock_nr,&sock_set)))
  538. {
  539. /* Valid socket number and pending input, let's read */
  540. int rlen = read(cur_ifsnr[i].sock_nr, buf, sizeof(buf));
  541. ipp_p = (struct packet *)&(buf[0]);
  542. NVBCR_PRINTF(("IP_Packet=(tot_len=%d, id=%02x%02x, ttl=%d, prot=%s, src_ip=%d.%d.%d.%d, dst_ip=%d.%d.%d.%d) (on if: %d/%d) ", ntohs(ipp_p->ip.tot_len), (ntohs(ipp_p->ip.id))>>8, (ntohs(ipp_p->ip.id))&0x00ff, ipp_p->ip.ttl, IpProtToString(ipp_p->ip.protocol), (ntohl(ipp_p->ip.saddr))>>24, ((ntohl(ipp_p->ip.saddr))&0x00ff0000)>>16, ((ntohl(ipp_p->ip.saddr))&0x0000ff00)>>8, (ntohl(ipp_p->ip.saddr))&0x000000ff, (ntohl(ipp_p->ip.daddr))>>24, ((ntohl(ipp_p->ip.daddr))&0x00ff0000)>>16, ((ntohl(ipp_p->ip.daddr))&0x0000ff00)>>8, (ntohl(ipp_p->ip.daddr))&0x000000ff, i, iflist[i].index));
  543. rcg -= 1;
  544. if ( (ipp_p->ip.protocol == IPPROTO_UDP) &&
  545. (((ntohl(ipp_p->ip.daddr)) & 0x000000ff) == 0x000000ff) &&
  546. (ipp_p->ip.ttl != 1) &&
  547. (!((*IP_UDPPDU_CHECKSUM_MSB_PTR((unsigned char *)ipp_p+(4*ipp_p->ip.ihl)) == 0) &&
  548. (*IP_UDPPDU_CHECKSUM_LSB_PTR((unsigned char *)ipp_p+(4*ipp_p->ip.ihl)) == 0))) )
  549. {
  550. int nrsent;
  551. int ifindex_to_exclude = iflist[i].index;
  552. NVBCR_PRINTF(("is an UDP BROADCAST (dstPort=%d, srcPort=%d) (with TTL!=1 and UDP_CHKSUM!=0)\n\n",
  553. ntohs(ipp_p->udp.dest), ntohs(ipp_p->udp.source)));
  554. if (vnologging == 0) {
  555. logstr = log_relayed;
  556. logstr_cntr = sprintf(logstr, "UDP_BroadCast(sp=%d,dp=%d) from: %s relayed to: ", ntohs(ipp_p->udp.source),
  557. ntohs(ipp_p->udp.dest), iflistLogRToString(&(iflist[i]), i, &(cur_ifsnr[i])));
  558. logstr += logstr_cntr;
  559. }
  560. /* going to relay a broadcast packet on all the other interfaces */
  561. for (j=0; iflist[j].index; ++j)
  562. {
  563. int prepare_ipp = 0; // Changing the incoming UDP broadcast needs to be done once
  564. if (iflist[j].index != ifindex_to_exclude)
  565. {
  566. NVBCR_PRINTF(("Going to sent UDP Broadcast on interface: %s, sock_nr=%d\n", iflistToString(&(iflist[j])), cur_ifsnr[j].sock_nr));
  567. memset(&sa, 0, salen);
  568. sa.sll_family = AF_PACKET;
  569. sa.sll_ifindex = iflist[j].index; /* Must be the SIOCGIFINDEX number */
  570. // Set the outgoing hardware address to 1's. True Broadcast
  571. sa.sll_addr[0] = sa.sll_addr[1] = sa.sll_addr[2] = sa.sll_addr[3] = 0xff;
  572. sa.sll_addr[4] = sa.sll_addr[5] = sa.sll_addr[6] = sa.sll_addr[7] = 0xff;
  573. sa.sll_halen = 6;
  574. /*
  575. * htons(ETH_P_IP) is necessary otherwise sendto will
  576. * succeed but no packet is actually sent on the wire (this
  577. * was the case for PPP interfaces, for ETH interfaces an unknown
  578. * LAN frame is sent if htons(ETH_P_IP) is not set as protocol).
  579. */
  580. sa.sll_protocol = htons(ETH_P_IP); /* ETH_P_PPP_MP */
  581. if (prepare_ipp == 0) {
  582. // change TimeToLive to 1, This is to avoid loops, bcrelay will *NOT* relay
  583. // anything with TTL==1.
  584. ipp_p->ip.ttl = 1;
  585. // The CRC gets broken here when sending over ipsec tunnels but that
  586. // should not matter as we reassemble the packet at the other end.
  587. ipp_p->ip.daddr = iflist[j].bcast;
  588. // check IP srcAddr (on some winXP boxes it is found to be
  589. // different from the configured ppp address).
  590. // Only do this for PPP interfaces.
  591. if ((iflist[i].flags1 & IFLIST_FLAGS1_IF_IS_PPP) &&
  592. (ntohl(ipp_p->ip.saddr) != iflist[i].ifdstaddr))
  593. {
  594. ipp_p->ip.saddr = htonl(iflist[i].ifdstaddr);
  595. iflist[i].flags1 |= IFLIST_FLAGS1_CHANGED_INNER_SADDR;
  596. }
  597. // Update IP checkSum (TTL and src/dest IP Address might have changed)
  598. ip_update_checksum((unsigned char *)ipp_p);
  599. /* Disable upper layer checksum */
  600. udppdu = (unsigned char *)ipp_p + (4 * ipp_p->ip.ihl);
  601. *IP_UDPPDU_CHECKSUM_MSB_PTR(udppdu) = (unsigned char)0;
  602. *IP_UDPPDU_CHECKSUM_LSB_PTR(udppdu) = (unsigned char)0;
  603. prepare_ipp = 1;
  604. }
  605. /*
  606. * The beauty of sending IP packets on a PACKET socket of type SOCK_DGRAM is that
  607. * there is no need to concern about the physical/link layer header because it is
  608. * filled in automatically (based on the contents of sa).
  609. */
  610. if ((nrsent = sendto(cur_ifsnr[j].sock_nr, ipp_p, rlen, MSG_DONTWAIT|MSG_TRYHARD, (struct sockaddr *)&sa, salen)) < 0)
  611. {
  612. if (errno == ENETDOWN) {
  613. syslog(LOG_NOTICE, "ignored ENETDOWN from sendto(), a network interface was going down?");
  614. } else if (errno == ENXIO) {
  615. syslog(LOG_NOTICE, "ignored ENXIO from sendto(), a network interface went down?");
  616. } else if (errno == ENOBUFS) {
  617. syslog(LOG_NOTICE, "ignored ENOBUFS from sendto(), temporary shortage of buffer memory");
  618. } else {
  619. syslog(LOG_ERR, "mainloop: Error, sendto failed! (rv=%d, errno=%d)", nrsent, errno);
  620. exit(1);
  621. }
  622. }
  623. NVBCR_PRINTF(("Successfully relayed %d bytes \n", nrsent));
  624. if (vnologging == 0) {
  625. logstr_cntr = sprintf(logstr, "%s ", iflistLogRToString(&(iflist[j]), j, &(cur_ifsnr[j])));
  626. logstr += logstr_cntr;
  627. }
  628. }
  629. }
  630. if (vnologging == 0) syslog(LOG_INFO, "%s", log_relayed);
  631. } else {
  632. NVBCR_PRINTF(("is NOT an UDP BROADCAST (with TTL!=1 and UDP_CHKSUM!=0)\n\n"));
  633. }
  634. }
  635. }
  636. /*
  637. * Don't forget to discover new interfaces if we keep getting
  638. * incoming packets (on an already discovered interface).
  639. */
  640. if (no_discifs_cntr == 0)
  641. {
  642. no_discifs_cntr = MAX_NODISCOVER_IFS;
  643. /* no_discifs_cntr became 0, rediscover interfaces */
  644. NVBCR_PRINTF(("no_discifs_cntr became 0, rediscover interfaces\n"));
  645. copy_ifsnr(cur_ifsnr, old_ifsnr);
  646. memset(cur_ifsnr, -1, sizeof(cur_ifsnr));
  647. iflist = discoverActiveInterfaces(s);
  648. /*
  649. * Build new cur_ifsnr list.
  650. * Make iflist[i] correspond with cur_ifsnr[i], so iflist[i].index == cur_ifsnr[i].ifindex
  651. * The old list (old_ifsnr) is used to compare.
  652. */
  653. for (i=0; iflist[i].index; ++i) {
  654. /* check to see if it is a NEW interface */
  655. int fsnr = find_sock_nr(old_ifsnr, iflist[i].index);
  656. if (fsnr == -1) {
  657. /* found new interface, open dedicated socket and bind it to the interface */
  658. if ((cur_ifsnr[i].sock_nr = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL))) < 0) {
  659. syslog(LOG_ERR, "mainloop: Error, socket error! (rv=%d, errno=%d)", cur_ifsnr[i].sock_nr, errno);
  660. exit(1);
  661. }
  662. bind_to_iface(cur_ifsnr[i].sock_nr, iflist[i].index);
  663. ifs_change = 1;
  664. }
  665. else
  666. {
  667. /*
  668. * not a new interface, socket already openen, interface already
  669. * bound. Update cur_ifsnr.
  670. */
  671. cur_ifsnr[i].sock_nr = fsnr;
  672. }
  673. cur_ifsnr[i].ifindex = iflist[i].index;
  674. }
  675. /* Close disappeared interfaces */
  676. for (i=0; i<MAXIF; ++i)
  677. {
  678. if ((old_ifsnr[i].sock_nr != -1) && (old_ifsnr[i].ifindex != -1) &&
  679. (find_sock_nr(cur_ifsnr, old_ifsnr[i].ifindex) == -1)) {
  680. NVBCR_PRINTF(("Closing an interface (it disappeared), namely: (s_nr=%d, ifidx=%d)\n", old_ifsnr[i].sock_nr, old_ifsnr[i].ifindex));
  681. close(old_ifsnr[i].sock_nr);
  682. old_ifsnr[i].sock_nr = -1;
  683. old_ifsnr[i].ifindex = -1;
  684. ifs_change = 1;
  685. }
  686. }
  687. if (ifs_change == 1)
  688. {
  689. NVBCR_PRINTF(("Active interface set changed --> displaying current active interfaces..\n"));
  690. if (vnologging == 0) {
  691. logstr = log_interfaces;
  692. logstr_cntr = sprintf(logstr, "Active interface set changed to: ");
  693. logstr += logstr_cntr;
  694. }
  695. for (i = 0; iflist[i].index; i++)
  696. {
  697. NVBCR_PRINTF(("\t\tactive interface number: %d, if=(%s), sock_nr=%d\n", i, iflistToString(&(iflist[i])), cur_ifsnr[i].sock_nr));
  698. if (vnologging == 0) {
  699. logstr_cntr = sprintf(logstr, "%s ", iflistLogIToString(&(iflist[i]), i, &(cur_ifsnr[i])));
  700. logstr += logstr_cntr;
  701. }
  702. }
  703. if (vnologging == 0) syslog(LOG_INFO, "%s", log_interfaces);
  704. ifs_change = 0;
  705. }
  706. }
  707. else
  708. {
  709. no_discifs_cntr -= MAX_SELECT_WAIT;
  710. }
  711. }
  712. }
  713. }
  714. // Discover active interfaces
  715. struct iflist *
  716. discoverActiveInterfaces(int s) {
  717. static struct iflist iflist[MAXIF+1]; // Allow for MAXIF interfaces
  718. static struct ifconf ifs;
  719. int i, cntr = 0;
  720. regex_t preg;
  721. struct ifreq ifrflags, ifr;
  722. struct sockaddr_in *sin;
  723. /* Reset iflist */
  724. memset(iflist, 0, sizeof(iflist));
  725. /* Reset ifs */
  726. memset(&ifs, 0, sizeof(ifs));
  727. //regcomp(&preg, argv[1], REG_ICASE|REG_EXTENDED);
  728. regcomp(&preg, interfaces, REG_ICASE|REG_EXTENDED);
  729. ifs.ifc_len = MAXIF*sizeof(struct ifreq);
  730. ifs.ifc_req = malloc(ifs.ifc_len);
  731. ioctl(s, SIOCGIFCONF, &ifs); // Discover active interfaces
  732. for (i = 0; i * sizeof(struct ifreq) < ifs.ifc_len && cntr < MAXIF; i++)
  733. {
  734. if (regexec(&preg, ifs.ifc_req[i].ifr_name, 0, NULL, 0) == 0) {
  735. /*
  736. * Get interface flags and check status and type.
  737. * Only if interface is up it will be used.
  738. */
  739. memset(&ifrflags, 0, sizeof(ifrflags));
  740. strncpy(ifrflags.ifr_name, ifs.ifc_req[i].ifr_name, strlen(ifs.ifc_req[i].ifr_name));
  741. if (ioctl(s, SIOCGIFFLAGS, &ifrflags) < 0) {
  742. syslog(LOG_ERR, "discoverActiveInterfaces: Error, SIOCGIFFLAGS Failed! (errno=%d)", errno);
  743. exit(1);
  744. }
  745. if (ifrflags.ifr_flags & IFF_UP)
  746. {
  747. /*
  748. * Get interface index
  749. */
  750. ioctl(s, SIOCGIFINDEX, &ifs.ifc_req[i]);
  751. //Fix 3mar2003
  752. //iflist[cntr].index = (char)ifs.ifc_req[i].ifr_ifindex;
  753. iflist[cntr].index = ifs.ifc_req[i].ifr_ifindex;
  754. /*
  755. * Get interface name
  756. */
  757. strncpy(iflist[cntr].ifname, ifs.ifc_req[i].ifr_ifrn.ifrn_name,
  758. sizeof(iflist[cntr].ifname));
  759. iflist[cntr].ifname[sizeof(iflist[cntr].ifname)-1] = 0;
  760. /*
  761. * Get local IP address
  762. */
  763. memset(&ifr, 0, sizeof(ifr));
  764. ifr.ifr_addr.sa_family = AF_INET;
  765. (void)strncpy(ifr.ifr_name, iflist[cntr].ifname, strlen(iflist[cntr].ifname)+1);
  766. if (ioctl(s, SIOCGIFADDR, (char *)&ifr) < 0) {
  767. syslog(LOG_ERR, "discoverActiveInterfaces: Error, SIOCGIFADDR Failed! (errno=%d)", errno);
  768. exit(1);
  769. }
  770. sin = (struct sockaddr_in *)&ifr.ifr_addr;
  771. iflist[cntr].ifaddr = ntohl(sin->sin_addr.s_addr);
  772. iflist[cntr].flags1 = 0;
  773. if (ifrflags.ifr_flags & IFF_POINTOPOINT) {
  774. /*
  775. * Get remote IP address (only for PPP interfaces)
  776. */
  777. memset(&ifr, 0, sizeof(ifr));
  778. ifr.ifr_addr.sa_family = AF_INET;
  779. (void)strncpy(ifr.ifr_name, iflist[cntr].ifname, strlen(iflist[cntr].ifname)+1);
  780. if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifr) < 0) {
  781. syslog(LOG_ERR, "discoverActiveInterfaces: Error, SIOCGIFDSTADDR Failed! (errno=%d)", errno);
  782. exit(1);
  783. }
  784. sin = (struct sockaddr_in *)&ifr.ifr_addr;
  785. iflist[cntr].ifdstaddr = ntohl(sin->sin_addr.s_addr);
  786. iflist[cntr].flags1 |= IFLIST_FLAGS1_IF_IS_PPP;
  787. iflist[cntr].bcast = INADDR_BROADCAST;
  788. }
  789. else if (ifrflags.ifr_flags & IFF_BROADCAST)
  790. {
  791. iflist[cntr].ifdstaddr = 0;
  792. iflist[cntr].flags1 |= IFLIST_FLAGS1_IF_IS_ETH;
  793. iflist[cntr].bcast = INADDR_BROADCAST;
  794. }
  795. else
  796. {
  797. iflist[cntr].ifdstaddr = 0;
  798. iflist[cntr].flags1 |= IFLIST_FLAGS1_IF_IS_UNKNOWN;
  799. iflist[cntr].bcast = INADDR_BROADCAST;
  800. }
  801. cntr++;
  802. }
  803. // IPSEC tunnels are a fun one. We must change the destination address
  804. // so that it will be routed to the correct tunnel end point.
  805. // We can define several tunnel end points for the same ipsec interface.
  806. } else if (ipsec != empty && strncmp(ifs.ifc_req[i].ifr_name, "ipsec", 5) == 0) {
  807. if (strncmp(ifs.ifc_req[i].ifr_name, ipsec, 6) == 0) {
  808. struct hostent *hp = gethostbyname(ipsec+7);
  809. ioctl(s, SIOCGIFINDEX, &ifs.ifc_req[i]);
  810. iflist[cntr].index = ifs.ifc_req[i].ifr_ifindex; /* Store the SIOCGIFINDEX number */
  811. memcpy(&(iflist[cntr++].bcast), hp->h_addr, sizeof(u_int32_t));
  812. }
  813. }
  814. }
  815. iflist[cntr].index = 0; // Terminate list
  816. free(ifs.ifc_req); // Stop that leak.
  817. regfree(&preg);
  818. return iflist;
  819. }
  820. unsigned int ip_compute_checksum(unsigned char *ippdu, int hlen)
  821. {
  822. unsigned int sum = 0, temp;
  823. unsigned char *p;
  824. unsigned char cs_msb;
  825. unsigned char cs_lsb;
  826. /* Save original checksum */
  827. cs_msb = *IP_IPPDU_CHECKSUM_MSB_PTR(ippdu);
  828. cs_lsb = *IP_IPPDU_CHECKSUM_LSB_PTR(ippdu);
  829. *IP_IPPDU_CHECKSUM_MSB_PTR(ippdu) = 0;
  830. *IP_IPPDU_CHECKSUM_LSB_PTR(ippdu) = 0;
  831. p = ippdu;
  832. hlen /= 2; /* We'll compute taking two bytes a a time */
  833. while(hlen--) { sum += ((*p * 256) + *(p + 1)); p += 2; }
  834. while ((temp = (sum >> 16))) { sum = (temp + (sum & 0xFFFF)); }
  835. /* Restore original checksum */
  836. *IP_IPPDU_CHECKSUM_MSB_PTR(ippdu) = cs_msb;
  837. *IP_IPPDU_CHECKSUM_LSB_PTR(ippdu) = cs_lsb;
  838. return(~sum & 0xFFFF);
  839. }
  840. void ip_update_checksum(unsigned char *ippdu)
  841. {
  842. unsigned int cs;
  843. cs = ip_compute_checksum(ippdu, 4 * IP_IPPDU_IHL(ippdu));
  844. *IP_IPPDU_CHECKSUM_MSB_PTR(ippdu) = (unsigned char)((cs >> 8) & 0xFF);
  845. *IP_IPPDU_CHECKSUM_LSB_PTR(ippdu) = (unsigned char)(cs & 0xFF);
  846. }
  847. static char *IpProtToString( unsigned char prot )
  848. {
  849. switch( prot )
  850. {
  851. case 0x11:
  852. return "UDP";
  853. case 0x6:
  854. return "TCP";
  855. case 0x2f:
  856. return "GRE";
  857. case 0x1:
  858. return "ICMP";
  859. default:
  860. return "???";
  861. }
  862. }
  863. static char *iflistToString( struct iflist *ifp )
  864. {
  865. static char str_tr[80+1];
  866. sprintf(str_tr, "index=%d, ifname=%s, ifaddr=%ld.%ld.%ld.%ld, ifdstaddr=%ld.%ld.%ld.%ld, flags1=0x%04lx",
  867. ifp->index, ifp->ifname,
  868. (ifp->ifaddr)>>24, ((ifp->ifaddr)&0x00ff0000)>>16, ((ifp->ifaddr)&0x0000ff00)>>8, (ifp->ifaddr)&0x000000ff,
  869. (ifp->ifdstaddr)>>24, ((ifp->ifdstaddr)&0x00ff0000)>>16, ((ifp->ifdstaddr)&0x0000ff00)>>8,
  870. (ifp->ifdstaddr)&0x000000ff, ifp->flags1);
  871. return str_tr;
  872. }
  873. static char *iflistLogRToString( struct iflist *ifp, int idx, struct ifsnr *ifnr )
  874. {
  875. static char str_tr[MAX_IFLOGTOSTR]; /*
  876. * This makes function: 1) non-reentrant (doesn't matter).
  877. * 2) not useable multiple times by (s)printf.
  878. */
  879. sprintf(str_tr, "%s", ifp->ifname);
  880. return str_tr;
  881. }
  882. static char *iflistLogIToString( struct iflist *ifp, int idx, struct ifsnr *ifnr )
  883. {
  884. static char str_tr[MAX_IFLOGTOSTR]; /*
  885. * This makes function: 1) non-reentrant (doesn't matter).
  886. * 2) not useable multiple times by (s)printf.
  887. */
  888. sprintf(str_tr, "%s(%d/%d/%d)", ifp->ifname, idx, ifp->index, ifnr->sock_nr);
  889. return str_tr;
  890. }
  891. static void bind_to_iface(int fd, int ifindex)
  892. {
  893. struct sockaddr_ll sll;
  894. memset(&sll, 0, sizeof(sll));
  895. sll.sll_family = AF_PACKET;
  896. sll.sll_ifindex = ifindex;
  897. sll.sll_protocol = htons(ETH_P_ALL);
  898. if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) == -1) {
  899. syslog(LOG_ERR, "bind_to_iface: Error, bind failed! (rv=-1, errno=%d)", errno);
  900. exit(1);
  901. }
  902. }
  903. static void copy_ifsnr(struct ifsnr *from, struct ifsnr *to)
  904. {
  905. int i;
  906. for (i=0; i<MAXIF; ++i)
  907. {
  908. to[i].sock_nr = from[i].sock_nr;
  909. to[i].ifindex = from[i].ifindex;
  910. }
  911. }
  912. static int find_sock_nr(struct ifsnr *l, int ifidx)
  913. {
  914. int i;
  915. for (i=0; i<MAXIF; ++i)
  916. if (l[i].ifindex == ifidx) return l[i].sock_nr;
  917. /* not found */
  918. return -1;
  919. }