socket.c 53 KB

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