tun.c 56 KB

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