tun.c 53 KB

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