tun.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824
  1. /*
  2. * TUN - Universal TUN/TAP device driver.
  3. * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
  16. */
  17. /*
  18. * Changes:
  19. *
  20. * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
  21. * Add TUNSETLINK ioctl to set the link encapsulation
  22. *
  23. * Mark Smith <markzzzsmith@yahoo.com.au>
  24. * Use eth_random_addr() for tap MAC address.
  25. *
  26. * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
  27. * Fixes in packet dropping, queue length setting and queue wakeup.
  28. * Increased default tx queue length.
  29. * Added ethtool API.
  30. * Minor cleanups
  31. *
  32. * Daniel Podlejski <underley@underley.eu.org>
  33. * Modifications for 2.3.99-pre5 kernel.
  34. */
  35. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  36. #define DRV_NAME "tun"
  37. #define DRV_VERSION "1.6"
  38. #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
  39. #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
  40. #include <linux/module.h>
  41. #include <linux/errno.h>
  42. #include <linux/kernel.h>
  43. #include <linux/major.h>
  44. #include <linux/slab.h>
  45. #include <linux/poll.h>
  46. #include <linux/fcntl.h>
  47. #include <linux/init.h>
  48. #include <linux/skbuff.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/etherdevice.h>
  51. #include <linux/miscdevice.h>
  52. #include <linux/ethtool.h>
  53. #include <linux/rtnetlink.h>
  54. #include <linux/compat.h>
  55. #include <linux/if.h>
  56. #include <linux/if_arp.h>
  57. #include <linux/if_ether.h>
  58. #include <linux/if_tun.h>
  59. #include <linux/crc32.h>
  60. #include <linux/nsproxy.h>
  61. #include <linux/virtio_net.h>
  62. #include <linux/rcupdate.h>
  63. #include <net/net_namespace.h>
  64. #include <net/netns/generic.h>
  65. #include <net/rtnetlink.h>
  66. #include <net/sock.h>
  67. #include <asm/uaccess.h>
  68. /* Uncomment to enable debugging */
  69. /* #define TUN_DEBUG 1 */
  70. #ifdef TUN_DEBUG
  71. static int debug;
  72. #define tun_debug(level, tun, fmt, args...) \
  73. do { \
  74. if (tun->debug) \
  75. netdev_printk(level, tun->dev, fmt, ##args); \
  76. } while (0)
  77. #define DBG1(level, fmt, args...) \
  78. do { \
  79. if (debug == 2) \
  80. printk(level fmt, ##args); \
  81. } while (0)
  82. #else
  83. #define tun_debug(level, tun, fmt, args...) \
  84. do { \
  85. if (0) \
  86. netdev_printk(level, tun->dev, fmt, ##args); \
  87. } while (0)
  88. #define DBG1(level, fmt, args...) \
  89. do { \
  90. if (0) \
  91. printk(level fmt, ##args); \
  92. } while (0)
  93. #endif
  94. #define GOODCOPY_LEN 128
  95. #define FLT_EXACT_COUNT 8
  96. struct tap_filter {
  97. unsigned int count; /* Number of addrs. Zero means disabled */
  98. u32 mask[2]; /* Mask of the hashed addrs */
  99. unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
  100. };
  101. struct tun_file {
  102. atomic_t count;
  103. struct tun_struct *tun;
  104. struct net *net;
  105. };
  106. struct tun_sock;
  107. struct tun_struct {
  108. struct tun_file *tfile;
  109. unsigned int flags;
  110. uid_t owner;
  111. gid_t group;
  112. struct net_device *dev;
  113. netdev_features_t set_features;
  114. #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
  115. NETIF_F_TSO6|NETIF_F_UFO)
  116. struct fasync_struct *fasync;
  117. struct tap_filter txflt;
  118. struct socket socket;
  119. struct socket_wq wq;
  120. int vnet_hdr_sz;
  121. #ifdef TUN_DEBUG
  122. int debug;
  123. #endif
  124. };
  125. struct tun_sock {
  126. struct sock sk;
  127. struct tun_struct *tun;
  128. };
  129. static inline struct tun_sock *tun_sk(struct sock *sk)
  130. {
  131. return container_of(sk, struct tun_sock, sk);
  132. }
  133. static int tun_attach(struct tun_struct *tun, struct file *file)
  134. {
  135. struct tun_file *tfile = file->private_data;
  136. int err;
  137. ASSERT_RTNL();
  138. netif_tx_lock_bh(tun->dev);
  139. err = -EINVAL;
  140. if (tfile->tun)
  141. goto out;
  142. err = -EBUSY;
  143. if (tun->tfile)
  144. goto out;
  145. err = 0;
  146. tfile->tun = tun;
  147. tun->tfile = tfile;
  148. tun->socket.file = file;
  149. netif_carrier_on(tun->dev);
  150. dev_hold(tun->dev);
  151. sock_hold(tun->socket.sk);
  152. atomic_inc(&tfile->count);
  153. out:
  154. netif_tx_unlock_bh(tun->dev);
  155. return err;
  156. }
  157. static void __tun_detach(struct tun_struct *tun)
  158. {
  159. /* Detach from net device */
  160. netif_tx_lock_bh(tun->dev);
  161. netif_carrier_off(tun->dev);
  162. tun->tfile = NULL;
  163. tun->socket.file = NULL;
  164. netif_tx_unlock_bh(tun->dev);
  165. /* Drop read queue */
  166. skb_queue_purge(&tun->socket.sk->sk_receive_queue);
  167. /* Drop the extra count on the net device */
  168. dev_put(tun->dev);
  169. }
  170. static void tun_detach(struct tun_struct *tun)
  171. {
  172. rtnl_lock();
  173. __tun_detach(tun);
  174. rtnl_unlock();
  175. }
  176. static struct tun_struct *__tun_get(struct tun_file *tfile)
  177. {
  178. struct tun_struct *tun = NULL;
  179. if (atomic_inc_not_zero(&tfile->count))
  180. tun = tfile->tun;
  181. return tun;
  182. }
  183. static struct tun_struct *tun_get(struct file *file)
  184. {
  185. return __tun_get(file->private_data);
  186. }
  187. static void tun_put(struct tun_struct *tun)
  188. {
  189. struct tun_file *tfile = tun->tfile;
  190. if (atomic_dec_and_test(&tfile->count))
  191. tun_detach(tfile->tun);
  192. }
  193. /* TAP filtering */
  194. static void addr_hash_set(u32 *mask, const u8 *addr)
  195. {
  196. int n = ether_crc(ETH_ALEN, addr) >> 26;
  197. mask[n >> 5] |= (1 << (n & 31));
  198. }
  199. static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
  200. {
  201. int n = ether_crc(ETH_ALEN, addr) >> 26;
  202. return mask[n >> 5] & (1 << (n & 31));
  203. }
  204. static int update_filter(struct tap_filter *filter, void __user *arg)
  205. {
  206. struct { u8 u[ETH_ALEN]; } *addr;
  207. struct tun_filter uf;
  208. int err, alen, n, nexact;
  209. if (copy_from_user(&uf, arg, sizeof(uf)))
  210. return -EFAULT;
  211. if (!uf.count) {
  212. /* Disabled */
  213. filter->count = 0;
  214. return 0;
  215. }
  216. alen = ETH_ALEN * uf.count;
  217. addr = kmalloc(alen, GFP_KERNEL);
  218. if (!addr)
  219. return -ENOMEM;
  220. if (copy_from_user(addr, arg + sizeof(uf), alen)) {
  221. err = -EFAULT;
  222. goto done;
  223. }
  224. /* The filter is updated without holding any locks. Which is
  225. * perfectly safe. We disable it first and in the worst
  226. * case we'll accept a few undesired packets. */
  227. filter->count = 0;
  228. wmb();
  229. /* Use first set of addresses as an exact filter */
  230. for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
  231. memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
  232. nexact = n;
  233. /* Remaining multicast addresses are hashed,
  234. * unicast will leave the filter disabled. */
  235. memset(filter->mask, 0, sizeof(filter->mask));
  236. for (; n < uf.count; n++) {
  237. if (!is_multicast_ether_addr(addr[n].u)) {
  238. err = 0; /* no filter */
  239. goto done;
  240. }
  241. addr_hash_set(filter->mask, addr[n].u);
  242. }
  243. /* For ALLMULTI just set the mask to all ones.
  244. * This overrides the mask populated above. */
  245. if ((uf.flags & TUN_FLT_ALLMULTI))
  246. memset(filter->mask, ~0, sizeof(filter->mask));
  247. /* Now enable the filter */
  248. wmb();
  249. filter->count = nexact;
  250. /* Return the number of exact filters */
  251. err = nexact;
  252. done:
  253. kfree(addr);
  254. return err;
  255. }
  256. /* Returns: 0 - drop, !=0 - accept */
  257. static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
  258. {
  259. /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
  260. * at this point. */
  261. struct ethhdr *eh = (struct ethhdr *) skb->data;
  262. int i;
  263. /* Exact match */
  264. for (i = 0; i < filter->count; i++)
  265. if (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 = -1;
  869. tun->group = -1;
  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 sprintf(buf, "%d\n", tun->owner);
  971. }
  972. static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
  973. char *buf)
  974. {
  975. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  976. return sprintf(buf, "%d\n", tun->group);
  977. }
  978. static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
  979. static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
  980. static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
  981. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  982. {
  983. struct sock *sk;
  984. struct tun_struct *tun;
  985. struct net_device *dev;
  986. int err;
  987. dev = __dev_get_by_name(net, ifr->ifr_name);
  988. if (dev) {
  989. const struct cred *cred = current_cred();
  990. if (ifr->ifr_flags & IFF_TUN_EXCL)
  991. return -EBUSY;
  992. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  993. tun = netdev_priv(dev);
  994. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  995. tun = netdev_priv(dev);
  996. else
  997. return -EINVAL;
  998. if (((tun->owner != -1 && cred->euid != tun->owner) ||
  999. (tun->group != -1 && !in_egroup_p(tun->group))) &&
  1000. !capable(CAP_NET_ADMIN))
  1001. return -EPERM;
  1002. err = security_tun_dev_attach(tun->socket.sk);
  1003. if (err < 0)
  1004. return err;
  1005. err = tun_attach(tun, file);
  1006. if (err < 0)
  1007. return err;
  1008. }
  1009. else {
  1010. char *name;
  1011. unsigned long flags = 0;
  1012. if (!capable(CAP_NET_ADMIN))
  1013. return -EPERM;
  1014. err = security_tun_dev_create();
  1015. if (err < 0)
  1016. return err;
  1017. /* Set dev type */
  1018. if (ifr->ifr_flags & IFF_TUN) {
  1019. /* TUN device */
  1020. flags |= TUN_TUN_DEV;
  1021. name = "tun%d";
  1022. } else if (ifr->ifr_flags & IFF_TAP) {
  1023. /* TAP device */
  1024. flags |= TUN_TAP_DEV;
  1025. name = "tap%d";
  1026. } else
  1027. return -EINVAL;
  1028. if (*ifr->ifr_name)
  1029. name = ifr->ifr_name;
  1030. dev = alloc_netdev(sizeof(struct tun_struct), name,
  1031. tun_setup);
  1032. if (!dev)
  1033. return -ENOMEM;
  1034. dev_net_set(dev, net);
  1035. dev->rtnl_link_ops = &tun_link_ops;
  1036. tun = netdev_priv(dev);
  1037. tun->dev = dev;
  1038. tun->flags = flags;
  1039. tun->txflt.count = 0;
  1040. tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  1041. set_bit(SOCK_EXTERNALLY_ALLOCATED, &tun->socket.flags);
  1042. err = -ENOMEM;
  1043. sk = sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
  1044. if (!sk)
  1045. goto err_free_dev;
  1046. sk_change_net(sk, net);
  1047. tun->socket.wq = &tun->wq;
  1048. init_waitqueue_head(&tun->wq.wait);
  1049. tun->socket.ops = &tun_socket_ops;
  1050. sock_init_data(&tun->socket, sk);
  1051. sk->sk_write_space = tun_sock_write_space;
  1052. sk->sk_sndbuf = INT_MAX;
  1053. sock_set_flag(sk, SOCK_ZEROCOPY);
  1054. tun_sk(sk)->tun = tun;
  1055. security_tun_dev_post_create(sk);
  1056. tun_net_init(dev);
  1057. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
  1058. TUN_USER_FEATURES;
  1059. dev->features = dev->hw_features;
  1060. err = register_netdevice(tun->dev);
  1061. if (err < 0)
  1062. goto err_free_sk;
  1063. if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
  1064. device_create_file(&tun->dev->dev, &dev_attr_owner) ||
  1065. device_create_file(&tun->dev->dev, &dev_attr_group))
  1066. pr_err("Failed to create tun sysfs files\n");
  1067. sk->sk_destruct = tun_sock_destruct;
  1068. err = tun_attach(tun, file);
  1069. if (err < 0)
  1070. goto failed;
  1071. }
  1072. tun_debug(KERN_INFO, tun, "tun_set_iff\n");
  1073. if (ifr->ifr_flags & IFF_NO_PI)
  1074. tun->flags |= TUN_NO_PI;
  1075. else
  1076. tun->flags &= ~TUN_NO_PI;
  1077. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  1078. tun->flags |= TUN_ONE_QUEUE;
  1079. else
  1080. tun->flags &= ~TUN_ONE_QUEUE;
  1081. if (ifr->ifr_flags & IFF_VNET_HDR)
  1082. tun->flags |= TUN_VNET_HDR;
  1083. else
  1084. tun->flags &= ~TUN_VNET_HDR;
  1085. /* Make sure persistent devices do not get stuck in
  1086. * xoff state.
  1087. */
  1088. if (netif_running(tun->dev))
  1089. netif_wake_queue(tun->dev);
  1090. strcpy(ifr->ifr_name, tun->dev->name);
  1091. return 0;
  1092. err_free_sk:
  1093. tun_free_netdev(dev);
  1094. err_free_dev:
  1095. free_netdev(dev);
  1096. failed:
  1097. return err;
  1098. }
  1099. static int tun_get_iff(struct net *net, struct tun_struct *tun,
  1100. struct ifreq *ifr)
  1101. {
  1102. tun_debug(KERN_INFO, tun, "tun_get_iff\n");
  1103. strcpy(ifr->ifr_name, tun->dev->name);
  1104. ifr->ifr_flags = tun_flags(tun);
  1105. return 0;
  1106. }
  1107. /* This is like a cut-down ethtool ops, except done via tun fd so no
  1108. * privs required. */
  1109. static int set_offload(struct tun_struct *tun, unsigned long arg)
  1110. {
  1111. netdev_features_t features = 0;
  1112. if (arg & TUN_F_CSUM) {
  1113. features |= NETIF_F_HW_CSUM;
  1114. arg &= ~TUN_F_CSUM;
  1115. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  1116. if (arg & TUN_F_TSO_ECN) {
  1117. features |= NETIF_F_TSO_ECN;
  1118. arg &= ~TUN_F_TSO_ECN;
  1119. }
  1120. if (arg & TUN_F_TSO4)
  1121. features |= NETIF_F_TSO;
  1122. if (arg & TUN_F_TSO6)
  1123. features |= NETIF_F_TSO6;
  1124. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  1125. }
  1126. if (arg & TUN_F_UFO) {
  1127. features |= NETIF_F_UFO;
  1128. arg &= ~TUN_F_UFO;
  1129. }
  1130. }
  1131. /* This gives the user a way to test for new features in future by
  1132. * trying to set them. */
  1133. if (arg)
  1134. return -EINVAL;
  1135. tun->set_features = features;
  1136. netdev_update_features(tun->dev);
  1137. return 0;
  1138. }
  1139. static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  1140. unsigned long arg, int ifreq_len)
  1141. {
  1142. struct tun_file *tfile = file->private_data;
  1143. struct tun_struct *tun;
  1144. void __user* argp = (void __user*)arg;
  1145. struct sock_fprog fprog;
  1146. struct ifreq ifr;
  1147. int sndbuf;
  1148. int vnet_hdr_sz;
  1149. int ret;
  1150. if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89) {
  1151. if (copy_from_user(&ifr, argp, ifreq_len))
  1152. return -EFAULT;
  1153. } else {
  1154. memset(&ifr, 0, sizeof(ifr));
  1155. }
  1156. if (cmd == TUNGETFEATURES) {
  1157. /* Currently this just means: "what IFF flags are valid?".
  1158. * This is needed because we never checked for invalid flags on
  1159. * TUNSETIFF. */
  1160. return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
  1161. IFF_VNET_HDR,
  1162. (unsigned int __user*)argp);
  1163. }
  1164. rtnl_lock();
  1165. tun = __tun_get(tfile);
  1166. if (cmd == TUNSETIFF && !tun) {
  1167. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  1168. ret = tun_set_iff(tfile->net, file, &ifr);
  1169. if (ret)
  1170. goto unlock;
  1171. if (copy_to_user(argp, &ifr, ifreq_len))
  1172. ret = -EFAULT;
  1173. goto unlock;
  1174. }
  1175. ret = -EBADFD;
  1176. if (!tun)
  1177. goto unlock;
  1178. tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %d\n", cmd);
  1179. ret = 0;
  1180. switch (cmd) {
  1181. case TUNGETIFF:
  1182. ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
  1183. if (ret)
  1184. break;
  1185. if (copy_to_user(argp, &ifr, ifreq_len))
  1186. ret = -EFAULT;
  1187. break;
  1188. case TUNSETNOCSUM:
  1189. /* Disable/Enable checksum */
  1190. /* [unimplemented] */
  1191. tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
  1192. arg ? "disabled" : "enabled");
  1193. break;
  1194. case TUNSETPERSIST:
  1195. /* Disable/Enable persist mode */
  1196. if (arg)
  1197. tun->flags |= TUN_PERSIST;
  1198. else
  1199. tun->flags &= ~TUN_PERSIST;
  1200. tun_debug(KERN_INFO, tun, "persist %s\n",
  1201. arg ? "enabled" : "disabled");
  1202. break;
  1203. case TUNSETOWNER:
  1204. /* Set owner of the device */
  1205. tun->owner = (uid_t) arg;
  1206. tun_debug(KERN_INFO, tun, "owner set to %d\n", tun->owner);
  1207. break;
  1208. case TUNSETGROUP:
  1209. /* Set group of the device */
  1210. tun->group= (gid_t) arg;
  1211. tun_debug(KERN_INFO, tun, "group set to %d\n", tun->group);
  1212. break;
  1213. case TUNSETLINK:
  1214. /* Only allow setting the type when the interface is down */
  1215. if (tun->dev->flags & IFF_UP) {
  1216. tun_debug(KERN_INFO, tun,
  1217. "Linktype set failed because interface is up\n");
  1218. ret = -EBUSY;
  1219. } else {
  1220. tun->dev->type = (int) arg;
  1221. tun_debug(KERN_INFO, tun, "linktype set to %d\n",
  1222. tun->dev->type);
  1223. ret = 0;
  1224. }
  1225. break;
  1226. #ifdef TUN_DEBUG
  1227. case TUNSETDEBUG:
  1228. tun->debug = arg;
  1229. break;
  1230. #endif
  1231. case TUNSETOFFLOAD:
  1232. ret = set_offload(tun, arg);
  1233. break;
  1234. case TUNSETTXFILTER:
  1235. /* Can be set only for TAPs */
  1236. ret = -EINVAL;
  1237. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1238. break;
  1239. ret = update_filter(&tun->txflt, (void __user *)arg);
  1240. break;
  1241. case SIOCGIFHWADDR:
  1242. /* Get hw address */
  1243. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  1244. ifr.ifr_hwaddr.sa_family = tun->dev->type;
  1245. if (copy_to_user(argp, &ifr, ifreq_len))
  1246. ret = -EFAULT;
  1247. break;
  1248. case SIOCSIFHWADDR:
  1249. /* Set hw address */
  1250. tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
  1251. ifr.ifr_hwaddr.sa_data);
  1252. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  1253. break;
  1254. case TUNGETSNDBUF:
  1255. sndbuf = tun->socket.sk->sk_sndbuf;
  1256. if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
  1257. ret = -EFAULT;
  1258. break;
  1259. case TUNSETSNDBUF:
  1260. if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
  1261. ret = -EFAULT;
  1262. break;
  1263. }
  1264. tun->socket.sk->sk_sndbuf = sndbuf;
  1265. break;
  1266. case TUNGETVNETHDRSZ:
  1267. vnet_hdr_sz = tun->vnet_hdr_sz;
  1268. if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
  1269. ret = -EFAULT;
  1270. break;
  1271. case TUNSETVNETHDRSZ:
  1272. if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
  1273. ret = -EFAULT;
  1274. break;
  1275. }
  1276. if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
  1277. ret = -EINVAL;
  1278. break;
  1279. }
  1280. tun->vnet_hdr_sz = vnet_hdr_sz;
  1281. break;
  1282. case TUNATTACHFILTER:
  1283. /* Can be set only for TAPs */
  1284. ret = -EINVAL;
  1285. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1286. break;
  1287. ret = -EFAULT;
  1288. if (copy_from_user(&fprog, argp, sizeof(fprog)))
  1289. break;
  1290. ret = sk_attach_filter(&fprog, tun->socket.sk);
  1291. break;
  1292. case TUNDETACHFILTER:
  1293. /* Can be set only for TAPs */
  1294. ret = -EINVAL;
  1295. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1296. break;
  1297. ret = sk_detach_filter(tun->socket.sk);
  1298. break;
  1299. default:
  1300. ret = -EINVAL;
  1301. break;
  1302. }
  1303. unlock:
  1304. rtnl_unlock();
  1305. if (tun)
  1306. tun_put(tun);
  1307. return ret;
  1308. }
  1309. static long tun_chr_ioctl(struct file *file,
  1310. unsigned int cmd, unsigned long arg)
  1311. {
  1312. return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
  1313. }
  1314. #ifdef CONFIG_COMPAT
  1315. static long tun_chr_compat_ioctl(struct file *file,
  1316. unsigned int cmd, unsigned long arg)
  1317. {
  1318. switch (cmd) {
  1319. case TUNSETIFF:
  1320. case TUNGETIFF:
  1321. case TUNSETTXFILTER:
  1322. case TUNGETSNDBUF:
  1323. case TUNSETSNDBUF:
  1324. case SIOCGIFHWADDR:
  1325. case SIOCSIFHWADDR:
  1326. arg = (unsigned long)compat_ptr(arg);
  1327. break;
  1328. default:
  1329. arg = (compat_ulong_t)arg;
  1330. break;
  1331. }
  1332. /*
  1333. * compat_ifreq is shorter than ifreq, so we must not access beyond
  1334. * the end of that structure. All fields that are used in this
  1335. * driver are compatible though, we don't need to convert the
  1336. * contents.
  1337. */
  1338. return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
  1339. }
  1340. #endif /* CONFIG_COMPAT */
  1341. static int tun_chr_fasync(int fd, struct file *file, int on)
  1342. {
  1343. struct tun_struct *tun = tun_get(file);
  1344. int ret;
  1345. if (!tun)
  1346. return -EBADFD;
  1347. tun_debug(KERN_INFO, tun, "tun_chr_fasync %d\n", on);
  1348. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  1349. goto out;
  1350. if (on) {
  1351. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  1352. if (ret)
  1353. goto out;
  1354. tun->flags |= TUN_FASYNC;
  1355. } else
  1356. tun->flags &= ~TUN_FASYNC;
  1357. ret = 0;
  1358. out:
  1359. tun_put(tun);
  1360. return ret;
  1361. }
  1362. static int tun_chr_open(struct inode *inode, struct file * file)
  1363. {
  1364. struct tun_file *tfile;
  1365. DBG1(KERN_INFO, "tunX: tun_chr_open\n");
  1366. tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
  1367. if (!tfile)
  1368. return -ENOMEM;
  1369. atomic_set(&tfile->count, 0);
  1370. tfile->tun = NULL;
  1371. tfile->net = get_net(current->nsproxy->net_ns);
  1372. file->private_data = tfile;
  1373. return 0;
  1374. }
  1375. static int tun_chr_close(struct inode *inode, struct file *file)
  1376. {
  1377. struct tun_file *tfile = file->private_data;
  1378. struct tun_struct *tun;
  1379. tun = __tun_get(tfile);
  1380. if (tun) {
  1381. struct net_device *dev = tun->dev;
  1382. tun_debug(KERN_INFO, tun, "tun_chr_close\n");
  1383. __tun_detach(tun);
  1384. /* If desirable, unregister the netdevice. */
  1385. if (!(tun->flags & TUN_PERSIST)) {
  1386. rtnl_lock();
  1387. if (dev->reg_state == NETREG_REGISTERED)
  1388. unregister_netdevice(dev);
  1389. rtnl_unlock();
  1390. }
  1391. }
  1392. tun = tfile->tun;
  1393. if (tun)
  1394. sock_put(tun->socket.sk);
  1395. put_net(tfile->net);
  1396. kfree(tfile);
  1397. return 0;
  1398. }
  1399. static const struct file_operations tun_fops = {
  1400. .owner = THIS_MODULE,
  1401. .llseek = no_llseek,
  1402. .read = do_sync_read,
  1403. .aio_read = tun_chr_aio_read,
  1404. .write = do_sync_write,
  1405. .aio_write = tun_chr_aio_write,
  1406. .poll = tun_chr_poll,
  1407. .unlocked_ioctl = tun_chr_ioctl,
  1408. #ifdef CONFIG_COMPAT
  1409. .compat_ioctl = tun_chr_compat_ioctl,
  1410. #endif
  1411. .open = tun_chr_open,
  1412. .release = tun_chr_close,
  1413. .fasync = tun_chr_fasync
  1414. };
  1415. static struct miscdevice tun_miscdev = {
  1416. .minor = TUN_MINOR,
  1417. .name = "tun",
  1418. .nodename = "net/tun",
  1419. .fops = &tun_fops,
  1420. };
  1421. /* ethtool interface */
  1422. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1423. {
  1424. cmd->supported = 0;
  1425. cmd->advertising = 0;
  1426. ethtool_cmd_speed_set(cmd, SPEED_10);
  1427. cmd->duplex = DUPLEX_FULL;
  1428. cmd->port = PORT_TP;
  1429. cmd->phy_address = 0;
  1430. cmd->transceiver = XCVR_INTERNAL;
  1431. cmd->autoneg = AUTONEG_DISABLE;
  1432. cmd->maxtxpkt = 0;
  1433. cmd->maxrxpkt = 0;
  1434. return 0;
  1435. }
  1436. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1437. {
  1438. struct tun_struct *tun = netdev_priv(dev);
  1439. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  1440. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1441. switch (tun->flags & TUN_TYPE_MASK) {
  1442. case TUN_TUN_DEV:
  1443. strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
  1444. break;
  1445. case TUN_TAP_DEV:
  1446. strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
  1447. break;
  1448. }
  1449. }
  1450. static u32 tun_get_msglevel(struct net_device *dev)
  1451. {
  1452. #ifdef TUN_DEBUG
  1453. struct tun_struct *tun = netdev_priv(dev);
  1454. return tun->debug;
  1455. #else
  1456. return -EOPNOTSUPP;
  1457. #endif
  1458. }
  1459. static void tun_set_msglevel(struct net_device *dev, u32 value)
  1460. {
  1461. #ifdef TUN_DEBUG
  1462. struct tun_struct *tun = netdev_priv(dev);
  1463. tun->debug = value;
  1464. #endif
  1465. }
  1466. static const struct ethtool_ops tun_ethtool_ops = {
  1467. .get_settings = tun_get_settings,
  1468. .get_drvinfo = tun_get_drvinfo,
  1469. .get_msglevel = tun_get_msglevel,
  1470. .set_msglevel = tun_set_msglevel,
  1471. .get_link = ethtool_op_get_link,
  1472. };
  1473. static int __init tun_init(void)
  1474. {
  1475. int ret = 0;
  1476. pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1477. pr_info("%s\n", DRV_COPYRIGHT);
  1478. ret = rtnl_link_register(&tun_link_ops);
  1479. if (ret) {
  1480. pr_err("Can't register link_ops\n");
  1481. goto err_linkops;
  1482. }
  1483. ret = misc_register(&tun_miscdev);
  1484. if (ret) {
  1485. pr_err("Can't register misc device %d\n", TUN_MINOR);
  1486. goto err_misc;
  1487. }
  1488. return 0;
  1489. err_misc:
  1490. rtnl_link_unregister(&tun_link_ops);
  1491. err_linkops:
  1492. return ret;
  1493. }
  1494. static void tun_cleanup(void)
  1495. {
  1496. misc_deregister(&tun_miscdev);
  1497. rtnl_link_unregister(&tun_link_ops);
  1498. }
  1499. /* Get an underlying socket object from tun file. Returns error unless file is
  1500. * attached to a device. The returned object works like a packet socket, it
  1501. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1502. * holding a reference to the file for as long as the socket is in use. */
  1503. struct socket *tun_get_socket(struct file *file)
  1504. {
  1505. struct tun_struct *tun;
  1506. if (file->f_op != &tun_fops)
  1507. return ERR_PTR(-EINVAL);
  1508. tun = tun_get(file);
  1509. if (!tun)
  1510. return ERR_PTR(-EBADFD);
  1511. tun_put(tun);
  1512. return &tun->socket;
  1513. }
  1514. EXPORT_SYMBOL_GPL(tun_get_socket);
  1515. module_init(tun_init);
  1516. module_exit(tun_cleanup);
  1517. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1518. MODULE_AUTHOR(DRV_COPYRIGHT);
  1519. MODULE_LICENSE("GPL");
  1520. MODULE_ALIAS_MISCDEV(TUN_MINOR);
  1521. MODULE_ALIAS("devname:net/tun");