bcrelay.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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 interfaces[32];
  192. static char log_interfaces[MAX_IFLOGTOSTR*MAXIF];
  193. static char log_relayed[(MAX_IFLOGTOSTR-1)*MAXIF+81];
  194. static char *ipsec = "";
  195. static void showusage(char *prog)
  196. {
  197. printf("\nBCrelay v%s\n\n", VERSION);
  198. printf("A broadcast packet repeater. This packet repeater (currently designed for udp packets) will listen\n");
  199. printf(" for broadcast packets. When it receives the packets, it will then re-broadcast the packet.\n\n");
  200. printf("Usage: %s [options], where options are:\n\n", prog);
  201. printf(" [-d] [--daemon] Run as daemon.\n");
  202. printf(" [-h] [--help] Displays this help message.\n");
  203. printf(" [-i] [--incoming <ifin>] Defines from which interface broadcasts will be relayed.\n");
  204. printf(" [-n] [--nolog] No logging/tracing to /var/log/messages.\n");
  205. printf(" [-o] [--outgoing <ifout>] Defines to which interface broadcasts will be relayed.\n");
  206. printf(" [-s] [--ipsec <arg>] Defines an ipsec tunnel to be relayed to.\n");
  207. printf(" Since ipsec tunnels terminate on the same interface, we need to define the broadcast\n");
  208. printf(" address of the other end-point of the tunnel. This is done as ipsec0:x.x.x.255\n");
  209. printf(" [-v] [--version] Displays the BCrelay version number.\n");
  210. printf("\nLog messages and debugging go to syslog as DAEMON.\n\n");
  211. printf("\nInterfaces can be specified as regexpressions, ie. ppp[0-9]+\n\n");
  212. }
  213. static void showversion()
  214. {
  215. printf("BCrelay v%s\n", VERSION);
  216. }
  217. #ifndef HAVE_DAEMON
  218. static void my_daemon(int argc, char **argv)
  219. {
  220. pid_t pid;
  221. #ifndef BCRELAY_BIN
  222. /* Need a smart way to locate the binary -rdv */
  223. #define BCRELAY_BIN argv[0]
  224. #endif
  225. #ifndef HAVE_FORK
  226. /* need to use vfork - eg, uClinux */
  227. char **new_argv;
  228. extern char **environ;
  229. int minusd=0;
  230. int i;
  231. int fdr;
  232. /* Strip -d option */
  233. new_argv = malloc((argc) * sizeof(char **));
  234. fdr = open("/dev/null", O_RDONLY);
  235. new_argv[0] = BCRELAY_BIN;
  236. for (i = 1; argv[i] != NULL; i++) {
  237. if (fdr != 0) { dup2(fdr, 0); close(fdr); }
  238. if ( (strcmp(argv[i],"-d")) == 0 ) {
  239. minusd=1;
  240. }
  241. if (minusd) {
  242. new_argv[i] = argv[i+1];
  243. } else {
  244. new_argv[i] = argv[i];
  245. }
  246. }
  247. syslog(LOG_DEBUG, "Option parse OK, re-execing as daemon");
  248. fflush(stderr);
  249. if ((pid = vfork()) == 0) {
  250. if (setsid() < 0) { /* shouldn't fail */
  251. syslog(LOG_ERR, "Setsid failed!");
  252. _exit(1);
  253. }
  254. chdir("/");
  255. umask(0);
  256. /* execve only returns on an error */
  257. execve(BCRELAY_BIN, new_argv, environ);
  258. exit(1);
  259. } else if (pid > 0) {
  260. syslog(LOG_DEBUG, "Success re-execing as daemon!");
  261. exit(0);
  262. } else {
  263. syslog(LOG_ERR, "Error vforking");
  264. exit(1);
  265. }
  266. #else
  267. pid=fork();
  268. if (pid<0) { syslog(LOG_ERR, "Error forking"); _exit(1); }
  269. if (pid>0) { syslog(LOG_DEBUG, "Parent exits"); _exit(0); }
  270. if (pid==0) { syslog(LOG_DEBUG, "Running as child"); }
  271. /* child (daemon) continues */
  272. if (setsid() < 0) { /* shouldn't fail */
  273. syslog(LOG_ERR, "Setsid failed!");
  274. _exit(1);
  275. }
  276. chdir("/");
  277. #endif
  278. }
  279. #endif
  280. int main(int argc, char **argv) {
  281. regex_t preg;
  282. /* command line options */
  283. int c;
  284. char *ifout = "";
  285. char *ifin = "";
  286. #ifndef BCRELAY
  287. fprintf(stderr,
  288. "bcrelay: pptpd was compiled without support for bcrelay, exiting.\n"
  289. " run configure --with-bcrelay, make, and install.\n");
  290. exit(1);
  291. #endif
  292. /* open a connection to the syslog daemon */
  293. openlog("bcrelay", LOG_PID, PPTP_FACILITY);
  294. while (1) {
  295. int option_index = 0;
  296. static struct option long_options[] =
  297. {
  298. {"nolog", 0, 0, 0},
  299. {"daemon", 0, 0, 0},
  300. {"help", 0, 0, 0},
  301. {"incoming", 1, 0, 0},
  302. {"outgoing", 1, 0, 0},
  303. {"ipsec", 1, 0, 0},
  304. {"version", 0, 0, 0},
  305. {0, 0, 0, 0}
  306. };
  307. c = getopt_long(argc, argv, "ndhi:o:s:v", long_options, &option_index);
  308. if (c == -1)
  309. break;
  310. /* convert long options to short form */
  311. if (c == 0)
  312. c = "ndhiosv"[option_index];
  313. switch (c) {
  314. case 'n':
  315. vnologging = 1;
  316. break;
  317. case 'd':
  318. vdaemon = 1;
  319. break;
  320. case 'h':
  321. showusage(argv[0]);
  322. return 0;
  323. case 'i':
  324. ifin = strdup(optarg);
  325. break;
  326. case 'o':
  327. ifout = strdup(optarg);
  328. break;
  329. case 's':
  330. ipsec = strdup(optarg);
  331. // Validate the ipsec parameters
  332. regcomp(&preg, "ipsec[0-9]+:[0-9]+.[0-9]+.[0-9]+.255", REG_EXTENDED);
  333. if (regexec(&preg, ipsec, 0, NULL, 0)) {
  334. syslog(LOG_INFO,"Bad syntax: %s", ipsec);
  335. fprintf(stderr, "\nBad syntax: %s\n", ipsec);
  336. showusage(argv[0]);
  337. return 0;
  338. } else {
  339. regfree(&preg);
  340. break;
  341. }
  342. case 'v':
  343. showversion();
  344. return 0;
  345. default:
  346. showusage(argv[0]);
  347. return 1;
  348. }
  349. }
  350. if (ifin == "") {
  351. syslog(LOG_INFO,"Incoming interface required!");
  352. showusage(argv[0]);
  353. _exit(1);
  354. }
  355. if (ifout == "" && ipsec == "") {
  356. syslog(LOG_INFO,"Listen-mode or outgoing or IPsec interface required!");
  357. showusage(argv[0]);
  358. _exit(1);
  359. } else {
  360. sprintf(interfaces,"%s|%s", ifin, ifout);
  361. }
  362. // If specified, become Daemon.
  363. if (vdaemon) {
  364. #if HAVE_DAEMON
  365. closelog();
  366. freopen("/dev/null", "r", stdin);
  367. /* set noclose, we want stdout/stderr still attached if we can */
  368. daemon(0, 1);
  369. /* returns to child only */
  370. /* pid will have changed */
  371. openlog("bcrelay", LOG_PID, PPTP_FACILITY);
  372. #else /* !HAVE_DAEMON */
  373. my_daemon(argc, argv);
  374. /* returns to child if !HAVE_FORK
  375. * never returns if HAVE_FORK (re-execs without -d)
  376. */
  377. #endif
  378. } else {
  379. syslog(LOG_INFO, "Running as child\n");
  380. }
  381. mainloop(argc,argv);
  382. _exit(0);
  383. }
  384. static void mainloop(int argc, char **argv)
  385. {
  386. socklen_t salen = sizeof(struct sockaddr_ll);
  387. int i, s, rcg, j, no_discifs_cntr, ifs_change;
  388. int logstr_cntr;
  389. struct iflist *iflist = NULL; // Initialised after the 1st packet
  390. struct sockaddr_ll sa;
  391. struct packet *ipp_p;
  392. char *udppdu; // FIXME: warning: pointer targets in assignment differ in signedness
  393. fd_set sock_set;
  394. struct timeval time_2_wait;
  395. static struct ifsnr old_ifsnr[MAXIF+1]; // Old iflist to socket fd's mapping list
  396. static struct ifsnr cur_ifsnr[MAXIF+1]; // Current iflist to socket fd's mapping list
  397. unsigned char buf[1518];
  398. char *logstr = "";
  399. no_discifs_cntr = MAX_NODISCOVER_IFS;
  400. ifs_change = 0;
  401. /*
  402. * Open general ethernet socket, only used to discover interfaces.
  403. */
  404. if ((s = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL))) < 0)
  405. syslog(LOG_INFO,"%s: Error creating socket", *argv);
  406. /*
  407. * Discover interfaces (initial set) and create a dedicated socket bound to the interface
  408. */
  409. memset(old_ifsnr, -1, sizeof(old_ifsnr));
  410. memset(cur_ifsnr, -1, sizeof(cur_ifsnr));
  411. iflist = discoverActiveInterfaces(s);
  412. for (i=0; iflist[i].index; ++i) {
  413. if ((cur_ifsnr[i].sock_nr = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL))) < 0) {
  414. syslog(LOG_ERR, "mainloop: Error, socket error! (rv=%d, errno=%d)", cur_ifsnr[i].sock_nr, errno);
  415. exit(1);
  416. }
  417. bind_to_iface(cur_ifsnr[i].sock_nr, iflist[i].index);
  418. cur_ifsnr[i].ifindex = iflist[i].index;
  419. }
  420. NVBCR_PRINTF(("Displaying INITIAL active interfaces..\n"));
  421. if (vnologging == 0) {
  422. logstr = log_interfaces;
  423. logstr_cntr = sprintf(logstr, "Initial active interfaces: ");
  424. logstr += logstr_cntr;
  425. }
  426. for (i = 0; iflist[i].index; i++)
  427. {
  428. NVBCR_PRINTF(("\t\tactive interface number: %d, if=(%s), sock_nr=%d\n", i, iflistToString(&(iflist[i])), cur_ifsnr[i].sock_nr));
  429. if (vnologging == 0) {
  430. logstr_cntr = sprintf(logstr, "%s ", iflistLogIToString(&(iflist[i]), i, &(cur_ifsnr[i])));
  431. logstr += logstr_cntr;
  432. }
  433. }
  434. if (vnologging == 0) syslog(LOG_INFO, "%s", log_interfaces);
  435. // Main loop
  436. while (1)
  437. {
  438. /*
  439. * Check all (interface) sockets for incoming packets
  440. */
  441. FD_ZERO(&sock_set);
  442. for (i=0; iflist[i].index; ++i)
  443. {
  444. if (cur_ifsnr[i].sock_nr >= 0) {
  445. FD_SET(cur_ifsnr[i].sock_nr, &sock_set);
  446. }
  447. }
  448. /*
  449. * Don't wait more than MAX_SELECT_WAIT seconds
  450. */
  451. time_2_wait.tv_sec = MAX_SELECT_WAIT;
  452. time_2_wait.tv_usec = 0L;
  453. /* select on sockets */
  454. rcg = select(MAXIF, &sock_set, (fd_set *) NULL,(fd_set *) NULL, &time_2_wait);
  455. if (rcg < 0)
  456. {
  457. syslog(LOG_ERR, "Error, select error! (rv=%d, errno=%d)", rcg, errno);
  458. exit(1);
  459. }
  460. if (rcg == 0)
  461. {
  462. /* TimeOut, rediscover interfaces */
  463. NVBCR_PRINTF(("Select timeout, rediscover interfaces\n"));
  464. copy_ifsnr(cur_ifsnr, old_ifsnr);
  465. memset(cur_ifsnr, -1, sizeof(cur_ifsnr));
  466. iflist = discoverActiveInterfaces(s);
  467. /*
  468. * Build new cur_ifsnr list.
  469. * Make iflist[i] correspond with cur_ifsnr[i], so iflist[i].index == cur_ifsnr[i].ifindex
  470. * The old list (old_ifsnr) is used to compare.
  471. */
  472. for (i=0; iflist[i].index; ++i) {
  473. /* check to see if it is a NEW interface */
  474. int fsnr = find_sock_nr(old_ifsnr, iflist[i].index);
  475. if (fsnr == -1) {
  476. /* found new interface, open dedicated socket and bind it to the interface */
  477. if ((cur_ifsnr[i].sock_nr = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL))) < 0) {
  478. syslog(LOG_ERR, "mainloop: Error, socket error! (rv=%d, errno=%d)", cur_ifsnr[i].sock_nr, errno);
  479. exit(1);
  480. }
  481. bind_to_iface(cur_ifsnr[i].sock_nr, iflist[i].index);
  482. ifs_change = 1;
  483. }
  484. else
  485. {
  486. /*
  487. * not a new interface, socket already openen, interface already
  488. * bound. Update cur_ifsnr.
  489. */
  490. cur_ifsnr[i].sock_nr = fsnr;
  491. }
  492. cur_ifsnr[i].ifindex = iflist[i].index;
  493. }
  494. /* Close disappeared interfaces */
  495. for (i=0; i<MAXIF; ++i)
  496. {
  497. if ((old_ifsnr[i].sock_nr != -1) && (old_ifsnr[i].ifindex != -1) &&
  498. (find_sock_nr(cur_ifsnr, old_ifsnr[i].ifindex) == -1)) {
  499. NVBCR_PRINTF(("Closing an interface (it disappeared), namely: (s_nr=%d, ifidx=%d)\n", old_ifsnr[i].sock_nr, old_ifsnr[i].ifindex));
  500. close(old_ifsnr[i].sock_nr);
  501. old_ifsnr[i].sock_nr = -1;
  502. old_ifsnr[i].ifindex = -1;
  503. ifs_change = 1;
  504. }
  505. }
  506. if (ifs_change == 1)
  507. {
  508. NVBCR_PRINTF(("Active interface set changed --> displaying current active interfaces..\n"));
  509. if (vnologging == 0) {
  510. logstr = log_interfaces;
  511. logstr_cntr = sprintf(logstr, "Active interface set changed to: ");
  512. logstr += logstr_cntr;
  513. }
  514. for (i = 0; iflist[i].index; i++)
  515. {
  516. NVBCR_PRINTF(("\t\tactive interface number: %d, if=(%s), sock_nr=%d\n", i, iflistToString(&(iflist[i])), cur_ifsnr[i].sock_nr));
  517. if (vnologging == 0) {
  518. logstr_cntr = sprintf(logstr, "%s ", iflistLogIToString(&(iflist[i]), i, &(cur_ifsnr[i])));
  519. logstr += logstr_cntr;
  520. }
  521. }
  522. if (vnologging == 0) syslog(LOG_INFO, "%s", log_interfaces);
  523. ifs_change = 0;
  524. }
  525. continue;
  526. }
  527. if (rcg > 0)
  528. {
  529. /* rcg interfaces have pending input */
  530. for (i=0; ((iflist[i].index != 0) && (rcg > 0)); ++i)
  531. {
  532. if ((cur_ifsnr[i].sock_nr != -1) && (FD_ISSET(cur_ifsnr[i].sock_nr,&sock_set)))
  533. {
  534. /* Valid socket number and pending input, let's read */
  535. int rlen = read(cur_ifsnr[i].sock_nr, buf, sizeof(buf));
  536. ipp_p = (struct packet *)&(buf[0]);
  537. 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));
  538. rcg -= 1;
  539. if ( (ipp_p->ip.protocol == IPPROTO_UDP) &&
  540. (((ntohl(ipp_p->ip.daddr)) & 0x000000ff) == 0x000000ff) &&
  541. (ipp_p->ip.ttl != 1) &&
  542. (!((*IP_UDPPDU_CHECKSUM_MSB_PTR((unsigned char *)ipp_p+(4*ipp_p->ip.ihl)) == 0) &&
  543. (*IP_UDPPDU_CHECKSUM_LSB_PTR((unsigned char *)ipp_p+(4*ipp_p->ip.ihl)) == 0))) )
  544. {
  545. int nrsent;
  546. int ifindex_to_exclude = iflist[i].index;
  547. NVBCR_PRINTF(("is an UDP BROADCAST (dstPort=%d, srcPort=%d) (with TTL!=1 and UDP_CHKSUM!=0)\n\n",
  548. ntohs(ipp_p->udp.dest), ntohs(ipp_p->udp.source)));
  549. if (vnologging == 0) {
  550. logstr = log_relayed;
  551. logstr_cntr = sprintf(logstr, "UDP_BroadCast(sp=%d,dp=%d) from: %s relayed to: ", ntohs(ipp_p->udp.source),
  552. ntohs(ipp_p->udp.dest), iflistLogRToString(&(iflist[i]), i, &(cur_ifsnr[i])));
  553. logstr += logstr_cntr;
  554. }
  555. /* going to relay a broadcast packet on all the other interfaces */
  556. for (j=0; iflist[j].index; ++j)
  557. {
  558. int prepare_ipp = 0; // Changing the incoming UDP broadcast needs to be done once
  559. if (iflist[j].index != ifindex_to_exclude)
  560. {
  561. NVBCR_PRINTF(("Going to sent UDP Broadcast on interface: %s, sock_nr=%d\n", iflistToString(&(iflist[j])), cur_ifsnr[j].sock_nr));
  562. memset(&sa, 0, salen);
  563. sa.sll_family = AF_PACKET;
  564. sa.sll_ifindex = iflist[j].index; /* Must be the SIOCGIFINDEX number */
  565. // Set the outgoing hardware address to 1's. True Broadcast
  566. sa.sll_addr[0] = sa.sll_addr[1] = sa.sll_addr[2] = sa.sll_addr[3] = 0xff;
  567. sa.sll_addr[4] = sa.sll_addr[5] = sa.sll_addr[6] = sa.sll_addr[7] = 0xff;
  568. sa.sll_halen = 6;
  569. /*
  570. * htons(ETH_P_IP) is necessary otherwise sendto will
  571. * succeed but no packet is actually sent on the wire (this
  572. * was the case for PPP interfaces, for ETH interfaces an unknown
  573. * LAN frame is sent if htons(ETH_P_IP) is not set as protocol).
  574. */
  575. sa.sll_protocol = htons(ETH_P_IP); /* ETH_P_PPP_MP */
  576. if (prepare_ipp == 0) {
  577. // change TimeToLive to 1, This is to avoid loops, bcrelay will *NOT* relay
  578. // anything with TTL==1.
  579. ipp_p->ip.ttl = 1;
  580. // The CRC gets broken here when sending over ipsec tunnels but that
  581. // should not matter as we reassemble the packet at the other end.
  582. ipp_p->ip.daddr = iflist[j].bcast;
  583. // check IP srcAddr (on some winXP boxes it is found to be
  584. // different from the configured ppp address).
  585. // Only do this for PPP interfaces.
  586. if ((iflist[i].flags1 & IFLIST_FLAGS1_IF_IS_PPP) &&
  587. (ntohl(ipp_p->ip.saddr) != iflist[i].ifdstaddr))
  588. {
  589. ipp_p->ip.saddr = htonl(iflist[i].ifdstaddr);
  590. iflist[i].flags1 |= IFLIST_FLAGS1_CHANGED_INNER_SADDR;
  591. }
  592. // Update IP checkSum (TTL and src/dest IP Address might have changed)
  593. ip_update_checksum((unsigned char *)ipp_p);
  594. /* Disable upper layer checksum */
  595. udppdu = (unsigned char *)ipp_p + (4 * ipp_p->ip.ihl);
  596. *IP_UDPPDU_CHECKSUM_MSB_PTR(udppdu) = (unsigned char)0;
  597. *IP_UDPPDU_CHECKSUM_LSB_PTR(udppdu) = (unsigned char)0;
  598. prepare_ipp = 1;
  599. }
  600. /*
  601. * The beauty of sending IP packets on a PACKET socket of type SOCK_DGRAM is that
  602. * there is no need to concern about the physical/link layer header because it is
  603. * filled in automatically (based on the contents of sa).
  604. */
  605. if ((nrsent = sendto(cur_ifsnr[j].sock_nr, ipp_p, rlen, MSG_DONTWAIT|MSG_TRYHARD, (struct sockaddr *)&sa, salen)) < 0)
  606. {
  607. if (errno == ENETDOWN) {
  608. syslog(LOG_NOTICE, "ignored ENETDOWN from sendto(), a network interface was going down?");
  609. } else if (errno == ENXIO) {
  610. syslog(LOG_NOTICE, "ignored ENXIO from sendto(), a network interface went down?");
  611. } else if (errno == ENOBUFS) {
  612. syslog(LOG_NOTICE, "ignored ENOBUFS from sendto(), temporary shortage of buffer memory");
  613. } else {
  614. syslog(LOG_ERR, "mainloop: Error, sendto failed! (rv=%d, errno=%d)", nrsent, errno);
  615. exit(1);
  616. }
  617. }
  618. NVBCR_PRINTF(("Successfully relayed %d bytes \n", nrsent));
  619. if (vnologging == 0) {
  620. logstr_cntr = sprintf(logstr, "%s ", iflistLogRToString(&(iflist[j]), j, &(cur_ifsnr[j])));
  621. logstr += logstr_cntr;
  622. }
  623. }
  624. }
  625. if (vnologging == 0) syslog(LOG_INFO, "%s", log_relayed);
  626. } else {
  627. NVBCR_PRINTF(("is NOT an UDP BROADCAST (with TTL!=1 and UDP_CHKSUM!=0)\n\n"));
  628. }
  629. }
  630. }
  631. /*
  632. * Don't forget to discover new interfaces if we keep getting
  633. * incoming packets (on an already discovered interface).
  634. */
  635. if (no_discifs_cntr == 0)
  636. {
  637. no_discifs_cntr = MAX_NODISCOVER_IFS;
  638. /* no_discifs_cntr became 0, rediscover interfaces */
  639. NVBCR_PRINTF(("no_discifs_cntr became 0, rediscover interfaces\n"));
  640. copy_ifsnr(cur_ifsnr, old_ifsnr);
  641. memset(cur_ifsnr, -1, sizeof(cur_ifsnr));
  642. iflist = discoverActiveInterfaces(s);
  643. /*
  644. * Build new cur_ifsnr list.
  645. * Make iflist[i] correspond with cur_ifsnr[i], so iflist[i].index == cur_ifsnr[i].ifindex
  646. * The old list (old_ifsnr) is used to compare.
  647. */
  648. for (i=0; iflist[i].index; ++i) {
  649. /* check to see if it is a NEW interface */
  650. int fsnr = find_sock_nr(old_ifsnr, iflist[i].index);
  651. if (fsnr == -1) {
  652. /* found new interface, open dedicated socket and bind it to the interface */
  653. if ((cur_ifsnr[i].sock_nr = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL))) < 0) {
  654. syslog(LOG_ERR, "mainloop: Error, socket error! (rv=%d, errno=%d)", cur_ifsnr[i].sock_nr, errno);
  655. exit(1);
  656. }
  657. bind_to_iface(cur_ifsnr[i].sock_nr, iflist[i].index);
  658. ifs_change = 1;
  659. }
  660. else
  661. {
  662. /*
  663. * not a new interface, socket already openen, interface already
  664. * bound. Update cur_ifsnr.
  665. */
  666. cur_ifsnr[i].sock_nr = fsnr;
  667. }
  668. cur_ifsnr[i].ifindex = iflist[i].index;
  669. }
  670. /* Close disappeared interfaces */
  671. for (i=0; i<MAXIF; ++i)
  672. {
  673. if ((old_ifsnr[i].sock_nr != -1) && (old_ifsnr[i].ifindex != -1) &&
  674. (find_sock_nr(cur_ifsnr, old_ifsnr[i].ifindex) == -1)) {
  675. NVBCR_PRINTF(("Closing an interface (it disappeared), namely: (s_nr=%d, ifidx=%d)\n", old_ifsnr[i].sock_nr, old_ifsnr[i].ifindex));
  676. close(old_ifsnr[i].sock_nr);
  677. old_ifsnr[i].sock_nr = -1;
  678. old_ifsnr[i].ifindex = -1;
  679. ifs_change = 1;
  680. }
  681. }
  682. if (ifs_change == 1)
  683. {
  684. NVBCR_PRINTF(("Active interface set changed --> displaying current active interfaces..\n"));
  685. if (vnologging == 0) {
  686. logstr = log_interfaces;
  687. logstr_cntr = sprintf(logstr, "Active interface set changed to: ");
  688. logstr += logstr_cntr;
  689. }
  690. for (i = 0; iflist[i].index; i++)
  691. {
  692. NVBCR_PRINTF(("\t\tactive interface number: %d, if=(%s), sock_nr=%d\n", i, iflistToString(&(iflist[i])), cur_ifsnr[i].sock_nr));
  693. if (vnologging == 0) {
  694. logstr_cntr = sprintf(logstr, "%s ", iflistLogIToString(&(iflist[i]), i, &(cur_ifsnr[i])));
  695. logstr += logstr_cntr;
  696. }
  697. }
  698. if (vnologging == 0) syslog(LOG_INFO, "%s", log_interfaces);
  699. ifs_change = 0;
  700. }
  701. }
  702. else
  703. {
  704. no_discifs_cntr -= MAX_SELECT_WAIT;
  705. }
  706. }
  707. }
  708. }
  709. // Discover active interfaces
  710. struct iflist *
  711. discoverActiveInterfaces(int s) {
  712. static struct iflist iflist[MAXIF+1]; // Allow for MAXIF interfaces
  713. static struct ifconf ifs;
  714. int i, j, cntr = 0;
  715. regex_t preg;
  716. struct ifreq ifrflags, ifr;
  717. struct sockaddr_in *sin;
  718. /* Reset iflist */
  719. memset(iflist, 0, sizeof(iflist));
  720. /* Reset ifs */
  721. memset(&ifs, 0, sizeof(ifs));
  722. //regcomp(&preg, argv[1], REG_ICASE|REG_EXTENDED);
  723. regcomp(&preg, interfaces, REG_ICASE|REG_EXTENDED);
  724. ifs.ifc_len = MAXIF*sizeof(struct ifreq);
  725. ifs.ifc_req = malloc(ifs.ifc_len);
  726. ioctl(s, SIOCGIFCONF, &ifs); // Discover active interfaces
  727. for (i = 0; i * sizeof(struct ifreq) < ifs.ifc_len && cntr < MAXIF; i++)
  728. {
  729. if (regexec(&preg, ifs.ifc_req[i].ifr_name, 0, NULL, 0) == 0) {
  730. /*
  731. * Get interface flags and check status and type.
  732. * Only if interface is up it will be used.
  733. */
  734. memset(&ifrflags, 0, sizeof(ifrflags));
  735. strncpy(ifrflags.ifr_name, ifs.ifc_req[i].ifr_name, strlen(ifs.ifc_req[i].ifr_name));
  736. if (ioctl(s, SIOCGIFFLAGS, &ifrflags) < 0) {
  737. syslog(LOG_ERR, "discoverActiveInterfaces: Error, SIOCGIFFLAGS Failed! (errno=%d)", errno);
  738. exit(1);
  739. }
  740. if (ifrflags.ifr_flags & IFF_UP)
  741. {
  742. /*
  743. * Get interface index
  744. */
  745. ioctl(s, SIOCGIFINDEX, &ifs.ifc_req[i]);
  746. //Fix 3mar2003
  747. //iflist[cntr].index = (char)ifs.ifc_req[i].ifr_ifindex;
  748. iflist[cntr].index = ifs.ifc_req[i].ifr_ifindex;
  749. /*
  750. * Get interface name
  751. */
  752. for (j=0; (j<sizeof(iflist[cntr].ifname) && j<strlen(ifs.ifc_req[i].ifr_ifrn.ifrn_name)); ++j)
  753. iflist[cntr].ifname[j] = ifs.ifc_req[i].ifr_ifrn.ifrn_name[j];
  754. iflist[cntr].ifname[j+1] = '\0';
  755. /*
  756. * Get local IP address
  757. */
  758. memset(&ifr, 0, sizeof(ifr));
  759. ifr.ifr_addr.sa_family = AF_INET;
  760. (void)strncpy(ifr.ifr_name, iflist[cntr].ifname, strlen(iflist[cntr].ifname)+1);
  761. if (ioctl(s, SIOCGIFADDR, (char *)&ifr) < 0) {
  762. syslog(LOG_ERR, "discoverActiveInterfaces: Error, SIOCGIFADDR Failed! (errno=%d)", errno);
  763. exit(1);
  764. }
  765. sin = (struct sockaddr_in *)&ifr.ifr_addr;
  766. iflist[cntr].ifaddr = ntohl(sin->sin_addr.s_addr);
  767. iflist[cntr].flags1 = 0;
  768. if (ifrflags.ifr_flags & IFF_POINTOPOINT) {
  769. /*
  770. * Get remote IP address (only for PPP interfaces)
  771. */
  772. memset(&ifr, 0, sizeof(ifr));
  773. ifr.ifr_addr.sa_family = AF_INET;
  774. (void)strncpy(ifr.ifr_name, iflist[cntr].ifname, strlen(iflist[cntr].ifname)+1);
  775. if (ioctl(s, SIOCGIFDSTADDR, (char *)&ifr) < 0) {
  776. syslog(LOG_ERR, "discoverActiveInterfaces: Error, SIOCGIFDSTADDR Failed! (errno=%d)", errno);
  777. exit(1);
  778. }
  779. sin = (struct sockaddr_in *)&ifr.ifr_addr;
  780. iflist[cntr].ifdstaddr = ntohl(sin->sin_addr.s_addr);
  781. iflist[cntr].flags1 |= IFLIST_FLAGS1_IF_IS_PPP;
  782. iflist[cntr].bcast = INADDR_BROADCAST;
  783. }
  784. else if (ifrflags.ifr_flags & IFF_BROADCAST)
  785. {
  786. iflist[cntr].ifdstaddr = 0;
  787. iflist[cntr].flags1 |= IFLIST_FLAGS1_IF_IS_ETH;
  788. iflist[cntr].bcast = INADDR_BROADCAST;
  789. }
  790. else
  791. {
  792. iflist[cntr].ifdstaddr = 0;
  793. iflist[cntr].flags1 |= IFLIST_FLAGS1_IF_IS_UNKNOWN;
  794. iflist[cntr].bcast = INADDR_BROADCAST;
  795. }
  796. cntr++;
  797. }
  798. // IPSEC tunnels are a fun one. We must change the destination address
  799. // so that it will be routed to the correct tunnel end point.
  800. // We can define several tunnel end points for the same ipsec interface.
  801. } else if (ipsec != "" && strncmp(ifs.ifc_req[i].ifr_name, "ipsec", 5) == 0) {
  802. if (strncmp(ifs.ifc_req[i].ifr_name, ipsec, 6) == 0) {
  803. struct hostent *hp = gethostbyname(ipsec+7);
  804. ioctl(s, SIOCGIFINDEX, &ifs.ifc_req[i]);
  805. iflist[cntr].index = ifs.ifc_req[i].ifr_ifindex; /* Store the SIOCGIFINDEX number */
  806. memcpy(&(iflist[cntr++].bcast), hp->h_addr, sizeof(u_int32_t));
  807. }
  808. }
  809. }
  810. iflist[cntr].index = 0; // Terminate list
  811. free(ifs.ifc_req); // Stop that leak.
  812. regfree(&preg);
  813. return iflist;
  814. }
  815. unsigned int ip_compute_checksum(unsigned char *ippdu, int hlen)
  816. {
  817. unsigned int sum = 0, temp;
  818. unsigned char *p;
  819. unsigned char cs_msb;
  820. unsigned char cs_lsb;
  821. /* Save original checksum */
  822. cs_msb = *IP_IPPDU_CHECKSUM_MSB_PTR(ippdu);
  823. cs_lsb = *IP_IPPDU_CHECKSUM_LSB_PTR(ippdu);
  824. *IP_IPPDU_CHECKSUM_MSB_PTR(ippdu) = 0;
  825. *IP_IPPDU_CHECKSUM_LSB_PTR(ippdu) = 0;
  826. p = ippdu;
  827. hlen /= 2; /* We'll compute taking two bytes a a time */
  828. while(hlen--) { sum += ((*p * 256) + *(p + 1)); p += 2; }
  829. while ((temp = (sum >> 16))) { sum = (temp + (sum & 0xFFFF)); }
  830. /* Restore original checksum */
  831. *IP_IPPDU_CHECKSUM_MSB_PTR(ippdu) = cs_msb;
  832. *IP_IPPDU_CHECKSUM_LSB_PTR(ippdu) = cs_lsb;
  833. return(~sum & 0xFFFF);
  834. }
  835. void ip_update_checksum(unsigned char *ippdu)
  836. {
  837. unsigned int cs;
  838. cs = ip_compute_checksum(ippdu, 4 * IP_IPPDU_IHL(ippdu));
  839. *IP_IPPDU_CHECKSUM_MSB_PTR(ippdu) = (unsigned char)((cs >> 8) & 0xFF);
  840. *IP_IPPDU_CHECKSUM_LSB_PTR(ippdu) = (unsigned char)(cs & 0xFF);
  841. }
  842. static char *IpProtToString( unsigned char prot )
  843. {
  844. switch( prot )
  845. {
  846. case 0x11:
  847. return "UDP";
  848. case 0x6:
  849. return "TCP";
  850. case 0x2f:
  851. return "GRE";
  852. case 0x1:
  853. return "ICMP";
  854. default:
  855. return "???";
  856. }
  857. }
  858. static char *iflistToString( struct iflist *ifp )
  859. {
  860. static char str_tr[80+1];
  861. sprintf(str_tr, "index=%d, ifname=%s, ifaddr=%ld.%ld.%ld.%ld, ifdstaddr=%ld.%ld.%ld.%ld, flags1=0x%04lx",
  862. ifp->index, ifp->ifname,
  863. (ifp->ifaddr)>>24, ((ifp->ifaddr)&0x00ff0000)>>16, ((ifp->ifaddr)&0x0000ff00)>>8, (ifp->ifaddr)&0x000000ff,
  864. (ifp->ifdstaddr)>>24, ((ifp->ifdstaddr)&0x00ff0000)>>16, ((ifp->ifdstaddr)&0x0000ff00)>>8,
  865. (ifp->ifdstaddr)&0x000000ff, ifp->flags1);
  866. return str_tr;
  867. }
  868. static char *iflistLogRToString( struct iflist *ifp, int idx, struct ifsnr *ifnr )
  869. {
  870. static char str_tr[MAX_IFLOGTOSTR]; /*
  871. * This makes function: 1) non-reentrant (doesn't matter).
  872. * 2) not useable multiple times by (s)printf.
  873. */
  874. sprintf(str_tr, "%s", ifp->ifname);
  875. return str_tr;
  876. }
  877. static char *iflistLogIToString( struct iflist *ifp, int idx, struct ifsnr *ifnr )
  878. {
  879. static char str_tr[MAX_IFLOGTOSTR]; /*
  880. * This makes function: 1) non-reentrant (doesn't matter).
  881. * 2) not useable multiple times by (s)printf.
  882. */
  883. sprintf(str_tr, "%s(%d/%d/%d)", ifp->ifname, idx, ifp->index, ifnr->sock_nr);
  884. return str_tr;
  885. }
  886. static void bind_to_iface(int fd, int ifindex)
  887. {
  888. struct sockaddr_ll sll;
  889. memset(&sll, 0, sizeof(sll));
  890. sll.sll_family = AF_PACKET;
  891. sll.sll_ifindex = ifindex;
  892. sll.sll_protocol = htons(ETH_P_ALL);
  893. if (bind(fd, (struct sockaddr *) &sll, sizeof(sll)) == -1) {
  894. syslog(LOG_ERR, "bind_to_iface: Error, bind failed! (rv=-1, errno=%d)", errno);
  895. exit(1);
  896. }
  897. }
  898. static void copy_ifsnr(struct ifsnr *from, struct ifsnr *to)
  899. {
  900. int i;
  901. for (i=0; i<MAXIF; ++i)
  902. {
  903. to[i].sock_nr = from[i].sock_nr;
  904. to[i].ifindex = from[i].ifindex;
  905. }
  906. }
  907. static int find_sock_nr(struct ifsnr *l, int ifidx)
  908. {
  909. int i;
  910. for (i=0; i<MAXIF; ++i)
  911. if (l[i].ifindex == ifidx) return l[i].sock_nr;
  912. /* not found */
  913. return -1;
  914. }