tun.c 53 KB

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