BridgeUnix.c 41 KB

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