af_inet.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * PF_INET protocol family socket handler.
  7. *
  8. * Authors: Ross Biro
  9. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10. * Florian La Roche, <flla@stud.uni-sb.de>
  11. * Alan Cox, <A.Cox@swansea.ac.uk>
  12. *
  13. * Changes (see also sock.c)
  14. *
  15. * piggy,
  16. * Karl Knutson : Socket protocol table
  17. * A.N.Kuznetsov : Socket death error in accept().
  18. * John Richardson : Fix non blocking error in connect()
  19. * so sockets that fail to connect
  20. * don't return -EINPROGRESS.
  21. * Alan Cox : Asynchronous I/O support
  22. * Alan Cox : Keep correct socket pointer on sock
  23. * structures
  24. * when accept() ed
  25. * Alan Cox : Semantics of SO_LINGER aren't state
  26. * moved to close when you look carefully.
  27. * With this fixed and the accept bug fixed
  28. * some RPC stuff seems happier.
  29. * Niibe Yutaka : 4.4BSD style write async I/O
  30. * Alan Cox,
  31. * Tony Gale : Fixed reuse semantics.
  32. * Alan Cox : bind() shouldn't abort existing but dead
  33. * sockets. Stops FTP netin:.. I hope.
  34. * Alan Cox : bind() works correctly for RAW sockets.
  35. * Note that FreeBSD at least was broken
  36. * in this respect so be careful with
  37. * compatibility tests...
  38. * Alan Cox : routing cache support
  39. * Alan Cox : memzero the socket structure for
  40. * compactness.
  41. * Matt Day : nonblock connect error handler
  42. * Alan Cox : Allow large numbers of pending sockets
  43. * (eg for big web sites), but only if
  44. * specifically application requested.
  45. * Alan Cox : New buffering throughout IP. Used
  46. * dumbly.
  47. * Alan Cox : New buffering now used smartly.
  48. * Alan Cox : BSD rather than common sense
  49. * interpretation of listen.
  50. * Germano Caronni : Assorted small races.
  51. * Alan Cox : sendmsg/recvmsg basic support.
  52. * Alan Cox : Only sendmsg/recvmsg now supported.
  53. * Alan Cox : Locked down bind (see security list).
  54. * Alan Cox : Loosened bind a little.
  55. * Mike McLagan : ADD/DEL DLCI Ioctls
  56. * Willy Konynenberg : Transparent proxying support.
  57. * David S. Miller : New socket lookup architecture.
  58. * Some other random speedups.
  59. * Cyrus Durgin : Cleaned up file for kmod hacks.
  60. * Andi Kleen : Fix inet_stream_connect TCP race.
  61. *
  62. * This program is free software; you can redistribute it and/or
  63. * modify it under the terms of the GNU General Public License
  64. * as published by the Free Software Foundation; either version
  65. * 2 of the License, or (at your option) any later version.
  66. */
  67. #include <linux/err.h>
  68. #include <linux/errno.h>
  69. #include <linux/types.h>
  70. #include <linux/socket.h>
  71. #include <linux/in.h>
  72. #include <linux/kernel.h>
  73. #include <linux/module.h>
  74. #include <linux/sched.h>
  75. #include <linux/timer.h>
  76. #include <linux/string.h>
  77. #include <linux/sockios.h>
  78. #include <linux/net.h>
  79. #include <linux/capability.h>
  80. #include <linux/fcntl.h>
  81. #include <linux/mm.h>
  82. #include <linux/interrupt.h>
  83. #include <linux/stat.h>
  84. #include <linux/init.h>
  85. #include <linux/poll.h>
  86. #include <linux/netfilter_ipv4.h>
  87. #include <linux/random.h>
  88. #include <asm/uaccess.h>
  89. #include <asm/system.h>
  90. #include <linux/inet.h>
  91. #include <linux/igmp.h>
  92. #include <linux/inetdevice.h>
  93. #include <linux/netdevice.h>
  94. #include <net/ip.h>
  95. #include <net/protocol.h>
  96. #include <net/arp.h>
  97. #include <net/route.h>
  98. #include <net/ip_fib.h>
  99. #include <net/inet_connection_sock.h>
  100. #include <net/tcp.h>
  101. #include <net/udp.h>
  102. #include <net/udplite.h>
  103. #include <linux/skbuff.h>
  104. #include <net/sock.h>
  105. #include <net/raw.h>
  106. #include <net/icmp.h>
  107. #include <net/ipip.h>
  108. #include <net/inet_common.h>
  109. #include <net/xfrm.h>
  110. #include <net/net_namespace.h>
  111. #ifdef CONFIG_IP_MROUTE
  112. #include <linux/mroute.h>
  113. #endif
  114. extern void ip_mc_drop_socket(struct sock *sk);
  115. /* The inetsw table contains everything that inet_create needs to
  116. * build a new socket.
  117. */
  118. static struct list_head inetsw[SOCK_MAX];
  119. static DEFINE_SPINLOCK(inetsw_lock);
  120. struct ipv4_config ipv4_config;
  121. EXPORT_SYMBOL(ipv4_config);
  122. /* New destruction routine */
  123. void inet_sock_destruct(struct sock *sk)
  124. {
  125. struct inet_sock *inet = inet_sk(sk);
  126. __skb_queue_purge(&sk->sk_receive_queue);
  127. __skb_queue_purge(&sk->sk_error_queue);
  128. sk_mem_reclaim(sk);
  129. if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
  130. printk("Attempt to release TCP socket in state %d %p\n",
  131. sk->sk_state, sk);
  132. return;
  133. }
  134. if (!sock_flag(sk, SOCK_DEAD)) {
  135. printk("Attempt to release alive inet socket %p\n", sk);
  136. return;
  137. }
  138. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  139. WARN_ON(atomic_read(&sk->sk_wmem_alloc));
  140. WARN_ON(sk->sk_wmem_queued);
  141. WARN_ON(sk->sk_forward_alloc);
  142. kfree(inet->opt);
  143. dst_release(sk->sk_dst_cache);
  144. sk_refcnt_debug_dec(sk);
  145. }
  146. /*
  147. * The routines beyond this point handle the behaviour of an AF_INET
  148. * socket object. Mostly it punts to the subprotocols of IP to do
  149. * the work.
  150. */
  151. /*
  152. * Automatically bind an unbound socket.
  153. */
  154. static int inet_autobind(struct sock *sk)
  155. {
  156. struct inet_sock *inet;
  157. /* We may need to bind the socket. */
  158. lock_sock(sk);
  159. inet = inet_sk(sk);
  160. if (!inet->num) {
  161. if (sk->sk_prot->get_port(sk, 0)) {
  162. release_sock(sk);
  163. return -EAGAIN;
  164. }
  165. inet->sport = htons(inet->num);
  166. }
  167. release_sock(sk);
  168. return 0;
  169. }
  170. /*
  171. * Move a socket into listening state.
  172. */
  173. int inet_listen(struct socket *sock, int backlog)
  174. {
  175. struct sock *sk = sock->sk;
  176. unsigned char old_state;
  177. int err;
  178. lock_sock(sk);
  179. err = -EINVAL;
  180. if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
  181. goto out;
  182. old_state = sk->sk_state;
  183. if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
  184. goto out;
  185. /* Really, if the socket is already in listen state
  186. * we can only allow the backlog to be adjusted.
  187. */
  188. if (old_state != TCP_LISTEN) {
  189. err = inet_csk_listen_start(sk, backlog);
  190. if (err)
  191. goto out;
  192. }
  193. sk->sk_max_ack_backlog = backlog;
  194. err = 0;
  195. out:
  196. release_sock(sk);
  197. return err;
  198. }
  199. u32 inet_ehash_secret __read_mostly;
  200. EXPORT_SYMBOL(inet_ehash_secret);
  201. /*
  202. * inet_ehash_secret must be set exactly once
  203. * Instead of using a dedicated spinlock, we (ab)use inetsw_lock
  204. */
  205. void build_ehash_secret(void)
  206. {
  207. u32 rnd;
  208. do {
  209. get_random_bytes(&rnd, sizeof(rnd));
  210. } while (rnd == 0);
  211. spin_lock_bh(&inetsw_lock);
  212. if (!inet_ehash_secret)
  213. inet_ehash_secret = rnd;
  214. spin_unlock_bh(&inetsw_lock);
  215. }
  216. EXPORT_SYMBOL(build_ehash_secret);
  217. static inline int inet_netns_ok(struct net *net, int protocol)
  218. {
  219. int hash;
  220. struct net_protocol *ipprot;
  221. if (net == &init_net)
  222. return 1;
  223. hash = protocol & (MAX_INET_PROTOS - 1);
  224. ipprot = rcu_dereference(inet_protos[hash]);
  225. if (ipprot == NULL)
  226. /* raw IP is OK */
  227. return 1;
  228. return ipprot->netns_ok;
  229. }
  230. /*
  231. * Create an inet socket.
  232. */
  233. static int inet_create(struct net *net, struct socket *sock, int protocol)
  234. {
  235. struct sock *sk;
  236. struct inet_protosw *answer;
  237. struct inet_sock *inet;
  238. struct proto *answer_prot;
  239. unsigned char answer_flags;
  240. char answer_no_check;
  241. int try_loading_module = 0;
  242. int err;
  243. if (sock->type != SOCK_RAW &&
  244. sock->type != SOCK_DGRAM &&
  245. !inet_ehash_secret)
  246. build_ehash_secret();
  247. sock->state = SS_UNCONNECTED;
  248. /* Look for the requested type/protocol pair. */
  249. lookup_protocol:
  250. err = -ESOCKTNOSUPPORT;
  251. rcu_read_lock();
  252. list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {
  253. err = 0;
  254. /* Check the non-wild match. */
  255. if (protocol == answer->protocol) {
  256. if (protocol != IPPROTO_IP)
  257. break;
  258. } else {
  259. /* Check for the two wild cases. */
  260. if (IPPROTO_IP == protocol) {
  261. protocol = answer->protocol;
  262. break;
  263. }
  264. if (IPPROTO_IP == answer->protocol)
  265. break;
  266. }
  267. err = -EPROTONOSUPPORT;
  268. }
  269. if (unlikely(err)) {
  270. if (try_loading_module < 2) {
  271. rcu_read_unlock();
  272. /*
  273. * Be more specific, e.g. net-pf-2-proto-132-type-1
  274. * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
  275. */
  276. if (++try_loading_module == 1)
  277. request_module("net-pf-%d-proto-%d-type-%d",
  278. PF_INET, protocol, sock->type);
  279. /*
  280. * Fall back to generic, e.g. net-pf-2-proto-132
  281. * (net-pf-PF_INET-proto-IPPROTO_SCTP)
  282. */
  283. else
  284. request_module("net-pf-%d-proto-%d",
  285. PF_INET, protocol);
  286. goto lookup_protocol;
  287. } else
  288. goto out_rcu_unlock;
  289. }
  290. err = -EPERM;
  291. if (answer->capability > 0 && !capable(answer->capability))
  292. goto out_rcu_unlock;
  293. err = -EAFNOSUPPORT;
  294. if (!inet_netns_ok(net, protocol))
  295. goto out_rcu_unlock;
  296. sock->ops = answer->ops;
  297. answer_prot = answer->prot;
  298. answer_no_check = answer->no_check;
  299. answer_flags = answer->flags;
  300. rcu_read_unlock();
  301. WARN_ON(answer_prot->slab == NULL);
  302. err = -ENOBUFS;
  303. sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot);
  304. if (sk == NULL)
  305. goto out;
  306. err = 0;
  307. sk->sk_no_check = answer_no_check;
  308. if (INET_PROTOSW_REUSE & answer_flags)
  309. sk->sk_reuse = 1;
  310. inet = inet_sk(sk);
  311. inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
  312. if (SOCK_RAW == sock->type) {
  313. inet->num = protocol;
  314. if (IPPROTO_RAW == protocol)
  315. inet->hdrincl = 1;
  316. }
  317. if (ipv4_config.no_pmtu_disc)
  318. inet->pmtudisc = IP_PMTUDISC_DONT;
  319. else
  320. inet->pmtudisc = IP_PMTUDISC_WANT;
  321. inet->id = 0;
  322. sock_init_data(sock, sk);
  323. sk->sk_destruct = inet_sock_destruct;
  324. sk->sk_family = PF_INET;
  325. sk->sk_protocol = protocol;
  326. sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  327. inet->uc_ttl = -1;
  328. inet->mc_loop = 1;
  329. inet->mc_ttl = 1;
  330. inet->mc_index = 0;
  331. inet->mc_list = NULL;
  332. sk_refcnt_debug_inc(sk);
  333. if (inet->num) {
  334. /* It assumes that any protocol which allows
  335. * the user to assign a number at socket
  336. * creation time automatically
  337. * shares.
  338. */
  339. inet->sport = htons(inet->num);
  340. /* Add to protocol hash chains. */
  341. sk->sk_prot->hash(sk);
  342. }
  343. if (sk->sk_prot->init) {
  344. err = sk->sk_prot->init(sk);
  345. if (err)
  346. sk_common_release(sk);
  347. }
  348. out:
  349. return err;
  350. out_rcu_unlock:
  351. rcu_read_unlock();
  352. goto out;
  353. }
  354. /*
  355. * The peer socket should always be NULL (or else). When we call this
  356. * function we are destroying the object and from then on nobody
  357. * should refer to it.
  358. */
  359. int inet_release(struct socket *sock)
  360. {
  361. struct sock *sk = sock->sk;
  362. if (sk) {
  363. long timeout;
  364. /* Applications forget to leave groups before exiting */
  365. ip_mc_drop_socket(sk);
  366. /* If linger is set, we don't return until the close
  367. * is complete. Otherwise we return immediately. The
  368. * actually closing is done the same either way.
  369. *
  370. * If the close is due to the process exiting, we never
  371. * linger..
  372. */
  373. timeout = 0;
  374. if (sock_flag(sk, SOCK_LINGER) &&
  375. !(current->flags & PF_EXITING))
  376. timeout = sk->sk_lingertime;
  377. sock->sk = NULL;
  378. sk->sk_prot->close(sk, timeout);
  379. }
  380. return 0;
  381. }
  382. /* It is off by default, see below. */
  383. int sysctl_ip_nonlocal_bind __read_mostly;
  384. int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  385. {
  386. struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
  387. struct sock *sk = sock->sk;
  388. struct inet_sock *inet = inet_sk(sk);
  389. unsigned short snum;
  390. int chk_addr_ret;
  391. int err;
  392. /* If the socket has its own bind function then use it. (RAW) */
  393. if (sk->sk_prot->bind) {
  394. err = sk->sk_prot->bind(sk, uaddr, addr_len);
  395. goto out;
  396. }
  397. err = -EINVAL;
  398. if (addr_len < sizeof(struct sockaddr_in))
  399. goto out;
  400. chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
  401. /* Not specified by any standard per-se, however it breaks too
  402. * many applications when removed. It is unfortunate since
  403. * allowing applications to make a non-local bind solves
  404. * several problems with systems using dynamic addressing.
  405. * (ie. your servers still start up even if your ISDN link
  406. * is temporarily down)
  407. */
  408. err = -EADDRNOTAVAIL;
  409. if (!sysctl_ip_nonlocal_bind &&
  410. !(inet->freebind || inet->transparent) &&
  411. addr->sin_addr.s_addr != htonl(INADDR_ANY) &&
  412. chk_addr_ret != RTN_LOCAL &&
  413. chk_addr_ret != RTN_MULTICAST &&
  414. chk_addr_ret != RTN_BROADCAST)
  415. goto out;
  416. snum = ntohs(addr->sin_port);
  417. err = -EACCES;
  418. if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  419. goto out;
  420. /* We keep a pair of addresses. rcv_saddr is the one
  421. * used by hash lookups, and saddr is used for transmit.
  422. *
  423. * In the BSD API these are the same except where it
  424. * would be illegal to use them (multicast/broadcast) in
  425. * which case the sending device address is used.
  426. */
  427. lock_sock(sk);
  428. /* Check these errors (active socket, double bind). */
  429. err = -EINVAL;
  430. if (sk->sk_state != TCP_CLOSE || inet->num)
  431. goto out_release_sock;
  432. inet->rcv_saddr = inet->saddr = addr->sin_addr.s_addr;
  433. if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
  434. inet->saddr = 0; /* Use device */
  435. /* Make sure we are allowed to bind here. */
  436. if (sk->sk_prot->get_port(sk, snum)) {
  437. inet->saddr = inet->rcv_saddr = 0;
  438. err = -EADDRINUSE;
  439. goto out_release_sock;
  440. }
  441. if (inet->rcv_saddr)
  442. sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
  443. if (snum)
  444. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  445. inet->sport = htons(inet->num);
  446. inet->daddr = 0;
  447. inet->dport = 0;
  448. sk_dst_reset(sk);
  449. err = 0;
  450. out_release_sock:
  451. release_sock(sk);
  452. out:
  453. return err;
  454. }
  455. int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
  456. int addr_len, int flags)
  457. {
  458. struct sock *sk = sock->sk;
  459. if (uaddr->sa_family == AF_UNSPEC)
  460. return sk->sk_prot->disconnect(sk, flags);
  461. if (!inet_sk(sk)->num && inet_autobind(sk))
  462. return -EAGAIN;
  463. return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
  464. }
  465. static long inet_wait_for_connect(struct sock *sk, long timeo)
  466. {
  467. DEFINE_WAIT(wait);
  468. prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  469. /* Basic assumption: if someone sets sk->sk_err, he _must_
  470. * change state of the socket from TCP_SYN_*.
  471. * Connect() does not allow to get error notifications
  472. * without closing the socket.
  473. */
  474. while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
  475. release_sock(sk);
  476. timeo = schedule_timeout(timeo);
  477. lock_sock(sk);
  478. if (signal_pending(current) || !timeo)
  479. break;
  480. prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  481. }
  482. finish_wait(sk->sk_sleep, &wait);
  483. return timeo;
  484. }
  485. /*
  486. * Connect to a remote host. There is regrettably still a little
  487. * TCP 'magic' in here.
  488. */
  489. int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
  490. int addr_len, int flags)
  491. {
  492. struct sock *sk = sock->sk;
  493. int err;
  494. long timeo;
  495. lock_sock(sk);
  496. if (uaddr->sa_family == AF_UNSPEC) {
  497. err = sk->sk_prot->disconnect(sk, flags);
  498. sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
  499. goto out;
  500. }
  501. switch (sock->state) {
  502. default:
  503. err = -EINVAL;
  504. goto out;
  505. case SS_CONNECTED:
  506. err = -EISCONN;
  507. goto out;
  508. case SS_CONNECTING:
  509. err = -EALREADY;
  510. /* Fall out of switch with err, set for this state */
  511. break;
  512. case SS_UNCONNECTED:
  513. err = -EISCONN;
  514. if (sk->sk_state != TCP_CLOSE)
  515. goto out;
  516. err = sk->sk_prot->connect(sk, uaddr, addr_len);
  517. if (err < 0)
  518. goto out;
  519. sock->state = SS_CONNECTING;
  520. /* Just entered SS_CONNECTING state; the only
  521. * difference is that return value in non-blocking
  522. * case is EINPROGRESS, rather than EALREADY.
  523. */
  524. err = -EINPROGRESS;
  525. break;
  526. }
  527. timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
  528. if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
  529. /* Error code is set above */
  530. if (!timeo || !inet_wait_for_connect(sk, timeo))
  531. goto out;
  532. err = sock_intr_errno(timeo);
  533. if (signal_pending(current))
  534. goto out;
  535. }
  536. /* Connection was closed by RST, timeout, ICMP error
  537. * or another process disconnected us.
  538. */
  539. if (sk->sk_state == TCP_CLOSE)
  540. goto sock_error;
  541. /* sk->sk_err may be not zero now, if RECVERR was ordered by user
  542. * and error was received after socket entered established state.
  543. * Hence, it is handled normally after connect() return successfully.
  544. */
  545. sock->state = SS_CONNECTED;
  546. err = 0;
  547. out:
  548. release_sock(sk);
  549. return err;
  550. sock_error:
  551. err = sock_error(sk) ? : -ECONNABORTED;
  552. sock->state = SS_UNCONNECTED;
  553. if (sk->sk_prot->disconnect(sk, flags))
  554. sock->state = SS_DISCONNECTING;
  555. goto out;
  556. }
  557. /*
  558. * Accept a pending connection. The TCP layer now gives BSD semantics.
  559. */
  560. int inet_accept(struct socket *sock, struct socket *newsock, int flags)
  561. {
  562. struct sock *sk1 = sock->sk;
  563. int err = -EINVAL;
  564. struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
  565. if (!sk2)
  566. goto do_err;
  567. lock_sock(sk2);
  568. WARN_ON(!((1 << sk2->sk_state) &
  569. (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE)));
  570. sock_graft(sk2, newsock);
  571. newsock->state = SS_CONNECTED;
  572. err = 0;
  573. release_sock(sk2);
  574. do_err:
  575. return err;
  576. }
  577. /*
  578. * This does both peername and sockname.
  579. */
  580. int inet_getname(struct socket *sock, struct sockaddr *uaddr,
  581. int *uaddr_len, int peer)
  582. {
  583. struct sock *sk = sock->sk;
  584. struct inet_sock *inet = inet_sk(sk);
  585. struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
  586. sin->sin_family = AF_INET;
  587. if (peer) {
  588. if (!inet->dport ||
  589. (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
  590. peer == 1))
  591. return -ENOTCONN;
  592. sin->sin_port = inet->dport;
  593. sin->sin_addr.s_addr = inet->daddr;
  594. } else {
  595. __be32 addr = inet->rcv_saddr;
  596. if (!addr)
  597. addr = inet->saddr;
  598. sin->sin_port = inet->sport;
  599. sin->sin_addr.s_addr = addr;
  600. }
  601. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  602. *uaddr_len = sizeof(*sin);
  603. return 0;
  604. }
  605. int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
  606. size_t size)
  607. {
  608. struct sock *sk = sock->sk;
  609. /* We may need to bind the socket. */
  610. if (!inet_sk(sk)->num && inet_autobind(sk))
  611. return -EAGAIN;
  612. return sk->sk_prot->sendmsg(iocb, sk, msg, size);
  613. }
  614. static ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
  615. {
  616. struct sock *sk = sock->sk;
  617. /* We may need to bind the socket. */
  618. if (!inet_sk(sk)->num && inet_autobind(sk))
  619. return -EAGAIN;
  620. if (sk->sk_prot->sendpage)
  621. return sk->sk_prot->sendpage(sk, page, offset, size, flags);
  622. return sock_no_sendpage(sock, page, offset, size, flags);
  623. }
  624. int inet_shutdown(struct socket *sock, int how)
  625. {
  626. struct sock *sk = sock->sk;
  627. int err = 0;
  628. /* This should really check to make sure
  629. * the socket is a TCP socket. (WHY AC...)
  630. */
  631. how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
  632. 1->2 bit 2 snds.
  633. 2->3 */
  634. if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */
  635. return -EINVAL;
  636. lock_sock(sk);
  637. if (sock->state == SS_CONNECTING) {
  638. if ((1 << sk->sk_state) &
  639. (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
  640. sock->state = SS_DISCONNECTING;
  641. else
  642. sock->state = SS_CONNECTED;
  643. }
  644. switch (sk->sk_state) {
  645. case TCP_CLOSE:
  646. err = -ENOTCONN;
  647. /* Hack to wake up other listeners, who can poll for
  648. POLLHUP, even on eg. unconnected UDP sockets -- RR */
  649. default:
  650. sk->sk_shutdown |= how;
  651. if (sk->sk_prot->shutdown)
  652. sk->sk_prot->shutdown(sk, how);
  653. break;
  654. /* Remaining two branches are temporary solution for missing
  655. * close() in multithreaded environment. It is _not_ a good idea,
  656. * but we have no choice until close() is repaired at VFS level.
  657. */
  658. case TCP_LISTEN:
  659. if (!(how & RCV_SHUTDOWN))
  660. break;
  661. /* Fall through */
  662. case TCP_SYN_SENT:
  663. err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
  664. sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
  665. break;
  666. }
  667. /* Wake up anyone sleeping in poll. */
  668. sk->sk_state_change(sk);
  669. release_sock(sk);
  670. return err;
  671. }
  672. /*
  673. * ioctl() calls you can issue on an INET socket. Most of these are
  674. * device configuration and stuff and very rarely used. Some ioctls
  675. * pass on to the socket itself.
  676. *
  677. * NOTE: I like the idea of a module for the config stuff. ie ifconfig
  678. * loads the devconfigure module does its configuring and unloads it.
  679. * There's a good 20K of config code hanging around the kernel.
  680. */
  681. int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  682. {
  683. struct sock *sk = sock->sk;
  684. int err = 0;
  685. struct net *net = sock_net(sk);
  686. switch (cmd) {
  687. case SIOCGSTAMP:
  688. err = sock_get_timestamp(sk, (struct timeval __user *)arg);
  689. break;
  690. case SIOCGSTAMPNS:
  691. err = sock_get_timestampns(sk, (struct timespec __user *)arg);
  692. break;
  693. case SIOCADDRT:
  694. case SIOCDELRT:
  695. case SIOCRTMSG:
  696. err = ip_rt_ioctl(net, cmd, (void __user *)arg);
  697. break;
  698. case SIOCDARP:
  699. case SIOCGARP:
  700. case SIOCSARP:
  701. err = arp_ioctl(net, cmd, (void __user *)arg);
  702. break;
  703. case SIOCGIFADDR:
  704. case SIOCSIFADDR:
  705. case SIOCGIFBRDADDR:
  706. case SIOCSIFBRDADDR:
  707. case SIOCGIFNETMASK:
  708. case SIOCSIFNETMASK:
  709. case SIOCGIFDSTADDR:
  710. case SIOCSIFDSTADDR:
  711. case SIOCSIFPFLAGS:
  712. case SIOCGIFPFLAGS:
  713. case SIOCSIFFLAGS:
  714. err = devinet_ioctl(net, cmd, (void __user *)arg);
  715. break;
  716. default:
  717. if (sk->sk_prot->ioctl)
  718. err = sk->sk_prot->ioctl(sk, cmd, arg);
  719. else
  720. err = -ENOIOCTLCMD;
  721. break;
  722. }
  723. return err;
  724. }
  725. const struct proto_ops inet_stream_ops = {
  726. .family = PF_INET,
  727. .owner = THIS_MODULE,
  728. .release = inet_release,
  729. .bind = inet_bind,
  730. .connect = inet_stream_connect,
  731. .socketpair = sock_no_socketpair,
  732. .accept = inet_accept,
  733. .getname = inet_getname,
  734. .poll = tcp_poll,
  735. .ioctl = inet_ioctl,
  736. .listen = inet_listen,
  737. .shutdown = inet_shutdown,
  738. .setsockopt = sock_common_setsockopt,
  739. .getsockopt = sock_common_getsockopt,
  740. .sendmsg = tcp_sendmsg,
  741. .recvmsg = sock_common_recvmsg,
  742. .mmap = sock_no_mmap,
  743. .sendpage = tcp_sendpage,
  744. .splice_read = tcp_splice_read,
  745. #ifdef CONFIG_COMPAT
  746. .compat_setsockopt = compat_sock_common_setsockopt,
  747. .compat_getsockopt = compat_sock_common_getsockopt,
  748. #endif
  749. };
  750. const struct proto_ops inet_dgram_ops = {
  751. .family = PF_INET,
  752. .owner = THIS_MODULE,
  753. .release = inet_release,
  754. .bind = inet_bind,
  755. .connect = inet_dgram_connect,
  756. .socketpair = sock_no_socketpair,
  757. .accept = sock_no_accept,
  758. .getname = inet_getname,
  759. .poll = udp_poll,
  760. .ioctl = inet_ioctl,
  761. .listen = sock_no_listen,
  762. .shutdown = inet_shutdown,
  763. .setsockopt = sock_common_setsockopt,
  764. .getsockopt = sock_common_getsockopt,
  765. .sendmsg = inet_sendmsg,
  766. .recvmsg = sock_common_recvmsg,
  767. .mmap = sock_no_mmap,
  768. .sendpage = inet_sendpage,
  769. #ifdef CONFIG_COMPAT
  770. .compat_setsockopt = compat_sock_common_setsockopt,
  771. .compat_getsockopt = compat_sock_common_getsockopt,
  772. #endif
  773. };
  774. /*
  775. * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
  776. * udp_poll
  777. */
  778. static const struct proto_ops inet_sockraw_ops = {
  779. .family = PF_INET,
  780. .owner = THIS_MODULE,
  781. .release = inet_release,
  782. .bind = inet_bind,
  783. .connect = inet_dgram_connect,
  784. .socketpair = sock_no_socketpair,
  785. .accept = sock_no_accept,
  786. .getname = inet_getname,
  787. .poll = datagram_poll,
  788. .ioctl = inet_ioctl,
  789. .listen = sock_no_listen,
  790. .shutdown = inet_shutdown,
  791. .setsockopt = sock_common_setsockopt,
  792. .getsockopt = sock_common_getsockopt,
  793. .sendmsg = inet_sendmsg,
  794. .recvmsg = sock_common_recvmsg,
  795. .mmap = sock_no_mmap,
  796. .sendpage = inet_sendpage,
  797. #ifdef CONFIG_COMPAT
  798. .compat_setsockopt = compat_sock_common_setsockopt,
  799. .compat_getsockopt = compat_sock_common_getsockopt,
  800. #endif
  801. };
  802. static struct net_proto_family inet_family_ops = {
  803. .family = PF_INET,
  804. .create = inet_create,
  805. .owner = THIS_MODULE,
  806. };
  807. /* Upon startup we insert all the elements in inetsw_array[] into
  808. * the linked list inetsw.
  809. */
  810. static struct inet_protosw inetsw_array[] =
  811. {
  812. {
  813. .type = SOCK_STREAM,
  814. .protocol = IPPROTO_TCP,
  815. .prot = &tcp_prot,
  816. .ops = &inet_stream_ops,
  817. .capability = -1,
  818. .no_check = 0,
  819. .flags = INET_PROTOSW_PERMANENT |
  820. INET_PROTOSW_ICSK,
  821. },
  822. {
  823. .type = SOCK_DGRAM,
  824. .protocol = IPPROTO_UDP,
  825. .prot = &udp_prot,
  826. .ops = &inet_dgram_ops,
  827. .capability = -1,
  828. .no_check = UDP_CSUM_DEFAULT,
  829. .flags = INET_PROTOSW_PERMANENT,
  830. },
  831. {
  832. .type = SOCK_RAW,
  833. .protocol = IPPROTO_IP, /* wild card */
  834. .prot = &raw_prot,
  835. .ops = &inet_sockraw_ops,
  836. .capability = CAP_NET_RAW,
  837. .no_check = UDP_CSUM_DEFAULT,
  838. .flags = INET_PROTOSW_REUSE,
  839. }
  840. };
  841. #define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array)
  842. void inet_register_protosw(struct inet_protosw *p)
  843. {
  844. struct list_head *lh;
  845. struct inet_protosw *answer;
  846. int protocol = p->protocol;
  847. struct list_head *last_perm;
  848. spin_lock_bh(&inetsw_lock);
  849. if (p->type >= SOCK_MAX)
  850. goto out_illegal;
  851. /* If we are trying to override a permanent protocol, bail. */
  852. answer = NULL;
  853. last_perm = &inetsw[p->type];
  854. list_for_each(lh, &inetsw[p->type]) {
  855. answer = list_entry(lh, struct inet_protosw, list);
  856. /* Check only the non-wild match. */
  857. if (INET_PROTOSW_PERMANENT & answer->flags) {
  858. if (protocol == answer->protocol)
  859. break;
  860. last_perm = lh;
  861. }
  862. answer = NULL;
  863. }
  864. if (answer)
  865. goto out_permanent;
  866. /* Add the new entry after the last permanent entry if any, so that
  867. * the new entry does not override a permanent entry when matched with
  868. * a wild-card protocol. But it is allowed to override any existing
  869. * non-permanent entry. This means that when we remove this entry, the
  870. * system automatically returns to the old behavior.
  871. */
  872. list_add_rcu(&p->list, last_perm);
  873. out:
  874. spin_unlock_bh(&inetsw_lock);
  875. synchronize_net();
  876. return;
  877. out_permanent:
  878. printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
  879. protocol);
  880. goto out;
  881. out_illegal:
  882. printk(KERN_ERR
  883. "Ignoring attempt to register invalid socket type %d.\n",
  884. p->type);
  885. goto out;
  886. }
  887. void inet_unregister_protosw(struct inet_protosw *p)
  888. {
  889. if (INET_PROTOSW_PERMANENT & p->flags) {
  890. printk(KERN_ERR
  891. "Attempt to unregister permanent protocol %d.\n",
  892. p->protocol);
  893. } else {
  894. spin_lock_bh(&inetsw_lock);
  895. list_del_rcu(&p->list);
  896. spin_unlock_bh(&inetsw_lock);
  897. synchronize_net();
  898. }
  899. }
  900. /*
  901. * Shall we try to damage output packets if routing dev changes?
  902. */
  903. int sysctl_ip_dynaddr __read_mostly;
  904. static int inet_sk_reselect_saddr(struct sock *sk)
  905. {
  906. struct inet_sock *inet = inet_sk(sk);
  907. int err;
  908. struct rtable *rt;
  909. __be32 old_saddr = inet->saddr;
  910. __be32 new_saddr;
  911. __be32 daddr = inet->daddr;
  912. if (inet->opt && inet->opt->srr)
  913. daddr = inet->opt->faddr;
  914. /* Query new route. */
  915. err = ip_route_connect(&rt, daddr, 0,
  916. RT_CONN_FLAGS(sk),
  917. sk->sk_bound_dev_if,
  918. sk->sk_protocol,
  919. inet->sport, inet->dport, sk, 0);
  920. if (err)
  921. return err;
  922. sk_setup_caps(sk, &rt->u.dst);
  923. new_saddr = rt->rt_src;
  924. if (new_saddr == old_saddr)
  925. return 0;
  926. if (sysctl_ip_dynaddr > 1) {
  927. printk(KERN_INFO "%s(): shifting inet->"
  928. "saddr from " NIPQUAD_FMT " to " NIPQUAD_FMT "\n",
  929. __func__,
  930. NIPQUAD(old_saddr),
  931. NIPQUAD(new_saddr));
  932. }
  933. inet->saddr = inet->rcv_saddr = new_saddr;
  934. /*
  935. * XXX The only one ugly spot where we need to
  936. * XXX really change the sockets identity after
  937. * XXX it has entered the hashes. -DaveM
  938. *
  939. * Besides that, it does not check for connection
  940. * uniqueness. Wait for troubles.
  941. */
  942. __sk_prot_rehash(sk);
  943. return 0;
  944. }
  945. int inet_sk_rebuild_header(struct sock *sk)
  946. {
  947. struct inet_sock *inet = inet_sk(sk);
  948. struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
  949. __be32 daddr;
  950. int err;
  951. /* Route is OK, nothing to do. */
  952. if (rt)
  953. return 0;
  954. /* Reroute. */
  955. daddr = inet->daddr;
  956. if (inet->opt && inet->opt->srr)
  957. daddr = inet->opt->faddr;
  958. {
  959. struct flowi fl = {
  960. .oif = sk->sk_bound_dev_if,
  961. .nl_u = {
  962. .ip4_u = {
  963. .daddr = daddr,
  964. .saddr = inet->saddr,
  965. .tos = RT_CONN_FLAGS(sk),
  966. },
  967. },
  968. .proto = sk->sk_protocol,
  969. .flags = inet_sk_flowi_flags(sk),
  970. .uli_u = {
  971. .ports = {
  972. .sport = inet->sport,
  973. .dport = inet->dport,
  974. },
  975. },
  976. };
  977. security_sk_classify_flow(sk, &fl);
  978. err = ip_route_output_flow(sock_net(sk), &rt, &fl, sk, 0);
  979. }
  980. if (!err)
  981. sk_setup_caps(sk, &rt->u.dst);
  982. else {
  983. /* Routing failed... */
  984. sk->sk_route_caps = 0;
  985. /*
  986. * Other protocols have to map its equivalent state to TCP_SYN_SENT.
  987. * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme
  988. */
  989. if (!sysctl_ip_dynaddr ||
  990. sk->sk_state != TCP_SYN_SENT ||
  991. (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
  992. (err = inet_sk_reselect_saddr(sk)) != 0)
  993. sk->sk_err_soft = -err;
  994. }
  995. return err;
  996. }
  997. EXPORT_SYMBOL(inet_sk_rebuild_header);
  998. static int inet_gso_send_check(struct sk_buff *skb)
  999. {
  1000. struct iphdr *iph;
  1001. struct net_protocol *ops;
  1002. int proto;
  1003. int ihl;
  1004. int err = -EINVAL;
  1005. if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
  1006. goto out;
  1007. iph = ip_hdr(skb);
  1008. ihl = iph->ihl * 4;
  1009. if (ihl < sizeof(*iph))
  1010. goto out;
  1011. if (unlikely(!pskb_may_pull(skb, ihl)))
  1012. goto out;
  1013. __skb_pull(skb, ihl);
  1014. skb_reset_transport_header(skb);
  1015. iph = ip_hdr(skb);
  1016. proto = iph->protocol & (MAX_INET_PROTOS - 1);
  1017. err = -EPROTONOSUPPORT;
  1018. rcu_read_lock();
  1019. ops = rcu_dereference(inet_protos[proto]);
  1020. if (likely(ops && ops->gso_send_check))
  1021. err = ops->gso_send_check(skb);
  1022. rcu_read_unlock();
  1023. out:
  1024. return err;
  1025. }
  1026. static struct sk_buff *inet_gso_segment(struct sk_buff *skb, int features)
  1027. {
  1028. struct sk_buff *segs = ERR_PTR(-EINVAL);
  1029. struct iphdr *iph;
  1030. struct net_protocol *ops;
  1031. int proto;
  1032. int ihl;
  1033. int id;
  1034. if (!(features & NETIF_F_V4_CSUM))
  1035. features &= ~NETIF_F_SG;
  1036. if (unlikely(skb_shinfo(skb)->gso_type &
  1037. ~(SKB_GSO_TCPV4 |
  1038. SKB_GSO_UDP |
  1039. SKB_GSO_DODGY |
  1040. SKB_GSO_TCP_ECN |
  1041. 0)))
  1042. goto out;
  1043. if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
  1044. goto out;
  1045. iph = ip_hdr(skb);
  1046. ihl = iph->ihl * 4;
  1047. if (ihl < sizeof(*iph))
  1048. goto out;
  1049. if (unlikely(!pskb_may_pull(skb, ihl)))
  1050. goto out;
  1051. __skb_pull(skb, ihl);
  1052. skb_reset_transport_header(skb);
  1053. iph = ip_hdr(skb);
  1054. id = ntohs(iph->id);
  1055. proto = iph->protocol & (MAX_INET_PROTOS - 1);
  1056. segs = ERR_PTR(-EPROTONOSUPPORT);
  1057. rcu_read_lock();
  1058. ops = rcu_dereference(inet_protos[proto]);
  1059. if (likely(ops && ops->gso_segment))
  1060. segs = ops->gso_segment(skb, features);
  1061. rcu_read_unlock();
  1062. if (!segs || IS_ERR(segs))
  1063. goto out;
  1064. skb = segs;
  1065. do {
  1066. iph = ip_hdr(skb);
  1067. iph->id = htons(id++);
  1068. iph->tot_len = htons(skb->len - skb->mac_len);
  1069. iph->check = 0;
  1070. iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
  1071. } while ((skb = skb->next));
  1072. out:
  1073. return segs;
  1074. }
  1075. int inet_ctl_sock_create(struct sock **sk, unsigned short family,
  1076. unsigned short type, unsigned char protocol,
  1077. struct net *net)
  1078. {
  1079. struct socket *sock;
  1080. int rc = sock_create_kern(family, type, protocol, &sock);
  1081. if (rc == 0) {
  1082. *sk = sock->sk;
  1083. (*sk)->sk_allocation = GFP_ATOMIC;
  1084. /*
  1085. * Unhash it so that IP input processing does not even see it,
  1086. * we do not wish this socket to see incoming packets.
  1087. */
  1088. (*sk)->sk_prot->unhash(*sk);
  1089. sk_change_net(*sk, net);
  1090. }
  1091. return rc;
  1092. }
  1093. EXPORT_SYMBOL_GPL(inet_ctl_sock_create);
  1094. unsigned long snmp_fold_field(void *mib[], int offt)
  1095. {
  1096. unsigned long res = 0;
  1097. int i;
  1098. for_each_possible_cpu(i) {
  1099. res += *(((unsigned long *) per_cpu_ptr(mib[0], i)) + offt);
  1100. res += *(((unsigned long *) per_cpu_ptr(mib[1], i)) + offt);
  1101. }
  1102. return res;
  1103. }
  1104. EXPORT_SYMBOL_GPL(snmp_fold_field);
  1105. int snmp_mib_init(void *ptr[2], size_t mibsize)
  1106. {
  1107. BUG_ON(ptr == NULL);
  1108. ptr[0] = __alloc_percpu(mibsize);
  1109. if (!ptr[0])
  1110. goto err0;
  1111. ptr[1] = __alloc_percpu(mibsize);
  1112. if (!ptr[1])
  1113. goto err1;
  1114. return 0;
  1115. err1:
  1116. free_percpu(ptr[0]);
  1117. ptr[0] = NULL;
  1118. err0:
  1119. return -ENOMEM;
  1120. }
  1121. EXPORT_SYMBOL_GPL(snmp_mib_init);
  1122. void snmp_mib_free(void *ptr[2])
  1123. {
  1124. BUG_ON(ptr == NULL);
  1125. free_percpu(ptr[0]);
  1126. free_percpu(ptr[1]);
  1127. ptr[0] = ptr[1] = NULL;
  1128. }
  1129. EXPORT_SYMBOL_GPL(snmp_mib_free);
  1130. #ifdef CONFIG_IP_MULTICAST
  1131. static struct net_protocol igmp_protocol = {
  1132. .handler = igmp_rcv,
  1133. };
  1134. #endif
  1135. static struct net_protocol tcp_protocol = {
  1136. .handler = tcp_v4_rcv,
  1137. .err_handler = tcp_v4_err,
  1138. .gso_send_check = tcp_v4_gso_send_check,
  1139. .gso_segment = tcp_tso_segment,
  1140. .no_policy = 1,
  1141. .netns_ok = 1,
  1142. };
  1143. static struct net_protocol udp_protocol = {
  1144. .handler = udp_rcv,
  1145. .err_handler = udp_err,
  1146. .no_policy = 1,
  1147. .netns_ok = 1,
  1148. };
  1149. static struct net_protocol icmp_protocol = {
  1150. .handler = icmp_rcv,
  1151. .no_policy = 1,
  1152. .netns_ok = 1,
  1153. };
  1154. static __net_init int ipv4_mib_init_net(struct net *net)
  1155. {
  1156. if (snmp_mib_init((void **)net->mib.tcp_statistics,
  1157. sizeof(struct tcp_mib)) < 0)
  1158. goto err_tcp_mib;
  1159. if (snmp_mib_init((void **)net->mib.ip_statistics,
  1160. sizeof(struct ipstats_mib)) < 0)
  1161. goto err_ip_mib;
  1162. if (snmp_mib_init((void **)net->mib.net_statistics,
  1163. sizeof(struct linux_mib)) < 0)
  1164. goto err_net_mib;
  1165. if (snmp_mib_init((void **)net->mib.udp_statistics,
  1166. sizeof(struct udp_mib)) < 0)
  1167. goto err_udp_mib;
  1168. if (snmp_mib_init((void **)net->mib.udplite_statistics,
  1169. sizeof(struct udp_mib)) < 0)
  1170. goto err_udplite_mib;
  1171. if (snmp_mib_init((void **)net->mib.icmp_statistics,
  1172. sizeof(struct icmp_mib)) < 0)
  1173. goto err_icmp_mib;
  1174. if (snmp_mib_init((void **)net->mib.icmpmsg_statistics,
  1175. sizeof(struct icmpmsg_mib)) < 0)
  1176. goto err_icmpmsg_mib;
  1177. tcp_mib_init(net);
  1178. return 0;
  1179. err_icmpmsg_mib:
  1180. snmp_mib_free((void **)net->mib.icmp_statistics);
  1181. err_icmp_mib:
  1182. snmp_mib_free((void **)net->mib.udplite_statistics);
  1183. err_udplite_mib:
  1184. snmp_mib_free((void **)net->mib.udp_statistics);
  1185. err_udp_mib:
  1186. snmp_mib_free((void **)net->mib.net_statistics);
  1187. err_net_mib:
  1188. snmp_mib_free((void **)net->mib.ip_statistics);
  1189. err_ip_mib:
  1190. snmp_mib_free((void **)net->mib.tcp_statistics);
  1191. err_tcp_mib:
  1192. return -ENOMEM;
  1193. }
  1194. static __net_exit void ipv4_mib_exit_net(struct net *net)
  1195. {
  1196. snmp_mib_free((void **)net->mib.icmpmsg_statistics);
  1197. snmp_mib_free((void **)net->mib.icmp_statistics);
  1198. snmp_mib_free((void **)net->mib.udplite_statistics);
  1199. snmp_mib_free((void **)net->mib.udp_statistics);
  1200. snmp_mib_free((void **)net->mib.net_statistics);
  1201. snmp_mib_free((void **)net->mib.ip_statistics);
  1202. snmp_mib_free((void **)net->mib.tcp_statistics);
  1203. }
  1204. static __net_initdata struct pernet_operations ipv4_mib_ops = {
  1205. .init = ipv4_mib_init_net,
  1206. .exit = ipv4_mib_exit_net,
  1207. };
  1208. static int __init init_ipv4_mibs(void)
  1209. {
  1210. return register_pernet_subsys(&ipv4_mib_ops);
  1211. }
  1212. static int ipv4_proc_init(void);
  1213. /*
  1214. * IP protocol layer initialiser
  1215. */
  1216. static struct packet_type ip_packet_type = {
  1217. .type = __constant_htons(ETH_P_IP),
  1218. .func = ip_rcv,
  1219. .gso_send_check = inet_gso_send_check,
  1220. .gso_segment = inet_gso_segment,
  1221. };
  1222. static int __init inet_init(void)
  1223. {
  1224. struct sk_buff *dummy_skb;
  1225. struct inet_protosw *q;
  1226. struct list_head *r;
  1227. int rc = -EINVAL;
  1228. BUILD_BUG_ON(sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb));
  1229. rc = proto_register(&tcp_prot, 1);
  1230. if (rc)
  1231. goto out;
  1232. rc = proto_register(&udp_prot, 1);
  1233. if (rc)
  1234. goto out_unregister_tcp_proto;
  1235. rc = proto_register(&raw_prot, 1);
  1236. if (rc)
  1237. goto out_unregister_udp_proto;
  1238. /*
  1239. * Tell SOCKET that we are alive...
  1240. */
  1241. (void)sock_register(&inet_family_ops);
  1242. #ifdef CONFIG_SYSCTL
  1243. ip_static_sysctl_init();
  1244. #endif
  1245. /*
  1246. * Add all the base protocols.
  1247. */
  1248. if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
  1249. printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
  1250. if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
  1251. printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
  1252. if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
  1253. printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
  1254. #ifdef CONFIG_IP_MULTICAST
  1255. if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
  1256. printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
  1257. #endif
  1258. /* Register the socket-side information for inet_create. */
  1259. for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
  1260. INIT_LIST_HEAD(r);
  1261. for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
  1262. inet_register_protosw(q);
  1263. /*
  1264. * Set the ARP module up
  1265. */
  1266. arp_init();
  1267. /*
  1268. * Set the IP module up
  1269. */
  1270. ip_init();
  1271. tcp_v4_init();
  1272. /* Setup TCP slab cache for open requests. */
  1273. tcp_init();
  1274. /* Setup UDP memory threshold */
  1275. udp_init();
  1276. /* Add UDP-Lite (RFC 3828) */
  1277. udplite4_register();
  1278. /*
  1279. * Set the ICMP layer up
  1280. */
  1281. if (icmp_init() < 0)
  1282. panic("Failed to create the ICMP control socket.\n");
  1283. /*
  1284. * Initialise the multicast router
  1285. */
  1286. #if defined(CONFIG_IP_MROUTE)
  1287. if (ip_mr_init())
  1288. printk(KERN_CRIT "inet_init: Cannot init ipv4 mroute\n");
  1289. #endif
  1290. /*
  1291. * Initialise per-cpu ipv4 mibs
  1292. */
  1293. if (init_ipv4_mibs())
  1294. printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n");
  1295. ipv4_proc_init();
  1296. ipfrag_init();
  1297. dev_add_pack(&ip_packet_type);
  1298. rc = 0;
  1299. out:
  1300. return rc;
  1301. out_unregister_udp_proto:
  1302. proto_unregister(&udp_prot);
  1303. out_unregister_tcp_proto:
  1304. proto_unregister(&tcp_prot);
  1305. goto out;
  1306. }
  1307. fs_initcall(inet_init);
  1308. /* ------------------------------------------------------------------------ */
  1309. #ifdef CONFIG_PROC_FS
  1310. static int __init ipv4_proc_init(void)
  1311. {
  1312. int rc = 0;
  1313. if (raw_proc_init())
  1314. goto out_raw;
  1315. if (tcp4_proc_init())
  1316. goto out_tcp;
  1317. if (udp4_proc_init())
  1318. goto out_udp;
  1319. if (ip_misc_proc_init())
  1320. goto out_misc;
  1321. out:
  1322. return rc;
  1323. out_misc:
  1324. udp4_proc_exit();
  1325. out_udp:
  1326. tcp4_proc_exit();
  1327. out_tcp:
  1328. raw_proc_exit();
  1329. out_raw:
  1330. rc = -ENOMEM;
  1331. goto out;
  1332. }
  1333. #else /* CONFIG_PROC_FS */
  1334. static int __init ipv4_proc_init(void)
  1335. {
  1336. return 0;
  1337. }
  1338. #endif /* CONFIG_PROC_FS */
  1339. MODULE_ALIAS_NETPROTO(PF_INET);
  1340. EXPORT_SYMBOL(inet_accept);
  1341. EXPORT_SYMBOL(inet_bind);
  1342. EXPORT_SYMBOL(inet_dgram_connect);
  1343. EXPORT_SYMBOL(inet_dgram_ops);
  1344. EXPORT_SYMBOL(inet_getname);
  1345. EXPORT_SYMBOL(inet_ioctl);
  1346. EXPORT_SYMBOL(inet_listen);
  1347. EXPORT_SYMBOL(inet_register_protosw);
  1348. EXPORT_SYMBOL(inet_release);
  1349. EXPORT_SYMBOL(inet_sendmsg);
  1350. EXPORT_SYMBOL(inet_shutdown);
  1351. EXPORT_SYMBOL(inet_sock_destruct);
  1352. EXPORT_SYMBOL(inet_stream_connect);
  1353. EXPORT_SYMBOL(inet_stream_ops);
  1354. EXPORT_SYMBOL(inet_unregister_protosw);
  1355. EXPORT_SYMBOL(sysctl_ip_nonlocal_bind);