BridgeUnix.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002
  1. // SoftEther VPN Source Code
  2. // Cedar Communication Module
  3. //
  4. // SoftEther VPN Server, Client and Bridge are free software under GPLv2.
  5. //
  6. // Copyright (c) 2012-2014 Daiyuu Nobori.
  7. // Copyright (c) 2012-2014 SoftEther VPN Project, University of Tsukuba, Japan.
  8. // Copyright (c) 2012-2014 SoftEther Corporation.
  9. //
  10. // All Rights Reserved.
  11. //
  12. // http://www.softether.org/
  13. //
  14. // Author: Daiyuu Nobori
  15. // Comments: Tetsuo Sugiyama, Ph.D.
  16. //
  17. // This program is free software; you can redistribute it and/or
  18. // modify it under the terms of the GNU General Public License
  19. // version 2 as published by the Free Software Foundation.
  20. //
  21. // This program is distributed in the hope that it will be useful,
  22. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. // GNU General Public License for more details.
  25. //
  26. // You should have received a copy of the GNU General Public License version 2
  27. // along with this program; if not, write to the Free Software
  28. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  29. //
  30. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  31. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  32. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  33. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  34. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  35. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  36. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  37. //
  38. // THE LICENSE AGREEMENT IS ATTACHED ON THE SOURCE-CODE PACKAGE
  39. // AS "LICENSE.TXT" FILE. READ THE TEXT FILE IN ADVANCE TO USE THE SOFTWARE.
  40. //
  41. //
  42. // THIS SOFTWARE IS DEVELOPED IN JAPAN, AND DISTRIBUTED FROM JAPAN,
  43. // UNDER JAPANESE LAWS. YOU MUST AGREE IN ADVANCE TO USE, COPY, MODIFY,
  44. // MERGE, PUBLISH, DISTRIBUTE, SUBLICENSE, AND/OR SELL COPIES OF THIS
  45. // SOFTWARE, THAT ANY JURIDICAL DISPUTES WHICH ARE CONCERNED TO THIS
  46. // SOFTWARE OR ITS CONTENTS, AGAINST US (SOFTETHER PROJECT, SOFTETHER
  47. // CORPORATION, DAIYUU NOBORI OR OTHER SUPPLIERS), OR ANY JURIDICAL
  48. // DISPUTES AGAINST US WHICH ARE CAUSED BY ANY KIND OF USING, COPYING,
  49. // MODIFYING, MERGING, PUBLISHING, DISTRIBUTING, SUBLICENSING, AND/OR
  50. // SELLING COPIES OF THIS SOFTWARE SHALL BE REGARDED AS BE CONSTRUED AND
  51. // CONTROLLED BY JAPANESE LAWS, AND YOU MUST FURTHER CONSENT TO
  52. // EXCLUSIVE JURISDICTION AND VENUE IN THE COURTS SITTING IN TOKYO,
  53. // JAPAN. YOU MUST WAIVE ALL DEFENSES OF LACK OF PERSONAL JURISDICTION
  54. // AND FORUM NON CONVENIENS. PROCESS MAY BE SERVED ON EITHER PARTY IN
  55. // THE MANNER AUTHORIZED BY APPLICABLE LAW OR COURT RULE.
  56. //
  57. // USE ONLY IN JAPAN. DO NOT USE THIS SOFTWARE IN ANOTHER COUNTRY UNLESS
  58. // YOU HAVE A CONFIRMATION THAT THIS SOFTWARE DOES NOT VIOLATE ANY
  59. // CRIMINAL LAWS OR CIVIL RIGHTS IN THAT PARTICULAR COUNTRY. USING THIS
  60. // SOFTWARE IN OTHER COUNTRIES IS COMPLETELY AT YOUR OWN RISK. THE
  61. // SOFTETHER VPN PROJECT HAS DEVELOPED AND DISTRIBUTED THIS SOFTWARE TO
  62. // COMPLY ONLY WITH THE JAPANESE LAWS AND EXISTING CIVIL RIGHTS INCLUDING
  63. // PATENTS WHICH ARE SUBJECTS APPLY IN JAPAN. OTHER COUNTRIES' LAWS OR
  64. // CIVIL RIGHTS ARE NONE OF OUR CONCERNS NOR RESPONSIBILITIES. WE HAVE
  65. // NEVER INVESTIGATED ANY CRIMINAL REGULATIONS, CIVIL LAWS OR
  66. // INTELLECTUAL PROPERTY RIGHTS INCLUDING PATENTS IN ANY OF OTHER 200+
  67. // COUNTRIES AND TERRITORIES. BY NATURE, THERE ARE 200+ REGIONS IN THE
  68. // WORLD, WITH DIFFERENT LAWS. IT IS IMPOSSIBLE TO VERIFY EVERY
  69. // COUNTRIES' LAWS, REGULATIONS AND CIVIL RIGHTS TO MAKE THE SOFTWARE
  70. // COMPLY WITH ALL COUNTRIES' LAWS BY THE PROJECT. EVEN IF YOU WILL BE
  71. // SUED BY A PRIVATE ENTITY OR BE DAMAGED BY A PUBLIC SERVANT IN YOUR
  72. // COUNTRY, THE DEVELOPERS OF THIS SOFTWARE WILL NEVER BE LIABLE TO
  73. // RECOVER OR COMPENSATE SUCH DAMAGES, CRIMINAL OR CIVIL
  74. // RESPONSIBILITIES. NOTE THAT THIS LINE IS NOT LICENSE RESTRICTION BUT
  75. // JUST A STATEMENT FOR WARNING AND DISCLAIMER.
  76. //
  77. //
  78. // SOURCE CODE CONTRIBUTION
  79. // ------------------------
  80. //
  81. // Your contribution to SoftEther VPN Project is much appreciated.
  82. // Please send patches to us through GitHub.
  83. // Read the SoftEther VPN Patch Acceptance Policy in advance:
  84. // http://www.softether.org/5-download/src/9.patch
  85. //
  86. //
  87. // DEAR SECURITY EXPERTS
  88. // ---------------------
  89. //
  90. // If you find a bug or a security vulnerability please kindly inform us
  91. // about the problem immediately so that we can fix the security problem
  92. // to protect a lot of users around the world as soon as possible.
  93. //
  94. // Our e-mail address for security reports is:
  95. // softether-vpn-security [at] softether.org
  96. //
  97. // Please note that the above e-mail address is not a technical support
  98. // inquiry address. If you need technical assistance, please visit
  99. // http://www.softether.org/ and ask your question on the users forum.
  100. //
  101. // Thank you for your cooperation.
  102. //
  103. //
  104. // NO MEMORY OR RESOURCE LEAKS
  105. // ---------------------------
  106. //
  107. // The memory-leaks and resource-leaks verification under the stress
  108. // test has been passed before release this source code.
  109. // BridgeUnix.c
  110. // Ethernet Bridge Program (for UNIX)
  111. //#define BRIDGE_C
  112. //#define UNIX_LINUX
  113. #include <GlobalConst.h>
  114. #ifdef BRIDGE_C
  115. #include <stdio.h>
  116. #include <stdlib.h>
  117. #include <string.h>
  118. #include <wchar.h>
  119. #include <stdarg.h>
  120. #include <time.h>
  121. #include <errno.h>
  122. #include <Mayaqua/Mayaqua.h>
  123. #include <Cedar/Cedar.h>
  124. #ifdef UNIX_SOLARIS
  125. #include <sys/sockio.h>
  126. #endif
  127. #ifdef BRIDGE_PCAP
  128. #include <pcap.h>
  129. #endif // BRIDGE_PCAP
  130. #ifdef BRIDGE_BPF
  131. #include <sys/ioctl.h>
  132. #include <net/bpf.h>
  133. #include <net/if_types.h>
  134. #include <net/if_dl.h>
  135. #include <ifaddrs.h>
  136. #endif // BRIDGE_BPF
  137. #ifdef UNIX_LINUX
  138. struct my_tpacket_auxdata
  139. {
  140. UINT tp_status;
  141. UINT tp_len;
  142. UINT tp_snaplen;
  143. USHORT tp_mac;
  144. USHORT tp_net;
  145. USHORT tp_vlan_tci;
  146. USHORT tp_vlan_tpid;
  147. };
  148. #define MY_TP_STATUS_VLAN_VALID (1 << 4)
  149. #define MY_TP_STATUS_VLAN_TPID_VALID (1 << 6)
  150. #define MY_PACKET_AUXDATA 8
  151. #endif // UNIX_LINUX
  152. // Initialize
  153. void InitEth()
  154. {
  155. }
  156. // Free
  157. void FreeEth()
  158. {
  159. }
  160. // Check whether interface description string of Ethernet device can be retrieved in this system
  161. bool EthIsInterfaceDescriptionSupportedUnix()
  162. {
  163. bool ret = false;
  164. DIRLIST *d = EnumDir("/etc/sysconfig/networking/devices/");
  165. if (d == NULL)
  166. {
  167. return false;
  168. }
  169. if (d->NumFiles >= 1)
  170. {
  171. ret = true;
  172. }
  173. FreeDir(d);
  174. return ret;
  175. }
  176. // Get interface description string
  177. bool EthGetInterfaceDescriptionUnix(char *name, char *str, UINT size)
  178. {
  179. char tmp[MAX_SIZE];
  180. bool ret = false;
  181. BUF *b;
  182. // Validate arguments
  183. if (name == NULL || str == NULL)
  184. {
  185. return false;
  186. }
  187. StrCpy(str, size, name);
  188. Format(tmp, sizeof(tmp), "/etc/sysconfig/networking/devices/ifcfg-%s", name);
  189. b = ReadDump(tmp);
  190. if (b != NULL)
  191. {
  192. char *line = CfgReadNextLine(b);
  193. if (IsEmptyStr(line) == false)
  194. {
  195. if (StartWith(line, "#"))
  196. {
  197. char tmp[MAX_SIZE];
  198. StrCpy(tmp, sizeof(tmp), line + 1);
  199. Trim(tmp);
  200. tmp[60] = 0;
  201. StrCpy(str, size, tmp);
  202. ret = true;
  203. }
  204. }
  205. Free(line);
  206. FreeBuf(b);
  207. }
  208. return ret;
  209. }
  210. // Open raw socket
  211. int UnixEthOpenRawSocket()
  212. {
  213. #ifdef UNIX_LINUX
  214. int s;
  215. s = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
  216. if (s < 0)
  217. {
  218. return INVALID_SOCKET;
  219. }
  220. else
  221. {
  222. return s;
  223. }
  224. #else // UNIX_LINUX
  225. return -1;
  226. #endif // UNIX_LINUX
  227. }
  228. // Is Ethernet device control supported?
  229. bool IsEthSupported()
  230. {
  231. bool ret = false;
  232. #if defined(UNIX_LINUX)
  233. ret = IsEthSupportedLinux();
  234. #elif defined(UNIX_SOLARIS)
  235. ret = IsEthSupportedSolaris();
  236. #elif defined(BRIDGE_PCAP)
  237. ret = true;
  238. #elif defined(BRIDGE_BPF)
  239. ret = true;
  240. #endif
  241. return ret;
  242. }
  243. #ifdef UNIX_LINUX
  244. bool IsEthSupportedLinux()
  245. {
  246. int s;
  247. // Try to open a raw socket
  248. s = UnixEthOpenRawSocket();
  249. if (s == INVALID_SOCKET)
  250. {
  251. // fail
  252. return false;
  253. }
  254. // success
  255. closesocket(s);
  256. return true;
  257. }
  258. #endif // UNIX_LINUX
  259. #ifdef UNIX_SOLARIS
  260. bool IsEthSupportedSolaris()
  261. {
  262. return true;
  263. }
  264. #endif // UNIX_SOLARIS
  265. #ifdef UNIX_SOLARIS
  266. // Get Ethernet device list on Solaris
  267. TOKEN_LIST *GetEthListSolaris()
  268. {
  269. TOKEN_LIST *t;
  270. int i, s;
  271. LIST *o;
  272. o = NewListFast(CompareStr);
  273. s = socket(AF_INET, SOCK_DGRAM, 0);
  274. if (s != INVALID_SOCKET)
  275. {
  276. struct lifnum lifn;
  277. lifn.lifn_family = AF_INET;
  278. lifn.lifn_flags = 0;
  279. if (ioctl(s, SIOCGLIFNUM, (char *)&lifn) >= 0)
  280. {
  281. struct lifconf lifc;
  282. struct lifreq *buf;
  283. UINT numifs;
  284. UINT bufsize;
  285. numifs = lifn.lifn_count;
  286. Debug("NumIFs:%d\n",numifs);
  287. bufsize = numifs * sizeof(struct lifreq);
  288. buf = Malloc(bufsize);
  289. lifc.lifc_family = AF_INET;
  290. lifc.lifc_flags = 0;
  291. lifc.lifc_len = bufsize;
  292. lifc.lifc_buf = (char*) buf;
  293. if (ioctl(s, SIOCGLIFCONF, (char *)&lifc) >= 0)
  294. {
  295. for (i = 0; i<numifs; i++)
  296. {
  297. if(StartWith(buf[i].lifr_name, "lo") == false){
  298. Add(o, CopyStr(buf[i].lifr_name));
  299. }
  300. }
  301. }
  302. Free(buf);
  303. }
  304. closesocket(s);
  305. }
  306. Sort(o);
  307. t = ZeroMalloc(sizeof(TOKEN_LIST));
  308. t->NumTokens = LIST_NUM(o);
  309. t->Token = ZeroMalloc(sizeof(char *) * t->NumTokens);
  310. for (i = 0;i < LIST_NUM(o);i++)
  311. {
  312. char *name = LIST_DATA(o, i);
  313. t->Token[i] = name;
  314. }
  315. ReleaseList(o);
  316. return t;
  317. }
  318. #endif // UNIX_SOLARIS
  319. #ifdef UNIX_LINUX
  320. // Get Ethernet device list on Linux
  321. TOKEN_LIST *GetEthListLinux()
  322. {
  323. struct ifreq ifr;
  324. TOKEN_LIST *t;
  325. UINT i, n;
  326. int s;
  327. LIST *o;
  328. char name[MAX_SIZE];
  329. o = NewListFast(CompareStr);
  330. s = UnixEthOpenRawSocket();
  331. if (s != INVALID_SOCKET)
  332. {
  333. n = 0;
  334. for (i = 0;;i++)
  335. {
  336. Zero(&ifr, sizeof(ifr));
  337. ifr.ifr_ifindex = i;
  338. if (ioctl(s, SIOCGIFNAME, &ifr) >= 0)
  339. {
  340. n = 0;
  341. StrCpy(name, sizeof(name), ifr.ifr_name);
  342. Zero(&ifr, sizeof(ifr));
  343. StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), name);
  344. if (ioctl(s, SIOCGIFHWADDR, &ifr) >= 0)
  345. {
  346. UINT type = ifr.ifr_hwaddr.sa_family;
  347. if (type == 1 || type == 2 || type == 6 || type == 800 || type == 801)
  348. {
  349. if (IsInListStr(o, name) == false)
  350. {
  351. if (StartWith(name, "tap_") == false)
  352. {
  353. Add(o, CopyStr(name));
  354. }
  355. }
  356. }
  357. }
  358. }
  359. else
  360. {
  361. n++;
  362. if (n >= 64)
  363. {
  364. break;
  365. }
  366. }
  367. }
  368. closesocket(s);
  369. }
  370. Sort(o);
  371. t = ZeroMalloc(sizeof(TOKEN_LIST));
  372. t->NumTokens = LIST_NUM(o);
  373. t->Token = ZeroMalloc(sizeof(char *) * t->NumTokens);
  374. for (i = 0;i < LIST_NUM(o);i++)
  375. {
  376. char *name = LIST_DATA(o, i);
  377. t->Token[i] = name;
  378. }
  379. ReleaseList(o);
  380. return t;
  381. }
  382. #endif // UNIX_LINUX
  383. #ifdef BRIDGE_PCAP
  384. // Ethernet device list by Pcap API
  385. TOKEN_LIST *GetEthListPcap()
  386. {
  387. pcap_if_t *alldevs;
  388. char errbuf[PCAP_ERRBUF_SIZE];
  389. LIST *o;
  390. TOKEN_LIST *t;
  391. int i;
  392. o = NewListFast(CompareStr);
  393. if( pcap_findalldevs(&alldevs,errbuf) != -1)
  394. {
  395. pcap_if_t *dev = alldevs;
  396. while(dev != NULL)
  397. {
  398. pcap_t *p;
  399. // Device type will be unknown until open the device?
  400. p = pcap_open_live(dev->name, 0, false, 0, errbuf);
  401. if(p != NULL)
  402. {
  403. int datalink = pcap_datalink(p);
  404. // Debug("type:%s\n",pcap_datalink_val_to_name(datalink));
  405. pcap_close(p);
  406. if(datalink == DLT_EN10MB){
  407. // Enumerate only Ethernet type device
  408. Add(o, CopyStr(dev->name));
  409. }
  410. }
  411. dev = dev->next;
  412. }
  413. pcap_freealldevs(alldevs);
  414. }
  415. Sort(o);
  416. t = ZeroMalloc(sizeof(TOKEN_LIST));
  417. t->NumTokens = LIST_NUM(o);
  418. t->Token = ZeroMalloc(sizeof(char *) * t->NumTokens);
  419. for (i = 0;i < LIST_NUM(o);i++)
  420. {
  421. t->Token[i] = LIST_DATA(o, i);
  422. }
  423. ReleaseList(o);
  424. return t;
  425. }
  426. #endif // BRIDGE_PCAP
  427. #ifdef BRIDGE_BPF
  428. // Ethernet device list by BPF API
  429. TOKEN_LIST *GetEthListBpf()
  430. {
  431. struct ifaddrs *ifadrs;
  432. struct sockaddr_dl *sockadr;
  433. LIST *o;
  434. TOKEN_LIST *t;
  435. int i;
  436. o = NewListFast(CompareStr);
  437. // Enumerate network devices
  438. if(getifaddrs( &ifadrs ) == 0)
  439. {
  440. struct ifaddrs *ifadr = ifadrs;
  441. while(ifadr)
  442. {
  443. sockadr = (struct sockaddr_dl*)ifadr->ifa_addr;
  444. if(sockadr->sdl_family == AF_LINK && sockadr->sdl_type == IFT_ETHER)
  445. {
  446. // Is this Ethernet device?
  447. if(!IsInListStr(o,ifadr->ifa_name))
  448. {
  449. // Ignore the foregoing device (for device which have multiple MAC address)
  450. Add(o, CopyStr(ifadr->ifa_name));
  451. }
  452. }
  453. ifadr = ifadr -> ifa_next;
  454. }
  455. freeifaddrs(ifadrs);
  456. }
  457. Sort(o);
  458. t = ZeroMalloc(sizeof(TOKEN_LIST));
  459. t->NumTokens = LIST_NUM(o);
  460. t->Token = ZeroMalloc(sizeof(char *) * t->NumTokens);
  461. for (i = 0;i < LIST_NUM(o);i++)
  462. {
  463. t->Token[i] = LIST_DATA(o, i);
  464. }
  465. ReleaseList(o);
  466. return t;
  467. }
  468. #endif // BRIDGE_BPF
  469. // Enumerate Ethernet devices
  470. TOKEN_LIST *GetEthList()
  471. {
  472. TOKEN_LIST *t = NULL;
  473. #if defined(UNIX_LINUX)
  474. t = GetEthListLinux();
  475. #elif defined(UNIX_SOLARIS)
  476. t = GetEthListSolaris();
  477. #elif defined(BRIDGE_PCAP)
  478. t = GetEthListPcap();
  479. #elif defined(BRIDGE_BPF)
  480. t = GetEthListBpf();
  481. #endif
  482. return t;
  483. }
  484. #ifdef UNIX_LINUX
  485. // Open Ethernet device (Linux)
  486. ETH *OpenEthLinux(char *name, bool local, bool tapmode, char *tapaddr)
  487. {
  488. ETH *e;
  489. struct ifreq ifr;
  490. struct sockaddr_ll addr;
  491. int s;
  492. int index;
  493. bool aux_ok = false;
  494. CANCEL *c;
  495. // Validate arguments
  496. if (name == NULL)
  497. {
  498. return NULL;
  499. }
  500. if (tapmode)
  501. {
  502. #ifndef NO_VLAN
  503. // In tap mode
  504. VLAN *v = NewTap(name, tapaddr);
  505. if (v == NULL)
  506. {
  507. return NULL;
  508. }
  509. e = ZeroMalloc(sizeof(ETH));
  510. e->Name = CopyStr(name);
  511. e->Title = CopyStr(name);
  512. e->Cancel = VLanGetCancel(v);
  513. e->IfIndex = 0;
  514. e->Socket = INVALID_SOCKET;
  515. e->Tap = v;
  516. return e;
  517. #else // NO_VLAN
  518. return NULL;
  519. #endif // NO_VLAN
  520. }
  521. s = UnixEthOpenRawSocket();
  522. if (s == INVALID_SOCKET)
  523. {
  524. return NULL;
  525. }
  526. Zero(&ifr, sizeof(ifr));
  527. StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), name);
  528. if (ioctl(s, SIOCGIFINDEX, &ifr) < 0)
  529. {
  530. closesocket(s);
  531. return NULL;
  532. }
  533. index = ifr.ifr_ifindex;
  534. Zero(&addr, sizeof(addr));
  535. addr.sll_family = PF_PACKET;
  536. addr.sll_protocol = htons(ETH_P_ALL);
  537. addr.sll_ifindex = index;
  538. if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
  539. {
  540. closesocket(s);
  541. return NULL;
  542. }
  543. if (local == false)
  544. {
  545. // Enable promiscious mode
  546. Zero(&ifr, sizeof(ifr));
  547. StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), name);
  548. if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
  549. {
  550. // Failed
  551. closesocket(s);
  552. return NULL;
  553. }
  554. ifr.ifr_flags |= IFF_PROMISC;
  555. if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0)
  556. {
  557. // Failed
  558. closesocket(s);
  559. return NULL;
  560. }
  561. }
  562. if (true)
  563. {
  564. int val = 1;
  565. int ss_ret = setsockopt(s, SOL_PACKET, MY_PACKET_AUXDATA, &val, sizeof(val));
  566. if (ss_ret < 0)
  567. {
  568. Debug("eth(%s): setsockopt: PACKET_AUXDATA failed.\n", name);
  569. }
  570. else
  571. {
  572. Debug("eth(%s): setsockopt: PACKET_AUXDATA ok.\n", name);
  573. aux_ok = true;
  574. }
  575. }
  576. e = ZeroMalloc(sizeof(ETH));
  577. e->Name = CopyStr(name);
  578. e->Title = CopyStr(name);
  579. e->IfIndex = index;
  580. e->Socket = s;
  581. e->Linux_IsAuxDataSupported = aux_ok;
  582. c = NewCancel();
  583. UnixDeletePipe(c->pipe_read, c->pipe_write);
  584. c->pipe_read = c->pipe_write = -1;
  585. UnixSetSocketNonBlockingMode(s, true);
  586. c->SpecialFlag = true;
  587. c->pipe_read = s;
  588. e->Cancel = c;
  589. // Get MTU
  590. e->InitialMtu = EthGetMtu(e);
  591. if (tapmode == false)
  592. {
  593. if (GetGlobalServerFlag(GSF_LOCALBRIDGE_NO_DISABLE_OFFLOAD) == false)
  594. {
  595. // Disable hardware offloading
  596. UnixDisableInterfaceOffload(name);
  597. }
  598. }
  599. return e;
  600. }
  601. #endif // UNIX_LINUX
  602. // Get the MTU value
  603. UINT EthGetMtu(ETH *e)
  604. {
  605. #if defined(UNIX_LINUX) || defined(UNIX_BSD) || defined(UNIX_SOLARIS)
  606. UINT ret = 0;
  607. #ifdef UNIX_SOLARIS
  608. struct lifreq ifr;
  609. #else // UNIX_SOLARIS
  610. struct ifreq ifr;
  611. #endif // UNIX_SOLARIS
  612. int s;
  613. // Validate arguments
  614. if (e == NULL || e->Tap != NULL)
  615. {
  616. return 0;
  617. }
  618. if (e->CurrentMtu != 0)
  619. {
  620. return e->CurrentMtu;
  621. }
  622. #if defined(UNIX_BSD) || defined(UNIX_SOLARIS)
  623. s = e->SocketBsdIf;
  624. #else // defined(UNIX_BSD) || defined(UNIX_SOLARIS)
  625. s = e->Socket;
  626. #endif // defined(UNIX_BSD) || defined(UNIX_SOLARIS)
  627. Zero(&ifr, sizeof(ifr));
  628. #ifdef UNIX_SOLARIS
  629. StrCpy(ifr.lifr_name, sizeof(ifr.lifr_name), e->Name);
  630. #else // UNIX_SOLARIS
  631. StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), e->Name);
  632. #endif // UNIX_SOLARIS
  633. #ifdef UNIX_SOLARIS
  634. if (ioctl(s, SIOCGLIFMTU, &ifr) < 0)
  635. {
  636. // failed
  637. return 0;
  638. }
  639. #else // UNIX_SOLARIS
  640. if (ioctl(s, SIOCGIFMTU, &ifr) < 0)
  641. {
  642. // failed
  643. return 0;
  644. }
  645. #endif // UNIX_SOLARIS
  646. #ifdef UNIX_SOLARIS
  647. ret = ifr.lifr_mtu + 14;
  648. #else // UNIX_SOLARIS
  649. ret = ifr.ifr_mtu + 14;
  650. #endif // UNIX_SOLARIS
  651. e->CurrentMtu = ret;
  652. Debug("%s: GetMtu: %u\n", e->Name, ret);
  653. return ret;
  654. #else // defined(UNIX_LINUX) || defined(UNIX_BSD) || defined(UNIX_SOLARIS)
  655. return 0;
  656. #endif // defined(UNIX_LINUX) || defined(UNIX_BSD) || defined(UNIX_SOLARIS)
  657. }
  658. // Set the MTU value
  659. bool EthSetMtu(ETH *e, UINT mtu)
  660. {
  661. #if defined(UNIX_LINUX) || defined(UNIX_BSD) || defined(UNIX_SOLARIS)
  662. UINT ret = 0;
  663. #ifdef UNIX_SOLARIS
  664. struct lifreq ifr;
  665. #else // UNIX_SOLARIS
  666. struct ifreq ifr;
  667. #endif // UNIX_SOLARIS
  668. int s;
  669. // Validate arguments
  670. if (e == NULL || e->Tap != NULL || (mtu > 1 && mtu < 1514))
  671. {
  672. return false;
  673. }
  674. if (mtu == 0 && e->InitialMtu == 0)
  675. {
  676. return false;
  677. }
  678. if (mtu == 0)
  679. {
  680. // Restore initial MTU value when parameter mtu == 0
  681. mtu = e->InitialMtu;
  682. }
  683. #if defined(UNIX_BSD) || defined(UNIX_SOLARIS)
  684. s = e->SocketBsdIf;
  685. #else // defined(UNIX_BSD) || defined(UNIX_SOLARIS)
  686. s = e->Socket;
  687. #endif // defined(UNIX_BSD) || defined(UNIX_SOLARIS)
  688. if (e->CurrentMtu == mtu)
  689. {
  690. // No need to change
  691. return true;
  692. }
  693. Zero(&ifr, sizeof(ifr));
  694. #ifdef UNIX_SOLARIS
  695. StrCpy(ifr.lifr_name, sizeof(ifr.lifr_name), e->Name);
  696. ifr.lifr_mtu = mtu - 14;
  697. #else // UNIX_SOLARIS
  698. StrCpy(ifr.ifr_name, sizeof(ifr.ifr_name), e->Name);
  699. ifr.ifr_mtu = mtu - 14;
  700. #endif // UNIX_SOLARIS
  701. #ifdef UNIX_SOLARIS
  702. if (ioctl(s, SIOCSLIFMTU, &ifr) < 0)
  703. {
  704. // Failed
  705. return false;
  706. }
  707. #else // UNIX_SOLARIS
  708. if (ioctl(s, SIOCSIFMTU, &ifr) < 0)
  709. {
  710. // Failed
  711. return false;
  712. }
  713. #endif // UNIX_SOLARIS
  714. e->CurrentMtu = mtu;
  715. Debug("%s: SetMtu: %u\n", e->Name, mtu);
  716. return true;
  717. #else // defined(UNIX_LINUX) || defined(UNIX_BSD) || defined(UNIX_SOLARIS)
  718. return false;
  719. #endif // defined(UNIX_LINUX) || defined(UNIX_BSD) || defined(UNIX_SOLARIS)
  720. }
  721. // Is changing MTU supported?
  722. bool EthIsChangeMtuSupported(ETH *e)
  723. {
  724. #if defined(UNIX_LINUX) || defined(UNIX_BSD) || defined(UNIX_SOLARIS)
  725. // Validate arguments
  726. if (e == NULL || e->Tap != NULL)
  727. {
  728. return false;
  729. }
  730. return true;
  731. #else // defined(UNIX_LINUX) || defined(UNIX_BSD) || defined(UNIX_SOLARIS)
  732. return false;
  733. #endif // defined(UNIX_LINUX) || defined(UNIX_BSD) || defined(UNIX_SOLARIS)
  734. }
  735. #ifdef UNIX_SOLARIS
  736. // Open Ethernet adapter (Solaris)
  737. ETH *OpenEthSolaris(char *name, bool local, bool tapmode, char *tapaddr)
  738. {
  739. char devname[MAX_SIZE];
  740. UINT devid;
  741. int fd;
  742. ETH *e;
  743. CANCEL *c;
  744. struct strioctl sioc;
  745. // Validate arguments
  746. if (name == NULL || tapmode != false)
  747. {
  748. return NULL;
  749. }
  750. // Parse device name
  751. if (ParseUnixEthDeviceName(devname, sizeof(devname), &devid, name) == false)
  752. {
  753. return NULL;
  754. }
  755. // Open the device
  756. fd = open(devname, O_RDWR);
  757. if (fd == -1)
  758. {
  759. // Failed
  760. return NULL;
  761. }
  762. // Attach to the device
  763. if (DlipAttatchRequest(fd, devid) == false)
  764. {
  765. // Failed
  766. close(fd);
  767. return NULL;
  768. }
  769. // Verify ACK message
  770. if (DlipReceiveAck(fd) == false)
  771. {
  772. // Failed
  773. close(fd);
  774. return NULL;
  775. }
  776. // Bind to SAP
  777. if (DlipBindRequest(fd) == false)
  778. {
  779. // Failed
  780. close(fd);
  781. return NULL;
  782. }
  783. // Verify ACK message
  784. if (DlipReceiveAck(fd) == false)
  785. {
  786. // Failed
  787. close(fd);
  788. return NULL;
  789. }
  790. // Set to ignore SAP and promiscuous mode
  791. if (DlipPromiscuous(fd, DL_PROMISC_SAP) == false)
  792. {
  793. // Failed
  794. close(fd);
  795. return NULL;
  796. }
  797. // Verify ACK message
  798. if (DlipReceiveAck(fd) == false)
  799. {
  800. // Failed
  801. close(fd);
  802. return NULL;
  803. }
  804. // Set to the mode to receive self sending packet
  805. if (DlipPromiscuous(fd, DL_PROMISC_PHYS) == false)
  806. {
  807. // Failed
  808. close(fd);
  809. return NULL;
  810. }
  811. // Verify ACK message
  812. if (DlipReceiveAck(fd) == false)
  813. {
  814. // Failed
  815. close(fd);
  816. return NULL;
  817. }
  818. // Set to raw mode
  819. sioc.ic_cmd = DLIOCRAW;
  820. sioc.ic_timout = -1;
  821. sioc.ic_len = 0;
  822. sioc.ic_dp = NULL;
  823. if (ioctl(fd, I_STR, &sioc) < 0)
  824. {
  825. // Failed
  826. close(fd);
  827. return NULL;
  828. }
  829. if (ioctl(fd, I_FLUSH, FLUSHR) < 0)
  830. {
  831. // Failed
  832. close(fd);
  833. return NULL;
  834. }
  835. e = ZeroMalloc(sizeof(ETH));
  836. e->Name = CopyStr(name);
  837. e->Title = CopyStr(name);
  838. c = NewCancel();
  839. UnixDeletePipe(c->pipe_read, c->pipe_write);
  840. c->pipe_read = c->pipe_write = -1;
  841. c->SpecialFlag = true;
  842. c->pipe_read = fd;
  843. e->Cancel = c;
  844. e->IfIndex = -1;
  845. e->Socket = fd;
  846. UnixSetSocketNonBlockingMode(fd, true);
  847. // Get control interface
  848. e->SocketBsdIf = socket(AF_INET, SOCK_DGRAM, 0);
  849. // Get MTU value
  850. e->InitialMtu = EthGetMtu(e);
  851. return e;
  852. }
  853. // Set to promiscuous mode
  854. bool DlipPromiscuous(int fd, UINT level)
  855. {
  856. dl_promiscon_req_t req;
  857. struct strbuf ctl;
  858. int flags;
  859. // Validate arguments
  860. if (fd == -1)
  861. {
  862. return false;
  863. }
  864. Zero(&req, sizeof(req));
  865. req.dl_primitive = DL_PROMISCON_REQ;
  866. req.dl_level = level;
  867. Zero(&ctl, sizeof(ctl));
  868. ctl.maxlen = 0;
  869. ctl.len = sizeof(req);
  870. ctl.buf = (char *)&req;
  871. flags = 0;
  872. if (putmsg(fd, &ctl, NULL, flags) < 0)
  873. {
  874. return false;
  875. }
  876. return true;
  877. }
  878. // Bind to a SAP
  879. bool DlipBindRequest(int fd)
  880. {
  881. dl_bind_req_t req;
  882. struct strbuf ctl;
  883. if (fd == -1)
  884. {
  885. return false;
  886. }
  887. Zero(&req, sizeof(req));
  888. req.dl_primitive = DL_BIND_REQ;
  889. req.dl_service_mode = DL_CLDLS;
  890. req.dl_sap = 0;
  891. Zero(&ctl, sizeof(ctl));
  892. ctl.maxlen = 0;
  893. ctl.len = sizeof(req);
  894. ctl.buf = (char *)&req;
  895. if (putmsg(fd, &ctl, NULL, 0) < 0)
  896. {
  897. return false;
  898. }
  899. return true;
  900. }
  901. // Attach to the device
  902. bool DlipAttatchRequest(int fd, UINT devid)
  903. {
  904. dl_attach_req_t req;
  905. struct strbuf ctl;
  906. int flags;
  907. // Validate arguments
  908. if (fd == -1)
  909. {
  910. return false;
  911. }
  912. Zero(&req, sizeof(req));
  913. req.dl_primitive = DL_ATTACH_REQ;
  914. req.dl_ppa = devid;
  915. Zero(&ctl, sizeof(ctl));
  916. ctl.maxlen = 0;
  917. ctl.len = sizeof(req);
  918. ctl.buf = (char *)&req;
  919. flags = 0;
  920. if (putmsg(fd, &ctl, NULL, flags) < 0)
  921. {
  922. return false;
  923. }
  924. return true;
  925. }
  926. // Verify the ACK message
  927. bool DlipReceiveAck(int fd)
  928. {
  929. union DL_primitives *dlp;
  930. struct strbuf ctl;
  931. int flags = 0;
  932. char *buf;
  933. // Validate arguments
  934. if (fd == -1)
  935. {
  936. return false;
  937. }
  938. buf = MallocFast(SOLARIS_MAXDLBUF);
  939. Zero(&ctl, sizeof(ctl));
  940. ctl.maxlen = SOLARIS_MAXDLBUF;
  941. ctl.len = 0;
  942. ctl.buf = buf;
  943. if (getmsg(fd, &ctl, NULL, &flags) < 0)
  944. {
  945. return false;
  946. }
  947. dlp = (union DL_primitives *)ctl.buf;
  948. if (dlp->dl_primitive != (UINT)DL_OK_ACK && dlp->dl_primitive != (UINT)DL_BIND_ACK)
  949. {
  950. Free(buf);
  951. return false;
  952. }
  953. Free(buf);
  954. return true;
  955. }
  956. #endif // UNIX_SOLARIS
  957. // Separate UNIX device name string into device name and id number
  958. bool ParseUnixEthDeviceName(char *dst_devname, UINT dst_devname_size, UINT *dst_devid, char *src_name)
  959. {
  960. UINT len, i, j;
  961. // Validate arguments
  962. if (dst_devname == NULL || dst_devid == NULL || src_name == NULL)
  963. {
  964. return false;
  965. }
  966. len = strlen(src_name);
  967. // Check string length
  968. if(len == 0)
  969. {
  970. return false;
  971. }
  972. for (i = len-1; i+1 != 0; i--)
  973. {
  974. // Find last non-numeric character
  975. if (src_name[i] < '0' || '9' < src_name[i])
  976. {
  977. // last character must be a number
  978. if(src_name[i+1]==0)
  979. {
  980. return false;
  981. }
  982. *dst_devid = ToInt(src_name + i + 1);
  983. StrCpy(dst_devname, dst_devname_size, "/dev/");
  984. for (j = 0; j<i+1 && j<dst_devname_size-6; j++)
  985. {
  986. dst_devname[j+5] = src_name[j];
  987. }
  988. dst_devname[j+5]=0;
  989. return true;
  990. }
  991. }
  992. // All characters in the string was numeric: error
  993. return false;
  994. }
  995. #if defined(BRIDGE_BPF) || defined(BRIDGE_PCAP)
  996. // Initialize captured packet data structure
  997. struct CAPTUREBLOCK *NewCaptureBlock(UCHAR *data, UINT size){
  998. struct CAPTUREBLOCK *block = Malloc(sizeof(struct CAPTUREBLOCK));
  999. block->Buf = data;
  1000. block->Size = size;
  1001. return block;
  1002. }
  1003. // Free captured packet data structure
  1004. void FreeCaptureBlock(struct CAPTUREBLOCK *block){
  1005. Free(block);
  1006. }
  1007. #endif // BRIDGE_BPF || BRIDGE_PCAP
  1008. #ifdef BRIDGE_PCAP
  1009. // Callback function to receive arriving packet (Pcap)
  1010. void PcapHandler(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
  1011. {
  1012. ETH *e = (ETH*) user;
  1013. struct CAPTUREBLOCK *block;
  1014. UCHAR *data;
  1015. data = Malloc(h->caplen);
  1016. Copy(data, bytes, h->caplen);
  1017. block = NewCaptureBlock(data, h->caplen);
  1018. LockQueue(e->Queue);
  1019. // Discard arriving packet when queue filled
  1020. if(e->QueueSize < BRIDGE_MAX_QUEUE_SIZE){
  1021. InsertQueue(e->Queue, block);
  1022. e->QueueSize += h->caplen;
  1023. }
  1024. UnlockQueue(e->Queue);
  1025. Cancel(e->Cancel);
  1026. return;
  1027. }
  1028. // Relay thread for captured packet (Pcap)
  1029. void PcapThread(THREAD *thread, void *param)
  1030. {
  1031. ETH *e = (ETH*)param;
  1032. pcap_t *p = e->Pcap;
  1033. int ret;
  1034. // Notify initialize completed
  1035. NoticeThreadInit(thread);
  1036. // Return -1:Error -2:Terminated externally
  1037. ret = pcap_loop(p, -1, PcapHandler, (u_char*) e);
  1038. if(ret == -1){
  1039. e->Socket = INVALID_SOCKET;
  1040. pcap_perror(p, "capture");
  1041. }
  1042. return;
  1043. }
  1044. // Open Ethernet adapter (Pcap)
  1045. ETH *OpenEthPcap(char *name, bool local, bool tapmode, char *tapaddr)
  1046. {
  1047. char errbuf[PCAP_ERRBUF_SIZE];
  1048. ETH *e;
  1049. pcap_t *p;
  1050. CANCEL *c;
  1051. // Validate arguments
  1052. if (name == NULL || tapmode != false)
  1053. {
  1054. return NULL;
  1055. }
  1056. // Initialize error message buffer
  1057. errbuf[0] = 0;
  1058. // Open capturing device
  1059. p = pcap_open_live(name, 65535, (local == false), 1, errbuf);
  1060. if(p==NULL)
  1061. {
  1062. return NULL;
  1063. }
  1064. // Set to non-block mode
  1065. // (In old BSD OSs, 'select(2)' don't block normally for BPF device. To prevent busy loop)
  1066. /*
  1067. if(pcap_setnonblock(p, true, errbuf) == -1)
  1068. {
  1069. Debug("pcap_setnonblock:%s\n",errbuf);
  1070. pcap_close(p);
  1071. return NULL;
  1072. }
  1073. */
  1074. e = ZeroMalloc(sizeof(ETH));
  1075. e->Name = CopyStr(name);
  1076. e->Title = CopyStr(name);
  1077. e->Queue = NewQueue();
  1078. e->QueueSize = 0;
  1079. e->Cancel = NewCancel();
  1080. e->IfIndex = -1;
  1081. e->Socket = pcap_get_selectable_fd(p);
  1082. e->Pcap = p;
  1083. e->CaptureThread = NewThread(PcapThread, e);
  1084. WaitThreadInit(e->CaptureThread);
  1085. return e;
  1086. }
  1087. #endif // BRIDGE_PCAP
  1088. #ifdef BRIDGE_BPF
  1089. #ifdef BRIDGE_BPF_THREAD
  1090. // Relay thread for captured packet (BPF)
  1091. void BpfThread(THREAD *thread, void *param)
  1092. {
  1093. ETH *e = (ETH*)param;
  1094. int fd = e->Socket;
  1095. int len;
  1096. int rest; // Rest size in buffer
  1097. UCHAR *next; // Head of next packet in buffer
  1098. struct CAPTUREBLOCK *block; // Data to enqueue
  1099. UCHAR *data;
  1100. struct bpf_hdr *hdr;
  1101. // Allocate the buffer
  1102. UCHAR *buf = Malloc(e->BufSize);
  1103. // Notify initialize completed
  1104. NoticeThreadInit(thread);
  1105. while(1){
  1106. // Determining to exit loop
  1107. if(e->Socket == INVALID_SOCKET){
  1108. break;
  1109. }
  1110. rest = read(fd, buf, e->BufSize);
  1111. if(rest < 0 && errno != EAGAIN){
  1112. // Error
  1113. close(fd);
  1114. e->Socket = INVALID_SOCKET;
  1115. Free(buf);
  1116. Cancel(e->Cancel);
  1117. return;
  1118. }
  1119. next = buf;
  1120. LockQueue(e->Queue);
  1121. while(rest>0){
  1122. // Cut out a packet
  1123. hdr = (struct bpf_hdr*)next;
  1124. // Discard arriving packet when queue filled
  1125. if(e->QueueSize < BRIDGE_MAX_QUEUE_SIZE){
  1126. data = Malloc(hdr->bh_caplen);
  1127. Copy(data, next+(hdr->bh_hdrlen), hdr->bh_caplen);
  1128. block = NewCaptureBlock(data, hdr->bh_caplen);
  1129. InsertQueue(e->Queue, block);
  1130. e->QueueSize += hdr->bh_caplen;
  1131. }
  1132. // Find the head of next packet
  1133. rest -= BPF_WORDALIGN(hdr->bh_hdrlen + hdr->bh_caplen);
  1134. next += BPF_WORDALIGN(hdr->bh_hdrlen + hdr->bh_caplen);
  1135. }
  1136. UnlockQueue(e->Queue);
  1137. Cancel(e->Cancel);
  1138. }
  1139. Free(buf);
  1140. Cancel(e->Cancel);
  1141. return;
  1142. }
  1143. #endif // BRIDGE_BPF_THREAD
  1144. // Open Ethernet adapter (BPF)
  1145. ETH *OpenEthBpf(char *name, bool local, bool tapmode, char *tapaddr)
  1146. {
  1147. ETH *e;
  1148. CANCEL *c;
  1149. char devname[MAX_SIZE];
  1150. int n = 0;
  1151. int fd;
  1152. int ret;
  1153. UINT bufsize;
  1154. struct ifreq ifr;
  1155. struct timeval to;
  1156. // Find unused bpf device and open it
  1157. do{
  1158. Format(devname, sizeof(devname), "/dev/bpf%d", n++);
  1159. fd = open (devname, O_RDWR);
  1160. if(fd<0){
  1161. perror("open");
  1162. }
  1163. }while(fd < 0 && errno == EBUSY);
  1164. // No free bpf device was found
  1165. if(fd < 0){
  1166. Debug("BPF: No minor number are free.\n");
  1167. return NULL;
  1168. }
  1169. // Enlarge buffer size
  1170. n = 524288; // Somehow(In libpcap, this size is 32768)
  1171. while(true){
  1172. // Specify buffer size
  1173. ioctl(fd, BIOCSBLEN, &n);
  1174. // Bind to the network device
  1175. StrCpy(ifr.ifr_name, IFNAMSIZ, name);
  1176. ret = ioctl(fd, BIOCSETIF, &ifr);
  1177. if(ret < 0){
  1178. if(ret == ENOBUFS && n>1500){
  1179. // Inappropriate buffer size
  1180. // Retry with half buffer size
  1181. // If buffer size is under 1500 bytes, something goes wrong
  1182. n /= 2;
  1183. continue;
  1184. }
  1185. Debug("bpf: binding network failed.\n");
  1186. close(fd);
  1187. return NULL;
  1188. }else{
  1189. break;
  1190. }
  1191. }
  1192. bufsize = n;
  1193. // Set to promiscuous mode
  1194. if(local == false){
  1195. if (ioctl(fd, BIOCPROMISC, NULL) < 0){
  1196. printf("bpf: promisc mode failed.\n");
  1197. close(fd);
  1198. return NULL;
  1199. }
  1200. }
  1201. // Set to immediate mode (Return immediately when packet arrives)
  1202. n = 1;
  1203. if (ioctl(fd, BIOCIMMEDIATE, &n) < 0){
  1204. Debug("BPF: non-block mode failed.\n");
  1205. close(fd);
  1206. return NULL;
  1207. }
  1208. // Set receiving self sending packet
  1209. n = 1;
  1210. if (ioctl(fd, BIOCGSEESENT, &n) < 0){
  1211. Debug("BPF: see sent mode failed.\n");
  1212. close(fd);
  1213. return NULL;
  1214. }
  1215. // Header complete mode (Generate whole header of sending packet)
  1216. n = 1;
  1217. if (ioctl(fd, BIOCSHDRCMPLT, &n) < 0){
  1218. Debug("BPF: Header complete mode failed.\n");
  1219. close(fd);
  1220. return NULL;
  1221. }
  1222. // Set timeout delay to 1 second
  1223. to.tv_sec = 1;
  1224. to.tv_usec = 0;
  1225. if (ioctl(fd, BIOCSRTIMEOUT, &to) < 0){
  1226. Debug("BPF: Read timeout setting failed.\n");
  1227. close(fd);
  1228. return NULL;
  1229. }
  1230. e = ZeroMalloc(sizeof(ETH));
  1231. e->Name = CopyStr(name);
  1232. e->Title = CopyStr(name);
  1233. e->IfIndex = -1;
  1234. e->Socket = fd;
  1235. e->BufSize = bufsize;
  1236. #ifdef BRIDGE_BPF_THREAD
  1237. e->Queue = NewQueue();
  1238. e->QueueSize = 0;
  1239. e->Cancel = NewCancel();
  1240. // Start capture thread
  1241. e->CaptureThread = NewThread(BpfThread, e);
  1242. WaitThreadInit(e->CaptureThread);
  1243. #else // BRIDGE_BPF_THREAD
  1244. c = NewCancel();
  1245. UnixDeletePipe(c->pipe_read, c->pipe_write);
  1246. c->pipe_read = c->pipe_write = -1;
  1247. c->SpecialFlag = true;
  1248. c->pipe_read = fd;
  1249. e->Cancel = c;
  1250. e->Buffer = Malloc(bufsize);
  1251. e->Next = e->Buffer;
  1252. e->Rest = 0;
  1253. // Set to non-blocking mode
  1254. UnixSetSocketNonBlockingMode(fd, true);
  1255. #endif // BRIDGE_BPF_THREAD
  1256. // Open interface control socket for FreeBSD
  1257. e->SocketBsdIf = socket(AF_LOCAL, SOCK_DGRAM, 0);
  1258. // Get MTU value
  1259. e->InitialMtu = EthGetMtu(e);
  1260. return e;
  1261. }
  1262. #endif // BRIDGE_BPF
  1263. // Open Ethernet adapter
  1264. ETH *OpenEth(char *name, bool local, bool tapmode, char *tapaddr)
  1265. {
  1266. ETH *ret = NULL;
  1267. #if defined(UNIX_LINUX)
  1268. ret = OpenEthLinux(name, local, tapmode, tapaddr);
  1269. #elif defined(UNIX_SOLARIS)
  1270. ret = OpenEthSolaris(name, local, tapmode, tapaddr);
  1271. #elif defined(BRIDGE_PCAP)
  1272. ret = OpenEthPcap(name, local, tapmode, tapaddr);
  1273. #elif defined(BRIDGE_BPF)
  1274. ret = OpenEthBpf(name, local, tapmode, tapaddr);
  1275. #endif
  1276. return ret;
  1277. }
  1278. typedef struct UNIXTHREAD
  1279. {
  1280. pthread_t thread;
  1281. bool finished;
  1282. } UNIXTHREAD;
  1283. // Close Ethernet adapter
  1284. void CloseEth(ETH *e)
  1285. {
  1286. // Validate arguments
  1287. if (e == NULL)
  1288. {
  1289. return;
  1290. }
  1291. if (e->Tap != NULL)
  1292. {
  1293. #ifndef NO_VLAN
  1294. FreeTap(e->Tap);
  1295. #endif // NO_VLAN
  1296. }
  1297. #ifdef BRIDGE_PCAP
  1298. {
  1299. struct CAPTUREBLOCK *block;
  1300. pcap_breakloop(e->Pcap);
  1301. WaitThread(e->CaptureThread, INFINITE);
  1302. ReleaseThread(e->CaptureThread);
  1303. pcap_close(e->Pcap);
  1304. while (block = GetNext(e->Queue)){
  1305. Free(block->Buf);
  1306. FreeCaptureBlock(block);
  1307. }
  1308. ReleaseQueue(e->Queue);
  1309. }
  1310. #endif // BRIDGE_PCAP
  1311. #ifdef BRIDGE_BPF
  1312. #ifdef BRIDGE_BPF_THREAD
  1313. {
  1314. struct CAPTUREBLOCK *block;
  1315. int fd = e->Socket;
  1316. e->Socket = INVALID_SOCKET;
  1317. WaitThread(e->CaptureThread, INFINITE);
  1318. ReleaseThread(e->CaptureThread);
  1319. e->Socket = fd; // restore to close after
  1320. while (block = GetNext(e->Queue)){
  1321. Free(block->Buf);
  1322. FreeCaptureBlock(block);
  1323. }
  1324. ReleaseQueue(e->Queue);
  1325. }
  1326. #else // BRIDGE_BPF_THREAD
  1327. Free(e->Buffer);
  1328. #endif // BRIDGE_BPF_THREAD
  1329. #endif // BRIDGE_BPF
  1330. ReleaseCancel(e->Cancel);
  1331. Free(e->Name);
  1332. Free(e->Title);
  1333. // Restore MTU value
  1334. EthSetMtu(e, 0);
  1335. if (e->Socket != INVALID_SOCKET)
  1336. {
  1337. #if defined(BRIDGE_BPF) || defined(BRIDGE_PCAP) || defined(UNIX_SOLARIS)
  1338. close(e->Socket);
  1339. #else // BRIDGE_PCAP
  1340. closesocket(e->Socket);
  1341. #endif // BRIDGE_PCAP
  1342. #if defined(BRIDGE_BPF) || defined(UNIX_SOLARIS)
  1343. if (e->SocketBsdIf != INVALID_SOCKET)
  1344. {
  1345. close(e->SocketBsdIf);
  1346. }
  1347. #endif // BRIDGE_BPF || UNIX_SOLARIS
  1348. }
  1349. Free(e);
  1350. }
  1351. // Get cancel object
  1352. CANCEL *EthGetCancel(ETH *e)
  1353. {
  1354. CANCEL *c;
  1355. // Validate arguments
  1356. if (e == NULL)
  1357. {
  1358. return NULL;
  1359. }
  1360. c = e->Cancel;
  1361. AddRef(c->ref);
  1362. return c;
  1363. }
  1364. // Read a packet
  1365. UINT EthGetPacket(ETH *e, void **data)
  1366. {
  1367. UINT ret = 0;
  1368. #if defined(UNIX_LINUX)
  1369. ret = EthGetPacketLinux(e, data);
  1370. #elif defined(UNIX_SOLARIS)
  1371. ret = EthGetPacketSolaris(e, data);
  1372. #elif defined(BRIDGE_PCAP)
  1373. ret = EthGetPacketPcap(e, data);
  1374. #elif defined(BRIDGE_BPF)
  1375. ret = EthGetPacketBpf(e, data);
  1376. #endif
  1377. return ret;
  1378. }
  1379. #ifdef UNIX_LINUX
  1380. UINT EthGetPacketLinux(ETH *e, void **data)
  1381. {
  1382. int s, ret;
  1383. UCHAR tmp[UNIX_ETH_TMP_BUFFER_SIZE];
  1384. struct iovec msg_iov;
  1385. struct msghdr msg_header;
  1386. struct cmsghdr *cmsg;
  1387. union
  1388. {
  1389. struct cmsghdr cmsg;
  1390. char buf[CMSG_SPACE(sizeof(struct my_tpacket_auxdata))];
  1391. } cmsg_buf;
  1392. // Validate arguments
  1393. if (e == NULL || data == NULL)
  1394. {
  1395. return INFINITE;
  1396. }
  1397. if (e->Tap != NULL)
  1398. {
  1399. #ifndef NO_VLAN
  1400. // tap mode
  1401. void *buf;
  1402. UINT size;
  1403. if (VLanGetNextPacket(e->Tap, &buf, &size) == false)
  1404. {
  1405. return INFINITE;
  1406. }
  1407. *data = buf;
  1408. return size;
  1409. #else // NO_VLAN
  1410. return INFINITE;
  1411. #endif
  1412. }
  1413. s = e->Socket;
  1414. if (s == INVALID_SOCKET)
  1415. {
  1416. return INFINITE;
  1417. }
  1418. // Read
  1419. msg_iov.iov_base = tmp;
  1420. msg_iov.iov_len = sizeof(tmp);
  1421. msg_header.msg_name = NULL;
  1422. msg_header.msg_namelen = 0;
  1423. msg_header.msg_iov = &msg_iov;
  1424. msg_header.msg_iovlen = 1;
  1425. if (e->Linux_IsAuxDataSupported)
  1426. {
  1427. memset(&cmsg_buf, 0, sizeof(cmsg_buf));
  1428. msg_header.msg_control = &cmsg_buf;
  1429. msg_header.msg_controllen = sizeof(cmsg_buf);
  1430. }
  1431. else
  1432. {
  1433. msg_header.msg_control = NULL;
  1434. msg_header.msg_controllen = 0;
  1435. }
  1436. msg_header.msg_flags = 0;
  1437. ret = recvmsg(s, &msg_header, 0);
  1438. if (ret == 0 || (ret == -1 && errno == EAGAIN))
  1439. {
  1440. // No packet
  1441. *data = NULL;
  1442. return 0;
  1443. }
  1444. else if (ret == -1 || ret > sizeof(tmp))
  1445. {
  1446. // Error
  1447. *data = NULL;
  1448. e->Socket = INVALID_SOCKET;
  1449. return INFINITE;
  1450. }
  1451. else
  1452. {
  1453. bool flag = false;
  1454. USHORT api_vlan_id = 0;
  1455. USHORT api_vlan_tpid = 0;
  1456. if (e->Linux_IsAuxDataSupported)
  1457. {
  1458. for (cmsg = CMSG_FIRSTHDR(&msg_header); cmsg; cmsg = CMSG_NXTHDR(&msg_header, cmsg))
  1459. {
  1460. struct my_tpacket_auxdata *aux;
  1461. UINT len;
  1462. USHORT vlan_tpid = 0x8100;
  1463. USHORT vlan_id = 0;
  1464. if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct my_tpacket_auxdata)) ||
  1465. cmsg->cmsg_level != SOL_PACKET ||
  1466. cmsg->cmsg_type != MY_PACKET_AUXDATA)
  1467. {
  1468. continue;
  1469. }
  1470. aux = (struct my_tpacket_auxdata *)CMSG_DATA(cmsg);
  1471. if (aux != NULL)
  1472. {
  1473. if (aux->tp_vlan_tci != 0)
  1474. {
  1475. vlan_id = aux->tp_vlan_tci;
  1476. }
  1477. }
  1478. if (vlan_id != 0)
  1479. {
  1480. api_vlan_id = vlan_id;
  1481. api_vlan_tpid = vlan_tpid;
  1482. break;
  1483. }
  1484. }
  1485. if (api_vlan_id != 0 && api_vlan_tpid != 0)
  1486. {
  1487. // VLAN ID has been received with PACKET_AUXDATA.
  1488. // Insert the tag.
  1489. USHORT vlan_id_ne = Endian16(api_vlan_id);
  1490. USHORT vlan_tpid_ne = Endian16(api_vlan_tpid);
  1491. if (ret >= 14)
  1492. {
  1493. if (*((USHORT *)(tmp + 12)) != vlan_tpid_ne)
  1494. {
  1495. *data = MallocFast(ret + 4);
  1496. Copy(*data, tmp, 12);
  1497. Copy(((UCHAR *)*data) + 12, &vlan_tpid_ne, 2);
  1498. Copy(((UCHAR *)*data) + 14, &vlan_id_ne, 2);
  1499. Copy(((UCHAR *)*data) + 16, tmp + 12, ret - 12);
  1500. flag = true;
  1501. ret += 4;
  1502. }
  1503. }
  1504. }
  1505. }
  1506. // Success to read a packet (No VLAN)
  1507. if (flag == false)
  1508. {
  1509. *data = MallocFast(ret);
  1510. Copy(*data, tmp, ret);
  1511. }
  1512. return ret;
  1513. }
  1514. return 0;
  1515. }
  1516. #endif // UNIX_LINUX
  1517. #ifdef UNIX_SOLARIS
  1518. UINT EthGetPacketSolaris(ETH *e, void **data)
  1519. {
  1520. UCHAR tmp[UNIX_ETH_TMP_BUFFER_SIZE];
  1521. struct strbuf buf;
  1522. int s;
  1523. int flags = 0;
  1524. int ret;
  1525. // Validate arguments
  1526. if (e == NULL || data == NULL)
  1527. {
  1528. return INFINITE;
  1529. }
  1530. s = e->Socket;
  1531. if (s == INVALID_SOCKET)
  1532. {
  1533. return INFINITE;
  1534. }
  1535. Zero(&buf, sizeof(buf));
  1536. buf.buf = tmp;
  1537. buf.maxlen = sizeof(tmp);
  1538. ret = getmsg(s, NULL, &buf, &flags);
  1539. if (ret < 0 || ret > sizeof(tmp))
  1540. {
  1541. if (errno == EAGAIN)
  1542. {
  1543. // No packet
  1544. *data = NULL;
  1545. return 0;
  1546. }
  1547. // Error
  1548. *data = NULL;
  1549. return INFINITE;
  1550. }
  1551. ret = buf.len;
  1552. *data = MallocFast(ret);
  1553. Copy(*data, tmp, ret);
  1554. return ret;
  1555. }
  1556. #endif // UNIX_SOLARIS
  1557. #ifdef BRIDGE_PCAP
  1558. UINT EthGetPacketPcap(ETH *e, void **data)
  1559. {
  1560. struct CAPTUREBLOCK *block;
  1561. UINT size;
  1562. LockQueue(e->Queue);
  1563. block = GetNext(e->Queue);
  1564. if(block != NULL){
  1565. e->QueueSize -= block->Size;
  1566. }
  1567. UnlockQueue(e->Queue);
  1568. if(block == NULL){
  1569. *data = NULL;
  1570. if(e->Socket == INVALID_SOCKET){
  1571. return INFINITE;
  1572. }
  1573. return 0;
  1574. }
  1575. *data = block->Buf;
  1576. size = block->Size;
  1577. FreeCaptureBlock(block);
  1578. return size;
  1579. }
  1580. #endif // BRIDGE_PCAP
  1581. #ifdef BRIDGE_BPF
  1582. #ifdef BRIDGE_BPF_THREAD
  1583. UINT EthGetPacketBpf(ETH *e, void **data)
  1584. {
  1585. struct CAPTUREBLOCK *block;
  1586. UINT size;
  1587. LockQueue(e->Queue);
  1588. block = GetNext(e->Queue);
  1589. if(block != NULL){
  1590. e->QueueSize -= block->Size;
  1591. }
  1592. UnlockQueue(e->Queue);
  1593. if(block == NULL){
  1594. *data = NULL;
  1595. if(e->Socket == INVALID_SOCKET){
  1596. return INFINITE;
  1597. }
  1598. return 0;
  1599. }
  1600. *data = block->Buf;
  1601. size = block->Size;
  1602. FreeCaptureBlock(block);
  1603. return size;
  1604. }
  1605. #else // BRIDGE_BPF_THREAD
  1606. UINT EthGetPacketBpf(ETH *e, void **data)
  1607. {
  1608. struct bpf_hdr *hdr;
  1609. if(e->Rest<=0){
  1610. e->Rest = read(e->Socket, e->Buffer, e->BufSize);
  1611. if(e->Rest < 0){
  1612. *data = NULL;
  1613. if(errno != EAGAIN){
  1614. // Error
  1615. return INFINITE;
  1616. }
  1617. // No packet
  1618. return 0;
  1619. }
  1620. e->Next = e->Buffer;
  1621. }
  1622. // Cut out a packet
  1623. hdr = (struct bpf_hdr*)e->Next;
  1624. *data = Malloc(hdr->bh_caplen);
  1625. Copy(*data, e->Next+(hdr->bh_hdrlen), hdr->bh_caplen);
  1626. // Find the head of next packet
  1627. e->Rest -= BPF_WORDALIGN(hdr->bh_hdrlen + hdr->bh_caplen);
  1628. e->Next += BPF_WORDALIGN(hdr->bh_hdrlen + hdr->bh_caplen);
  1629. return hdr->bh_caplen;
  1630. }
  1631. #endif // BRIDGE_BPF_THREAD
  1632. #endif // BRIDGE_BPF
  1633. // Send multiple packets
  1634. void EthPutPackets(ETH *e, UINT num, void **datas, UINT *sizes)
  1635. {
  1636. UINT i;
  1637. // Validate arguments
  1638. if (e == NULL || num == 0 || datas == NULL || sizes == NULL)
  1639. {
  1640. return;
  1641. }
  1642. for (i = 0;i < num;i++)
  1643. {
  1644. EthPutPacket(e, datas[i], sizes[i]);
  1645. }
  1646. }
  1647. // Send a packet
  1648. void EthPutPacket(ETH *e, void *data, UINT size)
  1649. {
  1650. int s, ret;
  1651. // Validate arguments
  1652. if (e == NULL || data == NULL)
  1653. {
  1654. return;
  1655. }
  1656. if (size < 14 || size > MAX_PACKET_SIZE)
  1657. {
  1658. Free(data);
  1659. return;
  1660. }
  1661. if (e->Tap != NULL)
  1662. {
  1663. #ifndef NO_VLAN
  1664. // tap mode
  1665. VLanPutPacket(e->Tap, data, size);
  1666. #endif // NO_VLAN
  1667. return;
  1668. }
  1669. s = e->Socket;
  1670. if (s == INVALID_SOCKET)
  1671. {
  1672. Free(data);
  1673. return;
  1674. }
  1675. // Send to device
  1676. #ifdef BRIDGE_PCAP
  1677. ret = pcap_inject(e->Pcap, data, size);
  1678. if( ret == -1 ){
  1679. #ifdef _DEBUG
  1680. pcap_perror(e->Pcap, "inject");
  1681. #endif // _DEBUG
  1682. Debug("EthPutPacket: ret:%d size:%d\n", ret, size);
  1683. }
  1684. #else // BRIDGE_PCAP
  1685. #ifndef UNIX_LINUX
  1686. ret = write(s, data, size);
  1687. if (ret<0)
  1688. {
  1689. Debug("EthPutPacket: ret:%d errno:%d size:%d\n", ret, errno, size);
  1690. }
  1691. #else // UNIX_LINUX
  1692. {
  1693. struct iovec msg_iov;
  1694. struct msghdr msg_header;
  1695. msg_iov.iov_base = data;
  1696. msg_iov.iov_len = size;
  1697. msg_header.msg_name = NULL;
  1698. msg_header.msg_namelen = 0;
  1699. msg_header.msg_iov = &msg_iov;
  1700. msg_header.msg_iovlen = 1;
  1701. msg_header.msg_control = NULL;
  1702. msg_header.msg_controllen = 0;
  1703. msg_header.msg_flags = 0;
  1704. ret = sendmsg(s, &msg_header, 0);
  1705. if (ret<0)
  1706. {
  1707. Debug("EthPutPacket: ret:%d errno:%d size:%d\n", ret, errno, size);
  1708. }
  1709. }
  1710. #endif // UNIX_LINUX
  1711. #endif //BRIDGE_PCAP
  1712. Free(data);
  1713. }
  1714. #endif // BRIDGE_C
  1715. // Developed by SoftEther VPN Project at University of Tsukuba in Japan.
  1716. // Department of Computer Science has dozens of overly-enthusiastic geeks.
  1717. // Join us: http://www.tsukuba.ac.jp/english/admission/