af_inet.c 51 KB

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