socket.c 58 KB

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