tun.c 42 KB

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