tun.c 88 KB

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