tun.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823
  1. /*
  2. * TUN - Universal TUN/TAP device driver.
  3. * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
  16. */
  17. /*
  18. * Changes:
  19. *
  20. * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
  21. * Add TUNSETLINK ioctl to set the link encapsulation
  22. *
  23. * Mark Smith <markzzzsmith@yahoo.com.au>
  24. * Use eth_random_addr() for tap MAC address.
  25. *
  26. * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
  27. * Fixes in packet dropping, queue length setting and queue wakeup.
  28. * Increased default tx queue length.
  29. * Added ethtool API.
  30. * Minor cleanups
  31. *
  32. * Daniel Podlejski <underley@underley.eu.org>
  33. * Modifications for 2.3.99-pre5 kernel.
  34. */
  35. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  36. #define DRV_NAME "tun"
  37. #define DRV_VERSION "1.6"
  38. #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
  39. #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
  40. #include <linux/module.h>
  41. #include <linux/errno.h>
  42. #include <linux/kernel.h>
  43. #include <linux/major.h>
  44. #include <linux/slab.h>
  45. #include <linux/poll.h>
  46. #include <linux/fcntl.h>
  47. #include <linux/init.h>
  48. #include <linux/skbuff.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/etherdevice.h>
  51. #include <linux/miscdevice.h>
  52. #include <linux/ethtool.h>
  53. #include <linux/rtnetlink.h>
  54. #include <linux/compat.h>
  55. #include <linux/if.h>
  56. #include <linux/if_arp.h>
  57. #include <linux/if_ether.h>
  58. #include <linux/if_tun.h>
  59. #include <linux/crc32.h>
  60. #include <linux/nsproxy.h>
  61. #include <linux/virtio_net.h>
  62. #include <linux/rcupdate.h>
  63. #include <net/net_namespace.h>
  64. #include <net/netns/generic.h>
  65. #include <net/rtnetlink.h>
  66. #include <net/sock.h>
  67. #include <asm/uaccess.h>
  68. /* Uncomment to enable debugging */
  69. /* #define TUN_DEBUG 1 */
  70. #ifdef TUN_DEBUG
  71. static int debug;
  72. #define tun_debug(level, tun, fmt, args...) \
  73. do { \
  74. if (tun->debug) \
  75. netdev_printk(level, tun->dev, fmt, ##args); \
  76. } while (0)
  77. #define DBG1(level, fmt, args...) \
  78. do { \
  79. if (debug == 2) \
  80. printk(level fmt, ##args); \
  81. } while (0)
  82. #else
  83. #define tun_debug(level, tun, fmt, args...) \
  84. do { \
  85. if (0) \
  86. netdev_printk(level, tun->dev, fmt, ##args); \
  87. } while (0)
  88. #define DBG1(level, fmt, args...) \
  89. do { \
  90. if (0) \
  91. printk(level fmt, ##args); \
  92. } while (0)
  93. #endif
  94. #define GOODCOPY_LEN 128
  95. #define FLT_EXACT_COUNT 8
  96. struct tap_filter {
  97. unsigned int count; /* Number of addrs. Zero means disabled */
  98. u32 mask[2]; /* Mask of the hashed addrs */
  99. unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
  100. };
  101. struct tun_file {
  102. atomic_t count;
  103. struct tun_struct *tun;
  104. struct net *net;
  105. };
  106. struct tun_sock;
  107. struct tun_struct {
  108. struct tun_file *tfile;
  109. unsigned int flags;
  110. uid_t owner;
  111. gid_t group;
  112. struct net_device *dev;
  113. netdev_features_t set_features;
  114. #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
  115. NETIF_F_TSO6|NETIF_F_UFO)
  116. struct fasync_struct *fasync;
  117. struct tap_filter txflt;
  118. struct socket socket;
  119. struct socket_wq wq;
  120. int vnet_hdr_sz;
  121. #ifdef TUN_DEBUG
  122. int debug;
  123. #endif
  124. };
  125. struct tun_sock {
  126. struct sock sk;
  127. struct tun_struct *tun;
  128. };
  129. static inline struct tun_sock *tun_sk(struct sock *sk)
  130. {
  131. return container_of(sk, struct tun_sock, sk);
  132. }
  133. static int tun_attach(struct tun_struct *tun, struct file *file)
  134. {
  135. struct tun_file *tfile = file->private_data;
  136. int err;
  137. ASSERT_RTNL();
  138. netif_tx_lock_bh(tun->dev);
  139. err = -EINVAL;
  140. if (tfile->tun)
  141. goto out;
  142. err = -EBUSY;
  143. if (tun->tfile)
  144. goto out;
  145. err = 0;
  146. tfile->tun = tun;
  147. tun->tfile = tfile;
  148. tun->socket.file = file;
  149. netif_carrier_on(tun->dev);
  150. dev_hold(tun->dev);
  151. sock_hold(tun->socket.sk);
  152. atomic_inc(&tfile->count);
  153. out:
  154. netif_tx_unlock_bh(tun->dev);
  155. return err;
  156. }
  157. static void __tun_detach(struct tun_struct *tun)
  158. {
  159. /* Detach from net device */
  160. netif_tx_lock_bh(tun->dev);
  161. netif_carrier_off(tun->dev);
  162. tun->tfile = NULL;
  163. netif_tx_unlock_bh(tun->dev);
  164. /* Drop read queue */
  165. skb_queue_purge(&tun->socket.sk->sk_receive_queue);
  166. /* Drop the extra count on the net device */
  167. dev_put(tun->dev);
  168. }
  169. static void tun_detach(struct tun_struct *tun)
  170. {
  171. rtnl_lock();
  172. __tun_detach(tun);
  173. rtnl_unlock();
  174. }
  175. static struct tun_struct *__tun_get(struct tun_file *tfile)
  176. {
  177. struct tun_struct *tun = NULL;
  178. if (atomic_inc_not_zero(&tfile->count))
  179. tun = tfile->tun;
  180. return tun;
  181. }
  182. static struct tun_struct *tun_get(struct file *file)
  183. {
  184. return __tun_get(file->private_data);
  185. }
  186. static void tun_put(struct tun_struct *tun)
  187. {
  188. struct tun_file *tfile = tun->tfile;
  189. if (atomic_dec_and_test(&tfile->count))
  190. tun_detach(tfile->tun);
  191. }
  192. /* TAP filtering */
  193. static void addr_hash_set(u32 *mask, const u8 *addr)
  194. {
  195. int n = ether_crc(ETH_ALEN, addr) >> 26;
  196. mask[n >> 5] |= (1 << (n & 31));
  197. }
  198. static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
  199. {
  200. int n = ether_crc(ETH_ALEN, addr) >> 26;
  201. return mask[n >> 5] & (1 << (n & 31));
  202. }
  203. static int update_filter(struct tap_filter *filter, void __user *arg)
  204. {
  205. struct { u8 u[ETH_ALEN]; } *addr;
  206. struct tun_filter uf;
  207. int err, alen, n, nexact;
  208. if (copy_from_user(&uf, arg, sizeof(uf)))
  209. return -EFAULT;
  210. if (!uf.count) {
  211. /* Disabled */
  212. filter->count = 0;
  213. return 0;
  214. }
  215. alen = ETH_ALEN * uf.count;
  216. addr = kmalloc(alen, GFP_KERNEL);
  217. if (!addr)
  218. return -ENOMEM;
  219. if (copy_from_user(addr, arg + sizeof(uf), alen)) {
  220. err = -EFAULT;
  221. goto done;
  222. }
  223. /* The filter is updated without holding any locks. Which is
  224. * perfectly safe. We disable it first and in the worst
  225. * case we'll accept a few undesired packets. */
  226. filter->count = 0;
  227. wmb();
  228. /* Use first set of addresses as an exact filter */
  229. for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
  230. memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
  231. nexact = n;
  232. /* Remaining multicast addresses are hashed,
  233. * unicast will leave the filter disabled. */
  234. memset(filter->mask, 0, sizeof(filter->mask));
  235. for (; n < uf.count; n++) {
  236. if (!is_multicast_ether_addr(addr[n].u)) {
  237. err = 0; /* no filter */
  238. goto done;
  239. }
  240. addr_hash_set(filter->mask, addr[n].u);
  241. }
  242. /* For ALLMULTI just set the mask to all ones.
  243. * This overrides the mask populated above. */
  244. if ((uf.flags & TUN_FLT_ALLMULTI))
  245. memset(filter->mask, ~0, sizeof(filter->mask));
  246. /* Now enable the filter */
  247. wmb();
  248. filter->count = nexact;
  249. /* Return the number of exact filters */
  250. err = nexact;
  251. done:
  252. kfree(addr);
  253. return err;
  254. }
  255. /* Returns: 0 - drop, !=0 - accept */
  256. static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
  257. {
  258. /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
  259. * at this point. */
  260. struct ethhdr *eh = (struct ethhdr *) skb->data;
  261. int i;
  262. /* Exact match */
  263. for (i = 0; i < filter->count; i++)
  264. if (ether_addr_equal(eh->h_dest, filter->addr[i]))
  265. return 1;
  266. /* Inexact match (multicast only) */
  267. if (is_multicast_ether_addr(eh->h_dest))
  268. return addr_hash_test(filter->mask, eh->h_dest);
  269. return 0;
  270. }
  271. /*
  272. * Checks whether the packet is accepted or not.
  273. * Returns: 0 - drop, !=0 - accept
  274. */
  275. static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
  276. {
  277. if (!filter->count)
  278. return 1;
  279. return run_filter(filter, skb);
  280. }
  281. /* Network device part of the driver */
  282. static const struct ethtool_ops tun_ethtool_ops;
  283. /* Net device detach from fd. */
  284. static void tun_net_uninit(struct net_device *dev)
  285. {
  286. struct tun_struct *tun = netdev_priv(dev);
  287. struct tun_file *tfile = tun->tfile;
  288. /* Inform the methods they need to stop using the dev.
  289. */
  290. if (tfile) {
  291. wake_up_all(&tun->wq.wait);
  292. if (atomic_dec_and_test(&tfile->count))
  293. __tun_detach(tun);
  294. }
  295. }
  296. static void tun_free_netdev(struct net_device *dev)
  297. {
  298. struct tun_struct *tun = netdev_priv(dev);
  299. BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED, &tun->socket.flags));
  300. sk_release_kernel(tun->socket.sk);
  301. }
  302. /* Net device open. */
  303. static int tun_net_open(struct net_device *dev)
  304. {
  305. netif_start_queue(dev);
  306. return 0;
  307. }
  308. /* Net device close. */
  309. static int tun_net_close(struct net_device *dev)
  310. {
  311. netif_stop_queue(dev);
  312. return 0;
  313. }
  314. /* Net device start xmit */
  315. static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
  316. {
  317. struct tun_struct *tun = netdev_priv(dev);
  318. tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
  319. /* Drop packet if interface is not attached */
  320. if (!tun->tfile)
  321. goto drop;
  322. /* Drop if the filter does not like it.
  323. * This is a noop if the filter is disabled.
  324. * Filter can be enabled only for the TAP devices. */
  325. if (!check_filter(&tun->txflt, skb))
  326. goto drop;
  327. if (tun->socket.sk->sk_filter &&
  328. sk_filter(tun->socket.sk, skb))
  329. goto drop;
  330. if (skb_queue_len(&tun->socket.sk->sk_receive_queue) >= dev->tx_queue_len) {
  331. if (!(tun->flags & TUN_ONE_QUEUE)) {
  332. /* Normal queueing mode. */
  333. /* Packet scheduler handles dropping of further packets. */
  334. netif_stop_queue(dev);
  335. /* We won't see all dropped packets individually, so overrun
  336. * error is more appropriate. */
  337. dev->stats.tx_fifo_errors++;
  338. } else {
  339. /* Single queue mode.
  340. * Driver handles dropping of all packets itself. */
  341. goto drop;
  342. }
  343. }
  344. /* Orphan the skb - required as we might hang on to it
  345. * for indefinite time. */
  346. if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
  347. goto drop;
  348. skb_orphan(skb);
  349. /* Enqueue packet */
  350. skb_queue_tail(&tun->socket.sk->sk_receive_queue, skb);
  351. /* Notify and wake up reader process */
  352. if (tun->flags & TUN_FASYNC)
  353. kill_fasync(&tun->fasync, SIGIO, POLL_IN);
  354. wake_up_interruptible_poll(&tun->wq.wait, POLLIN |
  355. POLLRDNORM | POLLRDBAND);
  356. return NETDEV_TX_OK;
  357. drop:
  358. dev->stats.tx_dropped++;
  359. kfree_skb(skb);
  360. return NETDEV_TX_OK;
  361. }
  362. static void tun_net_mclist(struct net_device *dev)
  363. {
  364. /*
  365. * This callback is supposed to deal with mc filter in
  366. * _rx_ path and has nothing to do with the _tx_ path.
  367. * In rx path we always accept everything userspace gives us.
  368. */
  369. }
  370. #define MIN_MTU 68
  371. #define MAX_MTU 65535
  372. static int
  373. tun_net_change_mtu(struct net_device *dev, int new_mtu)
  374. {
  375. if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
  376. return -EINVAL;
  377. dev->mtu = new_mtu;
  378. return 0;
  379. }
  380. static netdev_features_t tun_net_fix_features(struct net_device *dev,
  381. netdev_features_t features)
  382. {
  383. struct tun_struct *tun = netdev_priv(dev);
  384. return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
  385. }
  386. #ifdef CONFIG_NET_POLL_CONTROLLER
  387. static void tun_poll_controller(struct net_device *dev)
  388. {
  389. /*
  390. * Tun only receives frames when:
  391. * 1) the char device endpoint gets data from user space
  392. * 2) the tun socket gets a sendmsg call from user space
  393. * Since both of those are syncronous operations, we are guaranteed
  394. * never to have pending data when we poll for it
  395. * so theres nothing to do here but return.
  396. * We need this though so netpoll recognizes us as an interface that
  397. * supports polling, which enables bridge devices in virt setups to
  398. * still use netconsole
  399. */
  400. return;
  401. }
  402. #endif
  403. static const struct net_device_ops tun_netdev_ops = {
  404. .ndo_uninit = tun_net_uninit,
  405. .ndo_open = tun_net_open,
  406. .ndo_stop = tun_net_close,
  407. .ndo_start_xmit = tun_net_xmit,
  408. .ndo_change_mtu = tun_net_change_mtu,
  409. .ndo_fix_features = tun_net_fix_features,
  410. #ifdef CONFIG_NET_POLL_CONTROLLER
  411. .ndo_poll_controller = tun_poll_controller,
  412. #endif
  413. };
  414. static const struct net_device_ops tap_netdev_ops = {
  415. .ndo_uninit = tun_net_uninit,
  416. .ndo_open = tun_net_open,
  417. .ndo_stop = tun_net_close,
  418. .ndo_start_xmit = tun_net_xmit,
  419. .ndo_change_mtu = tun_net_change_mtu,
  420. .ndo_fix_features = tun_net_fix_features,
  421. .ndo_set_rx_mode = tun_net_mclist,
  422. .ndo_set_mac_address = eth_mac_addr,
  423. .ndo_validate_addr = eth_validate_addr,
  424. #ifdef CONFIG_NET_POLL_CONTROLLER
  425. .ndo_poll_controller = tun_poll_controller,
  426. #endif
  427. };
  428. /* Initialize net device. */
  429. static void tun_net_init(struct net_device *dev)
  430. {
  431. struct tun_struct *tun = netdev_priv(dev);
  432. switch (tun->flags & TUN_TYPE_MASK) {
  433. case TUN_TUN_DEV:
  434. dev->netdev_ops = &tun_netdev_ops;
  435. /* Point-to-Point TUN Device */
  436. dev->hard_header_len = 0;
  437. dev->addr_len = 0;
  438. dev->mtu = 1500;
  439. /* Zero header length */
  440. dev->type = ARPHRD_NONE;
  441. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  442. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  443. break;
  444. case TUN_TAP_DEV:
  445. dev->netdev_ops = &tap_netdev_ops;
  446. /* Ethernet TAP Device */
  447. ether_setup(dev);
  448. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  449. eth_hw_addr_random(dev);
  450. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  451. break;
  452. }
  453. }
  454. /* Character device part */
  455. /* Poll */
  456. static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
  457. {
  458. struct tun_file *tfile = file->private_data;
  459. struct tun_struct *tun = __tun_get(tfile);
  460. struct sock *sk;
  461. unsigned int mask = 0;
  462. if (!tun)
  463. return POLLERR;
  464. sk = tun->socket.sk;
  465. tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
  466. poll_wait(file, &tun->wq.wait, wait);
  467. if (!skb_queue_empty(&sk->sk_receive_queue))
  468. mask |= POLLIN | POLLRDNORM;
  469. if (sock_writeable(sk) ||
  470. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
  471. sock_writeable(sk)))
  472. mask |= POLLOUT | POLLWRNORM;
  473. if (tun->dev->reg_state != NETREG_REGISTERED)
  474. mask = POLLERR;
  475. tun_put(tun);
  476. return mask;
  477. }
  478. /* prepad is the amount to reserve at front. len is length after that.
  479. * linear is a hint as to how much to copy (usually headers). */
  480. static struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
  481. size_t prepad, size_t len,
  482. size_t linear, int noblock)
  483. {
  484. struct sock *sk = tun->socket.sk;
  485. struct sk_buff *skb;
  486. int err;
  487. sock_update_classid(sk);
  488. /* Under a page? Don't bother with paged skb. */
  489. if (prepad + len < PAGE_SIZE || !linear)
  490. linear = len;
  491. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  492. &err);
  493. if (!skb)
  494. return ERR_PTR(err);
  495. skb_reserve(skb, prepad);
  496. skb_put(skb, linear);
  497. skb->data_len = len - linear;
  498. skb->len += len - linear;
  499. return skb;
  500. }
  501. /* set skb frags from iovec, this can move to core network code for reuse */
  502. static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
  503. int offset, size_t count)
  504. {
  505. int len = iov_length(from, count) - offset;
  506. int copy = skb_headlen(skb);
  507. int size, offset1 = 0;
  508. int i = 0;
  509. /* Skip over from offset */
  510. while (count && (offset >= from->iov_len)) {
  511. offset -= from->iov_len;
  512. ++from;
  513. --count;
  514. }
  515. /* copy up to skb headlen */
  516. while (count && (copy > 0)) {
  517. size = min_t(unsigned int, copy, from->iov_len - offset);
  518. if (copy_from_user(skb->data + offset1, from->iov_base + offset,
  519. size))
  520. return -EFAULT;
  521. if (copy > size) {
  522. ++from;
  523. --count;
  524. offset = 0;
  525. } else
  526. offset += size;
  527. copy -= size;
  528. offset1 += size;
  529. }
  530. if (len == offset1)
  531. return 0;
  532. while (count--) {
  533. struct page *page[MAX_SKB_FRAGS];
  534. int num_pages;
  535. unsigned long base;
  536. unsigned long truesize;
  537. len = from->iov_len - offset;
  538. if (!len) {
  539. offset = 0;
  540. ++from;
  541. continue;
  542. }
  543. base = (unsigned long)from->iov_base + offset;
  544. size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
  545. if (i + size > MAX_SKB_FRAGS)
  546. return -EMSGSIZE;
  547. num_pages = get_user_pages_fast(base, size, 0, &page[i]);
  548. if (num_pages != size) {
  549. for (i = 0; i < num_pages; i++)
  550. put_page(page[i]);
  551. return -EFAULT;
  552. }
  553. truesize = size * PAGE_SIZE;
  554. skb->data_len += len;
  555. skb->len += len;
  556. skb->truesize += truesize;
  557. atomic_add(truesize, &skb->sk->sk_wmem_alloc);
  558. while (len) {
  559. int off = base & ~PAGE_MASK;
  560. int size = min_t(int, len, PAGE_SIZE - off);
  561. __skb_fill_page_desc(skb, i, page[i], off, size);
  562. skb_shinfo(skb)->nr_frags++;
  563. /* increase sk_wmem_alloc */
  564. base += size;
  565. len -= size;
  566. i++;
  567. }
  568. offset = 0;
  569. ++from;
  570. }
  571. return 0;
  572. }
  573. /* Get packet from user space buffer */
  574. static ssize_t tun_get_user(struct tun_struct *tun, void *msg_control,
  575. const struct iovec *iv, size_t total_len,
  576. size_t count, int noblock)
  577. {
  578. struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
  579. struct sk_buff *skb;
  580. size_t len = total_len, align = NET_SKB_PAD;
  581. struct virtio_net_hdr gso = { 0 };
  582. int offset = 0;
  583. int copylen;
  584. bool zerocopy = false;
  585. int err;
  586. if (!(tun->flags & TUN_NO_PI)) {
  587. if ((len -= sizeof(pi)) > total_len)
  588. return -EINVAL;
  589. if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
  590. return -EFAULT;
  591. offset += sizeof(pi);
  592. }
  593. if (tun->flags & TUN_VNET_HDR) {
  594. if ((len -= tun->vnet_hdr_sz) > total_len)
  595. return -EINVAL;
  596. if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
  597. return -EFAULT;
  598. if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  599. gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
  600. gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
  601. if (gso.hdr_len > len)
  602. return -EINVAL;
  603. offset += tun->vnet_hdr_sz;
  604. }
  605. if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
  606. align += NET_IP_ALIGN;
  607. if (unlikely(len < ETH_HLEN ||
  608. (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
  609. return -EINVAL;
  610. }
  611. if (msg_control)
  612. zerocopy = true;
  613. if (zerocopy) {
  614. /* Userspace may produce vectors with count greater than
  615. * MAX_SKB_FRAGS, so we need to linearize parts of the skb
  616. * to let the rest of data to be fit in the frags.
  617. */
  618. if (count > MAX_SKB_FRAGS) {
  619. copylen = iov_length(iv, count - MAX_SKB_FRAGS);
  620. if (copylen < offset)
  621. copylen = 0;
  622. else
  623. copylen -= offset;
  624. } else
  625. copylen = 0;
  626. /* There are 256 bytes to be copied in skb, so there is enough
  627. * room for skb expand head in case it is used.
  628. * The rest of the buffer is mapped from userspace.
  629. */
  630. if (copylen < gso.hdr_len)
  631. copylen = gso.hdr_len;
  632. if (!copylen)
  633. copylen = GOODCOPY_LEN;
  634. } else
  635. copylen = len;
  636. skb = tun_alloc_skb(tun, align, copylen, gso.hdr_len, noblock);
  637. if (IS_ERR(skb)) {
  638. if (PTR_ERR(skb) != -EAGAIN)
  639. tun->dev->stats.rx_dropped++;
  640. return PTR_ERR(skb);
  641. }
  642. if (zerocopy)
  643. err = zerocopy_sg_from_iovec(skb, iv, offset, count);
  644. else
  645. err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
  646. if (err) {
  647. tun->dev->stats.rx_dropped++;
  648. kfree_skb(skb);
  649. return -EFAULT;
  650. }
  651. if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  652. if (!skb_partial_csum_set(skb, gso.csum_start,
  653. gso.csum_offset)) {
  654. tun->dev->stats.rx_frame_errors++;
  655. kfree_skb(skb);
  656. return -EINVAL;
  657. }
  658. }
  659. switch (tun->flags & TUN_TYPE_MASK) {
  660. case TUN_TUN_DEV:
  661. if (tun->flags & TUN_NO_PI) {
  662. switch (skb->data[0] & 0xf0) {
  663. case 0x40:
  664. pi.proto = htons(ETH_P_IP);
  665. break;
  666. case 0x60:
  667. pi.proto = htons(ETH_P_IPV6);
  668. break;
  669. default:
  670. tun->dev->stats.rx_dropped++;
  671. kfree_skb(skb);
  672. return -EINVAL;
  673. }
  674. }
  675. skb_reset_mac_header(skb);
  676. skb->protocol = pi.proto;
  677. skb->dev = tun->dev;
  678. break;
  679. case TUN_TAP_DEV:
  680. skb->protocol = eth_type_trans(skb, tun->dev);
  681. break;
  682. }
  683. if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  684. pr_debug("GSO!\n");
  685. switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  686. case VIRTIO_NET_HDR_GSO_TCPV4:
  687. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  688. break;
  689. case VIRTIO_NET_HDR_GSO_TCPV6:
  690. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  691. break;
  692. case VIRTIO_NET_HDR_GSO_UDP:
  693. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  694. break;
  695. default:
  696. tun->dev->stats.rx_frame_errors++;
  697. kfree_skb(skb);
  698. return -EINVAL;
  699. }
  700. if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  701. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  702. skb_shinfo(skb)->gso_size = gso.gso_size;
  703. if (skb_shinfo(skb)->gso_size == 0) {
  704. tun->dev->stats.rx_frame_errors++;
  705. kfree_skb(skb);
  706. return -EINVAL;
  707. }
  708. /* Header must be checked, and gso_segs computed. */
  709. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  710. skb_shinfo(skb)->gso_segs = 0;
  711. }
  712. /* copy skb_ubuf_info for callback when skb has no error */
  713. if (zerocopy) {
  714. skb_shinfo(skb)->destructor_arg = msg_control;
  715. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  716. }
  717. netif_rx_ni(skb);
  718. tun->dev->stats.rx_packets++;
  719. tun->dev->stats.rx_bytes += len;
  720. return total_len;
  721. }
  722. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  723. unsigned long count, loff_t pos)
  724. {
  725. struct file *file = iocb->ki_filp;
  726. struct tun_struct *tun = tun_get(file);
  727. ssize_t result;
  728. if (!tun)
  729. return -EBADFD;
  730. tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
  731. result = tun_get_user(tun, NULL, iv, iov_length(iv, count), count,
  732. file->f_flags & O_NONBLOCK);
  733. tun_put(tun);
  734. return result;
  735. }
  736. /* Put packet to the user space buffer */
  737. static ssize_t tun_put_user(struct tun_struct *tun,
  738. struct sk_buff *skb,
  739. const struct iovec *iv, int len)
  740. {
  741. struct tun_pi pi = { 0, skb->protocol };
  742. ssize_t total = 0;
  743. if (!(tun->flags & TUN_NO_PI)) {
  744. if ((len -= sizeof(pi)) < 0)
  745. return -EINVAL;
  746. if (len < skb->len) {
  747. /* Packet will be striped */
  748. pi.flags |= TUN_PKT_STRIP;
  749. }
  750. if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
  751. return -EFAULT;
  752. total += sizeof(pi);
  753. }
  754. if (tun->flags & TUN_VNET_HDR) {
  755. struct virtio_net_hdr gso = { 0 }; /* no info leak */
  756. if ((len -= tun->vnet_hdr_sz) < 0)
  757. return -EINVAL;
  758. if (skb_is_gso(skb)) {
  759. struct skb_shared_info *sinfo = skb_shinfo(skb);
  760. /* This is a hint as to how much should be linear. */
  761. gso.hdr_len = skb_headlen(skb);
  762. gso.gso_size = sinfo->gso_size;
  763. if (sinfo->gso_type & SKB_GSO_TCPV4)
  764. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  765. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  766. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  767. else if (sinfo->gso_type & SKB_GSO_UDP)
  768. gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  769. else {
  770. pr_err("unexpected GSO type: "
  771. "0x%x, gso_size %d, hdr_len %d\n",
  772. sinfo->gso_type, gso.gso_size,
  773. gso.hdr_len);
  774. print_hex_dump(KERN_ERR, "tun: ",
  775. DUMP_PREFIX_NONE,
  776. 16, 1, skb->head,
  777. min((int)gso.hdr_len, 64), true);
  778. WARN_ON_ONCE(1);
  779. return -EINVAL;
  780. }
  781. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  782. gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  783. } else
  784. gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  785. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  786. gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  787. gso.csum_start = skb_checksum_start_offset(skb);
  788. gso.csum_offset = skb->csum_offset;
  789. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  790. gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
  791. } /* else everything is zero */
  792. if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
  793. sizeof(gso))))
  794. return -EFAULT;
  795. total += tun->vnet_hdr_sz;
  796. }
  797. len = min_t(int, skb->len, len);
  798. skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
  799. total += skb->len;
  800. tun->dev->stats.tx_packets++;
  801. tun->dev->stats.tx_bytes += len;
  802. return total;
  803. }
  804. static ssize_t tun_do_read(struct tun_struct *tun,
  805. struct kiocb *iocb, const struct iovec *iv,
  806. ssize_t len, int noblock)
  807. {
  808. DECLARE_WAITQUEUE(wait, current);
  809. struct sk_buff *skb;
  810. ssize_t ret = 0;
  811. tun_debug(KERN_INFO, tun, "tun_chr_read\n");
  812. if (unlikely(!noblock))
  813. add_wait_queue(&tun->wq.wait, &wait);
  814. while (len) {
  815. current->state = TASK_INTERRUPTIBLE;
  816. /* Read frames from the queue */
  817. if (!(skb=skb_dequeue(&tun->socket.sk->sk_receive_queue))) {
  818. if (noblock) {
  819. ret = -EAGAIN;
  820. break;
  821. }
  822. if (signal_pending(current)) {
  823. ret = -ERESTARTSYS;
  824. break;
  825. }
  826. if (tun->dev->reg_state != NETREG_REGISTERED) {
  827. ret = -EIO;
  828. break;
  829. }
  830. /* Nothing to read, let's sleep */
  831. schedule();
  832. continue;
  833. }
  834. netif_wake_queue(tun->dev);
  835. ret = tun_put_user(tun, skb, iv, len);
  836. kfree_skb(skb);
  837. break;
  838. }
  839. current->state = TASK_RUNNING;
  840. if (unlikely(!noblock))
  841. remove_wait_queue(&tun->wq.wait, &wait);
  842. return ret;
  843. }
  844. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  845. unsigned long count, loff_t pos)
  846. {
  847. struct file *file = iocb->ki_filp;
  848. struct tun_file *tfile = file->private_data;
  849. struct tun_struct *tun = __tun_get(tfile);
  850. ssize_t len, ret;
  851. if (!tun)
  852. return -EBADFD;
  853. len = iov_length(iv, count);
  854. if (len < 0) {
  855. ret = -EINVAL;
  856. goto out;
  857. }
  858. ret = tun_do_read(tun, iocb, iv, len, file->f_flags & O_NONBLOCK);
  859. ret = min_t(ssize_t, ret, len);
  860. out:
  861. tun_put(tun);
  862. return ret;
  863. }
  864. static void tun_setup(struct net_device *dev)
  865. {
  866. struct tun_struct *tun = netdev_priv(dev);
  867. tun->owner = -1;
  868. tun->group = -1;
  869. dev->ethtool_ops = &tun_ethtool_ops;
  870. dev->destructor = tun_free_netdev;
  871. }
  872. /* Trivial set of netlink ops to allow deleting tun or tap
  873. * device with netlink.
  874. */
  875. static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
  876. {
  877. return -EINVAL;
  878. }
  879. static struct rtnl_link_ops tun_link_ops __read_mostly = {
  880. .kind = DRV_NAME,
  881. .priv_size = sizeof(struct tun_struct),
  882. .setup = tun_setup,
  883. .validate = tun_validate,
  884. };
  885. static void tun_sock_write_space(struct sock *sk)
  886. {
  887. struct tun_struct *tun;
  888. wait_queue_head_t *wqueue;
  889. if (!sock_writeable(sk))
  890. return;
  891. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  892. return;
  893. wqueue = sk_sleep(sk);
  894. if (wqueue && waitqueue_active(wqueue))
  895. wake_up_interruptible_sync_poll(wqueue, POLLOUT |
  896. POLLWRNORM | POLLWRBAND);
  897. tun = tun_sk(sk)->tun;
  898. kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
  899. }
  900. static void tun_sock_destruct(struct sock *sk)
  901. {
  902. free_netdev(tun_sk(sk)->tun->dev);
  903. }
  904. static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
  905. struct msghdr *m, size_t total_len)
  906. {
  907. struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
  908. return tun_get_user(tun, m->msg_control, m->msg_iov, total_len,
  909. m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
  910. }
  911. static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
  912. struct msghdr *m, size_t total_len,
  913. int flags)
  914. {
  915. struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
  916. int ret;
  917. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  918. return -EINVAL;
  919. ret = tun_do_read(tun, iocb, m->msg_iov, total_len,
  920. flags & MSG_DONTWAIT);
  921. if (ret > total_len) {
  922. m->msg_flags |= MSG_TRUNC;
  923. ret = flags & MSG_TRUNC ? ret : total_len;
  924. }
  925. return ret;
  926. }
  927. static int tun_release(struct socket *sock)
  928. {
  929. if (sock->sk)
  930. sock_put(sock->sk);
  931. return 0;
  932. }
  933. /* Ops structure to mimic raw sockets with tun */
  934. static const struct proto_ops tun_socket_ops = {
  935. .sendmsg = tun_sendmsg,
  936. .recvmsg = tun_recvmsg,
  937. .release = tun_release,
  938. };
  939. static struct proto tun_proto = {
  940. .name = "tun",
  941. .owner = THIS_MODULE,
  942. .obj_size = sizeof(struct tun_sock),
  943. };
  944. static int tun_flags(struct tun_struct *tun)
  945. {
  946. int flags = 0;
  947. if (tun->flags & TUN_TUN_DEV)
  948. flags |= IFF_TUN;
  949. else
  950. flags |= IFF_TAP;
  951. if (tun->flags & TUN_NO_PI)
  952. flags |= IFF_NO_PI;
  953. if (tun->flags & TUN_ONE_QUEUE)
  954. flags |= IFF_ONE_QUEUE;
  955. if (tun->flags & TUN_VNET_HDR)
  956. flags |= IFF_VNET_HDR;
  957. return flags;
  958. }
  959. static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
  960. char *buf)
  961. {
  962. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  963. return sprintf(buf, "0x%x\n", tun_flags(tun));
  964. }
  965. static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
  966. char *buf)
  967. {
  968. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  969. return sprintf(buf, "%d\n", tun->owner);
  970. }
  971. static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
  972. char *buf)
  973. {
  974. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  975. return sprintf(buf, "%d\n", tun->group);
  976. }
  977. static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
  978. static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
  979. static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
  980. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  981. {
  982. struct sock *sk;
  983. struct tun_struct *tun;
  984. struct net_device *dev;
  985. int err;
  986. dev = __dev_get_by_name(net, ifr->ifr_name);
  987. if (dev) {
  988. const struct cred *cred = current_cred();
  989. if (ifr->ifr_flags & IFF_TUN_EXCL)
  990. return -EBUSY;
  991. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  992. tun = netdev_priv(dev);
  993. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  994. tun = netdev_priv(dev);
  995. else
  996. return -EINVAL;
  997. if (((tun->owner != -1 && cred->euid != tun->owner) ||
  998. (tun->group != -1 && !in_egroup_p(tun->group))) &&
  999. !capable(CAP_NET_ADMIN))
  1000. return -EPERM;
  1001. err = security_tun_dev_attach(tun->socket.sk);
  1002. if (err < 0)
  1003. return err;
  1004. err = tun_attach(tun, file);
  1005. if (err < 0)
  1006. return err;
  1007. }
  1008. else {
  1009. char *name;
  1010. unsigned long flags = 0;
  1011. if (!capable(CAP_NET_ADMIN))
  1012. return -EPERM;
  1013. err = security_tun_dev_create();
  1014. if (err < 0)
  1015. return err;
  1016. /* Set dev type */
  1017. if (ifr->ifr_flags & IFF_TUN) {
  1018. /* TUN device */
  1019. flags |= TUN_TUN_DEV;
  1020. name = "tun%d";
  1021. } else if (ifr->ifr_flags & IFF_TAP) {
  1022. /* TAP device */
  1023. flags |= TUN_TAP_DEV;
  1024. name = "tap%d";
  1025. } else
  1026. return -EINVAL;
  1027. if (*ifr->ifr_name)
  1028. name = ifr->ifr_name;
  1029. dev = alloc_netdev(sizeof(struct tun_struct), name,
  1030. tun_setup);
  1031. if (!dev)
  1032. return -ENOMEM;
  1033. dev_net_set(dev, net);
  1034. dev->rtnl_link_ops = &tun_link_ops;
  1035. tun = netdev_priv(dev);
  1036. tun->dev = dev;
  1037. tun->flags = flags;
  1038. tun->txflt.count = 0;
  1039. tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  1040. set_bit(SOCK_EXTERNALLY_ALLOCATED, &tun->socket.flags);
  1041. err = -ENOMEM;
  1042. sk = sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
  1043. if (!sk)
  1044. goto err_free_dev;
  1045. sk_change_net(sk, net);
  1046. tun->socket.wq = &tun->wq;
  1047. init_waitqueue_head(&tun->wq.wait);
  1048. tun->socket.ops = &tun_socket_ops;
  1049. sock_init_data(&tun->socket, sk);
  1050. sk->sk_write_space = tun_sock_write_space;
  1051. sk->sk_sndbuf = INT_MAX;
  1052. sock_set_flag(sk, SOCK_ZEROCOPY);
  1053. tun_sk(sk)->tun = tun;
  1054. security_tun_dev_post_create(sk);
  1055. tun_net_init(dev);
  1056. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
  1057. TUN_USER_FEATURES;
  1058. dev->features = dev->hw_features;
  1059. err = register_netdevice(tun->dev);
  1060. if (err < 0)
  1061. goto err_free_sk;
  1062. if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
  1063. device_create_file(&tun->dev->dev, &dev_attr_owner) ||
  1064. device_create_file(&tun->dev->dev, &dev_attr_group))
  1065. pr_err("Failed to create tun sysfs files\n");
  1066. sk->sk_destruct = tun_sock_destruct;
  1067. err = tun_attach(tun, file);
  1068. if (err < 0)
  1069. goto failed;
  1070. }
  1071. tun_debug(KERN_INFO, tun, "tun_set_iff\n");
  1072. if (ifr->ifr_flags & IFF_NO_PI)
  1073. tun->flags |= TUN_NO_PI;
  1074. else
  1075. tun->flags &= ~TUN_NO_PI;
  1076. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  1077. tun->flags |= TUN_ONE_QUEUE;
  1078. else
  1079. tun->flags &= ~TUN_ONE_QUEUE;
  1080. if (ifr->ifr_flags & IFF_VNET_HDR)
  1081. tun->flags |= TUN_VNET_HDR;
  1082. else
  1083. tun->flags &= ~TUN_VNET_HDR;
  1084. /* Make sure persistent devices do not get stuck in
  1085. * xoff state.
  1086. */
  1087. if (netif_running(tun->dev))
  1088. netif_wake_queue(tun->dev);
  1089. strcpy(ifr->ifr_name, tun->dev->name);
  1090. return 0;
  1091. err_free_sk:
  1092. tun_free_netdev(dev);
  1093. err_free_dev:
  1094. free_netdev(dev);
  1095. failed:
  1096. return err;
  1097. }
  1098. static int tun_get_iff(struct net *net, struct tun_struct *tun,
  1099. struct ifreq *ifr)
  1100. {
  1101. tun_debug(KERN_INFO, tun, "tun_get_iff\n");
  1102. strcpy(ifr->ifr_name, tun->dev->name);
  1103. ifr->ifr_flags = tun_flags(tun);
  1104. return 0;
  1105. }
  1106. /* This is like a cut-down ethtool ops, except done via tun fd so no
  1107. * privs required. */
  1108. static int set_offload(struct tun_struct *tun, unsigned long arg)
  1109. {
  1110. netdev_features_t features = 0;
  1111. if (arg & TUN_F_CSUM) {
  1112. features |= NETIF_F_HW_CSUM;
  1113. arg &= ~TUN_F_CSUM;
  1114. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  1115. if (arg & TUN_F_TSO_ECN) {
  1116. features |= NETIF_F_TSO_ECN;
  1117. arg &= ~TUN_F_TSO_ECN;
  1118. }
  1119. if (arg & TUN_F_TSO4)
  1120. features |= NETIF_F_TSO;
  1121. if (arg & TUN_F_TSO6)
  1122. features |= NETIF_F_TSO6;
  1123. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  1124. }
  1125. if (arg & TUN_F_UFO) {
  1126. features |= NETIF_F_UFO;
  1127. arg &= ~TUN_F_UFO;
  1128. }
  1129. }
  1130. /* This gives the user a way to test for new features in future by
  1131. * trying to set them. */
  1132. if (arg)
  1133. return -EINVAL;
  1134. tun->set_features = features;
  1135. netdev_update_features(tun->dev);
  1136. return 0;
  1137. }
  1138. static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  1139. unsigned long arg, int ifreq_len)
  1140. {
  1141. struct tun_file *tfile = file->private_data;
  1142. struct tun_struct *tun;
  1143. void __user* argp = (void __user*)arg;
  1144. struct sock_fprog fprog;
  1145. struct ifreq ifr;
  1146. int sndbuf;
  1147. int vnet_hdr_sz;
  1148. int ret;
  1149. if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) {
  1150. if (copy_from_user(&ifr, argp, ifreq_len))
  1151. return -EFAULT;
  1152. } else {
  1153. memset(&ifr, 0, sizeof(ifr));
  1154. }
  1155. if (cmd == TUNGETFEATURES) {
  1156. /* Currently this just means: "what IFF flags are valid?".
  1157. * This is needed because we never checked for invalid flags on
  1158. * TUNSETIFF. */
  1159. return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
  1160. IFF_VNET_HDR,
  1161. (unsigned int __user*)argp);
  1162. }
  1163. rtnl_lock();
  1164. tun = __tun_get(tfile);
  1165. if (cmd == TUNSETIFF && !tun) {
  1166. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  1167. ret = tun_set_iff(tfile->net, file, &ifr);
  1168. if (ret)
  1169. goto unlock;
  1170. if (copy_to_user(argp, &ifr, ifreq_len))
  1171. ret = -EFAULT;
  1172. goto unlock;
  1173. }
  1174. ret = -EBADFD;
  1175. if (!tun)
  1176. goto unlock;
  1177. tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %d\n", cmd);
  1178. ret = 0;
  1179. switch (cmd) {
  1180. case TUNGETIFF:
  1181. ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
  1182. if (ret)
  1183. break;
  1184. if (copy_to_user(argp, &ifr, ifreq_len))
  1185. ret = -EFAULT;
  1186. break;
  1187. case TUNSETNOCSUM:
  1188. /* Disable/Enable checksum */
  1189. /* [unimplemented] */
  1190. tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
  1191. arg ? "disabled" : "enabled");
  1192. break;
  1193. case TUNSETPERSIST:
  1194. /* Disable/Enable persist mode */
  1195. if (arg)
  1196. tun->flags |= TUN_PERSIST;
  1197. else
  1198. tun->flags &= ~TUN_PERSIST;
  1199. tun_debug(KERN_INFO, tun, "persist %s\n",
  1200. arg ? "enabled" : "disabled");
  1201. break;
  1202. case TUNSETOWNER:
  1203. /* Set owner of the device */
  1204. tun->owner = (uid_t) arg;
  1205. tun_debug(KERN_INFO, tun, "owner set to %d\n", tun->owner);
  1206. break;
  1207. case TUNSETGROUP:
  1208. /* Set group of the device */
  1209. tun->group= (gid_t) arg;
  1210. tun_debug(KERN_INFO, tun, "group set to %d\n", tun->group);
  1211. break;
  1212. case TUNSETLINK:
  1213. /* Only allow setting the type when the interface is down */
  1214. if (tun->dev->flags & IFF_UP) {
  1215. tun_debug(KERN_INFO, tun,
  1216. "Linktype set failed because interface is up\n");
  1217. ret = -EBUSY;
  1218. } else {
  1219. tun->dev->type = (int) arg;
  1220. tun_debug(KERN_INFO, tun, "linktype set to %d\n",
  1221. tun->dev->type);
  1222. ret = 0;
  1223. }
  1224. break;
  1225. #ifdef TUN_DEBUG
  1226. case TUNSETDEBUG:
  1227. tun->debug = arg;
  1228. break;
  1229. #endif
  1230. case TUNSETOFFLOAD:
  1231. ret = set_offload(tun, arg);
  1232. break;
  1233. case TUNSETTXFILTER:
  1234. /* Can be set only for TAPs */
  1235. ret = -EINVAL;
  1236. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1237. break;
  1238. ret = update_filter(&tun->txflt, (void __user *)arg);
  1239. break;
  1240. case SIOCGIFHWADDR:
  1241. /* Get hw address */
  1242. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  1243. ifr.ifr_hwaddr.sa_family = tun->dev->type;
  1244. if (copy_to_user(argp, &ifr, ifreq_len))
  1245. ret = -EFAULT;
  1246. break;
  1247. case SIOCSIFHWADDR:
  1248. /* Set hw address */
  1249. tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
  1250. ifr.ifr_hwaddr.sa_data);
  1251. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  1252. break;
  1253. case TUNGETSNDBUF:
  1254. sndbuf = tun->socket.sk->sk_sndbuf;
  1255. if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
  1256. ret = -EFAULT;
  1257. break;
  1258. case TUNSETSNDBUF:
  1259. if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
  1260. ret = -EFAULT;
  1261. break;
  1262. }
  1263. tun->socket.sk->sk_sndbuf = sndbuf;
  1264. break;
  1265. case TUNGETVNETHDRSZ:
  1266. vnet_hdr_sz = tun->vnet_hdr_sz;
  1267. if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
  1268. ret = -EFAULT;
  1269. break;
  1270. case TUNSETVNETHDRSZ:
  1271. if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
  1272. ret = -EFAULT;
  1273. break;
  1274. }
  1275. if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
  1276. ret = -EINVAL;
  1277. break;
  1278. }
  1279. tun->vnet_hdr_sz = vnet_hdr_sz;
  1280. break;
  1281. case TUNATTACHFILTER:
  1282. /* Can be set only for TAPs */
  1283. ret = -EINVAL;
  1284. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1285. break;
  1286. ret = -EFAULT;
  1287. if (copy_from_user(&fprog, argp, sizeof(fprog)))
  1288. break;
  1289. ret = sk_attach_filter(&fprog, tun->socket.sk);
  1290. break;
  1291. case TUNDETACHFILTER:
  1292. /* Can be set only for TAPs */
  1293. ret = -EINVAL;
  1294. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1295. break;
  1296. ret = sk_detach_filter(tun->socket.sk);
  1297. break;
  1298. default:
  1299. ret = -EINVAL;
  1300. break;
  1301. }
  1302. unlock:
  1303. rtnl_unlock();
  1304. if (tun)
  1305. tun_put(tun);
  1306. return ret;
  1307. }
  1308. static long tun_chr_ioctl(struct file *file,
  1309. unsigned int cmd, unsigned long arg)
  1310. {
  1311. return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
  1312. }
  1313. #ifdef CONFIG_COMPAT
  1314. static long tun_chr_compat_ioctl(struct file *file,
  1315. unsigned int cmd, unsigned long arg)
  1316. {
  1317. switch (cmd) {
  1318. case TUNSETIFF:
  1319. case TUNGETIFF:
  1320. case TUNSETTXFILTER:
  1321. case TUNGETSNDBUF:
  1322. case TUNSETSNDBUF:
  1323. case SIOCGIFHWADDR:
  1324. case SIOCSIFHWADDR:
  1325. arg = (unsigned long)compat_ptr(arg);
  1326. break;
  1327. default:
  1328. arg = (compat_ulong_t)arg;
  1329. break;
  1330. }
  1331. /*
  1332. * compat_ifreq is shorter than ifreq, so we must not access beyond
  1333. * the end of that structure. All fields that are used in this
  1334. * driver are compatible though, we don't need to convert the
  1335. * contents.
  1336. */
  1337. return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
  1338. }
  1339. #endif /* CONFIG_COMPAT */
  1340. static int tun_chr_fasync(int fd, struct file *file, int on)
  1341. {
  1342. struct tun_struct *tun = tun_get(file);
  1343. int ret;
  1344. if (!tun)
  1345. return -EBADFD;
  1346. tun_debug(KERN_INFO, tun, "tun_chr_fasync %d\n", on);
  1347. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  1348. goto out;
  1349. if (on) {
  1350. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  1351. if (ret)
  1352. goto out;
  1353. tun->flags |= TUN_FASYNC;
  1354. } else
  1355. tun->flags &= ~TUN_FASYNC;
  1356. ret = 0;
  1357. out:
  1358. tun_put(tun);
  1359. return ret;
  1360. }
  1361. static int tun_chr_open(struct inode *inode, struct file * file)
  1362. {
  1363. struct tun_file *tfile;
  1364. DBG1(KERN_INFO, "tunX: tun_chr_open\n");
  1365. tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
  1366. if (!tfile)
  1367. return -ENOMEM;
  1368. atomic_set(&tfile->count, 0);
  1369. tfile->tun = NULL;
  1370. tfile->net = get_net(current->nsproxy->net_ns);
  1371. file->private_data = tfile;
  1372. return 0;
  1373. }
  1374. static int tun_chr_close(struct inode *inode, struct file *file)
  1375. {
  1376. struct tun_file *tfile = file->private_data;
  1377. struct tun_struct *tun;
  1378. tun = __tun_get(tfile);
  1379. if (tun) {
  1380. struct net_device *dev = tun->dev;
  1381. tun_debug(KERN_INFO, tun, "tun_chr_close\n");
  1382. __tun_detach(tun);
  1383. /* If desirable, unregister the netdevice. */
  1384. if (!(tun->flags & TUN_PERSIST)) {
  1385. rtnl_lock();
  1386. if (dev->reg_state == NETREG_REGISTERED)
  1387. unregister_netdevice(dev);
  1388. rtnl_unlock();
  1389. }
  1390. }
  1391. tun = tfile->tun;
  1392. if (tun)
  1393. sock_put(tun->socket.sk);
  1394. put_net(tfile->net);
  1395. kfree(tfile);
  1396. return 0;
  1397. }
  1398. static const struct file_operations tun_fops = {
  1399. .owner = THIS_MODULE,
  1400. .llseek = no_llseek,
  1401. .read = do_sync_read,
  1402. .aio_read = tun_chr_aio_read,
  1403. .write = do_sync_write,
  1404. .aio_write = tun_chr_aio_write,
  1405. .poll = tun_chr_poll,
  1406. .unlocked_ioctl = tun_chr_ioctl,
  1407. #ifdef CONFIG_COMPAT
  1408. .compat_ioctl = tun_chr_compat_ioctl,
  1409. #endif
  1410. .open = tun_chr_open,
  1411. .release = tun_chr_close,
  1412. .fasync = tun_chr_fasync
  1413. };
  1414. static struct miscdevice tun_miscdev = {
  1415. .minor = TUN_MINOR,
  1416. .name = "tun",
  1417. .nodename = "net/tun",
  1418. .fops = &tun_fops,
  1419. };
  1420. /* ethtool interface */
  1421. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1422. {
  1423. cmd->supported = 0;
  1424. cmd->advertising = 0;
  1425. ethtool_cmd_speed_set(cmd, SPEED_10);
  1426. cmd->duplex = DUPLEX_FULL;
  1427. cmd->port = PORT_TP;
  1428. cmd->phy_address = 0;
  1429. cmd->transceiver = XCVR_INTERNAL;
  1430. cmd->autoneg = AUTONEG_DISABLE;
  1431. cmd->maxtxpkt = 0;
  1432. cmd->maxrxpkt = 0;
  1433. return 0;
  1434. }
  1435. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1436. {
  1437. struct tun_struct *tun = netdev_priv(dev);
  1438. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  1439. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1440. switch (tun->flags & TUN_TYPE_MASK) {
  1441. case TUN_TUN_DEV:
  1442. strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
  1443. break;
  1444. case TUN_TAP_DEV:
  1445. strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
  1446. break;
  1447. }
  1448. }
  1449. static u32 tun_get_msglevel(struct net_device *dev)
  1450. {
  1451. #ifdef TUN_DEBUG
  1452. struct tun_struct *tun = netdev_priv(dev);
  1453. return tun->debug;
  1454. #else
  1455. return -EOPNOTSUPP;
  1456. #endif
  1457. }
  1458. static void tun_set_msglevel(struct net_device *dev, u32 value)
  1459. {
  1460. #ifdef TUN_DEBUG
  1461. struct tun_struct *tun = netdev_priv(dev);
  1462. tun->debug = value;
  1463. #endif
  1464. }
  1465. static const struct ethtool_ops tun_ethtool_ops = {
  1466. .get_settings = tun_get_settings,
  1467. .get_drvinfo = tun_get_drvinfo,
  1468. .get_msglevel = tun_get_msglevel,
  1469. .set_msglevel = tun_set_msglevel,
  1470. .get_link = ethtool_op_get_link,
  1471. };
  1472. static int __init tun_init(void)
  1473. {
  1474. int ret = 0;
  1475. pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1476. pr_info("%s\n", DRV_COPYRIGHT);
  1477. ret = rtnl_link_register(&tun_link_ops);
  1478. if (ret) {
  1479. pr_err("Can't register link_ops\n");
  1480. goto err_linkops;
  1481. }
  1482. ret = misc_register(&tun_miscdev);
  1483. if (ret) {
  1484. pr_err("Can't register misc device %d\n", TUN_MINOR);
  1485. goto err_misc;
  1486. }
  1487. return 0;
  1488. err_misc:
  1489. rtnl_link_unregister(&tun_link_ops);
  1490. err_linkops:
  1491. return ret;
  1492. }
  1493. static void tun_cleanup(void)
  1494. {
  1495. misc_deregister(&tun_miscdev);
  1496. rtnl_link_unregister(&tun_link_ops);
  1497. }
  1498. /* Get an underlying socket object from tun file. Returns error unless file is
  1499. * attached to a device. The returned object works like a packet socket, it
  1500. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1501. * holding a reference to the file for as long as the socket is in use. */
  1502. struct socket *tun_get_socket(struct file *file)
  1503. {
  1504. struct tun_struct *tun;
  1505. if (file->f_op != &tun_fops)
  1506. return ERR_PTR(-EINVAL);
  1507. tun = tun_get(file);
  1508. if (!tun)
  1509. return ERR_PTR(-EBADFD);
  1510. tun_put(tun);
  1511. return &tun->socket;
  1512. }
  1513. EXPORT_SYMBOL_GPL(tun_get_socket);
  1514. module_init(tun_init);
  1515. module_exit(tun_cleanup);
  1516. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1517. MODULE_AUTHOR(DRV_COPYRIGHT);
  1518. MODULE_LICENSE("GPL");
  1519. MODULE_ALIAS_MISCDEV(TUN_MINOR);
  1520. MODULE_ALIAS("devname:net/tun");