socket.c 51 KB

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