tun.c 57 KB

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