tun.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * TUN - Universal TUN/TAP device driver.
  4. * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
  5. *
  6. * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
  7. */
  8. /*
  9. * Changes:
  10. *
  11. * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
  12. * Add TUNSETLINK ioctl to set the link encapsulation
  13. *
  14. * Mark Smith <markzzzsmith@yahoo.com.au>
  15. * Use eth_random_addr() for tap MAC address.
  16. *
  17. * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
  18. * Fixes in packet dropping, queue length setting and queue wakeup.
  19. * Increased default tx queue length.
  20. * Added ethtool API.
  21. * Minor cleanups
  22. *
  23. * Daniel Podlejski <underley@underley.eu.org>
  24. * Modifications for 2.3.99-pre5 kernel.
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #define DRV_NAME "tun"
  28. #define DRV_VERSION "1.6"
  29. #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
  30. #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
  31. #include <linux/module.h>
  32. #include <linux/errno.h>
  33. #include <linux/kernel.h>
  34. #include <linux/sched/signal.h>
  35. #include <linux/major.h>
  36. #include <linux/slab.h>
  37. #include <linux/poll.h>
  38. #include <linux/fcntl.h>
  39. #include <linux/init.h>
  40. #include <linux/skbuff.h>
  41. #include <linux/netdevice.h>
  42. #include <linux/etherdevice.h>
  43. #include <linux/miscdevice.h>
  44. #include <linux/ethtool.h>
  45. #include <linux/rtnetlink.h>
  46. #include <linux/compat.h>
  47. #include <linux/if.h>
  48. #include <linux/if_arp.h>
  49. #include <linux/if_ether.h>
  50. #include <linux/if_tun.h>
  51. #include <linux/if_vlan.h>
  52. #include <linux/crc32.h>
  53. #include <linux/nsproxy.h>
  54. #include <linux/virtio_net.h>
  55. #include <linux/rcupdate.h>
  56. #include <net/net_namespace.h>
  57. #include <net/netns/generic.h>
  58. #include <net/rtnetlink.h>
  59. #include <net/sock.h>
  60. #include <net/xdp.h>
  61. #include <linux/seq_file.h>
  62. #include <linux/uio.h>
  63. #include <linux/skb_array.h>
  64. #include <linux/bpf.h>
  65. #include <linux/bpf_trace.h>
  66. #include <linux/mutex.h>
  67. #include <linux/ieee802154.h>
  68. #include <linux/if_ltalk.h>
  69. #include <uapi/linux/if_fddi.h>
  70. #include <uapi/linux/if_hippi.h>
  71. #include <uapi/linux/if_fc.h>
  72. #include <net/ax25.h>
  73. #include <net/rose.h>
  74. #include <net/6lowpan.h>
  75. #include <linux/uaccess.h>
  76. #include <linux/proc_fs.h>
  77. static void tun_default_link_ksettings(struct net_device *dev,
  78. struct ethtool_link_ksettings *cmd);
  79. /* Uncomment to enable debugging */
  80. /* #define TUN_DEBUG 1 */
  81. #ifdef TUN_DEBUG
  82. static int debug;
  83. #define tun_debug(level, tun, fmt, args...) \
  84. do { \
  85. if (tun->debug) \
  86. netdev_printk(level, tun->dev, fmt, ##args); \
  87. } while (0)
  88. #define DBG1(level, fmt, args...) \
  89. do { \
  90. if (debug == 2) \
  91. printk(level fmt, ##args); \
  92. } while (0)
  93. #else
  94. #define tun_debug(level, tun, fmt, args...) \
  95. do { \
  96. if (0) \
  97. netdev_printk(level, tun->dev, fmt, ##args); \
  98. } while (0)
  99. #define DBG1(level, fmt, args...) \
  100. do { \
  101. if (0) \
  102. printk(level fmt, ##args); \
  103. } while (0)
  104. #endif
  105. #define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
  106. /* TUN device flags */
  107. /* IFF_ATTACH_QUEUE is never stored in device flags,
  108. * overload it to mean fasync when stored there.
  109. */
  110. #define TUN_FASYNC IFF_ATTACH_QUEUE
  111. /* High bits in flags field are unused. */
  112. #define TUN_VNET_LE 0x80000000
  113. #define TUN_VNET_BE 0x40000000
  114. #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
  115. IFF_MULTI_QUEUE | IFF_NAPI | IFF_NAPI_FRAGS)
  116. #define GOODCOPY_LEN 128
  117. #define FLT_EXACT_COUNT 8
  118. struct tap_filter {
  119. unsigned int count; /* Number of addrs. Zero means disabled */
  120. u32 mask[2]; /* Mask of the hashed addrs */
  121. unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
  122. };
  123. /* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal
  124. * to max number of VCPUs in guest. */
  125. #define MAX_TAP_QUEUES 256
  126. #define MAX_TAP_FLOWS 4096
  127. #define TUN_FLOW_EXPIRE (3 * HZ)
  128. struct tun_pcpu_stats {
  129. u64 rx_packets;
  130. u64 rx_bytes;
  131. u64 tx_packets;
  132. u64 tx_bytes;
  133. struct u64_stats_sync syncp;
  134. u32 rx_dropped;
  135. u32 tx_dropped;
  136. u32 rx_frame_errors;
  137. };
  138. /* A tun_file connects an open character device to a tuntap netdevice. It
  139. * also contains all socket related structures (except sock_fprog and tap_filter)
  140. * to serve as one transmit queue for tuntap device. The sock_fprog and
  141. * tap_filter were kept in tun_struct since they were used for filtering for the
  142. * netdevice not for a specific queue (at least I didn't see the requirement for
  143. * this).
  144. *
  145. * RCU usage:
  146. * The tun_file and tun_struct are loosely coupled, the pointer from one to the
  147. * other can only be read while rcu_read_lock or rtnl_lock is held.
  148. */
  149. struct tun_file {
  150. struct sock sk;
  151. struct socket socket;
  152. struct tun_struct __rcu *tun;
  153. struct fasync_struct *fasync;
  154. /* only used for fasnyc */
  155. unsigned int flags;
  156. union {
  157. u16 queue_index;
  158. unsigned int ifindex;
  159. };
  160. struct napi_struct napi;
  161. bool napi_enabled;
  162. bool napi_frags_enabled;
  163. struct mutex napi_mutex; /* Protects access to the above napi */
  164. struct list_head next;
  165. struct tun_struct *detached;
  166. struct ptr_ring tx_ring;
  167. struct xdp_rxq_info xdp_rxq;
  168. };
  169. struct tun_page {
  170. struct page *page;
  171. int count;
  172. };
  173. struct tun_flow_entry {
  174. struct hlist_node hash_link;
  175. struct rcu_head rcu;
  176. struct tun_struct *tun;
  177. u32 rxhash;
  178. u32 rps_rxhash;
  179. int queue_index;
  180. unsigned long updated ____cacheline_aligned_in_smp;
  181. };
  182. #define TUN_NUM_FLOW_ENTRIES 1024
  183. #define TUN_MASK_FLOW_ENTRIES (TUN_NUM_FLOW_ENTRIES - 1)
  184. struct tun_prog {
  185. struct rcu_head rcu;
  186. struct bpf_prog *prog;
  187. };
  188. /* Since the socket were moved to tun_file, to preserve the behavior of persist
  189. * device, socket filter, sndbuf and vnet header size were restore when the
  190. * file were attached to a persist device.
  191. */
  192. struct tun_struct {
  193. struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
  194. unsigned int numqueues;
  195. unsigned int flags;
  196. kuid_t owner;
  197. kgid_t group;
  198. struct net_device *dev;
  199. netdev_features_t set_features;
  200. #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
  201. NETIF_F_TSO6)
  202. int align;
  203. int vnet_hdr_sz;
  204. int sndbuf;
  205. struct tap_filter txflt;
  206. struct sock_fprog fprog;
  207. /* protected by rtnl lock */
  208. bool filter_attached;
  209. #ifdef TUN_DEBUG
  210. int debug;
  211. #endif
  212. spinlock_t lock;
  213. struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
  214. struct timer_list flow_gc_timer;
  215. unsigned long ageing_time;
  216. unsigned int numdisabled;
  217. struct list_head disabled;
  218. void *security;
  219. u32 flow_count;
  220. u32 rx_batched;
  221. struct tun_pcpu_stats __percpu *pcpu_stats;
  222. struct bpf_prog __rcu *xdp_prog;
  223. struct tun_prog __rcu *steering_prog;
  224. struct tun_prog __rcu *filter_prog;
  225. struct ethtool_link_ksettings link_ksettings;
  226. };
  227. struct veth {
  228. __be16 h_vlan_proto;
  229. __be16 h_vlan_TCI;
  230. };
  231. bool tun_is_xdp_frame(void *ptr)
  232. {
  233. return (unsigned long)ptr & TUN_XDP_FLAG;
  234. }
  235. EXPORT_SYMBOL(tun_is_xdp_frame);
  236. void *tun_xdp_to_ptr(void *ptr)
  237. {
  238. return (void *)((unsigned long)ptr | TUN_XDP_FLAG);
  239. }
  240. EXPORT_SYMBOL(tun_xdp_to_ptr);
  241. void *tun_ptr_to_xdp(void *ptr)
  242. {
  243. return (void *)((unsigned long)ptr & ~TUN_XDP_FLAG);
  244. }
  245. EXPORT_SYMBOL(tun_ptr_to_xdp);
  246. static int tun_napi_receive(struct napi_struct *napi, int budget)
  247. {
  248. struct tun_file *tfile = container_of(napi, struct tun_file, napi);
  249. struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
  250. struct sk_buff_head process_queue;
  251. struct sk_buff *skb;
  252. int received = 0;
  253. __skb_queue_head_init(&process_queue);
  254. spin_lock(&queue->lock);
  255. skb_queue_splice_tail_init(queue, &process_queue);
  256. spin_unlock(&queue->lock);
  257. while (received < budget && (skb = __skb_dequeue(&process_queue))) {
  258. napi_gro_receive(napi, skb);
  259. ++received;
  260. }
  261. if (!skb_queue_empty(&process_queue)) {
  262. spin_lock(&queue->lock);
  263. skb_queue_splice(&process_queue, queue);
  264. spin_unlock(&queue->lock);
  265. }
  266. return received;
  267. }
  268. static int tun_napi_poll(struct napi_struct *napi, int budget)
  269. {
  270. unsigned int received;
  271. received = tun_napi_receive(napi, budget);
  272. if (received < budget)
  273. napi_complete_done(napi, received);
  274. return received;
  275. }
  276. static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile,
  277. bool napi_en, bool napi_frags)
  278. {
  279. tfile->napi_enabled = napi_en;
  280. tfile->napi_frags_enabled = napi_en && napi_frags;
  281. if (napi_en) {
  282. netif_tx_napi_add(tun->dev, &tfile->napi, tun_napi_poll,
  283. NAPI_POLL_WEIGHT);
  284. napi_enable(&tfile->napi);
  285. }
  286. }
  287. static void tun_napi_disable(struct tun_file *tfile)
  288. {
  289. if (tfile->napi_enabled)
  290. napi_disable(&tfile->napi);
  291. }
  292. static void tun_napi_del(struct tun_file *tfile)
  293. {
  294. if (tfile->napi_enabled)
  295. netif_napi_del(&tfile->napi);
  296. }
  297. static bool tun_napi_frags_enabled(const struct tun_file *tfile)
  298. {
  299. return tfile->napi_frags_enabled;
  300. }
  301. #ifdef CONFIG_TUN_VNET_CROSS_LE
  302. static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
  303. {
  304. return tun->flags & TUN_VNET_BE ? false :
  305. virtio_legacy_is_little_endian();
  306. }
  307. static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
  308. {
  309. int be = !!(tun->flags & TUN_VNET_BE);
  310. if (put_user(be, argp))
  311. return -EFAULT;
  312. return 0;
  313. }
  314. static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
  315. {
  316. int be;
  317. if (get_user(be, argp))
  318. return -EFAULT;
  319. if (be)
  320. tun->flags |= TUN_VNET_BE;
  321. else
  322. tun->flags &= ~TUN_VNET_BE;
  323. return 0;
  324. }
  325. #else
  326. static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
  327. {
  328. return virtio_legacy_is_little_endian();
  329. }
  330. static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
  331. {
  332. return -EINVAL;
  333. }
  334. static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
  335. {
  336. return -EINVAL;
  337. }
  338. #endif /* CONFIG_TUN_VNET_CROSS_LE */
  339. static inline bool tun_is_little_endian(struct tun_struct *tun)
  340. {
  341. return tun->flags & TUN_VNET_LE ||
  342. tun_legacy_is_little_endian(tun);
  343. }
  344. static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
  345. {
  346. return __virtio16_to_cpu(tun_is_little_endian(tun), val);
  347. }
  348. static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
  349. {
  350. return __cpu_to_virtio16(tun_is_little_endian(tun), val);
  351. }
  352. static inline u32 tun_hashfn(u32 rxhash)
  353. {
  354. return rxhash & TUN_MASK_FLOW_ENTRIES;
  355. }
  356. static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
  357. {
  358. struct tun_flow_entry *e;
  359. hlist_for_each_entry_rcu(e, head, hash_link) {
  360. if (e->rxhash == rxhash)
  361. return e;
  362. }
  363. return NULL;
  364. }
  365. static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
  366. struct hlist_head *head,
  367. u32 rxhash, u16 queue_index)
  368. {
  369. struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
  370. if (e) {
  371. tun_debug(KERN_INFO, tun, "create flow: hash %u index %u\n",
  372. rxhash, queue_index);
  373. e->updated = jiffies;
  374. e->rxhash = rxhash;
  375. e->rps_rxhash = 0;
  376. e->queue_index = queue_index;
  377. e->tun = tun;
  378. hlist_add_head_rcu(&e->hash_link, head);
  379. ++tun->flow_count;
  380. }
  381. return e;
  382. }
  383. static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
  384. {
  385. tun_debug(KERN_INFO, tun, "delete flow: hash %u index %u\n",
  386. e->rxhash, e->queue_index);
  387. hlist_del_rcu(&e->hash_link);
  388. kfree_rcu(e, rcu);
  389. --tun->flow_count;
  390. }
  391. static void tun_flow_flush(struct tun_struct *tun)
  392. {
  393. int i;
  394. spin_lock_bh(&tun->lock);
  395. for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
  396. struct tun_flow_entry *e;
  397. struct hlist_node *n;
  398. hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
  399. tun_flow_delete(tun, e);
  400. }
  401. spin_unlock_bh(&tun->lock);
  402. }
  403. static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
  404. {
  405. int i;
  406. spin_lock_bh(&tun->lock);
  407. for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
  408. struct tun_flow_entry *e;
  409. struct hlist_node *n;
  410. hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
  411. if (e->queue_index == queue_index)
  412. tun_flow_delete(tun, e);
  413. }
  414. }
  415. spin_unlock_bh(&tun->lock);
  416. }
  417. static void tun_flow_cleanup(struct timer_list *t)
  418. {
  419. struct tun_struct *tun = from_timer(tun, t, flow_gc_timer);
  420. unsigned long delay = tun->ageing_time;
  421. unsigned long next_timer = jiffies + delay;
  422. unsigned long count = 0;
  423. int i;
  424. tun_debug(KERN_INFO, tun, "tun_flow_cleanup\n");
  425. spin_lock(&tun->lock);
  426. for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
  427. struct tun_flow_entry *e;
  428. struct hlist_node *n;
  429. hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
  430. unsigned long this_timer;
  431. this_timer = e->updated + delay;
  432. if (time_before_eq(this_timer, jiffies)) {
  433. tun_flow_delete(tun, e);
  434. continue;
  435. }
  436. count++;
  437. if (time_before(this_timer, next_timer))
  438. next_timer = this_timer;
  439. }
  440. }
  441. if (count)
  442. mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
  443. spin_unlock(&tun->lock);
  444. }
  445. static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
  446. struct tun_file *tfile)
  447. {
  448. struct hlist_head *head;
  449. struct tun_flow_entry *e;
  450. unsigned long delay = tun->ageing_time;
  451. u16 queue_index = tfile->queue_index;
  452. head = &tun->flows[tun_hashfn(rxhash)];
  453. rcu_read_lock();
  454. e = tun_flow_find(head, rxhash);
  455. if (likely(e)) {
  456. /* TODO: keep queueing to old queue until it's empty? */
  457. if (READ_ONCE(e->queue_index) != queue_index)
  458. WRITE_ONCE(e->queue_index, queue_index);
  459. if (e->updated != jiffies)
  460. e->updated = jiffies;
  461. sock_rps_record_flow_hash(e->rps_rxhash);
  462. } else {
  463. spin_lock_bh(&tun->lock);
  464. if (!tun_flow_find(head, rxhash) &&
  465. tun->flow_count < MAX_TAP_FLOWS)
  466. tun_flow_create(tun, head, rxhash, queue_index);
  467. if (!timer_pending(&tun->flow_gc_timer))
  468. mod_timer(&tun->flow_gc_timer,
  469. round_jiffies_up(jiffies + delay));
  470. spin_unlock_bh(&tun->lock);
  471. }
  472. rcu_read_unlock();
  473. }
  474. /**
  475. * Save the hash received in the stack receive path and update the
  476. * flow_hash table accordingly.
  477. */
  478. static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
  479. {
  480. if (unlikely(e->rps_rxhash != hash))
  481. e->rps_rxhash = hash;
  482. }
  483. /* We try to identify a flow through its rxhash. The reason that
  484. * we do not check rxq no. is because some cards(e.g 82599), chooses
  485. * the rxq based on the txq where the last packet of the flow comes. As
  486. * the userspace application move between processors, we may get a
  487. * different rxq no. here.
  488. */
  489. static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
  490. {
  491. struct tun_flow_entry *e;
  492. u32 txq = 0;
  493. u32 numqueues = 0;
  494. numqueues = READ_ONCE(tun->numqueues);
  495. txq = __skb_get_hash_symmetric(skb);
  496. e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
  497. if (e) {
  498. tun_flow_save_rps_rxhash(e, txq);
  499. txq = e->queue_index;
  500. } else {
  501. /* use multiply and shift instead of expensive divide */
  502. txq = ((u64)txq * numqueues) >> 32;
  503. }
  504. return txq;
  505. }
  506. static u16 tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb)
  507. {
  508. struct tun_prog *prog;
  509. u32 numqueues;
  510. u16 ret = 0;
  511. numqueues = READ_ONCE(tun->numqueues);
  512. if (!numqueues)
  513. return 0;
  514. prog = rcu_dereference(tun->steering_prog);
  515. if (prog)
  516. ret = bpf_prog_run_clear_cb(prog->prog, skb);
  517. return ret % numqueues;
  518. }
  519. static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
  520. struct net_device *sb_dev)
  521. {
  522. struct tun_struct *tun = netdev_priv(dev);
  523. u16 ret;
  524. rcu_read_lock();
  525. if (rcu_dereference(tun->steering_prog))
  526. ret = tun_ebpf_select_queue(tun, skb);
  527. else
  528. ret = tun_automq_select_queue(tun, skb);
  529. rcu_read_unlock();
  530. return ret;
  531. }
  532. static inline bool tun_not_capable(struct tun_struct *tun)
  533. {
  534. const struct cred *cred = current_cred();
  535. struct net *net = dev_net(tun->dev);
  536. return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
  537. (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
  538. !ns_capable(net->user_ns, CAP_NET_ADMIN);
  539. }
  540. static void tun_set_real_num_queues(struct tun_struct *tun)
  541. {
  542. netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
  543. netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
  544. }
  545. static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
  546. {
  547. tfile->detached = tun;
  548. list_add_tail(&tfile->next, &tun->disabled);
  549. ++tun->numdisabled;
  550. }
  551. static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
  552. {
  553. struct tun_struct *tun = tfile->detached;
  554. tfile->detached = NULL;
  555. list_del_init(&tfile->next);
  556. --tun->numdisabled;
  557. return tun;
  558. }
  559. void tun_ptr_free(void *ptr)
  560. {
  561. if (!ptr)
  562. return;
  563. if (tun_is_xdp_frame(ptr)) {
  564. struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
  565. xdp_return_frame(xdpf);
  566. } else {
  567. __skb_array_destroy_skb(ptr);
  568. }
  569. }
  570. EXPORT_SYMBOL_GPL(tun_ptr_free);
  571. static void tun_queue_purge(struct tun_file *tfile)
  572. {
  573. void *ptr;
  574. while ((ptr = ptr_ring_consume(&tfile->tx_ring)) != NULL)
  575. tun_ptr_free(ptr);
  576. skb_queue_purge(&tfile->sk.sk_write_queue);
  577. skb_queue_purge(&tfile->sk.sk_error_queue);
  578. }
  579. static void __tun_detach(struct tun_file *tfile, bool clean)
  580. {
  581. struct tun_file *ntfile;
  582. struct tun_struct *tun;
  583. tun = rtnl_dereference(tfile->tun);
  584. if (tun && clean) {
  585. tun_napi_disable(tfile);
  586. tun_napi_del(tfile);
  587. }
  588. if (tun && !tfile->detached) {
  589. u16 index = tfile->queue_index;
  590. BUG_ON(index >= tun->numqueues);
  591. rcu_assign_pointer(tun->tfiles[index],
  592. tun->tfiles[tun->numqueues - 1]);
  593. ntfile = rtnl_dereference(tun->tfiles[index]);
  594. ntfile->queue_index = index;
  595. rcu_assign_pointer(tun->tfiles[tun->numqueues - 1],
  596. NULL);
  597. --tun->numqueues;
  598. if (clean) {
  599. RCU_INIT_POINTER(tfile->tun, NULL);
  600. sock_put(&tfile->sk);
  601. } else
  602. tun_disable_queue(tun, tfile);
  603. synchronize_net();
  604. tun_flow_delete_by_queue(tun, tun->numqueues + 1);
  605. /* Drop read queue */
  606. tun_queue_purge(tfile);
  607. tun_set_real_num_queues(tun);
  608. } else if (tfile->detached && clean) {
  609. tun = tun_enable_queue(tfile);
  610. sock_put(&tfile->sk);
  611. }
  612. if (clean) {
  613. if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
  614. netif_carrier_off(tun->dev);
  615. if (!(tun->flags & IFF_PERSIST) &&
  616. tun->dev->reg_state == NETREG_REGISTERED)
  617. unregister_netdevice(tun->dev);
  618. }
  619. if (tun)
  620. xdp_rxq_info_unreg(&tfile->xdp_rxq);
  621. ptr_ring_cleanup(&tfile->tx_ring, tun_ptr_free);
  622. sock_put(&tfile->sk);
  623. }
  624. }
  625. static void tun_detach(struct tun_file *tfile, bool clean)
  626. {
  627. struct tun_struct *tun;
  628. struct net_device *dev;
  629. rtnl_lock();
  630. tun = rtnl_dereference(tfile->tun);
  631. dev = tun ? tun->dev : NULL;
  632. __tun_detach(tfile, clean);
  633. if (dev)
  634. netdev_state_change(dev);
  635. rtnl_unlock();
  636. }
  637. static void tun_detach_all(struct net_device *dev)
  638. {
  639. struct tun_struct *tun = netdev_priv(dev);
  640. struct tun_file *tfile, *tmp;
  641. int i, n = tun->numqueues;
  642. for (i = 0; i < n; i++) {
  643. tfile = rtnl_dereference(tun->tfiles[i]);
  644. BUG_ON(!tfile);
  645. tun_napi_disable(tfile);
  646. tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
  647. tfile->socket.sk->sk_data_ready(tfile->socket.sk);
  648. RCU_INIT_POINTER(tfile->tun, NULL);
  649. --tun->numqueues;
  650. }
  651. list_for_each_entry(tfile, &tun->disabled, next) {
  652. tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
  653. tfile->socket.sk->sk_data_ready(tfile->socket.sk);
  654. RCU_INIT_POINTER(tfile->tun, NULL);
  655. }
  656. BUG_ON(tun->numqueues != 0);
  657. synchronize_net();
  658. for (i = 0; i < n; i++) {
  659. tfile = rtnl_dereference(tun->tfiles[i]);
  660. tun_napi_del(tfile);
  661. /* Drop read queue */
  662. tun_queue_purge(tfile);
  663. xdp_rxq_info_unreg(&tfile->xdp_rxq);
  664. sock_put(&tfile->sk);
  665. }
  666. list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
  667. tun_enable_queue(tfile);
  668. tun_queue_purge(tfile);
  669. xdp_rxq_info_unreg(&tfile->xdp_rxq);
  670. sock_put(&tfile->sk);
  671. }
  672. BUG_ON(tun->numdisabled != 0);
  673. if (tun->flags & IFF_PERSIST)
  674. module_put(THIS_MODULE);
  675. }
  676. static int tun_attach(struct tun_struct *tun, struct file *file,
  677. bool skip_filter, bool napi, bool napi_frags,
  678. bool publish_tun)
  679. {
  680. struct tun_file *tfile = file->private_data;
  681. struct net_device *dev = tun->dev;
  682. int err;
  683. err = security_tun_dev_attach(tfile->socket.sk, tun->security);
  684. if (err < 0)
  685. goto out;
  686. err = -EINVAL;
  687. if (rtnl_dereference(tfile->tun) && !tfile->detached)
  688. goto out;
  689. err = -EBUSY;
  690. if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
  691. goto out;
  692. err = -E2BIG;
  693. if (!tfile->detached &&
  694. tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
  695. goto out;
  696. err = 0;
  697. /* Re-attach the filter to persist device */
  698. if (!skip_filter && (tun->filter_attached == true)) {
  699. lock_sock(tfile->socket.sk);
  700. err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
  701. release_sock(tfile->socket.sk);
  702. if (!err)
  703. goto out;
  704. }
  705. if (!tfile->detached &&
  706. ptr_ring_resize(&tfile->tx_ring, dev->tx_queue_len,
  707. GFP_KERNEL, tun_ptr_free)) {
  708. err = -ENOMEM;
  709. goto out;
  710. }
  711. tfile->queue_index = tun->numqueues;
  712. tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
  713. if (tfile->detached) {
  714. /* Re-attach detached tfile, updating XDP queue_index */
  715. WARN_ON(!xdp_rxq_info_is_reg(&tfile->xdp_rxq));
  716. if (tfile->xdp_rxq.queue_index != tfile->queue_index)
  717. tfile->xdp_rxq.queue_index = tfile->queue_index;
  718. } else {
  719. /* Setup XDP RX-queue info, for new tfile getting attached */
  720. err = xdp_rxq_info_reg(&tfile->xdp_rxq,
  721. tun->dev, tfile->queue_index);
  722. if (err < 0)
  723. goto out;
  724. err = xdp_rxq_info_reg_mem_model(&tfile->xdp_rxq,
  725. MEM_TYPE_PAGE_SHARED, NULL);
  726. if (err < 0) {
  727. xdp_rxq_info_unreg(&tfile->xdp_rxq);
  728. goto out;
  729. }
  730. err = 0;
  731. }
  732. if (tfile->detached) {
  733. tun_enable_queue(tfile);
  734. } else {
  735. sock_hold(&tfile->sk);
  736. tun_napi_init(tun, tfile, napi, napi_frags);
  737. }
  738. if (rtnl_dereference(tun->xdp_prog))
  739. sock_set_flag(&tfile->sk, SOCK_XDP);
  740. /* device is allowed to go away first, so no need to hold extra
  741. * refcnt.
  742. */
  743. /* Publish tfile->tun and tun->tfiles only after we've fully
  744. * initialized tfile; otherwise we risk using half-initialized
  745. * object.
  746. */
  747. if (publish_tun)
  748. rcu_assign_pointer(tfile->tun, tun);
  749. rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
  750. tun->numqueues++;
  751. tun_set_real_num_queues(tun);
  752. out:
  753. return err;
  754. }
  755. static struct tun_struct *tun_get(struct tun_file *tfile)
  756. {
  757. struct tun_struct *tun;
  758. rcu_read_lock();
  759. tun = rcu_dereference(tfile->tun);
  760. if (tun)
  761. dev_hold(tun->dev);
  762. rcu_read_unlock();
  763. return tun;
  764. }
  765. static void tun_put(struct tun_struct *tun)
  766. {
  767. dev_put(tun->dev);
  768. }
  769. /* TAP filtering */
  770. static void addr_hash_set(u32 *mask, const u8 *addr)
  771. {
  772. int n = ether_crc(ETH_ALEN, addr) >> 26;
  773. mask[n >> 5] |= (1 << (n & 31));
  774. }
  775. static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
  776. {
  777. int n = ether_crc(ETH_ALEN, addr) >> 26;
  778. return mask[n >> 5] & (1 << (n & 31));
  779. }
  780. static int update_filter(struct tap_filter *filter, void __user *arg)
  781. {
  782. struct { u8 u[ETH_ALEN]; } *addr;
  783. struct tun_filter uf;
  784. int err, alen, n, nexact;
  785. if (copy_from_user(&uf, arg, sizeof(uf)))
  786. return -EFAULT;
  787. if (!uf.count) {
  788. /* Disabled */
  789. filter->count = 0;
  790. return 0;
  791. }
  792. alen = ETH_ALEN * uf.count;
  793. addr = memdup_user(arg + sizeof(uf), alen);
  794. if (IS_ERR(addr))
  795. return PTR_ERR(addr);
  796. /* The filter is updated without holding any locks. Which is
  797. * perfectly safe. We disable it first and in the worst
  798. * case we'll accept a few undesired packets. */
  799. filter->count = 0;
  800. wmb();
  801. /* Use first set of addresses as an exact filter */
  802. for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
  803. memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
  804. nexact = n;
  805. /* Remaining multicast addresses are hashed,
  806. * unicast will leave the filter disabled. */
  807. memset(filter->mask, 0, sizeof(filter->mask));
  808. for (; n < uf.count; n++) {
  809. if (!is_multicast_ether_addr(addr[n].u)) {
  810. err = 0; /* no filter */
  811. goto free_addr;
  812. }
  813. addr_hash_set(filter->mask, addr[n].u);
  814. }
  815. /* For ALLMULTI just set the mask to all ones.
  816. * This overrides the mask populated above. */
  817. if ((uf.flags & TUN_FLT_ALLMULTI))
  818. memset(filter->mask, ~0, sizeof(filter->mask));
  819. /* Now enable the filter */
  820. wmb();
  821. filter->count = nexact;
  822. /* Return the number of exact filters */
  823. err = nexact;
  824. free_addr:
  825. kfree(addr);
  826. return err;
  827. }
  828. /* Returns: 0 - drop, !=0 - accept */
  829. static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
  830. {
  831. /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
  832. * at this point. */
  833. struct ethhdr *eh = (struct ethhdr *) skb->data;
  834. int i;
  835. /* Exact match */
  836. for (i = 0; i < filter->count; i++)
  837. if (ether_addr_equal(eh->h_dest, filter->addr[i]))
  838. return 1;
  839. /* Inexact match (multicast only) */
  840. if (is_multicast_ether_addr(eh->h_dest))
  841. return addr_hash_test(filter->mask, eh->h_dest);
  842. return 0;
  843. }
  844. /*
  845. * Checks whether the packet is accepted or not.
  846. * Returns: 0 - drop, !=0 - accept
  847. */
  848. static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
  849. {
  850. if (!filter->count)
  851. return 1;
  852. return run_filter(filter, skb);
  853. }
  854. /* Network device part of the driver */
  855. static const struct ethtool_ops tun_ethtool_ops;
  856. /* Net device detach from fd. */
  857. static void tun_net_uninit(struct net_device *dev)
  858. {
  859. tun_detach_all(dev);
  860. }
  861. /* Net device open. */
  862. static int tun_net_open(struct net_device *dev)
  863. {
  864. netif_tx_start_all_queues(dev);
  865. return 0;
  866. }
  867. /* Net device close. */
  868. static int tun_net_close(struct net_device *dev)
  869. {
  870. netif_tx_stop_all_queues(dev);
  871. return 0;
  872. }
  873. /* Net device start xmit */
  874. static void tun_automq_xmit(struct tun_struct *tun, struct sk_buff *skb)
  875. {
  876. #ifdef CONFIG_RPS
  877. if (tun->numqueues == 1 && static_branch_unlikely(&rps_needed)) {
  878. /* Select queue was not called for the skbuff, so we extract the
  879. * RPS hash and save it into the flow_table here.
  880. */
  881. struct tun_flow_entry *e;
  882. __u32 rxhash;
  883. rxhash = __skb_get_hash_symmetric(skb);
  884. e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], rxhash);
  885. if (e)
  886. tun_flow_save_rps_rxhash(e, rxhash);
  887. }
  888. #endif
  889. }
  890. static unsigned int run_ebpf_filter(struct tun_struct *tun,
  891. struct sk_buff *skb,
  892. int len)
  893. {
  894. struct tun_prog *prog = rcu_dereference(tun->filter_prog);
  895. if (prog)
  896. len = bpf_prog_run_clear_cb(prog->prog, skb);
  897. return len;
  898. }
  899. /* Net device start xmit */
  900. static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
  901. {
  902. struct tun_struct *tun = netdev_priv(dev);
  903. int txq = skb->queue_mapping;
  904. struct tun_file *tfile;
  905. int len = skb->len;
  906. rcu_read_lock();
  907. tfile = rcu_dereference(tun->tfiles[txq]);
  908. /* Drop packet if interface is not attached */
  909. if (!tfile)
  910. goto drop;
  911. if (!rcu_dereference(tun->steering_prog))
  912. tun_automq_xmit(tun, skb);
  913. tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
  914. BUG_ON(!tfile);
  915. /* Drop if the filter does not like it.
  916. * This is a noop if the filter is disabled.
  917. * Filter can be enabled only for the TAP devices. */
  918. if (!check_filter(&tun->txflt, skb))
  919. goto drop;
  920. if (tfile->socket.sk->sk_filter &&
  921. sk_filter(tfile->socket.sk, skb))
  922. goto drop;
  923. len = run_ebpf_filter(tun, skb, len);
  924. if (len == 0 || pskb_trim(skb, len))
  925. goto drop;
  926. if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
  927. goto drop;
  928. skb_tx_timestamp(skb);
  929. /* Orphan the skb - required as we might hang on to it
  930. * for indefinite time.
  931. */
  932. skb_orphan(skb);
  933. nf_reset_ct(skb);
  934. if (ptr_ring_produce(&tfile->tx_ring, skb))
  935. goto drop;
  936. /* Notify and wake up reader process */
  937. if (tfile->flags & TUN_FASYNC)
  938. kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
  939. tfile->socket.sk->sk_data_ready(tfile->socket.sk);
  940. rcu_read_unlock();
  941. return NETDEV_TX_OK;
  942. drop:
  943. this_cpu_inc(tun->pcpu_stats->tx_dropped);
  944. skb_tx_error(skb);
  945. kfree_skb(skb);
  946. rcu_read_unlock();
  947. return NET_XMIT_DROP;
  948. }
  949. static void tun_net_mclist(struct net_device *dev)
  950. {
  951. /*
  952. * This callback is supposed to deal with mc filter in
  953. * _rx_ path and has nothing to do with the _tx_ path.
  954. * In rx path we always accept everything userspace gives us.
  955. */
  956. }
  957. static netdev_features_t tun_net_fix_features(struct net_device *dev,
  958. netdev_features_t features)
  959. {
  960. struct tun_struct *tun = netdev_priv(dev);
  961. return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
  962. }
  963. static void tun_set_headroom(struct net_device *dev, int new_hr)
  964. {
  965. struct tun_struct *tun = netdev_priv(dev);
  966. if (new_hr < NET_SKB_PAD)
  967. new_hr = NET_SKB_PAD;
  968. tun->align = new_hr;
  969. }
  970. static void
  971. tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  972. {
  973. u32 rx_dropped = 0, tx_dropped = 0, rx_frame_errors = 0;
  974. struct tun_struct *tun = netdev_priv(dev);
  975. struct tun_pcpu_stats *p;
  976. int i;
  977. for_each_possible_cpu(i) {
  978. u64 rxpackets, rxbytes, txpackets, txbytes;
  979. unsigned int start;
  980. p = per_cpu_ptr(tun->pcpu_stats, i);
  981. do {
  982. start = u64_stats_fetch_begin(&p->syncp);
  983. rxpackets = p->rx_packets;
  984. rxbytes = p->rx_bytes;
  985. txpackets = p->tx_packets;
  986. txbytes = p->tx_bytes;
  987. } while (u64_stats_fetch_retry(&p->syncp, start));
  988. stats->rx_packets += rxpackets;
  989. stats->rx_bytes += rxbytes;
  990. stats->tx_packets += txpackets;
  991. stats->tx_bytes += txbytes;
  992. /* u32 counters */
  993. rx_dropped += p->rx_dropped;
  994. rx_frame_errors += p->rx_frame_errors;
  995. tx_dropped += p->tx_dropped;
  996. }
  997. stats->rx_dropped = rx_dropped;
  998. stats->rx_frame_errors = rx_frame_errors;
  999. stats->tx_dropped = tx_dropped;
  1000. }
  1001. static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog,
  1002. struct netlink_ext_ack *extack)
  1003. {
  1004. struct tun_struct *tun = netdev_priv(dev);
  1005. struct tun_file *tfile;
  1006. struct bpf_prog *old_prog;
  1007. int i;
  1008. old_prog = rtnl_dereference(tun->xdp_prog);
  1009. rcu_assign_pointer(tun->xdp_prog, prog);
  1010. if (old_prog)
  1011. bpf_prog_put(old_prog);
  1012. for (i = 0; i < tun->numqueues; i++) {
  1013. tfile = rtnl_dereference(tun->tfiles[i]);
  1014. if (prog)
  1015. sock_set_flag(&tfile->sk, SOCK_XDP);
  1016. else
  1017. sock_reset_flag(&tfile->sk, SOCK_XDP);
  1018. }
  1019. list_for_each_entry(tfile, &tun->disabled, next) {
  1020. if (prog)
  1021. sock_set_flag(&tfile->sk, SOCK_XDP);
  1022. else
  1023. sock_reset_flag(&tfile->sk, SOCK_XDP);
  1024. }
  1025. return 0;
  1026. }
  1027. static u32 tun_xdp_query(struct net_device *dev)
  1028. {
  1029. struct tun_struct *tun = netdev_priv(dev);
  1030. const struct bpf_prog *xdp_prog;
  1031. xdp_prog = rtnl_dereference(tun->xdp_prog);
  1032. if (xdp_prog)
  1033. return xdp_prog->aux->id;
  1034. return 0;
  1035. }
  1036. static int tun_xdp(struct net_device *dev, struct netdev_bpf *xdp)
  1037. {
  1038. switch (xdp->command) {
  1039. case XDP_SETUP_PROG:
  1040. return tun_xdp_set(dev, xdp->prog, xdp->extack);
  1041. case XDP_QUERY_PROG:
  1042. xdp->prog_id = tun_xdp_query(dev);
  1043. return 0;
  1044. default:
  1045. return -EINVAL;
  1046. }
  1047. }
  1048. static int tun_net_change_carrier(struct net_device *dev, bool new_carrier)
  1049. {
  1050. if (new_carrier) {
  1051. struct tun_struct *tun = netdev_priv(dev);
  1052. if (!tun->numqueues)
  1053. return -EPERM;
  1054. netif_carrier_on(dev);
  1055. } else {
  1056. netif_carrier_off(dev);
  1057. }
  1058. return 0;
  1059. }
  1060. static const struct net_device_ops tun_netdev_ops = {
  1061. .ndo_uninit = tun_net_uninit,
  1062. .ndo_open = tun_net_open,
  1063. .ndo_stop = tun_net_close,
  1064. .ndo_start_xmit = tun_net_xmit,
  1065. .ndo_fix_features = tun_net_fix_features,
  1066. .ndo_select_queue = tun_select_queue,
  1067. .ndo_set_rx_headroom = tun_set_headroom,
  1068. .ndo_get_stats64 = tun_net_get_stats64,
  1069. .ndo_change_carrier = tun_net_change_carrier,
  1070. };
  1071. static void __tun_xdp_flush_tfile(struct tun_file *tfile)
  1072. {
  1073. /* Notify and wake up reader process */
  1074. if (tfile->flags & TUN_FASYNC)
  1075. kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
  1076. tfile->socket.sk->sk_data_ready(tfile->socket.sk);
  1077. }
  1078. static int tun_xdp_xmit(struct net_device *dev, int n,
  1079. struct xdp_frame **frames, u32 flags)
  1080. {
  1081. struct tun_struct *tun = netdev_priv(dev);
  1082. struct tun_file *tfile;
  1083. u32 numqueues;
  1084. int drops = 0;
  1085. int cnt = n;
  1086. int i;
  1087. if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
  1088. return -EINVAL;
  1089. rcu_read_lock();
  1090. resample:
  1091. numqueues = READ_ONCE(tun->numqueues);
  1092. if (!numqueues) {
  1093. rcu_read_unlock();
  1094. return -ENXIO; /* Caller will free/return all frames */
  1095. }
  1096. tfile = rcu_dereference(tun->tfiles[smp_processor_id() %
  1097. numqueues]);
  1098. if (unlikely(!tfile))
  1099. goto resample;
  1100. spin_lock(&tfile->tx_ring.producer_lock);
  1101. for (i = 0; i < n; i++) {
  1102. struct xdp_frame *xdp = frames[i];
  1103. /* Encode the XDP flag into lowest bit for consumer to differ
  1104. * XDP buffer from sk_buff.
  1105. */
  1106. void *frame = tun_xdp_to_ptr(xdp);
  1107. if (__ptr_ring_produce(&tfile->tx_ring, frame)) {
  1108. this_cpu_inc(tun->pcpu_stats->tx_dropped);
  1109. xdp_return_frame_rx_napi(xdp);
  1110. drops++;
  1111. }
  1112. }
  1113. spin_unlock(&tfile->tx_ring.producer_lock);
  1114. if (flags & XDP_XMIT_FLUSH)
  1115. __tun_xdp_flush_tfile(tfile);
  1116. rcu_read_unlock();
  1117. return cnt - drops;
  1118. }
  1119. static int tun_xdp_tx(struct net_device *dev, struct xdp_buff *xdp)
  1120. {
  1121. struct xdp_frame *frame = convert_to_xdp_frame(xdp);
  1122. if (unlikely(!frame))
  1123. return -EOVERFLOW;
  1124. return tun_xdp_xmit(dev, 1, &frame, XDP_XMIT_FLUSH);
  1125. }
  1126. static const struct net_device_ops tap_netdev_ops = {
  1127. .ndo_uninit = tun_net_uninit,
  1128. .ndo_open = tun_net_open,
  1129. .ndo_stop = tun_net_close,
  1130. .ndo_start_xmit = tun_net_xmit,
  1131. .ndo_fix_features = tun_net_fix_features,
  1132. .ndo_set_rx_mode = tun_net_mclist,
  1133. .ndo_set_mac_address = eth_mac_addr,
  1134. .ndo_validate_addr = eth_validate_addr,
  1135. .ndo_select_queue = tun_select_queue,
  1136. .ndo_features_check = passthru_features_check,
  1137. .ndo_set_rx_headroom = tun_set_headroom,
  1138. .ndo_get_stats64 = tun_net_get_stats64,
  1139. .ndo_bpf = tun_xdp,
  1140. .ndo_xdp_xmit = tun_xdp_xmit,
  1141. .ndo_change_carrier = tun_net_change_carrier,
  1142. };
  1143. static void tun_flow_init(struct tun_struct *tun)
  1144. {
  1145. int i;
  1146. for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
  1147. INIT_HLIST_HEAD(&tun->flows[i]);
  1148. tun->ageing_time = TUN_FLOW_EXPIRE;
  1149. timer_setup(&tun->flow_gc_timer, tun_flow_cleanup, 0);
  1150. mod_timer(&tun->flow_gc_timer,
  1151. round_jiffies_up(jiffies + tun->ageing_time));
  1152. }
  1153. static void tun_flow_uninit(struct tun_struct *tun)
  1154. {
  1155. del_timer_sync(&tun->flow_gc_timer);
  1156. tun_flow_flush(tun);
  1157. }
  1158. #define MIN_MTU 68
  1159. #define MAX_MTU 65535
  1160. /* Initialize net device. */
  1161. static void tun_net_init(struct net_device *dev)
  1162. {
  1163. struct tun_struct *tun = netdev_priv(dev);
  1164. switch (tun->flags & TUN_TYPE_MASK) {
  1165. case IFF_TUN:
  1166. dev->netdev_ops = &tun_netdev_ops;
  1167. /* Point-to-Point TUN Device */
  1168. dev->hard_header_len = 0;
  1169. dev->addr_len = 0;
  1170. dev->mtu = 1500;
  1171. /* Zero header length */
  1172. dev->type = ARPHRD_NONE;
  1173. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  1174. break;
  1175. case IFF_TAP:
  1176. dev->netdev_ops = &tap_netdev_ops;
  1177. /* Ethernet TAP Device */
  1178. ether_setup(dev);
  1179. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  1180. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1181. eth_hw_addr_random(dev);
  1182. break;
  1183. }
  1184. dev->min_mtu = MIN_MTU;
  1185. dev->max_mtu = MAX_MTU - dev->hard_header_len;
  1186. }
  1187. static bool tun_sock_writeable(struct tun_struct *tun, struct tun_file *tfile)
  1188. {
  1189. struct sock *sk = tfile->socket.sk;
  1190. return (tun->dev->flags & IFF_UP) && sock_writeable(sk);
  1191. }
  1192. /* Character device part */
  1193. /* Poll */
  1194. static __poll_t tun_chr_poll(struct file *file, poll_table *wait)
  1195. {
  1196. struct tun_file *tfile = file->private_data;
  1197. struct tun_struct *tun = tun_get(tfile);
  1198. struct sock *sk;
  1199. __poll_t mask = 0;
  1200. if (!tun)
  1201. return EPOLLERR;
  1202. sk = tfile->socket.sk;
  1203. tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
  1204. poll_wait(file, sk_sleep(sk), wait);
  1205. if (!ptr_ring_empty(&tfile->tx_ring))
  1206. mask |= EPOLLIN | EPOLLRDNORM;
  1207. /* Make sure SOCKWQ_ASYNC_NOSPACE is set if not writable to
  1208. * guarantee EPOLLOUT to be raised by either here or
  1209. * tun_sock_write_space(). Then process could get notification
  1210. * after it writes to a down device and meets -EIO.
  1211. */
  1212. if (tun_sock_writeable(tun, tfile) ||
  1213. (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
  1214. tun_sock_writeable(tun, tfile)))
  1215. mask |= EPOLLOUT | EPOLLWRNORM;
  1216. if (tun->dev->reg_state != NETREG_REGISTERED)
  1217. mask = EPOLLERR;
  1218. tun_put(tun);
  1219. return mask;
  1220. }
  1221. static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile,
  1222. size_t len,
  1223. const struct iov_iter *it)
  1224. {
  1225. struct sk_buff *skb;
  1226. size_t linear;
  1227. int err;
  1228. int i;
  1229. if (it->nr_segs > MAX_SKB_FRAGS + 1)
  1230. return ERR_PTR(-EMSGSIZE);
  1231. local_bh_disable();
  1232. skb = napi_get_frags(&tfile->napi);
  1233. local_bh_enable();
  1234. if (!skb)
  1235. return ERR_PTR(-ENOMEM);
  1236. linear = iov_iter_single_seg_count(it);
  1237. err = __skb_grow(skb, linear);
  1238. if (err)
  1239. goto free;
  1240. skb->len = len;
  1241. skb->data_len = len - linear;
  1242. skb->truesize += skb->data_len;
  1243. for (i = 1; i < it->nr_segs; i++) {
  1244. size_t fragsz = it->iov[i].iov_len;
  1245. struct page *page;
  1246. void *frag;
  1247. if (fragsz == 0 || fragsz > PAGE_SIZE) {
  1248. err = -EINVAL;
  1249. goto free;
  1250. }
  1251. frag = netdev_alloc_frag(fragsz);
  1252. if (!frag) {
  1253. err = -ENOMEM;
  1254. goto free;
  1255. }
  1256. page = virt_to_head_page(frag);
  1257. skb_fill_page_desc(skb, i - 1, page,
  1258. frag - page_address(page), fragsz);
  1259. }
  1260. return skb;
  1261. free:
  1262. /* frees skb and all frags allocated with napi_alloc_frag() */
  1263. napi_free_frags(&tfile->napi);
  1264. return ERR_PTR(err);
  1265. }
  1266. /* prepad is the amount to reserve at front. len is length after that.
  1267. * linear is a hint as to how much to copy (usually headers). */
  1268. static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
  1269. size_t prepad, size_t len,
  1270. size_t linear, int noblock)
  1271. {
  1272. struct sock *sk = tfile->socket.sk;
  1273. struct sk_buff *skb;
  1274. int err;
  1275. /* Under a page? Don't bother with paged skb. */
  1276. if (prepad + len < PAGE_SIZE || !linear)
  1277. linear = len;
  1278. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  1279. &err, 0);
  1280. if (!skb)
  1281. return ERR_PTR(err);
  1282. skb_reserve(skb, prepad);
  1283. skb_put(skb, linear);
  1284. skb->data_len = len - linear;
  1285. skb->len += len - linear;
  1286. return skb;
  1287. }
  1288. static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile,
  1289. struct sk_buff *skb, int more)
  1290. {
  1291. struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
  1292. struct sk_buff_head process_queue;
  1293. u32 rx_batched = tun->rx_batched;
  1294. bool rcv = false;
  1295. if (!rx_batched || (!more && skb_queue_empty(queue))) {
  1296. local_bh_disable();
  1297. skb_record_rx_queue(skb, tfile->queue_index);
  1298. netif_receive_skb(skb);
  1299. local_bh_enable();
  1300. return;
  1301. }
  1302. spin_lock(&queue->lock);
  1303. if (!more || skb_queue_len(queue) == rx_batched) {
  1304. __skb_queue_head_init(&process_queue);
  1305. skb_queue_splice_tail_init(queue, &process_queue);
  1306. rcv = true;
  1307. } else {
  1308. __skb_queue_tail(queue, skb);
  1309. }
  1310. spin_unlock(&queue->lock);
  1311. if (rcv) {
  1312. struct sk_buff *nskb;
  1313. local_bh_disable();
  1314. while ((nskb = __skb_dequeue(&process_queue))) {
  1315. skb_record_rx_queue(nskb, tfile->queue_index);
  1316. netif_receive_skb(nskb);
  1317. }
  1318. skb_record_rx_queue(skb, tfile->queue_index);
  1319. netif_receive_skb(skb);
  1320. local_bh_enable();
  1321. }
  1322. }
  1323. static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile,
  1324. int len, int noblock, bool zerocopy)
  1325. {
  1326. if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
  1327. return false;
  1328. if (tfile->socket.sk->sk_sndbuf != INT_MAX)
  1329. return false;
  1330. if (!noblock)
  1331. return false;
  1332. if (zerocopy)
  1333. return false;
  1334. if (SKB_DATA_ALIGN(len + TUN_RX_PAD) +
  1335. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE)
  1336. return false;
  1337. return true;
  1338. }
  1339. static struct sk_buff *__tun_build_skb(struct tun_file *tfile,
  1340. struct page_frag *alloc_frag, char *buf,
  1341. int buflen, int len, int pad)
  1342. {
  1343. struct sk_buff *skb = build_skb(buf, buflen);
  1344. if (!skb)
  1345. return ERR_PTR(-ENOMEM);
  1346. skb_reserve(skb, pad);
  1347. skb_put(skb, len);
  1348. skb_set_owner_w(skb, tfile->socket.sk);
  1349. get_page(alloc_frag->page);
  1350. alloc_frag->offset += buflen;
  1351. return skb;
  1352. }
  1353. static int tun_xdp_act(struct tun_struct *tun, struct bpf_prog *xdp_prog,
  1354. struct xdp_buff *xdp, u32 act)
  1355. {
  1356. int err;
  1357. switch (act) {
  1358. case XDP_REDIRECT:
  1359. err = xdp_do_redirect(tun->dev, xdp, xdp_prog);
  1360. if (err)
  1361. return err;
  1362. break;
  1363. case XDP_TX:
  1364. err = tun_xdp_tx(tun->dev, xdp);
  1365. if (err < 0)
  1366. return err;
  1367. break;
  1368. case XDP_PASS:
  1369. break;
  1370. default:
  1371. bpf_warn_invalid_xdp_action(act);
  1372. /* fall through */
  1373. case XDP_ABORTED:
  1374. trace_xdp_exception(tun->dev, xdp_prog, act);
  1375. /* fall through */
  1376. case XDP_DROP:
  1377. this_cpu_inc(tun->pcpu_stats->rx_dropped);
  1378. break;
  1379. }
  1380. return act;
  1381. }
  1382. static struct sk_buff *tun_build_skb(struct tun_struct *tun,
  1383. struct tun_file *tfile,
  1384. struct iov_iter *from,
  1385. struct virtio_net_hdr *hdr,
  1386. int len, int *skb_xdp)
  1387. {
  1388. struct page_frag *alloc_frag = &current->task_frag;
  1389. struct bpf_prog *xdp_prog;
  1390. int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1391. char *buf;
  1392. size_t copied;
  1393. int pad = TUN_RX_PAD;
  1394. int err = 0;
  1395. rcu_read_lock();
  1396. xdp_prog = rcu_dereference(tun->xdp_prog);
  1397. if (xdp_prog)
  1398. pad += XDP_PACKET_HEADROOM;
  1399. buflen += SKB_DATA_ALIGN(len + pad);
  1400. rcu_read_unlock();
  1401. alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES);
  1402. if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL)))
  1403. return ERR_PTR(-ENOMEM);
  1404. buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
  1405. copied = copy_page_from_iter(alloc_frag->page,
  1406. alloc_frag->offset + pad,
  1407. len, from);
  1408. if (copied != len)
  1409. return ERR_PTR(-EFAULT);
  1410. /* There's a small window that XDP may be set after the check
  1411. * of xdp_prog above, this should be rare and for simplicity
  1412. * we do XDP on skb in case the headroom is not enough.
  1413. */
  1414. if (hdr->gso_type || !xdp_prog) {
  1415. *skb_xdp = 1;
  1416. return __tun_build_skb(tfile, alloc_frag, buf, buflen, len,
  1417. pad);
  1418. }
  1419. *skb_xdp = 0;
  1420. local_bh_disable();
  1421. rcu_read_lock();
  1422. xdp_prog = rcu_dereference(tun->xdp_prog);
  1423. if (xdp_prog) {
  1424. struct xdp_buff xdp;
  1425. u32 act;
  1426. xdp.data_hard_start = buf;
  1427. xdp.data = buf + pad;
  1428. xdp_set_data_meta_invalid(&xdp);
  1429. xdp.data_end = xdp.data + len;
  1430. xdp.rxq = &tfile->xdp_rxq;
  1431. act = bpf_prog_run_xdp(xdp_prog, &xdp);
  1432. if (act == XDP_REDIRECT || act == XDP_TX) {
  1433. get_page(alloc_frag->page);
  1434. alloc_frag->offset += buflen;
  1435. }
  1436. err = tun_xdp_act(tun, xdp_prog, &xdp, act);
  1437. if (err < 0) {
  1438. if (act == XDP_REDIRECT || act == XDP_TX)
  1439. put_page(alloc_frag->page);
  1440. goto out;
  1441. }
  1442. if (err == XDP_REDIRECT)
  1443. xdp_do_flush_map();
  1444. if (err != XDP_PASS)
  1445. goto out;
  1446. pad = xdp.data - xdp.data_hard_start;
  1447. len = xdp.data_end - xdp.data;
  1448. }
  1449. rcu_read_unlock();
  1450. local_bh_enable();
  1451. return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, pad);
  1452. out:
  1453. rcu_read_unlock();
  1454. local_bh_enable();
  1455. return NULL;
  1456. }
  1457. /* Get packet from user space buffer */
  1458. static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
  1459. void *msg_control, struct iov_iter *from,
  1460. int noblock, bool more)
  1461. {
  1462. struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
  1463. struct sk_buff *skb;
  1464. size_t total_len = iov_iter_count(from);
  1465. size_t len = total_len, align = tun->align, linear;
  1466. struct virtio_net_hdr gso = { 0 };
  1467. struct tun_pcpu_stats *stats;
  1468. int good_linear;
  1469. int copylen;
  1470. bool zerocopy = false;
  1471. int err;
  1472. u32 rxhash = 0;
  1473. int skb_xdp = 1;
  1474. bool frags = tun_napi_frags_enabled(tfile);
  1475. if (!(tun->flags & IFF_NO_PI)) {
  1476. if (len < sizeof(pi))
  1477. return -EINVAL;
  1478. len -= sizeof(pi);
  1479. if (!copy_from_iter_full(&pi, sizeof(pi), from))
  1480. return -EFAULT;
  1481. }
  1482. if (tun->flags & IFF_VNET_HDR) {
  1483. int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
  1484. if (len < vnet_hdr_sz)
  1485. return -EINVAL;
  1486. len -= vnet_hdr_sz;
  1487. if (!copy_from_iter_full(&gso, sizeof(gso), from))
  1488. return -EFAULT;
  1489. if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  1490. tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
  1491. gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2);
  1492. if (tun16_to_cpu(tun, gso.hdr_len) > len)
  1493. return -EINVAL;
  1494. iov_iter_advance(from, vnet_hdr_sz - sizeof(gso));
  1495. }
  1496. if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
  1497. align += NET_IP_ALIGN;
  1498. if (unlikely(len < ETH_HLEN ||
  1499. (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
  1500. return -EINVAL;
  1501. }
  1502. good_linear = SKB_MAX_HEAD(align);
  1503. if (msg_control) {
  1504. struct iov_iter i = *from;
  1505. /* There are 256 bytes to be copied in skb, so there is
  1506. * enough room for skb expand head in case it is used.
  1507. * The rest of the buffer is mapped from userspace.
  1508. */
  1509. copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
  1510. if (copylen > good_linear)
  1511. copylen = good_linear;
  1512. linear = copylen;
  1513. iov_iter_advance(&i, copylen);
  1514. if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
  1515. zerocopy = true;
  1516. }
  1517. if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) {
  1518. /* For the packet that is not easy to be processed
  1519. * (e.g gso or jumbo packet), we will do it at after
  1520. * skb was created with generic XDP routine.
  1521. */
  1522. skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp);
  1523. if (IS_ERR(skb)) {
  1524. this_cpu_inc(tun->pcpu_stats->rx_dropped);
  1525. return PTR_ERR(skb);
  1526. }
  1527. if (!skb)
  1528. return total_len;
  1529. } else {
  1530. if (!zerocopy) {
  1531. copylen = len;
  1532. if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
  1533. linear = good_linear;
  1534. else
  1535. linear = tun16_to_cpu(tun, gso.hdr_len);
  1536. }
  1537. if (frags) {
  1538. mutex_lock(&tfile->napi_mutex);
  1539. skb = tun_napi_alloc_frags(tfile, copylen, from);
  1540. /* tun_napi_alloc_frags() enforces a layout for the skb.
  1541. * If zerocopy is enabled, then this layout will be
  1542. * overwritten by zerocopy_sg_from_iter().
  1543. */
  1544. zerocopy = false;
  1545. } else {
  1546. skb = tun_alloc_skb(tfile, align, copylen, linear,
  1547. noblock);
  1548. }
  1549. if (IS_ERR(skb)) {
  1550. if (PTR_ERR(skb) != -EAGAIN)
  1551. this_cpu_inc(tun->pcpu_stats->rx_dropped);
  1552. if (frags)
  1553. mutex_unlock(&tfile->napi_mutex);
  1554. return PTR_ERR(skb);
  1555. }
  1556. if (zerocopy)
  1557. err = zerocopy_sg_from_iter(skb, from);
  1558. else
  1559. err = skb_copy_datagram_from_iter(skb, 0, from, len);
  1560. if (err) {
  1561. err = -EFAULT;
  1562. drop:
  1563. this_cpu_inc(tun->pcpu_stats->rx_dropped);
  1564. kfree_skb(skb);
  1565. if (frags) {
  1566. tfile->napi.skb = NULL;
  1567. mutex_unlock(&tfile->napi_mutex);
  1568. }
  1569. return err;
  1570. }
  1571. }
  1572. if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) {
  1573. this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
  1574. kfree_skb(skb);
  1575. if (frags) {
  1576. tfile->napi.skb = NULL;
  1577. mutex_unlock(&tfile->napi_mutex);
  1578. }
  1579. return -EINVAL;
  1580. }
  1581. switch (tun->flags & TUN_TYPE_MASK) {
  1582. case IFF_TUN:
  1583. if (tun->flags & IFF_NO_PI) {
  1584. u8 ip_version = skb->len ? (skb->data[0] >> 4) : 0;
  1585. switch (ip_version) {
  1586. case 4:
  1587. pi.proto = htons(ETH_P_IP);
  1588. break;
  1589. case 6:
  1590. pi.proto = htons(ETH_P_IPV6);
  1591. break;
  1592. default:
  1593. this_cpu_inc(tun->pcpu_stats->rx_dropped);
  1594. kfree_skb(skb);
  1595. return -EINVAL;
  1596. }
  1597. }
  1598. skb_reset_mac_header(skb);
  1599. skb->protocol = pi.proto;
  1600. skb->dev = tun->dev;
  1601. break;
  1602. case IFF_TAP:
  1603. if (frags && !pskb_may_pull(skb, ETH_HLEN)) {
  1604. err = -ENOMEM;
  1605. goto drop;
  1606. }
  1607. skb->protocol = eth_type_trans(skb, tun->dev);
  1608. break;
  1609. }
  1610. /* copy skb_ubuf_info for callback when skb has no error */
  1611. if (zerocopy) {
  1612. skb_shinfo(skb)->destructor_arg = msg_control;
  1613. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  1614. skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
  1615. } else if (msg_control) {
  1616. struct ubuf_info *uarg = msg_control;
  1617. uarg->callback(uarg, false);
  1618. }
  1619. skb_reset_network_header(skb);
  1620. skb_probe_transport_header(skb);
  1621. skb_record_rx_queue(skb, tfile->queue_index);
  1622. if (skb_xdp) {
  1623. struct bpf_prog *xdp_prog;
  1624. int ret;
  1625. local_bh_disable();
  1626. rcu_read_lock();
  1627. xdp_prog = rcu_dereference(tun->xdp_prog);
  1628. if (xdp_prog) {
  1629. ret = do_xdp_generic(xdp_prog, skb);
  1630. if (ret != XDP_PASS) {
  1631. rcu_read_unlock();
  1632. local_bh_enable();
  1633. if (frags) {
  1634. tfile->napi.skb = NULL;
  1635. mutex_unlock(&tfile->napi_mutex);
  1636. }
  1637. return total_len;
  1638. }
  1639. }
  1640. rcu_read_unlock();
  1641. local_bh_enable();
  1642. }
  1643. /* Compute the costly rx hash only if needed for flow updates.
  1644. * We may get a very small possibility of OOO during switching, not
  1645. * worth to optimize.
  1646. */
  1647. if (!rcu_access_pointer(tun->steering_prog) && tun->numqueues > 1 &&
  1648. !tfile->detached)
  1649. rxhash = __skb_get_hash_symmetric(skb);
  1650. rcu_read_lock();
  1651. if (unlikely(!(tun->dev->flags & IFF_UP))) {
  1652. err = -EIO;
  1653. rcu_read_unlock();
  1654. goto drop;
  1655. }
  1656. if (frags) {
  1657. u32 headlen;
  1658. /* Exercise flow dissector code path. */
  1659. skb_push(skb, ETH_HLEN);
  1660. headlen = eth_get_headlen(tun->dev, skb->data,
  1661. skb_headlen(skb));
  1662. if (unlikely(headlen > skb_headlen(skb))) {
  1663. this_cpu_inc(tun->pcpu_stats->rx_dropped);
  1664. napi_free_frags(&tfile->napi);
  1665. rcu_read_unlock();
  1666. mutex_unlock(&tfile->napi_mutex);
  1667. WARN_ON(1);
  1668. return -ENOMEM;
  1669. }
  1670. local_bh_disable();
  1671. napi_gro_frags(&tfile->napi);
  1672. local_bh_enable();
  1673. mutex_unlock(&tfile->napi_mutex);
  1674. } else if (tfile->napi_enabled) {
  1675. struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
  1676. int queue_len;
  1677. spin_lock_bh(&queue->lock);
  1678. __skb_queue_tail(queue, skb);
  1679. queue_len = skb_queue_len(queue);
  1680. spin_unlock(&queue->lock);
  1681. if (!more || queue_len > NAPI_POLL_WEIGHT)
  1682. napi_schedule(&tfile->napi);
  1683. local_bh_enable();
  1684. } else if (!IS_ENABLED(CONFIG_4KSTACKS)) {
  1685. tun_rx_batched(tun, tfile, skb, more);
  1686. } else {
  1687. netif_rx_ni(skb);
  1688. }
  1689. rcu_read_unlock();
  1690. stats = get_cpu_ptr(tun->pcpu_stats);
  1691. u64_stats_update_begin(&stats->syncp);
  1692. stats->rx_packets++;
  1693. stats->rx_bytes += len;
  1694. u64_stats_update_end(&stats->syncp);
  1695. put_cpu_ptr(stats);
  1696. if (rxhash)
  1697. tun_flow_update(tun, rxhash, tfile);
  1698. return total_len;
  1699. }
  1700. static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
  1701. {
  1702. struct file *file = iocb->ki_filp;
  1703. struct tun_file *tfile = file->private_data;
  1704. struct tun_struct *tun = tun_get(tfile);
  1705. ssize_t result;
  1706. int noblock = 0;
  1707. if (!tun)
  1708. return -EBADFD;
  1709. if ((file->f_flags & O_NONBLOCK) || (iocb->ki_flags & IOCB_NOWAIT))
  1710. noblock = 1;
  1711. result = tun_get_user(tun, tfile, NULL, from, noblock, false);
  1712. tun_put(tun);
  1713. return result;
  1714. }
  1715. static ssize_t tun_put_user_xdp(struct tun_struct *tun,
  1716. struct tun_file *tfile,
  1717. struct xdp_frame *xdp_frame,
  1718. struct iov_iter *iter)
  1719. {
  1720. int vnet_hdr_sz = 0;
  1721. size_t size = xdp_frame->len;
  1722. struct tun_pcpu_stats *stats;
  1723. size_t ret;
  1724. if (tun->flags & IFF_VNET_HDR) {
  1725. struct virtio_net_hdr gso = { 0 };
  1726. vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
  1727. if (unlikely(iov_iter_count(iter) < vnet_hdr_sz))
  1728. return -EINVAL;
  1729. if (unlikely(copy_to_iter(&gso, sizeof(gso), iter) !=
  1730. sizeof(gso)))
  1731. return -EFAULT;
  1732. iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
  1733. }
  1734. ret = copy_to_iter(xdp_frame->data, size, iter) + vnet_hdr_sz;
  1735. stats = get_cpu_ptr(tun->pcpu_stats);
  1736. u64_stats_update_begin(&stats->syncp);
  1737. stats->tx_packets++;
  1738. stats->tx_bytes += ret;
  1739. u64_stats_update_end(&stats->syncp);
  1740. put_cpu_ptr(tun->pcpu_stats);
  1741. return ret;
  1742. }
  1743. /* Put packet to the user space buffer */
  1744. static ssize_t tun_put_user(struct tun_struct *tun,
  1745. struct tun_file *tfile,
  1746. struct sk_buff *skb,
  1747. struct iov_iter *iter)
  1748. {
  1749. struct tun_pi pi = { 0, skb->protocol };
  1750. struct tun_pcpu_stats *stats;
  1751. ssize_t total;
  1752. int vlan_offset = 0;
  1753. int vlan_hlen = 0;
  1754. int vnet_hdr_sz = 0;
  1755. if (skb_vlan_tag_present(skb))
  1756. vlan_hlen = VLAN_HLEN;
  1757. if (tun->flags & IFF_VNET_HDR)
  1758. vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
  1759. total = skb->len + vlan_hlen + vnet_hdr_sz;
  1760. if (!(tun->flags & IFF_NO_PI)) {
  1761. if (iov_iter_count(iter) < sizeof(pi))
  1762. return -EINVAL;
  1763. total += sizeof(pi);
  1764. if (iov_iter_count(iter) < total) {
  1765. /* Packet will be striped */
  1766. pi.flags |= TUN_PKT_STRIP;
  1767. }
  1768. if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
  1769. return -EFAULT;
  1770. }
  1771. if (vnet_hdr_sz) {
  1772. struct virtio_net_hdr gso;
  1773. if (iov_iter_count(iter) < vnet_hdr_sz)
  1774. return -EINVAL;
  1775. if (virtio_net_hdr_from_skb(skb, &gso,
  1776. tun_is_little_endian(tun), true,
  1777. vlan_hlen)) {
  1778. struct skb_shared_info *sinfo = skb_shinfo(skb);
  1779. pr_err("unexpected GSO type: "
  1780. "0x%x, gso_size %d, hdr_len %d\n",
  1781. sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
  1782. tun16_to_cpu(tun, gso.hdr_len));
  1783. print_hex_dump(KERN_ERR, "tun: ",
  1784. DUMP_PREFIX_NONE,
  1785. 16, 1, skb->head,
  1786. min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
  1787. WARN_ON_ONCE(1);
  1788. return -EINVAL;
  1789. }
  1790. if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
  1791. return -EFAULT;
  1792. iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
  1793. }
  1794. if (vlan_hlen) {
  1795. int ret;
  1796. struct veth veth;
  1797. veth.h_vlan_proto = skb->vlan_proto;
  1798. veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
  1799. vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
  1800. ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
  1801. if (ret || !iov_iter_count(iter))
  1802. goto done;
  1803. ret = copy_to_iter(&veth, sizeof(veth), iter);
  1804. if (ret != sizeof(veth) || !iov_iter_count(iter))
  1805. goto done;
  1806. }
  1807. skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
  1808. done:
  1809. /* caller is in process context, */
  1810. stats = get_cpu_ptr(tun->pcpu_stats);
  1811. u64_stats_update_begin(&stats->syncp);
  1812. stats->tx_packets++;
  1813. stats->tx_bytes += skb->len + vlan_hlen;
  1814. u64_stats_update_end(&stats->syncp);
  1815. put_cpu_ptr(tun->pcpu_stats);
  1816. return total;
  1817. }
  1818. static void *tun_ring_recv(struct tun_file *tfile, int noblock, int *err)
  1819. {
  1820. DECLARE_WAITQUEUE(wait, current);
  1821. void *ptr = NULL;
  1822. int error = 0;
  1823. ptr = ptr_ring_consume(&tfile->tx_ring);
  1824. if (ptr)
  1825. goto out;
  1826. if (noblock) {
  1827. error = -EAGAIN;
  1828. goto out;
  1829. }
  1830. add_wait_queue(&tfile->socket.wq.wait, &wait);
  1831. while (1) {
  1832. set_current_state(TASK_INTERRUPTIBLE);
  1833. ptr = ptr_ring_consume(&tfile->tx_ring);
  1834. if (ptr)
  1835. break;
  1836. if (signal_pending(current)) {
  1837. error = -ERESTARTSYS;
  1838. break;
  1839. }
  1840. if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) {
  1841. error = -EFAULT;
  1842. break;
  1843. }
  1844. schedule();
  1845. }
  1846. __set_current_state(TASK_RUNNING);
  1847. remove_wait_queue(&tfile->socket.wq.wait, &wait);
  1848. out:
  1849. *err = error;
  1850. return ptr;
  1851. }
  1852. static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
  1853. struct iov_iter *to,
  1854. int noblock, void *ptr)
  1855. {
  1856. ssize_t ret;
  1857. int err;
  1858. tun_debug(KERN_INFO, tun, "tun_do_read\n");
  1859. if (!iov_iter_count(to)) {
  1860. tun_ptr_free(ptr);
  1861. return 0;
  1862. }
  1863. if (!ptr) {
  1864. /* Read frames from ring */
  1865. ptr = tun_ring_recv(tfile, noblock, &err);
  1866. if (!ptr)
  1867. return err;
  1868. }
  1869. if (tun_is_xdp_frame(ptr)) {
  1870. struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
  1871. ret = tun_put_user_xdp(tun, tfile, xdpf, to);
  1872. xdp_return_frame(xdpf);
  1873. } else {
  1874. struct sk_buff *skb = ptr;
  1875. ret = tun_put_user(tun, tfile, skb, to);
  1876. if (unlikely(ret < 0))
  1877. kfree_skb(skb);
  1878. else
  1879. consume_skb(skb);
  1880. }
  1881. return ret;
  1882. }
  1883. static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1884. {
  1885. struct file *file = iocb->ki_filp;
  1886. struct tun_file *tfile = file->private_data;
  1887. struct tun_struct *tun = tun_get(tfile);
  1888. ssize_t len = iov_iter_count(to), ret;
  1889. int noblock = 0;
  1890. if (!tun)
  1891. return -EBADFD;
  1892. if ((file->f_flags & O_NONBLOCK) || (iocb->ki_flags & IOCB_NOWAIT))
  1893. noblock = 1;
  1894. ret = tun_do_read(tun, tfile, to, noblock, NULL);
  1895. ret = min_t(ssize_t, ret, len);
  1896. if (ret > 0)
  1897. iocb->ki_pos = ret;
  1898. tun_put(tun);
  1899. return ret;
  1900. }
  1901. static void tun_prog_free(struct rcu_head *rcu)
  1902. {
  1903. struct tun_prog *prog = container_of(rcu, struct tun_prog, rcu);
  1904. bpf_prog_destroy(prog->prog);
  1905. kfree(prog);
  1906. }
  1907. static int __tun_set_ebpf(struct tun_struct *tun,
  1908. struct tun_prog __rcu **prog_p,
  1909. struct bpf_prog *prog)
  1910. {
  1911. struct tun_prog *old, *new = NULL;
  1912. if (prog) {
  1913. new = kmalloc(sizeof(*new), GFP_KERNEL);
  1914. if (!new)
  1915. return -ENOMEM;
  1916. new->prog = prog;
  1917. }
  1918. spin_lock_bh(&tun->lock);
  1919. old = rcu_dereference_protected(*prog_p,
  1920. lockdep_is_held(&tun->lock));
  1921. rcu_assign_pointer(*prog_p, new);
  1922. spin_unlock_bh(&tun->lock);
  1923. if (old)
  1924. call_rcu(&old->rcu, tun_prog_free);
  1925. return 0;
  1926. }
  1927. static void tun_free_netdev(struct net_device *dev)
  1928. {
  1929. struct tun_struct *tun = netdev_priv(dev);
  1930. BUG_ON(!(list_empty(&tun->disabled)));
  1931. free_percpu(tun->pcpu_stats);
  1932. tun_flow_uninit(tun);
  1933. security_tun_dev_free_security(tun->security);
  1934. __tun_set_ebpf(tun, &tun->steering_prog, NULL);
  1935. __tun_set_ebpf(tun, &tun->filter_prog, NULL);
  1936. }
  1937. static void tun_setup(struct net_device *dev)
  1938. {
  1939. struct tun_struct *tun = netdev_priv(dev);
  1940. tun->owner = INVALID_UID;
  1941. tun->group = INVALID_GID;
  1942. tun_default_link_ksettings(dev, &tun->link_ksettings);
  1943. dev->ethtool_ops = &tun_ethtool_ops;
  1944. dev->needs_free_netdev = true;
  1945. dev->priv_destructor = tun_free_netdev;
  1946. /* We prefer our own queue length */
  1947. dev->tx_queue_len = TUN_READQ_SIZE;
  1948. }
  1949. /* Trivial set of netlink ops to allow deleting tun or tap
  1950. * device with netlink.
  1951. */
  1952. static int tun_validate(struct nlattr *tb[], struct nlattr *data[],
  1953. struct netlink_ext_ack *extack)
  1954. {
  1955. NL_SET_ERR_MSG(extack,
  1956. "tun/tap creation via rtnetlink is not supported.");
  1957. return -EOPNOTSUPP;
  1958. }
  1959. static size_t tun_get_size(const struct net_device *dev)
  1960. {
  1961. BUILD_BUG_ON(sizeof(u32) != sizeof(uid_t));
  1962. BUILD_BUG_ON(sizeof(u32) != sizeof(gid_t));
  1963. return nla_total_size(sizeof(uid_t)) + /* OWNER */
  1964. nla_total_size(sizeof(gid_t)) + /* GROUP */
  1965. nla_total_size(sizeof(u8)) + /* TYPE */
  1966. nla_total_size(sizeof(u8)) + /* PI */
  1967. nla_total_size(sizeof(u8)) + /* VNET_HDR */
  1968. nla_total_size(sizeof(u8)) + /* PERSIST */
  1969. nla_total_size(sizeof(u8)) + /* MULTI_QUEUE */
  1970. nla_total_size(sizeof(u32)) + /* NUM_QUEUES */
  1971. nla_total_size(sizeof(u32)) + /* NUM_DISABLED_QUEUES */
  1972. 0;
  1973. }
  1974. static int tun_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1975. {
  1976. struct tun_struct *tun = netdev_priv(dev);
  1977. if (nla_put_u8(skb, IFLA_TUN_TYPE, tun->flags & TUN_TYPE_MASK))
  1978. goto nla_put_failure;
  1979. if (uid_valid(tun->owner) &&
  1980. nla_put_u32(skb, IFLA_TUN_OWNER,
  1981. from_kuid_munged(current_user_ns(), tun->owner)))
  1982. goto nla_put_failure;
  1983. if (gid_valid(tun->group) &&
  1984. nla_put_u32(skb, IFLA_TUN_GROUP,
  1985. from_kgid_munged(current_user_ns(), tun->group)))
  1986. goto nla_put_failure;
  1987. if (nla_put_u8(skb, IFLA_TUN_PI, !(tun->flags & IFF_NO_PI)))
  1988. goto nla_put_failure;
  1989. if (nla_put_u8(skb, IFLA_TUN_VNET_HDR, !!(tun->flags & IFF_VNET_HDR)))
  1990. goto nla_put_failure;
  1991. if (nla_put_u8(skb, IFLA_TUN_PERSIST, !!(tun->flags & IFF_PERSIST)))
  1992. goto nla_put_failure;
  1993. if (nla_put_u8(skb, IFLA_TUN_MULTI_QUEUE,
  1994. !!(tun->flags & IFF_MULTI_QUEUE)))
  1995. goto nla_put_failure;
  1996. if (tun->flags & IFF_MULTI_QUEUE) {
  1997. if (nla_put_u32(skb, IFLA_TUN_NUM_QUEUES, tun->numqueues))
  1998. goto nla_put_failure;
  1999. if (nla_put_u32(skb, IFLA_TUN_NUM_DISABLED_QUEUES,
  2000. tun->numdisabled))
  2001. goto nla_put_failure;
  2002. }
  2003. return 0;
  2004. nla_put_failure:
  2005. return -EMSGSIZE;
  2006. }
  2007. static struct rtnl_link_ops tun_link_ops __read_mostly = {
  2008. .kind = DRV_NAME,
  2009. .priv_size = sizeof(struct tun_struct),
  2010. .setup = tun_setup,
  2011. .validate = tun_validate,
  2012. .get_size = tun_get_size,
  2013. .fill_info = tun_fill_info,
  2014. };
  2015. static void tun_sock_write_space(struct sock *sk)
  2016. {
  2017. struct tun_file *tfile;
  2018. wait_queue_head_t *wqueue;
  2019. if (!sock_writeable(sk))
  2020. return;
  2021. if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
  2022. return;
  2023. wqueue = sk_sleep(sk);
  2024. if (wqueue && waitqueue_active(wqueue))
  2025. wake_up_interruptible_sync_poll(wqueue, EPOLLOUT |
  2026. EPOLLWRNORM | EPOLLWRBAND);
  2027. tfile = container_of(sk, struct tun_file, sk);
  2028. kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
  2029. }
  2030. static void tun_put_page(struct tun_page *tpage)
  2031. {
  2032. if (tpage->page)
  2033. __page_frag_cache_drain(tpage->page, tpage->count);
  2034. }
  2035. static int tun_xdp_one(struct tun_struct *tun,
  2036. struct tun_file *tfile,
  2037. struct xdp_buff *xdp, int *flush,
  2038. struct tun_page *tpage)
  2039. {
  2040. unsigned int datasize = xdp->data_end - xdp->data;
  2041. struct tun_xdp_hdr *hdr = xdp->data_hard_start;
  2042. struct virtio_net_hdr *gso = &hdr->gso;
  2043. struct tun_pcpu_stats *stats;
  2044. struct bpf_prog *xdp_prog;
  2045. struct sk_buff *skb = NULL;
  2046. u32 rxhash = 0, act;
  2047. int buflen = hdr->buflen;
  2048. int err = 0;
  2049. bool skb_xdp = false;
  2050. struct page *page;
  2051. xdp_prog = rcu_dereference(tun->xdp_prog);
  2052. if (xdp_prog) {
  2053. if (gso->gso_type) {
  2054. skb_xdp = true;
  2055. goto build;
  2056. }
  2057. xdp_set_data_meta_invalid(xdp);
  2058. xdp->rxq = &tfile->xdp_rxq;
  2059. act = bpf_prog_run_xdp(xdp_prog, xdp);
  2060. err = tun_xdp_act(tun, xdp_prog, xdp, act);
  2061. if (err < 0) {
  2062. put_page(virt_to_head_page(xdp->data));
  2063. return err;
  2064. }
  2065. switch (err) {
  2066. case XDP_REDIRECT:
  2067. *flush = true;
  2068. /* fall through */
  2069. case XDP_TX:
  2070. return 0;
  2071. case XDP_PASS:
  2072. break;
  2073. default:
  2074. page = virt_to_head_page(xdp->data);
  2075. if (tpage->page == page) {
  2076. ++tpage->count;
  2077. } else {
  2078. tun_put_page(tpage);
  2079. tpage->page = page;
  2080. tpage->count = 1;
  2081. }
  2082. return 0;
  2083. }
  2084. }
  2085. build:
  2086. skb = build_skb(xdp->data_hard_start, buflen);
  2087. if (!skb) {
  2088. err = -ENOMEM;
  2089. goto out;
  2090. }
  2091. skb_reserve(skb, xdp->data - xdp->data_hard_start);
  2092. skb_put(skb, xdp->data_end - xdp->data);
  2093. if (virtio_net_hdr_to_skb(skb, gso, tun_is_little_endian(tun))) {
  2094. this_cpu_inc(tun->pcpu_stats->rx_frame_errors);
  2095. kfree_skb(skb);
  2096. err = -EINVAL;
  2097. goto out;
  2098. }
  2099. skb->protocol = eth_type_trans(skb, tun->dev);
  2100. skb_reset_network_header(skb);
  2101. skb_probe_transport_header(skb);
  2102. skb_record_rx_queue(skb, tfile->queue_index);
  2103. if (skb_xdp) {
  2104. err = do_xdp_generic(xdp_prog, skb);
  2105. if (err != XDP_PASS)
  2106. goto out;
  2107. }
  2108. if (!rcu_dereference(tun->steering_prog) && tun->numqueues > 1 &&
  2109. !tfile->detached)
  2110. rxhash = __skb_get_hash_symmetric(skb);
  2111. netif_receive_skb(skb);
  2112. /* No need for get_cpu_ptr() here since this function is
  2113. * always called with bh disabled
  2114. */
  2115. stats = this_cpu_ptr(tun->pcpu_stats);
  2116. u64_stats_update_begin(&stats->syncp);
  2117. stats->rx_packets++;
  2118. stats->rx_bytes += datasize;
  2119. u64_stats_update_end(&stats->syncp);
  2120. if (rxhash)
  2121. tun_flow_update(tun, rxhash, tfile);
  2122. out:
  2123. return err;
  2124. }
  2125. static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
  2126. {
  2127. int ret, i;
  2128. struct tun_file *tfile = container_of(sock, struct tun_file, socket);
  2129. struct tun_struct *tun = tun_get(tfile);
  2130. struct tun_msg_ctl *ctl = m->msg_control;
  2131. struct xdp_buff *xdp;
  2132. if (!tun)
  2133. return -EBADFD;
  2134. if (ctl && (ctl->type == TUN_MSG_PTR)) {
  2135. struct tun_page tpage;
  2136. int n = ctl->num;
  2137. int flush = 0;
  2138. memset(&tpage, 0, sizeof(tpage));
  2139. local_bh_disable();
  2140. rcu_read_lock();
  2141. for (i = 0; i < n; i++) {
  2142. xdp = &((struct xdp_buff *)ctl->ptr)[i];
  2143. tun_xdp_one(tun, tfile, xdp, &flush, &tpage);
  2144. }
  2145. if (flush)
  2146. xdp_do_flush_map();
  2147. rcu_read_unlock();
  2148. local_bh_enable();
  2149. tun_put_page(&tpage);
  2150. ret = total_len;
  2151. goto out;
  2152. }
  2153. ret = tun_get_user(tun, tfile, ctl ? ctl->ptr : NULL, &m->msg_iter,
  2154. m->msg_flags & MSG_DONTWAIT,
  2155. m->msg_flags & MSG_MORE);
  2156. out:
  2157. tun_put(tun);
  2158. return ret;
  2159. }
  2160. static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
  2161. int flags)
  2162. {
  2163. struct tun_file *tfile = container_of(sock, struct tun_file, socket);
  2164. struct tun_struct *tun = tun_get(tfile);
  2165. void *ptr = m->msg_control;
  2166. int ret;
  2167. if (!tun) {
  2168. ret = -EBADFD;
  2169. goto out_free;
  2170. }
  2171. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
  2172. ret = -EINVAL;
  2173. goto out_put_tun;
  2174. }
  2175. if (flags & MSG_ERRQUEUE) {
  2176. ret = sock_recv_errqueue(sock->sk, m, total_len,
  2177. SOL_PACKET, TUN_TX_TIMESTAMP);
  2178. goto out;
  2179. }
  2180. ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, ptr);
  2181. if (ret > (ssize_t)total_len) {
  2182. m->msg_flags |= MSG_TRUNC;
  2183. ret = flags & MSG_TRUNC ? ret : total_len;
  2184. }
  2185. out:
  2186. tun_put(tun);
  2187. return ret;
  2188. out_put_tun:
  2189. tun_put(tun);
  2190. out_free:
  2191. tun_ptr_free(ptr);
  2192. return ret;
  2193. }
  2194. static int tun_ptr_peek_len(void *ptr)
  2195. {
  2196. if (likely(ptr)) {
  2197. if (tun_is_xdp_frame(ptr)) {
  2198. struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
  2199. return xdpf->len;
  2200. }
  2201. return __skb_array_len_with_tag(ptr);
  2202. } else {
  2203. return 0;
  2204. }
  2205. }
  2206. static int tun_peek_len(struct socket *sock)
  2207. {
  2208. struct tun_file *tfile = container_of(sock, struct tun_file, socket);
  2209. struct tun_struct *tun;
  2210. int ret = 0;
  2211. tun = tun_get(tfile);
  2212. if (!tun)
  2213. return 0;
  2214. ret = PTR_RING_PEEK_CALL(&tfile->tx_ring, tun_ptr_peek_len);
  2215. tun_put(tun);
  2216. return ret;
  2217. }
  2218. /* Ops structure to mimic raw sockets with tun */
  2219. static const struct proto_ops tun_socket_ops = {
  2220. .peek_len = tun_peek_len,
  2221. .sendmsg = tun_sendmsg,
  2222. .recvmsg = tun_recvmsg,
  2223. };
  2224. static struct proto tun_proto = {
  2225. .name = "tun",
  2226. .owner = THIS_MODULE,
  2227. .obj_size = sizeof(struct tun_file),
  2228. };
  2229. static int tun_flags(struct tun_struct *tun)
  2230. {
  2231. return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
  2232. }
  2233. static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
  2234. char *buf)
  2235. {
  2236. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  2237. return sprintf(buf, "0x%x\n", tun_flags(tun));
  2238. }
  2239. static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
  2240. char *buf)
  2241. {
  2242. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  2243. return uid_valid(tun->owner)?
  2244. sprintf(buf, "%u\n",
  2245. from_kuid_munged(current_user_ns(), tun->owner)):
  2246. sprintf(buf, "-1\n");
  2247. }
  2248. static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
  2249. char *buf)
  2250. {
  2251. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  2252. return gid_valid(tun->group) ?
  2253. sprintf(buf, "%u\n",
  2254. from_kgid_munged(current_user_ns(), tun->group)):
  2255. sprintf(buf, "-1\n");
  2256. }
  2257. static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
  2258. static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
  2259. static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
  2260. static struct attribute *tun_dev_attrs[] = {
  2261. &dev_attr_tun_flags.attr,
  2262. &dev_attr_owner.attr,
  2263. &dev_attr_group.attr,
  2264. NULL
  2265. };
  2266. static const struct attribute_group tun_attr_group = {
  2267. .attrs = tun_dev_attrs
  2268. };
  2269. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  2270. {
  2271. struct tun_struct *tun;
  2272. struct tun_file *tfile = file->private_data;
  2273. struct net_device *dev;
  2274. int err;
  2275. if (tfile->detached)
  2276. return -EINVAL;
  2277. if ((ifr->ifr_flags & IFF_NAPI_FRAGS)) {
  2278. if (!capable(CAP_NET_ADMIN))
  2279. return -EPERM;
  2280. if (!(ifr->ifr_flags & IFF_NAPI) ||
  2281. (ifr->ifr_flags & TUN_TYPE_MASK) != IFF_TAP)
  2282. return -EINVAL;
  2283. }
  2284. dev = __dev_get_by_name(net, ifr->ifr_name);
  2285. if (dev) {
  2286. if (ifr->ifr_flags & IFF_TUN_EXCL)
  2287. return -EBUSY;
  2288. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  2289. tun = netdev_priv(dev);
  2290. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  2291. tun = netdev_priv(dev);
  2292. else
  2293. return -EINVAL;
  2294. if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
  2295. !!(tun->flags & IFF_MULTI_QUEUE))
  2296. return -EINVAL;
  2297. if (tun_not_capable(tun))
  2298. return -EPERM;
  2299. err = security_tun_dev_open(tun->security);
  2300. if (err < 0)
  2301. return err;
  2302. err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER,
  2303. ifr->ifr_flags & IFF_NAPI,
  2304. ifr->ifr_flags & IFF_NAPI_FRAGS, true);
  2305. if (err < 0)
  2306. return err;
  2307. if (tun->flags & IFF_MULTI_QUEUE &&
  2308. (tun->numqueues + tun->numdisabled > 1)) {
  2309. /* One or more queue has already been attached, no need
  2310. * to initialize the device again.
  2311. */
  2312. netdev_state_change(dev);
  2313. return 0;
  2314. }
  2315. tun->flags = (tun->flags & ~TUN_FEATURES) |
  2316. (ifr->ifr_flags & TUN_FEATURES);
  2317. netdev_state_change(dev);
  2318. } else {
  2319. char *name;
  2320. unsigned long flags = 0;
  2321. int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
  2322. MAX_TAP_QUEUES : 1;
  2323. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2324. return -EPERM;
  2325. err = security_tun_dev_create();
  2326. if (err < 0)
  2327. return err;
  2328. /* Set dev type */
  2329. if (ifr->ifr_flags & IFF_TUN) {
  2330. /* TUN device */
  2331. flags |= IFF_TUN;
  2332. name = "tun%d";
  2333. } else if (ifr->ifr_flags & IFF_TAP) {
  2334. /* TAP device */
  2335. flags |= IFF_TAP;
  2336. name = "tap%d";
  2337. } else
  2338. return -EINVAL;
  2339. if (*ifr->ifr_name)
  2340. name = ifr->ifr_name;
  2341. dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
  2342. NET_NAME_UNKNOWN, tun_setup, queues,
  2343. queues);
  2344. if (!dev)
  2345. return -ENOMEM;
  2346. err = dev_get_valid_name(net, dev, name);
  2347. if (err < 0)
  2348. goto err_free_dev;
  2349. dev_net_set(dev, net);
  2350. dev->rtnl_link_ops = &tun_link_ops;
  2351. dev->ifindex = tfile->ifindex;
  2352. dev->sysfs_groups[0] = &tun_attr_group;
  2353. tun = netdev_priv(dev);
  2354. tun->dev = dev;
  2355. tun->flags = flags;
  2356. tun->txflt.count = 0;
  2357. tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  2358. tun->align = NET_SKB_PAD;
  2359. tun->filter_attached = false;
  2360. tun->sndbuf = tfile->socket.sk->sk_sndbuf;
  2361. tun->rx_batched = 0;
  2362. RCU_INIT_POINTER(tun->steering_prog, NULL);
  2363. tun->pcpu_stats = netdev_alloc_pcpu_stats(struct tun_pcpu_stats);
  2364. if (!tun->pcpu_stats) {
  2365. err = -ENOMEM;
  2366. goto err_free_dev;
  2367. }
  2368. spin_lock_init(&tun->lock);
  2369. err = security_tun_dev_alloc_security(&tun->security);
  2370. if (err < 0)
  2371. goto err_free_stat;
  2372. tun_net_init(dev);
  2373. tun_flow_init(tun);
  2374. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
  2375. TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
  2376. NETIF_F_HW_VLAN_STAG_TX;
  2377. dev->features = dev->hw_features | NETIF_F_LLTX;
  2378. dev->vlan_features = dev->features &
  2379. ~(NETIF_F_HW_VLAN_CTAG_TX |
  2380. NETIF_F_HW_VLAN_STAG_TX);
  2381. tun->flags = (tun->flags & ~TUN_FEATURES) |
  2382. (ifr->ifr_flags & TUN_FEATURES);
  2383. INIT_LIST_HEAD(&tun->disabled);
  2384. err = tun_attach(tun, file, false, ifr->ifr_flags & IFF_NAPI,
  2385. ifr->ifr_flags & IFF_NAPI_FRAGS, false);
  2386. if (err < 0)
  2387. goto err_free_flow;
  2388. err = register_netdevice(tun->dev);
  2389. if (err < 0)
  2390. goto err_detach;
  2391. /* free_netdev() won't check refcnt, to aovid race
  2392. * with dev_put() we need publish tun after registration.
  2393. */
  2394. rcu_assign_pointer(tfile->tun, tun);
  2395. }
  2396. netif_carrier_on(tun->dev);
  2397. tun_debug(KERN_INFO, tun, "tun_set_iff\n");
  2398. /* Make sure persistent devices do not get stuck in
  2399. * xoff state.
  2400. */
  2401. if (netif_running(tun->dev))
  2402. netif_tx_wake_all_queues(tun->dev);
  2403. strcpy(ifr->ifr_name, tun->dev->name);
  2404. return 0;
  2405. err_detach:
  2406. tun_detach_all(dev);
  2407. /* register_netdevice() already called tun_free_netdev() */
  2408. goto err_free_dev;
  2409. err_free_flow:
  2410. tun_flow_uninit(tun);
  2411. security_tun_dev_free_security(tun->security);
  2412. err_free_stat:
  2413. free_percpu(tun->pcpu_stats);
  2414. err_free_dev:
  2415. free_netdev(dev);
  2416. return err;
  2417. }
  2418. static void tun_get_iff(struct tun_struct *tun, struct ifreq *ifr)
  2419. {
  2420. tun_debug(KERN_INFO, tun, "tun_get_iff\n");
  2421. strcpy(ifr->ifr_name, tun->dev->name);
  2422. ifr->ifr_flags = tun_flags(tun);
  2423. }
  2424. /* This is like a cut-down ethtool ops, except done via tun fd so no
  2425. * privs required. */
  2426. static int set_offload(struct tun_struct *tun, unsigned long arg)
  2427. {
  2428. netdev_features_t features = 0;
  2429. if (arg & TUN_F_CSUM) {
  2430. features |= NETIF_F_HW_CSUM;
  2431. arg &= ~TUN_F_CSUM;
  2432. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  2433. if (arg & TUN_F_TSO_ECN) {
  2434. features |= NETIF_F_TSO_ECN;
  2435. arg &= ~TUN_F_TSO_ECN;
  2436. }
  2437. if (arg & TUN_F_TSO4)
  2438. features |= NETIF_F_TSO;
  2439. if (arg & TUN_F_TSO6)
  2440. features |= NETIF_F_TSO6;
  2441. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  2442. }
  2443. arg &= ~TUN_F_UFO;
  2444. }
  2445. /* This gives the user a way to test for new features in future by
  2446. * trying to set them. */
  2447. if (arg)
  2448. return -EINVAL;
  2449. tun->set_features = features;
  2450. tun->dev->wanted_features &= ~TUN_USER_FEATURES;
  2451. tun->dev->wanted_features |= features;
  2452. netdev_update_features(tun->dev);
  2453. return 0;
  2454. }
  2455. static void tun_detach_filter(struct tun_struct *tun, int n)
  2456. {
  2457. int i;
  2458. struct tun_file *tfile;
  2459. for (i = 0; i < n; i++) {
  2460. tfile = rtnl_dereference(tun->tfiles[i]);
  2461. lock_sock(tfile->socket.sk);
  2462. sk_detach_filter(tfile->socket.sk);
  2463. release_sock(tfile->socket.sk);
  2464. }
  2465. tun->filter_attached = false;
  2466. }
  2467. static int tun_attach_filter(struct tun_struct *tun)
  2468. {
  2469. int i, ret = 0;
  2470. struct tun_file *tfile;
  2471. for (i = 0; i < tun->numqueues; i++) {
  2472. tfile = rtnl_dereference(tun->tfiles[i]);
  2473. lock_sock(tfile->socket.sk);
  2474. ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
  2475. release_sock(tfile->socket.sk);
  2476. if (ret) {
  2477. tun_detach_filter(tun, i);
  2478. return ret;
  2479. }
  2480. }
  2481. tun->filter_attached = true;
  2482. return ret;
  2483. }
  2484. static void tun_set_sndbuf(struct tun_struct *tun)
  2485. {
  2486. struct tun_file *tfile;
  2487. int i;
  2488. for (i = 0; i < tun->numqueues; i++) {
  2489. tfile = rtnl_dereference(tun->tfiles[i]);
  2490. tfile->socket.sk->sk_sndbuf = tun->sndbuf;
  2491. }
  2492. }
  2493. static int tun_set_queue(struct file *file, struct ifreq *ifr)
  2494. {
  2495. struct tun_file *tfile = file->private_data;
  2496. struct tun_struct *tun;
  2497. int ret = 0;
  2498. rtnl_lock();
  2499. if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
  2500. tun = tfile->detached;
  2501. if (!tun) {
  2502. ret = -EINVAL;
  2503. goto unlock;
  2504. }
  2505. ret = security_tun_dev_attach_queue(tun->security);
  2506. if (ret < 0)
  2507. goto unlock;
  2508. ret = tun_attach(tun, file, false, tun->flags & IFF_NAPI,
  2509. tun->flags & IFF_NAPI_FRAGS, true);
  2510. } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
  2511. tun = rtnl_dereference(tfile->tun);
  2512. if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
  2513. ret = -EINVAL;
  2514. else
  2515. __tun_detach(tfile, false);
  2516. } else
  2517. ret = -EINVAL;
  2518. if (ret >= 0)
  2519. netdev_state_change(tun->dev);
  2520. unlock:
  2521. rtnl_unlock();
  2522. return ret;
  2523. }
  2524. static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog **prog_p,
  2525. void __user *data)
  2526. {
  2527. struct bpf_prog *prog;
  2528. int fd;
  2529. if (copy_from_user(&fd, data, sizeof(fd)))
  2530. return -EFAULT;
  2531. if (fd == -1) {
  2532. prog = NULL;
  2533. } else {
  2534. prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
  2535. if (IS_ERR(prog))
  2536. return PTR_ERR(prog);
  2537. }
  2538. return __tun_set_ebpf(tun, prog_p, prog);
  2539. }
  2540. /* Return correct value for tun->dev->addr_len based on tun->dev->type. */
  2541. static unsigned char tun_get_addr_len(unsigned short type)
  2542. {
  2543. switch (type) {
  2544. case ARPHRD_IP6GRE:
  2545. case ARPHRD_TUNNEL6:
  2546. return sizeof(struct in6_addr);
  2547. case ARPHRD_IPGRE:
  2548. case ARPHRD_TUNNEL:
  2549. case ARPHRD_SIT:
  2550. return 4;
  2551. case ARPHRD_ETHER:
  2552. return ETH_ALEN;
  2553. case ARPHRD_IEEE802154:
  2554. case ARPHRD_IEEE802154_MONITOR:
  2555. return IEEE802154_EXTENDED_ADDR_LEN;
  2556. case ARPHRD_PHONET_PIPE:
  2557. case ARPHRD_PPP:
  2558. case ARPHRD_NONE:
  2559. return 0;
  2560. case ARPHRD_6LOWPAN:
  2561. return EUI64_ADDR_LEN;
  2562. case ARPHRD_FDDI:
  2563. return FDDI_K_ALEN;
  2564. case ARPHRD_HIPPI:
  2565. return HIPPI_ALEN;
  2566. case ARPHRD_IEEE802:
  2567. return FC_ALEN;
  2568. case ARPHRD_ROSE:
  2569. return ROSE_ADDR_LEN;
  2570. case ARPHRD_NETROM:
  2571. return AX25_ADDR_LEN;
  2572. case ARPHRD_LOCALTLK:
  2573. return LTALK_ALEN;
  2574. default:
  2575. return 0;
  2576. }
  2577. }
  2578. static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  2579. unsigned long arg, int ifreq_len)
  2580. {
  2581. struct tun_file *tfile = file->private_data;
  2582. struct net *net = sock_net(&tfile->sk);
  2583. struct tun_struct *tun;
  2584. void __user* argp = (void __user*)arg;
  2585. unsigned int ifindex, carrier;
  2586. struct ifreq ifr;
  2587. kuid_t owner;
  2588. kgid_t group;
  2589. int sndbuf;
  2590. int vnet_hdr_sz;
  2591. int le;
  2592. int ret;
  2593. bool do_notify = false;
  2594. if (cmd == TUNSETIFF || cmd == TUNSETQUEUE ||
  2595. (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) {
  2596. if (copy_from_user(&ifr, argp, ifreq_len))
  2597. return -EFAULT;
  2598. } else {
  2599. memset(&ifr, 0, sizeof(ifr));
  2600. }
  2601. if (cmd == TUNGETFEATURES) {
  2602. /* Currently this just means: "what IFF flags are valid?".
  2603. * This is needed because we never checked for invalid flags on
  2604. * TUNSETIFF.
  2605. */
  2606. return put_user(IFF_TUN | IFF_TAP | TUN_FEATURES,
  2607. (unsigned int __user*)argp);
  2608. } else if (cmd == TUNSETQUEUE) {
  2609. return tun_set_queue(file, &ifr);
  2610. } else if (cmd == SIOCGSKNS) {
  2611. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2612. return -EPERM;
  2613. return open_related_ns(&net->ns, get_net_ns);
  2614. }
  2615. ret = 0;
  2616. rtnl_lock();
  2617. tun = tun_get(tfile);
  2618. if (cmd == TUNSETIFF) {
  2619. ret = -EEXIST;
  2620. if (tun)
  2621. goto unlock;
  2622. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  2623. ret = tun_set_iff(net, file, &ifr);
  2624. if (ret)
  2625. goto unlock;
  2626. if (copy_to_user(argp, &ifr, ifreq_len))
  2627. ret = -EFAULT;
  2628. goto unlock;
  2629. }
  2630. if (cmd == TUNSETIFINDEX) {
  2631. ret = -EPERM;
  2632. if (tun)
  2633. goto unlock;
  2634. ret = -EFAULT;
  2635. if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
  2636. goto unlock;
  2637. ret = 0;
  2638. tfile->ifindex = ifindex;
  2639. goto unlock;
  2640. }
  2641. ret = -EBADFD;
  2642. if (!tun)
  2643. goto unlock;
  2644. tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
  2645. net = dev_net(tun->dev);
  2646. ret = 0;
  2647. switch (cmd) {
  2648. case TUNGETIFF:
  2649. tun_get_iff(tun, &ifr);
  2650. if (tfile->detached)
  2651. ifr.ifr_flags |= IFF_DETACH_QUEUE;
  2652. if (!tfile->socket.sk->sk_filter)
  2653. ifr.ifr_flags |= IFF_NOFILTER;
  2654. if (copy_to_user(argp, &ifr, ifreq_len))
  2655. ret = -EFAULT;
  2656. break;
  2657. case TUNSETNOCSUM:
  2658. /* Disable/Enable checksum */
  2659. /* [unimplemented] */
  2660. tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
  2661. arg ? "disabled" : "enabled");
  2662. break;
  2663. case TUNSETPERSIST:
  2664. /* Disable/Enable persist mode. Keep an extra reference to the
  2665. * module to prevent the module being unprobed.
  2666. */
  2667. if (arg && !(tun->flags & IFF_PERSIST)) {
  2668. tun->flags |= IFF_PERSIST;
  2669. __module_get(THIS_MODULE);
  2670. do_notify = true;
  2671. }
  2672. if (!arg && (tun->flags & IFF_PERSIST)) {
  2673. tun->flags &= ~IFF_PERSIST;
  2674. module_put(THIS_MODULE);
  2675. do_notify = true;
  2676. }
  2677. tun_debug(KERN_INFO, tun, "persist %s\n",
  2678. arg ? "enabled" : "disabled");
  2679. break;
  2680. case TUNSETOWNER:
  2681. /* Set owner of the device */
  2682. owner = make_kuid(current_user_ns(), arg);
  2683. if (!uid_valid(owner)) {
  2684. ret = -EINVAL;
  2685. break;
  2686. }
  2687. tun->owner = owner;
  2688. do_notify = true;
  2689. tun_debug(KERN_INFO, tun, "owner set to %u\n",
  2690. from_kuid(&init_user_ns, tun->owner));
  2691. break;
  2692. case TUNSETGROUP:
  2693. /* Set group of the device */
  2694. group = make_kgid(current_user_ns(), arg);
  2695. if (!gid_valid(group)) {
  2696. ret = -EINVAL;
  2697. break;
  2698. }
  2699. tun->group = group;
  2700. do_notify = true;
  2701. tun_debug(KERN_INFO, tun, "group set to %u\n",
  2702. from_kgid(&init_user_ns, tun->group));
  2703. break;
  2704. case TUNSETLINK:
  2705. /* Only allow setting the type when the interface is down */
  2706. if (tun->dev->flags & IFF_UP) {
  2707. tun_debug(KERN_INFO, tun,
  2708. "Linktype set failed because interface is up\n");
  2709. ret = -EBUSY;
  2710. } else {
  2711. tun->dev->type = (int) arg;
  2712. tun->dev->addr_len = tun_get_addr_len(tun->dev->type);
  2713. tun_debug(KERN_INFO, tun, "linktype set to %d\n",
  2714. tun->dev->type);
  2715. ret = 0;
  2716. }
  2717. break;
  2718. #ifdef TUN_DEBUG
  2719. case TUNSETDEBUG:
  2720. tun->debug = arg;
  2721. break;
  2722. #endif
  2723. case TUNSETOFFLOAD:
  2724. ret = set_offload(tun, arg);
  2725. break;
  2726. case TUNSETTXFILTER:
  2727. /* Can be set only for TAPs */
  2728. ret = -EINVAL;
  2729. if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
  2730. break;
  2731. ret = update_filter(&tun->txflt, (void __user *)arg);
  2732. break;
  2733. case SIOCGIFHWADDR:
  2734. /* Get hw address */
  2735. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  2736. dev_get_mac_address(&ifr.ifr_hwaddr, net, tun->dev->name);
  2737. if (copy_to_user(argp, &ifr, ifreq_len))
  2738. ret = -EFAULT;
  2739. break;
  2740. case SIOCSIFHWADDR:
  2741. /* Set hw address */
  2742. tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
  2743. ifr.ifr_hwaddr.sa_data);
  2744. ret = dev_set_mac_address_user(tun->dev, &ifr.ifr_hwaddr, NULL);
  2745. break;
  2746. case TUNGETSNDBUF:
  2747. sndbuf = tfile->socket.sk->sk_sndbuf;
  2748. if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
  2749. ret = -EFAULT;
  2750. break;
  2751. case TUNSETSNDBUF:
  2752. if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
  2753. ret = -EFAULT;
  2754. break;
  2755. }
  2756. if (sndbuf <= 0) {
  2757. ret = -EINVAL;
  2758. break;
  2759. }
  2760. tun->sndbuf = sndbuf;
  2761. tun_set_sndbuf(tun);
  2762. break;
  2763. case TUNGETVNETHDRSZ:
  2764. vnet_hdr_sz = tun->vnet_hdr_sz;
  2765. if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
  2766. ret = -EFAULT;
  2767. break;
  2768. case TUNSETVNETHDRSZ:
  2769. if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
  2770. ret = -EFAULT;
  2771. break;
  2772. }
  2773. if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
  2774. ret = -EINVAL;
  2775. break;
  2776. }
  2777. tun->vnet_hdr_sz = vnet_hdr_sz;
  2778. break;
  2779. case TUNGETVNETLE:
  2780. le = !!(tun->flags & TUN_VNET_LE);
  2781. if (put_user(le, (int __user *)argp))
  2782. ret = -EFAULT;
  2783. break;
  2784. case TUNSETVNETLE:
  2785. if (get_user(le, (int __user *)argp)) {
  2786. ret = -EFAULT;
  2787. break;
  2788. }
  2789. if (le)
  2790. tun->flags |= TUN_VNET_LE;
  2791. else
  2792. tun->flags &= ~TUN_VNET_LE;
  2793. break;
  2794. case TUNGETVNETBE:
  2795. ret = tun_get_vnet_be(tun, argp);
  2796. break;
  2797. case TUNSETVNETBE:
  2798. ret = tun_set_vnet_be(tun, argp);
  2799. break;
  2800. case TUNATTACHFILTER:
  2801. /* Can be set only for TAPs */
  2802. ret = -EINVAL;
  2803. if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
  2804. break;
  2805. ret = -EFAULT;
  2806. if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
  2807. break;
  2808. ret = tun_attach_filter(tun);
  2809. break;
  2810. case TUNDETACHFILTER:
  2811. /* Can be set only for TAPs */
  2812. ret = -EINVAL;
  2813. if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
  2814. break;
  2815. ret = 0;
  2816. tun_detach_filter(tun, tun->numqueues);
  2817. break;
  2818. case TUNGETFILTER:
  2819. ret = -EINVAL;
  2820. if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
  2821. break;
  2822. ret = -EFAULT;
  2823. if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
  2824. break;
  2825. ret = 0;
  2826. break;
  2827. case TUNSETSTEERINGEBPF:
  2828. ret = tun_set_ebpf(tun, &tun->steering_prog, argp);
  2829. break;
  2830. case TUNSETFILTEREBPF:
  2831. ret = tun_set_ebpf(tun, &tun->filter_prog, argp);
  2832. break;
  2833. case TUNSETCARRIER:
  2834. ret = -EFAULT;
  2835. if (copy_from_user(&carrier, argp, sizeof(carrier)))
  2836. goto unlock;
  2837. ret = tun_net_change_carrier(tun->dev, (bool)carrier);
  2838. break;
  2839. case TUNGETDEVNETNS:
  2840. ret = -EPERM;
  2841. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2842. goto unlock;
  2843. ret = open_related_ns(&net->ns, get_net_ns);
  2844. break;
  2845. default:
  2846. ret = -EINVAL;
  2847. break;
  2848. }
  2849. if (do_notify)
  2850. netdev_state_change(tun->dev);
  2851. unlock:
  2852. rtnl_unlock();
  2853. if (tun)
  2854. tun_put(tun);
  2855. return ret;
  2856. }
  2857. static long tun_chr_ioctl(struct file *file,
  2858. unsigned int cmd, unsigned long arg)
  2859. {
  2860. return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
  2861. }
  2862. #ifdef CONFIG_COMPAT
  2863. static long tun_chr_compat_ioctl(struct file *file,
  2864. unsigned int cmd, unsigned long arg)
  2865. {
  2866. switch (cmd) {
  2867. case TUNSETIFF:
  2868. case TUNGETIFF:
  2869. case TUNSETTXFILTER:
  2870. case TUNGETSNDBUF:
  2871. case TUNSETSNDBUF:
  2872. case SIOCGIFHWADDR:
  2873. case SIOCSIFHWADDR:
  2874. arg = (unsigned long)compat_ptr(arg);
  2875. break;
  2876. default:
  2877. arg = (compat_ulong_t)arg;
  2878. break;
  2879. }
  2880. /*
  2881. * compat_ifreq is shorter than ifreq, so we must not access beyond
  2882. * the end of that structure. All fields that are used in this
  2883. * driver are compatible though, we don't need to convert the
  2884. * contents.
  2885. */
  2886. return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
  2887. }
  2888. #endif /* CONFIG_COMPAT */
  2889. static int tun_chr_fasync(int fd, struct file *file, int on)
  2890. {
  2891. struct tun_file *tfile = file->private_data;
  2892. int ret;
  2893. if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
  2894. goto out;
  2895. if (on) {
  2896. __f_setown(file, task_pid(current), PIDTYPE_TGID, 0);
  2897. tfile->flags |= TUN_FASYNC;
  2898. } else
  2899. tfile->flags &= ~TUN_FASYNC;
  2900. ret = 0;
  2901. out:
  2902. return ret;
  2903. }
  2904. static int tun_chr_open(struct inode *inode, struct file * file)
  2905. {
  2906. struct net *net = current->nsproxy->net_ns;
  2907. struct tun_file *tfile;
  2908. DBG1(KERN_INFO, "tunX: tun_chr_open\n");
  2909. tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
  2910. &tun_proto, 0);
  2911. if (!tfile)
  2912. return -ENOMEM;
  2913. if (ptr_ring_init(&tfile->tx_ring, 0, GFP_KERNEL)) {
  2914. sk_free(&tfile->sk);
  2915. return -ENOMEM;
  2916. }
  2917. mutex_init(&tfile->napi_mutex);
  2918. RCU_INIT_POINTER(tfile->tun, NULL);
  2919. tfile->flags = 0;
  2920. tfile->ifindex = 0;
  2921. init_waitqueue_head(&tfile->socket.wq.wait);
  2922. tfile->socket.file = file;
  2923. tfile->socket.ops = &tun_socket_ops;
  2924. sock_init_data(&tfile->socket, &tfile->sk);
  2925. tfile->sk.sk_write_space = tun_sock_write_space;
  2926. tfile->sk.sk_sndbuf = INT_MAX;
  2927. file->private_data = tfile;
  2928. INIT_LIST_HEAD(&tfile->next);
  2929. sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
  2930. return 0;
  2931. }
  2932. static int tun_chr_close(struct inode *inode, struct file *file)
  2933. {
  2934. struct tun_file *tfile = file->private_data;
  2935. tun_detach(tfile, true);
  2936. return 0;
  2937. }
  2938. #ifdef CONFIG_PROC_FS
  2939. static void tun_chr_show_fdinfo(struct seq_file *m, struct file *file)
  2940. {
  2941. struct tun_file *tfile = file->private_data;
  2942. struct tun_struct *tun;
  2943. struct ifreq ifr;
  2944. memset(&ifr, 0, sizeof(ifr));
  2945. rtnl_lock();
  2946. tun = tun_get(tfile);
  2947. if (tun)
  2948. tun_get_iff(tun, &ifr);
  2949. rtnl_unlock();
  2950. if (tun)
  2951. tun_put(tun);
  2952. seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
  2953. }
  2954. #endif
  2955. static const struct file_operations tun_fops = {
  2956. .owner = THIS_MODULE,
  2957. .llseek = no_llseek,
  2958. .read_iter = tun_chr_read_iter,
  2959. .write_iter = tun_chr_write_iter,
  2960. .poll = tun_chr_poll,
  2961. .unlocked_ioctl = tun_chr_ioctl,
  2962. #ifdef CONFIG_COMPAT
  2963. .compat_ioctl = tun_chr_compat_ioctl,
  2964. #endif
  2965. .open = tun_chr_open,
  2966. .release = tun_chr_close,
  2967. .fasync = tun_chr_fasync,
  2968. #ifdef CONFIG_PROC_FS
  2969. .show_fdinfo = tun_chr_show_fdinfo,
  2970. #endif
  2971. };
  2972. static struct miscdevice tun_miscdev = {
  2973. .minor = TUN_MINOR,
  2974. .name = "tun",
  2975. .nodename = "net/tun",
  2976. .fops = &tun_fops,
  2977. };
  2978. /* ethtool interface */
  2979. static void tun_default_link_ksettings(struct net_device *dev,
  2980. struct ethtool_link_ksettings *cmd)
  2981. {
  2982. ethtool_link_ksettings_zero_link_mode(cmd, supported);
  2983. ethtool_link_ksettings_zero_link_mode(cmd, advertising);
  2984. cmd->base.speed = SPEED_10;
  2985. cmd->base.duplex = DUPLEX_FULL;
  2986. cmd->base.port = PORT_TP;
  2987. cmd->base.phy_address = 0;
  2988. cmd->base.autoneg = AUTONEG_DISABLE;
  2989. }
  2990. static int tun_get_link_ksettings(struct net_device *dev,
  2991. struct ethtool_link_ksettings *cmd)
  2992. {
  2993. struct tun_struct *tun = netdev_priv(dev);
  2994. memcpy(cmd, &tun->link_ksettings, sizeof(*cmd));
  2995. return 0;
  2996. }
  2997. static int tun_set_link_ksettings(struct net_device *dev,
  2998. const struct ethtool_link_ksettings *cmd)
  2999. {
  3000. struct tun_struct *tun = netdev_priv(dev);
  3001. memcpy(&tun->link_ksettings, cmd, sizeof(*cmd));
  3002. return 0;
  3003. }
  3004. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  3005. {
  3006. struct tun_struct *tun = netdev_priv(dev);
  3007. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  3008. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  3009. switch (tun->flags & TUN_TYPE_MASK) {
  3010. case IFF_TUN:
  3011. strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
  3012. break;
  3013. case IFF_TAP:
  3014. strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
  3015. break;
  3016. }
  3017. }
  3018. static u32 tun_get_msglevel(struct net_device *dev)
  3019. {
  3020. #ifdef TUN_DEBUG
  3021. struct tun_struct *tun = netdev_priv(dev);
  3022. return tun->debug;
  3023. #else
  3024. return -EOPNOTSUPP;
  3025. #endif
  3026. }
  3027. static void tun_set_msglevel(struct net_device *dev, u32 value)
  3028. {
  3029. #ifdef TUN_DEBUG
  3030. struct tun_struct *tun = netdev_priv(dev);
  3031. tun->debug = value;
  3032. #endif
  3033. }
  3034. static int tun_get_coalesce(struct net_device *dev,
  3035. struct ethtool_coalesce *ec)
  3036. {
  3037. struct tun_struct *tun = netdev_priv(dev);
  3038. ec->rx_max_coalesced_frames = tun->rx_batched;
  3039. return 0;
  3040. }
  3041. static int tun_set_coalesce(struct net_device *dev,
  3042. struct ethtool_coalesce *ec)
  3043. {
  3044. struct tun_struct *tun = netdev_priv(dev);
  3045. if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT)
  3046. tun->rx_batched = NAPI_POLL_WEIGHT;
  3047. else
  3048. tun->rx_batched = ec->rx_max_coalesced_frames;
  3049. return 0;
  3050. }
  3051. static const struct ethtool_ops tun_ethtool_ops = {
  3052. .get_drvinfo = tun_get_drvinfo,
  3053. .get_msglevel = tun_get_msglevel,
  3054. .set_msglevel = tun_set_msglevel,
  3055. .get_link = ethtool_op_get_link,
  3056. .get_ts_info = ethtool_op_get_ts_info,
  3057. .get_coalesce = tun_get_coalesce,
  3058. .set_coalesce = tun_set_coalesce,
  3059. .get_link_ksettings = tun_get_link_ksettings,
  3060. .set_link_ksettings = tun_set_link_ksettings,
  3061. };
  3062. static int tun_queue_resize(struct tun_struct *tun)
  3063. {
  3064. struct net_device *dev = tun->dev;
  3065. struct tun_file *tfile;
  3066. struct ptr_ring **rings;
  3067. int n = tun->numqueues + tun->numdisabled;
  3068. int ret, i;
  3069. rings = kmalloc_array(n, sizeof(*rings), GFP_KERNEL);
  3070. if (!rings)
  3071. return -ENOMEM;
  3072. for (i = 0; i < tun->numqueues; i++) {
  3073. tfile = rtnl_dereference(tun->tfiles[i]);
  3074. rings[i] = &tfile->tx_ring;
  3075. }
  3076. list_for_each_entry(tfile, &tun->disabled, next)
  3077. rings[i++] = &tfile->tx_ring;
  3078. ret = ptr_ring_resize_multiple(rings, n,
  3079. dev->tx_queue_len, GFP_KERNEL,
  3080. tun_ptr_free);
  3081. kfree(rings);
  3082. return ret;
  3083. }
  3084. static int tun_device_event(struct notifier_block *unused,
  3085. unsigned long event, void *ptr)
  3086. {
  3087. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  3088. struct tun_struct *tun = netdev_priv(dev);
  3089. int i;
  3090. if (dev->rtnl_link_ops != &tun_link_ops)
  3091. return NOTIFY_DONE;
  3092. switch (event) {
  3093. case NETDEV_CHANGE_TX_QUEUE_LEN:
  3094. if (tun_queue_resize(tun))
  3095. return NOTIFY_BAD;
  3096. break;
  3097. case NETDEV_UP:
  3098. for (i = 0; i < tun->numqueues; i++) {
  3099. struct tun_file *tfile;
  3100. tfile = rtnl_dereference(tun->tfiles[i]);
  3101. tfile->socket.sk->sk_write_space(tfile->socket.sk);
  3102. }
  3103. break;
  3104. default:
  3105. break;
  3106. }
  3107. return NOTIFY_DONE;
  3108. }
  3109. static struct notifier_block tun_notifier_block __read_mostly = {
  3110. .notifier_call = tun_device_event,
  3111. };
  3112. static int __init tun_init(void)
  3113. {
  3114. int ret = 0;
  3115. pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  3116. ret = rtnl_link_register(&tun_link_ops);
  3117. if (ret) {
  3118. pr_err("Can't register link_ops\n");
  3119. goto err_linkops;
  3120. }
  3121. ret = misc_register(&tun_miscdev);
  3122. if (ret) {
  3123. pr_err("Can't register misc device %d\n", TUN_MINOR);
  3124. goto err_misc;
  3125. }
  3126. ret = register_netdevice_notifier(&tun_notifier_block);
  3127. if (ret) {
  3128. pr_err("Can't register netdevice notifier\n");
  3129. goto err_notifier;
  3130. }
  3131. return 0;
  3132. err_notifier:
  3133. misc_deregister(&tun_miscdev);
  3134. err_misc:
  3135. rtnl_link_unregister(&tun_link_ops);
  3136. err_linkops:
  3137. return ret;
  3138. }
  3139. static void tun_cleanup(void)
  3140. {
  3141. misc_deregister(&tun_miscdev);
  3142. rtnl_link_unregister(&tun_link_ops);
  3143. unregister_netdevice_notifier(&tun_notifier_block);
  3144. }
  3145. /* Get an underlying socket object from tun file. Returns error unless file is
  3146. * attached to a device. The returned object works like a packet socket, it
  3147. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  3148. * holding a reference to the file for as long as the socket is in use. */
  3149. struct socket *tun_get_socket(struct file *file)
  3150. {
  3151. struct tun_file *tfile;
  3152. if (file->f_op != &tun_fops)
  3153. return ERR_PTR(-EINVAL);
  3154. tfile = file->private_data;
  3155. if (!tfile)
  3156. return ERR_PTR(-EBADFD);
  3157. return &tfile->socket;
  3158. }
  3159. EXPORT_SYMBOL_GPL(tun_get_socket);
  3160. struct ptr_ring *tun_get_tx_ring(struct file *file)
  3161. {
  3162. struct tun_file *tfile;
  3163. if (file->f_op != &tun_fops)
  3164. return ERR_PTR(-EINVAL);
  3165. tfile = file->private_data;
  3166. if (!tfile)
  3167. return ERR_PTR(-EBADFD);
  3168. return &tfile->tx_ring;
  3169. }
  3170. EXPORT_SYMBOL_GPL(tun_get_tx_ring);
  3171. module_init(tun_init);
  3172. module_exit(tun_cleanup);
  3173. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  3174. MODULE_AUTHOR(DRV_COPYRIGHT);
  3175. MODULE_LICENSE("GPL");
  3176. MODULE_ALIAS_MISCDEV(TUN_MINOR);
  3177. MODULE_ALIAS("devname:net/tun");