tun.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  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/system.h>
  68. #include <asm/uaccess.h>
  69. /* Uncomment to enable debugging */
  70. /* #define TUN_DEBUG 1 */
  71. #ifdef TUN_DEBUG
  72. static int debug;
  73. #define tun_debug(level, tun, fmt, args...) \
  74. do { \
  75. if (tun->debug) \
  76. netdev_printk(level, tun->dev, fmt, ##args); \
  77. } while (0)
  78. #define DBG1(level, fmt, args...) \
  79. do { \
  80. if (debug == 2) \
  81. printk(level fmt, ##args); \
  82. } while (0)
  83. #else
  84. #define tun_debug(level, tun, fmt, args...) \
  85. do { \
  86. if (0) \
  87. netdev_printk(level, tun->dev, fmt, ##args); \
  88. } while (0)
  89. #define DBG1(level, fmt, args...) \
  90. do { \
  91. if (0) \
  92. printk(level fmt, ##args); \
  93. } while (0)
  94. #endif
  95. #define 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. u32 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 (!compare_ether_addr(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. sock_put(tun->socket.sk);
  301. }
  302. /* Net device open. */
  303. static int tun_net_open(struct net_device *dev)
  304. {
  305. netif_start_queue(dev);
  306. return 0;
  307. }
  308. /* Net device close. */
  309. static int tun_net_close(struct net_device *dev)
  310. {
  311. netif_stop_queue(dev);
  312. return 0;
  313. }
  314. /* Net device start xmit */
  315. static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
  316. {
  317. struct tun_struct *tun = netdev_priv(dev);
  318. tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
  319. /* Drop packet if interface is not attached */
  320. if (!tun->tfile)
  321. goto drop;
  322. /* Drop if the filter does not like it.
  323. * This is a noop if the filter is disabled.
  324. * Filter can be enabled only for the TAP devices. */
  325. if (!check_filter(&tun->txflt, skb))
  326. goto drop;
  327. if (tun->socket.sk->sk_filter &&
  328. sk_filter(tun->socket.sk, skb))
  329. goto drop;
  330. if (skb_queue_len(&tun->socket.sk->sk_receive_queue) >= dev->tx_queue_len) {
  331. if (!(tun->flags & TUN_ONE_QUEUE)) {
  332. /* Normal queueing mode. */
  333. /* Packet scheduler handles dropping of further packets. */
  334. netif_stop_queue(dev);
  335. /* We won't see all dropped packets individually, so overrun
  336. * error is more appropriate. */
  337. dev->stats.tx_fifo_errors++;
  338. } else {
  339. /* Single queue mode.
  340. * Driver handles dropping of all packets itself. */
  341. goto drop;
  342. }
  343. }
  344. /* Orphan the skb - required as we might hang on to it
  345. * for indefinite time. */
  346. skb_orphan(skb);
  347. /* Enqueue packet */
  348. skb_queue_tail(&tun->socket.sk->sk_receive_queue, skb);
  349. /* Notify and wake up reader process */
  350. if (tun->flags & TUN_FASYNC)
  351. kill_fasync(&tun->fasync, SIGIO, POLL_IN);
  352. wake_up_interruptible_poll(&tun->wq.wait, POLLIN |
  353. POLLRDNORM | POLLRDBAND);
  354. return NETDEV_TX_OK;
  355. drop:
  356. dev->stats.tx_dropped++;
  357. kfree_skb(skb);
  358. return NETDEV_TX_OK;
  359. }
  360. static void tun_net_mclist(struct net_device *dev)
  361. {
  362. /*
  363. * This callback is supposed to deal with mc filter in
  364. * _rx_ path and has nothing to do with the _tx_ path.
  365. * In rx path we always accept everything userspace gives us.
  366. */
  367. }
  368. #define MIN_MTU 68
  369. #define MAX_MTU 65535
  370. static int
  371. tun_net_change_mtu(struct net_device *dev, int new_mtu)
  372. {
  373. if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
  374. return -EINVAL;
  375. dev->mtu = new_mtu;
  376. return 0;
  377. }
  378. static u32 tun_net_fix_features(struct net_device *dev, u32 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_multicast_list = 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. random_ether_addr(dev->dev_addr);
  446. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  447. break;
  448. }
  449. }
  450. /* Character device part */
  451. /* Poll */
  452. static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
  453. {
  454. struct tun_file *tfile = file->private_data;
  455. struct tun_struct *tun = __tun_get(tfile);
  456. struct sock *sk;
  457. unsigned int mask = 0;
  458. if (!tun)
  459. return POLLERR;
  460. sk = tun->socket.sk;
  461. tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
  462. poll_wait(file, &tun->wq.wait, wait);
  463. if (!skb_queue_empty(&sk->sk_receive_queue))
  464. mask |= POLLIN | POLLRDNORM;
  465. if (sock_writeable(sk) ||
  466. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
  467. sock_writeable(sk)))
  468. mask |= POLLOUT | POLLWRNORM;
  469. if (tun->dev->reg_state != NETREG_REGISTERED)
  470. mask = POLLERR;
  471. tun_put(tun);
  472. return mask;
  473. }
  474. /* prepad is the amount to reserve at front. len is length after that.
  475. * linear is a hint as to how much to copy (usually headers). */
  476. static struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
  477. size_t prepad, size_t len,
  478. size_t linear, int noblock)
  479. {
  480. struct sock *sk = tun->socket.sk;
  481. struct sk_buff *skb;
  482. int err;
  483. sock_update_classid(sk);
  484. /* Under a page? Don't bother with paged skb. */
  485. if (prepad + len < PAGE_SIZE || !linear)
  486. linear = len;
  487. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  488. &err);
  489. if (!skb)
  490. return ERR_PTR(err);
  491. skb_reserve(skb, prepad);
  492. skb_put(skb, linear);
  493. skb->data_len = len - linear;
  494. skb->len += len - linear;
  495. return skb;
  496. }
  497. /* Get packet from user space buffer */
  498. static ssize_t tun_get_user(struct tun_struct *tun,
  499. const struct iovec *iv, size_t count,
  500. int noblock)
  501. {
  502. struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
  503. struct sk_buff *skb;
  504. size_t len = count, align = NET_SKB_PAD;
  505. struct virtio_net_hdr gso = { 0 };
  506. int offset = 0;
  507. if (!(tun->flags & TUN_NO_PI)) {
  508. if ((len -= sizeof(pi)) > count)
  509. return -EINVAL;
  510. if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
  511. return -EFAULT;
  512. offset += sizeof(pi);
  513. }
  514. if (tun->flags & TUN_VNET_HDR) {
  515. if ((len -= tun->vnet_hdr_sz) > count)
  516. return -EINVAL;
  517. if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
  518. return -EFAULT;
  519. if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  520. gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
  521. gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
  522. if (gso.hdr_len > len)
  523. return -EINVAL;
  524. offset += tun->vnet_hdr_sz;
  525. }
  526. if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
  527. align += NET_IP_ALIGN;
  528. if (unlikely(len < ETH_HLEN ||
  529. (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
  530. return -EINVAL;
  531. }
  532. skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock);
  533. if (IS_ERR(skb)) {
  534. if (PTR_ERR(skb) != -EAGAIN)
  535. tun->dev->stats.rx_dropped++;
  536. return PTR_ERR(skb);
  537. }
  538. if (skb_copy_datagram_from_iovec(skb, 0, iv, offset, len)) {
  539. tun->dev->stats.rx_dropped++;
  540. kfree_skb(skb);
  541. return -EFAULT;
  542. }
  543. if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  544. if (!skb_partial_csum_set(skb, gso.csum_start,
  545. gso.csum_offset)) {
  546. tun->dev->stats.rx_frame_errors++;
  547. kfree_skb(skb);
  548. return -EINVAL;
  549. }
  550. }
  551. switch (tun->flags & TUN_TYPE_MASK) {
  552. case TUN_TUN_DEV:
  553. if (tun->flags & TUN_NO_PI) {
  554. switch (skb->data[0] & 0xf0) {
  555. case 0x40:
  556. pi.proto = htons(ETH_P_IP);
  557. break;
  558. case 0x60:
  559. pi.proto = htons(ETH_P_IPV6);
  560. break;
  561. default:
  562. tun->dev->stats.rx_dropped++;
  563. kfree_skb(skb);
  564. return -EINVAL;
  565. }
  566. }
  567. skb_reset_mac_header(skb);
  568. skb->protocol = pi.proto;
  569. skb->dev = tun->dev;
  570. break;
  571. case TUN_TAP_DEV:
  572. skb->protocol = eth_type_trans(skb, tun->dev);
  573. break;
  574. }
  575. if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  576. pr_debug("GSO!\n");
  577. switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  578. case VIRTIO_NET_HDR_GSO_TCPV4:
  579. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  580. break;
  581. case VIRTIO_NET_HDR_GSO_TCPV6:
  582. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  583. break;
  584. case VIRTIO_NET_HDR_GSO_UDP:
  585. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  586. break;
  587. default:
  588. tun->dev->stats.rx_frame_errors++;
  589. kfree_skb(skb);
  590. return -EINVAL;
  591. }
  592. if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  593. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  594. skb_shinfo(skb)->gso_size = gso.gso_size;
  595. if (skb_shinfo(skb)->gso_size == 0) {
  596. tun->dev->stats.rx_frame_errors++;
  597. kfree_skb(skb);
  598. return -EINVAL;
  599. }
  600. /* Header must be checked, and gso_segs computed. */
  601. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  602. skb_shinfo(skb)->gso_segs = 0;
  603. }
  604. netif_rx_ni(skb);
  605. tun->dev->stats.rx_packets++;
  606. tun->dev->stats.rx_bytes += len;
  607. return count;
  608. }
  609. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  610. unsigned long count, loff_t pos)
  611. {
  612. struct file *file = iocb->ki_filp;
  613. struct tun_struct *tun = tun_get(file);
  614. ssize_t result;
  615. if (!tun)
  616. return -EBADFD;
  617. tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
  618. result = tun_get_user(tun, iv, iov_length(iv, count),
  619. file->f_flags & O_NONBLOCK);
  620. tun_put(tun);
  621. return result;
  622. }
  623. /* Put packet to the user space buffer */
  624. static ssize_t tun_put_user(struct tun_struct *tun,
  625. struct sk_buff *skb,
  626. const struct iovec *iv, int len)
  627. {
  628. struct tun_pi pi = { 0, skb->protocol };
  629. ssize_t total = 0;
  630. if (!(tun->flags & TUN_NO_PI)) {
  631. if ((len -= sizeof(pi)) < 0)
  632. return -EINVAL;
  633. if (len < skb->len) {
  634. /* Packet will be striped */
  635. pi.flags |= TUN_PKT_STRIP;
  636. }
  637. if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
  638. return -EFAULT;
  639. total += sizeof(pi);
  640. }
  641. if (tun->flags & TUN_VNET_HDR) {
  642. struct virtio_net_hdr gso = { 0 }; /* no info leak */
  643. if ((len -= tun->vnet_hdr_sz) < 0)
  644. return -EINVAL;
  645. if (skb_is_gso(skb)) {
  646. struct skb_shared_info *sinfo = skb_shinfo(skb);
  647. /* This is a hint as to how much should be linear. */
  648. gso.hdr_len = skb_headlen(skb);
  649. gso.gso_size = sinfo->gso_size;
  650. if (sinfo->gso_type & SKB_GSO_TCPV4)
  651. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  652. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  653. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  654. else if (sinfo->gso_type & SKB_GSO_UDP)
  655. gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  656. else {
  657. pr_err("unexpected GSO type: "
  658. "0x%x, gso_size %d, hdr_len %d\n",
  659. sinfo->gso_type, gso.gso_size,
  660. gso.hdr_len);
  661. print_hex_dump(KERN_ERR, "tun: ",
  662. DUMP_PREFIX_NONE,
  663. 16, 1, skb->head,
  664. min((int)gso.hdr_len, 64), true);
  665. WARN_ON_ONCE(1);
  666. return -EINVAL;
  667. }
  668. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  669. gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  670. } else
  671. gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  672. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  673. gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  674. gso.csum_start = skb_checksum_start_offset(skb);
  675. gso.csum_offset = skb->csum_offset;
  676. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  677. gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
  678. } /* else everything is zero */
  679. if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
  680. sizeof(gso))))
  681. return -EFAULT;
  682. total += tun->vnet_hdr_sz;
  683. }
  684. len = min_t(int, skb->len, len);
  685. skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
  686. total += skb->len;
  687. tun->dev->stats.tx_packets++;
  688. tun->dev->stats.tx_bytes += len;
  689. return total;
  690. }
  691. static ssize_t tun_do_read(struct tun_struct *tun,
  692. struct kiocb *iocb, const struct iovec *iv,
  693. ssize_t len, int noblock)
  694. {
  695. DECLARE_WAITQUEUE(wait, current);
  696. struct sk_buff *skb;
  697. ssize_t ret = 0;
  698. tun_debug(KERN_INFO, tun, "tun_chr_read\n");
  699. if (unlikely(!noblock))
  700. add_wait_queue(&tun->wq.wait, &wait);
  701. while (len) {
  702. current->state = TASK_INTERRUPTIBLE;
  703. /* Read frames from the queue */
  704. if (!(skb=skb_dequeue(&tun->socket.sk->sk_receive_queue))) {
  705. if (noblock) {
  706. ret = -EAGAIN;
  707. break;
  708. }
  709. if (signal_pending(current)) {
  710. ret = -ERESTARTSYS;
  711. break;
  712. }
  713. if (tun->dev->reg_state != NETREG_REGISTERED) {
  714. ret = -EIO;
  715. break;
  716. }
  717. /* Nothing to read, let's sleep */
  718. schedule();
  719. continue;
  720. }
  721. netif_wake_queue(tun->dev);
  722. ret = tun_put_user(tun, skb, iv, len);
  723. kfree_skb(skb);
  724. break;
  725. }
  726. current->state = TASK_RUNNING;
  727. if (unlikely(!noblock))
  728. remove_wait_queue(&tun->wq.wait, &wait);
  729. return ret;
  730. }
  731. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  732. unsigned long count, loff_t pos)
  733. {
  734. struct file *file = iocb->ki_filp;
  735. struct tun_file *tfile = file->private_data;
  736. struct tun_struct *tun = __tun_get(tfile);
  737. ssize_t len, ret;
  738. if (!tun)
  739. return -EBADFD;
  740. len = iov_length(iv, count);
  741. if (len < 0) {
  742. ret = -EINVAL;
  743. goto out;
  744. }
  745. ret = tun_do_read(tun, iocb, iv, len, file->f_flags & O_NONBLOCK);
  746. ret = min_t(ssize_t, ret, len);
  747. out:
  748. tun_put(tun);
  749. return ret;
  750. }
  751. static void tun_setup(struct net_device *dev)
  752. {
  753. struct tun_struct *tun = netdev_priv(dev);
  754. tun->owner = -1;
  755. tun->group = -1;
  756. dev->ethtool_ops = &tun_ethtool_ops;
  757. dev->destructor = tun_free_netdev;
  758. }
  759. /* Trivial set of netlink ops to allow deleting tun or tap
  760. * device with netlink.
  761. */
  762. static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
  763. {
  764. return -EINVAL;
  765. }
  766. static struct rtnl_link_ops tun_link_ops __read_mostly = {
  767. .kind = DRV_NAME,
  768. .priv_size = sizeof(struct tun_struct),
  769. .setup = tun_setup,
  770. .validate = tun_validate,
  771. };
  772. static void tun_sock_write_space(struct sock *sk)
  773. {
  774. struct tun_struct *tun;
  775. wait_queue_head_t *wqueue;
  776. if (!sock_writeable(sk))
  777. return;
  778. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  779. return;
  780. wqueue = sk_sleep(sk);
  781. if (wqueue && waitqueue_active(wqueue))
  782. wake_up_interruptible_sync_poll(wqueue, POLLOUT |
  783. POLLWRNORM | POLLWRBAND);
  784. tun = tun_sk(sk)->tun;
  785. kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
  786. }
  787. static void tun_sock_destruct(struct sock *sk)
  788. {
  789. free_netdev(tun_sk(sk)->tun->dev);
  790. }
  791. static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
  792. struct msghdr *m, size_t total_len)
  793. {
  794. struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
  795. return tun_get_user(tun, m->msg_iov, total_len,
  796. m->msg_flags & MSG_DONTWAIT);
  797. }
  798. static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
  799. struct msghdr *m, size_t total_len,
  800. int flags)
  801. {
  802. struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
  803. int ret;
  804. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  805. return -EINVAL;
  806. ret = tun_do_read(tun, iocb, m->msg_iov, total_len,
  807. flags & MSG_DONTWAIT);
  808. if (ret > total_len) {
  809. m->msg_flags |= MSG_TRUNC;
  810. ret = flags & MSG_TRUNC ? ret : total_len;
  811. }
  812. return ret;
  813. }
  814. /* Ops structure to mimic raw sockets with tun */
  815. static const struct proto_ops tun_socket_ops = {
  816. .sendmsg = tun_sendmsg,
  817. .recvmsg = tun_recvmsg,
  818. };
  819. static struct proto tun_proto = {
  820. .name = "tun",
  821. .owner = THIS_MODULE,
  822. .obj_size = sizeof(struct tun_sock),
  823. };
  824. static int tun_flags(struct tun_struct *tun)
  825. {
  826. int flags = 0;
  827. if (tun->flags & TUN_TUN_DEV)
  828. flags |= IFF_TUN;
  829. else
  830. flags |= IFF_TAP;
  831. if (tun->flags & TUN_NO_PI)
  832. flags |= IFF_NO_PI;
  833. if (tun->flags & TUN_ONE_QUEUE)
  834. flags |= IFF_ONE_QUEUE;
  835. if (tun->flags & TUN_VNET_HDR)
  836. flags |= IFF_VNET_HDR;
  837. return flags;
  838. }
  839. static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
  840. char *buf)
  841. {
  842. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  843. return sprintf(buf, "0x%x\n", tun_flags(tun));
  844. }
  845. static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
  846. char *buf)
  847. {
  848. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  849. return sprintf(buf, "%d\n", tun->owner);
  850. }
  851. static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
  852. char *buf)
  853. {
  854. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  855. return sprintf(buf, "%d\n", tun->group);
  856. }
  857. static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
  858. static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
  859. static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
  860. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  861. {
  862. struct sock *sk;
  863. struct tun_struct *tun;
  864. struct net_device *dev;
  865. int err;
  866. dev = __dev_get_by_name(net, ifr->ifr_name);
  867. if (dev) {
  868. const struct cred *cred = current_cred();
  869. if (ifr->ifr_flags & IFF_TUN_EXCL)
  870. return -EBUSY;
  871. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  872. tun = netdev_priv(dev);
  873. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  874. tun = netdev_priv(dev);
  875. else
  876. return -EINVAL;
  877. if (((tun->owner != -1 && cred->euid != tun->owner) ||
  878. (tun->group != -1 && !in_egroup_p(tun->group))) &&
  879. !capable(CAP_NET_ADMIN))
  880. return -EPERM;
  881. err = security_tun_dev_attach(tun->socket.sk);
  882. if (err < 0)
  883. return err;
  884. err = tun_attach(tun, file);
  885. if (err < 0)
  886. return err;
  887. }
  888. else {
  889. char *name;
  890. unsigned long flags = 0;
  891. if (!capable(CAP_NET_ADMIN))
  892. return -EPERM;
  893. err = security_tun_dev_create();
  894. if (err < 0)
  895. return err;
  896. /* Set dev type */
  897. if (ifr->ifr_flags & IFF_TUN) {
  898. /* TUN device */
  899. flags |= TUN_TUN_DEV;
  900. name = "tun%d";
  901. } else if (ifr->ifr_flags & IFF_TAP) {
  902. /* TAP device */
  903. flags |= TUN_TAP_DEV;
  904. name = "tap%d";
  905. } else
  906. return -EINVAL;
  907. if (*ifr->ifr_name)
  908. name = ifr->ifr_name;
  909. dev = alloc_netdev(sizeof(struct tun_struct), name,
  910. tun_setup);
  911. if (!dev)
  912. return -ENOMEM;
  913. dev_net_set(dev, net);
  914. dev->rtnl_link_ops = &tun_link_ops;
  915. tun = netdev_priv(dev);
  916. tun->dev = dev;
  917. tun->flags = flags;
  918. tun->txflt.count = 0;
  919. tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  920. err = -ENOMEM;
  921. sk = sk_alloc(net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
  922. if (!sk)
  923. goto err_free_dev;
  924. tun->socket.wq = &tun->wq;
  925. init_waitqueue_head(&tun->wq.wait);
  926. tun->socket.ops = &tun_socket_ops;
  927. sock_init_data(&tun->socket, sk);
  928. sk->sk_write_space = tun_sock_write_space;
  929. sk->sk_sndbuf = INT_MAX;
  930. tun_sk(sk)->tun = tun;
  931. security_tun_dev_post_create(sk);
  932. tun_net_init(dev);
  933. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
  934. TUN_USER_FEATURES;
  935. dev->features = dev->hw_features;
  936. err = register_netdevice(tun->dev);
  937. if (err < 0)
  938. goto err_free_sk;
  939. if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
  940. device_create_file(&tun->dev->dev, &dev_attr_owner) ||
  941. device_create_file(&tun->dev->dev, &dev_attr_group))
  942. pr_err("Failed to create tun sysfs files\n");
  943. sk->sk_destruct = tun_sock_destruct;
  944. err = tun_attach(tun, file);
  945. if (err < 0)
  946. goto failed;
  947. }
  948. tun_debug(KERN_INFO, tun, "tun_set_iff\n");
  949. if (ifr->ifr_flags & IFF_NO_PI)
  950. tun->flags |= TUN_NO_PI;
  951. else
  952. tun->flags &= ~TUN_NO_PI;
  953. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  954. tun->flags |= TUN_ONE_QUEUE;
  955. else
  956. tun->flags &= ~TUN_ONE_QUEUE;
  957. if (ifr->ifr_flags & IFF_VNET_HDR)
  958. tun->flags |= TUN_VNET_HDR;
  959. else
  960. tun->flags &= ~TUN_VNET_HDR;
  961. /* Make sure persistent devices do not get stuck in
  962. * xoff state.
  963. */
  964. if (netif_running(tun->dev))
  965. netif_wake_queue(tun->dev);
  966. strcpy(ifr->ifr_name, tun->dev->name);
  967. return 0;
  968. err_free_sk:
  969. sock_put(sk);
  970. err_free_dev:
  971. free_netdev(dev);
  972. failed:
  973. return err;
  974. }
  975. static int tun_get_iff(struct net *net, struct tun_struct *tun,
  976. struct ifreq *ifr)
  977. {
  978. tun_debug(KERN_INFO, tun, "tun_get_iff\n");
  979. strcpy(ifr->ifr_name, tun->dev->name);
  980. ifr->ifr_flags = tun_flags(tun);
  981. return 0;
  982. }
  983. /* This is like a cut-down ethtool ops, except done via tun fd so no
  984. * privs required. */
  985. static int set_offload(struct tun_struct *tun, unsigned long arg)
  986. {
  987. u32 features = 0;
  988. if (arg & TUN_F_CSUM) {
  989. features |= NETIF_F_HW_CSUM;
  990. arg &= ~TUN_F_CSUM;
  991. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  992. if (arg & TUN_F_TSO_ECN) {
  993. features |= NETIF_F_TSO_ECN;
  994. arg &= ~TUN_F_TSO_ECN;
  995. }
  996. if (arg & TUN_F_TSO4)
  997. features |= NETIF_F_TSO;
  998. if (arg & TUN_F_TSO6)
  999. features |= NETIF_F_TSO6;
  1000. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  1001. }
  1002. if (arg & TUN_F_UFO) {
  1003. features |= NETIF_F_UFO;
  1004. arg &= ~TUN_F_UFO;
  1005. }
  1006. }
  1007. /* This gives the user a way to test for new features in future by
  1008. * trying to set them. */
  1009. if (arg)
  1010. return -EINVAL;
  1011. tun->set_features = features;
  1012. netdev_update_features(tun->dev);
  1013. return 0;
  1014. }
  1015. static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  1016. unsigned long arg, int ifreq_len)
  1017. {
  1018. struct tun_file *tfile = file->private_data;
  1019. struct tun_struct *tun;
  1020. void __user* argp = (void __user*)arg;
  1021. struct sock_fprog fprog;
  1022. struct ifreq ifr;
  1023. int sndbuf;
  1024. int vnet_hdr_sz;
  1025. int ret;
  1026. if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
  1027. if (copy_from_user(&ifr, argp, ifreq_len))
  1028. return -EFAULT;
  1029. if (cmd == TUNGETFEATURES) {
  1030. /* Currently this just means: "what IFF flags are valid?".
  1031. * This is needed because we never checked for invalid flags on
  1032. * TUNSETIFF. */
  1033. return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
  1034. IFF_VNET_HDR,
  1035. (unsigned int __user*)argp);
  1036. }
  1037. rtnl_lock();
  1038. tun = __tun_get(tfile);
  1039. if (cmd == TUNSETIFF && !tun) {
  1040. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  1041. ret = tun_set_iff(tfile->net, file, &ifr);
  1042. if (ret)
  1043. goto unlock;
  1044. if (copy_to_user(argp, &ifr, ifreq_len))
  1045. ret = -EFAULT;
  1046. goto unlock;
  1047. }
  1048. ret = -EBADFD;
  1049. if (!tun)
  1050. goto unlock;
  1051. tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %d\n", cmd);
  1052. ret = 0;
  1053. switch (cmd) {
  1054. case TUNGETIFF:
  1055. ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
  1056. if (ret)
  1057. break;
  1058. if (copy_to_user(argp, &ifr, ifreq_len))
  1059. ret = -EFAULT;
  1060. break;
  1061. case TUNSETNOCSUM:
  1062. /* Disable/Enable checksum */
  1063. /* [unimplemented] */
  1064. tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
  1065. arg ? "disabled" : "enabled");
  1066. break;
  1067. case TUNSETPERSIST:
  1068. /* Disable/Enable persist mode */
  1069. if (arg)
  1070. tun->flags |= TUN_PERSIST;
  1071. else
  1072. tun->flags &= ~TUN_PERSIST;
  1073. tun_debug(KERN_INFO, tun, "persist %s\n",
  1074. arg ? "enabled" : "disabled");
  1075. break;
  1076. case TUNSETOWNER:
  1077. /* Set owner of the device */
  1078. tun->owner = (uid_t) arg;
  1079. tun_debug(KERN_INFO, tun, "owner set to %d\n", tun->owner);
  1080. break;
  1081. case TUNSETGROUP:
  1082. /* Set group of the device */
  1083. tun->group= (gid_t) arg;
  1084. tun_debug(KERN_INFO, tun, "group set to %d\n", tun->group);
  1085. break;
  1086. case TUNSETLINK:
  1087. /* Only allow setting the type when the interface is down */
  1088. if (tun->dev->flags & IFF_UP) {
  1089. tun_debug(KERN_INFO, tun,
  1090. "Linktype set failed because interface is up\n");
  1091. ret = -EBUSY;
  1092. } else {
  1093. tun->dev->type = (int) arg;
  1094. tun_debug(KERN_INFO, tun, "linktype set to %d\n",
  1095. tun->dev->type);
  1096. ret = 0;
  1097. }
  1098. break;
  1099. #ifdef TUN_DEBUG
  1100. case TUNSETDEBUG:
  1101. tun->debug = arg;
  1102. break;
  1103. #endif
  1104. case TUNSETOFFLOAD:
  1105. ret = set_offload(tun, arg);
  1106. break;
  1107. case TUNSETTXFILTER:
  1108. /* Can be set only for TAPs */
  1109. ret = -EINVAL;
  1110. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1111. break;
  1112. ret = update_filter(&tun->txflt, (void __user *)arg);
  1113. break;
  1114. case SIOCGIFHWADDR:
  1115. /* Get hw address */
  1116. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  1117. ifr.ifr_hwaddr.sa_family = tun->dev->type;
  1118. if (copy_to_user(argp, &ifr, ifreq_len))
  1119. ret = -EFAULT;
  1120. break;
  1121. case SIOCSIFHWADDR:
  1122. /* Set hw address */
  1123. tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
  1124. ifr.ifr_hwaddr.sa_data);
  1125. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  1126. break;
  1127. case TUNGETSNDBUF:
  1128. sndbuf = tun->socket.sk->sk_sndbuf;
  1129. if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
  1130. ret = -EFAULT;
  1131. break;
  1132. case TUNSETSNDBUF:
  1133. if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
  1134. ret = -EFAULT;
  1135. break;
  1136. }
  1137. tun->socket.sk->sk_sndbuf = sndbuf;
  1138. break;
  1139. case TUNGETVNETHDRSZ:
  1140. vnet_hdr_sz = tun->vnet_hdr_sz;
  1141. if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
  1142. ret = -EFAULT;
  1143. break;
  1144. case TUNSETVNETHDRSZ:
  1145. if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
  1146. ret = -EFAULT;
  1147. break;
  1148. }
  1149. if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
  1150. ret = -EINVAL;
  1151. break;
  1152. }
  1153. tun->vnet_hdr_sz = vnet_hdr_sz;
  1154. break;
  1155. case TUNATTACHFILTER:
  1156. /* Can be set only for TAPs */
  1157. ret = -EINVAL;
  1158. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1159. break;
  1160. ret = -EFAULT;
  1161. if (copy_from_user(&fprog, argp, sizeof(fprog)))
  1162. break;
  1163. ret = sk_attach_filter(&fprog, tun->socket.sk);
  1164. break;
  1165. case TUNDETACHFILTER:
  1166. /* Can be set only for TAPs */
  1167. ret = -EINVAL;
  1168. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1169. break;
  1170. ret = sk_detach_filter(tun->socket.sk);
  1171. break;
  1172. default:
  1173. ret = -EINVAL;
  1174. break;
  1175. }
  1176. unlock:
  1177. rtnl_unlock();
  1178. if (tun)
  1179. tun_put(tun);
  1180. return ret;
  1181. }
  1182. static long tun_chr_ioctl(struct file *file,
  1183. unsigned int cmd, unsigned long arg)
  1184. {
  1185. return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
  1186. }
  1187. #ifdef CONFIG_COMPAT
  1188. static long tun_chr_compat_ioctl(struct file *file,
  1189. unsigned int cmd, unsigned long arg)
  1190. {
  1191. switch (cmd) {
  1192. case TUNSETIFF:
  1193. case TUNGETIFF:
  1194. case TUNSETTXFILTER:
  1195. case TUNGETSNDBUF:
  1196. case TUNSETSNDBUF:
  1197. case SIOCGIFHWADDR:
  1198. case SIOCSIFHWADDR:
  1199. arg = (unsigned long)compat_ptr(arg);
  1200. break;
  1201. default:
  1202. arg = (compat_ulong_t)arg;
  1203. break;
  1204. }
  1205. /*
  1206. * compat_ifreq is shorter than ifreq, so we must not access beyond
  1207. * the end of that structure. All fields that are used in this
  1208. * driver are compatible though, we don't need to convert the
  1209. * contents.
  1210. */
  1211. return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
  1212. }
  1213. #endif /* CONFIG_COMPAT */
  1214. static int tun_chr_fasync(int fd, struct file *file, int on)
  1215. {
  1216. struct tun_struct *tun = tun_get(file);
  1217. int ret;
  1218. if (!tun)
  1219. return -EBADFD;
  1220. tun_debug(KERN_INFO, tun, "tun_chr_fasync %d\n", on);
  1221. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  1222. goto out;
  1223. if (on) {
  1224. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  1225. if (ret)
  1226. goto out;
  1227. tun->flags |= TUN_FASYNC;
  1228. } else
  1229. tun->flags &= ~TUN_FASYNC;
  1230. ret = 0;
  1231. out:
  1232. tun_put(tun);
  1233. return ret;
  1234. }
  1235. static int tun_chr_open(struct inode *inode, struct file * file)
  1236. {
  1237. struct tun_file *tfile;
  1238. DBG1(KERN_INFO, "tunX: tun_chr_open\n");
  1239. tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
  1240. if (!tfile)
  1241. return -ENOMEM;
  1242. atomic_set(&tfile->count, 0);
  1243. tfile->tun = NULL;
  1244. tfile->net = get_net(current->nsproxy->net_ns);
  1245. file->private_data = tfile;
  1246. return 0;
  1247. }
  1248. static int tun_chr_close(struct inode *inode, struct file *file)
  1249. {
  1250. struct tun_file *tfile = file->private_data;
  1251. struct tun_struct *tun;
  1252. tun = __tun_get(tfile);
  1253. if (tun) {
  1254. struct net_device *dev = tun->dev;
  1255. tun_debug(KERN_INFO, tun, "tun_chr_close\n");
  1256. __tun_detach(tun);
  1257. /* If desirable, unregister the netdevice. */
  1258. if (!(tun->flags & TUN_PERSIST)) {
  1259. rtnl_lock();
  1260. if (dev->reg_state == NETREG_REGISTERED)
  1261. unregister_netdevice(dev);
  1262. rtnl_unlock();
  1263. }
  1264. }
  1265. tun = tfile->tun;
  1266. if (tun)
  1267. sock_put(tun->socket.sk);
  1268. put_net(tfile->net);
  1269. kfree(tfile);
  1270. return 0;
  1271. }
  1272. static const struct file_operations tun_fops = {
  1273. .owner = THIS_MODULE,
  1274. .llseek = no_llseek,
  1275. .read = do_sync_read,
  1276. .aio_read = tun_chr_aio_read,
  1277. .write = do_sync_write,
  1278. .aio_write = tun_chr_aio_write,
  1279. .poll = tun_chr_poll,
  1280. .unlocked_ioctl = tun_chr_ioctl,
  1281. #ifdef CONFIG_COMPAT
  1282. .compat_ioctl = tun_chr_compat_ioctl,
  1283. #endif
  1284. .open = tun_chr_open,
  1285. .release = tun_chr_close,
  1286. .fasync = tun_chr_fasync
  1287. };
  1288. static struct miscdevice tun_miscdev = {
  1289. .minor = TUN_MINOR,
  1290. .name = "tun",
  1291. .nodename = "net/tun",
  1292. .fops = &tun_fops,
  1293. };
  1294. /* ethtool interface */
  1295. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1296. {
  1297. cmd->supported = 0;
  1298. cmd->advertising = 0;
  1299. ethtool_cmd_speed_set(cmd, SPEED_10);
  1300. cmd->duplex = DUPLEX_FULL;
  1301. cmd->port = PORT_TP;
  1302. cmd->phy_address = 0;
  1303. cmd->transceiver = XCVR_INTERNAL;
  1304. cmd->autoneg = AUTONEG_DISABLE;
  1305. cmd->maxtxpkt = 0;
  1306. cmd->maxrxpkt = 0;
  1307. return 0;
  1308. }
  1309. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1310. {
  1311. struct tun_struct *tun = netdev_priv(dev);
  1312. strcpy(info->driver, DRV_NAME);
  1313. strcpy(info->version, DRV_VERSION);
  1314. strcpy(info->fw_version, "N/A");
  1315. switch (tun->flags & TUN_TYPE_MASK) {
  1316. case TUN_TUN_DEV:
  1317. strcpy(info->bus_info, "tun");
  1318. break;
  1319. case TUN_TAP_DEV:
  1320. strcpy(info->bus_info, "tap");
  1321. break;
  1322. }
  1323. }
  1324. static u32 tun_get_msglevel(struct net_device *dev)
  1325. {
  1326. #ifdef TUN_DEBUG
  1327. struct tun_struct *tun = netdev_priv(dev);
  1328. return tun->debug;
  1329. #else
  1330. return -EOPNOTSUPP;
  1331. #endif
  1332. }
  1333. static void tun_set_msglevel(struct net_device *dev, u32 value)
  1334. {
  1335. #ifdef TUN_DEBUG
  1336. struct tun_struct *tun = netdev_priv(dev);
  1337. tun->debug = value;
  1338. #endif
  1339. }
  1340. static const struct ethtool_ops tun_ethtool_ops = {
  1341. .get_settings = tun_get_settings,
  1342. .get_drvinfo = tun_get_drvinfo,
  1343. .get_msglevel = tun_get_msglevel,
  1344. .set_msglevel = tun_set_msglevel,
  1345. .get_link = ethtool_op_get_link,
  1346. };
  1347. static int __init tun_init(void)
  1348. {
  1349. int ret = 0;
  1350. pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1351. pr_info("%s\n", DRV_COPYRIGHT);
  1352. ret = rtnl_link_register(&tun_link_ops);
  1353. if (ret) {
  1354. pr_err("Can't register link_ops\n");
  1355. goto err_linkops;
  1356. }
  1357. ret = misc_register(&tun_miscdev);
  1358. if (ret) {
  1359. pr_err("Can't register misc device %d\n", TUN_MINOR);
  1360. goto err_misc;
  1361. }
  1362. return 0;
  1363. err_misc:
  1364. rtnl_link_unregister(&tun_link_ops);
  1365. err_linkops:
  1366. return ret;
  1367. }
  1368. static void tun_cleanup(void)
  1369. {
  1370. misc_deregister(&tun_miscdev);
  1371. rtnl_link_unregister(&tun_link_ops);
  1372. }
  1373. /* Get an underlying socket object from tun file. Returns error unless file is
  1374. * attached to a device. The returned object works like a packet socket, it
  1375. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1376. * holding a reference to the file for as long as the socket is in use. */
  1377. struct socket *tun_get_socket(struct file *file)
  1378. {
  1379. struct tun_struct *tun;
  1380. if (file->f_op != &tun_fops)
  1381. return ERR_PTR(-EINVAL);
  1382. tun = tun_get(file);
  1383. if (!tun)
  1384. return ERR_PTR(-EBADFD);
  1385. tun_put(tun);
  1386. return &tun->socket;
  1387. }
  1388. EXPORT_SYMBOL_GPL(tun_get_socket);
  1389. module_init(tun_init);
  1390. module_exit(tun_cleanup);
  1391. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1392. MODULE_AUTHOR(DRV_COPYRIGHT);
  1393. MODULE_LICENSE("GPL");
  1394. MODULE_ALIAS_MISCDEV(TUN_MINOR);
  1395. MODULE_ALIAS("devname:net/tun");