socket.c 49 KB

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