socket.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475
  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/thread_info.h>
  72. #include <linux/wanrouter.h>
  73. #include <linux/if_bridge.h>
  74. #include <linux/if_frad.h>
  75. #include <linux/if_vlan.h>
  76. #include <linux/init.h>
  77. #include <linux/poll.h>
  78. #include <linux/cache.h>
  79. #include <linux/module.h>
  80. #include <linux/highmem.h>
  81. #include <linux/mount.h>
  82. #include <linux/security.h>
  83. #include <linux/syscalls.h>
  84. #include <linux/compat.h>
  85. #include <linux/kmod.h>
  86. #include <linux/audit.h>
  87. #include <linux/wireless.h>
  88. #include <linux/nsproxy.h>
  89. #include <asm/uaccess.h>
  90. #include <asm/unistd.h>
  91. #include <net/compat.h>
  92. #include <net/wext.h>
  93. #include <net/sock.h>
  94. #include <linux/netfilter.h>
  95. static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
  96. static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
  97. unsigned long nr_segs, loff_t pos);
  98. static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
  99. unsigned long nr_segs, loff_t pos);
  100. static int sock_mmap(struct file *file, struct vm_area_struct *vma);
  101. static int sock_close(struct inode *inode, struct file *file);
  102. static unsigned int sock_poll(struct file *file,
  103. struct poll_table_struct *wait);
  104. static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  105. #ifdef CONFIG_COMPAT
  106. static long compat_sock_ioctl(struct file *file,
  107. unsigned int cmd, unsigned long arg);
  108. #endif
  109. static int sock_fasync(int fd, struct file *filp, int on);
  110. static ssize_t sock_sendpage(struct file *file, struct page *page,
  111. int offset, size_t size, loff_t *ppos, int more);
  112. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  113. struct pipe_inode_info *pipe, size_t len,
  114. unsigned int flags);
  115. /*
  116. * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
  117. * in the operation structures but are done directly via the socketcall() multiplexor.
  118. */
  119. static const struct file_operations socket_file_ops = {
  120. .owner = THIS_MODULE,
  121. .llseek = no_llseek,
  122. .aio_read = sock_aio_read,
  123. .aio_write = sock_aio_write,
  124. .poll = sock_poll,
  125. .unlocked_ioctl = sock_ioctl,
  126. #ifdef CONFIG_COMPAT
  127. .compat_ioctl = compat_sock_ioctl,
  128. #endif
  129. .mmap = sock_mmap,
  130. .open = sock_no_open, /* special open code to disallow open via /proc */
  131. .release = sock_close,
  132. .fasync = sock_fasync,
  133. .sendpage = sock_sendpage,
  134. .splice_write = generic_splice_sendpage,
  135. .splice_read = sock_splice_read,
  136. };
  137. /*
  138. * The protocol list. Each protocol is registered in here.
  139. */
  140. static DEFINE_SPINLOCK(net_family_lock);
  141. static const struct net_proto_family *net_families[NPROTO] __read_mostly;
  142. /*
  143. * Statistics counters of the socket lists
  144. */
  145. static DEFINE_PER_CPU(int, sockets_in_use) = 0;
  146. /*
  147. * Support routines.
  148. * Move socket addresses back and forth across the kernel/user
  149. * divide and look after the messy bits.
  150. */
  151. #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
  152. 16 for IP, 16 for IPX,
  153. 24 for IPv6,
  154. about 80 for AX.25
  155. must be at least one bigger than
  156. the AF_UNIX size (see net/unix/af_unix.c
  157. :unix_mkname()).
  158. */
  159. /**
  160. * move_addr_to_kernel - copy a socket address into kernel space
  161. * @uaddr: Address in user space
  162. * @kaddr: Address in kernel space
  163. * @ulen: Length in user space
  164. *
  165. * The address is copied into kernel space. If the provided address is
  166. * too long an error code of -EINVAL is returned. If the copy gives
  167. * invalid addresses -EFAULT is returned. On a success 0 is returned.
  168. */
  169. int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr)
  170. {
  171. if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
  172. return -EINVAL;
  173. if (ulen == 0)
  174. return 0;
  175. if (copy_from_user(kaddr, uaddr, ulen))
  176. return -EFAULT;
  177. return audit_sockaddr(ulen, kaddr);
  178. }
  179. /**
  180. * move_addr_to_user - copy an address to user space
  181. * @kaddr: kernel space address
  182. * @klen: length of address in kernel
  183. * @uaddr: user space address
  184. * @ulen: pointer to user length field
  185. *
  186. * The value pointed to by ulen on entry is the buffer length available.
  187. * This is overwritten with the buffer space used. -EINVAL is returned
  188. * if an overlong buffer is specified or a negative buffer size. -EFAULT
  189. * is returned if either the buffer or the length field are not
  190. * accessible.
  191. * After copying the data up to the limit the user specifies, the true
  192. * length of the data is written over the length limit the user
  193. * specified. Zero is returned for a success.
  194. */
  195. int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr,
  196. int __user *ulen)
  197. {
  198. int err;
  199. int len;
  200. err = get_user(len, ulen);
  201. if (err)
  202. return err;
  203. if (len > klen)
  204. len = klen;
  205. if (len < 0 || len > sizeof(struct sockaddr_storage))
  206. return -EINVAL;
  207. if (len) {
  208. if (audit_sockaddr(klen, kaddr))
  209. return -ENOMEM;
  210. if (copy_to_user(uaddr, kaddr, len))
  211. return -EFAULT;
  212. }
  213. /*
  214. * "fromlen shall refer to the value before truncation.."
  215. * 1003.1g
  216. */
  217. return __put_user(klen, ulen);
  218. }
  219. #define SOCKFS_MAGIC 0x534F434B
  220. static struct kmem_cache *sock_inode_cachep __read_mostly;
  221. static struct inode *sock_alloc_inode(struct super_block *sb)
  222. {
  223. struct socket_alloc *ei;
  224. ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
  225. if (!ei)
  226. return NULL;
  227. init_waitqueue_head(&ei->socket.wait);
  228. ei->socket.fasync_list = NULL;
  229. ei->socket.state = SS_UNCONNECTED;
  230. ei->socket.flags = 0;
  231. ei->socket.ops = NULL;
  232. ei->socket.sk = NULL;
  233. ei->socket.file = NULL;
  234. return &ei->vfs_inode;
  235. }
  236. static void sock_destroy_inode(struct inode *inode)
  237. {
  238. kmem_cache_free(sock_inode_cachep,
  239. container_of(inode, struct socket_alloc, vfs_inode));
  240. }
  241. static void init_once(void *foo)
  242. {
  243. struct socket_alloc *ei = (struct socket_alloc *)foo;
  244. inode_init_once(&ei->vfs_inode);
  245. }
  246. static int init_inodecache(void)
  247. {
  248. sock_inode_cachep = kmem_cache_create("sock_inode_cache",
  249. sizeof(struct socket_alloc),
  250. 0,
  251. (SLAB_HWCACHE_ALIGN |
  252. SLAB_RECLAIM_ACCOUNT |
  253. SLAB_MEM_SPREAD),
  254. init_once);
  255. if (sock_inode_cachep == NULL)
  256. return -ENOMEM;
  257. return 0;
  258. }
  259. static struct super_operations sockfs_ops = {
  260. .alloc_inode = sock_alloc_inode,
  261. .destroy_inode =sock_destroy_inode,
  262. .statfs = simple_statfs,
  263. };
  264. static int sockfs_get_sb(struct file_system_type *fs_type,
  265. int flags, const char *dev_name, void *data,
  266. struct vfsmount *mnt)
  267. {
  268. return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
  269. mnt);
  270. }
  271. static struct vfsmount *sock_mnt __read_mostly;
  272. static struct file_system_type sock_fs_type = {
  273. .name = "sockfs",
  274. .get_sb = sockfs_get_sb,
  275. .kill_sb = kill_anon_super,
  276. };
  277. static int sockfs_delete_dentry(struct dentry *dentry)
  278. {
  279. /*
  280. * At creation time, we pretended this dentry was hashed
  281. * (by clearing DCACHE_UNHASHED bit in d_flags)
  282. * At delete time, we restore the truth : not hashed.
  283. * (so that dput() can proceed correctly)
  284. */
  285. dentry->d_flags |= DCACHE_UNHASHED;
  286. return 0;
  287. }
  288. /*
  289. * sockfs_dname() is called from d_path().
  290. */
  291. static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
  292. {
  293. return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
  294. dentry->d_inode->i_ino);
  295. }
  296. static struct dentry_operations sockfs_dentry_operations = {
  297. .d_delete = sockfs_delete_dentry,
  298. .d_dname = sockfs_dname,
  299. };
  300. /*
  301. * Obtains the first available file descriptor and sets it up for use.
  302. *
  303. * These functions create file structures and maps them to fd space
  304. * of the current process. On success it returns file descriptor
  305. * and file struct implicitly stored in sock->file.
  306. * Note that another thread may close file descriptor before we return
  307. * from this function. We use the fact that now we do not refer
  308. * to socket after mapping. If one day we will need it, this
  309. * function will increment ref. count on file by 1.
  310. *
  311. * In any case returned fd MAY BE not valid!
  312. * This race condition is unavoidable
  313. * with shared fd spaces, we cannot solve it inside kernel,
  314. * but we take care of internal coherence yet.
  315. */
  316. static int sock_alloc_fd(struct file **filep, int flags)
  317. {
  318. int fd;
  319. fd = get_unused_fd_flags(flags);
  320. if (likely(fd >= 0)) {
  321. struct file *file = get_empty_filp();
  322. *filep = file;
  323. if (unlikely(!file)) {
  324. put_unused_fd(fd);
  325. return -ENFILE;
  326. }
  327. } else
  328. *filep = NULL;
  329. return fd;
  330. }
  331. static int sock_attach_fd(struct socket *sock, struct file *file, int flags)
  332. {
  333. struct dentry *dentry;
  334. struct qstr name = { .name = "" };
  335. dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);
  336. if (unlikely(!dentry))
  337. return -ENOMEM;
  338. dentry->d_op = &sockfs_dentry_operations;
  339. /*
  340. * We dont want to push this dentry into global dentry hash table.
  341. * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
  342. * This permits a working /proc/$pid/fd/XXX on sockets
  343. */
  344. dentry->d_flags &= ~DCACHE_UNHASHED;
  345. d_instantiate(dentry, SOCK_INODE(sock));
  346. sock->file = file;
  347. init_file(file, sock_mnt, dentry, FMODE_READ | FMODE_WRITE,
  348. &socket_file_ops);
  349. SOCK_INODE(sock)->i_fop = &socket_file_ops;
  350. file->f_flags = O_RDWR | (flags & O_NONBLOCK);
  351. file->f_pos = 0;
  352. file->private_data = sock;
  353. return 0;
  354. }
  355. int sock_map_fd(struct socket *sock, int flags)
  356. {
  357. struct file *newfile;
  358. int fd = sock_alloc_fd(&newfile, flags);
  359. if (likely(fd >= 0)) {
  360. int err = sock_attach_fd(sock, newfile, flags);
  361. if (unlikely(err < 0)) {
  362. put_filp(newfile);
  363. put_unused_fd(fd);
  364. return err;
  365. }
  366. fd_install(fd, newfile);
  367. }
  368. return fd;
  369. }
  370. static struct socket *sock_from_file(struct file *file, int *err)
  371. {
  372. if (file->f_op == &socket_file_ops)
  373. return file->private_data; /* set in sock_map_fd */
  374. *err = -ENOTSOCK;
  375. return NULL;
  376. }
  377. /**
  378. * sockfd_lookup - Go from a file number to its socket slot
  379. * @fd: file handle
  380. * @err: pointer to an error code return
  381. *
  382. * The file handle passed in is locked and the socket it is bound
  383. * too is returned. If an error occurs the err pointer is overwritten
  384. * with a negative errno code and NULL is returned. The function checks
  385. * for both invalid handles and passing a handle which is not a socket.
  386. *
  387. * On a success the socket object pointer is returned.
  388. */
  389. struct socket *sockfd_lookup(int fd, int *err)
  390. {
  391. struct file *file;
  392. struct socket *sock;
  393. file = fget(fd);
  394. if (!file) {
  395. *err = -EBADF;
  396. return NULL;
  397. }
  398. sock = sock_from_file(file, err);
  399. if (!sock)
  400. fput(file);
  401. return sock;
  402. }
  403. static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
  404. {
  405. struct file *file;
  406. struct socket *sock;
  407. *err = -EBADF;
  408. file = fget_light(fd, fput_needed);
  409. if (file) {
  410. sock = sock_from_file(file, err);
  411. if (sock)
  412. return sock;
  413. fput_light(file, *fput_needed);
  414. }
  415. return NULL;
  416. }
  417. /**
  418. * sock_alloc - allocate a socket
  419. *
  420. * Allocate a new inode and socket object. The two are bound together
  421. * and initialised. The socket is then returned. If we are out of inodes
  422. * NULL is returned.
  423. */
  424. static struct socket *sock_alloc(void)
  425. {
  426. struct inode *inode;
  427. struct socket *sock;
  428. inode = new_inode(sock_mnt->mnt_sb);
  429. if (!inode)
  430. return NULL;
  431. sock = SOCKET_I(inode);
  432. inode->i_mode = S_IFSOCK | S_IRWXUGO;
  433. inode->i_uid = current->fsuid;
  434. inode->i_gid = current->fsgid;
  435. get_cpu_var(sockets_in_use)++;
  436. put_cpu_var(sockets_in_use);
  437. return sock;
  438. }
  439. /*
  440. * In theory you can't get an open on this inode, but /proc provides
  441. * a back door. Remember to keep it shut otherwise you'll let the
  442. * creepy crawlies in.
  443. */
  444. static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
  445. {
  446. return -ENXIO;
  447. }
  448. const struct file_operations bad_sock_fops = {
  449. .owner = THIS_MODULE,
  450. .open = sock_no_open,
  451. };
  452. /**
  453. * sock_release - close a socket
  454. * @sock: socket to close
  455. *
  456. * The socket is released from the protocol stack if it has a release
  457. * callback, and the inode is then released if the socket is bound to
  458. * an inode not a file.
  459. */
  460. void sock_release(struct socket *sock)
  461. {
  462. if (sock->ops) {
  463. struct module *owner = sock->ops->owner;
  464. sock->ops->release(sock);
  465. sock->ops = NULL;
  466. module_put(owner);
  467. }
  468. if (sock->fasync_list)
  469. printk(KERN_ERR "sock_release: fasync list not empty!\n");
  470. get_cpu_var(sockets_in_use)--;
  471. put_cpu_var(sockets_in_use);
  472. if (!sock->file) {
  473. iput(SOCK_INODE(sock));
  474. return;
  475. }
  476. sock->file = NULL;
  477. }
  478. static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
  479. struct msghdr *msg, size_t size)
  480. {
  481. struct sock_iocb *si = kiocb_to_siocb(iocb);
  482. int err;
  483. si->sock = sock;
  484. si->scm = NULL;
  485. si->msg = msg;
  486. si->size = size;
  487. err = security_socket_sendmsg(sock, msg, size);
  488. if (err)
  489. return err;
  490. return sock->ops->sendmsg(iocb, sock, msg, size);
  491. }
  492. int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  493. {
  494. struct kiocb iocb;
  495. struct sock_iocb siocb;
  496. int ret;
  497. init_sync_kiocb(&iocb, NULL);
  498. iocb.private = &siocb;
  499. ret = __sock_sendmsg(&iocb, sock, msg, size);
  500. if (-EIOCBQUEUED == ret)
  501. ret = wait_on_sync_kiocb(&iocb);
  502. return ret;
  503. }
  504. int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
  505. struct kvec *vec, size_t num, size_t size)
  506. {
  507. mm_segment_t oldfs = get_fs();
  508. int result;
  509. set_fs(KERNEL_DS);
  510. /*
  511. * the following is safe, since for compiler definitions of kvec and
  512. * iovec are identical, yielding the same in-core layout and alignment
  513. */
  514. msg->msg_iov = (struct iovec *)vec;
  515. msg->msg_iovlen = num;
  516. result = sock_sendmsg(sock, msg, size);
  517. set_fs(oldfs);
  518. return result;
  519. }
  520. /*
  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. ktime_t kt = skb->tstamp;
  527. if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
  528. struct timeval tv;
  529. /* Race occurred between timestamp enabling and packet
  530. receiving. Fill in the current time for now. */
  531. if (kt.tv64 == 0)
  532. kt = ktime_get_real();
  533. skb->tstamp = kt;
  534. tv = ktime_to_timeval(kt);
  535. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP, sizeof(tv), &tv);
  536. } else {
  537. struct timespec ts;
  538. /* Race occurred between timestamp enabling and packet
  539. receiving. Fill in the current time for now. */
  540. if (kt.tv64 == 0)
  541. kt = ktime_get_real();
  542. skb->tstamp = kt;
  543. ts = ktime_to_timespec(kt);
  544. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS, sizeof(ts), &ts);
  545. }
  546. }
  547. EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
  548. static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
  549. struct msghdr *msg, size_t size, int flags)
  550. {
  551. int err;
  552. struct sock_iocb *si = kiocb_to_siocb(iocb);
  553. si->sock = sock;
  554. si->scm = NULL;
  555. si->msg = msg;
  556. si->size = size;
  557. si->flags = flags;
  558. err = security_socket_recvmsg(sock, msg, size, flags);
  559. if (err)
  560. return err;
  561. return sock->ops->recvmsg(iocb, sock, msg, size, flags);
  562. }
  563. int sock_recvmsg(struct socket *sock, struct msghdr *msg,
  564. size_t size, int flags)
  565. {
  566. struct kiocb iocb;
  567. struct sock_iocb siocb;
  568. int ret;
  569. init_sync_kiocb(&iocb, NULL);
  570. iocb.private = &siocb;
  571. ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
  572. if (-EIOCBQUEUED == ret)
  573. ret = wait_on_sync_kiocb(&iocb);
  574. return ret;
  575. }
  576. int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
  577. struct kvec *vec, size_t num, size_t size, int flags)
  578. {
  579. mm_segment_t oldfs = get_fs();
  580. int result;
  581. set_fs(KERNEL_DS);
  582. /*
  583. * the following is safe, since for compiler definitions of kvec and
  584. * iovec are identical, yielding the same in-core layout and alignment
  585. */
  586. msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
  587. result = sock_recvmsg(sock, msg, size, flags);
  588. set_fs(oldfs);
  589. return result;
  590. }
  591. static void sock_aio_dtor(struct kiocb *iocb)
  592. {
  593. kfree(iocb->private);
  594. }
  595. static ssize_t sock_sendpage(struct file *file, struct page *page,
  596. int offset, size_t size, loff_t *ppos, int more)
  597. {
  598. struct socket *sock;
  599. int flags;
  600. sock = file->private_data;
  601. flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
  602. if (more)
  603. flags |= MSG_MORE;
  604. return sock->ops->sendpage(sock, page, offset, size, flags);
  605. }
  606. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  607. struct pipe_inode_info *pipe, size_t len,
  608. unsigned int flags)
  609. {
  610. struct socket *sock = file->private_data;
  611. if (unlikely(!sock->ops->splice_read))
  612. return -EINVAL;
  613. return sock->ops->splice_read(sock, ppos, pipe, len, flags);
  614. }
  615. static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
  616. struct sock_iocb *siocb)
  617. {
  618. if (!is_sync_kiocb(iocb)) {
  619. siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
  620. if (!siocb)
  621. return NULL;
  622. iocb->ki_dtor = sock_aio_dtor;
  623. }
  624. siocb->kiocb = iocb;
  625. iocb->private = siocb;
  626. return siocb;
  627. }
  628. static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
  629. struct file *file, const struct iovec *iov,
  630. unsigned long nr_segs)
  631. {
  632. struct socket *sock = file->private_data;
  633. size_t size = 0;
  634. int i;
  635. for (i = 0; i < nr_segs; i++)
  636. size += iov[i].iov_len;
  637. msg->msg_name = NULL;
  638. msg->msg_namelen = 0;
  639. msg->msg_control = NULL;
  640. msg->msg_controllen = 0;
  641. msg->msg_iov = (struct iovec *)iov;
  642. msg->msg_iovlen = nr_segs;
  643. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  644. return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
  645. }
  646. static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
  647. unsigned long nr_segs, loff_t pos)
  648. {
  649. struct sock_iocb siocb, *x;
  650. if (pos != 0)
  651. return -ESPIPE;
  652. if (iocb->ki_left == 0) /* Match SYS5 behaviour */
  653. return 0;
  654. x = alloc_sock_iocb(iocb, &siocb);
  655. if (!x)
  656. return -ENOMEM;
  657. return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  658. }
  659. static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
  660. struct file *file, const struct iovec *iov,
  661. unsigned long nr_segs)
  662. {
  663. struct socket *sock = file->private_data;
  664. size_t size = 0;
  665. int i;
  666. for (i = 0; i < nr_segs; i++)
  667. size += iov[i].iov_len;
  668. msg->msg_name = NULL;
  669. msg->msg_namelen = 0;
  670. msg->msg_control = NULL;
  671. msg->msg_controllen = 0;
  672. msg->msg_iov = (struct iovec *)iov;
  673. msg->msg_iovlen = nr_segs;
  674. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  675. if (sock->type == SOCK_SEQPACKET)
  676. msg->msg_flags |= MSG_EOR;
  677. return __sock_sendmsg(iocb, sock, msg, size);
  678. }
  679. static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
  680. unsigned long nr_segs, loff_t pos)
  681. {
  682. struct sock_iocb siocb, *x;
  683. if (pos != 0)
  684. return -ESPIPE;
  685. x = alloc_sock_iocb(iocb, &siocb);
  686. if (!x)
  687. return -ENOMEM;
  688. return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  689. }
  690. /*
  691. * Atomic setting of ioctl hooks to avoid race
  692. * with module unload.
  693. */
  694. static DEFINE_MUTEX(br_ioctl_mutex);
  695. static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg) = NULL;
  696. void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
  697. {
  698. mutex_lock(&br_ioctl_mutex);
  699. br_ioctl_hook = hook;
  700. mutex_unlock(&br_ioctl_mutex);
  701. }
  702. EXPORT_SYMBOL(brioctl_set);
  703. static DEFINE_MUTEX(vlan_ioctl_mutex);
  704. static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
  705. void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
  706. {
  707. mutex_lock(&vlan_ioctl_mutex);
  708. vlan_ioctl_hook = hook;
  709. mutex_unlock(&vlan_ioctl_mutex);
  710. }
  711. EXPORT_SYMBOL(vlan_ioctl_set);
  712. static DEFINE_MUTEX(dlci_ioctl_mutex);
  713. static int (*dlci_ioctl_hook) (unsigned int, void __user *);
  714. void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
  715. {
  716. mutex_lock(&dlci_ioctl_mutex);
  717. dlci_ioctl_hook = hook;
  718. mutex_unlock(&dlci_ioctl_mutex);
  719. }
  720. EXPORT_SYMBOL(dlci_ioctl_set);
  721. /*
  722. * With an ioctl, arg may well be a user mode pointer, but we don't know
  723. * what to do with it - that's up to the protocol still.
  724. */
  725. static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  726. {
  727. struct socket *sock;
  728. struct sock *sk;
  729. void __user *argp = (void __user *)arg;
  730. int pid, err;
  731. struct net *net;
  732. sock = file->private_data;
  733. sk = sock->sk;
  734. net = sock_net(sk);
  735. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
  736. err = dev_ioctl(net, cmd, argp);
  737. } else
  738. #ifdef CONFIG_WIRELESS_EXT
  739. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
  740. err = dev_ioctl(net, cmd, argp);
  741. } else
  742. #endif /* CONFIG_WIRELESS_EXT */
  743. switch (cmd) {
  744. case FIOSETOWN:
  745. case SIOCSPGRP:
  746. err = -EFAULT;
  747. if (get_user(pid, (int __user *)argp))
  748. break;
  749. err = f_setown(sock->file, pid, 1);
  750. break;
  751. case FIOGETOWN:
  752. case SIOCGPGRP:
  753. err = put_user(f_getown(sock->file),
  754. (int __user *)argp);
  755. break;
  756. case SIOCGIFBR:
  757. case SIOCSIFBR:
  758. case SIOCBRADDBR:
  759. case SIOCBRDELBR:
  760. err = -ENOPKG;
  761. if (!br_ioctl_hook)
  762. request_module("bridge");
  763. mutex_lock(&br_ioctl_mutex);
  764. if (br_ioctl_hook)
  765. err = br_ioctl_hook(net, cmd, argp);
  766. mutex_unlock(&br_ioctl_mutex);
  767. break;
  768. case SIOCGIFVLAN:
  769. case SIOCSIFVLAN:
  770. err = -ENOPKG;
  771. if (!vlan_ioctl_hook)
  772. request_module("8021q");
  773. mutex_lock(&vlan_ioctl_mutex);
  774. if (vlan_ioctl_hook)
  775. err = vlan_ioctl_hook(net, argp);
  776. mutex_unlock(&vlan_ioctl_mutex);
  777. break;
  778. case SIOCADDDLCI:
  779. case SIOCDELDLCI:
  780. err = -ENOPKG;
  781. if (!dlci_ioctl_hook)
  782. request_module("dlci");
  783. mutex_lock(&dlci_ioctl_mutex);
  784. if (dlci_ioctl_hook)
  785. err = dlci_ioctl_hook(cmd, argp);
  786. mutex_unlock(&dlci_ioctl_mutex);
  787. break;
  788. default:
  789. err = sock->ops->ioctl(sock, cmd, arg);
  790. /*
  791. * If this ioctl is unknown try to hand it down
  792. * to the NIC driver.
  793. */
  794. if (err == -ENOIOCTLCMD)
  795. err = dev_ioctl(net, cmd, argp);
  796. break;
  797. }
  798. return err;
  799. }
  800. int sock_create_lite(int family, int type, int protocol, struct socket **res)
  801. {
  802. int err;
  803. struct socket *sock = NULL;
  804. err = security_socket_create(family, type, protocol, 1);
  805. if (err)
  806. goto out;
  807. sock = sock_alloc();
  808. if (!sock) {
  809. err = -ENOMEM;
  810. goto out;
  811. }
  812. sock->type = type;
  813. err = security_socket_post_create(sock, family, type, protocol, 1);
  814. if (err)
  815. goto out_release;
  816. out:
  817. *res = sock;
  818. return err;
  819. out_release:
  820. sock_release(sock);
  821. sock = NULL;
  822. goto out;
  823. }
  824. /* No kernel lock held - perfect */
  825. static unsigned int sock_poll(struct file *file, poll_table *wait)
  826. {
  827. struct socket *sock;
  828. /*
  829. * We can't return errors to poll, so it's either yes or no.
  830. */
  831. sock = file->private_data;
  832. return sock->ops->poll(file, sock, wait);
  833. }
  834. static int sock_mmap(struct file *file, struct vm_area_struct *vma)
  835. {
  836. struct socket *sock = file->private_data;
  837. return sock->ops->mmap(file, sock, vma);
  838. }
  839. static int sock_close(struct inode *inode, struct file *filp)
  840. {
  841. /*
  842. * It was possible the inode is NULL we were
  843. * closing an unfinished socket.
  844. */
  845. if (!inode) {
  846. printk(KERN_DEBUG "sock_close: NULL inode\n");
  847. return 0;
  848. }
  849. sock_fasync(-1, filp, 0);
  850. sock_release(SOCKET_I(inode));
  851. return 0;
  852. }
  853. /*
  854. * Update the socket async list
  855. *
  856. * Fasync_list locking strategy.
  857. *
  858. * 1. fasync_list is modified only under process context socket lock
  859. * i.e. under semaphore.
  860. * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
  861. * or under socket lock.
  862. * 3. fasync_list can be used from softirq context, so that
  863. * modification under socket lock have to be enhanced with
  864. * write_lock_bh(&sk->sk_callback_lock).
  865. * --ANK (990710)
  866. */
  867. static int sock_fasync(int fd, struct file *filp, int on)
  868. {
  869. struct fasync_struct *fa, *fna = NULL, **prev;
  870. struct socket *sock;
  871. struct sock *sk;
  872. if (on) {
  873. fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
  874. if (fna == NULL)
  875. return -ENOMEM;
  876. }
  877. sock = filp->private_data;
  878. sk = sock->sk;
  879. if (sk == NULL) {
  880. kfree(fna);
  881. return -EINVAL;
  882. }
  883. lock_sock(sk);
  884. prev = &(sock->fasync_list);
  885. for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
  886. if (fa->fa_file == filp)
  887. break;
  888. if (on) {
  889. if (fa != NULL) {
  890. write_lock_bh(&sk->sk_callback_lock);
  891. fa->fa_fd = fd;
  892. write_unlock_bh(&sk->sk_callback_lock);
  893. kfree(fna);
  894. goto out;
  895. }
  896. fna->fa_file = filp;
  897. fna->fa_fd = fd;
  898. fna->magic = FASYNC_MAGIC;
  899. fna->fa_next = sock->fasync_list;
  900. write_lock_bh(&sk->sk_callback_lock);
  901. sock->fasync_list = fna;
  902. write_unlock_bh(&sk->sk_callback_lock);
  903. } else {
  904. if (fa != NULL) {
  905. write_lock_bh(&sk->sk_callback_lock);
  906. *prev = fa->fa_next;
  907. write_unlock_bh(&sk->sk_callback_lock);
  908. kfree(fa);
  909. }
  910. }
  911. out:
  912. release_sock(sock->sk);
  913. return 0;
  914. }
  915. /* This function may be called only under socket lock or callback_lock */
  916. int sock_wake_async(struct socket *sock, int how, int band)
  917. {
  918. if (!sock || !sock->fasync_list)
  919. return -1;
  920. switch (how) {
  921. case SOCK_WAKE_WAITD:
  922. if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
  923. break;
  924. goto call_kill;
  925. case SOCK_WAKE_SPACE:
  926. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
  927. break;
  928. /* fall through */
  929. case SOCK_WAKE_IO:
  930. call_kill:
  931. __kill_fasync(sock->fasync_list, SIGIO, band);
  932. break;
  933. case SOCK_WAKE_URG:
  934. __kill_fasync(sock->fasync_list, SIGURG, band);
  935. }
  936. return 0;
  937. }
  938. static int __sock_create(struct net *net, int family, int type, int protocol,
  939. struct socket **res, int kern)
  940. {
  941. int err;
  942. struct socket *sock;
  943. const struct net_proto_family *pf;
  944. /*
  945. * Check protocol is in range
  946. */
  947. if (family < 0 || family >= NPROTO)
  948. return -EAFNOSUPPORT;
  949. if (type < 0 || type >= SOCK_MAX)
  950. return -EINVAL;
  951. /* Compatibility.
  952. This uglymoron is moved from INET layer to here to avoid
  953. deadlock in module load.
  954. */
  955. if (family == PF_INET && type == SOCK_PACKET) {
  956. static int warned;
  957. if (!warned) {
  958. warned = 1;
  959. printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
  960. current->comm);
  961. }
  962. family = PF_PACKET;
  963. }
  964. err = security_socket_create(family, type, protocol, kern);
  965. if (err)
  966. return err;
  967. /*
  968. * Allocate the socket and allow the family to set things up. if
  969. * the protocol is 0, the family is instructed to select an appropriate
  970. * default.
  971. */
  972. sock = sock_alloc();
  973. if (!sock) {
  974. if (net_ratelimit())
  975. printk(KERN_WARNING "socket: no more sockets\n");
  976. return -ENFILE; /* Not exactly a match, but its the
  977. closest posix thing */
  978. }
  979. sock->type = type;
  980. #if defined(CONFIG_KMOD)
  981. /* Attempt to load a protocol module if the find failed.
  982. *
  983. * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
  984. * requested real, full-featured networking support upon configuration.
  985. * Otherwise module support will break!
  986. */
  987. if (net_families[family] == NULL)
  988. request_module("net-pf-%d", family);
  989. #endif
  990. rcu_read_lock();
  991. pf = rcu_dereference(net_families[family]);
  992. err = -EAFNOSUPPORT;
  993. if (!pf)
  994. goto out_release;
  995. /*
  996. * We will call the ->create function, that possibly is in a loadable
  997. * module, so we have to bump that loadable module refcnt first.
  998. */
  999. if (!try_module_get(pf->owner))
  1000. goto out_release;
  1001. /* Now protected by module ref count */
  1002. rcu_read_unlock();
  1003. err = pf->create(net, sock, protocol);
  1004. if (err < 0)
  1005. goto out_module_put;
  1006. /*
  1007. * Now to bump the refcnt of the [loadable] module that owns this
  1008. * socket at sock_release time we decrement its refcnt.
  1009. */
  1010. if (!try_module_get(sock->ops->owner))
  1011. goto out_module_busy;
  1012. /*
  1013. * Now that we're done with the ->create function, the [loadable]
  1014. * module can have its refcnt decremented
  1015. */
  1016. module_put(pf->owner);
  1017. err = security_socket_post_create(sock, family, type, protocol, kern);
  1018. if (err)
  1019. goto out_sock_release;
  1020. *res = sock;
  1021. return 0;
  1022. out_module_busy:
  1023. err = -EAFNOSUPPORT;
  1024. out_module_put:
  1025. sock->ops = NULL;
  1026. module_put(pf->owner);
  1027. out_sock_release:
  1028. sock_release(sock);
  1029. return err;
  1030. out_release:
  1031. rcu_read_unlock();
  1032. goto out_sock_release;
  1033. }
  1034. int sock_create(int family, int type, int protocol, struct socket **res)
  1035. {
  1036. return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
  1037. }
  1038. int sock_create_kern(int family, int type, int protocol, struct socket **res)
  1039. {
  1040. return __sock_create(&init_net, family, type, protocol, res, 1);
  1041. }
  1042. asmlinkage long sys_socket(int family, int type, int protocol)
  1043. {
  1044. int retval;
  1045. struct socket *sock;
  1046. int flags;
  1047. /* Check the SOCK_* constants for consistency. */
  1048. BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
  1049. BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
  1050. BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
  1051. BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
  1052. flags = type & ~SOCK_TYPE_MASK;
  1053. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1054. return -EINVAL;
  1055. type &= SOCK_TYPE_MASK;
  1056. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1057. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1058. retval = sock_create(family, type, protocol, &sock);
  1059. if (retval < 0)
  1060. goto out;
  1061. retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
  1062. if (retval < 0)
  1063. goto out_release;
  1064. out:
  1065. /* It may be already another descriptor 8) Not kernel problem. */
  1066. return retval;
  1067. out_release:
  1068. sock_release(sock);
  1069. return retval;
  1070. }
  1071. /*
  1072. * Create a pair of connected sockets.
  1073. */
  1074. asmlinkage long sys_socketpair(int family, int type, int protocol,
  1075. int __user *usockvec)
  1076. {
  1077. struct socket *sock1, *sock2;
  1078. int fd1, fd2, err;
  1079. struct file *newfile1, *newfile2;
  1080. int flags;
  1081. flags = type & ~SOCK_TYPE_MASK;
  1082. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1083. return -EINVAL;
  1084. type &= SOCK_TYPE_MASK;
  1085. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1086. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1087. /*
  1088. * Obtain the first socket and check if the underlying protocol
  1089. * supports the socketpair call.
  1090. */
  1091. err = sock_create(family, type, protocol, &sock1);
  1092. if (err < 0)
  1093. goto out;
  1094. err = sock_create(family, type, protocol, &sock2);
  1095. if (err < 0)
  1096. goto out_release_1;
  1097. err = sock1->ops->socketpair(sock1, sock2);
  1098. if (err < 0)
  1099. goto out_release_both;
  1100. fd1 = sock_alloc_fd(&newfile1, flags & O_CLOEXEC);
  1101. if (unlikely(fd1 < 0)) {
  1102. err = fd1;
  1103. goto out_release_both;
  1104. }
  1105. fd2 = sock_alloc_fd(&newfile2, flags & O_CLOEXEC);
  1106. if (unlikely(fd2 < 0)) {
  1107. err = fd2;
  1108. put_filp(newfile1);
  1109. put_unused_fd(fd1);
  1110. goto out_release_both;
  1111. }
  1112. err = sock_attach_fd(sock1, newfile1, flags & O_NONBLOCK);
  1113. if (unlikely(err < 0)) {
  1114. goto out_fd2;
  1115. }
  1116. err = sock_attach_fd(sock2, newfile2, flags & O_NONBLOCK);
  1117. if (unlikely(err < 0)) {
  1118. fput(newfile1);
  1119. goto out_fd1;
  1120. }
  1121. err = audit_fd_pair(fd1, fd2);
  1122. if (err < 0) {
  1123. fput(newfile1);
  1124. fput(newfile2);
  1125. goto out_fd;
  1126. }
  1127. fd_install(fd1, newfile1);
  1128. fd_install(fd2, newfile2);
  1129. /* fd1 and fd2 may be already another descriptors.
  1130. * Not kernel problem.
  1131. */
  1132. err = put_user(fd1, &usockvec[0]);
  1133. if (!err)
  1134. err = put_user(fd2, &usockvec[1]);
  1135. if (!err)
  1136. return 0;
  1137. sys_close(fd2);
  1138. sys_close(fd1);
  1139. return err;
  1140. out_release_both:
  1141. sock_release(sock2);
  1142. out_release_1:
  1143. sock_release(sock1);
  1144. out:
  1145. return err;
  1146. out_fd2:
  1147. put_filp(newfile1);
  1148. sock_release(sock1);
  1149. out_fd1:
  1150. put_filp(newfile2);
  1151. sock_release(sock2);
  1152. out_fd:
  1153. put_unused_fd(fd1);
  1154. put_unused_fd(fd2);
  1155. goto out;
  1156. }
  1157. /*
  1158. * Bind a name to a socket. Nothing much to do here since it's
  1159. * the protocol's responsibility to handle the local address.
  1160. *
  1161. * We move the socket address to kernel space before we call
  1162. * the protocol layer (having also checked the address is ok).
  1163. */
  1164. asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
  1165. {
  1166. struct socket *sock;
  1167. struct sockaddr_storage address;
  1168. int err, fput_needed;
  1169. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1170. if (sock) {
  1171. err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
  1172. if (err >= 0) {
  1173. err = security_socket_bind(sock,
  1174. (struct sockaddr *)&address,
  1175. addrlen);
  1176. if (!err)
  1177. err = sock->ops->bind(sock,
  1178. (struct sockaddr *)
  1179. &address, addrlen);
  1180. }
  1181. fput_light(sock->file, fput_needed);
  1182. }
  1183. return err;
  1184. }
  1185. /*
  1186. * Perform a listen. Basically, we allow the protocol to do anything
  1187. * necessary for a listen, and if that works, we mark the socket as
  1188. * ready for listening.
  1189. */
  1190. asmlinkage long sys_listen(int fd, int backlog)
  1191. {
  1192. struct socket *sock;
  1193. int err, fput_needed;
  1194. int somaxconn;
  1195. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1196. if (sock) {
  1197. somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
  1198. if ((unsigned)backlog > somaxconn)
  1199. backlog = somaxconn;
  1200. err = security_socket_listen(sock, backlog);
  1201. if (!err)
  1202. err = sock->ops->listen(sock, backlog);
  1203. fput_light(sock->file, fput_needed);
  1204. }
  1205. return err;
  1206. }
  1207. /*
  1208. * For accept, we attempt to create a new socket, set up the link
  1209. * with the client, wake up the client, then return the new
  1210. * connected fd. We collect the address of the connector in kernel
  1211. * space and move it to user at the very end. This is unclean because
  1212. * we open the socket then return an error.
  1213. *
  1214. * 1003.1g adds the ability to recvmsg() to query connection pending
  1215. * status to recvmsg. We need to add that support in a way thats
  1216. * clean when we restucture accept also.
  1217. */
  1218. long do_accept(int fd, struct sockaddr __user *upeer_sockaddr,
  1219. int __user *upeer_addrlen, int flags)
  1220. {
  1221. struct socket *sock, *newsock;
  1222. struct file *newfile;
  1223. int err, len, newfd, fput_needed;
  1224. struct sockaddr_storage address;
  1225. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1226. return -EINVAL;
  1227. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1228. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1229. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1230. if (!sock)
  1231. goto out;
  1232. err = -ENFILE;
  1233. if (!(newsock = sock_alloc()))
  1234. goto out_put;
  1235. newsock->type = sock->type;
  1236. newsock->ops = sock->ops;
  1237. /*
  1238. * We don't need try_module_get here, as the listening socket (sock)
  1239. * has the protocol module (sock->ops->owner) held.
  1240. */
  1241. __module_get(newsock->ops->owner);
  1242. newfd = sock_alloc_fd(&newfile, flags & O_CLOEXEC);
  1243. if (unlikely(newfd < 0)) {
  1244. err = newfd;
  1245. sock_release(newsock);
  1246. goto out_put;
  1247. }
  1248. err = sock_attach_fd(newsock, newfile, flags & O_NONBLOCK);
  1249. if (err < 0)
  1250. goto out_fd_simple;
  1251. err = security_socket_accept(sock, newsock);
  1252. if (err)
  1253. goto out_fd;
  1254. err = sock->ops->accept(sock, newsock, sock->file->f_flags);
  1255. if (err < 0)
  1256. goto out_fd;
  1257. if (upeer_sockaddr) {
  1258. if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
  1259. &len, 2) < 0) {
  1260. err = -ECONNABORTED;
  1261. goto out_fd;
  1262. }
  1263. err = move_addr_to_user((struct sockaddr *)&address,
  1264. len, upeer_sockaddr, upeer_addrlen);
  1265. if (err < 0)
  1266. goto out_fd;
  1267. }
  1268. /* File flags are not inherited via accept() unlike another OSes. */
  1269. fd_install(newfd, newfile);
  1270. err = newfd;
  1271. security_socket_post_accept(sock, newsock);
  1272. out_put:
  1273. fput_light(sock->file, fput_needed);
  1274. out:
  1275. return err;
  1276. out_fd_simple:
  1277. sock_release(newsock);
  1278. put_filp(newfile);
  1279. put_unused_fd(newfd);
  1280. goto out_put;
  1281. out_fd:
  1282. fput(newfile);
  1283. put_unused_fd(newfd);
  1284. goto out_put;
  1285. }
  1286. #if 0
  1287. #ifdef HAVE_SET_RESTORE_SIGMASK
  1288. asmlinkage long sys_paccept(int fd, struct sockaddr __user *upeer_sockaddr,
  1289. int __user *upeer_addrlen,
  1290. const sigset_t __user *sigmask,
  1291. size_t sigsetsize, int flags)
  1292. {
  1293. sigset_t ksigmask, sigsaved;
  1294. int ret;
  1295. if (sigmask) {
  1296. /* XXX: Don't preclude handling different sized sigset_t's. */
  1297. if (sigsetsize != sizeof(sigset_t))
  1298. return -EINVAL;
  1299. if (copy_from_user(&ksigmask, sigmask, sizeof(ksigmask)))
  1300. return -EFAULT;
  1301. sigdelsetmask(&ksigmask, sigmask(SIGKILL)|sigmask(SIGSTOP));
  1302. sigprocmask(SIG_SETMASK, &ksigmask, &sigsaved);
  1303. }
  1304. ret = do_accept(fd, upeer_sockaddr, upeer_addrlen, flags);
  1305. if (ret < 0 && signal_pending(current)) {
  1306. /*
  1307. * Don't restore the signal mask yet. Let do_signal() deliver
  1308. * the signal on the way back to userspace, before the signal
  1309. * mask is restored.
  1310. */
  1311. if (sigmask) {
  1312. memcpy(&current->saved_sigmask, &sigsaved,
  1313. sizeof(sigsaved));
  1314. set_restore_sigmask();
  1315. }
  1316. } else if (sigmask)
  1317. sigprocmask(SIG_SETMASK, &sigsaved, NULL);
  1318. return ret;
  1319. }
  1320. #else
  1321. asmlinkage long sys_paccept(int fd, struct sockaddr __user *upeer_sockaddr,
  1322. int __user *upeer_addrlen,
  1323. const sigset_t __user *sigmask,
  1324. size_t sigsetsize, int flags)
  1325. {
  1326. /* The platform does not support restoring the signal mask in the
  1327. * return path. So we do not allow using paccept() with a signal
  1328. * mask. */
  1329. if (sigmask)
  1330. return -EINVAL;
  1331. return do_accept(fd, upeer_sockaddr, upeer_addrlen, flags);
  1332. }
  1333. #endif
  1334. #endif
  1335. asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr,
  1336. int __user *upeer_addrlen)
  1337. {
  1338. return do_accept(fd, upeer_sockaddr, upeer_addrlen, 0);
  1339. }
  1340. /*
  1341. * Attempt to connect to a socket with the server address. The address
  1342. * is in user space so we verify it is OK and move it to kernel space.
  1343. *
  1344. * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
  1345. * break bindings
  1346. *
  1347. * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
  1348. * other SEQPACKET protocols that take time to connect() as it doesn't
  1349. * include the -EINPROGRESS status for such sockets.
  1350. */
  1351. asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr,
  1352. int addrlen)
  1353. {
  1354. struct socket *sock;
  1355. struct sockaddr_storage address;
  1356. int err, fput_needed;
  1357. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1358. if (!sock)
  1359. goto out;
  1360. err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
  1361. if (err < 0)
  1362. goto out_put;
  1363. err =
  1364. security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
  1365. if (err)
  1366. goto out_put;
  1367. err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
  1368. sock->file->f_flags);
  1369. out_put:
  1370. fput_light(sock->file, fput_needed);
  1371. out:
  1372. return err;
  1373. }
  1374. /*
  1375. * Get the local address ('name') of a socket object. Move the obtained
  1376. * name to user space.
  1377. */
  1378. asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr,
  1379. int __user *usockaddr_len)
  1380. {
  1381. struct socket *sock;
  1382. struct sockaddr_storage address;
  1383. int len, err, fput_needed;
  1384. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1385. if (!sock)
  1386. goto out;
  1387. err = security_socket_getsockname(sock);
  1388. if (err)
  1389. goto out_put;
  1390. err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
  1391. if (err)
  1392. goto out_put;
  1393. err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
  1394. out_put:
  1395. fput_light(sock->file, fput_needed);
  1396. out:
  1397. return err;
  1398. }
  1399. /*
  1400. * Get the remote address ('name') of a socket object. Move the obtained
  1401. * name to user space.
  1402. */
  1403. asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr,
  1404. int __user *usockaddr_len)
  1405. {
  1406. struct socket *sock;
  1407. struct sockaddr_storage address;
  1408. int len, err, fput_needed;
  1409. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1410. if (sock != NULL) {
  1411. err = security_socket_getpeername(sock);
  1412. if (err) {
  1413. fput_light(sock->file, fput_needed);
  1414. return err;
  1415. }
  1416. err =
  1417. sock->ops->getname(sock, (struct sockaddr *)&address, &len,
  1418. 1);
  1419. if (!err)
  1420. err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
  1421. usockaddr_len);
  1422. fput_light(sock->file, fput_needed);
  1423. }
  1424. return err;
  1425. }
  1426. /*
  1427. * Send a datagram to a given address. We move the address into kernel
  1428. * space and check the user space data area is readable before invoking
  1429. * the protocol.
  1430. */
  1431. asmlinkage long sys_sendto(int fd, void __user *buff, size_t len,
  1432. unsigned flags, struct sockaddr __user *addr,
  1433. int addr_len)
  1434. {
  1435. struct socket *sock;
  1436. struct sockaddr_storage address;
  1437. int err;
  1438. struct msghdr msg;
  1439. struct iovec iov;
  1440. int fput_needed;
  1441. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1442. if (!sock)
  1443. goto out;
  1444. iov.iov_base = buff;
  1445. iov.iov_len = len;
  1446. msg.msg_name = NULL;
  1447. msg.msg_iov = &iov;
  1448. msg.msg_iovlen = 1;
  1449. msg.msg_control = NULL;
  1450. msg.msg_controllen = 0;
  1451. msg.msg_namelen = 0;
  1452. if (addr) {
  1453. err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
  1454. if (err < 0)
  1455. goto out_put;
  1456. msg.msg_name = (struct sockaddr *)&address;
  1457. msg.msg_namelen = addr_len;
  1458. }
  1459. if (sock->file->f_flags & O_NONBLOCK)
  1460. flags |= MSG_DONTWAIT;
  1461. msg.msg_flags = flags;
  1462. err = sock_sendmsg(sock, &msg, len);
  1463. out_put:
  1464. fput_light(sock->file, fput_needed);
  1465. out:
  1466. return err;
  1467. }
  1468. /*
  1469. * Send a datagram down a socket.
  1470. */
  1471. asmlinkage long sys_send(int fd, void __user *buff, size_t len, unsigned flags)
  1472. {
  1473. return sys_sendto(fd, buff, len, flags, NULL, 0);
  1474. }
  1475. /*
  1476. * Receive a frame from the socket and optionally record the address of the
  1477. * sender. We verify the buffers are writable and if needed move the
  1478. * sender address from kernel to user space.
  1479. */
  1480. asmlinkage long sys_recvfrom(int fd, void __user *ubuf, size_t size,
  1481. unsigned flags, struct sockaddr __user *addr,
  1482. int __user *addr_len)
  1483. {
  1484. struct socket *sock;
  1485. struct iovec iov;
  1486. struct msghdr msg;
  1487. struct sockaddr_storage address;
  1488. int err, err2;
  1489. int fput_needed;
  1490. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1491. if (!sock)
  1492. goto out;
  1493. msg.msg_control = NULL;
  1494. msg.msg_controllen = 0;
  1495. msg.msg_iovlen = 1;
  1496. msg.msg_iov = &iov;
  1497. iov.iov_len = size;
  1498. iov.iov_base = ubuf;
  1499. msg.msg_name = (struct sockaddr *)&address;
  1500. msg.msg_namelen = sizeof(address);
  1501. if (sock->file->f_flags & O_NONBLOCK)
  1502. flags |= MSG_DONTWAIT;
  1503. err = sock_recvmsg(sock, &msg, size, flags);
  1504. if (err >= 0 && addr != NULL) {
  1505. err2 = move_addr_to_user((struct sockaddr *)&address,
  1506. msg.msg_namelen, addr, addr_len);
  1507. if (err2 < 0)
  1508. err = err2;
  1509. }
  1510. fput_light(sock->file, fput_needed);
  1511. out:
  1512. return err;
  1513. }
  1514. /*
  1515. * Receive a datagram from a socket.
  1516. */
  1517. asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
  1518. unsigned flags)
  1519. {
  1520. return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
  1521. }
  1522. /*
  1523. * Set a socket option. Because we don't know the option lengths we have
  1524. * to pass the user mode parameter for the protocols to sort out.
  1525. */
  1526. asmlinkage long sys_setsockopt(int fd, int level, int optname,
  1527. char __user *optval, int optlen)
  1528. {
  1529. int err, fput_needed;
  1530. struct socket *sock;
  1531. if (optlen < 0)
  1532. return -EINVAL;
  1533. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1534. if (sock != NULL) {
  1535. err = security_socket_setsockopt(sock, level, optname);
  1536. if (err)
  1537. goto out_put;
  1538. if (level == SOL_SOCKET)
  1539. err =
  1540. sock_setsockopt(sock, level, optname, optval,
  1541. optlen);
  1542. else
  1543. err =
  1544. sock->ops->setsockopt(sock, level, optname, optval,
  1545. optlen);
  1546. out_put:
  1547. fput_light(sock->file, fput_needed);
  1548. }
  1549. return err;
  1550. }
  1551. /*
  1552. * Get a socket option. Because we don't know the option lengths we have
  1553. * to pass a user mode parameter for the protocols to sort out.
  1554. */
  1555. asmlinkage long sys_getsockopt(int fd, int level, int optname,
  1556. char __user *optval, int __user *optlen)
  1557. {
  1558. int err, fput_needed;
  1559. struct socket *sock;
  1560. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1561. if (sock != NULL) {
  1562. err = security_socket_getsockopt(sock, level, optname);
  1563. if (err)
  1564. goto out_put;
  1565. if (level == SOL_SOCKET)
  1566. err =
  1567. sock_getsockopt(sock, level, optname, optval,
  1568. optlen);
  1569. else
  1570. err =
  1571. sock->ops->getsockopt(sock, level, optname, optval,
  1572. optlen);
  1573. out_put:
  1574. fput_light(sock->file, fput_needed);
  1575. }
  1576. return err;
  1577. }
  1578. /*
  1579. * Shutdown a socket.
  1580. */
  1581. asmlinkage long sys_shutdown(int fd, int how)
  1582. {
  1583. int err, fput_needed;
  1584. struct socket *sock;
  1585. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1586. if (sock != NULL) {
  1587. err = security_socket_shutdown(sock, how);
  1588. if (!err)
  1589. err = sock->ops->shutdown(sock, how);
  1590. fput_light(sock->file, fput_needed);
  1591. }
  1592. return err;
  1593. }
  1594. /* A couple of helpful macros for getting the address of the 32/64 bit
  1595. * fields which are the same type (int / unsigned) on our platforms.
  1596. */
  1597. #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
  1598. #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
  1599. #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
  1600. /*
  1601. * BSD sendmsg interface
  1602. */
  1603. asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
  1604. {
  1605. struct compat_msghdr __user *msg_compat =
  1606. (struct compat_msghdr __user *)msg;
  1607. struct socket *sock;
  1608. struct sockaddr_storage address;
  1609. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  1610. unsigned char ctl[sizeof(struct cmsghdr) + 20]
  1611. __attribute__ ((aligned(sizeof(__kernel_size_t))));
  1612. /* 20 is size of ipv6_pktinfo */
  1613. unsigned char *ctl_buf = ctl;
  1614. struct msghdr msg_sys;
  1615. int err, ctl_len, iov_size, total_len;
  1616. int fput_needed;
  1617. err = -EFAULT;
  1618. if (MSG_CMSG_COMPAT & flags) {
  1619. if (get_compat_msghdr(&msg_sys, msg_compat))
  1620. return -EFAULT;
  1621. }
  1622. else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
  1623. return -EFAULT;
  1624. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1625. if (!sock)
  1626. goto out;
  1627. /* do not move before msg_sys is valid */
  1628. err = -EMSGSIZE;
  1629. if (msg_sys.msg_iovlen > UIO_MAXIOV)
  1630. goto out_put;
  1631. /* Check whether to allocate the iovec area */
  1632. err = -ENOMEM;
  1633. iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
  1634. if (msg_sys.msg_iovlen > UIO_FASTIOV) {
  1635. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1636. if (!iov)
  1637. goto out_put;
  1638. }
  1639. /* This will also move the address data into kernel space */
  1640. if (MSG_CMSG_COMPAT & flags) {
  1641. err = verify_compat_iovec(&msg_sys, iov,
  1642. (struct sockaddr *)&address,
  1643. VERIFY_READ);
  1644. } else
  1645. err = verify_iovec(&msg_sys, iov,
  1646. (struct sockaddr *)&address,
  1647. VERIFY_READ);
  1648. if (err < 0)
  1649. goto out_freeiov;
  1650. total_len = err;
  1651. err = -ENOBUFS;
  1652. if (msg_sys.msg_controllen > INT_MAX)
  1653. goto out_freeiov;
  1654. ctl_len = msg_sys.msg_controllen;
  1655. if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
  1656. err =
  1657. cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
  1658. sizeof(ctl));
  1659. if (err)
  1660. goto out_freeiov;
  1661. ctl_buf = msg_sys.msg_control;
  1662. ctl_len = msg_sys.msg_controllen;
  1663. } else if (ctl_len) {
  1664. if (ctl_len > sizeof(ctl)) {
  1665. ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
  1666. if (ctl_buf == NULL)
  1667. goto out_freeiov;
  1668. }
  1669. err = -EFAULT;
  1670. /*
  1671. * Careful! Before this, msg_sys.msg_control contains a user pointer.
  1672. * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
  1673. * checking falls down on this.
  1674. */
  1675. if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
  1676. ctl_len))
  1677. goto out_freectl;
  1678. msg_sys.msg_control = ctl_buf;
  1679. }
  1680. msg_sys.msg_flags = flags;
  1681. if (sock->file->f_flags & O_NONBLOCK)
  1682. msg_sys.msg_flags |= MSG_DONTWAIT;
  1683. err = sock_sendmsg(sock, &msg_sys, total_len);
  1684. out_freectl:
  1685. if (ctl_buf != ctl)
  1686. sock_kfree_s(sock->sk, ctl_buf, ctl_len);
  1687. out_freeiov:
  1688. if (iov != iovstack)
  1689. sock_kfree_s(sock->sk, iov, iov_size);
  1690. out_put:
  1691. fput_light(sock->file, fput_needed);
  1692. out:
  1693. return err;
  1694. }
  1695. /*
  1696. * BSD recvmsg interface
  1697. */
  1698. asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg,
  1699. unsigned int flags)
  1700. {
  1701. struct compat_msghdr __user *msg_compat =
  1702. (struct compat_msghdr __user *)msg;
  1703. struct socket *sock;
  1704. struct iovec iovstack[UIO_FASTIOV];
  1705. struct iovec *iov = iovstack;
  1706. struct msghdr msg_sys;
  1707. unsigned long cmsg_ptr;
  1708. int err, iov_size, total_len, len;
  1709. int fput_needed;
  1710. /* kernel mode address */
  1711. struct sockaddr_storage addr;
  1712. /* user mode address pointers */
  1713. struct sockaddr __user *uaddr;
  1714. int __user *uaddr_len;
  1715. if (MSG_CMSG_COMPAT & flags) {
  1716. if (get_compat_msghdr(&msg_sys, msg_compat))
  1717. return -EFAULT;
  1718. }
  1719. else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
  1720. return -EFAULT;
  1721. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1722. if (!sock)
  1723. goto out;
  1724. err = -EMSGSIZE;
  1725. if (msg_sys.msg_iovlen > UIO_MAXIOV)
  1726. goto out_put;
  1727. /* Check whether to allocate the iovec area */
  1728. err = -ENOMEM;
  1729. iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
  1730. if (msg_sys.msg_iovlen > UIO_FASTIOV) {
  1731. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1732. if (!iov)
  1733. goto out_put;
  1734. }
  1735. /*
  1736. * Save the user-mode address (verify_iovec will change the
  1737. * kernel msghdr to use the kernel address space)
  1738. */
  1739. uaddr = (__force void __user *)msg_sys.msg_name;
  1740. uaddr_len = COMPAT_NAMELEN(msg);
  1741. if (MSG_CMSG_COMPAT & flags) {
  1742. err = verify_compat_iovec(&msg_sys, iov,
  1743. (struct sockaddr *)&addr,
  1744. VERIFY_WRITE);
  1745. } else
  1746. err = verify_iovec(&msg_sys, iov,
  1747. (struct sockaddr *)&addr,
  1748. VERIFY_WRITE);
  1749. if (err < 0)
  1750. goto out_freeiov;
  1751. total_len = err;
  1752. cmsg_ptr = (unsigned long)msg_sys.msg_control;
  1753. msg_sys.msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
  1754. if (sock->file->f_flags & O_NONBLOCK)
  1755. flags |= MSG_DONTWAIT;
  1756. err = sock_recvmsg(sock, &msg_sys, total_len, flags);
  1757. if (err < 0)
  1758. goto out_freeiov;
  1759. len = err;
  1760. if (uaddr != NULL) {
  1761. err = move_addr_to_user((struct sockaddr *)&addr,
  1762. msg_sys.msg_namelen, uaddr,
  1763. uaddr_len);
  1764. if (err < 0)
  1765. goto out_freeiov;
  1766. }
  1767. err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT),
  1768. COMPAT_FLAGS(msg));
  1769. if (err)
  1770. goto out_freeiov;
  1771. if (MSG_CMSG_COMPAT & flags)
  1772. err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
  1773. &msg_compat->msg_controllen);
  1774. else
  1775. err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
  1776. &msg->msg_controllen);
  1777. if (err)
  1778. goto out_freeiov;
  1779. err = len;
  1780. out_freeiov:
  1781. if (iov != iovstack)
  1782. sock_kfree_s(sock->sk, iov, iov_size);
  1783. out_put:
  1784. fput_light(sock->file, fput_needed);
  1785. out:
  1786. return err;
  1787. }
  1788. #ifdef __ARCH_WANT_SYS_SOCKETCALL
  1789. /* Argument list sizes for sys_socketcall */
  1790. #define AL(x) ((x) * sizeof(unsigned long))
  1791. static const unsigned char nargs[19]={
  1792. AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
  1793. AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
  1794. AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
  1795. AL(6)
  1796. };
  1797. #undef AL
  1798. /*
  1799. * System call vectors.
  1800. *
  1801. * Argument checking cleaned up. Saved 20% in size.
  1802. * This function doesn't need to set the kernel lock because
  1803. * it is set by the callees.
  1804. */
  1805. asmlinkage long sys_socketcall(int call, unsigned long __user *args)
  1806. {
  1807. unsigned long a[6];
  1808. unsigned long a0, a1;
  1809. int err;
  1810. if (call < 1 || call > SYS_PACCEPT)
  1811. return -EINVAL;
  1812. /* copy_from_user should be SMP safe. */
  1813. if (copy_from_user(a, args, nargs[call]))
  1814. return -EFAULT;
  1815. err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
  1816. if (err)
  1817. return err;
  1818. a0 = a[0];
  1819. a1 = a[1];
  1820. switch (call) {
  1821. case SYS_SOCKET:
  1822. err = sys_socket(a0, a1, a[2]);
  1823. break;
  1824. case SYS_BIND:
  1825. err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
  1826. break;
  1827. case SYS_CONNECT:
  1828. err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
  1829. break;
  1830. case SYS_LISTEN:
  1831. err = sys_listen(a0, a1);
  1832. break;
  1833. case SYS_ACCEPT:
  1834. err =
  1835. do_accept(a0, (struct sockaddr __user *)a1,
  1836. (int __user *)a[2], 0);
  1837. break;
  1838. case SYS_GETSOCKNAME:
  1839. err =
  1840. sys_getsockname(a0, (struct sockaddr __user *)a1,
  1841. (int __user *)a[2]);
  1842. break;
  1843. case SYS_GETPEERNAME:
  1844. err =
  1845. sys_getpeername(a0, (struct sockaddr __user *)a1,
  1846. (int __user *)a[2]);
  1847. break;
  1848. case SYS_SOCKETPAIR:
  1849. err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
  1850. break;
  1851. case SYS_SEND:
  1852. err = sys_send(a0, (void __user *)a1, a[2], a[3]);
  1853. break;
  1854. case SYS_SENDTO:
  1855. err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
  1856. (struct sockaddr __user *)a[4], a[5]);
  1857. break;
  1858. case SYS_RECV:
  1859. err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
  1860. break;
  1861. case SYS_RECVFROM:
  1862. err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  1863. (struct sockaddr __user *)a[4],
  1864. (int __user *)a[5]);
  1865. break;
  1866. case SYS_SHUTDOWN:
  1867. err = sys_shutdown(a0, a1);
  1868. break;
  1869. case SYS_SETSOCKOPT:
  1870. err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
  1871. break;
  1872. case SYS_GETSOCKOPT:
  1873. err =
  1874. sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
  1875. (int __user *)a[4]);
  1876. break;
  1877. case SYS_SENDMSG:
  1878. err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
  1879. break;
  1880. case SYS_RECVMSG:
  1881. err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
  1882. break;
  1883. case SYS_PACCEPT:
  1884. err =
  1885. sys_paccept(a0, (struct sockaddr __user *)a1,
  1886. (int __user *)a[2],
  1887. (const sigset_t __user *) a[3],
  1888. a[4], a[5]);
  1889. break;
  1890. default:
  1891. err = -EINVAL;
  1892. break;
  1893. }
  1894. return err;
  1895. }
  1896. #endif /* __ARCH_WANT_SYS_SOCKETCALL */
  1897. /**
  1898. * sock_register - add a socket protocol handler
  1899. * @ops: description of protocol
  1900. *
  1901. * This function is called by a protocol handler that wants to
  1902. * advertise its address family, and have it linked into the
  1903. * socket interface. The value ops->family coresponds to the
  1904. * socket system call protocol family.
  1905. */
  1906. int sock_register(const struct net_proto_family *ops)
  1907. {
  1908. int err;
  1909. if (ops->family >= NPROTO) {
  1910. printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
  1911. NPROTO);
  1912. return -ENOBUFS;
  1913. }
  1914. spin_lock(&net_family_lock);
  1915. if (net_families[ops->family])
  1916. err = -EEXIST;
  1917. else {
  1918. net_families[ops->family] = ops;
  1919. err = 0;
  1920. }
  1921. spin_unlock(&net_family_lock);
  1922. printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
  1923. return err;
  1924. }
  1925. /**
  1926. * sock_unregister - remove a protocol handler
  1927. * @family: protocol family to remove
  1928. *
  1929. * This function is called by a protocol handler that wants to
  1930. * remove its address family, and have it unlinked from the
  1931. * new socket creation.
  1932. *
  1933. * If protocol handler is a module, then it can use module reference
  1934. * counts to protect against new references. If protocol handler is not
  1935. * a module then it needs to provide its own protection in
  1936. * the ops->create routine.
  1937. */
  1938. void sock_unregister(int family)
  1939. {
  1940. BUG_ON(family < 0 || family >= NPROTO);
  1941. spin_lock(&net_family_lock);
  1942. net_families[family] = NULL;
  1943. spin_unlock(&net_family_lock);
  1944. synchronize_rcu();
  1945. printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
  1946. }
  1947. static int __init sock_init(void)
  1948. {
  1949. /*
  1950. * Initialize sock SLAB cache.
  1951. */
  1952. sk_init();
  1953. /*
  1954. * Initialize skbuff SLAB cache
  1955. */
  1956. skb_init();
  1957. /*
  1958. * Initialize the protocols module.
  1959. */
  1960. init_inodecache();
  1961. register_filesystem(&sock_fs_type);
  1962. sock_mnt = kern_mount(&sock_fs_type);
  1963. /* The real protocol initialization is performed in later initcalls.
  1964. */
  1965. #ifdef CONFIG_NETFILTER
  1966. netfilter_init();
  1967. #endif
  1968. return 0;
  1969. }
  1970. core_initcall(sock_init); /* early initcall */
  1971. #ifdef CONFIG_PROC_FS
  1972. void socket_seq_show(struct seq_file *seq)
  1973. {
  1974. int cpu;
  1975. int counter = 0;
  1976. for_each_possible_cpu(cpu)
  1977. counter += per_cpu(sockets_in_use, cpu);
  1978. /* It can be negative, by the way. 8) */
  1979. if (counter < 0)
  1980. counter = 0;
  1981. seq_printf(seq, "sockets: used %d\n", counter);
  1982. }
  1983. #endif /* CONFIG_PROC_FS */
  1984. #ifdef CONFIG_COMPAT
  1985. static long compat_sock_ioctl(struct file *file, unsigned cmd,
  1986. unsigned long arg)
  1987. {
  1988. struct socket *sock = file->private_data;
  1989. int ret = -ENOIOCTLCMD;
  1990. struct sock *sk;
  1991. struct net *net;
  1992. sk = sock->sk;
  1993. net = sock_net(sk);
  1994. if (sock->ops->compat_ioctl)
  1995. ret = sock->ops->compat_ioctl(sock, cmd, arg);
  1996. if (ret == -ENOIOCTLCMD &&
  1997. (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
  1998. ret = compat_wext_handle_ioctl(net, cmd, arg);
  1999. return ret;
  2000. }
  2001. #endif
  2002. int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
  2003. {
  2004. return sock->ops->bind(sock, addr, addrlen);
  2005. }
  2006. int kernel_listen(struct socket *sock, int backlog)
  2007. {
  2008. return sock->ops->listen(sock, backlog);
  2009. }
  2010. int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
  2011. {
  2012. struct sock *sk = sock->sk;
  2013. int err;
  2014. err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
  2015. newsock);
  2016. if (err < 0)
  2017. goto done;
  2018. err = sock->ops->accept(sock, *newsock, flags);
  2019. if (err < 0) {
  2020. sock_release(*newsock);
  2021. *newsock = NULL;
  2022. goto done;
  2023. }
  2024. (*newsock)->ops = sock->ops;
  2025. done:
  2026. return err;
  2027. }
  2028. int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
  2029. int flags)
  2030. {
  2031. return sock->ops->connect(sock, addr, addrlen, flags);
  2032. }
  2033. int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
  2034. int *addrlen)
  2035. {
  2036. return sock->ops->getname(sock, addr, addrlen, 0);
  2037. }
  2038. int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
  2039. int *addrlen)
  2040. {
  2041. return sock->ops->getname(sock, addr, addrlen, 1);
  2042. }
  2043. int kernel_getsockopt(struct socket *sock, int level, int optname,
  2044. char *optval, int *optlen)
  2045. {
  2046. mm_segment_t oldfs = get_fs();
  2047. int err;
  2048. set_fs(KERNEL_DS);
  2049. if (level == SOL_SOCKET)
  2050. err = sock_getsockopt(sock, level, optname, optval, optlen);
  2051. else
  2052. err = sock->ops->getsockopt(sock, level, optname, optval,
  2053. optlen);
  2054. set_fs(oldfs);
  2055. return err;
  2056. }
  2057. int kernel_setsockopt(struct socket *sock, int level, int optname,
  2058. char *optval, int optlen)
  2059. {
  2060. mm_segment_t oldfs = get_fs();
  2061. int err;
  2062. set_fs(KERNEL_DS);
  2063. if (level == SOL_SOCKET)
  2064. err = sock_setsockopt(sock, level, optname, optval, optlen);
  2065. else
  2066. err = sock->ops->setsockopt(sock, level, optname, optval,
  2067. optlen);
  2068. set_fs(oldfs);
  2069. return err;
  2070. }
  2071. int kernel_sendpage(struct socket *sock, struct page *page, int offset,
  2072. size_t size, int flags)
  2073. {
  2074. if (sock->ops->sendpage)
  2075. return sock->ops->sendpage(sock, page, offset, size, flags);
  2076. return sock_no_sendpage(sock, page, offset, size, flags);
  2077. }
  2078. int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
  2079. {
  2080. mm_segment_t oldfs = get_fs();
  2081. int err;
  2082. set_fs(KERNEL_DS);
  2083. err = sock->ops->ioctl(sock, cmd, arg);
  2084. set_fs(oldfs);
  2085. return err;
  2086. }
  2087. int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
  2088. {
  2089. return sock->ops->shutdown(sock, how);
  2090. }
  2091. EXPORT_SYMBOL(sock_create);
  2092. EXPORT_SYMBOL(sock_create_kern);
  2093. EXPORT_SYMBOL(sock_create_lite);
  2094. EXPORT_SYMBOL(sock_map_fd);
  2095. EXPORT_SYMBOL(sock_recvmsg);
  2096. EXPORT_SYMBOL(sock_register);
  2097. EXPORT_SYMBOL(sock_release);
  2098. EXPORT_SYMBOL(sock_sendmsg);
  2099. EXPORT_SYMBOL(sock_unregister);
  2100. EXPORT_SYMBOL(sock_wake_async);
  2101. EXPORT_SYMBOL(sockfd_lookup);
  2102. EXPORT_SYMBOL(kernel_sendmsg);
  2103. EXPORT_SYMBOL(kernel_recvmsg);
  2104. EXPORT_SYMBOL(kernel_bind);
  2105. EXPORT_SYMBOL(kernel_listen);
  2106. EXPORT_SYMBOL(kernel_accept);
  2107. EXPORT_SYMBOL(kernel_connect);
  2108. EXPORT_SYMBOL(kernel_getsockname);
  2109. EXPORT_SYMBOL(kernel_getpeername);
  2110. EXPORT_SYMBOL(kernel_getsockopt);
  2111. EXPORT_SYMBOL(kernel_setsockopt);
  2112. EXPORT_SYMBOL(kernel_sendpage);
  2113. EXPORT_SYMBOL(kernel_sock_ioctl);
  2114. EXPORT_SYMBOL(kernel_sock_shutdown);