socket.c 74 KB

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