socket.c 81 KB

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