socket.c 51 KB

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