socket.c 74 KB

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