socket.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * NET An implementation of the SOCKET network access protocol.
  4. *
  5. * Version: @(#)socket.c 1.1.93 18/02/95
  6. *
  7. * Authors: Orest Zborowski, <[email protected]>
  8. * Ross Biro
  9. * Fred N. van Kempen, <[email protected]>
  10. *
  11. * Fixes:
  12. * Anonymous : NOTSOCK/BADF cleanup. Error fix in
  13. * shutdown()
  14. * Alan Cox : verify_area() fixes
  15. * Alan Cox : Removed DDI
  16. * Jonathan Kamens : SOCK_DGRAM reconnect bug
  17. * Alan Cox : Moved a load of checks to the very
  18. * top level.
  19. * Alan Cox : Move address structures to/from user
  20. * mode above the protocol layers.
  21. * Rob Janssen : Allow 0 length sends.
  22. * Alan Cox : Asynchronous I/O support (cribbed from the
  23. * tty drivers).
  24. * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
  25. * Jeff Uphoff : Made max number of sockets command-line
  26. * configurable.
  27. * Matti Aarnio : Made the number of sockets dynamic,
  28. * to be allocated when needed, and mr.
  29. * Uphoff's max is used as max to be
  30. * allowed to allocate.
  31. * Linus : Argh. removed all the socket allocation
  32. * altogether: it's in the inode now.
  33. * Alan Cox : Made sock_alloc()/sock_release() public
  34. * for NetROM and future kernel nfsd type
  35. * stuff.
  36. * Alan Cox : sendmsg/recvmsg basics.
  37. * Tom Dyas : Export net symbols.
  38. * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
  39. * Alan Cox : Added thread locking to sys_* calls
  40. * for sockets. May have errors at the
  41. * moment.
  42. * Kevin Buhr : Fixed the dumb errors in the above.
  43. * Andi Kleen : Some small cleanups, optimizations,
  44. * and fixed a copy_from_user() bug.
  45. * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
  46. * Tigran Aivazian : Made listen(2) backlog sanity checks
  47. * protocol-independent
  48. *
  49. * This module is effectively the top level interface to the BSD socket
  50. * paradigm.
  51. *
  52. * Based upon Swansea University Computer Society NET3.039
  53. */
  54. #include <linux/bpf-cgroup.h>
  55. #include <linux/ethtool.h>
  56. #include <linux/mm.h>
  57. #include <linux/socket.h>
  58. #include <linux/file.h>
  59. #include <linux/net.h>
  60. #include <linux/interrupt.h>
  61. #include <linux/thread_info.h>
  62. #include <linux/rcupdate.h>
  63. #include <linux/netdevice.h>
  64. #include <linux/proc_fs.h>
  65. #include <linux/seq_file.h>
  66. #include <linux/mutex.h>
  67. #include <linux/if_bridge.h>
  68. #include <linux/if_vlan.h>
  69. #include <linux/ptp_classify.h>
  70. #include <linux/init.h>
  71. #include <linux/poll.h>
  72. #include <linux/cache.h>
  73. #include <linux/module.h>
  74. #include <linux/highmem.h>
  75. #include <linux/mount.h>
  76. #include <linux/pseudo_fs.h>
  77. #include <linux/security.h>
  78. #include <linux/syscalls.h>
  79. #include <linux/compat.h>
  80. #include <linux/kmod.h>
  81. #include <linux/audit.h>
  82. #include <linux/wireless.h>
  83. #include <linux/nsproxy.h>
  84. #include <linux/magic.h>
  85. #include <linux/slab.h>
  86. #include <linux/xattr.h>
  87. #include <linux/nospec.h>
  88. #include <linux/indirect_call_wrapper.h>
  89. #include <linux/uaccess.h>
  90. #include <asm/unistd.h>
  91. #include <net/compat.h>
  92. #include <net/wext.h>
  93. #include <net/cls_cgroup.h>
  94. #include <net/sock.h>
  95. #include <linux/netfilter.h>
  96. #include <linux/if_tun.h>
  97. #include <linux/ipv6_route.h>
  98. #include <linux/route.h>
  99. #include <linux/termios.h>
  100. #include <linux/sockios.h>
  101. #include <net/busy_poll.h>
  102. #include <linux/errqueue.h>
  103. #include <linux/ptp_clock_kernel.h>
  104. #ifdef CONFIG_NET_RX_BUSY_POLL
  105. unsigned int sysctl_net_busy_read __read_mostly;
  106. unsigned int sysctl_net_busy_poll __read_mostly;
  107. #endif
  108. static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
  109. static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
  110. static int sock_mmap(struct file *file, struct vm_area_struct *vma);
  111. static int sock_close(struct inode *inode, struct file *file);
  112. static __poll_t sock_poll(struct file *file,
  113. struct poll_table_struct *wait);
  114. static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  115. #ifdef CONFIG_COMPAT
  116. static long compat_sock_ioctl(struct file *file,
  117. unsigned int cmd, unsigned long arg);
  118. #endif
  119. static int sock_fasync(int fd, struct file *filp, int on);
  120. static ssize_t sock_sendpage(struct file *file, struct page *page,
  121. int offset, size_t size, loff_t *ppos, int more);
  122. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  123. struct pipe_inode_info *pipe, size_t len,
  124. unsigned int flags);
  125. #ifdef CONFIG_PROC_FS
  126. static void sock_show_fdinfo(struct seq_file *m, struct file *f)
  127. {
  128. struct socket *sock = f->private_data;
  129. if (sock->ops->show_fdinfo)
  130. sock->ops->show_fdinfo(m, sock);
  131. }
  132. #else
  133. #define sock_show_fdinfo NULL
  134. #endif
  135. /*
  136. * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
  137. * in the operation structures but are done directly via the socketcall() multiplexor.
  138. */
  139. static const struct file_operations socket_file_ops = {
  140. .owner = THIS_MODULE,
  141. .llseek = no_llseek,
  142. .read_iter = sock_read_iter,
  143. .write_iter = sock_write_iter,
  144. .poll = sock_poll,
  145. .unlocked_ioctl = sock_ioctl,
  146. #ifdef CONFIG_COMPAT
  147. .compat_ioctl = compat_sock_ioctl,
  148. #endif
  149. .mmap = sock_mmap,
  150. .release = sock_close,
  151. .fasync = sock_fasync,
  152. .sendpage = sock_sendpage,
  153. .splice_write = generic_splice_sendpage,
  154. .splice_read = sock_splice_read,
  155. .show_fdinfo = sock_show_fdinfo,
  156. };
  157. static const char * const pf_family_names[] = {
  158. [PF_UNSPEC] = "PF_UNSPEC",
  159. [PF_UNIX] = "PF_UNIX/PF_LOCAL",
  160. [PF_INET] = "PF_INET",
  161. [PF_AX25] = "PF_AX25",
  162. [PF_IPX] = "PF_IPX",
  163. [PF_APPLETALK] = "PF_APPLETALK",
  164. [PF_NETROM] = "PF_NETROM",
  165. [PF_BRIDGE] = "PF_BRIDGE",
  166. [PF_ATMPVC] = "PF_ATMPVC",
  167. [PF_X25] = "PF_X25",
  168. [PF_INET6] = "PF_INET6",
  169. [PF_ROSE] = "PF_ROSE",
  170. [PF_DECnet] = "PF_DECnet",
  171. [PF_NETBEUI] = "PF_NETBEUI",
  172. [PF_SECURITY] = "PF_SECURITY",
  173. [PF_KEY] = "PF_KEY",
  174. [PF_NETLINK] = "PF_NETLINK/PF_ROUTE",
  175. [PF_PACKET] = "PF_PACKET",
  176. [PF_ASH] = "PF_ASH",
  177. [PF_ECONET] = "PF_ECONET",
  178. [PF_ATMSVC] = "PF_ATMSVC",
  179. [PF_RDS] = "PF_RDS",
  180. [PF_SNA] = "PF_SNA",
  181. [PF_IRDA] = "PF_IRDA",
  182. [PF_PPPOX] = "PF_PPPOX",
  183. [PF_WANPIPE] = "PF_WANPIPE",
  184. [PF_LLC] = "PF_LLC",
  185. [PF_IB] = "PF_IB",
  186. [PF_MPLS] = "PF_MPLS",
  187. [PF_CAN] = "PF_CAN",
  188. [PF_TIPC] = "PF_TIPC",
  189. [PF_BLUETOOTH] = "PF_BLUETOOTH",
  190. [PF_IUCV] = "PF_IUCV",
  191. [PF_RXRPC] = "PF_RXRPC",
  192. [PF_ISDN] = "PF_ISDN",
  193. [PF_PHONET] = "PF_PHONET",
  194. [PF_IEEE802154] = "PF_IEEE802154",
  195. [PF_CAIF] = "PF_CAIF",
  196. [PF_ALG] = "PF_ALG",
  197. [PF_NFC] = "PF_NFC",
  198. [PF_VSOCK] = "PF_VSOCK",
  199. [PF_KCM] = "PF_KCM",
  200. [PF_QIPCRTR] = "PF_QIPCRTR",
  201. [PF_SMC] = "PF_SMC",
  202. [PF_XDP] = "PF_XDP",
  203. [PF_MCTP] = "PF_MCTP",
  204. };
  205. /*
  206. * The protocol list. Each protocol is registered in here.
  207. */
  208. static DEFINE_SPINLOCK(net_family_lock);
  209. static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
  210. /*
  211. * Support routines.
  212. * Move socket addresses back and forth across the kernel/user
  213. * divide and look after the messy bits.
  214. */
  215. /**
  216. * move_addr_to_kernel - copy a socket address into kernel space
  217. * @uaddr: Address in user space
  218. * @kaddr: Address in kernel space
  219. * @ulen: Length in user space
  220. *
  221. * The address is copied into kernel space. If the provided address is
  222. * too long an error code of -EINVAL is returned. If the copy gives
  223. * invalid addresses -EFAULT is returned. On a success 0 is returned.
  224. */
  225. int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
  226. {
  227. if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
  228. return -EINVAL;
  229. if (ulen == 0)
  230. return 0;
  231. if (copy_from_user(kaddr, uaddr, ulen))
  232. return -EFAULT;
  233. return audit_sockaddr(ulen, kaddr);
  234. }
  235. /**
  236. * move_addr_to_user - copy an address to user space
  237. * @kaddr: kernel space address
  238. * @klen: length of address in kernel
  239. * @uaddr: user space address
  240. * @ulen: pointer to user length field
  241. *
  242. * The value pointed to by ulen on entry is the buffer length available.
  243. * This is overwritten with the buffer space used. -EINVAL is returned
  244. * if an overlong buffer is specified or a negative buffer size. -EFAULT
  245. * is returned if either the buffer or the length field are not
  246. * accessible.
  247. * After copying the data up to the limit the user specifies, the true
  248. * length of the data is written over the length limit the user
  249. * specified. Zero is returned for a success.
  250. */
  251. static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
  252. void __user *uaddr, int __user *ulen)
  253. {
  254. int err;
  255. int len;
  256. BUG_ON(klen > sizeof(struct sockaddr_storage));
  257. err = get_user(len, ulen);
  258. if (err)
  259. return err;
  260. if (len > klen)
  261. len = klen;
  262. if (len < 0)
  263. return -EINVAL;
  264. if (len) {
  265. if (audit_sockaddr(klen, kaddr))
  266. return -ENOMEM;
  267. if (copy_to_user(uaddr, kaddr, len))
  268. return -EFAULT;
  269. }
  270. /*
  271. * "fromlen shall refer to the value before truncation.."
  272. * 1003.1g
  273. */
  274. return __put_user(klen, ulen);
  275. }
  276. static struct kmem_cache *sock_inode_cachep __ro_after_init;
  277. static struct inode *sock_alloc_inode(struct super_block *sb)
  278. {
  279. struct socket_alloc *ei;
  280. ei = alloc_inode_sb(sb, sock_inode_cachep, GFP_KERNEL);
  281. if (!ei)
  282. return NULL;
  283. init_waitqueue_head(&ei->socket.wq.wait);
  284. ei->socket.wq.fasync_list = NULL;
  285. ei->socket.wq.flags = 0;
  286. ei->socket.state = SS_UNCONNECTED;
  287. ei->socket.flags = 0;
  288. ei->socket.ops = NULL;
  289. ei->socket.sk = NULL;
  290. ei->socket.file = NULL;
  291. return &ei->vfs_inode;
  292. }
  293. static void sock_free_inode(struct inode *inode)
  294. {
  295. struct socket_alloc *ei;
  296. ei = container_of(inode, struct socket_alloc, vfs_inode);
  297. kmem_cache_free(sock_inode_cachep, ei);
  298. }
  299. static void init_once(void *foo)
  300. {
  301. struct socket_alloc *ei = (struct socket_alloc *)foo;
  302. inode_init_once(&ei->vfs_inode);
  303. }
  304. static void init_inodecache(void)
  305. {
  306. sock_inode_cachep = kmem_cache_create("sock_inode_cache",
  307. sizeof(struct socket_alloc),
  308. 0,
  309. (SLAB_HWCACHE_ALIGN |
  310. SLAB_RECLAIM_ACCOUNT |
  311. SLAB_MEM_SPREAD | SLAB_ACCOUNT),
  312. init_once);
  313. BUG_ON(sock_inode_cachep == NULL);
  314. }
  315. static const struct super_operations sockfs_ops = {
  316. .alloc_inode = sock_alloc_inode,
  317. .free_inode = sock_free_inode,
  318. .statfs = simple_statfs,
  319. };
  320. /*
  321. * sockfs_dname() is called from d_path().
  322. */
  323. static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
  324. {
  325. return dynamic_dname(buffer, buflen, "socket:[%lu]",
  326. d_inode(dentry)->i_ino);
  327. }
  328. static const struct dentry_operations sockfs_dentry_operations = {
  329. .d_dname = sockfs_dname,
  330. };
  331. static int sockfs_xattr_get(const struct xattr_handler *handler,
  332. struct dentry *dentry, struct inode *inode,
  333. const char *suffix, void *value, size_t size)
  334. {
  335. if (value) {
  336. if (dentry->d_name.len + 1 > size)
  337. return -ERANGE;
  338. memcpy(value, dentry->d_name.name, dentry->d_name.len + 1);
  339. }
  340. return dentry->d_name.len + 1;
  341. }
  342. #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
  343. #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
  344. #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
  345. static const struct xattr_handler sockfs_xattr_handler = {
  346. .name = XATTR_NAME_SOCKPROTONAME,
  347. .get = sockfs_xattr_get,
  348. };
  349. static int sockfs_security_xattr_set(const struct xattr_handler *handler,
  350. struct user_namespace *mnt_userns,
  351. struct dentry *dentry, struct inode *inode,
  352. const char *suffix, const void *value,
  353. size_t size, int flags)
  354. {
  355. /* Handled by LSM. */
  356. return -EAGAIN;
  357. }
  358. static const struct xattr_handler sockfs_security_xattr_handler = {
  359. .prefix = XATTR_SECURITY_PREFIX,
  360. .set = sockfs_security_xattr_set,
  361. };
  362. static const struct xattr_handler *sockfs_xattr_handlers[] = {
  363. &sockfs_xattr_handler,
  364. &sockfs_security_xattr_handler,
  365. NULL
  366. };
  367. static int sockfs_init_fs_context(struct fs_context *fc)
  368. {
  369. struct pseudo_fs_context *ctx = init_pseudo(fc, SOCKFS_MAGIC);
  370. if (!ctx)
  371. return -ENOMEM;
  372. ctx->ops = &sockfs_ops;
  373. ctx->dops = &sockfs_dentry_operations;
  374. ctx->xattr = sockfs_xattr_handlers;
  375. return 0;
  376. }
  377. static struct vfsmount *sock_mnt __read_mostly;
  378. static struct file_system_type sock_fs_type = {
  379. .name = "sockfs",
  380. .init_fs_context = sockfs_init_fs_context,
  381. .kill_sb = kill_anon_super,
  382. };
  383. /*
  384. * Obtains the first available file descriptor and sets it up for use.
  385. *
  386. * These functions create file structures and maps them to fd space
  387. * of the current process. On success it returns file descriptor
  388. * and file struct implicitly stored in sock->file.
  389. * Note that another thread may close file descriptor before we return
  390. * from this function. We use the fact that now we do not refer
  391. * to socket after mapping. If one day we will need it, this
  392. * function will increment ref. count on file by 1.
  393. *
  394. * In any case returned fd MAY BE not valid!
  395. * This race condition is unavoidable
  396. * with shared fd spaces, we cannot solve it inside kernel,
  397. * but we take care of internal coherence yet.
  398. */
  399. /**
  400. * sock_alloc_file - Bind a &socket to a &file
  401. * @sock: socket
  402. * @flags: file status flags
  403. * @dname: protocol name
  404. *
  405. * Returns the &file bound with @sock, implicitly storing it
  406. * in sock->file. If dname is %NULL, sets to "".
  407. *
  408. * On failure @sock is released, and an ERR pointer is returned.
  409. *
  410. * This function uses GFP_KERNEL internally.
  411. */
  412. struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
  413. {
  414. struct file *file;
  415. if (!dname)
  416. dname = sock->sk ? sock->sk->sk_prot_creator->name : "";
  417. file = alloc_file_pseudo(SOCK_INODE(sock), sock_mnt, dname,
  418. O_RDWR | (flags & O_NONBLOCK),
  419. &socket_file_ops);
  420. if (IS_ERR(file)) {
  421. sock_release(sock);
  422. return file;
  423. }
  424. sock->file = file;
  425. file->private_data = sock;
  426. stream_open(SOCK_INODE(sock), file);
  427. return file;
  428. }
  429. EXPORT_SYMBOL(sock_alloc_file);
  430. static int sock_map_fd(struct socket *sock, int flags)
  431. {
  432. struct file *newfile;
  433. int fd = get_unused_fd_flags(flags);
  434. if (unlikely(fd < 0)) {
  435. sock_release(sock);
  436. return fd;
  437. }
  438. newfile = sock_alloc_file(sock, flags, NULL);
  439. if (!IS_ERR(newfile)) {
  440. fd_install(fd, newfile);
  441. return fd;
  442. }
  443. put_unused_fd(fd);
  444. return PTR_ERR(newfile);
  445. }
  446. /**
  447. * sock_from_file - Return the &socket bounded to @file.
  448. * @file: file
  449. *
  450. * On failure returns %NULL.
  451. */
  452. struct socket *sock_from_file(struct file *file)
  453. {
  454. if (file->f_op == &socket_file_ops)
  455. return file->private_data; /* set in sock_alloc_file */
  456. return NULL;
  457. }
  458. EXPORT_SYMBOL(sock_from_file);
  459. /**
  460. * sockfd_lookup - Go from a file number to its socket slot
  461. * @fd: file handle
  462. * @err: pointer to an error code return
  463. *
  464. * The file handle passed in is locked and the socket it is bound
  465. * to is returned. If an error occurs the err pointer is overwritten
  466. * with a negative errno code and NULL is returned. The function checks
  467. * for both invalid handles and passing a handle which is not a socket.
  468. *
  469. * On a success the socket object pointer is returned.
  470. */
  471. struct socket *sockfd_lookup(int fd, int *err)
  472. {
  473. struct file *file;
  474. struct socket *sock;
  475. file = fget(fd);
  476. if (!file) {
  477. *err = -EBADF;
  478. return NULL;
  479. }
  480. sock = sock_from_file(file);
  481. if (!sock) {
  482. *err = -ENOTSOCK;
  483. fput(file);
  484. }
  485. return sock;
  486. }
  487. EXPORT_SYMBOL(sockfd_lookup);
  488. static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
  489. {
  490. struct fd f = fdget(fd);
  491. struct socket *sock;
  492. *err = -EBADF;
  493. if (f.file) {
  494. sock = sock_from_file(f.file);
  495. if (likely(sock)) {
  496. *fput_needed = f.flags & FDPUT_FPUT;
  497. return sock;
  498. }
  499. *err = -ENOTSOCK;
  500. fdput(f);
  501. }
  502. return NULL;
  503. }
  504. static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
  505. size_t size)
  506. {
  507. ssize_t len;
  508. ssize_t used = 0;
  509. len = security_inode_listsecurity(d_inode(dentry), buffer, size);
  510. if (len < 0)
  511. return len;
  512. used += len;
  513. if (buffer) {
  514. if (size < used)
  515. return -ERANGE;
  516. buffer += len;
  517. }
  518. len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
  519. used += len;
  520. if (buffer) {
  521. if (size < used)
  522. return -ERANGE;
  523. memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
  524. buffer += len;
  525. }
  526. return used;
  527. }
  528. static int sockfs_setattr(struct user_namespace *mnt_userns,
  529. struct dentry *dentry, struct iattr *iattr)
  530. {
  531. int err = simple_setattr(&init_user_ns, dentry, iattr);
  532. if (!err && (iattr->ia_valid & ATTR_UID)) {
  533. struct socket *sock = SOCKET_I(d_inode(dentry));
  534. if (sock->sk)
  535. sock->sk->sk_uid = iattr->ia_uid;
  536. else
  537. err = -ENOENT;
  538. }
  539. return err;
  540. }
  541. static const struct inode_operations sockfs_inode_ops = {
  542. .listxattr = sockfs_listxattr,
  543. .setattr = sockfs_setattr,
  544. };
  545. /**
  546. * sock_alloc - allocate a socket
  547. *
  548. * Allocate a new inode and socket object. The two are bound together
  549. * and initialised. The socket is then returned. If we are out of inodes
  550. * NULL is returned. This functions uses GFP_KERNEL internally.
  551. */
  552. struct socket *sock_alloc(void)
  553. {
  554. struct inode *inode;
  555. struct socket *sock;
  556. inode = new_inode_pseudo(sock_mnt->mnt_sb);
  557. if (!inode)
  558. return NULL;
  559. sock = SOCKET_I(inode);
  560. inode->i_ino = get_next_ino();
  561. inode->i_mode = S_IFSOCK | S_IRWXUGO;
  562. inode->i_uid = current_fsuid();
  563. inode->i_gid = current_fsgid();
  564. inode->i_op = &sockfs_inode_ops;
  565. return sock;
  566. }
  567. EXPORT_SYMBOL(sock_alloc);
  568. static void __sock_release(struct socket *sock, struct inode *inode)
  569. {
  570. if (sock->ops) {
  571. struct module *owner = sock->ops->owner;
  572. if (inode)
  573. inode_lock(inode);
  574. sock->ops->release(sock);
  575. sock->sk = NULL;
  576. if (inode)
  577. inode_unlock(inode);
  578. sock->ops = NULL;
  579. module_put(owner);
  580. }
  581. if (sock->wq.fasync_list)
  582. pr_err("%s: fasync list not empty!\n", __func__);
  583. if (!sock->file) {
  584. iput(SOCK_INODE(sock));
  585. return;
  586. }
  587. sock->file = NULL;
  588. }
  589. /**
  590. * sock_release - close a socket
  591. * @sock: socket to close
  592. *
  593. * The socket is released from the protocol stack if it has a release
  594. * callback, and the inode is then released if the socket is bound to
  595. * an inode not a file.
  596. */
  597. void sock_release(struct socket *sock)
  598. {
  599. __sock_release(sock, NULL);
  600. }
  601. EXPORT_SYMBOL(sock_release);
  602. void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags)
  603. {
  604. u8 flags = *tx_flags;
  605. if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE) {
  606. flags |= SKBTX_HW_TSTAMP;
  607. /* PTP hardware clocks can provide a free running cycle counter
  608. * as a time base for virtual clocks. Tell driver to use the
  609. * free running cycle counter for timestamp if socket is bound
  610. * to virtual clock.
  611. */
  612. if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
  613. flags |= SKBTX_HW_TSTAMP_USE_CYCLES;
  614. }
  615. if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
  616. flags |= SKBTX_SW_TSTAMP;
  617. if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
  618. flags |= SKBTX_SCHED_TSTAMP;
  619. *tx_flags = flags;
  620. }
  621. EXPORT_SYMBOL(__sock_tx_timestamp);
  622. INDIRECT_CALLABLE_DECLARE(int inet_sendmsg(struct socket *, struct msghdr *,
  623. size_t));
  624. INDIRECT_CALLABLE_DECLARE(int inet6_sendmsg(struct socket *, struct msghdr *,
  625. size_t));
  626. static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
  627. {
  628. int ret = INDIRECT_CALL_INET(sock->ops->sendmsg, inet6_sendmsg,
  629. inet_sendmsg, sock, msg,
  630. msg_data_left(msg));
  631. BUG_ON(ret == -EIOCBQUEUED);
  632. return ret;
  633. }
  634. static int __sock_sendmsg(struct socket *sock, struct msghdr *msg)
  635. {
  636. int err = security_socket_sendmsg(sock, msg,
  637. msg_data_left(msg));
  638. return err ?: sock_sendmsg_nosec(sock, msg);
  639. }
  640. /**
  641. * sock_sendmsg - send a message through @sock
  642. * @sock: socket
  643. * @msg: message to send
  644. *
  645. * Sends @msg through @sock, passing through LSM.
  646. * Returns the number of bytes sent, or an error code.
  647. */
  648. int sock_sendmsg(struct socket *sock, struct msghdr *msg)
  649. {
  650. struct sockaddr_storage *save_addr = (struct sockaddr_storage *)msg->msg_name;
  651. struct sockaddr_storage address;
  652. int ret;
  653. if (msg->msg_name) {
  654. memcpy(&address, msg->msg_name, msg->msg_namelen);
  655. msg->msg_name = &address;
  656. }
  657. ret = __sock_sendmsg(sock, msg);
  658. msg->msg_name = save_addr;
  659. return ret;
  660. }
  661. EXPORT_SYMBOL(sock_sendmsg);
  662. /**
  663. * kernel_sendmsg - send a message through @sock (kernel-space)
  664. * @sock: socket
  665. * @msg: message header
  666. * @vec: kernel vec
  667. * @num: vec array length
  668. * @size: total message data size
  669. *
  670. * Builds the message data with @vec and sends it through @sock.
  671. * Returns the number of bytes sent, or an error code.
  672. */
  673. int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
  674. struct kvec *vec, size_t num, size_t size)
  675. {
  676. iov_iter_kvec(&msg->msg_iter, ITER_SOURCE, vec, num, size);
  677. return sock_sendmsg(sock, msg);
  678. }
  679. EXPORT_SYMBOL(kernel_sendmsg);
  680. /**
  681. * kernel_sendmsg_locked - send a message through @sock (kernel-space)
  682. * @sk: sock
  683. * @msg: message header
  684. * @vec: output s/g array
  685. * @num: output s/g array length
  686. * @size: total message data size
  687. *
  688. * Builds the message data with @vec and sends it through @sock.
  689. * Returns the number of bytes sent, or an error code.
  690. * Caller must hold @sk.
  691. */
  692. int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
  693. struct kvec *vec, size_t num, size_t size)
  694. {
  695. struct socket *sock = sk->sk_socket;
  696. if (!sock->ops->sendmsg_locked)
  697. return sock_no_sendmsg_locked(sk, msg, size);
  698. iov_iter_kvec(&msg->msg_iter, ITER_SOURCE, vec, num, size);
  699. return sock->ops->sendmsg_locked(sk, msg, msg_data_left(msg));
  700. }
  701. EXPORT_SYMBOL(kernel_sendmsg_locked);
  702. static bool skb_is_err_queue(const struct sk_buff *skb)
  703. {
  704. /* pkt_type of skbs enqueued on the error queue are set to
  705. * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
  706. * in recvmsg, since skbs received on a local socket will never
  707. * have a pkt_type of PACKET_OUTGOING.
  708. */
  709. return skb->pkt_type == PACKET_OUTGOING;
  710. }
  711. /* On transmit, software and hardware timestamps are returned independently.
  712. * As the two skb clones share the hardware timestamp, which may be updated
  713. * before the software timestamp is received, a hardware TX timestamp may be
  714. * returned only if there is no software TX timestamp. Ignore false software
  715. * timestamps, which may be made in the __sock_recv_timestamp() call when the
  716. * option SO_TIMESTAMP_OLD(NS) is enabled on the socket, even when the skb has a
  717. * hardware timestamp.
  718. */
  719. static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
  720. {
  721. return skb->tstamp && !false_tstamp && skb_is_err_queue(skb);
  722. }
  723. static ktime_t get_timestamp(struct sock *sk, struct sk_buff *skb, int *if_index)
  724. {
  725. bool cycles = sk->sk_tsflags & SOF_TIMESTAMPING_BIND_PHC;
  726. struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
  727. struct net_device *orig_dev;
  728. ktime_t hwtstamp;
  729. rcu_read_lock();
  730. orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
  731. if (orig_dev) {
  732. *if_index = orig_dev->ifindex;
  733. hwtstamp = netdev_get_tstamp(orig_dev, shhwtstamps, cycles);
  734. } else {
  735. hwtstamp = shhwtstamps->hwtstamp;
  736. }
  737. rcu_read_unlock();
  738. return hwtstamp;
  739. }
  740. static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb,
  741. int if_index)
  742. {
  743. struct scm_ts_pktinfo ts_pktinfo;
  744. struct net_device *orig_dev;
  745. if (!skb_mac_header_was_set(skb))
  746. return;
  747. memset(&ts_pktinfo, 0, sizeof(ts_pktinfo));
  748. if (!if_index) {
  749. rcu_read_lock();
  750. orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
  751. if (orig_dev)
  752. if_index = orig_dev->ifindex;
  753. rcu_read_unlock();
  754. }
  755. ts_pktinfo.if_index = if_index;
  756. ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb);
  757. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
  758. sizeof(ts_pktinfo), &ts_pktinfo);
  759. }
  760. /*
  761. * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
  762. */
  763. void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
  764. struct sk_buff *skb)
  765. {
  766. int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
  767. int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW);
  768. struct scm_timestamping_internal tss;
  769. int empty = 1, false_tstamp = 0;
  770. struct skb_shared_hwtstamps *shhwtstamps =
  771. skb_hwtstamps(skb);
  772. int if_index;
  773. ktime_t hwtstamp;
  774. /* Race occurred between timestamp enabling and packet
  775. receiving. Fill in the current time for now. */
  776. if (need_software_tstamp && skb->tstamp == 0) {
  777. __net_timestamp(skb);
  778. false_tstamp = 1;
  779. }
  780. if (need_software_tstamp) {
  781. if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
  782. if (new_tstamp) {
  783. struct __kernel_sock_timeval tv;
  784. skb_get_new_timestamp(skb, &tv);
  785. put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_NEW,
  786. sizeof(tv), &tv);
  787. } else {
  788. struct __kernel_old_timeval tv;
  789. skb_get_timestamp(skb, &tv);
  790. put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_OLD,
  791. sizeof(tv), &tv);
  792. }
  793. } else {
  794. if (new_tstamp) {
  795. struct __kernel_timespec ts;
  796. skb_get_new_timestampns(skb, &ts);
  797. put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_NEW,
  798. sizeof(ts), &ts);
  799. } else {
  800. struct __kernel_old_timespec ts;
  801. skb_get_timestampns(skb, &ts);
  802. put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_OLD,
  803. sizeof(ts), &ts);
  804. }
  805. }
  806. }
  807. memset(&tss, 0, sizeof(tss));
  808. if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
  809. ktime_to_timespec64_cond(skb->tstamp, tss.ts + 0))
  810. empty = 0;
  811. if (shhwtstamps &&
  812. (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
  813. !skb_is_swtx_tstamp(skb, false_tstamp)) {
  814. if_index = 0;
  815. if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
  816. hwtstamp = get_timestamp(sk, skb, &if_index);
  817. else
  818. hwtstamp = shhwtstamps->hwtstamp;
  819. if (sk->sk_tsflags & SOF_TIMESTAMPING_BIND_PHC)
  820. hwtstamp = ptp_convert_timestamp(&hwtstamp,
  821. sk->sk_bind_phc);
  822. if (ktime_to_timespec64_cond(hwtstamp, tss.ts + 2)) {
  823. empty = 0;
  824. if ((sk->sk_tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) &&
  825. !skb_is_err_queue(skb))
  826. put_ts_pktinfo(msg, skb, if_index);
  827. }
  828. }
  829. if (!empty) {
  830. if (sock_flag(sk, SOCK_TSTAMP_NEW))
  831. put_cmsg_scm_timestamping64(msg, &tss);
  832. else
  833. put_cmsg_scm_timestamping(msg, &tss);
  834. if (skb_is_err_queue(skb) && skb->len &&
  835. SKB_EXT_ERR(skb)->opt_stats)
  836. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS,
  837. skb->len, skb->data);
  838. }
  839. }
  840. EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
  841. void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
  842. struct sk_buff *skb)
  843. {
  844. int ack;
  845. if (!sock_flag(sk, SOCK_WIFI_STATUS))
  846. return;
  847. if (!skb->wifi_acked_valid)
  848. return;
  849. ack = skb->wifi_acked;
  850. put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
  851. }
  852. EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
  853. static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
  854. struct sk_buff *skb)
  855. {
  856. if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
  857. put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
  858. sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
  859. }
  860. static void sock_recv_mark(struct msghdr *msg, struct sock *sk,
  861. struct sk_buff *skb)
  862. {
  863. if (sock_flag(sk, SOCK_RCVMARK) && skb) {
  864. /* We must use a bounce buffer for CONFIG_HARDENED_USERCOPY=y */
  865. __u32 mark = skb->mark;
  866. put_cmsg(msg, SOL_SOCKET, SO_MARK, sizeof(__u32), &mark);
  867. }
  868. }
  869. void __sock_recv_cmsgs(struct msghdr *msg, struct sock *sk,
  870. struct sk_buff *skb)
  871. {
  872. sock_recv_timestamp(msg, sk, skb);
  873. sock_recv_drops(msg, sk, skb);
  874. sock_recv_mark(msg, sk, skb);
  875. }
  876. EXPORT_SYMBOL_GPL(__sock_recv_cmsgs);
  877. INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *,
  878. size_t, int));
  879. INDIRECT_CALLABLE_DECLARE(int inet6_recvmsg(struct socket *, struct msghdr *,
  880. size_t, int));
  881. static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
  882. int flags)
  883. {
  884. return INDIRECT_CALL_INET(sock->ops->recvmsg, inet6_recvmsg,
  885. inet_recvmsg, sock, msg, msg_data_left(msg),
  886. flags);
  887. }
  888. /**
  889. * sock_recvmsg - receive a message from @sock
  890. * @sock: socket
  891. * @msg: message to receive
  892. * @flags: message flags
  893. *
  894. * Receives @msg from @sock, passing through LSM. Returns the total number
  895. * of bytes received, or an error.
  896. */
  897. int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
  898. {
  899. int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
  900. return err ?: sock_recvmsg_nosec(sock, msg, flags);
  901. }
  902. EXPORT_SYMBOL(sock_recvmsg);
  903. /**
  904. * kernel_recvmsg - Receive a message from a socket (kernel space)
  905. * @sock: The socket to receive the message from
  906. * @msg: Received message
  907. * @vec: Input s/g array for message data
  908. * @num: Size of input s/g array
  909. * @size: Number of bytes to read
  910. * @flags: Message flags (MSG_DONTWAIT, etc...)
  911. *
  912. * On return the msg structure contains the scatter/gather array passed in the
  913. * vec argument. The array is modified so that it consists of the unfilled
  914. * portion of the original array.
  915. *
  916. * The returned value is the total number of bytes received, or an error.
  917. */
  918. int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
  919. struct kvec *vec, size_t num, size_t size, int flags)
  920. {
  921. msg->msg_control_is_user = false;
  922. iov_iter_kvec(&msg->msg_iter, ITER_DEST, vec, num, size);
  923. return sock_recvmsg(sock, msg, flags);
  924. }
  925. EXPORT_SYMBOL(kernel_recvmsg);
  926. static ssize_t sock_sendpage(struct file *file, struct page *page,
  927. int offset, size_t size, loff_t *ppos, int more)
  928. {
  929. struct socket *sock;
  930. int flags;
  931. sock = file->private_data;
  932. flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  933. /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
  934. flags |= more;
  935. return kernel_sendpage(sock, page, offset, size, flags);
  936. }
  937. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  938. struct pipe_inode_info *pipe, size_t len,
  939. unsigned int flags)
  940. {
  941. struct socket *sock = file->private_data;
  942. if (unlikely(!sock->ops->splice_read))
  943. return generic_file_splice_read(file, ppos, pipe, len, flags);
  944. return sock->ops->splice_read(sock, ppos, pipe, len, flags);
  945. }
  946. static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
  947. {
  948. struct file *file = iocb->ki_filp;
  949. struct socket *sock = file->private_data;
  950. struct msghdr msg = {.msg_iter = *to,
  951. .msg_iocb = iocb};
  952. ssize_t res;
  953. if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
  954. msg.msg_flags = MSG_DONTWAIT;
  955. if (iocb->ki_pos != 0)
  956. return -ESPIPE;
  957. if (!iov_iter_count(to)) /* Match SYS5 behaviour */
  958. return 0;
  959. res = sock_recvmsg(sock, &msg, msg.msg_flags);
  960. *to = msg.msg_iter;
  961. return res;
  962. }
  963. static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
  964. {
  965. struct file *file = iocb->ki_filp;
  966. struct socket *sock = file->private_data;
  967. struct msghdr msg = {.msg_iter = *from,
  968. .msg_iocb = iocb};
  969. ssize_t res;
  970. if (iocb->ki_pos != 0)
  971. return -ESPIPE;
  972. if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
  973. msg.msg_flags = MSG_DONTWAIT;
  974. if (sock->type == SOCK_SEQPACKET)
  975. msg.msg_flags |= MSG_EOR;
  976. res = __sock_sendmsg(sock, &msg);
  977. *from = msg.msg_iter;
  978. return res;
  979. }
  980. /*
  981. * Atomic setting of ioctl hooks to avoid race
  982. * with module unload.
  983. */
  984. static DEFINE_MUTEX(br_ioctl_mutex);
  985. static int (*br_ioctl_hook)(struct net *net, struct net_bridge *br,
  986. unsigned int cmd, struct ifreq *ifr,
  987. void __user *uarg);
  988. void brioctl_set(int (*hook)(struct net *net, struct net_bridge *br,
  989. unsigned int cmd, struct ifreq *ifr,
  990. void __user *uarg))
  991. {
  992. mutex_lock(&br_ioctl_mutex);
  993. br_ioctl_hook = hook;
  994. mutex_unlock(&br_ioctl_mutex);
  995. }
  996. EXPORT_SYMBOL(brioctl_set);
  997. int br_ioctl_call(struct net *net, struct net_bridge *br, unsigned int cmd,
  998. struct ifreq *ifr, void __user *uarg)
  999. {
  1000. int err = -ENOPKG;
  1001. if (!br_ioctl_hook)
  1002. request_module("bridge");
  1003. mutex_lock(&br_ioctl_mutex);
  1004. if (br_ioctl_hook)
  1005. err = br_ioctl_hook(net, br, cmd, ifr, uarg);
  1006. mutex_unlock(&br_ioctl_mutex);
  1007. return err;
  1008. }
  1009. static DEFINE_MUTEX(vlan_ioctl_mutex);
  1010. static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
  1011. void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
  1012. {
  1013. mutex_lock(&vlan_ioctl_mutex);
  1014. vlan_ioctl_hook = hook;
  1015. mutex_unlock(&vlan_ioctl_mutex);
  1016. }
  1017. EXPORT_SYMBOL(vlan_ioctl_set);
  1018. static long sock_do_ioctl(struct net *net, struct socket *sock,
  1019. unsigned int cmd, unsigned long arg)
  1020. {
  1021. struct ifreq ifr;
  1022. bool need_copyout;
  1023. int err;
  1024. void __user *argp = (void __user *)arg;
  1025. void __user *data;
  1026. err = sock->ops->ioctl(sock, cmd, arg);
  1027. /*
  1028. * If this ioctl is unknown try to hand it down
  1029. * to the NIC driver.
  1030. */
  1031. if (err != -ENOIOCTLCMD)
  1032. return err;
  1033. if (!is_socket_ioctl_cmd(cmd))
  1034. return -ENOTTY;
  1035. if (get_user_ifreq(&ifr, &data, argp))
  1036. return -EFAULT;
  1037. err = dev_ioctl(net, cmd, &ifr, data, &need_copyout);
  1038. if (!err && need_copyout)
  1039. if (put_user_ifreq(&ifr, argp))
  1040. return -EFAULT;
  1041. return err;
  1042. }
  1043. /*
  1044. * With an ioctl, arg may well be a user mode pointer, but we don't know
  1045. * what to do with it - that's up to the protocol still.
  1046. */
  1047. static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  1048. {
  1049. struct socket *sock;
  1050. struct sock *sk;
  1051. void __user *argp = (void __user *)arg;
  1052. int pid, err;
  1053. struct net *net;
  1054. sock = file->private_data;
  1055. sk = sock->sk;
  1056. net = sock_net(sk);
  1057. if (unlikely(cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))) {
  1058. struct ifreq ifr;
  1059. void __user *data;
  1060. bool need_copyout;
  1061. if (get_user_ifreq(&ifr, &data, argp))
  1062. return -EFAULT;
  1063. err = dev_ioctl(net, cmd, &ifr, data, &need_copyout);
  1064. if (!err && need_copyout)
  1065. if (put_user_ifreq(&ifr, argp))
  1066. return -EFAULT;
  1067. } else
  1068. #ifdef CONFIG_WEXT_CORE
  1069. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
  1070. err = wext_handle_ioctl(net, cmd, argp);
  1071. } else
  1072. #endif
  1073. switch (cmd) {
  1074. case FIOSETOWN:
  1075. case SIOCSPGRP:
  1076. err = -EFAULT;
  1077. if (get_user(pid, (int __user *)argp))
  1078. break;
  1079. err = f_setown(sock->file, pid, 1);
  1080. break;
  1081. case FIOGETOWN:
  1082. case SIOCGPGRP:
  1083. err = put_user(f_getown(sock->file),
  1084. (int __user *)argp);
  1085. break;
  1086. case SIOCGIFBR:
  1087. case SIOCSIFBR:
  1088. case SIOCBRADDBR:
  1089. case SIOCBRDELBR:
  1090. err = br_ioctl_call(net, NULL, cmd, NULL, argp);
  1091. break;
  1092. case SIOCGIFVLAN:
  1093. case SIOCSIFVLAN:
  1094. err = -ENOPKG;
  1095. if (!vlan_ioctl_hook)
  1096. request_module("8021q");
  1097. mutex_lock(&vlan_ioctl_mutex);
  1098. if (vlan_ioctl_hook)
  1099. err = vlan_ioctl_hook(net, argp);
  1100. mutex_unlock(&vlan_ioctl_mutex);
  1101. break;
  1102. case SIOCGSKNS:
  1103. err = -EPERM;
  1104. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1105. break;
  1106. err = open_related_ns(&net->ns, get_net_ns);
  1107. break;
  1108. case SIOCGSTAMP_OLD:
  1109. case SIOCGSTAMPNS_OLD:
  1110. if (!sock->ops->gettstamp) {
  1111. err = -ENOIOCTLCMD;
  1112. break;
  1113. }
  1114. err = sock->ops->gettstamp(sock, argp,
  1115. cmd == SIOCGSTAMP_OLD,
  1116. !IS_ENABLED(CONFIG_64BIT));
  1117. break;
  1118. case SIOCGSTAMP_NEW:
  1119. case SIOCGSTAMPNS_NEW:
  1120. if (!sock->ops->gettstamp) {
  1121. err = -ENOIOCTLCMD;
  1122. break;
  1123. }
  1124. err = sock->ops->gettstamp(sock, argp,
  1125. cmd == SIOCGSTAMP_NEW,
  1126. false);
  1127. break;
  1128. case SIOCGIFCONF:
  1129. err = dev_ifconf(net, argp);
  1130. break;
  1131. default:
  1132. err = sock_do_ioctl(net, sock, cmd, arg);
  1133. break;
  1134. }
  1135. return err;
  1136. }
  1137. /**
  1138. * sock_create_lite - creates a socket
  1139. * @family: protocol family (AF_INET, ...)
  1140. * @type: communication type (SOCK_STREAM, ...)
  1141. * @protocol: protocol (0, ...)
  1142. * @res: new socket
  1143. *
  1144. * Creates a new socket and assigns it to @res, passing through LSM.
  1145. * The new socket initialization is not complete, see kernel_accept().
  1146. * Returns 0 or an error. On failure @res is set to %NULL.
  1147. * This function internally uses GFP_KERNEL.
  1148. */
  1149. int sock_create_lite(int family, int type, int protocol, struct socket **res)
  1150. {
  1151. int err;
  1152. struct socket *sock = NULL;
  1153. err = security_socket_create(family, type, protocol, 1);
  1154. if (err)
  1155. goto out;
  1156. sock = sock_alloc();
  1157. if (!sock) {
  1158. err = -ENOMEM;
  1159. goto out;
  1160. }
  1161. sock->type = type;
  1162. err = security_socket_post_create(sock, family, type, protocol, 1);
  1163. if (err)
  1164. goto out_release;
  1165. out:
  1166. *res = sock;
  1167. return err;
  1168. out_release:
  1169. sock_release(sock);
  1170. sock = NULL;
  1171. goto out;
  1172. }
  1173. EXPORT_SYMBOL(sock_create_lite);
  1174. /* No kernel lock held - perfect */
  1175. static __poll_t sock_poll(struct file *file, poll_table *wait)
  1176. {
  1177. struct socket *sock = file->private_data;
  1178. __poll_t events = poll_requested_events(wait), flag = 0;
  1179. if (!sock->ops->poll)
  1180. return 0;
  1181. if (sk_can_busy_loop(sock->sk)) {
  1182. /* poll once if requested by the syscall */
  1183. if (events & POLL_BUSY_LOOP)
  1184. sk_busy_loop(sock->sk, 1);
  1185. /* if this socket can poll_ll, tell the system call */
  1186. flag = POLL_BUSY_LOOP;
  1187. }
  1188. return sock->ops->poll(file, sock, wait) | flag;
  1189. }
  1190. static int sock_mmap(struct file *file, struct vm_area_struct *vma)
  1191. {
  1192. struct socket *sock = file->private_data;
  1193. return sock->ops->mmap(file, sock, vma);
  1194. }
  1195. static int sock_close(struct inode *inode, struct file *filp)
  1196. {
  1197. __sock_release(SOCKET_I(inode), inode);
  1198. return 0;
  1199. }
  1200. /*
  1201. * Update the socket async list
  1202. *
  1203. * Fasync_list locking strategy.
  1204. *
  1205. * 1. fasync_list is modified only under process context socket lock
  1206. * i.e. under semaphore.
  1207. * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
  1208. * or under socket lock
  1209. */
  1210. static int sock_fasync(int fd, struct file *filp, int on)
  1211. {
  1212. struct socket *sock = filp->private_data;
  1213. struct sock *sk = sock->sk;
  1214. struct socket_wq *wq = &sock->wq;
  1215. if (sk == NULL)
  1216. return -EINVAL;
  1217. lock_sock(sk);
  1218. fasync_helper(fd, filp, on, &wq->fasync_list);
  1219. if (!wq->fasync_list)
  1220. sock_reset_flag(sk, SOCK_FASYNC);
  1221. else
  1222. sock_set_flag(sk, SOCK_FASYNC);
  1223. release_sock(sk);
  1224. return 0;
  1225. }
  1226. /* This function may be called only under rcu_lock */
  1227. int sock_wake_async(struct socket_wq *wq, int how, int band)
  1228. {
  1229. if (!wq || !wq->fasync_list)
  1230. return -1;
  1231. switch (how) {
  1232. case SOCK_WAKE_WAITD:
  1233. if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
  1234. break;
  1235. goto call_kill;
  1236. case SOCK_WAKE_SPACE:
  1237. if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
  1238. break;
  1239. fallthrough;
  1240. case SOCK_WAKE_IO:
  1241. call_kill:
  1242. kill_fasync(&wq->fasync_list, SIGIO, band);
  1243. break;
  1244. case SOCK_WAKE_URG:
  1245. kill_fasync(&wq->fasync_list, SIGURG, band);
  1246. }
  1247. return 0;
  1248. }
  1249. EXPORT_SYMBOL(sock_wake_async);
  1250. /**
  1251. * __sock_create - creates a socket
  1252. * @net: net namespace
  1253. * @family: protocol family (AF_INET, ...)
  1254. * @type: communication type (SOCK_STREAM, ...)
  1255. * @protocol: protocol (0, ...)
  1256. * @res: new socket
  1257. * @kern: boolean for kernel space sockets
  1258. *
  1259. * Creates a new socket and assigns it to @res, passing through LSM.
  1260. * Returns 0 or an error. On failure @res is set to %NULL. @kern must
  1261. * be set to true if the socket resides in kernel space.
  1262. * This function internally uses GFP_KERNEL.
  1263. */
  1264. int __sock_create(struct net *net, int family, int type, int protocol,
  1265. struct socket **res, int kern)
  1266. {
  1267. int err;
  1268. struct socket *sock;
  1269. const struct net_proto_family *pf;
  1270. /*
  1271. * Check protocol is in range
  1272. */
  1273. if (family < 0 || family >= NPROTO)
  1274. return -EAFNOSUPPORT;
  1275. if (type < 0 || type >= SOCK_MAX)
  1276. return -EINVAL;
  1277. /* Compatibility.
  1278. This uglymoron is moved from INET layer to here to avoid
  1279. deadlock in module load.
  1280. */
  1281. if (family == PF_INET && type == SOCK_PACKET) {
  1282. pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
  1283. current->comm);
  1284. family = PF_PACKET;
  1285. }
  1286. err = security_socket_create(family, type, protocol, kern);
  1287. if (err)
  1288. return err;
  1289. /*
  1290. * Allocate the socket and allow the family to set things up. if
  1291. * the protocol is 0, the family is instructed to select an appropriate
  1292. * default.
  1293. */
  1294. sock = sock_alloc();
  1295. if (!sock) {
  1296. net_warn_ratelimited("socket: no more sockets\n");
  1297. return -ENFILE; /* Not exactly a match, but its the
  1298. closest posix thing */
  1299. }
  1300. sock->type = type;
  1301. #ifdef CONFIG_MODULES
  1302. /* Attempt to load a protocol module if the find failed.
  1303. *
  1304. * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
  1305. * requested real, full-featured networking support upon configuration.
  1306. * Otherwise module support will break!
  1307. */
  1308. if (rcu_access_pointer(net_families[family]) == NULL)
  1309. request_module("net-pf-%d", family);
  1310. #endif
  1311. rcu_read_lock();
  1312. pf = rcu_dereference(net_families[family]);
  1313. err = -EAFNOSUPPORT;
  1314. if (!pf)
  1315. goto out_release;
  1316. /*
  1317. * We will call the ->create function, that possibly is in a loadable
  1318. * module, so we have to bump that loadable module refcnt first.
  1319. */
  1320. if (!try_module_get(pf->owner))
  1321. goto out_release;
  1322. /* Now protected by module ref count */
  1323. rcu_read_unlock();
  1324. err = pf->create(net, sock, protocol, kern);
  1325. if (err < 0)
  1326. goto out_module_put;
  1327. /*
  1328. * Now to bump the refcnt of the [loadable] module that owns this
  1329. * socket at sock_release time we decrement its refcnt.
  1330. */
  1331. if (!try_module_get(sock->ops->owner))
  1332. goto out_module_busy;
  1333. /*
  1334. * Now that we're done with the ->create function, the [loadable]
  1335. * module can have its refcnt decremented
  1336. */
  1337. module_put(pf->owner);
  1338. err = security_socket_post_create(sock, family, type, protocol, kern);
  1339. if (err)
  1340. goto out_sock_release;
  1341. *res = sock;
  1342. return 0;
  1343. out_module_busy:
  1344. err = -EAFNOSUPPORT;
  1345. out_module_put:
  1346. sock->ops = NULL;
  1347. module_put(pf->owner);
  1348. out_sock_release:
  1349. sock_release(sock);
  1350. return err;
  1351. out_release:
  1352. rcu_read_unlock();
  1353. goto out_sock_release;
  1354. }
  1355. EXPORT_SYMBOL(__sock_create);
  1356. /**
  1357. * sock_create - creates a socket
  1358. * @family: protocol family (AF_INET, ...)
  1359. * @type: communication type (SOCK_STREAM, ...)
  1360. * @protocol: protocol (0, ...)
  1361. * @res: new socket
  1362. *
  1363. * A wrapper around __sock_create().
  1364. * Returns 0 or an error. This function internally uses GFP_KERNEL.
  1365. */
  1366. int sock_create(int family, int type, int protocol, struct socket **res)
  1367. {
  1368. return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
  1369. }
  1370. EXPORT_SYMBOL(sock_create);
  1371. /**
  1372. * sock_create_kern - creates a socket (kernel space)
  1373. * @net: net namespace
  1374. * @family: protocol family (AF_INET, ...)
  1375. * @type: communication type (SOCK_STREAM, ...)
  1376. * @protocol: protocol (0, ...)
  1377. * @res: new socket
  1378. *
  1379. * A wrapper around __sock_create().
  1380. * Returns 0 or an error. This function internally uses GFP_KERNEL.
  1381. */
  1382. int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
  1383. {
  1384. return __sock_create(net, family, type, protocol, res, 1);
  1385. }
  1386. EXPORT_SYMBOL(sock_create_kern);
  1387. static struct socket *__sys_socket_create(int family, int type, int protocol)
  1388. {
  1389. struct socket *sock;
  1390. int retval;
  1391. /* Check the SOCK_* constants for consistency. */
  1392. BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
  1393. BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
  1394. BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
  1395. BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
  1396. if ((type & ~SOCK_TYPE_MASK) & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1397. return ERR_PTR(-EINVAL);
  1398. type &= SOCK_TYPE_MASK;
  1399. retval = sock_create(family, type, protocol, &sock);
  1400. if (retval < 0)
  1401. return ERR_PTR(retval);
  1402. return sock;
  1403. }
  1404. struct file *__sys_socket_file(int family, int type, int protocol)
  1405. {
  1406. struct socket *sock;
  1407. int flags;
  1408. sock = __sys_socket_create(family, type, protocol);
  1409. if (IS_ERR(sock))
  1410. return ERR_CAST(sock);
  1411. flags = type & ~SOCK_TYPE_MASK;
  1412. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1413. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1414. return sock_alloc_file(sock, flags, NULL);
  1415. }
  1416. int __sys_socket(int family, int type, int protocol)
  1417. {
  1418. struct socket *sock;
  1419. int flags;
  1420. sock = __sys_socket_create(family, type, protocol);
  1421. if (IS_ERR(sock))
  1422. return PTR_ERR(sock);
  1423. flags = type & ~SOCK_TYPE_MASK;
  1424. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1425. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1426. return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
  1427. }
  1428. SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
  1429. {
  1430. return __sys_socket(family, type, protocol);
  1431. }
  1432. /*
  1433. * Create a pair of connected sockets.
  1434. */
  1435. int __sys_socketpair(int family, int type, int protocol, int __user *usockvec)
  1436. {
  1437. struct socket *sock1, *sock2;
  1438. int fd1, fd2, err;
  1439. struct file *newfile1, *newfile2;
  1440. int flags;
  1441. flags = type & ~SOCK_TYPE_MASK;
  1442. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1443. return -EINVAL;
  1444. type &= SOCK_TYPE_MASK;
  1445. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1446. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1447. /*
  1448. * reserve descriptors and make sure we won't fail
  1449. * to return them to userland.
  1450. */
  1451. fd1 = get_unused_fd_flags(flags);
  1452. if (unlikely(fd1 < 0))
  1453. return fd1;
  1454. fd2 = get_unused_fd_flags(flags);
  1455. if (unlikely(fd2 < 0)) {
  1456. put_unused_fd(fd1);
  1457. return fd2;
  1458. }
  1459. err = put_user(fd1, &usockvec[0]);
  1460. if (err)
  1461. goto out;
  1462. err = put_user(fd2, &usockvec[1]);
  1463. if (err)
  1464. goto out;
  1465. /*
  1466. * Obtain the first socket and check if the underlying protocol
  1467. * supports the socketpair call.
  1468. */
  1469. err = sock_create(family, type, protocol, &sock1);
  1470. if (unlikely(err < 0))
  1471. goto out;
  1472. err = sock_create(family, type, protocol, &sock2);
  1473. if (unlikely(err < 0)) {
  1474. sock_release(sock1);
  1475. goto out;
  1476. }
  1477. err = security_socket_socketpair(sock1, sock2);
  1478. if (unlikely(err)) {
  1479. sock_release(sock2);
  1480. sock_release(sock1);
  1481. goto out;
  1482. }
  1483. err = sock1->ops->socketpair(sock1, sock2);
  1484. if (unlikely(err < 0)) {
  1485. sock_release(sock2);
  1486. sock_release(sock1);
  1487. goto out;
  1488. }
  1489. newfile1 = sock_alloc_file(sock1, flags, NULL);
  1490. if (IS_ERR(newfile1)) {
  1491. err = PTR_ERR(newfile1);
  1492. sock_release(sock2);
  1493. goto out;
  1494. }
  1495. newfile2 = sock_alloc_file(sock2, flags, NULL);
  1496. if (IS_ERR(newfile2)) {
  1497. err = PTR_ERR(newfile2);
  1498. fput(newfile1);
  1499. goto out;
  1500. }
  1501. audit_fd_pair(fd1, fd2);
  1502. fd_install(fd1, newfile1);
  1503. fd_install(fd2, newfile2);
  1504. return 0;
  1505. out:
  1506. put_unused_fd(fd2);
  1507. put_unused_fd(fd1);
  1508. return err;
  1509. }
  1510. SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
  1511. int __user *, usockvec)
  1512. {
  1513. return __sys_socketpair(family, type, protocol, usockvec);
  1514. }
  1515. /*
  1516. * Bind a name to a socket. Nothing much to do here since it's
  1517. * the protocol's responsibility to handle the local address.
  1518. *
  1519. * We move the socket address to kernel space before we call
  1520. * the protocol layer (having also checked the address is ok).
  1521. */
  1522. int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
  1523. {
  1524. struct socket *sock;
  1525. struct sockaddr_storage address;
  1526. int err, fput_needed;
  1527. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1528. if (sock) {
  1529. err = move_addr_to_kernel(umyaddr, addrlen, &address);
  1530. if (!err) {
  1531. err = security_socket_bind(sock,
  1532. (struct sockaddr *)&address,
  1533. addrlen);
  1534. if (!err)
  1535. err = sock->ops->bind(sock,
  1536. (struct sockaddr *)
  1537. &address, addrlen);
  1538. }
  1539. fput_light(sock->file, fput_needed);
  1540. }
  1541. return err;
  1542. }
  1543. SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
  1544. {
  1545. return __sys_bind(fd, umyaddr, addrlen);
  1546. }
  1547. /*
  1548. * Perform a listen. Basically, we allow the protocol to do anything
  1549. * necessary for a listen, and if that works, we mark the socket as
  1550. * ready for listening.
  1551. */
  1552. int __sys_listen(int fd, int backlog)
  1553. {
  1554. struct socket *sock;
  1555. int err, fput_needed;
  1556. int somaxconn;
  1557. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1558. if (sock) {
  1559. somaxconn = READ_ONCE(sock_net(sock->sk)->core.sysctl_somaxconn);
  1560. if ((unsigned int)backlog > somaxconn)
  1561. backlog = somaxconn;
  1562. err = security_socket_listen(sock, backlog);
  1563. if (!err)
  1564. err = sock->ops->listen(sock, backlog);
  1565. fput_light(sock->file, fput_needed);
  1566. }
  1567. return err;
  1568. }
  1569. SYSCALL_DEFINE2(listen, int, fd, int, backlog)
  1570. {
  1571. return __sys_listen(fd, backlog);
  1572. }
  1573. struct file *do_accept(struct file *file, unsigned file_flags,
  1574. struct sockaddr __user *upeer_sockaddr,
  1575. int __user *upeer_addrlen, int flags)
  1576. {
  1577. struct socket *sock, *newsock;
  1578. struct file *newfile;
  1579. int err, len;
  1580. struct sockaddr_storage address;
  1581. sock = sock_from_file(file);
  1582. if (!sock)
  1583. return ERR_PTR(-ENOTSOCK);
  1584. newsock = sock_alloc();
  1585. if (!newsock)
  1586. return ERR_PTR(-ENFILE);
  1587. newsock->type = sock->type;
  1588. newsock->ops = sock->ops;
  1589. /*
  1590. * We don't need try_module_get here, as the listening socket (sock)
  1591. * has the protocol module (sock->ops->owner) held.
  1592. */
  1593. __module_get(newsock->ops->owner);
  1594. newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
  1595. if (IS_ERR(newfile))
  1596. return newfile;
  1597. err = security_socket_accept(sock, newsock);
  1598. if (err)
  1599. goto out_fd;
  1600. err = sock->ops->accept(sock, newsock, sock->file->f_flags | file_flags,
  1601. false);
  1602. if (err < 0)
  1603. goto out_fd;
  1604. if (upeer_sockaddr) {
  1605. len = newsock->ops->getname(newsock,
  1606. (struct sockaddr *)&address, 2);
  1607. if (len < 0) {
  1608. err = -ECONNABORTED;
  1609. goto out_fd;
  1610. }
  1611. err = move_addr_to_user(&address,
  1612. len, upeer_sockaddr, upeer_addrlen);
  1613. if (err < 0)
  1614. goto out_fd;
  1615. }
  1616. /* File flags are not inherited via accept() unlike another OSes. */
  1617. return newfile;
  1618. out_fd:
  1619. fput(newfile);
  1620. return ERR_PTR(err);
  1621. }
  1622. static int __sys_accept4_file(struct file *file, struct sockaddr __user *upeer_sockaddr,
  1623. int __user *upeer_addrlen, int flags)
  1624. {
  1625. struct file *newfile;
  1626. int newfd;
  1627. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1628. return -EINVAL;
  1629. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1630. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1631. newfd = get_unused_fd_flags(flags);
  1632. if (unlikely(newfd < 0))
  1633. return newfd;
  1634. newfile = do_accept(file, 0, upeer_sockaddr, upeer_addrlen,
  1635. flags);
  1636. if (IS_ERR(newfile)) {
  1637. put_unused_fd(newfd);
  1638. return PTR_ERR(newfile);
  1639. }
  1640. fd_install(newfd, newfile);
  1641. return newfd;
  1642. }
  1643. /*
  1644. * For accept, we attempt to create a new socket, set up the link
  1645. * with the client, wake up the client, then return the new
  1646. * connected fd. We collect the address of the connector in kernel
  1647. * space and move it to user at the very end. This is unclean because
  1648. * we open the socket then return an error.
  1649. *
  1650. * 1003.1g adds the ability to recvmsg() to query connection pending
  1651. * status to recvmsg. We need to add that support in a way thats
  1652. * clean when we restructure accept also.
  1653. */
  1654. int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
  1655. int __user *upeer_addrlen, int flags)
  1656. {
  1657. int ret = -EBADF;
  1658. struct fd f;
  1659. f = fdget(fd);
  1660. if (f.file) {
  1661. ret = __sys_accept4_file(f.file, upeer_sockaddr,
  1662. upeer_addrlen, flags);
  1663. fdput(f);
  1664. }
  1665. return ret;
  1666. }
  1667. SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1668. int __user *, upeer_addrlen, int, flags)
  1669. {
  1670. return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, flags);
  1671. }
  1672. SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1673. int __user *, upeer_addrlen)
  1674. {
  1675. return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
  1676. }
  1677. /*
  1678. * Attempt to connect to a socket with the server address. The address
  1679. * is in user space so we verify it is OK and move it to kernel space.
  1680. *
  1681. * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
  1682. * break bindings
  1683. *
  1684. * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
  1685. * other SEQPACKET protocols that take time to connect() as it doesn't
  1686. * include the -EINPROGRESS status for such sockets.
  1687. */
  1688. int __sys_connect_file(struct file *file, struct sockaddr_storage *address,
  1689. int addrlen, int file_flags)
  1690. {
  1691. struct socket *sock;
  1692. int err;
  1693. sock = sock_from_file(file);
  1694. if (!sock) {
  1695. err = -ENOTSOCK;
  1696. goto out;
  1697. }
  1698. err =
  1699. security_socket_connect(sock, (struct sockaddr *)address, addrlen);
  1700. if (err)
  1701. goto out;
  1702. err = sock->ops->connect(sock, (struct sockaddr *)address, addrlen,
  1703. sock->file->f_flags | file_flags);
  1704. out:
  1705. return err;
  1706. }
  1707. int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
  1708. {
  1709. int ret = -EBADF;
  1710. struct fd f;
  1711. f = fdget(fd);
  1712. if (f.file) {
  1713. struct sockaddr_storage address;
  1714. ret = move_addr_to_kernel(uservaddr, addrlen, &address);
  1715. if (!ret)
  1716. ret = __sys_connect_file(f.file, &address, addrlen, 0);
  1717. fdput(f);
  1718. }
  1719. return ret;
  1720. }
  1721. SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
  1722. int, addrlen)
  1723. {
  1724. return __sys_connect(fd, uservaddr, addrlen);
  1725. }
  1726. /*
  1727. * Get the local address ('name') of a socket object. Move the obtained
  1728. * name to user space.
  1729. */
  1730. int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
  1731. int __user *usockaddr_len)
  1732. {
  1733. struct socket *sock;
  1734. struct sockaddr_storage address;
  1735. int err, fput_needed;
  1736. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1737. if (!sock)
  1738. goto out;
  1739. err = security_socket_getsockname(sock);
  1740. if (err)
  1741. goto out_put;
  1742. err = sock->ops->getname(sock, (struct sockaddr *)&address, 0);
  1743. if (err < 0)
  1744. goto out_put;
  1745. /* "err" is actually length in this case */
  1746. err = move_addr_to_user(&address, err, usockaddr, usockaddr_len);
  1747. out_put:
  1748. fput_light(sock->file, fput_needed);
  1749. out:
  1750. return err;
  1751. }
  1752. SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
  1753. int __user *, usockaddr_len)
  1754. {
  1755. return __sys_getsockname(fd, usockaddr, usockaddr_len);
  1756. }
  1757. /*
  1758. * Get the remote address ('name') of a socket object. Move the obtained
  1759. * name to user space.
  1760. */
  1761. int __sys_getpeername(int fd, struct sockaddr __user *usockaddr,
  1762. int __user *usockaddr_len)
  1763. {
  1764. struct socket *sock;
  1765. struct sockaddr_storage address;
  1766. int err, fput_needed;
  1767. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1768. if (sock != NULL) {
  1769. err = security_socket_getpeername(sock);
  1770. if (err) {
  1771. fput_light(sock->file, fput_needed);
  1772. return err;
  1773. }
  1774. err = sock->ops->getname(sock, (struct sockaddr *)&address, 1);
  1775. if (err >= 0)
  1776. /* "err" is actually length in this case */
  1777. err = move_addr_to_user(&address, err, usockaddr,
  1778. usockaddr_len);
  1779. fput_light(sock->file, fput_needed);
  1780. }
  1781. return err;
  1782. }
  1783. SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
  1784. int __user *, usockaddr_len)
  1785. {
  1786. return __sys_getpeername(fd, usockaddr, usockaddr_len);
  1787. }
  1788. /*
  1789. * Send a datagram to a given address. We move the address into kernel
  1790. * space and check the user space data area is readable before invoking
  1791. * the protocol.
  1792. */
  1793. int __sys_sendto(int fd, void __user *buff, size_t len, unsigned int flags,
  1794. struct sockaddr __user *addr, int addr_len)
  1795. {
  1796. struct socket *sock;
  1797. struct sockaddr_storage address;
  1798. int err;
  1799. struct msghdr msg;
  1800. struct iovec iov;
  1801. int fput_needed;
  1802. err = import_single_range(ITER_SOURCE, buff, len, &iov, &msg.msg_iter);
  1803. if (unlikely(err))
  1804. return err;
  1805. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1806. if (!sock)
  1807. goto out;
  1808. msg.msg_name = NULL;
  1809. msg.msg_control = NULL;
  1810. msg.msg_controllen = 0;
  1811. msg.msg_namelen = 0;
  1812. msg.msg_ubuf = NULL;
  1813. if (addr) {
  1814. err = move_addr_to_kernel(addr, addr_len, &address);
  1815. if (err < 0)
  1816. goto out_put;
  1817. msg.msg_name = (struct sockaddr *)&address;
  1818. msg.msg_namelen = addr_len;
  1819. }
  1820. if (sock->file->f_flags & O_NONBLOCK)
  1821. flags |= MSG_DONTWAIT;
  1822. msg.msg_flags = flags;
  1823. err = __sock_sendmsg(sock, &msg);
  1824. out_put:
  1825. fput_light(sock->file, fput_needed);
  1826. out:
  1827. return err;
  1828. }
  1829. SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
  1830. unsigned int, flags, struct sockaddr __user *, addr,
  1831. int, addr_len)
  1832. {
  1833. return __sys_sendto(fd, buff, len, flags, addr, addr_len);
  1834. }
  1835. /*
  1836. * Send a datagram down a socket.
  1837. */
  1838. SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
  1839. unsigned int, flags)
  1840. {
  1841. return __sys_sendto(fd, buff, len, flags, NULL, 0);
  1842. }
  1843. /*
  1844. * Receive a frame from the socket and optionally record the address of the
  1845. * sender. We verify the buffers are writable and if needed move the
  1846. * sender address from kernel to user space.
  1847. */
  1848. int __sys_recvfrom(int fd, void __user *ubuf, size_t size, unsigned int flags,
  1849. struct sockaddr __user *addr, int __user *addr_len)
  1850. {
  1851. struct sockaddr_storage address;
  1852. struct msghdr msg = {
  1853. /* Save some cycles and don't copy the address if not needed */
  1854. .msg_name = addr ? (struct sockaddr *)&address : NULL,
  1855. };
  1856. struct socket *sock;
  1857. struct iovec iov;
  1858. int err, err2;
  1859. int fput_needed;
  1860. err = import_single_range(ITER_DEST, ubuf, size, &iov, &msg.msg_iter);
  1861. if (unlikely(err))
  1862. return err;
  1863. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1864. if (!sock)
  1865. goto out;
  1866. if (sock->file->f_flags & O_NONBLOCK)
  1867. flags |= MSG_DONTWAIT;
  1868. err = sock_recvmsg(sock, &msg, flags);
  1869. if (err >= 0 && addr != NULL) {
  1870. err2 = move_addr_to_user(&address,
  1871. msg.msg_namelen, addr, addr_len);
  1872. if (err2 < 0)
  1873. err = err2;
  1874. }
  1875. fput_light(sock->file, fput_needed);
  1876. out:
  1877. return err;
  1878. }
  1879. SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
  1880. unsigned int, flags, struct sockaddr __user *, addr,
  1881. int __user *, addr_len)
  1882. {
  1883. return __sys_recvfrom(fd, ubuf, size, flags, addr, addr_len);
  1884. }
  1885. /*
  1886. * Receive a datagram from a socket.
  1887. */
  1888. SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
  1889. unsigned int, flags)
  1890. {
  1891. return __sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
  1892. }
  1893. static bool sock_use_custom_sol_socket(const struct socket *sock)
  1894. {
  1895. const struct sock *sk = sock->sk;
  1896. /* Use sock->ops->setsockopt() for MPTCP */
  1897. return IS_ENABLED(CONFIG_MPTCP) &&
  1898. sk->sk_protocol == IPPROTO_MPTCP &&
  1899. sk->sk_type == SOCK_STREAM &&
  1900. (sk->sk_family == AF_INET || sk->sk_family == AF_INET6);
  1901. }
  1902. /*
  1903. * Set a socket option. Because we don't know the option lengths we have
  1904. * to pass the user mode parameter for the protocols to sort out.
  1905. */
  1906. int __sys_setsockopt(int fd, int level, int optname, char __user *user_optval,
  1907. int optlen)
  1908. {
  1909. sockptr_t optval = USER_SOCKPTR(user_optval);
  1910. char *kernel_optval = NULL;
  1911. int err, fput_needed;
  1912. struct socket *sock;
  1913. if (optlen < 0)
  1914. return -EINVAL;
  1915. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1916. if (!sock)
  1917. return err;
  1918. err = security_socket_setsockopt(sock, level, optname);
  1919. if (err)
  1920. goto out_put;
  1921. if (!in_compat_syscall())
  1922. err = BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock->sk, &level, &optname,
  1923. user_optval, &optlen,
  1924. &kernel_optval);
  1925. if (err < 0)
  1926. goto out_put;
  1927. if (err > 0) {
  1928. err = 0;
  1929. goto out_put;
  1930. }
  1931. if (kernel_optval)
  1932. optval = KERNEL_SOCKPTR(kernel_optval);
  1933. if (level == SOL_SOCKET && !sock_use_custom_sol_socket(sock))
  1934. err = sock_setsockopt(sock, level, optname, optval, optlen);
  1935. else if (unlikely(!sock->ops->setsockopt))
  1936. err = -EOPNOTSUPP;
  1937. else
  1938. err = sock->ops->setsockopt(sock, level, optname, optval,
  1939. optlen);
  1940. kfree(kernel_optval);
  1941. out_put:
  1942. fput_light(sock->file, fput_needed);
  1943. return err;
  1944. }
  1945. SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
  1946. char __user *, optval, int, optlen)
  1947. {
  1948. return __sys_setsockopt(fd, level, optname, optval, optlen);
  1949. }
  1950. INDIRECT_CALLABLE_DECLARE(bool tcp_bpf_bypass_getsockopt(int level,
  1951. int optname));
  1952. /*
  1953. * Get a socket option. Because we don't know the option lengths we have
  1954. * to pass a user mode parameter for the protocols to sort out.
  1955. */
  1956. int __sys_getsockopt(int fd, int level, int optname, char __user *optval,
  1957. int __user *optlen)
  1958. {
  1959. int err, fput_needed;
  1960. struct socket *sock;
  1961. int max_optlen;
  1962. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1963. if (!sock)
  1964. return err;
  1965. err = security_socket_getsockopt(sock, level, optname);
  1966. if (err)
  1967. goto out_put;
  1968. if (!in_compat_syscall())
  1969. max_optlen = BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen);
  1970. if (level == SOL_SOCKET)
  1971. err = sock_getsockopt(sock, level, optname, optval, optlen);
  1972. else if (unlikely(!sock->ops->getsockopt))
  1973. err = -EOPNOTSUPP;
  1974. else
  1975. err = sock->ops->getsockopt(sock, level, optname, optval,
  1976. optlen);
  1977. if (!in_compat_syscall())
  1978. err = BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock->sk, level, optname,
  1979. optval, optlen, max_optlen,
  1980. err);
  1981. out_put:
  1982. fput_light(sock->file, fput_needed);
  1983. return err;
  1984. }
  1985. SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
  1986. char __user *, optval, int __user *, optlen)
  1987. {
  1988. return __sys_getsockopt(fd, level, optname, optval, optlen);
  1989. }
  1990. /*
  1991. * Shutdown a socket.
  1992. */
  1993. int __sys_shutdown_sock(struct socket *sock, int how)
  1994. {
  1995. int err;
  1996. err = security_socket_shutdown(sock, how);
  1997. if (!err)
  1998. err = sock->ops->shutdown(sock, how);
  1999. return err;
  2000. }
  2001. int __sys_shutdown(int fd, int how)
  2002. {
  2003. int err, fput_needed;
  2004. struct socket *sock;
  2005. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  2006. if (sock != NULL) {
  2007. err = __sys_shutdown_sock(sock, how);
  2008. fput_light(sock->file, fput_needed);
  2009. }
  2010. return err;
  2011. }
  2012. SYSCALL_DEFINE2(shutdown, int, fd, int, how)
  2013. {
  2014. return __sys_shutdown(fd, how);
  2015. }
  2016. /* A couple of helpful macros for getting the address of the 32/64 bit
  2017. * fields which are the same type (int / unsigned) on our platforms.
  2018. */
  2019. #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
  2020. #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
  2021. #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
  2022. struct used_address {
  2023. struct sockaddr_storage name;
  2024. unsigned int name_len;
  2025. };
  2026. int __copy_msghdr(struct msghdr *kmsg,
  2027. struct user_msghdr *msg,
  2028. struct sockaddr __user **save_addr)
  2029. {
  2030. ssize_t err;
  2031. kmsg->msg_control_is_user = true;
  2032. kmsg->msg_get_inq = 0;
  2033. kmsg->msg_control_user = msg->msg_control;
  2034. kmsg->msg_controllen = msg->msg_controllen;
  2035. kmsg->msg_flags = msg->msg_flags;
  2036. kmsg->msg_namelen = msg->msg_namelen;
  2037. if (!msg->msg_name)
  2038. kmsg->msg_namelen = 0;
  2039. if (kmsg->msg_namelen < 0)
  2040. return -EINVAL;
  2041. if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
  2042. kmsg->msg_namelen = sizeof(struct sockaddr_storage);
  2043. if (save_addr)
  2044. *save_addr = msg->msg_name;
  2045. if (msg->msg_name && kmsg->msg_namelen) {
  2046. if (!save_addr) {
  2047. err = move_addr_to_kernel(msg->msg_name,
  2048. kmsg->msg_namelen,
  2049. kmsg->msg_name);
  2050. if (err < 0)
  2051. return err;
  2052. }
  2053. } else {
  2054. kmsg->msg_name = NULL;
  2055. kmsg->msg_namelen = 0;
  2056. }
  2057. if (msg->msg_iovlen > UIO_MAXIOV)
  2058. return -EMSGSIZE;
  2059. kmsg->msg_iocb = NULL;
  2060. kmsg->msg_ubuf = NULL;
  2061. return 0;
  2062. }
  2063. static int copy_msghdr_from_user(struct msghdr *kmsg,
  2064. struct user_msghdr __user *umsg,
  2065. struct sockaddr __user **save_addr,
  2066. struct iovec **iov)
  2067. {
  2068. struct user_msghdr msg;
  2069. ssize_t err;
  2070. if (copy_from_user(&msg, umsg, sizeof(*umsg)))
  2071. return -EFAULT;
  2072. err = __copy_msghdr(kmsg, &msg, save_addr);
  2073. if (err)
  2074. return err;
  2075. err = import_iovec(save_addr ? ITER_DEST : ITER_SOURCE,
  2076. msg.msg_iov, msg.msg_iovlen,
  2077. UIO_FASTIOV, iov, &kmsg->msg_iter);
  2078. return err < 0 ? err : 0;
  2079. }
  2080. static int ____sys_sendmsg(struct socket *sock, struct msghdr *msg_sys,
  2081. unsigned int flags, struct used_address *used_address,
  2082. unsigned int allowed_msghdr_flags)
  2083. {
  2084. unsigned char ctl[sizeof(struct cmsghdr) + 20]
  2085. __aligned(sizeof(__kernel_size_t));
  2086. /* 20 is size of ipv6_pktinfo */
  2087. unsigned char *ctl_buf = ctl;
  2088. int ctl_len;
  2089. ssize_t err;
  2090. err = -ENOBUFS;
  2091. if (msg_sys->msg_controllen > INT_MAX)
  2092. goto out;
  2093. flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
  2094. ctl_len = msg_sys->msg_controllen;
  2095. if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
  2096. err =
  2097. cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
  2098. sizeof(ctl));
  2099. if (err)
  2100. goto out;
  2101. ctl_buf = msg_sys->msg_control;
  2102. ctl_len = msg_sys->msg_controllen;
  2103. } else if (ctl_len) {
  2104. BUILD_BUG_ON(sizeof(struct cmsghdr) !=
  2105. CMSG_ALIGN(sizeof(struct cmsghdr)));
  2106. if (ctl_len > sizeof(ctl)) {
  2107. ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
  2108. if (ctl_buf == NULL)
  2109. goto out;
  2110. }
  2111. err = -EFAULT;
  2112. if (copy_from_user(ctl_buf, msg_sys->msg_control_user, ctl_len))
  2113. goto out_freectl;
  2114. msg_sys->msg_control = ctl_buf;
  2115. msg_sys->msg_control_is_user = false;
  2116. }
  2117. msg_sys->msg_flags = flags;
  2118. if (sock->file->f_flags & O_NONBLOCK)
  2119. msg_sys->msg_flags |= MSG_DONTWAIT;
  2120. /*
  2121. * If this is sendmmsg() and current destination address is same as
  2122. * previously succeeded address, omit asking LSM's decision.
  2123. * used_address->name_len is initialized to UINT_MAX so that the first
  2124. * destination address never matches.
  2125. */
  2126. if (used_address && msg_sys->msg_name &&
  2127. used_address->name_len == msg_sys->msg_namelen &&
  2128. !memcmp(&used_address->name, msg_sys->msg_name,
  2129. used_address->name_len)) {
  2130. err = sock_sendmsg_nosec(sock, msg_sys);
  2131. goto out_freectl;
  2132. }
  2133. err = __sock_sendmsg(sock, msg_sys);
  2134. /*
  2135. * If this is sendmmsg() and sending to current destination address was
  2136. * successful, remember it.
  2137. */
  2138. if (used_address && err >= 0) {
  2139. used_address->name_len = msg_sys->msg_namelen;
  2140. if (msg_sys->msg_name)
  2141. memcpy(&used_address->name, msg_sys->msg_name,
  2142. used_address->name_len);
  2143. }
  2144. out_freectl:
  2145. if (ctl_buf != ctl)
  2146. sock_kfree_s(sock->sk, ctl_buf, ctl_len);
  2147. out:
  2148. return err;
  2149. }
  2150. int sendmsg_copy_msghdr(struct msghdr *msg,
  2151. struct user_msghdr __user *umsg, unsigned flags,
  2152. struct iovec **iov)
  2153. {
  2154. int err;
  2155. if (flags & MSG_CMSG_COMPAT) {
  2156. struct compat_msghdr __user *msg_compat;
  2157. msg_compat = (struct compat_msghdr __user *) umsg;
  2158. err = get_compat_msghdr(msg, msg_compat, NULL, iov);
  2159. } else {
  2160. err = copy_msghdr_from_user(msg, umsg, NULL, iov);
  2161. }
  2162. if (err < 0)
  2163. return err;
  2164. return 0;
  2165. }
  2166. static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
  2167. struct msghdr *msg_sys, unsigned int flags,
  2168. struct used_address *used_address,
  2169. unsigned int allowed_msghdr_flags)
  2170. {
  2171. struct sockaddr_storage address;
  2172. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  2173. ssize_t err;
  2174. msg_sys->msg_name = &address;
  2175. err = sendmsg_copy_msghdr(msg_sys, msg, flags, &iov);
  2176. if (err < 0)
  2177. return err;
  2178. err = ____sys_sendmsg(sock, msg_sys, flags, used_address,
  2179. allowed_msghdr_flags);
  2180. kfree(iov);
  2181. return err;
  2182. }
  2183. /*
  2184. * BSD sendmsg interface
  2185. */
  2186. long __sys_sendmsg_sock(struct socket *sock, struct msghdr *msg,
  2187. unsigned int flags)
  2188. {
  2189. return ____sys_sendmsg(sock, msg, flags, NULL, 0);
  2190. }
  2191. long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
  2192. bool forbid_cmsg_compat)
  2193. {
  2194. int fput_needed, err;
  2195. struct msghdr msg_sys;
  2196. struct socket *sock;
  2197. if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
  2198. return -EINVAL;
  2199. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  2200. if (!sock)
  2201. goto out;
  2202. err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
  2203. fput_light(sock->file, fput_needed);
  2204. out:
  2205. return err;
  2206. }
  2207. SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
  2208. {
  2209. return __sys_sendmsg(fd, msg, flags, true);
  2210. }
  2211. /*
  2212. * Linux sendmmsg interface
  2213. */
  2214. int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
  2215. unsigned int flags, bool forbid_cmsg_compat)
  2216. {
  2217. int fput_needed, err, datagrams;
  2218. struct socket *sock;
  2219. struct mmsghdr __user *entry;
  2220. struct compat_mmsghdr __user *compat_entry;
  2221. struct msghdr msg_sys;
  2222. struct used_address used_address;
  2223. unsigned int oflags = flags;
  2224. if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
  2225. return -EINVAL;
  2226. if (vlen > UIO_MAXIOV)
  2227. vlen = UIO_MAXIOV;
  2228. datagrams = 0;
  2229. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  2230. if (!sock)
  2231. return err;
  2232. used_address.name_len = UINT_MAX;
  2233. entry = mmsg;
  2234. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  2235. err = 0;
  2236. flags |= MSG_BATCH;
  2237. while (datagrams < vlen) {
  2238. if (datagrams == vlen - 1)
  2239. flags = oflags;
  2240. if (MSG_CMSG_COMPAT & flags) {
  2241. err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
  2242. &msg_sys, flags, &used_address, MSG_EOR);
  2243. if (err < 0)
  2244. break;
  2245. err = __put_user(err, &compat_entry->msg_len);
  2246. ++compat_entry;
  2247. } else {
  2248. err = ___sys_sendmsg(sock,
  2249. (struct user_msghdr __user *)entry,
  2250. &msg_sys, flags, &used_address, MSG_EOR);
  2251. if (err < 0)
  2252. break;
  2253. err = put_user(err, &entry->msg_len);
  2254. ++entry;
  2255. }
  2256. if (err)
  2257. break;
  2258. ++datagrams;
  2259. if (msg_data_left(&msg_sys))
  2260. break;
  2261. cond_resched();
  2262. }
  2263. fput_light(sock->file, fput_needed);
  2264. /* We only return an error if no datagrams were able to be sent */
  2265. if (datagrams != 0)
  2266. return datagrams;
  2267. return err;
  2268. }
  2269. SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
  2270. unsigned int, vlen, unsigned int, flags)
  2271. {
  2272. return __sys_sendmmsg(fd, mmsg, vlen, flags, true);
  2273. }
  2274. int recvmsg_copy_msghdr(struct msghdr *msg,
  2275. struct user_msghdr __user *umsg, unsigned flags,
  2276. struct sockaddr __user **uaddr,
  2277. struct iovec **iov)
  2278. {
  2279. ssize_t err;
  2280. if (MSG_CMSG_COMPAT & flags) {
  2281. struct compat_msghdr __user *msg_compat;
  2282. msg_compat = (struct compat_msghdr __user *) umsg;
  2283. err = get_compat_msghdr(msg, msg_compat, uaddr, iov);
  2284. } else {
  2285. err = copy_msghdr_from_user(msg, umsg, uaddr, iov);
  2286. }
  2287. if (err < 0)
  2288. return err;
  2289. return 0;
  2290. }
  2291. static int ____sys_recvmsg(struct socket *sock, struct msghdr *msg_sys,
  2292. struct user_msghdr __user *msg,
  2293. struct sockaddr __user *uaddr,
  2294. unsigned int flags, int nosec)
  2295. {
  2296. struct compat_msghdr __user *msg_compat =
  2297. (struct compat_msghdr __user *) msg;
  2298. int __user *uaddr_len = COMPAT_NAMELEN(msg);
  2299. struct sockaddr_storage addr;
  2300. unsigned long cmsg_ptr;
  2301. int len;
  2302. ssize_t err;
  2303. msg_sys->msg_name = &addr;
  2304. cmsg_ptr = (unsigned long)msg_sys->msg_control;
  2305. msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
  2306. /* We assume all kernel code knows the size of sockaddr_storage */
  2307. msg_sys->msg_namelen = 0;
  2308. if (sock->file->f_flags & O_NONBLOCK)
  2309. flags |= MSG_DONTWAIT;
  2310. if (unlikely(nosec))
  2311. err = sock_recvmsg_nosec(sock, msg_sys, flags);
  2312. else
  2313. err = sock_recvmsg(sock, msg_sys, flags);
  2314. if (err < 0)
  2315. goto out;
  2316. len = err;
  2317. if (uaddr != NULL) {
  2318. err = move_addr_to_user(&addr,
  2319. msg_sys->msg_namelen, uaddr,
  2320. uaddr_len);
  2321. if (err < 0)
  2322. goto out;
  2323. }
  2324. err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
  2325. COMPAT_FLAGS(msg));
  2326. if (err)
  2327. goto out;
  2328. if (MSG_CMSG_COMPAT & flags)
  2329. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  2330. &msg_compat->msg_controllen);
  2331. else
  2332. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  2333. &msg->msg_controllen);
  2334. if (err)
  2335. goto out;
  2336. err = len;
  2337. out:
  2338. return err;
  2339. }
  2340. static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
  2341. struct msghdr *msg_sys, unsigned int flags, int nosec)
  2342. {
  2343. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  2344. /* user mode address pointers */
  2345. struct sockaddr __user *uaddr;
  2346. ssize_t err;
  2347. err = recvmsg_copy_msghdr(msg_sys, msg, flags, &uaddr, &iov);
  2348. if (err < 0)
  2349. return err;
  2350. err = ____sys_recvmsg(sock, msg_sys, msg, uaddr, flags, nosec);
  2351. kfree(iov);
  2352. return err;
  2353. }
  2354. /*
  2355. * BSD recvmsg interface
  2356. */
  2357. long __sys_recvmsg_sock(struct socket *sock, struct msghdr *msg,
  2358. struct user_msghdr __user *umsg,
  2359. struct sockaddr __user *uaddr, unsigned int flags)
  2360. {
  2361. return ____sys_recvmsg(sock, msg, umsg, uaddr, flags, 0);
  2362. }
  2363. long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
  2364. bool forbid_cmsg_compat)
  2365. {
  2366. int fput_needed, err;
  2367. struct msghdr msg_sys;
  2368. struct socket *sock;
  2369. if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
  2370. return -EINVAL;
  2371. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  2372. if (!sock)
  2373. goto out;
  2374. err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
  2375. fput_light(sock->file, fput_needed);
  2376. out:
  2377. return err;
  2378. }
  2379. SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
  2380. unsigned int, flags)
  2381. {
  2382. return __sys_recvmsg(fd, msg, flags, true);
  2383. }
  2384. /*
  2385. * Linux recvmmsg interface
  2386. */
  2387. static int do_recvmmsg(int fd, struct mmsghdr __user *mmsg,
  2388. unsigned int vlen, unsigned int flags,
  2389. struct timespec64 *timeout)
  2390. {
  2391. int fput_needed, err, datagrams;
  2392. struct socket *sock;
  2393. struct mmsghdr __user *entry;
  2394. struct compat_mmsghdr __user *compat_entry;
  2395. struct msghdr msg_sys;
  2396. struct timespec64 end_time;
  2397. struct timespec64 timeout64;
  2398. if (timeout &&
  2399. poll_select_set_timeout(&end_time, timeout->tv_sec,
  2400. timeout->tv_nsec))
  2401. return -EINVAL;
  2402. datagrams = 0;
  2403. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  2404. if (!sock)
  2405. return err;
  2406. if (likely(!(flags & MSG_ERRQUEUE))) {
  2407. err = sock_error(sock->sk);
  2408. if (err) {
  2409. datagrams = err;
  2410. goto out_put;
  2411. }
  2412. }
  2413. entry = mmsg;
  2414. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  2415. while (datagrams < vlen) {
  2416. /*
  2417. * No need to ask LSM for more than the first datagram.
  2418. */
  2419. if (MSG_CMSG_COMPAT & flags) {
  2420. err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
  2421. &msg_sys, flags & ~MSG_WAITFORONE,
  2422. datagrams);
  2423. if (err < 0)
  2424. break;
  2425. err = __put_user(err, &compat_entry->msg_len);
  2426. ++compat_entry;
  2427. } else {
  2428. err = ___sys_recvmsg(sock,
  2429. (struct user_msghdr __user *)entry,
  2430. &msg_sys, flags & ~MSG_WAITFORONE,
  2431. datagrams);
  2432. if (err < 0)
  2433. break;
  2434. err = put_user(err, &entry->msg_len);
  2435. ++entry;
  2436. }
  2437. if (err)
  2438. break;
  2439. ++datagrams;
  2440. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  2441. if (flags & MSG_WAITFORONE)
  2442. flags |= MSG_DONTWAIT;
  2443. if (timeout) {
  2444. ktime_get_ts64(&timeout64);
  2445. *timeout = timespec64_sub(end_time, timeout64);
  2446. if (timeout->tv_sec < 0) {
  2447. timeout->tv_sec = timeout->tv_nsec = 0;
  2448. break;
  2449. }
  2450. /* Timeout, return less than vlen datagrams */
  2451. if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
  2452. break;
  2453. }
  2454. /* Out of band data, return right away */
  2455. if (msg_sys.msg_flags & MSG_OOB)
  2456. break;
  2457. cond_resched();
  2458. }
  2459. if (err == 0)
  2460. goto out_put;
  2461. if (datagrams == 0) {
  2462. datagrams = err;
  2463. goto out_put;
  2464. }
  2465. /*
  2466. * We may return less entries than requested (vlen) if the
  2467. * sock is non block and there aren't enough datagrams...
  2468. */
  2469. if (err != -EAGAIN) {
  2470. /*
  2471. * ... or if recvmsg returns an error after we
  2472. * received some datagrams, where we record the
  2473. * error to return on the next call or if the
  2474. * app asks about it using getsockopt(SO_ERROR).
  2475. */
  2476. WRITE_ONCE(sock->sk->sk_err, -err);
  2477. }
  2478. out_put:
  2479. fput_light(sock->file, fput_needed);
  2480. return datagrams;
  2481. }
  2482. int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg,
  2483. unsigned int vlen, unsigned int flags,
  2484. struct __kernel_timespec __user *timeout,
  2485. struct old_timespec32 __user *timeout32)
  2486. {
  2487. int datagrams;
  2488. struct timespec64 timeout_sys;
  2489. if (timeout && get_timespec64(&timeout_sys, timeout))
  2490. return -EFAULT;
  2491. if (timeout32 && get_old_timespec32(&timeout_sys, timeout32))
  2492. return -EFAULT;
  2493. if (!timeout && !timeout32)
  2494. return do_recvmmsg(fd, mmsg, vlen, flags, NULL);
  2495. datagrams = do_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
  2496. if (datagrams <= 0)
  2497. return datagrams;
  2498. if (timeout && put_timespec64(&timeout_sys, timeout))
  2499. datagrams = -EFAULT;
  2500. if (timeout32 && put_old_timespec32(&timeout_sys, timeout32))
  2501. datagrams = -EFAULT;
  2502. return datagrams;
  2503. }
  2504. SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
  2505. unsigned int, vlen, unsigned int, flags,
  2506. struct __kernel_timespec __user *, timeout)
  2507. {
  2508. if (flags & MSG_CMSG_COMPAT)
  2509. return -EINVAL;
  2510. return __sys_recvmmsg(fd, mmsg, vlen, flags, timeout, NULL);
  2511. }
  2512. #ifdef CONFIG_COMPAT_32BIT_TIME
  2513. SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct mmsghdr __user *, mmsg,
  2514. unsigned int, vlen, unsigned int, flags,
  2515. struct old_timespec32 __user *, timeout)
  2516. {
  2517. if (flags & MSG_CMSG_COMPAT)
  2518. return -EINVAL;
  2519. return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL, timeout);
  2520. }
  2521. #endif
  2522. #ifdef __ARCH_WANT_SYS_SOCKETCALL
  2523. /* Argument list sizes for sys_socketcall */
  2524. #define AL(x) ((x) * sizeof(unsigned long))
  2525. static const unsigned char nargs[21] = {
  2526. AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
  2527. AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
  2528. AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
  2529. AL(4), AL(5), AL(4)
  2530. };
  2531. #undef AL
  2532. /*
  2533. * System call vectors.
  2534. *
  2535. * Argument checking cleaned up. Saved 20% in size.
  2536. * This function doesn't need to set the kernel lock because
  2537. * it is set by the callees.
  2538. */
  2539. SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
  2540. {
  2541. unsigned long a[AUDITSC_ARGS];
  2542. unsigned long a0, a1;
  2543. int err;
  2544. unsigned int len;
  2545. if (call < 1 || call > SYS_SENDMMSG)
  2546. return -EINVAL;
  2547. call = array_index_nospec(call, SYS_SENDMMSG + 1);
  2548. len = nargs[call];
  2549. if (len > sizeof(a))
  2550. return -EINVAL;
  2551. /* copy_from_user should be SMP safe. */
  2552. if (copy_from_user(a, args, len))
  2553. return -EFAULT;
  2554. err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
  2555. if (err)
  2556. return err;
  2557. a0 = a[0];
  2558. a1 = a[1];
  2559. switch (call) {
  2560. case SYS_SOCKET:
  2561. err = __sys_socket(a0, a1, a[2]);
  2562. break;
  2563. case SYS_BIND:
  2564. err = __sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
  2565. break;
  2566. case SYS_CONNECT:
  2567. err = __sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
  2568. break;
  2569. case SYS_LISTEN:
  2570. err = __sys_listen(a0, a1);
  2571. break;
  2572. case SYS_ACCEPT:
  2573. err = __sys_accept4(a0, (struct sockaddr __user *)a1,
  2574. (int __user *)a[2], 0);
  2575. break;
  2576. case SYS_GETSOCKNAME:
  2577. err =
  2578. __sys_getsockname(a0, (struct sockaddr __user *)a1,
  2579. (int __user *)a[2]);
  2580. break;
  2581. case SYS_GETPEERNAME:
  2582. err =
  2583. __sys_getpeername(a0, (struct sockaddr __user *)a1,
  2584. (int __user *)a[2]);
  2585. break;
  2586. case SYS_SOCKETPAIR:
  2587. err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
  2588. break;
  2589. case SYS_SEND:
  2590. err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
  2591. NULL, 0);
  2592. break;
  2593. case SYS_SENDTO:
  2594. err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
  2595. (struct sockaddr __user *)a[4], a[5]);
  2596. break;
  2597. case SYS_RECV:
  2598. err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  2599. NULL, NULL);
  2600. break;
  2601. case SYS_RECVFROM:
  2602. err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  2603. (struct sockaddr __user *)a[4],
  2604. (int __user *)a[5]);
  2605. break;
  2606. case SYS_SHUTDOWN:
  2607. err = __sys_shutdown(a0, a1);
  2608. break;
  2609. case SYS_SETSOCKOPT:
  2610. err = __sys_setsockopt(a0, a1, a[2], (char __user *)a[3],
  2611. a[4]);
  2612. break;
  2613. case SYS_GETSOCKOPT:
  2614. err =
  2615. __sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
  2616. (int __user *)a[4]);
  2617. break;
  2618. case SYS_SENDMSG:
  2619. err = __sys_sendmsg(a0, (struct user_msghdr __user *)a1,
  2620. a[2], true);
  2621. break;
  2622. case SYS_SENDMMSG:
  2623. err = __sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2],
  2624. a[3], true);
  2625. break;
  2626. case SYS_RECVMSG:
  2627. err = __sys_recvmsg(a0, (struct user_msghdr __user *)a1,
  2628. a[2], true);
  2629. break;
  2630. case SYS_RECVMMSG:
  2631. if (IS_ENABLED(CONFIG_64BIT))
  2632. err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
  2633. a[2], a[3],
  2634. (struct __kernel_timespec __user *)a[4],
  2635. NULL);
  2636. else
  2637. err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
  2638. a[2], a[3], NULL,
  2639. (struct old_timespec32 __user *)a[4]);
  2640. break;
  2641. case SYS_ACCEPT4:
  2642. err = __sys_accept4(a0, (struct sockaddr __user *)a1,
  2643. (int __user *)a[2], a[3]);
  2644. break;
  2645. default:
  2646. err = -EINVAL;
  2647. break;
  2648. }
  2649. return err;
  2650. }
  2651. #endif /* __ARCH_WANT_SYS_SOCKETCALL */
  2652. /**
  2653. * sock_register - add a socket protocol handler
  2654. * @ops: description of protocol
  2655. *
  2656. * This function is called by a protocol handler that wants to
  2657. * advertise its address family, and have it linked into the
  2658. * socket interface. The value ops->family corresponds to the
  2659. * socket system call protocol family.
  2660. */
  2661. int sock_register(const struct net_proto_family *ops)
  2662. {
  2663. int err;
  2664. if (ops->family >= NPROTO) {
  2665. pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
  2666. return -ENOBUFS;
  2667. }
  2668. spin_lock(&net_family_lock);
  2669. if (rcu_dereference_protected(net_families[ops->family],
  2670. lockdep_is_held(&net_family_lock)))
  2671. err = -EEXIST;
  2672. else {
  2673. rcu_assign_pointer(net_families[ops->family], ops);
  2674. err = 0;
  2675. }
  2676. spin_unlock(&net_family_lock);
  2677. pr_info("NET: Registered %s protocol family\n", pf_family_names[ops->family]);
  2678. return err;
  2679. }
  2680. EXPORT_SYMBOL(sock_register);
  2681. /**
  2682. * sock_unregister - remove a protocol handler
  2683. * @family: protocol family to remove
  2684. *
  2685. * This function is called by a protocol handler that wants to
  2686. * remove its address family, and have it unlinked from the
  2687. * new socket creation.
  2688. *
  2689. * If protocol handler is a module, then it can use module reference
  2690. * counts to protect against new references. If protocol handler is not
  2691. * a module then it needs to provide its own protection in
  2692. * the ops->create routine.
  2693. */
  2694. void sock_unregister(int family)
  2695. {
  2696. BUG_ON(family < 0 || family >= NPROTO);
  2697. spin_lock(&net_family_lock);
  2698. RCU_INIT_POINTER(net_families[family], NULL);
  2699. spin_unlock(&net_family_lock);
  2700. synchronize_rcu();
  2701. pr_info("NET: Unregistered %s protocol family\n", pf_family_names[family]);
  2702. }
  2703. EXPORT_SYMBOL(sock_unregister);
  2704. bool sock_is_registered(int family)
  2705. {
  2706. return family < NPROTO && rcu_access_pointer(net_families[family]);
  2707. }
  2708. static int __init sock_init(void)
  2709. {
  2710. int err;
  2711. /*
  2712. * Initialize the network sysctl infrastructure.
  2713. */
  2714. err = net_sysctl_init();
  2715. if (err)
  2716. goto out;
  2717. /*
  2718. * Initialize skbuff SLAB cache
  2719. */
  2720. skb_init();
  2721. /*
  2722. * Initialize the protocols module.
  2723. */
  2724. init_inodecache();
  2725. err = register_filesystem(&sock_fs_type);
  2726. if (err)
  2727. goto out;
  2728. sock_mnt = kern_mount(&sock_fs_type);
  2729. if (IS_ERR(sock_mnt)) {
  2730. err = PTR_ERR(sock_mnt);
  2731. goto out_mount;
  2732. }
  2733. /* The real protocol initialization is performed in later initcalls.
  2734. */
  2735. #ifdef CONFIG_NETFILTER
  2736. err = netfilter_init();
  2737. if (err)
  2738. goto out;
  2739. #endif
  2740. ptp_classifier_init();
  2741. out:
  2742. return err;
  2743. out_mount:
  2744. unregister_filesystem(&sock_fs_type);
  2745. goto out;
  2746. }
  2747. core_initcall(sock_init); /* early initcall */
  2748. #ifdef CONFIG_PROC_FS
  2749. void socket_seq_show(struct seq_file *seq)
  2750. {
  2751. seq_printf(seq, "sockets: used %d\n",
  2752. sock_inuse_get(seq->private));
  2753. }
  2754. #endif /* CONFIG_PROC_FS */
  2755. /* Handle the fact that while struct ifreq has the same *layout* on
  2756. * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data,
  2757. * which are handled elsewhere, it still has different *size* due to
  2758. * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit,
  2759. * resulting in struct ifreq being 32 and 40 bytes respectively).
  2760. * As a result, if the struct happens to be at the end of a page and
  2761. * the next page isn't readable/writable, we get a fault. To prevent
  2762. * that, copy back and forth to the full size.
  2763. */
  2764. int get_user_ifreq(struct ifreq *ifr, void __user **ifrdata, void __user *arg)
  2765. {
  2766. if (in_compat_syscall()) {
  2767. struct compat_ifreq *ifr32 = (struct compat_ifreq *)ifr;
  2768. memset(ifr, 0, sizeof(*ifr));
  2769. if (copy_from_user(ifr32, arg, sizeof(*ifr32)))
  2770. return -EFAULT;
  2771. if (ifrdata)
  2772. *ifrdata = compat_ptr(ifr32->ifr_data);
  2773. return 0;
  2774. }
  2775. if (copy_from_user(ifr, arg, sizeof(*ifr)))
  2776. return -EFAULT;
  2777. if (ifrdata)
  2778. *ifrdata = ifr->ifr_data;
  2779. return 0;
  2780. }
  2781. EXPORT_SYMBOL(get_user_ifreq);
  2782. int put_user_ifreq(struct ifreq *ifr, void __user *arg)
  2783. {
  2784. size_t size = sizeof(*ifr);
  2785. if (in_compat_syscall())
  2786. size = sizeof(struct compat_ifreq);
  2787. if (copy_to_user(arg, ifr, size))
  2788. return -EFAULT;
  2789. return 0;
  2790. }
  2791. EXPORT_SYMBOL(put_user_ifreq);
  2792. #ifdef CONFIG_COMPAT
  2793. static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
  2794. {
  2795. compat_uptr_t uptr32;
  2796. struct ifreq ifr;
  2797. void __user *saved;
  2798. int err;
  2799. if (get_user_ifreq(&ifr, NULL, uifr32))
  2800. return -EFAULT;
  2801. if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
  2802. return -EFAULT;
  2803. saved = ifr.ifr_settings.ifs_ifsu.raw_hdlc;
  2804. ifr.ifr_settings.ifs_ifsu.raw_hdlc = compat_ptr(uptr32);
  2805. err = dev_ioctl(net, SIOCWANDEV, &ifr, NULL, NULL);
  2806. if (!err) {
  2807. ifr.ifr_settings.ifs_ifsu.raw_hdlc = saved;
  2808. if (put_user_ifreq(&ifr, uifr32))
  2809. err = -EFAULT;
  2810. }
  2811. return err;
  2812. }
  2813. /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
  2814. static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
  2815. struct compat_ifreq __user *u_ifreq32)
  2816. {
  2817. struct ifreq ifreq;
  2818. void __user *data;
  2819. if (!is_socket_ioctl_cmd(cmd))
  2820. return -ENOTTY;
  2821. if (get_user_ifreq(&ifreq, &data, u_ifreq32))
  2822. return -EFAULT;
  2823. ifreq.ifr_data = data;
  2824. return dev_ioctl(net, cmd, &ifreq, data, NULL);
  2825. }
  2826. static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
  2827. unsigned int cmd, unsigned long arg)
  2828. {
  2829. void __user *argp = compat_ptr(arg);
  2830. struct sock *sk = sock->sk;
  2831. struct net *net = sock_net(sk);
  2832. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
  2833. return sock_ioctl(file, cmd, (unsigned long)argp);
  2834. switch (cmd) {
  2835. case SIOCWANDEV:
  2836. return compat_siocwandev(net, argp);
  2837. case SIOCGSTAMP_OLD:
  2838. case SIOCGSTAMPNS_OLD:
  2839. if (!sock->ops->gettstamp)
  2840. return -ENOIOCTLCMD;
  2841. return sock->ops->gettstamp(sock, argp, cmd == SIOCGSTAMP_OLD,
  2842. !COMPAT_USE_64BIT_TIME);
  2843. case SIOCETHTOOL:
  2844. case SIOCBONDSLAVEINFOQUERY:
  2845. case SIOCBONDINFOQUERY:
  2846. case SIOCSHWTSTAMP:
  2847. case SIOCGHWTSTAMP:
  2848. return compat_ifr_data_ioctl(net, cmd, argp);
  2849. case FIOSETOWN:
  2850. case SIOCSPGRP:
  2851. case FIOGETOWN:
  2852. case SIOCGPGRP:
  2853. case SIOCBRADDBR:
  2854. case SIOCBRDELBR:
  2855. case SIOCGIFVLAN:
  2856. case SIOCSIFVLAN:
  2857. case SIOCGSKNS:
  2858. case SIOCGSTAMP_NEW:
  2859. case SIOCGSTAMPNS_NEW:
  2860. case SIOCGIFCONF:
  2861. case SIOCSIFBR:
  2862. case SIOCGIFBR:
  2863. return sock_ioctl(file, cmd, arg);
  2864. case SIOCGIFFLAGS:
  2865. case SIOCSIFFLAGS:
  2866. case SIOCGIFMAP:
  2867. case SIOCSIFMAP:
  2868. case SIOCGIFMETRIC:
  2869. case SIOCSIFMETRIC:
  2870. case SIOCGIFMTU:
  2871. case SIOCSIFMTU:
  2872. case SIOCGIFMEM:
  2873. case SIOCSIFMEM:
  2874. case SIOCGIFHWADDR:
  2875. case SIOCSIFHWADDR:
  2876. case SIOCADDMULTI:
  2877. case SIOCDELMULTI:
  2878. case SIOCGIFINDEX:
  2879. case SIOCGIFADDR:
  2880. case SIOCSIFADDR:
  2881. case SIOCSIFHWBROADCAST:
  2882. case SIOCDIFADDR:
  2883. case SIOCGIFBRDADDR:
  2884. case SIOCSIFBRDADDR:
  2885. case SIOCGIFDSTADDR:
  2886. case SIOCSIFDSTADDR:
  2887. case SIOCGIFNETMASK:
  2888. case SIOCSIFNETMASK:
  2889. case SIOCSIFPFLAGS:
  2890. case SIOCGIFPFLAGS:
  2891. case SIOCGIFTXQLEN:
  2892. case SIOCSIFTXQLEN:
  2893. case SIOCBRADDIF:
  2894. case SIOCBRDELIF:
  2895. case SIOCGIFNAME:
  2896. case SIOCSIFNAME:
  2897. case SIOCGMIIPHY:
  2898. case SIOCGMIIREG:
  2899. case SIOCSMIIREG:
  2900. case SIOCBONDENSLAVE:
  2901. case SIOCBONDRELEASE:
  2902. case SIOCBONDSETHWADDR:
  2903. case SIOCBONDCHANGEACTIVE:
  2904. case SIOCSARP:
  2905. case SIOCGARP:
  2906. case SIOCDARP:
  2907. case SIOCOUTQ:
  2908. case SIOCOUTQNSD:
  2909. case SIOCATMARK:
  2910. return sock_do_ioctl(net, sock, cmd, arg);
  2911. }
  2912. return -ENOIOCTLCMD;
  2913. }
  2914. static long compat_sock_ioctl(struct file *file, unsigned int cmd,
  2915. unsigned long arg)
  2916. {
  2917. struct socket *sock = file->private_data;
  2918. int ret = -ENOIOCTLCMD;
  2919. struct sock *sk;
  2920. struct net *net;
  2921. sk = sock->sk;
  2922. net = sock_net(sk);
  2923. if (sock->ops->compat_ioctl)
  2924. ret = sock->ops->compat_ioctl(sock, cmd, arg);
  2925. if (ret == -ENOIOCTLCMD &&
  2926. (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
  2927. ret = compat_wext_handle_ioctl(net, cmd, arg);
  2928. if (ret == -ENOIOCTLCMD)
  2929. ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
  2930. return ret;
  2931. }
  2932. #endif
  2933. /**
  2934. * kernel_bind - bind an address to a socket (kernel space)
  2935. * @sock: socket
  2936. * @addr: address
  2937. * @addrlen: length of address
  2938. *
  2939. * Returns 0 or an error.
  2940. */
  2941. int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
  2942. {
  2943. struct sockaddr_storage address;
  2944. memcpy(&address, addr, addrlen);
  2945. return sock->ops->bind(sock, (struct sockaddr *)&address, addrlen);
  2946. }
  2947. EXPORT_SYMBOL(kernel_bind);
  2948. /**
  2949. * kernel_listen - move socket to listening state (kernel space)
  2950. * @sock: socket
  2951. * @backlog: pending connections queue size
  2952. *
  2953. * Returns 0 or an error.
  2954. */
  2955. int kernel_listen(struct socket *sock, int backlog)
  2956. {
  2957. return sock->ops->listen(sock, backlog);
  2958. }
  2959. EXPORT_SYMBOL(kernel_listen);
  2960. /**
  2961. * kernel_accept - accept a connection (kernel space)
  2962. * @sock: listening socket
  2963. * @newsock: new connected socket
  2964. * @flags: flags
  2965. *
  2966. * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0.
  2967. * If it fails, @newsock is guaranteed to be %NULL.
  2968. * Returns 0 or an error.
  2969. */
  2970. int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
  2971. {
  2972. struct sock *sk = sock->sk;
  2973. int err;
  2974. err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
  2975. newsock);
  2976. if (err < 0)
  2977. goto done;
  2978. err = sock->ops->accept(sock, *newsock, flags, true);
  2979. if (err < 0) {
  2980. sock_release(*newsock);
  2981. *newsock = NULL;
  2982. goto done;
  2983. }
  2984. (*newsock)->ops = sock->ops;
  2985. __module_get((*newsock)->ops->owner);
  2986. done:
  2987. return err;
  2988. }
  2989. EXPORT_SYMBOL(kernel_accept);
  2990. /**
  2991. * kernel_connect - connect a socket (kernel space)
  2992. * @sock: socket
  2993. * @addr: address
  2994. * @addrlen: address length
  2995. * @flags: flags (O_NONBLOCK, ...)
  2996. *
  2997. * For datagram sockets, @addr is the address to which datagrams are sent
  2998. * by default, and the only address from which datagrams are received.
  2999. * For stream sockets, attempts to connect to @addr.
  3000. * Returns 0 or an error code.
  3001. */
  3002. int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
  3003. int flags)
  3004. {
  3005. struct sockaddr_storage address;
  3006. memcpy(&address, addr, addrlen);
  3007. return sock->ops->connect(sock, (struct sockaddr *)&address, addrlen, flags);
  3008. }
  3009. EXPORT_SYMBOL(kernel_connect);
  3010. /**
  3011. * kernel_getsockname - get the address which the socket is bound (kernel space)
  3012. * @sock: socket
  3013. * @addr: address holder
  3014. *
  3015. * Fills the @addr pointer with the address which the socket is bound.
  3016. * Returns the length of the address in bytes or an error code.
  3017. */
  3018. int kernel_getsockname(struct socket *sock, struct sockaddr *addr)
  3019. {
  3020. return sock->ops->getname(sock, addr, 0);
  3021. }
  3022. EXPORT_SYMBOL(kernel_getsockname);
  3023. /**
  3024. * kernel_getpeername - get the address which the socket is connected (kernel space)
  3025. * @sock: socket
  3026. * @addr: address holder
  3027. *
  3028. * Fills the @addr pointer with the address which the socket is connected.
  3029. * Returns the length of the address in bytes or an error code.
  3030. */
  3031. int kernel_getpeername(struct socket *sock, struct sockaddr *addr)
  3032. {
  3033. return sock->ops->getname(sock, addr, 1);
  3034. }
  3035. EXPORT_SYMBOL(kernel_getpeername);
  3036. /**
  3037. * kernel_sendpage - send a &page through a socket (kernel space)
  3038. * @sock: socket
  3039. * @page: page
  3040. * @offset: page offset
  3041. * @size: total size in bytes
  3042. * @flags: flags (MSG_DONTWAIT, ...)
  3043. *
  3044. * Returns the total amount sent in bytes or an error.
  3045. */
  3046. int kernel_sendpage(struct socket *sock, struct page *page, int offset,
  3047. size_t size, int flags)
  3048. {
  3049. if (sock->ops->sendpage) {
  3050. /* Warn in case the improper page to zero-copy send */
  3051. WARN_ONCE(!sendpage_ok(page), "improper page for zero-copy send");
  3052. return sock->ops->sendpage(sock, page, offset, size, flags);
  3053. }
  3054. return sock_no_sendpage(sock, page, offset, size, flags);
  3055. }
  3056. EXPORT_SYMBOL(kernel_sendpage);
  3057. /**
  3058. * kernel_sendpage_locked - send a &page through the locked sock (kernel space)
  3059. * @sk: sock
  3060. * @page: page
  3061. * @offset: page offset
  3062. * @size: total size in bytes
  3063. * @flags: flags (MSG_DONTWAIT, ...)
  3064. *
  3065. * Returns the total amount sent in bytes or an error.
  3066. * Caller must hold @sk.
  3067. */
  3068. int kernel_sendpage_locked(struct sock *sk, struct page *page, int offset,
  3069. size_t size, int flags)
  3070. {
  3071. struct socket *sock = sk->sk_socket;
  3072. if (sock->ops->sendpage_locked)
  3073. return sock->ops->sendpage_locked(sk, page, offset, size,
  3074. flags);
  3075. return sock_no_sendpage_locked(sk, page, offset, size, flags);
  3076. }
  3077. EXPORT_SYMBOL(kernel_sendpage_locked);
  3078. /**
  3079. * kernel_sock_shutdown - shut down part of a full-duplex connection (kernel space)
  3080. * @sock: socket
  3081. * @how: connection part
  3082. *
  3083. * Returns 0 or an error.
  3084. */
  3085. int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
  3086. {
  3087. return sock->ops->shutdown(sock, how);
  3088. }
  3089. EXPORT_SYMBOL(kernel_sock_shutdown);
  3090. /**
  3091. * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket
  3092. * @sk: socket
  3093. *
  3094. * This routine returns the IP overhead imposed by a socket i.e.
  3095. * the length of the underlying IP header, depending on whether
  3096. * this is an IPv4 or IPv6 socket and the length from IP options turned
  3097. * on at the socket. Assumes that the caller has a lock on the socket.
  3098. */
  3099. u32 kernel_sock_ip_overhead(struct sock *sk)
  3100. {
  3101. struct inet_sock *inet;
  3102. struct ip_options_rcu *opt;
  3103. u32 overhead = 0;
  3104. #if IS_ENABLED(CONFIG_IPV6)
  3105. struct ipv6_pinfo *np;
  3106. struct ipv6_txoptions *optv6 = NULL;
  3107. #endif /* IS_ENABLED(CONFIG_IPV6) */
  3108. if (!sk)
  3109. return overhead;
  3110. switch (sk->sk_family) {
  3111. case AF_INET:
  3112. inet = inet_sk(sk);
  3113. overhead += sizeof(struct iphdr);
  3114. opt = rcu_dereference_protected(inet->inet_opt,
  3115. sock_owned_by_user(sk));
  3116. if (opt)
  3117. overhead += opt->opt.optlen;
  3118. return overhead;
  3119. #if IS_ENABLED(CONFIG_IPV6)
  3120. case AF_INET6:
  3121. np = inet6_sk(sk);
  3122. overhead += sizeof(struct ipv6hdr);
  3123. if (np)
  3124. optv6 = rcu_dereference_protected(np->opt,
  3125. sock_owned_by_user(sk));
  3126. if (optv6)
  3127. overhead += (optv6->opt_flen + optv6->opt_nflen);
  3128. return overhead;
  3129. #endif /* IS_ENABLED(CONFIG_IPV6) */
  3130. default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
  3131. return overhead;
  3132. }
  3133. }
  3134. EXPORT_SYMBOL(kernel_sock_ip_overhead);