tun.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  1. /****************************************************************************
  2. * drivers/net/tun.c
  3. *
  4. * Copyright (C) 2015-2016 Max Nekludov. All rights reserved.
  5. * Author: Max Nekludov <macscomp@gmail.com>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <stdint.h>
  40. #include <stdbool.h>
  41. #include <time.h>
  42. #include <string.h>
  43. #include <errno.h>
  44. #include <assert.h>
  45. #include <fcntl.h>
  46. #include <debug.h>
  47. #ifndef CONFIG_DISABLE_POLL
  48. # include <poll.h>
  49. #endif
  50. #include <arpa/inet.h>
  51. #include <net/if.h>
  52. #ifdef CONFIG_NET_PKT
  53. # include <nuttx/net/pkt.h>
  54. #endif
  55. #include <nuttx/arch.h>
  56. #include <nuttx/irq.h>
  57. #include <nuttx/wdog.h>
  58. #include <nuttx/wqueue.h>
  59. #include <nuttx/net/arp.h>
  60. #include <nuttx/net/netdev.h>
  61. #include <nuttx/net/ethernet.h>
  62. #include <nuttx/net/tun.h>
  63. #if defined(CONFIG_NET) && defined(CONFIG_NET_TUN)
  64. /****************************************************************************
  65. * Pre-processor Definitions
  66. ****************************************************************************/
  67. /* If processing is not done at the interrupt level, then high priority
  68. * work queue support is required.
  69. */
  70. #if !defined(CONFIG_SCHED_WORKQUEUE)
  71. # error Work queue support is required in this configuration (CONFIG_SCHED_WORKQUEUE)
  72. #endif
  73. /* The low priority work queue is preferred. If it is not enabled, LPWORK
  74. * will be the same as HPWORK.
  75. *
  76. * NOTE: However, the network should NEVER run on the high priority work
  77. * queue! That queue is intended only to service short back end interrupt
  78. * processing that never suspends. Suspending the high priority work queue
  79. * may bring the system to its knees!
  80. */
  81. #define TUNWORK LPWORK
  82. /* CONFIG_TUN_NINTERFACES determines the number of physical interfaces
  83. * that will be supported.
  84. */
  85. #ifndef CONFIG_TUN_NINTERFACES
  86. # define CONFIG_TUN_NINTERFACES 1
  87. #endif
  88. /* TX poll delay = 1 seconds. CLK_TCK is the number of clock ticks per
  89. * second
  90. */
  91. #define TUN_WDDELAY (1*CLK_TCK)
  92. /* This is a helper pointer for accessing the contents of the Ethernet header */
  93. #ifdef CONFIG_NET_ETHERNET
  94. # define BUF ((struct eth_hdr_s *)priv->dev.d_buf)
  95. #endif
  96. /* This is a helper pointer for accessing the contents of the ip header */
  97. #define IPv4BUF ((struct ipv4_hdr_s *)(priv->dev.d_buf + priv->dev.d_llhdrlen))
  98. #define IPv6BUF ((struct ipv6_hdr_s *)(priv->dev.d_buf + priv->dev.d_llhdrlen))
  99. /****************************************************************************
  100. * Private Types
  101. ****************************************************************************/
  102. /* The tun_device_s encapsulates all state information for a single hardware
  103. * interface
  104. */
  105. struct tun_device_s
  106. {
  107. bool bifup; /* true:ifup false:ifdown */
  108. WDOG_ID txpoll; /* TX poll timer */
  109. struct work_s work; /* For deferring poll work to the work queue */
  110. FAR struct file *filep;
  111. #ifndef CONFIG_DISABLE_POLL
  112. FAR struct pollfd *poll_fds;
  113. #endif
  114. bool read_wait;
  115. uint8_t read_buf[CONFIG_NET_TUN_PKTSIZE];
  116. size_t read_d_len;
  117. uint8_t write_buf[CONFIG_NET_TUN_PKTSIZE];
  118. size_t write_d_len;
  119. sem_t waitsem;
  120. sem_t read_wait_sem;
  121. /* This holds the information visible to the NuttX network */
  122. struct net_driver_s dev; /* Interface understood by the network */
  123. };
  124. struct tun_driver_s
  125. {
  126. uint8_t free_tuns;
  127. sem_t waitsem;
  128. };
  129. /****************************************************************************
  130. * Private Function Prototypes
  131. ****************************************************************************/
  132. static void tun_lock(FAR struct tun_device_s *priv);
  133. static void tun_unlock(FAR struct tun_device_s *priv);
  134. /* Common TX logic */
  135. static int tun_fd_transmit(FAR struct tun_device_s *priv);
  136. static int tun_txpoll(struct net_driver_s *dev);
  137. #ifdef CONFIG_NET_ETHERNET
  138. static int tun_txpoll_tap(struct net_driver_s *dev);
  139. #endif
  140. static int tun_txpoll_tun(struct net_driver_s *dev);
  141. /* Interrupt handling */
  142. static void tun_net_receive(FAR struct tun_device_s *priv);
  143. #ifdef CONFIG_NET_ETHERNET
  144. static void tun_net_receive_tap(FAR struct tun_device_s *priv);
  145. #endif
  146. static void tun_net_receive_tun(FAR struct tun_device_s *priv);
  147. static void tun_txdone(FAR struct tun_device_s *priv);
  148. /* Watchdog timer expirations */
  149. static void tun_poll_work(FAR void *arg);
  150. static void tun_poll_expiry(int argc, wdparm_t arg, ...);
  151. /* NuttX callback functions */
  152. static int tun_ifup(FAR struct net_driver_s *dev);
  153. static int tun_ifdown(FAR struct net_driver_s *dev);
  154. static int tun_txavail(FAR struct net_driver_s *dev);
  155. #ifdef CONFIG_NET_MCASTGROUP
  156. static int tun_addmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac);
  157. static int tun_rmmac(FAR struct net_driver_s *dev, FAR const uint8_t *mac);
  158. #endif
  159. #ifdef CONFIG_NET_ICMPv6
  160. static void tun_ipv6multicast(FAR struct tun_device_s *priv);
  161. #endif
  162. static int tun_dev_init(FAR struct tun_device_s *priv, FAR struct file *filep,
  163. FAR const char *devfmt, bool tun);
  164. static int tun_dev_uninit(FAR struct tun_device_s *priv);
  165. /* File interface */
  166. static int tun_open(FAR struct file *filep);
  167. static int tun_close(FAR struct file *filep);
  168. static ssize_t tun_read(FAR struct file *filep, FAR char *buffer,
  169. size_t buflen);
  170. static ssize_t tun_write(FAR struct file *filep, FAR const char *buffer,
  171. size_t buflen);
  172. static int tun_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
  173. #ifndef CONFIG_DISABLE_POLL
  174. static int tun_poll(FAR struct file *filep, FAR struct pollfd *fds,
  175. bool setup);
  176. #endif
  177. /****************************************************************************
  178. * Private Data
  179. ****************************************************************************/
  180. static struct tun_driver_s g_tun;
  181. static struct tun_device_s g_tun_devices[CONFIG_TUN_NINTERFACES];
  182. static const struct file_operations g_tun_file_ops =
  183. {
  184. tun_open, /* open */
  185. tun_close, /* close */
  186. tun_read, /* read */
  187. tun_write, /* write */
  188. 0, /* seek */
  189. tun_ioctl, /* ioctl */
  190. #ifndef CONFIG_DISABLE_POLL
  191. tun_poll, /* poll */
  192. #endif
  193. };
  194. /****************************************************************************
  195. * Private Functions
  196. ****************************************************************************/
  197. /****************************************************************************
  198. * Name: tundev_lock
  199. ****************************************************************************/
  200. static void tundev_lock(FAR struct tun_driver_s *tun)
  201. {
  202. int ret;
  203. do
  204. {
  205. /* Take the semaphore (perhaps waiting) */
  206. ret = nxsem_wait(&tun->waitsem);
  207. /* The only case that an error should occur here is if the wait was
  208. * awakened by a signal.
  209. */
  210. DEBUGASSERT(ret == OK || ret == -EINTR);
  211. }
  212. while (ret == -EINTR);
  213. }
  214. /****************************************************************************
  215. * Name: tundev_unlock
  216. ****************************************************************************/
  217. static void tundev_unlock(FAR struct tun_driver_s *tun)
  218. {
  219. nxsem_post(&tun->waitsem);
  220. }
  221. /****************************************************************************
  222. * Name: tun_lock
  223. ****************************************************************************/
  224. static void tun_lock(FAR struct tun_device_s *priv)
  225. {
  226. int ret;
  227. do
  228. {
  229. /* Take the semaphore (perhaps waiting) */
  230. ret = nxsem_wait(&priv->waitsem);
  231. /* The only case that an error should occur here is if the wait was
  232. * awakened by a signal.
  233. */
  234. DEBUGASSERT(ret == OK || ret == -EINTR);
  235. }
  236. while (ret == -EINTR);
  237. }
  238. /****************************************************************************
  239. * Name: tun_unlock
  240. ****************************************************************************/
  241. static void tun_unlock(FAR struct tun_device_s *priv)
  242. {
  243. nxsem_post(&priv->waitsem);
  244. }
  245. /****************************************************************************
  246. * Name: tun_pollnotify
  247. ****************************************************************************/
  248. #ifndef CONFIG_DISABLE_POLL
  249. static void tun_pollnotify(FAR struct tun_device_s *priv, pollevent_t eventset)
  250. {
  251. FAR struct pollfd *fds = priv->poll_fds;
  252. if (fds == NULL)
  253. {
  254. return;
  255. }
  256. eventset &= fds->events;
  257. if (eventset != 0)
  258. {
  259. fds->revents |= eventset;
  260. nxsem_post(fds->sem);
  261. }
  262. }
  263. #else
  264. # define tun_pollnotify(dev, event)
  265. #endif
  266. /****************************************************************************
  267. * Name: tun_fd_transmit
  268. *
  269. * Description:
  270. * Start hardware transmission. Called either from the txdone interrupt
  271. * handling or from watchdog based polling.
  272. *
  273. * Input Parameters:
  274. * priv - Reference to the driver state structure
  275. *
  276. * Returned Value:
  277. * OK on success; a negated errno on failure
  278. *
  279. * Assumptions:
  280. * May or may not be called from an interrupt handler. In either case,
  281. * global interrupts are disabled, either explicitly or indirectly through
  282. * interrupt handling logic.
  283. *
  284. ****************************************************************************/
  285. static int tun_fd_transmit(FAR struct tun_device_s *priv)
  286. {
  287. NETDEV_TXPACKETS(&priv->dev);
  288. /* Verify that the hardware is ready to send another packet. If we get
  289. * here, then we are committed to sending a packet; Higher level logic
  290. * must have assured that there is no transmission in progress.
  291. */
  292. if (priv->read_wait)
  293. {
  294. priv->read_wait = false;
  295. nxsem_post(&priv->read_wait_sem);
  296. }
  297. tun_pollnotify(priv, POLLIN);
  298. return OK;
  299. }
  300. /****************************************************************************
  301. * Name: tun_txpoll
  302. *
  303. * Description:
  304. * The transmitter is available, check if the network has any outgoing packets
  305. * ready to send. This is a callback from devif_poll(). devif_poll() may
  306. * be called:
  307. *
  308. * 1. When the preceding TX packet send is complete,
  309. * 2. When the preceding TX packet send timesout and the interface is reset
  310. * 3. During normal TX polling
  311. *
  312. * Input Parameters:
  313. * dev - Reference to the NuttX driver state structure
  314. *
  315. * Returned Value:
  316. * OK on success; a negated errno on failure
  317. *
  318. * Assumptions:
  319. * May or may not be called from an interrupt handler. In either case,
  320. * global interrupts are disabled, either explicitly or indirectly through
  321. * interrupt handling logic.
  322. *
  323. ****************************************************************************/
  324. static int tun_txpoll(struct net_driver_s *dev)
  325. {
  326. int ret;
  327. #ifdef CONFIG_NET_ETHERNET
  328. if (dev->d_lltype == NET_LL_ETHERNET)
  329. {
  330. ret = tun_txpoll_tap(dev);
  331. }
  332. else
  333. #endif
  334. {
  335. ret = tun_txpoll_tun(dev);
  336. }
  337. return ret;
  338. }
  339. /****************************************************************************
  340. * Name: tun_txpoll_tap : for tap (ethernet bridge) mode
  341. *
  342. * Description:
  343. * The transmitter is available, check if the network has any outgoing packets
  344. * ready to send. This is a callback from devif_poll(). devif_poll() may
  345. * be called:
  346. *
  347. * 1. When the preceding TX packet send is complete,
  348. * 2. When the preceding TX packet send timesout and the interface is reset
  349. * 3. During normal TX polling
  350. *
  351. * Input Parameters:
  352. * dev - Reference to the NuttX driver state structure
  353. *
  354. * Returned Value:
  355. * OK on success; a negated errno on failure
  356. *
  357. * Assumptions:
  358. * May or may not be called from an interrupt handler. In either case,
  359. * global interrupts are disabled, either explicitly or indirectly through
  360. * interrupt handling logic.
  361. *
  362. ****************************************************************************/
  363. #ifdef CONFIG_NET_ETHERNET
  364. static int tun_txpoll_tap(struct net_driver_s *dev)
  365. {
  366. FAR struct tun_device_s *priv = (FAR struct tun_device_s *)dev->d_private;
  367. /* If the polling resulted in data that should be sent out on the network,
  368. * the field d_len is set to a value > 0.
  369. */
  370. if (priv->dev.d_len > 0)
  371. {
  372. /* Look up the destination MAC address and add it to the Ethernet
  373. * header.
  374. */
  375. #ifdef CONFIG_NET_IPv4
  376. if (IFF_IS_IPv4(priv->dev.d_flags))
  377. {
  378. arp_out(&priv->dev);
  379. }
  380. #endif /* CONFIG_NET_IPv4 */
  381. #ifdef CONFIG_NET_IPv6
  382. if (IFF_IS_IPv6(priv->dev.d_flags))
  383. {
  384. neighbor_out(&priv->dev);
  385. }
  386. #endif /* CONFIG_NET_IPv6 */
  387. if (!devif_loopback(dev))
  388. {
  389. /* Send the packet */
  390. priv->read_d_len = priv->dev.d_len;
  391. tun_fd_transmit(priv);
  392. return 1;
  393. }
  394. }
  395. /* If zero is returned, the polling will continue until all connections have
  396. * been examined.
  397. */
  398. return 0;
  399. }
  400. #endif
  401. /****************************************************************************
  402. * Name: tun_txpoll_tun : for tun (IP tunneling) mode
  403. *
  404. * Description:
  405. * The transmitter is available, check if the network has any outgoing packets
  406. * ready to send. This is a callback from devif_poll(). devif_poll() may
  407. * be called:
  408. *
  409. * 1. When the preceding TX packet send is complete,
  410. * 2. When the preceding TX packet send timesout and the interface is reset
  411. * 3. During normal TX polling
  412. *
  413. * Input Parameters:
  414. * dev - Reference to the NuttX driver state structure
  415. *
  416. * Returned Value:
  417. * OK on success; a negated errno on failure
  418. *
  419. * Assumptions:
  420. * May or may not be called from an interrupt handler. In either case,
  421. * global interrupts are disabled, either explicitly or indirectly through
  422. * interrupt handling logic.
  423. *
  424. ****************************************************************************/
  425. static int tun_txpoll_tun(struct net_driver_s *dev)
  426. {
  427. FAR struct tun_device_s *priv = (FAR struct tun_device_s *)dev->d_private;
  428. /* If the polling resulted in data that should be sent out on the network,
  429. * the field d_len is set to a value > 0.
  430. */
  431. if (priv->dev.d_len > 0)
  432. {
  433. if (!devif_loopback(dev))
  434. {
  435. /* Send the packet */
  436. priv->read_d_len = priv->dev.d_len;
  437. tun_fd_transmit(priv);
  438. return 1;
  439. }
  440. }
  441. /* If zero is returned, the polling will continue until all connections have
  442. * been examined.
  443. */
  444. return 0;
  445. }
  446. /****************************************************************************
  447. * Name: tun_net_receive
  448. *
  449. * Description:
  450. * An interrupt was received indicating the availability of a new RX packet
  451. *
  452. * Input Parameters:
  453. * priv - Reference to the driver state structure
  454. *
  455. * Returned Value:
  456. * None
  457. *
  458. * Assumptions:
  459. * Global interrupts are disabled by interrupt handling logic.
  460. *
  461. ****************************************************************************/
  462. static void tun_net_receive(FAR struct tun_device_s *priv)
  463. {
  464. #ifdef CONFIG_NET_ETHERNET
  465. if (priv->dev.d_lltype == NET_LL_ETHERNET)
  466. {
  467. tun_net_receive_tap(priv);
  468. }
  469. else
  470. #endif
  471. {
  472. tun_net_receive_tun(priv);
  473. }
  474. }
  475. /****************************************************************************
  476. * Name: tun_net_receive_tap : for tap (ethernet bridge) mode
  477. *
  478. * Description:
  479. * An interrupt was received indicating the availability of a new RX packet
  480. *
  481. * Input Parameters:
  482. * priv - Reference to the driver state structure
  483. *
  484. * Returned Value:
  485. * None
  486. *
  487. * Assumptions:
  488. * Global interrupts are disabled by interrupt handling logic.
  489. *
  490. ****************************************************************************/
  491. #ifdef CONFIG_NET_ETHERNET
  492. static void tun_net_receive_tap(FAR struct tun_device_s *priv)
  493. {
  494. /* Copy the data data from the hardware to priv->dev.d_buf. Set amount of
  495. * data in priv->dev.d_len
  496. */
  497. NETDEV_RXPACKETS(&priv->dev);
  498. #ifdef CONFIG_NET_PKT
  499. /* When packet sockets are enabled, feed the frame into the packet tap */
  500. pkt_input(&priv->dev);
  501. #endif
  502. /* We only accept IP packets of the configured type and ARP packets */
  503. #if defined(CONFIG_NET_IPv4)
  504. if (BUF->type == HTONS(ETHTYPE_IP))
  505. {
  506. ninfo("IPv4 frame\n");
  507. NETDEV_RXIPV4(&priv->dev);
  508. /* Give the IPv4 packet to the network layer. */
  509. arp_ipin(&priv->dev);
  510. ipv4_input(&priv->dev);
  511. /* If the above function invocation resulted in data that should be
  512. * sent out on the network, the field d_len will set to a value > 0.
  513. */
  514. if (priv->dev.d_len > 0)
  515. {
  516. /* Update the Ethernet header with the correct MAC address */
  517. #ifdef CONFIG_NET_IPv6
  518. if (IFF_IS_IPv4(priv->dev.d_flags))
  519. #endif
  520. {
  521. arp_out(&priv->dev);
  522. }
  523. #ifdef CONFIG_NET_IPv6
  524. else
  525. {
  526. neighbor_out(&priv->dev);
  527. }
  528. #endif
  529. /* And send the packet */
  530. priv->write_d_len = priv->dev.d_len;
  531. tun_fd_transmit(priv);
  532. }
  533. }
  534. else
  535. #endif
  536. #ifdef CONFIG_NET_IPv6
  537. if (BUF->type == HTONS(ETHTYPE_IP6))
  538. {
  539. ninfo("Iv6 frame\n");
  540. NETDEV_RXIPV6(&priv->dev);
  541. /* Give the IPv6 packet to the network layer. */
  542. ipv6_input(&priv->dev);
  543. /* If the above function invocation resulted in data that should be
  544. * sent out on the network, the field d_len will set to a value > 0.
  545. */
  546. if (priv->dev.d_len > 0)
  547. {
  548. /* Update the Ethernet header with the correct MAC address */
  549. #ifdef CONFIG_NET_IPv4
  550. if (IFF_IS_IPv4(priv->dev.d_flags))
  551. {
  552. arp_out(&priv->dev);
  553. }
  554. else
  555. #endif
  556. #ifdef CONFIG_NET_IPv6
  557. {
  558. neighbor_out(&priv->dev);
  559. }
  560. #endif
  561. priv->write_d_len = priv->dev.d_len;
  562. tun_fd_transmit(priv);
  563. }
  564. }
  565. else
  566. #endif
  567. #ifdef CONFIG_NET_ARP
  568. if (BUF->type == htons(ETHTYPE_ARP))
  569. {
  570. arp_arpin(&priv->dev);
  571. NETDEV_RXARP(&priv->dev);
  572. /* If the above function invocation resulted in data that should be
  573. * sent out on the network, the field d_len will set to a value > 0.
  574. */
  575. if (priv->dev.d_len > 0)
  576. {
  577. priv->write_d_len = priv->dev.d_len;
  578. tun_fd_transmit(priv);
  579. }
  580. }
  581. else
  582. #endif
  583. {
  584. NETDEV_RXDROPPED(&priv->dev);
  585. }
  586. }
  587. #endif
  588. /****************************************************************************
  589. * Name: tun_net_receive_tun : for tun (IP tunneling) mode
  590. *
  591. * Description:
  592. * An interrupt was received indicating the availability of a new RX packet
  593. *
  594. * Input Parameters:
  595. * priv - Reference to the driver state structure
  596. *
  597. * Returned Value:
  598. * None
  599. *
  600. * Assumptions:
  601. * Global interrupts are disabled by interrupt handling logic.
  602. *
  603. ****************************************************************************/
  604. static void tun_net_receive_tun(FAR struct tun_device_s *priv)
  605. {
  606. /* Copy the data data from the hardware to priv->dev.d_buf. Set amount of
  607. * data in priv->dev.d_len
  608. */
  609. NETDEV_RXPACKETS(&priv->dev);
  610. #ifdef CONFIG_NET_PKT
  611. /* When packet sockets are enabled, feed the frame into the packet tap */
  612. pkt_input(&priv->dev);
  613. #endif
  614. /* We only accept IP packets of the configured type */
  615. #if defined(CONFIG_NET_IPv4)
  616. if ((IPv4BUF->vhl & IP_VERSION_MASK) == IPv4_VERSION)
  617. {
  618. ninfo("IPv4 frame\n");
  619. NETDEV_RXIPV4(&priv->dev);
  620. /* Give the IPv4 packet to the network layer. */
  621. ipv4_input(&priv->dev);
  622. }
  623. else
  624. #endif
  625. #if defined(CONFIG_NET_IPv6)
  626. if ((IPv6BUF->vtc & IP_VERSION_MASK) == IPv6_VERSION)
  627. {
  628. ninfo("Iv6 frame\n");
  629. NETDEV_RXIPV6(&priv->dev);
  630. /* Give the IPv6 packet to the network layer. */
  631. ipv6_input(&priv->dev);
  632. }
  633. else
  634. #endif
  635. {
  636. NETDEV_RXDROPPED(&priv->dev);
  637. priv->dev.d_len = 0;
  638. }
  639. /* If the above function invocation resulted in data that should be
  640. * sent out on the network, the field d_len will set to a value > 0.
  641. */
  642. if (priv->dev.d_len > 0)
  643. {
  644. priv->write_d_len = priv->dev.d_len;
  645. tun_fd_transmit(priv);
  646. }
  647. }
  648. /****************************************************************************
  649. * Name: tun_txdone
  650. *
  651. * Description:
  652. * An interrupt was received indicating that the last TX packet(s) is done
  653. *
  654. * Input Parameters:
  655. * dev - Reference to the driver state structure
  656. *
  657. * Returned Value:
  658. * None
  659. *
  660. * Assumptions:
  661. * Global interrupts are disabled by the watchdog logic.
  662. *
  663. ****************************************************************************/
  664. static void tun_txdone(FAR struct tun_device_s *priv)
  665. {
  666. /* Check for errors and update statistics */
  667. NETDEV_TXDONE(&priv->dev);
  668. /* Then poll the network for new XMIT data */
  669. priv->dev.d_buf = priv->read_buf;
  670. (void)devif_poll(&priv->dev, tun_txpoll);
  671. }
  672. /****************************************************************************
  673. * Name: tun_poll_work
  674. *
  675. * Description:
  676. * Perform periodic polling from the worker thread
  677. *
  678. * Input Parameters:
  679. * arg - The argument passed when work_queue() as called.
  680. *
  681. * Returned Value:
  682. * OK on success
  683. *
  684. * Assumptions:
  685. * Ethernet interrupts are disabled
  686. *
  687. ****************************************************************************/
  688. static void tun_poll_work(FAR void *arg)
  689. {
  690. FAR struct tun_device_s *priv = (FAR struct tun_device_s *)arg;
  691. /* Perform the poll */
  692. tun_lock(priv);
  693. net_lock();
  694. /* Check if there is room in the send another TX packet. We cannot perform
  695. * the TX poll if he are unable to accept another packet for transmission.
  696. */
  697. if (priv->read_d_len == 0)
  698. {
  699. /* If so, poll the network for new XMIT data. */
  700. priv->dev.d_buf = priv->read_buf;
  701. (void)devif_timer(&priv->dev, tun_txpoll);
  702. }
  703. /* Setup the watchdog poll timer again */
  704. (void)wd_start(priv->txpoll, TUN_WDDELAY, tun_poll_expiry, 1, priv);
  705. net_unlock();
  706. tun_unlock(priv);
  707. }
  708. /****************************************************************************
  709. * Name: tun_poll_expiry
  710. *
  711. * Description:
  712. * Periodic timer handler. Called from the timer interrupt handler.
  713. *
  714. * Input Parameters:
  715. * argc - The number of available arguments
  716. * arg - The first argument
  717. *
  718. * Returned Value:
  719. * None
  720. *
  721. * Assumptions:
  722. * Global interrupts are disabled by the watchdog logic.
  723. *
  724. ****************************************************************************/
  725. static void tun_poll_expiry(int argc, wdparm_t arg, ...)
  726. {
  727. FAR struct tun_device_s *priv = (FAR struct tun_device_s *)arg;
  728. /* Schedule to perform the timer expiration on the worker thread. */
  729. work_queue(TUNWORK, &priv->work, tun_poll_work, priv, 0);
  730. }
  731. /****************************************************************************
  732. * Name: tun_ifup
  733. *
  734. * Description:
  735. * NuttX Callback: Bring up the Ethernet interface when an IP address is
  736. * provided
  737. *
  738. * Input Parameters:
  739. * dev - Reference to the NuttX driver state structure
  740. *
  741. * Returned Value:
  742. * None
  743. *
  744. * Assumptions:
  745. *
  746. ****************************************************************************/
  747. static int tun_ifup(struct net_driver_s *dev)
  748. {
  749. FAR struct tun_device_s *priv = (FAR struct tun_device_s *)dev->d_private;
  750. #ifdef CONFIG_NET_IPv4
  751. ninfo("Bringing up: %d.%d.%d.%d\n",
  752. dev->d_ipaddr & 0xff, (dev->d_ipaddr >> 8) & 0xff,
  753. (dev->d_ipaddr >> 16) & 0xff, dev->d_ipaddr >> 24);
  754. #endif
  755. #ifdef CONFIG_NET_IPv6
  756. ninfo("Bringing up: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
  757. dev->d_ipv6addr[0], dev->d_ipv6addr[1], dev->d_ipv6addr[2],
  758. dev->d_ipv6addr[3], dev->d_ipv6addr[4], dev->d_ipv6addr[5],
  759. dev->d_ipv6addr[6], dev->d_ipv6addr[7]);
  760. #endif
  761. /* Initialize PHYs, the Ethernet interface, and setup up Ethernet interrupts */
  762. /* Instantiate the MAC address from priv->dev.d_mac.ether.ether_addr_octet */
  763. #ifdef CONFIG_NET_ICMPv6
  764. /* Set up IPv6 multicast address filtering */
  765. tun_ipv6multicast(priv);
  766. #endif
  767. /* Set and activate a timer process */
  768. (void)wd_start(priv->txpoll, TUN_WDDELAY, tun_poll_expiry,
  769. 1, (wdparm_t)priv);
  770. priv->bifup = true;
  771. return OK;
  772. }
  773. /****************************************************************************
  774. * Name: tun_ifdown
  775. *
  776. * Description:
  777. * NuttX Callback: Stop the interface.
  778. *
  779. * Input Parameters:
  780. * dev - Reference to the NuttX driver state structure
  781. *
  782. * Returned Value:
  783. * None
  784. *
  785. * Assumptions:
  786. *
  787. ****************************************************************************/
  788. static int tun_ifdown(struct net_driver_s *dev)
  789. {
  790. FAR struct tun_device_s *priv = (FAR struct tun_device_s *)dev->d_private;
  791. irqstate_t flags;
  792. flags = enter_critical_section();
  793. /* Cancel the TX poll timer */
  794. wd_cancel(priv->txpoll);
  795. /* Mark the device "down" */
  796. priv->bifup = false;
  797. leave_critical_section(flags);
  798. return OK;
  799. }
  800. /****************************************************************************
  801. * Name: tun_txavail_work
  802. *
  803. * Description:
  804. * Driver callback invoked when new TX data is available. This is a
  805. * stimulus perform an out-of-cycle poll and, thereby, reduce the TX
  806. * latency.
  807. *
  808. * Input Parameters:
  809. * dev - Reference to the NuttX driver state structure
  810. *
  811. * Returned Value:
  812. * None
  813. *
  814. * Assumptions:
  815. * Called in normal user mode
  816. *
  817. ****************************************************************************/
  818. static void tun_txavail_work(FAR void *arg)
  819. {
  820. FAR struct tun_device_s *priv = (FAR struct tun_device_s *)arg;
  821. tun_lock(priv);
  822. /* Check if there is room to hold another network packet. */
  823. if (priv->read_d_len != 0)
  824. {
  825. tun_unlock(priv);
  826. return;
  827. }
  828. net_lock();
  829. if (priv->bifup)
  830. {
  831. /* Poll the network for new XMIT data */
  832. priv->dev.d_buf = priv->read_buf;
  833. (void)devif_poll(&priv->dev, tun_txpoll);
  834. }
  835. net_unlock();
  836. tun_unlock(priv);
  837. }
  838. /****************************************************************************
  839. * Name: tun_txavail
  840. *
  841. * Description:
  842. * Driver callback invoked when new TX data is available. This is a
  843. * stimulus perform an out-of-cycle poll and, thereby, reduce the TX
  844. * latency.
  845. *
  846. * Input Parameters:
  847. * dev - Reference to the NuttX driver state structure
  848. *
  849. * Returned Value:
  850. * None
  851. *
  852. * Assumptions:
  853. * Called from the network stack with the network locked.
  854. *
  855. ****************************************************************************/
  856. static int tun_txavail(struct net_driver_s *dev)
  857. {
  858. FAR struct tun_device_s *priv = (FAR struct tun_device_s *)dev->d_private;
  859. /* Schedule to perform the TX poll on the worker thread. */
  860. if (work_available(&priv->work))
  861. {
  862. work_queue(TUNWORK, &priv->work, tun_txavail_work, priv, 0);
  863. }
  864. return OK;
  865. }
  866. /****************************************************************************
  867. * Name: tun_addmac
  868. *
  869. * Description:
  870. * NuttX Callback: Add the specified MAC address to the hardware multicast
  871. * address filtering
  872. *
  873. * Input Parameters:
  874. * dev - Reference to the NuttX driver state structure
  875. * mac - The MAC address to be added
  876. *
  877. * Returned Value:
  878. * None
  879. *
  880. * Assumptions:
  881. *
  882. ****************************************************************************/
  883. #ifdef CONFIG_NET_MCASTGROUP
  884. static int tun_addmac(struct net_driver_s *dev, FAR const uint8_t *mac)
  885. {
  886. /* Add the MAC address to the hardware multicast routing table */
  887. return OK;
  888. }
  889. #endif
  890. /****************************************************************************
  891. * Name: tun_rmmac
  892. *
  893. * Description:
  894. * NuttX Callback: Remove the specified MAC address from the hardware multicast
  895. * address filtering
  896. *
  897. * Input Parameters:
  898. * dev - Reference to the NuttX driver state structure
  899. * mac - The MAC address to be removed
  900. *
  901. * Returned Value:
  902. * None
  903. *
  904. * Assumptions:
  905. *
  906. ****************************************************************************/
  907. #ifdef CONFIG_NET_MCASTGROUP
  908. static int tun_rmmac(struct net_driver_s *dev, FAR const uint8_t *mac)
  909. {
  910. /* Add the MAC address to the hardware multicast routing table */
  911. return OK;
  912. }
  913. #endif
  914. /****************************************************************************
  915. * Name: tun_ipv6multicast
  916. *
  917. * Description:
  918. * Configure the IPv6 multicast MAC address.
  919. *
  920. * Input Parameters:
  921. * priv - A reference to the private driver state structure
  922. *
  923. * Returned Value:
  924. * OK on success; Negated errno on failure.
  925. *
  926. * Assumptions:
  927. *
  928. ****************************************************************************/
  929. #ifdef CONFIG_NET_ICMPv6
  930. static void tun_ipv6multicast(FAR struct tun_device_s *priv)
  931. {
  932. }
  933. #endif /* CONFIG_NET_ICMPv6 */
  934. /****************************************************************************
  935. * Name: tun_dev_init
  936. *
  937. * Description:
  938. * Initialize the TUN device
  939. *
  940. * Input Parameters:
  941. *
  942. * Returned Value:
  943. * OK on success; Negated errno on failure.
  944. *
  945. * Assumptions:
  946. *
  947. ****************************************************************************/
  948. static int tun_dev_init(FAR struct tun_device_s *priv, FAR struct file *filep,
  949. FAR const char *devfmt, bool tun)
  950. {
  951. int ret;
  952. /* Initialize the driver structure */
  953. memset(priv, 0, sizeof(struct tun_device_s));
  954. priv->dev.d_ifup = tun_ifup; /* I/F up (new IP address) callback */
  955. priv->dev.d_ifdown = tun_ifdown; /* I/F down callback */
  956. priv->dev.d_txavail = tun_txavail; /* New TX data callback */
  957. #ifdef CONFIG_NET_MCASTGROUP
  958. priv->dev.d_addmac = tun_addmac; /* Add multicast MAC address */
  959. priv->dev.d_rmmac = tun_rmmac; /* Remove multicast MAC address */
  960. #endif
  961. priv->dev.d_private = (FAR void *)priv; /* Used to recover private state from dev */
  962. /* Initialize the mutual exlcusion and wait semaphore */
  963. nxsem_init(&priv->waitsem, 0, 1);
  964. nxsem_init(&priv->read_wait_sem, 0, 0);
  965. /* The wait semaphore is used for signaling and, hence, should not have
  966. * priority inheritance enabled.
  967. */
  968. nxsem_setprotocol(&priv->read_wait_sem, SEM_PRIO_NONE);
  969. /* Create a watchdog for timing polling for and timing of transmissions */
  970. priv->txpoll = wd_create(); /* Create periodic poll timer */
  971. /* Assign d_ifname if specified. */
  972. if (devfmt)
  973. {
  974. strncpy(priv->dev.d_ifname, devfmt, IFNAMSIZ);
  975. }
  976. /* Register the device with the OS so that socket IOCTLs can be performed */
  977. ret = netdev_register(&priv->dev, tun ? NET_LL_TUN : NET_LL_ETHERNET);
  978. if (ret != OK)
  979. {
  980. nxsem_destroy(&priv->waitsem);
  981. nxsem_destroy(&priv->read_wait_sem);
  982. return ret;
  983. }
  984. priv->filep = filep; /* Set link to file */
  985. filep->f_priv = priv; /* Set link to TUN device */
  986. return ret;
  987. }
  988. /****************************************************************************
  989. * Name: tun_dev_uninit
  990. ****************************************************************************/
  991. static int tun_dev_uninit(FAR struct tun_device_s *priv)
  992. {
  993. /* Put the interface in the down state */
  994. tun_ifdown(&priv->dev);
  995. /* Remove the device from the OS */
  996. (void)netdev_unregister(&priv->dev);
  997. nxsem_destroy(&priv->waitsem);
  998. nxsem_destroy(&priv->read_wait_sem);
  999. return OK;
  1000. }
  1001. /****************************************************************************
  1002. * Name: tun_open
  1003. ****************************************************************************/
  1004. static int tun_open(FAR struct file *filep)
  1005. {
  1006. filep->f_priv = 0;
  1007. return OK;
  1008. }
  1009. /****************************************************************************
  1010. * Name: tun_close
  1011. ****************************************************************************/
  1012. static int tun_close(FAR struct file *filep)
  1013. {
  1014. FAR struct inode *inode = filep->f_inode;
  1015. FAR struct tun_driver_s *tun = inode->i_private;
  1016. FAR struct tun_device_s *priv = filep->f_priv;
  1017. int intf;
  1018. if (priv == NULL)
  1019. {
  1020. return OK;
  1021. }
  1022. intf = priv - g_tun_devices;
  1023. tundev_lock(tun);
  1024. tun->free_tuns |= (1 << intf);
  1025. (void)tun_dev_uninit(priv);
  1026. tundev_unlock(tun);
  1027. return OK;
  1028. }
  1029. /****************************************************************************
  1030. * Name: tun_write
  1031. ****************************************************************************/
  1032. static ssize_t tun_write(FAR struct file *filep, FAR const char *buffer,
  1033. size_t buflen)
  1034. {
  1035. FAR struct tun_device_s *priv = filep->f_priv;
  1036. ssize_t ret;
  1037. if (priv == NULL)
  1038. {
  1039. return -EINVAL;
  1040. }
  1041. tun_lock(priv);
  1042. if (priv->write_d_len > 0)
  1043. {
  1044. tun_unlock(priv);
  1045. return -EBUSY;
  1046. }
  1047. net_lock();
  1048. if (buflen > CONFIG_NET_TUN_PKTSIZE)
  1049. {
  1050. ret = -EINVAL;
  1051. }
  1052. else
  1053. {
  1054. memcpy(priv->write_buf, buffer, buflen);
  1055. priv->dev.d_buf = priv->write_buf;
  1056. priv->dev.d_len = buflen;
  1057. tun_net_receive(priv);
  1058. ret = (ssize_t)buflen;
  1059. }
  1060. net_unlock();
  1061. tun_unlock(priv);
  1062. return ret;
  1063. }
  1064. /****************************************************************************
  1065. * Name: tun_read
  1066. ****************************************************************************/
  1067. static ssize_t tun_read(FAR struct file *filep, FAR char *buffer,
  1068. size_t buflen)
  1069. {
  1070. FAR struct tun_device_s *priv = filep->f_priv;
  1071. ssize_t ret;
  1072. size_t write_d_len;
  1073. size_t read_d_len;
  1074. if (priv == NULL)
  1075. {
  1076. return -EINVAL;
  1077. }
  1078. tun_lock(priv);
  1079. /* Check if there are data to read in write buffer */
  1080. write_d_len = priv->write_d_len;
  1081. if (write_d_len > 0)
  1082. {
  1083. if (buflen < write_d_len)
  1084. {
  1085. ret = -EINVAL;
  1086. goto out;
  1087. }
  1088. memcpy(buffer, priv->write_buf, write_d_len);
  1089. ret = (ssize_t)write_d_len;
  1090. priv->write_d_len = 0;
  1091. NETDEV_TXDONE(&priv->dev);
  1092. tun_pollnotify(priv, POLLOUT);
  1093. goto out;
  1094. }
  1095. if (priv->read_d_len == 0)
  1096. {
  1097. if ((filep->f_oflags & O_NONBLOCK) != 0)
  1098. {
  1099. ret = -EAGAIN;
  1100. goto out;
  1101. }
  1102. priv->read_wait = true;
  1103. tun_unlock(priv);
  1104. (void)nxsem_wait(&priv->read_wait_sem);
  1105. tun_lock(priv);
  1106. }
  1107. net_lock();
  1108. read_d_len = priv->read_d_len;
  1109. if (buflen < read_d_len)
  1110. {
  1111. ret = -EINVAL;
  1112. }
  1113. else
  1114. {
  1115. memcpy(buffer, priv->read_buf, read_d_len);
  1116. ret = (ssize_t)read_d_len;
  1117. }
  1118. priv->read_d_len = 0;
  1119. tun_txdone(priv);
  1120. net_unlock();
  1121. out:
  1122. tun_unlock(priv);
  1123. return ret;
  1124. }
  1125. /****************************************************************************
  1126. * Name: tun_poll
  1127. ****************************************************************************/
  1128. #ifndef CONFIG_DISABLE_POLL
  1129. int tun_poll(FAR struct file *filep, FAR struct pollfd *fds, bool setup)
  1130. {
  1131. FAR struct tun_device_s *priv = filep->f_priv;
  1132. pollevent_t eventset;
  1133. int ret = OK;
  1134. /* Some sanity checking */
  1135. if (priv == NULL || fds == NULL)
  1136. {
  1137. return -EINVAL;
  1138. }
  1139. tun_lock(priv);
  1140. if (setup)
  1141. {
  1142. if (priv->poll_fds)
  1143. {
  1144. ret = -EBUSY;
  1145. goto errout;
  1146. }
  1147. priv->poll_fds = fds;
  1148. eventset = 0;
  1149. /* If write buffer is empty notify App. */
  1150. if (priv->write_d_len == 0)
  1151. {
  1152. eventset |= (fds->events & POLLOUT);
  1153. }
  1154. /* The write buffer sometimes could be used for TX.
  1155. * So check it too.
  1156. */
  1157. if (priv->read_d_len != 0 || priv->write_d_len != 0)
  1158. {
  1159. eventset |= (fds->events & POLLIN);
  1160. }
  1161. if (eventset)
  1162. {
  1163. tun_pollnotify(priv, eventset);
  1164. }
  1165. }
  1166. else
  1167. {
  1168. priv->poll_fds = 0;
  1169. }
  1170. errout:
  1171. tun_unlock(priv);
  1172. return ret;
  1173. }
  1174. #endif
  1175. /****************************************************************************
  1176. * Name: tun_ioctl
  1177. ****************************************************************************/
  1178. static int tun_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  1179. {
  1180. FAR struct inode *inode = filep->f_inode;
  1181. FAR struct tun_driver_s *tun = inode->i_private;
  1182. FAR struct tun_device_s *priv = filep->f_priv;
  1183. int ret = OK;
  1184. if (cmd == TUNSETIFF && priv == NULL)
  1185. {
  1186. uint8_t free_tuns;
  1187. int intf;
  1188. FAR struct ifreq *ifr = (FAR struct ifreq *)arg;
  1189. if (ifr == NULL ||
  1190. ((ifr->ifr_flags & IFF_MASK) != IFF_TUN &&
  1191. (ifr->ifr_flags & IFF_MASK) != IFF_TAP))
  1192. {
  1193. return -EINVAL;
  1194. }
  1195. tundev_lock(tun);
  1196. free_tuns = tun->free_tuns;
  1197. if (free_tuns == 0)
  1198. {
  1199. tundev_unlock(tun);
  1200. return -ENOMEM;
  1201. }
  1202. for (intf = 0;
  1203. intf < CONFIG_TUN_NINTERFACES && !(free_tuns & 1);
  1204. intf++, free_tuns >>= 1);
  1205. ret = tun_dev_init(&g_tun_devices[intf], filep,
  1206. *ifr->ifr_name ? ifr->ifr_name : 0,
  1207. (ifr->ifr_flags & IFF_MASK) == IFF_TUN);
  1208. if (ret != OK)
  1209. {
  1210. tundev_unlock(tun);
  1211. return ret;
  1212. }
  1213. tun->free_tuns &= ~(1 << intf);
  1214. priv = filep->f_priv;
  1215. strncpy(ifr->ifr_name, priv->dev.d_ifname, IFNAMSIZ);
  1216. tundev_unlock(tun);
  1217. return OK;
  1218. }
  1219. return -EBADFD;
  1220. }
  1221. /****************************************************************************
  1222. * Public Functions
  1223. ****************************************************************************/
  1224. /****************************************************************************
  1225. * Name: tun_initialize
  1226. *
  1227. * Description:
  1228. * Instantiate a TUN network interface.
  1229. *
  1230. * Input Parameters:
  1231. *
  1232. * Returned Value:
  1233. * OK on success; Negated errno on failure.
  1234. *
  1235. * Assumptions:
  1236. *
  1237. ****************************************************************************/
  1238. int tun_initialize(void)
  1239. {
  1240. nxsem_init(&g_tun.waitsem, 0, 1);
  1241. g_tun.free_tuns = (1 << CONFIG_TUN_NINTERFACES) - 1;
  1242. (void)register_driver("/dev/tun", &g_tun_file_ops, 0644, &g_tun);
  1243. return OK;
  1244. }
  1245. #endif /* CONFIG_NET && CONFIG_NET_TUN */