socket.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302
  1. /*
  2. * NET An implementation of the SOCKET network access protocol.
  3. *
  4. * Version: @(#)socket.c 1.1.93 18/02/95
  5. *
  6. * Authors: Orest Zborowski, <obz@Kodak.COM>
  7. * Ross Biro
  8. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  9. *
  10. * Fixes:
  11. * Anonymous : NOTSOCK/BADF cleanup. Error fix in
  12. * shutdown()
  13. * Alan Cox : verify_area() fixes
  14. * Alan Cox : Removed DDI
  15. * Jonathan Kamens : SOCK_DGRAM reconnect bug
  16. * Alan Cox : Moved a load of checks to the very
  17. * top level.
  18. * Alan Cox : Move address structures to/from user
  19. * mode above the protocol layers.
  20. * Rob Janssen : Allow 0 length sends.
  21. * Alan Cox : Asynchronous I/O support (cribbed from the
  22. * tty drivers).
  23. * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
  24. * Jeff Uphoff : Made max number of sockets command-line
  25. * configurable.
  26. * Matti Aarnio : Made the number of sockets dynamic,
  27. * to be allocated when needed, and mr.
  28. * Uphoff's max is used as max to be
  29. * allowed to allocate.
  30. * Linus : Argh. removed all the socket allocation
  31. * altogether: it's in the inode now.
  32. * Alan Cox : Made sock_alloc()/sock_release() public
  33. * for NetROM and future kernel nfsd type
  34. * stuff.
  35. * Alan Cox : sendmsg/recvmsg basics.
  36. * Tom Dyas : Export net symbols.
  37. * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
  38. * Alan Cox : Added thread locking to sys_* calls
  39. * for sockets. May have errors at the
  40. * moment.
  41. * Kevin Buhr : Fixed the dumb errors in the above.
  42. * Andi Kleen : Some small cleanups, optimizations,
  43. * and fixed a copy_from_user() bug.
  44. * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
  45. * Tigran Aivazian : Made listen(2) backlog sanity checks
  46. * protocol-independent
  47. *
  48. *
  49. * This program is free software; you can redistribute it and/or
  50. * modify it under the terms of the GNU General Public License
  51. * as published by the Free Software Foundation; either version
  52. * 2 of the License, or (at your option) any later version.
  53. *
  54. *
  55. * This module is effectively the top level interface to the BSD socket
  56. * paradigm.
  57. *
  58. * Based upon Swansea University Computer Society NET3.039
  59. */
  60. #include <linux/mm.h>
  61. #include <linux/socket.h>
  62. #include <linux/file.h>
  63. #include <linux/net.h>
  64. #include <linux/interrupt.h>
  65. #include <linux/thread_info.h>
  66. #include <linux/rcupdate.h>
  67. #include <linux/netdevice.h>
  68. #include <linux/proc_fs.h>
  69. #include <linux/seq_file.h>
  70. #include <linux/mutex.h>
  71. #include <linux/if_bridge.h>
  72. #include <linux/if_frad.h>
  73. #include <linux/if_vlan.h>
  74. #include <linux/ptp_classify.h>
  75. #include <linux/init.h>
  76. #include <linux/poll.h>
  77. #include <linux/cache.h>
  78. #include <linux/module.h>
  79. #include <linux/highmem.h>
  80. #include <linux/mount.h>
  81. #include <linux/security.h>
  82. #include <linux/syscalls.h>
  83. #include <linux/compat.h>
  84. #include <linux/kmod.h>
  85. #include <linux/audit.h>
  86. #include <linux/wireless.h>
  87. #include <linux/nsproxy.h>
  88. #include <linux/magic.h>
  89. #include <linux/slab.h>
  90. #include <linux/xattr.h>
  91. #include <asm/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/sockios.h>
  102. #include <linux/atalk.h>
  103. #include <net/busy_poll.h>
  104. #include <linux/errqueue.h>
  105. #ifdef CONFIG_NET_RX_BUSY_POLL
  106. unsigned int sysctl_net_busy_read __read_mostly;
  107. unsigned int sysctl_net_busy_poll __read_mostly;
  108. #endif
  109. static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
  110. static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
  111. static int sock_mmap(struct file *file, struct vm_area_struct *vma);
  112. static int sock_close(struct inode *inode, struct file *file);
  113. static unsigned int sock_poll(struct file *file,
  114. struct poll_table_struct *wait);
  115. static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  116. #ifdef CONFIG_COMPAT
  117. static long compat_sock_ioctl(struct file *file,
  118. unsigned int cmd, unsigned long arg);
  119. #endif
  120. static int sock_fasync(int fd, struct file *filp, int on);
  121. static ssize_t sock_sendpage(struct file *file, struct page *page,
  122. int offset, size_t size, loff_t *ppos, int more);
  123. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  124. struct pipe_inode_info *pipe, size_t len,
  125. unsigned int flags);
  126. /*
  127. * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
  128. * in the operation structures but are done directly via the socketcall() multiplexor.
  129. */
  130. static const struct file_operations socket_file_ops = {
  131. .owner = THIS_MODULE,
  132. .llseek = no_llseek,
  133. .read_iter = sock_read_iter,
  134. .write_iter = sock_write_iter,
  135. .poll = sock_poll,
  136. .unlocked_ioctl = sock_ioctl,
  137. #ifdef CONFIG_COMPAT
  138. .compat_ioctl = compat_sock_ioctl,
  139. #endif
  140. .mmap = sock_mmap,
  141. .release = sock_close,
  142. .fasync = sock_fasync,
  143. .sendpage = sock_sendpage,
  144. .splice_write = generic_splice_sendpage,
  145. .splice_read = sock_splice_read,
  146. };
  147. /*
  148. * The protocol list. Each protocol is registered in here.
  149. */
  150. static DEFINE_SPINLOCK(net_family_lock);
  151. static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
  152. /*
  153. * Statistics counters of the socket lists
  154. */
  155. static DEFINE_PER_CPU(int, sockets_in_use);
  156. /*
  157. * Support routines.
  158. * Move socket addresses back and forth across the kernel/user
  159. * divide and look after the messy bits.
  160. */
  161. /**
  162. * move_addr_to_kernel - copy a socket address into kernel space
  163. * @uaddr: Address in user space
  164. * @kaddr: Address in kernel space
  165. * @ulen: Length in user space
  166. *
  167. * The address is copied into kernel space. If the provided address is
  168. * too long an error code of -EINVAL is returned. If the copy gives
  169. * invalid addresses -EFAULT is returned. On a success 0 is returned.
  170. */
  171. int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
  172. {
  173. if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
  174. return -EINVAL;
  175. if (ulen == 0)
  176. return 0;
  177. if (copy_from_user(kaddr, uaddr, ulen))
  178. return -EFAULT;
  179. return audit_sockaddr(ulen, kaddr);
  180. }
  181. /**
  182. * move_addr_to_user - copy an address to user space
  183. * @kaddr: kernel space address
  184. * @klen: length of address in kernel
  185. * @uaddr: user space address
  186. * @ulen: pointer to user length field
  187. *
  188. * The value pointed to by ulen on entry is the buffer length available.
  189. * This is overwritten with the buffer space used. -EINVAL is returned
  190. * if an overlong buffer is specified or a negative buffer size. -EFAULT
  191. * is returned if either the buffer or the length field are not
  192. * accessible.
  193. * After copying the data up to the limit the user specifies, the true
  194. * length of the data is written over the length limit the user
  195. * specified. Zero is returned for a success.
  196. */
  197. static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
  198. void __user *uaddr, int __user *ulen)
  199. {
  200. int err;
  201. int len;
  202. BUG_ON(klen > sizeof(struct sockaddr_storage));
  203. err = get_user(len, ulen);
  204. if (err)
  205. return err;
  206. if (len > klen)
  207. len = klen;
  208. if (len < 0)
  209. return -EINVAL;
  210. if (len) {
  211. if (audit_sockaddr(klen, kaddr))
  212. return -ENOMEM;
  213. if (copy_to_user(uaddr, kaddr, len))
  214. return -EFAULT;
  215. }
  216. /*
  217. * "fromlen shall refer to the value before truncation.."
  218. * 1003.1g
  219. */
  220. return __put_user(klen, ulen);
  221. }
  222. static struct kmem_cache *sock_inode_cachep __read_mostly;
  223. static struct inode *sock_alloc_inode(struct super_block *sb)
  224. {
  225. struct socket_alloc *ei;
  226. struct socket_wq *wq;
  227. ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
  228. if (!ei)
  229. return NULL;
  230. wq = kmalloc(sizeof(*wq), GFP_KERNEL);
  231. if (!wq) {
  232. kmem_cache_free(sock_inode_cachep, ei);
  233. return NULL;
  234. }
  235. init_waitqueue_head(&wq->wait);
  236. wq->fasync_list = NULL;
  237. RCU_INIT_POINTER(ei->socket.wq, wq);
  238. ei->socket.state = SS_UNCONNECTED;
  239. ei->socket.flags = 0;
  240. ei->socket.ops = NULL;
  241. ei->socket.sk = NULL;
  242. ei->socket.file = NULL;
  243. return &ei->vfs_inode;
  244. }
  245. static void sock_destroy_inode(struct inode *inode)
  246. {
  247. struct socket_alloc *ei;
  248. struct socket_wq *wq;
  249. ei = container_of(inode, struct socket_alloc, vfs_inode);
  250. wq = rcu_dereference_protected(ei->socket.wq, 1);
  251. kfree_rcu(wq, rcu);
  252. kmem_cache_free(sock_inode_cachep, ei);
  253. }
  254. static void init_once(void *foo)
  255. {
  256. struct socket_alloc *ei = (struct socket_alloc *)foo;
  257. inode_init_once(&ei->vfs_inode);
  258. }
  259. static int init_inodecache(void)
  260. {
  261. sock_inode_cachep = kmem_cache_create("sock_inode_cache",
  262. sizeof(struct socket_alloc),
  263. 0,
  264. (SLAB_HWCACHE_ALIGN |
  265. SLAB_RECLAIM_ACCOUNT |
  266. SLAB_MEM_SPREAD),
  267. init_once);
  268. if (sock_inode_cachep == NULL)
  269. return -ENOMEM;
  270. return 0;
  271. }
  272. static const struct super_operations sockfs_ops = {
  273. .alloc_inode = sock_alloc_inode,
  274. .destroy_inode = sock_destroy_inode,
  275. .statfs = simple_statfs,
  276. };
  277. /*
  278. * sockfs_dname() is called from d_path().
  279. */
  280. static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
  281. {
  282. return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
  283. d_inode(dentry)->i_ino);
  284. }
  285. static const struct dentry_operations sockfs_dentry_operations = {
  286. .d_dname = sockfs_dname,
  287. };
  288. static struct dentry *sockfs_mount(struct file_system_type *fs_type,
  289. int flags, const char *dev_name, void *data)
  290. {
  291. return mount_pseudo(fs_type, "socket:", &sockfs_ops,
  292. &sockfs_dentry_operations, SOCKFS_MAGIC);
  293. }
  294. static struct vfsmount *sock_mnt __read_mostly;
  295. static struct file_system_type sock_fs_type = {
  296. .name = "sockfs",
  297. .mount = sockfs_mount,
  298. .kill_sb = kill_anon_super,
  299. };
  300. /*
  301. * Obtains the first available file descriptor and sets it up for use.
  302. *
  303. * These functions create file structures and maps them to fd space
  304. * of the current process. On success it returns file descriptor
  305. * and file struct implicitly stored in sock->file.
  306. * Note that another thread may close file descriptor before we return
  307. * from this function. We use the fact that now we do not refer
  308. * to socket after mapping. If one day we will need it, this
  309. * function will increment ref. count on file by 1.
  310. *
  311. * In any case returned fd MAY BE not valid!
  312. * This race condition is unavoidable
  313. * with shared fd spaces, we cannot solve it inside kernel,
  314. * but we take care of internal coherence yet.
  315. */
  316. struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
  317. {
  318. struct qstr name = { .name = "" };
  319. struct path path;
  320. struct file *file;
  321. if (dname) {
  322. name.name = dname;
  323. name.len = strlen(name.name);
  324. } else if (sock->sk) {
  325. name.name = sock->sk->sk_prot_creator->name;
  326. name.len = strlen(name.name);
  327. }
  328. path.dentry = d_alloc_pseudo(sock_mnt->mnt_sb, &name);
  329. if (unlikely(!path.dentry))
  330. return ERR_PTR(-ENOMEM);
  331. path.mnt = mntget(sock_mnt);
  332. d_instantiate(path.dentry, SOCK_INODE(sock));
  333. file = alloc_file(&path, FMODE_READ | FMODE_WRITE,
  334. &socket_file_ops);
  335. if (unlikely(IS_ERR(file))) {
  336. /* drop dentry, keep inode */
  337. ihold(d_inode(path.dentry));
  338. path_put(&path);
  339. return file;
  340. }
  341. sock->file = file;
  342. file->f_flags = O_RDWR | (flags & O_NONBLOCK);
  343. file->private_data = sock;
  344. return file;
  345. }
  346. EXPORT_SYMBOL(sock_alloc_file);
  347. static int sock_map_fd(struct socket *sock, int flags)
  348. {
  349. struct file *newfile;
  350. int fd = get_unused_fd_flags(flags);
  351. if (unlikely(fd < 0))
  352. return fd;
  353. newfile = sock_alloc_file(sock, flags, NULL);
  354. if (likely(!IS_ERR(newfile))) {
  355. fd_install(fd, newfile);
  356. return fd;
  357. }
  358. put_unused_fd(fd);
  359. return PTR_ERR(newfile);
  360. }
  361. struct socket *sock_from_file(struct file *file, int *err)
  362. {
  363. if (file->f_op == &socket_file_ops)
  364. return file->private_data; /* set in sock_map_fd */
  365. *err = -ENOTSOCK;
  366. return NULL;
  367. }
  368. EXPORT_SYMBOL(sock_from_file);
  369. /**
  370. * sockfd_lookup - Go from a file number to its socket slot
  371. * @fd: file handle
  372. * @err: pointer to an error code return
  373. *
  374. * The file handle passed in is locked and the socket it is bound
  375. * too is returned. If an error occurs the err pointer is overwritten
  376. * with a negative errno code and NULL is returned. The function checks
  377. * for both invalid handles and passing a handle which is not a socket.
  378. *
  379. * On a success the socket object pointer is returned.
  380. */
  381. struct socket *sockfd_lookup(int fd, int *err)
  382. {
  383. struct file *file;
  384. struct socket *sock;
  385. file = fget(fd);
  386. if (!file) {
  387. *err = -EBADF;
  388. return NULL;
  389. }
  390. sock = sock_from_file(file, err);
  391. if (!sock)
  392. fput(file);
  393. return sock;
  394. }
  395. EXPORT_SYMBOL(sockfd_lookup);
  396. static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
  397. {
  398. struct fd f = fdget(fd);
  399. struct socket *sock;
  400. *err = -EBADF;
  401. if (f.file) {
  402. sock = sock_from_file(f.file, err);
  403. if (likely(sock)) {
  404. *fput_needed = f.flags;
  405. return sock;
  406. }
  407. fdput(f);
  408. }
  409. return NULL;
  410. }
  411. #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
  412. #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
  413. #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
  414. static ssize_t sockfs_getxattr(struct dentry *dentry,
  415. const char *name, void *value, size_t size)
  416. {
  417. const char *proto_name;
  418. size_t proto_size;
  419. int error;
  420. error = -ENODATA;
  421. if (!strncmp(name, XATTR_NAME_SOCKPROTONAME, XATTR_NAME_SOCKPROTONAME_LEN)) {
  422. proto_name = dentry->d_name.name;
  423. proto_size = strlen(proto_name);
  424. if (value) {
  425. error = -ERANGE;
  426. if (proto_size + 1 > size)
  427. goto out;
  428. strncpy(value, proto_name, proto_size + 1);
  429. }
  430. error = proto_size + 1;
  431. }
  432. out:
  433. return error;
  434. }
  435. static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
  436. size_t size)
  437. {
  438. ssize_t len;
  439. ssize_t used = 0;
  440. len = security_inode_listsecurity(d_inode(dentry), buffer, size);
  441. if (len < 0)
  442. return len;
  443. used += len;
  444. if (buffer) {
  445. if (size < used)
  446. return -ERANGE;
  447. buffer += len;
  448. }
  449. len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
  450. used += len;
  451. if (buffer) {
  452. if (size < used)
  453. return -ERANGE;
  454. memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
  455. buffer += len;
  456. }
  457. return used;
  458. }
  459. static const struct inode_operations sockfs_inode_ops = {
  460. .getxattr = sockfs_getxattr,
  461. .listxattr = sockfs_listxattr,
  462. };
  463. /**
  464. * sock_alloc - allocate a socket
  465. *
  466. * Allocate a new inode and socket object. The two are bound together
  467. * and initialised. The socket is then returned. If we are out of inodes
  468. * NULL is returned.
  469. */
  470. static struct socket *sock_alloc(void)
  471. {
  472. struct inode *inode;
  473. struct socket *sock;
  474. inode = new_inode_pseudo(sock_mnt->mnt_sb);
  475. if (!inode)
  476. return NULL;
  477. sock = SOCKET_I(inode);
  478. kmemcheck_annotate_bitfield(sock, type);
  479. inode->i_ino = get_next_ino();
  480. inode->i_mode = S_IFSOCK | S_IRWXUGO;
  481. inode->i_uid = current_fsuid();
  482. inode->i_gid = current_fsgid();
  483. inode->i_op = &sockfs_inode_ops;
  484. this_cpu_add(sockets_in_use, 1);
  485. return sock;
  486. }
  487. /**
  488. * sock_release - close a socket
  489. * @sock: socket to close
  490. *
  491. * The socket is released from the protocol stack if it has a release
  492. * callback, and the inode is then released if the socket is bound to
  493. * an inode not a file.
  494. */
  495. void sock_release(struct socket *sock)
  496. {
  497. if (sock->ops) {
  498. struct module *owner = sock->ops->owner;
  499. sock->ops->release(sock);
  500. sock->ops = NULL;
  501. module_put(owner);
  502. }
  503. if (rcu_dereference_protected(sock->wq, 1)->fasync_list)
  504. pr_err("%s: fasync list not empty!\n", __func__);
  505. this_cpu_sub(sockets_in_use, 1);
  506. if (!sock->file) {
  507. iput(SOCK_INODE(sock));
  508. return;
  509. }
  510. sock->file = NULL;
  511. }
  512. EXPORT_SYMBOL(sock_release);
  513. void __sock_tx_timestamp(const struct sock *sk, __u8 *tx_flags)
  514. {
  515. u8 flags = *tx_flags;
  516. if (sk->sk_tsflags & SOF_TIMESTAMPING_TX_HARDWARE)
  517. flags |= SKBTX_HW_TSTAMP;
  518. if (sk->sk_tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
  519. flags |= SKBTX_SW_TSTAMP;
  520. if (sk->sk_tsflags & SOF_TIMESTAMPING_TX_SCHED)
  521. flags |= SKBTX_SCHED_TSTAMP;
  522. if (sk->sk_tsflags & SOF_TIMESTAMPING_TX_ACK)
  523. flags |= SKBTX_ACK_TSTAMP;
  524. *tx_flags = flags;
  525. }
  526. EXPORT_SYMBOL(__sock_tx_timestamp);
  527. static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
  528. {
  529. int ret = sock->ops->sendmsg(sock, msg, msg_data_left(msg));
  530. BUG_ON(ret == -EIOCBQUEUED);
  531. return ret;
  532. }
  533. int sock_sendmsg(struct socket *sock, struct msghdr *msg)
  534. {
  535. int err = security_socket_sendmsg(sock, msg,
  536. msg_data_left(msg));
  537. return err ?: sock_sendmsg_nosec(sock, msg);
  538. }
  539. EXPORT_SYMBOL(sock_sendmsg);
  540. int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
  541. struct kvec *vec, size_t num, size_t size)
  542. {
  543. iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC, vec, num, size);
  544. return sock_sendmsg(sock, msg);
  545. }
  546. EXPORT_SYMBOL(kernel_sendmsg);
  547. /*
  548. * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
  549. */
  550. void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
  551. struct sk_buff *skb)
  552. {
  553. int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
  554. struct scm_timestamping tss;
  555. int empty = 1;
  556. struct skb_shared_hwtstamps *shhwtstamps =
  557. skb_hwtstamps(skb);
  558. /* Race occurred between timestamp enabling and packet
  559. receiving. Fill in the current time for now. */
  560. if (need_software_tstamp && skb->tstamp.tv64 == 0)
  561. __net_timestamp(skb);
  562. if (need_software_tstamp) {
  563. if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
  564. struct timeval tv;
  565. skb_get_timestamp(skb, &tv);
  566. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP,
  567. sizeof(tv), &tv);
  568. } else {
  569. struct timespec ts;
  570. skb_get_timestampns(skb, &ts);
  571. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS,
  572. sizeof(ts), &ts);
  573. }
  574. }
  575. memset(&tss, 0, sizeof(tss));
  576. if ((sk->sk_tsflags & SOF_TIMESTAMPING_SOFTWARE) &&
  577. ktime_to_timespec_cond(skb->tstamp, tss.ts + 0))
  578. empty = 0;
  579. if (shhwtstamps &&
  580. (sk->sk_tsflags & SOF_TIMESTAMPING_RAW_HARDWARE) &&
  581. ktime_to_timespec_cond(shhwtstamps->hwtstamp, tss.ts + 2))
  582. empty = 0;
  583. if (!empty)
  584. put_cmsg(msg, SOL_SOCKET,
  585. SCM_TIMESTAMPING, sizeof(tss), &tss);
  586. }
  587. EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
  588. void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
  589. struct sk_buff *skb)
  590. {
  591. int ack;
  592. if (!sock_flag(sk, SOCK_WIFI_STATUS))
  593. return;
  594. if (!skb->wifi_acked_valid)
  595. return;
  596. ack = skb->wifi_acked;
  597. put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
  598. }
  599. EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
  600. static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
  601. struct sk_buff *skb)
  602. {
  603. if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
  604. put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
  605. sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
  606. }
  607. void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
  608. struct sk_buff *skb)
  609. {
  610. sock_recv_timestamp(msg, sk, skb);
  611. sock_recv_drops(msg, sk, skb);
  612. }
  613. EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
  614. static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
  615. size_t size, int flags)
  616. {
  617. return sock->ops->recvmsg(sock, msg, size, flags);
  618. }
  619. int sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  620. int flags)
  621. {
  622. int err = security_socket_recvmsg(sock, msg, size, flags);
  623. return err ?: sock_recvmsg_nosec(sock, msg, size, flags);
  624. }
  625. EXPORT_SYMBOL(sock_recvmsg);
  626. /**
  627. * kernel_recvmsg - Receive a message from a socket (kernel space)
  628. * @sock: The socket to receive the message from
  629. * @msg: Received message
  630. * @vec: Input s/g array for message data
  631. * @num: Size of input s/g array
  632. * @size: Number of bytes to read
  633. * @flags: Message flags (MSG_DONTWAIT, etc...)
  634. *
  635. * On return the msg structure contains the scatter/gather array passed in the
  636. * vec argument. The array is modified so that it consists of the unfilled
  637. * portion of the original array.
  638. *
  639. * The returned value is the total number of bytes received, or an error.
  640. */
  641. int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
  642. struct kvec *vec, size_t num, size_t size, int flags)
  643. {
  644. mm_segment_t oldfs = get_fs();
  645. int result;
  646. iov_iter_kvec(&msg->msg_iter, READ | ITER_KVEC, vec, num, size);
  647. set_fs(KERNEL_DS);
  648. result = sock_recvmsg(sock, msg, size, flags);
  649. set_fs(oldfs);
  650. return result;
  651. }
  652. EXPORT_SYMBOL(kernel_recvmsg);
  653. static ssize_t sock_sendpage(struct file *file, struct page *page,
  654. int offset, size_t size, loff_t *ppos, int more)
  655. {
  656. struct socket *sock;
  657. int flags;
  658. sock = file->private_data;
  659. flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  660. /* more is a combination of MSG_MORE and MSG_SENDPAGE_NOTLAST */
  661. flags |= more;
  662. return kernel_sendpage(sock, page, offset, size, flags);
  663. }
  664. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  665. struct pipe_inode_info *pipe, size_t len,
  666. unsigned int flags)
  667. {
  668. struct socket *sock = file->private_data;
  669. if (unlikely(!sock->ops->splice_read))
  670. return -EINVAL;
  671. return sock->ops->splice_read(sock, ppos, pipe, len, flags);
  672. }
  673. static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
  674. {
  675. struct file *file = iocb->ki_filp;
  676. struct socket *sock = file->private_data;
  677. struct msghdr msg = {.msg_iter = *to,
  678. .msg_iocb = iocb};
  679. ssize_t res;
  680. if (file->f_flags & O_NONBLOCK)
  681. msg.msg_flags = MSG_DONTWAIT;
  682. if (iocb->ki_pos != 0)
  683. return -ESPIPE;
  684. if (!iov_iter_count(to)) /* Match SYS5 behaviour */
  685. return 0;
  686. res = sock_recvmsg(sock, &msg, iov_iter_count(to), msg.msg_flags);
  687. *to = msg.msg_iter;
  688. return res;
  689. }
  690. static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
  691. {
  692. struct file *file = iocb->ki_filp;
  693. struct socket *sock = file->private_data;
  694. struct msghdr msg = {.msg_iter = *from,
  695. .msg_iocb = iocb};
  696. ssize_t res;
  697. if (iocb->ki_pos != 0)
  698. return -ESPIPE;
  699. if (file->f_flags & O_NONBLOCK)
  700. msg.msg_flags = MSG_DONTWAIT;
  701. if (sock->type == SOCK_SEQPACKET)
  702. msg.msg_flags |= MSG_EOR;
  703. res = sock_sendmsg(sock, &msg);
  704. *from = msg.msg_iter;
  705. return res;
  706. }
  707. /*
  708. * Atomic setting of ioctl hooks to avoid race
  709. * with module unload.
  710. */
  711. static DEFINE_MUTEX(br_ioctl_mutex);
  712. static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg);
  713. void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
  714. {
  715. mutex_lock(&br_ioctl_mutex);
  716. br_ioctl_hook = hook;
  717. mutex_unlock(&br_ioctl_mutex);
  718. }
  719. EXPORT_SYMBOL(brioctl_set);
  720. static DEFINE_MUTEX(vlan_ioctl_mutex);
  721. static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
  722. void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
  723. {
  724. mutex_lock(&vlan_ioctl_mutex);
  725. vlan_ioctl_hook = hook;
  726. mutex_unlock(&vlan_ioctl_mutex);
  727. }
  728. EXPORT_SYMBOL(vlan_ioctl_set);
  729. static DEFINE_MUTEX(dlci_ioctl_mutex);
  730. static int (*dlci_ioctl_hook) (unsigned int, void __user *);
  731. void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
  732. {
  733. mutex_lock(&dlci_ioctl_mutex);
  734. dlci_ioctl_hook = hook;
  735. mutex_unlock(&dlci_ioctl_mutex);
  736. }
  737. EXPORT_SYMBOL(dlci_ioctl_set);
  738. static long sock_do_ioctl(struct net *net, struct socket *sock,
  739. unsigned int cmd, unsigned long arg)
  740. {
  741. int err;
  742. void __user *argp = (void __user *)arg;
  743. err = sock->ops->ioctl(sock, cmd, arg);
  744. /*
  745. * If this ioctl is unknown try to hand it down
  746. * to the NIC driver.
  747. */
  748. if (err == -ENOIOCTLCMD)
  749. err = dev_ioctl(net, cmd, argp);
  750. return err;
  751. }
  752. /*
  753. * With an ioctl, arg may well be a user mode pointer, but we don't know
  754. * what to do with it - that's up to the protocol still.
  755. */
  756. static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  757. {
  758. struct socket *sock;
  759. struct sock *sk;
  760. void __user *argp = (void __user *)arg;
  761. int pid, err;
  762. struct net *net;
  763. sock = file->private_data;
  764. sk = sock->sk;
  765. net = sock_net(sk);
  766. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
  767. err = dev_ioctl(net, cmd, argp);
  768. } else
  769. #ifdef CONFIG_WEXT_CORE
  770. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
  771. err = dev_ioctl(net, cmd, argp);
  772. } else
  773. #endif
  774. switch (cmd) {
  775. case FIOSETOWN:
  776. case SIOCSPGRP:
  777. err = -EFAULT;
  778. if (get_user(pid, (int __user *)argp))
  779. break;
  780. f_setown(sock->file, pid, 1);
  781. err = 0;
  782. break;
  783. case FIOGETOWN:
  784. case SIOCGPGRP:
  785. err = put_user(f_getown(sock->file),
  786. (int __user *)argp);
  787. break;
  788. case SIOCGIFBR:
  789. case SIOCSIFBR:
  790. case SIOCBRADDBR:
  791. case SIOCBRDELBR:
  792. err = -ENOPKG;
  793. if (!br_ioctl_hook)
  794. request_module("bridge");
  795. mutex_lock(&br_ioctl_mutex);
  796. if (br_ioctl_hook)
  797. err = br_ioctl_hook(net, cmd, argp);
  798. mutex_unlock(&br_ioctl_mutex);
  799. break;
  800. case SIOCGIFVLAN:
  801. case SIOCSIFVLAN:
  802. err = -ENOPKG;
  803. if (!vlan_ioctl_hook)
  804. request_module("8021q");
  805. mutex_lock(&vlan_ioctl_mutex);
  806. if (vlan_ioctl_hook)
  807. err = vlan_ioctl_hook(net, argp);
  808. mutex_unlock(&vlan_ioctl_mutex);
  809. break;
  810. case SIOCADDDLCI:
  811. case SIOCDELDLCI:
  812. err = -ENOPKG;
  813. if (!dlci_ioctl_hook)
  814. request_module("dlci");
  815. mutex_lock(&dlci_ioctl_mutex);
  816. if (dlci_ioctl_hook)
  817. err = dlci_ioctl_hook(cmd, argp);
  818. mutex_unlock(&dlci_ioctl_mutex);
  819. break;
  820. default:
  821. err = sock_do_ioctl(net, sock, cmd, arg);
  822. break;
  823. }
  824. return err;
  825. }
  826. int sock_create_lite(int family, int type, int protocol, struct socket **res)
  827. {
  828. int err;
  829. struct socket *sock = NULL;
  830. err = security_socket_create(family, type, protocol, 1);
  831. if (err)
  832. goto out;
  833. sock = sock_alloc();
  834. if (!sock) {
  835. err = -ENOMEM;
  836. goto out;
  837. }
  838. sock->type = type;
  839. err = security_socket_post_create(sock, family, type, protocol, 1);
  840. if (err)
  841. goto out_release;
  842. out:
  843. *res = sock;
  844. return err;
  845. out_release:
  846. sock_release(sock);
  847. sock = NULL;
  848. goto out;
  849. }
  850. EXPORT_SYMBOL(sock_create_lite);
  851. /* No kernel lock held - perfect */
  852. static unsigned int sock_poll(struct file *file, poll_table *wait)
  853. {
  854. unsigned int busy_flag = 0;
  855. struct socket *sock;
  856. /*
  857. * We can't return errors to poll, so it's either yes or no.
  858. */
  859. sock = file->private_data;
  860. if (sk_can_busy_loop(sock->sk)) {
  861. /* this socket can poll_ll so tell the system call */
  862. busy_flag = POLL_BUSY_LOOP;
  863. /* once, only if requested by syscall */
  864. if (wait && (wait->_key & POLL_BUSY_LOOP))
  865. sk_busy_loop(sock->sk, 1);
  866. }
  867. return busy_flag | sock->ops->poll(file, sock, wait);
  868. }
  869. static int sock_mmap(struct file *file, struct vm_area_struct *vma)
  870. {
  871. struct socket *sock = file->private_data;
  872. return sock->ops->mmap(file, sock, vma);
  873. }
  874. static int sock_close(struct inode *inode, struct file *filp)
  875. {
  876. sock_release(SOCKET_I(inode));
  877. return 0;
  878. }
  879. /*
  880. * Update the socket async list
  881. *
  882. * Fasync_list locking strategy.
  883. *
  884. * 1. fasync_list is modified only under process context socket lock
  885. * i.e. under semaphore.
  886. * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
  887. * or under socket lock
  888. */
  889. static int sock_fasync(int fd, struct file *filp, int on)
  890. {
  891. struct socket *sock = filp->private_data;
  892. struct sock *sk = sock->sk;
  893. struct socket_wq *wq;
  894. if (sk == NULL)
  895. return -EINVAL;
  896. lock_sock(sk);
  897. wq = rcu_dereference_protected(sock->wq, sock_owned_by_user(sk));
  898. fasync_helper(fd, filp, on, &wq->fasync_list);
  899. if (!wq->fasync_list)
  900. sock_reset_flag(sk, SOCK_FASYNC);
  901. else
  902. sock_set_flag(sk, SOCK_FASYNC);
  903. release_sock(sk);
  904. return 0;
  905. }
  906. /* This function may be called only under socket lock or callback_lock or rcu_lock */
  907. int sock_wake_async(struct socket *sock, int how, int band)
  908. {
  909. struct socket_wq *wq;
  910. if (!sock)
  911. return -1;
  912. rcu_read_lock();
  913. wq = rcu_dereference(sock->wq);
  914. if (!wq || !wq->fasync_list) {
  915. rcu_read_unlock();
  916. return -1;
  917. }
  918. switch (how) {
  919. case SOCK_WAKE_WAITD:
  920. if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
  921. break;
  922. goto call_kill;
  923. case SOCK_WAKE_SPACE:
  924. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
  925. break;
  926. /* fall through */
  927. case SOCK_WAKE_IO:
  928. call_kill:
  929. kill_fasync(&wq->fasync_list, SIGIO, band);
  930. break;
  931. case SOCK_WAKE_URG:
  932. kill_fasync(&wq->fasync_list, SIGURG, band);
  933. }
  934. rcu_read_unlock();
  935. return 0;
  936. }
  937. EXPORT_SYMBOL(sock_wake_async);
  938. int __sock_create(struct net *net, int family, int type, int protocol,
  939. struct socket **res, int kern)
  940. {
  941. int err;
  942. struct socket *sock;
  943. const struct net_proto_family *pf;
  944. /*
  945. * Check protocol is in range
  946. */
  947. if (family < 0 || family >= NPROTO)
  948. return -EAFNOSUPPORT;
  949. if (type < 0 || type >= SOCK_MAX)
  950. return -EINVAL;
  951. /* Compatibility.
  952. This uglymoron is moved from INET layer to here to avoid
  953. deadlock in module load.
  954. */
  955. if (family == PF_INET && type == SOCK_PACKET) {
  956. static int warned;
  957. if (!warned) {
  958. warned = 1;
  959. pr_info("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
  960. current->comm);
  961. }
  962. family = PF_PACKET;
  963. }
  964. err = security_socket_create(family, type, protocol, kern);
  965. if (err)
  966. return err;
  967. /*
  968. * Allocate the socket and allow the family to set things up. if
  969. * the protocol is 0, the family is instructed to select an appropriate
  970. * default.
  971. */
  972. sock = sock_alloc();
  973. if (!sock) {
  974. net_warn_ratelimited("socket: no more sockets\n");
  975. return -ENFILE; /* Not exactly a match, but its the
  976. closest posix thing */
  977. }
  978. sock->type = type;
  979. #ifdef CONFIG_MODULES
  980. /* Attempt to load a protocol module if the find failed.
  981. *
  982. * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
  983. * requested real, full-featured networking support upon configuration.
  984. * Otherwise module support will break!
  985. */
  986. if (rcu_access_pointer(net_families[family]) == NULL)
  987. request_module("net-pf-%d", family);
  988. #endif
  989. rcu_read_lock();
  990. pf = rcu_dereference(net_families[family]);
  991. err = -EAFNOSUPPORT;
  992. if (!pf)
  993. goto out_release;
  994. /*
  995. * We will call the ->create function, that possibly is in a loadable
  996. * module, so we have to bump that loadable module refcnt first.
  997. */
  998. if (!try_module_get(pf->owner))
  999. goto out_release;
  1000. /* Now protected by module ref count */
  1001. rcu_read_unlock();
  1002. err = pf->create(net, sock, protocol, kern);
  1003. if (err < 0)
  1004. goto out_module_put;
  1005. /*
  1006. * Now to bump the refcnt of the [loadable] module that owns this
  1007. * socket at sock_release time we decrement its refcnt.
  1008. */
  1009. if (!try_module_get(sock->ops->owner))
  1010. goto out_module_busy;
  1011. /*
  1012. * Now that we're done with the ->create function, the [loadable]
  1013. * module can have its refcnt decremented
  1014. */
  1015. module_put(pf->owner);
  1016. err = security_socket_post_create(sock, family, type, protocol, kern);
  1017. if (err)
  1018. goto out_sock_release;
  1019. *res = sock;
  1020. return 0;
  1021. out_module_busy:
  1022. err = -EAFNOSUPPORT;
  1023. out_module_put:
  1024. sock->ops = NULL;
  1025. module_put(pf->owner);
  1026. out_sock_release:
  1027. sock_release(sock);
  1028. return err;
  1029. out_release:
  1030. rcu_read_unlock();
  1031. goto out_sock_release;
  1032. }
  1033. EXPORT_SYMBOL(__sock_create);
  1034. int sock_create(int family, int type, int protocol, struct socket **res)
  1035. {
  1036. return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
  1037. }
  1038. EXPORT_SYMBOL(sock_create);
  1039. int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
  1040. {
  1041. return __sock_create(net, family, type, protocol, res, 1);
  1042. }
  1043. EXPORT_SYMBOL(sock_create_kern);
  1044. SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
  1045. {
  1046. int retval;
  1047. struct socket *sock;
  1048. int flags;
  1049. /* Check the SOCK_* constants for consistency. */
  1050. BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
  1051. BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
  1052. BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
  1053. BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
  1054. flags = type & ~SOCK_TYPE_MASK;
  1055. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1056. return -EINVAL;
  1057. type &= SOCK_TYPE_MASK;
  1058. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1059. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1060. retval = sock_create(family, type, protocol, &sock);
  1061. if (retval < 0)
  1062. goto out;
  1063. retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
  1064. if (retval < 0)
  1065. goto out_release;
  1066. out:
  1067. /* It may be already another descriptor 8) Not kernel problem. */
  1068. return retval;
  1069. out_release:
  1070. sock_release(sock);
  1071. return retval;
  1072. }
  1073. /*
  1074. * Create a pair of connected sockets.
  1075. */
  1076. SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
  1077. int __user *, usockvec)
  1078. {
  1079. struct socket *sock1, *sock2;
  1080. int fd1, fd2, err;
  1081. struct file *newfile1, *newfile2;
  1082. int flags;
  1083. flags = type & ~SOCK_TYPE_MASK;
  1084. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1085. return -EINVAL;
  1086. type &= SOCK_TYPE_MASK;
  1087. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1088. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1089. /*
  1090. * Obtain the first socket and check if the underlying protocol
  1091. * supports the socketpair call.
  1092. */
  1093. err = sock_create(family, type, protocol, &sock1);
  1094. if (err < 0)
  1095. goto out;
  1096. err = sock_create(family, type, protocol, &sock2);
  1097. if (err < 0)
  1098. goto out_release_1;
  1099. err = sock1->ops->socketpair(sock1, sock2);
  1100. if (err < 0)
  1101. goto out_release_both;
  1102. fd1 = get_unused_fd_flags(flags);
  1103. if (unlikely(fd1 < 0)) {
  1104. err = fd1;
  1105. goto out_release_both;
  1106. }
  1107. fd2 = get_unused_fd_flags(flags);
  1108. if (unlikely(fd2 < 0)) {
  1109. err = fd2;
  1110. goto out_put_unused_1;
  1111. }
  1112. newfile1 = sock_alloc_file(sock1, flags, NULL);
  1113. if (unlikely(IS_ERR(newfile1))) {
  1114. err = PTR_ERR(newfile1);
  1115. goto out_put_unused_both;
  1116. }
  1117. newfile2 = sock_alloc_file(sock2, flags, NULL);
  1118. if (IS_ERR(newfile2)) {
  1119. err = PTR_ERR(newfile2);
  1120. goto out_fput_1;
  1121. }
  1122. err = put_user(fd1, &usockvec[0]);
  1123. if (err)
  1124. goto out_fput_both;
  1125. err = put_user(fd2, &usockvec[1]);
  1126. if (err)
  1127. goto out_fput_both;
  1128. audit_fd_pair(fd1, fd2);
  1129. fd_install(fd1, newfile1);
  1130. fd_install(fd2, newfile2);
  1131. /* fd1 and fd2 may be already another descriptors.
  1132. * Not kernel problem.
  1133. */
  1134. return 0;
  1135. out_fput_both:
  1136. fput(newfile2);
  1137. fput(newfile1);
  1138. put_unused_fd(fd2);
  1139. put_unused_fd(fd1);
  1140. goto out;
  1141. out_fput_1:
  1142. fput(newfile1);
  1143. put_unused_fd(fd2);
  1144. put_unused_fd(fd1);
  1145. sock_release(sock2);
  1146. goto out;
  1147. out_put_unused_both:
  1148. put_unused_fd(fd2);
  1149. out_put_unused_1:
  1150. put_unused_fd(fd1);
  1151. out_release_both:
  1152. sock_release(sock2);
  1153. out_release_1:
  1154. sock_release(sock1);
  1155. out:
  1156. return err;
  1157. }
  1158. /*
  1159. * Bind a name to a socket. Nothing much to do here since it's
  1160. * the protocol's responsibility to handle the local address.
  1161. *
  1162. * We move the socket address to kernel space before we call
  1163. * the protocol layer (having also checked the address is ok).
  1164. */
  1165. SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
  1166. {
  1167. struct socket *sock;
  1168. struct sockaddr_storage address;
  1169. int err, fput_needed;
  1170. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1171. if (sock) {
  1172. err = move_addr_to_kernel(umyaddr, addrlen, &address);
  1173. if (err >= 0) {
  1174. err = security_socket_bind(sock,
  1175. (struct sockaddr *)&address,
  1176. addrlen);
  1177. if (!err)
  1178. err = sock->ops->bind(sock,
  1179. (struct sockaddr *)
  1180. &address, addrlen);
  1181. }
  1182. fput_light(sock->file, fput_needed);
  1183. }
  1184. return err;
  1185. }
  1186. /*
  1187. * Perform a listen. Basically, we allow the protocol to do anything
  1188. * necessary for a listen, and if that works, we mark the socket as
  1189. * ready for listening.
  1190. */
  1191. SYSCALL_DEFINE2(listen, int, fd, int, backlog)
  1192. {
  1193. struct socket *sock;
  1194. int err, fput_needed;
  1195. int somaxconn;
  1196. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1197. if (sock) {
  1198. somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
  1199. if ((unsigned int)backlog > somaxconn)
  1200. backlog = somaxconn;
  1201. err = security_socket_listen(sock, backlog);
  1202. if (!err)
  1203. err = sock->ops->listen(sock, backlog);
  1204. fput_light(sock->file, fput_needed);
  1205. }
  1206. return err;
  1207. }
  1208. /*
  1209. * For accept, we attempt to create a new socket, set up the link
  1210. * with the client, wake up the client, then return the new
  1211. * connected fd. We collect the address of the connector in kernel
  1212. * space and move it to user at the very end. This is unclean because
  1213. * we open the socket then return an error.
  1214. *
  1215. * 1003.1g adds the ability to recvmsg() to query connection pending
  1216. * status to recvmsg. We need to add that support in a way thats
  1217. * clean when we restucture accept also.
  1218. */
  1219. SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1220. int __user *, upeer_addrlen, int, flags)
  1221. {
  1222. struct socket *sock, *newsock;
  1223. struct file *newfile;
  1224. int err, len, newfd, fput_needed;
  1225. struct sockaddr_storage address;
  1226. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1227. return -EINVAL;
  1228. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1229. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1230. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1231. if (!sock)
  1232. goto out;
  1233. err = -ENFILE;
  1234. newsock = sock_alloc();
  1235. if (!newsock)
  1236. goto out_put;
  1237. newsock->type = sock->type;
  1238. newsock->ops = sock->ops;
  1239. /*
  1240. * We don't need try_module_get here, as the listening socket (sock)
  1241. * has the protocol module (sock->ops->owner) held.
  1242. */
  1243. __module_get(newsock->ops->owner);
  1244. newfd = get_unused_fd_flags(flags);
  1245. if (unlikely(newfd < 0)) {
  1246. err = newfd;
  1247. sock_release(newsock);
  1248. goto out_put;
  1249. }
  1250. newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
  1251. if (unlikely(IS_ERR(newfile))) {
  1252. err = PTR_ERR(newfile);
  1253. put_unused_fd(newfd);
  1254. sock_release(newsock);
  1255. goto out_put;
  1256. }
  1257. err = security_socket_accept(sock, newsock);
  1258. if (err)
  1259. goto out_fd;
  1260. err = sock->ops->accept(sock, newsock, sock->file->f_flags);
  1261. if (err < 0)
  1262. goto out_fd;
  1263. if (upeer_sockaddr) {
  1264. if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
  1265. &len, 2) < 0) {
  1266. err = -ECONNABORTED;
  1267. goto out_fd;
  1268. }
  1269. err = move_addr_to_user(&address,
  1270. len, upeer_sockaddr, upeer_addrlen);
  1271. if (err < 0)
  1272. goto out_fd;
  1273. }
  1274. /* File flags are not inherited via accept() unlike another OSes. */
  1275. fd_install(newfd, newfile);
  1276. err = newfd;
  1277. out_put:
  1278. fput_light(sock->file, fput_needed);
  1279. out:
  1280. return err;
  1281. out_fd:
  1282. fput(newfile);
  1283. put_unused_fd(newfd);
  1284. goto out_put;
  1285. }
  1286. SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1287. int __user *, upeer_addrlen)
  1288. {
  1289. return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
  1290. }
  1291. /*
  1292. * Attempt to connect to a socket with the server address. The address
  1293. * is in user space so we verify it is OK and move it to kernel space.
  1294. *
  1295. * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
  1296. * break bindings
  1297. *
  1298. * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
  1299. * other SEQPACKET protocols that take time to connect() as it doesn't
  1300. * include the -EINPROGRESS status for such sockets.
  1301. */
  1302. SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
  1303. int, addrlen)
  1304. {
  1305. struct socket *sock;
  1306. struct sockaddr_storage address;
  1307. int err, fput_needed;
  1308. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1309. if (!sock)
  1310. goto out;
  1311. err = move_addr_to_kernel(uservaddr, addrlen, &address);
  1312. if (err < 0)
  1313. goto out_put;
  1314. err =
  1315. security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
  1316. if (err)
  1317. goto out_put;
  1318. err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
  1319. sock->file->f_flags);
  1320. out_put:
  1321. fput_light(sock->file, fput_needed);
  1322. out:
  1323. return err;
  1324. }
  1325. /*
  1326. * Get the local address ('name') of a socket object. Move the obtained
  1327. * name to user space.
  1328. */
  1329. SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
  1330. int __user *, usockaddr_len)
  1331. {
  1332. struct socket *sock;
  1333. struct sockaddr_storage address;
  1334. int len, err, fput_needed;
  1335. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1336. if (!sock)
  1337. goto out;
  1338. err = security_socket_getsockname(sock);
  1339. if (err)
  1340. goto out_put;
  1341. err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
  1342. if (err)
  1343. goto out_put;
  1344. err = move_addr_to_user(&address, len, usockaddr, usockaddr_len);
  1345. out_put:
  1346. fput_light(sock->file, fput_needed);
  1347. out:
  1348. return err;
  1349. }
  1350. /*
  1351. * Get the remote address ('name') of a socket object. Move the obtained
  1352. * name to user space.
  1353. */
  1354. SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
  1355. int __user *, usockaddr_len)
  1356. {
  1357. struct socket *sock;
  1358. struct sockaddr_storage address;
  1359. int len, err, fput_needed;
  1360. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1361. if (sock != NULL) {
  1362. err = security_socket_getpeername(sock);
  1363. if (err) {
  1364. fput_light(sock->file, fput_needed);
  1365. return err;
  1366. }
  1367. err =
  1368. sock->ops->getname(sock, (struct sockaddr *)&address, &len,
  1369. 1);
  1370. if (!err)
  1371. err = move_addr_to_user(&address, len, usockaddr,
  1372. usockaddr_len);
  1373. fput_light(sock->file, fput_needed);
  1374. }
  1375. return err;
  1376. }
  1377. /*
  1378. * Send a datagram to a given address. We move the address into kernel
  1379. * space and check the user space data area is readable before invoking
  1380. * the protocol.
  1381. */
  1382. SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
  1383. unsigned int, flags, struct sockaddr __user *, addr,
  1384. int, addr_len)
  1385. {
  1386. struct socket *sock;
  1387. struct sockaddr_storage address;
  1388. int err;
  1389. struct msghdr msg;
  1390. struct iovec iov;
  1391. int fput_needed;
  1392. err = import_single_range(WRITE, buff, len, &iov, &msg.msg_iter);
  1393. if (unlikely(err))
  1394. return err;
  1395. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1396. if (!sock)
  1397. goto out;
  1398. msg.msg_name = NULL;
  1399. msg.msg_control = NULL;
  1400. msg.msg_controllen = 0;
  1401. msg.msg_namelen = 0;
  1402. if (addr) {
  1403. err = move_addr_to_kernel(addr, addr_len, &address);
  1404. if (err < 0)
  1405. goto out_put;
  1406. msg.msg_name = (struct sockaddr *)&address;
  1407. msg.msg_namelen = addr_len;
  1408. }
  1409. if (sock->file->f_flags & O_NONBLOCK)
  1410. flags |= MSG_DONTWAIT;
  1411. msg.msg_flags = flags;
  1412. err = sock_sendmsg(sock, &msg);
  1413. out_put:
  1414. fput_light(sock->file, fput_needed);
  1415. out:
  1416. return err;
  1417. }
  1418. /*
  1419. * Send a datagram down a socket.
  1420. */
  1421. SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
  1422. unsigned int, flags)
  1423. {
  1424. return sys_sendto(fd, buff, len, flags, NULL, 0);
  1425. }
  1426. /*
  1427. * Receive a frame from the socket and optionally record the address of the
  1428. * sender. We verify the buffers are writable and if needed move the
  1429. * sender address from kernel to user space.
  1430. */
  1431. SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
  1432. unsigned int, flags, struct sockaddr __user *, addr,
  1433. int __user *, addr_len)
  1434. {
  1435. struct socket *sock;
  1436. struct iovec iov;
  1437. struct msghdr msg;
  1438. struct sockaddr_storage address;
  1439. int err, err2;
  1440. int fput_needed;
  1441. err = import_single_range(READ, ubuf, size, &iov, &msg.msg_iter);
  1442. if (unlikely(err))
  1443. return err;
  1444. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1445. if (!sock)
  1446. goto out;
  1447. msg.msg_control = NULL;
  1448. msg.msg_controllen = 0;
  1449. /* Save some cycles and don't copy the address if not needed */
  1450. msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
  1451. /* We assume all kernel code knows the size of sockaddr_storage */
  1452. msg.msg_namelen = 0;
  1453. if (sock->file->f_flags & O_NONBLOCK)
  1454. flags |= MSG_DONTWAIT;
  1455. err = sock_recvmsg(sock, &msg, iov_iter_count(&msg.msg_iter), flags);
  1456. if (err >= 0 && addr != NULL) {
  1457. err2 = move_addr_to_user(&address,
  1458. msg.msg_namelen, addr, addr_len);
  1459. if (err2 < 0)
  1460. err = err2;
  1461. }
  1462. fput_light(sock->file, fput_needed);
  1463. out:
  1464. return err;
  1465. }
  1466. /*
  1467. * Receive a datagram from a socket.
  1468. */
  1469. SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
  1470. unsigned int, flags)
  1471. {
  1472. return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
  1473. }
  1474. /*
  1475. * Set a socket option. Because we don't know the option lengths we have
  1476. * to pass the user mode parameter for the protocols to sort out.
  1477. */
  1478. SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
  1479. char __user *, optval, int, optlen)
  1480. {
  1481. int err, fput_needed;
  1482. struct socket *sock;
  1483. if (optlen < 0)
  1484. return -EINVAL;
  1485. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1486. if (sock != NULL) {
  1487. err = security_socket_setsockopt(sock, level, optname);
  1488. if (err)
  1489. goto out_put;
  1490. if (level == SOL_SOCKET)
  1491. err =
  1492. sock_setsockopt(sock, level, optname, optval,
  1493. optlen);
  1494. else
  1495. err =
  1496. sock->ops->setsockopt(sock, level, optname, optval,
  1497. optlen);
  1498. out_put:
  1499. fput_light(sock->file, fput_needed);
  1500. }
  1501. return err;
  1502. }
  1503. /*
  1504. * Get a socket option. Because we don't know the option lengths we have
  1505. * to pass a user mode parameter for the protocols to sort out.
  1506. */
  1507. SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
  1508. char __user *, optval, int __user *, optlen)
  1509. {
  1510. int err, fput_needed;
  1511. struct socket *sock;
  1512. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1513. if (sock != NULL) {
  1514. err = security_socket_getsockopt(sock, level, optname);
  1515. if (err)
  1516. goto out_put;
  1517. if (level == SOL_SOCKET)
  1518. err =
  1519. sock_getsockopt(sock, level, optname, optval,
  1520. optlen);
  1521. else
  1522. err =
  1523. sock->ops->getsockopt(sock, level, optname, optval,
  1524. optlen);
  1525. out_put:
  1526. fput_light(sock->file, fput_needed);
  1527. }
  1528. return err;
  1529. }
  1530. /*
  1531. * Shutdown a socket.
  1532. */
  1533. SYSCALL_DEFINE2(shutdown, int, fd, int, how)
  1534. {
  1535. int err, fput_needed;
  1536. struct socket *sock;
  1537. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1538. if (sock != NULL) {
  1539. err = security_socket_shutdown(sock, how);
  1540. if (!err)
  1541. err = sock->ops->shutdown(sock, how);
  1542. fput_light(sock->file, fput_needed);
  1543. }
  1544. return err;
  1545. }
  1546. /* A couple of helpful macros for getting the address of the 32/64 bit
  1547. * fields which are the same type (int / unsigned) on our platforms.
  1548. */
  1549. #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
  1550. #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
  1551. #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
  1552. struct used_address {
  1553. struct sockaddr_storage name;
  1554. unsigned int name_len;
  1555. };
  1556. static int copy_msghdr_from_user(struct msghdr *kmsg,
  1557. struct user_msghdr __user *umsg,
  1558. struct sockaddr __user **save_addr,
  1559. struct iovec **iov)
  1560. {
  1561. struct sockaddr __user *uaddr;
  1562. struct iovec __user *uiov;
  1563. size_t nr_segs;
  1564. ssize_t err;
  1565. if (!access_ok(VERIFY_READ, umsg, sizeof(*umsg)) ||
  1566. __get_user(uaddr, &umsg->msg_name) ||
  1567. __get_user(kmsg->msg_namelen, &umsg->msg_namelen) ||
  1568. __get_user(uiov, &umsg->msg_iov) ||
  1569. __get_user(nr_segs, &umsg->msg_iovlen) ||
  1570. __get_user(kmsg->msg_control, &umsg->msg_control) ||
  1571. __get_user(kmsg->msg_controllen, &umsg->msg_controllen) ||
  1572. __get_user(kmsg->msg_flags, &umsg->msg_flags))
  1573. return -EFAULT;
  1574. if (!uaddr)
  1575. kmsg->msg_namelen = 0;
  1576. if (kmsg->msg_namelen < 0)
  1577. return -EINVAL;
  1578. if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
  1579. kmsg->msg_namelen = sizeof(struct sockaddr_storage);
  1580. if (save_addr)
  1581. *save_addr = uaddr;
  1582. if (uaddr && kmsg->msg_namelen) {
  1583. if (!save_addr) {
  1584. err = move_addr_to_kernel(uaddr, kmsg->msg_namelen,
  1585. kmsg->msg_name);
  1586. if (err < 0)
  1587. return err;
  1588. }
  1589. } else {
  1590. kmsg->msg_name = NULL;
  1591. kmsg->msg_namelen = 0;
  1592. }
  1593. if (nr_segs > UIO_MAXIOV)
  1594. return -EMSGSIZE;
  1595. kmsg->msg_iocb = NULL;
  1596. return import_iovec(save_addr ? READ : WRITE, uiov, nr_segs,
  1597. UIO_FASTIOV, iov, &kmsg->msg_iter);
  1598. }
  1599. static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
  1600. struct msghdr *msg_sys, unsigned int flags,
  1601. struct used_address *used_address)
  1602. {
  1603. struct compat_msghdr __user *msg_compat =
  1604. (struct compat_msghdr __user *)msg;
  1605. struct sockaddr_storage address;
  1606. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  1607. unsigned char ctl[sizeof(struct cmsghdr) + 20]
  1608. __attribute__ ((aligned(sizeof(__kernel_size_t))));
  1609. /* 20 is size of ipv6_pktinfo */
  1610. unsigned char *ctl_buf = ctl;
  1611. int ctl_len;
  1612. ssize_t err;
  1613. msg_sys->msg_name = &address;
  1614. if (MSG_CMSG_COMPAT & flags)
  1615. err = get_compat_msghdr(msg_sys, msg_compat, NULL, &iov);
  1616. else
  1617. err = copy_msghdr_from_user(msg_sys, msg, NULL, &iov);
  1618. if (err < 0)
  1619. return err;
  1620. err = -ENOBUFS;
  1621. if (msg_sys->msg_controllen > INT_MAX)
  1622. goto out_freeiov;
  1623. ctl_len = msg_sys->msg_controllen;
  1624. if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
  1625. err =
  1626. cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
  1627. sizeof(ctl));
  1628. if (err)
  1629. goto out_freeiov;
  1630. ctl_buf = msg_sys->msg_control;
  1631. ctl_len = msg_sys->msg_controllen;
  1632. } else if (ctl_len) {
  1633. if (ctl_len > sizeof(ctl)) {
  1634. ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
  1635. if (ctl_buf == NULL)
  1636. goto out_freeiov;
  1637. }
  1638. err = -EFAULT;
  1639. /*
  1640. * Careful! Before this, msg_sys->msg_control contains a user pointer.
  1641. * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
  1642. * checking falls down on this.
  1643. */
  1644. if (copy_from_user(ctl_buf,
  1645. (void __user __force *)msg_sys->msg_control,
  1646. ctl_len))
  1647. goto out_freectl;
  1648. msg_sys->msg_control = ctl_buf;
  1649. }
  1650. msg_sys->msg_flags = flags;
  1651. if (sock->file->f_flags & O_NONBLOCK)
  1652. msg_sys->msg_flags |= MSG_DONTWAIT;
  1653. /*
  1654. * If this is sendmmsg() and current destination address is same as
  1655. * previously succeeded address, omit asking LSM's decision.
  1656. * used_address->name_len is initialized to UINT_MAX so that the first
  1657. * destination address never matches.
  1658. */
  1659. if (used_address && msg_sys->msg_name &&
  1660. used_address->name_len == msg_sys->msg_namelen &&
  1661. !memcmp(&used_address->name, msg_sys->msg_name,
  1662. used_address->name_len)) {
  1663. err = sock_sendmsg_nosec(sock, msg_sys);
  1664. goto out_freectl;
  1665. }
  1666. err = sock_sendmsg(sock, msg_sys);
  1667. /*
  1668. * If this is sendmmsg() and sending to current destination address was
  1669. * successful, remember it.
  1670. */
  1671. if (used_address && err >= 0) {
  1672. used_address->name_len = msg_sys->msg_namelen;
  1673. if (msg_sys->msg_name)
  1674. memcpy(&used_address->name, msg_sys->msg_name,
  1675. used_address->name_len);
  1676. }
  1677. out_freectl:
  1678. if (ctl_buf != ctl)
  1679. sock_kfree_s(sock->sk, ctl_buf, ctl_len);
  1680. out_freeiov:
  1681. kfree(iov);
  1682. return err;
  1683. }
  1684. /*
  1685. * BSD sendmsg interface
  1686. */
  1687. long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
  1688. {
  1689. int fput_needed, err;
  1690. struct msghdr msg_sys;
  1691. struct socket *sock;
  1692. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1693. if (!sock)
  1694. goto out;
  1695. err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL);
  1696. fput_light(sock->file, fput_needed);
  1697. out:
  1698. return err;
  1699. }
  1700. SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
  1701. {
  1702. if (flags & MSG_CMSG_COMPAT)
  1703. return -EINVAL;
  1704. return __sys_sendmsg(fd, msg, flags);
  1705. }
  1706. /*
  1707. * Linux sendmmsg interface
  1708. */
  1709. int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
  1710. unsigned int flags)
  1711. {
  1712. int fput_needed, err, datagrams;
  1713. struct socket *sock;
  1714. struct mmsghdr __user *entry;
  1715. struct compat_mmsghdr __user *compat_entry;
  1716. struct msghdr msg_sys;
  1717. struct used_address used_address;
  1718. if (vlen > UIO_MAXIOV)
  1719. vlen = UIO_MAXIOV;
  1720. datagrams = 0;
  1721. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1722. if (!sock)
  1723. return err;
  1724. used_address.name_len = UINT_MAX;
  1725. entry = mmsg;
  1726. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  1727. err = 0;
  1728. while (datagrams < vlen) {
  1729. if (MSG_CMSG_COMPAT & flags) {
  1730. err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
  1731. &msg_sys, flags, &used_address);
  1732. if (err < 0)
  1733. break;
  1734. err = __put_user(err, &compat_entry->msg_len);
  1735. ++compat_entry;
  1736. } else {
  1737. err = ___sys_sendmsg(sock,
  1738. (struct user_msghdr __user *)entry,
  1739. &msg_sys, flags, &used_address);
  1740. if (err < 0)
  1741. break;
  1742. err = put_user(err, &entry->msg_len);
  1743. ++entry;
  1744. }
  1745. if (err)
  1746. break;
  1747. ++datagrams;
  1748. }
  1749. fput_light(sock->file, fput_needed);
  1750. /* We only return an error if no datagrams were able to be sent */
  1751. if (datagrams != 0)
  1752. return datagrams;
  1753. return err;
  1754. }
  1755. SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
  1756. unsigned int, vlen, unsigned int, flags)
  1757. {
  1758. if (flags & MSG_CMSG_COMPAT)
  1759. return -EINVAL;
  1760. return __sys_sendmmsg(fd, mmsg, vlen, flags);
  1761. }
  1762. static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
  1763. struct msghdr *msg_sys, unsigned int flags, int nosec)
  1764. {
  1765. struct compat_msghdr __user *msg_compat =
  1766. (struct compat_msghdr __user *)msg;
  1767. struct iovec iovstack[UIO_FASTIOV];
  1768. struct iovec *iov = iovstack;
  1769. unsigned long cmsg_ptr;
  1770. int total_len, len;
  1771. ssize_t err;
  1772. /* kernel mode address */
  1773. struct sockaddr_storage addr;
  1774. /* user mode address pointers */
  1775. struct sockaddr __user *uaddr;
  1776. int __user *uaddr_len = COMPAT_NAMELEN(msg);
  1777. msg_sys->msg_name = &addr;
  1778. if (MSG_CMSG_COMPAT & flags)
  1779. err = get_compat_msghdr(msg_sys, msg_compat, &uaddr, &iov);
  1780. else
  1781. err = copy_msghdr_from_user(msg_sys, msg, &uaddr, &iov);
  1782. if (err < 0)
  1783. return err;
  1784. total_len = iov_iter_count(&msg_sys->msg_iter);
  1785. cmsg_ptr = (unsigned long)msg_sys->msg_control;
  1786. msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
  1787. /* We assume all kernel code knows the size of sockaddr_storage */
  1788. msg_sys->msg_namelen = 0;
  1789. if (sock->file->f_flags & O_NONBLOCK)
  1790. flags |= MSG_DONTWAIT;
  1791. err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
  1792. total_len, flags);
  1793. if (err < 0)
  1794. goto out_freeiov;
  1795. len = err;
  1796. if (uaddr != NULL) {
  1797. err = move_addr_to_user(&addr,
  1798. msg_sys->msg_namelen, uaddr,
  1799. uaddr_len);
  1800. if (err < 0)
  1801. goto out_freeiov;
  1802. }
  1803. err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
  1804. COMPAT_FLAGS(msg));
  1805. if (err)
  1806. goto out_freeiov;
  1807. if (MSG_CMSG_COMPAT & flags)
  1808. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  1809. &msg_compat->msg_controllen);
  1810. else
  1811. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  1812. &msg->msg_controllen);
  1813. if (err)
  1814. goto out_freeiov;
  1815. err = len;
  1816. out_freeiov:
  1817. kfree(iov);
  1818. return err;
  1819. }
  1820. /*
  1821. * BSD recvmsg interface
  1822. */
  1823. long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
  1824. {
  1825. int fput_needed, err;
  1826. struct msghdr msg_sys;
  1827. struct socket *sock;
  1828. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1829. if (!sock)
  1830. goto out;
  1831. err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
  1832. fput_light(sock->file, fput_needed);
  1833. out:
  1834. return err;
  1835. }
  1836. SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
  1837. unsigned int, flags)
  1838. {
  1839. if (flags & MSG_CMSG_COMPAT)
  1840. return -EINVAL;
  1841. return __sys_recvmsg(fd, msg, flags);
  1842. }
  1843. /*
  1844. * Linux recvmmsg interface
  1845. */
  1846. int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
  1847. unsigned int flags, struct timespec *timeout)
  1848. {
  1849. int fput_needed, err, datagrams;
  1850. struct socket *sock;
  1851. struct mmsghdr __user *entry;
  1852. struct compat_mmsghdr __user *compat_entry;
  1853. struct msghdr msg_sys;
  1854. struct timespec end_time;
  1855. if (timeout &&
  1856. poll_select_set_timeout(&end_time, timeout->tv_sec,
  1857. timeout->tv_nsec))
  1858. return -EINVAL;
  1859. datagrams = 0;
  1860. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1861. if (!sock)
  1862. return err;
  1863. err = sock_error(sock->sk);
  1864. if (err)
  1865. goto out_put;
  1866. entry = mmsg;
  1867. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  1868. while (datagrams < vlen) {
  1869. /*
  1870. * No need to ask LSM for more than the first datagram.
  1871. */
  1872. if (MSG_CMSG_COMPAT & flags) {
  1873. err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
  1874. &msg_sys, flags & ~MSG_WAITFORONE,
  1875. datagrams);
  1876. if (err < 0)
  1877. break;
  1878. err = __put_user(err, &compat_entry->msg_len);
  1879. ++compat_entry;
  1880. } else {
  1881. err = ___sys_recvmsg(sock,
  1882. (struct user_msghdr __user *)entry,
  1883. &msg_sys, flags & ~MSG_WAITFORONE,
  1884. datagrams);
  1885. if (err < 0)
  1886. break;
  1887. err = put_user(err, &entry->msg_len);
  1888. ++entry;
  1889. }
  1890. if (err)
  1891. break;
  1892. ++datagrams;
  1893. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  1894. if (flags & MSG_WAITFORONE)
  1895. flags |= MSG_DONTWAIT;
  1896. if (timeout) {
  1897. ktime_get_ts(timeout);
  1898. *timeout = timespec_sub(end_time, *timeout);
  1899. if (timeout->tv_sec < 0) {
  1900. timeout->tv_sec = timeout->tv_nsec = 0;
  1901. break;
  1902. }
  1903. /* Timeout, return less than vlen datagrams */
  1904. if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
  1905. break;
  1906. }
  1907. /* Out of band data, return right away */
  1908. if (msg_sys.msg_flags & MSG_OOB)
  1909. break;
  1910. }
  1911. out_put:
  1912. fput_light(sock->file, fput_needed);
  1913. if (err == 0)
  1914. return datagrams;
  1915. if (datagrams != 0) {
  1916. /*
  1917. * We may return less entries than requested (vlen) if the
  1918. * sock is non block and there aren't enough datagrams...
  1919. */
  1920. if (err != -EAGAIN) {
  1921. /*
  1922. * ... or if recvmsg returns an error after we
  1923. * received some datagrams, where we record the
  1924. * error to return on the next call or if the
  1925. * app asks about it using getsockopt(SO_ERROR).
  1926. */
  1927. sock->sk->sk_err = -err;
  1928. }
  1929. return datagrams;
  1930. }
  1931. return err;
  1932. }
  1933. SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
  1934. unsigned int, vlen, unsigned int, flags,
  1935. struct timespec __user *, timeout)
  1936. {
  1937. int datagrams;
  1938. struct timespec timeout_sys;
  1939. if (flags & MSG_CMSG_COMPAT)
  1940. return -EINVAL;
  1941. if (!timeout)
  1942. return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL);
  1943. if (copy_from_user(&timeout_sys, timeout, sizeof(timeout_sys)))
  1944. return -EFAULT;
  1945. datagrams = __sys_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
  1946. if (datagrams > 0 &&
  1947. copy_to_user(timeout, &timeout_sys, sizeof(timeout_sys)))
  1948. datagrams = -EFAULT;
  1949. return datagrams;
  1950. }
  1951. #ifdef __ARCH_WANT_SYS_SOCKETCALL
  1952. /* Argument list sizes for sys_socketcall */
  1953. #define AL(x) ((x) * sizeof(unsigned long))
  1954. static const unsigned char nargs[21] = {
  1955. AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
  1956. AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
  1957. AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
  1958. AL(4), AL(5), AL(4)
  1959. };
  1960. #undef AL
  1961. /*
  1962. * System call vectors.
  1963. *
  1964. * Argument checking cleaned up. Saved 20% in size.
  1965. * This function doesn't need to set the kernel lock because
  1966. * it is set by the callees.
  1967. */
  1968. SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
  1969. {
  1970. unsigned long a[AUDITSC_ARGS];
  1971. unsigned long a0, a1;
  1972. int err;
  1973. unsigned int len;
  1974. if (call < 1 || call > SYS_SENDMMSG)
  1975. return -EINVAL;
  1976. len = nargs[call];
  1977. if (len > sizeof(a))
  1978. return -EINVAL;
  1979. /* copy_from_user should be SMP safe. */
  1980. if (copy_from_user(a, args, len))
  1981. return -EFAULT;
  1982. err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
  1983. if (err)
  1984. return err;
  1985. a0 = a[0];
  1986. a1 = a[1];
  1987. switch (call) {
  1988. case SYS_SOCKET:
  1989. err = sys_socket(a0, a1, a[2]);
  1990. break;
  1991. case SYS_BIND:
  1992. err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
  1993. break;
  1994. case SYS_CONNECT:
  1995. err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
  1996. break;
  1997. case SYS_LISTEN:
  1998. err = sys_listen(a0, a1);
  1999. break;
  2000. case SYS_ACCEPT:
  2001. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  2002. (int __user *)a[2], 0);
  2003. break;
  2004. case SYS_GETSOCKNAME:
  2005. err =
  2006. sys_getsockname(a0, (struct sockaddr __user *)a1,
  2007. (int __user *)a[2]);
  2008. break;
  2009. case SYS_GETPEERNAME:
  2010. err =
  2011. sys_getpeername(a0, (struct sockaddr __user *)a1,
  2012. (int __user *)a[2]);
  2013. break;
  2014. case SYS_SOCKETPAIR:
  2015. err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
  2016. break;
  2017. case SYS_SEND:
  2018. err = sys_send(a0, (void __user *)a1, a[2], a[3]);
  2019. break;
  2020. case SYS_SENDTO:
  2021. err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
  2022. (struct sockaddr __user *)a[4], a[5]);
  2023. break;
  2024. case SYS_RECV:
  2025. err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
  2026. break;
  2027. case SYS_RECVFROM:
  2028. err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  2029. (struct sockaddr __user *)a[4],
  2030. (int __user *)a[5]);
  2031. break;
  2032. case SYS_SHUTDOWN:
  2033. err = sys_shutdown(a0, a1);
  2034. break;
  2035. case SYS_SETSOCKOPT:
  2036. err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
  2037. break;
  2038. case SYS_GETSOCKOPT:
  2039. err =
  2040. sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
  2041. (int __user *)a[4]);
  2042. break;
  2043. case SYS_SENDMSG:
  2044. err = sys_sendmsg(a0, (struct user_msghdr __user *)a1, a[2]);
  2045. break;
  2046. case SYS_SENDMMSG:
  2047. err = sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3]);
  2048. break;
  2049. case SYS_RECVMSG:
  2050. err = sys_recvmsg(a0, (struct user_msghdr __user *)a1, a[2]);
  2051. break;
  2052. case SYS_RECVMMSG:
  2053. err = sys_recvmmsg(a0, (struct mmsghdr __user *)a1, a[2], a[3],
  2054. (struct timespec __user *)a[4]);
  2055. break;
  2056. case SYS_ACCEPT4:
  2057. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  2058. (int __user *)a[2], a[3]);
  2059. break;
  2060. default:
  2061. err = -EINVAL;
  2062. break;
  2063. }
  2064. return err;
  2065. }
  2066. #endif /* __ARCH_WANT_SYS_SOCKETCALL */
  2067. /**
  2068. * sock_register - add a socket protocol handler
  2069. * @ops: description of protocol
  2070. *
  2071. * This function is called by a protocol handler that wants to
  2072. * advertise its address family, and have it linked into the
  2073. * socket interface. The value ops->family corresponds to the
  2074. * socket system call protocol family.
  2075. */
  2076. int sock_register(const struct net_proto_family *ops)
  2077. {
  2078. int err;
  2079. if (ops->family >= NPROTO) {
  2080. pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
  2081. return -ENOBUFS;
  2082. }
  2083. spin_lock(&net_family_lock);
  2084. if (rcu_dereference_protected(net_families[ops->family],
  2085. lockdep_is_held(&net_family_lock)))
  2086. err = -EEXIST;
  2087. else {
  2088. rcu_assign_pointer(net_families[ops->family], ops);
  2089. err = 0;
  2090. }
  2091. spin_unlock(&net_family_lock);
  2092. pr_info("NET: Registered protocol family %d\n", ops->family);
  2093. return err;
  2094. }
  2095. EXPORT_SYMBOL(sock_register);
  2096. /**
  2097. * sock_unregister - remove a protocol handler
  2098. * @family: protocol family to remove
  2099. *
  2100. * This function is called by a protocol handler that wants to
  2101. * remove its address family, and have it unlinked from the
  2102. * new socket creation.
  2103. *
  2104. * If protocol handler is a module, then it can use module reference
  2105. * counts to protect against new references. If protocol handler is not
  2106. * a module then it needs to provide its own protection in
  2107. * the ops->create routine.
  2108. */
  2109. void sock_unregister(int family)
  2110. {
  2111. BUG_ON(family < 0 || family >= NPROTO);
  2112. spin_lock(&net_family_lock);
  2113. RCU_INIT_POINTER(net_families[family], NULL);
  2114. spin_unlock(&net_family_lock);
  2115. synchronize_rcu();
  2116. pr_info("NET: Unregistered protocol family %d\n", family);
  2117. }
  2118. EXPORT_SYMBOL(sock_unregister);
  2119. static int __init sock_init(void)
  2120. {
  2121. int err;
  2122. /*
  2123. * Initialize the network sysctl infrastructure.
  2124. */
  2125. err = net_sysctl_init();
  2126. if (err)
  2127. goto out;
  2128. /*
  2129. * Initialize skbuff SLAB cache
  2130. */
  2131. skb_init();
  2132. /*
  2133. * Initialize the protocols module.
  2134. */
  2135. init_inodecache();
  2136. err = register_filesystem(&sock_fs_type);
  2137. if (err)
  2138. goto out_fs;
  2139. sock_mnt = kern_mount(&sock_fs_type);
  2140. if (IS_ERR(sock_mnt)) {
  2141. err = PTR_ERR(sock_mnt);
  2142. goto out_mount;
  2143. }
  2144. /* The real protocol initialization is performed in later initcalls.
  2145. */
  2146. #ifdef CONFIG_NETFILTER
  2147. err = netfilter_init();
  2148. if (err)
  2149. goto out;
  2150. #endif
  2151. ptp_classifier_init();
  2152. out:
  2153. return err;
  2154. out_mount:
  2155. unregister_filesystem(&sock_fs_type);
  2156. out_fs:
  2157. goto out;
  2158. }
  2159. core_initcall(sock_init); /* early initcall */
  2160. #ifdef CONFIG_PROC_FS
  2161. void socket_seq_show(struct seq_file *seq)
  2162. {
  2163. int cpu;
  2164. int counter = 0;
  2165. for_each_possible_cpu(cpu)
  2166. counter += per_cpu(sockets_in_use, cpu);
  2167. /* It can be negative, by the way. 8) */
  2168. if (counter < 0)
  2169. counter = 0;
  2170. seq_printf(seq, "sockets: used %d\n", counter);
  2171. }
  2172. #endif /* CONFIG_PROC_FS */
  2173. #ifdef CONFIG_COMPAT
  2174. static int do_siocgstamp(struct net *net, struct socket *sock,
  2175. unsigned int cmd, void __user *up)
  2176. {
  2177. mm_segment_t old_fs = get_fs();
  2178. struct timeval ktv;
  2179. int err;
  2180. set_fs(KERNEL_DS);
  2181. err = sock_do_ioctl(net, sock, cmd, (unsigned long)&ktv);
  2182. set_fs(old_fs);
  2183. if (!err)
  2184. err = compat_put_timeval(&ktv, up);
  2185. return err;
  2186. }
  2187. static int do_siocgstampns(struct net *net, struct socket *sock,
  2188. unsigned int cmd, void __user *up)
  2189. {
  2190. mm_segment_t old_fs = get_fs();
  2191. struct timespec kts;
  2192. int err;
  2193. set_fs(KERNEL_DS);
  2194. err = sock_do_ioctl(net, sock, cmd, (unsigned long)&kts);
  2195. set_fs(old_fs);
  2196. if (!err)
  2197. err = compat_put_timespec(&kts, up);
  2198. return err;
  2199. }
  2200. static int dev_ifname32(struct net *net, struct compat_ifreq __user *uifr32)
  2201. {
  2202. struct ifreq __user *uifr;
  2203. int err;
  2204. uifr = compat_alloc_user_space(sizeof(struct ifreq));
  2205. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2206. return -EFAULT;
  2207. err = dev_ioctl(net, SIOCGIFNAME, uifr);
  2208. if (err)
  2209. return err;
  2210. if (copy_in_user(uifr32, uifr, sizeof(struct compat_ifreq)))
  2211. return -EFAULT;
  2212. return 0;
  2213. }
  2214. static int dev_ifconf(struct net *net, struct compat_ifconf __user *uifc32)
  2215. {
  2216. struct compat_ifconf ifc32;
  2217. struct ifconf ifc;
  2218. struct ifconf __user *uifc;
  2219. struct compat_ifreq __user *ifr32;
  2220. struct ifreq __user *ifr;
  2221. unsigned int i, j;
  2222. int err;
  2223. if (copy_from_user(&ifc32, uifc32, sizeof(struct compat_ifconf)))
  2224. return -EFAULT;
  2225. memset(&ifc, 0, sizeof(ifc));
  2226. if (ifc32.ifcbuf == 0) {
  2227. ifc32.ifc_len = 0;
  2228. ifc.ifc_len = 0;
  2229. ifc.ifc_req = NULL;
  2230. uifc = compat_alloc_user_space(sizeof(struct ifconf));
  2231. } else {
  2232. size_t len = ((ifc32.ifc_len / sizeof(struct compat_ifreq)) + 1) *
  2233. sizeof(struct ifreq);
  2234. uifc = compat_alloc_user_space(sizeof(struct ifconf) + len);
  2235. ifc.ifc_len = len;
  2236. ifr = ifc.ifc_req = (void __user *)(uifc + 1);
  2237. ifr32 = compat_ptr(ifc32.ifcbuf);
  2238. for (i = 0; i < ifc32.ifc_len; i += sizeof(struct compat_ifreq)) {
  2239. if (copy_in_user(ifr, ifr32, sizeof(struct compat_ifreq)))
  2240. return -EFAULT;
  2241. ifr++;
  2242. ifr32++;
  2243. }
  2244. }
  2245. if (copy_to_user(uifc, &ifc, sizeof(struct ifconf)))
  2246. return -EFAULT;
  2247. err = dev_ioctl(net, SIOCGIFCONF, uifc);
  2248. if (err)
  2249. return err;
  2250. if (copy_from_user(&ifc, uifc, sizeof(struct ifconf)))
  2251. return -EFAULT;
  2252. ifr = ifc.ifc_req;
  2253. ifr32 = compat_ptr(ifc32.ifcbuf);
  2254. for (i = 0, j = 0;
  2255. i + sizeof(struct compat_ifreq) <= ifc32.ifc_len && j < ifc.ifc_len;
  2256. i += sizeof(struct compat_ifreq), j += sizeof(struct ifreq)) {
  2257. if (copy_in_user(ifr32, ifr, sizeof(struct compat_ifreq)))
  2258. return -EFAULT;
  2259. ifr32++;
  2260. ifr++;
  2261. }
  2262. if (ifc32.ifcbuf == 0) {
  2263. /* Translate from 64-bit structure multiple to
  2264. * a 32-bit one.
  2265. */
  2266. i = ifc.ifc_len;
  2267. i = ((i / sizeof(struct ifreq)) * sizeof(struct compat_ifreq));
  2268. ifc32.ifc_len = i;
  2269. } else {
  2270. ifc32.ifc_len = i;
  2271. }
  2272. if (copy_to_user(uifc32, &ifc32, sizeof(struct compat_ifconf)))
  2273. return -EFAULT;
  2274. return 0;
  2275. }
  2276. static int ethtool_ioctl(struct net *net, struct compat_ifreq __user *ifr32)
  2277. {
  2278. struct compat_ethtool_rxnfc __user *compat_rxnfc;
  2279. bool convert_in = false, convert_out = false;
  2280. size_t buf_size = ALIGN(sizeof(struct ifreq), 8);
  2281. struct ethtool_rxnfc __user *rxnfc;
  2282. struct ifreq __user *ifr;
  2283. u32 rule_cnt = 0, actual_rule_cnt;
  2284. u32 ethcmd;
  2285. u32 data;
  2286. int ret;
  2287. if (get_user(data, &ifr32->ifr_ifru.ifru_data))
  2288. return -EFAULT;
  2289. compat_rxnfc = compat_ptr(data);
  2290. if (get_user(ethcmd, &compat_rxnfc->cmd))
  2291. return -EFAULT;
  2292. /* Most ethtool structures are defined without padding.
  2293. * Unfortunately struct ethtool_rxnfc is an exception.
  2294. */
  2295. switch (ethcmd) {
  2296. default:
  2297. break;
  2298. case ETHTOOL_GRXCLSRLALL:
  2299. /* Buffer size is variable */
  2300. if (get_user(rule_cnt, &compat_rxnfc->rule_cnt))
  2301. return -EFAULT;
  2302. if (rule_cnt > KMALLOC_MAX_SIZE / sizeof(u32))
  2303. return -ENOMEM;
  2304. buf_size += rule_cnt * sizeof(u32);
  2305. /* fall through */
  2306. case ETHTOOL_GRXRINGS:
  2307. case ETHTOOL_GRXCLSRLCNT:
  2308. case ETHTOOL_GRXCLSRULE:
  2309. case ETHTOOL_SRXCLSRLINS:
  2310. convert_out = true;
  2311. /* fall through */
  2312. case ETHTOOL_SRXCLSRLDEL:
  2313. buf_size += sizeof(struct ethtool_rxnfc);
  2314. convert_in = true;
  2315. break;
  2316. }
  2317. ifr = compat_alloc_user_space(buf_size);
  2318. rxnfc = (void __user *)ifr + ALIGN(sizeof(struct ifreq), 8);
  2319. if (copy_in_user(&ifr->ifr_name, &ifr32->ifr_name, IFNAMSIZ))
  2320. return -EFAULT;
  2321. if (put_user(convert_in ? rxnfc : compat_ptr(data),
  2322. &ifr->ifr_ifru.ifru_data))
  2323. return -EFAULT;
  2324. if (convert_in) {
  2325. /* We expect there to be holes between fs.m_ext and
  2326. * fs.ring_cookie and at the end of fs, but nowhere else.
  2327. */
  2328. BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
  2329. sizeof(compat_rxnfc->fs.m_ext) !=
  2330. offsetof(struct ethtool_rxnfc, fs.m_ext) +
  2331. sizeof(rxnfc->fs.m_ext));
  2332. BUILD_BUG_ON(
  2333. offsetof(struct compat_ethtool_rxnfc, fs.location) -
  2334. offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
  2335. offsetof(struct ethtool_rxnfc, fs.location) -
  2336. offsetof(struct ethtool_rxnfc, fs.ring_cookie));
  2337. if (copy_in_user(rxnfc, compat_rxnfc,
  2338. (void __user *)(&rxnfc->fs.m_ext + 1) -
  2339. (void __user *)rxnfc) ||
  2340. copy_in_user(&rxnfc->fs.ring_cookie,
  2341. &compat_rxnfc->fs.ring_cookie,
  2342. (void __user *)(&rxnfc->fs.location + 1) -
  2343. (void __user *)&rxnfc->fs.ring_cookie) ||
  2344. copy_in_user(&rxnfc->rule_cnt, &compat_rxnfc->rule_cnt,
  2345. sizeof(rxnfc->rule_cnt)))
  2346. return -EFAULT;
  2347. }
  2348. ret = dev_ioctl(net, SIOCETHTOOL, ifr);
  2349. if (ret)
  2350. return ret;
  2351. if (convert_out) {
  2352. if (copy_in_user(compat_rxnfc, rxnfc,
  2353. (const void __user *)(&rxnfc->fs.m_ext + 1) -
  2354. (const void __user *)rxnfc) ||
  2355. copy_in_user(&compat_rxnfc->fs.ring_cookie,
  2356. &rxnfc->fs.ring_cookie,
  2357. (const void __user *)(&rxnfc->fs.location + 1) -
  2358. (const void __user *)&rxnfc->fs.ring_cookie) ||
  2359. copy_in_user(&compat_rxnfc->rule_cnt, &rxnfc->rule_cnt,
  2360. sizeof(rxnfc->rule_cnt)))
  2361. return -EFAULT;
  2362. if (ethcmd == ETHTOOL_GRXCLSRLALL) {
  2363. /* As an optimisation, we only copy the actual
  2364. * number of rules that the underlying
  2365. * function returned. Since Mallory might
  2366. * change the rule count in user memory, we
  2367. * check that it is less than the rule count
  2368. * originally given (as the user buffer size),
  2369. * which has been range-checked.
  2370. */
  2371. if (get_user(actual_rule_cnt, &rxnfc->rule_cnt))
  2372. return -EFAULT;
  2373. if (actual_rule_cnt < rule_cnt)
  2374. rule_cnt = actual_rule_cnt;
  2375. if (copy_in_user(&compat_rxnfc->rule_locs[0],
  2376. &rxnfc->rule_locs[0],
  2377. rule_cnt * sizeof(u32)))
  2378. return -EFAULT;
  2379. }
  2380. }
  2381. return 0;
  2382. }
  2383. static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
  2384. {
  2385. void __user *uptr;
  2386. compat_uptr_t uptr32;
  2387. struct ifreq __user *uifr;
  2388. uifr = compat_alloc_user_space(sizeof(*uifr));
  2389. if (copy_in_user(uifr, uifr32, sizeof(struct compat_ifreq)))
  2390. return -EFAULT;
  2391. if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
  2392. return -EFAULT;
  2393. uptr = compat_ptr(uptr32);
  2394. if (put_user(uptr, &uifr->ifr_settings.ifs_ifsu.raw_hdlc))
  2395. return -EFAULT;
  2396. return dev_ioctl(net, SIOCWANDEV, uifr);
  2397. }
  2398. static int bond_ioctl(struct net *net, unsigned int cmd,
  2399. struct compat_ifreq __user *ifr32)
  2400. {
  2401. struct ifreq kifr;
  2402. mm_segment_t old_fs;
  2403. int err;
  2404. switch (cmd) {
  2405. case SIOCBONDENSLAVE:
  2406. case SIOCBONDRELEASE:
  2407. case SIOCBONDSETHWADDR:
  2408. case SIOCBONDCHANGEACTIVE:
  2409. if (copy_from_user(&kifr, ifr32, sizeof(struct compat_ifreq)))
  2410. return -EFAULT;
  2411. old_fs = get_fs();
  2412. set_fs(KERNEL_DS);
  2413. err = dev_ioctl(net, cmd,
  2414. (struct ifreq __user __force *) &kifr);
  2415. set_fs(old_fs);
  2416. return err;
  2417. default:
  2418. return -ENOIOCTLCMD;
  2419. }
  2420. }
  2421. /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
  2422. static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
  2423. struct compat_ifreq __user *u_ifreq32)
  2424. {
  2425. struct ifreq __user *u_ifreq64;
  2426. char tmp_buf[IFNAMSIZ];
  2427. void __user *data64;
  2428. u32 data32;
  2429. if (copy_from_user(&tmp_buf[0], &(u_ifreq32->ifr_ifrn.ifrn_name[0]),
  2430. IFNAMSIZ))
  2431. return -EFAULT;
  2432. if (get_user(data32, &u_ifreq32->ifr_ifru.ifru_data))
  2433. return -EFAULT;
  2434. data64 = compat_ptr(data32);
  2435. u_ifreq64 = compat_alloc_user_space(sizeof(*u_ifreq64));
  2436. if (copy_to_user(&u_ifreq64->ifr_ifrn.ifrn_name[0], &tmp_buf[0],
  2437. IFNAMSIZ))
  2438. return -EFAULT;
  2439. if (put_user(data64, &u_ifreq64->ifr_ifru.ifru_data))
  2440. return -EFAULT;
  2441. return dev_ioctl(net, cmd, u_ifreq64);
  2442. }
  2443. static int dev_ifsioc(struct net *net, struct socket *sock,
  2444. unsigned int cmd, struct compat_ifreq __user *uifr32)
  2445. {
  2446. struct ifreq __user *uifr;
  2447. int err;
  2448. uifr = compat_alloc_user_space(sizeof(*uifr));
  2449. if (copy_in_user(uifr, uifr32, sizeof(*uifr32)))
  2450. return -EFAULT;
  2451. err = sock_do_ioctl(net, sock, cmd, (unsigned long)uifr);
  2452. if (!err) {
  2453. switch (cmd) {
  2454. case SIOCGIFFLAGS:
  2455. case SIOCGIFMETRIC:
  2456. case SIOCGIFMTU:
  2457. case SIOCGIFMEM:
  2458. case SIOCGIFHWADDR:
  2459. case SIOCGIFINDEX:
  2460. case SIOCGIFADDR:
  2461. case SIOCGIFBRDADDR:
  2462. case SIOCGIFDSTADDR:
  2463. case SIOCGIFNETMASK:
  2464. case SIOCGIFPFLAGS:
  2465. case SIOCGIFTXQLEN:
  2466. case SIOCGMIIPHY:
  2467. case SIOCGMIIREG:
  2468. if (copy_in_user(uifr32, uifr, sizeof(*uifr32)))
  2469. err = -EFAULT;
  2470. break;
  2471. }
  2472. }
  2473. return err;
  2474. }
  2475. static int compat_sioc_ifmap(struct net *net, unsigned int cmd,
  2476. struct compat_ifreq __user *uifr32)
  2477. {
  2478. struct ifreq ifr;
  2479. struct compat_ifmap __user *uifmap32;
  2480. mm_segment_t old_fs;
  2481. int err;
  2482. uifmap32 = &uifr32->ifr_ifru.ifru_map;
  2483. err = copy_from_user(&ifr, uifr32, sizeof(ifr.ifr_name));
  2484. err |= get_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
  2485. err |= get_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
  2486. err |= get_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
  2487. err |= get_user(ifr.ifr_map.irq, &uifmap32->irq);
  2488. err |= get_user(ifr.ifr_map.dma, &uifmap32->dma);
  2489. err |= get_user(ifr.ifr_map.port, &uifmap32->port);
  2490. if (err)
  2491. return -EFAULT;
  2492. old_fs = get_fs();
  2493. set_fs(KERNEL_DS);
  2494. err = dev_ioctl(net, cmd, (void __user __force *)&ifr);
  2495. set_fs(old_fs);
  2496. if (cmd == SIOCGIFMAP && !err) {
  2497. err = copy_to_user(uifr32, &ifr, sizeof(ifr.ifr_name));
  2498. err |= put_user(ifr.ifr_map.mem_start, &uifmap32->mem_start);
  2499. err |= put_user(ifr.ifr_map.mem_end, &uifmap32->mem_end);
  2500. err |= put_user(ifr.ifr_map.base_addr, &uifmap32->base_addr);
  2501. err |= put_user(ifr.ifr_map.irq, &uifmap32->irq);
  2502. err |= put_user(ifr.ifr_map.dma, &uifmap32->dma);
  2503. err |= put_user(ifr.ifr_map.port, &uifmap32->port);
  2504. if (err)
  2505. err = -EFAULT;
  2506. }
  2507. return err;
  2508. }
  2509. struct rtentry32 {
  2510. u32 rt_pad1;
  2511. struct sockaddr rt_dst; /* target address */
  2512. struct sockaddr rt_gateway; /* gateway addr (RTF_GATEWAY) */
  2513. struct sockaddr rt_genmask; /* target network mask (IP) */
  2514. unsigned short rt_flags;
  2515. short rt_pad2;
  2516. u32 rt_pad3;
  2517. unsigned char rt_tos;
  2518. unsigned char rt_class;
  2519. short rt_pad4;
  2520. short rt_metric; /* +1 for binary compatibility! */
  2521. /* char * */ u32 rt_dev; /* forcing the device at add */
  2522. u32 rt_mtu; /* per route MTU/Window */
  2523. u32 rt_window; /* Window clamping */
  2524. unsigned short rt_irtt; /* Initial RTT */
  2525. };
  2526. struct in6_rtmsg32 {
  2527. struct in6_addr rtmsg_dst;
  2528. struct in6_addr rtmsg_src;
  2529. struct in6_addr rtmsg_gateway;
  2530. u32 rtmsg_type;
  2531. u16 rtmsg_dst_len;
  2532. u16 rtmsg_src_len;
  2533. u32 rtmsg_metric;
  2534. u32 rtmsg_info;
  2535. u32 rtmsg_flags;
  2536. s32 rtmsg_ifindex;
  2537. };
  2538. static int routing_ioctl(struct net *net, struct socket *sock,
  2539. unsigned int cmd, void __user *argp)
  2540. {
  2541. int ret;
  2542. void *r = NULL;
  2543. struct in6_rtmsg r6;
  2544. struct rtentry r4;
  2545. char devname[16];
  2546. u32 rtdev;
  2547. mm_segment_t old_fs = get_fs();
  2548. if (sock && sock->sk && sock->sk->sk_family == AF_INET6) { /* ipv6 */
  2549. struct in6_rtmsg32 __user *ur6 = argp;
  2550. ret = copy_from_user(&r6.rtmsg_dst, &(ur6->rtmsg_dst),
  2551. 3 * sizeof(struct in6_addr));
  2552. ret |= get_user(r6.rtmsg_type, &(ur6->rtmsg_type));
  2553. ret |= get_user(r6.rtmsg_dst_len, &(ur6->rtmsg_dst_len));
  2554. ret |= get_user(r6.rtmsg_src_len, &(ur6->rtmsg_src_len));
  2555. ret |= get_user(r6.rtmsg_metric, &(ur6->rtmsg_metric));
  2556. ret |= get_user(r6.rtmsg_info, &(ur6->rtmsg_info));
  2557. ret |= get_user(r6.rtmsg_flags, &(ur6->rtmsg_flags));
  2558. ret |= get_user(r6.rtmsg_ifindex, &(ur6->rtmsg_ifindex));
  2559. r = (void *) &r6;
  2560. } else { /* ipv4 */
  2561. struct rtentry32 __user *ur4 = argp;
  2562. ret = copy_from_user(&r4.rt_dst, &(ur4->rt_dst),
  2563. 3 * sizeof(struct sockaddr));
  2564. ret |= get_user(r4.rt_flags, &(ur4->rt_flags));
  2565. ret |= get_user(r4.rt_metric, &(ur4->rt_metric));
  2566. ret |= get_user(r4.rt_mtu, &(ur4->rt_mtu));
  2567. ret |= get_user(r4.rt_window, &(ur4->rt_window));
  2568. ret |= get_user(r4.rt_irtt, &(ur4->rt_irtt));
  2569. ret |= get_user(rtdev, &(ur4->rt_dev));
  2570. if (rtdev) {
  2571. ret |= copy_from_user(devname, compat_ptr(rtdev), 15);
  2572. r4.rt_dev = (char __user __force *)devname;
  2573. devname[15] = 0;
  2574. } else
  2575. r4.rt_dev = NULL;
  2576. r = (void *) &r4;
  2577. }
  2578. if (ret) {
  2579. ret = -EFAULT;
  2580. goto out;
  2581. }
  2582. set_fs(KERNEL_DS);
  2583. ret = sock_do_ioctl(net, sock, cmd, (unsigned long) r);
  2584. set_fs(old_fs);
  2585. out:
  2586. return ret;
  2587. }
  2588. /* Since old style bridge ioctl's endup using SIOCDEVPRIVATE
  2589. * for some operations; this forces use of the newer bridge-utils that
  2590. * use compatible ioctls
  2591. */
  2592. static int old_bridge_ioctl(compat_ulong_t __user *argp)
  2593. {
  2594. compat_ulong_t tmp;
  2595. if (get_user(tmp, argp))
  2596. return -EFAULT;
  2597. if (tmp == BRCTL_GET_VERSION)
  2598. return BRCTL_VERSION + 1;
  2599. return -EINVAL;
  2600. }
  2601. static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
  2602. unsigned int cmd, unsigned long arg)
  2603. {
  2604. void __user *argp = compat_ptr(arg);
  2605. struct sock *sk = sock->sk;
  2606. struct net *net = sock_net(sk);
  2607. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
  2608. return compat_ifr_data_ioctl(net, cmd, argp);
  2609. switch (cmd) {
  2610. case SIOCSIFBR:
  2611. case SIOCGIFBR:
  2612. return old_bridge_ioctl(argp);
  2613. case SIOCGIFNAME:
  2614. return dev_ifname32(net, argp);
  2615. case SIOCGIFCONF:
  2616. return dev_ifconf(net, argp);
  2617. case SIOCETHTOOL:
  2618. return ethtool_ioctl(net, argp);
  2619. case SIOCWANDEV:
  2620. return compat_siocwandev(net, argp);
  2621. case SIOCGIFMAP:
  2622. case SIOCSIFMAP:
  2623. return compat_sioc_ifmap(net, cmd, argp);
  2624. case SIOCBONDENSLAVE:
  2625. case SIOCBONDRELEASE:
  2626. case SIOCBONDSETHWADDR:
  2627. case SIOCBONDCHANGEACTIVE:
  2628. return bond_ioctl(net, cmd, argp);
  2629. case SIOCADDRT:
  2630. case SIOCDELRT:
  2631. return routing_ioctl(net, sock, cmd, argp);
  2632. case SIOCGSTAMP:
  2633. return do_siocgstamp(net, sock, cmd, argp);
  2634. case SIOCGSTAMPNS:
  2635. return do_siocgstampns(net, sock, cmd, argp);
  2636. case SIOCBONDSLAVEINFOQUERY:
  2637. case SIOCBONDINFOQUERY:
  2638. case SIOCSHWTSTAMP:
  2639. case SIOCGHWTSTAMP:
  2640. return compat_ifr_data_ioctl(net, cmd, argp);
  2641. case FIOSETOWN:
  2642. case SIOCSPGRP:
  2643. case FIOGETOWN:
  2644. case SIOCGPGRP:
  2645. case SIOCBRADDBR:
  2646. case SIOCBRDELBR:
  2647. case SIOCGIFVLAN:
  2648. case SIOCSIFVLAN:
  2649. case SIOCADDDLCI:
  2650. case SIOCDELDLCI:
  2651. return sock_ioctl(file, cmd, arg);
  2652. case SIOCGIFFLAGS:
  2653. case SIOCSIFFLAGS:
  2654. case SIOCGIFMETRIC:
  2655. case SIOCSIFMETRIC:
  2656. case SIOCGIFMTU:
  2657. case SIOCSIFMTU:
  2658. case SIOCGIFMEM:
  2659. case SIOCSIFMEM:
  2660. case SIOCGIFHWADDR:
  2661. case SIOCSIFHWADDR:
  2662. case SIOCADDMULTI:
  2663. case SIOCDELMULTI:
  2664. case SIOCGIFINDEX:
  2665. case SIOCGIFADDR:
  2666. case SIOCSIFADDR:
  2667. case SIOCSIFHWBROADCAST:
  2668. case SIOCDIFADDR:
  2669. case SIOCGIFBRDADDR:
  2670. case SIOCSIFBRDADDR:
  2671. case SIOCGIFDSTADDR:
  2672. case SIOCSIFDSTADDR:
  2673. case SIOCGIFNETMASK:
  2674. case SIOCSIFNETMASK:
  2675. case SIOCSIFPFLAGS:
  2676. case SIOCGIFPFLAGS:
  2677. case SIOCGIFTXQLEN:
  2678. case SIOCSIFTXQLEN:
  2679. case SIOCBRADDIF:
  2680. case SIOCBRDELIF:
  2681. case SIOCSIFNAME:
  2682. case SIOCGMIIPHY:
  2683. case SIOCGMIIREG:
  2684. case SIOCSMIIREG:
  2685. return dev_ifsioc(net, sock, cmd, argp);
  2686. case SIOCSARP:
  2687. case SIOCGARP:
  2688. case SIOCDARP:
  2689. case SIOCATMARK:
  2690. return sock_do_ioctl(net, sock, cmd, arg);
  2691. }
  2692. return -ENOIOCTLCMD;
  2693. }
  2694. static long compat_sock_ioctl(struct file *file, unsigned int cmd,
  2695. unsigned long arg)
  2696. {
  2697. struct socket *sock = file->private_data;
  2698. int ret = -ENOIOCTLCMD;
  2699. struct sock *sk;
  2700. struct net *net;
  2701. sk = sock->sk;
  2702. net = sock_net(sk);
  2703. if (sock->ops->compat_ioctl)
  2704. ret = sock->ops->compat_ioctl(sock, cmd, arg);
  2705. if (ret == -ENOIOCTLCMD &&
  2706. (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
  2707. ret = compat_wext_handle_ioctl(net, cmd, arg);
  2708. if (ret == -ENOIOCTLCMD)
  2709. ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
  2710. return ret;
  2711. }
  2712. #endif
  2713. int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
  2714. {
  2715. return sock->ops->bind(sock, addr, addrlen);
  2716. }
  2717. EXPORT_SYMBOL(kernel_bind);
  2718. int kernel_listen(struct socket *sock, int backlog)
  2719. {
  2720. return sock->ops->listen(sock, backlog);
  2721. }
  2722. EXPORT_SYMBOL(kernel_listen);
  2723. int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
  2724. {
  2725. struct sock *sk = sock->sk;
  2726. int err;
  2727. err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
  2728. newsock);
  2729. if (err < 0)
  2730. goto done;
  2731. err = sock->ops->accept(sock, *newsock, flags);
  2732. if (err < 0) {
  2733. sock_release(*newsock);
  2734. *newsock = NULL;
  2735. goto done;
  2736. }
  2737. (*newsock)->ops = sock->ops;
  2738. __module_get((*newsock)->ops->owner);
  2739. done:
  2740. return err;
  2741. }
  2742. EXPORT_SYMBOL(kernel_accept);
  2743. int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
  2744. int flags)
  2745. {
  2746. return sock->ops->connect(sock, addr, addrlen, flags);
  2747. }
  2748. EXPORT_SYMBOL(kernel_connect);
  2749. int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
  2750. int *addrlen)
  2751. {
  2752. return sock->ops->getname(sock, addr, addrlen, 0);
  2753. }
  2754. EXPORT_SYMBOL(kernel_getsockname);
  2755. int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
  2756. int *addrlen)
  2757. {
  2758. return sock->ops->getname(sock, addr, addrlen, 1);
  2759. }
  2760. EXPORT_SYMBOL(kernel_getpeername);
  2761. int kernel_getsockopt(struct socket *sock, int level, int optname,
  2762. char *optval, int *optlen)
  2763. {
  2764. mm_segment_t oldfs = get_fs();
  2765. char __user *uoptval;
  2766. int __user *uoptlen;
  2767. int err;
  2768. uoptval = (char __user __force *) optval;
  2769. uoptlen = (int __user __force *) optlen;
  2770. set_fs(KERNEL_DS);
  2771. if (level == SOL_SOCKET)
  2772. err = sock_getsockopt(sock, level, optname, uoptval, uoptlen);
  2773. else
  2774. err = sock->ops->getsockopt(sock, level, optname, uoptval,
  2775. uoptlen);
  2776. set_fs(oldfs);
  2777. return err;
  2778. }
  2779. EXPORT_SYMBOL(kernel_getsockopt);
  2780. int kernel_setsockopt(struct socket *sock, int level, int optname,
  2781. char *optval, unsigned int optlen)
  2782. {
  2783. mm_segment_t oldfs = get_fs();
  2784. char __user *uoptval;
  2785. int err;
  2786. uoptval = (char __user __force *) optval;
  2787. set_fs(KERNEL_DS);
  2788. if (level == SOL_SOCKET)
  2789. err = sock_setsockopt(sock, level, optname, uoptval, optlen);
  2790. else
  2791. err = sock->ops->setsockopt(sock, level, optname, uoptval,
  2792. optlen);
  2793. set_fs(oldfs);
  2794. return err;
  2795. }
  2796. EXPORT_SYMBOL(kernel_setsockopt);
  2797. int kernel_sendpage(struct socket *sock, struct page *page, int offset,
  2798. size_t size, int flags)
  2799. {
  2800. if (sock->ops->sendpage)
  2801. return sock->ops->sendpage(sock, page, offset, size, flags);
  2802. return sock_no_sendpage(sock, page, offset, size, flags);
  2803. }
  2804. EXPORT_SYMBOL(kernel_sendpage);
  2805. int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
  2806. {
  2807. mm_segment_t oldfs = get_fs();
  2808. int err;
  2809. set_fs(KERNEL_DS);
  2810. err = sock->ops->ioctl(sock, cmd, arg);
  2811. set_fs(oldfs);
  2812. return err;
  2813. }
  2814. EXPORT_SYMBOL(kernel_sock_ioctl);
  2815. int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
  2816. {
  2817. return sock->ops->shutdown(sock, how);
  2818. }
  2819. EXPORT_SYMBOL(kernel_sock_shutdown);