tun.c 53 KB

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