af_inet.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053
  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. #define pr_fmt(fmt) "IPv4: " fmt
  68. #include <linux/err.h>
  69. #include <linux/errno.h>
  70. #include <linux/types.h>
  71. #include <linux/socket.h>
  72. #include <linux/in.h>
  73. #include <linux/kernel.h>
  74. #include <linux/kmod.h>
  75. #include <linux/sched.h>
  76. #include <linux/timer.h>
  77. #include <linux/string.h>
  78. #include <linux/sockios.h>
  79. #include <linux/net.h>
  80. #include <linux/capability.h>
  81. #include <linux/fcntl.h>
  82. #include <linux/mm.h>
  83. #include <linux/interrupt.h>
  84. #include <linux/stat.h>
  85. #include <linux/init.h>
  86. #include <linux/poll.h>
  87. #include <linux/netfilter_ipv4.h>
  88. #include <linux/random.h>
  89. #include <linux/slab.h>
  90. #include <linux/uaccess.h>
  91. #include <linux/inet.h>
  92. #include <linux/igmp.h>
  93. #include <linux/inetdevice.h>
  94. #include <linux/netdevice.h>
  95. #include <net/checksum.h>
  96. #include <net/ip.h>
  97. #include <net/protocol.h>
  98. #include <net/arp.h>
  99. #include <net/route.h>
  100. #include <net/ip_fib.h>
  101. #include <net/inet_connection_sock.h>
  102. #include <net/tcp.h>
  103. #include <net/udp.h>
  104. #include <net/udplite.h>
  105. #include <net/ping.h>
  106. #include <linux/skbuff.h>
  107. #include <net/sock.h>
  108. #include <net/raw.h>
  109. #include <net/icmp.h>
  110. #include <net/inet_common.h>
  111. #include <net/ip_tunnels.h>
  112. #include <net/xfrm.h>
  113. #include <net/net_namespace.h>
  114. #include <net/secure_seq.h>
  115. #ifdef CONFIG_IP_MROUTE
  116. #include <linux/mroute.h>
  117. #endif
  118. #include <net/l3mdev.h>
  119. #include <trace/events/sock.h>
  120. /* The inetsw table contains everything that inet_create needs to
  121. * build a new socket.
  122. */
  123. static struct list_head inetsw[SOCK_MAX];
  124. static DEFINE_SPINLOCK(inetsw_lock);
  125. /* New destruction routine */
  126. void inet_sock_destruct(struct sock *sk)
  127. {
  128. struct inet_sock *inet = inet_sk(sk);
  129. __skb_queue_purge(&sk->sk_receive_queue);
  130. __skb_queue_purge(&sk->sk_error_queue);
  131. sk_mem_reclaim(sk);
  132. if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
  133. pr_err("Attempt to release TCP socket in state %d %p\n",
  134. sk->sk_state, sk);
  135. return;
  136. }
  137. if (!sock_flag(sk, SOCK_DEAD)) {
  138. pr_err("Attempt to release alive inet socket %p\n", sk);
  139. return;
  140. }
  141. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  142. WARN_ON(refcount_read(&sk->sk_wmem_alloc));
  143. WARN_ON(sk->sk_wmem_queued);
  144. WARN_ON(sk->sk_forward_alloc);
  145. kfree(rcu_dereference_protected(inet->inet_opt, 1));
  146. dst_release(rcu_dereference_check(sk->sk_dst_cache, 1));
  147. dst_release(sk->sk_rx_dst);
  148. sk_refcnt_debug_dec(sk);
  149. }
  150. EXPORT_SYMBOL(inet_sock_destruct);
  151. /*
  152. * The routines beyond this point handle the behaviour of an AF_INET
  153. * socket object. Mostly it punts to the subprotocols of IP to do
  154. * the work.
  155. */
  156. /*
  157. * Automatically bind an unbound socket.
  158. */
  159. static int inet_autobind(struct sock *sk)
  160. {
  161. struct inet_sock *inet;
  162. /* We may need to bind the socket. */
  163. lock_sock(sk);
  164. inet = inet_sk(sk);
  165. if (!inet->inet_num) {
  166. if (sk->sk_prot->get_port(sk, 0)) {
  167. release_sock(sk);
  168. return -EAGAIN;
  169. }
  170. inet->inet_sport = htons(inet->inet_num);
  171. }
  172. release_sock(sk);
  173. return 0;
  174. }
  175. /*
  176. * Move a socket into listening state.
  177. */
  178. int inet_listen(struct socket *sock, int backlog)
  179. {
  180. struct sock *sk = sock->sk;
  181. unsigned char old_state;
  182. int err, tcp_fastopen;
  183. lock_sock(sk);
  184. err = -EINVAL;
  185. if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
  186. goto out;
  187. old_state = sk->sk_state;
  188. if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
  189. goto out;
  190. /* Really, if the socket is already in listen state
  191. * we can only allow the backlog to be adjusted.
  192. */
  193. if (old_state != TCP_LISTEN) {
  194. /* Enable TFO w/o requiring TCP_FASTOPEN socket option.
  195. * Note that only TCP sockets (SOCK_STREAM) will reach here.
  196. * Also fastopen backlog may already been set via the option
  197. * because the socket was in TCP_LISTEN state previously but
  198. * was shutdown() rather than close().
  199. */
  200. tcp_fastopen = sock_net(sk)->ipv4.sysctl_tcp_fastopen;
  201. if ((tcp_fastopen & TFO_SERVER_WO_SOCKOPT1) &&
  202. (tcp_fastopen & TFO_SERVER_ENABLE) &&
  203. !inet_csk(sk)->icsk_accept_queue.fastopenq.max_qlen) {
  204. fastopen_queue_tune(sk, backlog);
  205. tcp_fastopen_init_key_once(sock_net(sk));
  206. }
  207. err = inet_csk_listen_start(sk, backlog);
  208. if (err)
  209. goto out;
  210. tcp_call_bpf(sk, BPF_SOCK_OPS_TCP_LISTEN_CB, 0, NULL);
  211. }
  212. sk->sk_max_ack_backlog = backlog;
  213. err = 0;
  214. out:
  215. release_sock(sk);
  216. return err;
  217. }
  218. EXPORT_SYMBOL(inet_listen);
  219. /*
  220. * Create an inet socket.
  221. */
  222. static int inet_create(struct net *net, struct socket *sock, int protocol,
  223. int kern)
  224. {
  225. struct sock *sk;
  226. struct inet_protosw *answer;
  227. struct inet_sock *inet;
  228. struct proto *answer_prot;
  229. unsigned char answer_flags;
  230. int try_loading_module = 0;
  231. int err;
  232. if (protocol < 0 || protocol >= IPPROTO_MAX)
  233. return -EINVAL;
  234. sock->state = SS_UNCONNECTED;
  235. /* Look for the requested type/protocol pair. */
  236. lookup_protocol:
  237. err = -ESOCKTNOSUPPORT;
  238. rcu_read_lock();
  239. list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {
  240. err = 0;
  241. /* Check the non-wild match. */
  242. if (protocol == answer->protocol) {
  243. if (protocol != IPPROTO_IP)
  244. break;
  245. } else {
  246. /* Check for the two wild cases. */
  247. if (IPPROTO_IP == protocol) {
  248. protocol = answer->protocol;
  249. break;
  250. }
  251. if (IPPROTO_IP == answer->protocol)
  252. break;
  253. }
  254. err = -EPROTONOSUPPORT;
  255. }
  256. if (unlikely(err)) {
  257. if (try_loading_module < 2) {
  258. rcu_read_unlock();
  259. /*
  260. * Be more specific, e.g. net-pf-2-proto-132-type-1
  261. * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
  262. */
  263. if (++try_loading_module == 1)
  264. request_module("net-pf-%d-proto-%d-type-%d",
  265. PF_INET, protocol, sock->type);
  266. /*
  267. * Fall back to generic, e.g. net-pf-2-proto-132
  268. * (net-pf-PF_INET-proto-IPPROTO_SCTP)
  269. */
  270. else
  271. request_module("net-pf-%d-proto-%d",
  272. PF_INET, protocol);
  273. goto lookup_protocol;
  274. } else
  275. goto out_rcu_unlock;
  276. }
  277. err = -EPERM;
  278. if (sock->type == SOCK_RAW && !kern &&
  279. !ns_capable(net->user_ns, CAP_NET_RAW))
  280. goto out_rcu_unlock;
  281. sock->ops = answer->ops;
  282. answer_prot = answer->prot;
  283. answer_flags = answer->flags;
  284. rcu_read_unlock();
  285. WARN_ON(!answer_prot->slab);
  286. err = -ENOBUFS;
  287. sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot, kern);
  288. if (!sk)
  289. goto out;
  290. err = 0;
  291. if (INET_PROTOSW_REUSE & answer_flags)
  292. sk->sk_reuse = SK_CAN_REUSE;
  293. inet = inet_sk(sk);
  294. inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
  295. inet->nodefrag = 0;
  296. if (SOCK_RAW == sock->type) {
  297. inet->inet_num = protocol;
  298. if (IPPROTO_RAW == protocol)
  299. inet->hdrincl = 1;
  300. }
  301. if (net->ipv4.sysctl_ip_no_pmtu_disc)
  302. inet->pmtudisc = IP_PMTUDISC_DONT;
  303. else
  304. inet->pmtudisc = IP_PMTUDISC_WANT;
  305. inet->inet_id = 0;
  306. sock_init_data(sock, sk);
  307. sk->sk_destruct = inet_sock_destruct;
  308. sk->sk_protocol = protocol;
  309. sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  310. inet->uc_ttl = -1;
  311. inet->mc_loop = 1;
  312. inet->mc_ttl = 1;
  313. inet->mc_all = 1;
  314. inet->mc_index = 0;
  315. inet->mc_list = NULL;
  316. inet->rcv_tos = 0;
  317. sk_refcnt_debug_inc(sk);
  318. if (inet->inet_num) {
  319. /* It assumes that any protocol which allows
  320. * the user to assign a number at socket
  321. * creation time automatically
  322. * shares.
  323. */
  324. inet->inet_sport = htons(inet->inet_num);
  325. /* Add to protocol hash chains. */
  326. err = sk->sk_prot->hash(sk);
  327. if (err) {
  328. sk_common_release(sk);
  329. goto out;
  330. }
  331. }
  332. if (sk->sk_prot->init) {
  333. err = sk->sk_prot->init(sk);
  334. if (err) {
  335. sk_common_release(sk);
  336. goto out;
  337. }
  338. }
  339. if (!kern) {
  340. err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk);
  341. if (err) {
  342. sk_common_release(sk);
  343. goto out;
  344. }
  345. }
  346. out:
  347. return err;
  348. out_rcu_unlock:
  349. rcu_read_unlock();
  350. goto out;
  351. }
  352. /*
  353. * The peer socket should always be NULL (or else). When we call this
  354. * function we are destroying the object and from then on nobody
  355. * should refer to it.
  356. */
  357. int inet_release(struct socket *sock)
  358. {
  359. struct sock *sk = sock->sk;
  360. if (sk) {
  361. long timeout;
  362. /* Applications forget to leave groups before exiting */
  363. ip_mc_drop_socket(sk);
  364. /* If linger is set, we don't return until the close
  365. * is complete. Otherwise we return immediately. The
  366. * actually closing is done the same either way.
  367. *
  368. * If the close is due to the process exiting, we never
  369. * linger..
  370. */
  371. timeout = 0;
  372. if (sock_flag(sk, SOCK_LINGER) &&
  373. !(current->flags & PF_EXITING))
  374. timeout = sk->sk_lingertime;
  375. sk->sk_prot->close(sk, timeout);
  376. sock->sk = NULL;
  377. }
  378. return 0;
  379. }
  380. EXPORT_SYMBOL(inet_release);
  381. int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  382. {
  383. struct sock *sk = sock->sk;
  384. int err;
  385. /* If the socket has its own bind function then use it. (RAW) */
  386. if (sk->sk_prot->bind) {
  387. return sk->sk_prot->bind(sk, uaddr, addr_len);
  388. }
  389. if (addr_len < sizeof(struct sockaddr_in))
  390. return -EINVAL;
  391. /* BPF prog is run before any checks are done so that if the prog
  392. * changes context in a wrong way it will be caught.
  393. */
  394. err = BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr);
  395. if (err)
  396. return err;
  397. return __inet_bind(sk, uaddr, addr_len, false, true);
  398. }
  399. EXPORT_SYMBOL(inet_bind);
  400. int __inet_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
  401. bool force_bind_address_no_port, bool with_lock)
  402. {
  403. struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
  404. struct inet_sock *inet = inet_sk(sk);
  405. struct net *net = sock_net(sk);
  406. unsigned short snum;
  407. int chk_addr_ret;
  408. u32 tb_id = RT_TABLE_LOCAL;
  409. int err;
  410. if (addr->sin_family != AF_INET) {
  411. /* Compatibility games : accept AF_UNSPEC (mapped to AF_INET)
  412. * only if s_addr is INADDR_ANY.
  413. */
  414. err = -EAFNOSUPPORT;
  415. if (addr->sin_family != AF_UNSPEC ||
  416. addr->sin_addr.s_addr != htonl(INADDR_ANY))
  417. goto out;
  418. }
  419. tb_id = l3mdev_fib_table_by_index(net, sk->sk_bound_dev_if) ? : tb_id;
  420. chk_addr_ret = inet_addr_type_table(net, addr->sin_addr.s_addr, tb_id);
  421. /* Not specified by any standard per-se, however it breaks too
  422. * many applications when removed. It is unfortunate since
  423. * allowing applications to make a non-local bind solves
  424. * several problems with systems using dynamic addressing.
  425. * (ie. your servers still start up even if your ISDN link
  426. * is temporarily down)
  427. */
  428. err = -EADDRNOTAVAIL;
  429. if (!inet_can_nonlocal_bind(net, inet) &&
  430. addr->sin_addr.s_addr != htonl(INADDR_ANY) &&
  431. chk_addr_ret != RTN_LOCAL &&
  432. chk_addr_ret != RTN_MULTICAST &&
  433. chk_addr_ret != RTN_BROADCAST)
  434. goto out;
  435. snum = ntohs(addr->sin_port);
  436. err = -EACCES;
  437. if (snum && snum < inet_prot_sock(net) &&
  438. !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
  439. goto out;
  440. /* We keep a pair of addresses. rcv_saddr is the one
  441. * used by hash lookups, and saddr is used for transmit.
  442. *
  443. * In the BSD API these are the same except where it
  444. * would be illegal to use them (multicast/broadcast) in
  445. * which case the sending device address is used.
  446. */
  447. if (with_lock)
  448. lock_sock(sk);
  449. /* Check these errors (active socket, double bind). */
  450. err = -EINVAL;
  451. if (sk->sk_state != TCP_CLOSE || inet->inet_num)
  452. goto out_release_sock;
  453. inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
  454. if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
  455. inet->inet_saddr = 0; /* Use device */
  456. /* Make sure we are allowed to bind here. */
  457. if (snum || !(inet->bind_address_no_port ||
  458. force_bind_address_no_port)) {
  459. if (sk->sk_prot->get_port(sk, snum)) {
  460. inet->inet_saddr = inet->inet_rcv_saddr = 0;
  461. err = -EADDRINUSE;
  462. goto out_release_sock;
  463. }
  464. err = BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk);
  465. if (err) {
  466. inet->inet_saddr = inet->inet_rcv_saddr = 0;
  467. goto out_release_sock;
  468. }
  469. }
  470. if (inet->inet_rcv_saddr)
  471. sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
  472. if (snum)
  473. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  474. inet->inet_sport = htons(inet->inet_num);
  475. inet->inet_daddr = 0;
  476. inet->inet_dport = 0;
  477. sk_dst_reset(sk);
  478. err = 0;
  479. out_release_sock:
  480. if (with_lock)
  481. release_sock(sk);
  482. out:
  483. return err;
  484. }
  485. int inet_dgram_connect(struct socket *sock, struct sockaddr *uaddr,
  486. int addr_len, int flags)
  487. {
  488. struct sock *sk = sock->sk;
  489. int err;
  490. if (addr_len < sizeof(uaddr->sa_family))
  491. return -EINVAL;
  492. if (uaddr->sa_family == AF_UNSPEC)
  493. return sk->sk_prot->disconnect(sk, flags);
  494. if (BPF_CGROUP_PRE_CONNECT_ENABLED(sk)) {
  495. err = sk->sk_prot->pre_connect(sk, uaddr, addr_len);
  496. if (err)
  497. return err;
  498. }
  499. if (!inet_sk(sk)->inet_num && inet_autobind(sk))
  500. return -EAGAIN;
  501. return sk->sk_prot->connect(sk, uaddr, addr_len);
  502. }
  503. EXPORT_SYMBOL(inet_dgram_connect);
  504. static long inet_wait_for_connect(struct sock *sk, long timeo, int writebias)
  505. {
  506. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  507. add_wait_queue(sk_sleep(sk), &wait);
  508. sk->sk_write_pending += writebias;
  509. /* Basic assumption: if someone sets sk->sk_err, he _must_
  510. * change state of the socket from TCP_SYN_*.
  511. * Connect() does not allow to get error notifications
  512. * without closing the socket.
  513. */
  514. while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
  515. release_sock(sk);
  516. timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
  517. lock_sock(sk);
  518. if (signal_pending(current) || !timeo)
  519. break;
  520. }
  521. remove_wait_queue(sk_sleep(sk), &wait);
  522. sk->sk_write_pending -= writebias;
  523. return timeo;
  524. }
  525. /*
  526. * Connect to a remote host. There is regrettably still a little
  527. * TCP 'magic' in here.
  528. */
  529. int __inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
  530. int addr_len, int flags, int is_sendmsg)
  531. {
  532. struct sock *sk = sock->sk;
  533. int err;
  534. long timeo;
  535. /*
  536. * uaddr can be NULL and addr_len can be 0 if:
  537. * sk is a TCP fastopen active socket and
  538. * TCP_FASTOPEN_CONNECT sockopt is set and
  539. * we already have a valid cookie for this socket.
  540. * In this case, user can call write() after connect().
  541. * write() will invoke tcp_sendmsg_fastopen() which calls
  542. * __inet_stream_connect().
  543. */
  544. if (uaddr) {
  545. if (addr_len < sizeof(uaddr->sa_family))
  546. return -EINVAL;
  547. if (uaddr->sa_family == AF_UNSPEC) {
  548. err = sk->sk_prot->disconnect(sk, flags);
  549. sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
  550. goto out;
  551. }
  552. }
  553. switch (sock->state) {
  554. default:
  555. err = -EINVAL;
  556. goto out;
  557. case SS_CONNECTED:
  558. err = -EISCONN;
  559. goto out;
  560. case SS_CONNECTING:
  561. if (inet_sk(sk)->defer_connect)
  562. err = is_sendmsg ? -EINPROGRESS : -EISCONN;
  563. else
  564. err = -EALREADY;
  565. /* Fall out of switch with err, set for this state */
  566. break;
  567. case SS_UNCONNECTED:
  568. err = -EISCONN;
  569. if (sk->sk_state != TCP_CLOSE)
  570. goto out;
  571. if (BPF_CGROUP_PRE_CONNECT_ENABLED(sk)) {
  572. err = sk->sk_prot->pre_connect(sk, uaddr, addr_len);
  573. if (err)
  574. goto out;
  575. }
  576. err = sk->sk_prot->connect(sk, uaddr, addr_len);
  577. if (err < 0)
  578. goto out;
  579. sock->state = SS_CONNECTING;
  580. if (!err && inet_sk(sk)->defer_connect)
  581. goto out;
  582. /* Just entered SS_CONNECTING state; the only
  583. * difference is that return value in non-blocking
  584. * case is EINPROGRESS, rather than EALREADY.
  585. */
  586. err = -EINPROGRESS;
  587. break;
  588. }
  589. timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
  590. if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
  591. int writebias = (sk->sk_protocol == IPPROTO_TCP) &&
  592. tcp_sk(sk)->fastopen_req &&
  593. tcp_sk(sk)->fastopen_req->data ? 1 : 0;
  594. /* Error code is set above */
  595. if (!timeo || !inet_wait_for_connect(sk, timeo, writebias))
  596. goto out;
  597. err = sock_intr_errno(timeo);
  598. if (signal_pending(current))
  599. goto out;
  600. }
  601. /* Connection was closed by RST, timeout, ICMP error
  602. * or another process disconnected us.
  603. */
  604. if (sk->sk_state == TCP_CLOSE)
  605. goto sock_error;
  606. /* sk->sk_err may be not zero now, if RECVERR was ordered by user
  607. * and error was received after socket entered established state.
  608. * Hence, it is handled normally after connect() return successfully.
  609. */
  610. sock->state = SS_CONNECTED;
  611. err = 0;
  612. out:
  613. return err;
  614. sock_error:
  615. err = sock_error(sk) ? : -ECONNABORTED;
  616. sock->state = SS_UNCONNECTED;
  617. if (sk->sk_prot->disconnect(sk, flags))
  618. sock->state = SS_DISCONNECTING;
  619. goto out;
  620. }
  621. EXPORT_SYMBOL(__inet_stream_connect);
  622. int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
  623. int addr_len, int flags)
  624. {
  625. int err;
  626. lock_sock(sock->sk);
  627. err = __inet_stream_connect(sock, uaddr, addr_len, flags, 0);
  628. release_sock(sock->sk);
  629. return err;
  630. }
  631. EXPORT_SYMBOL(inet_stream_connect);
  632. /*
  633. * Accept a pending connection. The TCP layer now gives BSD semantics.
  634. */
  635. int inet_accept(struct socket *sock, struct socket *newsock, int flags,
  636. bool kern)
  637. {
  638. struct sock *sk1 = sock->sk;
  639. int err = -EINVAL;
  640. struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err, kern);
  641. if (!sk2)
  642. goto do_err;
  643. lock_sock(sk2);
  644. sock_rps_record_flow(sk2);
  645. WARN_ON(!((1 << sk2->sk_state) &
  646. (TCPF_ESTABLISHED | TCPF_SYN_RECV |
  647. TCPF_CLOSE_WAIT | TCPF_CLOSE)));
  648. sock_graft(sk2, newsock);
  649. newsock->state = SS_CONNECTED;
  650. err = 0;
  651. release_sock(sk2);
  652. do_err:
  653. return err;
  654. }
  655. EXPORT_SYMBOL(inet_accept);
  656. /*
  657. * This does both peername and sockname.
  658. */
  659. int inet_getname(struct socket *sock, struct sockaddr *uaddr,
  660. int peer)
  661. {
  662. struct sock *sk = sock->sk;
  663. struct inet_sock *inet = inet_sk(sk);
  664. DECLARE_SOCKADDR(struct sockaddr_in *, sin, uaddr);
  665. sin->sin_family = AF_INET;
  666. if (peer) {
  667. if (!inet->inet_dport ||
  668. (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
  669. peer == 1))
  670. return -ENOTCONN;
  671. sin->sin_port = inet->inet_dport;
  672. sin->sin_addr.s_addr = inet->inet_daddr;
  673. } else {
  674. __be32 addr = inet->inet_rcv_saddr;
  675. if (!addr)
  676. addr = inet->inet_saddr;
  677. sin->sin_port = inet->inet_sport;
  678. sin->sin_addr.s_addr = addr;
  679. }
  680. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  681. return sizeof(*sin);
  682. }
  683. EXPORT_SYMBOL(inet_getname);
  684. int inet_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  685. {
  686. struct sock *sk = sock->sk;
  687. sock_rps_record_flow(sk);
  688. /* We may need to bind the socket. */
  689. if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind &&
  690. inet_autobind(sk))
  691. return -EAGAIN;
  692. return sk->sk_prot->sendmsg(sk, msg, size);
  693. }
  694. EXPORT_SYMBOL(inet_sendmsg);
  695. ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset,
  696. size_t size, int flags)
  697. {
  698. struct sock *sk = sock->sk;
  699. sock_rps_record_flow(sk);
  700. /* We may need to bind the socket. */
  701. if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind &&
  702. inet_autobind(sk))
  703. return -EAGAIN;
  704. if (sk->sk_prot->sendpage)
  705. return sk->sk_prot->sendpage(sk, page, offset, size, flags);
  706. return sock_no_sendpage(sock, page, offset, size, flags);
  707. }
  708. EXPORT_SYMBOL(inet_sendpage);
  709. int inet_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  710. int flags)
  711. {
  712. struct sock *sk = sock->sk;
  713. int addr_len = 0;
  714. int err;
  715. if (likely(!(flags & MSG_ERRQUEUE)))
  716. sock_rps_record_flow(sk);
  717. err = sk->sk_prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT,
  718. flags & ~MSG_DONTWAIT, &addr_len);
  719. if (err >= 0)
  720. msg->msg_namelen = addr_len;
  721. return err;
  722. }
  723. EXPORT_SYMBOL(inet_recvmsg);
  724. int inet_shutdown(struct socket *sock, int how)
  725. {
  726. struct sock *sk = sock->sk;
  727. int err = 0;
  728. /* This should really check to make sure
  729. * the socket is a TCP socket. (WHY AC...)
  730. */
  731. how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
  732. 1->2 bit 2 snds.
  733. 2->3 */
  734. if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */
  735. return -EINVAL;
  736. lock_sock(sk);
  737. if (sock->state == SS_CONNECTING) {
  738. if ((1 << sk->sk_state) &
  739. (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
  740. sock->state = SS_DISCONNECTING;
  741. else
  742. sock->state = SS_CONNECTED;
  743. }
  744. switch (sk->sk_state) {
  745. case TCP_CLOSE:
  746. err = -ENOTCONN;
  747. /* Hack to wake up other listeners, who can poll for
  748. EPOLLHUP, even on eg. unconnected UDP sockets -- RR */
  749. /* fall through */
  750. default:
  751. sk->sk_shutdown |= how;
  752. if (sk->sk_prot->shutdown)
  753. sk->sk_prot->shutdown(sk, how);
  754. break;
  755. /* Remaining two branches are temporary solution for missing
  756. * close() in multithreaded environment. It is _not_ a good idea,
  757. * but we have no choice until close() is repaired at VFS level.
  758. */
  759. case TCP_LISTEN:
  760. if (!(how & RCV_SHUTDOWN))
  761. break;
  762. /* fall through */
  763. case TCP_SYN_SENT:
  764. err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
  765. sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
  766. break;
  767. }
  768. /* Wake up anyone sleeping in poll. */
  769. sk->sk_state_change(sk);
  770. release_sock(sk);
  771. return err;
  772. }
  773. EXPORT_SYMBOL(inet_shutdown);
  774. /*
  775. * ioctl() calls you can issue on an INET socket. Most of these are
  776. * device configuration and stuff and very rarely used. Some ioctls
  777. * pass on to the socket itself.
  778. *
  779. * NOTE: I like the idea of a module for the config stuff. ie ifconfig
  780. * loads the devconfigure module does its configuring and unloads it.
  781. * There's a good 20K of config code hanging around the kernel.
  782. */
  783. int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  784. {
  785. struct sock *sk = sock->sk;
  786. int err = 0;
  787. struct net *net = sock_net(sk);
  788. void __user *p = (void __user *)arg;
  789. struct ifreq ifr;
  790. struct rtentry rt;
  791. switch (cmd) {
  792. case SIOCGSTAMP:
  793. err = sock_get_timestamp(sk, (struct timeval __user *)arg);
  794. break;
  795. case SIOCGSTAMPNS:
  796. err = sock_get_timestampns(sk, (struct timespec __user *)arg);
  797. break;
  798. case SIOCADDRT:
  799. case SIOCDELRT:
  800. if (copy_from_user(&rt, p, sizeof(struct rtentry)))
  801. return -EFAULT;
  802. err = ip_rt_ioctl(net, cmd, &rt);
  803. break;
  804. case SIOCRTMSG:
  805. err = -EINVAL;
  806. break;
  807. case SIOCDARP:
  808. case SIOCGARP:
  809. case SIOCSARP:
  810. err = arp_ioctl(net, cmd, (void __user *)arg);
  811. break;
  812. case SIOCGIFADDR:
  813. case SIOCGIFBRDADDR:
  814. case SIOCGIFNETMASK:
  815. case SIOCGIFDSTADDR:
  816. case SIOCGIFPFLAGS:
  817. if (copy_from_user(&ifr, p, sizeof(struct ifreq)))
  818. return -EFAULT;
  819. err = devinet_ioctl(net, cmd, &ifr);
  820. if (!err && copy_to_user(p, &ifr, sizeof(struct ifreq)))
  821. err = -EFAULT;
  822. break;
  823. case SIOCSIFADDR:
  824. case SIOCSIFBRDADDR:
  825. case SIOCSIFNETMASK:
  826. case SIOCSIFDSTADDR:
  827. case SIOCSIFPFLAGS:
  828. case SIOCSIFFLAGS:
  829. if (copy_from_user(&ifr, p, sizeof(struct ifreq)))
  830. return -EFAULT;
  831. err = devinet_ioctl(net, cmd, &ifr);
  832. break;
  833. default:
  834. if (sk->sk_prot->ioctl)
  835. err = sk->sk_prot->ioctl(sk, cmd, arg);
  836. else
  837. err = -ENOIOCTLCMD;
  838. break;
  839. }
  840. return err;
  841. }
  842. EXPORT_SYMBOL(inet_ioctl);
  843. #ifdef CONFIG_COMPAT
  844. static int inet_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  845. {
  846. struct sock *sk = sock->sk;
  847. int err = -ENOIOCTLCMD;
  848. if (sk->sk_prot->compat_ioctl)
  849. err = sk->sk_prot->compat_ioctl(sk, cmd, arg);
  850. return err;
  851. }
  852. #endif
  853. const struct proto_ops inet_stream_ops = {
  854. .family = PF_INET,
  855. .owner = THIS_MODULE,
  856. .release = inet_release,
  857. .bind = inet_bind,
  858. .connect = inet_stream_connect,
  859. .socketpair = sock_no_socketpair,
  860. .accept = inet_accept,
  861. .getname = inet_getname,
  862. .poll = tcp_poll,
  863. .ioctl = inet_ioctl,
  864. .listen = inet_listen,
  865. .shutdown = inet_shutdown,
  866. .setsockopt = sock_common_setsockopt,
  867. .getsockopt = sock_common_getsockopt,
  868. .sendmsg = inet_sendmsg,
  869. .recvmsg = inet_recvmsg,
  870. #ifdef CONFIG_MMU
  871. .mmap = tcp_mmap,
  872. #endif
  873. .sendpage = inet_sendpage,
  874. .splice_read = tcp_splice_read,
  875. .read_sock = tcp_read_sock,
  876. .sendmsg_locked = tcp_sendmsg_locked,
  877. .sendpage_locked = tcp_sendpage_locked,
  878. .peek_len = tcp_peek_len,
  879. #ifdef CONFIG_COMPAT
  880. .compat_setsockopt = compat_sock_common_setsockopt,
  881. .compat_getsockopt = compat_sock_common_getsockopt,
  882. .compat_ioctl = inet_compat_ioctl,
  883. #endif
  884. .set_rcvlowat = tcp_set_rcvlowat,
  885. };
  886. EXPORT_SYMBOL(inet_stream_ops);
  887. const struct proto_ops inet_dgram_ops = {
  888. .family = PF_INET,
  889. .owner = THIS_MODULE,
  890. .release = inet_release,
  891. .bind = inet_bind,
  892. .connect = inet_dgram_connect,
  893. .socketpair = sock_no_socketpair,
  894. .accept = sock_no_accept,
  895. .getname = inet_getname,
  896. .poll = udp_poll,
  897. .ioctl = inet_ioctl,
  898. .listen = sock_no_listen,
  899. .shutdown = inet_shutdown,
  900. .setsockopt = sock_common_setsockopt,
  901. .getsockopt = sock_common_getsockopt,
  902. .sendmsg = inet_sendmsg,
  903. .recvmsg = inet_recvmsg,
  904. .mmap = sock_no_mmap,
  905. .sendpage = inet_sendpage,
  906. .set_peek_off = sk_set_peek_off,
  907. #ifdef CONFIG_COMPAT
  908. .compat_setsockopt = compat_sock_common_setsockopt,
  909. .compat_getsockopt = compat_sock_common_getsockopt,
  910. .compat_ioctl = inet_compat_ioctl,
  911. #endif
  912. };
  913. EXPORT_SYMBOL(inet_dgram_ops);
  914. /*
  915. * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
  916. * udp_poll
  917. */
  918. static const struct proto_ops inet_sockraw_ops = {
  919. .family = PF_INET,
  920. .owner = THIS_MODULE,
  921. .release = inet_release,
  922. .bind = inet_bind,
  923. .connect = inet_dgram_connect,
  924. .socketpair = sock_no_socketpair,
  925. .accept = sock_no_accept,
  926. .getname = inet_getname,
  927. .poll = datagram_poll,
  928. .ioctl = inet_ioctl,
  929. .listen = sock_no_listen,
  930. .shutdown = inet_shutdown,
  931. .setsockopt = sock_common_setsockopt,
  932. .getsockopt = sock_common_getsockopt,
  933. .sendmsg = inet_sendmsg,
  934. .recvmsg = inet_recvmsg,
  935. .mmap = sock_no_mmap,
  936. .sendpage = inet_sendpage,
  937. #ifdef CONFIG_COMPAT
  938. .compat_setsockopt = compat_sock_common_setsockopt,
  939. .compat_getsockopt = compat_sock_common_getsockopt,
  940. .compat_ioctl = inet_compat_ioctl,
  941. #endif
  942. };
  943. static const struct net_proto_family inet_family_ops = {
  944. .family = PF_INET,
  945. .create = inet_create,
  946. .owner = THIS_MODULE,
  947. };
  948. /* Upon startup we insert all the elements in inetsw_array[] into
  949. * the linked list inetsw.
  950. */
  951. static struct inet_protosw inetsw_array[] =
  952. {
  953. {
  954. .type = SOCK_STREAM,
  955. .protocol = IPPROTO_TCP,
  956. .prot = &tcp_prot,
  957. .ops = &inet_stream_ops,
  958. .flags = INET_PROTOSW_PERMANENT |
  959. INET_PROTOSW_ICSK,
  960. },
  961. {
  962. .type = SOCK_DGRAM,
  963. .protocol = IPPROTO_UDP,
  964. .prot = &udp_prot,
  965. .ops = &inet_dgram_ops,
  966. .flags = INET_PROTOSW_PERMANENT,
  967. },
  968. {
  969. .type = SOCK_DGRAM,
  970. .protocol = IPPROTO_ICMP,
  971. .prot = &ping_prot,
  972. .ops = &inet_sockraw_ops,
  973. .flags = INET_PROTOSW_REUSE,
  974. },
  975. {
  976. .type = SOCK_RAW,
  977. .protocol = IPPROTO_IP, /* wild card */
  978. .prot = &raw_prot,
  979. .ops = &inet_sockraw_ops,
  980. .flags = INET_PROTOSW_REUSE,
  981. }
  982. };
  983. #define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array)
  984. void inet_register_protosw(struct inet_protosw *p)
  985. {
  986. struct list_head *lh;
  987. struct inet_protosw *answer;
  988. int protocol = p->protocol;
  989. struct list_head *last_perm;
  990. spin_lock_bh(&inetsw_lock);
  991. if (p->type >= SOCK_MAX)
  992. goto out_illegal;
  993. /* If we are trying to override a permanent protocol, bail. */
  994. last_perm = &inetsw[p->type];
  995. list_for_each(lh, &inetsw[p->type]) {
  996. answer = list_entry(lh, struct inet_protosw, list);
  997. /* Check only the non-wild match. */
  998. if ((INET_PROTOSW_PERMANENT & answer->flags) == 0)
  999. break;
  1000. if (protocol == answer->protocol)
  1001. goto out_permanent;
  1002. last_perm = lh;
  1003. }
  1004. /* Add the new entry after the last permanent entry if any, so that
  1005. * the new entry does not override a permanent entry when matched with
  1006. * a wild-card protocol. But it is allowed to override any existing
  1007. * non-permanent entry. This means that when we remove this entry, the
  1008. * system automatically returns to the old behavior.
  1009. */
  1010. list_add_rcu(&p->list, last_perm);
  1011. out:
  1012. spin_unlock_bh(&inetsw_lock);
  1013. return;
  1014. out_permanent:
  1015. pr_err("Attempt to override permanent protocol %d\n", protocol);
  1016. goto out;
  1017. out_illegal:
  1018. pr_err("Ignoring attempt to register invalid socket type %d\n",
  1019. p->type);
  1020. goto out;
  1021. }
  1022. EXPORT_SYMBOL(inet_register_protosw);
  1023. void inet_unregister_protosw(struct inet_protosw *p)
  1024. {
  1025. if (INET_PROTOSW_PERMANENT & p->flags) {
  1026. pr_err("Attempt to unregister permanent protocol %d\n",
  1027. p->protocol);
  1028. } else {
  1029. spin_lock_bh(&inetsw_lock);
  1030. list_del_rcu(&p->list);
  1031. spin_unlock_bh(&inetsw_lock);
  1032. synchronize_net();
  1033. }
  1034. }
  1035. EXPORT_SYMBOL(inet_unregister_protosw);
  1036. static int inet_sk_reselect_saddr(struct sock *sk)
  1037. {
  1038. struct inet_sock *inet = inet_sk(sk);
  1039. __be32 old_saddr = inet->inet_saddr;
  1040. __be32 daddr = inet->inet_daddr;
  1041. struct flowi4 *fl4;
  1042. struct rtable *rt;
  1043. __be32 new_saddr;
  1044. struct ip_options_rcu *inet_opt;
  1045. inet_opt = rcu_dereference_protected(inet->inet_opt,
  1046. lockdep_sock_is_held(sk));
  1047. if (inet_opt && inet_opt->opt.srr)
  1048. daddr = inet_opt->opt.faddr;
  1049. /* Query new route. */
  1050. fl4 = &inet->cork.fl.u.ip4;
  1051. rt = ip_route_connect(fl4, daddr, 0, RT_CONN_FLAGS(sk),
  1052. sk->sk_bound_dev_if, sk->sk_protocol,
  1053. inet->inet_sport, inet->inet_dport, sk);
  1054. if (IS_ERR(rt))
  1055. return PTR_ERR(rt);
  1056. sk_setup_caps(sk, &rt->dst);
  1057. new_saddr = fl4->saddr;
  1058. if (new_saddr == old_saddr)
  1059. return 0;
  1060. if (sock_net(sk)->ipv4.sysctl_ip_dynaddr > 1) {
  1061. pr_info("%s(): shifting inet->saddr from %pI4 to %pI4\n",
  1062. __func__, &old_saddr, &new_saddr);
  1063. }
  1064. inet->inet_saddr = inet->inet_rcv_saddr = new_saddr;
  1065. /*
  1066. * XXX The only one ugly spot where we need to
  1067. * XXX really change the sockets identity after
  1068. * XXX it has entered the hashes. -DaveM
  1069. *
  1070. * Besides that, it does not check for connection
  1071. * uniqueness. Wait for troubles.
  1072. */
  1073. return __sk_prot_rehash(sk);
  1074. }
  1075. int inet_sk_rebuild_header(struct sock *sk)
  1076. {
  1077. struct inet_sock *inet = inet_sk(sk);
  1078. struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
  1079. __be32 daddr;
  1080. struct ip_options_rcu *inet_opt;
  1081. struct flowi4 *fl4;
  1082. int err;
  1083. /* Route is OK, nothing to do. */
  1084. if (rt)
  1085. return 0;
  1086. /* Reroute. */
  1087. rcu_read_lock();
  1088. inet_opt = rcu_dereference(inet->inet_opt);
  1089. daddr = inet->inet_daddr;
  1090. if (inet_opt && inet_opt->opt.srr)
  1091. daddr = inet_opt->opt.faddr;
  1092. rcu_read_unlock();
  1093. fl4 = &inet->cork.fl.u.ip4;
  1094. rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr, inet->inet_saddr,
  1095. inet->inet_dport, inet->inet_sport,
  1096. sk->sk_protocol, RT_CONN_FLAGS(sk),
  1097. sk->sk_bound_dev_if);
  1098. if (!IS_ERR(rt)) {
  1099. err = 0;
  1100. sk_setup_caps(sk, &rt->dst);
  1101. } else {
  1102. err = PTR_ERR(rt);
  1103. /* Routing failed... */
  1104. sk->sk_route_caps = 0;
  1105. /*
  1106. * Other protocols have to map its equivalent state to TCP_SYN_SENT.
  1107. * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme
  1108. */
  1109. if (!sock_net(sk)->ipv4.sysctl_ip_dynaddr ||
  1110. sk->sk_state != TCP_SYN_SENT ||
  1111. (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
  1112. (err = inet_sk_reselect_saddr(sk)) != 0)
  1113. sk->sk_err_soft = -err;
  1114. }
  1115. return err;
  1116. }
  1117. EXPORT_SYMBOL(inet_sk_rebuild_header);
  1118. void inet_sk_set_state(struct sock *sk, int state)
  1119. {
  1120. trace_inet_sock_set_state(sk, sk->sk_state, state);
  1121. sk->sk_state = state;
  1122. }
  1123. EXPORT_SYMBOL(inet_sk_set_state);
  1124. void inet_sk_state_store(struct sock *sk, int newstate)
  1125. {
  1126. trace_inet_sock_set_state(sk, sk->sk_state, newstate);
  1127. smp_store_release(&sk->sk_state, newstate);
  1128. }
  1129. struct sk_buff *inet_gso_segment(struct sk_buff *skb,
  1130. netdev_features_t features)
  1131. {
  1132. bool udpfrag = false, fixedid = false, gso_partial, encap;
  1133. struct sk_buff *segs = ERR_PTR(-EINVAL);
  1134. const struct net_offload *ops;
  1135. unsigned int offset = 0;
  1136. struct iphdr *iph;
  1137. int proto, tot_len;
  1138. int nhoff;
  1139. int ihl;
  1140. int id;
  1141. skb_reset_network_header(skb);
  1142. nhoff = skb_network_header(skb) - skb_mac_header(skb);
  1143. if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
  1144. goto out;
  1145. iph = ip_hdr(skb);
  1146. ihl = iph->ihl * 4;
  1147. if (ihl < sizeof(*iph))
  1148. goto out;
  1149. id = ntohs(iph->id);
  1150. proto = iph->protocol;
  1151. /* Warning: after this point, iph might be no longer valid */
  1152. if (unlikely(!pskb_may_pull(skb, ihl)))
  1153. goto out;
  1154. __skb_pull(skb, ihl);
  1155. encap = SKB_GSO_CB(skb)->encap_level > 0;
  1156. if (encap)
  1157. features &= skb->dev->hw_enc_features;
  1158. SKB_GSO_CB(skb)->encap_level += ihl;
  1159. skb_reset_transport_header(skb);
  1160. segs = ERR_PTR(-EPROTONOSUPPORT);
  1161. if (!skb->encapsulation || encap) {
  1162. udpfrag = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP);
  1163. fixedid = !!(skb_shinfo(skb)->gso_type & SKB_GSO_TCP_FIXEDID);
  1164. /* fixed ID is invalid if DF bit is not set */
  1165. if (fixedid && !(ip_hdr(skb)->frag_off & htons(IP_DF)))
  1166. goto out;
  1167. }
  1168. ops = rcu_dereference(inet_offloads[proto]);
  1169. if (likely(ops && ops->callbacks.gso_segment))
  1170. segs = ops->callbacks.gso_segment(skb, features);
  1171. if (IS_ERR_OR_NULL(segs))
  1172. goto out;
  1173. gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
  1174. skb = segs;
  1175. do {
  1176. iph = (struct iphdr *)(skb_mac_header(skb) + nhoff);
  1177. if (udpfrag) {
  1178. iph->frag_off = htons(offset >> 3);
  1179. if (skb->next)
  1180. iph->frag_off |= htons(IP_MF);
  1181. offset += skb->len - nhoff - ihl;
  1182. tot_len = skb->len - nhoff;
  1183. } else if (skb_is_gso(skb)) {
  1184. if (!fixedid) {
  1185. iph->id = htons(id);
  1186. id += skb_shinfo(skb)->gso_segs;
  1187. }
  1188. if (gso_partial)
  1189. tot_len = skb_shinfo(skb)->gso_size +
  1190. SKB_GSO_CB(skb)->data_offset +
  1191. skb->head - (unsigned char *)iph;
  1192. else
  1193. tot_len = skb->len - nhoff;
  1194. } else {
  1195. if (!fixedid)
  1196. iph->id = htons(id++);
  1197. tot_len = skb->len - nhoff;
  1198. }
  1199. iph->tot_len = htons(tot_len);
  1200. ip_send_check(iph);
  1201. if (encap)
  1202. skb_reset_inner_headers(skb);
  1203. skb->network_header = (u8 *)iph - skb->head;
  1204. skb_reset_mac_len(skb);
  1205. } while ((skb = skb->next));
  1206. out:
  1207. return segs;
  1208. }
  1209. EXPORT_SYMBOL(inet_gso_segment);
  1210. struct sk_buff *inet_gro_receive(struct list_head *head, struct sk_buff *skb)
  1211. {
  1212. const struct net_offload *ops;
  1213. struct sk_buff *pp = NULL;
  1214. const struct iphdr *iph;
  1215. struct sk_buff *p;
  1216. unsigned int hlen;
  1217. unsigned int off;
  1218. unsigned int id;
  1219. int flush = 1;
  1220. int proto;
  1221. off = skb_gro_offset(skb);
  1222. hlen = off + sizeof(*iph);
  1223. iph = skb_gro_header_fast(skb, off);
  1224. if (skb_gro_header_hard(skb, hlen)) {
  1225. iph = skb_gro_header_slow(skb, hlen, off);
  1226. if (unlikely(!iph))
  1227. goto out;
  1228. }
  1229. proto = iph->protocol;
  1230. rcu_read_lock();
  1231. ops = rcu_dereference(inet_offloads[proto]);
  1232. if (!ops || !ops->callbacks.gro_receive)
  1233. goto out_unlock;
  1234. if (*(u8 *)iph != 0x45)
  1235. goto out_unlock;
  1236. if (ip_is_fragment(iph))
  1237. goto out_unlock;
  1238. if (unlikely(ip_fast_csum((u8 *)iph, 5)))
  1239. goto out_unlock;
  1240. id = ntohl(*(__be32 *)&iph->id);
  1241. flush = (u16)((ntohl(*(__be32 *)iph) ^ skb_gro_len(skb)) | (id & ~IP_DF));
  1242. id >>= 16;
  1243. list_for_each_entry(p, head, list) {
  1244. struct iphdr *iph2;
  1245. u16 flush_id;
  1246. if (!NAPI_GRO_CB(p)->same_flow)
  1247. continue;
  1248. iph2 = (struct iphdr *)(p->data + off);
  1249. /* The above works because, with the exception of the top
  1250. * (inner most) layer, we only aggregate pkts with the same
  1251. * hdr length so all the hdrs we'll need to verify will start
  1252. * at the same offset.
  1253. */
  1254. if ((iph->protocol ^ iph2->protocol) |
  1255. ((__force u32)iph->saddr ^ (__force u32)iph2->saddr) |
  1256. ((__force u32)iph->daddr ^ (__force u32)iph2->daddr)) {
  1257. NAPI_GRO_CB(p)->same_flow = 0;
  1258. continue;
  1259. }
  1260. /* All fields must match except length and checksum. */
  1261. NAPI_GRO_CB(p)->flush |=
  1262. (iph->ttl ^ iph2->ttl) |
  1263. (iph->tos ^ iph2->tos) |
  1264. ((iph->frag_off ^ iph2->frag_off) & htons(IP_DF));
  1265. NAPI_GRO_CB(p)->flush |= flush;
  1266. /* We need to store of the IP ID check to be included later
  1267. * when we can verify that this packet does in fact belong
  1268. * to a given flow.
  1269. */
  1270. flush_id = (u16)(id - ntohs(iph2->id));
  1271. /* This bit of code makes it much easier for us to identify
  1272. * the cases where we are doing atomic vs non-atomic IP ID
  1273. * checks. Specifically an atomic check can return IP ID
  1274. * values 0 - 0xFFFF, while a non-atomic check can only
  1275. * return 0 or 0xFFFF.
  1276. */
  1277. if (!NAPI_GRO_CB(p)->is_atomic ||
  1278. !(iph->frag_off & htons(IP_DF))) {
  1279. flush_id ^= NAPI_GRO_CB(p)->count;
  1280. flush_id = flush_id ? 0xFFFF : 0;
  1281. }
  1282. /* If the previous IP ID value was based on an atomic
  1283. * datagram we can overwrite the value and ignore it.
  1284. */
  1285. if (NAPI_GRO_CB(skb)->is_atomic)
  1286. NAPI_GRO_CB(p)->flush_id = flush_id;
  1287. else
  1288. NAPI_GRO_CB(p)->flush_id |= flush_id;
  1289. }
  1290. NAPI_GRO_CB(skb)->is_atomic = !!(iph->frag_off & htons(IP_DF));
  1291. NAPI_GRO_CB(skb)->flush |= flush;
  1292. skb_set_network_header(skb, off);
  1293. /* The above will be needed by the transport layer if there is one
  1294. * immediately following this IP hdr.
  1295. */
  1296. /* Note : No need to call skb_gro_postpull_rcsum() here,
  1297. * as we already checked checksum over ipv4 header was 0
  1298. */
  1299. skb_gro_pull(skb, sizeof(*iph));
  1300. skb_set_transport_header(skb, skb_gro_offset(skb));
  1301. pp = call_gro_receive(ops->callbacks.gro_receive, head, skb);
  1302. out_unlock:
  1303. rcu_read_unlock();
  1304. out:
  1305. skb_gro_flush_final(skb, pp, flush);
  1306. return pp;
  1307. }
  1308. EXPORT_SYMBOL(inet_gro_receive);
  1309. static struct sk_buff *ipip_gro_receive(struct list_head *head,
  1310. struct sk_buff *skb)
  1311. {
  1312. if (NAPI_GRO_CB(skb)->encap_mark) {
  1313. NAPI_GRO_CB(skb)->flush = 1;
  1314. return NULL;
  1315. }
  1316. NAPI_GRO_CB(skb)->encap_mark = 1;
  1317. return inet_gro_receive(head, skb);
  1318. }
  1319. #define SECONDS_PER_DAY 86400
  1320. /* inet_current_timestamp - Return IP network timestamp
  1321. *
  1322. * Return milliseconds since midnight in network byte order.
  1323. */
  1324. __be32 inet_current_timestamp(void)
  1325. {
  1326. u32 secs;
  1327. u32 msecs;
  1328. struct timespec64 ts;
  1329. ktime_get_real_ts64(&ts);
  1330. /* Get secs since midnight. */
  1331. (void)div_u64_rem(ts.tv_sec, SECONDS_PER_DAY, &secs);
  1332. /* Convert to msecs. */
  1333. msecs = secs * MSEC_PER_SEC;
  1334. /* Convert nsec to msec. */
  1335. msecs += (u32)ts.tv_nsec / NSEC_PER_MSEC;
  1336. /* Convert to network byte order. */
  1337. return htonl(msecs);
  1338. }
  1339. EXPORT_SYMBOL(inet_current_timestamp);
  1340. int inet_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
  1341. {
  1342. if (sk->sk_family == AF_INET)
  1343. return ip_recv_error(sk, msg, len, addr_len);
  1344. #if IS_ENABLED(CONFIG_IPV6)
  1345. if (sk->sk_family == AF_INET6)
  1346. return pingv6_ops.ipv6_recv_error(sk, msg, len, addr_len);
  1347. #endif
  1348. return -EINVAL;
  1349. }
  1350. int inet_gro_complete(struct sk_buff *skb, int nhoff)
  1351. {
  1352. __be16 newlen = htons(skb->len - nhoff);
  1353. struct iphdr *iph = (struct iphdr *)(skb->data + nhoff);
  1354. const struct net_offload *ops;
  1355. int proto = iph->protocol;
  1356. int err = -ENOSYS;
  1357. if (skb->encapsulation) {
  1358. skb_set_inner_protocol(skb, cpu_to_be16(ETH_P_IP));
  1359. skb_set_inner_network_header(skb, nhoff);
  1360. }
  1361. csum_replace2(&iph->check, iph->tot_len, newlen);
  1362. iph->tot_len = newlen;
  1363. rcu_read_lock();
  1364. ops = rcu_dereference(inet_offloads[proto]);
  1365. if (WARN_ON(!ops || !ops->callbacks.gro_complete))
  1366. goto out_unlock;
  1367. /* Only need to add sizeof(*iph) to get to the next hdr below
  1368. * because any hdr with option will have been flushed in
  1369. * inet_gro_receive().
  1370. */
  1371. err = ops->callbacks.gro_complete(skb, nhoff + sizeof(*iph));
  1372. out_unlock:
  1373. rcu_read_unlock();
  1374. return err;
  1375. }
  1376. EXPORT_SYMBOL(inet_gro_complete);
  1377. static int ipip_gro_complete(struct sk_buff *skb, int nhoff)
  1378. {
  1379. skb->encapsulation = 1;
  1380. skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP4;
  1381. return inet_gro_complete(skb, nhoff);
  1382. }
  1383. int inet_ctl_sock_create(struct sock **sk, unsigned short family,
  1384. unsigned short type, unsigned char protocol,
  1385. struct net *net)
  1386. {
  1387. struct socket *sock;
  1388. int rc = sock_create_kern(net, family, type, protocol, &sock);
  1389. if (rc == 0) {
  1390. *sk = sock->sk;
  1391. (*sk)->sk_allocation = GFP_ATOMIC;
  1392. /*
  1393. * Unhash it so that IP input processing does not even see it,
  1394. * we do not wish this socket to see incoming packets.
  1395. */
  1396. (*sk)->sk_prot->unhash(*sk);
  1397. }
  1398. return rc;
  1399. }
  1400. EXPORT_SYMBOL_GPL(inet_ctl_sock_create);
  1401. u64 snmp_get_cpu_field(void __percpu *mib, int cpu, int offt)
  1402. {
  1403. return *(((unsigned long *)per_cpu_ptr(mib, cpu)) + offt);
  1404. }
  1405. EXPORT_SYMBOL_GPL(snmp_get_cpu_field);
  1406. unsigned long snmp_fold_field(void __percpu *mib, int offt)
  1407. {
  1408. unsigned long res = 0;
  1409. int i;
  1410. for_each_possible_cpu(i)
  1411. res += snmp_get_cpu_field(mib, i, offt);
  1412. return res;
  1413. }
  1414. EXPORT_SYMBOL_GPL(snmp_fold_field);
  1415. #if BITS_PER_LONG==32
  1416. u64 snmp_get_cpu_field64(void __percpu *mib, int cpu, int offt,
  1417. size_t syncp_offset)
  1418. {
  1419. void *bhptr;
  1420. struct u64_stats_sync *syncp;
  1421. u64 v;
  1422. unsigned int start;
  1423. bhptr = per_cpu_ptr(mib, cpu);
  1424. syncp = (struct u64_stats_sync *)(bhptr + syncp_offset);
  1425. do {
  1426. start = u64_stats_fetch_begin_irq(syncp);
  1427. v = *(((u64 *)bhptr) + offt);
  1428. } while (u64_stats_fetch_retry_irq(syncp, start));
  1429. return v;
  1430. }
  1431. EXPORT_SYMBOL_GPL(snmp_get_cpu_field64);
  1432. u64 snmp_fold_field64(void __percpu *mib, int offt, size_t syncp_offset)
  1433. {
  1434. u64 res = 0;
  1435. int cpu;
  1436. for_each_possible_cpu(cpu) {
  1437. res += snmp_get_cpu_field64(mib, cpu, offt, syncp_offset);
  1438. }
  1439. return res;
  1440. }
  1441. EXPORT_SYMBOL_GPL(snmp_fold_field64);
  1442. #endif
  1443. #ifdef CONFIG_IP_MULTICAST
  1444. static const struct net_protocol igmp_protocol = {
  1445. .handler = igmp_rcv,
  1446. .netns_ok = 1,
  1447. };
  1448. #endif
  1449. /* thinking of making this const? Don't.
  1450. * early_demux can change based on sysctl.
  1451. */
  1452. static struct net_protocol tcp_protocol = {
  1453. .early_demux = tcp_v4_early_demux,
  1454. .early_demux_handler = tcp_v4_early_demux,
  1455. .handler = tcp_v4_rcv,
  1456. .err_handler = tcp_v4_err,
  1457. .no_policy = 1,
  1458. .netns_ok = 1,
  1459. .icmp_strict_tag_validation = 1,
  1460. };
  1461. /* thinking of making this const? Don't.
  1462. * early_demux can change based on sysctl.
  1463. */
  1464. static struct net_protocol udp_protocol = {
  1465. .early_demux = udp_v4_early_demux,
  1466. .early_demux_handler = udp_v4_early_demux,
  1467. .handler = udp_rcv,
  1468. .err_handler = udp_err,
  1469. .no_policy = 1,
  1470. .netns_ok = 1,
  1471. };
  1472. static const struct net_protocol icmp_protocol = {
  1473. .handler = icmp_rcv,
  1474. .err_handler = icmp_err,
  1475. .no_policy = 1,
  1476. .netns_ok = 1,
  1477. };
  1478. static __net_init int ipv4_mib_init_net(struct net *net)
  1479. {
  1480. int i;
  1481. net->mib.tcp_statistics = alloc_percpu(struct tcp_mib);
  1482. if (!net->mib.tcp_statistics)
  1483. goto err_tcp_mib;
  1484. net->mib.ip_statistics = alloc_percpu(struct ipstats_mib);
  1485. if (!net->mib.ip_statistics)
  1486. goto err_ip_mib;
  1487. for_each_possible_cpu(i) {
  1488. struct ipstats_mib *af_inet_stats;
  1489. af_inet_stats = per_cpu_ptr(net->mib.ip_statistics, i);
  1490. u64_stats_init(&af_inet_stats->syncp);
  1491. }
  1492. net->mib.net_statistics = alloc_percpu(struct linux_mib);
  1493. if (!net->mib.net_statistics)
  1494. goto err_net_mib;
  1495. net->mib.udp_statistics = alloc_percpu(struct udp_mib);
  1496. if (!net->mib.udp_statistics)
  1497. goto err_udp_mib;
  1498. net->mib.udplite_statistics = alloc_percpu(struct udp_mib);
  1499. if (!net->mib.udplite_statistics)
  1500. goto err_udplite_mib;
  1501. net->mib.icmp_statistics = alloc_percpu(struct icmp_mib);
  1502. if (!net->mib.icmp_statistics)
  1503. goto err_icmp_mib;
  1504. net->mib.icmpmsg_statistics = kzalloc(sizeof(struct icmpmsg_mib),
  1505. GFP_KERNEL);
  1506. if (!net->mib.icmpmsg_statistics)
  1507. goto err_icmpmsg_mib;
  1508. tcp_mib_init(net);
  1509. return 0;
  1510. err_icmpmsg_mib:
  1511. free_percpu(net->mib.icmp_statistics);
  1512. err_icmp_mib:
  1513. free_percpu(net->mib.udplite_statistics);
  1514. err_udplite_mib:
  1515. free_percpu(net->mib.udp_statistics);
  1516. err_udp_mib:
  1517. free_percpu(net->mib.net_statistics);
  1518. err_net_mib:
  1519. free_percpu(net->mib.ip_statistics);
  1520. err_ip_mib:
  1521. free_percpu(net->mib.tcp_statistics);
  1522. err_tcp_mib:
  1523. return -ENOMEM;
  1524. }
  1525. static __net_exit void ipv4_mib_exit_net(struct net *net)
  1526. {
  1527. kfree(net->mib.icmpmsg_statistics);
  1528. free_percpu(net->mib.icmp_statistics);
  1529. free_percpu(net->mib.udplite_statistics);
  1530. free_percpu(net->mib.udp_statistics);
  1531. free_percpu(net->mib.net_statistics);
  1532. free_percpu(net->mib.ip_statistics);
  1533. free_percpu(net->mib.tcp_statistics);
  1534. }
  1535. static __net_initdata struct pernet_operations ipv4_mib_ops = {
  1536. .init = ipv4_mib_init_net,
  1537. .exit = ipv4_mib_exit_net,
  1538. };
  1539. static int __init init_ipv4_mibs(void)
  1540. {
  1541. return register_pernet_subsys(&ipv4_mib_ops);
  1542. }
  1543. static __net_init int inet_init_net(struct net *net)
  1544. {
  1545. /*
  1546. * Set defaults for local port range
  1547. */
  1548. seqlock_init(&net->ipv4.ip_local_ports.lock);
  1549. net->ipv4.ip_local_ports.range[0] = 32768;
  1550. net->ipv4.ip_local_ports.range[1] = 60999;
  1551. seqlock_init(&net->ipv4.ping_group_range.lock);
  1552. /*
  1553. * Sane defaults - nobody may create ping sockets.
  1554. * Boot scripts should set this to distro-specific group.
  1555. */
  1556. net->ipv4.ping_group_range.range[0] = make_kgid(&init_user_ns, 1);
  1557. net->ipv4.ping_group_range.range[1] = make_kgid(&init_user_ns, 0);
  1558. /* Default values for sysctl-controlled parameters.
  1559. * We set them here, in case sysctl is not compiled.
  1560. */
  1561. net->ipv4.sysctl_ip_default_ttl = IPDEFTTL;
  1562. net->ipv4.sysctl_ip_fwd_update_priority = 1;
  1563. net->ipv4.sysctl_ip_dynaddr = 0;
  1564. net->ipv4.sysctl_ip_early_demux = 1;
  1565. net->ipv4.sysctl_udp_early_demux = 1;
  1566. net->ipv4.sysctl_tcp_early_demux = 1;
  1567. #ifdef CONFIG_SYSCTL
  1568. net->ipv4.sysctl_ip_prot_sock = PROT_SOCK;
  1569. #endif
  1570. /* Some igmp sysctl, whose values are always used */
  1571. net->ipv4.sysctl_igmp_max_memberships = 20;
  1572. net->ipv4.sysctl_igmp_max_msf = 10;
  1573. /* IGMP reports for link-local multicast groups are enabled by default */
  1574. net->ipv4.sysctl_igmp_llm_reports = 1;
  1575. net->ipv4.sysctl_igmp_qrv = 2;
  1576. return 0;
  1577. }
  1578. static __net_exit void inet_exit_net(struct net *net)
  1579. {
  1580. }
  1581. static __net_initdata struct pernet_operations af_inet_ops = {
  1582. .init = inet_init_net,
  1583. .exit = inet_exit_net,
  1584. };
  1585. static int __init init_inet_pernet_ops(void)
  1586. {
  1587. return register_pernet_subsys(&af_inet_ops);
  1588. }
  1589. static int ipv4_proc_init(void);
  1590. /*
  1591. * IP protocol layer initialiser
  1592. */
  1593. static struct packet_offload ip_packet_offload __read_mostly = {
  1594. .type = cpu_to_be16(ETH_P_IP),
  1595. .callbacks = {
  1596. .gso_segment = inet_gso_segment,
  1597. .gro_receive = inet_gro_receive,
  1598. .gro_complete = inet_gro_complete,
  1599. },
  1600. };
  1601. static const struct net_offload ipip_offload = {
  1602. .callbacks = {
  1603. .gso_segment = inet_gso_segment,
  1604. .gro_receive = ipip_gro_receive,
  1605. .gro_complete = ipip_gro_complete,
  1606. },
  1607. };
  1608. static int __init ipip_offload_init(void)
  1609. {
  1610. return inet_add_offload(&ipip_offload, IPPROTO_IPIP);
  1611. }
  1612. static int __init ipv4_offload_init(void)
  1613. {
  1614. /*
  1615. * Add offloads
  1616. */
  1617. if (udpv4_offload_init() < 0)
  1618. pr_crit("%s: Cannot add UDP protocol offload\n", __func__);
  1619. if (tcpv4_offload_init() < 0)
  1620. pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
  1621. if (ipip_offload_init() < 0)
  1622. pr_crit("%s: Cannot add IPIP protocol offload\n", __func__);
  1623. dev_add_offload(&ip_packet_offload);
  1624. return 0;
  1625. }
  1626. fs_initcall(ipv4_offload_init);
  1627. static struct packet_type ip_packet_type __read_mostly = {
  1628. .type = cpu_to_be16(ETH_P_IP),
  1629. .func = ip_rcv,
  1630. .list_func = ip_list_rcv,
  1631. };
  1632. static int __init inet_init(void)
  1633. {
  1634. struct inet_protosw *q;
  1635. struct list_head *r;
  1636. int rc = -EINVAL;
  1637. sock_skb_cb_check_size(sizeof(struct inet_skb_parm));
  1638. rc = proto_register(&tcp_prot, 1);
  1639. if (rc)
  1640. goto out;
  1641. rc = proto_register(&udp_prot, 1);
  1642. if (rc)
  1643. goto out_unregister_tcp_proto;
  1644. rc = proto_register(&raw_prot, 1);
  1645. if (rc)
  1646. goto out_unregister_udp_proto;
  1647. rc = proto_register(&ping_prot, 1);
  1648. if (rc)
  1649. goto out_unregister_raw_proto;
  1650. /*
  1651. * Tell SOCKET that we are alive...
  1652. */
  1653. (void)sock_register(&inet_family_ops);
  1654. #ifdef CONFIG_SYSCTL
  1655. ip_static_sysctl_init();
  1656. #endif
  1657. /*
  1658. * Add all the base protocols.
  1659. */
  1660. if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
  1661. pr_crit("%s: Cannot add ICMP protocol\n", __func__);
  1662. if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
  1663. pr_crit("%s: Cannot add UDP protocol\n", __func__);
  1664. if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
  1665. pr_crit("%s: Cannot add TCP protocol\n", __func__);
  1666. #ifdef CONFIG_IP_MULTICAST
  1667. if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
  1668. pr_crit("%s: Cannot add IGMP protocol\n", __func__);
  1669. #endif
  1670. /* Register the socket-side information for inet_create. */
  1671. for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
  1672. INIT_LIST_HEAD(r);
  1673. for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
  1674. inet_register_protosw(q);
  1675. /*
  1676. * Set the ARP module up
  1677. */
  1678. arp_init();
  1679. /*
  1680. * Set the IP module up
  1681. */
  1682. ip_init();
  1683. /* Setup TCP slab cache for open requests. */
  1684. tcp_init();
  1685. /* Setup UDP memory threshold */
  1686. udp_init();
  1687. /* Add UDP-Lite (RFC 3828) */
  1688. udplite4_register();
  1689. ping_init();
  1690. /*
  1691. * Set the ICMP layer up
  1692. */
  1693. if (icmp_init() < 0)
  1694. panic("Failed to create the ICMP control socket.\n");
  1695. /*
  1696. * Initialise the multicast router
  1697. */
  1698. #if defined(CONFIG_IP_MROUTE)
  1699. if (ip_mr_init())
  1700. pr_crit("%s: Cannot init ipv4 mroute\n", __func__);
  1701. #endif
  1702. if (init_inet_pernet_ops())
  1703. pr_crit("%s: Cannot init ipv4 inet pernet ops\n", __func__);
  1704. /*
  1705. * Initialise per-cpu ipv4 mibs
  1706. */
  1707. if (init_ipv4_mibs())
  1708. pr_crit("%s: Cannot init ipv4 mibs\n", __func__);
  1709. ipv4_proc_init();
  1710. ipfrag_init();
  1711. dev_add_pack(&ip_packet_type);
  1712. ip_tunnel_core_init();
  1713. rc = 0;
  1714. out:
  1715. return rc;
  1716. out_unregister_raw_proto:
  1717. proto_unregister(&raw_prot);
  1718. out_unregister_udp_proto:
  1719. proto_unregister(&udp_prot);
  1720. out_unregister_tcp_proto:
  1721. proto_unregister(&tcp_prot);
  1722. goto out;
  1723. }
  1724. fs_initcall(inet_init);
  1725. /* ------------------------------------------------------------------------ */
  1726. #ifdef CONFIG_PROC_FS
  1727. static int __init ipv4_proc_init(void)
  1728. {
  1729. int rc = 0;
  1730. if (raw_proc_init())
  1731. goto out_raw;
  1732. if (tcp4_proc_init())
  1733. goto out_tcp;
  1734. if (udp4_proc_init())
  1735. goto out_udp;
  1736. if (ping_proc_init())
  1737. goto out_ping;
  1738. if (ip_misc_proc_init())
  1739. goto out_misc;
  1740. out:
  1741. return rc;
  1742. out_misc:
  1743. ping_proc_exit();
  1744. out_ping:
  1745. udp4_proc_exit();
  1746. out_udp:
  1747. tcp4_proc_exit();
  1748. out_tcp:
  1749. raw_proc_exit();
  1750. out_raw:
  1751. rc = -ENOMEM;
  1752. goto out;
  1753. }
  1754. #else /* CONFIG_PROC_FS */
  1755. static int __init ipv4_proc_init(void)
  1756. {
  1757. return 0;
  1758. }
  1759. #endif /* CONFIG_PROC_FS */