tun.c 53 KB

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