tun.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  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 random_ether_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/system.h>
  68. #include <asm/uaccess.h>
  69. /* Uncomment to enable debugging */
  70. /* #define TUN_DEBUG 1 */
  71. #ifdef TUN_DEBUG
  72. static int debug;
  73. #define tun_debug(level, tun, fmt, args...) \
  74. do { \
  75. if (tun->debug) \
  76. netdev_printk(level, tun->dev, fmt, ##args); \
  77. } while (0)
  78. #define DBG1(level, fmt, args...) \
  79. do { \
  80. if (debug == 2) \
  81. printk(level fmt, ##args); \
  82. } while (0)
  83. #else
  84. #define tun_debug(level, tun, fmt, args...) \
  85. do { \
  86. if (0) \
  87. netdev_printk(level, tun->dev, fmt, ##args); \
  88. } while (0)
  89. #define DBG1(level, fmt, args...) \
  90. do { \
  91. if (0) \
  92. printk(level fmt, ##args); \
  93. } while (0)
  94. #endif
  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. u32 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. tun->socket.file = NULL;
  164. netif_tx_unlock_bh(tun->dev);
  165. /* Drop read queue */
  166. skb_queue_purge(&tun->socket.sk->sk_receive_queue);
  167. /* Drop the extra count on the net device */
  168. dev_put(tun->dev);
  169. }
  170. static void tun_detach(struct tun_struct *tun)
  171. {
  172. rtnl_lock();
  173. __tun_detach(tun);
  174. rtnl_unlock();
  175. }
  176. static struct tun_struct *__tun_get(struct tun_file *tfile)
  177. {
  178. struct tun_struct *tun = NULL;
  179. if (atomic_inc_not_zero(&tfile->count))
  180. tun = tfile->tun;
  181. return tun;
  182. }
  183. static struct tun_struct *tun_get(struct file *file)
  184. {
  185. return __tun_get(file->private_data);
  186. }
  187. static void tun_put(struct tun_struct *tun)
  188. {
  189. struct tun_file *tfile = tun->tfile;
  190. if (atomic_dec_and_test(&tfile->count))
  191. tun_detach(tfile->tun);
  192. }
  193. /* TAP filtering */
  194. static void addr_hash_set(u32 *mask, const u8 *addr)
  195. {
  196. int n = ether_crc(ETH_ALEN, addr) >> 26;
  197. mask[n >> 5] |= (1 << (n & 31));
  198. }
  199. static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
  200. {
  201. int n = ether_crc(ETH_ALEN, addr) >> 26;
  202. return mask[n >> 5] & (1 << (n & 31));
  203. }
  204. static int update_filter(struct tap_filter *filter, void __user *arg)
  205. {
  206. struct { u8 u[ETH_ALEN]; } *addr;
  207. struct tun_filter uf;
  208. int err, alen, n, nexact;
  209. if (copy_from_user(&uf, arg, sizeof(uf)))
  210. return -EFAULT;
  211. if (!uf.count) {
  212. /* Disabled */
  213. filter->count = 0;
  214. return 0;
  215. }
  216. alen = ETH_ALEN * uf.count;
  217. addr = kmalloc(alen, GFP_KERNEL);
  218. if (!addr)
  219. return -ENOMEM;
  220. if (copy_from_user(addr, arg + sizeof(uf), alen)) {
  221. err = -EFAULT;
  222. goto done;
  223. }
  224. /* The filter is updated without holding any locks. Which is
  225. * perfectly safe. We disable it first and in the worst
  226. * case we'll accept a few undesired packets. */
  227. filter->count = 0;
  228. wmb();
  229. /* Use first set of addresses as an exact filter */
  230. for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
  231. memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
  232. nexact = n;
  233. /* Remaining multicast addresses are hashed,
  234. * unicast will leave the filter disabled. */
  235. memset(filter->mask, 0, sizeof(filter->mask));
  236. for (; n < uf.count; n++) {
  237. if (!is_multicast_ether_addr(addr[n].u)) {
  238. err = 0; /* no filter */
  239. goto done;
  240. }
  241. addr_hash_set(filter->mask, addr[n].u);
  242. }
  243. /* For ALLMULTI just set the mask to all ones.
  244. * This overrides the mask populated above. */
  245. if ((uf.flags & TUN_FLT_ALLMULTI))
  246. memset(filter->mask, ~0, sizeof(filter->mask));
  247. /* Now enable the filter */
  248. wmb();
  249. filter->count = nexact;
  250. /* Return the number of exact filters */
  251. err = nexact;
  252. done:
  253. kfree(addr);
  254. return err;
  255. }
  256. /* Returns: 0 - drop, !=0 - accept */
  257. static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
  258. {
  259. /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
  260. * at this point. */
  261. struct ethhdr *eh = (struct ethhdr *) skb->data;
  262. int i;
  263. /* Exact match */
  264. for (i = 0; i < filter->count; i++)
  265. if (!compare_ether_addr(eh->h_dest, filter->addr[i]))
  266. return 1;
  267. /* Inexact match (multicast only) */
  268. if (is_multicast_ether_addr(eh->h_dest))
  269. return addr_hash_test(filter->mask, eh->h_dest);
  270. return 0;
  271. }
  272. /*
  273. * Checks whether the packet is accepted or not.
  274. * Returns: 0 - drop, !=0 - accept
  275. */
  276. static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
  277. {
  278. if (!filter->count)
  279. return 1;
  280. return run_filter(filter, skb);
  281. }
  282. /* Network device part of the driver */
  283. static const struct ethtool_ops tun_ethtool_ops;
  284. /* Net device detach from fd. */
  285. static void tun_net_uninit(struct net_device *dev)
  286. {
  287. struct tun_struct *tun = netdev_priv(dev);
  288. struct tun_file *tfile = tun->tfile;
  289. /* Inform the methods they need to stop using the dev.
  290. */
  291. if (tfile) {
  292. wake_up_all(&tun->wq.wait);
  293. if (atomic_dec_and_test(&tfile->count))
  294. __tun_detach(tun);
  295. }
  296. }
  297. static void tun_free_netdev(struct net_device *dev)
  298. {
  299. struct tun_struct *tun = netdev_priv(dev);
  300. sock_put(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. skb_orphan(skb);
  347. /* Enqueue packet */
  348. skb_queue_tail(&tun->socket.sk->sk_receive_queue, skb);
  349. /* Notify and wake up reader process */
  350. if (tun->flags & TUN_FASYNC)
  351. kill_fasync(&tun->fasync, SIGIO, POLL_IN);
  352. wake_up_interruptible_poll(&tun->wq.wait, POLLIN |
  353. POLLRDNORM | POLLRDBAND);
  354. return NETDEV_TX_OK;
  355. drop:
  356. dev->stats.tx_dropped++;
  357. kfree_skb(skb);
  358. return NETDEV_TX_OK;
  359. }
  360. static void tun_net_mclist(struct net_device *dev)
  361. {
  362. /*
  363. * This callback is supposed to deal with mc filter in
  364. * _rx_ path and has nothing to do with the _tx_ path.
  365. * In rx path we always accept everything userspace gives us.
  366. */
  367. }
  368. #define MIN_MTU 68
  369. #define MAX_MTU 65535
  370. static int
  371. tun_net_change_mtu(struct net_device *dev, int new_mtu)
  372. {
  373. if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
  374. return -EINVAL;
  375. dev->mtu = new_mtu;
  376. return 0;
  377. }
  378. static u32 tun_net_fix_features(struct net_device *dev, u32 features)
  379. {
  380. struct tun_struct *tun = netdev_priv(dev);
  381. return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
  382. }
  383. #ifdef CONFIG_NET_POLL_CONTROLLER
  384. static void tun_poll_controller(struct net_device *dev)
  385. {
  386. /*
  387. * Tun only receives frames when:
  388. * 1) the char device endpoint gets data from user space
  389. * 2) the tun socket gets a sendmsg call from user space
  390. * Since both of those are syncronous operations, we are guaranteed
  391. * never to have pending data when we poll for it
  392. * so theres nothing to do here but return.
  393. * We need this though so netpoll recognizes us as an interface that
  394. * supports polling, which enables bridge devices in virt setups to
  395. * still use netconsole
  396. */
  397. return;
  398. }
  399. #endif
  400. static const struct net_device_ops tun_netdev_ops = {
  401. .ndo_uninit = tun_net_uninit,
  402. .ndo_open = tun_net_open,
  403. .ndo_stop = tun_net_close,
  404. .ndo_start_xmit = tun_net_xmit,
  405. .ndo_change_mtu = tun_net_change_mtu,
  406. .ndo_fix_features = tun_net_fix_features,
  407. #ifdef CONFIG_NET_POLL_CONTROLLER
  408. .ndo_poll_controller = tun_poll_controller,
  409. #endif
  410. };
  411. static const struct net_device_ops tap_netdev_ops = {
  412. .ndo_uninit = tun_net_uninit,
  413. .ndo_open = tun_net_open,
  414. .ndo_stop = tun_net_close,
  415. .ndo_start_xmit = tun_net_xmit,
  416. .ndo_change_mtu = tun_net_change_mtu,
  417. .ndo_fix_features = tun_net_fix_features,
  418. .ndo_set_multicast_list = tun_net_mclist,
  419. .ndo_set_mac_address = eth_mac_addr,
  420. .ndo_validate_addr = eth_validate_addr,
  421. #ifdef CONFIG_NET_POLL_CONTROLLER
  422. .ndo_poll_controller = tun_poll_controller,
  423. #endif
  424. };
  425. /* Initialize net device. */
  426. static void tun_net_init(struct net_device *dev)
  427. {
  428. struct tun_struct *tun = netdev_priv(dev);
  429. switch (tun->flags & TUN_TYPE_MASK) {
  430. case TUN_TUN_DEV:
  431. dev->netdev_ops = &tun_netdev_ops;
  432. /* Point-to-Point TUN Device */
  433. dev->hard_header_len = 0;
  434. dev->addr_len = 0;
  435. dev->mtu = 1500;
  436. /* Zero header length */
  437. dev->type = ARPHRD_NONE;
  438. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  439. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  440. break;
  441. case TUN_TAP_DEV:
  442. dev->netdev_ops = &tap_netdev_ops;
  443. /* Ethernet TAP Device */
  444. ether_setup(dev);
  445. random_ether_addr(dev->dev_addr);
  446. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  447. break;
  448. }
  449. }
  450. /* Character device part */
  451. /* Poll */
  452. static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
  453. {
  454. struct tun_file *tfile = file->private_data;
  455. struct tun_struct *tun = __tun_get(tfile);
  456. struct sock *sk;
  457. unsigned int mask = 0;
  458. if (!tun)
  459. return POLLERR;
  460. sk = tun->socket.sk;
  461. tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
  462. poll_wait(file, &tun->wq.wait, wait);
  463. if (!skb_queue_empty(&sk->sk_receive_queue))
  464. mask |= POLLIN | POLLRDNORM;
  465. if (sock_writeable(sk) ||
  466. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
  467. sock_writeable(sk)))
  468. mask |= POLLOUT | POLLWRNORM;
  469. if (tun->dev->reg_state != NETREG_REGISTERED)
  470. mask = POLLERR;
  471. tun_put(tun);
  472. return mask;
  473. }
  474. /* prepad is the amount to reserve at front. len is length after that.
  475. * linear is a hint as to how much to copy (usually headers). */
  476. static inline struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
  477. size_t prepad, size_t len,
  478. size_t linear, int noblock)
  479. {
  480. struct sock *sk = tun->socket.sk;
  481. struct sk_buff *skb;
  482. int err;
  483. sock_update_classid(sk);
  484. /* Under a page? Don't bother with paged skb. */
  485. if (prepad + len < PAGE_SIZE || !linear)
  486. linear = len;
  487. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  488. &err);
  489. if (!skb)
  490. return ERR_PTR(err);
  491. skb_reserve(skb, prepad);
  492. skb_put(skb, linear);
  493. skb->data_len = len - linear;
  494. skb->len += len - linear;
  495. return skb;
  496. }
  497. /* Get packet from user space buffer */
  498. static __inline__ ssize_t tun_get_user(struct tun_struct *tun,
  499. const struct iovec *iv, size_t count,
  500. int noblock)
  501. {
  502. struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
  503. struct sk_buff *skb;
  504. size_t len = count, align = 0;
  505. struct virtio_net_hdr gso = { 0 };
  506. int offset = 0;
  507. if (!(tun->flags & TUN_NO_PI)) {
  508. if ((len -= sizeof(pi)) > count)
  509. return -EINVAL;
  510. if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
  511. return -EFAULT;
  512. offset += sizeof(pi);
  513. }
  514. if (tun->flags & TUN_VNET_HDR) {
  515. if ((len -= tun->vnet_hdr_sz) > count)
  516. return -EINVAL;
  517. if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
  518. return -EFAULT;
  519. if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  520. gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
  521. gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
  522. if (gso.hdr_len > len)
  523. return -EINVAL;
  524. offset += tun->vnet_hdr_sz;
  525. }
  526. if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
  527. align = NET_IP_ALIGN;
  528. if (unlikely(len < ETH_HLEN ||
  529. (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
  530. return -EINVAL;
  531. }
  532. skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock);
  533. if (IS_ERR(skb)) {
  534. if (PTR_ERR(skb) != -EAGAIN)
  535. tun->dev->stats.rx_dropped++;
  536. return PTR_ERR(skb);
  537. }
  538. if (skb_copy_datagram_from_iovec(skb, 0, iv, offset, len)) {
  539. tun->dev->stats.rx_dropped++;
  540. kfree_skb(skb);
  541. return -EFAULT;
  542. }
  543. if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  544. if (!skb_partial_csum_set(skb, gso.csum_start,
  545. gso.csum_offset)) {
  546. tun->dev->stats.rx_frame_errors++;
  547. kfree_skb(skb);
  548. return -EINVAL;
  549. }
  550. }
  551. switch (tun->flags & TUN_TYPE_MASK) {
  552. case TUN_TUN_DEV:
  553. if (tun->flags & TUN_NO_PI) {
  554. switch (skb->data[0] & 0xf0) {
  555. case 0x40:
  556. pi.proto = htons(ETH_P_IP);
  557. break;
  558. case 0x60:
  559. pi.proto = htons(ETH_P_IPV6);
  560. break;
  561. default:
  562. tun->dev->stats.rx_dropped++;
  563. kfree_skb(skb);
  564. return -EINVAL;
  565. }
  566. }
  567. skb_reset_mac_header(skb);
  568. skb->protocol = pi.proto;
  569. skb->dev = tun->dev;
  570. break;
  571. case TUN_TAP_DEV:
  572. skb->protocol = eth_type_trans(skb, tun->dev);
  573. break;
  574. };
  575. if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  576. pr_debug("GSO!\n");
  577. switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  578. case VIRTIO_NET_HDR_GSO_TCPV4:
  579. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  580. break;
  581. case VIRTIO_NET_HDR_GSO_TCPV6:
  582. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  583. break;
  584. case VIRTIO_NET_HDR_GSO_UDP:
  585. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  586. break;
  587. default:
  588. tun->dev->stats.rx_frame_errors++;
  589. kfree_skb(skb);
  590. return -EINVAL;
  591. }
  592. if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  593. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  594. skb_shinfo(skb)->gso_size = gso.gso_size;
  595. if (skb_shinfo(skb)->gso_size == 0) {
  596. tun->dev->stats.rx_frame_errors++;
  597. kfree_skb(skb);
  598. return -EINVAL;
  599. }
  600. /* Header must be checked, and gso_segs computed. */
  601. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  602. skb_shinfo(skb)->gso_segs = 0;
  603. }
  604. netif_rx_ni(skb);
  605. tun->dev->stats.rx_packets++;
  606. tun->dev->stats.rx_bytes += len;
  607. return count;
  608. }
  609. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  610. unsigned long count, loff_t pos)
  611. {
  612. struct file *file = iocb->ki_filp;
  613. struct tun_struct *tun = tun_get(file);
  614. ssize_t result;
  615. if (!tun)
  616. return -EBADFD;
  617. tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
  618. result = tun_get_user(tun, iv, iov_length(iv, count),
  619. file->f_flags & O_NONBLOCK);
  620. tun_put(tun);
  621. return result;
  622. }
  623. /* Put packet to the user space buffer */
  624. static __inline__ ssize_t tun_put_user(struct tun_struct *tun,
  625. struct sk_buff *skb,
  626. const struct iovec *iv, int len)
  627. {
  628. struct tun_pi pi = { 0, skb->protocol };
  629. ssize_t total = 0;
  630. if (!(tun->flags & TUN_NO_PI)) {
  631. if ((len -= sizeof(pi)) < 0)
  632. return -EINVAL;
  633. if (len < skb->len) {
  634. /* Packet will be striped */
  635. pi.flags |= TUN_PKT_STRIP;
  636. }
  637. if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
  638. return -EFAULT;
  639. total += sizeof(pi);
  640. }
  641. if (tun->flags & TUN_VNET_HDR) {
  642. struct virtio_net_hdr gso = { 0 }; /* no info leak */
  643. if ((len -= tun->vnet_hdr_sz) < 0)
  644. return -EINVAL;
  645. if (skb_is_gso(skb)) {
  646. struct skb_shared_info *sinfo = skb_shinfo(skb);
  647. /* This is a hint as to how much should be linear. */
  648. gso.hdr_len = skb_headlen(skb);
  649. gso.gso_size = sinfo->gso_size;
  650. if (sinfo->gso_type & SKB_GSO_TCPV4)
  651. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  652. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  653. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  654. else if (sinfo->gso_type & SKB_GSO_UDP)
  655. gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  656. else {
  657. pr_err("unexpected GSO type: "
  658. "0x%x, gso_size %d, hdr_len %d\n",
  659. sinfo->gso_type, gso.gso_size,
  660. gso.hdr_len);
  661. print_hex_dump(KERN_ERR, "tun: ",
  662. DUMP_PREFIX_NONE,
  663. 16, 1, skb->head,
  664. min((int)gso.hdr_len, 64), true);
  665. WARN_ON_ONCE(1);
  666. return -EINVAL;
  667. }
  668. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  669. gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  670. } else
  671. gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  672. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  673. gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  674. gso.csum_start = skb_checksum_start_offset(skb);
  675. gso.csum_offset = skb->csum_offset;
  676. } /* else everything is zero */
  677. if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
  678. sizeof(gso))))
  679. return -EFAULT;
  680. total += tun->vnet_hdr_sz;
  681. }
  682. len = min_t(int, skb->len, len);
  683. skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
  684. total += skb->len;
  685. tun->dev->stats.tx_packets++;
  686. tun->dev->stats.tx_bytes += len;
  687. return total;
  688. }
  689. static ssize_t tun_do_read(struct tun_struct *tun,
  690. struct kiocb *iocb, const struct iovec *iv,
  691. ssize_t len, int noblock)
  692. {
  693. DECLARE_WAITQUEUE(wait, current);
  694. struct sk_buff *skb;
  695. ssize_t ret = 0;
  696. tun_debug(KERN_INFO, tun, "tun_chr_read\n");
  697. add_wait_queue(&tun->wq.wait, &wait);
  698. while (len) {
  699. current->state = TASK_INTERRUPTIBLE;
  700. /* Read frames from the queue */
  701. if (!(skb=skb_dequeue(&tun->socket.sk->sk_receive_queue))) {
  702. if (noblock) {
  703. ret = -EAGAIN;
  704. break;
  705. }
  706. if (signal_pending(current)) {
  707. ret = -ERESTARTSYS;
  708. break;
  709. }
  710. if (tun->dev->reg_state != NETREG_REGISTERED) {
  711. ret = -EIO;
  712. break;
  713. }
  714. /* Nothing to read, let's sleep */
  715. schedule();
  716. continue;
  717. }
  718. netif_wake_queue(tun->dev);
  719. ret = tun_put_user(tun, skb, iv, len);
  720. kfree_skb(skb);
  721. break;
  722. }
  723. current->state = TASK_RUNNING;
  724. remove_wait_queue(&tun->wq.wait, &wait);
  725. return ret;
  726. }
  727. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  728. unsigned long count, loff_t pos)
  729. {
  730. struct file *file = iocb->ki_filp;
  731. struct tun_file *tfile = file->private_data;
  732. struct tun_struct *tun = __tun_get(tfile);
  733. ssize_t len, ret;
  734. if (!tun)
  735. return -EBADFD;
  736. len = iov_length(iv, count);
  737. if (len < 0) {
  738. ret = -EINVAL;
  739. goto out;
  740. }
  741. ret = tun_do_read(tun, iocb, iv, len, file->f_flags & O_NONBLOCK);
  742. ret = min_t(ssize_t, ret, len);
  743. out:
  744. tun_put(tun);
  745. return ret;
  746. }
  747. static void tun_setup(struct net_device *dev)
  748. {
  749. struct tun_struct *tun = netdev_priv(dev);
  750. tun->owner = -1;
  751. tun->group = -1;
  752. dev->ethtool_ops = &tun_ethtool_ops;
  753. dev->destructor = tun_free_netdev;
  754. }
  755. /* Trivial set of netlink ops to allow deleting tun or tap
  756. * device with netlink.
  757. */
  758. static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
  759. {
  760. return -EINVAL;
  761. }
  762. static struct rtnl_link_ops tun_link_ops __read_mostly = {
  763. .kind = DRV_NAME,
  764. .priv_size = sizeof(struct tun_struct),
  765. .setup = tun_setup,
  766. .validate = tun_validate,
  767. };
  768. static void tun_sock_write_space(struct sock *sk)
  769. {
  770. struct tun_struct *tun;
  771. wait_queue_head_t *wqueue;
  772. if (!sock_writeable(sk))
  773. return;
  774. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  775. return;
  776. wqueue = sk_sleep(sk);
  777. if (wqueue && waitqueue_active(wqueue))
  778. wake_up_interruptible_sync_poll(wqueue, POLLOUT |
  779. POLLWRNORM | POLLWRBAND);
  780. tun = tun_sk(sk)->tun;
  781. kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
  782. }
  783. static void tun_sock_destruct(struct sock *sk)
  784. {
  785. free_netdev(tun_sk(sk)->tun->dev);
  786. }
  787. static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
  788. struct msghdr *m, size_t total_len)
  789. {
  790. struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
  791. return tun_get_user(tun, m->msg_iov, total_len,
  792. m->msg_flags & MSG_DONTWAIT);
  793. }
  794. static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
  795. struct msghdr *m, size_t total_len,
  796. int flags)
  797. {
  798. struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
  799. int ret;
  800. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  801. return -EINVAL;
  802. ret = tun_do_read(tun, iocb, m->msg_iov, total_len,
  803. flags & MSG_DONTWAIT);
  804. if (ret > total_len) {
  805. m->msg_flags |= MSG_TRUNC;
  806. ret = flags & MSG_TRUNC ? ret : total_len;
  807. }
  808. return ret;
  809. }
  810. /* Ops structure to mimic raw sockets with tun */
  811. static const struct proto_ops tun_socket_ops = {
  812. .sendmsg = tun_sendmsg,
  813. .recvmsg = tun_recvmsg,
  814. };
  815. static struct proto tun_proto = {
  816. .name = "tun",
  817. .owner = THIS_MODULE,
  818. .obj_size = sizeof(struct tun_sock),
  819. };
  820. static int tun_flags(struct tun_struct *tun)
  821. {
  822. int flags = 0;
  823. if (tun->flags & TUN_TUN_DEV)
  824. flags |= IFF_TUN;
  825. else
  826. flags |= IFF_TAP;
  827. if (tun->flags & TUN_NO_PI)
  828. flags |= IFF_NO_PI;
  829. if (tun->flags & TUN_ONE_QUEUE)
  830. flags |= IFF_ONE_QUEUE;
  831. if (tun->flags & TUN_VNET_HDR)
  832. flags |= IFF_VNET_HDR;
  833. return flags;
  834. }
  835. static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
  836. char *buf)
  837. {
  838. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  839. return sprintf(buf, "0x%x\n", tun_flags(tun));
  840. }
  841. static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
  842. char *buf)
  843. {
  844. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  845. return sprintf(buf, "%d\n", tun->owner);
  846. }
  847. static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
  848. char *buf)
  849. {
  850. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  851. return sprintf(buf, "%d\n", tun->group);
  852. }
  853. static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
  854. static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
  855. static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
  856. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  857. {
  858. struct sock *sk;
  859. struct tun_struct *tun;
  860. struct net_device *dev;
  861. int err;
  862. dev = __dev_get_by_name(net, ifr->ifr_name);
  863. if (dev) {
  864. const struct cred *cred = current_cred();
  865. if (ifr->ifr_flags & IFF_TUN_EXCL)
  866. return -EBUSY;
  867. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  868. tun = netdev_priv(dev);
  869. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  870. tun = netdev_priv(dev);
  871. else
  872. return -EINVAL;
  873. if (((tun->owner != -1 && cred->euid != tun->owner) ||
  874. (tun->group != -1 && !in_egroup_p(tun->group))) &&
  875. !capable(CAP_NET_ADMIN))
  876. return -EPERM;
  877. err = security_tun_dev_attach(tun->socket.sk);
  878. if (err < 0)
  879. return err;
  880. err = tun_attach(tun, file);
  881. if (err < 0)
  882. return err;
  883. }
  884. else {
  885. char *name;
  886. unsigned long flags = 0;
  887. if (!capable(CAP_NET_ADMIN))
  888. return -EPERM;
  889. err = security_tun_dev_create();
  890. if (err < 0)
  891. return err;
  892. /* Set dev type */
  893. if (ifr->ifr_flags & IFF_TUN) {
  894. /* TUN device */
  895. flags |= TUN_TUN_DEV;
  896. name = "tun%d";
  897. } else if (ifr->ifr_flags & IFF_TAP) {
  898. /* TAP device */
  899. flags |= TUN_TAP_DEV;
  900. name = "tap%d";
  901. } else
  902. return -EINVAL;
  903. if (*ifr->ifr_name)
  904. name = ifr->ifr_name;
  905. dev = alloc_netdev(sizeof(struct tun_struct), name,
  906. tun_setup);
  907. if (!dev)
  908. return -ENOMEM;
  909. dev_net_set(dev, net);
  910. dev->rtnl_link_ops = &tun_link_ops;
  911. tun = netdev_priv(dev);
  912. tun->dev = dev;
  913. tun->flags = flags;
  914. tun->txflt.count = 0;
  915. tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  916. err = -ENOMEM;
  917. sk = sk_alloc(net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
  918. if (!sk)
  919. goto err_free_dev;
  920. tun->socket.wq = &tun->wq;
  921. init_waitqueue_head(&tun->wq.wait);
  922. tun->socket.ops = &tun_socket_ops;
  923. sock_init_data(&tun->socket, sk);
  924. sk->sk_write_space = tun_sock_write_space;
  925. sk->sk_sndbuf = INT_MAX;
  926. tun_sk(sk)->tun = tun;
  927. security_tun_dev_post_create(sk);
  928. tun_net_init(dev);
  929. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
  930. TUN_USER_FEATURES;
  931. dev->features = dev->hw_features;
  932. err = register_netdevice(tun->dev);
  933. if (err < 0)
  934. goto err_free_sk;
  935. if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
  936. device_create_file(&tun->dev->dev, &dev_attr_owner) ||
  937. device_create_file(&tun->dev->dev, &dev_attr_group))
  938. pr_err("Failed to create tun sysfs files\n");
  939. sk->sk_destruct = tun_sock_destruct;
  940. err = tun_attach(tun, file);
  941. if (err < 0)
  942. goto failed;
  943. }
  944. tun_debug(KERN_INFO, tun, "tun_set_iff\n");
  945. if (ifr->ifr_flags & IFF_NO_PI)
  946. tun->flags |= TUN_NO_PI;
  947. else
  948. tun->flags &= ~TUN_NO_PI;
  949. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  950. tun->flags |= TUN_ONE_QUEUE;
  951. else
  952. tun->flags &= ~TUN_ONE_QUEUE;
  953. if (ifr->ifr_flags & IFF_VNET_HDR)
  954. tun->flags |= TUN_VNET_HDR;
  955. else
  956. tun->flags &= ~TUN_VNET_HDR;
  957. /* Make sure persistent devices do not get stuck in
  958. * xoff state.
  959. */
  960. if (netif_running(tun->dev))
  961. netif_wake_queue(tun->dev);
  962. strcpy(ifr->ifr_name, tun->dev->name);
  963. return 0;
  964. err_free_sk:
  965. sock_put(sk);
  966. err_free_dev:
  967. free_netdev(dev);
  968. failed:
  969. return err;
  970. }
  971. static int tun_get_iff(struct net *net, struct tun_struct *tun,
  972. struct ifreq *ifr)
  973. {
  974. tun_debug(KERN_INFO, tun, "tun_get_iff\n");
  975. strcpy(ifr->ifr_name, tun->dev->name);
  976. ifr->ifr_flags = tun_flags(tun);
  977. return 0;
  978. }
  979. /* This is like a cut-down ethtool ops, except done via tun fd so no
  980. * privs required. */
  981. static int set_offload(struct tun_struct *tun, unsigned long arg)
  982. {
  983. u32 features = 0;
  984. if (arg & TUN_F_CSUM) {
  985. features |= NETIF_F_HW_CSUM;
  986. arg &= ~TUN_F_CSUM;
  987. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  988. if (arg & TUN_F_TSO_ECN) {
  989. features |= NETIF_F_TSO_ECN;
  990. arg &= ~TUN_F_TSO_ECN;
  991. }
  992. if (arg & TUN_F_TSO4)
  993. features |= NETIF_F_TSO;
  994. if (arg & TUN_F_TSO6)
  995. features |= NETIF_F_TSO6;
  996. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  997. }
  998. if (arg & TUN_F_UFO) {
  999. features |= NETIF_F_UFO;
  1000. arg &= ~TUN_F_UFO;
  1001. }
  1002. }
  1003. /* This gives the user a way to test for new features in future by
  1004. * trying to set them. */
  1005. if (arg)
  1006. return -EINVAL;
  1007. tun->set_features = features;
  1008. netdev_update_features(tun->dev);
  1009. return 0;
  1010. }
  1011. static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  1012. unsigned long arg, int ifreq_len)
  1013. {
  1014. struct tun_file *tfile = file->private_data;
  1015. struct tun_struct *tun;
  1016. void __user* argp = (void __user*)arg;
  1017. struct sock_fprog fprog;
  1018. struct ifreq ifr;
  1019. int sndbuf;
  1020. int vnet_hdr_sz;
  1021. int ret;
  1022. if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
  1023. if (copy_from_user(&ifr, argp, ifreq_len))
  1024. return -EFAULT;
  1025. if (cmd == TUNGETFEATURES) {
  1026. /* Currently this just means: "what IFF flags are valid?".
  1027. * This is needed because we never checked for invalid flags on
  1028. * TUNSETIFF. */
  1029. return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
  1030. IFF_VNET_HDR,
  1031. (unsigned int __user*)argp);
  1032. }
  1033. rtnl_lock();
  1034. tun = __tun_get(tfile);
  1035. if (cmd == TUNSETIFF && !tun) {
  1036. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  1037. ret = tun_set_iff(tfile->net, file, &ifr);
  1038. if (ret)
  1039. goto unlock;
  1040. if (copy_to_user(argp, &ifr, ifreq_len))
  1041. ret = -EFAULT;
  1042. goto unlock;
  1043. }
  1044. ret = -EBADFD;
  1045. if (!tun)
  1046. goto unlock;
  1047. tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %d\n", cmd);
  1048. ret = 0;
  1049. switch (cmd) {
  1050. case TUNGETIFF:
  1051. ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
  1052. if (ret)
  1053. break;
  1054. if (copy_to_user(argp, &ifr, ifreq_len))
  1055. ret = -EFAULT;
  1056. break;
  1057. case TUNSETNOCSUM:
  1058. /* Disable/Enable checksum */
  1059. /* [unimplemented] */
  1060. tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
  1061. arg ? "disabled" : "enabled");
  1062. break;
  1063. case TUNSETPERSIST:
  1064. /* Disable/Enable persist mode */
  1065. if (arg)
  1066. tun->flags |= TUN_PERSIST;
  1067. else
  1068. tun->flags &= ~TUN_PERSIST;
  1069. tun_debug(KERN_INFO, tun, "persist %s\n",
  1070. arg ? "enabled" : "disabled");
  1071. break;
  1072. case TUNSETOWNER:
  1073. /* Set owner of the device */
  1074. tun->owner = (uid_t) arg;
  1075. tun_debug(KERN_INFO, tun, "owner set to %d\n", tun->owner);
  1076. break;
  1077. case TUNSETGROUP:
  1078. /* Set group of the device */
  1079. tun->group= (gid_t) arg;
  1080. tun_debug(KERN_INFO, tun, "group set to %d\n", tun->group);
  1081. break;
  1082. case TUNSETLINK:
  1083. /* Only allow setting the type when the interface is down */
  1084. if (tun->dev->flags & IFF_UP) {
  1085. tun_debug(KERN_INFO, tun,
  1086. "Linktype set failed because interface is up\n");
  1087. ret = -EBUSY;
  1088. } else {
  1089. tun->dev->type = (int) arg;
  1090. tun_debug(KERN_INFO, tun, "linktype set to %d\n",
  1091. tun->dev->type);
  1092. ret = 0;
  1093. }
  1094. break;
  1095. #ifdef TUN_DEBUG
  1096. case TUNSETDEBUG:
  1097. tun->debug = arg;
  1098. break;
  1099. #endif
  1100. case TUNSETOFFLOAD:
  1101. ret = set_offload(tun, arg);
  1102. break;
  1103. case TUNSETTXFILTER:
  1104. /* Can be set only for TAPs */
  1105. ret = -EINVAL;
  1106. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1107. break;
  1108. ret = update_filter(&tun->txflt, (void __user *)arg);
  1109. break;
  1110. case SIOCGIFHWADDR:
  1111. /* Get hw address */
  1112. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  1113. ifr.ifr_hwaddr.sa_family = tun->dev->type;
  1114. if (copy_to_user(argp, &ifr, ifreq_len))
  1115. ret = -EFAULT;
  1116. break;
  1117. case SIOCSIFHWADDR:
  1118. /* Set hw address */
  1119. tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
  1120. ifr.ifr_hwaddr.sa_data);
  1121. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  1122. break;
  1123. case TUNGETSNDBUF:
  1124. sndbuf = tun->socket.sk->sk_sndbuf;
  1125. if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
  1126. ret = -EFAULT;
  1127. break;
  1128. case TUNSETSNDBUF:
  1129. if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
  1130. ret = -EFAULT;
  1131. break;
  1132. }
  1133. tun->socket.sk->sk_sndbuf = sndbuf;
  1134. break;
  1135. case TUNGETVNETHDRSZ:
  1136. vnet_hdr_sz = tun->vnet_hdr_sz;
  1137. if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
  1138. ret = -EFAULT;
  1139. break;
  1140. case TUNSETVNETHDRSZ:
  1141. if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
  1142. ret = -EFAULT;
  1143. break;
  1144. }
  1145. if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
  1146. ret = -EINVAL;
  1147. break;
  1148. }
  1149. tun->vnet_hdr_sz = vnet_hdr_sz;
  1150. break;
  1151. case TUNATTACHFILTER:
  1152. /* Can be set only for TAPs */
  1153. ret = -EINVAL;
  1154. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1155. break;
  1156. ret = -EFAULT;
  1157. if (copy_from_user(&fprog, argp, sizeof(fprog)))
  1158. break;
  1159. ret = sk_attach_filter(&fprog, tun->socket.sk);
  1160. break;
  1161. case TUNDETACHFILTER:
  1162. /* Can be set only for TAPs */
  1163. ret = -EINVAL;
  1164. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1165. break;
  1166. ret = sk_detach_filter(tun->socket.sk);
  1167. break;
  1168. default:
  1169. ret = -EINVAL;
  1170. break;
  1171. }
  1172. unlock:
  1173. rtnl_unlock();
  1174. if (tun)
  1175. tun_put(tun);
  1176. return ret;
  1177. }
  1178. static long tun_chr_ioctl(struct file *file,
  1179. unsigned int cmd, unsigned long arg)
  1180. {
  1181. return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
  1182. }
  1183. #ifdef CONFIG_COMPAT
  1184. static long tun_chr_compat_ioctl(struct file *file,
  1185. unsigned int cmd, unsigned long arg)
  1186. {
  1187. switch (cmd) {
  1188. case TUNSETIFF:
  1189. case TUNGETIFF:
  1190. case TUNSETTXFILTER:
  1191. case TUNGETSNDBUF:
  1192. case TUNSETSNDBUF:
  1193. case SIOCGIFHWADDR:
  1194. case SIOCSIFHWADDR:
  1195. arg = (unsigned long)compat_ptr(arg);
  1196. break;
  1197. default:
  1198. arg = (compat_ulong_t)arg;
  1199. break;
  1200. }
  1201. /*
  1202. * compat_ifreq is shorter than ifreq, so we must not access beyond
  1203. * the end of that structure. All fields that are used in this
  1204. * driver are compatible though, we don't need to convert the
  1205. * contents.
  1206. */
  1207. return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
  1208. }
  1209. #endif /* CONFIG_COMPAT */
  1210. static int tun_chr_fasync(int fd, struct file *file, int on)
  1211. {
  1212. struct tun_struct *tun = tun_get(file);
  1213. int ret;
  1214. if (!tun)
  1215. return -EBADFD;
  1216. tun_debug(KERN_INFO, tun, "tun_chr_fasync %d\n", on);
  1217. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  1218. goto out;
  1219. if (on) {
  1220. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  1221. if (ret)
  1222. goto out;
  1223. tun->flags |= TUN_FASYNC;
  1224. } else
  1225. tun->flags &= ~TUN_FASYNC;
  1226. ret = 0;
  1227. out:
  1228. tun_put(tun);
  1229. return ret;
  1230. }
  1231. static int tun_chr_open(struct inode *inode, struct file * file)
  1232. {
  1233. struct tun_file *tfile;
  1234. DBG1(KERN_INFO, "tunX: tun_chr_open\n");
  1235. tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
  1236. if (!tfile)
  1237. return -ENOMEM;
  1238. atomic_set(&tfile->count, 0);
  1239. tfile->tun = NULL;
  1240. tfile->net = get_net(current->nsproxy->net_ns);
  1241. file->private_data = tfile;
  1242. return 0;
  1243. }
  1244. static int tun_chr_close(struct inode *inode, struct file *file)
  1245. {
  1246. struct tun_file *tfile = file->private_data;
  1247. struct tun_struct *tun;
  1248. tun = __tun_get(tfile);
  1249. if (tun) {
  1250. struct net_device *dev = tun->dev;
  1251. tun_debug(KERN_INFO, tun, "tun_chr_close\n");
  1252. __tun_detach(tun);
  1253. /* If desirable, unregister the netdevice. */
  1254. if (!(tun->flags & TUN_PERSIST)) {
  1255. rtnl_lock();
  1256. if (dev->reg_state == NETREG_REGISTERED)
  1257. unregister_netdevice(dev);
  1258. rtnl_unlock();
  1259. }
  1260. }
  1261. tun = tfile->tun;
  1262. if (tun)
  1263. sock_put(tun->socket.sk);
  1264. put_net(tfile->net);
  1265. kfree(tfile);
  1266. return 0;
  1267. }
  1268. static const struct file_operations tun_fops = {
  1269. .owner = THIS_MODULE,
  1270. .llseek = no_llseek,
  1271. .read = do_sync_read,
  1272. .aio_read = tun_chr_aio_read,
  1273. .write = do_sync_write,
  1274. .aio_write = tun_chr_aio_write,
  1275. .poll = tun_chr_poll,
  1276. .unlocked_ioctl = tun_chr_ioctl,
  1277. #ifdef CONFIG_COMPAT
  1278. .compat_ioctl = tun_chr_compat_ioctl,
  1279. #endif
  1280. .open = tun_chr_open,
  1281. .release = tun_chr_close,
  1282. .fasync = tun_chr_fasync
  1283. };
  1284. static struct miscdevice tun_miscdev = {
  1285. .minor = TUN_MINOR,
  1286. .name = "tun",
  1287. .nodename = "net/tun",
  1288. .fops = &tun_fops,
  1289. };
  1290. /* ethtool interface */
  1291. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1292. {
  1293. cmd->supported = 0;
  1294. cmd->advertising = 0;
  1295. ethtool_cmd_speed_set(cmd, SPEED_10);
  1296. cmd->duplex = DUPLEX_FULL;
  1297. cmd->port = PORT_TP;
  1298. cmd->phy_address = 0;
  1299. cmd->transceiver = XCVR_INTERNAL;
  1300. cmd->autoneg = AUTONEG_DISABLE;
  1301. cmd->maxtxpkt = 0;
  1302. cmd->maxrxpkt = 0;
  1303. return 0;
  1304. }
  1305. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1306. {
  1307. struct tun_struct *tun = netdev_priv(dev);
  1308. strcpy(info->driver, DRV_NAME);
  1309. strcpy(info->version, DRV_VERSION);
  1310. strcpy(info->fw_version, "N/A");
  1311. switch (tun->flags & TUN_TYPE_MASK) {
  1312. case TUN_TUN_DEV:
  1313. strcpy(info->bus_info, "tun");
  1314. break;
  1315. case TUN_TAP_DEV:
  1316. strcpy(info->bus_info, "tap");
  1317. break;
  1318. }
  1319. }
  1320. static u32 tun_get_msglevel(struct net_device *dev)
  1321. {
  1322. #ifdef TUN_DEBUG
  1323. struct tun_struct *tun = netdev_priv(dev);
  1324. return tun->debug;
  1325. #else
  1326. return -EOPNOTSUPP;
  1327. #endif
  1328. }
  1329. static void tun_set_msglevel(struct net_device *dev, u32 value)
  1330. {
  1331. #ifdef TUN_DEBUG
  1332. struct tun_struct *tun = netdev_priv(dev);
  1333. tun->debug = value;
  1334. #endif
  1335. }
  1336. static const struct ethtool_ops tun_ethtool_ops = {
  1337. .get_settings = tun_get_settings,
  1338. .get_drvinfo = tun_get_drvinfo,
  1339. .get_msglevel = tun_get_msglevel,
  1340. .set_msglevel = tun_set_msglevel,
  1341. .get_link = ethtool_op_get_link,
  1342. };
  1343. static int __init tun_init(void)
  1344. {
  1345. int ret = 0;
  1346. pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1347. pr_info("%s\n", DRV_COPYRIGHT);
  1348. ret = rtnl_link_register(&tun_link_ops);
  1349. if (ret) {
  1350. pr_err("Can't register link_ops\n");
  1351. goto err_linkops;
  1352. }
  1353. ret = misc_register(&tun_miscdev);
  1354. if (ret) {
  1355. pr_err("Can't register misc device %d\n", TUN_MINOR);
  1356. goto err_misc;
  1357. }
  1358. return 0;
  1359. err_misc:
  1360. rtnl_link_unregister(&tun_link_ops);
  1361. err_linkops:
  1362. return ret;
  1363. }
  1364. static void tun_cleanup(void)
  1365. {
  1366. misc_deregister(&tun_miscdev);
  1367. rtnl_link_unregister(&tun_link_ops);
  1368. }
  1369. /* Get an underlying socket object from tun file. Returns error unless file is
  1370. * attached to a device. The returned object works like a packet socket, it
  1371. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1372. * holding a reference to the file for as long as the socket is in use. */
  1373. struct socket *tun_get_socket(struct file *file)
  1374. {
  1375. struct tun_struct *tun;
  1376. if (file->f_op != &tun_fops)
  1377. return ERR_PTR(-EINVAL);
  1378. tun = tun_get(file);
  1379. if (!tun)
  1380. return ERR_PTR(-EBADFD);
  1381. tun_put(tun);
  1382. return &tun->socket;
  1383. }
  1384. EXPORT_SYMBOL_GPL(tun_get_socket);
  1385. module_init(tun_init);
  1386. module_exit(tun_cleanup);
  1387. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1388. MODULE_AUTHOR(DRV_COPYRIGHT);
  1389. MODULE_LICENSE("GPL");
  1390. MODULE_ALIAS_MISCDEV(TUN_MINOR);
  1391. MODULE_ALIAS("devname:net/tun");