tun.c 38 KB

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