00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include "NetworkInterfaces.h"
00043
00044
00045 #include <stddef.h>
00046 #include <syslog.h>
00047 #include <string.h>
00048 #include <errno.h>
00049 #include <unistd.h>
00050 #include <sys/ioctl.h>
00051 #include <fcntl.h>
00052 #include <assert.h>
00053 #include <net/if.h>
00054 #include <netinet/in.h>
00055 #include <linux/if_ether.h>
00056 #include <linux/if_packet.h>
00057 #include <linux/if_tun.h>
00058 #include <netinet/ip.h>
00059 #include <netinet/udp.h>
00060 #include <stdlib.h>
00061
00062
00063 #include "olsr.h"
00064 #include "ipcalc.h"
00065 #include "defs.h"
00066 #include "link_set.h"
00067 #include "tc_set.h"
00068 #include "net_olsr.h"
00069 #include "lq_plugin.h"
00070 #include "olsr_ip_prefix_list.h"
00071
00072
00073 #include "Packet.h"
00074 #include "mdns.h"
00075 #include "Address.h"
00076
00077 int my_MDNS_TTL = 0;
00078
00079
00080 struct TBmfInterface *BmfInterfaces = NULL;
00081 struct TBmfInterface *LastBmfInterface = NULL;
00082
00083
00084
00085 int HighestSkfd = -1;
00086
00087
00088 fd_set InputSet;
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 static int
00102 CreateCaptureSocket(const char *ifName)
00103 {
00104 int ifIndex = if_nametoindex(ifName);
00105 struct packet_mreq mreq;
00106 struct ifreq req;
00107 struct sockaddr_ll bindTo;
00108 int skfd = 0;
00109
00110 if (olsr_cnf->ip_version == AF_INET) {
00111 skfd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IP));
00112 } else {
00113 skfd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_IPV6));
00114 }
00115 if (skfd < 0) {
00116 BmfPError("socket(PF_PACKET) error");
00117 return -1;
00118 }
00119
00120
00121 memset(&mreq, 0, sizeof(struct packet_mreq));
00122 mreq.mr_ifindex = ifIndex;
00123 mreq.mr_type = PACKET_MR_PROMISC;
00124 if (setsockopt(skfd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
00125 BmfPError("setsockopt(PACKET_MR_PROMISC) error");
00126 close(skfd);
00127 return -1;
00128 }
00129
00130
00131 memset(&req, 0, sizeof(struct ifreq));
00132 strncpy(req.ifr_name, ifName, IFNAMSIZ - 1);
00133 req.ifr_name[IFNAMSIZ - 1] = '\0';
00134 if (ioctl(skfd, SIOCGIFHWADDR, &req) < 0) {
00135 BmfPError("error retrieving MAC address");
00136 close(skfd);
00137 return -1;
00138 }
00139
00140
00141 memset(&bindTo, 0, sizeof(bindTo));
00142 bindTo.sll_family = AF_PACKET;
00143 if (olsr_cnf->ip_version == AF_INET) {
00144 bindTo.sll_protocol = htons(ETH_P_IP);
00145 } else {
00146 bindTo.sll_protocol = htons(ETH_P_IPV6);
00147 }
00148 bindTo.sll_ifindex = ifIndex;
00149 memcpy(bindTo.sll_addr, req.ifr_hwaddr.sa_data, IFHWADDRLEN);
00150 bindTo.sll_halen = IFHWADDRLEN;
00151
00152 if (bind(skfd, (struct sockaddr *)&bindTo, sizeof(bindTo)) < 0) {
00153 BmfPError("bind() error");
00154 close(skfd);
00155 return -1;
00156 }
00157
00158
00159 if (fcntl(skfd, F_SETFL, fcntl(skfd, F_GETFL, 0) & ~O_NONBLOCK) < 0) {
00160 BmfPError("fcntl() error");
00161 close(skfd);
00162 return -1;
00163 }
00164
00165 if (NULL == olsr_socket_add(skfd, &DoMDNS, NULL, OLSR_SOCKET_READ)) {
00166 BmfPError("Cannot bind socket to scheduler");
00167 close(skfd);
00168 return -1;
00169 }
00170
00171 return skfd;
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 static int
00189 CreateInterface(const char *ifName, struct interface *olsrIntf)
00190 {
00191 int capturingSkfd = -1;
00192 int encapsulatingSkfd = -1;
00193 int listeningSkfd = -1;
00194 int ioctlSkfd;
00195 struct ifreq ifr;
00196 int nOpened = 0;
00197 struct TBmfInterface *newIf = olsr_malloc(sizeof(struct TBmfInterface), "TBMFInterface (mdns)");
00198
00199 assert(ifName != NULL);
00200
00201 if (newIf == NULL) {
00202 return 0;
00203 }
00204
00205
00206
00207
00208
00209 if ((olsrIntf == NULL)) {
00210 capturingSkfd = CreateCaptureSocket(ifName);
00211 if (capturingSkfd < 0) {
00212 close(encapsulatingSkfd);
00213 free(newIf);
00214 return 0;
00215 }
00216
00217 nOpened++;
00218 }
00219
00220
00221
00222 ioctlSkfd = (capturingSkfd >= 0) ? capturingSkfd : encapsulatingSkfd;
00223
00224
00225 memset(&ifr, 0, sizeof(struct ifreq));
00226 strncpy(ifr.ifr_name, ifName, IFNAMSIZ - 1);
00227 ifr.ifr_name[IFNAMSIZ - 1] = '\0';
00228 if (ioctl(ioctlSkfd, SIOCGIFHWADDR, &ifr) < 0) {
00229 BmfPError("ioctl(SIOCGIFHWADDR) error for interface \"%s\"", ifName);
00230 close(capturingSkfd);
00231 close(encapsulatingSkfd);
00232 free(newIf);
00233 return 0;
00234 }
00235
00236
00237 newIf->capturingSkfd = capturingSkfd;
00238 newIf->encapsulatingSkfd = encapsulatingSkfd;
00239 newIf->listeningSkfd = listeningSkfd;
00240 memcpy(newIf->macAddr, ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
00241 memcpy(newIf->ifName, ifName, IFNAMSIZ);
00242 newIf->olsrIntf = olsrIntf;
00243 if (olsrIntf != NULL) {
00244
00245
00246
00247 newIf->intAddr.v4 = olsrIntf->int_src.v4.sin_addr;
00248 newIf->broadAddr.v4 = olsrIntf->int_multicast.v4.sin_addr;
00249 } else {
00250
00251 memset(&ifr, 0, sizeof(struct ifreq));
00252 strncpy(ifr.ifr_name, ifName, IFNAMSIZ - 1);
00253 ifr.ifr_name[IFNAMSIZ - 1] = '\0';
00254 if (ioctl(ioctlSkfd, SIOCGIFADDR, &ifr) < 0) {
00255 BmfPError("ioctl(SIOCGIFADDR) error for interface \"%s\"", ifName);
00256
00257 newIf->intAddr.v4.s_addr = inet_addr("0.0.0.0");
00258 } else {
00259
00260 newIf->intAddr.v4 = ((struct sockaddr_in *)(ARM_NOWARN_ALIGN(&ifr.ifr_addr)))->sin_addr;
00261 }
00262
00263
00264 memset(&ifr, 0, sizeof(struct ifreq));
00265 strncpy(ifr.ifr_name, ifName, IFNAMSIZ - 1);
00266 ifr.ifr_name[IFNAMSIZ - 1] = '\0';
00267 if (ioctl(ioctlSkfd, SIOCGIFBRDADDR, &ifr) < 0) {
00268 BmfPError("ioctl(SIOCGIFBRDADDR) error for interface \"%s\"", ifName);
00269
00270 newIf->broadAddr.v4.s_addr = inet_addr("0.0.0.0");
00271 } else {
00272
00273 newIf->broadAddr.v4 = ((struct sockaddr_in *)(ARM_NOWARN_ALIGN(&ifr.ifr_broadaddr)))->sin_addr;
00274 }
00275 }
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 if (BmfInterfaces == NULL) {
00289
00290 newIf->next = NULL;
00291 BmfInterfaces = newIf;
00292 LastBmfInterface = newIf;
00293 } else if (olsrIntf != NULL) {
00294
00295 newIf->next = BmfInterfaces;
00296 BmfInterfaces = newIf;
00297 } else {
00298
00299 newIf->next = NULL;
00300 LastBmfInterface->next = newIf;
00301 LastBmfInterface = newIf;
00302 }
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313 return nOpened;
00314 }
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325 int
00326 CreateBmfNetworkInterfaces(struct interface *skipThisIntf)
00327 {
00328 int skfd;
00329 struct ifconf ifc;
00330 int numreqs = 30;
00331 struct ifreq *ifr;
00332 int n;
00333 int nOpenedSockets = 0;
00334
00335
00336 FD_ZERO(&InputSet);
00337
00338 skfd = socket(PF_INET, SOCK_DGRAM, 0);
00339 if (skfd < 0) {
00340 BmfPError("no inet socket available to retrieve interface list");
00341 return -1;
00342 }
00343
00344
00345 ifc.ifc_buf = NULL;
00346 for (;;) {
00347 ifc.ifc_len = sizeof(struct ifreq) * numreqs;
00348 ifc.ifc_buf = realloc(ifc.ifc_buf, ifc.ifc_len);
00349
00350 if (ioctl(skfd, SIOCGIFCONF, &ifc) < 0) {
00351 BmfPError("ioctl(SIOCGIFCONF) error");
00352
00353 close(skfd);
00354 free(ifc.ifc_buf);
00355 return -1;
00356 }
00357 if ((unsigned)ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
00358
00359 numreqs *= 2;
00360 assert(numreqs < 1024);
00361 continue;
00362 }
00363 break;
00364 }
00365
00366 close(skfd);
00367
00368
00369 ifr = ifc.ifc_req;
00370 for (n = ifc.ifc_len / sizeof(struct ifreq); --n >= 0; ifr++) {
00371 struct interface *olsrIntf;
00372 union olsr_ip_addr ipAddr;
00373
00374
00375
00376
00377
00378
00379
00380
00381 ipAddr.v4 = ((struct sockaddr_in *)(ARM_NOWARN_ALIGN(&ifr->ifr_addr)))->sin_addr;
00382 olsrIntf = if_ifwithaddr(&ipAddr);
00383
00384 if (skipThisIntf != NULL && olsrIntf == skipThisIntf) {
00385 continue;
00386 }
00387
00388 if (olsrIntf == NULL && !IsNonOlsrBmfIf(ifr->ifr_name)) {
00389
00390
00391 continue;
00392 }
00393
00394 if (!IsNonOlsrBmfIf(ifr->ifr_name)) {
00395
00396 continue;
00397 }
00398
00399
00400 nOpenedSockets += CreateInterface(ifr->ifr_name, NULL);
00401
00402 }
00403
00404 free(ifc.ifc_buf);
00405
00406 if (BmfInterfaces == NULL) {
00407
00408 } else {
00409
00410 }
00411 return 0;
00412 }
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423 void
00424 AddInterface(struct interface *newIntf)
00425 {
00426 int nOpened;
00427
00428 assert(newIntf != NULL);
00429
00430 nOpened = CreateInterface(newIntf->int_name, newIntf);
00431
00432
00433 }
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451 void
00452 CloseBmfNetworkInterfaces(void)
00453 {
00454 int nClosed = 0;
00455 u_int32_t totalOlsrBmfPacketsRx = 0;
00456 u_int32_t totalOlsrBmfPacketsRxDup = 0;
00457 u_int32_t totalOlsrBmfPacketsTx = 0;
00458 u_int32_t totalNonOlsrBmfPacketsRx = 0;
00459 u_int32_t totalNonOlsrBmfPacketsRxDup = 0;
00460 u_int32_t totalNonOlsrBmfPacketsTx = 0;
00461
00462
00463 struct TBmfInterface *nextBmfIf = BmfInterfaces;
00464 while (nextBmfIf != NULL) {
00465 struct TBmfInterface *bmfIf = nextBmfIf;
00466 nextBmfIf = bmfIf->next;
00467
00468 if (bmfIf->capturingSkfd >= 0) {
00469 close(bmfIf->capturingSkfd);
00470 nClosed++;
00471 }
00472 if (bmfIf->encapsulatingSkfd >= 0) {
00473 close(bmfIf->encapsulatingSkfd);
00474 nClosed++;
00475 }
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494 if (bmfIf->olsrIntf != NULL) {
00495 totalOlsrBmfPacketsRx += bmfIf->nBmfPacketsRx;
00496 totalOlsrBmfPacketsRxDup += bmfIf->nBmfPacketsRxDup;
00497 totalOlsrBmfPacketsTx += bmfIf->nBmfPacketsTx;
00498 } else {
00499 totalNonOlsrBmfPacketsRx += bmfIf->nBmfPacketsRx;
00500 totalNonOlsrBmfPacketsRxDup += bmfIf->nBmfPacketsRxDup;
00501 totalNonOlsrBmfPacketsTx += bmfIf->nBmfPacketsTx;
00502 }
00503
00504 free(bmfIf);
00505 }
00506
00507 BmfInterfaces = NULL;
00508
00509
00510
00511 }
00512
00513 #define MAX_NON_OLSR_IFS 32
00514 static char NonOlsrIfNames[MAX_NON_OLSR_IFS][IFNAMSIZ];
00515 static int nNonOlsrIfs = 0;
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527 int
00528 AddNonOlsrBmfIf(const char *ifName, void *data __attribute__ ((unused)), set_plugin_parameter_addon addon __attribute__ ((unused)))
00529 {
00530 assert(ifName != NULL);
00531
00532 if (nNonOlsrIfs >= MAX_NON_OLSR_IFS) {
00533
00534
00535
00536
00537
00538 return 1;
00539 }
00540
00541 strncpy(NonOlsrIfNames[nNonOlsrIfs], ifName, IFNAMSIZ - 1);
00542 NonOlsrIfNames[nNonOlsrIfs][IFNAMSIZ - 1] = '\0';
00543 nNonOlsrIfs++;
00544 return 0;
00545 }
00546
00547
00548 int
00549 set_MDNS_TTL(const char *MDNS_TTL, void *data __attribute__ ((unused)), set_plugin_parameter_addon addon __attribute__ ((unused)))
00550 {
00551 assert(MDNS_TTL!= NULL);
00552 my_MDNS_TTL = atoi(MDNS_TTL);
00553 return 0;
00554 }
00555
00556
00557
00558
00559
00560
00561
00562
00563 int
00564 IsNonOlsrBmfIf(const char *ifName)
00565 {
00566 int i;
00567
00568 assert(ifName != NULL);
00569
00570 for (i = 0; i < nNonOlsrIfs; i++) {
00571 if (strncmp(NonOlsrIfNames[i], ifName, IFNAMSIZ) == 0)
00572 return 1;
00573 }
00574 return 0;
00575 }