tun.c 42 KB

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