tun.c 61 KB

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