socket.c 91 KB

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