socket.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096
  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. /*
  553. * Read data from a socket. ubuf is a user mode pointer. We make sure the user
  554. * area ubuf...ubuf+size-1 is writable before asking the protocol.
  555. */
  556. static ssize_t sock_aio_read(struct kiocb *iocb, char __user *ubuf,
  557. size_t size, loff_t pos)
  558. {
  559. struct sock_iocb *x, siocb;
  560. struct socket *sock;
  561. int flags;
  562. if (pos != 0)
  563. return -ESPIPE;
  564. if (size==0) /* Match SYS5 behaviour */
  565. return 0;
  566. if (is_sync_kiocb(iocb))
  567. x = &siocb;
  568. else {
  569. x = kmalloc(sizeof(struct sock_iocb), GFP_KERNEL);
  570. if (!x)
  571. return -ENOMEM;
  572. iocb->ki_dtor = sock_aio_dtor;
  573. }
  574. iocb->private = x;
  575. x->kiocb = iocb;
  576. sock = iocb->ki_filp->private_data;
  577. x->async_msg.msg_name = NULL;
  578. x->async_msg.msg_namelen = 0;
  579. x->async_msg.msg_iov = &x->async_iov;
  580. x->async_msg.msg_iovlen = 1;
  581. x->async_msg.msg_control = NULL;
  582. x->async_msg.msg_controllen = 0;
  583. x->async_iov.iov_base = ubuf;
  584. x->async_iov.iov_len = size;
  585. flags = !(iocb->ki_filp->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
  586. return __sock_recvmsg(iocb, sock, &x->async_msg, size, flags);
  587. }
  588. /*
  589. * Write data to a socket. We verify that the user area ubuf..ubuf+size-1
  590. * is readable by the user process.
  591. */
  592. static ssize_t sock_aio_write(struct kiocb *iocb, const char __user *ubuf,
  593. size_t size, loff_t pos)
  594. {
  595. struct sock_iocb *x, siocb;
  596. struct socket *sock;
  597. if (pos != 0)
  598. return -ESPIPE;
  599. if(size==0) /* Match SYS5 behaviour */
  600. return 0;
  601. if (is_sync_kiocb(iocb))
  602. x = &siocb;
  603. else {
  604. x = kmalloc(sizeof(struct sock_iocb), GFP_KERNEL);
  605. if (!x)
  606. return -ENOMEM;
  607. iocb->ki_dtor = sock_aio_dtor;
  608. }
  609. iocb->private = x;
  610. x->kiocb = iocb;
  611. sock = iocb->ki_filp->private_data;
  612. x->async_msg.msg_name = NULL;
  613. x->async_msg.msg_namelen = 0;
  614. x->async_msg.msg_iov = &x->async_iov;
  615. x->async_msg.msg_iovlen = 1;
  616. x->async_msg.msg_control = NULL;
  617. x->async_msg.msg_controllen = 0;
  618. x->async_msg.msg_flags = !(iocb->ki_filp->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
  619. if (sock->type == SOCK_SEQPACKET)
  620. x->async_msg.msg_flags |= MSG_EOR;
  621. x->async_iov.iov_base = (void __user *)ubuf;
  622. x->async_iov.iov_len = size;
  623. return __sock_sendmsg(iocb, sock, &x->async_msg, size);
  624. }
  625. static ssize_t sock_sendpage(struct file *file, struct page *page,
  626. int offset, size_t size, loff_t *ppos, int more)
  627. {
  628. struct socket *sock;
  629. int flags;
  630. sock = file->private_data;
  631. flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
  632. if (more)
  633. flags |= MSG_MORE;
  634. return sock->ops->sendpage(sock, page, offset, size, flags);
  635. }
  636. static int sock_readv_writev(int type,
  637. struct file * file, const struct iovec * iov,
  638. long count, size_t size)
  639. {
  640. struct msghdr msg;
  641. struct socket *sock;
  642. sock = file->private_data;
  643. msg.msg_name = NULL;
  644. msg.msg_namelen = 0;
  645. msg.msg_control = NULL;
  646. msg.msg_controllen = 0;
  647. msg.msg_iov = (struct iovec *) iov;
  648. msg.msg_iovlen = count;
  649. msg.msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  650. /* read() does a VERIFY_WRITE */
  651. if (type == VERIFY_WRITE)
  652. return sock_recvmsg(sock, &msg, size, msg.msg_flags);
  653. if (sock->type == SOCK_SEQPACKET)
  654. msg.msg_flags |= MSG_EOR;
  655. return sock_sendmsg(sock, &msg, size);
  656. }
  657. static ssize_t sock_readv(struct file *file, const struct iovec *vector,
  658. unsigned long count, loff_t *ppos)
  659. {
  660. size_t tot_len = 0;
  661. int i;
  662. for (i = 0 ; i < count ; i++)
  663. tot_len += vector[i].iov_len;
  664. return sock_readv_writev(VERIFY_WRITE,
  665. file, vector, count, tot_len);
  666. }
  667. static ssize_t sock_writev(struct file *file, const struct iovec *vector,
  668. unsigned long count, loff_t *ppos)
  669. {
  670. size_t tot_len = 0;
  671. int i;
  672. for (i = 0 ; i < count ; i++)
  673. tot_len += vector[i].iov_len;
  674. return sock_readv_writev(VERIFY_READ,
  675. file, vector, count, tot_len);
  676. }
  677. /*
  678. * Atomic setting of ioctl hooks to avoid race
  679. * with module unload.
  680. */
  681. static DECLARE_MUTEX(br_ioctl_mutex);
  682. static int (*br_ioctl_hook)(unsigned int cmd, void __user *arg) = NULL;
  683. void brioctl_set(int (*hook)(unsigned int, void __user *))
  684. {
  685. down(&br_ioctl_mutex);
  686. br_ioctl_hook = hook;
  687. up(&br_ioctl_mutex);
  688. }
  689. EXPORT_SYMBOL(brioctl_set);
  690. static DECLARE_MUTEX(vlan_ioctl_mutex);
  691. static int (*vlan_ioctl_hook)(void __user *arg);
  692. void vlan_ioctl_set(int (*hook)(void __user *))
  693. {
  694. down(&vlan_ioctl_mutex);
  695. vlan_ioctl_hook = hook;
  696. up(&vlan_ioctl_mutex);
  697. }
  698. EXPORT_SYMBOL(vlan_ioctl_set);
  699. static DECLARE_MUTEX(dlci_ioctl_mutex);
  700. static int (*dlci_ioctl_hook)(unsigned int, void __user *);
  701. void dlci_ioctl_set(int (*hook)(unsigned int, void __user *))
  702. {
  703. down(&dlci_ioctl_mutex);
  704. dlci_ioctl_hook = hook;
  705. up(&dlci_ioctl_mutex);
  706. }
  707. EXPORT_SYMBOL(dlci_ioctl_set);
  708. /*
  709. * With an ioctl, arg may well be a user mode pointer, but we don't know
  710. * what to do with it - that's up to the protocol still.
  711. */
  712. static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  713. {
  714. struct socket *sock;
  715. void __user *argp = (void __user *)arg;
  716. int pid, err;
  717. sock = file->private_data;
  718. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
  719. err = dev_ioctl(cmd, argp);
  720. } else
  721. #ifdef WIRELESS_EXT
  722. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
  723. err = dev_ioctl(cmd, argp);
  724. } else
  725. #endif /* WIRELESS_EXT */
  726. switch (cmd) {
  727. case FIOSETOWN:
  728. case SIOCSPGRP:
  729. err = -EFAULT;
  730. if (get_user(pid, (int __user *)argp))
  731. break;
  732. err = f_setown(sock->file, pid, 1);
  733. break;
  734. case FIOGETOWN:
  735. case SIOCGPGRP:
  736. err = put_user(sock->file->f_owner.pid, (int __user *)argp);
  737. break;
  738. case SIOCGIFBR:
  739. case SIOCSIFBR:
  740. case SIOCBRADDBR:
  741. case SIOCBRDELBR:
  742. err = -ENOPKG;
  743. if (!br_ioctl_hook)
  744. request_module("bridge");
  745. down(&br_ioctl_mutex);
  746. if (br_ioctl_hook)
  747. err = br_ioctl_hook(cmd, argp);
  748. up(&br_ioctl_mutex);
  749. break;
  750. case SIOCGIFVLAN:
  751. case SIOCSIFVLAN:
  752. err = -ENOPKG;
  753. if (!vlan_ioctl_hook)
  754. request_module("8021q");
  755. down(&vlan_ioctl_mutex);
  756. if (vlan_ioctl_hook)
  757. err = vlan_ioctl_hook(argp);
  758. up(&vlan_ioctl_mutex);
  759. break;
  760. case SIOCGIFDIVERT:
  761. case SIOCSIFDIVERT:
  762. /* Convert this to call through a hook */
  763. err = divert_ioctl(cmd, argp);
  764. break;
  765. case SIOCADDDLCI:
  766. case SIOCDELDLCI:
  767. err = -ENOPKG;
  768. if (!dlci_ioctl_hook)
  769. request_module("dlci");
  770. if (dlci_ioctl_hook) {
  771. down(&dlci_ioctl_mutex);
  772. err = dlci_ioctl_hook(cmd, argp);
  773. up(&dlci_ioctl_mutex);
  774. }
  775. break;
  776. default:
  777. err = sock->ops->ioctl(sock, cmd, arg);
  778. break;
  779. }
  780. return err;
  781. }
  782. int sock_create_lite(int family, int type, int protocol, struct socket **res)
  783. {
  784. int err;
  785. struct socket *sock = NULL;
  786. err = security_socket_create(family, type, protocol, 1);
  787. if (err)
  788. goto out;
  789. sock = sock_alloc();
  790. if (!sock) {
  791. err = -ENOMEM;
  792. goto out;
  793. }
  794. security_socket_post_create(sock, family, type, protocol, 1);
  795. sock->type = type;
  796. out:
  797. *res = sock;
  798. return err;
  799. }
  800. /* No kernel lock held - perfect */
  801. static unsigned int sock_poll(struct file *file, poll_table * wait)
  802. {
  803. struct socket *sock;
  804. /*
  805. * We can't return errors to poll, so it's either yes or no.
  806. */
  807. sock = file->private_data;
  808. return sock->ops->poll(file, sock, wait);
  809. }
  810. static int sock_mmap(struct file * file, struct vm_area_struct * vma)
  811. {
  812. struct socket *sock = file->private_data;
  813. return sock->ops->mmap(file, sock, vma);
  814. }
  815. static int sock_close(struct inode *inode, struct file *filp)
  816. {
  817. /*
  818. * It was possible the inode is NULL we were
  819. * closing an unfinished socket.
  820. */
  821. if (!inode)
  822. {
  823. printk(KERN_DEBUG "sock_close: NULL inode\n");
  824. return 0;
  825. }
  826. sock_fasync(-1, filp, 0);
  827. sock_release(SOCKET_I(inode));
  828. return 0;
  829. }
  830. /*
  831. * Update the socket async list
  832. *
  833. * Fasync_list locking strategy.
  834. *
  835. * 1. fasync_list is modified only under process context socket lock
  836. * i.e. under semaphore.
  837. * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
  838. * or under socket lock.
  839. * 3. fasync_list can be used from softirq context, so that
  840. * modification under socket lock have to be enhanced with
  841. * write_lock_bh(&sk->sk_callback_lock).
  842. * --ANK (990710)
  843. */
  844. static int sock_fasync(int fd, struct file *filp, int on)
  845. {
  846. struct fasync_struct *fa, *fna=NULL, **prev;
  847. struct socket *sock;
  848. struct sock *sk;
  849. if (on)
  850. {
  851. fna=(struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
  852. if(fna==NULL)
  853. return -ENOMEM;
  854. }
  855. sock = filp->private_data;
  856. if ((sk=sock->sk) == NULL) {
  857. kfree(fna);
  858. return -EINVAL;
  859. }
  860. lock_sock(sk);
  861. prev=&(sock->fasync_list);
  862. for (fa=*prev; fa!=NULL; prev=&fa->fa_next,fa=*prev)
  863. if (fa->fa_file==filp)
  864. break;
  865. if(on)
  866. {
  867. if(fa!=NULL)
  868. {
  869. write_lock_bh(&sk->sk_callback_lock);
  870. fa->fa_fd=fd;
  871. write_unlock_bh(&sk->sk_callback_lock);
  872. kfree(fna);
  873. goto out;
  874. }
  875. fna->fa_file=filp;
  876. fna->fa_fd=fd;
  877. fna->magic=FASYNC_MAGIC;
  878. fna->fa_next=sock->fasync_list;
  879. write_lock_bh(&sk->sk_callback_lock);
  880. sock->fasync_list=fna;
  881. write_unlock_bh(&sk->sk_callback_lock);
  882. }
  883. else
  884. {
  885. if (fa!=NULL)
  886. {
  887. write_lock_bh(&sk->sk_callback_lock);
  888. *prev=fa->fa_next;
  889. write_unlock_bh(&sk->sk_callback_lock);
  890. kfree(fa);
  891. }
  892. }
  893. out:
  894. release_sock(sock->sk);
  895. return 0;
  896. }
  897. /* This function may be called only under socket lock or callback_lock */
  898. int sock_wake_async(struct socket *sock, int how, int band)
  899. {
  900. if (!sock || !sock->fasync_list)
  901. return -1;
  902. switch (how)
  903. {
  904. case 1:
  905. if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
  906. break;
  907. goto call_kill;
  908. case 2:
  909. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
  910. break;
  911. /* fall through */
  912. case 0:
  913. call_kill:
  914. __kill_fasync(sock->fasync_list, SIGIO, band);
  915. break;
  916. case 3:
  917. __kill_fasync(sock->fasync_list, SIGURG, band);
  918. }
  919. return 0;
  920. }
  921. static int __sock_create(int family, int type, int protocol, struct socket **res, int kern)
  922. {
  923. int err;
  924. struct socket *sock;
  925. /*
  926. * Check protocol is in range
  927. */
  928. if (family < 0 || family >= NPROTO)
  929. return -EAFNOSUPPORT;
  930. if (type < 0 || type >= SOCK_MAX)
  931. return -EINVAL;
  932. /* Compatibility.
  933. This uglymoron is moved from INET layer to here to avoid
  934. deadlock in module load.
  935. */
  936. if (family == PF_INET && type == SOCK_PACKET) {
  937. static int warned;
  938. if (!warned) {
  939. warned = 1;
  940. printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n", current->comm);
  941. }
  942. family = PF_PACKET;
  943. }
  944. err = security_socket_create(family, type, protocol, kern);
  945. if (err)
  946. return err;
  947. #if defined(CONFIG_KMOD)
  948. /* Attempt to load a protocol module if the find failed.
  949. *
  950. * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
  951. * requested real, full-featured networking support upon configuration.
  952. * Otherwise module support will break!
  953. */
  954. if (net_families[family]==NULL)
  955. {
  956. request_module("net-pf-%d",family);
  957. }
  958. #endif
  959. net_family_read_lock();
  960. if (net_families[family] == NULL) {
  961. err = -EAFNOSUPPORT;
  962. goto out;
  963. }
  964. /*
  965. * Allocate the socket and allow the family to set things up. if
  966. * the protocol is 0, the family is instructed to select an appropriate
  967. * default.
  968. */
  969. if (!(sock = sock_alloc())) {
  970. printk(KERN_WARNING "socket: no more sockets\n");
  971. err = -ENFILE; /* Not exactly a match, but its the
  972. closest posix thing */
  973. goto out;
  974. }
  975. sock->type = type;
  976. /*
  977. * We will call the ->create function, that possibly is in a loadable
  978. * module, so we have to bump that loadable module refcnt first.
  979. */
  980. err = -EAFNOSUPPORT;
  981. if (!try_module_get(net_families[family]->owner))
  982. goto out_release;
  983. if ((err = net_families[family]->create(sock, protocol)) < 0)
  984. goto out_module_put;
  985. /*
  986. * Now to bump the refcnt of the [loadable] module that owns this
  987. * socket at sock_release time we decrement its refcnt.
  988. */
  989. if (!try_module_get(sock->ops->owner)) {
  990. sock->ops = NULL;
  991. goto out_module_put;
  992. }
  993. /*
  994. * Now that we're done with the ->create function, the [loadable]
  995. * module can have its refcnt decremented
  996. */
  997. module_put(net_families[family]->owner);
  998. *res = sock;
  999. security_socket_post_create(sock, family, type, protocol, kern);
  1000. out:
  1001. net_family_read_unlock();
  1002. return err;
  1003. out_module_put:
  1004. module_put(net_families[family]->owner);
  1005. out_release:
  1006. sock_release(sock);
  1007. goto out;
  1008. }
  1009. int sock_create(int family, int type, int protocol, struct socket **res)
  1010. {
  1011. return __sock_create(family, type, protocol, res, 0);
  1012. }
  1013. int sock_create_kern(int family, int type, int protocol, struct socket **res)
  1014. {
  1015. return __sock_create(family, type, protocol, res, 1);
  1016. }
  1017. asmlinkage long sys_socket(int family, int type, int protocol)
  1018. {
  1019. int retval;
  1020. struct socket *sock;
  1021. retval = sock_create(family, type, protocol, &sock);
  1022. if (retval < 0)
  1023. goto out;
  1024. retval = sock_map_fd(sock);
  1025. if (retval < 0)
  1026. goto out_release;
  1027. out:
  1028. /* It may be already another descriptor 8) Not kernel problem. */
  1029. return retval;
  1030. out_release:
  1031. sock_release(sock);
  1032. return retval;
  1033. }
  1034. /*
  1035. * Create a pair of connected sockets.
  1036. */
  1037. asmlinkage long sys_socketpair(int family, int type, int protocol, int __user *usockvec)
  1038. {
  1039. struct socket *sock1, *sock2;
  1040. int fd1, fd2, err;
  1041. /*
  1042. * Obtain the first socket and check if the underlying protocol
  1043. * supports the socketpair call.
  1044. */
  1045. err = sock_create(family, type, protocol, &sock1);
  1046. if (err < 0)
  1047. goto out;
  1048. err = sock_create(family, type, protocol, &sock2);
  1049. if (err < 0)
  1050. goto out_release_1;
  1051. err = sock1->ops->socketpair(sock1, sock2);
  1052. if (err < 0)
  1053. goto out_release_both;
  1054. fd1 = fd2 = -1;
  1055. err = sock_map_fd(sock1);
  1056. if (err < 0)
  1057. goto out_release_both;
  1058. fd1 = err;
  1059. err = sock_map_fd(sock2);
  1060. if (err < 0)
  1061. goto out_close_1;
  1062. fd2 = err;
  1063. /* fd1 and fd2 may be already another descriptors.
  1064. * Not kernel problem.
  1065. */
  1066. err = put_user(fd1, &usockvec[0]);
  1067. if (!err)
  1068. err = put_user(fd2, &usockvec[1]);
  1069. if (!err)
  1070. return 0;
  1071. sys_close(fd2);
  1072. sys_close(fd1);
  1073. return err;
  1074. out_close_1:
  1075. sock_release(sock2);
  1076. sys_close(fd1);
  1077. return err;
  1078. out_release_both:
  1079. sock_release(sock2);
  1080. out_release_1:
  1081. sock_release(sock1);
  1082. out:
  1083. return err;
  1084. }
  1085. /*
  1086. * Bind a name to a socket. Nothing much to do here since it's
  1087. * the protocol's responsibility to handle the local address.
  1088. *
  1089. * We move the socket address to kernel space before we call
  1090. * the protocol layer (having also checked the address is ok).
  1091. */
  1092. asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
  1093. {
  1094. struct socket *sock;
  1095. char address[MAX_SOCK_ADDR];
  1096. int err;
  1097. if((sock = sockfd_lookup(fd,&err))!=NULL)
  1098. {
  1099. if((err=move_addr_to_kernel(umyaddr,addrlen,address))>=0) {
  1100. err = security_socket_bind(sock, (struct sockaddr *)address, addrlen);
  1101. if (err) {
  1102. sockfd_put(sock);
  1103. return err;
  1104. }
  1105. err = sock->ops->bind(sock, (struct sockaddr *)address, addrlen);
  1106. }
  1107. sockfd_put(sock);
  1108. }
  1109. return err;
  1110. }
  1111. /*
  1112. * Perform a listen. Basically, we allow the protocol to do anything
  1113. * necessary for a listen, and if that works, we mark the socket as
  1114. * ready for listening.
  1115. */
  1116. int sysctl_somaxconn = SOMAXCONN;
  1117. asmlinkage long sys_listen(int fd, int backlog)
  1118. {
  1119. struct socket *sock;
  1120. int err;
  1121. if ((sock = sockfd_lookup(fd, &err)) != NULL) {
  1122. if ((unsigned) backlog > sysctl_somaxconn)
  1123. backlog = sysctl_somaxconn;
  1124. err = security_socket_listen(sock, backlog);
  1125. if (err) {
  1126. sockfd_put(sock);
  1127. return err;
  1128. }
  1129. err=sock->ops->listen(sock, backlog);
  1130. sockfd_put(sock);
  1131. }
  1132. return err;
  1133. }
  1134. /*
  1135. * For accept, we attempt to create a new socket, set up the link
  1136. * with the client, wake up the client, then return the new
  1137. * connected fd. We collect the address of the connector in kernel
  1138. * space and move it to user at the very end. This is unclean because
  1139. * we open the socket then return an error.
  1140. *
  1141. * 1003.1g adds the ability to recvmsg() to query connection pending
  1142. * status to recvmsg. We need to add that support in a way thats
  1143. * clean when we restucture accept also.
  1144. */
  1145. asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr, int __user *upeer_addrlen)
  1146. {
  1147. struct socket *sock, *newsock;
  1148. int err, len;
  1149. char address[MAX_SOCK_ADDR];
  1150. sock = sockfd_lookup(fd, &err);
  1151. if (!sock)
  1152. goto out;
  1153. err = -ENFILE;
  1154. if (!(newsock = sock_alloc()))
  1155. goto out_put;
  1156. newsock->type = sock->type;
  1157. newsock->ops = sock->ops;
  1158. err = security_socket_accept(sock, newsock);
  1159. if (err)
  1160. goto out_release;
  1161. /*
  1162. * We don't need try_module_get here, as the listening socket (sock)
  1163. * has the protocol module (sock->ops->owner) held.
  1164. */
  1165. __module_get(newsock->ops->owner);
  1166. err = sock->ops->accept(sock, newsock, sock->file->f_flags);
  1167. if (err < 0)
  1168. goto out_release;
  1169. if (upeer_sockaddr) {
  1170. if(newsock->ops->getname(newsock, (struct sockaddr *)address, &len, 2)<0) {
  1171. err = -ECONNABORTED;
  1172. goto out_release;
  1173. }
  1174. err = move_addr_to_user(address, len, upeer_sockaddr, upeer_addrlen);
  1175. if (err < 0)
  1176. goto out_release;
  1177. }
  1178. /* File flags are not inherited via accept() unlike another OSes. */
  1179. if ((err = sock_map_fd(newsock)) < 0)
  1180. goto out_release;
  1181. security_socket_post_accept(sock, newsock);
  1182. out_put:
  1183. sockfd_put(sock);
  1184. out:
  1185. return err;
  1186. out_release:
  1187. sock_release(newsock);
  1188. goto out_put;
  1189. }
  1190. /*
  1191. * Attempt to connect to a socket with the server address. The address
  1192. * is in user space so we verify it is OK and move it to kernel space.
  1193. *
  1194. * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
  1195. * break bindings
  1196. *
  1197. * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
  1198. * other SEQPACKET protocols that take time to connect() as it doesn't
  1199. * include the -EINPROGRESS status for such sockets.
  1200. */
  1201. asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
  1202. {
  1203. struct socket *sock;
  1204. char address[MAX_SOCK_ADDR];
  1205. int err;
  1206. sock = sockfd_lookup(fd, &err);
  1207. if (!sock)
  1208. goto out;
  1209. err = move_addr_to_kernel(uservaddr, addrlen, address);
  1210. if (err < 0)
  1211. goto out_put;
  1212. err = security_socket_connect(sock, (struct sockaddr *)address, addrlen);
  1213. if (err)
  1214. goto out_put;
  1215. err = sock->ops->connect(sock, (struct sockaddr *) address, addrlen,
  1216. sock->file->f_flags);
  1217. out_put:
  1218. sockfd_put(sock);
  1219. out:
  1220. return err;
  1221. }
  1222. /*
  1223. * Get the local address ('name') of a socket object. Move the obtained
  1224. * name to user space.
  1225. */
  1226. asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len)
  1227. {
  1228. struct socket *sock;
  1229. char address[MAX_SOCK_ADDR];
  1230. int len, err;
  1231. sock = sockfd_lookup(fd, &err);
  1232. if (!sock)
  1233. goto out;
  1234. err = security_socket_getsockname(sock);
  1235. if (err)
  1236. goto out_put;
  1237. err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 0);
  1238. if (err)
  1239. goto out_put;
  1240. err = move_addr_to_user(address, len, usockaddr, usockaddr_len);
  1241. out_put:
  1242. sockfd_put(sock);
  1243. out:
  1244. return err;
  1245. }
  1246. /*
  1247. * Get the remote address ('name') of a socket object. Move the obtained
  1248. * name to user space.
  1249. */
  1250. asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr, int __user *usockaddr_len)
  1251. {
  1252. struct socket *sock;
  1253. char address[MAX_SOCK_ADDR];
  1254. int len, err;
  1255. if ((sock = sockfd_lookup(fd, &err))!=NULL)
  1256. {
  1257. err = security_socket_getpeername(sock);
  1258. if (err) {
  1259. sockfd_put(sock);
  1260. return err;
  1261. }
  1262. err = sock->ops->getname(sock, (struct sockaddr *)address, &len, 1);
  1263. if (!err)
  1264. err=move_addr_to_user(address,len, usockaddr, usockaddr_len);
  1265. sockfd_put(sock);
  1266. }
  1267. return err;
  1268. }
  1269. /*
  1270. * Send a datagram to a given address. We move the address into kernel
  1271. * space and check the user space data area is readable before invoking
  1272. * the protocol.
  1273. */
  1274. asmlinkage long sys_sendto(int fd, void __user * buff, size_t len, unsigned flags,
  1275. struct sockaddr __user *addr, int addr_len)
  1276. {
  1277. struct socket *sock;
  1278. char address[MAX_SOCK_ADDR];
  1279. int err;
  1280. struct msghdr msg;
  1281. struct iovec iov;
  1282. sock = sockfd_lookup(fd, &err);
  1283. if (!sock)
  1284. goto out;
  1285. iov.iov_base=buff;
  1286. iov.iov_len=len;
  1287. msg.msg_name=NULL;
  1288. msg.msg_iov=&iov;
  1289. msg.msg_iovlen=1;
  1290. msg.msg_control=NULL;
  1291. msg.msg_controllen=0;
  1292. msg.msg_namelen=0;
  1293. if(addr)
  1294. {
  1295. err = move_addr_to_kernel(addr, addr_len, address);
  1296. if (err < 0)
  1297. goto out_put;
  1298. msg.msg_name=address;
  1299. msg.msg_namelen=addr_len;
  1300. }
  1301. if (sock->file->f_flags & O_NONBLOCK)
  1302. flags |= MSG_DONTWAIT;
  1303. msg.msg_flags = flags;
  1304. err = sock_sendmsg(sock, &msg, len);
  1305. out_put:
  1306. sockfd_put(sock);
  1307. out:
  1308. return err;
  1309. }
  1310. /*
  1311. * Send a datagram down a socket.
  1312. */
  1313. asmlinkage long sys_send(int fd, void __user * buff, size_t len, unsigned flags)
  1314. {
  1315. return sys_sendto(fd, buff, len, flags, NULL, 0);
  1316. }
  1317. /*
  1318. * Receive a frame from the socket and optionally record the address of the
  1319. * sender. We verify the buffers are writable and if needed move the
  1320. * sender address from kernel to user space.
  1321. */
  1322. asmlinkage long sys_recvfrom(int fd, void __user * ubuf, size_t size, unsigned flags,
  1323. struct sockaddr __user *addr, int __user *addr_len)
  1324. {
  1325. struct socket *sock;
  1326. struct iovec iov;
  1327. struct msghdr msg;
  1328. char address[MAX_SOCK_ADDR];
  1329. int err,err2;
  1330. sock = sockfd_lookup(fd, &err);
  1331. if (!sock)
  1332. goto out;
  1333. msg.msg_control=NULL;
  1334. msg.msg_controllen=0;
  1335. msg.msg_iovlen=1;
  1336. msg.msg_iov=&iov;
  1337. iov.iov_len=size;
  1338. iov.iov_base=ubuf;
  1339. msg.msg_name=address;
  1340. msg.msg_namelen=MAX_SOCK_ADDR;
  1341. if (sock->file->f_flags & O_NONBLOCK)
  1342. flags |= MSG_DONTWAIT;
  1343. err=sock_recvmsg(sock, &msg, size, flags);
  1344. if(err >= 0 && addr != NULL)
  1345. {
  1346. err2=move_addr_to_user(address, msg.msg_namelen, addr, addr_len);
  1347. if(err2<0)
  1348. err=err2;
  1349. }
  1350. sockfd_put(sock);
  1351. out:
  1352. return err;
  1353. }
  1354. /*
  1355. * Receive a datagram from a socket.
  1356. */
  1357. asmlinkage long sys_recv(int fd, void __user * ubuf, size_t size, unsigned flags)
  1358. {
  1359. return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
  1360. }
  1361. /*
  1362. * Set a socket option. Because we don't know the option lengths we have
  1363. * to pass the user mode parameter for the protocols to sort out.
  1364. */
  1365. asmlinkage long sys_setsockopt(int fd, int level, int optname, char __user *optval, int optlen)
  1366. {
  1367. int err;
  1368. struct socket *sock;
  1369. if (optlen < 0)
  1370. return -EINVAL;
  1371. if ((sock = sockfd_lookup(fd, &err))!=NULL)
  1372. {
  1373. err = security_socket_setsockopt(sock,level,optname);
  1374. if (err) {
  1375. sockfd_put(sock);
  1376. return err;
  1377. }
  1378. if (level == SOL_SOCKET)
  1379. err=sock_setsockopt(sock,level,optname,optval,optlen);
  1380. else
  1381. err=sock->ops->setsockopt(sock, level, optname, optval, optlen);
  1382. sockfd_put(sock);
  1383. }
  1384. return err;
  1385. }
  1386. /*
  1387. * Get a socket option. Because we don't know the option lengths we have
  1388. * to pass a user mode parameter for the protocols to sort out.
  1389. */
  1390. asmlinkage long sys_getsockopt(int fd, int level, int optname, char __user *optval, int __user *optlen)
  1391. {
  1392. int err;
  1393. struct socket *sock;
  1394. if ((sock = sockfd_lookup(fd, &err))!=NULL)
  1395. {
  1396. err = security_socket_getsockopt(sock, level,
  1397. optname);
  1398. if (err) {
  1399. sockfd_put(sock);
  1400. return err;
  1401. }
  1402. if (level == SOL_SOCKET)
  1403. err=sock_getsockopt(sock,level,optname,optval,optlen);
  1404. else
  1405. err=sock->ops->getsockopt(sock, level, optname, optval, optlen);
  1406. sockfd_put(sock);
  1407. }
  1408. return err;
  1409. }
  1410. /*
  1411. * Shutdown a socket.
  1412. */
  1413. asmlinkage long sys_shutdown(int fd, int how)
  1414. {
  1415. int err;
  1416. struct socket *sock;
  1417. if ((sock = sockfd_lookup(fd, &err))!=NULL)
  1418. {
  1419. err = security_socket_shutdown(sock, how);
  1420. if (err) {
  1421. sockfd_put(sock);
  1422. return err;
  1423. }
  1424. err=sock->ops->shutdown(sock, how);
  1425. sockfd_put(sock);
  1426. }
  1427. return err;
  1428. }
  1429. /* A couple of helpful macros for getting the address of the 32/64 bit
  1430. * fields which are the same type (int / unsigned) on our platforms.
  1431. */
  1432. #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
  1433. #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
  1434. #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
  1435. /*
  1436. * BSD sendmsg interface
  1437. */
  1438. asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
  1439. {
  1440. struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg;
  1441. struct socket *sock;
  1442. char address[MAX_SOCK_ADDR];
  1443. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  1444. unsigned char ctl[sizeof(struct cmsghdr) + 20]; /* 20 is size of ipv6_pktinfo */
  1445. unsigned char *ctl_buf = ctl;
  1446. struct msghdr msg_sys;
  1447. int err, ctl_len, iov_size, total_len;
  1448. err = -EFAULT;
  1449. if (MSG_CMSG_COMPAT & flags) {
  1450. if (get_compat_msghdr(&msg_sys, msg_compat))
  1451. return -EFAULT;
  1452. } else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
  1453. return -EFAULT;
  1454. sock = sockfd_lookup(fd, &err);
  1455. if (!sock)
  1456. goto out;
  1457. /* do not move before msg_sys is valid */
  1458. err = -EMSGSIZE;
  1459. if (msg_sys.msg_iovlen > UIO_MAXIOV)
  1460. goto out_put;
  1461. /* Check whether to allocate the iovec area*/
  1462. err = -ENOMEM;
  1463. iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
  1464. if (msg_sys.msg_iovlen > UIO_FASTIOV) {
  1465. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1466. if (!iov)
  1467. goto out_put;
  1468. }
  1469. /* This will also move the address data into kernel space */
  1470. if (MSG_CMSG_COMPAT & flags) {
  1471. err = verify_compat_iovec(&msg_sys, iov, address, VERIFY_READ);
  1472. } else
  1473. err = verify_iovec(&msg_sys, iov, address, VERIFY_READ);
  1474. if (err < 0)
  1475. goto out_freeiov;
  1476. total_len = err;
  1477. err = -ENOBUFS;
  1478. if (msg_sys.msg_controllen > INT_MAX)
  1479. goto out_freeiov;
  1480. ctl_len = msg_sys.msg_controllen;
  1481. if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
  1482. err = cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl, sizeof(ctl));
  1483. if (err)
  1484. goto out_freeiov;
  1485. ctl_buf = msg_sys.msg_control;
  1486. ctl_len = msg_sys.msg_controllen;
  1487. } else if (ctl_len) {
  1488. if (ctl_len > sizeof(ctl))
  1489. {
  1490. ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
  1491. if (ctl_buf == NULL)
  1492. goto out_freeiov;
  1493. }
  1494. err = -EFAULT;
  1495. /*
  1496. * Careful! Before this, msg_sys.msg_control contains a user pointer.
  1497. * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
  1498. * checking falls down on this.
  1499. */
  1500. if (copy_from_user(ctl_buf, (void __user *) msg_sys.msg_control, ctl_len))
  1501. goto out_freectl;
  1502. msg_sys.msg_control = ctl_buf;
  1503. }
  1504. msg_sys.msg_flags = flags;
  1505. if (sock->file->f_flags & O_NONBLOCK)
  1506. msg_sys.msg_flags |= MSG_DONTWAIT;
  1507. err = sock_sendmsg(sock, &msg_sys, total_len);
  1508. out_freectl:
  1509. if (ctl_buf != ctl)
  1510. sock_kfree_s(sock->sk, ctl_buf, ctl_len);
  1511. out_freeiov:
  1512. if (iov != iovstack)
  1513. sock_kfree_s(sock->sk, iov, iov_size);
  1514. out_put:
  1515. sockfd_put(sock);
  1516. out:
  1517. return err;
  1518. }
  1519. /*
  1520. * BSD recvmsg interface
  1521. */
  1522. asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg, unsigned int flags)
  1523. {
  1524. struct compat_msghdr __user *msg_compat = (struct compat_msghdr __user *)msg;
  1525. struct socket *sock;
  1526. struct iovec iovstack[UIO_FASTIOV];
  1527. struct iovec *iov=iovstack;
  1528. struct msghdr msg_sys;
  1529. unsigned long cmsg_ptr;
  1530. int err, iov_size, total_len, len;
  1531. /* kernel mode address */
  1532. char addr[MAX_SOCK_ADDR];
  1533. /* user mode address pointers */
  1534. struct sockaddr __user *uaddr;
  1535. int __user *uaddr_len;
  1536. if (MSG_CMSG_COMPAT & flags) {
  1537. if (get_compat_msghdr(&msg_sys, msg_compat))
  1538. return -EFAULT;
  1539. } else
  1540. if (copy_from_user(&msg_sys,msg,sizeof(struct msghdr)))
  1541. return -EFAULT;
  1542. sock = sockfd_lookup(fd, &err);
  1543. if (!sock)
  1544. goto out;
  1545. err = -EMSGSIZE;
  1546. if (msg_sys.msg_iovlen > UIO_MAXIOV)
  1547. goto out_put;
  1548. /* Check whether to allocate the iovec area*/
  1549. err = -ENOMEM;
  1550. iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
  1551. if (msg_sys.msg_iovlen > UIO_FASTIOV) {
  1552. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1553. if (!iov)
  1554. goto out_put;
  1555. }
  1556. /*
  1557. * Save the user-mode address (verify_iovec will change the
  1558. * kernel msghdr to use the kernel address space)
  1559. */
  1560. uaddr = (void __user *) msg_sys.msg_name;
  1561. uaddr_len = COMPAT_NAMELEN(msg);
  1562. if (MSG_CMSG_COMPAT & flags) {
  1563. err = verify_compat_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
  1564. } else
  1565. err = verify_iovec(&msg_sys, iov, addr, VERIFY_WRITE);
  1566. if (err < 0)
  1567. goto out_freeiov;
  1568. total_len=err;
  1569. cmsg_ptr = (unsigned long)msg_sys.msg_control;
  1570. msg_sys.msg_flags = 0;
  1571. if (MSG_CMSG_COMPAT & flags)
  1572. msg_sys.msg_flags = MSG_CMSG_COMPAT;
  1573. if (sock->file->f_flags & O_NONBLOCK)
  1574. flags |= MSG_DONTWAIT;
  1575. err = sock_recvmsg(sock, &msg_sys, total_len, flags);
  1576. if (err < 0)
  1577. goto out_freeiov;
  1578. len = err;
  1579. if (uaddr != NULL) {
  1580. err = move_addr_to_user(addr, msg_sys.msg_namelen, uaddr, uaddr_len);
  1581. if (err < 0)
  1582. goto out_freeiov;
  1583. }
  1584. err = __put_user(msg_sys.msg_flags, COMPAT_FLAGS(msg));
  1585. if (err)
  1586. goto out_freeiov;
  1587. if (MSG_CMSG_COMPAT & flags)
  1588. err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr,
  1589. &msg_compat->msg_controllen);
  1590. else
  1591. err = __put_user((unsigned long)msg_sys.msg_control-cmsg_ptr,
  1592. &msg->msg_controllen);
  1593. if (err)
  1594. goto out_freeiov;
  1595. err = len;
  1596. out_freeiov:
  1597. if (iov != iovstack)
  1598. sock_kfree_s(sock->sk, iov, iov_size);
  1599. out_put:
  1600. sockfd_put(sock);
  1601. out:
  1602. return err;
  1603. }
  1604. #ifdef __ARCH_WANT_SYS_SOCKETCALL
  1605. /* Argument list sizes for sys_socketcall */
  1606. #define AL(x) ((x) * sizeof(unsigned long))
  1607. static unsigned char nargs[18]={AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
  1608. AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
  1609. AL(6),AL(2),AL(5),AL(5),AL(3),AL(3)};
  1610. #undef AL
  1611. /*
  1612. * System call vectors.
  1613. *
  1614. * Argument checking cleaned up. Saved 20% in size.
  1615. * This function doesn't need to set the kernel lock because
  1616. * it is set by the callees.
  1617. */
  1618. asmlinkage long sys_socketcall(int call, unsigned long __user *args)
  1619. {
  1620. unsigned long a[6];
  1621. unsigned long a0,a1;
  1622. int err;
  1623. if(call<1||call>SYS_RECVMSG)
  1624. return -EINVAL;
  1625. /* copy_from_user should be SMP safe. */
  1626. if (copy_from_user(a, args, nargs[call]))
  1627. return -EFAULT;
  1628. err = audit_socketcall(nargs[call]/sizeof(unsigned long), a);
  1629. if (err)
  1630. return err;
  1631. a0=a[0];
  1632. a1=a[1];
  1633. switch(call)
  1634. {
  1635. case SYS_SOCKET:
  1636. err = sys_socket(a0,a1,a[2]);
  1637. break;
  1638. case SYS_BIND:
  1639. err = sys_bind(a0,(struct sockaddr __user *)a1, a[2]);
  1640. break;
  1641. case SYS_CONNECT:
  1642. err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
  1643. break;
  1644. case SYS_LISTEN:
  1645. err = sys_listen(a0,a1);
  1646. break;
  1647. case SYS_ACCEPT:
  1648. err = sys_accept(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
  1649. break;
  1650. case SYS_GETSOCKNAME:
  1651. err = sys_getsockname(a0,(struct sockaddr __user *)a1, (int __user *)a[2]);
  1652. break;
  1653. case SYS_GETPEERNAME:
  1654. err = sys_getpeername(a0, (struct sockaddr __user *)a1, (int __user *)a[2]);
  1655. break;
  1656. case SYS_SOCKETPAIR:
  1657. err = sys_socketpair(a0,a1, a[2], (int __user *)a[3]);
  1658. break;
  1659. case SYS_SEND:
  1660. err = sys_send(a0, (void __user *)a1, a[2], a[3]);
  1661. break;
  1662. case SYS_SENDTO:
  1663. err = sys_sendto(a0,(void __user *)a1, a[2], a[3],
  1664. (struct sockaddr __user *)a[4], a[5]);
  1665. break;
  1666. case SYS_RECV:
  1667. err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
  1668. break;
  1669. case SYS_RECVFROM:
  1670. err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  1671. (struct sockaddr __user *)a[4], (int __user *)a[5]);
  1672. break;
  1673. case SYS_SHUTDOWN:
  1674. err = sys_shutdown(a0,a1);
  1675. break;
  1676. case SYS_SETSOCKOPT:
  1677. err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
  1678. break;
  1679. case SYS_GETSOCKOPT:
  1680. err = sys_getsockopt(a0, a1, a[2], (char __user *)a[3], (int __user *)a[4]);
  1681. break;
  1682. case SYS_SENDMSG:
  1683. err = sys_sendmsg(a0, (struct msghdr __user *) a1, a[2]);
  1684. break;
  1685. case SYS_RECVMSG:
  1686. err = sys_recvmsg(a0, (struct msghdr __user *) a1, a[2]);
  1687. break;
  1688. default:
  1689. err = -EINVAL;
  1690. break;
  1691. }
  1692. return err;
  1693. }
  1694. #endif /* __ARCH_WANT_SYS_SOCKETCALL */
  1695. /*
  1696. * This function is called by a protocol handler that wants to
  1697. * advertise its address family, and have it linked into the
  1698. * SOCKET module.
  1699. */
  1700. int sock_register(struct net_proto_family *ops)
  1701. {
  1702. int err;
  1703. if (ops->family >= NPROTO) {
  1704. printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
  1705. return -ENOBUFS;
  1706. }
  1707. net_family_write_lock();
  1708. err = -EEXIST;
  1709. if (net_families[ops->family] == NULL) {
  1710. net_families[ops->family]=ops;
  1711. err = 0;
  1712. }
  1713. net_family_write_unlock();
  1714. printk(KERN_INFO "NET: Registered protocol family %d\n",
  1715. ops->family);
  1716. return err;
  1717. }
  1718. /*
  1719. * This function is called by a protocol handler that wants to
  1720. * remove its address family, and have it unlinked from the
  1721. * SOCKET module.
  1722. */
  1723. int sock_unregister(int family)
  1724. {
  1725. if (family < 0 || family >= NPROTO)
  1726. return -1;
  1727. net_family_write_lock();
  1728. net_families[family]=NULL;
  1729. net_family_write_unlock();
  1730. printk(KERN_INFO "NET: Unregistered protocol family %d\n",
  1731. family);
  1732. return 0;
  1733. }
  1734. void __init sock_init(void)
  1735. {
  1736. /*
  1737. * Initialize sock SLAB cache.
  1738. */
  1739. sk_init();
  1740. #ifdef SLAB_SKB
  1741. /*
  1742. * Initialize skbuff SLAB cache
  1743. */
  1744. skb_init();
  1745. #endif
  1746. /*
  1747. * Initialize the protocols module.
  1748. */
  1749. init_inodecache();
  1750. register_filesystem(&sock_fs_type);
  1751. sock_mnt = kern_mount(&sock_fs_type);
  1752. /* The real protocol initialization is performed when
  1753. * do_initcalls is run.
  1754. */
  1755. #ifdef CONFIG_NETFILTER
  1756. netfilter_init();
  1757. #endif
  1758. }
  1759. #ifdef CONFIG_PROC_FS
  1760. void socket_seq_show(struct seq_file *seq)
  1761. {
  1762. int cpu;
  1763. int counter = 0;
  1764. for (cpu = 0; cpu < NR_CPUS; cpu++)
  1765. counter += per_cpu(sockets_in_use, cpu);
  1766. /* It can be negative, by the way. 8) */
  1767. if (counter < 0)
  1768. counter = 0;
  1769. seq_printf(seq, "sockets: used %d\n", counter);
  1770. }
  1771. #endif /* CONFIG_PROC_FS */
  1772. /* ABI emulation layers need these two */
  1773. EXPORT_SYMBOL(move_addr_to_kernel);
  1774. EXPORT_SYMBOL(move_addr_to_user);
  1775. EXPORT_SYMBOL(sock_create);
  1776. EXPORT_SYMBOL(sock_create_kern);
  1777. EXPORT_SYMBOL(sock_create_lite);
  1778. EXPORT_SYMBOL(sock_map_fd);
  1779. EXPORT_SYMBOL(sock_recvmsg);
  1780. EXPORT_SYMBOL(sock_register);
  1781. EXPORT_SYMBOL(sock_release);
  1782. EXPORT_SYMBOL(sock_sendmsg);
  1783. EXPORT_SYMBOL(sock_unregister);
  1784. EXPORT_SYMBOL(sock_wake_async);
  1785. EXPORT_SYMBOL(sockfd_lookup);
  1786. EXPORT_SYMBOL(kernel_sendmsg);
  1787. EXPORT_SYMBOL(kernel_recvmsg);