tun.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
  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 FLT_EXACT_COUNT 8
  95. struct tap_filter {
  96. unsigned int count; /* Number of addrs. Zero means disabled */
  97. u32 mask[2]; /* Mask of the hashed addrs */
  98. unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
  99. };
  100. struct tun_file {
  101. atomic_t count;
  102. struct tun_struct *tun;
  103. struct net *net;
  104. };
  105. struct tun_sock;
  106. struct tun_struct {
  107. struct tun_file *tfile;
  108. unsigned int flags;
  109. uid_t owner;
  110. gid_t group;
  111. struct net_device *dev;
  112. netdev_features_t set_features;
  113. #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
  114. NETIF_F_TSO6|NETIF_F_UFO)
  115. struct fasync_struct *fasync;
  116. struct tap_filter txflt;
  117. struct socket socket;
  118. struct socket_wq wq;
  119. int vnet_hdr_sz;
  120. #ifdef TUN_DEBUG
  121. int debug;
  122. #endif
  123. };
  124. struct tun_sock {
  125. struct sock sk;
  126. struct tun_struct *tun;
  127. };
  128. static inline struct tun_sock *tun_sk(struct sock *sk)
  129. {
  130. return container_of(sk, struct tun_sock, sk);
  131. }
  132. static int tun_attach(struct tun_struct *tun, struct file *file)
  133. {
  134. struct tun_file *tfile = file->private_data;
  135. int err;
  136. ASSERT_RTNL();
  137. netif_tx_lock_bh(tun->dev);
  138. err = -EINVAL;
  139. if (tfile->tun)
  140. goto out;
  141. err = -EBUSY;
  142. if (tun->tfile)
  143. goto out;
  144. err = 0;
  145. tfile->tun = tun;
  146. tun->tfile = tfile;
  147. tun->socket.file = file;
  148. netif_carrier_on(tun->dev);
  149. dev_hold(tun->dev);
  150. sock_hold(tun->socket.sk);
  151. atomic_inc(&tfile->count);
  152. out:
  153. netif_tx_unlock_bh(tun->dev);
  154. return err;
  155. }
  156. static void __tun_detach(struct tun_struct *tun)
  157. {
  158. /* Detach from net device */
  159. netif_tx_lock_bh(tun->dev);
  160. netif_carrier_off(tun->dev);
  161. tun->tfile = NULL;
  162. tun->socket.file = 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. /* Get packet from user space buffer */
  502. static ssize_t tun_get_user(struct tun_struct *tun,
  503. const struct iovec *iv, size_t count,
  504. int noblock)
  505. {
  506. struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
  507. struct sk_buff *skb;
  508. size_t len = count, align = NET_SKB_PAD;
  509. struct virtio_net_hdr gso = { 0 };
  510. int offset = 0;
  511. if (!(tun->flags & TUN_NO_PI)) {
  512. if ((len -= sizeof(pi)) > count)
  513. return -EINVAL;
  514. if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
  515. return -EFAULT;
  516. offset += sizeof(pi);
  517. }
  518. if (tun->flags & TUN_VNET_HDR) {
  519. if ((len -= tun->vnet_hdr_sz) > count)
  520. return -EINVAL;
  521. if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
  522. return -EFAULT;
  523. if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  524. gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
  525. gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
  526. if (gso.hdr_len > len)
  527. return -EINVAL;
  528. offset += tun->vnet_hdr_sz;
  529. }
  530. if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
  531. align += NET_IP_ALIGN;
  532. if (unlikely(len < ETH_HLEN ||
  533. (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
  534. return -EINVAL;
  535. }
  536. skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock);
  537. if (IS_ERR(skb)) {
  538. if (PTR_ERR(skb) != -EAGAIN)
  539. tun->dev->stats.rx_dropped++;
  540. return PTR_ERR(skb);
  541. }
  542. if (skb_copy_datagram_from_iovec(skb, 0, iv, offset, len)) {
  543. tun->dev->stats.rx_dropped++;
  544. kfree_skb(skb);
  545. return -EFAULT;
  546. }
  547. if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  548. if (!skb_partial_csum_set(skb, gso.csum_start,
  549. gso.csum_offset)) {
  550. tun->dev->stats.rx_frame_errors++;
  551. kfree_skb(skb);
  552. return -EINVAL;
  553. }
  554. }
  555. switch (tun->flags & TUN_TYPE_MASK) {
  556. case TUN_TUN_DEV:
  557. if (tun->flags & TUN_NO_PI) {
  558. switch (skb->data[0] & 0xf0) {
  559. case 0x40:
  560. pi.proto = htons(ETH_P_IP);
  561. break;
  562. case 0x60:
  563. pi.proto = htons(ETH_P_IPV6);
  564. break;
  565. default:
  566. tun->dev->stats.rx_dropped++;
  567. kfree_skb(skb);
  568. return -EINVAL;
  569. }
  570. }
  571. skb_reset_mac_header(skb);
  572. skb->protocol = pi.proto;
  573. skb->dev = tun->dev;
  574. break;
  575. case TUN_TAP_DEV:
  576. skb->protocol = eth_type_trans(skb, tun->dev);
  577. break;
  578. }
  579. if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  580. pr_debug("GSO!\n");
  581. switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  582. case VIRTIO_NET_HDR_GSO_TCPV4:
  583. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  584. break;
  585. case VIRTIO_NET_HDR_GSO_TCPV6:
  586. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  587. break;
  588. case VIRTIO_NET_HDR_GSO_UDP:
  589. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  590. break;
  591. default:
  592. tun->dev->stats.rx_frame_errors++;
  593. kfree_skb(skb);
  594. return -EINVAL;
  595. }
  596. if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  597. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  598. skb_shinfo(skb)->gso_size = gso.gso_size;
  599. if (skb_shinfo(skb)->gso_size == 0) {
  600. tun->dev->stats.rx_frame_errors++;
  601. kfree_skb(skb);
  602. return -EINVAL;
  603. }
  604. /* Header must be checked, and gso_segs computed. */
  605. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  606. skb_shinfo(skb)->gso_segs = 0;
  607. }
  608. netif_rx_ni(skb);
  609. tun->dev->stats.rx_packets++;
  610. tun->dev->stats.rx_bytes += len;
  611. return count;
  612. }
  613. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  614. unsigned long count, loff_t pos)
  615. {
  616. struct file *file = iocb->ki_filp;
  617. struct tun_struct *tun = tun_get(file);
  618. ssize_t result;
  619. if (!tun)
  620. return -EBADFD;
  621. tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
  622. result = tun_get_user(tun, iv, iov_length(iv, count),
  623. file->f_flags & O_NONBLOCK);
  624. tun_put(tun);
  625. return result;
  626. }
  627. /* Put packet to the user space buffer */
  628. static ssize_t tun_put_user(struct tun_struct *tun,
  629. struct sk_buff *skb,
  630. const struct iovec *iv, int len)
  631. {
  632. struct tun_pi pi = { 0, skb->protocol };
  633. ssize_t total = 0;
  634. if (!(tun->flags & TUN_NO_PI)) {
  635. if ((len -= sizeof(pi)) < 0)
  636. return -EINVAL;
  637. if (len < skb->len) {
  638. /* Packet will be striped */
  639. pi.flags |= TUN_PKT_STRIP;
  640. }
  641. if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
  642. return -EFAULT;
  643. total += sizeof(pi);
  644. }
  645. if (tun->flags & TUN_VNET_HDR) {
  646. struct virtio_net_hdr gso = { 0 }; /* no info leak */
  647. if ((len -= tun->vnet_hdr_sz) < 0)
  648. return -EINVAL;
  649. if (skb_is_gso(skb)) {
  650. struct skb_shared_info *sinfo = skb_shinfo(skb);
  651. /* This is a hint as to how much should be linear. */
  652. gso.hdr_len = skb_headlen(skb);
  653. gso.gso_size = sinfo->gso_size;
  654. if (sinfo->gso_type & SKB_GSO_TCPV4)
  655. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  656. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  657. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  658. else if (sinfo->gso_type & SKB_GSO_UDP)
  659. gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  660. else {
  661. pr_err("unexpected GSO type: "
  662. "0x%x, gso_size %d, hdr_len %d\n",
  663. sinfo->gso_type, gso.gso_size,
  664. gso.hdr_len);
  665. print_hex_dump(KERN_ERR, "tun: ",
  666. DUMP_PREFIX_NONE,
  667. 16, 1, skb->head,
  668. min((int)gso.hdr_len, 64), true);
  669. WARN_ON_ONCE(1);
  670. return -EINVAL;
  671. }
  672. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  673. gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  674. } else
  675. gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  676. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  677. gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  678. gso.csum_start = skb_checksum_start_offset(skb);
  679. gso.csum_offset = skb->csum_offset;
  680. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  681. gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
  682. } /* else everything is zero */
  683. if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
  684. sizeof(gso))))
  685. return -EFAULT;
  686. total += tun->vnet_hdr_sz;
  687. }
  688. len = min_t(int, skb->len, len);
  689. skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
  690. total += skb->len;
  691. tun->dev->stats.tx_packets++;
  692. tun->dev->stats.tx_bytes += len;
  693. return total;
  694. }
  695. static ssize_t tun_do_read(struct tun_struct *tun,
  696. struct kiocb *iocb, const struct iovec *iv,
  697. ssize_t len, int noblock)
  698. {
  699. DECLARE_WAITQUEUE(wait, current);
  700. struct sk_buff *skb;
  701. ssize_t ret = 0;
  702. tun_debug(KERN_INFO, tun, "tun_chr_read\n");
  703. if (unlikely(!noblock))
  704. add_wait_queue(&tun->wq.wait, &wait);
  705. while (len) {
  706. current->state = TASK_INTERRUPTIBLE;
  707. /* Read frames from the queue */
  708. if (!(skb=skb_dequeue(&tun->socket.sk->sk_receive_queue))) {
  709. if (noblock) {
  710. ret = -EAGAIN;
  711. break;
  712. }
  713. if (signal_pending(current)) {
  714. ret = -ERESTARTSYS;
  715. break;
  716. }
  717. if (tun->dev->reg_state != NETREG_REGISTERED) {
  718. ret = -EIO;
  719. break;
  720. }
  721. /* Nothing to read, let's sleep */
  722. schedule();
  723. continue;
  724. }
  725. netif_wake_queue(tun->dev);
  726. ret = tun_put_user(tun, skb, iv, len);
  727. kfree_skb(skb);
  728. break;
  729. }
  730. current->state = TASK_RUNNING;
  731. if (unlikely(!noblock))
  732. remove_wait_queue(&tun->wq.wait, &wait);
  733. return ret;
  734. }
  735. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  736. unsigned long count, loff_t pos)
  737. {
  738. struct file *file = iocb->ki_filp;
  739. struct tun_file *tfile = file->private_data;
  740. struct tun_struct *tun = __tun_get(tfile);
  741. ssize_t len, ret;
  742. if (!tun)
  743. return -EBADFD;
  744. len = iov_length(iv, count);
  745. if (len < 0) {
  746. ret = -EINVAL;
  747. goto out;
  748. }
  749. ret = tun_do_read(tun, iocb, iv, len, file->f_flags & O_NONBLOCK);
  750. ret = min_t(ssize_t, ret, len);
  751. out:
  752. tun_put(tun);
  753. return ret;
  754. }
  755. static void tun_setup(struct net_device *dev)
  756. {
  757. struct tun_struct *tun = netdev_priv(dev);
  758. tun->owner = -1;
  759. tun->group = -1;
  760. dev->ethtool_ops = &tun_ethtool_ops;
  761. dev->destructor = tun_free_netdev;
  762. }
  763. /* Trivial set of netlink ops to allow deleting tun or tap
  764. * device with netlink.
  765. */
  766. static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
  767. {
  768. return -EINVAL;
  769. }
  770. static struct rtnl_link_ops tun_link_ops __read_mostly = {
  771. .kind = DRV_NAME,
  772. .priv_size = sizeof(struct tun_struct),
  773. .setup = tun_setup,
  774. .validate = tun_validate,
  775. };
  776. static void tun_sock_write_space(struct sock *sk)
  777. {
  778. struct tun_struct *tun;
  779. wait_queue_head_t *wqueue;
  780. if (!sock_writeable(sk))
  781. return;
  782. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  783. return;
  784. wqueue = sk_sleep(sk);
  785. if (wqueue && waitqueue_active(wqueue))
  786. wake_up_interruptible_sync_poll(wqueue, POLLOUT |
  787. POLLWRNORM | POLLWRBAND);
  788. tun = tun_sk(sk)->tun;
  789. kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
  790. }
  791. static void tun_sock_destruct(struct sock *sk)
  792. {
  793. free_netdev(tun_sk(sk)->tun->dev);
  794. }
  795. static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
  796. struct msghdr *m, size_t total_len)
  797. {
  798. struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
  799. return tun_get_user(tun, m->msg_iov, total_len,
  800. m->msg_flags & MSG_DONTWAIT);
  801. }
  802. static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
  803. struct msghdr *m, size_t total_len,
  804. int flags)
  805. {
  806. struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
  807. int ret;
  808. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  809. return -EINVAL;
  810. ret = tun_do_read(tun, iocb, m->msg_iov, total_len,
  811. flags & MSG_DONTWAIT);
  812. if (ret > total_len) {
  813. m->msg_flags |= MSG_TRUNC;
  814. ret = flags & MSG_TRUNC ? ret : total_len;
  815. }
  816. return ret;
  817. }
  818. static int tun_release(struct socket *sock)
  819. {
  820. if (sock->sk)
  821. sock_put(sock->sk);
  822. return 0;
  823. }
  824. /* Ops structure to mimic raw sockets with tun */
  825. static const struct proto_ops tun_socket_ops = {
  826. .sendmsg = tun_sendmsg,
  827. .recvmsg = tun_recvmsg,
  828. .release = tun_release,
  829. };
  830. static struct proto tun_proto = {
  831. .name = "tun",
  832. .owner = THIS_MODULE,
  833. .obj_size = sizeof(struct tun_sock),
  834. };
  835. static int tun_flags(struct tun_struct *tun)
  836. {
  837. int flags = 0;
  838. if (tun->flags & TUN_TUN_DEV)
  839. flags |= IFF_TUN;
  840. else
  841. flags |= IFF_TAP;
  842. if (tun->flags & TUN_NO_PI)
  843. flags |= IFF_NO_PI;
  844. if (tun->flags & TUN_ONE_QUEUE)
  845. flags |= IFF_ONE_QUEUE;
  846. if (tun->flags & TUN_VNET_HDR)
  847. flags |= IFF_VNET_HDR;
  848. return flags;
  849. }
  850. static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
  851. char *buf)
  852. {
  853. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  854. return sprintf(buf, "0x%x\n", tun_flags(tun));
  855. }
  856. static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
  857. char *buf)
  858. {
  859. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  860. return sprintf(buf, "%d\n", tun->owner);
  861. }
  862. static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
  863. char *buf)
  864. {
  865. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  866. return sprintf(buf, "%d\n", tun->group);
  867. }
  868. static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
  869. static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
  870. static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
  871. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  872. {
  873. struct sock *sk;
  874. struct tun_struct *tun;
  875. struct net_device *dev;
  876. int err;
  877. dev = __dev_get_by_name(net, ifr->ifr_name);
  878. if (dev) {
  879. const struct cred *cred = current_cred();
  880. if (ifr->ifr_flags & IFF_TUN_EXCL)
  881. return -EBUSY;
  882. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  883. tun = netdev_priv(dev);
  884. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  885. tun = netdev_priv(dev);
  886. else
  887. return -EINVAL;
  888. if (((tun->owner != -1 && cred->euid != tun->owner) ||
  889. (tun->group != -1 && !in_egroup_p(tun->group))) &&
  890. !capable(CAP_NET_ADMIN))
  891. return -EPERM;
  892. err = security_tun_dev_attach(tun->socket.sk);
  893. if (err < 0)
  894. return err;
  895. err = tun_attach(tun, file);
  896. if (err < 0)
  897. return err;
  898. }
  899. else {
  900. char *name;
  901. unsigned long flags = 0;
  902. if (!capable(CAP_NET_ADMIN))
  903. return -EPERM;
  904. err = security_tun_dev_create();
  905. if (err < 0)
  906. return err;
  907. /* Set dev type */
  908. if (ifr->ifr_flags & IFF_TUN) {
  909. /* TUN device */
  910. flags |= TUN_TUN_DEV;
  911. name = "tun%d";
  912. } else if (ifr->ifr_flags & IFF_TAP) {
  913. /* TAP device */
  914. flags |= TUN_TAP_DEV;
  915. name = "tap%d";
  916. } else
  917. return -EINVAL;
  918. if (*ifr->ifr_name)
  919. name = ifr->ifr_name;
  920. dev = alloc_netdev(sizeof(struct tun_struct), name,
  921. tun_setup);
  922. if (!dev)
  923. return -ENOMEM;
  924. dev_net_set(dev, net);
  925. dev->rtnl_link_ops = &tun_link_ops;
  926. tun = netdev_priv(dev);
  927. tun->dev = dev;
  928. tun->flags = flags;
  929. tun->txflt.count = 0;
  930. tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  931. set_bit(SOCK_EXTERNALLY_ALLOCATED, &tun->socket.flags);
  932. err = -ENOMEM;
  933. sk = sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
  934. if (!sk)
  935. goto err_free_dev;
  936. sk_change_net(sk, net);
  937. tun->socket.wq = &tun->wq;
  938. init_waitqueue_head(&tun->wq.wait);
  939. tun->socket.ops = &tun_socket_ops;
  940. sock_init_data(&tun->socket, sk);
  941. sk->sk_write_space = tun_sock_write_space;
  942. sk->sk_sndbuf = INT_MAX;
  943. tun_sk(sk)->tun = tun;
  944. security_tun_dev_post_create(sk);
  945. tun_net_init(dev);
  946. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
  947. TUN_USER_FEATURES;
  948. dev->features = dev->hw_features;
  949. err = register_netdevice(tun->dev);
  950. if (err < 0)
  951. goto err_free_sk;
  952. if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
  953. device_create_file(&tun->dev->dev, &dev_attr_owner) ||
  954. device_create_file(&tun->dev->dev, &dev_attr_group))
  955. pr_err("Failed to create tun sysfs files\n");
  956. sk->sk_destruct = tun_sock_destruct;
  957. err = tun_attach(tun, file);
  958. if (err < 0)
  959. goto failed;
  960. }
  961. tun_debug(KERN_INFO, tun, "tun_set_iff\n");
  962. if (ifr->ifr_flags & IFF_NO_PI)
  963. tun->flags |= TUN_NO_PI;
  964. else
  965. tun->flags &= ~TUN_NO_PI;
  966. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  967. tun->flags |= TUN_ONE_QUEUE;
  968. else
  969. tun->flags &= ~TUN_ONE_QUEUE;
  970. if (ifr->ifr_flags & IFF_VNET_HDR)
  971. tun->flags |= TUN_VNET_HDR;
  972. else
  973. tun->flags &= ~TUN_VNET_HDR;
  974. /* Make sure persistent devices do not get stuck in
  975. * xoff state.
  976. */
  977. if (netif_running(tun->dev))
  978. netif_wake_queue(tun->dev);
  979. strcpy(ifr->ifr_name, tun->dev->name);
  980. return 0;
  981. err_free_sk:
  982. tun_free_netdev(dev);
  983. err_free_dev:
  984. free_netdev(dev);
  985. failed:
  986. return err;
  987. }
  988. static int tun_get_iff(struct net *net, struct tun_struct *tun,
  989. struct ifreq *ifr)
  990. {
  991. tun_debug(KERN_INFO, tun, "tun_get_iff\n");
  992. strcpy(ifr->ifr_name, tun->dev->name);
  993. ifr->ifr_flags = tun_flags(tun);
  994. return 0;
  995. }
  996. /* This is like a cut-down ethtool ops, except done via tun fd so no
  997. * privs required. */
  998. static int set_offload(struct tun_struct *tun, unsigned long arg)
  999. {
  1000. netdev_features_t features = 0;
  1001. if (arg & TUN_F_CSUM) {
  1002. features |= NETIF_F_HW_CSUM;
  1003. arg &= ~TUN_F_CSUM;
  1004. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  1005. if (arg & TUN_F_TSO_ECN) {
  1006. features |= NETIF_F_TSO_ECN;
  1007. arg &= ~TUN_F_TSO_ECN;
  1008. }
  1009. if (arg & TUN_F_TSO4)
  1010. features |= NETIF_F_TSO;
  1011. if (arg & TUN_F_TSO6)
  1012. features |= NETIF_F_TSO6;
  1013. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  1014. }
  1015. if (arg & TUN_F_UFO) {
  1016. features |= NETIF_F_UFO;
  1017. arg &= ~TUN_F_UFO;
  1018. }
  1019. }
  1020. /* This gives the user a way to test for new features in future by
  1021. * trying to set them. */
  1022. if (arg)
  1023. return -EINVAL;
  1024. tun->set_features = features;
  1025. netdev_update_features(tun->dev);
  1026. return 0;
  1027. }
  1028. static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  1029. unsigned long arg, int ifreq_len)
  1030. {
  1031. struct tun_file *tfile = file->private_data;
  1032. struct tun_struct *tun;
  1033. void __user* argp = (void __user*)arg;
  1034. struct sock_fprog fprog;
  1035. struct ifreq ifr;
  1036. int sndbuf;
  1037. int vnet_hdr_sz;
  1038. int ret;
  1039. if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
  1040. if (copy_from_user(&ifr, argp, ifreq_len))
  1041. return -EFAULT;
  1042. if (cmd == TUNGETFEATURES) {
  1043. /* Currently this just means: "what IFF flags are valid?".
  1044. * This is needed because we never checked for invalid flags on
  1045. * TUNSETIFF. */
  1046. return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
  1047. IFF_VNET_HDR,
  1048. (unsigned int __user*)argp);
  1049. }
  1050. rtnl_lock();
  1051. tun = __tun_get(tfile);
  1052. if (cmd == TUNSETIFF && !tun) {
  1053. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  1054. ret = tun_set_iff(tfile->net, file, &ifr);
  1055. if (ret)
  1056. goto unlock;
  1057. if (copy_to_user(argp, &ifr, ifreq_len))
  1058. ret = -EFAULT;
  1059. goto unlock;
  1060. }
  1061. ret = -EBADFD;
  1062. if (!tun)
  1063. goto unlock;
  1064. tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %d\n", cmd);
  1065. ret = 0;
  1066. switch (cmd) {
  1067. case TUNGETIFF:
  1068. ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
  1069. if (ret)
  1070. break;
  1071. if (copy_to_user(argp, &ifr, ifreq_len))
  1072. ret = -EFAULT;
  1073. break;
  1074. case TUNSETNOCSUM:
  1075. /* Disable/Enable checksum */
  1076. /* [unimplemented] */
  1077. tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
  1078. arg ? "disabled" : "enabled");
  1079. break;
  1080. case TUNSETPERSIST:
  1081. /* Disable/Enable persist mode */
  1082. if (arg)
  1083. tun->flags |= TUN_PERSIST;
  1084. else
  1085. tun->flags &= ~TUN_PERSIST;
  1086. tun_debug(KERN_INFO, tun, "persist %s\n",
  1087. arg ? "enabled" : "disabled");
  1088. break;
  1089. case TUNSETOWNER:
  1090. /* Set owner of the device */
  1091. tun->owner = (uid_t) arg;
  1092. tun_debug(KERN_INFO, tun, "owner set to %d\n", tun->owner);
  1093. break;
  1094. case TUNSETGROUP:
  1095. /* Set group of the device */
  1096. tun->group= (gid_t) arg;
  1097. tun_debug(KERN_INFO, tun, "group set to %d\n", tun->group);
  1098. break;
  1099. case TUNSETLINK:
  1100. /* Only allow setting the type when the interface is down */
  1101. if (tun->dev->flags & IFF_UP) {
  1102. tun_debug(KERN_INFO, tun,
  1103. "Linktype set failed because interface is up\n");
  1104. ret = -EBUSY;
  1105. } else {
  1106. tun->dev->type = (int) arg;
  1107. tun_debug(KERN_INFO, tun, "linktype set to %d\n",
  1108. tun->dev->type);
  1109. ret = 0;
  1110. }
  1111. break;
  1112. #ifdef TUN_DEBUG
  1113. case TUNSETDEBUG:
  1114. tun->debug = arg;
  1115. break;
  1116. #endif
  1117. case TUNSETOFFLOAD:
  1118. ret = set_offload(tun, arg);
  1119. break;
  1120. case TUNSETTXFILTER:
  1121. /* Can be set only for TAPs */
  1122. ret = -EINVAL;
  1123. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1124. break;
  1125. ret = update_filter(&tun->txflt, (void __user *)arg);
  1126. break;
  1127. case SIOCGIFHWADDR:
  1128. /* Get hw address */
  1129. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  1130. ifr.ifr_hwaddr.sa_family = tun->dev->type;
  1131. if (copy_to_user(argp, &ifr, ifreq_len))
  1132. ret = -EFAULT;
  1133. break;
  1134. case SIOCSIFHWADDR:
  1135. /* Set hw address */
  1136. tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
  1137. ifr.ifr_hwaddr.sa_data);
  1138. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  1139. break;
  1140. case TUNGETSNDBUF:
  1141. sndbuf = tun->socket.sk->sk_sndbuf;
  1142. if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
  1143. ret = -EFAULT;
  1144. break;
  1145. case TUNSETSNDBUF:
  1146. if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
  1147. ret = -EFAULT;
  1148. break;
  1149. }
  1150. tun->socket.sk->sk_sndbuf = sndbuf;
  1151. break;
  1152. case TUNGETVNETHDRSZ:
  1153. vnet_hdr_sz = tun->vnet_hdr_sz;
  1154. if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
  1155. ret = -EFAULT;
  1156. break;
  1157. case TUNSETVNETHDRSZ:
  1158. if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
  1159. ret = -EFAULT;
  1160. break;
  1161. }
  1162. if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
  1163. ret = -EINVAL;
  1164. break;
  1165. }
  1166. tun->vnet_hdr_sz = vnet_hdr_sz;
  1167. break;
  1168. case TUNATTACHFILTER:
  1169. /* Can be set only for TAPs */
  1170. ret = -EINVAL;
  1171. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1172. break;
  1173. ret = -EFAULT;
  1174. if (copy_from_user(&fprog, argp, sizeof(fprog)))
  1175. break;
  1176. ret = sk_attach_filter(&fprog, tun->socket.sk);
  1177. break;
  1178. case TUNDETACHFILTER:
  1179. /* Can be set only for TAPs */
  1180. ret = -EINVAL;
  1181. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1182. break;
  1183. ret = sk_detach_filter(tun->socket.sk);
  1184. break;
  1185. default:
  1186. ret = -EINVAL;
  1187. break;
  1188. }
  1189. unlock:
  1190. rtnl_unlock();
  1191. if (tun)
  1192. tun_put(tun);
  1193. return ret;
  1194. }
  1195. static long tun_chr_ioctl(struct file *file,
  1196. unsigned int cmd, unsigned long arg)
  1197. {
  1198. return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
  1199. }
  1200. #ifdef CONFIG_COMPAT
  1201. static long tun_chr_compat_ioctl(struct file *file,
  1202. unsigned int cmd, unsigned long arg)
  1203. {
  1204. switch (cmd) {
  1205. case TUNSETIFF:
  1206. case TUNGETIFF:
  1207. case TUNSETTXFILTER:
  1208. case TUNGETSNDBUF:
  1209. case TUNSETSNDBUF:
  1210. case SIOCGIFHWADDR:
  1211. case SIOCSIFHWADDR:
  1212. arg = (unsigned long)compat_ptr(arg);
  1213. break;
  1214. default:
  1215. arg = (compat_ulong_t)arg;
  1216. break;
  1217. }
  1218. /*
  1219. * compat_ifreq is shorter than ifreq, so we must not access beyond
  1220. * the end of that structure. All fields that are used in this
  1221. * driver are compatible though, we don't need to convert the
  1222. * contents.
  1223. */
  1224. return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
  1225. }
  1226. #endif /* CONFIG_COMPAT */
  1227. static int tun_chr_fasync(int fd, struct file *file, int on)
  1228. {
  1229. struct tun_struct *tun = tun_get(file);
  1230. int ret;
  1231. if (!tun)
  1232. return -EBADFD;
  1233. tun_debug(KERN_INFO, tun, "tun_chr_fasync %d\n", on);
  1234. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  1235. goto out;
  1236. if (on) {
  1237. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  1238. if (ret)
  1239. goto out;
  1240. tun->flags |= TUN_FASYNC;
  1241. } else
  1242. tun->flags &= ~TUN_FASYNC;
  1243. ret = 0;
  1244. out:
  1245. tun_put(tun);
  1246. return ret;
  1247. }
  1248. static int tun_chr_open(struct inode *inode, struct file * file)
  1249. {
  1250. struct tun_file *tfile;
  1251. DBG1(KERN_INFO, "tunX: tun_chr_open\n");
  1252. tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
  1253. if (!tfile)
  1254. return -ENOMEM;
  1255. atomic_set(&tfile->count, 0);
  1256. tfile->tun = NULL;
  1257. tfile->net = get_net(current->nsproxy->net_ns);
  1258. file->private_data = tfile;
  1259. return 0;
  1260. }
  1261. static int tun_chr_close(struct inode *inode, struct file *file)
  1262. {
  1263. struct tun_file *tfile = file->private_data;
  1264. struct tun_struct *tun;
  1265. tun = __tun_get(tfile);
  1266. if (tun) {
  1267. struct net_device *dev = tun->dev;
  1268. tun_debug(KERN_INFO, tun, "tun_chr_close\n");
  1269. __tun_detach(tun);
  1270. /* If desirable, unregister the netdevice. */
  1271. if (!(tun->flags & TUN_PERSIST)) {
  1272. rtnl_lock();
  1273. if (dev->reg_state == NETREG_REGISTERED)
  1274. unregister_netdevice(dev);
  1275. rtnl_unlock();
  1276. }
  1277. }
  1278. tun = tfile->tun;
  1279. if (tun)
  1280. sock_put(tun->socket.sk);
  1281. put_net(tfile->net);
  1282. kfree(tfile);
  1283. return 0;
  1284. }
  1285. static const struct file_operations tun_fops = {
  1286. .owner = THIS_MODULE,
  1287. .llseek = no_llseek,
  1288. .read = do_sync_read,
  1289. .aio_read = tun_chr_aio_read,
  1290. .write = do_sync_write,
  1291. .aio_write = tun_chr_aio_write,
  1292. .poll = tun_chr_poll,
  1293. .unlocked_ioctl = tun_chr_ioctl,
  1294. #ifdef CONFIG_COMPAT
  1295. .compat_ioctl = tun_chr_compat_ioctl,
  1296. #endif
  1297. .open = tun_chr_open,
  1298. .release = tun_chr_close,
  1299. .fasync = tun_chr_fasync
  1300. };
  1301. static struct miscdevice tun_miscdev = {
  1302. .minor = TUN_MINOR,
  1303. .name = "tun",
  1304. .nodename = "net/tun",
  1305. .fops = &tun_fops,
  1306. };
  1307. /* ethtool interface */
  1308. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1309. {
  1310. cmd->supported = 0;
  1311. cmd->advertising = 0;
  1312. ethtool_cmd_speed_set(cmd, SPEED_10);
  1313. cmd->duplex = DUPLEX_FULL;
  1314. cmd->port = PORT_TP;
  1315. cmd->phy_address = 0;
  1316. cmd->transceiver = XCVR_INTERNAL;
  1317. cmd->autoneg = AUTONEG_DISABLE;
  1318. cmd->maxtxpkt = 0;
  1319. cmd->maxrxpkt = 0;
  1320. return 0;
  1321. }
  1322. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1323. {
  1324. struct tun_struct *tun = netdev_priv(dev);
  1325. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  1326. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1327. switch (tun->flags & TUN_TYPE_MASK) {
  1328. case TUN_TUN_DEV:
  1329. strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
  1330. break;
  1331. case TUN_TAP_DEV:
  1332. strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
  1333. break;
  1334. }
  1335. }
  1336. static u32 tun_get_msglevel(struct net_device *dev)
  1337. {
  1338. #ifdef TUN_DEBUG
  1339. struct tun_struct *tun = netdev_priv(dev);
  1340. return tun->debug;
  1341. #else
  1342. return -EOPNOTSUPP;
  1343. #endif
  1344. }
  1345. static void tun_set_msglevel(struct net_device *dev, u32 value)
  1346. {
  1347. #ifdef TUN_DEBUG
  1348. struct tun_struct *tun = netdev_priv(dev);
  1349. tun->debug = value;
  1350. #endif
  1351. }
  1352. static const struct ethtool_ops tun_ethtool_ops = {
  1353. .get_settings = tun_get_settings,
  1354. .get_drvinfo = tun_get_drvinfo,
  1355. .get_msglevel = tun_get_msglevel,
  1356. .set_msglevel = tun_set_msglevel,
  1357. .get_link = ethtool_op_get_link,
  1358. };
  1359. static int __init tun_init(void)
  1360. {
  1361. int ret = 0;
  1362. pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1363. pr_info("%s\n", DRV_COPYRIGHT);
  1364. ret = rtnl_link_register(&tun_link_ops);
  1365. if (ret) {
  1366. pr_err("Can't register link_ops\n");
  1367. goto err_linkops;
  1368. }
  1369. ret = misc_register(&tun_miscdev);
  1370. if (ret) {
  1371. pr_err("Can't register misc device %d\n", TUN_MINOR);
  1372. goto err_misc;
  1373. }
  1374. return 0;
  1375. err_misc:
  1376. rtnl_link_unregister(&tun_link_ops);
  1377. err_linkops:
  1378. return ret;
  1379. }
  1380. static void tun_cleanup(void)
  1381. {
  1382. misc_deregister(&tun_miscdev);
  1383. rtnl_link_unregister(&tun_link_ops);
  1384. }
  1385. /* Get an underlying socket object from tun file. Returns error unless file is
  1386. * attached to a device. The returned object works like a packet socket, it
  1387. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1388. * holding a reference to the file for as long as the socket is in use. */
  1389. struct socket *tun_get_socket(struct file *file)
  1390. {
  1391. struct tun_struct *tun;
  1392. if (file->f_op != &tun_fops)
  1393. return ERR_PTR(-EINVAL);
  1394. tun = tun_get(file);
  1395. if (!tun)
  1396. return ERR_PTR(-EBADFD);
  1397. tun_put(tun);
  1398. return &tun->socket;
  1399. }
  1400. EXPORT_SYMBOL_GPL(tun_get_socket);
  1401. module_init(tun_init);
  1402. module_exit(tun_cleanup);
  1403. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1404. MODULE_AUTHOR(DRV_COPYRIGHT);
  1405. MODULE_LICENSE("GPL");
  1406. MODULE_ALIAS_MISCDEV(TUN_MINOR);
  1407. MODULE_ALIAS("devname:net/tun");